YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/postgres/src/include/utils/acl.h
Line
Count
Source (jump to first uncovered line)
1
/*-------------------------------------------------------------------------
2
 *
3
 * acl.h
4
 *    Definition of (and support for) access control list data structures.
5
 *
6
 *
7
 * Portions Copyright (c) 1996-2018, 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/array.h"
39
#include "utils/snapshot.h"
40
41
42
/*
43
 * typedef AclMode is declared in parsenodes.h, also the individual privilege
44
 * bit meanings are defined there
45
 */
46
47
22.4k
#define ACL_ID_PUBLIC 0    /* placeholder for id in a PUBLIC acl item */
48
49
/*
50
 * AclItem
51
 *
52
 * Note: must be same size on all platforms, because the size is hardcoded
53
 * in the pg_type.h entry for aclitem.
54
 */
55
typedef struct AclItem
56
{
57
  Oid     ai_grantee;   /* ID that this item grants privs to */
58
  Oid     ai_grantor;   /* grantor of privs */
59
  AclMode   ai_privs;   /* privilege bits */
60
} AclItem;
61
62
/*
63
 * The upper 16 bits of the ai_privs field of an AclItem are the grant option
64
 * bits, and the lower 16 bits are the actual privileges.  We use "rights"
65
 * to mean the combined grant option and privilege bits fields.
66
 */
67
977
#define ACLITEM_GET_PRIVS(item)    ((item).ai_privs & 0xFFFF)
68
2.69k
#define ACLITEM_GET_GOPTIONS(item) (((item).ai_privs >> 16) & 0xFFFF)
69
1.46k
#define ACLITEM_GET_RIGHTS(item)   ((item).ai_privs)
70
71
41.8k
#define ACL_GRANT_OPTION_FOR(privs) (((AclMode) (privs) & 0xFFFF) << 16)
72
686
#define ACL_OPTION_TO_PRIVS(privs)  (((AclMode) (privs) >> 16) & 0xFFFF)
73
74
#define ACLITEM_SET_PRIVS(item,privs) \
75
  ((item).ai_privs = ((item).ai_privs & ~((AclMode) 0xFFFF)) | \
76
           ((AclMode) (privs) & 0xFFFF))
77
#define ACLITEM_SET_GOPTIONS(item,goptions) \
78
  ((item).ai_privs = ((item).ai_privs & ~(((AclMode) 0xFFFF) << 16)) | \
79
           (((AclMode) (goptions) & 0xFFFF) << 16))
80
#define ACLITEM_SET_RIGHTS(item,rights) \
81
733
  ((item).ai_privs = (AclMode) (rights))
82
83
#define ACLITEM_SET_PRIVS_GOPTIONS(item,privs,goptions) \
84
6.22k
  ((item).ai_privs = ((AclMode) (privs) & 0xFFFF) | \
85
1.67k
           (((AclMode) (goptions) & 0xFFFF) << 16))
86
87
88
#define ACLITEM_ALL_PRIV_BITS   ((AclMode) 0xFFFF)
89
5.53k
#define ACLITEM_ALL_GOPTION_BITS  ((AclMode) 0xFFFF << 16)
90
91
/*
92
 * Definitions for convenient access to Acl (array of AclItem).
93
 * These are standard PostgreSQL arrays, but are restricted to have one
94
 * dimension and no nulls.  We also ignore the lower bound when reading,
95
 * and set it to one when writing.
96
 *
97
 * CAUTION: as of PostgreSQL 7.1, these arrays are toastable (just like all
98
 * other array types).  Therefore, be careful to detoast them with the
99
 * macros provided, unless you know for certain that a particular array
100
 * can't have been toasted.
101
 */
102
103
104
/*
105
 * Acl      a one-dimensional array of AclItem
106
 */
107
typedef ArrayType Acl;
108
109
10.9k
#define ACL_NUM(ACL)      (ARR_DIMS(ACL)[0])
110
10.6k
#define ACL_DAT(ACL)      ((AclItem *) ARR_DATA_PTR(ACL))
111
4.48k
#define ACL_N_SIZE(N)     (ARR_OVERHEAD_NONULLS(1) + ((N) * sizeof(AclItem)))
112
#define ACL_SIZE(ACL)     ARR_SIZE(ACL)
113
114
/*
115
 * fmgr macros for these types
116
 */
