urp.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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. /*
  42. * urp.c - Update Resolution Procedures
  43. */
  44. #include "slapi-plugin.h"
  45. #include "repl.h"
  46. #include "repl5.h"
  47. #include "urp.h"
  48. extern int slapi_log_urp;
  49. static int urp_add_resolve_parententry (Slapi_PBlock *pb, char *sessionid, Slapi_Entry *entry, Slapi_Entry *parententry, CSN *opcsn);
  50. static int urp_annotate_dn (char *sessionid, Slapi_Entry *entry, CSN *opcsn, const char *optype);
  51. static int urp_naming_conflict_removal (Slapi_PBlock *pb, char *sessionid, CSN *opcsn, const char *optype);
  52. static int mod_namingconflict_attr (const char *uniqueid, const Slapi_DN *entrysdn, const Slapi_DN *conflictsdn, CSN *opcsn);
  53. static int del_replconflict_attr (Slapi_Entry *entry, CSN *opcsn, int opflags);
  54. static char *get_dn_plus_uniqueid(char *sessionid,const char *olddn,const char *uniqueid);
  55. static char *get_rdn_plus_uniqueid(char *sessionid,const char *olddn,const char *uniqueid);
  56. static int is_suffix_entry (Slapi_PBlock *pb, Slapi_Entry *entry, Slapi_DN **parenddn);
  57. /*
  58. * Return 0 for OK, -1 for Error.
  59. */
  60. int
  61. urp_modify_operation( Slapi_PBlock *pb )
  62. {
  63. Slapi_Entry *modifyentry= NULL;
  64. int op_result= 0;
  65. int rc= 0; /* OK */
  66. if ( slapi_op_abandoned(pb) )
  67. {
  68. return rc;
  69. }
  70. slapi_pblock_get( pb, SLAPI_MODIFY_EXISTING_ENTRY, &modifyentry );
  71. if(modifyentry!=NULL)
  72. {
  73. /*
  74. * The entry to be modified exists.
  75. * - the entry could be a tombstone... but that's OK.
  76. * - the entry could be glue... that may not be OK. JCMREPL
  77. */
  78. rc= 0; /* OK, Modify the entry */
  79. PROFILE_POINT; /* Modify Conflict; Entry Exists; Apply Modification */
  80. }
  81. else
  82. {
  83. /*
  84. * The entry to be modified could not be found.
  85. */
  86. op_result= LDAP_NO_SUCH_OBJECT;
  87. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  88. rc= -1; /* Must discard this Modification */
  89. PROFILE_POINT; /* Modify Conflict; Entry Does Not Exist; Discard Modification */
  90. }
  91. return rc;
  92. }
  93. /*
  94. * Return 0 for OK,
  95. * -1 for Ignore or Error depending on SLAPI_RESULT_CODE,
  96. * >0 for action code
  97. * Action Code Bit 0: Fetch existing entry.
  98. * Action Code Bit 1: Fetch parent entry.
  99. * The function is called as a be pre-op on consumers.
  100. */
  101. int
  102. urp_add_operation( Slapi_PBlock *pb )
  103. {
  104. Slapi_Entry *existing_uniqueid_entry;
  105. Slapi_Entry *existing_dn_entry;
  106. Slapi_Entry *addentry;
  107. const char *adduniqueid;
  108. CSN *opcsn;
  109. const char *basedn;
  110. char sessionid[REPL_SESSION_ID_SIZE];
  111. int r;
  112. int op_result= 0;
  113. int rc= 0; /* OK */
  114. Slapi_DN *sdn = NULL;
  115. if ( slapi_op_abandoned(pb) )
  116. {
  117. return rc;
  118. }
  119. slapi_pblock_get( pb, SLAPI_ADD_EXISTING_UNIQUEID_ENTRY, &existing_uniqueid_entry );
  120. if (existing_uniqueid_entry!=NULL)
  121. {
  122. /*
  123. * An entry with this uniqueid already exists.
  124. * - It could be a replay of the same Add, or
  125. * - It could be a UUID generation collision, or
  126. */
  127. /*
  128. * This operation won't be replayed. That is, this CSN won't update
  129. * the max csn in RUV. The CSN is left uncommitted in RUV unless an
  130. * error is set to op_result. Just to get rid of this CSN from RUV,
  131. * setting an error to op_result
  132. */
  133. /* op_result = LDAP_SUCCESS; */
  134. slapi_log_error(slapi_log_urp, sessionid,
  135. "urp_add (%s): an entry with this uniqueid already exists.\n",
  136. slapi_entry_get_dn_const(existing_uniqueid_entry));
  137. op_result= LDAP_UNWILLING_TO_PERFORM;
  138. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  139. rc= -1; /* Ignore this Operation */
  140. PROFILE_POINT; /* Add Conflict; UniqueID Exists; Ignore */
  141. goto bailout;
  142. }
  143. get_repl_session_id (pb, sessionid, &opcsn);
  144. slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &addentry );
  145. slapi_pblock_get( pb, SLAPI_ADD_EXISTING_DN_ENTRY, &existing_dn_entry );
  146. if (existing_dn_entry==NULL) /* The target DN does not exist */
  147. {
  148. /* Check for parent entry... this could be an orphan. */
  149. Slapi_Entry *parententry;
  150. slapi_pblock_get( pb, SLAPI_ADD_PARENT_ENTRY, &parententry );
  151. rc = urp_add_resolve_parententry (pb, sessionid, addentry, parententry, opcsn);
  152. PROFILE_POINT; /* Add Entry */
  153. goto bailout;
  154. }
  155. /*
  156. * Naming conflict: an entry with the target DN already exists.
  157. * Compare the DistinguishedNameCSN of the existing entry
  158. * and the OperationCSN. The smaller CSN wins. The loser changes
  159. * its RDN to uniqueid+baserdn, and adds operational attribute
  160. * ATTR_NSDS5_REPLCONFLIC.
  161. */
  162. basedn = slapi_entry_get_ndn (addentry);
  163. adduniqueid = slapi_entry_get_uniqueid (addentry);
  164. r = csn_compare (entry_get_dncsn(existing_dn_entry), opcsn);
  165. if (r<0)
  166. {
  167. /* Entry to be added is a loser */
  168. char *newdn= get_dn_plus_uniqueid (sessionid, basedn, adduniqueid);
  169. if(newdn==NULL)
  170. {
  171. op_result= LDAP_OPERATIONS_ERROR;
  172. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  173. rc= -1; /* Abort this Operation */
  174. PROFILE_POINT; /* Add Conflict; Entry Exists; Unique ID already in RDN - Abort this update. */
  175. }
  176. else
  177. {
  178. /* Add the nsds5ReplConflict attribute in the mods */
  179. Slapi_Attr *attr = NULL;
  180. Slapi_Value **vals = NULL;
  181. Slapi_RDN *rdn;
  182. char buf[BUFSIZ];
  183. PR_snprintf(buf, BUFSIZ, "%s %s", REASON_ANNOTATE_DN, basedn);
  184. if (slapi_entry_attr_find (addentry, ATTR_NSDS5_REPLCONFLICT, &attr) == 0)
  185. {
  186. /* ATTR_NSDS5_REPLCONFLICT exists */
  187. slapi_log_error (SLAPI_LOG_FATAL, sessionid, "New entry has nsds5ReplConflict already\n");
  188. vals = attr_get_present_values (attr); /* this returns a pointer to the contents */
  189. }
  190. if ( vals == NULL || *vals == NULL )
  191. {
  192. /* Add new attribute */
  193. slapi_entry_add_string (addentry, ATTR_NSDS5_REPLCONFLICT, buf);
  194. }
  195. else
  196. {
  197. /*
  198. * Replace old attribute. We don't worry about the index
  199. * change here since the entry is yet to be added.
  200. */
  201. slapi_value_set_string (*vals, buf);
  202. }
  203. /* slapi_pblock_get(pb, SLAPI_ADD_TARGET, &dn); */
  204. slapi_pblock_get(pb, SLAPI_ADD_TARGET_SDN, &sdn);
  205. slapi_sdn_free(&sdn);
  206. slapi_entry_set_normdn(addentry, newdn); /* dn: passin */
  207. sdn = slapi_sdn_dup(slapi_entry_get_sdn_const(addentry));
  208. slapi_pblock_set(pb, SLAPI_ADD_TARGET_SDN, sdn);
  209. rdn = slapi_rdn_new_sdn ( slapi_entry_get_sdn_const(addentry) );
  210. slapi_log_error (slapi_log_urp, sessionid,
  211. "Naming conflict ADD. Add %s instead\n", slapi_rdn_get_rdn(rdn) );
  212. slapi_rdn_free(&rdn);
  213. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
  214. PROFILE_POINT; /* Add Conflict; Entry Exists; Rename Operation Entry */
  215. }
  216. }
  217. else if(r>0)
  218. {
  219. /* Existing entry is a loser */
  220. if (!urp_annotate_dn(sessionid, existing_dn_entry, opcsn, "ADD"))
  221. {
  222. op_result= LDAP_OPERATIONS_ERROR;
  223. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  224. rc= -1; /* Ignore this Operation */
  225. }
  226. else
  227. {
  228. /* The backend add code should now search for the existing entry again. */
  229. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
  230. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY);
  231. }
  232. PROFILE_POINT; /* Add Conflict; Entry Exists; Rename Existing Entry */
  233. }
  234. else /* r==0 */
  235. {
  236. /* The CSN of the Operation and the Entry DN are the same.
  237. * This could only happen if:
  238. * a) There are two replicas with the same ReplicaID.
  239. * b) We've seen the Operation before.
  240. * Let's go with (b) and ignore the little bastard.
  241. */
  242. /*
  243. * This operation won't be replayed. That is, this CSN won't update
  244. * the max csn in RUV. The CSN is left uncommitted in RUV unless an
  245. * error is set to op_result. Just to get rid of this CSN from RUV,
  246. * setting an error to op_result
  247. */
  248. /* op_result = LDAP_SUCCESS; */
  249. slapi_log_error(slapi_log_urp, sessionid,
  250. "urp_add (%s): The CSN of the Operation and the Entry DN are the same.",
  251. slapi_entry_get_dn_const(existing_dn_entry));
  252. op_result= LDAP_UNWILLING_TO_PERFORM;
  253. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  254. rc= -1; /* Ignore this Operation */
  255. PROFILE_POINT; /* Add Conflict; Entry Exists; Same CSN */
  256. }
  257. bailout:
  258. return rc;
  259. }
  260. /*
  261. * Return 0 for OK, -1 for Error, >0 for action code
  262. * Action Code Bit 0: Fetch existing entry.
  263. * Action Code Bit 1: Fetch parent entry.
  264. */
  265. int
  266. urp_modrdn_operation( Slapi_PBlock *pb )
  267. {
  268. slapi_operation_parameters *op_params = NULL;
  269. Slapi_Entry *parent_entry;
  270. Slapi_Entry *new_parent_entry;
  271. Slapi_DN *newsuperior = NULL;
  272. Slapi_DN *parentdn = NULL;
  273. Slapi_Entry *target_entry;
  274. Slapi_Entry *existing_entry;
  275. const CSN *target_entry_dncsn;
  276. CSN *opcsn= NULL;
  277. char *op_uniqueid = NULL;
  278. const char *existing_uniqueid = NULL;
  279. const Slapi_DN *target_sdn;
  280. const Slapi_DN *existing_sdn;
  281. char *newrdn;
  282. char sessionid[REPL_SESSION_ID_SIZE];
  283. int r;
  284. int op_result= 0;
  285. int rc= 0; /* OK */
  286. int del_old_replconflict_attr = 0;
  287. if ( slapi_op_abandoned(pb) )
  288. {
  289. return rc;
  290. }
  291. slapi_pblock_get (pb, SLAPI_MODRDN_TARGET_ENTRY, &target_entry);
  292. if(target_entry==NULL)
  293. {
  294. /* An entry can't be found for the Unique Identifier */
  295. op_result= LDAP_NO_SUCH_OBJECT;
  296. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  297. rc= -1; /* No entry to modrdn */
  298. PROFILE_POINT; /* ModRDN Conflict; Entry does not Exist; Discard ModRDN */
  299. goto bailout;
  300. }
  301. get_repl_session_id (pb, sessionid, &opcsn);
  302. target_entry_dncsn = entry_get_dncsn (target_entry);
  303. if ( csn_compare (target_entry_dncsn, opcsn) >= 0 )
  304. {
  305. /*
  306. * The Operation CSN is not newer than the DN CSN.
  307. * Either we're beaten by another ModRDN or we've applied the op.
  308. */
  309. /* op_result= LDAP_SUCCESS; */
  310. /*
  311. * This operation won't be replayed. That is, this CSN won't update
  312. * the max csn in RUV. The CSN is left uncommitted in RUV unless an
  313. * error is set to op_result. Just to get rid of this CSN from RUV,
  314. * setting an error to op_result
  315. */
  316. slapi_log_error(slapi_log_urp, sessionid,
  317. "urp_modrdn (%s): operation CSN is newer than the DN CSN.\n",
  318. slapi_entry_get_dn_const(target_entry));
  319. op_result= LDAP_UNWILLING_TO_PERFORM;
  320. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  321. rc= -1; /* Ignore the modrdn */
  322. PROFILE_POINT; /* ModRDN Conflict; Entry with Target DN Exists; OPCSN is not newer. */
  323. goto bailout;
  324. }
  325. /* The DN CSN is older than the Operation CSN. Apply the operation */
  326. target_sdn = slapi_entry_get_sdn_const (target_entry);
  327. /* newrdn is no need to be case-ignored (get_rdn_plus_uniqueid) */
  328. slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &newrdn);
  329. slapi_pblock_get(pb, SLAPI_TARGET_UNIQUEID, &op_uniqueid);
  330. slapi_pblock_get(pb, SLAPI_MODRDN_PARENT_ENTRY, &parent_entry);
  331. slapi_pblock_get(pb, SLAPI_MODRDN_NEWPARENT_ENTRY, &new_parent_entry);
  332. slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &newsuperior);
  333. if ( is_tombstone_entry (target_entry) )
  334. {
  335. /*
  336. * It is a non-trivial task to rename a tombstone.
  337. * This op has been ignored so far by
  338. * setting SLAPI_RESULT_CODE to LDAP_NO_SUCH_OBJECT
  339. * and rc to -1.
  340. */
  341. /* Turn the tombstone to glue before rename it */
  342. /*
  343. op_result = tombstone_to_glue (pb, sessionid, target_entry,
  344. slapi_entry_get_sdn (target_entry), "renameTombstone", opcsn);
  345. */
  346. op_result = LDAP_NO_SUCH_OBJECT;
  347. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  348. if (op_result == 0)
  349. {
  350. /*
  351. * Remember to turn this entry back to tombstone in post op.
  352. * We'll just borrow an obsolete pblock type here.
  353. */
  354. slapi_pblock_set (pb, SLAPI_URP_TOMBSTONE_UNIQUEID, slapi_ch_strdup(op_uniqueid));
  355. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_TARGET_ENTRY);
  356. rc = 0;
  357. }
  358. else
  359. {
  360. rc = -1;
  361. }
  362. PROFILE_POINT; /* ModRDN Conflict; Entry with Target DN Exists; OPCSN is not newer. */
  363. goto bailout;
  364. }
  365. slapi_pblock_get(pb, SLAPI_MODRDN_EXISTING_ENTRY, &existing_entry);
  366. if(existing_entry!=NULL)
  367. {
  368. /*
  369. * An entry with the target DN already exists.
  370. * The smaller dncsn wins. The loser changes its RDN to
  371. * uniqueid+baserdn, and adds operational attribute
  372. * ATTR_NSDS5_REPLCONFLIC
  373. */
  374. existing_uniqueid = slapi_entry_get_uniqueid (existing_entry);
  375. existing_sdn = slapi_entry_get_sdn_const ( existing_entry);
  376. /*
  377. * It used to dismiss the operation if the existing entry is
  378. * the same as the target one.
  379. * But renaming the RDN with the one which only cases are different,
  380. * cn=ABC --> cn=Abc, this case matches. We should go forward the op.
  381. */
  382. if (strcmp(op_uniqueid, existing_uniqueid) == 0) {
  383. op_result= LDAP_SUCCESS;
  384. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  385. rc = 0; /* Don't ignore the op */
  386. PROFILE_POINT; /* ModRDN Replay */
  387. goto bailout;
  388. }
  389. r= csn_compare ( entry_get_dncsn (existing_entry), opcsn);
  390. if (r == 0)
  391. {
  392. /*
  393. * The CSN of the Operation and the Entry DN are the same
  394. * but the uniqueids are not.
  395. * There might be two replicas with the same ReplicaID.
  396. */
  397. slapi_log_error(SLAPI_LOG_FATAL, sessionid,
  398. "Duplicated CSN for different uniqueids [%s][%s]",
  399. existing_uniqueid, op_uniqueid);
  400. op_result= LDAP_OPERATIONS_ERROR;
  401. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  402. rc= -1; /* Abort */
  403. PROFILE_POINT; /* ModRDN Conflict; Duplicated CSN for Different Entries */
  404. goto bailout;
  405. }
  406. if(r<0)
  407. {
  408. /* The target entry is a loser */
  409. char *newrdn_with_uniqueid;
  410. newrdn_with_uniqueid= get_rdn_plus_uniqueid (sessionid, newrdn, op_uniqueid);
  411. if(newrdn_with_uniqueid==NULL)
  412. {
  413. op_result= LDAP_OPERATIONS_ERROR;
  414. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  415. rc= -1; /* Ignore this Operation */
  416. PROFILE_POINT; /* ModRDN Conflict; Entry with Target DN Exists;
  417. Unique ID already in RDN - Change to Lost and Found entry */
  418. goto bailout;
  419. }
  420. mod_namingconflict_attr (op_uniqueid, target_sdn, existing_sdn, opcsn);
  421. slapi_pblock_set(pb, SLAPI_MODRDN_NEWRDN, newrdn_with_uniqueid);
  422. slapi_log_error(slapi_log_urp, sessionid,
  423. "Naming conflict MODRDN. Rename target entry to %s\n",
  424. newrdn_with_uniqueid );
  425. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
  426. PROFILE_POINT; /* ModRDN Conflict; Entry with Target DN Exists; Rename Operation Entry */
  427. goto bailout;
  428. }
  429. if ( r>0 )
  430. {
  431. /* The existing entry is a loser */
  432. int resolve = urp_annotate_dn (sessionid, existing_entry, opcsn, "MODRDN");
  433. if(!resolve)
  434. {
  435. op_result= LDAP_OPERATIONS_ERROR;
  436. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  437. rc= -1; /* Abort this Operation */
  438. goto bailout;
  439. }
  440. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
  441. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_NEWPARENT_ENTRY);
  442. if (LDAP_NO_SUCH_OBJECT == resolve) {
  443. /* This means that existing_dn_entry did not really exist!!!
  444. * This indicates that a get_copy_of_entry -> dn2entry returned
  445. * an entry (existing_dn_entry) that was already removed from the ldbm.
  446. * This is bad, because it indicates a dn cache or DB corruption.
  447. * However, as far as the conflict is concerned, this error is harmless:
  448. * if the existing_dn_entry did not exist in the first place, there was no
  449. * conflict!! Return 0 for success to break the ldbm_back_modrdn loop
  450. * and get out of this inexistent conflict resolution ASAP.
  451. */
  452. rc = 0;
  453. }
  454. /* Set flag to remove possible old naming conflict */
  455. del_old_replconflict_attr = 1;
  456. PROFILE_POINT; /* ModRDN Conflict; Entry with Target DN Exists; Rename Entry with Target DN */
  457. goto bailout;
  458. }
  459. }
  460. else
  461. {
  462. /*
  463. * No entry with the target DN exists.
  464. */
  465. /* Set flag to remove possible old naming conflict */
  466. del_old_replconflict_attr = 1;
  467. if(new_parent_entry!=NULL)
  468. {
  469. /* The new superior entry exists */
  470. rc= 0; /* OK, Apply the ModRDN */
  471. PROFILE_POINT; /* ModRDN Conflict; OK */
  472. goto bailout;
  473. }
  474. /* The new superior entry doesn't exist */
  475. slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &newsuperior);
  476. if(newsuperior == NULL)
  477. {
  478. /* (new_parent_entry==NULL && newsuperiordn==NULL)
  479. * This is ok - SLAPI_MODRDN_NEWPARENT_ENTRY will
  480. * only be set if SLAPI_MODRDN_NEWSUPERIOR_SDN was
  481. * suplied by the client. If it wasn't, we're just
  482. * changing the RDN of the entry. In that case,
  483. * if the entry exists, its parent won't change
  484. * when it's renamed, and therefore we can assume
  485. * its parent exists.
  486. */
  487. rc=0;
  488. PROFILE_POINT; /* ModRDN OK */
  489. goto bailout;
  490. }
  491. if((0 == slapi_sdn_compare(slapi_entry_get_sdn(parent_entry),
  492. newsuperior)) ||
  493. is_suffix_dn (pb, newsuperior, &parentdn) )
  494. {
  495. /*
  496. * The new superior is the same as the current one, or
  497. * this entry is a suffix whose parent can be absent.
  498. */
  499. rc= 0; /* OK, Move the entry */
  500. PROFILE_POINT; /* ModRDN Conflict; Absent Target Parent; Create Suffix Entry */
  501. goto bailout;
  502. }
  503. /*
  504. * This entry is not a suffix entry, so the parent entry should exist.
  505. * (This shouldn't happen in a ds5 server)
  506. */
  507. slapi_pblock_get ( pb, SLAPI_OPERATION_PARAMETERS, &op_params );
  508. op_result = create_glue_entry (pb, sessionid, newsuperior,
  509. op_params->p.p_modrdn.modrdn_newsuperior_address.uniqueid, opcsn);
  510. if (LDAP_SUCCESS != op_result)
  511. {
  512. /*
  513. * FATAL ERROR
  514. * We should probably just abort the rename
  515. * this will cause replication divergence requiring
  516. * admin intercession
  517. */
  518. slapi_log_error( SLAPI_LOG_FATAL, sessionid,
  519. "Parent %s couldn't be found, nor recreated as a glue entry\n",
  520. slapi_sdn_get_dn(newsuperior) );
  521. op_result= LDAP_OPERATIONS_ERROR;
  522. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  523. rc = -1;
  524. PROFILE_POINT;
  525. goto bailout;
  526. }
  527. /* The backend add code should now search for the parent again. */
  528. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_NEWPARENT_ENTRY);
  529. PROFILE_POINT; /* ModRDN Conflict; Absent Target Parent - Change to Lost and Found entry */
  530. goto bailout;
  531. }
  532. bailout:
  533. if ( del_old_replconflict_attr && rc == 0 )
  534. {
  535. del_replconflict_attr (target_entry, opcsn, 0);
  536. }
  537. if ( parentdn )
  538. slapi_sdn_free(&parentdn);
  539. return rc;
  540. }
  541. /*
  542. * Return 0 for OK, -1 for Error
  543. */
  544. int
  545. urp_delete_operation( Slapi_PBlock *pb )
  546. {
  547. Slapi_Entry *deleteentry;
  548. CSN *opcsn= NULL;
  549. char sessionid[REPL_SESSION_ID_SIZE];
  550. int op_result= 0;
  551. int rc= 0; /* OK */
  552. if ( slapi_op_abandoned(pb) )
  553. {
  554. return rc;
  555. }
  556. slapi_pblock_get(pb, SLAPI_DELETE_EXISTING_ENTRY, &deleteentry);
  557. if(deleteentry==NULL) /* uniqueid can't be found */
  558. {
  559. op_result= LDAP_NO_SUCH_OBJECT;
  560. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  561. rc= -1; /* Don't apply the Delete */
  562. PROFILE_POINT; /* Delete Operation; Entry not exist. */
  563. }
  564. else if(is_tombstone_entry(deleteentry))
  565. {
  566. /* The entry is already a Tombstone, ignore this delete. */
  567. op_result= LDAP_SUCCESS;
  568. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  569. rc = -1; /* Don't apply the Delete */
  570. PROFILE_POINT; /* Delete Operation; Already a Tombstone. */
  571. }
  572. else /* The entry to be deleted exists and is not a tombstone */
  573. {
  574. get_repl_session_id (pb, sessionid, &opcsn);
  575. /* Check if the entry has children. */
  576. if(!slapi_entry_has_children(deleteentry))
  577. {
  578. /* Remove possible conflict attributes */
  579. del_replconflict_attr (deleteentry, opcsn, 0);
  580. rc= 0; /* OK, to delete the entry */
  581. PROFILE_POINT; /* Delete Operation; OK. */
  582. }
  583. else
  584. {
  585. /* Turn this entry into a glue_absent_parent entry */
  586. entry_to_glue(sessionid, deleteentry, REASON_RESURRECT_ENTRY, opcsn);
  587. /* Turn the Delete into a No-Op */
  588. op_result= LDAP_SUCCESS;
  589. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
  590. rc = -1; /* Don't apply the Delete */
  591. PROFILE_POINT; /* Delete Operation; Entry has children. */
  592. }
  593. }
  594. return rc;
  595. }
  596. int urp_post_modrdn_operation (Slapi_PBlock *pb)
  597. {
  598. CSN *opcsn;
  599. char sessionid[REPL_SESSION_ID_SIZE];
  600. char *tombstone_uniqueid;
  601. Slapi_Entry *postentry;
  602. Slapi_Operation *op;
  603. /*
  604. * Do not abandon the post op - the processed CSN needs to be
  605. * committed to keep the consistency between the changelog
  606. * and the backend DB.
  607. * if ( slapi_op_abandoned(pb) ) return 0;
  608. */
  609. slapi_pblock_get (pb, SLAPI_URP_TOMBSTONE_UNIQUEID, &tombstone_uniqueid );
  610. if (tombstone_uniqueid == NULL)
  611. {
  612. /*
  613. * The entry is not resurrected from tombstone. Hence
  614. * we need to check if any naming conflict with its
  615. * old dn can be resolved.
  616. */
  617. slapi_pblock_get( pb, SLAPI_OPERATION, &op);
  618. if (!operation_is_flag_set(op, OP_FLAG_REPL_FIXUP))
  619. {
  620. get_repl_session_id (pb, sessionid, &opcsn);
  621. urp_naming_conflict_removal (pb, sessionid, opcsn, "MODRDN");
  622. }
  623. }
  624. else
  625. {
  626. /*
  627. * The entry was a resurrected tombstone.
  628. * This could happen when we applied a rename
  629. * to a tombstone to avoid server divergence. Now
  630. * it's time to put the entry back to tombstone.
  631. */
  632. slapi_pblock_get ( pb, SLAPI_ENTRY_POST_OP, &postentry );
  633. if (postentry && strcmp(tombstone_uniqueid, slapi_entry_get_uniqueid(postentry)) == 0)
  634. {
  635. entry_to_tombstone (pb, postentry);
  636. }
  637. slapi_ch_free ((void**)&tombstone_uniqueid);
  638. slapi_pblock_set (pb, SLAPI_URP_TOMBSTONE_UNIQUEID, NULL);
  639. }
  640. return 0;
  641. }
  642. /*
  643. * Conflict removal
  644. */
  645. int
  646. urp_post_delete_operation( Slapi_PBlock *pb )
  647. {
  648. Slapi_Operation *op;
  649. Slapi_Entry *entry;
  650. CSN *opcsn;
  651. char sessionid[REPL_SESSION_ID_SIZE];
  652. int op_result;
  653. /*
  654. * Do not abandon the post op - the processed CSN needs to be
  655. * committed to keep the consistency between the changelog
  656. * and the backend DB
  657. * if ( slapi_op_abandoned(pb) ) return 0;
  658. */
  659. get_repl_session_id (pb, sessionid, &opcsn);
  660. /*
  661. * Conflict removal from the parent entry:
  662. * If the parent is glue and has no more children,
  663. * turn the parent to tombstone
  664. */
  665. slapi_pblock_get ( pb, SLAPI_DELETE_GLUE_PARENT_ENTRY, &entry );
  666. if ( entry != NULL )
  667. {
  668. op_result = entry_to_tombstone ( pb, entry );
  669. if ( op_result == LDAP_SUCCESS )
  670. {
  671. slapi_log_error ( slapi_log_urp, sessionid,
  672. "Tombstoned glue entry %s since it has no more children\n",
  673. slapi_entry_get_dn_const (entry) );
  674. }
  675. }
  676. slapi_pblock_get( pb, SLAPI_OPERATION, &op);
  677. if (!operation_is_flag_set(op, OP_FLAG_REPL_FIXUP))
  678. {
  679. /*
  680. * Conflict removal from the peers of the old dn
  681. */
  682. urp_naming_conflict_removal (pb, sessionid, opcsn, "DEL");
  683. }
  684. return 0;
  685. }
  686. int
  687. urp_fixup_add_entry (Slapi_Entry *e, const char *target_uniqueid, const char *parentuniqueid, CSN *opcsn, int opflags)
  688. {
  689. Slapi_PBlock *newpb;
  690. Slapi_Operation *op;
  691. int op_result;
  692. newpb = slapi_pblock_new ();
  693. /*
  694. * Mark this operation as replicated, so that the front end
  695. * doesn't add extra attributes.
  696. */
  697. slapi_add_entry_internal_set_pb (
  698. newpb,
  699. e, /* entry will be consumed */
  700. NULL, /*Controls*/
  701. repl_get_plugin_identity ( PLUGIN_MULTIMASTER_REPLICATION ),
  702. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | opflags);
  703. if (target_uniqueid)
  704. {
  705. slapi_pblock_set( newpb, SLAPI_TARGET_UNIQUEID, (void*)target_uniqueid);
  706. }
  707. if (parentuniqueid)
  708. {
  709. struct slapi_operation_parameters *op_params;
  710. slapi_pblock_get( newpb, SLAPI_OPERATION_PARAMETERS, &op_params );
  711. op_params->p.p_add.parentuniqueid = (char*)parentuniqueid; /* Consumes parentuniqueid */
  712. }
  713. slapi_pblock_get ( newpb, SLAPI_OPERATION, &op );
  714. operation_set_csn ( op, opcsn );
  715. slapi_add_internal_pb ( newpb );
  716. slapi_pblock_get ( newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result );
  717. slapi_pblock_destroy ( newpb );
  718. return op_result;
  719. }
  720. int
  721. urp_fixup_rename_entry (Slapi_Entry *entry, const char *newrdn, int opflags)
  722. {
  723. Slapi_PBlock *newpb;
  724. Slapi_Operation *op;
  725. CSN *opcsn;
  726. int op_result;
  727. newpb = slapi_pblock_new();
  728. /*
  729. * Must mark this operation as replicated,
  730. * so that the frontend doesn't add extra attributes.
  731. */
  732. slapi_rename_internal_set_pb_ext (
  733. newpb,
  734. slapi_entry_get_sdn_const (entry),
  735. newrdn, /*NewRDN*/
  736. NULL, /*NewSuperior*/
  737. 0, /* !Delete Old RDNS */
  738. NULL, /*Controls*/
  739. slapi_entry_get_uniqueid (entry), /*uniqueid*/
  740. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  741. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | opflags);
  742. /* set operation csn to the entry's dncsn */
  743. opcsn = (CSN *)entry_get_dncsn (entry);
  744. slapi_pblock_get (newpb, SLAPI_OPERATION, &op);
  745. operation_set_csn (op, opcsn);
  746. slapi_modrdn_internal_pb(newpb);
  747. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
  748. slapi_pblock_destroy(newpb);
  749. return op_result;
  750. }
  751. int
  752. urp_fixup_delete_entry (const char *uniqueid, const char *dn, CSN *opcsn, int opflags)
  753. {
  754. Slapi_PBlock *newpb;
  755. Slapi_Operation *op;
  756. int op_result;
  757. newpb = slapi_pblock_new ();
  758. /*
  759. * Mark this operation as replicated, so that the front end
  760. * doesn't add extra attributes.
  761. */
  762. slapi_delete_internal_set_pb (
  763. newpb,
  764. dn,
  765. NULL, /*Controls*/
  766. uniqueid, /*uniqueid*/
  767. repl_get_plugin_identity ( PLUGIN_MULTIMASTER_REPLICATION ),
  768. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | opflags );
  769. slapi_pblock_get ( newpb, SLAPI_OPERATION, &op );
  770. operation_set_csn ( op, opcsn );
  771. slapi_delete_internal_pb ( newpb );
  772. slapi_pblock_get ( newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result );
  773. slapi_pblock_destroy ( newpb );
  774. return op_result;
  775. }
  776. int
  777. urp_fixup_modify_entry (const char *uniqueid, const Slapi_DN *sdn, CSN *opcsn, Slapi_Mods *smods, int opflags)
  778. {
  779. Slapi_PBlock *newpb;
  780. Slapi_Operation *op;
  781. int op_result;
  782. newpb = slapi_pblock_new();
  783. slapi_modify_internal_set_pb_ext (
  784. newpb,
  785. sdn,
  786. slapi_mods_get_ldapmods_byref (smods),
  787. NULL, /* Controls */
  788. uniqueid,
  789. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  790. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | opflags);
  791. /* set operation csn */
  792. slapi_pblock_get (newpb, SLAPI_OPERATION, &op);
  793. operation_set_csn (op, opcsn);
  794. /* do modify */
  795. slapi_modify_internal_pb (newpb);
  796. slapi_pblock_get (newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
  797. slapi_pblock_destroy(newpb);
  798. return op_result;
  799. }
  800. static int
  801. urp_add_resolve_parententry (Slapi_PBlock *pb, char *sessionid, Slapi_Entry *entry, Slapi_Entry *parententry, CSN *opcsn)
  802. {
  803. Slapi_DN *parentdn = NULL;
  804. Slapi_RDN *add_rdn = NULL;
  805. char *newdn = NULL;
  806. int ldap_rc;
  807. int rc = 0;
  808. Slapi_DN *sdn = NULL;
  809. if( is_suffix_entry (pb, entry, &parentdn) )
  810. {
  811. /* It's OK for the suffix entry's parent to be absent */
  812. rc= 0;
  813. PROFILE_POINT; /* Add Conflict; Suffix Entry */
  814. goto bailout;
  815. }
  816. /* The entry is not a suffix. */
  817. if(parententry==NULL) /* The parent entry was not found. */
  818. {
  819. /* Create a glue entry to stand in for the absent parent */
  820. slapi_operation_parameters *op_params;
  821. slapi_pblock_get( pb, SLAPI_OPERATION_PARAMETERS, &op_params );
  822. ldap_rc = create_glue_entry (pb, sessionid, parentdn, op_params->p.p_add.parentuniqueid, opcsn);
  823. if ( LDAP_SUCCESS == ldap_rc )
  824. {
  825. /* The backend code should now search for the parent again. */
  826. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
  827. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY);
  828. PROFILE_POINT; /* Add Conflict; Orphaned Entry; Glue Parent */
  829. }
  830. else
  831. {
  832. /*
  833. * Error. The parent can't be created as a glue entry.
  834. * This will cause replication divergence and will
  835. * require admin intercession
  836. */
  837. ldap_rc= LDAP_OPERATIONS_ERROR;
  838. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_rc);
  839. rc= -1; /* Abort this Operation */
  840. PROFILE_POINT; /* Add Conflict; Orphaned Entry; Impossible to create parent; Refuse Change. */
  841. }
  842. goto bailout;
  843. }
  844. if(is_tombstone_entry(parententry)) /* The parent is a tombstone */
  845. {
  846. /* The parent entry must be resurected from the dead. */
  847. ldap_rc = tombstone_to_glue (pb, sessionid, parententry, parentdn, REASON_RESURRECT_ENTRY, opcsn);
  848. if ( ldap_rc != LDAP_SUCCESS )
  849. {
  850. ldap_rc= LDAP_OPERATIONS_ERROR;
  851. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_rc);
  852. rc = -1; /* Abort the operation */
  853. }
  854. else
  855. {
  856. /* The backend add code should now search for the parent again. */
  857. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
  858. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY);
  859. }
  860. PROFILE_POINT; /* Add Conflict; Orphaned Entry; Parent Was Tombstone */
  861. goto bailout;
  862. }
  863. /* The parent is healthy */
  864. /* Now we need to check that the parent has the correct DN */
  865. if (slapi_sdn_isparent(slapi_entry_get_sdn(parententry), slapi_entry_get_sdn(entry)))
  866. {
  867. rc= 0; /* OK, Add the entry */
  868. PROFILE_POINT; /* Add Conflict; Parent Exists */
  869. goto bailout;
  870. }
  871. /*
  872. * Parent entry doesn't have a DN parent to the entry.
  873. * This can happen if parententry was renamed due to
  874. * conflict and the child entry was created before
  875. * replication occured. See defect 530942.
  876. * We need to rename the entry to be child of its parent.
  877. */
  878. add_rdn = slapi_rdn_new_dn(slapi_entry_get_dn_const (entry));
  879. newdn = slapi_dn_plus_rdn(slapi_entry_get_dn_const (parententry), slapi_rdn_get_rdn(add_rdn));
  880. slapi_entry_set_normdn ( entry, newdn );
  881. /* slapi_pblock_get(pb, SLAPI_ADD_TARGET, &dn); */
  882. slapi_pblock_get(pb, SLAPI_ADD_TARGET_SDN, &sdn);
  883. slapi_sdn_free(&sdn);
  884. sdn = slapi_sdn_dup(slapi_entry_get_sdn_const(entry));
  885. slapi_pblock_set(pb, SLAPI_ADD_TARGET_SDN, sdn);
  886. slapi_log_error ( slapi_log_urp, sessionid,
  887. "Parent was renamed. Renamed the child to %s\n", newdn );
  888. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
  889. PROFILE_POINT; /* Add Conflict; Parent Renamed; Rename Operation Entry */
  890. bailout:
  891. if (parentdn)
  892. slapi_sdn_free(&parentdn);
  893. slapi_rdn_free(&add_rdn);
  894. return rc;
  895. }
  896. /*
  897. * urp_annotate_dn:
  898. * Returns 0 on failure
  899. * Returns > 0 on success (1 on general conflict resolution success, LDAP_NO_SUCH_OBJECT on no-conflict success)
  900. *
  901. * Use this function to annotate an existing entry only. To annotate
  902. * a new entry (the operation entry) see urp_add_operation.
  903. */
  904. static int
  905. urp_annotate_dn (char *sessionid, Slapi_Entry *entry, CSN *opcsn, const char *optype)
  906. {
  907. int rc = 0; /* Fail */
  908. int op_result;
  909. char *newrdn;
  910. const char *uniqueid;
  911. const Slapi_DN *basesdn;
  912. const char *basedn;
  913. uniqueid = slapi_entry_get_uniqueid (entry);
  914. basesdn = slapi_entry_get_sdn_const (entry);
  915. basedn = slapi_entry_get_dn_const (entry);
  916. newrdn = get_rdn_plus_uniqueid ( sessionid, basedn, uniqueid );
  917. if(newrdn!=NULL)
  918. {
  919. mod_namingconflict_attr (uniqueid, basesdn, basesdn, opcsn);
  920. op_result = urp_fixup_rename_entry ( entry, newrdn, 0 );
  921. switch(op_result)
  922. {
  923. case LDAP_SUCCESS:
  924. slapi_log_error(slapi_log_urp, sessionid,
  925. "Naming conflict %s. Renamed existing entry to %s\n",
  926. optype, newrdn);
  927. rc = 1;
  928. break;
  929. case LDAP_NO_SUCH_OBJECT:
  930. /* This means that entry did not really exist!!!
  931. * This is clearly indicating that there is a
  932. * get_copy_of_entry -> dn2entry returned
  933. * an entry (entry) that was already removed
  934. * from the ldbm database...
  935. * This is bad, because it clearly indicates
  936. * some kind of db or cache corruption. We need to print
  937. * this fact clearly in the errors log to try
  938. * to solve this corruption one day.
  939. * However, as far as the conflict is concerned,
  940. * this error is completely harmless:
  941. * if thew entry did not exist in the first place,
  942. * there was never a room
  943. * for a conflict!! After fix for 558293, this
  944. * state can't be reproduced anymore (5-Oct-01)
  945. */
  946. slapi_log_error( SLAPI_LOG_FATAL, sessionid,
  947. "Entry %s exists in cache but not in DB\n",
  948. basedn );
  949. rc = LDAP_NO_SUCH_OBJECT;
  950. break;
  951. default:
  952. slapi_log_error( slapi_log_urp, sessionid,
  953. "Failed to annotate %s, err=%d\n", newrdn, op_result);
  954. }
  955. slapi_ch_free ( (void**)&newrdn );
  956. }
  957. return rc;
  958. }
  959. /*
  960. * An URP Naming Collision helper function. Retreives a list of entries
  961. * that have the given dn excluding the unique id of the entry. Any
  962. * entries returned will be entries that have been added with the same
  963. * dn, but caused a naming conflict when replicated. The URP to fix
  964. * this constraint violation is to append the unique id of the entry
  965. * to its RDN.
  966. */
  967. static Slapi_Entry *
  968. urp_get_min_naming_conflict_entry ( Slapi_PBlock *pb, char *sessionid, CSN *opcsn )
  969. {
  970. Slapi_PBlock *newpb = NULL;
  971. LDAPControl **server_ctrls = NULL;
  972. Slapi_Entry **entries = NULL;
  973. Slapi_Entry *min_naming_conflict_entry = NULL;
  974. const CSN *min_csn = NULL;
  975. char *filter = NULL;
  976. char *parent_dn = NULL;
  977. char *basedn;
  978. int i = 0;
  979. int min_i = -1;
  980. int op_result = LDAP_SUCCESS;
  981. slapi_pblock_get (pb, SLAPI_URP_NAMING_COLLISION_DN, &basedn);
  982. if (NULL == basedn || strncmp (basedn, SLAPI_ATTR_UNIQUEID, strlen(SLAPI_ATTR_UNIQUEID)) == 0)
  983. return NULL;
  984. slapi_log_error ( SLAPI_LOG_REPL, sessionid,
  985. "Enter urp_get_min_naming_conflict_entry for %s\n", basedn);
  986. filter = PR_smprintf("(%s=%s %s)", ATTR_NSDS5_REPLCONFLICT, REASON_ANNOTATE_DN, basedn);
  987. /* server_ctrls will be freed when newpb is destroyed */
  988. server_ctrls = (LDAPControl **)slapi_ch_calloc (2, sizeof (LDAPControl *));
  989. server_ctrls[0] = create_managedsait_control();
  990. server_ctrls[1] = NULL;
  991. newpb = slapi_pblock_new();
  992. parent_dn = slapi_dn_parent (basedn);
  993. slapi_search_internal_set_pb(newpb,
  994. parent_dn, /* Base DN */
  995. LDAP_SCOPE_ONELEVEL,
  996. filter,
  997. NULL, /* Attrs */
  998. 0, /* AttrOnly */
  999. server_ctrls, /* Controls */
  1000. NULL, /* UniqueID */
  1001. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  1002. 0);
  1003. slapi_search_internal_pb(newpb);
  1004. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
  1005. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1006. if ( (op_result != LDAP_SUCCESS) || (entries == NULL) )
  1007. {
  1008. /* Log a message */
  1009. goto done;
  1010. }
  1011. /* For all entries, get the one with the smallest dn csn */
  1012. for (i = 0; NULL != entries[i]; i++)
  1013. {
  1014. const CSN *dncsn;
  1015. dncsn = entry_get_dncsn(entries[i]);
  1016. if ((dncsn != opcsn) &&
  1017. ((min_csn == NULL) || (csn_compare(dncsn, min_csn) < 0)) &&
  1018. !is_tombstone_entry (entries[i]))
  1019. {
  1020. min_csn = dncsn;
  1021. min_i = i;
  1022. }
  1023. /*
  1024. * If there are too many conflicts, the current urp code has no
  1025. * guarantee for all servers to converge anyway, because the
  1026. * urp and the backend can't be done in one transaction due
  1027. * to either performance or the deadlock problem.
  1028. * Don't sacrifice the performance too much for impossible.
  1029. */
  1030. if (min_csn && i > 5)
  1031. {
  1032. break;
  1033. }
  1034. }
  1035. if (min_csn != NULL) {
  1036. /* Found one entry */
  1037. min_naming_conflict_entry = slapi_entry_dup(entries[min_i]);
  1038. }
  1039. done:
  1040. slapi_ch_free_string(&parent_dn);
  1041. if (filter) {
  1042. PR_smprintf_free(filter);
  1043. }
  1044. slapi_free_search_results_internal(newpb);
  1045. slapi_pblock_destroy(newpb);
  1046. newpb = NULL;
  1047. slapi_log_error ( SLAPI_LOG_REPL, sessionid,
  1048. "Leave urp_get_min_naming_conflict_entry (found %d entries)\n", i);
  1049. return min_naming_conflict_entry;
  1050. }
  1051. /*
  1052. * If an entry is deleted or renamed, a new winner may be
  1053. * chosen from its naming competitors.
  1054. * The entry with the smallest dncsn restores its original DN.
  1055. */
  1056. static int
  1057. urp_naming_conflict_removal ( Slapi_PBlock *pb, char *sessionid, CSN *opcsn, const char *optype )
  1058. {
  1059. Slapi_Entry *min_naming_conflict_entry;
  1060. Slapi_RDN *oldrdn, *newrdn;
  1061. const char *oldrdnstr, *newrdnstr;
  1062. int op_result;
  1063. /*
  1064. * Backend op has set SLAPI_URP_NAMING_COLLISION_DN to the basedn.
  1065. */
  1066. min_naming_conflict_entry = urp_get_min_naming_conflict_entry (pb, sessionid, opcsn);
  1067. if (min_naming_conflict_entry == NULL)
  1068. {
  1069. return 0;
  1070. }
  1071. /* Step 1: Restore the entry's original DN */
  1072. oldrdn = slapi_rdn_new_sdn ( slapi_entry_get_sdn (min_naming_conflict_entry) );
  1073. oldrdnstr = slapi_rdn_get_rdn ( oldrdn );
  1074. /* newrdnstr is the old rdn of the entry minus the nsuniqueid part */
  1075. newrdn = slapi_rdn_new_rdn ( oldrdn );
  1076. slapi_rdn_remove_attr (newrdn, SLAPI_ATTR_UNIQUEID );
  1077. newrdnstr = slapi_rdn_get_rdn ( newrdn );
  1078. /*
  1079. * Set OP_FLAG_ACTION_INVOKE_FOR_REPLOP since this operation
  1080. * is done after DB lock was released. The backend modrdn
  1081. * will acquire the DB lock if it sees this flag.
  1082. */
  1083. op_result = urp_fixup_rename_entry (min_naming_conflict_entry, newrdnstr, OP_FLAG_ACTION_INVOKE_FOR_REPLOP);
  1084. if ( op_result != LDAP_SUCCESS )
  1085. {
  1086. slapi_log_error (slapi_log_urp, sessionid,
  1087. "Failed to restore RDN of %s, err=%d\n", oldrdnstr, op_result);
  1088. goto bailout;
  1089. }
  1090. slapi_log_error (slapi_log_urp, sessionid,
  1091. "Naming conflict removed by %s. RDN of %s was restored\n", optype, oldrdnstr);
  1092. /* Step2: Remove ATTR_NSDS5_REPLCONFLICT from the winning entry */
  1093. /*
  1094. * A fixup op will not invoke urp_modrdn_operation(). Even it does,
  1095. * urp_modrdn_operation() will do nothing because of the same CSN.
  1096. */
  1097. op_result = del_replconflict_attr (min_naming_conflict_entry, opcsn, OP_FLAG_ACTION_INVOKE_FOR_REPLOP);
  1098. if (op_result != LDAP_SUCCESS) {
  1099. slapi_log_error(SLAPI_LOG_REPL, sessionid,
  1100. "Failed to remove nsds5ReplConflict for %s, err=%d\n",
  1101. newrdnstr, op_result);
  1102. }
  1103. bailout:
  1104. slapi_entry_free (min_naming_conflict_entry);
  1105. slapi_rdn_free(&oldrdn);
  1106. slapi_rdn_free(&newrdn);
  1107. return op_result;
  1108. }
  1109. /* The returned value is either null or "uniqueid=<uniqueid>+<basedn>" */
  1110. static char *
  1111. get_dn_plus_uniqueid(char *sessionid, const char *olddn, const char *uniqueid)
  1112. {
  1113. Slapi_DN *sdn= slapi_sdn_new_dn_byval(olddn);
  1114. Slapi_RDN *rdn= slapi_rdn_new();
  1115. char *newdn;
  1116. PR_ASSERT(uniqueid!=NULL);
  1117. /* Check if the RDN already contains the Unique ID */
  1118. slapi_sdn_get_rdn(sdn,rdn);
  1119. if(slapi_rdn_contains(rdn,SLAPI_ATTR_UNIQUEID,uniqueid,strlen(uniqueid)))
  1120. {
  1121. /* The Unique ID is already in the RDN.
  1122. * This is a highly improbable collision.
  1123. * It suggests that a duplicate UUID was generated.
  1124. * This will cause replication divergence and will
  1125. * require admin intercession
  1126. */
  1127. slapi_log_error(SLAPI_LOG_FATAL, sessionid,
  1128. "Annotated DN %s has naming conflict\n", olddn );
  1129. newdn= NULL;
  1130. }
  1131. else
  1132. {
  1133. slapi_rdn_add(rdn,SLAPI_ATTR_UNIQUEID,uniqueid);
  1134. slapi_sdn_set_rdn(sdn, rdn);
  1135. newdn= slapi_ch_strdup(slapi_sdn_get_dn(sdn));
  1136. }
  1137. slapi_sdn_free(&sdn);
  1138. slapi_rdn_free(&rdn);
  1139. return newdn;
  1140. }
  1141. static char *
  1142. get_rdn_plus_uniqueid(char *sessionid, const char *olddn, const char *uniqueid)
  1143. {
  1144. char *newrdn;
  1145. /* Check if the RDN already contains the Unique ID */
  1146. Slapi_DN *sdn= slapi_sdn_new_dn_byval(olddn);
  1147. Slapi_RDN *rdn= slapi_rdn_new();
  1148. slapi_sdn_get_rdn(sdn,rdn);
  1149. PR_ASSERT(uniqueid!=NULL);
  1150. if(slapi_rdn_contains(rdn,SLAPI_ATTR_UNIQUEID,uniqueid,strlen(uniqueid)))
  1151. {
  1152. /* The Unique ID is already in the RDN.
  1153. * This is a highly improbable collision.
  1154. * It suggests that a duplicate UUID was generated.
  1155. * This will cause replication divergence and will
  1156. * require admin intercession
  1157. */
  1158. slapi_log_error(SLAPI_LOG_FATAL, sessionid,
  1159. "Annotated DN %s has naming conflict\n", olddn );
  1160. newrdn= NULL;
  1161. }
  1162. else
  1163. {
  1164. slapi_rdn_add(rdn,SLAPI_ATTR_UNIQUEID,uniqueid);
  1165. newrdn= slapi_ch_strdup(slapi_rdn_get_rdn(rdn));
  1166. }
  1167. slapi_sdn_free(&sdn);
  1168. slapi_rdn_free(&rdn);
  1169. return newrdn;
  1170. }
  1171. static int
  1172. is_suffix_entry ( Slapi_PBlock *pb, Slapi_Entry *entry, Slapi_DN **parentdn )
  1173. {
  1174. return is_suffix_dn ( pb, slapi_entry_get_sdn(entry), parentdn );
  1175. }
  1176. int
  1177. is_suffix_dn_ext ( Slapi_PBlock *pb, const Slapi_DN *dn, Slapi_DN **parentdn,
  1178. int is_tombstone )
  1179. {
  1180. Slapi_Backend *backend;
  1181. int rc;
  1182. *parentdn = slapi_sdn_new();
  1183. slapi_pblock_get( pb, SLAPI_BACKEND, &backend );
  1184. slapi_sdn_get_backend_parent_ext (dn, *parentdn, backend, is_tombstone);
  1185. /* A suffix entry doesn't have parent dn */
  1186. rc = slapi_sdn_isempty (*parentdn) ? 1 : 0;
  1187. return rc;
  1188. }
  1189. int
  1190. is_suffix_dn ( Slapi_PBlock *pb, const Slapi_DN *dn, Slapi_DN **parentdn )
  1191. {
  1192. return is_suffix_dn_ext ( pb, dn, parentdn, 0 );
  1193. }
  1194. static int
  1195. mod_namingconflict_attr (const char *uniqueid, const Slapi_DN *entrysdn,
  1196. const Slapi_DN *conflictsdn, CSN *opcsn)
  1197. {
  1198. Slapi_Mods smods;
  1199. char buf[BUFSIZ];
  1200. int op_result;
  1201. PR_snprintf (buf, sizeof(buf), "%s %s",
  1202. REASON_ANNOTATE_DN, slapi_sdn_get_dn(conflictsdn));
  1203. slapi_mods_init (&smods, 2);
  1204. if ( strncmp (slapi_sdn_get_dn(entrysdn), SLAPI_ATTR_UNIQUEID,
  1205. strlen(SLAPI_ATTR_UNIQUEID)) != 0 )
  1206. {
  1207. slapi_mods_add (&smods, LDAP_MOD_ADD, ATTR_NSDS5_REPLCONFLICT, strlen(buf), buf);
  1208. }
  1209. else
  1210. {
  1211. /*
  1212. * If the existing entry is already a naming conflict loser,
  1213. * the following replace operation should result in the
  1214. * replace of the ATTR_NSDS5_REPLCONFLICT index as well.
  1215. */
  1216. slapi_mods_add (&smods, LDAP_MOD_REPLACE, ATTR_NSDS5_REPLCONFLICT, strlen(buf), buf);
  1217. }
  1218. op_result = urp_fixup_modify_entry (uniqueid, entrysdn, opcsn, &smods, 0);
  1219. slapi_mods_done (&smods);
  1220. return op_result;
  1221. }
  1222. static int
  1223. del_replconflict_attr (Slapi_Entry *entry, CSN *opcsn, int opflags)
  1224. {
  1225. Slapi_Attr *attr;
  1226. int op_result = 0;
  1227. if (slapi_entry_attr_find (entry, ATTR_NSDS5_REPLCONFLICT, &attr) == 0)
  1228. {
  1229. Slapi_Mods smods;
  1230. const char *uniqueid;
  1231. const Slapi_DN *entrysdn;
  1232. uniqueid = slapi_entry_get_uniqueid (entry);
  1233. entrysdn = slapi_entry_get_sdn_const (entry);
  1234. slapi_mods_init (&smods, 2);
  1235. slapi_mods_add (&smods, LDAP_MOD_DELETE, ATTR_NSDS5_REPLCONFLICT, 0, NULL);
  1236. op_result = urp_fixup_modify_entry (uniqueid, entrysdn, opcsn, &smods, opflags);
  1237. slapi_mods_done (&smods);
  1238. }
  1239. return op_result;
  1240. }