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 3313 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(), 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(), CreateTrigger(), 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(), 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(), sanity_check_relation(), transformTableLikeClause(), truncate_check_perms(), TypeCreate(), user_mapping_ddl_aclcheck(), ValidateJoinEstimator(), and ValidateRestrictionEstimator().

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

3598 {
3599  switch (aclerr)
3600  {
3601  case ACLCHECK_OK:
3602  /* no error, so return to caller */
3603  break;
3604  case ACLCHECK_NO_PRIV:
3605  ereport(ERROR,
3606  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3607  errmsg("permission denied for column \"%s\" of relation \"%s\"",
3608  colname, objectname)));
3609  break;
3610  case ACLCHECK_NOT_OWNER:
3611  /* relation msg is OK since columns don't have separate owners */
3612  aclcheck_error(aclerr, objtype, objectname);
3613  break;
3614  default:
3615  elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
3616  break;
3617  }
3618 }
int errcode(int sqlerrcode)
Definition: elog.c:698
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3313
#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 3626 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().

3627 {
3628  Oid element_type = get_element_type(typeOid);
3629 
3630  aclcheck_error(aclerr, OBJECT_TYPE, format_type_be(element_type ? element_type : typeOid));
3631 }
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:3313

◆ 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:217
#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 904 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, get_rolespec_oid(), gettext_noop, GetUserId(), InternalDefaultACL::grant_option, GrantStmt::grant_option, InternalDefaultACL::grantees, GrantStmt::grantees, InternalDefaultACL::is_grant, GrantStmt::is_grant, lappend_oid(), lfirst, DefElem::location, NIL, OBJECT_FUNCTION, OBJECT_PROCEDURE, OBJECT_ROUTINE, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_TABLE, OBJECT_TYPE, InternalDefaultACL::objtype, GrantStmt::objtype, AlterDefaultPrivilegesStmt::options, parser_errposition(), AccessPriv::priv_name, privilege_to_string(), InternalDefaultACL::privileges, GrantStmt::privileges, InternalDefaultACL::roleid, ROLESPEC_PUBLIC, RoleSpec::roletype, SetDefaultACLsInSchemas(), and string_to_privilege().

Referenced by ProcessUtilitySlow().

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

◆ ExecuteGrantStmt()

void ExecuteGrantStmt ( GrantStmt stmt)

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

