]> granicus.if.org Git - postgresql/blob - src/backend/utils/misc/superuser.c
f677d64fd3e3615d85b3745d6282625a62702120
[postgresql] / src / backend / utils / misc / superuser.c
1 /*-------------------------------------------------------------------------
2  *
3  * superuser.c
4  *        The superuser() function.  Determines if user has superuser privilege.
5  *        Also, a function to check for the owner (datdba) of a database.
6  *
7  *
8  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  *
12  * IDENTIFICATION
13  *        $Header: /cvsroot/pgsql/src/backend/utils/misc/superuser.c,v 1.20 2002/02/18 23:11:26 petere Exp $
14  *
15  *-------------------------------------------------------------------------
16  */
17 #include "postgres.h"
18
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"
26
27
28 /*
29  * The Postgres user running this command has Postgres superuser privileges
30  */
31 bool
32 superuser(void)
33 {
34         return superuser_arg(GetUserId());
35 }
36
37
38 bool
39 superuser_arg(Oid userid)
40 {
41         bool            result = false;
42         HeapTuple       utup;
43
44         /* Special escape path in case you deleted all your users. */
45         if (!IsUnderPostmaster && userid == BOOTSTRAP_USESYSID)
46                 return true;
47
48         utup = SearchSysCache(SHADOWSYSID,
49                                                   ObjectIdGetDatum(userid),
50                                                   0, 0, 0);
51         if (HeapTupleIsValid(utup))
52         {
53                 result = ((Form_pg_shadow) GETSTRUCT(utup))->usesuper;
54                 ReleaseSysCache(utup);
55         }
56         return result;
57 }
58
59
60 /*
61  * The Postgres user running this command is the owner of the specified
62  * database.
63  */
64 bool
65 is_dbadmin(Oid dbid)
66 {
67         Relation        pg_database;
68         ScanKeyData entry[1];
69         HeapScanDesc scan;
70         HeapTuple       dbtuple;
71         int32           dba;
72
73         /* There's no syscache for pg_database, so must look the hard way */
74         pg_database = heap_openr(DatabaseRelationName, AccessShareLock);
75         ScanKeyEntryInitialize(&entry[0], 0x0,
76                                                    ObjectIdAttributeNumber, F_OIDEQ,
77                                                    ObjectIdGetDatum(dbid));
78         scan = heap_beginscan(pg_database, 0, SnapshotNow, 1, entry);
79         dbtuple = heap_getnext(scan, 0);
80         if (!HeapTupleIsValid(dbtuple))
81                 elog(ERROR, "database %u does not exist", dbid);
82         dba = ((Form_pg_database) GETSTRUCT(dbtuple))->datdba;
83         heap_endscan(scan);
84         heap_close(pg_database, AccessShareLock);
85
86         /* XXX some confusion about whether userids are OID or int4 ... */
87         return (GetUserId() == (Oid) dba);
88 }