1 /*-------------------------------------------------------------------------
4 * Definition of (and support for) access control list data structures.
7 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: acl.h,v 1.62 2003/08/17 19:58:06 tgl Exp $
13 * For backward-compatibility purposes we have to allow there
14 * to be a null ACL in a pg_class tuple. This will be defined as
15 * meaning "default protection" (i.e., whatever acldefault() returns).
17 * The AclItems in an ACL array are currently kept in sorted order.
18 * Things will break hard if you change that without changing the
19 * code wherever this is included.
20 *-------------------------------------------------------------------------
25 #include "nodes/parsenodes.h"
26 #include "utils/array.h"
29 /* typedef AclId is declared in c.h */
31 #define ACL_ID_WORLD 0 /* placeholder for id in a WORLD acl item */
34 * AclIdType tag that describes if the AclId is a user, group, etc.
36 #define ACL_IDTYPE_WORLD 0x00 /* PUBLIC */
37 #define ACL_IDTYPE_UID 0x01 /* user id - from pg_shadow */
38 #define ACL_IDTYPE_GID 0x02 /* group id - from pg_group */
41 * AclMode a bitmask of privilege bits
43 typedef uint32 AclMode;
48 * Note: must be same size on all platforms, because the size is hardcoded
49 * in the pg_type.h entry for aclitem.
51 typedef struct AclItem
53 AclId ai_grantee; /* ID that this item applies to */
55 AclMode ai_privs; /* AclIdType plus privilege bits */
59 * The AclIdType is stored in the top two bits of the ai_privs field
60 * of an AclItem. The middle 15 bits are the grant option markers,
61 * and the lower 15 bits are the actual privileges.
63 #define ACLITEM_GET_PRIVS(item) ((item).ai_privs & 0x7FFF)
64 #define ACLITEM_GET_GOPTIONS(item) (((item).ai_privs >> 15) & 0x7FFF)
65 #define ACLITEM_GET_IDTYPE(item) ((item).ai_privs >> 30)
67 #define ACL_GRANT_OPTION_FOR(privs) (((privs) & 0x7FFF) << 15)
69 #define ACLITEM_SET_PRIVS(item,privs) \
70 ((item).ai_privs = (ACLITEM_GET_IDTYPE(item)<<30) | (ACLITEM_GET_GOPTIONS(item)<<15) | ((privs) & 0x7FFF))
71 #define ACLITEM_SET_GOPTIONS(item,goptions) \
72 ((item).ai_privs = (ACLITEM_GET_IDTYPE(item)<<30) | (((goptions) & 0x7FFF) << 15) | ACLITEM_GET_PRIVS(item))
73 #define ACLITEM_SET_IDTYPE(item,idtype) \
74 ((item).ai_privs = ((idtype)<<30) | (ACLITEM_GET_GOPTIONS(item)<<15) | ACLITEM_GET_PRIVS(item))
76 #define ACLITEM_SET_PRIVS_IDTYPE(item,privs,goption,idtype) \
77 ((item).ai_privs = ((privs) & 0x7FFF) |(((goption) & 0x7FFF) << 15) | ((idtype) << 30))
81 * Definitions for convenient access to Acl (array of AclItem) and IdList
82 * (array of AclId). These are standard PostgreSQL arrays, but are restricted
83 * to have one dimension. We also ignore the lower bound when reading,
84 * and set it to zero when writing.
86 * CAUTION: as of PostgreSQL 7.1, these arrays are toastable (just like all
87 * other array types). Therefore, be careful to detoast them with the
88 * macros provided, unless you know for certain that a particular array
89 * can't have been toasted. Presently, we do not provide toast tables for
90 * pg_class or pg_group, so the entries in those tables won't have been
91 * stored externally --- but they could have been compressed!
96 * Acl a one-dimensional array of AclItem
98 typedef ArrayType Acl;
100 #define ACL_NUM(ACL) (ARR_DIMS(ACL)[0])
101 #define ACL_DAT(ACL) ((AclItem *) ARR_DATA_PTR(ACL))
102 #define ACL_N_SIZE(N) (ARR_OVERHEAD(1) + ((N) * sizeof(AclItem)))
103 #define ACL_SIZE(ACL) ARR_SIZE(ACL)
106 * IdList a one-dimensional array of AclId
108 typedef ArrayType IdList;
110 #define IDLIST_NUM(IDL) (ARR_DIMS(IDL)[0])
111 #define IDLIST_DAT(IDL) ((AclId *) ARR_DATA_PTR(IDL))
112 #define IDLIST_N_SIZE(N) (ARR_OVERHEAD(1) + ((N) * sizeof(AclId)))
113 #define IDLIST_SIZE(IDL) ARR_SIZE(IDL)
116 * fmgr macros for these types
118 #define DatumGetAclItemP(X) ((AclItem *) DatumGetPointer(X))
119 #define PG_GETARG_ACLITEM_P(n) DatumGetAclItemP(PG_GETARG_DATUM(n))
120 #define PG_RETURN_ACLITEM_P(x) PG_RETURN_POINTER(x)
122 #define DatumGetAclP(X) ((Acl *) PG_DETOAST_DATUM(X))
123 #define DatumGetAclPCopy(X) ((Acl *) PG_DETOAST_DATUM_COPY(X))
124 #define PG_GETARG_ACL_P(n) DatumGetAclP(PG_GETARG_DATUM(n))
125 #define PG_GETARG_ACL_P_COPY(n) DatumGetAclPCopy(PG_GETARG_DATUM(n))
126 #define PG_RETURN_ACL_P(x) PG_RETURN_POINTER(x)
128 #define DatumGetIdListP(X) ((IdList *) PG_DETOAST_DATUM(X))
129 #define DatumGetIdListPCopy(X) ((IdList *) PG_DETOAST_DATUM_COPY(X))
130 #define PG_GETARG_IDLIST_P(n) DatumGetIdListP(PG_GETARG_DATUM(n))
131 #define PG_GETARG_IDLIST_P_COPY(n) DatumGetIdListPCopy(PG_GETARG_DATUM(n))
132 #define PG_RETURN_IDLIST_P(x) PG_RETURN_POINTER(x)
136 * ACL modification opcodes
138 #define ACL_MODECHG_ADD 1
139 #define ACL_MODECHG_DEL 2
140 #define ACL_MODECHG_EQL 3
143 * External representations of the privilege bits --- aclitemin/aclitemout
144 * represent each possible privilege bit with a distinct 1-character code
146 #define ACL_INSERT_CHR 'a' /* formerly known as "append" */
147 #define ACL_SELECT_CHR 'r' /* formerly known as "read" */
148 #define ACL_UPDATE_CHR 'w' /* formerly known as "write" */
149 #define ACL_DELETE_CHR 'd'
150 #define ACL_RULE_CHR 'R'
151 #define ACL_REFERENCES_CHR 'x'
152 #define ACL_TRIGGER_CHR 't'
153 #define ACL_EXECUTE_CHR 'X'
154 #define ACL_USAGE_CHR 'U'
155 #define ACL_CREATE_CHR 'C'
156 #define ACL_CREATE_TEMP_CHR 'T'
158 /* string holding all privilege code chars, in order by bitmask position */
159 #define ACL_ALL_RIGHTS_STR "arwdRxtXUCT"
162 * Bitmasks defining "all rights" for each supported object type
164 #define ACL_ALL_RIGHTS_RELATION (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_DELETE|ACL_RULE|ACL_REFERENCES|ACL_TRIGGER)
165 #define ACL_ALL_RIGHTS_DATABASE (ACL_CREATE|ACL_CREATE_TEMP)
166 #define ACL_ALL_RIGHTS_FUNCTION (ACL_EXECUTE)
167 #define ACL_ALL_RIGHTS_LANGUAGE (ACL_USAGE)
168 #define ACL_ALL_RIGHTS_NAMESPACE (ACL_USAGE|ACL_CREATE)
171 /* result codes for pg_*_aclcheck */
179 /* this enum covers all object types that can have privilege errors */
180 /* currently it's only used to tell aclcheck_error what to say */
181 typedef enum AclObjectKind
183 ACL_KIND_CLASS, /* pg_class */
184 ACL_KIND_DATABASE, /* pg_database */
185 ACL_KIND_PROC, /* pg_proc */
186 ACL_KIND_OPER, /* pg_operator */
187 ACL_KIND_TYPE, /* pg_type */
188 ACL_KIND_LANGUAGE, /* pg_language */
189 ACL_KIND_NAMESPACE, /* pg_namespace */
190 ACL_KIND_OPCLASS, /* pg_opclass */
191 ACL_KIND_CONVERSION, /* pg_conversion */
192 MAX_ACL_KIND /* MUST BE LAST */
196 * routines used internally
198 extern Acl *acldefault(GrantObjectType objtype, AclId ownerid);
199 extern Acl *aclinsert3(const Acl *old_acl, const AclItem *mod_aip,
200 unsigned modechg, DropBehavior behavior);
203 * SQL functions (from acl.c)
205 extern Datum aclitemin(PG_FUNCTION_ARGS);
206 extern Datum aclitemout(PG_FUNCTION_ARGS);
207 extern Datum aclinsert(PG_FUNCTION_ARGS);
208 extern Datum aclremove(PG_FUNCTION_ARGS);
209 extern Datum aclcontains(PG_FUNCTION_ARGS);
210 extern Datum makeaclitem(PG_FUNCTION_ARGS);
211 extern Datum aclitem_eq(PG_FUNCTION_ARGS);
212 extern Datum hash_aclitem(PG_FUNCTION_ARGS);
215 * prototypes for functions in aclchk.c
217 extern void ExecuteGrantStmt(GrantStmt *stmt);
218 extern AclId get_grosysid(char *groname);
219 extern char *get_groname(AclId grosysid);
221 extern AclResult pg_class_aclcheck(Oid table_oid, AclId userid, AclMode mode);
222 extern AclResult pg_database_aclcheck(Oid db_oid, AclId userid, AclMode mode);
223 extern AclResult pg_proc_aclcheck(Oid proc_oid, AclId userid, AclMode mode);
224 extern AclResult pg_language_aclcheck(Oid lang_oid, AclId userid, AclMode mode);
225 extern AclResult pg_namespace_aclcheck(Oid nsp_oid, AclId userid, AclMode mode);
227 extern void aclcheck_error(AclResult aclerr, AclObjectKind objectkind,
228 const char *objectname);
230 /* ownercheck routines just return true (owner) or false (not) */
231 extern bool pg_class_ownercheck(Oid class_oid, AclId userid);
232 extern bool pg_type_ownercheck(Oid type_oid, AclId userid);
233 extern bool pg_oper_ownercheck(Oid oper_oid, AclId userid);
234 extern bool pg_proc_ownercheck(Oid proc_oid, AclId userid);
235 extern bool pg_namespace_ownercheck(Oid nsp_oid, AclId userid);
236 extern bool pg_opclass_ownercheck(Oid opc_oid, AclId userid);
237 extern bool pg_database_ownercheck(Oid db_oid, AclId userid);