sync.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2013 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. #include <config.h>
  10. #endif
  11. /**
  12. * LDAP content synchronization plug-in
  13. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "slap.h"
  17. #include "slapi-plugin.h"
  18. #include "slapi-private.h"
  19. #define PLUGIN_NAME "content-sync-plugin"
  20. #define SYNC_PLUGIN_SUBSYSTEM "content-sync-plugin"
  21. #define SYNC_PREOP_DESC "content-sync-preop-subplugin"
  22. #define SYNC_POSTOP_DESC "content-sync-postop-subplugin"
  23. #define SYNC_INT_POSTOP_DESC "content-sync-int-postop-subplugin"
  24. #define SYNC_BETXN_PREOP_DESC "content-sync-betxn-preop-subplugin"
  25. #define SYNC_BE_POSTOP_DESC "content-sync-be-post-subplugin"
  26. #define SYNC_ALLOW_OPENLDAP_COMPAT "syncrepl-allow-openldap"
  27. #define OP_FLAG_SYNC_PERSIST 0x01
  28. #define E_SYNC_REFRESH_REQUIRED 0x1000
  29. #define CL_ATTR_CHANGENUMBER "changenumber"
  30. #define CL_ATTR_ENTRYDN "targetDn"
  31. #define CL_ATTR_UNIQUEID "targetUniqueId"
  32. #define CL_ATTR_ENTRYUUID "targetEntryUUID"
  33. #define CL_ATTR_CHGTYPE "changetype"
  34. #define CL_ATTR_NEWSUPERIOR "newsuperior"
  35. #define CL_SRCH_BASE "cn=changelog"
  36. #define SYNC_INVALID_CHANGENUM ((unsigned long)-1)
  37. typedef struct sync_cookie
  38. {
  39. char *cookie_client_signature;
  40. char *cookie_server_signature;
  41. unsigned long cookie_change_info;
  42. PRBool openldap_compat;
  43. } Sync_Cookie;
  44. typedef struct sync_update
  45. {
  46. char *upd_uuid;
  47. char *upd_euuid;
  48. int upd_chgtype;
  49. Slapi_Entry *upd_e;
  50. } Sync_UpdateNode;
  51. #define SYNC_CALLBACK_PREINIT (-1)
  52. typedef struct sync_callback
  53. {
  54. Slapi_PBlock *orig_pb;
  55. unsigned long changenr;
  56. unsigned long change_start;
  57. int cb_err;
  58. Sync_UpdateNode *cb_updates;
  59. PRBool openldap_compat;
  60. } Sync_CallBackData;
  61. /* Pending list flags
  62. * OPERATION_PL_PENDING: operation not yet completed
  63. * OPERATION_PL_SUCCEEDED: operation completed successfully
  64. * OPERATION_PL_FAILED: operation completed and failed
  65. * OPERATION_PL_IGNORED: operation completed but with an undefine status
  66. */
  67. typedef enum _pl_flags {
  68. OPERATION_PL_HEAD = 1,
  69. OPERATION_PL_PENDING = 2,
  70. OPERATION_PL_SUCCEEDED = 3,
  71. OPERATION_PL_FAILED = 4,
  72. OPERATION_PL_IGNORED = 5
  73. } pl_flags_t;
  74. typedef struct op_ext_ident
  75. {
  76. uint32_t idx_pl; /* To uniquely identify an operation in PL, the operation extension
  77. * contains the index of that operation in the pending list
  78. */
  79. } op_ext_ident_t;
  80. /* Pending list operations.
  81. * it contains a list ('next') of nested operations. The
  82. * order the same order that the server applied the operation
  83. * see https://www.port389.org/docs/389ds/design/content-synchronization-plugin.html#queue-and-pending-list
  84. */
  85. typedef struct OPERATION_PL_CTX
  86. {
  87. Operation *op; /* Pending operation, should not be freed as it belongs to the pblock */
  88. uint32_t idx_pl; /* index of the operation in the pending list */
  89. pl_flags_t flags; /* operation is completed (set to TRUE in POST) */
  90. Slapi_Entry *entry; /* entry to be store in the enqueued node. 1st arg sync_queue_change */
  91. Slapi_Entry *eprev; /* pre-entry to be stored in the enqueued node. 2nd arg sync_queue_change */
  92. ber_int_t chgtype; /* change type to be stored in the enqueued node. 3rd arg of sync_queue_change */
  93. struct OPERATION_PL_CTX *next; /* list of nested operation, the head of the list is the primary operation */
  94. } OPERATION_PL_CTX_T;
  95. OPERATION_PL_CTX_T * get_thread_primary_op(void);
  96. void set_thread_primary_op(OPERATION_PL_CTX_T *op);
  97. op_ext_ident_t * sync_persist_get_operation_extension(Slapi_PBlock *pb);
  98. void sync_persist_set_operation_extension(Slapi_PBlock *pb, op_ext_ident_t *op_ident);
  99. void sync_register_allow_openldap_compat(PRBool allow);
  100. int sync_register_operation_extension(void);
  101. int sync_unregister_operation_entension(void);
  102. int sync_srch_refresh_pre_search(Slapi_PBlock *pb);
  103. int sync_srch_refresh_post_search(Slapi_PBlock *pb);
  104. int sync_srch_refresh_pre_entry(Slapi_PBlock *pb);
  105. int sync_srch_refresh_pre_result(Slapi_PBlock *pb);
  106. int sync_del_persist_post_op(Slapi_PBlock *pb);
  107. int sync_mod_persist_post_op(Slapi_PBlock *pb);
  108. int sync_modrdn_persist_post_op(Slapi_PBlock *pb);
  109. int sync_add_persist_post_op(Slapi_PBlock *pb);
  110. int sync_update_persist_betxn_pre_op(Slapi_PBlock *pb);
  111. int sync_parse_control_value(struct berval *psbvp, ber_int_t *mode, int *reload, char **cookie);
  112. int sync_create_state_control(Slapi_Entry *e, LDAPControl **ctrlp, int type, Sync_Cookie *cookie, PRBool openldap_compat);
  113. int sync_create_sync_done_control(LDAPControl **ctrlp, int refresh, char *cookie);
  114. int sync_intermediate_msg(Slapi_PBlock *pb, int tag, Sync_Cookie *cookie, struct berval **uuids);
  115. int sync_result_msg(Slapi_PBlock *pb, Sync_Cookie *cookie);
  116. int sync_result_err(Slapi_PBlock *pb, int rc, char *msg);
  117. Sync_Cookie *sync_cookie_create(Slapi_PBlock *pb, Sync_Cookie *client_cookie);
  118. void sync_cookie_update(Sync_Cookie *cookie, Slapi_Entry *ec);
  119. Sync_Cookie *sync_cookie_parse(char *cookie, PRBool *cookie_refresh, PRBool *allow_openldap_compat);
  120. int sync_cookie_isvalid(Sync_Cookie *testcookie, Sync_Cookie *refcookie);
  121. void sync_cookie_free(Sync_Cookie **freecookie);
  122. char *sync_cookie2str(Sync_Cookie *cookie);
  123. int sync_number2int(char *nrstr);
  124. unsigned long sync_number2ulong(char *nrstr);
  125. char *sync_nsuniqueid2uuid(const char *nsuniqueid);
  126. char *sync_entryuuid2uuid(const char *nsuniqueid);
  127. int sync_is_active(Slapi_Entry *e, Slapi_PBlock *pb);
  128. int sync_is_active_scope(const Slapi_DN *dn, Slapi_PBlock *pb);
  129. int sync_refresh_update_content(Slapi_PBlock *pb, Sync_Cookie *client_cookie, Sync_Cookie *session_cookie);
  130. int sync_refresh_initial_content(Slapi_PBlock *pb, int persist, PRThread *tid, Sync_Cookie *session_cookie);
  131. int sync_read_entry_from_changelog(Slapi_Entry *cl_entry, void *cb_data);
  132. int sync_send_entry_from_changelog(Slapi_PBlock *pb, int chg_req, char *uniqueid, Sync_Cookie *session_cookie);
  133. void sync_send_deleted_entries(Slapi_PBlock *pb, Sync_UpdateNode *upd, int chg_count, Sync_Cookie *session_cookie);
  134. void sync_send_modified_entries(Slapi_PBlock *pb, Sync_UpdateNode *upd, int chg_count, Sync_Cookie *session_cookie);
  135. int sync_persist_initialize(int argc, char **argv);
  136. PRThread *sync_persist_add(Slapi_PBlock *pb);
  137. int sync_persist_startup(PRThread *tid, Sync_Cookie *session_cookie);
  138. int sync_persist_terminate_all(void);
  139. int sync_persist_terminate(PRThread *tid);
  140. Slapi_PBlock *sync_pblock_copy(Slapi_PBlock *src);
  141. /* prototype for functions not in slapi-plugin.h */
  142. Slapi_ComponentId *plugin_get_default_component_id(void);
  143. /*
  144. * Structures to handle the persitent phase of
  145. * Content Synchronization Requests
  146. *
  147. * A queue of entries being to be sent by a particular persistent
  148. * sync thread
  149. *
  150. * will be created in post op plugins
  151. */
  152. typedef struct sync_queue_node
  153. {
  154. Slapi_Entry *sync_entry;
  155. LDAPControl *pe_ctrls[2]; /* XXX ?? XXX */
  156. struct sync_queue_node *sync_next;
  157. int sync_chgtype;
  158. } SyncQueueNode;
  159. /*
  160. * Information about a single sync search
  161. *
  162. * will be created when a content sync control with
  163. * mode == 3 is decoded
  164. */
  165. typedef struct sync_request
  166. {
  167. Slapi_PBlock *req_pblock;
  168. Slapi_Operation *req_orig_op;
  169. PRLock *req_lock;
  170. PRThread *req_tid;
  171. char *req_orig_base;
  172. Slapi_Filter *req_filter;
  173. PRInt32 req_complete;
  174. Sync_Cookie *req_cookie;
  175. SyncQueueNode *ps_eq_head;
  176. SyncQueueNode *ps_eq_tail;
  177. int req_active;
  178. struct sync_request *req_next;
  179. } SyncRequest;
  180. /*
  181. * A list of established persistent synchronization searches.
  182. *
  183. * will be initialized at plugin initialization
  184. */
  185. #define SYNC_MAX_CONCURRENT 10
  186. typedef struct sync_request_list
  187. {
  188. Slapi_RWLock *sync_req_rwlock; /* R/W lock struct to serialize access */
  189. SyncRequest *sync_req_head; /* Head of list */
  190. pthread_mutex_t sync_req_cvarlock; /* Lock for cvar */
  191. pthread_cond_t sync_req_cvar; /* ps threads sleep on this */
  192. int sync_req_max_persist;
  193. int sync_req_cur_persist;
  194. } SyncRequestList;
  195. #define SYNC_FLAG_ADD_STATE_CTRL 0x01
  196. #define SYNC_FLAG_ADD_DONE_CTRL 0x02
  197. #define SYNC_FLAG_NO_RESULT 0x04
  198. #define SYNC_FLAG_SEND_INTERMEDIATE 0x08
  199. typedef struct sync_op_info
  200. {
  201. int send_flag; /* hint for preop plugins what to send */
  202. Sync_Cookie *cookie; /* cookie to add in control */
  203. PRThread *tid; /* thread for persistent phase */
  204. } SyncOpInfo;