1 /*-------------------------------------------------------------------------
4 * The superuser() function. Determines if user has superuser privilege.
5 * Also, a function to check for the owner (datdba) of a database.
8 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * $Header: /cvsroot/pgsql/src/backend/utils/misc/superuser.c,v 1.19 2001/09/08 15:24:00 petere Exp $
15 *-------------------------------------------------------------------------
19 #include "access/heapam.h"
20 #include "catalog/catname.h"
21 #include "catalog/pg_database.h"
22 #include "catalog/pg_shadow.h"
23 #include "utils/syscache.h"
24 #include "miscadmin.h"
25 #include "utils/fmgroids.h"
29 * The Postgres user running this command has Postgres superuser privileges
37 /* Special escape path in case you deleted all your users. */
38 if (!IsUnderPostmaster && GetUserId() == BOOTSTRAP_USESYSID)
41 utup = SearchSysCache(SHADOWSYSID,
42 ObjectIdGetDatum(GetUserId()),
44 if (HeapTupleIsValid(utup))
46 result = ((Form_pg_shadow) GETSTRUCT(utup))->usesuper;
47 ReleaseSysCache(utup);
53 * The Postgres user running this command is the owner of the specified
65 /* There's no syscache for pg_database, so must look the hard way */
66 pg_database = heap_openr(DatabaseRelationName, AccessShareLock);
67 ScanKeyEntryInitialize(&entry[0], 0x0,
68 ObjectIdAttributeNumber, F_OIDEQ,
69 ObjectIdGetDatum(dbid));
70 scan = heap_beginscan(pg_database, 0, SnapshotNow, 1, entry);
71 dbtuple = heap_getnext(scan, 0);
72 if (!HeapTupleIsValid(dbtuple))
73 elog(ERROR, "database %u does not exist", dbid);
74 dba = ((Form_pg_database) GETSTRUCT(dbtuple))->datdba;
76 heap_close(pg_database, AccessShareLock);
78 /* XXX some confusion about whether userids are OID or int4 ... */
79 return (GetUserId() == (Oid) dba);