PostgreSQL Source Code  git master
acl.h File Reference
#include "access/htup.h"
#include "nodes/parsenodes.h"
#include "parser/parse_node.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_SCHEMA   (ACL_USAGE|ACL_CREATE)
 
#define ACL_ALL_RIGHTS_TABLESPACE   (ACL_CREATE)
 
#define ACL_ALL_RIGHTS_TYPE   (ACL_USAGE)
 

Typedefs

typedef struct AclItem AclItem
 
typedef struct ArrayType Acl
 

Enumerations

enum  AclMaskHow { ACLMASK_ALL, ACLMASK_ANY }
 
enum  AclResult { ACLCHECK_OK = 0, ACLCHECK_NO_PRIV, ACLCHECK_NOT_OWNER }
 

Functions

Aclacldefault (ObjectType objtype, Oid ownerId)
 
Aclget_user_default_acl (ObjectType objtype, Oid ownerId, Oid nsp_oid)
 
void recordDependencyOnNewAcl (Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
 
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)
 
AclMode pg_attribute_aclmask (Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_attribute_aclmask_ext (Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how, bool *is_missing)
 
AclMode pg_class_aclmask (Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_class_aclmask_ext (Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how, bool *is_missing)
 
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_ext (Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode, bool *is_missing)
 
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_class_aclcheck_ext (Oid table_oid, Oid roleid, AclMode mode, bool *is_missing)
 
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, ObjectType objtype, const char *objectname)
 
void aclcheck_error_col (AclResult aclerr, ObjectType objtype, 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

◆ ACL_ALL_RIGHTS_COLUMN

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

Definition at line 156 of file acl.h.

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

◆ ACL_ALL_RIGHTS_DATABASE

#define ACL_ALL_RIGHTS_DATABASE   (ACL_CREATE|ACL_CREATE_TEMP|ACL_CONNECT)

Definition at line 159 of file acl.h.

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

◆ ACL_ALL_RIGHTS_FDW

#define ACL_ALL_RIGHTS_FDW   (ACL_USAGE)

Definition at line 160 of file acl.h.

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

◆ ACL_ALL_RIGHTS_FOREIGN_SERVER

#define ACL_ALL_RIGHTS_FOREIGN_SERVER   (ACL_USAGE)

◆ ACL_ALL_RIGHTS_FUNCTION

#define ACL_ALL_RIGHTS_FUNCTION   (ACL_EXECUTE)

◆ ACL_ALL_RIGHTS_LANGUAGE

#define ACL_ALL_RIGHTS_LANGUAGE   (ACL_USAGE)

Definition at line 163 of file acl.h.

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

◆ ACL_ALL_RIGHTS_LARGEOBJECT

#define ACL_ALL_RIGHTS_LARGEOBJECT   (ACL_SELECT|ACL_UPDATE)

◆ ACL_ALL_RIGHTS_RELATION

◆ ACL_ALL_RIGHTS_SCHEMA

◆ ACL_ALL_RIGHTS_SEQUENCE

◆ ACL_ALL_RIGHTS_STR

#define ACL_ALL_RIGHTS_STR   "arwdDxtXUCTc"

Definition at line 151 of file acl.h.

Referenced by aclitemout(), and aclparse().

◆ ACL_ALL_RIGHTS_TABLESPACE

#define ACL_ALL_RIGHTS_TABLESPACE   (ACL_CREATE)

◆ ACL_ALL_RIGHTS_TYPE

#define ACL_ALL_RIGHTS_TYPE   (ACL_USAGE)

◆ ACL_CONNECT_CHR

#define ACL_CONNECT_CHR   'c'

Definition at line 148 of file acl.h.

Referenced by aclparse().

◆ ACL_CREATE_CHR

#define ACL_CREATE_CHR   'C'

Definition at line 146 of file acl.h.

Referenced by aclparse().

◆ ACL_CREATE_TEMP_CHR

#define ACL_CREATE_TEMP_CHR   'T'

Definition at line 147 of file acl.h.

Referenced by aclparse().

◆ ACL_DAT

◆ ACL_DELETE_CHR

#define ACL_DELETE_CHR   'd'

◆ ACL_EXECUTE_CHR

#define ACL_EXECUTE_CHR   'X'

Definition at line 144 of file acl.h.

Referenced by aclparse().

◆ ACL_GRANT_OPTION_FOR

◆ ACL_ID_PUBLIC

◆ ACL_INSERT_CHR

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

◆ ACL_MODECHG_ADD

#define ACL_MODECHG_ADD   1

Definition at line 129 of file acl.h.

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

◆ ACL_MODECHG_DEL

#define ACL_MODECHG_DEL   2

Definition at line 130 of file acl.h.

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

◆ ACL_MODECHG_EQL

#define ACL_MODECHG_EQL   3

Definition at line 131 of file acl.h.

Referenced by aclupdate().

◆ ACL_N_SIZE

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

Definition at line 110 of file acl.h.

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

◆ ACL_NUM

◆ ACL_OPTION_TO_PRIVS

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

Definition at line 71 of file acl.h.

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

◆ ACL_REFERENCES_CHR

#define ACL_REFERENCES_CHR   'x'

Definition at line 142 of file acl.h.

Referenced by aclparse().

◆ ACL_SELECT_CHR

#define ACL_SELECT_CHR   'r' /* formerly known as "read" */

◆ ACL_SIZE

#define ACL_SIZE (   ACL)    ARR_SIZE(ACL)

Definition at line 111 of file acl.h.

Referenced by aclupdate(), and check_circularity().

◆ ACL_TRIGGER_CHR

#define ACL_TRIGGER_CHR   't'

Definition at line 143 of file acl.h.

Referenced by aclparse().

◆ ACL_TRUNCATE_CHR

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

Definition at line 141 of file acl.h.

Referenced by aclparse().

◆ ACL_UPDATE_CHR

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

Definition at line 139 of file acl.h.

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

◆ ACL_USAGE_CHR

#define ACL_USAGE_CHR   'U'

Definition at line 145 of file acl.h.

Referenced by aclparse().

◆ ACLITEM_ALL_GOPTION_BITS

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

Definition at line 88 of file acl.h.

Referenced by aclmask(), and aclmask_direct().

◆ ACLITEM_ALL_PRIV_BITS

#define ACLITEM_ALL_PRIV_BITS   ((AclMode) 0xFFFF)

Definition at line 87 of file acl.h.

◆ ACLITEM_GET_GOPTIONS

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

Definition at line 67 of file acl.h.

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

◆ ACLITEM_GET_PRIVS

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

Definition at line 66 of file acl.h.

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

◆ ACLITEM_GET_RIGHTS

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

Definition at line 68 of file acl.h.

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

◆ ACLITEM_SET_GOPTIONS

#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:80

Definition at line 76 of file acl.h.

◆ ACLITEM_SET_PRIVS

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

Definition at line 73 of file acl.h.

◆ ACLITEM_SET_PRIVS_GOPTIONS

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

Definition at line 82 of file acl.h.

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

◆ ACLITEM_SET_RIGHTS

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

Definition at line 79 of file acl.h.

Referenced by aclnewowner(), and aclupdate().

◆ DatumGetAclItemP

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

Definition at line 116 of file acl.h.

◆ DatumGetAclP

◆ DatumGetAclPCopy

◆ PG_GETARG_ACL_P

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

Definition at line 122 of file acl.h.

Referenced by aclcontains(), and aclexplode().

◆ PG_GETARG_ACL_P_COPY

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

Definition at line 123 of file acl.h.

◆ PG_GETARG_ACLITEM_P

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

Definition at line 117 of file acl.h.

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

◆ PG_RETURN_ACL_P

#define PG_RETURN_ACL_P (   x)    PG_RETURN_POINTER(x)

Definition at line 124 of file acl.h.

Referenced by acldefault_sql().

◆ PG_RETURN_ACLITEM_P

#define PG_RETURN_ACLITEM_P (   x)    PG_RETURN_POINTER(x)

Definition at line 118 of file acl.h.

Referenced by aclitemin(), and makeaclitem().

Typedef Documentation

◆ Acl

typedef struct ArrayType Acl

Definition at line 106 of file acl.h.

◆ AclItem

typedef struct AclItem AclItem

Enumeration Type Documentation

◆ AclMaskHow

enum AclMaskHow
Enumerator
ACLMASK_ALL 
ACLMASK_ANY 

Definition at line 170 of file acl.h.

171 {
172  ACLMASK_ALL, /* normal case: compute all bits */
173  ACLMASK_ANY /* return when result is known nonzero */
174 } AclMaskHow;
AclMaskHow
Definition: acl.h:170

◆ AclResult

enum AclResult
Enumerator
ACLCHECK_OK 
ACLCHECK_NO_PRIV 
ACLCHECK_NOT_OWNER 

Definition at line 177 of file acl.h.

178 {
179  ACLCHECK_OK = 0,
182 } AclResult;
AclResult
Definition: acl.h:177

Function Documentation

◆ aclcheck_error()

void aclcheck_error ( AclResult  aclerr,
ObjectType  objtype,
const char *  objectname 
)

Definition at line 3308 of file aclchk.c.

References ACLCHECK_NO_PRIV, ACLCHECK_NOT_OWNER, ACLCHECK_OK, elog, ereport, errcode(), errmsg(), ERROR, gettext_noop, OBJECT_ACCESS_METHOD, OBJECT_AGGREGATE, OBJECT_AMOP, OBJECT_AMPROC, OBJECT_ATTRIBUTE, OBJECT_CAST, OBJECT_COLLATION, OBJECT_COLUMN, OBJECT_CONVERSION, OBJECT_DATABASE, OBJECT_DEFACL, OBJECT_DEFAULT, OBJECT_DOMAIN, OBJECT_DOMCONSTRAINT, OBJECT_EVENT_TRIGGER, OBJECT_EXTENSION, OBJECT_FDW, OBJECT_FOREIGN_SERVER, OBJECT_FOREIGN_TABLE, OBJECT_FUNCTION, OBJECT_INDEX, OBJECT_LANGUAGE, OBJECT_LARGEOBJECT, OBJECT_MATVIEW, OBJECT_OPCLASS, OBJECT_OPERATOR, OBJECT_OPFAMILY, OBJECT_POLICY, OBJECT_PROCEDURE, OBJECT_PUBLICATION, OBJECT_PUBLICATION_REL, OBJECT_ROLE, OBJECT_ROUTINE, OBJECT_RULE, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_STATISTIC_EXT, OBJECT_SUBSCRIPTION, OBJECT_TABCONSTRAINT, OBJECT_TABLE, OBJECT_TABLESPACE, OBJECT_TRANSFORM, OBJECT_TRIGGER, OBJECT_TSCONFIGURATION, OBJECT_TSDICTIONARY, OBJECT_TSPARSER, OBJECT_TSTEMPLATE, OBJECT_TYPE, OBJECT_USER_MAPPING, and OBJECT_VIEW.

Referenced by aclcheck_error_col(), 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(), AlterStatistics(), AlterSubscription(), AlterSubscriptionOwner_internal(), AlterTableMoveAll(), AlterTableSpaceOptions(), AlterTSConfiguration(), AlterTSDictionary(), AlterTypeOwner(), ATExecChangeOwner(), 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(), CreateConversionCommand(), createdb(), CreateForeignServer(), CreateForeignTable(), CreateFunction(), CreateProceduralLanguage(), CreatePublication(), CreateSchemaCommand(), CreateStatistics(), CreateTransform(), CreateTriggerFiringOn(), currtid_internal(), DefineAggregate(), DefineCollation(), DefineDomain(), DefineEnum(), DefineIndex(), DefineOpClass(), DefineOperator(), DefineOpFamily(), DefineQueryRewrite(), DefineRange(), DefineRelation(), DefineTSConfiguration(), DefineTSDictionary(), DefineType(), dropdb(), DropSubscription(), DropTableSpace(), EnableDisableRule(), ExecAlterExtensionContentsStmt(), ExecAlterExtensionStmt(), ExecBuildGroupingEqual(), ExecBuildParamSetEqual(), ExecCheckRTPerms(), ExecInitAgg(), ExecInitExprRec(), ExecInitFunc(), ExecInitWindowAgg(), ExecReindex(), ExecuteCallStmt(), ExecuteDoStmt(), ExecuteTruncateGuts(), findRangeCanonicalFunction(), findRangeSubtypeDiffFunction(), get_connect_string(), get_other_operator(), get_rel_from_relname(), gin_clean_pending_list(), HandleFunctionRequest(), heap_force_common(), ImportForeignSchema(), init_sexpr(), initialize_peragg(), LockViewRecurse_walker(), lookup_agg_function(), LookupCreationNamespace(), LookupExplicitNamespace(), MergeAttributes(), movedb(), OperatorCreate(), pg_prewarm(), pgrowlocks(), ProcedureCreate(), PublicationAddTables(), RangeVarCallbackForAlterRelation(), RangeVarCallbackForDropRelation(), RangeVarCallbackForLockTable(), RangeVarCallbackForPolicy(), RangeVarCallbackForReindexIndex(), RangeVarCallbackForRenameRule(), RangeVarCallbackForRenameTrigger(), RangeVarCallbackOwnsRelation(), RangeVarCallbackOwnsTable(), RangeVarGetAndCheckCreationNamespace(), ReindexMultipleInternal(), ReindexMultipleTables(), renameatt_check(), RenameDatabase(), RenameSchema(), RenameTableSpace(), restrict_and_check_grant(), transformTableLikeClause(), truncate_check_perms(), TypeCreate(), user_mapping_ddl_aclcheck(), ValidateJoinEstimator(), and ValidateRestrictionEstimator().

3310 {
3311  switch (aclerr)
3312  {
3313  case ACLCHECK_OK:
3314  /* no error, so return to caller */
3315  break;
3316  case ACLCHECK_NO_PRIV:
3317  {
3318  const char *msg = "???";
3319 
3320  switch (objtype)
3321  {
3322  case OBJECT_AGGREGATE:
3323  msg = gettext_noop("permission denied for aggregate %s");
3324  break;
3325  case OBJECT_COLLATION:
3326  msg = gettext_noop("permission denied for collation %s");
3327  break;
3328  case OBJECT_COLUMN:
3329  msg = gettext_noop("permission denied for column %s");
3330  break;
3331  case OBJECT_CONVERSION:
3332  msg = gettext_noop("permission denied for conversion %s");
3333  break;
3334  case OBJECT_DATABASE:
3335  msg = gettext_noop("permission denied for database %s");
3336  break;
3337  case OBJECT_DOMAIN:
3338  msg = gettext_noop("permission denied for domain %s");
3339  break;
3340  case OBJECT_EVENT_TRIGGER:
3341  msg = gettext_noop("permission denied for event trigger %s");
3342  break;
3343  case OBJECT_EXTENSION:
3344  msg = gettext_noop("permission denied for extension %s");
3345  break;
3346  case OBJECT_FDW:
3347  msg = gettext_noop("permission denied for foreign-data wrapper %s");
3348  break;
3349  case OBJECT_FOREIGN_SERVER:
3350  msg = gettext_noop("permission denied for foreign server %s");
3351  break;
3352  case OBJECT_FOREIGN_TABLE:
3353  msg = gettext_noop("permission denied for foreign table %s");
3354  break;
3355  case OBJECT_FUNCTION:
3356  msg = gettext_noop("permission denied for function %s");
3357  break;
3358  case OBJECT_INDEX:
3359  msg = gettext_noop("permission denied for index %s");
3360  break;
3361  case OBJECT_LANGUAGE:
3362  msg = gettext_noop("permission denied for language %s");
3363  break;
3364  case OBJECT_LARGEOBJECT:
3365  msg = gettext_noop("permission denied for large object %s");
3366  break;
3367  case OBJECT_MATVIEW:
3368  msg = gettext_noop("permission denied for materialized view %s");
3369  break;
3370  case OBJECT_OPCLASS:
3371  msg = gettext_noop("permission denied for operator class %s");
3372  break;
3373  case OBJECT_OPERATOR:
3374  msg = gettext_noop("permission denied for operator %s");
3375  break;
3376  case OBJECT_OPFAMILY:
3377  msg = gettext_noop("permission denied for operator family %s");
3378  break;
3379  case OBJECT_POLICY:
3380  msg = gettext_noop("permission denied for policy %s");
3381  break;
3382  case OBJECT_PROCEDURE:
3383  msg = gettext_noop("permission denied for procedure %s");
3384  break;
3385  case OBJECT_PUBLICATION:
3386  msg = gettext_noop("permission denied for publication %s");
3387  break;
3388  case OBJECT_ROUTINE:
3389  msg = gettext_noop("permission denied for routine %s");
3390  break;
3391  case OBJECT_SCHEMA:
3392  msg = gettext_noop("permission denied for schema %s");
3393  break;
3394  case OBJECT_SEQUENCE:
3395  msg = gettext_noop("permission denied for sequence %s");
3396  break;
3397  case OBJECT_STATISTIC_EXT:
3398  msg = gettext_noop("permission denied for statistics object %s");
3399  break;
3400  case OBJECT_SUBSCRIPTION:
3401  msg = gettext_noop("permission denied for subscription %s");
3402  break;
3403  case OBJECT_TABLE:
3404  msg = gettext_noop("permission denied for table %s");
3405  break;
3406  case OBJECT_TABLESPACE:
3407  msg = gettext_noop("permission denied for tablespace %s");
3408  break;
3410  msg = gettext_noop("permission denied for text search configuration %s");
3411  break;
3412  case OBJECT_TSDICTIONARY:
3413  msg = gettext_noop("permission denied for text search dictionary %s");
3414  break;
3415  case OBJECT_TYPE:
3416  msg = gettext_noop("permission denied for type %s");
3417  break;
3418  case OBJECT_VIEW:
3419  msg = gettext_noop("permission denied for view %s");
3420  break;
3421  /* these currently aren't used */
3422  case OBJECT_ACCESS_METHOD:
3423  case OBJECT_AMOP:
3424  case OBJECT_AMPROC:
3425  case OBJECT_ATTRIBUTE:
3426  case OBJECT_CAST:
3427  case OBJECT_DEFAULT:
3428  case OBJECT_DEFACL:
3429  case OBJECT_DOMCONSTRAINT:
3431  case OBJECT_ROLE:
3432  case OBJECT_RULE:
3433  case OBJECT_TABCONSTRAINT:
3434  case OBJECT_TRANSFORM:
3435  case OBJECT_TRIGGER:
3436  case OBJECT_TSPARSER:
3437  case OBJECT_TSTEMPLATE:
3438  case OBJECT_USER_MAPPING:
3439  elog(ERROR, "unsupported object type %d", objtype);
3440  }
3441 
3442  ereport(ERROR,
3443  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3444  errmsg(msg, objectname)));
3445  break;
3446  }
3447  case ACLCHECK_NOT_OWNER:
3448  {
3449  const char *msg = "???";
3450 
3451  switch (objtype)
3452  {
3453  case OBJECT_AGGREGATE:
3454  msg = gettext_noop("must be owner of aggregate %s");
3455  break;
3456  case OBJECT_COLLATION:
3457  msg = gettext_noop("must be owner of collation %s");
3458  break;
3459  case OBJECT_CONVERSION:
3460  msg = gettext_noop("must be owner of conversion %s");
3461  break;
3462  case OBJECT_DATABASE:
3463  msg = gettext_noop("must be owner of database %s");
3464  break;
3465  case OBJECT_DOMAIN:
3466  msg = gettext_noop("must be owner of domain %s");
3467  break;
3468  case OBJECT_EVENT_TRIGGER:
3469  msg = gettext_noop("must be owner of event trigger %s");
3470  break;
3471  case OBJECT_EXTENSION:
3472  msg = gettext_noop("must be owner of extension %s");
3473  break;
3474  case OBJECT_FDW:
3475  msg = gettext_noop("must be owner of foreign-data wrapper %s");
3476  break;
3477  case OBJECT_FOREIGN_SERVER:
3478  msg = gettext_noop("must be owner of foreign server %s");
3479  break;
3480  case OBJECT_FOREIGN_TABLE:
3481  msg = gettext_noop("must be owner of foreign table %s");
3482  break;
3483  case OBJECT_FUNCTION:
3484  msg = gettext_noop("must be owner of function %s");
3485  break;
3486  case OBJECT_INDEX:
3487  msg = gettext_noop("must be owner of index %s");
3488  break;
3489  case OBJECT_LANGUAGE:
3490  msg = gettext_noop("must be owner of language %s");
3491  break;
3492  case OBJECT_LARGEOBJECT:
3493  msg = gettext_noop("must be owner of large object %s");
3494  break;
3495  case OBJECT_MATVIEW:
3496  msg = gettext_noop("must be owner of materialized view %s");
3497  break;
3498  case OBJECT_OPCLASS:
3499  msg = gettext_noop("must be owner of operator class %s");
3500  break;
3501  case OBJECT_OPERATOR:
3502  msg = gettext_noop("must be owner of operator %s");
3503  break;
3504  case OBJECT_OPFAMILY:
3505  msg = gettext_noop("must be owner of operator family %s");
3506  break;
3507  case OBJECT_PROCEDURE:
3508  msg = gettext_noop("must be owner of procedure %s");
3509  break;
3510  case OBJECT_PUBLICATION:
3511  msg = gettext_noop("must be owner of publication %s");
3512  break;
3513  case OBJECT_ROUTINE:
3514  msg = gettext_noop("must be owner of routine %s");
3515  break;
3516  case OBJECT_SEQUENCE:
3517  msg = gettext_noop("must be owner of sequence %s");
3518  break;
3519  case OBJECT_SUBSCRIPTION:
3520  msg = gettext_noop("must be owner of subscription %s");
3521  break;
3522  case OBJECT_TABLE:
3523  msg = gettext_noop("must be owner of table %s");
3524  break;
3525  case OBJECT_TYPE:
3526  msg = gettext_noop("must be owner of type %s");
3527  break;
3528  case OBJECT_VIEW:
3529  msg = gettext_noop("must be owner of view %s");
3530  break;
3531  case OBJECT_SCHEMA:
3532  msg = gettext_noop("must be owner of schema %s");
3533  break;
3534  case OBJECT_STATISTIC_EXT:
3535  msg = gettext_noop("must be owner of statistics object %s");
3536  break;
3537  case OBJECT_TABLESPACE:
3538  msg = gettext_noop("must be owner of tablespace %s");
3539  break;
3541  msg = gettext_noop("must be owner of text search configuration %s");
3542  break;
3543  case OBJECT_TSDICTIONARY:
3544  msg = gettext_noop("must be owner of text search dictionary %s");
3545  break;
3546 
3547  /*
3548  * Special cases: For these, the error message talks
3549  * about "relation", because that's where the
3550  * ownership is attached. See also
3551  * check_object_ownership().
3552  */
3553  case OBJECT_COLUMN:
3554  case OBJECT_POLICY:
3555  case OBJECT_RULE:
3556  case OBJECT_TABCONSTRAINT:
3557  case OBJECT_TRIGGER:
3558  msg = gettext_noop("must be owner of relation %s");
3559  break;
3560  /* these currently aren't used */
3561  case OBJECT_ACCESS_METHOD:
3562  case OBJECT_AMOP:
3563  case OBJECT_AMPROC:
3564  case OBJECT_ATTRIBUTE:
3565  case OBJECT_CAST:
3566  case OBJECT_DEFAULT:
3567  case OBJECT_DEFACL:
3568  case OBJECT_DOMCONSTRAINT:
3570  case OBJECT_ROLE:
3571  case OBJECT_TRANSFORM:
3572  case OBJECT_TSPARSER:
3573  case OBJECT_TSTEMPLATE:
3574  case OBJECT_USER_MAPPING:
3575  elog(ERROR, "unsupported object type %d", objtype);
3576  }
3577 
3578  ereport(ERROR,
3579  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3580  errmsg(msg, objectname)));
3581  break;
3582  }
3583  default:
3584  elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
3585  break;
3586  }
3587 }
#define gettext_noop(x)
Definition: c.h:1197
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ aclcheck_error_col()

