acl.c 42.9 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * acl.c
4
 *	  Basic access control list data structures manipulation routines.
5
 *
B
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
T
Tom Lane 已提交
11
 *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/acl.c,v 1.86 2003/01/24 21:53:29 tgl Exp $
12 13 14 15
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"
16

17 18
#include <ctype.h>

19
#include "catalog/namespace.h"
20
#include "catalog/pg_shadow.h"
21
#include "catalog/pg_type.h"
22
#include "commands/dbcommands.h"
23
#include "miscadmin.h"
B
Bruce Momjian 已提交
24
#include "utils/acl.h"
25
#include "utils/builtins.h"
26
#include "utils/lsyscache.h"
B
Bruce Momjian 已提交
27
#include "utils/syscache.h"
28

29 30 31 32

#define ACL_IDTYPE_GID_KEYWORD	"group"
#define ACL_IDTYPE_UID_KEYWORD	"user"

33
static const char *getid(const char *s, char *n);
34
static Acl *makeacl(int n);
35
static const char *aclparse(const char *s, AclItem *aip);
36
static bool aclitemeq(const AclItem *a1, const AclItem *a2);
37 38
static Acl *recursive_revoke(Acl *acl, AclId grantee,
							 AclMode revoke_privs, DropBehavior behavior);
39

B
Bruce Momjian 已提交
40
static Oid	convert_table_name(text *tablename);
41
static AclMode convert_table_priv_string(text *priv_type_text);
B
Bruce Momjian 已提交
42
static Oid	convert_database_name(text *databasename);
43
static AclMode convert_database_priv_string(text *priv_type_text);
B
Bruce Momjian 已提交
44
static Oid	convert_function_name(text *functionname);
45
static AclMode convert_function_priv_string(text *priv_type_text);
B
Bruce Momjian 已提交
46
static Oid	convert_language_name(text *languagename);
47
static AclMode convert_language_priv_string(text *priv_type_text);
B
Bruce Momjian 已提交
48
static Oid	convert_schema_name(text *schemaname);
49
static AclMode convert_schema_priv_string(text *priv_type_text);
50

51 52 53

/*
 * getid
54
 *		Consumes the first alphanumeric string (identifier) found in string
B
Bruce Momjian 已提交
55
 *		's', ignoring any leading white space.	If it finds a double quote
56
 *		it returns the word inside the quotes.
57 58
 *
 * RETURNS:
59
 *		the string position in 's' that points to the next non-space character
60
 *		in 's', after any quotes.  Also:
61
 *		- loads the identifier into 'name'.  (If no identifier is found, 'name'
62
 *		  contains an empty string.)  name must be NAMEDATALEN bytes.
63
 */
64 65
static const char *
getid(const char *s, char *n)
66
{
67
	unsigned	len;
68
	const char *id;
B
Bruce Momjian 已提交
69
	int			in_quotes = 0;
70 71 72

	Assert(s && n);

73
	while (isspace((unsigned char) *s))
74
		++s;
75 76 77 78 79 80 81

	if (*s == '"')
	{
		in_quotes = 1;
		s++;
	}

82 83 84
	for (id = s, len = 0;
		 isalnum((unsigned char) *s) || *s == '_' || in_quotes;
		 ++len, ++s)
85
	{
B
Bruce Momjian 已提交
86
		if (in_quotes && *s == '"')
87 88 89 90 91
		{
			len--;
			in_quotes = 0;
		}
	}
92 93 94
	if (len >= NAMEDATALEN)
		elog(ERROR, "getid: identifier must be <%d characters",
			 NAMEDATALEN);
95 96 97
	if (len > 0)
		memmove(n, id, len);
	n[len] = '\0';
98
	while (isspace((unsigned char) *s))
99
		++s;
100
	return s;
101 102 103 104
}

/*
 * aclparse
105
 *		Consumes and parses an ACL specification of the form:
106
 *				[group|user] [A-Za-z0-9]*=[rwaR]*
107 108 109
 *		from string 's', ignoring any leading white space or white space
 *		between the optional id type keyword (group|user) and the actual
 *		ACL specification.
110
 *
111 112
 *		This routine is called by the parser as well as aclitemin(), hence
 *		the added generality.
113 114
 *
 * RETURNS:
115 116 117 118
 *		the string position in 's' immediately following the ACL
 *		specification.	Also:
 *		- loads the structure pointed to by 'aip' with the appropriate
 *		  UID/GID, id type identifier and mode type values.
119
 */
120
static const char *
121
aclparse(const char *s, AclItem *aip)
122
{
123
	AclMode		privs, goption, read;
124
	uint32		idtype;
125
	char		name[NAMEDATALEN];
126
	char		name2[NAMEDATALEN];
127

128
	Assert(s && aip);
129

130
#ifdef ACLDEBUG
131
	elog(LOG, "aclparse: input = '%s'", s);
132
#endif
133
	idtype = ACL_IDTYPE_UID;
134
	s = getid(s, name);
135
	if (*s != '=')
136 137
	{
		/* we just read a keyword, not a name */
138
		if (strncmp(name, ACL_IDTYPE_GID_KEYWORD, sizeof(name)) == 0)
139
			idtype = ACL_IDTYPE_GID;
140
		else if (strncmp(name, ACL_IDTYPE_UID_KEYWORD, sizeof(name)) != 0)
141
			elog(ERROR, "aclparse: bad keyword, must be [group|user]");
142 143
		s = getid(s, name);		/* move s to the name beyond the keyword */
		if (name[0] == '\0')
144
			elog(ERROR, "aclparse: a name must follow the [group|user] keyword");
145 146
	}
	if (name[0] == '\0')
147
		idtype = ACL_IDTYPE_WORLD;
148

149 150
	if (*s != '=')
		elog(ERROR, "aclparse: expecting \"=\" sign");
151

152
	privs = goption = ACL_NO_RIGHTS;
153

154
	for (++s, read=0; isalpha((unsigned char) *s) || *s == '*'; s++)
155 156 157
	{
		switch (*s)
		{
158 159 160
			case '*':
				goption |= read;
				break;
161
			case ACL_INSERT_CHR:
162
				read = ACL_INSERT;
163 164
				break;
			case ACL_SELECT_CHR:
165
				read = ACL_SELECT;
166 167
				break;
			case ACL_UPDATE_CHR:
168
				read = ACL_UPDATE;
169 170
				break;
			case ACL_DELETE_CHR:
171
				read = ACL_DELETE;
172
				break;
173
			case ACL_RULE_CHR:
174
				read = ACL_RULE;
175
				break;
176
			case ACL_REFERENCES_CHR:
177
				read = ACL_REFERENCES;
178
				break;
179
			case ACL_TRIGGER_CHR:
180
				read = ACL_TRIGGER;
181
				break;
182
			case ACL_EXECUTE_CHR:
183
				read = ACL_EXECUTE;
184
				break;
185
			case ACL_USAGE_CHR:
186
				read = ACL_USAGE;
187
				break;
188
			case ACL_CREATE_CHR:
189
				read = ACL_CREATE;
190 191
				break;
			case ACL_CREATE_TEMP_CHR:
192
				read = ACL_CREATE_TEMP;
193 194
				break;
			default:
195
				elog(ERROR, "aclparse: mode flags must use \"%s\"",
196
					 ACL_ALL_RIGHTS_STR);
197
		}
198 199

		privs |= read;
200 201
	}

202
	switch (idtype)
203
	{
204
		case ACL_IDTYPE_UID:
205
			aip->ai_grantee = get_usesysid(name);
206 207
			break;
		case ACL_IDTYPE_GID:
208
			aip->ai_grantee = get_grosysid(name);
209 210
			break;
		case ACL_IDTYPE_WORLD:
211
			aip->ai_grantee = ACL_ID_WORLD;
212
			break;
213 214
	}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	/* XXX Allow a degree of backward compatibility by defaulting the
	 * grantor to the superuser. */
	if (*s == '/')
	{
		s = getid(s + 1, name2);
		if (name2[0] == '\0')
			elog(ERROR, "aclparse: a name must follow the \"/\" sign");

		aip->ai_grantor = get_usesysid(name2);
	}
	else
	{
		aip->ai_grantor = BOOTSTRAP_USESYSID;
		elog(WARNING, "defaulting grantor to %u", BOOTSTRAP_USESYSID);
	}

	ACLITEM_SET_PRIVS_IDTYPE(*aip, privs, goption, idtype);
232

233
#ifdef ACLDEBUG
234 235
	elog(LOG, "aclparse: correctly read [%x %d %x]",
		 idtype, aip->ai_grantee, privs);
236
#endif
237
	return s;
238 239 240 241
}