360 {
361  InternalGrant istmt;
362  ListCell *cell;
363  const char *errormsg;
364  AclMode all_privileges;
365 
366  if (stmt->grantor)
367  {
368  Oid grantor;
369 
370  grantor = get_rolespec_oid(stmt->grantor, false);
371 
372  /*
373  * Currently, this clause is only for SQL compatibility, not very
374  * interesting otherwise.
375  */
376  if (grantor != GetUserId())
377  ereport(ERROR,
378  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
379  errmsg("grantor must be current user")));
380  }
381 
382  /*
383  * Turn the regular GrantStmt into the InternalGrant form.
384  */
385  istmt.is_grant = stmt->is_grant;
386  istmt.objtype = stmt->objtype;
387 
388  /* Collect the OIDs of the target objects */
389  switch (stmt->targtype)
390  {
391  case ACL_TARGET_OBJECT:
392  istmt.objects = objectNamesToOids(stmt->objtype, stmt->objects);
393  break;
395  istmt.objects = objectsInSchemaToOids(stmt->objtype, stmt->objects);
396  break;
397  /* ACL_TARGET_DEFAULTS should not be seen here */
398  default:
399  elog(ERROR, "unrecognized GrantStmt.targtype: %d",
400  (int) stmt->targtype);
401  }
402 
403  /* all_privs to be filled below */
404  /* privileges to be filled below */
405  istmt.col_privs = NIL; /* may get filled below */
406  istmt.grantees = NIL; /* filled below */
407  istmt.grant_option = stmt->grant_option;
408  istmt.behavior = stmt->behavior;
409 
410  /*
411  * Convert the RoleSpec list into an Oid list. Note that at this point we
412  * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
413  * there shouldn't be any additional work needed to support this case.
414  */
415  foreach(cell, stmt->grantees)
416  {
417  RoleSpec *grantee = (RoleSpec *) lfirst(cell);
418  Oid grantee_uid;
419 
420  switch (grantee->roletype)
421  {
422  case ROLESPEC_PUBLIC:
423  grantee_uid = ACL_ID_PUBLIC;
424  break;
425  default:
426  grantee_uid = get_rolespec_oid(grantee, false);
427  break;
428  }
429  istmt.grantees = lappend_oid(istmt.grantees, grantee_uid);
430  }
431 
432  /*
433  * Convert stmt->privileges, a list of AccessPriv nodes, into an AclMode
434  * bitmask. Note: objtype can't be OBJECT_COLUMN.
435  */
436  switch (stmt->objtype)
437  {
438  case OBJECT_TABLE:
439 
440  /*
441  * Because this might be a sequence, we test both relation and
442  * sequence bits, and later do a more limited test when we know
443  * the object type.
444  */
446  errormsg = gettext_noop("invalid privilege type %s for relation");
447  break;
448  case OBJECT_SEQUENCE:
449  all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
450  errormsg = gettext_noop("invalid privilege type %s for sequence");
451  break;
452  case OBJECT_DATABASE:
453  all_privileges = ACL_ALL_RIGHTS_DATABASE;
454  errormsg = gettext_noop("invalid privilege type %s for database");
455  break;
456  case OBJECT_DOMAIN:
457  all_privileges = ACL_ALL_RIGHTS_TYPE;
458  errormsg = gettext_noop("invalid privilege type %s for domain");
459  break;
460  case OBJECT_FUNCTION:
461  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
462  errormsg = gettext_noop("invalid privilege type %s for function");
463  break;
464  case OBJECT_LANGUAGE:
465  all_privileges = ACL_ALL_RIGHTS_LANGUAGE;
466  errormsg = gettext_noop("invalid privilege type %s for language");
467  break;
468  case OBJECT_LARGEOBJECT:
469  all_privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
470  errormsg = gettext_noop("invalid privilege type %s for large object");
471  break;
472  case OBJECT_SCHEMA:
473  all_privileges = ACL_ALL_RIGHTS_SCHEMA;
474  errormsg = gettext_noop("invalid privilege type %s for schema");
475  break;
476  case OBJECT_PROCEDURE:
477  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
478  errormsg = gettext_noop("invalid privilege type %s for procedure");
479  break;
480  case OBJECT_ROUTINE:
481  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
482  errormsg = gettext_noop("invalid privilege type %s for routine");
483  break;
484  case OBJECT_TABLESPACE:
485  all_privileges = ACL_ALL_RIGHTS_TABLESPACE;
486  errormsg = gettext_noop("invalid privilege type %s for tablespace");
487  break;
488  case OBJECT_TYPE:
489  all_privileges = ACL_ALL_RIGHTS_TYPE;
490  errormsg = gettext_noop("invalid privilege type %s for type");
491  break;
492  case OBJECT_FDW:
493  all_privileges = ACL_ALL_RIGHTS_FDW;
494  errormsg = gettext_noop("invalid privilege type %s for foreign-data wrapper");
495  break;
497  all_privileges = ACL_ALL_RIGHTS_FOREIGN_SERVER;
498  errormsg = gettext_noop("invalid privilege type %s for foreign server");
499  break;
500  default:
501  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
502  (int) stmt->objtype);
503  /* keep compiler quiet */
504  all_privileges = ACL_NO_RIGHTS;
505  errormsg = NULL;
506  }
507 
508  if (stmt->privileges == NIL)
509  {
510  istmt.all_privs = true;
511 
512  /*
513  * will be turned into ACL_ALL_RIGHTS_* by the internal routines
514  * depending on the object type
515  */
516  istmt.privileges = ACL_NO_RIGHTS;
517  }
518  else
519  {
520  istmt.all_privs = false;
521  istmt.privileges = ACL_NO_RIGHTS;
522 
523  foreach(cell, stmt->privileges)
524  {
525  AccessPriv *privnode = (AccessPriv *) lfirst(cell);
526  AclMode priv;
527 
528  /*
529  * If it's a column-level specification, we just set it aside in
530  * col_privs for the moment; but insist it's for a relation.
531  */
532  if (privnode->cols)
533  {
534  if (stmt->objtype != OBJECT_TABLE)
535  ereport(ERROR,
536  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
537  errmsg("column privileges are only valid for relations")));
538  istmt.col_privs = lappend(istmt.col_privs, privnode);
539  continue;
540  }
541 
542  if (privnode->priv_name == NULL) /* parser mistake? */
543  elog(ERROR, "AccessPriv node must specify privilege or columns");
544  priv = string_to_privilege(privnode->priv_name);
545 
546  if (priv & ~((AclMode) all_privileges))
547  ereport(ERROR,
548  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
549  errmsg(errormsg, privilege_to_string(priv))));
550 
551  istmt.privileges |= priv;
552  }
553  }
554 
555  ExecGrantStmt_oids(&istmt);
556 }
#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:3235
AclMode privileges
bool grant_option
Definition: parsenodes.h:2012
#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:2043
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:2004
static void ExecGrantStmt_oids(InternalGrant *istmt)
Definition: aclchk.c:564
static List * objectNamesToOids(ObjectType objtype, List *objnames)
Definition: aclchk.c:628
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:2009
static List * objectsInSchemaToOids(ObjectType objtype, List *nspnames)
Definition: aclchk.c:777
#define ereport(elevel,...)
Definition: elog.h:157
RoleSpec * grantor
Definition: parsenodes.h:2013
#define ACL_ALL_RIGHTS_LARGEOBJECT
Definition: acl.h:164
DropBehavior behavior
DropBehavior behavior
Definition: parsenodes.h:2014
#define lfirst(lc)
Definition: pg_list.h:169
List * objects
Definition: parsenodes.h:2007
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:2006
#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:3272
List * grantees
Definition: parsenodes.h:2011
GrantTargetType targtype
Definition: parsenodes.h:2005
char * priv_name
Definition: parsenodes.h:2042