void aclcheck_error_col ( AclResult  aclerr,
ObjectType  objtype,
const char *  objectname,
const char *  colname 
)

Definition at line 3591 of file aclchk.c.

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

Referenced by restrict_and_check_grant().

3593 {
3594  switch (aclerr)
3595  {
3596  case ACLCHECK_OK:
3597  /* no error, so return to caller */
3598  break;
3599  case ACLCHECK_NO_PRIV:
3600  ereport(ERROR,
3601  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3602  errmsg("permission denied for column \"%s\" of relation \"%s\"",
3603  colname, objectname)));
3604  break;
3605  case ACLCHECK_NOT_OWNER:
3606  /* relation msg is OK since columns don't have separate owners */
3607  aclcheck_error(aclerr, objtype, objectname);
3608  break;
3609  default:
3610  elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
3611  break;
3612  }
3613 }
int errcode(int sqlerrcode)
Definition: elog.c:698
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3308
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ aclcheck_error_type()

void aclcheck_error_type ( AclResult  aclerr,
Oid  typeOid 
)

Definition at line 3621 of file aclchk.c.

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

Referenced by AggregateCreate(), AlterType(), 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().

3622 {
3623  Oid element_type = get_element_type(typeOid);
3624 
3625  aclcheck_error(aclerr, OBJECT_TYPE, format_type_be(element_type ? element_type : typeOid));
3626 }
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2706
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3308

◆ aclconcat()

Acl* aclconcat ( const Acl left_acl,
const Acl right_acl 
)

Definition at line 413 of file acl.c.

References ACL_DAT, ACL_NUM, and allocacl().

Referenced by ExecGrant_Attribute().

414 {
415  Acl *result_acl;
416 
417  result_acl = allocacl(ACL_NUM(left_acl) + ACL_NUM(right_acl));
418 
419  memcpy(ACL_DAT(result_acl),
420  ACL_DAT(left_acl),
421  ACL_NUM(left_acl) * sizeof(AclItem));
422 
423  memcpy(ACL_DAT(result_acl) + ACL_NUM(left_acl),
424  ACL_DAT(right_acl),
425  ACL_NUM(right_acl) * sizeof(AclItem));
426 
427  return result_acl;
428 }
static Acl * allocacl(int n)
Definition: acl.c:362
#define ACL_NUM(ACL)
Definition: acl.h:108
#define ACL_DAT(ACL)
Definition: acl.h:109
Definition: acl.h:54

◆ aclcopy()

Acl* aclcopy ( const Acl orig_acl)

Definition at line 393 of file acl.c.

References ACL_DAT, ACL_NUM, and allocacl().

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

394 {
395  Acl *result_acl;
396 
397  result_acl = allocacl(ACL_NUM(orig_acl));
398 
399  memcpy(ACL_DAT(result_acl),
400  ACL_DAT(orig_acl),
401  ACL_NUM(orig_acl) * sizeof(AclItem));
402 
403  return result_acl;
404 }
static Acl * allocacl(int n)
Definition: acl.c:362
#define ACL_NUM(ACL)
Definition: acl.h:108
#define ACL_DAT(ACL)
Definition: acl.h:109
Definition: acl.h:54

◆ acldefault()

Acl* acldefault ( ObjectType  objtype,
Oid  ownerId 
)

Definition at line 734 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_RELATION, ACL_ALL_RIGHTS_SCHEMA, 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_USAGE, ACLITEM_SET_PRIVS_GOPTIONS, AclItem::ai_grantee, AclItem::ai_grantor, allocacl(), elog, ERROR, OBJECT_COLUMN, OBJECT_DATABASE, OBJECT_DOMAIN, OBJECT_FDW, OBJECT_FOREIGN_SERVER, OBJECT_FUNCTION, OBJECT_LANGUAGE, OBJECT_LARGEOBJECT, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_TABLE, OBJECT_TABLESPACE, and OBJECT_TYPE.

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_ext(), 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().

