urp.c 41 KB

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