windows_tot_protocol.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* windows_tot_protocol.c */
  13. /*
  14. The tot_protocol object implements the DS 5.0 multi-master total update
  15. replication protocol, used to (re)populate a replica.
  16. */
  17. #include "repl.h"
  18. #include "repl5.h"
  19. #include "windowsrepl.h"
  20. #include "windows_prot_private.h"
  21. #include "slap.h"
  22. /* Private data structures */
  23. typedef struct windows_tot_private
  24. {
  25. Repl_Protocol *rp;
  26. Repl_Agmt *ra;
  27. PRLock *lock;
  28. PRUint32 eventbits;
  29. } windows_tot_private;
  30. typedef struct callback_data
  31. {
  32. Private_Repl_Protocol *prp;
  33. int rc;
  34. unsigned long num_entries;
  35. time_t sleep_on_busy; /* not used ??? */
  36. time_t last_busy; /* not used ??? */
  37. } callback_data;
  38. /*
  39. * Number of window seconds to wait until we programmatically decide
  40. * that the replica has got out of BUSY state
  41. */
  42. #define SLEEP_ON_BUSY_WINDOW (10)
  43. /* Helper functions */
  44. static void get_result (int rc, void *cb_data);
  45. static int send_entry (Slapi_Entry *e, void *callback_data);
  46. static void windows_tot_delete(Private_Repl_Protocol **prp);
  47. static void
  48. _windows_tot_send_entry(const Repl_Agmt *ra, callback_data *cbp, const Slapi_DN *local_sdn)
  49. {
  50. Slapi_PBlock *pb = NULL;
  51. char* dn = NULL;
  52. int scope = LDAP_SCOPE_SUBTREE;
  53. char *filter = NULL;
  54. const char *userfilter = NULL;
  55. char **attrs = NULL;
  56. LDAPControl **server_controls = NULL;
  57. if ((NULL == ra) || (NULL == cbp) || (NULL == local_sdn)) {
  58. return;
  59. }
  60. dn = slapi_ch_strdup(slapi_sdn_get_dn(local_sdn));
  61. userfilter = windows_private_get_directory_userfilter(ra);
  62. if (userfilter) {
  63. if ('(' == *userfilter) {
  64. filter = slapi_ch_smprintf("(&(|(objectclass=ntuser)(objectclass=ntgroup))%s)",
  65. userfilter);
  66. } else {
  67. filter = slapi_ch_smprintf("(&(|(objectclass=ntuser)(objectclass=ntgroup))(%s))",
  68. userfilter);
  69. }
  70. } else {
  71. filter = slapi_ch_strdup("(|(objectclass=ntuser)(objectclass=ntgroup))");
  72. }
  73. winsync_plugin_call_pre_ds_search_all_cb(ra, NULL, &dn, &scope, &filter,
  74. &attrs, &server_controls);
  75. pb = slapi_pblock_new ();
  76. /* Perform a subtree search for any ntuser or ntgroup entries underneath the
  77. * suffix defined in the sync agreement. */
  78. slapi_search_internal_set_pb(pb, dn, scope, filter, attrs, 0, server_controls, NULL,
  79. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  80. slapi_search_internal_callback_pb(pb, cbp /* callback data */,
  81. get_result /* result callback */,
  82. send_entry /* entry callback */,
  83. NULL /* referral callback */);
  84. slapi_ch_free_string(&dn);
  85. slapi_ch_free_string(&filter);
  86. slapi_ch_array_free(attrs);
  87. attrs = NULL;
  88. ldap_controls_free(server_controls);
  89. server_controls = NULL;
  90. slapi_pblock_destroy (pb);
  91. }
  92. /*
  93. * Completely refresh a replica. The basic protocol interaction goes
  94. * like this:
  95. * - Acquire Replica by sending a StartReplicationRequest extop, with the
  96. * total update protocol OID and supplier's ruv.
  97. * - Send a series of extended operations containing entries.
  98. * - send an EndReplicationRequest extended operation
  99. */
  100. static void
  101. windows_tot_run(Private_Repl_Protocol *prp)
  102. {
  103. int rc;
  104. callback_data cb_data;
  105. RUV *ruv = NULL;
  106. RUV *starting_ruv = NULL;
  107. Replica *replica = NULL;
  108. Object *local_ruv_obj = NULL;
  109. int one_way;
  110. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "=> windows_tot_run\n" );
  111. PR_ASSERT(NULL != prp);
  112. prp->stopped = 0;
  113. if (prp->terminate)
  114. {
  115. prp->stopped = 1;
  116. goto done;
  117. }
  118. one_way = windows_private_get_one_way(prp->agmt);
  119. windows_conn_set_timeout(prp->conn, agmt_get_timeout(prp->agmt));
  120. /* acquire remote replica */
  121. agmt_set_last_init_start(prp->agmt, current_time());
  122. rc = windows_acquire_replica (prp, &ruv, 0 /* don't check RUV for total protocol */);
  123. /* We never retry total protocol, even in case a transient error.
  124. * This is because if somebody already updated the replica we don't
  125. * want to do it again */
  126. if (rc != ACQUIRE_SUCCESS)
  127. {
  128. int optype, ldaprc;
  129. windows_conn_get_error(prp->conn, &optype, &ldaprc);
  130. agmt_set_last_init_status(prp->agmt, ldaprc,
  131. prp->last_acquire_response_code, 0, NULL);
  132. goto done;
  133. }
  134. else if (prp->terminate)
  135. {
  136. windows_conn_disconnect(prp->conn);
  137. prp->stopped = 1;
  138. goto done;
  139. }
  140. agmt_set_last_init_status(prp->agmt, 0, 0, 0, "Total schema update in progress");
  141. agmt_set_last_init_status(prp->agmt, 0, 0, 0, "Total update in progress");
  142. agmt_set_update_in_progress(prp->agmt, PR_TRUE);
  143. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name, "windows_tot_run - Beginning total update of replica "
  144. "\"%s\".\n", agmt_get_long_name(prp->agmt));
  145. windows_private_null_dirsync_cookie(prp->agmt);
  146. /* call begin total update callback */
  147. winsync_plugin_call_begin_update_cb(prp->agmt,
  148. windows_private_get_directory_treetop(prp->agmt),
  149. windows_private_get_windows_treetop(prp->agmt),
  150. 1 /* is_total == TRUE */);
  151. if ((one_way == ONE_WAY_SYNC_DISABLED) || (one_way == ONE_WAY_SYNC_FROM_AD)) {
  152. /* get everything */
  153. windows_dirsync_inc_run(prp);
  154. }
  155. windows_private_save_dirsync_cookie(prp->agmt);
  156. /* If we got a change from dirsync, we should have a good RUV
  157. * that has a min & max value. If no change was generated,
  158. * the RUV will have NULL min and max csns. We deal with
  159. * updating these values when we process the first change in
  160. * the incremental sync protocol ( send_updates() ). We will
  161. * use this value for setting the consumer RUV if the total
  162. * update succeeds. */
  163. replica = object_get_data(prp->replica_object);
  164. local_ruv_obj = replica_get_ruv (replica);
  165. starting_ruv = ruv_dup((RUV*)object_get_data ( local_ruv_obj ));
  166. object_release (local_ruv_obj);
  167. /* Set up the callback data. */
  168. cb_data.prp = prp;
  169. cb_data.rc = 0;
  170. cb_data.num_entries = 0UL;
  171. cb_data.sleep_on_busy = 0UL;
  172. cb_data.last_busy = current_time ();
  173. /* Don't send anything if one-way (ONE_WAY_SYNC_FROM_AD) is set. */
  174. if ((one_way == ONE_WAY_SYNC_DISABLED) || (one_way == ONE_WAY_SYNC_TO_AD)) {
  175. /* send everything */
  176. const subtreePair* subtree_pairs = NULL;
  177. const subtreePair* sp = NULL;
  178. subtree_pairs = windows_private_get_subtreepairs(prp->agmt);
  179. if (subtree_pairs) {
  180. for (sp = subtree_pairs; sp && sp->DSsubtree; sp++) {
  181. _windows_tot_send_entry(prp->agmt, &cb_data, sp->DSsubtree);
  182. }
  183. } else {
  184. _windows_tot_send_entry(prp->agmt, &cb_data, windows_private_get_directory_subtree(prp->agmt));
  185. }
  186. }
  187. rc = cb_data.rc;
  188. windows_release_replica(prp);
  189. if (rc != CONN_OPERATION_SUCCESS) {
  190. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name, "windows_tot_run - %s - "
  191. "failed to obtain data to send to the consumer; LDAP error - %d\n",
  192. agmt_get_long_name(prp->agmt), rc);
  193. agmt_set_last_init_status(prp->agmt, 0, 0, rc, "Total update aborted");
  194. } else {
  195. slapi_log_error(SLAPI_LOG_ERR, windows_repl_plugin_name, "windows_tot_run - Finished total update of replica "
  196. "\"%s\". Sent %lu entries.\n", agmt_get_long_name(prp->agmt), cb_data.num_entries);
  197. agmt_set_last_init_status(prp->agmt, 0, 0, 0, "Total update succeeded");
  198. /* Now update our consumer RUV for this agreement.
  199. * This ensures that future incrememental updates work.
  200. */
  201. if (slapi_is_loglevel_set(SLAPI_LOG_REPL))
  202. {
  203. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name, "windows_tot_run - "
  204. "Total update setting consumer RUV:\n");
  205. ruv_dump (starting_ruv, "consumer", NULL);
  206. }
  207. agmt_set_consumer_ruv(prp->agmt, starting_ruv );
  208. }
  209. /* Do another dirsync to ensure we get GUIDs for newly added entries. */
  210. if ((one_way == ONE_WAY_SYNC_DISABLED) || (one_way == ONE_WAY_SYNC_FROM_AD)) {
  211. windows_dirsync_inc_run(prp);
  212. }
  213. /* Save the dirsync cookie. */
  214. windows_private_save_dirsync_cookie(prp->agmt);
  215. agmt_set_last_init_end(prp->agmt, current_time());
  216. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  217. agmt_update_done(prp->agmt, 1);
  218. /* call end total update callback */
  219. winsync_plugin_call_end_update_cb(prp->agmt,
  220. windows_private_get_directory_treetop(prp->agmt),
  221. windows_private_get_windows_treetop(prp->agmt),
  222. 1 /* is_total == TRUE */);
  223. done:
  224. if (starting_ruv)
  225. {
  226. ruv_destroy(&starting_ruv);
  227. }
  228. prp->stopped = 1;
  229. ruv_destroy(&ruv);
  230. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "<= windows_tot_run\n" );
  231. }
  232. static int
  233. windows_tot_stop(Private_Repl_Protocol *prp)
  234. {
  235. int return_value;
  236. int seconds = 600;
  237. PRIntervalTime start, maxwait, now;
  238. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "=> windows_tot_stop\n" );
  239. prp->terminate = 1;
  240. maxwait = PR_SecondsToInterval(seconds);
  241. start = PR_IntervalNow();
  242. now = start;
  243. while (!prp->stopped && ((now - start) < maxwait))
  244. {
  245. DS_Sleep(PR_SecondsToInterval(1));
  246. now = PR_IntervalNow();
  247. }
  248. if (!prp->stopped)
  249. {
  250. /* Isn't listening. Disconnect from the replica. */
  251. slapi_log_error(SLAPI_LOG_REPL, windows_repl_plugin_name, "windows_tot_stop - "
  252. "Protocol not stopped after waiting for %d seconds "
  253. "for agreement %s\n", PR_IntervalToSeconds(now-start),
  254. agmt_get_long_name(prp->agmt));
  255. windows_conn_disconnect(prp->conn);
  256. return_value = -1;
  257. }
  258. else
  259. {
  260. return_value = 0;
  261. }
  262. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "<= windows_tot_stop\n" );
  263. return return_value;
  264. }
  265. static int
  266. windows_tot_status(Private_Repl_Protocol *prp)
  267. {
  268. int return_value = 0;
  269. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "=> windows_tot_status\n" );
  270. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "<= windows_tot_status\n" );
  271. return return_value;
  272. }
  273. static void
  274. windows_tot_noop(Private_Repl_Protocol *prp)
  275. {
  276. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "=> windows_tot_noop\n" );
  277. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "<= windows_tot_noop\n" );
  278. /* noop */
  279. }
  280. Private_Repl_Protocol *
  281. Windows_Tot_Protocol_new(Repl_Protocol *rp)
  282. {
  283. windows_tot_private *rip = NULL;
  284. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)slapi_ch_calloc(1, sizeof(Private_Repl_Protocol));
  285. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "=> Windows_Tot_Protocol_new\n" );
  286. prp->delete = windows_tot_delete;
  287. prp->run = windows_tot_run;
  288. prp->stop = windows_tot_stop;
  289. prp->status = windows_tot_status;
  290. prp->notify_update = windows_tot_noop;
  291. prp->notify_agmt_changed = windows_tot_noop;
  292. prp->notify_window_opened = windows_tot_noop;
  293. prp->notify_window_closed = windows_tot_noop;
  294. prp->replica_object = prot_get_replica_object(rp);
  295. prp->update_now = windows_tot_noop;
  296. if ((prp->lock = PR_NewLock()) == NULL)
  297. {
  298. goto loser;
  299. }
  300. if ((prp->cvar = PR_NewCondVar(prp->lock)) == NULL)
  301. {
  302. goto loser;
  303. }
  304. prp->stopped = 1;
  305. prp->terminate = 0;
  306. prp->eventbits = 0;
  307. prp->conn = prot_get_connection(rp);
  308. prp->agmt = prot_get_agreement(rp);
  309. rip = (void *)slapi_ch_malloc(sizeof(windows_tot_private));
  310. rip->rp = rp;
  311. prp->private = (void *)rip;
  312. prp->replica_acquired = PR_FALSE;
  313. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "<= Windows_Tot_Protocol_new\n" );
  314. return prp;
  315. loser:
  316. windows_tot_delete(&prp);
  317. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "<= Windows_Tot_Protocol_new - Failed\n" );
  318. return NULL;
  319. }
  320. static void
  321. windows_tot_delete(Private_Repl_Protocol **prpp)
  322. {
  323. LDAPDebug0Args(LDAP_DEBUG_TRACE, "=> windows_tot_delete\n" );
  324. /* First, stop the protocol if it isn't already stopped */
  325. if (!(*prpp)->stopped) {
  326. (*prpp)->stopped = 1;
  327. (*prpp)->stop(*prpp);
  328. }
  329. /* Then, delete all resources used by the protocol */
  330. if ((*prpp)->lock) {
  331. PR_DestroyLock((*prpp)->lock);
  332. (*prpp)->lock = NULL;
  333. }
  334. if ((*prpp)->cvar) {
  335. PR_DestroyCondVar((*prpp)->cvar);
  336. (*prpp)->cvar = NULL;
  337. }
  338. slapi_ch_free((void **)&(*prpp)->private);
  339. slapi_ch_free((void **)prpp);
  340. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "<= windows_tot_delete\n" );
  341. }
  342. static
  343. void get_result (int rc, void *cb_data)
  344. {
  345. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "=> get_result\n" );
  346. PR_ASSERT (cb_data);
  347. ((callback_data*)cb_data)->rc = rc;
  348. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "<= get_result\n" );
  349. }
  350. static
  351. int send_entry (Slapi_Entry *e, void *cb_data)
  352. {
  353. int rc;
  354. Private_Repl_Protocol *prp;
  355. unsigned long *num_entriesp;
  356. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "=> send_entry\n" );
  357. PR_ASSERT (cb_data);
  358. prp = ((callback_data*)cb_data)->prp;
  359. num_entriesp = &((callback_data *)cb_data)->num_entries;
  360. PR_ASSERT (prp);
  361. if (prp->terminate)
  362. {
  363. windows_conn_disconnect(prp->conn);
  364. prp->stopped = 1;
  365. ((callback_data*)cb_data)->rc = -1;
  366. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "<= send_entry\n" );
  367. return -1;
  368. }
  369. /* skip ruv tombstone - not relvant to Active Directory */
  370. if (is_ruv_tombstone_entry (e)) {
  371. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "<= send_entry\n" );
  372. return 0;
  373. }
  374. /* push the entry to the consumer */
  375. rc = windows_process_total_entry(prp,e);
  376. (*num_entriesp)++;
  377. slapi_log_error(SLAPI_LOG_TRACE, windows_repl_plugin_name, "<= send_entry\n" );
  378. if (CONN_OPERATION_SUCCESS == rc) {
  379. return 0;
  380. } else {
  381. ((callback_data*)cb_data)->rc = rc;
  382. return -1;
  383. }
  384. }