]> granicus.if.org Git - postgresql/blob - src/include/utils/acl.h
e7f609f0615317a297df6f51d6e9c036afa42567
[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-2002, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: acl.h,v 1.47 2002/09/04 20:31:45 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 /*
30  * AclId                system identifier for the user, group, etc.
31  *                              XXX Perhaps replace this type by OID?
32  */
33 typedef uint32 AclId;
34
35 #define ACL_ID_WORLD    0               /* placeholder for id in a WORLD acl item */
36
37 /*
38  * AclIdType    tag that describes if the AclId is a user, group, etc.
39  */
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 */
43
44 /*
45  * AclMode              a bitmask of privilege bits
46  */
47 typedef uint32 AclMode;
48
49 /*
50  * AclItem
51  *
52  * Note: must be same size on all platforms, because the size is hardcoded
53  * in the pg_type.h entry for aclitem.
54  */
55 typedef struct AclItem
56 {
57         AclId           ai_id;                  /* ID that this item applies to */
58         AclMode         ai_privs;               /* AclIdType plus privilege bits */
59 } AclItem;
60
61 /*
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.
64  */
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))
69
70
71 /*
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.
76  *
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!
83  */
84
85
86 /*
87  * Acl                  a one-dimensional POSTGRES array of AclItem
88  */
89 typedef ArrayType Acl;
90
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)
95
96 /*
97  * IdList               a one-dimensional POSTGRES array of AclId
98  */
99 typedef ArrayType IdList;
100
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)
105
106 /*
107  * fmgr macros for these types
108  */
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)
112
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)
118
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)
124
125
126 /*
127  * ACL modification opcodes
128  */
129 #define ACL_MODECHG_ADD                 1
130 #define ACL_MODECHG_DEL                 2
131 #define ACL_MODECHG_EQL                 3
132
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             '='
137
138 /*
139  * External representations of the privilege bits --- aclitemin/aclitemout
140  * represent each possible privilege bit with a distinct 1-character code
141  */
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'
153
154 /* string holding all privilege code chars, in order by bitmask position */
155 #define ACL_ALL_RIGHTS_STR      "arwdRxtXUCT"
156
157 /*
158  * Bitmasks defining "all rights" for each supported object type
159  */
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)
165
166
167 /* result codes for pg_*_aclcheck */
168 typedef enum
169 {
170         ACLCHECK_OK = 0,
171         ACLCHECK_NO_PRIV,
172         ACLCHECK_NOT_OWNER
173 } AclResult;
174
175 /*
176  * routines used internally
177  */
178 extern Acl *acldefault(GrantObjectType objtype, AclId ownerid);
179 extern Acl *aclinsert3(const Acl *old_acl, const AclItem *mod_aip,
180                    unsigned modechg);
181
182 /*
183  * SQL functions (from acl.c)
184  */
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);
190
191 /*
192  * prototypes for functions in aclchk.c
193  */
194 extern void ExecuteGrantStmt(GrantStmt *stmt);
195 extern AclId get_grosysid(char *groname);
196 extern char *get_groname(AclId grosysid);
197
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);
203
204 extern void aclcheck_error(AclResult errcode, const char *objectname);
205
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);
213
214 #endif   /* ACL_H */