Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * acl.c
4 : : * Basic access control list data structures manipulation routines.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/utils/adt/acl.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include <ctype.h>
18 : :
19 : : #include "access/htup_details.h"
20 : : #include "bootstrap/bootstrap.h"
21 : : #include "catalog/catalog.h"
22 : : #include "catalog/namespace.h"
23 : : #include "catalog/pg_auth_members.h"
24 : : #include "catalog/pg_authid.h"
25 : : #include "catalog/pg_class.h"
26 : : #include "catalog/pg_database.h"
27 : : #include "catalog/pg_foreign_data_wrapper.h"
28 : : #include "catalog/pg_foreign_server.h"
29 : : #include "catalog/pg_language.h"
30 : : #include "catalog/pg_largeobject.h"
31 : : #include "catalog/pg_namespace.h"
32 : : #include "catalog/pg_proc.h"
33 : : #include "catalog/pg_tablespace.h"
34 : : #include "catalog/pg_type.h"
35 : : #include "commands/proclang.h"
36 : : #include "commands/tablespace.h"
37 : : #include "common/hashfn.h"
38 : : #include "foreign/foreign.h"
39 : : #include "funcapi.h"
40 : : #include "lib/bloomfilter.h"
41 : : #include "lib/qunique.h"
42 : : #include "miscadmin.h"
43 : : #include "port/pg_bitutils.h"
44 : : #include "storage/large_object.h"
45 : : #include "utils/acl.h"
46 : : #include "utils/array.h"
47 : : #include "utils/builtins.h"
48 : : #include "utils/catcache.h"
49 : : #include "utils/inval.h"
50 : : #include "utils/lsyscache.h"
51 : : #include "utils/memutils.h"
52 : : #include "utils/snapmgr.h"
53 : : #include "utils/syscache.h"
54 : : #include "utils/varlena.h"
55 : :
56 : : typedef struct
57 : : {
58 : : const char *name;
59 : : AclMode value;
60 : : } priv_map;
61 : :
62 : : /*
63 : : * We frequently need to test whether a given role is a member of some other
64 : : * role. In most of these tests the "given role" is the same, namely the
65 : : * active current user. So we can optimize it by keeping cached lists of all
66 : : * the roles the "given role" is a member of, directly or indirectly.
67 : : *
68 : : * Possibly this mechanism should be generalized to allow caching membership
69 : : * info for multiple roles?
70 : : *
71 : : * Each element of cached_roles is an OID list of constituent roles for the
72 : : * corresponding element of cached_role (always including the cached_role
73 : : * itself). There's a separate cache for each RoleRecurseType, with the
74 : : * corresponding semantics.
75 : : */
76 : : enum RoleRecurseType
77 : : {
78 : : ROLERECURSE_MEMBERS = 0, /* recurse unconditionally */
79 : : ROLERECURSE_PRIVS = 1, /* recurse through inheritable grants */
80 : : ROLERECURSE_SETROLE = 2 /* recurse through grants with set_option */
81 : : };
82 : : static Oid cached_role[] = {InvalidOid, InvalidOid, InvalidOid};
83 : : static List *cached_roles[] = {NIL, NIL, NIL};
84 : : static uint32 cached_db_hash;
85 : :
86 : : /*
87 : : * If the list of roles gathered by roles_is_member_of() grows larger than the
88 : : * below threshold, a Bloom filter is created to speed up list membership
89 : : * checks. This threshold is set arbitrarily high to avoid the overhead of
90 : : * creating the Bloom filter until it seems likely to provide a net benefit.
91 : : */
92 : : #define ROLES_LIST_BLOOM_THRESHOLD 1024
93 : :
94 : : static const char *getid(const char *s, char *n, Node *escontext);
95 : : static void putid(char *p, const char *s);
96 : : static Acl *allocacl(int n);
97 : : static void check_acl(const Acl *acl);
98 : : static const char *aclparse(const char *s, AclItem *aip, Node *escontext);
99 : : static bool aclitem_match(const AclItem *a1, const AclItem *a2);
100 : : static int aclitemComparator(const void *arg1, const void *arg2);
101 : : static void check_circularity(const Acl *old_acl, const AclItem *mod_aip,
102 : : Oid ownerId);
103 : : static Acl *recursive_revoke(Acl *acl, Oid grantee, AclMode revoke_privs,
104 : : Oid ownerId, DropBehavior behavior);
105 : :
106 : : static AclMode convert_any_priv_string(text *priv_type_text,
107 : : const priv_map *privileges);
108 : :
109 : : static Oid convert_table_name(text *tablename);
110 : : static AclMode convert_table_priv_string(text *priv_type_text);
111 : : static AclMode convert_sequence_priv_string(text *priv_type_text);
112 : : static AttrNumber convert_column_name(Oid tableoid, text *column);
113 : : static AclMode convert_column_priv_string(text *priv_type_text);
114 : : static Oid convert_database_name(text *databasename);
115 : : static AclMode convert_database_priv_string(text *priv_type_text);
116 : : static Oid convert_foreign_data_wrapper_name(text *fdwname);
117 : : static AclMode convert_foreign_data_wrapper_priv_string(text *priv_type_text);
118 : : static Oid convert_function_name(text *functionname);
119 : : static AclMode convert_function_priv_string(text *priv_type_text);
120 : : static Oid convert_language_name(text *languagename);
121 : : static AclMode convert_language_priv_string(text *priv_type_text);
122 : : static Oid convert_schema_name(text *schemaname);
123 : : static AclMode convert_schema_priv_string(text *priv_type_text);
124 : : static Oid convert_server_name(text *servername);
125 : : static AclMode convert_server_priv_string(text *priv_type_text);
126 : : static Oid convert_tablespace_name(text *tablespacename);
127 : : static AclMode convert_tablespace_priv_string(text *priv_type_text);
128 : : static Oid convert_type_name(text *typename);
129 : : static AclMode convert_type_priv_string(text *priv_type_text);
130 : : static AclMode convert_parameter_priv_string(text *priv_text);
131 : : static AclMode convert_largeobject_priv_string(text *priv_type_text);
132 : : static AclMode convert_role_priv_string(text *priv_type_text);
133 : : static AclResult pg_role_aclcheck(Oid role_oid, Oid roleid, AclMode mode);
134 : :
135 : : static void RoleMembershipCacheCallback(Datum arg, SysCacheIdentifier cacheid,
136 : : uint32 hashvalue);
137 : :
138 : :
139 : : /*
140 : : * Test whether an identifier char can be left unquoted in ACLs.
141 : : *
142 : : * Formerly, we used isalnum() even on non-ASCII characters, resulting in
143 : : * unportable behavior. To ensure dump compatibility with old versions,
144 : : * we now treat high-bit-set characters as always requiring quoting during
145 : : * putid(), but getid() will always accept them without quotes.
146 : : */
147 : : static inline bool
298 tgl@sss.pgh.pa.us 148 :CBC 7194697 : is_safe_acl_char(unsigned char c, bool is_getid)
149 : : {
150 [ - + ]: 7194697 : if (IS_HIGHBIT_SET(c))
298 tgl@sss.pgh.pa.us 151 :UBC 0 : return is_getid;
298 tgl@sss.pgh.pa.us 152 [ + + + + ]:CBC 7194697 : return isalnum(c) || c == '_';
153 : : }
154 : :
155 : : /*
156 : : * getid
157 : : * Consumes the first alphanumeric string (identifier) found in string
158 : : * 's', ignoring any leading white space. If it finds a double quote
159 : : * it returns the word inside the quotes.
160 : : *
161 : : * RETURNS:
162 : : * the string position in 's' that points to the next non-space character
163 : : * in 's', after any quotes. Also:
164 : : * - loads the identifier into 'n'. (If no identifier is found, 'n'
165 : : * contains an empty string.) 'n' must be NAMEDATALEN bytes.
166 : : *
167 : : * Errors are reported via ereport, unless escontext is an ErrorSaveData node,
168 : : * in which case we log the error there and return NULL.
169 : : */
170 : : static const char *
1238 171 : 5027 : getid(const char *s, char *n, Node *escontext)
172 : : {
8373 173 : 5027 : int len = 0;
174 : 5027 : bool in_quotes = false;
175 : :
10467 bruce@momjian.us 176 [ + - - + ]: 5027 : Assert(s && n);
177 : :
9284 tgl@sss.pgh.pa.us 178 [ - + ]: 5027 : while (isspace((unsigned char) *s))
9907 bruce@momjian.us 179 :UBC 0 : s++;
8373 tgl@sss.pgh.pa.us 180 :CBC 5027 : for (;
181 [ + + + + ]: 48988 : *s != '\0' &&
298 182 [ + + + + ]: 48374 : (in_quotes || *s == '"' || is_safe_acl_char(*s, true));
8373 183 : 43961 : s++)
184 : : {
185 [ + + ]: 43961 : if (*s == '"')
186 : : {
298 187 [ + + ]: 150 : if (!in_quotes)
188 : : {
189 : 71 : in_quotes = true;
190 : 71 : continue;
191 : : }
192 : : /* safe to look at next char (could be '\0' though) */
8300 193 [ + + ]: 79 : if (*(s + 1) != '"')
194 : : {
298 195 : 71 : in_quotes = false;
8300 196 : 71 : continue;
197 : : }
198 : : /* it's an escaped double quote; skip the escaping char */
199 : 8 : s++;
200 : : }
201 : :
202 : : /* Add the character to the string */
203 [ - + ]: 43819 : if (len >= NAMEDATALEN - 1)
1238 tgl@sss.pgh.pa.us 204 [ # # ]:UBC 0 : ereturn(escontext, NULL,
205 : : (errcode(ERRCODE_NAME_TOO_LONG),
206 : : errmsg("identifier too long"),
207 : : errdetail("Identifier must be less than %d characters.",
208 : : NAMEDATALEN)));
209 : :
8300 tgl@sss.pgh.pa.us 210 :CBC 43819 : n[len++] = *s;
211 : : }
10467 bruce@momjian.us 212 : 5027 : n[len] = '\0';
9284 tgl@sss.pgh.pa.us 213 [ - + ]: 5027 : while (isspace((unsigned char) *s))
8373 tgl@sss.pgh.pa.us 214 :UBC 0 : s++;
10108 bruce@momjian.us 215 :CBC 5027 : return s;
216 : : }
217 : :
218 : : /*
219 : : * Write a role name at *p, adding double quotes if needed.
220 : : * There must be at least (2*NAMEDATALEN)+2 bytes available at *p.
221 : : * This needs to be kept in sync with dequoteAclUserName in pg_dump/dumputils.c
222 : : */
223 : : static void
8373 tgl@sss.pgh.pa.us 224 : 1126169 : putid(char *p, const char *s)
225 : : {
226 : : const char *src;
8310 bruce@momjian.us 227 : 1126169 : bool safe = true;
228 : :
229 : : /* Detect whether we need to use double quotes */
8373 tgl@sss.pgh.pa.us 230 [ + + ]: 8272356 : for (src = s; *src; src++)
231 : : {
298 232 [ + + ]: 7146394 : if (!is_safe_acl_char(*src, false))
233 : : {
8373 234 : 207 : safe = false;
235 : 207 : break;
236 : : }
237 : : }
238 [ + + ]: 1126169 : if (!safe)
239 : 207 : *p++ = '"';
240 [ + + ]: 8274187 : for (src = s; *src; src++)
241 : : {
242 : : /* A double quote character in a username is encoded as "" */
8300 243 [ + + ]: 7148018 : if (*src == '"')
244 : 207 : *p++ = '"';
8373 245 : 7148018 : *p++ = *src;
246 : : }
247 [ + + ]: 1126169 : if (!safe)
248 : 207 : *p++ = '"';
249 : 1126169 : *p = '\0';
250 : 1126169 : }
251 : :
252 : : /*
253 : : * aclparse
254 : : * Consumes and parses an ACL specification of the form:
255 : : * [group|user] [A-Za-z0-9]*=[rwaR]*
256 : : * from string 's', ignoring any leading white space or white space
257 : : * between the optional id type keyword (group|user) and the actual
258 : : * ACL specification.
259 : : *
260 : : * The group|user decoration is unnecessary in the roles world,
261 : : * but we still accept it for backward compatibility.
262 : : *
263 : : * This routine is called by the parser as well as aclitemin(), hence
264 : : * the added generality.
265 : : *
266 : : * In bootstrap mode, we consult a hard-wired list of role names
267 : : * (see bootstrap.c) rather than trying to access the catalogs.
268 : : *
269 : : * RETURNS:
270 : : * the string position in 's' immediately following the ACL
271 : : * specification. Also:
272 : : * - loads the structure pointed to by 'aip' with the appropriate
273 : : * UID/GID, id type identifier and mode type values.
274 : : *
275 : : * Errors are reported via ereport, unless escontext is an ErrorSaveData node,
276 : : * in which case we log the error there and return NULL.
277 : : */
278 : : static const char *
1238 279 : 4940 : aclparse(const char *s, AclItem *aip, Node *escontext)
280 : : {
281 : : AclMode privs,
282 : : goption,
283 : : read;
284 : : char name[NAMEDATALEN];
285 : : char name2[NAMEDATALEN];
286 : :
8503 peter_e@gmx.net 287 [ + - - + ]: 4940 : Assert(s && aip);
288 : :
1238 tgl@sss.pgh.pa.us 289 : 4940 : s = getid(s, name, escontext);
290 [ - + ]: 4940 : if (s == NULL)
1238 tgl@sss.pgh.pa.us 291 :UBC 0 : return NULL;
8503 peter_e@gmx.net 292 [ - + ]:CBC 4940 : if (*s != '=')
293 : : {
294 : : /* we just read a keyword, not a name */
7615 tgl@sss.pgh.pa.us 295 [ # # # # ]:UBC 0 : if (strcmp(name, "group") != 0 && strcmp(name, "user") != 0)
1238 296 [ # # ]: 0 : ereturn(escontext, NULL,
297 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
298 : : errmsg("unrecognized key word: \"%s\"", name),
299 : : errhint("ACL key word must be \"group\" or \"user\".")));
300 : : /* move s to the name beyond the keyword */
301 : 0 : s = getid(s, name, escontext);
302 [ # # ]: 0 : if (s == NULL)
303 : 0 : return NULL;
10467 bruce@momjian.us 304 [ # # ]: 0 : if (name[0] == '\0')
1238 tgl@sss.pgh.pa.us 305 [ # # ]: 0 : ereturn(escontext, NULL,
306 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
307 : : errmsg("missing name"),
308 : : errhint("A name must follow the \"group\" or \"user\" key word.")));
309 : : }
310 : :
8503 peter_e@gmx.net 311 [ - + ]:CBC 4940 : if (*s != '=')
1238 tgl@sss.pgh.pa.us 312 [ # # ]:UBC 0 : ereturn(escontext, NULL,
313 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
314 : : errmsg("missing \"=\" sign")));
315 : :
8503 peter_e@gmx.net 316 :CBC 4940 : privs = goption = ACL_NO_RIGHTS;
317 : :
8310 bruce@momjian.us 318 [ + + + + ]: 9892 : for (++s, read = 0; isalpha((unsigned char) *s) || *s == '*'; s++)
319 : : {
10467 320 [ + - + - : 4960 : switch (*s)
- - - - +
+ + - - -
- - + ]
321 : : {
8503 peter_e@gmx.net 322 : 8 : case '*':
323 : 8 : goption |= read;
324 : 8 : break;
8780 tgl@sss.pgh.pa.us 325 :UBC 0 : case ACL_INSERT_CHR:
8503 peter_e@gmx.net 326 : 0 : read = ACL_INSERT;
8780 tgl@sss.pgh.pa.us 327 : 0 : break;
8780 tgl@sss.pgh.pa.us 328 :CBC 91 : case ACL_SELECT_CHR:
8503 peter_e@gmx.net 329 : 91 : read = ACL_SELECT;
8780 tgl@sss.pgh.pa.us 330 : 91 : break;
8780 tgl@sss.pgh.pa.us 331 :UBC 0 : case ACL_UPDATE_CHR:
8503 peter_e@gmx.net 332 : 0 : read = ACL_UPDATE;
8780 tgl@sss.pgh.pa.us 333 : 0 : break;
334 : 0 : case ACL_DELETE_CHR:
8503 peter_e@gmx.net 335 : 0 : read = ACL_DELETE;
10466 bruce@momjian.us 336 : 0 : break;
6448 tgl@sss.pgh.pa.us 337 : 0 : case ACL_TRUNCATE_CHR:
338 : 0 : read = ACL_TRUNCATE;
339 : 0 : break;
8780 340 : 0 : case ACL_REFERENCES_CHR:
8503 peter_e@gmx.net 341 : 0 : read = ACL_REFERENCES;
10466 bruce@momjian.us 342 : 0 : break;
8780 tgl@sss.pgh.pa.us 343 : 0 : case ACL_TRIGGER_CHR:
8503 peter_e@gmx.net 344 : 0 : read = ACL_TRIGGER;
9109 345 : 0 : break;
8780 tgl@sss.pgh.pa.us 346 :GBC 4845 : case ACL_EXECUTE_CHR:
8503 peter_e@gmx.net 347 : 4845 : read = ACL_EXECUTE;
9109 348 : 4845 : break;
8780 tgl@sss.pgh.pa.us 349 :CBC 4 : case ACL_USAGE_CHR:
8503 peter_e@gmx.net 350 : 4 : read = ACL_USAGE;
9109 351 : 4 : break;
8780 tgl@sss.pgh.pa.us 352 : 4 : case ACL_CREATE_CHR:
8503 peter_e@gmx.net 353 : 4 : read = ACL_CREATE;
8780 tgl@sss.pgh.pa.us 354 : 4 : break;
8780 tgl@sss.pgh.pa.us 355 :UBC 0 : case ACL_CREATE_TEMP_CHR:
8503 peter_e@gmx.net 356 : 0 : read = ACL_CREATE_TEMP;
10466 bruce@momjian.us 357 : 0 : break;
7310 358 : 0 : case ACL_CONNECT_CHR:
359 : 0 : read = ACL_CONNECT;
360 : 0 : break;
1490 tgl@sss.pgh.pa.us 361 : 0 : case ACL_SET_CHR:
362 : 0 : read = ACL_SET;
363 : 0 : break;
364 : 0 : case ACL_ALTER_SYSTEM_CHR:
365 : 0 : read = ACL_ALTER_SYSTEM;
366 : 0 : break;
783 nathan@postgresql.or 367 : 0 : case ACL_MAINTAIN_CHR:
368 : 0 : read = ACL_MAINTAIN;
369 : 0 : break;
10466 bruce@momjian.us 370 :CBC 8 : default:
1238 tgl@sss.pgh.pa.us 371 [ + + ]: 8 : ereturn(escontext, NULL,
372 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
373 : : errmsg("invalid mode character: must be one of \"%s\"",
374 : : ACL_ALL_RIGHTS_STR)));
375 : : }
376 : :
8503 peter_e@gmx.net 377 : 4952 : privs |= read;
378 : : }
379 : :
7616 tgl@sss.pgh.pa.us 380 [ + + ]: 4932 : if (name[0] == '\0')
381 : 59 : aip->ai_grantee = ACL_ID_PUBLIC;
382 : : else
383 : : {
61 tgl@sss.pgh.pa.us 384 [ + + ]:GNC 4873 : if (IsBootstrapProcessingMode())
385 : 4845 : aip->ai_grantee = boot_get_role_oid(name);
386 : : else
387 : 28 : aip->ai_grantee = get_role_oid(name, true);
1238 tgl@sss.pgh.pa.us 388 [ - + ]:CBC 4873 : if (!OidIsValid(aip->ai_grantee))
1238 tgl@sss.pgh.pa.us 389 [ # # ]:UBC 0 : ereturn(escontext, NULL,
390 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
391 : : errmsg("role \"%s\" does not exist", name)));
392 : : }
393 : :
394 : : /*
395 : : * XXX Allow a degree of backward compatibility by defaulting the grantor
396 : : * to the superuser. We condone that practice in the catalog .dat files
397 : : * (i.e., in bootstrap mode) for brevity; otherwise, issue a warning.
398 : : */
8503 peter_e@gmx.net 399 [ + + ]:CBC 4932 : if (*s == '/')
400 : : {
1238 tgl@sss.pgh.pa.us 401 : 87 : s = getid(s + 1, name2, escontext);
402 [ - + ]: 87 : if (s == NULL)
1238 tgl@sss.pgh.pa.us 403 :UBC 0 : return NULL;
8503 peter_e@gmx.net 404 [ + + ]:CBC 87 : if (name2[0] == '\0')
1238 tgl@sss.pgh.pa.us 405 [ + + ]: 12 : ereturn(escontext, NULL,
406 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
407 : : errmsg("a name must follow the \"/\" sign")));
61 tgl@sss.pgh.pa.us 408 [ - + ]:GNC 75 : if (IsBootstrapProcessingMode())
61 tgl@sss.pgh.pa.us 409 :UNC 0 : aip->ai_grantor = boot_get_role_oid(name2);
410 : : else
61 tgl@sss.pgh.pa.us 411 :GNC 75 : aip->ai_grantor = get_role_oid(name2, true);
1238 tgl@sss.pgh.pa.us 412 [ + + ]:CBC 75 : if (!OidIsValid(aip->ai_grantor))
413 [ + + ]: 8 : ereturn(escontext, NULL,
414 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
415 : : errmsg("role \"%s\" does not exist", name2)));
416 : : }
417 : : else
418 : : {
7616 tgl@sss.pgh.pa.us 419 :GBC 4845 : aip->ai_grantor = BOOTSTRAP_SUPERUSERID;
61 tgl@sss.pgh.pa.us 420 [ - + ]:GNC 4845 : if (!IsBootstrapProcessingMode())
61 tgl@sss.pgh.pa.us 421 [ # # ]:UNC 0 : ereport(WARNING,
422 : : (errcode(ERRCODE_INVALID_GRANTOR),
423 : : errmsg("defaulting grantor to user ID %u",
424 : : BOOTSTRAP_SUPERUSERID)));
425 : : }
426 : :
7616 tgl@sss.pgh.pa.us 427 :CBC 4912 : ACLITEM_SET_PRIVS_GOPTIONS(*aip, privs, goption);
428 : :
10108 bruce@momjian.us 429 : 4912 : return s;
430 : : }
431 : :
432 : : /*
433 : : * allocacl
434 : : * Allocates storage for a new Acl with 'n' entries.
435 : : *
436 : : * RETURNS:
437 : : * the new Acl
438 : : */
439 : : static Acl *
8364 peter_e@gmx.net 440 : 466129 : allocacl(int n)
441 : : {
442 : : Acl *new_acl;
443 : : Size size;
444 : :
10467 bruce@momjian.us 445 [ - + ]: 466129 : if (n < 0)
8318 tgl@sss.pgh.pa.us 446 [ # # ]:UBC 0 : elog(ERROR, "invalid size: %d", n);
10467 bruce@momjian.us 447 :CBC 466129 : size = ACL_N_SIZE(n);
8574 448 : 466129 : new_acl = (Acl *) palloc0(size);
7007 tgl@sss.pgh.pa.us 449 : 466129 : SET_VARSIZE(new_acl, size);
10467 bruce@momjian.us 450 : 466129 : new_acl->ndim = 1;
7474 tgl@sss.pgh.pa.us 451 : 466129 : new_acl->dataoffset = 0; /* we never put in any nulls */
8653 452 : 466129 : new_acl->elemtype = ACLITEMOID;
7942 453 : 466129 : ARR_LBOUND(new_acl)[0] = 1;
10467 bruce@momjian.us 454 : 466129 : ARR_DIMS(new_acl)[0] = n;
10108 455 : 466129 : return new_acl;
456 : : }
457 : :
458 : : /*
459 : : * Create a zero-entry ACL
460 : : */
461 : : Acl *
6056 tgl@sss.pgh.pa.us 462 : 43 : make_empty_acl(void)
463 : : {
464 : 43 : return allocacl(0);
465 : : }
466 : :
467 : : /*
468 : : * Copy an ACL
469 : : */
470 : : Acl *
6312 471 : 11565 : aclcopy(const Acl *orig_acl)
472 : : {
473 : : Acl *result_acl;
474 : :
475 : 11565 : result_acl = allocacl(ACL_NUM(orig_acl));
476 : :
477 [ - + ]: 11565 : memcpy(ACL_DAT(result_acl),
478 : 11565 : ACL_DAT(orig_acl),
479 [ - + ]: 11565 : ACL_NUM(orig_acl) * sizeof(AclItem));
480 : :
481 : 11565 : return result_acl;
482 : : }
483 : :
484 : : /*
485 : : * Concatenate two ACLs
486 : : *
487 : : * This is a bit cheesy, since we may produce an ACL with redundant entries.
488 : : * Be careful what the result is used for!
489 : : */
490 : : Acl *
491 : 28114 : aclconcat(const Acl *left_acl, const Acl *right_acl)
492 : : {
493 : : Acl *result_acl;
494 : :
495 : 28114 : result_acl = allocacl(ACL_NUM(left_acl) + ACL_NUM(right_acl));
496 : :
497 [ - + ]: 28114 : memcpy(ACL_DAT(result_acl),
498 : 28114 : ACL_DAT(left_acl),
499 [ - + ]: 28114 : ACL_NUM(left_acl) * sizeof(AclItem));
500 : :
501 [ - + ]: 28114 : memcpy(ACL_DAT(result_acl) + ACL_NUM(left_acl),
502 : 28114 : ACL_DAT(right_acl),
503 [ - + ]: 28114 : ACL_NUM(right_acl) * sizeof(AclItem));
504 : :
505 : 28114 : return result_acl;
506 : : }
507 : :
508 : : /*
509 : : * Merge two ACLs
510 : : *
511 : : * This produces a properly merged ACL with no redundant entries.
512 : : * Returns NULL on NULL input.
513 : : */
514 : : Acl *
6056 515 : 140 : aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId)
516 : : {
517 : : Acl *result_acl;
518 : : AclItem *aip;
519 : : int i,
520 : : num;
521 : :
522 : : /* Check for cases where one or both are empty/null */
523 [ + - - + ]: 140 : if (left_acl == NULL || ACL_NUM(left_acl) == 0)
524 : : {
6056 tgl@sss.pgh.pa.us 525 [ # # # # ]:UBC 0 : if (right_acl == NULL || ACL_NUM(right_acl) == 0)
526 : 0 : return NULL;
527 : : else
528 : 0 : return aclcopy(right_acl);
529 : : }
530 : : else
531 : : {
6056 tgl@sss.pgh.pa.us 532 [ + + - + ]:CBC 140 : if (right_acl == NULL || ACL_NUM(right_acl) == 0)
533 : 92 : return aclcopy(left_acl);
534 : : }
535 : :
536 : : /* Merge them the hard way, one item at a time */
537 : 48 : result_acl = aclcopy(left_acl);
538 : :
539 [ - + ]: 48 : aip = ACL_DAT(right_acl);
540 : 48 : num = ACL_NUM(right_acl);
541 : :
542 [ + + ]: 120 : for (i = 0; i < num; i++, aip++)
543 : : {
544 : : Acl *tmp_acl;
545 : :
546 : 72 : tmp_acl = aclupdate(result_acl, aip, ACL_MODECHG_ADD,
547 : : ownerId, DROP_RESTRICT);
548 : 72 : pfree(result_acl);
549 : 72 : result_acl = tmp_acl;
550 : : }
551 : :
552 : 48 : return result_acl;
553 : : }
554 : :
555 : : /*
556 : : * Sort the items in an ACL (into an arbitrary but consistent order)
557 : : */
558 : : void
559 : 590 : aclitemsort(Acl *acl)
560 : : {
561 [ + - + + ]: 590 : if (acl != NULL && ACL_NUM(acl) > 1)
562 [ - + ]: 171 : qsort(ACL_DAT(acl), ACL_NUM(acl), sizeof(AclItem), aclitemComparator);
563 : 590 : }
564 : :
565 : : /*
566 : : * Check if two ACLs are exactly equal
567 : : *
568 : : * This will not detect equality if the two arrays contain the same items
569 : : * in different orders. To handle that case, sort both inputs first,
570 : : * using aclitemsort().
571 : : */
572 : : bool
573 : 363 : aclequal(const Acl *left_acl, const Acl *right_acl)
574 : : {
575 : : /* Check for cases where one or both are empty/null */
576 [ + - + + ]: 363 : if (left_acl == NULL || ACL_NUM(left_acl) == 0)
577 : : {
578 [ + - + - ]: 1 : if (right_acl == NULL || ACL_NUM(right_acl) == 0)
579 : 1 : return true;
580 : : else
6056 tgl@sss.pgh.pa.us 581 :UBC 0 : return false;
582 : : }
583 : : else
584 : : {
6056 tgl@sss.pgh.pa.us 585 [ + - + + ]:CBC 362 : if (right_acl == NULL || ACL_NUM(right_acl) == 0)
586 : 34 : return false;
587 : : }
588 : :
589 [ + + ]: 328 : if (ACL_NUM(left_acl) != ACL_NUM(right_acl))
590 : 149 : return false;
591 : :
592 [ - + + + ]: 179 : if (memcmp(ACL_DAT(left_acl),
593 : 179 : ACL_DAT(right_acl),
594 [ - + ]: 179 : ACL_NUM(left_acl) * sizeof(AclItem)) == 0)
595 : 86 : return true;
596 : :
597 : 93 : return false;
598 : : }
599 : :
600 : : /*
601 : : * Verify that an ACL array is acceptable (one-dimensional and has no nulls)
602 : : */
603 : : static void
7473 604 : 197615 : check_acl(const Acl *acl)
605 : : {
606 [ - + ]: 197615 : if (ARR_ELEMTYPE(acl) != ACLITEMOID)
7473 tgl@sss.pgh.pa.us 607 [ # # ]:UBC 0 : ereport(ERROR,
608 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
609 : : errmsg("ACL array contains wrong data type")));
7473 tgl@sss.pgh.pa.us 610 [ - + ]:CBC 197615 : if (ARR_NDIM(acl) != 1)
7473 tgl@sss.pgh.pa.us 611 [ # # ]:UBC 0 : ereport(ERROR,
612 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
613 : : errmsg("ACL arrays must be one-dimensional")));
7473 tgl@sss.pgh.pa.us 614 [ - + ]:CBC 197615 : if (ARR_HASNULL(acl))
7473 tgl@sss.pgh.pa.us 615 [ # # ]:UBC 0 : ereport(ERROR,
616 : : (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
617 : : errmsg("ACL arrays must not contain null values")));
7473 tgl@sss.pgh.pa.us 618 :CBC 197615 : }
619 : :
620 : : /*
621 : : * aclitemin
622 : : * Allocates storage for, and fills in, a new AclItem given a string
623 : : * 's' that contains an ACL specification. See aclparse for details.
624 : : *
625 : : * RETURNS:
626 : : * the new AclItem
627 : : */
628 : : Datum
9409 629 : 4940 : aclitemin(PG_FUNCTION_ARGS)
630 : : {
9096 peter_e@gmx.net 631 : 4940 : const char *s = PG_GETARG_CSTRING(0);
1238 tgl@sss.pgh.pa.us 632 : 4940 : Node *escontext = fcinfo->context;
633 : : AclItem *aip;
634 : :
146 michael@paquier.xyz 635 :GNC 4940 : aip = palloc_object(AclItem);
636 : :
1238 tgl@sss.pgh.pa.us 637 :CBC 4940 : s = aclparse(s, aip, escontext);
638 [ + + ]: 4936 : if (s == NULL)
639 : 24 : PG_RETURN_NULL();
640 : :
9284 641 [ - + ]: 4912 : while (isspace((unsigned char) *s))
10467 bruce@momjian.us 642 :UBC 0 : ++s;
10467 bruce@momjian.us 643 [ - + ]:CBC 4912 : if (*s)
1238 tgl@sss.pgh.pa.us 644 [ # # ]:UBC 0 : ereturn(escontext, (Datum) 0,
645 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
646 : : errmsg("extra garbage at the end of the ACL specification")));
647 : :
9409 tgl@sss.pgh.pa.us 648 :CBC 4912 : PG_RETURN_ACLITEM_P(aip);
649 : : }
650 : :
651 : : /*
652 : : * aclitemout
653 : : * Allocates storage for, and fills in, a new null-delimited string
654 : : * containing a formatted ACL specification. See aclparse for details.
655 : : *
656 : : * In bootstrap mode, this is called for debug printouts (initdb -d).
657 : : * We could ask bootstrap.c to provide an inverse of boot_get_role_oid(),
658 : : * but it seems at least as useful to just print numeric role OIDs.
659 : : *
660 : : * RETURNS:
661 : : * the new string
662 : : */
663 : : Datum
664 : 719323 : aclitemout(PG_FUNCTION_ARGS)
665 : : {
9175 bruce@momjian.us 666 : 719323 : AclItem *aip = PG_GETARG_ACLITEM_P(0);
667 : : char *p;
668 : : char *out;
669 : : HeapTuple htup;
670 : : unsigned i;
671 : :
7615 tgl@sss.pgh.pa.us 672 : 719323 : out = palloc(strlen("=/") +
673 : : 2 * N_ACL_RIGHTS +
674 : : 2 * (2 * NAMEDATALEN + 2) +
675 : : 1);
676 : :
8373 677 : 719323 : p = out;
10467 bruce@momjian.us 678 : 719323 : *p = '\0';
679 : :
7616 tgl@sss.pgh.pa.us 680 [ + + ]: 719323 : if (aip->ai_grantee != ACL_ID_PUBLIC)
681 : : {
52 tgl@sss.pgh.pa.us 682 [ + - ]:GNC 406846 : if (!IsBootstrapProcessingMode())
683 : 406846 : htup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(aip->ai_grantee));
684 : : else
52 tgl@sss.pgh.pa.us 685 :UNC 0 : htup = NULL;
7616 tgl@sss.pgh.pa.us 686 [ + - ]:CBC 406846 : if (HeapTupleIsValid(htup))
687 : : {
688 : 406846 : putid(p, NameStr(((Form_pg_authid) GETSTRUCT(htup))->rolname));
689 : 406846 : ReleaseSysCache(htup);
690 : : }
691 : : else
692 : : {
693 : : /* No such entry, or bootstrap mode: print numeric OID */
7616 tgl@sss.pgh.pa.us 694 :UBC 0 : sprintf(p, "%u", aip->ai_grantee);
695 : : }
696 : : }
10467 bruce@momjian.us 697 [ + + ]:CBC 3350773 : while (*p)
698 : 2631450 : ++p;
699 : :
700 : 719323 : *p++ = '=';
701 : :
8780 tgl@sss.pgh.pa.us 702 [ + + ]: 11509168 : for (i = 0; i < N_ACL_RIGHTS; ++i)
703 : : {
1257 drowley@postgresql.o 704 [ + + ]: 10789845 : if (ACLITEM_GET_PRIVS(*aip) & (UINT64CONST(1) << i))
8780 tgl@sss.pgh.pa.us 705 : 1942817 : *p++ = ACL_ALL_RIGHTS_STR[i];
1257 drowley@postgresql.o 706 [ + + ]: 10789845 : if (ACLITEM_GET_GOPTIONS(*aip) & (UINT64CONST(1) << i))
8503 peter_e@gmx.net 707 : 195 : *p++ = '*';
708 : : }
709 : :
710 : 719323 : *p++ = '/';
711 : 719323 : *p = '\0';
712 : :
52 tgl@sss.pgh.pa.us 713 [ + - ]:GNC 719323 : if (!IsBootstrapProcessingMode())
714 : 719323 : htup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(aip->ai_grantor));
715 : : else
52 tgl@sss.pgh.pa.us 716 :UNC 0 : htup = NULL;
8503 peter_e@gmx.net 717 [ + - ]:CBC 719323 : if (HeapTupleIsValid(htup))
718 : : {
7616 tgl@sss.pgh.pa.us 719 : 719323 : putid(p, NameStr(((Form_pg_authid) GETSTRUCT(htup))->rolname));
8503 peter_e@gmx.net 720 : 719323 : ReleaseSysCache(htup);
721 : : }
722 : : else
723 : : {
724 : : /* No such entry, or bootstrap mode: print numeric OID */
7616 tgl@sss.pgh.pa.us 725 :UBC 0 : sprintf(p, "%u", aip->ai_grantor);
726 : : }
727 : :
9409 tgl@sss.pgh.pa.us 728 :CBC 719323 : PG_RETURN_CSTRING(out);
729 : : }
730 : :
731 : : /*
732 : : * aclitem_match
733 : : * Two AclItems are considered to match iff they have the same
734 : : * grantee and grantor; the privileges are ignored.
735 : : */
736 : : static bool
8348 737 : 16829 : aclitem_match(const AclItem *a1, const AclItem *a2)
738 : : {
7616 739 [ + + ]: 20684 : return a1->ai_grantee == a2->ai_grantee &&
8503 peter_e@gmx.net 740 [ + + ]: 3855 : a1->ai_grantor == a2->ai_grantor;
741 : : }
742 : :
743 : : /*
744 : : * aclitemComparator
745 : : * qsort comparison function for AclItems
746 : : */
747 : : static int
6056 tgl@sss.pgh.pa.us 748 : 183 : aclitemComparator(const void *arg1, const void *arg2)
749 : : {
750 : 183 : const AclItem *a1 = (const AclItem *) arg1;
751 : 183 : const AclItem *a2 = (const AclItem *) arg2;
752 : :
753 [ + + ]: 183 : if (a1->ai_grantee > a2->ai_grantee)
754 : 28 : return 1;
755 [ + - ]: 155 : if (a1->ai_grantee < a2->ai_grantee)
756 : 155 : return -1;
6056 tgl@sss.pgh.pa.us 757 [ # # ]:UBC 0 : if (a1->ai_grantor > a2->ai_grantor)
758 : 0 : return 1;
759 [ # # ]: 0 : if (a1->ai_grantor < a2->ai_grantor)
760 : 0 : return -1;
761 [ # # ]: 0 : if (a1->ai_privs > a2->ai_privs)
762 : 0 : return 1;
763 [ # # ]: 0 : if (a1->ai_privs < a2->ai_privs)
764 : 0 : return -1;
765 : 0 : return 0;
766 : : }
767 : :
768 : : /*
769 : : * aclitem equality operator
770 : : */
771 : : Datum
8348 tgl@sss.pgh.pa.us 772 :CBC 136957 : aclitem_eq(PG_FUNCTION_ARGS)
773 : : {
8310 bruce@momjian.us 774 : 136957 : AclItem *a1 = PG_GETARG_ACLITEM_P(0);
775 : 136957 : AclItem *a2 = PG_GETARG_ACLITEM_P(1);
776 : : bool result;
777 : :
8348 tgl@sss.pgh.pa.us 778 : 400695 : result = a1->ai_privs == a2->ai_privs &&
779 [ + + + + ]: 260653 : a1->ai_grantee == a2->ai_grantee &&
780 [ + - ]: 123696 : a1->ai_grantor == a2->ai_grantor;
781 : 136957 : PG_RETURN_BOOL(result);
782 : : }
783 : :
784 : : /*
785 : : * aclitem hash function
786 : : *
787 : : * We make aclitems hashable not so much because anyone is likely to hash
788 : : * them, as because we want array equality to work on aclitem arrays, and
789 : : * with the typcache mechanism we must have a hash or btree opclass.
790 : : */
791 : : Datum
8297 792 : 16210 : hash_aclitem(PG_FUNCTION_ARGS)
793 : : {
794 : 16210 : AclItem *a = PG_GETARG_ACLITEM_P(0);
795 : :
796 : : /* not very bright, but avoids any issue of padding in struct */
797 : 16210 : PG_RETURN_UINT32((uint32) (a->ai_privs + a->ai_grantee + a->ai_grantor));
798 : : }
799 : :
800 : : /*
801 : : * 64-bit hash function for aclitem.
802 : : *
803 : : * Similar to hash_aclitem, but accepts a seed and returns a uint64 value.
804 : : */
805 : : Datum
3169 rhaas@postgresql.org 806 : 8 : hash_aclitem_extended(PG_FUNCTION_ARGS)
807 : : {
808 : 8 : AclItem *a = PG_GETARG_ACLITEM_P(0);
809 : 8 : uint64 seed = PG_GETARG_INT64(1);
810 : 8 : uint32 sum = (uint32) (a->ai_privs + a->ai_grantee + a->ai_grantor);
811 : :
812 [ + + ]: 8 : return (seed == 0) ? UInt64GetDatum(sum) : hash_uint32_extended(sum, seed);
813 : : }
814 : :
815 : : /*
816 : : * acldefault() --- create an ACL describing default access permissions
817 : : *
818 : : * Change this routine if you want to alter the default access policy for
819 : : * newly-created objects (or any object with a NULL acl entry). When
820 : : * you make a change here, don't forget to update the GRANT man page,
821 : : * which explains all the default permissions.
822 : : *
823 : : * Note that these are the hard-wired "defaults" that are used in the
824 : : * absence of any pg_default_acl entry.
825 : : */
826 : : Acl *
3128 peter_e@gmx.net 827 : 385247 : acldefault(ObjectType objtype, Oid ownerId)
828 : : {
829 : : AclMode world_default;
830 : : AclMode owner_default;
831 : : int nacl;
832 : : Acl *acl;
833 : : AclItem *aip;
834 : :
8780 tgl@sss.pgh.pa.us 835 [ + + + + : 385247 : switch (objtype)
+ + + + +
+ + + + +
- ]
836 : : {
3128 peter_e@gmx.net 837 : 27894 : case OBJECT_COLUMN:
838 : : /* by default, columns have no extra privileges */
6312 tgl@sss.pgh.pa.us 839 : 27894 : world_default = ACL_NO_RIGHTS;
840 : 27894 : owner_default = ACL_NO_RIGHTS;
841 : 27894 : break;
3128 peter_e@gmx.net 842 : 118625 : case OBJECT_TABLE:
8780 tgl@sss.pgh.pa.us 843 : 118625 : world_default = ACL_NO_RIGHTS;
844 : 118625 : owner_default = ACL_ALL_RIGHTS_RELATION;
845 : 118625 : break;
3128 peter_e@gmx.net 846 : 805 : case OBJECT_SEQUENCE:
7409 bruce@momjian.us 847 : 805 : world_default = ACL_NO_RIGHTS;
848 : 805 : owner_default = ACL_ALL_RIGHTS_SEQUENCE;
849 : 805 : break;
3128 peter_e@gmx.net 850 : 887 : case OBJECT_DATABASE:
851 : : /* for backwards compatibility, grant some rights by default */
7310 tgl@sss.pgh.pa.us 852 : 887 : world_default = ACL_CREATE_TEMP | ACL_CONNECT;
8780 853 : 887 : owner_default = ACL_ALL_RIGHTS_DATABASE;
854 : 887 : break;
3128 peter_e@gmx.net 855 : 36177 : case OBJECT_FUNCTION:
856 : : /* Grant EXECUTE by default, for now */
8624 tgl@sss.pgh.pa.us 857 : 36177 : world_default = ACL_EXECUTE;
8780 858 : 36177 : owner_default = ACL_ALL_RIGHTS_FUNCTION;
859 : 36177 : break;
3128 peter_e@gmx.net 860 : 518 : case OBJECT_LANGUAGE:
861 : : /* Grant USAGE by default, for now */
8624 tgl@sss.pgh.pa.us 862 : 518 : world_default = ACL_USAGE;
8780 863 : 518 : owner_default = ACL_ALL_RIGHTS_LANGUAGE;
864 : 518 : break;
3128 peter_e@gmx.net 865 : 219 : case OBJECT_LARGEOBJECT:
5989 itagaki.takahiro@gma 866 : 219 : world_default = ACL_NO_RIGHTS;
867 : 219 : owner_default = ACL_ALL_RIGHTS_LARGEOBJECT;
868 : 219 : break;
3128 peter_e@gmx.net 869 : 2295 : case OBJECT_SCHEMA:
8780 tgl@sss.pgh.pa.us 870 : 2295 : world_default = ACL_NO_RIGHTS;
3128 peter_e@gmx.net 871 : 2295 : owner_default = ACL_ALL_RIGHTS_SCHEMA;
8780 tgl@sss.pgh.pa.us 872 : 2295 : break;
3128 peter_e@gmx.net 873 : 44 : case OBJECT_TABLESPACE:
7991 tgl@sss.pgh.pa.us 874 : 44 : world_default = ACL_NO_RIGHTS;
875 : 44 : owner_default = ACL_ALL_RIGHTS_TABLESPACE;
876 : 44 : break;
3128 peter_e@gmx.net 877 : 86 : case OBJECT_FDW:
6346 878 : 86 : world_default = ACL_NO_RIGHTS;
879 : 86 : owner_default = ACL_ALL_RIGHTS_FDW;
880 : 86 : break;
3128 881 : 177 : case OBJECT_FOREIGN_SERVER:
6346 882 : 177 : world_default = ACL_NO_RIGHTS;
883 : 177 : owner_default = ACL_ALL_RIGHTS_FOREIGN_SERVER;
884 : 177 : break;
3128 885 : 197356 : case OBJECT_DOMAIN:
886 : : case OBJECT_TYPE:
5250 887 : 197356 : world_default = ACL_USAGE;
888 : 197356 : owner_default = ACL_ALL_RIGHTS_TYPE;
889 : 197356 : break;
1490 tgl@sss.pgh.pa.us 890 : 142 : case OBJECT_PARAMETER_ACL:
891 : 142 : world_default = ACL_NO_RIGHTS;
892 : 142 : owner_default = ACL_ALL_RIGHTS_PARAMETER_ACL;
893 : 142 : break;
50 peter@eisentraut.org 894 :GNC 22 : case OBJECT_PROPGRAPH:
895 : 22 : world_default = ACL_NO_RIGHTS;
896 : 22 : owner_default = ACL_ALL_RIGHTS_PROPGRAPH;
897 : 22 : break;
8780 tgl@sss.pgh.pa.us 898 :UBC 0 : default:
1274 peter@eisentraut.org 899 [ # # ]: 0 : elog(ERROR, "unrecognized object type: %d", (int) objtype);
900 : : world_default = ACL_NO_RIGHTS; /* keep compiler quiet */
901 : : owner_default = ACL_NO_RIGHTS;
902 : : break;
903 : : }
904 : :
6312 tgl@sss.pgh.pa.us 905 :CBC 385247 : nacl = 0;
906 [ + + ]: 385247 : if (world_default != ACL_NO_RIGHTS)
907 : 234938 : nacl++;
908 [ + + ]: 385247 : if (owner_default != ACL_NO_RIGHTS)
909 : 357353 : nacl++;
910 : :
911 : 385247 : acl = allocacl(nacl);
10467 bruce@momjian.us 912 [ - + ]: 385247 : aip = ACL_DAT(acl);
913 : :
8503 peter_e@gmx.net 914 [ + + ]: 385247 : if (world_default != ACL_NO_RIGHTS)
915 : : {
7616 tgl@sss.pgh.pa.us 916 : 234938 : aip->ai_grantee = ACL_ID_PUBLIC;
917 : 234938 : aip->ai_grantor = ownerId;
918 : 234938 : ACLITEM_SET_PRIVS_GOPTIONS(*aip, world_default, ACL_NO_RIGHTS);
8224 919 : 234938 : 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 : : */
6312 932 [ + + ]: 385247 : if (owner_default != ACL_NO_RIGHTS)
933 : : {
934 : 357353 : aip->ai_grantee = ownerId;
935 : 357353 : aip->ai_grantor = ownerId;
936 : 357353 : ACLITEM_SET_PRIVS_GOPTIONS(*aip, owner_default, ACL_NO_RIGHTS);
937 : : }
938 : :
10108 bruce@momjian.us 939 : 385247 : return acl;
940 : : }
941 : :
942 : :
943 : : /*
944 : : * SQL-accessible version of acldefault(). Hackish mapping from "char" type to
945 : : * OBJECT_* values.
946 : : */
947 : : Datum
5212 peter_e@gmx.net 948 : 304874 : acldefault_sql(PG_FUNCTION_ARGS)
949 : : {
5077 bruce@momjian.us 950 : 304874 : char objtypec = PG_GETARG_CHAR(0);
951 : 304874 : Oid owner = PG_GETARG_OID(1);
3128 peter_e@gmx.net 952 : 304874 : ObjectType objtype = 0;
953 : :
5212 954 [ - + + + : 304874 : switch (objtypec)
+ + + + +
+ + + +
- ]
955 : : {
5212 peter_e@gmx.net 956 :UBC 0 : case 'c':
3128 957 : 0 : objtype = OBJECT_COLUMN;
5212 958 : 0 : break;
5212 peter_e@gmx.net 959 :CBC 101189 : case 'r':
3128 960 : 101189 : objtype = OBJECT_TABLE;
5212 961 : 101189 : break;
962 : 650 : case 's':
3128 963 : 650 : objtype = OBJECT_SEQUENCE;
5212 964 : 650 : break;
965 : 162 : case 'd':
3128 966 : 162 : objtype = OBJECT_DATABASE;
5212 967 : 162 : break;
968 : 6137 : case 'f':
3128 969 : 6137 : objtype = OBJECT_FUNCTION;
5212 970 : 6137 : break;
971 : 306 : case 'l':
3128 972 : 306 : objtype = OBJECT_LANGUAGE;
5212 973 : 306 : break;
974 : 101 : case 'L':
3128 975 : 101 : objtype = OBJECT_LARGEOBJECT;
5212 976 : 101 : break;
977 : 1787 : case 'n':
3128 978 : 1787 : objtype = OBJECT_SCHEMA;
5212 979 : 1787 : break;
1490 tgl@sss.pgh.pa.us 980 : 41 : case 'p':
981 : 41 : objtype = OBJECT_PARAMETER_ACL;
982 : 41 : break;
5212 peter_e@gmx.net 983 : 32 : case 't':
3128 984 : 32 : objtype = OBJECT_TABLESPACE;
5212 985 : 32 : break;
986 : 70 : case 'F':
3128 987 : 70 : objtype = OBJECT_FDW;
5212 988 : 70 : break;
989 : 72 : case 'S':
3128 990 : 72 : objtype = OBJECT_FOREIGN_SERVER;
5212 991 : 72 : break;
992 : 194327 : case 'T':
3128 993 : 194327 : objtype = OBJECT_TYPE;
5212 994 : 194327 : break;
5212 peter_e@gmx.net 995 :UBC 0 : default:
1274 peter@eisentraut.org 996 [ # # ]: 0 : elog(ERROR, "unrecognized object type abbreviation: %c", objtypec);
997 : : }
998 : :
5212 peter_e@gmx.net 999 :CBC 304874 : 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 : : */
1019 : : Acl *
8008 tgl@sss.pgh.pa.us 1020 : 41056 : aclupdate(const Acl *old_acl, const AclItem *mod_aip,
1021 : : int modechg, Oid ownerId, DropBehavior behavior)
1022 : : {
8502 1023 : 41056 : Acl *new_acl = NULL;
1024 : : AclItem *old_aip,
1025 : 41056 : *new_aip = NULL;
1026 : : AclMode old_rights,
1027 : : old_goptions,
1028 : : new_rights,
1029 : : new_goptions;
1030 : : int dst,
1031 : : num;
1032 : :
1033 : : /* Caller probably already checked old_acl, but be safe */
7473 1034 : 41056 : check_acl(old_acl);
1035 : :
1036 : : /* If granting grant options, check for circularity */
8008 1037 [ + + ]: 41056 : if (modechg != ACL_MODECHG_DEL &&
1038 [ + + ]: 10067 : ACLITEM_GET_GOPTIONS(*mod_aip) != ACL_NO_RIGHTS)
7616 1039 : 77 : check_circularity(old_acl, mod_aip, ownerId);
1040 : :
10467 bruce@momjian.us 1041 : 41056 : num = ACL_NUM(old_acl);
1042 [ - + ]: 41056 : old_aip = ACL_DAT(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 : :
8503 peter_e@gmx.net 1051 [ + + ]: 54050 : for (dst = 0; dst < num; ++dst)
1052 : : {
8348 tgl@sss.pgh.pa.us 1053 [ + + ]: 16816 : if (aclitem_match(mod_aip, old_aip + dst))
1054 : : {
1055 : : /* found a match, so modify existing item */
8364 peter_e@gmx.net 1056 : 3822 : new_acl = allocacl(num);
8503 1057 [ - + ]: 3822 : new_aip = ACL_DAT(new_acl);
1058 : 3822 : memcpy(new_acl, old_acl, ACL_SIZE(old_acl));
1059 : 3822 : break;
1060 : : }
1061 : : }
1062 : :
1063 [ + + ]: 41056 : if (dst == num)
1064 : : {
1065 : : /* need to append a new item */
8364 1066 : 37234 : new_acl = allocacl(num + 1);
10467 bruce@momjian.us 1067 [ - + ]: 37234 : new_aip = ACL_DAT(new_acl);
8503 peter_e@gmx.net 1068 : 37234 : memcpy(new_aip, old_aip, num * sizeof(AclItem));
1069 : :
1070 : : /* initialize the new entry with no permissions */
1071 : 37234 : new_aip[dst].ai_grantee = mod_aip->ai_grantee;
1072 : 37234 : new_aip[dst].ai_grantor = mod_aip->ai_grantor;
7616 tgl@sss.pgh.pa.us 1073 : 37234 : ACLITEM_SET_PRIVS_GOPTIONS(new_aip[dst],
1074 : : ACL_NO_RIGHTS, ACL_NO_RIGHTS);
10467 bruce@momjian.us 1075 : 37234 : num++; /* set num to the size of new_acl */
1076 : : }
1077 : :
8008 tgl@sss.pgh.pa.us 1078 : 41056 : old_rights = ACLITEM_GET_RIGHTS(new_aip[dst]);
8224 1079 : 41056 : old_goptions = ACLITEM_GET_GOPTIONS(new_aip[dst]);
1080 : :
1081 : : /* apply the specified permissions change */
10467 bruce@momjian.us 1082 [ + + - - ]: 41056 : switch (modechg)
1083 : : {
10466 1084 : 10067 : case ACL_MODECHG_ADD:
8008 tgl@sss.pgh.pa.us 1085 : 10067 : ACLITEM_SET_RIGHTS(new_aip[dst],
1086 : : old_rights | ACLITEM_GET_RIGHTS(*mod_aip));
10466 bruce@momjian.us 1087 : 10067 : break;
1088 : 30989 : case ACL_MODECHG_DEL:
8008 tgl@sss.pgh.pa.us 1089 : 30989 : ACLITEM_SET_RIGHTS(new_aip[dst],
1090 : : old_rights & ~ACLITEM_GET_RIGHTS(*mod_aip));
10466 bruce@momjian.us 1091 : 30989 : break;
10466 bruce@momjian.us 1092 :UBC 0 : case ACL_MODECHG_EQL:
8008 tgl@sss.pgh.pa.us 1093 : 0 : ACLITEM_SET_RIGHTS(new_aip[dst],
1094 : : ACLITEM_GET_RIGHTS(*mod_aip));
10466 bruce@momjian.us 1095 : 0 : break;
1096 : : }
1097 : :
8008 tgl@sss.pgh.pa.us 1098 :CBC 41056 : new_rights = ACLITEM_GET_RIGHTS(new_aip[dst]);
8224 1099 : 41056 : new_goptions = ACLITEM_GET_GOPTIONS(new_aip[dst]);
1100 : :
1101 : : /*
1102 : : * If the adjusted entry has no permissions, delete it from the list.
1103 : : */
8008 1104 [ + + ]: 41056 : if (new_rights == ACL_NO_RIGHTS)
1105 : : {
8503 peter_e@gmx.net 1106 : 30795 : memmove(new_aip + dst,
1107 : 30795 : new_aip + dst + 1,
9100 tgl@sss.pgh.pa.us 1108 : 30795 : (num - dst - 1) * sizeof(AclItem));
1109 : : /* Adjust array size to be 'num - 1' items */
9409 1110 : 30795 : ARR_DIMS(new_acl)[0] = num - 1;
7007 1111 : 30795 : 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 : : */
8008 1118 [ + + ]: 41056 : if ((old_goptions & ~new_goptions) != 0)
1119 : : {
7616 1120 [ - + ]: 59 : Assert(mod_aip->ai_grantee != ACL_ID_PUBLIC);
8224 1121 : 59 : new_acl = recursive_revoke(new_acl, mod_aip->ai_grantee,
1122 : 59 : (old_goptions & ~new_goptions),
1123 : : ownerId, behavior);
1124 : : }
1125 : :
10108 bruce@momjian.us 1126 : 41048 : 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 : : */
1146 : : Acl *
7616 tgl@sss.pgh.pa.us 1147 : 92 : aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
1148 : : {
1149 : : Acl *new_acl;
1150 : : AclItem *new_aip;
1151 : : AclItem *old_aip;
1152 : : AclItem *dst_aip;
1153 : : AclItem *src_aip;
1154 : : AclItem *targ_aip;
7947 1155 : 92 : bool newpresent = false;
1156 : : int dst,
1157 : : src,
1158 : : targ,
1159 : : num;
1160 : :
7473 1161 : 92 : check_acl(old_acl);
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 : : */
7947 1168 : 92 : num = ACL_NUM(old_acl);
1169 [ - + ]: 92 : old_aip = ACL_DAT(old_acl);
1170 : 92 : new_acl = allocacl(num);
1171 [ - + ]: 92 : new_aip = ACL_DAT(new_acl);
1172 : 92 : memcpy(new_aip, old_aip, num * sizeof(AclItem));
1173 [ + + ]: 261 : for (dst = 0, dst_aip = new_aip; dst < num; dst++, dst_aip++)
1174 : : {
7616 1175 [ + - ]: 169 : if (dst_aip->ai_grantor == oldOwnerId)
1176 : 169 : dst_aip->ai_grantor = newOwnerId;
7616 tgl@sss.pgh.pa.us 1177 [ # # ]:UBC 0 : else if (dst_aip->ai_grantor == newOwnerId)
1178 : 0 : newpresent = true;
7616 tgl@sss.pgh.pa.us 1179 [ + + ]:CBC 169 : if (dst_aip->ai_grantee == oldOwnerId)
1180 : 90 : dst_aip->ai_grantee = newOwnerId;
1181 [ + + ]: 79 : else if (dst_aip->ai_grantee == newOwnerId)
7947 1182 : 13 : 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 [ + + ]: 92 : if (newpresent)
1202 : : {
1203 : 13 : dst = 0;
1204 [ + + ]: 39 : for (targ = 0, targ_aip = new_aip; targ < num; targ++, targ_aip++)
1205 : : {
1206 : : /* ignore if deleted in an earlier pass */
1207 [ + + ]: 26 : if (ACLITEM_GET_RIGHTS(*targ_aip) == ACL_NO_RIGHTS)
1208 : 13 : continue;
1209 : : /* find and merge any duplicates */
1210 [ + + ]: 26 : for (src = targ + 1, src_aip = targ_aip + 1; src < num;
1211 : 13 : src++, src_aip++)
1212 : : {
1213 [ - + ]: 13 : if (ACLITEM_GET_RIGHTS(*src_aip) == ACL_NO_RIGHTS)
7947 tgl@sss.pgh.pa.us 1214 :UBC 0 : continue;
7947 tgl@sss.pgh.pa.us 1215 [ + - ]:CBC 13 : if (aclitem_match(targ_aip, src_aip))
1216 : : {
1217 : 13 : ACLITEM_SET_RIGHTS(*targ_aip,
1218 : : ACLITEM_GET_RIGHTS(*targ_aip) |
1219 : : ACLITEM_GET_RIGHTS(*src_aip));
1220 : : /* mark the duplicate deleted */
1221 : 13 : ACLITEM_SET_RIGHTS(*src_aip, ACL_NO_RIGHTS);
1222 : : }
1223 : : }
1224 : : /* and emit to output */
1225 : 13 : new_aip[dst] = *targ_aip;
1226 : 13 : dst++;
1227 : : }
1228 : : /* Adjust array size to be 'dst' items */
1229 : 13 : ARR_DIMS(new_acl)[0] = dst;
7007 1230 : 13 : SET_VARSIZE(new_acl, ACL_N_SIZE(dst));
1231 : : }
1232 : :
7947 1233 : 92 : 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 : : */
1249 : : static void
8008 1250 : 77 : check_circularity(const Acl *old_acl, const AclItem *mod_aip,
1251 : : Oid ownerId)
1252 : : {
1253 : : Acl *acl;
1254 : : AclItem *aip;
1255 : : int i,
1256 : : num;
1257 : : AclMode own_privs;
1258 : :
7473 1259 : 77 : check_acl(old_acl);
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 : : */
7616 1265 [ - + ]: 77 : Assert(mod_aip->ai_grantee != ACL_ID_PUBLIC);
1266 : :
1267 : : /* The owner always has grant options, no need to check */
1268 [ + + ]: 77 : if (mod_aip->ai_grantor == ownerId)
8008 1269 : 65 : return;
1270 : :
1271 : : /* Make a working copy */
1272 : 12 : acl = allocacl(ACL_NUM(old_acl));
1273 : 12 : memcpy(acl, old_acl, ACL_SIZE(old_acl));
1274 : :
1275 : : /* Zap all grant options of target grantee, plus what depends on 'em */
1276 : 16 : cc_restart:
1277 : 16 : num = ACL_NUM(acl);
1278 [ - + ]: 16 : aip = ACL_DAT(acl);
1279 [ + + ]: 64 : for (i = 0; i < num; i++)
1280 : : {
7616 1281 [ + + ]: 52 : if (aip[i].ai_grantee == mod_aip->ai_grantee &&
8008 1282 [ + - ]: 4 : ACLITEM_GET_GOPTIONS(aip[i]) != ACL_NO_RIGHTS)
1283 : : {
1284 : : Acl *new_acl;
1285 : :
1286 : : /* We'll actually zap ordinary privs too, but no matter */
1287 : 4 : new_acl = aclupdate(acl, &aip[i], ACL_MODECHG_DEL,
1288 : : ownerId, DROP_CASCADE);
1289 : :
1290 : 4 : pfree(acl);
1291 : 4 : acl = new_acl;
1292 : :
1293 : 4 : goto cc_restart;
1294 : : }
1295 : : }
1296 : :
1297 : : /* Now we can compute grantor's independently-derived privileges */
1298 : 12 : own_privs = aclmask(acl,
1299 : 12 : mod_aip->ai_grantor,
1300 : : ownerId,
7507 bruce@momjian.us 1301 : 12 : ACL_GRANT_OPTION_FOR(ACLITEM_GET_GOPTIONS(*mod_aip)),
1302 : : ACLMASK_ALL);
8008 tgl@sss.pgh.pa.us 1303 : 12 : own_privs = ACL_OPTION_TO_PRIVS(own_privs);
1304 : :
1305 [ - + ]: 12 : if ((ACLITEM_GET_GOPTIONS(*mod_aip) & ~own_privs) != 0)
8008 tgl@sss.pgh.pa.us 1306 [ # # ]:UBC 0 : ereport(ERROR,
1307 : : (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1308 : : errmsg("grant options cannot be granted back to your own grantor")));
1309 : :
8008 tgl@sss.pgh.pa.us 1310 :CBC 12 : 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 : : */
1329 : : static Acl *
8503 peter_e@gmx.net 1330 : 59 : recursive_revoke(Acl *acl,
1331 : : Oid grantee,
1332 : : AclMode revoke_privs,
1333 : : Oid ownerId,
1334 : : DropBehavior behavior)
1335 : : {
1336 : : AclMode still_has;
1337 : : AclItem *aip;
1338 : : int i,
1339 : : num;
1340 : :
7473 tgl@sss.pgh.pa.us 1341 : 59 : check_acl(acl);
1342 : :
1343 : : /* The owner can never truly lose grant options, so short-circuit */
7616 1344 [ - + ]: 59 : if (grantee == ownerId)
8008 tgl@sss.pgh.pa.us 1345 :UBC 0 : return acl;
1346 : :
1347 : : /* The grantee might still have some grant options via another grantor */
7616 tgl@sss.pgh.pa.us 1348 :CBC 59 : still_has = aclmask(acl, grantee, ownerId,
1349 : : ACL_GRANT_OPTION_FOR(revoke_privs),
1350 : : ACLMASK_ALL);
5003 1351 : 59 : revoke_privs &= ~ACL_OPTION_TO_PRIVS(still_has);
8008 1352 [ + + ]: 59 : if (revoke_privs == ACL_NO_RIGHTS)
1353 : 4 : return acl;
1354 : :
8503 peter_e@gmx.net 1355 : 55 : restart:
8008 tgl@sss.pgh.pa.us 1356 : 79 : num = ACL_NUM(acl);
1357 [ - + ]: 79 : aip = ACL_DAT(acl);
1358 [ + + ]: 256 : for (i = 0; i < num; i++)
1359 : : {
8503 peter_e@gmx.net 1360 [ + + ]: 209 : if (aip[i].ai_grantor == grantee
1361 [ + - ]: 32 : && (ACLITEM_GET_PRIVS(aip[i]) & revoke_privs) != 0)
1362 : : {
1363 : : AclItem mod_acl;
1364 : : Acl *new_acl;
1365 : :
1366 [ + + ]: 32 : if (behavior == DROP_RESTRICT)
8318 tgl@sss.pgh.pa.us 1367 [ + - ]: 8 : ereport(ERROR,
1368 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1369 : : errmsg("dependent privileges exist"),
1370 : : errhint("Use CASCADE to revoke them too.")));
1371 : :
8503 peter_e@gmx.net 1372 : 24 : mod_acl.ai_grantor = grantee;
1373 : 24 : mod_acl.ai_grantee = aip[i].ai_grantee;
7616 tgl@sss.pgh.pa.us 1374 : 24 : ACLITEM_SET_PRIVS_GOPTIONS(mod_acl,
1375 : : revoke_privs,
1376 : : revoke_privs);
1377 : :
8008 1378 : 24 : new_acl = aclupdate(acl, &mod_acl, ACL_MODECHG_DEL,
1379 : : ownerId, behavior);
1380 : :
1381 : 24 : pfree(acl);
1382 : 24 : acl = new_acl;
1383 : :
8503 peter_e@gmx.net 1384 : 24 : goto restart;
1385 : : }
1386 : : }
1387 : :
1388 : 47 : 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 : : */
1415 : : AclMode
7616 tgl@sss.pgh.pa.us 1416 : 95276 : aclmask(const Acl *acl, Oid roleid, Oid ownerId,
1417 : : AclMode mask, AclMaskHow how)
1418 : : {
1419 : : AclMode result;
1420 : : AclMode remaining;
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 : : */
8008 1429 [ - + ]: 95276 : if (acl == NULL)
8008 tgl@sss.pgh.pa.us 1430 [ # # ]:UBC 0 : elog(ERROR, "null ACL");
1431 : :
7473 tgl@sss.pgh.pa.us 1432 :CBC 95276 : check_acl(acl);
1433 : :
1434 : : /* Quick exit for mask == 0 */
8008 1435 [ + + ]: 95276 : if (mask == 0)
1436 : 46 : return 0;
1437 : :
1438 : 95230 : result = 0;
1439 : :
1440 : : /* Owner always implicitly has all grant options */
7515 1441 [ + + + + ]: 95354 : if ((mask & ACLITEM_ALL_GOPTION_BITS) &&
1442 : 124 : has_privs_of_role(roleid, ownerId))
1443 : : {
8008 1444 : 4 : result = mask & ACLITEM_ALL_GOPTION_BITS;
7515 1445 [ - + + - ]: 4 : if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
8008 1446 : 4 : return result;
1447 : : }
1448 : :
1449 : 95226 : num = ACL_NUM(acl);
1450 [ - + ]: 95226 : aidat = ACL_DAT(acl);
1451 : :
1452 : : /*
1453 : : * Check privileges granted directly to roleid or to public
1454 : : */
1455 [ + + ]: 149093 : for (i = 0; i < num; i++)
1456 : : {
7919 bruce@momjian.us 1457 : 136919 : AclItem *aidata = &aidat[i];
1458 : :
7616 tgl@sss.pgh.pa.us 1459 [ + + ]: 136919 : if (aidata->ai_grantee == ACL_ID_PUBLIC ||
7615 1460 [ + + ]: 64365 : aidata->ai_grantee == roleid)
1461 : : {
7593 bruce@momjian.us 1462 : 85159 : result |= aidata->ai_privs & mask;
8008 tgl@sss.pgh.pa.us 1463 [ + + + + ]: 85159 : if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1464 : 83052 : 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 : : */
7593 bruce@momjian.us 1475 : 12174 : remaining = mask & ~result;
7615 tgl@sss.pgh.pa.us 1476 [ + + ]: 27365 : for (i = 0; i < num; i++)
1477 : : {
1478 : 15380 : AclItem *aidata = &aidat[i];
1479 : :
1480 [ + + ]: 15380 : if (aidata->ai_grantee == ACL_ID_PUBLIC ||
1481 [ + + ]: 14779 : aidata->ai_grantee == roleid)
1482 : 2007 : continue; /* already checked it */
1483 : :
1484 [ + + + + ]: 26023 : if ((aidata->ai_privs & remaining) &&
7588 1485 : 12650 : has_privs_of_role(roleid, aidata->ai_grantee))
1486 : : {
7593 bruce@momjian.us 1487 : 189 : result |= aidata->ai_privs & mask;
7615 tgl@sss.pgh.pa.us 1488 [ + + + - ]: 189 : if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1489 : 189 : return result;
7593 bruce@momjian.us 1490 :UBC 0 : remaining = mask & ~result;
1491 : : }
1492 : : }
1493 : :
7615 tgl@sss.pgh.pa.us 1494 :CBC 11985 : 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 : : */
1504 : : static AclMode
7512 1505 : 380 : aclmask_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 [ - + ]: 380 : if (acl == NULL)
7512 tgl@sss.pgh.pa.us 1518 [ # # ]:UBC 0 : elog(ERROR, "null ACL");
1519 : :
7473 tgl@sss.pgh.pa.us 1520 :CBC 380 : check_acl(acl);
1521 : :
1522 : : /* Quick exit for mask == 0 */
7512 1523 [ - + ]: 380 : if (mask == 0)
7512 tgl@sss.pgh.pa.us 1524 :UBC 0 : return 0;
1525 : :
7512 tgl@sss.pgh.pa.us 1526 :CBC 380 : result = 0;
1527 : :
1528 : : /* Owner always implicitly has all grant options */
1529 [ + - + + ]: 380 : if ((mask & ACLITEM_ALL_GOPTION_BITS) &&
1530 : : roleid == ownerId)
1531 : : {
7512 tgl@sss.pgh.pa.us 1532 :GBC 8 : result = mask & ACLITEM_ALL_GOPTION_BITS;
1533 [ + - + - ]: 8 : if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1534 : 8 : return result;
1535 : : }
1536 : :
7512 tgl@sss.pgh.pa.us 1537 :CBC 372 : num = ACL_NUM(acl);
1538 [ - + ]: 372 : aidat = ACL_DAT(acl);
1539 : :
1540 : : /*
1541 : : * Check privileges granted directly to roleid (and not to public)
1542 : : */
1543 [ + + ]: 1616 : for (i = 0; i < num; i++)
1544 : : {
1545 : 1384 : AclItem *aidata = &aidat[i];
1546 : :
1547 [ + + ]: 1384 : if (aidata->ai_grantee == roleid)
1548 : : {
1549 : 296 : result |= aidata->ai_privs & mask;
1550 [ + - + + ]: 296 : if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
1551 : 140 : return result;
1552 : : }
1553 : : }
1554 : :
1555 : 232 : 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 : : */
1567 : : int
7607 1568 : 49312 : aclmembers(const Acl *acl, Oid **roleids)
1569 : : {
1570 : : Oid *list;
1571 : : const AclItem *acldat;
1572 : : int i,
1573 : : j;
1574 : :
1575 [ + + + + ]: 49312 : if (acl == NULL || ACL_NUM(acl) == 0)
1576 : : {
1577 : 27949 : *roleids = NULL;
1578 : 27949 : return 0;
1579 : : }
1580 : :
7473 1581 : 21363 : check_acl(acl);
1582 : :
1583 : : /* Allocate the worst-case space requirement */
7607 1584 : 21363 : list = palloc(ACL_NUM(acl) * 2 * sizeof(Oid));
1585 [ - + ]: 21363 : acldat = ACL_DAT(acl);
1586 : :
1587 : : /*
1588 : : * Walk the ACL collecting mentioned RoleIds.
1589 : : */
1590 : 21363 : j = 0;
1591 [ + + ]: 55945 : for (i = 0; i < ACL_NUM(acl); i++)
1592 : : {
1593 : 34582 : const AclItem *ai = &acldat[i];
1594 : :
1595 [ + + ]: 34582 : if (ai->ai_grantee != ACL_ID_PUBLIC)
1596 : 23647 : list[j++] = ai->ai_grantee;
1597 : : /* grantor is currently never PUBLIC, but let's check anyway */
1598 [ + - ]: 34582 : if (ai->ai_grantor != ACL_ID_PUBLIC)
1599 : 34582 : list[j++] = ai->ai_grantor;
1600 : : }
1601 : :
1602 : : /* Sort the array */
3352 peter_e@gmx.net 1603 : 21363 : 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 : : */
7607 tgl@sss.pgh.pa.us 1609 : 21363 : *roleids = list;
1610 : :
1611 : : /* Remove duplicates from the array */
2371 tmunro@postgresql.or 1612 : 21363 : return qunique(list, j, sizeof(Oid), oid_cmp);
1613 : : }
1614 : :
1615 : :
1616 : : /*
1617 : : * aclinsert (exported function)
1618 : : */
1619 : : Datum
8008 tgl@sss.pgh.pa.us 1620 :UBC 0 : aclinsert(PG_FUNCTION_ARGS)
1621 : : {
1622 [ # # ]: 0 : ereport(ERROR,
1623 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1624 : : errmsg("aclinsert is no longer supported")));
1625 : :
1626 : : PG_RETURN_NULL(); /* keep compiler quiet */
1627 : : }
1628 : :
1629 : : Datum
1630 : 0 : aclremove(PG_FUNCTION_ARGS)
1631 : : {
1632 [ # # ]: 0 : ereport(ERROR,
1633 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1634 : : errmsg("aclremove is no longer supported")));
1635 : :
1636 : : PG_RETURN_NULL(); /* keep compiler quiet */
1637 : : }
1638 : :
1639 : : Datum
9409 1640 : 0 : aclcontains(PG_FUNCTION_ARGS)
1641 : : {
1642 : 0 : Acl *acl = PG_GETARG_ACL_P(0);
9175 bruce@momjian.us 1643 : 0 : AclItem *aip = PG_GETARG_ACLITEM_P(1);
1644 : : AclItem *aidat;
1645 : : int i,
1646 : : num;
1647 : :
7473 tgl@sss.pgh.pa.us 1648 : 0 : check_acl(acl);
9409 1649 : 0 : num = ACL_NUM(acl);
10467 bruce@momjian.us 1650 [ # # ]: 0 : aidat = ACL_DAT(acl);
1651 [ # # ]: 0 : for (i = 0; i < num; ++i)
1652 : : {
7616 tgl@sss.pgh.pa.us 1653 [ # # ]: 0 : if (aip->ai_grantee == aidat[i].ai_grantee &&
1654 [ # # ]: 0 : aip->ai_grantor == aidat[i].ai_grantor &&
1655 [ # # ]: 0 : (ACLITEM_GET_RIGHTS(*aip) & ACLITEM_GET_RIGHTS(aidat[i])) == ACLITEM_GET_RIGHTS(*aip))
9409 1656 : 0 : PG_RETURN_BOOL(true);
1657 : : }
1658 : 0 : PG_RETURN_BOOL(false);
1659 : : }
1660 : :
1661 : : Datum
8364 peter_e@gmx.net 1662 :CBC 24 : makeaclitem(PG_FUNCTION_ARGS)
1663 : : {
7616 tgl@sss.pgh.pa.us 1664 : 24 : Oid grantee = PG_GETARG_OID(0);
7507 bruce@momjian.us 1665 : 24 : Oid grantor = PG_GETARG_OID(1);
3341 noah@leadboat.com 1666 : 24 : text *privtext = PG_GETARG_TEXT_PP(2);
7616 tgl@sss.pgh.pa.us 1667 : 24 : 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 : :
1402 1690 : 24 : priv = convert_any_priv_string(privtext, any_priv_map);
1691 : :
146 michael@paquier.xyz 1692 :GNC 20 : result = palloc_object(AclItem);
1693 : :
7615 tgl@sss.pgh.pa.us 1694 :CBC 20 : result->ai_grantee = grantee;
1695 : 20 : result->ai_grantor = grantor;
1696 : :
1697 [ + + ]: 20 : ACLITEM_SET_PRIVS_GOPTIONS(*result, priv,
1698 : : (goption ? priv : ACL_NO_RIGHTS));
1699 : :
1700 : 20 : 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 : : */
1713 : : static AclMode
6297 1714 : 446209 : convert_any_priv_string(text *priv_type_text,
1715 : : const priv_map *privileges)
1716 : : {
1717 : 446209 : AclMode result = 0;
1718 : 446209 : char *priv_type = text_to_cstring(priv_type_text);
1719 : : char *chunk;
1720 : : char *next_chunk;
1721 : :
1722 : : /* We rely on priv_type being a private, modifiable string */
1723 [ + + ]: 892425 : 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 : 446235 : next_chunk = strchr(chunk, ',');
1730 [ + + ]: 446235 : if (next_chunk)
1731 : 28 : *next_chunk++ = '\0';
1732 : :
1733 : : /* Drop leading/trailing whitespace in this chunk */
1734 [ + - + + ]: 446266 : while (*chunk && isspace((unsigned char) *chunk))
1735 : 31 : chunk++;
1736 : 446235 : chunk_len = strlen(chunk);
1737 [ + - + + ]: 446250 : while (chunk_len > 0 && isspace((unsigned char) chunk[chunk_len - 1]))
1738 : 15 : chunk_len--;
1739 : 446235 : chunk[chunk_len] = '\0';
1740 : :
1741 : : /* Match to the privileges list */
1742 [ + + ]: 447477 : for (this_priv = privileges; this_priv->name; this_priv++)
1743 : : {
1744 [ + + ]: 447458 : if (pg_strcasecmp(this_priv->name, chunk) == 0)
1745 : : {
1746 : 446216 : result |= this_priv->value;
1747 : 446216 : break;
1748 : : }
1749 : : }
1750 [ + + ]: 446235 : if (!this_priv->name)
1751 [ + - ]: 19 : ereport(ERROR,
1752 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1753 : : errmsg("unrecognized privilege type: \"%s\"", chunk)));
1754 : : }
1755 : :
1756 : 446190 : pfree(priv_type);
1757 : 446190 : return result;
1758 : : }
1759 : :
1760 : :
1761 : : static const char *
5995 peter_e@gmx.net 1762 : 330020 : convert_aclright_to_string(int aclright)
1763 : : {
1764 [ + + + + : 330020 : switch (aclright)
+ + + + +
- - - - -
+ - ]
1765 : : {
1766 : 39231 : case ACL_INSERT:
1767 : 39231 : return "INSERT";
1768 : 55045 : case ACL_SELECT:
1769 : 55045 : return "SELECT";
1770 : 39512 : case ACL_UPDATE:
1771 : 39512 : return "UPDATE";
1772 : 39159 : case ACL_DELETE:
1773 : 39159 : return "DELETE";
1774 : 39231 : case ACL_TRUNCATE:
1775 : 39231 : return "TRUNCATE";
1776 : 39231 : case ACL_REFERENCES:
1777 : 39231 : return "REFERENCES";
1778 : 39231 : case ACL_TRIGGER:
1779 : 39231 : return "TRIGGER";
1780 : 41 : case ACL_EXECUTE:
1781 : 41 : return "EXECUTE";
1782 : 108 : case ACL_USAGE:
1783 : 108 : return "USAGE";
5995 peter_e@gmx.net 1784 :UBC 0 : case ACL_CREATE:
1785 : 0 : return "CREATE";
1786 : 0 : case ACL_CREATE_TEMP:
1787 : 0 : return "TEMPORARY";
1788 : 0 : case ACL_CONNECT:
1789 : 0 : return "CONNECT";
1490 tgl@sss.pgh.pa.us 1790 : 0 : case ACL_SET:
1791 : 0 : return "SET";
1792 : 0 : case ACL_ALTER_SYSTEM:
1793 : 0 : return "ALTER SYSTEM";
783 nathan@postgresql.or 1794 :CBC 39231 : case ACL_MAINTAIN:
1795 : 39231 : return "MAINTAIN";
5995 peter_e@gmx.net 1796 :UBC 0 : default:
1797 [ # # ]: 0 : 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 : : */
1817 : : Datum
5995 peter_e@gmx.net 1818 :CBC 369332 : aclexplode(PG_FUNCTION_ARGS)
1819 : : {
5957 tgl@sss.pgh.pa.us 1820 : 369332 : Acl *acl = PG_GETARG_ACL_P(0);
1821 : : FuncCallContext *funcctx;
1822 : : int *idx;
1823 : : AclItem *aidat;
1824 : :
5995 peter_e@gmx.net 1825 [ + + ]: 369332 : if (SRF_IS_FIRSTCALL())
1826 : : {
1827 : : TupleDesc tupdesc;
1828 : : MemoryContext oldcontext;
1829 : :
1830 : 39312 : check_acl(acl);
1831 : :
1832 : 39312 : funcctx = SRF_FIRSTCALL_INIT();
1833 : 39312 : oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1834 : :
1835 : : /*
1836 : : * build tupdesc for result tuples (matches out parameters in pg_proc
1837 : : * entry)
1838 : : */
2723 andres@anarazel.de 1839 : 39312 : tupdesc = CreateTemplateTupleDesc(4);
5995 peter_e@gmx.net 1840 : 39312 : TupleDescInitEntry(tupdesc, (AttrNumber) 1, "grantor",
1841 : : OIDOID, -1, 0);
1842 : 39312 : TupleDescInitEntry(tupdesc, (AttrNumber) 2, "grantee",
1843 : : OIDOID, -1, 0);
1844 : 39312 : TupleDescInitEntry(tupdesc, (AttrNumber) 3, "privilege_type",
1845 : : TEXTOID, -1, 0);
1846 : 39312 : TupleDescInitEntry(tupdesc, (AttrNumber) 4, "is_grantable",
1847 : : BOOLOID, -1, 0);
1848 : :
50 drowley@postgresql.o 1849 :GNC 39312 : TupleDescFinalize(tupdesc);
5995 peter_e@gmx.net 1850 :CBC 39312 : funcctx->tuple_desc = BlessTupleDesc(tupdesc);
1851 : :
1852 : : /* allocate memory for user context */
146 michael@paquier.xyz 1853 :GNC 39312 : idx = palloc_array(int, 2);
5995 peter_e@gmx.net 1854 :CBC 39312 : idx[0] = 0; /* ACL array item index */
1855 : 39312 : idx[1] = -1; /* privilege type counter */
523 peter@eisentraut.org 1856 : 39312 : funcctx->user_fctx = idx;
1857 : :
5995 peter_e@gmx.net 1858 : 39312 : MemoryContextSwitchTo(oldcontext);
1859 : : }
1860 : :
1861 : 369332 : funcctx = SRF_PERCALL_SETUP();
1862 : 369332 : idx = (int *) funcctx->user_fctx;
1863 [ - + ]: 369332 : aidat = ACL_DAT(acl);
1864 : :
1865 : : /* need test here in case acl has no items */
5957 tgl@sss.pgh.pa.us 1866 [ + - ]: 868047 : while (idx[0] < ACL_NUM(acl))
1867 : : {
1868 : : AclItem *aidata;
1869 : : AclMode priv_bit;
1870 : :
5995 peter_e@gmx.net 1871 : 868047 : idx[1]++;
1872 [ + + ]: 868047 : if (idx[1] == N_ACL_RIGHTS)
1873 : : {
1874 : 55249 : idx[1] = 0;
1875 : 55249 : idx[0]++;
5912 bruce@momjian.us 1876 [ + + ]: 55249 : if (idx[0] >= ACL_NUM(acl)) /* done */
5995 peter_e@gmx.net 1877 : 39312 : break;
1878 : : }
5957 tgl@sss.pgh.pa.us 1879 : 828735 : aidata = &aidat[idx[0]];
1257 drowley@postgresql.o 1880 : 828735 : priv_bit = UINT64CONST(1) << idx[1];
1881 : :
5957 tgl@sss.pgh.pa.us 1882 [ + + ]: 828735 : if (ACLITEM_GET_PRIVS(*aidata) & priv_bit)
1883 : : {
1884 : : Datum result;
1885 : : Datum values[4];
1389 peter@eisentraut.org 1886 : 330020 : bool nulls[4] = {0};
1887 : : HeapTuple tuple;
1888 : :
5957 tgl@sss.pgh.pa.us 1889 : 330020 : values[0] = ObjectIdGetDatum(aidata->ai_grantor);
1890 : 330020 : values[1] = ObjectIdGetDatum(aidata->ai_grantee);
1891 : 330020 : values[2] = CStringGetTextDatum(convert_aclright_to_string(priv_bit));
1892 : 330020 : values[3] = BoolGetDatum((ACLITEM_GET_GOPTIONS(*aidata) & priv_bit) != 0);
1893 : :
5995 peter_e@gmx.net 1894 : 330020 : tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
1895 : 330020 : result = HeapTupleGetDatum(tuple);
1896 : :
1897 : 330020 : SRF_RETURN_NEXT(funcctx, result);
1898 : : }
1899 : : }
1900 : :
1901 : 39312 : SRF_RETURN_DONE(funcctx);
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 : : */
1922 : : Datum
9091 tgl@sss.pgh.pa.us 1923 : 144 : has_table_privilege_name_name(PG_FUNCTION_ARGS)
1924 : : {
7616 1925 : 144 : Name rolename = PG_GETARG_NAME(0);
3341 noah@leadboat.com 1926 : 144 : text *tablename = PG_GETARG_TEXT_PP(1);
1927 : 144 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
1928 : : Oid roleid;
1929 : : Oid tableoid;
1930 : : AclMode mode;
1931 : : AclResult aclresult;
1932 : :
5683 itagaki.takahiro@gma 1933 : 144 : roleid = get_role_oid_or_public(NameStr(*rolename));
8670 tgl@sss.pgh.pa.us 1934 : 140 : tableoid = convert_table_name(tablename);
1935 : 140 : mode = convert_table_priv_string(priv_type_text);
1936 : :
7616 1937 : 140 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
1938 : :
8775 1939 : 140 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
1948 : : Datum
9091 1949 : 44 : has_table_privilege_name(PG_FUNCTION_ARGS)
1950 : : {
3341 noah@leadboat.com 1951 : 44 : text *tablename = PG_GETARG_TEXT_PP(0);
1952 : 44 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
1953 : : Oid roleid;
1954 : : Oid tableoid;
1955 : : AclMode mode;
1956 : : AclResult aclresult;
1957 : :
7616 tgl@sss.pgh.pa.us 1958 : 44 : roleid = GetUserId();
8670 1959 : 44 : tableoid = convert_table_name(tablename);
1960 : 40 : mode = convert_table_priv_string(priv_type_text);
1961 : :
7616 1962 : 36 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
1963 : :
8775 1964 : 36 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
1972 : : Datum
9091 1973 : 12 : has_table_privilege_name_id(PG_FUNCTION_ARGS)
1974 : : {
1975 : 12 : Name username = PG_GETARG_NAME(0);
8670 1976 : 12 : Oid tableoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 1977 : 12 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
1978 : : Oid roleid;
1979 : : AclMode mode;
1980 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 1981 : 12 : bool is_missing = false;
1982 : :
5683 itagaki.takahiro@gma 1983 : 12 : roleid = get_role_oid_or_public(NameStr(*username));
8670 tgl@sss.pgh.pa.us 1984 : 12 : mode = convert_table_priv_string(priv_type_text);
1985 : :
934 1986 : 12 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
1987 : :
1988 [ - + ]: 12 : if (is_missing)
934 tgl@sss.pgh.pa.us 1989 :UBC 0 : PG_RETURN_NULL();
1990 : :
8775 tgl@sss.pgh.pa.us 1991 :CBC 12 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2000 : : Datum
9091 2001 : 77 : has_table_privilege_id(PG_FUNCTION_ARGS)
2002 : : {
8670 2003 : 77 : Oid tableoid = PG_GETARG_OID(0);
3341 noah@leadboat.com 2004 : 77 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
2005 : : Oid roleid;
2006 : : AclMode mode;
2007 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 2008 : 77 : bool is_missing = false;
2009 : :
7616 2010 : 77 : roleid = GetUserId();
8670 2011 : 77 : mode = convert_table_priv_string(priv_type_text);
2012 : :
934 2013 : 77 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2014 : :
2015 [ + + ]: 77 : if (is_missing)
2016 : 5 : PG_RETURN_NULL();
2017 : :
8775 2018 : 72 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2026 : : Datum
9091 2027 : 28 : has_table_privilege_id_name(PG_FUNCTION_ARGS)
2028 : : {
7616 2029 : 28 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 2030 : 28 : text *tablename = PG_GETARG_TEXT_PP(1);
2031 : 28 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2032 : : Oid tableoid;
2033 : : AclMode mode;
2034 : : AclResult aclresult;
2035 : :
8670 tgl@sss.pgh.pa.us 2036 : 28 : tableoid = convert_table_name(tablename);
2037 : 28 : mode = convert_table_priv_string(priv_type_text);
2038 : :
7616 2039 : 28 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2040 : :
8775 2041 : 28 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2049 : : Datum
9091 2050 : 24 : has_table_privilege_id_id(PG_FUNCTION_ARGS)
2051 : : {
7616 2052 : 24 : Oid roleid = PG_GETARG_OID(0);
8670 2053 : 24 : Oid tableoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 2054 : 24 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2055 : : AclMode mode;
2056 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 2057 : 24 : bool is_missing = false;
2058 : :
8670 2059 : 24 : mode = convert_table_priv_string(priv_type_text);
2060 : :
934 2061 : 24 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
2062 : :
2063 [ - + ]: 24 : if (is_missing)
934 tgl@sss.pgh.pa.us 2064 :UBC 0 : PG_RETURN_NULL();
2065 : :
8775 tgl@sss.pgh.pa.us 2066 :CBC 24 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2076 : : static Oid
8670 2077 : 256 : convert_table_name(text *tablename)
2078 : : {
2079 : : RangeVar *relrv;
2080 : :
7648 neilc@samurai.com 2081 : 256 : relrv = makeRangeVarFromNameList(textToQualifiedNameList(tablename));
2082 : :
2083 : : /* We might not even have permissions on this relation; don't lock it. */
5270 rhaas@postgresql.org 2084 : 256 : return RangeVarGetRelid(relrv, NoLock, false);
2085 : : }
2086 : :
2087 : : /*
2088 : : * convert_table_priv_string
2089 : : * Convert text string to AclMode value.
2090 : : */
2091 : : static AclMode
8670 tgl@sss.pgh.pa.us 2092 : 321 : convert_table_priv_string(text *priv_type_text)
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 : :
6297 2114 : 321 : return convert_any_priv_string(priv_type_text, table_priv_map);
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 : : */
2133 : : Datum
6119 mail@joeconway.com 2134 : 12 : has_sequence_privilege_name_name(PG_FUNCTION_ARGS)
2135 : : {
2136 : 12 : Name rolename = PG_GETARG_NAME(0);
3341 noah@leadboat.com 2137 : 12 : text *sequencename = PG_GETARG_TEXT_PP(1);
2138 : 12 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2139 : : Oid roleid;
2140 : : Oid sequenceoid;
2141 : : AclMode mode;
2142 : : AclResult aclresult;
2143 : :
5683 itagaki.takahiro@gma 2144 : 12 : roleid = get_role_oid_or_public(NameStr(*rolename));
6119 mail@joeconway.com 2145 : 12 : mode = convert_sequence_priv_string(priv_type_text);
2146 : 8 : sequenceoid = convert_table_name(sequencename);
2147 [ + + ]: 8 : if (get_rel_relkind(sequenceoid) != RELKIND_SEQUENCE)
2148 [ + - ]: 4 : ereport(ERROR,
2149 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2150 : : errmsg("\"%s\" is not a sequence",
2151 : : text_to_cstring(sequencename))));
2152 : :
2153 : 4 : aclresult = pg_class_aclcheck(sequenceoid, roleid, mode);
2154 : :
2155 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2156 : : }
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 : : */
2164 : : Datum
2165 : 4 : has_sequence_privilege_name(PG_FUNCTION_ARGS)
2166 : : {
3341 noah@leadboat.com 2167 : 4 : text *sequencename = PG_GETARG_TEXT_PP(0);
2168 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
2169 : : Oid roleid;
2170 : : Oid sequenceoid;
2171 : : AclMode mode;
2172 : : AclResult aclresult;
2173 : :
6119 mail@joeconway.com 2174 : 4 : roleid = GetUserId();
2175 : 4 : mode = convert_sequence_priv_string(priv_type_text);
2176 : 4 : sequenceoid = convert_table_name(sequencename);
2177 [ - + ]: 4 : if (get_rel_relkind(sequenceoid) != RELKIND_SEQUENCE)
6119 mail@joeconway.com 2178 [ # # ]:UBC 0 : ereport(ERROR,
2179 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2180 : : errmsg("\"%s\" is not a sequence",
2181 : : text_to_cstring(sequencename))));
2182 : :
6119 mail@joeconway.com 2183 :CBC 4 : aclresult = pg_class_aclcheck(sequenceoid, roleid, mode);
2184 : :
2185 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2186 : : }
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 : : */
2193 : : Datum
6119 mail@joeconway.com 2194 :UBC 0 : has_sequence_privilege_name_id(PG_FUNCTION_ARGS)
2195 : : {
2196 : 0 : Name username = PG_GETARG_NAME(0);
2197 : 0 : Oid sequenceoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 2198 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2199 : : Oid roleid;
2200 : : AclMode mode;
2201 : : AclResult aclresult;
2202 : : char relkind;
934 tgl@sss.pgh.pa.us 2203 : 0 : bool is_missing = false;
2204 : :
5683 itagaki.takahiro@gma 2205 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
6119 mail@joeconway.com 2206 : 0 : mode = convert_sequence_priv_string(priv_type_text);
2207 : 0 : relkind = get_rel_relkind(sequenceoid);
2208 [ # # ]: 0 : if (relkind == '\0')
2209 : 0 : PG_RETURN_NULL();
2210 [ # # ]: 0 : else if (relkind != RELKIND_SEQUENCE)
2211 [ # # ]: 0 : ereport(ERROR,
2212 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2213 : : errmsg("\"%s\" is not a sequence",
2214 : : get_rel_name(sequenceoid))));
2215 : :
934 tgl@sss.pgh.pa.us 2216 : 0 : aclresult = pg_class_aclcheck_ext(sequenceoid, roleid, mode, &is_missing);
2217 : :
2218 [ # # ]: 0 : if (is_missing)
2219 : 0 : PG_RETURN_NULL();
2220 : :
6119 mail@joeconway.com 2221 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2230 : : Datum
2231 : 0 : has_sequence_privilege_id(PG_FUNCTION_ARGS)
2232 : : {
2233 : 0 : Oid sequenceoid = PG_GETARG_OID(0);
3341 noah@leadboat.com 2234 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
2235 : : Oid roleid;
2236 : : AclMode mode;
2237 : : AclResult aclresult;
2238 : : char relkind;
934 tgl@sss.pgh.pa.us 2239 : 0 : bool is_missing = false;
2240 : :
6119 mail@joeconway.com 2241 : 0 : roleid = GetUserId();
2242 : 0 : mode = convert_sequence_priv_string(priv_type_text);
2243 : 0 : relkind = get_rel_relkind(sequenceoid);
2244 [ # # ]: 0 : if (relkind == '\0')
2245 : 0 : PG_RETURN_NULL();
2246 [ # # ]: 0 : else if (relkind != RELKIND_SEQUENCE)
2247 [ # # ]: 0 : ereport(ERROR,
2248 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2249 : : errmsg("\"%s\" is not a sequence",
2250 : : get_rel_name(sequenceoid))));
2251 : :
934 tgl@sss.pgh.pa.us 2252 : 0 : aclresult = pg_class_aclcheck_ext(sequenceoid, roleid, mode, &is_missing);
2253 : :
2254 [ # # ]: 0 : if (is_missing)
2255 : 0 : PG_RETURN_NULL();
2256 : :
6119 mail@joeconway.com 2257 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2265 : : Datum
2266 : 0 : has_sequence_privilege_id_name(PG_FUNCTION_ARGS)
2267 : : {
2268 : 0 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 2269 : 0 : text *sequencename = PG_GETARG_TEXT_PP(1);
2270 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2271 : : Oid sequenceoid;
2272 : : AclMode mode;
2273 : : AclResult aclresult;
2274 : :
6119 mail@joeconway.com 2275 : 0 : mode = convert_sequence_priv_string(priv_type_text);
2276 : 0 : sequenceoid = convert_table_name(sequencename);
2277 [ # # ]: 0 : if (get_rel_relkind(sequenceoid) != RELKIND_SEQUENCE)
2278 [ # # ]: 0 : ereport(ERROR,
2279 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2280 : : errmsg("\"%s\" is not a sequence",
2281 : : text_to_cstring(sequencename))));
2282 : :
2283 : 0 : aclresult = pg_class_aclcheck(sequenceoid, roleid, mode);
2284 : :
2285 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2286 : : }
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 : : */
2293 : : Datum
2294 : 0 : has_sequence_privilege_id_id(PG_FUNCTION_ARGS)
2295 : : {
2296 : 0 : Oid roleid = PG_GETARG_OID(0);
2297 : 0 : Oid sequenceoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 2298 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2299 : : AclMode mode;
2300 : : AclResult aclresult;
2301 : : char relkind;
934 tgl@sss.pgh.pa.us 2302 : 0 : bool is_missing = false;
2303 : :
6119 mail@joeconway.com 2304 : 0 : mode = convert_sequence_priv_string(priv_type_text);
2305 : 0 : relkind = get_rel_relkind(sequenceoid);
2306 [ # # ]: 0 : if (relkind == '\0')
2307 : 0 : PG_RETURN_NULL();
2308 [ # # ]: 0 : else if (relkind != RELKIND_SEQUENCE)
2309 [ # # ]: 0 : ereport(ERROR,
2310 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2311 : : errmsg("\"%s\" is not a sequence",
2312 : : get_rel_name(sequenceoid))));
2313 : :
934 tgl@sss.pgh.pa.us 2314 : 0 : aclresult = pg_class_aclcheck_ext(sequenceoid, roleid, mode, &is_missing);
2315 : :
2316 [ # # ]: 0 : if (is_missing)
2317 : 0 : PG_RETURN_NULL();
2318 : :
6119 mail@joeconway.com 2319 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2320 : : }
2321 : :
2322 : : /*
2323 : : * convert_sequence_priv_string
2324 : : * Convert text string to AclMode value.
2325 : : */
2326 : : static AclMode
6119 mail@joeconway.com 2327 :CBC 16 : convert_sequence_priv_string(text *priv_type_text)
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 : :
2339 : 16 : return convert_any_priv_string(priv_type_text, sequence_priv_map);
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 : : */
2359 : : Datum
6297 tgl@sss.pgh.pa.us 2360 :UBC 0 : has_any_column_privilege_name_name(PG_FUNCTION_ARGS)
2361 : : {
2362 : 0 : Name rolename = PG_GETARG_NAME(0);
3341 noah@leadboat.com 2363 : 0 : text *tablename = PG_GETARG_TEXT_PP(1);
2364 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2365 : : Oid roleid;
2366 : : Oid tableoid;
2367 : : AclMode mode;
2368 : : AclResult aclresult;
2369 : :
5683 itagaki.takahiro@gma 2370 : 0 : roleid = get_role_oid_or_public(NameStr(*rolename));
6297 tgl@sss.pgh.pa.us 2371 : 0 : tableoid = convert_table_name(tablename);
2372 : 0 : mode = convert_column_priv_string(priv_type_text);
2373 : :
2374 : : /* First check at table level, then examine each column if needed */
2375 : 0 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2376 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2377 : 0 : aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
2378 : : ACLMASK_ANY);
2379 : :
8670 2380 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2389 : : Datum
6297 2390 : 0 : has_any_column_privilege_name(PG_FUNCTION_ARGS)
2391 : : {
3341 noah@leadboat.com 2392 : 0 : text *tablename = PG_GETARG_TEXT_PP(0);
2393 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
2394 : : Oid roleid;
2395 : : Oid tableoid;
2396 : : AclMode mode;
2397 : : AclResult aclresult;
2398 : :
7616 tgl@sss.pgh.pa.us 2399 : 0 : roleid = GetUserId();
6297 2400 : 0 : tableoid = convert_table_name(tablename);
2401 : 0 : mode = convert_column_priv_string(priv_type_text);
2402 : :
2403 : : /* First check at table level, then examine each column if needed */
2404 : 0 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2405 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2406 : 0 : aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
2407 : : ACLMASK_ANY);
2408 : :
8670 2409 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2417 : : Datum
6297 2418 : 0 : has_any_column_privilege_name_id(PG_FUNCTION_ARGS)
2419 : : {
8670 2420 : 0 : Name username = PG_GETARG_NAME(0);
6297 2421 : 0 : Oid tableoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 2422 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2423 : : Oid roleid;
2424 : : AclMode mode;
2425 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 2426 : 0 : bool is_missing = false;
2427 : :
5683 itagaki.takahiro@gma 2428 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
6297 tgl@sss.pgh.pa.us 2429 : 0 : mode = convert_column_priv_string(priv_type_text);
2430 : :
2431 : : /* First check at table level, then examine each column if needed */
934 2432 : 0 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
6297 2433 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2434 : : {
934 2435 [ # # ]: 0 : if (is_missing)
2436 : 0 : PG_RETURN_NULL();
2437 : 0 : aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
2438 : : ACLMASK_ANY, &is_missing);
2439 [ # # ]: 0 : if (is_missing)
2440 : 0 : PG_RETURN_NULL();
2441 : : }
2442 : :
8670 2443 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2452 : : Datum
6297 2453 : 0 : has_any_column_privilege_id(PG_FUNCTION_ARGS)
2454 : : {
2455 : 0 : Oid tableoid = PG_GETARG_OID(0);
3341 noah@leadboat.com 2456 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
2457 : : Oid roleid;
2458 : : AclMode mode;
2459 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 2460 : 0 : bool is_missing = false;
2461 : :
7616 2462 : 0 : roleid = GetUserId();
6297 2463 : 0 : mode = convert_column_priv_string(priv_type_text);
2464 : :
2465 : : /* First check at table level, then examine each column if needed */
934 2466 : 0 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
6297 2467 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2468 : : {
934 2469 [ # # ]: 0 : if (is_missing)
2470 : 0 : PG_RETURN_NULL();
2471 : 0 : aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
2472 : : ACLMASK_ANY, &is_missing);
2473 [ # # ]: 0 : if (is_missing)
2474 : 0 : PG_RETURN_NULL();
2475 : : }
2476 : :
8670 2477 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2485 : : Datum
6297 2486 : 0 : has_any_column_privilege_id_name(PG_FUNCTION_ARGS)
2487 : : {
7616 2488 : 0 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 2489 : 0 : text *tablename = PG_GETARG_TEXT_PP(1);
2490 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2491 : : Oid tableoid;
2492 : : AclMode mode;
2493 : : AclResult aclresult;
2494 : :
6297 tgl@sss.pgh.pa.us 2495 : 0 : tableoid = convert_table_name(tablename);
2496 : 0 : mode = convert_column_priv_string(priv_type_text);
2497 : :
2498 : : /* First check at table level, then examine each column if needed */
2499 : 0 : aclresult = pg_class_aclcheck(tableoid, roleid, mode);
2500 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2501 : 0 : aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
2502 : : ACLMASK_ANY);
2503 : :
8670 2504 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2512 : : Datum
6297 2513 : 0 : has_any_column_privilege_id_id(PG_FUNCTION_ARGS)
2514 : : {
7616 2515 : 0 : Oid roleid = PG_GETARG_OID(0);
6297 2516 : 0 : Oid tableoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 2517 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2518 : : AclMode mode;
2519 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 2520 : 0 : bool is_missing = false;
2521 : :
6297 2522 : 0 : mode = convert_column_priv_string(priv_type_text);
2523 : :
2524 : : /* First check at table level, then examine each column if needed */
934 2525 : 0 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
6297 2526 [ # # ]: 0 : if (aclresult != ACLCHECK_OK)
2527 : : {
934 2528 [ # # ]: 0 : if (is_missing)
2529 : 0 : PG_RETURN_NULL();
2530 : 0 : aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
2531 : : ACLMASK_ANY, &is_missing);
2532 [ # # ]: 0 : if (is_missing)
2533 : 0 : PG_RETURN_NULL();
2534 : : }
2535 : :
8670 2536 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
2563 : : static int
6297 tgl@sss.pgh.pa.us 2564 :CBC 378217 : column_privilege_check(Oid tableoid, AttrNumber attnum,
2565 : : Oid roleid, AclMode mode)
2566 : : {
2567 : : AclResult aclresult;
1861 mail@joeconway.com 2568 : 378217 : bool is_missing = false;
2569 : :
2570 : : /*
2571 : : * If convert_column_name failed, we can just return -1 immediately.
2572 : : */
2772 tgl@sss.pgh.pa.us 2573 [ + + ]: 378217 : if (attnum == InvalidAttrNumber)
2574 : 8 : 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 : : */
1861 mail@joeconway.com 2581 : 378209 : aclresult = pg_attribute_aclcheck_ext(tableoid, attnum, roleid,
2582 : : mode, &is_missing);
2583 [ + + ]: 378209 : if (aclresult == ACLCHECK_OK)
2584 : 3976 : return 1;
2585 [ + + ]: 374233 : else if (is_missing)
6297 tgl@sss.pgh.pa.us 2586 : 28 : return -1;
2587 : :
2588 : : /* Next check if we have the privilege at the table level */
1861 mail@joeconway.com 2589 : 374205 : aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
6297 tgl@sss.pgh.pa.us 2590 [ + + ]: 374205 : if (aclresult == ACLCHECK_OK)
1861 mail@joeconway.com 2591 : 371405 : return 1;
1861 mail@joeconway.com 2592 [ - + ]:GBC 2800 : else if (is_missing)
6297 tgl@sss.pgh.pa.us 2593 :UBC 0 : return -1;
2594 : : else
1861 mail@joeconway.com 2595 :GBC 2800 : 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 : : */
2603 : : Datum
6297 tgl@sss.pgh.pa.us 2604 :UBC 0 : has_column_privilege_name_name_name(PG_FUNCTION_ARGS)
2605 : : {
2606 : 0 : Name rolename = PG_GETARG_NAME(0);
3341 noah@leadboat.com 2607 : 0 : text *tablename = PG_GETARG_TEXT_PP(1);
2608 : 0 : text *column = PG_GETARG_TEXT_PP(2);
2609 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2610 : : Oid roleid;
2611 : : Oid tableoid;
2612 : : AttrNumber colattnum;
2613 : : AclMode mode;
2614 : : int privresult;
2615 : :
5683 itagaki.takahiro@gma 2616 : 0 : roleid = get_role_oid_or_public(NameStr(*rolename));
6297 tgl@sss.pgh.pa.us 2617 : 0 : tableoid = convert_table_name(tablename);
2618 : 0 : colattnum = convert_column_name(tableoid, column);
2619 : 0 : mode = convert_column_priv_string(priv_type_text);
2620 : :
2621 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2622 [ # # ]: 0 : if (privresult < 0)
2623 : 0 : PG_RETURN_NULL();
2624 : 0 : PG_RETURN_BOOL(privresult);
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 : : */
2632 : : Datum
2633 : 0 : has_column_privilege_name_name_attnum(PG_FUNCTION_ARGS)
2634 : : {
2635 : 0 : Name rolename = PG_GETARG_NAME(0);
3341 noah@leadboat.com 2636 : 0 : text *tablename = PG_GETARG_TEXT_PP(1);
6297 tgl@sss.pgh.pa.us 2637 : 0 : AttrNumber colattnum = PG_GETARG_INT16(2);
3341 noah@leadboat.com 2638 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2639 : : Oid roleid;
2640 : : Oid tableoid;
2641 : : AclMode mode;
2642 : : int privresult;
2643 : :
5683 itagaki.takahiro@gma 2644 : 0 : roleid = get_role_oid_or_public(NameStr(*rolename));
6297 tgl@sss.pgh.pa.us 2645 : 0 : tableoid = convert_table_name(tablename);
2646 : 0 : mode = convert_column_priv_string(priv_type_text);
2647 : :
2648 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2649 [ # # ]: 0 : if (privresult < 0)
2650 : 0 : PG_RETURN_NULL();
2651 : 0 : PG_RETURN_BOOL(privresult);
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 : : */
2659 : : Datum
2660 : 0 : has_column_privilege_name_id_name(PG_FUNCTION_ARGS)
2661 : : {
6346 peter_e@gmx.net 2662 : 0 : Name username = PG_GETARG_NAME(0);
6297 tgl@sss.pgh.pa.us 2663 : 0 : Oid tableoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 2664 : 0 : text *column = PG_GETARG_TEXT_PP(2);
2665 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2666 : : Oid roleid;
2667 : : AttrNumber colattnum;
2668 : : AclMode mode;
2669 : : int privresult;
2670 : :
5683 itagaki.takahiro@gma 2671 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
6297 tgl@sss.pgh.pa.us 2672 : 0 : colattnum = convert_column_name(tableoid, column);
2673 : 0 : mode = convert_column_priv_string(priv_type_text);
2674 : :
2675 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2676 [ # # ]: 0 : if (privresult < 0)
2677 : 0 : PG_RETURN_NULL();
2678 : 0 : PG_RETURN_BOOL(privresult);
2679 : : }
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 : : */
2686 : : Datum
2687 : 0 : has_column_privilege_name_id_attnum(PG_FUNCTION_ARGS)
2688 : : {
2689 : 0 : Name username = PG_GETARG_NAME(0);
2690 : 0 : Oid tableoid = PG_GETARG_OID(1);
2691 : 0 : AttrNumber colattnum = PG_GETARG_INT16(2);
3341 noah@leadboat.com 2692 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2693 : : Oid roleid;
2694 : : AclMode mode;
2695 : : int privresult;
2696 : :
5683 itagaki.takahiro@gma 2697 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
6297 tgl@sss.pgh.pa.us 2698 : 0 : mode = convert_column_priv_string(priv_type_text);
2699 : :
2700 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2701 [ # # ]: 0 : if (privresult < 0)
2702 : 0 : PG_RETURN_NULL();
2703 : 0 : PG_RETURN_BOOL(privresult);
2704 : : }
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 : : */
2711 : : Datum
2712 : 0 : has_column_privilege_id_name_name(PG_FUNCTION_ARGS)
2713 : : {
2714 : 0 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 2715 : 0 : text *tablename = PG_GETARG_TEXT_PP(1);
2716 : 0 : text *column = PG_GETARG_TEXT_PP(2);
2717 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2718 : : Oid tableoid;
2719 : : AttrNumber colattnum;
2720 : : AclMode mode;
2721 : : int privresult;
2722 : :
6297 tgl@sss.pgh.pa.us 2723 : 0 : tableoid = convert_table_name(tablename);
2724 : 0 : colattnum = convert_column_name(tableoid, column);
2725 : 0 : mode = convert_column_priv_string(priv_type_text);
2726 : :
2727 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2728 [ # # ]: 0 : if (privresult < 0)
2729 : 0 : PG_RETURN_NULL();
2730 : 0 : PG_RETURN_BOOL(privresult);
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 : : */
2738 : : Datum
2739 : 0 : has_column_privilege_id_name_attnum(PG_FUNCTION_ARGS)
2740 : : {
2741 : 0 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 2742 : 0 : text *tablename = PG_GETARG_TEXT_PP(1);
6297 tgl@sss.pgh.pa.us 2743 : 0 : AttrNumber colattnum = PG_GETARG_INT16(2);
3341 noah@leadboat.com 2744 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2745 : : Oid tableoid;
2746 : : AclMode mode;
2747 : : int privresult;
2748 : :
6297 tgl@sss.pgh.pa.us 2749 : 0 : tableoid = convert_table_name(tablename);
2750 : 0 : mode = convert_column_priv_string(priv_type_text);
2751 : :
2752 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2753 [ # # ]: 0 : if (privresult < 0)
2754 : 0 : PG_RETURN_NULL();
2755 : 0 : PG_RETURN_BOOL(privresult);
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 : : */
2763 : : Datum
2764 : 0 : has_column_privilege_id_id_name(PG_FUNCTION_ARGS)
2765 : : {
2766 : 0 : Oid roleid = PG_GETARG_OID(0);
2767 : 0 : Oid tableoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 2768 : 0 : text *column = PG_GETARG_TEXT_PP(2);
2769 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2770 : : AttrNumber colattnum;
2771 : : AclMode mode;
2772 : : int privresult;
2773 : :
6297 tgl@sss.pgh.pa.us 2774 : 0 : colattnum = convert_column_name(tableoid, column);
2775 : 0 : mode = convert_column_priv_string(priv_type_text);
2776 : :
2777 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2778 [ # # ]: 0 : if (privresult < 0)
2779 : 0 : PG_RETURN_NULL();
2780 : 0 : PG_RETURN_BOOL(privresult);
2781 : : }
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 : : */
2788 : : Datum
2789 : 0 : has_column_privilege_id_id_attnum(PG_FUNCTION_ARGS)
2790 : : {
2791 : 0 : Oid roleid = PG_GETARG_OID(0);
2792 : 0 : Oid tableoid = PG_GETARG_OID(1);
2793 : 0 : AttrNumber colattnum = PG_GETARG_INT16(2);
3341 noah@leadboat.com 2794 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(3);
2795 : : AclMode mode;
2796 : : int privresult;
2797 : :
6297 tgl@sss.pgh.pa.us 2798 : 0 : mode = convert_column_priv_string(priv_type_text);
2799 : :
2800 : 0 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2801 [ # # ]: 0 : if (privresult < 0)
2802 : 0 : PG_RETURN_NULL();
2803 : 0 : PG_RETURN_BOOL(privresult);
2804 : : }
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 : : */
2812 : : Datum
6297 tgl@sss.pgh.pa.us 2813 :CBC 12 : has_column_privilege_name_name(PG_FUNCTION_ARGS)
2814 : : {
3341 noah@leadboat.com 2815 : 12 : text *tablename = PG_GETARG_TEXT_PP(0);
2816 : 12 : text *column = PG_GETARG_TEXT_PP(1);
2817 : 12 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2818 : : Oid roleid;
2819 : : Oid tableoid;
2820 : : AttrNumber colattnum;
2821 : : AclMode mode;
2822 : : int privresult;
2823 : :
6297 tgl@sss.pgh.pa.us 2824 : 12 : roleid = GetUserId();
2825 : 12 : tableoid = convert_table_name(tablename);
2826 : 12 : colattnum = convert_column_name(tableoid, column);
2827 : 4 : mode = convert_column_priv_string(priv_type_text);
2828 : :
2829 : 4 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2830 [ + - ]: 4 : if (privresult < 0)
2831 : 4 : PG_RETURN_NULL();
6297 tgl@sss.pgh.pa.us 2832 :UBC 0 : PG_RETURN_BOOL(privresult);
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 : : */
2841 : : Datum
6297 tgl@sss.pgh.pa.us 2842 :CBC 20 : has_column_privilege_name_attnum(PG_FUNCTION_ARGS)
2843 : : {
3341 noah@leadboat.com 2844 : 20 : text *tablename = PG_GETARG_TEXT_PP(0);
6297 tgl@sss.pgh.pa.us 2845 : 20 : AttrNumber colattnum = PG_GETARG_INT16(1);
3341 noah@leadboat.com 2846 : 20 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2847 : : Oid roleid;
2848 : : Oid tableoid;
2849 : : AclMode mode;
2850 : : int privresult;
2851 : :
6297 tgl@sss.pgh.pa.us 2852 : 20 : roleid = GetUserId();
2853 : 20 : tableoid = convert_table_name(tablename);
2854 : 20 : mode = convert_column_priv_string(priv_type_text);
2855 : :
2856 : 20 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2857 [ + - ]: 20 : if (privresult < 0)
2858 : 20 : PG_RETURN_NULL();
6297 tgl@sss.pgh.pa.us 2859 :UBC 0 : PG_RETURN_BOOL(privresult);
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 : : */
2868 : : Datum
6297 tgl@sss.pgh.pa.us 2869 :CBC 4 : has_column_privilege_id_name(PG_FUNCTION_ARGS)
2870 : : {
2871 : 4 : Oid tableoid = PG_GETARG_OID(0);
3341 noah@leadboat.com 2872 : 4 : text *column = PG_GETARG_TEXT_PP(1);
2873 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2874 : : Oid roleid;
2875 : : AttrNumber colattnum;
2876 : : AclMode mode;
2877 : : int privresult;
2878 : :
6297 tgl@sss.pgh.pa.us 2879 : 4 : roleid = GetUserId();
2880 : 4 : colattnum = convert_column_name(tableoid, column);
2881 : 4 : mode = convert_column_priv_string(priv_type_text);
2882 : :
2883 : 4 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2884 [ + - ]: 4 : if (privresult < 0)
2885 : 4 : PG_RETURN_NULL();
6297 tgl@sss.pgh.pa.us 2886 :UBC 0 : PG_RETURN_BOOL(privresult);
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 : : */
2895 : : Datum
6297 tgl@sss.pgh.pa.us 2896 :CBC 378189 : has_column_privilege_id_attnum(PG_FUNCTION_ARGS)
2897 : : {
2898 : 378189 : Oid tableoid = PG_GETARG_OID(0);
2899 : 378189 : AttrNumber colattnum = PG_GETARG_INT16(1);
3341 noah@leadboat.com 2900 : 378189 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
2901 : : Oid roleid;
2902 : : AclMode mode;
2903 : : int privresult;
2904 : :
6297 tgl@sss.pgh.pa.us 2905 : 378189 : roleid = GetUserId();
2906 : 378189 : mode = convert_column_priv_string(priv_type_text);
2907 : :
2908 : 378189 : privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
2909 [ + + ]: 378189 : if (privresult < 0)
2910 : 8 : PG_RETURN_NULL();
2911 : 378181 : PG_RETURN_BOOL(privresult);
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 : : */
2923 : : static AttrNumber
2924 : 16 : convert_column_name(Oid tableoid, text *column)
2925 : : {
2926 : : char *colname;
2927 : : HeapTuple attTuple;
2928 : : AttrNumber attnum;
2929 : :
2930 : 16 : 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 : : */
2772 2937 : 16 : attTuple = SearchSysCache2(ATTNAME,
2938 : : ObjectIdGetDatum(tableoid),
2939 : : CStringGetDatum(colname));
2940 [ + + ]: 16 : if (HeapTupleIsValid(attTuple))
2941 : : {
2942 : : Form_pg_attribute attributeForm;
2943 : :
2944 : 4 : attributeForm = (Form_pg_attribute) GETSTRUCT(attTuple);
2945 : : /* We want to return NULL for dropped columns */
2946 [ + - ]: 4 : if (attributeForm->attisdropped)
2947 : 4 : attnum = InvalidAttrNumber;
2948 : : else
2772 tgl@sss.pgh.pa.us 2949 :UBC 0 : attnum = attributeForm->attnum;
2772 tgl@sss.pgh.pa.us 2950 :CBC 4 : ReleaseSysCache(attTuple);
2951 : : }
2952 : : else
2953 : : {
2954 : 12 : 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 [ + + ]: 12 : if (tablename != NULL)
2962 : : {
2963 : : /* tableoid exists, colname does not, so throw error */
2964 [ + - ]: 8 : ereport(ERROR,
2965 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
2966 : : errmsg("column \"%s\" of relation \"%s\" does not exist",
2967 : : colname, tablename)));
2968 : : }
2969 : : /* tableoid doesn't exist, so act like attisdropped case */
2970 : 4 : attnum = InvalidAttrNumber;
2971 : : }
2972 : :
6297 2973 : 8 : pfree(colname);
2974 : 8 : return attnum;
2975 : : }
2976 : :
2977 : : /*
2978 : : * convert_column_priv_string
2979 : : * Convert text string to AclMode value.
2980 : : */
2981 : : static AclMode
2982 : 378217 : convert_column_priv_string(text *priv_type_text)
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 : :
2996 : 378217 : return convert_any_priv_string(priv_type_text, column_priv_map);
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 : : */
3015 : : Datum
6297 tgl@sss.pgh.pa.us 3016 :UBC 0 : has_database_privilege_name_name(PG_FUNCTION_ARGS)
3017 : : {
3018 : 0 : Name username = PG_GETARG_NAME(0);
3341 noah@leadboat.com 3019 : 0 : text *databasename = PG_GETARG_TEXT_PP(1);
3020 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3021 : : Oid roleid;
3022 : : Oid databaseoid;
3023 : : AclMode mode;
3024 : : AclResult aclresult;
3025 : :
5683 itagaki.takahiro@gma 3026 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
6297 tgl@sss.pgh.pa.us 3027 : 0 : databaseoid = convert_database_name(databasename);
3028 : 0 : mode = convert_database_priv_string(priv_type_text);
3029 : :
1269 peter@eisentraut.org 3030 : 0 : aclresult = object_aclcheck(DatabaseRelationId, databaseoid, roleid, mode);
3031 : :
6297 tgl@sss.pgh.pa.us 3032 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3033 : : }
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 : : */
3041 : : Datum
3042 : 0 : has_database_privilege_name(PG_FUNCTION_ARGS)
3043 : : {
3341 noah@leadboat.com 3044 : 0 : text *databasename = PG_GETARG_TEXT_PP(0);
3045 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3046 : : Oid roleid;
3047 : : Oid databaseoid;
3048 : : AclMode mode;
3049 : : AclResult aclresult;
3050 : :
6297 tgl@sss.pgh.pa.us 3051 : 0 : roleid = GetUserId();
3052 : 0 : databaseoid = convert_database_name(databasename);
3053 : 0 : mode = convert_database_priv_string(priv_type_text);
3054 : :
1269 peter@eisentraut.org 3055 : 0 : aclresult = object_aclcheck(DatabaseRelationId, databaseoid, roleid, mode);
3056 : :
6297 tgl@sss.pgh.pa.us 3057 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3058 : : }
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 : : */
3065 : : Datum
3066 : 0 : has_database_privilege_name_id(PG_FUNCTION_ARGS)
3067 : : {
3068 : 0 : Name username = PG_GETARG_NAME(0);
3069 : 0 : Oid databaseoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 3070 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3071 : : Oid roleid;
3072 : : AclMode mode;
3073 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3074 : 0 : bool is_missing = false;
3075 : :
5683 itagaki.takahiro@gma 3076 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
6297 tgl@sss.pgh.pa.us 3077 : 0 : mode = convert_database_priv_string(priv_type_text);
3078 : :
934 3079 : 0 : aclresult = object_aclcheck_ext(DatabaseRelationId, databaseoid,
3080 : : roleid, mode,
3081 : : &is_missing);
3082 : :
3083 [ # # ]: 0 : if (is_missing)
3084 : 0 : PG_RETURN_NULL();
3085 : :
6297 3086 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3087 : : }
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 : : */
3095 : : Datum
3096 : 0 : has_database_privilege_id(PG_FUNCTION_ARGS)
3097 : : {
3098 : 0 : Oid databaseoid = PG_GETARG_OID(0);
3341 noah@leadboat.com 3099 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3100 : : Oid roleid;
3101 : : AclMode mode;
3102 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3103 : 0 : bool is_missing = false;
3104 : :
6297 3105 : 0 : roleid = GetUserId();
3106 : 0 : mode = convert_database_priv_string(priv_type_text);
3107 : :
934 3108 : 0 : aclresult = object_aclcheck_ext(DatabaseRelationId, databaseoid,
3109 : : roleid, mode,
3110 : : &is_missing);
3111 : :
3112 [ # # ]: 0 : if (is_missing)
3113 : 0 : PG_RETURN_NULL();
3114 : :
6297 3115 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3116 : : }
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 : : */
3123 : : Datum
3124 : 0 : has_database_privilege_id_name(PG_FUNCTION_ARGS)
3125 : : {
3126 : 0 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 3127 : 0 : text *databasename = PG_GETARG_TEXT_PP(1);
3128 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3129 : : Oid databaseoid;
3130 : : AclMode mode;
3131 : : AclResult aclresult;
3132 : :
6297 tgl@sss.pgh.pa.us 3133 : 0 : databaseoid = convert_database_name(databasename);
3134 : 0 : mode = convert_database_priv_string(priv_type_text);
3135 : :
1269 peter@eisentraut.org 3136 : 0 : aclresult = object_aclcheck(DatabaseRelationId, databaseoid, roleid, mode);
3137 : :
6297 tgl@sss.pgh.pa.us 3138 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3139 : : }
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 : : */
3146 : : Datum
3147 : 0 : has_database_privilege_id_id(PG_FUNCTION_ARGS)
3148 : : {
3149 : 0 : Oid roleid = PG_GETARG_OID(0);
3150 : 0 : Oid databaseoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 3151 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3152 : : AclMode mode;
3153 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3154 : 0 : bool is_missing = false;
3155 : :
6297 3156 : 0 : mode = convert_database_priv_string(priv_type_text);
3157 : :
934 3158 : 0 : aclresult = object_aclcheck_ext(DatabaseRelationId, databaseoid,
3159 : : roleid, mode,
3160 : : &is_missing);
3161 : :
3162 [ # # ]: 0 : if (is_missing)
3163 : 0 : PG_RETURN_NULL();
3164 : :
6297 3165 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3166 : : }
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 : : */
3175 : : static Oid
3176 : 0 : convert_database_name(text *databasename)
3177 : : {
3178 : 0 : char *dbname = text_to_cstring(databasename);
3179 : :
5752 rhaas@postgresql.org 3180 : 0 : return get_database_oid(dbname, false);
3181 : : }
3182 : :
3183 : : /*
3184 : : * convert_database_priv_string
3185 : : * Convert text string to AclMode value.
3186 : : */
3187 : : static AclMode
6297 tgl@sss.pgh.pa.us 3188 : 0 : convert_database_priv_string(text *priv_type_text)
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 : :
3202 : 0 : return convert_any_priv_string(priv_type_text, database_priv_map);
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 : : */
3221 : : Datum
6297 tgl@sss.pgh.pa.us 3222 :CBC 8 : has_foreign_data_wrapper_privilege_name_name(PG_FUNCTION_ARGS)
3223 : : {
3224 : 8 : Name username = PG_GETARG_NAME(0);
3341 noah@leadboat.com 3225 : 8 : text *fdwname = PG_GETARG_TEXT_PP(1);
3226 : 8 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3227 : : Oid roleid;
3228 : : Oid fdwid;
3229 : : AclMode mode;
3230 : : AclResult aclresult;
3231 : :
5683 itagaki.takahiro@gma 3232 : 8 : roleid = get_role_oid_or_public(NameStr(*username));
6297 tgl@sss.pgh.pa.us 3233 : 8 : fdwid = convert_foreign_data_wrapper_name(fdwname);
3234 : 8 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3235 : :
1269 peter@eisentraut.org 3236 : 8 : aclresult = object_aclcheck(ForeignDataWrapperRelationId, fdwid, roleid, mode);
3237 : :
6297 tgl@sss.pgh.pa.us 3238 : 8 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3239 : : }
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 : : */
3247 : : Datum
6346 peter_e@gmx.net 3248 : 4 : has_foreign_data_wrapper_privilege_name(PG_FUNCTION_ARGS)
3249 : : {
3341 noah@leadboat.com 3250 : 4 : text *fdwname = PG_GETARG_TEXT_PP(0);
3251 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3252 : : Oid roleid;
3253 : : Oid fdwid;
3254 : : AclMode mode;
3255 : : AclResult aclresult;
3256 : :
6297 tgl@sss.pgh.pa.us 3257 : 4 : roleid = GetUserId();
3258 : 4 : fdwid = convert_foreign_data_wrapper_name(fdwname);
3259 : 4 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3260 : :
1269 peter@eisentraut.org 3261 : 4 : aclresult = object_aclcheck(ForeignDataWrapperRelationId, fdwid, roleid, mode);
3262 : :
6297 tgl@sss.pgh.pa.us 3263 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3264 : : }
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 : : */
3271 : : Datum
6346 peter_e@gmx.net 3272 : 4 : has_foreign_data_wrapper_privilege_name_id(PG_FUNCTION_ARGS)
3273 : : {
3274 : 4 : Name username = PG_GETARG_NAME(0);
3275 : 4 : Oid fdwid = PG_GETARG_OID(1);
3341 noah@leadboat.com 3276 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3277 : : Oid roleid;
3278 : : AclMode mode;
3279 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3280 : 4 : bool is_missing = false;
3281 : :
5683 itagaki.takahiro@gma 3282 : 4 : roleid = get_role_oid_or_public(NameStr(*username));
6297 tgl@sss.pgh.pa.us 3283 : 4 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3284 : :
934 3285 : 4 : aclresult = object_aclcheck_ext(ForeignDataWrapperRelationId, fdwid,
3286 : : roleid, mode,
3287 : : &is_missing);
3288 : :
3289 [ - + ]: 4 : if (is_missing)
934 tgl@sss.pgh.pa.us 3290 :UBC 0 : PG_RETURN_NULL();
3291 : :
6297 tgl@sss.pgh.pa.us 3292 :CBC 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3293 : : }
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 : : */
3301 : : Datum
6346 peter_e@gmx.net 3302 : 4 : has_foreign_data_wrapper_privilege_id(PG_FUNCTION_ARGS)
3303 : : {
3304 : 4 : Oid fdwid = PG_GETARG_OID(0);
3341 noah@leadboat.com 3305 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3306 : : Oid roleid;
3307 : : AclMode mode;
3308 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3309 : 4 : bool is_missing = false;
3310 : :
6297 3311 : 4 : roleid = GetUserId();
3312 : 4 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3313 : :
934 3314 : 4 : aclresult = object_aclcheck_ext(ForeignDataWrapperRelationId, fdwid,
3315 : : roleid, mode,
3316 : : &is_missing);
3317 : :
3318 [ - + ]: 4 : if (is_missing)
934 tgl@sss.pgh.pa.us 3319 :UBC 0 : PG_RETURN_NULL();
3320 : :
6297 tgl@sss.pgh.pa.us 3321 :CBC 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3322 : : }
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 : : */
3329 : : Datum
6346 peter_e@gmx.net 3330 : 4 : has_foreign_data_wrapper_privilege_id_name(PG_FUNCTION_ARGS)
3331 : : {
3332 : 4 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 3333 : 4 : text *fdwname = PG_GETARG_TEXT_PP(1);
3334 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3335 : : Oid fdwid;
3336 : : AclMode mode;
3337 : : AclResult aclresult;
3338 : :
6297 tgl@sss.pgh.pa.us 3339 : 4 : fdwid = convert_foreign_data_wrapper_name(fdwname);
3340 : 4 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3341 : :
1269 peter@eisentraut.org 3342 : 4 : aclresult = object_aclcheck(ForeignDataWrapperRelationId, fdwid, roleid, mode);
3343 : :
6297 tgl@sss.pgh.pa.us 3344 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3345 : : }
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 : : */
3352 : : Datum
6346 peter_e@gmx.net 3353 : 4 : has_foreign_data_wrapper_privilege_id_id(PG_FUNCTION_ARGS)
3354 : : {
3355 : 4 : Oid roleid = PG_GETARG_OID(0);
3356 : 4 : Oid fdwid = PG_GETARG_OID(1);
3341 noah@leadboat.com 3357 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3358 : : AclMode mode;
3359 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3360 : 4 : bool is_missing = false;
3361 : :
6297 3362 : 4 : mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
3363 : :
934 3364 : 4 : aclresult = object_aclcheck_ext(ForeignDataWrapperRelationId, fdwid,
3365 : : roleid, mode,
3366 : : &is_missing);
3367 : :
3368 [ - + ]: 4 : if (is_missing)
934 tgl@sss.pgh.pa.us 3369 :UBC 0 : PG_RETURN_NULL();
3370 : :
6297 tgl@sss.pgh.pa.us 3371 :CBC 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3372 : : }
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 : : */
3381 : : static Oid
3382 : 16 : convert_foreign_data_wrapper_name(text *fdwname)
3383 : : {
3384 : 16 : char *fdwstr = text_to_cstring(fdwname);
3385 : :
5513 rhaas@postgresql.org 3386 : 16 : 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 : : */
3393 : : static AclMode
6297 tgl@sss.pgh.pa.us 3394 : 28 : convert_foreign_data_wrapper_priv_string(text *priv_type_text)
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 : :
3402 : 28 : return convert_any_priv_string(priv_type_text, foreign_data_wrapper_priv_map);
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 : : */
3421 : : Datum
8670 3422 : 116 : has_function_privilege_name_name(PG_FUNCTION_ARGS)
3423 : : {
3424 : 116 : Name username = PG_GETARG_NAME(0);
3341 noah@leadboat.com 3425 : 116 : text *functionname = PG_GETARG_TEXT_PP(1);
3426 : 116 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3427 : : Oid roleid;
3428 : : Oid functionoid;
3429 : : AclMode mode;
3430 : : AclResult aclresult;
3431 : :
5683 itagaki.takahiro@gma 3432 : 116 : roleid = get_role_oid_or_public(NameStr(*username));
8670 tgl@sss.pgh.pa.us 3433 : 116 : functionoid = convert_function_name(functionname);
3434 : 116 : mode = convert_function_priv_string(priv_type_text);
3435 : :
1269 peter@eisentraut.org 3436 : 116 : aclresult = object_aclcheck(ProcedureRelationId, functionoid, roleid, mode);
3437 : :
8670 tgl@sss.pgh.pa.us 3438 : 116 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3439 : : }
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 : : */
3447 : : Datum
8670 tgl@sss.pgh.pa.us 3448 :UBC 0 : has_function_privilege_name(PG_FUNCTION_ARGS)
3449 : : {
3341 noah@leadboat.com 3450 : 0 : text *functionname = PG_GETARG_TEXT_PP(0);
3451 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3452 : : Oid roleid;
3453 : : Oid functionoid;
3454 : : AclMode mode;
3455 : : AclResult aclresult;
3456 : :
7616 tgl@sss.pgh.pa.us 3457 : 0 : roleid = GetUserId();
8670 3458 : 0 : functionoid = convert_function_name(functionname);
3459 : 0 : mode = convert_function_priv_string(priv_type_text);
3460 : :
1269 peter@eisentraut.org 3461 : 0 : aclresult = object_aclcheck(ProcedureRelationId, functionoid, roleid, mode);
3462 : :
8670 tgl@sss.pgh.pa.us 3463 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3464 : : }
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 : : */
3471 : : Datum
3472 : 0 : has_function_privilege_name_id(PG_FUNCTION_ARGS)
3473 : : {
3474 : 0 : Name username = PG_GETARG_NAME(0);
3475 : 0 : Oid functionoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 3476 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3477 : : Oid roleid;
3478 : : AclMode mode;
3479 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3480 : 0 : bool is_missing = false;
3481 : :
5683 itagaki.takahiro@gma 3482 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
8670 tgl@sss.pgh.pa.us 3483 : 0 : mode = convert_function_priv_string(priv_type_text);
3484 : :
934 3485 : 0 : aclresult = object_aclcheck_ext(ProcedureRelationId, functionoid,
3486 : : roleid, mode,
3487 : : &is_missing);
3488 : :
3489 [ # # ]: 0 : if (is_missing)
3490 : 0 : PG_RETURN_NULL();
3491 : :
8670 3492 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3493 : : }
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 : : */
3501 : : Datum
3502 : 0 : has_function_privilege_id(PG_FUNCTION_ARGS)
3503 : : {
3504 : 0 : Oid functionoid = PG_GETARG_OID(0);
3341 noah@leadboat.com 3505 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3506 : : Oid roleid;
3507 : : AclMode mode;
3508 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3509 : 0 : bool is_missing = false;
3510 : :
7616 3511 : 0 : roleid = GetUserId();
8670 3512 : 0 : mode = convert_function_priv_string(priv_type_text);
3513 : :
934 3514 : 0 : aclresult = object_aclcheck_ext(ProcedureRelationId, functionoid,
3515 : : roleid, mode,
3516 : : &is_missing);
3517 : :
3518 [ # # ]: 0 : if (is_missing)
3519 : 0 : PG_RETURN_NULL();
3520 : :
8670 3521 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3522 : : }
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 : : */
3529 : : Datum
3530 : 0 : has_function_privilege_id_name(PG_FUNCTION_ARGS)
3531 : : {
7616 3532 : 0 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 3533 : 0 : text *functionname = PG_GETARG_TEXT_PP(1);
3534 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3535 : : Oid functionoid;
3536 : : AclMode mode;
3537 : : AclResult aclresult;
3538 : :
8670 tgl@sss.pgh.pa.us 3539 : 0 : functionoid = convert_function_name(functionname);
3540 : 0 : mode = convert_function_priv_string(priv_type_text);
3541 : :
1269 peter@eisentraut.org 3542 : 0 : aclresult = object_aclcheck(ProcedureRelationId, functionoid, roleid, mode);
3543 : :
8670 tgl@sss.pgh.pa.us 3544 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3545 : : }
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 : : */
3552 : : Datum
3553 : 0 : has_function_privilege_id_id(PG_FUNCTION_ARGS)
3554 : : {
7616 3555 : 0 : Oid roleid = PG_GETARG_OID(0);
8670 3556 : 0 : Oid functionoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 3557 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3558 : : AclMode mode;
3559 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3560 : 0 : bool is_missing = false;
3561 : :
8670 3562 : 0 : mode = convert_function_priv_string(priv_type_text);
3563 : :
934 3564 : 0 : aclresult = object_aclcheck_ext(ProcedureRelationId, functionoid,
3565 : : roleid, mode,
3566 : : &is_missing);
3567 : :
3568 [ # # ]: 0 : if (is_missing)
3569 : 0 : PG_RETURN_NULL();
3570 : :
8670 3571 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3572 : : }
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 : : */
3581 : : static Oid
8670 tgl@sss.pgh.pa.us 3582 :CBC 116 : convert_function_name(text *functionname)
3583 : : {
6615 3584 : 116 : char *funcname = text_to_cstring(functionname);
3585 : : Oid oid;
3586 : :
8670 3587 : 116 : oid = DatumGetObjectId(DirectFunctionCall1(regprocedurein,
3588 : : CStringGetDatum(funcname)));
3589 : :
3590 [ - + ]: 116 : if (!OidIsValid(oid))
8318 tgl@sss.pgh.pa.us 3591 [ # # ]:UBC 0 : ereport(ERROR,
3592 : : (errcode(ERRCODE_UNDEFINED_FUNCTION),
3593 : : errmsg("function \"%s\" does not exist", funcname)));
3594 : :
8670 tgl@sss.pgh.pa.us 3595 :CBC 116 : return oid;
3596 : : }
3597 : :
3598 : : /*
3599 : : * convert_function_priv_string
3600 : : * Convert text string to AclMode value.
3601 : : */
3602 : : static AclMode
3603 : 116 : convert_function_priv_string(text *priv_type_text)
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 : :
6297 3611 : 116 : return convert_any_priv_string(priv_type_text, function_priv_map);
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 : : */
3630 : : Datum
8670 tgl@sss.pgh.pa.us 3631 :UBC 0 : has_language_privilege_name_name(PG_FUNCTION_ARGS)
3632 : : {
3633 : 0 : Name username = PG_GETARG_NAME(0);
3341 noah@leadboat.com 3634 : 0 : text *languagename = PG_GETARG_TEXT_PP(1);
3635 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3636 : : Oid roleid;
3637 : : Oid languageoid;
3638 : : AclMode mode;
3639 : : AclResult aclresult;
3640 : :
5683 itagaki.takahiro@gma 3641 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
8670 tgl@sss.pgh.pa.us 3642 : 0 : languageoid = convert_language_name(languagename);
3643 : 0 : mode = convert_language_priv_string(priv_type_text);
3644 : :
1269 peter@eisentraut.org 3645 : 0 : aclresult = object_aclcheck(LanguageRelationId, languageoid, roleid, mode);
3646 : :
8670 tgl@sss.pgh.pa.us 3647 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3648 : : }
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 : : */
3656 : : Datum
3657 : 0 : has_language_privilege_name(PG_FUNCTION_ARGS)
3658 : : {
3341 noah@leadboat.com 3659 : 0 : text *languagename = PG_GETARG_TEXT_PP(0);
3660 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3661 : : Oid roleid;
3662 : : Oid languageoid;
3663 : : AclMode mode;
3664 : : AclResult aclresult;
3665 : :
7616 tgl@sss.pgh.pa.us 3666 : 0 : roleid = GetUserId();
8670 3667 : 0 : languageoid = convert_language_name(languagename);
3668 : 0 : mode = convert_language_priv_string(priv_type_text);
3669 : :
1269 peter@eisentraut.org 3670 : 0 : aclresult = object_aclcheck(LanguageRelationId, languageoid, roleid, mode);
3671 : :
8670 tgl@sss.pgh.pa.us 3672 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3673 : : }
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 : : */
3680 : : Datum
3681 : 0 : has_language_privilege_name_id(PG_FUNCTION_ARGS)
3682 : : {
3683 : 0 : Name username = PG_GETARG_NAME(0);
3684 : 0 : Oid languageoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 3685 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3686 : : Oid roleid;
3687 : : AclMode mode;
3688 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3689 : 0 : bool is_missing = false;
3690 : :
5683 itagaki.takahiro@gma 3691 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
8670 tgl@sss.pgh.pa.us 3692 : 0 : mode = convert_language_priv_string(priv_type_text);
3693 : :
934 3694 : 0 : aclresult = object_aclcheck_ext(LanguageRelationId, languageoid,
3695 : : roleid, mode,
3696 : : &is_missing);
3697 : :
3698 [ # # ]: 0 : if (is_missing)
3699 : 0 : PG_RETURN_NULL();
3700 : :
8670 3701 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3702 : : }
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 : : */
3710 : : Datum
3711 : 0 : has_language_privilege_id(PG_FUNCTION_ARGS)
3712 : : {
3713 : 0 : Oid languageoid = PG_GETARG_OID(0);
3341 noah@leadboat.com 3714 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3715 : : Oid roleid;
3716 : : AclMode mode;
3717 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3718 : 0 : bool is_missing = false;
3719 : :
7616 3720 : 0 : roleid = GetUserId();
8670 3721 : 0 : mode = convert_language_priv_string(priv_type_text);
3722 : :
934 3723 : 0 : aclresult = object_aclcheck_ext(LanguageRelationId, languageoid,
3724 : : roleid, mode,
3725 : : &is_missing);
3726 : :
3727 [ # # ]: 0 : if (is_missing)
3728 : 0 : PG_RETURN_NULL();
3729 : :
8670 3730 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3731 : : }
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 : : */
3738 : : Datum
3739 : 0 : has_language_privilege_id_name(PG_FUNCTION_ARGS)
3740 : : {
7616 3741 : 0 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 3742 : 0 : text *languagename = PG_GETARG_TEXT_PP(1);
3743 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3744 : : Oid languageoid;
3745 : : AclMode mode;
3746 : : AclResult aclresult;
3747 : :
8670 tgl@sss.pgh.pa.us 3748 : 0 : languageoid = convert_language_name(languagename);
3749 : 0 : mode = convert_language_priv_string(priv_type_text);
3750 : :
1269 peter@eisentraut.org 3751 : 0 : aclresult = object_aclcheck(LanguageRelationId, languageoid, roleid, mode);
3752 : :
8670 tgl@sss.pgh.pa.us 3753 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3754 : : }
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 : : */
3761 : : Datum
3762 : 0 : has_language_privilege_id_id(PG_FUNCTION_ARGS)
3763 : : {
7616 3764 : 0 : Oid roleid = PG_GETARG_OID(0);
8670 3765 : 0 : Oid languageoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 3766 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3767 : : AclMode mode;
3768 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3769 : 0 : bool is_missing = false;
3770 : :
8670 3771 : 0 : mode = convert_language_priv_string(priv_type_text);
3772 : :
934 3773 : 0 : aclresult = object_aclcheck_ext(LanguageRelationId, languageoid,
3774 : : roleid, mode,
3775 : : &is_missing);
3776 : :
3777 [ # # ]: 0 : if (is_missing)
3778 : 0 : PG_RETURN_NULL();
3779 : :
8670 3780 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3781 : : }
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 : : */
3790 : : static Oid
3791 : 0 : convert_language_name(text *languagename)
3792 : : {
6615 3793 : 0 : char *langname = text_to_cstring(languagename);
3794 : :
5752 rhaas@postgresql.org 3795 : 0 : return get_language_oid(langname, false);
3796 : : }
3797 : :
3798 : : /*
3799 : : * convert_language_priv_string
3800 : : * Convert text string to AclMode value.
3801 : : */
3802 : : static AclMode
8670 tgl@sss.pgh.pa.us 3803 : 0 : convert_language_priv_string(text *priv_type_text)
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 : :
6297 3811 : 0 : return convert_any_priv_string(priv_type_text, language_priv_map);
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 : : */
3830 : : Datum
8670 tgl@sss.pgh.pa.us 3831 :CBC 36 : has_schema_privilege_name_name(PG_FUNCTION_ARGS)
3832 : : {
3833 : 36 : Name username = PG_GETARG_NAME(0);
3341 noah@leadboat.com 3834 : 36 : text *schemaname = PG_GETARG_TEXT_PP(1);
3835 : 36 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3836 : : Oid roleid;
3837 : : Oid schemaoid;
3838 : : AclMode mode;
3839 : : AclResult aclresult;
3840 : :
5683 itagaki.takahiro@gma 3841 : 36 : roleid = get_role_oid_or_public(NameStr(*username));
8670 tgl@sss.pgh.pa.us 3842 : 36 : schemaoid = convert_schema_name(schemaname);
3843 : 36 : mode = convert_schema_priv_string(priv_type_text);
3844 : :
1269 peter@eisentraut.org 3845 : 36 : aclresult = object_aclcheck(NamespaceRelationId, schemaoid, roleid, mode);
3846 : :
8670 tgl@sss.pgh.pa.us 3847 : 36 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3848 : : }
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 : : */
3856 : : Datum
8670 tgl@sss.pgh.pa.us 3857 :UBC 0 : has_schema_privilege_name(PG_FUNCTION_ARGS)
3858 : : {
3341 noah@leadboat.com 3859 : 0 : text *schemaname = PG_GETARG_TEXT_PP(0);
3860 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3861 : : Oid roleid;
3862 : : Oid schemaoid;
3863 : : AclMode mode;
3864 : : AclResult aclresult;
3865 : :
7616 tgl@sss.pgh.pa.us 3866 : 0 : roleid = GetUserId();
8670 3867 : 0 : schemaoid = convert_schema_name(schemaname);
3868 : 0 : mode = convert_schema_priv_string(priv_type_text);
3869 : :
1269 peter@eisentraut.org 3870 : 0 : aclresult = object_aclcheck(NamespaceRelationId, schemaoid, roleid, mode);
3871 : :
8670 tgl@sss.pgh.pa.us 3872 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3873 : : }
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 : : */
3880 : : Datum
3881 : 0 : has_schema_privilege_name_id(PG_FUNCTION_ARGS)
3882 : : {
3883 : 0 : Name username = PG_GETARG_NAME(0);
3884 : 0 : Oid schemaoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 3885 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3886 : : Oid roleid;
3887 : : AclMode mode;
3888 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3889 : 0 : bool is_missing = false;
3890 : :
5683 itagaki.takahiro@gma 3891 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
8670 tgl@sss.pgh.pa.us 3892 : 0 : mode = convert_schema_priv_string(priv_type_text);
3893 : :
934 3894 : 0 : aclresult = object_aclcheck_ext(NamespaceRelationId, schemaoid,
3895 : : roleid, mode,
3896 : : &is_missing);
3897 : :
3898 [ # # ]: 0 : if (is_missing)
3899 : 0 : PG_RETURN_NULL();
3900 : :
8670 3901 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3902 : : }
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 : : */
3910 : : Datum
3911 : 0 : has_schema_privilege_id(PG_FUNCTION_ARGS)
3912 : : {
3913 : 0 : Oid schemaoid = PG_GETARG_OID(0);
3341 noah@leadboat.com 3914 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
3915 : : Oid roleid;
3916 : : AclMode mode;
3917 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3918 : 0 : bool is_missing = false;
3919 : :
7616 3920 : 0 : roleid = GetUserId();
8670 3921 : 0 : mode = convert_schema_priv_string(priv_type_text);
3922 : :
934 3923 : 0 : aclresult = object_aclcheck_ext(NamespaceRelationId, schemaoid,
3924 : : roleid, mode,
3925 : : &is_missing);
3926 : :
3927 [ # # ]: 0 : if (is_missing)
3928 : 0 : PG_RETURN_NULL();
3929 : :
8670 3930 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3931 : : }
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 : : */
3938 : : Datum
3939 : 0 : has_schema_privilege_id_name(PG_FUNCTION_ARGS)
3940 : : {
7616 3941 : 0 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 3942 : 0 : text *schemaname = PG_GETARG_TEXT_PP(1);
3943 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3944 : : Oid schemaoid;
3945 : : AclMode mode;
3946 : : AclResult aclresult;
3947 : :
8670 tgl@sss.pgh.pa.us 3948 : 0 : schemaoid = convert_schema_name(schemaname);
3949 : 0 : mode = convert_schema_priv_string(priv_type_text);
3950 : :
1269 peter@eisentraut.org 3951 : 0 : aclresult = object_aclcheck(NamespaceRelationId, schemaoid, roleid, mode);
3952 : :
8670 tgl@sss.pgh.pa.us 3953 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3954 : : }
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 : : */
3961 : : Datum
3962 : 0 : has_schema_privilege_id_id(PG_FUNCTION_ARGS)
3963 : : {
7616 3964 : 0 : Oid roleid = PG_GETARG_OID(0);
8670 3965 : 0 : Oid schemaoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 3966 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
3967 : : AclMode mode;
3968 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 3969 : 0 : bool is_missing = false;
3970 : :
8670 3971 : 0 : mode = convert_schema_priv_string(priv_type_text);
3972 : :
934 3973 : 0 : aclresult = object_aclcheck_ext(NamespaceRelationId, schemaoid,
3974 : : roleid, mode,
3975 : : &is_missing);
3976 : :
3977 [ # # ]: 0 : if (is_missing)
3978 : 0 : PG_RETURN_NULL();
3979 : :
8670 3980 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
3981 : : }
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 : : */
3990 : : static Oid
8670 tgl@sss.pgh.pa.us 3991 :CBC 36 : convert_schema_name(text *schemaname)
3992 : : {
6615 3993 : 36 : char *nspname = text_to_cstring(schemaname);
3994 : :
5752 rhaas@postgresql.org 3995 : 36 : return get_namespace_oid(nspname, false);
3996 : : }
3997 : :
3998 : : /*
3999 : : * convert_schema_priv_string
4000 : : * Convert text string to AclMode value.
4001 : : */
4002 : : static AclMode
8670 tgl@sss.pgh.pa.us 4003 : 36 : convert_schema_priv_string(text *priv_type_text)
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 : :
6297 4013 : 36 : return convert_any_priv_string(priv_type_text, schema_priv_map);
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 : : */
4032 : : Datum
6346 peter_e@gmx.net 4033 : 8 : has_server_privilege_name_name(PG_FUNCTION_ARGS)
4034 : : {
4035 : 8 : Name username = PG_GETARG_NAME(0);
3341 noah@leadboat.com 4036 : 8 : text *servername = PG_GETARG_TEXT_PP(1);
4037 : 8 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4038 : : Oid roleid;
4039 : : Oid serverid;
4040 : : AclMode mode;
4041 : : AclResult aclresult;
4042 : :
5683 itagaki.takahiro@gma 4043 : 8 : roleid = get_role_oid_or_public(NameStr(*username));
6297 tgl@sss.pgh.pa.us 4044 : 8 : serverid = convert_server_name(servername);
4045 : 8 : mode = convert_server_priv_string(priv_type_text);
4046 : :
1269 peter@eisentraut.org 4047 : 8 : aclresult = object_aclcheck(ForeignServerRelationId, serverid, roleid, mode);
4048 : :
6297 tgl@sss.pgh.pa.us 4049 : 8 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4058 : : Datum
6346 peter_e@gmx.net 4059 : 4 : has_server_privilege_name(PG_FUNCTION_ARGS)
4060 : : {
3341 noah@leadboat.com 4061 : 4 : text *servername = PG_GETARG_TEXT_PP(0);
4062 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4063 : : Oid roleid;
4064 : : Oid serverid;
4065 : : AclMode mode;
4066 : : AclResult aclresult;
4067 : :
6297 tgl@sss.pgh.pa.us 4068 : 4 : roleid = GetUserId();
4069 : 4 : serverid = convert_server_name(servername);
4070 : 4 : mode = convert_server_priv_string(priv_type_text);
4071 : :
1269 peter@eisentraut.org 4072 : 4 : aclresult = object_aclcheck(ForeignServerRelationId, serverid, roleid, mode);
4073 : :
6297 tgl@sss.pgh.pa.us 4074 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4082 : : Datum
6346 peter_e@gmx.net 4083 : 4 : has_server_privilege_name_id(PG_FUNCTION_ARGS)
4084 : : {
4085 : 4 : Name username = PG_GETARG_NAME(0);
4086 : 4 : Oid serverid = PG_GETARG_OID(1);
3341 noah@leadboat.com 4087 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4088 : : Oid roleid;
4089 : : AclMode mode;
4090 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 4091 : 4 : bool is_missing = false;
4092 : :
5683 itagaki.takahiro@gma 4093 : 4 : roleid = get_role_oid_or_public(NameStr(*username));
6297 tgl@sss.pgh.pa.us 4094 : 4 : mode = convert_server_priv_string(priv_type_text);
4095 : :
934 4096 : 4 : aclresult = object_aclcheck_ext(ForeignServerRelationId, serverid,
4097 : : roleid, mode,
4098 : : &is_missing);
4099 : :
4100 [ - + ]: 4 : if (is_missing)
934 tgl@sss.pgh.pa.us 4101 :UBC 0 : PG_RETURN_NULL();
4102 : :
6297 tgl@sss.pgh.pa.us 4103 :CBC 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4104 : : }
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 : : */
4112 : : Datum
6346 peter_e@gmx.net 4113 : 52 : has_server_privilege_id(PG_FUNCTION_ARGS)
4114 : : {
4115 : 52 : Oid serverid = PG_GETARG_OID(0);
3341 noah@leadboat.com 4116 : 52 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4117 : : Oid roleid;
4118 : : AclMode mode;
4119 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 4120 : 52 : bool is_missing = false;
4121 : :
6297 4122 : 52 : roleid = GetUserId();
4123 : 52 : mode = convert_server_priv_string(priv_type_text);
4124 : :
934 4125 : 52 : aclresult = object_aclcheck_ext(ForeignServerRelationId, serverid,
4126 : : roleid, mode,
4127 : : &is_missing);
4128 : :
4129 [ - + ]: 52 : if (is_missing)
934 tgl@sss.pgh.pa.us 4130 :UBC 0 : PG_RETURN_NULL();
4131 : :
6297 tgl@sss.pgh.pa.us 4132 :CBC 52 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4140 : : Datum
6346 peter_e@gmx.net 4141 : 4 : has_server_privilege_id_name(PG_FUNCTION_ARGS)
4142 : : {
4143 : 4 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 4144 : 4 : text *servername = PG_GETARG_TEXT_PP(1);
4145 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4146 : : Oid serverid;
4147 : : AclMode mode;
4148 : : AclResult aclresult;
4149 : :
6297 tgl@sss.pgh.pa.us 4150 : 4 : serverid = convert_server_name(servername);
4151 : 4 : mode = convert_server_priv_string(priv_type_text);
4152 : :
1269 peter@eisentraut.org 4153 : 4 : aclresult = object_aclcheck(ForeignServerRelationId, serverid, roleid, mode);
4154 : :
6297 tgl@sss.pgh.pa.us 4155 : 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4163 : : Datum
6346 peter_e@gmx.net 4164 : 4 : has_server_privilege_id_id(PG_FUNCTION_ARGS)
4165 : : {
4166 : 4 : Oid roleid = PG_GETARG_OID(0);
4167 : 4 : Oid serverid = PG_GETARG_OID(1);
3341 noah@leadboat.com 4168 : 4 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4169 : : AclMode mode;
4170 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 4171 : 4 : bool is_missing = false;
4172 : :
6297 4173 : 4 : mode = convert_server_priv_string(priv_type_text);
4174 : :
934 4175 : 4 : aclresult = object_aclcheck_ext(ForeignServerRelationId, serverid,
4176 : : roleid, mode,
4177 : : &is_missing);
4178 : :
4179 [ - + ]: 4 : if (is_missing)
934 tgl@sss.pgh.pa.us 4180 :UBC 0 : PG_RETURN_NULL();
4181 : :
6297 tgl@sss.pgh.pa.us 4182 :CBC 4 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4192 : : static Oid
4193 : 16 : convert_server_name(text *servername)
4194 : : {
4195 : 16 : char *serverstr = text_to_cstring(servername);
4196 : :
5513 rhaas@postgresql.org 4197 : 16 : return get_foreign_server_oid(serverstr, false);
4198 : : }
4199 : :
4200 : : /*
4201 : : * convert_server_priv_string
4202 : : * Convert text string to AclMode value.
4203 : : */
4204 : : static AclMode
6297 tgl@sss.pgh.pa.us 4205 : 76 : convert_server_priv_string(text *priv_type_text)
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 : :
4213 : 76 : return convert_any_priv_string(priv_type_text, server_priv_map);
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 : : */
4232 : : Datum
7967 bruce@momjian.us 4233 :UBC 0 : has_tablespace_privilege_name_name(PG_FUNCTION_ARGS)
4234 : : {
4235 : 0 : Name username = PG_GETARG_NAME(0);
3341 noah@leadboat.com 4236 : 0 : text *tablespacename = PG_GETARG_TEXT_PP(1);
4237 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4238 : : Oid roleid;
4239 : : Oid tablespaceoid;
4240 : : AclMode mode;
4241 : : AclResult aclresult;
4242 : :
5683 itagaki.takahiro@gma 4243 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
7967 bruce@momjian.us 4244 : 0 : tablespaceoid = convert_tablespace_name(tablespacename);
4245 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4246 : :
1269 peter@eisentraut.org 4247 : 0 : aclresult = object_aclcheck(TableSpaceRelationId, tablespaceoid, roleid, mode);
4248 : :
7967 bruce@momjian.us 4249 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4250 : : }
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 : : */
4258 : : Datum
4259 : 0 : has_tablespace_privilege_name(PG_FUNCTION_ARGS)
4260 : : {
3341 noah@leadboat.com 4261 : 0 : text *tablespacename = PG_GETARG_TEXT_PP(0);
4262 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4263 : : Oid roleid;
4264 : : Oid tablespaceoid;
4265 : : AclMode mode;
4266 : : AclResult aclresult;
4267 : :
7616 tgl@sss.pgh.pa.us 4268 : 0 : roleid = GetUserId();
7967 bruce@momjian.us 4269 : 0 : tablespaceoid = convert_tablespace_name(tablespacename);
4270 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4271 : :
1269 peter@eisentraut.org 4272 : 0 : aclresult = object_aclcheck(TableSpaceRelationId, tablespaceoid, roleid, mode);
4273 : :
7967 bruce@momjian.us 4274 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4275 : : }
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 : : */
4282 : : Datum
4283 : 0 : has_tablespace_privilege_name_id(PG_FUNCTION_ARGS)
4284 : : {
4285 : 0 : Name username = PG_GETARG_NAME(0);
4286 : 0 : Oid tablespaceoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 4287 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4288 : : Oid roleid;
4289 : : AclMode mode;
4290 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 4291 : 0 : bool is_missing = false;
4292 : :
5683 itagaki.takahiro@gma 4293 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
7967 bruce@momjian.us 4294 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4295 : :
934 tgl@sss.pgh.pa.us 4296 : 0 : aclresult = object_aclcheck_ext(TableSpaceRelationId, tablespaceoid,
4297 : : roleid, mode,
4298 : : &is_missing);
4299 : :
4300 [ # # ]: 0 : if (is_missing)
4301 : 0 : PG_RETURN_NULL();
4302 : :
7967 bruce@momjian.us 4303 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4304 : : }
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 : : */
4312 : : Datum
4313 : 0 : has_tablespace_privilege_id(PG_FUNCTION_ARGS)
4314 : : {
4315 : 0 : Oid tablespaceoid = PG_GETARG_OID(0);
3341 noah@leadboat.com 4316 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4317 : : Oid roleid;
4318 : : AclMode mode;
4319 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 4320 : 0 : bool is_missing = false;
4321 : :
7616 4322 : 0 : roleid = GetUserId();
7967 bruce@momjian.us 4323 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4324 : :
934 tgl@sss.pgh.pa.us 4325 : 0 : aclresult = object_aclcheck_ext(TableSpaceRelationId, tablespaceoid,
4326 : : roleid, mode,
4327 : : &is_missing);
4328 : :
4329 [ # # ]: 0 : if (is_missing)
4330 : 0 : PG_RETURN_NULL();
4331 : :
7967 bruce@momjian.us 4332 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4333 : : }
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 : : */
4340 : : Datum
4341 : 0 : has_tablespace_privilege_id_name(PG_FUNCTION_ARGS)
4342 : : {
7616 tgl@sss.pgh.pa.us 4343 : 0 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 4344 : 0 : text *tablespacename = PG_GETARG_TEXT_PP(1);
4345 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4346 : : Oid tablespaceoid;
4347 : : AclMode mode;
4348 : : AclResult aclresult;
4349 : :
7967 bruce@momjian.us 4350 : 0 : tablespaceoid = convert_tablespace_name(tablespacename);
4351 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4352 : :
1269 peter@eisentraut.org 4353 : 0 : aclresult = object_aclcheck(TableSpaceRelationId, tablespaceoid, roleid, mode);
4354 : :
7967 bruce@momjian.us 4355 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4356 : : }
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 : : */
4363 : : Datum
4364 : 0 : has_tablespace_privilege_id_id(PG_FUNCTION_ARGS)
4365 : : {
7616 tgl@sss.pgh.pa.us 4366 : 0 : Oid roleid = PG_GETARG_OID(0);
7967 bruce@momjian.us 4367 : 0 : Oid tablespaceoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 4368 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4369 : : AclMode mode;
4370 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 4371 : 0 : bool is_missing = false;
4372 : :
7967 bruce@momjian.us 4373 : 0 : mode = convert_tablespace_priv_string(priv_type_text);
4374 : :
934 tgl@sss.pgh.pa.us 4375 : 0 : aclresult = object_aclcheck_ext(TableSpaceRelationId, tablespaceoid,
4376 : : roleid, mode,
4377 : : &is_missing);
4378 : :
4379 [ # # ]: 0 : if (is_missing)
4380 : 0 : PG_RETURN_NULL();
4381 : :
7967 bruce@momjian.us 4382 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4383 : : }
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 : : */
4392 : : static Oid
4393 : 0 : convert_tablespace_name(text *tablespacename)
4394 : : {
6615 tgl@sss.pgh.pa.us 4395 : 0 : char *spcname = text_to_cstring(tablespacename);
4396 : :
5752 rhaas@postgresql.org 4397 : 0 : return get_tablespace_oid(spcname, false);
4398 : : }
4399 : :
4400 : : /*
4401 : : * convert_tablespace_priv_string
4402 : : * Convert text string to AclMode value.
4403 : : */
4404 : : static AclMode
7967 bruce@momjian.us 4405 : 0 : convert_tablespace_priv_string(text *priv_type_text)
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 : :
6297 tgl@sss.pgh.pa.us 4413 : 0 : return convert_any_priv_string(priv_type_text, tablespace_priv_map);
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 : : */
4431 : : Datum
5250 peter_e@gmx.net 4432 :CBC 8 : has_type_privilege_name_name(PG_FUNCTION_ARGS)
4433 : : {
4434 : 8 : Name username = PG_GETARG_NAME(0);
3341 noah@leadboat.com 4435 : 8 : text *typename = PG_GETARG_TEXT_PP(1);
4436 : 8 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4437 : : Oid roleid;
4438 : : Oid typeoid;
4439 : : AclMode mode;
4440 : : AclResult aclresult;
4441 : :
5250 peter_e@gmx.net 4442 : 8 : roleid = get_role_oid_or_public(NameStr(*username));
4443 : 8 : typeoid = convert_type_name(typename);
4444 : 8 : mode = convert_type_priv_string(priv_type_text);
4445 : :
1269 peter@eisentraut.org 4446 : 8 : aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
4447 : :
5250 peter_e@gmx.net 4448 : 8 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4457 : : Datum
5250 peter_e@gmx.net 4458 :UBC 0 : has_type_privilege_name(PG_FUNCTION_ARGS)
4459 : : {
3341 noah@leadboat.com 4460 : 0 : text *typename = PG_GETARG_TEXT_PP(0);
4461 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4462 : : Oid roleid;
4463 : : Oid typeoid;
4464 : : AclMode mode;
4465 : : AclResult aclresult;
4466 : :
5250 peter_e@gmx.net 4467 : 0 : roleid = GetUserId();
4468 : 0 : typeoid = convert_type_name(typename);
4469 : 0 : mode = convert_type_priv_string(priv_type_text);
4470 : :
1269 peter@eisentraut.org 4471 : 0 : aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
4472 : :
5250 peter_e@gmx.net 4473 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4481 : : Datum
4482 : 0 : has_type_privilege_name_id(PG_FUNCTION_ARGS)
4483 : : {
4484 : 0 : Name username = PG_GETARG_NAME(0);
4485 : 0 : Oid typeoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 4486 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4487 : : Oid roleid;
4488 : : AclMode mode;
4489 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 4490 : 0 : bool is_missing = false;
4491 : :
5250 peter_e@gmx.net 4492 : 0 : roleid = get_role_oid_or_public(NameStr(*username));
4493 : 0 : mode = convert_type_priv_string(priv_type_text);
4494 : :
934 tgl@sss.pgh.pa.us 4495 : 0 : aclresult = object_aclcheck_ext(TypeRelationId, typeoid,
4496 : : roleid, mode,
4497 : : &is_missing);
4498 : :
4499 [ # # ]: 0 : if (is_missing)
4500 : 0 : PG_RETURN_NULL();
4501 : :
5250 peter_e@gmx.net 4502 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4511 : : Datum
4512 : 0 : has_type_privilege_id(PG_FUNCTION_ARGS)
4513 : : {
4514 : 0 : Oid typeoid = PG_GETARG_OID(0);
3341 noah@leadboat.com 4515 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4516 : : Oid roleid;
4517 : : AclMode mode;
4518 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 4519 : 0 : bool is_missing = false;
4520 : :
5250 peter_e@gmx.net 4521 : 0 : roleid = GetUserId();
4522 : 0 : mode = convert_type_priv_string(priv_type_text);
4523 : :
934 tgl@sss.pgh.pa.us 4524 : 0 : aclresult = object_aclcheck_ext(TypeRelationId, typeoid,
4525 : : roleid, mode,
4526 : : &is_missing);
4527 : :
4528 [ # # ]: 0 : if (is_missing)
4529 : 0 : PG_RETURN_NULL();
4530 : :
5250 peter_e@gmx.net 4531 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4539 : : Datum
4540 : 0 : has_type_privilege_id_name(PG_FUNCTION_ARGS)
4541 : : {
4542 : 0 : Oid roleid = PG_GETARG_OID(0);
3341 noah@leadboat.com 4543 : 0 : text *typename = PG_GETARG_TEXT_PP(1);
4544 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4545 : : Oid typeoid;
4546 : : AclMode mode;
4547 : : AclResult aclresult;
4548 : :
5250 peter_e@gmx.net 4549 : 0 : typeoid = convert_type_name(typename);
4550 : 0 : mode = convert_type_priv_string(priv_type_text);
4551 : :
1269 peter@eisentraut.org 4552 : 0 : aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
4553 : :
5250 peter_e@gmx.net 4554 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4562 : : Datum
4563 : 0 : has_type_privilege_id_id(PG_FUNCTION_ARGS)
4564 : : {
4565 : 0 : Oid roleid = PG_GETARG_OID(0);
4566 : 0 : Oid typeoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 4567 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4568 : : AclMode mode;
4569 : : AclResult aclresult;
934 tgl@sss.pgh.pa.us 4570 : 0 : bool is_missing = false;
4571 : :
5250 peter_e@gmx.net 4572 : 0 : mode = convert_type_priv_string(priv_type_text);
4573 : :
934 tgl@sss.pgh.pa.us 4574 : 0 : aclresult = object_aclcheck_ext(TypeRelationId, typeoid,
4575 : : roleid, mode,
4576 : : &is_missing);
4577 : :
4578 [ # # ]: 0 : if (is_missing)
4579 : 0 : PG_RETURN_NULL();
4580 : :
5250 peter_e@gmx.net 4581 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4591 : : static Oid
5250 peter_e@gmx.net 4592 :CBC 8 : convert_type_name(text *typename)
4593 : : {
4594 : 8 : char *typname = text_to_cstring(typename);
4595 : : Oid oid;
4596 : :
4597 : 8 : oid = DatumGetObjectId(DirectFunctionCall1(regtypein,
4598 : : CStringGetDatum(typname)));
4599 : :
4600 [ - + ]: 8 : if (!OidIsValid(oid))
5250 peter_e@gmx.net 4601 [ # # ]:UBC 0 : ereport(ERROR,
4602 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
4603 : : errmsg("type \"%s\" does not exist", typname)));
4604 : :
5250 peter_e@gmx.net 4605 :CBC 8 : return oid;
4606 : : }
4607 : :
4608 : : /*
4609 : : * convert_type_priv_string
4610 : : * Convert text string to AclMode value.
4611 : : */
4612 : : static AclMode
4613 : 8 : convert_type_priv_string(text *priv_type_text)
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 : :
4621 : 8 : return convert_any_priv_string(priv_type_text, type_priv_map);
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 : : */
4640 : : static bool
1490 tgl@sss.pgh.pa.us 4641 : 37 : has_param_priv_byname(Oid roleid, const text *parameter, AclMode priv)
4642 : : {
4643 : 37 : char *paramstr = text_to_cstring(parameter);
4644 : :
4645 : 37 : 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 : : */
4653 : : Datum
4654 : 42 : has_parameter_privilege_name_name(PG_FUNCTION_ARGS)
4655 : : {
4656 : 42 : Name username = PG_GETARG_NAME(0);
4657 : 42 : text *parameter = PG_GETARG_TEXT_PP(1);
4658 : 42 : AclMode priv = convert_parameter_priv_string(PG_GETARG_TEXT_PP(2));
4659 : 35 : Oid roleid = get_role_oid_or_public(NameStr(*username));
4660 : :
4661 : 35 : PG_RETURN_BOOL(has_param_priv_byname(roleid, parameter, priv));
4662 : : }
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 : : */
4669 : : Datum
4670 : 1 : has_parameter_privilege_name(PG_FUNCTION_ARGS)
4671 : : {
4672 : 1 : text *parameter = PG_GETARG_TEXT_PP(0);
4673 : 1 : AclMode priv = convert_parameter_priv_string(PG_GETARG_TEXT_PP(1));
4674 : :
4675 : 1 : PG_RETURN_BOOL(has_param_priv_byname(GetUserId(), parameter, priv));
4676 : : }
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 : : */
4683 : : Datum
4684 : 1 : has_parameter_privilege_id_name(PG_FUNCTION_ARGS)
4685 : : {
4686 : 1 : Oid roleid = PG_GETARG_OID(0);
4687 : 1 : text *parameter = PG_GETARG_TEXT_PP(1);
4688 : 1 : AclMode priv = convert_parameter_priv_string(PG_GETARG_TEXT_PP(2));
4689 : :
4690 : 1 : PG_RETURN_BOOL(has_param_priv_byname(roleid, parameter, priv));
4691 : : }
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 : : */
4701 : : static AclMode
4702 : 44 : convert_parameter_priv_string(text *priv_text)
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 : :
4712 : 44 : return convert_any_priv_string(priv_text, parameter_priv_map);
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 : : */
4731 : : static bool
600 fujii@postgresql.org 4732 : 140 : has_lo_priv_byid(Oid roleid, Oid lobjId, AclMode priv, bool *is_missing)
4733 : : {
4734 : 140 : Snapshot snapshot = NULL;
4735 : : AclResult aclresult;
4736 : :
4737 [ + + ]: 140 : if (priv & ACL_UPDATE)
4738 : 64 : snapshot = NULL;
4739 : : else
4740 : 76 : snapshot = GetActiveSnapshot();
4741 : :
4742 [ + + ]: 140 : if (!LargeObjectExistsWithSnapshot(lobjId, snapshot))
4743 : : {
4744 [ - + ]: 4 : Assert(is_missing != NULL);
4745 : 4 : *is_missing = true;
4746 : 4 : return false;
4747 : : }
4748 : :
4749 [ + + ]: 136 : if (lo_compat_privileges)
4750 : 8 : return true;
4751 : :
4752 : 128 : aclresult = pg_largeobject_aclcheck_snapshot(lobjId,
4753 : : roleid,
4754 : : priv,
4755 : : snapshot);
4756 : 128 : 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 : : */
4764 : : Datum
4765 : 56 : has_largeobject_privilege_name_id(PG_FUNCTION_ARGS)
4766 : : {
4767 : 56 : Name username = PG_GETARG_NAME(0);
4768 : 56 : Oid roleid = get_role_oid_or_public(NameStr(*username));
4769 : 56 : Oid lobjId = PG_GETARG_OID(1);
4770 : 56 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4771 : : AclMode mode;
4772 : 56 : bool is_missing = false;
4773 : : bool result;
4774 : :
4775 : 56 : mode = convert_largeobject_priv_string(priv_type_text);
4776 : 56 : result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
4777 : :
4778 [ - + ]: 56 : if (is_missing)
600 fujii@postgresql.org 4779 :UBC 0 : PG_RETURN_NULL();
4780 : :
600 fujii@postgresql.org 4781 :CBC 56 : 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 : : */
4790 : : Datum
4791 : 84 : has_largeobject_privilege_id(PG_FUNCTION_ARGS)
4792 : : {
4793 : 84 : Oid lobjId = PG_GETARG_OID(0);
4794 : 84 : Oid roleid = GetUserId();
4795 : 84 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4796 : : AclMode mode;
4797 : 84 : bool is_missing = false;
4798 : : bool result;
4799 : :
4800 : 84 : mode = convert_largeobject_priv_string(priv_type_text);
4801 : 84 : result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
4802 : :
4803 [ + + ]: 84 : if (is_missing)
4804 : 4 : PG_RETURN_NULL();
4805 : :
4806 : 80 : 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 : : */
4814 : : Datum
600 fujii@postgresql.org 4815 :UBC 0 : has_largeobject_privilege_id_id(PG_FUNCTION_ARGS)
4816 : : {
4817 : 0 : Oid roleid = PG_GETARG_OID(0);
4818 : 0 : Oid lobjId = PG_GETARG_OID(1);
4819 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4820 : : AclMode mode;
4821 : 0 : bool is_missing = false;
4822 : : bool result;
4823 : :
4824 : 0 : mode = convert_largeobject_priv_string(priv_type_text);
4825 : 0 : result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
4826 : :
4827 [ # # ]: 0 : if (is_missing)
4828 : 0 : PG_RETURN_NULL();
4829 : :
4830 : 0 : PG_RETURN_BOOL(result);
4831 : : }
4832 : :
4833 : : /*
4834 : : * convert_largeobject_priv_string
4835 : : * Convert text string to AclMode value.
4836 : : */
4837 : : static AclMode
600 fujii@postgresql.org 4838 :CBC 140 : convert_largeobject_priv_string(text *priv_type_text)
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 : :
4848 : 140 : return convert_any_priv_string(priv_type_text, largeobject_priv_map);
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 : : */
4866 : : Datum
7588 tgl@sss.pgh.pa.us 4867 : 24 : pg_has_role_name_name(PG_FUNCTION_ARGS)
4868 : : {
4869 : 24 : Name username = PG_GETARG_NAME(0);
4870 : 24 : Name rolename = PG_GETARG_NAME(1);
3341 noah@leadboat.com 4871 : 24 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4872 : : Oid roleid;
4873 : : Oid roleoid;
4874 : : AclMode mode;
4875 : : AclResult aclresult;
4876 : :
5752 rhaas@postgresql.org 4877 : 24 : roleid = get_role_oid(NameStr(*username), false);
4878 : 24 : roleoid = get_role_oid(NameStr(*rolename), false);
7588 tgl@sss.pgh.pa.us 4879 : 24 : mode = convert_role_priv_string(priv_type_text);
4880 : :
4881 : 24 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4882 : :
4883 : 24 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4892 : : Datum
4893 : 12 : pg_has_role_name(PG_FUNCTION_ARGS)
4894 : : {
4895 : 12 : Name rolename = PG_GETARG_NAME(0);
3341 noah@leadboat.com 4896 : 12 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4897 : : Oid roleid;
4898 : : Oid roleoid;
4899 : : AclMode mode;
4900 : : AclResult aclresult;
4901 : :
7588 tgl@sss.pgh.pa.us 4902 : 12 : roleid = GetUserId();
5752 rhaas@postgresql.org 4903 : 12 : roleoid = get_role_oid(NameStr(*rolename), false);
7588 tgl@sss.pgh.pa.us 4904 : 12 : mode = convert_role_priv_string(priv_type_text);
4905 : :
4906 : 12 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4907 : :
4908 : 12 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4916 : : Datum
7588 tgl@sss.pgh.pa.us 4917 :UBC 0 : pg_has_role_name_id(PG_FUNCTION_ARGS)
4918 : : {
4919 : 0 : Name username = PG_GETARG_NAME(0);
4920 : 0 : Oid roleoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 4921 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4922 : : Oid roleid;
4923 : : AclMode mode;
4924 : : AclResult aclresult;
4925 : :
5752 rhaas@postgresql.org 4926 : 0 : roleid = get_role_oid(NameStr(*username), false);
7588 tgl@sss.pgh.pa.us 4927 : 0 : mode = convert_role_priv_string(priv_type_text);
4928 : :
4929 : 0 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4930 : :
4931 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4940 : : Datum
7588 tgl@sss.pgh.pa.us 4941 :CBC 67027 : pg_has_role_id(PG_FUNCTION_ARGS)
4942 : : {
4943 : 67027 : Oid roleoid = PG_GETARG_OID(0);
3341 noah@leadboat.com 4944 : 67027 : text *priv_type_text = PG_GETARG_TEXT_PP(1);
4945 : : Oid roleid;
4946 : : AclMode mode;
4947 : : AclResult aclresult;
4948 : :
7588 tgl@sss.pgh.pa.us 4949 : 67027 : roleid = GetUserId();
4950 : 67027 : mode = convert_role_priv_string(priv_type_text);
4951 : :
4952 : 67027 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4953 : :
4954 : 67027 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4962 : : Datum
7588 tgl@sss.pgh.pa.us 4963 :UBC 0 : pg_has_role_id_name(PG_FUNCTION_ARGS)
4964 : : {
4965 : 0 : Oid roleid = PG_GETARG_OID(0);
4966 : 0 : Name rolename = PG_GETARG_NAME(1);
3341 noah@leadboat.com 4967 : 0 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4968 : : Oid roleoid;
4969 : : AclMode mode;
4970 : : AclResult aclresult;
4971 : :
5752 rhaas@postgresql.org 4972 : 0 : roleoid = get_role_oid(NameStr(*rolename), false);
7588 tgl@sss.pgh.pa.us 4973 : 0 : mode = convert_role_priv_string(priv_type_text);
4974 : :
4975 : 0 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4976 : :
4977 : 0 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
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 : : */
4985 : : Datum
7588 tgl@sss.pgh.pa.us 4986 :CBC 120 : pg_has_role_id_id(PG_FUNCTION_ARGS)
4987 : : {
4988 : 120 : Oid roleid = PG_GETARG_OID(0);
4989 : 120 : Oid roleoid = PG_GETARG_OID(1);
3341 noah@leadboat.com 4990 : 120 : text *priv_type_text = PG_GETARG_TEXT_PP(2);
4991 : : AclMode mode;
4992 : : AclResult aclresult;
4993 : :
7588 tgl@sss.pgh.pa.us 4994 : 120 : mode = convert_role_priv_string(priv_type_text);
4995 : :
4996 : 120 : aclresult = pg_role_aclcheck(roleoid, roleid, mode);
4997 : :
4998 : 120 : PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
4999 : : }
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 : : */
5015 : : static AclMode
5016 : 67183 : convert_role_priv_string(text *priv_type_text)
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 : :
6297 5031 : 67183 : return convert_any_priv_string(priv_type_text, role_priv_map);
5032 : : }
5033 : :
5034 : : /*
5035 : : * pg_role_aclcheck
5036 : : * Quick-and-dirty support for pg_has_role
5037 : : */
5038 : : static AclResult
7588 5039 : 67183 : pg_role_aclcheck(Oid role_oid, Oid roleid, AclMode mode)
5040 : : {
5041 [ + + ]: 67183 : if (mode & ACL_GRANT_OPTION_FOR(ACL_CREATE))
5042 : : {
5043 [ - + ]: 8 : if (is_admin_of_role(roleid, role_oid))
7588 tgl@sss.pgh.pa.us 5044 :UBC 0 : return ACLCHECK_OK;
5045 : : }
7588 tgl@sss.pgh.pa.us 5046 [ + + ]:CBC 67183 : if (mode & ACL_CREATE)
5047 : : {
5048 [ + + ]: 8 : if (is_member_of_role(roleid, role_oid))
5049 : 4 : return ACLCHECK_OK;
5050 : : }
5051 [ + + ]: 67179 : if (mode & ACL_USAGE)
5052 : : {
5053 [ + + ]: 67167 : if (has_privs_of_role(roleid, role_oid))
5054 : 66463 : return ACLCHECK_OK;
5055 : : }
1264 rhaas@postgresql.org 5056 [ - + ]: 716 : if (mode & ACL_SET)
5057 : : {
1264 rhaas@postgresql.org 5058 [ # # ]:UBC 0 : if (member_can_set_role(roleid, role_oid))
5059 : 0 : return ACLCHECK_OK;
5060 : : }
7588 tgl@sss.pgh.pa.us 5061 :CBC 716 : return ACLCHECK_NO_PRIV;
5062 : : }
5063 : :
5064 : :
5065 : : /*
5066 : : * initialization function (called by InitPostgres)
5067 : : */
5068 : : void
7616 5069 : 16863 : initialize_acl(void)
5070 : : {
5071 [ + + ]: 16863 : if (!IsBootstrapProcessingMode())
5072 : : {
1866 noah@leadboat.com 5073 : 16806 : cached_db_hash =
5074 : 16806 : GetSysCacheHashValue1(DATABASEOID,
5075 : : ObjectIdGetDatum(MyDatabaseId));
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 : : */
7616 tgl@sss.pgh.pa.us 5082 : 16806 : CacheRegisterSyscacheCallback(AUTHMEMROLEMEM,
5083 : : RoleMembershipCacheCallback,
5084 : : (Datum) 0);
1957 noah@leadboat.com 5085 : 16806 : CacheRegisterSyscacheCallback(AUTHOID,
5086 : : RoleMembershipCacheCallback,
5087 : : (Datum) 0);
1866 5088 : 16806 : CacheRegisterSyscacheCallback(DATABASEOID,
5089 : : RoleMembershipCacheCallback,
5090 : : (Datum) 0);
5091 : : }
7616 tgl@sss.pgh.pa.us 5092 : 16863 : }
5093 : :
5094 : : /*
5095 : : * RoleMembershipCacheCallback
5096 : : * Syscache inval callback function
5097 : : */
5098 : : static void
76 michael@paquier.xyz 5099 :GNC 40540 : RoleMembershipCacheCallback(Datum arg, SysCacheIdentifier cacheid,
5100 : : uint32 hashvalue)
5101 : : {
1866 noah@leadboat.com 5102 [ + + ]:CBC 40540 : if (cacheid == DATABASEOID &&
5103 [ + + + + ]: 5323 : hashvalue != cached_db_hash &&
5104 : : hashvalue != 0)
5105 : : {
5106 : 1567 : return; /* ignore pg_database changes for other DBs */
5107 : : }
5108 : :
5109 : : /* Force membership caches to be recomputed on next use */
5110 : 38973 : cached_role[ROLERECURSE_MEMBERS] = InvalidOid;
1264 rhaas@postgresql.org 5111 : 38973 : cached_role[ROLERECURSE_PRIVS] = InvalidOid;
5112 : 38973 : cached_role[ROLERECURSE_SETROLE] = InvalidOid;
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 : : */
5121 : : static inline List *
770 nathan@postgresql.or 5122 : 3047 : roles_list_append(List *roles_list, bloom_filter **bf, Oid role)
5123 : : {
5124 : 3047 : 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 [ - + - - ]: 3047 : if ((*bf && bloom_lacks_element(*bf, roleptr, sizeof(Oid))) ||
5135 [ + + ]: 3047 : !list_member_oid(roles_list, role))
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 [ + - - + ]: 4974 : if (*bf == NULL &&
5143 : 2487 : list_length(roles_list) > ROLES_LIST_BLOOM_THRESHOLD)
5144 : : {
770 nathan@postgresql.or 5145 :UBC 0 : *bf = bloom_create(ROLES_LIST_BLOOM_THRESHOLD * 10, work_mem, 0);
5146 [ # # # # : 0 : foreach_oid(roleid, roles_list)
# # ]
5147 : 0 : 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 : : */
770 nathan@postgresql.or 5154 :CBC 2487 : roles_list = lappend_oid(roles_list, role);
5155 [ - + ]: 2487 : if (*bf)
770 nathan@postgresql.or 5156 :UBC 0 : bloom_add_element(*bf, roleptr, sizeof(Oid));
5157 : : }
5158 : :
770 nathan@postgresql.or 5159 :CBC 3047 : 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 : : */
5181 : : static List *
1866 noah@leadboat.com 5182 : 44647 : roles_is_member_of(Oid roleid, enum RoleRecurseType type,
5183 : : Oid admin_of, Oid *admin_role)
5184 : : {
5185 : : Oid dba;
5186 : : List *roles_list;
5187 : : ListCell *l;
5188 : : List *new_cached_roles;
5189 : : MemoryContext oldctx;
770 nathan@postgresql.or 5190 : 44647 : bloom_filter *bf = NULL;
5191 : :
223 peter@eisentraut.org 5192 [ - + ]:GNC 44647 : Assert(OidIsValid(admin_of) == (admin_role != NULL));
1352 rhaas@postgresql.org 5193 [ + + ]:CBC 44647 : if (admin_role != NULL)
5194 : 602 : *admin_role = InvalidOid;
5195 : :
5196 : : /* If cache is valid and ADMIN OPTION not sought, just return the list */
1866 noah@leadboat.com 5197 [ + + + + ]: 44647 : if (cached_role[type] == roleid && !OidIsValid(admin_of) &&
5198 [ + - ]: 41664 : OidIsValid(cached_role[type]))
5199 : 41664 : 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 : : */
5206 [ + + ]: 2983 : if (!OidIsValid(MyDatabaseId))
5207 : 16 : dba = InvalidOid;
5208 : : else
5209 : : {
5210 : : HeapTuple dbtup;
5211 : :
5212 : 2967 : dbtup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
5213 [ - + ]: 2967 : if (!HeapTupleIsValid(dbtup))
1866 noah@leadboat.com 5214 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
1866 noah@leadboat.com 5215 :CBC 2967 : dba = ((Form_pg_database) GETSTRUCT(dbtup))->datdba;
5216 : 2967 : ReleaseSysCache(dbtup);
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 : : */
7512 tgl@sss.pgh.pa.us 5230 : 2983 : roles_list = list_make1_oid(roleid);
5231 : :
7588 5232 [ + - + + : 8453 : foreach(l, roles_list)
+ + ]
5233 : : {
7507 bruce@momjian.us 5234 : 5470 : Oid memberid = lfirst_oid(l);
5235 : : CatCList *memlist;
5236 : : int i;
5237 : :
5238 : : /* Find roles that memberid is directly a member of */
5924 rhaas@postgresql.org 5239 : 5470 : memlist = SearchSysCacheList1(AUTHMEMMEMROLE,
5240 : : ObjectIdGetDatum(memberid));
7588 tgl@sss.pgh.pa.us 5241 [ + + ]: 9984 : for (i = 0; i < memlist->n_members; i++)
5242 : : {
5243 : 4514 : HeapTuple tup = &memlist->members[i]->tuple;
1349 rhaas@postgresql.org 5244 : 4514 : Form_pg_auth_members form = (Form_pg_auth_members) GETSTRUCT(tup);
5245 : 4514 : 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 [ + + + + : 4514 : if (otherid == admin_of && form->admin_option &&
+ - ]
1352 5252 [ + + ]: 500 : OidIsValid(admin_of) && !OidIsValid(*admin_role))
5253 : 484 : *admin_role = memberid;
5254 : :
5255 : : /* If we're supposed to ignore non-heritable grants, do so. */
1344 5256 [ + + + + ]: 4514 : if (type == ROLERECURSE_PRIVS && !form->inherit_option)
5257 : 1391 : continue;
5258 : :
5259 : : /* If we're supposed to ignore non-SET grants, do so. */
1264 5260 [ + + + + ]: 3123 : if (type == ROLERECURSE_SETROLE && !form->set_option)
5261 : 88 : 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 : : */
770 nathan@postgresql.or 5268 : 3035 : roles_list = roles_list_append(roles_list, &bf, otherid);
5269 : : }
7616 tgl@sss.pgh.pa.us 5270 : 5470 : ReleaseSysCacheList(memlist);
5271 : :
5272 : : /* implement pg_database_owner implicit membership */
1866 noah@leadboat.com 5273 [ + + + - ]: 5470 : if (memberid == dba && OidIsValid(dba))
770 nathan@postgresql.or 5274 : 12 : roles_list = roles_list_append(roles_list, &bf,
5275 : : ROLE_PG_DATABASE_OWNER);
5276 : : }
5277 : :
5278 : : /*
5279 : : * Free the Bloom filter created by roles_list_append(), if there is one.
5280 : : */
5281 [ - + ]: 2983 : if (bf)
770 nathan@postgresql.or 5282 :UBC 0 : bloom_free(bf);
5283 : :
5284 : : /*
5285 : : * Copy the completed list into TopMemoryContext so it will persist.
5286 : : */
7616 tgl@sss.pgh.pa.us 5287 :CBC 2983 : oldctx = MemoryContextSwitchTo(TopMemoryContext);
1866 noah@leadboat.com 5288 : 2983 : new_cached_roles = list_copy(roles_list);
7616 tgl@sss.pgh.pa.us 5289 : 2983 : MemoryContextSwitchTo(oldctx);
7615 5290 : 2983 : list_free(roles_list);
5291 : :
5292 : : /*
5293 : : * Now safe to assign to state variable
5294 : : */
1866 noah@leadboat.com 5295 : 2983 : cached_role[type] = InvalidOid; /* just paranoia */
5296 : 2983 : list_free(cached_roles[type]);
5297 : 2983 : cached_roles[type] = new_cached_roles;
5298 : 2983 : cached_role[type] = roleid;
5299 : :
5300 : : /* And now we can return the answer */
5301 : 2983 : 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 : : */
5313 : : bool
7512 tgl@sss.pgh.pa.us 5314 : 226601 : has_privs_of_role(Oid member, Oid role)
5315 : : {
5316 : : /* Fast path for simple case */
5317 [ + + ]: 226601 : if (member == role)
5318 : 70049 : return true;
5319 : :
5320 : : /* Superusers have every privilege, so are part of every role */
5321 [ + + ]: 156552 : if (superuser_arg(member))
5322 : 113452 : 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 : : */
1866 noah@leadboat.com 5328 : 43100 : return list_member_oid(roles_is_member_of(member, ROLERECURSE_PRIVS,
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 : : */
5347 : : bool
1264 rhaas@postgresql.org 5348 : 339815 : member_can_set_role(Oid member, Oid role)
5349 : : {
5350 : : /* Fast path for simple case */
7512 tgl@sss.pgh.pa.us 5351 [ + + ]: 339815 : if (member == role)
5352 : 338764 : return true;
5353 : :
5354 : : /* Superusers have every privilege, so can always SET ROLE */
5355 [ + + ]: 1051 : if (superuser_arg(member))
5356 : 781 : 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 : : */
1264 rhaas@postgresql.org 5362 : 270 : return list_member_oid(roles_is_member_of(member, ROLERECURSE_SETROLE,
5363 : : InvalidOid, NULL),
5364 : : role);
5365 : : }
5366 : :
5367 : : /*
5368 : : * Permission violation error unless able to SET ROLE to target role.
5369 : : */
5370 : : void
5371 : 1401 : check_can_set_role(Oid member, Oid role)
5372 : : {
5373 [ + + ]: 1401 : if (!member_can_set_role(member, role))
7600 tgl@sss.pgh.pa.us 5374 [ + - ]: 104 : ereport(ERROR,
5375 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
5376 : : errmsg("must be able to SET ROLE \"%s\"",
5377 : : GetUserNameFromId(role, false))));
5378 : 1297 : }
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 : : */
5393 : : bool
1264 rhaas@postgresql.org 5394 : 8 : is_member_of_role(Oid member, Oid role)
5395 : : {
5396 : : /* Fast path for simple case */
5397 [ - + ]: 8 : if (member == role)
1264 rhaas@postgresql.org 5398 :UBC 0 : return true;
5399 : :
5400 : : /* Superusers have every privilege, so are part of every role */
1264 rhaas@postgresql.org 5401 [ - + ]:CBC 8 : if (superuser_arg(member))
1264 rhaas@postgresql.org 5402 :UBC 0 : 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 : : */
1264 rhaas@postgresql.org 5408 :CBC 8 : return list_member_oid(roles_is_member_of(member, ROLERECURSE_MEMBERS,
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 : : */
5421 : : bool
7487 tgl@sss.pgh.pa.us 5422 : 488 : is_member_of_role_nosuper(Oid member, Oid role)
5423 : : {
5424 : : /* Fast path for simple case */
5425 [ + + ]: 488 : if (member == role)
5426 : 13 : 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 : : */
1866 noah@leadboat.com 5432 : 475 : return list_member_oid(roles_is_member_of(member, ROLERECURSE_MEMBERS,
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 : : */
5443 : : bool
7615 tgl@sss.pgh.pa.us 5444 : 1887 : is_admin_of_role(Oid member, Oid role)
5445 : : {
5446 : : Oid admin_role;
5447 : :
7588 5448 [ + + ]: 1887 : if (superuser_arg(member))
5449 : 1555 : return true;
5450 : :
5451 : : /* By policy, a role cannot have WITH ADMIN OPTION on itself. */
4460 noah@leadboat.com 5452 [ + + ]: 332 : if (member == role)
1499 rhaas@postgresql.org 5453 : 12 : return false;
5454 : :
1352 5455 : 320 : (void) roles_is_member_of(member, ROLERECURSE_MEMBERS, role, &admin_role);
5456 : 320 : 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 : : */
5468 : : Oid
5469 : 286 : select_best_admin(Oid member, Oid role)
5470 : : {
5471 : : Oid admin_role;
5472 : :
5473 : : /* By policy, a role cannot have WITH ADMIN OPTION on itself. */
5474 [ + + ]: 286 : if (member == role)
5475 : 4 : return InvalidOid;
5476 : :
5477 : 282 : (void) roles_is_member_of(member, ROLERECURSE_PRIVS, role, &admin_role);
5478 : 282 : 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 : : */
5507 : : void
47 nathan@postgresql.or 5508 :GNC 40729 : select_best_grantor(const RoleSpec *grantedBy, AclMode privileges,
5509 : : const Acl *acl, Oid ownerId,
5510 : : Oid *grantorId, AclMode *grantOptions)
5511 : : {
5512 : 40729 : Oid roleId = GetUserId();
7512 tgl@sss.pgh.pa.us 5513 :CBC 40729 : AclMode needed_goptions = ACL_GRANT_OPTION_FOR(privileges);
5514 : : List *roles_list;
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 : : */
47 nathan@postgresql.or 5522 [ + + ]:GNC 40729 : if (grantedBy)
5523 : : {
5524 : 76 : Oid grantor = get_rolespec_oid(grantedBy, false);
5525 : :
5526 [ + + ]: 76 : if (!has_privs_of_role(roleId, grantor))
5527 [ + - ]: 4 : ereport(ERROR,
5528 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
5529 : : errmsg("must inherit privileges of role \"%s\"",
5530 : : GetUserNameFromId(grantor, false))));
5531 : : /* Use exactly that grantor, whether it has privileges or not */
5532 : 72 : *grantorId = grantor;
5533 : 72 : *grantOptions = aclmask_direct(acl, grantor, ownerId,
5534 : : needed_goptions, ACLMASK_ALL);
5535 : 72 : 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 : : */
7512 tgl@sss.pgh.pa.us 5544 [ + + + + ]:CBC 40653 : if (roleId == ownerId || superuser_arg(roleId))
5545 : : {
5546 : 40461 : *grantorId = ownerId;
5547 : 40461 : *grantOptions = needed_goptions;
5548 : 40461 : 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 : : */
1866 noah@leadboat.com 5557 : 192 : roles_list = roles_is_member_of(roleId, ROLERECURSE_PRIVS,
5558 : : InvalidOid, NULL);
5559 : :
5560 : : /* initialize candidate result as default */
7512 tgl@sss.pgh.pa.us 5561 : 192 : *grantorId = roleId;
5562 : 192 : *grantOptions = ACL_NO_RIGHTS;
5563 : 192 : nrights = 0;
5564 : :
5565 [ + - + + : 360 : foreach(l, roles_list)
+ + ]
5566 : : {
7507 bruce@momjian.us 5567 : 308 : Oid otherrole = lfirst_oid(l);
5568 : : AclMode otherprivs;
5569 : :
7512 tgl@sss.pgh.pa.us 5570 : 308 : otherprivs = aclmask_direct(acl, otherrole, ownerId,
5571 : : needed_goptions, ACLMASK_ALL);
5572 [ + + ]: 308 : if (otherprivs == needed_goptions)
5573 : : {
5574 : : /* Found a suitable grantor */
5575 : 140 : *grantorId = otherrole;
5576 : 140 : *grantOptions = otherprivs;
5577 : 140 : return;
5578 : : }
5579 : :
5580 : : /*
5581 : : * If it has just some of the needed privileges, remember best
5582 : : * candidate.
5583 : : */
5584 [ + + ]: 168 : if (otherprivs != ACL_NO_RIGHTS)
5585 : : {
419 nathan@postgresql.or 5586 : 68 : int nnewrights = pg_popcount64(otherprivs);
5587 : :
7512 tgl@sss.pgh.pa.us 5588 [ + + ]: 68 : if (nnewrights > nrights)
5589 : : {
5590 : 36 : *grantorId = otherrole;
5591 : 36 : *grantOptions = otherprivs;
5592 : 36 : nrights = nnewrights;
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 : : */
5604 : : Oid
5752 rhaas@postgresql.org 5605 : 22932 : get_role_oid(const char *rolname, bool missing_ok)
5606 : : {
5607 : : Oid oid;
5608 : :
2723 andres@anarazel.de 5609 : 22932 : oid = GetSysCacheOid1(AUTHNAME, Anum_pg_authid_oid,
5610 : : CStringGetDatum(rolname));
5752 rhaas@postgresql.org 5611 [ + + + + ]: 22932 : if (!OidIsValid(oid) && !missing_ok)
5612 [ + - ]: 40 : ereport(ERROR,
5613 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
5614 : : errmsg("role \"%s\" does not exist", rolname)));
5615 : 22892 : 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 : : */
5622 : : Oid
5683 itagaki.takahiro@gma 5623 : 443 : get_role_oid_or_public(const char *rolname)
5624 : : {
5625 [ - + ]: 443 : if (strcmp(rolname, "public") == 0)
5683 itagaki.takahiro@gma 5626 :UBC 0 : return ACL_ID_PUBLIC;
5627 : :
5683 itagaki.takahiro@gma 5628 :CBC 443 : 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 : : */
5638 : : Oid
3415 peter_e@gmx.net 5639 : 5859 : get_rolespec_oid(const RoleSpec *role, bool missing_ok)
5640 : : {
5641 : : Oid oid;
5642 : :
4075 alvherre@alvh.no-ip. 5643 [ + + + + : 5859 : switch (role->roletype)
- ]
5644 : : {
5645 : 5598 : case ROLESPEC_CSTRING:
5646 [ - + ]: 5598 : Assert(role->rolename);
5647 : 5598 : oid = get_role_oid(role->rolename, missing_ok);
5648 : 5567 : break;
5649 : :
2056 peter@eisentraut.org 5650 : 242 : case ROLESPEC_CURRENT_ROLE:
5651 : : case ROLESPEC_CURRENT_USER:
4075 alvherre@alvh.no-ip. 5652 : 242 : oid = GetUserId();
5653 : 242 : break;
5654 : :
5655 : 11 : case ROLESPEC_SESSION_USER:
5656 : 11 : oid = GetSessionUserId();
5657 : 11 : break;
5658 : :
5659 : 8 : case ROLESPEC_PUBLIC:
5660 [ + - ]: 8 : ereport(ERROR,
5661 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
5662 : : errmsg("role \"%s\" does not exist", "public")));
5663 : : oid = InvalidOid; /* make compiler happy */
5664 : : break;
5665 : :
4075 alvherre@alvh.no-ip. 5666 :UBC 0 : default:
5667 [ # # ]: 0 : elog(ERROR, "unexpected role type %d", role->roletype);
5668 : : }
5669 : :
4075 alvherre@alvh.no-ip. 5670 :CBC 5820 : 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 : : */
5677 : : HeapTuple
3415 peter_e@gmx.net 5678 : 427 : get_rolespec_tuple(const RoleSpec *role)
5679 : : {
5680 : : HeapTuple tuple;
5681 : :
4075 alvherre@alvh.no-ip. 5682 [ + + + + : 427 : switch (role->roletype)
- ]
5683 : : {
5684 : 401 : case ROLESPEC_CSTRING:
5685 [ - + ]: 401 : Assert(role->rolename);
5686 : 401 : tuple = SearchSysCache1(AUTHNAME, CStringGetDatum(role->rolename));
5687 [ + + ]: 401 : if (!HeapTupleIsValid(tuple))
5688 [ + - ]: 6 : ereport(ERROR,
5689 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
5690 : : errmsg("role \"%s\" does not exist", role->rolename)));
5691 : 395 : break;
5692 : :
2056 peter@eisentraut.org 5693 : 14 : case ROLESPEC_CURRENT_ROLE:
5694 : : case ROLESPEC_CURRENT_USER:
1020 michael@paquier.xyz 5695 : 14 : tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(GetUserId()));
4075 alvherre@alvh.no-ip. 5696 [ - + ]: 14 : if (!HeapTupleIsValid(tuple))
4075 alvherre@alvh.no-ip. 5697 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for role %u", GetUserId());
4075 alvherre@alvh.no-ip. 5698 :CBC 14 : break;
5699 : :
5700 : 6 : case ROLESPEC_SESSION_USER:
1020 michael@paquier.xyz 5701 : 6 : tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(GetSessionUserId()));
4075 alvherre@alvh.no-ip. 5702 [ - + ]: 6 : if (!HeapTupleIsValid(tuple))
4075 alvherre@alvh.no-ip. 5703 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for role %u", GetSessionUserId());
4075 alvherre@alvh.no-ip. 5704 :CBC 6 : break;
5705 : :
5706 : 6 : case ROLESPEC_PUBLIC:
5707 [ + - ]: 6 : ereport(ERROR,
5708 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
5709 : : errmsg("role \"%s\" does not exist", "public")));
5710 : : tuple = NULL; /* make compiler happy */
5711 : : break;
5712 : :
4075 alvherre@alvh.no-ip. 5713 :UBC 0 : default:
5714 [ # # ]: 0 : elog(ERROR, "unexpected role type %d", role->roletype);
5715 : : }
5716 : :
4075 alvherre@alvh.no-ip. 5717 :CBC 415 : return tuple;
5718 : : }
5719 : :
5720 : : /*
5721 : : * Given a RoleSpec, returns a palloc'ed copy of the corresponding role's name.
5722 : : */
5723 : : char *
3415 peter_e@gmx.net 5724 : 28 : get_rolespec_name(const RoleSpec *role)
5725 : : {
5726 : : HeapTuple tp;
5727 : : Form_pg_authid authForm;
5728 : : char *rolename;
5729 : :
5730 : 28 : tp = get_rolespec_tuple(role);
4075 alvherre@alvh.no-ip. 5731 : 28 : authForm = (Form_pg_authid) GETSTRUCT(tp);
5732 : 28 : rolename = pstrdup(NameStr(authForm->rolname));
5733 : 28 : ReleaseSysCache(tp);
5734 : :
5735 : 28 : 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 : : */
5745 : : void
3415 peter_e@gmx.net 5746 : 334 : check_rolespec_name(const RoleSpec *role, const char *detail_msg)
5747 : : {
5748 [ - + ]: 334 : if (!role)
3679 sfrost@snowman.net 5749 :UBC 0 : return;
5750 : :
3679 sfrost@snowman.net 5751 [ + + ]:CBC 334 : if (role->roletype != ROLESPEC_CSTRING)
5752 : 26 : return;
5753 : :
5754 [ - + ]: 308 : if (IsReservedName(role->rolename))
5755 : : {
3679 sfrost@snowman.net 5756 [ # # ]:UBC 0 : if (detail_msg)
5757 [ # # ]: 0 : ereport(ERROR,
5758 : : (errcode(ERRCODE_RESERVED_NAME),
5759 : : errmsg("role name \"%s\" is reserved",
5760 : : role->rolename),
5761 : : errdetail_internal("%s", detail_msg)));
5762 : : else
5763 [ # # ]: 0 : ereport(ERROR,
5764 : : (errcode(ERRCODE_RESERVED_NAME),
5765 : : errmsg("role name \"%s\" is reserved",
5766 : : role->rolename)));
5767 : : }
5768 : : }
|