gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
13KB

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