◆ 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 5526 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().

5527 {
5528  Acl *result;
5529  Acl *glob_acl;
5530  Acl *schema_acl;
5531  Acl *def_acl;
5532  char defaclobjtype;
5533 
5534  /*
5535  * Use NULL during bootstrap, since pg_default_acl probably isn't there
5536  * yet.
5537  */
5539  return NULL;
5540 
5541  /* Check if object type is supported in pg_default_acl */
5542  switch (objtype)
5543  {
5544  case OBJECT_TABLE:
5545  defaclobjtype = DEFACLOBJ_RELATION;
5546  break;
5547 
5548  case OBJECT_SEQUENCE:
5549  defaclobjtype = DEFACLOBJ_SEQUENCE;
5550  break;
5551 
5552  case OBJECT_FUNCTION:
5553  defaclobjtype = DEFACLOBJ_FUNCTION;
5554  break;
5555 
5556  case OBJECT_TYPE:
5557  defaclobjtype = DEFACLOBJ_TYPE;
5558  break;
5559 
5560  case OBJECT_SCHEMA:
5561  defaclobjtype = DEFACLOBJ_NAMESPACE;
5562  break;
5563 
5564  default:
5565  return NULL;
5566  }
5567 
5568  /* Look up the relevant pg_default_acl entries */
5569  glob_acl = get_default_acl_internal(ownerId, InvalidOid, defaclobjtype);
5570  schema_acl = get_default_acl_internal(ownerId, nsp_oid, defaclobjtype);
5571 
5572  /* Quick out if neither entry exists */
5573  if (glob_acl == NULL && schema_acl == NULL)
5574  return NULL;
5575 
5576  /* We need to know the hard-wired default value, too */
5577  def_acl = acldefault(objtype, ownerId);
5578 
5579  /* If there's no global entry, substitute the hard-wired default */
5580  if (glob_acl == NULL)
5581  glob_acl = def_acl;
5582 
5583  /* Merge in any per-schema privileges */
5584  result = aclmerge(glob_acl, schema_acl, ownerId);
5585 
5586  /*
5587  * For efficiency, we want to return NULL if the result equals default.
5588  * This requires sorting both arrays to get an accurate comparison.
5589  */
5590  aclitemsort(result);
5591  aclitemsort(def_acl);
5592  if (aclequal(result, def_acl))
5593  result = NULL;
5594 
5595  return result;
5596 }
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:5491
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:394

◆ has_bypassrls_privilege()

bool has_bypassrls_privilege ( Oid  roleid)

Definition at line 5467 of file aclchk.c.

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

Referenced by check_enable_rls(), and RI_Initial_Check().

5468 {
5469  bool result = false;
5470  HeapTuple utup;
5471 
5472  /* Superusers bypass all permission checking. */
5473  if (superuser_arg(roleid))
5474  return true;
5475 
5476  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5477  if (HeapTupleIsValid(utup))
5478  {
5479  result = ((Form_pg_authid) GETSTRUCT(utup))->rolbypassrls;
5480  ReleaseSysCache(utup);
5481  }
5482  return result;
5483 }
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 5448 of file aclchk.c.

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

Referenced by check_object_ownership(), and have_createrole_privilege().

5449 {
5450  bool result = false;
5451  HeapTuple utup;
5452 
5453  /* Superusers bypass all permission checking. */
5454  if (superuser_arg(roleid))
5455  return true;
5456 
5457  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5458  if (HeapTupleIsValid(utup))
5459  {
5460  result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreaterole;
5461  ReleaseSysCache(utup);
5462  }
5463  return result;
5464 }
#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:1434
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:394

◆ 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 4556 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().