735 {
736  AclMode world_default;
737  AclMode owner_default;
738  int nacl;
739  Acl *acl;
740  AclItem *aip;
741 
742  switch (objtype)
743  {
744  case OBJECT_COLUMN:
745  /* by default, columns have no extra privileges */
746  world_default = ACL_NO_RIGHTS;
747  owner_default = ACL_NO_RIGHTS;
748  break;
749  case OBJECT_TABLE:
750  world_default = ACL_NO_RIGHTS;
751  owner_default = ACL_ALL_RIGHTS_RELATION;
752  break;
753  case OBJECT_SEQUENCE:
754  world_default = ACL_NO_RIGHTS;
755  owner_default = ACL_ALL_RIGHTS_SEQUENCE;
756  break;
757  case OBJECT_DATABASE:
758  /* for backwards compatibility, grant some rights by default */
759  world_default = ACL_CREATE_TEMP | ACL_CONNECT;
760  owner_default = ACL_ALL_RIGHTS_DATABASE;
761  break;
762  case OBJECT_FUNCTION:
763  /* Grant EXECUTE by default, for now */
764  world_default = ACL_EXECUTE;
765  owner_default = ACL_ALL_RIGHTS_FUNCTION;
766  break;
767  case OBJECT_LANGUAGE:
768  /* Grant USAGE by default, for now */
769  world_default = ACL_USAGE;
770  owner_default = ACL_ALL_RIGHTS_LANGUAGE;
771  break;
772  case OBJECT_LARGEOBJECT:
773  world_default = ACL_NO_RIGHTS;
774  owner_default = ACL_ALL_RIGHTS_LARGEOBJECT;
775  break;
776  case OBJECT_SCHEMA:
777  world_default = ACL_NO_RIGHTS;
778  owner_default = ACL_ALL_RIGHTS_SCHEMA;
779  break;
780  case OBJECT_TABLESPACE:
781  world_default = ACL_NO_RIGHTS;
782  owner_default = ACL_ALL_RIGHTS_TABLESPACE;
783  break;
784  case OBJECT_FDW:
785  world_default = ACL_NO_RIGHTS;
786  owner_default = ACL_ALL_RIGHTS_FDW;
787  break;
789  world_default = ACL_NO_RIGHTS;
790  owner_default = ACL_ALL_RIGHTS_FOREIGN_SERVER;
791  break;
792  case OBJECT_DOMAIN:
793  case OBJECT_TYPE:
794  world_default = ACL_USAGE;
795  owner_default = ACL_ALL_RIGHTS_TYPE;
796  break;
797  default:
798  elog(ERROR, "unrecognized objtype: %d", (int) objtype);
799  world_default = ACL_NO_RIGHTS; /* keep compiler quiet */
800  owner_default = ACL_NO_RIGHTS;
801  break;
802  }
803 
804  nacl = 0;
805  if (world_default != ACL_NO_RIGHTS)
806  nacl++;
807  if (owner_default != ACL_NO_RIGHTS)
808  nacl++;
809 
810  acl = allocacl(nacl);
811  aip = ACL_DAT(acl);
812 
813  if (world_default != ACL_NO_RIGHTS)
814  {
815  aip->ai_grantee = ACL_ID_PUBLIC;
816  aip->ai_grantor = ownerId;
817  ACLITEM_SET_PRIVS_GOPTIONS(*aip, world_default, ACL_NO_RIGHTS);
818  aip++;
819  }
820 
821  /*
822  * Note that the owner's entry shows all ordinary privileges but no grant
823  * options. This is because his grant options come "from the system" and
824  * not from his own efforts. (The SQL spec says that the owner's rights
825  * come from a "_SYSTEM" authid.) However, we do consider that the
826  * owner's ordinary privileges are self-granted; this lets him revoke
827  * them. We implement the owner's grant options without any explicit
828  * "_SYSTEM"-like ACL entry, by internally special-casing the owner
829  * wherever we are testing grant options.
830  */
831  if (owner_default != ACL_NO_RIGHTS)
832  {
833  aip->ai_grantee = ownerId;
834  aip->ai_grantor = ownerId;
835  ACLITEM_SET_PRIVS_GOPTIONS(*aip, owner_default, ACL_NO_RIGHTS);
836  }
837 
838  return acl;
839 }
Oid ai_grantee
Definition: acl.h:56
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:162
static Acl * allocacl(int n)
Definition: acl.c:362
#define ACL_ALL_RIGHTS_TABLESPACE
Definition: acl.h:166
Oid ai_grantor
Definition: acl.h:57
#define ACL_ALL_RIGHTS_LANGUAGE
Definition: acl.h:163
uint32 AclMode
Definition: parsenodes.h:80
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
#define ACL_ALL_RIGHTS_SCHEMA
Definition: acl.h:165
#define ACL_ALL_RIGHTS_TYPE
Definition: acl.h:167
#define ACL_USAGE
Definition: parsenodes.h:90
#define ACL_CONNECT
Definition: parsenodes.h:94
#define ACL_DAT(ACL)
Definition: acl.h:109
#define ACL_ALL_RIGHTS_SEQUENCE
Definition: acl.h:158
Definition: acl.h:54
#define ACL_ALL_RIGHTS_LARGEOBJECT
Definition: acl.h:164
#define ACL_ALL_RIGHTS_DATABASE
Definition: acl.h:159
#define ACL_ALL_RIGHTS_FOREIGN_SERVER
Definition: acl.h:161
#define ACL_EXECUTE
Definition: parsenodes.h:89
#define elog(elevel,...)
Definition: elog.h:232
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:157
#define ACL_CREATE_TEMP
Definition: parsenodes.h:93
#define ACL_ID_PUBLIC
Definition: acl.h:46
#define ACL_ALL_RIGHTS_FDW
Definition: acl.h:160
#define ACLITEM_SET_PRIVS_GOPTIONS(item, privs, goptions)
Definition: acl.h:82

◆ aclequal()

bool aclequal ( const Acl left_acl,
const Acl right_acl 
)

Definition at line 495 of file acl.c.

References ACL_DAT, and ACL_NUM.

Referenced by get_user_default_acl(), and SetDefaultACL().

496 {
497  /* Check for cases where one or both are empty/null */
498  if (left_acl == NULL || ACL_NUM(left_acl) == 0)
499  {
500  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
501  return true;
502  else
503  return false;
504  }
505  else
506  {
507  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
508  return false;
509  }
510 
511  if (ACL_NUM(left_acl) != ACL_NUM(right_acl))
512  return false;
513 
514  if (memcmp(ACL_DAT(left_acl),
515  ACL_DAT(right_acl),
516  ACL_NUM(left_acl) * sizeof(AclItem)) == 0)
517  return true;
518 
519  return false;
520 }
#define ACL_NUM(ACL)
Definition: acl.h:108
#define ACL_DAT(ACL)
Definition: acl.h:109
Definition: acl.h:54

◆ aclitemsort()

void aclitemsort ( Acl acl)

Definition at line 481 of file acl.c.

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

Referenced by get_user_default_acl(), and SetDefaultACL().

482 {
483  if (acl != NULL && ACL_NUM(acl) > 1)
484  qsort(ACL_DAT(acl), ACL_NUM(acl), sizeof(AclItem), aclitemComparator);
485 }
#define ACL_NUM(ACL)
Definition: acl.h:108
#define ACL_DAT(ACL)
Definition: acl.h:109
Definition: acl.h:54
#define qsort(a, b, c, d)
Definition: port.h:504
static int aclitemComparator(const void *arg1, const void *arg2)
Definition: acl.c:655

◆ aclmask()

