PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
acl.h File Reference
#include "access/htup.h"
#include "nodes/parsenodes.h"
#include "parser/parse_node.h"
#include "utils/array.h"
#include "utils/snapshot.h"
Include dependency graph for acl.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  AclItem
 

Macros

#define ACL_ID_PUBLIC   0 /* placeholder for id in a PUBLIC acl item */
 
#define ACLITEM_GET_PRIVS(item)   ((item).ai_privs & 0xFFFF)
 
#define ACLITEM_GET_GOPTIONS(item)   (((item).ai_privs >> 16) & 0xFFFF)
 
#define ACLITEM_GET_RIGHTS(item)   ((item).ai_privs)
 
#define ACL_GRANT_OPTION_FOR(privs)   (((AclMode) (privs) & 0xFFFF) << 16)
 
#define ACL_OPTION_TO_PRIVS(privs)   (((AclMode) (privs) >> 16) & 0xFFFF)
 
#define ACLITEM_SET_PRIVS(item, privs)
 
#define ACLITEM_SET_GOPTIONS(item, goptions)
 
#define ACLITEM_SET_RIGHTS(item, rights)   ((item).ai_privs = (AclMode) (rights))
 
#define ACLITEM_SET_PRIVS_GOPTIONS(item, privs, goptions)
 
#define ACLITEM_ALL_PRIV_BITS   ((AclMode) 0xFFFF)
 
#define ACLITEM_ALL_GOPTION_BITS   ((AclMode) 0xFFFF << 16)
 
#define ACL_NUM(ACL)   (ARR_DIMS(ACL)[0])
 
#define ACL_DAT(ACL)   ((AclItem *) ARR_DATA_PTR(ACL))
 
#define ACL_N_SIZE(N)   (ARR_OVERHEAD_NONULLS(1) + ((N) * sizeof(AclItem)))
 
#define ACL_SIZE(ACL)   ARR_SIZE(ACL)
 
#define DatumGetAclItemP(X)   ((AclItem *) DatumGetPointer(X))
 
#define PG_GETARG_ACLITEM_P(n)   DatumGetAclItemP(PG_GETARG_DATUM(n))
 
#define PG_RETURN_ACLITEM_P(x)   PG_RETURN_POINTER(x)
 
#define DatumGetAclP(X)   ((Acl *) PG_DETOAST_DATUM(X))
 
#define DatumGetAclPCopy(X)   ((Acl *) PG_DETOAST_DATUM_COPY(X))
 
#define PG_GETARG_ACL_P(n)   DatumGetAclP(PG_GETARG_DATUM(n))
 
#define PG_GETARG_ACL_P_COPY(n)   DatumGetAclPCopy(PG_GETARG_DATUM(n))
 
#define PG_RETURN_ACL_P(x)   PG_RETURN_POINTER(x)
 
#define ACL_MODECHG_ADD   1
 
#define ACL_MODECHG_DEL   2
 
#define ACL_MODECHG_EQL   3
 
#define ACL_INSERT_CHR   'a' /* formerly known as "append" */
 
#define ACL_SELECT_CHR   'r' /* formerly known as "read" */
 
#define ACL_UPDATE_CHR   'w' /* formerly known as "write" */
 
#define ACL_DELETE_CHR   'd'
 
#define ACL_TRUNCATE_CHR   'D' /* super-delete, as it were */
 
#define ACL_REFERENCES_CHR   'x'
 
#define ACL_TRIGGER_CHR   't'
 
#define ACL_EXECUTE_CHR   'X'
 
#define ACL_USAGE_CHR   'U'
 
#define ACL_CREATE_CHR   'C'
 
#define ACL_CREATE_TEMP_CHR   'T'
 
#define ACL_CONNECT_CHR   'c'
 
#define ACL_ALL_RIGHTS_STR   "arwdDxtXUCTc"
 
#define ACL_ALL_RIGHTS_COLUMN   (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_REFERENCES)
 
#define ACL_ALL_RIGHTS_RELATION   (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_DELETE|ACL_TRUNCATE|ACL_REFERENCES|ACL_TRIGGER)
 
#define ACL_ALL_RIGHTS_SEQUENCE   (ACL_USAGE|ACL_SELECT|ACL_UPDATE)
 
#define ACL_ALL_RIGHTS_DATABASE   (ACL_CREATE|ACL_CREATE_TEMP|ACL_CONNECT)
 
#define ACL_ALL_RIGHTS_FDW   (ACL_USAGE)
 
#define ACL_ALL_RIGHTS_FOREIGN_SERVER   (ACL_USAGE)
 
#define ACL_ALL_RIGHTS_FUNCTION   (ACL_EXECUTE)
 
#define ACL_ALL_RIGHTS_LANGUAGE   (ACL_USAGE)
 
#define ACL_ALL_RIGHTS_LARGEOBJECT   (ACL_SELECT|ACL_UPDATE)
 
#define ACL_ALL_RIGHTS_NAMESPACE   (ACL_USAGE|ACL_CREATE)
 
#define ACL_ALL_RIGHTS_TABLESPACE   (ACL_CREATE)
 
#define ACL_ALL_RIGHTS_TYPE   (ACL_USAGE)
 

Typedefs

typedef struct AclItem AclItem
 
typedef ArrayType Acl
 
typedef enum AclObjectKind AclObjectKind
 

Enumerations

enum  AclMaskHow { ACLMASK_ALL, ACLMASK_ANY }
 
enum  AclResult { ACLCHECK_OK = 0, ACLCHECK_NO_PRIV, ACLCHECK_NOT_OWNER }
 
enum  AclObjectKind {
  ACL_KIND_COLUMN, ACL_KIND_CLASS, ACL_KIND_SEQUENCE, ACL_KIND_DATABASE,
  ACL_KIND_PROC, ACL_KIND_OPER, ACL_KIND_TYPE, ACL_KIND_LANGUAGE,
  ACL_KIND_LARGEOBJECT, ACL_KIND_NAMESPACE, ACL_KIND_OPCLASS, ACL_KIND_OPFAMILY,
  ACL_KIND_COLLATION, ACL_KIND_CONVERSION, ACL_KIND_STATISTICS, ACL_KIND_TABLESPACE,
  ACL_KIND_TSDICTIONARY, ACL_KIND_TSCONFIGURATION, ACL_KIND_FDW, ACL_KIND_FOREIGN_SERVER,
  ACL_KIND_EVENT_TRIGGER, ACL_KIND_EXTENSION, ACL_KIND_PUBLICATION, ACL_KIND_SUBSCRIPTION,
  MAX_ACL_KIND
}
 

Functions

Aclacldefault (GrantObjectType objtype, Oid ownerId)
 
Aclget_user_default_acl (GrantObjectType objtype, Oid ownerId, Oid nsp_oid)
 
Aclaclupdate (const Acl *old_acl, const AclItem *mod_aip, int modechg, Oid ownerId, DropBehavior behavior)
 
