1
0

repl5_replica_config.c 27 KB

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