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)
 
void RemoveDefaultACLById (Oid defaclOid)
 
AclMode pg_attribute_aclmask (Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_class_aclmask (Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_database_aclmask (Oid db_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_proc_aclmask (Oid proc_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_language_aclmask (Oid lang_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_largeobject_aclmask_snapshot (Oid lobj_oid, Oid roleid, AclMode mask, AclMaskHow how, Snapshot snapshot)
 
AclMode pg_namespace_aclmask (Oid nsp_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_tablespace_aclmask (Oid spc_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_foreign_data_wrapper_aclmask (Oid fdw_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_foreign_server_aclmask (Oid srv_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_type_aclmask (Oid type_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclResult pg_attribute_aclcheck (Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode)
 
AclResult pg_attribute_aclcheck_all (Oid table_oid, Oid roleid, AclMode mode, AclMaskHow how)
 
AclResult pg_class_aclcheck (Oid table_oid, Oid roleid, AclMode mode)
 
AclResult pg_database_aclcheck (Oid db_oid, Oid roleid, AclMode mode)
 
AclResult pg_proc_aclcheck (Oid proc_oid, Oid roleid, AclMode mode)
 
AclResult pg_language_aclcheck (Oid lang_oid, Oid roleid, AclMode mode)
 
AclResult pg_largeobject_aclcheck_snapshot (Oid lang_oid, Oid roleid, AclMode mode, Snapshot snapshot)
 
AclResult pg_namespace_aclcheck (Oid nsp_oid, Oid roleid, AclMode mode)
 
AclResult pg_tablespace_aclcheck (Oid spc_oid, Oid roleid, AclMode mode)
 
AclResult pg_foreign_data_wrapper_aclcheck (Oid fdw_oid, Oid roleid, AclMode mode)
 
AclResult pg_foreign_server_aclcheck (Oid srv_oid, Oid roleid, AclMode mode)
 
AclResult pg_type_aclcheck (Oid type_oid, Oid roleid, AclMode mode)
 
void aclcheck_error (AclResult aclerr, 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:72

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:72

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:72

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 3352 of file aclchk.c.

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

Referenced by aclcheck_error_col(), aclcheck_error_type(), AlterCollation(), AlterDatabase(), AlterDatabaseOwner(), AlterDatabaseSet(), AlterEventTrigger(), AlterEventTriggerOwner_internal(), AlterExtensionNamespace(), AlterForeignServer(), AlterForeignServerOwner_internal(), AlterFunction(), AlterObjectNamespace_internal(), AlterObjectOwner_internal(), AlterObjectRename_internal(), AlterOperator(), AlterOpFamilyAdd(), AlterPublication(), AlterPublicationOwner_internal(), AlterRoleSet(), AlterSchemaOwner_internal(), AlterStatistics(), AlterSubscription(), AlterSubscriptionOwner_internal(), AlterTableMoveAll(), AlterTableSpaceOptions(), AlterTSConfiguration(), AlterTSDictionary(), AlterTypeOwner(), ATExecChangeOwner(), ATPrepSetStatistics(), ATPrepSetTableSpace(), ATSimplePermissions(), brin_desummarize_range(), brin_summarize_range(), calculate_database_size(), calculate_tablespace_size(), call_pltcl_start_proc(), check_object_ownership(), check_temp_tablespaces(), checkFkeyPermissions(), CheckFunctionValidatorAccess(), compute_return_type(), create_proc_lang(), CreateConversionCommand(), createdb(), CreateForeignServer(), CreateForeignTable(), CreateFunction(), CreateProceduralLanguage(), CreatePublication(), CreateSchemaCommand(), CreateStatistics(), CreateTransform(), CreateTrigger(), currtid_byrelname(), currtid_byreloid(), DefineAggregate(), DefineCollation(), DefineDomain(), DefineEnum(), DefineIndex(), DefineOpClass(), DefineOperator(), DefineOpFamily(), DefineQueryRewrite(), DefineRange(), DefineRelation(), DefineTSConfiguration(), DefineTSDictionary(), DefineType(), dropdb(), DropSubscription(), DropTableSpace(), EnableDisableRule(), ExecAlterExtensionContentsStmt(), ExecAlterExtensionStmt(), ExecBuildGroupingEqual(), ExecCheckRTPerms(), ExecInitAgg(), ExecInitExprRec(), ExecInitFunc(), ExecInitWindowAgg(), 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(), LockTableRecurse(), 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(), ReindexMultipleTables(), renameatt_check(), RenameDatabase(), RenameSchema(), RenameTableSpace(), restrict_and_check_grant(), transformTableLikeClause(), truncate_check_rel(), TypeCreate(), user_mapping_ddl_aclcheck(), ValidateJoinEstimator(), and ValidateRestrictionEstimator().

3354 {
3355  switch (aclerr)
3356  {
3357  case ACLCHECK_OK:
3358  /* no error, so return to caller */
3359  break;
3360  case ACLCHECK_NO_PRIV:
3361  {
3362  const char *msg = "???";
3363 
3364  switch (objtype)
3365  {
3366  case OBJECT_AGGREGATE:
3367  msg = gettext_noop("permission denied for aggregate %s");
3368  break;
3369  case OBJECT_COLLATION:
3370  msg = gettext_noop("permission denied for collation %s");
3371  break;
3372  case OBJECT_COLUMN:
3373  msg = gettext_noop("permission denied for column %s");
3374  break;
3375  case OBJECT_CONVERSION:
3376  msg = gettext_noop("permission denied for conversion %s");
3377  break;
3378  case OBJECT_DATABASE:
3379  msg = gettext_noop("permission denied for database %s");
3380  break;
3381  case OBJECT_DOMAIN:
3382  msg = gettext_noop("permission denied for domain %s");
3383  break;
3384  case OBJECT_EVENT_TRIGGER:
3385  msg = gettext_noop("permission denied for event trigger %s");
3386  break;
3387  case OBJECT_EXTENSION:
3388  msg = gettext_noop("permission denied for extension %s");
3389  break;
3390  case OBJECT_FDW:
3391  msg = gettext_noop("permission denied for foreign-data wrapper %s");
3392  break;
3393  case OBJECT_FOREIGN_SERVER:
3394  msg = gettext_noop("permission denied for foreign server %s");
3395  break;
3396  case OBJECT_FOREIGN_TABLE:
3397  msg = gettext_noop("permission denied for foreign table %s");
3398  break;
3399  case OBJECT_FUNCTION:
3400  msg = gettext_noop("permission denied for function %s");
3401  break;
3402  case OBJECT_INDEX:
3403  msg = gettext_noop("permission denied for index %s");
3404  break;
3405  case OBJECT_LANGUAGE:
3406  msg = gettext_noop("permission denied for language %s");
3407  break;
3408  case OBJECT_LARGEOBJECT:
3409  msg = gettext_noop("permission denied for large object %s");
3410  break;
3411  case OBJECT_MATVIEW:
3412  msg = gettext_noop("permission denied for materialized view %s");
3413  break;
3414  case OBJECT_OPCLASS:
3415  msg = gettext_noop("permission denied for operator class %s");
3416  break;
3417  case OBJECT_OPERATOR:
3418  msg = gettext_noop("permission denied for operator %s");
3419  break;
3420  case OBJECT_OPFAMILY:
3421  msg = gettext_noop("permission denied for operator family %s");
3422  break;
3423  case OBJECT_POLICY:
3424  msg = gettext_noop("permission denied for policy %s");
3425  break;
3426  case OBJECT_PROCEDURE:
3427  msg = gettext_noop("permission denied for procedure %s");
3428  break;
3429  case OBJECT_PUBLICATION:
3430  msg = gettext_noop("permission denied for publication %s");
3431  break;
3432  case OBJECT_ROUTINE:
3433  msg = gettext_noop("permission denied for routine %s");
3434  break;
3435  case OBJECT_SCHEMA:
3436  msg = gettext_noop("permission denied for schema %s");
3437  break;
3438  case OBJECT_SEQUENCE:
3439  msg = gettext_noop("permission denied for sequence %s");
3440  break;
3441  case OBJECT_STATISTIC_EXT:
3442  msg = gettext_noop("permission denied for statistics object %s");
3443  break;
3444  case OBJECT_SUBSCRIPTION:
3445  msg = gettext_noop("permission denied for subscription %s");
3446  break;
3447  case OBJECT_TABLE:
3448  msg = gettext_noop("permission denied for table %s");
3449  break;
3450  case OBJECT_TABLESPACE:
3451  msg = gettext_noop("permission denied for tablespace %s");
3452  break;
3454  msg = gettext_noop("permission denied for text search configuration %s");
3455  break;
3456  case OBJECT_TSDICTIONARY:
3457  msg = gettext_noop("permission denied for text search dictionary %s");
3458  break;
3459  case OBJECT_TYPE:
3460  msg = gettext_noop("permission denied for type %s");
3461  break;
3462  case OBJECT_VIEW:
3463  msg = gettext_noop("permission denied for view %s");
3464  break;
3465  /* these currently aren't used */
3466  case OBJECT_ACCESS_METHOD:
3467  case OBJECT_AMOP:
3468  case OBJECT_AMPROC:
3469  case OBJECT_ATTRIBUTE:
3470  case OBJECT_CAST:
3471  case OBJECT_DEFAULT:
3472  case OBJECT_DEFACL:
3473  case OBJECT_DOMCONSTRAINT:
3475  case OBJECT_ROLE:
3476  case OBJECT_RULE:
3477  case OBJECT_TABCONSTRAINT:
3478  case OBJECT_TRANSFORM:
3479  case OBJECT_TRIGGER:
3480  case OBJECT_TSPARSER:
3481  case OBJECT_TSTEMPLATE:
3482  case OBJECT_USER_MAPPING:
3483  elog(ERROR, "unsupported object type %d", objtype);
3484  }
3485 
3486  ereport(ERROR,
3487  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3488  errmsg(msg, objectname)));
3489  break;
3490  }
3491  case ACLCHECK_NOT_OWNER:
3492  {
3493  const char *msg = "???";
3494 
3495  switch (objtype)
3496  {
3497  case OBJECT_AGGREGATE:
3498  msg = gettext_noop("must be owner of aggregate %s");
3499  break;
3500  case OBJECT_COLLATION:
3501  msg = gettext_noop("must be owner of collation %s");
3502  break;
3503  case OBJECT_CONVERSION:
3504  msg = gettext_noop("must be owner of conversion %s");
3505  break;
3506  case OBJECT_DATABASE:
3507  msg = gettext_noop("must be owner of database %s");
3508  break;
3509  case OBJECT_DOMAIN:
3510  msg = gettext_noop("must be owner of domain %s");
3511  break;
3512  case OBJECT_EVENT_TRIGGER:
3513  msg = gettext_noop("must be owner of event trigger %s");
3514  break;
3515  case OBJECT_EXTENSION:
3516  msg = gettext_noop("must be owner of extension %s");
3517  break;
3518  case OBJECT_FDW:
3519  msg = gettext_noop("must be owner of foreign-data wrapper %s");
3520  break;
3521  case OBJECT_FOREIGN_SERVER:
3522  msg = gettext_noop("must be owner of foreign server %s");
3523  break;
3524  case OBJECT_FOREIGN_TABLE:
3525  msg = gettext_noop("must be owner of foreign table %s");
3526  break;
3527  case OBJECT_FUNCTION:
3528  msg = gettext_noop("must be owner of function %s");
3529  break;
3530  case OBJECT_INDEX:
3531  msg = gettext_noop("must be owner of index %s");
3532  break;
3533  case OBJECT_LANGUAGE:
3534  msg = gettext_noop("must be owner of language %s");
3535  break;
3536  case OBJECT_LARGEOBJECT:
3537  msg = gettext_noop("must be owner of large object %s");
3538  break;
3539  case OBJECT_MATVIEW:
3540  msg = gettext_noop("must be owner of materialized view %s");
3541  break;
3542  case OBJECT_OPCLASS:
3543  msg = gettext_noop("must be owner of operator class %s");
3544  break;
3545  case OBJECT_OPERATOR:
3546  msg = gettext_noop("must be owner of operator %s");
3547  break;
3548  case OBJECT_OPFAMILY:
3549  msg = gettext_noop("must be owner of operator family %s");
3550  break;
3551  case OBJECT_PROCEDURE:
3552  msg = gettext_noop("must be owner of procedure %s");
3553  break;
3554  case OBJECT_PUBLICATION:
3555  msg = gettext_noop("must be owner of publication %s");
3556  break;
3557  case OBJECT_ROUTINE:
3558  msg = gettext_noop("must be owner of routine %s");
3559  break;
3560  case OBJECT_SEQUENCE:
3561  msg = gettext_noop("must be owner of sequence %s");
3562  break;
3563  case OBJECT_SUBSCRIPTION:
3564  msg = gettext_noop("must be owner of subscription %s");
3565  break;
3566  case OBJECT_TABLE:
3567  msg = gettext_noop("must be owner of table %s");
3568  break;
3569  case OBJECT_TYPE:
3570  msg = gettext_noop("must be owner of type %s");
3571  break;
3572  case OBJECT_VIEW:
3573  msg = gettext_noop("must be owner of view %s");
3574  break;
3575  case OBJECT_SCHEMA:
3576  msg = gettext_noop("must be owner of schema %s");
3577  break;
3578  case OBJECT_STATISTIC_EXT:
3579  msg = gettext_noop("must be owner of statistics object %s");
3580  break;
3581  case OBJECT_TABLESPACE:
3582  msg = gettext_noop("must be owner of tablespace %s");
3583  break;
3585  msg = gettext_noop("must be owner of text search configuration %s");
3586  break;
3587  case OBJECT_TSDICTIONARY:
3588  msg = gettext_noop("must be owner of text search dictionary %s");
3589  break;
3590 
3591  /*
3592  * Special cases: For these, the error message talks
3593  * about "relation", because that's where the
3594  * ownership is attached. See also
3595  * check_object_ownership().
3596  */
3597  case OBJECT_COLUMN:
3598  case OBJECT_POLICY:
3599  case OBJECT_RULE:
3600  case OBJECT_TABCONSTRAINT:
3601  case OBJECT_TRIGGER:
3602  msg = gettext_noop("must be owner of relation %s");
3603  break;
3604  /* these currently aren't used */
3605  case OBJECT_ACCESS_METHOD:
3606  case OBJECT_AMOP:
3607  case OBJECT_AMPROC:
3608  case OBJECT_ATTRIBUTE:
3609  case OBJECT_CAST:
3610  case OBJECT_DEFAULT:
3611  case OBJECT_DEFACL:
3612  case OBJECT_DOMCONSTRAINT:
3614  case OBJECT_ROLE:
3615  case OBJECT_TRANSFORM:
3616  case OBJECT_TSPARSER:
3617  case OBJECT_TSTEMPLATE:
3618  case OBJECT_USER_MAPPING:
3619  elog(ERROR, "unsupported object type %d", objtype);
3620  }
3621 
3622  ereport(ERROR,
3623  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3624  errmsg(msg, objectname)));
3625  break;
3626  }
3627  default:
3628  elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
3629  break;
3630  }
3631 }
#define gettext_noop(x)
Definition: c.h:1148
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ aclcheck_error_col()

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

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

3637 {
3638  switch (aclerr)
3639  {
3640  case ACLCHECK_OK:
3641  /* no error, so return to caller */
3642  break;
3643  case ACLCHECK_NO_PRIV:
3644  ereport(ERROR,
3645  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3646  errmsg("permission denied for column \"%s\" of relation \"%s\"",
3647  colname, objectname)));
3648  break;
3649  case ACLCHECK_NOT_OWNER:
3650  /* relation msg is OK since columns don't have separate owners */
3651  aclcheck_error(aclerr, objtype, objectname);
3652  break;
3653  default:
3654  elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
3655  break;
3656  }
3657 }
int errcode(int sqlerrcode)
Definition: elog.c:608
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3352
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ aclcheck_error_type()

void aclcheck_error_type ( AclResult  aclerr,
Oid  typeOid 
)

Definition at line 3665 of file aclchk.c.

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

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

3666 {
3667  Oid element_type = get_element_type(typeOid);
3668 
3669  aclcheck_error(aclerr, OBJECT_TYPE, format_type_be(element_type ? element_type : typeOid));
3670 }
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2526
char * format_type_be(Oid type_oid)
Definition: format_type.c:326
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3352

◆ aclconcat()

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

Definition at line 428 of file acl.c.

References ACL_DAT, ACL_NUM, and allocacl().

Referenced by ExecGrant_Attribute().

429 {
430  Acl *result_acl;
431 
432  result_acl = allocacl(ACL_NUM(left_acl) + ACL_NUM(right_acl));
433 
434  memcpy(ACL_DAT(result_acl),
435  ACL_DAT(left_acl),
436  ACL_NUM(left_acl) * sizeof(AclItem));
437 
438  memcpy(ACL_DAT(result_acl) + ACL_NUM(left_acl),
439  ACL_DAT(right_acl),
440  ACL_NUM(right_acl) * sizeof(AclItem));
441 
442  return result_acl;
443 }
static Acl * allocacl(int n)
Definition: acl.c:377
#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 408 of file acl.c.

References ACL_DAT, ACL_NUM, and allocacl().

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

409 {
410  Acl *result_acl;
411 
412  result_acl = allocacl(ACL_NUM(orig_acl));
413 
414  memcpy(ACL_DAT(result_acl),
415  ACL_DAT(orig_acl),
416  ACL_NUM(orig_acl) * sizeof(AclItem));
417 
418  return result_acl;
419 }
static Acl * allocacl(int n)
Definition: acl.c:377
#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 749 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(), 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().

750 {
751  AclMode world_default;
752  AclMode owner_default;
753  int nacl;
754  Acl *acl;
755  AclItem *aip;
756 
757  switch (objtype)
758  {
759  case OBJECT_COLUMN:
760  /* by default, columns have no extra privileges */
761  world_default = ACL_NO_RIGHTS;
762  owner_default = ACL_NO_RIGHTS;
763  break;
764  case OBJECT_TABLE:
765  world_default = ACL_NO_RIGHTS;
766  owner_default = ACL_ALL_RIGHTS_RELATION;
767  break;
768  case OBJECT_SEQUENCE:
769  world_default = ACL_NO_RIGHTS;
770  owner_default = ACL_ALL_RIGHTS_SEQUENCE;
771  break;
772  case OBJECT_DATABASE:
773  /* for backwards compatibility, grant some rights by default */
774  world_default = ACL_CREATE_TEMP | ACL_CONNECT;
775  owner_default = ACL_ALL_RIGHTS_DATABASE;
776  break;
777  case OBJECT_FUNCTION:
778  /* Grant EXECUTE by default, for now */
779  world_default = ACL_EXECUTE;
780  owner_default = ACL_ALL_RIGHTS_FUNCTION;
781  break;
782  case OBJECT_LANGUAGE:
783  /* Grant USAGE by default, for now */
784  world_default = ACL_USAGE;
785  owner_default = ACL_ALL_RIGHTS_LANGUAGE;
786  break;
787  case OBJECT_LARGEOBJECT:
788  world_default = ACL_NO_RIGHTS;
789  owner_default = ACL_ALL_RIGHTS_LARGEOBJECT;
790  break;
791  case OBJECT_SCHEMA:
792  world_default = ACL_NO_RIGHTS;
793  owner_default = ACL_ALL_RIGHTS_SCHEMA;
794  break;
795  case OBJECT_TABLESPACE:
796  world_default = ACL_NO_RIGHTS;
797  owner_default = ACL_ALL_RIGHTS_TABLESPACE;
798  break;
799  case OBJECT_FDW:
800  world_default = ACL_NO_RIGHTS;
801  owner_default = ACL_ALL_RIGHTS_FDW;
802  break;
804  world_default = ACL_NO_RIGHTS;
805  owner_default = ACL_ALL_RIGHTS_FOREIGN_SERVER;
806  break;
807  case OBJECT_DOMAIN:
808  case OBJECT_TYPE:
809  world_default = ACL_USAGE;
810  owner_default = ACL_ALL_RIGHTS_TYPE;
811  break;
812  default:
813  elog(ERROR, "unrecognized objtype: %d", (int) objtype);
814  world_default = ACL_NO_RIGHTS; /* keep compiler quiet */
815  owner_default = ACL_NO_RIGHTS;
816  break;
817  }
818 
819  nacl = 0;
820  if (world_default != ACL_NO_RIGHTS)
821  nacl++;
822  if (owner_default != ACL_NO_RIGHTS)
823  nacl++;
824 
825  acl = allocacl(nacl);
826  aip = ACL_DAT(acl);
827 
828  if (world_default != ACL_NO_RIGHTS)
829  {
830  aip->ai_grantee = ACL_ID_PUBLIC;
831  aip->ai_grantor = ownerId;
832  ACLITEM_SET_PRIVS_GOPTIONS(*aip, world_default, ACL_NO_RIGHTS);
833  aip++;
834  }
835 
836  /*
837  * Note that the owner's entry shows all ordinary privileges but no grant
838  * options. This is because his grant options come "from the system" and
839  * not from his own efforts. (The SQL spec says that the owner's rights
840  * come from a "_SYSTEM" authid.) However, we do consider that the
841  * owner's ordinary privileges are self-granted; this lets him revoke
842  * them. We implement the owner's grant options without any explicit
843  * "_SYSTEM"-like ACL entry, by internally special-casing the owner
844  * wherever we are testing grant options.
845  */
846  if (owner_default != ACL_NO_RIGHTS)
847  {
848  aip->ai_grantee = ownerId;
849  aip->ai_grantor = ownerId;
850  ACLITEM_SET_PRIVS_GOPTIONS(*aip, owner_default, ACL_NO_RIGHTS);
851  }
852 
853  return acl;
854 }
Oid ai_grantee
Definition: acl.h:56
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:162
static Acl * allocacl(int n)
Definition: acl.c:377
#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:72
#define ERROR
Definition: elog.h:43
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
#define ACL_ALL_RIGHTS_SCHEMA
Definition: acl.h:165
#define ACL_ALL_RIGHTS_TYPE
Definition: acl.h:167
#define ACL_USAGE
Definition: parsenodes.h:82
#define ACL_CONNECT
Definition: parsenodes.h:86
#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:81
#define elog(elevel,...)
Definition: elog.h:228
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:157
#define ACL_CREATE_TEMP
Definition: parsenodes.h:85
#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 510 of file acl.c.

References ACL_DAT, and ACL_NUM.

Referenced by get_user_default_acl(), and SetDefaultACL().

511 {
512  /* Check for cases where one or both are empty/null */
513  if (left_acl == NULL || ACL_NUM(left_acl) == 0)
514  {
515  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
516  return true;
517  else
518  return false;
519  }
520  else
521  {
522  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
523  return false;
524  }
525 
526  if (ACL_NUM(left_acl) != ACL_NUM(right_acl))
527  return false;
528 
529  if (memcmp(ACL_DAT(left_acl),
530  ACL_DAT(right_acl),
531  ACL_NUM(left_acl) * sizeof(AclItem)) == 0)
532  return true;
533 
534  return false;
535 }
#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 496 of file acl.c.

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

Referenced by get_user_default_acl(), and SetDefaultACL().

497 {
498  if (acl != NULL && ACL_NUM(acl) > 1)
499  qsort(ACL_DAT(acl), ACL_NUM(acl), sizeof(AclItem), aclitemComparator);
500 }
#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:488
static int aclitemComparator(const void *arg1, const void *arg2)
Definition: acl.c:670

◆ aclmask()

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

Definition at line 1321 of file acl.c.

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

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

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

◆ aclmembers()

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

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

1474 {
1475  Oid *list;
1476  const AclItem *acldat;
1477  int i,
1478  j;
1479 
1480  if (acl == NULL || ACL_NUM(acl) == 0)
1481  {
1482  *roleids = NULL;
1483  return 0;
1484  }
1485 
1486  check_acl(acl);
1487 
1488  /* Allocate the worst-case space requirement */
1489  list = palloc(ACL_NUM(acl) * 2 * sizeof(Oid));
1490  acldat = ACL_DAT(acl);
1491 
1492  /*
1493  * Walk the ACL collecting mentioned RoleIds.
1494  */
1495  j = 0;
1496  for (i = 0; i < ACL_NUM(acl); i++)
1497  {
1498  const AclItem *ai = &acldat[i];
1499 
1500  if (ai->ai_grantee != ACL_ID_PUBLIC)
1501  list[j++] = ai->ai_grantee;
1502  /* grantor is currently never PUBLIC, but let's check anyway */
1503  if (ai->ai_grantor != ACL_ID_PUBLIC)
1504  list[j++] = ai->ai_grantor;
1505  }
1506 
1507  /* Sort the array */
1508  qsort(list, j, sizeof(Oid), oid_cmp);
1509 
1510  /*
1511  * We could repalloc the array down to minimum size, but it's hardly worth
1512  * it since it's only transient memory.
1513  */
1514  *roleids = list;
1515 
1516  /* Remove duplicates from the array */
1517  return qunique(list, j, sizeof(Oid), oid_cmp);
1518 }
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:949
int i
#define ACL_ID_PUBLIC
Definition: acl.h:46
#define qsort(a, b, c, d)
Definition: port.h:488
static void check_acl(const Acl *acl)
Definition: acl.c:541

◆ aclmerge()

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

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

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

◆ aclnewowner()

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

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

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

◆ aclupdate()

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

Definition at line 931 of file acl.c.

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

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

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

◆ check_is_member_of_role()

void check_is_member_of_role ( Oid  member,
Oid  role 
)

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

4947 {
4948  if (!is_member_of_role(member, role))
4949  ereport(ERROR,
4950  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4951  errmsg("must be member of role \"%s\"",
4952  GetUserNameFromId(role, false))));
4953 }
int errcode(int sqlerrcode)
Definition: elog.c:608
Oid member
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4924
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:795
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ check_rolespec_name()

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

Definition at line 5322 of file acl.c.

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

Referenced by AlterRole(), and AlterRoleSet().

5323 {
5324  if (!role)
5325  return;
5326 
5327  if (role->roletype != ROLESPEC_CSTRING)
5328  return;
5329 
5330  if (IsReservedName(role->rolename))
5331  {
5332  if (detail_msg)
5333  ereport(ERROR,
5334  (errcode(ERRCODE_RESERVED_NAME),
5335  errmsg("role name \"%s\" is reserved",
5336  role->rolename),
5337  errdetail("%s", detail_msg)));
5338  else
5339  ereport(ERROR,
5340  (errcode(ERRCODE_RESERVED_NAME),
5341  errmsg("role name \"%s\" is reserved",
5342  role->rolename)));
5343  }
5344 }
int errcode(int sqlerrcode)
Definition: elog.c:608
bool IsReservedName(const char *name)
Definition: catalog.c:213
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:955
#define ereport(elevel, rest)
Definition: elog.h:141
RoleSpecType roletype
Definition: parsenodes.h:328
char * rolename
Definition: parsenodes.h:329
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ ExecAlterDefaultPrivilegesStmt()

void ExecAlterDefaultPrivilegesStmt ( ParseState pstate,
AlterDefaultPrivilegesStmt stmt 
)

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

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

◆ ExecuteGrantStmt()

void ExecuteGrantStmt ( GrantStmt stmt)

Definition at line 384 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, InternalGrant::grant_option, GrantStmt::grant_option, InternalGrant::grantees, GrantStmt::grantees, 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().

385 {
386  InternalGrant istmt;
387  ListCell *cell;
388  const char *errormsg;
389  AclMode all_privileges;
390 
391  /*
392  * Turn the regular GrantStmt into the InternalGrant form.
393  */
394  istmt.is_grant = stmt->is_grant;
395  istmt.objtype = stmt->objtype;
396 
397  /* Collect the OIDs of the target objects */
398  switch (stmt->targtype)
399  {
400  case ACL_TARGET_OBJECT:
401  istmt.objects = objectNamesToOids(stmt->objtype, stmt->objects);
402  break;
404  istmt.objects = objectsInSchemaToOids(stmt->objtype, stmt->objects);
405  break;
406  /* ACL_TARGET_DEFAULTS should not be seen here */
407  default:
408  elog(ERROR, "unrecognized GrantStmt.targtype: %d",
409  (int) stmt->targtype);
410  }
411 
412  /* all_privs to be filled below */
413  /* privileges to be filled below */
414  istmt.col_privs = NIL; /* may get filled below */
415  istmt.grantees = NIL; /* filled below */
416  istmt.grant_option = stmt->grant_option;
417  istmt.behavior = stmt->behavior;
418 
419  /*
420  * Convert the RoleSpec list into an Oid list. Note that at this point we
421  * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
422  * there shouldn't be any additional work needed to support this case.
423  */
424  foreach(cell, stmt->grantees)
425  {
426  RoleSpec *grantee = (RoleSpec *) lfirst(cell);
427  Oid grantee_uid;
428 
429  switch (grantee->roletype)
430  {
431  case ROLESPEC_PUBLIC:
432  grantee_uid = ACL_ID_PUBLIC;
433  break;
434  default:
435  grantee_uid = get_rolespec_oid(grantee, false);
436  break;
437  }
438  istmt.grantees = lappend_oid(istmt.grantees, grantee_uid);
439  }
440 
441  /*
442  * Convert stmt->privileges, a list of AccessPriv nodes, into an AclMode
443  * bitmask. Note: objtype can't be OBJECT_COLUMN.
444  */
445  switch (stmt->objtype)
446  {
447  case OBJECT_TABLE:
448 
449  /*
450  * Because this might be a sequence, we test both relation and
451  * sequence bits, and later do a more limited test when we know
452  * the object type.
453  */
455  errormsg = gettext_noop("invalid privilege type %s for relation");
456  break;
457  case OBJECT_SEQUENCE:
458  all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
459  errormsg = gettext_noop("invalid privilege type %s for sequence");
460  break;
461  case OBJECT_DATABASE:
462  all_privileges = ACL_ALL_RIGHTS_DATABASE;
463  errormsg = gettext_noop("invalid privilege type %s for database");
464  break;
465  case OBJECT_DOMAIN:
466  all_privileges = ACL_ALL_RIGHTS_TYPE;
467  errormsg = gettext_noop("invalid privilege type %s for domain");
468  break;
469  case OBJECT_FUNCTION:
470  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
471  errormsg = gettext_noop("invalid privilege type %s for function");
472  break;
473  case OBJECT_LANGUAGE:
474  all_privileges = ACL_ALL_RIGHTS_LANGUAGE;
475  errormsg = gettext_noop("invalid privilege type %s for language");
476  break;
477  case OBJECT_LARGEOBJECT:
478  all_privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
479  errormsg = gettext_noop("invalid privilege type %s for large object");
480  break;
481  case OBJECT_SCHEMA:
482  all_privileges = ACL_ALL_RIGHTS_SCHEMA;
483  errormsg = gettext_noop("invalid privilege type %s for schema");
484  break;
485  case OBJECT_PROCEDURE:
486  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
487  errormsg = gettext_noop("invalid privilege type %s for procedure");
488  break;
489  case OBJECT_ROUTINE:
490  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
491  errormsg = gettext_noop("invalid privilege type %s for routine");
492  break;
493  case OBJECT_TABLESPACE:
494  all_privileges = ACL_ALL_RIGHTS_TABLESPACE;
495  errormsg = gettext_noop("invalid privilege type %s for tablespace");
496  break;
497  case OBJECT_TYPE:
498  all_privileges = ACL_ALL_RIGHTS_TYPE;
499  errormsg = gettext_noop("invalid privilege type %s for type");
500  break;
501  case OBJECT_FDW:
502  all_privileges = ACL_ALL_RIGHTS_FDW;
503  errormsg = gettext_noop("invalid privilege type %s for foreign-data wrapper");
504  break;
506  all_privileges = ACL_ALL_RIGHTS_FOREIGN_SERVER;
507  errormsg = gettext_noop("invalid privilege type %s for foreign server");
508  break;
509  default:
510  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
511  (int) stmt->objtype);
512  /* keep compiler quiet */
513  all_privileges = ACL_NO_RIGHTS;
514  errormsg = NULL;
515  }
516 
517  if (stmt->privileges == NIL)
518  {
519  istmt.all_privs = true;
520 
521  /*
522  * will be turned into ACL_ALL_RIGHTS_* by the internal routines
523  * depending on the object type
524  */
525  istmt.privileges = ACL_NO_RIGHTS;
526  }
527  else
528  {
529  istmt.all_privs = false;
530  istmt.privileges = ACL_NO_RIGHTS;
531 
532  foreach(cell, stmt->privileges)
533  {
534  AccessPriv *privnode = (AccessPriv *) lfirst(cell);
535  AclMode priv;
536 
537  /*
538  * If it's a column-level specification, we just set it aside in
539  * col_privs for the moment; but insist it's for a relation.
540  */
541  if (privnode->cols)
542  {
543  if (stmt->objtype != OBJECT_TABLE)
544  ereport(ERROR,
545  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
546  errmsg("column privileges are only valid for relations")));
547  istmt.col_privs = lappend(istmt.col_privs, privnode);
548  continue;
549  }
550 
551  if (privnode->priv_name == NULL) /* parser mistake? */
552  elog(ERROR, "AccessPriv node must specify privilege or columns");
553  priv = string_to_privilege(privnode->priv_name);
554 
555  if (priv & ~((AclMode) all_privileges))
556  ereport(ERROR,
557  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
558  errmsg(errormsg, privilege_to_string(priv))));
559 
560  istmt.privileges |= priv;
561  }
562  }
563 
564  ExecGrantStmt_oids(&istmt);
565 }
#define NIL
Definition: pg_list.h:65
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:162
static AclMode string_to_privilege(const char *privname)
Definition: aclchk.c:3274
AclMode privileges
bool grant_option
Definition: parsenodes.h:1912
#define gettext_noop(x)
Definition: c.h:1148
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ACL_ALL_RIGHTS_TABLESPACE
Definition: acl.h:166
List * cols
Definition: parsenodes.h:1942
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:358
#define ACL_ALL_RIGHTS_LANGUAGE
Definition: acl.h:163
uint32 AclMode
Definition: parsenodes.h:72
#define ERROR
Definition: elog.h:43
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
#define ACL_ALL_RIGHTS_SCHEMA
Definition: acl.h:165
#define ACL_ALL_RIGHTS_TYPE
Definition: acl.h:167
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:322
bool is_grant
Definition: parsenodes.h:1904
static void ExecGrantStmt_oids(InternalGrant *istmt)
Definition: aclchk.c:573
static List * objectNamesToOids(ObjectType objtype, List *objnames)
Definition: aclchk.c:637
RoleSpecType roletype
Definition: parsenodes.h:328
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition: acl.c:5217
#define ACL_ALL_RIGHTS_SEQUENCE
Definition: acl.h:158
List * privileges
Definition: parsenodes.h:1909
static List * objectsInSchemaToOids(ObjectType objtype, List *nspnames)
Definition: aclchk.c:786
#define ACL_ALL_RIGHTS_LARGEOBJECT
Definition: acl.h:164
DropBehavior behavior
DropBehavior behavior
Definition: parsenodes.h:1913
#define lfirst(lc)
Definition: pg_list.h:190
List * objects
Definition: parsenodes.h:1907
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:822
#define elog(elevel,...)
Definition: elog.h:228
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:157
ObjectType objtype
Definition: parsenodes.h:1906
#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:3311
List * grantees
Definition: parsenodes.h:1911
GrantTargetType targtype
Definition: parsenodes.h:1905
char * priv_name
Definition: parsenodes.h:1941

◆ get_role_oid()

Oid get_role_oid ( const char *  rolename,
bool  missing_ok 
)

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

5184 {
5185  Oid oid;
5186 
5187  oid = GetSysCacheOid1(AUTHNAME, Anum_pg_authid_oid,
5189  if (!OidIsValid(oid) && !missing_ok)
5190  ereport(ERROR,
5191  (errcode(ERRCODE_UNDEFINED_OBJECT),
5192  errmsg("role \"%s\" does not exist", rolname)));
5193  return oid;
5194 }
NameData rolname
Definition: pg_authid.h:34
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ get_role_oid_or_public()

◆ get_rolespec_name()

char* get_rolespec_name ( const RoleSpec role)

Definition at line 5300 of file acl.c.

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

Referenced by AddRoleMems(), and DelRoleMems().

5301 {
5302  HeapTuple tp;
5303  Form_pg_authid authForm;
5304  char *rolename;
5305 
5306  tp = get_rolespec_tuple(role);
5307  authForm = (Form_pg_authid) GETSTRUCT(tp);
5308  rolename = pstrdup(NameStr(authForm->rolname));
5309  ReleaseSysCache(tp);
5310 
5311  return rolename;
5312 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * pstrdup(const char *in)
Definition: mcxt.c:1186
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:56
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
HeapTuple get_rolespec_tuple(const RoleSpec *role)
Definition: acl.c:5255
#define NameStr(name)
Definition: c.h:616

◆ get_rolespec_oid()

Oid get_rolespec_oid ( const RoleSpec role,
bool  missing_ok 
)

Definition at line 5217 of file acl.c.

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

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

5218 {
5219  Oid oid;
5220 
5221  switch (role->roletype)
5222  {
5223  case ROLESPEC_CSTRING:
5224  Assert(role->rolename);
5225  oid = get_role_oid(role->rolename, missing_ok);
5226  break;
5227 
5228  case ROLESPEC_CURRENT_USER:
5229  oid = GetUserId();
5230  break;
5231 
5232  case ROLESPEC_SESSION_USER:
5233  oid = GetSessionUserId();
5234  break;
5235 
5236  case ROLESPEC_PUBLIC:
5237  ereport(ERROR,
5238  (errcode(ERRCODE_UNDEFINED_OBJECT),
5239  errmsg("role \"%s\" does not exist", "public")));
5240  oid = InvalidOid; /* make compiler happy */
5241  break;
5242 
5243  default:
5244  elog(ERROR, "unexpected role type %d", role->roletype);
5245  }
5246 
5247  return oid;
5248 }
Oid GetUserId(void)
Definition: miscinit.c:380
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
Oid GetSessionUserId(void)
Definition: miscinit.c:414
Oid get_role_oid(const char *rolname, bool missing_ok)
Definition: acl.c:5183
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
RoleSpecType roletype
Definition: parsenodes.h:328
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:739
char * rolename
Definition: parsenodes.h:329
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ get_rolespec_tuple()

HeapTuple get_rolespec_tuple ( const RoleSpec role)

Definition at line 5255 of file acl.c.

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

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

5256 {
5257  HeapTuple tuple;
5258 
5259  switch (role->roletype)
5260  {
5261  case ROLESPEC_CSTRING:
5262  Assert(role->rolename);
5264  if (!HeapTupleIsValid(tuple))
5265  ereport(ERROR,
5266  (errcode(ERRCODE_UNDEFINED_OBJECT),
5267  errmsg("role \"%s\" does not exist", role->rolename)));
5268  break;
5269 
5270  case ROLESPEC_CURRENT_USER:
5271  tuple = SearchSysCache1(AUTHOID, GetUserId());
5272  if (!HeapTupleIsValid(tuple))
5273  elog(ERROR, "cache lookup failed for role %u", GetUserId());
5274  break;
5275 
5276  case ROLESPEC_SESSION_USER:
5278  if (!HeapTupleIsValid(tuple))
5279  elog(ERROR, "cache lookup failed for role %u", GetSessionUserId());
5280  break;
5281 
5282  case ROLESPEC_PUBLIC:
5283  ereport(ERROR,
5284  (errcode(ERRCODE_UNDEFINED_OBJECT),
5285  errmsg("role \"%s\" does not exist", "public")));
5286  tuple = NULL; /* make compiler happy */
5287  break;
5288 
5289  default:
5290  elog(ERROR, "unexpected role type %d", role->roletype);
5291  }
5292 
5293  return tuple;
5294 }
Oid GetUserId(void)
Definition: miscinit.c:380
int errcode(int sqlerrcode)
Definition: elog.c:608
Oid GetSessionUserId(void)
Definition: miscinit.c:414
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
RoleSpecType roletype
Definition: parsenodes.h:328
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:739
char * rolename
Definition: parsenodes.h:329
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ get_user_default_acl()

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

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

5457 {
5458  Acl *result;
5459  Acl *glob_acl;
5460  Acl *schema_acl;
5461  Acl *def_acl;
5462  char defaclobjtype;
5463 
5464  /*
5465  * Use NULL during bootstrap, since pg_default_acl probably isn't there
5466  * yet.
5467  */
5469  return NULL;
5470 
5471  /* Check if object type is supported in pg_default_acl */
5472  switch (objtype)
5473  {
5474  case OBJECT_TABLE:
5475  defaclobjtype = DEFACLOBJ_RELATION;
5476  break;
5477 
5478  case OBJECT_SEQUENCE:
5479  defaclobjtype = DEFACLOBJ_SEQUENCE;
5480  break;
5481 
5482  case OBJECT_FUNCTION:
5483  defaclobjtype = DEFACLOBJ_FUNCTION;
5484  break;
5485 
5486  case OBJECT_TYPE:
5487  defaclobjtype = DEFACLOBJ_TYPE;
5488  break;
5489 
5490  case OBJECT_SCHEMA:
5491  defaclobjtype = DEFACLOBJ_NAMESPACE;
5492  break;
5493 
5494  default:
5495  return NULL;
5496  }
5497 
5498  /* Look up the relevant pg_default_acl entries */
5499  glob_acl = get_default_acl_internal(ownerId, InvalidOid, defaclobjtype);
5500  schema_acl = get_default_acl_internal(ownerId, nsp_oid, defaclobjtype);
5501 
5502  /* Quick out if neither entry exists */
5503  if (glob_acl == NULL && schema_acl == NULL)
5504  return NULL;
5505 
5506  /* We need to know the hard-wired default value, too */
5507  def_acl = acldefault(objtype, ownerId);
5508 
5509  /* If there's no global entry, substitute the hard-wired default */
5510  if (glob_acl == NULL)
5511  glob_acl = def_acl;
5512 
5513  /* Merge in any per-schema privileges */
5514  result = aclmerge(glob_acl, schema_acl, ownerId);
5515 
5516  /*
5517  * For efficiency, we want to return NULL if the result equals default.
5518  * This requires sorting both arrays to get an accurate comparison.
5519  */
5520  aclitemsort(result);
5521  aclitemsort(def_acl);
5522  if (aclequal(result, def_acl))
5523  result = NULL;
5524 
5525  return result;
5526 }
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:749
static Acl * get_default_acl_internal(Oid roleId, Oid nsp_oid, char objtype)
Definition: aclchk.c:5421
bool aclequal(const Acl *left_acl, const Acl *right_acl)
Definition: acl.c:510
#define InvalidOid
Definition: postgres_ext.h:36
Acl * aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId)
Definition: acl.c:452
void aclitemsort(Acl *acl)
Definition: acl.c:496
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:374

◆ has_bypassrls_privilege()

bool has_bypassrls_privilege ( Oid  roleid)

Definition at line 5397 of file aclchk.c.

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

Referenced by check_enable_rls(), and RI_Initial_Check().

5398 {
5399  bool result = false;
5400  HeapTuple utup;
5401 
5402  /* Superusers bypass all permission checking. */
5403  if (superuser_arg(roleid))
5404  return true;
5405 
5406  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5407  if (HeapTupleIsValid(utup))
5408  {
5409  result = ((Form_pg_authid) GETSTRUCT(utup))->rolbypassrls;
5410  ReleaseSysCache(utup);
5411  }
5412  return result;
5413 }
bool rolbypassrls
Definition: pg_authid.h:41
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:56
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78

◆ has_createrole_privilege()

bool has_createrole_privilege ( Oid  roleid)

Definition at line 5378 of file aclchk.c.

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

Referenced by check_object_ownership(), and have_createrole_privilege().

5379 {
5380  bool result = false;
5381  HeapTuple utup;
5382 
5383  /* Superusers bypass all permission checking. */
5384  if (superuser_arg(roleid))
5385  return true;
5386 
5387  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5388  if (HeapTupleIsValid(utup))
5389  {
5390  result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreaterole;
5391  ReleaseSysCache(utup);
5392  }
5393  return result;
5394 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:56
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
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:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78

◆ has_privs_of_role()

bool has_privs_of_role ( Oid  member,
Oid  role 
)

Definition at line 4900 of file acl.c.

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

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

4901 {
4902  /* Fast path for simple case */
4903  if (member == role)
4904  return true;
4905 
4906  /* Superusers have every privilege, so are part of every role */
4907  if (superuser_arg(member))
4908  return true;
4909 
4910  /*
4911  * Find all the roles that member has the privileges of, including
4912  * multi-level recursion, then see if target role is any one of them.
4913  */
4914  return list_member_oid(roles_has_privs_of(member), role);
4915 }
Oid member
static List * roles_has_privs_of(Oid roleid)
Definition: acl.c:4740
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:675

◆ initialize_acl()

void initialize_acl ( void  )

Definition at line 4681 of file acl.c.

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

Referenced by InitPostgres().

4682 {
4684  {
4685  /*
4686  * In normal mode, set a callback on any syscache invalidation of
4687  * pg_auth_members rows
4688  */
4691  (Datum) 0);
4692  }
4693 }
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1426
uintptr_t Datum
Definition: postgres.h:367
static void RoleMembershipCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: acl.c:4700
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:374

◆ is_admin_of_role()

bool is_admin_of_role ( Oid  member,
Oid  role 
)

Definition at line 4982 of file acl.c.

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

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

4983 {
4984  bool result = false;
4985  List *roles_list;
4986  ListCell *l;
4987 
4988  if (superuser_arg(member))
4989  return true;
4990 
4991  if (member == role)
4992 
4993  /*
4994  * A role can admin itself when it matches the session user and we're
4995  * outside any security-restricted operation, SECURITY DEFINER or
4996  * similar context. SQL-standard roles cannot self-admin. However,
4997  * SQL-standard users are distinct from roles, and they are not
4998  * grantable like roles: PostgreSQL's role-user duality extends the
4999  * standard. Checking for a session user match has the effect of
5000  * letting a role self-admin only when it's conspicuously behaving
5001  * like a user. Note that allowing self-admin under a mere SET ROLE
5002  * would make WITH ADMIN OPTION largely irrelevant; any member could
5003  * SET ROLE to issue the otherwise-forbidden command.
5004  *
5005  * Withholding self-admin in a security-restricted operation prevents
5006  * object owners from harnessing the session user identity during
5007  * administrative maintenance. Suppose Alice owns a database, has
5008  * issued "GRANT alice TO bob", and runs a daily ANALYZE. Bob creates
5009  * an alice-owned SECURITY DEFINER function that issues "REVOKE alice
5010  * FROM carol". If he creates an expression index calling that
5011  * function, Alice will attempt the REVOKE during each ANALYZE.
5012  * Checking InSecurityRestrictedOperation() thwarts that attack.
5013  *
5014  * Withholding self-admin in SECURITY DEFINER functions makes their
5015  * behavior independent of the calling user. There's no security or
5016  * SQL-standard-conformance need for that restriction, though.
5017  *
5018  * A role cannot have actual WITH ADMIN OPTION on itself, because that
5019  * would imply a membership loop. Therefore, we're done either way.
5020  */
5021  return member == GetSessionUserId() &&
5023 
5024  /*
5025  * Find all the roles that member is a member of, including multi-level
5026  * recursion. We build a list in the same way that is_member_of_role does
5027  * to track visited and unvisited roles.
5028  */
5029  roles_list = list_make1_oid(member);
5030 
5031  foreach(l, roles_list)
5032  {
5033  Oid memberid = lfirst_oid(l);
5034  CatCList *memlist;
5035  int i;
5036 
5037  /* Find roles that memberid is directly a member of */
5039  ObjectIdGetDatum(memberid));
5040  for (i = 0; i < memlist->n_members; i++)
5041  {
5042  HeapTuple tup = &memlist->members[i]->tuple;
5043  Oid otherid = ((Form_pg_auth_members) GETSTRUCT(tup))->roleid;
5044 
5045  if (otherid == role &&
5047  {
5048  /* Found what we came for, so can stop searching */
5049  result = true;
5050  break;
5051  }
5052 
5053  roles_list = list_append_unique_oid(roles_list, otherid);
5054  }
5055  ReleaseSysCacheList(memlist);
5056  if (result)
5057  break;
5058  }
5059 
5060  list_free(roles_list);
5061 
5062  return result;
5063 }
bool InLocalUserIdChange(void)
Definition: miscinit.c:503
int n_members
Definition: catcache.h:176
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:1218
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
unsigned int Oid
Definition: postgres_ext.h:31
Oid GetSessionUserId(void)
Definition: miscinit.c:414
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:178
Oid member
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:210
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
#define ReleaseSysCacheList(x)
Definition: syscache.h:217
#define list_make1_oid(x1)
Definition: pg_list.h:249
FormData_pg_auth_members * Form_pg_auth_members
bool admin_option
bool InSecurityRestrictedOperation(void)
Definition: miscinit.c:512
void list_free(List *list)
Definition: list.c:1377
int i
HeapTupleData tuple
Definition: catcache.h:121
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ is_member_of_role()

bool is_member_of_role ( Oid  member,
Oid  role 
)

Definition at line 4924 of file acl.c.

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

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

4925 {
4926  /* Fast path for simple case */
4927  if (member == role)
4928  return true;
4929 
4930  /* Superusers have every privilege, so are part of every role */
4931  if (superuser_arg(member))
4932  return true;
4933 
4934  /*
4935  * Find all the roles that member is a member of, including multi-level
4936  * recursion, then see if target role is any one of them.
4937  */
4938  return list_member_oid(roles_is_member_of(member), role);
4939 }
Oid member
static List * roles_is_member_of(Oid roleid)
Definition: acl.c:4823
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:675

◆ is_member_of_role_nosuper()

bool is_member_of_role_nosuper ( Oid  member,
Oid  role 
)

Definition at line 4962 of file acl.c.

References list_member_oid(), and roles_is_member_of().

Referenced by AddRoleMems(), and is_member().

4963 {
4964  /* Fast path for simple case */
4965  if (member == role)
4966  return true;
4967 
4968  /*
4969  * Find all the roles that member is a member of, including multi-level
4970  * recursion, then see if target role is any one of them.
4971  */
4972  return list_member_oid(roles_is_member_of(member), role);
4973 }
Oid member
static List * roles_is_member_of(Oid roleid)
Definition: acl.c:4823
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:675

◆ make_empty_acl()

Acl* make_empty_acl ( void  )

Definition at line 399 of file acl.c.

References allocacl().

Referenced by SetDefaultACL().

400 {
401  return allocacl(0);
402 }
static Acl * allocacl(int n)
Definition: acl.c:377

◆ pg_attribute_aclcheck()

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

Definition at line 4515 of file aclchk.c.

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

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

4517 {
4518  if (pg_attribute_aclmask(table_oid, attnum, roleid, mode, ACLMASK_ANY) != 0)
4519  return ACLCHECK_OK;
4520  else
4521  return ACLCHECK_NO_PRIV;
4522 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3745
int16 attnum
Definition: pg_attribute.h:79

◆ pg_attribute_aclcheck_all()

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

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

4546 {
4547  AclResult result;
4548  HeapTuple classTuple;
4549  Form_pg_class classForm;
4550  AttrNumber nattrs;
4551  AttrNumber curr_att;
4552 
4553  /*
4554  * Must fetch pg_class row to check number of attributes. As in
4555  * pg_attribute_aclmask, we prefer to return "no privileges" instead of
4556  * throwing an error if we get any unexpected lookup errors.
4557  */
4558  classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
4559  if (!HeapTupleIsValid(classTuple))
4560  return ACLCHECK_NO_PRIV;
4561  classForm = (Form_pg_class) GETSTRUCT(classTuple);
4562 
4563  nattrs = classForm->relnatts;
4564 
4565  ReleaseSysCache(classTuple);
4566 
4567  /*
4568  * Initialize result in case there are no non-dropped columns. We want to
4569  * report failure in such cases for either value of 'how'.
4570  */
4571  result = ACLCHECK_NO_PRIV;
4572 
4573  for (curr_att = 1; curr_att <= nattrs; curr_att++)
4574  {
4575  HeapTuple attTuple;
4576  AclMode attmask;
4577 
4578  attTuple = SearchSysCache2(ATTNUM,
4579  ObjectIdGetDatum(table_oid),
4580  Int16GetDatum(curr_att));
4581  if (!HeapTupleIsValid(attTuple))
4582  continue;
4583 
4584  /* ignore dropped columns */
4585  if (((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped)
4586  {
4587  ReleaseSysCache(attTuple);
4588  continue;
4589  }
4590 
4591  /*
4592  * Here we hard-wire knowledge that the default ACL for a column
4593  * grants no privileges, so that we can fall out quickly in the very
4594  * common case where attacl is null.
4595  */
4596  if (heap_attisnull(attTuple, Anum_pg_attribute_attacl, NULL))
4597  attmask = 0;
4598  else
4599  attmask = pg_attribute_aclmask(table_oid, curr_att, roleid,
4600  mode, ACLMASK_ANY);
4601 
4602  ReleaseSysCache(attTuple);
4603 
4604  if (attmask != 0)
4605  {
4606  result = ACLCHECK_OK;
4607  if (how == ACLMASK_ANY)
4608  break; /* succeed on any success */
4609  }
4610  else
4611  {
4612  result = ACLCHECK_NO_PRIV;
4613  if (how == ACLMASK_ALL)
4614  break; /* fail on any failure */
4615  }
4616  }
4617 
4618  return result;
4619 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3745
#define Int16GetDatum(X)
Definition: postgres.h:451
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
uint32 AclMode
Definition: parsenodes.h:72
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
AclResult
Definition: acl.h:177
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127
FormData_pg_class * Form_pg_class
Definition: pg_class.h:150
int16 AttrNumber
Definition: attnum.h:21

◆ pg_attribute_aclmask()

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

Definition at line 3745 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_aclmask(), pg_attribute_aclcheck(), and pg_attribute_aclcheck_all().

3747 {
3748  AclMode result;
3749  HeapTuple classTuple;
3750  HeapTuple attTuple;
3751  Form_pg_class classForm;
3752  Form_pg_attribute attributeForm;
3753  Datum aclDatum;
3754  bool isNull;
3755  Acl *acl;
3756  Oid ownerId;
3757 
3758  /*
3759  * First, get the column's ACL from its pg_attribute entry
3760  */
3761  attTuple = SearchSysCache2(ATTNUM,
3762  ObjectIdGetDatum(table_oid),
3764  if (!HeapTupleIsValid(attTuple))
3765  ereport(ERROR,
3766  (errcode(ERRCODE_UNDEFINED_COLUMN),
3767  errmsg("attribute %d of relation with OID %u does not exist",
3768  attnum, table_oid)));
3769  attributeForm = (Form_pg_attribute) GETSTRUCT(attTuple);
3770 
3771  /* Throw error on dropped columns, too */
3772  if (attributeForm->attisdropped)
3773  ereport(ERROR,
3774  (errcode(ERRCODE_UNDEFINED_COLUMN),
3775  errmsg("attribute %d of relation with OID %u does not exist",
3776  attnum, table_oid)));
3777 
3778  aclDatum = SysCacheGetAttr(ATTNUM, attTuple, Anum_pg_attribute_attacl,
3779  &isNull);
3780 
3781  /*
3782  * Here we hard-wire knowledge that the default ACL for a column grants no
3783  * privileges, so that we can fall out quickly in the very common case
3784  * where attacl is null.
3785  */
3786  if (isNull)
3787  {
3788  ReleaseSysCache(attTuple);
3789  return 0;
3790  }
3791 
3792  /*
3793  * Must get the relation's ownerId from pg_class. Since we already found
3794  * a pg_attribute entry, the only likely reason for this to fail is that a
3795  * concurrent DROP of the relation committed since then (which could only
3796  * happen if we don't have lock on the relation). We prefer to report "no
3797  * privileges" rather than failing in such a case, so as to avoid unwanted
3798  * failures in has_column_privilege() tests.
3799  */
3800  classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
3801  if (!HeapTupleIsValid(classTuple))
3802  {
3803  ReleaseSysCache(attTuple);
3804  return 0;
3805  }
3806  classForm = (Form_pg_class) GETSTRUCT(classTuple);
3807 
3808  ownerId = classForm->relowner;
3809 
3810  ReleaseSysCache(classTuple);
3811 
3812  /* detoast column's ACL if necessary */
3813  acl = DatumGetAclP(aclDatum);
3814 
3815  result = aclmask(acl, roleid, ownerId, mask, how);
3816 
3817  /* if we have a detoasted copy, free it */
3818  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3819  pfree(acl);
3820 
3821  ReleaseSysCache(attTuple);
3822 
3823  return result;
3824 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DatumGetAclP(X)
Definition: acl.h:120
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1321
#define Int16GetDatum(X)
Definition: postgres.h:451
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
uint32 AclMode
Definition: parsenodes.h:72
void pfree(void *pointer)
Definition: mcxt.c:1056
char * Pointer
Definition: c.h:336
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
int16 attnum
Definition: pg_attribute.h:79
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127
#define DatumGetPointer(X)
Definition: postgres.h:549
FormData_pg_class * Form_pg_class
Definition: pg_class.h:150
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_class_aclcheck()

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

Definition at line 4629 of file aclchk.c.

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

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

4630 {
4631  if (pg_class_aclmask(table_oid, roleid, mode, ACLMASK_ANY) != 0)
4632  return ACLCHECK_OK;
4633  else
4634  return ACLCHECK_NO_PRIV;
4635 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
AclMode pg_class_aclmask(Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3830

◆ pg_class_aclmask()

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

Definition at line 3830 of file aclchk.c.

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

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

3832 {
3833  AclMode result;
3834  HeapTuple tuple;
3835  Form_pg_class classForm;
3836  Datum aclDatum;
3837  bool isNull;
3838  Acl *acl;
3839  Oid ownerId;
3840 
3841  /*
3842  * Must get the relation's tuple from pg_class
3843  */
3844  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
3845  if (!HeapTupleIsValid(tuple))
3846  ereport(ERROR,
3848  errmsg("relation with OID %u does not exist",
3849  table_oid)));
3850  classForm = (Form_pg_class) GETSTRUCT(tuple);
3851 
3852  /*
3853  * Deny anyone permission to update a system catalog unless
3854  * pg_authid.rolsuper is set.
3855  *
3856  * As of 7.4 we have some updatable system views; those shouldn't be
3857  * protected in this way. Assume the view rules can take care of
3858  * themselves. ACL_USAGE is if we ever have system sequences.
3859  */
3860  if ((mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE | ACL_USAGE)) &&
3861  IsSystemClass(table_oid, classForm) &&
3862  classForm->relkind != RELKIND_VIEW &&
3863  !superuser_arg(roleid))
3864  {
3865 #ifdef ACLDEBUG
3866  elog(DEBUG2, "permission denied for system catalog update");
3867 #endif
3869  }
3870 
3871  /*
3872  * Otherwise, superusers bypass all permission-checking.
3873  */
3874  if (superuser_arg(roleid))
3875  {
3876 #ifdef ACLDEBUG
3877  elog(DEBUG2, "OID %u is superuser, home free", roleid);
3878 #endif
3879  ReleaseSysCache(tuple);
3880  return mask;
3881  }
3882 
3883  /*
3884  * Normal case: get the relation's ACL from pg_class
3885  */
3886  ownerId = classForm->relowner;
3887 
3888  aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
3889  &isNull);
3890  if (isNull)
3891  {
3892  /* No ACL, so build default ACL */
3893  switch (classForm->relkind)
3894  {
3895  case RELKIND_SEQUENCE:
3896  acl = acldefault(OBJECT_SEQUENCE, ownerId);
3897  break;
3898  default:
3899  acl = acldefault(OBJECT_TABLE, ownerId);
3900  break;
3901  }
3902  aclDatum = (Datum) 0;
3903  }
3904  else
3905  {
3906  /* detoast rel's ACL if necessary */
3907  acl = DatumGetAclP(aclDatum);
3908  }
3909 
3910  result = aclmask(acl, roleid, ownerId, mask, how);
3911 
3912  /* if we have a detoasted copy, free it */
3913  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3914  pfree(acl);
3915 
3916  ReleaseSysCache(tuple);
3917 
3918  return result;
3919 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:74
#define DatumGetAclP(X)
Definition: acl.h:120
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1321
int errcode(int sqlerrcode)
Definition: elog.c:608
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:749
#define ACL_DELETE
Definition: parsenodes.h:77
unsigned int Oid
Definition: postgres_ext.h:31
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:81
uint32 AclMode
Definition: parsenodes.h:72
void pfree(void *pointer)
Definition: mcxt.c:1056
char * Pointer
Definition: c.h:336
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define DEBUG2
Definition: elog.h:24
#define ACL_USAGE
Definition: parsenodes.h:82
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
#define ACL_UPDATE
Definition: parsenodes.h:76
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define ACL_INSERT
Definition: parsenodes.h:74
#define DatumGetPointer(X)
Definition: postgres.h:549
FormData_pg_class * Form_pg_class
Definition: pg_class.h:150
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
#define ACL_TRUNCATE
Definition: parsenodes.h:78

◆ pg_class_ownercheck()

bool pg_class_ownercheck ( Oid  class_oid,
Oid  roleid 
)

Definition at line 4753 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(), ATPrepSetStatistics(), 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(), and vacuum_is_relation_owner().

4754 {
4755  HeapTuple tuple;
4756  Oid ownerId;
4757 
4758  /* Superusers bypass all permission checking. */
4759  if (superuser_arg(roleid))
4760  return true;
4761 
4762  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(class_oid));
4763  if (!HeapTupleIsValid(tuple))
4764  ereport(ERROR,
4766  errmsg("relation with OID %u does not exist", class_oid)));
4767 
4768  ownerId = ((Form_pg_class) GETSTRUCT(tuple))->relowner;
4769 
4770  ReleaseSysCache(tuple);
4771 
4772  return has_privs_of_role(roleid, ownerId);
4773 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:74
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4900
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_class * Form_pg_class
Definition: pg_class.h:150
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_collation_ownercheck()

bool pg_collation_ownercheck ( Oid  coll_oid,
Oid  roleid 
)

Definition at line 5197 of file aclchk.c.

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

Referenced by AlterCollation(), and check_object_ownership().

5198 {
5199  HeapTuple tuple;
5200  Oid ownerId;
5201 
5202  /* Superusers bypass all permission checking. */
5203  if (superuser_arg(roleid))
5204  return true;
5205 
5206  tuple = SearchSysCache1(COLLOID, ObjectIdGetDatum(coll_oid));
5207  if (!HeapTupleIsValid(tuple))
5208  ereport(ERROR,
5209  (errcode(ERRCODE_UNDEFINED_OBJECT),
5210  errmsg("collation with OID %u does not exist", coll_oid)));
5211 
5212  ownerId = ((Form_pg_collation) GETSTRUCT(tuple))->collowner;
5213 
5214  ReleaseSysCache(tuple);
5215 
5216  return has_privs_of_role(roleid, ownerId);
5217 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4900
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#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:822

◆ pg_conversion_ownercheck()

bool pg_conversion_ownercheck ( Oid  conv_oid,
Oid  roleid 
)

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

5224 {
5225  HeapTuple tuple;
5226  Oid ownerId;
5227 
5228  /* Superusers bypass all permission checking. */
5229  if (superuser_arg(roleid))
5230  return true;
5231 
5232  tuple = SearchSysCache1(CONVOID, ObjectIdGetDatum(conv_oid));
5233  if (!HeapTupleIsValid(tuple))
5234  ereport(ERROR,
5235  (errcode(ERRCODE_UNDEFINED_OBJECT),
5236  errmsg("conversion with OID %u does not exist", conv_oid)));
5237 
5238  ownerId = ((Form_pg_conversion) GETSTRUCT(tuple))->conowner;
5239 
5240  ReleaseSysCache(tuple);
5241 
5242  return has_privs_of_role(roleid, ownerId);
5243 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4900
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
FormData_pg_conversion * Form_pg_conversion
Definition: pg_conversion.h:61
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_database_aclcheck()

◆ pg_database_aclmask()

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

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

3927 {
3928  AclMode result;
3929  HeapTuple tuple;
3930  Datum aclDatum;
3931  bool isNull;
3932  Acl *acl;
3933  Oid ownerId;
3934 
3935  /* Superusers bypass all permission checking. */
3936  if (superuser_arg(roleid))
3937  return mask;
3938 
3939  /*
3940  * Get the database's ACL from pg_database
3941  */
3942  tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(db_oid));
3943  if (!HeapTupleIsValid(tuple))
3944  ereport(ERROR,
3945  (errcode(ERRCODE_UNDEFINED_DATABASE),
3946  errmsg("database with OID %u does not exist", db_oid)));
3947 
3948  ownerId = ((Form_pg_database) GETSTRUCT(tuple))->datdba;
3949 
3950  aclDatum = SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_datacl,
3951  &isNull);
3952  if (isNull)
3953  {
3954  /* No ACL, so build default ACL */
3955  acl = acldefault(OBJECT_DATABASE, ownerId);
3956  aclDatum = (Datum) 0;
3957  }
3958  else
3959  {
3960  /* detoast ACL if necessary */
3961  acl = DatumGetAclP(aclDatum);
3962  }
3963 
3964  result = aclmask(acl, roleid, ownerId, mask, how);
3965 
3966  /* if we have a detoasted copy, free it */
3967  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
3968  pfree(acl);
3969 
3970  ReleaseSysCache(tuple);
3971 
3972  return result;
3973 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
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:1321
int errcode(int sqlerrcode)
Definition: elog.c:608
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:749
unsigned int Oid
Definition: postgres_ext.h:31
uint32 AclMode
Definition: parsenodes.h:72
void pfree(void *pointer)
Definition: mcxt.c:1056
char * Pointer
Definition: c.h:336
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define DatumGetPointer(X)
Definition: postgres.h:549
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_database_ownercheck()

bool pg_database_ownercheck ( Oid  db_oid,
Oid  roleid 
)

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

5172 {
5173  HeapTuple tuple;
5174  Oid dba;
5175 
5176  /* Superusers bypass all permission checking. */
5177  if (superuser_arg(roleid))
5178  return true;
5179 
5180  tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(db_oid));
5181  if (!HeapTupleIsValid(tuple))
5182  ereport(ERROR,
5183  (errcode(ERRCODE_UNDEFINED_DATABASE),
5184  errmsg("database with OID %u does not exist", db_oid)));
5185 
5186  dba = ((Form_pg_database) GETSTRUCT(tuple))->datdba;
5187 
5188  ReleaseSysCache(tuple);
5189 
5190  return has_privs_of_role(roleid, dba);
5191 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4900
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_event_trigger_ownercheck()

bool pg_event_trigger_ownercheck ( Oid  et_oid,
Oid  roleid 
)

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

5145 {
5146  HeapTuple tuple;
5147  Oid ownerId;
5148 
5149  /* Superusers bypass all permission checking. */
5150  if (superuser_arg(roleid))
5151  return true;
5152 
5154  if (!HeapTupleIsValid(tuple))
5155  ereport(ERROR,
5156  (errcode(ERRCODE_UNDEFINED_OBJECT),
5157  errmsg("event trigger with OID %u does not exist",
5158  et_oid)));
5159 
5160  ownerId = ((Form_pg_event_trigger) GETSTRUCT(tuple))->evtowner;
5161 
5162  ReleaseSysCache(tuple);
5163 
5164  return has_privs_of_role(roleid, ownerId);
5165 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4900
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
FormData_pg_event_trigger * Form_pg_event_trigger
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_extension_ownercheck()

bool pg_extension_ownercheck ( Oid  ext_oid,
Oid  roleid 
)

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

5250 {
5251  Relation pg_extension;
5252  ScanKeyData entry[1];
5253  SysScanDesc scan;
5254  HeapTuple tuple;
5255  Oid ownerId;
5256 
5257  /* Superusers bypass all permission checking. */
5258  if (superuser_arg(roleid))
5259  return true;
5260 
5261  /* There's no syscache for pg_extension, so do it the hard way */
5262  pg_extension = table_open(ExtensionRelationId, AccessShareLock);
5263 
5264  ScanKeyInit(&entry[0],
5265  Anum_pg_extension_oid,
5266  BTEqualStrategyNumber, F_OIDEQ,
5267  ObjectIdGetDatum(ext_oid));
5268 
5269  scan = systable_beginscan(pg_extension,
5270  ExtensionOidIndexId, true,
5271  NULL, 1, entry);
5272 
5273  tuple = systable_getnext(scan);
5274  if (!HeapTupleIsValid(tuple))
5275  ereport(ERROR,
5276  (errcode(ERRCODE_UNDEFINED_OBJECT),
5277  errmsg("extension with OID %u does not exist", ext_oid)));
5278 
5279  ownerId = ((Form_pg_extension) GETSTRUCT(tuple))->extowner;
5280 
5281  systable_endscan(scan);
5282  table_close(pg_extension, AccessShareLock);
5283 
5284  return has_privs_of_role(roleid, ownerId);
5285 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:525
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4900
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:608
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:352
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:444
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
#define ExtensionOidIndexId
Definition: indexing.h:326
FormData_pg_extension * Form_pg_extension
Definition: pg_extension.h:50
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:822
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 4308 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().

4310 {
4311  AclMode result;
4312  HeapTuple tuple;
4313  Datum aclDatum;
4314  bool isNull;
4315  Acl *acl;
4316  Oid ownerId;
4317 
4319 
4320  /* Bypass permission checks for superusers */
4321  if (superuser_arg(roleid))
4322  return mask;
4323 
4324  /*
4325  * Must get the FDW's tuple from pg_foreign_data_wrapper
4326  */
4328  if (!HeapTupleIsValid(tuple))
4329  ereport(ERROR,
4330  (errcode(ERRCODE_UNDEFINED_OBJECT),
4331  errmsg("foreign-data wrapper with OID %u does not exist",
4332  fdw_oid)));
4333  fdwForm = (Form_pg_foreign_data_wrapper) GETSTRUCT(tuple);
4334 
4335  /*
4336  * Normal case: get the FDW's ACL from pg_foreign_data_wrapper
4337  */
4338  ownerId = fdwForm->fdwowner;
4339 
4340  aclDatum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID, tuple,
4341  Anum_pg_foreign_data_wrapper_fdwacl, &isNull);
4342  if (isNull)
4343  {
4344  /* No ACL, so build default ACL */
4345  acl = acldefault(OBJECT_FDW, ownerId);
4346  aclDatum = (Datum) 0;
4347  }
4348  else
4349  {
4350  /* detoast rel's ACL if necessary */
4351  acl = DatumGetAclP(aclDatum);
4352  }
4353 
4354  result = aclmask(acl, roleid, ownerId, mask, how);
4355 
4356  /* if we have a detoasted copy, free it */
4357  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
4358  pfree(acl);
4359 
4360  ReleaseSysCache(tuple);
4361 
4362  return result;
4363 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DatumGetAclP(X)
Definition: acl.h:120
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1321
int errcode(int sqlerrcode)
Definition: elog.c:608
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:749
unsigned int Oid
Definition: postgres_ext.h:31
uint32 AclMode
Definition: parsenodes.h:72
void pfree(void *pointer)
Definition: mcxt.c:1056
char * Pointer
Definition: c.h:336
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
#define DatumGetPointer(X)
Definition: postgres.h:549
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_foreign_data_wrapper_ownercheck()

bool pg_foreign_data_wrapper_ownercheck ( Oid  srv_oid,
Oid  roleid 
)

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

5091 {
5092  HeapTuple tuple;
5093  Oid ownerId;
5094 
5095  /* Superusers bypass all permission checking. */
5096  if (superuser_arg(roleid))
5097  return true;
5098 
5100  if (!HeapTupleIsValid(tuple))
5101  ereport(ERROR,
5102  (errcode(ERRCODE_UNDEFINED_OBJECT),
5103  errmsg("foreign-data wrapper with OID %u does not exist",
5104  srv_oid)));
5105 
5106  ownerId = ((Form_pg_foreign_data_wrapper) GETSTRUCT(tuple))->fdwowner;
5107 
5108  ReleaseSysCache(tuple);
5109 
5110  return has_privs_of_role(roleid, ownerId);
5111 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4900
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#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:822

◆ pg_foreign_server_aclcheck()

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

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

4730 {
4731  if (pg_foreign_server_aclmask(srv_oid, roleid, mode, ACLMASK_ANY) != 0)
4732  return ACLCHECK_OK;
4733  else
4734  return ACLCHECK_NO_PRIV;
4735 }
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:4370

◆ pg_foreign_server_aclmask()

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

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

4372 {
4373  AclMode result;
4374  HeapTuple tuple;
4375  Datum aclDatum;
4376  bool isNull;
4377  Acl *acl;
4378  Oid ownerId;
4379 
4380  Form_pg_foreign_server srvForm;
4381 
4382  /* Bypass permission checks for superusers */
4383  if (superuser_arg(roleid))
4384  return mask;
4385 
4386  /*
4387  * Must get the FDW's tuple from pg_foreign_data_wrapper
4388  */
4390  if (!HeapTupleIsValid(tuple))
4391  ereport(ERROR,
4392  (errcode(ERRCODE_UNDEFINED_OBJECT),
4393  errmsg("foreign server with OID %u does not exist",
4394  srv_oid)));
4395  srvForm = (Form_pg_foreign_server) GETSTRUCT(tuple);
4396 
4397  /*
4398  * Normal case: get the foreign server's ACL from pg_foreign_server
4399  */
4400  ownerId = srvForm->srvowner;
4401 
4402  aclDatum = SysCacheGetAttr(FOREIGNSERVEROID, tuple,
4403  Anum_pg_foreign_server_srvacl, &isNull);
4404  if (isNull)
4405  {
4406  /* No ACL, so build default ACL */
4407  acl = acldefault(OBJECT_FOREIGN_SERVER, ownerId);
4408  aclDatum = (Datum) 0;
4409  }
4410  else
4411  {
4412  /* detoast rel's ACL if necessary */
4413  acl = DatumGetAclP(aclDatum);
4414  }
4415 
4416  result = aclmask(acl, roleid, ownerId, mask, how);
4417 
4418  /* if we have a detoasted copy, free it */
4419  if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
4420  pfree(acl);
4421 
4422  ReleaseSysCache(tuple);
4423 
4424  return result;
4425 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DatumGetAclP(X)
Definition: acl.h:120
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1321
int errcode(int sqlerrcode)
Definition: elog.c:608
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:749
unsigned int Oid
Definition: postgres_ext.h:31
uint32 AclMode
Definition: parsenodes.h:72
void pfree(void *pointer)
Definition: mcxt.c:1056
char * Pointer
Definition: c.h:336
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
FormData_pg_foreign_server * Form_pg_foreign_server
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define DatumGetPointer(X)
Definition: postgres.h:549
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_foreign_server_ownercheck()

bool pg_foreign_server_ownercheck ( Oid  srv_oid,
Oid  roleid 
)

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

5118 {
5119  HeapTuple tuple;
5120  Oid ownerId;
5121 
5122  /* Superusers bypass all permission checking. */
5123  if (superuser_arg(roleid))
5124  return true;
5125 
5127  if (!HeapTupleIsValid(tuple))
5128  ereport(ERROR,
5129  (errcode(ERRCODE_UNDEFINED_OBJECT),
5130  errmsg("foreign server with OID %u does not exist",
5131  srv_oid)));
5132 
5133  ownerId = ((Form_pg_foreign_server) GETSTRUCT(tuple))->srvowner;
5134 
5135  ReleaseSysCache(tuple);
5136 
5137  return has_privs_of_role(roleid, ownerId);
5138 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4900
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
FormData_pg_foreign_server * Form_pg_foreign_server
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_language_aclcheck()

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

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

4666 {
4667  if (pg_language_aclmask(lang_oid, roleid, mode, ACLMASK_ANY) != 0)
4668  return ACLCHECK_OK;
4669  else
4670  return ACLCHECK_NO_PRIV;
4671 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
AclMode pg_language_aclmask(Oid lang_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4033

◆ pg_language_aclmask()

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

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

4035 {
4036  AclMode result;
4037  HeapTuple tuple;
4038  Datum aclDatum;
4039  bool isNull;
4040  Acl *acl;
4041  Oid ownerId;
4042 
4043  /* Superusers bypass all permission checking. */
4044  if (superuser_arg(roleid))
4045  return mask;
4046 
4047  /*
4048  * Get the language's ACL from pg_language