PostgreSQL Source Code  git master
acl.h File Reference
#include "access/htup.h"
#include "nodes/parsenodes.h"
#include "parser/parse_node.h"
#include "utils/array.h"
#include "utils/snapshot.h"
Include dependency graph for acl.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  AclItem
 

Macros

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

Referenced by aclparse().

◆ ACL_CREATE_CHR

#define ACL_CREATE_CHR   'C'

Definition at line 147 of file acl.h.

Referenced by aclparse().

◆ ACL_CREATE_TEMP_CHR

#define ACL_CREATE_TEMP_CHR   'T'

Definition at line 148 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 145 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 130 of file acl.h.

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

◆ ACL_MODECHG_DEL

#define ACL_MODECHG_DEL   2

Definition at line 131 of file acl.h.

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

◆ ACL_MODECHG_EQL

#define ACL_MODECHG_EQL   3

Definition at line 132 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 111 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 72 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 143 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 112 of file acl.h.

Referenced by aclupdate(), and check_circularity().

◆ ACL_TRIGGER_CHR

#define ACL_TRIGGER_CHR   't'

Definition at line 144 of file acl.h.

Referenced by aclparse().

◆ ACL_TRUNCATE_CHR

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

Definition at line 142 of file acl.h.

Referenced by aclparse().

◆ ACL_UPDATE_CHR

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

Definition at line 140 of file acl.h.

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

◆ ACL_USAGE_CHR

#define ACL_USAGE_CHR   'U'

Definition at line 146 of file acl.h.

Referenced by aclparse().

◆ ACLITEM_ALL_GOPTION_BITS

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

Definition at line 89 of file acl.h.

Referenced by aclmask(), and aclmask_direct().

◆ ACLITEM_ALL_PRIV_BITS

#define ACLITEM_ALL_PRIV_BITS   ((AclMode) 0xFFFF)

Definition at line 88 of file acl.h.

◆ ACLITEM_GET_GOPTIONS

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

Definition at line 68 of file acl.h.

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

◆ ACLITEM_GET_PRIVS

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

Definition at line 67 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 69 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 77 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 74 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 83 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 80 of file acl.h.

Referenced by aclnewowner(), and aclupdate().

◆ DatumGetAclItemP

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

Definition at line 117 of file acl.h.

◆ DatumGetAclP

◆ DatumGetAclPCopy

◆ PG_GETARG_ACL_P

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

Definition at line 123 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 124 of file acl.h.

◆ PG_GETARG_ACLITEM_P

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

Definition at line 118 of file acl.h.

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

◆ PG_RETURN_ACL_P

#define PG_RETURN_ACL_P (   x)    PG_RETURN_POINTER(x)

Definition at line 125 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 119 of file acl.h.

Referenced by aclitemin(), and makeaclitem().

Typedef Documentation

◆ Acl

Definition at line 107 of file acl.h.

◆ AclItem

Enumeration Type Documentation

◆ AclMaskHow

enum AclMaskHow
Enumerator
ACLMASK_ALL 
ACLMASK_ANY 

Definition at line 171 of file acl.h.

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

◆ AclResult

enum AclResult
Enumerator
ACLCHECK_OK 
ACLCHECK_NO_PRIV 
ACLCHECK_NOT_OWNER 

Definition at line 178 of file acl.h.

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

Function Documentation

◆ aclcheck_error()

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

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

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

◆ aclcheck_error_col()

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

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

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

◆ aclcheck_error_type()

void aclcheck_error_type ( AclResult  aclerr,
Oid  typeOid 
)

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

3663 {
3664  Oid element_type = get_element_type(typeOid);
3665 
3666  aclcheck_error(aclerr, OBJECT_TYPE, format_type_be(element_type ? element_type : typeOid));
3667 }
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2502
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3349

◆ aclconcat()

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

Definition at line 427 of file acl.c.

References ACL_DAT, ACL_NUM, and allocacl().

Referenced by ExecGrant_Attribute().

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

◆ aclcopy()

Acl* aclcopy ( const Acl orig_acl)

Definition at line 407 of file acl.c.

References ACL_DAT, ACL_NUM, and allocacl().

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

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