AclMode aclmask ( const Acl acl,
Oid  roleid,
Oid  ownerId,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 1306 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_ext(), pg_class_aclmask_ext(), 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().

1308 {
1309  AclMode result;
1311  AclItem *aidat;
1312  int i,
1313  num;
1314 
1315  /*
1316  * Null ACL should not happen, since caller should have inserted
1317  * appropriate default
1318  */
1319  if (acl == NULL)
1320  elog(ERROR, "null ACL");
1321 
1322  check_acl(acl);
1323 
1324  /* Quick exit for mask == 0 */
1325  if (mask == 0)
1326  return 0;
1327 
1328  result = 0;
1329 
1330  /* Owner always implicitly has all grant options */
1331  if ((mask & ACLITEM_ALL_GOPTION_BITS) &&
1332  has_privs_of_role(roleid, ownerId))
1333  {
1334  result = mask & ACLITEM_ALL_GOPTION_BITS;
1335  if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1336  return result;
1337  }
1338 
1339  num = ACL_NUM(acl);
1340  aidat = ACL_DAT(acl);
1341 
1342  /*
1343  * Check privileges granted directly to roleid or to public
1344  */
1345  for (i = 0; i < num; i++)
1346  {
1347  AclItem *aidata = &aidat[i];
1348 
1349  if (aidata->ai_grantee == ACL_ID_PUBLIC ||
1350  aidata->ai_grantee == roleid)
1351  {
1352  result |= aidata->ai_privs & mask;
1353  if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1354  return result;
1355  }
1356  }
1357 
1358  /*
1359  * Check privileges granted indirectly via role memberships. We do this in
1360  * a separate pass to minimize expensive indirect membership tests. In
1361  * particular, it's worth testing whether a given ACL entry grants any
1362  * privileges still of interest before we perform the has_privs_of_role
1363  * test.
1364  */
1365  remaining = mask & ~result;
1366  for (i = 0; i < num; i++)
1367  {
1368  AclItem *aidata = &aidat[i];
1369 
1370  if (aidata->ai_grantee == ACL_ID_PUBLIC ||
1371  aidata->ai_grantee == roleid)
1372  continue; /* already checked it */
1373 
1374  if ((aidata->ai_privs & remaining) &&
1375  has_privs_of_role(roleid, aidata->ai_grantee))
1376  {
1377  result |= aidata->ai_privs & mask;
1378  if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1379  return result;
1380  remaining = mask & ~result;
1381  }
1382  }
1383 
1384  return result;
1385 }
int remaining
Definition: informix.c:667
Oid ai_grantee
Definition: acl.h:56
#define ACLITEM_ALL_GOPTION_BITS
Definition: acl.h:88
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
AclMode ai_privs
Definition: acl.h:58
uint32 AclMode
Definition: parsenodes.h:80
#define ERROR
Definition: elog.h:46
#define ACL_NUM(ACL)
Definition: acl.h:108
#define ACL_DAT(ACL)
Definition: acl.h:109
Definition: acl.h:54
#define elog(elevel,...)
Definition: elog.h:232
int i
#define ACL_ID_PUBLIC
Definition: acl.h:46
static void check_acl(const Acl *acl)
Definition: acl.c:526

◆ aclmembers()

int aclmembers ( const Acl acl,
Oid **  roleids 
)

Definition at line 1458 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(), qsort, and qunique().

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

1459 {
1460  Oid *list;
1461  const AclItem *acldat;
1462  int i,
1463  j;
1464 
1465  if (acl == NULL || ACL_NUM(acl) == 0)
1466  {
1467  *roleids = NULL;
1468  return 0;
1469  }
1470 
1471  check_acl(acl);
1472 
1473  /* Allocate the worst-case space requirement */
1474  list = palloc(ACL_NUM(acl) * 2 * sizeof(Oid));
1475  acldat = ACL_DAT(acl);
1476 
1477  /*
1478  * Walk the ACL collecting mentioned RoleIds.
1479  */
1480  j = 0;
1481  for (i = 0; i < ACL_NUM(acl); i++)
1482  {
1483  const AclItem *ai = &acldat[i];
1484 
1485  if (ai->ai_grantee != ACL_ID_PUBLIC)
1486  list[j++] = ai->ai_grantee;
1487  /* grantor is currently never PUBLIC, but let's check anyway */
1488  if (ai->ai_grantor != ACL_ID_PUBLIC)
1489  list[j++] = ai->ai_grantor;
1490  }
1491 
1492  /* Sort the array */
1493  qsort(list, j, sizeof(Oid), oid_cmp);
1494 
1495  /*
1496  * We could repalloc the array down to minimum size, but it's hardly worth
1497  * it since it's only transient memory.
1498  */
1499  *roleids = list;
1500 
1501  /* Remove duplicates from the array */
1502  return qunique(list, j, sizeof(Oid), oid_cmp);
1503 }
Oid ai_grantee
Definition: acl.h:56
unsigned int Oid
Definition: postgres_ext.h:31
Oid ai_grantor
Definition: acl.h:57
#define ACL_NUM(ACL)
Definition: acl.h:108
#define ACL_DAT(ACL)
Definition: acl.h:109
Definition: acl.h:54
int oid_cmp(const void *p1, const void *p2)
Definition: oid.c:336
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
void * palloc(Size size)
Definition: mcxt.c:1062
int i
#define ACL_ID_PUBLIC
Definition: acl.h:46
#define qsort(a, b, c, d)
Definition: port.h:504
static void check_acl(const Acl *acl)
Definition: acl.c:526

◆ aclmerge()

Acl* aclmerge ( const Acl left_acl,
const Acl right_acl,
Oid  ownerId 
)

Definition at line 437 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().

438 {
439  Acl *result_acl;
440  AclItem *aip;
441  int i,
442  num;
443 
444  /* Check for cases where one or both are empty/null */
445  if (left_acl == NULL || ACL_NUM(left_acl) == 0)
446  {
447  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
448  return NULL;
449  else
450  return aclcopy(right_acl);
451  }
452  else
453  {
454  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
455  return aclcopy(left_acl);
456  }
457 
458  /* Merge them the hard way, one item at a time */
459  result_acl = aclcopy(left_acl);
460 
461  aip = ACL_DAT(right_acl);
462  num = ACL_NUM(right_acl);
463 
464  for (i = 0; i < num; i++, aip++)
465  {
466  Acl *tmp_acl;
467 
468  tmp_acl = aclupdate(result_acl, aip, ACL_MODECHG_ADD,
469  ownerId, DROP_RESTRICT);
470  pfree(result_acl);
471  result_acl = tmp_acl;
472  }
473 
474  return result_acl;
475 }
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ACL_NUM(ACL)
Definition: acl.h:108
#define ACL_MODECHG_ADD
Definition: acl.h:129
#define ACL_DAT(ACL)
Definition: acl.h:109
Definition: acl.h:54
Acl * aclcopy(const Acl *orig_acl)
Definition: acl.c:393
int i
Acl * aclupdate(const Acl *old_acl, const AclItem *mod_aip, int modechg, Oid ownerId, DropBehavior behavior)
Definition: acl.c:916

◆ aclnewowner()

Acl* aclnewowner ( const Acl old_acl,
Oid  oldOwnerId,
Oid  newOwnerId 
)

Definition at line 1037 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().

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

◆ aclupdate()

Acl* aclupdate ( const Acl old_acl,
const AclItem mod_aip,
int  modechg,
Oid  ownerId,
DropBehavior  behavior 
)

Definition at line 916 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(), recursive_revoke(), and SET_VARSIZE.

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

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

◆ check_is_member_of_role()

void check_is_member_of_role ( Oid  member,
Oid  role 
)

Definition at line 4893 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().

4894 {
4895  if (!is_member_of_role(member, role))
4896  ereport(ERROR,
4897  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4898  errmsg("must be member of role \"%s\"",
4899  GetUserNameFromId(role, false))));
4900 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4869
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:893
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ check_rolespec_name()

void check_rolespec_name ( const RoleSpec role,
const char *  detail_msg 
)

Definition at line 5235 of file acl.c.

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

Referenced by AlterRole(), and AlterRoleSet().

5236 {
5237  if (!role)
5238  return;
5239 
5240  if (role->roletype != ROLESPEC_CSTRING)
5241  return;
5242 
5243  if (IsReservedName(role->rolename))
5244  {
5245  if (detail_msg)
5246  ereport(ERROR,
5247  (errcode(ERRCODE_RESERVED_NAME),
5248  errmsg("role name \"%s\" is reserved",
5249  role->rolename),
5250  errdetail("%s", detail_msg)));
5251  else
5252  ereport(ERROR,
5253  (errcode(ERRCODE_RESERVED_NAME),
5254  errmsg("role name \"%s\" is reserved",
5255  role->rolename)));
5256  }
5257 }
int errcode(int sqlerrcode)
Definition: elog.c:698
bool IsReservedName(const char *name)
Definition: catalog.c:218
#define ERROR
Definition: elog.h:46
int errdetail(const char *fmt,...)
Definition: elog.c:1042
RoleSpecType roletype
Definition: parsenodes.h:339
#define ereport(elevel,...)
Definition: elog.h:157
char * rolename
Definition: parsenodes.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ ExecAlterDefaultPrivilegesStmt()

void ExecAlterDefaultPrivilegesStmt ( ParseState pstate,
AlterDefaultPrivilegesStmt stmt 
)

Definition at line 905 of file aclchk.c.

References ACL_ALL_RIGHTS_FUNCTION, ACL_ALL_RIGHTS_RELATION, ACL_ALL_RIGHTS_SCHEMA, ACL_ALL_RIGHTS_SEQUENCE, ACL_ALL_RIGHTS_TYPE, ACL_ID_PUBLIC, ACL_NO_RIGHTS, generate_unaccent_rules::action, 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, errorConflictingDefElem(), 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, NIL, OBJECT_FUNCTION, OBJECT_PROCEDURE, OBJECT_ROUTINE, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_TABLE, OBJECT_TYPE, InternalDefaultACL::objtype, GrantStmt::objtype, AlterDefaultPrivilegesStmt::options, AccessPriv::priv_name, privilege_to_string(), InternalDefaultACL::privileges, GrantStmt::privileges, InternalDefaultACL::roleid, ROLESPEC_PUBLIC, RoleSpec::roletype, SetDefaultACLsInSchemas(), and string_to_privilege().

Referenced by ProcessUtilitySlow().

906 {
907  GrantStmt *action = stmt->action;
908  InternalDefaultACL iacls;
909  ListCell *cell;
910  List *rolespecs = NIL;
911  List *nspnames = NIL;
912  DefElem *drolespecs = NULL;
913  DefElem *dnspnames = NULL;
914  AclMode all_privileges;
915  const char *errormsg;
916 
917  /* Deconstruct the "options" part of the statement */
918  foreach(cell, stmt->options)
919  {
920  DefElem *defel = (DefElem *) lfirst(cell);
921 
922  if (strcmp(defel->defname, "schemas") == 0)
923  {
924  if (dnspnames)
925  errorConflictingDefElem(defel, pstate);
926  dnspnames = defel;
927  }
928  else if (strcmp(defel->defname, "roles") == 0)
929  {
930  if (drolespecs)
931  errorConflictingDefElem(defel, pstate);
932  drolespecs = defel;
933  }
934  else
935  elog(ERROR, "option \"%s\" not recognized", defel->defname);
936  }
937 
938  if (dnspnames)
939  nspnames = (List *) dnspnames->arg;
940  if (drolespecs)
941  rolespecs = (List *) drolespecs->arg;
942 
943  /* Prepare the InternalDefaultACL representation of the statement */
944  /* roleid to be filled below */
945  /* nspid to be filled in SetDefaultACLsInSchemas */
946  iacls.is_grant = action->is_grant;
947  iacls.objtype = action->objtype;
948  /* all_privs to be filled below */
949  /* privileges to be filled below */
950  iacls.grantees = NIL; /* filled below */
951  iacls.grant_option = action->grant_option;
952  iacls.behavior = action->behavior;
953 
954  /*
955  * Convert the RoleSpec list into an Oid list. Note that at this point we
956  * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
957  * there shouldn't be any additional work needed to support this case.
958  */
959  foreach(cell, action->grantees)
960  {
961  RoleSpec *grantee = (RoleSpec *) lfirst(cell);
962  Oid grantee_uid;
963 
964  switch (grantee->roletype)
965  {
966  case ROLESPEC_PUBLIC:
967  grantee_uid = ACL_ID_PUBLIC;
968  break;
969  default:
970  grantee_uid = get_rolespec_oid(grantee, false);
971  break;
972  }
973  iacls.grantees = lappend_oid(iacls.grantees, grantee_uid);
974  }
975 
976  /*
977  * Convert action->privileges, a list of privilege strings, into an
978  * AclMode bitmask.
979  */
980  switch (action->objtype)
981  {
982  case OBJECT_TABLE:
983  all_privileges = ACL_ALL_RIGHTS_RELATION;
984  errormsg = gettext_noop("invalid privilege type %s for relation");
985  break;
986  case OBJECT_SEQUENCE:
987  all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
988  errormsg = gettext_noop("invalid privilege type %s for sequence");
989  break;
990  case OBJECT_FUNCTION:
991  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
992  errormsg = gettext_noop("invalid privilege type %s for function");
993  break;
994  case OBJECT_PROCEDURE:
995  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
996  errormsg = gettext_noop("invalid privilege type %s for procedure");
997  break;
998  case OBJECT_ROUTINE:
999  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
1000  errormsg = gettext_noop("invalid privilege type %s for routine");
1001  break;
1002  case OBJECT_TYPE:
1003  all_privileges = ACL_ALL_RIGHTS_TYPE;
1004  errormsg = gettext_noop("invalid privilege type %s for type");
1005  break;
1006  case OBJECT_SCHEMA:
1007  all_privileges = ACL_ALL_RIGHTS_SCHEMA;
1008  errormsg = gettext_noop("invalid privilege type %s for schema");
1009  break;
1010  default:
1011  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
1012  (int) action->objtype);
1013  /* keep compiler quiet */
1014  all_privileges = ACL_NO_RIGHTS;
1015  errormsg = NULL;
1016  }
1017 
1018  if (action->privileges == NIL)
1019  {
1020  iacls.all_privs = true;
1021 
1022  /*
1023  * will be turned into ACL_ALL_RIGHTS_* by the internal routines
1024  * depending on the object type
1025  */
1026  iacls.privileges = ACL_NO_RIGHTS;
1027  }
1028  else
1029  {
1030  iacls.all_privs = false;
1031  iacls.privileges = ACL_NO_RIGHTS;
1032 
1033  foreach(cell, action->privileges)
1034  {
1035  AccessPriv *privnode = (AccessPriv *) lfirst(cell);
1036  AclMode priv;
1037 
1038  if (privnode->cols)
1039  ereport(ERROR,
1040  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1041  errmsg("default privileges cannot be set for columns")));
1042 
1043  if (privnode->priv_name == NULL) /* parser mistake? */
1044  elog(ERROR, "AccessPriv node must specify privilege");
1045  priv = string_to_privilege(privnode->priv_name);
1046 
1047  if (priv & ~((AclMode) all_privileges))
1048  ereport(ERROR,
1049  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1050  errmsg(errormsg, privilege_to_string(priv))));
1051 
1052  iacls.privileges |= priv;
1053  }
1054  }
1055 
1056  if (rolespecs == NIL)
1057  {
1058  /* Set permissions for myself */
1059  iacls.roleid = GetUserId();
1060 
1061  SetDefaultACLsInSchemas(&iacls, nspnames);
1062  }
1063  else
1064  {
1065  /* Look up the role OIDs and do permissions checks */
1066  ListCell *rolecell;
1067 
1068  foreach(rolecell, rolespecs)
1069  {
1070  RoleSpec *rolespec = lfirst(rolecell);
1071 
1072  iacls.roleid = get_rolespec_oid(rolespec, false);
1073 
1074  /*
1075  * We insist that calling user be a member of each target role. If
1076  * he has that, he could become that role anyway via SET ROLE, so
1077  * FOR ROLE is just a syntactic convenience and doesn't give any
1078  * special privileges.
1079  */
1081 
1082  SetDefaultACLsInSchemas(&iacls, nspnames);
1083  }
1084  }
1085 }
#define NIL
Definition: pg_list.h:65
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:162
bool grant_option
Definition: aclchk.c:93
Oid GetUserId(void)
Definition: miscinit.c:478
static AclMode string_to_privilege(const char *privname)
Definition: aclchk.c:3230
bool grant_option
Definition: parsenodes.h:2021
#define gettext_noop(x)
Definition: c.h:1197
int errcode(int sqlerrcode)
Definition: elog.c:698
List * cols
Definition: parsenodes.h:2063
unsigned int Oid
Definition: postgres_ext.h:31
List * grantees
Definition: aclchk.c:92
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
uint32 AclMode
Definition: parsenodes.h:80
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
#define ACL_ALL_RIGHTS_SCHEMA
Definition: acl.h:165
#define ACL_ALL_RIGHTS_TYPE
Definition: acl.h:167
void check_is_member_of_role(Oid member, Oid role)
Definition: acl.c:4893
Node * arg
Definition: parsenodes.h:747
bool is_grant
Definition: parsenodes.h:2013
RoleSpecType roletype
Definition: parsenodes.h:339
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition: acl.c:5128
#define ACL_ALL_RIGHTS_SEQUENCE
Definition: acl.h:158
DropBehavior behavior
Definition: aclchk.c:94
List * privileges
Definition: parsenodes.h:2018
#define ereport(elevel,...)
Definition: elog.h:157
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition: define.c:355
DropBehavior behavior
Definition: parsenodes.h:2023
#define lfirst(lc)
Definition: pg_list.h:169
static void SetDefaultACLsInSchemas(InternalDefaultACL *iacls, List *nspnames)
Definition: aclchk.c:1093
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:157
ObjectType objtype
Definition: parsenodes.h:2015
#define ACL_ID_PUBLIC
Definition: acl.h:46
char * defname
Definition: parsenodes.h:746
static const char * privilege_to_string(AclMode privilege)
Definition: aclchk.c:3267
List * grantees
Definition: parsenodes.h:2020
AclMode privileges
Definition: aclchk.c:91
Definition: pg_list.h:50
ObjectType objtype
Definition: aclchk.c:89
char * priv_name
Definition: parsenodes.h:2062

◆ ExecuteGrantStmt()

void ExecuteGrantStmt ( GrantStmt stmt)

Definition at line 360 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_RELATION, ACL_ALL_RIGHTS_SCHEMA, ACL_ALL_RIGHTS_SEQUENCE, ACL_ALL_RIGHTS_TABLESPACE, ACL_ALL_RIGHTS_TYPE, ACL_ID_PUBLIC, ACL_NO_RIGHTS, 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, GetUserId(), InternalGrant::grant_option, GrantStmt::grant_option, InternalGrant::grantees, GrantStmt::grantees, GrantStmt::grantor, InternalGrant::is_grant, GrantStmt::is_grant, lappend(), lappend_oid(), lfirst, NIL, OBJECT_DATABASE, OBJECT_DOMAIN, OBJECT_FDW, OBJECT_FOREIGN_SERVER, OBJECT_FUNCTION, OBJECT_LANGUAGE, OBJECT_LARGEOBJECT, OBJECT_PROCEDURE, OBJECT_ROUTINE, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_TABLE, OBJECT_TABLESPACE, OBJECT_TYPE, 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().

