urp.c 40 KB

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