1 /*-------------------------------------------------------------------------
4 * Definition of (and support for) access control list data structures.
7 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/include/utils/acl.h,v 1.76 2004/12/31 22:03:45 pgsql Exp $
13 * An ACL array is simply an array of AclItems, representing the union
14 * of the privileges represented by the individual items. A zero-length
15 * array represents "no privileges". There are no assumptions about the
16 * ordering of the items, but we do expect that there are no two entries
17 * in the array with the same grantor and grantee.
19 * For backward-compatibility purposes we have to allow null ACL entries
20 * in system catalogs. A null ACL will be treated as meaning "default
21 * protection" (i.e., whatever acldefault() returns).
22 *-------------------------------------------------------------------------
27 #include "nodes/parsenodes.h"
28 #include "utils/array.h"
32 * typedef AclId is declared in c.h
34 * typedef AclMode is declared in parsenodes.h, also the individual privilege
35 * bit meanings are defined there
38 #define ACL_ID_WORLD 0 /* placeholder for id in a WORLD acl item */
41 * 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 */
50 * The IDTYPE included in ai_privs identifies the type of the grantee ID.
51 * The grantor ID currently must always be a user, never a group. (FIXME)
53 * Note: must be same size on all platforms, because the size is hardcoded
54 * in the pg_type.h entry for aclitem.
56 typedef struct AclItem
58 AclId ai_grantee; /* ID that this item grants privs to */
59 AclId ai_grantor; /* grantor of privs (always a user id) */
60 AclMode ai_privs; /* AclIdType plus privilege bits */
64 * The AclIdType is stored in the top two bits of the ai_privs field
65 * of an AclItem. The middle 15 bits are the grant option markers,
66 * and the lower 15 bits are the actual privileges. We use "rights"
67 * to mean the combined grant option and privilege bits fields.
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 #define ACLITEM_GET_IDTYPE(item) ((item).ai_privs >> 30)
74 #define ACL_GRANT_OPTION_FOR(privs) (((AclMode) (privs) & 0x7FFF) << 15)
75 #define ACL_OPTION_TO_PRIVS(privs) (((AclMode) (privs) >> 15) & 0x7FFF)
77 #define ACLITEM_SET_PRIVS(item,privs) \
78 ((item).ai_privs = ((item).ai_privs & ~((AclMode) 0x7FFF)) | \
79 ((AclMode) (privs) & 0x7FFF))
80 #define ACLITEM_SET_GOPTIONS(item,goptions) \
81 ((item).ai_privs = ((item).ai_privs & ~(((AclMode) 0x7FFF) << 15)) | \
82 (((AclMode) (goptions) & 0x7FFF) << 15))
83 #define ACLITEM_SET_RIGHTS(item,rights) \
84 ((item).ai_privs = ((item).ai_privs & ~((AclMode) 0x3FFFFFFF)) | \
85 ((AclMode) (rights) & 0x3FFFFFFF))
86 #define ACLITEM_SET_IDTYPE(item,idtype) \
87 ((item).ai_privs = ((item).ai_privs & ~(((AclMode) 0x03) << 30)) | \
88 (((AclMode) (idtype) & 0x03) << 30))
90 #define ACLITEM_SET_PRIVS_IDTYPE(item,privs,goption,idtype) \
91 ((item).ai_privs = ((AclMode) (privs) & 0x7FFF) | \
92 (((AclMode) (goption) & 0x7FFF) << 15) | \
93 ((AclMode) (idtype) << 30))
95 #define ACLITEM_ALL_PRIV_BITS ((AclMode) 0x7FFF)
96 #define ACLITEM_ALL_GOPTION_BITS ((AclMode) 0x7FFF << 15)
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.
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 * macros provided, unless you know for certain that a particular array
107 * can't have been toasted. Presently, we do not provide toast tables for
108 * pg_class or pg_group, so the entries in those tables won't have been
109 * stored externally --- but they could have been compressed!
114 * Acl a one-dimensional array of AclItem
116 typedef ArrayType Acl;
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)
124 * IdList a one-dimensional array of AclId
126 typedef ArrayType IdList;
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 #define IDLIST_SIZE(IDL) ARR_SIZE(IDL)
134 * fmgr macros for these types
136 #define DatumGetAclItemP(X) ((AclItem *) DatumGetPointer(X))
137 #define PG_GETARG_ACLITEM_P(n) DatumGetAclItemP(PG_GETARG_DATUM(n))
138 #define PG_RETURN_ACLITEM_P(x) PG_RETURN_POINTER(x)
140 #define DatumGetAclP(X) ((Acl *) PG_DETOAST_DATUM(X))
141 #define DatumGetAclPCopy(X) ((Acl *) PG_DETOAST_DATUM_COPY(X))
142 #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)
146 #define DatumGetIdListP(X) ((IdList *) PG_DETOAST_DATUM(X))
147 #define DatumGetIdListPCopy(X) ((IdList *) PG_DETOAST_DATUM_COPY(X))
148 #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)
154 * ACL modification opcodes for aclupdate
156 #define ACL_MODECHG_ADD 1
157 #define ACL_MODECHG_DEL 2
158 #define ACL_MODECHG_EQL 3
161 * External representations of the privilege bits --- aclitemin/aclitemout
162 * represent each possible privilege bit with a distinct 1-character code
164 #define ACL_INSERT_CHR 'a' /* formerly known as "append" */
165 #define ACL_SELECT_CHR 'r' /* formerly known as "read" */
166 #define ACL_UPDATE_CHR 'w' /* formerly known as "write" */
167 #define ACL_DELETE_CHR 'd'
168 #define ACL_RULE_CHR 'R'
169 #define ACL_REFERENCES_CHR 'x'
170 #define ACL_TRIGGER_CHR 't'
171 #define ACL_EXECUTE_CHR 'X'
172 #define ACL_USAGE_CHR 'U'
173 #define ACL_CREATE_CHR 'C'
174 #define ACL_CREATE_TEMP_CHR 'T'
176 /* string holding all privilege code chars, in order by bitmask position */
177 #define ACL_ALL_RIGHTS_STR "arwdRxtXUCT"
180 * Bitmasks defining "all rights" for each supported object type
182 #define ACL_ALL_RIGHTS_RELATION (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_DELETE|ACL_RULE|ACL_REFERENCES|ACL_TRIGGER)
183 #define ACL_ALL_RIGHTS_DATABASE (ACL_CREATE|ACL_CREATE_TEMP)
184 #define ACL_ALL_RIGHTS_FUNCTION (ACL_EXECUTE)
185 #define ACL_ALL_RIGHTS_LANGUAGE (ACL_USAGE)
186 #define ACL_ALL_RIGHTS_NAMESPACE (ACL_USAGE|ACL_CREATE)
187 #define ACL_ALL_RIGHTS_TABLESPACE (ACL_CREATE)
189 /* operation codes for pg_*_aclmask */
192 ACLMASK_ALL, /* normal case: compute all bits */
193 ACLMASK_ANY /* return when result is known nonzero */
196 /* result codes for pg_*_aclcheck */
204 /* this enum covers all object types that can have privilege errors */
205 /* currently it's only used to tell aclcheck_error what to say */
206 typedef enum AclObjectKind
208 ACL_KIND_CLASS, /* pg_class */
209 ACL_KIND_DATABASE, /* pg_database */
210 ACL_KIND_PROC, /* pg_proc */
211 ACL_KIND_OPER, /* pg_operator */
212 ACL_KIND_TYPE, /* pg_type */
213 ACL_KIND_LANGUAGE, /* pg_language */
214 ACL_KIND_NAMESPACE, /* pg_namespace */
215 ACL_KIND_OPCLASS, /* pg_opclass */
216 ACL_KIND_CONVERSION, /* pg_conversion */
217 ACL_KIND_TABLESPACE, /* pg_tablespace */
218 MAX_ACL_KIND /* MUST BE LAST */
222 * routines used internally
224 extern Acl *acldefault(GrantObjectType objtype, AclId ownerid);
225 extern Acl *aclupdate(const Acl *old_acl, const AclItem *mod_aip,
226 int modechg, AclId ownerid, DropBehavior behavior);
227 extern Acl *aclnewowner(const Acl *old_acl, AclId oldownerid, AclId newownerid);
229 extern AclMode aclmask(const Acl *acl, AclId userid, AclId ownerid,
230 AclMode mask, AclMaskHow how);
233 * SQL functions (from acl.c)
235 extern Datum aclitemin(PG_FUNCTION_ARGS);
236 extern Datum aclitemout(PG_FUNCTION_ARGS);
237 extern Datum aclinsert(PG_FUNCTION_ARGS);
238 extern Datum aclremove(PG_FUNCTION_ARGS);
239 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);
245 * prototypes for functions in aclchk.c
247 extern void ExecuteGrantStmt(GrantStmt *stmt);
248 extern AclId get_grosysid(char *groname);
249 extern char *get_groname(AclId grosysid);
251 extern AclMode pg_class_aclmask(Oid table_oid, AclId userid,
252 AclMode mask, AclMaskHow how);
253 extern AclMode pg_database_aclmask(Oid db_oid, AclId userid,
254 AclMode mask, AclMaskHow how);
255 extern AclMode pg_proc_aclmask(Oid proc_oid, AclId userid,
256 AclMode mask, AclMaskHow how);
257 extern AclMode pg_language_aclmask(Oid lang_oid, AclId userid,
258 AclMode mask, AclMaskHow how);
259 extern AclMode pg_namespace_aclmask(Oid nsp_oid, AclId userid,
260 AclMode mask, AclMaskHow how);
261 extern AclMode pg_tablespace_aclmask(Oid spc_oid, AclId userid,
262 AclMode mask, AclMaskHow how);
264 extern AclResult pg_class_aclcheck(Oid table_oid, AclId userid, AclMode mode);
265 extern AclResult pg_database_aclcheck(Oid db_oid, AclId userid, AclMode mode);
266 extern AclResult pg_proc_aclcheck(Oid proc_oid, AclId userid, AclMode mode);
267 extern AclResult pg_language_aclcheck(Oid lang_oid, AclId userid, AclMode mode);
268 extern AclResult pg_namespace_aclcheck(Oid nsp_oid, AclId userid, AclMode mode);
269 extern AclResult pg_tablespace_aclcheck(Oid spc_oid, AclId userid, AclMode mode);
271 extern void aclcheck_error(AclResult aclerr, AclObjectKind objectkind,
272 const char *objectname);
274 /* ownercheck routines just return true (owner) or false (not) */
275 extern bool pg_class_ownercheck(Oid class_oid, AclId userid);
276 extern bool pg_type_ownercheck(Oid type_oid, AclId userid);
277 extern bool pg_oper_ownercheck(Oid oper_oid, AclId userid);
278 extern bool pg_proc_ownercheck(Oid proc_oid, AclId userid);
279 extern bool pg_namespace_ownercheck(Oid nsp_oid, AclId userid);
280 extern bool pg_tablespace_ownercheck(Oid spc_oid, AclId userid);
281 extern bool pg_opclass_ownercheck(Oid opc_oid, AclId userid);
282 extern bool pg_database_ownercheck(Oid db_oid, AclId userid);
283 extern bool pg_conversion_ownercheck(Oid conv_oid, AclId userid);