◆ acldefault()

Acl* acldefault ( ObjectType  objtype,
Oid  ownerId 
)

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

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

◆ aclequal()

bool aclequal ( const Acl left_acl,
const Acl right_acl 
)

Definition at line 509 of file acl.c.

References ACL_DAT, and ACL_NUM.

Referenced by get_user_default_acl(), and SetDefaultACL().

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

◆ aclitemsort()

void aclitemsort ( Acl acl)

Definition at line 495 of file acl.c.

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

Referenced by get_user_default_acl(), and SetDefaultACL().

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

◆ 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:692
Oid ai_grantee
Definition: acl.h:57
#define ACLITEM_ALL_GOPTION_BITS
Definition: acl.h:89
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4833
AclMode ai_privs
Definition: acl.h:59
uint32 AclMode
Definition: parsenodes.h:72
#define ERROR
Definition: elog.h:43
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
int i
#define ACL_ID_PUBLIC
Definition: acl.h:47
#define elog
Definition: elog.h:219
static void check_acl(const Acl *acl)
Definition: acl.c:540

◆ 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(), and qsort.

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

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

◆ aclmerge()

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

Definition at line 451 of file acl.c.

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

Referenced by get_user_default_acl().

452 {
453  Acl *result_acl;
454  AclItem *aip;
455  int i,
456  num;
457 
458  /* Check for cases where one or both are empty/null */
459  if (left_acl == NULL || ACL_NUM(left_acl) == 0)
460  {
461  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
462  return NULL;
463  else
464  return aclcopy(right_acl);
465  }
466  else
467  {
468  if (right_acl == NULL || ACL_NUM(right_acl) == 0)
469  return aclcopy(left_acl);
470  }
471 
472  /* Merge them the hard way, one item at a time */
473  result_acl = aclcopy(left_acl);
474 
475  aip = ACL_DAT(right_acl);
476  num = ACL_NUM(right_acl);
477 
478  for (i = 0; i < num; i++, aip++)
479  {
480  Acl *tmp_acl;
481 
482  tmp_acl = aclupdate(result_acl, aip, ACL_MODECHG_ADD,
483  ownerId, DROP_RESTRICT);
484  pfree(result_acl);
485  result_acl = tmp_acl;
486  }
487 
488  return result_acl;
489 }
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_MODECHG_ADD
Definition: acl.h:130
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
Acl * aclcopy(const Acl *orig_acl)
Definition: acl.c:407
int i
Acl * aclupdate(const Acl *old_acl, const AclItem *mod_aip, int modechg, Oid ownerId, DropBehavior behavior)
Definition: acl.c: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:57
static Acl * allocacl(int n)
Definition: acl.c:376
Oid ai_grantor
Definition: acl.h:58
#define ARR_DIMS(a)
Definition: array.h:279
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
#define ACLITEM_GET_RIGHTS(item)
Definition: acl.h:69
#define ACLITEM_SET_RIGHTS(item, rights)
Definition: acl.h:80
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
static bool aclitem_match(const AclItem *a1, const AclItem *a2)
Definition: acl.c:658
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define ACL_N_SIZE(N)
Definition: acl.h:111
static void check_acl(const Acl *acl)
Definition: acl.c:540

◆ 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:57
#define ACL_MODECHG_EQL
Definition: acl.h:132
#define ACLITEM_GET_GOPTIONS(item)
Definition: acl.h:68
static Acl * allocacl(int n)
Definition: acl.c:376
#define ACL_MODECHG_DEL
Definition: acl.h:131
#define ACL_SIZE(ACL)
Definition: acl.h:112
Oid ai_grantor
Definition: acl.h:58
uint32 AclMode
Definition: parsenodes.h: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:279
#define ACL_NUM(ACL)
Definition: acl.h:109
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
#define memmove(d, s, c)
Definition: c.h:1135
#define ACLITEM_GET_RIGHTS(item)
Definition: acl.h:69
#define ACLITEM_SET_RIGHTS(item, rights)
Definition: acl.h:80
#define ACL_MODECHG_ADD
Definition: acl.h:130
#define ACL_DAT(ACL)
Definition: acl.h:110
Definition: acl.h:55
#define Assert(condition)
Definition: c.h:699
static bool aclitem_match(const AclItem *a1, const AclItem *a2)
Definition: acl.c:658
static Acl * recursive_revoke(Acl *acl, Oid grantee, AclMode revoke_privs, Oid ownerId, DropBehavior behavior)
Definition: acl.c:1235
#define ACL_ID_PUBLIC
Definition: acl.h:47
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define ACL_N_SIZE(N)
Definition: acl.h:111
#define ACLITEM_SET_PRIVS_GOPTIONS(item, privs, goptions)
Definition: acl.h:83
static void check_acl(const Acl *acl)
Definition: acl.c:540

