repl5_agmtlist.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. /* repl5_agmtlist.c */
  39. /*
  40. Replication agreements are held in object set (objset.c).
  41. */
  42. #include "repl5.h"
  43. #define AGMT_CONFIG_BASE "cn=mapping tree, cn=config"
  44. #define CONFIG_FILTER "(objectclass=nsds5replicationagreement)"
  45. #define WINDOWS_CONFIG_FILTER "(objectclass=nsdsWindowsreplicationagreement)"
  46. #define GLOBAL_CONFIG_FILTER "(|" CONFIG_FILTER WINDOWS_CONFIG_FILTER " )"
  47. PRCallOnceType once = {0};
  48. Objset *agmt_set = NULL; /* The set of replication agreements */
  49. typedef struct agmt_wrapper {
  50. Repl_Agmt *agmt;
  51. void *handle;
  52. } agmt_wrapper;
  53. /*
  54. * Find the replication agreement whose entry DN matches the given DN.
  55. * Object is returned referenced, so be sure to release it when
  56. * finished.
  57. */
  58. Repl_Agmt *
  59. agmtlist_get_by_agmt_name(const Slapi_DN *agmt_name)
  60. {
  61. Repl_Agmt *ra;
  62. Object *ro;
  63. for (ro = objset_first_obj(agmt_set); NULL != ro;
  64. ro = objset_next_obj(agmt_set, ro))
  65. {
  66. ra = (Repl_Agmt *)object_get_data(ro);
  67. if (agmt_matches_name(ra, agmt_name))
  68. {
  69. break;
  70. }
  71. }
  72. return ra;
  73. }
  74. static int
  75. agmt_ptr_cmp(Object *ro, const void *arg)
  76. {
  77. Repl_Agmt *ra;
  78. Repl_Agmt *provided_ra = (Repl_Agmt *)arg;
  79. ra = object_get_data(ro);
  80. if (ra == provided_ra)
  81. return 0;
  82. else
  83. return 1;
  84. }
  85. static int
  86. agmt_dn_cmp(Object *ro, const void *arg)
  87. {
  88. Repl_Agmt *ra;
  89. Slapi_DN *sdn = (Slapi_DN *)arg;
  90. ra = object_get_data(ro);
  91. return(slapi_sdn_compare(sdn, agmt_get_dn_byref(ra)));
  92. }
  93. void
  94. agmtlist_release_agmt(Repl_Agmt *ra)
  95. {
  96. Object *ro;
  97. PR_ASSERT(NULL != agmt_set);
  98. PR_ASSERT(NULL != ra);
  99. ro = objset_find(agmt_set, agmt_ptr_cmp, (const void *)ra);
  100. if (NULL != ro)
  101. {
  102. /*
  103. * Release twice - once for the reference we got when finding
  104. * it, and once for the reference we got when we called
  105. * agmtlist_get_*().
  106. */
  107. object_release(ro);
  108. object_release(ro);
  109. }
  110. }
  111. /*
  112. * Note: when we add the new object, we have a reference to it. We hold
  113. * on to this reference until the agreement is deleted (or until the
  114. * server is shut down).
  115. */
  116. int
  117. add_new_agreement(Slapi_Entry *e)
  118. {
  119. int rc = 0;
  120. Repl_Agmt *ra = agmt_new_from_entry(e);
  121. Slapi_DN *replarea_sdn = NULL;
  122. Replica *replica = NULL;
  123. Object *repl_obj = NULL;
  124. Object *ro = NULL;
  125. if (ra == NULL) return 1; /* tell search result handler callback this entry was not sent */
  126. ro = object_new((void *)ra, agmt_delete);
  127. objset_add_obj(agmt_set, ro);
  128. object_release(ro); /* Object now owned by objset */
  129. /* get the replica for this agreement */
  130. replarea_sdn = agmt_get_replarea(ra);
  131. repl_obj = replica_get_replica_from_dn(replarea_sdn);
  132. slapi_sdn_free(&replarea_sdn);
  133. if (repl_obj) {
  134. replica = (Replica*)object_get_data (repl_obj);
  135. }
  136. rc = replica_start_agreement(replica, ra);
  137. if (repl_obj) object_release(repl_obj);
  138. return rc;
  139. }
  140. static int
  141. agmtlist_add_callback(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *entryAfter,
  142. int *returncode, char *returntext, void *arg)
  143. {
  144. int rc;
  145. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmt_add: begin\n");
  146. rc = add_new_agreement(e);
  147. if (0 != rc) {
  148. char *dn;
  149. slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn);
  150. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmtlist_add_callback: "
  151. "Can't start agreement \"%s\"\n", dn);
  152. *returncode = LDAP_UNWILLING_TO_PERFORM;
  153. return SLAPI_DSE_CALLBACK_ERROR;
  154. }
  155. *returncode = LDAP_SUCCESS;
  156. return SLAPI_DSE_CALLBACK_OK;
  157. }
  158. static int
  159. agmtlist_modify_callback(Slapi_PBlock *pb, Slapi_Entry *entryBefore, Slapi_Entry *e,
  160. int *returncode, char *returntext, void *arg)
  161. {
  162. int i;
  163. char *dn;
  164. Slapi_DN *sdn = NULL;
  165. int start_initialize = 0, stop_initialize = 0, cancel_initialize = 0;
  166. int update_the_schedule = 0; /* do we need to update the repl sched? */
  167. Repl_Agmt *agmt = NULL;
  168. LDAPMod **mods;
  169. char buff [SLAPI_DSE_RETURNTEXT_SIZE];
  170. char *errortext = returntext ? returntext : buff;
  171. int rc = SLAPI_DSE_CALLBACK_OK;
  172. Slapi_Operation *op;
  173. void *identity;
  174. *returncode = LDAP_SUCCESS;
  175. /* just let internal operations originated from replication plugin to go through */
  176. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  177. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  178. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  179. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  180. {
  181. goto done;
  182. }
  183. slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn);
  184. sdn= slapi_sdn_new_dn_byref(dn);
  185. agmt = agmtlist_get_by_agmt_name(sdn);
  186. if (NULL == agmt)
  187. {
  188. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmtlist_modify_callback: received "
  189. "a modification for unknown replication agreement \"%s\"\n", dn);
  190. goto done;
  191. }
  192. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  193. for (i = 0; NULL != mods && NULL != mods[i]; i++)
  194. {
  195. if (slapi_attr_types_equivalent(mods[i]->mod_type, type_nsds5ReplicaInitialize))
  196. {
  197. /* we don't allow delete attribute operations unless it was issued by
  198. the replication plugin - handled above */
  199. if (mods[i]->mod_op & LDAP_MOD_DELETE)
  200. {
  201. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  202. "deletion of %s attribute is not allowed\n", type_nsds5ReplicaInitialize);
  203. *returncode = LDAP_UNWILLING_TO_PERFORM;
  204. rc = SLAPI_DSE_CALLBACK_ERROR;
  205. break;
  206. }
  207. else
  208. {
  209. char *val;
  210. if (mods[i]->mod_bvalues && mods[i]->mod_bvalues[0])
  211. val = slapi_berval_get_string_copy (mods[i]->mod_bvalues[0]);
  212. else
  213. {
  214. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  215. "no value provided for %s attribute\n", type_nsds5ReplicaInitialize);
  216. *returncode = LDAP_UNWILLING_TO_PERFORM;
  217. rc = SLAPI_DSE_CALLBACK_ERROR;
  218. break;
  219. }
  220. /* Start replica initialization */
  221. if (val == NULL)
  222. {
  223. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "No value supplied for attr (%s)", mods[i]->mod_type);
  224. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: %s\n",
  225. errortext);
  226. *returncode = LDAP_UNWILLING_TO_PERFORM;
  227. rc = SLAPI_DSE_CALLBACK_ERROR;
  228. break;
  229. }
  230. if (strcasecmp (val, "start") == 0)
  231. {
  232. start_initialize = 1;
  233. }
  234. else if (strcasecmp (val, "stop") == 0)
  235. {
  236. stop_initialize = 1;
  237. }
  238. else if (strcasecmp (val, "cancel") == 0)
  239. {
  240. cancel_initialize = 1;
  241. }
  242. else
  243. {
  244. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid value (%s) value supplied for attr (%s)",
  245. val, mods[i]->mod_type);
  246. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: %s\n",
  247. errortext);
  248. }
  249. slapi_ch_free ((void**)&val);
  250. }
  251. }
  252. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  253. type_nsds5ReplicaUpdateSchedule))
  254. {
  255. /*
  256. * Request to update the replication schedule. Set a flag so
  257. * we know to update the schedule later.
  258. */
  259. update_the_schedule = 1;
  260. }
  261. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  262. type_nsds5ReplicaCredentials))
  263. {
  264. /* New replica credentials */
  265. if (agmt_set_credentials_from_entry(agmt, e) != 0)
  266. {
  267. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  268. "failed to update credentials for agreement %s\n",
  269. agmt_get_long_name(agmt));
  270. *returncode = LDAP_OPERATIONS_ERROR;
  271. rc = SLAPI_DSE_CALLBACK_ERROR;
  272. }
  273. }
  274. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  275. type_nsds5ReplicaTimeout))
  276. {
  277. /* New replica timeout */
  278. if (agmt_set_timeout_from_entry(agmt, e) != 0)
  279. {
  280. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  281. "failed to update timeout for agreement %s\n",
  282. agmt_get_long_name(agmt));
  283. *returncode = LDAP_OPERATIONS_ERROR;
  284. rc = SLAPI_DSE_CALLBACK_ERROR;
  285. }
  286. }
  287. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  288. type_nsds5ReplicaBusyWaitTime))
  289. {
  290. /* New replica busywaittime */
  291. if (agmt_set_busywaittime_from_entry(agmt, e) != 0)
  292. {
  293. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  294. "failed to update busy wait time for agreement %s\n",
  295. agmt_get_long_name(agmt));
  296. *returncode = LDAP_OPERATIONS_ERROR;
  297. rc = SLAPI_DSE_CALLBACK_ERROR;
  298. }
  299. }
  300. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  301. type_nsds5ReplicaSessionPauseTime))
  302. {
  303. /* New replica pausetime */
  304. if (agmt_set_pausetime_from_entry(agmt, e) != 0)
  305. {
  306. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  307. "failed to update session pause time for agreement %s\n",
  308. agmt_get_long_name(agmt));
  309. *returncode = LDAP_OPERATIONS_ERROR;
  310. rc = SLAPI_DSE_CALLBACK_ERROR;
  311. }
  312. }
  313. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  314. type_nsds5ReplicaBindDN))
  315. {
  316. /* New replica Bind DN */
  317. if (agmt_set_binddn_from_entry(agmt, e) != 0)
  318. {
  319. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  320. "failed to update bind DN for agreement %s\n",
  321. agmt_get_long_name(agmt));
  322. *returncode = LDAP_OPERATIONS_ERROR;
  323. rc = SLAPI_DSE_CALLBACK_ERROR;
  324. }
  325. }
  326. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  327. type_nsds5TransportInfo))
  328. {
  329. /* New Transport info */
  330. if (agmt_set_transportinfo_from_entry(agmt, e) != 0)
  331. {
  332. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  333. "failed to update transport info for agreement %s\n",
  334. agmt_get_long_name(agmt));
  335. *returncode = LDAP_OPERATIONS_ERROR;
  336. rc = SLAPI_DSE_CALLBACK_ERROR;
  337. }
  338. }
  339. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  340. type_nsds5ReplicaBindMethod))
  341. {
  342. /* New replica bind method */
  343. if (agmt_set_bind_method_from_entry(agmt, e) != 0)
  344. {
  345. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  346. "failed to update bind method for agreement %s\n",
  347. agmt_get_long_name(agmt));
  348. *returncode = LDAP_OPERATIONS_ERROR;
  349. rc = SLAPI_DSE_CALLBACK_ERROR;
  350. }
  351. }
  352. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  353. type_nsds5ReplicatedAttributeList))
  354. {
  355. char **denied_attrs = NULL;
  356. /* New set of excluded attributes */
  357. if (agmt_set_replicated_attributes_from_entry(agmt, e) != 0)
  358. {
  359. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  360. "failed to update replicated attributes for agreement %s\n",
  361. agmt_get_long_name(agmt));
  362. *returncode = LDAP_OPERATIONS_ERROR;
  363. rc = SLAPI_DSE_CALLBACK_ERROR;
  364. }
  365. /* Check that there are no verboten attributes in the exclude list */
  366. denied_attrs = agmt_validate_replicated_attributes(agmt);
  367. if (denied_attrs)
  368. {
  369. /* Report the error to the client */
  370. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "attempt to exclude an illegal attribute in a fractional agreement");
  371. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  372. "attempt to exclude an illegal attribute in a fractional agreement\n");
  373. *returncode = LDAP_UNWILLING_TO_PERFORM;
  374. rc = SLAPI_DSE_CALLBACK_ERROR;
  375. /* Free the deny list if we got one */
  376. slapi_ch_array_free(denied_attrs);
  377. break;
  378. }
  379. }
  380. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  381. "nsds5debugreplicatimeout"))
  382. {
  383. char *val = slapi_entry_attr_get_charptr(e, "nsds5debugreplicatimeout");
  384. repl5_set_debug_timeout(val);
  385. slapi_ch_free_string(&val);
  386. }
  387. else if (strcasecmp (mods[i]->mod_type, "modifytimestamp") == 0 ||
  388. strcasecmp (mods[i]->mod_type, "modifiersname") == 0 ||
  389. strcasecmp (mods[i]->mod_type, "description") == 0)
  390. {
  391. /* ignore modifier's name and timestamp attributes and the description. */
  392. continue;
  393. }
  394. else if (0 == windows_handle_modify_agreement(agmt, mods[i]->mod_type, e))
  395. {
  396. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  397. "modification of %s attribute is not allowed\n", mods[i]->mod_type);
  398. *returncode = LDAP_UNWILLING_TO_PERFORM;
  399. rc = SLAPI_DSE_CALLBACK_ERROR;
  400. break;
  401. }
  402. }
  403. if (stop_initialize)
  404. {
  405. agmt_stop (agmt);
  406. }
  407. else if (start_initialize)
  408. {
  409. if (agmt_initialize_replica(agmt) != 0) {
  410. /* The suffix is disabled */
  411. agmt_set_last_init_status(agmt, 0, NSDS50_REPL_DISABLED, NULL);
  412. }
  413. }
  414. else if (cancel_initialize)
  415. {
  416. agmt_replica_init_done(agmt);
  417. }
  418. if (update_the_schedule)
  419. {
  420. if (agmt_set_schedule_from_entry(agmt, e) != 0)
  421. {
  422. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  423. "failed to update replication schedule for agreement %s\n",
  424. agmt_get_long_name(agmt));
  425. *returncode = LDAP_OPERATIONS_ERROR;
  426. rc = SLAPI_DSE_CALLBACK_ERROR;
  427. }
  428. }
  429. done:
  430. if (NULL != agmt)
  431. {
  432. agmtlist_release_agmt(agmt);
  433. }
  434. if (sdn)
  435. slapi_sdn_free(&sdn);
  436. return rc;
  437. }
  438. static int
  439. agmtlist_delete_callback(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *entryAfter,
  440. int *returncode, char *returntext, void *arg)
  441. {
  442. Repl_Agmt *ra;
  443. Object *ro;
  444. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmt_delete: begin\n");
  445. ro = objset_find(agmt_set, agmt_dn_cmp, (const void *)slapi_entry_get_sdn_const(e));
  446. ra = (NULL == ro) ? NULL : (Repl_Agmt *)object_get_data(ro);
  447. if (NULL == ra)
  448. {
  449. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmtlist_delete: "
  450. "Tried to delete replication agreement \"%s\", but no such "
  451. "agreement was configured.\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  452. }
  453. else
  454. {
  455. agmt_stop(ra);
  456. object_release(ro); /* Release ref acquired in objset_find */
  457. objset_remove_obj(agmt_set, ro); /* Releases a reference (should be final reference */
  458. }
  459. *returncode = LDAP_SUCCESS;
  460. return SLAPI_DSE_CALLBACK_OK;
  461. }
  462. static int
  463. agmtlist_rename_callback(Slapi_PBlock *pb, Slapi_Entry *entryBefore, Slapi_Entry *e,
  464. int *returncode, char *returntext, void *arg)
  465. {
  466. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmt_rename: begin\n");
  467. *returncode = LDAP_SUCCESS;
  468. return SLAPI_DSE_CALLBACK_OK;
  469. }
  470. static int
  471. handle_agmt_search(Slapi_Entry *e, void *callback_data)
  472. {
  473. int *agmtcount = (int *)callback_data;
  474. int rc;
  475. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  476. "Found replication agreement named \"%s\".\n",
  477. slapi_sdn_get_dn(slapi_entry_get_sdn(e)));
  478. rc = add_new_agreement(e);
  479. if (0 == rc)
  480. {
  481. (*agmtcount)++;
  482. }
  483. else
  484. {
  485. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "The replication "
  486. "agreement named \"%s\" could not be correctly parsed. No "
  487. "replication will occur with this replica.\n",
  488. slapi_sdn_get_dn(slapi_entry_get_sdn(e)));
  489. }
  490. return rc;
  491. }
  492. static void
  493. agmtlist_objset_destructor(void **o)
  494. {
  495. /* XXXggood Nothing to do, I think. */
  496. }
  497. int
  498. agmtlist_config_init()
  499. {
  500. Slapi_PBlock *pb;
  501. int agmtcount = 0;
  502. agmt_set = objset_new(agmtlist_objset_destructor);
  503. /* Register callbacks so we're informed about updates */
  504. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, AGMT_CONFIG_BASE,
  505. LDAP_SCOPE_SUBTREE, GLOBAL_CONFIG_FILTER, agmtlist_add_callback, NULL);
  506. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, AGMT_CONFIG_BASE,
  507. LDAP_SCOPE_SUBTREE, GLOBAL_CONFIG_FILTER, agmtlist_modify_callback, NULL);
  508. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, AGMT_CONFIG_BASE,
  509. LDAP_SCOPE_SUBTREE, GLOBAL_CONFIG_FILTER, agmtlist_delete_callback, NULL);
  510. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, AGMT_CONFIG_BASE,
  511. LDAP_SCOPE_SUBTREE, GLOBAL_CONFIG_FILTER, agmtlist_rename_callback, NULL);
  512. /* Search the DIT and find all the replication agreements */
  513. pb = slapi_pblock_new();
  514. slapi_search_internal_set_pb(pb, AGMT_CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  515. GLOBAL_CONFIG_FILTER, NULL /* attrs */, 0 /* attrsonly */,
  516. NULL, /* controls */ NULL /* uniqueid */,
  517. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0 /* actions */);
  518. slapi_search_internal_callback_pb(pb,
  519. (void *)&agmtcount /* callback data */,
  520. NULL /* result_callback */,
  521. handle_agmt_search /* search entry cb */,
  522. NULL /* referral callback */);
  523. slapi_pblock_destroy(pb);
  524. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_config_init: found %d replication agreements in DIT\n", agmtcount);
  525. return 0;
  526. }
  527. void
  528. agmtlist_shutdown()
  529. {
  530. Repl_Agmt *ra;
  531. Object *ro;
  532. Object *next_ro;
  533. ro = objset_first_obj(agmt_set);
  534. while (NULL != ro)
  535. {
  536. next_ro = objset_next_obj(agmt_set, ro);
  537. ra = (Repl_Agmt *)object_get_data(ro);
  538. agmt_stop(ra);
  539. agmt_update_consumer_ruv (ra);
  540. objset_remove_obj(agmt_set, ro);
  541. ro = next_ro;
  542. }
  543. objset_delete(&agmt_set);
  544. agmt_set = NULL;
  545. }
  546. /*
  547. * Notify each replication agreement about an update.
  548. */
  549. void
  550. agmtlist_notify_all(Slapi_PBlock *pb)
  551. {
  552. Repl_Agmt *ra;
  553. Object *ro;
  554. if (NULL != agmt_set)
  555. {
  556. ro = objset_first_obj(agmt_set);
  557. while (NULL != ro)
  558. {
  559. ra = (Repl_Agmt *)object_get_data(ro);
  560. agmt_notify_change(ra, pb);
  561. ro = objset_next_obj(agmt_set, ro);
  562. }
  563. }
  564. }
  565. Object* agmtlist_get_first_agreement_for_replica (Replica *r)
  566. {
  567. return agmtlist_get_next_agreement_for_replica (r, NULL) ;
  568. }
  569. Object* agmtlist_get_next_agreement_for_replica (Replica *r, Object *prev)
  570. {
  571. const Slapi_DN *replica_root;
  572. Slapi_DN *agmt_root;
  573. Object *obj;
  574. Repl_Agmt *agmt;
  575. if (r == NULL)
  576. {
  577. /* ONREPL - log error */
  578. return NULL;
  579. }
  580. replica_root = replica_get_root(r);
  581. if (prev)
  582. obj = objset_next_obj(agmt_set, prev);
  583. else
  584. obj = objset_first_obj(agmt_set);
  585. while (obj)
  586. {
  587. agmt = (Repl_Agmt*)object_get_data (obj);
  588. PR_ASSERT (agmt);
  589. agmt_root = agmt_get_replarea(agmt);
  590. PR_ASSERT (agmt_root);
  591. if (slapi_sdn_compare (replica_root, agmt_root) == 0)
  592. {
  593. slapi_sdn_free (&agmt_root);
  594. return obj;
  595. }
  596. slapi_sdn_free (&agmt_root);
  597. obj = objset_next_obj(agmt_set, obj);
  598. }
  599. return NULL;
  600. }