/*
 * makeacl
242
 *		Allocates storage for a new Acl with 'n' entries.
243 244
 *
 * RETURNS:
245
 *		the new Acl
246
 */
247
static Acl *
248 249
makeacl(int n)
{
250 251
	Acl		   *new_acl;
	Size		size;
252 253

	if (n < 0)
254
		elog(ERROR, "makeacl: invalid size: %d", n);
255
	size = ACL_N_SIZE(n);
256
	new_acl = (Acl *) palloc0(size);
257 258 259
	new_acl->size = size;
	new_acl->ndim = 1;
	new_acl->flags = 0;
260
	new_acl->elemtype = ACLITEMOID;
261 262
	ARR_LBOUND(new_acl)[0] = 0;
	ARR_DIMS(new_acl)[0] = n;
263
	return new_acl;
264 265 266 267
}

/*
 * aclitemin
268 269
 *		Allocates storage for, and fills in, a new AclItem given a string
 *		's' that contains an ACL specification.  See aclparse for details.
270 271
 *
 * RETURNS:
272
 *		the new AclItem
273
 */
274 275
Datum
aclitemin(PG_FUNCTION_ARGS)
276
{
277
	const char *s = PG_GETARG_CSTRING(0);
278
	AclItem    *aip;
279 280

	aip = (AclItem *) palloc(sizeof(AclItem));
281
	s = aclparse(s, aip);
282
	while (isspace((unsigned char) *s))
283 284
		++s;
	if (*s)
285
		elog(ERROR, "aclitemin: extra garbage at end of specification");
286
	PG_RETURN_ACLITEM_P(aip);
287 288 289 290
}

/*
 * aclitemout
291 292
 *		Allocates storage for, and fills in, a new null-delimited string
 *		containing a formatted ACL specification.  See aclparse for details.
293 294
 *
 * RETURNS:
295
 *		the new string
296
 */
297 298
Datum
aclitemout(PG_FUNCTION_ARGS)
299
{
B
Bruce Momjian 已提交
300
	AclItem    *aip = PG_GETARG_ACLITEM_P(0);
301
	char	   *p;
302
	char	   *out;
303
	HeapTuple	htup;
304 305
	unsigned	i;
	char	   *tmpname;
306

307
	p = out = palloc(strlen("group = ") + 2 * N_ACL_RIGHTS + 2* NAMEDATALEN + 2);
308 309
	*p = '\0';

310
	switch (ACLITEM_GET_IDTYPE(*aip))
311
	{
312
		case ACL_IDTYPE_UID:
313
			htup = SearchSysCache(SHADOWSYSID,
314
								  ObjectIdGetDatum(aip->ai_grantee),
315
								  0, 0, 0);
316 317 318
			if (HeapTupleIsValid(htup))
			{
				strncat(p,
B
Bruce Momjian 已提交
319
					NameStr(((Form_pg_shadow) GETSTRUCT(htup))->usename),
320 321 322 323
						NAMEDATALEN);
				ReleaseSysCache(htup);
			}
			else
324
			{
325 326
				/* Generate numeric UID if we don't find an entry */
				char	   *tmp;
327

328
				tmp = DatumGetCString(DirectFunctionCall1(int4out,
329
									 Int32GetDatum((int32) aip->ai_grantee)));
330 331 332 333 334 335
				strcat(p, tmp);
				pfree(tmp);
			}
			break;
		case ACL_IDTYPE_GID:
			strcat(p, "group ");
336
			tmpname = get_groname(aip->ai_grantee);
337 338 339 340 341 342 343 344
			if (tmpname != NULL)
				strncat(p, tmpname, NAMEDATALEN);
			else
			{
				/* Generate numeric GID if we don't find an entry */
				char	   *tmp;

				tmp = DatumGetCString(DirectFunctionCall1(int4out,
345
									 Int32GetDatum((int32) aip->ai_grantee)));
346 347 348
				strcat(p, tmp);
				pfree(tmp);
			}
349 350 351 352
			break;
		case ACL_IDTYPE_WORLD:
			break;
		default:
353 354
			elog(ERROR, "aclitemout: bad idtype: %d",
				 ACLITEM_GET_IDTYPE(*aip));
355
			break;
356 357 358
	}
	while (*p)
		++p;
359

360
	*p++ = '=';
361

362
	for (i = 0; i < N_ACL_RIGHTS; ++i)
363 364
	{
		if (ACLITEM_GET_PRIVS(*aip) & (1 << i))
365
			*p++ = ACL_ALL_RIGHTS_STR[i];
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
		if (ACLITEM_GET_GOPTIONS(*aip) & (1 << i))
			*p++ = '*';
	}

	*p++ = '/';
	*p = '\0';

	htup = SearchSysCache(SHADOWSYSID,
						  ObjectIdGetDatum(aip->ai_grantor),
						  0, 0, 0);
	if (HeapTupleIsValid(htup))
	{
		strncat(p,
				NameStr(((Form_pg_shadow) GETSTRUCT(htup))->usename),
				NAMEDATALEN);
		ReleaseSysCache(htup);
	}
	else
	{
		/* Generate numeric UID if we don't find an entry */
		char	   *tmp;

		tmp = DatumGetCString(DirectFunctionCall1(int4out,
												  Int32GetDatum((int32) aip->ai_grantor)));
		strcat(p, tmp);
		pfree(tmp);
	}

	while (*p)
		++p;
396 397
	*p = '\0';

398
	PG_RETURN_CSTRING(out);
399 400 401 402
}

