acl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*-------------------------------------------------------------------------
  2. *
  3. * acl.h
  4. * Definition of (and support for) access control list data structures.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, 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".
  16. *
  17. * The order of items in the array is important as client utilities (in
  18. * particular, pg_dump, though possibly other clients) expect to be able
  19. * to issue GRANTs in the ordering of the items in the array. The reason
  20. * this matters is that GRANTs WITH GRANT OPTION must be before any GRANTs
  21. * which depend on it. This happens naturally in the backend during
  22. * operations as we update ACLs in-place, new items are appended, and
  23. * existing entries are only removed if there's no dependency on them (no
  24. * GRANT can been based on it, or, if there was, those GRANTs are also
  25. * removed).
  26. *
  27. * For backward-compatibility purposes we have to allow null ACL entries
  28. * in system catalogs. A null ACL will be treated as meaning "default
  29. * protection" (i.e., whatever acldefault() returns).
  30. *-------------------------------------------------------------------------
  31. */
  32. #ifndef ACL_H
  33. #define ACL_H
  34. #include "access/htup.h"
  35. #include "nodes/parsenodes.h"
  36. #include "parser/parse_node.h"
  37. #include "utils/snapshot.h"
  38. /*
  39. * typedef AclMode is declared in parsenodes.h, also the individual privilege
  40. * bit meanings are defined there
  41. */
  42. #define ACL_ID_PUBLIC 0 /* placeholder for id in a PUBLIC acl item */
  43. /*
  44. * AclItem
  45. *
  46. * Note: must be same size on all platforms, because the size is hardcoded
  47. * in the pg_type.h entry for aclitem.
  48. */
  49. typedef struct AclItem
  50. {
  51. Oid ai_grantee; /* ID that this item grants privs to */
  52. Oid ai_grantor; /* grantor of privs */
  53. AclMode ai_privs; /* privilege bits */
  54. } AclItem;
  55. /*
  56. * The upper 16 bits of the ai_privs field of an AclItem are the grant option
  57. * bits, and the lower 16 bits are the actual privileges. We use "rights"
  58. * to mean the combined grant option and privilege bits fields.
  59. */
  60. #define ACLITEM_GET_PRIVS(item) ((item).ai_privs & 0xFFFF)
  61. #define ACLITEM_GET_GOPTIONS(item) (((item).ai_privs >> 16) & 0xFFFF)
  62. #define ACLITEM_GET_RIGHTS(item) ((item).ai_privs)
  63. #define ACL_GRANT_OPTION_FOR(privs) (((AclMode) (privs) & 0xFFFF) << 16)
  64. #define ACL_OPTION_TO_PRIVS(privs) (((AclMode) (privs) >> 16) & 0xFFFF)
  65. #define ACLITEM_SET_PRIVS(item,privs) \
  66. ((item).ai_privs = ((item).ai_privs & ~((AclMode) 0xFFFF)) | \
  67. ((AclMode) (privs) & 0xFFFF))
  68. #define ACLITEM_SET_GOPTIONS(item,goptions) \
  69. ((item).ai_privs = ((item).ai_privs & ~(((AclMode) 0xFFFF) << 16)) | \
  70. (((AclMode) (goptions) & 0xFFFF) << 16))
  71. #define ACLITEM_SET_RIGHTS(item,rights) \
  72. ((item).ai_privs = (AclMode) (rights))
  73. #define ACLITEM_SET_PRIVS_GOPTIONS(item,privs,goptions) \
  74. ((item).ai_privs = ((AclMode) (privs) & 0xFFFF) | \
  75. (((AclMode) (goptions) & 0xFFFF) << 16))
  76. #define ACLITEM_ALL_PRIV_BITS ((AclMode) 0xFFFF)
  77. #define ACLITEM_ALL_GOPTION_BITS ((AclMode) 0xFFFF << 16)
  78. /*
  79. * Definitions for convenient access to Acl (array of AclItem).
  80. * These are standard PostgreSQL arrays, but are restricted to have one
  81. * dimension and no nulls. We also ignore the lower bound when reading,
  82. * and set it to one when writing.
  83. *
  84. * CAUTION: as of PostgreSQL 7.1, these arrays are toastable (just like all
  85. * other array types). Therefore, be careful to detoast them with the
  86. * macros provided, unless you know for certain that a particular array
  87. * can't have been toasted.
  88. */
  89. /*
  90. * Acl a one-dimensional array of AclItem
  91. */
  92. typedef struct ArrayType Acl;
  93. #define ACL_NUM(ACL) (ARR_DIMS(ACL)[0])
  94. #define ACL_DAT(ACL) ((AclItem *) ARR_DATA_PTR(ACL))
  95. #define ACL_N_SIZE(N) (ARR_OVERHEAD_NONULLS(1) + ((N) * sizeof(AclItem)))
  96. #define ACL_SIZE(ACL) ARR_SIZE(ACL)
  97. /*
  98. * fmgr macros for these types
  99. */
  100. #define DatumGetAclItemP(X) ((AclItem *) DatumGetPointer(X))
  101. #define PG_GETARG_ACLITEM_P(n) DatumGetAclItemP(PG_GETARG_DATUM(n))
  102. #define PG_RETURN_ACLITEM_P(x) PG_RETURN_POINTER(x)
  103. #define DatumGetAclP(X) ((Acl *) PG_DETOAST_DATUM(X))
  104. #define DatumGetAclPCopy(X) ((Acl *) PG_DETOAST_DATUM_COPY(X))
  105. #define PG_GETARG_ACL_P(n) DatumGetAclP(PG_GETARG_DATUM(n))
  106. #define PG_GETARG_ACL_P_COPY(n) DatumGetAclPCopy(PG_GETARG_DATUM(n))
  107. #define PG_RETURN_ACL_P(x) PG_RETURN_POINTER(x)
  108. /*
  109. * ACL modification opcodes for aclupdate
  110. */
  111. #define ACL_MODECHG_ADD 1
  112. #define ACL_MODECHG_DEL 2
  113. #define ACL_MODECHG_EQL 3
  114. /*
  115. * External representations of the privilege bits --- aclitemin/aclitemout
  116. * represent each possible privilege bit with a distinct 1-character code
  117. */
  118. #define ACL_INSERT_CHR 'a' /* formerly known as "append" */
  119. #define ACL_SELECT_CHR 'r' /* formerly known as "read" */
  120. #define ACL_UPDATE_CHR 'w' /* formerly known as "write" */
  121. #define ACL_DELETE_CHR 'd'
  122. #define ACL_TRUNCATE_CHR 'D' /* super-delete, as it were */
  123. #define ACL_REFERENCES_CHR 'x'
  124. #define ACL_TRIGGER_CHR 't'
  125. #define ACL_EXECUTE_CHR 'X'
  126. #define ACL_USAGE_CHR 'U'
  127. #define ACL_CREATE_CHR 'C'
  128. #define ACL_CREATE_TEMP_CHR 'T'
  129. #define ACL_CONNECT_CHR 'c'
  130. #define ACL_SET_CHR 's'
  131. #define ACL_ALTER_SYSTEM_CHR 'A'
  132. /* string holding all privilege code chars, in order by bitmask position */
  133. #define ACL_ALL_RIGHTS_STR "arwdDxtXUCTcsA"
  134. /*
  135. * Bitmasks defining "all rights" for each supported object type
  136. */
  137. #define ACL_ALL_RIGHTS_COLUMN (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_REFERENCES)
  138. #define ACL_ALL_RIGHTS_RELATION (ACL_INSERT|ACL_SELECT|ACL_UPDATE|ACL_DELETE|ACL_TRUNCATE|ACL_REFERENCES|ACL_TRIGGER)
  139. #define ACL_ALL_RIGHTS_SEQUENCE (ACL_USAGE|ACL_SELECT|ACL_UPDATE)
  140. #define ACL_ALL_RIGHTS_DATABASE (ACL_CREATE|ACL_CREATE_TEMP|ACL_CONNECT)
  141. #define ACL_ALL_RIGHTS_FDW (ACL_USAGE)
  142. #define ACL_ALL_RIGHTS_FOREIGN_SERVER (ACL_USAGE)
  143. #define ACL_ALL_RIGHTS_FUNCTION (ACL_EXECUTE)
  144. #define ACL_ALL_RIGHTS_LANGUAGE (ACL_USAGE)
  145. #define ACL_ALL_RIGHTS_LARGEOBJECT (ACL_SELECT|ACL_UPDATE)
  146. #define ACL_ALL_RIGHTS_PARAMETER_ACL (ACL_SET|ACL_ALTER_SYSTEM)
  147. #define ACL_ALL_RIGHTS_SCHEMA (ACL_USAGE|ACL_CREATE)
  148. #define ACL_ALL_RIGHTS_TABLESPACE (ACL_CREATE)
  149. #define ACL_ALL_RIGHTS_TYPE (ACL_USAGE)
  150. /* operation codes for pg_*_aclmask */
  151. typedef enum
  152. {
  153. ACLMASK_ALL, /* normal case: compute all bits */
  154. ACLMASK_ANY /* return when result is known nonzero */
  155. } AclMaskHow;
  156. /* result codes for pg_*_aclcheck */
  157. typedef enum
  158. {
  159. ACLCHECK_OK = 0,
  160. ACLCHECK_NO_PRIV,
  161. ACLCHECK_NOT_OWNER
  162. } AclResult;
  163. /*
  164. * routines used internally
  165. */
  166. extern Acl *acldefault(ObjectType objtype, Oid ownerId);
  167. extern Acl *get_user_default_acl(ObjectType objtype, Oid ownerId,
  168. Oid nsp_oid);
  169. extern void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId,
  170. Oid ownerId, Acl *acl);
  171. extern Acl *aclupdate(const Acl *old_acl, const AclItem *mod_aip,
  172. int modechg, Oid ownerId, DropBehavior behavior);
  173. extern Acl *aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId);
  174. extern Acl *make_empty_acl(void);
  175. extern Acl *aclcopy(const Acl *orig_acl);
  176. extern Acl *aclconcat(const Acl *left_acl, const Acl *right_acl);
  177. extern Acl *aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId);
  178. extern void aclitemsort(Acl *acl);
  179. extern bool aclequal(const Acl *left_acl, const Acl *right_acl);
  180. extern AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId,
  181. AclMode mask, AclMaskHow how);
  182. extern int aclmembers(const Acl *acl, Oid **roleids);
  183. extern bool has_privs_of_role(Oid member, Oid role);
  184. extern bool is_member_of_role(Oid member, Oid role);
  185. extern bool is_member_of_role_nosuper(Oid member, Oid role);
  186. extern bool is_admin_of_role(Oid member, Oid role);
  187. extern void check_is_member_of_role(Oid member, Oid role);
  188. extern Oid get_role_oid(const char *rolename, bool missing_ok);
  189. extern Oid get_role_oid_or_public(const char *rolename);
  190. extern Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok);
  191. extern void check_rolespec_name(const RoleSpec *role, const char *detail_msg);
  192. extern HeapTuple get_rolespec_tuple(const RoleSpec *role);
  193. extern char *get_rolespec_name(const RoleSpec *role);
  194. extern void select_best_grantor(Oid roleId, AclMode privileges,
  195. const Acl *acl, Oid ownerId,
  196. Oid *grantorId, AclMode *grantOptions);
  197. extern void initialize_acl(void);
  198. /*
  199. * prototypes for functions in aclchk.c
  200. */
  201. extern void ExecuteGrantStmt(GrantStmt *stmt);
  202. extern void ExecAlterDefaultPrivilegesStmt(ParseState *pstate, AlterDefaultPrivilegesStmt *stmt);
  203. extern void RemoveRoleFromObjectACL(Oid roleid, Oid classid, Oid objid);
  204. extern AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum,
  205. Oid roleid, AclMode mask, AclMaskHow how);
  206. extern AclMode pg_attribute_aclmask_ext(Oid table_oid, AttrNumber attnum,
  207. Oid roleid, AclMode mask,
  208. AclMaskHow how, bool *is_missing);
  209. extern AclMode pg_class_aclmask(Oid table_oid, Oid roleid,
  210. AclMode mask, AclMaskHow how);
  211. extern AclMode pg_class_aclmask_ext(Oid table_oid, Oid roleid,
  212. AclMode mask, AclMaskHow how,
  213. bool *is_missing);
  214. extern AclMode pg_database_aclmask(Oid db_oid, Oid roleid,
  215. AclMode mask, AclMaskHow how);
  216. extern AclMode pg_parameter_aclmask(const char *name, Oid roleid,
  217. AclMode mask, AclMaskHow how);
  218. extern AclMode pg_parameter_acl_aclmask(Oid acl_oid, Oid roleid,
  219. AclMode mask, AclMaskHow how);
  220. extern AclMode pg_proc_aclmask(Oid proc_oid, Oid roleid,
  221. AclMode mask, AclMaskHow how);
  222. extern AclMode pg_language_aclmask(Oid lang_oid, Oid roleid,
  223. AclMode mask, AclMaskHow how);
  224. extern AclMode pg_largeobject_aclmask_snapshot(Oid lobj_oid, Oid roleid,
  225. AclMode mask, AclMaskHow how, Snapshot snapshot);
  226. extern AclMode pg_namespace_aclmask(Oid nsp_oid, Oid roleid,
  227. AclMode mask, AclMaskHow how);
  228. extern AclMode pg_tablespace_aclmask(Oid spc_oid, Oid roleid,
  229. AclMode mask, AclMaskHow how);
  230. extern AclMode pg_foreign_data_wrapper_aclmask(Oid fdw_oid, Oid roleid,
  231. AclMode mask, AclMaskHow how);
  232. extern AclMode pg_foreign_server_aclmask(Oid srv_oid, Oid roleid,
  233. AclMode mask, AclMaskHow how);
  234. extern AclMode pg_type_aclmask(Oid type_oid, Oid roleid,
  235. AclMode mask, AclMaskHow how);
  236. extern AclResult pg_attribute_aclcheck(Oid table_oid, AttrNumber attnum,
  237. Oid roleid, AclMode mode);
  238. extern AclResult pg_attribute_aclcheck_ext(Oid table_oid, AttrNumber attnum,
  239. Oid roleid, AclMode mode,
  240. bool *is_missing);
  241. extern AclResult pg_attribute_aclcheck_all(Oid table_oid, Oid roleid,
  242. AclMode mode, AclMaskHow how);
  243. extern AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode);
  244. extern AclResult pg_class_aclcheck_ext(Oid table_oid, Oid roleid,
  245. AclMode mode, bool *is_missing);
  246. extern AclResult pg_database_aclcheck(Oid db_oid, Oid roleid, AclMode mode);
  247. extern AclResult pg_parameter_aclcheck(const char *name, Oid roleid,
  248. AclMode mode);
  249. extern AclResult pg_parameter_acl_aclcheck(Oid acl_oid, Oid roleid,
  250. AclMode mode);
  251. extern AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode);
  252. extern AclResult pg_language_aclcheck(Oid lang_oid, Oid roleid, AclMode mode);
  253. extern AclResult pg_largeobject_aclcheck_snapshot(Oid lang_oid, Oid roleid,
  254. AclMode mode, Snapshot snapshot);
  255. extern AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode);
  256. extern AclResult pg_tablespace_aclcheck(Oid spc_oid, Oid roleid, AclMode mode);
  257. extern AclResult pg_foreign_data_wrapper_aclcheck(Oid fdw_oid, Oid roleid, AclMode mode);
  258. extern AclResult pg_foreign_server_aclcheck(Oid srv_oid, Oid roleid, AclMode mode);
  259. extern AclResult pg_type_aclcheck(Oid type_oid, Oid roleid, AclMode mode);
  260. extern void aclcheck_error(AclResult aclerr, ObjectType objtype,
  261. const char *objectname);
  262. extern void aclcheck_error_col(AclResult aclerr, ObjectType objtype,
  263. const char *objectname, const char *colname);
  264. extern void aclcheck_error_type(AclResult aclerr, Oid typeOid);
  265. extern void recordExtObjInitPriv(Oid objoid, Oid classoid);
  266. extern void removeExtObjInitPriv(Oid objoid, Oid classoid);
  267. /* ownercheck routines just return true (owner) or false (not) */
  268. extern bool pg_class_ownercheck(Oid class_oid, Oid roleid);
  269. extern bool pg_type_ownercheck(Oid type_oid, Oid roleid);
  270. extern bool pg_oper_ownercheck(Oid oper_oid, Oid roleid);
  271. extern bool pg_proc_ownercheck(Oid proc_oid, Oid roleid);
  272. extern bool pg_language_ownercheck(Oid lan_oid, Oid roleid);
  273. extern bool pg_largeobject_ownercheck(Oid lobj_oid, Oid roleid);
  274. extern bool pg_namespace_ownercheck(Oid nsp_oid, Oid roleid);
  275. extern bool pg_tablespace_ownercheck(Oid spc_oid, Oid roleid);
  276. extern bool pg_opclass_ownercheck(Oid opc_oid, Oid roleid);
  277. extern bool pg_opfamily_ownercheck(Oid opf_oid, Oid roleid);
  278. extern bool pg_database_ownercheck(Oid db_oid, Oid roleid);
  279. extern bool pg_collation_ownercheck(Oid coll_oid, Oid roleid);
  280. extern bool pg_conversion_ownercheck(Oid conv_oid, Oid roleid);
  281. extern bool pg_ts_dict_ownercheck(Oid dict_oid, Oid roleid);
  282. extern bool pg_ts_config_ownercheck(Oid cfg_oid, Oid roleid);
  283. extern bool pg_foreign_data_wrapper_ownercheck(Oid srv_oid, Oid roleid);
  284. extern bool pg_foreign_server_ownercheck(Oid srv_oid, Oid roleid);
  285. extern bool pg_event_trigger_ownercheck(Oid et_oid, Oid roleid);
  286. extern bool pg_extension_ownercheck(Oid ext_oid, Oid roleid);
  287. extern bool pg_publication_ownercheck(Oid pub_oid, Oid roleid);
  288. extern bool pg_subscription_ownercheck(Oid sub_oid, Oid roleid);
  289. extern bool pg_statistics_object_ownercheck(Oid stat_oid, Oid roleid);
  290. extern bool has_createrole_privilege(Oid roleid);
  291. extern bool has_bypassrls_privilege(Oid roleid);
  292. #endif /* ACL_H */