aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/misc/superuser.c
blob: 520dc470cdcef7e6cab563bdc86a3e8425b1466b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*-------------------------------------------------------------------------
 *
 * superuser.c
 *	  The superuser() function.  Determines if user has superuser privilege.
 *
 * All code should use either of these two functions to find out
 * whether a given user is a superuser, rather than examining
 * pg_shadow.usesuper directly, so that the escape hatch built in for
 * the single-user case works.
 *
 *
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $PostgreSQL: pgsql/src/backend/utils/misc/superuser.c,v 1.31 2005/05/29 20:38:06 tgl Exp $
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "catalog/pg_shadow.h"
#include "utils/inval.h"
#include "utils/syscache.h"
#include "miscadmin.h"


/*
 * In common cases the same userid (ie, the session or current ID) will
 * be queried repeatedly.  So we maintain a simple one-entry cache for
 * the status of the last requested userid.  The cache can be flushed
 * at need by watching for cache update events on pg_shadow.
 */
static AclId	last_userid = 0;		/* 0 == cache not valid */
static bool		last_userid_is_super = false;
static bool		userid_callback_registered = false;

static void UseridCallback(Datum arg, Oid relid);


/*
 * The Postgres user running this command has Postgres superuser privileges
 */
bool
superuser(void)
{
	return superuser_arg(GetUserId());
}


/*
 * The specified userid has Postgres superuser privileges
 */
bool
superuser_arg(AclId userid)
{
	bool		result;
	HeapTuple	utup;

	/* Quick out for cache hit */
	if (AclIdIsValid(last_userid) && last_userid == userid)
		return last_userid_is_super;

	/* Special escape path in case you deleted all your users. */
	if (!IsUnderPostmaster && userid == BOOTSTRAP_USESYSID)
		return true;

	/* OK, look up the information in pg_shadow */
	utup = SearchSysCache(SHADOWSYSID,
						  Int32GetDatum(userid),
						  0, 0, 0);
	if (HeapTupleIsValid(utup))
	{
		result = ((Form_pg_shadow) GETSTRUCT(utup))->usesuper;
		ReleaseSysCache(utup);
	}
	else
	{
		/* Report "not superuser" for invalid userids */
		result = false;
	}

	/* If first time through, set up callback for cache flushes */
	if (!userid_callback_registered)
	{
		CacheRegisterSyscacheCallback(SHADOWSYSID,
									  UseridCallback,
									  (Datum) 0);
		userid_callback_registered = true;
	}

	/* Cache the result for next time */
	last_userid = userid;
	last_userid_is_super = result;

	return result;
}

/*
 * UseridCallback
 *		Syscache inval callback function
 */
static void
UseridCallback(Datum arg, Oid relid)
{
	/* Invalidate our local cache in case user's superuserness changed */
	last_userid = 0;
}