/*
 * aclitemeq
403
 *		Two AclItems are considered equal iff they have the same
404
 *		grantee and grantor; the privileges are ignored.
405
 */
406
static bool
407
aclitemeq(const AclItem *a1, const AclItem *a2)
408
{
409
	return ACLITEM_GET_IDTYPE(*a1) == ACLITEM_GET_IDTYPE(*a2) &&
410 411
		a1->ai_grantee == a2->ai_grantee &&
		a1->ai_grantor == a2->ai_grantor;
412 413
}

414 415 416 417 418

/*
 * acldefault()  --- create an ACL describing default access permissions
 *
 * Change this routine if you want to alter the default access policy for
419
 * newly-created objects (or any object with a NULL acl entry).
420
 */
421
Acl *
422
acldefault(GrantObjectType objtype, AclId ownerid)
423
{
424 425
	AclMode		world_default;
	AclMode		owner_default;
426 427
	Acl		   *acl;
	AclItem    *aip;
428

429 430 431 432 433 434 435
	switch (objtype)
	{
		case ACL_OBJECT_RELATION:
			world_default = ACL_NO_RIGHTS;
			owner_default = ACL_ALL_RIGHTS_RELATION;
			break;
		case ACL_OBJECT_DATABASE:
B
Bruce Momjian 已提交
436
			world_default = ACL_CREATE_TEMP;	/* not NO_RIGHTS! */
437 438 439
			owner_default = ACL_ALL_RIGHTS_DATABASE;
			break;
		case ACL_OBJECT_FUNCTION:
440 441
			/* Grant EXECUTE by default, for now */
			world_default = ACL_EXECUTE;
442 443 444
			owner_default = ACL_ALL_RIGHTS_FUNCTION;
			break;
		case ACL_OBJECT_LANGUAGE:
445 446
			/* Grant USAGE by default, for now */
			world_default = ACL_USAGE;
447 448 449 450 451 452 453 454
			owner_default = ACL_ALL_RIGHTS_LANGUAGE;
			break;
		case ACL_OBJECT_NAMESPACE:
			world_default = ACL_NO_RIGHTS;
			owner_default = ACL_ALL_RIGHTS_NAMESPACE;
			break;
		default:
			elog(ERROR, "acldefault: bogus objtype %d", (int) objtype);
B
Bruce Momjian 已提交
455
			world_default = ACL_NO_RIGHTS;		/* keep compiler quiet */
456 457 458
			owner_default = ACL_NO_RIGHTS;
			break;
	}
459

460 461
	acl = makeacl((world_default != ACL_NO_RIGHTS ? 1 : 0)
				  + (ownerid ? 1 : 0));
462
	aip = ACL_DAT(acl);
463

464 465 466 467 468 469 470
	if (world_default != ACL_NO_RIGHTS)
	{
		aip[0].ai_grantee = ACL_ID_WORLD;
		aip[0].ai_grantor = ownerid;
		ACLITEM_SET_PRIVS_IDTYPE(aip[0], world_default, ACL_NO_RIGHTS, ACL_IDTYPE_WORLD);
	}

471 472
	if (ownerid)
	{
473 474 475 476 477 478
		int index = (world_default != ACL_NO_RIGHTS ? 1: 0);

		aip[index].ai_grantee = ownerid;
		aip[index].ai_grantor = ownerid;
		/* owner gets default privileges with grant option */
		ACLITEM_SET_PRIVS_IDTYPE(aip[index], owner_default, owner_default, ACL_IDTYPE_UID);
479
	}
480

481
	return acl;
482 483 484
}


485
/*
486
 * Add or replace an item in an ACL array.	The result is a modified copy;
487
 * the input object is not changed.
488 489 490
 *
 * NB: caller is responsible for having detoasted the input ACL, if needed.
 */
491
Acl *
492
aclinsert3(const Acl *old_acl, const AclItem *mod_aip, unsigned modechg, DropBehavior behavior)
493
{
T
Tom Lane 已提交
494
	Acl		   *new_acl = NULL;
495
	AclItem    *old_aip,
T
Tom Lane 已提交
496
			   *new_aip = NULL;
497
	int			dst,
498
				num;
499

500
	/* These checks for null input are probably dead code, but... */
501
	if (!old_acl || ACL_NUM(old_acl) < 1)
502
		old_acl = makeacl(1);
503 504 505
	if (!mod_aip)
	{
		new_acl = makeacl(ACL_NUM(old_acl));
506
		memcpy((char *) new_acl, (char *) old_acl, ACL_SIZE(old_acl));
507
		return new_acl;
508 509
	}

510 511 512 513
	num = ACL_NUM(old_acl);
	old_aip = ACL_DAT(old_acl);

	/*
514 515 516 517
	 * Search the ACL for an existing entry for this grantee and
	 * grantor.  If one exists, just modify the entry in-place (well,
	 * in the same position, since we actually return a copy);
	 * otherwise, insert the new entry at the end.
518
	 */
519

520
	for (dst = 0; dst < num; ++dst)
521
	{
522 523 524 525 526 527 528 529
		if (aclitemeq(mod_aip, old_aip + dst))
		{
			/* found a match, so modify existing item */
			new_acl = makeacl(num);
			new_aip = ACL_DAT(new_acl);
			memcpy(new_acl, old_acl, ACL_SIZE(old_acl));
			break;
		}
530
	}
531 532

	if (dst == num)
533
	{
534
		/* need to append a new item */
535 536
		new_acl = makeacl(num + 1);
		new_aip = ACL_DAT(new_acl);
537 538
		memcpy(new_aip, old_aip, num * sizeof(AclItem));

539
		/* initialize the new entry with no permissions */
540 541 542
		new_aip[dst].ai_grantee = mod_aip->ai_grantee;
		new_aip[dst].ai_grantor = mod_aip->ai_grantor;
		ACLITEM_SET_PRIVS_IDTYPE(new_aip[dst], ACL_NO_RIGHTS, ACL_NO_RIGHTS,
543
								 ACLITEM_GET_IDTYPE(*mod_aip));
544 545
		num++;					/* set num to the size of new_acl */
	}
546 547

	/* apply the permissions mod */
548 549
	switch (modechg)
	{
550
		case ACL_MODECHG_ADD:
551 552
			ACLITEM_SET_PRIVS(new_aip[dst], ACLITEM_GET_PRIVS(new_aip[dst]) | ACLITEM_GET_PRIVS(*mod_aip));
			ACLITEM_SET_GOPTIONS(new_aip[dst], ACLITEM_GET_GOPTIONS(new_aip[dst]) | ACLITEM_GET_GOPTIONS(*mod_aip));
553 554
			break;
		case ACL_MODECHG_DEL:
555 556
			ACLITEM_SET_PRIVS(new_aip[dst], ACLITEM_GET_PRIVS(new_aip[dst]) & ~ACLITEM_GET_PRIVS(*mod_aip));
			ACLITEM_SET_GOPTIONS(new_aip[dst], ACLITEM_GET_GOPTIONS(new_aip[dst]) & ~ACLITEM_GET_GOPTIONS(*mod_aip));
557 558
			break;
		case ACL_MODECHG_EQL:
559 560
			ACLITEM_SET_PRIVS_IDTYPE(new_aip[dst],
									 ACLITEM_GET_PRIVS(*mod_aip),
561
									 ACLITEM_GET_GOPTIONS(*mod_aip),
562
									 ACLITEM_GET_IDTYPE(new_aip[dst]));
563
			break;
564 565 566
	}

	/*
567
	 * If the adjusted entry has no permissions, delete it from the list.
568
	 */
569
	if (ACLITEM_GET_PRIVS(new_aip[dst]) == ACL_NO_RIGHTS)
570
	{
571 572
		memmove(new_aip + dst,
				new_aip + dst + 1,
573
				(num - dst - 1) * sizeof(AclItem));
574 575
		ARR_DIMS(new_acl)[0] = num - 1;
		ARR_SIZE(new_acl) -= sizeof(AclItem);
576 577
	}

578 579 580 581 582 583 584 585
	/*
	 * Remove abandoned privileges (cascading revoke)
	 */
	if (modechg != ACL_MODECHG_ADD
		&& ACLITEM_GET_IDTYPE(*mod_aip) == ACL_IDTYPE_UID
		&& ACLITEM_GET_GOPTIONS(*mod_aip))
		new_acl = recursive_revoke(new_acl, mod_aip->ai_grantee, ACLITEM_GET_GOPTIONS(*mod_aip), behavior);

586
	return new_acl;
587 588
}

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

