1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* aclchk.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Routines to check access control permissions.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-06-20 22:29:54 +02:00
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2003-07-21 03:59:11 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.84 2003/07/21 01:59:07 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* See acl.h.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1996-11-03 07:54:38 +01:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "catalog/catalog.h"
|
|
|
|
#include "catalog/catname.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "catalog/indexing.h"
|
2002-03-26 20:17:02 +01:00
|
|
|
#include "catalog/namespace.h"
|
2002-04-21 02:26:44 +02:00
|
|
|
#include "catalog/pg_database.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "catalog/pg_group.h"
|
2002-02-19 00:11:58 +01:00
|
|
|
#include "catalog/pg_language.h"
|
2002-04-21 02:26:44 +02:00
|
|
|
#include "catalog/pg_namespace.h"
|
2002-07-30 00:14:11 +02:00
|
|
|
#include "catalog/pg_opclass.h"
|
1996-10-31 06:55:24 +01:00
|
|
|
#include "catalog/pg_operator.h"
|
1996-11-08 07:02:30 +01:00
|
|
|
#include "catalog/pg_proc.h"
|
1998-02-25 14:09:49 +01:00
|
|
|
#include "catalog/pg_shadow.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "catalog/pg_type.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "miscadmin.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "parser/parse_func.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/acl.h"
|
2002-04-21 02:26:44 +02:00
|
|
|
#include "utils/fmgroids.h"
|
|
|
|
#include "utils/lsyscache.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
static void ExecuteGrantStmt_Relation(GrantStmt *stmt);
|
|
|
|
static void ExecuteGrantStmt_Database(GrantStmt *stmt);
|
2002-02-19 00:11:58 +01:00
|
|
|
static void ExecuteGrantStmt_Function(GrantStmt *stmt);
|
2002-04-21 02:26:44 +02:00
|
|
|
static void ExecuteGrantStmt_Language(GrantStmt *stmt);
|
|
|
|
static void ExecuteGrantStmt_Namespace(GrantStmt *stmt);
|
2002-02-19 00:11:58 +01:00
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
static const char *privilege_to_string(AclMode privilege);
|
2002-02-19 00:11:58 +01:00
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
static AclResult aclcheck(Acl *acl, AclId userid, AclMode mode);
|
1997-03-12 21:48:48 +01:00
|
|
|
|
2000-10-02 06:49:28 +02:00
|
|
|
|
2001-05-27 11:59:30 +02:00
|
|
|
#ifdef ACLDEBUG
|
1996-07-09 08:22:35 +02:00
|
|
|
static
|
1997-09-08 22:59:27 +02:00
|
|
|
dumpacl(Acl *acl)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-08-01 00:39:17 +02:00
|
|
|
int i;
|
1997-09-08 04:41:22 +02:00
|
|
|
AclItem *aip;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-27 19:49:47 +02:00
|
|
|
elog(DEBUG2, "acl size = %d, # acls = %d",
|
1997-09-07 07:04:48 +02:00
|
|
|
ACL_SIZE(acl), ACL_NUM(acl));
|
2000-08-01 00:39:17 +02:00
|
|
|
aip = ACL_DAT(acl);
|
1997-09-07 07:04:48 +02:00
|
|
|
for (i = 0; i < ACL_NUM(acl); ++i)
|
2003-05-27 19:49:47 +02:00
|
|
|
elog(DEBUG2, " acl[%d]: %s", i,
|
2000-08-01 00:39:17 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(aclitemout,
|
2001-03-22 05:01:46 +01:00
|
|
|
PointerGetDatum(aip + i))));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* ACLDEBUG */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-06-10 01:21:55 +02:00
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
/*
|
|
|
|
* If is_grant is true, adds the given privileges for the list of
|
|
|
|
* grantees to the existing old_acl. If is_grant is false, the
|
|
|
|
* privileges for the given grantees are removed from old_acl.
|
|
|
|
*/
|
2002-04-21 02:26:44 +02:00
|
|
|
static Acl *
|
|
|
|
merge_acl_with_grant(Acl *old_acl, bool is_grant,
|
2003-01-24 00:39:07 +01:00
|
|
|
List *grantees, AclMode privileges,
|
|
|
|
bool grant_option, DropBehavior behavior)
|
2002-02-19 00:11:58 +01:00
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
unsigned modechg;
|
2002-02-19 00:11:58 +01:00
|
|
|
List *j;
|
|
|
|
Acl *new_acl;
|
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
modechg = is_grant ? ACL_MODECHG_ADD : ACL_MODECHG_DEL;
|
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
#ifdef ACLDEBUG
|
|
|
|
dumpacl(old_acl);
|
|
|
|
#endif
|
|
|
|
new_acl = old_acl;
|
|
|
|
|
|
|
|
foreach(j, grantees)
|
|
|
|
{
|
|
|
|
PrivGrantee *grantee = (PrivGrantee *) lfirst(j);
|
2002-09-04 22:31:48 +02:00
|
|
|
AclItem aclitem;
|
2002-04-21 02:26:44 +02:00
|
|
|
uint32 idtype;
|
2002-02-19 00:11:58 +01:00
|
|
|
|
|
|
|
if (grantee->username)
|
2002-04-21 02:26:44 +02:00
|
|
|
{
|
2003-01-24 00:39:07 +01:00
|
|
|
aclitem.ai_grantee = get_usesysid(grantee->username);
|
2002-04-21 02:26:44 +02:00
|
|
|
idtype = ACL_IDTYPE_UID;
|
|
|
|
}
|
2002-02-19 00:11:58 +01:00
|
|
|
else if (grantee->groupname)
|
2002-04-21 02:26:44 +02:00
|
|
|
{
|
2003-01-24 00:39:07 +01:00
|
|
|
aclitem.ai_grantee = get_grosysid(grantee->groupname);
|
2002-04-21 02:26:44 +02:00
|
|
|
idtype = ACL_IDTYPE_GID;
|
|
|
|
}
|
2002-02-19 00:11:58 +01:00
|
|
|
else
|
2002-04-21 02:26:44 +02:00
|
|
|
{
|
2003-01-24 00:39:07 +01:00
|
|
|
aclitem.ai_grantee = ACL_ID_WORLD;
|
2002-04-21 02:26:44 +02:00
|
|
|
idtype = ACL_IDTYPE_WORLD;
|
|
|
|
}
|
2002-02-19 00:11:58 +01:00
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
/*
|
|
|
|
* Grant options can only be granted to individual users, not
|
|
|
|
* groups or public. The reason is that if a user would
|
|
|
|
* re-grant a privilege that he held through a group having a
|
|
|
|
* grant option, and later the user is removed from the group,
|
|
|
|
* the situation is impossible to clean up.
|
|
|
|
*/
|
|
|
|
if (is_grant && idtype != ACL_IDTYPE_UID && grant_option)
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_GRANT_OPERATION),
|
|
|
|
errmsg("grant options can only be granted to individual users")));
|
2003-01-24 00:39:07 +01:00
|
|
|
|
|
|
|
aclitem.ai_grantor = GetUserId();
|
|
|
|
|
|
|
|
ACLITEM_SET_PRIVS_IDTYPE(aclitem,
|
|
|
|
(is_grant || !grant_option) ? privileges : ACL_NO_RIGHTS,
|
|
|
|
(grant_option || !is_grant) ? privileges : ACL_NO_RIGHTS,
|
|
|
|
idtype);
|
2002-02-19 00:11:58 +01:00
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
new_acl = aclinsert3(new_acl, &aclitem, modechg, behavior);
|
2002-02-19 00:11:58 +01:00
|
|
|
|
|
|
|
#ifdef ACLDEBUG
|
|
|
|
dumpacl(new_acl);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_acl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2001-06-10 01:21:55 +02:00
|
|
|
* Called to execute the utility commands GRANT and REVOKE
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
2001-06-10 01:21:55 +02:00
|
|
|
ExecuteGrantStmt(GrantStmt *stmt)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
switch (stmt->objtype)
|
2002-02-19 00:11:58 +01:00
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
case ACL_OBJECT_RELATION:
|
|
|
|
ExecuteGrantStmt_Relation(stmt);
|
|
|
|
break;
|
|
|
|
case ACL_OBJECT_DATABASE:
|
|
|
|
ExecuteGrantStmt_Database(stmt);
|
2002-02-19 00:11:58 +01:00
|
|
|
break;
|
2002-04-21 02:26:44 +02:00
|
|
|
case ACL_OBJECT_FUNCTION:
|
2002-02-19 00:11:58 +01:00
|
|
|
ExecuteGrantStmt_Function(stmt);
|
|
|
|
break;
|
2002-04-21 02:26:44 +02:00
|
|
|
case ACL_OBJECT_LANGUAGE:
|
|
|
|
ExecuteGrantStmt_Language(stmt);
|
|
|
|
break;
|
|
|
|
case ACL_OBJECT_NAMESPACE:
|
|
|
|
ExecuteGrantStmt_Namespace(stmt);
|
2002-02-19 00:11:58 +01:00
|
|
|
break;
|
|
|
|
default:
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "unrecognized GrantStmt.objtype: %d",
|
|
|
|
(int) stmt->objtype);
|
2002-02-19 00:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-04-21 02:26:44 +02:00
|
|
|
ExecuteGrantStmt_Relation(GrantStmt *stmt)
|
2002-02-19 00:11:58 +01:00
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
AclMode privileges;
|
2002-02-19 00:11:58 +01:00
|
|
|
List *i;
|
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
if (lfirsti(stmt->privileges) == ACL_ALL_RIGHTS)
|
|
|
|
privileges = ACL_ALL_RIGHTS_RELATION;
|
2002-02-19 00:11:58 +01:00
|
|
|
else
|
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
privileges = ACL_NO_RIGHTS;
|
2002-02-19 00:11:58 +01:00
|
|
|
foreach(i, stmt->privileges)
|
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
AclMode priv = lfirsti(i);
|
2002-02-19 00:11:58 +01:00
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
if (priv & ~((AclMode) ACL_ALL_RIGHTS_RELATION))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_GRANT_OPERATION),
|
|
|
|
errmsg("invalid privilege type %s for table",
|
|
|
|
privilege_to_string(priv))));
|
2002-04-21 02:26:44 +02:00
|
|
|
privileges |= priv;
|
2002-02-19 00:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(i, stmt->objects)
|
2000-08-01 00:39:17 +02:00
|
|
|
{
|
2002-03-26 20:17:02 +01:00
|
|
|
RangeVar *relvar = (RangeVar *) lfirst(i);
|
|
|
|
Oid relOid;
|
2001-06-10 01:21:55 +02:00
|
|
|
Relation relation;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Form_pg_class pg_class_tuple;
|
|
|
|
Datum aclDatum;
|
|
|
|
bool isNull;
|
|
|
|
Acl *old_acl;
|
|
|
|
Acl *new_acl;
|
|
|
|
HeapTuple newtuple;
|
|
|
|
Datum values[Natts_pg_class];
|
|
|
|
char nulls[Natts_pg_class];
|
|
|
|
char replaces[Natts_pg_class];
|
|
|
|
|
|
|
|
/* open pg_class */
|
|
|
|
relation = heap_openr(RelationRelationName, RowExclusiveLock);
|
2002-03-26 20:17:02 +01:00
|
|
|
relOid = RangeVarGetRelid(relvar, false);
|
|
|
|
tuple = SearchSysCache(RELOID,
|
|
|
|
ObjectIdGetDatum(relOid),
|
2001-06-10 01:21:55 +02:00
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "cache lookup failed for relation %u", relOid);
|
2001-06-10 01:21:55 +02:00
|
|
|
pg_class_tuple = (Form_pg_class) GETSTRUCT(tuple);
|
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
if (stmt->is_grant
|
|
|
|
&& !pg_class_ownercheck(relOid, GetUserId())
|
|
|
|
&& pg_class_aclcheck(relOid, GetUserId(), ACL_GRANT_OPTION_FOR(privileges)) != ACLCHECK_OK)
|
|
|
|
aclcheck_error(ACLCHECK_NO_PRIV, relvar->relname);
|
2002-03-22 00:27:25 +01:00
|
|
|
|
2003-07-21 03:59:11 +02:00
|
|
|
/* Not sensible to grant on an index */
|
2001-06-10 01:21:55 +02:00
|
|
|
if (pg_class_tuple->relkind == RELKIND_INDEX)
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is an index",
|
|
|
|
relvar->relname)));
|
2001-06-10 01:21:55 +02:00
|
|
|
|
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* If there's no ACL, create a default using the pg_class.relowner
|
|
|
|
* field.
|
2001-06-10 01:21:55 +02:00
|
|
|
*/
|
2002-03-26 20:17:02 +01:00
|
|
|
aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
|
2001-06-10 01:21:55 +02:00
|
|
|
&isNull);
|
|
|
|
if (isNull)
|
2002-04-21 02:26:44 +02:00
|
|
|
old_acl = acldefault(ACL_OBJECT_RELATION,
|
|
|
|
pg_class_tuple->relowner);
|
2001-06-10 01:21:55 +02:00
|
|
|
else
|
2002-04-21 02:26:44 +02:00
|
|
|
/* get a detoasted copy of the ACL */
|
2001-06-10 01:21:55 +02:00
|
|
|
old_acl = DatumGetAclPCopy(aclDatum);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
new_acl = merge_acl_with_grant(old_acl, stmt->is_grant,
|
2003-01-24 00:39:07 +01:00
|
|
|
stmt->grantees, privileges,
|
|
|
|
stmt->grant_option, stmt->behavior);
|
2001-06-10 01:21:55 +02:00
|
|
|
|
|
|
|
/* finished building new ACL value, now insert it */
|
2002-04-21 02:26:44 +02:00
|
|
|
MemSet(values, 0, sizeof(values));
|
|
|
|
MemSet(nulls, ' ', sizeof(nulls));
|
|
|
|
MemSet(replaces, ' ', sizeof(replaces));
|
|
|
|
|
2001-06-10 01:21:55 +02:00
|
|
|
replaces[Anum_pg_class_relacl - 1] = 'r';
|
|
|
|
values[Anum_pg_class_relacl - 1] = PointerGetDatum(new_acl);
|
2002-04-21 02:26:44 +02:00
|
|
|
|
2001-06-10 01:21:55 +02:00
|
|
|
newtuple = heap_modifytuple(tuple, relation, values, nulls, replaces);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-06-10 01:21:55 +02:00
|
|
|
ReleaseSysCache(tuple);
|
2000-11-16 23:30:52 +01:00
|
|
|
|
2001-06-10 01:21:55 +02:00
|
|
|
simple_heap_update(relation, &newtuple->t_self, newtuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-08-05 05:29:17 +02:00
|
|
|
/* keep the catalog indexes up to date */
|
|
|
|
CatalogUpdateIndexes(relation, newtuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-10 01:21:55 +02:00
|
|
|
pfree(old_acl);
|
|
|
|
pfree(new_acl);
|
2000-10-02 06:49:28 +02:00
|
|
|
|
2001-06-10 01:21:55 +02:00
|
|
|
heap_close(relation, RowExclusiveLock);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
static void
|
|
|
|
ExecuteGrantStmt_Database(GrantStmt *stmt)
|
|
|
|
{
|
|
|
|
AclMode privileges;
|
|
|
|
List *i;
|
|
|
|
|
|
|
|
if (lfirsti(stmt->privileges) == ACL_ALL_RIGHTS)
|
|
|
|
privileges = ACL_ALL_RIGHTS_DATABASE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
privileges = ACL_NO_RIGHTS;
|
|
|
|
foreach(i, stmt->privileges)
|
|
|
|
{
|
|
|
|
AclMode priv = lfirsti(i);
|
|
|
|
|
|
|
|
if (priv & ~((AclMode) ACL_ALL_RIGHTS_DATABASE))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_GRANT_OPERATION),
|
|
|
|
errmsg("invalid privilege type %s for database",
|
|
|
|
privilege_to_string(priv))));
|
2002-04-21 02:26:44 +02:00
|
|
|
privileges |= priv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(i, stmt->objects)
|
|
|
|
{
|
|
|
|
char *dbname = strVal(lfirst(i));
|
|
|
|
Relation relation;
|
|
|
|
ScanKeyData entry[1];
|
|
|
|
HeapScanDesc scan;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Form_pg_database pg_database_tuple;
|
|
|
|
Datum aclDatum;
|
|
|
|
bool isNull;
|
|
|
|
Acl *old_acl;
|
|
|
|
Acl *new_acl;
|
|
|
|
HeapTuple newtuple;
|
|
|
|
Datum values[Natts_pg_database];
|
|
|
|
char nulls[Natts_pg_database];
|
|
|
|
char replaces[Natts_pg_database];
|
|
|
|
|
|
|
|
relation = heap_openr(DatabaseRelationName, RowExclusiveLock);
|
|
|
|
ScanKeyEntryInitialize(&entry[0], 0,
|
|
|
|
Anum_pg_database_datname, F_NAMEEQ,
|
|
|
|
CStringGetDatum(dbname));
|
2002-05-21 01:51:44 +02:00
|
|
|
scan = heap_beginscan(relation, SnapshotNow, 1, entry);
|
|
|
|
tuple = heap_getnext(scan, ForwardScanDirection);
|
2002-04-21 02:26:44 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
|
|
|
errmsg("database \"%s\" does not exist", dbname)));
|
2002-04-21 02:26:44 +02:00
|
|
|
pg_database_tuple = (Form_pg_database) GETSTRUCT(tuple);
|
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
if (stmt->is_grant
|
|
|
|
&& pg_database_tuple->datdba != GetUserId()
|
|
|
|
&& pg_database_aclcheck(HeapTupleGetOid(tuple), GetUserId(), ACL_GRANT_OPTION_FOR(privileges)) != ACLCHECK_OK)
|
|
|
|
aclcheck_error(ACLCHECK_NO_PRIV, NameStr(pg_database_tuple->datname));
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there's no ACL, create a default.
|
|
|
|
*/
|
|
|
|
aclDatum = heap_getattr(tuple, Anum_pg_database_datacl,
|
|
|
|
RelationGetDescr(relation), &isNull);
|
|
|
|
if (isNull)
|
|
|
|
old_acl = acldefault(ACL_OBJECT_DATABASE,
|
|
|
|
pg_database_tuple->datdba);
|
|
|
|
else
|
|
|
|
/* get a detoasted copy of the ACL */
|
|
|
|
old_acl = DatumGetAclPCopy(aclDatum);
|
|
|
|
|
|
|
|
new_acl = merge_acl_with_grant(old_acl, stmt->is_grant,
|
2003-01-24 00:39:07 +01:00
|
|
|
stmt->grantees, privileges,
|
|
|
|
stmt->grant_option, stmt->behavior);
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
/* finished building new ACL value, now insert it */
|
|
|
|
MemSet(values, 0, sizeof(values));
|
|
|
|
MemSet(nulls, ' ', sizeof(nulls));
|
|
|
|
MemSet(replaces, ' ', sizeof(replaces));
|
|
|
|
|
|
|
|
replaces[Anum_pg_database_datacl - 1] = 'r';
|
|
|
|
values[Anum_pg_database_datacl - 1] = PointerGetDatum(new_acl);
|
|
|
|
|
|
|
|
newtuple = heap_modifytuple(tuple, relation, values, nulls, replaces);
|
|
|
|
|
|
|
|
simple_heap_update(relation, &newtuple->t_self, newtuple);
|
|
|
|
|
2002-08-05 05:29:17 +02:00
|
|
|
/* keep the catalog indexes up to date */
|
|
|
|
CatalogUpdateIndexes(relation, newtuple);
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
pfree(old_acl);
|
|
|
|
pfree(new_acl);
|
|
|
|
|
|
|
|
heap_endscan(scan);
|
|
|
|
|
|
|
|
heap_close(relation, RowExclusiveLock);
|
|
|
|
}
|
|
|
|
}
|
2001-06-10 01:21:55 +02:00
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
static void
|
|
|
|
ExecuteGrantStmt_Function(GrantStmt *stmt)
|
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
AclMode privileges;
|
2002-02-19 00:11:58 +01:00
|
|
|
List *i;
|
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
if (lfirsti(stmt->privileges) == ACL_ALL_RIGHTS)
|
|
|
|
privileges = ACL_ALL_RIGHTS_FUNCTION;
|
2002-02-19 00:11:58 +01:00
|
|
|
else
|
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
privileges = ACL_NO_RIGHTS;
|
2002-02-19 00:11:58 +01:00
|
|
|
foreach(i, stmt->privileges)
|
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
AclMode priv = lfirsti(i);
|
|
|
|
|
|
|
|
if (priv & ~((AclMode) ACL_ALL_RIGHTS_FUNCTION))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_GRANT_OPERATION),
|
|
|
|
errmsg("invalid privilege type %s for function",
|
|
|
|
privilege_to_string(priv))));
|
2002-04-21 02:26:44 +02:00
|
|
|
privileges |= priv;
|
2002-02-19 00:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(i, stmt->objects)
|
|
|
|
{
|
|
|
|
FuncWithArgs *func = (FuncWithArgs *) lfirst(i);
|
|
|
|
Oid oid;
|
|
|
|
Relation relation;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Form_pg_proc pg_proc_tuple;
|
|
|
|
Datum aclDatum;
|
|
|
|
bool isNull;
|
|
|
|
Acl *old_acl;
|
|
|
|
Acl *new_acl;
|
|
|
|
HeapTuple newtuple;
|
|
|
|
Datum values[Natts_pg_proc];
|
|
|
|
char nulls[Natts_pg_proc];
|
|
|
|
char replaces[Natts_pg_proc];
|
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
oid = LookupFuncNameTypeNames(func->funcname, func->funcargs, false);
|
2002-07-18 06:50:10 +02:00
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
relation = heap_openr(ProcedureRelationName, RowExclusiveLock);
|
2002-04-21 02:26:44 +02:00
|
|
|
tuple = SearchSysCache(PROCOID,
|
|
|
|
ObjectIdGetDatum(oid),
|
|
|
|
0, 0, 0);
|
2002-02-19 00:11:58 +01:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "cache lookup failed for function %u", oid);
|
2002-02-19 00:11:58 +01:00
|
|
|
pg_proc_tuple = (Form_pg_proc) GETSTRUCT(tuple);
|
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
if (stmt->is_grant
|
|
|
|
&& !pg_proc_ownercheck(oid, GetUserId())
|
|
|
|
&& pg_proc_aclcheck(oid, GetUserId(), ACL_GRANT_OPTION_FOR(privileges)) != ACLCHECK_OK)
|
|
|
|
aclcheck_error(ACLCHECK_NO_PRIV,
|
2002-04-27 05:45:03 +02:00
|
|
|
NameStr(pg_proc_tuple->proname));
|
2002-02-19 00:11:58 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there's no ACL, create a default using the pg_proc.proowner
|
|
|
|
* field.
|
|
|
|
*/
|
|
|
|
aclDatum = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_proacl,
|
|
|
|
&isNull);
|
|
|
|
if (isNull)
|
2002-04-21 02:26:44 +02:00
|
|
|
old_acl = acldefault(ACL_OBJECT_FUNCTION,
|
|
|
|
pg_proc_tuple->proowner);
|
2002-02-19 00:11:58 +01:00
|
|
|
else
|
2002-04-21 02:26:44 +02:00
|
|
|
/* get a detoasted copy of the ACL */
|
2002-02-19 00:11:58 +01:00
|
|
|
old_acl = DatumGetAclPCopy(aclDatum);
|
|
|
|
|
|
|
|
new_acl = merge_acl_with_grant(old_acl, stmt->is_grant,
|
2003-01-24 00:39:07 +01:00
|
|
|
stmt->grantees, privileges,
|
|
|
|
stmt->grant_option, stmt->behavior);
|
2002-02-19 00:11:58 +01:00
|
|
|
|
|
|
|
/* finished building new ACL value, now insert it */
|
2002-04-21 02:26:44 +02:00
|
|
|
MemSet(values, 0, sizeof(values));
|
|
|
|
MemSet(nulls, ' ', sizeof(nulls));
|
|
|
|
MemSet(replaces, ' ', sizeof(replaces));
|
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
replaces[Anum_pg_proc_proacl - 1] = 'r';
|
|
|
|
values[Anum_pg_proc_proacl - 1] = PointerGetDatum(new_acl);
|
2002-04-21 02:26:44 +02:00
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
newtuple = heap_modifytuple(tuple, relation, values, nulls, replaces);
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
simple_heap_update(relation, &newtuple->t_self, newtuple);
|
|
|
|
|
2002-08-05 05:29:17 +02:00
|
|
|
/* keep the catalog indexes up to date */
|
|
|
|
CatalogUpdateIndexes(relation, newtuple);
|
2002-02-19 00:11:58 +01:00
|
|
|
|
|
|
|
pfree(old_acl);
|
|
|
|
pfree(new_acl);
|
|
|
|
|
|
|
|
heap_close(relation, RowExclusiveLock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-04-21 02:26:44 +02:00
|
|
|
ExecuteGrantStmt_Language(GrantStmt *stmt)
|
2002-02-19 00:11:58 +01:00
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
AclMode privileges;
|
2002-02-19 00:11:58 +01:00
|
|
|
List *i;
|
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
if (lfirsti(stmt->privileges) == ACL_ALL_RIGHTS)
|
|
|
|
privileges = ACL_ALL_RIGHTS_LANGUAGE;
|
2002-02-19 00:11:58 +01:00
|
|
|
else
|
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
privileges = ACL_NO_RIGHTS;
|
2002-02-19 00:11:58 +01:00
|
|
|
foreach(i, stmt->privileges)
|
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
AclMode priv = lfirsti(i);
|
|
|
|
|
|
|
|
if (priv & ~((AclMode) ACL_ALL_RIGHTS_LANGUAGE))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_GRANT_OPERATION),
|
|
|
|
errmsg("invalid privilege type %s for language",
|
|
|
|
privilege_to_string(priv))));
|
2002-04-21 02:26:44 +02:00
|
|
|
privileges |= priv;
|
2002-02-19 00:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(i, stmt->objects)
|
|
|
|
{
|
|
|
|
char *langname = strVal(lfirst(i));
|
|
|
|
Relation relation;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Form_pg_language pg_language_tuple;
|
|
|
|
Datum aclDatum;
|
|
|
|
bool isNull;
|
|
|
|
Acl *old_acl;
|
|
|
|
Acl *new_acl;
|
|
|
|
HeapTuple newtuple;
|
|
|
|
Datum values[Natts_pg_language];
|
|
|
|
char nulls[Natts_pg_language];
|
|
|
|
char replaces[Natts_pg_language];
|
|
|
|
|
|
|
|
relation = heap_openr(LanguageRelationName, RowExclusiveLock);
|
2002-04-21 02:26:44 +02:00
|
|
|
tuple = SearchSysCache(LANGNAME,
|
|
|
|
PointerGetDatum(langname),
|
|
|
|
0, 0, 0);
|
2002-02-19 00:11:58 +01:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("language \"%s\" does not exist", langname)));
|
2002-02-19 00:11:58 +01:00
|
|
|
pg_language_tuple = (Form_pg_language) GETSTRUCT(tuple);
|
|
|
|
|
2002-09-25 01:14:25 +02:00
|
|
|
if (!pg_language_tuple->lanpltrusted && stmt->is_grant)
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("language \"%s\" is not trusted", langname)));
|
2002-02-19 00:11:58 +01:00
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
if (stmt->is_grant
|
|
|
|
&& !superuser()
|
|
|
|
&& pg_language_aclcheck(HeapTupleGetOid(tuple), GetUserId(), ACL_GRANT_OPTION_FOR(privileges)) != ACLCHECK_OK)
|
|
|
|
aclcheck_error(ACLCHECK_NO_PRIV, NameStr(pg_language_tuple->lanname));
|
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
/*
|
|
|
|
* If there's no ACL, create a default.
|
|
|
|
*/
|
|
|
|
aclDatum = SysCacheGetAttr(LANGNAME, tuple, Anum_pg_language_lanacl,
|
|
|
|
&isNull);
|
|
|
|
if (isNull)
|
2002-04-21 02:26:44 +02:00
|
|
|
old_acl = acldefault(ACL_OBJECT_LANGUAGE,
|
|
|
|
InvalidOid);
|
2002-02-19 00:11:58 +01:00
|
|
|
else
|
2002-04-21 02:26:44 +02:00
|
|
|
/* get a detoasted copy of the ACL */
|
2002-02-19 00:11:58 +01:00
|
|
|
old_acl = DatumGetAclPCopy(aclDatum);
|
|
|
|
|
|
|
|
new_acl = merge_acl_with_grant(old_acl, stmt->is_grant,
|
2003-01-24 00:39:07 +01:00
|
|
|
stmt->grantees, privileges,
|
|
|
|
stmt->grant_option, stmt->behavior);
|
2002-02-19 00:11:58 +01:00
|
|
|
|
|
|
|
/* finished building new ACL value, now insert it */
|
2002-04-21 02:26:44 +02:00
|
|
|
MemSet(values, 0, sizeof(values));
|
|
|
|
MemSet(nulls, ' ', sizeof(nulls));
|
|
|
|
MemSet(replaces, ' ', sizeof(replaces));
|
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
replaces[Anum_pg_language_lanacl - 1] = 'r';
|
|
|
|
values[Anum_pg_language_lanacl - 1] = PointerGetDatum(new_acl);
|
2002-04-21 02:26:44 +02:00
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
newtuple = heap_modifytuple(tuple, relation, values, nulls, replaces);
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
simple_heap_update(relation, &newtuple->t_self, newtuple);
|
|
|
|
|
2002-08-05 05:29:17 +02:00
|
|
|
/* keep the catalog indexes up to date */
|
|
|
|
CatalogUpdateIndexes(relation, newtuple);
|
2002-02-19 00:11:58 +01:00
|
|
|
|
|
|
|
pfree(old_acl);
|
|
|
|
pfree(new_acl);
|
|
|
|
|
|
|
|
heap_close(relation, RowExclusiveLock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
static void
|
|
|
|
ExecuteGrantStmt_Namespace(GrantStmt *stmt)
|
2002-02-19 00:11:58 +01:00
|
|
|
{
|
2002-04-21 02:26:44 +02:00
|
|
|
AclMode privileges;
|
|
|
|
List *i;
|
2002-02-19 00:11:58 +01:00
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
if (lfirsti(stmt->privileges) == ACL_ALL_RIGHTS)
|
|
|
|
privileges = ACL_ALL_RIGHTS_NAMESPACE;
|
2002-02-19 00:11:58 +01:00
|
|
|
else
|
2002-04-21 02:26:44 +02:00
|
|
|
{
|
|
|
|
privileges = ACL_NO_RIGHTS;
|
|
|
|
foreach(i, stmt->privileges)
|
|
|
|
{
|
|
|
|
AclMode priv = lfirsti(i);
|
|
|
|
|
|
|
|
if (priv & ~((AclMode) ACL_ALL_RIGHTS_NAMESPACE))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_GRANT_OPERATION),
|
|
|
|
errmsg("invalid privilege type %s for schema",
|
|
|
|
privilege_to_string(priv))));
|
2002-04-21 02:26:44 +02:00
|
|
|
privileges |= priv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(i, stmt->objects)
|
|
|
|
{
|
|
|
|
char *nspname = strVal(lfirst(i));
|
|
|
|
Relation relation;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Form_pg_namespace pg_namespace_tuple;
|
|
|
|
Datum aclDatum;
|
|
|
|
bool isNull;
|
|
|
|
Acl *old_acl;
|
|
|
|
Acl *new_acl;
|
|
|
|
HeapTuple newtuple;
|
|
|
|
Datum values[Natts_pg_namespace];
|
|
|
|
char nulls[Natts_pg_namespace];
|
|
|
|
char replaces[Natts_pg_namespace];
|
|
|
|
|
|
|
|
relation = heap_openr(NamespaceRelationName, RowExclusiveLock);
|
|
|
|
tuple = SearchSysCache(NAMESPACENAME,
|
|
|
|
CStringGetDatum(nspname),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_SCHEMA),
|
|
|
|
errmsg("schema \"%s\" does not exist", nspname)));
|
2002-04-21 02:26:44 +02:00
|
|
|
pg_namespace_tuple = (Form_pg_namespace) GETSTRUCT(tuple);
|
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
if (stmt->is_grant
|
|
|
|
&& !pg_namespace_ownercheck(HeapTupleGetOid(tuple), GetUserId())
|
|
|
|
&& pg_namespace_aclcheck(HeapTupleGetOid(tuple), GetUserId(), ACL_GRANT_OPTION_FOR(privileges)) != ACLCHECK_OK)
|
|
|
|
aclcheck_error(ACLCHECK_NO_PRIV, nspname);
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* If there's no ACL, create a default using the
|
|
|
|
* pg_namespace.nspowner field.
|
2002-04-21 02:26:44 +02:00
|
|
|
*/
|
|
|
|
aclDatum = SysCacheGetAttr(NAMESPACENAME, tuple,
|
|
|
|
Anum_pg_namespace_nspacl,
|
|
|
|
&isNull);
|
|
|
|
if (isNull)
|
|
|
|
old_acl = acldefault(ACL_OBJECT_NAMESPACE,
|
|
|
|
pg_namespace_tuple->nspowner);
|
|
|
|
else
|
|
|
|
/* get a detoasted copy of the ACL */
|
|
|
|
old_acl = DatumGetAclPCopy(aclDatum);
|
|
|
|
|
|
|
|
new_acl = merge_acl_with_grant(old_acl, stmt->is_grant,
|
2003-01-24 00:39:07 +01:00
|
|
|
stmt->grantees, privileges,
|
|
|
|
stmt->grant_option, stmt->behavior);
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
/* finished building new ACL value, now insert it */
|
|
|
|
MemSet(values, 0, sizeof(values));
|
|
|
|
MemSet(nulls, ' ', sizeof(nulls));
|
|
|
|
MemSet(replaces, ' ', sizeof(replaces));
|
|
|
|
|
|
|
|
replaces[Anum_pg_namespace_nspacl - 1] = 'r';
|
|
|
|
values[Anum_pg_namespace_nspacl - 1] = PointerGetDatum(new_acl);
|
|
|
|
|
|
|
|
newtuple = heap_modifytuple(tuple, relation, values, nulls, replaces);
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
simple_heap_update(relation, &newtuple->t_self, newtuple);
|
|
|
|
|
2002-08-05 05:29:17 +02:00
|
|
|
/* keep the catalog indexes up to date */
|
|
|
|
CatalogUpdateIndexes(relation, newtuple);
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
pfree(old_acl);
|
|
|
|
pfree(new_acl);
|
|
|
|
|
|
|
|
heap_close(relation, RowExclusiveLock);
|
|
|
|
}
|
2002-02-19 00:11:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
static const char *
|
|
|
|
privilege_to_string(AclMode privilege)
|
|
|
|
{
|
|
|
|
switch (privilege)
|
|
|
|
{
|
|
|
|
case ACL_INSERT:
|
|
|
|
return "INSERT";
|
|
|
|
case ACL_SELECT:
|
|
|
|
return "SELECT";
|
|
|
|
case ACL_UPDATE:
|
|
|
|
return "UPDATE";
|
|
|
|
case ACL_DELETE:
|
|
|
|
return "DELETE";
|
|
|
|
case ACL_RULE:
|
|
|
|
return "RULE";
|
|
|
|
case ACL_REFERENCES:
|
|
|
|
return "REFERENCES";
|
|
|
|
case ACL_TRIGGER:
|
|
|
|
return "TRIGGER";
|
|
|
|
case ACL_EXECUTE:
|
|
|
|
return "EXECUTE";
|
|
|
|
case ACL_USAGE:
|
|
|
|
return "USAGE";
|
|
|
|
case ACL_CREATE:
|
|
|
|
return "CREATE";
|
|
|
|
case ACL_CREATE_TEMP:
|
|
|
|
return "TEMP";
|
|
|
|
default:
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "unrecognized privilege: %d", (int) privilege);
|
2002-04-21 02:26:44 +02:00
|
|
|
}
|
|
|
|
return NULL; /* appease compiler */
|
|
|
|
}
|
|
|
|
|
2001-06-10 01:21:55 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
AclId
|
|
|
|
get_grosysid(char *groname)
|
|
|
|
{
|
1998-08-19 04:04:17 +02:00
|
|
|
HeapTuple tuple;
|
1997-09-08 04:41:22 +02:00
|
|
|
AclId id = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
tuple = SearchSysCache(GRONAME,
|
|
|
|
PointerGetDatum(groname),
|
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (HeapTupleIsValid(tuple))
|
2000-11-16 23:30:52 +01:00
|
|
|
{
|
1998-08-19 04:04:17 +02:00
|
|
|
id = ((Form_pg_group) GETSTRUCT(tuple))->grosysid;
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("group \"%s\" does not exist", groname)));
|
1998-09-01 05:29:17 +02:00
|
|
|
return id;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-11-29 00:42:31 +01:00
|
|
|
/*
|
|
|
|
* Convert group ID to name, or return NULL if group can't be found
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
char *
|
1996-07-09 08:22:35 +02:00
|
|
|
get_groname(AclId grosysid)
|
|
|
|
{
|
1998-08-19 04:04:17 +02:00
|
|
|
HeapTuple tuple;
|
1997-09-08 04:41:22 +02:00
|
|
|
char *name = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
tuple = SearchSysCache(GROSYSID,
|
|
|
|
ObjectIdGetDatum(grosysid),
|
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (HeapTupleIsValid(tuple))
|
2000-11-16 23:30:52 +01:00
|
|
|
{
|
|
|
|
name = pstrdup(NameStr(((Form_pg_group) GETSTRUCT(tuple))->groname));
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
}
|
1998-09-01 05:29:17 +02:00
|
|
|
return name;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-06-05 21:34:56 +02:00
|
|
|
/*
|
|
|
|
* Is user a member of group?
|
|
|
|
*/
|
2000-08-01 00:39:17 +02:00
|
|
|
static bool
|
1996-07-09 08:22:35 +02:00
|
|
|
in_group(AclId uid, AclId gid)
|
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
bool result = false;
|
1998-08-19 04:04:17 +02:00
|
|
|
HeapTuple tuple;
|
2000-11-03 20:02:18 +01:00
|
|
|
Datum att;
|
|
|
|
bool isNull;
|
2001-06-05 21:34:56 +02:00
|
|
|
IdList *glist;
|
2000-11-03 20:02:18 +01:00
|
|
|
AclId *aidp;
|
2000-08-01 00:39:17 +02:00
|
|
|
int i,
|
1997-09-08 04:41:22 +02:00
|
|
|
num;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
tuple = SearchSysCache(GROSYSID,
|
|
|
|
ObjectIdGetDatum(gid),
|
|
|
|
0, 0, 0);
|
2000-11-03 20:02:18 +01:00
|
|
|
if (HeapTupleIsValid(tuple))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-11-03 20:02:18 +01:00
|
|
|
att = SysCacheGetAttr(GROSYSID,
|
|
|
|
tuple,
|
|
|
|
Anum_pg_group_grolist,
|
|
|
|
&isNull);
|
|
|
|
if (!isNull)
|
|
|
|
{
|
|
|
|
/* be sure the IdList is not toasted */
|
2001-06-05 21:34:56 +02:00
|
|
|
glist = DatumGetIdListP(att);
|
2000-11-03 20:02:18 +01:00
|
|
|
/* scan it */
|
2001-06-05 21:34:56 +02:00
|
|
|
num = IDLIST_NUM(glist);
|
|
|
|
aidp = IDLIST_DAT(glist);
|
2000-11-03 20:02:18 +01:00
|
|
|
for (i = 0; i < num; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-11-03 20:02:18 +01:00
|
|
|
if (aidp[i] == uid)
|
2000-11-16 23:30:52 +01:00
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2001-06-05 21:34:56 +02:00
|
|
|
/* if IdList was toasted, free detoasted copy */
|
|
|
|
if ((Pointer) glist != DatumGetPointer(att))
|
|
|
|
pfree(glist);
|
2000-11-03 20:02:18 +01:00
|
|
|
}
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(tuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(WARNING,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("group with ID %u does not exist", gid)));
|
2000-11-16 23:30:52 +01:00
|
|
|
return result;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2002-04-27 05:45:03 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
|
|
|
* aclcheck
|
2001-06-05 21:34:56 +02:00
|
|
|
*
|
2003-01-24 00:39:07 +01:00
|
|
|
* Returns ACLCHECK_OK if the 'userid' has ACL entries in 'acl' to
|
|
|
|
* satisfy any one of the requirements of 'mode'. Returns an
|
|
|
|
* appropriate ACLCHECK_* error code otherwise.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2002-04-27 05:45:03 +02:00
|
|
|
static AclResult
|
2003-01-24 00:39:07 +01:00
|
|
|
aclcheck(Acl *acl, AclId userid, AclMode mode)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-01-24 00:39:07 +01:00
|
|
|
AclItem *aidat;
|
2000-11-03 20:02:18 +01:00
|
|
|
int i,
|
|
|
|
num;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-10-02 06:49:28 +02:00
|
|
|
/*
|
2003-01-24 00:39:07 +01:00
|
|
|
* Null ACL should not happen, since caller should have inserted
|
|
|
|
* appropriate default
|
2000-10-02 06:49:28 +02:00
|
|
|
*/
|
2003-01-24 00:39:07 +01:00
|
|
|
if (acl == NULL)
|
2000-10-02 06:49:28 +02:00
|
|
|
{
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "null ACL");
|
2003-01-24 00:39:07 +01:00
|
|
|
return ACLCHECK_NO_PRIV;
|
2000-10-02 06:49:28 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
num = ACL_NUM(acl);
|
|
|
|
aidat = ACL_DAT(acl);
|
|
|
|
|
|
|
|
/*
|
2003-01-24 00:39:07 +01:00
|
|
|
* See if privilege is granted directly to user or to public
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2003-01-24 00:39:07 +01:00
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
if (ACLITEM_GET_IDTYPE(aidat[i]) == ACL_IDTYPE_WORLD
|
|
|
|
|| (ACLITEM_GET_IDTYPE(aidat[i]) == ACL_IDTYPE_UID
|
|
|
|
&& aidat[i].ai_grantee == userid))
|
|
|
|
{
|
|
|
|
if (aidat[i].ai_privs & mode)
|
|
|
|
return ACLCHECK_OK;
|
|
|
|
}
|
|
|
|
|
2001-06-05 21:34:56 +02:00
|
|
|
/*
|
2003-01-24 00:39:07 +01:00
|
|
|
* See if he has the permission via any group (do this in a
|
|
|
|
* separate pass to avoid expensive(?) lookups in pg_group)
|
2001-06-05 21:34:56 +02:00
|
|
|
*/
|
2003-01-24 00:39:07 +01:00
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
if (ACLITEM_GET_IDTYPE(aidat[i]) == ACL_IDTYPE_GID
|
|
|
|
&& aidat[i].ai_privs & mode
|
|
|
|
&& in_group(userid, aidat[i].ai_grantee))
|
|
|
|
return ACLCHECK_OK;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
/* If here, doesn't have the privilege. */
|
2001-06-05 21:34:56 +02:00
|
|
|
return ACLCHECK_NO_PRIV;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2002-03-22 00:27:25 +01:00
|
|
|
|
2002-04-27 05:45:03 +02:00
|
|
|
/*
|
|
|
|
* Standardized reporting of aclcheck permissions failures.
|
|
|
|
*/
|
|
|
|
void
|
2003-07-21 03:59:11 +02:00
|
|
|
aclcheck_error(AclResult aclerr, const char *objectname)
|
2002-04-27 05:45:03 +02:00
|
|
|
{
|
2003-07-21 03:59:11 +02:00
|
|
|
switch (aclerr)
|
2002-04-27 05:45:03 +02:00
|
|
|
{
|
|
|
|
case ACLCHECK_OK:
|
|
|
|
/* no error, so return to caller */
|
|
|
|
break;
|
|
|
|
case ACLCHECK_NO_PRIV:
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("permission denied for \"%s\"", objectname)));
|
2002-04-27 05:45:03 +02:00
|
|
|
break;
|
|
|
|
case ACLCHECK_NOT_OWNER:
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("must be owner of \"%s\"", objectname)));
|
2002-04-27 05:45:03 +02:00
|
|
|
break;
|
|
|
|
default:
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
|
2002-04-27 05:45:03 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-05 21:34:56 +02:00
|
|
|
/*
|
|
|
|
* Exported routine for checking a user's access privileges to a table
|
2003-07-21 03:59:11 +02:00
|
|
|
*
|
|
|
|
* Note: we give lookup failure the full ereport treatment because the
|
|
|
|
* has_table_privilege() family of functions allow users to pass
|
|
|
|
* any random OID to this function. Likewise for the sibling functions
|
|
|
|
* below.
|
2001-06-05 21:34:56 +02:00
|
|
|
*/
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult
|
2002-12-04 06:18:38 +01:00
|
|
|
pg_class_aclcheck(Oid table_oid, AclId userid, AclMode mode)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult result;
|
2002-03-22 00:27:25 +01:00
|
|
|
bool usesuper,
|
|
|
|
usecatupd;
|
2000-10-02 06:49:28 +02:00
|
|
|
HeapTuple tuple;
|
|
|
|
Datum aclDatum;
|
|
|
|
bool isNull;
|
|
|
|
Acl *acl;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-05 21:34:56 +02:00
|
|
|
/*
|
2002-04-11 22:00:18 +02:00
|
|
|
* Validate userid, find out if he is superuser, also get usecatupd
|
2001-06-05 21:34:56 +02:00
|
|
|
*/
|
2000-11-16 23:30:52 +01:00
|
|
|
tuple = SearchSysCache(SHADOWSYSID,
|
|
|
|
ObjectIdGetDatum(userid),
|
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("user with ID %u does not exist", userid)));
|
2000-09-06 16:15:31 +02:00
|
|
|
|
2002-03-22 00:27:25 +01:00
|
|
|
usecatupd = ((Form_pg_shadow) GETSTRUCT(tuple))->usecatupd;
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
2002-04-11 07:32:03 +02:00
|
|
|
usesuper = superuser_arg(userid);
|
|
|
|
|
2002-03-22 00:27:25 +01:00
|
|
|
/*
|
|
|
|
* Now get the relation's tuple from pg_class
|
|
|
|
*/
|
|
|
|
tuple = SearchSysCache(RELOID,
|
|
|
|
ObjectIdGetDatum(table_oid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_TABLE),
|
|
|
|
errmsg("relation with OID %u does not exist", table_oid)));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Deny anyone permission to update a system catalog unless
|
1998-02-26 05:46:47 +01:00
|
|
|
* pg_shadow.usecatupd is set. (This is to let superusers protect
|
1997-09-07 07:04:48 +02:00
|
|
|
* themselves from themselves.)
|
|
|
|
*/
|
2001-06-05 21:34:56 +02:00
|
|
|
if ((mode & (ACL_INSERT | ACL_UPDATE | ACL_DELETE)) &&
|
2002-04-12 22:38:31 +02:00
|
|
|
!allowSystemTableMods &&
|
|
|
|
IsSystemClass((Form_pg_class) GETSTRUCT(tuple)) &&
|
2002-03-22 00:27:25 +01:00
|
|
|
!usecatupd)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-06-05 21:34:56 +02:00
|
|
|
#ifdef ACLDEBUG
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(DEBUG2, "permission denied for system catalog update");
|
2001-06-05 21:34:56 +02:00
|
|
|
#endif
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(tuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
return ACLCHECK_NO_PRIV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise, superusers bypass all permission-checking.
|
|
|
|
*/
|
2002-03-22 00:27:25 +01:00
|
|
|
if (usesuper)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-05-27 11:59:30 +02:00
|
|
|
#ifdef ACLDEBUG
|
2003-07-21 03:59:11 +02:00
|
|
|
elog(DEBUG2, "%u is superuser, home free", userid);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(tuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
return ACLCHECK_OK;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-10-02 06:49:28 +02:00
|
|
|
/*
|
|
|
|
* Normal case: get the relation's ACL from pg_class
|
|
|
|
*/
|
2002-03-22 00:27:25 +01:00
|
|
|
aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
|
2000-10-02 06:49:28 +02:00
|
|
|
&isNull);
|
|
|
|
if (isNull)
|
|
|
|
{
|
|
|
|
/* No ACL, so build default ACL for rel */
|
2000-08-01 00:39:17 +02:00
|
|
|
AclId ownerId;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-08-01 00:39:17 +02:00
|
|
|
ownerId = ((Form_pg_class) GETSTRUCT(tuple))->relowner;
|
2002-04-21 02:26:44 +02:00
|
|
|
acl = acldefault(ACL_OBJECT_RELATION, ownerId);
|
2001-06-05 21:34:56 +02:00
|
|
|
aclDatum = (Datum) 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-10-02 06:49:28 +02:00
|
|
|
else
|
1999-09-18 21:08:25 +02:00
|
|
|
{
|
2001-06-05 21:34:56 +02:00
|
|
|
/* detoast rel's ACL if necessary */
|
|
|
|
acl = DatumGetAclP(aclDatum);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2000-10-02 06:49:28 +02:00
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
result = aclcheck(acl, userid, mode);
|
2000-11-16 23:30:52 +01:00
|
|
|
|
2001-06-05 21:34:56 +02:00
|
|
|
/* if we have a detoasted copy, free it */
|
|
|
|
if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
|
1997-09-07 07:04:48 +02:00
|
|
|
pfree(acl);
|
2001-06-05 21:34:56 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return result;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
/*
|
|
|
|
* Exported routine for checking a user's access privileges to a database
|
|
|
|
*/
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult
|
2002-12-04 06:18:38 +01:00
|
|
|
pg_database_aclcheck(Oid db_oid, AclId userid, AclMode mode)
|
2002-04-21 02:26:44 +02:00
|
|
|
{
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult result;
|
2002-04-21 02:26:44 +02:00
|
|
|
Relation pg_database;
|
|
|
|
ScanKeyData entry[1];
|
|
|
|
HeapScanDesc scan;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Datum aclDatum;
|
|
|
|
bool isNull;
|
|
|
|
Acl *acl;
|
|
|
|
|
|
|
|
/* Superusers bypass all permission checking. */
|
|
|
|
if (superuser_arg(userid))
|
|
|
|
return ACLCHECK_OK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the database's ACL from pg_database
|
|
|
|
*
|
|
|
|
* There's no syscache for pg_database, so must look the hard way
|
|
|
|
*/
|
|
|
|
pg_database = heap_openr(DatabaseRelationName, AccessShareLock);
|
|
|
|
ScanKeyEntryInitialize(&entry[0], 0x0,
|
|
|
|
ObjectIdAttributeNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(db_oid));
|
2002-05-21 01:51:44 +02:00
|
|
|
scan = heap_beginscan(pg_database, SnapshotNow, 1, entry);
|
|
|
|
tuple = heap_getnext(scan, ForwardScanDirection);
|
2002-04-21 02:26:44 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
|
|
|
errmsg("database with OID %u does not exist", db_oid)));
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
aclDatum = heap_getattr(tuple, Anum_pg_database_datacl,
|
|
|
|
RelationGetDescr(pg_database), &isNull);
|
|
|
|
|
|
|
|
if (isNull)
|
|
|
|
{
|
|
|
|
/* No ACL, so build default ACL */
|
|
|
|
AclId ownerId;
|
|
|
|
|
|
|
|
ownerId = ((Form_pg_database) GETSTRUCT(tuple))->datdba;
|
|
|
|
acl = acldefault(ACL_OBJECT_DATABASE, ownerId);
|
|
|
|
aclDatum = (Datum) 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* detoast ACL if necessary */
|
|
|
|
acl = DatumGetAclP(aclDatum);
|
|
|
|
}
|
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
result = aclcheck(acl, userid, mode);
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
/* if we have a detoasted copy, free it */
|
|
|
|
if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
|
|
|
|
pfree(acl);
|
|
|
|
|
|
|
|
heap_endscan(scan);
|
|
|
|
heap_close(pg_database, AccessShareLock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
/*
|
|
|
|
* Exported routine for checking a user's access privileges to a function
|
|
|
|
*/
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult
|
2002-12-04 06:18:38 +01:00
|
|
|
pg_proc_aclcheck(Oid proc_oid, AclId userid, AclMode mode)
|
2002-02-19 00:11:58 +01:00
|
|
|
{
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult result;
|
2002-02-19 00:11:58 +01:00
|
|
|
HeapTuple tuple;
|
|
|
|
Datum aclDatum;
|
|
|
|
bool isNull;
|
|
|
|
Acl *acl;
|
|
|
|
|
2002-03-22 00:27:25 +01:00
|
|
|
/* Superusers bypass all permission checking. */
|
2002-02-19 00:11:58 +01:00
|
|
|
if (superuser_arg(userid))
|
|
|
|
return ACLCHECK_OK;
|
|
|
|
|
|
|
|
/*
|
2002-03-22 00:27:25 +01:00
|
|
|
* Get the function's ACL from pg_proc
|
2002-02-19 00:11:58 +01:00
|
|
|
*/
|
|
|
|
tuple = SearchSysCache(PROCOID,
|
|
|
|
ObjectIdGetDatum(proc_oid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("function with OID %u does not exist", proc_oid)));
|
2002-02-19 00:11:58 +01:00
|
|
|
|
|
|
|
aclDatum = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_proacl,
|
|
|
|
&isNull);
|
|
|
|
if (isNull)
|
|
|
|
{
|
|
|
|
/* No ACL, so build default ACL */
|
|
|
|
AclId ownerId;
|
|
|
|
|
|
|
|
ownerId = ((Form_pg_proc) GETSTRUCT(tuple))->proowner;
|
2002-04-21 02:26:44 +02:00
|
|
|
acl = acldefault(ACL_OBJECT_FUNCTION, ownerId);
|
2002-02-19 00:11:58 +01:00
|
|
|
aclDatum = (Datum) 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* detoast ACL if necessary */
|
|
|
|
acl = DatumGetAclP(aclDatum);
|
|
|
|
}
|
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
result = aclcheck(acl, userid, mode);
|
2002-02-19 00:11:58 +01:00
|
|
|
|
|
|
|
/* if we have a detoasted copy, free it */
|
|
|
|
if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
|
|
|
|
pfree(acl);
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exported routine for checking a user's access privileges to a language
|
|
|
|
*/
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult
|
2002-12-04 06:18:38 +01:00
|
|
|
pg_language_aclcheck(Oid lang_oid, AclId userid, AclMode mode)
|
2002-02-19 00:11:58 +01:00
|
|
|
{
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult result;
|
2002-02-19 00:11:58 +01:00
|
|
|
HeapTuple tuple;
|
|
|
|
Datum aclDatum;
|
|
|
|
bool isNull;
|
|
|
|
Acl *acl;
|
|
|
|
|
2002-03-22 00:27:25 +01:00
|
|
|
/* Superusers bypass all permission checking. */
|
2002-02-19 00:11:58 +01:00
|
|
|
if (superuser_arg(userid))
|
|
|
|
return ACLCHECK_OK;
|
|
|
|
|
|
|
|
/*
|
2003-07-21 03:59:11 +02:00
|
|
|
* Get the language's ACL from pg_language
|
2002-02-19 00:11:58 +01:00
|
|
|
*/
|
|
|
|
tuple = SearchSysCache(LANGOID,
|
|
|
|
ObjectIdGetDatum(lang_oid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("language with OID %u does not exist", lang_oid)));
|
2002-02-19 00:11:58 +01:00
|
|
|
|
|
|
|
aclDatum = SysCacheGetAttr(LANGOID, tuple, Anum_pg_language_lanacl,
|
|
|
|
&isNull);
|
|
|
|
if (isNull)
|
|
|
|
{
|
|
|
|
/* No ACL, so build default ACL */
|
2002-04-21 02:26:44 +02:00
|
|
|
acl = acldefault(ACL_OBJECT_LANGUAGE, InvalidOid);
|
2002-02-19 00:11:58 +01:00
|
|
|
aclDatum = (Datum) 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* detoast ACL if necessary */
|
|
|
|
acl = DatumGetAclP(aclDatum);
|
|
|
|
}
|
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
result = aclcheck(acl, userid, mode);
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
/* if we have a detoasted copy, free it */
|
|
|
|
if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
|
|
|
|
pfree(acl);
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exported routine for checking a user's access privileges to a namespace
|
|
|
|
*/
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult
|
2002-12-04 06:18:38 +01:00
|
|
|
pg_namespace_aclcheck(Oid nsp_oid, AclId userid, AclMode mode)
|
2002-04-21 02:26:44 +02:00
|
|
|
{
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult result;
|
2002-04-21 02:26:44 +02:00
|
|
|
HeapTuple tuple;
|
|
|
|
Datum aclDatum;
|
|
|
|
bool isNull;
|
|
|
|
Acl *acl;
|
|
|
|
|
2002-08-07 23:45:02 +02:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* If we have been assigned this namespace as a temp namespace, assume
|
|
|
|
* we have all grantable privileges on it.
|
2002-08-07 23:45:02 +02:00
|
|
|
*/
|
|
|
|
if (isTempNamespace(nsp_oid))
|
|
|
|
return ACLCHECK_OK;
|
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
/* Superusers bypass all permission checking. */
|
|
|
|
if (superuser_arg(userid))
|
|
|
|
return ACLCHECK_OK;
|
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
/*
|
2003-07-21 03:59:11 +02:00
|
|
|
* Get the schema's ACL from pg_namespace
|
2002-02-19 00:11:58 +01:00
|
|
|
*/
|
2002-04-21 02:26:44 +02:00
|
|
|
tuple = SearchSysCache(NAMESPACEOID,
|
|
|
|
ObjectIdGetDatum(nsp_oid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_SCHEMA),
|
|
|
|
errmsg("schema with OID %u does not exist", nsp_oid)));
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
aclDatum = SysCacheGetAttr(NAMESPACEOID, tuple, Anum_pg_namespace_nspacl,
|
|
|
|
&isNull);
|
|
|
|
if (isNull)
|
|
|
|
{
|
|
|
|
/* No ACL, so build default ACL */
|
|
|
|
AclId ownerId;
|
|
|
|
|
|
|
|
ownerId = ((Form_pg_namespace) GETSTRUCT(tuple))->nspowner;
|
|
|
|
acl = acldefault(ACL_OBJECT_NAMESPACE, ownerId);
|
|
|
|
aclDatum = (Datum) 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* detoast ACL if necessary */
|
|
|
|
acl = DatumGetAclP(aclDatum);
|
|
|
|
}
|
|
|
|
|
2003-01-24 00:39:07 +01:00
|
|
|
result = aclcheck(acl, userid, mode);
|
2002-02-19 00:11:58 +01:00
|
|
|
|
|
|
|
/* if we have a detoasted copy, free it */
|
|
|
|
if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
|
|
|
|
pfree(acl);
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2002-03-22 00:27:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ownership check for a relation (specified by OID).
|
|
|
|
*/
|
|
|
|
bool
|
2002-12-04 06:18:38 +01:00
|
|
|
pg_class_ownercheck(Oid class_oid, AclId userid)
|
2002-03-22 00:27:25 +01:00
|
|
|
{
|
|
|
|
HeapTuple tuple;
|
|
|
|
AclId owner_id;
|
|
|
|
|
|
|
|
/* Superusers bypass all permission checking. */
|
|
|
|
if (superuser_arg(userid))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
tuple = SearchSysCache(RELOID,
|
|
|
|
ObjectIdGetDatum(class_oid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_TABLE),
|
|
|
|
errmsg("relation with OID %u does not exist", class_oid)));
|
2002-03-22 00:27:25 +01:00
|
|
|
|
|
|
|
owner_id = ((Form_pg_class) GETSTRUCT(tuple))->relowner;
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
return userid == owner_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ownership check for a type (specified by OID).
|
|
|
|
*/
|
|
|
|
bool
|
2002-12-04 06:18:38 +01:00
|
|
|
pg_type_ownercheck(Oid type_oid, AclId userid)
|
2002-03-22 00:27:25 +01:00
|
|
|
{
|
|
|
|
HeapTuple tuple;
|
|
|
|
AclId owner_id;
|
|
|
|
|
|
|
|
/* Superusers bypass all permission checking. */
|
|
|
|
if (superuser_arg(userid))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
tuple = SearchSysCache(TYPEOID,
|
|
|
|
ObjectIdGetDatum(type_oid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("type with OID %u does not exist", type_oid)));
|
2002-03-22 00:27:25 +01:00
|
|
|
|
|
|
|
owner_id = ((Form_pg_type) GETSTRUCT(tuple))->typowner;
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
return userid == owner_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ownership check for an operator (specified by OID).
|
|
|
|
*/
|
|
|
|
bool
|
2002-12-04 06:18:38 +01:00
|
|
|
pg_oper_ownercheck(Oid oper_oid, AclId userid)
|
2002-03-22 00:27:25 +01:00
|
|
|
{
|
|
|
|
HeapTuple tuple;
|
|
|
|
AclId owner_id;
|
|
|
|
|
|
|
|
/* Superusers bypass all permission checking. */
|
|
|
|
if (superuser_arg(userid))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
tuple = SearchSysCache(OPEROID,
|
|
|
|
ObjectIdGetDatum(oper_oid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("operator with OID %u does not exist", oper_oid)));
|
2002-03-22 00:27:25 +01:00
|
|
|
|
|
|
|
owner_id = ((Form_pg_operator) GETSTRUCT(tuple))->oprowner;
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
return userid == owner_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ownership check for a function (specified by OID).
|
|
|
|
*/
|
|
|
|
bool
|
2002-12-04 06:18:38 +01:00
|
|
|
pg_proc_ownercheck(Oid proc_oid, AclId userid)
|
2002-03-22 00:27:25 +01:00
|
|
|
{
|
|
|
|
HeapTuple tuple;
|
|
|
|
AclId owner_id;
|
|
|
|
|
|
|
|
/* Superusers bypass all permission checking. */
|
|
|
|
if (superuser_arg(userid))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
tuple = SearchSysCache(PROCOID,
|
|
|
|
ObjectIdGetDatum(proc_oid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("function with OID %u does not exist", proc_oid)));
|
2002-03-22 00:27:25 +01:00
|
|
|
|
|
|
|
owner_id = ((Form_pg_proc) GETSTRUCT(tuple))->proowner;
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
return userid == owner_id;
|
|
|
|
}
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ownership check for a namespace (specified by OID).
|
|
|
|
*/
|
|
|
|
bool
|
2002-12-04 06:18:38 +01:00
|
|
|
pg_namespace_ownercheck(Oid nsp_oid, AclId userid)
|
2002-04-21 02:26:44 +02:00
|
|
|
{
|
|
|
|
HeapTuple tuple;
|
|
|
|
AclId owner_id;
|
|
|
|
|
|
|
|
/* Superusers bypass all permission checking. */
|
|
|
|
if (superuser_arg(userid))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
tuple = SearchSysCache(NAMESPACEOID,
|
|
|
|
ObjectIdGetDatum(nsp_oid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_SCHEMA),
|
|
|
|
errmsg("schema with OID %u does not exist", nsp_oid)));
|
2002-04-21 02:26:44 +02:00
|
|
|
|
|
|
|
owner_id = ((Form_pg_namespace) GETSTRUCT(tuple))->nspowner;
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
return userid == owner_id;
|
|
|
|
}
|
2002-07-30 00:14:11 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ownership check for an operator class (specified by OID).
|
|
|
|
*/
|
|
|
|
bool
|
2002-12-04 06:18:38 +01:00
|
|
|
pg_opclass_ownercheck(Oid opc_oid, AclId userid)
|
2002-07-30 00:14:11 +02:00
|
|
|
{
|
|
|
|
HeapTuple tuple;
|
|
|
|
AclId owner_id;
|
|
|
|
|
|
|
|
/* Superusers bypass all permission checking. */
|
|
|
|
if (superuser_arg(userid))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
tuple = SearchSysCache(CLAOID,
|
|
|
|
ObjectIdGetDatum(opc_oid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("operator class with OID %u does not exist",
|
|
|
|
opc_oid)));
|
2002-07-30 00:14:11 +02:00
|
|
|
|
|
|
|
owner_id = ((Form_pg_opclass) GETSTRUCT(tuple))->opcowner;
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
return userid == owner_id;
|
|
|
|
}
|
2003-06-27 16:45:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ownership check for database (specified as OID)
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
pg_database_ownercheck(Oid db_oid, AclId userid)
|
|
|
|
{
|
|
|
|
Relation pg_database;
|
|
|
|
ScanKeyData entry[1];
|
|
|
|
HeapScanDesc scan;
|
|
|
|
HeapTuple dbtuple;
|
|
|
|
int32 dba;
|
|
|
|
|
|
|
|
/* Superusers bypass all permission checking. */
|
|
|
|
if (superuser_arg(userid))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* There's no syscache for pg_database, so must look the hard way */
|
|
|
|
pg_database = heap_openr(DatabaseRelationName, AccessShareLock);
|
|
|
|
ScanKeyEntryInitialize(&entry[0], 0x0,
|
|
|
|
ObjectIdAttributeNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(db_oid));
|
|
|
|
scan = heap_beginscan(pg_database, SnapshotNow, 1, entry);
|
|
|
|
|
|
|
|
dbtuple = heap_getnext(scan, ForwardScanDirection);
|
|
|
|
|
|
|
|
if (!HeapTupleIsValid(dbtuple))
|
2003-07-21 03:59:11 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
|
|
|
errmsg("database with OID %u does not exist", db_oid)));
|
2003-06-27 16:45:32 +02:00
|
|
|
|
|
|
|
dba = ((Form_pg_database) GETSTRUCT(dbtuple))->datdba;
|
|
|
|
|
|
|
|
heap_endscan(scan);
|
|
|
|
heap_close(pg_database, AccessShareLock);
|
|
|
|
|
|
|
|
return userid == dba;
|
|
|
|
}
|