acl.h 13 KB

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