acl.c

来自「PostgreSQL7.4.6 for Linux」· C语言 代码 · 共 1,969 行 · 第 1/4 页

C
1,969
字号
/*------------------------------------------------------------------------- * * acl.c *	  Basic access control list data structures manipulation routines. * * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/acl.c,v 1.100 2003/10/29 22:20:54 tgl Exp $ * *------------------------------------------------------------------------- */#include "postgres.h"#include <ctype.h>#include "catalog/namespace.h"#include "catalog/pg_shadow.h"#include "catalog/pg_type.h"#include "commands/dbcommands.h"#include "miscadmin.h"#include "utils/acl.h"#include "utils/builtins.h"#include "utils/lsyscache.h"#include "utils/syscache.h"#define ACL_IDTYPE_GID_KEYWORD	"group"#define ACL_IDTYPE_UID_KEYWORD	"user"static const char *getid(const char *s, char *n);static void putid(char *p, const char *s);static Acl *allocacl(int n);static const char *aclparse(const char *s, AclItem *aip);static bool aclitem_match(const AclItem *a1, const AclItem *a2);static Acl *recursive_revoke(Acl *acl, AclId grantee,				 AclMode revoke_privs, DropBehavior behavior);static AclMode convert_priv_string(text *priv_type_text);static Oid	convert_table_name(text *tablename);static AclMode convert_table_priv_string(text *priv_type_text);static Oid	convert_database_name(text *databasename);static AclMode convert_database_priv_string(text *priv_type_text);static Oid	convert_function_name(text *functionname);static AclMode convert_function_priv_string(text *priv_type_text);static Oid	convert_language_name(text *languagename);static AclMode convert_language_priv_string(text *priv_type_text);static Oid	convert_schema_name(text *schemaname);static AclMode convert_schema_priv_string(text *priv_type_text);/* * getid *		Consumes the first alphanumeric string (identifier) found in string *		's', ignoring any leading white space.	If it finds a double quote *		it returns the word inside the quotes. * * RETURNS: *		the string position in 's' that points to the next non-space character *		in 's', after any quotes.  Also: *		- loads the identifier into 'n'.  (If no identifier is found, 'n' *		  contains an empty string.)  'n' must be NAMEDATALEN bytes. */static const char *getid(const char *s, char *n){	int			len = 0;	bool		in_quotes = false;	Assert(s && n);	while (isspace((unsigned char) *s))		s++;	/* This code had better match what putid() does, below */	for (;		 *s != '\0' &&		 (isalnum((unsigned char) *s) ||		  *s == '_' ||		  *s == '"' ||		  in_quotes);		 s++)	{		if (*s == '"')		{			/* safe to look at next char (could be '\0' though) */			if (*(s + 1) != '"')			{				in_quotes = !in_quotes;				continue;			}			/* it's an escaped double quote; skip the escaping char */			s++;		}		/* Add the character to the string */		if (len >= NAMEDATALEN - 1)			ereport(ERROR,					(errcode(ERRCODE_NAME_TOO_LONG),					 errmsg("identifier too long"),					 errdetail("Identifier must be less than %d characters.",							   NAMEDATALEN)));		n[len++] = *s;	}	n[len] = '\0';	while (isspace((unsigned char) *s))		s++;	return s;}/* * Write a user or group Name at *p, adding double quotes if needed. * There must be at least (2*NAMEDATALEN)+2 bytes available at *p. * This needs to be kept in sync with copyAclUserName in pg_dump/dumputils.c */static voidputid(char *p, const char *s){	const char *src;	bool		safe = true;	for (src = s; *src; src++)	{		/* This test had better match what getid() does, above */		if (!isalnum((unsigned char) *src) && *src != '_')		{			safe = false;			break;		}	}	if (!safe)		*p++ = '"';	for (src = s; *src; src++)	{		/* A double quote character in a username is encoded as "" */		if (*src == '"')			*p++ = '"';		*p++ = *src;	}	if (!safe)		*p++ = '"';	*p = '\0';}/* * aclparse *		Consumes and parses an ACL specification of the form: *				[group|user] [A-Za-z0-9]*=[rwaR]* *		from string 's', ignoring any leading white space or white space *		between the optional id type keyword (group|user) and the actual *		ACL specification. * *		This routine is called by the parser as well as aclitemin(), hence *		the added generality. * * RETURNS: *		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. */static const char *aclparse(const char *s, AclItem *aip){	AclMode		privs,				goption,				read;	uint32		idtype;	char		name[NAMEDATALEN];	char		name2[NAMEDATALEN];	Assert(s && aip);#ifdef ACLDEBUG	elog(LOG, "aclparse: input = \"%s\"", s);#endif	idtype = ACL_IDTYPE_UID;	s = getid(s, name);	if (*s != '=')	{		/* we just read a keyword, not a name */		if (strcmp(name, ACL_IDTYPE_GID_KEYWORD) == 0)			idtype = ACL_IDTYPE_GID;		else if (strcmp(name, ACL_IDTYPE_UID_KEYWORD) != 0)			ereport(ERROR,					(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),					 errmsg("unrecognized key word: \"%s\"", name),				 errhint("ACL key word must be \"group\" or \"user\".")));		s = getid(s, name);		/* move s to the name beyond the keyword */		if (name[0] == '\0')			ereport(ERROR,					(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),					 errmsg("missing name"),			   errhint("A name must follow the \"group\" or \"user\" key word.")));	}	if (name[0] == '\0')		idtype = ACL_IDTYPE_WORLD;	if (*s != '=')		ereport(ERROR,				(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),				 errmsg("missing \"=\" sign")));	privs = goption = ACL_NO_RIGHTS;	for (++s, read = 0; isalpha((unsigned char) *s) || *s == '*'; s++)	{		switch (*s)		{			case '*':				goption |= read;				break;			case ACL_INSERT_CHR:				read = ACL_INSERT;				break;			case ACL_SELECT_CHR:				read = ACL_SELECT;				break;			case ACL_UPDATE_CHR:				read = ACL_UPDATE;				break;			case ACL_DELETE_CHR:				read = ACL_DELETE;				break;			case ACL_RULE_CHR:				read = ACL_RULE;				break;			case ACL_REFERENCES_CHR:				read = ACL_REFERENCES;				break;			case ACL_TRIGGER_CHR:				read = ACL_TRIGGER;				break;			case ACL_EXECUTE_CHR:				read = ACL_EXECUTE;				break;			case ACL_USAGE_CHR:				read = ACL_USAGE;				break;			case ACL_CREATE_CHR:				read = ACL_CREATE;				break;			case ACL_CREATE_TEMP_CHR:				read = ACL_CREATE_TEMP;				break;			default:				ereport(ERROR,						(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),				  errmsg("invalid mode character: must be one of \"%s\"",						 ACL_ALL_RIGHTS_STR)));		}		privs |= read;	}	switch (idtype)	{		case ACL_IDTYPE_UID:			aip->ai_grantee = get_usesysid(name);			break;		case ACL_IDTYPE_GID:			aip->ai_grantee = get_grosysid(name);			break;		case ACL_IDTYPE_WORLD:			aip->ai_grantee = ACL_ID_WORLD;			break;	}	/*	 * 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')			ereport(ERROR,					(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),					 errmsg("a name must follow the \"/\" sign")));		aip->ai_grantor = get_usesysid(name2);	}	else	{		aip->ai_grantor = BOOTSTRAP_USESYSID;		ereport(WARNING,				(errcode(ERRCODE_INVALID_GRANTOR),				 errmsg("defaulting grantor to user ID %u", BOOTSTRAP_USESYSID)));	}	ACLITEM_SET_PRIVS_IDTYPE(*aip, privs, goption, idtype);#ifdef ACLDEBUG	elog(LOG, "aclparse: correctly read [%x %d %x]",		 idtype, aip->ai_grantee, privs);#endif	return s;}/* * allocacl *		Allocates storage for a new Acl with 'n' entries. * * RETURNS: *		the new Acl */static Acl *allocacl(int n){	Acl		   *new_acl;	Size		size;	if (n < 0)		elog(ERROR, "invalid size: %d", n);	size = ACL_N_SIZE(n);	new_acl = (Acl *) palloc0(size);	new_acl->size = size;	new_acl->ndim = 1;	new_acl->flags = 0;	new_acl->elemtype = ACLITEMOID;	ARR_LBOUND(new_acl)[0] = 0;	ARR_DIMS(new_acl)[0] = n;	return new_acl;}/* * aclitemin *		Allocates storage for, and fills in, a new AclItem given a string *		's' that contains an ACL specification.  See aclparse for details. * * RETURNS: *		the new AclItem */Datumaclitemin(PG_FUNCTION_ARGS){	const char *s = PG_GETARG_CSTRING(0);	AclItem    *aip;	aip = (AclItem *) palloc(sizeof(AclItem));	s = aclparse(s, aip);	while (isspace((unsigned char) *s))		++s;	if (*s)		ereport(ERROR,				(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),		   errmsg("extra garbage at the end of the ACL specification")));	PG_RETURN_ACLITEM_P(aip);}/* * aclitemout *		Allocates storage for, and fills in, a new null-delimited string *		containing a formatted ACL specification.  See aclparse for details. * * RETURNS: *		the new string */Datumaclitemout(PG_FUNCTION_ARGS){	AclItem    *aip = PG_GETARG_ACLITEM_P(0);	char	   *p;	char	   *out;	HeapTuple	htup;	unsigned	i;	char	   *tmpname;	out = palloc(strlen("group =/") +				 2 * N_ACL_RIGHTS +				 2 * (2 * NAMEDATALEN + 2) +				 1);	p = out;	*p = '\0';	switch (ACLITEM_GET_IDTYPE(*aip))	{		case ACL_IDTYPE_UID:			htup = SearchSysCache(SHADOWSYSID,								  ObjectIdGetDatum(aip->ai_grantee),								  0, 0, 0);			if (HeapTupleIsValid(htup))			{				putid(p, NameStr(((Form_pg_shadow) GETSTRUCT(htup))->usename));				ReleaseSysCache(htup);			}			else			{				/* Generate numeric UID if we don't find an entry */				sprintf(p, "%d", aip->ai_grantee);			}			break;		case ACL_IDTYPE_GID:			strcpy(p, "group ");			p += strlen(p);			tmpname = get_groname(aip->ai_grantee);			if (tmpname != NULL)				putid(p, tmpname);			else			{				/* Generate numeric GID if we don't find an entry */				sprintf(p, "%d", aip->ai_grantee);			}			break;		case ACL_IDTYPE_WORLD:			break;		default:			elog(ERROR, "unrecognized idtype: %d",				 (int) ACLITEM_GET_IDTYPE(*aip));			break;	}	while (*p)		++p;	*p++ = '=';	for (i = 0; i < N_ACL_RIGHTS; ++i)	{		if (ACLITEM_GET_PRIVS(*aip) & (1 << i))			*p++ = ACL_ALL_RIGHTS_STR[i];		if (ACLITEM_GET_GOPTIONS(*aip) & (1 << i))			*p++ = '*';	}	*p++ = '/';	*p = '\0';	htup = SearchSysCache(SHADOWSYSID,						  ObjectIdGetDatum(aip->ai_grantor),						  0, 0, 0);	if (HeapTupleIsValid(htup))	{		putid(p, NameStr(((Form_pg_shadow) GETSTRUCT(htup))->usename));		ReleaseSysCache(htup);	}	else	{		/* Generate numeric UID if we don't find an entry */		sprintf(p, "%d", aip->ai_grantor);	}	while (*p)		++p;	*p = '\0';	PG_RETURN_CSTRING(out);}/* * aclitem_match *		Two AclItems are considered to match iff they have the same *		grantee and grantor; the privileges are ignored. */static boolaclitem_match(const AclItem *a1, const AclItem *a2){	return ACLITEM_GET_IDTYPE(*a1) == ACLITEM_GET_IDTYPE(*a2) &&		a1->ai_grantee == a2->ai_grantee &&		a1->ai_grantor == a2->ai_grantor;}/* * aclitem equality operator */Datumaclitem_eq(PG_FUNCTION_ARGS){	AclItem    *a1 = PG_GETARG_ACLITEM_P(0);	AclItem    *a2 = PG_GETARG_ACLITEM_P(1);	bool		result;	result = a1->ai_privs == a2->ai_privs &&		a1->ai_grantee == a2->ai_grantee &&		a1->ai_grantor == a2->ai_grantor;	PG_RETURN_BOOL(result);}/* * aclitem hash function * * We make aclitems hashable not so much because anyone is likely to hash * them, as because we want array equality to work on aclitem arrays, and * with the typcache mechanism we must have a hash or btree opclass. */Datumhash_aclitem(PG_FUNCTION_ARGS){	AclItem    *a = PG_GETARG_ACLITEM_P(0);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?