urp.c 44 KB

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