1
0

repl5_agmtlist.c 23 KB

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