acl.h 10.5 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * acl.h
4
 *	  Definition of (and support for) access control list data structures.
5 6
 *
 *
7
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * $PostgreSQL: pgsql/src/include/utils/acl.h,v 1.77 2005/01/27 23:36:14 neilc Exp $
11 12
 *
 * NOTES
13 14 15 16 17
 *	  An ACL array is simply an array of AclItems, representing the union
 *	  of the privileges represented by the individual items.  A zero-length
 *	  array represents "no privileges".  There are no assumptions about the
 *	  ordering of the items, but we do expect that there are no two entries
 *	  in the array with the same grantor and grantee.
18
 *
19 20 21
 *	  For backward-compatibility purposes we have to allow null ACL entries
 *	  in system catalogs.  A null ACL will be treated as meaning "default
 *	  protection" (i.e., whatever acldefault() returns).
22 23 24 25 26
 *-------------------------------------------------------------------------
 */
#ifndef ACL_H
#define ACL_H

27 28
#include "nodes/parsenodes.h"
#include "utils/array.h"
29

30

31 32 33 34 35 36
/*
 * typedef AclId is declared in c.h
 *
 * typedef AclMode is declared in parsenodes.h, also the individual privilege
 * bit meanings are defined there
 */
37

38
#define ACL_ID_WORLD	0		/* placeholder for id in a WORLD acl item */
39 40 41 42

/*
 * AclIdType	tag that describes if the AclId is a user, group, etc.
 */
43
#define ACL_IDTYPE_WORLD		0x00	/* PUBLIC */
44
#define ACL_IDTYPE_UID			0x01	/* user id - from pg_shadow */
45
#define ACL_IDTYPE_GID			0x02	/* group id - from pg_group */
46 47 48

/*
 * AclItem
49
 *
50 51 52
 * The IDTYPE included in ai_privs identifies the type of the grantee ID.
 * The grantor ID currently must always be a user, never a group.  (FIXME)
 *
53 54
 * Note: must be same size on all platforms, because the size is hardcoded
 * in the pg_type.h entry for aclitem.
55
 */
56 57
typedef struct AclItem
{
58 59
	AclId		ai_grantee;		/* ID that this item grants privs to */
	AclId		ai_grantor;		/* grantor of privs (always a user id) */
60
	AclMode		ai_privs;		/* AclIdType plus privilege bits */
Bruce Momjian's avatar
Bruce Momjian committed
61
} AclItem;
62

63
/*
64 65
 * The AclIdType is stored in the top two bits of the ai_privs field
 * of an AclItem.  The middle 15 bits are the grant option markers,
66 67
 * and the lower 15 bits are the actual privileges.  We use "rights"
 * to mean the combined grant option and privilege bits fields.
68
 */
69
#define ACLITEM_GET_PRIVS(item)    ((item).ai_privs & 0x7FFF)
70
#define ACLITEM_GET_GOPTIONS(item) (((item).ai_privs >> 15) & 0x7FFF)
71
#define ACLITEM_GET_RIGHTS(item)   ((item).ai_privs & 0x3FFFFFFF)
72 73
#define ACLITEM_GET_IDTYPE(item)   ((item).ai_privs >> 30)

74
#define ACL_GRANT_OPTION_FOR(privs) (((AclMode) (privs) & 0x7FFF) << 15)
Bruce Momjian's avatar
Bruce Momjian committed
75
#define ACL_OPTION_TO_PRIVS(privs)	(((AclMode) (privs) >> 15) & 0x7FFF)
76 77

#define ACLITEM_SET_PRIVS(item,privs) \
78 79
  ((item).ai_privs = ((item).ai_privs & ~((AclMode) 0x7FFF)) | \
					 ((AclMode) (privs) & 0x7FFF))
80
#define ACLITEM_SET_GOPTIONS(item,goptions) \
81 82
  ((item).ai_privs = ((item).ai_privs & ~(((AclMode) 0x7FFF) << 15)) | \
					 (((AclMode) (goptions) & 0x7FFF) << 15))