117
1.63k
#define DatumGetAclItemP(X)      ((AclItem *) DatumGetPointer(X))
118
1.63k
#define PG_GETARG_ACLITEM_P(n)     DatumGetAclItemP(PG_GETARG_DATUM(n))
119
0
#define PG_RETURN_ACLITEM_P(x)     PG_RETURN_POINTER(x)
120
121
3.05k
#define DatumGetAclP(X)        ((Acl *) PG_DETOAST_DATUM(X))
122
221
#define DatumGetAclPCopy(X)      ((Acl *) PG_DETOAST_DATUM_COPY(X))
123
0
#define PG_GETARG_ACL_P(n)       DatumGetAclP(PG_GETARG_DATUM(n))
124
#define PG_GETARG_ACL_P_COPY(n)    DatumGetAclPCopy(PG_GETARG_DATUM(n))
125
0
#define PG_RETURN_ACL_P(x)       PG_RETURN_POINTER(x)
126
127
/*
128
 * ACL modification opcodes for aclupdate
129
 */
130
498
#define ACL_MODECHG_ADD     1
131
3.10k
#define ACL_MODECHG_DEL     2
132
0
#define ACL_MODECHG_EQL     3
133
134
/*
135
 * External representations of the privilege bits --- aclitemin/aclitemout
136
 * represent each possible privilege bit with a distinct 1-character code
137
 */
138
226
#define ACL_INSERT_CHR      'a' /* formerly known as "append" */
139
355
#define ACL_SELECT_CHR      'r' /* formerly known as "read" */
140
64
#define ACL_UPDATE_CHR      'w' /* formerly known as "write" */
141
94
#define ACL_DELETE_CHR      'd'
142
0
#define ACL_TRUNCATE_CHR    'D' /* super-delete, as it were */
143
0
#define ACL_REFERENCES_CHR    'x'
144
0
#define ACL_TRIGGER_CHR     't'
145
0
#define ACL_EXECUTE_CHR     'X'
146
0
#define ACL_USAGE_CHR     'U'
147
0
#define ACL_CREATE_CHR      'C'
148
0
#define ACL_CREATE_TEMP_CHR   'T'
149
0
#define ACL_CONNECT_CHR     'c'
150
151
/* string holding all privilege code chars, in order by bitmask position */
152
142
#define ACL_ALL_RIGHTS_STR  "arwdDxtXUCTc"
153
154
/*
155
 * Bitmasks defining "all rights" for each supported object type
156
 */
157
973
#define ACL_ALL_RIGHTS_COLUMN   (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_REFERENCES)
158
914
#define ACL_ALL_RIGHTS_RELATION   (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_DELETE|ACL_TRUNCATE|ACL_REFERENCES|ACL_TRIGGER)
159
314
#define ACL_ALL_RIGHTS_SEQUENCE   (ACL_USAGE|ACL_SELECT|ACL_UPDATE)
160
57
#define ACL_ALL_RIGHTS_DATABASE   (ACL_CREATE|ACL_CREATE_TEMP|ACL_CONNECT)
161
4
#define ACL_ALL_RIGHTS_FDW      (ACL_USAGE)
162
6
#define ACL_ALL_RIGHTS_FOREIGN_SERVER (ACL_USAGE)
163
1.77k
#define ACL_ALL_RIGHTS_FUNCTION   (ACL_EXECUTE)
164
58
#define ACL_ALL_RIGHTS_LANGUAGE   (ACL_USAGE)
165
34
#define ACL_ALL_RIGHTS_LARGEOBJECT  (ACL_SELECT|ACL_UPDATE)
166
80
#define ACL_ALL_RIGHTS_SCHEMA   (ACL_USAGE|ACL_CREATE)
167
0
#define ACL_ALL_RIGHTS_TABLEGROUP (ACL_CREATE)
168
0
#define ACL_ALL_RIGHTS_TABLESPACE (ACL_CREATE)
169
512
#define ACL_ALL_RIGHTS_TYPE     (ACL_USAGE)
170
171
/* operation codes for pg_*_aclmask */
172
typedef enum
173
{
174
  ACLMASK_ALL,        /* normal case: compute all bits */
175
  ACLMASK_ANY         /* return when result is known nonzero */
176
} AclMaskHow;
177
178
/* result codes for pg_*_aclcheck */
179
typedef enum
180
{
181
  ACLCHECK_OK = 0,
182
  ACLCHECK_NO_PRIV,
183
  ACLCHECK_NOT_OWNER
184
} AclResult;
185
186
187
/*
188
 * routines used internally
189
 */
