1
0

repl5_protocol_util.c 24 KB

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