4558 {
4559  return pg_attribute_aclcheck_ext(table_oid, attnum, roleid, mode, NULL);
4560 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
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:4570

◆ pg_attribute_aclcheck_all()

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

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

4602 {
4603  AclResult result;
4604  HeapTuple classTuple;
4605  Form_pg_class classForm;
4606  AttrNumber nattrs;
4607  AttrNumber curr_att;
4608 
4609  /*
4610  * Must fetch pg_class row to check number of attributes. As in
4611  * pg_attribute_aclmask, we prefer to return "no privileges" instead of
4612  * throwing an error if we get any unexpected lookup errors.
4613  */
4614  classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
4615  if (!HeapTupleIsValid(classTuple))
4616  return ACLCHECK_NO_PRIV;
4617  classForm = (Form_pg_class) GETSTRUCT(classTuple);
4618 
4619  nattrs = classForm->relnatts;
4620 
4621  ReleaseSysCache(classTuple);
4622 
4623  /*
4624  * Initialize result in case there are no non-dropped columns. We want to
4625  * report failure in such cases for either value of 'how'.
4626  */
4627  result = ACLCHECK_NO_PRIV;
4628 
4629  for (curr_att = 1; curr_att <= nattrs; curr_att++)
4630  {
4631  HeapTuple attTuple;
4632  AclMode attmask;
4633 
4634  attTuple = SearchSysCache2(ATTNUM,
4635  ObjectIdGetDatum(table_oid),
4636  Int16GetDatum(curr_att));
4637  if (!HeapTupleIsValid(attTuple))
4638  continue;
4639 
4640  /* ignore dropped columns */
4641  if (((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped)
4642  {
4643  ReleaseSysCache(attTuple);
4644  continue;
4645  }
4646 
4647  /*
4648  * Here we hard-wire knowledge that the default ACL for a column
4649  * grants no privileges, so that we can fall out quickly in the very
4650  * common case where attacl is null.
4651  */
4652  if (heap_attisnull(attTuple, Anum_pg_attribute_attacl, NULL))
4653  attmask = 0;
4654  else
4655  attmask = pg_attribute_aclmask(table_oid, curr_att, roleid,
4656  mode, ACLMASK_ANY);
4657 
4658  ReleaseSysCache(attTuple);
4659 
4660  if (attmask != 0)
4661  {
4662  result = ACLCHECK_OK;
4663  if (how == ACLMASK_ANY)
4664  break; /* succeed on any success */
4665  }
4666  else
4667  {
4668  result = ACLCHECK_NO_PRIV;
4669  if (how == ACLMASK_ALL)
4670  break; /* fail on any failure */
4671  }
4672  }
4673 
4674  return result;
4675 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
#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:3706
#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:203
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 4570 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().

4572 {
4573  if (pg_attribute_aclmask_ext(table_oid, attnum, roleid, mode,
4574  ACLMASK_ANY, is_missing) != 0)
4575  return ACLCHECK_OK;
4576  else
4577  return ACLCHECK_NO_PRIV;
4578 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
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:3720

◆ pg_attribute_aclmask()

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

Definition at line 3706 of file aclchk.c.

References pg_attribute_aclmask_ext().

Referenced by pg_aclmask(), and pg_attribute_aclcheck_all().

3708 {
3709  return pg_attribute_aclmask_ext(table_oid, attnum, roleid,
3710  mask, how, NULL);
3711 }
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:3720

◆ 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 3720 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().

3722 {
3723  AclMode result;
3724  HeapTuple classTuple;
3725  HeapTuple attTuple;
3726  Form_pg_class classForm;
3727  Form_pg_attribute attributeForm;
3728  Datum aclDatum;
3729  bool isNull;
3730  Acl *acl;
3731  Oid ownerId;
3732 
3733  /*
3734  * First, get the column's ACL from its pg_attribute entry
3735  */
3736  attTuple = SearchSysCache2(ATTNUM,
3737  ObjectIdGetDatum(table_oid),
3739  if (!HeapTupleIsValid(attTuple))
3740  {
3741  if (is_missing != NULL)
3742  {
3743  /* return "no privileges" instead of throwing an error */
3744  *is_missing = true;
3745  return 0;
3746  }
3747  else
3748  ereport(ERROR,
3749  (errcode(ERRCODE_UNDEFINED_COLUMN),
3750  errmsg("attribute %d of relation with OID %u does not exist",
3751  attnum, table_oid)));
3752  }
3753 
3754  attributeForm = (Form_pg_attribute) GETSTRUCT(attTuple);
3755 
3756  /* Check dropped columns, too */
3757  if (attributeForm->attisdropped)
3758  {
3759  if (is_missing != NULL)
3760  {
3761  /* return "no privileges" instead of throwing an error */
3762  *is_missing = true;
3763  ReleaseSysCache(attTuple);
3764  return 0;
3765  }
3766  else
3767  ereport(ERROR,
3768  (errcode(ERRCODE_UNDEFINED_COLUMN),
3769  errmsg("attribute %d of relation with OID %u does not exist",
3770  attnum, table_oid)));
3771  }
3772 
3773  aclDatum = SysCacheGetAttr(ATTNUM, attTuple, Anum_pg_attribute_attacl,
3774  &isNull);
3775 
3776  /*
3777  * Here we hard-wire knowledge that the default ACL for a column grants no
3778  * privileges, so that we can fall out quickly in the very common case
3779  * where attacl is null.
3780  */
3781  if (isNull)
3782  {
3783  ReleaseSysCache(attTuple);
3784  return 0;
3785  }
3786 
3787  /*
3788  * Must get the relation's ownerId from pg_class. Since we already found
3789  * a pg_attribute entry, the only likely reason for this to fail is that a
3790  * concurrent DROP of the relation committed since then (which could only
3791  * happen if we don't have lock on the relation). We prefer to report "no
3792  * privileges" rather than failing in such a case, so as to avoid unwanted
3793  * failures in has_column_privilege() tests.
3794  */
3795  classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
3796  if (!HeapTupleIsValid(classTuple))
3797  {
3798  ReleaseSysCache(attTuple);
3799  return 0;
3800  }
3801  classForm = (Form_pg_class) GETSTRUCT(classTuple);
3802 
3803  ownerId = classForm->relowner;
3804 
3805  ReleaseSysCache(classTuple);
3806 
3807  /* detoast column's ACL if necessary */
3808  acl = DatumGetAclP(aclDatum);
3809 
3810  result = aclmask(acl, roleid, ownerId, mask, how);
3811 
3812  /* if we have a detoasted copy, free it */
3813  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3814  pfree(acl);
3815 
3816  ReleaseSysCache(attTuple);
3817 
3818  return result;
3819 }
#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:203
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 4697 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().

4699 {
4700  if (pg_class_aclmask_ext(table_oid, roleid, mode,
4701  ACLMASK_ANY, is_missing) != 0)
4702  return ACLCHECK_OK;
4703  else
4704  return ACLCHECK_NO_PRIV;
4705 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
AclMode pg_class_aclmask_ext(Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how, bool *is_missing)
Definition: aclchk.c:3838

◆ pg_class_aclmask()

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

Definition at line 3825 of file aclchk.c.

References pg_class_aclmask_ext().

Referenced by ExecCheckRTEPerms(), and pg_aclmask().

3827 {
3828  return pg_class_aclmask_ext(table_oid, roleid, mask, how, NULL);
3829 }
AclMode pg_class_aclmask_ext(Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how, bool *is_missing)
Definition: aclchk.c:3838

◆ pg_class_aclmask_ext()

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

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

3840 {
3841  AclMode result;
3842  HeapTuple tuple;
3843  Form_pg_class classForm;
3844  Datum aclDatum;
3845  bool isNull;
3846  Acl *acl;
3847  Oid ownerId;
3848 
3849  /*
3850  * Must get the relation's tuple from pg_class
3851  */
3852  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
3853  if (!HeapTupleIsValid(tuple))
3854  {
3855  if (is_missing != NULL)
3856  {
3857  /* return "no privileges" instead of throwing an error */
3858  *is_missing = true;
3859  return 0;
3860  }
3861  else
3862  ereport(ERROR,
3864  errmsg("relation with OID %u does not exist",
3865  table_oid)));
3866  }
3867 
3868  classForm = (Form_pg_class) GETSTRUCT(tuple);
3869 
3870  /*
3871  * Deny anyone permission to update a system catalog unless
3872  * pg_authid.rolsuper is set.
3873  *
3874  * As of 7.4 we have some updatable system views; those shouldn't be
3875  * protected in this way. Assume the view rules can take care of
3876  * themselves. ACL_USAGE is if we ever have system sequences.
3877  */
3878  if ((mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE | ACL_USAGE)) &&
3879  IsSystemClass(table_oid, classForm) &&
3880  classForm->relkind != RELKIND_VIEW &&
3881  !superuser_arg(roleid))
3883 
3884  /*
3885  * Otherwise, superusers bypass all permission-checking.
3886  */
3887  if (superuser_arg(roleid))
3888  {
3889  ReleaseSysCache(tuple);
3890  return mask;
3891  }
3892 
3893  /*
3894  * Normal case: get the relation's ACL from pg_class
3895  */
3896  ownerId = classForm->relowner;
3897 
3898  aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
3899  &isNull);
3900  if (isNull)
3901  {
3902  /* No ACL, so build default ACL */
3903  switch (classForm->relkind)
3904  {
3905  case RELKIND_SEQUENCE:
3906  acl = acldefault(OBJECT_SEQUENCE, ownerId);
3907  break;
3908  default:
3909  acl = acldefault(OBJECT_TABLE, ownerId);
3910  break;
3911  }
3912  aclDatum = (Datum) 0;
3913  }
3914  else
3915  {
3916  /* detoast rel's ACL if necessary */
3917  acl = DatumGetAclP(aclDatum);
3918  }
3919 
3920  result = aclmask(acl, roleid, ownerId, mask, how);
3921 
3922  /* if we have a detoasted copy, free it */
3923  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3924  pfree(acl);
3925 
3926  ReleaseSysCache(tuple);
3927 
3928  /*
3929  * Check if ACL_SELECT is being checked and, if so, and not set already
3930  * as part of the result, then check if the user is a member of the
3931  * pg_read_all_data role, which allows read access to all relations.
3932  */
3933  if (mask & ACL_SELECT && !(result & ACL_SELECT) &&
3934  has_privs_of_role(roleid, ROLE_PG_READ_ALL_DATA))
3935  result |= ACL_SELECT;
3936 
3937  /*
3938  * Check if ACL_INSERT, ACL_UPDATE, or ACL_DELETE is being checked
3939  * and, if so, and not set already as part of the result, then check
3940  * if the user is a member of the pg_write_all_data role, which
3941  * allows INSERT/UPDATE/DELETE access to all relations (except
3942  * system catalogs, which requires superuser, see above).
3943  */
3944  if (mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE) &&
3945  !(result & (ACL_INSERT | ACL_UPDATE | ACL_DELETE)) &&
3946  has_privs_of_role(roleid, ROLE_PG_WRITE_ALL_DATA))
3947  result |= (mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE));
3948 
3949  return result;
3950 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:76
#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:85
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 4823 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(), MergeAttributes(), PublicationAddTables(), RangeVarCallbackForAlterRelation(), RangeVarCallbackForDropRelation(), RangeVarCallbackForPolicy(), RangeVarCallbackForReindexIndex(), RangeVarCallbackForRenameRule(), RangeVarCallbackForRenameTrigger(), RangeVarCallbackOwnsRelation(), RangeVarCallbackOwnsTable(), RangeVarGetAndCheckCreationNamespace(), ReindexMultipleTables(), RemoveRoleFromObjectPolicy(), renameatt_check(), RI_Initial_Check(), sanity_check_relation(), and vacuum_is_relation_owner().

4824 {
4825  HeapTuple tuple;
4826  Oid ownerId;
4827 
4828  /* Superusers bypass all permission checking. */
4829  if (superuser_arg(roleid))
4830  return true;
4831 
4832  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(class_oid));
4833  if (!HeapTupleIsValid(tuple))
4834  ereport(ERROR,
4836  errmsg("relation with OID %u does not exist", class_oid)));
4837 
4838  ownerId = ((Form_pg_class) GETSTRUCT(tuple))->relowner;
4839 
4840  ReleaseSysCache(tuple);
4841 
4842  return has_privs_of_role(roleid, ownerId);
4843 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:76
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 5267 of file aclchk.c.

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

Referenced by check_object_ownership().

5268 {
5269  HeapTuple tuple;
5270  Oid ownerId;
5271 
5272  /* Superusers bypass all permission checking. */
5273  if (superuser_arg(roleid))
5274  return true;
5275 
5276  tuple = SearchSysCache1(COLLOID, ObjectIdGetDatum(coll_oid));
5277  if (!HeapTupleIsValid(tuple))
5278  ereport(ERROR,
5279  (errcode(ERRCODE_UNDEFINED_OBJECT),
5280  errmsg("collation with OID %u does not exist", coll_oid)));
5281 
5282  ownerId = ((Form_pg_collation) GETSTRUCT(tuple))->collowner;
5283 
5284  ReleaseSysCache(tuple);
5285 
5286  return has_privs_of_role(roleid, ownerId);
5287 }
#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:51
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_conversion_ownercheck()

bool pg_conversion_ownercheck ( Oid  conv_oid,
Oid  roleid 
)

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

5294 {
5295  HeapTuple tuple;
5296  Oid ownerId;
5297 
5298  /* Superusers bypass all permission checking. */
5299  if (superuser_arg(roleid))
5300  return true;
5301 
5302  tuple = SearchSysCache1(CONVOID, ObjectIdGetDatum(conv_oid));
5303  if (!HeapTupleIsValid(tuple))
5304  ereport(ERROR,
5305  (errcode(ERRCODE_UNDEFINED_OBJECT),
5306  errmsg("conversion with OID %u does not exist", conv_oid)));
5307 
5308  ownerId = ((Form_pg_conversion) GETSTRUCT(tuple))->conowner;
5309 
5310  ReleaseSysCache(tuple);
5311 
5312  return has_privs_of_role(roleid, ownerId);
5313 }
#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 3956 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().

3958 {
3959  AclMode result;
3960  HeapTuple tuple;
3961  Datum aclDatum;
3962  bool isNull;
3963  Acl *acl;
3964  Oid ownerId;
3965 
3966  /* Superusers bypass all permission checking. */
3967  if (superuser_arg(roleid))
3968  return mask;
3969 
3970  /*
3971  * Get the database's ACL from pg_database
3972  */
3973  tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(db_oid));
3974  if (!HeapTupleIsValid(tuple))
3975  ereport(ERROR,
3976  (errcode(ERRCODE_UNDEFINED_DATABASE),
3977  errmsg("database with OID %u does not exist", db_oid)));
3978 
3979  ownerId = ((Form_pg_database) GETSTRUCT(tuple))->datdba;
3980 
3981  aclDatum = SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_datacl,
3982  &isNull);
3983  if (isNull)
3984  {
3985  /* No ACL, so build default ACL */
3986  acl = acldefault(OBJECT_DATABASE, ownerId);
3987  aclDatum = (Datum) 0;
3988  }
3989  else
3990  {
3991  /* detoast ACL if necessary */
3992  acl = DatumGetAclP(aclDatum);
3993  }
3994 
3995  result = aclmask(acl, roleid, ownerId, mask, how);
3996 
3997  /* if we have a detoasted copy, free it */
3998  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3999  pfree(acl);
4000 
4001  ReleaseSysCache(tuple);
4002 
4003  return result;
4004 }
#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 5241 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().