190
extern Acl *acldefault(ObjectType objtype, Oid ownerId);
191
extern Acl *get_user_default_acl(ObjectType objtype, Oid ownerId,
192
           Oid nsp_oid);
193
extern void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId,
194
             Oid ownerId, Acl *acl);
195
196
extern Acl *aclupdate(const Acl *old_acl, const AclItem *mod_aip,
197
      int modechg, Oid ownerId, DropBehavior behavior);
198
extern Acl *aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId);
199
extern Acl *make_empty_acl(void);
200
extern Acl *aclcopy(const Acl *orig_acl);
201
extern Acl *aclconcat(const Acl *left_acl, const Acl *right_acl);
202
extern Acl *aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId);
203
extern void aclitemsort(Acl *acl);
204
extern bool aclequal(const Acl *left_acl, const Acl *right_acl);
205
206
extern AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId,
207
    AclMode mask, AclMaskHow how);
208
extern int  aclmembers(const Acl *acl, Oid **roleids);
209
210
extern bool has_privs_of_role(Oid member, Oid role);
211
extern bool is_member_of_role(Oid member, Oid role);
212
extern bool is_member_of_role_nosuper(Oid member, Oid role);
213
extern bool is_admin_of_role(Oid member, Oid role);
214
extern void check_is_member_of_role(Oid member, Oid role);
215
extern Oid  get_role_oid(const char *rolename, bool missing_ok);
216
extern Oid  get_role_oid_or_public(const char *rolename);
217
extern Oid  get_rolespec_oid(const RoleSpec *role, bool missing_ok);
218
extern void check_rolespec_name(const RoleSpec *role, const char *detail_msg);
219
extern HeapTuple get_rolespec_tuple(const RoleSpec *role);
220
extern char *get_rolespec_name(const RoleSpec *role);
221
222
extern void select_best_grantor(Oid roleId, AclMode privileges,
223
          const Acl *acl, Oid ownerId,
224
          Oid *grantorId, AclMode *grantOptions);
225
226
extern void initialize_acl(void);
227
228
/*
229
 * prototypes for functions in aclchk.c
230
 */
231
extern void ExecuteGrantStmt(GrantStmt *stmt);
232
extern void ExecAlterDefaultPrivilegesStmt(ParseState *pstate, AlterDefaultPrivilegesStmt *stmt);
233
234
extern void RemoveRoleFromObjectACL(Oid roleid, Oid classid, Oid objid);
235
extern void RemoveDefaultACLById(Oid defaclOid);
236
237
extern AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum,
238
           Oid roleid, AclMode mask, AclMaskHow how);
239
extern AclMode pg_class_aclmask(Oid table_oid, Oid roleid,
240
         AclMode mask, AclMaskHow how);
241
extern AclMode pg_database_aclmask(Oid db_oid, Oid roleid,
242
          AclMode mask, AclMaskHow how);
243
extern AclMode pg_proc_aclmask(Oid proc_oid, Oid roleid,
244
        AclMode mask, AclMaskHow how);
245
extern AclMode pg_language_aclmask(Oid lang_oid, Oid roleid,
246
          AclMode mask, AclMaskHow how);
247
extern AclMode pg_largeobject_aclmask_snapshot(Oid lobj_oid, Oid roleid,
248
                AclMode mask, AclMaskHow how, Snapshot snapshot);
249
extern AclMode pg_namespace_aclmask(Oid nsp_oid, Oid roleid,
250
           AclMode mask, AclMaskHow how);
251
extern AclMode pg_tablegroup_aclmask(Oid grp_oid, Oid roleid,
252
            AclMode mask, AclMaskHow how);
253
extern AclMode pg_tablespace_aclmask(Oid spc_oid, Oid roleid,
254
            AclMode mask, AclMaskHow how);
255
extern AclMode pg_foreign_data_wrapper_aclmask(Oid fdw_oid, Oid roleid,
256
                AclMode mask, AclMaskHow how);
257
extern AclMode pg_foreign_server_aclmask(Oid srv_oid, Oid roleid,
258
              AclMode mask, AclMaskHow how);
