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.
|
2005-05-29 22:38:06 +02:00
|
|
|
*
|
|
|
|
* All code should use either of these two functions to find out
|
|
|
|
* whether a given user is a superuser, rather than examining
|
2005-08-15 04:40:36 +02:00
|
|
|
* pg_authid.rolsuper directly, so that the escape hatch built in for
|
2005-05-29 22:38:06 +02:00
|
|
|
* the single-user case works.
|
2001-06-13 23:44:41 +02:00
|
|
|
*
|
1996-11-02 03:06:47 +01:00
|
|
|
*
|
2010-01-02 17:58:17 +01:00
|
|
|
* Portions Copyright (c) 1996-2010, 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
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/misc/superuser.c
|
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
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
#include "catalog/pg_authid.h"
|
2005-05-29 22:38:06 +02:00
|
|
|
#include "utils/inval.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
|
|
|
|
2005-05-29 22:38:06 +02:00
|
|
|
/*
|
2005-06-28 07:09:14 +02:00
|
|
|
* In common cases the same roleid (ie, the session or current ID) will
|
2005-05-29 22:38:06 +02:00
|
|
|
* be queried repeatedly. So we maintain a simple one-entry cache for
|
2005-06-28 07:09:14 +02:00
|
|
|
* the status of the last requested roleid. The cache can be flushed
|
|
|
|
* at need by watching for cache update events on pg_authid.
|
2005-05-29 22:38:06 +02:00
|
|
|
*/
|
2005-10-15 04:49:52 +02:00
|
|
|
static Oid last_roleid = InvalidOid; /* InvalidOid == cache not valid */
|
|
|
|
static bool last_roleid_is_super = false;
|
|
|
|
static bool roleid_callback_registered = false;
|
2005-05-29 22:38:06 +02:00
|
|
|
|
2008-09-09 20:58:09 +02:00
|
|
|
static void RoleidCallback(Datum arg, int cacheid, ItemPointer tuplePtr);
|
2005-05-29 22:38:06 +02:00
|
|
|
|
|
|
|
|
2001-06-13 23:44:41 +02:00
|
|
|
/*
|
|
|
|
* The Postgres user running this command has Postgres superuser privileges
|
|
|
|
*/
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-29 22:38:06 +02:00
|
|
|
/*
|
2005-06-28 07:09:14 +02:00
|
|
|
* The specified role has Postgres superuser privileges
|
2005-05-29 22:38:06 +02:00
|
|
|
*/
|
2002-02-19 00:11:58 +01:00
|
|
|
bool
|
2005-06-28 07:09:14 +02:00
|
|
|
superuser_arg(Oid roleid)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2005-05-29 22:38:06 +02:00
|
|
|
bool result;
|
2005-06-28 07:09:14 +02:00
|
|
|
HeapTuple rtup;
|
1996-11-02 03:06:47 +01:00
|
|
|
|
2005-05-29 22:38:06 +02:00
|
|
|
/* Quick out for cache hit */
|
2005-06-28 07:09:14 +02:00
|
|
|
if (OidIsValid(last_roleid) && last_roleid == roleid)
|
|
|
|
return last_roleid_is_super;
|
2005-05-29 22:38:06 +02:00
|
|
|
|
2001-09-08 17:24:00 +02:00
|
|
|
/* Special escape path in case you deleted all your users. */
|
2005-06-28 07:09:14 +02:00
|
|
|
if (!IsUnderPostmaster && roleid == BOOTSTRAP_SUPERUSERID)
|
2001-09-08 17:24:00 +02:00
|
|
|
return true;
|
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
/* OK, look up the information in pg_authid */
|
2010-02-14 19:42:19 +01:00
|
|
|
rtup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
|
2005-06-28 07:09:14 +02:00
|
|
|
if (HeapTupleIsValid(rtup))
|
2000-11-16 23:30:52 +01:00
|
|
|
{
|
2005-06-28 07:09:14 +02:00
|
|
|
result = ((Form_pg_authid) GETSTRUCT(rtup))->rolsuper;
|
|
|
|
ReleaseSysCache(rtup);
|
2000-11-16 23:30:52 +01:00
|
|
|
}
|
2005-05-29 22:38:06 +02:00
|
|
|
else
|
|
|
|
{
|
2005-06-28 07:09:14 +02:00
|
|
|
/* Report "not superuser" for invalid roleids */
|
2005-05-29 22:38:06 +02:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If first time through, set up callback for cache flushes */
|
2005-06-28 07:09:14 +02:00
|
|
|
if (!roleid_callback_registered)
|
2005-05-29 22:38:06 +02:00
|
|
|
{
|
2005-06-28 07:09:14 +02:00
|
|
|
CacheRegisterSyscacheCallback(AUTHOID,
|
|
|
|
RoleidCallback,
|
2005-05-29 22:38:06 +02:00
|
|
|
(Datum) 0);
|
2005-06-28 07:09:14 +02:00
|
|
|
roleid_callback_registered = true;
|
2005-05-29 22:38:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cache the result for next time */
|
2005-06-28 07:09:14 +02:00
|
|
|
last_roleid = roleid;
|
|
|
|
last_roleid_is_super = result;
|
2005-05-29 22:38:06 +02:00
|
|
|
|
2001-06-13 23:44:41 +02:00
|
|
|
return result;
|
|
|
|
}
|
2005-05-29 22:38:06 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* UseridCallback
|
|
|
|
* Syscache inval callback function
|
|
|
|
*/
|
|
|
|
static void
|
2008-09-09 20:58:09 +02:00
|
|
|
RoleidCallback(Datum arg, int cacheid, ItemPointer tuplePtr)
|
2005-05-29 22:38:06 +02:00
|
|
|
{
|
2005-06-28 07:09:14 +02:00
|
|
|
/* Invalidate our local cache in case role's superuserness changed */
|
|
|
|
last_roleid = InvalidOid;
|
2005-05-29 22:38:06 +02:00
|
|
|
}
|