urp.c 50 KB

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