361 {
362  InternalGrant istmt;
363  ListCell *cell;
364  const char *errormsg;
365  AclMode all_privileges;
366 
367  if (stmt->grantor)
368  {
369  Oid grantor;
370 
371  grantor = get_rolespec_oid(stmt->grantor, false);
372 
373  /*
374  * Currently, this clause is only for SQL compatibility, not very
375  * interesting otherwise.
376  */
377  if (grantor != GetUserId())
378  ereport(ERROR,
379  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
380  errmsg("grantor must be current user")));
381  }
382 
383  /*
384  * Turn the regular GrantStmt into the InternalGrant form.
385  */
386  istmt.is_grant = stmt->is_grant;
387  istmt.objtype = stmt->objtype;
388 
389  /* Collect the OIDs of the target objects */
390  switch (stmt->targtype)
391  {
392  case ACL_TARGET_OBJECT:
393  istmt.objects = objectNamesToOids(stmt->objtype, stmt->objects);
394  break;
396  istmt.objects = objectsInSchemaToOids(stmt->objtype, stmt->objects);
397  break;
398  /* ACL_TARGET_DEFAULTS should not be seen here */
399  default:
400  elog(ERROR, "unrecognized GrantStmt.targtype: %d",
401  (int) stmt->targtype);
402  }
403 
404  /* all_privs to be filled below */
405  /* privileges to be filled below */
406  istmt.col_privs = NIL; /* may get filled below */
407  istmt.grantees = NIL; /* filled below */
408  istmt.grant_option = stmt->grant_option;
409  istmt.behavior = stmt->behavior;
410 
411  /*
412  * Convert the RoleSpec list into an Oid list. Note that at this point we
413  * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
414  * there shouldn't be any additional work needed to support this case.
415  */
416  foreach(cell, stmt->grantees)
417  {
418  RoleSpec *grantee = (RoleSpec *) lfirst(cell);
419  Oid grantee_uid;
420 
421  switch (grantee->roletype)
422  {
423  case ROLESPEC_PUBLIC:
424  grantee_uid = ACL_ID_PUBLIC;
425  break;
426  default:
427  grantee_uid = get_rolespec_oid(grantee, false);
428  break;
429  }
430  istmt.grantees = lappend_oid(istmt.grantees, grantee_uid);
431  }
432 
433  /*
434  * Convert stmt->privileges, a list of AccessPriv nodes, into an AclMode
435  * bitmask. Note: objtype can't be OBJECT_COLUMN.
436  */
437  switch (stmt->objtype)
438  {
439  case OBJECT_TABLE:
440 
441  /*
442  * Because this might be a sequence, we test both relation and
443  * sequence bits, and later do a more limited test when we know
444  * the object type.
445  */
447  errormsg = gettext_noop("invalid privilege type %s for relation");
448  break;
449  case OBJECT_SEQUENCE:
450  all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
451  errormsg = gettext_noop("invalid privilege type %s for sequence");
452  break;
453  case OBJECT_DATABASE:
454  all_privileges = ACL_ALL_RIGHTS_DATABASE;
455  errormsg = gettext_noop("invalid privilege type %s for database");
456  break;
457  case OBJECT_DOMAIN:
458  all_privileges = ACL_ALL_RIGHTS_TYPE;
459  errormsg = gettext_noop("invalid privilege type %s for domain");
460  break;
461  case OBJECT_FUNCTION:
462  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
463  errormsg = gettext_noop("invalid privilege type %s for function");
464  break;
465  case OBJECT_LANGUAGE:
466  all_privileges = ACL_ALL_RIGHTS_LANGUAGE;
467  errormsg = gettext_noop("invalid privilege type %s for language");
468  break;
469  case OBJECT_LARGEOBJECT:
470  all_privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
471  errormsg = gettext_noop("invalid privilege type %s for large object");
472  break;
473  case OBJECT_SCHEMA:
474  all_privileges = ACL_ALL_RIGHTS_SCHEMA;
475  errormsg = gettext_noop("invalid privilege type %s for schema");
476  break;
477  case OBJECT_PROCEDURE:
478  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
479  errormsg = gettext_noop("invalid privilege type %s for procedure");
480  break;
481  case OBJECT_ROUTINE:
482  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
483  errormsg = gettext_noop("invalid privilege type %s for routine");
484  break;
485  case OBJECT_TABLESPACE:
486  all_privileges = ACL_ALL_RIGHTS_TABLESPACE;
487  errormsg = gettext_noop("invalid privilege type %s for tablespace");
488  break;
489  case OBJECT_TYPE:
490  all_privileges = ACL_ALL_RIGHTS_TYPE;
491  errormsg = gettext_noop("invalid privilege type %s for type");
492  break;
493  case 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 != OBJECT_TABLE)
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:65
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:162
Oid GetUserId(void)
Definition: miscinit.c:478
static AclMode string_to_privilege(const char *privname)
Definition: aclchk.c:3230
AclMode privileges
bool grant_option
Definition: parsenodes.h:2021
#define gettext_noop(x)
Definition: c.h:1197
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ACL_ALL_RIGHTS_TABLESPACE
Definition: acl.h:166
List * cols
Definition: parsenodes.h:2063
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define ACL_ALL_RIGHTS_LANGUAGE
Definition: acl.h:163
uint32 AclMode
Definition: parsenodes.h:80
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
#define ACL_ALL_RIGHTS_SCHEMA
Definition: acl.h:165
#define ACL_ALL_RIGHTS_TYPE
Definition: acl.h:167
List * lappend(List *list, void *datum)
Definition: list.c:336
bool is_grant
Definition: parsenodes.h:2013
static void ExecGrantStmt_oids(InternalGrant *istmt)
Definition: aclchk.c:565
static List * objectNamesToOids(ObjectType objtype, List *objnames)
Definition: aclchk.c:629
RoleSpecType roletype
Definition: parsenodes.h:339
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition: acl.c:5128
#define ACL_ALL_RIGHTS_SEQUENCE
Definition: acl.h:158
List * privileges
Definition: parsenodes.h:2018
static List * objectsInSchemaToOids(ObjectType objtype, List *nspnames)
Definition: aclchk.c:778
#define ereport(elevel,...)
Definition: elog.h:157
RoleSpec * grantor
Definition: parsenodes.h:2022
#define ACL_ALL_RIGHTS_LARGEOBJECT
Definition: acl.h:164
DropBehavior behavior
DropBehavior behavior
Definition: parsenodes.h:2023
#define lfirst(lc)
Definition: pg_list.h:169
List * objects
Definition: parsenodes.h:2016
ObjectType objtype
#define ACL_ALL_RIGHTS_DATABASE
Definition: acl.h:159
#define ACL_ALL_RIGHTS_FOREIGN_SERVER
Definition: acl.h:161
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:157
ObjectType objtype
Definition: parsenodes.h:2015
#define ACL_ID_PUBLIC
Definition: acl.h:46
#define ACL_ALL_RIGHTS_FDW
Definition: acl.h:160
static const char * privilege_to_string(AclMode privilege)
Definition: aclchk.c:3267
List * grantees
Definition: parsenodes.h:2020
GrantTargetType targtype
Definition: parsenodes.h:2014
char * priv_name
Definition: parsenodes.h:2062

◆ get_role_oid()

Oid get_role_oid ( const char *  rolename,
bool  missing_ok 
)

Definition at line 5094 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().

5095 {
5096  Oid oid;
5097 
5098  oid = GetSysCacheOid1(AUTHNAME, Anum_pg_authid_oid,
5100  if (!OidIsValid(oid) && !missing_ok)
5101  ereport(ERROR,
5102  (errcode(ERRCODE_UNDEFINED_OBJECT),
5103  errmsg("role \"%s\" does not exist", rolname)));
5104  return oid;
5105 }
NameData rolname
Definition: pg_authid.h:34
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:193
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:46
#define CStringGetDatum(X)
Definition: postgres.h:622
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ get_role_oid_or_public()

◆ get_rolespec_name()

char* get_rolespec_name ( const RoleSpec role)

Definition at line 5213 of file acl.c.

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

Referenced by AddRoleMems(), and DelRoleMems().

5214 {
5215  HeapTuple tp;
5216  Form_pg_authid authForm;
5217  char *rolename;
5218 
5219  tp = get_rolespec_tuple(role);
5220  authForm = (Form_pg_authid) GETSTRUCT(tp);
5221  rolename = pstrdup(NameStr(authForm->rolname));
5222  ReleaseSysCache(tp);
5223 
5224  return rolename;
5225 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
char * pstrdup(const char *in)
Definition: mcxt.c:1299
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:56
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
HeapTuple get_rolespec_tuple(const RoleSpec *role)
Definition: acl.c:5167
#define NameStr(name)
Definition: c.h:681

◆ get_rolespec_oid()

Oid get_rolespec_oid ( const RoleSpec role,
bool  missing_ok 
)

Definition at line 5128 of file acl.c.

References Assert, elog, ereport, errcode(), errmsg(), ERROR, get_role_oid(), GetSessionUserId(), GetUserId(), InvalidOid, RoleSpec::rolename, ROLESPEC_CSTRING, ROLESPEC_CURRENT_ROLE, 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().

5129 {
5130  Oid oid;
5131 
5132  switch (role->roletype)
5133  {
5134  case ROLESPEC_CSTRING:
5135  Assert(role->rolename);
5136  oid = get_role_oid(role->rolename, missing_ok);
5137  break;
5138 
5139  case ROLESPEC_CURRENT_ROLE:
5140  case ROLESPEC_CURRENT_USER:
5141  oid = GetUserId();
5142  break;
5143 
5144  case ROLESPEC_SESSION_USER:
5145  oid = GetSessionUserId();
5146  break;
5147 
5148  case ROLESPEC_PUBLIC:
5149  ereport(ERROR,
5150  (errcode(ERRCODE_UNDEFINED_OBJECT),
5151  errmsg("role \"%s\" does not exist", "public")));
5152  oid = InvalidOid; /* make compiler happy */
5153  break;
5154 
5155  default:
5156  elog(ERROR, "unexpected role type %d", role->roletype);
5157  }
5158 
5159  return oid;
5160 }
Oid GetUserId(void)
Definition: miscinit.c:478
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
Oid GetSessionUserId(void)
Definition: miscinit.c:512
Oid get_role_oid(const char *rolname, bool missing_ok)
Definition: acl.c:5094
#define ERROR
Definition: elog.h:46
RoleSpecType roletype
Definition: parsenodes.h:339
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
char * rolename
Definition: parsenodes.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ get_rolespec_tuple()

HeapTuple get_rolespec_tuple ( const RoleSpec role)

Definition at line 5167 of file acl.c.

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

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

5168 {
5169  HeapTuple tuple;
5170 
5171  switch (role->roletype)
5172  {
5173  case ROLESPEC_CSTRING:
5174  Assert(role->rolename);
5176  if (!HeapTupleIsValid(tuple))
5177  ereport(ERROR,
5178  (errcode(ERRCODE_UNDEFINED_OBJECT),
5179  errmsg("role \"%s\" does not exist", role->rolename)));
5180  break;
5181 
5182  case ROLESPEC_CURRENT_ROLE:
5183  case ROLESPEC_CURRENT_USER:
5184  tuple = SearchSysCache1(AUTHOID, GetUserId());
5185  if (!HeapTupleIsValid(tuple))
5186  elog(ERROR, "cache lookup failed for role %u", GetUserId());
5187  break;
5188 
5189  case ROLESPEC_SESSION_USER:
5191  if (!HeapTupleIsValid(tuple))
5192  elog(ERROR, "cache lookup failed for role %u", GetSessionUserId());
5193  break;
5194 
5195  case ROLESPEC_PUBLIC:
5196  ereport(ERROR,
5197  (errcode(ERRCODE_UNDEFINED_OBJECT),
5198  errmsg("role \"%s\" does not exist", "public")));
5199  tuple = NULL; /* make compiler happy */
5200  break;
5201 
5202  default:
5203  elog(ERROR, "unexpected role type %d", role->roletype);
5204  }
5205 
5206  return tuple;
5207 }
Oid GetUserId(void)
Definition: miscinit.c:478
int errcode(int sqlerrcode)
Definition: elog.c:698
Oid GetSessionUserId(void)
Definition: miscinit.c:512
#define ERROR
Definition: elog.h:46
#define CStringGetDatum(X)
Definition: postgres.h:622
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
RoleSpecType roletype
Definition: parsenodes.h:339
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
char * rolename
Definition: parsenodes.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ get_user_default_acl()

Acl* get_user_default_acl ( ObjectType  objtype,
Oid  ownerId,
Oid  nsp_oid 
)

Definition at line 5521 of file aclchk.c.

References acldefault(), aclequal(), aclitemsort(), aclmerge(), get_default_acl_internal(), InvalidOid, IsBootstrapProcessingMode, OBJECT_FUNCTION, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_TABLE, and OBJECT_TYPE.

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

5522 {
5523  Acl *result;
5524  Acl *glob_acl;
5525  Acl *schema_acl;
5526  Acl *def_acl;
5527  char defaclobjtype;
5528 
5529  /*
5530  * Use NULL during bootstrap, since pg_default_acl probably isn't there
5531  * yet.
5532  */
5534  return NULL;
5535 
5536  /* Check if object type is supported in pg_default_acl */
5537  switch (objtype)
5538  {
5539  case OBJECT_TABLE:
5540  defaclobjtype = DEFACLOBJ_RELATION;
5541  break;
5542 
5543  case OBJECT_SEQUENCE:
5544  defaclobjtype = DEFACLOBJ_SEQUENCE;
5545  break;
5546 
5547  case OBJECT_FUNCTION:
5548  defaclobjtype = DEFACLOBJ_FUNCTION;
5549  break;
5550 
5551  case OBJECT_TYPE:
5552  defaclobjtype = DEFACLOBJ_TYPE;
5553  break;
5554 
5555  case OBJECT_SCHEMA:
5556  defaclobjtype = DEFACLOBJ_NAMESPACE;
5557  break;
5558 
5559  default:
5560  return NULL;
5561  }
5562 
5563  /* Look up the relevant pg_default_acl entries */
5564  glob_acl = get_default_acl_internal(ownerId, InvalidOid, defaclobjtype);
5565  schema_acl = get_default_acl_internal(ownerId, nsp_oid, defaclobjtype);
5566 
5567  /* Quick out if neither entry exists */
5568  if (glob_acl == NULL && schema_acl == NULL)
5569  return NULL;
5570 
5571  /* We need to know the hard-wired default value, too */
5572  def_acl = acldefault(objtype, ownerId);
5573 
5574  /* If there's no global entry, substitute the hard-wired default */
5575  if (glob_acl == NULL)
5576  glob_acl = def_acl;
5577 
5578  /* Merge in any per-schema privileges */
5579  result = aclmerge(glob_acl, schema_acl, ownerId);
5580 
5581  /*
5582  * For efficiency, we want to return NULL if the result equals default.
5583  * This requires sorting both arrays to get an accurate comparison.
5584  */
5585  aclitemsort(result);
5586  aclitemsort(def_acl);
5587  if (aclequal(result, def_acl))
5588  result = NULL;
5589 
5590  return result;
5591 }
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
static Acl * get_default_acl_internal(Oid roleId, Oid nsp_oid, char objtype)
Definition: aclchk.c:5486
bool aclequal(const Acl *left_acl, const Acl *right_acl)
Definition: acl.c:495
#define InvalidOid
Definition: postgres_ext.h:36
Acl * aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId)
Definition: acl.c:437
void aclitemsort(Acl *acl)
Definition: acl.c:481
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:406

◆ has_bypassrls_privilege()

bool has_bypassrls_privilege ( Oid  roleid)

Definition at line 5462 of file aclchk.c.

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

Referenced by check_enable_rls(), and RI_Initial_Check().

5463 {
5464  bool result = false;
5465  HeapTuple utup;
5466 
5467  /* Superusers bypass all permission checking. */
5468  if (superuser_arg(roleid))
5469  return true;
5470 
5471  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5472  if (HeapTupleIsValid(utup))
5473  {
5474  result = ((Form_pg_authid) GETSTRUCT(utup))->rolbypassrls;
5475  ReleaseSysCache(utup);
5476  }
5477  return result;
5478 }
bool rolbypassrls
Definition: pg_authid.h:41
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:56
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78

◆ has_createrole_privilege()

bool has_createrole_privilege ( Oid  roleid)

Definition at line 5443 of file aclchk.c.

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

Referenced by check_object_ownership(), and have_createrole_privilege().

5444 {
5445  bool result = false;
5446  HeapTuple utup;
5447 
5448  /* Superusers bypass all permission checking. */
5449  if (superuser_arg(roleid))
5450  return true;
5451 
5452  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5453  if (HeapTupleIsValid(utup))
5454  {
5455  result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreaterole;
5456  ReleaseSysCache(utup);
5457  }
5458  return result;
5459 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:56
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
bool rolcreaterole
Definition: pg_authid.h:37
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78

◆ has_privs_of_role()

bool has_privs_of_role ( Oid  member,
Oid  role 
)

Definition at line 4843 of file acl.c.

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

Referenced by aclmask(), AlterObjectNamespace_internal(), AlterObjectOwner_internal(), AlterObjectRename_internal(), check_role_for_policy(), DropOwnedObjects(), pg_class_aclmask_ext(), 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_aclmask(), 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_statistics_object_ownercheck(), pg_subscription_ownercheck(), pg_tablespace_ownercheck(), pg_ts_config_ownercheck(), pg_ts_dict_ownercheck(), pg_type_ownercheck(), ReassignOwnedObjects(), and TerminateOtherDBBackends().

4844 {
4845  /* Fast path for simple case */
4846  if (member == role)
4847  return true;
4848 
4849  /* Superusers have every privilege, so are part of every role */
4850  if (superuser_arg(member))
4851  return true;
4852 
4853  /*
4854  * Find all the roles that member has the privileges of, including
4855  * multi-level recursion, then see if target role is any one of them.
4856  */
4858  InvalidOid, NULL),
4859  role);
4860 }
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
#define InvalidOid
Definition: postgres_ext.h:36
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:689
static List * roles_is_member_of(Oid roleid, enum RoleRecurseType type, Oid admin_of, bool *is_admin)
Definition: acl.c:4726