5242 {
5243  HeapTuple tuple;
5244  Oid dba;
5245 
5246  /* Superusers bypass all permission checking. */
5247  if (superuser_arg(roleid))
5248  return true;
5249 
5250  tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(db_oid));
5251  if (!HeapTupleIsValid(tuple))
5252  ereport(ERROR,
5253  (errcode(ERRCODE_UNDEFINED_DATABASE),
5254  errmsg("database with OID %u does not exist", db_oid)));
5255 
5256  dba = ((Form_pg_database) GETSTRUCT(tuple))->datdba;
5257 
5258  ReleaseSysCache(tuple);
5259 
5260  return has_privs_of_role(roleid, dba);
5261 }
#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 5214 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().

5215 {
5216  HeapTuple tuple;
5217  Oid ownerId;
5218 
5219  /* Superusers bypass all permission checking. */
5220  if (superuser_arg(roleid))
5221  return true;
5222 
5224  if (!HeapTupleIsValid(tuple))
5225  ereport(ERROR,
5226  (errcode(ERRCODE_UNDEFINED_OBJECT),
5227  errmsg("event trigger with OID %u does not exist",
5228  et_oid)));
5229 
5230  ownerId = ((Form_pg_event_trigger) GETSTRUCT(tuple))->evtowner;
5231 
5232  ReleaseSysCache(tuple);
5233 
5234  return has_privs_of_role(roleid, ownerId);
5235 }
#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 5319 of file aclchk.c.

