repl5_replica_config.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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_replica_config.c - replica configuration over ldap */
  42. #include <ctype.h> /* for isdigit() */
  43. #include "repl.h" /* ONREPL - this is bad */
  44. #include "repl5.h"
  45. #include "cl5_api.h"
  46. /* CONFIG_BASE: no need to optimize */
  47. #define CONFIG_BASE "cn=mapping tree,cn=config"
  48. #define CONFIG_FILTER "(objectclass=nsDS5Replica)"
  49. #define TASK_ATTR "nsds5Task"
  50. #define CL2LDIF_TASK "CL2LDIF"
  51. #define CLEANRUV "CLEANRUV"
  52. #define CLEANRUVLEN 8
  53. int slapi_log_urp = SLAPI_LOG_REPL;
  54. /* Forward Declartions */
  55. static int replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  56. static int replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  57. static int replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  58. static int replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  59. static int replica_config_change_type_and_id (Replica *r, const char *new_type, const char *new_id, char *returntext, int apply_mods);
  60. static int replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext, int apply_mods);
  61. static int replica_config_change_flags (Replica *r, const char *new_flags, char *returntext, int apply_mods);
  62. static int replica_execute_task (Object *r, const char *task_name, char *returntext, int apply_mods);
  63. static int replica_execute_cl2ldif_task (Object *r, char *returntext);
  64. static int replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext);
  65. static multimaster_mtnode_extension * _replica_config_get_mtnode_ext (const Slapi_Entry *e);
  66. static PRLock *s_configLock;
  67. static int
  68. dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  69. {
  70. *returncode = LDAP_UNWILLING_TO_PERFORM;
  71. return SLAPI_DSE_CALLBACK_ERROR;
  72. }
  73. int
  74. replica_config_init()
  75. {
  76. s_configLock = PR_NewLock ();
  77. if (s_configLock == NULL)
  78. {
  79. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  80. "failed to cretate configuration lock; NSPR error - %d\n",
  81. PR_GetError ());
  82. return -1;
  83. }
  84. /* config DSE must be initialized before we get here */
  85. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  86. CONFIG_FILTER, replica_config_add, NULL);
  87. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  88. CONFIG_FILTER, replica_config_modify,NULL);
  89. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  90. CONFIG_FILTER, dont_allow_that, NULL);
  91. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  92. CONFIG_FILTER, replica_config_delete,NULL);
  93. slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  94. CONFIG_FILTER, replica_config_search,NULL);
  95. return 0;
  96. }
  97. void
  98. replica_config_destroy ()
  99. {
  100. if (s_configLock)
  101. {
  102. PR_DestroyLock (s_configLock);
  103. s_configLock = NULL;
  104. }
  105. /* config DSE must be initialized before we get here */
  106. slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  107. CONFIG_FILTER, replica_config_add);
  108. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  109. CONFIG_FILTER, replica_config_modify);
  110. slapi_config_remove_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  111. CONFIG_FILTER, dont_allow_that);
  112. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  113. CONFIG_FILTER, replica_config_delete);
  114. slapi_config_remove_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  115. CONFIG_FILTER, replica_config_search);
  116. }
  117. static int
  118. replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  119. int *returncode, char *errorbuf, void *arg)
  120. {
  121. Replica *r = NULL;
  122. multimaster_mtnode_extension *mtnode_ext;
  123. char *replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  124. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  125. char *errortext = errorbuf ? errorbuf : buf;
  126. if (errorbuf)
  127. {
  128. errorbuf[0] = '\0';
  129. }
  130. *returncode = LDAP_SUCCESS;
  131. PR_Lock (s_configLock);
  132. /* add the dn to the dn hash so we can tell this replica is being configured */
  133. replica_add_by_dn(replica_root);
  134. mtnode_ext = _replica_config_get_mtnode_ext (e);
  135. PR_ASSERT (mtnode_ext);
  136. if (mtnode_ext->replica)
  137. {
  138. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica already configured for %s", replica_root);
  139. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: %s\n", errortext);
  140. *returncode = LDAP_UNWILLING_TO_PERFORM;
  141. goto done;
  142. }
  143. /* create replica object */
  144. r = replica_new_from_entry (e, errortext, PR_TRUE /* is a newly added entry */);
  145. if (r == NULL)
  146. {
  147. *returncode = LDAP_OPERATIONS_ERROR;
  148. goto done;
  149. }
  150. /* Set the mapping tree node state, and the referrals from the RUV */
  151. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  152. if (!replica_is_legacy_consumer (r))
  153. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  154. /* ONREPL if replica is added as writable we need to execute protocol that
  155. introduces new writable replica to the topology */
  156. mtnode_ext->replica = object_new (r, replica_destroy); /* Refcnt is 1 */
  157. /* add replica object to the hash */
  158. *returncode = replica_add_by_name (replica_get_name (r), mtnode_ext->replica); /* Increments object refcnt */
  159. /* delete the dn from the dn hash - done with configuration */
  160. replica_delete_by_dn(replica_root);
  161. done:
  162. PR_Unlock (s_configLock);
  163. /* slapi_ch_free accepts NULL pointer */
  164. slapi_ch_free ((void**)&replica_root);
  165. if (*returncode != LDAP_SUCCESS)
  166. {
  167. if (mtnode_ext->replica)
  168. object_release (mtnode_ext->replica);
  169. return SLAPI_DSE_CALLBACK_ERROR;
  170. }
  171. else
  172. return SLAPI_DSE_CALLBACK_OK;
  173. }
  174. static int
  175. replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  176. int *returncode, char *returntext, void *arg)
  177. {
  178. int rc= 0;
  179. LDAPMod **mods;
  180. int i, apply_mods;
  181. multimaster_mtnode_extension *mtnode_ext;
  182. Replica *r = NULL;
  183. char *replica_root = NULL;
  184. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  185. char *errortext = returntext ? returntext : buf;
  186. char *config_attr, *config_attr_value;
  187. Slapi_Operation *op;
  188. void *identity;
  189. if (returntext)
  190. {
  191. returntext[0] = '\0';
  192. }
  193. *returncode = LDAP_SUCCESS;
  194. /* just let internal operations originated from replication plugin to go through */
  195. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  196. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  197. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  198. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  199. {
  200. *returncode = LDAP_SUCCESS;
  201. return SLAPI_DSE_CALLBACK_OK;
  202. }
  203. replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  204. PR_Lock (s_configLock);
  205. mtnode_ext = _replica_config_get_mtnode_ext (e);
  206. PR_ASSERT (mtnode_ext);
  207. if (mtnode_ext->replica)
  208. object_acquire (mtnode_ext->replica);
  209. if (mtnode_ext->replica == NULL)
  210. {
  211. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica does not exist for %s", replica_root);
  212. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  213. errortext);
  214. *returncode = LDAP_OPERATIONS_ERROR;
  215. goto done;
  216. }
  217. r = object_get_data (mtnode_ext->replica);
  218. PR_ASSERT (r);
  219. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  220. for (apply_mods = 0; apply_mods <= 1; apply_mods++)
  221. {
  222. /* we only allow the replica ID and type to be modified together e.g.
  223. if converting a read only replica to a master or vice versa -
  224. we will need to change both the replica ID and the type at the same
  225. time - we must disallow changing the replica ID if the type is not
  226. being changed and vice versa
  227. */
  228. char *new_repl_id = NULL;
  229. char *new_repl_type = NULL;
  230. if (*returncode != LDAP_SUCCESS)
  231. break;
  232. for (i = 0; (mods[i] && (LDAP_SUCCESS == rc)); i++)
  233. {
  234. if (*returncode != LDAP_SUCCESS)
  235. break;
  236. config_attr = (char *) mods[i]->mod_type;
  237. PR_ASSERT (config_attr);
  238. /* disallow modifications or removal of replica root,
  239. replica name and replica state attributes */
  240. if (strcasecmp (config_attr, attr_replicaRoot) == 0 ||
  241. strcasecmp (config_attr, attr_replicaName) == 0 ||
  242. strcasecmp (config_attr, attr_state) == 0)
  243. {
  244. *returncode = LDAP_UNWILLING_TO_PERFORM;
  245. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "modification of %s attribute is not allowed",
  246. config_attr);
  247. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  248. errortext);
  249. }
  250. /* this is a request to delete an attribute */
  251. else if (mods[i]->mod_op & LDAP_MOD_DELETE || mods[i]->mod_bvalues == NULL
  252. || mods[i]->mod_bvalues[0]->bv_val == NULL)
  253. {
  254. /* currently, you can only remove referral,
  255. legacy consumer or bind dn attribute */
  256. if (strcasecmp (config_attr, attr_replicaBindDn) == 0)
  257. {
  258. *returncode = replica_config_change_updatedn (r, mods[i], errortext, apply_mods);
  259. }
  260. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  261. {
  262. if (apply_mods) {
  263. replica_set_referrals(r, NULL);
  264. if (!replica_is_legacy_consumer (r)) {
  265. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  266. }
  267. }
  268. }
  269. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  270. {
  271. if (apply_mods)
  272. replica_set_legacy_consumer (r, PR_FALSE);
  273. }
  274. else
  275. {
  276. *returncode = LDAP_UNWILLING_TO_PERFORM;
  277. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "deletion of %s attribute is not allowed", config_attr);
  278. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  279. errortext);
  280. }
  281. }
  282. else /* modify an attribute */
  283. {
  284. config_attr_value = (char *) mods[i]->mod_bvalues[0]->bv_val;
  285. if (strcasecmp (config_attr, attr_replicaBindDn) == 0)
  286. {
  287. *returncode = replica_config_change_updatedn (r, mods[i],
  288. errortext, apply_mods);
  289. }
  290. else if (strcasecmp (config_attr, attr_replicaType) == 0)
  291. {
  292. new_repl_type = slapi_ch_strdup(config_attr_value);
  293. }
  294. else if (strcasecmp (config_attr, attr_replicaId) == 0)
  295. {
  296. new_repl_id = slapi_ch_strdup(config_attr_value);
  297. }
  298. else if (strcasecmp (config_attr, attr_flags) == 0)
  299. {
  300. *returncode = replica_config_change_flags (r, config_attr_value,
  301. errortext, apply_mods);
  302. }
  303. else if (strcasecmp (config_attr, TASK_ATTR) == 0)
  304. {
  305. *returncode = replica_execute_task (mtnode_ext->replica, config_attr_value,
  306. errortext, apply_mods);
  307. }
  308. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  309. {
  310. if (apply_mods)
  311. {
  312. Slapi_Mod smod;
  313. Slapi_ValueSet *vs= slapi_valueset_new();
  314. slapi_mod_init_byref(&smod,mods[i]);
  315. slapi_valueset_set_from_smod(vs, &smod);
  316. replica_set_referrals (r, vs);
  317. slapi_mod_done(&smod);
  318. slapi_valueset_free(vs);
  319. if (!replica_is_legacy_consumer (r)) {
  320. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  321. }
  322. }
  323. }
  324. else if (strcasecmp (config_attr, type_replicaPurgeDelay) == 0)
  325. {
  326. if (apply_mods && config_attr_value && config_attr_value[0])
  327. {
  328. PRUint32 delay;
  329. if (isdigit (config_attr_value[0]))
  330. {
  331. delay = (unsigned int)atoi(config_attr_value);
  332. replica_set_purge_delay(r, delay);
  333. }
  334. else
  335. *returncode = LDAP_OPERATIONS_ERROR;
  336. }
  337. }
  338. else if (strcasecmp (config_attr, type_replicaTombstonePurgeInterval) == 0)
  339. {
  340. if (apply_mods && config_attr_value && config_attr_value[0])
  341. {
  342. long interval;
  343. interval = atol (config_attr_value);
  344. replica_set_tombstone_reap_interval (r, interval);
  345. }
  346. }
  347. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  348. {
  349. if (apply_mods)
  350. {
  351. PRBool legacy = (strcasecmp (config_attr_value, "on") == 0) ||
  352. (strcasecmp (config_attr_value, "true") == 0) ||
  353. (strcasecmp (config_attr_value, "yes") == 0) ||
  354. (strcasecmp (config_attr_value, "1") == 0);
  355. replica_set_legacy_consumer (r, legacy);
  356. }
  357. }
  358. /* ignore modifiers attributes added by the server */
  359. else if (strcasecmp (config_attr, "modifytimestamp") == 0 ||
  360. strcasecmp (config_attr, "modifiersname") == 0)
  361. {
  362. *returncode = LDAP_SUCCESS;
  363. }
  364. else
  365. {
  366. *returncode = LDAP_UNWILLING_TO_PERFORM;
  367. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  368. "modification of attribute %s is not allowed in replica entry", config_attr);
  369. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  370. errortext);
  371. }
  372. }
  373. }
  374. if (new_repl_id || new_repl_type)
  375. {
  376. *returncode = replica_config_change_type_and_id(r, new_repl_type,
  377. new_repl_id, errortext,
  378. apply_mods);
  379. slapi_ch_free_string(&new_repl_id);
  380. slapi_ch_free_string(&new_repl_type);
  381. }
  382. }
  383. done:
  384. if (mtnode_ext->replica)
  385. object_release (mtnode_ext->replica);
  386. /* slapi_ch_free accepts NULL pointer */
  387. slapi_ch_free ((void**)&replica_root);
  388. PR_Unlock (s_configLock);
  389. if (*returncode != LDAP_SUCCESS)
  390. {
  391. return SLAPI_DSE_CALLBACK_ERROR;
  392. }
  393. else
  394. {
  395. return SLAPI_DSE_CALLBACK_OK;
  396. }
  397. }
  398. static int
  399. replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  400. int *returncode, char *returntext, void *arg)
  401. {
  402. multimaster_mtnode_extension *mtnode_ext;
  403. Replica *r;
  404. PR_Lock (s_configLock);
  405. mtnode_ext = _replica_config_get_mtnode_ext (e);
  406. PR_ASSERT (mtnode_ext);
  407. if (mtnode_ext->replica)
  408. {
  409. char ebuf[BUFSIZ];
  410. /* remove object from the hash */
  411. r = (Replica*)object_get_data (mtnode_ext->replica);
  412. PR_ASSERT (r);
  413. /* The changelog for this replica is no longer valid, so we should remove it. */
  414. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_delete: "
  415. "Warning: The changelog for replica %s is no longer valid since "
  416. "the replica config is being deleted. Removing the changelog.\n",
  417. escape_string(slapi_sdn_get_dn(replica_get_root(r)),ebuf));
  418. cl5DeleteDBSync(mtnode_ext->replica);
  419. replica_delete_by_name (replica_get_name (r));
  420. object_release (mtnode_ext->replica);
  421. mtnode_ext->replica = NULL;
  422. }
  423. PR_Unlock (s_configLock);
  424. *returncode = LDAP_SUCCESS;
  425. return SLAPI_DSE_CALLBACK_OK;
  426. }
  427. static int
  428. replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode,
  429. char *returntext, void *arg)
  430. {
  431. multimaster_mtnode_extension *mtnode_ext;
  432. int changeCount = 0;
  433. PRBool reapActive = PR_FALSE;
  434. char val [64];
  435. /* add attribute that contains number of entries in the changelog for this replica */
  436. PR_Lock (s_configLock);
  437. mtnode_ext = _replica_config_get_mtnode_ext (e);
  438. PR_ASSERT (mtnode_ext);
  439. if (mtnode_ext->replica) {
  440. Replica *replica;
  441. object_acquire (mtnode_ext->replica);
  442. if (cl5GetState () == CL5_STATE_OPEN) {
  443. changeCount = cl5GetOperationCount (mtnode_ext->replica);
  444. }
  445. replica = (Replica*)object_get_data (mtnode_ext->replica);
  446. if (replica) {
  447. reapActive = replica_get_tombstone_reap_active(replica);
  448. }
  449. object_release (mtnode_ext->replica);
  450. }
  451. sprintf (val, "%d", changeCount);
  452. slapi_entry_add_string (e, type_replicaChangeCount, val);
  453. slapi_entry_attr_set_int(e, "nsds5replicaReapActive", (int)reapActive);
  454. PR_Unlock (s_configLock);
  455. return SLAPI_DSE_CALLBACK_OK;
  456. }
  457. static int
  458. replica_config_change_type_and_id (Replica *r, const char *new_type,
  459. const char *new_id, char *returntext,
  460. int apply_mods)
  461. {
  462. int type;
  463. ReplicaType oldtype;
  464. ReplicaId rid;
  465. ReplicaId oldrid;
  466. PR_ASSERT (r);
  467. oldtype = replica_get_type(r);
  468. oldrid = replica_get_rid(r);
  469. if (new_type == NULL) /* by default - replica is read-only */
  470. {
  471. type = REPLICA_TYPE_READONLY;
  472. }
  473. else
  474. {
  475. type = atoi (new_type);
  476. if (type <= REPLICA_TYPE_UNKNOWN || type >= REPLICA_TYPE_END)
  477. {
  478. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "invalid replica type %d", type);
  479. return LDAP_OPERATIONS_ERROR;
  480. }
  481. }
  482. /* disallow changing type to itself just to permit a replica ID change */
  483. if (oldtype == type)
  484. {
  485. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica type is already %d - not changing", type);
  486. return LDAP_OPERATIONS_ERROR;
  487. }
  488. if (type == REPLICA_TYPE_READONLY)
  489. {
  490. rid = READ_ONLY_REPLICA_ID; /* default rid for read only */
  491. }
  492. else if (!new_id)
  493. {
  494. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "a replica ID is required when changing replica type to read-write");
  495. return LDAP_UNWILLING_TO_PERFORM;
  496. }
  497. else
  498. {
  499. int temprid = atoi (new_id);
  500. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID)
  501. {
  502. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  503. "attribute %s must have a value greater than 0 "
  504. "and less than %d",
  505. attr_replicaId, READ_ONLY_REPLICA_ID);
  506. return LDAP_UNWILLING_TO_PERFORM;
  507. }
  508. else
  509. {
  510. rid = (ReplicaId)temprid;
  511. }
  512. }
  513. /* error if old rid == new rid */
  514. if (oldrid == rid)
  515. {
  516. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica ID is already %d - not changing", rid);
  517. return LDAP_OPERATIONS_ERROR;
  518. }
  519. if (apply_mods)
  520. {
  521. replica_set_type (r, type);
  522. replica_set_rid(r, rid);
  523. /* Set the mapping tree node, and the list of referrals */
  524. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  525. if (!replica_is_legacy_consumer(r))
  526. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  527. }
  528. return LDAP_SUCCESS;
  529. }
  530. static int
  531. replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext,
  532. int apply_mods)
  533. {
  534. PR_ASSERT (r);
  535. if (apply_mods)
  536. {
  537. Slapi_Mod smod;
  538. Slapi_ValueSet *vs= slapi_valueset_new();
  539. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  540. slapi_valueset_set_from_smod(vs, &smod);
  541. replica_set_updatedn(r, vs, mod->mod_op);
  542. slapi_mod_done(&smod);
  543. slapi_valueset_free(vs);
  544. }
  545. return LDAP_SUCCESS;
  546. }
  547. static int replica_config_change_flags (Replica *r, const char *new_flags,
  548. char *returntext, int apply_mods)
  549. {
  550. PR_ASSERT (r);
  551. if (apply_mods)
  552. {
  553. PRUint32 flags;
  554. flags = atol (new_flags);
  555. replica_replace_flags (r, flags);
  556. }
  557. return LDAP_SUCCESS;
  558. }
  559. static int replica_execute_task (Object *r, const char *task_name, char *returntext,
  560. int apply_mods)
  561. {
  562. if (strcasecmp (task_name, CL2LDIF_TASK) == 0)
  563. {
  564. if (apply_mods)
  565. {
  566. return replica_execute_cl2ldif_task (r, returntext);
  567. }
  568. else
  569. return LDAP_SUCCESS;
  570. }
  571. else if (strncasecmp (task_name, CLEANRUV, CLEANRUVLEN) == 0)
  572. {
  573. int temprid = atoi(&(task_name[CLEANRUVLEN]));
  574. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  575. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  576. "Invalid replica id for task - %s", task_name);
  577. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  578. "replica_execute_task: %s\n", returntext);
  579. return LDAP_OPERATIONS_ERROR;
  580. }
  581. if (apply_mods)
  582. {
  583. return replica_execute_cleanruv_task (r, (ReplicaId)temprid, returntext);
  584. }
  585. else
  586. return LDAP_SUCCESS;
  587. }
  588. else
  589. {
  590. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "unsupported replica task - %s", task_name);
  591. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  592. "replica_execute_task: %s\n", returntext);
  593. return LDAP_OPERATIONS_ERROR;
  594. }
  595. }
  596. static int replica_execute_cl2ldif_task (Object *r, char *returntext)
  597. {
  598. int rc;
  599. Object *rlist [2];
  600. Replica *replica;
  601. char fName [MAXPATHLEN];
  602. char *clDir;
  603. if (cl5GetState () != CL5_STATE_OPEN)
  604. {
  605. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  606. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  607. "replica_execute_cl2ldif_task: %s\n", returntext);
  608. return LDAP_OPERATIONS_ERROR;
  609. }
  610. rlist[0] = r;
  611. rlist[1] = NULL;
  612. /* file is stored in the changelog directory and is named
  613. <replica name>.ldif */
  614. clDir = cl5GetDir ();
  615. PR_ASSERT (clDir);
  616. replica = (Replica*)object_get_data (r);
  617. PR_ASSERT (replica);
  618. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  619. slapi_ch_free ((void**)&clDir);
  620. rc = cl5ExportLDIF (fName, rlist);
  621. if (rc != CL5_SUCCESS)
  622. {
  623. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to export changelog data to file %s; "
  624. "changelog error - %d", fName, rc);
  625. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  626. "replica_execute_cl2ldif_task: %s\n", returntext);
  627. return LDAP_OPERATIONS_ERROR;
  628. }
  629. return LDAP_SUCCESS;
  630. }
  631. static multimaster_mtnode_extension *
  632. _replica_config_get_mtnode_ext (const Slapi_Entry *e)
  633. {
  634. const char *replica_root;
  635. Slapi_DN *sdn = NULL;
  636. mapping_tree_node *mtnode;
  637. multimaster_mtnode_extension *ext = NULL;
  638. char ebuf[BUFSIZ];
  639. /* retirve root of the tree for which replica is configured */
  640. replica_root = slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  641. if (replica_root == NULL)
  642. {
  643. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  644. "configuration entry %s missing %s attribute\n",
  645. escape_string(slapi_entry_get_dn((Slapi_Entry *)e), ebuf),
  646. attr_replicaRoot);
  647. return NULL;
  648. }
  649. sdn = slapi_sdn_new_dn_passin (replica_root);
  650. /* locate mapping tree node for the specified subtree */
  651. mtnode = slapi_get_mapping_tree_node_by_dn (sdn);
  652. if (mtnode == NULL)
  653. {
  654. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  655. "failed to locate mapping tree node for dn %s\n",
  656. escape_string(slapi_sdn_get_dn(sdn), ebuf));
  657. }
  658. else
  659. {
  660. /* check if replica object already exists for the specified subtree */
  661. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  662. }
  663. slapi_sdn_free (&sdn);
  664. return ext;
  665. }
  666. static int
  667. replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext)
  668. {
  669. int rc = 0;
  670. Object *RUVObj;
  671. RUV *local_ruv = NULL;
  672. Replica *replica = (Replica*)object_get_data (r);
  673. PR_ASSERT (replica);
  674. RUVObj = replica_get_ruv(replica);
  675. PR_ASSERT(RUVObj);
  676. local_ruv = (RUV*)object_get_data (RUVObj);
  677. /* Need to check that :
  678. * - rid is not the local one
  679. * - rid is not the last one
  680. */
  681. if ((replica_get_rid(replica) == rid) ||
  682. (ruv_replica_count(local_ruv) <= 1)) {
  683. return LDAP_UNWILLING_TO_PERFORM;
  684. }
  685. rc = ruv_delete_replica(local_ruv, rid);
  686. replica_set_ruv_dirty(replica);
  687. replica_write_ruv(replica);
  688. object_release(RUVObj);
  689. /* Update Mapping Tree to reflect RUV changes */
  690. consumer5_set_mapping_tree_state_for_replica(replica, NULL);
  691. if (rc != RUV_SUCCESS){
  692. return LDAP_OPERATIONS_ERROR;
  693. }
  694. return LDAP_SUCCESS;
  695. }