Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * aclchk.c
4 : : * Routines to check access control permissions.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/catalog/aclchk.c
12 : : *
13 : : * NOTES
14 : : * See acl.h.
15 : : *
16 : : * The xxx_aclmask() functions in this file are wrappers around
17 : : * acl.c's aclmask() function; see that for basic usage information.
18 : : * The wrapper functions add object-type-specific lookup capability.
19 : : * Generally, they will throw error if the object doesn't exist.
20 : : *
21 : : * The xxx_aclmask_ext() functions add the ability to not throw
22 : : * error if the object doesn't exist. If their "is_missing" argument
23 : : * isn't NULL, then when the object isn't found they will set
24 : : * *is_missing = true and return zero (no privileges) instead of
25 : : * throwing an error. Caller must initialize *is_missing = false.
26 : : *
27 : : * The xxx_aclcheck() functions are simplified wrappers around the
28 : : * corresponding xxx_aclmask() functions, simply returning ACLCHECK_OK
29 : : * if any of the privileges specified in "mode" are held, and otherwise
30 : : * a suitable error code (in practice, always ACLCHECK_NO_PRIV).
31 : : * Again, they will throw error if the object doesn't exist.
32 : : *
33 : : * The xxx_aclcheck_ext() functions add the ability to not throw
34 : : * error if the object doesn't exist. Their "is_missing" argument
35 : : * works similarly to the xxx_aclmask_ext() functions.
36 : : *
37 : : *-------------------------------------------------------------------------
38 : : */
39 : : #include "postgres.h"
40 : :
41 : : #include "access/genam.h"
42 : : #include "access/heapam.h"
43 : : #include "access/htup_details.h"
44 : : #include "access/sysattr.h"
45 : : #include "access/tableam.h"
46 : : #include "access/xact.h"
47 : : #include "catalog/binary_upgrade.h"
48 : : #include "catalog/catalog.h"
49 : : #include "catalog/dependency.h"
50 : : #include "catalog/indexing.h"
51 : : #include "catalog/objectaccess.h"
52 : : #include "catalog/pg_authid.h"
53 : : #include "catalog/pg_class.h"
54 : : #include "catalog/pg_database.h"
55 : : #include "catalog/pg_default_acl.h"
56 : : #include "catalog/pg_foreign_data_wrapper.h"
57 : : #include "catalog/pg_foreign_server.h"
58 : : #include "catalog/pg_init_privs.h"
59 : : #include "catalog/pg_language.h"
60 : : #include "catalog/pg_largeobject.h"
61 : : #include "catalog/pg_largeobject_metadata.h"
62 : : #include "catalog/pg_namespace.h"
63 : : #include "catalog/pg_parameter_acl.h"
64 : : #include "catalog/pg_proc.h"
65 : : #include "catalog/pg_tablespace.h"
66 : : #include "catalog/pg_type.h"
67 : : #include "commands/defrem.h"
68 : : #include "commands/event_trigger.h"
69 : : #include "commands/extension.h"
70 : : #include "commands/proclang.h"
71 : : #include "commands/tablespace.h"
72 : : #include "foreign/foreign.h"
73 : : #include "miscadmin.h"
74 : : #include "nodes/makefuncs.h"
75 : : #include "parser/parse_func.h"
76 : : #include "parser/parse_type.h"
77 : : #include "storage/lmgr.h"
78 : : #include "utils/acl.h"
79 : : #include "utils/aclchk_internal.h"
80 : : #include "utils/builtins.h"
81 : : #include "utils/fmgroids.h"
82 : : #include "utils/guc.h"
83 : : #include "utils/lsyscache.h"
84 : : #include "utils/rel.h"
85 : : #include "utils/syscache.h"
86 : :
87 : : /*
88 : : * Internal format used by ALTER DEFAULT PRIVILEGES.
89 : : */
90 : : typedef struct
91 : : {
92 : : Oid roleid; /* owning role */
93 : : Oid nspid; /* namespace, or InvalidOid if none */
94 : : /* remaining fields are same as in InternalGrant: */
95 : : bool is_grant;
96 : : ObjectType objtype;
97 : : bool all_privs;
98 : : AclMode privileges;
99 : : List *grantees;
100 : : bool grant_option;
101 : : RoleSpec *grantor;
102 : : DropBehavior behavior;
103 : : } InternalDefaultACL;
104 : :
105 : : /*
106 : : * When performing a binary-upgrade, pg_dump will call a function to set
107 : : * this variable to let us know that we need to populate the pg_init_privs
108 : : * table for the GRANT/REVOKE commands while this variable is set to true.
109 : : */
110 : : bool binary_upgrade_record_init_privs = false;
111 : :
112 : : static void ExecGrantStmt_oids(InternalGrant *istmt);
113 : : static void ExecGrant_Relation(InternalGrant *istmt);
114 : : static void ExecGrant_common(InternalGrant *istmt, Oid classid, AclMode default_privs,
115 : : void (*object_check) (InternalGrant *istmt, HeapTuple tuple));
116 : : static void ExecGrant_Language_check(InternalGrant *istmt, HeapTuple tuple);
117 : : static void ExecGrant_Largeobject(InternalGrant *istmt);
118 : : static void ExecGrant_Type_check(InternalGrant *istmt, HeapTuple tuple);
119 : : static void ExecGrant_Parameter(InternalGrant *istmt);
120 : :
121 : : static void SetDefaultACLsInSchemas(InternalDefaultACL *iacls, List *nspnames);
122 : : static void SetDefaultACL(InternalDefaultACL *iacls);
123 : :
124 : : static List *objectNamesToOids(ObjectType objtype, List *objnames,
125 : : bool is_grant);
126 : : static List *objectsInSchemaToOids(ObjectType objtype, List *nspnames);
127 : : static List *getRelationsInNamespace(Oid namespaceId, char relkind);
128 : : static void expand_col_privileges(List *colnames, Oid table_oid,
129 : : AclMode this_privileges,
130 : : AclMode *col_privileges,
131 : : int num_col_privileges);
132 : : static void expand_all_col_privileges(Oid table_oid, Form_pg_class classForm,
133 : : AclMode this_privileges,
134 : : AclMode *col_privileges,
135 : : int num_col_privileges);
136 : : static AclMode string_to_privilege(const char *privname);
137 : : static const char *privilege_to_string(AclMode privilege);
138 : : static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions,
139 : : bool all_privs, AclMode privileges,
140 : : Oid objectId, Oid grantorId,
141 : : ObjectType objtype, const char *objname,
142 : : AttrNumber att_number, const char *colname);
143 : : static AclMode pg_aclmask(ObjectType objtype, Oid object_oid, AttrNumber attnum,
144 : : Oid roleid, AclMode mask, AclMaskHow how);
145 : : static AclMode object_aclmask(Oid classid, Oid objectid, Oid roleid,
146 : : AclMode mask, AclMaskHow how);
147 : : static AclMode object_aclmask_ext(Oid classid, Oid objectid, Oid roleid,
148 : : AclMode mask, AclMaskHow how,
149 : : bool *is_missing);
150 : : static AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum,
151 : : Oid roleid, AclMode mask, AclMaskHow how);
152 : : static AclMode pg_attribute_aclmask_ext(Oid table_oid, AttrNumber attnum,
153 : : Oid roleid, AclMode mask,
154 : : AclMaskHow how, bool *is_missing);
155 : : static AclMode pg_class_aclmask_ext(Oid table_oid, Oid roleid,
156 : : AclMode mask, AclMaskHow how,
157 : : bool *is_missing);
158 : : static AclMode pg_parameter_acl_aclmask(Oid acl_oid, Oid roleid,
159 : : AclMode mask, AclMaskHow how);
160 : : static AclMode pg_largeobject_aclmask_snapshot(Oid lobj_oid, Oid roleid,
161 : : AclMode mask, AclMaskHow how, Snapshot snapshot);
162 : : static AclMode pg_namespace_aclmask_ext(Oid nsp_oid, Oid roleid,
163 : : AclMode mask, AclMaskHow how,
164 : : bool *is_missing);
165 : : static AclMode pg_type_aclmask_ext(Oid type_oid, Oid roleid,
166 : : AclMode mask, AclMaskHow how,
167 : : bool *is_missing);
168 : : static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid,
169 : : Acl *new_acl);
170 : : static void recordExtensionInitPrivWorker(Oid objoid, Oid classoid, int objsubid,
171 : : Acl *new_acl);
172 : :
173 : :
174 : : /*
175 : : * If is_grant is true, adds the given privileges for the list of
176 : : * grantees to the existing old_acl. If is_grant is false, the
177 : : * privileges for the given grantees are removed from old_acl.
178 : : *
179 : : * NB: the original old_acl is pfree'd.
180 : : */
181 : : static Acl *
182 : 41492 : merge_acl_with_grant(Acl *old_acl, bool is_grant,
183 : : bool grant_option, DropBehavior behavior,
184 : : List *grantees, AclMode privileges,
185 : : Oid grantorId, Oid ownerId)
186 : : {
187 : : unsigned modechg;
188 : : ListCell *j;
189 : : Acl *new_acl;
190 : :
191 [ + + ]: 41492 : modechg = is_grant ? ACL_MODECHG_ADD : ACL_MODECHG_DEL;
192 : :
193 : 41492 : new_acl = old_acl;
194 : :
195 [ + - + + : 83063 : foreach(j, grantees)
+ + ]
196 : : {
197 : : AclItem aclitem;
198 : : Acl *newer_acl;
199 : :
200 : 41579 : aclitem.ai_grantee = lfirst_oid(j);
201 : :
202 : : /*
203 : : * Grant options can only be granted to individual roles, not PUBLIC.
204 : : * The reason is that if a user would re-grant a privilege that he
205 : : * held through PUBLIC, and later the user is removed, the situation
206 : : * is impossible to clean up.
207 : : */
208 [ + + + + : 41579 : if (is_grant && grant_option && aclitem.ai_grantee == ACL_ID_PUBLIC)
- + ]
209 [ # # ]: 0 : ereport(ERROR,
210 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
211 : : errmsg("grant options can only be granted to roles")));
212 : :
213 : 41579 : aclitem.ai_grantor = grantorId;
214 : :
215 : : /*
216 : : * The asymmetry in the conditions here comes from the spec. In
217 : : * GRANT, the grant_option flag signals WITH GRANT OPTION, which means
218 : : * to grant both the basic privilege and its grant option. But in
219 : : * REVOKE, plain revoke revokes both the basic privilege and its grant
220 : : * option, while REVOKE GRANT OPTION revokes only the option.
221 : : */
222 [ + + + + : 41579 : ACLITEM_SET_PRIVS_GOPTIONS(aclitem,
+ + + + ]
223 : : (is_grant || !grant_option) ? privileges : ACL_NO_RIGHTS,
224 : : (!is_grant || grant_option) ? privileges : ACL_NO_RIGHTS);
225 : :
226 : 41579 : newer_acl = aclupdate(new_acl, &aclitem, modechg, ownerId, behavior);
227 : :
228 : : /* avoid memory leak when there are many grantees */
229 : 41571 : pfree(new_acl);
230 : 41571 : new_acl = newer_acl;
231 : : }
232 : :
233 : 41484 : return new_acl;
234 : : }
235 : :
236 : : /*
237 : : * Restrict the privileges to what we can actually grant, and emit
238 : : * the standards-mandated warning and error messages.
239 : : */
240 : : static AclMode
241 : 41347 : restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs,
242 : : AclMode privileges, Oid objectId, Oid grantorId,
243 : : ObjectType objtype, const char *objname,
244 : : AttrNumber att_number, const char *colname)
245 : : {
246 : : AclMode this_privileges;
247 : : AclMode whole_mask;
248 : :
249 [ + + + + : 41347 : switch (objtype)
+ + + + +
+ + - + +
+ - ]
250 : : {
251 : 28591 : case OBJECT_COLUMN:
252 : 28591 : whole_mask = ACL_ALL_RIGHTS_COLUMN;
253 : 28591 : break;
254 : 10945 : case OBJECT_TABLE:
255 : 10945 : whole_mask = ACL_ALL_RIGHTS_RELATION;
256 : 10945 : break;
257 : 115 : case OBJECT_SEQUENCE:
258 : 115 : whole_mask = ACL_ALL_RIGHTS_SEQUENCE;
259 : 115 : break;
260 : 220 : case OBJECT_DATABASE:
261 : 220 : whole_mask = ACL_ALL_RIGHTS_DATABASE;
262 : 220 : break;
263 : 609 : case OBJECT_FUNCTION:
264 : 609 : whole_mask = ACL_ALL_RIGHTS_FUNCTION;
265 : 609 : break;
266 : 23 : case OBJECT_LANGUAGE:
267 : 23 : whole_mask = ACL_ALL_RIGHTS_LANGUAGE;
268 : 23 : break;
269 : 62 : case OBJECT_LARGEOBJECT:
270 : 62 : whole_mask = ACL_ALL_RIGHTS_LARGEOBJECT;
271 : 62 : break;
272 : 449 : case OBJECT_SCHEMA:
273 : 449 : whole_mask = ACL_ALL_RIGHTS_SCHEMA;
274 : 449 : break;
275 : 3 : case OBJECT_TABLESPACE:
276 : 3 : whole_mask = ACL_ALL_RIGHTS_TABLESPACE;
277 : 3 : break;
278 : 61 : case OBJECT_FDW:
279 : 61 : whole_mask = ACL_ALL_RIGHTS_FDW;
280 : 61 : break;
281 : 79 : case OBJECT_FOREIGN_SERVER:
282 : 79 : whole_mask = ACL_ALL_RIGHTS_FOREIGN_SERVER;
283 : 79 : break;
284 : 0 : case OBJECT_EVENT_TRIGGER:
285 [ # # ]: 0 : elog(ERROR, "grantable rights not supported for event triggers");
286 : : /* not reached, but keep compiler quiet */
287 : : return ACL_NO_RIGHTS;
288 : 96 : case OBJECT_TYPE:
289 : 96 : whole_mask = ACL_ALL_RIGHTS_TYPE;
290 : 96 : break;
291 : 68 : case OBJECT_PARAMETER_ACL:
292 : 68 : whole_mask = ACL_ALL_RIGHTS_PARAMETER_ACL;
293 : 68 : break;
294 : 26 : case OBJECT_PROPGRAPH:
295 : 26 : whole_mask = ACL_ALL_RIGHTS_PROPGRAPH;
296 : 26 : break;
297 : 0 : default:
298 [ # # ]: 0 : elog(ERROR, "unrecognized object type: %d", objtype);
299 : : /* not reached, but keep compiler quiet */
300 : : return ACL_NO_RIGHTS;
301 : : }
302 : :
303 : : /*
304 : : * If we found no grant options, consider whether to issue a hard error.
305 : : * Per spec, having any privilege at all on the object will get you by
306 : : * here.
307 : : */
308 [ + + ]: 41347 : if (avail_goptions == ACL_NO_RIGHTS)
309 : : {
310 [ + + ]: 48 : if (pg_aclmask(objtype, objectId, att_number, grantorId,
311 : 48 : whole_mask | ACL_GRANT_OPTION_FOR(whole_mask),
312 : : ACLMASK_ANY) == ACL_NO_RIGHTS)
313 : : {
314 [ - + - - ]: 24 : if (objtype == OBJECT_COLUMN && colname)
315 : 0 : aclcheck_error_col(ACLCHECK_NO_PRIV, objtype, objname, colname);
316 : : else
317 : 24 : aclcheck_error(ACLCHECK_NO_PRIV, objtype, objname);
318 : : }
319 : : }
320 : :
321 : : /*
322 : : * Restrict the operation to what we can actually grant or revoke, and
323 : : * issue a warning if appropriate. (For REVOKE this isn't quite what the
324 : : * spec says to do: the spec seems to want a warning only if no privilege
325 : : * bits actually change in the ACL. In practice that behavior seems much
326 : : * too noisy, as well as inconsistent with the GRANT case.)
327 : : */
328 : 41323 : this_privileges = privileges & ACL_OPTION_TO_PRIVS(avail_goptions);
329 [ + + ]: 41323 : if (is_grant)
330 : : {
331 [ + + ]: 10148 : if (this_privileges == 0)
332 : : {
333 [ - + - - ]: 20 : if (objtype == OBJECT_COLUMN && colname)
334 [ # # ]: 0 : ereport(WARNING,
335 : : (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_GRANTED),
336 : : errmsg("no privileges were granted for column \"%s\" of relation \"%s\"",
337 : : colname, objname)));
338 : : else
339 [ + - ]: 20 : ereport(WARNING,
340 : : (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_GRANTED),
341 : : errmsg("no privileges were granted for \"%s\"",
342 : : objname)));
343 : : }
344 [ + + + + ]: 10128 : else if (!all_privs && this_privileges != privileges)
345 : : {
346 [ - + - - ]: 12 : if (objtype == OBJECT_COLUMN && colname)
347 [ # # ]: 0 : ereport(WARNING,
348 : : (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_GRANTED),
349 : : errmsg("not all privileges were granted for column \"%s\" of relation \"%s\"",
350 : : colname, objname)));
351 : : else
352 [ + - ]: 12 : ereport(WARNING,
353 : : (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_GRANTED),
354 : : errmsg("not all privileges were granted for \"%s\"",
355 : : objname)));
356 : : }
357 : : }
358 : : else
359 : : {
360 [ + + ]: 31175 : if (this_privileges == 0)
361 : : {
362 [ - + - - ]: 4 : if (objtype == OBJECT_COLUMN && colname)
363 [ # # ]: 0 : ereport(WARNING,
364 : : (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_REVOKED),
365 : : errmsg("no privileges could be revoked for column \"%s\" of relation \"%s\"",
366 : : colname, objname)));
367 : : else
368 [ + - ]: 4 : ereport(WARNING,
369 : : (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_REVOKED),
370 : : errmsg("no privileges could be revoked for \"%s\"",
371 : : objname)));
372 : : }
373 [ + + + + ]: 31171 : else if (!all_privs && this_privileges != privileges)
374 : : {
375 [ + + + - ]: 56 : if (objtype == OBJECT_COLUMN && colname)
376 [ + - ]: 48 : ereport(WARNING,
377 : : (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_REVOKED),
378 : : errmsg("not all privileges could be revoked for column \"%s\" of relation \"%s\"",
379 : : colname, objname)));
380 : : else
381 [ + - ]: 8 : ereport(WARNING,
382 : : (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_REVOKED),
383 : : errmsg("not all privileges could be revoked for \"%s\"",
384 : : objname)));
385 : : }
386 : : }
387 : :
388 : 41323 : return this_privileges;
389 : : }
390 : :
391 : : /*
392 : : * Called to execute the utility commands GRANT and REVOKE
393 : : */
394 : : void
395 : 12824 : ExecuteGrantStmt(GrantStmt *stmt)
396 : : {
397 : : InternalGrant istmt;
398 : : ListCell *cell;
399 : : const char *errormsg;
400 : : AclMode all_privileges;
401 : :
402 : : /*
403 : : * Turn the regular GrantStmt into the InternalGrant form.
404 : : */
405 : 12824 : istmt.is_grant = stmt->is_grant;
406 : 12824 : istmt.objtype = stmt->objtype;
407 : :
408 : : /* Collect the OIDs of the target objects */
409 [ + + - ]: 12824 : switch (stmt->targtype)
410 : : {
411 : 12803 : case ACL_TARGET_OBJECT:
412 : 25585 : istmt.objects = objectNamesToOids(stmt->objtype, stmt->objects,
413 : 12803 : stmt->is_grant);
414 : 12782 : break;
415 : 21 : case ACL_TARGET_ALL_IN_SCHEMA:
416 : 21 : istmt.objects = objectsInSchemaToOids(stmt->objtype, stmt->objects);
417 : 21 : break;
418 : : /* ACL_TARGET_DEFAULTS should not be seen here */
419 : 0 : default:
420 [ # # ]: 0 : elog(ERROR, "unrecognized GrantStmt.targtype: %d",
421 : : (int) stmt->targtype);
422 : : }
423 : :
424 : : /* all_privs to be filled below */
425 : : /* privileges to be filled below */
426 : 12803 : istmt.col_privs = NIL; /* may get filled below */
427 : 12803 : istmt.grantees = NIL; /* filled below */
428 : 12803 : istmt.grant_option = stmt->grant_option;
429 : 12803 : istmt.grantor = stmt->grantor;
430 : 12803 : istmt.behavior = stmt->behavior;
431 : :
432 : : /*
433 : : * Convert the RoleSpec list into an Oid list. Note that at this point we
434 : : * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
435 : : * there shouldn't be any additional work needed to support this case.
436 : : */
437 [ + - + + : 25669 : foreach(cell, stmt->grantees)
+ + ]
438 : : {
439 : 12870 : RoleSpec *grantee = (RoleSpec *) lfirst(cell);
440 : : Oid grantee_uid;
441 : :
442 [ + + ]: 12870 : switch (grantee->roletype)
443 : : {
444 : 10637 : case ROLESPEC_PUBLIC:
445 : 10637 : grantee_uid = ACL_ID_PUBLIC;
446 : 10637 : break;
447 : 2233 : default:
448 : 2233 : grantee_uid = get_rolespec_oid(grantee, false);
449 : 2229 : break;
450 : : }
451 : 12866 : istmt.grantees = lappend_oid(istmt.grantees, grantee_uid);
452 : : }
453 : :
454 : : /*
455 : : * Convert stmt->privileges, a list of AccessPriv nodes, into an AclMode
456 : : * bitmask. Note: objtype can't be OBJECT_COLUMN.
457 : : */
458 [ + + + + : 12799 : switch (stmt->objtype)
+ + + + +
+ + + + +
+ + - ]
459 : : {
460 : 11254 : case OBJECT_TABLE:
461 : :
462 : : /*
463 : : * Because this might be a sequence, we test both relation and
464 : : * sequence bits, and later do a more limited test when we know
465 : : * the object type.
466 : : */
467 : 11254 : all_privileges = ACL_ALL_RIGHTS_RELATION | ACL_ALL_RIGHTS_SEQUENCE;
468 : 11254 : errormsg = gettext_noop("invalid privilege type %s for relation");
469 : 11254 : break;
470 : 13 : case OBJECT_SEQUENCE:
471 : 13 : all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
472 : 13 : errormsg = gettext_noop("invalid privilege type %s for sequence");
473 : 13 : break;
474 : 214 : case OBJECT_DATABASE:
475 : 214 : all_privileges = ACL_ALL_RIGHTS_DATABASE;
476 : 214 : errormsg = gettext_noop("invalid privilege type %s for database");
477 : 214 : break;
478 : 13 : case OBJECT_DOMAIN:
479 : 13 : all_privileges = ACL_ALL_RIGHTS_TYPE;
480 : 13 : errormsg = gettext_noop("invalid privilege type %s for domain");
481 : 13 : break;
482 : 535 : case OBJECT_FUNCTION:
483 : 535 : all_privileges = ACL_ALL_RIGHTS_FUNCTION;
484 : 535 : errormsg = gettext_noop("invalid privilege type %s for function");
485 : 535 : break;
486 : 27 : case OBJECT_LANGUAGE:
487 : 27 : all_privileges = ACL_ALL_RIGHTS_LANGUAGE;
488 : 27 : errormsg = gettext_noop("invalid privilege type %s for language");
489 : 27 : break;
490 : 50 : case OBJECT_LARGEOBJECT:
491 : 50 : all_privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
492 : 50 : errormsg = gettext_noop("invalid privilege type %s for large object");
493 : 50 : break;
494 : 369 : case OBJECT_SCHEMA:
495 : 369 : all_privileges = ACL_ALL_RIGHTS_SCHEMA;
496 : 369 : errormsg = gettext_noop("invalid privilege type %s for schema");
497 : 369 : break;
498 : 32 : case OBJECT_PROCEDURE:
499 : 32 : all_privileges = ACL_ALL_RIGHTS_FUNCTION;
500 : 32 : errormsg = gettext_noop("invalid privilege type %s for procedure");
501 : 32 : break;
502 : 4 : case OBJECT_ROUTINE:
503 : 4 : all_privileges = ACL_ALL_RIGHTS_FUNCTION;
504 : 4 : errormsg = gettext_noop("invalid privilege type %s for routine");
505 : 4 : break;
506 : 3 : case OBJECT_TABLESPACE:
507 : 3 : all_privileges = ACL_ALL_RIGHTS_TABLESPACE;
508 : 3 : errormsg = gettext_noop("invalid privilege type %s for tablespace");
509 : 3 : break;
510 : 88 : case OBJECT_TYPE:
511 : 88 : all_privileges = ACL_ALL_RIGHTS_TYPE;
512 : 88 : errormsg = gettext_noop("invalid privilege type %s for type");
513 : 88 : break;
514 : 60 : case OBJECT_FDW:
515 : 60 : all_privileges = ACL_ALL_RIGHTS_FDW;
516 : 60 : errormsg = gettext_noop("invalid privilege type %s for foreign-data wrapper");
517 : 60 : break;
518 : 70 : case OBJECT_FOREIGN_SERVER:
519 : 70 : all_privileges = ACL_ALL_RIGHTS_FOREIGN_SERVER;
520 : 70 : errormsg = gettext_noop("invalid privilege type %s for foreign server");
521 : 70 : break;
522 : 37 : case OBJECT_PARAMETER_ACL:
523 : 37 : all_privileges = ACL_ALL_RIGHTS_PARAMETER_ACL;
524 : 37 : errormsg = gettext_noop("invalid privilege type %s for parameter");
525 : 37 : break;
526 : 30 : case OBJECT_PROPGRAPH:
527 : 30 : all_privileges = ACL_ALL_RIGHTS_PROPGRAPH;
528 : 30 : errormsg = gettext_noop("invalid privilege type %s for property graph");
529 : 30 : break;
530 : 0 : default:
531 [ # # ]: 0 : elog(ERROR, "unrecognized GrantStmt.objtype: %d",
532 : : (int) stmt->objtype);
533 : : /* keep compiler quiet */
534 : : all_privileges = ACL_NO_RIGHTS;
535 : : errormsg = NULL;
536 : : }
537 : :
538 [ + + ]: 12799 : if (stmt->privileges == NIL)
539 : : {
540 : 1661 : istmt.all_privs = true;
541 : :
542 : : /*
543 : : * will be turned into ACL_ALL_RIGHTS_* by the internal routines
544 : : * depending on the object type
545 : : */
546 : 1661 : istmt.privileges = ACL_NO_RIGHTS;
547 : : }
548 : : else
549 : : {
550 : 11138 : istmt.all_privs = false;
551 : 11138 : istmt.privileges = ACL_NO_RIGHTS;
552 : :
553 [ + - + + : 22645 : foreach(cell, stmt->privileges)
+ + ]
554 : : {
555 : 11527 : AccessPriv *privnode = (AccessPriv *) lfirst(cell);
556 : : AclMode priv;
557 : :
558 : : /*
559 : : * If it's a column-level specification, we just set it aside in
560 : : * col_privs for the moment; but insist it's for a relation.
561 : : */
562 [ + + ]: 11527 : if (privnode->cols)
563 : : {
564 [ - + ]: 319 : if (stmt->objtype != OBJECT_TABLE)
565 [ # # ]: 0 : ereport(ERROR,
566 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
567 : : errmsg("column privileges are only valid for relations")));
568 : 319 : istmt.col_privs = lappend(istmt.col_privs, privnode);
569 : 319 : continue;
570 : : }
571 : :
572 [ - + ]: 11208 : if (privnode->priv_name == NULL) /* parser mistake? */
573 [ # # ]: 0 : elog(ERROR, "AccessPriv node must specify privilege or columns");
574 : 11208 : priv = string_to_privilege(privnode->priv_name);
575 : :
576 [ + + ]: 11208 : if (priv & ~all_privileges)
577 [ + - ]: 20 : ereport(ERROR,
578 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
579 : : errmsg(errormsg, privilege_to_string(priv))));
580 : :
581 : 11188 : istmt.privileges |= priv;
582 : : }
583 : : }
584 : :
585 : 12779 : ExecGrantStmt_oids(&istmt);
586 : 12724 : }
587 : :
588 : : /*
589 : : * ExecGrantStmt_oids
590 : : *
591 : : * Internal entry point for granting and revoking privileges.
592 : : */
593 : : static void
594 : 12915 : ExecGrantStmt_oids(InternalGrant *istmt)
595 : : {
596 [ + + + + : 12915 : switch (istmt->objtype)
+ + + + +
+ + - ]
597 : : {
598 : 11355 : case OBJECT_TABLE:
599 : : case OBJECT_SEQUENCE:
600 : : case OBJECT_PROPGRAPH:
601 : 11355 : ExecGrant_Relation(istmt);
602 : 11336 : break;
603 : 220 : case OBJECT_DATABASE:
604 : 220 : ExecGrant_common(istmt, DatabaseRelationId, ACL_ALL_RIGHTS_DATABASE, NULL);
605 : 220 : break;
606 : 104 : case OBJECT_DOMAIN:
607 : : case OBJECT_TYPE:
608 : 104 : ExecGrant_common(istmt, TypeRelationId, ACL_ALL_RIGHTS_TYPE, ExecGrant_Type_check);
609 : 92 : break;
610 : 61 : case OBJECT_FDW:
611 : 61 : ExecGrant_common(istmt, ForeignDataWrapperRelationId, ACL_ALL_RIGHTS_FDW, NULL);
612 : 49 : break;
613 : 79 : case OBJECT_FOREIGN_SERVER:
614 : 79 : ExecGrant_common(istmt, ForeignServerRelationId, ACL_ALL_RIGHTS_FOREIGN_SERVER, NULL);
615 : 71 : break;
616 : 581 : case OBJECT_FUNCTION:
617 : : case OBJECT_PROCEDURE:
618 : : case OBJECT_ROUTINE:
619 : 581 : ExecGrant_common(istmt, ProcedureRelationId, ACL_ALL_RIGHTS_FUNCTION, NULL);
620 : 581 : break;
621 : 27 : case OBJECT_LANGUAGE:
622 : 27 : ExecGrant_common(istmt, LanguageRelationId, ACL_ALL_RIGHTS_LANGUAGE, ExecGrant_Language_check);
623 : 23 : break;
624 : 58 : case OBJECT_LARGEOBJECT:
625 : 58 : ExecGrant_Largeobject(istmt);
626 : 58 : break;
627 : 378 : case OBJECT_SCHEMA:
628 : 378 : ExecGrant_common(istmt, NamespaceRelationId, ACL_ALL_RIGHTS_SCHEMA, NULL);
629 : 378 : break;
630 : 3 : case OBJECT_TABLESPACE:
631 : 3 : ExecGrant_common(istmt, TableSpaceRelationId, ACL_ALL_RIGHTS_TABLESPACE, NULL);
632 : 3 : break;
633 : 49 : case OBJECT_PARAMETER_ACL:
634 : 49 : ExecGrant_Parameter(istmt);
635 : 49 : break;
636 : 0 : default:
637 [ # # ]: 0 : elog(ERROR, "unrecognized GrantStmt.objtype: %d",
638 : : (int) istmt->objtype);
639 : : }
640 : :
641 : : /*
642 : : * Pass the info to event triggers about the just-executed GRANT. Note
643 : : * that we prefer to do it after actually executing it, because that gives
644 : : * the functions a chance to adjust the istmt with privileges actually
645 : : * granted.
646 : : */
647 [ + + ]: 12860 : if (EventTriggerSupportsObjectType(istmt->objtype))
648 : 12588 : EventTriggerCollectGrant(istmt);
649 : 12860 : }
650 : :
651 : : /*
652 : : * objectNamesToOids
653 : : *
654 : : * Turn a list of object names of a given type into an Oid list.
655 : : *
656 : : * XXX This function intentionally takes only an AccessShareLock. In the face
657 : : * of concurrent DDL, we might easily latch onto an old version of an object,
658 : : * causing the GRANT or REVOKE statement to fail. But it does prevent the
659 : : * object from disappearing altogether. To do better, we would need to use a
660 : : * self-exclusive lock, perhaps ShareUpdateExclusiveLock, here and before
661 : : * *every* CatalogTupleUpdate() of a row that GRANT/REVOKE can affect.
662 : : * Besides that additional work, this could have operational costs. For
663 : : * example, it would make GRANT ALL TABLES IN SCHEMA terminate every
664 : : * autovacuum running in the schema and consume a shared lock table entry per
665 : : * table in the schema. The user-visible benefit of that additional work is
666 : : * just changing "ERROR: tuple concurrently updated" to blocking. That's not
667 : : * nothing, but it might not outweigh autovacuum termination and lock table
668 : : * consumption spikes.
669 : : */
670 : : static List *
671 : 12803 : objectNamesToOids(ObjectType objtype, List *objnames, bool is_grant)
672 : : {
673 : 12803 : List *objects = NIL;
674 : : ListCell *cell;
675 : 12803 : const LOCKMODE lockmode = AccessShareLock;
676 : :
677 : : Assert(objnames != NIL);
678 : :
679 [ + + + + ]: 12803 : switch (objtype)
680 : : {
681 : 1372 : default:
682 : :
683 : : /*
684 : : * For most object types, we use get_object_address() directly.
685 : : */
686 [ + - + + : 2819 : foreach(cell, objnames)
+ + ]
687 : : {
688 : : ObjectAddress address;
689 : :
690 : 1463 : address = get_object_address(objtype, lfirst(cell), NULL, lockmode, false);
691 : 1447 : objects = lappend_oid(objects, address.objectId);
692 : : }
693 : 1356 : break;
694 : :
695 : 11288 : case OBJECT_TABLE:
696 : : case OBJECT_SEQUENCE:
697 : : case OBJECT_PROPGRAPH:
698 : :
699 : : /*
700 : : * Here, we don't use get_object_address(). It requires that the
701 : : * specified object type match the actual type of the object, but
702 : : * in GRANT/REVOKE, all table-like things are addressed as TABLE.
703 : : */
704 [ + - + + : 22611 : foreach(cell, objnames)
+ + ]
705 : : {
706 : 11323 : RangeVar *relvar = (RangeVar *) lfirst(cell);
707 : : Oid relOid;
708 : :
709 : 11323 : relOid = RangeVarGetRelid(relvar, lockmode, false);
710 : 11323 : objects = lappend_oid(objects, relOid);
711 : : }
712 : 11288 : break;
713 : :
714 : 105 : case OBJECT_DOMAIN:
715 : : case OBJECT_TYPE:
716 : :
717 : : /*
718 : : * The parse representation of types and domains in privilege
719 : : * targets is different from that expected by get_object_address()
720 : : * (for parse conflict reasons), so we have to do a bit of
721 : : * conversion here.
722 : : */
723 [ + - + + : 206 : foreach(cell, objnames)
+ + ]
724 : : {
725 : 105 : List *typname = (List *) lfirst(cell);
726 : 105 : TypeName *tn = makeTypeNameFromNameList(typname);
727 : : ObjectAddress address;
728 : : Relation relation;
729 : :
730 : 105 : address = get_object_address(objtype, (Node *) tn, &relation, lockmode, false);
731 : : Assert(relation == NULL);
732 : 101 : objects = lappend_oid(objects, address.objectId);
733 : : }
734 : 101 : break;
735 : :
736 : 38 : case OBJECT_PARAMETER_ACL:
737 : :
738 : : /*
739 : : * Parameters are handled completely differently.
740 : : */
741 [ + - + + : 100 : foreach(cell, objnames)
+ + ]
742 : : {
743 : : /*
744 : : * In this code we represent a GUC by the OID of its entry in
745 : : * pg_parameter_acl, which we have to manufacture here if it
746 : : * doesn't exist yet. (That's a hack for sure, but it avoids
747 : : * messing with all the GRANT/REVOKE infrastructure that
748 : : * expects to use OIDs for object identities.) However, if
749 : : * this is a REVOKE, we can instead just ignore any GUCs that
750 : : * don't have such an entry, as they must not have any
751 : : * privileges needing removal.
752 : : */
753 : 63 : char *parameter = strVal(lfirst(cell));
754 : 63 : Oid parameterId = ParameterAclLookup(parameter, true);
755 : :
756 [ + + + + ]: 63 : if (!OidIsValid(parameterId) && is_grant)
757 : : {
758 : 34 : parameterId = ParameterAclCreate(parameter);
759 : :
760 : : /*
761 : : * Prevent error when processing duplicate objects, and
762 : : * make this new entry visible so that ExecGrant_Parameter
763 : : * can update it.
764 : : */
765 : 33 : CommandCounterIncrement();
766 : : }
767 [ + + ]: 62 : if (OidIsValid(parameterId))
768 : 56 : objects = lappend_oid(objects, parameterId);
769 : : }
770 : 37 : break;
771 : : }
772 : :
773 : 12782 : return objects;
774 : : }
775 : :
776 : : /*
777 : : * objectsInSchemaToOids
778 : : *
779 : : * Find all objects of a given type in specified schemas, and make a list
780 : : * of their Oids. We check USAGE privilege on the schemas, but there is
781 : : * no privilege checking on the individual objects here.
782 : : */
783 : : static List *
784 : 21 : objectsInSchemaToOids(ObjectType objtype, List *nspnames)
785 : : {
786 : 21 : List *objects = NIL;
787 : : ListCell *cell;
788 : :
789 [ + - + + : 42 : foreach(cell, nspnames)
+ + ]
790 : : {
791 : 21 : char *nspname = strVal(lfirst(cell));
792 : : Oid namespaceId;
793 : : List *objs;
794 : :
795 : 21 : namespaceId = LookupExplicitNamespace(nspname, false);
796 : :
797 [ + + - + : 21 : switch (objtype)
- ]
798 : : {
799 : 8 : case OBJECT_TABLE:
800 : 8 : objs = getRelationsInNamespace(namespaceId, RELKIND_RELATION);
801 : 8 : objects = list_concat(objects, objs);
802 : 8 : objs = getRelationsInNamespace(namespaceId, RELKIND_VIEW);
803 : 8 : objects = list_concat(objects, objs);
804 : 8 : objs = getRelationsInNamespace(namespaceId, RELKIND_MATVIEW);
805 : 8 : objects = list_concat(objects, objs);
806 : 8 : objs = getRelationsInNamespace(namespaceId, RELKIND_FOREIGN_TABLE);
807 : 8 : objects = list_concat(objects, objs);
808 : 8 : objs = getRelationsInNamespace(namespaceId, RELKIND_PARTITIONED_TABLE);
809 : 8 : objects = list_concat(objects, objs);
810 : 8 : break;
811 : 1 : case OBJECT_SEQUENCE:
812 : 1 : objs = getRelationsInNamespace(namespaceId, RELKIND_SEQUENCE);
813 : 1 : objects = list_concat(objects, objs);
814 : 1 : break;
815 : 0 : case OBJECT_PROPGRAPH:
816 : 0 : objs = getRelationsInNamespace(namespaceId, RELKIND_PROPGRAPH);
817 : 0 : objects = list_concat(objects, objs);
818 : 0 : break;
819 : 12 : case OBJECT_FUNCTION:
820 : : case OBJECT_PROCEDURE:
821 : : case OBJECT_ROUTINE:
822 : : {
823 : : ScanKeyData key[2];
824 : : int keycount;
825 : : Relation rel;
826 : : TableScanDesc scan;
827 : : HeapTuple tuple;
828 : :
829 : 12 : keycount = 0;
830 : 12 : ScanKeyInit(&key[keycount++],
831 : : Anum_pg_proc_pronamespace,
832 : : BTEqualStrategyNumber, F_OIDEQ,
833 : : ObjectIdGetDatum(namespaceId));
834 : :
835 [ + + ]: 12 : if (objtype == OBJECT_FUNCTION)
836 : : /* includes aggregates and window functions */
837 : 4 : ScanKeyInit(&key[keycount++],
838 : : Anum_pg_proc_prokind,
839 : : BTEqualStrategyNumber, F_CHARNE,
840 : : CharGetDatum(PROKIND_PROCEDURE));
841 [ + + ]: 8 : else if (objtype == OBJECT_PROCEDURE)
842 : 4 : ScanKeyInit(&key[keycount++],
843 : : Anum_pg_proc_prokind,
844 : : BTEqualStrategyNumber, F_CHAREQ,
845 : : CharGetDatum(PROKIND_PROCEDURE));
846 : :
847 : 12 : rel = table_open(ProcedureRelationId, AccessShareLock);
848 : 12 : scan = table_beginscan_catalog(rel, keycount, key);
849 : :
850 [ + + ]: 36 : while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
851 : : {
852 : 24 : Oid oid = ((Form_pg_proc) GETSTRUCT(tuple))->oid;
853 : :
854 : 24 : objects = lappend_oid(objects, oid);
855 : : }
856 : :
857 : 12 : table_endscan(scan);
858 : 12 : table_close(rel, AccessShareLock);
859 : : }
860 : 12 : break;
861 : 0 : default:
862 : : /* should not happen */
863 [ # # ]: 0 : elog(ERROR, "unrecognized GrantStmt.objtype: %d",
864 : : (int) objtype);
865 : : }
866 : : }
867 : :
868 : 21 : return objects;
869 : : }
870 : :
871 : : /*
872 : : * getRelationsInNamespace
873 : : *
874 : : * Return Oid list of relations in given namespace filtered by relation kind
875 : : */
876 : : static List *
877 : 41 : getRelationsInNamespace(Oid namespaceId, char relkind)
878 : : {
879 : 41 : List *relations = NIL;
880 : : ScanKeyData key[2];
881 : : Relation rel;
882 : : TableScanDesc scan;
883 : : HeapTuple tuple;
884 : :
885 : 41 : ScanKeyInit(&key[0],
886 : : Anum_pg_class_relnamespace,
887 : : BTEqualStrategyNumber, F_OIDEQ,
888 : : ObjectIdGetDatum(namespaceId));
889 : 41 : ScanKeyInit(&key[1],
890 : : Anum_pg_class_relkind,
891 : : BTEqualStrategyNumber, F_CHAREQ,
892 : : CharGetDatum(relkind));
893 : :
894 : 41 : rel = table_open(RelationRelationId, AccessShareLock);
895 : 41 : scan = table_beginscan_catalog(rel, 2, key);
896 : :
897 [ + + ]: 62 : while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
898 : : {
899 : 21 : Oid oid = ((Form_pg_class) GETSTRUCT(tuple))->oid;
900 : :
901 : 21 : relations = lappend_oid(relations, oid);
902 : : }
903 : :
904 : 41 : table_endscan(scan);
905 : 41 : table_close(rel, AccessShareLock);
906 : :
907 : 41 : return relations;
908 : : }
909 : :
910 : :
911 : : /*
912 : : * ALTER DEFAULT PRIVILEGES statement
913 : : */
914 : : void
915 : 135 : ExecAlterDefaultPrivilegesStmt(ParseState *pstate, AlterDefaultPrivilegesStmt *stmt)
916 : : {
917 : 135 : GrantStmt *action = stmt->action;
918 : : InternalDefaultACL iacls;
919 : : ListCell *cell;
920 : 135 : List *rolespecs = NIL;
921 : 135 : List *nspnames = NIL;
922 : 135 : DefElem *drolespecs = NULL;
923 : 135 : DefElem *dnspnames = NULL;
924 : : AclMode all_privileges;
925 : : const char *errormsg;
926 : :
927 : : /* Deconstruct the "options" part of the statement */
928 [ + + + + : 228 : foreach(cell, stmt->options)
+ + ]
929 : : {
930 : 93 : DefElem *defel = (DefElem *) lfirst(cell);
931 : :
932 [ + + ]: 93 : if (strcmp(defel->defname, "schemas") == 0)
933 : : {
934 [ - + ]: 39 : if (dnspnames)
935 : 0 : errorConflictingDefElem(defel, pstate);
936 : 39 : dnspnames = defel;
937 : : }
938 [ + - ]: 54 : else if (strcmp(defel->defname, "roles") == 0)
939 : : {
940 [ - + ]: 54 : if (drolespecs)
941 : 0 : errorConflictingDefElem(defel, pstate);
942 : 54 : drolespecs = defel;
943 : : }
944 : : else
945 [ # # ]: 0 : elog(ERROR, "option \"%s\" not recognized", defel->defname);
946 : : }
947 : :
948 [ + + ]: 135 : if (dnspnames)
949 : 39 : nspnames = (List *) dnspnames->arg;
950 [ + + ]: 135 : if (drolespecs)
951 : 54 : rolespecs = (List *) drolespecs->arg;
952 : :
953 : : /* Prepare the InternalDefaultACL representation of the statement */
954 : : /* roleid to be filled below */
955 : : /* nspid to be filled in SetDefaultACLsInSchemas */
956 : 135 : iacls.is_grant = action->is_grant;
957 : 135 : iacls.objtype = action->objtype;
958 : : /* all_privs to be filled below */
959 : : /* privileges to be filled below */
960 : 135 : iacls.grantees = NIL; /* filled below */
961 : 135 : iacls.grant_option = action->grant_option;
962 : 135 : iacls.grantor = action->grantor;
963 : 135 : 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 [ + - + + : 274 : foreach(cell, action->grantees)
+ + ]
971 : : {
972 : 139 : RoleSpec *grantee = (RoleSpec *) lfirst(cell);
973 : : Oid grantee_uid;
974 : :
975 [ + + ]: 139 : switch (grantee->roletype)
976 : : {
977 : 30 : case ROLESPEC_PUBLIC:
978 : 30 : grantee_uid = ACL_ID_PUBLIC;
979 : 30 : break;
980 : 109 : default:
981 : 109 : grantee_uid = get_rolespec_oid(grantee, false);
982 : 109 : break;
983 : : }
984 : 139 : 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 [ + + + - : 135 : switch (action->objtype)
- + + + -
- ]
992 : : {
993 : 51 : case OBJECT_TABLE:
994 : 51 : all_privileges = ACL_ALL_RIGHTS_RELATION;
995 : 51 : errormsg = gettext_noop("invalid privilege type %s for relation");
996 : 51 : break;
997 : 4 : case OBJECT_SEQUENCE:
998 : 4 : all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
999 : 4 : errormsg = gettext_noop("invalid privilege type %s for sequence");
1000 : 4 : break;
1001 : 14 : case OBJECT_FUNCTION:
1002 : 14 : all_privileges = ACL_ALL_RIGHTS_FUNCTION;
1003 : 14 : errormsg = gettext_noop("invalid privilege type %s for function");
1004 : 14 : break;
1005 : 0 : case OBJECT_PROCEDURE:
1006 : 0 : all_privileges = ACL_ALL_RIGHTS_FUNCTION;
1007 : 0 : errormsg = gettext_noop("invalid privilege type %s for procedure");
1008 : 0 : break;
1009 : 0 : case OBJECT_ROUTINE:
1010 : 0 : all_privileges = ACL_ALL_RIGHTS_FUNCTION;
1011 : 0 : errormsg = gettext_noop("invalid privilege type %s for routine");
1012 : 0 : break;
1013 : 22 : case OBJECT_TYPE:
1014 : 22 : all_privileges = ACL_ALL_RIGHTS_TYPE;
1015 : 22 : errormsg = gettext_noop("invalid privilege type %s for type");
1016 : 22 : break;
1017 : 24 : case OBJECT_SCHEMA:
1018 : 24 : all_privileges = ACL_ALL_RIGHTS_SCHEMA;
1019 : 24 : errormsg = gettext_noop("invalid privilege type %s for schema");
1020 : 24 : break;
1021 : 20 : case OBJECT_LARGEOBJECT:
1022 : 20 : all_privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
1023 : 20 : errormsg = gettext_noop("invalid privilege type %s for large object");
1024 : 20 : break;
1025 : 0 : case OBJECT_PROPGRAPH:
1026 : 0 : all_privileges = ACL_ALL_RIGHTS_PROPGRAPH;
1027 : 0 : errormsg = gettext_noop("invalid privilege type %s for property graph");
1028 : 0 : break;
1029 : 0 : default:
1030 [ # # ]: 0 : elog(ERROR, "unrecognized GrantStmt.objtype: %d",
1031 : : (int) action->objtype);
1032 : : /* keep compiler quiet */
1033 : : all_privileges = ACL_NO_RIGHTS;
1034 : : errormsg = NULL;
1035 : : }
1036 : :
1037 [ + + ]: 135 : if (action->privileges == NIL)
1038 : : {
1039 : 51 : iacls.all_privs = true;
1040 : :
1041 : : /*
1042 : : * will be turned into ACL_ALL_RIGHTS_* by the internal routines
1043 : : * depending on the object type
1044 : : */
1045 : 51 : iacls.privileges = ACL_NO_RIGHTS;
1046 : : }
1047 : : else
1048 : : {
1049 : 84 : iacls.all_privs = false;
1050 : 84 : iacls.privileges = ACL_NO_RIGHTS;
1051 : :
1052 [ + - + + : 168 : foreach(cell, action->privileges)
+ + ]
1053 : : {
1054 : 84 : AccessPriv *privnode = (AccessPriv *) lfirst(cell);
1055 : : AclMode priv;
1056 : :
1057 [ - + ]: 84 : if (privnode->cols)
1058 [ # # ]: 0 : ereport(ERROR,
1059 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1060 : : errmsg("default privileges cannot be set for columns")));
1061 : :
1062 [ - + ]: 84 : if (privnode->priv_name == NULL) /* parser mistake? */
1063 [ # # ]: 0 : elog(ERROR, "AccessPriv node must specify privilege");
1064 : 84 : priv = string_to_privilege(privnode->priv_name);
1065 : :
1066 [ - + ]: 84 : if (priv & ~all_privileges)
1067 [ # # ]: 0 : ereport(ERROR,
1068 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1069 : : errmsg(errormsg, privilege_to_string(priv))));
1070 : :
1071 : 84 : iacls.privileges |= priv;
1072 : : }
1073 : : }
1074 : :
1075 [ + + ]: 135 : if (rolespecs == NIL)
1076 : : {
1077 : : /* Set permissions for myself */
1078 : 81 : iacls.roleid = GetUserId();
1079 : :
1080 : 81 : SetDefaultACLsInSchemas(&iacls, nspnames);
1081 : : }
1082 : : else
1083 : : {
1084 : : /* Look up the role OIDs and do permissions checks */
1085 : : ListCell *rolecell;
1086 : :
1087 [ + - + + : 108 : foreach(rolecell, rolespecs)
+ + ]
1088 : : {
1089 : 54 : RoleSpec *rolespec = lfirst(rolecell);
1090 : :
1091 : 54 : iacls.roleid = get_rolespec_oid(rolespec, false);
1092 : :
1093 [ - + ]: 54 : if (!has_privs_of_role(GetUserId(), iacls.roleid))
1094 [ # # ]: 0 : ereport(ERROR,
1095 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1096 : : errmsg("permission denied to change default privileges")));
1097 : :
1098 : 54 : SetDefaultACLsInSchemas(&iacls, nspnames);
1099 : : }
1100 : : }
1101 : 127 : }
1102 : :
1103 : : /*
1104 : : * Process ALTER DEFAULT PRIVILEGES for a list of target schemas
1105 : : *
1106 : : * All fields of *iacls except nspid were filled already
1107 : : */
1108 : : static void
1109 : 135 : SetDefaultACLsInSchemas(InternalDefaultACL *iacls, List *nspnames)
1110 : : {
1111 [ + + ]: 135 : if (nspnames == NIL)
1112 : : {
1113 : : /* Set database-wide permissions if no schema was specified */
1114 : 96 : iacls->nspid = InvalidOid;
1115 : :
1116 : 96 : SetDefaultACL(iacls);
1117 : : }
1118 : : else
1119 : : {
1120 : : /* Look up the schema OIDs and set permissions for each one */
1121 : : ListCell *nspcell;
1122 : :
1123 [ + - + + : 74 : foreach(nspcell, nspnames)
+ + ]
1124 : : {
1125 : 43 : char *nspname = strVal(lfirst(nspcell));
1126 : :
1127 : 43 : iacls->nspid = get_namespace_oid(nspname, false);
1128 : :
1129 : : /*
1130 : : * We used to insist that the target role have CREATE privileges
1131 : : * on the schema, since without that it wouldn't be able to create
1132 : : * an object for which these default privileges would apply.
1133 : : * However, this check proved to be more confusing than helpful,
1134 : : * and it also caused certain database states to not be
1135 : : * dumpable/restorable, since revoking CREATE doesn't cause
1136 : : * default privileges for the schema to go away. So now, we just
1137 : : * allow the ALTER; if the user lacks CREATE he'll find out when
1138 : : * he tries to create an object.
1139 : : */
1140 : :
1141 : 43 : SetDefaultACL(iacls);
1142 : : }
1143 : : }
1144 : 127 : }
1145 : :
1146 : :
1147 : : /*
1148 : : * Create or update a pg_default_acl entry
1149 : : */
1150 : : static void
1151 : 163 : SetDefaultACL(InternalDefaultACL *iacls)
1152 : : {
1153 : 163 : AclMode this_privileges = iacls->privileges;
1154 : : char objtype;
1155 : : Relation rel;
1156 : : HeapTuple tuple;
1157 : : bool isNew;
1158 : : Acl *def_acl;
1159 : : Acl *old_acl;
1160 : : Acl *new_acl;
1161 : : HeapTuple newtuple;
1162 : : int noldmembers;
1163 : : int nnewmembers;
1164 : : Oid *oldmembers;
1165 : : Oid *newmembers;
1166 : :
1167 : 163 : rel = table_open(DefaultAclRelationId, RowExclusiveLock);
1168 : :
1169 : : /*
1170 : : * The default for a global entry is the hard-wired default ACL for the
1171 : : * particular object type. The default for non-global entries is an empty
1172 : : * ACL. This must be so because global entries replace the hard-wired
1173 : : * defaults, while others are added on.
1174 : : */
1175 [ + + ]: 163 : if (!OidIsValid(iacls->nspid))
1176 : 120 : def_acl = acldefault(iacls->objtype, iacls->roleid);
1177 : : else
1178 : 43 : def_acl = make_empty_acl();
1179 : :
1180 : : /*
1181 : : * Convert ACL object type to pg_default_acl object type and handle
1182 : : * all_privs option
1183 : : */
1184 [ + + + + : 163 : switch (iacls->objtype)
+ + - ]
1185 : : {
1186 : 59 : case OBJECT_TABLE:
1187 : 59 : objtype = DEFACLOBJ_RELATION;
1188 [ + + + - ]: 59 : if (iacls->all_privs && this_privileges == ACL_NO_RIGHTS)
1189 : 17 : this_privileges = ACL_ALL_RIGHTS_RELATION;
1190 : 59 : break;
1191 : :
1192 : 8 : case OBJECT_SEQUENCE:
1193 : 8 : objtype = DEFACLOBJ_SEQUENCE;
1194 [ + - + - ]: 8 : if (iacls->all_privs && this_privileges == ACL_NO_RIGHTS)
1195 : 8 : this_privileges = ACL_ALL_RIGHTS_SEQUENCE;
1196 : 8 : break;
1197 : :
1198 : 18 : case OBJECT_FUNCTION:
1199 : 18 : objtype = DEFACLOBJ_FUNCTION;
1200 [ + + + - ]: 18 : if (iacls->all_privs && this_privileges == ACL_NO_RIGHTS)
1201 : 8 : this_privileges = ACL_ALL_RIGHTS_FUNCTION;
1202 : 18 : break;
1203 : :
1204 : 26 : case OBJECT_TYPE:
1205 : 26 : objtype = DEFACLOBJ_TYPE;
1206 [ + + + - ]: 26 : if (iacls->all_privs && this_privileges == ACL_NO_RIGHTS)
1207 : 10 : this_privileges = ACL_ALL_RIGHTS_TYPE;
1208 : 26 : break;
1209 : :
1210 : 28 : case OBJECT_SCHEMA:
1211 [ + + ]: 28 : if (OidIsValid(iacls->nspid))
1212 [ + - ]: 4 : ereport(ERROR,
1213 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1214 : : errmsg("cannot use IN SCHEMA clause when using %s",
1215 : : "GRANT/REVOKE ON SCHEMAS")));
1216 : 24 : objtype = DEFACLOBJ_NAMESPACE;
1217 [ + + + - ]: 24 : if (iacls->all_privs && this_privileges == ACL_NO_RIGHTS)
1218 : 16 : this_privileges = ACL_ALL_RIGHTS_SCHEMA;
1219 : 24 : break;
1220 : :
1221 : 24 : case OBJECT_LARGEOBJECT:
1222 [ + + ]: 24 : if (OidIsValid(iacls->nspid))
1223 [ + - ]: 4 : ereport(ERROR,
1224 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1225 : : errmsg("cannot use IN SCHEMA clause when using %s",
1226 : : "GRANT/REVOKE ON LARGE OBJECTS")));
1227 : 20 : objtype = DEFACLOBJ_LARGEOBJECT;
1228 [ + + + - ]: 20 : if (iacls->all_privs && this_privileges == ACL_NO_RIGHTS)
1229 : 12 : this_privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
1230 : 20 : break;
1231 : :
1232 : 0 : default:
1233 [ # # ]: 0 : elog(ERROR, "unrecognized object type: %d",
1234 : : (int) iacls->objtype);
1235 : : objtype = 0; /* keep compiler quiet */
1236 : : break;
1237 : : }
1238 : :
1239 : : /* Search for existing row for this object type in catalog */
1240 : 155 : tuple = SearchSysCache3(DEFACLROLENSPOBJ,
1241 : : ObjectIdGetDatum(iacls->roleid),
1242 : : ObjectIdGetDatum(iacls->nspid),
1243 : : CharGetDatum(objtype));
1244 : :
1245 [ + + ]: 155 : if (HeapTupleIsValid(tuple))
1246 : : {
1247 : : Datum aclDatum;
1248 : : bool isNull;
1249 : :
1250 : 60 : aclDatum = SysCacheGetAttr(DEFACLROLENSPOBJ, tuple,
1251 : : Anum_pg_default_acl_defaclacl,
1252 : : &isNull);
1253 [ + - ]: 60 : if (!isNull)
1254 : 60 : old_acl = DatumGetAclPCopy(aclDatum);
1255 : : else
1256 : 0 : old_acl = NULL; /* this case shouldn't happen, probably */
1257 : 60 : isNew = false;
1258 : : }
1259 : : else
1260 : : {
1261 : 95 : old_acl = NULL;
1262 : 95 : isNew = true;
1263 : : }
1264 : :
1265 [ + + ]: 155 : if (old_acl != NULL)
1266 : : {
1267 : : /*
1268 : : * We need the members of both old and new ACLs so we can correct the
1269 : : * shared dependency information. Collect data before
1270 : : * merge_acl_with_grant throws away old_acl.
1271 : : */
1272 : 60 : noldmembers = aclmembers(old_acl, &oldmembers);
1273 : : }
1274 : : else
1275 : : {
1276 : : /* If no or null entry, start with the default ACL value */
1277 : 95 : old_acl = aclcopy(def_acl);
1278 : : /* There are no old member roles according to the catalogs */
1279 : 95 : noldmembers = 0;
1280 : 95 : oldmembers = NULL;
1281 : : }
1282 : :
1283 : : /*
1284 : : * Generate new ACL. Grantor of rights is always the same as the target
1285 : : * role.
1286 : : */
1287 : 155 : new_acl = merge_acl_with_grant(old_acl,
1288 : 155 : iacls->is_grant,
1289 : 155 : iacls->grant_option,
1290 : : iacls->behavior,
1291 : : iacls->grantees,
1292 : : this_privileges,
1293 : : iacls->roleid,
1294 : : iacls->roleid);
1295 : :
1296 : : /*
1297 : : * If the result is the same as the default value, we do not need an
1298 : : * explicit pg_default_acl entry, and should in fact remove the entry if
1299 : : * it exists. Must sort both arrays to compare properly.
1300 : : */
1301 : 155 : aclitemsort(new_acl);
1302 : 155 : aclitemsort(def_acl);
1303 [ + + ]: 155 : if (aclequal(new_acl, def_acl))
1304 : : {
1305 : : /* delete old entry, if indeed there is one */
1306 [ + + ]: 41 : if (!isNew)
1307 : : {
1308 : : ObjectAddress myself;
1309 : :
1310 : : /*
1311 : : * The dependency machinery will take care of removing all
1312 : : * associated dependency entries. We use DROP_RESTRICT since
1313 : : * there shouldn't be anything depending on this entry.
1314 : : */
1315 : 40 : myself.classId = DefaultAclRelationId;
1316 : 40 : myself.objectId = ((Form_pg_default_acl) GETSTRUCT(tuple))->oid;
1317 : 40 : myself.objectSubId = 0;
1318 : :
1319 : 40 : performDeletion(&myself, DROP_RESTRICT, 0);
1320 : : }
1321 : : }
1322 : : else
1323 : : {
1324 : 114 : Datum values[Natts_pg_default_acl] = {0};
1325 : 114 : bool nulls[Natts_pg_default_acl] = {0};
1326 : 114 : bool replaces[Natts_pg_default_acl] = {0};
1327 : : Oid defAclOid;
1328 : :
1329 [ + + ]: 114 : if (isNew)
1330 : : {
1331 : : /* insert new entry */
1332 : 94 : defAclOid = GetNewOidWithIndex(rel, DefaultAclOidIndexId,
1333 : : Anum_pg_default_acl_oid);
1334 : 94 : values[Anum_pg_default_acl_oid - 1] = ObjectIdGetDatum(defAclOid);
1335 : 94 : values[Anum_pg_default_acl_defaclrole - 1] = ObjectIdGetDatum(iacls->roleid);
1336 : 94 : values[Anum_pg_default_acl_defaclnamespace - 1] = ObjectIdGetDatum(iacls->nspid);
1337 : 94 : values[Anum_pg_default_acl_defaclobjtype - 1] = CharGetDatum(objtype);
1338 : 94 : values[Anum_pg_default_acl_defaclacl - 1] = PointerGetDatum(new_acl);
1339 : :
1340 : 94 : newtuple = heap_form_tuple(RelationGetDescr(rel), values, nulls);
1341 : 94 : CatalogTupleInsert(rel, newtuple);
1342 : : }
1343 : : else
1344 : : {
1345 : 20 : defAclOid = ((Form_pg_default_acl) GETSTRUCT(tuple))->oid;
1346 : :
1347 : : /* update existing entry */
1348 : 20 : values[Anum_pg_default_acl_defaclacl - 1] = PointerGetDatum(new_acl);
1349 : 20 : replaces[Anum_pg_default_acl_defaclacl - 1] = true;
1350 : :
1351 : 20 : newtuple = heap_modify_tuple(tuple, RelationGetDescr(rel),
1352 : : values, nulls, replaces);
1353 : 20 : CatalogTupleUpdate(rel, &newtuple->t_self, newtuple);
1354 : : }
1355 : :
1356 : : /* these dependencies don't change in an update */
1357 [ + + ]: 114 : if (isNew)
1358 : : {
1359 : : /* dependency on role */
1360 : 94 : recordDependencyOnOwner(DefaultAclRelationId, defAclOid,
1361 : : iacls->roleid);
1362 : :
1363 : : /* dependency on namespace */
1364 [ + + ]: 94 : if (OidIsValid(iacls->nspid))
1365 : : {
1366 : : ObjectAddress myself,
1367 : : referenced;
1368 : :
1369 : 22 : myself.classId = DefaultAclRelationId;
1370 : 22 : myself.objectId = defAclOid;
1371 : 22 : myself.objectSubId = 0;
1372 : :
1373 : 22 : referenced.classId = NamespaceRelationId;
1374 : 22 : referenced.objectId = iacls->nspid;
1375 : 22 : referenced.objectSubId = 0;
1376 : :
1377 : 22 : recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
1378 : : }
1379 : : }
1380 : :
1381 : : /*
1382 : : * Update the shared dependency ACL info
1383 : : */
1384 : 114 : nnewmembers = aclmembers(new_acl, &newmembers);
1385 : :
1386 : 114 : updateAclDependencies(DefaultAclRelationId,
1387 : : defAclOid, 0,
1388 : : iacls->roleid,
1389 : : noldmembers, oldmembers,
1390 : : nnewmembers, newmembers);
1391 : :
1392 [ + + ]: 114 : if (isNew)
1393 [ - + ]: 94 : InvokeObjectPostCreateHook(DefaultAclRelationId, defAclOid, 0);
1394 : : else
1395 [ - + ]: 20 : InvokeObjectPostAlterHook(DefaultAclRelationId, defAclOid, 0);
1396 : : }
1397 : :
1398 [ + + ]: 155 : if (HeapTupleIsValid(tuple))
1399 : 60 : ReleaseSysCache(tuple);
1400 : :
1401 : 155 : table_close(rel, RowExclusiveLock);
1402 : :
1403 : : /* prevent error when processing duplicate objects */
1404 : 155 : CommandCounterIncrement();
1405 : 155 : }
1406 : :
1407 : :
1408 : : /*
1409 : : * RemoveRoleFromObjectACL
1410 : : *
1411 : : * Used by shdepDropOwned to remove mentions of a role in ACLs.
1412 : : *
1413 : : * Notice that this doesn't accept an objsubid parameter, which is a bit bogus
1414 : : * since the pg_shdepend record that caused us to call it certainly had one.
1415 : : * If, for example, pg_shdepend records the existence of a permission on
1416 : : * mytable.mycol, this function will effectively issue a REVOKE ALL ON TABLE
1417 : : * mytable. That gets the job done because (per SQL spec) such a REVOKE also
1418 : : * revokes per-column permissions. We could not recreate a situation where
1419 : : * the role has table-level but not column-level permissions; but it's okay
1420 : : * (for now anyway) because this is only used when we're dropping the role
1421 : : * and so all its permissions everywhere must go away. At worst it's a bit
1422 : : * inefficient if the role has column permissions on several columns of the
1423 : : * same table.
1424 : : */
1425 : : void
1426 : 160 : RemoveRoleFromObjectACL(Oid roleid, Oid classid, Oid objid)
1427 : : {
1428 [ + + ]: 160 : if (classid == DefaultAclRelationId)
1429 : : {
1430 : : InternalDefaultACL iacls;
1431 : : Form_pg_default_acl pg_default_acl_tuple;
1432 : : Relation rel;
1433 : : ScanKeyData skey[1];
1434 : : SysScanDesc scan;
1435 : : HeapTuple tuple;
1436 : :
1437 : : /* first fetch info needed by SetDefaultACL */
1438 : 24 : rel = table_open(DefaultAclRelationId, AccessShareLock);
1439 : :
1440 : 24 : ScanKeyInit(&skey[0],
1441 : : Anum_pg_default_acl_oid,
1442 : : BTEqualStrategyNumber, F_OIDEQ,
1443 : : ObjectIdGetDatum(objid));
1444 : :
1445 : 24 : scan = systable_beginscan(rel, DefaultAclOidIndexId, true,
1446 : : NULL, 1, skey);
1447 : :
1448 : 24 : tuple = systable_getnext(scan);
1449 : :
1450 [ - + ]: 24 : if (!HeapTupleIsValid(tuple))
1451 [ # # ]: 0 : elog(ERROR, "could not find tuple for default ACL %u", objid);
1452 : :
1453 : 24 : pg_default_acl_tuple = (Form_pg_default_acl) GETSTRUCT(tuple);
1454 : :
1455 : 24 : iacls.roleid = pg_default_acl_tuple->defaclrole;
1456 : 24 : iacls.nspid = pg_default_acl_tuple->defaclnamespace;
1457 : :
1458 [ + + + + : 24 : switch (pg_default_acl_tuple->defaclobjtype)
+ + - ]
1459 : : {
1460 : 4 : case DEFACLOBJ_RELATION:
1461 : 4 : iacls.objtype = OBJECT_TABLE;
1462 : 4 : break;
1463 : 4 : case DEFACLOBJ_SEQUENCE:
1464 : 4 : iacls.objtype = OBJECT_SEQUENCE;
1465 : 4 : break;
1466 : 4 : case DEFACLOBJ_FUNCTION:
1467 : 4 : iacls.objtype = OBJECT_FUNCTION;
1468 : 4 : break;
1469 : 4 : case DEFACLOBJ_TYPE:
1470 : 4 : iacls.objtype = OBJECT_TYPE;
1471 : 4 : break;
1472 : 4 : case DEFACLOBJ_NAMESPACE:
1473 : 4 : iacls.objtype = OBJECT_SCHEMA;
1474 : 4 : break;
1475 : 4 : case DEFACLOBJ_LARGEOBJECT:
1476 : 4 : iacls.objtype = OBJECT_LARGEOBJECT;
1477 : 4 : break;
1478 : 0 : default:
1479 : : /* Shouldn't get here */
1480 [ # # ]: 0 : elog(ERROR, "unexpected default ACL type: %d",
1481 : : (int) pg_default_acl_tuple->defaclobjtype);
1482 : : break;
1483 : : }
1484 : :
1485 : 24 : systable_endscan(scan);
1486 : 24 : table_close(rel, AccessShareLock);
1487 : :
1488 : 24 : iacls.is_grant = false;
1489 : 24 : iacls.all_privs = true;
1490 : 24 : iacls.privileges = ACL_NO_RIGHTS;
1491 : 24 : iacls.grantees = list_make1_oid(roleid);
1492 : 24 : iacls.grant_option = false;
1493 : 24 : iacls.grantor = NULL;
1494 : 24 : iacls.behavior = DROP_CASCADE;
1495 : :
1496 : : /* Do it */
1497 : 24 : SetDefaultACL(&iacls);
1498 : : }
1499 : : else
1500 : : {
1501 : : InternalGrant istmt;
1502 : :
1503 [ + + + + : 136 : switch (classid)
- + + - +
+ + - ]
1504 : : {
1505 : 62 : case RelationRelationId:
1506 : : /* it's OK to use TABLE for a sequence */
1507 : 62 : istmt.objtype = OBJECT_TABLE;
1508 : 62 : break;
1509 : 6 : case DatabaseRelationId:
1510 : 6 : istmt.objtype = OBJECT_DATABASE;
1511 : 6 : break;
1512 : 3 : case TypeRelationId:
1513 : 3 : istmt.objtype = OBJECT_TYPE;
1514 : 3 : break;
1515 : 22 : case ProcedureRelationId:
1516 : 22 : istmt.objtype = OBJECT_ROUTINE;
1517 : 22 : break;
1518 : 0 : case LanguageRelationId:
1519 : 0 : istmt.objtype = OBJECT_LANGUAGE;
1520 : 0 : break;
1521 : 12 : case LargeObjectRelationId:
1522 : 12 : istmt.objtype = OBJECT_LARGEOBJECT;
1523 : 12 : break;
1524 : 9 : case NamespaceRelationId:
1525 : 9 : istmt.objtype = OBJECT_SCHEMA;
1526 : 9 : break;
1527 : 0 : case TableSpaceRelationId:
1528 : 0 : istmt.objtype = OBJECT_TABLESPACE;
1529 : 0 : break;
1530 : 9 : case ForeignServerRelationId:
1531 : 9 : istmt.objtype = OBJECT_FOREIGN_SERVER;
1532 : 9 : break;
1533 : 1 : case ForeignDataWrapperRelationId:
1534 : 1 : istmt.objtype = OBJECT_FDW;
1535 : 1 : break;
1536 : 12 : case ParameterAclRelationId:
1537 : 12 : istmt.objtype = OBJECT_PARAMETER_ACL;
1538 : 12 : break;
1539 : 0 : default:
1540 [ # # ]: 0 : elog(ERROR, "unexpected object class %u", classid);
1541 : : break;
1542 : : }
1543 : 136 : istmt.is_grant = false;
1544 : 136 : istmt.objects = list_make1_oid(objid);
1545 : 136 : istmt.all_privs = true;
1546 : 136 : istmt.privileges = ACL_NO_RIGHTS;
1547 : 136 : istmt.col_privs = NIL;
1548 : 136 : istmt.grantees = list_make1_oid(roleid);
1549 : 136 : istmt.grant_option = false;
1550 : 136 : istmt.grantor = NULL;
1551 : 136 : istmt.behavior = DROP_CASCADE;
1552 : :
1553 : 136 : ExecGrantStmt_oids(&istmt);
1554 : : }
1555 : 160 : }
1556 : :
1557 : :
1558 : : /*
1559 : : * expand_col_privileges
1560 : : *
1561 : : * OR the specified privilege(s) into per-column array entries for each
1562 : : * specified attribute. The per-column array is indexed starting at
1563 : : * FirstLowInvalidHeapAttributeNumber, up to relation's last attribute.
1564 : : */
1565 : : static void
1566 : 319 : expand_col_privileges(List *colnames, Oid table_oid,
1567 : : AclMode this_privileges,
1568 : : AclMode *col_privileges,
1569 : : int num_col_privileges)
1570 : : {
1571 : : ListCell *cell;
1572 : :
1573 [ + - + + : 2096 : foreach(cell, colnames)
+ + ]
1574 : : {
1575 : 1777 : char *colname = strVal(lfirst(cell));
1576 : : AttrNumber attnum;
1577 : :
1578 : 1777 : attnum = get_attnum(table_oid, colname);
1579 [ - + ]: 1777 : if (attnum == InvalidAttrNumber)
1580 [ # # ]: 0 : ereport(ERROR,
1581 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
1582 : : errmsg("column \"%s\" of relation \"%s\" does not exist",
1583 : : colname, get_rel_name(table_oid))));
1584 : 1777 : attnum -= FirstLowInvalidHeapAttributeNumber;
1585 [ + - - + ]: 1777 : if (attnum <= 0 || attnum >= num_col_privileges)
1586 [ # # ]: 0 : elog(ERROR, "column number out of range"); /* safety check */
1587 : 1777 : col_privileges[attnum] |= this_privileges;
1588 : : }
1589 : 319 : }
1590 : :
1591 : : /*
1592 : : * expand_all_col_privileges
1593 : : *
1594 : : * OR the specified privilege(s) into per-column array entries for each valid
1595 : : * attribute of a relation. The per-column array is indexed starting at
1596 : : * FirstLowInvalidHeapAttributeNumber, up to relation's last attribute.
1597 : : */
1598 : : static void
1599 : 3554 : expand_all_col_privileges(Oid table_oid, Form_pg_class classForm,
1600 : : AclMode this_privileges,
1601 : : AclMode *col_privileges,
1602 : : int num_col_privileges)
1603 : : {
1604 : : AttrNumber curr_att;
1605 : :
1606 : : Assert(classForm->relnatts - FirstLowInvalidHeapAttributeNumber < num_col_privileges);
1607 : 3554 : for (curr_att = FirstLowInvalidHeapAttributeNumber + 1;
1608 [ + + ]: 37710 : curr_att <= classForm->relnatts;
1609 : 34156 : curr_att++)
1610 : : {
1611 : : HeapTuple attTuple;
1612 : : bool isdropped;
1613 : :
1614 [ + + ]: 34156 : if (curr_att == InvalidAttrNumber)
1615 : 3554 : continue;
1616 : :
1617 : : /* Views don't have any system columns at all */
1618 [ + + + + ]: 30602 : if (classForm->relkind == RELKIND_VIEW && curr_att < 0)
1619 : 3756 : continue;
1620 : :
1621 : 26846 : attTuple = SearchSysCache2(ATTNUM,
1622 : : ObjectIdGetDatum(table_oid),
1623 : : Int16GetDatum(curr_att));
1624 [ - + ]: 26846 : if (!HeapTupleIsValid(attTuple))
1625 [ # # ]: 0 : elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1626 : : curr_att, table_oid);
1627 : :
1628 : 26846 : isdropped = ((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped;
1629 : :
1630 : 26846 : ReleaseSysCache(attTuple);
1631 : :
1632 : : /* ignore dropped columns */
1633 [ + + ]: 26846 : if (isdropped)
1634 : 4 : continue;
1635 : :
1636 : 26842 : col_privileges[curr_att - FirstLowInvalidHeapAttributeNumber] |= this_privileges;
1637 : : }
1638 : 3554 : }
1639 : :
1640 : : /*
1641 : : * This processes attributes, but expects to be called from
1642 : : * ExecGrant_Relation, not directly from ExecuteGrantStmt.
1643 : : */
1644 : : static void
1645 : 28591 : ExecGrant_Attribute(InternalGrant *istmt, Oid relOid, const char *relname,
1646 : : AttrNumber attnum, Oid ownerId, AclMode col_privileges,
1647 : : Relation attRelation, const Acl *old_rel_acl)
1648 : : {
1649 : : HeapTuple attr_tuple;
1650 : : Form_pg_attribute pg_attribute_tuple;
1651 : : Acl *old_acl;
1652 : : Acl *new_acl;
1653 : : Acl *merged_acl;
1654 : : Datum aclDatum;
1655 : : bool isNull;
1656 : : Oid grantorId;
1657 : : AclMode avail_goptions;
1658 : : bool need_update;
1659 : : HeapTuple newtuple;
1660 : 28591 : Datum values[Natts_pg_attribute] = {0};
1661 : 28591 : bool nulls[Natts_pg_attribute] = {0};
1662 : 28591 : bool replaces[Natts_pg_attribute] = {0};
1663 : : int noldmembers;
1664 : : int nnewmembers;
1665 : : Oid *oldmembers;
1666 : : Oid *newmembers;
1667 : :
1668 : 28591 : attr_tuple = SearchSysCache2(ATTNUM,
1669 : : ObjectIdGetDatum(relOid),
1670 : : Int16GetDatum(attnum));
1671 [ - + ]: 28591 : if (!HeapTupleIsValid(attr_tuple))
1672 [ # # ]: 0 : elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1673 : : attnum, relOid);
1674 : 28591 : pg_attribute_tuple = (Form_pg_attribute) GETSTRUCT(attr_tuple);
1675 : :
1676 : : /*
1677 : : * Get working copy of existing ACL. If there's no ACL, substitute the
1678 : : * proper default.
1679 : : */
1680 : 28591 : aclDatum = SysCacheGetAttr(ATTNUM, attr_tuple, Anum_pg_attribute_attacl,
1681 : : &isNull);
1682 [ + + ]: 28591 : if (isNull)
1683 : : {
1684 : 28343 : old_acl = acldefault(OBJECT_COLUMN, ownerId);
1685 : : /* There are no old member roles according to the catalogs */
1686 : 28343 : noldmembers = 0;
1687 : 28343 : oldmembers = NULL;
1688 : : }
1689 : : else
1690 : : {
1691 : 248 : old_acl = DatumGetAclPCopy(aclDatum);
1692 : : /* Get the roles mentioned in the existing ACL */
1693 : 248 : noldmembers = aclmembers(old_acl, &oldmembers);
1694 : : }
1695 : :
1696 : : /*
1697 : : * In select_best_grantor we should consider existing table-level ACL bits
1698 : : * as well as the per-column ACL. Build a new ACL that is their
1699 : : * concatenation. (This is a bit cheap and dirty compared to merging them
1700 : : * properly with no duplications, but it's all we need here.)
1701 : : */
1702 : 28591 : merged_acl = aclconcat(old_rel_acl, old_acl);
1703 : :
1704 : : /* Determine ID to do the grant as, and available grant options */
1705 : 28591 : select_best_grantor(istmt->grantor, col_privileges,
1706 : : merged_acl, ownerId,
1707 : : &grantorId, &avail_goptions);
1708 : :
1709 : 28591 : pfree(merged_acl);
1710 : :
1711 : : /*
1712 : : * Restrict the privileges to what we can actually grant, and emit the
1713 : : * standards-mandated warning and error messages. Note: we don't track
1714 : : * whether the user actually used the ALL PRIVILEGES(columns) syntax for
1715 : : * each column; we just approximate it by whether all the possible
1716 : : * privileges are specified now. Since the all_privs flag only determines
1717 : : * whether a warning is issued, this seems close enough.
1718 : : */
1719 : : col_privileges =
1720 : 28591 : restrict_and_check_grant(istmt->is_grant, avail_goptions,
1721 : : (col_privileges == ACL_ALL_RIGHTS_COLUMN),
1722 : : col_privileges,
1723 : : relOid, grantorId, OBJECT_COLUMN,
1724 : : relname, attnum,
1725 : 28591 : NameStr(pg_attribute_tuple->attname));
1726 : :
1727 : : /*
1728 : : * Generate new ACL.
1729 : : */
1730 : 28591 : new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
1731 : 28591 : istmt->grant_option,
1732 : : istmt->behavior, istmt->grantees,
1733 : : col_privileges, grantorId,
1734 : : ownerId);
1735 : :
1736 : : /*
1737 : : * We need the members of both old and new ACLs so we can correct the
1738 : : * shared dependency information.
1739 : : */
1740 : 28591 : nnewmembers = aclmembers(new_acl, &newmembers);
1741 : :
1742 : : /* finished building new ACL value, now insert it */
1743 : :
1744 : : /*
1745 : : * If the updated ACL is empty, we can set attacl to null, and maybe even
1746 : : * avoid an update of the pg_attribute row. This is worth testing because
1747 : : * we'll come through here multiple times for any relation-level REVOKE,
1748 : : * even if there were never any column GRANTs. Note we are assuming that
1749 : : * the "default" ACL state for columns is empty.
1750 : : */
1751 [ + + ]: 28591 : if (ACL_NUM(new_acl) > 0)
1752 : : {
1753 : 1790 : values[Anum_pg_attribute_attacl - 1] = PointerGetDatum(new_acl);
1754 : 1790 : need_update = true;
1755 : : }
1756 : : else
1757 : : {
1758 : 26801 : nulls[Anum_pg_attribute_attacl - 1] = true;
1759 : 26801 : need_update = !isNull;
1760 : : }
1761 : 28591 : replaces[Anum_pg_attribute_attacl - 1] = true;
1762 : :
1763 [ + + ]: 28591 : if (need_update)
1764 : : {
1765 : 1856 : newtuple = heap_modify_tuple(attr_tuple, RelationGetDescr(attRelation),
1766 : : values, nulls, replaces);
1767 : :
1768 : 1856 : CatalogTupleUpdate(attRelation, &newtuple->t_self, newtuple);
1769 : :
1770 : : /* Update initial privileges for extensions */
1771 : 1856 : recordExtensionInitPriv(relOid, RelationRelationId, attnum,
1772 [ + + ]: 1856 : ACL_NUM(new_acl) > 0 ? new_acl : NULL);
1773 : :
1774 : : /* Update the shared dependency ACL info */
1775 : 1856 : updateAclDependencies(RelationRelationId, relOid, attnum,
1776 : : ownerId,
1777 : : noldmembers, oldmembers,
1778 : : nnewmembers, newmembers);
1779 : : }
1780 : :
1781 : 28591 : pfree(new_acl);
1782 : :
1783 : 28591 : ReleaseSysCache(attr_tuple);
1784 : 28591 : }
1785 : :
1786 : : /*
1787 : : * This processes all pg_class entries including sequences and property graphs.
1788 : : */
1789 : : static void
1790 : 11355 : ExecGrant_Relation(InternalGrant *istmt)
1791 : : {
1792 : : Relation relation;
1793 : : Relation attRelation;
1794 : : ListCell *cell;
1795 : :
1796 : 11355 : relation = table_open(RelationRelationId, RowExclusiveLock);
1797 : 11355 : attRelation = table_open(AttributeRelationId, RowExclusiveLock);
1798 : :
1799 [ + - + + : 22738 : foreach(cell, istmt->objects)
+ + ]
1800 : : {
1801 : 11402 : Oid relOid = lfirst_oid(cell);
1802 : : Datum aclDatum;
1803 : : Form_pg_class pg_class_tuple;
1804 : : bool isNull;
1805 : : AclMode this_privileges;
1806 : : AclMode *col_privileges;
1807 : : int num_col_privileges;
1808 : : bool have_col_privileges;
1809 : : Acl *old_acl;
1810 : : Acl *old_rel_acl;
1811 : : int noldmembers;
1812 : : Oid *oldmembers;
1813 : : Oid ownerId;
1814 : : HeapTuple tuple;
1815 : : ListCell *cell_colprivs;
1816 : :
1817 : 11402 : tuple = SearchSysCacheLocked1(RELOID, ObjectIdGetDatum(relOid));
1818 [ + + ]: 11402 : if (!HeapTupleIsValid(tuple))
1819 [ + - ]: 1 : elog(ERROR, "cache lookup failed for relation %u", relOid);
1820 : 11401 : pg_class_tuple = (Form_pg_class) GETSTRUCT(tuple);
1821 : :
1822 : : /* Not sensible to grant on an index */
1823 [ + - ]: 11401 : if (pg_class_tuple->relkind == RELKIND_INDEX ||
1824 [ - + ]: 11401 : pg_class_tuple->relkind == RELKIND_PARTITIONED_INDEX)
1825 [ # # ]: 0 : ereport(ERROR,
1826 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1827 : : errmsg("\"%s\" is an index",
1828 : : NameStr(pg_class_tuple->relname))));
1829 : :
1830 : : /* Composite types aren't tables either */
1831 [ - + ]: 11401 : if (pg_class_tuple->relkind == RELKIND_COMPOSITE_TYPE)
1832 [ # # ]: 0 : ereport(ERROR,
1833 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1834 : : errmsg("\"%s\" is a composite type",
1835 : : NameStr(pg_class_tuple->relname))));
1836 : :
1837 : : /* Used GRANT SEQUENCE on a non-sequence? */
1838 [ + + ]: 11401 : if (istmt->objtype == OBJECT_SEQUENCE &&
1839 [ - + ]: 17 : pg_class_tuple->relkind != RELKIND_SEQUENCE)
1840 [ # # ]: 0 : ereport(ERROR,
1841 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1842 : : errmsg("\"%s\" is not a sequence",
1843 : : NameStr(pg_class_tuple->relname))));
1844 : :
1845 [ + + ]: 11401 : if (istmt->objtype == OBJECT_PROPGRAPH &&
1846 [ - + ]: 26 : pg_class_tuple->relkind != RELKIND_PROPGRAPH)
1847 [ # # ]: 0 : ereport(ERROR,
1848 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1849 : : errmsg("\"%s\" is not a property graph",
1850 : : NameStr(pg_class_tuple->relname))));
1851 : :
1852 : : /* Adjust the default permissions based on object type */
1853 [ + + + - ]: 11401 : if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
1854 : : {
1855 [ + + ]: 1276 : if (pg_class_tuple->relkind == RELKIND_SEQUENCE)
1856 : 52 : this_privileges = ACL_ALL_RIGHTS_SEQUENCE;
1857 [ + + ]: 1224 : else if (pg_class_tuple->relkind == RELKIND_PROPGRAPH)
1858 : 1 : this_privileges = ACL_ALL_RIGHTS_PROPGRAPH;
1859 : : else
1860 : 1223 : this_privileges = ACL_ALL_RIGHTS_RELATION;
1861 : : }
1862 : : else
1863 : 10125 : this_privileges = istmt->privileges;
1864 : :
1865 : : /*
1866 : : * The GRANT TABLE syntax can be used for sequences and non-sequences,
1867 : : * so we have to look at the relkind to determine the supported
1868 : : * permissions. The OR of table and sequence permissions were already
1869 : : * checked.
1870 : : */
1871 [ + + ]: 11401 : if (istmt->objtype == OBJECT_TABLE)
1872 : : {
1873 [ + + ]: 11358 : if (pg_class_tuple->relkind == RELKIND_SEQUENCE)
1874 : : {
1875 : : /*
1876 : : * For backward compatibility, just throw a warning for
1877 : : * invalid sequence permissions when using the non-sequence
1878 : : * GRANT syntax.
1879 : : */
1880 [ - + ]: 98 : if (this_privileges & ~((AclMode) ACL_ALL_RIGHTS_SEQUENCE))
1881 : : {
1882 : : /*
1883 : : * Mention the object name because the user needs to know
1884 : : * which operations succeeded. This is required because
1885 : : * WARNING allows the command to continue.
1886 : : */
1887 [ # # ]: 0 : ereport(WARNING,
1888 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1889 : : errmsg("sequence \"%s\" only supports USAGE, SELECT, and UPDATE privileges",
1890 : : NameStr(pg_class_tuple->relname))));
1891 : 0 : this_privileges &= (AclMode) ACL_ALL_RIGHTS_SEQUENCE;
1892 : : }
1893 : : }
1894 [ + + ]: 11260 : else if (pg_class_tuple->relkind == RELKIND_PROPGRAPH)
1895 : : {
1896 : : /*
1897 : : * Do not allow GRANT ... TABLE on property graph. We allowed
1898 : : * it on sequences for backward compatibility but there is no
1899 : : * reason to continue that further.
1900 : : */
1901 [ + - ]: 4 : ereport(ERROR,
1902 : : errcode(ERRCODE_WRONG_OBJECT_TYPE),
1903 : : errmsg("\"%s\" is a property graph", NameStr(pg_class_tuple->relname)),
1904 : : errhint("Use GRANT ... ON PROPERTY GRAPH instead."));
1905 : : }
1906 : : else
1907 : : {
1908 [ - + ]: 11256 : if (this_privileges & ~((AclMode) ACL_ALL_RIGHTS_RELATION))
1909 : : {
1910 : : /*
1911 : : * USAGE is the only permission supported by sequences but
1912 : : * not by non-sequences. Don't mention the object name
1913 : : * because we didn't in the combined TABLE | SEQUENCE
1914 : : * check.
1915 : : */
1916 [ # # ]: 0 : ereport(ERROR,
1917 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1918 : : errmsg("invalid privilege type %s for table",
1919 : : "USAGE")));
1920 : : }
1921 : : }
1922 : : }
1923 : :
1924 : : /*
1925 : : * Set up array in which we'll accumulate any column privilege bits
1926 : : * that need modification. The array is indexed such that entry [0]
1927 : : * corresponds to FirstLowInvalidHeapAttributeNumber.
1928 : : */
1929 : 11397 : num_col_privileges = pg_class_tuple->relnatts - FirstLowInvalidHeapAttributeNumber + 1;
1930 : 11397 : col_privileges = (AclMode *) palloc0(num_col_privileges * sizeof(AclMode));
1931 : 11397 : have_col_privileges = false;
1932 : :
1933 : : /*
1934 : : * If we are revoking relation privileges that are also column
1935 : : * privileges, we must implicitly revoke them from each column too,
1936 : : * per SQL spec. (We don't need to implicitly add column privileges
1937 : : * during GRANT because the permissions-checking code always checks
1938 : : * both relation and per-column privileges.)
1939 : : */
1940 [ + + ]: 11397 : if (!istmt->is_grant &&
1941 [ + + ]: 3603 : (this_privileges & ACL_ALL_RIGHTS_COLUMN) != 0)
1942 : : {
1943 : 3554 : expand_all_col_privileges(relOid, pg_class_tuple,
1944 : : this_privileges & ACL_ALL_RIGHTS_COLUMN,
1945 : : col_privileges,
1946 : : num_col_privileges);
1947 : 3554 : have_col_privileges = true;
1948 : : }
1949 : :
1950 : : /*
1951 : : * Get owner ID and working copy of existing ACL. If there's no ACL,
1952 : : * substitute the proper default.
1953 : : */
1954 : 11397 : ownerId = pg_class_tuple->relowner;
1955 : 11397 : aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
1956 : : &isNull);
1957 [ + + ]: 11397 : if (isNull)
1958 : : {
1959 [ + + + ]: 5836 : switch (pg_class_tuple->relkind)
1960 : : {
1961 : 62 : case RELKIND_SEQUENCE:
1962 : 62 : old_acl = acldefault(OBJECT_SEQUENCE, ownerId);
1963 : 62 : break;
1964 : 22 : case RELKIND_PROPGRAPH:
1965 : 22 : old_acl = acldefault(OBJECT_PROPGRAPH, ownerId);
1966 : 22 : break;
1967 : 5752 : default:
1968 : 5752 : old_acl = acldefault(OBJECT_TABLE, ownerId);
1969 : 5752 : break;
1970 : : }
1971 : : /* There are no old member roles according to the catalogs */
1972 : 5836 : noldmembers = 0;
1973 : 5836 : oldmembers = NULL;
1974 : : }
1975 : : else
1976 : : {
1977 : 5561 : old_acl = DatumGetAclPCopy(aclDatum);
1978 : : /* Get the roles mentioned in the existing ACL */
1979 : 5561 : noldmembers = aclmembers(old_acl, &oldmembers);
1980 : : }
1981 : :
1982 : : /* Need an extra copy of original rel ACL for column handling */
1983 : 11397 : old_rel_acl = aclcopy(old_acl);
1984 : :
1985 : : /*
1986 : : * Handle relation-level privileges, if any were specified
1987 : : */
1988 [ + + ]: 11397 : if (this_privileges != ACL_NO_RIGHTS)
1989 : : {
1990 : : AclMode avail_goptions;
1991 : : Acl *new_acl;
1992 : : Oid grantorId;
1993 : : HeapTuple newtuple;
1994 : 11090 : Datum values[Natts_pg_class] = {0};
1995 : 11090 : bool nulls[Natts_pg_class] = {0};
1996 : 11090 : bool replaces[Natts_pg_class] = {0};
1997 : : int nnewmembers;
1998 : : Oid *newmembers;
1999 : : ObjectType objtype;
2000 : :
2001 : : /* Determine ID to do the grant as, and available grant options */
2002 : 11090 : select_best_grantor(istmt->grantor, this_privileges,
2003 : : old_acl, ownerId,
2004 : : &grantorId, &avail_goptions);
2005 : :
2006 [ + + + ]: 11086 : switch (pg_class_tuple->relkind)
2007 : : {
2008 : 115 : case RELKIND_SEQUENCE:
2009 : 115 : objtype = OBJECT_SEQUENCE;
2010 : 115 : break;
2011 : 26 : case RELKIND_PROPGRAPH:
2012 : 26 : objtype = OBJECT_PROPGRAPH;
2013 : 26 : break;
2014 : 10945 : default:
2015 : 10945 : objtype = OBJECT_TABLE;
2016 : 10945 : break;
2017 : : }
2018 : :
2019 : : /*
2020 : : * Restrict the privileges to what we can actually grant, and emit
2021 : : * the standards-mandated warning and error messages.
2022 : : */
2023 : : this_privileges =
2024 : 11086 : restrict_and_check_grant(istmt->is_grant, avail_goptions,
2025 : 11086 : istmt->all_privs, this_privileges,
2026 : : relOid, grantorId, objtype,
2027 : 11086 : NameStr(pg_class_tuple->relname),
2028 : : 0, NULL);
2029 : :
2030 : : /*
2031 : : * Generate new ACL.
2032 : : */
2033 : 11082 : new_acl = merge_acl_with_grant(old_acl,
2034 : 11082 : istmt->is_grant,
2035 : 11082 : istmt->grant_option,
2036 : : istmt->behavior,
2037 : : istmt->grantees,
2038 : : this_privileges,
2039 : : grantorId,
2040 : : ownerId);
2041 : :
2042 : : /*
2043 : : * We need the members of both old and new ACLs so we can correct
2044 : : * the shared dependency information.
2045 : : */
2046 : 11078 : nnewmembers = aclmembers(new_acl, &newmembers);
2047 : :
2048 : : /* finished building new ACL value, now insert it */
2049 : 11078 : replaces[Anum_pg_class_relacl - 1] = true;
2050 : 11078 : values[Anum_pg_class_relacl - 1] = PointerGetDatum(new_acl);
2051 : :
2052 : 11078 : newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation),
2053 : : values, nulls, replaces);
2054 : :
2055 : 11078 : CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2056 : 11076 : UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
2057 : :
2058 : : /* Update initial privileges for extensions */
2059 : 11076 : recordExtensionInitPriv(relOid, RelationRelationId, 0, new_acl);
2060 : :
2061 : : /* Update the shared dependency ACL info */
2062 : 11076 : updateAclDependencies(RelationRelationId, relOid, 0,
2063 : : ownerId,
2064 : : noldmembers, oldmembers,
2065 : : nnewmembers, newmembers);
2066 : :
2067 : 11076 : pfree(new_acl);
2068 : : }
2069 : : else
2070 : 307 : UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
2071 : :
2072 : : /*
2073 : : * Handle column-level privileges, if any were specified or implied.
2074 : : * We first expand the user-specified column privileges into the
2075 : : * array, and then iterate over all nonempty array entries.
2076 : : */
2077 [ + + + + : 11702 : foreach(cell_colprivs, istmt->col_privs)
+ + ]
2078 : : {
2079 : 319 : AccessPriv *col_privs = (AccessPriv *) lfirst(cell_colprivs);
2080 : :
2081 [ + + ]: 319 : if (col_privs->priv_name == NULL)
2082 : 12 : this_privileges = ACL_ALL_RIGHTS_COLUMN;
2083 : : else
2084 : 307 : this_privileges = string_to_privilege(col_privs->priv_name);
2085 : :
2086 [ - + ]: 319 : if (this_privileges & ~((AclMode) ACL_ALL_RIGHTS_COLUMN))
2087 [ # # ]: 0 : ereport(ERROR,
2088 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
2089 : : errmsg("invalid privilege type %s for column",
2090 : : privilege_to_string(this_privileges))));
2091 : :
2092 [ - + ]: 319 : if (pg_class_tuple->relkind == RELKIND_SEQUENCE &&
2093 [ # # ]: 0 : this_privileges & ~((AclMode) ACL_SELECT))
2094 : : {
2095 : : /*
2096 : : * The only column privilege allowed on sequences is SELECT.
2097 : : * This is a warning not error because we do it that way for
2098 : : * relation-level privileges.
2099 : : */
2100 [ # # ]: 0 : ereport(WARNING,
2101 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
2102 : : errmsg("sequence \"%s\" only supports SELECT column privileges",
2103 : : NameStr(pg_class_tuple->relname))));
2104 : :
2105 : 0 : this_privileges &= (AclMode) ACL_SELECT;
2106 : : }
2107 : :
2108 : 319 : expand_col_privileges(col_privs->cols, relOid,
2109 : : this_privileges,
2110 : : col_privileges,
2111 : : num_col_privileges);
2112 : 319 : have_col_privileges = true;
2113 : : }
2114 : :
2115 [ + + ]: 11383 : if (have_col_privileges)
2116 : : {
2117 : : AttrNumber i;
2118 : :
2119 [ + + ]: 46389 : for (i = 0; i < num_col_privileges; i++)
2120 : : {
2121 [ + + ]: 42528 : if (col_privileges[i] == ACL_NO_RIGHTS)
2122 : 13937 : continue;
2123 : 28591 : ExecGrant_Attribute(istmt,
2124 : : relOid,
2125 : 28591 : NameStr(pg_class_tuple->relname),
2126 : 28591 : i + FirstLowInvalidHeapAttributeNumber,
2127 : : ownerId,
2128 : 28591 : col_privileges[i],
2129 : : attRelation,
2130 : : old_rel_acl);
2131 : : }
2132 : : }
2133 : :
2134 : 11383 : pfree(old_rel_acl);
2135 : 11383 : pfree(col_privileges);
2136 : :
2137 : 11383 : ReleaseSysCache(tuple);
2138 : :
2139 : : /* prevent error when processing duplicate objects */
2140 : 11383 : CommandCounterIncrement();
2141 : : }
2142 : :
2143 : 11336 : table_close(attRelation, RowExclusiveLock);
2144 : 11336 : table_close(relation, RowExclusiveLock);
2145 : 11336 : }
2146 : :
2147 : : static void
2148 : 1453 : ExecGrant_common(InternalGrant *istmt, Oid classid, AclMode default_privs,
2149 : : void (*object_check) (InternalGrant *istmt, HeapTuple tuple))
2150 : : {
2151 : : SysCacheIdentifier cacheid;
2152 : : Relation relation;
2153 : : ListCell *cell;
2154 : :
2155 [ + + + - ]: 1453 : if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2156 : 499 : istmt->privileges = default_privs;
2157 : :
2158 : 1453 : cacheid = get_object_catcache_oid(classid);
2159 : :
2160 : 1453 : relation = table_open(classid, RowExclusiveLock);
2161 : :
2162 [ + - + + : 2969 : foreach(cell, istmt->objects)
+ + ]
2163 : : {
2164 : 1552 : Oid objectid = lfirst_oid(cell);
2165 : : Datum aclDatum;
2166 : : Datum nameDatum;
2167 : : bool isNull;
2168 : : AclMode avail_goptions;
2169 : : AclMode this_privileges;
2170 : : Acl *old_acl;
2171 : : Acl *new_acl;
2172 : : Oid grantorId;
2173 : : Oid ownerId;
2174 : : HeapTuple tuple;
2175 : : HeapTuple newtuple;
2176 : 1552 : Datum *values = palloc0_array(Datum, RelationGetDescr(relation)->natts);
2177 : 1552 : bool *nulls = palloc0_array(bool, RelationGetDescr(relation)->natts);
2178 : 1552 : bool *replaces = palloc0_array(bool, RelationGetDescr(relation)->natts);
2179 : : int noldmembers;
2180 : : int nnewmembers;
2181 : : Oid *oldmembers;
2182 : : Oid *newmembers;
2183 : :
2184 : 1552 : tuple = SearchSysCacheLocked1(cacheid, ObjectIdGetDatum(objectid));
2185 [ - + ]: 1552 : if (!HeapTupleIsValid(tuple))
2186 [ # # ]: 0 : elog(ERROR, "cache lookup failed for %s %u", get_object_class_descr(classid), objectid);
2187 : :
2188 : : /*
2189 : : * Additional object-type-specific checks
2190 : : */
2191 [ + + ]: 1552 : if (object_check)
2192 : 131 : object_check(istmt, tuple);
2193 : :
2194 : : /*
2195 : : * Get owner ID and working copy of existing ACL. If there's no ACL,
2196 : : * substitute the proper default.
2197 : : */
2198 : 1540 : ownerId = DatumGetObjectId(SysCacheGetAttrNotNull(cacheid,
2199 : : tuple,
2200 : 1540 : get_object_attnum_owner(classid)));
2201 : 1540 : aclDatum = SysCacheGetAttr(cacheid,
2202 : : tuple,
2203 : 1540 : get_object_attnum_acl(classid),
2204 : : &isNull);
2205 [ + + ]: 1540 : if (isNull)
2206 : : {
2207 : 938 : old_acl = acldefault(get_object_type(classid, objectid), ownerId);
2208 : : /* There are no old member roles according to the catalogs */
2209 : 938 : noldmembers = 0;
2210 : 938 : oldmembers = NULL;
2211 : : }
2212 : : else
2213 : : {
2214 : 602 : old_acl = DatumGetAclPCopy(aclDatum);
2215 : : /* Get the roles mentioned in the existing ACL */
2216 : 602 : noldmembers = aclmembers(old_acl, &oldmembers);
2217 : : }
2218 : :
2219 : : /* Determine ID to do the grant as, and available grant options */
2220 : 1540 : select_best_grantor(istmt->grantor, istmt->privileges,
2221 : : old_acl, ownerId,
2222 : : &grantorId, &avail_goptions);
2223 : :
2224 : 1540 : nameDatum = SysCacheGetAttrNotNull(cacheid, tuple,
2225 : 1540 : get_object_attnum_name(classid));
2226 : :
2227 : : /*
2228 : : * Restrict the privileges to what we can actually grant, and emit the
2229 : : * standards-mandated warning and error messages.
2230 : : */
2231 : : this_privileges =
2232 : 3080 : restrict_and_check_grant(istmt->is_grant, avail_goptions,
2233 : 1540 : istmt->all_privs, istmt->privileges,
2234 : : objectid, grantorId, get_object_type(classid, objectid),
2235 : 1540 : NameStr(*DatumGetName(nameDatum)),
2236 : : 0, NULL);
2237 : :
2238 : : /*
2239 : : * Generate new ACL.
2240 : : */
2241 : 1520 : new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2242 : 1520 : istmt->grant_option, istmt->behavior,
2243 : : istmt->grantees, this_privileges,
2244 : : grantorId, ownerId);
2245 : :
2246 : : /*
2247 : : * We need the members of both old and new ACLs so we can correct the
2248 : : * shared dependency information.
2249 : : */
2250 : 1516 : nnewmembers = aclmembers(new_acl, &newmembers);
2251 : :
2252 : : /* finished building new ACL value, now insert it */
2253 : 1516 : replaces[get_object_attnum_acl(classid) - 1] = true;
2254 : 1516 : values[get_object_attnum_acl(classid) - 1] = PointerGetDatum(new_acl);
2255 : :
2256 : 1516 : newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2257 : : nulls, replaces);
2258 : :
2259 : 1516 : CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2260 : 1516 : UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
2261 : :
2262 : : /* Update initial privileges for extensions */
2263 : 1516 : recordExtensionInitPriv(objectid, classid, 0, new_acl);
2264 : :
2265 : : /* Update the shared dependency ACL info */
2266 : 1516 : updateAclDependencies(classid,
2267 : : objectid, 0,
2268 : : ownerId,
2269 : : noldmembers, oldmembers,
2270 : : nnewmembers, newmembers);
2271 : :
2272 : 1516 : ReleaseSysCache(tuple);
2273 : :
2274 : 1516 : pfree(new_acl);
2275 : :
2276 : : /* prevent error when processing duplicate objects */
2277 : 1516 : CommandCounterIncrement();
2278 : : }
2279 : :
2280 : 1417 : table_close(relation, RowExclusiveLock);
2281 : 1417 : }
2282 : :
2283 : : static void
2284 : 27 : ExecGrant_Language_check(InternalGrant *istmt, HeapTuple tuple)
2285 : : {
2286 : : Form_pg_language pg_language_tuple;
2287 : :
2288 : 27 : pg_language_tuple = (Form_pg_language) GETSTRUCT(tuple);
2289 : :
2290 [ + + ]: 27 : if (!pg_language_tuple->lanpltrusted)
2291 [ + - ]: 4 : ereport(ERROR,
2292 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2293 : : errmsg("language \"%s\" is not trusted",
2294 : : NameStr(pg_language_tuple->lanname)),
2295 : : errdetail("GRANT and REVOKE are not allowed on untrusted languages, "
2296 : : "because only superusers can use untrusted languages.")));
2297 : 23 : }
2298 : :
2299 : : static void
2300 : 58 : ExecGrant_Largeobject(InternalGrant *istmt)
2301 : : {
2302 : : Relation relation;
2303 : : ListCell *cell;
2304 : :
2305 [ + + + - ]: 58 : if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2306 : 29 : istmt->privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
2307 : :
2308 : 58 : relation = table_open(LargeObjectMetadataRelationId,
2309 : : RowExclusiveLock);
2310 : :
2311 [ + - + + : 120 : foreach(cell, istmt->objects)
+ + ]
2312 : : {
2313 : 62 : Oid loid = lfirst_oid(cell);
2314 : : Form_pg_largeobject_metadata form_lo_meta;
2315 : : char loname[NAMEDATALEN];
2316 : : Datum aclDatum;
2317 : : bool isNull;
2318 : : AclMode avail_goptions;
2319 : : AclMode this_privileges;
2320 : : Acl *old_acl;
2321 : : Acl *new_acl;
2322 : : Oid grantorId;
2323 : : Oid ownerId;
2324 : : HeapTuple newtuple;
2325 : 62 : Datum values[Natts_pg_largeobject_metadata] = {0};
2326 : 62 : bool nulls[Natts_pg_largeobject_metadata] = {0};
2327 : 62 : bool replaces[Natts_pg_largeobject_metadata] = {0};
2328 : : int noldmembers;
2329 : : int nnewmembers;
2330 : : Oid *oldmembers;
2331 : : Oid *newmembers;
2332 : : ScanKeyData entry[1];
2333 : : SysScanDesc scan;
2334 : : HeapTuple tuple;
2335 : :
2336 : : /* There's no syscache for pg_largeobject_metadata */
2337 : 62 : ScanKeyInit(&entry[0],
2338 : : Anum_pg_largeobject_metadata_oid,
2339 : : BTEqualStrategyNumber, F_OIDEQ,
2340 : : ObjectIdGetDatum(loid));
2341 : :
2342 : 62 : scan = systable_beginscan(relation,
2343 : : LargeObjectMetadataOidIndexId, true,
2344 : : NULL, 1, entry);
2345 : :
2346 : 62 : tuple = systable_getnext(scan);
2347 [ - + ]: 62 : if (!HeapTupleIsValid(tuple))
2348 [ # # ]: 0 : elog(ERROR, "could not find tuple for large object %u", loid);
2349 : :
2350 : 62 : form_lo_meta = (Form_pg_largeobject_metadata) GETSTRUCT(tuple);
2351 : :
2352 : : /*
2353 : : * Get owner ID and working copy of existing ACL. If there's no ACL,
2354 : : * substitute the proper default.
2355 : : */
2356 : 62 : ownerId = form_lo_meta->lomowner;
2357 : 62 : aclDatum = heap_getattr(tuple,
2358 : : Anum_pg_largeobject_metadata_lomacl,
2359 : : RelationGetDescr(relation), &isNull);
2360 [ + + ]: 62 : if (isNull)
2361 : : {
2362 : 38 : old_acl = acldefault(OBJECT_LARGEOBJECT, ownerId);
2363 : : /* There are no old member roles according to the catalogs */
2364 : 38 : noldmembers = 0;
2365 : 38 : oldmembers = NULL;
2366 : : }
2367 : : else
2368 : : {
2369 : 24 : old_acl = DatumGetAclPCopy(aclDatum);
2370 : : /* Get the roles mentioned in the existing ACL */
2371 : 24 : noldmembers = aclmembers(old_acl, &oldmembers);
2372 : : }
2373 : :
2374 : : /* Determine ID to do the grant as, and available grant options */
2375 : 62 : select_best_grantor(istmt->grantor, istmt->privileges,
2376 : : old_acl, ownerId,
2377 : : &grantorId, &avail_goptions);
2378 : :
2379 : : /*
2380 : : * Restrict the privileges to what we can actually grant, and emit the
2381 : : * standards-mandated warning and error messages.
2382 : : */
2383 : 62 : snprintf(loname, sizeof(loname), "large object %u", loid);
2384 : : this_privileges =
2385 : 62 : restrict_and_check_grant(istmt->is_grant, avail_goptions,
2386 : 62 : istmt->all_privs, istmt->privileges,
2387 : : loid, grantorId, OBJECT_LARGEOBJECT,
2388 : : loname, 0, NULL);
2389 : :
2390 : : /*
2391 : : * Generate new ACL.
2392 : : */
2393 : 62 : new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2394 : 62 : istmt->grant_option, istmt->behavior,
2395 : : istmt->grantees, this_privileges,
2396 : : grantorId, ownerId);
2397 : :
2398 : : /*
2399 : : * We need the members of both old and new ACLs so we can correct the
2400 : : * shared dependency information.
2401 : : */
2402 : 62 : nnewmembers = aclmembers(new_acl, &newmembers);
2403 : :
2404 : : /* finished building new ACL value, now insert it */
2405 : 62 : replaces[Anum_pg_largeobject_metadata_lomacl - 1] = true;
2406 : : values[Anum_pg_largeobject_metadata_lomacl - 1]
2407 : 62 : = PointerGetDatum(new_acl);
2408 : :
2409 : 62 : newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation),
2410 : : values, nulls, replaces);
2411 : :
2412 : 62 : CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2413 : :
2414 : : /* Update initial privileges for extensions */
2415 : 62 : recordExtensionInitPriv(loid, LargeObjectRelationId, 0, new_acl);
2416 : :
2417 : : /* Update the shared dependency ACL info */
2418 : 62 : updateAclDependencies(LargeObjectRelationId,
2419 : : form_lo_meta->oid, 0,
2420 : : ownerId,
2421 : : noldmembers, oldmembers,
2422 : : nnewmembers, newmembers);
2423 : :
2424 : 62 : systable_endscan(scan);
2425 : :
2426 : 62 : pfree(new_acl);
2427 : :
2428 : : /* prevent error when processing duplicate objects */
2429 : 62 : CommandCounterIncrement();
2430 : : }
2431 : :
2432 : 58 : table_close(relation, RowExclusiveLock);
2433 : 58 : }
2434 : :
2435 : : static void
2436 : 104 : ExecGrant_Type_check(InternalGrant *istmt, HeapTuple tuple)
2437 : : {
2438 : : Form_pg_type pg_type_tuple;
2439 : :
2440 : 104 : pg_type_tuple = (Form_pg_type) GETSTRUCT(tuple);
2441 : :
2442 : : /* Disallow GRANT on dependent types */
2443 [ + + + - ]: 104 : if (IsTrueArrayType(pg_type_tuple))
2444 [ + - ]: 4 : ereport(ERROR,
2445 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
2446 : : errmsg("cannot set privileges of array types"),
2447 : : errhint("Set the privileges of the element type instead.")));
2448 [ + + ]: 100 : if (pg_type_tuple->typtype == TYPTYPE_MULTIRANGE)
2449 [ + - ]: 4 : ereport(ERROR,
2450 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
2451 : : errmsg("cannot set privileges of multirange types"),
2452 : : errhint("Set the privileges of the range type instead.")));
2453 : 96 : }
2454 : :
2455 : : static void
2456 : 49 : ExecGrant_Parameter(InternalGrant *istmt)
2457 : : {
2458 : : Relation relation;
2459 : : ListCell *cell;
2460 : :
2461 [ + + + - ]: 49 : if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2462 : 21 : istmt->privileges = ACL_ALL_RIGHTS_PARAMETER_ACL;
2463 : :
2464 : 49 : relation = table_open(ParameterAclRelationId, RowExclusiveLock);
2465 : :
2466 [ + + + + : 117 : foreach(cell, istmt->objects)
+ + ]
2467 : : {
2468 : 68 : Oid parameterId = lfirst_oid(cell);
2469 : : Datum nameDatum;
2470 : : const char *parname;
2471 : : Datum aclDatum;
2472 : : bool isNull;
2473 : : AclMode avail_goptions;
2474 : : AclMode this_privileges;
2475 : : Acl *old_acl;
2476 : : Acl *new_acl;
2477 : : Oid grantorId;
2478 : : Oid ownerId;
2479 : : HeapTuple tuple;
2480 : : int noldmembers;
2481 : : int nnewmembers;
2482 : : Oid *oldmembers;
2483 : : Oid *newmembers;
2484 : :
2485 : 68 : tuple = SearchSysCache1(PARAMETERACLOID, ObjectIdGetDatum(parameterId));
2486 [ - + ]: 68 : if (!HeapTupleIsValid(tuple))
2487 [ # # ]: 0 : elog(ERROR, "cache lookup failed for parameter ACL %u",
2488 : : parameterId);
2489 : :
2490 : : /* We'll need the GUC's name */
2491 : 68 : nameDatum = SysCacheGetAttrNotNull(PARAMETERACLOID, tuple,
2492 : : Anum_pg_parameter_acl_parname);
2493 : 68 : parname = TextDatumGetCString(nameDatum);
2494 : :
2495 : : /* Treat all parameters as belonging to the bootstrap superuser. */
2496 : 68 : ownerId = BOOTSTRAP_SUPERUSERID;
2497 : :
2498 : : /*
2499 : : * Get working copy of existing ACL. If there's no ACL, substitute the
2500 : : * proper default.
2501 : : */
2502 : 68 : aclDatum = SysCacheGetAttr(PARAMETERACLOID, tuple,
2503 : : Anum_pg_parameter_acl_paracl,
2504 : : &isNull);
2505 : :
2506 [ + + ]: 68 : if (isNull)
2507 : : {
2508 : 33 : old_acl = acldefault(istmt->objtype, ownerId);
2509 : : /* There are no old member roles according to the catalogs */
2510 : 33 : noldmembers = 0;
2511 : 33 : oldmembers = NULL;
2512 : : }
2513 : : else
2514 : : {
2515 : 35 : old_acl = DatumGetAclPCopy(aclDatum);
2516 : : /* Get the roles mentioned in the existing ACL */
2517 : 35 : noldmembers = aclmembers(old_acl, &oldmembers);
2518 : : }
2519 : :
2520 : : /* Determine ID to do the grant as, and available grant options */
2521 : 68 : select_best_grantor(istmt->grantor, istmt->privileges,
2522 : : old_acl, ownerId,
2523 : : &grantorId, &avail_goptions);
2524 : :
2525 : : /*
2526 : : * Restrict the privileges to what we can actually grant, and emit the
2527 : : * standards-mandated warning and error messages.
2528 : : */
2529 : : this_privileges =
2530 : 68 : restrict_and_check_grant(istmt->is_grant, avail_goptions,
2531 : 68 : istmt->all_privs, istmt->privileges,
2532 : : parameterId, grantorId,
2533 : : OBJECT_PARAMETER_ACL,
2534 : : parname,
2535 : : 0, NULL);
2536 : :
2537 : : /*
2538 : : * Generate new ACL.
2539 : : */
2540 : 68 : new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2541 : 68 : istmt->grant_option, istmt->behavior,
2542 : : istmt->grantees, this_privileges,
2543 : : grantorId, ownerId);
2544 : :
2545 : : /*
2546 : : * We need the members of both old and new ACLs so we can correct the
2547 : : * shared dependency information.
2548 : : */
2549 : 68 : nnewmembers = aclmembers(new_acl, &newmembers);
2550 : :
2551 : : /*
2552 : : * If the new ACL is equal to the default, we don't need the catalog
2553 : : * entry any longer. Delete it rather than updating it, to avoid
2554 : : * leaving a degenerate entry.
2555 : : */
2556 [ + + ]: 68 : if (aclequal(new_acl, acldefault(istmt->objtype, ownerId)))
2557 : : {
2558 : 30 : CatalogTupleDelete(relation, &tuple->t_self);
2559 : : }
2560 : : else
2561 : : {
2562 : : /* finished building new ACL value, now insert it */
2563 : : HeapTuple newtuple;
2564 : 38 : Datum values[Natts_pg_parameter_acl] = {0};
2565 : 38 : bool nulls[Natts_pg_parameter_acl] = {0};
2566 : 38 : bool replaces[Natts_pg_parameter_acl] = {0};
2567 : :
2568 : 38 : replaces[Anum_pg_parameter_acl_paracl - 1] = true;
2569 : 38 : values[Anum_pg_parameter_acl_paracl - 1] = PointerGetDatum(new_acl);
2570 : :
2571 : 38 : newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation),
2572 : : values, nulls, replaces);
2573 : :
2574 : 38 : CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2575 : : }
2576 : :
2577 : : /* Update initial privileges for extensions */
2578 : 68 : recordExtensionInitPriv(parameterId, ParameterAclRelationId, 0,
2579 : : new_acl);
2580 : :
2581 : : /* Update the shared dependency ACL info */
2582 : 68 : updateAclDependencies(ParameterAclRelationId, parameterId, 0,
2583 : : ownerId,
2584 : : noldmembers, oldmembers,
2585 : : nnewmembers, newmembers);
2586 : :
2587 : 68 : ReleaseSysCache(tuple);
2588 : 68 : pfree(new_acl);
2589 : :
2590 : : /* prevent error when processing duplicate objects */
2591 : 68 : CommandCounterIncrement();
2592 : : }
2593 : :
2594 : 49 : table_close(relation, RowExclusiveLock);
2595 : 49 : }
2596 : :
2597 : :
2598 : : static AclMode
2599 : 11599 : string_to_privilege(const char *privname)
2600 : : {
2601 [ + + ]: 11599 : if (strcmp(privname, "insert") == 0)
2602 : 160 : return ACL_INSERT;
2603 [ + + ]: 11439 : if (strcmp(privname, "select") == 0)
2604 : 9755 : return ACL_SELECT;
2605 [ + + ]: 1684 : if (strcmp(privname, "update") == 0)
2606 : 317 : return ACL_UPDATE;
2607 [ + + ]: 1367 : if (strcmp(privname, "delete") == 0)
2608 : 99 : return ACL_DELETE;
2609 [ + + ]: 1268 : if (strcmp(privname, "truncate") == 0)
2610 : 29 : return ACL_TRUNCATE;
2611 [ + + ]: 1239 : if (strcmp(privname, "references") == 0)
2612 : 9 : return ACL_REFERENCES;
2613 [ + + ]: 1230 : if (strcmp(privname, "trigger") == 0)
2614 : 5 : return ACL_TRIGGER;
2615 [ + + ]: 1225 : if (strcmp(privname, "execute") == 0)
2616 : 270 : return ACL_EXECUTE;
2617 [ + + ]: 955 : if (strcmp(privname, "usage") == 0)
2618 : 532 : return ACL_USAGE;
2619 [ + + ]: 423 : if (strcmp(privname, "create") == 0)
2620 : 205 : return ACL_CREATE;
2621 [ + + ]: 218 : if (strcmp(privname, "temporary") == 0)
2622 : 120 : return ACL_CREATE_TEMP;
2623 [ + + ]: 98 : if (strcmp(privname, "temp") == 0)
2624 : 1 : return ACL_CREATE_TEMP;
2625 [ + + ]: 97 : if (strcmp(privname, "connect") == 0)
2626 : 23 : return ACL_CONNECT;
2627 [ + + ]: 74 : if (strcmp(privname, "set") == 0)
2628 : 25 : return ACL_SET;
2629 [ + + ]: 49 : if (strcmp(privname, "alter system") == 0)
2630 : 12 : return ACL_ALTER_SYSTEM;
2631 [ + - ]: 37 : if (strcmp(privname, "maintain") == 0)
2632 : 37 : return ACL_MAINTAIN;
2633 [ # # ]: 0 : ereport(ERROR,
2634 : : (errcode(ERRCODE_SYNTAX_ERROR),
2635 : : errmsg("unrecognized privilege type \"%s\"", privname)));
2636 : : return 0; /* appease compiler */
2637 : : }
2638 : :
2639 : : static const char *
2640 : 20 : privilege_to_string(AclMode privilege)
2641 : : {
2642 [ + - + - : 20 : switch (privilege)
- - - - +
- - - - -
- - ]
2643 : : {
2644 : 4 : case ACL_INSERT:
2645 : 4 : return "INSERT";
2646 : 0 : case ACL_SELECT:
2647 : 0 : return "SELECT";
2648 : 4 : case ACL_UPDATE:
2649 : 4 : return "UPDATE";
2650 : 0 : case ACL_DELETE:
2651 : 0 : return "DELETE";
2652 : 0 : case ACL_TRUNCATE:
2653 : 0 : return "TRUNCATE";
2654 : 0 : case ACL_REFERENCES:
2655 : 0 : return "REFERENCES";
2656 : 0 : case ACL_TRIGGER:
2657 : 0 : return "TRIGGER";
2658 : 0 : case ACL_EXECUTE:
2659 : 0 : return "EXECUTE";
2660 : 12 : case ACL_USAGE:
2661 : 12 : return "USAGE";
2662 : 0 : case ACL_CREATE:
2663 : 0 : return "CREATE";
2664 : 0 : case ACL_CREATE_TEMP:
2665 : 0 : return "TEMP";
2666 : 0 : case ACL_CONNECT:
2667 : 0 : return "CONNECT";
2668 : 0 : case ACL_SET:
2669 : 0 : return "SET";
2670 : 0 : case ACL_ALTER_SYSTEM:
2671 : 0 : return "ALTER SYSTEM";
2672 : 0 : case ACL_MAINTAIN:
2673 : 0 : return "MAINTAIN";
2674 : 0 : default:
2675 [ # # ]: 0 : elog(ERROR, "unrecognized privilege: %d", (int) privilege);
2676 : : }
2677 : : return NULL; /* appease compiler */
2678 : : }
2679 : :
2680 : : /*
2681 : : * Standardized reporting of aclcheck permissions failures.
2682 : : *
2683 : : * Note: we do not double-quote the %s's below, because many callers
2684 : : * supply strings that might be already quoted.
2685 : : */
2686 : : void
2687 : 1991 : aclcheck_error(AclResult aclerr, ObjectType objtype,
2688 : : const char *objectname)
2689 : : {
2690 [ - + + - ]: 1991 : switch (aclerr)
2691 : : {
2692 : 0 : case ACLCHECK_OK:
2693 : : /* no error, so return to caller */
2694 : 0 : break;
2695 : 1617 : case ACLCHECK_NO_PRIV:
2696 : : {
2697 : 1617 : const char *msg = "???";
2698 : :
2699 [ + - - - : 1617 : switch (objtype)
+ - - - +
+ + + + +
- + - - -
- - + + -
- + - - -
+ + - - +
+ - - ]
2700 : : {
2701 : 4 : case OBJECT_AGGREGATE:
2702 : 4 : msg = gettext_noop("permission denied for aggregate %s");
2703 : 4 : break;
2704 : 0 : case OBJECT_COLLATION:
2705 : 0 : msg = gettext_noop("permission denied for collation %s");
2706 : 0 : break;
2707 : 0 : case OBJECT_COLUMN:
2708 : 0 : msg = gettext_noop("permission denied for column %s");
2709 : 0 : break;
2710 : 0 : case OBJECT_CONVERSION:
2711 : 0 : msg = gettext_noop("permission denied for conversion %s");
2712 : 0 : break;
2713 : 13 : case OBJECT_DATABASE:
2714 : 13 : msg = gettext_noop("permission denied for database %s");
2715 : 13 : break;
2716 : 0 : case OBJECT_DOMAIN:
2717 : 0 : msg = gettext_noop("permission denied for domain %s");
2718 : 0 : break;
2719 : 0 : case OBJECT_EVENT_TRIGGER:
2720 : 0 : msg = gettext_noop("permission denied for event trigger %s");
2721 : 0 : break;
2722 : 0 : case OBJECT_EXTENSION:
2723 : 0 : msg = gettext_noop("permission denied for extension %s");
2724 : 0 : break;
2725 : 29 : case OBJECT_FDW:
2726 : 29 : msg = gettext_noop("permission denied for foreign-data wrapper %s");
2727 : 29 : break;
2728 : 17 : case OBJECT_FOREIGN_SERVER:
2729 : 17 : msg = gettext_noop("permission denied for foreign server %s");
2730 : 17 : break;
2731 : 1 : case OBJECT_FOREIGN_TABLE:
2732 : 1 : msg = gettext_noop("permission denied for foreign table %s");
2733 : 1 : break;
2734 : 61 : case OBJECT_FUNCTION:
2735 : 61 : msg = gettext_noop("permission denied for function %s");
2736 : 61 : break;
2737 : 9 : case OBJECT_INDEX:
2738 : 9 : msg = gettext_noop("permission denied for index %s");
2739 : 9 : break;
2740 : 5 : case OBJECT_LANGUAGE:
2741 : 5 : msg = gettext_noop("permission denied for language %s");
2742 : 5 : break;
2743 : 0 : case OBJECT_LARGEOBJECT:
2744 : 0 : msg = gettext_noop("permission denied for large object %s");
2745 : 0 : break;
2746 : 4 : case OBJECT_MATVIEW:
2747 : 4 : msg = gettext_noop("permission denied for materialized view %s");
2748 : 4 : break;
2749 : 0 : case OBJECT_OPCLASS:
2750 : 0 : msg = gettext_noop("permission denied for operator class %s");
2751 : 0 : break;
2752 : 0 : case OBJECT_OPERATOR:
2753 : 0 : msg = gettext_noop("permission denied for operator %s");
2754 : 0 : break;
2755 : 0 : case OBJECT_OPFAMILY:
2756 : 0 : msg = gettext_noop("permission denied for operator family %s");
2757 : 0 : break;
2758 : 0 : case OBJECT_PARAMETER_ACL:
2759 : 0 : msg = gettext_noop("permission denied for parameter %s");
2760 : 0 : break;
2761 : 0 : case OBJECT_POLICY:
2762 : 0 : msg = gettext_noop("permission denied for policy %s");
2763 : 0 : break;
2764 : 8 : case OBJECT_PROCEDURE:
2765 : 8 : msg = gettext_noop("permission denied for procedure %s");
2766 : 8 : break;
2767 : 8 : case OBJECT_PROPGRAPH:
2768 : 8 : msg = gettext_noop("permission denied for property graph %s");
2769 : 8 : break;
2770 : 0 : case OBJECT_PUBLICATION:
2771 : 0 : msg = gettext_noop("permission denied for publication %s");
2772 : 0 : break;
2773 : 0 : case OBJECT_ROUTINE:
2774 : 0 : msg = gettext_noop("permission denied for routine %s");
2775 : 0 : break;
2776 : 37 : case OBJECT_SCHEMA:
2777 : 37 : msg = gettext_noop("permission denied for schema %s");
2778 : 37 : break;
2779 : 0 : case OBJECT_SEQUENCE:
2780 : 0 : msg = gettext_noop("permission denied for sequence %s");
2781 : 0 : break;
2782 : 0 : case OBJECT_STATISTIC_EXT:
2783 : 0 : msg = gettext_noop("permission denied for statistics object %s");
2784 : 0 : break;
2785 : 0 : case OBJECT_SUBSCRIPTION:
2786 : 0 : msg = gettext_noop("permission denied for subscription %s");
2787 : 0 : break;
2788 : 997 : case OBJECT_TABLE:
2789 : 997 : msg = gettext_noop("permission denied for table %s");
2790 : 997 : break;
2791 : 10 : case OBJECT_TABLESPACE:
2792 : 10 : msg = gettext_noop("permission denied for tablespace %s");
2793 : 10 : break;
2794 : 0 : case OBJECT_TSCONFIGURATION:
2795 : 0 : msg = gettext_noop("permission denied for text search configuration %s");
2796 : 0 : break;
2797 : 0 : case OBJECT_TSDICTIONARY:
2798 : 0 : msg = gettext_noop("permission denied for text search dictionary %s");
2799 : 0 : break;
2800 : 136 : case OBJECT_TYPE:
2801 : 136 : msg = gettext_noop("permission denied for type %s");
2802 : 136 : break;
2803 : 278 : case OBJECT_VIEW:
2804 : 278 : msg = gettext_noop("permission denied for view %s");
2805 : 278 : break;
2806 : : /* these currently aren't used */
2807 : 0 : case OBJECT_ACCESS_METHOD:
2808 : : case OBJECT_AMOP:
2809 : : case OBJECT_AMPROC:
2810 : : case OBJECT_ATTRIBUTE:
2811 : : case OBJECT_CAST:
2812 : : case OBJECT_DEFAULT:
2813 : : case OBJECT_DEFACL:
2814 : : case OBJECT_DOMCONSTRAINT:
2815 : : case OBJECT_PUBLICATION_NAMESPACE:
2816 : : case OBJECT_PUBLICATION_REL:
2817 : : case OBJECT_ROLE:
2818 : : case OBJECT_RULE:
2819 : : case OBJECT_TABCONSTRAINT:
2820 : : case OBJECT_TRANSFORM:
2821 : : case OBJECT_TRIGGER:
2822 : : case OBJECT_TSPARSER:
2823 : : case OBJECT_TSTEMPLATE:
2824 : : case OBJECT_USER_MAPPING:
2825 [ # # ]: 0 : elog(ERROR, "unsupported object type: %d", objtype);
2826 : : }
2827 : :
2828 [ + - ]: 1617 : ereport(ERROR,
2829 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2830 : : errmsg(msg, objectname)));
2831 : : break;
2832 : : }
2833 : 374 : case ACLCHECK_NOT_OWNER:
2834 : : {
2835 : 374 : const char *msg = "???";
2836 : :
2837 [ + - + - : 374 : switch (objtype)
- - - + +
- + + + -
- + + + +
+ + - + +
+ + + + +
- + + + -
- ]
2838 : : {
2839 : 4 : case OBJECT_AGGREGATE:
2840 : 4 : msg = gettext_noop("must be owner of aggregate %s");
2841 : 4 : break;
2842 : 0 : case OBJECT_COLLATION:
2843 : 0 : msg = gettext_noop("must be owner of collation %s");
2844 : 0 : break;
2845 : 12 : case OBJECT_CONVERSION:
2846 : 12 : msg = gettext_noop("must be owner of conversion %s");
2847 : 12 : break;
2848 : 0 : case OBJECT_DATABASE:
2849 : 0 : msg = gettext_noop("must be owner of database %s");
2850 : 0 : break;
2851 : 0 : case OBJECT_DOMAIN:
2852 : 0 : msg = gettext_noop("must be owner of domain %s");
2853 : 0 : break;
2854 : 0 : case OBJECT_EVENT_TRIGGER:
2855 : 0 : msg = gettext_noop("must be owner of event trigger %s");
2856 : 0 : break;
2857 : 0 : case OBJECT_EXTENSION:
2858 : 0 : msg = gettext_noop("must be owner of extension %s");
2859 : 0 : break;
2860 : 12 : case OBJECT_FDW:
2861 : 12 : msg = gettext_noop("must be owner of foreign-data wrapper %s");
2862 : 12 : break;
2863 : 76 : case OBJECT_FOREIGN_SERVER:
2864 : 76 : msg = gettext_noop("must be owner of foreign server %s");
2865 : 76 : break;
2866 : 0 : case OBJECT_FOREIGN_TABLE:
2867 : 0 : msg = gettext_noop("must be owner of foreign table %s");
2868 : 0 : break;
2869 : 28 : case OBJECT_FUNCTION:
2870 : 28 : msg = gettext_noop("must be owner of function %s");
2871 : 28 : break;
2872 : 16 : case OBJECT_INDEX:
2873 : 16 : msg = gettext_noop("must be owner of index %s");
2874 : 16 : break;
2875 : 8 : case OBJECT_LANGUAGE:
2876 : 8 : msg = gettext_noop("must be owner of language %s");
2877 : 8 : break;
2878 : 0 : case OBJECT_LARGEOBJECT:
2879 : 0 : msg = gettext_noop("must be owner of large object %s");
2880 : 0 : break;
2881 : 0 : case OBJECT_MATVIEW:
2882 : 0 : msg = gettext_noop("must be owner of materialized view %s");
2883 : 0 : break;
2884 : 12 : case OBJECT_OPCLASS:
2885 : 12 : msg = gettext_noop("must be owner of operator class %s");
2886 : 12 : break;
2887 : 12 : case OBJECT_OPERATOR:
2888 : 12 : msg = gettext_noop("must be owner of operator %s");
2889 : 12 : break;
2890 : 12 : case OBJECT_OPFAMILY:
2891 : 12 : msg = gettext_noop("must be owner of operator family %s");
2892 : 12 : break;
2893 : 4 : case OBJECT_PROCEDURE:
2894 : 4 : msg = gettext_noop("must be owner of procedure %s");
2895 : 4 : break;
2896 : 12 : case OBJECT_PROPGRAPH:
2897 : 12 : msg = gettext_noop("must be owner of property graph %s");
2898 : 12 : break;
2899 : 4 : case OBJECT_PUBLICATION:
2900 : 4 : msg = gettext_noop("must be owner of publication %s");
2901 : 4 : break;
2902 : 0 : case OBJECT_ROUTINE:
2903 : 0 : msg = gettext_noop("must be owner of routine %s");
2904 : 0 : break;
2905 : 4 : case OBJECT_SEQUENCE:
2906 : 4 : msg = gettext_noop("must be owner of sequence %s");
2907 : 4 : break;
2908 : 4 : case OBJECT_SUBSCRIPTION:
2909 : 4 : msg = gettext_noop("must be owner of subscription %s");
2910 : 4 : break;
2911 : 66 : case OBJECT_TABLE:
2912 : 66 : msg = gettext_noop("must be owner of table %s");
2913 : 66 : break;
2914 : 4 : case OBJECT_TYPE:
2915 : 4 : msg = gettext_noop("must be owner of type %s");
2916 : 4 : break;
2917 : 12 : case OBJECT_VIEW:
2918 : 12 : msg = gettext_noop("must be owner of view %s");
2919 : 12 : break;
2920 : 12 : case OBJECT_SCHEMA:
2921 : 12 : msg = gettext_noop("must be owner of schema %s");
2922 : 12 : break;
2923 : 24 : case OBJECT_STATISTIC_EXT:
2924 : 24 : msg = gettext_noop("must be owner of statistics object %s");
2925 : 24 : break;
2926 : 0 : case OBJECT_TABLESPACE:
2927 : 0 : msg = gettext_noop("must be owner of tablespace %s");
2928 : 0 : break;
2929 : 12 : case OBJECT_TSCONFIGURATION:
2930 : 12 : msg = gettext_noop("must be owner of text search configuration %s");
2931 : 12 : break;
2932 : 12 : case OBJECT_TSDICTIONARY:
2933 : 12 : msg = gettext_noop("must be owner of text search dictionary %s");
2934 : 12 : break;
2935 : :
2936 : : /*
2937 : : * Special cases: For these, the error message talks
2938 : : * about "relation", because that's where the
2939 : : * ownership is attached. See also
2940 : : * check_object_ownership().
2941 : : */
2942 : 12 : case OBJECT_COLUMN:
2943 : : case OBJECT_POLICY:
2944 : : case OBJECT_RULE:
2945 : : case OBJECT_TABCONSTRAINT:
2946 : : case OBJECT_TRIGGER:
2947 : 12 : msg = gettext_noop("must be owner of relation %s");
2948 : 12 : break;
2949 : : /* these currently aren't used */
2950 : 0 : case OBJECT_ACCESS_METHOD:
2951 : : case OBJECT_AMOP:
2952 : : case OBJECT_AMPROC:
2953 : : case OBJECT_ATTRIBUTE:
2954 : : case OBJECT_CAST:
2955 : : case OBJECT_DEFAULT:
2956 : : case OBJECT_DEFACL:
2957 : : case OBJECT_DOMCONSTRAINT:
2958 : : case OBJECT_PARAMETER_ACL:
2959 : : case OBJECT_PUBLICATION_NAMESPACE:
2960 : : case OBJECT_PUBLICATION_REL:
2961 : : case OBJECT_ROLE:
2962 : : case OBJECT_TRANSFORM:
2963 : : case OBJECT_TSPARSER:
2964 : : case OBJECT_TSTEMPLATE:
2965 : : case OBJECT_USER_MAPPING:
2966 [ # # ]: 0 : elog(ERROR, "unsupported object type: %d", objtype);
2967 : : }
2968 : :
2969 [ + - ]: 374 : ereport(ERROR,
2970 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2971 : : errmsg(msg, objectname)));
2972 : : break;
2973 : : }
2974 : 0 : default:
2975 [ # # ]: 0 : elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
2976 : : break;
2977 : : }
2978 : 0 : }
2979 : :
2980 : :
2981 : : void
2982 : 0 : aclcheck_error_col(AclResult aclerr, ObjectType objtype,
2983 : : const char *objectname, const char *colname)
2984 : : {
2985 [ # # # # ]: 0 : switch (aclerr)
2986 : : {
2987 : 0 : case ACLCHECK_OK:
2988 : : /* no error, so return to caller */
2989 : 0 : break;
2990 : 0 : case ACLCHECK_NO_PRIV:
2991 [ # # ]: 0 : ereport(ERROR,
2992 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2993 : : errmsg("permission denied for column \"%s\" of relation \"%s\"",
2994 : : colname, objectname)));
2995 : : break;
2996 : 0 : case ACLCHECK_NOT_OWNER:
2997 : : /* relation msg is OK since columns don't have separate owners */
2998 : 0 : aclcheck_error(aclerr, objtype, objectname);
2999 : 0 : break;
3000 : 0 : default:
3001 [ # # ]: 0 : elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
3002 : : break;
3003 : : }
3004 : 0 : }
3005 : :
3006 : :
3007 : : /*
3008 : : * Special common handling for types: use element type instead of array type,
3009 : : * and format nicely
3010 : : */
3011 : : void
3012 : 136 : aclcheck_error_type(AclResult aclerr, Oid typeOid)
3013 : : {
3014 : 136 : Oid element_type = get_element_type(typeOid);
3015 : :
3016 [ + + ]: 136 : aclcheck_error(aclerr, OBJECT_TYPE, format_type_be(element_type ? element_type : typeOid));
3017 : 0 : }
3018 : :
3019 : :
3020 : : /*
3021 : : * Relay for the various pg_*_mask routines depending on object kind
3022 : : */
3023 : : static AclMode
3024 : 48 : pg_aclmask(ObjectType objtype, Oid object_oid, AttrNumber attnum, Oid roleid,
3025 : : AclMode mask, AclMaskHow how)
3026 : : {
3027 [ - + - - : 48 : switch (objtype)
+ - - - -
- + + - +
- ]
3028 : : {
3029 : 0 : case OBJECT_COLUMN:
3030 : : return
3031 : 0 : pg_class_aclmask(object_oid, roleid, mask, how) |
3032 : 0 : pg_attribute_aclmask(object_oid, attnum, roleid, mask, how);
3033 : 12 : case OBJECT_TABLE:
3034 : : case OBJECT_SEQUENCE:
3035 : : case OBJECT_PROPGRAPH:
3036 : 12 : return pg_class_aclmask(object_oid, roleid, mask, how);
3037 : 0 : case OBJECT_DATABASE:
3038 : 0 : return object_aclmask(DatabaseRelationId, object_oid, roleid, mask, how);
3039 : 0 : case OBJECT_FUNCTION:
3040 : 0 : return object_aclmask(ProcedureRelationId, object_oid, roleid, mask, how);
3041 : 4 : case OBJECT_LANGUAGE:
3042 : 4 : return object_aclmask(LanguageRelationId, object_oid, roleid, mask, how);
3043 : 0 : case OBJECT_LARGEOBJECT:
3044 : 0 : return pg_largeobject_aclmask_snapshot(object_oid, roleid,
3045 : : mask, how, NULL);
3046 : 0 : case OBJECT_PARAMETER_ACL:
3047 : 0 : return pg_parameter_acl_aclmask(object_oid, roleid, mask, how);
3048 : 0 : case OBJECT_SCHEMA:
3049 : 0 : return object_aclmask(NamespaceRelationId, object_oid, roleid, mask, how);
3050 : 0 : case OBJECT_STATISTIC_EXT:
3051 [ # # ]: 0 : elog(ERROR, "grantable rights not supported for statistics objects");
3052 : : /* not reached, but keep compiler quiet */
3053 : : return ACL_NO_RIGHTS;
3054 : 0 : case OBJECT_TABLESPACE:
3055 : 0 : return object_aclmask(TableSpaceRelationId, object_oid, roleid, mask, how);
3056 : 12 : case OBJECT_FDW:
3057 : 12 : return object_aclmask(ForeignDataWrapperRelationId, object_oid, roleid, mask, how);
3058 : 12 : case OBJECT_FOREIGN_SERVER:
3059 : 12 : return object_aclmask(ForeignServerRelationId, object_oid, roleid, mask, how);
3060 : 0 : case OBJECT_EVENT_TRIGGER:
3061 [ # # ]: 0 : elog(ERROR, "grantable rights not supported for event triggers");
3062 : : /* not reached, but keep compiler quiet */
3063 : : return ACL_NO_RIGHTS;
3064 : 8 : case OBJECT_TYPE:
3065 : 8 : return object_aclmask(TypeRelationId, object_oid, roleid, mask, how);
3066 : 0 : default:
3067 [ # # ]: 0 : elog(ERROR, "unrecognized object type: %d",
3068 : : (int) objtype);
3069 : : /* not reached, but keep compiler quiet */
3070 : : return ACL_NO_RIGHTS;
3071 : : }
3072 : : }
3073 : :
3074 : :
3075 : : /*
3076 : : * ****************************************************************
3077 : : * Exported routines for examining a user's privileges for various objects
3078 : : *
3079 : : * See aclmask() for a description of the common API for these functions.
3080 : : * ****************************************************************
3081 : : */
3082 : :
3083 : : /*
3084 : : * Generic routine for examining a user's privileges for an object
3085 : : */
3086 : : static AclMode
3087 : 36 : object_aclmask(Oid classid, Oid objectid, Oid roleid,
3088 : : AclMode mask, AclMaskHow how)
3089 : : {
3090 : 36 : return object_aclmask_ext(classid, objectid, roleid, mask, how, NULL);
3091 : : }
3092 : :
3093 : : /*
3094 : : * Generic routine for examining a user's privileges for an object,
3095 : : * with is_missing
3096 : : */
3097 : : static AclMode
3098 : 2120894 : object_aclmask_ext(Oid classid, Oid objectid, Oid roleid,
3099 : : AclMode mask, AclMaskHow how,
3100 : : bool *is_missing)
3101 : : {
3102 : : SysCacheIdentifier cacheid;
3103 : : AclMode result;
3104 : : HeapTuple tuple;
3105 : : Datum aclDatum;
3106 : : bool isNull;
3107 : : Acl *acl;
3108 : : Oid ownerId;
3109 : :
3110 : : /* Special cases */
3111 [ + + + ]: 2120894 : switch (classid)
3112 : : {
3113 : 647118 : case NamespaceRelationId:
3114 : 647118 : return pg_namespace_aclmask_ext(objectid, roleid, mask, how,
3115 : : is_missing);
3116 : 224387 : case TypeRelationId:
3117 : 224387 : return pg_type_aclmask_ext(objectid, roleid, mask, how,
3118 : : is_missing);
3119 : : }
3120 : :
3121 : : /* Even more special cases */
3122 : : Assert(classid != RelationRelationId); /* should use pg_class_acl* */
3123 : : Assert(classid != LargeObjectMetadataRelationId); /* should use
3124 : : * pg_largeobject_acl* */
3125 : :
3126 : : /* Superusers bypass all permission checking. */
3127 [ + + ]: 1249389 : if (superuser_arg(roleid))
3128 : 1217649 : return mask;
3129 : :
3130 : : /*
3131 : : * Get the object's ACL from its catalog
3132 : : */
3133 : :
3134 : 31740 : cacheid = get_object_catcache_oid(classid);
3135 : :
3136 : 31740 : tuple = SearchSysCache1(cacheid, ObjectIdGetDatum(objectid));
3137 [ - + ]: 31740 : if (!HeapTupleIsValid(tuple))
3138 : : {
3139 [ # # ]: 0 : if (is_missing != NULL)
3140 : : {
3141 : : /* return "no privileges" instead of throwing an error */
3142 : 0 : *is_missing = true;
3143 : 0 : return 0;
3144 : : }
3145 : : else
3146 [ # # ]: 0 : elog(ERROR, "cache lookup failed for %s %u",
3147 : : get_object_class_descr(classid), objectid);
3148 : : }
3149 : :
3150 : 31740 : ownerId = DatumGetObjectId(SysCacheGetAttrNotNull(cacheid,
3151 : : tuple,
3152 : 31740 : get_object_attnum_owner(classid)));
3153 : :
3154 : 31740 : aclDatum = SysCacheGetAttr(cacheid, tuple, get_object_attnum_acl(classid),
3155 : : &isNull);
3156 [ + + ]: 31740 : if (isNull)
3157 : : {
3158 : : /* No ACL, so build default ACL */
3159 : 29972 : acl = acldefault(get_object_type(classid, objectid), ownerId);
3160 : 29972 : aclDatum = (Datum) 0;
3161 : : }
3162 : : else
3163 : : {
3164 : : /* detoast ACL if necessary */
3165 : 1768 : acl = DatumGetAclP(aclDatum);
3166 : : }
3167 : :
3168 : 31740 : result = aclmask(acl, roleid, ownerId, mask, how);
3169 : :
3170 : : /* if we have a detoasted copy, free it */
3171 [ + - + - ]: 31740 : if (acl && acl != DatumGetPointer(aclDatum))
3172 : 31740 : pfree(acl);
3173 : :
3174 : 31740 : ReleaseSysCache(tuple);
3175 : :
3176 : 31740 : return result;
3177 : : }
3178 : :
3179 : : /*
3180 : : * Routine for examining a user's privileges for a column
3181 : : *
3182 : : * Note: this considers only privileges granted specifically on the column.
3183 : : * It is caller's responsibility to take relation-level privileges into account
3184 : : * as appropriate. (For the same reason, we have no special case for
3185 : : * superuser-ness here.)
3186 : : */
3187 : : static AclMode
3188 : 0 : pg_attribute_aclmask(Oid table_oid, AttrNumber attnum, Oid roleid,
3189 : : AclMode mask, AclMaskHow how)
3190 : : {
3191 : 0 : return pg_attribute_aclmask_ext(table_oid, attnum, roleid,
3192 : : mask, how, NULL);
3193 : : }
3194 : :
3195 : : /*
3196 : : * Routine for examining a user's privileges for a column, with is_missing
3197 : : */
3198 : : static AclMode
3199 : 391166 : pg_attribute_aclmask_ext(Oid table_oid, AttrNumber attnum, Oid roleid,
3200 : : AclMode mask, AclMaskHow how, bool *is_missing)
3201 : : {
3202 : : AclMode result;
3203 : : HeapTuple classTuple;
3204 : : HeapTuple attTuple;
3205 : : Form_pg_class classForm;
3206 : : Form_pg_attribute attributeForm;
3207 : : Datum aclDatum;
3208 : : bool isNull;
3209 : : Acl *acl;
3210 : : Oid ownerId;
3211 : :
3212 : : /*
3213 : : * First, get the column's ACL from its pg_attribute entry
3214 : : */
3215 : 391166 : attTuple = SearchSysCache2(ATTNUM,
3216 : : ObjectIdGetDatum(table_oid),
3217 : : Int16GetDatum(attnum));
3218 [ + + ]: 391166 : if (!HeapTupleIsValid(attTuple))
3219 : : {
3220 [ + - ]: 20 : if (is_missing != NULL)
3221 : : {
3222 : : /* return "no privileges" instead of throwing an error */
3223 : 20 : *is_missing = true;
3224 : 20 : return 0;
3225 : : }
3226 : : else
3227 [ # # ]: 0 : ereport(ERROR,
3228 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
3229 : : errmsg("attribute %d of relation with OID %u does not exist",
3230 : : attnum, table_oid)));
3231 : : }
3232 : :
3233 : 391146 : attributeForm = (Form_pg_attribute) GETSTRUCT(attTuple);
3234 : :
3235 : : /* Check dropped columns, too */
3236 [ + + ]: 391146 : if (attributeForm->attisdropped)
3237 : : {
3238 [ + - ]: 8 : if (is_missing != NULL)
3239 : : {
3240 : : /* return "no privileges" instead of throwing an error */
3241 : 8 : *is_missing = true;
3242 : 8 : ReleaseSysCache(attTuple);
3243 : 8 : return 0;
3244 : : }
3245 : : else
3246 [ # # ]: 0 : ereport(ERROR,
3247 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
3248 : : errmsg("attribute %d of relation with OID %u does not exist",
3249 : : attnum, table_oid)));
3250 : : }
3251 : :
3252 : 391138 : aclDatum = SysCacheGetAttr(ATTNUM, attTuple, Anum_pg_attribute_attacl,
3253 : : &isNull);
3254 : :
3255 : : /*
3256 : : * Here we hard-wire knowledge that the default ACL for a column grants no
3257 : : * privileges, so that we can fall out quickly in the very common case
3258 : : * where attacl is null.
3259 : : */
3260 [ + + ]: 391138 : if (isNull)
3261 : : {
3262 : 384460 : ReleaseSysCache(attTuple);
3263 : 384460 : return 0;
3264 : : }
3265 : :
3266 : : /*
3267 : : * Must get the relation's ownerId from pg_class. Since we already found
3268 : : * a pg_attribute entry, the only likely reason for this to fail is that a
3269 : : * concurrent DROP of the relation committed since then (which could only
3270 : : * happen if we don't have lock on the relation). Treat that similarly to
3271 : : * not finding the attribute entry.
3272 : : */
3273 : 6678 : classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
3274 [ - + ]: 6678 : if (!HeapTupleIsValid(classTuple))
3275 : : {
3276 : 0 : ReleaseSysCache(attTuple);
3277 [ # # ]: 0 : if (is_missing != NULL)
3278 : : {
3279 : : /* return "no privileges" instead of throwing an error */
3280 : 0 : *is_missing = true;
3281 : 0 : return 0;
3282 : : }
3283 : : else
3284 [ # # ]: 0 : ereport(ERROR,
3285 : : (errcode(ERRCODE_UNDEFINED_TABLE),
3286 : : errmsg("relation with OID %u does not exist",
3287 : : table_oid)));
3288 : : }
3289 : 6678 : classForm = (Form_pg_class) GETSTRUCT(classTuple);
3290 : :
3291 : 6678 : ownerId = classForm->relowner;
3292 : :
3293 : 6678 : ReleaseSysCache(classTuple);
3294 : :
3295 : : /* detoast column's ACL if necessary */
3296 : 6678 : acl = DatumGetAclP(aclDatum);
3297 : :
3298 : 6678 : result = aclmask(acl, roleid, ownerId, mask, how);
3299 : :
3300 : : /* if we have a detoasted copy, free it */
3301 [ + - + - ]: 6678 : if (acl && acl != DatumGetPointer(aclDatum))
3302 : 6678 : pfree(acl);
3303 : :
3304 : 6678 : ReleaseSysCache(attTuple);
3305 : :
3306 : 6678 : return result;
3307 : : }
3308 : :
3309 : : /*
3310 : : * Exported routine for examining a user's privileges for a table
3311 : : */
3312 : : AclMode
3313 : 397195 : pg_class_aclmask(Oid table_oid, Oid roleid,
3314 : : AclMode mask, AclMaskHow how)
3315 : : {
3316 : 397195 : return pg_class_aclmask_ext(table_oid, roleid, mask, how, NULL);
3317 : : }
3318 : :
3319 : : /*
3320 : : * Routine for examining a user's privileges for a table, with is_missing
3321 : : */
3322 : : static AclMode
3323 : 2451191 : pg_class_aclmask_ext(Oid table_oid, Oid roleid, AclMode mask,
3324 : : AclMaskHow how, bool *is_missing)
3325 : : {
3326 : : AclMode result;
3327 : : HeapTuple tuple;
3328 : : Form_pg_class classForm;
3329 : : Datum aclDatum;
3330 : : bool isNull;
3331 : : Acl *acl;
3332 : : Oid ownerId;
3333 : :
3334 : : /*
3335 : : * Must get the relation's tuple from pg_class
3336 : : */
3337 : 2451191 : tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
3338 [ + + ]: 2451191 : if (!HeapTupleIsValid(tuple))
3339 : : {
3340 [ + - ]: 5 : if (is_missing != NULL)
3341 : : {
3342 : : /* return "no privileges" instead of throwing an error */
3343 : 5 : *is_missing = true;
3344 : 5 : return 0;
3345 : : }
3346 : : else
3347 [ # # ]: 0 : ereport(ERROR,
3348 : : (errcode(ERRCODE_UNDEFINED_TABLE),
3349 : : errmsg("relation with OID %u does not exist",
3350 : : table_oid)));
3351 : : }
3352 : :
3353 : 2451186 : classForm = (Form_pg_class) GETSTRUCT(tuple);
3354 : :
3355 [ + + ]: 2451186 : if (!superuser_arg(roleid))
3356 : : {
3357 [ + + ]: 40382 : if (mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE | ACL_USAGE))
3358 : : {
3359 [ + + ]: 4872 : if (IsConflictLogTableClass(classForm))
3360 : : {
3361 : : /*
3362 : : * For conflict log tables, allow non-superusers to perform
3363 : : * DELETE and TRUNCATE for cleanup and maintenance, while
3364 : : * still restricting INSERT, UPDATE, and USAGE.
3365 : : */
3366 : 8 : mask &= ~(ACL_INSERT | ACL_UPDATE | ACL_USAGE);
3367 : : }
3368 [ + + ]: 4864 : else if (IsSystemClass(table_oid, classForm) &&
3369 [ + - ]: 46 : classForm->relkind != RELKIND_VIEW)
3370 : : {
3371 : : /*
3372 : : * Deny anyone permission to update a system catalog unless
3373 : : * pg_authid.rolsuper is set.
3374 : : *
3375 : : * As of 7.4 we have some updatable system views; those
3376 : : * shouldn't be protected in this way. Assume the view rules
3377 : : * can take care of themselves. ACL_USAGE is if we ever have
3378 : : * system sequences.
3379 : : */
3380 : 46 : mask &= ~(ACL_INSERT | ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE |
3381 : : ACL_USAGE);
3382 : : }
3383 : : }
3384 : : }
3385 : : else
3386 : : {
3387 : : /* Superusers bypass all permission-checking. */
3388 : 2410804 : ReleaseSysCache(tuple);
3389 : 2410804 : return mask;
3390 : : }
3391 : :
3392 : : /*
3393 : : * Normal case: get the relation's ACL from pg_class
3394 : : */
3395 : 40382 : ownerId = classForm->relowner;
3396 : :
3397 : 40382 : aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
3398 : : &isNull);
3399 [ + + ]: 40382 : if (isNull)
3400 : : {
3401 : : /* No ACL, so build default ACL */
3402 [ + + + ]: 11705 : switch (classForm->relkind)
3403 : : {
3404 : 84 : case RELKIND_SEQUENCE:
3405 : 84 : acl = acldefault(OBJECT_SEQUENCE, ownerId);
3406 : 84 : break;
3407 : 4 : case RELKIND_PROPGRAPH:
3408 : 4 : acl = acldefault(OBJECT_PROPGRAPH, ownerId);
3409 : 4 : break;
3410 : 11617 : default:
3411 : 11617 : acl = acldefault(OBJECT_TABLE, ownerId);
3412 : 11617 : break;
3413 : : }
3414 : 11705 : aclDatum = (Datum) 0;
3415 : : }
3416 : : else
3417 : : {
3418 : : /* detoast rel's ACL if necessary */
3419 : 28677 : acl = DatumGetAclP(aclDatum);
3420 : : }
3421 : :
3422 : 40382 : result = aclmask(acl, roleid, ownerId, mask, how);
3423 : :
3424 : : /* if we have a detoasted copy, free it */
3425 [ + - + - ]: 40382 : if (acl && acl != DatumGetPointer(aclDatum))
3426 : 40382 : pfree(acl);
3427 : :
3428 : 40382 : ReleaseSysCache(tuple);
3429 : :
3430 : : /*
3431 : : * Check if ACL_SELECT is being checked and, if so, and not set already as
3432 : : * part of the result, then check if the user is a member of the
3433 : : * pg_read_all_data role, which allows read access to all relations.
3434 : : */
3435 [ + + + + : 44839 : if (mask & ACL_SELECT && !(result & ACL_SELECT) &&
+ + ]
3436 : 4457 : has_privs_of_role(roleid, ROLE_PG_READ_ALL_DATA))
3437 : 8 : result |= ACL_SELECT;
3438 : :
3439 : : /*
3440 : : * Check if ACL_INSERT, ACL_UPDATE, or ACL_DELETE is being checked and, if
3441 : : * so, and not set already as part of the result, then check if the user
3442 : : * is a member of the pg_write_all_data role, which allows
3443 : : * INSERT/UPDATE/DELETE access to all relations (except system catalogs,
3444 : : * which requires superuser, see above).
3445 : : */
3446 [ + + ]: 40382 : if (mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE) &&
3447 [ + + + + ]: 6093 : !(result & (ACL_INSERT | ACL_UPDATE | ACL_DELETE)) &&
3448 : 1361 : has_privs_of_role(roleid, ROLE_PG_WRITE_ALL_DATA))
3449 : 12 : result |= (mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE));
3450 : :
3451 : : /*
3452 : : * Check if ACL_MAINTAIN is being checked and, if so, and not already set
3453 : : * as part of the result, then check if the user is a member of the
3454 : : * pg_maintain role, which allows VACUUM, ANALYZE, CLUSTER, REPACK,
3455 : : * REFRESH MATERIALIZED VIEW, REINDEX, and LOCK TABLE on all relations.
3456 : : */
3457 [ + + ]: 40382 : if (mask & ACL_MAINTAIN &&
3458 [ + + + + ]: 11486 : !(result & ACL_MAINTAIN) &&
3459 : 5538 : has_privs_of_role(roleid, ROLE_PG_MAINTAIN))
3460 : 44 : result |= ACL_MAINTAIN;
3461 : :
3462 : 40382 : return result;
3463 : : }
3464 : :
3465 : : /*
3466 : : * Routine for examining a user's privileges for a configuration
3467 : : * parameter (GUC), identified by GUC name.
3468 : : */
3469 : : static AclMode
3470 : 80 : pg_parameter_aclmask(const char *name, Oid roleid, AclMode mask, AclMaskHow how)
3471 : : {
3472 : : AclMode result;
3473 : : char *parname;
3474 : : text *partext;
3475 : : HeapTuple tuple;
3476 : :
3477 : : /* Superusers bypass all permission checking. */
3478 [ + + ]: 80 : if (superuser_arg(roleid))
3479 : 1 : return mask;
3480 : :
3481 : : /* Convert name to the form it should have in pg_parameter_acl... */
3482 : 79 : parname = convert_GUC_name_for_parameter_acl(name);
3483 : 79 : partext = cstring_to_text(parname);
3484 : :
3485 : : /* ... and look it up */
3486 : 79 : tuple = SearchSysCache1(PARAMETERACLNAME, PointerGetDatum(partext));
3487 : :
3488 [ + + ]: 79 : if (!HeapTupleIsValid(tuple))
3489 : : {
3490 : : /* If no entry, GUC has no permissions for non-superusers */
3491 : 35 : result = ACL_NO_RIGHTS;
3492 : : }
3493 : : else
3494 : : {
3495 : : Datum aclDatum;
3496 : : bool isNull;
3497 : : Acl *acl;
3498 : :
3499 : 44 : aclDatum = SysCacheGetAttr(PARAMETERACLNAME, tuple,
3500 : : Anum_pg_parameter_acl_paracl,
3501 : : &isNull);
3502 [ - + ]: 44 : if (isNull)
3503 : : {
3504 : : /* No ACL, so build default ACL */
3505 : 0 : acl = acldefault(OBJECT_PARAMETER_ACL, BOOTSTRAP_SUPERUSERID);
3506 : 0 : aclDatum = (Datum) 0;
3507 : : }
3508 : : else
3509 : : {
3510 : : /* detoast ACL if necessary */
3511 : 44 : acl = DatumGetAclP(aclDatum);
3512 : : }
3513 : :
3514 : 44 : result = aclmask(acl, roleid, BOOTSTRAP_SUPERUSERID, mask, how);
3515 : :
3516 : : /* if we have a detoasted copy, free it */
3517 [ + - + - ]: 44 : if (acl && acl != DatumGetPointer(aclDatum))
3518 : 44 : pfree(acl);
3519 : :
3520 : 44 : ReleaseSysCache(tuple);
3521 : : }
3522 : :
3523 : 79 : pfree(parname);
3524 : 79 : pfree(partext);
3525 : :
3526 : 79 : return result;
3527 : : }
3528 : :
3529 : : /*
3530 : : * Routine for examining a user's privileges for a configuration
3531 : : * parameter (GUC), identified by the OID of its pg_parameter_acl entry.
3532 : : */
3533 : : static AclMode
3534 : 0 : pg_parameter_acl_aclmask(Oid acl_oid, Oid roleid, AclMode mask, AclMaskHow how)
3535 : : {
3536 : : AclMode result;
3537 : : HeapTuple tuple;
3538 : : Datum aclDatum;
3539 : : bool isNull;
3540 : : Acl *acl;
3541 : :
3542 : : /* Superusers bypass all permission checking. */
3543 [ # # ]: 0 : if (superuser_arg(roleid))
3544 : 0 : return mask;
3545 : :
3546 : : /* Get the ACL from pg_parameter_acl */
3547 : 0 : tuple = SearchSysCache1(PARAMETERACLOID, ObjectIdGetDatum(acl_oid));
3548 [ # # ]: 0 : if (!HeapTupleIsValid(tuple))
3549 [ # # ]: 0 : ereport(ERROR,
3550 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
3551 : : errmsg("parameter ACL with OID %u does not exist",
3552 : : acl_oid)));
3553 : :
3554 : 0 : aclDatum = SysCacheGetAttr(PARAMETERACLOID, tuple,
3555 : : Anum_pg_parameter_acl_paracl,
3556 : : &isNull);
3557 [ # # ]: 0 : if (isNull)
3558 : : {
3559 : : /* No ACL, so build default ACL */
3560 : 0 : acl = acldefault(OBJECT_PARAMETER_ACL, BOOTSTRAP_SUPERUSERID);
3561 : 0 : aclDatum = (Datum) 0;
3562 : : }
3563 : : else
3564 : : {
3565 : : /* detoast ACL if necessary */
3566 : 0 : acl = DatumGetAclP(aclDatum);
3567 : : }
3568 : :
3569 : 0 : result = aclmask(acl, roleid, BOOTSTRAP_SUPERUSERID, mask, how);
3570 : :
3571 : : /* if we have a detoasted copy, free it */
3572 [ # # # # ]: 0 : if (acl && acl != DatumGetPointer(aclDatum))
3573 : 0 : pfree(acl);
3574 : :
3575 : 0 : ReleaseSysCache(tuple);
3576 : :
3577 : 0 : return result;
3578 : : }
3579 : :
3580 : : /*
3581 : : * Routine for examining a user's privileges for a largeobject
3582 : : *
3583 : : * When a large object is opened for reading, it is opened relative to the
3584 : : * caller's snapshot, but when it is opened for writing, a current
3585 : : * MVCC snapshot will be used. See doc/src/sgml/lobj.sgml. This function
3586 : : * takes a snapshot argument so that the permissions check can be made
3587 : : * relative to the same snapshot that will be used to read the underlying
3588 : : * data. The caller will actually pass NULL for an instantaneous MVCC
3589 : : * snapshot, since all we do with the snapshot argument is pass it through
3590 : : * to systable_beginscan().
3591 : : */
3592 : : static AclMode
3593 : 590 : pg_largeobject_aclmask_snapshot(Oid lobj_oid, Oid roleid,
3594 : : AclMode mask, AclMaskHow how,
3595 : : Snapshot snapshot)
3596 : : {
3597 : : AclMode result;
3598 : : Relation pg_lo_meta;
3599 : : ScanKeyData entry[1];
3600 : : SysScanDesc scan;
3601 : : HeapTuple tuple;
3602 : : Datum aclDatum;
3603 : : bool isNull;
3604 : : Acl *acl;
3605 : : Oid ownerId;
3606 : :
3607 : : /* Superusers bypass all permission checking. */
3608 [ + + ]: 590 : if (superuser_arg(roleid))
3609 : 338 : return mask;
3610 : :
3611 : : /*
3612 : : * Get the largeobject's ACL from pg_largeobject_metadata
3613 : : */
3614 : 252 : pg_lo_meta = table_open(LargeObjectMetadataRelationId,
3615 : : AccessShareLock);
3616 : :
3617 : 252 : ScanKeyInit(&entry[0],
3618 : : Anum_pg_largeobject_metadata_oid,
3619 : : BTEqualStrategyNumber, F_OIDEQ,
3620 : : ObjectIdGetDatum(lobj_oid));
3621 : :
3622 : 252 : scan = systable_beginscan(pg_lo_meta,
3623 : : LargeObjectMetadataOidIndexId, true,
3624 : : snapshot, 1, entry);
3625 : :
3626 : 252 : tuple = systable_getnext(scan);
3627 [ - + ]: 252 : if (!HeapTupleIsValid(tuple))
3628 [ # # ]: 0 : ereport(ERROR,
3629 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
3630 : : errmsg("large object %u does not exist", lobj_oid)));
3631 : :
3632 : 252 : ownerId = ((Form_pg_largeobject_metadata) GETSTRUCT(tuple))->lomowner;
3633 : :
3634 : 252 : aclDatum = heap_getattr(tuple, Anum_pg_largeobject_metadata_lomacl,
3635 : : RelationGetDescr(pg_lo_meta), &isNull);
3636 : :
3637 [ + + ]: 252 : if (isNull)
3638 : : {
3639 : : /* No ACL, so build default ACL */
3640 : 48 : acl = acldefault(OBJECT_LARGEOBJECT, ownerId);
3641 : 48 : aclDatum = (Datum) 0;
3642 : : }
3643 : : else
3644 : : {
3645 : : /* detoast ACL if necessary */
3646 : 204 : acl = DatumGetAclP(aclDatum);
3647 : : }
3648 : :
3649 : 252 : result = aclmask(acl, roleid, ownerId, mask, how);
3650 : :
3651 : : /* if we have a detoasted copy, free it */
3652 [ + - + - ]: 252 : if (acl && acl != DatumGetPointer(aclDatum))
3653 : 252 : pfree(acl);
3654 : :
3655 : 252 : systable_endscan(scan);
3656 : :
3657 : 252 : table_close(pg_lo_meta, AccessShareLock);
3658 : :
3659 : : /*
3660 : : * Check if ACL_SELECT is being checked and, if so, and not set already as
3661 : : * part of the result, then check if the user has privileges of the
3662 : : * pg_read_all_data role, which allows read access to all large objects.
3663 : : */
3664 [ + + + + : 320 : if (mask & ACL_SELECT && !(result & ACL_SELECT) &&
+ + ]
3665 : 68 : has_privs_of_role(roleid, ROLE_PG_READ_ALL_DATA))
3666 : 20 : result |= ACL_SELECT;
3667 : :
3668 : : /*
3669 : : * Check if ACL_UPDATE is being checked and, if so, and not set already as
3670 : : * part of the result, then check if the user has privileges of the
3671 : : * pg_write_all_data role, which allows write access to all large objects.
3672 : : */
3673 [ + + + + : 312 : if (mask & ACL_UPDATE && !(result & ACL_UPDATE) &&
+ + ]
3674 : 60 : has_privs_of_role(roleid, ROLE_PG_WRITE_ALL_DATA))
3675 : 12 : result |= ACL_UPDATE;
3676 : :
3677 : 252 : return result;
3678 : : }
3679 : :
3680 : : /*
3681 : : * Routine for examining a user's privileges for a namespace, with is_missing
3682 : : */
3683 : : static AclMode
3684 : 647118 : pg_namespace_aclmask_ext(Oid nsp_oid, Oid roleid,
3685 : : AclMode mask, AclMaskHow how,
3686 : : bool *is_missing)
3687 : : {
3688 : : AclMode result;
3689 : : HeapTuple tuple;
3690 : : Datum aclDatum;
3691 : : bool isNull;
3692 : : Acl *acl;
3693 : : Oid ownerId;
3694 : :
3695 : : /*
3696 : : * Disallow creation in the conflict schema for everyone, including
3697 : : * superusers, unless in binary-upgrade mode.
3698 : : */
3699 [ + + + + : 763011 : if (!IsBinaryUpgrade && (mask & ACL_CREATE) &&
+ + ]
3700 : 115893 : IsConflictLogTableNamespace(nsp_oid))
3701 : 8 : mask &= ~ACL_CREATE;
3702 : :
3703 : : /* Superusers bypass all permission checking. */
3704 [ + + ]: 647118 : if (superuser_arg(roleid))
3705 : 634263 : return mask;
3706 : :
3707 : : /*
3708 : : * If we have been assigned this namespace as a temp namespace, check to
3709 : : * make sure we have CREATE TEMP permission on the database, and if so act
3710 : : * as though we have all standard (but not GRANT OPTION) permissions on
3711 : : * the namespace. If we don't have CREATE TEMP, act as though we have
3712 : : * only USAGE (and not CREATE) rights.
3713 : : *
3714 : : * This may seem redundant given the check in InitTempTableNamespace, but
3715 : : * it really isn't since current user ID may have changed since then. The
3716 : : * upshot of this behavior is that a SECURITY DEFINER function can create
3717 : : * temp tables that can then be accessed (if permission is granted) by
3718 : : * code in the same session that doesn't have permissions to create temp
3719 : : * tables.
3720 : : *
3721 : : * XXX Would it be safe to ereport a special error message as
3722 : : * InitTempTableNamespace does? Returning zero here means we'll get a
3723 : : * generic "permission denied for schema pg_temp_N" message, which is not
3724 : : * remarkably user-friendly.
3725 : : */
3726 [ + + ]: 12855 : if (isTempNamespace(nsp_oid))
3727 : : {
3728 [ + - ]: 213 : if (object_aclcheck_ext(DatabaseRelationId, MyDatabaseId, roleid,
3729 : : ACL_CREATE_TEMP, is_missing) == ACLCHECK_OK)
3730 : 213 : return mask & ACL_ALL_RIGHTS_SCHEMA;
3731 : : else
3732 : 0 : return mask & ACL_USAGE;
3733 : : }
3734 : :
3735 : : /*
3736 : : * Get the schema's ACL from pg_namespace
3737 : : */
3738 : 12642 : tuple = SearchSysCache1(NAMESPACEOID, ObjectIdGetDatum(nsp_oid));
3739 [ - + ]: 12642 : if (!HeapTupleIsValid(tuple))
3740 : : {
3741 [ # # ]: 0 : if (is_missing != NULL)
3742 : : {
3743 : : /* return "no privileges" instead of throwing an error */
3744 : 0 : *is_missing = true;
3745 : 0 : return 0;
3746 : : }
3747 : : else
3748 [ # # ]: 0 : ereport(ERROR,
3749 : : (errcode(ERRCODE_UNDEFINED_SCHEMA),
3750 : : errmsg("schema with OID %u does not exist", nsp_oid)));
3751 : : }
3752 : :
3753 : 12642 : ownerId = ((Form_pg_namespace) GETSTRUCT(tuple))->nspowner;
3754 : :
3755 : 12642 : aclDatum = SysCacheGetAttr(NAMESPACEOID, tuple, Anum_pg_namespace_nspacl,
3756 : : &isNull);
3757 [ + + ]: 12642 : if (isNull)
3758 : : {
3759 : : /* No ACL, so build default ACL */
3760 : 201 : acl = acldefault(OBJECT_SCHEMA, ownerId);
3761 : 201 : aclDatum = (Datum) 0;
3762 : : }
3763 : : else
3764 : : {
3765 : : /* detoast ACL if necessary */
3766 : 12441 : acl = DatumGetAclP(aclDatum);
3767 : : }
3768 : :
3769 : 12642 : result = aclmask(acl, roleid, ownerId, mask, how);
3770 : :
3771 : : /* if we have a detoasted copy, free it */
3772 [ + - + - ]: 12642 : if (acl && acl != DatumGetPointer(aclDatum))
3773 : 12642 : pfree(acl);
3774 : :
3775 : 12642 : ReleaseSysCache(tuple);
3776 : :
3777 : : /*
3778 : : * Check if ACL_USAGE is being checked and, if so, and not set already as
3779 : : * part of the result, then check if the user is a member of the
3780 : : * pg_read_all_data or pg_write_all_data roles, which allow usage access
3781 : : * to all schemas.
3782 : : */
3783 [ + + + + : 12680 : if (mask & ACL_USAGE && !(result & ACL_USAGE) &&
+ + ]
3784 [ + + ]: 72 : (has_privs_of_role(roleid, ROLE_PG_READ_ALL_DATA) ||
3785 : 34 : has_privs_of_role(roleid, ROLE_PG_WRITE_ALL_DATA)))
3786 : 9 : result |= ACL_USAGE;
3787 : 12642 : return result;
3788 : : }
3789 : :
3790 : : /*
3791 : : * Routine for examining a user's privileges for a type, with is_missing
3792 : : */
3793 : : static AclMode
3794 : 224387 : pg_type_aclmask_ext(Oid type_oid, Oid roleid, AclMode mask, AclMaskHow how,
3795 : : bool *is_missing)
3796 : : {
3797 : : AclMode result;
3798 : : HeapTuple tuple;
3799 : : Form_pg_type typeForm;
3800 : : Datum aclDatum;
3801 : : bool isNull;
3802 : : Acl *acl;
3803 : : Oid ownerId;
3804 : :
3805 : : /* Bypass permission checks for superusers */
3806 [ + + ]: 224387 : if (superuser_arg(roleid))
3807 : 220944 : return mask;
3808 : :
3809 : : /*
3810 : : * Must get the type's tuple from pg_type
3811 : : */
3812 : 3443 : tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid));
3813 [ - + ]: 3443 : if (!HeapTupleIsValid(tuple))
3814 : : {
3815 [ # # ]: 0 : if (is_missing != NULL)
3816 : : {
3817 : : /* return "no privileges" instead of throwing an error */
3818 : 0 : *is_missing = true;
3819 : 0 : return 0;
3820 : : }
3821 : : else
3822 [ # # ]: 0 : ereport(ERROR,
3823 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
3824 : : errmsg("type with OID %u does not exist",
3825 : : type_oid)));
3826 : : }
3827 : 3443 : typeForm = (Form_pg_type) GETSTRUCT(tuple);
3828 : :
3829 : : /*
3830 : : * "True" array types don't manage permissions of their own; consult the
3831 : : * element type instead.
3832 : : */
3833 [ + + + + ]: 3443 : if (IsTrueArrayType(typeForm))
3834 : : {
3835 : 32 : Oid elttype_oid = typeForm->typelem;
3836 : :
3837 : 32 : ReleaseSysCache(tuple);
3838 : :
3839 : 32 : tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(elttype_oid));
3840 [ - + ]: 32 : if (!HeapTupleIsValid(tuple))
3841 : : {
3842 [ # # ]: 0 : if (is_missing != NULL)
3843 : : {
3844 : : /* return "no privileges" instead of throwing an error */
3845 : 0 : *is_missing = true;
3846 : 0 : return 0;
3847 : : }
3848 : : else
3849 [ # # ]: 0 : ereport(ERROR,
3850 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
3851 : : errmsg("type with OID %u does not exist",
3852 : : elttype_oid)));
3853 : : }
3854 : 32 : typeForm = (Form_pg_type) GETSTRUCT(tuple);
3855 : : }
3856 : :
3857 : : /*
3858 : : * Likewise, multirange types don't manage their own permissions; consult
3859 : : * the associated range type. (Note we must do this after the array step
3860 : : * to get the right answer for arrays of multiranges.)
3861 : : */
3862 [ + + ]: 3443 : if (typeForm->typtype == TYPTYPE_MULTIRANGE)
3863 : : {
3864 : 8 : Oid rangetype = get_multirange_range(typeForm->oid);
3865 : :
3866 : 8 : ReleaseSysCache(tuple);
3867 : :
3868 : 8 : tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(rangetype));
3869 [ - + ]: 8 : if (!HeapTupleIsValid(tuple))
3870 : : {
3871 [ # # ]: 0 : if (is_missing != NULL)
3872 : : {
3873 : : /* return "no privileges" instead of throwing an error */
3874 : 0 : *is_missing = true;
3875 : 0 : return 0;
3876 : : }
3877 : : else
3878 [ # # ]: 0 : ereport(ERROR,
3879 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
3880 : : errmsg("type with OID %u does not exist",
3881 : : rangetype)));
3882 : : }
3883 : 8 : typeForm = (Form_pg_type) GETSTRUCT(tuple);
3884 : : }
3885 : :
3886 : : /*
3887 : : * Now get the type's owner and ACL from the tuple
3888 : : */
3889 : 3443 : ownerId = typeForm->typowner;
3890 : :
3891 : 3443 : aclDatum = SysCacheGetAttr(TYPEOID, tuple,
3892 : : Anum_pg_type_typacl, &isNull);
3893 [ + + ]: 3443 : if (isNull)
3894 : : {
3895 : : /* No ACL, so build default ACL */
3896 : 3187 : acl = acldefault(OBJECT_TYPE, ownerId);
3897 : 3187 : aclDatum = (Datum) 0;
3898 : : }
3899 : : else
3900 : : {
3901 : : /* detoast rel's ACL if necessary */
3902 : 256 : acl = DatumGetAclP(aclDatum);
3903 : : }
3904 : :
3905 : 3443 : result = aclmask(acl, roleid, ownerId, mask, how);
3906 : :
3907 : : /* if we have a detoasted copy, free it */
3908 [ + - + - ]: 3443 : if (acl && acl != DatumGetPointer(aclDatum))
3909 : 3443 : pfree(acl);
3910 : :
3911 : 3443 : ReleaseSysCache(tuple);
3912 : :
3913 : 3443 : return result;
3914 : : }
3915 : :
3916 : : /*
3917 : : * Exported generic routine for checking a user's access privileges to an object
3918 : : */
3919 : : AclResult
3920 : 2120573 : object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
3921 : : {
3922 : 2120573 : return object_aclcheck_ext(classid, objectid, roleid, mode, NULL);
3923 : : }
3924 : :
3925 : : /*
3926 : : * Exported generic routine for checking a user's access privileges to an
3927 : : * object, with is_missing
3928 : : */
3929 : : AclResult
3930 : 2120858 : object_aclcheck_ext(Oid classid, Oid objectid,
3931 : : Oid roleid, AclMode mode,
3932 : : bool *is_missing)
3933 : : {
3934 [ + + ]: 2120858 : if (object_aclmask_ext(classid, objectid, roleid, mode, ACLMASK_ANY,
3935 : : is_missing) != 0)
3936 : 2120385 : return ACLCHECK_OK;
3937 : : else
3938 : 473 : return ACLCHECK_NO_PRIV;
3939 : : }
3940 : :
3941 : : /*
3942 : : * Exported routine for checking a user's access privileges to a column
3943 : : *
3944 : : * Returns ACLCHECK_OK if the user has any of the privileges identified by
3945 : : * 'mode'; otherwise returns a suitable error code (in practice, always
3946 : : * ACLCHECK_NO_PRIV).
3947 : : *
3948 : : * As with pg_attribute_aclmask, only privileges granted directly on the
3949 : : * column are considered here.
3950 : : */
3951 : : AclResult
3952 : 2957 : pg_attribute_aclcheck(Oid table_oid, AttrNumber attnum,
3953 : : Oid roleid, AclMode mode)
3954 : : {
3955 : 2957 : return pg_attribute_aclcheck_ext(table_oid, attnum, roleid, mode, NULL);
3956 : : }
3957 : :
3958 : :
3959 : : /*
3960 : : * Exported routine for checking a user's access privileges to a column,
3961 : : * with is_missing
3962 : : */
3963 : : AclResult
3964 : 391166 : pg_attribute_aclcheck_ext(Oid table_oid, AttrNumber attnum,
3965 : : Oid roleid, AclMode mode, bool *is_missing)
3966 : : {
3967 [ + + ]: 391166 : if (pg_attribute_aclmask_ext(table_oid, attnum, roleid, mode,
3968 : : ACLMASK_ANY, is_missing) != 0)
3969 : 6227 : return ACLCHECK_OK;
3970 : : else
3971 : 384939 : return ACLCHECK_NO_PRIV;
3972 : : }
3973 : :
3974 : : /*
3975 : : * Exported routine for checking a user's access privileges to any/all columns
3976 : : *
3977 : : * If 'how' is ACLMASK_ANY, then returns ACLCHECK_OK if user has any of the
3978 : : * privileges identified by 'mode' on any non-dropped column in the relation;
3979 : : * otherwise returns a suitable error code (in practice, always
3980 : : * ACLCHECK_NO_PRIV).
3981 : : *
3982 : : * If 'how' is ACLMASK_ALL, then returns ACLCHECK_OK if user has any of the
3983 : : * privileges identified by 'mode' on each non-dropped column in the relation
3984 : : * (and there must be at least one such column); otherwise returns a suitable
3985 : : * error code (in practice, always ACLCHECK_NO_PRIV).
3986 : : *
3987 : : * As with pg_attribute_aclmask, only privileges granted directly on the
3988 : : * column(s) are considered here.
3989 : : *
3990 : : * Note: system columns are not considered here; there are cases where that
3991 : : * might be appropriate but there are also cases where it wouldn't.
3992 : : */
3993 : : AclResult
3994 : 162 : pg_attribute_aclcheck_all(Oid table_oid, Oid roleid, AclMode mode,
3995 : : AclMaskHow how)
3996 : : {
3997 : 162 : return pg_attribute_aclcheck_all_ext(table_oid, roleid, mode, how, NULL);
3998 : : }
3999 : :
4000 : : /*
4001 : : * Exported routine for checking a user's access privileges to any/all columns,
4002 : : * with is_missing
4003 : : */
4004 : : AclResult
4005 : 162 : pg_attribute_aclcheck_all_ext(Oid table_oid, Oid roleid,
4006 : : AclMode mode, AclMaskHow how,
4007 : : bool *is_missing)
4008 : : {
4009 : : AclResult result;
4010 : : HeapTuple classTuple;
4011 : : Form_pg_class classForm;
4012 : : Oid ownerId;
4013 : : AttrNumber nattrs;
4014 : : AttrNumber curr_att;
4015 : :
4016 : : /*
4017 : : * Must fetch pg_class row to get owner ID and number of attributes.
4018 : : */
4019 : 162 : classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
4020 [ - + ]: 162 : if (!HeapTupleIsValid(classTuple))
4021 : : {
4022 [ # # ]: 0 : if (is_missing != NULL)
4023 : : {
4024 : : /* return "no privileges" instead of throwing an error */
4025 : 0 : *is_missing = true;
4026 : 0 : return ACLCHECK_NO_PRIV;
4027 : : }
4028 : : else
4029 [ # # ]: 0 : ereport(ERROR,
4030 : : (errcode(ERRCODE_UNDEFINED_TABLE),
4031 : : errmsg("relation with OID %u does not exist",
4032 : : table_oid)));
4033 : : }
4034 : 162 : classForm = (Form_pg_class) GETSTRUCT(classTuple);
4035 : :
4036 : 162 : ownerId = classForm->relowner;
4037 : 162 : nattrs = classForm->relnatts;
4038 : :
4039 : 162 : ReleaseSysCache(classTuple);
4040 : :
4041 : : /*
4042 : : * Initialize result in case there are no non-dropped columns. We want to
4043 : : * report failure in such cases for either value of 'how'.
4044 : : */
4045 : 162 : result = ACLCHECK_NO_PRIV;
4046 : :
4047 [ + + ]: 386 : for (curr_att = 1; curr_att <= nattrs; curr_att++)
4048 : : {
4049 : : HeapTuple attTuple;
4050 : : Datum aclDatum;
4051 : : bool isNull;
4052 : : Acl *acl;
4053 : : AclMode attmask;
4054 : :
4055 : 310 : attTuple = SearchSysCache2(ATTNUM,
4056 : : ObjectIdGetDatum(table_oid),
4057 : : Int16GetDatum(curr_att));
4058 : :
4059 : : /*
4060 : : * Lookup failure probably indicates that the table was just dropped,
4061 : : * but we'll treat it the same as a dropped column rather than
4062 : : * throwing error.
4063 : : */
4064 [ - + ]: 310 : if (!HeapTupleIsValid(attTuple))
4065 : 12 : continue;
4066 : :
4067 : : /* ignore dropped columns */
4068 [ + + ]: 310 : if (((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped)
4069 : : {
4070 : 12 : ReleaseSysCache(attTuple);
4071 : 12 : continue;
4072 : : }
4073 : :
4074 : 298 : aclDatum = SysCacheGetAttr(ATTNUM, attTuple, Anum_pg_attribute_attacl,
4075 : : &isNull);
4076 : :
4077 : : /*
4078 : : * Here we hard-wire knowledge that the default ACL for a column
4079 : : * grants no privileges, so that we can fall out quickly in the very
4080 : : * common case where attacl is null.
4081 : : */
4082 [ + + ]: 298 : if (isNull)
4083 : 121 : attmask = 0;
4084 : : else
4085 : : {
4086 : : /* detoast column's ACL if necessary */
4087 : 177 : acl = DatumGetAclP(aclDatum);
4088 : :
4089 : 177 : attmask = aclmask(acl, roleid, ownerId, mode, ACLMASK_ANY);
4090 : :
4091 : : /* if we have a detoasted copy, free it */
4092 [ + - ]: 177 : if (acl != DatumGetPointer(aclDatum))
4093 : 177 : pfree(acl);
4094 : : }
4095 : :
4096 : 298 : ReleaseSysCache(attTuple);
4097 : :
4098 [ + + ]: 298 : if (attmask != 0)
4099 : : {
4100 : 117 : result = ACLCHECK_OK;
4101 [ + + ]: 117 : if (how == ACLMASK_ANY)
4102 : 86 : break; /* succeed on any success */
4103 : : }
4104 : : else
4105 : : {
4106 : 181 : result = ACLCHECK_NO_PRIV;
4107 [ + + ]: 181 : if (how == ACLMASK_ALL)
4108 : 33 : break; /* fail on any failure */
4109 : : }
4110 : : }
4111 : :
4112 : 162 : return result;
4113 : : }
4114 : :
4115 : : /*
4116 : : * Exported routine for checking a user's access privileges to a table
4117 : : *
4118 : : * Returns ACLCHECK_OK if the user has any of the privileges identified by
4119 : : * 'mode'; otherwise returns a suitable error code (in practice, always
4120 : : * ACLCHECK_NO_PRIV).
4121 : : */
4122 : : AclResult
4123 : 1641227 : pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
4124 : : {
4125 : 1641227 : return pg_class_aclcheck_ext(table_oid, roleid, mode, NULL);
4126 : : }
4127 : :
4128 : : /*
4129 : : * Exported routine for checking a user's access privileges to a table,
4130 : : * with is_missing
4131 : : */
4132 : : AclResult
4133 : 2053996 : pg_class_aclcheck_ext(Oid table_oid, Oid roleid,
4134 : : AclMode mode, bool *is_missing)
4135 : : {
4136 [ + + ]: 2053996 : if (pg_class_aclmask_ext(table_oid, roleid, mode,
4137 : : ACLMASK_ANY, is_missing) != 0)
4138 : 2045178 : return ACLCHECK_OK;
4139 : : else
4140 : 8818 : return ACLCHECK_NO_PRIV;
4141 : : }
4142 : :
4143 : : /*
4144 : : * Exported routine for checking a user's access privileges to a configuration
4145 : : * parameter (GUC), identified by GUC name.
4146 : : */
4147 : : AclResult
4148 : 80 : pg_parameter_aclcheck(const char *name, Oid roleid, AclMode mode)
4149 : : {
4150 [ + + ]: 80 : if (pg_parameter_aclmask(name, roleid, mode, ACLMASK_ANY) != 0)
4151 : 34 : return ACLCHECK_OK;
4152 : : else
4153 : 46 : return ACLCHECK_NO_PRIV;
4154 : : }
4155 : :
4156 : : /*
4157 : : * Exported routine for checking a user's access privileges to a largeobject
4158 : : */
4159 : : AclResult
4160 : 590 : pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode,
4161 : : Snapshot snapshot)
4162 : : {
4163 [ + + ]: 590 : if (pg_largeobject_aclmask_snapshot(lobj_oid, roleid, mode,
4164 : : ACLMASK_ANY, snapshot) != 0)
4165 : 494 : return ACLCHECK_OK;
4166 : : else
4167 : 96 : return ACLCHECK_NO_PRIV;
4168 : : }
4169 : :
4170 : : /*
4171 : : * Generic ownership check for an object
4172 : : */
4173 : : bool
4174 : 288853 : object_ownercheck(Oid classid, Oid objectid, Oid roleid)
4175 : : {
4176 : : SysCacheIdentifier cacheid;
4177 : : Oid ownerId;
4178 : :
4179 : : /* Superusers bypass all permission checking. */
4180 [ + + ]: 288853 : if (superuser_arg(roleid))
4181 : 280312 : return true;
4182 : :
4183 : : /* For large objects, the catalog to consult is pg_largeobject_metadata */
4184 [ + + ]: 8541 : if (classid == LargeObjectRelationId)
4185 : 24 : classid = LargeObjectMetadataRelationId;
4186 : :
4187 : 8541 : cacheid = get_object_catcache_oid(classid);
4188 [ + + ]: 8541 : if (cacheid != SYSCACHEID_INVALID)
4189 : : {
4190 : : /* we can get the object's tuple from the syscache */
4191 : : HeapTuple tuple;
4192 : :
4193 : 8515 : tuple = SearchSysCache1(cacheid, ObjectIdGetDatum(objectid));
4194 [ - + ]: 8515 : if (!HeapTupleIsValid(tuple))
4195 [ # # ]: 0 : elog(ERROR, "cache lookup failed for %s %u",
4196 : : get_object_class_descr(classid), objectid);
4197 : :
4198 : 8515 : ownerId = DatumGetObjectId(SysCacheGetAttrNotNull(cacheid,
4199 : : tuple,
4200 : 8515 : get_object_attnum_owner(classid)));
4201 : 8515 : ReleaseSysCache(tuple);
4202 : : }
4203 : : else
4204 : : {
4205 : : /* for catalogs without an appropriate syscache */
4206 : : Relation rel;
4207 : : ScanKeyData entry[1];
4208 : : SysScanDesc scan;
4209 : : HeapTuple tuple;
4210 : : bool isnull;
4211 : :
4212 : 26 : rel = table_open(classid, AccessShareLock);
4213 : :
4214 : 52 : ScanKeyInit(&entry[0],
4215 : 26 : get_object_attnum_oid(classid),
4216 : : BTEqualStrategyNumber, F_OIDEQ,
4217 : : ObjectIdGetDatum(objectid));
4218 : :
4219 : 26 : scan = systable_beginscan(rel,
4220 : : get_object_oid_index(classid), true,
4221 : : NULL, 1, entry);
4222 : :
4223 : 26 : tuple = systable_getnext(scan);
4224 [ - + ]: 26 : if (!HeapTupleIsValid(tuple))
4225 [ # # ]: 0 : elog(ERROR, "could not find tuple for %s %u",
4226 : : get_object_class_descr(classid), objectid);
4227 : :
4228 : 26 : ownerId = DatumGetObjectId(heap_getattr(tuple,
4229 : 26 : get_object_attnum_owner(classid),
4230 : : RelationGetDescr(rel),
4231 : : &isnull));
4232 : : Assert(!isnull);
4233 : :
4234 : 26 : systable_endscan(scan);
4235 : 26 : table_close(rel, AccessShareLock);
4236 : : }
4237 : :
4238 : 8541 : return has_privs_of_role(roleid, ownerId);
4239 : : }
4240 : :
4241 : : /*
4242 : : * Check whether specified role has CREATEROLE privilege (or is a superuser)
4243 : : *
4244 : : * Note: roles do not have owners per se; instead we use this test in
4245 : : * places where an ownership-like permissions test is needed for a role.
4246 : : * Be sure to apply it to the role trying to do the operation, not the
4247 : : * role being operated on! Also note that this generally should not be
4248 : : * considered enough privilege if the target role is a superuser.
4249 : : * (We don't handle that consideration here because we want to give a
4250 : : * separate error message for such cases, so the caller has to deal with it.)
4251 : : */
4252 : : bool
4253 : 1749 : has_createrole_privilege(Oid roleid)
4254 : : {
4255 : 1749 : bool result = false;
4256 : : HeapTuple utup;
4257 : :
4258 : : /* Superusers bypass all permission checking. */
4259 [ + + ]: 1749 : if (superuser_arg(roleid))
4260 : 1383 : return true;
4261 : :
4262 : 366 : utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
4263 [ + - ]: 366 : if (HeapTupleIsValid(utup))
4264 : : {
4265 : 366 : result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreaterole;
4266 : 366 : ReleaseSysCache(utup);
4267 : : }
4268 : 366 : return result;
4269 : : }
4270 : :
4271 : : bool
4272 : 3863 : has_bypassrls_privilege(Oid roleid)
4273 : : {
4274 : 3863 : bool result = false;
4275 : : HeapTuple utup;
4276 : :
4277 : : /* Superusers bypass all permission checking. */
4278 [ + + ]: 3863 : if (superuser_arg(roleid))
4279 : 1201 : return true;
4280 : :
4281 : 2662 : utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
4282 [ + - ]: 2662 : if (HeapTupleIsValid(utup))
4283 : : {
4284 : 2662 : result = ((Form_pg_authid) GETSTRUCT(utup))->rolbypassrls;
4285 : 2662 : ReleaseSysCache(utup);
4286 : : }
4287 : 2662 : return result;
4288 : : }
4289 : :
4290 : : /*
4291 : : * Fetch pg_default_acl entry for given role, namespace and object type
4292 : : * (object type must be given in pg_default_acl's encoding).
4293 : : * Returns NULL if no such entry.
4294 : : */
4295 : : static Acl *
4296 : 111520 : get_default_acl_internal(Oid roleId, Oid nsp_oid, char objtype)
4297 : : {
4298 : 111520 : Acl *result = NULL;
4299 : : HeapTuple tuple;
4300 : :
4301 : 111520 : tuple = SearchSysCache3(DEFACLROLENSPOBJ,
4302 : : ObjectIdGetDatum(roleId),
4303 : : ObjectIdGetDatum(nsp_oid),
4304 : : CharGetDatum(objtype));
4305 : :
4306 [ + + ]: 111520 : if (HeapTupleIsValid(tuple))
4307 : : {
4308 : : Datum aclDatum;
4309 : : bool isNull;
4310 : :
4311 : 176 : aclDatum = SysCacheGetAttr(DEFACLROLENSPOBJ, tuple,
4312 : : Anum_pg_default_acl_defaclacl,
4313 : : &isNull);
4314 [ + - ]: 176 : if (!isNull)
4315 : 176 : result = DatumGetAclPCopy(aclDatum);
4316 : 176 : ReleaseSysCache(tuple);
4317 : : }
4318 : :
4319 : 111520 : return result;
4320 : : }
4321 : :
4322 : : /*
4323 : : * Get default permissions for newly created object within given schema
4324 : : *
4325 : : * Returns NULL if built-in system defaults should be used.
4326 : : *
4327 : : * If the result is not NULL, caller must call recordDependencyOnNewAcl
4328 : : * once the OID of the new object is known.
4329 : : */
4330 : : Acl *
4331 : 55760 : get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
4332 : : {
4333 : : Acl *result;
4334 : : Acl *glob_acl;
4335 : : Acl *schema_acl;
4336 : : Acl *def_acl;
4337 : : char defaclobjtype;
4338 : :
4339 : : /*
4340 : : * Use NULL during bootstrap, since pg_default_acl probably isn't there
4341 : : * yet.
4342 : : */
4343 [ - + ]: 55760 : if (IsBootstrapProcessingMode())
4344 : 0 : return NULL;
4345 : :
4346 : : /* Check if object type is supported in pg_default_acl */
4347 [ + + + + : 55760 : switch (objtype)
+ + - ]
4348 : : {
4349 : 38890 : case OBJECT_TABLE:
4350 : 38890 : defaclobjtype = DEFACLOBJ_RELATION;
4351 : 38890 : break;
4352 : :
4353 : 1213 : case OBJECT_SEQUENCE:
4354 : 1213 : defaclobjtype = DEFACLOBJ_SEQUENCE;
4355 : 1213 : break;
4356 : :
4357 : 10926 : case OBJECT_FUNCTION:
4358 : 10926 : defaclobjtype = DEFACLOBJ_FUNCTION;
4359 : 10926 : break;
4360 : :
4361 : 3877 : case OBJECT_TYPE:
4362 : 3877 : defaclobjtype = DEFACLOBJ_TYPE;
4363 : 3877 : break;
4364 : :
4365 : 750 : case OBJECT_SCHEMA:
4366 : 750 : defaclobjtype = DEFACLOBJ_NAMESPACE;
4367 : 750 : break;
4368 : :
4369 : 104 : case OBJECT_LARGEOBJECT:
4370 : 104 : defaclobjtype = DEFACLOBJ_LARGEOBJECT;
4371 : 104 : break;
4372 : :
4373 : 0 : default:
4374 : 0 : return NULL;
4375 : : }
4376 : :
4377 : : /* Look up the relevant pg_default_acl entries */
4378 : 55760 : glob_acl = get_default_acl_internal(ownerId, InvalidOid, defaclobjtype);
4379 : 55760 : schema_acl = get_default_acl_internal(ownerId, nsp_oid, defaclobjtype);
4380 : :
4381 : : /* Quick out if neither entry exists */
4382 [ + + + + ]: 55760 : if (glob_acl == NULL && schema_acl == NULL)
4383 : 55620 : return NULL;
4384 : :
4385 : : /* We need to know the hard-wired default value, too */
4386 : 140 : def_acl = acldefault(objtype, ownerId);
4387 : :
4388 : : /* If there's no global entry, substitute the hard-wired default */
4389 [ + + ]: 140 : if (glob_acl == NULL)
4390 : 12 : glob_acl = def_acl;
4391 : :
4392 : : /* Merge in any per-schema privileges */
4393 : 140 : result = aclmerge(glob_acl, schema_acl, ownerId);
4394 : :
4395 : : /*
4396 : : * For efficiency, we want to return NULL if the result equals default.
4397 : : * This requires sorting both arrays to get an accurate comparison.
4398 : : */
4399 : 140 : aclitemsort(result);
4400 : 140 : aclitemsort(def_acl);
4401 [ + + ]: 140 : if (aclequal(result, def_acl))
4402 : 16 : result = NULL;
4403 : :
4404 : 140 : return result;
4405 : : }
4406 : :
4407 : : /*
4408 : : * Record dependencies on roles mentioned in a new object's ACL.
4409 : : */
4410 : : void
4411 : 58357 : recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId,
4412 : : Oid ownerId, Acl *acl)
4413 : : {
4414 : : int nmembers;
4415 : : Oid *members;
4416 : :
4417 : : /* Nothing to do if ACL is defaulted */
4418 [ + + ]: 58357 : if (acl == NULL)
4419 : 58233 : return;
4420 : :
4421 : : /* Extract roles mentioned in ACL */
4422 : 124 : nmembers = aclmembers(acl, &members);
4423 : :
4424 : : /* Update the shared dependency ACL info */
4425 : 124 : updateAclDependencies(classId, objectId, objsubId,
4426 : : ownerId,
4427 : : 0, NULL,
4428 : : nmembers, members);
4429 : : }
4430 : :
4431 : : /*
4432 : : * Record initial privileges for the top-level object passed in.
4433 : : *
4434 : : * For the object passed in, this will record its ACL (if any) and the ACLs of
4435 : : * any sub-objects (eg: columns) into pg_init_privs.
4436 : : */
4437 : : void
4438 : 53 : recordExtObjInitPriv(Oid objoid, Oid classoid)
4439 : : {
4440 : : /*
4441 : : * pg_class / pg_attribute
4442 : : *
4443 : : * If this is a relation then we need to see if there are any sub-objects
4444 : : * (eg: columns) for it and, if so, be sure to call
4445 : : * recordExtensionInitPrivWorker() for each one.
4446 : : */
4447 [ + + ]: 53 : if (classoid == RelationRelationId)
4448 : : {
4449 : : Form_pg_class pg_class_tuple;
4450 : : Datum aclDatum;
4451 : : bool isNull;
4452 : : HeapTuple tuple;
4453 : :
4454 : 8 : tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(objoid));
4455 [ - + ]: 8 : if (!HeapTupleIsValid(tuple))
4456 [ # # ]: 0 : elog(ERROR, "cache lookup failed for relation %u", objoid);
4457 : 8 : pg_class_tuple = (Form_pg_class) GETSTRUCT(tuple);
4458 : :
4459 : : /*
4460 : : * Indexes don't have permissions, neither do the pg_class rows for
4461 : : * composite types. (These cases are unreachable given the
4462 : : * restrictions in ALTER EXTENSION ADD, but let's check anyway.)
4463 : : */
4464 [ + - ]: 8 : if (pg_class_tuple->relkind == RELKIND_INDEX ||
4465 [ + - ]: 8 : pg_class_tuple->relkind == RELKIND_PARTITIONED_INDEX ||
4466 [ - + ]: 8 : pg_class_tuple->relkind == RELKIND_COMPOSITE_TYPE)
4467 : : {
4468 : 0 : ReleaseSysCache(tuple);
4469 : 0 : return;
4470 : : }
4471 : :
4472 : : /*
4473 : : * If this isn't a sequence then it's possibly going to have
4474 : : * column-level ACLs associated with it.
4475 : : */
4476 [ + + ]: 8 : if (pg_class_tuple->relkind != RELKIND_SEQUENCE)
4477 : : {
4478 : : AttrNumber curr_att;
4479 : 7 : AttrNumber nattrs = pg_class_tuple->relnatts;
4480 : :
4481 [ + + ]: 19 : for (curr_att = 1; curr_att <= nattrs; curr_att++)
4482 : : {
4483 : : HeapTuple attTuple;
4484 : : Datum attaclDatum;
4485 : :
4486 : 12 : attTuple = SearchSysCache2(ATTNUM,
4487 : : ObjectIdGetDatum(objoid),
4488 : : Int16GetDatum(curr_att));
4489 : :
4490 [ - + ]: 12 : if (!HeapTupleIsValid(attTuple))
4491 : 0 : continue;
4492 : :
4493 : : /* ignore dropped columns */
4494 [ + + ]: 12 : if (((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped)
4495 : : {
4496 : 1 : ReleaseSysCache(attTuple);
4497 : 1 : continue;
4498 : : }
4499 : :
4500 : 11 : attaclDatum = SysCacheGetAttr(ATTNUM, attTuple,
4501 : : Anum_pg_attribute_attacl,
4502 : : &isNull);
4503 : :
4504 : : /* no need to do anything for a NULL ACL */
4505 [ + + ]: 11 : if (isNull)
4506 : : {
4507 : 9 : ReleaseSysCache(attTuple);
4508 : 9 : continue;
4509 : : }
4510 : :
4511 : 2 : recordExtensionInitPrivWorker(objoid, classoid, curr_att,
4512 : 2 : DatumGetAclP(attaclDatum));
4513 : :
4514 : 2 : ReleaseSysCache(attTuple);
4515 : : }
4516 : : }
4517 : :
4518 : 8 : aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
4519 : : &isNull);
4520 : :
4521 : : /* Add the record, if any, for the top-level object */
4522 [ + + ]: 8 : if (!isNull)
4523 : 4 : recordExtensionInitPrivWorker(objoid, classoid, 0,
4524 : 4 : DatumGetAclP(aclDatum));
4525 : :
4526 : 8 : ReleaseSysCache(tuple);
4527 : : }
4528 [ - + ]: 45 : else if (classoid == LargeObjectRelationId)
4529 : : {
4530 : : /* For large objects, we must consult pg_largeobject_metadata */
4531 : : Datum aclDatum;
4532 : : bool isNull;
4533 : : HeapTuple tuple;
4534 : : ScanKeyData entry[1];
4535 : : SysScanDesc scan;
4536 : : Relation relation;
4537 : :
4538 : : /*
4539 : : * Note: this is dead code, given that we don't allow large objects to
4540 : : * be made extension members. But it seems worth carrying in case
4541 : : * some future caller of this function has need for it.
4542 : : */
4543 : 0 : relation = table_open(LargeObjectMetadataRelationId, RowExclusiveLock);
4544 : :
4545 : : /* There's no syscache for pg_largeobject_metadata */
4546 : 0 : ScanKeyInit(&entry[0],
4547 : : Anum_pg_largeobject_metadata_oid,
4548 : : BTEqualStrategyNumber, F_OIDEQ,
4549 : : ObjectIdGetDatum(objoid));
4550 : :
4551 : 0 : scan = systable_beginscan(relation,
4552 : : LargeObjectMetadataOidIndexId, true,
4553 : : NULL, 1, entry);
4554 : :
4555 : 0 : tuple = systable_getnext(scan);
4556 [ # # ]: 0 : if (!HeapTupleIsValid(tuple))
4557 [ # # ]: 0 : elog(ERROR, "could not find tuple for large object %u", objoid);
4558 : :
4559 : 0 : aclDatum = heap_getattr(tuple,
4560 : : Anum_pg_largeobject_metadata_lomacl,
4561 : : RelationGetDescr(relation), &isNull);
4562 : :
4563 : : /* Add the record, if any, for the top-level object */
4564 [ # # ]: 0 : if (!isNull)
4565 : 0 : recordExtensionInitPrivWorker(objoid, classoid, 0,
4566 : 0 : DatumGetAclP(aclDatum));
4567 : :
4568 : 0 : systable_endscan(scan);
4569 : : }
4570 : : /* This will error on unsupported classoid. */
4571 [ + + ]: 45 : else if (get_object_attnum_acl(classoid) != InvalidAttrNumber)
4572 : : {
4573 : : SysCacheIdentifier cacheid;
4574 : : Datum aclDatum;
4575 : : bool isNull;
4576 : : HeapTuple tuple;
4577 : :
4578 : 34 : cacheid = get_object_catcache_oid(classoid);
4579 : 34 : tuple = SearchSysCache1(cacheid, ObjectIdGetDatum(objoid));
4580 [ - + ]: 34 : if (!HeapTupleIsValid(tuple))
4581 [ # # ]: 0 : elog(ERROR, "cache lookup failed for %s %u",
4582 : : get_object_class_descr(classoid), objoid);
4583 : :
4584 : 34 : aclDatum = SysCacheGetAttr(cacheid, tuple,
4585 : 34 : get_object_attnum_acl(classoid),
4586 : : &isNull);
4587 : :
4588 : : /* Add the record, if any, for the top-level object */
4589 [ + + ]: 34 : if (!isNull)
4590 : 5 : recordExtensionInitPrivWorker(objoid, classoid, 0,
4591 : 5 : DatumGetAclP(aclDatum));
4592 : :
4593 : 34 : ReleaseSysCache(tuple);
4594 : : }
4595 : : }
4596 : :
4597 : : /*
4598 : : * For the object passed in, remove its ACL and the ACLs of any object subIds
4599 : : * from pg_init_privs (via recordExtensionInitPrivWorker()).
4600 : : */
4601 : : void
4602 : 159 : removeExtObjInitPriv(Oid objoid, Oid classoid)
4603 : : {
4604 : : /*
4605 : : * If this is a relation then we need to see if there are any sub-objects
4606 : : * (eg: columns) for it and, if so, be sure to call
4607 : : * recordExtensionInitPrivWorker() for each one.
4608 : : */
4609 [ + + ]: 159 : if (classoid == RelationRelationId)
4610 : : {
4611 : : Form_pg_class pg_class_tuple;
4612 : : HeapTuple tuple;
4613 : :
4614 : 30 : tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(objoid));
4615 [ - + ]: 30 : if (!HeapTupleIsValid(tuple))
4616 [ # # ]: 0 : elog(ERROR, "cache lookup failed for relation %u", objoid);
4617 : 30 : pg_class_tuple = (Form_pg_class) GETSTRUCT(tuple);
4618 : :
4619 : : /*
4620 : : * Indexes don't have permissions, neither do the pg_class rows for
4621 : : * composite types. (These cases are unreachable given the
4622 : : * restrictions in ALTER EXTENSION DROP, but let's check anyway.)
4623 : : */
4624 [ + - ]: 30 : if (pg_class_tuple->relkind == RELKIND_INDEX ||
4625 [ + - ]: 30 : pg_class_tuple->relkind == RELKIND_PARTITIONED_INDEX ||
4626 [ - + ]: 30 : pg_class_tuple->relkind == RELKIND_COMPOSITE_TYPE)
4627 : : {
4628 : 0 : ReleaseSysCache(tuple);
4629 : 0 : return;
4630 : : }
4631 : :
4632 : : /*
4633 : : * If this isn't a sequence then it's possibly going to have
4634 : : * column-level ACLs associated with it.
4635 : : */
4636 [ + - ]: 30 : if (pg_class_tuple->relkind != RELKIND_SEQUENCE)
4637 : : {
4638 : : AttrNumber curr_att;
4639 : 30 : AttrNumber nattrs = pg_class_tuple->relnatts;
4640 : :
4641 [ + + ]: 984 : for (curr_att = 1; curr_att <= nattrs; curr_att++)
4642 : : {
4643 : : HeapTuple attTuple;
4644 : :
4645 : 954 : attTuple = SearchSysCache2(ATTNUM,
4646 : : ObjectIdGetDatum(objoid),
4647 : : Int16GetDatum(curr_att));
4648 : :
4649 [ - + ]: 954 : if (!HeapTupleIsValid(attTuple))
4650 : 0 : continue;
4651 : :
4652 : : /* when removing, remove all entries, even dropped columns */
4653 : :
4654 : 954 : recordExtensionInitPrivWorker(objoid, classoid, curr_att, NULL);
4655 : :
4656 : 954 : ReleaseSysCache(attTuple);
4657 : : }
4658 : : }
4659 : :
4660 : 30 : ReleaseSysCache(tuple);
4661 : : }
4662 : :
4663 : : /* Remove the record, if any, for the top-level object */
4664 : 159 : recordExtensionInitPrivWorker(objoid, classoid, 0, NULL);
4665 : : }
4666 : :
4667 : : /*
4668 : : * Record initial ACL for an extension object
4669 : : *
4670 : : * Can be called at any time, we check if 'creating_extension' is set and, if
4671 : : * not, exit immediately.
4672 : : *
4673 : : * Pass in the object OID, the OID of the class (the OID of the table which
4674 : : * the object is defined in) and the 'sub' id of the object (objsubid), if
4675 : : * any. If there is no 'sub' id (they are currently only used for columns of
4676 : : * tables) then pass in '0'. Finally, pass in the complete ACL to store.
4677 : : *
4678 : : * If an ACL already exists for this object/sub-object then we will replace
4679 : : * it with what is passed in.
4680 : : *
4681 : : * Passing in NULL for 'new_acl' will result in the entry for the object being
4682 : : * removed, if one is found.
4683 : : */
4684 : : static void
4685 : 14578 : recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
4686 : : {
4687 : : /*
4688 : : * Generally, we only record the initial privileges when an extension is
4689 : : * being created, but because we don't actually use CREATE EXTENSION
4690 : : * during binary upgrades with pg_upgrade, there is a variable to let us
4691 : : * know that the GRANT and REVOKE statements being issued, while this
4692 : : * variable is true, are for the initial privileges of the extension
4693 : : * object and therefore we need to record them.
4694 : : */
4695 [ + + + - ]: 14578 : if (!creating_extension && !binary_upgrade_record_init_privs)
4696 : 14139 : return;
4697 : :
4698 : 439 : recordExtensionInitPrivWorker(objoid, classoid, objsubid, new_acl);
4699 : : }
4700 : :
4701 : : /*
4702 : : * Record initial ACL for an extension object, worker.
4703 : : *
4704 : : * This will perform a wholesale replacement of the entire ACL for the object
4705 : : * passed in, therefore be sure to pass in the complete new ACL to use.
4706 : : *
4707 : : * Generally speaking, do *not* use this function directly but instead use
4708 : : * recordExtensionInitPriv(), which checks if 'creating_extension' is set.
4709 : : * This function does *not* check if 'creating_extension' is set as it is also
4710 : : * used when an object is added to or removed from an extension via ALTER
4711 : : * EXTENSION ... ADD/DROP.
4712 : : */
4713 : : static void
4714 : 1563 : recordExtensionInitPrivWorker(Oid objoid, Oid classoid, int objsubid,
4715 : : Acl *new_acl)
4716 : : {
4717 : : Relation relation;
4718 : : ScanKeyData key[3];
4719 : : SysScanDesc scan;
4720 : : HeapTuple tuple;
4721 : : HeapTuple oldtuple;
4722 : : int noldmembers;
4723 : : int nnewmembers;
4724 : : Oid *oldmembers;
4725 : : Oid *newmembers;
4726 : :
4727 : : /* We'll need the role membership of the new ACL. */
4728 : 1563 : nnewmembers = aclmembers(new_acl, &newmembers);
4729 : :
4730 : : /* Search pg_init_privs for an existing entry. */
4731 : 1563 : relation = table_open(InitPrivsRelationId, RowExclusiveLock);
4732 : :
4733 : 1563 : ScanKeyInit(&key[0],
4734 : : Anum_pg_init_privs_objoid,
4735 : : BTEqualStrategyNumber, F_OIDEQ,
4736 : : ObjectIdGetDatum(objoid));
4737 : 1563 : ScanKeyInit(&key[1],
4738 : : Anum_pg_init_privs_classoid,
4739 : : BTEqualStrategyNumber, F_OIDEQ,
4740 : : ObjectIdGetDatum(classoid));
4741 : 1563 : ScanKeyInit(&key[2],
4742 : : Anum_pg_init_privs_objsubid,
4743 : : BTEqualStrategyNumber, F_INT4EQ,
4744 : : Int32GetDatum(objsubid));
4745 : :
4746 : 1563 : scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
4747 : : NULL, 3, key);
4748 : :
4749 : : /* There should exist only one entry or none. */
4750 : 1563 : oldtuple = systable_getnext(scan);
4751 : :
4752 : : /* If we find an entry, update it with the latest ACL. */
4753 [ + + ]: 1563 : if (HeapTupleIsValid(oldtuple))
4754 : : {
4755 : 139 : Datum values[Natts_pg_init_privs] = {0};
4756 : 139 : bool nulls[Natts_pg_init_privs] = {0};
4757 : 139 : bool replace[Natts_pg_init_privs] = {0};
4758 : : Datum oldAclDatum;
4759 : : bool isNull;
4760 : : Acl *old_acl;
4761 : :
4762 : : /* Update pg_shdepend for roles mentioned in the old/new ACLs. */
4763 : 139 : oldAclDatum = heap_getattr(oldtuple, Anum_pg_init_privs_initprivs,
4764 : : RelationGetDescr(relation), &isNull);
4765 : : Assert(!isNull);
4766 : 139 : old_acl = DatumGetAclP(oldAclDatum);
4767 : 139 : noldmembers = aclmembers(old_acl, &oldmembers);
4768 : :
4769 : 139 : updateInitAclDependencies(classoid, objoid, objsubid,
4770 : : noldmembers, oldmembers,
4771 : : nnewmembers, newmembers);
4772 : :
4773 : : /* If we have a new ACL to set, then update the row with it. */
4774 [ + + + - ]: 139 : if (new_acl && ACL_NUM(new_acl) != 0)
4775 : : {
4776 : 96 : values[Anum_pg_init_privs_initprivs - 1] = PointerGetDatum(new_acl);
4777 : 96 : replace[Anum_pg_init_privs_initprivs - 1] = true;
4778 : :
4779 : 96 : oldtuple = heap_modify_tuple(oldtuple, RelationGetDescr(relation),
4780 : : values, nulls, replace);
4781 : :
4782 : 96 : CatalogTupleUpdate(relation, &oldtuple->t_self, oldtuple);
4783 : : }
4784 : : else
4785 : : {
4786 : : /* new_acl is NULL/empty, so delete the entry we found. */
4787 : 43 : CatalogTupleDelete(relation, &oldtuple->t_self);
4788 : : }
4789 : : }
4790 : : else
4791 : : {
4792 : 1424 : Datum values[Natts_pg_init_privs] = {0};
4793 : 1424 : bool nulls[Natts_pg_init_privs] = {0};
4794 : :
4795 : : /*
4796 : : * Only add a new entry if the new ACL is non-NULL.
4797 : : *
4798 : : * If we are passed in a NULL ACL and no entry exists, we can just
4799 : : * fall through and do nothing.
4800 : : */
4801 [ + + + - ]: 1424 : if (new_acl && ACL_NUM(new_acl) != 0)
4802 : : {
4803 : : /* No entry found, so add it. */
4804 : 351 : values[Anum_pg_init_privs_objoid - 1] = ObjectIdGetDatum(objoid);
4805 : 351 : values[Anum_pg_init_privs_classoid - 1] = ObjectIdGetDatum(classoid);
4806 : 351 : values[Anum_pg_init_privs_objsubid - 1] = Int32GetDatum(objsubid);
4807 : :
4808 : : /* This function only handles initial privileges of extensions */
4809 : 351 : values[Anum_pg_init_privs_privtype - 1] =
4810 : 351 : CharGetDatum(INITPRIVS_EXTENSION);
4811 : :
4812 : 351 : values[Anum_pg_init_privs_initprivs - 1] = PointerGetDatum(new_acl);
4813 : :
4814 : 351 : tuple = heap_form_tuple(RelationGetDescr(relation), values, nulls);
4815 : :
4816 : 351 : CatalogTupleInsert(relation, tuple);
4817 : :
4818 : : /* Update pg_shdepend, too. */
4819 : 351 : noldmembers = 0;
4820 : 351 : oldmembers = NULL;
4821 : :
4822 : 351 : updateInitAclDependencies(classoid, objoid, objsubid,
4823 : : noldmembers, oldmembers,
4824 : : nnewmembers, newmembers);
4825 : : }
4826 : : }
4827 : :
4828 : 1563 : systable_endscan(scan);
4829 : :
4830 : : /* prevent error when processing objects multiple times */
4831 : 1563 : CommandCounterIncrement();
4832 : :
4833 : 1563 : table_close(relation, RowExclusiveLock);
4834 : 1563 : }
4835 : :
4836 : : /*
4837 : : * ReplaceRoleInInitPriv
4838 : : *
4839 : : * Used by shdepReassignOwned to replace mentions of a role in pg_init_privs.
4840 : : */
4841 : : void
4842 : 12 : ReplaceRoleInInitPriv(Oid oldroleid, Oid newroleid,
4843 : : Oid classid, Oid objid, int32 objsubid)
4844 : : {
4845 : : Relation rel;
4846 : : ScanKeyData key[3];
4847 : : SysScanDesc scan;
4848 : : HeapTuple oldtuple;
4849 : : Datum oldAclDatum;
4850 : : bool isNull;
4851 : : Acl *old_acl;
4852 : : Acl *new_acl;
4853 : : HeapTuple newtuple;
4854 : : int noldmembers;
4855 : : int nnewmembers;
4856 : : Oid *oldmembers;
4857 : : Oid *newmembers;
4858 : :
4859 : : /* Search for existing pg_init_privs entry for the target object. */
4860 : 12 : rel = table_open(InitPrivsRelationId, RowExclusiveLock);
4861 : :
4862 : 12 : ScanKeyInit(&key[0],
4863 : : Anum_pg_init_privs_objoid,
4864 : : BTEqualStrategyNumber, F_OIDEQ,
4865 : : ObjectIdGetDatum(objid));
4866 : 12 : ScanKeyInit(&key[1],
4867 : : Anum_pg_init_privs_classoid,
4868 : : BTEqualStrategyNumber, F_OIDEQ,
4869 : : ObjectIdGetDatum(classid));
4870 : 12 : ScanKeyInit(&key[2],
4871 : : Anum_pg_init_privs_objsubid,
4872 : : BTEqualStrategyNumber, F_INT4EQ,
4873 : : Int32GetDatum(objsubid));
4874 : :
4875 : 12 : scan = systable_beginscan(rel, InitPrivsObjIndexId, true,
4876 : : NULL, 3, key);
4877 : :
4878 : : /* There should exist only one entry or none. */
4879 : 12 : oldtuple = systable_getnext(scan);
4880 : :
4881 [ - + ]: 12 : if (!HeapTupleIsValid(oldtuple))
4882 : : {
4883 : : /*
4884 : : * Hmm, why are we here if there's no entry? But pack up and go away
4885 : : * quietly.
4886 : : */
4887 : 0 : systable_endscan(scan);
4888 : 0 : table_close(rel, RowExclusiveLock);
4889 : 0 : return;
4890 : : }
4891 : :
4892 : : /* Get a writable copy of the existing ACL. */
4893 : 12 : oldAclDatum = heap_getattr(oldtuple, Anum_pg_init_privs_initprivs,
4894 : : RelationGetDescr(rel), &isNull);
4895 : : Assert(!isNull);
4896 : 12 : old_acl = DatumGetAclPCopy(oldAclDatum);
4897 : :
4898 : : /*
4899 : : * Generate new ACL. This usage of aclnewowner is a bit off-label when
4900 : : * oldroleid isn't the owner; but it does the job fine.
4901 : : */
4902 : 12 : new_acl = aclnewowner(old_acl, oldroleid, newroleid);
4903 : :
4904 : : /*
4905 : : * If we end with an empty ACL, delete the pg_init_privs entry. (That
4906 : : * probably can't happen here, but we may as well cover the case.)
4907 : : */
4908 [ + - - + ]: 12 : if (new_acl == NULL || ACL_NUM(new_acl) == 0)
4909 : : {
4910 : 0 : CatalogTupleDelete(rel, &oldtuple->t_self);
4911 : : }
4912 : : else
4913 : : {
4914 : 12 : Datum values[Natts_pg_init_privs] = {0};
4915 : 12 : bool nulls[Natts_pg_init_privs] = {0};
4916 : 12 : bool replaces[Natts_pg_init_privs] = {0};
4917 : :
4918 : : /* Update existing entry. */
4919 : 12 : values[Anum_pg_init_privs_initprivs - 1] = PointerGetDatum(new_acl);
4920 : 12 : replaces[Anum_pg_init_privs_initprivs - 1] = true;
4921 : :
4922 : 12 : newtuple = heap_modify_tuple(oldtuple, RelationGetDescr(rel),
4923 : : values, nulls, replaces);
4924 : 12 : CatalogTupleUpdate(rel, &newtuple->t_self, newtuple);
4925 : : }
4926 : :
4927 : : /*
4928 : : * Update the shared dependency ACL info.
4929 : : */
4930 : 12 : noldmembers = aclmembers(old_acl, &oldmembers);
4931 : 12 : nnewmembers = aclmembers(new_acl, &newmembers);
4932 : :
4933 : 12 : updateInitAclDependencies(classid, objid, objsubid,
4934 : : noldmembers, oldmembers,
4935 : : nnewmembers, newmembers);
4936 : :
4937 : 12 : systable_endscan(scan);
4938 : :
4939 : : /* prevent error when processing objects multiple times */
4940 : 12 : CommandCounterIncrement();
4941 : :
4942 : 12 : table_close(rel, RowExclusiveLock);
4943 : : }
4944 : :
4945 : : /*
4946 : : * RemoveRoleFromInitPriv
4947 : : *
4948 : : * Used by shdepDropOwned to remove mentions of a role in pg_init_privs.
4949 : : */
4950 : : void
4951 : 14 : RemoveRoleFromInitPriv(Oid roleid, Oid classid, Oid objid, int32 objsubid)
4952 : : {
4953 : : Relation rel;
4954 : : ScanKeyData key[3];
4955 : : SysScanDesc scan;
4956 : : HeapTuple oldtuple;
4957 : : SysCacheIdentifier cacheid;
4958 : : HeapTuple objtuple;
4959 : : Oid ownerId;
4960 : : Datum oldAclDatum;
4961 : : bool isNull;
4962 : : Acl *old_acl;
4963 : : Acl *new_acl;
4964 : : HeapTuple newtuple;
4965 : : int noldmembers;
4966 : : int nnewmembers;
4967 : : Oid *oldmembers;
4968 : : Oid *newmembers;
4969 : :
4970 : : /* Search for existing pg_init_privs entry for the target object. */
4971 : 14 : rel = table_open(InitPrivsRelationId, RowExclusiveLock);
4972 : :
4973 : 14 : ScanKeyInit(&key[0],
4974 : : Anum_pg_init_privs_objoid,
4975 : : BTEqualStrategyNumber, F_OIDEQ,
4976 : : ObjectIdGetDatum(objid));
4977 : 14 : ScanKeyInit(&key[1],
4978 : : Anum_pg_init_privs_classoid,
4979 : : BTEqualStrategyNumber, F_OIDEQ,
4980 : : ObjectIdGetDatum(classid));
4981 : 14 : ScanKeyInit(&key[2],
4982 : : Anum_pg_init_privs_objsubid,
4983 : : BTEqualStrategyNumber, F_INT4EQ,
4984 : : Int32GetDatum(objsubid));
4985 : :
4986 : 14 : scan = systable_beginscan(rel, InitPrivsObjIndexId, true,
4987 : : NULL, 3, key);
4988 : :
4989 : : /* There should exist only one entry or none. */
4990 : 14 : oldtuple = systable_getnext(scan);
4991 : :
4992 [ - + ]: 14 : if (!HeapTupleIsValid(oldtuple))
4993 : : {
4994 : : /*
4995 : : * Hmm, why are we here if there's no entry? But pack up and go away
4996 : : * quietly.
4997 : : */
4998 : 0 : systable_endscan(scan);
4999 : 0 : table_close(rel, RowExclusiveLock);
5000 : 0 : return;
5001 : : }
5002 : :
5003 : : /* Get a writable copy of the existing ACL. */
5004 : 14 : oldAclDatum = heap_getattr(oldtuple, Anum_pg_init_privs_initprivs,
5005 : : RelationGetDescr(rel), &isNull);
5006 : : Assert(!isNull);
5007 : 14 : old_acl = DatumGetAclPCopy(oldAclDatum);
5008 : :
5009 : : /*
5010 : : * We need the members of both old and new ACLs so we can correct the
5011 : : * shared dependency information. Collect data before
5012 : : * merge_acl_with_grant throws away old_acl.
5013 : : */
5014 : 14 : noldmembers = aclmembers(old_acl, &oldmembers);
5015 : :
5016 : : /* Must find out the owner's OID the hard way. */
5017 : 14 : cacheid = get_object_catcache_oid(classid);
5018 : 14 : objtuple = SearchSysCache1(cacheid, ObjectIdGetDatum(objid));
5019 [ - + ]: 14 : if (!HeapTupleIsValid(objtuple))
5020 [ # # ]: 0 : elog(ERROR, "cache lookup failed for %s %u",
5021 : : get_object_class_descr(classid), objid);
5022 : :
5023 : 14 : ownerId = DatumGetObjectId(SysCacheGetAttrNotNull(cacheid,
5024 : : objtuple,
5025 : 14 : get_object_attnum_owner(classid)));
5026 : 14 : ReleaseSysCache(objtuple);
5027 : :
5028 : : /*
5029 : : * Generate new ACL. Grantor of rights is always the same as the owner.
5030 : : */
5031 [ + - ]: 14 : if (old_acl != NULL)
5032 : 14 : new_acl = merge_acl_with_grant(old_acl,
5033 : : false, /* is_grant */
5034 : : false, /* grant_option */
5035 : : DROP_RESTRICT,
5036 : : list_make1_oid(roleid),
5037 : : ACLITEM_ALL_PRIV_BITS,
5038 : : ownerId,
5039 : : ownerId);
5040 : : else
5041 : 0 : new_acl = NULL; /* this case shouldn't happen, probably */
5042 : :
5043 : : /* If we end with an empty ACL, delete the pg_init_privs entry. */
5044 [ + - - + ]: 14 : if (new_acl == NULL || ACL_NUM(new_acl) == 0)
5045 : : {
5046 : 0 : CatalogTupleDelete(rel, &oldtuple->t_self);
5047 : : }
5048 : : else
5049 : : {
5050 : 14 : Datum values[Natts_pg_init_privs] = {0};
5051 : 14 : bool nulls[Natts_pg_init_privs] = {0};
5052 : 14 : bool replaces[Natts_pg_init_privs] = {0};
5053 : :
5054 : : /* Update existing entry. */
5055 : 14 : values[Anum_pg_init_privs_initprivs - 1] = PointerGetDatum(new_acl);
5056 : 14 : replaces[Anum_pg_init_privs_initprivs - 1] = true;
5057 : :
5058 : 14 : newtuple = heap_modify_tuple(oldtuple, RelationGetDescr(rel),
5059 : : values, nulls, replaces);
5060 : 14 : CatalogTupleUpdate(rel, &newtuple->t_self, newtuple);
5061 : : }
5062 : :
5063 : : /*
5064 : : * Update the shared dependency ACL info.
5065 : : */
5066 : 14 : nnewmembers = aclmembers(new_acl, &newmembers);
5067 : :
5068 : 14 : updateInitAclDependencies(classid, objid, objsubid,
5069 : : noldmembers, oldmembers,
5070 : : nnewmembers, newmembers);
5071 : :
5072 : 14 : systable_endscan(scan);
5073 : :
5074 : : /* prevent error when processing objects multiple times */
5075 : 14 : CommandCounterIncrement();
5076 : :
5077 : 14 : table_close(rel, RowExclusiveLock);
5078 : : }
|