◆ initialize_acl()

void initialize_acl ( void  )

Definition at line 4648 of file acl.c.

References AUTHMEMROLEMEM, AUTHOID, cached_db_hash, CacheRegisterSyscacheCallback(), DATABASEOID, GetSysCacheHashValue1, IsBootstrapProcessingMode, MyDatabaseId, ObjectIdGetDatum, and RoleMembershipCacheCallback().

Referenced by InitPostgres().

4649 {
4651  {
4652  cached_db_hash =
4655 
4656  /*
4657  * In normal mode, set a callback on any syscache invalidation of rows
4658  * of pg_auth_members (for roles_is_member_of()), pg_authid (for
4659  * has_rolinherit()), or pg_database (for roles_is_member_of())
4660  */
4663  (Datum) 0);
4666  (Datum) 0);
4669  (Datum) 0);
4670  }
4671 }
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:202
static uint32 cached_db_hash
Definition: acl.c:72
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1435
uintptr_t Datum
Definition: postgres.h:411
Oid MyDatabaseId
Definition: globals.c:88
static void RoleMembershipCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: acl.c:4678
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:406

◆ is_admin_of_role()

bool is_admin_of_role ( Oid  member,
Oid  role 
)

Definition at line 4931 of file acl.c.

References GetSessionUserId(), InLocalUserIdChange(), InSecurityRestrictedOperation(), ROLERECURSE_MEMBERS, roles_is_member_of(), and superuser_arg().

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

4932 {
4933  bool result = false;
4934 
4935  if (superuser_arg(member))
4936  return true;
4937 
4938  if (member == role)
4939 
4940  /*
4941  * A role can admin itself when it matches the session user and we're
4942  * outside any security-restricted operation, SECURITY DEFINER or
4943  * similar context. SQL-standard roles cannot self-admin. However,
4944  * SQL-standard users are distinct from roles, and they are not
4945  * grantable like roles: PostgreSQL's role-user duality extends the
4946  * standard. Checking for a session user match has the effect of
4947  * letting a role self-admin only when it's conspicuously behaving
4948  * like a user. Note that allowing self-admin under a mere SET ROLE
4949  * would make WITH ADMIN OPTION largely irrelevant; any member could
4950  * SET ROLE to issue the otherwise-forbidden command.
4951  *
4952  * Withholding self-admin in a security-restricted operation prevents
4953  * object owners from harnessing the session user identity during
4954  * administrative maintenance. Suppose Alice owns a database, has
4955  * issued "GRANT alice TO bob", and runs a daily ANALYZE. Bob creates
4956  * an alice-owned SECURITY DEFINER function that issues "REVOKE alice
4957  * FROM carol". If he creates an expression index calling that
4958  * function, Alice will attempt the REVOKE during each ANALYZE.
4959  * Checking InSecurityRestrictedOperation() thwarts that attack.
4960  *
4961  * Withholding self-admin in SECURITY DEFINER functions makes their
4962  * behavior independent of the calling user. There's no security or
4963  * SQL-standard-conformance need for that restriction, though.
4964  *
4965  * A role cannot have actual WITH ADMIN OPTION on itself, because that
4966  * would imply a membership loop. Therefore, we're done either way.
4967  */
4968  return member == GetSessionUserId() &&
4970 
4971  (void) roles_is_member_of(member, ROLERECURSE_MEMBERS, role, &result);
4972  return result;
4973 }
bool InLocalUserIdChange(void)
Definition: miscinit.c:601
Oid GetSessionUserId(void)
Definition: miscinit.c:512
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
bool InSecurityRestrictedOperation(void)
Definition: miscinit.c:610
static List * roles_is_member_of(Oid roleid, enum RoleRecurseType type, Oid admin_of, bool *is_admin)
Definition: acl.c:4726

◆ is_member_of_role()

bool is_member_of_role ( Oid  member,
Oid  role 
)

Definition at line 4869 of file acl.c.

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

Referenced by calculate_database_size(), calculate_tablespace_size(), check_is_member_of_role(), check_role(), convert_and_check_filename(), DoCopy(), file_fdw_validator(), get_explain_guc_options(), GetConfigOption(), GetConfigOptionByName(), GetConfigOptionByNum(), GetConfigOptionResetString(), pg_role_aclcheck(), pg_stat_get_wal_receiver(), pg_stat_get_wal_senders(), pg_stat_statements_internal(), pgrowlocks(), and ShowAllGUCConfig().

4870 {
4871  /* Fast path for simple case */
4872  if (member == role)
4873  return true;
4874 
4875  /* Superusers have every privilege, so are part of every role */
4876  if (superuser_arg(member))
4877  return true;
4878 
4879  /*
4880  * Find all the roles that member is a member of, including multi-level
4881  * recursion, then see if target role is any one of them.
4882  */
4884  InvalidOid, NULL),
4885  role);
4886 }
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
#define InvalidOid
Definition: postgres_ext.h:36
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:689
static List * roles_is_member_of(Oid roleid, enum RoleRecurseType type, Oid admin_of, bool *is_admin)
Definition: acl.c:4726

◆ is_member_of_role_nosuper()

bool is_member_of_role_nosuper ( Oid  member,
Oid  role 
)

Definition at line 4909 of file acl.c.

References InvalidOid, list_member_oid(), ROLERECURSE_MEMBERS, and roles_is_member_of().

Referenced by AddRoleMems(), and is_member().

4910 {
4911  /* Fast path for simple case */
4912  if (member == role)
4913  return true;
4914 
4915  /*
4916  * Find all the roles that member is a member of, including multi-level
4917  * recursion, then see if target role is any one of them.
4918  */
4920  InvalidOid, NULL),
4921  role);
4922 }
#define InvalidOid
Definition: postgres_ext.h:36
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:689
static List * roles_is_member_of(Oid roleid, enum RoleRecurseType type, Oid admin_of, bool *is_admin)
Definition: acl.c:4726

◆ make_empty_acl()

Acl* make_empty_acl ( void  )

Definition at line 384 of file acl.c.

References allocacl().

Referenced by SetDefaultACL().

385 {
386  return allocacl(0);
387 }
static Acl * allocacl(int n)
Definition: acl.c:362

◆ pg_attribute_aclcheck()

AclResult pg_attribute_aclcheck ( Oid  table_oid,
AttrNumber  attnum,
Oid  roleid,
AclMode  mode 
)

Definition at line 4551 of file aclchk.c.

References pg_attribute_aclcheck_ext().

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

4553 {
4554  return pg_attribute_aclcheck_ext(table_oid, attnum, roleid, mode, NULL);
4555 }
static PgChecksumMode mode
Definition: pg_checksums.c:63
int16 attnum
Definition: pg_attribute.h:83
AclResult pg_attribute_aclcheck_ext(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode, bool *is_missing)
Definition: aclchk.c:4565

◆ pg_attribute_aclcheck_all()

AclResult pg_attribute_aclcheck_all ( Oid  table_oid,
Oid  roleid,
AclMode  mode,
AclMaskHow  how 
)

Definition at line 4595 of file aclchk.c.

References ACLCHECK_NO_PRIV, ACLCHECK_OK, ACLMASK_ALL, ACLMASK_ANY, 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(), has_any_column_privilege_name_name(), and statext_is_compatible_clause().