Aclaclnewowner (const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
 
Aclmake_empty_acl (void)
 
Aclaclcopy (const Acl *orig_acl)
 
Aclaclconcat (const Acl *left_acl, const Acl *right_acl)
 
Aclaclmerge (const Acl *left_acl, const Acl *right_acl, Oid ownerId)
 
void aclitemsort (Acl *acl)
 
bool aclequal (const Acl *left_acl, const Acl *right_acl)
 
AclMode aclmask (const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
 
int aclmembers (const Acl *acl, Oid **roleids)
 
bool has_privs_of_role (Oid member, Oid role)
 
bool is_member_of_role (Oid member, Oid role)
 
bool is_member_of_role_nosuper (Oid member, Oid role)
 
bool is_admin_of_role (Oid member, Oid role)
 
void check_is_member_of_role (Oid member, Oid role)
 
Oid get_role_oid (const char *rolename, bool missing_ok)
 
Oid get_role_oid_or_public (const char *rolename)
 
Oid get_rolespec_oid (const RoleSpec *role, bool missing_ok)
 
void check_rolespec_name (const RoleSpec *role, const char *detail_msg)
 
HeapTuple get_rolespec_tuple (const RoleSpec *role)
 
char * get_rolespec_name (const RoleSpec *role)
 
void select_best_grantor (Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
 
void initialize_acl (void)
 
void ExecuteGrantStmt (GrantStmt *stmt)
 
void ExecAlterDefaultPrivilegesStmt (ParseState *pstate, AlterDefaultPrivilegesStmt *stmt)
 
void RemoveRoleFromObjectACL (Oid roleid, Oid classid, Oid objid)
 
void RemoveDefaultACLById (Oid defaclOid)
 
AclMode pg_attribute_aclmask (Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_class_aclmask (Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_database_aclmask (Oid db_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_proc_aclmask (Oid proc_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_language_aclmask (Oid lang_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_largeobject_aclmask_snapshot (Oid lobj_oid, Oid roleid, AclMode mask, AclMaskHow how, Snapshot snapshot)
 
AclMode pg_namespace_aclmask (Oid nsp_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_tablespace_aclmask (Oid spc_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_foreign_data_wrapper_aclmask (Oid fdw_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_foreign_server_aclmask (Oid srv_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_type_aclmask (Oid type_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclResult pg_attribute_aclcheck (Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode)
 
AclResult pg_attribute_aclcheck_all (Oid table_oid, Oid roleid, AclMode mode, AclMaskHow how)
 
AclResult pg_class_aclcheck (Oid table_oid, Oid roleid, AclMode mode)
 
AclResult pg_database_aclcheck (Oid db_oid, Oid roleid, AclMode mode)
 
AclResult pg_proc_aclcheck (Oid proc_oid, Oid roleid, AclMode mode)
 
AclResult pg_language_aclcheck (Oid lang_oid, Oid roleid, AclMode mode)
 
AclResult pg_largeobject_aclcheck_snapshot (Oid lang_oid, Oid roleid, AclMode mode, Snapshot snapshot)
 
AclResult pg_namespace_aclcheck (Oid nsp_oid, Oid roleid, AclMode mode)
 
AclResult pg_tablespace_aclcheck (Oid spc_oid, Oid roleid, AclMode mode)
 
AclResult pg_foreign_data_wrapper_aclcheck (Oid fdw_oid, Oid roleid, AclMode mode)
 
AclResult pg_foreign_server_aclcheck (Oid srv_oid, Oid roleid, AclMode mode)
 
AclResult pg_type_aclcheck (Oid type_oid, Oid roleid, AclMode mode)
 
void aclcheck_error (AclResult aclerr, AclObjectKind objectkind, const char *objectname)
 
void aclcheck_error_col (AclResult aclerr, AclObjectKind objectkind, const char *objectname, const char *colname)
 
void aclcheck_error_type (AclResult aclerr, Oid typeOid)
 
void recordExtObjInitPriv (Oid objoid, Oid classoid)
 
void removeExtObjInitPriv (Oid objoid, Oid classoid)
 
bool pg_class_ownercheck (Oid class_oid, Oid roleid)
 
bool pg_type_ownercheck (Oid type_oid, Oid roleid)
 
bool pg_oper_ownercheck (Oid oper_oid, Oid roleid)
 
bool pg_proc_ownercheck (Oid proc_oid, Oid roleid)
 
bool pg_language_ownercheck (Oid lan_oid, Oid roleid)
 
bool pg_largeobject_ownercheck (Oid lobj_oid, Oid roleid)
 
bool pg_namespace_ownercheck (Oid nsp_oid, Oid roleid)
 
bool pg_tablespace_ownercheck (Oid spc_oid, Oid roleid)
 
bool pg_opclass_ownercheck (Oid opc_oid, Oid roleid)
 
bool pg_opfamily_ownercheck (Oid opf_oid, Oid roleid)
 
bool pg_database_ownercheck (Oid db_oid, Oid roleid)
 
bool pg_collation_ownercheck (Oid coll_oid, Oid roleid)
 
bool pg_conversion_ownercheck (Oid conv_oid, Oid roleid)
 
bool pg_ts_dict_ownercheck (Oid dict_oid, Oid roleid)
 
bool pg_ts_config_ownercheck (Oid cfg_oid, Oid roleid)
 
bool pg_foreign_data_wrapper_ownercheck (Oid srv_oid, Oid roleid)
 
bool pg_foreign_server_ownercheck (Oid srv_oid, Oid roleid)
 
bool pg_event_trigger_ownercheck (Oid et_oid, Oid roleid)
 
bool pg_extension_ownercheck (Oid ext_oid, Oid roleid)
 
bool pg_publication_ownercheck (Oid pub_oid, Oid roleid)
 
bool pg_subscription_ownercheck (Oid sub_oid, Oid roleid)
 
bool pg_statistics_object_ownercheck (Oid stat_oid, Oid roleid)
 
bool has_createrole_privilege (Oid roleid)
 
bool has_bypassrls_privilege (Oid roleid)
 

Macro Definition Documentation

#define ACL_ALL_RIGHTS_COLUMN   (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_REFERENCES)

Definition at line 157 of file acl.h.

Referenced by ExecGrant_Attribute(), ExecGrant_Relation(), and restrict_and_check_grant().

#define ACL_ALL_RIGHTS_DATABASE   (ACL_CREATE|ACL_CREATE_TEMP|ACL_CONNECT)

Definition at line 160 of file acl.h.

Referenced by acldefault(), ExecGrant_Database(), ExecuteGrantStmt(), and restrict_and_check_grant().

#define ACL_ALL_RIGHTS_FDW   (ACL_USAGE)

Definition at line 161 of file acl.h.

Referenced by acldefault(), ExecGrant_Fdw(), ExecuteGrantStmt(), and restrict_and_check_grant().

#define ACL_ALL_RIGHTS_FOREIGN_SERVER   (ACL_USAGE)
#define ACL_ALL_RIGHTS_FUNCTION   (ACL_EXECUTE)
#define ACL_ALL_RIGHTS_LANGUAGE   (ACL_USAGE)

Definition at line 164 of file acl.h.

Referenced by acldefault(), ExecGrant_Language(), ExecuteGrantStmt(), and restrict_and_check_grant().

#define ACL_ALL_RIGHTS_LARGEOBJECT   (ACL_SELECT|ACL_UPDATE)
#define ACL_ALL_RIGHTS_STR   "arwdDxtXUCTc"

Definition at line 152 of file acl.h.

Referenced by aclitemout(), and aclparse().

#define ACL_ALL_RIGHTS_TABLESPACE   (ACL_CREATE)
#define ACL_ALL_RIGHTS_TYPE   (ACL_USAGE)
#define ACL_CONNECT_CHR   'c'

Definition at line 149 of file acl.h.

Referenced by aclparse().

#define ACL_CREATE_CHR   'C'

Definition at line 147 of file acl.h.

Referenced by aclparse().

#define ACL_CREATE_TEMP_CHR   'T'

Definition at line 148 of file acl.h.

Referenced by aclparse().

#define ACL_DELETE_CHR   'd'
#define ACL_EXECUTE_CHR   'X'

Definition at line 145 of file acl.h.

Referenced by aclparse().

#define ACL_INSERT_CHR   'a' /* formerly known as "append" */
#define ACL_MODECHG_ADD   1

Definition at line 130 of file acl.h.

Referenced by aclmerge(), aclupdate(), and merge_acl_with_grant().

#define ACL_MODECHG_DEL   2

Definition at line 131 of file acl.h.

Referenced by aclupdate(), check_circularity(), merge_acl_with_grant(), and recursive_revoke().

#define ACL_MODECHG_EQL   3

Definition at line 132 of file acl.h.

Referenced by aclupdate().

#define ACL_N_SIZE (   N)    (ARR_OVERHEAD_NONULLS(1) + ((N) * sizeof(AclItem)))

Definition at line 111 of file acl.h.

Referenced by aclnewowner(), aclupdate(), and allocacl().

#define ACL_OPTION_TO_PRIVS (   privs)    (((AclMode) (privs) >> 16) & 0xFFFF)

Definition at line 72 of file acl.h.

Referenced by check_circularity(), recursive_revoke(), and restrict_and_check_grant().

#define ACL_REFERENCES_CHR   'x'

Definition at line 143 of file acl.h.

Referenced by aclparse().

#define ACL_SELECT_CHR   'r' /* formerly known as "read" */
#define ACL_SIZE (   ACL)    ARR_SIZE(ACL)

Definition at line 112 of file acl.h.

Referenced by aclupdate(), and check_circularity().

#define ACL_TRIGGER_CHR   't'

Definition at line 144 of file acl.h.

Referenced by aclparse().

#define ACL_TRUNCATE_CHR   'D' /* super-delete, as it were */

Definition at line 142 of file acl.h.

Referenced by aclparse().

#define ACL_UPDATE_CHR   'w' /* formerly known as "write" */

Definition at line 140 of file acl.h.

Referenced by aclparse(), get_policies_for_relation(), and parse_policy_command().

#define ACL_USAGE_CHR   'U'

Definition at line 146 of file acl.h.

Referenced by aclparse().

#define ACLITEM_ALL_GOPTION_BITS   ((AclMode) 0xFFFF << 16)

Definition at line 89 of file acl.h.

Referenced by aclmask(), and aclmask_direct().

#define ACLITEM_ALL_PRIV_BITS   ((AclMode) 0xFFFF)

Definition at line 88 of file acl.h.

#define ACLITEM_GET_GOPTIONS (   item)    (((item).ai_privs >> 16) & 0xFFFF)

Definition at line 68 of file acl.h.

Referenced by aclexplode(), aclitemout(), aclupdate(), and check_circularity().

#define ACLITEM_GET_PRIVS (   item)    ((item).ai_privs & 0xFFFF)

Definition at line 67 of file acl.h.

Referenced by aclexplode(), aclitemout(), and recursive_revoke().

#define ACLITEM_GET_RIGHTS (   item)    ((item).ai_privs)

Definition at line 69 of file acl.h.

Referenced by aclcontains(), aclnewowner(), and aclupdate().

#define ACLITEM_SET_GOPTIONS (   item,
  goptions 
)
Value:
((item).ai_privs = ((item).ai_privs & ~(((AclMode) 0xFFFF) << 16)) | \
(((AclMode) (goptions) & 0xFFFF) << 16))
uint32 AclMode
Definition: parsenodes.h:70

Definition at line 77 of file acl.h.

#define ACLITEM_SET_PRIVS (   item,
  privs 
)
Value:
((item).ai_privs = ((item).ai_privs & ~((AclMode) 0xFFFF)) | \
((AclMode) (privs) & 0xFFFF))
uint32 AclMode
Definition: parsenodes.h:70

Definition at line 74 of file acl.h.

#define ACLITEM_SET_PRIVS_GOPTIONS (   item,
  privs,
  goptions 
)
Value:
((item).ai_privs = ((AclMode) (privs) & 0xFFFF) | \
(((AclMode) (goptions) & 0xFFFF) << 16))
uint32 AclMode
Definition: parsenodes.h:70

Definition at line 83 of file acl.h.

Referenced by acldefault(), aclparse(), aclupdate(), makeaclitem(), merge_acl_with_grant(), and recursive_revoke().

#define ACLITEM_SET_RIGHTS (   item,
  rights 
)    ((item).ai_privs = (AclMode) (rights))

Definition at line 80 of file acl.h.

Referenced by aclnewowner(), and aclupdate().

#define DatumGetAclItemP (   X)    ((AclItem *) DatumGetPointer(X))

Definition at line 117 of file acl.h.

#define PG_GETARG_ACL_P (   n)    DatumGetAclP(PG_GETARG_DATUM(n))

Definition at line 123 of file acl.h.

Referenced by aclcontains(), and aclexplode().

#define PG_GETARG_ACL_P_COPY (   n)    DatumGetAclPCopy(PG_GETARG_DATUM(n))

Definition at line 124 of file acl.h.

#define PG_GETARG_ACLITEM_P (   n)    DatumGetAclItemP(PG_GETARG_DATUM(n))

Definition at line 118 of file acl.h.

Referenced by aclcontains(), aclitem_eq(), aclitemout(), hash_aclitem(), and hash_aclitem_extended().

#define PG_RETURN_ACL_P (   x)    PG_RETURN_POINTER(x)

Definition at line 125 of file acl.h.

Referenced by acldefault_sql().

#define PG_RETURN_ACLITEM_P (   x)    PG_RETURN_POINTER(x)

Definition at line 119 of file acl.h.

Referenced by aclitemin(), and makeaclitem().

Typedef Documentation

Definition at line 107 of file acl.h.

Enumeration Type Documentation

enum AclMaskHow
Enumerator
ACLMASK_ALL 
ACLMASK_ANY 

Definition at line 171 of file acl.h.

172 {
173  ACLMASK_ALL, /* normal case: compute all bits */
174  ACLMASK_ANY /* return when result is known nonzero */
175 } AclMaskHow;
AclMaskHow
Definition: acl.h:171
Enumerator
ACL_KIND_COLUMN 
ACL_KIND_CLASS 
ACL_KIND_SEQUENCE 
ACL_KIND_DATABASE 
ACL_KIND_PROC 
ACL_KIND_OPER 
ACL_KIND_TYPE 
ACL_KIND_LANGUAGE 
ACL_KIND_LARGEOBJECT 
ACL_KIND_NAMESPACE 
ACL_KIND_OPCLASS 
ACL_KIND_OPFAMILY 
ACL_KIND_COLLATION 
ACL_KIND_CONVERSION 
ACL_KIND_STATISTICS 
ACL_KIND_TABLESPACE 
ACL_KIND_TSDICTIONARY 
ACL_KIND_TSCONFIGURATION 
ACL_KIND_FDW 
ACL_KIND_FOREIGN_SERVER 
ACL_KIND_EVENT_TRIGGER 
ACL_KIND_EXTENSION 
ACL_KIND_PUBLICATION 
ACL_KIND_SUBSCRIPTION 
MAX_ACL_KIND 

Definition at line 187 of file acl.h.

188 {
189  ACL_KIND_COLUMN, /* pg_attribute */
190  ACL_KIND_CLASS, /* pg_class */
191  ACL_KIND_SEQUENCE, /* pg_sequence */
192  ACL_KIND_DATABASE, /* pg_database */
193  ACL_KIND_PROC, /* pg_proc */
194  ACL_KIND_OPER, /* pg_operator */
195  ACL_KIND_TYPE, /* pg_type */
196  ACL_KIND_LANGUAGE, /* pg_language */
197  ACL_KIND_LARGEOBJECT, /* pg_largeobject */
198  ACL_KIND_NAMESPACE, /* pg_namespace */
199  ACL_KIND_OPCLASS, /* pg_opclass */
200  ACL_KIND_OPFAMILY, /* pg_opfamily */
201  ACL_KIND_COLLATION, /* pg_collation */
202  ACL_KIND_CONVERSION, /* pg_conversion */
203  ACL_KIND_STATISTICS, /* pg_statistic_ext */
204  ACL_KIND_TABLESPACE, /* pg_tablespace */
205  ACL_KIND_TSDICTIONARY, /* pg_ts_dict */
206  ACL_KIND_TSCONFIGURATION, /* pg_ts_config */
207  ACL_KIND_FDW, /* pg_foreign_data_wrapper */
208  ACL_KIND_FOREIGN_SERVER, /* pg_foreign_server */
209  ACL_KIND_EVENT_TRIGGER, /* pg_event_trigger */
210  ACL_KIND_EXTENSION, /* pg_extension */
211  ACL_KIND_PUBLICATION, /* pg_publication */
212  ACL_KIND_SUBSCRIPTION, /* pg_subscription */
213  MAX_ACL_KIND /* MUST BE LAST */
214 } AclObjectKind;
AclObjectKind
Definition: acl.h:187
enum AclResult
Enumerator
ACLCHECK_OK 
ACLCHECK_NO_PRIV 
ACLCHECK_NOT_OWNER 

Definition at line 178 of file acl.h.

179 {
180  ACLCHECK_OK = 0,
183 } AclResult;
AclResult
Definition: acl.h:178

Function Documentation

void aclcheck_error ( AclResult  aclerr,
AclObjectKind  objectkind,
const char *  objectname 
)

Definition at line 3399 of file aclchk.c.

References ACLCHECK_NO_PRIV, ACLCHECK_NOT_OWNER, ACLCHECK_OK, elog, ereport, errcode(), errmsg(), and ERROR.

Referenced by aclcheck_error_type(), AlterCollation(), AlterDatabase(), AlterDatabaseOwner(), AlterDatabaseSet(), AlterEventTrigger(), AlterEventTriggerOwner_internal(), AlterExtensionNamespace(), AlterForeignServer(), AlterForeignServerOwner_internal(), AlterFunction(), AlterObjectNamespace_internal(), AlterObjectOwner_internal(), AlterObjectRename_internal(), AlterOperator(), AlterOpFamilyAdd(), AlterPublication(), AlterPublicationOwner_internal(), AlterRoleSet(), AlterSchemaOwner_internal(), AlterSubscription(), AlterSubscriptionOwner_internal(), AlterTableMoveAll(), AlterTableSpaceOptions(), AlterTSConfiguration(), AlterTSDictionary(), AlterTypeOwner(), ATExecChangeOwner(), ATPrepSetStatistics(), ATPrepSetTableSpace(), ATSimplePermissions(), brin_desummarize_range(), brin_summarize_range(), calculate_database_size(), calculate_tablespace_size(), call_pltcl_start_proc(), check_object_ownership(), check_temp_tablespaces(), checkFkeyPermissions(), CheckFunctionValidatorAccess(), compute_return_type(), create_proc_lang(), CreateConversionCommand(), createdb(), CreateForeignServer(), CreateForeignTable(), CreateFunction(), CreateProceduralLanguage(), CreatePublication(), CreateSchemaCommand(), CreateStatistics(), CreateTransform(), CreateTrigger(), currtid_byrelname(), currtid_byreloid(), DefineAggregate(), DefineCollation(), DefineDomain(), DefineEnum(), DefineIndex(), DefineOpClass(), DefineOperator(), DefineOpFamily(), DefineQueryRewrite(), DefineRange(), DefineRelation(), DefineTSConfiguration(), DefineTSDictionary(), DefineType(), dropdb(), DropSubscription(), DropTableSpace(), EnableDisableRule(), ExecAlterExtensionContentsStmt(), ExecAlterExtensionStmt(), ExecCheckRTPerms(), ExecInitAgg(), ExecInitExprRec(), ExecInitFunc(), ExecInitWindowAgg(), ExecuteDoStmt(), ExecuteTruncate(), findRangeCanonicalFunction(), findRangeSubtypeDiffFunction(), get_connect_string(), get_other_operator(), get_rel_from_relname(), gin_clean_pending_list(), HandleFunctionRequest(), ImportForeignSchema(), init_sexpr(), initialize_peragg(), LockTableRecurse(), lookup_agg_function(), LookupCreationNamespace(), LookupExplicitNamespace(), MergeAttributes(), movedb(), OperatorCreate(), pg_prewarm(), pgrowlocks(), ProcedureCreate(), PublicationAddTables(), RangeVarCallbackForAlterRelation(), RangeVarCallbackForDropRelation(), RangeVarCallbackForLockTable(), RangeVarCallbackForPolicy(), RangeVarCallbackForReindexIndex(), RangeVarCallbackForRenameRule(), RangeVarCallbackForRenameTrigger(), RangeVarCallbackOwnsRelation(), RangeVarCallbackOwnsTable(), RangeVarGetAndCheckCreationNamespace(), ReindexMultipleTables(), renameatt_check(), RenameDatabase(), RenameSchema(), RenameTableSpace(), restrict_and_check_grant(), transformTableLikeClause(), truncate_check_rel(), TypeCreate(), user_mapping_ddl_aclcheck(), ValidateJoinEstimator(), and ValidateRestrictionEstimator().

3401 {
3402  switch (aclerr)
3403  {
3404  case ACLCHECK_OK:
3405  /* no error, so return to caller */
3406  break;
3407  case ACLCHECK_NO_PRIV:
3408  ereport(ERROR,
3409  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3410  errmsg(no_priv_msg[objectkind], objectname)));
3411  break;
3412  case ACLCHECK_NOT_OWNER:
3413  ereport(ERROR,
3414  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3415  errmsg(not_owner_msg[objectkind], objectname)));
3416  break;
3417  default:
3418  elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
3419  break;
3420  }
3421 }
static const char *const no_priv_msg[MAX_ACL_KIND]
Definition: aclchk.c:3293
static const char *const not_owner_msg[MAX_ACL_KIND]
Definition: aclchk.c:3345
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
void aclcheck_error_col ( AclResult  aclerr,
AclObjectKind  objectkind,
const char *  objectname,
const char *  colname 
)

Definition at line 3425 of file aclchk.c.

References ACLCHECK_NO_PRIV, ACLCHECK_NOT_OWNER, ACLCHECK_OK, elog, ereport, errcode(), errmsg(), and ERROR.

Referenced by restrict_and_check_grant().

3427 {
3428  switch (aclerr)
3429  {
3430  case ACLCHECK_OK:
3431  /* no error, so return to caller */
3432  break;
3433  case ACLCHECK_NO_PRIV:
3434  ereport(ERROR,
3435  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3436  errmsg("permission denied for column \"%s\" of relation \"%s\"",
3437  colname, objectname)));
3438  break;
3439  case ACLCHECK_NOT_OWNER:
3440  /* relation msg is OK since columns don't have separate owners */
3441  ereport(ERROR,
3442  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3443  errmsg(not_owner_msg[objectkind], objectname)));
3444  break;
3445  default:
3446  elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
3447  break;
3448  }
3449 }
static const char *const not_owner_msg[MAX_ACL_KIND]
Definition: aclchk.c:3345
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
void aclcheck_error_type ( AclResult  aclerr,
Oid  typeOid 
)

Definition at line 3457 of file aclchk.c.

References ACL_KIND_TYPE, aclcheck_error(), format_type_be(), and get_element_type().

Referenced by AggregateCreate(), AlterTypeNamespace_oid(), AlterTypeOwner(), ATExecAddColumn(), ATPrepAlterColumnType(), BuildDescForRelation(), check_object_ownership(), checkDomainOwner(), checkEnumOwner(), compute_return_type(), CreateCast(), CreateTransform(), DefineDomain(), DefineOpClass(), DefineOperator(), DefineRelation(), interpret_function_parameter_list(), and RenameType().

3458 {
3459  Oid element_type = get_element_type(typeOid);
3460 
3461  aclcheck_error(aclerr, ACL_KIND_TYPE, format_type_be(element_type ? element_type : typeOid));
3462 }
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2484
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
Acl* aclconcat ( const Acl left_acl,
const Acl right_acl 
)

Definition at line 427 of file acl.c.

References ACL_DAT, ACL_NUM, and allocacl().

Referenced by ExecGrant_Attribute().

428 {
429  Acl *result_acl;
430 
431  result_acl = allocacl(ACL_NUM(left_acl) + ACL_NUM(right_acl));
432 
433  memcpy(ACL_DAT(result_acl),
434  ACL_DAT(left_acl),
435  ACL_NUM(left_acl) * sizeof(AclItem));
436 
437  memcpy(ACL_DAT(result_acl) + ACL_NUM(left_acl),
438  ACL_DAT(right_acl),
439  ACL_NUM(right_acl) * sizeof(AclItem));
440 
441  return result_acl;
442 }
static Acl * allocacl(int n)
Definition: acl.c:376
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
Acl* aclcopy ( const Acl orig_acl)

Definition at line 407 of file acl.c.

References ACL_DAT, ACL_NUM, and allocacl().

Referenced by aclmerge(), ExecGrant_Relation(), and SetDefaultACL().

408 {
409  Acl *result_acl;
410 
411  result_acl = allocacl(ACL_NUM(orig_acl));
412 
413  memcpy(ACL_DAT(result_acl),
414  ACL_DAT(orig_acl),
415  ACL_NUM(orig_acl) * sizeof(AclItem));
416 
417  return result_acl;
418 }
static Acl * allocacl(int n)
Definition: acl.c:376
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
Acl* acldefault ( GrantObjectType  objtype,
Oid  ownerId 
)

Definition at line 746 of file acl.c.

References ACL_ALL_RIGHTS_DATABASE, ACL_ALL_RIGHTS_FDW, ACL_ALL_RIGHTS_FOREIGN_SERVER, ACL_ALL_RIGHTS_FUNCTION, ACL_ALL_RIGHTS_LANGUAGE, ACL_ALL_RIGHTS_LARGEOBJECT, ACL_ALL_RIGHTS_NAMESPACE, ACL_ALL_RIGHTS_RELATION, ACL_ALL_RIGHTS_SEQUENCE, ACL_ALL_RIGHTS_TABLESPACE, ACL_ALL_RIGHTS_TYPE, ACL_CONNECT, ACL_CREATE_TEMP, ACL_DAT, ACL_EXECUTE, ACL_ID_PUBLIC, ACL_NO_RIGHTS, ACL_OBJECT_COLUMN, ACL_OBJECT_DATABASE, ACL_OBJECT_DOMAIN, ACL_OBJECT_FDW, ACL_OBJECT_FOREIGN_SERVER, ACL_OBJECT_FUNCTION, ACL_OBJECT_LANGUAGE, ACL_OBJECT_LARGEOBJECT, ACL_OBJECT_NAMESPACE, ACL_OBJECT_RELATION, ACL_OBJECT_SEQUENCE, ACL_OBJECT_TABLESPACE, ACL_OBJECT_TYPE, ACL_USAGE, ACLITEM_SET_PRIVS_GOPTIONS, AclItem::ai_grantee, AclItem::ai_grantor, allocacl(), elog, and ERROR.

Referenced by acldefault_sql(), ExecGrant_Attribute(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), get_user_default_acl(), pg_class_aclmask(), pg_database_aclmask(), pg_foreign_data_wrapper_aclmask(), pg_foreign_server_aclmask(), pg_language_aclmask(), pg_largeobject_aclmask_snapshot(), pg_namespace_aclmask(), pg_proc_aclmask(), pg_tablespace_aclmask(), pg_type_aclmask(), and SetDefaultACL().

747 {
748  AclMode world_default;
749  AclMode owner_default;
750  int nacl;
751  Acl *acl;
752  AclItem *aip;
753 
754  switch (objtype)
755  {
756  case ACL_OBJECT_COLUMN:
757  /* by default, columns have no extra privileges */
758  world_default = ACL_NO_RIGHTS;
759  owner_default = ACL_NO_RIGHTS;
760  break;
761  case ACL_OBJECT_RELATION:
762  world_default = ACL_NO_RIGHTS;
763  owner_default = ACL_ALL_RIGHTS_RELATION;
764  break;
765  case ACL_OBJECT_SEQUENCE:
766  world_default = ACL_NO_RIGHTS;
767  owner_default = ACL_ALL_RIGHTS_SEQUENCE;
768  break;
769  case ACL_OBJECT_DATABASE:
770  /* for backwards compatibility, grant some rights by default */
771  world_default = ACL_CREATE_TEMP | ACL_CONNECT;
772  owner_default = ACL_ALL_RIGHTS_DATABASE;
773  break;
774  case ACL_OBJECT_FUNCTION:
775  /* Grant EXECUTE by default, for now */
776  world_default = ACL_EXECUTE;
777  owner_default = ACL_ALL_RIGHTS_FUNCTION;
778  break;
779  case ACL_OBJECT_LANGUAGE:
780  /* Grant USAGE by default, for now */
781  world_default = ACL_USAGE;
782  owner_default = ACL_ALL_RIGHTS_LANGUAGE;
783  break;
785  world_default = ACL_NO_RIGHTS;
786  owner_default = ACL_ALL_RIGHTS_LARGEOBJECT;
787  break;
789  world_default = ACL_NO_RIGHTS;
790  owner_default = ACL_ALL_RIGHTS_NAMESPACE;
791  break;
793  world_default = ACL_NO_RIGHTS;
794  owner_default = ACL_ALL_RIGHTS_TABLESPACE;
795  break;
796  case ACL_OBJECT_FDW:
797  world_default = ACL_NO_RIGHTS;
798  owner_default = ACL_ALL_RIGHTS_FDW;
799  break;
801  world_default = ACL_NO_RIGHTS;
802  owner_default = ACL_ALL_RIGHTS_FOREIGN_SERVER;
803  break;
804  case ACL_OBJECT_DOMAIN:
805  case ACL_OBJECT_TYPE:
806  world_default = ACL_USAGE;
807  owner_default = ACL_ALL_RIGHTS_TYPE;
808  break;
809  default:
810  elog(ERROR, "unrecognized objtype: %d", (int) objtype);
811  world_default = ACL_NO_RIGHTS; /* keep compiler quiet */
812  owner_default = ACL_NO_RIGHTS;
813  break;
814  }
815 
816  nacl = 0;
817  if (world_default != ACL_NO_RIGHTS)
818  nacl++;
819  if (owner_default != ACL_NO_RIGHTS)
820  nacl++;
821 
822  acl = allocacl(nacl);
823  aip = ACL_DAT(acl);
824 
825  if (world_default != ACL_NO_RIGHTS)
826  {
827  aip->ai_grantee = ACL_ID_PUBLIC;
828  aip->ai_grantor = ownerId;
829  ACLITEM_SET_PRIVS_GOPTIONS(*aip, world_default, ACL_NO_RIGHTS);
830  aip++;
831  }
832 
833  /*
834  * Note that the owner's entry shows all ordinary privileges but no grant
835  * options. This is because his grant options come "from the system" and
836  * not from his own efforts. (The SQL spec says that the owner's rights
837  * come from a "_SYSTEM" authid.) However, we do consider that the
838  * owner's ordinary privileges are self-granted; this lets him revoke
839  * them. We implement the owner's grant options without any explicit
840  * "_SYSTEM"-like ACL entry, by internally special-casing the owner
841  * wherever we are testing grant options.
842  */
843  if (owner_default != ACL_NO_RIGHTS)
844  {
845  aip->ai_grantee = ownerId;
846  aip->ai_grantor = ownerId;
847  ACLITEM_SET_PRIVS_GOPTIONS(*aip, owner_default, ACL_NO_RIGHTS);
848  }
849 
850  return acl;
851 }
Oid ai_grantee
Definition: acl.h:57
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:163
static Acl * allocacl(int n)
Definition: acl.c:376
#define ACL_ALL_RIGHTS_TABLESPACE
Definition: acl.h:167
Oid ai_grantor
Definition: acl.h:58
#define ACL_ALL_RIGHTS_LANGUAGE
Definition: acl.h:164
uint32 AclMode
Definition: parsenodes.h:70
#define ERROR
Definition: elog.h:43
#define ACL_NO_RIGHTS
Definition: parsenodes.h:86
#define ACL_ALL_RIGHTS_TYPE
Definition: acl.h:168
#define ACL_USAGE
Definition: parsenodes.h:80
#define ACL_CONNECT
Definition: parsenodes.h:84
#define ACL_ALL_RIGHTS_NAMESPACE
Definition: acl.h:166
#define ACL_DAT(ACL)
Definition: acl.h:110
#define ACL_ALL_RIGHTS_SEQUENCE
Definition: acl.h:159
Definition: acl.h:55
#define ACL_ALL_RIGHTS_LARGEOBJECT
Definition: acl.h:165
#define ACL_ALL_RIGHTS_DATABASE
Definition: acl.h:160
#define ACL_ALL_RIGHTS_FOREIGN_SERVER
Definition: acl.h:162
#define ACL_EXECUTE
Definition: parsenodes.h:79
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:158
#define ACL_CREATE_TEMP
Definition: parsenodes.h:83
#define ACL_ID_PUBLIC
Definition: acl.h:47
#define ACL_ALL_RIGHTS_FDW
Definition: acl.h:161
#define elog
Definition: elog.h:219
#define ACLITEM_SET_PRIVS_GOPTIONS(item, privs, goptions)
Definition: acl.h:83
bool aclequal ( const Acl left_acl,
const Acl right_acl 
)

Definition at line 509 of file acl.c.

References ACL_DAT, and ACL_NUM.

Referenced by get_user_default_acl(), and SetDefaultACL().

510 {
511  /* Check for cases where one or both are empty/null */
512  if (left_acl == NULL || ACL_NUM(left_acl) == 0)
513  {
514  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
515  return true;
516  else
517  return false;
518  }
519  else
520  {
521  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
522  return false;
523  }
524 
525  if (ACL_NUM(left_acl) != ACL_NUM(right_acl))
526  return false;
527 
528  if (memcmp(ACL_DAT(left_acl),
529  ACL_DAT(right_acl),
530  ACL_NUM(left_acl) * sizeof(AclItem)) == 0)
531  return true;
532 
533  return false;
534 }
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
void aclitemsort ( Acl acl)

Definition at line 495 of file acl.c.

References ACL_DAT, ACL_NUM, aclitemComparator(), and qsort.

Referenced by get_user_default_acl(), and SetDefaultACL().

496 {
497  if (acl != NULL && ACL_NUM(acl) > 1)
498  qsort(ACL_DAT(acl), ACL_NUM(acl), sizeof(AclItem), aclitemComparator);
499 }
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
#define qsort(a, b, c, d)
Definition: port.h:443
static int aclitemComparator(const void *arg1, const void *arg2)
Definition: acl.c:669
AclMode aclmask ( const Acl acl,
Oid  roleid,
Oid  ownerId,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 1319 of file acl.c.

References ACL_DAT, ACL_ID_PUBLIC, ACL_NUM, ACLITEM_ALL_GOPTION_BITS, ACLMASK_ALL, AclItem::ai_grantee, AclItem::ai_privs, check_acl(), elog, ERROR, has_privs_of_role(), i, and remaining.

Referenced by check_circularity(), LockTableAclCheck(), pg_attribute_aclmask(), pg_class_aclmask(), pg_database_aclmask(), pg_foreign_data_wrapper_aclmask(), pg_foreign_server_aclmask(), pg_language_aclmask(), pg_largeobject_aclmask_snapshot(), pg_namespace_aclmask(), pg_proc_aclmask(), pg_tablespace_aclmask(), pg_type_aclmask(), and recursive_revoke().

1321 {
1322  AclMode result;
1324  AclItem *aidat;
1325  int i,
1326  num;
1327 
1328  /*
1329  * Null ACL should not happen, since caller should have inserted
1330  * appropriate default
1331  */
1332  if (acl == NULL)
1333  elog(ERROR, "null ACL");
1334 
1335  check_acl(acl);
1336 
1337  /* Quick exit for mask == 0 */
1338  if (mask == 0)
1339  return 0;
1340 
1341  result = 0;
1342 
1343  /* Owner always implicitly has all grant options */
1344  if ((mask & ACLITEM_ALL_GOPTION_BITS) &&
1345  has_privs_of_role(roleid, ownerId))
1346  {
1347  result = mask & ACLITEM_ALL_GOPTION_BITS;
1348  if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1349  return result;
1350  }
1351 
1352  num = ACL_NUM(acl);
1353  aidat = ACL_DAT(acl);
1354 
1355  /*
1356  * Check privileges granted directly to roleid or to public
1357  */
1358  for (i = 0; i < num; i++)
1359  {
1360  AclItem *aidata = &aidat[i];
1361 
1362  if (aidata->ai_grantee == ACL_ID_PUBLIC ||
1363  aidata->ai_grantee == roleid)
1364  {
1365  result |= aidata->ai_privs & mask;
1366  if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1367  return result;
1368  }
1369  }
1370 
1371  /*
1372  * Check privileges granted indirectly via role memberships. We do this in
1373  * a separate pass to minimize expensive indirect membership tests. In
1374  * particular, it's worth testing whether a given ACL entry grants any
1375  * privileges still of interest before we perform the has_privs_of_role
1376  * test.
1377  */
1378  remaining = mask & ~result;
1379  for (i = 0; i < num; i++)
1380  {
1381  AclItem *aidata = &aidat[i];
1382 
1383  if (aidata->ai_grantee == ACL_ID_PUBLIC ||
1384  aidata->ai_grantee == roleid)
1385  continue; /* already checked it */
1386 
1387  if ((aidata->ai_privs & remaining) &&
1388  has_privs_of_role(roleid, aidata->ai_grantee))
1389  {
1390  result |= aidata->ai_privs & mask;
1391  if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1392  return result;
1393  remaining = mask & ~result;
1394  }
1395  }
1396 
1397  return result;
1398 }
int remaining
Definition: informix.c:692
Oid ai_grantee
Definition: acl.h:57
#define ACLITEM_ALL_GOPTION_BITS
Definition: acl.h:89
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4828
AclMode ai_privs
Definition: acl.h:59
uint32 AclMode
Definition: parsenodes.h:70
#define ERROR
Definition: elog.h:43
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
int i
#define ACL_ID_PUBLIC
Definition: acl.h:47
#define elog
Definition: elog.h:219
static void check_acl(const Acl *acl)
Definition: acl.c:540
int aclmembers ( const Acl acl,
Oid **  roleids 
)

Definition at line 1471 of file acl.c.

References ACL_DAT, ACL_ID_PUBLIC, ACL_NUM, AclItem::ai_grantee, AclItem::ai_grantor, check_acl(), i, sort-test::list, oid_cmp(), palloc(), and qsort.

Referenced by ExecGrant_Attribute(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), heap_create_with_catalog(), ProcedureCreate(), and SetDefaultACL().

1472 {
1473  Oid *list;
1474  const AclItem *acldat;
1475  int i,
1476  j,
1477  k;
1478 
1479  if (acl == NULL || ACL_NUM(acl) == 0)
1480  {
1481  *roleids = NULL;
1482  return 0;
1483  }
1484 
1485  check_acl(acl);
1486 
1487  /* Allocate the worst-case space requirement */
1488  list = palloc(ACL_NUM(acl) * 2 * sizeof(Oid));
1489  acldat = ACL_DAT(acl);
1490 
1491  /*
1492  * Walk the ACL collecting mentioned RoleIds.
1493  */
1494  j = 0;
1495  for (i = 0; i < ACL_NUM(acl); i++)
1496  {
1497  const AclItem *ai = &acldat[i];
1498 
1499  if (ai->ai_grantee != ACL_ID_PUBLIC)
1500  list[j++] = ai->ai_grantee;
1501  /* grantor is currently never PUBLIC, but let's check anyway */
1502  if (ai->ai_grantor != ACL_ID_PUBLIC)
1503  list[j++] = ai->ai_grantor;
1504  }
1505 
1506  /* Sort the array */
1507  qsort(list, j, sizeof(Oid), oid_cmp);
1508 
1509  /* Remove duplicates from the array */
1510  k = 0;
1511  for (i = 1; i < j; i++)
1512  {
1513  if (list[k] != list[i])
1514  list[++k] = list[i];
1515  }
1516 
1517  /*
1518  * We could repalloc the array down to minimum size, but it's hardly worth
1519  * it since it's only transient memory.
1520  */
1521  *roleids = list;
1522 
1523  return k + 1;
1524 }
Oid ai_grantee
Definition: acl.h:57
unsigned int Oid
Definition: postgres_ext.h:31
Oid ai_grantor
Definition: acl.h:58
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
int oid_cmp(const void *p1, const void *p2)
Definition: oid.c:336
tuple list
Definition: sort-test.py:11
void * palloc(Size size)
Definition: mcxt.c:848
int i
#define ACL_ID_PUBLIC
Definition: acl.h:47
#define qsort(a, b, c, d)
Definition: port.h:443
static void check_acl(const Acl *acl)
Definition: acl.c:540
Acl* aclmerge ( const Acl left_acl,
const Acl right_acl,
Oid  ownerId 
)

Definition at line 451 of file acl.c.

References ACL_DAT, ACL_MODECHG_ADD, ACL_NUM, aclcopy(), aclupdate(), DROP_RESTRICT, i, and pfree().

Referenced by get_user_default_acl().

452 {
453  Acl *result_acl;
454  AclItem *aip;
455  int i,
456  num;
457 
458  /* Check for cases where one or both are empty/null */
459  if (left_acl == NULL || ACL_NUM(left_acl) == 0)
460  {
461  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
462  return NULL;
463  else
464  return aclcopy(right_acl);
465  }
466  else
467  {
468  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
469  return aclcopy(left_acl);
470  }
471 
472  /* Merge them the hard way, one item at a time */
473  result_acl = aclcopy(left_acl);
474 
475  aip = ACL_DAT(right_acl);
476  num = ACL_NUM(right_acl);
477 
478  for (i = 0; i < num; i++, aip++)
479  {
480  Acl *tmp_acl;
481 
482  tmp_acl = aclupdate(result_acl, aip, ACL_MODECHG_ADD,
483  ownerId, DROP_RESTRICT);
484  pfree(result_acl);
485  result_acl = tmp_acl;
486  }
487 
488  return result_acl;
489 }
void pfree(void *pointer)
Definition: mcxt.c:949
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_MODECHG_ADD
Definition: acl.h:130
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
Acl * aclcopy(const Acl *orig_acl)
Definition: acl.c:407
int i
Acl * aclupdate(const Acl *old_acl, const AclItem *mod_aip, int modechg, Oid ownerId, DropBehavior behavior)
Definition: acl.c:929
Acl* aclnewowner ( const Acl old_acl,
Oid  oldOwnerId,
Oid  newOwnerId 
)

Definition at line 1050 of file acl.c.

References ACL_DAT, ACL_N_SIZE, ACL_NO_RIGHTS, ACL_NUM, ACLITEM_GET_RIGHTS, aclitem_match(), ACLITEM_SET_RIGHTS, AclItem::ai_grantee, AclItem::ai_grantor, allocacl(), ARR_DIMS, check_acl(), and SET_VARSIZE.

Referenced by AlterDatabaseOwner(), AlterForeignDataWrapperOwner_internal(), AlterForeignServerOwner_internal(), AlterObjectOwner_internal(), AlterSchemaOwner_internal(), AlterTypeOwnerInternal(), ATExecChangeOwner(), and change_owner_fix_column_acls().

1051 {
1052  Acl *new_acl;
1053  AclItem *new_aip;
1054  AclItem *old_aip;
1055  AclItem *dst_aip;
1056  AclItem *src_aip;
1057  AclItem *targ_aip;
1058  bool newpresent = false;
1059  int dst,
1060  src,
1061  targ,
1062  num;
1063 
1064  check_acl(old_acl);
1065 
1066  /*
1067  * Make a copy of the given ACL, substituting new owner ID for old
1068  * wherever it appears as either grantor or grantee. Also note if the new
1069  * owner ID is already present.
1070  */
1071  num = ACL_NUM(old_acl);
1072  old_aip = ACL_DAT(old_acl);
1073  new_acl = allocacl(num);
1074  new_aip = ACL_DAT(new_acl);
1075  memcpy(new_aip, old_aip, num * sizeof(AclItem));
1076  for (dst = 0, dst_aip = new_aip; dst < num; dst++, dst_aip++)
1077  {
1078  if (dst_aip->ai_grantor == oldOwnerId)
1079  dst_aip->ai_grantor = newOwnerId;
1080  else if (dst_aip->ai_grantor == newOwnerId)
1081  newpresent = true;
1082  if (dst_aip->ai_grantee == oldOwnerId)
1083  dst_aip->ai_grantee = newOwnerId;
1084  else if (dst_aip->ai_grantee == newOwnerId)
1085  newpresent = true;
1086  }
1087 
1088  /*
1089  * If the old ACL contained any references to the new owner, then we may
1090  * now have generated an ACL containing duplicate entries. Find them and
1091  * merge them so that there are not duplicates. (This is relatively
1092  * expensive since we use a stupid O(N^2) algorithm, but it's unlikely to
1093  * be the normal case.)
1094  *
1095  * To simplify deletion of duplicate entries, we temporarily leave them in
1096  * the array but set their privilege masks to zero; when we reach such an
1097  * entry it's just skipped. (Thus, a side effect of this code will be to
1098  * remove privilege-free entries, should there be any in the input.) dst
1099  * is the next output slot, targ is the currently considered input slot
1100  * (always >= dst), and src scans entries to the right of targ looking for
1101  * duplicates. Once an entry has been emitted to dst it is known
1102  * duplicate-free and need not be considered anymore.
1103  */
1104  if (newpresent)
1105  {
1106  dst = 0;
1107  for (targ = 0, targ_aip = new_aip; targ < num; targ++, targ_aip++)
1108  {
1109  /* ignore if deleted in an earlier pass */
1110  if (ACLITEM_GET_RIGHTS(*targ_aip) == ACL_NO_RIGHTS)
1111  continue;
1112  /* find and merge any duplicates */
1113  for (src = targ + 1, src_aip = targ_aip + 1; src < num;
1114  src++, src_aip++)
1115  {
1116  if (ACLITEM_GET_RIGHTS(*src_aip) == ACL_NO_RIGHTS)
1117  continue;
1118  if (aclitem_match(targ_aip, src_aip))
1119  {
1120  ACLITEM_SET_RIGHTS(*targ_aip,
1121  ACLITEM_GET_RIGHTS(*targ_aip) |
1122  ACLITEM_GET_RIGHTS(*src_aip));
1123  /* mark the duplicate deleted */
1124  ACLITEM_SET_RIGHTS(*src_aip, ACL_NO_RIGHTS);
1125  }
1126  }
1127  /* and emit to output */
1128  new_aip[dst] = *targ_aip;
1129  dst++;
1130  }
1131  /* Adjust array size to be 'dst' items */
1132  ARR_DIMS(new_acl)[0] = dst;
1133  SET_VARSIZE(new_acl, ACL_N_SIZE(dst));
1134  }
1135 
1136  return new_acl;
1137 }
Oid ai_grantee
Definition: acl.h:57
static Acl * allocacl(int n)
Definition: acl.c:376
Oid ai_grantor
Definition: acl.h:58
#define ARR_DIMS(a)
Definition: array.h:275
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_NO_RIGHTS
Definition: parsenodes.h:86
#define ACLITEM_GET_RIGHTS(item)
Definition: acl.h:69
#define ACLITEM_SET_RIGHTS(item, rights)
Definition: acl.h:80
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
static bool aclitem_match(const AclItem *a1, const AclItem *a2)
Definition: acl.c:658
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define ACL_N_SIZE(N)
Definition: acl.h:111
static void check_acl(const Acl *acl)
Definition: acl.c:540
Acl* aclupdate ( const Acl old_acl,
const AclItem mod_aip,
int  modechg,
Oid  ownerId,
DropBehavior  behavior 
)

Definition at line 929 of file acl.c.

References ACL_DAT, ACL_ID_PUBLIC, ACL_MODECHG_ADD, ACL_MODECHG_DEL, ACL_MODECHG_EQL, ACL_N_SIZE, ACL_NO_RIGHTS, ACL_NUM, ACL_SIZE, ACLITEM_GET_GOPTIONS, ACLITEM_GET_RIGHTS, aclitem_match(), ACLITEM_SET_PRIVS_GOPTIONS, ACLITEM_SET_RIGHTS, AclItem::ai_grantee, AclItem::ai_grantor, allocacl(), ARR_DIMS, Assert, check_acl(), check_circularity(), memmove, recursive_revoke(), and SET_VARSIZE.

Referenced by aclmerge(), check_circularity(), merge_acl_with_grant(), and recursive_revoke().

931 {
932  Acl *new_acl = NULL;
933  AclItem *old_aip,
934  *new_aip = NULL;
935  AclMode old_rights,
936  old_goptions,
937  new_rights,
938  new_goptions;
939  int dst,
940  num;
941 
942  /* Caller probably already checked old_acl, but be safe */
943  check_acl(old_acl);
944 
945  /* If granting grant options, check for circularity */
946  if (modechg != ACL_MODECHG_DEL &&
947  ACLITEM_GET_GOPTIONS(*mod_aip) != ACL_NO_RIGHTS)
948  check_circularity(old_acl, mod_aip, ownerId);
949 
950  num = ACL_NUM(old_acl);
951  old_aip = ACL_DAT(old_acl);
952 
953  /*
954  * Search the ACL for an existing entry for this grantee and grantor. If
955  * one exists, just modify the entry in-place (well, in the same position,
956  * since we actually return a copy); otherwise, insert the new entry at
957  * the end.
958  */
959 
960  for (dst = 0; dst < num; ++dst)
961  {
962  if (aclitem_match(mod_aip, old_aip + dst))
963  {
964  /* found a match, so modify existing item */
965  new_acl = allocacl(num);
966  new_aip = ACL_DAT(new_acl);
967  memcpy(new_acl, old_acl, ACL_SIZE(old_acl));
968  break;
969  }
970  }
971 
972  if (dst == num)
973  {
974  /* need to append a new item */
975  new_acl = allocacl(num + 1);
976  new_aip = ACL_DAT(new_acl);
977  memcpy(new_aip, old_aip, num * sizeof(AclItem));
978 
979  /* initialize the new entry with no permissions */
980  new_aip[dst].ai_grantee = mod_aip->ai_grantee;
981  new_aip[dst].ai_grantor = mod_aip->ai_grantor;
982  ACLITEM_SET_PRIVS_GOPTIONS(new_aip[dst],
984  num++; /* set num to the size of new_acl */
985  }
986 
987  old_rights = ACLITEM_GET_RIGHTS(new_aip[dst]);
988  old_goptions = ACLITEM_GET_GOPTIONS(new_aip[dst]);
989 
990  /* apply the specified permissions change */
991  switch (modechg)
992  {
993  case ACL_MODECHG_ADD:
994  ACLITEM_SET_RIGHTS(new_aip[dst],
995  old_rights | ACLITEM_GET_RIGHTS(*mod_aip));
996  break;
997  case ACL_MODECHG_DEL:
998  ACLITEM_SET_RIGHTS(new_aip[dst],
999  old_rights & ~ACLITEM_GET_RIGHTS(*mod_aip));
1000  break;
1001  case ACL_MODECHG_EQL:
1002  ACLITEM_SET_RIGHTS(new_aip[dst],
1003  ACLITEM_GET_RIGHTS(*mod_aip));
1004  break;
1005  }
1006 
1007  new_rights = ACLITEM_GET_RIGHTS(new_aip[dst]);
1008  new_goptions = ACLITEM_GET_GOPTIONS(new_aip[dst]);
1009 
1010  /*
1011  * If the adjusted entry has no permissions, delete it from the list.
1012  */
1013  if (new_rights == ACL_NO_RIGHTS)
1014  {
1015  memmove(new_aip + dst,
1016  new_aip + dst + 1,
1017  (num - dst - 1) * sizeof(AclItem));
1018  /* Adjust array size to be 'num - 1' items */
1019  ARR_DIMS(new_acl)[0] = num - 1;
1020  SET_VARSIZE(new_acl, ACL_N_SIZE(num - 1));
1021  }
1022 
1023  /*
1024  * Remove abandoned privileges (cascading revoke). Currently we can only
1025  * handle this when the grantee is not PUBLIC.
1026  */
1027  if ((old_goptions & ~new_goptions) != 0)
1028  {
1029  Assert(mod_aip->ai_grantee != ACL_ID_PUBLIC);
1030  new_acl = recursive_revoke(new_acl, mod_aip->ai_grantee,
1031  (old_goptions & ~new_goptions),
1032  ownerId, behavior);
1033  }
1034 
1035  return new_acl;
1036 }
Oid ai_grantee
Definition: acl.h:57
#define ACL_MODECHG_EQL
Definition: acl.h:132
#define ACLITEM_GET_GOPTIONS(item)
Definition: acl.h:68
static Acl * allocacl(int n)
Definition: acl.c:376
#define ACL_MODECHG_DEL
Definition: acl.h:131
#define ACL_SIZE(ACL)
Definition: acl.h:112
Oid ai_grantor
Definition: acl.h:58
uint32 AclMode
Definition: parsenodes.h:70
static void check_circularity(const Acl *old_acl, const AclItem *mod_aip, Oid ownerId)
Definition: acl.c:1153
#define ARR_DIMS(a)
Definition: array.h:275
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_NO_RIGHTS
Definition: parsenodes.h:86
#define memmove(d, s, c)
Definition: c.h:1047
#define ACLITEM_GET_RIGHTS(item)
Definition: acl.h:69
#define ACLITEM_SET_RIGHTS(item, rights)
Definition: acl.h:80
#define ACL_MODECHG_ADD
Definition: acl.h:130
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
#define Assert(condition)
Definition: c.h:664
static bool aclitem_match(const AclItem *a1, const AclItem *a2)
Definition: acl.c:658
static Acl * recursive_revoke(Acl *acl, Oid grantee, AclMode revoke_privs, Oid ownerId, DropBehavior behavior)
Definition: acl.c:1233
#define ACL_ID_PUBLIC
Definition: acl.h:47
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define ACL_N_SIZE(N)
Definition: acl.h:111
#define ACLITEM_SET_PRIVS_GOPTIONS(item, privs, goptions)
Definition: acl.h:83
static void check_acl(const Acl *acl)
Definition: acl.c:540
void check_is_member_of_role ( Oid  member,
Oid  role 
)

Definition at line 4874 of file acl.c.

References ereport, errcode(), errmsg(), ERROR, GetUserNameFromId(), and is_member_of_role().

Referenced by AlterDatabaseOwner(), AlterForeignServerOwner_internal(), AlterObjectOwner_internal(), AlterPublicationOwner_internal(), AlterSchemaOwner_internal(), AlterTypeOwner(), ATExecChangeOwner(), createdb(), CreateSchemaCommand(), and ExecAlterDefaultPrivilegesStmt().

4875 {
4876  if (!is_member_of_role(member, role))
4877  ereport(ERROR,
4878  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4879  errmsg("must be member of role \"%s\"",
4880  GetUserNameFromId(role, false))));
4881 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4852
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:692
int errmsg(const char *fmt,...)
Definition: elog.c:797
void check_rolespec_name ( const RoleSpec role,
const char *  detail_msg 
)

Definition at line 5248 of file acl.c.

References ereport, errcode(), errdetail(), errmsg(), ERROR, IsReservedName(), RoleSpec::rolename, ROLESPEC_CSTRING, and RoleSpec::roletype.

Referenced by AlterRole(), and AlterRoleSet().

5249 {
5250  if (!role)
5251  return;
5252 
5253  if (role->roletype != ROLESPEC_CSTRING)
5254  return;
5255 
5256  if (IsReservedName(role->rolename))
5257  {
5258  if (detail_msg)
5259  ereport(ERROR,
5260  (errcode(ERRCODE_RESERVED_NAME),
5261  errmsg("role name \"%s\" is reserved",
5262  role->rolename),
5263  errdetail("%s", detail_msg)));
5264  else
5265  ereport(ERROR,
5266  (errcode(ERRCODE_RESERVED_NAME),
5267  errmsg("role name \"%s\" is reserved",
5268  role->rolename)));
5269  }
5270 }
int errcode(int sqlerrcode)
Definition: elog.c:575
bool IsReservedName(const char *name)
Definition: catalog.c:194
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
RoleSpecType roletype
Definition: parsenodes.h:327
char * rolename
Definition: parsenodes.h:328
int errmsg(const char *fmt,...)
Definition: elog.c:797
void ExecAlterDefaultPrivilegesStmt ( ParseState pstate,
AlterDefaultPrivilegesStmt stmt 
)

Definition at line 863 of file aclchk.c.

References ACL_ALL_RIGHTS_FUNCTION, ACL_ALL_RIGHTS_NAMESPACE, ACL_ALL_RIGHTS_RELATION, ACL_ALL_RIGHTS_SEQUENCE, ACL_ALL_RIGHTS_TYPE, ACL_ID_PUBLIC, ACL_NO_RIGHTS, ACL_OBJECT_FUNCTION, ACL_OBJECT_NAMESPACE, ACL_OBJECT_RELATION, ACL_OBJECT_SEQUENCE, ACL_OBJECT_TYPE, AlterDefaultPrivilegesStmt::action, InternalDefaultACL::all_privs, DefElem::arg, InternalDefaultACL::behavior, GrantStmt::behavior, check_is_member_of_role(), AccessPriv::cols, DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, get_rolespec_oid(), gettext_noop, GetUserId(), InternalDefaultACL::grant_option, GrantStmt::grant_option, InternalDefaultACL::grantees, GrantStmt::grantees, InternalDefaultACL::is_grant, GrantStmt::is_grant, lappend_oid(), lfirst, DefElem::location, NIL, InternalDefaultACL::objtype, GrantStmt::objtype, AlterDefaultPrivilegesStmt::options, parser_errposition(), AccessPriv::priv_name, privilege_to_string(), InternalDefaultACL::privileges, GrantStmt::privileges, InternalDefaultACL::roleid, ROLESPEC_PUBLIC, RoleSpec::roletype, SetDefaultACLsInSchemas(), and string_to_privilege().

Referenced by ProcessUtilitySlow().

864 {
865  GrantStmt *action = stmt->action;
866  InternalDefaultACL iacls;
867  ListCell *cell;
868  List *rolespecs = NIL;
869  List *nspnames = NIL;
870  DefElem *drolespecs = NULL;
871  DefElem *dnspnames = NULL;
872  AclMode all_privileges;
873  const char *errormsg;
874 
875  /* Deconstruct the "options" part of the statement */
876  foreach(cell, stmt->options)
877  {
878  DefElem *defel = (DefElem *) lfirst(cell);
879 
880  if (strcmp(defel->defname, "schemas") == 0)
881  {
882  if (dnspnames)
883  ereport(ERROR,
884  (errcode(ERRCODE_SYNTAX_ERROR),
885  errmsg("conflicting or redundant options"),
886  parser_errposition(pstate, defel->location)));
887  dnspnames = defel;
888  }
889  else if (strcmp(defel->defname, "roles") == 0)
890  {
891  if (drolespecs)
892  ereport(ERROR,
893  (errcode(ERRCODE_SYNTAX_ERROR),
894  errmsg("conflicting or redundant options"),
895  parser_errposition(pstate, defel->location)));
896  drolespecs = defel;
897  }
898  else
899  elog(ERROR, "option \"%s\" not recognized", defel->defname);
900  }
901 
902  if (dnspnames)
903  nspnames = (List *) dnspnames->arg;
904  if (drolespecs)
905  rolespecs = (List *) drolespecs->arg;
906 
907  /* Prepare the InternalDefaultACL representation of the statement */
908  /* roleid to be filled below */
909  /* nspid to be filled in SetDefaultACLsInSchemas */
910  iacls.is_grant = action->is_grant;
911  iacls.objtype = action->objtype;
912  /* all_privs to be filled below */
913  /* privileges to be filled below */
914  iacls.grantees = NIL; /* filled below */
915  iacls.grant_option = action->grant_option;
916  iacls.behavior = action->behavior;
917 
918  /*
919  * Convert the RoleSpec list into an Oid list. Note that at this point we
920  * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
921  * there shouldn't be any additional work needed to support this case.
922  */
923  foreach(cell, action->grantees)
924  {
925  RoleSpec *grantee = (RoleSpec *) lfirst(cell);
926  Oid grantee_uid;
927 
928  switch (grantee->roletype)
929  {
930  case ROLESPEC_PUBLIC:
931  grantee_uid = ACL_ID_PUBLIC;
932  break;
933  default:
934  grantee_uid = get_rolespec_oid(grantee, false);
935  break;
936  }
937  iacls.grantees = lappend_oid(iacls.grantees, grantee_uid);
938  }
939 
940  /*
941  * Convert action->privileges, a list of privilege strings, into an
942  * AclMode bitmask.
943  */
944  switch (action->objtype)
945  {
946  case ACL_OBJECT_RELATION:
947  all_privileges = ACL_ALL_RIGHTS_RELATION;
948  errormsg = gettext_noop("invalid privilege type %s for relation");
949  break;
950  case ACL_OBJECT_SEQUENCE:
951  all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
952  errormsg = gettext_noop("invalid privilege type %s for sequence");
953  break;
954  case ACL_OBJECT_FUNCTION:
955  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
956  errormsg = gettext_noop("invalid privilege type %s for function");
957  break;
958  case ACL_OBJECT_TYPE:
959  all_privileges = ACL_ALL_RIGHTS_TYPE;
960  errormsg = gettext_noop("invalid privilege type %s for type");
961  break;
963  all_privileges = ACL_ALL_RIGHTS_NAMESPACE;
964  errormsg = gettext_noop("invalid privilege type %s for schema");
965  break;
966  default:
967  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
968  (int) action->objtype);
969  /* keep compiler quiet */
970  all_privileges = ACL_NO_RIGHTS;
971  errormsg = NULL;
972  }
973 
974  if (action->privileges == NIL)
975  {
976  iacls.all_privs = true;
977 
978  /*
979  * will be turned into ACL_ALL_RIGHTS_* by the internal routines
980  * depending on the object type
981  */
982  iacls.privileges = ACL_NO_RIGHTS;
983  }
984  else
985  {
986  iacls.all_privs = false;
987  iacls.privileges = ACL_NO_RIGHTS;
988 
989  foreach(cell, action->privileges)
990  {
991  AccessPriv *privnode = (AccessPriv *) lfirst(cell);
992  AclMode priv;
993 
994  if (privnode->cols)
995  ereport(ERROR,
996  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
997  errmsg("default privileges cannot be set for columns")));
998 
999  if (privnode->priv_name == NULL) /* parser mistake? */
1000  elog(ERROR, "AccessPriv node must specify privilege");
1001  priv = string_to_privilege(privnode->priv_name);
1002 
1003  if (priv & ~((AclMode) all_privileges))
1004  ereport(ERROR,
1005  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1006  errmsg(errormsg, privilege_to_string(priv))));
1007 
1008  iacls.privileges |= priv;
1009  }
1010  }
1011 
1012  if (rolespecs == NIL)
1013  {
1014  /* Set permissions for myself */
1015  iacls.roleid = GetUserId();
1016 
1017  SetDefaultACLsInSchemas(&iacls, nspnames);
1018  }
1019  else
1020  {
1021  /* Look up the role OIDs and do permissions checks */
1022  ListCell *rolecell;
1023 
1024  foreach(rolecell, rolespecs)
1025  {
1026  RoleSpec *rolespec = lfirst(rolecell);
1027 
1028  iacls.roleid = get_rolespec_oid(rolespec, false);
1029 
1030  /*
1031  * We insist that calling user be a member of each target role. If
1032  * he has that, he could become that role anyway via SET ROLE, so
1033  * FOR ROLE is just a syntactic convenience and doesn't give any
1034  * special privileges.
1035  */
1037 
1038  SetDefaultACLsInSchemas(&iacls, nspnames);
1039  }
1040  }
1041 }
#define NIL
Definition: pg_list.h:69
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:163
bool grant_option
Definition: aclchk.c:93
Oid GetUserId(void)
Definition: miscinit.c:284
static AclMode string_to_privilege(const char *privname)
Definition: aclchk.c:3215
bool grant_option
Definition: parsenodes.h:1867
#define gettext_noop(x)
Definition: c.h:139
int errcode(int sqlerrcode)
Definition: elog.c:575
List * cols
Definition: parsenodes.h:1897
unsigned int Oid
Definition: postgres_ext.h:31
GrantObjectType objtype
Definition: aclchk.c:89
List * grantees
Definition: aclchk.c:92
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
uint32 AclMode
Definition: parsenodes.h:70
#define ERROR
Definition: elog.h:43
#define ACL_NO_RIGHTS
Definition: parsenodes.h:86
int location
Definition: parsenodes.h:722
#define ACL_ALL_RIGHTS_TYPE
Definition: acl.h:168
void check_is_member_of_role(Oid member, Oid role)
Definition: acl.c:4874
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:720
#define ACL_ALL_RIGHTS_NAMESPACE
Definition: acl.h:166
bool is_grant
Definition: parsenodes.h:1859
RoleSpecType roletype
Definition: parsenodes.h:327
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition: acl.c:5144
#define ACL_ALL_RIGHTS_SEQUENCE
Definition: acl.h:159
DropBehavior behavior
Definition: aclchk.c:94
List * privileges
Definition: parsenodes.h:1864
DropBehavior behavior
Definition: parsenodes.h:1868
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
static void SetDefaultACLsInSchemas(InternalDefaultACL *iacls, List *nspnames)
Definition: aclchk.c:1049
GrantObjectType objtype
Definition: parsenodes.h:1861
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:158
#define ACL_ID_PUBLIC
Definition: acl.h:47
char * defname
Definition: parsenodes.h:719
static const char * privilege_to_string(AclMode privilege)
Definition: aclchk.c:3252
List * grantees
Definition: parsenodes.h:1866
#define elog
Definition: elog.h:219
AclMode privileges
Definition: aclchk.c:91
Definition: pg_list.h:45
char * priv_name
Definition: parsenodes.h:1896
void ExecuteGrantStmt ( GrantStmt stmt)

Definition at line 385 of file aclchk.c.

References ACL_ALL_RIGHTS_DATABASE, ACL_ALL_RIGHTS_FDW, ACL_ALL_RIGHTS_FOREIGN_SERVER, ACL_ALL_RIGHTS_FUNCTION, ACL_ALL_RIGHTS_LANGUAGE, ACL_ALL_RIGHTS_LARGEOBJECT, ACL_ALL_RIGHTS_NAMESPACE, ACL_ALL_RIGHTS_RELATION, ACL_ALL_RIGHTS_SEQUENCE, ACL_ALL_RIGHTS_TABLESPACE, ACL_ALL_RIGHTS_TYPE, ACL_ID_PUBLIC, ACL_NO_RIGHTS, ACL_OBJECT_DATABASE, ACL_OBJECT_DOMAIN, ACL_OBJECT_FDW, ACL_OBJECT_FOREIGN_SERVER, ACL_OBJECT_FUNCTION, ACL_OBJECT_LANGUAGE, ACL_OBJECT_LARGEOBJECT, ACL_OBJECT_NAMESPACE, ACL_OBJECT_RELATION, ACL_OBJECT_SEQUENCE, ACL_OBJECT_TABLESPACE, ACL_OBJECT_TYPE, ACL_TARGET_ALL_IN_SCHEMA, ACL_TARGET_OBJECT, InternalGrant::all_privs, InternalGrant::behavior, GrantStmt::behavior, InternalGrant::col_privs, AccessPriv::cols, elog, ereport, errcode(), errmsg(), ERROR, ExecGrantStmt_oids(), get_rolespec_oid(), gettext_noop, InternalGrant::grant_option, GrantStmt::grant_option, InternalGrant::grantees, GrantStmt::grantees, InternalGrant::is_grant, GrantStmt::is_grant, lappend(), lappend_oid(), lfirst, NIL, objectNamesToOids(), InternalGrant::objects, GrantStmt::objects, objectsInSchemaToOids(), InternalGrant::objtype, GrantStmt::objtype, AccessPriv::priv_name, privilege_to_string(), InternalGrant::privileges, GrantStmt::privileges, ROLESPEC_PUBLIC, RoleSpec::roletype, string_to_privilege(), and GrantStmt::targtype.

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

386 {
387  InternalGrant istmt;
388  ListCell *cell;
389  const char *errormsg;
390  AclMode all_privileges;
391 
392  /*
393  * Turn the regular GrantStmt into the InternalGrant form.
394  */
395  istmt.is_grant = stmt->is_grant;
396  istmt.objtype = stmt->objtype;
397 
398  /* Collect the OIDs of the target objects */
399  switch (stmt->targtype)
400  {
401  case ACL_TARGET_OBJECT:
402  istmt.objects = objectNamesToOids(stmt->objtype, stmt->objects);
403  break;
405  istmt.objects = objectsInSchemaToOids(stmt->objtype, stmt->objects);
406  break;
407  /* ACL_TARGET_DEFAULTS should not be seen here */
408  default:
409  elog(ERROR, "unrecognized GrantStmt.targtype: %d",
410  (int) stmt->targtype);
411  }
412 
413  /* all_privs to be filled below */
414  /* privileges to be filled below */
415  istmt.col_privs = NIL; /* may get filled below */
416  istmt.grantees = NIL; /* filled below */
417  istmt.grant_option = stmt->grant_option;
418  istmt.behavior = stmt->behavior;
419 
420  /*
421  * Convert the RoleSpec list into an Oid list. Note that at this point we
422  * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
423  * there shouldn't be any additional work needed to support this case.
424  */
425  foreach(cell, stmt->grantees)
426  {
427  RoleSpec *grantee = (RoleSpec *) lfirst(cell);
428  Oid grantee_uid;
429 
430  switch (grantee->roletype)
431  {
432  case ROLESPEC_PUBLIC:
433  grantee_uid = ACL_ID_PUBLIC;
434  break;
435  default:
436  grantee_uid = get_rolespec_oid(grantee, false);
437  break;
438  }
439  istmt.grantees = lappend_oid(istmt.grantees, grantee_uid);
440  }
441 
442  /*
443  * Convert stmt->privileges, a list of AccessPriv nodes, into an AclMode
444  * bitmask. Note: objtype can't be ACL_OBJECT_COLUMN.
445  */
446  switch (stmt->objtype)
447  {
448  /*
449  * Because this might be a sequence, we test both relation and
450  * sequence bits, and later do a more limited test when we know
451  * the object type.
452  */
453  case ACL_OBJECT_RELATION:
455  errormsg = gettext_noop("invalid privilege type %s for relation");
456  break;
457  case ACL_OBJECT_SEQUENCE:
458  all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
459  errormsg = gettext_noop("invalid privilege type %s for sequence");
460  break;
461  case ACL_OBJECT_DATABASE:
462  all_privileges = ACL_ALL_RIGHTS_DATABASE;
463  errormsg = gettext_noop("invalid privilege type %s for database");
464  break;
465  case ACL_OBJECT_DOMAIN:
466  all_privileges = ACL_ALL_RIGHTS_TYPE;
467  errormsg = gettext_noop("invalid privilege type %s for domain");
468  break;
469  case ACL_OBJECT_FUNCTION:
470  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
471  errormsg = gettext_noop("invalid privilege type %s for function");
472  break;
473  case ACL_OBJECT_LANGUAGE:
474  all_privileges = ACL_ALL_RIGHTS_LANGUAGE;
475  errormsg = gettext_noop("invalid privilege type %s for language");
476  break;
478  all_privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
479  errormsg = gettext_noop("invalid privilege type %s for large object");
480  break;
482  all_privileges = ACL_ALL_RIGHTS_NAMESPACE;
483  errormsg = gettext_noop("invalid privilege type %s for schema");
484  break;
486  all_privileges = ACL_ALL_RIGHTS_TABLESPACE;
487  errormsg = gettext_noop("invalid privilege type %s for tablespace");
488  break;
489  case ACL_OBJECT_TYPE:
490  all_privileges = ACL_ALL_RIGHTS_TYPE;
491  errormsg = gettext_noop("invalid privilege type %s for type");
492  break;
493  case ACL_OBJECT_FDW:
494  all_privileges = ACL_ALL_RIGHTS_FDW;
495  errormsg = gettext_noop("invalid privilege type %s for foreign-data wrapper");
496  break;
498  all_privileges = ACL_ALL_RIGHTS_FOREIGN_SERVER;
499  errormsg = gettext_noop("invalid privilege type %s for foreign server");
500  break;
501  default:
502  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
503  (int) stmt->objtype);
504  /* keep compiler quiet */
505  all_privileges = ACL_NO_RIGHTS;
506  errormsg = NULL;
507  }
508 
509  if (stmt->privileges == NIL)
510  {
511  istmt.all_privs = true;
512 
513  /*
514  * will be turned into ACL_ALL_RIGHTS_* by the internal routines
515  * depending on the object type
516  */
517  istmt.privileges = ACL_NO_RIGHTS;
518  }
519  else
520  {
521  istmt.all_privs = false;
522  istmt.privileges = ACL_NO_RIGHTS;
523 
524  foreach(cell, stmt->privileges)
525  {
526  AccessPriv *privnode = (AccessPriv *) lfirst(cell);
527  AclMode priv;
528 
529  /*
530  * If it's a column-level specification, we just set it aside in
531  * col_privs for the moment; but insist it's for a relation.
532  */
533  if (privnode->cols)
534  {
535  if (stmt->objtype != ACL_OBJECT_RELATION)
536  ereport(ERROR,
537  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
538  errmsg("column privileges are only valid for relations")));
539  istmt.col_privs = lappend(istmt.col_privs, privnode);
540  continue;
541  }
542 
543  if (privnode->priv_name == NULL) /* parser mistake? */
544  elog(ERROR, "AccessPriv node must specify privilege or columns");
545  priv = string_to_privilege(privnode->priv_name);
546 
547  if (priv & ~((AclMode) all_privileges))
548  ereport(ERROR,
549  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
550  errmsg(errormsg, privilege_to_string(priv))));
551 
552  istmt.privileges |= priv;
553  }
554  }
555 
556  ExecGrantStmt_oids(&istmt);
557 }
#define NIL
Definition: pg_list.h:69
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:163
static AclMode string_to_privilege(const char *privname)
Definition: aclchk.c:3215
AclMode privileges
bool grant_option
Definition: parsenodes.h:1867
#define gettext_noop(x)
Definition: c.h:139
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ACL_ALL_RIGHTS_TABLESPACE
Definition: acl.h:167
List * cols
Definition: parsenodes.h:1897
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
static List * objectsInSchemaToOids(GrantObjectType objtype, List *nspnames)
Definition: aclchk.c:756
#define ACL_ALL_RIGHTS_LANGUAGE
Definition: acl.h:164
GrantObjectType objtype
uint32 AclMode
Definition: parsenodes.h:70
#define ERROR
Definition: elog.h:43
#define ACL_NO_RIGHTS
Definition: parsenodes.h:86
#define ACL_ALL_RIGHTS_TYPE
Definition: acl.h:168
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define ACL_ALL_RIGHTS_NAMESPACE
Definition: acl.h:166
bool is_grant
Definition: parsenodes.h:1859
static void ExecGrantStmt_oids(InternalGrant *istmt)
Definition: aclchk.c:565
RoleSpecType roletype
Definition: parsenodes.h:327
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition: acl.c:5144
#define ACL_ALL_RIGHTS_SEQUENCE
Definition: acl.h:159
List * privileges
Definition: parsenodes.h:1864
#define ACL_ALL_RIGHTS_LARGEOBJECT
Definition: acl.h:165
DropBehavior behavior
DropBehavior behavior
Definition: parsenodes.h:1868
#define lfirst(lc)
Definition: pg_list.h:106
List * objects
Definition: parsenodes.h:1862
#define ACL_ALL_RIGHTS_DATABASE
Definition: acl.h:160
#define ACL_ALL_RIGHTS_FOREIGN_SERVER
Definition: acl.h:162
GrantObjectType objtype
Definition: parsenodes.h:1861
static List * objectNamesToOids(GrantObjectType objtype, List *objnames)
Definition: aclchk.c:627
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:158
#define ACL_ID_PUBLIC
Definition: acl.h:47
#define ACL_ALL_RIGHTS_FDW
Definition: acl.h:161
static const char * privilege_to_string(AclMode privilege)
Definition: aclchk.c:3252
List * grantees
Definition: parsenodes.h:1866
#define elog
Definition: elog.h:219
GrantTargetType targtype
Definition: parsenodes.h:1860
char * priv_name
Definition: parsenodes.h:1896
Oid get_role_oid ( const char *  rolename,
bool  missing_ok 
)

Definition at line 5111 of file acl.c.

References AUTHNAME, CStringGetDatum, ereport, errcode(), errmsg(), ERROR, GetSysCacheOid1, and OidIsValid.

Referenced by aclparse(), check_hba(), createdb(), CreateRole(), get_object_address_unqualified(), get_role_oid_or_public(), get_rolespec_oid(), GrantRole(), is_member(), pg_has_role_id_name(), pg_has_role_name(), pg_has_role_name_id(), pg_has_role_name_name(), regrolein(), and to_regrole().

5112 {
5113  Oid oid;
5114 
5115  oid = GetSysCacheOid1(AUTHNAME, CStringGetDatum(rolname));
5116  if (!OidIsValid(oid) && !missing_ok)
5117  ereport(ERROR,
5118  (errcode(ERRCODE_UNDEFINED_OBJECT),
5119  errmsg("role \"%s\" does not exist", rolname)));
5120  return oid;
5121 }
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:532
#define GetSysCacheOid1(cacheId, key1)
Definition: syscache.h:186
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:584
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
char* get_rolespec_name ( const RoleSpec role)

Definition at line 5226 of file acl.c.

References get_rolespec_tuple(), GETSTRUCT, NameStr, pstrdup(), and ReleaseSysCache().

Referenced by AddRoleMems(), and DelRoleMems().

5227 {
5228  HeapTuple tp;
5229  Form_pg_authid authForm;
5230  char *rolename;
5231 
5232  tp = get_rolespec_tuple(role);
5233  authForm = (Form_pg_authid) GETSTRUCT(tp);
5234  rolename = pstrdup(NameStr(authForm->rolname));
5235  ReleaseSysCache(tp);
5236 
5237  return rolename;
5238 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
char * pstrdup(const char *in)
Definition: mcxt.c:1076
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:72
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
HeapTuple get_rolespec_tuple(const RoleSpec *role)
Definition: acl.c:5182
#define NameStr(name)
Definition: c.h:493
Oid get_rolespec_oid ( const RoleSpec role,
bool  missing_ok 
)

Definition at line 5144 of file acl.c.

References Assert, elog, ereport, errcode(), errmsg(), ERROR, get_role_oid(), GetSessionUserId(), GetUserId(), InvalidOid, RoleSpec::rolename, ROLESPEC_CSTRING, ROLESPEC_CURRENT_USER, ROLESPEC_PUBLIC, ROLESPEC_SESSION_USER, and RoleSpec::roletype.

Referenced by AlterUserMapping(), ATExecCmd(), CreateSchemaCommand(), CreateTableSpace(), CreateUserMapping(), ExecAlterDefaultPrivilegesStmt(), ExecAlterOwnerStmt(), ExecuteGrantStmt(), GrantRole(), policy_role_list_to_array(), ReassignOwnedObjects(), RemoveUserMapping(), and roleSpecsToIds().

5145 {
5146  Oid oid;
5147 
5148  switch (role->roletype)
5149  {
5150  case ROLESPEC_CSTRING:
5151  Assert(role->rolename);
5152  oid = get_role_oid(role->rolename, missing_ok);
5153  break;
5154 
5155  case ROLESPEC_CURRENT_USER:
5156  oid = GetUserId();
5157  break;
5158 
5159  case ROLESPEC_SESSION_USER:
5160  oid = GetSessionUserId();
5161  break;
5162 
5163  case ROLESPEC_PUBLIC:
5164  ereport(ERROR,
5165  (errcode(ERRCODE_UNDEFINED_OBJECT),
5166  errmsg("role \"%s\" does not exist", "public")));
5167  oid = InvalidOid; /* make compiler happy */
5168  break;
5169 
5170  default:
5171  elog(ERROR, "unexpected role type %d", role->roletype);
5172  }
5173 
5174  return oid;
5175 }
Oid GetUserId(void)
Definition: miscinit.c:284
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
Oid GetSessionUserId(void)
Definition: miscinit.c:318
Oid get_role_oid(const char *rolname, bool missing_ok)
Definition: acl.c:5111
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
RoleSpecType roletype
Definition: parsenodes.h:327
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:664
char * rolename
Definition: parsenodes.h:328
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
HeapTuple get_rolespec_tuple ( const RoleSpec role)

Definition at line 5182 of file acl.c.

References Assert, AUTHNAME, AUTHOID, CStringGetDatum, elog, ereport, errcode(), errmsg(), ERROR, GetSessionUserId(), GetUserId(), HeapTupleIsValid, RoleSpec::rolename, ROLESPEC_CSTRING, ROLESPEC_CURRENT_USER, ROLESPEC_PUBLIC, ROLESPEC_SESSION_USER, RoleSpec::roletype, and SearchSysCache1.

Referenced by AlterRole(), AlterRoleSet(), CreateRole(), and get_rolespec_name().

5183 {
5184  HeapTuple tuple;
5185 
5186  switch (role->roletype)
5187  {
5188  case ROLESPEC_CSTRING:
5189  Assert(role->rolename);
5191  if (!HeapTupleIsValid(tuple))
5192  ereport(ERROR,
5193  (errcode(ERRCODE_UNDEFINED_OBJECT),
5194  errmsg("role \"%s\" does not exist", role->rolename)));
5195  break;
5196 
5197  case ROLESPEC_CURRENT_USER:
5198  tuple = SearchSysCache1(AUTHOID, GetUserId());
5199  if (!HeapTupleIsValid(tuple))
5200  elog(ERROR, "cache lookup failed for role %u", GetUserId());
5201  break;
5202 
5203  case ROLESPEC_SESSION_USER:
5205  if (!HeapTupleIsValid(tuple))
5206  elog(ERROR, "cache lookup failed for role %u", GetSessionUserId());
5207  break;
5208 
5209  case ROLESPEC_PUBLIC:
5210  ereport(ERROR,
5211  (errcode(ERRCODE_UNDEFINED_OBJECT),
5212  errmsg("role \"%s\" does not exist", "public")));
5213  tuple = NULL; /* make compiler happy */
5214 
5215  default:
5216  elog(ERROR, "unexpected role type %d", role->roletype);
5217  }
5218 
5219  return tuple;
5220 }
Oid GetUserId(void)
Definition: miscinit.c:284
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid GetSessionUserId(void)
Definition: miscinit.c:318
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:584
#define ereport(elevel, rest)
Definition: elog.h:122
RoleSpecType roletype
Definition: parsenodes.h:327
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:664
char * rolename
Definition: parsenodes.h:328
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Acl* get_user_default_acl ( GrantObjectType  objtype,
Oid  ownerId,
Oid  nsp_oid 
)

Definition at line 5246 of file aclchk.c.

References ACL_OBJECT_FUNCTION, ACL_OBJECT_NAMESPACE, ACL_OBJECT_RELATION, ACL_OBJECT_SEQUENCE, ACL_OBJECT_TYPE, acldefault(), aclequal(), aclitemsort(), aclmerge(), DEFACLOBJ_FUNCTION, DEFACLOBJ_NAMESPACE, DEFACLOBJ_RELATION, DEFACLOBJ_SEQUENCE, DEFACLOBJ_TYPE, get_default_acl_internal(), InvalidOid, and IsBootstrapProcessingMode.

Referenced by heap_create_with_catalog(), NamespaceCreate(), ProcedureCreate(), and TypeCreate().

5247 {
5248  Acl *result;
5249  Acl *glob_acl;
5250  Acl *schema_acl;
5251  Acl *def_acl;
5252  char defaclobjtype;
5253 
5254  /*
5255  * Use NULL during bootstrap, since pg_default_acl probably isn't there
5256  * yet.
5257  */
5259  return NULL;
5260 
5261  /* Check if object type is supported in pg_default_acl */
5262  switch (objtype)
5263  {
5264  case ACL_OBJECT_RELATION:
5265  defaclobjtype = DEFACLOBJ_RELATION;
5266  break;
5267 
5268  case ACL_OBJECT_SEQUENCE:
5269  defaclobjtype = DEFACLOBJ_SEQUENCE;
5270  break;
5271 
5272  case ACL_OBJECT_FUNCTION:
5273  defaclobjtype = DEFACLOBJ_FUNCTION;
5274  break;
5275 
5276  case ACL_OBJECT_TYPE:
5277  defaclobjtype = DEFACLOBJ_TYPE;
5278  break;
5279 
5280  case ACL_OBJECT_NAMESPACE:
5281  defaclobjtype = DEFACLOBJ_NAMESPACE;
5282  break;
5283 
5284  default:
5285  return NULL;
5286  }
5287 
5288  /* Look up the relevant pg_default_acl entries */
5289  glob_acl = get_default_acl_internal(ownerId, InvalidOid, defaclobjtype);
5290  schema_acl = get_default_acl_internal(ownerId, nsp_oid, defaclobjtype);
5291 
5292  /* Quick out if neither entry exists */
5293  if (glob_acl == NULL && schema_acl == NULL)
5294  return NULL;
5295 
5296  /* We need to know the hard-wired default value, too */
5297  def_acl = acldefault(objtype, ownerId);
5298 
5299  /* If there's no global entry, substitute the hard-wired default */
5300  if (glob_acl == NULL)
5301  glob_acl = def_acl;
5302 
5303  /* Merge in any per-schema privileges */
5304  result = aclmerge(glob_acl, schema_acl, ownerId);
5305 
5306  /*
5307  * For efficiency, we want to return NULL if the result equals default.
5308  * This requires sorting both arrays to get an accurate comparison.
5309  */
5310  aclitemsort(result);
5311  aclitemsort(def_acl);
5312  if (aclequal(result, def_acl))
5313  result = NULL;
5314 
5315  return result;
5316 }
Acl * acldefault(GrantObjectType objtype, Oid ownerId)
Definition: acl.c:746
static Acl * get_default_acl_internal(Oid roleId, Oid nsp_oid, char objtype)
Definition: aclchk.c:5214
bool aclequal(const Acl *left_acl, const Acl *right_acl)
Definition: acl.c:509
#define DEFACLOBJ_SEQUENCE
#define DEFACLOBJ_TYPE
#define InvalidOid
Definition: postgres_ext.h:36
#define DEFACLOBJ_NAMESPACE
#define DEFACLOBJ_RELATION
Acl * aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId)
Definition: acl.c:451
#define DEFACLOBJ_FUNCTION
void aclitemsort(Acl *acl)
Definition: acl.c:495
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:368
bool has_bypassrls_privilege ( Oid  roleid)

Definition at line 5190 of file aclchk.c.

References AUTHOID, GETSTRUCT, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1, and superuser_arg().

Referenced by check_enable_rls(), and RI_Initial_Check().

5191 {
5192  bool result = false;
5193  HeapTuple utup;
5194 
5195  /* Superusers bypass all permission checking. */
5196  if (superuser_arg(roleid))
5197  return true;
5198 
5199  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5200  if (HeapTupleIsValid(utup))
5201  {
5202  result = ((Form_pg_authid) GETSTRUCT(utup))->rolbypassrls;
5203  ReleaseSysCache(utup);
5204  }
5205  return result;
5206 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:72
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
bool has_createrole_privilege ( Oid  roleid)

Definition at line 5171 of file aclchk.c.

References AUTHOID, GETSTRUCT, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1, and superuser_arg().

Referenced by check_object_ownership(), and have_createrole_privilege().

5172 {
5173  bool result = false;
5174  HeapTuple utup;
5175 
5176  /* Superusers bypass all permission checking. */
5177  if (superuser_arg(roleid))
5178  return true;
5179 
5180  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5181  if (HeapTupleIsValid(utup))
5182  {
5183  result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreaterole;
5184  ReleaseSysCache(utup);
5185  }
5186  return result;
5187 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:72
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
bool has_privs_of_role ( Oid  member,
Oid  role 
)

Definition at line 4828 of file acl.c.

References list_member_oid(), roles_has_privs_of(), and superuser_arg().

Referenced by aclmask(), AlterObjectNamespace_internal(), AlterObjectOwner_internal(), AlterObjectRename_internal(), check_role_for_policy(), DropOwnedObjects(), pg_class_ownercheck(), pg_collation_ownercheck(), pg_conversion_ownercheck(), pg_database_ownercheck(), pg_event_trigger_ownercheck(), pg_extension_ownercheck(), pg_foreign_data_wrapper_ownercheck(), pg_foreign_server_ownercheck(), pg_language_ownercheck(), pg_largeobject_ownercheck(), pg_namespace_ownercheck(), pg_opclass_ownercheck(), pg_oper_ownercheck(), pg_opfamily_ownercheck(), pg_proc_ownercheck(), pg_publication_ownercheck(), pg_role_aclcheck(), pg_signal_backend(), pg_stat_get_activity(), pg_stat_get_backend_activity(), pg_stat_get_backend_activity_start(), pg_stat_get_backend_client_addr(), pg_stat_get_backend_client_port(), pg_stat_get_backend_start(), pg_stat_get_backend_wait_event(), pg_stat_get_backend_wait_event_type(), pg_stat_get_backend_xact_start(), pg_stat_get_progress_info(), pg_statistics_object_ownercheck(), pg_subscription_ownercheck(), pg_tablespace_ownercheck(), pg_ts_config_ownercheck(), pg_ts_dict_ownercheck(), pg_type_ownercheck(), and ReassignOwnedObjects().

4829 {
4830  /* Fast path for simple case */
4831  if (member == role)
4832  return true;
4833 
4834  /* Superusers have every privilege, so are part of every role */
4835  if (superuser_arg(member))
4836  return true;
4837 
4838  /*
4839  * Find all the roles that member has the privileges of, including
4840  * multi-level recursion, then see if target role is any one of them.
4841  */
4842  return list_member_oid(roles_has_privs_of(member), role);
4843 }
static List * roles_has_privs_of(Oid roleid)
Definition: acl.c:4668
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
void initialize_acl ( void  )

Definition at line 4609 of file acl.c.

References AUTHMEMROLEMEM, CacheRegisterSyscacheCallback(), IsBootstrapProcessingMode, and RoleMembershipCacheCallback().

Referenced by InitPostgres().

4610 {
4612  {
4613  /*
4614  * In normal mode, set a callback on any syscache invalidation of
4615  * pg_auth_members rows
4616  */
4619  (Datum) 0);
4620  }
4621 }
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
uintptr_t Datum
Definition: postgres.h:372
static void RoleMembershipCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: acl.c:4628
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:368
bool is_admin_of_role ( Oid  member,
Oid  role 
)

Definition at line 4910 of file acl.c.

References AUTHMEMMEMROLE, GetSessionUserId(), GETSTRUCT, i, InLocalUserIdChange(), InSecurityRestrictedOperation(), lfirst_oid, list_append_unique_oid(), list_free(), list_make1_oid, catclist::members, catclist::n_members, ObjectIdGetDatum, ReleaseSysCacheList, SearchSysCacheList1, superuser_arg(), and catctup::tuple.

Referenced by AddRoleMems(), DelRoleMems(), and pg_role_aclcheck().

4911 {
4912  bool result = false;
4913  List *roles_list;
4914  ListCell *l;
4915 
4916  if (superuser_arg(member))
4917  return true;
4918 
4919  if (member == role)
4920 
4921  /*
4922  * A role can admin itself when it matches the session user and we're
4923  * outside any security-restricted operation, SECURITY DEFINER or
4924  * similar context. SQL-standard roles cannot self-admin. However,
4925  * SQL-standard users are distinct from roles, and they are not
4926  * grantable like roles: PostgreSQL's role-user duality extends the
4927  * standard. Checking for a session user match has the effect of
4928  * letting a role self-admin only when it's conspicuously behaving
4929  * like a user. Note that allowing self-admin under a mere SET ROLE
4930  * would make WITH ADMIN OPTION largely irrelevant; any member could
4931  * SET ROLE to issue the otherwise-forbidden command.
4932  *
4933  * Withholding self-admin in a security-restricted operation prevents
4934  * object owners from harnessing the session user identity during
4935  * administrative maintenance. Suppose Alice owns a database, has
4936  * issued "GRANT alice TO bob", and runs a daily ANALYZE. Bob creates
4937  * an alice-owned SECURITY DEFINER function that issues "REVOKE alice
4938  * FROM carol". If he creates an expression index calling that
4939  * function, Alice will attempt the REVOKE during each ANALYZE.
4940  * Checking InSecurityRestrictedOperation() thwarts that attack.
4941  *
4942  * Withholding self-admin in SECURITY DEFINER functions makes their
4943  * behavior independent of the calling user. There's no security or
4944  * SQL-standard-conformance need for that restriction, though.
4945  *
4946  * A role cannot have actual WITH ADMIN OPTION on itself, because that
4947  * would imply a membership loop. Therefore, we're done either way.
4948  */
4949  return member == GetSessionUserId() &&
4951 
4952  /*
4953  * Find all the roles that member is a member of, including multi-level
4954  * recursion. We build a list in the same way that is_member_of_role does
4955  * to track visited and unvisited roles.
4956  */
4957  roles_list = list_make1_oid(member);
4958 
4959  foreach(l, roles_list)
4960  {
4961  Oid memberid = lfirst_oid(l);
4962  CatCList *memlist;
4963  int i;
4964 
4965  /* Find roles that memberid is directly a member of */
4967  ObjectIdGetDatum(memberid));
4968  for (i = 0; i < memlist->n_members; i++)
4969  {
4970  HeapTuple tup = &memlist->members[i]->tuple;
4971  Oid otherid = ((Form_pg_auth_members) GETSTRUCT(tup))->roleid;
4972 
4973  if (otherid == role &&
4974  ((Form_pg_auth_members) GETSTRUCT(tup))->admin_option)
4975  {
4976  /* Found what we came for, so can stop searching */
4977  result = true;
4978  break;
4979  }
4980 
4981  roles_list = list_append_unique_oid(roles_list, otherid);
4982  }
4983  ReleaseSysCacheList(memlist);
4984  if (result)
4985  break;
4986  }
4987 
4988  list_free(roles_list);
4989 
4990  return result;
4991 }
bool InLocalUserIdChange(void)
Definition: miscinit.c:407
int n_members
Definition: catcache.h:154
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:999
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
unsigned int Oid
Definition: postgres_ext.h:31
Oid GetSessionUserId(void)
Definition: miscinit.c:318
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:155
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:204
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
#define ReleaseSysCacheList(x)
Definition: syscache.h:213
#define list_make1_oid(x1)
Definition: pg_list.h:151
FormData_pg_auth_members * Form_pg_auth_members
bool InSecurityRestrictedOperation(void)
Definition: miscinit.c:416
void list_free(List *list)
Definition: list.c:1133
int i
HeapTupleData tuple
Definition: catcache.h:116
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
bool is_member_of_role ( Oid  member,
Oid  role 
)

Definition at line 4852 of file acl.c.

References list_member_oid(), roles_is_member_of(), and superuser_arg().

Referenced by calculate_database_size(), calculate_tablespace_size(), check_is_member_of_role(), check_role(), GetConfigOption(), GetConfigOptionByName(), GetConfigOptionByNum(), GetConfigOptionResetString(), pg_role_aclcheck(), pg_stat_get_activity(), pg_stat_get_wal_receiver(), pg_stat_statements_internal(), and pgrowlocks().

4853 {
4854  /* Fast path for simple case */
4855  if (member == role)
4856  return true;
4857 
4858  /* Superusers have every privilege, so are part of every role */
4859  if (superuser_arg(member))
4860  return true;
4861 
4862  /*
4863  * Find all the roles that member is a member of, including multi-level
4864  * recursion, then see if target role is any one of them.
4865  */
4866  return list_member_oid(roles_is_member_of(member), role);
4867 }
static List * roles_is_member_of(Oid roleid)
Definition: acl.c:4751
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
bool is_member_of_role_nosuper ( Oid  member,
Oid  role 
)

Definition at line 4890 of file acl.c.

References list_member_oid(), and roles_is_member_of().

Referenced by AddRoleMems(), and is_member().

4891 {
4892  /* Fast path for simple case */
4893  if (member == role)
4894  return true;
4895 
4896  /*
4897  * Find all the roles that member is a member of, including multi-level
4898  * recursion, then see if target role is any one of them.
4899  */
4900  return list_member_oid(roles_is_member_of(member), role);
4901 }
static List * roles_is_member_of(Oid roleid)
Definition: acl.c:4751
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
Acl* make_empty_acl ( void  )

Definition at line 398 of file acl.c.

References allocacl().

Referenced by SetDefaultACL().

399 {
400  return allocacl(0);
401 }
static Acl * allocacl(int n)
Definition: acl.c:376
AclResult pg_attribute_aclcheck ( Oid  table_oid,
AttrNumber  attnum,
Oid  roleid,
AclMode  mode 
)

Definition at line 4308 of file aclchk.c.

References ACLCHECK_NO_PRIV, ACLCHECK_OK, ACLMASK_ANY, and pg_attribute_aclmask().

Referenced by BuildIndexValueDescription(), checkFkeyPermissions(), column_privilege_check(), examine_simple_variable(), ExecBuildSlotPartitionKeyDescription(), ExecBuildSlotValueDescription(), ExecCheckRTEPerms(), ExecCheckRTEPermsModified(), and ri_ReportViolation().

4310 {
4311  if (pg_attribute_aclmask(table_oid, attnum, roleid, mode, ACLMASK_ANY) != 0)
4312  return ACLCHECK_OK;
4313  else
4314  return ACLCHECK_NO_PRIV;
4315 }
AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3537
AclResult pg_attribute_aclcheck_all ( Oid  table_oid,
Oid  roleid,
AclMode  mode,
AclMaskHow  how 
)

Definition at line 4337 of file aclchk.c.

References ACLCHECK_NO_PRIV, ACLCHECK_OK, ACLMASK_ALL, ACLMASK_ANY, Anum_pg_attribute_attacl, ATTNUM, GETSTRUCT, heap_attisnull(), HeapTupleIsValid, Int16GetDatum, ObjectIdGetDatum, pg_attribute_aclmask(), ReleaseSysCache(), RELOID, SearchSysCache1, and SearchSysCache2.

Referenced by ExecCheckRTEPerms(), ExecCheckRTEPermsModified(), has_any_column_privilege_id(), has_any_column_privilege_id_id(), has_any_column_privilege_id_name(), has_any_column_privilege_name(), has_any_column_privilege_name_id(), and has_any_column_privilege_name_name().

4339 {
4340  AclResult result;
4341  HeapTuple classTuple;
4342  Form_pg_class classForm;
4343  AttrNumber nattrs;
4344  AttrNumber curr_att;
4345 
4346  /*
4347  * Must fetch pg_class row to check number of attributes. As in
4348  * pg_attribute_aclmask, we prefer to return "no privileges" instead of
4349  * throwing an error if we get any unexpected lookup errors.
4350  */
4351  classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
4352  if (!HeapTupleIsValid(classTuple))
4353  return ACLCHECK_NO_PRIV;
4354  classForm = (Form_pg_class) GETSTRUCT(classTuple);
4355 
4356  nattrs = classForm->relnatts;
4357 
4358  ReleaseSysCache(classTuple);
4359 
4360  /*
4361  * Initialize result in case there are no non-dropped columns. We want to
4362  * report failure in such cases for either value of 'how'.
4363  */
4364  result = ACLCHECK_NO_PRIV;
4365 
4366  for (curr_att = 1; curr_att <= nattrs; curr_att++)
4367  {
4368  HeapTuple attTuple;
4369  AclMode attmask;
4370 
4371  attTuple = SearchSysCache2(ATTNUM,
4372  ObjectIdGetDatum(table_oid),
4373  Int16GetDatum(curr_att));
4374  if (!HeapTupleIsValid(attTuple))
4375  continue;
4376 
4377  /* ignore dropped columns */
4378  if (((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped)
4379  {
4380  ReleaseSysCache(attTuple);
4381  continue;
4382  }
4383 
4384  /*
4385  * Here we hard-wire knowledge that the default ACL for a column
4386  * grants no privileges, so that we can fall out quickly in the very
4387  * common case where attacl is null.
4388  */
4390  attmask = 0;
4391  else
4392  attmask = pg_attribute_aclmask(table_oid, curr_att, roleid,
4393  mode, ACLMASK_ANY);
4394 
4395  ReleaseSysCache(attTuple);
4396 
4397  if (attmask != 0)
4398  {
4399  result = ACLCHECK_OK;
4400  if (how == ACLMASK_ANY)
4401  break; /* succeed on any success */
4402  }
4403  else
4404  {
4405  result = ACLCHECK_NO_PRIV;
4406  if (how == ACLMASK_ALL)
4407  break; /* fail on any failure */
4408  }
4409  }
4410 
4411  return result;
4412 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3537
#define Int16GetDatum(X)
Definition: postgres.h:457
#define Anum_pg_attribute_attacl
Definition: pg_attribute.h:214
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
uint32 AclMode
Definition: parsenodes.h:70
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
bool heap_attisnull(HeapTuple tup, int attnum)
Definition: heaptuple.c:296
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
AclResult
Definition: acl.h:178
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
int16 AttrNumber
Definition: attnum.h:21
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:161
AclMode pg_attribute_aclmask ( Oid  table_oid,
AttrNumber  attnum,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 3537 of file aclchk.c.

References aclmask(), Anum_pg_attribute_attacl, ATTNUM, DatumGetAclP, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, HeapTupleIsValid, Int16GetDatum, ObjectIdGetDatum, pfree(), ReleaseSysCache(), RELOID, SearchSysCache1, SearchSysCache2, and SysCacheGetAttr().

Referenced by pg_aclmask(), pg_attribute_aclcheck(), and pg_attribute_aclcheck_all().

3539 {
3540  AclMode result;
3541  HeapTuple classTuple;
3542  HeapTuple attTuple;
3543  Form_pg_class classForm;
3544  Form_pg_attribute attributeForm;
3545  Datum aclDatum;
3546  bool isNull;
3547  Acl *acl;
3548  Oid ownerId;
3549 
3550  /*
3551  * First, get the column's ACL from its pg_attribute entry
3552  */
3553  attTuple = SearchSysCache2(ATTNUM,
3554  ObjectIdGetDatum(table_oid),
3555  Int16GetDatum(attnum));
3556  if (!HeapTupleIsValid(attTuple))
3557  ereport(ERROR,
3558  (errcode(ERRCODE_UNDEFINED_COLUMN),
3559  errmsg("attribute %d of relation with OID %u does not exist",
3560  attnum, table_oid)));
3561  attributeForm = (Form_pg_attribute) GETSTRUCT(attTuple);
3562 
3563  /* Throw error on dropped columns, too */
3564  if (attributeForm->attisdropped)
3565  ereport(ERROR,
3566  (errcode(ERRCODE_UNDEFINED_COLUMN),
3567  errmsg("attribute %d of relation with OID %u does not exist",
3568  attnum, table_oid)));
3569 
3570  aclDatum = SysCacheGetAttr(ATTNUM, attTuple, Anum_pg_attribute_attacl,
3571  &isNull);
3572 
3573  /*
3574  * Here we hard-wire knowledge that the default ACL for a column grants no
3575  * privileges, so that we can fall out quickly in the very common case
3576  * where attacl is null.
3577  */
3578  if (isNull)
3579  {
3580  ReleaseSysCache(attTuple);
3581  return 0;
3582  }
3583 
3584  /*
3585  * Must get the relation's ownerId from pg_class. Since we already found
3586  * a pg_attribute entry, the only likely reason for this to fail is that a
3587  * concurrent DROP of the relation committed since then (which could only
3588  * happen if we don't have lock on the relation). We prefer to report "no
3589  * privileges" rather than failing in such a case, so as to avoid unwanted
3590  * failures in has_column_privilege() tests.
3591  */
3592  classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
3593  if (!HeapTupleIsValid(classTuple))
3594  {
3595  ReleaseSysCache(attTuple);
3596  return 0;
3597  }
3598  classForm = (Form_pg_class) GETSTRUCT(classTuple);
3599 
3600  ownerId = classForm->relowner;
3601 
3602  ReleaseSysCache(classTuple);
3603 
3604  /* detoast column's ACL if necessary */
3605  acl = DatumGetAclP(aclDatum);
3606 
3607  result = aclmask(acl, roleid, ownerId, mask, how);
3608 
3609  /* if we have a detoasted copy, free it */
3610  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3611  pfree(acl);
3612 
3613  ReleaseSysCache(attTuple);
3614 
3615  return result;
3616 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define DatumGetAclP(X)
Definition: acl.h:121
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1319
#define Int16GetDatum(X)
Definition: postgres.h:457
int errcode(int sqlerrcode)
Definition: elog.c:575
#define Anum_pg_attribute_attacl
Definition: pg_attribute.h:214
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
uint32 AclMode
Definition: parsenodes.h:70
void pfree(void *pointer)
Definition: mcxt.c:949
char * Pointer
Definition: c.h:235
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1325
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define DatumGetPointer(X)
Definition: postgres.h:555
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:161
AclResult pg_class_aclcheck ( Oid  table_oid,
Oid  roleid,
AclMode  mode 
)

Definition at line 4422 of file aclchk.c.

References ACLCHECK_NO_PRIV, ACLCHECK_OK, ACLMASK_ANY, and pg_class_aclmask().

Referenced by BuildIndexValueDescription(), checkFkeyPermissions(), column_privilege_check(), CreateTrigger(), currtid_byrelname(), currtid_byreloid(), currval_oid(), do_setval(), examine_simple_variable(), examine_variable(), ExecBuildSlotPartitionKeyDescription(), ExecBuildSlotValueDescription(), get_rel_from_relname(), has_any_column_privilege_id(), has_any_column_privilege_id_id(), has_any_column_privilege_id_name(), has_any_column_privilege_name(), has_any_column_privilege_name_id(), has_any_column_privilege_name_name(), has_sequence_privilege_id(), has_sequence_privilege_id_id(), has_sequence_privilege_id_name(), has_sequence_privilege_name(), has_sequence_privilege_name_id(), has_sequence_privilege_name_name(), has_table_privilege_id(), has_table_privilege_id_id(), has_table_privilege_id_name(), has_table_privilege_name(), has_table_privilege_name_id(), has_table_privilege_name_name(), lastval(), LockTableAclCheck(), nextval_internal(), pg_prewarm(), pg_sequence_last_value(), pg_sequence_parameters(), pgrowlocks(), ri_ReportViolation(), transformTableLikeClause(), and truncate_check_rel().

4423 {
4424  if (pg_class_aclmask(table_oid, roleid, mode, ACLMASK_ANY) != 0)
4425  return ACLCHECK_OK;
4426  else
4427  return ACLCHECK_NO_PRIV;
4428 }
AclMode pg_class_aclmask(Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3622
AclMode pg_class_aclmask ( Oid  table_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 3622 of file aclchk.c.

References ACL_DELETE, ACL_INSERT, ACL_OBJECT_RELATION, ACL_OBJECT_SEQUENCE, ACL_TRUNCATE, ACL_UPDATE, ACL_USAGE, acldefault(), aclmask(), allowSystemTableMods, Anum_pg_class_relacl, DatumGetAclP, DatumGetPointer, DEBUG2, elog, ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg(), ERROR, GETSTRUCT, HeapTupleIsValid, IsSystemClass(), ObjectIdGetDatum, pfree(), ReleaseSysCache(), RELKIND_SEQUENCE, RELKIND_VIEW, RELOID, SearchSysCache1, superuser_arg(), and SysCacheGetAttr().

Referenced by ExecCheckRTEPerms(), pg_aclmask(), and pg_class_aclcheck().

3624 {
3625  AclMode result;
3626  HeapTuple tuple;
3627  Form_pg_class classForm;
3628  Datum aclDatum;
3629  bool isNull;
3630  Acl *acl;
3631  Oid ownerId;
3632 
3633  /*
3634  * Must get the relation's tuple from pg_class
3635  */
3636  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
3637  if (!HeapTupleIsValid(tuple))
3638  ereport(ERROR,
3640  errmsg("relation with OID %u does not exist",
3641  table_oid)));
3642  classForm = (Form_pg_class) GETSTRUCT(tuple);
3643 
3644  /*
3645  * Deny anyone permission to update a system catalog unless
3646  * pg_authid.rolsuper is set. Also allow it if allowSystemTableMods.
3647  *
3648  * As of 7.4 we have some updatable system views; those shouldn't be
3649  * protected in this way. Assume the view rules can take care of
3650  * themselves. ACL_USAGE is if we ever have system sequences.
3651  */
3652  if ((mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE | ACL_USAGE)) &&
3653  IsSystemClass(table_oid, classForm) &&
3654  classForm->relkind != RELKIND_VIEW &&
3655  !superuser_arg(roleid) &&
3657  {
3658 #ifdef ACLDEBUG
3659  elog(DEBUG2, "permission denied for system catalog update");
3660 #endif
3662  }
3663 
3664  /*
3665  * Otherwise, superusers bypass all permission-checking.
3666  */
3667  if (superuser_arg(roleid))
3668  {
3669 #ifdef ACLDEBUG
3670  elog(DEBUG2, "OID %u is superuser, home free", roleid);
3671 #endif
3672  ReleaseSysCache(tuple);
3673  return mask;
3674  }
3675 
3676  /*
3677  * Normal case: get the relation's ACL from pg_class
3678  */
3679  ownerId = classForm->relowner;
3680 
3681  aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
3682  &isNull);
3683  if (isNull)
3684  {
3685  /* No ACL, so build default ACL */
3686  switch (classForm->relkind)
3687  {
3688  case RELKIND_SEQUENCE:
3689  acl = acldefault(ACL_OBJECT_SEQUENCE, ownerId);
3690  break;
3691  default:
3692  acl = acldefault(ACL_OBJECT_RELATION, ownerId);
3693  break;
3694  }
3695  aclDatum = (Datum) 0;
3696  }
3697  else
3698  {
3699  /* detoast rel's ACL if necessary */
3700  acl = DatumGetAclP(aclDatum);
3701  }
3702 
3703  result = aclmask(acl, roleid, ownerId, mask, how);
3704 
3705  /* if we have a detoasted copy, free it */
3706  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3707  pfree(acl);
3708 
3709  ReleaseSysCache(tuple);
3710 
3711  return result;
3712 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
#define Anum_pg_class_relacl
Definition: pg_class.h:133
#define DatumGetAclP(X)
Definition: acl.h:121
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1319
Acl * acldefault(GrantObjectType objtype, Oid ownerId)
Definition: acl.c:746
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ACL_DELETE
Definition: parsenodes.h:75
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:75
uint32 AclMode
Definition: parsenodes.h:70
void pfree(void *pointer)
Definition: mcxt.c:949
char * Pointer
Definition: c.h:235
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define DEBUG2
Definition: elog.h:24
#define ACL_USAGE
Definition: parsenodes.h:80
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
#define ACL_UPDATE
Definition: parsenodes.h:74
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1325
bool allowSystemTableMods
Definition: globals.c:112
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define ACL_INSERT
Definition: parsenodes.h:72
#define DatumGetPointer(X)
Definition: postgres.h:555
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define RELKIND_VIEW
Definition: pg_class.h:164
#define elog
Definition: elog.h:219
#define RELKIND_SEQUENCE
Definition: pg_class.h:162
#define ACL_TRUNCATE
Definition: parsenodes.h:76
bool pg_class_ownercheck ( Oid  class_oid,
Oid  roleid 
)

Definition at line 4546 of file aclchk.c.

References ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg(), ERROR, GETSTRUCT, has_privs_of_role(), HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), RELOID, SearchSysCache1, and superuser_arg().

Referenced by AlterTableMoveAll(), analyze_rel(), ATExecChangeOwner(), ATPrepSetStatistics(), ATSimplePermissions(), brin_desummarize_range(), brin_summarize_range(), check_enable_rls(), check_object_ownership(), cluster_rel(), CreateStatistics(), DefineQueryRewrite(), EnableDisableRule(), ExecuteTruncate(), get_tables_to_cluster(), gin_clean_pending_list(), MergeAttributes(), PublicationAddTables(), RangeVarCallbackForAlterRelation(), RangeVarCallbackForDropRelation(), RangeVarCallbackForPolicy(), RangeVarCallbackForReindexIndex(), RangeVarCallbackForRenameRule(), RangeVarCallbackForRenameTrigger(), RangeVarCallbackOwnsRelation(), RangeVarCallbackOwnsTable(), RangeVarGetAndCheckCreationNamespace(), RemoveRoleFromObjectPolicy(), renameatt_check(), RI_Initial_Check(), and vacuum_rel().

4547 {
4548  HeapTuple tuple;
4549  Oid ownerId;
4550 
4551  /* Superusers bypass all permission checking. */
4552  if (superuser_arg(roleid))
4553  return true;
4554 
4555  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(class_oid));
4556  if (!HeapTupleIsValid(tuple))
4557  ereport(ERROR,
4559  errmsg("relation with OID %u does not exist", class_oid)));
4560 
4561  ownerId = ((Form_pg_class) GETSTRUCT(tuple))->relowner;
4562 
4563  ReleaseSysCache(tuple);
4564 
4565  return has_privs_of_role(roleid, ownerId);
4566 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4828
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool pg_collation_ownercheck ( Oid  coll_oid,
Oid  roleid 
)

Definition at line 4990 of file aclchk.c.

References COLLOID, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, has_privs_of_role(), HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1, and superuser_arg().

Referenced by AlterCollation(), and check_object_ownership().

4991 {
4992  HeapTuple tuple;
4993  Oid ownerId;
4994 
4995  /* Superusers bypass all permission checking. */
4996  if (superuser_arg(roleid))
4997  return true;
4998 
4999  tuple = SearchSysCache1(COLLOID, ObjectIdGetDatum(coll_oid));
5000  if (!HeapTupleIsValid(tuple))
5001  ereport(ERROR,
5002  (errcode(ERRCODE_UNDEFINED_OBJECT),
5003  errmsg("collation with OID %u does not exist", coll_oid)));
5004 
5005  ownerId = ((Form_pg_collation) GETSTRUCT(tuple))->collowner;
5006 
5007  ReleaseSysCache(tuple);
5008 
5009  return has_privs_of_role(roleid, ownerId);
5010 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4828
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool pg_conversion_ownercheck ( Oid  conv_oid,
Oid  roleid 
)

Definition at line 5016 of file aclchk.c.

References CONVOID, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, has_privs_of_role(), HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1, and superuser_arg().

Referenced by check_object_ownership().

5017 {
5018  HeapTuple tuple;
5019  Oid ownerId;
5020 
5021  /* Superusers bypass all permission checking. */
5022  if (superuser_arg(roleid))
5023  return true;
5024 
5025  tuple = SearchSysCache1(CONVOID, ObjectIdGetDatum(conv_oid));
5026  if (!HeapTupleIsValid(tuple))
5027  ereport(ERROR,
5028  (errcode(ERRCODE_UNDEFINED_OBJECT),
5029  errmsg("conversion with OID %u does not exist", conv_oid)));
5030 
5031  ownerId = ((Form_pg_conversion) GETSTRUCT(tuple))->conowner;
5032 
5033  ReleaseSysCache(tuple);
5034 
5035  return has_privs_of_role(roleid, ownerId);
5036 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4828
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
FormData_pg_conversion * Form_pg_conversion
Definition: pg_conversion.h:56
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
int errmsg(const char *fmt,...)
Definition: elog.c:797
AclMode pg_database_aclmask ( Oid  db_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 3718 of file aclchk.c.

References ACL_OBJECT_DATABASE, acldefault(), aclmask(), Anum_pg_database_datacl, DATABASEOID, DatumGetAclP, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, HeapTupleIsValid, ObjectIdGetDatum, pfree(), ReleaseSysCache(), SearchSysCache1, superuser_arg(), and SysCacheGetAttr().

Referenced by pg_aclmask(), and pg_database_aclcheck().

3720 {
3721  AclMode result;
3722  HeapTuple tuple;
3723  Datum aclDatum;
3724  bool isNull;
3725  Acl *acl;
3726  Oid ownerId;
3727 
3728  /* Superusers bypass all permission checking. */
3729  if (superuser_arg(roleid))
3730  return mask;
3731 
3732  /*
3733  * Get the database's ACL from pg_database
3734  */
3735  tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(db_oid));
3736  if (!HeapTupleIsValid(tuple))
3737  ereport(ERROR,
3738  (errcode(ERRCODE_UNDEFINED_DATABASE),
3739  errmsg("database with OID %u does not exist", db_oid)));
3740 
3741  ownerId = ((Form_pg_database) GETSTRUCT(tuple))->datdba;
3742 
3744  &isNull);
3745  if (isNull)
3746  {
3747  /* No ACL, so build default ACL */
3748  acl = acldefault(ACL_OBJECT_DATABASE, ownerId);
3749  aclDatum = (Datum) 0;
3750  }
3751  else
3752  {
3753  /* detoast ACL if necessary */
3754  acl = DatumGetAclP(aclDatum);
3755  }
3756 
3757  result = aclmask(acl, roleid, ownerId, mask, how);
3758 
3759  /* if we have a detoasted copy, free it */
3760  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3761  pfree(acl);
3762 
3763  ReleaseSysCache(tuple);
3764 
3765  return result;
3766 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
FormData_pg_database * Form_pg_database
Definition: pg_database.h:57
#define DatumGetAclP(X)
Definition: acl.h:121
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1319
Acl * acldefault(GrantObjectType objtype, Oid ownerId)
Definition: acl.c:746
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
uint32 AclMode
Definition: parsenodes.h:70
void pfree(void *pointer)
Definition: mcxt.c:949
char * Pointer
Definition: c.h:235
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
#define Anum_pg_database_datacl
Definition: pg_database.h:76
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1325
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define DatumGetPointer(X)
Definition: postgres.h:555
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool pg_database_ownercheck ( Oid  db_oid,
Oid  roleid 
)

Definition at line 4964 of file aclchk.c.

References DATABASEOID, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, has_privs_of_role(), HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1, and superuser_arg().

Referenced by AlterDatabase(), AlterDatabaseOwner(), AlterDatabaseSet(), AlterRoleSet(), analyze_rel(), check_object_ownership(), createdb(), CreateProceduralLanguage(), dropdb(), movedb(), ReindexMultipleTables(), RenameDatabase(), and vacuum_rel().

4965 {
4966  HeapTuple tuple;
4967  Oid dba;
4968 
4969  /* Superusers bypass all permission checking. */
4970  if (superuser_arg(roleid))
4971  return true;
4972 
4973  tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(db_oid));
4974  if (!HeapTupleIsValid(tuple))
4975  ereport(ERROR,
4976  (errcode(ERRCODE_UNDEFINED_DATABASE),
4977  errmsg("database with OID %u does not exist", db_oid)));
4978 
4979  dba = ((Form_pg_database) GETSTRUCT(tuple))->datdba;
4980 
4981  ReleaseSysCache(tuple);
4982 
4983  return has_privs_of_role(roleid, dba);
4984 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
FormData_pg_database * Form_pg_database
Definition: pg_database.h:57
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4828
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool pg_event_trigger_ownercheck ( Oid  et_oid,
Oid  roleid 
)

Definition at line 4937 of file aclchk.c.

References ereport, errcode(), errmsg(), ERROR, EVENTTRIGGEROID, GETSTRUCT, has_privs_of_role(), HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1, and superuser_arg().

Referenced by AlterEventTrigger(), AlterEventTriggerOwner_internal(), and check_object_ownership().

4938 {
4939  HeapTuple tuple;
4940  Oid ownerId;
4941 
4942  /* Superusers bypass all permission checking. */
4943  if (superuser_arg(roleid))
4944  return true;
4945 
4947  if (!HeapTupleIsValid(tuple))
4948  ereport(ERROR,
4949  (errcode(ERRCODE_UNDEFINED_OBJECT),
4950  errmsg("event trigger with OID %u does not exist",
4951  et_oid)));
4952 
4953  ownerId = ((Form_pg_event_trigger) GETSTRUCT(tuple))->evtowner;
4954 
4955  ReleaseSysCache(tuple);
4956 
4957  return has_privs_of_role(roleid, ownerId);
4958 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4828
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
FormData_pg_event_trigger * Form_pg_event_trigger
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool pg_extension_ownercheck ( Oid  ext_oid,
Oid  roleid 
)

Definition at line 5042 of file aclchk.c.

References AccessShareLock, BTEqualStrategyNumber, ereport, errcode(), errmsg(), ERROR, ExtensionOidIndexId, ExtensionRelationId, GETSTRUCT, has_privs_of_role(), heap_close, heap_open(), HeapTupleIsValid, ObjectIdAttributeNumber, ObjectIdGetDatum, ScanKeyInit(), superuser_arg(), systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by AlterExtensionNamespace(), check_object_ownership(), ExecAlterExtensionContentsStmt(), and ExecAlterExtensionStmt().

5043 {
5044  Relation pg_extension;
5045  ScanKeyData entry[1];
5046  SysScanDesc scan;
5047  HeapTuple tuple;
5048  Oid ownerId;
5049 
5050  /* Superusers bypass all permission checking. */
5051  if (superuser_arg(roleid))
5052  return true;
5053 
5054  /* There's no syscache for pg_extension, so do it the hard way */
5056 
5057  ScanKeyInit(&entry[0],
5059  BTEqualStrategyNumber, F_OIDEQ,
5060  ObjectIdGetDatum(ext_oid));
5061 
5062  scan = systable_beginscan(pg_extension,
5063  ExtensionOidIndexId, true,
5064  NULL, 1, entry);
5065 
5066  tuple = systable_getnext(scan);
5067  if (!HeapTupleIsValid(tuple))
5068  ereport(ERROR,
5069  (errcode(ERRCODE_UNDEFINED_OBJECT),
5070  errmsg("extension with OID %u does not exist", ext_oid)));
5071 
5072  ownerId = ((Form_pg_extension) GETSTRUCT(tuple))->extowner;
5073 
5074  systable_endscan(scan);
5075  heap_close(pg_extension, AccessShareLock);
5076 
5077  return has_privs_of_role(roleid, ownerId);
5078 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4828
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
#define ExtensionOidIndexId
Definition: indexing.h:319
FormData_pg_extension * Form_pg_extension
Definition: pg_extension.h:51
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
int errmsg(const char *fmt,...)
Definition: elog.c:797
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define ExtensionRelationId
Definition: pg_extension.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
AclResult pg_foreign_data_wrapper_aclcheck ( Oid  fdw_oid,
Oid  roleid,
AclMode  mode 
)
AclMode pg_foreign_data_wrapper_aclmask ( Oid  fdw_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 4101 of file aclchk.c.

References ACL_OBJECT_FDW, acldefault(), aclmask(), Anum_pg_foreign_data_wrapper_fdwacl, DatumGetAclP, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, FOREIGNDATAWRAPPEROID, GETSTRUCT, HeapTupleIsValid, ObjectIdGetDatum, pfree(), ReleaseSysCache(), SearchSysCache1, superuser_arg(), and SysCacheGetAttr().

Referenced by pg_aclmask(), and pg_foreign_data_wrapper_aclcheck().

4103 {
4104  AclMode result;
4105  HeapTuple tuple;
4106  Datum aclDatum;
4107  bool isNull;
4108  Acl *acl;
4109  Oid ownerId;
4110 
4112 
4113  /* Bypass permission checks for superusers */
4114  if (superuser_arg(roleid))
4115  return mask;
4116 
4117  /*
4118  * Must get the FDW's tuple from pg_foreign_data_wrapper
4119  */
4121  if (!HeapTupleIsValid(tuple))
4122  ereport(ERROR,
4123  (errcode(ERRCODE_UNDEFINED_OBJECT),
4124  errmsg("foreign-data wrapper with OID %u does not exist",
4125  fdw_oid)));
4126  fdwForm = (Form_pg_foreign_data_wrapper) GETSTRUCT(tuple);
4127 
4128  /*
4129  * Normal case: get the FDW's ACL from pg_foreign_data_wrapper
4130  */
4131  ownerId = fdwForm->fdwowner;
4132 
4133  aclDatum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID, tuple,
4135  if (isNull)
4136  {
4137  /* No ACL, so build default ACL */
4138  acl = acldefault(ACL_OBJECT_FDW, ownerId);
4139  aclDatum = (Datum) 0;
4140  }
4141  else
4142  {
4143  /* detoast rel's ACL if necessary */
4144  acl = DatumGetAclP(aclDatum);
4145  }
4146 
4147  result = aclmask(acl, roleid, ownerId, mask, how);
4148 
4149  /* if we have a detoasted copy, free it */
4150  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
4151  pfree(acl);
4152 
4153  ReleaseSysCache(tuple);
4154 
4155  return result;
4156 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define DatumGetAclP(X)
Definition: acl.h:121
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1319
Acl * acldefault(GrantObjectType objtype, Oid ownerId)
Definition: acl.c:746
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
#define Anum_pg_foreign_data_wrapper_fdwacl
uint32 AclMode
Definition: parsenodes.h:70
void pfree(void *pointer)
Definition: mcxt.c:949
char * Pointer
Definition: c.h:235
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1325
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
#define DatumGetPointer(X)
Definition: postgres.h:555
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool pg_foreign_data_wrapper_ownercheck ( Oid  srv_oid,
Oid  roleid 
)

Definition at line 4883 of file aclchk.c.

References ereport, errcode(), errmsg(), ERROR, FOREIGNDATAWRAPPEROID, GETSTRUCT, has_privs_of_role(), HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1, and superuser_arg().

Referenced by check_object_ownership().

4884 {
4885  HeapTuple tuple;
4886  Oid ownerId;
4887 
4888  /* Superusers bypass all permission checking. */
4889  if (superuser_arg(roleid))
4890  return true;
4891 
4893  if (!HeapTupleIsValid(tuple))
4894  ereport(ERROR,
4895  (errcode(ERRCODE_UNDEFINED_OBJECT),
4896  errmsg("foreign-data wrapper with OID %u does not exist",
4897  srv_oid)));
4898 
4899  ownerId = ((Form_pg_foreign_data_wrapper) GETSTRUCT(tuple))->fdwowner;
4900 
4901  ReleaseSysCache(tuple);
4902 
4903  return has_privs_of_role(roleid, ownerId);
4904 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4828
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
int errmsg(const char *fmt,...)
Definition: elog.c:797
AclResult pg_foreign_server_aclcheck ( Oid  srv_oid,
Oid  roleid,
AclMode  mode 
)
AclMode pg_foreign_server_aclmask ( Oid  srv_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 4163 of file aclchk.c.

References ACL_OBJECT_FOREIGN_SERVER, acldefault(), aclmask(), Anum_pg_foreign_server_srvacl, DatumGetAclP, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, FOREIGNSERVEROID, GETSTRUCT, HeapTupleIsValid, ObjectIdGetDatum, pfree(), ReleaseSysCache(), SearchSysCache1, superuser_arg(), and SysCacheGetAttr().

Referenced by pg_aclmask(), and pg_foreign_server_aclcheck().

4165 {
4166  AclMode result;
4167  HeapTuple tuple;
4168  Datum aclDatum;
4169  bool isNull;
4170  Acl *acl;
4171  Oid ownerId;
4172 
4173  Form_pg_foreign_server srvForm;
4174 
4175  /* Bypass permission checks for superusers */
4176  if (superuser_arg(roleid))
4177  return mask;
4178 
4179  /*
4180  * Must get the FDW's tuple from pg_foreign_data_wrapper
4181  */
4183  if (!HeapTupleIsValid(tuple))
4184  ereport(ERROR,
4185  (errcode(ERRCODE_UNDEFINED_OBJECT),
4186  errmsg("foreign server with OID %u does not exist",
4187  srv_oid)));
4188  srvForm = (Form_pg_foreign_server) GETSTRUCT(tuple);
4189 
4190  /*
4191  * Normal case: get the foreign server's ACL from pg_foreign_server
4192  */
4193  ownerId = srvForm->srvowner;
4194 
4195  aclDatum = SysCacheGetAttr(FOREIGNSERVEROID, tuple,
4197  if (isNull)
4198  {
4199  /* No ACL, so build default ACL */
4200  acl = acldefault(ACL_OBJECT_FOREIGN_SERVER, ownerId);
4201  aclDatum = (Datum) 0;
4202  }
4203  else
4204  {
4205  /* detoast rel's ACL if necessary */
4206  acl = DatumGetAclP(aclDatum);
4207  }
4208 
4209  result = aclmask(acl, roleid, ownerId, mask, how);
4210 
4211  /* if we have a detoasted copy, free it */
4212  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
4213  pfree(acl);
4214 
4215  ReleaseSysCache(tuple);
4216 
4217  return result;
4218 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define DatumGetAclP(X)
Definition: acl.h:121
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1319
Acl * acldefault(GrantObjectType objtype, Oid ownerId)
Definition: acl.c:746
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
uint32 AclMode
Definition: parsenodes.h:70
void pfree(void *pointer)
Definition: mcxt.c:949
char * Pointer
Definition: c.h:235
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
#define Anum_pg_foreign_server_srvacl
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1325
FormData_pg_foreign_server * Form_pg_foreign_server
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define DatumGetPointer(X)
Definition: postgres.h:555
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool pg_foreign_server_ownercheck ( Oid  srv_oid,
Oid  roleid 
)

Definition at line 4910 of file aclchk.c.

References ereport, errcode(), errmsg(), ERROR, FOREIGNSERVEROID, GETSTRUCT, has_privs_of_role(), HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1, and superuser_arg().

Referenced by AlterForeignServer(), AlterForeignServerOwner_internal(), check_object_ownership(), and user_mapping_ddl_aclcheck().

4911 {
4912  HeapTuple tuple;
4913  Oid ownerId;
4914 
4915  /* Superusers bypass all permission checking. */
4916  if (superuser_arg(roleid))
4917  return true;
4918 
4920  if (!HeapTupleIsValid(tuple))
4921  ereport(ERROR,
4922  (errcode(ERRCODE_UNDEFINED_OBJECT),
4923  errmsg("foreign server with OID %u does not exist",
4924  srv_oid)));
4925 
4926  ownerId = ((Form_pg_foreign_server) GETSTRUCT(tuple))->srvowner;
4927 
4928  ReleaseSysCache(tuple);
4929 
4930  return has_privs_of_role(roleid, ownerId);
4931 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4828
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
FormData_pg_foreign_server * Form_pg_foreign_server
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
int errmsg(const char *fmt,...)
Definition: elog.c:797
AclResult pg_language_aclcheck ( Oid  lang_oid,
Oid  roleid,
AclMode  mode 
)

Definition at line 4458 of file aclchk.c.

References ACLCHECK_NO_PRIV, ACLCHECK_OK, ACLMASK_ANY, and pg_language_aclmask().

Referenced by CheckFunctionValidatorAccess(), CreateFunction(), CreateTransform(), ExecuteDoStmt(), has_language_privilege_id(), has_language_privilege_id_id(), has_language_privilege_id_name(), has_language_privilege_name(), has_language_privilege_name_id(), and has_language_privilege_name_name().

4459 {
4460  if (pg_language_aclmask(lang_oid, roleid, mode, ACLMASK_ANY) != 0)
4461  return ACLCHECK_OK;
4462  else
4463  return ACLCHECK_NO_PRIV;
4464 }
AclMode pg_language_aclmask(Oid lang_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3826
AclMode pg_language_aclmask ( Oid  lang_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 3826 of file aclchk.c.

References ACL_OBJECT_LANGUAGE, acldefault(), aclmask(), Anum_pg_language_lanacl, DatumGetAclP, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, HeapTupleIsValid, LANGOID, ObjectIdGetDatum, pfree(), ReleaseSysCache(), SearchSysCache1, superuser_arg(), and SysCacheGetAttr().

Referenced by pg_aclmask(), and pg_language_aclcheck().

3828 {
3829  AclMode result;
3830  HeapTuple tuple;
3831  Datum aclDatum;
3832  bool isNull;
3833  Acl *acl;
3834  Oid ownerId;
3835 
3836  /* Superusers bypass all permission checking. */
3837  if (superuser_arg(roleid))
3838  return mask;
3839 
3840  /*
3841  * Get the language's ACL from pg_language
3842  */
3843  tuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(lang_oid));
3844  if (!HeapTupleIsValid(tuple))
3845  ereport(ERROR,
3846  (errcode(ERRCODE_UNDEFINED_OBJECT),
3847  errmsg("language with OID %u does not exist", lang_oid)));
3848 
3849  ownerId = ((Form_pg_language) GETSTRUCT(tuple))->lanowner;
3850 
3851  aclDatum = SysCacheGetAttr(LANGOID, tuple, Anum_pg_language_lanacl,
3852  &isNull);
3853  if (isNull)
3854  {
3855  /* No ACL, so build default ACL */
3856  acl = acldefault(ACL_OBJECT_LANGUAGE, ownerId);
3857  aclDatum = (Datum) 0;
3858  }
3859  else
3860  {
3861  /* detoast ACL if necessary */
3862  acl = DatumGetAclP(aclDatum);
3863  }
3864 
3865  result = aclmask(acl, roleid, ownerId, mask, how);
3866 
3867  /* if we have a detoasted copy, free it */
3868  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3869  pfree(acl);
3870 
3871  ReleaseSysCache(tuple);
3872 
3873  return result;
3874 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define DatumGetAclP(X)
Definition: acl.h:121
#define Anum_pg_language_lanacl
Definition: pg_language.h:65
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1319
Acl * acldefault(GrantObjectType objtype, Oid ownerId)
Definition: acl.c:746
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
uint32 AclMode
Definition: parsenodes.h:70
void pfree(void *pointer)
Definition: mcxt.c:949
char * Pointer
Definition: c.h:235
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1325
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define DatumGetPointer(X)
Definition: postgres.h:555
FormData_pg_language * Form_pg_language
Definition: pg_language.h:51
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool pg_language_ownercheck ( Oid  lan_oid,
Oid  roleid 
)

Definition at line 4650 of file aclchk.c.

References ereport, errcode(), errmsg(), ERROR, GETSTRUCT, has_privs_of_role(), HeapTupleIsValid, LANGOID, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1, and superuser_arg().

Referenced by check_object_ownership(), and create_proc_lang().

4651 {
4652  HeapTuple tuple;
4653  Oid ownerId;
4654 
4655  /* Superusers bypass all permission checking. */
4656  if (superuser_arg(roleid))
4657  return true;
4658 
4659  tuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(lan_oid));
4660  if (!HeapTupleIsValid(tuple))
4661  ereport(ERROR,
4662  (errcode(ERRCODE_UNDEFINED_FUNCTION),
4663  errmsg("language with OID %u does not exist", lan_oid)));
4664 
4665  ownerId = ((Form_pg_language) GETSTRUCT(tuple))->lanowner;
4666 
4667  ReleaseSysCache(tuple);
4668 
4669  return has_privs_of_role(roleid, ownerId);
4670 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4828
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
FormData_pg_language * Form_pg_language
Definition: pg_language.h:51
int errmsg(const char *fmt,...)
Definition: elog.c:797
AclResult pg_largeobject_aclcheck_snapshot ( Oid  lang_oid,
Oid  roleid,
AclMode  mode,
Snapshot  snapshot 
)

Definition at line 4470 of file aclchk.c.

References ACLCHECK_NO_PRIV, ACLCHECK_OK, ACLMASK_ANY, and pg_largeobject_aclmask_snapshot().

Referenced by be_lo_put(), lo_get_fragment_internal(), lo_read(), lo_truncate_internal(), and lo_write().

4472 {
4473  if (pg_largeobject_aclmask_snapshot(lobj_oid, roleid, mode,
4474  ACLMASK_ANY, snapshot) != 0)
4475  return ACLCHECK_OK;
4476  else
4477  return ACLCHECK_NO_PRIV;
4478 }
AclMode pg_largeobject_aclmask_snapshot(Oid lobj_oid, Oid roleid, AclMode mask, AclMaskHow how, Snapshot snapshot)
Definition: aclchk.c:3889
AclMode pg_largeobject_aclmask_snapshot ( Oid  lobj_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how,
Snapshot  snapshot 
)

Definition at line 3889 of file aclchk.c.

References AccessShareLock, ACL_OBJECT_LARGEOBJECT, acldefault(), aclmask(), Anum_pg_largeobject_metadata_lomacl, BTEqualStrategyNumber, DatumGetAclP, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, heap_close, heap_getattr, heap_open(), HeapTupleIsValid, LargeObjectMetadataOidIndexId, LargeObjectMetadataRelationId, ObjectIdAttributeNumber, ObjectIdGetDatum, pfree(), RelationGetDescr, ScanKeyInit(), superuser_arg(), systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by pg_aclmask(), and pg_largeobject_aclcheck_snapshot().

3892 {
3893  AclMode result;
3894  Relation pg_lo_meta;
3895  ScanKeyData entry[1];
3896  SysScanDesc scan;
3897  HeapTuple tuple;
3898  Datum aclDatum;
3899  bool isNull;
3900  Acl *acl;
3901  Oid ownerId;
3902 
3903  /* Superusers bypass all permission checking. */
3904  if (superuser_arg(roleid))
3905  return mask;
3906 
3907  /*
3908  * Get the largeobject's ACL from pg_language_metadata
3909  */
3911  AccessShareLock);
3912 
3913  ScanKeyInit(&entry[0],
3915  BTEqualStrategyNumber, F_OIDEQ,
3916  ObjectIdGetDatum(lobj_oid));
3917 
3918  scan = systable_beginscan(pg_lo_meta,
3920  snapshot, 1, entry);
3921 
3922  tuple = systable_getnext(scan);
3923  if (!HeapTupleIsValid(tuple))
3924  ereport(ERROR,
3925  (errcode(ERRCODE_UNDEFINED_OBJECT),
3926  errmsg("large object %u does not exist", lobj_oid)));
3927 
3928  ownerId = ((Form_pg_largeobject_metadata) GETSTRUCT(tuple))->lomowner;
3929 
3931  RelationGetDescr(pg_lo_meta), &isNull);
3932 
3933  if (isNull)
3934  {
3935  /* No ACL, so build default ACL */
3936  acl = acldefault(ACL_OBJECT_LARGEOBJECT, ownerId);
3937  aclDatum = (Datum) 0;
3938  }
3939  else
3940  {
3941  /* detoast ACL if necessary */
3942  acl = DatumGetAclP(aclDatum);
3943  }
3944 
3945  result = aclmask(acl, roleid, ownerId, mask, how);
3946 
3947  /* if we have a detoasted copy, free it */
3948  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3949  pfree(acl);
3950 
3951  systable_endscan(scan);
3952 
3953  heap_close(pg_lo_meta, AccessShareLock);
3954 
3955  return result;
3956 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define RelationGetDescr(relation)
Definition: rel.h:428
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#define DatumGetAclP(X)
Definition: acl.h:121
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1319
#define AccessShareLock
Definition: lockdefs.h:36
Acl * acldefault(GrantObjectType objtype, Oid ownerId)
Definition: acl.c:746
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
uint32 AclMode
Definition: parsenodes.h:70
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
void pfree(void *pointer)
Definition: mcxt.c:949
char * Pointer
Definition: c.h:235
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
#define LargeObjectMetadataOidIndexId
Definition: indexing.h:183
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:769
FormData_pg_largeobject_metadata * Form_pg_largeobject_metadata
uintptr_t Datum
Definition: postgres.h:372
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define LargeObjectMetadataRelationId
#define DatumGetPointer(X)
Definition: postgres.h:555
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define Anum_pg_largeobject_metadata_lomacl
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
bool pg_largeobject_ownercheck ( Oid  lobj_oid,
Oid  roleid 
)

Definition at line 4679 of file aclchk.c.

References AccessShareLock, BTEqualStrategyNumber, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, has_privs_of_role(), heap_close, heap_open(), HeapTupleIsValid, LargeObjectMetadataOidIndexId, LargeObjectMetadataRelationId, ObjectIdAttributeNumber, ObjectIdGetDatum, ScanKeyInit(), superuser_arg(), systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by be_lo_unlink(), and check_object_ownership().

4680 {
4681  Relation pg_lo_meta;
4682  ScanKeyData entry[1];
4683  SysScanDesc scan;
4684  HeapTuple tuple;
4685  Oid ownerId;
4686 
4687  /* Superusers bypass all permission checking. */
4688  if (superuser_arg(roleid))
4689  return true;
4690 
4691  /* There's no syscache for pg_largeobject_metadata */
4693  AccessShareLock);
4694 
4695  ScanKeyInit(&entry[0],
4697  BTEqualStrategyNumber, F_OIDEQ,
4698  ObjectIdGetDatum(lobj_oid));
4699 
4700  scan = systable_beginscan(pg_lo_meta,
4702  NULL, 1, entry);
4703 
4704  tuple = systable_getnext(scan);
4705  if (!HeapTupleIsValid(tuple))
4706  ereport(ERROR,
4707  (errcode(ERRCODE_UNDEFINED_OBJECT),
4708  errmsg("large object %u does not exist", lobj_oid)));
4709 
4710  ownerId = ((Form_pg_largeobject_metadata) GETSTRUCT(tuple))->lomowner;
4711 
4712  systable_endscan(scan);
4713  heap_close(pg_lo_meta, AccessShareLock);
4714 
4715  return has_privs_of_role(roleid, ownerId);
4716 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4828
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
#define LargeObjectMetadataOidIndexId
Definition: indexing.h:183
FormData_pg_largeobject_metadata * Form_pg_largeobject_metadata
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define LargeObjectMetadataRelationId
int errmsg(const char *fmt,...)
Definition: elog.c:797
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
AclMode pg_namespace_aclmask ( Oid  nsp_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 3962 of file aclchk.c.

References ACL_ALL_RIGHTS_NAMESPACE, ACL_CREATE_TEMP, ACL_OBJECT_NAMESPACE, ACL_USAGE, ACLCHECK_OK, acldefault(), aclmask(), Anum_pg_namespace_nspacl, DatumGetAclP, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, HeapTupleIsValid, isTempNamespace(), MyDatabaseId, NAMESPACEOID, ObjectIdGetDatum, pfree(), pg_database_aclcheck(), ReleaseSysCache(), SearchSysCache1, superuser_arg(), and SysCacheGetAttr().

Referenced by pg_aclmask(), and pg_namespace_aclcheck().

3964 {
3965  AclMode result;
3966  HeapTuple tuple;
3967  Datum aclDatum;
3968  bool isNull;
3969  Acl *acl;
3970  Oid ownerId;
3971 
3972  /* Superusers bypass all permission checking. */
3973  if (superuser_arg(roleid))
3974  return mask;
3975 
3976  /*
3977  * If we have been assigned this namespace as a temp namespace, check to
3978  * make sure we have CREATE TEMP permission on the database, and if so act
3979  * as though we have all standard (but not GRANT OPTION) permissions on
3980  * the namespace. If we don't have CREATE TEMP, act as though we have
3981  * only USAGE (and not CREATE) rights.
3982  *
3983  * This may seem redundant given the check in InitTempTableNamespace, but
3984  * it really isn't since current user ID may have changed since then. The
3985  * upshot of this behavior is that a SECURITY DEFINER function can create
3986  * temp tables that can then be accessed (if permission is granted) by
3987  * code in the same session that doesn't have permissions to create temp
3988  * tables.
3989  *
3990  * XXX Would it be safe to ereport a special error message as
3991  * InitTempTableNamespace does? Returning zero here means we'll get a
3992  * generic "permission denied for schema pg_temp_N" message, which is not
3993  * remarkably user-friendly.
3994  */
3995  if (isTempNamespace(nsp_oid))
3996  {
3997  if (pg_database_aclcheck(MyDatabaseId, roleid,
3999  return mask & ACL_ALL_RIGHTS_NAMESPACE;
4000  else
4001  return mask & ACL_USAGE;
4002  }
4003 
4004  /*
4005  * Get the schema's ACL from pg_namespace
4006  */
4007  tuple = SearchSysCache1(NAMESPACEOID, ObjectIdGetDatum(nsp_oid));
4008  if (!HeapTupleIsValid(tuple))
4009  ereport(ERROR,
4010  (errcode(ERRCODE_UNDEFINED_SCHEMA),
4011  errmsg("schema with OID %u does not exist", nsp_oid)));
4012 
4013  ownerId = ((Form_pg_namespace) GETSTRUCT(tuple))->nspowner;
4014 
4016  &isNull);
4017  if (isNull)
4018  {
4019  /* No ACL, so build default ACL */
4020  acl = acldefault(ACL_OBJECT_NAMESPACE, ownerId);
4021  aclDatum = (Datum) 0;
4022  }
4023  else
4024  {
4025  /* detoast ACL if necessary */
4026  acl = DatumGetAclP(aclDatum);
4027  }
4028 
4029  result = aclmask(acl, roleid, ownerId, mask, how);
4030 
4031  /* if we have a detoasted copy, free it */
4032  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
4033  pfree(acl);
4034 
4035  ReleaseSysCache(tuple);