]> granicus.if.org Git - postgresql/blob - src/include/utils/acl.h
Update copyrights to 2003.
[postgresql] / src / include / utils / acl.h
1 /*-------------------------------------------------------------------------
2  *
3  * acl.h
4  *        Definition of (and support for) access control list data structures.
5  *
6  *
7  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: acl.h,v 1.60 2003/08/04 02:40:15 momjian Exp $
11  *
12  * NOTES
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).
16  *
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  *-------------------------------------------------------------------------
21  */
22 #ifndef ACL_H
23 #define ACL_H
24
25 #include "nodes/parsenodes.h"
26 #include "utils/array.h"
27
28
29 /* typedef AclId is declared in c.h */
30
31 #define ACL_ID_WORLD    0               /* placeholder for id in a WORLD acl item */
32
33 /*
34  * AclIdType    tag that describes if the AclId is a user, group, etc.
35  */
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 */
39
40 /*
41  * AclMode              a bitmask of privilege bits
42  */
43 typedef uint32 AclMode;
44
45 /*
46  * AclItem
47  *
48  * Note: must be same size on all platforms, because the size is hardcoded
49  * in the pg_type.h entry for aclitem.
50  */
51 typedef struct AclItem
52 {
53         AclId           ai_grantee;             /* ID that this item applies to */
54         AclId           ai_grantor;
55         AclMode         ai_privs;               /* AclIdType plus privilege bits */
56 } AclItem;
57
58 /*
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.
62  */
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)
66
67 #define ACL_GRANT_OPTION_FOR(privs) (((privs) & 0x7FFF) << 15)
68
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))
75
76 #define ACLITEM_SET_PRIVS_IDTYPE(item,privs,goption,idtype) \
77   ((item).ai_privs = ((privs) & 0x7FFF) |(((goption) & 0x7FFF) << 15) | ((idtype) << 30))
78
79
80 /*
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.
85  *
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!
92  */
93
94
95 /*
96  * Acl                  a one-dimensional array of AclItem
97  */
98 typedef ArrayType Acl;
99
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)
104
105 /*
106  * IdList               a one-dimensional array of AclId
107  */
108 typedef ArrayType IdList;
109
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)
114
115 /*
116  * fmgr macros for these types
117  */
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)
121
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)
127
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)
133
134
135 /*
136  * ACL modification opcodes
137  */
138 #define ACL_MODECHG_ADD                 1
139 #define ACL_MODECHG_DEL                 2
140 #define ACL_MODECHG_EQL                 3
141
142 /*
143  * External representations of the privilege bits --- aclitemin/aclitemout
144  * represent each possible privilege bit with a distinct 1-character code
145  */
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'
157
158 /* string holding all privilege code chars, in order by bitmask position */
159 #define ACL_ALL_RIGHTS_STR      "arwdRxtXUCT"
160
161 /*
162  * Bitmasks defining "all rights" for each supported object type
163  */
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)
169
170
171 /* result codes for pg_*_aclcheck */
172 typedef enum
173 {
174         ACLCHECK_OK = 0,
175         ACLCHECK_NO_PRIV,
176         ACLCHECK_NOT_OWNER
177 } AclResult;
178
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
182 {
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 */
193 }       AclObjectKind;
194
195 /*
196  * routines used internally
197  */
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);
201
202 /*
203  * SQL functions (from acl.c)
204  */
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
213 /*
214  * prototypes for functions in aclchk.c
215  */
216 extern void ExecuteGrantStmt(GrantStmt *stmt);
217 extern AclId get_grosysid(char *groname);
218 extern char *get_groname(AclId grosysid);
219
220 extern AclResult pg_class_aclcheck(Oid table_oid, AclId userid, AclMode mode);
221 extern AclResult pg_database_aclcheck(Oid db_oid, AclId userid, AclMode mode);
222 extern AclResult pg_proc_aclcheck(Oid proc_oid, AclId userid, AclMode mode);
223 extern AclResult pg_language_aclcheck(Oid lang_oid, AclId userid, AclMode mode);
224 extern AclResult pg_namespace_aclcheck(Oid nsp_oid, AclId userid, AclMode mode);
225
226 extern void aclcheck_error(AclResult aclerr, AclObjectKind objectkind,
227                            const char *objectname);
228
229 /* ownercheck routines just return true (owner) or false (not) */
230 extern bool pg_class_ownercheck(Oid class_oid, AclId userid);
231 extern bool pg_type_ownercheck(Oid type_oid, AclId userid);
232 extern bool pg_oper_ownercheck(Oid oper_oid, AclId userid);
233 extern bool pg_proc_ownercheck(Oid proc_oid, AclId userid);
234 extern bool pg_namespace_ownercheck(Oid nsp_oid, AclId userid);
235 extern bool pg_opclass_ownercheck(Oid opc_oid, AclId userid);
236 extern bool pg_database_ownercheck(Oid db_oid, AclId userid);
237
238 #endif   /* ACL_H */