urp.c 41 KB

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