repl_extop.c 55 KB

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