repl5_replica_config.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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. #include "cl5.h"
  47. /* CONFIG_BASE: no need to optimize */
  48. #define CONFIG_BASE "cn=mapping tree,cn=config"
  49. #define CONFIG_FILTER "(objectclass=nsDS5Replica)"
  50. #define TASK_ATTR "nsds5Task"
  51. #define CL2LDIF_TASK "CL2LDIF"
  52. #define LDIF2CL_TASK "LDIF2CL"
  53. #define CLEANRUV "CLEANRUV"
  54. #define CLEANRUVLEN 8
  55. #define CLEANALLRUV "CLEANALLRUV"
  56. #define CLEANALLRUVLEN 11
  57. #define RELEASERUV "RELEASERUV"
  58. #define RELEASERUVLEN 10
  59. #define REPLICA_RDN "cn=replica"
  60. int slapi_log_urp = SLAPI_LOG_REPL;
  61. static ReplicaId cleaned_rid = 0;
  62. static int released_rid = 0;
  63. /* Forward Declartions */
  64. static int replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  65. static int replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  66. static int replica_config_post_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  67. static int replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  68. static int replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  69. static int replica_config_change_type_and_id (Replica *r, const char *new_type, const char *new_id, char *returntext, int apply_mods);
  70. static int replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext, int apply_mods);
  71. static int replica_config_change_flags (Replica *r, const char *new_flags, char *returntext, int apply_mods);
  72. static int replica_execute_task (Object *r, const char *task_name, char *returntext, int apply_mods);
  73. static int replica_execute_cl2ldif_task (Object *r, char *returntext);
  74. static int replica_execute_ldif2cl_task (Object *r, char *returntext);
  75. static int replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext);
  76. static int replica_execute_cleanall_ruv_task (Object *r, ReplicaId rid, char *returntext);
  77. static int replica_execute_release_ruv_task(Object *r, ReplicaId rid, char *returntext);
  78. static struct berval *create_ruv_payload(char *value);
  79. static int replica_cleanup_task (Object *r, const char *task_name, char *returntext, int apply_mods);
  80. static int replica_task_done(Replica *replica);
  81. static multimaster_mtnode_extension * _replica_config_get_mtnode_ext (const Slapi_Entry *e);
  82. /*
  83. * Note: internal add/modify/delete operations should not be run while
  84. * s_configLock is held. E.g., slapi_modify_internal_pb via replica_task_done
  85. * in replica_config_post_modify.
  86. */
  87. static PRLock *s_configLock;
  88. static int
  89. dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  90. {
  91. *returncode = LDAP_UNWILLING_TO_PERFORM;
  92. return SLAPI_DSE_CALLBACK_ERROR;
  93. }
  94. int
  95. replica_config_init()
  96. {
  97. s_configLock = PR_NewLock ();
  98. if (s_configLock == NULL)
  99. {
  100. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  101. "failed to cretate configuration lock; NSPR error - %d\n",
  102. PR_GetError ());
  103. return -1;
  104. }
  105. /* config DSE must be initialized before we get here */
  106. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  107. CONFIG_FILTER, replica_config_add, NULL);
  108. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  109. CONFIG_FILTER, replica_config_modify,NULL);
  110. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  111. CONFIG_FILTER, dont_allow_that, NULL);
  112. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  113. CONFIG_FILTER, replica_config_delete,NULL);
  114. slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  115. CONFIG_FILTER, replica_config_search,NULL);
  116. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_POSTOP,
  117. CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  118. CONFIG_FILTER, replica_config_post_modify,
  119. NULL);
  120. return 0;
  121. }
  122. void
  123. replica_config_destroy ()
  124. {
  125. if (s_configLock)
  126. {
  127. PR_DestroyLock (s_configLock);
  128. s_configLock = NULL;
  129. }
  130. /* config DSE must be initialized before we get here */
  131. slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  132. CONFIG_FILTER, replica_config_add);
  133. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  134. CONFIG_FILTER, replica_config_modify);
  135. slapi_config_remove_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  136. CONFIG_FILTER, dont_allow_that);
  137. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  138. CONFIG_FILTER, replica_config_delete);
  139. slapi_config_remove_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  140. CONFIG_FILTER, replica_config_search);
  141. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  142. CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  143. CONFIG_FILTER, replica_config_post_modify);
  144. }
  145. static int
  146. replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  147. int *returncode, char *errorbuf, void *arg)
  148. {
  149. Replica *r = NULL;
  150. multimaster_mtnode_extension *mtnode_ext;
  151. char *replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  152. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  153. char *errortext = errorbuf ? errorbuf : buf;
  154. if (errorbuf)
  155. {
  156. errorbuf[0] = '\0';
  157. }
  158. *returncode = LDAP_SUCCESS;
  159. PR_Lock (s_configLock);
  160. /* add the dn to the dn hash so we can tell this replica is being configured */
  161. replica_add_by_dn(replica_root);
  162. mtnode_ext = _replica_config_get_mtnode_ext (e);
  163. PR_ASSERT (mtnode_ext);
  164. if (mtnode_ext->replica)
  165. {
  166. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica already configured for %s", replica_root);
  167. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: %s\n", errortext);
  168. *returncode = LDAP_UNWILLING_TO_PERFORM;
  169. goto done;
  170. }
  171. /* create replica object */
  172. r = replica_new_from_entry (e, errortext, PR_TRUE /* is a newly added entry */);
  173. if (r == NULL)
  174. {
  175. *returncode = LDAP_OPERATIONS_ERROR;
  176. goto done;
  177. }
  178. /* Set the mapping tree node state, and the referrals from the RUV */
  179. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  180. if (!replica_is_legacy_consumer (r))
  181. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  182. /* ONREPL if replica is added as writable we need to execute protocol that
  183. introduces new writable replica to the topology */
  184. mtnode_ext->replica = object_new (r, replica_destroy); /* Refcnt is 1 */
  185. /* add replica object to the hash */
  186. *returncode = replica_add_by_name (replica_get_name (r), mtnode_ext->replica); /* Increments object refcnt */
  187. /* delete the dn from the dn hash - done with configuration */
  188. replica_delete_by_dn(replica_root);
  189. done:
  190. PR_Unlock (s_configLock);
  191. /* slapi_ch_free accepts NULL pointer */
  192. slapi_ch_free ((void**)&replica_root);
  193. if (*returncode != LDAP_SUCCESS)
  194. {
  195. if (mtnode_ext->replica)
  196. object_release (mtnode_ext->replica);
  197. return SLAPI_DSE_CALLBACK_ERROR;
  198. }
  199. else
  200. return SLAPI_DSE_CALLBACK_OK;
  201. }
  202. static int
  203. replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  204. int *returncode, char *returntext, void *arg)
  205. {
  206. int rc= 0;
  207. LDAPMod **mods;
  208. int i, apply_mods;
  209. multimaster_mtnode_extension *mtnode_ext;
  210. Replica *r = NULL;
  211. char *replica_root = NULL;
  212. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  213. char *errortext = returntext ? returntext : buf;
  214. char *config_attr, *config_attr_value;
  215. Slapi_Operation *op;
  216. void *identity;
  217. if (returntext)
  218. {
  219. returntext[0] = '\0';
  220. }
  221. *returncode = LDAP_SUCCESS;
  222. /* just let internal operations originated from replication plugin to go through */
  223. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  224. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  225. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  226. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  227. {
  228. *returncode = LDAP_SUCCESS;
  229. return SLAPI_DSE_CALLBACK_OK;
  230. }
  231. replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  232. PR_Lock (s_configLock);
  233. mtnode_ext = _replica_config_get_mtnode_ext (e);
  234. PR_ASSERT (mtnode_ext);
  235. if (mtnode_ext->replica)
  236. object_acquire (mtnode_ext->replica);
  237. if (mtnode_ext->replica == NULL)
  238. {
  239. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica does not exist for %s", replica_root);
  240. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  241. errortext);
  242. *returncode = LDAP_OPERATIONS_ERROR;
  243. goto done;
  244. }
  245. r = object_get_data (mtnode_ext->replica);
  246. PR_ASSERT (r);
  247. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  248. for (apply_mods = 0; apply_mods <= 1; apply_mods++)
  249. {
  250. /* we only allow the replica ID and type to be modified together e.g.
  251. if converting a read only replica to a master or vice versa -
  252. we will need to change both the replica ID and the type at the same
  253. time - we must disallow changing the replica ID if the type is not
  254. being changed and vice versa
  255. */
  256. char *new_repl_id = NULL;
  257. char *new_repl_type = NULL;
  258. if (*returncode != LDAP_SUCCESS)
  259. break;
  260. for (i = 0; (mods[i] && (LDAP_SUCCESS == rc)); i++)
  261. {
  262. if (*returncode != LDAP_SUCCESS)
  263. break;
  264. config_attr = (char *) mods[i]->mod_type;
  265. PR_ASSERT (config_attr);
  266. /* disallow modifications or removal of replica root,
  267. replica name and replica state attributes */
  268. if (strcasecmp (config_attr, attr_replicaRoot) == 0 ||
  269. strcasecmp (config_attr, attr_replicaName) == 0 ||
  270. strcasecmp (config_attr, attr_state) == 0)
  271. {
  272. *returncode = LDAP_UNWILLING_TO_PERFORM;
  273. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "modification of %s attribute is not allowed",
  274. config_attr);
  275. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  276. errortext);
  277. }
  278. /* this is a request to delete an attribute */
  279. else if (mods[i]->mod_op & LDAP_MOD_DELETE || mods[i]->mod_bvalues == NULL
  280. || mods[i]->mod_bvalues[0]->bv_val == NULL)
  281. {
  282. /* currently, you can only remove referral,
  283. legacy consumer or bind dn attribute */
  284. if (strcasecmp (config_attr, attr_replicaBindDn) == 0)
  285. {
  286. *returncode = replica_config_change_updatedn (r, mods[i], errortext, apply_mods);
  287. }
  288. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  289. {
  290. if (apply_mods) {
  291. replica_set_referrals(r, NULL);
  292. if (!replica_is_legacy_consumer (r)) {
  293. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  294. }
  295. }
  296. }
  297. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  298. {
  299. if (apply_mods)
  300. replica_set_legacy_consumer (r, PR_FALSE);
  301. }
  302. else
  303. {
  304. *returncode = LDAP_UNWILLING_TO_PERFORM;
  305. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "deletion of %s attribute is not allowed", config_attr);
  306. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  307. errortext);
  308. }
  309. }
  310. else /* modify an attribute */
  311. {
  312. config_attr_value = (char *) mods[i]->mod_bvalues[0]->bv_val;
  313. if (strcasecmp (config_attr, attr_replicaBindDn) == 0)
  314. {
  315. *returncode = replica_config_change_updatedn (r, mods[i],
  316. errortext, apply_mods);
  317. }
  318. else if (strcasecmp (config_attr, attr_replicaType) == 0)
  319. {
  320. new_repl_type = slapi_ch_strdup(config_attr_value);
  321. }
  322. else if (strcasecmp (config_attr, attr_replicaId) == 0)
  323. {
  324. new_repl_id = slapi_ch_strdup(config_attr_value);
  325. }
  326. else if (strcasecmp (config_attr, attr_flags) == 0)
  327. {
  328. *returncode = replica_config_change_flags (r, config_attr_value,
  329. errortext, apply_mods);
  330. }
  331. else if (strcasecmp (config_attr, TASK_ATTR) == 0)
  332. {
  333. *returncode = replica_execute_task (mtnode_ext->replica, config_attr_value,
  334. errortext, apply_mods);
  335. }
  336. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  337. {
  338. if (apply_mods)
  339. {
  340. Slapi_Mod smod;
  341. Slapi_ValueSet *vs= slapi_valueset_new();
  342. slapi_mod_init_byref(&smod,mods[i]);
  343. slapi_valueset_set_from_smod(vs, &smod);
  344. replica_set_referrals (r, vs);
  345. slapi_mod_done(&smod);
  346. slapi_valueset_free(vs);
  347. if (!replica_is_legacy_consumer (r)) {
  348. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  349. }
  350. }
  351. }
  352. else if (strcasecmp (config_attr, type_replicaPurgeDelay) == 0)
  353. {
  354. if (apply_mods && config_attr_value && config_attr_value[0])
  355. {
  356. PRUint32 delay;
  357. if (isdigit (config_attr_value[0]))
  358. {
  359. delay = (unsigned int)atoi(config_attr_value);
  360. replica_set_purge_delay(r, delay);
  361. }
  362. else
  363. *returncode = LDAP_OPERATIONS_ERROR;
  364. }
  365. }
  366. else if (strcasecmp (config_attr, type_replicaTombstonePurgeInterval) == 0)
  367. {
  368. if (apply_mods && config_attr_value && config_attr_value[0])
  369. {
  370. long interval;
  371. interval = atol (config_attr_value);
  372. replica_set_tombstone_reap_interval (r, interval);
  373. }
  374. }
  375. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  376. {
  377. if (apply_mods)
  378. {
  379. PRBool legacy = (strcasecmp (config_attr_value, "on") == 0) ||
  380. (strcasecmp (config_attr_value, "true") == 0) ||
  381. (strcasecmp (config_attr_value, "yes") == 0) ||
  382. (strcasecmp (config_attr_value, "1") == 0);
  383. replica_set_legacy_consumer (r, legacy);
  384. }
  385. }
  386. /* ignore modifiers attributes added by the server */
  387. else if (strcasecmp (config_attr, "modifytimestamp") == 0 ||
  388. strcasecmp (config_attr, "modifiersname") == 0)
  389. {
  390. *returncode = LDAP_SUCCESS;
  391. }
  392. else
  393. {
  394. *returncode = LDAP_UNWILLING_TO_PERFORM;
  395. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  396. "modification of attribute %s is not allowed in replica entry", config_attr);
  397. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  398. errortext);
  399. }
  400. }
  401. }
  402. if (new_repl_id || new_repl_type)
  403. {
  404. *returncode = replica_config_change_type_and_id(r, new_repl_type,
  405. new_repl_id, errortext,
  406. apply_mods);
  407. slapi_ch_free_string(&new_repl_id);
  408. slapi_ch_free_string(&new_repl_type);
  409. }
  410. }
  411. done:
  412. if (mtnode_ext->replica)
  413. object_release (mtnode_ext->replica);
  414. /* slapi_ch_free accepts NULL pointer */
  415. slapi_ch_free ((void**)&replica_root);
  416. PR_Unlock (s_configLock);
  417. if (*returncode != LDAP_SUCCESS)
  418. {
  419. return SLAPI_DSE_CALLBACK_ERROR;
  420. }
  421. else
  422. {
  423. return SLAPI_DSE_CALLBACK_OK;
  424. }
  425. }
  426. static int
  427. replica_config_post_modify(Slapi_PBlock *pb,
  428. Slapi_Entry* entryBefore,
  429. Slapi_Entry* e,
  430. int *returncode,
  431. char *returntext,
  432. void *arg)
  433. {
  434. int rc= 0;
  435. LDAPMod **mods;
  436. int i, apply_mods;
  437. multimaster_mtnode_extension *mtnode_ext;
  438. Replica *r = NULL;
  439. char *replica_root = NULL;
  440. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  441. char *errortext = returntext ? returntext : buf;
  442. char *config_attr, *config_attr_value;
  443. Slapi_Operation *op;
  444. void *identity;
  445. int flag_need_cleanup = 0;
  446. if (returntext)
  447. {
  448. returntext[0] = '\0';
  449. }
  450. *returncode = LDAP_SUCCESS;
  451. /* just let internal operations originated from replication plugin to go through */
  452. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  453. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  454. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  455. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  456. {
  457. *returncode = LDAP_SUCCESS;
  458. return SLAPI_DSE_CALLBACK_OK;
  459. }
  460. replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  461. PR_Lock (s_configLock);
  462. mtnode_ext = _replica_config_get_mtnode_ext (e);
  463. PR_ASSERT (mtnode_ext);
  464. if (mtnode_ext->replica)
  465. object_acquire (mtnode_ext->replica);
  466. if (mtnode_ext->replica == NULL)
  467. {
  468. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  469. "replica does not exist for %s", replica_root);
  470. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  471. "replica_config_post_modify: %s\n",
  472. errortext);
  473. *returncode = LDAP_OPERATIONS_ERROR;
  474. goto done;
  475. }
  476. r = object_get_data (mtnode_ext->replica);
  477. PR_ASSERT (r);
  478. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  479. for (apply_mods = 0; apply_mods <= 1; apply_mods++)
  480. {
  481. /* we only allow the replica ID and type to be modified together e.g.
  482. if converting a read only replica to a master or vice versa -
  483. we will need to change both the replica ID and the type at the same
  484. time - we must disallow changing the replica ID if the type is not
  485. being changed and vice versa
  486. */
  487. if (*returncode != LDAP_SUCCESS)
  488. break;
  489. for (i = 0; (mods[i] && (LDAP_SUCCESS == rc)); i++)
  490. {
  491. if (*returncode != LDAP_SUCCESS)
  492. break;
  493. config_attr = (char *) mods[i]->mod_type;
  494. PR_ASSERT (config_attr);
  495. /* disallow modifications or removal of replica root,
  496. replica name and replica state attributes */
  497. if (strcasecmp (config_attr, attr_replicaRoot) == 0 ||
  498. strcasecmp (config_attr, attr_replicaName) == 0 ||
  499. strcasecmp (config_attr, attr_state) == 0)
  500. {
  501. *returncode = LDAP_UNWILLING_TO_PERFORM;
  502. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  503. "modification of %s attribute is not allowed",
  504. config_attr);
  505. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  506. "replica_config_post_modify: %s\n",
  507. errortext);
  508. }
  509. /* this is a request to delete an attribute */
  510. else if (mods[i]->mod_op & LDAP_MOD_DELETE ||
  511. mods[i]->mod_bvalues == NULL ||
  512. mods[i]->mod_bvalues[0]->bv_val == NULL)
  513. {
  514. ;
  515. }
  516. else /* modify an attribute */
  517. {
  518. config_attr_value = (char *) mods[i]->mod_bvalues[0]->bv_val;
  519. if (strcasecmp (config_attr, TASK_ATTR) == 0)
  520. {
  521. flag_need_cleanup = 1;
  522. }
  523. }
  524. }
  525. }
  526. done:
  527. PR_Unlock (s_configLock);
  528. /* slapi_ch_free accepts NULL pointer */
  529. slapi_ch_free_string (&replica_root);
  530. /* Call replica_cleanup_task after s_configLock is reliesed */
  531. if (flag_need_cleanup)
  532. {
  533. *returncode = replica_cleanup_task(mtnode_ext->replica,
  534. config_attr_value,
  535. errortext, apply_mods);
  536. }
  537. if (mtnode_ext->replica)
  538. object_release (mtnode_ext->replica);
  539. if (*returncode != LDAP_SUCCESS)
  540. {
  541. return SLAPI_DSE_CALLBACK_ERROR;
  542. }
  543. else
  544. {
  545. return SLAPI_DSE_CALLBACK_OK;
  546. }
  547. }
  548. static int
  549. replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  550. int *returncode, char *returntext, void *arg)
  551. {
  552. multimaster_mtnode_extension *mtnode_ext;
  553. Replica *r;
  554. PR_Lock (s_configLock);
  555. mtnode_ext = _replica_config_get_mtnode_ext (e);
  556. PR_ASSERT (mtnode_ext);
  557. if (mtnode_ext->replica)
  558. {
  559. /* remove object from the hash */
  560. r = (Replica*)object_get_data (mtnode_ext->replica);
  561. PR_ASSERT (r);
  562. /* The changelog for this replica is no longer valid, so we should remove it. */
  563. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_delete: "
  564. "Warning: The changelog for replica %s is no longer valid since "
  565. "the replica config is being deleted. Removing the changelog.\n",
  566. slapi_sdn_get_dn(replica_get_root(r)));
  567. cl5DeleteDBSync(mtnode_ext->replica);
  568. replica_delete_by_name (replica_get_name (r));
  569. object_release (mtnode_ext->replica);
  570. mtnode_ext->replica = NULL;
  571. }
  572. PR_Unlock (s_configLock);
  573. *returncode = LDAP_SUCCESS;
  574. return SLAPI_DSE_CALLBACK_OK;
  575. }
  576. static int
  577. replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode,
  578. char *returntext, void *arg)
  579. {
  580. multimaster_mtnode_extension *mtnode_ext;
  581. int changeCount = 0;
  582. PRBool reapActive = PR_FALSE;
  583. char val [64];
  584. /* add attribute that contains number of entries in the changelog for this replica */
  585. PR_Lock (s_configLock);
  586. mtnode_ext = _replica_config_get_mtnode_ext (e);
  587. PR_ASSERT (mtnode_ext);
  588. if (mtnode_ext->replica) {
  589. Replica *replica;
  590. object_acquire (mtnode_ext->replica);
  591. if (cl5GetState () == CL5_STATE_OPEN) {
  592. changeCount = cl5GetOperationCount (mtnode_ext->replica);
  593. }
  594. replica = (Replica*)object_get_data (mtnode_ext->replica);
  595. if (replica) {
  596. reapActive = replica_get_tombstone_reap_active(replica);
  597. }
  598. object_release (mtnode_ext->replica);
  599. }
  600. sprintf (val, "%d", changeCount);
  601. slapi_entry_add_string (e, type_replicaChangeCount, val);
  602. slapi_entry_attr_set_int(e, "nsds5replicaReapActive", (int)reapActive);
  603. PR_Unlock (s_configLock);
  604. return SLAPI_DSE_CALLBACK_OK;
  605. }
  606. static int
  607. replica_config_change_type_and_id (Replica *r, const char *new_type,
  608. const char *new_id, char *returntext,
  609. int apply_mods)
  610. {
  611. int type;
  612. ReplicaType oldtype;
  613. ReplicaId rid;
  614. ReplicaId oldrid;
  615. PR_ASSERT (r);
  616. oldtype = replica_get_type(r);
  617. oldrid = replica_get_rid(r);
  618. if (new_type == NULL) /* by default - replica is read-only */
  619. {
  620. type = REPLICA_TYPE_READONLY;
  621. }
  622. else
  623. {
  624. type = atoi (new_type);
  625. if (type <= REPLICA_TYPE_UNKNOWN || type >= REPLICA_TYPE_END)
  626. {
  627. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "invalid replica type %d", type);
  628. return LDAP_OPERATIONS_ERROR;
  629. }
  630. }
  631. /* disallow changing type to itself just to permit a replica ID change */
  632. if (oldtype == type)
  633. {
  634. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica type is already %d - not changing", type);
  635. return LDAP_OPERATIONS_ERROR;
  636. }
  637. if (type == REPLICA_TYPE_READONLY)
  638. {
  639. rid = READ_ONLY_REPLICA_ID; /* default rid for read only */
  640. }
  641. else if (!new_id)
  642. {
  643. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "a replica ID is required when changing replica type to read-write");
  644. return LDAP_UNWILLING_TO_PERFORM;
  645. }
  646. else
  647. {
  648. int temprid = atoi (new_id);
  649. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID)
  650. {
  651. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  652. "attribute %s must have a value greater than 0 "
  653. "and less than %d",
  654. attr_replicaId, READ_ONLY_REPLICA_ID);
  655. return LDAP_UNWILLING_TO_PERFORM;
  656. }
  657. else
  658. {
  659. rid = (ReplicaId)temprid;
  660. }
  661. }
  662. /* error if old rid == new rid */
  663. if (oldrid == rid)
  664. {
  665. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica ID is already %d - not changing", rid);
  666. return LDAP_OPERATIONS_ERROR;
  667. }
  668. if (apply_mods)
  669. {
  670. replica_set_type (r, type);
  671. replica_set_rid(r, rid);
  672. /* Set the mapping tree node, and the list of referrals */
  673. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  674. if (!replica_is_legacy_consumer(r))
  675. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  676. }
  677. return LDAP_SUCCESS;
  678. }
  679. static int
  680. replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext,
  681. int apply_mods)
  682. {
  683. PR_ASSERT (r);
  684. if (apply_mods)
  685. {
  686. Slapi_Mod smod;
  687. Slapi_ValueSet *vs= slapi_valueset_new();
  688. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  689. slapi_valueset_set_from_smod(vs, &smod);
  690. replica_set_updatedn(r, vs, mod->mod_op);
  691. slapi_mod_done(&smod);
  692. slapi_valueset_free(vs);
  693. }
  694. return LDAP_SUCCESS;
  695. }
  696. static int replica_config_change_flags (Replica *r, const char *new_flags,
  697. char *returntext, int apply_mods)
  698. {
  699. PR_ASSERT (r);
  700. if (apply_mods)
  701. {
  702. PRUint32 flags;
  703. flags = atol (new_flags);
  704. replica_replace_flags (r, flags);
  705. }
  706. return LDAP_SUCCESS;
  707. }
  708. static int replica_execute_task (Object *r, const char *task_name, char *returntext,
  709. int apply_mods)
  710. {
  711. if (strcasecmp (task_name, CL2LDIF_TASK) == 0)
  712. {
  713. if (apply_mods)
  714. {
  715. return replica_execute_cl2ldif_task (r, returntext);
  716. }
  717. else
  718. return LDAP_SUCCESS;
  719. }
  720. else if (strcasecmp (task_name, LDIF2CL_TASK) == 0)
  721. {
  722. if (apply_mods)
  723. {
  724. return replica_execute_ldif2cl_task (r, returntext);
  725. }
  726. else
  727. return LDAP_SUCCESS;
  728. }
  729. else if (strncasecmp (task_name, CLEANRUV, CLEANRUVLEN) == 0)
  730. {
  731. int temprid = atoi(&(task_name[CLEANRUVLEN]));
  732. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  733. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - %s", temprid, task_name);
  734. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  735. return LDAP_OPERATIONS_ERROR;
  736. }
  737. if (apply_mods)
  738. {
  739. return replica_execute_cleanruv_task (r, (ReplicaId)temprid, returntext);
  740. }
  741. else
  742. return LDAP_SUCCESS;
  743. }
  744. else if (strncasecmp (task_name, CLEANALLRUV, CLEANALLRUVLEN) == 0)
  745. {
  746. int temprid = atoi(&(task_name[CLEANALLRUVLEN]));
  747. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  748. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)", temprid, task_name);
  749. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  750. return LDAP_OPERATIONS_ERROR;
  751. }
  752. if (apply_mods)
  753. {
  754. return replica_execute_cleanall_ruv_task(r, (ReplicaId)temprid, returntext);
  755. }
  756. else
  757. return LDAP_SUCCESS;
  758. }
  759. else if (strncasecmp (task_name, RELEASERUV, RELEASERUVLEN) == 0)
  760. {
  761. int temprid = atoi(&(task_name[RELEASERUVLEN]));
  762. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  763. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id for task - %s", task_name);
  764. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,"replica_execute_task: %s\n", returntext);
  765. return LDAP_OPERATIONS_ERROR;
  766. }
  767. if (apply_mods)
  768. {
  769. return replica_execute_release_ruv_task(r, (ReplicaId)temprid, returntext);
  770. }
  771. else
  772. return LDAP_SUCCESS;
  773. }
  774. else
  775. {
  776. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "unsupported replica task - %s", task_name);
  777. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  778. "replica_execute_task: %s\n", returntext);
  779. return LDAP_OPERATIONS_ERROR;
  780. }
  781. }
  782. static int
  783. replica_cleanup_task (Object *r, const char *task_name, char *returntext,
  784. int apply_mods)
  785. {
  786. int rc = LDAP_SUCCESS;
  787. if (apply_mods) {
  788. Replica *replica = (Replica*)object_get_data (r);
  789. if (NULL == replica) {
  790. rc = LDAP_OPERATIONS_ERROR;
  791. } else {
  792. rc = replica_task_done(replica);
  793. }
  794. }
  795. return rc;
  796. }
  797. static int
  798. replica_task_done(Replica *replica)
  799. {
  800. int rc = LDAP_OPERATIONS_ERROR;
  801. char *replica_dn = NULL;
  802. Slapi_DN *replica_sdn = NULL;
  803. Slapi_PBlock *pb = NULL;
  804. LDAPMod *mods [2];
  805. LDAPMod mod;
  806. if (NULL == replica) {
  807. return rc;
  808. }
  809. /* dn: cn=replica,cn=dc\3Dexample\2Cdc\3Dcom,cn=mapping tree,cn=config */
  810. replica_dn = slapi_ch_smprintf("%s,cn=\"%s\",%s",
  811. REPLICA_RDN,
  812. slapi_sdn_get_dn(replica_get_root(replica)),
  813. CONFIG_BASE);
  814. if (NULL == replica_dn) {
  815. return rc;
  816. }
  817. replica_sdn = slapi_sdn_new_dn_passin(replica_dn);
  818. pb = slapi_pblock_new();
  819. mods[0] = &mod;
  820. mods[1] = NULL;
  821. mod.mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
  822. mod.mod_type = (char *)TASK_ATTR;
  823. mod.mod_bvalues = NULL;
  824. slapi_modify_internal_set_pb_ext(pb, replica_sdn, mods, NULL/* controls */,
  825. NULL/* uniqueid */,
  826. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  827. 0/* flags */);
  828. slapi_modify_internal_pb (pb);
  829. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  830. if ((rc != LDAP_SUCCESS) && (rc != LDAP_NO_SUCH_ATTRIBUTE)) {
  831. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  832. "replica_task_done: "
  833. "failed to remove (%s) attribute from (%s) entry; "
  834. "LDAP error - %d\n",
  835. TASK_ATTR, replica_dn, rc);
  836. }
  837. slapi_pblock_destroy (pb);
  838. slapi_sdn_free(&replica_sdn);
  839. return rc;
  840. }
  841. static int replica_execute_cl2ldif_task (Object *r, char *returntext)
  842. {
  843. int rc;
  844. Object *rlist [2];
  845. Replica *replica;
  846. char fName [MAXPATHLEN];
  847. char *clDir = NULL;
  848. if (cl5GetState () != CL5_STATE_OPEN)
  849. {
  850. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  851. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  852. "replica_execute_cl2ldif_task: %s\n", returntext);
  853. rc = LDAP_OPERATIONS_ERROR;
  854. goto bail;
  855. }
  856. rlist[0] = r;
  857. rlist[1] = NULL;
  858. /* file is stored in the changelog directory and is named
  859. <replica name>.ldif */
  860. clDir = cl5GetDir ();
  861. if (NULL == clDir) {
  862. rc = LDAP_OPERATIONS_ERROR;
  863. goto bail;
  864. }
  865. replica = (Replica*)object_get_data (r);
  866. if (NULL == replica) {
  867. rc = LDAP_OPERATIONS_ERROR;
  868. goto bail;
  869. }
  870. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  871. slapi_ch_free_string (&clDir);
  872. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  873. "Beginning changelog export of replica \"%s\"\n",
  874. replica_get_name(replica));
  875. rc = cl5ExportLDIF (fName, rlist);
  876. if (rc == CL5_SUCCESS) {
  877. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  878. "Finished changelog export of replica \"%s\"\n",
  879. replica_get_name(replica));
  880. rc = LDAP_SUCCESS;
  881. } else {
  882. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  883. "Failed changelog export replica %s; "
  884. "changelog error - %d", replica_get_name(replica), rc);
  885. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  886. "replica_execute_cl2ldif_task: %s\n", returntext);
  887. rc = LDAP_OPERATIONS_ERROR;
  888. }
  889. bail:
  890. return rc;
  891. }
  892. static int replica_execute_ldif2cl_task (Object *r, char *returntext)
  893. {
  894. int rc, imprc = 0;
  895. Object *rlist [2];
  896. Replica *replica;
  897. char fName [MAXPATHLEN];
  898. char *clDir = NULL;
  899. changelog5Config config;
  900. if (cl5GetState () != CL5_STATE_OPEN)
  901. {
  902. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  903. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  904. "replica_execute_ldif2cl_task: %s\n", returntext);
  905. rc = LDAP_OPERATIONS_ERROR;
  906. goto bail;
  907. }
  908. rlist[0] = r;
  909. rlist[1] = NULL;
  910. /* file is stored in the changelog directory and is named
  911. <replica name>.ldif */
  912. clDir = cl5GetDir ();
  913. if (NULL == clDir) {
  914. rc = LDAP_OPERATIONS_ERROR;
  915. goto bail;
  916. }
  917. replica = (Replica*)object_get_data (r);
  918. if (NULL == replica) {
  919. rc = LDAP_OPERATIONS_ERROR;
  920. goto bail;
  921. }
  922. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  923. rc = cl5Close();
  924. if (rc != CL5_SUCCESS)
  925. {
  926. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  927. "failed to close changelog to import changelog data; "
  928. "changelog error - %d", rc);
  929. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  930. "replica_execute_ldif2cl_task: %s\n", returntext);
  931. rc = LDAP_OPERATIONS_ERROR;
  932. goto bail;
  933. }
  934. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  935. "Beginning changelog import of replica \"%s\"\n",
  936. replica_get_name(replica));
  937. imprc = cl5ImportLDIF (clDir, fName, rlist);
  938. slapi_ch_free_string (&clDir);
  939. if (CL5_SUCCESS == imprc)
  940. {
  941. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  942. "Finished changelog import of replica \"%s\"\n",
  943. replica_get_name(replica));
  944. }
  945. else
  946. {
  947. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  948. "Failed changelog import replica %s; "
  949. "changelog error - %d", replica_get_name(replica), rc);
  950. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  951. "replica_execute_ldif2cl_task: %s\n", returntext);
  952. imprc = LDAP_OPERATIONS_ERROR;
  953. }
  954. changelog5_read_config (&config);
  955. /* restart changelog */
  956. rc = cl5Open (config.dir, &config.dbconfig);
  957. if (CL5_SUCCESS == rc)
  958. {
  959. rc = LDAP_SUCCESS;
  960. }
  961. else
  962. {
  963. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  964. "replica_execute_ldif2cl_task: failed to start changelog at %s\n",
  965. config.dir?config.dir:"null config dir");
  966. rc = LDAP_OPERATIONS_ERROR;
  967. }
  968. bail:
  969. changelog5_config_done(&config);
  970. /* if cl5ImportLDIF returned an error, report it first. */
  971. return imprc?imprc:rc;
  972. }
  973. static multimaster_mtnode_extension *
  974. _replica_config_get_mtnode_ext (const Slapi_Entry *e)
  975. {
  976. const char *replica_root;
  977. Slapi_DN *sdn = NULL;
  978. mapping_tree_node *mtnode;
  979. multimaster_mtnode_extension *ext = NULL;
  980. /* retirve root of the tree for which replica is configured */
  981. replica_root = slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  982. if (replica_root == NULL)
  983. {
  984. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  985. "configuration entry %s missing %s attribute\n",
  986. slapi_entry_get_dn((Slapi_Entry *)e),
  987. attr_replicaRoot);
  988. return NULL;
  989. }
  990. sdn = slapi_sdn_new_dn_passin (replica_root);
  991. /* locate mapping tree node for the specified subtree */
  992. mtnode = slapi_get_mapping_tree_node_by_dn (sdn);
  993. if (mtnode == NULL)
  994. {
  995. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  996. "failed to locate mapping tree node for dn %s\n",
  997. slapi_sdn_get_dn(sdn));
  998. }
  999. else
  1000. {
  1001. /* check if replica object already exists for the specified subtree */
  1002. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  1003. }
  1004. slapi_sdn_free (&sdn);
  1005. return ext;
  1006. }
  1007. int
  1008. replica_execute_cleanruv_task_ext(Object *r, ReplicaId rid)
  1009. {
  1010. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_extop: calling clean_ruv_ext\n");
  1011. return replica_execute_cleanruv_task(r, rid, NULL);
  1012. }
  1013. static int
  1014. replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext /* not used */)
  1015. {
  1016. int rc = 0;
  1017. Object *RUVObj;
  1018. RUV *local_ruv = NULL;
  1019. Replica *replica = (Replica*)object_get_data (r);
  1020. PR_ASSERT (replica);
  1021. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: cleaning rid (%d)...\n",(int)rid);
  1022. RUVObj = replica_get_ruv(replica);
  1023. PR_ASSERT(RUVObj);
  1024. local_ruv = (RUV*)object_get_data (RUVObj);
  1025. /* Need to check that :
  1026. * - rid is not the local one
  1027. * - rid is not the last one
  1028. */
  1029. if ((replica_get_rid(replica) == rid) ||
  1030. (ruv_replica_count(local_ruv) <= 1)) {
  1031. return LDAP_UNWILLING_TO_PERFORM;
  1032. }
  1033. rc = ruv_delete_replica(local_ruv, rid);
  1034. replica_set_ruv_dirty(replica);
  1035. replica_write_ruv(replica);
  1036. object_release(RUVObj);
  1037. /* Update Mapping Tree to reflect RUV changes */
  1038. consumer5_set_mapping_tree_state_for_replica(replica, NULL);
  1039. /*
  1040. * Clean the changelog RUV's, and set the rids
  1041. */
  1042. cl5CleanRUV(rid);
  1043. set_cleaned_rid(rid);
  1044. delete_released_rid();
  1045. if (rc != RUV_SUCCESS){
  1046. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanruv_task: task failed(%d)\n",rc);
  1047. return LDAP_OPERATIONS_ERROR;
  1048. }
  1049. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: finished successfully\n");
  1050. return LDAP_SUCCESS;
  1051. }
  1052. static int
  1053. replica_execute_cleanall_ruv_task (Object *r, ReplicaId rid, char *returntext)
  1054. {
  1055. Repl_Connection *conn;
  1056. Replica *replica = (Replica*)object_get_data (r);
  1057. Object *agmt_obj;
  1058. Repl_Agmt *agmt;
  1059. ConnResult crc;
  1060. const Slapi_DN *dn = NULL;
  1061. struct berval *payload = NULL;
  1062. char *ridstr = NULL;
  1063. int send_msgid = 0;
  1064. int rc = 0;
  1065. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanAllRUV_task: cleaning rid (%d)...\n",(int)rid);
  1066. /*
  1067. * Create payload
  1068. */
  1069. ridstr = slapi_ch_smprintf("%d:%s", rid, slapi_sdn_get_dn(replica_get_root(replica)));
  1070. payload = create_ruv_payload(ridstr);
  1071. if(payload == NULL){
  1072. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: failed to create ext op payload, aborting task\n");
  1073. goto done;
  1074. }
  1075. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  1076. while (agmt_obj)
  1077. {
  1078. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1079. if(!agmt_is_enabled(agmt)){
  1080. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  1081. continue;
  1082. }
  1083. dn = agmt_get_dn_byref(agmt);
  1084. conn = (Repl_Connection *)agmt_get_connection(agmt);
  1085. if(conn == NULL){
  1086. /* no connection for this agreement, and move on */
  1087. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: the replica (%s), is "
  1088. "missing the connection. This replica will not be cleaned.\n", slapi_sdn_get_dn(dn));
  1089. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  1090. continue;
  1091. }
  1092. crc = conn_connect(conn);
  1093. if (CONN_OPERATION_FAILED == crc ){
  1094. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: failed to connect "
  1095. "to repl agreement connection (%s), error %d\n",slapi_sdn_get_dn(dn), ACQUIRE_TRANSIENT_ERROR);
  1096. } else if (CONN_SSL_NOT_ENABLED == crc){
  1097. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: failed to acquire "
  1098. "repl agmt connection (%s), errror %d\n",slapi_sdn_get_dn(dn), ACQUIRE_FATAL_ERROR);
  1099. } else {
  1100. conn_cancel_linger(conn);
  1101. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_OID, payload, NULL, &send_msgid);
  1102. if (CONN_OPERATION_SUCCESS != crc){
  1103. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: failed to send "
  1104. "cleanruv extended op to repl agmt (%s), error %d\n", slapi_sdn_get_dn(dn), crc);
  1105. } else {
  1106. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanAllRUV_task: successfully sent "
  1107. "cleanruv extended op to (%s)\n",slapi_sdn_get_dn(dn));
  1108. }
  1109. conn_start_linger(conn);
  1110. }
  1111. if(crc){
  1112. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: replica (%s) has not "
  1113. "been cleaned. You will need to rerun the CLEANALLRUV task on this replica.\n", slapi_sdn_get_dn(dn));
  1114. rc = crc;
  1115. }
  1116. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  1117. }
  1118. done:
  1119. if(payload)
  1120. ber_bvfree(payload);
  1121. slapi_ch_free_string(&ridstr);
  1122. /*
  1123. * Now run the cleanruv task
  1124. */
  1125. replica_execute_cleanruv_task (r, rid, returntext);
  1126. if(rc == 0){
  1127. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanAllRUV_task: operation successful\n");
  1128. } else {
  1129. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: operation failed (%d)\n",rc);
  1130. }
  1131. return rc;
  1132. }
  1133. static int
  1134. replica_execute_release_ruv_task(Object *r, ReplicaId rid, char *returntext)
  1135. {
  1136. Repl_Connection *conn;
  1137. Replica *replica = (Replica*)object_get_data (r);
  1138. Object *agmt_obj;
  1139. Repl_Agmt *agmt;
  1140. ConnResult crc;
  1141. const Slapi_DN *dn = NULL;
  1142. struct berval *payload = NULL;
  1143. char *ridstr = NULL;
  1144. int send_msgid = 0;
  1145. int rc = 0;
  1146. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "releaseRUV_task: releasing rid (%d)...\n", rid);
  1147. /*
  1148. * Set the released rid, and trigger cl trimmming
  1149. */
  1150. set_released_rid((int)rid);
  1151. trigger_cl_trimming();
  1152. /*
  1153. * Create payload
  1154. */
  1155. ridstr = slapi_ch_smprintf("%d:%s", rid, slapi_sdn_get_dn(replica_get_root(replica)));
  1156. payload = create_ruv_payload(ridstr);
  1157. if(payload == NULL){
  1158. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "releaseRUV_task: failed to create ext op payload, aborting op\n");
  1159. rc = -1;
  1160. goto done;
  1161. }
  1162. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  1163. while (agmt_obj)
  1164. {
  1165. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1166. if(!agmt_is_enabled(agmt)){
  1167. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  1168. continue;
  1169. }
  1170. dn = agmt_get_dn_byref(agmt);
  1171. conn = (Repl_Connection *)agmt_get_connection(agmt);
  1172. if(conn == NULL){
  1173. /* no connection for this agreement, log error, and move on */
  1174. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  1175. continue;
  1176. }
  1177. crc = conn_connect(conn);
  1178. if (CONN_OPERATION_FAILED == crc ){
  1179. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "releaseRUV_task: failed to connect "
  1180. "to repl agmt (%s), error %d\n",slapi_sdn_get_dn(dn), ACQUIRE_TRANSIENT_ERROR);
  1181. rc = LDAP_OPERATIONS_ERROR;
  1182. } else if (CONN_SSL_NOT_ENABLED == crc){
  1183. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "releaseRUV_task: failed to acquire "
  1184. "repl agmt (%s), error %d\n",slapi_sdn_get_dn(dn), ACQUIRE_FATAL_ERROR);
  1185. rc = LDAP_OPERATIONS_ERROR;
  1186. } else {
  1187. conn_cancel_linger(conn);
  1188. crc = conn_send_extended_operation(conn, REPL_RELEASERUV_OID, payload, NULL, &send_msgid);
  1189. if (CONN_OPERATION_SUCCESS != crc){
  1190. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "releaseRUV_task: failed to send "
  1191. "releaseruv extended op to repl agmt (%s), error %d\n", slapi_sdn_get_dn(dn), crc);
  1192. rc = LDAP_OPERATIONS_ERROR;
  1193. } else {
  1194. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "releaseRUV_task: successfully sent "
  1195. "extended op to (%s)\n",slapi_sdn_get_dn(dn));
  1196. }
  1197. conn_start_linger(conn);
  1198. }
  1199. if(crc){
  1200. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "releaseRUV_task: replica (%s) has not "
  1201. "been cleaned. You will need to rerun the RELEASERUV task on this replica\n",
  1202. slapi_sdn_get_dn(dn));
  1203. rc = crc;
  1204. }
  1205. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  1206. }
  1207. done:
  1208. /*
  1209. * reset the released/clean rid
  1210. */
  1211. if(rc == 0){
  1212. set_released_rid(ALREADY_RELEASED);
  1213. delete_cleaned_rid();
  1214. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "releaseRUV_task: Successfully released rid (%d)\n", rid);
  1215. } else {
  1216. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "releaseRUV_task: Failed to release rid (%d), error (%d)\n", rid, rc);
  1217. }
  1218. if(payload)
  1219. ber_bvfree(payload);
  1220. slapi_ch_free_string(&ridstr);
  1221. return rc;
  1222. }
  1223. static struct berval *
  1224. create_ruv_payload(char *value){
  1225. struct berval *req_data = NULL;
  1226. BerElement *tmp_bere = NULL;
  1227. if ((tmp_bere = der_alloc()) == NULL){
  1228. goto error;
  1229. }
  1230. if (ber_printf(tmp_bere, "{s", value) == -1){
  1231. goto error;
  1232. }
  1233. if (ber_printf(tmp_bere, "}") == -1){
  1234. goto error;
  1235. }
  1236. if (ber_flatten(tmp_bere, &req_data) == -1){
  1237. goto error;
  1238. }
  1239. goto done;
  1240. error:
  1241. if (NULL != req_data){
  1242. ber_bvfree(req_data);
  1243. req_data = NULL;
  1244. }
  1245. done:
  1246. if (NULL != tmp_bere){
  1247. ber_free(tmp_bere, 1);
  1248. tmp_bere = NULL;
  1249. }
  1250. return req_data;
  1251. }
  1252. int
  1253. is_cleaned_rid(ReplicaId rid)
  1254. {
  1255. if(rid == cleaned_rid){
  1256. return 1;
  1257. } else {
  1258. return 0;
  1259. }
  1260. }
  1261. void
  1262. set_cleaned_rid( ReplicaId rid )
  1263. {
  1264. cleaned_rid = rid;
  1265. }
  1266. void
  1267. delete_cleaned_rid()
  1268. {
  1269. cleaned_rid = 0;
  1270. }
  1271. int
  1272. get_released_rid()
  1273. {
  1274. return released_rid;
  1275. }
  1276. int
  1277. is_released_rid(int rid)
  1278. {
  1279. if(rid == released_rid){
  1280. return 1;
  1281. } else {
  1282. return 0;
  1283. }
  1284. }
  1285. int
  1286. is_already_released_rid()
  1287. {
  1288. if(released_rid == ALREADY_RELEASED){
  1289. return 1;
  1290. } else {
  1291. return 0;
  1292. }
  1293. }
  1294. void
  1295. set_released_rid( int rid )
  1296. {
  1297. released_rid = rid;
  1298. }
  1299. void
  1300. delete_released_rid()
  1301. {
  1302. released_rid = 0;
  1303. }