1
0

urp.c 44 KB

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