/*
 * Ensure that no privilege is "abandoned".  A privilege is abandoned
 * if the user that granted the privilege loses the grant option.  (So
 * the chain through which it was granted is broken.)  Either the
 * abandoned privileges are revoked as well, or an error message is
 * printed, depending on the drop behavior option.
 */
static Acl *
recursive_revoke(Acl *acl,
				 AclId grantee,
				 AclMode revoke_privs,
				 DropBehavior behavior)
{
	int i;

restart:
	for (i = 0; i < ACL_NUM(acl); i++)
	{
		AclItem *aip = ACL_DAT(acl);

		if (aip[i].ai_grantor == grantee
			&& (ACLITEM_GET_PRIVS(aip[i]) & revoke_privs) != 0)
		{
			AclItem mod_acl;

			if (behavior == DROP_RESTRICT)
				elog(ERROR, "dependent privileges exist (use CASCADE to revoke them too)");

			mod_acl.ai_grantor = grantee;
			mod_acl.ai_grantee = aip[i].ai_grantee;
			ACLITEM_SET_PRIVS_IDTYPE(mod_acl,
									 revoke_privs,
									 revoke_privs,
									 ACLITEM_GET_IDTYPE(aip[i]));

			acl = aclinsert3(acl, &mod_acl, ACL_MODECHG_DEL, behavior);
			goto restart;
		}
	}

	return acl;
}


634
/*
635
 * aclinsert (exported function)
636
 */
637 638
Datum
aclinsert(PG_FUNCTION_ARGS)
639
{
640
	Acl		   *old_acl = PG_GETARG_ACL_P(0);
B
Bruce Momjian 已提交
641
	AclItem    *mod_aip = PG_GETARG_ACLITEM_P(1);
642

643
	PG_RETURN_ACL_P(aclinsert3(old_acl, mod_aip, ACL_MODECHG_EQL, DROP_CASCADE));
644 645
}

646 647
Datum
aclremove(PG_FUNCTION_ARGS)
648
{
649
	Acl		   *old_acl = PG_GETARG_ACL_P(0);
B
Bruce Momjian 已提交
650
	AclItem    *mod_aip = PG_GETARG_ACLITEM_P(1);
651 652 653
	Acl		   *new_acl;
	AclItem    *old_aip,
			   *new_aip;
654
	int			dst,
655 656
				old_num,
				new_num;
657

658
	/* These checks for null input should be dead code, but... */
659
	if (!old_acl || ACL_NUM(old_acl) < 1)
660
		old_acl = makeacl(1);
661 662 663
	if (!mod_aip)
	{
		new_acl = makeacl(ACL_NUM(old_acl));
664 665
		memcpy((char *) new_acl, (char *) old_acl, ACL_SIZE(old_acl));
		PG_RETURN_ACL_P(new_acl);
666 667 668 669 670
	}

	old_num = ACL_NUM(old_acl);
	old_aip = ACL_DAT(old_acl);

671
	/* Search for the matching entry */
672 673
	for (dst = 0; dst < old_num && !aclitemeq(mod_aip, old_aip + dst); ++dst)
		;
674

675
	if (dst >= old_num)
676 677 678
	{
		/* Not found, so return copy of source ACL */
		new_acl = makeacl(old_num);
679
		memcpy((char *) new_acl, (char *) old_acl, ACL_SIZE(old_acl));
680 681 682 683
	}
	else
	{
		new_num = old_num - 1;
684
		new_acl = makeacl(new_num);
685 686 687
		new_aip = ACL_DAT(new_acl);
		if (dst == 0)
		{						/* start */
688
			elog(ERROR, "aclremove: removal of the world ACL??");
689 690 691
		}
		else if (dst == old_num - 1)
		{						/* end */
692 693 694
			memcpy((char *) new_aip,
				   (char *) old_aip,
				   new_num * sizeof(AclItem));
695 696 697
		}
		else
		{						/* middle */
698 699 700 701 702 703
			memcpy((char *) new_aip,
				   (char *) old_aip,
				   dst * sizeof(AclItem));
			memcpy((char *) (new_aip + dst),
				   (char *) (old_aip + dst + 1),
				   (new_num - dst) * sizeof(AclItem));
704 705
		}
	}
706

707
	PG_RETURN_ACL_P(new_acl);
708 709
}

710 711
Datum
aclcontains(PG_FUNCTION_ARGS)
712
{
713
	Acl		   *acl = PG_GETARG_ACL_P(0);
B
Bruce Momjian 已提交
714
	AclItem    *aip = PG_GETARG_ACLITEM_P(1);
715
	AclItem    *aidat;
716 717
	int			i,
				num;
718

719
	num = ACL_NUM(acl);
720 721
	aidat = ACL_DAT(acl);
	for (i = 0; i < num; ++i)
722
	{
723
		if (aip->ai_grantee == aidat[i].ai_grantee &&
724
			aip->ai_privs == aidat[i].ai_privs)
725
			PG_RETURN_BOOL(true);
726
	}
727
	PG_RETURN_BOOL(false);
728 729
}

