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
|
|
|
*
|
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-11-02 03:06:47 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2002-08-09 18:45:16 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/misc/superuser.c,v 1.24 2002/08/09 16:45:14 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"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "catalog/pg_shadow.h"
|
2002-08-09 18:45:16 +02:00
|
|
|
#include "commands/dbcommands.h"
|
1999-07-16 07:23:30 +02:00
|
|
|
#include "utils/syscache.h"
|
2000-01-14 23:11:38 +01:00
|
|
|
#include "miscadmin.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)
|
|
|
|
{
|
2002-08-09 18:45:16 +02:00
|
|
|
Oid dba;
|
2001-06-13 23:44:41 +02:00
|
|
|
|
2002-08-09 18:45:16 +02:00
|
|
|
dba = get_database_owner(dbid);
|
2001-06-13 23:44:41 +02:00
|
|
|
|
2002-08-09 18:45:16 +02:00
|
|
|
return (GetUserId() == dba);
|
1996-11-02 03:06:47 +01:00
|
|
|
}
|