]> granicus.if.org Git - postgresql/blob - src/include/utils/acl.h
Basic foreign table support.
[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-2011, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/utils/acl.h
11  *
12  * NOTES
13  *        An ACL array is simply an array of AclItems, representing the union
14  *        of the privileges represented by the individual items.  A zero-length
15  *        array represents "no privileges".  There are no assumptions about the
16  *        ordering of the items, but we do expect that there are no two entries
17  *        in the array with the same grantor and grantee.
18  *
19  *        For backward-compatibility purposes we have to allow null ACL entries
20  *        in system catalogs.  A null ACL will be treated as meaning "default
21  *        protection" (i.e., whatever acldefault() returns).
22  *-------------------------------------------------------------------------
23  */
24 #ifndef ACL_H
25 #define ACL_H
26
27 #include "nodes/parsenodes.h"
28 #include "utils/array.h"
29 #include "utils/snapshot.h"
30
31
32 /*
33  * typedef AclMode is declared in parsenodes.h, also the individual privilege
34  * bit meanings are defined there
35  */
36
37 #define ACL_ID_PUBLIC   0               /* placeholder for id in a PUBLIC acl item */
38
39 /*
40  * AclItem
41  *
42  * Note: must be same size on all platforms, because the size is hardcoded
43  * in the pg_type.h entry for aclitem.
44  */
45 typedef struct AclItem
46 {
47         Oid                     ai_grantee;             /* ID that this item grants privs to */
48         Oid                     ai_grantor;             /* grantor of privs */
49         AclMode         ai_privs;               /* privilege bits */
50 } AclItem;
51
52 /*
53  * The upper 16 bits of the ai_privs field of an AclItem are the grant option
54  * bits, and the lower 16 bits are the actual privileges.  We use "rights"
55  * to mean the combined grant option and privilege bits fields.
56  */
57 #define ACLITEM_GET_PRIVS(item)    ((item).ai_privs & 0xFFFF)
58 #define ACLITEM_GET_GOPTIONS(item) (((item).ai_privs >> 16) & 0xFFFF)
59 #define ACLITEM_GET_RIGHTS(item)   ((item).ai_privs)
60
61 #define ACL_GRANT_OPTION_FOR(privs) (((AclMode) (privs) & 0xFFFF) << 16)
62 #define ACL_OPTION_TO_PRIVS(privs)      (((AclMode) (privs) >> 16) & 0xFFFF)
63
64 #define ACLITEM_SET_PRIVS(item,privs) \
65   ((item).ai_privs = ((item).ai_privs & ~((AclMode) 0xFFFF)) | \
66                                          ((AclMode) (privs) & 0xFFFF))
67 #define ACLITEM_SET_GOPTIONS(item,goptions) \
68   ((item).ai_privs = ((item).ai_privs & ~(((AclMode) 0xFFFF) << 16)) | \
69                                          (((AclMode) (goptions) & 0xFFFF) << 16))
70 #define ACLITEM_SET_RIGHTS(item,rights) \
71   ((item).ai_privs = (AclMode) (rights))
72
73 #define ACLITEM_SET_PRIVS_GOPTIONS(item,privs,goptions) \
74   ((item).ai_privs = ((AclMode) (privs) & 0xFFFF) | \
75                                          (((AclMode) (goptions) & 0xFFFF) << 16))
76
77
78 #define ACLITEM_ALL_PRIV_BITS           ((AclMode) 0xFFFF)
79 #define ACLITEM_ALL_GOPTION_BITS        ((AclMode) 0xFFFF << 16)
80
81 /*
82  * Definitions for convenient access to Acl (array of AclItem).
83  * These are standard PostgreSQL arrays, but are restricted to have one
84  * dimension and no nulls.      We also ignore the lower bound when reading,
85  * and set it to one when writing.
86  *
87  * CAUTION: as of PostgreSQL 7.1, these arrays are toastable (just like all
88  * other array types).  Therefore, be careful to detoast them with the
89  * macros provided, unless you know for certain that a particular array
90  * can't have been toasted.
91  */
92
93
94 /*
95  * Acl                  a one-dimensional array of AclItem
96  */
97 typedef ArrayType Acl;
98
99 #define ACL_NUM(ACL)                    (ARR_DIMS(ACL)[0])
100 #define ACL_DAT(ACL)                    ((AclItem *) ARR_DATA_PTR(ACL))
101 #define ACL_N_SIZE(N)                   (ARR_OVERHEAD_NONULLS(1) + ((N) * sizeof(AclItem)))
102 #define ACL_SIZE(ACL)                   ARR_SIZE(ACL)
103
104 /*
105  * fmgr macros for these types
106  */
107 #define DatumGetAclItemP(X)                ((AclItem *) DatumGetPointer(X))
108 #define PG_GETARG_ACLITEM_P(n)     DatumGetAclItemP(PG_GETARG_DATUM(n))
109 #define PG_RETURN_ACLITEM_P(x)     PG_RETURN_POINTER(x)
110
111 #define DatumGetAclP(X)                    ((Acl *) PG_DETOAST_DATUM(X))
112 #define DatumGetAclPCopy(X)                ((Acl *) PG_DETOAST_DATUM_COPY(X))
113 #define PG_GETARG_ACL_P(n)                 DatumGetAclP(PG_GETARG_DATUM(n))
114 #define PG_GETARG_ACL_P_COPY(n)    DatumGetAclPCopy(PG_GETARG_DATUM(n))
115 #define PG_RETURN_ACL_P(x)                 PG_RETURN_POINTER(x)
116
117 /*
118  * ACL modification opcodes for aclupdate
119  */
120 #define ACL_MODECHG_ADD                 1
121 #define ACL_MODECHG_DEL                 2
122 #define ACL_MODECHG_EQL                 3
123
124 /*
125  * External representations of the privilege bits --- aclitemin/aclitemout
126  * represent each possible privilege bit with a distinct 1-character code
127  */
128 #define ACL_INSERT_CHR                  'a'             /* formerly known as "append" */
129 #define ACL_SELECT_CHR                  'r'             /* formerly known as "read" */
130 #define ACL_UPDATE_CHR                  'w'             /* formerly known as "write" */
131 #define ACL_DELETE_CHR                  'd'
132 #define ACL_TRUNCATE_CHR                'D'             /* super-delete, as it were */
133 #define ACL_REFERENCES_CHR              'x'
134 #define ACL_TRIGGER_CHR                 't'
135 #define ACL_EXECUTE_CHR                 'X'
136 #define ACL_USAGE_CHR                   'U'
137 #define ACL_CREATE_CHR                  'C'
138 #define ACL_CREATE_TEMP_CHR             'T'
139 #define ACL_CONNECT_CHR                 'c'
140
141 /* string holding all privilege code chars, in order by bitmask position */
142 #define ACL_ALL_RIGHTS_STR      "arwdDxtXUCTc"
143
144 /*
145  * Bitmasks defining "all rights" for each supported object type
146  */
147 #define ACL_ALL_RIGHTS_COLUMN           (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_REFERENCES)
148 #define ACL_ALL_RIGHTS_RELATION         (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_DELETE|ACL_TRUNCATE|ACL_REFERENCES|ACL_TRIGGER)
149 #define ACL_ALL_RIGHTS_SEQUENCE         (ACL_USAGE|ACL_SELECT|ACL_UPDATE)
150 #define ACL_ALL_RIGHTS_DATABASE         (ACL_CREATE|ACL_CREATE_TEMP|ACL_CONNECT)
151 #define ACL_ALL_RIGHTS_FDW                      (ACL_USAGE)
152 #define ACL_ALL_RIGHTS_FOREIGN_SERVER (ACL_USAGE)
153 #define ACL_ALL_RIGHTS_FOREIGN_TABLE (ACL_SELECT)
154 #define ACL_ALL_RIGHTS_FUNCTION         (ACL_EXECUTE)
155 #define ACL_ALL_RIGHTS_LANGUAGE         (ACL_USAGE)
156 #define ACL_ALL_RIGHTS_LARGEOBJECT      (ACL_SELECT|ACL_UPDATE)
157 #define ACL_ALL_RIGHTS_NAMESPACE        (ACL_USAGE|ACL_CREATE)
158 #define ACL_ALL_RIGHTS_TABLESPACE       (ACL_CREATE)
159
160 /* operation codes for pg_*_aclmask */
161 typedef enum
162 {
163         ACLMASK_ALL,                            /* normal case: compute all bits */
164         ACLMASK_ANY                                     /* return when result is known nonzero */
165 } AclMaskHow;
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 /* this enum covers all object types that can have privilege errors */
176 /* currently it's only used to tell aclcheck_error what to say */
177 typedef enum AclObjectKind
178 {
179         ACL_KIND_COLUMN,                        /* pg_attribute */
180         ACL_KIND_CLASS,                         /* pg_class */
181         ACL_KIND_SEQUENCE,                      /* pg_sequence */
182         ACL_KIND_DATABASE,                      /* pg_database */
183         ACL_KIND_PROC,                          /* pg_proc */
184         ACL_KIND_OPER,                          /* pg_operator */
185         ACL_KIND_TYPE,                          /* pg_type */
186         ACL_KIND_LANGUAGE,                      /* pg_language */
187         ACL_KIND_LARGEOBJECT,           /* pg_largeobject */
188         ACL_KIND_NAMESPACE,                     /* pg_namespace */
189         ACL_KIND_OPCLASS,                       /* pg_opclass */
190         ACL_KIND_OPFAMILY,                      /* pg_opfamily */
191         ACL_KIND_CONVERSION,            /* pg_conversion */
192         ACL_KIND_TABLESPACE,            /* pg_tablespace */
193         ACL_KIND_TSDICTIONARY,          /* pg_ts_dict */
194         ACL_KIND_TSCONFIGURATION,       /* pg_ts_config */
195         ACL_KIND_FDW,                           /* pg_foreign_data_wrapper */
196         ACL_KIND_FOREIGN_SERVER,        /* pg_foreign_server */
197         ACL_KIND_FOREIGN_TABLE,         /* pg_foreign_table */
198         MAX_ACL_KIND                            /* MUST BE LAST */
199 } AclObjectKind;
200
201
202 /*
203  * routines used internally
204  */
205 extern Acl *acldefault(GrantObjectType objtype, Oid ownerId);
206 extern Acl *get_user_default_acl(GrantObjectType objtype, Oid ownerId,
207                                          Oid nsp_oid);
208
209 extern Acl *aclupdate(const Acl *old_acl, const AclItem *mod_aip,
210                   int modechg, Oid ownerId, DropBehavior behavior);
211 extern Acl *aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId);
212 extern Acl *make_empty_acl(void);
213 extern Acl *aclcopy(const Acl *orig_acl);
214 extern Acl *aclconcat(const Acl *left_acl, const Acl *right_acl);
215 extern Acl *aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId);
216 extern void aclitemsort(Acl *acl);
217 extern bool aclequal(const Acl *left_acl, const Acl *right_acl);
218
219 extern AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId,
220                 AclMode mask, AclMaskHow how);
221 extern int      aclmembers(const Acl *acl, Oid **roleids);
222
223 extern bool has_privs_of_role(Oid member, Oid role);
224 extern bool is_member_of_role(Oid member, Oid role);
225 extern bool is_member_of_role_nosuper(Oid member, Oid role);
226 extern bool is_admin_of_role(Oid member, Oid role);
227 extern void check_is_member_of_role(Oid member, Oid role);
228 extern Oid get_role_oid(const char *rolname, bool missing_ok);
229
230 extern void select_best_grantor(Oid roleId, AclMode privileges,
231                                         const Acl *acl, Oid ownerId,
232                                         Oid *grantorId, AclMode *grantOptions);
233
234 extern void initialize_acl(void);
235
236 /*
237  * SQL functions (from acl.c)
238  */
239 extern Datum aclitemin(PG_FUNCTION_ARGS);
240 extern Datum aclitemout(PG_FUNCTION_ARGS);
241 extern Datum aclinsert(PG_FUNCTION_ARGS);
242 extern Datum aclremove(PG_FUNCTION_ARGS);
243 extern Datum aclcontains(PG_FUNCTION_ARGS);
244 extern Datum makeaclitem(PG_FUNCTION_ARGS);
245 extern Datum aclitem_eq(PG_FUNCTION_ARGS);
246 extern Datum hash_aclitem(PG_FUNCTION_ARGS);
247 extern Datum aclexplode(PG_FUNCTION_ARGS);
248
249 /*
250  * prototypes for functions in aclchk.c
251  */
252 extern void ExecuteGrantStmt(GrantStmt *stmt);
253 extern void ExecAlterDefaultPrivilegesStmt(AlterDefaultPrivilegesStmt *stmt);
254
255 extern void RemoveRoleFromObjectACL(Oid roleid, Oid classid, Oid objid);
256 extern void RemoveDefaultACLById(Oid defaclOid);
257
258 extern AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum,
259                                          Oid roleid, AclMode mask, AclMaskHow how);
260 extern AclMode pg_class_aclmask(Oid table_oid, Oid roleid,
261                                  AclMode mask, AclMaskHow how);
262 extern AclMode pg_database_aclmask(Oid db_oid, Oid roleid,
263                                         AclMode mask, AclMaskHow how);
264 extern AclMode pg_proc_aclmask(Oid proc_oid, Oid roleid,
265                                 AclMode mask, AclMaskHow how);
266 extern AclMode pg_language_aclmask(Oid lang_oid, Oid roleid,
267                                         AclMode mask, AclMaskHow how);
268 extern AclMode pg_largeobject_aclmask_snapshot(Oid lobj_oid, Oid roleid,
269                                                         AclMode mask, AclMaskHow how, Snapshot snapshot);
270 extern AclMode pg_namespace_aclmask(Oid nsp_oid, Oid roleid,
271                                          AclMode mask, AclMaskHow how);
272 extern AclMode pg_tablespace_aclmask(Oid spc_oid, Oid roleid,
273                                           AclMode mask, AclMaskHow how);
274 extern AclMode pg_foreign_data_wrapper_aclmask(Oid fdw_oid, Oid roleid,
275                                                                 AclMode mask, AclMaskHow how);
276 extern AclMode pg_foreign_server_aclmask(Oid srv_oid, Oid roleid,
277                                                   AclMode mask, AclMaskHow how);
278
279 extern AclResult pg_attribute_aclcheck(Oid table_oid, AttrNumber attnum,
280                                           Oid roleid, AclMode mode);
281 extern AclResult pg_attribute_aclcheck_all(Oid table_oid, Oid roleid,
282                                                   AclMode mode, AclMaskHow how);
283 extern AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode);
284 extern AclResult pg_database_aclcheck(Oid db_oid, Oid roleid, AclMode mode);
285 extern AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode);
286 extern AclResult pg_language_aclcheck(Oid lang_oid, Oid roleid, AclMode mode);
287 extern AclResult pg_largeobject_aclcheck_snapshot(Oid lang_oid, Oid roleid,
288                                                                  AclMode mode, Snapshot snapshot);
289 extern AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode);
290 extern AclResult pg_tablespace_aclcheck(Oid spc_oid, Oid roleid, AclMode mode);
291 extern AclResult pg_foreign_data_wrapper_aclcheck(Oid fdw_oid, Oid roleid, AclMode mode);
292 extern AclResult pg_foreign_server_aclcheck(Oid srv_oid, Oid roleid, AclMode mode);
293
294 extern void aclcheck_error(AclResult aclerr, AclObjectKind objectkind,
295                            const char *objectname);
296
297 extern void aclcheck_error_col(AclResult aclerr, AclObjectKind objectkind,
298                                    const char *objectname, const char *colname);
299
300 /* ownercheck routines just return true (owner) or false (not) */
301 extern bool pg_class_ownercheck(Oid class_oid, Oid roleid);
302 extern bool pg_type_ownercheck(Oid type_oid, Oid roleid);
303 extern bool pg_oper_ownercheck(Oid oper_oid, Oid roleid);
304 extern bool pg_proc_ownercheck(Oid proc_oid, Oid roleid);
305 extern bool pg_language_ownercheck(Oid lan_oid, Oid roleid);
306 extern bool pg_largeobject_ownercheck(Oid lobj_oid, Oid roleid);
307 extern bool pg_namespace_ownercheck(Oid nsp_oid, Oid roleid);
308 extern bool pg_tablespace_ownercheck(Oid spc_oid, Oid roleid);
309 extern bool pg_opclass_ownercheck(Oid opc_oid, Oid roleid);
310 extern bool pg_opfamily_ownercheck(Oid opf_oid, Oid roleid);
311 extern bool pg_database_ownercheck(Oid db_oid, Oid roleid);
312 extern bool pg_conversion_ownercheck(Oid conv_oid, Oid roleid);
313 extern bool pg_ts_dict_ownercheck(Oid dict_oid, Oid roleid);
314 extern bool pg_ts_config_ownercheck(Oid cfg_oid, Oid roleid);
315 extern bool pg_foreign_server_ownercheck(Oid srv_oid, Oid roleid);
316
317 #endif   /* ACL_H */