◆ check_is_member_of_role()

void check_is_member_of_role ( Oid  member,
Oid  role 
)

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

4880 {
4881  if (!is_member_of_role(member, role))
4882  ereport(ERROR,
4883  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4884  errmsg("must be member of role \"%s\"",
4885  GetUserNameFromId(role, false))));
4886 }
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid member
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4857
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:787
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ check_rolespec_name()

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

Definition at line 5254 of file acl.c.

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

Referenced by AlterRole(), and AlterRoleSet().

5255 {
5256  if (!role)
5257  return;
5258 
5259  if (role->roletype != ROLESPEC_CSTRING)
5260  return;
5261 
5262  if (IsReservedName(role->rolename))
5263  {
5264  if (detail_msg)
5265  ereport(ERROR,
5266  (errcode(ERRCODE_RESERVED_NAME),
5267  errmsg("role name \"%s\" is reserved",
5268  role->rolename),
5269  errdetail("%s", detail_msg)));
5270  else
5271  ereport(ERROR,
5272  (errcode(ERRCODE_RESERVED_NAME),
5273  errmsg("role name \"%s\" is reserved",
5274  role->rolename)));
5275  }
5276 }
int errcode(int sqlerrcode)
Definition: elog.c:575
bool IsReservedName(const char *name)
Definition: catalog.c:194
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
RoleSpecType roletype
Definition: parsenodes.h:329
char * rolename
Definition: parsenodes.h:330
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ ExecAlterDefaultPrivilegesStmt()

void ExecAlterDefaultPrivilegesStmt ( ParseState pstate,
AlterDefaultPrivilegesStmt stmt 
)

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

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

◆ ExecuteGrantStmt()

void ExecuteGrantStmt ( GrantStmt stmt)

Definition at line 385 of file aclchk.c.

References ACL_ALL_RIGHTS_DATABASE, ACL_ALL_RIGHTS_FDW, ACL_ALL_RIGHTS_FOREIGN_SERVER, ACL_ALL_RIGHTS_FUNCTION, ACL_ALL_RIGHTS_LANGUAGE, ACL_ALL_RIGHTS_LARGEOBJECT, ACL_ALL_RIGHTS_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().

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

◆ get_role_oid()

Oid get_role_oid ( const char *  rolename,
bool  missing_ok 
)

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

5117 {
5118  Oid oid;
5119 
5120  oid = GetSysCacheOid1(AUTHNAME, CStringGetDatum(rolname));
5121  if (!OidIsValid(oid) && !missing_ok)
5122  ereport(ERROR,
5123  (errcode(ERRCODE_UNDEFINED_OBJECT),
5124  errmsg("role \"%s\" does not exist", rolname)));
5125  return oid;
5126 }
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
#define GetSysCacheOid1(cacheId, key1)
Definition: syscache.h:191
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:563
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ get_role_oid_or_public()

◆ get_rolespec_name()

char* get_rolespec_name ( const RoleSpec role)

Definition at line 5232 of file acl.c.

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

Referenced by AddRoleMems(), and DelRoleMems().