259
extern AclMode pg_type_aclmask(Oid type_oid, Oid roleid,
260
        AclMode mask, AclMaskHow how);
261
262
extern AclResult pg_attribute_aclcheck(Oid table_oid, AttrNumber attnum,
263
            Oid roleid, AclMode mode);
264
extern AclResult pg_attribute_aclcheck_all(Oid table_oid, Oid roleid,
265
              AclMode mode, AclMaskHow how);
266
extern AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode);
267
extern AclResult pg_database_aclcheck(Oid db_oid, Oid roleid, AclMode mode);
268
extern AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode);
269
extern AclResult pg_language_aclcheck(Oid lang_oid, Oid roleid, AclMode mode);
270
extern AclResult pg_largeobject_aclcheck_snapshot(Oid lang_oid, Oid roleid,
271
                 AclMode mode, Snapshot snapshot);
272
extern AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode);
273
extern AclResult pg_tablegroup_aclcheck(Oid grp_oid, Oid roleid, AclMode mode);
274
extern AclResult pg_tablespace_aclcheck(Oid spc_oid, Oid roleid, AclMode mode);
275
extern AclResult pg_foreign_data_wrapper_aclcheck(Oid fdw_oid, Oid roleid, AclMode mode);
276
extern AclResult pg_foreign_server_aclcheck(Oid srv_oid, Oid roleid, AclMode mode);
277
extern AclResult pg_type_aclcheck(Oid type_oid, Oid roleid, AclMode mode);
278
279
extern void aclcheck_error(AclResult aclerr, ObjectType objtype,
280
         const char *objectname);
281
282
extern void aclcheck_error_col(AclResult aclerr, ObjectType objtype,
283
           const char *objectname, const char *colname);
284
285
extern void aclcheck_error_type(AclResult aclerr, Oid typeOid);
286
287
extern void recordExtObjInitPriv(Oid objoid, Oid classoid);
288
extern void removeExtObjInitPriv(Oid objoid, Oid classoid);
289
290
291
/* ownercheck routines just return true (owner) or false (not) */
292
extern bool pg_class_ownercheck(Oid class_oid, Oid roleid);
293
extern bool pg_type_ownercheck(Oid type_oid, Oid roleid);
294
extern bool pg_oper_ownercheck(Oid oper_oid, Oid roleid);
295
extern bool pg_proc_ownercheck(Oid proc_oid, Oid roleid);
296
extern bool pg_language_ownercheck(Oid lan_oid, Oid roleid);
297
extern bool pg_largeobject_ownercheck(Oid lobj_oid, Oid roleid);
298
extern bool pg_namespace_ownercheck(Oid nsp_oid, Oid roleid);
299
extern bool pg_tablegroup_ownercheck(Oid grp_oid, Oid roleid);
300
extern bool pg_tablespace_ownercheck(Oid spc_oid, Oid roleid);
301
extern bool pg_opclass_ownercheck(Oid opc_oid, Oid roleid);
302
extern bool pg_opfamily_ownercheck(Oid opf_oid, Oid roleid);
303
extern bool pg_database_ownercheck(Oid db_oid, Oid roleid);
304
extern bool pg_collation_ownercheck(Oid coll_oid, Oid roleid);
305
extern bool pg_conversion_ownercheck(Oid conv_oid, Oid roleid);
306
extern bool pg_ts_dict_ownercheck(Oid dict_oid, Oid roleid);
307
extern bool pg_ts_config_ownercheck(Oid cfg_oid, Oid roleid);
308
extern bool pg_foreign_data_wrapper_ownercheck(Oid srv_oid, Oid roleid);
309
extern bool pg_foreign_server_ownercheck(Oid srv_oid, Oid roleid);
310
extern bool pg_event_trigger_ownercheck(Oid et_oid, Oid roleid);
311
extern bool pg_extension_ownercheck(Oid ext_oid, Oid roleid);
312
extern bool pg_publication_ownercheck(Oid pub_oid, Oid roleid);
313
extern bool pg_subscription_ownercheck(Oid sub_oid, Oid roleid);
314
extern bool pg_statistics_object_ownercheck(Oid stat_oid, Oid roleid);
315
extern bool has_createrole_privilege(Oid roleid);
316
extern bool has_bypassrls_privilege(Oid roleid);
317
318
#endif              /* ACL_H */