1
0

urp.c 40 KB

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