PostgreSQL Source Code: src/include/utils/acl.h Source File

PostgreSQL Source Code git master
acl.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * acl.h
4 * Definition of (and support for) access control list data structures.
5 *
6 *
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/include/utils/acl.h
11 *
12 * NOTES
13 * An ACL array is simply an array of AclItems, representing the union
14 * of the privileges represented by the individual items. A zero-length
15 * array represents "no privileges".
16 *
17 * The order of items in the array is important as client utilities (in
18 * particular, pg_dump, though possibly other clients) expect to be able
19 * to issue GRANTs in the ordering of the items in the array. The reason
20 * this matters is that GRANTs WITH GRANT OPTION must be before any GRANTs
21 * which depend on it. This happens naturally in the backend during
22 * operations as we update ACLs in-place, new items are appended, and
23 * existing entries are only removed if there's no dependency on them (no
24 * GRANT can been based on it, or, if there was, those GRANTs are also
25 * removed).
26 *
27 * For backward-compatibility purposes we have to allow null ACL entries
28 * in system catalogs. A null ACL will be treated as meaning "default
29 * protection" (i.e., whatever acldefault() returns).
30 *-------------------------------------------------------------------------
31 */
32#ifndef ACL_H
33#define ACL_H
34
35#include "access/htup.h"
36#include "nodes/parsenodes.h"
37#include "parser/parse_node.h"
38#include "utils/snapshot.h"
39
40
41/*
42 * typedef AclMode is declared in parsenodes.h, also the individual privilege
43 * bit meanings are defined there
44 */
45
46 #define ACL_ID_PUBLIC 0 /* placeholder for id in a PUBLIC acl item */
47
48/*
49 * AclItem
50 *
51 * Note: must be same size on all platforms, because the size is hardcoded
52 * in the pg_type.h entry for aclitem.
53 */
54 typedef struct AclItem
55{
56 Oid ai_grantee; /* ID that this item grants privs to */
57 Oid ai_grantor; /* grantor of privs */
58 AclMode ai_privs; /* privilege bits */
59 } AclItem;
60
61/*
62 * The upper 32 bits of the ai_privs field of an AclItem are the grant option
63 * bits, and the lower 32 bits are the actual privileges. We use "rights"
64 * to mean the combined grant option and privilege bits fields.
65 */
66 #define ACLITEM_GET_PRIVS(item) ((item).ai_privs & 0xFFFFFFFF)
67 #define ACLITEM_GET_GOPTIONS(item) (((item).ai_privs >> 32) & 0xFFFFFFFF)
68 #define ACLITEM_GET_RIGHTS(item) ((item).ai_privs)
69
70 #define ACL_GRANT_OPTION_FOR(privs) (((AclMode) (privs) & 0xFFFFFFFF) << 32)
71 #define ACL_OPTION_TO_PRIVS(privs) (((AclMode) (privs) >> 32) & 0xFFFFFFFF)
72
73 #define ACLITEM_SET_PRIVS(item,privs) \
74 ((item).ai_privs = ((item).ai_privs & ~((AclMode) 0xFFFFFFFF)) | \
75 ((AclMode) (privs) & 0xFFFFFFFF))
76 #define ACLITEM_SET_GOPTIONS(item,goptions) \
77 ((item).ai_privs = ((item).ai_privs & ~(((AclMode) 0xFFFFFFFF) << 32)) | \
78 (((AclMode) (goptions) & 0xFFFFFFFF) << 32))
79 #define ACLITEM_SET_RIGHTS(item,rights) \
80 ((item).ai_privs = (AclMode) (rights))
81
82 #define ACLITEM_SET_PRIVS_GOPTIONS(item,privs,goptions) \
83 ((item).ai_privs = ((AclMode) (privs) & 0xFFFFFFFF) | \
84 (((AclMode) (goptions) & 0xFFFFFFFF) << 32))
85
86
87 #define ACLITEM_ALL_PRIV_BITS ((AclMode) 0xFFFFFFFF)
88 #define ACLITEM_ALL_GOPTION_BITS ((AclMode) 0xFFFFFFFF << 32)
89
90/*
91 * Definitions for convenient access to Acl (array of AclItem).
92 * These are standard PostgreSQL arrays, but are restricted to have one
93 * dimension and no nulls. We also ignore the lower bound when reading,
94 * and set it to one when writing.
95 *
96 * CAUTION: as of PostgreSQL 7.1, these arrays are toastable (just like all
97 * other array types). Therefore, be careful to detoast them with the
98 * macros provided, unless you know for certain that a particular array
99 * can't have been toasted.
100 */
101
102
103/*
104 * Acl a one-dimensional array of AclItem
105 */
106 typedef struct ArrayType Acl;
107
108 #define ACL_NUM(ACL) (ARR_DIMS(ACL)[0])
109 #define ACL_DAT(ACL) ((AclItem *) ARR_DATA_PTR(ACL))
110 #define ACL_N_SIZE(N) (ARR_OVERHEAD_NONULLS(1) + ((N) * sizeof(AclItem)))
111 #define ACL_SIZE(ACL) ARR_SIZE(ACL)
112
113/*
114 * fmgr macros for these types
115 */
116 #define DatumGetAclItemP(X) ((AclItem *) DatumGetPointer(X))
117 #define PG_GETARG_ACLITEM_P(n) DatumGetAclItemP(PG_GETARG_DATUM(n))
118 #define PG_RETURN_ACLITEM_P(x) PG_RETURN_POINTER(x)
119
120 #define DatumGetAclP(X) ((Acl *) PG_DETOAST_DATUM(X))
121 #define DatumGetAclPCopy(X) ((Acl *) PG_DETOAST_DATUM_COPY(X))
122 #define PG_GETARG_ACL_P(n) DatumGetAclP(PG_GETARG_DATUM(n))
123 #define PG_GETARG_ACL_P_COPY(n) DatumGetAclPCopy(PG_GETARG_DATUM(n))
124 #define PG_RETURN_ACL_P(x) PG_RETURN_POINTER(x)
125
126/*
127 * ACL modification opcodes for aclupdate
128 */
129 #define ACL_MODECHG_ADD 1
130 #define ACL_MODECHG_DEL 2
131 #define ACL_MODECHG_EQL 3
132
133/*
134 * External representations of the privilege bits --- aclitemin/aclitemout
135 * represent each possible privilege bit with a distinct 1-character code
136 */
137 #define ACL_INSERT_CHR 'a' /* formerly known as "append" */
138 #define ACL_SELECT_CHR 'r' /* formerly known as "read" */
139 #define ACL_UPDATE_CHR 'w' /* formerly known as "write" */
140 #define ACL_DELETE_CHR 'd'
141 #define ACL_TRUNCATE_CHR 'D' /* super-delete, as it were */
142 #define ACL_REFERENCES_CHR 'x'
143 #define ACL_TRIGGER_CHR 't'
144 #define ACL_EXECUTE_CHR 'X'
145 #define ACL_USAGE_CHR 'U'
146 #define ACL_CREATE_CHR 'C'
147 #define ACL_CREATE_TEMP_CHR 'T'
148 #define ACL_CONNECT_CHR 'c'
149 #define ACL_SET_CHR 's'
150 #define ACL_ALTER_SYSTEM_CHR 'A'
151 #define ACL_MAINTAIN_CHR 'm'
152
153/* string holding all privilege code chars, in order by bitmask position */
154 #define ACL_ALL_RIGHTS_STR "arwdDxtXUCTcsAm"
155
156/*
157 * Bitmasks defining "all rights" for each supported object type
158 */
159 #define ACL_ALL_RIGHTS_COLUMN (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_REFERENCES)
160 #define ACL_ALL_RIGHTS_RELATION (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_DELETE|ACL_TRUNCATE|ACL_REFERENCES|ACL_TRIGGER|ACL_MAINTAIN)
161 #define ACL_ALL_RIGHTS_SEQUENCE (ACL_USAGE|ACL_SELECT|ACL_UPDATE)
162 #define ACL_ALL_RIGHTS_DATABASE (ACL_CREATE|ACL_CREATE_TEMP|ACL_CONNECT)
163 #define ACL_ALL_RIGHTS_FDW (ACL_USAGE)
164 #define ACL_ALL_RIGHTS_FOREIGN_SERVER (ACL_USAGE)
165 #define ACL_ALL_RIGHTS_FUNCTION (ACL_EXECUTE)
166 #define ACL_ALL_RIGHTS_LANGUAGE (ACL_USAGE)
167 #define ACL_ALL_RIGHTS_LARGEOBJECT (ACL_SELECT|ACL_UPDATE)
168 #define ACL_ALL_RIGHTS_PARAMETER_ACL (ACL_SET|ACL_ALTER_SYSTEM)
169 #define ACL_ALL_RIGHTS_SCHEMA (ACL_USAGE|ACL_CREATE)
170 #define ACL_ALL_RIGHTS_TABLESPACE (ACL_CREATE)
171 #define ACL_ALL_RIGHTS_TYPE (ACL_USAGE)
172
173/* operation codes for pg_*_aclmask */
174 typedef enum
175{
176 ACLMASK_ALL, /* normal case: compute all bits */
177 ACLMASK_ANY, /* return when result is known nonzero */
178} AclMaskHow;
179
180/* result codes for pg_*_aclcheck */
181 typedef enum
182{
183 ACLCHECK_OK = 0,
184 ACLCHECK_NO_PRIV,
185 ACLCHECK_NOT_OWNER,
186} AclResult;
187
188
189/*
190 * routines used internally
191 */
192extern Acl *acldefault(ObjectType objtype, Oid ownerId);
193extern Acl *get_user_default_acl(ObjectType objtype, Oid ownerId,
194 Oid nsp_oid);
195extern void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId,
196 Oid ownerId, Acl *acl);
197
198extern Acl *aclupdate(const Acl *old_acl, const AclItem *mod_aip,
199 int modechg, Oid ownerId, DropBehavior behavior);
200extern Acl *aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId);
201extern Acl *make_empty_acl(void);
202extern Acl *aclcopy(const Acl *orig_acl);
203extern Acl *aclconcat(const Acl *left_acl, const Acl *right_acl);
204extern Acl *aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId);
205extern void aclitemsort(Acl *acl);
206extern bool aclequal(const Acl *left_acl, const Acl *right_acl);
207
208extern AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId,
209 AclMode mask, AclMaskHow how);
210extern int aclmembers(const Acl *acl, Oid **roleids);
211
212extern bool has_privs_of_role(Oid member, Oid role);
213extern bool member_can_set_role(Oid member, Oid role);
214extern void check_can_set_role(Oid member, Oid role);
215extern bool is_member_of_role(Oid member, Oid role);
216extern bool is_member_of_role_nosuper(Oid member, Oid role);
217extern bool is_admin_of_role(Oid member, Oid role);
218extern Oid select_best_admin(Oid member, Oid role);
219extern Oid get_role_oid(const char *rolname, bool missing_ok);
220extern Oid get_role_oid_or_public(const char *rolname);
221extern Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok);
222extern void check_rolespec_name(const RoleSpec *role, const char *detail_msg);
223extern HeapTuple get_rolespec_tuple(const RoleSpec *role);
224extern char *get_rolespec_name(const RoleSpec *role);
225
226extern void select_best_grantor(Oid roleId, AclMode privileges,
227 const Acl *acl, Oid ownerId,
228 Oid *grantorId, AclMode *grantOptions);
229
230extern void initialize_acl(void);
231
232/*
233 * prototypes for functions in aclchk.c
234 */
235extern void ExecuteGrantStmt(GrantStmt *stmt);
236extern void ExecAlterDefaultPrivilegesStmt(ParseState *pstate, AlterDefaultPrivilegesStmt *stmt);
237
238extern void RemoveRoleFromObjectACL(Oid roleid, Oid classid, Oid objid);
239
240extern AclMode pg_class_aclmask(Oid table_oid, Oid roleid,
241 AclMode mask, AclMaskHow how);
242
243/* generic functions */
244extern AclResult object_aclcheck(Oid classid, Oid objectid,
245 Oid roleid, AclMode mode);
246extern AclResult object_aclcheck_ext(Oid classid, Oid objectid,
247 Oid roleid, AclMode mode,
248 bool *is_missing);
249
250/* special cases */
251extern AclResult pg_attribute_aclcheck(Oid table_oid, AttrNumber attnum,
252 Oid roleid, AclMode mode);
253extern AclResult pg_attribute_aclcheck_ext(Oid table_oid, AttrNumber attnum,
254 Oid roleid, AclMode mode,
255 bool *is_missing);
256extern AclResult pg_attribute_aclcheck_all(Oid table_oid, Oid roleid,
257 AclMode mode, AclMaskHow how);
258extern AclResult pg_attribute_aclcheck_all_ext(Oid table_oid, Oid roleid,
259 AclMode mode, AclMaskHow how,
260 bool *is_missing);
261extern AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode);
262extern AclResult pg_class_aclcheck_ext(Oid table_oid, Oid roleid,
263 AclMode mode, bool *is_missing);
264extern AclResult pg_parameter_aclcheck(const char *name, Oid roleid,
265 AclMode mode);
266extern AclResult pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid,
267 AclMode mode, Snapshot snapshot);
268
269extern void aclcheck_error(AclResult aclerr, ObjectType objtype,
270 const char *objectname);
271
272extern void aclcheck_error_col(AclResult aclerr, ObjectType objtype,
273 const char *objectname, const char *colname);
274
275extern void aclcheck_error_type(AclResult aclerr, Oid typeOid);
276
277extern void recordExtObjInitPriv(Oid objoid, Oid classoid);
278extern void removeExtObjInitPriv(Oid objoid, Oid classoid);
279extern void ReplaceRoleInInitPriv(Oid oldroleid, Oid newroleid,
280 Oid classid, Oid objid, int32 objsubid);
281extern void RemoveRoleFromInitPriv(Oid roleid,
282 Oid classid, Oid objid, int32 objsubid);
283
284
285/* ownercheck routines just return true (owner) or false (not) */
286extern bool object_ownercheck(Oid classid, Oid objectid, Oid roleid);
287extern bool has_createrole_privilege(Oid roleid);
288extern bool has_bypassrls_privilege(Oid roleid);
289
290#endif /* ACL_H */
AclResult object_aclcheck_ext(Oid classid, Oid objectid, Oid roleid, AclMode mode, bool *is_missing)
Definition: aclchk.c:3844
void initialize_acl(void)
Definition: acl.c:5040
void ExecuteGrantStmt(GrantStmt *stmt)
Definition: aclchk.c:391
AclResult pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode, Snapshot snapshot)
Definition: aclchk.c:4074
void RemoveRoleFromInitPriv(Oid roleid, Oid classid, Oid objid, int32 objsubid)
Definition: aclchk.c:4865
bool is_admin_of_role(Oid member, Oid role)
Definition: acl.c:5414
Acl * aclconcat(const Acl *left_acl, const Acl *right_acl)
Definition: acl.c:477
Acl * aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId)
Definition: acl.c:501
bool has_bypassrls_privilege(Oid roleid)
Definition: aclchk.c:4186
AclResult pg_class_aclcheck_ext(Oid table_oid, Oid roleid, AclMode mode, bool *is_missing)
Definition: aclchk.c:4047
void aclcheck_error_col(AclResult aclerr, ObjectType objtype, const char *objectname, const char *colname)
Definition: aclchk.c:2941
AclResult pg_attribute_aclcheck_all_ext(Oid table_oid, Oid roleid, AclMode mode, AclMaskHow how, bool *is_missing)
Definition: aclchk.c:3919
void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
Definition: aclchk.c:4325
Oid select_best_admin(Oid member, Oid role)
Definition: acl.c:5439
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:803
Oid get_role_oid_or_public(const char *rolname)
Definition: acl.c:5570
void ExecAlterDefaultPrivilegesStmt(ParseState *pstate, AlterDefaultPrivilegesStmt *stmt)
Definition: aclchk.c:916
bool aclequal(const Acl *left_acl, const Acl *right_acl)
Definition: acl.c:559
AclResult
Definition: acl.h:182
@ ACLCHECK_NO_PRIV
Definition: acl.h:184
@ ACLCHECK_OK
Definition: acl.h:183
@ ACLCHECK_NOT_OWNER
Definition: acl.h:185
AclResult pg_attribute_aclcheck_all(Oid table_oid, Oid roleid, AclMode mode, AclMaskHow how)
Definition: aclchk.c:3908
Acl * aclupdate(const Acl *old_acl, const AclItem *mod_aip, int modechg, Oid ownerId, DropBehavior behavior)
Definition: acl.c:992
struct AclItem AclItem
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:5364
AclResult pg_parameter_aclcheck(const char *name, Oid roleid, AclMode mode)
Definition: aclchk.c:4062
void ReplaceRoleInInitPriv(Oid oldroleid, Oid newroleid, Oid classid, Oid objid, int32 objsubid)
Definition: aclchk.c:4756
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5476
bool is_member_of_role_nosuper(Oid member, Oid role)
Definition: acl.c:5392
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:5284
Acl * make_empty_acl(void)
Definition: acl.c:448
void recordExtObjInitPriv(Oid objoid, Oid classoid)
Definition: aclchk.c:4352
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1540
Acl * aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
Definition: acl.c:1119
bool member_can_set_role(Oid member, Oid role)
Definition: acl.c:5318
Acl * aclcopy(const Acl *orig_acl)
Definition: acl.c:457
void aclitemsort(Acl *acl)
Definition: acl.c:545
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1388
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2652
AclResult pg_attribute_aclcheck(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode)
Definition: aclchk.c:3866
Oid get_role_oid(const char *rolname, bool missing_ok)
Definition: acl.c:5552
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3834
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4088
AclResult pg_attribute_aclcheck_ext(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode, bool *is_missing)
Definition: aclchk.c:3878
char * get_rolespec_name(const RoleSpec *role)
Definition: acl.c:5671
void check_can_set_role(Oid member, Oid role)
Definition: acl.c:5341
AclMode pg_class_aclmask(Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3270
void check_rolespec_name(const RoleSpec *role, const char *detail_msg)
Definition: acl.c:5693
void aclcheck_error_type(AclResult aclerr, Oid typeOid)
Definition: aclchk.c:2971
bool has_createrole_privilege(Oid roleid)
Definition: aclchk.c:4167
AclMaskHow
Definition: acl.h:175
@ ACLMASK_ANY
Definition: acl.h:177
@ ACLMASK_ALL
Definition: acl.h:176
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition: acl.c:5586
Acl * get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
Definition: aclchk.c:4245
void RemoveRoleFromObjectACL(Oid roleid, Oid classid, Oid objid)
Definition: aclchk.c:1420
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4037
HeapTuple get_rolespec_tuple(const RoleSpec *role)
Definition: acl.c:5625
void removeExtObjInitPriv(Oid objoid, Oid classoid)
Definition: aclchk.c:4516
int16 AttrNumber
Definition: attnum.h:21
int32_t int32
Definition: c.h:534
#define stmt
Definition: indent_codes.h:59
uint64 AclMode
Definition: parsenodes.h:74
DropBehavior
Definition: parsenodes.h:2397
ObjectType
Definition: parsenodes.h:2324
int16 attnum
Definition: pg_attribute.h:74
NameData rolname
Definition: pg_authid.h:34
static PgChecksumMode mode
Definition: pg_checksums.c:55
unsigned int Oid
Definition: postgres_ext.h:32
Definition: acl.h:55
Oid ai_grantee
Definition: acl.h:56
AclMode ai_privs
Definition: acl.h:58
Oid ai_grantor
Definition: acl.h:57
Definition: array.h:93
const char * name

AltStyle によって変換されたページ (->オリジナル) /