83 84 85
#define ACLITEM_SET_RIGHTS(item,rights) \
  ((item).ai_privs = ((item).ai_privs & ~((AclMode) 0x3FFFFFFF)) | \
					 ((AclMode) (rights) & 0x3FFFFFFF))
86
#define ACLITEM_SET_IDTYPE(item,idtype) \
87 88
  ((item).ai_privs = ((item).ai_privs & ~(((AclMode) 0x03) << 30)) | \
					 (((AclMode) (idtype) & 0x03) << 30))
89

90
#define ACLITEM_SET_PRIVS_IDTYPE(item,privs,goption,idtype) \
91 92 93
  ((item).ai_privs = ((AclMode) (privs) & 0x7FFF) | \
					 (((AclMode) (goption) & 0x7FFF) << 15) | \
					 ((AclMode) (idtype) << 30))
94

95 96
#define ACLITEM_ALL_PRIV_BITS		((AclMode) 0x7FFF)
#define ACLITEM_ALL_GOPTION_BITS	((AclMode) 0x7FFF << 15)
97

98
/*
99
 * Definitions for convenient access to Acl (array of AclItem) and IdList
100
 * (array of AclId).  These are standard PostgreSQL arrays, but are restricted
101
 * to have one dimension.  We also ignore the lower bound when reading,
102
 * and set it to one when writing.
103
 *
104
 * CAUTION: as of PostgreSQL 7.1, these arrays are toastable (just like all
105
 * other array types).	Therefore, be careful to detoast them with the
106 107 108 109
 * macros provided, unless you know for certain that a particular array
 * can't have been toasted.  Presently, we do not provide toast tables for
 * pg_class or pg_group, so the entries in those tables won't have been
 * stored externally --- but they could have been compressed!
110
 */
111

112 113

/*
114
 * Acl			a one-dimensional array of AclItem
115 116
 */
typedef ArrayType Acl;
117

118
#define ACL_NUM(ACL)			(ARR_DIMS(ACL)[0])
119
#define ACL_DAT(ACL)			((AclItem *) ARR_DATA_PTR(ACL))
120
#define ACL_N_SIZE(N)			(ARR_OVERHEAD(1) + ((N) * sizeof(AclItem)))
121
#define ACL_SIZE(ACL)			ARR_SIZE(ACL)
122 123

/*
124
 * IdList		a one-dimensional array of AclId
125 126
 */
typedef ArrayType IdList;
127

128
#define IDLIST_NUM(IDL)			(ARR_DIMS(IDL)[0])
129
#define IDLIST_DAT(IDL)			((AclId *) ARR_DATA_PTR(IDL))
130
#define IDLIST_N_SIZE(N)		(ARR_OVERHEAD(1) + ((N) * sizeof(AclId)))
131 132
#define IDLIST_SIZE(IDL)		ARR_SIZE(IDL)

133 134 135
/*
 * fmgr macros for these types
 */
136 137 138
#define DatumGetAclItemP(X)		   ((AclItem *) DatumGetPointer(X))
#define PG_GETARG_ACLITEM_P(n)	   DatumGetAclItemP(PG_GETARG_DATUM(n))
#define PG_RETURN_ACLITEM_P(x)	   PG_RETURN_POINTER(x)
139

140 141 142
#define DatumGetAclP(X)			   ((Acl *) PG_DETOAST_DATUM(X))
#define DatumGetAclPCopy(X)		   ((Acl *) PG_DETOAST_DATUM_COPY(X))
#define PG_GETARG_ACL_P(n)		   DatumGetAclP(PG_GETARG_DATUM(n))
143
#define PG_GETARG_ACL_P_COPY(n)    DatumGetAclPCopy(PG_GETARG_DATUM(n))
144
#define PG_RETURN_ACL_P(x)		   PG_RETURN_POINTER(x)
145

146 147 148
#define DatumGetIdListP(X)		   ((IdList *) PG_DETOAST_DATUM(X))
#define DatumGetIdListPCopy(X)	   ((IdList *) PG_DETOAST_DATUM_COPY(X))
#define PG_GETARG_IDLIST_P(n)	   DatumGetIdListP(PG_GETARG_DATUM(n))
149
#define PG_GETARG_IDLIST_P_COPY(n) DatumGetIdListPCopy(PG_GETARG_DATUM(n))
150
#define PG_RETURN_IDLIST_P(x)	   PG_RETURN_POINTER(x)
151 152