References AccessShareLock, BTEqualStrategyNumber, ereport, errcode(), errmsg(), ERROR, ExtensionOidIndexId, 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().

5320 {
5321  Relation pg_extension;
5322  ScanKeyData entry[1];
5323  SysScanDesc scan;
5324  HeapTuple tuple;
5325  Oid ownerId;
5326 
5327  /* Superusers bypass all permission checking. */
5328  if (superuser_arg(roleid))
5329  return true;
5330 
5331  /* There's no syscache for pg_extension, so do it the hard way */
5332  pg_extension = table_open(ExtensionRelationId, AccessShareLock);
5333 
5334  ScanKeyInit(&entry[0],
5335  Anum_pg_extension_oid,
5336  BTEqualStrategyNumber, F_OIDEQ,
5337  ObjectIdGetDatum(ext_oid));
5338 
5339  scan = systable_beginscan(pg_extension,
5340  ExtensionOidIndexId, true,
5341  NULL, 1, entry);
5342 
5343  tuple = systable_getnext(scan);
5344  if (!HeapTupleIsValid(tuple))
5345  ereport(ERROR,
5346  (errcode(ERRCODE_UNDEFINED_OBJECT),
5347  errmsg("extension with OID %u does not exist", ext_oid)));
5348 
5349  ownerId = ((Form_pg_extension) GETSTRUCT(tuple))->extowner;
5350 
5351  systable_endscan(scan);
5352  table_close(pg_extension, AccessShareLock);
5353 
5354  return has_privs_of_role(roleid, ownerId);
5355 }
#define ExtensionOidIndexId
Definition: pg_extension.h:57
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 4349 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().

