repl5_protocol_util.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  89. "%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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  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_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  271. "%s: Unable to acquire replica: "
  272. "an internal error occurred on the remote replica. "
  273. "Replication is aborting.\n",
  274. agmt_get_long_name(prp->agmt));
  275. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  276. "Failed to acquire replica: "
  277. "Internal error occurred on the remote replica");
  278. return_value = ACQUIRE_FATAL_ERROR;
  279. break;
  280. case NSDS50_REPL_PERMISSION_DENIED:
  281. /* Not allowed to send updates */
  282. {
  283. char *repl_binddn = agmt_get_binddn(prp->agmt);
  284. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  285. "%s: Unable to acquire replica: permission denied. "
  286. "The bind dn \"%s\" does not have permission to "
  287. "supply replication updates to the replica. "
  288. "Will retry later.\n",
  289. agmt_get_long_name(prp->agmt), repl_binddn);
  290. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  291. "Unable to acquire replica: permission denied. "
  292. "The bind dn does not have permission to "
  293. "supply replication updates to the replica. "
  294. "Will retry later.");
  295. slapi_ch_free((void **)&repl_binddn);
  296. return_value = ACQUIRE_TRANSIENT_ERROR;
  297. break;
  298. }
  299. case NSDS50_REPL_NO_SUCH_REPLICA:
  300. /* There is no such replica on the consumer */
  301. {
  302. Slapi_DN *repl_root = agmt_get_replarea(prp->agmt);
  303. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  304. "%s: Unable to acquire replica: there is no "
  305. "replicated area \"%s\" on the consumer server. "
  306. "Replication is aborting.\n",
  307. agmt_get_long_name(prp->agmt),
  308. slapi_sdn_get_dn(repl_root));
  309. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  310. "Unable to acquire replica: there is no "
  311. "replicated area on the consumer server. "
  312. "Replication is aborting.");
  313. slapi_sdn_free(&repl_root);
  314. return_value = ACQUIRE_FATAL_ERROR;
  315. break;
  316. }
  317. case NSDS50_REPL_EXCESSIVE_CLOCK_SKEW:
  318. /* Large clock skew between the consumer and the supplier */
  319. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  320. "%s: Unable to acquire replica: "
  321. "Excessive clock skew between the supplier and "
  322. "the consumer. Replication is aborting.\n",
  323. agmt_get_long_name(prp->agmt));
  324. return_value = ACQUIRE_FATAL_ERROR;
  325. break;
  326. case NSDS50_REPL_DECODING_ERROR:
  327. /* We sent something the replica couldn't understand. */
  328. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  329. "%s: Unable to acquire replica: "
  330. "the consumer was unable to decode the "
  331. "startReplicationRequest extended operation sent by the "
  332. "supplier. Replication is aborting.\n",
  333. agmt_get_long_name(prp->agmt));
  334. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  335. "Unable to acquire replica: "
  336. "the consumer was unable to decode the "
  337. "startReplicationRequest extended operation sent "
  338. "by the supplier. Replication is aborting.");
  339. return_value = ACQUIRE_FATAL_ERROR;
  340. break;
  341. case NSDS50_REPL_REPLICA_BUSY:
  342. /* Someone else is updating the replica. Try later. */
  343. /* if acquire_replica is called for replica
  344. initialization, log REPLICA_BUSY, too */
  345. if (strcmp(REPL_NSDS50_TOTAL_PROTOCOL_OID,
  346. prot_oid) == 0)
  347. {
  348. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  349. "%s: Unable to acquire replica: "
  350. "the replica is currently being updated"
  351. "by another supplier.\n",
  352. agmt_get_long_name(prp->agmt));
  353. }
  354. else /* REPL_NSDS50_INCREMENTAL_PROTOCOL_OID */
  355. {
  356. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  357. "%s: Unable to acquire replica: "
  358. "the replica is currently being updated"
  359. "by another supplier. Will try later\n",
  360. agmt_get_long_name(prp->agmt));
  361. }
  362. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  363. "Unable to acquire replica: "
  364. "the replica is currently being updated by another "
  365. "supplier.");
  366. return_value = ACQUIRE_REPLICA_BUSY;
  367. break;
  368. case NSDS50_REPL_LEGACY_CONSUMER:
  369. /* remote replica is a legacy consumer */
  370. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  371. "%s: Unable to acquire replica: the replica "
  372. "is supplied by a legacy supplier. "
  373. "Replication is aborting.\n", agmt_get_long_name(prp->agmt));
  374. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  375. "Unable to acquire replica: the replica is supplied "
  376. "by a legacy supplier. Replication is aborting.");
  377. return_value = ACQUIRE_FATAL_ERROR;
  378. break;
  379. case NSDS50_REPL_REPLICAID_ERROR:
  380. /* remote replica detected a duplicate ReplicaID */
  381. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  382. "%s: Unable to aquire replica: the replica "
  383. "has the same Replica ID as this one. "
  384. "Replication is aborting.\n",
  385. agmt_get_long_name(prp->agmt));
  386. agmt_set_last_update_status(prp->agmt, 0, 0,
  387. "Unable to aquire replica: the replica has the same "
  388. "Replica ID as this one. Replication is aborting.");
  389. return_value = ACQUIRE_FATAL_ERROR;
  390. break;
  391. case NSDS50_REPL_BACKOFF:
  392. /* A replication sesssion hook on the replica
  393. * wants us to go into backoff mode. */
  394. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  395. "%s: Unable to acquire replica: "
  396. "the replica instructed us to go into "
  397. "backoff mode. Will retry later.\n",
  398. agmt_get_long_name(prp->agmt));
  399. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  400. "Unable to acquire replica: the replica instructed "
  401. "us to go into backoff mode. Will retry later.");
  402. return_value = ACQUIRE_TRANSIENT_ERROR;
  403. break;
  404. case NSDS50_REPL_REPLICA_READY:
  405. /* Call any registered replication session post
  406. * acquire callback if we are dealing with a 9.0
  407. * style replica. We want to bail on sending
  408. * updates if the return value is non-0. */
  409. if (prp->repl90consumer)
  410. {
  411. int is_total = 0;
  412. /* Check if this is a total or incremental update. */
  413. if (strcmp(REPL_NSDS50_TOTAL_PROTOCOL_OID, prot_oid) == 0)
  414. {
  415. is_total = 1;
  416. }
  417. if (repl_session_plugin_call_post_acquire_cb(prp->agmt, is_total, data_guid, data))
  418. {
  419. slapi_ch_free_string(&data_guid);
  420. ber_bvfree(data);
  421. data = NULL;
  422. return_value = ACQUIRE_TRANSIENT_ERROR;
  423. break;
  424. }
  425. slapi_ch_free_string(&data_guid);
  426. ber_bvfree(data);
  427. data = NULL;
  428. }
  429. /* We've acquired the replica. */
  430. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  431. "%s: Replica was successfully acquired.\n",
  432. agmt_get_long_name(prp->agmt));
  433. /* Parse the update vector */
  434. if (NULL != ruv_bervals && NULL != ruv)
  435. {
  436. if (ruv_init_from_bervals(ruv_bervals, ruv) != RUV_SUCCESS)
  437. {
  438. /* Couldn't parse the update vector */
  439. *ruv = NULL;
  440. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  441. "%s: Warning: acquired replica, "
  442. "but could not parse update vector. "
  443. "The replica must be reinitialized.\n",
  444. agmt_get_long_name(prp->agmt));
  445. }
  446. }
  447. /* Save consumer's RUV in the replication agreement.
  448. It is used by the changelog trimming code */
  449. if (ruv && *ruv)
  450. agmt_set_consumer_ruv (prp->agmt, *ruv);
  451. return_value = ACQUIRE_SUCCESS;
  452. break;
  453. default:
  454. agmt_set_last_update_status(prp->agmt, 0, extop_result,
  455. "Unable to acquire replica");
  456. return_value = ACQUIRE_FATAL_ERROR;
  457. }
  458. }
  459. else
  460. {
  461. /* Couldn't parse the response */
  462. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  463. "%s: Unable to parse the response to the "
  464. "startReplication extended operation. "
  465. "Replication is aborting.\n",
  466. agmt_get_long_name(prp->agmt));
  467. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_DECODING_ERROR,
  468. "Unable to parse the response to the "
  469. "startReplication extended operation. "
  470. "Replication is aborting.");
  471. prp->last_acquire_response_code = NSDS50_REPL_INTERNAL_ERROR;
  472. return_value = ACQUIRE_FATAL_ERROR;
  473. }
  474. }
  475. else
  476. {
  477. int operation, error;
  478. conn_get_error(conn, &operation, &error);
  479. /* Couldn't send the extended operation */
  480. return_value = ACQUIRE_TRANSIENT_ERROR; /* XXX right return value? */
  481. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  482. "%s: Unable to receive the response for a startReplication "
  483. "extended operation to consumer (%s). Will retry later.\n",
  484. agmt_get_long_name(prp->agmt),
  485. error ? ldap_err2string(error) : "unknown error");
  486. agmt_set_last_update_status(prp->agmt, error, NSDS50_REPL_CONN_ERROR,
  487. "Unable to receive the response for a startReplication "
  488. "extended operation to consumer. Will retry later.");
  489. }
  490. }
  491. else
  492. {
  493. /* Couldn't get a current CSN */
  494. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  495. "%s: Unable to obtain current CSN. "
  496. "Replication is aborting.\n",
  497. agmt_get_long_name(prp->agmt));
  498. agmt_set_last_update_status(prp->agmt, 0, 0,
  499. "Unable to obtain current CSN. "
  500. "Replication is aborting.");
  501. return_value = ACQUIRE_FATAL_ERROR;
  502. }
  503. }
  504. error:
  505. csn_free(&current_csn);
  506. if (NULL != ruv_bervals)
  507. ber_bvecfree(ruv_bervals);
  508. if (NULL != replarea_sdn)
  509. slapi_sdn_free(&replarea_sdn);
  510. if (NULL != retoid)
  511. ldap_memfree(retoid);
  512. if (NULL != retdata)
  513. ber_bvfree(retdata);
  514. if (ACQUIRE_SUCCESS != return_value)
  515. {
  516. /* could not acquire the replica, so reinstate the linger timer, since this
  517. means we won't call release_replica, which also reinstates the timer */
  518. conn_start_linger(conn);
  519. }
  520. else
  521. {
  522. /* replica successfully acquired */
  523. prp->replica_acquired = PR_TRUE;
  524. }
  525. return return_value;
  526. }
  527. /*
  528. * Release a replica by sending an "end replication" extended request.
  529. */
  530. void
  531. release_replica(Private_Repl_Protocol *prp)
  532. {
  533. int rc;
  534. struct berval *retdata = NULL;
  535. char *retoid = NULL;
  536. struct berval *payload = NULL;
  537. Slapi_DN *replarea_sdn = NULL;
  538. int sent_message_id = 0;
  539. int ret_message_id = 0;
  540. ConnResult conres = 0;
  541. PR_ASSERT(NULL != prp);
  542. PR_ASSERT(NULL != prp->conn);
  543. if (!prp->replica_acquired) {
  544. return;
  545. }
  546. replarea_sdn = agmt_get_replarea(prp->agmt);
  547. payload = NSDS50EndReplicationRequest_new((char *)slapi_sdn_get_dn(replarea_sdn)); /* XXXggood had to cast away const */
  548. slapi_sdn_free(&replarea_sdn);
  549. rc = conn_send_extended_operation(prp->conn,
  550. REPL_END_NSDS50_REPLICATION_REQUEST_OID, payload, NULL /* update control */, &sent_message_id /* Message ID */);
  551. ber_bvfree(payload); /* done with this - free it now */
  552. if (0 != rc)
  553. {
  554. int operation, error;
  555. conn_get_error(prp->conn, &operation, &error);
  556. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  557. "%s: Warning: unable to send endReplication extended operation (%s)\n",
  558. agmt_get_long_name(prp->agmt),
  559. error ? ldap_err2string(error) : "unknown error");
  560. goto error;
  561. }
  562. /* Since the operation request is async, we need to wait for the response here */
  563. conres = conn_read_result_ex(prp->conn,&retoid,&retdata,NULL,sent_message_id,&ret_message_id,1);
  564. if (CONN_OPERATION_SUCCESS != conres)
  565. {
  566. int operation, error;
  567. conn_get_error(prp->conn, &operation, &error);
  568. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  569. "%s: Warning: Attempting to release replica, but unable to receive endReplication extended "
  570. "operation response from the replica. Error %d (%s)\n", agmt_get_long_name(prp->agmt), error,
  571. error ? ldap_err2string(error) : "unknown error");
  572. }
  573. else
  574. {
  575. struct berval **ruv_bervals = NULL; /* Shouldn't actually be returned */
  576. int extop_result;
  577. int extop_rc = 0;
  578. char *data_guid = NULL;
  579. struct berval *data = NULL;
  580. /* Check the message id's match */
  581. if (sent_message_id != ret_message_id)
  582. {
  583. int operation, error;
  584. conn_get_error(prp->conn, &operation, &error);
  585. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  586. "%s: Warning: response message id does not match the request (%s)\n",
  587. agmt_get_long_name(prp->agmt),
  588. error ? ldap_err2string(error) : "unknown error");
  589. }
  590. /* We need to pass data_guid and data in even though they
  591. * are not used here. We will free them anyway in case they
  592. * are used in the future. */
  593. extop_rc = decode_repl_ext_response(retdata, &extop_result,
  594. (struct berval ***)&ruv_bervals, &data_guid, &data);
  595. slapi_ch_free_string(&data_guid);
  596. ber_bvfree(data);
  597. data = NULL;
  598. if (0 == extop_rc)
  599. {
  600. if (NSDS50_REPL_REPLICA_RELEASE_SUCCEEDED == extop_result)
  601. {
  602. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  603. "%s: Successfully released consumer\n", agmt_get_long_name(prp->agmt));
  604. }
  605. else
  606. {
  607. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  608. "%s: Unable to release consumer: response code %d\n",
  609. agmt_get_long_name(prp->agmt), extop_result);
  610. /* disconnect from the consumer so that it does not stay locked */
  611. conn_disconnect (prp->conn);
  612. }
  613. }
  614. else
  615. {
  616. /* Couldn't parse the response */
  617. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  618. "%s: Warning: Unable to parse the response "
  619. " to the endReplication extended operation.\n",
  620. agmt_get_long_name(prp->agmt));
  621. }
  622. if (NULL != ruv_bervals)
  623. ber_bvecfree(ruv_bervals);
  624. /* XXXggood free ruv_bervals if we got them for some reason */
  625. }
  626. if (NULL != retoid)
  627. ldap_memfree(retoid);
  628. if (NULL != retdata)
  629. ber_bvfree(retdata);
  630. /* replica is released, start the linger timer on the connection, which
  631. was stopped in acquire_replica */
  632. conn_start_linger(prp->conn);
  633. error:
  634. prp->replica_acquired = PR_FALSE;
  635. }
  636. /* converts consumer's response to a string */
  637. char *
  638. protocol_response2string (int response)
  639. {
  640. switch (response)
  641. {
  642. case NSDS50_REPL_REPLICA_READY: return "replica acquired";
  643. case NSDS50_REPL_REPLICA_BUSY: return "replica busy";
  644. case NSDS50_REPL_EXCESSIVE_CLOCK_SKEW: return "excessive clock skew";
  645. case NSDS50_REPL_PERMISSION_DENIED: return "permission denied";
  646. case NSDS50_REPL_DECODING_ERROR: return "decoding error";
  647. case NSDS50_REPL_UNKNOWN_UPDATE_PROTOCOL: return "unknown update protocol";
  648. case NSDS50_REPL_NO_SUCH_REPLICA: return "no such replica";
  649. case NSDS50_REPL_BELOW_PURGEPOINT: return "csn below purge point";
  650. case NSDS50_REPL_INTERNAL_ERROR: return "internal error";
  651. case NSDS50_REPL_REPLICA_RELEASE_SUCCEEDED: return "replica released";
  652. case NSDS50_REPL_LEGACY_CONSUMER: return "replica is a legacy consumer";
  653. case NSDS50_REPL_REPLICAID_ERROR: return "duplicate replica ID detected";
  654. case NSDS50_REPL_UPTODATE: return "no change to send";
  655. case NSDS50_REPL_CL_ERROR: return "changelog error";
  656. case NSDS50_REPL_CONN_ERROR: return "connection error";
  657. case NSDS50_REPL_CONN_TIMEOUT: return "connection timeout";
  658. case NSDS50_REPL_TRANSIENT_ERROR: return "transient error";
  659. case NSDS50_REPL_RUV_ERROR: return "RUV error";
  660. default: return "unknown error";
  661. }
  662. }
  663. int
  664. repl5_strip_fractional_mods(Repl_Agmt *agmt, LDAPMod ** mods)
  665. {
  666. char **a;
  667. char **attrs_to_strip;
  668. int retval = 0;
  669. int strip = 1;
  670. int i, j, k;
  671. if (mods == NULL) {
  672. return retval;
  673. }
  674. a = agmt_get_fractional_attrs(agmt);
  675. if (a) {
  676. /* Iterate through the fractional attr list */
  677. for ( i = 0; a[i] != NULL; i++ )
  678. {
  679. for ( j = 0; NULL != mods[ j ]; )
  680. {
  681. /*
  682. * Iterate through the attrs in this mod list.
  683. * If any match the fractional attr then remove the mod.
  684. */
  685. if (0 == slapi_attr_type_cmp(mods[j]->mod_type, a[i], SLAPI_TYPE_CMP_SUBTYPE))
  686. {
  687. /* Adjust value of j, implicit in not incrementing it */
  688. /* Free this mod */
  689. ber_bvecfree(mods[j]->mod_bvalues);
  690. slapi_ch_free((void **)&(mods[j]->mod_type));
  691. slapi_ch_free((void **)&mods[j]);
  692. /* Move down all subsequent mods */
  693. for (k = j; mods[k+1] ; k++)
  694. {
  695. mods[k] = mods[k+1];
  696. }
  697. /* Zero the end of the array */
  698. mods[k] = NULL;
  699. } else {
  700. j++;
  701. }
  702. }
  703. }
  704. /*
  705. * Check if "all" the remaining mods are on attributes we want to strip from the update.
  706. * If all the mods are on attrs_to_strip, then free them.
  707. */
  708. if((attrs_to_strip = agmt_get_attrs_to_strip(agmt)) != NULL){
  709. for(j = 0; mods[j] != NULL; j++)
  710. {
  711. if(slapi_ch_array_utf8_inlist(attrs_to_strip, mods[j]->mod_type) == 0){
  712. /* at least one of the mods is "real", so don't strip anything */
  713. strip = 0;
  714. break;
  715. }
  716. }
  717. if(strip){
  718. /* free the remaining mods */
  719. for(j = 0; mods[j] != NULL; j++)
  720. {
  721. ber_bvecfree(mods[j]->mod_bvalues);
  722. slapi_ch_free((void **)&(mods[j]->mod_type));
  723. slapi_ch_free((void **)&mods[j]);
  724. }
  725. }
  726. }
  727. slapi_ch_array_free(a);
  728. }
  729. return retval;
  730. }