730

731
/*
732 733 734 735
 * has_table_privilege variants
 *		These are all named "has_table_privilege" at the SQL level.
 *		They take various combinations of relation name, relation OID,
 *		user name, user sysid, or implicit user = current_user.
736
 *
737 738 739 740 741 742 743 744
 *		The result is a boolean value: true if user has the indicated
 *		privilege, false if not.
 */

/*
 * has_table_privilege_name_name
 *		Check user privileges on a table given
 *		name username, text tablename, and text priv name.
745 746 747 748 749
 */
Datum
has_table_privilege_name_name(PG_FUNCTION_ARGS)
{
	Name		username = PG_GETARG_NAME(0);
750
	text	   *tablename = PG_GETARG_TEXT_P(1);
751
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
752
	int32		usesysid;
753
	Oid			tableoid;
754
	AclMode		mode;
755
	AclResult	aclresult;
756 757

	usesysid = get_usesysid(NameStr(*username));
758 759
	tableoid = convert_table_name(tablename);
	mode = convert_table_priv_string(priv_type_text);
760

761
	aclresult = pg_class_aclcheck(tableoid, usesysid, mode);
762 763

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
764 765 766 767
}

/*
 * has_table_privilege_name
768 769
 *		Check user privileges on a table given
 *		text tablename and text priv name.
770 771 772 773 774
 *		current_user is assumed
 */
Datum
has_table_privilege_name(PG_FUNCTION_ARGS)
{
775
	text	   *tablename = PG_GETARG_TEXT_P(0);
776
	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
777
	AclId		usesysid;
778
	Oid			tableoid;
779
	AclMode		mode;
780
	AclResult	aclresult;
781 782

	usesysid = GetUserId();
783 784
	tableoid = convert_table_name(tablename);
	mode = convert_table_priv_string(priv_type_text);
785

786
	aclresult = pg_class_aclcheck(tableoid, usesysid, mode);
787

788
	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
789 790 791 792
}

/*
 * has_table_privilege_name_id
793 794
 *		Check user privileges on a table given
 *		name usename, table oid, and text priv name.
795 796 797 798 799
 */
Datum
has_table_privilege_name_id(PG_FUNCTION_ARGS)
{
	Name		username = PG_GETARG_NAME(0);
800
	Oid			tableoid = PG_GETARG_OID(1);
801
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
802 803
	int32		usesysid;
	AclMode		mode;
804
	AclResult	aclresult;
805

806
	usesysid = get_usesysid(NameStr(*username));
807
	mode = convert_table_priv_string(priv_type_text);
808

809
	aclresult = pg_class_aclcheck(tableoid, usesysid, mode);
810 811

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
812 813 814 815
}

/*
 * has_table_privilege_id
816 817
 *		Check user privileges on a table given
 *		table oid, and text priv name.
818 819 820 821 822
 *		current_user is assumed
 */
Datum
has_table_privilege_id(PG_FUNCTION_ARGS)
{
823
	Oid			tableoid = PG_GETARG_OID(0);
824
	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
825
	AclId		usesysid;
826
	AclMode		mode;
827
	AclResult	aclresult;
828 829

	usesysid = GetUserId();
830
	mode = convert_table_priv_string(priv_type_text);
831

832
	aclresult = pg_class_aclcheck(tableoid, usesysid, mode);
833

834
	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
835 836 837 838
}

/*
 * has_table_privilege_id_name
839 840
 *		Check user privileges on a table given
 *		usesysid, text tablename, and text priv name.
841 842 843 844 845
 */
Datum
has_table_privilege_id_name(PG_FUNCTION_ARGS)
{
	int32		usesysid = PG_GETARG_INT32(0);
846
	text	   *tablename = PG_GETARG_TEXT_P(1);
847
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
848
	Oid			tableoid;
849
	AclMode		mode;
850
	AclResult	aclresult;
851

852 853
	tableoid = convert_table_name(tablename);
	mode = convert_table_priv_string(priv_type_text);
854

855
	aclresult = pg_class_aclcheck(tableoid, usesysid, mode);
856

857
	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
858 859 860 861
}

/*
 * has_table_privilege_id_id
862 863
 *		Check user privileges on a table given
 *		usesysid, table oid, and text priv name.
864 865 866 867 868
 */
Datum
has_table_privilege_id_id(PG_FUNCTION_ARGS)
{
	int32		usesysid = PG_GETARG_INT32(0);
869
	Oid			tableoid = PG_GETARG_OID(1);
870
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
871
	AclMode		mode;
872
	AclResult	aclresult;
873

874
	mode = convert_table_priv_string(priv_type_text);
875

876
	aclresult = pg_class_aclcheck(tableoid, usesysid, mode);
877

878
	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
879 880 881
}

/*
882
 *		Support routines for has_table_privilege family.
883 884
 */

885
/*
886
 * Given a table name expressed as a string, look it up and return Oid
887 888
 */
static Oid
889
convert_table_name(text *tablename)
890 891 892
{
	RangeVar   *relrv;

893
	relrv = makeRangeVarFromNameList(textToQualifiedNameList(tablename,
B
Bruce Momjian 已提交
894
												 "has_table_privilege"));
895 896 897 898

	return RangeVarGetRelid(relrv, false);
}

899
/*
900 901
 * convert_table_priv_string
 *		Convert text string to AclMode value.
902 903
 */
static AclMode
904
convert_table_priv_string(text *priv_type_text)
905
{
906 907 908
	char	   *priv_type;

	priv_type = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
909
									   PointerGetDatum(priv_type_text)));
910 911 912 913 914 915

	/*
	 * Return mode from priv_type string
	 */
	if (strcasecmp(priv_type, "SELECT") == 0)
		return ACL_SELECT;
