1 /*-------------------------------------------------------------------------
4 * Definition of (and support for) access control list data structures.
7 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: acl.h,v 1.50 2003/01/09 18:00:24 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_id; /* ID that this item applies to */
54 AclMode ai_privs; /* AclIdType plus privilege bits */
58 * The AclIdType is stored in the top two bits of the ai_privs field of an
59 * AclItem, leaving us with thirty usable privilege bits.
61 #define ACLITEM_GET_PRIVS(item) ((item).ai_privs & 0x3FFFFFFF)
62 #define ACLITEM_GET_IDTYPE(item) ((item).ai_privs >> 30)
63 #define ACLITEM_SET_PRIVS_IDTYPE(item,privs,idtype) \
64 ((item).ai_privs = ((privs) & 0x3FFFFFFF) | ((idtype) << 30))
68 * Definitions for convenient access to Acl (array of AclItem) and IdList
69 * (array of AclId). These are standard Postgres arrays, but are restricted
70 * to have one dimension. We also ignore the lower bound when reading,
71 * and set it to zero when writing.
73 * CAUTION: as of Postgres 7.1, these arrays are toastable (just like all
74 * other array types). Therefore, be careful to detoast them with the
75 * macros provided, unless you know for certain that a particular array
76 * can't have been toasted. Presently, we do not provide toast tables for
77 * pg_class or pg_group, so the entries in those tables won't have been
78 * stored externally --- but they could have been compressed!
83 * Acl a one-dimensional POSTGRES array of AclItem
85 typedef ArrayType Acl;
87 #define ACL_NUM(ACL) (ARR_DIMS(ACL)[0])
88 #define ACL_DAT(ACL) ((AclItem *) ARR_DATA_PTR(ACL))
89 #define ACL_N_SIZE(N) (ARR_OVERHEAD(1) + ((N) * sizeof(AclItem)))
90 #define ACL_SIZE(ACL) ARR_SIZE(ACL)
93 * IdList a one-dimensional POSTGRES array of AclId
95 typedef ArrayType IdList;
97 #define IDLIST_NUM(IDL) (ARR_DIMS(IDL)[0])
98 #define IDLIST_DAT(IDL) ((AclId *) ARR_DATA_PTR(IDL))
99 #define IDLIST_N_SIZE(N) (ARR_OVERHEAD(1) + ((N) * sizeof(AclId)))
100 #define IDLIST_SIZE(IDL) ARR_SIZE(IDL)
103 * fmgr macros for these types
105 #define DatumGetAclItemP(X) ((AclItem *) DatumGetPointer(X))
106 #define PG_GETARG_ACLITEM_P(n) DatumGetAclItemP(PG_GETARG_DATUM(n))
107 #define PG_RETURN_ACLITEM_P(x) PG_RETURN_POINTER(x)
109 #define DatumGetAclP(X) ((Acl *) PG_DETOAST_DATUM(X))
110 #define DatumGetAclPCopy(X) ((Acl *) PG_DETOAST_DATUM_COPY(X))
111 #define PG_GETARG_ACL_P(n) DatumGetAclP(PG_GETARG_DATUM(n))
112 #define PG_GETARG_ACL_P_COPY(n) DatumGetAclPCopy(PG_GETARG_DATUM(n))
113 #define PG_RETURN_ACL_P(x) PG_RETURN_POINTER(x)
115 #define DatumGetIdListP(X) ((IdList *) PG_DETOAST_DATUM(X))
116 #define DatumGetIdListPCopy(X) ((IdList *) PG_DETOAST_DATUM_COPY(X))
117 #define PG_GETARG_IDLIST_P(n) DatumGetIdListP(PG_GETARG_DATUM(n))
118 #define PG_GETARG_IDLIST_P_COPY(n) DatumGetIdListPCopy(PG_GETARG_DATUM(n))
119 #define PG_RETURN_IDLIST_P(x) PG_RETURN_POINTER(x)
123 * ACL modification opcodes
125 #define ACL_MODECHG_ADD 1
126 #define ACL_MODECHG_DEL 2
127 #define ACL_MODECHG_EQL 3
129 /* external representation of mode indicators for I/O */
130 #define ACL_MODECHG_ADD_CHR '+'
131 #define ACL_MODECHG_DEL_CHR '-'
132 #define ACL_MODECHG_EQL_CHR '='
135 * External representations of the privilege bits --- aclitemin/aclitemout
136 * represent each possible privilege bit with a distinct 1-character code
138 #define ACL_INSERT_CHR 'a' /* formerly known as "append" */
139 #define ACL_SELECT_CHR 'r' /* formerly known as "read" */
140 #define ACL_UPDATE_CHR 'w' /* formerly known as "write" */
141 #define ACL_DELETE_CHR 'd'
142 #define ACL_RULE_CHR 'R'
143 #define ACL_REFERENCES_CHR 'x'
144 #define ACL_TRIGGER_CHR 't'
145 #define ACL_EXECUTE_CHR 'X'
146 #define ACL_USAGE_CHR 'U'
147 #define ACL_CREATE_CHR 'C'
148 #define ACL_CREATE_TEMP_CHR 'T'
150 /* string holding all privilege code chars, in order by bitmask position */
151 #define ACL_ALL_RIGHTS_STR "arwdRxtXUCT"
154 * Bitmasks defining "all rights" for each supported object type
156 #define ACL_ALL_RIGHTS_RELATION (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_DELETE|ACL_RULE|ACL_REFERENCES|ACL_TRIGGER)
157 #define ACL_ALL_RIGHTS_DATABASE (ACL_CREATE|ACL_CREATE_TEMP)
158 #define ACL_ALL_RIGHTS_FUNCTION (ACL_EXECUTE)
159 #define ACL_ALL_RIGHTS_LANGUAGE (ACL_USAGE)
160 #define ACL_ALL_RIGHTS_NAMESPACE (ACL_USAGE|ACL_CREATE)
163 /* result codes for pg_*_aclcheck */
172 * routines used internally
174 extern Acl *acldefault(GrantObjectType objtype, AclId ownerid);
175 extern Acl *aclinsert3(const Acl *old_acl, const AclItem *mod_aip,
179 * SQL functions (from acl.c)
181 extern Datum aclitemin(PG_FUNCTION_ARGS);
182 extern Datum aclitemout(PG_FUNCTION_ARGS);
183 extern Datum aclinsert(PG_FUNCTION_ARGS);
184 extern Datum aclremove(PG_FUNCTION_ARGS);
185 extern Datum aclcontains(PG_FUNCTION_ARGS);
188 * prototypes for functions in aclchk.c
190 extern void ExecuteGrantStmt(GrantStmt *stmt);
191 extern AclId get_grosysid(char *groname);
192 extern char *get_groname(AclId grosysid);
194 extern AclResult pg_class_aclcheck(Oid table_oid, AclId userid, AclMode mode);
195 extern AclResult pg_database_aclcheck(Oid db_oid, AclId userid, AclMode mode);
196 extern AclResult pg_proc_aclcheck(Oid proc_oid, AclId userid, AclMode mode);
197 extern AclResult pg_language_aclcheck(Oid lang_oid, AclId userid, AclMode mode);
198 extern AclResult pg_namespace_aclcheck(Oid nsp_oid, AclId userid, AclMode mode);
200 extern void aclcheck_error(AclResult errcode, const char *objectname);
202 /* ownercheck routines just return true (owner) or false (not) */
203 extern bool pg_class_ownercheck(Oid class_oid, AclId userid);
204 extern bool pg_type_ownercheck(Oid type_oid, AclId userid);
205 extern bool pg_oper_ownercheck(Oid oper_oid, AclId userid);
206 extern bool pg_proc_ownercheck(Oid proc_oid, AclId userid);
207 extern bool pg_namespace_ownercheck(Oid nsp_oid, AclId userid);
208 extern bool pg_opclass_ownercheck(Oid opc_oid, AclId userid);