windows_tot_protocol.c 14 KB

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