repl_extop.c 33 KB

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