repl_extop.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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. #include "slapi-plugin.h"
  42. #include "repl.h"
  43. #include "repl5.h"
  44. #include "repl5_prot_private.h"
  45. #include "cl5_api.h"
  46. #define ENABLE_TEST_TICKET_374
  47. #ifdef ENABLE_TEST_TICKET_374
  48. #include <unistd.h> /* for usleep */
  49. #endif
  50. /*
  51. * repl_extop.c - there are two types of functions in this file:
  52. * - Code that implements an extended operation plugin.
  53. * The replication DLL arranges for this code to
  54. * be called when a StartNSDS50ReplicationRequest
  55. * or an EndNSDS50ReplicationRequest extended operation
  56. * is received.
  57. * - Code that sends extended operations on an already-
  58. * established client connection.
  59. *
  60. * The requestValue portion of the StartNSDS50ReplicationRequest
  61. * looks like this:
  62. *
  63. * requestValue ::= SEQUENCE {
  64. * replProtocolOID LDAPOID,
  65. * replicatedTree LDAPDN,
  66. supplierRUV OCTET STRING
  67. * referralURLs SET of LDAPURL OPTIONAL
  68. * csn OCTET STRING OPTIONAL
  69. * }
  70. *
  71. */
  72. static int check_replica_id_uniqueness(Replica *replica, RUV *supplier_ruv);
  73. static multimaster_mtnode_extension *replica_config_get_mtnode_by_dn(const char *dn);
  74. static int
  75. encode_ruv (BerElement *ber, const RUV *ruv)
  76. {
  77. int rc = LDAP_SUCCESS;
  78. struct berval **bvals = NULL;
  79. PR_ASSERT (ber);
  80. PR_ASSERT (ruv);
  81. if (ruv_to_bervals(ruv, &bvals) != 0)
  82. {
  83. rc = LDAP_OPERATIONS_ERROR;
  84. goto done;
  85. }
  86. if (ber_printf(ber, "[V]", bvals) == -1)
  87. {
  88. rc = LDAP_ENCODING_ERROR;
  89. goto done;
  90. }
  91. rc = LDAP_SUCCESS;
  92. done:
  93. if (bvals)
  94. ber_bvecfree (bvals);
  95. return rc;
  96. }
  97. /* The data_guid and data parameters should only be set if we
  98. * are talking with a 9.0 replica. */
  99. static struct berval *
  100. create_ReplicationExtopPayload(const char *protocol_oid,
  101. const char *repl_root, char **extra_referrals, CSN *csn,
  102. int send_end, const char *data_guid, const struct berval *data)
  103. {
  104. struct berval *req_data = NULL;
  105. BerElement *tmp_bere = NULL;
  106. int rc = 0;
  107. Object *repl_obj = NULL, *ruv_obj = NULL;
  108. Replica *repl;
  109. RUV *ruv;
  110. Slapi_DN *sdn = NULL;
  111. PR_ASSERT(protocol_oid != NULL || send_end);
  112. PR_ASSERT(repl_root != NULL);
  113. /* Create the request data */
  114. if ((tmp_bere = der_alloc()) == NULL)
  115. {
  116. rc = LDAP_ENCODING_ERROR;
  117. goto loser;
  118. }
  119. if (!send_end)
  120. {
  121. if (ber_printf(tmp_bere, "{ss", protocol_oid, repl_root) == -1)
  122. {
  123. rc = LDAP_ENCODING_ERROR;
  124. goto loser;
  125. }
  126. }
  127. else
  128. {
  129. if (ber_printf(tmp_bere, "{s", repl_root) == -1)
  130. {
  131. rc = LDAP_ENCODING_ERROR;
  132. goto loser;
  133. }
  134. }
  135. sdn = slapi_sdn_new_dn_byref(repl_root);
  136. repl_obj = replica_get_replica_from_dn (sdn);
  137. if (repl_obj == NULL)
  138. {
  139. rc = LDAP_OPERATIONS_ERROR;
  140. goto loser;
  141. }
  142. repl = (Replica*)object_get_data (repl_obj);
  143. PR_ASSERT (repl);
  144. ruv_obj = replica_get_ruv (repl);
  145. if (ruv_obj == NULL)
  146. {
  147. rc = LDAP_OPERATIONS_ERROR;
  148. goto loser;
  149. }
  150. ruv = object_get_data(ruv_obj);
  151. PR_ASSERT(ruv);
  152. /* send supplier's ruv so that consumer can build its own referrals.
  153. In case of total protocol, it is also used as consumer's ruv once
  154. protocol successfully completes */
  155. /* We need to encode and send each time the local ruv in case we have changed it */
  156. rc = encode_ruv (tmp_bere, ruv);
  157. if (rc != 0)
  158. {
  159. goto loser;
  160. }
  161. if (!send_end)
  162. {
  163. char s[CSN_STRSIZE];
  164. ReplicaId rid;
  165. char *local_replica_referral[2] = {0};
  166. char **referrals_to_send = NULL;
  167. /* Add the referral URL(s), if present */
  168. rid = replica_get_rid(repl);
  169. if (!ruv_contains_replica(ruv, rid))
  170. {
  171. /*
  172. * In the event that there is no RUV component for this replica (e.g.
  173. * if the database was just loaded from LDIF and no local CSNs have been
  174. * generated), then we need to explicitly add this server to the list
  175. * of referrals, since it wouldn't have been sent with the RUV.
  176. */
  177. local_replica_referral[0] = (char *)multimaster_get_local_purl(); /* XXXggood had to cast away const */
  178. }
  179. charray_merge(&referrals_to_send, extra_referrals, 0);
  180. charray_merge(&referrals_to_send, local_replica_referral, 0);
  181. if (NULL != referrals_to_send)
  182. {
  183. if (ber_printf(tmp_bere, "[v]", referrals_to_send) == -1)
  184. {
  185. rc = LDAP_ENCODING_ERROR;
  186. goto loser;
  187. }
  188. slapi_ch_free((void **)&referrals_to_send);
  189. }
  190. /* Add the CSN */
  191. PR_ASSERT(NULL != csn);
  192. if (ber_printf(tmp_bere, "s", csn_as_string(csn,PR_FALSE,s)) == -1)
  193. {
  194. rc = LDAP_ENCODING_ERROR;
  195. goto loser;
  196. }
  197. }
  198. /* If we have data to send to a 9.0 style replica, set it here. */
  199. if (data_guid && data) {
  200. if (ber_printf(tmp_bere, "sO", data_guid, data) == -1)
  201. {
  202. rc = LDAP_ENCODING_ERROR;
  203. goto loser;
  204. }
  205. }
  206. if (ber_printf(tmp_bere, "}") == -1)
  207. {
  208. rc = LDAP_ENCODING_ERROR;
  209. goto loser;
  210. }
  211. if (ber_flatten(tmp_bere, &req_data) == -1)
  212. {
  213. rc = LDAP_LOCAL_ERROR;
  214. goto loser;
  215. }
  216. /* Success */
  217. goto done;
  218. loser:
  219. /* Free stuff we allocated */
  220. if (NULL != req_data)
  221. {
  222. ber_bvfree(req_data); req_data = NULL;
  223. }
  224. done:
  225. if (NULL != tmp_bere)
  226. {
  227. ber_free(tmp_bere, 1); tmp_bere = NULL;
  228. }
  229. if (NULL != sdn)
  230. {
  231. slapi_sdn_free (&sdn); /* Put on stack instead of allocating? */
  232. }
  233. if (NULL != repl_obj)
  234. {
  235. object_release (repl_obj);
  236. }
  237. if (NULL != ruv_obj)
  238. {
  239. object_release (ruv_obj);
  240. }
  241. return req_data;
  242. }
  243. struct berval *
  244. NSDS50StartReplicationRequest_new(const char *protocol_oid,
  245. const char *repl_root, char **extra_referrals, CSN *csn)
  246. {
  247. return(create_ReplicationExtopPayload(protocol_oid,
  248. repl_root, extra_referrals, csn, 0, 0, 0));
  249. }
  250. struct berval *
  251. NSDS90StartReplicationRequest_new(const char *protocol_oid,
  252. const char *repl_root, char **extra_referrals, CSN *csn,
  253. const char *data_guid, const struct berval *data)
  254. {
  255. return(create_ReplicationExtopPayload(protocol_oid,
  256. repl_root, extra_referrals, csn, 0, data_guid, data));
  257. }
  258. struct berval *
  259. NSDS50EndReplicationRequest_new(char *repl_root)
  260. {
  261. return(create_ReplicationExtopPayload(NULL, repl_root, NULL, NULL, 1, 0, 0));
  262. }
  263. static int
  264. decode_ruv (BerElement *ber, RUV **ruv)
  265. {
  266. int rc = -1;
  267. struct berval **bvals = NULL;
  268. PR_ASSERT (ber && ruv);
  269. if (ber_scanf(ber, "[V]", &bvals) == LBER_DEFAULT)
  270. {
  271. goto done;
  272. }
  273. if (ruv_init_from_bervals(bvals, ruv) != 0)
  274. {
  275. goto done;
  276. }
  277. rc = 0;
  278. done:
  279. if (bvals)
  280. ber_bvecfree (bvals);
  281. return rc;
  282. }
  283. /*
  284. * Decode an NSDS50 or NSDS90 Start Replication Request extended
  285. * operation. Returns 0 on success, -1 on decoding error.
  286. * The caller is responsible for freeing protocol_oid,
  287. * repl_root, referrals, csn, data_guid, and data.
  288. */
  289. static int
  290. decode_startrepl_extop(Slapi_PBlock *pb, char **protocol_oid, char **repl_root,
  291. RUV **supplier_ruv, char ***extra_referrals, char **csnstr,
  292. char **data_guid, struct berval **data, int *is90)
  293. {
  294. char *extop_oid = NULL;
  295. struct berval *extop_value = NULL;
  296. BerElement *tmp_bere = NULL;
  297. ber_len_t len;
  298. int rc = 0;
  299. PR_ASSERT (pb && protocol_oid && repl_root && supplier_ruv && extra_referrals && csnstr && data_guid && data);
  300. *protocol_oid = NULL;
  301. *repl_root = NULL;
  302. *supplier_ruv = NULL;
  303. *extra_referrals = NULL;
  304. *csnstr = NULL;
  305. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &extop_oid);
  306. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &extop_value);
  307. if ((NULL == extop_oid) ||
  308. ((strcmp(extop_oid, REPL_START_NSDS50_REPLICATION_REQUEST_OID) != 0) &&
  309. (strcmp(extop_oid, REPL_START_NSDS90_REPLICATION_REQUEST_OID) != 0)) ||
  310. !BV_HAS_DATA(extop_value))
  311. {
  312. /* bogus */
  313. rc = -1;
  314. goto free_and_return;
  315. }
  316. /* Set a flag to let the caller know if this is a 9.0 style start extop */
  317. if (strcmp(extop_oid, REPL_START_NSDS90_REPLICATION_REQUEST_OID) == 0)
  318. {
  319. *is90 = 1;
  320. }
  321. else
  322. {
  323. *is90 = 0;
  324. }
  325. if ((tmp_bere = ber_init(extop_value)) == NULL)
  326. {
  327. rc = -1;
  328. goto free_and_return;
  329. }
  330. if (ber_scanf(tmp_bere, "{") == LBER_ERROR)
  331. {
  332. rc = -1;
  333. goto free_and_return;
  334. }
  335. /* Get the required protocol OID and root of replicated subtree */
  336. if (ber_get_stringa(tmp_bere, protocol_oid) == LBER_DEFAULT)
  337. {
  338. rc = -1;
  339. goto free_and_return;
  340. }
  341. if (ber_get_stringa(tmp_bere, repl_root) == LBER_DEFAULT)
  342. {
  343. rc = -1;
  344. goto free_and_return;
  345. }
  346. /* get supplier's ruv */
  347. if (decode_ruv (tmp_bere, supplier_ruv) == -1)
  348. {
  349. rc = -1;
  350. goto free_and_return;
  351. }
  352. /* Get the optional set of referral URLs */
  353. if (ber_peek_tag(tmp_bere, &len) == LBER_SET)
  354. {
  355. if (ber_scanf(tmp_bere, "[v]", extra_referrals) == LBER_ERROR)
  356. {
  357. rc = -1;
  358. goto free_and_return;
  359. }
  360. }
  361. /* Get the CSN */
  362. if (ber_get_stringa(tmp_bere, csnstr) == LBER_ERROR)
  363. {
  364. rc = -1;
  365. goto free_and_return;
  366. }
  367. /* Get the optional replication session callback data. */
  368. if (ber_peek_tag(tmp_bere, &len) == LBER_OCTETSTRING)
  369. {
  370. if (ber_get_stringa(tmp_bere, data_guid) == LBER_ERROR)
  371. {
  372. rc = -1;
  373. goto free_and_return;
  374. }
  375. /* If a data_guid was specified, data must be specified as well. */
  376. if (ber_peek_tag(tmp_bere, &len) == LBER_OCTETSTRING)
  377. {
  378. if (ber_get_stringal(tmp_bere, data) == LBER_ERROR)
  379. {
  380. rc = -1;
  381. goto free_and_return;
  382. }
  383. }
  384. else
  385. {
  386. rc = -1;
  387. goto free_and_return;
  388. }
  389. }
  390. if (ber_scanf(tmp_bere, "}") == LBER_ERROR)
  391. {
  392. rc = -1;
  393. goto free_and_return;
  394. }
  395. free_and_return:
  396. if (-1 == rc)
  397. {
  398. /* Free everything when error encountered */
  399. /* slapi_ch_free accepts NULL pointer */
  400. slapi_ch_free ((void**)protocol_oid);
  401. slapi_ch_free ((void**)repl_root);
  402. slapi_ch_array_free (*extra_referrals);
  403. *extra_referrals = NULL;
  404. slapi_ch_free ((void**)csnstr);
  405. if (*supplier_ruv)
  406. {
  407. ruv_destroy (supplier_ruv);
  408. }
  409. }
  410. if (NULL != tmp_bere)
  411. {
  412. ber_free(tmp_bere, 1);
  413. tmp_bere = NULL;
  414. }
  415. return rc;
  416. }
  417. /*
  418. * Decode an NSDS50 End Replication Request extended
  419. * operation. Returns 0 on success, -1 on decoding error.
  420. * The caller is responsible for freeing repl_root.
  421. */
  422. static int
  423. decode_endrepl_extop(Slapi_PBlock *pb, char **repl_root)
  424. {
  425. char *extop_oid = NULL;
  426. struct berval *extop_value = NULL;
  427. BerElement *tmp_bere = NULL;
  428. int rc = 0;
  429. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &extop_oid);
  430. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &extop_value);
  431. if ((NULL == extop_oid) ||
  432. (strcmp(extop_oid, REPL_END_NSDS50_REPLICATION_REQUEST_OID) != 0) ||
  433. !BV_HAS_DATA(extop_value))
  434. {
  435. /* bogus */
  436. rc = -1;
  437. goto free_and_return;
  438. }
  439. if ((tmp_bere = ber_init(extop_value)) == NULL)
  440. {
  441. rc = -1;
  442. goto free_and_return;
  443. }
  444. if (ber_scanf(tmp_bere, "{") == LBER_DEFAULT)
  445. {
  446. rc = -1;
  447. goto free_and_return;
  448. }
  449. /* Get the required root of replicated subtree */
  450. if (ber_get_stringa(tmp_bere, repl_root) == LBER_DEFAULT)
  451. {
  452. rc = -1;
  453. goto free_and_return;
  454. }
  455. if (ber_scanf(tmp_bere, "}") == LBER_DEFAULT)
  456. {
  457. rc = -1;
  458. goto free_and_return;
  459. }
  460. free_and_return:
  461. if (NULL != tmp_bere)
  462. {
  463. ber_free(tmp_bere, 1);
  464. tmp_bere = NULL;
  465. }
  466. return rc;
  467. }
  468. /*
  469. * Decode an NSDS50ReplicationResponse or NSDS90ReplicationResponse
  470. * extended response. The extended response just contains a sequence
  471. * that contains:
  472. * 1) An integer response code
  473. * 2) An optional array of bervals representing the consumer
  474. * replica's update vector
  475. * 3) An optional data guid and data string if this is a 9.0
  476. * style response
  477. * Returns 0 on success, or -1 if the response could not be parsed.
  478. */
  479. int
  480. decode_repl_ext_response(struct berval *bvdata, int *response_code,
  481. struct berval ***ruv_bervals, char **data_guid, struct berval **data)
  482. {
  483. BerElement *tmp_bere = NULL;
  484. int return_value = 0;
  485. PR_ASSERT(NULL != response_code);
  486. PR_ASSERT(NULL != ruv_bervals);
  487. if ((NULL == response_code) || (NULL == ruv_bervals) ||
  488. (NULL == data_guid) || (NULL == data) || !BV_HAS_DATA(bvdata))
  489. {
  490. return_value = -1;
  491. }
  492. else
  493. {
  494. ber_len_t len;
  495. ber_int_t temp_response_code = 0;
  496. *ruv_bervals = NULL;
  497. if ((tmp_bere = ber_init(bvdata)) == NULL)
  498. {
  499. return_value = -1;
  500. }
  501. else if (ber_scanf(tmp_bere, "{e", &temp_response_code) == LBER_ERROR)
  502. {
  503. return_value = -1;
  504. }
  505. else if (ber_peek_tag(tmp_bere, &len) == LBER_SEQUENCE)
  506. {
  507. if (ber_scanf(tmp_bere, "{V}", ruv_bervals) == LBER_ERROR)
  508. {
  509. return_value = -1;
  510. }
  511. }
  512. /* Check for optional data from replication session callback */
  513. if (ber_peek_tag(tmp_bere, &len) == LBER_OCTETSTRING)
  514. {
  515. if (ber_scanf(tmp_bere, "aO}", data_guid, data) == LBER_ERROR)
  516. {
  517. return_value = -1;
  518. }
  519. }
  520. else if (ber_scanf(tmp_bere, "}") == LBER_ERROR)
  521. {
  522. return_value = -1;
  523. }
  524. *response_code = (int)temp_response_code;
  525. }
  526. if (0 != return_value)
  527. {
  528. if (NULL != ruv_bervals && NULL != *ruv_bervals)
  529. {
  530. ber_bvecfree(*ruv_bervals);
  531. }
  532. }
  533. if (NULL != tmp_bere)
  534. {
  535. ber_free(tmp_bere, 1); tmp_bere = NULL;
  536. }
  537. return return_value;
  538. }
  539. /*
  540. * This plugin entry point is called whenever a
  541. * StartNSDS50ReplicationRequest is received.
  542. */
  543. int
  544. multimaster_extop_StartNSDS50ReplicationRequest(Slapi_PBlock *pb)
  545. {
  546. int return_value = SLAPI_PLUGIN_EXTENDED_NOT_HANDLED;
  547. ber_int_t response = 0;
  548. int rc = 0;
  549. BerElement *resp_bere = NULL;
  550. struct berval *resp_bval = NULL;
  551. char *protocol_oid = NULL;
  552. char *repl_root = NULL;
  553. Slapi_DN *repl_root_sdn = NULL;
  554. char **referrals = NULL;
  555. Object *replica_object = NULL;
  556. Replica *replica = NULL;
  557. void *conn;
  558. consumer_connection_extension *connext = NULL;
  559. char *replicacsnstr = NULL;
  560. CSN *replicacsn = NULL;
  561. int zero = 0;
  562. int one = 1;
  563. RUV *ruv = NULL;
  564. struct berval **ruv_bervals = NULL;
  565. CSNGen *gen = NULL;
  566. Object *gen_obj = NULL;
  567. Slapi_DN *bind_sdn = NULL;
  568. char *bind_dn = NULL;
  569. Object *ruv_object = NULL;
  570. RUV *supplier_ruv = NULL;
  571. PRUint64 connid = 0;
  572. int opid = 0;
  573. PRBool isInc = PR_FALSE; /* true if incremental update */
  574. char *locking_purl = NULL; /* the supplier contacting us */
  575. char *current_purl = NULL; /* the supplier which already has exclusive access */
  576. char locking_session[24];
  577. char *data_guid = NULL;
  578. struct berval *data = NULL;
  579. int is90 = 0;
  580. /* Decode the extended operation */
  581. if (decode_startrepl_extop(pb, &protocol_oid, &repl_root, &supplier_ruv,
  582. &referrals, &replicacsnstr, &data_guid, &data, &is90) == -1)
  583. {
  584. response = NSDS50_REPL_DECODING_ERROR;
  585. goto send_response;
  586. }
  587. if (NULL == protocol_oid || NULL == repl_root || NULL == replicacsnstr)
  588. {
  589. response = NSDS50_REPL_DECODING_ERROR;
  590. goto send_response;
  591. }
  592. slapi_pblock_get(pb, SLAPI_CONN_ID, &connid);
  593. slapi_pblock_get(pb, SLAPI_OPERATION_ID, &opid);
  594. /*
  595. * Get a hold of the connection extension object and
  596. * make sure it's there.
  597. */
  598. slapi_pblock_get(pb, SLAPI_CONNECTION, &conn);
  599. connext = consumer_connection_extension_acquire_exclusive_access(conn, connid, opid);
  600. if (NULL == connext)
  601. {
  602. /* TEL 20120531: This used to be a much worse and unexpected thing
  603. * before acquiring exclusive access to the connext. Now it should
  604. * be highly unusual, but not completely unheard of. We don't want to
  605. * return an internal error here as before, because it will eventually
  606. * result in a fatal error on the other end. Better to tell it
  607. * we are busy instead--which is also probably true. */
  608. response = NSDS50_REPL_REPLICA_BUSY;
  609. goto send_response;
  610. }
  611. /* Verify that we know about this replication protocol OID */
  612. if (strcmp(protocol_oid, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID) == 0)
  613. {
  614. if (repl_session_plugin_call_recv_acquire_cb(repl_root, 0 /* is_total == FALSE */,
  615. data_guid, data))
  616. {
  617. slapi_ch_free_string(&data_guid);
  618. ber_bvfree(data);
  619. data = NULL;
  620. response = NSDS50_REPL_BACKOFF;
  621. goto send_response;
  622. } else {
  623. slapi_ch_free_string(&data_guid);
  624. ber_bvfree(data);
  625. data = NULL;
  626. }
  627. /* Stash info that this is an incremental update session */
  628. connext->repl_protocol_version = REPL_PROTOCOL_50_INCREMENTAL;
  629. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  630. "conn=%" NSPRIu64 " op=%d repl=\"%s\": Begin incremental protocol\n",
  631. connid, opid, repl_root);
  632. isInc = PR_TRUE;
  633. }
  634. else if (strcmp(protocol_oid, REPL_NSDS50_TOTAL_PROTOCOL_OID) == 0)
  635. {
  636. if (repl_session_plugin_call_recv_acquire_cb(repl_root, 1 /* is_total == TRUE */,
  637. data_guid, data))
  638. {
  639. slapi_ch_free_string(&data_guid);
  640. ber_bvfree(data);
  641. data = NULL;
  642. response = NSDS50_REPL_DISABLED;
  643. goto send_response;
  644. } else {
  645. slapi_ch_free_string(&data_guid);
  646. ber_bvfree(data);
  647. data = NULL;
  648. }
  649. /* Stash info that this is a total update session */
  650. if (NULL != connext)
  651. {
  652. connext->repl_protocol_version = REPL_PROTOCOL_50_TOTALUPDATE;
  653. }
  654. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  655. "conn=%" NSPRIu64 " op=%d repl=\"%s\": Begin total protocol\n",
  656. connid, opid, repl_root);
  657. isInc = PR_FALSE;
  658. }
  659. else if (strcmp(protocol_oid, REPL_NSDS71_INCREMENTAL_PROTOCOL_OID) == 0)
  660. {
  661. /* Stash info that this is an incremental update session */
  662. connext->repl_protocol_version = REPL_PROTOCOL_50_INCREMENTAL;
  663. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  664. "conn=%" NSPRIu64 " op=%d repl=\"%s\": Begin 7.1 incremental protocol\n",
  665. connid, opid, repl_root);
  666. isInc = PR_TRUE;
  667. }
  668. else if (strcmp(protocol_oid, REPL_NSDS71_TOTAL_PROTOCOL_OID) == 0)
  669. {
  670. /* Stash info that this is a total update session */
  671. if (NULL != connext)
  672. {
  673. connext->repl_protocol_version = REPL_PROTOCOL_50_TOTALUPDATE;
  674. }
  675. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  676. "conn=%" NSPRIu64 " op=%d repl=\"%s\": Begin 7.1 total protocol\n",
  677. connid, opid, repl_root);
  678. isInc = PR_FALSE;
  679. }
  680. else
  681. {
  682. /* Unknown replication protocol */
  683. response = NSDS50_REPL_UNKNOWN_UPDATE_PROTOCOL;
  684. goto send_response;
  685. }
  686. /* Verify that repl_root names a valid replicated area */
  687. if ((repl_root_sdn = slapi_sdn_new_dn_byval(repl_root)) == NULL)
  688. {
  689. response = NSDS50_REPL_INTERNAL_ERROR;
  690. goto send_response;
  691. }
  692. /* see if this replica is being configured and wait for it */
  693. if (replica_is_being_configured(repl_root))
  694. {
  695. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  696. "conn=%" NSPRIu64 " op=%d replica=\"%s\": "
  697. "Replica is being configured: try again later\n",
  698. connid, opid, repl_root);
  699. response = NSDS50_REPL_REPLICA_BUSY;
  700. goto send_response;
  701. }
  702. replica_object = replica_get_replica_from_dn(repl_root_sdn);
  703. if (NULL != replica_object)
  704. {
  705. replica = object_get_data(replica_object);
  706. }
  707. if (NULL == replica)
  708. {
  709. response = NSDS50_REPL_NO_SUCH_REPLICA;
  710. goto send_response;
  711. }
  712. if (REPL_PROTOCOL_50_TOTALUPDATE == connext->repl_protocol_version)
  713. {
  714. /* If total update has been initiated against other replicas or
  715. * this replica is already being initialized, we should return
  716. * an error immediately. */
  717. if (replica_is_state_flag_set(replica,
  718. REPLICA_TOTAL_EXCL_SEND|REPLICA_TOTAL_EXCL_RECV))
  719. {
  720. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  721. "%s: total update on is initiated on the replica. Cannot execute the total update from other master.\n", repl_root);
  722. response = NSDS50_REPL_REPLICA_BUSY;
  723. goto send_response;
  724. }
  725. else
  726. {
  727. replica_set_state_flag (replica, REPLICA_TOTAL_EXCL_RECV, 0);
  728. }
  729. }
  730. /* check that this replica is not a 4.0 consumer */
  731. if (replica_is_legacy_consumer (replica))
  732. {
  733. response = NSDS50_REPL_LEGACY_CONSUMER;
  734. goto send_response;
  735. }
  736. /* Check that bind dn is authorized to supply replication updates */
  737. slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn); /* bind_dn is allocated */
  738. bind_sdn = slapi_sdn_new_dn_passin(bind_dn);
  739. if (replica_is_updatedn(replica, bind_sdn) == PR_FALSE)
  740. {
  741. response = NSDS50_REPL_PERMISSION_DENIED;
  742. goto send_response;
  743. }
  744. /* Check received CSN for clock skew */
  745. gen_obj = replica_get_csngen(replica);
  746. if (NULL != gen_obj)
  747. {
  748. gen = object_get_data(gen_obj);
  749. if (NULL != gen)
  750. {
  751. replicacsn = csn_new_by_string(replicacsnstr);
  752. if (NULL != replicacsn)
  753. {
  754. /* ONREPL - we used to manage clock skew here. However, csn generator
  755. code already does it. The csngen also manages local skew caused by
  756. system clock reset, so to keep it consistent, I removed code from here */
  757. /* update the state of the csn generator */
  758. rc = replica_update_csngen_state_ext (replica, supplier_ruv, replicacsn); /* too much skew */
  759. if (rc == CSN_LIMIT_EXCEEDED)
  760. {
  761. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  762. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  763. "Excessive clock skew from supplier RUV\n",
  764. connid, opid, repl_root);
  765. response = NSDS50_REPL_EXCESSIVE_CLOCK_SKEW;
  766. goto send_response;
  767. }
  768. else if (rc != 0)
  769. {
  770. /* Oops, problem csn or ruv format, or memory, or .... */
  771. response = NSDS50_REPL_INTERNAL_ERROR;
  772. goto send_response;
  773. }
  774. }
  775. else
  776. {
  777. /* Oops, csnstr couldn't be converted */
  778. response = NSDS50_REPL_INTERNAL_ERROR;
  779. goto send_response;
  780. }
  781. }
  782. else
  783. {
  784. /* Oops, no csn generator */
  785. response = NSDS50_REPL_INTERNAL_ERROR;
  786. goto send_response;
  787. }
  788. }
  789. else
  790. {
  791. /* Oops, no csn generator object */
  792. response = NSDS50_REPL_INTERNAL_ERROR;
  793. goto send_response;
  794. }
  795. if (check_replica_id_uniqueness(replica, supplier_ruv) != 0){
  796. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  797. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  798. "Replica has same replicaID %d as supplier\n",
  799. connid, opid, repl_root, replica_get_rid(replica));
  800. response = NSDS50_REPL_REPLICAID_ERROR;
  801. goto send_response;
  802. }
  803. /* Attempt to acquire exclusive access to the replicated area */
  804. /* Since partial URL is always the master, this locking_purl does not
  805. * help us to know the true locker when it is a hub. Change to use
  806. * the session's conn id and op id to identify the the supplier.
  807. */
  808. /* junkrc = ruv_get_first_id_and_purl(supplier_ruv, &junkrid, &locking_purl); */
  809. PR_snprintf(locking_session, sizeof(locking_session), "conn=%" NSPRIu64 " id=%d",
  810. connid, opid);
  811. locking_purl = &locking_session[0];
  812. if (replica_get_exclusive_access(replica, &isInc, connid, opid,
  813. locking_purl,
  814. &current_purl) == PR_FALSE)
  815. {
  816. locking_purl = NULL; /* no dangling pointers */
  817. response = NSDS50_REPL_REPLICA_BUSY;
  818. goto send_response;
  819. }
  820. else
  821. {
  822. locking_purl = NULL; /* no dangling pointers */
  823. /* Stick the replica object pointer in the connection extension */
  824. connext->replica_acquired = (void *)replica_object;
  825. replica_object = NULL;
  826. }
  827. /* remove this code once ticket 374 is fixed */
  828. #ifdef ENABLE_TEST_TICKET_374
  829. #include <unistd.h>
  830. if (getenv("SLAPD_TEST_TICKET_374") && (opid > 20)) {
  831. int i = 0;
  832. int max = 480 * 5;
  833. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  834. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  835. "374 - Starting sleep: connext->repl_protocol_version == %d\n",
  836. connid, opid, repl_root, connext->repl_protocol_version);
  837. while (REPL_PROTOCOL_50_INCREMENTAL == connext->repl_protocol_version && i++ < max) {
  838. usleep(200000);
  839. }
  840. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  841. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  842. "374 - Finished sleep: connext->repl_protocol_version == %d\n",
  843. connid, opid, repl_root, connext->repl_protocol_version);
  844. }
  845. #endif
  846. /* If this is incremental protocol get replica's ruv to return to the supplier */
  847. if (connext->repl_protocol_version == REPL_PROTOCOL_50_INCREMENTAL)
  848. {
  849. ruv_object = replica_get_ruv(replica);
  850. if (NULL != ruv_object)
  851. {
  852. ruv = object_get_data(ruv_object);
  853. (void)ruv_to_bervals(ruv, &ruv_bervals);
  854. object_release(ruv_object);
  855. }
  856. }
  857. /*
  858. * Save the supplier ruv in the connection extension so it can
  859. * either (a) be installed upon successful initialization (if this
  860. * is a total update session) or used to update referral information
  861. * for new replicas that show up in the supplier's RUV.
  862. */
  863. /*
  864. * the supplier_ruv may have been set before, so free it here
  865. * (in ruv_copy_and_destroy)
  866. */
  867. ruv_copy_and_destroy(&supplier_ruv, (RUV **)&connext->supplier_ruv);
  868. /* incremental update protocol */
  869. if (connext->repl_protocol_version == REPL_PROTOCOL_50_INCREMENTAL)
  870. {
  871. /* The supplier ruv may have changed, so let's update the referrals */
  872. consumer5_set_mapping_tree_state_for_replica(replica, connext->supplier_ruv);
  873. }
  874. /* total update protocol */
  875. else if (connext->repl_protocol_version == REPL_PROTOCOL_50_TOTALUPDATE)
  876. {
  877. char *mtnstate = slapi_mtn_get_state(repl_root_sdn);
  878. char **mtnreferral = slapi_mtn_get_referral(repl_root_sdn);
  879. /* richm 20041118 - we do not want to reap tombstones while there is
  880. a total update in progress, so shut it down */
  881. replica_set_tombstone_reap_stop(replica, PR_TRUE);
  882. /* richm 20010831 - set the mapping tree to the referral state *before*
  883. we invoke slapi_start_bulk_import - see bug 556992 -
  884. slapi_start_bulk_import sets the database offline, if an operation comes
  885. in while the database is offline but the mapping tree is not referring yet,
  886. the server gets confused
  887. */
  888. /* During a total update we refer *all* operations */
  889. repl_set_mtn_state_and_referrals(repl_root_sdn, STATE_REFERRAL,
  890. connext->supplier_ruv, NULL, referrals);
  891. /* LPREPL - check the return code.
  892. * But what do we do if mapping tree could not be updated ? */
  893. /* start the bulk import */
  894. slapi_pblock_set (pb, SLAPI_TARGET_SDN, repl_root_sdn);
  895. rc = slapi_start_bulk_import (pb);
  896. if (rc != LDAP_SUCCESS)
  897. {
  898. response = NSDS50_REPL_INTERNAL_ERROR;
  899. /* reset the mapping tree state to what it was before
  900. we tried to do the bulk import if mtnstate exists */
  901. if (mtnstate) {
  902. repl_set_mtn_state_and_referrals(repl_root_sdn, mtnstate,
  903. NULL, NULL, mtnreferral);
  904. slapi_ch_free_string(&mtnstate);
  905. }
  906. charray_free(mtnreferral);
  907. mtnreferral = NULL;
  908. goto send_response;
  909. }
  910. slapi_ch_free_string(&mtnstate);
  911. charray_free(mtnreferral);
  912. mtnreferral = NULL;
  913. }
  914. /* something unexpected at this point, like REPL_PROTOCOL_UNKNOWN */
  915. else
  916. {
  917. /* TEL 20120529: This condition isn't supposed to happen, but it
  918. * has been observed in the past when the consumer is under such
  919. * stress that the supplier sends additional start extops before
  920. * the consumer has finished processing an earlier one. Fixing
  921. * the underlying race should prevent this from happening in the
  922. * future at all, but just in case it is still worth testing the
  923. * requested protocol explictly and returning an error here rather
  924. * than assuming a total update was requested.
  925. * https://fedorahosted.org/389/ticket/374 */
  926. response = NSDS50_REPL_INTERNAL_ERROR;
  927. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  928. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  929. "Unexpected update protocol received: %d. "
  930. "Expected incremental or total.\n",
  931. connid, opid, repl_root, connext->repl_protocol_version);
  932. goto send_response;
  933. }
  934. response = NSDS50_REPL_REPLICA_READY;
  935. /* Set the "is replication session" flag in the connection extension */
  936. slapi_pblock_set( pb, SLAPI_CONN_IS_REPLICATION_SESSION, &one );
  937. connext->isreplicationsession = 1;
  938. /* Save away the connection */
  939. slapi_pblock_get(pb, SLAPI_CONNECTION, &connext->connection);
  940. send_response:
  941. if (connext && replica &&
  942. (REPL_PROTOCOL_50_TOTALUPDATE == connext->repl_protocol_version))
  943. {
  944. replica_set_state_flag (replica, REPLICA_TOTAL_EXCL_RECV, 1);
  945. }
  946. if (response != NSDS50_REPL_REPLICA_READY)
  947. {
  948. int resp_log_level = SLAPI_LOG_FATAL;
  949. char purlstr[1024] = {0};
  950. if (current_purl)
  951. PR_snprintf(purlstr, sizeof(purlstr), " locked by %s for %s update", current_purl,
  952. isInc ? "incremental" : "total");
  953. /* Don't log replica busy as errors - these are almost always not
  954. errors - use the replication monitoring tools to determine if
  955. a replica is not converging, then look for pathological replica
  956. busy errors by turning on the replication log level. We also
  957. don't want to log replica backoff as an error, as that response
  958. is only used when a replication session hook wants a master to
  959. go into incremental backoff mode. */
  960. if ((response == NSDS50_REPL_REPLICA_BUSY) || (response == NSDS50_REPL_BACKOFF)) {
  961. resp_log_level = SLAPI_LOG_REPL;
  962. }
  963. slapi_log_error (resp_log_level, repl_plugin_name,
  964. "conn=%" NSPRIu64 " op=%d replica=\"%s\": "
  965. "Unable to acquire replica: error: %s%s\n",
  966. connid, opid,
  967. (replica ? slapi_sdn_get_dn(replica_get_root(replica)) : "unknown"),
  968. protocol_response2string (response), purlstr);
  969. /* enable tombstone reap again since the total update failed */
  970. replica_set_tombstone_reap_stop(replica, PR_FALSE);
  971. }
  972. /* Call any registered replica session reply callback. We
  973. * want to reject the updates if the return value is non-0. */
  974. if (repl_session_plugin_call_reply_acquire_cb(replica ?
  975. slapi_sdn_get_ndn(replica_get_root(replica)) : "",
  976. ((isInc == PR_TRUE) ? 0 : 1), &data_guid, &data))
  977. {
  978. slapi_ch_free_string(&data_guid);
  979. ber_bvfree(data);
  980. data = NULL;
  981. response = NSDS50_REPL_BACKOFF;
  982. }
  983. /* Send the response */
  984. if ((resp_bere = der_alloc()) == NULL)
  985. {
  986. /* ONREPL - not sure what we suppose to do here */
  987. }
  988. ber_printf(resp_bere, "{e", response);
  989. if (NULL != ruv_bervals)
  990. {
  991. ber_printf(resp_bere, "{V}", ruv_bervals);
  992. }
  993. /* Add extra data from replication session callback if necessary */
  994. if (is90 && data_guid && data)
  995. {
  996. ber_printf(resp_bere, "sO", data_guid, data);
  997. }
  998. ber_printf(resp_bere, "}");
  999. ber_flatten(resp_bere, &resp_bval);
  1000. if (is90)
  1001. {
  1002. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, REPL_NSDS90_REPLICATION_RESPONSE_OID);
  1003. }
  1004. else
  1005. {
  1006. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, REPL_NSDS50_REPLICATION_RESPONSE_OID);
  1007. }
  1008. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, resp_bval);
  1009. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1010. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  1011. "%s: response=%d rc=%d\n",
  1012. connid, opid, repl_root,
  1013. is90 ? "StartNSDS90ReplicationRequest" :
  1014. "StartNSDS50ReplicationRequest", response, rc);
  1015. slapi_send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
  1016. return_value = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  1017. /* Free any data allocated by the replication
  1018. * session reply callback. */
  1019. slapi_ch_free_string(&data_guid);
  1020. ber_bvfree(data);
  1021. data = NULL;
  1022. slapi_ch_free_string(&current_purl);
  1023. /* protocol_oid */
  1024. /* slapi_ch_free accepts NULL pointer */
  1025. slapi_ch_free((void **)&protocol_oid);
  1026. /* repl_root */
  1027. slapi_ch_free((void **)&repl_root);
  1028. /* supplier's ruv */
  1029. if (supplier_ruv)
  1030. {
  1031. ruv_destroy (&supplier_ruv);
  1032. }
  1033. /* referrals (char **) */
  1034. slapi_ch_array_free(referrals);
  1035. /* replicacsnstr */
  1036. slapi_ch_free((void **)&replicacsnstr);
  1037. /* repl_root_sdn */
  1038. slapi_sdn_free(&repl_root_sdn);
  1039. if (NSDS50_REPL_REPLICA_READY != response)
  1040. {
  1041. /*
  1042. * Something went wrong, and we never told the other end that the
  1043. * replica had been acquired, so we'd better release it.
  1044. */
  1045. if (NULL != connext && NULL != connext->replica_acquired)
  1046. {
  1047. Object *r_obj = (Object*)connext->replica_acquired;
  1048. replica_relinquish_exclusive_access((Replica*)object_get_data (r_obj),
  1049. connid, opid);
  1050. }
  1051. /* Remove any flags that would indicate repl session in progress */
  1052. if (NULL != connext)
  1053. {
  1054. connext->repl_protocol_version = REPL_PROTOCOL_UNKNOWN;
  1055. connext->isreplicationsession = 0;
  1056. }
  1057. slapi_pblock_set( pb, SLAPI_CONN_IS_REPLICATION_SESSION, &zero );
  1058. }
  1059. /* Release reference to replica_object */
  1060. if (NULL != replica_object)
  1061. {
  1062. object_release(replica_object);
  1063. }
  1064. /* bind_sdn */
  1065. if (NULL != bind_sdn)
  1066. {
  1067. slapi_sdn_free(&bind_sdn);
  1068. }
  1069. /* Release reference to gen_obj */
  1070. if (NULL != gen_obj)
  1071. {
  1072. object_release(gen_obj);
  1073. }
  1074. /* replicacsn */
  1075. if (NULL != replicacsn)
  1076. {
  1077. csn_free(&replicacsn);
  1078. }
  1079. /* resp_bere */
  1080. if (NULL != resp_bere)
  1081. {
  1082. ber_free(resp_bere, 1);
  1083. }
  1084. /* resp_bval */
  1085. if (NULL != resp_bval)
  1086. {
  1087. ber_bvfree(resp_bval);
  1088. }
  1089. /* ruv_bervals */
  1090. if (NULL != ruv_bervals)
  1091. {
  1092. ber_bvecfree(ruv_bervals);
  1093. }
  1094. /* connext (our hold on it at least) */
  1095. if (NULL != connext)
  1096. {
  1097. /* don't free it, just let go of it */
  1098. consumer_connection_extension_relinquish_exclusive_access(conn, connid, opid, PR_FALSE);
  1099. connext = NULL;
  1100. }
  1101. return return_value;
  1102. }
  1103. /*
  1104. * This plugin entry point is called whenever an
  1105. * EndNSDS50ReplicationRequest is received.
  1106. * XXXggood this code is not finished.
  1107. */
  1108. int
  1109. multimaster_extop_EndNSDS50ReplicationRequest(Slapi_PBlock *pb)
  1110. {
  1111. int return_value = SLAPI_PLUGIN_EXTENDED_NOT_HANDLED;
  1112. char *repl_root = NULL;
  1113. Slapi_DN *repl_root_sdn = NULL;
  1114. BerElement *resp_bere = NULL;
  1115. struct berval *resp_bval = NULL;
  1116. ber_int_t response;
  1117. void *conn;
  1118. consumer_connection_extension *connext = NULL;
  1119. PRUint64 connid = 0;
  1120. int opid=-1;
  1121. /* Decode the extended operation */
  1122. if (decode_endrepl_extop(pb, &repl_root) == -1)
  1123. {
  1124. response = NSDS50_REPL_DECODING_ERROR;
  1125. }
  1126. else
  1127. {
  1128. /* First, verify that the current connection is a replication session */
  1129. /* XXXggood - do we need to wait around for any pending updates to complete?
  1130. I suppose it's possible that the end request may arrive asynchronously, before
  1131. we're really done processing all the updates.
  1132. */
  1133. /* Get a hold of the connection extension object */
  1134. slapi_pblock_get(pb, SLAPI_CONNECTION, &conn);
  1135. slapi_pblock_get (pb, SLAPI_OPERATION_ID, &opid);
  1136. if (opid) slapi_pblock_get (pb, SLAPI_CONN_ID, &connid);
  1137. /* TEL 20120531: unlike the replica, exclusive access to the connext should
  1138. * have been dropped at the end of the 'start' op. the only reason we couldn't
  1139. * get access to it would be if some other start or end op currently has it.
  1140. * if that is the case, the result of our getting it would be unpredictable anyway.
  1141. */
  1142. connext = consumer_connection_extension_acquire_exclusive_access(conn, connid, opid);
  1143. if (NULL != connext && NULL != connext->replica_acquired)
  1144. {
  1145. int zero= 0;
  1146. Replica *r = (Replica*)object_get_data ((Object*)connext->replica_acquired);
  1147. /* if this is total protocol we need to install suppliers ruv for the replica */
  1148. if (connext->repl_protocol_version == REPL_PROTOCOL_50_TOTALUPDATE)
  1149. {
  1150. /* We no longer need to refer all operations...
  1151. * and update the referrals on the mapping tree node
  1152. */
  1153. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  1154. /* LPREPL - First we clear the total in progress flag
  1155. Like this we know it's a normal termination of import. This is required by
  1156. the replication function that responds to backend state change.
  1157. If the flag is not clear, the callback knows that replication should not be
  1158. enabled again */
  1159. replica_set_state_flag(r, REPLICA_TOTAL_IN_PROGRESS, PR_TRUE /* clear flag */);
  1160. /* slapi_pblock_set (pb, SLAPI_TARGET_DN, repl_root); */
  1161. /* Verify that repl_root names a valid replicated area */
  1162. if ((repl_root_sdn = slapi_sdn_new_dn_byref(repl_root)) == NULL)
  1163. {
  1164. response = NSDS50_REPL_INTERNAL_ERROR;
  1165. goto send_response;
  1166. }
  1167. slapi_pblock_set (pb, SLAPI_TARGET_SDN, repl_root_sdn);
  1168. slapi_stop_bulk_import (pb);
  1169. /* ONREPL - this is a bit of a hack. Once bulk import is finished,
  1170. the replication function that responds to backend state change
  1171. will be called. That function normally do all ruv and changelog
  1172. processing. However, in the case of replica initalization, it
  1173. will not do the right thing because supplier does not send its
  1174. ruv tombstone to the consumer. So that's why we need to do the
  1175. second processing here.
  1176. The supplier does not send its RUV entry because it could be
  1177. more up to date then the data send to the consumer.
  1178. The best solution I think, would be to "fake" on the supplier
  1179. an entry that corresponds to the ruv sent to the consumer and then
  1180. send it as part of the data */
  1181. if (cl5GetState () == CL5_STATE_OPEN)
  1182. {
  1183. cl5DeleteDBSync (connext->replica_acquired);
  1184. }
  1185. replica_set_ruv (r, connext->supplier_ruv);
  1186. connext->supplier_ruv = NULL;
  1187. /* if changelog is enabled, we need to log a dummy change for the
  1188. smallest csn in the new ruv, so that this replica ca supply
  1189. other servers.
  1190. */
  1191. if (cl5GetState () == CL5_STATE_OPEN)
  1192. {
  1193. replica_log_ruv_elements (r);
  1194. }
  1195. /* ONREPL code that dealt with new RUV, etc was moved into the code
  1196. that enables replication when a backend comes back online. This
  1197. code is called once the bulk import is finished */
  1198. /* allow reaping again */
  1199. replica_set_tombstone_reap_stop(r, PR_FALSE);
  1200. }
  1201. else if (connext->repl_protocol_version == REPL_PROTOCOL_50_INCREMENTAL)
  1202. {
  1203. /* The ruv from the supplier may have changed. Report the change on the
  1204. consumer side */
  1205. replica_update_ruv_consumer(r, connext->supplier_ruv);
  1206. }
  1207. /* Relinquish control of the replica */
  1208. replica_relinquish_exclusive_access(r, connid, opid);
  1209. object_release ((Object*)connext->replica_acquired);
  1210. connext->replica_acquired = NULL;
  1211. connext->isreplicationsession= 0;
  1212. slapi_pblock_set( pb, SLAPI_CONN_IS_REPLICATION_SESSION, &zero );
  1213. response = NSDS50_REPL_REPLICA_RELEASE_SUCCEEDED;
  1214. /* Outbound replication agreements need to all be restarted now */
  1215. /* XXXGGOOD RESTART REEPL AGREEMENTS */
  1216. } else {
  1217. /* Unless bail out, we return uninitialized response */
  1218. goto free_and_return;
  1219. }
  1220. }
  1221. send_response:
  1222. /* Send the response code */
  1223. if ((resp_bere = der_alloc()) == NULL)
  1224. {
  1225. goto free_and_return;
  1226. }
  1227. ber_printf(resp_bere, "{e}", response);
  1228. ber_flatten(resp_bere, &resp_bval);
  1229. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, REPL_NSDS50_REPLICATION_RESPONSE_OID);
  1230. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, resp_bval);
  1231. slapi_send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
  1232. return_value = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  1233. free_and_return:
  1234. /* repl_root */
  1235. slapi_ch_free((void **)&repl_root);
  1236. slapi_sdn_free(&repl_root_sdn);
  1237. /* BerElement */
  1238. if (NULL != resp_bere)
  1239. {
  1240. ber_free(resp_bere, 1);
  1241. }
  1242. /* response */
  1243. if (NULL != resp_bval)
  1244. {
  1245. ber_bvfree(resp_bval);
  1246. }
  1247. /* connext (our hold on it at least) */
  1248. if (NULL != connext)
  1249. {
  1250. /* don't free it, just let go of it */
  1251. consumer_connection_extension_relinquish_exclusive_access(conn, connid, opid, PR_FALSE);
  1252. connext = NULL;
  1253. }
  1254. return return_value;
  1255. }
  1256. /*
  1257. * Return the mtnode extension of the dn
  1258. */
  1259. static multimaster_mtnode_extension *
  1260. replica_config_get_mtnode_by_dn(const char *dn)
  1261. {
  1262. Slapi_DN *sdn;
  1263. mapping_tree_node *mtnode;
  1264. multimaster_mtnode_extension *ext = NULL;
  1265. sdn = slapi_sdn_new_dn_byval(dn);
  1266. mtnode = slapi_get_mapping_tree_node_by_dn (sdn);
  1267. if (mtnode) {
  1268. /* check if the replica object already exists in the subtree */
  1269. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  1270. }
  1271. slapi_sdn_free (&sdn);
  1272. return ext;
  1273. }
  1274. /*
  1275. * Decode the ber element passed to us by the cleanAllRUV task
  1276. */
  1277. int
  1278. decode_cleanruv_payload(struct berval *extop_value, char **payload)
  1279. {
  1280. BerElement *tmp_bere = NULL;
  1281. int rc = 0;
  1282. if (!BV_HAS_DATA(extop_value)) {
  1283. rc = -1;
  1284. goto free_and_return;
  1285. }
  1286. if ((tmp_bere = ber_init(extop_value)) == NULL){
  1287. rc = -1;
  1288. goto free_and_return;
  1289. }
  1290. if (ber_scanf(tmp_bere, "{") == LBER_ERROR){
  1291. rc = -1;
  1292. goto free_and_return;
  1293. }
  1294. if (ber_get_stringa(tmp_bere, payload) == LBER_DEFAULT){
  1295. rc = -1;
  1296. goto free_and_return;
  1297. }
  1298. if (ber_scanf(tmp_bere, "}") == LBER_ERROR){
  1299. rc = -1;
  1300. goto free_and_return;
  1301. }
  1302. free_and_return:
  1303. if (-1 == rc){
  1304. slapi_ch_free_string(payload);
  1305. }
  1306. if (NULL != tmp_bere){
  1307. ber_free(tmp_bere, 1);
  1308. tmp_bere = NULL;
  1309. }
  1310. return rc;
  1311. }
  1312. int
  1313. multimaster_extop_abort_cleanruv(Slapi_PBlock *pb)
  1314. {
  1315. multimaster_mtnode_extension *mtnode_ext = NULL;
  1316. int release_it = 0;
  1317. PRThread *thread = NULL;
  1318. cleanruv_data *data;
  1319. Replica *r;
  1320. ReplicaId rid;
  1321. struct berval *extop_payload = NULL;
  1322. char *extop_oid;
  1323. char *repl_root;
  1324. char *payload = NULL;
  1325. char *certify_all;
  1326. char *iter = NULL;
  1327. int rc = LDAP_SUCCESS;
  1328. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &extop_oid);
  1329. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &extop_payload);
  1330. if (NULL == extop_oid || strcmp(extop_oid, REPL_ABORT_CLEANRUV_OID) != 0 ||
  1331. NULL == extop_payload || NULL == extop_payload->bv_val){
  1332. /* something is wrong, error out */
  1333. return LDAP_OPERATIONS_ERROR;
  1334. }
  1335. /*
  1336. * Decode the payload, and grab our settings
  1337. */
  1338. if(decode_cleanruv_payload(extop_payload, &payload)){
  1339. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to decode payload. Aborting ext op\n");
  1340. return LDAP_OPERATIONS_ERROR;
  1341. }
  1342. rid = atoi(ldap_utf8strtok_r(payload, ":", &iter));
  1343. repl_root = ldap_utf8strtok_r(iter, ":", &iter);
  1344. certify_all = ldap_utf8strtok_r(iter, ":", &iter);
  1345. if(!is_cleaned_rid(rid) || !is_pre_cleaned_rid(rid) || is_task_aborted(rid)){
  1346. /* This replica has already been aborted, or was never cleaned, or already finished cleaning */
  1347. goto out;
  1348. } else {
  1349. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: aborting cleanallruv task for rid(%d)\n", rid);
  1350. }
  1351. /*
  1352. * Get the node, so we can get the replica and its agreements
  1353. */
  1354. if((mtnode_ext = replica_config_get_mtnode_by_dn(repl_root)) == NULL){
  1355. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to get replication node "
  1356. "from (%s), aborting operation\n", repl_root);
  1357. rc = LDAP_OPERATIONS_ERROR;
  1358. goto out;
  1359. }
  1360. if (mtnode_ext->replica){
  1361. object_acquire (mtnode_ext->replica);
  1362. release_it = 1;
  1363. } else {
  1364. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: replica is missing from (%s), "
  1365. "aborting operation\n",repl_root);
  1366. rc = LDAP_OPERATIONS_ERROR;
  1367. goto out;
  1368. }
  1369. r = (Replica*)object_get_data (mtnode_ext->replica);
  1370. if(r == NULL){
  1371. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: replica is NULL, aborting task\n");
  1372. rc = LDAP_OPERATIONS_ERROR;
  1373. goto out;
  1374. }
  1375. /*
  1376. * Prepare the abort data
  1377. */
  1378. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  1379. if (data == NULL) {
  1380. slapi_log_error( SLAPI_LOG_REPL, repl_plugin_name, "Abort CleanAllRUV Task: failed to allocate "
  1381. "abort_cleanruv_data. Aborting task.\n");
  1382. rc = LDAP_OPERATIONS_ERROR;
  1383. goto out;
  1384. }
  1385. data->repl_obj = mtnode_ext->replica; /* released in replica_abort_task_thread() */
  1386. release_it = 0; /* thread owns it now */
  1387. data->replica = r;
  1388. data->task = NULL;
  1389. data->payload = slapi_ch_bvdup(extop_payload);
  1390. data->rid = rid;
  1391. data->repl_root = slapi_ch_strdup(repl_root);
  1392. data->certify = slapi_ch_strdup(certify_all);
  1393. /*
  1394. * Set the aborted rid and stop the cleaning
  1395. */
  1396. add_aborted_rid(rid, r, repl_root);
  1397. stop_ruv_cleaning();
  1398. /*
  1399. * Send out the extended ops to the replicas
  1400. */
  1401. thread = PR_CreateThread(PR_USER_THREAD, replica_abort_task_thread,
  1402. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1403. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1404. if (thread == NULL) {
  1405. slapi_log_error( SLAPI_LOG_REPL, repl_plugin_name, "Abort CleanAllRUV Task: unable to create abort "
  1406. "thread. Aborting task.\n");
  1407. release_it = 1; /* have to release mtnode_ext->replica now */
  1408. slapi_ch_free_string(&data->repl_root);
  1409. slapi_ch_free_string(&data->certify);
  1410. ber_bvfree(data->payload);
  1411. slapi_ch_free((void **)&data);
  1412. rc = LDAP_OPERATIONS_ERROR;
  1413. }
  1414. out:
  1415. if (release_it && mtnode_ext && mtnode_ext->replica) {
  1416. object_release(mtnode_ext->replica);
  1417. }
  1418. slapi_ch_free_string(&payload);
  1419. return rc;
  1420. }
  1421. /*
  1422. * Process the REPL_CLEANRUV_OID extended operation.
  1423. *
  1424. * The payload consists of the replica ID, repl root dn, and the maxcsn. Since this is
  1425. * basically a replication operation, it could of originated here and bounced
  1426. * back from another master. So check the rid against the "cleaned_rid". If
  1427. * it's a match, then we were already here, and we can just return success.
  1428. *
  1429. * Otherwise, we the set the cleaned_rid from the payload, fire off extended ops
  1430. * to all the replica agreements on this replica. Then perform the actual
  1431. * cleanruv_task on this replica.
  1432. */
  1433. int
  1434. multimaster_extop_cleanruv(Slapi_PBlock *pb)
  1435. {
  1436. multimaster_mtnode_extension *mtnode_ext = NULL;
  1437. PRThread *thread = NULL;
  1438. Replica *r = NULL;
  1439. cleanruv_data *data = NULL;
  1440. CSN *maxcsn = NULL;
  1441. struct berval *extop_payload;
  1442. struct berval *resp_bval = NULL;
  1443. BerElement *resp_bere = NULL;
  1444. char *payload = NULL;
  1445. char *csnstr = NULL;
  1446. char *force = NULL;
  1447. char *extop_oid;
  1448. char *repl_root;
  1449. char *iter = NULL;
  1450. int release_it = 0;
  1451. int rid = 0;
  1452. int rc = LDAP_OPERATIONS_ERROR;
  1453. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &extop_oid);
  1454. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &extop_payload);
  1455. if (NULL == extop_oid || strcmp(extop_oid, REPL_CLEANRUV_OID) != 0 ||
  1456. NULL == extop_payload || NULL == extop_payload->bv_val){
  1457. /* something is wrong, error out */
  1458. goto free_and_return;
  1459. }
  1460. /*
  1461. * Decode the payload
  1462. */
  1463. if(decode_cleanruv_payload(extop_payload, &payload)){
  1464. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: failed to decode payload. Aborting ext op\n");
  1465. goto free_and_return;
  1466. }
  1467. rid = atoi(ldap_utf8strtok_r(payload, ":", &iter));
  1468. repl_root = ldap_utf8strtok_r(iter, ":", &iter);
  1469. csnstr = ldap_utf8strtok_r(iter, ":", &iter);
  1470. force = ldap_utf8strtok_r(iter, ":", &iter);
  1471. if(force == NULL){
  1472. force = "no";
  1473. }
  1474. maxcsn = csn_new();
  1475. csn_init_by_string(maxcsn, csnstr);
  1476. /*
  1477. * If we already cleaned this server, just return success
  1478. */
  1479. if(is_cleaned_rid(rid) || is_pre_cleaned_rid(rid) || is_task_aborted(rid)){
  1480. csn_free(&maxcsn);
  1481. rc = LDAP_SUCCESS;
  1482. goto free_and_return;
  1483. }
  1484. /*
  1485. * Get the node, so we can get the replica and its agreements
  1486. */
  1487. if((mtnode_ext = replica_config_get_mtnode_by_dn(repl_root)) == NULL){
  1488. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: failed to get replication node "
  1489. "from (%s), aborting operation\n", repl_root);
  1490. goto free_and_return;
  1491. }
  1492. if (mtnode_ext->replica){
  1493. object_acquire (mtnode_ext->replica);
  1494. release_it = 1;
  1495. } else {
  1496. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: replica is missing from (%s), "
  1497. "aborting operation\n",repl_root);
  1498. goto free_and_return;
  1499. }
  1500. r = (Replica*)object_get_data (mtnode_ext->replica);
  1501. if(r == NULL){
  1502. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: replica is NULL, aborting task\n");
  1503. goto free_and_return;
  1504. }
  1505. if(replica_get_type(r) != REPLICA_TYPE_READONLY){
  1506. /*
  1507. * Launch the cleanruv monitoring thread. Once all the replicas are cleaned it will release the rid
  1508. *
  1509. * This will also release mtnode_ext->replica
  1510. */
  1511. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: launching cleanAllRUV thread...\n");
  1512. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  1513. if (data == NULL) {
  1514. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: failed to allocate "
  1515. "cleanruv_Data\n");
  1516. goto free_and_return;
  1517. }
  1518. data->repl_obj = mtnode_ext->replica;
  1519. data->replica = r;
  1520. data->rid = rid;
  1521. data->task = NULL;
  1522. data->maxcsn = maxcsn;
  1523. data->payload = slapi_ch_bvdup(extop_payload);
  1524. data->force = slapi_ch_strdup(force);
  1525. data->repl_root = slapi_ch_strdup(repl_root);
  1526. thread = PR_CreateThread(PR_USER_THREAD, replica_cleanallruv_thread_ext,
  1527. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1528. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1529. if (thread == NULL) {
  1530. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: unable to create cleanAllRUV "
  1531. "monitoring thread. Aborting task.\n");
  1532. ber_bvfree(data->payload);
  1533. data->payload = NULL;
  1534. slapi_ch_free_string(&data->force);
  1535. slapi_ch_free_string(&data->repl_root);
  1536. slapi_ch_free((void **)&data);
  1537. } else {
  1538. release_it = 0; /* thread will release data->repl_obj == mtnode_ext->replica */
  1539. maxcsn = NULL; /* thread owns it now */
  1540. rc = LDAP_SUCCESS;
  1541. }
  1542. } else { /* this is a read-only consumer */
  1543. /*
  1544. * wait for the maxcsn to be covered
  1545. */
  1546. Object *ruv_obj;
  1547. const RUV *ruv;
  1548. ruv_obj = replica_get_ruv(r);
  1549. ruv = object_get_data (ruv_obj);
  1550. while(!is_task_aborted(rid) && !slapi_is_shutting_down()){
  1551. if(!ruv_contains_replica(ruv, rid)){
  1552. /* we've already been cleaned */
  1553. break;
  1554. }
  1555. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: checking if we're caught up...\n");
  1556. if(ruv_covers_csn_cleanallruv(ruv,maxcsn) || csn_get_replicaid(maxcsn) == 0|| strcmp(force,"yes") == 0){
  1557. /* We are caught up */
  1558. break;
  1559. } else {
  1560. char csnstr[CSN_STRSIZE];
  1561. csn_as_string(maxcsn, PR_FALSE, csnstr);
  1562. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: not ruv caught up maxcsn(%s)\n", csnstr);
  1563. }
  1564. DS_Sleep(PR_SecondsToInterval(5));
  1565. }
  1566. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: we're caught up...\n");
  1567. /*
  1568. * Set cleaned rid in memory only - does not survive a server restart
  1569. */
  1570. set_cleaned_rid(rid);
  1571. /*
  1572. * Clean the ruv
  1573. */
  1574. replica_execute_cleanruv_task_ext(mtnode_ext->replica, rid);
  1575. /* free everything */
  1576. object_release(ruv_obj);
  1577. /*
  1578. * This read-only replica has no easy way to tell when it's safe to release the rid.
  1579. * So we won't release it, not until a server restart.
  1580. */
  1581. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: You must restart the server if you want to reuse rid(%d).\n", rid);
  1582. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: Successfully cleaned rid(%d).\n", rid);
  1583. rc = LDAP_SUCCESS;
  1584. }
  1585. free_and_return:
  1586. if(release_it && mtnode_ext && mtnode_ext->replica) {
  1587. object_release (mtnode_ext->replica);
  1588. }
  1589. csn_free(&maxcsn);
  1590. slapi_ch_free_string(&payload);
  1591. /*
  1592. * Craft a message so we know this replica supports the task
  1593. */
  1594. if ((resp_bere = der_alloc())){
  1595. ber_printf(resp_bere, "{s}", CLEANRUV_ACCEPTED);
  1596. ber_flatten(resp_bere, &resp_bval);
  1597. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, resp_bval);
  1598. slapi_send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
  1599. /* resp_bere */
  1600. if (NULL != resp_bere)
  1601. {
  1602. ber_free(resp_bere, 1);
  1603. }
  1604. /* resp_bval */
  1605. if (NULL != resp_bval)
  1606. {
  1607. ber_bvfree(resp_bval);
  1608. }
  1609. /* tell extendop code that we have already sent the result */
  1610. rc = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  1611. } else {
  1612. rc = LDAP_OPERATIONS_ERROR;
  1613. }
  1614. return rc;
  1615. }
  1616. /*
  1617. * Get the max csn for the designated repl area
  1618. */
  1619. int
  1620. multimaster_extop_cleanruv_get_maxcsn(Slapi_PBlock *pb)
  1621. {
  1622. struct berval *resp_bval = NULL;
  1623. struct berval *extop_payload;
  1624. BerElement *resp_bere = NULL;
  1625. char *extop_oid = NULL;
  1626. char *base_dn = NULL;
  1627. char *payload = NULL;
  1628. char *maxcsn = NULL;
  1629. char *iter = NULL;
  1630. int rid = 0;
  1631. int rc = LDAP_OPERATIONS_ERROR;
  1632. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &extop_oid);
  1633. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &extop_payload);
  1634. if (NULL == extop_oid || strcmp(extop_oid, REPL_CLEANRUV_GET_MAXCSN_OID) != 0 ||
  1635. NULL == extop_payload || NULL == extop_payload->bv_val){
  1636. /* something is wrong, error out */
  1637. goto free_and_return;
  1638. }
  1639. /*
  1640. * Decode the payload
  1641. */
  1642. if(decode_cleanruv_payload(extop_payload, &payload)){
  1643. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Get MaxCSN Task: failed to decode payload. Aborting ext op\n");
  1644. goto free_and_return;
  1645. }
  1646. rid = atoi(ldap_utf8strtok_r(payload, ":", &iter));
  1647. base_dn = ldap_utf8strtok_r(iter, ":", &iter);
  1648. maxcsn = replica_cleanallruv_get_local_maxcsn(rid, base_dn);
  1649. if(maxcsn == NULL){
  1650. maxcsn = slapi_ch_strdup(CLEANRUV_NO_MAXCSN);
  1651. }
  1652. /*
  1653. * Send the extended op response
  1654. */
  1655. if ((resp_bere = der_alloc())){
  1656. ber_printf(resp_bere, "{s}", maxcsn);
  1657. ber_flatten(resp_bere, &resp_bval);
  1658. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, resp_bval);
  1659. slapi_send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
  1660. /* resp_bere */
  1661. if (NULL != resp_bere)
  1662. {
  1663. ber_free(resp_bere, 1);
  1664. }
  1665. /* resp_bval */
  1666. if (NULL != resp_bval)
  1667. {
  1668. ber_bvfree(resp_bval);
  1669. }
  1670. /* tell extendop code that we have already sent the result */
  1671. rc = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  1672. } else {
  1673. rc = LDAP_OPERATIONS_ERROR;
  1674. }
  1675. free_and_return:
  1676. slapi_ch_free_string(&payload);
  1677. slapi_ch_free_string(&maxcsn);
  1678. return rc;
  1679. }
  1680. /*
  1681. * Search cn=config for the cleanallruv attributes (clean & abort)
  1682. */
  1683. int
  1684. multimaster_extop_cleanruv_check_status(Slapi_PBlock *pb)
  1685. {
  1686. Slapi_PBlock *search_pb = NULL;
  1687. Slapi_Entry **entries = NULL;
  1688. struct berval *resp_bval = NULL;
  1689. struct berval *extop_payload;
  1690. BerElement *resp_bere = NULL;
  1691. char *response = NULL;
  1692. char *filter = NULL;
  1693. char *extop_oid;
  1694. int res = 0;
  1695. int rc = LDAP_OPERATIONS_ERROR;
  1696. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &extop_oid);
  1697. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &extop_payload);
  1698. if (NULL == extop_oid || strcmp(extop_oid, REPL_CLEANRUV_CHECK_STATUS_OID) != 0 ||
  1699. NULL == extop_payload || NULL == extop_payload->bv_val){
  1700. /* something is wrong, error out */
  1701. goto free_and_return;
  1702. }
  1703. /*
  1704. * Decode the payload - which should just be a filter
  1705. */
  1706. if(decode_cleanruv_payload(extop_payload, &filter)){
  1707. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Check Status Task: failed to decode payload. Aborting ext op\n");
  1708. goto free_and_return;
  1709. }
  1710. search_pb = slapi_pblock_new();
  1711. slapi_search_internal_set_pb(search_pb, "cn=config", LDAP_SCOPE_SUBTREE,
  1712. filter, NULL, 0, NULL, NULL, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  1713. slapi_search_internal_pb (search_pb);
  1714. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  1715. if ( LDAP_SUCCESS == res ) {
  1716. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1717. if (NULL == entries || entries[0] == NULL) {
  1718. /* cleaning task has finished, send repsonse */
  1719. response = CLEANRUV_FINISHED;
  1720. } else {
  1721. response = CLEANRUV_CLEANING;
  1722. }
  1723. /*
  1724. * Send the extended op response
  1725. */
  1726. if ((resp_bere = der_alloc())){
  1727. ber_printf(resp_bere, "{s}", response);
  1728. ber_flatten(resp_bere, &resp_bval);
  1729. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, resp_bval);
  1730. slapi_send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
  1731. /* resp_bere */
  1732. if (NULL != resp_bere)
  1733. {
  1734. ber_free(resp_bere, 1);
  1735. }
  1736. /* resp_bval */
  1737. if (NULL != resp_bval)
  1738. {
  1739. ber_bvfree(resp_bval);
  1740. }
  1741. /* tell extendop code that we have already sent the result */
  1742. rc = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  1743. }
  1744. }
  1745. free_and_return:
  1746. slapi_free_search_results_internal(search_pb);
  1747. slapi_pblock_destroy(search_pb);
  1748. slapi_ch_free_string(&filter);
  1749. return rc;
  1750. }
  1751. /*
  1752. * This plugin entry point is a noop entry
  1753. * point. It's used when registering extops that
  1754. * are only used as responses. We'll never receive
  1755. * one of those, unsolicited, but we still want to
  1756. * register them so they appear in the
  1757. * supportedextension attribute in the root DSE.
  1758. */
  1759. int
  1760. extop_noop(Slapi_PBlock *pb)
  1761. {
  1762. return SLAPI_PLUGIN_EXTENDED_NOT_HANDLED;
  1763. }
  1764. static int
  1765. check_replica_id_uniqueness(Replica *replica, RUV *supplier_ruv)
  1766. {
  1767. ReplicaId local_rid = replica_get_rid(replica);
  1768. ReplicaId sup_rid = 0;
  1769. char *sup_purl = NULL;
  1770. if (ruv_get_first_id_and_purl(supplier_ruv, &sup_rid, &sup_purl) == RUV_SUCCESS) {
  1771. /* ReplicaID Uniqueness is checked only on Masters */
  1772. if ((replica_get_type(replica) == REPLICA_TYPE_UPDATABLE) &&
  1773. (sup_rid == local_rid)) {
  1774. return 1;
  1775. }
  1776. }
  1777. return 0;
  1778. }