urp.c 42 KB

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