153
/*
154
 * ACL modification opcodes for aclupdate
155 156 157 158 159
 */
#define ACL_MODECHG_ADD			1
#define ACL_MODECHG_DEL			2
#define ACL_MODECHG_EQL			3

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/*
 * External representations of the privilege bits --- aclitemin/aclitemout
 * represent each possible privilege bit with a distinct 1-character code
 */
#define ACL_INSERT_CHR			'a'		/* formerly known as "append" */
#define ACL_SELECT_CHR			'r'		/* formerly known as "read" */
#define ACL_UPDATE_CHR			'w'		/* formerly known as "write" */
#define ACL_DELETE_CHR			'd'
#define ACL_RULE_CHR			'R'
#define ACL_REFERENCES_CHR		'x'
#define ACL_TRIGGER_CHR			't'
#define ACL_EXECUTE_CHR			'X'
#define ACL_USAGE_CHR			'U'
#define ACL_CREATE_CHR			'C'
#define ACL_CREATE_TEMP_CHR		'T'

/* string holding all privilege code chars, in order by bitmask position */
#define ACL_ALL_RIGHTS_STR	"arwdRxtXUCT"

/*
 * Bitmasks defining "all rights" for each supported object type
 */
#define ACL_ALL_RIGHTS_RELATION		(ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_DELETE|ACL_RULE|ACL_REFERENCES|ACL_TRIGGER)
#define ACL_ALL_RIGHTS_DATABASE		(ACL_CREATE|ACL_CREATE_TEMP)
#define ACL_ALL_RIGHTS_FUNCTION		(ACL_EXECUTE)
#define ACL_ALL_RIGHTS_LANGUAGE		(ACL_USAGE)
#define ACL_ALL_RIGHTS_NAMESPACE	(ACL_USAGE|ACL_CREATE)
187
#define ACL_ALL_RIGHTS_TABLESPACE	(ACL_CREATE)
188

189 190 191 192 193 194
/* operation codes for pg_*_aclmask */
typedef enum
{
	ACLMASK_ALL,				/* normal case: compute all bits */
	ACLMASK_ANY					/* return when result is known nonzero */
} AclMaskHow;
195

196
/* result codes for pg_*_aclcheck */
197 198 199 200 201 202
typedef enum
{
	ACLCHECK_OK = 0,
	ACLCHECK_NO_PRIV,
	ACLCHECK_NOT_OWNER
} AclResult;
203

204 205 206 207 208 209 210 211 212 213 214 215 216
/* this enum covers all object types that can have privilege errors */
/* currently it's only used to tell aclcheck_error what to say */
typedef enum AclObjectKind
{
	ACL_KIND_CLASS,				/* pg_class */
	ACL_KIND_DATABASE,			/* pg_database */
	ACL_KIND_PROC,				/* pg_proc */
	ACL_KIND_OPER,				/* pg_operator */
	ACL_KIND_TYPE,				/* pg_type */
	ACL_KIND_LANGUAGE,			/* pg_language */
	ACL_KIND_NAMESPACE,			/* pg_namespace */
	ACL_KIND_OPCLASS,			/* pg_opclass */
	ACL_KIND_CONVERSION,		/* pg_conversion */
217
	ACL_KIND_TABLESPACE,		/* pg_tablespace */
218
	MAX_ACL_KIND				/* MUST BE LAST */
219
} AclObjectKind;
220

221
/*
222
 * routines used internally
223
 */
224
extern Acl *acldefault(GrantObjectType objtype, AclId ownerid);
225
extern Acl *aclupdate(const Acl *old_acl, const AclItem *mod_aip,
Bruce Momjian's avatar
Bruce Momjian committed
226
		  int modechg, AclId ownerid, DropBehavior behavior);