5233 {
5234  HeapTuple tp;
5235  Form_pg_authid authForm;
5236  char *rolename;
5237 
5238  tp = get_rolespec_tuple(role);
5239  authForm = (Form_pg_authid) GETSTRUCT(tp);
5240  rolename = pstrdup(NameStr(authForm->rolname));
5241  ReleaseSysCache(tp);
5242 
5243  return rolename;
5244 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
char * pstrdup(const char *in)
Definition: mcxt.c:1161
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:55
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
HeapTuple get_rolespec_tuple(const RoleSpec *role)
Definition: acl.c:5187
#define NameStr(name)
Definition: c.h:576

◆ get_rolespec_oid()

Oid get_rolespec_oid ( const RoleSpec role,
bool  missing_ok 
)

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

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

◆ get_rolespec_tuple()

HeapTuple get_rolespec_tuple ( const RoleSpec role)

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

5188 {
5189  HeapTuple tuple;
5190 
5191  switch (role->roletype)
5192  {
5193  case ROLESPEC_CSTRING:
5194  Assert(role->rolename);
5196  if (!HeapTupleIsValid(tuple))
5197  ereport(ERROR,
5198  (errcode(ERRCODE_UNDEFINED_OBJECT),
5199  errmsg("role \"%s\" does not exist", role->rolename)));
5200  break;
5201 
5202  case ROLESPEC_CURRENT_USER:
5203  tuple = SearchSysCache1(AUTHOID, GetUserId());
5204  if (!HeapTupleIsValid(tuple))
5205  elog(ERROR, "cache lookup failed for role %u", GetUserId());
5206  break;
5207 
5208  case ROLESPEC_SESSION_USER:
5210  if (!HeapTupleIsValid(tuple))
5211  elog(ERROR, "cache lookup failed for role %u", GetSessionUserId());
5212  break;
5213 
5214  case ROLESPEC_PUBLIC:
5215  ereport(ERROR,
5216  (errcode(ERRCODE_UNDEFINED_OBJECT),
5217  errmsg("role \"%s\" does not exist", "public")));
5218  tuple = NULL; /* make compiler happy */
5219  break;
5220 
5221  default:
5222  elog(ERROR, "unexpected role type %d", role->roletype);
5223  }
5224 
5225  return tuple;
5226 }
Oid GetUserId(void)
Definition: miscinit.c:379
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid GetSessionUserId(void)
Definition: miscinit.c:413
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:563
#define ereport(elevel, rest)
Definition: elog.h:122
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
RoleSpecType roletype
Definition: parsenodes.h:329
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:699
char * rolename
Definition: parsenodes.h:330
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219

◆ get_user_default_acl()

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

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

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

◆ has_bypassrls_privilege()

bool has_bypassrls_privilege ( Oid  roleid)

Definition at line 5395 of file aclchk.c.

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

Referenced by check_enable_rls(), and RI_Initial_Check().

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

◆ has_createrole_privilege()

bool has_createrole_privilege ( Oid  roleid)

Definition at line 5376 of file aclchk.c.

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

Referenced by check_object_ownership(), and have_createrole_privilege().

5377 {
5378  bool result = false;
5379  HeapTuple utup;
5380 
5381  /* Superusers bypass all permission checking. */
5382  if (superuser_arg(roleid))
5383  return true;
5384 
5385  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5386  if (HeapTupleIsValid(utup))
5387  {
5388  result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreaterole;
5389  ReleaseSysCache(utup);
5390  }
5391  return result;
5392 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:55
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
bool rolcreaterole
Definition: pg_authid.h:36
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define HeapTupleIsValid(tuple)
Definition: htup.h:78

◆ has_privs_of_role()

bool has_privs_of_role ( Oid  member,
Oid  role 
)

Definition at line 4833 of file acl.c.

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

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

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

◆ initialize_acl()

void initialize_acl ( void  )

Definition at line 4614 of file acl.c.

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

Referenced by InitPostgres().

4615 {
4617  {
4618  /*
4619  * In normal mode, set a callback on any syscache invalidation of
4620  * pg_auth_members rows
4621  */
4624  (Datum) 0);
4625  }
4626 }
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
uintptr_t Datum
Definition: postgres.h:367
static void RoleMembershipCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: acl.c:4633
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:372

◆ is_admin_of_role()

bool is_admin_of_role ( Oid  member,
Oid  role 
)

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

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

◆ is_member_of_role()

bool is_member_of_role ( Oid  member,
Oid  role 
)

Definition at line 4857 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(), 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(), and pgrowlocks().

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

◆ is_member_of_role_nosuper()

bool is_member_of_role_nosuper ( Oid  member,
Oid  role 
)

Definition at line 4895 of file acl.c.

References list_member_oid(), and roles_is_member_of().

Referenced by AddRoleMems(), and is_member().

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

◆ make_empty_acl()

Acl* make_empty_acl ( void  )

Definition at line 398 of file acl.c.

References allocacl().

Referenced by SetDefaultACL().

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

◆ pg_attribute_aclcheck()

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

Definition at line 4513 of file aclchk.c.

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

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

4515 {
4516  if (pg_attribute_aclmask(table_oid, attnum, roleid, mode, ACLMASK_ANY) != 0)
4517  return ACLCHECK_OK;
4518  else
4519  return ACLCHECK_NO_PRIV;
4520 }
AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3742
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 4542 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(), and has_any_column_privilege_name_name().

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

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

◆ pg_class_aclcheck()

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

Definition at line 4627 of file aclchk.c.

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

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

4628 {
4629  if (pg_class_aclmask(table_oid, roleid, mode, ACLMASK_ANY) != 0)
4630  return ACLCHECK_OK;
4631  else
4632  return ACLCHECK_NO_PRIV;
4633 }
AclMode pg_class_aclmask(Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3827

◆ pg_class_aclmask()

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

Definition at line 3827 of file aclchk.c.

References ACL_DELETE, ACL_INSERT, ACL_TRUNCATE, ACL_UPDATE, ACL_USAGE, acldefault(), aclmask(), allowSystemTableMods, 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().

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

◆ pg_class_ownercheck()

bool pg_class_ownercheck ( Oid  class_oid,
Oid  roleid 
)

Definition at line 4751 of file aclchk.c.

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

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

4752 {
4753  HeapTuple tuple;
4754  Oid ownerId;
4755 
4756  /* Superusers bypass all permission checking. */
4757  if (superuser_arg(roleid))
4758  return true;
4759 
4760  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(class_oid));
4761  if (!HeapTupleIsValid(tuple))
4762  ereport(ERROR,
4764  errmsg("relation with OID %u does not exist", class_oid)));
4765 
4766  ownerId = ((Form_pg_class) GETSTRUCT(tuple))->relowner;
4767 
4768  ReleaseSysCache(tuple);
4769 
4770  return has_privs_of_role(roleid, ownerId);
4771 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:62
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4833
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_class * Form_pg_class
Definition: pg_class.h:93
int errmsg(const char *fmt,...)
Definition: elog.c:797
Oid relowner
Definition: pg_class.h:37

◆ pg_collation_ownercheck()

bool pg_collation_ownercheck ( Oid  coll_oid,
Oid  roleid 
)

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

5196 {
5197  HeapTuple tuple;
5198  Oid ownerId;
5199 
5200  /* Superusers bypass all permission checking. */
5201  if (superuser_arg(roleid))
5202  return true;
5203 
5204  tuple = SearchSysCache1(COLLOID, ObjectIdGetDatum(coll_oid));
5205  if (!HeapTupleIsValid(tuple))
5206  ereport(ERROR,
5207  (errcode(ERRCODE_UNDEFINED_OBJECT),
5208  errmsg("collation with OID %u does not exist", coll_oid)));
5209 
5210  ownerId = ((Form_pg_collation) GETSTRUCT(tuple))->collowner;
5211 
5212  ReleaseSysCache(tuple);
5213 
5214  return has_privs_of_role(roleid, ownerId);
5215 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4833
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:49
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ pg_conversion_ownercheck()

bool pg_conversion_ownercheck ( Oid  conv_oid,
Oid  roleid 
)

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

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

◆ pg_database_aclcheck()

◆ pg_database_aclmask()

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

Definition at line 3923 of file aclchk.c.

References acldefault(), aclmask(), DATABASEOID, datdba, 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().

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

◆ pg_database_ownercheck()

bool pg_database_ownercheck ( Oid  db_oid,
Oid  roleid 
)

Definition at line 5169 of file aclchk.c.

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

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

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

◆ pg_event_trigger_ownercheck()

bool pg_event_trigger_ownercheck ( Oid  et_oid,
Oid  roleid 
)

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

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

◆ pg_extension_ownercheck()

bool pg_extension_ownercheck ( Oid  ext_oid,
Oid  roleid 
)

Definition at line 5247 of file aclchk.c.

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

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

5248 {
5249  Relation pg_extension;
5250  ScanKeyData entry[1];
5251  SysScanDesc scan;
5252  HeapTuple tuple;
5253  Oid ownerId;
5254 
5255  /* Superusers bypass all permission checking. */
5256  if (superuser_arg(roleid))
5257  return true;
5258 
5259  /* There's no syscache for pg_extension, so do it the hard way */
5260  pg_extension = heap_open(ExtensionRelationId, AccessShareLock);
5261 
5262  ScanKeyInit(&entry[0],
5264  BTEqualStrategyNumber, F_OIDEQ,
5265  ObjectIdGetDatum(ext_oid));
5266 
5267  scan = systable_beginscan(pg_extension,
5268  ExtensionOidIndexId, true,
5269  NULL, 1, entry);
5270 
5271  tuple = systable_getnext(scan);
5272  if (!HeapTupleIsValid(tuple))
5273  ereport(ERROR,
5274  (errcode(ERRCODE_UNDEFINED_OBJECT),
5275  errmsg("extension with OID %u does not exist", ext_oid)));
5276 
5277  ownerId = ((Form_pg_extension) GETSTRUCT(tuple))->extowner;
5278 
5279  systable_endscan(scan);
5280  heap_close(pg_extension, AccessShareLock);
5281 
5282  return has_privs_of_role(roleid, ownerId);
5283 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:502
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4833
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:331
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:419
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
#define ExtensionOidIndexId
Definition: indexing.h:320
FormData_pg_extension * Form_pg_extension
Definition: pg_extension.h:49
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:797
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31

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

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

◆ pg_foreign_data_wrapper_ownercheck()

bool pg_foreign_data_wrapper_ownercheck ( Oid  srv_oid,
Oid  roleid 
)

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

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

◆ pg_foreign_server_aclcheck()

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

◆ pg_foreign_server_aclmask()

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

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

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

◆ pg_foreign_server_ownercheck()

bool pg_foreign_server_ownercheck ( Oid  srv_oid,
Oid  roleid 
)

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

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

◆ pg_language_aclcheck()

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

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

4664 {
4665  if (pg_language_aclmask(lang_oid, roleid, mode, ACLMASK_ANY) != 0)
4666  return ACLCHECK_OK;
4667  else
4668  return ACLCHECK_NO_PRIV;
4669 }
AclMode pg_language_aclmask(Oid lang_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4031

◆ pg_language_aclmask()

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

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

4033 {
4034  AclMode result;
4035  HeapTuple tuple;
4036  Datum aclDatum;
4037  bool isNull;
4038  Acl *acl;
4039  Oid ownerId;
4040 
4041  /* Superusers bypass all permission checking. */
4042  if (superuser_arg(roleid))
4043  return mask;
4044 
4045  /*
4046  * Get the language's ACL from pg_language
4047  */
4048  tuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(lang_oid));
4049  if (!HeapTupleIsValid(tuple))
4050  ereport(ERROR,
4051  (errcode(ERRCODE_UNDEFINED_OBJECT),
4052  errmsg("language with OID %u does not exist", lang_oid)));
4053 
4054  ownerId = ((Form_pg_language) GETSTRUCT(tuple))->lanowner;
4055 
4056  aclDatum = SysCacheGetAttr(LANGOID, tuple, Anum_pg_language_lanacl,
4057  &isNull);
4058  if (isNull)
4059  {
4060  /* No ACL, so build default ACL */
4061  acl = acldefault(OBJECT_LANGUAGE, ownerId);
4062  aclDatum = (Datum) 0;
4063  }
4064  else
4065  {
4066  /* detoast ACL if necessary */
4067  acl = DatumGetAclP(aclDatum);
4068  }
4069 
4070  result = aclmask(acl, roleid, ownerId, mask, how);
4071 
4072  /* if we have