urp.c 44 KB

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