repl5_agmtlist.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. /* tell search result handler callback this entry was not sent */
  131. if (ra == NULL)
  132. return 1;
  133. ro = object_new((void *)ra, agmt_delete);
  134. objset_add_obj(agmt_set, ro);
  135. object_release(ro); /* Object now owned by objset */
  136. /* get the replica for this agreement */
  137. replarea_sdn = agmt_get_replarea(ra);
  138. repl_obj = replica_get_replica_from_dn(replarea_sdn);
  139. slapi_sdn_free(&replarea_sdn);
  140. if (repl_obj) {
  141. replica = (Replica*)object_get_data (repl_obj);
  142. }
  143. rc = replica_start_agreement(replica, ra);
  144. if (repl_obj) object_release(repl_obj);
  145. return rc;
  146. }
  147. static int
  148. agmtlist_add_callback(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *entryAfter,
  149. int *returncode, char *returntext, void *arg)
  150. {
  151. int rc;
  152. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmt_add: begin\n");
  153. rc = add_new_agreement(e);
  154. if (0 != rc) {
  155. Slapi_DN *sdn = NULL;
  156. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  157. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmtlist_add_callback: "
  158. "Can't start agreement \"%s\"\n", slapi_sdn_get_dn(sdn));
  159. *returncode = LDAP_UNWILLING_TO_PERFORM;
  160. return SLAPI_DSE_CALLBACK_ERROR;
  161. }
  162. *returncode = LDAP_SUCCESS;
  163. return SLAPI_DSE_CALLBACK_OK;
  164. }
  165. static int
  166. agmtlist_modify_callback(Slapi_PBlock *pb, Slapi_Entry *entryBefore, Slapi_Entry *e,
  167. int *returncode, char *returntext, void *arg)
  168. {
  169. int i;
  170. Slapi_DN *sdn = NULL;
  171. int start_initialize = 0, stop_initialize = 0, cancel_initialize = 0;
  172. int update_the_schedule = 0; /* do we need to update the repl sched? */
  173. Repl_Agmt *agmt = NULL;
  174. LDAPMod **mods;
  175. char buff [SLAPI_DSE_RETURNTEXT_SIZE];
  176. char *errortext = returntext ? returntext : buff;
  177. char *val = NULL;
  178. int rc = SLAPI_DSE_CALLBACK_OK;
  179. Slapi_Operation *op;
  180. void *identity;
  181. *returncode = LDAP_SUCCESS;
  182. /* just let internal operations originated from replication plugin to go through */
  183. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  184. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  185. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  186. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  187. {
  188. goto done;
  189. }
  190. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  191. if (NULL == sdn) {
  192. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  193. "agmtlist_modify_callback: NULL target dn\n");
  194. goto done;
  195. }
  196. agmt = agmtlist_get_by_agmt_name(sdn);
  197. if (NULL == agmt)
  198. {
  199. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmtlist_modify_callback: received "
  200. "a modification for unknown replication agreement \"%s\"\n",
  201. slapi_sdn_get_dn(sdn));
  202. goto done;
  203. }
  204. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  205. for (i = 0; NULL != mods && NULL != mods[i]; i++)
  206. {
  207. slapi_ch_free_string(&val);
  208. if (mods[i]->mod_bvalues && mods[i]->mod_bvalues[0]){
  209. val = slapi_berval_get_string_copy (mods[i]->mod_bvalues[0]);
  210. }
  211. if (slapi_attr_types_equivalent(mods[i]->mod_type, type_nsds5ReplicaInitialize))
  212. {
  213. /* we don't allow delete attribute operations unless it was issued by
  214. the replication plugin - handled above */
  215. if (mods[i]->mod_op & LDAP_MOD_DELETE)
  216. {
  217. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  218. "deletion of %s attribute is not allowed\n", type_nsds5ReplicaInitialize);
  219. *returncode = LDAP_UNWILLING_TO_PERFORM;
  220. rc = SLAPI_DSE_CALLBACK_ERROR;
  221. break;
  222. }
  223. else
  224. {
  225. if(val == NULL){
  226. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  227. "no value provided for %s attribute\n", type_nsds5ReplicaInitialize);
  228. *returncode = LDAP_UNWILLING_TO_PERFORM;
  229. rc = SLAPI_DSE_CALLBACK_ERROR;
  230. break;
  231. }
  232. /* Start replica initialization */
  233. if (strcasecmp (val, "start") == 0)
  234. {
  235. start_initialize = 1;
  236. }
  237. else if (strcasecmp (val, "stop") == 0)
  238. {
  239. stop_initialize = 1;
  240. }
  241. else if (strcasecmp (val, "cancel") == 0)
  242. {
  243. cancel_initialize = 1;
  244. }
  245. else
  246. {
  247. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  248. "Invalid value (%s) value supplied for attr (%s); Ignoring ...",
  249. val, mods[i]->mod_type);
  250. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: %s\n", errortext);
  251. *returncode = LDAP_UNWILLING_TO_PERFORM;
  252. rc = SLAPI_DSE_CALLBACK_ERROR;
  253. }
  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, type_nsds5ReplicaHost))
  331. {
  332. /* New replica host */
  333. if (agmt_set_host_from_entry(agmt, e) != 0)
  334. {
  335. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  336. "agmtlist_modify_callback: "
  337. "failed to update host for agreement %s\n",
  338. agmt_get_long_name(agmt));
  339. *returncode = LDAP_OPERATIONS_ERROR;
  340. rc = SLAPI_DSE_CALLBACK_ERROR;
  341. } else {
  342. /*
  343. * Changing the host invalidates the agmt maxcsn, so remove it.
  344. * The next update will add the correct maxcsn back to the agmt/local ruv.
  345. */
  346. agmt_remove_maxcsn(agmt);
  347. }
  348. }
  349. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  350. type_nsds5ReplicaPort))
  351. {
  352. /* New replica port */
  353. if (agmt_set_port_from_entry(agmt, e) != 0)
  354. {
  355. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  356. "agmtlist_modify_callback: "
  357. "failed to update port for agreement %s\n",
  358. agmt_get_long_name(agmt));
  359. *returncode = LDAP_OPERATIONS_ERROR;
  360. rc = SLAPI_DSE_CALLBACK_ERROR;
  361. } else {
  362. /*
  363. * Changing the port invalidates the agmt maxcsn, so remove it.
  364. * The next update will add the correct maxcsn back to the agmt/local ruv.
  365. */
  366. agmt_remove_maxcsn(agmt);
  367. }
  368. }
  369. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  370. type_nsds5TransportInfo))
  371. {
  372. /* New Transport info */
  373. if (agmt_set_transportinfo_from_entry(agmt, e) != 0)
  374. {
  375. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  376. "failed to update transport info for agreement %s\n",
  377. agmt_get_long_name(agmt));
  378. *returncode = LDAP_OPERATIONS_ERROR;
  379. rc = SLAPI_DSE_CALLBACK_ERROR;
  380. }
  381. }
  382. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  383. type_nsds5ReplicaBindMethod))
  384. {
  385. if (agmt_set_bind_method_from_entry(agmt, e) != 0)
  386. {
  387. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  388. "failed to update bind method for agreement %s\n",
  389. agmt_get_long_name(agmt));
  390. *returncode = LDAP_OPERATIONS_ERROR;
  391. rc = SLAPI_DSE_CALLBACK_ERROR;
  392. }
  393. }
  394. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  395. type_nsds5ReplicatedAttributeList))
  396. {
  397. char **denied_attrs = NULL;
  398. /* New set of excluded attributes */
  399. if (agmt_set_replicated_attributes_from_entry(agmt, e) != 0)
  400. {
  401. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  402. "failed to update replicated attributes for agreement %s\n",
  403. agmt_get_long_name(agmt));
  404. *returncode = LDAP_OPERATIONS_ERROR;
  405. rc = SLAPI_DSE_CALLBACK_ERROR;
  406. }
  407. /* Check that there are no verboten attributes in the exclude list */
  408. denied_attrs = agmt_validate_replicated_attributes(agmt, 0 /* incremental */);
  409. if (denied_attrs)
  410. {
  411. /* Report the error to the client */
  412. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "attempt to exclude an illegal attribute in a fractional agreement");
  413. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  414. "attempt to exclude an illegal attribute in a fractional agreement\n");
  415. *returncode = LDAP_UNWILLING_TO_PERFORM;
  416. rc = SLAPI_DSE_CALLBACK_ERROR;
  417. /* Free the deny list if we got one */
  418. slapi_ch_array_free(denied_attrs);
  419. break;
  420. }
  421. }
  422. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  423. type_nsds5ReplicatedAttributeListTotal))
  424. {
  425. char **denied_attrs = NULL;
  426. /* New set of excluded attributes */
  427. if (agmt_set_replicated_attributes_total_from_entry(agmt, e) != 0)
  428. {
  429. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  430. "failed to update total update replicated attributes for agreement %s\n",
  431. agmt_get_long_name(agmt));
  432. *returncode = LDAP_OPERATIONS_ERROR;
  433. rc = SLAPI_DSE_CALLBACK_ERROR;
  434. }
  435. /* Check that there are no verboten attributes in the exclude list */
  436. denied_attrs = agmt_validate_replicated_attributes(agmt, 1 /* total */);
  437. if (denied_attrs)
  438. {
  439. /* Report the error to the client */
  440. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "attempt to exclude an illegal total update "
  441. "attribute in a fractional agreement");
  442. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  443. "attempt to exclude an illegal total update attribute in a fractional agreement\n");
  444. *returncode = LDAP_UNWILLING_TO_PERFORM;
  445. rc = SLAPI_DSE_CALLBACK_ERROR;
  446. /* Free the deny list if we got one */
  447. slapi_ch_array_free(denied_attrs);
  448. break;
  449. }
  450. }
  451. else if (slapi_attr_types_equivalent(mods[i]->mod_type,
  452. "nsds5debugreplicatimeout"))
  453. {
  454. char *val = slapi_entry_attr_get_charptr(e, "nsds5debugreplicatimeout");
  455. repl5_set_debug_timeout(val);
  456. slapi_ch_free_string(&val);
  457. }
  458. else if (slapi_attr_is_last_mod(mods[i]->mod_type) ||
  459. strcasecmp (mods[i]->mod_type, "description") == 0)
  460. {
  461. /* ignore modifier's name and timestamp attributes and the description. */
  462. continue;
  463. }
  464. else if (slapi_attr_types_equivalent(mods[i]->mod_type, type_nsds5ReplicaEnabled))
  465. {
  466. if(agmt_set_enabled_from_entry(agmt, e, returntext) != 0){
  467. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  468. "failed to set replica agmt state \"enabled/disabled\" for %s\n",agmt_get_long_name(agmt));
  469. *returncode = LDAP_OPERATIONS_ERROR;
  470. rc = SLAPI_DSE_CALLBACK_ERROR;
  471. }
  472. }
  473. else if (slapi_attr_types_equivalent(mods[i]->mod_type, type_nsds5ReplicaStripAttrs))
  474. {
  475. if(agmt_set_attrs_to_strip(agmt, e) != 0){
  476. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  477. "failed to set replica agmt attributes to strip for %s\n",agmt_get_long_name(agmt));
  478. *returncode = LDAP_OPERATIONS_ERROR;
  479. rc = SLAPI_DSE_CALLBACK_ERROR;
  480. }
  481. }
  482. else if (slapi_attr_types_equivalent(mods[i]->mod_type, type_replicaProtocolTimeout)){
  483. if (mods[i]->mod_op & LDAP_MOD_DELETE)
  484. {
  485. agmt_set_protocol_timeout(agmt, 0);
  486. }
  487. else
  488. {
  489. long ptimeout = 0;
  490. if (val){
  491. ptimeout = atol(val);
  492. }
  493. if(ptimeout <= 0){
  494. *returncode = LDAP_UNWILLING_TO_PERFORM;
  495. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  496. "attribute %s value (%s) is invalid, must be a number greater than zero.\n",
  497. type_replicaProtocolTimeout, val ? val : "");
  498. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "attribute %s value (%s) is invalid, "
  499. "must be a number greater than zero.\n",
  500. type_replicaProtocolTimeout, val ? val : "");
  501. rc = SLAPI_DSE_CALLBACK_ERROR;
  502. break;
  503. }
  504. agmt_set_protocol_timeout(agmt, ptimeout);
  505. }
  506. }
  507. else if (0 == windows_handle_modify_agreement(agmt, mods[i]->mod_type, e))
  508. {
  509. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  510. "modification of %s attribute is not allowed\n", mods[i]->mod_type);
  511. *returncode = LDAP_UNWILLING_TO_PERFORM;
  512. rc = SLAPI_DSE_CALLBACK_ERROR;
  513. break;
  514. }
  515. }
  516. if (stop_initialize)
  517. {
  518. agmt_stop (agmt);
  519. }
  520. else if (start_initialize)
  521. {
  522. if (agmt_initialize_replica(agmt) != 0) {
  523. /* The suffix/repl agmt is disabled */
  524. agmt_set_last_init_status(agmt, 0, NSDS50_REPL_DISABLED, NULL);
  525. if(agmt_is_enabled(agmt)){
  526. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Suffix is disabled");
  527. } else {
  528. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replication agreement is disabled");
  529. }
  530. *returncode = LDAP_UNWILLING_TO_PERFORM;
  531. rc = SLAPI_DSE_CALLBACK_ERROR;
  532. }
  533. }
  534. else if (cancel_initialize)
  535. {
  536. agmt_replica_init_done(agmt);
  537. }
  538. if (update_the_schedule)
  539. {
  540. if (agmt_set_schedule_from_entry(agmt, e) != 0)
  541. {
  542. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_modify_callback: "
  543. "failed to update replication schedule for agreement %s\n",
  544. agmt_get_long_name(agmt));
  545. *returncode = LDAP_OPERATIONS_ERROR;
  546. rc = SLAPI_DSE_CALLBACK_ERROR;
  547. }
  548. }
  549. done:
  550. if (NULL != agmt)
  551. {
  552. agmtlist_release_agmt(agmt);
  553. }
  554. slapi_ch_free_string(&val);
  555. return rc;
  556. }
  557. static int
  558. agmtlist_delete_callback(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *entryAfter,
  559. int *returncode, char *returntext, void *arg)
  560. {
  561. Repl_Agmt *ra;
  562. Object *ro;
  563. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmt_delete: begin\n");
  564. ro = objset_find(agmt_set, agmt_dn_cmp, (const void *)slapi_entry_get_sdn_const(e));
  565. ra = (NULL == ro) ? NULL : (Repl_Agmt *)object_get_data(ro);
  566. if (NULL == ra)
  567. {
  568. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmtlist_delete: "
  569. "Tried to delete replication agreement \"%s\", but no such "
  570. "agreement was configured.\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  571. }
  572. else
  573. {
  574. agmt_remove_maxcsn(ra); /* remove the agmt maxcsn from the localruv */
  575. agmt_stop(ra);
  576. object_release(ro); /* Release ref acquired in objset_find */
  577. objset_remove_obj(agmt_set, ro); /* Releases a reference (should be final reference */
  578. }
  579. *returncode = LDAP_SUCCESS;
  580. return SLAPI_DSE_CALLBACK_OK;
  581. }
  582. static int
  583. agmtlist_rename_callback(Slapi_PBlock *pb, Slapi_Entry *entryBefore, Slapi_Entry *e,
  584. int *returncode, char *returntext, void *arg)
  585. {
  586. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "agmt_rename: begin\n");
  587. *returncode = LDAP_SUCCESS;
  588. return SLAPI_DSE_CALLBACK_OK;
  589. }
  590. static int
  591. handle_agmt_search(Slapi_Entry *e, void *callback_data)
  592. {
  593. int *agmtcount = (int *)callback_data;
  594. int rc;
  595. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  596. "Found replication agreement named \"%s\".\n",
  597. slapi_sdn_get_dn(slapi_entry_get_sdn(e)));
  598. rc = add_new_agreement(e);
  599. if (0 == rc)
  600. {
  601. (*agmtcount)++;
  602. }
  603. else
  604. {
  605. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "The replication "
  606. "agreement named \"%s\" could not be correctly parsed. No "
  607. "replication will occur with this replica.\n",
  608. slapi_sdn_get_dn(slapi_entry_get_sdn(e)));
  609. }
  610. return rc;
  611. }
  612. static void
  613. agmtlist_objset_destructor(void **o)
  614. {
  615. /* XXXggood Nothing to do, I think. */
  616. }
  617. int
  618. agmtlist_config_init()
  619. {
  620. Slapi_PBlock *pb;
  621. int agmtcount = 0;
  622. agmt_set = objset_new(agmtlist_objset_destructor);
  623. /* Register callbacks so we're informed about updates */
  624. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, AGMT_CONFIG_BASE,
  625. LDAP_SCOPE_SUBTREE, GLOBAL_CONFIG_FILTER, agmtlist_add_callback, NULL);
  626. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, AGMT_CONFIG_BASE,
  627. LDAP_SCOPE_SUBTREE, GLOBAL_CONFIG_FILTER, agmtlist_modify_callback, NULL);
  628. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, AGMT_CONFIG_BASE,
  629. LDAP_SCOPE_SUBTREE, GLOBAL_CONFIG_FILTER, agmtlist_delete_callback, NULL);
  630. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, AGMT_CONFIG_BASE,
  631. LDAP_SCOPE_SUBTREE, GLOBAL_CONFIG_FILTER, agmtlist_rename_callback, NULL);
  632. /* Search the DIT and find all the replication agreements */
  633. pb = slapi_pblock_new();
  634. slapi_search_internal_set_pb(pb, AGMT_CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  635. GLOBAL_CONFIG_FILTER, NULL /* attrs */, 0 /* attrsonly */,
  636. NULL, /* controls */ NULL /* uniqueid */,
  637. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0 /* actions */);
  638. slapi_search_internal_callback_pb(pb,
  639. (void *)&agmtcount /* callback data */,
  640. NULL /* result_callback */,
  641. handle_agmt_search /* search entry cb */,
  642. NULL /* referral callback */);
  643. slapi_pblock_destroy(pb);
  644. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "agmtlist_config_init: found %d replication agreements in DIT\n", agmtcount);
  645. return 0;
  646. }
  647. void
  648. agmtlist_shutdown()
  649. {
  650. Repl_Agmt *ra;
  651. Object *ro;
  652. Object *next_ro;
  653. ro = objset_first_obj(agmt_set);
  654. while (NULL != ro)
  655. {
  656. ra = (Repl_Agmt *)object_get_data(ro);
  657. agmt_stop(ra);
  658. agmt_update_consumer_ruv (ra);
  659. next_ro = objset_next_obj(agmt_set, ro);
  660. /* Object ro was released in objset_next_obj,
  661. * but the address ro can be still used to remove ro from objset. */
  662. objset_remove_obj(agmt_set, ro);
  663. ro = next_ro;
  664. }
  665. objset_delete(&agmt_set);
  666. agmt_set = NULL;
  667. }
  668. /*
  669. * Notify each replication agreement about an update.
  670. */
  671. void
  672. agmtlist_notify_all(Slapi_PBlock *pb)
  673. {
  674. Repl_Agmt *ra;
  675. Object *ro;
  676. if (NULL != agmt_set)
  677. {
  678. ro = objset_first_obj(agmt_set);
  679. while (NULL != ro)
  680. {
  681. ra = (Repl_Agmt *)object_get_data(ro);
  682. agmt_notify_change(ra, pb);
  683. ro = objset_next_obj(agmt_set, ro);
  684. }
  685. }
  686. }
  687. Object* agmtlist_get_first_agreement_for_replica (Replica *r)
  688. {
  689. return agmtlist_get_next_agreement_for_replica (r, NULL) ;
  690. }
  691. Object* agmtlist_get_next_agreement_for_replica (Replica *r, Object *prev)
  692. {
  693. const Slapi_DN *replica_root;
  694. Slapi_DN *agmt_root;
  695. Object *obj;
  696. Repl_Agmt *agmt;
  697. if (r == NULL)
  698. {
  699. /* ONREPL - log error */
  700. return NULL;
  701. }
  702. replica_root = replica_get_root(r);
  703. if (prev)
  704. obj = objset_next_obj(agmt_set, prev);
  705. else
  706. obj = objset_first_obj(agmt_set);
  707. while (obj)
  708. {
  709. agmt = (Repl_Agmt*)object_get_data (obj);
  710. PR_ASSERT (agmt);
  711. agmt_root = agmt_get_replarea(agmt);
  712. PR_ASSERT (agmt_root);
  713. if (slapi_sdn_compare (replica_root, agmt_root) == 0)
  714. {
  715. slapi_sdn_free (&agmt_root);
  716. return obj;
  717. }
  718. slapi_sdn_free (&agmt_root);
  719. obj = objset_next_obj(agmt_set, obj);
  720. }
  721. return NULL;
  722. }