repl5_total.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. /*
  39. repl5_total.c - code that implements a total replica update.
  40. The requestValue of the NSDS50ReplicationEntry looks like this:
  41. requestValue ::= SEQUENCE {
  42. uniqueid OCTET STRING,
  43. dn LDAPDN,
  44. annotatedAttributes AnnotatedAttributeList
  45. }
  46. AnnotatedAttributeList ::= SET OF SEQUENCE {
  47. attributeType AttributeDescription,
  48. attributeDeletionCSN OCTET STRING OPTIONAL,
  49. attributeDeleted BOOLEAN DEFAULT FALSE,
  50. annotatedValues SET OF AnnotatedValue
  51. }
  52. AnnotatedValue ::= SEQUENCE {
  53. value AttributeValue,
  54. valueDeleted BOOLEAN DEFAULT FALSE,
  55. valueCSNSet SEQUENCE OF ValueCSN,
  56. }
  57. ValueCSN ::= SEQUENCE {
  58. CSNType ENUMERATED {
  59. valuePresenceCSN (1),
  60. valueDeletionCSN (2),
  61. valueDistinguishedCSN (3)
  62. }
  63. CSN OCTET STRING,
  64. }
  65. */
  66. #include "repl5.h"
  67. #define CSN_TYPE_VALUE_UPDATED_ON_WIRE 1
  68. #define CSN_TYPE_VALUE_DELETED_ON_WIRE 2
  69. #define CSN_TYPE_VALUE_DISTINGUISHED_ON_WIRE 3
  70. /* #define GORDONS_PATENTED_BER_DEBUG 1 */
  71. #ifdef GORDONS_PATENTED_BER_DEBUG
  72. #define BER_DEBUG(a) printf(a)
  73. #else
  74. #define BER_DEBUG(a)
  75. #endif
  76. /* Forward declarations */
  77. static int my_ber_printf_csn(BerElement *ber, const CSN *csn, const CSNType t);
  78. static int my_ber_printf_value(BerElement *ber, const char *type,
  79. const Slapi_Value *value, PRBool deleted);
  80. static int my_ber_printf_attr (BerElement *ber, Slapi_Attr *attr, PRBool deleted);
  81. static int my_ber_scanf_attr (BerElement *ber, Slapi_Attr **attr, PRBool *deleted);
  82. static int my_ber_scanf_value(BerElement *ber, Slapi_Value **value, PRBool *deleted);
  83. /*
  84. * Get a Slapi_Entry ready to send over the wire as part of
  85. * a total update protocol stream. Convert the entry and all
  86. * of its state information to a BerElement which will be the
  87. * payload of an extended LDAP operation.
  88. *
  89. * Entries consist of:
  90. * - An entry DN
  91. * - A uniqueID
  92. * - A set of present attributes, each of which consists of:
  93. * - A set of present values, each of which consists of:
  94. * - A value
  95. * - A set of CSNs
  96. * - A set of deleted values, each of which consists of:
  97. * - A value
  98. * - A set of CSNs
  99. * - A set of deleted attibutes, each of which consists of:
  100. * - An attribute type
  101. * - A set of CSNs. Note that this list of CSNs will always contain exactly one CSN.
  102. *
  103. * This all gets mashed into one BerElement, ready to be blasted over the wire to
  104. * a replica.
  105. *
  106. */
  107. BerElement *
  108. entry2bere(const Slapi_Entry *e, char **excluded_attrs)
  109. {
  110. BerElement *ber = NULL;
  111. const char *str = NULL;
  112. const char *dnstr = NULL;
  113. char *type;
  114. Slapi_DN *sdn = NULL;
  115. Slapi_Attr *attr = NULL, *prev_attr;
  116. int rc;
  117. PR_ASSERT(NULL != e);
  118. if ((ber = ber_alloc()) == NULL)
  119. {
  120. goto loser;
  121. }
  122. BER_DEBUG("{");
  123. if (ber_printf(ber, "{") == -1) /* Begin outer sequence */
  124. {
  125. goto loser;
  126. }
  127. /* Get the entry's uniqueid */
  128. if ((str = slapi_entry_get_uniqueid(e)) == NULL)
  129. {
  130. goto loser;
  131. }
  132. BER_DEBUG("s(uniqueid)");
  133. if (ber_printf(ber, "s", str) == -1)
  134. {
  135. goto loser;
  136. }
  137. /* Get the entry's DN */
  138. if ((sdn = slapi_entry_get_sdn((Slapi_Entry *)e)) == NULL) /* XXXggood had to cast away const */
  139. {
  140. goto loser;
  141. }
  142. if ((dnstr = slapi_sdn_get_dn(sdn)) == NULL)
  143. {
  144. goto loser;
  145. }
  146. BER_DEBUG("s(dn)");
  147. if (ber_printf(ber, "s", dnstr) == -1)
  148. {
  149. goto loser;
  150. }
  151. /* Next comes the annoted list of the entry's attributes */
  152. BER_DEBUG("[");
  153. if (ber_printf(ber, "[") == -1) /* Begin set of attributes */
  154. {
  155. goto loser;
  156. }
  157. /*
  158. * We iterate over all of the non-deleted attributes first.
  159. */
  160. slapi_entry_first_attr(e, &attr);
  161. while (NULL != attr)
  162. {
  163. /* ONREPL - skip uniqueid attribute since we already sent uniqueid
  164. This is a hack; need to figure a better way of storing uniqueid
  165. in an entry */
  166. slapi_attr_get_type (attr, &type);
  167. if (strcasecmp (type, SLAPI_ATTR_UNIQUEID) != 0)
  168. {
  169. /* Check to see if this attribute is excluded by the fractional list */
  170. if ( (NULL == excluded_attrs) || !charray_inlist(excluded_attrs,type))
  171. {
  172. /* Process this attribute */
  173. rc = my_ber_printf_attr (ber, attr, PR_FALSE);
  174. if (rc != 0)
  175. {
  176. goto loser;
  177. }
  178. }
  179. }
  180. prev_attr = attr;
  181. slapi_entry_next_attr(e, prev_attr, &attr);
  182. }
  183. /*
  184. * Now iterate over the deleted attributes.
  185. */
  186. entry_first_deleted_attribute(e, &attr);
  187. while (attr != NULL)
  188. {
  189. slapi_attr_get_type (attr, &type);
  190. /* Check to see if this attribute is excluded by the fractional list */
  191. if ( (NULL == excluded_attrs) || !charray_inlist(excluded_attrs,type))
  192. {
  193. /* Process this attribute */
  194. rc = my_ber_printf_attr (ber, attr, PR_TRUE);
  195. if (rc != 0)
  196. {
  197. goto loser;
  198. }
  199. }
  200. entry_next_deleted_attribute(e, &attr);
  201. }
  202. BER_DEBUG("]");
  203. if (ber_printf(ber, "]") == -1) /* End set for attributes */
  204. {
  205. goto loser;
  206. }
  207. BER_DEBUG("}");
  208. if (ber_printf(ber, "}") == -1) /* End sequence for this entry */
  209. {
  210. goto loser;
  211. }
  212. /* If we get here, everything went ok */
  213. BER_DEBUG("\n");
  214. goto free_and_return;
  215. loser:
  216. if (NULL != ber)
  217. {
  218. ber_free(ber, 1);
  219. ber = NULL;
  220. }
  221. free_and_return:
  222. return ber;
  223. }
  224. /*
  225. * Helper function - convert a CSN to a string and ber_printf() it.
  226. */
  227. static int
  228. my_ber_printf_csn(BerElement *ber, const CSN *csn, const CSNType t)
  229. {
  230. char csn_str[CSN_STRSIZE];
  231. unsigned long len;
  232. int rc = -1;
  233. int csn_type_as_ber = -1;
  234. switch (t)
  235. {
  236. case CSN_TYPE_VALUE_UPDATED:
  237. csn_type_as_ber = CSN_TYPE_VALUE_UPDATED_ON_WIRE;
  238. break;
  239. case CSN_TYPE_VALUE_DELETED:
  240. csn_type_as_ber = CSN_TYPE_VALUE_DELETED_ON_WIRE;
  241. break;
  242. case CSN_TYPE_VALUE_DISTINGUISHED:
  243. csn_type_as_ber = CSN_TYPE_VALUE_DISTINGUISHED_ON_WIRE;
  244. break;
  245. case CSN_TYPE_ATTRIBUTE_DELETED:
  246. break;
  247. default:
  248. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "my_ber_printf_csn: unknown "
  249. "csn type %d encountered.\n", (int)t);
  250. return -1;
  251. }
  252. csn_as_string(csn,PR_FALSE,csn_str);
  253. /* we don't send type for attr csn since there is only one */
  254. if (t == CSN_TYPE_ATTRIBUTE_DELETED)
  255. {
  256. rc = ber_printf(ber, "s", csn_str);
  257. BER_DEBUG("s(csn_str)");
  258. }
  259. else
  260. {
  261. len = CSN_STRSIZE;
  262. rc = ber_printf(ber, "{es}", csn_type_as_ber, csn_str);
  263. BER_DEBUG("{e(csn type)s(csn)}");
  264. }
  265. return rc;
  266. }
  267. /*
  268. * Send a single annotated attribute value.
  269. */
  270. static int
  271. my_ber_printf_value(BerElement *ber, const char *type, const Slapi_Value *value, PRBool deleted)
  272. {
  273. const struct berval *bval = NULL;
  274. int rc = -1;
  275. const CSNSet *csnset;
  276. void *cookie;
  277. CSN *csn;
  278. CSNType t;
  279. bval = slapi_value_get_berval(value);
  280. BER_DEBUG("{o(value)");
  281. if (ber_printf(ber, "{o", bval->bv_val, bval->bv_len) == -1) /* Start sequence */
  282. {
  283. goto done;
  284. }
  285. /* if (ber_printf(ber, "o", bval->bv_val, bval->bv_len) == -1)
  286. {
  287. goto done;
  288. } */
  289. if (deleted)
  290. {
  291. BER_DEBUG("b(deleted flag)");
  292. if (ber_printf (ber, "b", PR_TRUE) == -1)
  293. {
  294. goto done;
  295. }
  296. }
  297. /* Send value CSN list */
  298. BER_DEBUG("{");
  299. if (ber_printf(ber, "{") == -1) /* Start set */
  300. {
  301. goto done;
  302. }
  303. /* Iterate over the sequence of CSNs. */
  304. csnset = value_get_csnset (value);
  305. if (csnset)
  306. {
  307. for (cookie = csnset_get_first_csn (csnset, &csn, &t); NULL != cookie;
  308. cookie = csnset_get_next_csn (csnset, cookie, &csn, &t))
  309. {
  310. /* Don't send any adcsns, since that was already sent */
  311. if (t != CSN_TYPE_ATTRIBUTE_DELETED)
  312. {
  313. if (my_ber_printf_csn(ber, csn, t) == -1)
  314. {
  315. goto done;
  316. }
  317. }
  318. }
  319. }
  320. BER_DEBUG("}");
  321. if (ber_printf(ber, "}") == -1) /* End CSN sequence */
  322. {
  323. goto done;
  324. }
  325. BER_DEBUG("}");
  326. if (ber_printf(ber, "}") == -1) /* End sequence */
  327. {
  328. goto done;
  329. }
  330. /* Everything's ok */
  331. rc = 0;
  332. done:
  333. return rc;
  334. }
  335. /* send a single attribute */
  336. static int
  337. my_ber_printf_attr (BerElement *ber, Slapi_Attr *attr, PRBool deleted)
  338. {
  339. Slapi_Value *value;
  340. char *type;
  341. int i;
  342. const CSN *csn;
  343. /* First, send the type */
  344. slapi_attr_get_type(attr, &type);
  345. BER_DEBUG("{s(type ");
  346. BER_DEBUG(type);
  347. BER_DEBUG(")");
  348. if (ber_printf(ber, "{s", type) == -1) /* Begin sequence for this type */
  349. {
  350. goto loser;
  351. }
  352. /* Send the attribute deletion CSN if present */
  353. csn = attr_get_deletion_csn(attr);
  354. if (csn)
  355. {
  356. if (my_ber_printf_csn(ber, csn, CSN_TYPE_ATTRIBUTE_DELETED) == -1)
  357. {
  358. goto loser;
  359. }
  360. }
  361. /* only send "is deleted" flag for deleted attributes since it defaults to false */
  362. if (deleted)
  363. {
  364. BER_DEBUG("b(del flag)");
  365. if (ber_printf (ber, "b", PR_TRUE) == -1)
  366. {
  367. goto loser;
  368. }
  369. }
  370. /*
  371. * Iterate through all the values.
  372. */
  373. BER_DEBUG("[");
  374. if (ber_printf(ber, "[") == -1) /* Begin set */
  375. {
  376. goto loser;
  377. }
  378. /*
  379. * Process the non-deleted values first.
  380. */
  381. i = slapi_attr_first_value(attr, &value);
  382. while (i != -1)
  383. {
  384. if (my_ber_printf_value(ber, type, value, PR_FALSE) == -1)
  385. {
  386. goto loser;
  387. }
  388. i= slapi_attr_next_value(attr, i, &value);
  389. }
  390. /*
  391. * Now iterate over all of the deleted values.
  392. */
  393. i= attr_first_deleted_value(attr, &value);
  394. while (i != -1)
  395. {
  396. if (my_ber_printf_value(ber, type, value, PR_TRUE) == -1)
  397. {
  398. goto loser;
  399. }
  400. i= attr_next_deleted_value(attr, i, &value);
  401. }
  402. BER_DEBUG("]");
  403. if (ber_printf(ber, "]") == -1) /* End set */
  404. {
  405. goto loser;
  406. }
  407. BER_DEBUG("}");
  408. if (ber_printf(ber, "}") == -1) /* End sequence for this type */
  409. {
  410. goto loser;
  411. }
  412. return 0;
  413. loser:
  414. return -1;
  415. }
  416. /*
  417. * Get an annotated value from the BerElement. Returns 0 on
  418. * success, -1 on failure.
  419. */
  420. static int
  421. my_ber_scanf_value(BerElement *ber, Slapi_Value **value, PRBool *deleted)
  422. {
  423. struct berval *attrval = NULL;
  424. ber_len_t len;
  425. ber_tag_t tag;
  426. CSN *csn = NULL;
  427. char csnstring[CSN_STRSIZE + 1];
  428. CSNType csntype;
  429. char *lasti;
  430. PR_ASSERT(ber && value && deleted);
  431. *value = NULL;
  432. if (NULL == ber && NULL == value)
  433. {
  434. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "my_ber_scanf_value BAD 1\n");
  435. goto loser;
  436. }
  437. /* Each value is a sequence */
  438. if (ber_scanf(ber, "{O", &attrval) == LBER_ERROR)
  439. {
  440. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "my_ber_scanf_value BAD 2\n");
  441. goto loser;
  442. }
  443. /* Allocate and fill in the attribute value */
  444. if ((*value = slapi_value_new_berval(attrval)) == NULL)
  445. {
  446. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "my_ber_scanf_value BAD 3\n");
  447. goto loser;
  448. }
  449. /* check if this is a deleted value */
  450. if (ber_peek_tag(ber, &len) == LBER_BOOLEAN)
  451. {
  452. if (ber_scanf(ber, "b", deleted) == LBER_ERROR)
  453. {
  454. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "my_ber_scanf_value BAD 4\n");
  455. goto loser;
  456. }
  457. }
  458. else /* default is present value */
  459. {
  460. *deleted = PR_FALSE;
  461. }
  462. /* Read the sequence of CSNs */
  463. for (tag = ber_first_element(ber, &len, &lasti);
  464. tag != LBER_ERROR && tag != LBER_END_OF_SEQORSET;
  465. tag = ber_next_element(ber, &len, lasti))
  466. {
  467. ber_int_t csntype_tmp;
  468. /* Each CSN is in a sequence that includes a csntype and CSN */
  469. len = CSN_STRSIZE;
  470. if (ber_scanf(ber, "{es}", &csntype_tmp, csnstring, &len) == LBER_ERROR)
  471. {
  472. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "my_ber_scanf_value BAD 7 - bval is %s\n", attrval->bv_val);
  473. goto loser;
  474. }
  475. switch (csntype_tmp)
  476. {
  477. case CSN_TYPE_VALUE_UPDATED_ON_WIRE:
  478. csntype = CSN_TYPE_VALUE_UPDATED;
  479. break;
  480. case CSN_TYPE_VALUE_DELETED_ON_WIRE:
  481. csntype = CSN_TYPE_VALUE_DELETED;
  482. break;
  483. case CSN_TYPE_VALUE_DISTINGUISHED_ON_WIRE:
  484. csntype = CSN_TYPE_VALUE_DISTINGUISHED;
  485. break;
  486. default:
  487. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Error: preposterous CSN type "
  488. "%d received during total update.\n", csntype_tmp);
  489. goto loser;
  490. }
  491. csn = csn_new_by_string(csnstring);
  492. if (csn == NULL)
  493. {
  494. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "my_ber_scanf_value BAD 8\n");
  495. goto loser;
  496. }
  497. value_add_csn(*value, csntype, csn);
  498. csn_free (&csn);
  499. }
  500. if (ber_scanf(ber, "}") == LBER_ERROR) /* End of annotated attribute value seq */
  501. {
  502. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "my_ber_scanf_value BAD 10\n");
  503. goto loser;
  504. }
  505. if (attrval)
  506. ber_bvfree(attrval);
  507. return 0;
  508. loser:
  509. /* Free any stuff we allocated */
  510. if (csn)
  511. csn_free (&csn);
  512. if (attrval)
  513. ber_bvfree(attrval);
  514. if (value)
  515. {
  516. slapi_value_free (value);
  517. }
  518. return -1;
  519. }
  520. static int
  521. my_ber_scanf_attr (BerElement *ber, Slapi_Attr **attr, PRBool *deleted)
  522. {
  523. char *attrtype = NULL;
  524. CSN *attr_deletion_csn = NULL;
  525. PRBool val_deleted;
  526. char *lasti;
  527. ber_len_t len;
  528. ber_tag_t tag;
  529. char *str = NULL;
  530. int rc;
  531. Slapi_Value *value;
  532. PR_ASSERT (ber && attr && deleted);
  533. /* allocate the attribute */
  534. *attr = slapi_attr_new ();
  535. if (attr == NULL)
  536. {
  537. goto loser;
  538. }
  539. if (ber_scanf(ber, "{a", &attrtype) == LBER_ERROR) /* Begin sequence for this attr */
  540. {
  541. goto loser;
  542. }
  543. slapi_attr_init(*attr, attrtype);
  544. slapi_ch_free ((void **)&attrtype);
  545. /* The attribute deletion CSN is next and is optional? */
  546. if (ber_peek_tag(ber, &len) == LBER_OCTETSTRING)
  547. {
  548. if (ber_scanf(ber, "a", &str) == LBER_ERROR)
  549. {
  550. goto loser;
  551. }
  552. attr_deletion_csn = csn_new_by_string(str);
  553. slapi_ch_free((void **)&str);
  554. }
  555. if (attr_deletion_csn)
  556. {
  557. rc = attr_set_deletion_csn(*attr, attr_deletion_csn);
  558. csn_free (&attr_deletion_csn);
  559. if (rc != 0)
  560. {
  561. goto loser;
  562. }
  563. }
  564. /* The "attribute deleted" flag is next, and is optional */
  565. if (ber_peek_tag(ber, &len) == LBER_BOOLEAN)
  566. {
  567. if (ber_scanf(ber, "b", deleted) == -1)
  568. {
  569. goto loser;
  570. }
  571. }
  572. else /* default is present */
  573. {
  574. *deleted = PR_FALSE;
  575. }
  576. /* loop over the list of attribute values */
  577. for (tag = ber_first_element(ber, &len, &lasti);
  578. tag != LBER_ERROR && tag != LBER_END_OF_SEQORSET;
  579. tag = ber_next_element(ber, &len, lasti))
  580. {
  581. value = NULL;
  582. if (my_ber_scanf_value(ber, &value, &val_deleted) == -1)
  583. {
  584. goto loser;
  585. }
  586. if (val_deleted)
  587. {
  588. /* Add the value to the attribute */
  589. if (attr_add_deleted_value(*attr, value) == -1) /* attr has ownership of value */
  590. {
  591. goto loser;
  592. }
  593. }
  594. else
  595. {
  596. /* Add the value to the attribute */
  597. if (slapi_attr_add_value(*attr, value) == -1) /* attr has ownership of value */
  598. {
  599. goto loser;
  600. }
  601. }
  602. if (value)
  603. slapi_value_free(&value);
  604. }
  605. if (ber_scanf(ber, "}") == LBER_ERROR) /* End sequence for this attribute */
  606. {
  607. goto loser;
  608. }
  609. return 0;
  610. loser:
  611. if (*attr)
  612. slapi_attr_free (attr);
  613. if (value)
  614. slapi_value_free (&value);
  615. slapi_ch_free_string(&attrtype);
  616. slapi_ch_free_string(&str);
  617. return -1;
  618. }
  619. /*
  620. * Extract the payload from a total update extended operation,
  621. * decode it, and produce a Slapi_Entry structure representing a new
  622. * entry to be added to the local database.
  623. */
  624. static int
  625. decode_total_update_extop(Slapi_PBlock *pb, Slapi_Entry **ep)
  626. {
  627. BerElement *tmp_bere = NULL;
  628. Slapi_Entry *e = NULL;
  629. Slapi_Attr *attr = NULL;
  630. char *str = NULL;
  631. CSN *dn_csn = NULL;
  632. struct berval *extop_value = NULL;
  633. char *extop_oid = NULL;
  634. ber_len_t len;
  635. char *lasto;
  636. ber_tag_t tag;
  637. int rc;
  638. PRBool deleted;
  639. PR_ASSERT(NULL != pb);
  640. PR_ASSERT(NULL != ep);
  641. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &extop_oid);
  642. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &extop_value);
  643. if (NULL == extop_oid ||
  644. ((strcmp(extop_oid, REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID) != 0) &&
  645. (strcmp(extop_oid, REPL_NSDS71_REPLICATION_ENTRY_REQUEST_OID) != 0)) ||
  646. NULL == extop_value)
  647. {
  648. /* Bogus */
  649. goto loser;
  650. }
  651. if ((tmp_bere = ber_init(extop_value)) == NULL)
  652. {
  653. goto loser;
  654. }
  655. if ((e = slapi_entry_alloc()) == NULL)
  656. {
  657. goto loser;
  658. }
  659. if (ber_scanf(tmp_bere, "{") == LBER_ERROR) /* Begin outer sequence */
  660. {
  661. goto loser;
  662. }
  663. /* The entry's uniqueid is first */
  664. if (ber_scanf(tmp_bere, "a", &str) == LBER_ERROR)
  665. {
  666. goto loser;
  667. }
  668. slapi_entry_set_uniqueid(e, str);
  669. str = NULL; /* Slapi_Entry now owns the uniqueid */
  670. /* The entry's DN is next */
  671. if (ber_scanf(tmp_bere, "a", &str) == LBER_ERROR)
  672. {
  673. goto loser;
  674. }
  675. slapi_entry_set_dn(e, str);
  676. str = NULL; /* Slapi_Entry now owns the dn */
  677. /* Get the attributes */
  678. for ( tag = ber_first_element( tmp_bere, &len, &lasto );
  679. tag != LBER_ERROR && tag != LBER_END_OF_SEQORSET;
  680. tag = ber_next_element( tmp_bere, &len, lasto ) )
  681. {
  682. if (my_ber_scanf_attr (tmp_bere, &attr, &deleted) != 0)
  683. {
  684. goto loser;
  685. }
  686. /* Add the attribute to the entry */
  687. if (deleted)
  688. entry_add_deleted_attribute_wsi(e, attr); /* entry now owns attr */
  689. else
  690. entry_add_present_attribute_wsi(e, attr); /* entry now owns attr */
  691. attr = NULL;
  692. }
  693. if (ber_scanf(tmp_bere, "}") == LBER_ERROR) /* End sequence for this entry */
  694. {
  695. goto loser;
  696. }
  697. /* Check for ldapsubentries and tombstone entries to set flags properly */
  698. slapi_entry_attr_find(e, "objectclass", &attr);
  699. if (attr != NULL) {
  700. struct berval bv;
  701. bv.bv_val = "ldapsubentry";
  702. bv.bv_len = strlen(bv.bv_val);
  703. if (slapi_attr_value_find(attr, &bv) == 0) {
  704. slapi_entry_set_flag(e, SLAPI_ENTRY_LDAPSUBENTRY);
  705. }
  706. bv.bv_val = SLAPI_ATTR_VALUE_TOMBSTONE;
  707. bv.bv_len = strlen(bv.bv_val);
  708. if (slapi_attr_value_find(attr, &bv) == 0) {
  709. slapi_entry_set_flag(e, SLAPI_ENTRY_FLAG_TOMBSTONE);
  710. }
  711. }
  712. /* If we get here, the entry is properly constructed. Return it. */
  713. rc = 0;
  714. *ep = e;
  715. goto free_and_return;
  716. loser:
  717. rc = -1;
  718. /* slapi_ch_free accepts NULL pointer */
  719. slapi_ch_free((void **)&str);
  720. if (NULL != dn_csn)
  721. {
  722. csn_free(&dn_csn);
  723. }
  724. if (attr != NULL)
  725. {
  726. slapi_attr_free (&attr);
  727. }
  728. if (NULL != e)
  729. {
  730. slapi_entry_free (e);
  731. }
  732. *ep = NULL;
  733. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Error: could not decode extended "
  734. "operation containing entry for total update.\n");
  735. free_and_return:
  736. if (NULL != tmp_bere)
  737. {
  738. ber_free(tmp_bere, 1);
  739. tmp_bere = NULL;
  740. }
  741. return rc;
  742. }
  743. /*
  744. * This plugin entry point is called whenever an NSDS50ReplicationEntry
  745. * extended operation is received.
  746. */
  747. int
  748. multimaster_extop_NSDS50ReplicationEntry(Slapi_PBlock *pb)
  749. {
  750. int rc;
  751. Slapi_Entry *e = NULL;
  752. Slapi_Connection *conn = NULL;
  753. int connid, opid;
  754. connid = 0;
  755. slapi_pblock_get(pb, SLAPI_CONN_ID, &connid);
  756. opid = 0;
  757. slapi_pblock_get(pb, SLAPI_OPERATION_ID, &opid);
  758. /* Decode the extended operation */
  759. rc = decode_total_update_extop(pb, &e);
  760. if (0 == rc)
  761. {
  762. #ifdef notdef
  763. /*
  764. * Just spew LDIF so we're sure we got it right. Later we'll firehose
  765. * this into the database import code
  766. */
  767. int len;
  768. char *str = slapi_entry2str_with_options(e, &len,SLAPI_DUMP_UNIQUEID);
  769. puts(str);
  770. free(str);
  771. #endif
  772. rc = slapi_import_entry (pb, e);
  773. /* slapi_import_entry return an LDAP error in case of problem
  774. * LDAP_BUSY is used to indicate that the import queue is full
  775. * and that flow control must happen to stop the supplier
  776. * from sending entries
  777. */
  778. if ((rc != LDAP_SUCCESS) && (rc != LDAP_BUSY))
  779. {
  780. const char *dn = slapi_entry_get_dn_const(e);
  781. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  782. "Error %d: could not import entry dn %s "
  783. "for total update operation conn=%d op=%d\n",
  784. rc, dn, connid, opid);
  785. rc = -1;
  786. }
  787. }
  788. else
  789. {
  790. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  791. "Error %d: could not decode the total update extop "
  792. "for total update operation conn=%d op=%d\n",
  793. rc, connid, opid);
  794. }
  795. if ((rc != 0) && (rc != LDAP_BUSY))
  796. {
  797. /* just disconnect from the supplier. bulk import is stopped when
  798. connection object is destroyed */
  799. slapi_pblock_get (pb, SLAPI_CONNECTION, &conn);
  800. if (conn)
  801. {
  802. slapi_disconnect_server(conn);
  803. }
  804. /* cleanup */
  805. if (e)
  806. {
  807. slapi_entry_free (e);
  808. }
  809. }
  810. return rc;
  811. }