urp.c 40 KB

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