PostgreSQL Source Code git master
Loading...
Searching...
No Matches
acl.c
Go to the documentation of this file.
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"
24#include "catalog/pg_authid.h"
25#include "catalog/pg_class.h"
26#include "catalog/pg_database.h"
29#include "catalog/pg_language.h"
32#include "catalog/pg_proc.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"
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
56typedef struct
57{
58 const char *name;
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 */
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};
83static List *cached_roles[] = {NIL, NIL, NIL};
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
94static const char *getid(const char *s, char *n, Node *escontext);
95static void putid(char *p, const char *s);
96static Acl *allocacl(int n);
97static void check_acl(const Acl *acl);
98static const char *aclparse(const char *s, AclItem *aip, Node *escontext);
99static bool aclitem_match(const AclItem *a1, const AclItem *a2);
100static int aclitemComparator(const void *arg1, const void *arg2);
101static void check_circularity(const Acl *old_acl, const AclItem *mod_aip,
102 Oid ownerId);
104 Oid ownerId, DropBehavior behavior);
105
107 const priv_map *privileges);
108
109static Oid convert_table_name(text *tablename);
112static AttrNumber convert_column_name(Oid tableoid, text *column);
122static Oid convert_schema_name(text *schemaname);
124static Oid convert_server_name(text *servername);
126static Oid convert_tablespace_name(text *tablespacename);
128static Oid convert_type_name(text *typename);
134
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 */
147static inline bool
148is_safe_acl_char(unsigned char c, bool is_getid)
149{
150 if (IS_HIGHBIT_SET(c))
151 return is_getid;
152 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 */
170static const char *
171getid(const char *s, char *n, Node *escontext)
172{
173 int len = 0;
174 bool in_quotes = false;
175
176 Assert(s && n);
177
178 while (isspace((unsigned char) *s))
179 s++;
180 for (;
181 *s != '\0' &&
182 (in_quotes || *s == '"' || is_safe_acl_char(*s, true));
183 s++)
184 {
185 if (*s == '"')
186 {
187 if (!in_quotes)
188 {
189 in_quotes = true;
190 continue;
191 }
192 /* safe to look at next char (could be '\0' though) */
193 if (*(s + 1) != '"')
194 {
195 in_quotes = false;
196 continue;
197 }
198 /* it's an escaped double quote; skip the escaping char */
199 s++;
200 }
201
202 /* Add the character to the string */
203 if (len >= NAMEDATALEN - 1)
204 ereturn(escontext, NULL,
206 errmsg("identifier too long"),
207 errdetail("Identifier must be less than %d characters.",
208 NAMEDATALEN)));
209
210 n[len++] = *s;
211 }
212 n[len] = '\0';
213 while (isspace((unsigned char) *s))
214 s++;
215 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 */
223static void
224putid(char *p, const char *s)
225{
226 const char *src;
227 bool safe = true;
228
229 /* Detect whether we need to use double quotes */
230 for (src = s; *src; src++)
231 {
232 if (!is_safe_acl_char(*src, false))
233 {
234 safe = false;
235 break;
236 }
237 }
238 if (!safe)
239 *p++ = '"';
240 for (src = s; *src; src++)
241 {
242 /* A double quote character in a username is encoded as "" */
243 if (*src == '"')
244 *p++ = '"';
245 *p++ = *src;
246 }
247 if (!safe)
248 *p++ = '"';
249 *p = '\0';
250}
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 */
278static const char *
279aclparse(const char *s, AclItem *aip, Node *escontext)
280{
282 goption,
283 read;
284 char name[NAMEDATALEN];
285 char name2[NAMEDATALEN];
286
287 Assert(s && aip);
288
289 s = getid(s, name, escontext);
290 if (s == NULL)
291 return NULL;
292 if (*s != '=')
293 {
294 /* we just read a keyword, not a name */
295 if (strcmp(name, "group") != 0 && strcmp(name, "user") != 0)
296 ereturn(escontext, NULL,
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 s = getid(s, name, escontext);
302 if (s == NULL)
303 return NULL;
304 if (name[0] == '\0')
305 ereturn(escontext, NULL,
307 errmsg("missing name"),
308 errhint("A name must follow the \"group\" or \"user\" key word.")));
309 }
310
311 if (*s != '=')
312 ereturn(escontext, NULL,
314 errmsg("missing \"=\" sign")));
315
317
318 for (++s, read = 0; isalpha((unsigned char) *s) || *s == '*'; s++)
319 {
320 switch (*s)
321 {
322 case '*':
323 goption |= read;
324 break;
325 case ACL_INSERT_CHR:
327 break;
328 case ACL_SELECT_CHR:
330 break;
331 case ACL_UPDATE_CHR:
333 break;
334 case ACL_DELETE_CHR:
336 break;
337 case ACL_TRUNCATE_CHR:
339 break;
342 break;
343 case ACL_TRIGGER_CHR:
345 break;
346 case ACL_EXECUTE_CHR:
348 break;
349 case ACL_USAGE_CHR:
350 read = ACL_USAGE;
351 break;
352 case ACL_CREATE_CHR:
354 break;
357 break;
358 case ACL_CONNECT_CHR:
360 break;
361 case ACL_SET_CHR:
362 read = ACL_SET;
363 break;
366 break;
367 case ACL_MAINTAIN_CHR:
369 break;
370 default:
371 ereturn(escontext, NULL,
373 errmsg("invalid mode character: must be one of \"%s\"",
375 }
376
377 privs |= read;
378 }
379
380 if (name[0] == '\0')
381 aip->ai_grantee = ACL_ID_PUBLIC;
382 else
383 {
385 aip->ai_grantee = boot_get_role_oid(name);
386 else
387 aip->ai_grantee = get_role_oid(name, true);
388 if (!OidIsValid(aip->ai_grantee))
389 ereturn(escontext, NULL,
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 if (*s == '/')
400 {
401 s = getid(s + 1, name2, escontext);
402 if (s == NULL)
403 return NULL;
404 if (name2[0] == '\0')
405 ereturn(escontext, NULL,
407 errmsg("a name must follow the \"/\" sign")));
409 aip->ai_grantor = boot_get_role_oid(name2);
410 else
411 aip->ai_grantor = get_role_oid(name2, true);
412 if (!OidIsValid(aip->ai_grantor))
413 ereturn(escontext, NULL,
415 errmsg("role \"%s\" does not exist", name2)));
416 }
417 else
418 {
419 aip->ai_grantor = BOOTSTRAP_SUPERUSERID;
423 errmsg("defaulting grantor to user ID %u",
425 }
426
428
429 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 */
439static Acl *
441{
442 Acl *new_acl;
443 Size size;
444
445 if (n < 0)
446 elog(ERROR, "invalid size: %d", n);
447 size = ACL_N_SIZE(n);
448 new_acl = (Acl *) palloc0(size);
449 SET_VARSIZE(new_acl, size);
450 new_acl->ndim = 1;
451 new_acl->dataoffset = 0; /* we never put in any nulls */
452 new_acl->elemtype = ACLITEMOID;
453 ARR_LBOUND(new_acl)[0] = 1;
454 ARR_DIMS(new_acl)[0] = n;
455 return new_acl;
456}
457
458/*
459 * Create a zero-entry ACL
460 */
461Acl *
463{
464 return allocacl(0);
465}
466
467/*
468 * Copy an ACL
469 */
470Acl *
472{
474
476
479 ACL_NUM(orig_acl) * sizeof(AclItem));
480
481 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 */
490Acl *
492{
494
496
499 ACL_NUM(left_acl) * sizeof(AclItem));
500
503 ACL_NUM(right_acl) * sizeof(AclItem));
504
505 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 */
514Acl *
515aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId)
516{
518 AclItem *aip;
519 int i,
520 num;
521
522 /* Check for cases where one or both are empty/null */
523 if (left_acl == NULL || ACL_NUM(left_acl) == 0)
524 {
525 if (right_acl == NULL || ACL_NUM(right_acl) == 0)
526 return NULL;
527 else
528 return aclcopy(right_acl);
529 }
530 else
531 {
532 if (right_acl == NULL || ACL_NUM(right_acl) == 0)
533 return aclcopy(left_acl);
534 }
535
536 /* Merge them the hard way, one item at a time */
538
540 num = ACL_NUM(right_acl);
541
542 for (i = 0; i < num; i++, aip++)
543 {
544 Acl *tmp_acl;
545
547 ownerId, DROP_RESTRICT);
550 }
551
552 return result_acl;
553}
554
555/*
556 * Sort the items in an ACL (into an arbitrary but consistent order)
557 */
558void
560{
561 if (acl != NULL && ACL_NUM(acl) > 1)
562 qsort(ACL_DAT(acl), ACL_NUM(acl), sizeof(AclItem), aclitemComparator);
563}
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 */
572bool
574{
575 /* Check for cases where one or both are empty/null */
576 if (left_acl == NULL || ACL_NUM(left_acl) == 0)
577 {
578 if (right_acl == NULL || ACL_NUM(right_acl) == 0)
579 return true;
580 else
581 return false;
582 }
583 else
584 {
585 if (right_acl == NULL || ACL_NUM(right_acl) == 0)
586 return false;
587 }
588
590 return false;
591
594 ACL_NUM(left_acl) * sizeof(AclItem)) == 0)
595 return true;
596
597 return false;
598}
599
600/*
601 * Verify that an ACL array is acceptable (one-dimensional and has no nulls)
602 */
603static void
604check_acl(const Acl *acl)
605{
606 if (ARR_ELEMTYPE(acl) != ACLITEMOID)
609 errmsg("ACL array contains wrong data type")));
610 if (ARR_NDIM(acl) != 1)
613 errmsg("ACL arrays must be one-dimensional")));
614 if (ARR_HASNULL(acl))
617 errmsg("ACL arrays must not contain null values")));
618}
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 */
628Datum
630{
631 const char *s = PG_GETARG_CSTRING(0);
632 Node *escontext = fcinfo->context;
633 AclItem *aip;
634
636
637 s = aclparse(s, aip, escontext);
638 if (s == NULL)
640
641 while (isspace((unsigned char) *s))
642 ++s;
643 if (*s)
644 ereturn(escontext, (Datum) 0,
646 errmsg("extra garbage at the end of the ACL specification")));
647
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 */
663Datum
665{
667 char *p;
668 char *out;
669 HeapTuple htup;
670 unsigned i;
671
672 out = palloc(strlen("=/") +
673 2 * N_ACL_RIGHTS +
674 2 * (2 * NAMEDATALEN + 2) +
675 1);
676
677 p = out;
678 *p = '\0';
679
680 if (aip->ai_grantee != ACL_ID_PUBLIC)
681 {
683 htup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(aip->ai_grantee));
684 else
685 htup = NULL;
686 if (HeapTupleIsValid(htup))
687 {
689 ReleaseSysCache(htup);
690 }
691 else
692 {
693 /* No such entry, or bootstrap mode: print numeric OID */
694 sprintf(p, "%u", aip->ai_grantee);
695 }
696 }
697 while (*p)
698 ++p;
699
700 *p++ = '=';
701
702 for (i = 0; i < N_ACL_RIGHTS; ++i)
703 {
704 if (ACLITEM_GET_PRIVS(*aip) & (UINT64CONST(1) << i))
705 *p++ = ACL_ALL_RIGHTS_STR[i];
706 if (ACLITEM_GET_GOPTIONS(*aip) & (UINT64CONST(1) << i))
707 *p++ = '*';
708 }
709
710 *p++ = '/';
711 *p = '\0';
712
714 htup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(aip->ai_grantor));
715 else
716 htup = NULL;
717 if (HeapTupleIsValid(htup))
718 {
720 ReleaseSysCache(htup);
721 }
722 else
723 {
724 /* No such entry, or bootstrap mode: print numeric OID */
725 sprintf(p, "%u", aip->ai_grantor);
726 }
727
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 */
736static bool
738{
739 return a1->ai_grantee == a2->ai_grantee &&
740 a1->ai_grantor == a2->ai_grantor;
741}
742
743/*
744 * aclitemComparator
745 * qsort comparison function for AclItems
746 */
747static int
748aclitemComparator(const void *arg1, const void *arg2)
749{
750 const AclItem *a1 = (const AclItem *) arg1;
751 const AclItem *a2 = (const AclItem *) arg2;
752
753 if (a1->ai_grantee > a2->ai_grantee)
754 return 1;
755 if (a1->ai_grantee < a2->ai_grantee)
756 return -1;
757 if (a1->ai_grantor > a2->ai_grantor)
758 return 1;
759 if (a1->ai_grantor < a2->ai_grantor)
760 return -1;
761 if (a1->ai_privs > a2->ai_privs)
762 return 1;
763 if (a1->ai_privs < a2->ai_privs)
764 return -1;
765 return 0;
766}
767
768/*
769 * aclitem equality operator
770 */
771Datum
773{
776 bool result;
777
778 result = a1->ai_privs == a2->ai_privs &&
779 a1->ai_grantee == a2->ai_grantee &&
780 a1->ai_grantor == a2->ai_grantor;
781 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 */
791Datum
793{
795
796 /* not very bright, but avoids any issue of padding in struct */
797 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 */
805Datum
807{
809 uint64 seed = PG_GETARG_INT64(1);
810 uint32 sum = (uint32) (a->ai_privs + a->ai_grantee + a->ai_grantor);
811
812 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 */
826Acl *
827acldefault(ObjectType objtype, Oid ownerId)
828{
831 int nacl;
832 Acl *acl;
833 AclItem *aip;
834
835 switch (objtype)
836 {
837 case OBJECT_COLUMN:
838 /* by default, columns have no extra privileges */
841 break;
842 case OBJECT_TABLE:
845 break;
846 case OBJECT_SEQUENCE:
849 break;
850 case OBJECT_DATABASE:
851 /* for backwards compatibility, grant some rights by default */
854 break;
855 case OBJECT_FUNCTION:
856 /* Grant EXECUTE by default, for now */
859 break;
860 case OBJECT_LANGUAGE:
861 /* Grant USAGE by default, for now */
864 break;
868 break;
869 case OBJECT_SCHEMA:
872 break;
876 break;
877 case OBJECT_FDW:
880 break;
884 break;
885 case OBJECT_DOMAIN:
886 case OBJECT_TYPE:
889 break;
893 break;
894 case OBJECT_PROPGRAPH:
897 break;
898 default:
899 elog(ERROR, "unrecognized object type: %d", (int) objtype);
900 world_default = ACL_NO_RIGHTS; /* keep compiler quiet */
902 break;
903 }
904
905 nacl = 0;
907 nacl++;
909 nacl++;
910
911 acl = allocacl(nacl);
912 aip = ACL_DAT(acl);
913
915 {
916 aip->ai_grantee = ACL_ID_PUBLIC;
917 aip->ai_grantor = ownerId;
919 aip++;
920 }
921
922 /*
923 * Note that the owner's entry shows all ordinary privileges but no grant
924 * options. This is because his grant options come "from the system" and
925 * not from his own efforts. (The SQL spec says that the owner's rights
926 * come from a "_SYSTEM" authid.) However, we do consider that the
927 * owner's ordinary privileges are self-granted; this lets him revoke
928 * them. We implement the owner's grant options without any explicit
929 * "_SYSTEM"-like ACL entry, by internally special-casing the owner
930 * wherever we are testing grant options.
931 */
933 {
934 aip->ai_grantee = ownerId;
935 aip->ai_grantor = ownerId;
937 }
938
939 return acl;
940}
941
942
943/*
944 * SQL-accessible version of acldefault(). Hackish mapping from "char" type to
945 * OBJECT_* values.
946 */
947Datum
949{
950 char objtypec = PG_GETARG_CHAR(0);
951 Oid owner = PG_GETARG_OID(1);
952 ObjectType objtype = 0;
953
954 switch (objtypec)
955 {
956 case 'c':
957 objtype = OBJECT_COLUMN;
958 break;
959 case 'r':
960 objtype = OBJECT_TABLE;
961 break;
962 case 's':
963 objtype = OBJECT_SEQUENCE;
964 break;
965 case 'd':
966 objtype = OBJECT_DATABASE;
967 break;
968 case 'f':
969 objtype = OBJECT_FUNCTION;
970 break;
971 case 'l':
972 objtype = OBJECT_LANGUAGE;
973 break;
974 case 'L':
975 objtype = OBJECT_LARGEOBJECT;
976 break;
977 case 'n':
978 objtype = OBJECT_SCHEMA;
979 break;
980 case 'p':
981 objtype = OBJECT_PARAMETER_ACL;
982 break;
983 case 't':
984 objtype = OBJECT_TABLESPACE;
985 break;
986 case 'F':
987 objtype = OBJECT_FDW;
988 break;
989 case 'S':
990 objtype = OBJECT_FOREIGN_SERVER;
991 break;
992 case 'T':
993 objtype = OBJECT_TYPE;
994 break;
995 default:
996 elog(ERROR, "unrecognized object type abbreviation: %c", objtypec);
997 }
998
999 PG_RETURN_ACL_P(acldefault(objtype, owner));
1000}
1001
1002
1003/*
1004 * Update an ACL array to add or remove specified privileges.
1005 *
1006 * old_acl: the input ACL array
1007 * mod_aip: defines the privileges to be added, removed, or substituted
1008 * modechg: ACL_MODECHG_ADD, ACL_MODECHG_DEL, or ACL_MODECHG_EQL
1009 * ownerId: Oid of object owner
1010 * behavior: RESTRICT or CASCADE behavior for recursive removal
1011 *
1012 * ownerid and behavior are only relevant when the update operation specifies
1013 * deletion of grant options.
1014 *
1015 * The result is a modified copy; the input object is not changed.
1016 *
1017 * NB: caller is responsible for having detoasted the input ACL, if needed.
1018 */
1019Acl *
1021 int modechg, Oid ownerId, DropBehavior behavior)
1022{
1023 Acl *new_acl = NULL;
1025 *new_aip = NULL;
1028 new_rights,
1030 int dst,
1031 num;
1032
1033 /* Caller probably already checked old_acl, but be safe */
1035
1036 /* If granting grant options, check for circularity */
1037 if (modechg != ACL_MODECHG_DEL &&
1040
1041 num = ACL_NUM(old_acl);
1043
1044 /*
1045 * Search the ACL for an existing entry for this grantee and grantor. If
1046 * one exists, just modify the entry in-place (well, in the same position,
1047 * since we actually return a copy); otherwise, insert the new entry at
1048 * the end.
1049 */
1050
1051 for (dst = 0; dst < num; ++dst)
1052 {
1054 {
1055 /* found a match, so modify existing item */
1056 new_acl = allocacl(num);
1059 break;
1060 }
1061 }
1062
1063 if (dst == num)
1064 {
1065 /* need to append a new item */
1066 new_acl = allocacl(num + 1);
1068 memcpy(new_aip, old_aip, num * sizeof(AclItem));
1069
1070 /* initialize the new entry with no permissions */
1071 new_aip[dst].ai_grantee = mod_aip->ai_grantee;
1072 new_aip[dst].ai_grantor = mod_aip->ai_grantor;
1075 num++; /* set num to the size of new_acl */
1076 }
1077
1080
1081 /* apply the specified permissions change */
1082 switch (modechg)
1083 {
1084 case ACL_MODECHG_ADD:
1087 break;
1088 case ACL_MODECHG_DEL:
1091 break;
1092 case ACL_MODECHG_EQL:
1095 break;
1096 }
1097
1100
1101 /*
1102 * If the adjusted entry has no permissions, delete it from the list.
1103 */
1105 {
1107 new_aip + dst + 1,
1108 (num - dst - 1) * sizeof(AclItem));
1109 /* Adjust array size to be 'num - 1' items */
1110 ARR_DIMS(new_acl)[0] = num - 1;
1111 SET_VARSIZE(new_acl, ACL_N_SIZE(num - 1));
1112 }
1113
1114 /*
1115 * Remove abandoned privileges (cascading revoke). Currently we can only
1116 * handle this when the grantee is not PUBLIC.
1117 */
1118 if ((old_goptions & ~new_goptions) != 0)
1119 {
1120 Assert(mod_aip->ai_grantee != ACL_ID_PUBLIC);
1121 new_acl = recursive_revoke(new_acl, mod_aip->ai_grantee,
1123 ownerId, behavior);
1124 }
1125
1126 return new_acl;
1127}
1128
1129/*
1130 * Update an ACL array to reflect a change of owner to the parent object
1131 *
1132 * old_acl: the input ACL array (must not be NULL)
1133 * oldOwnerId: Oid of the old object owner
1134 * newOwnerId: Oid of the new object owner
1135 *
1136 * The result is a modified copy; the input object is not changed.
1137 *
1138 * NB: caller is responsible for having detoasted the input ACL, if needed.
1139 *
1140 * Note: the name of this function is a bit of a misnomer, since it will
1141 * happily make the specified role substitution whether the old role is
1142 * really the owner of the parent object or merely mentioned in its ACL.
1143 * But the vast majority of callers use it in connection with ALTER OWNER
1144 * operations, so we'll keep the name.
1145 */
1146Acl *
1148{
1149 Acl *new_acl;
1155 bool newpresent = false;
1156 int dst,
1157 src,
1158 targ,
1159 num;
1160
1162
1163 /*
1164 * Make a copy of the given ACL, substituting new owner ID for old
1165 * wherever it appears as either grantor or grantee. Also note if the new
1166 * owner ID is already present.
1167 */
1168 num = ACL_NUM(old_acl);
1170 new_acl = allocacl(num);
1172 memcpy(new_aip, old_aip, num * sizeof(AclItem));
1173 for (dst = 0, dst_aip = new_aip; dst < num; dst++, dst_aip++)
1174 {
1175 if (dst_aip->ai_grantor == oldOwnerId)
1176 dst_aip->ai_grantor = newOwnerId;
1177 else if (dst_aip->ai_grantor == newOwnerId)
1178 newpresent = true;
1179 if (dst_aip->ai_grantee == oldOwnerId)
1180 dst_aip->ai_grantee = newOwnerId;
1181 else if (dst_aip->ai_grantee == newOwnerId)
1182 newpresent = true;
1183 }
1184
1185 /*
1186 * If the old ACL contained any references to the new owner, then we may
1187 * now have generated an ACL containing duplicate entries. Find them and
1188 * merge them so that there are not duplicates. (This is relatively
1189 * expensive since we use a stupid O(N^2) algorithm, but it's unlikely to
1190 * be the normal case.)
1191 *
1192 * To simplify deletion of duplicate entries, we temporarily leave them in
1193 * the array but set their privilege masks to zero; when we reach such an
1194 * entry it's just skipped. (Thus, a side effect of this code will be to
1195 * remove privilege-free entries, should there be any in the input.) dst
1196 * is the next output slot, targ is the currently considered input slot
1197 * (always >= dst), and src scans entries to the right of targ looking for
1198 * duplicates. Once an entry has been emitted to dst it is known
1199 * duplicate-free and need not be considered anymore.
1200 */
1201 if (newpresent)
1202 {
1203 dst = 0;
1204 for (targ = 0, targ_aip = new_aip; targ < num; targ++, targ_aip++)
1205 {
1206 /* ignore if deleted in an earlier pass */
1208 continue;
1209 /* find and merge any duplicates */
1210 for (src = targ + 1, src_aip = targ_aip + 1; src < num;
1211 src++, src_aip++)
1212 {
1214 continue;
1216 {
1220 /* mark the duplicate deleted */
1222 }
1223 }
1224 /* and emit to output */
1225 new_aip[dst] = *targ_aip;
1226 dst++;
1227 }
1228 /* Adjust array size to be 'dst' items */
1229 ARR_DIMS(new_acl)[0] = dst;
1231 }
1232
1233 return new_acl;
1234}
1235
1236
1237/*
1238 * When granting grant options, we must disallow attempts to set up circular
1239 * chains of grant options. Suppose A (the object owner) grants B some
1240 * privileges with grant option, and B re-grants them to C. If C could
1241 * grant the privileges to B as well, then A would be unable to effectively
1242 * revoke the privileges from B, since recursive_revoke would consider that
1243 * B still has 'em from C.
1244 *
1245 * We check for this by recursively deleting all grant options belonging to
1246 * the target grantee, and then seeing if the would-be grantor still has the
1247 * grant option or not.
1248 */
1249static void
1251 Oid ownerId)
1252{
1253 Acl *acl;
1254 AclItem *aip;
1255 int i,
1256 num;
1258
1260
1261 /*
1262 * For now, grant options can only be granted to roles, not PUBLIC.
1263 * Otherwise we'd have to work a bit harder here.
1264 */
1265 Assert(mod_aip->ai_grantee != ACL_ID_PUBLIC);
1266
1267 /* The owner always has grant options, no need to check */
1268 if (mod_aip->ai_grantor == ownerId)
1269 return;
1270
1271 /* Make a working copy */
1272 acl = allocacl(ACL_NUM(old_acl));
1274
1275 /* Zap all grant options of target grantee, plus what depends on 'em */
1277 num = ACL_NUM(acl);
1278 aip = ACL_DAT(acl);
1279 for (i = 0; i < num; i++)
1280 {
1281 if (aip[i].ai_grantee == mod_aip->ai_grantee &&
1283 {
1284 Acl *new_acl;
1285
1286 /* We'll actually zap ordinary privs too, but no matter */
1288 ownerId, DROP_CASCADE);
1289
1290 pfree(acl);
1291 acl = new_acl;
1292
1293 goto cc_restart;
1294 }
1295 }
1296
1297 /* Now we can compute grantor's independently-derived privileges */
1298 own_privs = aclmask(acl,
1299 mod_aip->ai_grantor,
1300 ownerId,
1302 ACLMASK_ALL);
1304
1305 if ((ACLITEM_GET_GOPTIONS(*mod_aip) & ~own_privs) != 0)
1306 ereport(ERROR,
1308 errmsg("grant options cannot be granted back to your own grantor")));
1309
1310 pfree(acl);
1311}
1312
1313
1314/*
1315 * Ensure that no privilege is "abandoned". A privilege is abandoned
1316 * if the user that granted the privilege loses the grant option. (So
1317 * the chain through which it was granted is broken.) Either the
1318 * abandoned privileges are revoked as well, or an error message is
1319 * printed, depending on the drop behavior option.
1320 *
1321 * acl: the input ACL list
1322 * grantee: the user from whom some grant options have been revoked
1323 * revoke_privs: the grant options being revoked
1324 * ownerId: Oid of object owner
1325 * behavior: RESTRICT or CASCADE behavior for recursive removal
1326 *
1327 * The input Acl object is pfree'd if replaced.
1328 */
1329static Acl *
1331 Oid grantee,
1333 Oid ownerId,
1334 DropBehavior behavior)
1335{
1337 AclItem *aip;
1338 int i,
1339 num;
1340
1341 check_acl(acl);
1342
1343 /* The owner can never truly lose grant options, so short-circuit */
1344 if (grantee == ownerId)
1345 return acl;
1346
1347 /* The grantee might still have some grant options via another grantor */
1348 still_has = aclmask(acl, grantee, ownerId,
1350 ACLMASK_ALL);
1353 return acl;
1354
1355restart:
1356 num = ACL_NUM(acl);
1357 aip = ACL_DAT(acl);
1358 for (i = 0; i < num; i++)
1359 {
1360 if (aip[i].ai_grantor == grantee
1361 && (ACLITEM_GET_PRIVS(aip[i]) & revoke_privs) != 0)
1362 {
1364 Acl *new_acl;
1365
1366 if (behavior == DROP_RESTRICT)
1367 ereport(ERROR,
1369 errmsg("dependent privileges exist"),
1370 errhint("Use CASCADE to revoke them too.")));
1371
1372 mod_acl.ai_grantor = grantee;
1373 mod_acl.ai_grantee = aip[i].ai_grantee;
1376 revoke_privs);
1377
1379 ownerId, behavior);
1380
1381 pfree(acl);
1382 acl = new_acl;
1383
1384 goto restart;
1385 }
1386 }
1387
1388 return acl;
1389}
1390
1391
1392/*
1393 * aclmask --- compute bitmask of all privileges held by roleid.
1394 *
1395 * When 'how' = ACLMASK_ALL, this simply returns the privilege bits
1396 * held by the given roleid according to the given ACL list, ANDed
1397 * with 'mask'. (The point of passing 'mask' is to let the routine
1398 * exit early if all privileges of interest have been found.)
1399 *
1400 * When 'how' = ACLMASK_ANY, returns as soon as any bit in the mask
1401 * is known true. (This lets us exit soonest in cases where the
1402 * caller is only going to test for zero or nonzero result.)
1403 *
1404 * Usage patterns:
1405 *
1406 * To see if any of a set of privileges are held:
1407 * if (aclmask(acl, roleid, ownerId, privs, ACLMASK_ANY) != 0)
1408 *
1409 * To see if all of a set of privileges are held:
1410 * if (aclmask(acl, roleid, ownerId, privs, ACLMASK_ALL) == privs)
1411 *
1412 * To determine exactly which of a set of privileges are held:
1413 * heldprivs = aclmask(acl, roleid, ownerId, privs, ACLMASK_ALL);
1414 */
1415AclMode
1416aclmask(const Acl *acl, Oid roleid, Oid ownerId,
1417 AclMode mask, AclMaskHow how)
1418{
1419 AclMode result;
1421 AclItem *aidat;
1422 int i,
1423 num;
1424
1425 /*
1426 * Null ACL should not happen, since caller should have inserted
1427 * appropriate default
1428 */
1429 if (acl == NULL)
1430 elog(ERROR, "null ACL");
1431
1432 check_acl(acl);
1433
1434 /* Quick exit for mask == 0 */
1435 if (mask == 0)
1436 return 0;
1437
1438 result = 0;
1439
1440 /* Owner always implicitly has all grant options */
1441 if ((mask & ACLITEM_ALL_GOPTION_BITS) &&
1442 has_privs_of_role(roleid, ownerId))
1443 {
1444 result = mask & ACLITEM_ALL_GOPTION_BITS;
1445 if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1446 return result;
1447 }
1448
1449 num = ACL_NUM(acl);
1450 aidat = ACL_DAT(acl);
1451
1452 /*
1453 * Check privileges granted directly to roleid or to public
1454 */
1455 for (i = 0; i < num; i++)
1456 {
1457 AclItem *aidata = &aidat[i];
1458
1459 if (aidata->ai_grantee == ACL_ID_PUBLIC ||
1460 aidata->ai_grantee == roleid)
1461 {
1462 result |= aidata->ai_privs & mask;
1463 if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1464 return result;
1465 }
1466 }
1467
1468 /*
1469 * Check privileges granted indirectly via role memberships. We do this in
1470 * a separate pass to minimize expensive indirect membership tests. In
1471 * particular, it's worth testing whether a given ACL entry grants any
1472 * privileges still of interest before we perform the has_privs_of_role
1473 * test.
1474 */
1475 remaining = mask & ~result;
1476 for (i = 0; i < num; i++)
1477 {
1478 AclItem *aidata = &aidat[i];
1479
1480 if (aidata->ai_grantee == ACL_ID_PUBLIC ||
1481 aidata->ai_grantee == roleid)
1482 continue; /* already checked it */
1483
1484 if ((aidata->ai_privs & remaining) &&
1485 has_privs_of_role(roleid, aidata->ai_grantee))
1486 {
1487 result |= aidata->ai_privs & mask;
1488 if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1489 return result;
1490 remaining = mask & ~result;
1491 }
1492 }
1493
1494 return result;
1495}
1496
1497
1498/*
1499 * aclmask_direct --- compute bitmask of all privileges held by roleid.
1500 *
1501 * This is exactly like aclmask() except that we consider only privileges
1502 * held *directly* by roleid, not those inherited via role membership.
1503 */
1504static AclMode
1505aclmask_direct(const Acl *acl, Oid roleid, Oid ownerId,
1506 AclMode mask, AclMaskHow how)
1507{
1508 AclMode result;
1509 AclItem *aidat;
1510 int i,
1511 num;
1512
1513 /*
1514 * Null ACL should not happen, since caller should have inserted
1515 * appropriate default
1516 */
1517 if (acl == NULL)
1518 elog(ERROR, "null ACL");
1519
1520 check_acl(acl);
1521
1522 /* Quick exit for mask == 0 */
1523 if (mask == 0)
1524 return 0;
1525
1526 result = 0;
1527
1528 /* Owner always implicitly has all grant options */
1529 if ((mask & ACLITEM_ALL_GOPTION_BITS) &&
1530 roleid == ownerId)
1531 {
1532 result = mask & ACLITEM_ALL_GOPTION_BITS;
1533 if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1534 return result;
1535 }
1536
1537 num = ACL_NUM(acl);
1538 aidat = ACL_DAT(acl);
1539
1540 /*
1541 * Check privileges granted directly to roleid (and not to public)
1542 */
1543 for (i = 0; i < num; i++)
1544 {
1545 AclItem *aidata = &aidat[i];
1546
1547 if (aidata->ai_grantee == roleid)
1548 {
1549 result |= aidata->ai_privs & mask;
1550 if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1551 return result;
1552 }
1553 }
1554
1555 return result;
1556}
1557
1558
1559/*
1560 * aclmembers
1561 * Find out all the roleids mentioned in an Acl.
1562 * Note that we do not distinguish grantors from grantees.
1563 *
1564 * *roleids is set to point to a palloc'd array containing distinct OIDs
1565 * in sorted order. The length of the array is the function result.
1566 */
1567int
1569{
1570 Oid *list;
1571 const AclItem *acldat;
1572 int i,
1573 j;
1574
1575 if (acl == NULL || ACL_NUM(acl) == 0)
1576 {
1577 *roleids = NULL;
1578 return 0;
1579 }
1580
1581 check_acl(acl);
1582
1583 /* Allocate the worst-case space requirement */
1584 list = palloc(ACL_NUM(acl) * 2 * sizeof(Oid));
1585 acldat = ACL_DAT(acl);
1586
1587 /*
1588 * Walk the ACL collecting mentioned RoleIds.
1589 */
1590 j = 0;
1591 for (i = 0; i < ACL_NUM(acl); i++)
1592 {
1593 const AclItem *ai = &acldat[i];
1594
1595 if (ai->ai_grantee != ACL_ID_PUBLIC)
1596 list[j++] = ai->ai_grantee;
1597 /* grantor is currently never PUBLIC, but let's check anyway */
1598 if (ai->ai_grantor != ACL_ID_PUBLIC)
1599 list[j++] = ai->ai_grantor;
1600 }
1601
1602 /* Sort the array */
1603 qsort(list, j, sizeof(Oid), oid_cmp);
1604
1605 /*
1606 * We could repalloc the array down to minimum size, but it's hardly worth
1607 * it since it's only transient memory.
1608 */
1609 *roleids = list;
1610
1611 /* Remove duplicates from the array */
1612 return qunique(list, j, sizeof(Oid), oid_cmp);
1613}
1614
1615
1616/*
1617 * aclinsert (exported function)
1618 */
1619Datum
1621{
1622 ereport(ERROR,
1624 errmsg("aclinsert is no longer supported")));
1625
1626 PG_RETURN_NULL(); /* keep compiler quiet */
1627}
1628
1629Datum
1631{
1632 ereport(ERROR,
1634 errmsg("aclremove is no longer supported")));
1635
1636 PG_RETURN_NULL(); /* keep compiler quiet */
1637}
1638
1639Datum
1641{
1642 Acl *acl = PG_GETARG_ACL_P(0);
1644 AclItem *aidat;
1645 int i,
1646 num;
1647
1648 check_acl(acl);
1649 num = ACL_NUM(acl);
1650 aidat = ACL_DAT(acl);
1651 for (i = 0; i < num; ++i)
1652 {
1653 if (aip->ai_grantee == aidat[i].ai_grantee &&
1654 aip->ai_grantor == aidat[i].ai_grantor &&
1656 PG_RETURN_BOOL(true);
1657 }
1658 PG_RETURN_BOOL(false);
1659}
1660
1661Datum
1663{
1665 Oid grantor = PG_GETARG_OID(1);
1667 bool goption = PG_GETARG_BOOL(3);
1668 AclItem *result;
1669 AclMode priv;
1670 static const priv_map any_priv_map[] = {
1671 {"SELECT", ACL_SELECT},
1672 {"INSERT", ACL_INSERT},
1673 {"UPDATE", ACL_UPDATE},
1674 {"DELETE", ACL_DELETE},
1675 {"TRUNCATE", ACL_TRUNCATE},
1676 {"REFERENCES", ACL_REFERENCES},
1677 {"TRIGGER", ACL_TRIGGER},
1678 {"EXECUTE", ACL_EXECUTE},
1679 {"USAGE", ACL_USAGE},
1680 {"CREATE", ACL_CREATE},
1681 {"TEMP", ACL_CREATE_TEMP},
1682 {"TEMPORARY", ACL_CREATE_TEMP},
1683 {"CONNECT", ACL_CONNECT},
1684 {"SET", ACL_SET},
1685 {"ALTER SYSTEM", ACL_ALTER_SYSTEM},
1686 {"MAINTAIN", ACL_MAINTAIN},
1687 {NULL, 0}
1688 };
1689
1691
1692 result = palloc_object(AclItem);
1693
1694 result->ai_grantee = grantee;
1695 result->ai_grantor = grantor;
1696
1697 ACLITEM_SET_PRIVS_GOPTIONS(*result, priv,
1698 (goption ? priv : ACL_NO_RIGHTS));
1699
1700 PG_RETURN_ACLITEM_P(result);
1701}
1702
1703
1704/*
1705 * convert_any_priv_string: recognize privilege strings for has_foo_privilege
1706 *
1707 * We accept a comma-separated list of case-insensitive privilege names,
1708 * producing a bitmask of the OR'd privilege bits. We are liberal about
1709 * whitespace between items, not so much about whitespace within items.
1710 * The allowed privilege names are given as an array of priv_map structs,
1711 * terminated by one with a NULL name pointer.
1712 */
1713static AclMode
1715 const priv_map *privileges)
1716{
1717 AclMode result = 0;
1719 char *chunk;
1720 char *next_chunk;
1721
1722 /* We rely on priv_type being a private, modifiable string */
1723 for (chunk = priv_type; chunk; chunk = next_chunk)
1724 {
1725 int chunk_len;
1726 const priv_map *this_priv;
1727
1728 /* Split string at commas */
1729 next_chunk = strchr(chunk, ',');
1730 if (next_chunk)
1731 *next_chunk++ = '\0';
1732
1733 /* Drop leading/trailing whitespace in this chunk */
1734 while (*chunk && isspace((unsigned char) *chunk))
1735 chunk++;
1736 chunk_len = strlen(chunk);
1737 while (chunk_len > 0 && isspace((unsigned char) chunk[chunk_len - 1]))
1738 chunk_len--;
1739 chunk[chunk_len] = '\0';
1740
1741 /* Match to the privileges list */
1742 for (this_priv = privileges; this_priv->name; this_priv++)
1743 {
1744 if (pg_strcasecmp(this_priv->name, chunk) == 0)
1745 {
1746 result |= this_priv->value;
1747 break;
1748 }
1749 }
1750 if (!this_priv->name)
1751 ereport(ERROR,
1753 errmsg("unrecognized privilege type: \"%s\"", chunk)));
1754 }
1755
1757 return result;
1758}
1759
1760
1761static const char *
1763{
1764 switch (aclright)
1765 {
1766 case ACL_INSERT:
1767 return "INSERT";
1768 case ACL_SELECT:
1769 return "SELECT";
1770 case ACL_UPDATE:
1771 return "UPDATE";
1772 case ACL_DELETE:
1773 return "DELETE";
1774 case ACL_TRUNCATE:
1775 return "TRUNCATE";
1776 case ACL_REFERENCES:
1777 return "REFERENCES";
1778 case ACL_TRIGGER:
1779 return "TRIGGER";
1780 case ACL_EXECUTE:
1781 return "EXECUTE";
1782 case ACL_USAGE:
1783 return "USAGE";
1784 case ACL_CREATE:
1785 return "CREATE";
1786 case ACL_CREATE_TEMP:
1787 return "TEMPORARY";
1788 case ACL_CONNECT:
1789 return "CONNECT";
1790 case ACL_SET:
1791 return "SET";
1792 case ACL_ALTER_SYSTEM:
1793 return "ALTER SYSTEM";
1794 case ACL_MAINTAIN:
1795 return "MAINTAIN";
1796 default:
1797 elog(ERROR, "unrecognized aclright: %d", aclright);
1798 return NULL;
1799 }
1800}
1801
1802
1803/*----------
1804 * Convert an aclitem[] to a table.
1805 *
1806 * Example:
1807 *
1808 * aclexplode('{=r/joe,foo=a*w/joe}'::aclitem[])
1809 *
1810 * returns the table
1811 *
1812 * {{ OID(joe), 0::OID, 'SELECT', false },
1813 * { OID(joe), OID(foo), 'INSERT', true },
1814 * { OID(joe), OID(foo), 'UPDATE', false }}
1815 *----------
1816 */
1817Datum
1819{
1820 Acl *acl = PG_GETARG_ACL_P(0);
1822 int *idx;
1823 AclItem *aidat;
1824
1825 if (SRF_IS_FIRSTCALL())
1826 {
1827 TupleDesc tupdesc;
1828 MemoryContext oldcontext;
1829
1830 check_acl(acl);
1831
1833 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1834
1835 /*
1836 * build tupdesc for result tuples (matches out parameters in pg_proc
1837 * entry)
1838 */
1839 tupdesc = CreateTemplateTupleDesc(4);
1840 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "grantor",
1841 OIDOID, -1, 0);
1842 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "grantee",
1843 OIDOID, -1, 0);
1844 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "privilege_type",
1845 TEXTOID, -1, 0);
1846 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "is_grantable",
1847 BOOLOID, -1, 0);
1848
1849 TupleDescFinalize(tupdesc);
1850 funcctx->tuple_desc = BlessTupleDesc(tupdesc);
1851
1852 /* allocate memory for user context */
1853 idx = palloc_array(int, 2);
1854 idx[0] = 0; /* ACL array item index */
1855 idx[1] = -1; /* privilege type counter */
1856 funcctx->user_fctx = idx;
1857
1858 MemoryContextSwitchTo(oldcontext);
1859 }
1860
1862 idx = (int *) funcctx->user_fctx;
1863 aidat = ACL_DAT(acl);
1864
1865 /* need test here in case acl has no items */
1866 while (idx[0] < ACL_NUM(acl))
1867 {
1868 AclItem *aidata;
1870
1871 idx[1]++;
1872 if (idx[1] == N_ACL_RIGHTS)
1873 {
1874 idx[1] = 0;
1875 idx[0]++;
1876 if (idx[0] >= ACL_NUM(acl)) /* done */
1877 break;
1878 }
1879 aidata = &aidat[idx[0]];
1880 priv_bit = UINT64CONST(1) << idx[1];
1881
1883 {
1884 Datum result;
1885 Datum values[4];
1886 bool nulls[4] = {0};
1887 HeapTuple tuple;
1888
1889 values[0] = ObjectIdGetDatum(aidata->ai_grantor);
1890 values[1] = ObjectIdGetDatum(aidata->ai_grantee);
1893
1894 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
1895 result = HeapTupleGetDatum(tuple);
1896
1897 SRF_RETURN_NEXT(funcctx, result);
1898 }
1899 }
1900
1902}
1903
1904
1905/*
1906 * has_table_privilege variants
1907 * These are all named "has_table_privilege" at the SQL level.
1908 * They take various combinations of relation name, relation OID,
1909 * user name, user OID, or implicit user = current_user.
1910 *
1911 * The result is a boolean value: true if user has the indicated
1912 * privilege, false if not. The variants that take a relation OID
1913 * return NULL if the OID doesn't exist (rather than failing, as
1914 * they did before Postgres 8.4).
1915 */
1916
1917/*
1918 * has_table_privilege_name_name
1919 * Check user privileges on a table given
1920 * name username, text tablename, and text priv name.
1921 */
1922Datum
1924{
1925 Name rolename = PG_GETARG_NAME(0);
1926 text *tablename = PG_GETARG_TEXT_PP(1);
1928 Oid roleid;
1929 Oid tableoid;
1930 AclMode mode;
1932
1933 roleid = get_role_oid_or_public(NameStr(*rolename));
1934 tableoid = convert_table_name(tablename);
1936
1937 aclresult = pg_class_aclcheck(tableoid, roleid, mode);
1938
1940}
1941
1942/*
1943 * has_table_privilege_name
1944 * Check user privileges on a table given
1945 * text tablename and text priv name.
1946 * current_user is assumed
1947 */
1948Datum
1950{
1951 text *tablename = PG_GETARG_TEXT_PP(0);
1953 Oid roleid;
1954 Oid tableoid;
1955 AclMode mode;
1957
1958 roleid = GetUserId();
1959 tableoid = convert_table_name(tablename);
1961
1962 aclresult = pg_class_aclcheck(tableoid, roleid, mode);
1963
1965}
1966
1967/*
1968 * has_table_privilege_name_id
1969 * Check user privileges on a table given
1970 * name usename, table oid, and text priv name.
1971 */
1972Datum
1974{
1976 Oid tableoid = PG_GETARG_OID(1);
1978 Oid roleid;
1979 AclMode mode;
1981 bool is_missing = false;
1982
1985
1986 aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
1987
1988 if (is_missing)
1990
1992}
1993
1994/*
1995 * has_table_privilege_id
1996 * Check user privileges on a table given
1997 * table oid, and text priv name.
1998 * current_user is assumed
1999 */
2000Datum
2002{
2003 Oid tableoid = PG_GETARG_OID(0);
2005 Oid roleid;
2006 AclMode mode;
2008 bool is_missing = false;
2009
2010 roleid = GetUserId();
2012
2013 aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2014
2015 if (is_missing)
2017
2019}
2020
2021/*
2022 * has_table_privilege_id_name
2023 * Check user privileges on a table given
2024 * roleid, text tablename, and text priv name.
2025 */
2026Datum
2028{
2029 Oid roleid = PG_GETARG_OID(0);
2030 text *tablename = PG_GETARG_TEXT_PP(1);
2032 Oid tableoid;
2033 AclMode mode;
2035
2036 tableoid = convert_table_name(tablename);
2038
2039 aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2040
2042}
2043
2044/*
2045 * has_table_privilege_id_id
2046 * Check user privileges on a table given
2047 * roleid, table oid, and text priv name.
2048 */
2049Datum
2051{
2052 Oid roleid = PG_GETARG_OID(0);
2053 Oid tableoid = PG_GETARG_OID(1);
2055 AclMode mode;
2057 bool is_missing = false;
2058
2060
2061 aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2062
2063 if (is_missing)
2065
2067}
2068
2069/*
2070 * Support routines for has_table_privilege family.
2071 */
2072
2073/*
2074 * Given a table name expressed as a string, look it up and return Oid
2075 */
2076static Oid
2078{
2079 RangeVar *relrv;
2080
2082
2083 /* We might not even have permissions on this relation; don't lock it. */
2084 return RangeVarGetRelid(relrv, NoLock, false);
2085}
2086
2087/*
2088 * convert_table_priv_string
2089 * Convert text string to AclMode value.
2090 */
2091static AclMode
2093{
2094 static const priv_map table_priv_map[] = {
2095 {"SELECT", ACL_SELECT},
2096 {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
2097 {"INSERT", ACL_INSERT},
2098 {"INSERT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_INSERT)},
2099 {"UPDATE", ACL_UPDATE},
2100 {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
2101 {"DELETE", ACL_DELETE},
2102 {"DELETE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_DELETE)},
2103 {"TRUNCATE", ACL_TRUNCATE},
2104 {"TRUNCATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_TRUNCATE)},
2105 {"REFERENCES", ACL_REFERENCES},
2106 {"REFERENCES WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_REFERENCES)},
2107 {"TRIGGER", ACL_TRIGGER},
2108 {"TRIGGER WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_TRIGGER)},
2109 {"MAINTAIN", ACL_MAINTAIN},
2110 {"MAINTAIN WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_MAINTAIN)},
2111 {NULL, 0}
2112 };
2113
2115}
2116
2117/*
2118 * has_sequence_privilege variants
2119 * These are all named "has_sequence_privilege" at the SQL level.
2120 * They take various combinations of relation name, relation OID,
2121 * user name, user OID, or implicit user = current_user.
2122 *
2123 * The result is a boolean value: true if user has the indicated
2124 * privilege, false if not. The variants that take a relation OID
2125 * return NULL if the OID doesn't exist.
2126 */
2127
2128/*
2129 * has_sequence_privilege_name_name
2130 * Check user privileges on a sequence given
2131 * name username, text sequencename, and text priv name.
2132 */
2133Datum
2157
2158/*
2159 * has_sequence_privilege_name
2160 * Check user privileges on a sequence given
2161 * text sequencename and text priv name.
2162 * current_user is assumed
2163 */
2164Datum
2187
2188/*
2189 * has_sequence_privilege_name_id
2190 * Check user privileges on a sequence given
2191 * name usename, sequence oid, and text priv name.
2192 */
2193Datum
2195{
2199 Oid roleid;
2200 AclMode mode;
2202 char relkind;
2203 bool is_missing = false;
2204
2207 relkind = get_rel_relkind(sequenceoid);
2208 if (relkind == '\0')
2210 else if (relkind != RELKIND_SEQUENCE)
2211 ereport(ERROR,
2213 errmsg("\"%s\" is not a sequence",
2215
2217
2218 if (is_missing)
2220
2222}
2223
2224/*
2225 * has_sequence_privilege_id
2226 * Check user privileges on a sequence given
2227 * sequence oid, and text priv name.
2228 * current_user is assumed
2229 */
2230Datum
2232{
2235 Oid roleid;
2236 AclMode mode;
2238 char relkind;
2239 bool is_missing = false;
2240
2241 roleid = GetUserId();
2243 relkind = get_rel_relkind(sequenceoid);
2244 if (relkind == '\0')
2246 else if (relkind != RELKIND_SEQUENCE)
2247 ereport(ERROR,
2249 errmsg("\"%s\" is not a sequence",
2251
2253
2254 if (is_missing)
2256
2258}
2259
2260/*
2261 * has_sequence_privilege_id_name
2262 * Check user privileges on a sequence given
2263 * roleid, text sequencename, and text priv name.
2264 */
2265Datum
2287
2288/*
2289 * has_sequence_privilege_id_id
2290 * Check user privileges on a sequence given
2291 * roleid, sequence oid, and text priv name.
2292 */
2293Datum
2295{
2296 Oid roleid = PG_GETARG_OID(0);
2299 AclMode mode;
2301 char relkind;
2302 bool is_missing = false;
2303
2305 relkind = get_rel_relkind(sequenceoid);
2306 if (relkind == '\0')
2308 else if (relkind != RELKIND_SEQUENCE)
2309 ereport(ERROR,
2311 errmsg("\"%s\" is not a sequence",
2313
2315
2316 if (is_missing)
2318
2320}
2321
2322/*
2323 * convert_sequence_priv_string
2324 * Convert text string to AclMode value.
2325 */
2326static AclMode
2328{
2329 static const priv_map sequence_priv_map[] = {
2330 {"USAGE", ACL_USAGE},
2331 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
2332 {"SELECT", ACL_SELECT},
2333 {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
2334 {"UPDATE", ACL_UPDATE},
2335 {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
2336 {NULL, 0}
2337 };
2338
2340}
2341
2342
2343/*
2344 * has_any_column_privilege variants
2345 * These are all named "has_any_column_privilege" at the SQL level.
2346 * They take various combinations of relation name, relation OID,
2347 * user name, user OID, or implicit user = current_user.
2348 *
2349 * The result is a boolean value: true if user has the indicated
2350 * privilege for any column of the table, false if not. The variants
2351 * that take a relation OID return NULL if the OID doesn't exist.
2352 */
2353
2354/*
2355 * has_any_column_privilege_name_name
2356 * Check user privileges on any column of a table given
2357 * name username, text tablename, and text priv name.
2358 */
2359Datum
2361{
2362 Name rolename = PG_GETARG_NAME(0);
2363 text *tablename = PG_GETARG_TEXT_PP(1);
2365 Oid roleid;
2366 Oid tableoid;
2367 AclMode mode;
2369
2370 roleid = get_role_oid_or_public(NameStr(*rolename));
2371 tableoid = convert_table_name(tablename);
2373
2374 /* First check at table level, then examine each column if needed */
2375 aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2376 if (aclresult != ACLCHECK_OK)
2377 aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
2378 ACLMASK_ANY);
2379
2381}
2382
2383/*
2384 * has_any_column_privilege_name
2385 * Check user privileges on any column of a table given
2386 * text tablename and text priv name.
2387 * current_user is assumed
2388 */
2389Datum
2391{
2392 text *tablename = PG_GETARG_TEXT_PP(0);
2394 Oid roleid;
2395 Oid tableoid;
2396 AclMode mode;
2398
2399 roleid = GetUserId();
2400 tableoid = convert_table_name(tablename);
2402
2403 /* First check at table level, then examine each column if needed */
2404 aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2405 if (aclresult != ACLCHECK_OK)
2406 aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
2407 ACLMASK_ANY);
2408
2410}
2411
2412/*
2413 * has_any_column_privilege_name_id
2414 * Check user privileges on any column of a table given
2415 * name usename, table oid, and text priv name.
2416 */
2417Datum
2419{
2421 Oid tableoid = PG_GETARG_OID(1);
2423 Oid roleid;
2424 AclMode mode;
2426 bool is_missing = false;
2427
2430
2431 /* First check at table level, then examine each column if needed */
2432 aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2433 if (aclresult != ACLCHECK_OK)
2434 {
2435 if (is_missing)
2437 aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
2439 if (is_missing)
2441 }
2442
2444}
2445
2446/*
2447 * has_any_column_privilege_id
2448 * Check user privileges on any column of a table given
2449 * table oid, and text priv name.
2450 * current_user is assumed
2451 */
2452Datum
2454{
2455 Oid tableoid = PG_GETARG_OID(0);
2457 Oid roleid;
2458 AclMode mode;
2460 bool is_missing = false;
2461
2462 roleid = GetUserId();
2464
2465 /* First check at table level, then examine each column if needed */
2466 aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2467 if (aclresult != ACLCHECK_OK)
2468 {
2469 if (is_missing)
2471 aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
2473 if (is_missing)
2475 }
2476
2478}
2479
2480/*
2481 * has_any_column_privilege_id_name
2482 * Check user privileges on any column of a table given
2483 * roleid, text tablename, and text priv name.
2484 */
2485Datum
2487{
2488 Oid roleid = PG_GETARG_OID(0);
2489 text *tablename = PG_GETARG_TEXT_PP(1);
2491 Oid tableoid;
2492 AclMode mode;
2494
2495 tableoid = convert_table_name(tablename);
2497
2498 /* First check at table level, then examine each column if needed */
2499 aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2500 if (aclresult != ACLCHECK_OK)
2501 aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
2502 ACLMASK_ANY);
2503
2505}
2506
2507/*
2508 * has_any_column_privilege_id_id
2509 * Check user privileges on any column of a table given
2510 * roleid, table oid, and text priv name.
2511 */
2512Datum
2514{
2515 Oid roleid = PG_GETARG_OID(0);
2516 Oid tableoid = PG_GETARG_OID(1);
2518 AclMode mode;
2520 bool is_missing = false;
2521
2523
2524 /* First check at table level, then examine each column if needed */
2525 aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2526 if (aclresult != ACLCHECK_OK)
2527 {
2528 if (is_missing)
2530 aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
2532 if (is_missing)
2534 }
2535
2537}
2538
2539
2540/*
2541 * has_column_privilege variants
2542 * These are all named "has_column_privilege" at the SQL level.
2543 * They take various combinations of relation name, relation OID,
2544 * column name, column attnum, user name, user OID, or
2545 * implicit user = current_user.
2546 *
2547 * The result is a boolean value: true if user has the indicated
2548 * privilege, false if not. The variants that take a relation OID
2549 * return NULL (rather than throwing an error) if that relation OID
2550 * doesn't exist. Likewise, the variants that take an integer attnum
2551 * return NULL (rather than throwing an error) if there is no such
2552 * pg_attribute entry. All variants return NULL if an attisdropped
2553 * column is selected. These rules are meant to avoid unnecessary
2554 * failures in queries that scan pg_attribute.
2555 */
2556
2557/*
2558 * column_privilege_check: check column privileges, but don't throw an error
2559 * for dropped column or table
2560 *
2561 * Returns 1 if have the privilege, 0 if not, -1 if dropped column/table.
2562 */
2563static int
2565 Oid roleid, AclMode mode)
2566{
2568 bool is_missing = false;
2569
2570 /*
2571 * If convert_column_name failed, we can just return -1 immediately.
2572 */
2574 return -1;
2575
2576 /*
2577 * Check for column-level privileges first. This serves in part as a check
2578 * on whether the column even exists, so we need to do it before checking
2579 * table-level privilege.
2580 */
2581 aclresult = pg_attribute_aclcheck_ext(tableoid, attnum, roleid,
2582 mode, &is_missing);
2583 if (aclresult == ACLCHECK_OK)
2584 return 1;
2585 else if (is_missing)
2586 return -1;
2587
2588 /* Next check if we have the privilege at the table level */
2589 aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2590 if (aclresult == ACLCHECK_OK)
2591 return 1;
2592 else if (is_missing)
2593 return -1;
2594 else
2595 return 0;
2596}
2597
2598/*
2599 * has_column_privilege_name_name_name
2600 * Check user privileges on a column given
2601 * name username, text tablename, text colname, and text priv name.
2602 */
2603Datum
2605{
2606 Name rolename = PG_GETARG_NAME(0);
2607 text *tablename = PG_GETARG_TEXT_PP(1);
2610 Oid roleid;
2611 Oid tableoid;
2613 AclMode mode;
2614 int privresult;
2615
2616 roleid = get_role_oid_or_public(NameStr(*rolename));
2617 tableoid = convert_table_name(tablename);
2620
2621 privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2622 if (privresult < 0)
2625}
2626
2627/*
2628 * has_column_privilege_name_name_attnum
2629 * Check user privileges on a column given
2630 * name username, text tablename, int attnum, and text priv name.
2631 */
2632Datum
2634{
2635 Name rolename = PG_GETARG_NAME(0);
2636 text *tablename = PG_GETARG_TEXT_PP(1);
2639 Oid roleid;
2640 Oid tableoid;
2641 AclMode mode;
2642 int privresult;
2643
2644 roleid = get_role_oid_or_public(NameStr(*rolename));
2645 tableoid = convert_table_name(tablename);
2647
2648 privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2649 if (privresult < 0)
2652}
2653
2654/*
2655 * has_column_privilege_name_id_name
2656 * Check user privileges on a column given
2657 * name username, table oid, text colname, and text priv name.
2658 */
2659Datum
2680
2681/*
2682 * has_column_privilege_name_id_attnum
2683 * Check user privileges on a column given
2684 * name username, table oid, int attnum, and text priv name.
2685 */
2686Datum
2705
2706/*
2707 * has_column_privilege_id_name_name
2708 * Check user privileges on a column given
2709 * oid roleid, text tablename, text colname, and text priv name.
2710 */
2711Datum
2713{
2714 Oid roleid = PG_GETARG_OID(0);
2715 text *tablename = PG_GETARG_TEXT_PP(1);
2718 Oid tableoid;
2720 AclMode mode;
2721 int privresult;
2722
2723 tableoid = convert_table_name(tablename);
2726
2727 privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2728 if (privresult < 0)
2731}
2732
2733/*
2734 * has_column_privilege_id_name_attnum
2735 * Check user privileges on a column given
2736 * oid roleid, text tablename, int attnum, and text priv name.
2737 */
2738Datum
2740{
2741 Oid roleid = PG_GETARG_OID(0);
2742 text *tablename = PG_GETARG_TEXT_PP(1);
2745 Oid tableoid;
2746 AclMode mode;
2747 int privresult;
2748
2749 tableoid = convert_table_name(tablename);
2751
2752 privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2753 if (privresult < 0)
2756}
2757
2758/*
2759 * has_column_privilege_id_id_name
2760 * Check user privileges on a column given
2761 * oid roleid, table oid, text colname, and text priv name.
2762 */
2763Datum
2782
2783/*
2784 * has_column_privilege_id_id_attnum
2785 * Check user privileges on a column given
2786 * oid roleid, table oid, int attnum, and text priv name.
2787 */
2788Datum
2805
2806/*
2807 * has_column_privilege_name_name
2808 * Check user privileges on a column given
2809 * text tablename, text colname, and text priv name.
2810 * current_user is assumed
2811 */
2812Datum
2814{
2815 text *tablename = PG_GETARG_TEXT_PP(0);
2818 Oid roleid;
2819 Oid tableoid;
2821 AclMode mode;
2822 int privresult;
2823
2824 roleid = GetUserId();
2825 tableoid = convert_table_name(tablename);
2828
2829 privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2830 if (privresult < 0)
2833}
2834
2835/*
2836 * has_column_privilege_name_attnum
2837 * Check user privileges on a column given
2838 * text tablename, int attnum, and text priv name.
2839 * current_user is assumed
2840 */
2841Datum
2843{
2844 text *tablename = PG_GETARG_TEXT_PP(0);
2847 Oid roleid;
2848 Oid tableoid;
2849 AclMode mode;
2850 int privresult;
2851
2852 roleid = GetUserId();
2853 tableoid = convert_table_name(tablename);
2855
2856 privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2857 if (privresult < 0)
2860}
2861
2862/*
2863 * has_column_privilege_id_name
2864 * Check user privileges on a column given
2865 * table oid, text colname, and text priv name.
2866 * current_user is assumed
2867 */
2868Datum
2870{
2871 Oid tableoid = PG_GETARG_OID(0);
2874 Oid roleid;
2876 AclMode mode;
2877 int privresult;
2878
2879 roleid = GetUserId();
2882
2883 privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2884 if (privresult < 0)
2887}
2888
2889/*
2890 * has_column_privilege_id_attnum
2891 * Check user privileges on a column given
2892 * table oid, int attnum, and text priv name.
2893 * current_user is assumed
2894 */
2895Datum
2897{
2898 Oid tableoid = PG_GETARG_OID(0);
2901 Oid roleid;
2902 AclMode mode;
2903 int privresult;
2904
2905 roleid = GetUserId();
2907
2908 privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2909 if (privresult < 0)
2912}
2913
2914/*
2915 * Support routines for has_column_privilege family.
2916 */
2917
2918/*
2919 * Given a table OID and a column name expressed as a string, look it up
2920 * and return the column number. Returns InvalidAttrNumber in cases
2921 * where caller should return NULL instead of failing.
2922 */
2923static AttrNumber
2925{
2926 char *colname;
2929
2930 colname = text_to_cstring(column);
2931
2932 /*
2933 * We don't use get_attnum() here because it will report that dropped
2934 * columns don't exist. We need to treat dropped columns differently from
2935 * nonexistent columns.
2936 */
2938 ObjectIdGetDatum(tableoid),
2939 CStringGetDatum(colname));
2941 {
2943
2945 /* We want to return NULL for dropped columns */
2946 if (attributeForm->attisdropped)
2948 else
2949 attnum = attributeForm->attnum;
2951 }
2952 else
2953 {
2954 char *tablename = get_rel_name(tableoid);
2955
2956 /*
2957 * If the table OID is bogus, or it's just been dropped, we'll get
2958 * NULL back. In such cases we want has_column_privilege to return
2959 * NULL too, so just return InvalidAttrNumber.
2960 */
2961 if (tablename != NULL)
2962 {
2963 /* tableoid exists, colname does not, so throw error */
2964 ereport(ERROR,
2966 errmsg("column \"%s\" of relation \"%s\" does not exist",
2967 colname, tablename)));
2968 }
2969 /* tableoid doesn't exist, so act like attisdropped case */
2971 }
2972
2973 pfree(colname);
2974 return attnum;
2975}
2976
2977/*
2978 * convert_column_priv_string
2979 * Convert text string to AclMode value.
2980 */
2981static AclMode
2983{
2984 static const priv_map column_priv_map[] = {
2985 {"SELECT", ACL_SELECT},
2986 {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
2987 {"INSERT", ACL_INSERT},
2988 {"INSERT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_INSERT)},
2989 {"UPDATE", ACL_UPDATE},
2990 {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
2991 {"REFERENCES", ACL_REFERENCES},
2992 {"REFERENCES WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_REFERENCES)},
2993 {NULL, 0}
2994 };
2995
2997}
2998
2999
3000/*
3001 * has_database_privilege variants
3002 * These are all named "has_database_privilege" at the SQL level.
3003 * They take various combinations of database name, database OID,
3004 * user name, user OID, or implicit user = current_user.
3005 *
3006 * The result is a boolean value: true if user has the indicated
3007 * privilege, false if not, or NULL if object doesn't exist.
3008 */
3009
3010/*
3011 * has_database_privilege_name_name
3012 * Check user privileges on a database given
3013 * name username, text databasename, and text priv name.
3014 */
3015Datum
3034
3035/*
3036 * has_database_privilege_name
3037 * Check user privileges on a database given
3038 * text databasename and text priv name.
3039 * current_user is assumed
3040 */
3041Datum
3059
3060/*
3061 * has_database_privilege_name_id
3062 * Check user privileges on a database given
3063 * name usename, database oid, and text priv name.
3064 */
3065Datum
3088
3089/*
3090 * has_database_privilege_id
3091 * Check user privileges on a database given
3092 * database oid, and text priv name.
3093 * current_user is assumed
3094 */
3095Datum
3117
3118/*
3119 * has_database_privilege_id_name
3120 * Check user privileges on a database given
3121 * roleid, text databasename, and text priv name.
3122 */
3123Datum
3140
3141/*
3142 * has_database_privilege_id_id
3143 * Check user privileges on a database given
3144 * roleid, database oid, and text priv name.
3145 */
3146Datum
3167
3168/*
3169 * Support routines for has_database_privilege family.
3170 */
3171
3172/*
3173 * Given a database name expressed as a string, look it up and return Oid
3174 */
3175static Oid
3182
3183/*
3184 * convert_database_priv_string
3185 * Convert text string to AclMode value.
3186 */
3187static AclMode
3189{
3190 static const priv_map database_priv_map[] = {
3191 {"CREATE", ACL_CREATE},
3192 {"CREATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
3193 {"TEMPORARY", ACL_CREATE_TEMP},
3194 {"TEMPORARY WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE_TEMP)},
3195 {"TEMP", ACL_CREATE_TEMP},
3196 {"TEMP WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE_TEMP)},
3197 {"CONNECT", ACL_CONNECT},
3198 {"CONNECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CONNECT)},
3199 {NULL, 0}
3200 };
3201
3203}
3204
3205
3206/*
3207 * has_foreign_data_wrapper_privilege variants
3208 * These are all named "has_foreign_data_wrapper_privilege" at the SQL level.
3209 * They take various combinations of foreign-data wrapper name,
3210 * fdw OID, user name, user OID, or implicit user = current_user.
3211 *
3212 * The result is a boolean value: true if user has the indicated
3213 * privilege, false if not.
3214 */
3215
3216/*
3217 * has_foreign_data_wrapper_privilege_name_name
3218 * Check user privileges on a foreign-data wrapper given
3219 * name username, text fdwname, and text priv name.
3220 */
3221Datum
3240
3241/*
3242 * has_foreign_data_wrapper_privilege_name
3243 * Check user privileges on a foreign-data wrapper given
3244 * text fdwname and text priv name.
3245 * current_user is assumed
3246 */
3247Datum
3265
3266/*
3267 * has_foreign_data_wrapper_privilege_name_id
3268 * Check user privileges on a foreign-data wrapper given
3269 * name usename, foreign-data wrapper oid, and text priv name.
3270 */
3271Datum
3294
3295/*
3296 * has_foreign_data_wrapper_privilege_id
3297 * Check user privileges on a foreign-data wrapper given
3298 * foreign-data wrapper oid, and text priv name.
3299 * current_user is assumed
3300 */
3301Datum
3323
3324/*
3325 * has_foreign_data_wrapper_privilege_id_name
3326 * Check user privileges on a foreign-data wrapper given
3327 * roleid, text fdwname, and text priv name.
3328 */
3329Datum
3346
3347/*
3348 * has_foreign_data_wrapper_privilege_id_id
3349 * Check user privileges on a foreign-data wrapper given
3350 * roleid, fdw oid, and text priv name.
3351 */
3352Datum
3373
3374/*
3375 * Support routines for has_foreign_data_wrapper_privilege family.
3376 */
3377
3378/*
3379 * Given a FDW name expressed as a string, look it up and return Oid
3380 */
3381static Oid
3383{
3384 char *fdwstr = text_to_cstring(fdwname);
3385
3386 return get_foreign_data_wrapper_oid(fdwstr, false);
3387}
3388
3389/*
3390 * convert_foreign_data_wrapper_priv_string
3391 * Convert text string to AclMode value.
3392 */
3393static AclMode
3395{
3396 static const priv_map foreign_data_wrapper_priv_map[] = {
3397 {"USAGE", ACL_USAGE},
3398 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
3399 {NULL, 0}
3400 };
3401
3403}
3404
3405
3406/*
3407 * has_function_privilege variants
3408 * These are all named "has_function_privilege" at the SQL level.
3409 * They take various combinations of function name, function OID,
3410 * user name, user OID, or implicit user = current_user.
3411 *
3412 * The result is a boolean value: true if user has the indicated
3413 * privilege, false if not, or NULL if object doesn't exist.
3414 */
3415
3416/*
3417 * has_function_privilege_name_name
3418 * Check user privileges on a function given
3419 * name username, text functionname, and text priv name.
3420 */
3421Datum
3440
3441/*
3442 * has_function_privilege_name
3443 * Check user privileges on a function given
3444 * text functionname and text priv name.
3445 * current_user is assumed
3446 */
3447Datum
3465
3466/*
3467 * has_function_privilege_name_id
3468 * Check user privileges on a function given
3469 * name usename, function oid, and text priv name.
3470 */
3471Datum
3494
3495/*
3496 * has_function_privilege_id
3497 * Check user privileges on a function given
3498 * function oid, and text priv name.
3499 * current_user is assumed
3500 */
3501Datum
3523
3524/*
3525 * has_function_privilege_id_name
3526 * Check user privileges on a function given
3527 * roleid, text functionname, and text priv name.
3528 */
3529Datum
3546
3547/*
3548 * has_function_privilege_id_id
3549 * Check user privileges on a function given
3550 * roleid, function oid, and text priv name.
3551 */
3552Datum
3573
3574/*
3575 * Support routines for has_function_privilege family.
3576 */
3577
3578/*
3579 * Given a function name expressed as a string, look it up and return Oid
3580 */
3581static Oid
3583{
3585 Oid oid;
3586
3589
3590 if (!OidIsValid(oid))
3591 ereport(ERROR,
3593 errmsg("function \"%s\" does not exist", funcname)));
3594
3595 return oid;
3596}
3597
3598/*
3599 * convert_function_priv_string
3600 * Convert text string to AclMode value.
3601 */
3602static AclMode
3604{
3605 static const priv_map function_priv_map[] = {
3606 {"EXECUTE", ACL_EXECUTE},
3607 {"EXECUTE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_EXECUTE)},
3608 {NULL, 0}
3609 };
3610
3612}
3613
3614
3615/*
3616 * has_language_privilege variants
3617 * These are all named "has_language_privilege" at the SQL level.
3618 * They take various combinations of language name, language OID,
3619 * user name, user OID, or implicit user = current_user.
3620 *
3621 * The result is a boolean value: true if user has the indicated
3622 * privilege, false if not, or NULL if object doesn't exist.
3623 */
3624
3625/*
3626 * has_language_privilege_name_name
3627 * Check user privileges on a language given
3628 * name username, text languagename, and text priv name.
3629 */
3630Datum
3649
3650/*
3651 * has_language_privilege_name
3652 * Check user privileges on a language given
3653 * text languagename and text priv name.
3654 * current_user is assumed
3655 */
3656Datum
3674
3675/*
3676 * has_language_privilege_name_id
3677 * Check user privileges on a language given
3678 * name usename, language oid, and text priv name.
3679 */
3680Datum
3703
3704/*
3705 * has_language_privilege_id
3706 * Check user privileges on a language given
3707 * language oid, and text priv name.
3708 * current_user is assumed
3709 */
3710Datum
3732
3733/*
3734 * has_language_privilege_id_name
3735 * Check user privileges on a language given
3736 * roleid, text languagename, and text priv name.
3737 */
3738Datum
3755
3756/*
3757 * has_language_privilege_id_id
3758 * Check user privileges on a language given
3759 * roleid, language oid, and text priv name.
3760 */
3761Datum
3782
3783/*
3784 * Support routines for has_language_privilege family.
3785 */
3786
3787/*
3788 * Given a language name expressed as a string, look it up and return Oid
3789 */
3790static Oid
3792{
3793 char *langname = text_to_cstring(languagename);
3794
3795 return get_language_oid(langname, false);
3796}
3797
3798/*
3799 * convert_language_priv_string
3800 * Convert text string to AclMode value.
3801 */
3802static AclMode
3804{
3805 static const priv_map language_priv_map[] = {
3806 {"USAGE", ACL_USAGE},
3807 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
3808 {NULL, 0}
3809 };
3810
3812}
3813
3814
3815/*
3816 * has_schema_privilege variants
3817 * These are all named "has_schema_privilege" at the SQL level.
3818 * They take various combinations of schema name, schema OID,
3819 * user name, user OID, or implicit user = current_user.
3820 *
3821 * The result is a boolean value: true if user has the indicated
3822 * privilege, false if not, or NULL if object doesn't exist.
3823 */
3824
3825/*
3826 * has_schema_privilege_name_name
3827 * Check user privileges on a schema given
3828 * name username, text schemaname, and text priv name.
3829 */
3830Datum
3849
3850/*
3851 * has_schema_privilege_name
3852 * Check user privileges on a schema given
3853 * text schemaname and text priv name.
3854 * current_user is assumed
3855 */
3856Datum
3874
3875/*
3876 * has_schema_privilege_name_id
3877 * Check user privileges on a schema given
3878 * name usename, schema oid, and text priv name.
3879 */
3880Datum
3903
3904/*
3905 * has_schema_privilege_id
3906 * Check user privileges on a schema given
3907 * schema oid, and text priv name.
3908 * current_user is assumed
3909 */
3910Datum
3932
3933/*
3934 * has_schema_privilege_id_name
3935 * Check user privileges on a schema given
3936 * roleid, text schemaname, and text priv name.
3937 */
3938Datum
3955
3956/*
3957 * has_schema_privilege_id_id
3958 * Check user privileges on a schema given
3959 * roleid, schema oid, and text priv name.
3960 */
3961Datum
3982
3983/*
3984 * Support routines for has_schema_privilege family.
3985 */
3986
3987/*
3988 * Given a schema name expressed as a string, look it up and return Oid
3989 */
3990static Oid
3992{
3993 char *nspname = text_to_cstring(schemaname);
3994
3995 return get_namespace_oid(nspname, false);
3996}
3997
3998/*
3999 * convert_schema_priv_string
4000 * Convert text string to AclMode value.
4001 */
4002static AclMode
4004{
4005 static const priv_map schema_priv_map[] = {
4006 {"CREATE", ACL_CREATE},
4007 {"CREATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
4008 {"USAGE", ACL_USAGE},
4009 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
4010 {NULL, 0}
4011 };
4012
4014}
4015
4016
4017/*
4018 * has_server_privilege variants
4019 * These are all named "has_server_privilege" at the SQL level.
4020 * They take various combinations of foreign server name,
4021 * server OID, user name, user OID, or implicit user = current_user.
4022 *
4023 * The result is a boolean value: true if user has the indicated
4024 * privilege, false if not.
4025 */
4026
4027/*
4028 * has_server_privilege_name_name
4029 * Check user privileges on a foreign server given
4030 * name username, text servername, and text priv name.
4031 */
4032Datum
4034{
4036 text *servername = PG_GETARG_TEXT_PP(1);
4038 Oid roleid;
4039 Oid serverid;
4040 AclMode mode;
4042
4044 serverid = convert_server_name(servername);
4046
4048
4050}
4051
4052/*
4053 * has_server_privilege_name
4054 * Check user privileges on a foreign server given
4055 * text servername and text priv name.
4056 * current_user is assumed
4057 */
4058Datum
4060{
4061 text *servername = PG_GETARG_TEXT_PP(0);
4063 Oid roleid;
4064 Oid serverid;
4065 AclMode mode;
4067
4068 roleid = GetUserId();
4069 serverid = convert_server_name(servername);
4071
4073
4075}
4076
4077/*
4078 * has_server_privilege_name_id
4079 * Check user privileges on a foreign server given
4080 * name usename, foreign server oid, and text priv name.
4081 */
4082Datum
4105
4106/*
4107 * has_server_privilege_id
4108 * Check user privileges on a foreign server given
4109 * server oid, and text priv name.
4110 * current_user is assumed
4111 */
4112Datum
4114{
4115 Oid serverid = PG_GETARG_OID(0);
4117 Oid roleid;
4118 AclMode mode;
4120 bool is_missing = false;
4121
4122 roleid = GetUserId();
4124
4126 roleid, mode,
4127 &is_missing);
4128
4129 if (is_missing)
4131
4133}
4134
4135/*
4136 * has_server_privilege_id_name
4137 * Check user privileges on a foreign server given
4138 * roleid, text servername, and text priv name.
4139 */
4140Datum
4142{
4143 Oid roleid = PG_GETARG_OID(0);
4144 text *servername = PG_GETARG_TEXT_PP(1);
4146 Oid serverid;
4147 AclMode mode;
4149
4150 serverid = convert_server_name(servername);
4152
4154
4156}
4157
4158/*
4159 * has_server_privilege_id_id
4160 * Check user privileges on a foreign server given
4161 * roleid, server oid, and text priv name.
4162 */
4163Datum
4165{
4166 Oid roleid = PG_GETARG_OID(0);
4167 Oid serverid = PG_GETARG_OID(1);
4169 AclMode mode;
4171 bool is_missing = false;
4172
4174
4176 roleid, mode,
4177 &is_missing);
4178
4179 if (is_missing)
4181
4183}
4184
4185/*
4186 * Support routines for has_server_privilege family.
4187 */
4188
4189/*
4190 * Given a server name expressed as a string, look it up and return Oid
4191 */
4192static Oid
4194{
4195 char *serverstr = text_to_cstring(servername);
4196
4197 return get_foreign_server_oid(serverstr, false);
4198}
4199
4200/*
4201 * convert_server_priv_string
4202 * Convert text string to AclMode value.
4203 */
4204static AclMode
4206{
4207 static const priv_map server_priv_map[] = {
4208 {"USAGE", ACL_USAGE},
4209 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
4210 {NULL, 0}
4211 };
4212
4214}
4215
4216
4217/*
4218 * has_tablespace_privilege variants
4219 * These are all named "has_tablespace_privilege" at the SQL level.
4220 * They take various combinations of tablespace name, tablespace OID,
4221 * user name, user OID, or implicit user = current_user.
4222 *
4223 * The result is a boolean value: true if user has the indicated
4224 * privilege, false if not.
4225 */
4226
4227/*
4228 * has_tablespace_privilege_name_name
4229 * Check user privileges on a tablespace given
4230 * name username, text tablespacename, and text priv name.
4231 */
4232Datum
4251
4252/*
4253 * has_tablespace_privilege_name
4254 * Check user privileges on a tablespace given
4255 * text tablespacename and text priv name.
4256 * current_user is assumed
4257 */
4258Datum
4276
4277/*
4278 * has_tablespace_privilege_name_id
4279 * Check user privileges on a tablespace given
4280 * name usename, tablespace oid, and text priv name.
4281 */
4282Datum
4305
4306/*
4307 * has_tablespace_privilege_id
4308 * Check user privileges on a tablespace given
4309 * tablespace oid, and text priv name.
4310 * current_user is assumed
4311 */
4312Datum
4334
4335/*
4336 * has_tablespace_privilege_id_name
4337 * Check user privileges on a tablespace given
4338 * roleid, text tablespacename, and text priv name.
4339 */
4340Datum
4357
4358/*
4359 * has_tablespace_privilege_id_id
4360 * Check user privileges on a tablespace given
4361 * roleid, tablespace oid, and text priv name.
4362 */
4363Datum
4384
4385/*
4386 * Support routines for has_tablespace_privilege family.
4387 */
4388
4389/*
4390 * Given a tablespace name expressed as a string, look it up and return Oid
4391 */
4392static Oid
4394{
4395 char *spcname = text_to_cstring(tablespacename);
4396
4397 return get_tablespace_oid(spcname, false);
4398}
4399
4400/*
4401 * convert_tablespace_priv_string
4402 * Convert text string to AclMode value.
4403 */
4404static AclMode
4406{
4407 static const priv_map tablespace_priv_map[] = {
4408 {"CREATE", ACL_CREATE},
4409 {"CREATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
4410 {NULL, 0}
4411 };
4412
4414}
4415
4416/*
4417 * has_type_privilege variants
4418 * These are all named "has_type_privilege" at the SQL level.
4419 * They take various combinations of type name, type OID,
4420 * user name, user OID, or implicit user = current_user.
4421 *
4422 * The result is a boolean value: true if user has the indicated
4423 * privilege, false if not, or NULL if object doesn't exist.
4424 */
4425
4426/*
4427 * has_type_privilege_name_name
4428 * Check user privileges on a type given
4429 * name username, text typename, and text priv name.
4430 */
4431Datum
4433{
4435 text *typename = PG_GETARG_TEXT_PP(1);
4437 Oid roleid;
4438 Oid typeoid;
4439 AclMode mode;
4441
4443 typeoid = convert_type_name(typename);
4445
4446 aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
4447
4449}
4450
4451/*
4452 * has_type_privilege_name
4453 * Check user privileges on a type given
4454 * text typename and text priv name.
4455 * current_user is assumed
4456 */
4457Datum
4459{
4460 text *typename = PG_GETARG_TEXT_PP(0);
4462 Oid roleid;
4463 Oid typeoid;
4464 AclMode mode;
4466
4467 roleid = GetUserId();
4468 typeoid = convert_type_name(typename);
4470
4471 aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
4472
4474}
4475
4476/*
4477 * has_type_privilege_name_id
4478 * Check user privileges on a type given
4479 * name usename, type oid, and text priv name.
4480 */
4481Datum
4483{
4485 Oid typeoid = PG_GETARG_OID(1);
4487 Oid roleid;
4488 AclMode mode;
4490 bool is_missing = false;
4491
4494
4496 roleid, mode,
4497 &is_missing);
4498
4499 if (is_missing)
4501
4503}
4504
4505/*
4506 * has_type_privilege_id
4507 * Check user privileges on a type given
4508 * type oid, and text priv name.
4509 * current_user is assumed
4510 */
4511Datum
4513{
4514 Oid typeoid = PG_GETARG_OID(0);
4516 Oid roleid;
4517 AclMode mode;
4519 bool is_missing = false;
4520
4521 roleid = GetUserId();
4523
4525 roleid, mode,
4526 &is_missing);
4527
4528 if (is_missing)
4530
4532}
4533
4534/*
4535 * has_type_privilege_id_name
4536 * Check user privileges on a type given
4537 * roleid, text typename, and text priv name.
4538 */
4539Datum
4541{
4542 Oid roleid = PG_GETARG_OID(0);
4543 text *typename = PG_GETARG_TEXT_PP(1);
4545 Oid typeoid;
4546 AclMode mode;
4548
4549 typeoid = convert_type_name(typename);
4551
4552 aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
4553
4555}
4556
4557/*
4558 * has_type_privilege_id_id
4559 * Check user privileges on a type given
4560 * roleid, type oid, and text priv name.
4561 */
4562Datum
4564{
4565 Oid roleid = PG_GETARG_OID(0);
4566 Oid typeoid = PG_GETARG_OID(1);
4568 AclMode mode;
4570 bool is_missing = false;
4571
4573
4575 roleid, mode,
4576 &is_missing);
4577
4578 if (is_missing)
4580
4582}
4583
4584/*
4585 * Support routines for has_type_privilege family.
4586 */
4587
4588/*
4589 * Given a type name expressed as a string, look it up and return Oid
4590 */
4591static Oid
4593{
4594 char *typname = text_to_cstring(typename);
4595 Oid oid;
4596
4599
4600 if (!OidIsValid(oid))
4601 ereport(ERROR,
4603 errmsg("type \"%s\" does not exist", typname)));
4604
4605 return oid;
4606}
4607
4608/*
4609 * convert_type_priv_string
4610 * Convert text string to AclMode value.
4611 */
4612static AclMode
4614{
4615 static const priv_map type_priv_map[] = {
4616 {"USAGE", ACL_USAGE},
4617 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
4618 {NULL, 0}
4619 };
4620
4622}
4623
4624/*
4625 * has_parameter_privilege variants
4626 * These are all named "has_parameter_privilege" at the SQL level.
4627 * They take various combinations of parameter name with
4628 * user name, user OID, or implicit user = current_user.
4629 *
4630 * The result is a boolean value: true if user has been granted
4631 * the indicated privilege or false if not.
4632 */
4633
4634/*
4635 * has_param_priv_byname
4636 *
4637 * Helper function to check user privileges on a parameter given the
4638 * role by Oid, parameter by text name, and privileges as AclMode.
4639 */
4640static bool
4642{
4644
4645 return pg_parameter_aclcheck(paramstr, roleid, priv) == ACLCHECK_OK;
4646}
4647
4648/*
4649 * has_parameter_privilege_name_name
4650 * Check user privileges on a parameter given name username, text
4651 * parameter, and text priv name.
4652 */
4653Datum
4663
4664/*
4665 * has_parameter_privilege_name
4666 * Check user privileges on a parameter given text parameter and text priv
4667 * name. current_user is assumed
4668 */
4669Datum
4677
4678/*
4679 * has_parameter_privilege_id_name
4680 * Check user privileges on a parameter given roleid, text parameter, and
4681 * text priv name.
4682 */
4683Datum
4692
4693/*
4694 * Support routines for has_parameter_privilege family.
4695 */
4696
4697/*
4698 * convert_parameter_priv_string
4699 * Convert text string to AclMode value.
4700 */
4701static AclMode
4703{
4704 static const priv_map parameter_priv_map[] = {
4705 {"SET", ACL_SET},
4706 {"SET WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SET)},
4707 {"ALTER SYSTEM", ACL_ALTER_SYSTEM},
4708 {"ALTER SYSTEM WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_ALTER_SYSTEM)},
4709 {NULL, 0}
4710 };
4711
4713}
4714
4715/*
4716 * has_largeobject_privilege variants
4717 * These are all named "has_largeobject_privilege" at the SQL level.
4718 * They take various combinations of large object OID with
4719 * user name, user OID, or implicit user = current_user.
4720 *
4721 * The result is a boolean value: true if user has the indicated
4722 * privilege, false if not, or NULL if object doesn't exist.
4723 */
4724
4725/*
4726 * has_lo_priv_byid
4727 *
4728 * Helper function to check user privileges on a large object given the
4729 * role by Oid, large object by Oid, and privileges as AclMode.
4730 */
4731static bool
4733{
4734 Snapshot snapshot = NULL;
4736
4737 if (priv & ACL_UPDATE)
4738 snapshot = NULL;
4739 else
4740 snapshot = GetActiveSnapshot();
4741
4742 if (!LargeObjectExistsWithSnapshot(lobjId, snapshot))
4743 {
4745 *is_missing = true;
4746 return false;
4747 }
4748
4750 return true;
4751
4753 roleid,
4754 priv,
4755 snapshot);
4756 return aclresult == ACLCHECK_OK;
4757}
4758
4759/*
4760 * has_largeobject_privilege_name_id
4761 * Check user privileges on a large object given
4762 * name username, large object oid, and text priv name.
4763 */
4764Datum
4766{
4771 AclMode mode;
4772 bool is_missing = false;
4773 bool result;
4774
4776 result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
4777
4778 if (is_missing)
4780
4781 PG_RETURN_BOOL(result);
4782}
4783
4784/*
4785 * has_largeobject_privilege_id
4786 * Check user privileges on a large object given
4787 * large object oid, and text priv name.
4788 * current_user is assumed
4789 */
4790Datum
4792{
4794 Oid roleid = GetUserId();
4796 AclMode mode;
4797 bool is_missing = false;
4798 bool result;
4799
4801 result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
4802
4803 if (is_missing)
4805
4806 PG_RETURN_BOOL(result);
4807}
4808
4809/*
4810 * has_largeobject_privilege_id_id
4811 * Check user privileges on a large object given
4812 * roleid, large object oid, and text priv name.
4813 */
4814Datum
4816{
4817 Oid roleid = PG_GETARG_OID(0);
4820 AclMode mode;
4821 bool is_missing = false;
4822 bool result;
4823
4825 result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
4826
4827 if (is_missing)
4829
4830 PG_RETURN_BOOL(result);
4831}
4832
4833/*
4834 * convert_largeobject_priv_string
4835 * Convert text string to AclMode value.
4836 */
4837static AclMode
4839{
4840 static const priv_map largeobject_priv_map[] = {
4841 {"SELECT", ACL_SELECT},
4842 {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
4843 {"UPDATE", ACL_UPDATE},
4844 {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
4845 {NULL, 0}
4846 };
4847
4849}
4850
4851/*
4852 * pg_has_role variants
4853 * These are all named "pg_has_role" at the SQL level.
4854 * They take various combinations of role name, role OID,
4855 * user name, user OID, or implicit user = current_user.
4856 *
4857 * The result is a boolean value: true if user has the indicated
4858 * privilege, false if not.
4859 */
4860
4861/*
4862 * pg_has_role_name_name
4863 * Check user privileges on a role given
4864 * name username, name rolename, and text priv name.
4865 */
4866Datum
4868{
4870 Name rolename = PG_GETARG_NAME(1);
4872 Oid roleid;
4873 Oid roleoid;
4874 AclMode mode;
4876
4877 roleid = get_role_oid(NameStr(*username), false);
4878 roleoid = get_role_oid(NameStr(*rolename), false);
4880
4881 aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4882
4884}
4885
4886/*
4887 * pg_has_role_name
4888 * Check user privileges on a role given
4889 * name rolename and text priv name.
4890 * current_user is assumed
4891 */
4892Datum
4894{
4895 Name rolename = PG_GETARG_NAME(0);
4897 Oid roleid;
4898 Oid roleoid;
4899 AclMode mode;
4901
4902 roleid = GetUserId();
4903 roleoid = get_role_oid(NameStr(*rolename), false);
4905
4906 aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4907
4909}
4910
4911/*
4912 * pg_has_role_name_id
4913 * Check user privileges on a role given
4914 * name usename, role oid, and text priv name.
4915 */
4916Datum
4918{
4920 Oid roleoid = PG_GETARG_OID(1);
4922 Oid roleid;
4923 AclMode mode;
4925
4926 roleid = get_role_oid(NameStr(*username), false);
4928
4929 aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4930
4932}
4933
4934/*
4935 * pg_has_role_id
4936 * Check user privileges on a role given
4937 * role oid, and text priv name.
4938 * current_user is assumed
4939 */
4940Datum
4942{
4943 Oid roleoid = PG_GETARG_OID(0);
4945 Oid roleid;
4946 AclMode mode;
4948
4949 roleid = GetUserId();
4951
4952 aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4953
4955}
4956
4957/*
4958 * pg_has_role_id_name
4959 * Check user privileges on a role given
4960 * roleid, name rolename, and text priv name.
4961 */
4962Datum
4964{
4965 Oid roleid = PG_GETARG_OID(0);
4966 Name rolename = PG_GETARG_NAME(1);
4968 Oid roleoid;
4969 AclMode mode;
4971
4972 roleoid = get_role_oid(NameStr(*rolename), false);
4974
4975 aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4976
4978}
4979
4980/*
4981 * pg_has_role_id_id
4982 * Check user privileges on a role given
4983 * roleid, role oid, and text priv name.
4984 */
4985Datum
5000
5001/*
5002 * Support routines for pg_has_role family.
5003 */
5004
5005/*
5006 * convert_role_priv_string
5007 * Convert text string to AclMode value.
5008 *
5009 * We use USAGE to denote whether the privileges of the role are accessible
5010 * (has_privs_of_role), MEMBER to denote is_member, and MEMBER WITH GRANT
5011 * (or ADMIN) OPTION to denote is_admin. There is no ACL bit corresponding
5012 * to MEMBER so we cheat and use ACL_CREATE for that. This convention
5013 * is shared only with pg_role_aclcheck, below.
5014 */
5015static AclMode
5017{
5018 static const priv_map role_priv_map[] = {
5019 {"USAGE", ACL_USAGE},
5020 {"MEMBER", ACL_CREATE},
5021 {"SET", ACL_SET},
5022 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5023 {"USAGE WITH ADMIN OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5024 {"MEMBER WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5025 {"MEMBER WITH ADMIN OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5026 {"SET WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5027 {"SET WITH ADMIN OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
5028 {NULL, 0}
5029 };
5030
5032}
5033
5034/*
5035 * pg_role_aclcheck
5036 * Quick-and-dirty support for pg_has_role
5037 */
5038static AclResult
5040{
5042 {
5043 if (is_admin_of_role(roleid, role_oid))
5044 return ACLCHECK_OK;
5045 }
5046 if (mode & ACL_CREATE)
5047 {
5048 if (is_member_of_role(roleid, role_oid))
5049 return ACLCHECK_OK;
5050 }
5051 if (mode & ACL_USAGE)
5052 {
5053 if (has_privs_of_role(roleid, role_oid))
5054 return ACLCHECK_OK;
5055 }
5056 if (mode & ACL_SET)
5057 {
5058 if (member_can_set_role(roleid, role_oid))
5059 return ACLCHECK_OK;
5060 }
5061 return ACLCHECK_NO_PRIV;
5062}
5063
5064
5065/*
5066 * initialization function (called by InitPostgres)
5067 */
5068void
5070{
5072 {
5076
5077 /*
5078 * In normal mode, set a callback on any syscache invalidation of rows
5079 * of pg_auth_members (for roles_is_member_of()) pg_database (for
5080 * roles_is_member_of())
5081 */
5084 (Datum) 0);
5087 (Datum) 0);
5090 (Datum) 0);
5091 }
5092}
5093
5094/*
5095 * RoleMembershipCacheCallback
5096 * Syscache inval callback function
5097 */
5098static void
5100 uint32 hashvalue)
5101{
5102 if (cacheid == DATABASEOID &&
5103 hashvalue != cached_db_hash &&
5104 hashvalue != 0)
5105 {
5106 return; /* ignore pg_database changes for other DBs */
5107 }
5108
5109 /* Force membership caches to be recomputed on next use */
5113}
5114
5115/*
5116 * A helper function for roles_is_member_of() that provides an optimized
5117 * implementation of list_append_unique_oid() via a Bloom filter. The caller
5118 * (i.e., roles_is_member_of()) is responsible for freeing bf once it is done
5119 * using this function.
5120 */
5121static inline List *
5123{
5124 unsigned char *roleptr = (unsigned char *) &role;
5125
5126 /*
5127 * If there is a previously-created Bloom filter, use it to try to
5128 * determine whether the role is missing from the list. If it says yes,
5129 * that's a hard fact and we can go ahead and add the role. If it says
5130 * no, that's only probabilistic and we'd better search the list. Without
5131 * a filter, we must always do an ordinary linear search through the
5132 * existing list.
5133 */
5134 if ((*bf && bloom_lacks_element(*bf, roleptr, sizeof(Oid))) ||
5136 {
5137 /*
5138 * If the list is large, we take on the overhead of creating and
5139 * populating a Bloom filter to speed up future calls to this
5140 * function.
5141 */
5142 if (*bf == NULL &&
5144 {
5146 foreach_oid(roleid, roles_list)
5147 bloom_add_element(*bf, (unsigned char *) &roleid, sizeof(Oid));
5148 }
5149
5150 /*
5151 * Finally, add the role to the list and the Bloom filter, if it
5152 * exists.
5153 */
5155 if (*bf)
5156 bloom_add_element(*bf, roleptr, sizeof(Oid));
5157 }
5158
5159 return roles_list;
5160}
5161
5162/*
5163 * Get a list of roles that the specified roleid is a member of
5164 *
5165 * Type ROLERECURSE_MEMBERS recurses through all grants; ROLERECURSE_PRIVS
5166 * recurses only through inheritable grants; and ROLERECURSE_SETROLE recurses
5167 * only through grants with set_option.
5168 *
5169 * Since indirect membership testing is relatively expensive, we cache
5170 * a list of memberships. Hence, the result is only guaranteed good until
5171 * the next call of roles_is_member_of()!
5172 *
5173 * For the benefit of select_best_grantor, the result is defined to be
5174 * in breadth-first order, ie, closer relationships earlier.
5175 *
5176 * If admin_of is not InvalidOid, this function sets *admin_role, either
5177 * to the OID of the first role in the result list that directly possesses
5178 * ADMIN OPTION on the role corresponding to admin_of, or to InvalidOid if
5179 * there is no such role.
5180 */
5181static List *
5184{
5185 Oid dba;
5187 ListCell *l;
5190 bloom_filter *bf = NULL;
5191
5193 if (admin_role != NULL)
5195
5196 /* If cache is valid and ADMIN OPTION not sought, just return the list */
5197 if (cached_role[type] == roleid && !OidIsValid(admin_of) &&
5199 return cached_roles[type];
5200
5201 /*
5202 * Role expansion happens in a non-database backend when guc.c checks
5203 * ROLE_PG_READ_ALL_SETTINGS for a physical walsender SHOW command. In
5204 * that case, no role gets pg_database_owner.
5205 */
5207 dba = InvalidOid;
5208 else
5209 {
5211
5213 if (!HeapTupleIsValid(dbtup))
5214 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
5217 }
5218
5219 /*
5220 * Find all the roles that roleid is a member of, including multi-level
5221 * recursion. The role itself will always be the first element of the
5222 * resulting list.
5223 *
5224 * Each element of the list is scanned to see if it adds any indirect
5225 * memberships. We can use a single list as both the record of
5226 * already-found memberships and the agenda of roles yet to be scanned.
5227 * This is a bit tricky but works because the foreach() macro doesn't
5228 * fetch the next list element until the bottom of the loop.
5229 */
5230 roles_list = list_make1_oid(roleid);
5231
5232 foreach(l, roles_list)
5233 {
5234 Oid memberid = lfirst_oid(l);
5236 int i;
5237
5238 /* Find roles that memberid is directly a member of */
5241 for (i = 0; i < memlist->n_members; i++)
5242 {
5243 HeapTuple tup = &memlist->members[i]->tuple;
5245 Oid otherid = form->roleid;
5246
5247 /*
5248 * While otherid==InvalidOid shouldn't appear in the catalog, the
5249 * OidIsValid() avoids crashing if that arises.
5250 */
5251 if (otherid == admin_of && form->admin_option &&
5254
5255 /* If we're supposed to ignore non-heritable grants, do so. */
5256 if (type == ROLERECURSE_PRIVS && !form->inherit_option)
5257 continue;
5258
5259 /* If we're supposed to ignore non-SET grants, do so. */
5260 if (type == ROLERECURSE_SETROLE && !form->set_option)
5261 continue;
5262
5263 /*
5264 * Even though there shouldn't be any loops in the membership
5265 * graph, we must test for having already seen this role. It is
5266 * legal for instance to have both A->B and A->C->B.
5267 */
5269 }
5271
5272 /* implement pg_database_owner implicit membership */
5273 if (memberid == dba && OidIsValid(dba))
5276 }
5277
5278 /*
5279 * Free the Bloom filter created by roles_list_append(), if there is one.
5280 */
5281 if (bf)
5282 bloom_free(bf);
5283
5284 /*
5285 * Copy the completed list into TopMemoryContext so it will persist.
5286 */
5291
5292 /*
5293 * Now safe to assign to state variable
5294 */
5295 cached_role[type] = InvalidOid; /* just paranoia */
5298 cached_role[type] = roleid;
5299
5300 /* And now we can return the answer */
5301 return cached_roles[type];
5302}
5303
5304
5305/*
5306 * Does member have the privileges of role (directly or indirectly)?
5307 *
5308 * This is defined not to recurse through grants that are not inherited,
5309 * and only inherited grants confer the associated privileges automatically.
5310 *
5311 * See also member_can_set_role, below.
5312 */
5313bool
5315{
5316 /* Fast path for simple case */
5317 if (member == role)
5318 return true;
5319
5320 /* Superusers have every privilege, so are part of every role */
5321 if (superuser_arg(member))
5322 return true;
5323
5324 /*
5325 * Find all the roles that member has the privileges of, including
5326 * multi-level recursion, then see if target role is any one of them.
5327 */
5329 InvalidOid, NULL),
5330 role);
5331}
5332
5333/*
5334 * Can member use SET ROLE to this role?
5335 *
5336 * There must be a chain of grants from 'member' to 'role' each of which
5337 * permits SET ROLE; that is, each of which has set_option = true.
5338 *
5339 * It doesn't matter whether the grants are inheritable. That's a separate
5340 * question; see has_privs_of_role.
5341 *
5342 * This function should be used to determine whether the session user can
5343 * use SET ROLE to become the target user. We also use it to determine whether
5344 * the session user can change an existing object to be owned by the target
5345 * user, or create new objects owned by the target user.
5346 */
5347bool
5349{
5350 /* Fast path for simple case */
5351 if (member == role)
5352 return true;
5353
5354 /* Superusers have every privilege, so can always SET ROLE */
5355 if (superuser_arg(member))
5356 return true;
5357
5358 /*
5359 * Find all the roles that member can access via SET ROLE, including
5360 * multi-level recursion, then see if target role is any one of them.
5361 */
5363 InvalidOid, NULL),
5364 role);
5365}
5366
5367/*
5368 * Permission violation error unless able to SET ROLE to target role.
5369 */
5370void
5372{
5373 if (!member_can_set_role(member, role))
5374 ereport(ERROR,
5376 errmsg("must be able to SET ROLE \"%s\"",
5377 GetUserNameFromId(role, false))));
5378}
5379
5380/*
5381 * Is member a member of role (directly or indirectly)?
5382 *
5383 * This is defined to recurse through grants whether they are inherited or not.
5384 *
5385 * Do not use this for privilege checking, instead use has_privs_of_role().
5386 * Don't use it for determining whether it's possible to SET ROLE to some
5387 * other role; for that, use member_can_set_role(). And don't use it for
5388 * determining whether it's OK to create an object owned by some other role:
5389 * use member_can_set_role() for that, too.
5390 *
5391 * In short, calling this function is the wrong thing to do nearly everywhere.
5392 */
5393bool
5395{
5396 /* Fast path for simple case */
5397 if (member == role)
5398 return true;
5399
5400 /* Superusers have every privilege, so are part of every role */
5401 if (superuser_arg(member))
5402 return true;
5403
5404 /*
5405 * Find all the roles that member is a member of, including multi-level
5406 * recursion, then see if target role is any one of them.
5407 */
5409 InvalidOid, NULL),
5410 role);
5411}
5412
5413/*
5414 * Is member a member of role, not considering superuserness?
5415 *
5416 * This is identical to is_member_of_role except we ignore superuser
5417 * status.
5418 *
5419 * Do not use this for privilege checking, instead use has_privs_of_role()
5420 */
5421bool
5423{
5424 /* Fast path for simple case */
5425 if (member == role)
5426 return true;
5427
5428 /*
5429 * Find all the roles that member is a member of, including multi-level
5430 * recursion, then see if target role is any one of them.
5431 */
5433 InvalidOid, NULL),
5434 role);
5435}
5436
5437
5438/*
5439 * Is member an admin of role? That is, is member the role itself (subject to
5440 * restrictions below), a member (directly or indirectly) WITH ADMIN OPTION,
5441 * or a superuser?
5442 */
5443bool
5445{
5447
5448 if (superuser_arg(member))
5449 return true;
5450
5451 /* By policy, a role cannot have WITH ADMIN OPTION on itself. */
5452 if (member == role)
5453 return false;
5454
5456 return OidIsValid(admin_role);
5457}
5458
5459/*
5460 * Find a role whose privileges "member" inherits which has ADMIN OPTION
5461 * on "role", ignoring super-userness.
5462 *
5463 * There might be more than one such role; prefer one which involves fewer
5464 * hops. That is, if member has ADMIN OPTION, prefer that over all other
5465 * options; if not, prefer a role from which member inherits more directly
5466 * over more indirect inheritance.
5467 */
5468Oid
5470{
5472
5473 /* By policy, a role cannot have WITH ADMIN OPTION on itself. */
5474 if (member == role)
5475 return InvalidOid;
5476
5478 return admin_role;
5479}
5480
5481/*
5482 * Select the effective grantor ID for a GRANT or REVOKE operation.
5483 *
5484 * If the GRANT/REVOKE has an explicit GRANTED BY clause, we always use
5485 * exactly that role (which may result in granting/revoking no privileges).
5486 * Otherwise, we seek a "best" grantor, starting with the current user.
5487 *
5488 * The grantor must always be either the object owner or some role that has
5489 * been explicitly granted grant options. This ensures that all granted
5490 * privileges appear to flow from the object owner, and there are never
5491 * multiple "original sources" of a privilege. Therefore, if the would-be
5492 * grantor is a member of a role that has the needed grant options, we have
5493 * to do the grant as that role instead.
5494 *
5495 * It is possible that the would-be grantor is a member of several roles
5496 * that have different subsets of the desired grant options, but no one
5497 * role has 'em all. In this case we pick a role with the largest number
5498 * of desired options. Ties are broken in favor of closer ancestors.
5499 *
5500 * grantedBy: the GRANTED BY clause of GRANT/REVOKE, or NULL if none
5501 * privileges: the privileges to be granted/revoked
5502 * acl: the ACL of the object in question
5503 * ownerId: the role owning the object in question
5504 * *grantorId: receives the OID of the role to do the grant as
5505 * *grantOptions: receives grant options actually held by grantorId (maybe 0)
5506 */
5507void
5509 const Acl *acl, Oid ownerId,
5511{
5512 Oid roleId = GetUserId();
5515 int nrights;
5516 ListCell *l;
5517
5518 /*
5519 * If we have GRANTED BY, resolve it and verify current user is allowed to
5520 * specify that role.
5521 */
5522 if (grantedBy)
5523 {
5524 Oid grantor = get_rolespec_oid(grantedBy, false);
5525
5526 if (!has_privs_of_role(roleId, grantor))
5527 ereport(ERROR,
5529 errmsg("must inherit privileges of role \"%s\"",
5530 GetUserNameFromId(grantor, false))));
5531 /* Use exactly that grantor, whether it has privileges or not */
5532 *grantorId = grantor;
5533 *grantOptions = aclmask_direct(acl, grantor, ownerId,
5535 return;
5536 }
5537
5538 /*
5539 * The object owner is always treated as having all grant options, so if
5540 * roleId is the owner it's easy. Also, if roleId is a superuser it's
5541 * easy: superusers are implicitly members of every role, so they act as
5542 * the object owner.
5543 */
5544 if (roleId == ownerId || superuser_arg(roleId))
5545 {
5546 *grantorId = ownerId;
5548 return;
5549 }
5550
5551 /*
5552 * Otherwise we have to do a careful search to see if roleId has the
5553 * privileges of any suitable role. Note: we can hang onto the result of
5554 * roles_is_member_of() throughout this loop, because aclmask_direct()
5555 * doesn't query any role memberships.
5556 */
5558 InvalidOid, NULL);
5559
5560 /* initialize candidate result as default */
5561 *grantorId = roleId;
5563 nrights = 0;
5564
5565 foreach(l, roles_list)
5566 {
5569
5570 otherprivs = aclmask_direct(acl, otherrole, ownerId,
5573 {
5574 /* Found a suitable grantor */
5577 return;
5578 }
5579
5580 /*
5581 * If it has just some of the needed privileges, remember best
5582 * candidate.
5583 */
5585 {
5587
5588 if (nnewrights > nrights)
5589 {
5593 }
5594 }
5595 }
5596}
5597
5598/*
5599 * get_role_oid - Given a role name, look up the role's OID.
5600 *
5601 * If missing_ok is false, throw an error if role name not found. If
5602 * true, just return InvalidOid.
5603 */
5604Oid
5605get_role_oid(const char *rolname, bool missing_ok)
5606{
5607 Oid oid;
5608
5611 if (!OidIsValid(oid) && !missing_ok)
5612 ereport(ERROR,
5614 errmsg("role \"%s\" does not exist", rolname)));
5615 return oid;
5616}
5617
5618/*
5619 * get_role_oid_or_public - As above, but return ACL_ID_PUBLIC if the
5620 * role name is "public".
5621 */
5622Oid
5624{
5625 if (strcmp(rolname, "public") == 0)
5626 return ACL_ID_PUBLIC;
5627
5628 return get_role_oid(rolname, false);
5629}
5630
5631/*
5632 * Given a RoleSpec node, return the OID it corresponds to. If missing_ok is
5633 * true, return InvalidOid if the role does not exist.
5634 *
5635 * PUBLIC is always disallowed here. Routines wanting to handle the PUBLIC
5636 * case must check the case separately.
5637 */
5638Oid
5639get_rolespec_oid(const RoleSpec *role, bool missing_ok)
5640{
5641 Oid oid;
5642
5643 switch (role->roletype)
5644 {
5645 case ROLESPEC_CSTRING:
5646 Assert(role->rolename);
5647 oid = get_role_oid(role->rolename, missing_ok);
5648 break;
5649
5652 oid = GetUserId();
5653 break;
5654
5656 oid = GetSessionUserId();
5657 break;
5658
5659 case ROLESPEC_PUBLIC:
5660 ereport(ERROR,
5662 errmsg("role \"%s\" does not exist", "public")));
5663 oid = InvalidOid; /* make compiler happy */
5664 break;
5665
5666 default:
5667 elog(ERROR, "unexpected role type %d", role->roletype);
5668 }
5669
5670 return oid;
5671}
5672
5673/*
5674 * Given a RoleSpec node, return the pg_authid HeapTuple it corresponds to.
5675 * Caller must ReleaseSysCache when done with the result tuple.
5676 */
5679{
5680 HeapTuple tuple;
5681
5682 switch (role->roletype)
5683 {
5684 case ROLESPEC_CSTRING:
5685 Assert(role->rolename);
5687 if (!HeapTupleIsValid(tuple))
5688 ereport(ERROR,
5690 errmsg("role \"%s\" does not exist", role->rolename)));
5691 break;
5692
5696 if (!HeapTupleIsValid(tuple))
5697 elog(ERROR, "cache lookup failed for role %u", GetUserId());
5698 break;
5699
5702 if (!HeapTupleIsValid(tuple))
5703 elog(ERROR, "cache lookup failed for role %u", GetSessionUserId());
5704 break;
5705
5706 case ROLESPEC_PUBLIC:
5707 ereport(ERROR,
5709 errmsg("role \"%s\" does not exist", "public")));
5710 tuple = NULL; /* make compiler happy */
5711 break;
5712
5713 default:
5714 elog(ERROR, "unexpected role type %d", role->roletype);
5715 }
5716
5717 return tuple;
5718}
5719
5720/*
5721 * Given a RoleSpec, returns a palloc'ed copy of the corresponding role's name.
5722 */
5723char *
5725{
5726 HeapTuple tp;
5728 char *rolename;
5729
5730 tp = get_rolespec_tuple(role);
5732 rolename = pstrdup(NameStr(authForm->rolname));
5733 ReleaseSysCache(tp);
5734
5735 return rolename;
5736}
5737
5738/*
5739 * Given a RoleSpec, throw an error if the name is reserved, using detail_msg,
5740 * if provided (which must be already translated).
5741 *
5742 * If node is NULL, no error is thrown. If detail_msg is NULL then no detail
5743 * message is provided.
5744 */
5745void
5746check_rolespec_name(const RoleSpec *role, const char *detail_msg)
5747{
5748 if (!role)
5749 return;
5750
5751 if (role->roletype != ROLESPEC_CSTRING)
5752 return;
5753
5754 if (IsReservedName(role->rolename))
5755 {
5756 if (detail_msg)
5757 ereport(ERROR,
5759 errmsg("role name \"%s\" is reserved",
5760 role->rolename),
5762 else
5763 ereport(ERROR,
5765 errmsg("role name \"%s\" is reserved",
5766 role->rolename)));
5767 }
5768}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:262
static List * roles_is_member_of(Oid roleid, enum RoleRecurseType type, Oid admin_of, Oid *admin_role)
Definition acl.c:5182
Datum pg_has_role_id_id(PG_FUNCTION_ARGS)
Definition acl.c:4986
Datum has_schema_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:3911
Datum has_largeobject_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:4815
Datum hash_aclitem(PG_FUNCTION_ARGS)
Definition acl.c:792
void initialize_acl(void)
Definition acl.c:5069
Datum has_sequence_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:2165
Datum has_table_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:2050
Datum has_server_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:4113
Datum has_tablespace_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:4364
Datum has_column_privilege_id_id_attnum(PG_FUNCTION_ARGS)
Definition acl.c:2789
Datum has_server_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:4033
Datum aclinsert(PG_FUNCTION_ARGS)
Definition acl.c:1620
static AclMode convert_function_priv_string(text *priv_type_text)
Definition acl.c:3603
Datum has_largeobject_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:4765
static const char * getid(const char *s, char *n, Node *escontext)
Definition acl.c:171
bool is_admin_of_role(Oid member, Oid role)
Definition acl.c:5444
Datum has_column_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:2869
static bool aclitem_match(const AclItem *a1, const AclItem *a2)
Definition acl.c:737
Datum pg_has_role_name_name(PG_FUNCTION_ARGS)
Definition acl.c:4867
Datum has_language_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:3631
Acl * aclconcat(const Acl *left_acl, const Acl *right_acl)
Definition acl.c:491
Datum has_schema_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:3831
Acl * aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId)
Definition acl.c:515
Datum aclitem_eq(PG_FUNCTION_ARGS)
Definition acl.c:772
Datum has_tablespace_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:4283
Datum makeaclitem(PG_FUNCTION_ARGS)
Definition acl.c:1662
Datum has_table_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:2027
Datum has_server_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:4083
Datum has_column_privilege_name_id_attnum(PG_FUNCTION_ARGS)
Definition acl.c:2687
Datum has_language_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:3739
static int column_privilege_check(Oid tableoid, AttrNumber attnum, Oid roleid, AclMode mode)
Definition acl.c:2564
static Oid convert_type_name(text *typename)
Definition acl.c:4592
Oid select_best_admin(Oid member, Oid role)
Definition acl.c:5469
Datum aclexplode(PG_FUNCTION_ARGS)
Definition acl.c:1818
static Oid convert_database_name(text *databasename)
Definition acl.c:3176
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition acl.c:827
Datum aclitemout(PG_FUNCTION_ARGS)
Definition acl.c:664
Oid get_role_oid_or_public(const char *rolname)
Definition acl.c:5623
Datum has_foreign_data_wrapper_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:3272
Datum has_foreign_data_wrapper_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:3353
bool aclequal(const Acl *left_acl, const Acl *right_acl)
Definition acl.c:573
Datum hash_aclitem_extended(PG_FUNCTION_ARGS)
Definition acl.c:806
static void check_acl(const Acl *acl)
Definition acl.c:604
Datum has_parameter_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:4654
Datum has_foreign_data_wrapper_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:3302
#define ROLES_LIST_BLOOM_THRESHOLD
Definition acl.c:92
Datum has_database_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:3042
static uint32 cached_db_hash
Definition acl.c:84
Datum has_type_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:4432
Datum has_column_privilege_name_name_name(PG_FUNCTION_ARGS)
Definition acl.c:2604
Datum pg_has_role_name(PG_FUNCTION_ARGS)
Definition acl.c:4893
Datum has_sequence_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:2231
static AclMode convert_largeobject_priv_string(text *priv_type_text)
Definition acl.c:4838
static List * roles_list_append(List *roles_list, bloom_filter **bf, Oid role)
Definition acl.c:5122
static AclMode convert_type_priv_string(text *priv_type_text)
Definition acl.c:4613
Datum has_tablespace_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:4259
static AclMode convert_table_priv_string(text *priv_type_text)
Definition acl.c:2092
static AclMode convert_server_priv_string(text *priv_type_text)
Definition acl.c:4205
Datum has_table_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:1973
Datum has_foreign_data_wrapper_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:3222
Datum has_tablespace_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:4341
Acl * aclupdate(const Acl *old_acl, const AclItem *mod_aip, int modechg, Oid ownerId, DropBehavior behavior)
Definition acl.c:1020
static Acl * recursive_revoke(Acl *acl, Oid grantee, AclMode revoke_privs, Oid ownerId, DropBehavior behavior)
Definition acl.c:1330
Datum has_database_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:3066
static Oid convert_schema_name(text *schemaname)
Definition acl.c:3991
Datum has_column_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:2813
static void putid(char *p, const char *s)
Definition acl.c:224
Datum has_schema_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:3939
static void check_circularity(const Acl *old_acl, const AclItem *mod_aip, Oid ownerId)
Definition acl.c:1250
static bool has_param_priv_byname(Oid roleid, const text *parameter, AclMode priv)
Definition acl.c:4641
bool is_member_of_role(Oid member, Oid role)
Definition acl.c:5394
Datum has_sequence_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:2266
Datum has_database_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:3124
static Oid convert_foreign_data_wrapper_name(text *fdwname)
Definition acl.c:3382
Datum has_language_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:3711
static AclMode convert_database_priv_string(text *priv_type_text)
Definition acl.c:3188
Datum has_foreign_data_wrapper_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:3248
Datum has_column_privilege_name_attnum(PG_FUNCTION_ARGS)
Definition acl.c:2842
Datum has_column_privilege_id_attnum(PG_FUNCTION_ARGS)
Definition acl.c:2896
RoleRecurseType
Definition acl.c:77
@ ROLERECURSE_PRIVS
Definition acl.c:79
@ ROLERECURSE_MEMBERS
Definition acl.c:78
@ ROLERECURSE_SETROLE
Definition acl.c:80
Datum has_type_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:4482
Datum has_any_column_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:2418
Datum has_server_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:4141
Datum has_language_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:3681
Datum has_foreign_data_wrapper_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:3330
Datum has_schema_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:3881
Datum has_function_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:3422
Datum has_any_column_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:2360
bool is_member_of_role_nosuper(Oid member, Oid role)
Definition acl.c:5422
Datum has_sequence_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:2194
Datum has_schema_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:3857
Datum acldefault_sql(PG_FUNCTION_ARGS)
Definition acl.c:948
bool has_privs_of_role(Oid member, Oid role)
Definition acl.c:5314
Acl * make_empty_acl(void)
Definition acl.c:462
static Oid convert_language_name(text *languagename)
Definition acl.c:3791
Datum has_language_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:3762
static AclMode convert_any_priv_string(text *priv_type_text, const priv_map *privileges)
Definition acl.c:1714
Datum has_database_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:3147
Datum has_function_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:3448
static bool is_safe_acl_char(unsigned char c, bool is_getid)
Definition acl.c:148
int aclmembers(const Acl *acl, Oid **roleids)
Definition acl.c:1568
Datum has_language_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:3657
Acl * aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
Definition acl.c:1147
Datum has_server_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:4059
bool member_can_set_role(Oid member, Oid role)
Definition acl.c:5348
Datum aclremove(PG_FUNCTION_ARGS)
Definition acl.c:1630
static const char * convert_aclright_to_string(int aclright)
Definition acl.c:1762
Acl * aclcopy(const Acl *orig_acl)
Definition acl.c:471
Datum has_function_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:3502
void aclitemsort(Acl *acl)
Definition acl.c:559
Datum has_table_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:2001
Datum has_function_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:3553
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition acl.c:1416
static Oid convert_server_name(text *servername)
Definition acl.c:4193
static Oid convert_tablespace_name(text *tablespacename)
Definition acl.c:4393
Datum has_column_privilege_name_name_attnum(PG_FUNCTION_ARGS)
Definition acl.c:2633
static AclMode convert_parameter_priv_string(text *priv_text)
Definition acl.c:4702
static Oid convert_table_name(text *tablename)
Definition acl.c:2077
Datum has_column_privilege_name_id_name(PG_FUNCTION_ARGS)
Definition acl.c:2660
static AclMode convert_tablespace_priv_string(text *priv_type_text)
Definition acl.c:4405
static AclMode convert_language_priv_string(text *priv_type_text)
Definition acl.c:3803
Datum aclitemin(PG_FUNCTION_ARGS)
Definition acl.c:629
static AclMode convert_sequence_priv_string(text *priv_type_text)
Definition acl.c:2327
Datum pg_has_role_id(PG_FUNCTION_ARGS)
Definition acl.c:4941
Datum has_parameter_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:4684
static Acl * allocacl(int n)
Definition acl.c:440
static AttrNumber convert_column_name(Oid tableoid, text *column)
Definition acl.c:2924
static AclMode convert_role_priv_string(text *priv_type_text)
Definition acl.c:5016
static bool has_lo_priv_byid(Oid roleid, Oid lobjId, AclMode priv, bool *is_missing)
Definition acl.c:4732
Oid get_role_oid(const char *rolname, bool missing_ok)
Definition acl.c:5605
static Oid cached_role[]
Definition acl.c:82
Datum has_tablespace_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:4233
Datum has_parameter_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:4670
static void RoleMembershipCacheCallback(Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
Definition acl.c:5099
Datum has_any_column_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:2390
Datum has_sequence_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:2134
Datum has_function_privilege_name_id(PG_FUNCTION_ARGS)
Definition acl.c:3472
Datum has_type_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:4563
char * get_rolespec_name(const RoleSpec *role)
Definition acl.c:5724
void select_best_grantor(const RoleSpec *grantedBy, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition acl.c:5508
Datum has_server_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:4164
static AclResult pg_role_aclcheck(Oid role_oid, Oid roleid, AclMode mode)
Definition acl.c:5039
Datum has_function_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:3530
Datum has_table_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:1949
Datum has_type_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:4540
Datum has_column_privilege_id_name_attnum(PG_FUNCTION_ARGS)
Definition acl.c:2739
void check_can_set_role(Oid member, Oid role)
Definition acl.c:5371
static const char * aclparse(const char *s, AclItem *aip, Node *escontext)
Definition acl.c:279
void check_rolespec_name(const RoleSpec *role, const char *detail_msg)
Definition acl.c:5746
Datum has_type_privilege_name(PG_FUNCTION_ARGS)
Definition acl.c:4458
static List * cached_roles[]
Definition acl.c:83
static AclMode aclmask_direct(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition acl.c:1505
Datum has_tablespace_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:4313
Datum pg_has_role_name_id(PG_FUNCTION_ARGS)
Definition acl.c:4917
Datum aclcontains(PG_FUNCTION_ARGS)
Definition acl.c:1640
static Oid convert_function_name(text *functionname)
Definition acl.c:3582
Datum has_column_privilege_id_name_name(PG_FUNCTION_ARGS)
Definition acl.c:2712
Datum has_schema_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:3962
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition acl.c:5639
Datum has_any_column_privilege_id_name(PG_FUNCTION_ARGS)
Definition acl.c:2486
Datum has_largeobject_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:4791
Datum has_database_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:3016
Datum has_column_privilege_id_id_name(PG_FUNCTION_ARGS)
Definition acl.c:2764
static AclMode convert_schema_priv_string(text *priv_type_text)
Definition acl.c:4003
Datum has_table_privilege_name_name(PG_FUNCTION_ARGS)
Definition acl.c:1923
Datum has_any_column_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:2513
static AclMode convert_foreign_data_wrapper_priv_string(text *priv_type_text)
Definition acl.c:3394
Datum has_database_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:3096
static int aclitemComparator(const void *arg1, const void *arg2)
Definition acl.c:748
Datum has_type_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:4512
Datum has_any_column_privilege_id(PG_FUNCTION_ARGS)
Definition acl.c:2453
Datum pg_has_role_id_name(PG_FUNCTION_ARGS)
Definition acl.c:4963
Datum has_sequence_privilege_id_id(PG_FUNCTION_ARGS)
Definition acl.c:2294
HeapTuple get_rolespec_tuple(const RoleSpec *role)
Definition acl.c:5678
static AclMode convert_column_priv_string(text *priv_type_text)
Definition acl.c:2982
#define ACL_CREATE_CHR
Definition acl.h:146
#define PG_RETURN_ACLITEM_P(x)
Definition acl.h:118
#define ACL_ALL_RIGHTS_STR
Definition acl.h:154
#define ACL_SET_CHR
Definition acl.h:149
#define ACL_REFERENCES_CHR
Definition acl.h:142
#define ACLITEM_ALL_GOPTION_BITS
Definition acl.h:88
#define ACL_SIZE(ACL)
Definition acl.h:111
#define PG_GETARG_ACLITEM_P(n)
Definition acl.h:117
#define ACL_DAT(ACL)
Definition acl.h:109
#define ACL_ALL_RIGHTS_FOREIGN_SERVER
Definition acl.h:164
#define ACL_TRUNCATE_CHR
Definition acl.h:141
#define ACL_ALL_RIGHTS_TABLESPACE
Definition acl.h:171
AclResult
Definition acl.h:183
@ ACLCHECK_NO_PRIV
Definition acl.h:185
@ ACLCHECK_OK
Definition acl.h:184
#define ACLITEM_GET_PRIVS(item)
Definition acl.h:66
#define ACL_ALL_RIGHTS_PARAMETER_ACL
Definition acl.h:168
#define ACL_SELECT_CHR
Definition acl.h:138
#define ACL_ALL_RIGHTS_SCHEMA
Definition acl.h:170
#define ACL_EXECUTE_CHR
Definition acl.h:144
#define ACL_MODECHG_DEL
Definition acl.h:130
#define ACL_DELETE_CHR
Definition acl.h:140
#define ACL_ALL_RIGHTS_SEQUENCE
Definition acl.h:161
#define ACL_ALL_RIGHTS_DATABASE
Definition acl.h:162
#define ACL_ALL_RIGHTS_PROPGRAPH
Definition acl.h:169
#define ACL_MODECHG_ADD
Definition acl.h:129
#define ACL_OPTION_TO_PRIVS(privs)
Definition acl.h:71
#define ACL_INSERT_CHR
Definition acl.h:137
#define ACL_UPDATE_CHR
Definition acl.h:139
#define ACL_ALL_RIGHTS_FUNCTION
Definition acl.h:165
#define ACL_N_SIZE(N)
Definition acl.h:110
#define ACL_ALL_RIGHTS_LANGUAGE
Definition acl.h:166
#define ACL_ALL_RIGHTS_TYPE
Definition acl.h:172
#define ACL_ALTER_SYSTEM_CHR
Definition acl.h:150
#define ACL_ALL_RIGHTS_FDW
Definition acl.h:163
#define ACLITEM_SET_PRIVS_GOPTIONS(item, privs, goptions)
Definition acl.h:82
#define ACL_NUM(ACL)
Definition acl.h:108
#define ACL_USAGE_CHR
Definition acl.h:145
#define PG_GETARG_ACL_P(n)
Definition acl.h:122
#define ACLITEM_GET_GOPTIONS(item)
Definition acl.h:67
#define ACLITEM_GET_RIGHTS(item)
Definition acl.h:68
#define ACL_ALL_RIGHTS_RELATION
Definition acl.h:160
#define ACL_MODECHG_EQL
Definition acl.h:131
#define ACL_ID_PUBLIC
Definition acl.h:46
#define ACL_CONNECT_CHR
Definition acl.h:148
#define ACL_ALL_RIGHTS_LARGEOBJECT
Definition acl.h:167
AclMaskHow
Definition acl.h:176
@ ACLMASK_ANY
Definition acl.h:178
@ ACLMASK_ALL
Definition acl.h:177
#define PG_RETURN_ACL_P(x)
Definition acl.h:124
#define ACL_TRIGGER_CHR
Definition acl.h:143
#define ACL_CREATE_TEMP_CHR
Definition acl.h:147
#define ACL_GRANT_OPTION_FOR(privs)
Definition acl.h:70
#define ACLITEM_SET_RIGHTS(item, rights)
Definition acl.h:79
#define ACL_MAINTAIN_CHR
Definition acl.h:151
AclResult object_aclcheck_ext(Oid classid, Oid objectid, Oid roleid, AclMode mode, bool *is_missing)
Definition aclchk.c:3889
AclResult pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode, Snapshot snapshot)
Definition aclchk.c:4119
AclResult pg_class_aclcheck_ext(Oid table_oid, Oid roleid, AclMode mode, bool *is_missing)
Definition aclchk.c:4092
AclResult pg_attribute_aclcheck_all_ext(Oid table_oid, Oid roleid, AclMode mode, AclMaskHow how, bool *is_missing)
Definition aclchk.c:3964
AclResult pg_attribute_aclcheck_all(Oid table_oid, Oid roleid, AclMode mode, AclMaskHow how)
Definition aclchk.c:3953
AclResult pg_parameter_aclcheck(const char *name, Oid roleid, AclMode mode)
Definition aclchk.c:4107
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition aclchk.c:3879
AclResult pg_attribute_aclcheck_ext(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode, bool *is_missing)
Definition aclchk.c:3923
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition aclchk.c:4082
#define ARR_NDIM(a)
Definition array.h:290
#define ARR_ELEMTYPE(a)
Definition array.h:292
#define ARR_DIMS(a)
Definition array.h:294
#define ARR_HASNULL(a)
Definition array.h:291
#define ARR_LBOUND(a)
Definition array.h:296
int16 AttrNumber
Definition attnum.h:21
#define InvalidAttrNumber
Definition attnum.h:23
Oid get_tablespace_oid(const char *tablespacename, bool missing_ok)
void bloom_free(bloom_filter *filter)
bloom_filter * bloom_create(int64 total_elems, int bloom_work_mem, uint64 seed)
Definition bloomfilter.c:87
bool bloom_lacks_element(bloom_filter *filter, unsigned char *elem, size_t len)
void bloom_add_element(bloom_filter *filter, unsigned char *elem, size_t len)
static Datum values[MAXATTR]
Definition bootstrap.c:188
Oid boot_get_role_oid(const char *rolname)
Definition bootstrap.c:1087
#define CStringGetTextDatum(s)
Definition builtins.h:98
#define NameStr(name)
Definition c.h:837
#define IS_HIGHBIT_SET(ch)
Definition c.h:1246
#define Assert(condition)
Definition c.h:945
uint64_t uint64
Definition c.h:619
uint32_t uint32
Definition c.h:618
#define UINT64CONST(x)
Definition c.h:633
#define OidIsValid(objectId)
Definition c.h:860
size_t Size
Definition c.h:691
bool IsReservedName(const char *name)
Definition catalog.c:278
Oid get_database_oid(const char *dbname, bool missing_ok)
Datum arg
Definition elog.c:1322
int errcode(int sqlerrcode)
Definition elog.c:874
#define ereturn(context, dummy_value,...)
Definition elog.h:278
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define WARNING
Definition elog.h:36
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_RETURN_UINT32(x)
Definition fmgr.h:356
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_GETARG_CHAR(n)
Definition fmgr.h:273
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
#define PG_GETARG_NAME(n)
Definition fmgr.h:279
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define PG_GETARG_INT16(n)
Definition fmgr.h:271
Oid get_foreign_server_oid(const char *servername, bool missing_ok)
Definition foreign.c:793
Oid get_foreign_data_wrapper_oid(const char *fdwname, bool missing_ok)
Definition foreign.c:770
#define SRF_IS_FIRSTCALL()
Definition funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition funcapi.h:306
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition funcapi.h:230
#define SRF_RETURN_DONE(_funcctx)
Definition funcapi.h:328
int work_mem
Definition globals.c:131
Oid MyDatabaseId
Definition globals.c:94
static Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition hashfn.h:49
static const FormData_pg_attribute a1
Definition heap.c:144
static const FormData_pg_attribute a2
Definition heap.c:157
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1037
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
#define funcname
int remaining
Definition informix.c:692
static char * username
Definition initdb.c:153
#define read(a, b, c)
Definition win32.h:13
bool lo_compat_privileges
Definition inv_api.c:56
void CacheRegisterSyscacheCallback(SysCacheIdentifier cacheid, SyscacheCallbackFunction func, Datum arg)
Definition inval.c:1816
int a
Definition isn.c:73
int j
Definition isn.c:78
int i
Definition isn.c:77
List * list_copy(const List *oldlist)
Definition list.c:1573
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
void list_free(List *list)
Definition list.c:1546
bool list_member_oid(const List *list, Oid datum)
Definition list.c:722
#define NoLock
Definition lockdefs.h:34
char * get_rel_name(Oid relid)
Definition lsyscache.c:2148
char get_rel_relkind(Oid relid)
Definition lsyscache.c:2223
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc0(Size size)
Definition mcxt.c:1417
MemoryContext TopMemoryContext
Definition mcxt.c:166
void * palloc(Size size)
Definition mcxt.c:1387
#define IsBootstrapProcessingMode()
Definition miscadmin.h:477
Oid GetUserId(void)
Definition miscinit.c:470
Oid GetSessionUserId(void)
Definition miscinit.c:509
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition miscinit.c:989
Oid get_namespace_oid(const char *nspname, bool missing_ok)
Definition namespace.c:3607
RangeVar * makeRangeVarFromNameList(const List *names)
Definition namespace.c:3626
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition namespace.h:98
static char * errmsg
int oid_cmp(const void *p1, const void *p2)
Definition oid.c:287
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
@ ROLESPEC_CURRENT_USER
Definition parsenodes.h:421
@ ROLESPEC_CSTRING
Definition parsenodes.h:419
@ ROLESPEC_SESSION_USER
Definition parsenodes.h:422
@ ROLESPEC_CURRENT_ROLE
Definition parsenodes.h:420
@ ROLESPEC_PUBLIC
Definition parsenodes.h:423
#define ACL_CREATE_TEMP
Definition parsenodes.h:86
#define ACL_SET
Definition parsenodes.h:88
#define ACL_DELETE
Definition parsenodes.h:79
uint64 AclMode
Definition parsenodes.h:74
#define ACL_MAINTAIN
Definition parsenodes.h:90
#define ACL_USAGE
Definition parsenodes.h:84
#define ACL_INSERT
Definition parsenodes.h:76
#define ACL_NO_RIGHTS
Definition parsenodes.h:92
#define ACL_UPDATE
Definition parsenodes.h:78
DropBehavior
@ DROP_CASCADE
@ DROP_RESTRICT
ObjectType
@ OBJECT_FDW
@ OBJECT_PROPGRAPH
@ OBJECT_SCHEMA
@ OBJECT_DOMAIN
@ OBJECT_COLUMN
@ OBJECT_TABLESPACE
@ OBJECT_LARGEOBJECT
@ OBJECT_DATABASE
@ OBJECT_SEQUENCE
@ OBJECT_LANGUAGE
@ OBJECT_FOREIGN_SERVER
@ OBJECT_TABLE
@ OBJECT_PARAMETER_ACL
@ OBJECT_TYPE
@ OBJECT_FUNCTION
#define ACL_CONNECT
Definition parsenodes.h:87
#define ACL_ALTER_SYSTEM
Definition parsenodes.h:89
#define ACL_REFERENCES
Definition parsenodes.h:81
#define ACL_SELECT
Definition parsenodes.h:77
#define ACL_TRUNCATE
Definition parsenodes.h:80
#define ACL_EXECUTE
Definition parsenodes.h:83
#define ACL_CREATE
Definition parsenodes.h:85
#define ACL_TRIGGER
Definition parsenodes.h:82
#define N_ACL_RIGHTS
Definition parsenodes.h:91
int16 attnum
FormData_pg_attribute * Form_pg_attribute
END_CATALOG_STRUCT typedef FormData_pg_auth_members * Form_pg_auth_members
NameData rolname
Definition pg_authid.h:36
END_CATALOG_STRUCT typedef FormData_pg_authid * Form_pg_authid
Definition pg_authid.h:60
static int pg_popcount64(uint64 word)
static PgChecksumMode mode
#define NAMEDATALEN
const void size_t len
END_CATALOG_STRUCT typedef FormData_pg_database * Form_pg_database
bool LargeObjectExistsWithSnapshot(Oid loid, Snapshot snapshot)
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define list_make1_oid(x1)
Definition pg_list.h:242
#define foreach_oid(var, lst)
Definition pg_list.h:471
#define lfirst_oid(lc)
Definition pg_list.h:174
NameData typname
Definition pg_type.h:43
int pg_strcasecmp(const char *s1, const char *s2)
#define sprintf
Definition port.h:262
#define qsort(a, b, c, d)
Definition port.h:495
static Oid DatumGetObjectId(Datum X)
Definition postgres.h:242
static Datum UInt64GetDatum(uint64 X)
Definition postgres.h:433
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
static Datum CStringGetDatum(const char *X)
Definition postgres.h:370
#define InvalidOid
unsigned int Oid
char * c
static int fb(int x)
Oid get_language_oid(const char *langname, bool missing_ok)
Definition proclang.c:227
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition qunique.h:21
Datum regtypein(PG_FUNCTION_ARGS)
Definition regproc.c:1184
Datum regprocedurein(PG_FUNCTION_ARGS)
Definition regproc.c:229
Snapshot GetActiveSnapshot(void)
Definition snapmgr.c:800
char * dbname
Definition streamutil.c:49
Definition acl.h:55
Oid ai_grantee
Definition acl.h:56
AclMode ai_privs
Definition acl.h:58
Oid ai_grantor
Definition acl.h:57
Definition pg_list.h:54
Definition nodes.h:135
RoleSpecType roletype
Definition parsenodes.h:429
char * rolename
Definition parsenodes.h:430
Definition c.h:832
Definition acl.c:57
const char * name
Definition acl.c:58
AclMode value
Definition acl.c:59
Definition c.h:778
bool superuser_arg(Oid roleid)
Definition superuser.c:57
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache2(SysCacheIdentifier cacheId, Datum key1, Datum key2)
Definition syscache.c:230
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:220
#define ReleaseSysCacheList(x)
Definition syscache.h:134
#define SearchSysCacheList1(cacheId, key1)
Definition syscache.h:127
#define GetSysCacheHashValue1(cacheId, key1)
Definition syscache.h:118
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition syscache.h:109
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:165
void TupleDescFinalize(TupleDesc tupdesc)
Definition tupdesc.c:508
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:897
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432
char * text_to_cstring(const text *t)
Definition varlena.c:217
List * textToQualifiedNameList(text *textval)
Definition varlena.c:2719
const char * type
const char * name