repl5_replica_config.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  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 [SLAPI_DSE_RETURNTEXT_SIZE];
  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. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "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 [SLAPI_DSE_RETURNTEXT_SIZE];
  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. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "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. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "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. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "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. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  332. "modification of attribute %s is not allowed in replica entry", config_attr);
  333. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  334. errortext);
  335. }
  336. }
  337. }
  338. if (new_repl_id || new_repl_type)
  339. {
  340. *returncode = replica_config_change_type_and_id(r, new_repl_type,
  341. new_repl_id, errortext,
  342. apply_mods);
  343. slapi_ch_free_string(&new_repl_id);
  344. slapi_ch_free_string(&new_repl_type);
  345. }
  346. }
  347. done:
  348. if (mtnode_ext->replica)
  349. object_release (mtnode_ext->replica);
  350. /* slapi_ch_free accepts NULL pointer */
  351. slapi_ch_free ((void**)&replica_root);
  352. PR_Unlock (s_configLock);
  353. if (*returncode != LDAP_SUCCESS)
  354. {
  355. return SLAPI_DSE_CALLBACK_ERROR;
  356. }
  357. else
  358. {
  359. return SLAPI_DSE_CALLBACK_OK;
  360. }
  361. }
  362. static int
  363. replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  364. int *returncode, char *returntext, void *arg)
  365. {
  366. multimaster_mtnode_extension *mtnode_ext;
  367. Replica *r;
  368. PR_Lock (s_configLock);
  369. mtnode_ext = _replica_config_get_mtnode_ext (e);
  370. PR_ASSERT (mtnode_ext);
  371. if (mtnode_ext->replica)
  372. {
  373. /* remove object from the hash */
  374. r = (Replica*)object_get_data (mtnode_ext->replica);
  375. PR_ASSERT (r);
  376. replica_delete_by_name (replica_get_name (r));
  377. object_release (mtnode_ext->replica);
  378. mtnode_ext->replica = NULL;
  379. }
  380. PR_Unlock (s_configLock);
  381. *returncode = LDAP_SUCCESS;
  382. return SLAPI_DSE_CALLBACK_OK;
  383. }
  384. static int
  385. replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode,
  386. char *returntext, void *arg)
  387. {
  388. multimaster_mtnode_extension *mtnode_ext;
  389. int changeCount = 0;
  390. PRBool reapActive = PR_FALSE;
  391. char val [64];
  392. /* add attribute that contains number of entries in the changelog for this replica */
  393. PR_Lock (s_configLock);
  394. mtnode_ext = _replica_config_get_mtnode_ext (e);
  395. PR_ASSERT (mtnode_ext);
  396. if (mtnode_ext->replica) {
  397. Replica *replica;
  398. object_acquire (mtnode_ext->replica);
  399. if (cl5GetState () == CL5_STATE_OPEN) {
  400. changeCount = cl5GetOperationCount (mtnode_ext->replica);
  401. }
  402. replica = (Replica*)object_get_data (mtnode_ext->replica);
  403. if (replica) {
  404. reapActive = replica_get_tombstone_reap_active(replica);
  405. }
  406. object_release (mtnode_ext->replica);
  407. }
  408. sprintf (val, "%d", changeCount);
  409. slapi_entry_add_string (e, type_replicaChangeCount, val);
  410. slapi_entry_attr_set_int(e, "nsds5replicaReapActive", (int)reapActive);
  411. PR_Unlock (s_configLock);
  412. return SLAPI_DSE_CALLBACK_OK;
  413. }
  414. static int
  415. replica_config_change_type_and_id (Replica *r, const char *new_type,
  416. const char *new_id, char *returntext,
  417. int apply_mods)
  418. {
  419. int type;
  420. ReplicaType oldtype;
  421. ReplicaId rid;
  422. ReplicaId oldrid;
  423. PR_ASSERT (r);
  424. oldtype = replica_get_type(r);
  425. oldrid = replica_get_rid(r);
  426. if (new_type == NULL) /* by default - replica is read-only */
  427. {
  428. type = REPLICA_TYPE_READONLY;
  429. }
  430. else
  431. {
  432. type = atoi (new_type);
  433. if (type <= REPLICA_TYPE_UNKNOWN || type >= REPLICA_TYPE_END)
  434. {
  435. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "invalid replica type %d", type);
  436. return LDAP_OPERATIONS_ERROR;
  437. }
  438. }
  439. /* disallow changing type to itself just to permit a replica ID change */
  440. if (oldtype == type)
  441. {
  442. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica type is already %d - not changing", type);
  443. return LDAP_OPERATIONS_ERROR;
  444. }
  445. if (type == REPLICA_TYPE_READONLY)
  446. {
  447. rid = READ_ONLY_REPLICA_ID; /* default rid for read only */
  448. }
  449. else if (!new_id)
  450. {
  451. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "a replica ID is required when changing replica type to read-write");
  452. return LDAP_UNWILLING_TO_PERFORM;
  453. }
  454. else
  455. {
  456. int temprid = atoi (new_id);
  457. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID)
  458. {
  459. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  460. "attribute %s must have a value greater than 0 "
  461. "and less than %d",
  462. attr_replicaId, READ_ONLY_REPLICA_ID);
  463. return LDAP_UNWILLING_TO_PERFORM;
  464. }
  465. else
  466. {
  467. rid = (ReplicaId)temprid;
  468. }
  469. }
  470. /* error if old rid == new rid */
  471. if (oldrid == rid)
  472. {
  473. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica ID is already %d - not changing", rid);
  474. return LDAP_OPERATIONS_ERROR;
  475. }
  476. if (apply_mods)
  477. {
  478. replica_set_type (r, type);
  479. replica_set_rid(r, rid);
  480. /* Set the mapping tree node, and the list of referrals */
  481. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  482. if (!replica_is_legacy_consumer(r))
  483. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  484. }
  485. return LDAP_SUCCESS;
  486. }
  487. static int
  488. replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext,
  489. int apply_mods)
  490. {
  491. PR_ASSERT (r);
  492. if (apply_mods)
  493. {
  494. Slapi_Mod smod;
  495. Slapi_ValueSet *vs= slapi_valueset_new();
  496. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  497. slapi_valueset_set_from_smod(vs, &smod);
  498. replica_set_updatedn(r, vs, mod->mod_op);
  499. slapi_mod_done(&smod);
  500. slapi_valueset_free(vs);
  501. }
  502. return LDAP_SUCCESS;
  503. }
  504. static int replica_config_change_flags (Replica *r, const char *new_flags,
  505. char *returntext, int apply_mods)
  506. {
  507. PR_ASSERT (r);
  508. if (apply_mods)
  509. {
  510. PRUint32 flags;
  511. flags = atol (new_flags);
  512. replica_replace_flags (r, flags);
  513. }
  514. return LDAP_SUCCESS;
  515. }
  516. static int replica_execute_task (Object *r, const char *task_name, char *returntext,
  517. int apply_mods)
  518. {
  519. if (strcasecmp (task_name, CL2LDIF_TASK) == 0)
  520. {
  521. if (apply_mods)
  522. {
  523. return replica_execute_cl2ldif_task (r, returntext);
  524. }
  525. else
  526. return LDAP_SUCCESS;
  527. }
  528. else if (strncasecmp (task_name, CLEANRUV, CLEANRUVLEN) == 0)
  529. {
  530. int temprid = atoi(&(task_name[CLEANRUVLEN]));
  531. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  532. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  533. "Invalid replica id for task - %s", task_name);
  534. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  535. "replica_execute_task: %s\n", returntext);
  536. return LDAP_OPERATIONS_ERROR;
  537. }
  538. if (apply_mods)
  539. {
  540. return replica_execute_cleanruv_task (r, (ReplicaId)temprid, returntext);
  541. }
  542. else
  543. return LDAP_SUCCESS;
  544. }
  545. else
  546. {
  547. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "unsupported replica task - %s", task_name);
  548. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  549. "replica_execute_task: %s\n", returntext);
  550. return LDAP_OPERATIONS_ERROR;
  551. }
  552. }
  553. static int replica_execute_cl2ldif_task (Object *r, char *returntext)
  554. {
  555. int rc;
  556. Object *rlist [2];
  557. Replica *replica;
  558. char fName [MAXPATHLEN];
  559. char *clDir;
  560. if (cl5GetState () != CL5_STATE_OPEN)
  561. {
  562. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  563. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  564. "replica_execute_cl2ldif_task: %s\n", returntext);
  565. return LDAP_OPERATIONS_ERROR;
  566. }
  567. rlist[0] = r;
  568. rlist[1] = NULL;
  569. /* file is stored in the changelog directory and is named
  570. <replica name>.ldif */
  571. clDir = cl5GetDir ();
  572. PR_ASSERT (clDir);
  573. replica = (Replica*)object_get_data (r);
  574. PR_ASSERT (replica);
  575. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  576. slapi_ch_free ((void**)&clDir);
  577. rc = cl5ExportLDIF (fName, rlist);
  578. if (rc != CL5_SUCCESS)
  579. {
  580. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to export changelog data to file %s; "
  581. "changelog error - %d", fName, rc);
  582. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  583. "replica_execute_cl2ldif_task: %s\n", returntext);
  584. return LDAP_OPERATIONS_ERROR;
  585. }
  586. return LDAP_SUCCESS;
  587. }
  588. static multimaster_mtnode_extension *
  589. _replica_config_get_mtnode_ext (const Slapi_Entry *e)
  590. {
  591. const char *replica_root;
  592. Slapi_DN *sdn = NULL;
  593. mapping_tree_node *mtnode;
  594. multimaster_mtnode_extension *ext = NULL;
  595. char ebuf[BUFSIZ];
  596. /* retirve root of the tree for which replica is configured */
  597. replica_root = slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  598. if (replica_root == NULL)
  599. {
  600. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  601. "configuration entry %s missing %s attribute\n",
  602. escape_string(slapi_entry_get_dn((Slapi_Entry *)e), ebuf),
  603. attr_replicaRoot);
  604. return NULL;
  605. }
  606. sdn = slapi_sdn_new_dn_passin (replica_root);
  607. /* locate mapping tree node for the specified subtree */
  608. mtnode = slapi_get_mapping_tree_node_by_dn (sdn);
  609. if (mtnode == NULL)
  610. {
  611. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  612. "failed to locate mapping tree node for dn %s\n",
  613. escape_string(slapi_sdn_get_dn(sdn), ebuf));
  614. }
  615. else
  616. {
  617. /* check if replica object already exists for the specified subtree */
  618. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  619. }
  620. slapi_sdn_free (&sdn);
  621. return ext;
  622. }
  623. static int
  624. replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext)
  625. {
  626. int rc = 0;
  627. Object *RUVObj;
  628. RUV *local_ruv = NULL;
  629. Replica *replica = (Replica*)object_get_data (r);
  630. PR_ASSERT (replica);
  631. RUVObj = replica_get_ruv(replica);
  632. PR_ASSERT(RUVObj);
  633. local_ruv = (RUV*)object_get_data (RUVObj);
  634. /* Need to check that :
  635. * - rid is not the local one
  636. * - rid is not the last one
  637. */
  638. if ((replica_get_rid(replica) == rid) ||
  639. (ruv_replica_count(local_ruv) <= 1)) {
  640. return LDAP_UNWILLING_TO_PERFORM;
  641. }
  642. rc = ruv_delete_replica(local_ruv, rid);
  643. replica_set_ruv_dirty(replica);
  644. replica_write_ruv(replica);
  645. object_release(RUVObj);
  646. /* Update Mapping Tree to reflect RUV changes */
  647. consumer5_set_mapping_tree_state_for_replica(replica, NULL);
  648. if (rc != RUV_SUCCESS){
  649. return LDAP_OPERATIONS_ERROR;
  650. }
  651. return LDAP_SUCCESS;
  652. }