1996-11-02 03:06:47 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* superuser.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* The superuser() function. Determines if user has superuser privilege.
|
2001-06-13 23:44:41 +02:00
|
|
|
* Also, a function to check for the owner (datdba) of a database.
|
|
|
|
*
|
1996-11-02 03:06:47 +01:00
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-11-02 03:06:47 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2002-05-21 01:51:44 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/misc/superuser.c,v 1.22 2002/05/20 23:51:43 tgl Exp $
|
1996-11-02 03:06:47 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "postgres.h"
|
2001-06-13 23:44:41 +02:00
|
|
|
|
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "catalog/catname.h"
|
|
|
|
#include "catalog/pg_database.h"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "catalog/pg_shadow.h"
|
1999-07-16 07:23:30 +02:00
|
|
|
#include "utils/syscache.h"
|
2000-01-14 23:11:38 +01:00
|
|
|
#include "miscadmin.h"
|
2001-06-13 23:44:41 +02:00
|
|
|
#include "utils/fmgroids.h"
|
1996-11-02 03:06:47 +01:00
|
|
|
|
2001-06-13 23:44:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The Postgres user running this command has Postgres superuser privileges
|
2002-04-11 07:32:03 +02:00
|
|
|
*
|
|
|
|
* All code should use either of these two functions to find out
|
|
|
|
* whether a given user is a superuser, rather than evaluating
|
|
|
|
* pg_shadow.usesuper directly, so that the escape hatch built in for
|
|
|
|
* the single-user case works.
|
2001-06-13 23:44:41 +02:00
|
|
|
*/
|
1996-11-02 03:06:47 +01:00
|
|
|
bool
|
1997-09-07 07:04:48 +02:00
|
|
|
superuser(void)
|
2002-02-19 00:11:58 +01:00
|
|
|
{
|
|
|
|
return superuser_arg(GetUserId());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
superuser_arg(Oid userid)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-06-13 23:44:41 +02:00
|
|
|
bool result = false;
|
1997-09-08 04:41:22 +02:00
|
|
|
HeapTuple utup;
|
1996-11-02 03:06:47 +01:00
|
|
|
|
2001-09-08 17:24:00 +02:00
|
|
|
/* Special escape path in case you deleted all your users. */
|
2002-02-19 00:11:58 +01:00
|
|
|
if (!IsUnderPostmaster && userid == BOOTSTRAP_USESYSID)
|
2001-09-08 17:24:00 +02:00
|
|
|
return true;
|
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
utup = SearchSysCache(SHADOWSYSID,
|
2002-02-19 00:11:58 +01:00
|
|
|
ObjectIdGetDatum(userid),
|
2000-11-16 23:30:52 +01:00
|
|
|
0, 0, 0);
|
|
|
|
if (HeapTupleIsValid(utup))
|
|
|
|
{
|
|
|
|
result = ((Form_pg_shadow) GETSTRUCT(utup))->usesuper;
|
|
|
|
ReleaseSysCache(utup);
|
|
|
|
}
|
2001-06-13 23:44:41 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-02-19 00:11:58 +01:00
|
|
|
|
2001-06-13 23:44:41 +02:00
|
|
|
/*
|
|
|
|
* The Postgres user running this command is the owner of the specified
|
|
|
|
* database.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
is_dbadmin(Oid dbid)
|
|
|
|
{
|
|
|
|
Relation pg_database;
|
|
|
|
ScanKeyData entry[1];
|
|
|
|
HeapScanDesc scan;
|
|
|
|
HeapTuple dbtuple;
|
|
|
|
int32 dba;
|
|
|
|
|
|
|
|
/* 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(dbid));
|
2002-05-21 01:51:44 +02:00
|
|
|
scan = heap_beginscan(pg_database, SnapshotNow, 1, entry);
|
|
|
|
dbtuple = heap_getnext(scan, ForwardScanDirection);
|
2001-06-13 23:44:41 +02:00
|
|
|
if (!HeapTupleIsValid(dbtuple))
|
|
|
|
elog(ERROR, "database %u does not exist", dbid);
|
|
|
|
dba = ((Form_pg_database) GETSTRUCT(dbtuple))->datdba;
|
|
|
|
heap_endscan(scan);
|
|
|
|
heap_close(pg_database, AccessShareLock);
|
|
|
|
|
|
|
|
/* XXX some confusion about whether userids are OID or int4 ... */
|
|
|
|
return (GetUserId() == (Oid) dba);
|
1996-11-02 03:06:47 +01:00
|
|
|
}
|