repl_extop.c 33 KB

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