4351 {
4352  AclMode result;
4353  HeapTuple tuple;
4354  Datum aclDatum;
4355  bool isNull;
4356  Acl *acl;
4357  Oid ownerId;
4358 
4360 
4361  /* Bypass permission checks for superusers */
4362  if (superuser_arg(roleid))
4363  return mask;
4364 
4365  /*
4366  * Must get the FDW's tuple from pg_foreign_data_wrapper
4367  */
4369  if (!HeapTupleIsValid(tuple))
4370  ereport(ERROR,
4371  (errcode(ERRCODE_UNDEFINED_OBJECT),
4372  errmsg("foreign-data wrapper with OID %u does not exist",
4373  fdw_oid)));
4374  fdwForm = (Form_pg_foreign_data_wrapper) GETSTRUCT(tuple);
4375 
4376  /*
4377  * Normal case: get the FDW's ACL from pg_foreign_data_wrapper
4378  */
4379  ownerId = fdwForm->fdwowner;
4380 
4381  aclDatum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID, tuple,
4382  Anum_pg_foreign_data_wrapper_fdwacl, &isNull);
4383  if (isNull)
4384  {
4385  /* No ACL, so build default ACL */
4386  acl = acldefault(OBJECT_FDW, ownerId);
4387  aclDatum = (Datum) 0;
4388  }
4389  else
4390  {
4391  /* detoast rel's ACL if necessary */
4392  acl = DatumGetAclP(aclDatum);
4393  }
4394 
4395  result = aclmask(acl, roleid, ownerId, mask, how);
4396 
4397  /* if we have a detoasted copy, free it */
4398  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
4399  pfree(acl);
4400 
4401  ReleaseSysCache(tuple);
4402 
4403  return result;
4404 }
#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 5160 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().

5161 {
5162  HeapTuple tuple;
5163  Oid ownerId;
5164 
5165  /* Superusers bypass all permission checking. */
5166  if (superuser_arg(roleid))
5167  return true;
5168 
5170  if (!HeapTupleIsValid(tuple))
5171  ereport(ERROR,
5172  (errcode(ERRCODE_UNDEFINED_OBJECT),
5173  errmsg("foreign-data wrapper with OID %u does not exist",
5174  srv_oid)));
5175 
5176  ownerId = ((Form_pg_foreign_data_wrapper) GETSTRUCT(tuple))->fdwowner;
5177 
5178  ReleaseSysCache(tuple);
5179 
5180  return has_privs_of_role(roleid, ownerId);
5181 }
#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 4799 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().

