repl5_replica_config.c 24 KB

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