227
extern Acl *aclnewowner(const Acl *old_acl, AclId oldownerid, AclId newownerid);
Bruce Momjian's avatar
Bruce Momjian committed
228

229
extern AclMode aclmask(const Acl *acl, AclId userid, AclId ownerid,
Bruce Momjian's avatar
Bruce Momjian committed
230
		AclMode mask, AclMaskHow how);
231 232

/*
233
 * SQL functions (from acl.c)
234
 */
235 236 237 238 239
extern Datum aclitemin(PG_FUNCTION_ARGS);
extern Datum aclitemout(PG_FUNCTION_ARGS);
extern Datum aclinsert(PG_FUNCTION_ARGS);
extern Datum aclremove(PG_FUNCTION_ARGS);
extern Datum aclcontains(PG_FUNCTION_ARGS);
240
extern Datum makeaclitem(PG_FUNCTION_ARGS);
241
extern Datum aclitem_eq(PG_FUNCTION_ARGS);
242
extern Datum hash_aclitem(PG_FUNCTION_ARGS);
243 244 245 246

/*
 * prototypes for functions in aclchk.c
 */
247
extern void ExecuteGrantStmt(GrantStmt *stmt);
248
extern char *get_groname(AclId grosysid);
249

250
extern AclMode pg_class_aclmask(Oid table_oid, AclId userid,
Bruce Momjian's avatar
Bruce Momjian committed
251
				 AclMode mask, AclMaskHow how);
252
extern AclMode pg_database_aclmask(Oid db_oid, AclId userid,
Bruce Momjian's avatar
Bruce Momjian committed
253
					AclMode mask, AclMaskHow how);
254
extern AclMode pg_proc_aclmask(Oid proc_oid, AclId userid,
Bruce Momjian's avatar
Bruce Momjian committed
255
				AclMode mask, AclMaskHow how);
256
extern AclMode pg_language_aclmask(Oid lang_oid, AclId userid,
Bruce Momjian's avatar
Bruce Momjian committed
257
					AclMode mask, AclMaskHow how);
258
extern AclMode pg_namespace_aclmask(Oid nsp_oid, AclId userid,
Bruce Momjian's avatar
Bruce Momjian committed
259
					 AclMode mask, AclMaskHow how);
260
extern AclMode pg_tablespace_aclmask(Oid spc_oid, AclId userid,
Bruce Momjian's avatar
Bruce Momjian committed
261
					  AclMode mask, AclMaskHow how);
262

263 264 265 266 267
extern AclResult pg_class_aclcheck(Oid table_oid, AclId userid, AclMode mode);
extern AclResult pg_database_aclcheck(Oid db_oid, AclId userid, AclMode mode);
extern AclResult pg_proc_aclcheck(Oid proc_oid, AclId userid, AclMode mode);
extern AclResult pg_language_aclcheck(Oid lang_oid, AclId userid, AclMode mode);
extern AclResult pg_namespace_aclcheck(Oid nsp_oid, AclId userid, AclMode mode);
268
extern AclResult pg_tablespace_aclcheck(Oid spc_oid, AclId userid, AclMode mode);
269

270
extern void aclcheck_error(AclResult aclerr, AclObjectKind objectkind,
Bruce Momjian's avatar
Bruce Momjian committed
271
			   const char *objectname);
272

273
/* ownercheck routines just return true (owner) or false (not) */
274 275 276 277 278
extern bool pg_class_ownercheck(Oid class_oid, AclId userid);
extern bool pg_type_ownercheck(Oid type_oid, AclId userid);
extern bool pg_oper_ownercheck(Oid oper_oid, AclId userid);
extern bool pg_proc_ownercheck(Oid proc_oid, AclId userid);
extern bool pg_namespace_ownercheck(Oid nsp_oid, AclId userid);
279
extern bool pg_tablespace_ownercheck(Oid spc_oid, AclId userid);
280
extern bool pg_opclass_ownercheck(Oid opc_oid, AclId userid);
281
extern bool pg_database_ownercheck(Oid db_oid, AclId userid);
282
extern bool pg_conversion_ownercheck(Oid conv_oid, AclId userid);
283

284
#endif   /* ACL_H */