repl5_protocol_util.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* repl5_protocol_util.c */
  13. /*
  14. Code common to both incremental and total protocols.
  15. */
  16. #include "repl5.h"
  17. #include "repl5_prot_private.h"
  18. /*
  19. * Obtain a current CSN (e.g. one that would have been
  20. * generated for an operation occurring at this time)
  21. * for a given replica.
  22. */
  23. CSN *
  24. get_current_csn(Slapi_DN *replarea_sdn)
  25. {
  26. Object *replica_obj;
  27. Replica *replica;
  28. Object *gen_obj;
  29. CSNGen *gen;
  30. CSN *current_csn = NULL;
  31. if (NULL != replarea_sdn)
  32. {
  33. replica_obj = replica_get_replica_from_dn(replarea_sdn);
  34. if (NULL != replica_obj)
  35. {
  36. replica = object_get_data(replica_obj);
  37. if (NULL != replica)
  38. {
  39. gen_obj = replica_get_csngen(replica);
  40. if (NULL != gen_obj)
  41. {
  42. gen = (CSNGen *)object_get_data(gen_obj);
  43. if (NULL != gen)
  44. {
  45. if (csngen_new_csn(gen, &current_csn,
  46. PR_FALSE /* notify */) != CSN_SUCCESS)
  47. {
  48. csn_free(&current_csn);
  49. }
  50. object_release(gen_obj);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. return current_csn;
  57. }
  58. /*
  59. * Acquire exclusive access to a replica. Send a start replication extended
  60. * operation to the replica. The response will contain a success code, and
  61. * optionally the replica's update vector if acquisition is successful.
  62. * This function returns one of the following:
  63. * ACQUIRE_SUCCESS - the replica was acquired, and we have exclusive update access
  64. * ACQUIRE_REPLICA_BUSY - another master was updating the replica
  65. * ACQUIRE_FATAL_ERROR - something bad happened, and it's not likely to improve
  66. * if we wait.
  67. * ACQUIRE_TRANSIENT_ERROR - something bad happened, but it's probably worth
  68. * another try after waiting a while.
  69. * If ACQUIRE_SUCCESS is returned, then ruv will point to the replica's update
  70. * vector. It's possible that the replica does something goofy and doesn't
  71. * return us an update vector, so be prepared for ruv to be NULL (but this is
  72. * an error).
  73. */
  74. int
  75. acquire_replica(Private_Repl_Protocol *prp, char *prot_oid, RUV **ruv)
  76. {
  77. int return_value;
  78. ConnResult crc;
  79. Repl_Connection *conn;
  80. struct berval *retdata = NULL;
  81. char *retoid = NULL;
  82. Slapi_DN *replarea_sdn = NULL;
  83. struct berval **ruv_bervals = NULL;
  84. CSN *current_csn = NULL;
  85. PR_ASSERT(prp && prot_oid);
  86. if (prp->replica_acquired) /* we already acquire replica */
  87. {
  88. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name,
  89. "acquire_replica - %s: Remote replica already acquired\n",
  90. agmt_get_long_name(prp->agmt));
  91. return_value = ACQUIRE_FATAL_ERROR;
  92. return ACQUIRE_SUCCESS;
  93. }
  94. if (NULL != ruv)
  95. {
  96. ruv_destroy ( ruv );
  97. }
  98. if (strcmp(prot_oid, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID) == 0)
  99. {
  100. Replica *replica;
  101. Object *supl_ruv_obj, *cons_ruv_obj;
  102. PRBool is_newer = PR_FALSE;
  103. object_acquire(prp->replica_object);
  104. replica = object_get_data(prp->replica_object);
  105. supl_ruv_obj = replica_get_ruv ( replica );
  106. cons_ruv_obj = agmt_get_consumer_ruv ( prp->agmt );
  107. is_newer = ruv_is_newer ( supl_ruv_obj, cons_ruv_obj );
  108. if ( supl_ruv_obj ) object_release ( supl_ruv_obj );
  109. if ( cons_ruv_obj ) object_release ( cons_ruv_obj );
  110. object_release (prp->replica_object);
  111. replica = NULL;
  112. if (is_newer == PR_FALSE) {
  113. prp->last_acquire_response_code = NSDS50_REPL_UPTODATE;
  114. return ACQUIRE_CONSUMER_WAS_UPTODATE;
  115. }
  116. }
  117. prp->last_acquire_response_code = NSDS50_REPL_REPLICA_NO_RESPONSE;
  118. /* Get the connection */
  119. conn = prp->conn;
  120. crc = conn_connect(conn);
  121. if (CONN_OPERATION_FAILED == crc)
  122. {
  123. int operation, error;
  124. conn_get_error(conn, &operation, &error);
  125. agmt_set_last_update_status(prp->agmt, error, NSDS50_REPL_CONN_ERROR,
  126. "Problem connecting to replica");
  127. return_value = ACQUIRE_TRANSIENT_ERROR;
  128. }
  129. else if (CONN_SSL_NOT_ENABLED == crc)
  130. {
  131. int operation, error;
  132. conn_get_error(conn, &operation, &error);
  133. agmt_set_last_update_status(prp->agmt, error, NSDS50_REPL_CONN_ERROR,
  134. "Problem connecting to replica (SSL not enabled)");
  135. return_value = ACQUIRE_FATAL_ERROR;
  136. }
  137. else
  138. {
  139. /* we don't want the timer to go off in the middle of an operation */
  140. conn_cancel_linger(conn);
  141. /* Does the remote replica support the 5.0 protocol? */
  142. crc = conn_replica_supports_ds5_repl(conn);
  143. if (CONN_DOES_NOT_SUPPORT_DS5_REPL == crc)
  144. {
  145. return_value = ACQUIRE_FATAL_ERROR;
  146. }
  147. else if (CONN_NOT_CONNECTED == crc || CONN_OPERATION_FAILED == crc)
  148. {
  149. /* We don't know anything about the remote replica. Try again later. */
  150. return_value = ACQUIRE_TRANSIENT_ERROR;
  151. goto error;
  152. }
  153. /* Find out what level of replication the replica supports. */
  154. crc = conn_replica_supports_ds90_repl(conn);
  155. if (CONN_DOES_NOT_SUPPORT_DS90_REPL == crc)
  156. {
  157. /* Does the remote replica support the 7.1 protocol? */
  158. crc = conn_replica_supports_ds71_repl(conn);
  159. if (CONN_DOES_NOT_SUPPORT_DS71_REPL == crc)
  160. {
  161. /* This is a pre-7.1 replica. */
  162. prp->repl50consumer = 1;
  163. }
  164. else if (CONN_NOT_CONNECTED == crc || CONN_OPERATION_FAILED == crc)
  165. {
  166. /* We don't know anything about the remote replica. Try again later. */
  167. return_value = ACQUIRE_TRANSIENT_ERROR;
  168. goto error;
  169. }
  170. else
  171. {
  172. /* This replica is later than 7.1, but pre-9.0. */
  173. prp->repl71consumer = 1;
  174. }
  175. }
  176. else if (CONN_NOT_CONNECTED == crc || CONN_OPERATION_FAILED == crc)
  177. {
  178. /* We don't know anything about the remote replica. Try again later. */
  179. return_value = ACQUIRE_TRANSIENT_ERROR;
  180. goto error;
  181. }
  182. else
  183. {
  184. /* This replica is a 9.0 or later replica. */
  185. prp->repl90consumer = 1;
  186. }
  187. /* Good to go. Start the protocol. */
  188. /* Obtain a current CSN */
  189. replarea_sdn = agmt_get_replarea(prp->agmt);
  190. current_csn = get_current_csn(replarea_sdn);
  191. if (NULL != current_csn)
  192. {
  193. struct berval *payload = NULL;
  194. int send_msgid = 0;
  195. if (prp->repl90consumer)
  196. {
  197. int is_total = 0;
  198. char *data_guid = NULL;
  199. struct berval *data = NULL;
  200. /* Check if this is a total or incremental update. */
  201. if (strcmp(REPL_NSDS50_TOTAL_PROTOCOL_OID, prot_oid) == 0)
  202. {
  203. is_total = 1;
  204. }
  205. /* Call pre-start replication session callback. This callback
  206. * may have extra data to be sent to the replica. */
  207. if (repl_session_plugin_call_pre_acquire_cb(prp->agmt, is_total,
  208. &data_guid, &data) == 0) {
  209. payload = NSDS90StartReplicationRequest_new(
  210. prot_oid, slapi_sdn_get_ndn(replarea_sdn),
  211. NULL, current_csn, data_guid, data);
  212. slapi_ch_free_string(&data_guid);
  213. ber_bvfree(data);
  214. data = NULL;
  215. } else {
  216. return_value = ACQUIRE_TRANSIENT_ERROR;
  217. slapi_ch_free_string(&data_guid);
  218. ber_bvfree(data);
  219. data = NULL;
  220. goto error;
  221. }
  222. }
  223. else
  224. {
  225. payload = NSDS50StartReplicationRequest_new(
  226. prot_oid, slapi_sdn_get_ndn(replarea_sdn),
  227. NULL /* XXXggood need to provide referral(s) */, current_csn);
  228. }
  229. /* JCMREPL - Need to extract the referrals from the RUV */
  230. crc = conn_send_extended_operation(conn,
  231. prp->repl90consumer ? REPL_START_NSDS90_REPLICATION_REQUEST_OID :
  232. REPL_START_NSDS50_REPLICATION_REQUEST_OID, payload,
  233. NULL /* update control */, &send_msgid /* Message ID */);
  234. if (CONN_OPERATION_SUCCESS != crc)
  235. {
  236. int operation, error;
  237. conn_get_error(conn, &operation, &error);
  238. /* Couldn't send the extended operation */
  239. return_value = ACQUIRE_TRANSIENT_ERROR; /* XXX right return value? */
  240. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name, "acquire_replica - "
  241. "%s: Unable to send a startReplication "
  242. "extended operation to consumer (%s). Will retry later.\n",
  243. agmt_get_long_name(prp->agmt),
  244. error ? ldap_err2string(error) : "unknown error");
  245. }
  246. /* Since the operation request is async, we need to wait for the response here */
  247. crc = conn_read_result_ex(conn,&retoid,&retdata,NULL,send_msgid,NULL,1);
  248. ber_bvfree(payload);
  249. payload = NULL;
  250. /* Look at the response we got. */
  251. if (CONN_OPERATION_SUCCESS == crc)
  252. {
  253. /*
  254. * Extop was processed. Look at extop response to see if we're
  255. * permitted to go ahead.
  256. */
  257. int extop_result;
  258. char *data_guid = NULL;
  259. struct berval *data = NULL;
  260. int extop_rc = decode_repl_ext_response(retdata, &extop_result,
  261. &ruv_bervals, &data_guid,
  262. &data);
  263. if (0 == extop_rc)
  264. {
  265. prp->last_acquire_response_code = extop_result;
  266. switch (extop_result)
  267. {
  268. /* XXXggood handle other error codes here */
  269. case NSDS50_REPL_INTERNAL_ERROR:
  270. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  271. "acquire_replica - "
  272. "%s: Unable to acquire replica: "
  273. "an internal error occurred on the remote replica. "
  274. "Replication is aborting.\n",
  275. agmt_get_long_name(prp->agmt));
  276. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  277. "Failed to acquire replica: "
  278. "Internal error occurred on the remote replica");
  279. return_value = ACQUIRE_FATAL_ERROR;
  280. break;
  281. case NSDS50_REPL_PERMISSION_DENIED:
  282. /* Not allowed to send updates */
  283. {
  284. char *repl_binddn = agmt_get_binddn(prp->agmt);
  285. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  286. "acquire_replica - "
  287. "%s: Unable to acquire replica: permission denied. "
  288. "The bind dn \"%s\" does not have permission to "
  289. "supply replication updates to the replica. "
  290. "Will retry later.\n",
  291. agmt_get_long_name(prp->agmt), repl_binddn);
  292. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  293. "Unable to acquire replica: permission denied. "
  294. "The bind dn does not have permission to "
  295. "supply replication updates to the replica. "
  296. "Will retry later.");
  297. slapi_ch_free((void **)&repl_binddn);
  298. return_value = ACQUIRE_TRANSIENT_ERROR;
  299. break;
  300. }
  301. case NSDS50_REPL_NO_SUCH_REPLICA:
  302. /* There is no such replica on the consumer */
  303. {
  304. Slapi_DN *repl_root = agmt_get_replarea(prp->agmt);
  305. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  306. "acquire_replica - "
  307. "%s: Unable to acquire replica: there is no "
  308. "replicated area \"%s\" on the consumer server. "
  309. "Replication is aborting.\n",
  310. agmt_get_long_name(prp->agmt),
  311. slapi_sdn_get_dn(repl_root));
  312. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  313. "Unable to acquire replica: there is no "
  314. "replicated area on the consumer server. "
  315. "Replication is aborting.");
  316. slapi_sdn_free(&repl_root);
  317. return_value = ACQUIRE_FATAL_ERROR;
  318. break;
  319. }
  320. case NSDS50_REPL_EXCESSIVE_CLOCK_SKEW:
  321. /* Large clock skew between the consumer and the supplier */
  322. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  323. "acquire_replica - "
  324. "%s: Unable to acquire replica: "
  325. "Excessive clock skew between the supplier and "
  326. "the consumer. Replication is aborting.\n",
  327. agmt_get_long_name(prp->agmt));
  328. return_value = ACQUIRE_FATAL_ERROR;
  329. break;
  330. case NSDS50_REPL_DECODING_ERROR:
  331. /* We sent something the replica couldn't understand. */
  332. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  333. "acquire_replica - "
  334. "%s: Unable to acquire replica: "
  335. "the consumer was unable to decode the "
  336. "startReplicationRequest extended operation sent by the "
  337. "supplier. Replication is aborting.\n",
  338. agmt_get_long_name(prp->agmt));
  339. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  340. "Unable to acquire replica: "
  341. "the consumer was unable to decode the "
  342. "startReplicationRequest extended operation sent "
  343. "by the supplier. Replication is aborting.");
  344. return_value = ACQUIRE_FATAL_ERROR;
  345. break;
  346. case NSDS50_REPL_REPLICA_BUSY:
  347. /* Someone else is updating the replica. Try later. */
  348. /* if acquire_replica is called for replica
  349. initialization, log REPLICA_BUSY, too */
  350. if (strcmp(REPL_NSDS50_TOTAL_PROTOCOL_OID,
  351. prot_oid) == 0)
  352. {
  353. slapi_log_err(SLAPI_LOG_NOTICE, repl_plugin_name,
  354. "acquire_replica - "
  355. "%s: Unable to acquire replica: "
  356. "the replica is currently being updated"
  357. "by another supplier.\n",
  358. agmt_get_long_name(prp->agmt));
  359. }
  360. else /* REPL_NSDS50_INCREMENTAL_PROTOCOL_OID */
  361. {
  362. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  363. "acquire_replica - "
  364. "%s: Unable to acquire replica: "
  365. "the replica is currently being updated"
  366. "by another supplier. Will try later\n",
  367. agmt_get_long_name(prp->agmt));
  368. }
  369. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  370. "Unable to acquire replica: "
  371. "the replica is currently being updated by another "
  372. "supplier.");
  373. return_value = ACQUIRE_REPLICA_BUSY;
  374. break;
  375. case NSDS50_REPL_LEGACY_CONSUMER:
  376. /* remote replica is a legacy consumer */
  377. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  378. "acquire_replica - "
  379. "%s: Unable to acquire replica: the replica "
  380. "is supplied by a legacy supplier. "
  381. "Replication is aborting.\n", agmt_get_long_name(prp->agmt));
  382. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  383. "Unable to acquire replica: the replica is supplied "
  384. "by a legacy supplier. Replication is aborting.");
  385. return_value = ACQUIRE_FATAL_ERROR;
  386. break;
  387. case NSDS50_REPL_REPLICAID_ERROR:
  388. /* remote replica detected a duplicate ReplicaID */
  389. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  390. "acquire_replica - "
  391. "%s: Unable to aquire replica: the replica "
  392. "has the same Replica ID as this one. "
  393. "Replication is aborting.\n",
  394. agmt_get_long_name(prp->agmt));
  395. agmt_set_last_update_status(prp->agmt, 0, 0,
  396. "Unable to aquire replica: the replica has the same "
  397. "Replica ID as this one. Replication is aborting.");
  398. return_value = ACQUIRE_FATAL_ERROR;
  399. break;
  400. case NSDS50_REPL_BACKOFF:
  401. /* A replication sesssion hook on the replica
  402. * wants us to go into backoff mode. */
  403. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  404. "acquire_replica - "
  405. "%s: Unable to acquire replica: "
  406. "the replica instructed us to go into "
  407. "backoff mode. Will retry later.\n",
  408. agmt_get_long_name(prp->agmt));
  409. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  410. "Unable to acquire replica: the replica instructed "
  411. "us to go into backoff mode. Will retry later.");
  412. return_value = ACQUIRE_TRANSIENT_ERROR;
  413. break;
  414. case NSDS50_REPL_REPLICA_READY:
  415. /* Call any registered replication session post
  416. * acquire callback if we are dealing with a 9.0
  417. * style replica. We want to bail on sending
  418. * updates if the return value is non-0. */
  419. if (prp->repl90consumer)
  420. {
  421. int is_total = 0;
  422. /* Check if this is a total or incremental update. */
  423. if (strcmp(REPL_NSDS50_TOTAL_PROTOCOL_OID, prot_oid) == 0)
  424. {
  425. is_total = 1;
  426. }
  427. if (repl_session_plugin_call_post_acquire_cb(prp->agmt, is_total, data_guid, data))
  428. {
  429. slapi_ch_free_string(&data_guid);
  430. ber_bvfree(data);
  431. data = NULL;
  432. return_value = ACQUIRE_TRANSIENT_ERROR;
  433. break;
  434. }
  435. slapi_ch_free_string(&data_guid);
  436. ber_bvfree(data);
  437. data = NULL;
  438. }
  439. /* We've acquired the replica. */
  440. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  441. "acquire_replica - "
  442. "%s: Replica was successfully acquired.\n",
  443. agmt_get_long_name(prp->agmt));
  444. /* Parse the update vector */
  445. if (NULL != ruv_bervals && NULL != ruv)
  446. {
  447. if (ruv_init_from_bervals(ruv_bervals, ruv) != RUV_SUCCESS)
  448. {
  449. /* Couldn't parse the update vector */
  450. *ruv = NULL;
  451. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  452. "acquire_replica - "
  453. "%s: acquired replica, but could not parse update vector. "
  454. "The replica must be reinitialized.\n",
  455. agmt_get_long_name(prp->agmt));
  456. }
  457. }
  458. /* Save consumer's RUV in the replication agreement.
  459. It is used by the changelog trimming code */
  460. if (ruv && *ruv)
  461. agmt_set_consumer_ruv (prp->agmt, *ruv);
  462. return_value = ACQUIRE_SUCCESS;
  463. break;
  464. default:
  465. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  466. "Unable to acquire replica");
  467. return_value = ACQUIRE_FATAL_ERROR;
  468. }
  469. }
  470. else
  471. {
  472. /* Couldn't parse the response */
  473. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  474. "acquire_replica - "
  475. "%s: Unable to parse the response to the "
  476. "startReplication extended operation. "
  477. "Replication is aborting.\n",
  478. agmt_get_long_name(prp->agmt));
  479. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_DECODING_ERROR,
  480. "Unable to parse the response to the "
  481. "startReplication extended operation. "
  482. "Replication is aborting.");
  483. prp->last_acquire_response_code = NSDS50_REPL_INTERNAL_ERROR;
  484. return_value = ACQUIRE_FATAL_ERROR;
  485. }
  486. }
  487. else
  488. {
  489. int operation, error;
  490. conn_get_error(conn, &operation, &error);
  491. /* Couldn't send the extended operation */
  492. return_value = ACQUIRE_TRANSIENT_ERROR; /* XXX right return value? */
  493. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name,
  494. "acquire_replica - "
  495. "%s: Unable to receive the response for a startReplication "
  496. "extended operation to consumer (%s). Will retry later.\n",
  497. agmt_get_long_name(prp->agmt),
  498. error ? ldap_err2string(error) : "unknown error");
  499. agmt_set_last_update_status(prp->agmt, error, NSDS50_REPL_CONN_ERROR,
  500. "Unable to receive the response for a startReplication "
  501. "extended operation to consumer. Will retry later.");
  502. }
  503. }
  504. else
  505. {
  506. /* Couldn't get a current CSN */
  507. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  508. "acquire_replica - "
  509. "%s: Unable to obtain current CSN. "
  510. "Replication is aborting.\n",
  511. agmt_get_long_name(prp->agmt));
  512. agmt_set_last_update_status(prp->agmt, 0, 0,
  513. "Unable to obtain current CSN. "
  514. "Replication is aborting.");
  515. return_value = ACQUIRE_FATAL_ERROR;
  516. }
  517. }
  518. error:
  519. csn_free(&current_csn);
  520. if (NULL != ruv_bervals)
  521. ber_bvecfree(ruv_bervals);
  522. if (NULL != replarea_sdn)
  523. slapi_sdn_free(&replarea_sdn);
  524. if (NULL != retoid)
  525. ldap_memfree(retoid);
  526. if (NULL != retdata)
  527. ber_bvfree(retdata);
  528. if (ACQUIRE_SUCCESS != return_value)
  529. {
  530. /* could not acquire the replica, so reinstate the linger timer, since this
  531. means we won't call release_replica, which also reinstates the timer */
  532. conn_start_linger(conn);
  533. }
  534. else
  535. {
  536. /* replica successfully acquired */
  537. prp->replica_acquired = PR_TRUE;
  538. }
  539. return return_value;
  540. }
  541. /*
  542. * Release a replica by sending an "end replication" extended request.
  543. */
  544. void
  545. release_replica(Private_Repl_Protocol *prp)
  546. {
  547. int rc;
  548. struct berval *retdata = NULL;
  549. char *retoid = NULL;
  550. struct berval *payload = NULL;
  551. Slapi_DN *replarea_sdn = NULL;
  552. int sent_message_id = 0;
  553. int ret_message_id = 0;
  554. ConnResult conres = 0;
  555. PR_ASSERT(NULL != prp);
  556. PR_ASSERT(NULL != prp->conn);
  557. if (!prp->replica_acquired) {
  558. return;
  559. }
  560. replarea_sdn = agmt_get_replarea(prp->agmt);
  561. payload = NSDS50EndReplicationRequest_new((char *)slapi_sdn_get_dn(replarea_sdn)); /* XXXggood had to cast away const */
  562. slapi_sdn_free(&replarea_sdn);
  563. rc = conn_send_extended_operation(prp->conn,
  564. REPL_END_NSDS50_REPLICATION_REQUEST_OID, payload, NULL /* update control */, &sent_message_id /* Message ID */);
  565. ber_bvfree(payload); /* done with this - free it now */
  566. if (0 != rc)
  567. {
  568. int operation, error;
  569. conn_get_error(prp->conn, &operation, &error);
  570. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  571. "release_replica - %s: Unable to send endReplication extended operation (%s)\n",
  572. agmt_get_long_name(prp->agmt),
  573. error ? ldap_err2string(error) : "unknown error");
  574. goto error;
  575. }
  576. /* Since the operation request is async, we need to wait for the response here */
  577. conres = conn_read_result_ex(prp->conn,&retoid,&retdata,NULL,sent_message_id,&ret_message_id,1);
  578. if (CONN_OPERATION_SUCCESS != conres)
  579. {
  580. int operation, error;
  581. conn_get_error(prp->conn, &operation, &error);
  582. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  583. "release_replica - %s: Attempting to release replica, but unable to receive endReplication extended "
  584. "operation response from the replica. Error %d (%s)\n", agmt_get_long_name(prp->agmt), error,
  585. error ? ldap_err2string(error) : "unknown error");
  586. }
  587. else
  588. {
  589. struct berval **ruv_bervals = NULL; /* Shouldn't actually be returned */
  590. int extop_result;
  591. int extop_rc = 0;
  592. char *data_guid = NULL;
  593. struct berval *data = NULL;
  594. /* Check the message id's match */
  595. if (sent_message_id != ret_message_id)
  596. {
  597. int operation, error;
  598. conn_get_error(prp->conn, &operation, &error);
  599. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  600. "release_replica - %s: Response message id does not match the request (%s)\n",
  601. agmt_get_long_name(prp->agmt),
  602. error ? ldap_err2string(error) : "unknown error");
  603. }
  604. /* We need to pass data_guid and data in even though they
  605. * are not used here. We will free them anyway in case they
  606. * are used in the future. */
  607. extop_rc = decode_repl_ext_response(retdata, &extop_result,
  608. (struct berval ***)&ruv_bervals, &data_guid, &data);
  609. slapi_ch_free_string(&data_guid);
  610. ber_bvfree(data);
  611. data = NULL;
  612. if (0 == extop_rc)
  613. {
  614. if (NSDS50_REPL_REPLICA_RELEASE_SUCCEEDED == extop_result)
  615. {
  616. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  617. "release_replica - %s: Successfully released consumer\n", agmt_get_long_name(prp->agmt));
  618. }
  619. else
  620. {
  621. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  622. "release_replica - %s: Unable to release consumer: response code %d\n",
  623. agmt_get_long_name(prp->agmt), extop_result);
  624. /* disconnect from the consumer so that it does not stay locked */
  625. conn_disconnect (prp->conn);
  626. }
  627. }
  628. else
  629. {
  630. /* Couldn't parse the response */
  631. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  632. "release_replica - %s: Unable to parse the response "
  633. " to the endReplication extended operation.\n",
  634. agmt_get_long_name(prp->agmt));
  635. }
  636. if (NULL != ruv_bervals)
  637. ber_bvecfree(ruv_bervals);
  638. /* XXXggood free ruv_bervals if we got them for some reason */
  639. }
  640. if (NULL != retoid)
  641. ldap_memfree(retoid);
  642. if (NULL != retdata)
  643. ber_bvfree(retdata);
  644. /* replica is released, start the linger timer on the connection, which
  645. was stopped in acquire_replica */
  646. conn_start_linger(prp->conn);
  647. error:
  648. prp->replica_acquired = PR_FALSE;
  649. }
  650. /* converts consumer's response to a string */
  651. char *
  652. protocol_response2string (int response)
  653. {
  654. switch (response)
  655. {
  656. case NSDS50_REPL_REPLICA_READY: return "replica acquired";
  657. case NSDS50_REPL_REPLICA_BUSY: return "replica busy";
  658. case NSDS50_REPL_EXCESSIVE_CLOCK_SKEW: return "excessive clock skew";
  659. case NSDS50_REPL_PERMISSION_DENIED: return "permission denied";
  660. case NSDS50_REPL_DECODING_ERROR: return "decoding error";
  661. case NSDS50_REPL_UNKNOWN_UPDATE_PROTOCOL: return "unknown update protocol";
  662. case NSDS50_REPL_NO_SUCH_REPLICA: return "no such replica";
  663. case NSDS50_REPL_BELOW_PURGEPOINT: return "csn below purge point";
  664. case NSDS50_REPL_INTERNAL_ERROR: return "internal error";
  665. case NSDS50_REPL_REPLICA_RELEASE_SUCCEEDED: return "replica released";
  666. case NSDS50_REPL_LEGACY_CONSUMER: return "replica is a legacy consumer";
  667. case NSDS50_REPL_REPLICAID_ERROR: return "duplicate replica ID detected";
  668. case NSDS50_REPL_UPTODATE: return "no change to send";
  669. case NSDS50_REPL_CL_ERROR: return "changelog error";
  670. case NSDS50_REPL_CONN_ERROR: return "connection error";
  671. case NSDS50_REPL_CONN_TIMEOUT: return "connection timeout";
  672. case NSDS50_REPL_TRANSIENT_ERROR: return "transient error";
  673. case NSDS50_REPL_RUV_ERROR: return "RUV error";
  674. default: return "unknown error";
  675. }
  676. }
  677. int
  678. repl5_strip_fractional_mods(Repl_Agmt *agmt, LDAPMod ** mods)
  679. {
  680. char **a;
  681. char **attrs_to_strip;
  682. int retval = 0;
  683. int strip = 1;
  684. int i, j, k;
  685. if (mods == NULL) {
  686. return retval;
  687. }
  688. a = agmt_get_fractional_attrs(agmt);
  689. if (a) {
  690. /* Iterate through the fractional attr list */
  691. for ( i = 0; a[i] != NULL; i++ )
  692. {
  693. for ( j = 0; NULL != mods[ j ]; )
  694. {
  695. /*
  696. * Iterate through the attrs in this mod list.
  697. * If any match the fractional attr then remove the mod.
  698. */
  699. if (0 == slapi_attr_type_cmp(mods[j]->mod_type, a[i], SLAPI_TYPE_CMP_SUBTYPE))
  700. {
  701. /* Adjust value of j, implicit in not incrementing it */
  702. /* Free this mod */
  703. ber_bvecfree(mods[j]->mod_bvalues);
  704. slapi_ch_free((void **)&(mods[j]->mod_type));
  705. slapi_ch_free((void **)&mods[j]);
  706. /* Move down all subsequent mods */
  707. for (k = j; mods[k+1] ; k++)
  708. {
  709. mods[k] = mods[k+1];
  710. }
  711. /* Zero the end of the array */
  712. mods[k] = NULL;
  713. } else {
  714. j++;
  715. }
  716. }
  717. }
  718. /*
  719. * Check if "all" the remaining mods are on attributes we want to strip from the update.
  720. * If all the mods are on attrs_to_strip, then free them.
  721. */
  722. if((attrs_to_strip = agmt_get_attrs_to_strip(agmt)) != NULL){
  723. for(j = 0; mods[j] != NULL; j++)
  724. {
  725. if(slapi_ch_array_utf8_inlist(attrs_to_strip, mods[j]->mod_type) == 0){
  726. /* at least one of the mods is "real", so don't strip anything */
  727. strip = 0;
  728. break;
  729. }
  730. }
  731. if(strip){
  732. /* free the remaining mods */
  733. for(j = 0; mods[j] != NULL; j++)
  734. {
  735. ber_bvecfree(mods[j]->mod_bvalues);
  736. slapi_ch_free((void **)&(mods[j]->mod_type));
  737. slapi_ch_free((void **)&mods[j]);
  738. }
  739. }
  740. }
  741. slapi_ch_array_free(a);
  742. }
  743. return retval;
  744. }