]> granicus.if.org Git - postgresql/blob - src/backend/utils/misc/superuser.c
5848b444199aa63c6f20e03cd9201172a74f8fd7
[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.22 2002/05/20 23:51:43 tgl 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  * All code should use either of these two functions to find out
32  * whether a given user is a superuser, rather than evaluating
33  * pg_shadow.usesuper directly, so that the escape hatch built in for
34  * the single-user case works.
35  */
36 bool
37 superuser(void)
38 {
39         return superuser_arg(GetUserId());
40 }
41
42
43 bool
44 superuser_arg(Oid userid)
45 {
46         bool            result = false;
47         HeapTuple       utup;
48
49         /* Special escape path in case you deleted all your users. */
50         if (!IsUnderPostmaster && userid == BOOTSTRAP_USESYSID)
51                 return true;
52
53         utup = SearchSysCache(SHADOWSYSID,
54                                                   ObjectIdGetDatum(userid),
55                                                   0, 0, 0);
56         if (HeapTupleIsValid(utup))
57         {
58                 result = ((Form_pg_shadow) GETSTRUCT(utup))->usesuper;
59                 ReleaseSysCache(utup);
60         }
61         return result;
62 }
63
64
65 /*
66  * The Postgres user running this command is the owner of the specified
67  * database.
68  */
69 bool
70 is_dbadmin(Oid dbid)
71 {
72         Relation        pg_database;
73         ScanKeyData entry[1];
74         HeapScanDesc scan;
75         HeapTuple       dbtuple;
76         int32           dba;
77
78         /* There's no syscache for pg_database, so must look the hard way */
79         pg_database = heap_openr(DatabaseRelationName, AccessShareLock);
80         ScanKeyEntryInitialize(&entry[0], 0x0,
81                                                    ObjectIdAttributeNumber, F_OIDEQ,
82                                                    ObjectIdGetDatum(dbid));
83         scan = heap_beginscan(pg_database, SnapshotNow, 1, entry);
84         dbtuple = heap_getnext(scan, ForwardScanDirection);
85         if (!HeapTupleIsValid(dbtuple))
86                 elog(ERROR, "database %u does not exist", dbid);
87         dba = ((Form_pg_database) GETSTRUCT(dbtuple))->datdba;
88         heap_endscan(scan);
89         heap_close(pg_database, AccessShareLock);
90
91         /* XXX some confusion about whether userids are OID or int4 ... */
92         return (GetUserId() == (Oid) dba);
93 }