4597 {
4598  AclResult result;
4599  HeapTuple classTuple;
4600  Form_pg_class classForm;
4601  AttrNumber nattrs;
4602  AttrNumber curr_att;
4603 
4604  /*
4605  * Must fetch pg_class row to check number of attributes. As in
4606  * pg_attribute_aclmask, we prefer to return "no privileges" instead of
4607  * throwing an error if we get any unexpected lookup errors.
4608  */
4609  classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
4610  if (!HeapTupleIsValid(classTuple))
4611  return ACLCHECK_NO_PRIV;
4612  classForm = (Form_pg_class) GETSTRUCT(classTuple);
4613 
4614  nattrs = classForm->relnatts;
4615 
4616  ReleaseSysCache(classTuple);
4617 
4618  /*
4619  * Initialize result in case there are no non-dropped columns. We want to
4620  * report failure in such cases for either value of 'how'.
4621  */
4622  result = ACLCHECK_NO_PRIV;
4623 
4624  for (curr_att = 1; curr_att <= nattrs; curr_att++)
4625  {
4626  HeapTuple attTuple;
4627  AclMode attmask;
4628 
4629  attTuple = SearchSysCache2(ATTNUM,
4630  ObjectIdGetDatum(table_oid),
4631  Int16GetDatum(curr_att));
4632  if (!HeapTupleIsValid(attTuple))
4633  continue;
4634 
4635  /* ignore dropped columns */
4636  if (((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped)
4637  {
4638  ReleaseSysCache(attTuple);
4639  continue;
4640  }
4641 
4642  /*
4643  * Here we hard-wire knowledge that the default ACL for a column
4644  * grants no privileges, so that we can fall out quickly in the very
4645  * common case where attacl is null.
4646  */
4647  if (heap_attisnull(attTuple, Anum_pg_attribute_attacl, NULL))
4648  attmask = 0;
4649  else
4650  attmask = pg_attribute_aclmask(table_oid, curr_att, roleid,
4651  mode, ACLMASK_ANY);
4652 
4653  ReleaseSysCache(attTuple);
4654 
4655  if (attmask != 0)
4656  {
4657  result = ACLCHECK_OK;
4658  if (how == ACLMASK_ANY)
4659  break; /* succeed on any success */
4660  }
4661  else
4662  {
4663  result = ACLCHECK_NO_PRIV;
4664  if (how == ACLMASK_ALL)
4665  break; /* fail on any failure */
4666  }
4667  }
4668 
4669  return result;
4670 }
static PgChecksumMode mode
Definition: pg_checksums.c:63
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3701
#define Int16GetDatum(X)
Definition: postgres.h:495
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
uint32 AclMode
Definition: parsenodes.h:80
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
AclResult
Definition: acl.h:177
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1138
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
int16 AttrNumber
Definition: attnum.h:21

◆ pg_attribute_aclcheck_ext()

AclResult pg_attribute_aclcheck_ext ( Oid  table_oid,
AttrNumber  attnum,
Oid  roleid,
AclMode  mode,
bool is_missing 
)

Definition at line 4565 of file aclchk.c.

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

Referenced by column_privilege_check(), and pg_attribute_aclcheck().

4567 {
4568  if (pg_attribute_aclmask_ext(table_oid, attnum, roleid, mode,
4569  ACLMASK_ANY, is_missing) != 0)
4570  return ACLCHECK_OK;
4571  else
4572  return ACLCHECK_NO_PRIV;
4573 }
static PgChecksumMode mode
Definition: pg_checksums.c:63
int16 attnum
Definition: pg_attribute.h:83
AclMode pg_attribute_aclmask_ext(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how, bool *is_missing)
Definition: aclchk.c:3715

◆ pg_attribute_aclmask()

AclMode pg_attribute_aclmask ( Oid  table_oid,
AttrNumber  attnum,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 3701 of file aclchk.c.

References pg_attribute_aclmask_ext().

Referenced by pg_aclmask(), and pg_attribute_aclcheck_all().

3703 {
3704  return pg_attribute_aclmask_ext(table_oid, attnum, roleid,
3705  mask, how, NULL);
3706 }
int16 attnum
Definition: pg_attribute.h:83
AclMode pg_attribute_aclmask_ext(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how, bool *is_missing)
Definition: aclchk.c:3715

◆ pg_attribute_aclmask_ext()

AclMode pg_attribute_aclmask_ext ( Oid  table_oid,
AttrNumber  attnum,
Oid  roleid,
AclMode  mask,
AclMaskHow  how,
bool is_missing 
)

Definition at line 3715 of file aclchk.c.

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

Referenced by pg_attribute_aclcheck_ext(), and pg_attribute_aclmask().

3717 {
3718  AclMode result;
3719  HeapTuple classTuple;
3720  HeapTuple attTuple;
3721  Form_pg_class classForm;
3722  Form_pg_attribute attributeForm;
3723  Datum aclDatum;
3724  bool isNull;
3725  Acl *acl;
3726  Oid ownerId;
3727 
3728  /*
3729  * First, get the column's ACL from its pg_attribute entry
3730  */
3731  attTuple = SearchSysCache2(ATTNUM,
3732  ObjectIdGetDatum(table_oid),
3734  if (!HeapTupleIsValid(attTuple))
3735  {
3736  if (is_missing != NULL)
3737  {
3738  /* return "no privileges" instead of throwing an error */
3739  *is_missing = true;
3740  return 0;
3741  }
3742  else
3743  ereport(ERROR,
3744  (errcode(ERRCODE_UNDEFINED_COLUMN),
3745  errmsg("attribute %d of relation with OID %u does not exist",
3746  attnum, table_oid)));
3747  }
3748 
3749  attributeForm = (Form_pg_attribute) GETSTRUCT(attTuple);
3750 
3751  /* Check dropped columns, too */
3752  if (attributeForm->attisdropped)
3753  {
3754  if (is_missing != NULL)
3755  {
3756  /* return "no privileges" instead of throwing an error */
3757  *is_missing = true;
3758  ReleaseSysCache(attTuple);
3759  return 0;
3760  }
3761  else
3762  ereport(ERROR,
3763  (errcode(ERRCODE_UNDEFINED_COLUMN),
3764  errmsg("attribute %d of relation with OID %u does not exist",
3765  attnum, table_oid)));
3766  }
3767 
3768  aclDatum = SysCacheGetAttr(ATTNUM, attTuple, Anum_pg_attribute_attacl,
3769  &isNull);
3770 
3771  /*
3772  * Here we hard-wire knowledge that the default ACL for a column grants no
3773  * privileges, so that we can fall out quickly in the very common case
3774  * where attacl is null.
3775  */
3776  if (isNull)
3777  {
3778  ReleaseSysCache(attTuple);
3779  return 0;
3780  }
3781 
3782  /*
3783  * Must get the relation's ownerId from pg_class. Since we already found
3784  * a pg_attribute entry, the only likely reason for this to fail is that a
3785  * concurrent DROP of the relation committed since then (which could only
3786  * happen if we don't have lock on the relation). We prefer to report "no
3787  * privileges" rather than failing in such a case, so as to avoid unwanted
3788  * failures in has_column_privilege() tests.
3789  */
3790  classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
3791  if (!HeapTupleIsValid(classTuple))
3792  {
3793  ReleaseSysCache(attTuple);
3794  return 0;
3795  }
3796  classForm = (Form_pg_class) GETSTRUCT(classTuple);
3797 
3798  ownerId = classForm->relowner;
3799 
3800  ReleaseSysCache(classTuple);
3801 
3802  /* detoast column's ACL if necessary */
3803  acl = DatumGetAclP(aclDatum);
3804 
3805  result = aclmask(acl, roleid, ownerId, mask, how);
3806 
3807  /* if we have a detoasted copy, free it */
3808  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3809  pfree(acl);
3810 
3811  ReleaseSysCache(attTuple);
3812 
3813  return result;
3814 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define DatumGetAclP(X)
Definition: acl.h:120
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1306
#define Int16GetDatum(X)
Definition: postgres.h:495
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
char * Pointer
Definition: c.h:418
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
int16 attnum
Definition: pg_attribute.h:83
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1138
#define DatumGetPointer(X)
Definition: postgres.h:593
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_class_aclcheck()

AclResult pg_class_aclcheck ( Oid  table_oid,
Oid  roleid,
AclMode  mode 
)

◆ pg_class_aclcheck_ext()

AclResult pg_class_aclcheck_ext ( Oid  table_oid,
Oid  roleid,
AclMode  mode,
bool is_missing 
)

Definition at line 4692 of file aclchk.c.

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

Referenced by column_privilege_check(), and pg_class_aclcheck().

4694 {
4695  if (pg_class_aclmask_ext(table_oid, roleid, mode,
4696  ACLMASK_ANY, is_missing) != 0)
4697  return ACLCHECK_OK;
4698  else
4699  return ACLCHECK_NO_PRIV;
4700 }
static PgChecksumMode mode
Definition: pg_checksums.c:63
AclMode pg_class_aclmask_ext(Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how, bool *is_missing)
Definition: aclchk.c:3833

◆ pg_class_aclmask()

AclMode pg_class_aclmask ( Oid  table_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 3820 of file aclchk.c.

References pg_class_aclmask_ext().

Referenced by ExecCheckRTEPerms(), and pg_aclmask().

3822 {
3823  return pg_class_aclmask_ext(table_oid, roleid, mask, how, NULL);
3824 }
AclMode pg_class_aclmask_ext(Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how, bool *is_missing)
Definition: aclchk.c:3833

◆ pg_class_aclmask_ext()

AclMode pg_class_aclmask_ext ( Oid  table_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how,
bool is_missing 
)

Definition at line 3833 of file aclchk.c.

References ACL_DELETE, ACL_INSERT, ACL_SELECT, ACL_TRUNCATE, ACL_UPDATE, ACL_USAGE, acldefault(), aclmask(), DatumGetAclP, DatumGetPointer, ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg(), ERROR, GETSTRUCT, has_privs_of_role(), HeapTupleIsValid, IsSystemClass(), OBJECT_SEQUENCE, OBJECT_TABLE, ObjectIdGetDatum, pfree(), ReleaseSysCache(), RELOID, SearchSysCache1(), superuser_arg(), and SysCacheGetAttr().

Referenced by pg_class_aclcheck_ext(), and pg_class_aclmask().

3835 {
3836  AclMode result;
3837  HeapTuple tuple;
3838  Form_pg_class classForm;
3839  Datum aclDatum;
3840  bool isNull;
3841  Acl *acl;
3842  Oid ownerId;
3843 
3844  /*
3845  * Must get the relation's tuple from pg_class
3846  */
3847  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
3848  if (!HeapTupleIsValid(tuple))
3849  {
3850  if (is_missing != NULL)
3851  {
3852  /* return "no privileges" instead of throwing an error */
3853  *is_missing = true;
3854  return 0;
3855  }
3856  else
3857  ereport(ERROR,
3859  errmsg("relation with OID %u does not exist",
3860  table_oid)));
3861  }
3862 
3863  classForm = (Form_pg_class) GETSTRUCT(tuple);
3864 
3865  /*
3866  * Deny anyone permission to update a system catalog unless
3867  * pg_authid.rolsuper is set.
3868  *
3869  * As of 7.4 we have some updatable system views; those shouldn't be
3870  * protected in this way. Assume the view rules can take care of
3871  * themselves. ACL_USAGE is if we ever have system sequences.
3872  */
3873  if ((mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE | ACL_USAGE)) &&
3874  IsSystemClass(table_oid, classForm) &&
3875  classForm->relkind != RELKIND_VIEW &&
3876  !superuser_arg(roleid))
3878 
3879  /*
3880  * Otherwise, superusers bypass all permission-checking.
3881  */
3882  if (superuser_arg(roleid))
3883  {
3884  ReleaseSysCache(tuple);
3885  return mask;
3886  }
3887 
3888  /*
3889  * Normal case: get the relation's ACL from pg_class
3890  */
3891  ownerId = classForm->relowner;
3892 
3893  aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
3894  &isNull);
3895  if (isNull)
3896  {
3897  /* No ACL, so build default ACL */
3898  switch (classForm->relkind)
3899  {
3900  case RELKIND_SEQUENCE:
3901  acl = acldefault(OBJECT_SEQUENCE, ownerId);
3902  break;
3903  default:
3904  acl = acldefault(OBJECT_TABLE, ownerId);
3905  break;
3906  }
3907  aclDatum = (Datum) 0;
3908  }
3909  else
3910  {
3911  /* detoast rel's ACL if necessary */
3912  acl = DatumGetAclP(aclDatum);
3913  }
3914 
3915  result = aclmask(acl, roleid, ownerId, mask, how);
3916 
3917  /* if we have a detoasted copy, free it */
3918  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3919  pfree(acl);
3920 
3921  ReleaseSysCache(tuple);
3922 
3923  /*
3924  * Check if ACL_SELECT is being checked and, if so, and not set already as
3925  * part of the result, then check if the user is a member of the
3926  * pg_read_all_data role, which allows read access to all relations.
3927  */
3928  if (mask & ACL_SELECT && !(result & ACL_SELECT) &&
3929  has_privs_of_role(roleid, ROLE_PG_READ_ALL_DATA))
3930  result |= ACL_SELECT;
3931 
3932  /*
3933  * Check if ACL_INSERT, ACL_UPDATE, or ACL_DELETE is being checked and, if
3934  * so, and not set already as part of the result, then check if the user
3935  * is a member of the pg_write_all_data role, which allows
3936  * INSERT/UPDATE/DELETE access to all relations (except system catalogs,
3937  * which requires superuser, see above).
3938  */
3939  if (mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE) &&
3940  !(result & (ACL_INSERT | ACL_UPDATE | ACL_DELETE)) &&
3941  has_privs_of_role(roleid, ROLE_PG_WRITE_ALL_DATA))
3942  result |= (mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE));
3943 
3944  return result;
3945 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:77
#define DatumGetAclP(X)
Definition: acl.h:120
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1306
int errcode(int sqlerrcode)
Definition: elog.c:698
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define ACL_DELETE
Definition: parsenodes.h:85
unsigned int Oid
Definition: postgres_ext.h:31
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:86
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
char * Pointer
Definition: c.h:418
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_USAGE
Definition: parsenodes.h:90
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
#define ACL_UPDATE
Definition: parsenodes.h:84
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define ACL_SELECT
Definition: parsenodes.h:83
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define ACL_INSERT
Definition: parsenodes.h:82
#define DatumGetPointer(X)
Definition: postgres.h:593
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define ACL_TRUNCATE
Definition: parsenodes.h:86

◆ pg_class_ownercheck()

bool pg_class_ownercheck ( Oid  class_oid,
Oid  roleid 
)

Definition at line 4818 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(), ATExecChangeOwner(), ATSimplePermissions(), brin_desummarize_range(), brin_summarize_range(), check_enable_rls(), check_object_ownership(), cluster_rel(), CreateStatistics(), DefineQueryRewrite(), EnableDisableRule(), ExecuteTruncateGuts(), get_tables_to_cluster(), gin_clean_pending_list(), heap_force_common(), MergeAttributes(), PublicationAddTables(), RangeVarCallbackForAlterRelation(), RangeVarCallbackForDropRelation(), RangeVarCallbackForPolicy(), RangeVarCallbackForReindexIndex(), RangeVarCallbackForRenameRule(), RangeVarCallbackForRenameTrigger(), RangeVarCallbackOwnsRelation(), RangeVarCallbackOwnsTable(), RangeVarGetAndCheckCreationNamespace(), ReindexMultipleTables(), renameatt_check(), RI_Initial_Check(), and vacuum_is_relation_owner().

4819 {
4820  HeapTuple tuple;
4821  Oid ownerId;
4822 
4823  /* Superusers bypass all permission checking. */
4824  if (superuser_arg(roleid))
4825  return true;
4826 
4827  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(class_oid));
4828  if (!HeapTupleIsValid(tuple))
4829  ereport(ERROR,
4831  errmsg("relation with OID %u does not exist", class_oid)));
4832 
4833  ownerId = ((Form_pg_class) GETSTRUCT(tuple))->relowner;
4834 
4835  ReleaseSysCache(tuple);
4836 
4837  return has_privs_of_role(roleid, ownerId);
4838 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:77
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_collation_ownercheck()

bool pg_collation_ownercheck ( Oid  coll_oid,
Oid  roleid 
)

Definition at line 5262 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().

5263 {
5264  HeapTuple tuple;
5265  Oid ownerId;
5266 
5267  /* Superusers bypass all permission checking. */
5268  if (superuser_arg(roleid))
5269  return true;
5270 
5271  tuple = SearchSysCache1(COLLOID, ObjectIdGetDatum(coll_oid));
5272  if (!HeapTupleIsValid(tuple))
5273  ereport(ERROR,
5274  (errcode(ERRCODE_UNDEFINED_OBJECT),
5275  errmsg("collation with OID %u does not exist", coll_oid)));
5276 
5277  ownerId = ((Form_pg_collation) GETSTRUCT(tuple))->collowner;
5278 
5279  ReleaseSysCache(tuple);
5280 
5281  return has_privs_of_role(roleid, ownerId);
5282 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:56
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_conversion_ownercheck()

bool pg_conversion_ownercheck ( Oid  conv_oid,
Oid  roleid 
)

Definition at line 5288 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().

5289 {
5290  HeapTuple tuple;
5291  Oid ownerId;
5292 
5293  /* Superusers bypass all permission checking. */
5294  if (superuser_arg(roleid))
5295  return true;
5296 
5297  tuple = SearchSysCache1(CONVOID, ObjectIdGetDatum(conv_oid));
5298  if (!HeapTupleIsValid(tuple))
5299  ereport(ERROR,
5300  (errcode(ERRCODE_UNDEFINED_OBJECT),
5301  errmsg("conversion with OID %u does not exist", conv_oid)));
5302 
5303  ownerId = ((Form_pg_conversion) GETSTRUCT(tuple))->conowner;
5304 
5305  ReleaseSysCache(tuple);
5306 
5307  return has_privs_of_role(roleid, ownerId);
5308 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
FormData_pg_conversion * Form_pg_conversion
Definition: pg_conversion.h:61
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_database_aclcheck()

◆ pg_database_aclmask()

AclMode pg_database_aclmask ( Oid  db_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 3951 of file aclchk.c.

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

Referenced by pg_aclmask(), and pg_database_aclcheck().

3953 {
3954  AclMode result;
3955  HeapTuple tuple;
3956  Datum aclDatum;
3957  bool isNull;
3958  Acl *acl;
3959  Oid ownerId;
3960 
3961  /* Superusers bypass all permission checking. */
3962  if (superuser_arg(roleid))
3963  return mask;
3964 
3965  /*
3966  * Get the database's ACL from pg_database
3967  */
3968  tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(db_oid));
3969  if (!HeapTupleIsValid(tuple))
3970  ereport(ERROR,
3971  (errcode(ERRCODE_UNDEFINED_DATABASE),
3972  errmsg("database with OID %u does not exist", db_oid)));
3973 
3974  ownerId = ((Form_pg_database) GETSTRUCT(tuple))->datdba;
3975 
3976  aclDatum = SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_datacl,
3977  &isNull);
3978  if (isNull)
3979  {
3980  /* No ACL, so build default ACL */
3981  acl = acldefault(OBJECT_DATABASE, ownerId);
3982  aclDatum = (Datum) 0;
3983  }
3984  else
3985  {
3986  /* detoast ACL if necessary */
3987  acl = DatumGetAclP(aclDatum);
3988  }
3989 
3990  result = aclmask(acl, roleid, ownerId, mask, how);
3991 
3992  /* if we have a detoasted copy, free it */
3993  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3994  pfree(acl);
3995 
3996  ReleaseSysCache(tuple);
3997 
3998  return result;
3999 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
#define DatumGetAclP(X)
Definition: acl.h:120
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1306
int errcode(int sqlerrcode)
Definition: elog.c:698
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
unsigned int Oid
Definition: postgres_ext.h:31
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
char * Pointer
Definition: c.h:418
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define DatumGetPointer(X)
Definition: postgres.h:593
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_database_ownercheck()

bool pg_database_ownercheck ( Oid  db_oid,
Oid  roleid 
)

Definition at line 5236 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(), check_object_ownership(), createdb(), dropdb(), movedb(), ReindexMultipleTables(), RenameDatabase(), and vacuum_is_relation_owner().

5237 {
5238  HeapTuple tuple;
5239  Oid dba;
5240 
5241  /* Superusers bypass all permission checking. */
5242  if (superuser_arg(roleid))
5243  return true;
5244 
5245  tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(db_oid));
5246  if (!HeapTupleIsValid(tuple))
5247  ereport(ERROR,
5248  (errcode(ERRCODE_UNDEFINED_DATABASE),
5249  errmsg("database with OID %u does not exist", db_oid)));
5250 
5251  dba = ((Form_pg_database) GETSTRUCT(tuple))->datdba;
5252 
5253  ReleaseSysCache(tuple);
5254 
5255  return has_privs_of_role(roleid, dba);
5256 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_event_trigger_ownercheck()

bool pg_event_trigger_ownercheck ( Oid  et_oid,
Oid  roleid 
)

Definition at line 5209 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().

5210 {
5211  HeapTuple tuple;
5212  Oid ownerId;
5213 
5214  /* Superusers bypass all permission checking. */
5215  if (superuser_arg(roleid))
5216  return true;
5217 
5219  if (!HeapTupleIsValid(tuple))
5220  ereport(ERROR,
5221  (errcode(ERRCODE_UNDEFINED_OBJECT),
5222  errmsg("event trigger with OID %u does not exist",
5223  et_oid)));
5224 
5225  ownerId = ((Form_pg_event_trigger) GETSTRUCT(tuple))->evtowner;
5226 
5227  ReleaseSysCache(tuple);
5228 
5229  return has_privs_of_role(roleid, ownerId);
5230 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define ereport(elevel,...)
Definition: elog.h:157
FormData_pg_event_trigger * Form_pg_event_trigger
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_extension_ownercheck()

bool pg_extension_ownercheck ( Oid  ext_oid,
Oid  roleid 
)

Definition at line 5314 of file aclchk.c.

References AccessShareLock, BTEqualStrategyNumber, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, has_privs_of_role(), HeapTupleIsValid, ObjectIdGetDatum, ScanKeyInit(), superuser_arg(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

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

5315 {
5316  Relation pg_extension;
5317  ScanKeyData entry[1];
5318  SysScanDesc scan;
5319  HeapTuple tuple;
5320  Oid ownerId;
5321 
5322  /* Superusers bypass all permission checking. */
5323  if (superuser_arg(roleid))
5324  return true;
5325 
5326  /* There's no syscache for pg_extension, so do it the hard way */
5327  pg_extension = table_open(ExtensionRelationId, AccessShareLock);
5328 
5329  ScanKeyInit(&entry[0],
5330  Anum_pg_extension_oid,
5331  BTEqualStrategyNumber, F_OIDEQ,
5332  ObjectIdGetDatum(ext_oid));
5333 
5334  scan = systable_beginscan(pg_extension,
5335  ExtensionOidIndexId, true,
5336  NULL, 1, entry);
5337 
5338  tuple = systable_getnext(scan);
5339  if (!HeapTupleIsValid(tuple))
5340  ereport(ERROR,
5341  (errcode(ERRCODE_UNDEFINED_OBJECT),
5342  errmsg("extension with OID %u does not exist", ext_oid)));
5343 
5344  ownerId = ((Form_pg_extension) GETSTRUCT(tuple))->extowner;
5345 
5346  systable_endscan(scan);
5347  table_close(pg_extension, AccessShareLock);
5348 
5349  return has_privs_of_role(roleid, ownerId);
5350 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:698
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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
FormData_pg_extension * Form_pg_extension
Definition: pg_extension.h:52
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:909
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ pg_foreign_data_wrapper_aclcheck()

AclResult pg_foreign_data_wrapper_aclcheck ( Oid  fdw_oid,
Oid  roleid,
AclMode  mode 
)

◆ pg_foreign_data_wrapper_aclmask()

AclMode pg_foreign_data_wrapper_aclmask ( Oid  fdw_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 4344 of file aclchk.c.

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

Referenced by pg_aclmask(), and pg_foreign_data_wrapper_aclcheck().

4346 {
4347  AclMode result;
4348  HeapTuple tuple;
4349  Datum aclDatum;
4350  bool isNull;
4351  Acl *acl;
4352  Oid ownerId;
4353 
4355 
4356  /* Bypass permission checks for superusers */
4357  if (superuser_arg(roleid))
4358  return mask;
4359 
4360  /*
4361  * Must get the FDW's tuple from pg_foreign_data_wrapper
4362  */
4364  if (!HeapTupleIsValid(tuple))
4365  ereport(ERROR,
4366  (errcode(ERRCODE_UNDEFINED_OBJECT),
4367  errmsg("foreign-data wrapper with OID %u does not exist",
4368  fdw_oid)));
4369  fdwForm = (Form_pg_foreign_data_wrapper) GETSTRUCT(tuple);
4370 
4371  /*
4372  * Normal case: get the FDW's ACL from pg_foreign_data_wrapper
4373  */
4374  ownerId = fdwForm->fdwowner;
4375 
4376  aclDatum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID, tuple,
4377  Anum_pg_foreign_data_wrapper_fdwacl, &isNull);
4378  if (isNull)
4379  {
4380  /* No ACL, so build default ACL */
4381  acl = acldefault(OBJECT_FDW, ownerId);
4382  aclDatum = (Datum) 0;
4383  }
4384  else
4385  {
4386  /* detoast rel's ACL if necessary */
4387  acl = DatumGetAclP(aclDatum);
4388  }
4389 
4390  result = aclmask(acl, roleid, ownerId, mask, how);
4391 
4392  /* if we have a detoasted copy, free it */
4393  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
4394  pfree(acl);
4395 
4396  ReleaseSysCache(tuple);
4397 
4398  return result;
4399 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define DatumGetAclP(X)
Definition: acl.h:120
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1306
int errcode(int sqlerrcode)
Definition: elog.c:698
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
unsigned int Oid
Definition: postgres_ext.h:31
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
char * Pointer
Definition: c.h:418
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
#define DatumGetPointer(X)
Definition: postgres.h:593
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_foreign_data_wrapper_ownercheck()

bool pg_foreign_data_wrapper_ownercheck ( Oid  srv_oid,
Oid  roleid 
)

Definition at line 5155 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().

5156 {
5157  HeapTuple tuple;
5158  Oid ownerId;
5159 
5160  /* Superusers bypass all permission checking. */
5161  if (superuser_arg(roleid))
5162  return true;
5163 
5165  if (!HeapTupleIsValid(tuple))
5166  ereport(ERROR,
5167  (errcode(ERRCODE_UNDEFINED_OBJECT),
5168  errmsg("foreign-data wrapper with OID %u does not exist",
5169  srv_oid)));
5170 
5171  ownerId = ((Form_pg_foreign_data_wrapper) GETSTRUCT(tuple))->fdwowner;
5172 
5173  ReleaseSysCache(tuple);
5174 
5175  return has_privs_of_role(roleid, ownerId);
5176 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_foreign_server_aclcheck()

AclResult pg_foreign_server_aclcheck ( Oid  srv_oid,
Oid  roleid,
AclMode  mode 
)

Definition at line 4794 of file aclchk.c.

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

Referenced by CreateForeignTable(), get_connect_string(), has_server_privilege_id(), has_server_privilege_id_id(), has_server_privilege_id_name(), has_server_privilege_name(), has_server_privilege_name_id(), has_server_privilege_name_name(), ImportForeignSchema(), and user_mapping_ddl_aclcheck().

4795 {
4796  if (pg_foreign_server_aclmask(srv_oid, roleid, mode, ACLMASK_ANY) != 0)
4797  return ACLCHECK_OK;
4798  else
4799  return ACLCHECK_NO_PRIV;
4800 }
static PgChecksumMode mode
Definition: pg_checksums.c:63
AclMode pg_foreign_server_aclmask(Oid srv_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4406

◆ pg_foreign_server_aclmask()

AclMode pg_foreign_server_aclmask ( Oid  srv_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 4406 of file aclchk.c.

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

Referenced by pg_aclmask(), and pg_foreign_server_aclcheck().

4408 {
4409  AclMode result;
4410  HeapTuple tuple;
4411  Datum aclDatum;
4412  bool isNull;
4413  Acl *acl;
4414  Oid ownerId;
4415 
4416  Form_pg_foreign_server srvForm;
4417 
4418  /* Bypass permission checks for superusers */
4419  if (superuser_arg(roleid))
4420  return mask;
4421 
4422  /*
4423  * Must get the FDW's tuple from pg_foreign_data_wrapper
4424  */
4426  if (!HeapTupleIsValid(tuple))
4427  ereport(ERROR,
4428  (errcode(ERRCODE_UNDEFINED_OBJECT),
4429  errmsg("foreign server with OID %u does not exist",
4430  srv_oid)));
4431  srvForm = (Form_pg_foreign_server) GETSTRUCT(tuple);
4432 
4433  /*
4434  * Normal case: get the foreign server's ACL from pg_foreign_server
4435  */
4436  ownerId = srvForm->srvowner;
4437 
4438  aclDatum = SysCacheGetAttr(FOREIGNSERVEROID, tuple,
4439  Anum_pg_foreign_server_srvacl, &isNull);
4440  if (isNull)
4441  {
4442  /* No ACL, so build default ACL */
4443  acl = acldefault(OBJECT_FOREIGN_SERVER, ownerId);
4444  aclDatum = (Datum) 0;
4445  }
4446  else
4447  {
4448  /* detoast rel's ACL if necessary */
4449  acl = DatumGetAclP(aclDatum);
4450  }
4451 
4452  result = aclmask(acl, roleid, ownerId, mask, how);
4453 
4454  /* if we have a detoasted copy, free it */
4455  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
4456  pfree(acl);
4457 
4458  ReleaseSysCache(tuple);
4459 
4460  return result;
4461 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define DatumGetAclP(X)
Definition: acl.h:120
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1306
int errcode(int sqlerrcode)
Definition: elog.c:698
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
unsigned int Oid
Definition: postgres_ext.h:31
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
char * Pointer
Definition: c.h:418
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
FormData_pg_foreign_server * Form_pg_foreign_server
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define DatumGetPointer(X)
Definition: postgres.h:593
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_foreign_server_ownercheck()

bool pg_foreign_server_ownercheck ( Oid  srv_oid,
Oid  roleid 
)

Definition at line 5182 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().

5183 {
5184  HeapTuple tuple;
5185  Oid ownerId;
5186 
5187  /* Superusers bypass all permission checking. */
5188  if (superuser_arg(roleid))
5189  return true;
5190 
5192  if (!HeapTupleIsValid(tuple))
5193  ereport(ERROR,
5194  (errcode(ERRCODE_UNDEFINED_OBJECT),
5195  errmsg("foreign server with OID %u does not exist",
5196  srv_oid)));
5197 
5198  ownerId = ((Form_pg_foreign_server) GETSTRUCT(tuple))->srvowner;
5199 
5200  ReleaseSysCache(tuple);
5201 
5202  return has_privs_of_role(roleid, ownerId);
5203 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
FormData_pg_foreign_server * Form_pg_foreign_server
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_language_aclcheck()

AclResult pg_language_aclcheck ( Oid  lang_oid,
Oid  roleid,
AclMode  mode 
)

Definition at line 4730 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().

4731 {
4732  if (pg_language_aclmask(lang_oid, roleid, mode, ACLMASK_ANY) != 0)
4733  return ACLCHECK_OK;
4734  else
4735  return ACLCHECK_NO_PRIV;
4736 }
static PgChecksumMode mode
Definition: pg_checksums.c:63
AclMode pg_language_aclmask(Oid lang_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4059

◆ pg_language_aclmask()

AclMode pg_language_aclmask ( Oid  lang_oid,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)

Definition at line 4059 of file aclchk.c.

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

Referenced by pg_aclmask(), and pg_language_aclcheck().

4061 {
4062  AclMode result;
4063  HeapTuple tuple;
4064  Datum aclDatum;
4065  bool isNull;
4066  Acl *acl;
4067  Oid ownerId;
4068 
4069  /* Superusers bypass all permission checking. */
4070  if (superuser_arg(roleid))
4071  return mask;
4072 
4073  /*
4074  * Get the language's ACL from pg_language
4075  */
4076  tuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(lang_oid));
4077  if (!HeapTupleIsValid(tuple))
4078  ereport(ERROR,
4079  (errcode(ERRCODE_UNDEFINED_OBJECT),
4080  errmsg("language with OID %u does not exist", lang_oid)));
4081 
4082  ownerId = ((Form_pg_language) GETSTRUCT(tuple))->lanowner;
4083 
4084  aclDatum = SysCacheGetAttr(LANGOID, tuple, Anum_pg_language_lanacl,
4085  &isNull);
4086  if (isNull)
4087  {
4088  /* No ACL, so build default ACL */
4089  acl = acldefault(OBJECT_LANGUAGE, ownerId);
4090  aclDatum = (Datum) 0;
4091  }
4092  else
4093  {
4094  /* detoast ACL if necessary */
4095  acl = DatumGetAclP(aclDatum);
4096  }
4097 
4098  result = aclmask(acl, roleid, ownerId, mask, how);
4099 
4100  /* if we have a detoasted copy, free it */
4101  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
4102  pfree(acl);
4103 
4104  ReleaseSysCache(tuple);
4105 
4106  return result;
4107 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define DatumGetAclP(X)
Definition: acl.h:120
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1306
int errcode(int sqlerrcode)
Definition: elog.c:698
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
unsigned int Oid
Definition: postgres_ext.h:31
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
char * Pointer
Definition: c.h:418
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define DatumGetPointer(X)
Definition: postgres.h:593
FormData_pg_language * Form_pg_language
Definition: pg_language.h:65
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_language_ownercheck()

bool pg_language_ownercheck ( Oid  lan_oid,
Oid  roleid 
)

Definition at line 4922 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 CreateProceduralLanguage().

4923 {
4924  HeapTuple tuple;
4925  Oid ownerId;
4926 
4927  /* Superusers bypass all permission checking. */
4928  if (superuser_arg(roleid))
4929  return true;
4930 
4931  tuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(lan_oid));
4932  if (!HeapTupleIsValid(tuple))
4933  ereport(ERROR,
4934  (errcode(ERRCODE_UNDEFINED_FUNCTION),
4935  errmsg("language with OID %u does not exist", lan_oid)));
4936 
4937  ownerId = ((Form_pg_language) GETSTRUCT(tuple))->lanowner;
4938 
4939  ReleaseSysCache(tuple);
4940 
4941  return has_privs_of_role(roleid, ownerId);
4942 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
int errcode(int sqlerrcode)