1
0

repl_extop.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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. /*
  47. * repl_extop.c - there are two types of functions in this file:
  48. * - Code that implements an extended operation plugin.
  49. * The replication DLL arranges for this code to
  50. * be called when a StartNSDS50ReplicationRequest
  51. * or an EndNSDS50ReplicationRequest extended operation
  52. * is received.
  53. * - Code that sends extended operations on an already-
  54. * established client connection.
  55. *
  56. * The requestValue portion of the StartNSDS50ReplicationRequest
  57. * looks like this:
  58. *
  59. * requestValue ::= SEQUENCE {
  60. * replProtocolOID LDAPOID,
  61. * replicatedTree LDAPDN,
  62. supplierRUV OCTET STRING
  63. * referralURLs SET of LDAPURL OPTIONAL
  64. * csn OCTET STRING OPTIONAL
  65. * }
  66. *
  67. */
  68. static int check_replica_id_uniqueness(Replica *replica, RUV *supplier_ruv);
  69. static int
  70. encode_ruv (BerElement *ber, const RUV *ruv)
  71. {
  72. int rc = LDAP_SUCCESS;
  73. struct berval **bvals = NULL;
  74. PR_ASSERT (ber);
  75. PR_ASSERT (ruv);
  76. if (ruv_to_bervals(ruv, &bvals) != 0)
  77. {
  78. rc = LDAP_OPERATIONS_ERROR;
  79. goto done;
  80. }
  81. if (ber_printf(ber, "[V]", bvals) == -1)
  82. {
  83. rc = LDAP_ENCODING_ERROR;
  84. goto done;
  85. }
  86. rc = LDAP_SUCCESS;
  87. done:
  88. if (bvals)
  89. ber_bvecfree (bvals);
  90. return rc;
  91. }
  92. /* The data_guid and data parameters should only be set if we
  93. * are talking with a 9.0 replica. */
  94. static struct berval *
  95. create_ReplicationExtopPayload(const char *protocol_oid,
  96. const char *repl_root, char **extra_referrals, CSN *csn,
  97. int send_end, const char *data_guid, const struct berval *data)
  98. {
  99. struct berval *req_data = NULL;
  100. BerElement *tmp_bere = NULL;
  101. int rc = 0;
  102. Object *repl_obj = NULL, *ruv_obj = NULL;
  103. Replica *repl;
  104. RUV *ruv;
  105. Slapi_DN *sdn = NULL;
  106. PR_ASSERT(protocol_oid != NULL || send_end);
  107. PR_ASSERT(repl_root != NULL);
  108. /* Create the request data */
  109. if ((tmp_bere = der_alloc()) == NULL)
  110. {
  111. rc = LDAP_ENCODING_ERROR;
  112. goto loser;
  113. }
  114. if (!send_end)
  115. {
  116. if (ber_printf(tmp_bere, "{ss", protocol_oid, repl_root) == -1)
  117. {
  118. rc = LDAP_ENCODING_ERROR;
  119. goto loser;
  120. }
  121. }
  122. else
  123. {
  124. if (ber_printf(tmp_bere, "{s", repl_root) == -1)
  125. {
  126. rc = LDAP_ENCODING_ERROR;
  127. goto loser;
  128. }
  129. }
  130. sdn = slapi_sdn_new_dn_byref(repl_root);
  131. repl_obj = replica_get_replica_from_dn (sdn);
  132. if (repl_obj == NULL)
  133. {
  134. rc = LDAP_OPERATIONS_ERROR;
  135. goto loser;
  136. }
  137. repl = (Replica*)object_get_data (repl_obj);
  138. PR_ASSERT (repl);
  139. ruv_obj = replica_get_ruv (repl);
  140. if (ruv_obj == NULL)
  141. {
  142. rc = LDAP_OPERATIONS_ERROR;
  143. goto loser;
  144. }
  145. ruv = object_get_data(ruv_obj);
  146. PR_ASSERT(ruv);
  147. /* send supplier's ruv so that consumer can build its own referrals.
  148. In case of total protocol, it is also used as consumer's ruv once
  149. protocol successfully completes */
  150. /* We need to encode and send each time the local ruv in case we have changed it */
  151. rc = encode_ruv (tmp_bere, ruv);
  152. if (rc != 0)
  153. {
  154. goto loser;
  155. }
  156. if (!send_end)
  157. {
  158. char s[CSN_STRSIZE];
  159. ReplicaId rid;
  160. char *local_replica_referral[2] = {0};
  161. char **referrals_to_send = NULL;
  162. /* Add the referral URL(s), if present */
  163. rid = replica_get_rid(repl);
  164. if (!ruv_contains_replica(ruv, rid))
  165. {
  166. /*
  167. * In the event that there is no RUV component for this replica (e.g.
  168. * if the database was just loaded from LDIF and no local CSNs have been
  169. * generated), then we need to explicitly add this server to the list
  170. * of referrals, since it wouldn't have been sent with the RUV.
  171. */
  172. local_replica_referral[0] = (char *)multimaster_get_local_purl(); /* XXXggood had to cast away const */
  173. }
  174. charray_merge(&referrals_to_send, extra_referrals, 0);
  175. charray_merge(&referrals_to_send, local_replica_referral, 0);
  176. if (NULL != referrals_to_send)
  177. {
  178. if (ber_printf(tmp_bere, "[v]", referrals_to_send) == -1)
  179. {
  180. rc = LDAP_ENCODING_ERROR;
  181. goto loser;
  182. }
  183. slapi_ch_free((void **)&referrals_to_send);
  184. }
  185. /* Add the CSN */
  186. PR_ASSERT(NULL != csn);
  187. if (ber_printf(tmp_bere, "s", csn_as_string(csn,PR_FALSE,s)) == -1)
  188. {
  189. rc = LDAP_ENCODING_ERROR;
  190. goto loser;
  191. }
  192. }
  193. /* If we have data to send to a 9.0 style replica, set it here. */
  194. if (data_guid && data) {
  195. if (ber_printf(tmp_bere, "sO", data_guid, data) == -1)
  196. {
  197. rc = LDAP_ENCODING_ERROR;
  198. goto loser;
  199. }
  200. }
  201. if (ber_printf(tmp_bere, "}") == -1)
  202. {
  203. rc = LDAP_ENCODING_ERROR;
  204. goto loser;
  205. }
  206. if (ber_flatten(tmp_bere, &req_data) == -1)
  207. {
  208. rc = LDAP_LOCAL_ERROR;
  209. goto loser;
  210. }
  211. /* Success */
  212. goto done;
  213. loser:
  214. /* Free stuff we allocated */
  215. if (NULL != req_data)
  216. {
  217. ber_bvfree(req_data); req_data = NULL;
  218. }
  219. done:
  220. if (NULL != tmp_bere)
  221. {
  222. ber_free(tmp_bere, 1); tmp_bere = NULL;
  223. }
  224. if (NULL != sdn)
  225. {
  226. slapi_sdn_free (&sdn); /* Put on stack instead of allocating? */
  227. }
  228. if (NULL != repl_obj)
  229. {
  230. object_release (repl_obj);
  231. }
  232. if (NULL != ruv_obj)
  233. {
  234. object_release (ruv_obj);
  235. }
  236. return req_data;
  237. }
  238. struct berval *
  239. NSDS50StartReplicationRequest_new(const char *protocol_oid,
  240. const char *repl_root, char **extra_referrals, CSN *csn)
  241. {
  242. return(create_ReplicationExtopPayload(protocol_oid,
  243. repl_root, extra_referrals, csn, 0, 0, 0));
  244. }
  245. struct berval *
  246. NSDS90StartReplicationRequest_new(const char *protocol_oid,
  247. const char *repl_root, char **extra_referrals, CSN *csn,
  248. const char *data_guid, const struct berval *data)
  249. {
  250. return(create_ReplicationExtopPayload(protocol_oid,
  251. repl_root, extra_referrals, csn, 0, data_guid, data));
  252. }
  253. struct berval *
  254. NSDS50EndReplicationRequest_new(char *repl_root)
  255. {
  256. return(create_ReplicationExtopPayload(NULL, repl_root, NULL, NULL, 1, 0, 0));
  257. }
  258. static int
  259. decode_ruv (BerElement *ber, RUV **ruv)
  260. {
  261. int rc = -1;
  262. struct berval **bvals = NULL;
  263. PR_ASSERT (ber && ruv);
  264. if (ber_scanf(ber, "[V]", &bvals) == LBER_DEFAULT)
  265. {
  266. goto done;
  267. }
  268. if (ruv_init_from_bervals(bvals, ruv) != 0)
  269. {
  270. goto done;
  271. }
  272. rc = 0;
  273. done:
  274. if (bvals)
  275. ber_bvecfree (bvals);
  276. return rc;
  277. }
  278. /*
  279. * Decode an NSDS50 or NSDS90 Start Replication Request extended
  280. * operation. Returns 0 on success, -1 on decoding error.
  281. * The caller is responsible for freeing protocol_oid,
  282. * repl_root, referrals, csn, data_guid, and data.
  283. */
  284. static int
  285. decode_startrepl_extop(Slapi_PBlock *pb, char **protocol_oid, char **repl_root,
  286. RUV **supplier_ruv, char ***extra_referrals, char **csnstr,
  287. char **data_guid, struct berval **data, int *is90)
  288. {
  289. char *extop_oid = NULL;
  290. struct berval *extop_value = NULL;
  291. BerElement *tmp_bere = NULL;
  292. ber_len_t len;
  293. int rc = 0;
  294. PR_ASSERT (pb && protocol_oid && repl_root && supplier_ruv && extra_referrals && csnstr && data_guid && data);
  295. *protocol_oid = NULL;
  296. *repl_root = NULL;
  297. *supplier_ruv = NULL;
  298. *extra_referrals = NULL;
  299. *csnstr = NULL;
  300. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &extop_oid);
  301. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &extop_value);
  302. if (NULL == extop_oid ||
  303. ((strcmp(extop_oid, REPL_START_NSDS50_REPLICATION_REQUEST_OID) != 0) &&
  304. (strcmp(extop_oid, REPL_START_NSDS90_REPLICATION_REQUEST_OID) != 0)) ||
  305. NULL == extop_value || NULL == extop_value->bv_val)
  306. {
  307. /* bogus */
  308. rc = -1;
  309. goto free_and_return;
  310. }
  311. /* Set a flag to let the caller know if this is a 9.0 style start extop */
  312. if (strcmp(extop_oid, REPL_START_NSDS90_REPLICATION_REQUEST_OID) == 0)
  313. {
  314. *is90 = 1;
  315. }
  316. else
  317. {
  318. *is90 = 0;
  319. }
  320. if ((tmp_bere = ber_init(extop_value)) == NULL)
  321. {
  322. rc = -1;
  323. goto free_and_return;
  324. }
  325. if (ber_scanf(tmp_bere, "{") == LBER_ERROR)
  326. {
  327. rc = -1;
  328. goto free_and_return;
  329. }
  330. /* Get the required protocol OID and root of replicated subtree */
  331. if (ber_get_stringa(tmp_bere, protocol_oid) == LBER_DEFAULT)
  332. {
  333. rc = -1;
  334. goto free_and_return;
  335. }
  336. if (ber_get_stringa(tmp_bere, repl_root) == LBER_DEFAULT)
  337. {
  338. rc = -1;
  339. goto free_and_return;
  340. }
  341. /* get supplier's ruv */
  342. if (decode_ruv (tmp_bere, supplier_ruv) == -1)
  343. {
  344. rc = -1;
  345. goto free_and_return;
  346. }
  347. /* Get the optional set of referral URLs */
  348. if (ber_peek_tag(tmp_bere, &len) == LBER_SET)
  349. {
  350. if (ber_scanf(tmp_bere, "[v]", extra_referrals) == LBER_ERROR)
  351. {
  352. rc = -1;
  353. goto free_and_return;
  354. }
  355. }
  356. /* Get the CSN */
  357. if (ber_get_stringa(tmp_bere, csnstr) == LBER_ERROR)
  358. {
  359. rc = -1;
  360. goto free_and_return;
  361. }
  362. /* Get the optional replication session callback data. */
  363. if (ber_peek_tag(tmp_bere, &len) == LBER_OCTETSTRING)
  364. {
  365. if (ber_get_stringa(tmp_bere, data_guid) == LBER_ERROR)
  366. {
  367. rc = -1;
  368. goto free_and_return;
  369. }
  370. /* If a data_guid was specified, data must be specified as well. */
  371. if (ber_peek_tag(tmp_bere, &len) == LBER_OCTETSTRING)
  372. {
  373. if (ber_get_stringal(tmp_bere, data) == LBER_ERROR)
  374. {
  375. rc = -1;
  376. goto free_and_return;
  377. }
  378. }
  379. else
  380. {
  381. rc = -1;
  382. goto free_and_return;
  383. }
  384. }
  385. if (ber_scanf(tmp_bere, "}") == LBER_ERROR)
  386. {
  387. rc = -1;
  388. goto free_and_return;
  389. }
  390. free_and_return:
  391. if (-1 == rc)
  392. {
  393. /* Free everything when error encountered */
  394. /* slapi_ch_free accepts NULL pointer */
  395. slapi_ch_free ((void**)protocol_oid);
  396. slapi_ch_free ((void**)repl_root);
  397. slapi_ch_array_free (*extra_referrals);
  398. *extra_referrals = NULL;
  399. slapi_ch_free ((void**)csnstr);
  400. if (*supplier_ruv)
  401. {
  402. ruv_destroy (supplier_ruv);
  403. }
  404. }
  405. if (NULL != tmp_bere)
  406. {
  407. ber_free(tmp_bere, 1);
  408. tmp_bere = NULL;
  409. }
  410. return rc;
  411. }
  412. /*
  413. * Decode an NSDS50 End Replication Request extended
  414. * operation. Returns 0 on success, -1 on decoding error.
  415. * The caller is responsible for freeing repl_root.
  416. */
  417. static int
  418. decode_endrepl_extop(Slapi_PBlock *pb, char **repl_root)
  419. {
  420. char *extop_oid = NULL;
  421. struct berval *extop_value = NULL;
  422. BerElement *tmp_bere = NULL;
  423. int rc = 0;
  424. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &extop_oid);
  425. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &extop_value);
  426. if (NULL == extop_oid ||
  427. strcmp(extop_oid, REPL_END_NSDS50_REPLICATION_REQUEST_OID) != 0 ||
  428. NULL == extop_value || NULL == extop_value->bv_val)
  429. {
  430. /* bogus */
  431. rc = -1;
  432. goto free_and_return;
  433. }
  434. if ((tmp_bere = ber_init(extop_value)) == NULL)
  435. {
  436. rc = -1;
  437. goto free_and_return;
  438. }
  439. if (ber_scanf(tmp_bere, "{") == LBER_DEFAULT)
  440. {
  441. rc = -1;
  442. goto free_and_return;
  443. }
  444. /* Get the required root of replicated subtree */
  445. if (ber_get_stringa(tmp_bere, repl_root) == LBER_DEFAULT)
  446. {
  447. rc = -1;
  448. goto free_and_return;
  449. }
  450. if (ber_scanf(tmp_bere, "}") == LBER_DEFAULT)
  451. {
  452. rc = -1;
  453. goto free_and_return;
  454. }
  455. free_and_return:
  456. if (NULL != tmp_bere)
  457. {
  458. ber_free(tmp_bere, 1);
  459. tmp_bere = NULL;
  460. }
  461. return rc;
  462. }
  463. /*
  464. * Decode an NSDS50ReplicationResponse or NSDS90ReplicationResponse
  465. * extended response. The extended response just contains a sequence
  466. * that contains:
  467. * 1) An integer response code
  468. * 2) An optional array of bervals representing the consumer
  469. * replica's update vector
  470. * 3) An optional data guid and data string if this is a 9.0
  471. * style response
  472. * Returns 0 on success, or -1 if the response could not be parsed.
  473. */
  474. int
  475. decode_repl_ext_response(struct berval *bvdata, int *response_code,
  476. struct berval ***ruv_bervals, char **data_guid, struct berval **data)
  477. {
  478. BerElement *tmp_bere = NULL;
  479. int return_value = 0;
  480. PR_ASSERT(NULL != response_code);
  481. PR_ASSERT(NULL != ruv_bervals);
  482. if (NULL == bvdata || NULL == response_code || NULL == ruv_bervals ||
  483. NULL == data_guid || NULL == data || NULL == bvdata->bv_val)
  484. {
  485. return_value = -1;
  486. }
  487. else
  488. {
  489. ber_len_t len;
  490. ber_int_t temp_response_code = 0;
  491. *ruv_bervals = NULL;
  492. if ((tmp_bere = ber_init(bvdata)) == NULL)
  493. {
  494. return_value = -1;
  495. }
  496. else if (ber_scanf(tmp_bere, "{e", &temp_response_code) == LBER_ERROR)
  497. {
  498. return_value = -1;
  499. }
  500. else if (ber_peek_tag(tmp_bere, &len) == LBER_SEQUENCE)
  501. {
  502. if (ber_scanf(tmp_bere, "{V}", ruv_bervals) == LBER_ERROR)
  503. {
  504. return_value = -1;
  505. }
  506. }
  507. /* Check for optional data from replication session callback */
  508. if (ber_peek_tag(tmp_bere, &len) == LBER_OCTETSTRING)
  509. {
  510. if (ber_scanf(tmp_bere, "aO}", data_guid, data) == LBER_ERROR)
  511. {
  512. return_value = -1;
  513. }
  514. }
  515. else if (ber_scanf(tmp_bere, "}") == LBER_ERROR)
  516. {
  517. return_value = -1;
  518. }
  519. *response_code = (int)temp_response_code;
  520. }
  521. if (0 != return_value)
  522. {
  523. if (NULL != ruv_bervals && NULL != *ruv_bervals)
  524. {
  525. ber_bvecfree(*ruv_bervals);
  526. }
  527. }
  528. if (NULL != tmp_bere)
  529. {
  530. ber_free(tmp_bere, 1); tmp_bere = NULL;
  531. }
  532. return return_value;
  533. }
  534. /*
  535. * This plugin entry point is called whenever a
  536. * StartNSDS50ReplicationRequest is received.
  537. */
  538. int
  539. multimaster_extop_StartNSDS50ReplicationRequest(Slapi_PBlock *pb)
  540. {
  541. int return_value = SLAPI_PLUGIN_EXTENDED_NOT_HANDLED;
  542. ber_int_t response = 0;
  543. int rc = 0;
  544. BerElement *resp_bere = NULL;
  545. struct berval *resp_bval = NULL;
  546. char *protocol_oid = NULL;
  547. char *repl_root = NULL;
  548. Slapi_DN *repl_root_sdn = NULL;
  549. char **referrals = NULL;
  550. Object *replica_object = NULL;
  551. Replica *replica = NULL;
  552. void *conn;
  553. consumer_connection_extension *connext = NULL;
  554. char *replicacsnstr = NULL;
  555. CSN *replicacsn = NULL;
  556. int zero = 0;
  557. int one = 1;
  558. RUV *ruv = NULL;
  559. struct berval **ruv_bervals = NULL;
  560. CSNGen *gen = NULL;
  561. Object *gen_obj = NULL;
  562. Slapi_DN *bind_sdn = NULL;
  563. char *bind_dn = NULL;
  564. Object *ruv_object = NULL;
  565. RUV *supplier_ruv = NULL;
  566. PRUint64 connid = 0;
  567. int opid = 0;
  568. PRBool isInc = PR_FALSE; /* true if incremental update */
  569. char *locking_purl = NULL; /* the supplier contacting us */
  570. char *current_purl = NULL; /* the supplier which already has exclusive access */
  571. char locking_session[24];
  572. char *data_guid = NULL;
  573. struct berval *data = NULL;
  574. int is90 = 0;
  575. /* Decode the extended operation */
  576. if (decode_startrepl_extop(pb, &protocol_oid, &repl_root, &supplier_ruv,
  577. &referrals, &replicacsnstr, &data_guid, &data, &is90) == -1)
  578. {
  579. response = NSDS50_REPL_DECODING_ERROR;
  580. goto send_response;
  581. }
  582. if (NULL == protocol_oid || NULL == repl_root || NULL == replicacsnstr)
  583. {
  584. response = NSDS50_REPL_DECODING_ERROR;
  585. goto send_response;
  586. }
  587. slapi_pblock_get(pb, SLAPI_CONN_ID, &connid);
  588. slapi_pblock_get(pb, SLAPI_OPERATION_ID, &opid);
  589. /*
  590. * Get a hold of the connection extension object and
  591. * make sure it's there.
  592. */
  593. slapi_pblock_get(pb, SLAPI_CONNECTION, &conn);
  594. connext = (consumer_connection_extension *)repl_con_get_ext(
  595. REPL_CON_EXT_CONN, conn);
  596. if (NULL == connext)
  597. {
  598. /* Something bad happened. Don't go any further */
  599. response = NSDS50_REPL_INTERNAL_ERROR;
  600. goto send_response;
  601. }
  602. /* Verify that we know about this replication protocol OID */
  603. if (strcmp(protocol_oid, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID) == 0)
  604. {
  605. if (repl_session_plugin_call_recv_acquire_cb(repl_root, 0 /* is_total == FALSE */,
  606. data_guid, data))
  607. {
  608. slapi_ch_free_string(&data_guid);
  609. ber_bvfree(data);
  610. data = NULL;
  611. response = NSDS50_REPL_BACKOFF;
  612. goto send_response;
  613. } else {
  614. slapi_ch_free_string(&data_guid);
  615. ber_bvfree(data);
  616. data = NULL;
  617. }
  618. /* Stash info that this is an incremental update session */
  619. connext->repl_protocol_version = REPL_PROTOCOL_50_INCREMENTAL;
  620. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  621. "conn=%" NSPRIu64 " op=%d repl=\"%s\": Begin incremental protocol\n",
  622. connid, opid, repl_root);
  623. isInc = PR_TRUE;
  624. }
  625. else if (strcmp(protocol_oid, REPL_NSDS50_TOTAL_PROTOCOL_OID) == 0)
  626. {
  627. if (repl_session_plugin_call_recv_acquire_cb(repl_root, 1 /* is_total == TRUE */,
  628. data_guid, data))
  629. {
  630. slapi_ch_free_string(&data_guid);
  631. ber_bvfree(data);
  632. data = NULL;
  633. response = NSDS50_REPL_DISABLED;
  634. goto send_response;
  635. } else {
  636. slapi_ch_free_string(&data_guid);
  637. ber_bvfree(data);
  638. data = NULL;
  639. }
  640. /* Stash info that this is a total update session */
  641. if (NULL != connext)
  642. {
  643. connext->repl_protocol_version = REPL_PROTOCOL_50_TOTALUPDATE;
  644. }
  645. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  646. "conn=%" NSPRIu64 " op=%d repl=\"%s\": Begin total protocol\n",
  647. connid, opid, repl_root);
  648. isInc = PR_FALSE;
  649. }
  650. else if (strcmp(protocol_oid, REPL_NSDS71_INCREMENTAL_PROTOCOL_OID) == 0)
  651. {
  652. /* Stash info that this is an incremental update session */
  653. connext->repl_protocol_version = REPL_PROTOCOL_50_INCREMENTAL;
  654. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  655. "conn=%" NSPRIu64 " op=%d repl=\"%s\": Begin 7.1 incremental protocol\n",
  656. connid, opid, repl_root);
  657. isInc = PR_TRUE;
  658. }
  659. else if (strcmp(protocol_oid, REPL_NSDS71_TOTAL_PROTOCOL_OID) == 0)
  660. {
  661. /* Stash info that this is a total update session */
  662. if (NULL != connext)
  663. {
  664. connext->repl_protocol_version = REPL_PROTOCOL_71_TOTALUPDATE;
  665. }
  666. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  667. "conn=%" NSPRIu64 " op=%d repl=\"%s\": Begin 7.1 total protocol\n",
  668. connid, opid, repl_root);
  669. isInc = PR_FALSE;
  670. }
  671. else
  672. {
  673. /* Unknown replication protocol */
  674. response = NSDS50_REPL_UNKNOWN_UPDATE_PROTOCOL;
  675. goto send_response;
  676. }
  677. /* Verify that repl_root names a valid replicated area */
  678. if ((repl_root_sdn = slapi_sdn_new_dn_byval(repl_root)) == NULL)
  679. {
  680. response = NSDS50_REPL_INTERNAL_ERROR;
  681. goto send_response;
  682. }
  683. /* see if this replica is being configured and wait for it */
  684. if (replica_is_being_configured(repl_root))
  685. {
  686. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  687. "conn=%" NSPRIu64 " op=%d replica=\"%s\": "
  688. "Replica is being configured: try again later\n",
  689. connid, opid, repl_root);
  690. response = NSDS50_REPL_REPLICA_BUSY;
  691. goto send_response;
  692. }
  693. replica_object = replica_get_replica_from_dn(repl_root_sdn);
  694. if (NULL != replica_object)
  695. {
  696. replica = object_get_data(replica_object);
  697. }
  698. if (NULL == replica)
  699. {
  700. response = NSDS50_REPL_NO_SUCH_REPLICA;
  701. goto send_response;
  702. }
  703. if (REPL_PROTOCOL_50_TOTALUPDATE == connext->repl_protocol_version)
  704. {
  705. /* If total update has been initiated against other replicas or
  706. * this replica is already being initialized, we should return
  707. * an error immediately. */
  708. if (replica_is_state_flag_set(replica,
  709. REPLICA_TOTAL_EXCL_SEND|REPLICA_TOTAL_EXCL_RECV))
  710. {
  711. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  712. "%s: total update on is initiated on the replica. Cannot execute the total update from other master.\n", repl_root);
  713. response = NSDS50_REPL_REPLICA_BUSY;
  714. goto send_response;
  715. }
  716. else
  717. {
  718. replica_set_state_flag (replica, REPLICA_TOTAL_EXCL_RECV, 0);
  719. }
  720. }
  721. /* check that this replica is not a 4.0 consumer */
  722. if (replica_is_legacy_consumer (replica))
  723. {
  724. response = NSDS50_REPL_LEGACY_CONSUMER;
  725. goto send_response;
  726. }
  727. /* Check that bind dn is authorized to supply replication updates */
  728. slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn); /* bind_dn is allocated */
  729. bind_sdn = slapi_sdn_new_dn_passin(bind_dn);
  730. if (replica_is_updatedn(replica, bind_sdn) == PR_FALSE)
  731. {
  732. response = NSDS50_REPL_PERMISSION_DENIED;
  733. goto send_response;
  734. }
  735. /* Check received CSN for clock skew */
  736. gen_obj = replica_get_csngen(replica);
  737. if (NULL != gen_obj)
  738. {
  739. gen = object_get_data(gen_obj);
  740. if (NULL != gen)
  741. {
  742. replicacsn = csn_new_by_string(replicacsnstr);
  743. if (NULL != replicacsn)
  744. {
  745. /* ONREPL - we used to manage clock skew here. However, csn generator
  746. code already does it. The csngen also manages local skew caused by
  747. system clock reset, so to keep it consistent, I removed code from here */
  748. /* update the state of the csn generator */
  749. rc = replica_update_csngen_state_ext (replica, supplier_ruv, replicacsn); /* too much skew */
  750. if (rc == CSN_LIMIT_EXCEEDED)
  751. {
  752. response = NSDS50_REPL_EXCESSIVE_CLOCK_SKEW;
  753. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  754. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  755. "Excessive clock skew from supplier RUV\n",
  756. connid, opid, repl_root);
  757. goto send_response;
  758. }
  759. else if (rc != 0)
  760. {
  761. /* Oops, problem csn or ruv format, or memory, or .... */
  762. response = NSDS50_REPL_INTERNAL_ERROR;
  763. goto send_response;
  764. }
  765. }
  766. else
  767. {
  768. /* Oops, csnstr couldn't be converted */
  769. response = NSDS50_REPL_INTERNAL_ERROR;
  770. goto send_response;
  771. }
  772. }
  773. else
  774. {
  775. /* Oops, no csn generator */
  776. response = NSDS50_REPL_INTERNAL_ERROR;
  777. goto send_response;
  778. }
  779. }
  780. else
  781. {
  782. /* Oops, no csn generator object */
  783. response = NSDS50_REPL_INTERNAL_ERROR;
  784. goto send_response;
  785. }
  786. if (check_replica_id_uniqueness(replica, supplier_ruv) != 0){
  787. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  788. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  789. "Replica has same replicaID %d as supplier\n",
  790. connid, opid, repl_root, replica_get_rid(replica));
  791. response = NSDS50_REPL_REPLICAID_ERROR;
  792. goto send_response;
  793. }
  794. /* Attempt to acquire exclusive access to the replicated area */
  795. /* Since partial URL is always the master, this locking_purl does not
  796. * help us to know the true locker when it is a hub. Change to use
  797. * the session's conn id and op id to identify the the supplier.
  798. */
  799. /* junkrc = ruv_get_first_id_and_purl(supplier_ruv, &junkrid, &locking_purl); */
  800. PR_snprintf(locking_session, sizeof(locking_session), "conn=%" NSPRIu64 " id=%d", connid, opid);
  801. locking_purl = &locking_session[0];
  802. if (replica_get_exclusive_access(replica, &isInc, connid, opid,
  803. locking_purl,
  804. &current_purl) == PR_FALSE)
  805. {
  806. locking_purl = NULL; /* no dangling pointers */
  807. response = NSDS50_REPL_REPLICA_BUSY;
  808. goto send_response;
  809. }
  810. else
  811. {
  812. locking_purl = NULL; /* no dangling pointers */
  813. /* Stick the replica object pointer in the connection extension */
  814. connext->replica_acquired = (void *)replica_object;
  815. replica_object = NULL;
  816. }
  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. if (connext->repl_protocol_version == REPL_PROTOCOL_50_INCREMENTAL)
  840. {
  841. /* The supplier ruv may have changed, so let's update the referrals */
  842. consumer5_set_mapping_tree_state_for_replica(replica, connext->supplier_ruv);
  843. }
  844. else /* full protocol */
  845. {
  846. char *mtnstate = slapi_mtn_get_state(repl_root_sdn);
  847. char **mtnreferral = slapi_mtn_get_referral(repl_root_sdn);
  848. /* richm 20041118 - we do not want to reap tombstones while there is
  849. a total update in progress, so shut it down */
  850. replica_set_tombstone_reap_stop(replica, PR_TRUE);
  851. /* richm 20010831 - set the mapping tree to the referral state *before*
  852. we invoke slapi_start_bulk_import - see bug 556992 -
  853. slapi_start_bulk_import sets the database offline, if an operation comes
  854. in while the database is offline but the mapping tree is not referring yet,
  855. the server gets confused
  856. */
  857. /* During a total update we refer *all* operations */
  858. repl_set_mtn_state_and_referrals(repl_root_sdn, STATE_REFERRAL,
  859. connext->supplier_ruv, NULL, referrals);
  860. /* LPREPL - check the return code.
  861. * But what do we do if mapping tree could not be updated ? */
  862. /* start the bulk import */
  863. slapi_pblock_set (pb, SLAPI_TARGET_DN, repl_root);
  864. rc = slapi_start_bulk_import (pb);
  865. if (rc != LDAP_SUCCESS)
  866. {
  867. response = NSDS50_REPL_INTERNAL_ERROR;
  868. /* reset the mapping tree state to what it was before
  869. we tried to do the bulk import if mtnstate exists */
  870. if (mtnstate) {
  871. repl_set_mtn_state_and_referrals(repl_root_sdn, mtnstate,
  872. NULL, NULL, mtnreferral);
  873. slapi_ch_free_string(&mtnstate);
  874. }
  875. charray_free(mtnreferral);
  876. mtnreferral = NULL;
  877. goto send_response;
  878. }
  879. slapi_ch_free_string(&mtnstate);
  880. charray_free(mtnreferral);
  881. mtnreferral = NULL;
  882. }
  883. response = NSDS50_REPL_REPLICA_READY;
  884. /* Set the "is replication session" flag in the connection extension */
  885. slapi_pblock_set( pb, SLAPI_CONN_IS_REPLICATION_SESSION, &one );
  886. connext->isreplicationsession = 1;
  887. /* Save away the connection */
  888. slapi_pblock_get(pb, SLAPI_CONNECTION, &connext->connection);
  889. send_response:
  890. if (connext && replica &&
  891. (REPL_PROTOCOL_50_TOTALUPDATE == connext->repl_protocol_version))
  892. {
  893. replica_set_state_flag (replica, REPLICA_TOTAL_EXCL_RECV, 1);
  894. }
  895. if (response != NSDS50_REPL_REPLICA_READY)
  896. {
  897. int resp_log_level = SLAPI_LOG_FATAL;
  898. char purlstr[1024] = {0};
  899. if (current_purl)
  900. PR_snprintf(purlstr, sizeof(purlstr), " locked by %s for %s update", current_purl,
  901. isInc ? "incremental" : "total");
  902. /* Don't log replica busy as errors - these are almost always not
  903. errors - use the replication monitoring tools to determine if
  904. a replica is not converging, then look for pathological replica
  905. busy errors by turning on the replication log level. We also
  906. don't want to log replica backoff as an error, as that response
  907. is only used when a replication session hook wants a master to
  908. go into incremental backoff mode. */
  909. if ((response == NSDS50_REPL_REPLICA_BUSY) || (response == NSDS50_REPL_BACKOFF)) {
  910. resp_log_level = SLAPI_LOG_REPL;
  911. }
  912. slapi_log_error (resp_log_level, repl_plugin_name,
  913. "conn=%" NSPRIu64 " op=%d replica=\"%s\": "
  914. "Unable to acquire replica: error: %s%s\n",
  915. connid, opid,
  916. (replica ? slapi_sdn_get_dn(replica_get_root(replica)) : "unknown"),
  917. protocol_response2string (response), purlstr);
  918. /* enable tombstone reap again since the total update failed */
  919. replica_set_tombstone_reap_stop(replica, PR_FALSE);
  920. }
  921. /* Call any registered replica session reply callback. We
  922. * want to reject the updates if the return value is non-0. */
  923. if (repl_session_plugin_call_reply_acquire_cb(replica ?
  924. slapi_sdn_get_ndn(replica_get_root(replica)) : "",
  925. ((isInc == PR_TRUE) ? 0 : 1), &data_guid, &data))
  926. {
  927. slapi_ch_free_string(&data_guid);
  928. ber_bvfree(data);
  929. data = NULL;
  930. response = NSDS50_REPL_BACKOFF;
  931. }
  932. /* Send the response */
  933. if ((resp_bere = der_alloc()) == NULL)
  934. {
  935. /* ONREPL - not sure what we suppose to do here */
  936. }
  937. ber_printf(resp_bere, "{e", response);
  938. if (NULL != ruv_bervals)
  939. {
  940. ber_printf(resp_bere, "{V}", ruv_bervals);
  941. }
  942. /* Add extra data from replication session callback if necessary */
  943. if (is90 && data_guid && data)
  944. {
  945. ber_printf(resp_bere, "sO", data_guid, data);
  946. }
  947. ber_printf(resp_bere, "}");
  948. ber_flatten(resp_bere, &resp_bval);
  949. if (is90)
  950. {
  951. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, REPL_NSDS90_REPLICATION_RESPONSE_OID);
  952. }
  953. else
  954. {
  955. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, REPL_NSDS50_REPLICATION_RESPONSE_OID);
  956. }
  957. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, resp_bval);
  958. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  959. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  960. "%s: response=%d rc=%d\n",
  961. connid, opid, repl_root,
  962. is90 ? "StartNSDS90ReplicationRequest" :
  963. "StartNSDS50ReplicationRequest", response, rc);
  964. slapi_send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
  965. return_value = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  966. /* Free any data allocated by the replication
  967. * session reply callback. */
  968. slapi_ch_free_string(&data_guid);
  969. ber_bvfree(data);
  970. data = NULL;
  971. slapi_ch_free_string(&current_purl);
  972. /* protocol_oid */
  973. /* slapi_ch_free accepts NULL pointer */
  974. slapi_ch_free((void **)&protocol_oid);
  975. /* repl_root */
  976. slapi_ch_free((void **)&repl_root);
  977. /* supplier's ruv */
  978. if (supplier_ruv)
  979. {
  980. ruv_destroy (&supplier_ruv);
  981. }
  982. /* referrals */
  983. slapi_ch_free((void **)&referrals);
  984. /* replicacsnstr */
  985. slapi_ch_free((void **)&replicacsnstr);
  986. /* repl_root_sdn */
  987. if (NULL != repl_root_sdn)
  988. {
  989. slapi_sdn_free(&repl_root_sdn);
  990. }
  991. if (NSDS50_REPL_REPLICA_READY != response)
  992. {
  993. /*
  994. * Something went wrong, and we never told the other end that the
  995. * replica had been acquired, so we'd better release it.
  996. */
  997. if (NULL != connext && NULL != connext->replica_acquired)
  998. {
  999. Object *r_obj = (Object*)connext->replica_acquired;
  1000. replica_relinquish_exclusive_access((Replica*)object_get_data (r_obj),
  1001. connid, opid);
  1002. }
  1003. /* Remove any flags that would indicate repl session in progress */
  1004. if (NULL != connext)
  1005. {
  1006. connext->repl_protocol_version = REPL_PROTOCOL_UNKNOWN;
  1007. connext->isreplicationsession = 0;
  1008. }
  1009. slapi_pblock_set( pb, SLAPI_CONN_IS_REPLICATION_SESSION, &zero );
  1010. }
  1011. /* Release reference to replica_object */
  1012. if (NULL != replica_object)
  1013. {
  1014. object_release(replica_object);
  1015. }
  1016. /* bind_sdn */
  1017. if (NULL != bind_sdn)
  1018. {
  1019. slapi_sdn_free(&bind_sdn);
  1020. }
  1021. /* Release reference to gen_obj */
  1022. if (NULL != gen_obj)
  1023. {
  1024. object_release(gen_obj);
  1025. }
  1026. /* replicacsn */
  1027. if (NULL != replicacsn)
  1028. {
  1029. csn_free(&replicacsn);
  1030. }
  1031. /* resp_bere */
  1032. if (NULL != resp_bere)
  1033. {
  1034. ber_free(resp_bere, 1);
  1035. }
  1036. /* resp_bval */
  1037. if (NULL != resp_bval)
  1038. {
  1039. ber_bvfree(resp_bval);
  1040. }
  1041. /* ruv_bervals */
  1042. if (NULL != ruv_bervals)
  1043. {
  1044. ber_bvecfree(ruv_bervals);
  1045. }
  1046. return return_value;
  1047. }
  1048. /*
  1049. * This plugin entry point is called whenever an
  1050. * EndNSDS50ReplicationRequest is received.
  1051. * XXXggood this code is not finished.
  1052. */
  1053. int
  1054. multimaster_extop_EndNSDS50ReplicationRequest(Slapi_PBlock *pb)
  1055. {
  1056. int return_value = SLAPI_PLUGIN_EXTENDED_NOT_HANDLED;
  1057. char *repl_root = NULL;
  1058. BerElement *resp_bere = NULL;
  1059. struct berval *resp_bval = NULL;
  1060. ber_int_t response;
  1061. void *conn;
  1062. consumer_connection_extension *connext = NULL;
  1063. PRUint64 connid = 0;
  1064. int opid=-1;
  1065. /* Decode the extended operation */
  1066. if (decode_endrepl_extop(pb, &repl_root) == -1)
  1067. {
  1068. response = NSDS50_REPL_DECODING_ERROR;
  1069. }
  1070. else
  1071. {
  1072. /* First, verify that the current connection is a replication session */
  1073. /* XXXggood - do we need to wait around for any pending updates to complete?
  1074. I suppose it's possible that the end request may arrive asynchronously, before
  1075. we're really done processing all the updates.
  1076. */
  1077. /* Get a hold of the connection extension object */
  1078. slapi_pblock_get(pb, SLAPI_CONNECTION, &conn);
  1079. connext = (consumer_connection_extension *)repl_con_get_ext(
  1080. REPL_CON_EXT_CONN, conn);
  1081. if (NULL != connext && NULL != connext->replica_acquired)
  1082. {
  1083. int zero= 0;
  1084. Replica *r = (Replica*)object_get_data ((Object*)connext->replica_acquired);
  1085. /* if this is total protocol we need to install suppliers ruv for the replica */
  1086. if (connext->repl_protocol_version == REPL_PROTOCOL_50_TOTALUPDATE)
  1087. {
  1088. /* We no longer need to refer all operations...
  1089. * and update the referrals on the mapping tree node
  1090. */
  1091. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  1092. /* LPREPL - First we clear the total in progress flag
  1093. Like this we know it's a normal termination of import. This is required by
  1094. the replication function that responds to backend state change.
  1095. If the flag is not clear, the callback knows that replication should not be
  1096. enabled again */
  1097. replica_set_state_flag(r, REPLICA_TOTAL_IN_PROGRESS, PR_TRUE /* clear flag */);
  1098. slapi_pblock_set (pb, SLAPI_TARGET_DN, repl_root);
  1099. slapi_stop_bulk_import (pb);
  1100. /* ONREPL - this is a bit of a hack. Once bulk import is finished,
  1101. the replication function that responds to backend state change
  1102. will be called. That function normally do all ruv and changelog
  1103. processing. However, in the case of replica initalization, it
  1104. will not do the right thing because supplier does not send its
  1105. ruv tombstone to the consumer. So that's why we need to do the
  1106. second processing here.
  1107. The supplier does not send its RUV entry because it could be
  1108. more up to date then the data send to the consumer.
  1109. The best solution I think, would be to "fake" on the supplier
  1110. an entry that corresponds to the ruv sent to the consumer and then
  1111. send it as part of the data */
  1112. if (cl5GetState () == CL5_STATE_OPEN)
  1113. {
  1114. cl5DeleteDBSync (connext->replica_acquired);
  1115. }
  1116. replica_set_ruv (r, connext->supplier_ruv);
  1117. connext->supplier_ruv = NULL;
  1118. /* if changelog is enabled, we need to log a dummy change for the
  1119. smallest csn in the new ruv, so that this replica ca supply
  1120. other servers.
  1121. */
  1122. if (cl5GetState () == CL5_STATE_OPEN)
  1123. {
  1124. replica_log_ruv_elements (r);
  1125. }
  1126. /* ONREPL code that dealt with new RUV, etc was moved into the code
  1127. that enables replication when a backend comes back online. This
  1128. code is called once the bulk import is finished */
  1129. /* allow reaping again */
  1130. replica_set_tombstone_reap_stop(r, PR_FALSE);
  1131. }
  1132. else if (connext->repl_protocol_version == REPL_PROTOCOL_50_INCREMENTAL)
  1133. {
  1134. /* The ruv from the supplier may have changed. Report the change on the
  1135. consumer side */
  1136. replica_update_ruv_consumer(r, connext->supplier_ruv);
  1137. }
  1138. /* Relinquish control of the replica */
  1139. slapi_pblock_get (pb, SLAPI_OPERATION_ID, &opid);
  1140. if (opid) slapi_pblock_get (pb, SLAPI_CONN_ID, &connid);
  1141. replica_relinquish_exclusive_access(r, connid, opid);
  1142. object_release ((Object*)connext->replica_acquired);
  1143. connext->replica_acquired = NULL;
  1144. connext->isreplicationsession= 0;
  1145. slapi_pblock_set( pb, SLAPI_CONN_IS_REPLICATION_SESSION, &zero );
  1146. response = NSDS50_REPL_REPLICA_RELEASE_SUCCEEDED;
  1147. /* Outbound replication agreements need to all be restarted now */
  1148. /* XXXGGOOD RESTART REEPL AGREEMENTS */
  1149. } else {
  1150. /* Unless bail out, we return uninitialized response */
  1151. goto free_and_return;
  1152. }
  1153. }
  1154. /* Send the response code */
  1155. if ((resp_bere = der_alloc()) == NULL)
  1156. {
  1157. goto free_and_return;
  1158. }
  1159. ber_printf(resp_bere, "{e}", response);
  1160. ber_flatten(resp_bere, &resp_bval);
  1161. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, REPL_NSDS50_REPLICATION_RESPONSE_OID);
  1162. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, resp_bval);
  1163. slapi_send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
  1164. return_value = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  1165. free_and_return:
  1166. /* repl_root */
  1167. slapi_ch_free((void **)&repl_root);
  1168. /* BerElement */
  1169. if (NULL != resp_bere)
  1170. {
  1171. ber_free(resp_bere, 1);
  1172. }
  1173. /* response */
  1174. if (NULL != resp_bval)
  1175. {
  1176. ber_bvfree(resp_bval);
  1177. }
  1178. return return_value;
  1179. }
  1180. /*
  1181. * This plugin entry point is a noop entry
  1182. * point. It's used when registering extops that
  1183. * are only used as responses. We'll never receive
  1184. * one of those, unsolicited, but we still want to
  1185. * register them so they appear in the
  1186. * supportedextension attribute in the root DSE.
  1187. */
  1188. int
  1189. extop_noop(Slapi_PBlock *pb)
  1190. {
  1191. return SLAPI_PLUGIN_EXTENDED_NOT_HANDLED;
  1192. }
  1193. static int
  1194. check_replica_id_uniqueness(Replica *replica, RUV *supplier_ruv)
  1195. {
  1196. ReplicaId local_rid = replica_get_rid(replica);
  1197. ReplicaId sup_rid = 0;
  1198. char *sup_purl = NULL;
  1199. if (ruv_get_first_id_and_purl(supplier_ruv, &sup_rid, &sup_purl) == RUV_SUCCESS) {
  1200. /* ReplicaID Uniqueness is checked only on Masters */
  1201. if ((replica_get_type(replica) == REPLICA_TYPE_UPDATABLE) &&
  1202. (sup_rid == local_rid)) {
  1203. return 1;
  1204. }
  1205. }
  1206. return 0;
  1207. }