916 917
	if (strcasecmp(priv_type, "SELECT WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_SELECT);
918 919 920

	if (strcasecmp(priv_type, "INSERT") == 0)
		return ACL_INSERT;
921 922
	if (strcasecmp(priv_type, "INSERT WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_INSERT);
923 924 925

	if (strcasecmp(priv_type, "UPDATE") == 0)
		return ACL_UPDATE;
926 927
	if (strcasecmp(priv_type, "UPDATE WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_UPDATE);
928 929 930

	if (strcasecmp(priv_type, "DELETE") == 0)
		return ACL_DELETE;
931 932
	if (strcasecmp(priv_type, "DELETE WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_DELETE);
933 934 935

	if (strcasecmp(priv_type, "RULE") == 0)
		return ACL_RULE;
936 937
	if (strcasecmp(priv_type, "RULE WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_RULE);
938 939 940

	if (strcasecmp(priv_type, "REFERENCES") == 0)
		return ACL_REFERENCES;
941 942
	if (strcasecmp(priv_type, "REFERENCES WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_REFERENCES);
943 944 945

	if (strcasecmp(priv_type, "TRIGGER") == 0)
		return ACL_TRIGGER;
946 947
	if (strcasecmp(priv_type, "TRIGGER WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_TRIGGER);
948

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
	elog(ERROR, "has_table_privilege: invalid privilege type %s",
		 priv_type);
	return ACL_NO_RIGHTS;		/* keep compiler quiet */
}


/*
 * has_database_privilege variants
 *		These are all named "has_database_privilege" at the SQL level.
 *		They take various combinations of database name, database OID,
 *		user name, user sysid, or implicit user = current_user.
 *
 *		The result is a boolean value: true if user has the indicated
 *		privilege, false if not.
 */

/*
 * has_database_privilege_name_name
 *		Check user privileges on a database given
 *		name username, text databasename, and text priv name.
 */
Datum
has_database_privilege_name_name(PG_FUNCTION_ARGS)
{
	Name		username = PG_GETARG_NAME(0);
	text	   *databasename = PG_GETARG_TEXT_P(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	int32		usesysid;
	Oid			databaseoid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = get_usesysid(NameStr(*username));
	databaseoid = convert_database_name(databasename);
	mode = convert_database_priv_string(priv_type_text);

	aclresult = pg_database_aclcheck(databaseoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_database_privilege_name
 *		Check user privileges on a database given
 *		text databasename and text priv name.
 *		current_user is assumed
 */
Datum
has_database_privilege_name(PG_FUNCTION_ARGS)
{
	text	   *databasename = PG_GETARG_TEXT_P(0);
	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
1001
	AclId		usesysid;
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	Oid			databaseoid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = GetUserId();
	databaseoid = convert_database_name(databasename);
	mode = convert_database_priv_string(priv_type_text);

	aclresult = pg_database_aclcheck(databaseoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_database_privilege_name_id
 *		Check user privileges on a database given
 *		name usename, database oid, and text priv name.
 */
Datum
has_database_privilege_name_id(PG_FUNCTION_ARGS)
{
	Name		username = PG_GETARG_NAME(0);
	Oid			databaseoid = PG_GETARG_OID(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	int32		usesysid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = get_usesysid(NameStr(*username));
	mode = convert_database_priv_string(priv_type_text);

	aclresult = pg_database_aclcheck(databaseoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_database_privilege_id
 *		Check user privileges on a database given
 *		database oid, and text priv name.
 *		current_user is assumed
 */
Datum
has_database_privilege_id(PG_FUNCTION_ARGS)
{
	Oid			databaseoid = PG_GETARG_OID(0);
	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
1049
	AclId		usesysid;
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	AclMode		mode;
	AclResult	aclresult;

	usesysid = GetUserId();
	mode = convert_database_priv_string(priv_type_text);

	aclresult = pg_database_aclcheck(databaseoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_database_privilege_id_name
 *		Check user privileges on a database given
 *		usesysid, text databasename, and text priv name.
 */
Datum
has_database_privilege_id_name(PG_FUNCTION_ARGS)
{
	int32		usesysid = PG_GETARG_INT32(0);
	text	   *databasename = PG_GETARG_TEXT_P(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	Oid			databaseoid;
	AclMode		mode;
	AclResult	aclresult;

	databaseoid = convert_database_name(databasename);
	mode = convert_database_priv_string(priv_type_text);

	aclresult = pg_database_aclcheck(databaseoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_database_privilege_id_id
 *		Check user privileges on a database given
 *		usesysid, database oid, and text priv name.
 */
Datum
has_database_privilege_id_id(PG_FUNCTION_ARGS)
{
	int32		usesysid = PG_GETARG_INT32(0);
	Oid			databaseoid = PG_GETARG_OID(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	AclMode		mode;
	AclResult	aclresult;

	mode = convert_database_priv_string(priv_type_text);

	aclresult = pg_database_aclcheck(databaseoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 *		Support routines for has_database_privilege family.
 */

/*
 * Given a database name expressed as a string, look it up and return Oid
 */
static Oid
convert_database_name(text *databasename)
{
	char	   *dbname;
	Oid			oid;

	dbname = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
1119
										 PointerGetDatum(databasename)));
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137

	oid = get_database_oid(dbname);
	if (!OidIsValid(oid))
		elog(ERROR, "database \"%s\" does not exist", dbname);

	return oid;
}

/*
 * convert_database_priv_string
 *		Convert text string to AclMode value.
 */
static AclMode
convert_database_priv_string(text *priv_type_text)
{
	char	   *priv_type;

	priv_type = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
1138
									   PointerGetDatum(priv_type_text)));
1139

1140
	/*
1141
	 * Return mode from priv_type string
1142
	 */
1143 1144
	if (strcasecmp(priv_type, "CREATE") == 0)
		return ACL_CREATE;
1145 1146
	if (strcasecmp(priv_type, "CREATE WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_CREATE);
1147 1148 1149

	if (strcasecmp(priv_type, "TEMPORARY") == 0)
		return ACL_CREATE_TEMP;
1150 1151
	if (strcasecmp(priv_type, "TEMPORARY WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_CREATE_TEMP);
1152 1153 1154

	if (strcasecmp(priv_type, "TEMP") == 0)
		return ACL_CREATE_TEMP;
1155 1156
	if (strcasecmp(priv_type, "TEMP WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_CREATE_TEMP);
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

	elog(ERROR, "has_database_privilege: invalid privilege type %s",
		 priv_type);
	return ACL_NO_RIGHTS;		/* keep compiler quiet */
}


/*
 * has_function_privilege variants
 *		These are all named "has_function_privilege" at the SQL level.
 *		They take various combinations of function name, function OID,
 *		user name, user sysid, or implicit user = current_user.
 *
 *		The result is a boolean value: true if user has the indicated
 *		privilege, false if not.
 */

/*
 * has_function_privilege_name_name
 *		Check user privileges on a function given
 *		name username, text functionname, and text priv name.
 */
Datum
has_function_privilege_name_name(PG_FUNCTION_ARGS)
{
	Name		username = PG_GETARG_NAME(0);
	text	   *functionname = PG_GETARG_TEXT_P(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	int32		usesysid;
	Oid			functionoid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = get_usesysid(NameStr(*username));
	functionoid = convert_function_name(functionname);
	mode = convert_function_priv_string(priv_type_text);

	aclresult = pg_proc_aclcheck(functionoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_function_privilege_name
 *		Check user privileges on a function given
 *		text functionname and text priv name.
 *		current_user is assumed
 */
Datum
has_function_privilege_name(PG_FUNCTION_ARGS)
{
	text	   *functionname = PG_GETARG_TEXT_P(0);
	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
1210
	AclId		usesysid;
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
	Oid			functionoid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = GetUserId();
	functionoid = convert_function_name(functionname);
	mode = convert_function_priv_string(priv_type_text);

	aclresult = pg_proc_aclcheck(functionoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_function_privilege_name_id
 *		Check user privileges on a function given
 *		name usename, function oid, and text priv name.
 */
Datum
has_function_privilege_name_id(PG_FUNCTION_ARGS)
{
	Name		username = PG_GETARG_NAME(0);
	Oid			functionoid = PG_GETARG_OID(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	int32		usesysid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = get_usesysid(NameStr(*username));
	mode = convert_function_priv_string(priv_type_text);

	aclresult = pg_proc_aclcheck(functionoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_function_privilege_id
 *		Check user privileges on a function given
 *		function oid, and text priv name.
 *		current_user is assumed
 */
Datum
has_function_privilege_id(PG_FUNCTION_ARGS)
{
	Oid			functionoid = PG_GETARG_OID(0);
	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
1258
	AclId		usesysid;
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
	AclMode		mode;
	AclResult	aclresult;

	usesysid = GetUserId();
	mode = convert_function_priv_string(priv_type_text);

	aclresult = pg_proc_aclcheck(functionoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_function_privilege_id_name
 *		Check user privileges on a function given
 *		usesysid, text functionname, and text priv name.
 */
Datum
has_function_privilege_id_name(PG_FUNCTION_ARGS)
{
	int32		usesysid = PG_GETARG_INT32(0);
	text	   *functionname = PG_GETARG_TEXT_P(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	Oid			functionoid;
	AclMode		mode;
	AclResult	aclresult;

	functionoid = convert_function_name(functionname);
	mode = convert_function_priv_string(priv_type_text);

	aclresult = pg_proc_aclcheck(functionoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_function_privilege_id_id
 *		Check user privileges on a function given
 *		usesysid, function oid, and text priv name.
 */
Datum
has_function_privilege_id_id(PG_FUNCTION_ARGS)
{
	int32		usesysid = PG_GETARG_INT32(0);
	Oid			functionoid = PG_GETARG_OID(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	AclMode		mode;
	AclResult	aclresult;

	mode = convert_function_priv_string(priv_type_text);

	aclresult = pg_proc_aclcheck(functionoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 *		Support routines for has_function_privilege family.
 */

/*
 * Given a function name expressed as a string, look it up and return Oid
 */
static Oid
convert_function_name(text *functionname)
{
	char	   *funcname;
	Oid			oid;

	funcname = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
1328
										 PointerGetDatum(functionname)));
1329 1330

	oid = DatumGetObjectId(DirectFunctionCall1(regprocedurein,
B
Bruce Momjian 已提交
1331
											 CStringGetDatum(funcname)));
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348

	if (!OidIsValid(oid))
		elog(ERROR, "function \"%s\" does not exist", funcname);

	return oid;
}

/*
 * convert_function_priv_string
 *		Convert text string to AclMode value.
 */
static AclMode
convert_function_priv_string(text *priv_type_text)
{
	char	   *priv_type;

	priv_type = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
1349
									   PointerGetDatum(priv_type_text)));
1350 1351 1352 1353 1354 1355

	/*
	 * Return mode from priv_type string
	 */
	if (strcasecmp(priv_type, "EXECUTE") == 0)
		return ACL_EXECUTE;
1356 1357
	if (strcasecmp(priv_type, "EXECUTE WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_EXECUTE);
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410

	elog(ERROR, "has_function_privilege: invalid privilege type %s",
		 priv_type);
	return ACL_NO_RIGHTS;		/* keep compiler quiet */
}


/*
 * has_language_privilege variants
 *		These are all named "has_language_privilege" at the SQL level.
 *		They take various combinations of language name, language OID,
 *		user name, user sysid, or implicit user = current_user.
 *
 *		The result is a boolean value: true if user has the indicated
 *		privilege, false if not.
 */

/*
 * has_language_privilege_name_name
 *		Check user privileges on a language given
 *		name username, text languagename, and text priv name.
 */
Datum
has_language_privilege_name_name(PG_FUNCTION_ARGS)
{
	Name		username = PG_GETARG_NAME(0);
	text	   *languagename = PG_GETARG_TEXT_P(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	int32		usesysid;
	Oid			languageoid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = get_usesysid(NameStr(*username));
	languageoid = convert_language_name(languagename);
	mode = convert_language_priv_string(priv_type_text);

	aclresult = pg_language_aclcheck(languageoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_language_privilege_name
 *		Check user privileges on a language given
 *		text languagename and text priv name.
 *		current_user is assumed
 */
Datum
has_language_privilege_name(PG_FUNCTION_ARGS)
{
	text	   *languagename = PG_GETARG_TEXT_P(0);
	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
1411
	AclId		usesysid;
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
	Oid			languageoid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = GetUserId();
	languageoid = convert_language_name(languagename);
	mode = convert_language_priv_string(priv_type_text);

	aclresult = pg_language_aclcheck(languageoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_language_privilege_name_id
 *		Check user privileges on a language given
 *		name usename, language oid, and text priv name.
 */
Datum
has_language_privilege_name_id(PG_FUNCTION_ARGS)
{
	Name		username = PG_GETARG_NAME(0);
	Oid			languageoid = PG_GETARG_OID(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	int32		usesysid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = get_usesysid(NameStr(*username));
	mode = convert_language_priv_string(priv_type_text);

	aclresult = pg_language_aclcheck(languageoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_language_privilege_id
 *		Check user privileges on a language given
 *		language oid, and text priv name.
 *		current_user is assumed
 */
Datum
has_language_privilege_id(PG_FUNCTION_ARGS)
{
	Oid			languageoid = PG_GETARG_OID(0);
	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
1459
	AclId		usesysid;
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
	AclMode		mode;
	AclResult	aclresult;

	usesysid = GetUserId();
	mode = convert_language_priv_string(priv_type_text);

	aclresult = pg_language_aclcheck(languageoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_language_privilege_id_name
 *		Check user privileges on a language given
 *		usesysid, text languagename, and text priv name.
 */
Datum
has_language_privilege_id_name(PG_FUNCTION_ARGS)
{
	int32		usesysid = PG_GETARG_INT32(0);
	text	   *languagename = PG_GETARG_TEXT_P(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	Oid			languageoid;
	AclMode		mode;
	AclResult	aclresult;

	languageoid = convert_language_name(languagename);
	mode = convert_language_priv_string(priv_type_text);

	aclresult = pg_language_aclcheck(languageoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_language_privilege_id_id
 *		Check user privileges on a language given
 *		usesysid, language oid, and text priv name.
 */
Datum
has_language_privilege_id_id(PG_FUNCTION_ARGS)
{
	int32		usesysid = PG_GETARG_INT32(0);
	Oid			languageoid = PG_GETARG_OID(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	AclMode		mode;
	AclResult	aclresult;

	mode = convert_language_priv_string(priv_type_text);

	aclresult = pg_language_aclcheck(languageoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 *		Support routines for has_language_privilege family.
 */

/*
 * Given a language name expressed as a string, look it up and return Oid
 */
static Oid
convert_language_name(text *languagename)
{
	char	   *langname;
	Oid			oid;

	langname = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
1529
										 PointerGetDatum(languagename)));
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549

	oid = GetSysCacheOid(LANGNAME,
						 CStringGetDatum(langname),
						 0, 0, 0);
	if (!OidIsValid(oid))
		elog(ERROR, "language \"%s\" does not exist", langname);

	return oid;
}

/*
 * convert_language_priv_string
 *		Convert text string to AclMode value.
 */
static AclMode
convert_language_priv_string(text *priv_type_text)
{
	char	   *priv_type;

	priv_type = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
1550
									   PointerGetDatum(priv_type_text)));
1551 1552 1553 1554 1555 1556

	/*
	 * Return mode from priv_type string
	 */
	if (strcasecmp(priv_type, "USAGE") == 0)
		return ACL_USAGE;
1557 1558
	if (strcasecmp(priv_type, "USAGE WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_USAGE);
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611

	elog(ERROR, "has_language_privilege: invalid privilege type %s",
		 priv_type);
	return ACL_NO_RIGHTS;		/* keep compiler quiet */
}


/*
 * has_schema_privilege variants
 *		These are all named "has_schema_privilege" at the SQL level.
 *		They take various combinations of schema name, schema OID,
 *		user name, user sysid, or implicit user = current_user.
 *
 *		The result is a boolean value: true if user has the indicated
 *		privilege, false if not.
 */

/*
 * has_schema_privilege_name_name
 *		Check user privileges on a schema given
 *		name username, text schemaname, and text priv name.
 */
Datum
has_schema_privilege_name_name(PG_FUNCTION_ARGS)
{
	Name		username = PG_GETARG_NAME(0);
	text	   *schemaname = PG_GETARG_TEXT_P(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	int32		usesysid;
	Oid			schemaoid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = get_usesysid(NameStr(*username));
	schemaoid = convert_schema_name(schemaname);
	mode = convert_schema_priv_string(priv_type_text);

	aclresult = pg_namespace_aclcheck(schemaoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_schema_privilege_name
 *		Check user privileges on a schema given
 *		text schemaname and text priv name.
 *		current_user is assumed
 */
Datum
has_schema_privilege_name(PG_FUNCTION_ARGS)
{
	text	   *schemaname = PG_GETARG_TEXT_P(0);
	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
1612
	AclId		usesysid;
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
	Oid			schemaoid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = GetUserId();
	schemaoid = convert_schema_name(schemaname);
	mode = convert_schema_priv_string(priv_type_text);

	aclresult = pg_namespace_aclcheck(schemaoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_schema_privilege_name_id
 *		Check user privileges on a schema given
 *		name usename, schema oid, and text priv name.
 */
Datum
has_schema_privilege_name_id(PG_FUNCTION_ARGS)
{
	Name		username = PG_GETARG_NAME(0);
	Oid			schemaoid = PG_GETARG_OID(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	int32		usesysid;
	AclMode		mode;
	AclResult	aclresult;

	usesysid = get_usesysid(NameStr(*username));
	mode = convert_schema_priv_string(priv_type_text);

	aclresult = pg_namespace_aclcheck(schemaoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_schema_privilege_id
 *		Check user privileges on a schema given
 *		schema oid, and text priv name.
 *		current_user is assumed
 */
Datum
has_schema_privilege_id(PG_FUNCTION_ARGS)
{
	Oid			schemaoid = PG_GETARG_OID(0);
	text	   *priv_type_text = PG_GETARG_TEXT_P(1);
1660
	AclId		usesysid;
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
	AclMode		mode;
	AclResult	aclresult;

	usesysid = GetUserId();
	mode = convert_schema_priv_string(priv_type_text);

	aclresult = pg_namespace_aclcheck(schemaoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_schema_privilege_id_name
 *		Check user privileges on a schema given
 *		usesysid, text schemaname, and text priv name.
 */
Datum
has_schema_privilege_id_name(PG_FUNCTION_ARGS)
{
	int32		usesysid = PG_GETARG_INT32(0);
	text	   *schemaname = PG_GETARG_TEXT_P(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	Oid			schemaoid;
	AclMode		mode;
	AclResult	aclresult;

	schemaoid = convert_schema_name(schemaname);
	mode = convert_schema_priv_string(priv_type_text);

	aclresult = pg_namespace_aclcheck(schemaoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 * has_schema_privilege_id_id
 *		Check user privileges on a schema given
 *		usesysid, schema oid, and text priv name.
 */
Datum
has_schema_privilege_id_id(PG_FUNCTION_ARGS)
{
	int32		usesysid = PG_GETARG_INT32(0);
	Oid			schemaoid = PG_GETARG_OID(1);
	text	   *priv_type_text = PG_GETARG_TEXT_P(2);
	AclMode		mode;
	AclResult	aclresult;

	mode = convert_schema_priv_string(priv_type_text);

	aclresult = pg_namespace_aclcheck(schemaoid, usesysid, mode);

	PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
}

/*
 *		Support routines for has_schema_privilege family.
 */

/*
 * Given a schema name expressed as a string, look it up and return Oid
 */
static Oid
convert_schema_name(text *schemaname)
{
	char	   *nspname;
	Oid			oid;

	nspname = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
1730
										   PointerGetDatum(schemaname)));
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750

	oid = GetSysCacheOid(NAMESPACENAME,
						 CStringGetDatum(nspname),
						 0, 0, 0);
	if (!OidIsValid(oid))
		elog(ERROR, "schema \"%s\" does not exist", nspname);

	return oid;
}

/*
 * convert_schema_priv_string
 *		Convert text string to AclMode value.
 */
static AclMode
convert_schema_priv_string(text *priv_type_text)
{
	char	   *priv_type;

	priv_type = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
1751
									   PointerGetDatum(priv_type_text)));
1752 1753 1754 1755 1756 1757

	/*
	 * Return mode from priv_type string
	 */
	if (strcasecmp(priv_type, "CREATE") == 0)
		return ACL_CREATE;
1758 1759
	if (strcasecmp(priv_type, "CREATE WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_CREATE);
1760 1761 1762

	if (strcasecmp(priv_type, "USAGE") == 0)
		return ACL_USAGE;
1763 1764
	if (strcasecmp(priv_type, "USAGE WITH GRANT OPTION") == 0)
		return ACL_GRANT_OPTION_FOR(ACL_USAGE);
1765 1766 1767 1768

	elog(ERROR, "has_schema_privilege: invalid privilege type %s",
		 priv_type);
	return ACL_NO_RIGHTS;		/* keep compiler quiet */
1769
}