Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * acl.c
4 : : * Basic access control list data structures manipulation routines.
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/utils/adt/acl.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include <ctype.h>
18 : :
19 : : #include "access/htup_details.h"
20 : : #include "bootstrap/bootstrap.h"
21 : : #include "catalog/catalog.h"
22 : : #include "catalog/namespace.h"
23 : : #include "catalog/pg_auth_members.h"
24 : : #include "catalog/pg_authid.h"
25 : : #include "catalog/pg_class.h"
26 : : #include "catalog/pg_database.h"
27 : : #include "catalog/pg_foreign_data_wrapper.h"
28 : : #include "catalog/pg_foreign_server.h"
29 : : #include "catalog/pg_language.h"
30 : : #include "catalog/pg_largeobject.h"
31 : : #include "catalog/pg_namespace.h"
32 : : #include "catalog/pg_proc.h"
33 : : #include "catalog/pg_tablespace.h"
34 : : #include "catalog/pg_type.h"
35 : : #include "commands/proclang.h"
36 : : #include "commands/tablespace.h"
37 : : #include "common/hashfn.h"
38 : : #include "foreign/foreign.h"
39 : : #include "funcapi.h"
40 : : #include "lib/bloomfilter.h"
41 : : #include "lib/qunique.h"
42 : : #include "miscadmin.h"
43 : : #include "port/pg_bitutils.h"
44 : : #include "storage/large_object.h"
45 : : #include "utils/acl.h"
46 : : #include "utils/array.h"
47 : : #include "utils/builtins.h"
48 : : #include "utils/catcache.h"
49 : : #include "utils/inval.h"
50 : : #include "utils/lsyscache.h"
51 : : #include "utils/memutils.h"
52 : : #include "utils/snapmgr.h"
53 : : #include "utils/syscache.h"
54 : : #include "utils/varlena.h"
55 : :
56 : : typedef struct
57 : : {
58 : : const char *name;
59 : : AclMode value;
60 : : } priv_map;
61 : :
62 : : /*
63 : : * We frequently need to test whether a given role is a member of some other
64 : : * role. In most of these tests the "given role" is the same, namely the
65 : : * active current user. So we can optimize it by keeping cached lists of all
66 : : * the roles the "given role" is a member of, directly or indirectly.
67 : : *
68 : : * Possibly this mechanism should be generalized to allow caching membership
69 : : * info for multiple roles?
70 : : *
71 : : * Each element of cached_roles is an OID list of constituent roles for the
72 : : * corresponding element of cached_role (always including the cached_role
73 : : * itself). There's a separate cache for each RoleRecurseType, with the
74 : : * corresponding semantics.
75 : : */
76 : : enum RoleRecurseType
77 : : {
78 : : ROLERECURSE_MEMBERS = 0, /* recurse unconditionally */
79 : : ROLERECURSE_PRIVS = 1, /* recurse through inheritable grants */
80 : : ROLERECURSE_SETROLE = 2 /* recurse through grants with set_option */
81 : : };
82 : : static Oid cached_role[] = {InvalidOid, InvalidOid, InvalidOid};
83 : : static List *cached_roles[] = {NIL, NIL, NIL};
84 : : uint32 cached_db_hash;
85 : :
86 : : /*
87 : : * If the list of roles gathered by roles_is_member_of() grows larger than the
88 : : * below threshold, a Bloom filter is created to speed up list membership
89 : : * checks. This threshold is set arbitrarily high to avoid the overhead of
90 : : * creating the Bloom filter until it seems likely to provide a net benefit.
91 : : */
92 : : #define ROLES_LIST_BLOOM_THRESHOLD 1024
93 : :
94 : : static const char *getid(const char *s, char *n, Node *escontext);
95 : : static void putid(char *p, const char *s);
96 : : static Acl *allocacl(int n);
97 : : static void check_acl(const Acl *acl);
98 : : static const char *aclparse(const char *s, AclItem *aip, Node *escontext);
99 : : static bool aclitem_match(const AclItem *a1, const AclItem *a2);
100 : : static int aclitemComparator(const void *arg1, const void *arg2);
101 : : static void check_circularity(const Acl *old_acl, const AclItem *mod_aip,
102 : : Oid ownerId);
103 : : static Acl *recursive_revoke(Acl *acl, Oid grantee, AclMode revoke_privs,
104 : : Oid ownerId, DropBehavior behavior);
105 : :
106 : : static AclMode convert_any_priv_string(text *priv_type_text,
107 : : const priv_map *privileges);
108 : :
109 : : static Oid convert_table_name(text *tablename);
110 : : static AclMode convert_table_priv_string(text *priv_type_text);
111 : : static AclMode convert_sequence_priv_string(text *priv_type_text);
112 : : static AttrNumber convert_column_name(Oid tableoid, text *column);
113 : : static AclMode convert_column_priv_string(text *priv_type_text);
114 : : static Oid convert_database_name(text *databasename);
115 : : static AclMode convert_database_priv_string(text *priv_type_text);
116 : : static Oid convert_foreign_data_wrapper_name(text *fdwname);
117 : : static AclMode convert_foreign_data_wrapper_priv_string(text *priv_type_text);
118 : : static Oid convert_function_name(text *functionname);
119 : : static AclMode convert_function_priv_string(text *priv_type_text);
120 : : static Oid convert_language_name(text *languagename);
121 : : static AclMode convert_language_priv_string(text *priv_type_text);
122 : : static Oid convert_schema_name(text *schemaname);
123 : : static AclMode convert_schema_priv_string(text *priv_type_text);
124 : : static Oid convert_server_name(text *servername);
125 : : static AclMode convert_server_priv_string(text *priv_type_text);
126 : : static Oid convert_tablespace_name(text *tablespacename);
127 : : static AclMode convert_tablespace_priv_string(text *priv_type_text);
128 : : static Oid convert_type_name(text *typename);
129 : : static AclMode convert_type_priv_string(text *priv_type_text);
130 : : static AclMode convert_parameter_priv_string(text *priv_text);
131 : : static AclMode convert_largeobject_priv_string(text *priv_type_text);
132 : : static AclMode convert_role_priv_string(text *priv_type_text);
133 : : static AclResult pg_role_aclcheck(Oid role_oid, Oid roleid, AclMode mode);
134 : :
135 : : static void RoleMembershipCacheCallback(Datum arg, SysCacheIdentifier cacheid,
136 : : uint32 hashvalue);
137 : :
138 : :
139 : : /*
140 : : * Test whether an identifier char can be left unquoted in ACLs.
141 : : *
142 : : * Formerly, we used isalnum() even on non-ASCII characters, resulting in
143 : : * unportable behavior. To ensure dump compatibility with old versions,
144 : : * we now treat high-bit-set characters as always requiring quoting during
145 : : * putid(), but getid() will always accept them without quotes.
146 : : */
147 : : static inline bool
148 : 6968534 : is_safe_acl_char(unsigned char c, bool is_getid)
149 : : {
150 [ - + ]: 6968534 : if (IS_HIGHBIT_SET(c))
151 : 0 : return is_getid;
152 [ + + + + ]: 6968534 : return isalnum(c) || c == '_';
153 : : }
154 : :
155 : : /*
156 : : * getid
157 : : * Consumes the first alphanumeric string (identifier) found in string
158 : : * 's', ignoring any leading white space. If it finds a double quote
159 : : * it returns the word inside the quotes.
160 : : *
161 : : * RETURNS:
162 : : * the string position in 's' that points to the next non-space character
163 : : * in 's', after any quotes. Also:
164 : : * - loads the identifier into 'n'. (If no identifier is found, 'n'
165 : : * contains an empty string.) 'n' must be NAMEDATALEN bytes.
166 : : *
167 : : * Errors are reported via ereport, unless escontext is an ErrorSaveData node,
168 : : * in which case we log the error there and return NULL.
169 : : */
170 : : static const char *
171 : 5260 : getid(const char *s, char *n, Node *escontext)
172 : : {
173 : 5260 : int len = 0;
174 : 5260 : bool in_quotes = false;
175 : :
176 : : Assert(s && n);
177 : :
178 [ - + ]: 5260 : while (isspace((unsigned char) *s))
179 : 0 : s++;
180 : 5260 : for (;
181 [ + + + + ]: 51327 : *s != '\0' &&
182 [ + + + + ]: 50589 : (in_quotes || *s == '"' || is_safe_acl_char(*s, true));
183 : 46067 : s++)
184 : : {
185 [ + + ]: 46067 : if (*s == '"')
186 : : {
187 [ + + ]: 154 : if (!in_quotes)
188 : : {
189 : 73 : in_quotes = true;
190 : 73 : continue;
191 : : }
192 : : /* safe to look at next char (could be '\0' though) */
193 [ + + ]: 81 : if (*(s + 1) != '"')
194 : : {
195 : 73 : in_quotes = false;
196 : 73 : continue;
197 : : }
198 : : /* it's an escaped double quote; skip the escaping char */
199 : 8 : s++;
200 : : }
201 : :
202 : : /* Add the character to the string */
203 [ - + ]: 45921 : if (len >= NAMEDATALEN - 1)
204 [ # # ]: 0 : ereturn(escontext, NULL,
205 : : (errcode(ERRCODE_NAME_TOO_LONG),
206 : : errmsg("identifier too long"),
207 : : errdetail("Identifier must be less than %d characters.",
208 : : NAMEDATALEN)));
209 : :
210 : 45921 : n[len++] = *s;
211 : : }
212 : 5260 : n[len] = '\0';
213 [ - + ]: 5260 : while (isspace((unsigned char) *s))
214 : 0 : s++;
215 : 5260 : return s;
216 : : }
217 : :
218 : : /*
219 : : * Write a role name at *p, adding double quotes if needed.
220 : : * There must be at least (2*NAMEDATALEN)+2 bytes available at *p.
221 : : * This needs to be kept in sync with dequoteAclUserName in pg_dump/dumputils.c
222 : : */
223 : : static void
224 : 828672 : putid(char *p, const char *s)
225 : : {
226 : : const char *src;
227 : 828672 : bool safe = true;
228 : :
229 : : /* Detect whether we need to use double quotes */
230 [ + + ]: 7746472 : for (src = s; *src; src++)
231 : : {
232 [ + + ]: 6918018 : if (!is_safe_acl_char(*src, false))
233 : : {
234 : 218 : safe = false;
235 : 218 : break;
236 : : }
237 : : }
238 [ + + ]: 828672 : if (!safe)
239 : 218 : *p++ = '"';
240 [ + + ]: 7748402 : for (src = s; *src; src++)
241 : : {
242 : : /* A double quote character in a username is encoded as "" */
243 [ + + ]: 6919730 : if (*src == '"')
244 : 218 : *p++ = '"';
245 : 6919730 : *p++ = *src;
246 : : }
247 [ + + ]: 828672 : if (!safe)
248 : 218 : *p++ = '"';
249 : 828672 : *p = '\0';
250 : 828672 : }
251 : :
252 : : /*
253 : : * aclparse
254 : : * Consumes and parses an ACL specification of the form:
255 : : * [group|user] [A-Za-z0-9]*=[rwaR]*
256 : : * from string 's', ignoring any leading white space or white space
257 : : * between the optional id type keyword (group|user) and the actual
258 : : * ACL specification.
259 : : *
260 : : * The group|user decoration is unnecessary in the roles world,
261 : : * but we still accept it for backward compatibility.
262 : : *
263 : : * This routine is called by the parser as well as aclitemin(), hence
264 : : * the added generality.
265 : : *
266 : : * In bootstrap mode, we consult a hard-wired list of role names
267 : : * (see bootstrap.c) rather than trying to access the catalogs.
268 : : *
269 : : * RETURNS:
270 : : * the string position in 's' immediately following the ACL
271 : : * specification. Also:
272 : : * - loads the structure pointed to by 'aip' with the appropriate
273 : : * UID/GID, id type identifier and mode type values.
274 : : *
275 : : * Errors are reported via ereport, unless escontext is an ErrorSaveData node,
276 : : * in which case we log the error there and return NULL.
277 : : */
278 : : static const char *
279 : 5171 : aclparse(const char *s, AclItem *aip, Node *escontext)
280 : : {
281 : : AclMode privs,
282 : : goption,
283 : : read;
284 : : char name[NAMEDATALEN];
285 : : char name2[NAMEDATALEN];
286 : :
287 : : Assert(s && aip);
288 : :
289 : 5171 : s = getid(s, name, escontext);
290 [ - + ]: 5171 : if (s == NULL)
291 : 0 : return NULL;
292 [ - + ]: 5171 : if (*s != '=')
293 : : {
294 : : /* we just read a keyword, not a name */
295 [ # # # # ]: 0 : if (strcmp(name, "group") != 0 && strcmp(name, "user") != 0)
296 [ # # ]: 0 : ereturn(escontext, NULL,
297 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
298 : : errmsg("unrecognized key word: \"%s\"", name),
299 : : errhint("ACL key word must be \"group\" or \"user\".")));
300 : : /* move s to the name beyond the keyword */
301 : 0 : s = getid(s, name, escontext);
302 [ # # ]: 0 : if (s == NULL)
303 : 0 : return NULL;
304 [ # # ]: 0 : if (name[0] == '\0')
305 [ # # ]: 0 : ereturn(escontext, NULL,
306 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
307 : : errmsg("missing name"),
308 : : errhint("A name must follow the \"group\" or \"user\" key word.")));
309 : : }
310 : :
311 [ - + ]: 5171 : if (*s != '=')
312 [ # # ]: 0 : ereturn(escontext, NULL,
313 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
314 : : errmsg("missing \"=\" sign")));
315 : :
316 : 5171 : privs = goption = ACL_NO_RIGHTS;
317 : :
318 [ + + + + ]: 10354 : for (++s, read = 0; isalpha((unsigned char) *s) || *s == '*'; s++)
319 : : {
320 [ + - + - : 5191 : switch (*s)
- - - - +
+ + - - -
- - + ]
321 : : {
322 : 8 : case '*':
323 : 8 : goption |= read;
324 : 8 : break;
325 : 0 : case ACL_INSERT_CHR:
326 : 0 : read = ACL_INSERT;
327 : 0 : break;
328 : 93 : case ACL_SELECT_CHR:
329 : 93 : read = ACL_SELECT;
330 : 93 : break;
331 : 0 : case ACL_UPDATE_CHR:
332 : 0 : read = ACL_UPDATE;
333 : 0 : break;
334 : 0 : case ACL_DELETE_CHR:
335 : 0 : read = ACL_DELETE;
336 : 0 : break;
337 : 0 : case ACL_TRUNCATE_CHR:
338 : 0 : read = ACL_TRUNCATE;
339 : 0 : break;
340 : 0 : case ACL_REFERENCES_CHR:
341 : 0 : read = ACL_REFERENCES;
342 : 0 : break;
343 : 0 : case ACL_TRIGGER_CHR:
344 : 0 : read = ACL_TRIGGER;
345 : 0 : break;
346 : 5074 : case ACL_EXECUTE_CHR:
347 : 5074 : read = ACL_EXECUTE;
348 : 5074 : break;
349 : 4 : case ACL_USAGE_CHR:
350 : 4 : read = ACL_USAGE;
351 : 4 : break;
352 : 4 : case ACL_CREATE_CHR:
353 : 4 : read = ACL_CREATE;
354 : 4 : break;
355 : 0 : case ACL_CREATE_TEMP_CHR:
356 : 0 : read = ACL_CREATE_TEMP;
357 : 0 : break;
358 : 0 : case ACL_CONNECT_CHR:
359 : 0 : read = ACL_CONNECT;
360 : 0 : break;
361 : 0 : case ACL_SET_CHR:
362 : 0 : read = ACL_SET;
363 : 0 : break;
364 : 0 : case ACL_ALTER_SYSTEM_CHR:
365 : 0 : read = ACL_ALTER_SYSTEM;
366 : 0 : break;
367 : 0 : case ACL_MAINTAIN_CHR:
368 : 0 : read = ACL_MAINTAIN;
369 : 0 : break;
370 : 8 : default:
371 [ + + ]: 8 : ereturn(escontext, NULL,
372 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
373 : : errmsg("invalid mode character: must be one of \"%s\"",
374 : : ACL_ALL_RIGHTS_STR)));
375 : : }
376 : :
377 : 5183 : privs |= read;
378 : : }
379 : :
380 [ + + ]: 5163 : if (name[0] == '\0')
381 : 61 : aip->ai_grantee = ACL_ID_PUBLIC;
382 : : else
383 : : {
384 [ + + ]: 5102 : if (IsBootstrapProcessingMode())
385 : 5074 : aip->ai_grantee = boot_get_role_oid(name);
386 : : else
387 : 28 : aip->ai_grantee = get_role_oid(name, true);
388 [ - + ]: 5102 : if (!OidIsValid(aip->ai_grantee))
389 [ # # ]: 0 : ereturn(escontext, NULL,
390 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
391 : : errmsg("role \"%s\" does not exist", name)));
392 : : }
393 : :
394 : : /*
395 : : * XXX Allow a degree of backward compatibility by defaulting the grantor
396 : : * to the superuser. We condone that practice in the catalog .dat files
397 : : * (i.e., in bootstrap mode) for brevity; otherwise, issue a warning.
398 : : */
399 [ + + ]: 5163 : if (*s == '/')
400 : : {
401 : 89 : s = getid(s + 1, name2, escontext);
402 [ - + ]: 89 : if (s == NULL)
403 : 0 : return NULL;
404 [ + + ]: 89 : if (name2[0] == '\0')
405 [ + + ]: 12 : ereturn(escontext, NULL,
406 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
407 : : errmsg("a name must follow the \"/\" sign")));
408 [ - + ]: 77 : if (IsBootstrapProcessingMode())
409 : 0 : aip->ai_grantor = boot_get_role_oid(name2);
410 : : else
411 : 77 : aip->ai_grantor = get_role_oid(name2, true);
412 [ + + ]: 77 : if (!OidIsValid(aip->ai_grantor))
413 [ + + ]: 8 : ereturn(escontext, NULL,
414 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
415 : : errmsg("role \"%s\" does not exist", name2)));
416 : : }
417 : : else
418 : : {
419 : 5074 : aip->ai_grantor = BOOTSTRAP_SUPERUSERID;
420 [ - + ]: 5074 : if (!IsBootstrapProcessingMode())
421 [ # # ]: 0 : ereport(WARNING,
422 : : (errcode(ERRCODE_INVALID_GRANTOR),
423 : : errmsg("defaulting grantor to user ID %u",
424 : : BOOTSTRAP_SUPERUSERID)));
425 : : }
426 : :
427 : 5143 : ACLITEM_SET_PRIVS_GOPTIONS(*aip, privs, goption);
428 : :
429 : 5143 : return s;
430 : : }
431 : :
432 : : /*
433 : : * allocacl
434 : : * Allocates storage for a new Acl with 'n' entries.
435 : : *
436 : : * RETURNS:
437 : : * the new Acl
438 : : */
439 : : static Acl *
440 : 390362 : allocacl(int n)
441 : : {
442 : : Acl *new_acl;
443 : : Size size;
444 : :
445 [ - + ]: 390362 : if (n < 0)
446 [ # # ]: 0 : elog(ERROR, "invalid size: %d", n);
447 : 390362 : size = ACL_N_SIZE(n);
448 : 390362 : new_acl = (Acl *) palloc0(size);
449 : 390362 : SET_VARSIZE(new_acl, size);
450 : 390362 : new_acl->ndim = 1;
451 : 390362 : new_acl->dataoffset = 0; /* we never put in any nulls */
452 : 390362 : new_acl->elemtype = ACLITEMOID;
453 : 390362 : ARR_LBOUND(new_acl)[0] = 1;
454 : 390362 : ARR_DIMS(new_acl)[0] = n;
455 : 390362 : return new_acl;
456 : : }
457 : :
458 : : /*
459 : : * Create a zero-entry ACL
460 : : */
461 : : Acl *
462 : 43 : make_empty_acl(void)
463 : : {
464 : 43 : return allocacl(0);
465 : : }
466 : :
467 : : /*
468 : : * Copy an ACL
469 : : */
470 : : Acl *
471 : 11139 : aclcopy(const Acl *orig_acl)
472 : : {
473 : : Acl *result_acl;
474 : :
475 : 11139 : result_acl = allocacl(ACL_NUM(orig_acl));
476 : :
477 [ - + ]: 11139 : memcpy(ACL_DAT(result_acl),
478 : 11139 : ACL_DAT(orig_acl),
479 [ - + ]: 11139 : ACL_NUM(orig_acl) * sizeof(AclItem));
480 : :
481 : 11139 : return result_acl;
482 : : }
483 : :
484 : : /*
485 : : * Concatenate two ACLs
486 : : *
487 : : * This is a bit cheesy, since we may produce an ACL with redundant entries.
488 : : * Be careful what the result is used for!
489 : : */
490 : : Acl *
491 : 28851 : aclconcat(const Acl *left_acl, const Acl *right_acl)
492 : : {
493 : : Acl *result_acl;
494 : :
495 : 28851 : result_acl = allocacl(ACL_NUM(left_acl) + ACL_NUM(right_acl));
496 : :
497 [ - + ]: 28851 : memcpy(ACL_DAT(result_acl),
498 : 28851 : ACL_DAT(left_acl),
499 [ - + ]: 28851 : ACL_NUM(left_acl) * sizeof(AclItem));
500 : :
501 [ - + ]: 28851 : memcpy(ACL_DAT(result_acl) + ACL_NUM(left_acl),
502 : 28851 : ACL_DAT(right_acl),
503 [ - + ]: 28851 : ACL_NUM(right_acl) * sizeof(AclItem));
504 : :
505 : 28851 : return result_acl;
506 : : }
507 : :
508 : : /*
509 : : * Merge two ACLs
510 : : *
511 : : * This produces a properly merged ACL with no redundant entries.
512 : : * Returns NULL on NULL input.
513 : : */
514 : : Acl *
515 : 140 : aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId)
516 : : {
517 : : Acl *result_acl;
518 : : AclItem *aip;
519 : : int i,
520 : : num;
521 : :
522 : : /* Check for cases where one or both are empty/null */
523 [ + - - + ]: 140 : if (left_acl == NULL || ACL_NUM(left_acl) == 0)
524 : : {
525 [ # # # # ]: 0 : if (right_acl == NULL || ACL_NUM(right_acl) == 0)
526 : 0 : return NULL;
527 : : else
528 : 0 : return aclcopy(right_acl);
529 : : }
530 : : else
531 : : {
532 [ + + - + ]: 140 : if (right_acl == NULL || ACL_NUM(right_acl) == 0)
533 : 92 : return aclcopy(left_acl);
534 : : }
535 : :
536 : : /* Merge them the hard way, one item at a time */
537 : 48 : result_acl = aclcopy(left_acl);
538 : :
539 [ - + ]: 48 : aip = ACL_DAT(right_acl);
540 : 48 : num = ACL_NUM(right_acl);
541 : :
542 [ + + ]: 120 : for (i = 0; i < num; i++, aip++)
543 : : {
544 : : Acl *tmp_acl;
545 : :
546 : 72 : tmp_acl = aclupdate(result_acl, aip, ACL_MODECHG_ADD,
547 : : ownerId, DROP_RESTRICT);
548 : 72 : pfree(result_acl);
549 : 72 : result_acl = tmp_acl;
550 : : }
551 : :
552 : 48 : return result_acl;
553 : : }
554 : :
555 : : /*
556 : : * Sort the items in an ACL (into an arbitrary but consistent order)
557 : : */
558 : : void
559 : 590 : aclitemsort(Acl *acl)
560 : : {
561 [ + - + + ]: 590 : if (acl != NULL && ACL_NUM(acl) > 1)
562 [ - + ]: 171 : qsort(ACL_DAT(acl), ACL_NUM(acl), sizeof(AclItem), aclitemComparator);
563 : 590 : }
564 : :
565 : : /*
566 : : * Check if two ACLs are exactly equal
567 : : *
568 : : * This will not detect equality if the two arrays contain the same items
569 : : * in different orders. To handle that case, sort both inputs first,
570 : : * using aclitemsort().
571 : : */
572 : : bool
573 : 363 : aclequal(const Acl *left_acl, const Acl *right_acl)
574 : : {
575 : : /* Check for cases where one or both are empty/null */
576 [ + - + + ]: 363 : if (left_acl == NULL || ACL_NUM(left_acl) == 0)
577 : : {
578 [ + - + - ]: 1 : if (right_acl == NULL || ACL_NUM(right_acl) == 0)
579 : 1 : return true;
580 : : else
581 : 0 : return false;
582 : : }
583 : : else
584 : : {
585 [ + - + + ]: 362 : if (right_acl == NULL || ACL_NUM(right_acl) == 0)
586 : 34 : return false;
587 : : }
588 : :
589 [ + + ]: 328 : if (ACL_NUM(left_acl) != ACL_NUM(right_acl))
590 : 149 : return false;
591 : :
592 [ - + + + ]: 179 : if (memcmp(ACL_DAT(left_acl),
593 : 179 : ACL_DAT(right_acl),
594 [ - + ]: 179 : ACL_NUM(left_acl) * sizeof(AclItem)) == 0)
595 : 86 : return true;
596 : :
597 : 93 : return false;
598 : : }
599 : :
600 : : /*
601 : : * Verify that an ACL array is acceptable (one-dimensional and has no nulls)
602 : : */
603 : : static void
604 : 181072 : check_acl(const Acl *acl)
605 : : {
606 [ - + ]: 181072 : if (ARR_ELEMTYPE(acl) != ACLITEMOID)
607 [ # # ]: 0 : ereport(ERROR,
608 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
609 : : errmsg("ACL array contains wrong data type")));
610 [ - + ]: 181072 : if (ARR_NDIM(acl) != 1)
611 [ # # ]: 0 : ereport(ERROR,
612 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
613 : : errmsg("ACL arrays must be one-dimensional")));
614 [ - + ]: 181072 : if (ARR_HASNULL(acl))
615 [ # # ]: 0 : ereport(ERROR,
616 : : (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
617 : : errmsg("ACL arrays must not contain null values")));
618 : 181072 : }
619 : :
620 : : /*
621 : : * aclitemin
622 : : * Allocates storage for, and fills in, a new AclItem given a string
623 : : * 's' that contains an ACL specification. See aclparse for details.
624 : : *
625 : : * RETURNS:
626 : : * the new AclItem
627 : : */
628 : : Datum
629 : 5171 : aclitemin(PG_FUNCTION_ARGS)
630 : : {
631 : 5171 : const char *s = PG_GETARG_CSTRING(0);
632 : 5171 : Node *escontext = fcinfo->context;
633 : : AclItem *aip;
634 : :
635 : 5171 : aip = palloc_object(AclItem);
636 : :
637 : 5171 : s = aclparse(s, aip, escontext);
638 [ + + ]: 5167 : if (s == NULL)
639 : 24 : PG_RETURN_NULL();
640 : :
641 [ - + ]: 5143 : while (isspace((unsigned char) *s))
642 : 0 : ++s;
643 [ - + ]: 5143 : if (*s)
644 [ # # ]: 0 : ereturn(escontext, (Datum) 0,
645 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
646 : : errmsg("extra garbage at the end of the ACL specification")));
647 : :
648 : 5143 : PG_RETURN_ACLITEM_P(aip);
649 : : }
650 : :
651 : : /*
652 : : * aclitemout
653 : : * Allocates storage for, and fills in, a new null-delimited string
654 : : * containing a formatted ACL specification. See aclparse for details.
655 : : *
656 : : * In bootstrap mode, this is called for debug printouts (initdb -d).
657 : : * We could ask bootstrap.c to provide an inverse of boot_get_role_oid(),
658 : : * but it seems at least as useful to just print numeric role OIDs.
659 : : *
660 : : * RETURNS:
661 : : * the new string
662 : : */
663 : : Datum
664 : 529361 : aclitemout(PG_FUNCTION_ARGS)
665 : : {
666 : 529361 : AclItem *aip = PG_GETARG_ACLITEM_P(0);
667 : : char *p;
668 : : char *out;
669 : : HeapTuple htup;
670 : : unsigned i;
671 : :
672 : 529361 : out = palloc(strlen("=/") +
673 : : 2 * N_ACL_RIGHTS +
674 : : 2 * (2 * NAMEDATALEN + 2) +
675 : : 1);
676 : :
677 : 529361 : p = out;
678 : 529361 : *p = '\0';
679 : :
680 [ + + ]: 529361 : if (aip->ai_grantee != ACL_ID_PUBLIC)
681 : : {
682 [ + - ]: 299311 : if (!IsBootstrapProcessingMode())
683 : 299311 : htup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(aip->ai_grantee));
684 : : else
685 : 0 : htup = NULL;
686 [ + - ]: 299311 : if (HeapTupleIsValid(htup))
687 : : {
688 : 299311 : putid(p, NameStr(((Form_pg_authid) GETSTRUCT(htup))->rolname));
689 : 299311 : ReleaseSysCache(htup);
690 : : }
691 : : else
692 : : {
693 : : /* No such entry, or bootstrap mode: print numeric OID */
694 : 0 : sprintf(p, "%u", aip->ai_grantee);
695 : : }
696 : : }
697 [ + + ]: 3060465 : while (*p)
698 : 2531104 : ++p;
699 : :
700 : 529361 : *p++ = '=';
701 : :
702 [ + + ]: 8469776 : for (i = 0; i < N_ACL_RIGHTS; ++i)
703 : : {
704 [ + + ]: 7940415 : if (ACLITEM_GET_PRIVS(*aip) & (UINT64CONST(1) << i))
705 : 1410645 : *p++ = ACL_ALL_RIGHTS_STR[i];
706 [ + + ]: 7940415 : if (ACLITEM_GET_GOPTIONS(*aip) & (UINT64CONST(1) << i))
707 : 195 : *p++ = '*';
708 : : }
709 : :
710 : 529361 : *p++ = '/';
711 : 529361 : *p = '\0';
712 : :
713 [ + - ]: 529361 : if (!IsBootstrapProcessingMode())
714 : 529361 : htup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(aip->ai_grantor));
715 : : else
716 : 0 : htup = NULL;
717 [ + - ]: 529361 : if (HeapTupleIsValid(htup))
718 : : {
719 : 529361 : putid(p, NameStr(((Form_pg_authid) GETSTRUCT(htup))->rolname));
720 : 529361 : ReleaseSysCache(htup);
721 : : }
722 : : else
723 : : {
724 : : /* No such entry, or bootstrap mode: print numeric OID */
725 : 0 : sprintf(p, "%u", aip->ai_grantor);
726 : : }
727 : :
728 : 529361 : PG_RETURN_CSTRING(out);
729 : : }
730 : :
731 : : /*
732 : : * aclitem_match
733 : : * Two AclItems are considered to match iff they have the same
734 : : * grantee and grantor; the privileges are ignored.
735 : : */
736 : : static bool
737 : 16447 : aclitem_match(const AclItem *a1, const AclItem *a2)
738 : : {
739 [ + + ]: 20338 : return a1->ai_grantee == a2->ai_grantee &&
740 [ + + ]: 3891 : a1->ai_grantor == a2->ai_grantor;
741 : : }
742 : :
743 : : /*
744 : : * aclitemComparator
745 : : * qsort comparison function for AclItems
746 : : */
747 : : static int
748 : 183 : aclitemComparator(const void *arg1, const void *arg2)
749 : : {
750 : 183 : const AclItem *a1 = (const AclItem *) arg1;
751 : 183 : const AclItem *a2 = (const AclItem *) arg2;
752 : :
753 [ + + ]: 183 : if (a1->ai_grantee > a2->ai_grantee)
754 : 28 : return 1;
755 [ + - ]: 155 : if (a1->ai_grantee < a2->ai_grantee)
756 : 155 : return -1;
757 [ # # ]: 0 : if (a1->ai_grantor > a2->ai_grantor)
758 : 0 : return 1;
759 [ # # ]: 0 : if (a1->ai_grantor < a2->ai_grantor)
760 : 0 : return -1;
761 [ # # ]: 0 : if (a1->ai_privs > a2->ai_privs)
762 : 0 : return 1;
763 [ # # ]: 0 : if (a1->ai_privs < a2->ai_privs)
764 : 0 : return -1;
765 : 0 : return 0;
766 : : }
767 : :
768 : : /*
769 : : * aclitem equality operator
770 : : */
771 : : Datum
772 : 137279 : aclitem_eq(PG_FUNCTION_ARGS)
773 : : {
774 : 137279 : AclItem *a1 = PG_GETARG_ACLITEM_P(0);
775 : 137279 : AclItem *a2 = PG_GETARG_ACLITEM_P(1);
776 : : bool result;
777 : :
778 : 400266 : result = a1->ai_privs == a2->ai_privs &&
779 [ + + + + ]: 259583 : a1->ai_grantee == a2->ai_grantee &&
780 [ + - ]: 122304 : a1->ai_grantor == a2->ai_grantor;
781 : 137279 : PG_RETURN_BOOL(result);
782 : : }
783 : :
784 : : /*
785 : : * aclitem hash function
786 : : *
787 : : * We make aclitems hashable not so much because anyone is likely to hash
788 : : * them, as because we want array equality to work on aclitem arrays, and
789 : : * with the typcache mechanism we must have a hash or btree opclass.
790 : : */
791 : : Datum
792 : 16254 : hash_aclitem(PG_FUNCTION_ARGS)
793 : : {
794 : 16254 : AclItem *a = PG_GETARG_ACLITEM_P(0);
795 : :
796 : : /* not very bright, but avoids any issue of padding in struct */
797 : 16254 : PG_RETURN_UINT32((uint32) (a->ai_privs + a->ai_grantee + a->ai_grantor));
798 : : }
799 : :
800 : : /*
801 : : * 64-bit hash function for aclitem.
802 : : *
803 : : * Similar to hash_aclitem, but accepts a seed and returns a uint64 value.
804 : : */
805 : : Datum
806 : 8 : hash_aclitem_extended(PG_FUNCTION_ARGS)
807 : : {
808 : 8 : AclItem *a = PG_GETARG_ACLITEM_P(0);
809 : 8 : uint64 seed = PG_GETARG_INT64(1);
810 : 8 : uint32 sum = (uint32) (a->ai_privs + a->ai_grantee + a->ai_grantor);
811 : :
812 [ + + ]: 8 : return (seed == 0) ? UInt64GetDatum(sum) : hash_uint32_extended(sum, seed);
813 : : }
814 : :
815 : : /*
816 : : * acldefault() --- create an ACL describing default access permissions
817 : : *
818 : : * Change this routine if you want to alter the default access policy for
819 : : * newly-created objects (or any object with a NULL acl entry). When
820 : : * you make a change here, don't forget to update the GRANT man page,
821 : : * which explains all the default permissions.
822 : : *
823 : : * Note that these are the hard-wired "defaults" that are used in the
824 : : * absence of any pg_default_acl entry.
825 : : */
826 : : Acl *
827 : 308812 : acldefault(ObjectType objtype, Oid ownerId)
828 : : {
829 : : AclMode world_default;
830 : : AclMode owner_default;
831 : : int nacl;
832 : : Acl *acl;
833 : : AclItem *aip;
834 : :
835 [ + + + + : 308812 : switch (objtype)
+ + + + +
+ + + +
- ]
836 : : {
837 : 28619 : case OBJECT_COLUMN:
838 : : /* by default, columns have no extra privileges */
839 : 28619 : world_default = ACL_NO_RIGHTS;
840 : 28619 : owner_default = ACL_NO_RIGHTS;
841 : 28619 : break;
842 : 95448 : case OBJECT_TABLE:
843 : 95448 : world_default = ACL_NO_RIGHTS;
844 : 95448 : owner_default = ACL_ALL_RIGHTS_RELATION;
845 : 95448 : break;
846 : 750 : case OBJECT_SEQUENCE:
847 : 750 : world_default = ACL_NO_RIGHTS;
848 : 750 : owner_default = ACL_ALL_RIGHTS_SEQUENCE;
849 : 750 : break;
850 : 696 : case OBJECT_DATABASE:
851 : : /* for backwards compatibility, grant some rights by default */
852 : 696 : world_default = ACL_CREATE_TEMP | ACL_CONNECT;
853 : 696 : owner_default = ACL_ALL_RIGHTS_DATABASE;
854 : 696 : break;
855 : 33957 : case OBJECT_FUNCTION:
856 : : /* Grant EXECUTE by default, for now */
857 : 33957 : world_default = ACL_EXECUTE;
858 : 33957 : owner_default = ACL_ALL_RIGHTS_FUNCTION;
859 : 33957 : break;
860 : 485 : case OBJECT_LANGUAGE:
861 : : /* Grant USAGE by default, for now */
862 : 485 : world_default = ACL_USAGE;
863 : 485 : owner_default = ACL_ALL_RIGHTS_LANGUAGE;
864 : 485 : break;
865 : 220 : case OBJECT_LARGEOBJECT:
866 : 220 : world_default = ACL_NO_RIGHTS;
867 : 220 : owner_default = ACL_ALL_RIGHTS_LARGEOBJECT;
868 : 220 : break;
869 : 2230 : case OBJECT_SCHEMA:
870 : 2230 : world_default = ACL_NO_RIGHTS;
871 : 2230 : owner_default = ACL_ALL_RIGHTS_SCHEMA;
872 : 2230 : break;
873 : 34 : case OBJECT_TABLESPACE:
874 : 34 : world_default = ACL_NO_RIGHTS;
875 : 34 : owner_default = ACL_ALL_RIGHTS_TABLESPACE;
876 : 34 : break;
877 : 90 : case OBJECT_FDW:
878 : 90 : world_default = ACL_NO_RIGHTS;
879 : 90 : owner_default = ACL_ALL_RIGHTS_FDW;
880 : 90 : break;
881 : 185 : case OBJECT_FOREIGN_SERVER:
882 : 185 : world_default = ACL_NO_RIGHTS;
883 : 185 : owner_default = ACL_ALL_RIGHTS_FOREIGN_SERVER;
884 : 185 : break;
885 : 145965 : case OBJECT_DOMAIN:
886 : : case OBJECT_TYPE:
887 : 145965 : world_default = ACL_USAGE;
888 : 145965 : owner_default = ACL_ALL_RIGHTS_TYPE;
889 : 145965 : break;
890 : 133 : case OBJECT_PARAMETER_ACL:
891 : 133 : world_default = ACL_NO_RIGHTS;
892 : 133 : owner_default = ACL_ALL_RIGHTS_PARAMETER_ACL;
893 : 133 : break;
894 : 0 : default:
895 [ # # ]: 0 : elog(ERROR, "unrecognized object type: %d", (int) objtype);
896 : : world_default = ACL_NO_RIGHTS; /* keep compiler quiet */
897 : : owner_default = ACL_NO_RIGHTS;
898 : : break;
899 : : }
900 : :
901 : 308812 : nacl = 0;
902 [ + + ]: 308812 : if (world_default != ACL_NO_RIGHTS)
903 : 181103 : nacl++;
904 [ + + ]: 308812 : if (owner_default != ACL_NO_RIGHTS)
905 : 280193 : nacl++;
906 : :
907 : 308812 : acl = allocacl(nacl);
908 [ - + ]: 308812 : aip = ACL_DAT(acl);
909 : :
910 [ + + ]: 308812 : if (world_default != ACL_NO_RIGHTS)
911 : : {
912 : 181103 : aip->ai_grantee = ACL_ID_PUBLIC;
913 : 181103 : aip->ai_grantor = ownerId;
914 : 181103 : ACLITEM_SET_PRIVS_GOPTIONS(*aip, world_default, ACL_NO_RIGHTS);
915 : 181103 : aip++;
916 : : }
917 : :
918 : : /*
919 : : * Note that the owner's entry shows all ordinary privileges but no grant
920 : : * options. This is because his grant options come "from the system" and
921 : : * not from his own efforts. (The SQL spec says that the owner's rights
922 : : * come from a "_SYSTEM" authid.) However, we do consider that the
923 : : * owner's ordinary privileges are self-granted; this lets him revoke
924 : : * them. We implement the owner's grant options without any explicit
925 : : * "_SYSTEM"-like ACL entry, by internally special-casing the owner
926 : : * wherever we are testing grant options.
927 : : */
928 [ + + ]: 308812 : if (owner_default != ACL_NO_RIGHTS)
929 : : {
930 : 280193 : aip->ai_grantee = ownerId;
931 : 280193 : aip->ai_grantor = ownerId;
932 : 280193 : ACLITEM_SET_PRIVS_GOPTIONS(*aip, owner_default, ACL_NO_RIGHTS);
933 : : }
934 : :
935 : 308812 : return acl;
936 : : }
937 : :
938 : :
939 : : /*
940 : : * SQL-accessible version of acldefault(). Hackish mapping from "char" type to
941 : : * OBJECT_* values.
942 : : */
943 : : Datum
944 : 232312 : acldefault_sql(PG_FUNCTION_ARGS)
945 : : {
946 : 232312 : char objtypec = PG_GETARG_CHAR(0);
947 : 232312 : Oid owner = PG_GETARG_OID(1);
948 : 232312 : ObjectType objtype = 0;
949 : :
950 [ - + + + : 232312 : switch (objtypec)
+ + + + +
+ + + +
- ]
951 : : {
952 : 0 : case 'c':
953 : 0 : objtype = OBJECT_COLUMN;
954 : 0 : break;
955 : 80998 : case 'r':
956 : 80998 : objtype = OBJECT_TABLE;
957 : 80998 : break;
958 : 650 : case 's':
959 : 650 : objtype = OBJECT_SEQUENCE;
960 : 650 : break;
961 : 94 : case 'd':
962 : 94 : objtype = OBJECT_DATABASE;
963 : 94 : break;
964 : 5375 : case 'f':
965 : 5375 : objtype = OBJECT_FUNCTION;
966 : 5375 : break;
967 : 241 : case 'l':
968 : 241 : objtype = OBJECT_LANGUAGE;
969 : 241 : break;
970 : 102 : case 'L':
971 : 102 : objtype = OBJECT_LARGEOBJECT;
972 : 102 : break;
973 : 1674 : case 'n':
974 : 1674 : objtype = OBJECT_SCHEMA;
975 : 1674 : break;
976 : 32 : case 'p':
977 : 32 : objtype = OBJECT_PARAMETER_ACL;
978 : 32 : break;
979 : 12 : case 't':
980 : 12 : objtype = OBJECT_TABLESPACE;
981 : 12 : break;
982 : 74 : case 'F':
983 : 74 : objtype = OBJECT_FDW;
984 : 74 : break;
985 : 78 : case 'S':
986 : 78 : objtype = OBJECT_FOREIGN_SERVER;
987 : 78 : break;
988 : 142982 : case 'T':
989 : 142982 : objtype = OBJECT_TYPE;
990 : 142982 : break;
991 : 0 : default:
992 : : /* Avoid printing non-ASCII bytes, else we have encoding issues */
993 [ # # # # ]: 0 : if (objtypec >= ' ' && objtypec < 0x7f)
994 [ # # ]: 0 : ereport(ERROR,
995 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
996 : : errmsg("unrecognized object type abbreviation: \"%c\"", objtypec)));
997 : : else /* use \ooo format, like charout() */
998 [ # # ]: 0 : ereport(ERROR,
999 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1000 : : errmsg("unrecognized object type abbreviation: \"\\%03o\"",
1001 : : (unsigned char) objtypec)));
1002 : : }
1003 : :
1004 : 232312 : PG_RETURN_ACL_P(acldefault(objtype, owner));
1005 : : }
1006 : :
1007 : :
1008 : : /*
1009 : : * Update an ACL array to add or remove specified privileges.
1010 : : *
1011 : : * old_acl: the input ACL array
1012 : : * mod_aip: defines the privileges to be added, removed, or substituted
1013 : : * modechg: ACL_MODECHG_ADD, ACL_MODECHG_DEL, or ACL_MODECHG_EQL
1014 : : * ownerId: Oid of object owner
1015 : : * behavior: RESTRICT or CASCADE behavior for recursive removal
1016 : : *
1017 : : * ownerid and behavior are only relevant when the update operation specifies
1018 : : * deletion of grant options.
1019 : : *
1020 : : * The result is a modified copy; the input object is not changed.
1021 : : *
1022 : : * NB: caller is responsible for having detoasted the input ACL, if needed.
1023 : : */
1024 : : Acl *
1025 : 41433 : aclupdate(const Acl *old_acl, const AclItem *mod_aip,
1026 : : int modechg, Oid ownerId, DropBehavior behavior)
1027 : : {
1028 : 41433 : Acl *new_acl = NULL;
1029 : : AclItem *old_aip,
1030 : 41433 : *new_aip = NULL;
1031 : : AclMode old_rights,
1032 : : old_goptions,
1033 : : new_rights,
1034 : : new_goptions;
1035 : : int dst,
1036 : : num;
1037 : :
1038 : : /* Caller probably already checked old_acl, but be safe */
1039 : 41433 : check_acl(old_acl);
1040 : :
1041 : : /* If granting grant options, check for circularity */
1042 [ + + ]: 41433 : if (modechg != ACL_MODECHG_DEL &&
1043 [ + + ]: 9879 : ACLITEM_GET_GOPTIONS(*mod_aip) != ACL_NO_RIGHTS)
1044 : 77 : check_circularity(old_acl, mod_aip, ownerId);
1045 : :
1046 : 41433 : num = ACL_NUM(old_acl);
1047 [ - + ]: 41433 : old_aip = ACL_DAT(old_acl);
1048 : :
1049 : : /*
1050 : : * Search the ACL for an existing entry for this grantee and grantor. If
1051 : : * one exists, just modify the entry in-place (well, in the same position,
1052 : : * since we actually return a copy); otherwise, insert the new entry at
1053 : : * the end.
1054 : : */
1055 : :
1056 [ + + ]: 54009 : for (dst = 0; dst < num; ++dst)
1057 : : {
1058 [ + + ]: 16434 : if (aclitem_match(mod_aip, old_aip + dst))
1059 : : {
1060 : : /* found a match, so modify existing item */
1061 : 3858 : new_acl = allocacl(num);
1062 [ - + ]: 3858 : new_aip = ACL_DAT(new_acl);
1063 : 3858 : memcpy(new_acl, old_acl, ACL_SIZE(old_acl));
1064 : 3858 : break;
1065 : : }
1066 : : }
1067 : :
1068 [ + + ]: 41433 : if (dst == num)
1069 : : {
1070 : : /* need to append a new item */
1071 : 37575 : new_acl = allocacl(num + 1);
1072 [ - + ]: 37575 : new_aip = ACL_DAT(new_acl);
1073 : 37575 : memcpy(new_aip, old_aip, num * sizeof(AclItem));
1074 : :
1075 : : /* initialize the new entry with no permissions */
1076 : 37575 : new_aip[dst].ai_grantee = mod_aip->ai_grantee;
1077 : 37575 : new_aip[dst].ai_grantor = mod_aip->ai_grantor;
1078 : 37575 : ACLITEM_SET_PRIVS_GOPTIONS(new_aip[dst],
1079 : : ACL_NO_RIGHTS, ACL_NO_RIGHTS);
1080 : 37575 : num++; /* set num to the size of new_acl */
1081 : : }
1082 : :
1083 : 41433 : old_rights = ACLITEM_GET_RIGHTS(new_aip[dst]);
1084 : 41433 : old_goptions = ACLITEM_GET_GOPTIONS(new_aip[dst]);
1085 : :
1086 : : /* apply the specified permissions change */
1087 [ + + - - ]: 41433 : switch (modechg)
1088 : : {
1089 : 9879 : case ACL_MODECHG_ADD:
1090 : 9879 : ACLITEM_SET_RIGHTS(new_aip[dst],
1091 : : old_rights | ACLITEM_GET_RIGHTS(*mod_aip));
1092 : 9879 : break;
1093 : 31554 : case ACL_MODECHG_DEL:
1094 : 31554 : ACLITEM_SET_RIGHTS(new_aip[dst],
1095 : : old_rights & ~ACLITEM_GET_RIGHTS(*mod_aip));
1096 : 31554 : break;
1097 : 0 : case ACL_MODECHG_EQL:
1098 : 0 : ACLITEM_SET_RIGHTS(new_aip[dst],
1099 : : ACLITEM_GET_RIGHTS(*mod_aip));
1100 : 0 : break;
1101 : : }
1102 : :
1103 : 41433 : new_rights = ACLITEM_GET_RIGHTS(new_aip[dst]);
1104 : 41433 : new_goptions = ACLITEM_GET_GOPTIONS(new_aip[dst]);
1105 : :
1106 : : /*
1107 : : * If the adjusted entry has no permissions, delete it from the list.
1108 : : */
1109 [ + + ]: 41433 : if (new_rights == ACL_NO_RIGHTS)
1110 : : {
1111 : 31328 : memmove(new_aip + dst,
1112 : 31328 : new_aip + dst + 1,
1113 : 31328 : (num - dst - 1) * sizeof(AclItem));
1114 : : /* Adjust array size to be 'num - 1' items */
1115 : 31328 : ARR_DIMS(new_acl)[0] = num - 1;
1116 : 31328 : SET_VARSIZE(new_acl, ACL_N_SIZE(num - 1));
1117 : : }
1118 : :
1119 : : /*
1120 : : * Remove abandoned privileges (cascading revoke). Currently we can only
1121 : : * handle this when the grantee is not PUBLIC.
1122 : : */
1123 [ + + ]: 41433 : if ((old_goptions & ~new_goptions) != 0)
1124 : : {
1125 : : Assert(mod_aip->ai_grantee != ACL_ID_PUBLIC);
1126 : 59 : new_acl = recursive_revoke(new_acl, mod_aip->ai_grantee,
1127 : 59 : (old_goptions & ~new_goptions),
1128 : : ownerId, behavior);
1129 : : }
1130 : :
1131 : 41425 : return new_acl;
1132 : : }
1133 : :
1134 : : /*
1135 : : * Update an ACL array to reflect a change of owner to the parent object
1136 : : *
1137 : : * old_acl: the input ACL array (must not be NULL)
1138 : : * oldOwnerId: Oid of the old object owner
1139 : : * newOwnerId: Oid of the new object owner
1140 : : *
1141 : : * The result is a modified copy; the input object is not changed.
1142 : : *
1143 : : * NB: caller is responsible for having detoasted the input ACL, if needed.
1144 : : *
1145 : : * Note: the name of this function is a bit of a misnomer, since it will
1146 : : * happily make the specified role substitution whether the old role is
1147 : : * really the owner of the parent object or merely mentioned in its ACL.
1148 : : * But the vast majority of callers use it in connection with ALTER OWNER
1149 : : * operations, so we'll keep the name.
1150 : : */
1151 : : Acl *
1152 : 72 : aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
1153 : : {
1154 : : Acl *new_acl;
1155 : : AclItem *new_aip;
1156 : : AclItem *old_aip;
1157 : : AclItem *dst_aip;
1158 : : AclItem *src_aip;
1159 : : AclItem *targ_aip;
1160 : 72 : bool newpresent = false;
1161 : : int dst,
1162 : : src,
1163 : : targ,
1164 : : num;
1165 : :
1166 : 72 : check_acl(old_acl);
1167 : :
1168 : : /*
1169 : : * Make a copy of the given ACL, substituting new owner ID for old
1170 : : * wherever it appears as either grantor or grantee. Also note if the new
1171 : : * owner ID is already present.
1172 : : */
1173 : 72 : num = ACL_NUM(old_acl);
1174 [ - + ]: 72 : old_aip = ACL_DAT(old_acl);
1175 : 72 : new_acl = allocacl(num);
1176 [ - + ]: 72 : new_aip = ACL_DAT(new_acl);
1177 : 72 : memcpy(new_aip, old_aip, num * sizeof(AclItem));
1178 [ + + ]: 198 : for (dst = 0, dst_aip = new_aip; dst < num; dst++, dst_aip++)
1179 : : {
1180 [ + - ]: 126 : if (dst_aip->ai_grantor == oldOwnerId)
1181 : 126 : dst_aip->ai_grantor = newOwnerId;
1182 [ # # ]: 0 : else if (dst_aip->ai_grantor == newOwnerId)
1183 : 0 : newpresent = true;
1184 [ + + ]: 126 : if (dst_aip->ai_grantee == oldOwnerId)
1185 : 70 : dst_aip->ai_grantee = newOwnerId;
1186 [ + + ]: 56 : else if (dst_aip->ai_grantee == newOwnerId)
1187 : 13 : newpresent = true;
1188 : : }
1189 : :
1190 : : /*
1191 : : * If the old ACL contained any references to the new owner, then we may
1192 : : * now have generated an ACL containing duplicate entries. Find them and
1193 : : * merge them so that there are not duplicates. (This is relatively
1194 : : * expensive since we use a stupid O(N^2) algorithm, but it's unlikely to
1195 : : * be the normal case.)
1196 : : *
1197 : : * To simplify deletion of duplicate entries, we temporarily leave them in
1198 : : * the array but set their privilege masks to zero; when we reach such an
1199 : : * entry it's just skipped. (Thus, a side effect of this code will be to
1200 : : * remove privilege-free entries, should there be any in the input.) dst
1201 : : * is the next output slot, targ is the currently considered input slot
1202 : : * (always >= dst), and src scans entries to the right of targ looking for
1203 : : * duplicates. Once an entry has been emitted to dst it is known
1204 : : * duplicate-free and need not be considered anymore.
1205 : : */
1206 [ + + ]: 72 : if (newpresent)
1207 : : {
1208 : 13 : dst = 0;
1209 [ + + ]: 39 : for (targ = 0, targ_aip = new_aip; targ < num; targ++, targ_aip++)
1210 : : {
1211 : : /* ignore if deleted in an earlier pass */
1212 [ + + ]: 26 : if (ACLITEM_GET_RIGHTS(*targ_aip) == ACL_NO_RIGHTS)
1213 : 13 : continue;
1214 : : /* find and merge any duplicates */
1215 [ + + ]: 26 : for (src = targ + 1, src_aip = targ_aip + 1; src < num;
1216 : 13 : src++, src_aip++)
1217 : : {
1218 [ - + ]: 13 : if (ACLITEM_GET_RIGHTS(*src_aip) == ACL_NO_RIGHTS)
1219 : 0 : continue;
1220 [ + - ]: 13 : if (aclitem_match(targ_aip, src_aip))
1221 : : {
1222 : 13 : ACLITEM_SET_RIGHTS(*targ_aip,
1223 : : ACLITEM_GET_RIGHTS(*targ_aip) |
1224 : : ACLITEM_GET_RIGHTS(*src_aip));
1225 : : /* mark the duplicate deleted */
1226 : 13 : ACLITEM_SET_RIGHTS(*src_aip, ACL_NO_RIGHTS);
1227 : : }
1228 : : }
1229 : : /* and emit to output */
1230 : 13 : new_aip[dst] = *targ_aip;
1231 : 13 : dst++;
1232 : : }
1233 : : /* Adjust array size to be 'dst' items */
1234 : 13 : ARR_DIMS(new_acl)[0] = dst;
1235 : 13 : SET_VARSIZE(new_acl, ACL_N_SIZE(dst));
1236 : : }
1237 : :
1238 : 72 : return new_acl;
1239 : : }
1240 : :
1241 : :
1242 : : /*
1243 : : * When granting grant options, we must disallow attempts to set up circular
1244 : : * chains of grant options. Suppose A (the object owner) grants B some
1245 : : * privileges with grant option, and B re-grants them to C. If C could
1246 : : * grant the privileges to B as well, then A would be unable to effectively
1247 : : * revoke the privileges from B, since recursive_revoke would consider that
1248 : : * B still has 'em from C.
1249 : : *
1250 : : * We check for this by recursively deleting all grant options belonging to
1251 : : * the target grantee, and then seeing if the would-be grantor still has the
1252 : : * grant option or not.
1253 : : */
1254 : : static void
1255 : 77 : check_circularity(const Acl *old_acl, const AclItem *mod_aip,
1256 : : Oid ownerId)
1257 : : {
1258 : : Acl *acl;
1259 : : AclItem *aip;
1260 : : int i,
1261 : : num;
1262 : : AclMode own_privs;
1263 : :
1264 : 77 : check_acl(old_acl);
1265 : :
1266 : : /*
1267 : : * For now, grant options can only be granted to roles, not PUBLIC.
1268 : : * Otherwise we'd have to work a bit harder here.
1269 : : */
1270 : : Assert(mod_aip->ai_grantee != ACL_ID_PUBLIC);
1271 : :
1272 : : /* The owner always has grant options, no need to check */
1273 [ + + ]: 77 : if (mod_aip->ai_grantor == ownerId)
1274 : 65 : return;
1275 : :
1276 : : /* Make a working copy */
1277 : 12 : acl = allocacl(ACL_NUM(old_acl));
1278 : 12 : memcpy(acl, old_acl, ACL_SIZE(old_acl));
1279 : :
1280 : : /* Zap all grant options of target grantee, plus what depends on 'em */
1281 : 16 : cc_restart:
1282 : 16 : num = ACL_NUM(acl);
1283 [ - + ]: 16 : aip = ACL_DAT(acl);
1284 [ + + ]: 64 : for (i = 0; i < num; i++)
1285 : : {
1286 [ + + ]: 52 : if (aip[i].ai_grantee == mod_aip->ai_grantee &&
1287 [ + - ]: 4 : ACLITEM_GET_GOPTIONS(aip[i]) != ACL_NO_RIGHTS)
1288 : : {
1289 : : Acl *new_acl;
1290 : :
1291 : : /* We'll actually zap ordinary privs too, but no matter */
1292 : 4 : new_acl = aclupdate(acl, &aip[i], ACL_MODECHG_DEL,
1293 : : ownerId, DROP_CASCADE);
1294 : :
1295 : 4 : pfree(acl);
1296 : 4 : acl = new_acl;
1297 : :
1298 : 4 : goto cc_restart;
1299 : : }
1300 : : }
1301 : :
1302 : : /* Now we can compute grantor's independently-derived privileges */
1303 : 12 : own_privs = aclmask(acl,
1304 : 12 : mod_aip->ai_grantor,
1305 : : ownerId,
1306 : 12 : ACL_GRANT_OPTION_FOR(ACLITEM_GET_GOPTIONS(*mod_aip)),
1307 : : ACLMASK_ALL);
1308 : 12 : own_privs = ACL_OPTION_TO_PRIVS(own_privs);
1309 : :
1310 [ - + ]: 12 : if ((ACLITEM_GET_GOPTIONS(*mod_aip) & ~own_privs) != 0)
1311 [ # # ]: 0 : ereport(ERROR,
1312 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1313 : : errmsg("grant options cannot be granted back to your own grantor")));
1314 : :
1315 : 12 : pfree(acl);
1316 : : }
1317 : :
1318 : :
1319 : : /*
1320 : : * Ensure that no privilege is "abandoned". A privilege is abandoned
1321 : : * if the user that granted the privilege loses the grant option. (So
1322 : : * the chain through which it was granted is broken.) Either the
1323 : : * abandoned privileges are revoked as well, or an error message is
1324 : : * printed, depending on the drop behavior option.
1325 : : *
1326 : : * acl: the input ACL list
1327 : : * grantee: the user from whom some grant options have been revoked
1328 : : * revoke_privs: the grant options being revoked
1329 : : * ownerId: Oid of object owner
1330 : : * behavior: RESTRICT or CASCADE behavior for recursive removal
1331 : : *
1332 : : * The input Acl object is pfree'd if replaced.
1333 : : */
1334 : : static Acl *
1335 : 59 : recursive_revoke(Acl *acl,
1336 : : Oid grantee,
1337 : : AclMode revoke_privs,
1338 : : Oid ownerId,
1339 : : DropBehavior behavior)
1340 : : {
1341 : : AclMode still_has;
1342 : : AclItem *aip;
1343 : : int i,
1344 : : num;
1345 : :
1346 : 59 : check_acl(acl);
1347 : :
1348 : : /* The owner can never truly lose grant options, so short-circuit */
1349 [ - + ]: 59 : if (grantee == ownerId)
1350 : 0 : return acl;
1351 : :
1352 : : /* The grantee might still have some grant options via another grantor */
1353 : 59 : still_has = aclmask(acl, grantee, ownerId,
1354 : : ACL_GRANT_OPTION_FOR(revoke_privs),
1355 : : ACLMASK_ALL);
1356 : 59 : revoke_privs &= ~ACL_OPTION_TO_PRIVS(still_has);
1357 [ + + ]: 59 : if (revoke_privs == ACL_NO_RIGHTS)
1358 : 4 : return acl;
1359 : :
1360 : 55 : restart:
1361 : 79 : num = ACL_NUM(acl);
1362 [ - + ]: 79 : aip = ACL_DAT(acl);
1363 [ + + ]: 256 : for (i = 0; i < num; i++)
1364 : : {
1365 [ + + ]: 209 : if (aip[i].ai_grantor == grantee
1366 [ + - ]: 32 : && (ACLITEM_GET_PRIVS(aip[i]) & revoke_privs) != 0)
1367 : : {
1368 : : AclItem mod_acl;
1369 : : Acl *new_acl;
1370 : :
1371 [ + + ]: 32 : if (behavior == DROP_RESTRICT)
1372 [ + - ]: 8 : ereport(ERROR,
1373 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1374 : : errmsg("dependent privileges exist"),
1375 : : errhint("Use CASCADE to revoke them too.")));
1376 : :
1377 : 24 : mod_acl.ai_grantor = grantee;
1378 : 24 : mod_acl.ai_grantee = aip[i].ai_grantee;
1379 : 24 : ACLITEM_SET_PRIVS_GOPTIONS(mod_acl,
1380 : : revoke_privs,
1381 : : revoke_privs);
1382 : :
1383 : 24 : new_acl = aclupdate(acl, &mod_acl, ACL_MODECHG_DEL,
1384 : : ownerId, behavior);
1385 : :
1386 : 24 : pfree(acl);
1387 : 24 : acl = new_acl;
1388 : :
1389 : 24 : goto restart;
1390 : : }
1391 : : }
1392 : :
1393 : 47 : return acl;
1394 : : }
1395 : :
1396 : :
1397 : : /*
1398 : : * aclmask --- compute bitmask of all privileges held by roleid.
1399 : : *
1400 : : * When 'how' = ACLMASK_ALL, this simply returns the privilege bits
1401 : : * held by the given roleid according to the given ACL list, ANDed
1402 : : * with 'mask'. (The point of passing 'mask' is to let the routine
1403 : : * exit early if all privileges of interest have been found.)
1404 : : *
1405 : : * When 'how' = ACLMASK_ANY, returns as soon as any bit in the mask
1406 : : * is known true. (This lets us exit soonest in cases where the
1407 : : * caller is only going to test for zero or nonzero result.)
1408 : : *
1409 : : * Usage patterns:
1410 : : *
1411 : : * To see if any of a set of privileges are held:
1412 : : * if (aclmask(acl, roleid, ownerId, privs, ACLMASK_ANY) != 0)
1413 : : *
1414 : : * To see if all of a set of privileges are held:
1415 : : * if (aclmask(acl, roleid, ownerId, privs, ACLMASK_ALL) == privs)
1416 : : *
1417 : : * To determine exactly which of a set of privileges are held:
1418 : : * heldprivs = aclmask(acl, roleid, ownerId, privs, ACLMASK_ALL);
1419 : : */
1420 : : AclMode
1421 : 80621 : aclmask(const Acl *acl, Oid roleid, Oid ownerId,
1422 : : AclMode mask, AclMaskHow how)
1423 : : {
1424 : : AclMode result;
1425 : : AclMode remaining;
1426 : : AclItem *aidat;
1427 : : int i,
1428 : : num;
1429 : :
1430 : : /*
1431 : : * Null ACL should not happen, since caller should have inserted
1432 : : * appropriate default
1433 : : */
1434 [ - + ]: 80621 : if (acl == NULL)
1435 [ # # ]: 0 : elog(ERROR, "null ACL");
1436 : :
1437 : 80621 : check_acl(acl);
1438 : :
1439 : : /* Quick exit for mask == 0 */
1440 [ + + ]: 80621 : if (mask == 0)
1441 : 46 : return 0;
1442 : :
1443 : 80575 : result = 0;
1444 : :
1445 : : /* Owner always implicitly has all grant options */
1446 [ + + + + ]: 80699 : if ((mask & ACLITEM_ALL_GOPTION_BITS) &&
1447 : 124 : has_privs_of_role(roleid, ownerId))
1448 : : {
1449 : 4 : result = mask & ACLITEM_ALL_GOPTION_BITS;
1450 [ - + + - ]: 4 : if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1451 : 4 : return result;
1452 : : }
1453 : :
1454 : 80571 : num = ACL_NUM(acl);
1455 [ - + ]: 80571 : aidat = ACL_DAT(acl);
1456 : :
1457 : : /*
1458 : : * Check privileges granted directly to roleid or to public
1459 : : */
1460 [ + + ]: 125036 : for (i = 0; i < num; i++)
1461 : : {
1462 : 115934 : AclItem *aidata = &aidat[i];
1463 : :
1464 [ + + ]: 115934 : if (aidata->ai_grantee == ACL_ID_PUBLIC ||
1465 [ + + ]: 54772 : aidata->ai_grantee == roleid)
1466 : : {
1467 : 73607 : result |= aidata->ai_privs & mask;
1468 [ + + + + ]: 73607 : if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1469 : 71469 : return result;
1470 : : }
1471 : : }
1472 : :
1473 : : /*
1474 : : * Check privileges granted indirectly via role memberships. We do this in
1475 : : * a separate pass to minimize expensive indirect membership tests. In
1476 : : * particular, it's worth testing whether a given ACL entry grants any
1477 : : * privileges still of interest before we perform the has_privs_of_role
1478 : : * test.
1479 : : */
1480 : 9102 : remaining = mask & ~result;
1481 [ + + ]: 21171 : for (i = 0; i < num; i++)
1482 : : {
1483 : 12198 : AclItem *aidata = &aidat[i];
1484 : :
1485 [ + + ]: 12198 : if (aidata->ai_grantee == ACL_ID_PUBLIC ||
1486 [ + + ]: 11653 : aidata->ai_grantee == roleid)
1487 : 2035 : continue; /* already checked it */
1488 : :
1489 [ + + + + ]: 19705 : if ((aidata->ai_privs & remaining) &&
1490 : 9542 : has_privs_of_role(roleid, aidata->ai_grantee))
1491 : : {
1492 : 129 : result |= aidata->ai_privs & mask;
1493 [ + + + - ]: 129 : if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1494 : 129 : return result;
1495 : 0 : remaining = mask & ~result;
1496 : : }
1497 : : }
1498 : :
1499 : 8973 : return result;
1500 : : }
1501 : :
1502 : :
1503 : : /*
1504 : : * aclmask_direct --- compute bitmask of all privileges held by roleid.
1505 : : *
1506 : : * This is exactly like aclmask() except that we consider only privileges
1507 : : * held *directly* by roleid, not those inherited via role membership.
1508 : : */
1509 : : static AclMode
1510 : 380 : aclmask_direct(const Acl *acl, Oid roleid, Oid ownerId,
1511 : : AclMode mask, AclMaskHow how)
1512 : : {
1513 : : AclMode result;
1514 : : AclItem *aidat;
1515 : : int i,
1516 : : num;
1517 : :
1518 : : /*
1519 : : * Null ACL should not happen, since caller should have inserted
1520 : : * appropriate default
1521 : : */
1522 [ - + ]: 380 : if (acl == NULL)
1523 [ # # ]: 0 : elog(ERROR, "null ACL");
1524 : :
1525 : 380 : check_acl(acl);
1526 : :
1527 : : /* Quick exit for mask == 0 */
1528 [ - + ]: 380 : if (mask == 0)
1529 : 0 : return 0;
1530 : :
1531 : 380 : result = 0;
1532 : :
1533 : : /* Owner always implicitly has all grant options */
1534 [ + - + + ]: 380 : if ((mask & ACLITEM_ALL_GOPTION_BITS) &&
1535 : : roleid == ownerId)
1536 : : {
1537 : 8 : result = mask & ACLITEM_ALL_GOPTION_BITS;
1538 [ + - + - ]: 8 : if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1539 : 8 : return result;
1540 : : }
1541 : :
1542 : 372 : num = ACL_NUM(acl);
1543 [ - + ]: 372 : aidat = ACL_DAT(acl);
1544 : :
1545 : : /*
1546 : : * Check privileges granted directly to roleid (and not to public)
1547 : : */
1548 [ + + ]: 1616 : for (i = 0; i < num; i++)
1549 : : {
1550 : 1384 : AclItem *aidata = &aidat[i];
1551 : :
1552 [ + + ]: 1384 : if (aidata->ai_grantee == roleid)
1553 : : {
1554 : 296 : result |= aidata->ai_privs & mask;
1555 [ + - + + ]: 296 : if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1556 : 140 : return result;
1557 : : }
1558 : : }
1559 : :
1560 : 232 : return result;
1561 : : }
1562 : :
1563 : :
1564 : : /*
1565 : : * aclmembers
1566 : : * Find out all the roleids mentioned in an Acl.
1567 : : * Note that we do not distinguish grantors from grantees.
1568 : : *
1569 : : * *roleids is set to point to a palloc'd array containing distinct OIDs
1570 : : * in sorted order. The length of the array is the function result.
1571 : : */
1572 : : int
1573 : 49760 : aclmembers(const Acl *acl, Oid **roleids)
1574 : : {
1575 : : Oid *list;
1576 : : const AclItem *acldat;
1577 : : int i,
1578 : : j;
1579 : :
1580 [ + + + + ]: 49760 : if (acl == NULL || ACL_NUM(acl) == 0)
1581 : : {
1582 : 28454 : *roleids = NULL;
1583 : 28454 : return 0;
1584 : : }
1585 : :
1586 : 21306 : check_acl(acl);
1587 : :
1588 : : /* Allocate the worst-case space requirement */
1589 : 21306 : list = palloc_array(Oid, ACL_NUM(acl) * 2);
1590 [ - + ]: 21306 : acldat = ACL_DAT(acl);
1591 : :
1592 : : /*
1593 : : * Walk the ACL collecting mentioned RoleIds.
1594 : : */
1595 : 21306 : j = 0;
1596 [ + + ]: 55358 : for (i = 0; i < ACL_NUM(acl); i++)
1597 : : {
1598 : 34052 : const AclItem *ai = &acldat[i];
1599 : :
1600 [ + + ]: 34052 : if (ai->ai_grantee != ACL_ID_PUBLIC)
1601 : 23356 : list[j++] = ai->ai_grantee;
1602 : : /* grantor is currently never PUBLIC, but let's check anyway */
1603 [ + - ]: 34052 : if (ai->ai_grantor != ACL_ID_PUBLIC)
1604 : 34052 : list[j++] = ai->ai_grantor;
1605 : : }
1606 : :
1607 : : /* Sort the array */
1608 : 21306 : qsort(list, j, sizeof(Oid), oid_cmp);
1609 : :
1610 : : /*
1611 : : * We could repalloc the array down to minimum size, but it's hardly worth
1612 : : * it since it's only transient memory.
1613 : : */
1614 : 21306 : *roleids = list;
1615 : :
1616 : : /* Remove duplicates from the array */
1617 : 21306 : return qunique(list, j, sizeof(Oid), oid_cmp);
1618 : : }
1619 : :
1620 : :
1621 : : /*
1622 : : * aclinsert (exported function)
1623 : : */
1624 : : Datum
1625 : 0 : aclinsert(PG_FUNCTION_ARGS)
1626 : : {
1627 [ # # ]: 0 : ereport(ERROR,
1628 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1629 : : errmsg("aclinsert is no longer supported")));
1630 : :
1631 : : PG_RETURN_NULL(); /* keep compiler quiet */
1632 : : }
1633 : :
1634 : : Datum
1635 : 0 : aclremove(PG_FUNCTION_ARGS)
1636 : : {
1637 [ # # ]: 0 : ereport(ERROR,
1638 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1639 : : errmsg("aclremove is no longer supported")));
1640 : :
1641 : : PG_RETURN_NULL(); /* keep compiler quiet */
1642 : : }
1643 : :
1644 : : Datum
1645 : 0 : aclcontains(PG_FUNCTION_ARGS)
1646 : : {
1647 : 0 : Acl *acl = PG_GETARG_ACL_P(0);
1648 : 0 : AclItem *aip = PG_GETARG_ACLITEM_P(1);
1649 : : AclItem *aidat;
1650 : : int i,
1651 : : num;
1652 : :
1653 : 0 : check_acl(acl);
1654 : 0 : num = ACL_NUM(acl);
1655 [ # # ]: 0 : aidat = ACL_DAT(acl);
1656 [ # # ]: 0 : for (i = 0; i < num; ++i)
1657 : : {
1658 [ # # ]: 0 : if (aip->ai_grantee == aidat[i].ai_grantee &&
1659 [ # # ]: 0 : aip->ai_grantor == aidat[i].ai_grantor &&
1660 [ # # ]: 0 : (ACLITEM_GET_RIGHTS(*aip) & ACLITEM_GET_RIGHTS(aidat[i])) == ACLITEM_GET_RIGHTS(*aip))
1661 : 0 : PG_RETURN_BOOL(true);
1662 : : }
1663 : 0 : PG_RETURN_BOOL(false);
1664 : : }
1665 : :
1666 : : Datum
1667 : 24 : makeaclitem(PG_FUNCTION_ARGS)
1668 : : {
1669 : 24 : Oid grantee = PG_GETARG_OID(0);
1670 : 24 : Oid grantor = PG_GETARG_OID(1);
1671 : 24 : text *privtext = PG_GETARG_TEXT_PP(2);
1672 : 24 : bool goption = PG_GETARG_BOOL(3);
1673 : : AclItem *result;
1674 : : AclMode priv;
1675 : : static const priv_map any_priv_map[] = {
1676 : : {"SELECT", ACL_SELECT},
1677 : : {"INSERT", ACL_INSERT},
1678 : : {"UPDATE", ACL_UPDATE},
1679 : : {"DELETE", ACL_DELETE},
1680 : : {"TRUNCATE", ACL_TRUNCATE},
1681 : : {"REFERENCES", ACL_REFERENCES},
1682 : : {"TRIGGER", ACL_TRIGGER},
1683 : : {"EXECUTE", ACL_EXECUTE},
1684 : : {"USAGE", ACL_USAGE},
1685 : : {"CREATE", ACL_CREATE},
1686 : : {"TEMP", ACL_CREATE_TEMP},
1687 : : {"TEMPORARY", ACL_CREATE_TEMP},
1688 : : {"CONNECT", ACL_CONNECT},
1689 : : {"SET", ACL_SET},
1690 : : {"ALTER SYSTEM", ACL_ALTER_SYSTEM},
1691 : : {"MAINTAIN", ACL_MAINTAIN},
1692 : : {NULL, 0}
1693 : : };
1694 : :
1695 : 24 : priv = convert_any_priv_string(privtext, any_priv_map);
1696 : :
1697 : 20 : result = palloc_object(AclItem);
1698 : :
1699 : 20 : result->ai_grantee = grantee;
1700 : 20 : result->ai_grantor = grantor;
1701 : :
1702 [ + + ]: 20 : ACLITEM_SET_PRIVS_GOPTIONS(*result, priv,
1703 : : (goption ? priv : ACL_NO_RIGHTS));
1704 : :
1705 : 20 : PG_RETURN_ACLITEM_P(result);
1706 : : }
1707 : :
1708 : :
1709 : : /*
1710 : : * convert_any_priv_string: recognize privilege strings for has_foo_privilege
1711 : : *
1712 : : * We accept a comma-separated list of case-insensitive privilege names,
1713 : : * producing a bitmask of the OR'd privilege bits. We are liberal about
1714 : : * whitespace between items, not so much about whitespace within items.
1715 : : * The allowed privilege names are given as an array of priv_map structs,
1716 : : * terminated by one with a NULL name pointer.
1717 : : */
1718 : : static AclMode
1719 : 67981 : convert_any_priv_string(text *priv_type_text,
1720 : : const priv_map *privileges)
1721 : : {
1722 : 67981 : AclMode result = 0;
1723 : 67981 : char *priv_type = text_to_cstring(priv_type_text);
1724 : : char *chunk;
1725 : : char *next_chunk;
1726 : :
1727 : : /* We rely on priv_type being a private, modifiable string */
1728 [ + + ]: 135969 : for (chunk = priv_type; chunk; chunk = next_chunk)
1729 : : {
1730 : : int chunk_len;
1731 : : const priv_map *this_priv;
1732 : :
1733 : : /* Split string at commas */
1734 : 68007 : next_chunk = strchr(chunk, ',');
1735 [ + + ]: 68007 : if (next_chunk)
1736 : 28 : *next_chunk++ = '\0';
1737 : :
1738 : : /* Drop leading/trailing whitespace in this chunk */
1739 [ + - + + ]: 68038 : while (*chunk && isspace((unsigned char) *chunk))
1740 : 31 : chunk++;
1741 : 68007 : chunk_len = strlen(chunk);
1742 [ + - + + ]: 68022 : while (chunk_len > 0 && isspace((unsigned char) chunk[chunk_len - 1]))
1743 : 15 : chunk_len--;
1744 : 68007 : chunk[chunk_len] = '\0';
1745 : :
1746 : : /* Match to the privileges list */
1747 [ + + ]: 69311 : for (this_priv = privileges; this_priv->name; this_priv++)
1748 : : {
1749 [ + + ]: 69292 : if (pg_strcasecmp(this_priv->name, chunk) == 0)
1750 : : {
1751 : 67988 : result |= this_priv->value;
1752 : 67988 : break;
1753 : : }
1754 : : }
1755 [ + + ]: 68007 : if (!this_priv->name)
1756 [ + - ]: 19 : ereport(ERROR,
1757 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1758 : : errmsg("unrecognized privilege type: \"%s\"", chunk)));
1759 : : }
1760 : :
1761 : 67962 : pfree(priv_type);
1762 : 67962 : return result;
1763 : : }
1764 : :
1765 : :
1766 : : static const char *
1767 : 311532 : convert_aclright_to_string(int aclright)
1768 : : {
1769 [ + + + + : 311532 : switch (aclright)
+ + + + +
- - - - -
+ - ]
1770 : : {
1771 : 37047 : case ACL_INSERT:
1772 : 37047 : return "INSERT";
1773 : 51845 : case ACL_SELECT:
1774 : 51845 : return "SELECT";
1775 : 37328 : case ACL_UPDATE:
1776 : 37328 : return "UPDATE";
1777 : 36975 : case ACL_DELETE:
1778 : 36975 : return "DELETE";
1779 : 37047 : case ACL_TRUNCATE:
1780 : 37047 : return "TRUNCATE";
1781 : 37047 : case ACL_REFERENCES:
1782 : 37047 : return "REFERENCES";
1783 : 37047 : case ACL_TRIGGER:
1784 : 37047 : return "TRIGGER";
1785 : 41 : case ACL_EXECUTE:
1786 : 41 : return "EXECUTE";
1787 : 108 : case ACL_USAGE:
1788 : 108 : return "USAGE";
1789 : 0 : case ACL_CREATE:
1790 : 0 : return "CREATE";
1791 : 0 : case ACL_CREATE_TEMP:
1792 : 0 : return "TEMPORARY";
1793 : 0 : case ACL_CONNECT:
1794 : 0 : return "CONNECT";
1795 : 0 : case ACL_SET:
1796 : 0 : return "SET";
1797 : 0 : case ACL_ALTER_SYSTEM:
1798 : 0 : return "ALTER SYSTEM";
1799 : 37047 : case ACL_MAINTAIN:
1800 : 37047 : return "MAINTAIN";
1801 : 0 : default:
1802 [ # # ]: 0 : elog(ERROR, "unrecognized aclright: %d", aclright);
1803 : : return NULL;
1804 : : }
1805 : : }
1806 : :
1807 : :
1808 : : /*----------
1809 : : * Convert an aclitem[] to a table.
1810 : : *
1811 : : * Example:
1812 : : *
1813 : : * aclexplode('{=r/joe,foo=a*w/joe}'::aclitem[])
1814 : : *
1815 : : * returns the table
1816 : : *
1817 : : * {{ OID(joe), 0::OID, 'SELECT', false },
1818 : : * { OID(joe), OID(foo), 'INSERT', true },
1819 : : * { OID(joe), OID(foo), 'UPDATE', false }}
1820 : : *----------
1821 : : */
1822 : : Datum
1823 : 348656 : aclexplode(PG_FUNCTION_ARGS)
1824 : : {
1825 : 348656 : Acl *acl = PG_GETARG_ACL_P(0);
1826 : : FuncCallContext *funcctx;
1827 : : int *idx;
1828 : : AclItem *aidat;
1829 : :
1830 [ + + ]: 348656 : if (SRF_IS_FIRSTCALL())
1831 : : {
1832 : : TupleDesc tupdesc;
1833 : : MemoryContext oldcontext;
1834 : :
1835 : 37124 : check_acl(acl);
1836 : :
1837 : 37124 : funcctx = SRF_FIRSTCALL_INIT();
1838 : 37124 : oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1839 : :
1840 : : /*
1841 : : * build tupdesc for result tuples (matches out parameters in pg_proc
1842 : : * entry)
1843 : : */
1844 : 37124 : tupdesc = CreateTemplateTupleDesc(4);
1845 : 37124 : TupleDescInitEntry(tupdesc, (AttrNumber) 1, "grantor",
1846 : : OIDOID, -1, 0);
1847 : 37124 : TupleDescInitEntry(tupdesc, (AttrNumber) 2, "grantee",
1848 : : OIDOID, -1, 0);
1849 : 37124 : TupleDescInitEntry(tupdesc, (AttrNumber) 3, "privilege_type",
1850 : : TEXTOID, -1, 0);
1851 : 37124 : TupleDescInitEntry(tupdesc, (AttrNumber) 4, "is_grantable",
1852 : : BOOLOID, -1, 0);
1853 : :
1854 : 37124 : TupleDescFinalize(tupdesc);
1855 : 37124 : funcctx->tuple_desc = BlessTupleDesc(tupdesc);
1856 : :
1857 : : /* allocate memory for user context */
1858 : 37124 : idx = palloc_array(int, 2);
1859 : 37124 : idx[0] = 0; /* ACL array item index */
1860 : 37124 : idx[1] = -1; /* privilege type counter */
1861 : 37124 : funcctx->user_fctx = idx;
1862 : :
1863 : 37124 : MemoryContextSwitchTo(oldcontext);
1864 : : }
1865 : :
1866 : 348656 : funcctx = SRF_PERCALL_SETUP();
1867 : 348656 : idx = (int *) funcctx->user_fctx;
1868 [ - + ]: 348656 : aidat = ACL_DAT(acl);
1869 : :
1870 : : /* need test here in case acl has no items */
1871 [ + - ]: 817859 : while (idx[0] < ACL_NUM(acl))
1872 : : {
1873 : : AclItem *aidata;
1874 : : AclMode priv_bit;
1875 : :
1876 : 817859 : idx[1]++;
1877 [ + + ]: 817859 : if (idx[1] == N_ACL_RIGHTS)
1878 : : {
1879 : 52049 : idx[1] = 0;
1880 : 52049 : idx[0]++;
1881 [ + + ]: 52049 : if (idx[0] >= ACL_NUM(acl)) /* done */
1882 : 37124 : break;
1883 : : }
1884 : 780735 : aidata = &aidat[idx[0]];
1885 : 780735 : priv_bit = UINT64CONST(1) << idx[1];
1886 : :
1887 [ + + ]: 780735 : if (ACLITEM_GET_PRIVS(*aidata) & priv_bit)
1888 : : {
1889 : : Datum result;
1890 : : Datum values[4];
1891 : 311532 : bool nulls[4] = {0};
1892 : : HeapTuple tuple;
1893 : :
1894 : 311532 : values[0] = ObjectIdGetDatum(aidata->ai_grantor);
1895 : 311532 : values[1] = ObjectIdGetDatum(aidata->ai_grantee);
1896 : 311532 : values[2] = CStringGetTextDatum(convert_aclright_to_string(priv_bit));
1897 : 311532 : values[3] = BoolGetDatum((ACLITEM_GET_GOPTIONS(*aidata) & priv_bit) != 0);
1898 : :
1899 : 311532 : tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
1900 : 311532 : result = HeapTupleGetDatum(tuple);
1901 : :
1902 : 311532 : SRF_RETURN_NEXT(funcctx, result);
1903 : : }
1904 : : }
1905 : :
1906 : 37124 : SRF_RETURN_DONE(funcctx);
1907 : : }
1908 : :
1909 : :
1910 : : /*
1911 : : * has_table_privilege variants
1912 : : * These are all named "has_table_privilege" at the SQL level.
1913 : : * They take various combinations of relation name, relation OID,
1914 : : * user name, user OID, or implicit user = current_user.
1915 : : *
1916 : : * The result is a boolean value: true if user has the indicated
1917 : : * privilege, false if not. The variants that take a relation OID
1918 : : * return NULL if the OID doesn't exist (rather than failing, as
1919 : : * they did before Postgres 8.4).
1920 : : */
1921 : :
1922 : : /*
1923 : : * has_table_privilege_name_name
1924 : : * Check user privileges on a table given
1925 : : * name username, text tablename, and text priv name.
1926 : : */
1927 : : Datum
1928 : 144 : has_table_privilege_name_name(PG_FUNCTION_ARGS)
1929 : : {
1930 : 144 : Name rolename = PG_GETARG_NAME(0);
1931 : 144 : text *tablename = PG_GETARG_TEXT_PP(1);
1932 : 144 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
1933 : : Oid roleid;
1934 : : Oid tableoid;
1935 : : AclMode mode;
1936 : : AclResult aclresult;
1937 : :
1938 : 144 : roleid = get_role_oid_or_public(NameStr(*rolename));
1939 : 140 : tableoid = convert_table_name(tablename);
1940 : 140 : mode = convert_table_priv_string(priv_type_text);
1941 : :
1942 : 140 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
1943 : :
1944 : 140 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
1945 : : }
1946 : :
1947 : : /*
1948 : : * has_table_privilege_name
1949 : : * Check user privileges on a table given
1950 : : * text tablename and text priv name.
1951 : : * current_user is assumed
1952 : : */
1953 : : Datum
1954 : 44 : has_table_privilege_name(PG_FUNCTION_ARGS)
1955 : : {
1956 : 44 : text *tablename = PG_GETARG_TEXT_PP(0);
1957 : 44 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
1958 : : Oid roleid;
1959 : : Oid tableoid;
1960 : : AclMode mode;
1961 : : AclResult aclresult;
1962 : :
1963 : 44 : roleid = GetUserId();
1964 : 44 : tableoid = convert_table_name(tablename);
1965 : 40 : mode = convert_table_priv_string(priv_type_text);
1966 : :
1967 : 36 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
1968 : :
1969 : 36 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
1970 : : }
1971 : :
1972 : : /*
1973 : : * has_table_privilege_name_id
1974 : : * Check user privileges on a table given
1975 : : * name usename, table oid, and text priv name.
1976 : : */
1977 : : Datum
1978 : 12 : has_table_privilege_name_id(PG_FUNCTION_ARGS)
1979 : : {
1980 : 12 : Name username = PG_GETARG_NAME(0);
1981 : 12 : Oid tableoid = PG_GETARG_OID(1);
1982 : 12 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
1983 : : Oid roleid;
1984 : : AclMode mode;
1985 : : AclResult aclresult;
1986 : 12 : bool is_missing = false;
1987 : :
1988 : 12 : roleid = get_role_oid_or_public(NameStr(*username));
1989 : 12 : mode = convert_table_priv_string(priv_type_text);
1990 : :
1991 : 12 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
1992 : :
1993 [ - + ]: 12 : if (is_missing)
1994 : 0 : PG_RETURN_NULL();
1995 : :
1996 : 12 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
1997 : : }
1998 : :
1999 : : /*
2000 : : * has_table_privilege_id
2001 : : * Check user privileges on a table given
2002 : : * table oid, and text priv name.
2003 : : * current_user is assumed
2004 : : */
2005 : : Datum
2006 : 77 : has_table_privilege_id(PG_FUNCTION_ARGS)
2007 : : {
2008 : 77 : Oid tableoid = PG_GETARG_OID(0);
2009 : 77 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
2010 : : Oid roleid;
2011 : : AclMode mode;
2012 : : AclResult aclresult;
2013 : 77 : bool is_missing = false;
2014 : :
2015 : 77 : roleid = GetUserId();
2016 : 77 : mode = convert_table_priv_string(priv_type_text);
2017 : :
2018 : 77 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2019 : :
2020 [ + + ]: 77 : if (is_missing)
2021 : 5 : PG_RETURN_NULL();
2022 : :
2023 : 72 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2024 : : }
2025 : :
2026 : : /*
2027 : : * has_table_privilege_id_name
2028 : : * Check user privileges on a table given
2029 : : * roleid, text tablename, and text priv name.
2030 : : */
2031 : : Datum
2032 : 28 : has_table_privilege_id_name(PG_FUNCTION_ARGS)
2033 : : {
2034 : 28 : Oid roleid = PG_GETARG_OID(0);
2035 : 28 : text *tablename = PG_GETARG_TEXT_PP(1);
2036 : 28 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2037 : : Oid tableoid;
2038 : : AclMode mode;
2039 : : AclResult aclresult;
2040 : :
2041 : 28 : tableoid = convert_table_name(tablename);
2042 : 28 : mode = convert_table_priv_string(priv_type_text);
2043 : :
2044 : 28 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2045 : :
2046 : 28 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2047 : : }
2048 : :
2049 : : /*
2050 : : * has_table_privilege_id_id
2051 : : * Check user privileges on a table given
2052 : : * roleid, table oid, and text priv name.
2053 : : */
2054 : : Datum
2055 : 24 : has_table_privilege_id_id(PG_FUNCTION_ARGS)
2056 : : {
2057 : 24 : Oid roleid = PG_GETARG_OID(0);
2058 : 24 : Oid tableoid = PG_GETARG_OID(1);
2059 : 24 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2060 : : AclMode mode;
2061 : : AclResult aclresult;
2062 : 24 : bool is_missing = false;
2063 : :
2064 : 24 : mode = convert_table_priv_string(priv_type_text);
2065 : :
2066 : 24 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2067 : :
2068 [ - + ]: 24 : if (is_missing)
2069 : 0 : PG_RETURN_NULL();
2070 : :
2071 : 24 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2072 : : }
2073 : :
2074 : : /*
2075 : : * Support routines for has_table_privilege family.
2076 : : */
2077 : :
2078 : : /*
2079 : : * Given a table name expressed as a string, look it up and return Oid
2080 : : */
2081 : : static Oid
2082 : 356 : convert_table_name(text *tablename)
2083 : : {
2084 : : RangeVar *relrv;
2085 : :
2086 : 356 : relrv = makeRangeVarFromNameList(textToQualifiedNameList(tablename));
2087 : :
2088 : : /* We might not even have permissions on this relation; don't lock it. */
2089 : 356 : return RangeVarGetRelid(relrv, NoLock, false);
2090 : : }
2091 : :
2092 : : /*
2093 : : * convert_table_priv_string
2094 : : * Convert text string to AclMode value.
2095 : : */
2096 : : static AclMode
2097 : 321 : convert_table_priv_string(text *priv_type_text)
2098 : : {
2099 : : static const priv_map table_priv_map[] = {
2100 : : {"SELECT", ACL_SELECT},
2101 : : {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
2102 : : {"INSERT", ACL_INSERT},
2103 : : {"INSERT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_INSERT)},
2104 : : {"UPDATE", ACL_UPDATE},
2105 : : {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
2106 : : {"DELETE", ACL_DELETE},
2107 : : {"DELETE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_DELETE)},
2108 : : {"TRUNCATE", ACL_TRUNCATE},
2109 : : {"TRUNCATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_TRUNCATE)},
2110 : : {"REFERENCES", ACL_REFERENCES},
2111 : : {"REFERENCES WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_REFERENCES)},
2112 : : {"TRIGGER", ACL_TRIGGER},
2113 : : {"TRIGGER WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_TRIGGER)},
2114 : : {"MAINTAIN", ACL_MAINTAIN},
2115 : : {"MAINTAIN WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_MAINTAIN)},
2116 : : {NULL, 0}
2117 : : };
2118 : :
2119 : 321 : return convert_any_priv_string(priv_type_text, table_priv_map);
2120 : : }
2121 : :
2122 : : /*
2123 : : * has_sequence_privilege variants
2124 : : * These are all named "has_sequence_privilege" at the SQL level.
2125 : : * They take various combinations of relation name, relation OID,
2126 : : * user name, user OID, or implicit user = current_user.
2127 : : *
2128 : : * The result is a boolean value: true if user has the indicated
2129 : : * privilege, false if not. The variants that take a relation OID
2130 : : * return NULL if the OID doesn't exist.
2131 : : */
2132 : :
2133 : : /*
2134 : : * has_sequence_privilege_name_name
2135 : : * Check user privileges on a sequence given
2136 : : * name username, text sequencename, and text priv name.
2137 : : */
2138 : : Datum
2139 : 12 : has_sequence_privilege_name_name(PG_FUNCTION_ARGS)
2140 : : {
2141 : 12 : Name rolename = PG_GETARG_NAME(0);
2142 : 12 : text *sequencename = PG_GETARG_TEXT_PP(1);
2143 : 12 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2144 : : Oid roleid;
2145 : : Oid sequenceoid;
2146 : : AclMode mode;
2147 : : AclResult aclresult;
2148 : :
2149 : 12 : roleid = get_role_oid_or_public(NameStr(*rolename));
2150 : 12 : mode = convert_sequence_priv_string(priv_type_text);
2151 : 8 : sequenceoid = convert_table_name(sequencename);
2152 [ + + ]: 8 : if (get_rel_relkind(sequenceoid) != RELKIND_SEQUENCE)
2153 [ + - ]: 4 : ereport(ERROR,
2154 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2155 : : errmsg("\"%s\" is not a sequence",
2156 : : text_to_cstring(sequencename))));
2157 : :
2158 : 4 : aclresult = pg_class_aclcheck(sequenceoid, roleid, mode);
2159 : :
2160 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2161 : : }
2162 : :
2163 : : /*
2164 : : * has_sequence_privilege_name
2165 : : * Check user privileges on a sequence given
2166 : : * text sequencename and text priv name.
2167 : : * current_user is assumed
2168 : : */
2169 : : Datum
2170 : 4 : has_sequence_privilege_name(PG_FUNCTION_ARGS)
2171 : : {
2172 : 4 : text *sequencename = PG_GETARG_TEXT_PP(0);
2173 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
2174 : : Oid roleid;
2175 : : Oid sequenceoid;
2176 : : AclMode mode;
2177 : : AclResult aclresult;
2178 : :
2179 : 4 : roleid = GetUserId();
2180 : 4 : mode = convert_sequence_priv_string(priv_type_text);
2181 : 4 : sequenceoid = convert_table_name(sequencename);
2182 [ - + ]: 4 : if (get_rel_relkind(sequenceoid) != RELKIND_SEQUENCE)
2183 [ # # ]: 0 : ereport(ERROR,
2184 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2185 : : errmsg("\"%s\" is not a sequence",
2186 : : text_to_cstring(sequencename))));
2187 : :
2188 : 4 : aclresult = pg_class_aclcheck(sequenceoid, roleid, mode);
2189 : :
2190 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2191 : : }
2192 : :
2193 : : /*
2194 : : * has_sequence_privilege_name_id
2195 : : * Check user privileges on a sequence given
2196 : : * name usename, sequence oid, and text priv name.
2197 : : */
2198 : : Datum
2199 : 0 : has_sequence_privilege_name_id(PG_FUNCTION_ARGS)
2200 : : {
2201 : 0 : Name username = PG_GETARG_NAME(0);
2202 : 0 : Oid sequenceoid = PG_GETARG_OID(1);
2203 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2204 : : Oid roleid;
2205 : : AclMode mode;
2206 : : AclResult aclresult;
2207 : : char relkind;
2208 : 0 : bool is_missing = false;
2209 : :
2210 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
2211 : 0 : mode = convert_sequence_priv_string(priv_type_text);
2212 : 0 : relkind = get_rel_relkind(sequenceoid);
2213 [ # # ]: 0 : if (relkind == '\0')
2214 : 0 : PG_RETURN_NULL();
2215 [ # # ]: 0 : else if (relkind != RELKIND_SEQUENCE)
2216 [ # # ]: 0 : ereport(ERROR,
2217 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2218 : : errmsg("\"%s\" is not a sequence",
2219 : : get_rel_name(sequenceoid))));
2220 : :
2221 : 0 : aclresult = pg_class_aclcheck_ext(sequenceoid, roleid, mode, &is_missing);
2222 : :
2223 [ # # ]: 0 : if (is_missing)
2224 : 0 : PG_RETURN_NULL();
2225 : :
2226 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2227 : : }
2228 : :
2229 : : /*
2230 : : * has_sequence_privilege_id
2231 : : * Check user privileges on a sequence given
2232 : : * sequence oid, and text priv name.
2233 : : * current_user is assumed
2234 : : */
2235 : : Datum
2236 : 31 : has_sequence_privilege_id(PG_FUNCTION_ARGS)
2237 : : {
2238 : 31 : Oid sequenceoid = PG_GETARG_OID(0);
2239 : 31 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
2240 : : Oid roleid;
2241 : : AclMode mode;
2242 : : AclResult aclresult;
2243 : : char relkind;
2244 : 31 : bool is_missing = false;
2245 : :
2246 : 31 : roleid = GetUserId();
2247 : 31 : mode = convert_sequence_priv_string(priv_type_text);
2248 : 31 : relkind = get_rel_relkind(sequenceoid);
2249 [ + + ]: 31 : if (relkind == '\0')
2250 : 1 : PG_RETURN_NULL();
2251 [ - + ]: 30 : else if (relkind != RELKIND_SEQUENCE)
2252 [ # # ]: 0 : ereport(ERROR,
2253 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2254 : : errmsg("\"%s\" is not a sequence",
2255 : : get_rel_name(sequenceoid))));
2256 : :
2257 : 30 : aclresult = pg_class_aclcheck_ext(sequenceoid, roleid, mode, &is_missing);
2258 : :
2259 [ - + ]: 30 : if (is_missing)
2260 : 0 : PG_RETURN_NULL();
2261 : :
2262 : 30 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2263 : : }
2264 : :
2265 : : /*
2266 : : * has_sequence_privilege_id_name
2267 : : * Check user privileges on a sequence given
2268 : : * roleid, text sequencename, and text priv name.
2269 : : */
2270 : : Datum
2271 : 0 : has_sequence_privilege_id_name(PG_FUNCTION_ARGS)
2272 : : {
2273 : 0 : Oid roleid = PG_GETARG_OID(0);
2274 : 0 : text *sequencename = PG_GETARG_TEXT_PP(1);
2275 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2276 : : Oid sequenceoid;
2277 : : AclMode mode;
2278 : : AclResult aclresult;
2279 : :
2280 : 0 : mode = convert_sequence_priv_string(priv_type_text);
2281 : 0 : sequenceoid = convert_table_name(sequencename);
2282 [ # # ]: 0 : if (get_rel_relkind(sequenceoid) != RELKIND_SEQUENCE)
2283 [ # # ]: 0 : ereport(ERROR,
2284 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2285 : : errmsg("\"%s\" is not a sequence",
2286 : : text_to_cstring(sequencename))));
2287 : :
2288 : 0 : aclresult = pg_class_aclcheck(sequenceoid, roleid, mode);
2289 : :
2290 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2291 : : }
2292 : :
2293 : : /*
2294 : : * has_sequence_privilege_id_id
2295 : : * Check user privileges on a sequence given
2296 : : * roleid, sequence oid, and text priv name.
2297 : : */
2298 : : Datum
2299 : 0 : has_sequence_privilege_id_id(PG_FUNCTION_ARGS)
2300 : : {
2301 : 0 : Oid roleid = PG_GETARG_OID(0);
2302 : 0 : Oid sequenceoid = PG_GETARG_OID(1);
2303 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2304 : : AclMode mode;
2305 : : AclResult aclresult;
2306 : : char relkind;
2307 : 0 : bool is_missing = false;
2308 : :
2309 : 0 : mode = convert_sequence_priv_string(priv_type_text);
2310 : 0 : relkind = get_rel_relkind(sequenceoid);
2311 [ # # ]: 0 : if (relkind == '\0')
2312 : 0 : PG_RETURN_NULL();
2313 [ # # ]: 0 : else if (relkind != RELKIND_SEQUENCE)
2314 [ # # ]: 0 : ereport(ERROR,
2315 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2316 : : errmsg("\"%s\" is not a sequence",
2317 : : get_rel_name(sequenceoid))));
2318 : :
2319 : 0 : aclresult = pg_class_aclcheck_ext(sequenceoid, roleid, mode, &is_missing);
2320 : :
2321 [ # # ]: 0 : if (is_missing)
2322 : 0 : PG_RETURN_NULL();
2323 : :
2324 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2325 : : }
2326 : :
2327 : : /*
2328 : : * convert_sequence_priv_string
2329 : : * Convert text string to AclMode value.
2330 : : */
2331 : : static AclMode
2332 : 47 : convert_sequence_priv_string(text *priv_type_text)
2333 : : {
2334 : : static const priv_map sequence_priv_map[] = {
2335 : : {"USAGE", ACL_USAGE},
2336 : : {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
2337 : : {"SELECT", ACL_SELECT},
2338 : : {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
2339 : : {"UPDATE", ACL_UPDATE},
2340 : : {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
2341 : : {NULL, 0}
2342 : : };
2343 : :
2344 : 47 : return convert_any_priv_string(priv_type_text, sequence_priv_map);
2345 : : }
2346 : :
2347 : :
2348 : : /*
2349 : : * has_any_column_privilege variants
2350 : : * These are all named "has_any_column_privilege" at the SQL level.
2351 : : * They take various combinations of relation name, relation OID,
2352 : : * user name, user OID, or implicit user = current_user.
2353 : : *
2354 : : * The result is a boolean value: true if user has the indicated
2355 : : * privilege for any column of the table, false if not. The variants
2356 : : * that take a relation OID return NULL if the OID doesn't exist.
2357 : : */
2358 : :
2359 : : /*
2360 : : * has_any_column_privilege_name_name
2361 : : * Check user privileges on any column of a table given
2362 : : * name username, text tablename, and text priv name.
2363 : : */
2364 : : Datum
2365 : 0 : has_any_column_privilege_name_name(PG_FUNCTION_ARGS)
2366 : : {
2367 : 0 : Name rolename = PG_GETARG_NAME(0);
2368 : 0 : text *tablename = PG_GETARG_TEXT_PP(1);
2369 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2370 : : Oid roleid;
2371 : : Oid tableoid;
2372 : : AclMode mode;
2373 : : AclResult aclresult;
2374 : :
2375 : 0 : roleid = get_role_oid_or_public(NameStr(*rolename));
2376 : 0 : tableoid = convert_table_name(tablename);
2377 : 0 : mode = convert_column_priv_string(priv_type_text);
2378 : :
2379 : : /* First check at table level, then examine each column if needed */
2380 : 0 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2381 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2382 : 0 : aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
2383 : : ACLMASK_ANY);
2384 : :
2385 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2386 : : }
2387 : :
2388 : : /*
2389 : : * has_any_column_privilege_name
2390 : : * Check user privileges on any column of a table given
2391 : : * text tablename and text priv name.
2392 : : * current_user is assumed
2393 : : */
2394 : : Datum
2395 : 0 : has_any_column_privilege_name(PG_FUNCTION_ARGS)
2396 : : {
2397 : 0 : text *tablename = PG_GETARG_TEXT_PP(0);
2398 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
2399 : : Oid roleid;
2400 : : Oid tableoid;
2401 : : AclMode mode;
2402 : : AclResult aclresult;
2403 : :
2404 : 0 : roleid = GetUserId();
2405 : 0 : tableoid = convert_table_name(tablename);
2406 : 0 : mode = convert_column_priv_string(priv_type_text);
2407 : :
2408 : : /* First check at table level, then examine each column if needed */
2409 : 0 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2410 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2411 : 0 : aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
2412 : : ACLMASK_ANY);
2413 : :
2414 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2415 : : }
2416 : :
2417 : : /*
2418 : : * has_any_column_privilege_name_id
2419 : : * Check user privileges on any column of a table given
2420 : : * name usename, table oid, and text priv name.
2421 : : */
2422 : : Datum
2423 : 0 : has_any_column_privilege_name_id(PG_FUNCTION_ARGS)
2424 : : {
2425 : 0 : Name username = PG_GETARG_NAME(0);
2426 : 0 : Oid tableoid = PG_GETARG_OID(1);
2427 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2428 : : Oid roleid;
2429 : : AclMode mode;
2430 : : AclResult aclresult;
2431 : 0 : bool is_missing = false;
2432 : :
2433 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
2434 : 0 : mode = convert_column_priv_string(priv_type_text);
2435 : :
2436 : : /* First check at table level, then examine each column if needed */
2437 : 0 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2438 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2439 : : {
2440 [ # # ]: 0 : if (is_missing)
2441 : 0 : PG_RETURN_NULL();
2442 : 0 : aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
2443 : : ACLMASK_ANY, &is_missing);
2444 [ # # ]: 0 : if (is_missing)
2445 : 0 : PG_RETURN_NULL();
2446 : : }
2447 : :
2448 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2449 : : }
2450 : :
2451 : : /*
2452 : : * has_any_column_privilege_id
2453 : : * Check user privileges on any column of a table given
2454 : : * table oid, and text priv name.
2455 : : * current_user is assumed
2456 : : */
2457 : : Datum
2458 : 0 : has_any_column_privilege_id(PG_FUNCTION_ARGS)
2459 : : {
2460 : 0 : Oid tableoid = PG_GETARG_OID(0);
2461 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
2462 : : Oid roleid;
2463 : : AclMode mode;
2464 : : AclResult aclresult;
2465 : 0 : bool is_missing = false;
2466 : :
2467 : 0 : roleid = GetUserId();
2468 : 0 : mode = convert_column_priv_string(priv_type_text);
2469 : :
2470 : : /* First check at table level, then examine each column if needed */
2471 : 0 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2472 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2473 : : {
2474 [ # # ]: 0 : if (is_missing)
2475 : 0 : PG_RETURN_NULL();
2476 : 0 : aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
2477 : : ACLMASK_ANY, &is_missing);
2478 [ # # ]: 0 : if (is_missing)
2479 : 0 : PG_RETURN_NULL();
2480 : : }
2481 : :
2482 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2483 : : }
2484 : :
2485 : : /*
2486 : : * has_any_column_privilege_id_name
2487 : : * Check user privileges on any column of a table given
2488 : : * roleid, text tablename, and text priv name.
2489 : : */
2490 : : Datum
2491 : 0 : has_any_column_privilege_id_name(PG_FUNCTION_ARGS)
2492 : : {
2493 : 0 : Oid roleid = PG_GETARG_OID(0);
2494 : 0 : text *tablename = PG_GETARG_TEXT_PP(1);
2495 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2496 : : Oid tableoid;
2497 : : AclMode mode;
2498 : : AclResult aclresult;
2499 : :
2500 : 0 : tableoid = convert_table_name(tablename);
2501 : 0 : mode = convert_column_priv_string(priv_type_text);
2502 : :
2503 : : /* First check at table level, then examine each column if needed */
2504 : 0 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2505 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2506 : 0 : aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
2507 : : ACLMASK_ANY);
2508 : :
2509 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2510 : : }
2511 : :
2512 : : /*
2513 : : * has_any_column_privilege_id_id
2514 : : * Check user privileges on any column of a table given
2515 : : * roleid, table oid, and text priv name.
2516 : : */
2517 : : Datum
2518 : 0 : has_any_column_privilege_id_id(PG_FUNCTION_ARGS)
2519 : : {
2520 : 0 : Oid roleid = PG_GETARG_OID(0);
2521 : 0 : Oid tableoid = PG_GETARG_OID(1);
2522 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2523 : : AclMode mode;
2524 : : AclResult aclresult;
2525 : 0 : bool is_missing = false;
2526 : :
2527 : 0 : mode = convert_column_priv_string(priv_type_text);
2528 : :
2529 : : /* First check at table level, then examine each column if needed */
2530 : 0 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2531 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2532 : : {
2533 [ # # ]: 0 : if (is_missing)
2534 : 0 : PG_RETURN_NULL();
2535 : 0 : aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
2536 : : ACLMASK_ANY, &is_missing);
2537 [ # # ]: 0 : if (is_missing)
2538 : 0 : PG_RETURN_NULL();
2539 : : }
2540 : :
2541 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2542 : : }
2543 : :
2544 : :
2545 : : /*
2546 : : * has_column_privilege variants
2547 : : * These are all named "has_column_privilege" at the SQL level.
2548 : : * They take various combinations of relation name, relation OID,
2549 : : * column name, column attnum, user name, user OID, or
2550 : : * implicit user = current_user.
2551 : : *
2552 : : * The result is a boolean value: true if user has the indicated
2553 : : * privilege, false if not. The variants that take a relation OID
2554 : : * return NULL (rather than throwing an error) if that relation OID
2555 : : * doesn't exist. Likewise, the variants that take an integer attnum
2556 : : * return NULL (rather than throwing an error) if there is no such
2557 : : * pg_attribute entry. All variants return NULL if an attisdropped
2558 : : * column is selected. These rules are meant to avoid unnecessary
2559 : : * failures in queries that scan pg_attribute.
2560 : : */
2561 : :
2562 : : /*
2563 : : * column_privilege_check: check column privileges, but don't throw an error
2564 : : * for dropped column or table
2565 : : *
2566 : : * Returns 1 if have the privilege, 0 if not, -1 if dropped column/table.
2567 : : */
2568 : : static int
2569 : 3018 : column_privilege_check(Oid tableoid, AttrNumber attnum,
2570 : : Oid roleid, AclMode mode)
2571 : : {
2572 : : AclResult aclresult;
2573 : 3018 : bool is_missing = false;
2574 : :
2575 : : /*
2576 : : * If convert_column_name failed, we can just return -1 immediately.
2577 : : */
2578 [ + + ]: 3018 : if (attnum == InvalidAttrNumber)
2579 : 8 : return -1;
2580 : :
2581 : : /*
2582 : : * Check for column-level privileges first. This serves in part as a check
2583 : : * on whether the column even exists, so we need to do it before checking
2584 : : * table-level privilege.
2585 : : */
2586 : 3010 : aclresult = pg_attribute_aclcheck_ext(tableoid, attnum, roleid,
2587 : : mode, &is_missing);
2588 [ + + ]: 3010 : if (aclresult == ACLCHECK_OK)
2589 : 104 : return 1;
2590 [ + + ]: 2906 : else if (is_missing)
2591 : 28 : return -1;
2592 : :
2593 : : /* Next check if we have the privilege at the table level */
2594 : 2878 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2595 [ + + ]: 2878 : if (aclresult == ACLCHECK_OK)
2596 : 2874 : return 1;
2597 [ - + ]: 4 : else if (is_missing)
2598 : 0 : return -1;
2599 : : else
2600 : 4 : return 0;
2601 : : }
2602 : :
2603 : : /*
2604 : : * has_column_privilege_name_name_name
2605 : : * Check user privileges on a column given
2606 : : * name username, text tablename, text colname, and text priv name.
2607 : : */
2608 : : Datum
2609 : 100 : has_column_privilege_name_name_name(PG_FUNCTION_ARGS)
2610 : : {
2611 : 100 : Name rolename = PG_GETARG_NAME(0);
2612 : 100 : text *tablename = PG_GETARG_TEXT_PP(1);
2613 : 100 : text *column = PG_GETARG_TEXT_PP(2);
2614 : 100 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2615 : : Oid roleid;
2616 : : Oid tableoid;
2617 : : AttrNumber colattnum;
2618 : : AclMode mode;
2619 : : int privresult;
2620 : :
2621 : 100 : roleid = get_role_oid_or_public(NameStr(*rolename));
2622 : 100 : tableoid = convert_table_name(tablename);
2623 : 100 : colattnum = convert_column_name(tableoid, column);
2624 : 100 : mode = convert_column_priv_string(priv_type_text);
2625 : :
2626 : 100 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2627 [ - + ]: 100 : if (privresult < 0)
2628 : 0 : PG_RETURN_NULL();
2629 : 100 : PG_RETURN_BOOL(privresult);
2630 : : }
2631 : :
2632 : : /*
2633 : : * has_column_privilege_name_name_attnum
2634 : : * Check user privileges on a column given
2635 : : * name username, text tablename, int attnum, and text priv name.
2636 : : */
2637 : : Datum
2638 : 0 : has_column_privilege_name_name_attnum(PG_FUNCTION_ARGS)
2639 : : {
2640 : 0 : Name rolename = PG_GETARG_NAME(0);
2641 : 0 : text *tablename = PG_GETARG_TEXT_PP(1);
2642 : 0 : AttrNumber colattnum = PG_GETARG_INT16(2);
2643 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2644 : : Oid roleid;
2645 : : Oid tableoid;
2646 : : AclMode mode;
2647 : : int privresult;
2648 : :
2649 : 0 : roleid = get_role_oid_or_public(NameStr(*rolename));
2650 : 0 : tableoid = convert_table_name(tablename);
2651 : 0 : mode = convert_column_priv_string(priv_type_text);
2652 : :
2653 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2654 [ # # ]: 0 : if (privresult < 0)
2655 : 0 : PG_RETURN_NULL();
2656 : 0 : PG_RETURN_BOOL(privresult);
2657 : : }
2658 : :
2659 : : /*
2660 : : * has_column_privilege_name_id_name
2661 : : * Check user privileges on a column given
2662 : : * name username, table oid, text colname, and text priv name.
2663 : : */
2664 : : Datum
2665 : 0 : has_column_privilege_name_id_name(PG_FUNCTION_ARGS)
2666 : : {
2667 : 0 : Name username = PG_GETARG_NAME(0);
2668 : 0 : Oid tableoid = PG_GETARG_OID(1);
2669 : 0 : text *column = PG_GETARG_TEXT_PP(2);
2670 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2671 : : Oid roleid;
2672 : : AttrNumber colattnum;
2673 : : AclMode mode;
2674 : : int privresult;
2675 : :
2676 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
2677 : 0 : colattnum = convert_column_name(tableoid, column);
2678 : 0 : mode = convert_column_priv_string(priv_type_text);
2679 : :
2680 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2681 [ # # ]: 0 : if (privresult < 0)
2682 : 0 : PG_RETURN_NULL();
2683 : 0 : PG_RETURN_BOOL(privresult);
2684 : : }
2685 : :
2686 : : /*
2687 : : * has_column_privilege_name_id_attnum
2688 : : * Check user privileges on a column given
2689 : : * name username, table oid, int attnum, and text priv name.
2690 : : */
2691 : : Datum
2692 : 0 : has_column_privilege_name_id_attnum(PG_FUNCTION_ARGS)
2693 : : {
2694 : 0 : Name username = PG_GETARG_NAME(0);
2695 : 0 : Oid tableoid = PG_GETARG_OID(1);
2696 : 0 : AttrNumber colattnum = PG_GETARG_INT16(2);
2697 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2698 : : Oid roleid;
2699 : : AclMode mode;
2700 : : int privresult;
2701 : :
2702 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
2703 : 0 : mode = convert_column_priv_string(priv_type_text);
2704 : :
2705 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2706 [ # # ]: 0 : if (privresult < 0)
2707 : 0 : PG_RETURN_NULL();
2708 : 0 : PG_RETURN_BOOL(privresult);
2709 : : }
2710 : :
2711 : : /*
2712 : : * has_column_privilege_id_name_name
2713 : : * Check user privileges on a column given
2714 : : * oid roleid, text tablename, text colname, and text priv name.
2715 : : */
2716 : : Datum
2717 : 0 : has_column_privilege_id_name_name(PG_FUNCTION_ARGS)
2718 : : {
2719 : 0 : Oid roleid = PG_GETARG_OID(0);
2720 : 0 : text *tablename = PG_GETARG_TEXT_PP(1);
2721 : 0 : text *column = PG_GETARG_TEXT_PP(2);
2722 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2723 : : Oid tableoid;
2724 : : AttrNumber colattnum;
2725 : : AclMode mode;
2726 : : int privresult;
2727 : :
2728 : 0 : tableoid = convert_table_name(tablename);
2729 : 0 : colattnum = convert_column_name(tableoid, column);
2730 : 0 : mode = convert_column_priv_string(priv_type_text);
2731 : :
2732 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2733 [ # # ]: 0 : if (privresult < 0)
2734 : 0 : PG_RETURN_NULL();
2735 : 0 : PG_RETURN_BOOL(privresult);
2736 : : }
2737 : :
2738 : : /*
2739 : : * has_column_privilege_id_name_attnum
2740 : : * Check user privileges on a column given
2741 : : * oid roleid, text tablename, int attnum, and text priv name.
2742 : : */
2743 : : Datum
2744 : 0 : has_column_privilege_id_name_attnum(PG_FUNCTION_ARGS)
2745 : : {
2746 : 0 : Oid roleid = PG_GETARG_OID(0);
2747 : 0 : text *tablename = PG_GETARG_TEXT_PP(1);
2748 : 0 : AttrNumber colattnum = PG_GETARG_INT16(2);
2749 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2750 : : Oid tableoid;
2751 : : AclMode mode;
2752 : : int privresult;
2753 : :
2754 : 0 : tableoid = convert_table_name(tablename);
2755 : 0 : mode = convert_column_priv_string(priv_type_text);
2756 : :
2757 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2758 [ # # ]: 0 : if (privresult < 0)
2759 : 0 : PG_RETURN_NULL();
2760 : 0 : PG_RETURN_BOOL(privresult);
2761 : : }
2762 : :
2763 : : /*
2764 : : * has_column_privilege_id_id_name
2765 : : * Check user privileges on a column given
2766 : : * oid roleid, table oid, text colname, and text priv name.
2767 : : */
2768 : : Datum
2769 : 0 : has_column_privilege_id_id_name(PG_FUNCTION_ARGS)
2770 : : {
2771 : 0 : Oid roleid = PG_GETARG_OID(0);
2772 : 0 : Oid tableoid = PG_GETARG_OID(1);
2773 : 0 : text *column = PG_GETARG_TEXT_PP(2);
2774 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2775 : : AttrNumber colattnum;
2776 : : AclMode mode;
2777 : : int privresult;
2778 : :
2779 : 0 : colattnum = convert_column_name(tableoid, column);
2780 : 0 : mode = convert_column_priv_string(priv_type_text);
2781 : :
2782 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2783 [ # # ]: 0 : if (privresult < 0)
2784 : 0 : PG_RETURN_NULL();
2785 : 0 : PG_RETURN_BOOL(privresult);
2786 : : }
2787 : :
2788 : : /*
2789 : : * has_column_privilege_id_id_attnum
2790 : : * Check user privileges on a column given
2791 : : * oid roleid, table oid, int attnum, and text priv name.
2792 : : */
2793 : : Datum
2794 : 0 : has_column_privilege_id_id_attnum(PG_FUNCTION_ARGS)
2795 : : {
2796 : 0 : Oid roleid = PG_GETARG_OID(0);
2797 : 0 : Oid tableoid = PG_GETARG_OID(1);
2798 : 0 : AttrNumber colattnum = PG_GETARG_INT16(2);
2799 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2800 : : AclMode mode;
2801 : : int privresult;
2802 : :
2803 : 0 : mode = convert_column_priv_string(priv_type_text);
2804 : :
2805 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2806 [ # # ]: 0 : if (privresult < 0)
2807 : 0 : PG_RETURN_NULL();
2808 : 0 : PG_RETURN_BOOL(privresult);
2809 : : }
2810 : :
2811 : : /*
2812 : : * has_column_privilege_name_name
2813 : : * Check user privileges on a column given
2814 : : * text tablename, text colname, and text priv name.
2815 : : * current_user is assumed
2816 : : */
2817 : : Datum
2818 : 12 : has_column_privilege_name_name(PG_FUNCTION_ARGS)
2819 : : {
2820 : 12 : text *tablename = PG_GETARG_TEXT_PP(0);
2821 : 12 : text *column = PG_GETARG_TEXT_PP(1);
2822 : 12 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2823 : : Oid roleid;
2824 : : Oid tableoid;
2825 : : AttrNumber colattnum;
2826 : : AclMode mode;
2827 : : int privresult;
2828 : :
2829 : 12 : roleid = GetUserId();
2830 : 12 : tableoid = convert_table_name(tablename);
2831 : 12 : colattnum = convert_column_name(tableoid, column);
2832 : 4 : mode = convert_column_priv_string(priv_type_text);
2833 : :
2834 : 4 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2835 [ + - ]: 4 : if (privresult < 0)
2836 : 4 : PG_RETURN_NULL();
2837 : 0 : PG_RETURN_BOOL(privresult);
2838 : : }
2839 : :
2840 : : /*
2841 : : * has_column_privilege_name_attnum
2842 : : * Check user privileges on a column given
2843 : : * text tablename, int attnum, and text priv name.
2844 : : * current_user is assumed
2845 : : */
2846 : : Datum
2847 : 20 : has_column_privilege_name_attnum(PG_FUNCTION_ARGS)
2848 : : {
2849 : 20 : text *tablename = PG_GETARG_TEXT_PP(0);
2850 : 20 : AttrNumber colattnum = PG_GETARG_INT16(1);
2851 : 20 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2852 : : Oid roleid;
2853 : : Oid tableoid;
2854 : : AclMode mode;
2855 : : int privresult;
2856 : :
2857 : 20 : roleid = GetUserId();
2858 : 20 : tableoid = convert_table_name(tablename);
2859 : 20 : mode = convert_column_priv_string(priv_type_text);
2860 : :
2861 : 20 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2862 [ + - ]: 20 : if (privresult < 0)
2863 : 20 : PG_RETURN_NULL();
2864 : 0 : PG_RETURN_BOOL(privresult);
2865 : : }
2866 : :
2867 : : /*
2868 : : * has_column_privilege_id_name
2869 : : * Check user privileges on a column given
2870 : : * table oid, text colname, and text priv name.
2871 : : * current_user is assumed
2872 : : */
2873 : : Datum
2874 : 4 : has_column_privilege_id_name(PG_FUNCTION_ARGS)
2875 : : {
2876 : 4 : Oid tableoid = PG_GETARG_OID(0);
2877 : 4 : text *column = PG_GETARG_TEXT_PP(1);
2878 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2879 : : Oid roleid;
2880 : : AttrNumber colattnum;
2881 : : AclMode mode;
2882 : : int privresult;
2883 : :
2884 : 4 : roleid = GetUserId();
2885 : 4 : colattnum = convert_column_name(tableoid, column);
2886 : 4 : mode = convert_column_priv_string(priv_type_text);
2887 : :
2888 : 4 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2889 [ + - ]: 4 : if (privresult < 0)
2890 : 4 : PG_RETURN_NULL();
2891 : 0 : PG_RETURN_BOOL(privresult);
2892 : : }
2893 : :
2894 : : /*
2895 : : * has_column_privilege_id_attnum
2896 : : * Check user privileges on a column given
2897 : : * table oid, int attnum, and text priv name.
2898 : : * current_user is assumed
2899 : : */
2900 : : Datum
2901 : 2890 : has_column_privilege_id_attnum(PG_FUNCTION_ARGS)
2902 : : {
2903 : 2890 : Oid tableoid = PG_GETARG_OID(0);
2904 : 2890 : AttrNumber colattnum = PG_GETARG_INT16(1);
2905 : 2890 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2906 : : Oid roleid;
2907 : : AclMode mode;
2908 : : int privresult;
2909 : :
2910 : 2890 : roleid = GetUserId();
2911 : 2890 : mode = convert_column_priv_string(priv_type_text);
2912 : :
2913 : 2890 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2914 [ + + ]: 2890 : if (privresult < 0)
2915 : 8 : PG_RETURN_NULL();
2916 : 2882 : PG_RETURN_BOOL(privresult);
2917 : : }
2918 : :
2919 : : /*
2920 : : * Support routines for has_column_privilege family.
2921 : : */
2922 : :
2923 : : /*
2924 : : * Given a table OID and a column name expressed as a string, look it up
2925 : : * and return the column number. Returns InvalidAttrNumber in cases
2926 : : * where caller should return NULL instead of failing.
2927 : : */
2928 : : static AttrNumber
2929 : 116 : convert_column_name(Oid tableoid, text *column)
2930 : : {
2931 : : char *colname;
2932 : : HeapTuple attTuple;
2933 : : AttrNumber attnum;
2934 : :
2935 : 116 : colname = text_to_cstring(column);
2936 : :
2937 : : /*
2938 : : * We don't use get_attnum() here because it will report that dropped
2939 : : * columns don't exist. We need to treat dropped columns differently from
2940 : : * nonexistent columns.
2941 : : */
2942 : 116 : attTuple = SearchSysCache2(ATTNAME,
2943 : : ObjectIdGetDatum(tableoid),
2944 : : CStringGetDatum(colname));
2945 [ + + ]: 116 : if (HeapTupleIsValid(attTuple))
2946 : : {
2947 : : Form_pg_attribute attributeForm;
2948 : :
2949 : 104 : attributeForm = (Form_pg_attribute) GETSTRUCT(attTuple);
2950 : : /* We want to return NULL for dropped columns */
2951 [ + + ]: 104 : if (attributeForm->attisdropped)
2952 : 4 : attnum = InvalidAttrNumber;
2953 : : else
2954 : 100 : attnum = attributeForm->attnum;
2955 : 104 : ReleaseSysCache(attTuple);
2956 : : }
2957 : : else
2958 : : {
2959 : 12 : char *tablename = get_rel_name(tableoid);
2960 : :
2961 : : /*
2962 : : * If the table OID is bogus, or it's just been dropped, we'll get
2963 : : * NULL back. In such cases we want has_column_privilege to return
2964 : : * NULL too, so just return InvalidAttrNumber.
2965 : : */
2966 [ + + ]: 12 : if (tablename != NULL)
2967 : : {
2968 : : /* tableoid exists, colname does not, so throw error */
2969 [ + - ]: 8 : ereport(ERROR,
2970 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
2971 : : errmsg("column \"%s\" of relation \"%s\" does not exist",
2972 : : colname, tablename)));
2973 : : }
2974 : : /* tableoid doesn't exist, so act like attisdropped case */
2975 : 4 : attnum = InvalidAttrNumber;
2976 : : }
2977 : :
2978 : 108 : pfree(colname);
2979 : 108 : return attnum;
2980 : : }
2981 : :
2982 : : /*
2983 : : * convert_column_priv_string
2984 : : * Convert text string to AclMode value.
2985 : : */
2986 : : static AclMode
2987 : 3018 : convert_column_priv_string(text *priv_type_text)
2988 : : {
2989 : : static const priv_map column_priv_map[] = {
2990 : : {"SELECT", ACL_SELECT},
2991 : : {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
2992 : : {"INSERT", ACL_INSERT},
2993 : : {"INSERT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_INSERT)},
2994 : : {"UPDATE", ACL_UPDATE},
2995 : : {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
2996 : : {"REFERENCES", ACL_REFERENCES},
2997 : : {"REFERENCES WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_REFERENCES)},
2998 : : {NULL, 0}
2999 : : };
3000 : :
3001 : 3018 : return convert_any_priv_string(priv_type_text, column_priv_map);
3002 : : }
3003 : :
3004 : :
3005 : : /*
3006 : : * has_database_privilege variants
3007 : : * These are all named "has_database_privilege" at the SQL level.
3008 : : * They take various combinations of database name, database OID,
3009 : : * user name, user OID, or implicit user = current_user.
3010 : : *
3011 : : * The result is a boolean value: true if user has the indicated
3012 : : * privilege, false if not, or NULL if object doesn't exist.
3013 : : */
3014 : :
3015 : : /*
3016 : : * has_database_privilege_name_name
3017 : : * Check user privileges on a database given
3018 : : * name username, text databasename, and text priv name.
3019 : : */
3020 : : Datum
3021 : 0 : has_database_privilege_name_name(PG_FUNCTION_ARGS)
3022 : : {
3023 : 0 : Name username = PG_GETARG_NAME(0);
3024 : 0 : text *databasename = PG_GETARG_TEXT_PP(1);
3025 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3026 : : Oid roleid;
3027 : : Oid databaseoid;
3028 : : AclMode mode;
3029 : : AclResult aclresult;
3030 : :
3031 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
3032 : 0 : databaseoid = convert_database_name(databasename);
3033 : 0 : mode = convert_database_priv_string(priv_type_text);
3034 : :
3035 : 0 : aclresult = object_aclcheck(DatabaseRelationId, databaseoid, roleid, mode);
3036 : :
3037 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3038 : : }
3039 : :
3040 : : /*
3041 : : * has_database_privilege_name
3042 : : * Check user privileges on a database given
3043 : : * text databasename and text priv name.
3044 : : * current_user is assumed
3045 : : */
3046 : : Datum
3047 : 0 : has_database_privilege_name(PG_FUNCTION_ARGS)
3048 : : {
3049 : 0 : text *databasename = PG_GETARG_TEXT_PP(0);
3050 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3051 : : Oid roleid;
3052 : : Oid databaseoid;
3053 : : AclMode mode;
3054 : : AclResult aclresult;
3055 : :
3056 : 0 : roleid = GetUserId();
3057 : 0 : databaseoid = convert_database_name(databasename);
3058 : 0 : mode = convert_database_priv_string(priv_type_text);
3059 : :
3060 : 0 : aclresult = object_aclcheck(DatabaseRelationId, databaseoid, roleid, mode);
3061 : :
3062 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3063 : : }
3064 : :
3065 : : /*
3066 : : * has_database_privilege_name_id
3067 : : * Check user privileges on a database given
3068 : : * name usename, database oid, and text priv name.
3069 : : */
3070 : : Datum
3071 : 0 : has_database_privilege_name_id(PG_FUNCTION_ARGS)
3072 : : {
3073 : 0 : Name username = PG_GETARG_NAME(0);
3074 : 0 : Oid databaseoid = PG_GETARG_OID(1);
3075 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3076 : : Oid roleid;
3077 : : AclMode mode;
3078 : : AclResult aclresult;
3079 : 0 : bool is_missing = false;
3080 : :
3081 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
3082 : 0 : mode = convert_database_priv_string(priv_type_text);
3083 : :
3084 : 0 : aclresult = object_aclcheck_ext(DatabaseRelationId, databaseoid,
3085 : : roleid, mode,
3086 : : &is_missing);
3087 : :
3088 [ # # ]: 0 : if (is_missing)
3089 : 0 : PG_RETURN_NULL();
3090 : :
3091 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3092 : : }
3093 : :
3094 : : /*
3095 : : * has_database_privilege_id
3096 : : * Check user privileges on a database given
3097 : : * database oid, and text priv name.
3098 : : * current_user is assumed
3099 : : */
3100 : : Datum
3101 : 0 : has_database_privilege_id(PG_FUNCTION_ARGS)
3102 : : {
3103 : 0 : Oid databaseoid = PG_GETARG_OID(0);
3104 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3105 : : Oid roleid;
3106 : : AclMode mode;
3107 : : AclResult aclresult;
3108 : 0 : bool is_missing = false;
3109 : :
3110 : 0 : roleid = GetUserId();
3111 : 0 : mode = convert_database_priv_string(priv_type_text);
3112 : :
3113 : 0 : aclresult = object_aclcheck_ext(DatabaseRelationId, databaseoid,
3114 : : roleid, mode,
3115 : : &is_missing);
3116 : :
3117 [ # # ]: 0 : if (is_missing)
3118 : 0 : PG_RETURN_NULL();
3119 : :
3120 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3121 : : }
3122 : :
3123 : : /*
3124 : : * has_database_privilege_id_name
3125 : : * Check user privileges on a database given
3126 : : * roleid, text databasename, and text priv name.
3127 : : */
3128 : : Datum
3129 : 0 : has_database_privilege_id_name(PG_FUNCTION_ARGS)
3130 : : {
3131 : 0 : Oid roleid = PG_GETARG_OID(0);
3132 : 0 : text *databasename = PG_GETARG_TEXT_PP(1);
3133 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3134 : : Oid databaseoid;
3135 : : AclMode mode;
3136 : : AclResult aclresult;
3137 : :
3138 : 0 : databaseoid = convert_database_name(databasename);
3139 : 0 : mode = convert_database_priv_string(priv_type_text);
3140 : :
3141 : 0 : aclresult = object_aclcheck(DatabaseRelationId, databaseoid, roleid, mode);
3142 : :
3143 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3144 : : }
3145 : :
3146 : : /*
3147 : : * has_database_privilege_id_id
3148 : : * Check user privileges on a database given
3149 : : * roleid, database oid, and text priv name.
3150 : : */
3151 : : Datum
3152 : 0 : has_database_privilege_id_id(PG_FUNCTION_ARGS)
3153 : : {
3154 : 0 : Oid roleid = PG_GETARG_OID(0);
3155 : 0 : Oid databaseoid = PG_GETARG_OID(1);
3156 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3157 : : AclMode mode;
3158 : : AclResult aclresult;
3159 : 0 : bool is_missing = false;
3160 : :
3161 : 0 : mode = convert_database_priv_string(priv_type_text);
3162 : :
3163 : 0 : aclresult = object_aclcheck_ext(DatabaseRelationId, databaseoid,
3164 : : roleid, mode,
3165 : : &is_missing);
3166 : :
3167 [ # # ]: 0 : if (is_missing)
3168 : 0 : PG_RETURN_NULL();
3169 : :
3170 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3171 : : }
3172 : :
3173 : : /*
3174 : : * Support routines for has_database_privilege family.
3175 : : */
3176 : :
3177 : : /*
3178 : : * Given a database name expressed as a string, look it up and return Oid
3179 : : */
3180 : : static Oid
3181 : 0 : convert_database_name(text *databasename)
3182 : : {
3183 : 0 : char *dbname = text_to_cstring(databasename);
3184 : :
3185 : 0 : return get_database_oid(dbname, false);
3186 : : }
3187 : :
3188 : : /*
3189 : : * convert_database_priv_string
3190 : : * Convert text string to AclMode value.
3191 : : */
3192 : : static AclMode
3193 : 0 : convert_database_priv_string(text *priv_type_text)
3194 : : {
3195 : : static const priv_map database_priv_map[] = {
3196 : : {"CREATE", ACL_CREATE},
3197 : : {"CREATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
3198 : : {"TEMPORARY", ACL_CREATE_TEMP},
3199 : : {"TEMPORARY WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE_TEMP)},
3200 : : {"TEMP", ACL_CREATE_TEMP},
3201 : : {"TEMP WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE_TEMP)},
3202 : : {"CONNECT", ACL_CONNECT},
3203 : : {"CONNECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CONNECT)},
3204 : : {NULL, 0}
3205 : : };
3206 : :
3207 : 0 : return convert_any_priv_string(priv_type_text, database_priv_map);
3208 : : }
3209 : :
3210 : :
3211 : : /*
3212 : : * has_foreign_data_wrapper_privilege variants
3213 : : * These are all named "has_foreign_data_wrapper_privilege" at the SQL level.
3214 : : * They take various combinations of foreign-data wrapper name,
3215 : : * fdw OID, user name, user OID, or implicit user = current_user.
3216 : : *
3217 : : * The result is a boolean value: true if user has the indicated
3218 : : * privilege, false if not.
3219 : : */
3220 : :
3221 : : /*
3222 : : * has_foreign_data_wrapper_privilege_name_name
3223 : : * Check user privileges on a foreign-data wrapper given
3224 : : * name username, text fdwname, and text priv name.
3225 : : */
3226 : : Datum
3227 : 8 : has_foreign_data_wrapper_privilege_name_name(PG_FUNCTION_ARGS)
3228 : : {
3229 : 8 : Name username = PG_GETARG_NAME(0);
3230 : 8 : text *fdwname = PG_GETARG_TEXT_PP(1);
3231 : 8 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3232 : : Oid roleid;
3233 : : Oid fdwid;
3234 : : AclMode mode;
3235 : : AclResult aclresult;
3236 : :
3237 : 8 : roleid = get_role_oid_or_public(NameStr(*username));
3238 : 8 : fdwid = convert_foreign_data_wrapper_name(fdwname);
3239 : 8 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3240 : :
3241 : 8 : aclresult = object_aclcheck(ForeignDataWrapperRelationId, fdwid, roleid, mode);
3242 : :
3243 : 8 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3244 : : }
3245 : :
3246 : : /*
3247 : : * has_foreign_data_wrapper_privilege_name
3248 : : * Check user privileges on a foreign-data wrapper given
3249 : : * text fdwname and text priv name.
3250 : : * current_user is assumed
3251 : : */
3252 : : Datum
3253 : 4 : has_foreign_data_wrapper_privilege_name(PG_FUNCTION_ARGS)
3254 : : {
3255 : 4 : text *fdwname = PG_GETARG_TEXT_PP(0);
3256 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3257 : : Oid roleid;
3258 : : Oid fdwid;
3259 : : AclMode mode;
3260 : : AclResult aclresult;
3261 : :
3262 : 4 : roleid = GetUserId();
3263 : 4 : fdwid = convert_foreign_data_wrapper_name(fdwname);
3264 : 4 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3265 : :
3266 : 4 : aclresult = object_aclcheck(ForeignDataWrapperRelationId, fdwid, roleid, mode);
3267 : :
3268 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3269 : : }
3270 : :
3271 : : /*
3272 : : * has_foreign_data_wrapper_privilege_name_id
3273 : : * Check user privileges on a foreign-data wrapper given
3274 : : * name usename, foreign-data wrapper oid, and text priv name.
3275 : : */
3276 : : Datum
3277 : 4 : has_foreign_data_wrapper_privilege_name_id(PG_FUNCTION_ARGS)
3278 : : {
3279 : 4 : Name username = PG_GETARG_NAME(0);
3280 : 4 : Oid fdwid = PG_GETARG_OID(1);
3281 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3282 : : Oid roleid;
3283 : : AclMode mode;
3284 : : AclResult aclresult;
3285 : 4 : bool is_missing = false;
3286 : :
3287 : 4 : roleid = get_role_oid_or_public(NameStr(*username));
3288 : 4 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3289 : :
3290 : 4 : aclresult = object_aclcheck_ext(ForeignDataWrapperRelationId, fdwid,
3291 : : roleid, mode,
3292 : : &is_missing);
3293 : :
3294 [ - + ]: 4 : if (is_missing)
3295 : 0 : PG_RETURN_NULL();
3296 : :
3297 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3298 : : }
3299 : :
3300 : : /*
3301 : : * has_foreign_data_wrapper_privilege_id
3302 : : * Check user privileges on a foreign-data wrapper given
3303 : : * foreign-data wrapper oid, and text priv name.
3304 : : * current_user is assumed
3305 : : */
3306 : : Datum
3307 : 4 : has_foreign_data_wrapper_privilege_id(PG_FUNCTION_ARGS)
3308 : : {
3309 : 4 : Oid fdwid = PG_GETARG_OID(0);
3310 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3311 : : Oid roleid;
3312 : : AclMode mode;
3313 : : AclResult aclresult;
3314 : 4 : bool is_missing = false;
3315 : :
3316 : 4 : roleid = GetUserId();
3317 : 4 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3318 : :
3319 : 4 : aclresult = object_aclcheck_ext(ForeignDataWrapperRelationId, fdwid,
3320 : : roleid, mode,
3321 : : &is_missing);
3322 : :
3323 [ - + ]: 4 : if (is_missing)
3324 : 0 : PG_RETURN_NULL();
3325 : :
3326 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3327 : : }
3328 : :
3329 : : /*
3330 : : * has_foreign_data_wrapper_privilege_id_name
3331 : : * Check user privileges on a foreign-data wrapper given
3332 : : * roleid, text fdwname, and text priv name.
3333 : : */
3334 : : Datum
3335 : 4 : has_foreign_data_wrapper_privilege_id_name(PG_FUNCTION_ARGS)
3336 : : {
3337 : 4 : Oid roleid = PG_GETARG_OID(0);
3338 : 4 : text *fdwname = PG_GETARG_TEXT_PP(1);
3339 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3340 : : Oid fdwid;
3341 : : AclMode mode;
3342 : : AclResult aclresult;
3343 : :
3344 : 4 : fdwid = convert_foreign_data_wrapper_name(fdwname);
3345 : 4 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3346 : :
3347 : 4 : aclresult = object_aclcheck(ForeignDataWrapperRelationId, fdwid, roleid, mode);
3348 : :
3349 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3350 : : }
3351 : :
3352 : : /*
3353 : : * has_foreign_data_wrapper_privilege_id_id
3354 : : * Check user privileges on a foreign-data wrapper given
3355 : : * roleid, fdw oid, and text priv name.
3356 : : */
3357 : : Datum
3358 : 4 : has_foreign_data_wrapper_privilege_id_id(PG_FUNCTION_ARGS)
3359 : : {
3360 : 4 : Oid roleid = PG_GETARG_OID(0);
3361 : 4 : Oid fdwid = PG_GETARG_OID(1);
3362 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3363 : : AclMode mode;
3364 : : AclResult aclresult;
3365 : 4 : bool is_missing = false;
3366 : :
3367 : 4 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3368 : :
3369 : 4 : aclresult = object_aclcheck_ext(ForeignDataWrapperRelationId, fdwid,
3370 : : roleid, mode,
3371 : : &is_missing);
3372 : :
3373 [ - + ]: 4 : if (is_missing)
3374 : 0 : PG_RETURN_NULL();
3375 : :
3376 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3377 : : }
3378 : :
3379 : : /*
3380 : : * Support routines for has_foreign_data_wrapper_privilege family.
3381 : : */
3382 : :
3383 : : /*
3384 : : * Given a FDW name expressed as a string, look it up and return Oid
3385 : : */
3386 : : static Oid
3387 : 16 : convert_foreign_data_wrapper_name(text *fdwname)
3388 : : {
3389 : 16 : char *fdwstr = text_to_cstring(fdwname);
3390 : :
3391 : 16 : return get_foreign_data_wrapper_oid(fdwstr, false);
3392 : : }
3393 : :
3394 : : /*
3395 : : * convert_foreign_data_wrapper_priv_string
3396 : : * Convert text string to AclMode value.
3397 : : */
3398 : : static AclMode
3399 : 28 : convert_foreign_data_wrapper_priv_string(text *priv_type_text)
3400 : : {
3401 : : static const priv_map foreign_data_wrapper_priv_map[] = {
3402 : : {"USAGE", ACL_USAGE},
3403 : : {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
3404 : : {NULL, 0}
3405 : : };
3406 : :
3407 : 28 : return convert_any_priv_string(priv_type_text, foreign_data_wrapper_priv_map);
3408 : : }
3409 : :
3410 : :
3411 : : /*
3412 : : * has_function_privilege variants
3413 : : * These are all named "has_function_privilege" at the SQL level.
3414 : : * They take various combinations of function name, function OID,
3415 : : * user name, user OID, or implicit user = current_user.
3416 : : *
3417 : : * The result is a boolean value: true if user has the indicated
3418 : : * privilege, false if not, or NULL if object doesn't exist.
3419 : : */
3420 : :
3421 : : /*
3422 : : * has_function_privilege_name_name
3423 : : * Check user privileges on a function given
3424 : : * name username, text functionname, and text priv name.
3425 : : */
3426 : : Datum
3427 : 120 : has_function_privilege_name_name(PG_FUNCTION_ARGS)
3428 : : {
3429 : 120 : Name username = PG_GETARG_NAME(0);
3430 : 120 : text *functionname = PG_GETARG_TEXT_PP(1);
3431 : 120 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3432 : : Oid roleid;
3433 : : Oid functionoid;
3434 : : AclMode mode;
3435 : : AclResult aclresult;
3436 : :
3437 : 120 : roleid = get_role_oid_or_public(NameStr(*username));
3438 : 120 : functionoid = convert_function_name(functionname);
3439 : 120 : mode = convert_function_priv_string(priv_type_text);
3440 : :
3441 : 120 : aclresult = object_aclcheck(ProcedureRelationId, functionoid, roleid, mode);
3442 : :
3443 : 120 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3444 : : }
3445 : :
3446 : : /*
3447 : : * has_function_privilege_name
3448 : : * Check user privileges on a function given
3449 : : * text functionname and text priv name.
3450 : : * current_user is assumed
3451 : : */
3452 : : Datum
3453 : 0 : has_function_privilege_name(PG_FUNCTION_ARGS)
3454 : : {
3455 : 0 : text *functionname = PG_GETARG_TEXT_PP(0);
3456 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3457 : : Oid roleid;
3458 : : Oid functionoid;
3459 : : AclMode mode;
3460 : : AclResult aclresult;
3461 : :
3462 : 0 : roleid = GetUserId();
3463 : 0 : functionoid = convert_function_name(functionname);
3464 : 0 : mode = convert_function_priv_string(priv_type_text);
3465 : :
3466 : 0 : aclresult = object_aclcheck(ProcedureRelationId, functionoid, roleid, mode);
3467 : :
3468 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3469 : : }
3470 : :
3471 : : /*
3472 : : * has_function_privilege_name_id
3473 : : * Check user privileges on a function given
3474 : : * name usename, function oid, and text priv name.
3475 : : */
3476 : : Datum
3477 : 0 : has_function_privilege_name_id(PG_FUNCTION_ARGS)
3478 : : {
3479 : 0 : Name username = PG_GETARG_NAME(0);
3480 : 0 : Oid functionoid = PG_GETARG_OID(1);
3481 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3482 : : Oid roleid;
3483 : : AclMode mode;
3484 : : AclResult aclresult;
3485 : 0 : bool is_missing = false;
3486 : :
3487 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
3488 : 0 : mode = convert_function_priv_string(priv_type_text);
3489 : :
3490 : 0 : aclresult = object_aclcheck_ext(ProcedureRelationId, functionoid,
3491 : : roleid, mode,
3492 : : &is_missing);
3493 : :
3494 [ # # ]: 0 : if (is_missing)
3495 : 0 : PG_RETURN_NULL();
3496 : :
3497 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3498 : : }
3499 : :
3500 : : /*
3501 : : * has_function_privilege_id
3502 : : * Check user privileges on a function given
3503 : : * function oid, and text priv name.
3504 : : * current_user is assumed
3505 : : */
3506 : : Datum
3507 : 0 : has_function_privilege_id(PG_FUNCTION_ARGS)
3508 : : {
3509 : 0 : Oid functionoid = PG_GETARG_OID(0);
3510 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3511 : : Oid roleid;
3512 : : AclMode mode;
3513 : : AclResult aclresult;
3514 : 0 : bool is_missing = false;
3515 : :
3516 : 0 : roleid = GetUserId();
3517 : 0 : mode = convert_function_priv_string(priv_type_text);
3518 : :
3519 : 0 : aclresult = object_aclcheck_ext(ProcedureRelationId, functionoid,
3520 : : roleid, mode,
3521 : : &is_missing);
3522 : :
3523 [ # # ]: 0 : if (is_missing)
3524 : 0 : PG_RETURN_NULL();
3525 : :
3526 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3527 : : }
3528 : :
3529 : : /*
3530 : : * has_function_privilege_id_name
3531 : : * Check user privileges on a function given
3532 : : * roleid, text functionname, and text priv name.
3533 : : */
3534 : : Datum
3535 : 0 : has_function_privilege_id_name(PG_FUNCTION_ARGS)
3536 : : {
3537 : 0 : Oid roleid = PG_GETARG_OID(0);
3538 : 0 : text *functionname = PG_GETARG_TEXT_PP(1);
3539 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3540 : : Oid functionoid;
3541 : : AclMode mode;
3542 : : AclResult aclresult;
3543 : :
3544 : 0 : functionoid = convert_function_name(functionname);
3545 : 0 : mode = convert_function_priv_string(priv_type_text);
3546 : :
3547 : 0 : aclresult = object_aclcheck(ProcedureRelationId, functionoid, roleid, mode);
3548 : :
3549 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3550 : : }
3551 : :
3552 : : /*
3553 : : * has_function_privilege_id_id
3554 : : * Check user privileges on a function given
3555 : : * roleid, function oid, and text priv name.
3556 : : */
3557 : : Datum
3558 : 0 : has_function_privilege_id_id(PG_FUNCTION_ARGS)
3559 : : {
3560 : 0 : Oid roleid = PG_GETARG_OID(0);
3561 : 0 : Oid functionoid = PG_GETARG_OID(1);
3562 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3563 : : AclMode mode;
3564 : : AclResult aclresult;
3565 : 0 : bool is_missing = false;
3566 : :
3567 : 0 : mode = convert_function_priv_string(priv_type_text);
3568 : :
3569 : 0 : aclresult = object_aclcheck_ext(ProcedureRelationId, functionoid,
3570 : : roleid, mode,
3571 : : &is_missing);
3572 : :
3573 [ # # ]: 0 : if (is_missing)
3574 : 0 : PG_RETURN_NULL();
3575 : :
3576 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3577 : : }
3578 : :
3579 : : /*
3580 : : * Support routines for has_function_privilege family.
3581 : : */
3582 : :
3583 : : /*
3584 : : * Given a function name expressed as a string, look it up and return Oid
3585 : : */
3586 : : static Oid
3587 : 120 : convert_function_name(text *functionname)
3588 : : {
3589 : 120 : char *funcname = text_to_cstring(functionname);
3590 : : Oid oid;
3591 : :
3592 : 120 : oid = DatumGetObjectId(DirectFunctionCall1(regprocedurein,
3593 : : CStringGetDatum(funcname)));
3594 : :
3595 [ - + ]: 120 : if (!OidIsValid(oid))
3596 [ # # ]: 0 : ereport(ERROR,
3597 : : (errcode(ERRCODE_UNDEFINED_FUNCTION),
3598 : : errmsg("function \"%s\" does not exist", funcname)));
3599 : :
3600 : 120 : return oid;
3601 : : }
3602 : :
3603 : : /*
3604 : : * convert_function_priv_string
3605 : : * Convert text string to AclMode value.
3606 : : */
3607 : : static AclMode
3608 : 120 : convert_function_priv_string(text *priv_type_text)
3609 : : {
3610 : : static const priv_map function_priv_map[] = {
3611 : : {"EXECUTE", ACL_EXECUTE},
3612 : : {"EXECUTE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_EXECUTE)},
3613 : : {NULL, 0}
3614 : : };
3615 : :
3616 : 120 : return convert_any_priv_string(priv_type_text, function_priv_map);
3617 : : }
3618 : :
3619 : :
3620 : : /*
3621 : : * has_language_privilege variants
3622 : : * These are all named "has_language_privilege" at the SQL level.
3623 : : * They take various combinations of language name, language OID,
3624 : : * user name, user OID, or implicit user = current_user.
3625 : : *
3626 : : * The result is a boolean value: true if user has the indicated
3627 : : * privilege, false if not, or NULL if object doesn't exist.
3628 : : */
3629 : :
3630 : : /*
3631 : : * has_language_privilege_name_name
3632 : : * Check user privileges on a language given
3633 : : * name username, text languagename, and text priv name.
3634 : : */
3635 : : Datum
3636 : 0 : has_language_privilege_name_name(PG_FUNCTION_ARGS)
3637 : : {
3638 : 0 : Name username = PG_GETARG_NAME(0);
3639 : 0 : text *languagename = PG_GETARG_TEXT_PP(1);
3640 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3641 : : Oid roleid;
3642 : : Oid languageoid;
3643 : : AclMode mode;
3644 : : AclResult aclresult;
3645 : :
3646 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
3647 : 0 : languageoid = convert_language_name(languagename);
3648 : 0 : mode = convert_language_priv_string(priv_type_text);
3649 : :
3650 : 0 : aclresult = object_aclcheck(LanguageRelationId, languageoid, roleid, mode);
3651 : :
3652 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3653 : : }
3654 : :
3655 : : /*
3656 : : * has_language_privilege_name
3657 : : * Check user privileges on a language given
3658 : : * text languagename and text priv name.
3659 : : * current_user is assumed
3660 : : */
3661 : : Datum
3662 : 0 : has_language_privilege_name(PG_FUNCTION_ARGS)
3663 : : {
3664 : 0 : text *languagename = PG_GETARG_TEXT_PP(0);
3665 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3666 : : Oid roleid;
3667 : : Oid languageoid;
3668 : : AclMode mode;
3669 : : AclResult aclresult;
3670 : :
3671 : 0 : roleid = GetUserId();
3672 : 0 : languageoid = convert_language_name(languagename);
3673 : 0 : mode = convert_language_priv_string(priv_type_text);
3674 : :
3675 : 0 : aclresult = object_aclcheck(LanguageRelationId, languageoid, roleid, mode);
3676 : :
3677 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3678 : : }
3679 : :
3680 : : /*
3681 : : * has_language_privilege_name_id
3682 : : * Check user privileges on a language given
3683 : : * name usename, language oid, and text priv name.
3684 : : */
3685 : : Datum
3686 : 0 : has_language_privilege_name_id(PG_FUNCTION_ARGS)
3687 : : {
3688 : 0 : Name username = PG_GETARG_NAME(0);
3689 : 0 : Oid languageoid = PG_GETARG_OID(1);
3690 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3691 : : Oid roleid;
3692 : : AclMode mode;
3693 : : AclResult aclresult;
3694 : 0 : bool is_missing = false;
3695 : :
3696 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
3697 : 0 : mode = convert_language_priv_string(priv_type_text);
3698 : :
3699 : 0 : aclresult = object_aclcheck_ext(LanguageRelationId, languageoid,
3700 : : roleid, mode,
3701 : : &is_missing);
3702 : :
3703 [ # # ]: 0 : if (is_missing)
3704 : 0 : PG_RETURN_NULL();
3705 : :
3706 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3707 : : }
3708 : :
3709 : : /*
3710 : : * has_language_privilege_id
3711 : : * Check user privileges on a language given
3712 : : * language oid, and text priv name.
3713 : : * current_user is assumed
3714 : : */
3715 : : Datum
3716 : 0 : has_language_privilege_id(PG_FUNCTION_ARGS)
3717 : : {
3718 : 0 : Oid languageoid = PG_GETARG_OID(0);
3719 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3720 : : Oid roleid;
3721 : : AclMode mode;
3722 : : AclResult aclresult;
3723 : 0 : bool is_missing = false;
3724 : :
3725 : 0 : roleid = GetUserId();
3726 : 0 : mode = convert_language_priv_string(priv_type_text);
3727 : :
3728 : 0 : aclresult = object_aclcheck_ext(LanguageRelationId, languageoid,
3729 : : roleid, mode,
3730 : : &is_missing);
3731 : :
3732 [ # # ]: 0 : if (is_missing)
3733 : 0 : PG_RETURN_NULL();
3734 : :
3735 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3736 : : }
3737 : :
3738 : : /*
3739 : : * has_language_privilege_id_name
3740 : : * Check user privileges on a language given
3741 : : * roleid, text languagename, and text priv name.
3742 : : */
3743 : : Datum
3744 : 0 : has_language_privilege_id_name(PG_FUNCTION_ARGS)
3745 : : {
3746 : 0 : Oid roleid = PG_GETARG_OID(0);
3747 : 0 : text *languagename = PG_GETARG_TEXT_PP(1);
3748 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3749 : : Oid languageoid;
3750 : : AclMode mode;
3751 : : AclResult aclresult;
3752 : :
3753 : 0 : languageoid = convert_language_name(languagename);
3754 : 0 : mode = convert_language_priv_string(priv_type_text);
3755 : :
3756 : 0 : aclresult = object_aclcheck(LanguageRelationId, languageoid, roleid, mode);
3757 : :
3758 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3759 : : }
3760 : :
3761 : : /*
3762 : : * has_language_privilege_id_id
3763 : : * Check user privileges on a language given
3764 : : * roleid, language oid, and text priv name.
3765 : : */
3766 : : Datum
3767 : 0 : has_language_privilege_id_id(PG_FUNCTION_ARGS)
3768 : : {
3769 : 0 : Oid roleid = PG_GETARG_OID(0);
3770 : 0 : Oid languageoid = PG_GETARG_OID(1);
3771 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3772 : : AclMode mode;
3773 : : AclResult aclresult;
3774 : 0 : bool is_missing = false;
3775 : :
3776 : 0 : mode = convert_language_priv_string(priv_type_text);
3777 : :
3778 : 0 : aclresult = object_aclcheck_ext(LanguageRelationId, languageoid,
3779 : : roleid, mode,
3780 : : &is_missing);
3781 : :
3782 [ # # ]: 0 : if (is_missing)
3783 : 0 : PG_RETURN_NULL();
3784 : :
3785 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3786 : : }
3787 : :
3788 : : /*
3789 : : * Support routines for has_language_privilege family.
3790 : : */
3791 : :
3792 : : /*
3793 : : * Given a language name expressed as a string, look it up and return Oid
3794 : : */
3795 : : static Oid
3796 : 0 : convert_language_name(text *languagename)
3797 : : {
3798 : 0 : char *langname = text_to_cstring(languagename);
3799 : :
3800 : 0 : return get_language_oid(langname, false);
3801 : : }
3802 : :
3803 : : /*
3804 : : * convert_language_priv_string
3805 : : * Convert text string to AclMode value.
3806 : : */
3807 : : static AclMode
3808 : 0 : convert_language_priv_string(text *priv_type_text)
3809 : : {
3810 : : static const priv_map language_priv_map[] = {
3811 : : {"USAGE", ACL_USAGE},
3812 : : {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
3813 : : {NULL, 0}
3814 : : };
3815 : :
3816 : 0 : return convert_any_priv_string(priv_type_text, language_priv_map);
3817 : : }
3818 : :
3819 : :
3820 : : /*
3821 : : * has_schema_privilege variants
3822 : : * These are all named "has_schema_privilege" at the SQL level.
3823 : : * They take various combinations of schema name, schema OID,
3824 : : * user name, user OID, or implicit user = current_user.
3825 : : *
3826 : : * The result is a boolean value: true if user has the indicated
3827 : : * privilege, false if not, or NULL if object doesn't exist.
3828 : : */
3829 : :
3830 : : /*
3831 : : * has_schema_privilege_name_name
3832 : : * Check user privileges on a schema given
3833 : : * name username, text schemaname, and text priv name.
3834 : : */
3835 : : Datum
3836 : 36 : has_schema_privilege_name_name(PG_FUNCTION_ARGS)
3837 : : {
3838 : 36 : Name username = PG_GETARG_NAME(0);
3839 : 36 : text *schemaname = PG_GETARG_TEXT_PP(1);
3840 : 36 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3841 : : Oid roleid;
3842 : : Oid schemaoid;
3843 : : AclMode mode;
3844 : : AclResult aclresult;
3845 : :
3846 : 36 : roleid = get_role_oid_or_public(NameStr(*username));
3847 : 36 : schemaoid = convert_schema_name(schemaname);
3848 : 36 : mode = convert_schema_priv_string(priv_type_text);
3849 : :
3850 : 36 : aclresult = object_aclcheck(NamespaceRelationId, schemaoid, roleid, mode);
3851 : :
3852 : 36 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3853 : : }
3854 : :
3855 : : /*
3856 : : * has_schema_privilege_name
3857 : : * Check user privileges on a schema given
3858 : : * text schemaname and text priv name.
3859 : : * current_user is assumed
3860 : : */
3861 : : Datum
3862 : 0 : has_schema_privilege_name(PG_FUNCTION_ARGS)
3863 : : {
3864 : 0 : text *schemaname = PG_GETARG_TEXT_PP(0);
3865 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3866 : : Oid roleid;
3867 : : Oid schemaoid;
3868 : : AclMode mode;
3869 : : AclResult aclresult;
3870 : :
3871 : 0 : roleid = GetUserId();
3872 : 0 : schemaoid = convert_schema_name(schemaname);
3873 : 0 : mode = convert_schema_priv_string(priv_type_text);
3874 : :
3875 : 0 : aclresult = object_aclcheck(NamespaceRelationId, schemaoid, roleid, mode);
3876 : :
3877 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3878 : : }
3879 : :
3880 : : /*
3881 : : * has_schema_privilege_name_id
3882 : : * Check user privileges on a schema given
3883 : : * name usename, schema oid, and text priv name.
3884 : : */
3885 : : Datum
3886 : 0 : has_schema_privilege_name_id(PG_FUNCTION_ARGS)
3887 : : {
3888 : 0 : Name username = PG_GETARG_NAME(0);
3889 : 0 : Oid schemaoid = PG_GETARG_OID(1);
3890 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3891 : : Oid roleid;
3892 : : AclMode mode;
3893 : : AclResult aclresult;
3894 : 0 : bool is_missing = false;
3895 : :
3896 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
3897 : 0 : mode = convert_schema_priv_string(priv_type_text);
3898 : :
3899 : 0 : aclresult = object_aclcheck_ext(NamespaceRelationId, schemaoid,
3900 : : roleid, mode,
3901 : : &is_missing);
3902 : :
3903 [ # # ]: 0 : if (is_missing)
3904 : 0 : PG_RETURN_NULL();
3905 : :
3906 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3907 : : }
3908 : :
3909 : : /*
3910 : : * has_schema_privilege_id
3911 : : * Check user privileges on a schema given
3912 : : * schema oid, and text priv name.
3913 : : * current_user is assumed
3914 : : */
3915 : : Datum
3916 : 0 : has_schema_privilege_id(PG_FUNCTION_ARGS)
3917 : : {
3918 : 0 : Oid schemaoid = PG_GETARG_OID(0);
3919 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3920 : : Oid roleid;
3921 : : AclMode mode;
3922 : : AclResult aclresult;
3923 : 0 : bool is_missing = false;
3924 : :
3925 : 0 : roleid = GetUserId();
3926 : 0 : mode = convert_schema_priv_string(priv_type_text);
3927 : :
3928 : 0 : aclresult = object_aclcheck_ext(NamespaceRelationId, schemaoid,
3929 : : roleid, mode,
3930 : : &is_missing);
3931 : :
3932 [ # # ]: 0 : if (is_missing)
3933 : 0 : PG_RETURN_NULL();
3934 : :
3935 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3936 : : }
3937 : :
3938 : : /*
3939 : : * has_schema_privilege_id_name
3940 : : * Check user privileges on a schema given
3941 : : * roleid, text schemaname, and text priv name.
3942 : : */
3943 : : Datum
3944 : 0 : has_schema_privilege_id_name(PG_FUNCTION_ARGS)
3945 : : {
3946 : 0 : Oid roleid = PG_GETARG_OID(0);
3947 : 0 : text *schemaname = PG_GETARG_TEXT_PP(1);
3948 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3949 : : Oid schemaoid;
3950 : : AclMode mode;
3951 : : AclResult aclresult;
3952 : :
3953 : 0 : schemaoid = convert_schema_name(schemaname);
3954 : 0 : mode = convert_schema_priv_string(priv_type_text);
3955 : :
3956 : 0 : aclresult = object_aclcheck(NamespaceRelationId, schemaoid, roleid, mode);
3957 : :
3958 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3959 : : }
3960 : :
3961 : : /*
3962 : : * has_schema_privilege_id_id
3963 : : * Check user privileges on a schema given
3964 : : * roleid, schema oid, and text priv name.
3965 : : */
3966 : : Datum
3967 : 0 : has_schema_privilege_id_id(PG_FUNCTION_ARGS)
3968 : : {
3969 : 0 : Oid roleid = PG_GETARG_OID(0);
3970 : 0 : Oid schemaoid = PG_GETARG_OID(1);
3971 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3972 : : AclMode mode;
3973 : : AclResult aclresult;
3974 : 0 : bool is_missing = false;
3975 : :
3976 : 0 : mode = convert_schema_priv_string(priv_type_text);
3977 : :
3978 : 0 : aclresult = object_aclcheck_ext(NamespaceRelationId, schemaoid,
3979 : : roleid, mode,
3980 : : &is_missing);
3981 : :
3982 [ # # ]: 0 : if (is_missing)
3983 : 0 : PG_RETURN_NULL();
3984 : :
3985 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3986 : : }
3987 : :
3988 : : /*
3989 : : * Support routines for has_schema_privilege family.
3990 : : */
3991 : :
3992 : : /*
3993 : : * Given a schema name expressed as a string, look it up and return Oid
3994 : : */
3995 : : static Oid
3996 : 36 : convert_schema_name(text *schemaname)
3997 : : {
3998 : 36 : char *nspname = text_to_cstring(schemaname);
3999 : :
4000 : 36 : return get_namespace_oid(nspname, false);
4001 : : }
4002 : :
4003 : : /*
4004 : : * convert_schema_priv_string
4005 : : * Convert text string to AclMode value.
4006 : : */
4007 : : static AclMode
4008 : 36 : convert_schema_priv_string(text *priv_type_text)
4009 : : {
4010 : : static const priv_map schema_priv_map[] = {
4011 : : {"CREATE", ACL_CREATE},
4012 : : {"CREATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
4013 : : {"USAGE", ACL_USAGE},
4014 : : {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
4015 : : {NULL, 0}
4016 : : };
4017 : :
4018 : 36 : return convert_any_priv_string(priv_type_text, schema_priv_map);
4019 : : }
4020 : :
4021 : :
4022 : : /*
4023 : : * has_server_privilege variants
4024 : : * These are all named "has_server_privilege" at the SQL level.
4025 : : * They take various combinations of foreign server name,
4026 : : * server OID, user name, user OID, or implicit user = current_user.
4027 : : *
4028 : : * The result is a boolean value: true if user has the indicated
4029 : : * privilege, false if not.
4030 : : */
4031 : :
4032 : : /*
4033 : : * has_server_privilege_name_name
4034 : : * Check user privileges on a foreign server given
4035 : : * name username, text servername, and text priv name.
4036 : : */
4037 : : Datum
4038 : 8 : has_server_privilege_name_name(PG_FUNCTION_ARGS)
4039 : : {
4040 : 8 : Name username = PG_GETARG_NAME(0);
4041 : 8 : text *servername = PG_GETARG_TEXT_PP(1);
4042 : 8 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4043 : : Oid roleid;
4044 : : Oid serverid;
4045 : : AclMode mode;
4046 : : AclResult aclresult;
4047 : :
4048 : 8 : roleid = get_role_oid_or_public(NameStr(*username));
4049 : 8 : serverid = convert_server_name(servername);
4050 : 8 : mode = convert_server_priv_string(priv_type_text);
4051 : :
4052 : 8 : aclresult = object_aclcheck(ForeignServerRelationId, serverid, roleid, mode);
4053 : :
4054 : 8 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4055 : : }
4056 : :
4057 : : /*
4058 : : * has_server_privilege_name
4059 : : * Check user privileges on a foreign server given
4060 : : * text servername and text priv name.
4061 : : * current_user is assumed
4062 : : */
4063 : : Datum
4064 : 4 : has_server_privilege_name(PG_FUNCTION_ARGS)
4065 : : {
4066 : 4 : text *servername = PG_GETARG_TEXT_PP(0);
4067 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4068 : : Oid roleid;
4069 : : Oid serverid;
4070 : : AclMode mode;
4071 : : AclResult aclresult;
4072 : :
4073 : 4 : roleid = GetUserId();
4074 : 4 : serverid = convert_server_name(servername);
4075 : 4 : mode = convert_server_priv_string(priv_type_text);
4076 : :
4077 : 4 : aclresult = object_aclcheck(ForeignServerRelationId, serverid, roleid, mode);
4078 : :
4079 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4080 : : }
4081 : :
4082 : : /*
4083 : : * has_server_privilege_name_id
4084 : : * Check user privileges on a foreign server given
4085 : : * name usename, foreign server oid, and text priv name.
4086 : : */
4087 : : Datum
4088 : 4 : has_server_privilege_name_id(PG_FUNCTION_ARGS)
4089 : : {
4090 : 4 : Name username = PG_GETARG_NAME(0);
4091 : 4 : Oid serverid = PG_GETARG_OID(1);
4092 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4093 : : Oid roleid;
4094 : : AclMode mode;
4095 : : AclResult aclresult;
4096 : 4 : bool is_missing = false;
4097 : :
4098 : 4 : roleid = get_role_oid_or_public(NameStr(*username));
4099 : 4 : mode = convert_server_priv_string(priv_type_text);
4100 : :
4101 : 4 : aclresult = object_aclcheck_ext(ForeignServerRelationId, serverid,
4102 : : roleid, mode,
4103 : : &is_missing);
4104 : :
4105 [ - + ]: 4 : if (is_missing)
4106 : 0 : PG_RETURN_NULL();
4107 : :
4108 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4109 : : }
4110 : :
4111 : : /*
4112 : : * has_server_privilege_id
4113 : : * Check user privileges on a foreign server given
4114 : : * server oid, and text priv name.
4115 : : * current_user is assumed
4116 : : */
4117 : : Datum
4118 : 52 : has_server_privilege_id(PG_FUNCTION_ARGS)
4119 : : {
4120 : 52 : Oid serverid = PG_GETARG_OID(0);
4121 : 52 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4122 : : Oid roleid;
4123 : : AclMode mode;
4124 : : AclResult aclresult;
4125 : 52 : bool is_missing = false;
4126 : :
4127 : 52 : roleid = GetUserId();
4128 : 52 : mode = convert_server_priv_string(priv_type_text);
4129 : :
4130 : 52 : aclresult = object_aclcheck_ext(ForeignServerRelationId, serverid,
4131 : : roleid, mode,
4132 : : &is_missing);
4133 : :
4134 [ - + ]: 52 : if (is_missing)
4135 : 0 : PG_RETURN_NULL();
4136 : :
4137 : 52 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4138 : : }
4139 : :
4140 : : /*
4141 : : * has_server_privilege_id_name
4142 : : * Check user privileges on a foreign server given
4143 : : * roleid, text servername, and text priv name.
4144 : : */
4145 : : Datum
4146 : 4 : has_server_privilege_id_name(PG_FUNCTION_ARGS)
4147 : : {
4148 : 4 : Oid roleid = PG_GETARG_OID(0);
4149 : 4 : text *servername = PG_GETARG_TEXT_PP(1);
4150 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4151 : : Oid serverid;
4152 : : AclMode mode;
4153 : : AclResult aclresult;
4154 : :
4155 : 4 : serverid = convert_server_name(servername);
4156 : 4 : mode = convert_server_priv_string(priv_type_text);
4157 : :
4158 : 4 : aclresult = object_aclcheck(ForeignServerRelationId, serverid, roleid, mode);
4159 : :
4160 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4161 : : }
4162 : :
4163 : : /*
4164 : : * has_server_privilege_id_id
4165 : : * Check user privileges on a foreign server given
4166 : : * roleid, server oid, and text priv name.
4167 : : */
4168 : : Datum
4169 : 4 : has_server_privilege_id_id(PG_FUNCTION_ARGS)
4170 : : {
4171 : 4 : Oid roleid = PG_GETARG_OID(0);
4172 : 4 : Oid serverid = PG_GETARG_OID(1);
4173 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4174 : : AclMode mode;
4175 : : AclResult aclresult;
4176 : 4 : bool is_missing = false;
4177 : :
4178 : 4 : mode = convert_server_priv_string(priv_type_text);
4179 : :
4180 : 4 : aclresult = object_aclcheck_ext(ForeignServerRelationId, serverid,
4181 : : roleid, mode,
4182 : : &is_missing);
4183 : :
4184 [ - + ]: 4 : if (is_missing)
4185 : 0 : PG_RETURN_NULL();
4186 : :
4187 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4188 : : }
4189 : :
4190 : : /*
4191 : : * Support routines for has_server_privilege family.
4192 : : */
4193 : :
4194 : : /*
4195 : : * Given a server name expressed as a string, look it up and return Oid
4196 : : */
4197 : : static Oid
4198 : 16 : convert_server_name(text *servername)
4199 : : {
4200 : 16 : char *serverstr = text_to_cstring(servername);
4201 : :
4202 : 16 : return get_foreign_server_oid(serverstr, false);
4203 : : }
4204 : :
4205 : : /*
4206 : : * convert_server_priv_string
4207 : : * Convert text string to AclMode value.
4208 : : */
4209 : : static AclMode
4210 : 76 : convert_server_priv_string(text *priv_type_text)
4211 : : {
4212 : : static const priv_map server_priv_map[] = {
4213 : : {"USAGE", ACL_USAGE},
4214 : : {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
4215 : : {NULL, 0}
4216 : : };
4217 : :
4218 : 76 : return convert_any_priv_string(priv_type_text, server_priv_map);
4219 : : }
4220 : :
4221 : :
4222 : : /*
4223 : : * has_tablespace_privilege variants
4224 : : * These are all named "has_tablespace_privilege" at the SQL level.
4225 : : * They take various combinations of tablespace name, tablespace OID,
4226 : : * user name, user OID, or implicit user = current_user.
4227 : : *
4228 : : * The result is a boolean value: true if user has the indicated
4229 : : * privilege, false if not.
4230 : : */
4231 : :
4232 : : /*
4233 : : * has_tablespace_privilege_name_name
4234 : : * Check user privileges on a tablespace given
4235 : : * name username, text tablespacename, and text priv name.
4236 : : */
4237 : : Datum
4238 : 0 : has_tablespace_privilege_name_name(PG_FUNCTION_ARGS)
4239 : : {
4240 : 0 : Name username = PG_GETARG_NAME(0);
4241 : 0 : text *tablespacename = PG_GETARG_TEXT_PP(1);
4242 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4243 : : Oid roleid;
4244 : : Oid tablespaceoid;
4245 : : AclMode mode;
4246 : : AclResult aclresult;
4247 : :
4248 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
4249 : 0 : tablespaceoid = convert_tablespace_name(tablespacename);
4250 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4251 : :
4252 : 0 : aclresult = object_aclcheck(TableSpaceRelationId, tablespaceoid, roleid, mode);
4253 : :
4254 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4255 : : }
4256 : :
4257 : : /*
4258 : : * has_tablespace_privilege_name
4259 : : * Check user privileges on a tablespace given
4260 : : * text tablespacename and text priv name.
4261 : : * current_user is assumed
4262 : : */
4263 : : Datum
4264 : 0 : has_tablespace_privilege_name(PG_FUNCTION_ARGS)
4265 : : {
4266 : 0 : text *tablespacename = PG_GETARG_TEXT_PP(0);
4267 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4268 : : Oid roleid;
4269 : : Oid tablespaceoid;
4270 : : AclMode mode;
4271 : : AclResult aclresult;
4272 : :
4273 : 0 : roleid = GetUserId();
4274 : 0 : tablespaceoid = convert_tablespace_name(tablespacename);
4275 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4276 : :
4277 : 0 : aclresult = object_aclcheck(TableSpaceRelationId, tablespaceoid, roleid, mode);
4278 : :
4279 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4280 : : }
4281 : :
4282 : : /*
4283 : : * has_tablespace_privilege_name_id
4284 : : * Check user privileges on a tablespace given
4285 : : * name usename, tablespace oid, and text priv name.
4286 : : */
4287 : : Datum
4288 : 0 : has_tablespace_privilege_name_id(PG_FUNCTION_ARGS)
4289 : : {
4290 : 0 : Name username = PG_GETARG_NAME(0);
4291 : 0 : Oid tablespaceoid = PG_GETARG_OID(1);
4292 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4293 : : Oid roleid;
4294 : : AclMode mode;
4295 : : AclResult aclresult;
4296 : 0 : bool is_missing = false;
4297 : :
4298 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
4299 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4300 : :
4301 : 0 : aclresult = object_aclcheck_ext(TableSpaceRelationId, tablespaceoid,
4302 : : roleid, mode,
4303 : : &is_missing);
4304 : :
4305 [ # # ]: 0 : if (is_missing)
4306 : 0 : PG_RETURN_NULL();
4307 : :
4308 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4309 : : }
4310 : :
4311 : : /*
4312 : : * has_tablespace_privilege_id
4313 : : * Check user privileges on a tablespace given
4314 : : * tablespace oid, and text priv name.
4315 : : * current_user is assumed
4316 : : */
4317 : : Datum
4318 : 0 : has_tablespace_privilege_id(PG_FUNCTION_ARGS)
4319 : : {
4320 : 0 : Oid tablespaceoid = PG_GETARG_OID(0);
4321 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4322 : : Oid roleid;
4323 : : AclMode mode;
4324 : : AclResult aclresult;
4325 : 0 : bool is_missing = false;
4326 : :
4327 : 0 : roleid = GetUserId();
4328 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4329 : :
4330 : 0 : aclresult = object_aclcheck_ext(TableSpaceRelationId, tablespaceoid,
4331 : : roleid, mode,
4332 : : &is_missing);
4333 : :
4334 [ # # ]: 0 : if (is_missing)
4335 : 0 : PG_RETURN_NULL();
4336 : :
4337 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4338 : : }
4339 : :
4340 : : /*
4341 : : * has_tablespace_privilege_id_name
4342 : : * Check user privileges on a tablespace given
4343 : : * roleid, text tablespacename, and text priv name.
4344 : : */
4345 : : Datum
4346 : 0 : has_tablespace_privilege_id_name(PG_FUNCTION_ARGS)
4347 : : {
4348 : 0 : Oid roleid = PG_GETARG_OID(0);
4349 : 0 : text *tablespacename = PG_GETARG_TEXT_PP(1);
4350 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4351 : : Oid tablespaceoid;
4352 : : AclMode mode;
4353 : : AclResult aclresult;
4354 : :
4355 : 0 : tablespaceoid = convert_tablespace_name(tablespacename);
4356 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4357 : :
4358 : 0 : aclresult = object_aclcheck(TableSpaceRelationId, tablespaceoid, roleid, mode);
4359 : :
4360 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4361 : : }
4362 : :
4363 : : /*
4364 : : * has_tablespace_privilege_id_id
4365 : : * Check user privileges on a tablespace given
4366 : : * roleid, tablespace oid, and text priv name.
4367 : : */
4368 : : Datum
4369 : 0 : has_tablespace_privilege_id_id(PG_FUNCTION_ARGS)
4370 : : {
4371 : 0 : Oid roleid = PG_GETARG_OID(0);
4372 : 0 : Oid tablespaceoid = PG_GETARG_OID(1);
4373 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4374 : : AclMode mode;
4375 : : AclResult aclresult;
4376 : 0 : bool is_missing = false;
4377 : :
4378 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4379 : :
4380 : 0 : aclresult = object_aclcheck_ext(TableSpaceRelationId, tablespaceoid,
4381 : : roleid, mode,
4382 : : &is_missing);
4383 : :
4384 [ # # ]: 0 : if (is_missing)
4385 : 0 : PG_RETURN_NULL();
4386 : :
4387 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4388 : : }
4389 : :
4390 : : /*
4391 : : * Support routines for has_tablespace_privilege family.
4392 : : */
4393 : :
4394 : : /*
4395 : : * Given a tablespace name expressed as a string, look it up and return Oid
4396 : : */
4397 : : static Oid
4398 : 0 : convert_tablespace_name(text *tablespacename)
4399 : : {
4400 : 0 : char *spcname = text_to_cstring(tablespacename);
4401 : :
4402 : 0 : return get_tablespace_oid(spcname, false);
4403 : : }
4404 : :
4405 : : /*
4406 : : * convert_tablespace_priv_string
4407 : : * Convert text string to AclMode value.
4408 : : */
4409 : : static AclMode
4410 : 0 : convert_tablespace_priv_string(text *priv_type_text)
4411 : : {
4412 : : static const priv_map tablespace_priv_map[] = {
4413 : : {"CREATE", ACL_CREATE},
4414 : : {"CREATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
4415 : : {NULL, 0}
4416 : : };
4417 : :
4418 : 0 : return convert_any_priv_string(priv_type_text, tablespace_priv_map);
4419 : : }
4420 : :
4421 : : /*
4422 : : * has_type_privilege variants
4423 : : * These are all named "has_type_privilege" at the SQL level.
4424 : : * They take various combinations of type name, type OID,
4425 : : * user name, user OID, or implicit user = current_user.
4426 : : *
4427 : : * The result is a boolean value: true if user has the indicated
4428 : : * privilege, false if not, or NULL if object doesn't exist.
4429 : : */
4430 : :
4431 : : /*
4432 : : * has_type_privilege_name_name
4433 : : * Check user privileges on a type given
4434 : : * name username, text typename, and text priv name.
4435 : : */
4436 : : Datum
4437 : 8 : has_type_privilege_name_name(PG_FUNCTION_ARGS)
4438 : : {
4439 : 8 : Name username = PG_GETARG_NAME(0);
4440 : 8 : text *typename = PG_GETARG_TEXT_PP(1);
4441 : 8 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4442 : : Oid roleid;
4443 : : Oid typeoid;
4444 : : AclMode mode;
4445 : : AclResult aclresult;
4446 : :
4447 : 8 : roleid = get_role_oid_or_public(NameStr(*username));
4448 : 8 : typeoid = convert_type_name(typename);
4449 : 8 : mode = convert_type_priv_string(priv_type_text);
4450 : :
4451 : 8 : aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
4452 : :
4453 : 8 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4454 : : }
4455 : :
4456 : : /*
4457 : : * has_type_privilege_name
4458 : : * Check user privileges on a type given
4459 : : * text typename and text priv name.
4460 : : * current_user is assumed
4461 : : */
4462 : : Datum
4463 : 0 : has_type_privilege_name(PG_FUNCTION_ARGS)
4464 : : {
4465 : 0 : text *typename = PG_GETARG_TEXT_PP(0);
4466 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4467 : : Oid roleid;
4468 : : Oid typeoid;
4469 : : AclMode mode;
4470 : : AclResult aclresult;
4471 : :
4472 : 0 : roleid = GetUserId();
4473 : 0 : typeoid = convert_type_name(typename);
4474 : 0 : mode = convert_type_priv_string(priv_type_text);
4475 : :
4476 : 0 : aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
4477 : :
4478 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4479 : : }
4480 : :
4481 : : /*
4482 : : * has_type_privilege_name_id
4483 : : * Check user privileges on a type given
4484 : : * name usename, type oid, and text priv name.
4485 : : */
4486 : : Datum
4487 : 0 : has_type_privilege_name_id(PG_FUNCTION_ARGS)
4488 : : {
4489 : 0 : Name username = PG_GETARG_NAME(0);
4490 : 0 : Oid typeoid = PG_GETARG_OID(1);
4491 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4492 : : Oid roleid;
4493 : : AclMode mode;
4494 : : AclResult aclresult;
4495 : 0 : bool is_missing = false;
4496 : :
4497 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
4498 : 0 : mode = convert_type_priv_string(priv_type_text);
4499 : :
4500 : 0 : aclresult = object_aclcheck_ext(TypeRelationId, typeoid,
4501 : : roleid, mode,
4502 : : &is_missing);
4503 : :
4504 [ # # ]: 0 : if (is_missing)
4505 : 0 : PG_RETURN_NULL();
4506 : :
4507 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4508 : : }
4509 : :
4510 : : /*
4511 : : * has_type_privilege_id
4512 : : * Check user privileges on a type given
4513 : : * type oid, and text priv name.
4514 : : * current_user is assumed
4515 : : */
4516 : : Datum
4517 : 0 : has_type_privilege_id(PG_FUNCTION_ARGS)
4518 : : {
4519 : 0 : Oid typeoid = PG_GETARG_OID(0);
4520 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4521 : : Oid roleid;
4522 : : AclMode mode;
4523 : : AclResult aclresult;
4524 : 0 : bool is_missing = false;
4525 : :
4526 : 0 : roleid = GetUserId();
4527 : 0 : mode = convert_type_priv_string(priv_type_text);
4528 : :
4529 : 0 : aclresult = object_aclcheck_ext(TypeRelationId, typeoid,
4530 : : roleid, mode,
4531 : : &is_missing);
4532 : :
4533 [ # # ]: 0 : if (is_missing)
4534 : 0 : PG_RETURN_NULL();
4535 : :
4536 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4537 : : }
4538 : :
4539 : : /*
4540 : : * has_type_privilege_id_name
4541 : : * Check user privileges on a type given
4542 : : * roleid, text typename, and text priv name.
4543 : : */
4544 : : Datum
4545 : 0 : has_type_privilege_id_name(PG_FUNCTION_ARGS)
4546 : : {
4547 : 0 : Oid roleid = PG_GETARG_OID(0);
4548 : 0 : text *typename = PG_GETARG_TEXT_PP(1);
4549 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4550 : : Oid typeoid;
4551 : : AclMode mode;
4552 : : AclResult aclresult;
4553 : :
4554 : 0 : typeoid = convert_type_name(typename);
4555 : 0 : mode = convert_type_priv_string(priv_type_text);
4556 : :
4557 : 0 : aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
4558 : :
4559 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4560 : : }
4561 : :
4562 : : /*
4563 : : * has_type_privilege_id_id
4564 : : * Check user privileges on a type given
4565 : : * roleid, type oid, and text priv name.
4566 : : */
4567 : : Datum
4568 : 0 : has_type_privilege_id_id(PG_FUNCTION_ARGS)
4569 : : {
4570 : 0 : Oid roleid = PG_GETARG_OID(0);
4571 : 0 : Oid typeoid = PG_GETARG_OID(1);
4572 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4573 : : AclMode mode;
4574 : : AclResult aclresult;
4575 : 0 : bool is_missing = false;
4576 : :
4577 : 0 : mode = convert_type_priv_string(priv_type_text);
4578 : :
4579 : 0 : aclresult = object_aclcheck_ext(TypeRelationId, typeoid,
4580 : : roleid, mode,
4581 : : &is_missing);
4582 : :
4583 [ # # ]: 0 : if (is_missing)
4584 : 0 : PG_RETURN_NULL();
4585 : :
4586 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4587 : : }
4588 : :
4589 : : /*
4590 : : * Support routines for has_type_privilege family.
4591 : : */
4592 : :
4593 : : /*
4594 : : * Given a type name expressed as a string, look it up and return Oid
4595 : : */
4596 : : static Oid
4597 : 8 : convert_type_name(text *typename)
4598 : : {
4599 : 8 : char *typname = text_to_cstring(typename);
4600 : : Oid oid;
4601 : :
4602 : 8 : oid = DatumGetObjectId(DirectFunctionCall1(regtypein,
4603 : : CStringGetDatum(typname)));
4604 : :
4605 [ - + ]: 8 : if (!OidIsValid(oid))
4606 [ # # ]: 0 : ereport(ERROR,
4607 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
4608 : : errmsg("type \"%s\" does not exist", typname)));
4609 : :
4610 : 8 : return oid;
4611 : : }
4612 : :
4613 : : /*
4614 : : * convert_type_priv_string
4615 : : * Convert text string to AclMode value.
4616 : : */
4617 : : static AclMode
4618 : 8 : convert_type_priv_string(text *priv_type_text)
4619 : : {
4620 : : static const priv_map type_priv_map[] = {
4621 : : {"USAGE", ACL_USAGE},
4622 : : {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
4623 : : {NULL, 0}
4624 : : };
4625 : :
4626 : 8 : return convert_any_priv_string(priv_type_text, type_priv_map);
4627 : : }
4628 : :
4629 : : /*
4630 : : * has_parameter_privilege variants
4631 : : * These are all named "has_parameter_privilege" at the SQL level.
4632 : : * They take various combinations of parameter name with
4633 : : * user name, user OID, or implicit user = current_user.
4634 : : *
4635 : : * The result is a boolean value: true if user has been granted
4636 : : * the indicated privilege or false if not.
4637 : : */
4638 : :
4639 : : /*
4640 : : * has_param_priv_byname
4641 : : *
4642 : : * Helper function to check user privileges on a parameter given the
4643 : : * role by Oid, parameter by text name, and privileges as AclMode.
4644 : : */
4645 : : static bool
4646 : 37 : has_param_priv_byname(Oid roleid, const text *parameter, AclMode priv)
4647 : : {
4648 : 37 : char *paramstr = text_to_cstring(parameter);
4649 : :
4650 : 37 : return pg_parameter_aclcheck(paramstr, roleid, priv) == ACLCHECK_OK;
4651 : : }
4652 : :
4653 : : /*
4654 : : * has_parameter_privilege_name_name
4655 : : * Check user privileges on a parameter given name username, text
4656 : : * parameter, and text priv name.
4657 : : */
4658 : : Datum
4659 : 42 : has_parameter_privilege_name_name(PG_FUNCTION_ARGS)
4660 : : {
4661 : 42 : Name username = PG_GETARG_NAME(0);
4662 : 42 : text *parameter = PG_GETARG_TEXT_PP(1);
4663 : 42 : AclMode priv = convert_parameter_priv_string(PG_GETARG_TEXT_PP(2));
4664 : 35 : Oid roleid = get_role_oid_or_public(NameStr(*username));
4665 : :
4666 : 35 : PG_RETURN_BOOL(has_param_priv_byname(roleid, parameter, priv));
4667 : : }
4668 : :
4669 : : /*
4670 : : * has_parameter_privilege_name
4671 : : * Check user privileges on a parameter given text parameter and text priv
4672 : : * name. current_user is assumed
4673 : : */
4674 : : Datum
4675 : 1 : has_parameter_privilege_name(PG_FUNCTION_ARGS)
4676 : : {
4677 : 1 : text *parameter = PG_GETARG_TEXT_PP(0);
4678 : 1 : AclMode priv = convert_parameter_priv_string(PG_GETARG_TEXT_PP(1));
4679 : :
4680 : 1 : PG_RETURN_BOOL(has_param_priv_byname(GetUserId(), parameter, priv));
4681 : : }
4682 : :
4683 : : /*
4684 : : * has_parameter_privilege_id_name
4685 : : * Check user privileges on a parameter given roleid, text parameter, and
4686 : : * text priv name.
4687 : : */
4688 : : Datum
4689 : 1 : has_parameter_privilege_id_name(PG_FUNCTION_ARGS)
4690 : : {
4691 : 1 : Oid roleid = PG_GETARG_OID(0);
4692 : 1 : text *parameter = PG_GETARG_TEXT_PP(1);
4693 : 1 : AclMode priv = convert_parameter_priv_string(PG_GETARG_TEXT_PP(2));
4694 : :
4695 : 1 : PG_RETURN_BOOL(has_param_priv_byname(roleid, parameter, priv));
4696 : : }
4697 : :
4698 : : /*
4699 : : * Support routines for has_parameter_privilege family.
4700 : : */
4701 : :
4702 : : /*
4703 : : * convert_parameter_priv_string
4704 : : * Convert text string to AclMode value.
4705 : : */
4706 : : static AclMode
4707 : 44 : convert_parameter_priv_string(text *priv_text)
4708 : : {
4709 : : static const priv_map parameter_priv_map[] = {
4710 : : {"SET", ACL_SET},
4711 : : {"SET WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SET)},
4712 : : {"ALTER SYSTEM", ACL_ALTER_SYSTEM},
4713 : : {"ALTER SYSTEM WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_ALTER_SYSTEM)},
4714 : : {NULL, 0}
4715 : : };
4716 : :
4717 : 44 : return convert_any_priv_string(priv_text, parameter_priv_map);
4718 : : }
4719 : :
4720 : : /*
4721 : : * has_largeobject_privilege variants
4722 : : * These are all named "has_largeobject_privilege" at the SQL level.
4723 : : * They take various combinations of large object OID with
4724 : : * user name, user OID, or implicit user = current_user.
4725 : : *
4726 : : * The result is a boolean value: true if user has the indicated
4727 : : * privilege, false if not, or NULL if object doesn't exist.
4728 : : */
4729 : :
4730 : : /*
4731 : : * has_lo_priv_byid
4732 : : *
4733 : : * Helper function to check user privileges on a large object given the
4734 : : * role by Oid, large object by Oid, and privileges as AclMode.
4735 : : */
4736 : : static bool
4737 : 140 : has_lo_priv_byid(Oid roleid, Oid lobjId, AclMode priv, bool *is_missing)
4738 : : {
4739 : 140 : Snapshot snapshot = NULL;
4740 : : AclResult aclresult;
4741 : :
4742 [ + + ]: 140 : if (priv & ACL_UPDATE)
4743 : 64 : snapshot = NULL;
4744 : : else
4745 : 76 : snapshot = GetActiveSnapshot();
4746 : :
4747 [ + + ]: 140 : if (!LargeObjectExistsWithSnapshot(lobjId, snapshot))
4748 : : {
4749 : : Assert(is_missing != NULL);
4750 : 4 : *is_missing = true;
4751 : 4 : return false;
4752 : : }
4753 : :
4754 [ + + ]: 136 : if (lo_compat_privileges)
4755 : 8 : return true;
4756 : :
4757 : 128 : aclresult = pg_largeobject_aclcheck_snapshot(lobjId,
4758 : : roleid,
4759 : : priv,
4760 : : snapshot);
4761 : 128 : return aclresult == ACLCHECK_OK;
4762 : : }
4763 : :
4764 : : /*
4765 : : * has_largeobject_privilege_name_id
4766 : : * Check user privileges on a large object given
4767 : : * name username, large object oid, and text priv name.
4768 : : */
4769 : : Datum
4770 : 56 : has_largeobject_privilege_name_id(PG_FUNCTION_ARGS)
4771 : : {
4772 : 56 : Name username = PG_GETARG_NAME(0);
4773 : 56 : Oid roleid = get_role_oid_or_public(NameStr(*username));
4774 : 56 : Oid lobjId = PG_GETARG_OID(1);
4775 : 56 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4776 : : AclMode mode;
4777 : 56 : bool is_missing = false;
4778 : : bool result;
4779 : :
4780 : 56 : mode = convert_largeobject_priv_string(priv_type_text);
4781 : 56 : result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
4782 : :
4783 [ - + ]: 56 : if (is_missing)
4784 : 0 : PG_RETURN_NULL();
4785 : :
4786 : 56 : PG_RETURN_BOOL(result);
4787 : : }
4788 : :
4789 : : /*
4790 : : * has_largeobject_privilege_id
4791 : : * Check user privileges on a large object given
4792 : : * large object oid, and text priv name.
4793 : : * current_user is assumed
4794 : : */
4795 : : Datum
4796 : 84 : has_largeobject_privilege_id(PG_FUNCTION_ARGS)
4797 : : {
4798 : 84 : Oid lobjId = PG_GETARG_OID(0);
4799 : 84 : Oid roleid = GetUserId();
4800 : 84 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4801 : : AclMode mode;
4802 : 84 : bool is_missing = false;
4803 : : bool result;
4804 : :
4805 : 84 : mode = convert_largeobject_priv_string(priv_type_text);
4806 : 84 : result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
4807 : :
4808 [ + + ]: 84 : if (is_missing)
4809 : 4 : PG_RETURN_NULL();
4810 : :
4811 : 80 : PG_RETURN_BOOL(result);
4812 : : }
4813 : :
4814 : : /*
4815 : : * has_largeobject_privilege_id_id
4816 : : * Check user privileges on a large object given
4817 : : * roleid, large object oid, and text priv name.
4818 : : */
4819 : : Datum
4820 : 0 : has_largeobject_privilege_id_id(PG_FUNCTION_ARGS)
4821 : : {
4822 : 0 : Oid roleid = PG_GETARG_OID(0);
4823 : 0 : Oid lobjId = PG_GETARG_OID(1);
4824 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4825 : : AclMode mode;
4826 : 0 : bool is_missing = false;
4827 : : bool result;
4828 : :
4829 : 0 : mode = convert_largeobject_priv_string(priv_type_text);
4830 : 0 : result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
4831 : :
4832 [ # # ]: 0 : if (is_missing)
4833 : 0 : PG_RETURN_NULL();
4834 : :
4835 : 0 : PG_RETURN_BOOL(result);
4836 : : }
4837 : :
4838 : : /*
4839 : : * convert_largeobject_priv_string
4840 : : * Convert text string to AclMode value.
4841 : : */
4842 : : static AclMode
4843 : 140 : convert_largeobject_priv_string(text *priv_type_text)
4844 : : {
4845 : : static const priv_map largeobject_priv_map[] = {
4846 : : {"SELECT", ACL_SELECT},
4847 : : {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
4848 : : {"UPDATE", ACL_UPDATE},
4849 : : {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
4850 : : {NULL, 0}
4851 : : };
4852 : :
4853 : 140 : return convert_any_priv_string(priv_type_text, largeobject_priv_map);
4854 : : }
4855 : :
4856 : : /*
4857 : : * pg_has_role variants
4858 : : * These are all named "pg_has_role" at the SQL level.
4859 : : * They take various combinations of role name, role OID,
4860 : : * user name, user OID, or implicit user = current_user.
4861 : : *
4862 : : * The result is a boolean value: true if user has the indicated
4863 : : * privilege, false if not.
4864 : : */
4865 : :
4866 : : /*
4867 : : * pg_has_role_name_name
4868 : : * Check user privileges on a role given
4869 : : * name username, name rolename, and text priv name.
4870 : : */
4871 : : Datum
4872 : 24 : pg_has_role_name_name(PG_FUNCTION_ARGS)
4873 : : {
4874 : 24 : Name username = PG_GETARG_NAME(0);
4875 : 24 : Name rolename = PG_GETARG_NAME(1);
4876 : 24 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4877 : : Oid roleid;
4878 : : Oid roleoid;
4879 : : AclMode mode;
4880 : : AclResult aclresult;
4881 : :
4882 : 24 : roleid = get_role_oid(NameStr(*username), false);
4883 : 24 : roleoid = get_role_oid(NameStr(*rolename), false);
4884 : 24 : mode = convert_role_priv_string(priv_type_text);
4885 : :
4886 : 24 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4887 : :
4888 : 24 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4889 : : }
4890 : :
4891 : : /*
4892 : : * pg_has_role_name
4893 : : * Check user privileges on a role given
4894 : : * name rolename and text priv name.
4895 : : * current_user is assumed
4896 : : */
4897 : : Datum
4898 : 12 : pg_has_role_name(PG_FUNCTION_ARGS)
4899 : : {
4900 : 12 : Name rolename = PG_GETARG_NAME(0);
4901 : 12 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4902 : : Oid roleid;
4903 : : Oid roleoid;
4904 : : AclMode mode;
4905 : : AclResult aclresult;
4906 : :
4907 : 12 : roleid = GetUserId();
4908 : 12 : roleoid = get_role_oid(NameStr(*rolename), false);
4909 : 12 : mode = convert_role_priv_string(priv_type_text);
4910 : :
4911 : 12 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4912 : :
4913 : 12 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4914 : : }
4915 : :
4916 : : /*
4917 : : * pg_has_role_name_id
4918 : : * Check user privileges on a role given
4919 : : * name usename, role oid, and text priv name.
4920 : : */
4921 : : Datum
4922 : 0 : pg_has_role_name_id(PG_FUNCTION_ARGS)
4923 : : {
4924 : 0 : Name username = PG_GETARG_NAME(0);
4925 : 0 : Oid roleoid = PG_GETARG_OID(1);
4926 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4927 : : Oid roleid;
4928 : : AclMode mode;
4929 : : AclResult aclresult;
4930 : :
4931 : 0 : roleid = get_role_oid(NameStr(*username), false);
4932 : 0 : mode = convert_role_priv_string(priv_type_text);
4933 : :
4934 : 0 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4935 : :
4936 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4937 : : }
4938 : :
4939 : : /*
4940 : : * pg_has_role_id
4941 : : * Check user privileges on a role given
4942 : : * role oid, and text priv name.
4943 : : * current_user is assumed
4944 : : */
4945 : : Datum
4946 : 63971 : pg_has_role_id(PG_FUNCTION_ARGS)
4947 : : {
4948 : 63971 : Oid roleoid = PG_GETARG_OID(0);
4949 : 63971 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4950 : : Oid roleid;
4951 : : AclMode mode;
4952 : : AclResult aclresult;
4953 : :
4954 : 63971 : roleid = GetUserId();
4955 : 63971 : mode = convert_role_priv_string(priv_type_text);
4956 : :
4957 : 63971 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4958 : :
4959 : 63971 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4960 : : }
4961 : :
4962 : : /*
4963 : : * pg_has_role_id_name
4964 : : * Check user privileges on a role given
4965 : : * roleid, name rolename, and text priv name.
4966 : : */
4967 : : Datum
4968 : 0 : pg_has_role_id_name(PG_FUNCTION_ARGS)
4969 : : {
4970 : 0 : Oid roleid = PG_GETARG_OID(0);
4971 : 0 : Name rolename = PG_GETARG_NAME(1);
4972 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4973 : : Oid roleoid;
4974 : : AclMode mode;
4975 : : AclResult aclresult;
4976 : :
4977 : 0 : roleoid = get_role_oid(NameStr(*rolename), false);
4978 : 0 : mode = convert_role_priv_string(priv_type_text);
4979 : :
4980 : 0 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4981 : :
4982 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4983 : : }
4984 : :
4985 : : /*
4986 : : * pg_has_role_id_id
4987 : : * Check user privileges on a role given
4988 : : * roleid, role oid, and text priv name.
4989 : : */
4990 : : Datum
4991 : 112 : pg_has_role_id_id(PG_FUNCTION_ARGS)
4992 : : {
4993 : 112 : Oid roleid = PG_GETARG_OID(0);
4994 : 112 : Oid roleoid = PG_GETARG_OID(1);
4995 : 112 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4996 : : AclMode mode;
4997 : : AclResult aclresult;
4998 : :
4999 : 112 : mode = convert_role_priv_string(priv_type_text);
5000 : :
5001 : 112 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
5002 : :
5003 : 112 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
5004 : : }
5005 : :
5006 : : /*
5007 : : * Support routines for pg_has_role family.
5008 : : */
5009 : :
5010 : : /*
5011 : : * convert_role_priv_string
5012 : : * Convert text string to AclMode value.
5013 : : *
5014 : : * We use USAGE to denote whether the privileges of the role are accessible
5015 : : * (has_privs_of_role), MEMBER to denote is_member, and MEMBER WITH GRANT
5016 : : * (or ADMIN) OPTION to denote is_admin. There is no ACL bit corresponding
5017 : : * to MEMBER so we cheat and use ACL_CREATE for that. This convention
5018 : : * is shared only with pg_role_aclcheck, below.
5019 : : */
5020 : : static AclMode
5021 : 64119 : convert_role_priv_string(text *priv_type_text)
5022 : : {
5023 : : static const priv_map role_priv_map[] = {
5024 : : {"USAGE", ACL_USAGE},
5025 : : {"MEMBER", ACL_CREATE},
5026 : : {"SET", ACL_SET},
5027 : : {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5028 : : {"USAGE WITH ADMIN OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5029 : : {"MEMBER WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5030 : : {"MEMBER WITH ADMIN OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5031 : : {"SET WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5032 : : {"SET WITH ADMIN OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5033 : : {NULL, 0}
5034 : : };
5035 : :
5036 : 64119 : return convert_any_priv_string(priv_type_text, role_priv_map);
5037 : : }
5038 : :
5039 : : /*
5040 : : * pg_role_aclcheck
5041 : : * Quick-and-dirty support for pg_has_role
5042 : : */
5043 : : static AclResult
5044 : 64119 : pg_role_aclcheck(Oid role_oid, Oid roleid, AclMode mode)
5045 : : {
5046 [ + + ]: 64119 : if (mode & ACL_GRANT_OPTION_FOR(ACL_CREATE))
5047 : : {
5048 [ - + ]: 8 : if (is_admin_of_role(roleid, role_oid))
5049 : 0 : return ACLCHECK_OK;
5050 : : }
5051 [ + + ]: 64119 : if (mode & ACL_CREATE)
5052 : : {
5053 [ + + ]: 8 : if (is_member_of_role(roleid, role_oid))
5054 : 4 : return ACLCHECK_OK;
5055 : : }
5056 [ + + ]: 64115 : if (mode & ACL_USAGE)
5057 : : {
5058 [ + + ]: 64103 : if (has_privs_of_role(roleid, role_oid))
5059 : 63403 : return ACLCHECK_OK;
5060 : : }
5061 [ - + ]: 712 : if (mode & ACL_SET)
5062 : : {
5063 [ # # ]: 0 : if (member_can_set_role(roleid, role_oid))
5064 : 0 : return ACLCHECK_OK;
5065 : : }
5066 : 712 : return ACLCHECK_NO_PRIV;
5067 : : }
5068 : :
5069 : :
5070 : : /*
5071 : : * initialization function (called by InitPostgres)
5072 : : */
5073 : : void
5074 : 19607 : initialize_acl(void)
5075 : : {
5076 [ + + ]: 19607 : if (!IsBootstrapProcessingMode())
5077 : : {
5078 : 19548 : cached_db_hash =
5079 : 19548 : GetSysCacheHashValue1(DATABASEOID,
5080 : : ObjectIdGetDatum(MyDatabaseId));
5081 : :
5082 : : /*
5083 : : * In normal mode, set a callback on any syscache invalidation of rows
5084 : : * of pg_auth_members (for roles_is_member_of()) pg_database (for
5085 : : * roles_is_member_of())
5086 : : */
5087 : 19548 : CacheRegisterSyscacheCallback(AUTHMEMROLEMEM,
5088 : : RoleMembershipCacheCallback,
5089 : : (Datum) 0);
5090 : 19548 : CacheRegisterSyscacheCallback(AUTHOID,
5091 : : RoleMembershipCacheCallback,
5092 : : (Datum) 0);
5093 : 19548 : CacheRegisterSyscacheCallback(DATABASEOID,
5094 : : RoleMembershipCacheCallback,
5095 : : (Datum) 0);
5096 : : }
5097 : 19607 : }
5098 : :
5099 : : /*
5100 : : * RoleMembershipCacheCallback
5101 : : * Syscache inval callback function
5102 : : */
5103 : : static void
5104 : 36207 : RoleMembershipCacheCallback(Datum arg, SysCacheIdentifier cacheid,
5105 : : uint32 hashvalue)
5106 : : {
5107 [ + + ]: 36207 : if (cacheid == DATABASEOID &&
5108 [ + + + + ]: 5582 : hashvalue != cached_db_hash &&
5109 : : hashvalue != 0)
5110 : : {
5111 : 1608 : return; /* ignore pg_database changes for other DBs */
5112 : : }
5113 : :
5114 : : /* Force membership caches to be recomputed on next use */
5115 : 34599 : cached_role[ROLERECURSE_MEMBERS] = InvalidOid;
5116 : 34599 : cached_role[ROLERECURSE_PRIVS] = InvalidOid;
5117 : 34599 : cached_role[ROLERECURSE_SETROLE] = InvalidOid;
5118 : : }
5119 : :
5120 : : /*
5121 : : * A helper function for roles_is_member_of() that provides an optimized
5122 : : * implementation of list_append_unique_oid() via a Bloom filter. The caller
5123 : : * (i.e., roles_is_member_of()) is responsible for freeing bf once it is done
5124 : : * using this function.
5125 : : */
5126 : : static inline List *
5127 : 2726 : roles_list_append(List *roles_list, bloom_filter **bf, Oid role)
5128 : : {
5129 : 2726 : unsigned char *roleptr = (unsigned char *) &role;
5130 : :
5131 : : /*
5132 : : * If there is a previously-created Bloom filter, use it to try to
5133 : : * determine whether the role is missing from the list. If it says yes,
5134 : : * that's a hard fact and we can go ahead and add the role. If it says
5135 : : * no, that's only probabilistic and we'd better search the list. Without
5136 : : * a filter, we must always do an ordinary linear search through the
5137 : : * existing list.
5138 : : */
5139 [ - + - - ]: 2726 : if ((*bf && bloom_lacks_element(*bf, roleptr, sizeof(Oid))) ||
5140 [ + + ]: 2726 : !list_member_oid(roles_list, role))
5141 : : {
5142 : : /*
5143 : : * If the list is large, we take on the overhead of creating and
5144 : : * populating a Bloom filter to speed up future calls to this
5145 : : * function.
5146 : : */
5147 [ + - - + ]: 4436 : if (*bf == NULL &&
5148 : 2218 : list_length(roles_list) > ROLES_LIST_BLOOM_THRESHOLD)
5149 : : {
5150 : 0 : *bf = bloom_create(ROLES_LIST_BLOOM_THRESHOLD * 10, work_mem, 0);
5151 [ # # # # : 0 : foreach_oid(roleid, roles_list)
# # ]
5152 : 0 : bloom_add_element(*bf, (unsigned char *) &roleid, sizeof(Oid));
5153 : : }
5154 : :
5155 : : /*
5156 : : * Finally, add the role to the list and the Bloom filter, if it
5157 : : * exists.
5158 : : */
5159 : 2218 : roles_list = lappend_oid(roles_list, role);
5160 [ - + ]: 2218 : if (*bf)
5161 : 0 : bloom_add_element(*bf, roleptr, sizeof(Oid));
5162 : : }
5163 : :
5164 : 2726 : return roles_list;
5165 : : }
5166 : :
5167 : : /*
5168 : : * Get a list of roles that the specified roleid is a member of
5169 : : *
5170 : : * Type ROLERECURSE_MEMBERS recurses through all grants; ROLERECURSE_PRIVS
5171 : : * recurses only through inheritable grants; and ROLERECURSE_SETROLE recurses
5172 : : * only through grants with set_option.
5173 : : *
5174 : : * Since indirect membership testing is relatively expensive, we cache
5175 : : * a list of memberships. Hence, the result is only guaranteed good until
5176 : : * the next call of roles_is_member_of()!
5177 : : *
5178 : : * For the benefit of select_best_grantor, the result is defined to be
5179 : : * in breadth-first order, ie, closer relationships earlier.
5180 : : *
5181 : : * If admin_of is not InvalidOid, this function sets *admin_role, either
5182 : : * to the OID of the first role in the result list that directly possesses
5183 : : * ADMIN OPTION on the role corresponding to admin_of, or to InvalidOid if
5184 : : * there is no such role.
5185 : : */
5186 : : static List *
5187 : 35260 : roles_is_member_of(Oid roleid, enum RoleRecurseType type,
5188 : : Oid admin_of, Oid *admin_role)
5189 : : {
5190 : : Oid dba;
5191 : : List *roles_list;
5192 : : ListCell *l;
5193 : : List *new_cached_roles;
5194 : : MemoryContext oldctx;
5195 : 35260 : bloom_filter *bf = NULL;
5196 : :
5197 : : Assert(OidIsValid(admin_of) == (admin_role != NULL));
5198 [ + + ]: 35260 : if (admin_role != NULL)
5199 : 612 : *admin_role = InvalidOid;
5200 : :
5201 : : /* If cache is valid and ADMIN OPTION not sought, just return the list */
5202 [ + + + + ]: 35260 : if (cached_role[type] == roleid && !OidIsValid(admin_of) &&
5203 [ + - ]: 32377 : OidIsValid(cached_role[type]))
5204 : 32377 : return cached_roles[type];
5205 : :
5206 : : /*
5207 : : * Role expansion happens in a non-database backend when guc.c checks
5208 : : * ROLE_PG_READ_ALL_SETTINGS for a physical walsender SHOW command. In
5209 : : * that case, no role gets pg_database_owner.
5210 : : */
5211 [ + + ]: 2883 : if (!OidIsValid(MyDatabaseId))
5212 : 18 : dba = InvalidOid;
5213 : : else
5214 : : {
5215 : : HeapTuple dbtup;
5216 : :
5217 : 2865 : dbtup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
5218 [ - + ]: 2865 : if (!HeapTupleIsValid(dbtup))
5219 [ # # ]: 0 : elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
5220 : 2865 : dba = ((Form_pg_database) GETSTRUCT(dbtup))->datdba;
5221 : 2865 : ReleaseSysCache(dbtup);
5222 : : }
5223 : :
5224 : : /*
5225 : : * Find all the roles that roleid is a member of, including multi-level
5226 : : * recursion. The role itself will always be the first element of the
5227 : : * resulting list.
5228 : : *
5229 : : * Each element of the list is scanned to see if it adds any indirect
5230 : : * memberships. We can use a single list as both the record of
5231 : : * already-found memberships and the agenda of roles yet to be scanned.
5232 : : * This is a bit tricky but works because the foreach() macro doesn't
5233 : : * fetch the next list element until the bottom of the loop.
5234 : : */
5235 : 2883 : roles_list = list_make1_oid(roleid);
5236 : :
5237 [ + - + + : 7984 : foreach(l, roles_list)
+ + ]
5238 : : {
5239 : 5101 : Oid memberid = lfirst_oid(l);
5240 : : CatCList *memlist;
5241 : : int i;
5242 : :
5243 : : /* Find roles that memberid is directly a member of */
5244 : 5101 : memlist = SearchSysCacheList1(AUTHMEMMEMROLE,
5245 : : ObjectIdGetDatum(memberid));
5246 [ + + ]: 9601 : for (i = 0; i < memlist->n_members; i++)
5247 : : {
5248 : 4500 : HeapTuple tup = &memlist->members[i]->tuple;
5249 : 4500 : Form_pg_auth_members form = (Form_pg_auth_members) GETSTRUCT(tup);
5250 : 4500 : Oid otherid = form->roleid;
5251 : :
5252 : : /*
5253 : : * While otherid==InvalidOid shouldn't appear in the catalog, the
5254 : : * OidIsValid() avoids crashing if that arises.
5255 : : */
5256 [ + + + + : 4500 : if (otherid == admin_of && form->admin_option &&
+ - ]
5257 [ + + ]: 502 : OidIsValid(admin_of) && !OidIsValid(*admin_role))
5258 : 486 : *admin_role = memberid;
5259 : :
5260 : : /* If we're supposed to ignore non-heritable grants, do so. */
5261 [ + + + + ]: 4500 : if (type == ROLERECURSE_PRIVS && !form->inherit_option)
5262 : 1702 : continue;
5263 : :
5264 : : /* If we're supposed to ignore non-SET grants, do so. */
5265 [ + + + + ]: 2798 : if (type == ROLERECURSE_SETROLE && !form->set_option)
5266 : 84 : continue;
5267 : :
5268 : : /*
5269 : : * Even though there shouldn't be any loops in the membership
5270 : : * graph, we must test for having already seen this role. It is
5271 : : * legal for instance to have both A->B and A->C->B.
5272 : : */
5273 : 2714 : roles_list = roles_list_append(roles_list, &bf, otherid);
5274 : : }
5275 : 5101 : ReleaseSysCacheList(memlist);
5276 : :
5277 : : /* implement pg_database_owner implicit membership */
5278 [ + + + - ]: 5101 : if (memberid == dba && OidIsValid(dba))
5279 : 12 : roles_list = roles_list_append(roles_list, &bf,
5280 : : ROLE_PG_DATABASE_OWNER);
5281 : : }
5282 : :
5283 : : /*
5284 : : * Free the Bloom filter created by roles_list_append(), if there is one.
5285 : : */
5286 [ - + ]: 2883 : if (bf)
5287 : 0 : bloom_free(bf);
5288 : :
5289 : : /*
5290 : : * Copy the completed list into TopMemoryContext so it will persist.
5291 : : */
5292 : 2883 : oldctx = MemoryContextSwitchTo(TopMemoryContext);
5293 : 2883 : new_cached_roles = list_copy(roles_list);
5294 : 2883 : MemoryContextSwitchTo(oldctx);
5295 : 2883 : list_free(roles_list);
5296 : :
5297 : : /*
5298 : : * Now safe to assign to state variable
5299 : : */
5300 : 2883 : cached_role[type] = InvalidOid; /* just paranoia */
5301 : 2883 : list_free(cached_roles[type]);
5302 : 2883 : cached_roles[type] = new_cached_roles;
5303 : 2883 : cached_role[type] = roleid;
5304 : :
5305 : : /* And now we can return the answer */
5306 : 2883 : return cached_roles[type];
5307 : : }
5308 : :
5309 : :
5310 : : /*
5311 : : * Does member have the privileges of role (directly or indirectly)?
5312 : : *
5313 : : * This is defined not to recurse through grants that are not inherited,
5314 : : * and only inherited grants confer the associated privileges automatically.
5315 : : *
5316 : : * See also member_can_set_role, below.
5317 : : */
5318 : : bool
5319 : 229008 : has_privs_of_role(Oid member, Oid role)
5320 : : {
5321 : : /* Fast path for simple case */
5322 [ + + ]: 229008 : if (member == role)
5323 : 66586 : return true;
5324 : :
5325 : : /* Superusers have every privilege, so are part of every role */
5326 [ + + ]: 162422 : if (superuser_arg(member))
5327 : 128712 : return true;
5328 : :
5329 : : /*
5330 : : * Find all the roles that member has the privileges of, including
5331 : : * multi-level recursion, then see if target role is any one of them.
5332 : : */
5333 : 33710 : return list_member_oid(roles_is_member_of(member, ROLERECURSE_PRIVS,
5334 : : InvalidOid, NULL),
5335 : : role);
5336 : : }
5337 : :
5338 : : /*
5339 : : * Can member use SET ROLE to this role?
5340 : : *
5341 : : * There must be a chain of grants from 'member' to 'role' each of which
5342 : : * permits SET ROLE; that is, each of which has set_option = true.
5343 : : *
5344 : : * It doesn't matter whether the grants are inheritable. That's a separate
5345 : : * question; see has_privs_of_role.
5346 : : *
5347 : : * This function should be used to determine whether the session user can
5348 : : * use SET ROLE to become the target user. We also use it to determine whether
5349 : : * the session user can change an existing object to be owned by the target
5350 : : * user, or create new objects owned by the target user.
5351 : : */
5352 : : bool
5353 : 340166 : member_can_set_role(Oid member, Oid role)
5354 : : {
5355 : : /* Fast path for simple case */
5356 [ + + ]: 340166 : if (member == role)
5357 : 339105 : return true;
5358 : :
5359 : : /* Superusers have every privilege, so can always SET ROLE */
5360 [ + + ]: 1061 : if (superuser_arg(member))
5361 : 797 : return true;
5362 : :
5363 : : /*
5364 : : * Find all the roles that member can access via SET ROLE, including
5365 : : * multi-level recursion, then see if target role is any one of them.
5366 : : */
5367 : 264 : return list_member_oid(roles_is_member_of(member, ROLERECURSE_SETROLE,
5368 : : InvalidOid, NULL),
5369 : : role);
5370 : : }
5371 : :
5372 : : /*
5373 : : * Permission violation error unless able to SET ROLE to target role.
5374 : : */
5375 : : void
5376 : 1419 : check_can_set_role(Oid member, Oid role)
5377 : : {
5378 [ + + ]: 1419 : if (!member_can_set_role(member, role))
5379 [ + - ]: 96 : ereport(ERROR,
5380 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
5381 : : errmsg("must be able to SET ROLE \"%s\"",
5382 : : GetUserNameFromId(role, false))));
5383 : 1323 : }
5384 : :
5385 : : /*
5386 : : * Is member a member of role (directly or indirectly)?
5387 : : *
5388 : : * This is defined to recurse through grants whether they are inherited or not.
5389 : : *
5390 : : * Do not use this for privilege checking, instead use has_privs_of_role().
5391 : : * Don't use it for determining whether it's possible to SET ROLE to some
5392 : : * other role; for that, use member_can_set_role(). And don't use it for
5393 : : * determining whether it's OK to create an object owned by some other role:
5394 : : * use member_can_set_role() for that, too.
5395 : : *
5396 : : * In short, calling this function is the wrong thing to do nearly everywhere.
5397 : : */
5398 : : bool
5399 : 8 : is_member_of_role(Oid member, Oid role)
5400 : : {
5401 : : /* Fast path for simple case */
5402 [ - + ]: 8 : if (member == role)
5403 : 0 : return true;
5404 : :
5405 : : /* Superusers have every privilege, so are part of every role */
5406 [ - + ]: 8 : if (superuser_arg(member))
5407 : 0 : return true;
5408 : :
5409 : : /*
5410 : : * Find all the roles that member is a member of, including multi-level
5411 : : * recursion, then see if target role is any one of them.
5412 : : */
5413 : 8 : return list_member_oid(roles_is_member_of(member, ROLERECURSE_MEMBERS,
5414 : : InvalidOid, NULL),
5415 : : role);
5416 : : }
5417 : :
5418 : : /*
5419 : : * Is member a member of role, not considering superuserness?
5420 : : *
5421 : : * This is identical to is_member_of_role except we ignore superuser
5422 : : * status.
5423 : : *
5424 : : * Do not use this for privilege checking, instead use has_privs_of_role()
5425 : : */
5426 : : bool
5427 : 487 : is_member_of_role_nosuper(Oid member, Oid role)
5428 : : {
5429 : : /* Fast path for simple case */
5430 [ + + ]: 487 : if (member == role)
5431 : 13 : return true;
5432 : :
5433 : : /*
5434 : : * Find all the roles that member is a member of, including multi-level
5435 : : * recursion, then see if target role is any one of them.
5436 : : */
5437 : 474 : return list_member_oid(roles_is_member_of(member, ROLERECURSE_MEMBERS,
5438 : : InvalidOid, NULL),
5439 : : role);
5440 : : }
5441 : :
5442 : :
5443 : : /*
5444 : : * Is member an admin of role? That is, is member the role itself (subject to
5445 : : * restrictions below), a member (directly or indirectly) WITH ADMIN OPTION,
5446 : : * or a superuser?
5447 : : *
5448 : : * See also has_admin_privs_of_role() below.
5449 : : */
5450 : : bool
5451 : 1397 : is_admin_of_role(Oid member, Oid role)
5452 : : {
5453 : : Oid admin_role;
5454 : :
5455 [ + + ]: 1397 : if (superuser_arg(member))
5456 : 1245 : return true;
5457 : :
5458 : : /* By policy, a role cannot have WITH ADMIN OPTION on itself. */
5459 [ - + ]: 152 : if (member == role)
5460 : 0 : return false;
5461 : :
5462 : 152 : (void) roles_is_member_of(member, ROLERECURSE_MEMBERS, role, &admin_role);
5463 : 152 : return OidIsValid(admin_role);
5464 : : }
5465 : :
5466 : : /*
5467 : : * Does member hold ADMIN OPTION on role, either directly or through a role
5468 : : * whose privileges member inherits?
5469 : : *
5470 : : * Unlike is_admin_of_role(), this does not recurse through grants that are not
5471 : : * inherited. Callers that must go on to record a grantor for the operation
5472 : : * should use this rather than is_admin_of_role(), since select_best_admin()
5473 : : * searches the same way.
5474 : : */
5475 : : bool
5476 : 514 : has_admin_privs_of_role(Oid member, Oid role)
5477 : : {
5478 : : Oid admin_role;
5479 : :
5480 [ + + ]: 514 : if (superuser_arg(member))
5481 : 325 : return true;
5482 : :
5483 : : /* By policy, a role cannot have WITH ADMIN OPTION on itself. */
5484 [ + + ]: 189 : if (member == role)
5485 : 12 : return false;
5486 : :
5487 : 177 : (void) roles_is_member_of(member, ROLERECURSE_PRIVS, role, &admin_role);
5488 : 177 : return OidIsValid(admin_role);
5489 : : }
5490 : :
5491 : : /*
5492 : : * Find a role whose privileges "member" inherits which has ADMIN OPTION
5493 : : * on "role", ignoring super-userness.
5494 : : *
5495 : : * There might be more than one such role; prefer one which involves fewer
5496 : : * hops. That is, if member has ADMIN OPTION, prefer that over all other
5497 : : * options; if not, prefer a role from which member inherits more directly
5498 : : * over more indirect inheritance.
5499 : : */
5500 : : Oid
5501 : 287 : select_best_admin(Oid member, Oid role)
5502 : : {
5503 : : Oid admin_role;
5504 : :
5505 : : /* By policy, a role cannot have WITH ADMIN OPTION on itself. */
5506 [ + + ]: 287 : if (member == role)
5507 : 4 : return InvalidOid;
5508 : :
5509 : 283 : (void) roles_is_member_of(member, ROLERECURSE_PRIVS, role, &admin_role);
5510 : 283 : return admin_role;
5511 : : }
5512 : :
5513 : : /*
5514 : : * Select the effective grantor ID for a GRANT or REVOKE operation.
5515 : : *
5516 : : * If the GRANT/REVOKE has an explicit GRANTED BY clause, we always use
5517 : : * exactly that role (which may result in granting/revoking no privileges).
5518 : : * Otherwise, we seek a "best" grantor, starting with the current user.
5519 : : *
5520 : : * The grantor must always be either the object owner or some role that has
5521 : : * been explicitly granted grant options. This ensures that all granted
5522 : : * privileges appear to flow from the object owner, and there are never
5523 : : * multiple "original sources" of a privilege. Therefore, if the would-be
5524 : : * grantor is a member of a role that has the needed grant options, we have
5525 : : * to do the grant as that role instead.
5526 : : *
5527 : : * It is possible that the would-be grantor is a member of several roles
5528 : : * that have different subsets of the desired grant options, but no one
5529 : : * role has 'em all. In this case we pick a role with the largest number
5530 : : * of desired options. Ties are broken in favor of closer ancestors.
5531 : : *
5532 : : * grantedBy: the GRANTED BY clause of GRANT/REVOKE, or NULL if none
5533 : : * privileges: the privileges to be granted/revoked
5534 : : * acl: the ACL of the object in question
5535 : : * ownerId: the role owning the object in question
5536 : : * *grantorId: receives the OID of the role to do the grant as
5537 : : * *grantOptions: receives grant options actually held by grantorId (maybe 0)
5538 : : */
5539 : : void
5540 : 41105 : select_best_grantor(const RoleSpec *grantedBy, AclMode privileges,
5541 : : const Acl *acl, Oid ownerId,
5542 : : Oid *grantorId, AclMode *grantOptions)
5543 : : {
5544 : 41105 : Oid roleId = GetUserId();
5545 : 41105 : AclMode needed_goptions = ACL_GRANT_OPTION_FOR(privileges);
5546 : : List *roles_list;
5547 : : int nrights;
5548 : : ListCell *l;
5549 : :
5550 : : /*
5551 : : * If we have GRANTED BY, resolve it and verify current user is allowed to
5552 : : * specify that role.
5553 : : */
5554 [ + + ]: 41105 : if (grantedBy)
5555 : : {
5556 : 76 : Oid grantor = get_rolespec_oid(grantedBy, false);
5557 : :
5558 [ + + ]: 76 : if (!has_privs_of_role(roleId, grantor))
5559 [ + - ]: 4 : ereport(ERROR,
5560 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
5561 : : errmsg("must inherit privileges of role \"%s\"",
5562 : : GetUserNameFromId(grantor, false))));
5563 : : /* Use exactly that grantor, whether it has privileges or not */
5564 : 72 : *grantorId = grantor;
5565 : 72 : *grantOptions = aclmask_direct(acl, grantor, ownerId,
5566 : : needed_goptions, ACLMASK_ALL);
5567 : 72 : return;
5568 : : }
5569 : :
5570 : : /*
5571 : : * The object owner is always treated as having all grant options, so if
5572 : : * roleId is the owner it's easy. Also, if roleId is a superuser it's
5573 : : * easy: superusers are implicitly members of every role, so they act as
5574 : : * the object owner.
5575 : : */
5576 [ + + + + ]: 41029 : if (roleId == ownerId || superuser_arg(roleId))
5577 : : {
5578 : 40837 : *grantorId = ownerId;
5579 : 40837 : *grantOptions = needed_goptions;
5580 : 40837 : return;
5581 : : }
5582 : :
5583 : : /*
5584 : : * Otherwise we have to do a careful search to see if roleId has the
5585 : : * privileges of any suitable role. Note: we can hang onto the result of
5586 : : * roles_is_member_of() throughout this loop, because aclmask_direct()
5587 : : * doesn't query any role memberships.
5588 : : */
5589 : 192 : roles_list = roles_is_member_of(roleId, ROLERECURSE_PRIVS,
5590 : : InvalidOid, NULL);
5591 : :
5592 : : /* initialize candidate result as default */
5593 : 192 : *grantorId = roleId;
5594 : 192 : *grantOptions = ACL_NO_RIGHTS;
5595 : 192 : nrights = 0;
5596 : :
5597 [ + - + + : 360 : foreach(l, roles_list)
+ + ]
5598 : : {
5599 : 308 : Oid otherrole = lfirst_oid(l);
5600 : : AclMode otherprivs;
5601 : :
5602 : 308 : otherprivs = aclmask_direct(acl, otherrole, ownerId,
5603 : : needed_goptions, ACLMASK_ALL);
5604 [ + + ]: 308 : if (otherprivs == needed_goptions)
5605 : : {
5606 : : /* Found a suitable grantor */
5607 : 140 : *grantorId = otherrole;
5608 : 140 : *grantOptions = otherprivs;
5609 : 140 : return;
5610 : : }
5611 : :
5612 : : /*
5613 : : * If it has just some of the needed privileges, remember best
5614 : : * candidate.
5615 : : */
5616 [ + + ]: 168 : if (otherprivs != ACL_NO_RIGHTS)
5617 : : {
5618 : 68 : int nnewrights = pg_popcount64(otherprivs);
5619 : :
5620 [ + + ]: 68 : if (nnewrights > nrights)
5621 : : {
5622 : 36 : *grantorId = otherrole;
5623 : 36 : *grantOptions = otherprivs;
5624 : 36 : nrights = nnewrights;
5625 : : }
5626 : : }
5627 : : }
5628 : : }
5629 : :
5630 : : /*
5631 : : * get_role_oid - Given a role name, look up the role's OID.
5632 : : *
5633 : : * If missing_ok is false, throw an error if role name not found. If
5634 : : * true, just return InvalidOid.
5635 : : */
5636 : : Oid
5637 : 24018 : get_role_oid(const char *rolname, bool missing_ok)
5638 : : {
5639 : : Oid oid;
5640 : :
5641 : 24018 : oid = GetSysCacheOid1(AUTHNAME, Anum_pg_authid_oid,
5642 : : CStringGetDatum(rolname));
5643 [ + + + + ]: 24018 : if (!OidIsValid(oid) && !missing_ok)
5644 [ + - ]: 40 : ereport(ERROR,
5645 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
5646 : : errmsg("role \"%s\" does not exist", rolname)));
5647 : 23978 : return oid;
5648 : : }
5649 : :
5650 : : /*
5651 : : * get_role_oid_or_public - As above, but return ACL_ID_PUBLIC if the
5652 : : * role name is "public".
5653 : : */
5654 : : Oid
5655 : 547 : get_role_oid_or_public(const char *rolname)
5656 : : {
5657 [ - + ]: 547 : if (strcmp(rolname, "public") == 0)
5658 : 0 : return ACL_ID_PUBLIC;
5659 : :
5660 : 547 : return get_role_oid(rolname, false);
5661 : : }
5662 : :
5663 : : /*
5664 : : * Given a RoleSpec node, return the OID it corresponds to. If missing_ok is
5665 : : * true, return InvalidOid if the role does not exist.
5666 : : *
5667 : : * PUBLIC is always disallowed here. Routines wanting to handle the PUBLIC
5668 : : * case must check the case separately.
5669 : : */
5670 : : Oid
5671 : 5867 : get_rolespec_oid(const RoleSpec *role, bool missing_ok)
5672 : : {
5673 : : Oid oid;
5674 : :
5675 [ + + + + : 5867 : switch (role->roletype)
- ]
5676 : : {
5677 : 5606 : case ROLESPEC_CSTRING:
5678 : : Assert(role->rolename);
5679 : 5606 : oid = get_role_oid(role->rolename, missing_ok);
5680 : 5575 : break;
5681 : :
5682 : 242 : case ROLESPEC_CURRENT_ROLE:
5683 : : case ROLESPEC_CURRENT_USER:
5684 : 242 : oid = GetUserId();
5685 : 242 : break;
5686 : :
5687 : 11 : case ROLESPEC_SESSION_USER:
5688 : 11 : oid = GetSessionUserId();
5689 : 11 : break;
5690 : :
5691 : 8 : case ROLESPEC_PUBLIC:
5692 [ + - ]: 8 : ereport(ERROR,
5693 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
5694 : : errmsg("role \"%s\" does not exist", "public")));
5695 : : oid = InvalidOid; /* make compiler happy */
5696 : : break;
5697 : :
5698 : 0 : default:
5699 [ # # ]: 0 : elog(ERROR, "unexpected role type %d", role->roletype);
5700 : : }
5701 : :
5702 : 5828 : return oid;
5703 : : }
5704 : :
5705 : : /*
5706 : : * Given a RoleSpec node, return the pg_authid HeapTuple it corresponds to.
5707 : : * Caller must ReleaseSysCache when done with the result tuple.
5708 : : */
5709 : : HeapTuple
5710 : 435 : get_rolespec_tuple(const RoleSpec *role)
5711 : : {
5712 : : HeapTuple tuple;
5713 : :
5714 [ + + + + : 435 : switch (role->roletype)
- ]
5715 : : {
5716 : 409 : case ROLESPEC_CSTRING:
5717 : : Assert(role->rolename);
5718 : 409 : tuple = SearchSysCache1(AUTHNAME, CStringGetDatum(role->rolename));
5719 [ + + ]: 409 : if (!HeapTupleIsValid(tuple))
5720 [ + - ]: 6 : ereport(ERROR,
5721 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
5722 : : errmsg("role \"%s\" does not exist", role->rolename)));
5723 : 403 : break;
5724 : :
5725 : 14 : case ROLESPEC_CURRENT_ROLE:
5726 : : case ROLESPEC_CURRENT_USER:
5727 : 14 : tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(GetUserId()));
5728 [ - + ]: 14 : if (!HeapTupleIsValid(tuple))
5729 [ # # ]: 0 : elog(ERROR, "cache lookup failed for role %u", GetUserId());
5730 : 14 : break;
5731 : :
5732 : 6 : case ROLESPEC_SESSION_USER:
5733 : 6 : tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(GetSessionUserId()));
5734 [ - + ]: 6 : if (!HeapTupleIsValid(tuple))
5735 [ # # ]: 0 : elog(ERROR, "cache lookup failed for role %u", GetSessionUserId());
5736 : 6 : break;
5737 : :
5738 : 6 : case ROLESPEC_PUBLIC:
5739 [ + - ]: 6 : ereport(ERROR,
5740 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
5741 : : errmsg("role \"%s\" does not exist", "public")));
5742 : : tuple = NULL; /* make compiler happy */
5743 : : break;
5744 : :
5745 : 0 : default:
5746 [ # # ]: 0 : elog(ERROR, "unexpected role type %d", role->roletype);
5747 : : }
5748 : :
5749 : 423 : return tuple;
5750 : : }
5751 : :
5752 : : /*
5753 : : * Given a RoleSpec, returns a palloc'ed copy of the corresponding role's name.
5754 : : */
5755 : : char *
5756 : 28 : get_rolespec_name(const RoleSpec *role)
5757 : : {
5758 : : HeapTuple tp;
5759 : : Form_pg_authid authForm;
5760 : : char *rolename;
5761 : :
5762 : 28 : tp = get_rolespec_tuple(role);
5763 : 28 : authForm = (Form_pg_authid) GETSTRUCT(tp);
5764 : 28 : rolename = pstrdup(NameStr(authForm->rolname));
5765 : 28 : ReleaseSysCache(tp);
5766 : :
5767 : 28 : return rolename;
5768 : : }
5769 : :
5770 : : /*
5771 : : * Given a RoleSpec, throw an error if the name is reserved, using detail_msg,
5772 : : * if provided (which must be already translated).
5773 : : *
5774 : : * If node is NULL, no error is thrown. If detail_msg is NULL then no detail
5775 : : * message is provided.
5776 : : */
5777 : : void
5778 : 342 : check_rolespec_name(const RoleSpec *role, const char *detail_msg)
5779 : : {
5780 [ - + ]: 342 : if (!role)
5781 : 0 : return;
5782 : :
5783 [ + + ]: 342 : if (role->roletype != ROLESPEC_CSTRING)
5784 : 26 : return;
5785 : :
5786 [ - + ]: 316 : if (IsReservedName(role->rolename))
5787 : : {
5788 [ # # ]: 0 : if (detail_msg)
5789 [ # # ]: 0 : ereport(ERROR,
5790 : : (errcode(ERRCODE_RESERVED_NAME),
5791 : : errmsg("role name \"%s\" is reserved",
5792 : : role->rolename),
5793 : : errdetail_internal("%s", detail_msg)));
5794 : : else
5795 [ # # ]: 0 : ereport(ERROR,
5796 : : (errcode(ERRCODE_RESERVED_NAME),
5797 : : errmsg("role name \"%s\" is reserved",
5798 : : role->rolename)));
5799 : : }
5800 : : }
|