urp.c 38 KB

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