4800 {
4801  if (pg_foreign_server_aclmask(srv_oid, roleid, mode, ACLMASK_ANY) != 0)
4802  return ACLCHECK_OK;
4803  else
4804  return ACLCHECK_NO_PRIV;
4805 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
AclMode pg_foreign_server_aclmask(Oid srv_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4411

◆ pg_foreign_server_aclmask()

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

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

4413 {
4414  AclMode result;
4415  HeapTuple tuple;
4416  Datum aclDatum;
4417  bool isNull;
4418  Acl *acl;
4419  Oid ownerId;
4420 
4421  Form_pg_foreign_server srvForm;
4422 
4423  /* Bypass permission checks for superusers */
4424  if (superuser_arg(roleid))
4425  return mask;
4426 
4427  /*
4428  * Must get the FDW's tuple from pg_foreign_data_wrapper
4429  */
4431  if (!HeapTupleIsValid(tuple))
4432  ereport(ERROR,
4433  (errcode(ERRCODE_UNDEFINED_OBJECT),
4434  errmsg("foreign server with OID %u does not exist",
4435  srv_oid)));
4436  srvForm = (Form_pg_foreign_server) GETSTRUCT(tuple);
4437 
4438  /*
4439  * Normal case: get the foreign server's ACL from pg_foreign_server
4440  */
4441  ownerId = srvForm->srvowner;
4442 
4443  aclDatum = SysCacheGetAttr(FOREIGNSERVEROID, tuple,
4444  Anum_pg_foreign_server_srvacl, &isNull);
4445  if (isNull)
4446  {
4447  /* No ACL, so build default ACL */
4448  acl = acldefault(OBJECT_FOREIGN_SERVER, ownerId);
4449  aclDatum = (Datum) 0;
4450  }
4451  else
4452  {
4453  /* detoast rel's ACL if necessary */
4454  acl = DatumGetAclP(aclDatum);
4455  }
4456 
4457  result = aclmask(acl, roleid, ownerId, mask, how);
4458 
4459  /* if we have a detoasted copy, free it */
4460  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
4461  pfree(acl);
4462 
4463  ReleaseSysCache(tuple);
4464 
4465  return result;
4466 }
#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 5187 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().

5188 {
5189  HeapTuple tuple;
5190  Oid ownerId;
5191 
5192  /* Superusers bypass all permission checking. */
5193  if (superuser_arg(roleid))
5194  return true;
5195 
5197  if (!HeapTupleIsValid(tuple))
5198  ereport(ERROR,
5199  (errcode(ERRCODE_UNDEFINED_OBJECT),
5200  errmsg("foreign server with OID %u does not exist",
5201  srv_oid)));
5202 
5203  ownerId = ((Form_pg_foreign_server) GETSTRUCT(tuple))->srvowner;
5204 
5205  ReleaseSysCache(tuple);
5206 
5207  return has_privs_of_role(roleid, ownerId);
5208 }
#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 4735 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().

4736 {
4737  if (pg_language_aclmask(lang_oid, roleid, mode, ACLMASK_ANY) != 0)
4738  return ACLCHECK_OK;
4739  else
4740  return ACLCHECK_NO_PRIV;
4741 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
AclMode pg_language_aclmask(Oid lang_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4064

◆ pg_language_aclmask()

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

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

4066 {
4067  AclMode result;
4068  HeapTuple tuple;
4069  Datum aclDatum;
4070  bool isNull;
4071  Acl *acl;
4072  Oid ownerId;
4073 
4074  /* Superusers bypass all permission checking. */
4075  if (superuser_arg(roleid))
4076  return mask;
4077 
4078  /*
4079  * Get the language's ACL from pg_language
4080  */
4081  tuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(lang_oid));
4082  if (!HeapTupleIsValid(tuple))
4083  ereport(ERROR,
4084  (errcode(ERRCODE_UNDEFINED_OBJECT),
4085  errmsg("language with OID %u does not exist", lang_oid)));
4086 
4087  ownerId = ((Form_pg_language) GETSTRUCT(tuple))->lanowner;
4088 
4089  aclDatum = SysCacheGetAttr(LANGOID, tuple, Anum_pg_language_lanacl,
4090  &isNull);
4091  if (isNull)
4092  {
4093  /* No ACL, so build default ACL */
4094  acl = acldefault(OBJECT_LANGUAGE, ownerId);
4095  aclDatum = (Datum) 0;
4096  }
4097  else
4098  {
4099  /* detoast ACL if necessary */
4100  acl = DatumGetAclP(aclDatum);
4101  }
4102 
4103  result = aclmask(acl, roleid, ownerId, mask, how);
4104 
4105  /* if we have a detoasted copy, free it */
4106  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
4107  pfree(acl);
4108 
4109  ReleaseSysCache(tuple);
4110 
4111  return result;
4112 }
#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 4927 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().

4928 {
4929  HeapTuple tuple;
4930  Oid ownerId;
4931 
4932  /* Superusers bypass all permission checking. */
4933  if (superuser_arg(roleid))
4934  return true;
4935 
4936  tuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(lan_oid));
4937  if (!HeapTupleIsValid(tuple))
4938  ereport(