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
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-01-18 00:57:48 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.36 2000/01/17 23:57:43 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"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "catalog/pg_aggregate.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "catalog/pg_group.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_agg.h"
|
|
|
|
#include "parser/parse_func.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/acl.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
1998-02-24 04:31:50 +01:00
|
|
|
static int32 aclcheck(char *relname, Acl *acl, AclId id, AclIdType idtype, AclMode mode);
|
1997-08-19 23:40:56 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
|
|
|
* Enable use of user relations in place of real system catalogs.
|
|
|
|
*/
|
|
|
|
/*#define ACLDEBUG*/
|
|
|
|
|
|
|
|
#ifdef ACLDEBUG
|
|
|
|
/*
|
|
|
|
* Fool the code below into thinking that "pgacls" is pg_class.
|
|
|
|
* relname and relowner are in the same place, happily.
|
|
|
|
*/
|
|
|
|
#undef Anum_pg_class_relacl
|
1997-09-07 07:04:48 +02:00
|
|
|
#define Anum_pg_class_relacl 3
|
1996-07-09 08:22:35 +02:00
|
|
|
#undef Natts_pg_class
|
1997-09-07 07:04:48 +02:00
|
|
|
#define Natts_pg_class 3
|
1996-07-09 08:22:35 +02:00
|
|
|
#undef Name_pg_class
|
1997-09-07 07:04:48 +02:00
|
|
|
#define Name_pg_class "pgacls"
|
1996-07-09 08:22:35 +02:00
|
|
|
#undef Name_pg_group
|
1997-09-07 07:04:48 +02:00
|
|
|
#define Name_pg_group "pggroup"
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
|
|
|
|
1997-03-12 21:48:48 +01:00
|
|
|
/* warning messages, now more explicit. */
|
|
|
|
/* should correspond to the order of the ACLCHK_* result codes above. */
|
1997-09-08 04:41:22 +02:00
|
|
|
char *aclcheck_error_strings[] = {
|
1997-09-07 07:04:48 +02:00
|
|
|
"No error.",
|
|
|
|
"Permission denied.",
|
|
|
|
"Table does not exist.",
|
|
|
|
"Must be table owner."
|
1997-03-12 21:48:48 +01:00
|
|
|
};
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef ACLDEBUG_TRACE
|
|
|
|
static
|
1997-09-08 22:59:27 +02:00
|
|
|
dumpacl(Acl *acl)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
unsigned i;
|
1997-09-08 04:41:22 +02:00
|
|
|
AclItem *aip;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
elog(DEBUG, "acl size = %d, # acls = %d",
|
|
|
|
ACL_SIZE(acl), ACL_NUM(acl));
|
|
|
|
aip = (AclItem *) ACL_DAT(acl);
|
|
|
|
for (i = 0; i < ACL_NUM(acl); ++i)
|
|
|
|
elog(DEBUG, " acl[%d]: %s", i, aclitemout(aip + i));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
1997-09-07 07:04:48 +02:00
|
|
|
ChangeAcl(char *relname,
|
1997-09-08 22:59:27 +02:00
|
|
|
AclItem *mod_aip,
|
1997-09-07 07:04:48 +02:00
|
|
|
unsigned modechg)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
unsigned i;
|
1997-09-08 04:41:22 +02:00
|
|
|
Acl *old_acl = (Acl *) NULL,
|
|
|
|
*new_acl;
|
|
|
|
Relation relation;
|
1998-08-19 04:04:17 +02:00
|
|
|
HeapTuple tuple;
|
1997-09-08 04:41:22 +02:00
|
|
|
Datum values[Natts_pg_class];
|
|
|
|
char nulls[Natts_pg_class];
|
|
|
|
char replaces[Natts_pg_class];
|
|
|
|
Relation idescs[Num_pg_class_indices];
|
|
|
|
int free_old_acl = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the pg_class tuple matching 'relname' and extract the ACL. If
|
|
|
|
* there's no ACL, create a default using the pg_class.relowner field.
|
|
|
|
*
|
1999-11-24 01:44:37 +01:00
|
|
|
* We can't use the syscache here, since we need to do a heap_update on
|
1998-08-19 04:04:17 +02:00
|
|
|
* the tuple we find.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
relation = heap_openr(RelationRelationName, RowExclusiveLock);
|
1998-08-19 04:04:17 +02:00
|
|
|
tuple = SearchSysCacheTuple(RELNAME,
|
|
|
|
PointerGetDatum(relname),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(relation, RowExclusiveLock);
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "ChangeAcl: class \"%s\" not found",
|
1997-09-07 07:04:48 +02:00
|
|
|
relname);
|
|
|
|
}
|
1998-08-19 04:04:17 +02:00
|
|
|
|
|
|
|
if (!heap_attisnull(tuple, Anum_pg_class_relacl))
|
|
|
|
old_acl = (Acl *) heap_getattr(tuple,
|
1997-09-07 07:04:48 +02:00
|
|
|
Anum_pg_class_relacl,
|
1998-09-01 05:29:17 +02:00
|
|
|
RelationGetDescr(relation),
|
1997-09-07 07:04:48 +02:00
|
|
|
(bool *) NULL);
|
|
|
|
if (!old_acl || ACL_NUM(old_acl) < 1)
|
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef ACLDEBUG_TRACE
|
1997-09-07 07:04:48 +02:00
|
|
|
elog(DEBUG, "ChangeAcl: using default ACL");
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1998-08-19 04:04:17 +02:00
|
|
|
/* old_acl = acldefault(((Form_pg_class) GETSTRUCT(tuple))->relowner); */
|
1998-02-24 04:31:50 +01:00
|
|
|
old_acl = acldefault(relname);
|
1997-09-07 07:04:48 +02:00
|
|
|
free_old_acl = 1;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
#ifdef ACLDEBUG_TRACE
|
1997-09-07 07:04:48 +02:00
|
|
|
dumpacl(old_acl);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
new_acl = aclinsert3(old_acl, mod_aip, modechg);
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef ACLDEBUG_TRACE
|
1997-09-07 07:04:48 +02:00
|
|
|
dumpacl(new_acl);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
for (i = 0; i < Natts_pg_class; ++i)
|
|
|
|
{
|
|
|
|
replaces[i] = ' ';
|
|
|
|
nulls[i] = ' '; /* ignored if replaces[i] == ' ' anyway */
|
|
|
|
values[i] = (Datum) NULL; /* ignored if replaces[i] == ' '
|
|
|
|
* anyway */
|
|
|
|
}
|
|
|
|
replaces[Anum_pg_class_relacl - 1] = 'r';
|
|
|
|
values[Anum_pg_class_relacl - 1] = (Datum) new_acl;
|
1998-08-19 04:04:17 +02:00
|
|
|
tuple = heap_modifytuple(tuple, relation, values, nulls, replaces);
|
2000-01-18 00:57:48 +01:00
|
|
|
|
1999-11-24 01:44:37 +01:00
|
|
|
heap_update(relation, &tuple->t_self, tuple, NULL);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* keep the catalog indices up to date */
|
|
|
|
CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices,
|
|
|
|
idescs);
|
1998-08-19 04:04:17 +02:00
|
|
|
CatalogIndexInsert(idescs, Num_pg_class_indices, relation, tuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
CatalogCloseIndices(Num_pg_class_indices, idescs);
|
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(relation, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (free_old_acl)
|
|
|
|
pfree(old_acl);
|
|
|
|
pfree(new_acl);
|
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
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
tuple = SearchSysCacheTuple(GRONAME,
|
|
|
|
PointerGetDatum(groname),
|
1998-09-01 06:40:42 +02:00
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (HeapTupleIsValid(tuple))
|
|
|
|
id = ((Form_pg_group) GETSTRUCT(tuple))->grosysid;
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "non-existent group \"%s\"", groname);
|
1998-09-01 05:29:17 +02:00
|
|
|
return id;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
tuple = SearchSysCacheTuple(GROSYSID,
|
|
|
|
ObjectIdGetDatum(grosysid),
|
1998-09-01 06:40:42 +02:00
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (HeapTupleIsValid(tuple))
|
1999-11-08 00:08:36 +01:00
|
|
|
name = NameStr(((Form_pg_group) GETSTRUCT(tuple))->groname);
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
|
|
|
elog(NOTICE, "get_groname: group %d not found", grosysid);
|
1998-09-01 05:29:17 +02:00
|
|
|
return name;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
static int32
|
1996-07-09 08:22:35 +02:00
|
|
|
in_group(AclId uid, AclId gid)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation relation;
|
1998-08-19 04:04:17 +02:00
|
|
|
HeapTuple tuple;
|
1997-09-08 04:41:22 +02:00
|
|
|
Acl *tmp;
|
|
|
|
unsigned i,
|
|
|
|
num;
|
|
|
|
AclId *aidp;
|
|
|
|
int32 found = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
relation = heap_openr(GroupRelationName, RowExclusiveLock);
|
1998-08-19 04:04:17 +02:00
|
|
|
tuple = SearchSysCacheTuple(GROSYSID,
|
|
|
|
ObjectIdGetDatum(gid),
|
1998-09-01 06:40:42 +02:00
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (HeapTupleIsValid(tuple) &&
|
|
|
|
!heap_attisnull(tuple, Anum_pg_group_grolist))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-08-19 04:04:17 +02:00
|
|
|
tmp = (IdList *) heap_getattr(tuple,
|
1997-09-07 07:04:48 +02:00
|
|
|
Anum_pg_group_grolist,
|
1998-09-01 06:40:42 +02:00
|
|
|
RelationGetDescr(relation),
|
1997-09-07 07:04:48 +02:00
|
|
|
(bool *) NULL);
|
1996-07-09 08:22:35 +02:00
|
|
|
/* XXX make me a function */
|
|
|
|
num = IDLIST_NUM(tmp);
|
|
|
|
aidp = IDLIST_DAT(tmp);
|
|
|
|
for (i = 0; i < num; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
if (aidp[i] == uid)
|
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
1996-07-09 08:22:35 +02:00
|
|
|
elog(NOTICE, "in_group: group %d not found", gid);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(relation, RowExclusiveLock);
|
1998-09-01 05:29:17 +02:00
|
|
|
return found;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* aclcheck
|
|
|
|
* Returns 1 if the 'id' of type 'idtype' has ACL entries in 'acl' to satisfy
|
|
|
|
* any one of the requirements of 'mode'. Returns 0 otherwise.
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static int32
|
1998-02-24 04:31:50 +01:00
|
|
|
aclcheck(char *relname, Acl *acl, AclId id, AclIdType idtype, AclMode mode)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
unsigned i;
|
|
|
|
AclItem *aip,
|
1997-09-08 04:41:22 +02:00
|
|
|
*aidat;
|
|
|
|
unsigned num,
|
|
|
|
found_group;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* if no acl is found, use world default */
|
|
|
|
if (!acl)
|
1998-02-24 04:31:50 +01:00
|
|
|
acl = acldefault(relname);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
num = ACL_NUM(acl);
|
|
|
|
aidat = ACL_DAT(acl);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We'll treat the empty ACL like that, too, although this is more
|
|
|
|
* like an error (i.e., you manually blew away your ACL array) -- the
|
|
|
|
* system never creates an empty ACL.
|
|
|
|
*/
|
|
|
|
if (num < 1)
|
|
|
|
{
|
2000-01-15 03:59:43 +01:00
|
|
|
#if defined(ACLDEBUG_TRACE) || 1
|
1997-09-07 07:04:48 +02:00
|
|
|
elog(DEBUG, "aclcheck: zero-length ACL, returning 1");
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
return ACLCHECK_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (idtype)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case ACL_IDTYPE_UID:
|
|
|
|
for (i = 1, aip = aidat + 1; /* skip world entry */
|
|
|
|
i < num && aip->ai_idtype == ACL_IDTYPE_UID;
|
|
|
|
++i, ++aip)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
if (aip->ai_id == id)
|
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef ACLDEBUG_TRACE
|
1997-09-08 04:41:22 +02:00
|
|
|
elog(DEBUG, "aclcheck: found %d/%d",
|
|
|
|
aip->ai_id, aip->ai_mode);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1998-09-01 05:29:17 +02:00
|
|
|
return (aip->ai_mode & mode) ? ACLCHECK_OK : ACLCHECK_NO_PRIV;
|
1997-09-08 04:41:22 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 04:41:22 +02:00
|
|
|
for (found_group = 0;
|
|
|
|
i < num && aip->ai_idtype == ACL_IDTYPE_GID;
|
|
|
|
++i, ++aip)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
if (in_group(id, aip->ai_id))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
if (aip->ai_mode & mode)
|
|
|
|
{
|
|
|
|
found_group = 1;
|
|
|
|
break;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
1997-09-08 04:41:22 +02:00
|
|
|
if (found_group)
|
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef ACLDEBUG_TRACE
|
1997-09-08 04:41:22 +02:00
|
|
|
elog(DEBUG, "aclcheck: all groups ok");
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-08 04:41:22 +02:00
|
|
|
return ACLCHECK_OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ACL_IDTYPE_GID:
|
|
|
|
for (i = 1, aip = aidat + 1; /* skip world entry and
|
|
|
|
* UIDs */
|
|
|
|
i < num && aip->ai_idtype == ACL_IDTYPE_UID;
|
|
|
|
++i, ++aip)
|
|
|
|
;
|
|
|
|
for (;
|
|
|
|
i < num && aip->ai_idtype == ACL_IDTYPE_GID;
|
|
|
|
++i, ++aip)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
if (aip->ai_id == id)
|
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef ACLDEBUG_TRACE
|
1997-09-08 04:41:22 +02:00
|
|
|
elog(DEBUG, "aclcheck: found %d/%d",
|
|
|
|
aip->ai_id, aip->ai_mode);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1998-09-01 05:29:17 +02:00
|
|
|
return (aip->ai_mode & mode) ? ACLCHECK_OK : ACLCHECK_NO_PRIV;
|
1997-09-08 04:41:22 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case ACL_IDTYPE_WORLD:
|
|
|
|
break;
|
|
|
|
default:
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "aclcheck: bogus ACL id type: %d", idtype);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef ACLDEBUG_TRACE
|
1997-09-07 07:04:48 +02:00
|
|
|
elog(DEBUG, "aclcheck: using world=%d", aidat->ai_mode);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1998-09-01 05:29:17 +02:00
|
|
|
return (aidat->ai_mode & mode) ? ACLCHECK_OK : ACLCHECK_NO_PRIV;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
pg_aclcheck(char *relname, char *usename, AclMode mode)
|
|
|
|
{
|
1998-08-19 04:04:17 +02:00
|
|
|
HeapTuple tuple;
|
1997-09-08 04:41:22 +02:00
|
|
|
AclId id;
|
|
|
|
Acl *acl = (Acl *) NULL,
|
|
|
|
*tmp;
|
|
|
|
int32 result;
|
|
|
|
Relation relation;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-11-24 17:52:50 +01:00
|
|
|
tuple = SearchSysCacheTuple(SHADOWNAME,
|
1998-08-19 04:04:17 +02:00
|
|
|
PointerGetDatum(usename),
|
1998-09-01 06:40:42 +02:00
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "pg_aclcheck: user \"%s\" not found",
|
1997-09-07 07:04:48 +02:00
|
|
|
usename);
|
1998-08-19 04:04:17 +02:00
|
|
|
id = (AclId) ((Form_pg_shadow) GETSTRUCT(tuple))->usesysid;
|
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.)
|
|
|
|
*/
|
|
|
|
if (((mode & ACL_WR) || (mode & ACL_AP)) &&
|
1999-03-17 23:53:31 +01:00
|
|
|
!allowSystemTableMods && IsSystemRelationName(relname) &&
|
1999-07-30 20:09:49 +02:00
|
|
|
strncmp(relname,"pg_temp.", strlen("pg_temp.")) != 0 &&
|
1998-08-19 04:04:17 +02:00
|
|
|
!((Form_pg_shadow) GETSTRUCT(tuple))->usecatupd)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
elog(DEBUG, "pg_aclcheck: catalog update to \"%s\": permission denied",
|
|
|
|
relname);
|
|
|
|
return ACLCHECK_NO_PRIV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise, superusers bypass all permission-checking.
|
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
if (((Form_pg_shadow) GETSTRUCT(tuple))->usesuper)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef ACLDEBUG_TRACE
|
1997-09-07 07:04:48 +02:00
|
|
|
elog(DEBUG, "pg_aclcheck: \"%s\" is superuser",
|
|
|
|
usename);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
return ACLCHECK_OK;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#ifndef ACLDEBUG
|
1999-09-18 21:08:25 +02:00
|
|
|
relation = heap_openr(RelationRelationName, RowExclusiveLock);
|
1998-08-19 04:04:17 +02:00
|
|
|
tuple = SearchSysCacheTuple(RELNAME,
|
1998-09-01 06:40:42 +02:00
|
|
|
PointerGetDatum(relname),
|
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "pg_aclcheck: class \"%s\" not found",
|
1997-09-07 07:04:48 +02:00
|
|
|
relname);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!heap_attisnull(tuple, Anum_pg_class_relacl))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-08-19 04:04:17 +02:00
|
|
|
tmp = (Acl *) heap_getattr(tuple,
|
1997-09-07 07:04:48 +02:00
|
|
|
Anum_pg_class_relacl,
|
1998-09-01 05:29:17 +02:00
|
|
|
RelationGetDescr(relation),
|
1997-09-07 07:04:48 +02:00
|
|
|
(bool *) NULL);
|
|
|
|
acl = makeacl(ACL_NUM(tmp));
|
|
|
|
memmove((char *) acl, (char *) tmp, ACL_SIZE(tmp));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if the acl is null, by default the owner can do whatever he
|
|
|
|
* wants to with it
|
|
|
|
*/
|
1998-08-11 20:28:49 +02:00
|
|
|
int4 ownerId;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
ownerId = (int4) heap_getattr(tuple,
|
1998-09-01 06:40:42 +02:00
|
|
|
Anum_pg_class_relowner,
|
|
|
|
RelationGetDescr(relation),
|
|
|
|
(bool *) NULL);
|
|
|
|
acl = aclownerdefault(relname, (AclId) ownerId);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(relation, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
#else
|
1999-09-18 21:08:25 +02:00
|
|
|
relation = heap_openr(RelationRelationName, RowExclusiveLock);
|
|
|
|
tuple = SearchSysCacheTuple(RELNAME,
|
|
|
|
PointerGetDatum(relname),
|
|
|
|
0, 0, 0);
|
|
|
|
if (HeapTupleIsValid(tuple) &&
|
|
|
|
!heap_attisnull(tuple, Anum_pg_class_relacl))
|
|
|
|
{
|
|
|
|
tmp = (Acl *) heap_getattr(tuple,
|
|
|
|
Anum_pg_class_relacl,
|
|
|
|
RelationGetDescr(relation),
|
|
|
|
(bool *) NULL);
|
|
|
|
acl = makeacl(ACL_NUM(tmp));
|
|
|
|
memmove((char *) acl, (char *) tmp, ACL_SIZE(tmp));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(relation, RowExclusiveLock);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1998-02-24 04:31:50 +01:00
|
|
|
result = aclcheck(relname, acl, id, (AclIdType) ACL_IDTYPE_UID, mode);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (acl)
|
|
|
|
pfree(acl);
|
1998-09-01 05:29:17 +02:00
|
|
|
return result;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
2000-01-16 21:05:00 +01:00
|
|
|
pg_ownercheck(const char *usename,
|
|
|
|
const char *value,
|
1997-09-07 07:04:48 +02:00
|
|
|
int cacheid)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-08-19 04:04:17 +02:00
|
|
|
HeapTuple tuple;
|
1997-09-08 04:41:22 +02:00
|
|
|
AclId user_id,
|
|
|
|
owner_id = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-11-24 17:52:50 +01:00
|
|
|
tuple = SearchSysCacheTuple(SHADOWNAME,
|
1998-08-19 04:04:17 +02:00
|
|
|
PointerGetDatum(usename),
|
1998-09-01 06:40:42 +02:00
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "pg_ownercheck: user \"%s\" not found",
|
1997-09-07 07:04:48 +02:00
|
|
|
usename);
|
1998-08-19 04:04:17 +02:00
|
|
|
user_id = (AclId) ((Form_pg_shadow) GETSTRUCT(tuple))->usesysid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Superusers bypass all permission-checking.
|
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
if (((Form_pg_shadow) GETSTRUCT(tuple))->usesuper)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef ACLDEBUG_TRACE
|
1997-09-07 07:04:48 +02:00
|
|
|
elog(DEBUG, "pg_ownercheck: user \"%s\" is superuser",
|
|
|
|
usename);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1998-09-01 05:29:17 +02:00
|
|
|
return 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
tuple = SearchSysCacheTuple(cacheid, PointerGetDatum(value),
|
1998-09-01 06:40:42 +02:00
|
|
|
0, 0, 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
switch (cacheid)
|
|
|
|
{
|
1999-11-22 18:56:41 +01:00
|
|
|
case OPEROID:
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "pg_ownercheck: operator %ld not found",
|
1997-09-08 04:41:22 +02:00
|
|
|
PointerGetDatum(value));
|
1998-09-01 05:29:17 +02:00
|
|
|
owner_id = ((Form_pg_operator) GETSTRUCT(tuple))->oprowner;
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1999-11-22 18:56:41 +01:00
|
|
|
case PROCNAME:
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "pg_ownercheck: function \"%s\" not found",
|
1997-09-08 04:41:22 +02:00
|
|
|
value);
|
1998-08-19 04:04:17 +02:00
|
|
|
owner_id = ((Form_pg_proc) GETSTRUCT(tuple))->proowner;
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case RELNAME:
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "pg_ownercheck: class \"%s\" not found",
|
1997-09-08 04:41:22 +02:00
|
|
|
value);
|
1998-08-19 04:04:17 +02:00
|
|
|
owner_id = ((Form_pg_class) GETSTRUCT(tuple))->relowner;
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1999-11-22 18:56:41 +01:00
|
|
|
case TYPENAME:
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "pg_ownercheck: type \"%s\" not found",
|
1997-09-08 04:41:22 +02:00
|
|
|
value);
|
1998-09-01 05:29:17 +02:00
|
|
|
owner_id = ((Form_pg_type) GETSTRUCT(tuple))->typowner;
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
default:
|
1999-05-10 02:46:32 +02:00
|
|
|
elog(ERROR, "pg_ownercheck: invalid cache id: %d", cacheid);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return user_id == owner_id;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
1997-09-07 07:04:48 +02:00
|
|
|
pg_func_ownercheck(char *usename,
|
|
|
|
char *funcname,
|
|
|
|
int nargs,
|
1997-09-08 23:56:23 +02:00
|
|
|
Oid *arglist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-08-19 04:04:17 +02:00
|
|
|
HeapTuple tuple;
|
1997-09-08 04:41:22 +02:00
|
|
|
AclId user_id,
|
|
|
|
owner_id;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-11-24 17:52:50 +01:00
|
|
|
tuple = SearchSysCacheTuple(SHADOWNAME,
|
1998-08-19 04:04:17 +02:00
|
|
|
PointerGetDatum(usename),
|
1998-09-01 06:40:42 +02:00
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "pg_func_ownercheck: user \"%s\" not found",
|
1997-09-07 07:04:48 +02:00
|
|
|
usename);
|
1998-08-19 04:04:17 +02:00
|
|
|
user_id = (AclId) ((Form_pg_shadow) GETSTRUCT(tuple))->usesysid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Superusers bypass all permission-checking.
|
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
if (((Form_pg_shadow) GETSTRUCT(tuple))->usesuper)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef ACLDEBUG_TRACE
|
1997-09-07 07:04:48 +02:00
|
|
|
elog(DEBUG, "pg_ownercheck: user \"%s\" is superuser",
|
|
|
|
usename);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1998-09-01 05:29:17 +02:00
|
|
|
return 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1999-11-22 18:56:41 +01:00
|
|
|
tuple = SearchSysCacheTuple(PROCNAME,
|
1998-09-01 06:40:42 +02:00
|
|
|
PointerGetDatum(funcname),
|
|
|
|
Int32GetDatum(nargs),
|
|
|
|
PointerGetDatum(arglist),
|
|
|
|
0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
1998-05-10 01:43:45 +02:00
|
|
|
func_error("pg_func_ownercheck", funcname, nargs, arglist, NULL);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
owner_id = ((Form_pg_proc) GETSTRUCT(tuple))->proowner;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return user_id == owner_id;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-05-22 02:17:24 +02:00
|
|
|
|
|
|
|
int32
|
1997-09-07 07:04:48 +02:00
|
|
|
pg_aggr_ownercheck(char *usename,
|
|
|
|
char *aggname,
|
|
|
|
Oid basetypeID)
|
1997-05-22 02:17:24 +02:00
|
|
|
{
|
1998-08-19 04:04:17 +02:00
|
|
|
HeapTuple tuple;
|
1997-09-08 04:41:22 +02:00
|
|
|
AclId user_id,
|
|
|
|
owner_id;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-11-24 17:52:50 +01:00
|
|
|
tuple = SearchSysCacheTuple(SHADOWNAME,
|
1998-08-19 04:04:17 +02:00
|
|
|
PointerGetDatum(usename),
|
1998-09-01 06:40:42 +02:00
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "pg_aggr_ownercheck: user \"%s\" not found",
|
1997-09-07 07:04:48 +02:00
|
|
|
usename);
|
1998-08-19 04:04:17 +02:00
|
|
|
user_id = (AclId) ((Form_pg_shadow) GETSTRUCT(tuple))->usesysid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Superusers bypass all permission-checking.
|
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
if (((Form_pg_shadow) GETSTRUCT(tuple))->usesuper)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-05-22 02:17:24 +02:00
|
|
|
#ifdef ACLDEBUG_TRACE
|
1997-09-07 07:04:48 +02:00
|
|
|
elog(DEBUG, "pg_aggr_ownercheck: user \"%s\" is superuser",
|
|
|
|
usename);
|
1997-05-22 02:17:24 +02:00
|
|
|
#endif
|
1998-09-01 05:29:17 +02:00
|
|
|
return 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-05-22 02:17:24 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
tuple = SearchSysCacheTuple(AGGNAME,
|
1998-09-01 06:40:42 +02:00
|
|
|
PointerGetDatum(aggname),
|
|
|
|
ObjectIdGetDatum(basetypeID),
|
|
|
|
0, 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
1997-09-07 07:04:48 +02:00
|
|
|
agg_error("pg_aggr_ownercheck", aggname, basetypeID);
|
1997-05-22 02:17:24 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
owner_id = ((Form_pg_aggregate) GETSTRUCT(tuple))->aggowner;
|
1997-05-22 02:17:24 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return user_id == owner_id;
|
1997-05-22 02:17:24 +02:00
|
|
|
}
|