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.47 2002/09/04 20:31:45 momjian 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"
30 * AclId system identifier for the user, group, etc.
31 * XXX Perhaps replace this type by OID?
35 #define ACL_ID_WORLD 0 /* placeholder for id in a WORLD acl item */
38 * AclIdType tag that describes if the AclId is a user, group, etc.
40 #define ACL_IDTYPE_WORLD 0x00 /* PUBLIC */
41 #define ACL_IDTYPE_UID 0x01 /* user id - from pg_shadow */
42 #define ACL_IDTYPE_GID 0x02 /* group id - from pg_group */
45 * AclMode a bitmask of privilege bits
47 typedef uint32 AclMode;
52 * Note: must be same size on all platforms, because the size is hardcoded
53 * in the pg_type.h entry for aclitem.
55 typedef struct AclItem
57 AclId ai_id; /* ID that this item applies to */
58 AclMode ai_privs; /* AclIdType plus privilege bits */
62 * The AclIdType is stored in the top two bits of the ai_privs field of an
63 * AclItem, leaving us with thirty usable privilege bits.
65 #define ACLITEM_GET_PRIVS(item) ((item).ai_privs & 0x3FFFFFFF)
66 #define ACLITEM_GET_IDTYPE(item) ((item).ai_privs >> 30)
67 #define ACLITEM_SET_PRIVS_IDTYPE(item,privs,idtype) \
68 ((item).ai_privs = ((privs) & 0x3FFFFFFF) | ((idtype) << 30))
72 * Definitions for convenient access to Acl (array of AclItem) and IdList
73 * (array of AclId). These are standard Postgres arrays, but are restricted
74 * to have one dimension. We also ignore the lower bound when reading,
75 * and set it to zero when writing.
77 * CAUTION: as of Postgres 7.1, these arrays are toastable (just like all
78 * other array types). Therefore, be careful to detoast them with the
79 * macros provided, unless you know for certain that a particular array
80 * can't have been toasted. Presently, we do not provide toast tables for
81 * pg_class or pg_group, so the entries in those tables won't have been
82 * stored externally --- but they could have been compressed!
87 * Acl a one-dimensional POSTGRES array of AclItem
89 typedef ArrayType Acl;
91 #define ACL_NUM(ACL) (ARR_DIMS(ACL)[0])
92 #define ACL_DAT(ACL) ((AclItem *) ARR_DATA_PTR(ACL))
93 #define ACL_N_SIZE(N) (ARR_OVERHEAD(1) + ((N) * sizeof(AclItem)))
94 #define ACL_SIZE(ACL) ARR_SIZE(ACL)
97 * IdList a one-dimensional POSTGRES array of AclId
99 typedef ArrayType IdList;
101 #define IDLIST_NUM(IDL) (ARR_DIMS(IDL)[0])
102 #define IDLIST_DAT(IDL) ((AclId *) ARR_DATA_PTR(IDL))
103 #define IDLIST_N_SIZE(N) (ARR_OVERHEAD(1) + ((N) * sizeof(AclId)))
104 #define IDLIST_SIZE(IDL) ARR_SIZE(IDL)
107 * fmgr macros for these types
109 #define DatumGetAclItemP(X) ((AclItem *) DatumGetPointer(X))
110 #define PG_GETARG_ACLITEM_P(n) DatumGetAclItemP(PG_GETARG_DATUM(n))
111 #define PG_RETURN_ACLITEM_P(x) PG_RETURN_POINTER(x)
113 #define DatumGetAclP(X) ((Acl *) PG_DETOAST_DATUM(X))
114 #define DatumGetAclPCopy(X) ((Acl *) PG_DETOAST_DATUM_COPY(X))
115 #define PG_GETARG_ACL_P(n) DatumGetAclP(PG_GETARG_DATUM(n))
116 #define PG_GETARG_ACL_P_COPY(n) DatumGetAclPCopy(PG_GETARG_DATUM(n))
117 #define PG_RETURN_ACL_P(x) PG_RETURN_POINTER(x)
119 #define DatumGetIdListP(X) ((IdList *) PG_DETOAST_DATUM(X))
120 #define DatumGetIdListPCopy(X) ((IdList *) PG_DETOAST_DATUM_COPY(X))
121 #define PG_GETARG_IDLIST_P(n) DatumGetIdListP(PG_GETARG_DATUM(n))
122 #define PG_GETARG_IDLIST_P_COPY(n) DatumGetIdListPCopy(PG_GETARG_DATUM(n))
123 #define PG_RETURN_IDLIST_P(x) PG_RETURN_POINTER(x)
127 * ACL modification opcodes
129 #define ACL_MODECHG_ADD 1
130 #define ACL_MODECHG_DEL 2
131 #define ACL_MODECHG_EQL 3
133 /* external representation of mode indicators for I/O */
134 #define ACL_MODECHG_ADD_CHR '+'
135 #define ACL_MODECHG_DEL_CHR '-'
136 #define ACL_MODECHG_EQL_CHR '='
139 * External representations of the privilege bits --- aclitemin/aclitemout
140 * represent each possible privilege bit with a distinct 1-character code
142 #define ACL_INSERT_CHR 'a' /* formerly known as "append" */
143 #define ACL_SELECT_CHR 'r' /* formerly known as "read" */
144 #define ACL_UPDATE_CHR 'w' /* formerly known as "write" */
145 #define ACL_DELETE_CHR 'd'
146 #define ACL_RULE_CHR 'R'
147 #define ACL_REFERENCES_CHR 'x'
148 #define ACL_TRIGGER_CHR 't'
149 #define ACL_EXECUTE_CHR 'X'
150 #define ACL_USAGE_CHR 'U'
151 #define ACL_CREATE_CHR 'C'
152 #define ACL_CREATE_TEMP_CHR 'T'
154 /* string holding all privilege code chars, in order by bitmask position */
155 #define ACL_ALL_RIGHTS_STR "arwdRxtXUCT"
158 * Bitmasks defining "all rights" for each supported object type
160 #define ACL_ALL_RIGHTS_RELATION (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_DELETE|ACL_RULE|ACL_REFERENCES|ACL_TRIGGER)
161 #define ACL_ALL_RIGHTS_DATABASE (ACL_CREATE|ACL_CREATE_TEMP)
162 #define ACL_ALL_RIGHTS_FUNCTION (ACL_EXECUTE)
163 #define ACL_ALL_RIGHTS_LANGUAGE (ACL_USAGE)
164 #define ACL_ALL_RIGHTS_NAMESPACE (ACL_USAGE|ACL_CREATE)
167 /* result codes for pg_*_aclcheck */
176 * routines used internally
178 extern Acl *acldefault(GrantObjectType objtype, AclId ownerid);
179 extern Acl *aclinsert3(const Acl *old_acl, const AclItem *mod_aip,
183 * SQL functions (from acl.c)
185 extern Datum aclitemin(PG_FUNCTION_ARGS);
186 extern Datum aclitemout(PG_FUNCTION_ARGS);
187 extern Datum aclinsert(PG_FUNCTION_ARGS);
188 extern Datum aclremove(PG_FUNCTION_ARGS);
189 extern Datum aclcontains(PG_FUNCTION_ARGS);
192 * prototypes for functions in aclchk.c
194 extern void ExecuteGrantStmt(GrantStmt *stmt);
195 extern AclId get_grosysid(char *groname);
196 extern char *get_groname(AclId grosysid);
198 extern AclResult pg_class_aclcheck(Oid table_oid, Oid userid, AclMode mode);
199 extern AclResult pg_database_aclcheck(Oid db_oid, Oid userid, AclMode mode);
200 extern AclResult pg_proc_aclcheck(Oid proc_oid, Oid userid, AclMode mode);
201 extern AclResult pg_language_aclcheck(Oid lang_oid, Oid userid, AclMode mode);
202 extern AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid userid, AclMode mode);
204 extern void aclcheck_error(AclResult errcode, const char *objectname);
206 /* ownercheck routines just return true (owner) or false (not) */
207 extern bool pg_class_ownercheck(Oid class_oid, Oid userid);
208 extern bool pg_type_ownercheck(Oid type_oid, Oid userid);
209 extern bool pg_oper_ownercheck(Oid oper_oid, Oid userid);
210 extern bool pg_proc_ownercheck(Oid proc_oid, Oid userid);
211 extern bool pg_namespace_ownercheck(Oid nsp_oid, Oid userid);
212 extern bool pg_opclass_ownercheck(Oid opc_oid, Oid userid);