repl5_agmtlist.c 23 KB

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