File: urp.c

package info (click to toggle)
389-ds-base 2.3.1%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 37,536 kB
  • sloc: ansic: 306,972; python: 96,937; cpp: 10,257; perl: 2,854; makefile: 2,046; sh: 925; yacc: 806; xml: 379; lex: 366; javascript: 148; java: 50
file content (2239 lines) | stat: -rw-r--r-- 90,739 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2021 Red Hat, Inc.
 * All rights reserved.
 *
 * License: GPL (version 3 or any later version).
 * See LICENSE for details.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


/*
 * urp.c - Update Resolution Procedures
 */

#include "slapi-plugin.h"
#include "repl5.h"
#include "urp.h"

extern int slapi_log_urp;

static int urp_add_resolve_parententry(Slapi_PBlock *pb, char *sessionid, Slapi_Entry *entry, Slapi_Entry *parententry, CSN *opcsn);
static int urp_add_check_tombstone(Slapi_PBlock *pb, char *sessionid, Slapi_Entry *entry, CSN *opcsn);
static int urp_delete_check_conflict(char *sessionid, Slapi_Entry *tombstone_entry, CSN *opcsn);
static int urp_add_new_entry_to_conflict(Slapi_PBlock *pb, char *sessionid, Slapi_Entry *addentry, CSN *opcsn);
static int urp_annotate_dn(char *sessionid, const Slapi_Entry *entry, CSN *opcsn, const char *optype, char **conflict_dn);
static int urp_conflict_to_glue(char *sessionid, const Slapi_Entry *entry, Slapi_DN *parentdn, CSN *opcsn);
static char *urp_find_tombstone_for_glue(Slapi_PBlock *pb, char *sessionid, const Slapi_Entry *entry, Slapi_DN *parentdn, CSN *opcsn);
static char *urp_find_valid_entry_to_delete(Slapi_PBlock *pb, const Slapi_Entry *deleteentry, char *sessionid, CSN *opcsn);
static int urp_naming_conflict_removal(Slapi_PBlock *pb, char *sessionid, CSN *opcsn, const char *optype);
static int mod_namingconflict_attr(const char *uniqueid, const Slapi_DN *entrysdn, const Slapi_DN *conflictsdn, CSN *opcsn, const char *optype);
static int mod_objectclass_attr(const char *uniqueid, const Slapi_DN *entrysdn, const Slapi_DN *conflictsdn, CSN *opcsn, const char *optype);
static int del_replconflict_attr(const Slapi_Entry *entry, CSN *opcsn, int opflags);
static char *get_dn_plus_uniqueid(char *sessionid,const Slapi_DN *oldsdn,const char *uniqueid);
static int is_suffix_entry(Slapi_PBlock *pb, Slapi_Entry *entry, Slapi_DN **parenddn);
static int is_renamed_entry(Slapi_PBlock *pb, Slapi_Entry *entry, CSN *opcsn);
static int urp_fixup_add_cenotaph(Slapi_PBlock *pb, char *sessionid, CSN *opcsn);
static int is_deleted_at_csn(const Slapi_Entry *entry, CSN *opcsn);
static char * urp_get_valid_parent_nsuniqueid(Slapi_DN *parentdn);
static int urp_rename_conflict_children(const char *old_parent, const Slapi_DN *new_parent);
static Slapi_Entry *urp_get_min_naming_conflict_entry(Slapi_PBlock *pb, const char *collision_dn, char *sessionid, CSN *opcsn);

/*
 * Return 0 for OK, -1 for Error.
 */
int
urp_modify_operation(Slapi_PBlock *pb)
{
    Slapi_Entry *modifyentry = NULL;
    int op_result = 0;
    int rc = 0; /* OK */
    char sessionid[REPL_SESSION_ID_SIZE];
    CSN *opcsn;

    if (slapi_op_abandoned(pb)) {
        return rc;
    }

    get_repl_session_id(pb, sessionid, &opcsn);
    slapi_pblock_get(pb, SLAPI_MODIFY_EXISTING_ENTRY, &modifyentry);
    if (modifyentry != NULL) {
        /*
         * The entry to be modified exists.
         * - the entry could be a tombstone... but that's OK.
         * - the entry could be glue... that may not be OK. JCMREPL
         */
        rc = 0;        /* OK, Modify the entry */
        PROFILE_POINT; /* Modify Conflict; Entry Exists; Apply Modification */
    } else {
        /*
         * The entry to be modified could not be found.
         */
        op_result = LDAP_NO_SUCH_OBJECT;
        slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
        rc = SLAPI_PLUGIN_NOOP; /* Must discard this Modification */
        slapi_log_err(slapi_log_urp, sessionid,
                      "urp_modify_operation - No such entry\n");
        PROFILE_POINT; /* Modify Conflict; Entry Does Not Exist; Discard Modification */
    }
    return rc;
}

/*
 * Return 0 for OK,
 *       -1 for Ignore or Error depending on SLAPI_RESULT_CODE,
 *       >0 for action code
 * Action Code Bit 0: Fetch existing entry.
 * Action Code Bit 1: Fetch parent entry.
 * The function is called as a be pre-op on consumers.
 */
int
urp_add_operation(Slapi_PBlock *pb)
{
    Slapi_Entry *existing_uniqueid_entry;
    Slapi_Entry *existing_dn_entry;
    Slapi_Entry *addentry;
    CSN *opcsn;
    const char *basedn;
    char sessionid[REPL_SESSION_ID_SIZE];
    int r;
    int op_result = 0;
    int rc = 0; /* OK */

    if (slapi_op_abandoned(pb)) {
        return rc;
    }

    get_repl_session_id(pb, sessionid, &opcsn);
    slapi_pblock_get(pb, SLAPI_ADD_EXISTING_UNIQUEID_ENTRY, &existing_uniqueid_entry);
    if (existing_uniqueid_entry != NULL) {
        /*
         * An entry with this uniqueid already exists.
         * - It could be a replay of the same Add, or
         * - It could be a UUID generation collision, or
         */
        /*
         * This operation won't be replayed.  That is, this CSN won't update
         * the max csn in RUV. The CSN is left uncommitted in RUV unless an
         * error is set to op_result.  Just to get rid of this CSN from RUV,
         * setting an error to op_result
         */
        /* op_result = LDAP_SUCCESS; */
        slapi_log_err(slapi_log_urp, sessionid,
                      "urp_add_operation - %s - An entry with this uniqueid already exists.\n",
                      slapi_entry_get_dn_const(existing_uniqueid_entry));
        op_result = LDAP_ALREADY_EXISTS;
        slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
        rc = SLAPI_PLUGIN_NOOP; /* Ignore this Operation */
        PROFILE_POINT;          /* Add Conflict; UniqueID Exists;  Ignore */
        goto bailout;
    }

    slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &addentry);
    slapi_pblock_get(pb, SLAPI_ADD_EXISTING_DN_ENTRY, &existing_dn_entry);

    slapi_log_err(SLAPI_LOG_REPL, sessionid,
                   "urp_add_operation - handling add of (%s).\n",
                   slapi_entry_get_dn_const(addentry));
    /* first check if there was an existing entry at the time of this add 
     * in that case the new entry will become a conflict entry independent 
     * of the case if the other entry still exists
     */
    rc = urp_add_check_tombstone(pb, sessionid, addentry, opcsn);
    if (rc == 1) {
        /* the new entry is conflicting with an already deleted entry
         * transform to conflict entry
          */
        rc = urp_add_new_entry_to_conflict(pb, sessionid, addentry, opcsn);
        slapi_log_err(SLAPI_LOG_REPL, sessionid,
                       "urp_add_operation - new entry to conflictentry (%s).\n",
                       slapi_entry_get_dn_const(addentry));
        goto bailout;
    } else if (rc == 2) {
        /* the tombstone is newer than the added entry
         * inform the caller that this needs to become a tombstone operation
         * and turn the tombstone to a conflict
         */
        rc = SLAPI_PLUGIN_NOOP_TOMBSTONE;
        slapi_log_err(SLAPI_LOG_REPL, sessionid,
                       "urp_add_operation - new entry to tombstone (%s).\n",
                       slapi_entry_get_dn_const(addentry));
        goto bailout;
    }

    if (existing_dn_entry == NULL) /* The target DN does not exist */
    {
        /* Check for parent entry... this could be an orphan. */
        Slapi_Entry *parententry;
        slapi_pblock_get(pb, SLAPI_ADD_PARENT_ENTRY, &parententry);
        rc = urp_add_resolve_parententry(pb, sessionid, addentry, parententry, opcsn);
        PROFILE_POINT; /* Add Entry */
        goto bailout;
    }

    /*
     * Naming conflict: an entry with the target DN already exists.
     * Compare the DistinguishedNameCSN of the existing entry
     * and the OperationCSN. The smaller CSN wins. The loser changes
     * its RDN to uniqueid+baserdn, and adds operational attribute
     * ATTR_NSDS5_REPLCONFLIC.
     */
    basedn = slapi_entry_get_ndn (addentry);
    r = csn_compare (entry_get_dncsn(existing_dn_entry), opcsn);
    if (r < 0) {
        /* Entry to be added is a loser */
        rc = urp_add_new_entry_to_conflict(pb, sessionid, addentry, opcsn);
    } else if (r > 0) {
        char *conflict_dn = NULL;
        /* Existing entry is a loser */
        if (!urp_annotate_dn(sessionid, existing_dn_entry, opcsn, "ADD", &conflict_dn)) {
            op_result= LDAP_OPERATIONS_ERROR;
            slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
            slapi_log_err(slapi_log_urp, sessionid,
                           "urp_add_operation - %s - Entry to be added is a loser; "
                           "urp_annotate_dn failed.\n", basedn);
            rc = SLAPI_PLUGIN_NOOP; /* Ignore this Operation */
        } else {
            /* The backend add code should now search for the existing entry again. */
            rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
            rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY);
            slapi_pblock_set(pb, SLAPI_URP_NAMING_COLLISION_DN, conflict_dn);
        }
        PROFILE_POINT; /* Add Conflict; Entry Exists; Rename Existing Entry */
    } else             /* r==0 */
    {
        /* The CSN of the Operation and the Entry DN are the same.
         * This could only happen if:
         * a) There are two replicas with the same ReplicaID.
         * b) We've seen the Operation before.
         * Let's go with (b) and ignore the little bastard.
         */
        /*
         * This operation won't be replayed.  That is, this CSN won't update
         * the max csn in RUV. The CSN is left uncommitted in RUV unless an
         * error is set to op_result.  Just to get rid of this CSN from RUV,
         * setting an error to op_result
         */
        /* op_result = LDAP_SUCCESS; */
        slapi_log_err(slapi_log_urp, sessionid,
                      "urp_add_operation - %s - The CSN of the Operation and the Entry DN are the same.",
                      slapi_entry_get_dn_const(existing_dn_entry));
        op_result = LDAP_UNWILLING_TO_PERFORM;
        slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
        rc = SLAPI_PLUGIN_NOOP; /* Ignore this Operation */
        PROFILE_POINT;          /* Add Conflict; Entry Exists; Same CSN */
    }

bailout:
    return rc;
}

/*
 * Return 0 for OK, -1 for Error, >0 for action code
 * Action Code Bit 0: Fetch existing entry.
 * Action Code Bit 1: Fetch parent entry.
 */
int
urp_modrdn_operation(Slapi_PBlock *pb)
{
    slapi_operation_parameters *op_params = NULL;
    Slapi_Entry *parent_entry;
    Slapi_Entry *new_parent_entry;
    Slapi_DN *newsuperior = NULL;
    Slapi_DN *parentdn = NULL;
    Slapi_Entry *target_entry;
    Slapi_Entry *existing_entry;
    const CSN *target_entry_dncsn;
    CSN *opcsn = NULL;
    char *op_uniqueid = NULL;
    const char *existing_uniqueid = NULL;
    const Slapi_DN *target_sdn;
    const Slapi_DN *existing_sdn;
    char *newrdn;
    char sessionid[REPL_SESSION_ID_SIZE];
    int r;
    int op_result = 0;
    int rc = 0; /* OK */
    int del_old_replconflict_attr = 0;

    if (slapi_op_abandoned(pb)) {
        return rc;
    }

    slapi_pblock_get(pb, SLAPI_MODRDN_TARGET_ENTRY, &target_entry);
    if (target_entry == NULL) {
        /* An entry can't be found for the Unique Identifier */
        op_result = LDAP_NO_SUCH_OBJECT;
        slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
        rc = -1;       /* No entry to modrdn */
        PROFILE_POINT; /* ModRDN Conflict; Entry does not Exist; Discard ModRDN */
        goto bailout;
    }

    get_repl_session_id(pb, sessionid, &opcsn);
    target_entry_dncsn = entry_get_dncsn(target_entry);
    if (csn_compare(target_entry_dncsn, opcsn) >= 0) {
        /*
         * The Operation CSN is not newer than the DN CSN.
         * Either we're beaten by another ModRDN or we've applied the op.
         */
        /* op_result= LDAP_SUCCESS; */
        /*
         * This operation won't be replayed.  That is, this CSN won't update
         * the max csn in RUV. The CSN is left uncommitted in RUV unless an
         * error is set to op_result.  Just to get rid of this CSN from RUV,
         * setting an error to op_result
         */
        slapi_log_err(slapi_log_urp, sessionid,
                      "urp_modrdn_operation - %s - operation CSN is newer than the DN CSN.\n",
                      slapi_entry_get_dn_const(target_entry));
        op_result = LDAP_UNWILLING_TO_PERFORM;
        slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
        rc = SLAPI_PLUGIN_NOOP; /* Ignore the modrdn */
        PROFILE_POINT;          /* ModRDN Conflict; Entry with Target DN Exists; OPCSN is not newer. */
        goto bailout;
    }

    /* The DN CSN is older than the Operation CSN. Apply the operation */
    target_sdn = slapi_entry_get_sdn_const(target_entry);
    /* newrdn is no need to be case-ignored (get_rdn_plus_uniqueid) */
    slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &newrdn);
    slapi_pblock_get(pb, SLAPI_TARGET_UNIQUEID, &op_uniqueid);
    slapi_pblock_get(pb, SLAPI_MODRDN_PARENT_ENTRY, &parent_entry);
    slapi_pblock_get(pb, SLAPI_MODRDN_NEWPARENT_ENTRY, &new_parent_entry);
    slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &newsuperior);

    if (is_conflict_entry (target_entry)) {
        slapi_log_err(SLAPI_LOG_REPL, sessionid,
                      "urp_modrdn_operation  - Target_entry %s is a conflict; what to do ?\n",
                      slapi_entry_get_dn((Slapi_Entry *)target_entry));
    }

    if (is_tombstone_entry(target_entry)) {
        /*
         * It is a non-trivial task to rename a tombstone.
         * This op has been ignored so far by
         * setting SLAPI_RESULT_CODE to LDAP_NO_SUCH_OBJECT
         * and rc to -1.
         */

        /* Turn the tombstone to glue before rename it */
        /* check if the delete was after the modrdn */
        char *del_str = (char*)slapi_entry_attr_get_ref(target_entry, "nstombstonecsn");
        CSN *del_csn = csn_new_by_string(del_str);
        if (csn_compare(del_csn,opcsn)>0) {
            char *glue_dn = (char*)slapi_entry_attr_get_ref(target_entry, "nscpentrydn");
            Slapi_DN *glue_sdn = slapi_sdn_new_dn_byval(glue_dn);
            op_result = tombstone_to_glue (pb, sessionid, target_entry,
                                           glue_sdn, "renameTombstone", opcsn, NULL);
            slapi_log_err(SLAPI_LOG_REPL, sessionid,
                          "urp_modrdn_operation  - Target_entry %s is a tombstone; Renaming since delete was after rename.\n",
                           slapi_entry_get_dn((Slapi_Entry *)target_entry));
            slapi_sdn_free(&glue_sdn);
        } else {
            op_result = LDAP_NO_SUCH_OBJECT;
            slapi_log_err(SLAPI_LOG_REPL, sessionid,
                          "urp_modrdn_operation  - Target_entry %s is a tombstone; returning LDAP_NO_SUCH_OBJECT.\n",
                          slapi_entry_get_dn((Slapi_Entry *)target_entry));
        }
        csn_free(&del_csn);

        slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);

        if (op_result == 0) {
            /*
             * Remember to turn this entry back to tombstone in post op.
             * We'll just borrow an obsolete pblock type here. ???
             */
            /* no. slapi_pblock_set (pb, SLAPI_URP_TOMBSTONE_UNIQUEID, slapi_ch_strdup(op_uniqueid)); */
            (void) slapi_setbit_int(rc, SLAPI_RTN_BIT_FETCH_TARGET_ENTRY);
            rc = 0;
        } else {
            slapi_log_err(slapi_log_urp, sessionid,
                          "urp_modrdn_operation - %s - Target entry is a tombstone.\n",
                          slapi_entry_get_dn_const(target_entry));
            rc = SLAPI_PLUGIN_NOOP; /* Ignore the modrdn */
        }
        PROFILE_POINT; /* ModRDN Conflict; Entry with Target DN Exists; OPCSN is not newer. */
        goto bailout;
    }

    slapi_pblock_get(pb, SLAPI_MODRDN_EXISTING_ENTRY, &existing_entry);
    if (existing_entry != NULL) {
        /*
         * An entry with the target DN already exists.
         * The smaller dncsn wins. The loser changes its RDN to
         * uniqueid+baserdn, and adds operational attribute
         * ATTR_NSDS5_REPLCONFLICT
         */

        if (is_conflict_entry (existing_entry)) {
            slapi_log_err(SLAPI_LOG_REPL, sessionid,
                          "urp_modrdn_operation  - Existing_entry %s is a conflict; what to do ?\n",
                          slapi_entry_get_dn((Slapi_Entry *)existing_entry));
        }

        existing_uniqueid = slapi_entry_get_uniqueid(existing_entry);
        existing_sdn = slapi_entry_get_sdn_const(existing_entry);

        /*
         * It used to dismiss the operation if the existing entry is
         * the same as the target one.
         * But renaming the RDN with the one which only cases are different,
         * cn=ABC --> cn=Abc, this case matches.  We should go forward the op.
         */
        if (strcmp(op_uniqueid, existing_uniqueid) == 0) {
            op_result = LDAP_SUCCESS;
            slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
            rc = 0;        /* Don't ignore the op */
            PROFILE_POINT; /* ModRDN Replay */
            goto bailout;
        }

        r = csn_compare(entry_get_dncsn(existing_entry), opcsn);
        if (r == 0) {
            /*
             * The CSN of the Operation and the Entry DN are the same
             * but the uniqueids are not.
             * There might be two replicas with the same ReplicaID.
             */
            slapi_log_err(slapi_log_urp, sessionid,
                          "urp_modrdn_operation - Duplicated CSN for different uniqueids [%s][%s]",
                          existing_uniqueid, op_uniqueid);
            op_result = LDAP_OPERATIONS_ERROR;
            slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
            rc = SLAPI_PLUGIN_NOOP; /* Ignore this Operation */
            PROFILE_POINT;          /* ModRDN Conflict; Duplicated CSN for Different Entries */
            goto bailout;
        }

        if (r < 0) {
            /* The target entry is a loser */

            char *newrdn_with_uniqueid;
            newrdn_with_uniqueid = get_rdn_plus_uniqueid(sessionid, newrdn, op_uniqueid);
            if (newrdn_with_uniqueid == NULL) {
                op_result = LDAP_OPERATIONS_ERROR;
                slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
                rc = -1;       /* Ignore this Operation */
                PROFILE_POINT; /* ModRDN Conflict; Entry with Target DN Exists;
                                  Unique ID already in RDN - Change to Lost and Found entry */
                goto bailout;
            }
            mod_namingconflict_attr(op_uniqueid, target_sdn, existing_sdn, opcsn, "MODRDN");
            mod_objectclass_attr(op_uniqueid, target_sdn, target_sdn, opcsn, "MODRDN");
            slapi_pblock_set(pb, SLAPI_MODRDN_NEWRDN, newrdn_with_uniqueid);
            slapi_log_err(slapi_log_urp, sessionid,
                          "urp_modrdn_operation - Naming conflict MODRDN. Rename target entry from %s to %s\n",
                          newrdn, newrdn_with_uniqueid);
            rc = slapi_setbit_int(rc, SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
            PROFILE_POINT; /* ModRDN Conflict; Entry with Target DN Exists; Rename Operation Entry */
            goto bailout;
        }

        if (r > 0) {
            /* The existing entry is a loser */

            int resolve = urp_annotate_dn(sessionid, existing_entry, opcsn, "MODRDN", NULL);
            if (!resolve) {
                op_result = LDAP_OPERATIONS_ERROR;
                slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
                rc = -1; /* Abort this Operation */
                goto bailout;
            }
            rc = slapi_setbit_int(rc, SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
            rc = slapi_setbit_int(rc, SLAPI_RTN_BIT_FETCH_NEWPARENT_ENTRY);
            if (LDAP_NO_SUCH_OBJECT == resolve) {
                /* This means that existing_dn_entry did not really exist!!!
                 * This indicates that a get_copy_of_entry -> dn2entry returned
                 * an entry (existing_dn_entry) that was already removed from the ldbm.
                 * This is bad, because it indicates a dn cache or DB corruption.
                 * However, as far as the conflict is concerned, this error is harmless:
                 * if the existing_dn_entry did not exist in the first place, there was no
                 * conflict!! Return 0 for success to break the ldbm_back_modrdn loop
                 * and get out of this inexistent conflict resolution ASAP.
                 */
                rc = 0;
            }
            /* Set flag to remove possible old naming conflict */
            del_old_replconflict_attr = 1;
            PROFILE_POINT; /* ModRDN Conflict; Entry with Target DN Exists; Rename Entry with Target DN */
            goto bailout;
        }
    } else {
        /*
         * No entry with the target DN exists.
         */

        /* Set flag to remove possible old naming conflict */
        del_old_replconflict_attr = 1;

        if (new_parent_entry != NULL) {
            /* The new superior entry exists */
            rc = 0;        /* OK, Apply the ModRDN */
            PROFILE_POINT; /* ModRDN Conflict; OK */
            goto bailout;
        }

        /* The new superior entry doesn't exist */

        slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &newsuperior);
        if (newsuperior == NULL) {
            /* (new_parent_entry==NULL && newsuperiordn==NULL)
             * This is ok - SLAPI_MODRDN_NEWPARENT_ENTRY will
             * only be set if SLAPI_MODRDN_NEWSUPERIOR_SDN was
             * suplied by the client. If it wasn't, we're just
             * changing the RDN of the entry. In that case,
             * if the entry exists, its parent won't change
             * when it's renamed, and therefore we can assume
             * its parent exists.
             */
            rc = 0;
            PROFILE_POINT; /* ModRDN OK */
            goto bailout;
        }

        if ((0 == slapi_sdn_compare(slapi_entry_get_sdn(parent_entry),
                                    newsuperior)) ||
            is_suffix_dn(pb, newsuperior, &parentdn)) {
            /*
             * The new superior is the same as the current one, or
             * this entry is a suffix whose parent can be absent.
             */
            rc = 0;        /* OK, Move the entry */
            PROFILE_POINT; /* ModRDN Conflict; Absent Target Parent; Create Suffix Entry */
            goto bailout;
        }

        /*
         * This entry is not a suffix entry, so the parent entry should exist.
         * (This shouldn't happen in a ds5 server)
         */
        slapi_pblock_get(pb, SLAPI_OPERATION_PARAMETERS, &op_params);
        op_result = create_glue_entry(pb, sessionid, newsuperior,
                                      op_params->p.p_modrdn.modrdn_newsuperior_address.uniqueid, opcsn);
        if (LDAP_SUCCESS != op_result) {
            /*
             * FATAL ERROR
             * We should probably just abort the rename
             * this will cause replication divergence requiring
             * admin intercession
             */
            slapi_log_err(SLAPI_LOG_ERR, sessionid,
                          "urp_modrdn_operation - Parent %s couldn't be found, nor recreated as a glue entry\n",
                          slapi_sdn_get_dn(newsuperior));
            op_result = LDAP_OPERATIONS_ERROR;
            slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
            rc = SLAPI_PLUGIN_FAILURE; /* Ignore this Operation */
            PROFILE_POINT;
            goto bailout;
        }

        /* The backend add code should now search for the parent again. */
        rc = slapi_setbit_int(rc, SLAPI_RTN_BIT_FETCH_NEWPARENT_ENTRY);
        PROFILE_POINT; /* ModRDN Conflict; Absent Target Parent - Change to Lost and Found entry */
        goto bailout;
    }

bailout:
    if (del_old_replconflict_attr && rc == 0) {
        del_replconflict_attr(target_entry, opcsn, 0);
    }
    if (parentdn)
        slapi_sdn_free(&parentdn);
    return rc;
}

/*
 * Return 0 for OK, -1 for Error
 */
int
urp_delete_operation(Slapi_PBlock *pb)
{
    Slapi_Entry *deleteentry;
    CSN *opcsn = NULL;
    char sessionid[REPL_SESSION_ID_SIZE];
    int op_result = 0;
    int rc = SLAPI_PLUGIN_SUCCESS; /* OK */

    if (slapi_op_abandoned(pb)) {
        return rc;
    }

    slapi_pblock_get(pb, SLAPI_DELETE_EXISTING_ENTRY, &deleteentry);

    get_repl_session_id(pb, sessionid, &opcsn);
    if (deleteentry == NULL) /* uniqueid can't be found */
    {
        op_result = LDAP_NO_SUCH_OBJECT;
        slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
        rc = SLAPI_PLUGIN_FAILURE; /* Don't apply the Delete */
        slapi_log_err(slapi_log_urp, sessionid,
                      "urp_delete_operation - Entry %s does not exist; returning NO_SUCH_OBJECT.\n",
                      slapi_entry_get_dn((Slapi_Entry *)deleteentry));
        PROFILE_POINT; /* Delete Operation; Entry not exist. */
    } else if (is_tombstone_entry(deleteentry)) {
        /* The entry is already a Tombstone,
         * either because the operation was already applied,
         * then ignore this delete.
         * check if the tombstone csn matches the operationcsn
         */
        if (is_deleted_at_csn(deleteentry, opcsn)) {
            op_result= LDAP_ALREADY_EXISTS;
            slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
            rc = SLAPI_PLUGIN_NOOP; /* Don't apply the Delete */
            slapi_log_err(slapi_log_urp, sessionid,
                           "urp_delete_operation - Entry \"%s\" is already a Tombstone.\n",
                           slapi_entry_get_dn_const(deleteentry));
        } else if (urp_delete_check_conflict (sessionid, deleteentry, opcsn)) {
            /* the other option is that an entry was turned into a conflict
             * when a conflicting  add was handled for an already delted entry
             * This means that now the conflict has also to be turned into a tombstone
             * Check if such a conflict exists - and turn to a tombstone
             */
            op_result= LDAP_SUCCESS;
            slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
            rc = SLAPI_PLUGIN_NOOP_COMMIT; /* Don't apply the Delete */
            slapi_log_err(slapi_log_urp, sessionid,
                           "urp_delete_operation - Deleted conflict entry instead of tombstone \"%s\"\n",
                           slapi_entry_get_dn_const(deleteentry));
        } else {
            /* no already deleted tombstone and no alternative to delete
             * do nothing
             */
            op_result= LDAP_OPERATIONS_ERROR;
            slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
            rc = SLAPI_PLUGIN_NOOP; /* Don't apply the Delete */
            slapi_log_err(slapi_log_urp, sessionid,
                           "urp_delete_operation - Entry \"%s\" cannot be deleted.\n",
                           slapi_entry_get_dn_const(deleteentry));
        }

        PROFILE_POINT; /* Delete Operation; Already a Tombstone. */
    } else             /* The entry to be deleted exists and is not a tombstone */
    {
        get_repl_session_id(pb, sessionid, &opcsn);

        /* Check if the entry has children. */
        if (!slapi_entry_has_children(deleteentry)) {
            /* Remove possible conflict attributes */
            rc = SLAPI_PLUGIN_SUCCESS; /* OK, to delete the entry */
            if (is_conflict_entry(deleteentry)) {
                Slapi_DN *sdn = NULL;
                slapi_pblock_get(pb, SLAPI_DELETE_TARGET_SDN, &sdn);
                if (0 == slapi_sdn_compare(sdn, slapi_entry_get_sdn(deleteentry))) {
                    /* the delete directly targetd the conflict entry, just continue */
                } else {
                    /* check if there is a valid entry added before the delete,
                     * then we need to delete this entry
                     */
                    char *valid_uniqueid = urp_find_valid_entry_to_delete(pb, deleteentry, sessionid, opcsn);
                    if (valid_uniqueid) {
                        /* do we need to free SLAPI_TARGET_UNIQUEID ? */
                        slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void*)valid_uniqueid);
                        rc = slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_UNIQUEID_ENTRY);
                    } else { 
                        del_replconflict_attr (deleteentry, opcsn, 0);
                        rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
                    }
                }
            }  else if (is_renamed_entry(pb, deleteentry, opcsn)) {
                /* the entry was renamed before the delete, ignore the delete */
                op_result= LDAP_SUCCESS;
                slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
                rc = SLAPI_PLUGIN_NOOP; /* Don't apply the Delete */
            }
            PROFILE_POINT; /* Delete Operation; OK. */
        } else {
            /* the entry has children, so it is either the orphan-glue
             * scenario or the more complex situatin when there is a conflict entry
             * for the entry to be deleted which would be restored in the post_delete
             * phase. We would just have to swap the entries and be done,
             * but unfortunately as a plugin this is not possible.
             * So the approach is:
             * - move the children to th conflict entry
             * - let the delete happen
             * - let the post_del rename the conflict entry
             */
            Slapi_Entry *conflict_entry = urp_get_min_naming_conflict_entry (pb, slapi_entry_get_dn_const(deleteentry), sessionid, opcsn);
            if (conflict_entry) {
                urp_rename_conflict_children(slapi_entry_get_dn_const(deleteentry),
                                             slapi_entry_get_sdn_const(conflict_entry));
                slapi_entry_free(conflict_entry);
                rc = SLAPI_PLUGIN_SUCCESS; /* OK, to delete the entry */
            } else {
                /* Turn this entry into a glue_absent_parent entry */
                rc = entry_to_glue(sessionid, deleteentry, REASON_RESURRECT_ENTRY, opcsn);

                /* Turn the Delete into a No-Op */
                op_result= LDAP_SUCCESS;
                slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
                if (rc == 0) {
                    /* Don't apply the Delete, but commit the glue change */
                    rc = SLAPI_PLUGIN_NOOP_COMMIT;
                } else {
                    rc = SLAPI_PLUGIN_NOOP; /* Don't apply the Delete */
                }
                slapi_log_err(slapi_log_urp, sessionid,
                               "urp_delete_operation - Turn entry \"%s\" into a glue_absent_parent entry.\n",
                                 slapi_entry_get_dn_const(deleteentry));
                PROFILE_POINT; /* Delete Operation; Entry has children. */
            }
        }
    }
    return rc;
}


/*
 * Return 0 for OK, -1 for Error
 */
int
urp_post_add_operation(Slapi_PBlock *pb)
{
    char sessionid[REPL_SESSION_ID_SIZE];
    Slapi_Operation *op;
    CSN *opcsn;
    Slapi_Entry *addentry;
    char *conflict_dn = NULL;
    const char *valid_dn = NULL;
    int rc = 0;

    slapi_pblock_get( pb, SLAPI_URP_NAMING_COLLISION_DN, &conflict_dn );

    if (conflict_dn) {
        /* the existing entry was turned into a conflict,
         * move children to the valid entry
         */
        slapi_pblock_get( pb, SLAPI_OPERATION, &op);
        get_repl_session_id (pb, sessionid, &opcsn);
        slapi_log_err(SLAPI_LOG_REPL, sessionid,
                      "urp_post_add_operation - Entry %s is conflict entry, check for children\n",
                      conflict_dn);
        slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &addentry );
        valid_dn = slapi_entry_get_dn_const(addentry);
        slapi_log_err(SLAPI_LOG_REPL, sessionid,
                      "urp_post_add_operation - Entry %s is valid entry, check for children\n",
                      valid_dn);
        rc = urp_rename_conflict_children(conflict_dn, slapi_entry_get_sdn_const(addentry));
    }
    slapi_ch_free_string(&conflict_dn);
    slapi_pblock_set( pb, SLAPI_URP_NAMING_COLLISION_DN, NULL );
    slapi_pblock_get( pb, SLAPI_URP_TOMBSTONE_CONFLICT_DN, &conflict_dn );

    if (conflict_dn) {
    /* a tombstone was tirned into a conflict
     * move it to a valid parent if parent is also a conflict
     */
        Slapi_DN *conflict_sdn = slapi_sdn_new_dn_byval(conflict_dn);
        char *parent_dn = slapi_dn_parent(conflict_dn);
        slapi_pblock_get( pb, SLAPI_OPERATION, &op);
        get_repl_session_id (pb, sessionid, &opcsn);
        slapi_log_err(SLAPI_LOG_REPL, sessionid,
                      "urp_post_add_operation - Entry %s is conflict from tombstone, check parent\n",
                      conflict_dn);
        rc = tombstone_to_conflict_check_parent(sessionid, parent_dn, NULL, NULL, opcsn, conflict_sdn);
        slapi_sdn_free(&conflict_sdn);
        slapi_ch_free_string(&parent_dn);
    }
    return rc;
}

int
urp_post_modrdn_operation(Slapi_PBlock *pb)
{
    CSN *opcsn;
    char sessionid[REPL_SESSION_ID_SIZE];
    char *tombstone_uniqueid;
    Slapi_Entry *postentry;
    Slapi_Operation *op;
    int rc = SLAPI_PLUGIN_SUCCESS; /* OK */
    int oprc = 0;

    /* only execute if operation was successful */
    slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc);
    if (oprc) return rc;

    /*
     * Do not abandon the post op - the processed CSN needs to be
     * committed to keep the consistency between the changelog
     * and the backend DB.
     * if ( slapi_op_abandoned(pb) ) return 0;
     */

    slapi_pblock_get(pb, SLAPI_URP_TOMBSTONE_UNIQUEID, &tombstone_uniqueid);
    if (tombstone_uniqueid == NULL) {
        /*
         * The entry is not resurrected from tombstone. Hence
         * we need to check if any naming conflict with its
         * old dn can be resolved.
         */
        slapi_pblock_get(pb, SLAPI_OPERATION, &op);
        if (!operation_is_flag_set(op, OP_FLAG_REPL_FIXUP)) {
            get_repl_session_id(pb, sessionid, &opcsn);
            urp_naming_conflict_removal(pb, sessionid, opcsn, "MODRDN");
            /* keep track of the entry's lifetime befor renaming
             * by creating a cenotaph
             */
            urp_fixup_add_cenotaph(pb, sessionid, opcsn);
        }
    } else {
        /*
         * The entry was a resurrected tombstone.
         * This could happen when we applied a rename
         * to a tombstone to avoid server divergence. Now
         * it's time to put the entry back to tombstone.
         */
        slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &postentry);
        if (postentry && strcmp(tombstone_uniqueid, slapi_entry_get_uniqueid(postentry)) == 0) {
            entry_to_tombstone(pb, postentry);
        }
        slapi_ch_free((void **)&tombstone_uniqueid);
        slapi_pblock_set(pb, SLAPI_URP_TOMBSTONE_UNIQUEID, NULL);
    }

    return rc;
}

/*
 * Conflict removal
 */
int
urp_post_delete_operation(Slapi_PBlock *pb)
{
    Slapi_Operation *op;
    Slapi_Entry *entry;
    CSN *opcsn;
    char sessionid[REPL_SESSION_ID_SIZE];
    int op_result;
    int oprc = 0;

    /* only execute if operation was successful */
    slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc);
    if (oprc) return 0;

    /*
     * Do not abandon the post op - the processed CSN needs to be
     * committed to keep the consistency between the changelog
     * and the backend DB
     * if ( slapi_op_abandoned(pb) ) return 0;
     */

    get_repl_session_id(pb, sessionid, &opcsn);

    /*
     * Conflict removal from the parent entry:
     * If the parent is glue and has no more children,
     * turn the parent to tombstone
     */
    slapi_pblock_get(pb, SLAPI_DELETE_GLUE_PARENT_ENTRY, &entry);
    if (entry != NULL) {
        op_result = entry_to_tombstone(pb, entry);
        if (op_result == LDAP_SUCCESS) {
            slapi_log_err(slapi_log_urp, sessionid,
                          "urp_post_delete_operation - Tombstoned glue entry %s since it has no more children\n",
                          slapi_entry_get_dn_const(entry));
        }
    }

    slapi_pblock_get(pb, SLAPI_OPERATION, &op);
    if (!operation_is_flag_set(op, OP_FLAG_REPL_FIXUP)) {
        /*
         * Conflict removal from the peers of the old dn
         */
        urp_naming_conflict_removal(pb, sessionid, opcsn, "DEL");
    }

    return 0;
}

static int
urp_fixup_add_cenotaph(Slapi_PBlock *pb, char *sessionid, CSN *opcsn)
{
    Slapi_PBlock *add_pb;
    Slapi_Entry *cenotaph = NULL;
    Slapi_Entry *pre_entry = NULL;
    int ret = 0;
    Slapi_DN *pre_sdn = NULL;
    Slapi_RDN *rdn = NULL;
    char *parentdn = NULL;
    char *newdn;
    const char *entrydn;
    const char *uniqueid = NULL;
    CSN *dncsn = NULL;
    char csnstr[CSN_STRSIZE+1];

    slapi_pblock_get (pb, SLAPI_ENTRY_PRE_OP, &pre_entry);
    if (pre_entry == NULL) {
        slapi_log_err(SLAPI_LOG_ERR, sessionid,
                       "urp_fixup_add_cenotaph - failed to get preop entry\n");
        return -1;
    }

    slapi_pblock_get(pb, SLAPI_MODRDN_TARGET_SDN, &pre_sdn);
    entrydn = slapi_sdn_get_ndn(pre_sdn);
    /* pre_sdn = slapi_entry_get_sdn(pre_entry); 
       entrydn = slapi_entry_get_ndn (pre_entry);*/
    uniqueid = slapi_entry_get_uniqueid (pre_entry);
    parentdn = slapi_dn_parent(entrydn);
    rdn = slapi_rdn_new();
    slapi_sdn_get_rdn(pre_sdn, rdn);
    slapi_rdn_remove_attr (rdn, SLAPI_ATTR_UNIQUEID );
    slapi_rdn_add(rdn, "cenotaphID", uniqueid);
    newdn = slapi_ch_smprintf("%s,%s", slapi_rdn_get_rdn(rdn), parentdn);
    slapi_rdn_free(&rdn);
    slapi_ch_free_string(&parentdn);
    /* slapi_sdn_free(&pre_sdn); */

    cenotaph = slapi_entry_alloc();
    slapi_entry_init(cenotaph, slapi_ch_strdup(newdn), NULL);

    dncsn = (CSN *)entry_get_dncsn (pre_entry);
    slapi_entry_add_string(cenotaph, SLAPI_ATTR_OBJECTCLASS, "extensibleobject");
    slapi_entry_add_string(cenotaph, SLAPI_ATTR_OBJECTCLASS, "nstombstone");
    slapi_entry_add_string(cenotaph,"cenotaphfrom", csn_as_string(dncsn, PR_FALSE, csnstr));
    slapi_entry_add_string(cenotaph,"cenotaphto", csn_as_string(opcsn, PR_FALSE, csnstr));
    slapi_entry_add_string(cenotaph,"nstombstonecsn", csn_as_string(opcsn, PR_FALSE, csnstr));
    slapi_entry_add_string(cenotaph, SLAPI_ATTR_NSCP_ENTRYDN, entrydn);

    slapi_log_err(SLAPI_LOG_REPL, sessionid,
                   "urp_fixup_add_cenotaph - addinng cenotaph: %s \n", newdn);
    add_pb = slapi_pblock_new();
    slapi_pblock_init(add_pb);

    slapi_add_entry_internal_set_pb(add_pb,
                                    cenotaph,
                                    NULL,
                                    repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
                                    OP_FLAG_REPL_FIXUP|OP_FLAG_NOOP|OP_FLAG_CENOTAPH_ENTRY|SLAPI_OP_FLAG_BYPASS_REFERRALS);
    slapi_add_internal_pb(add_pb);
    slapi_pblock_get(add_pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
    slapi_pblock_destroy(add_pb);

    if (ret == LDAP_ALREADY_EXISTS) {
        /* the cenotaph already exists, probably because of a loop
         * in renaming entries. Update it with new csns
         */
        slapi_log_err(SLAPI_LOG_REPL, sessionid,
                       "urp_fixup_add_cenotaph - cenotaph (%s) already exists, updating\n", newdn);
        Slapi_PBlock *mod_pb = slapi_pblock_new();
        Slapi_Mods smods;
        Slapi_DN *sdn = slapi_sdn_new_dn_byval(newdn);
        slapi_mods_init(&smods, 4);
        slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "cenotaphfrom", csn_as_string(dncsn, PR_FALSE, csnstr));
        slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "cenotaphto", csn_as_string(opcsn, PR_FALSE, csnstr));
        slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "nstombstonecsn", csn_as_string(opcsn, PR_FALSE, csnstr));

        slapi_modify_internal_set_pb_ext(
            mod_pb,
            sdn,
            slapi_mods_get_ldapmods_byref(&smods),
            NULL, /* Controls */
            NULL,
            repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
            OP_FLAG_REPL_FIXUP|OP_FLAG_NOOP|OP_FLAG_CENOTAPH_ENTRY|SLAPI_OP_FLAG_BYPASS_REFERRALS);

        slapi_modify_internal_pb(mod_pb);
        slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
        if (ret != LDAP_SUCCESS) {
            slapi_log_err(SLAPI_LOG_ERR, sessionid,
                       "urp_fixup_add_cenotaph - failed to modify cenotaph, err= %d\n", ret);
        }
        slapi_mods_done(&smods);
        slapi_sdn_free(&sdn);
        slapi_pblock_destroy(mod_pb);

    } else if (ret != LDAP_SUCCESS) {
        slapi_log_err(SLAPI_LOG_ERR, sessionid,
                       "urp_fixup_add_cenotaph - failed to add cenotaph, err= %d\n", ret);
    }
    slapi_ch_free_string(&newdn);

    return ret;
}

int
urp_fixup_add_entry(Slapi_Entry *e, const char *target_uniqueid, const char *parentuniqueid, CSN *opcsn, int opflags)
{
    Slapi_PBlock *newpb;
    Slapi_Operation *op;
    int op_result;

    newpb = slapi_pblock_new();

    /*
     * Mark this operation as replicated, so that the front end
     * doesn't add extra attributes.
     */
    slapi_add_entry_internal_set_pb(
        newpb,
        e,    /* entry will be consumed */
        NULL, /*Controls*/
        repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
        OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | opflags);
    if (target_uniqueid) {
        slapi_pblock_set(newpb, SLAPI_TARGET_UNIQUEID, (void *)target_uniqueid);
    }
    if (parentuniqueid) {
        struct slapi_operation_parameters *op_params;
        slapi_pblock_get(newpb, SLAPI_OPERATION_PARAMETERS, &op_params);
        op_params->p.p_add.parentuniqueid = (char *)parentuniqueid; /* Consumes parentuniqueid */
    }
    slapi_pblock_get(newpb, SLAPI_OPERATION, &op);
    operation_set_csn(op, opcsn);

    slapi_add_internal_pb(newpb);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
    slapi_pblock_destroy(newpb);

    return op_result;
}

int
urp_fixup_modrdn_entry (const Slapi_DN *entrydn, const char *newrdn, const Slapi_DN *newsuperior, const char *entryuniqueid, const char *parentuniqueid, CSN *opcsn, int opflags)
{
    Slapi_PBlock *newpb;
    Slapi_Operation *op;
    int op_result;

    slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
                  "urp_fixup_modrdn_entry: moving entry (%s) to (%s) as (%s)\n",
                   slapi_sdn_get_dn(entrydn), slapi_sdn_get_dn(newsuperior),newrdn);
    /* log only
    return 0; */
    newpb = slapi_pblock_new();

    slapi_rename_internal_set_pb_ext(newpb,
                                     entrydn,
                                     newrdn,
                                     newsuperior,
                                     0,
                                     NULL,
                                     entryuniqueid,
                                     repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
                                     OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | opflags);

    /* set operation csn if provided */
    if (opcsn) {
        slapi_pblock_get (newpb, SLAPI_OPERATION, &op);
        operation_set_csn (op, opcsn);
    }
    if (parentuniqueid) {
        struct slapi_operation_parameters *op_params;
        slapi_pblock_get( newpb, SLAPI_OPERATION_PARAMETERS, &op_params );
        op_params->p.p_modrdn.modrdn_newsuperior_address.uniqueid = (char*)parentuniqueid;
    }
    slapi_modrdn_internal_pb(newpb); 
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);

    slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
                  "urp_fixup_modrdn_entry: moving entry (%s) result %d\n",
                  slapi_sdn_get_dn(entrydn), op_result);
    slapi_pblock_destroy(newpb);
    return op_result;
}

int
urp_fixup_rename_entry(const Slapi_Entry *entry, const char *newrdn, const char *parentuniqueid, int opflags)
{
    Slapi_PBlock *newpb;
    Slapi_Operation *op;
    CSN *opcsn;
    int op_result;

    newpb = slapi_pblock_new();

    /*
     * Must mark this operation as replicated,
     * so that the frontend doesn't add extra attributes.
     */
    slapi_rename_internal_set_pb_ext(
        newpb,
        slapi_entry_get_sdn_const(entry),
        newrdn,                          /*NewRDN*/
        NULL,                            /*NewSuperior*/
        0,                               /* !Delete Old RDNS */
        NULL,                            /*Controls*/
        slapi_entry_get_uniqueid(entry), /*uniqueid*/
        repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
        OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | opflags);

    /* set operation csn to the entry's dncsn */
    opcsn = (CSN *)entry_get_dncsn(entry);
    slapi_pblock_get(newpb, SLAPI_OPERATION, &op);
    operation_set_csn(op, opcsn);
    if (parentuniqueid) {
        struct slapi_operation_parameters *op_params;
        slapi_pblock_get(newpb, SLAPI_OPERATION_PARAMETERS, &op_params);
        op_params->p.p_modrdn.modrdn_newsuperior_address.uniqueid = (char *)parentuniqueid;
    }
    slapi_modrdn_internal_pb(newpb);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);

    slapi_pblock_destroy(newpb);
    return op_result;
}

int
urp_fixup_move_entry (const Slapi_Entry *entry, const Slapi_DN *newsuperior, int opflags)
{
    Slapi_PBlock *newpb;
    int op_result;

    newpb = slapi_pblock_new();

    /* 
     * Must mark this operation as replicated,
     * so that the frontend doesn't add extra attributes.
     */
    slapi_rename_internal_set_pb_ext(newpb,
                                     slapi_entry_get_sdn_const (entry),
                                     slapi_entry_get_rdn_const(entry), /*NewRDN*/
                                     newsuperior, /*NewSuperior*/
                                     0, /* !Delete Old RDNS */
                                     NULL, /*Controls*/
                                     slapi_entry_get_uniqueid (entry), /*uniqueid*/
                                     repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
                                     OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | opflags);

    /* set operation csn to the entry's dncsn */
	/* TBD check which csn to use 
    Slapi_Operation *op;
	CSN *opcsn;
	opcsn = (CSN *)entry_get_dncsn (entry);
    slapi_pblock_get (newpb, SLAPI_OPERATION, &op);
    operation_set_csn (op, opcsn);
TBD */
    slapi_modrdn_internal_pb(newpb); 
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);

    slapi_pblock_destroy(newpb);
    return op_result;
}

int
urp_fixup_delete_entry(const char *uniqueid, const char *dn, CSN *opcsn, int opflags)
{
    Slapi_PBlock *newpb;
    Slapi_Operation *op;
    int op_result;

    newpb = slapi_pblock_new();

    /*
     * Mark this operation as replicated, so that the front end
     * doesn't add extra attributes.
     */
    slapi_delete_internal_set_pb(
        newpb,
        dn,
        NULL,     /*Controls*/
        uniqueid, /*uniqueid*/
        repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
        OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | opflags);
    slapi_pblock_get(newpb, SLAPI_OPERATION, &op);
    operation_set_csn(op, opcsn);

    slapi_delete_internal_pb(newpb);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
    slapi_pblock_destroy(newpb);

    return op_result;
}

int
urp_fixup_modify_entry(const char *uniqueid, const Slapi_DN *sdn, CSN *opcsn, Slapi_Mods *smods, int opflags)
{
    Slapi_PBlock *newpb;
    Slapi_Operation *op;
    int op_result;

    newpb = slapi_pblock_new();

    slapi_modify_internal_set_pb_ext(
        newpb,
        sdn,
        slapi_mods_get_ldapmods_byref(smods),
        NULL, /* Controls */
        uniqueid,
        repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
        OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | opflags);

    /* set operation csn */
    slapi_pblock_get(newpb, SLAPI_OPERATION, &op);
    operation_set_csn(op, opcsn);

    /* do modify */
    slapi_modify_internal_pb(newpb);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
    slapi_pblock_destroy(newpb);

    return op_result;
}

static int
urp_add_resolve_parententry(Slapi_PBlock *pb, char *sessionid, Slapi_Entry *entry, Slapi_Entry *parententry, CSN *opcsn)
{
    Slapi_DN *parentdn = NULL;
    Slapi_RDN *add_rdn = NULL;
    char *newdn = NULL;
    int ldap_rc;
    int rc = 0;
    Slapi_DN *sdn = NULL;

    if (is_suffix_entry(pb, entry, &parentdn)) {
        /* It's OK for the suffix entry's parent to be absent */
        rc = 0;
        PROFILE_POINT; /* Add Conflict; Suffix Entry */
        goto bailout;
    }

    /* The entry is not a suffix. */
    if (parententry == NULL) /* The parent entry was not found. */
    {
        /* Create a glue entry to stand in for the absent parent */
        slapi_operation_parameters *op_params;
        slapi_pblock_get(pb, SLAPI_OPERATION_PARAMETERS, &op_params);
        ldap_rc = create_glue_entry(pb, sessionid, parentdn, op_params->p.p_add.parentuniqueid, opcsn);
        if (LDAP_SUCCESS == ldap_rc) {
            /* The backend code should now search for the parent again. */
            rc = slapi_setbit_int(rc, SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
            rc = slapi_setbit_int(rc, SLAPI_RTN_BIT_FETCH_PARENT_ENTRY);
            PROFILE_POINT; /* Add Conflict; Orphaned Entry; Glue Parent */
        } else {
            /*
             * Error. The parent can't be created as a glue entry.
             * This will cause replication divergence and will
             * require admin intercession
             */
            ldap_rc = LDAP_OPERATIONS_ERROR;
            slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_rc);
            rc = -1;       /* Abort this Operation */
            PROFILE_POINT; /* Add Conflict; Orphaned Entry; Impossible to create parent; Refuse Change. */
        }
        goto bailout;
    }

    if (is_tombstone_entry(parententry)) /* The parent is a tombstone */
    {
        /* The parent entry must be resurected from the dead. */
        /* parentdn retrieved from entry is not tombstone dn. */
        ldap_rc = tombstone_to_glue(pb, sessionid, parententry, parentdn, REASON_RESURRECT_ENTRY, opcsn, NULL);
        if (ldap_rc != LDAP_SUCCESS) {
            ldap_rc = LDAP_OPERATIONS_ERROR;
            slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_rc);
            rc = -1; /* Abort the operation */
        } else {
            /* The backend add code should now search for the parent again. */
            rc = slapi_setbit_int(rc, SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
            rc = slapi_setbit_int(rc, SLAPI_RTN_BIT_FETCH_PARENT_ENTRY);
        }
        PROFILE_POINT; /* Add Conflict; Orphaned Entry; Parent Was Tombstone */
        goto bailout;
    }

    if(is_conflict_entry(parententry)) {
        /* The parent is a conflict entry
         * Check if a valid entry exists and make this the new parent
         *
         * or if no valid entry exists
         * - check if also a tombstone for the parentdn exists
         * - compare csns of tombstone and conflict generation
         * - turn the latest into a valid glue
         */
        char *valid_nsuniqueid = urp_get_valid_parent_nsuniqueid(parentdn);
        if (valid_nsuniqueid) {
            slapi_log_err(SLAPI_LOG_REPL, sessionid,
                          "urp_resolve parent entry: found valid parent %s\n", slapi_sdn_get_dn(parentdn));
            struct slapi_operation_parameters *op_params;
            slapi_pblock_get( pb, SLAPI_OPERATION_PARAMETERS, &op_params );
            /* free it ? */
            op_params->p.p_add.parentuniqueid = valid_nsuniqueid;
        } else {
            int op_result;
            char *tombstone_nsuniqueid = urp_find_tombstone_for_glue(pb, sessionid, parententry, parentdn, opcsn);
            slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
            if (tombstone_nsuniqueid) {
                struct slapi_operation_parameters *op_params;
                slapi_pblock_get( pb, SLAPI_OPERATION_PARAMETERS, &op_params );
                op_params->p.p_add.parentuniqueid = tombstone_nsuniqueid;
                rc = LDAP_SUCCESS;
            } else if (0 == op_result) {
                rc = urp_conflict_to_glue(sessionid, parententry, parentdn, opcsn);
                if (rc == 0) {
                    slapi_log_err(SLAPI_LOG_REPL, sessionid,
                                  "urp_resolve parent entry: created valid parent from conflict %s\n", slapi_sdn_get_dn(parentdn));
                } else {
                    slapi_log_err(SLAPI_LOG_REPL, sessionid,
                                  "urp_resolve parent entry: cannot resolve parent %s\n", slapi_sdn_get_dn(parentdn));
                }
            }
        }
        goto bailout;
    }

    /* The parent is healthy */
    /* Now we need to check that the parent has the correct DN */
    if (slapi_sdn_isparent(slapi_entry_get_sdn(parententry), slapi_entry_get_sdn(entry))) {
        rc = 0;        /* OK, Add the entry */
        PROFILE_POINT; /* Add Conflict; Parent Exists */
        goto bailout;
    }

    /*
     * Parent entry doesn't have a DN parent to the entry.
     * This can happen if parententry was renamed due to
     * conflict and the child entry was created before
     * replication occured. See defect 530942.
     * We need to rename the entry to be child of its parent.
     */
    add_rdn = slapi_rdn_new_dn(slapi_entry_get_dn_const(entry));
    newdn = slapi_dn_plus_rdn(slapi_entry_get_dn_const(parententry), slapi_rdn_get_rdn(add_rdn));
    slapi_entry_set_normdn(entry, newdn);

    /* slapi_pblock_get(pb, SLAPI_ADD_TARGET, &dn); */
    slapi_pblock_get(pb, SLAPI_ADD_TARGET_SDN, &sdn);
    slapi_sdn_free(&sdn);

    sdn = slapi_sdn_dup(slapi_entry_get_sdn_const(entry));
    slapi_pblock_set(pb, SLAPI_ADD_TARGET_SDN, sdn);

    slapi_log_err(slapi_log_urp, sessionid,
                  "Parent was renamed. Renamed the child to %s\n", newdn);
    rc = slapi_setbit_int(rc, SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
    PROFILE_POINT; /* Add Conflict; Parent Renamed; Rename Operation Entry */

bailout:
    if (parentdn)
        slapi_sdn_free(&parentdn);
    slapi_rdn_free(&add_rdn);
    return rc;
}
static int
urp_add_new_entry_to_conflict (Slapi_PBlock *pb, char *sessionid, Slapi_Entry *addentry, CSN *opcsn)
{
    int rc = 0;
    int op_result;

    /* Entry to be added is a loser */
    char *basedn = slapi_entry_get_ndn(addentry);
    const char *adduniqueid = slapi_entry_get_uniqueid(addentry);
    char *newdn= get_dn_plus_uniqueid(sessionid, (const Slapi_DN *)addentry, adduniqueid);
    if(newdn == NULL) {
        op_result= LDAP_OPERATIONS_ERROR;
        slapi_pblock_set(pb, SLAPI_RESULT_CODE, &op_result);
        rc = SLAPI_PLUGIN_NOOP; /* Abort this Operation */
        slapi_log_err(slapi_log_urp, sessionid,
                       "urp_add_operation - %s - Add conflict; Unique ID (%s) already in RDN\n",
                       basedn, adduniqueid);
    } else {
        /* Add the nsds5ReplConflict attribute in the mods */
        Slapi_Attr *attr = NULL;
        Slapi_Value **vals = NULL;
        Slapi_Value **csn_vals = NULL;
        Slapi_DN *sdn = NULL;
        Slapi_RDN *rdn;
        char buf[BUFSIZ];
        char csnstr[CSN_STRSIZE+1];

        PR_snprintf(buf, BUFSIZ, "%s (ADD) %s", REASON_ANNOTATE_DN, basedn);
        if (slapi_entry_attr_find(addentry, ATTR_NSDS5_REPLCONFLICT, &attr) == 0) {
            /* ATTR_NSDS5_REPLCONFLICT exists */
            slapi_log_err(SLAPI_LOG_REPL, sessionid,
                          "urp_add_operation - New entry has nsds5ReplConflict already\n");
            vals = attr_get_present_values (attr); /* this returns a pointer to the contents */
        }
        if ( vals == NULL || *vals == NULL ) {
            /* Add new attribute */
            slapi_entry_add_string(addentry, ATTR_NSDS5_REPLCONFLICT, buf);
        } else {
            /*
             * Replace old attribute. We don't worry about the index
             * change here since the entry is yet to be added.
             */
            slapi_value_set_string(*vals, buf);
        }

        /* add the ldapsubentry objectclass if not present */
        slapi_entry_attr_find(addentry, "objectclass", &attr);
        if (attr != NULL) {
            struct berval bv;
            bv.bv_val = "ldapsubentry";
            bv.bv_len = strlen(bv.bv_val);
            if (slapi_attr_value_find(attr, &bv) != 0) {
                Slapi_Value *new_v = slapi_value_new();
                slapi_value_init_berval(new_v, &bv);
                slapi_attr_add_value(attr, new_v);
                slapi_value_free(&new_v);
                slapi_entry_set_flag(addentry, SLAPI_ENTRY_FLAG_LDAPSUBENTRY);
            }
        }
        /* add or replace the conflict csn */
        if (slapi_entry_attr_find (addentry, "conflictcsn", &attr) == 0) {
            csn_vals = attr_get_present_values (attr);
        }
        if (csn_vals == NULL || *csn_vals == NULL ) {
            slapi_entry_add_string(addentry,"conflictcsn", csn_as_string(opcsn, PR_FALSE, csnstr));
        } else {

            slapi_value_set_string (*csn_vals, csn_as_string(opcsn, PR_FALSE, csnstr));
        }

        /* slapi_pblock_get(pb, SLAPI_ADD_TARGET, &dn); */
        slapi_pblock_get(pb, SLAPI_ADD_TARGET_SDN, &sdn);
        slapi_sdn_free(&sdn);

        slapi_entry_set_normdn(addentry, newdn); /* dn: passin */

        sdn = slapi_sdn_dup(slapi_entry_get_sdn_const(addentry));
        slapi_pblock_set(pb, SLAPI_ADD_TARGET_SDN, sdn);

        rdn = slapi_rdn_new_sdn ( slapi_entry_get_sdn_const(addentry) );
        slapi_log_err(SLAPI_LOG_REPL, sessionid,
                         "urp_add_operation - Naming conflict ADD. Add %s instead\n",
                         slapi_rdn_get_rdn(rdn));
        slapi_rdn_free(&rdn);
        rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);

    }
    return rc;
}

static int
urp_add_check_tombstone (Slapi_PBlock *pb, char *sessionid, Slapi_Entry *entry, CSN *opcsn)
{
    int rc = 0;
    int op_result;
    Slapi_Entry **entries = NULL;
    Slapi_PBlock *newpb;
    char *basedn = slapi_entry_get_ndn(entry);
    char *escaped_filter;
    const Slapi_DN *suffix = slapi_get_suffix_by_dn(slapi_entry_get_sdn (entry));
    escaped_filter = slapi_filter_escape_filter_value("nscpentrydn", basedn);

    char *filter = slapi_filter_sprintf("(&(objectclass=nstombstone)%s)", escaped_filter);
    slapi_ch_free((void **)&escaped_filter);
    newpb = slapi_pblock_new();
    slapi_search_internal_set_pb(newpb,
                                 slapi_sdn_get_dn(suffix), /* Base DN */
                                 LDAP_SCOPE_SUBTREE,
                                 filter,
                                 NULL, /* Attrs */
                                 0, /* AttrOnly */
                                 NULL, /* Controls */
                                 NULL, /* UniqueID */
                                 repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
                                 0);
    slapi_search_internal_pb(newpb);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
    if ( (op_result != LDAP_SUCCESS) || (entries == NULL) )
    {
        /* Log a message */
        goto done;
    }
    for (int i = 0; entries && (entries[i] != NULL); i++) {
        /* we need to distinguish between normal tombstones abd cenotaphs */
        int is_cenotaph;
        CSN *from_csn = NULL;
        CSN *to_csn = NULL;
        char *to_value = NULL;
        char *from_value = (char*)slapi_entry_attr_get_ref(entries[i], "cenotaphfrom");
        if (from_value) {
            is_cenotaph = 1;
            from_csn = csn_new_by_string(from_value);
            to_value = (char*)slapi_entry_attr_get_ref(entries[i], "cenotaphto");
            to_csn = csn_new_by_string(to_value);
        } else {
            is_cenotaph = 0;
            from_csn = csn_dup(entry_get_dncsn(entries[i]));
            to_value = (char*)slapi_entry_attr_get_ref(entries[i], "nstombstonecsn");
            to_csn = csn_new_by_string(to_value);
        }
        if (csn_compare(from_csn, opcsn) < 0 &&
            csn_compare(to_csn, opcsn) > 0 ) {
            slapi_log_err(SLAPI_LOG_REPL, sessionid,
                           "urp_add_check_tombstone - found conflicting tombstone (%s).\n",
                           slapi_entry_get_dn_const(entries[i]));
            rc = 1;
        } else if (!is_cenotaph && csn_compare(opcsn, from_csn) < 0) {
            /* the tombstone is from an entry added later than the received one */
            Slapi_Value *tomb_value;
            const char *adduniqueid = slapi_entry_get_uniqueid (entries[i]);
            const char *base_dn = slapi_entry_get_dn_const (entry);
            char *newrdn = get_rdn_plus_uniqueid ( sessionid, base_dn, adduniqueid );
            char *parentdn = slapi_dn_parent_ext(slapi_entry_get_dn_const(entries[i]),1);
            char *conflict_dn = slapi_ch_smprintf("%s,%s",newrdn, parentdn);
            slapi_log_err(SLAPI_LOG_REPL, sessionid,
                           "urp_add_check_tombstone - found tombstone for newer entry(%s) create conflict (%s).\n",
                           slapi_entry_get_dn_const(entries[i]),conflict_dn);
            Slapi_DN *conflict_sdn = slapi_sdn_new_dn_byval(conflict_dn);
            /* need to add the tombstone value here since we need the deletion csn */
            tomb_value = slapi_value_new_string(SLAPI_ATTR_VALUE_TOMBSTONE);
            value_update_csn(tomb_value, CSN_TYPE_VALUE_UPDATED, entry_get_deletion_csn(entries[i]));
            slapi_entry_add_value(entry, SLAPI_ATTR_OBJECTCLASS, tomb_value);
            slapi_value_free(&tomb_value);

            /* now turn the existing tombstone into a conflict */
            tombstone_to_conflict(sessionid, slapi_entry_dup(entries[i]), conflict_sdn, "tombstoneToConflict", opcsn, NULL);
            slapi_pblock_set(pb, SLAPI_URP_TOMBSTONE_CONFLICT_DN, conflict_dn);
            slapi_ch_free_string(&newrdn);
            slapi_ch_free_string(&parentdn);
            slapi_sdn_free(&conflict_sdn);
            rc = 2;
        }
        csn_free(&from_csn);
        csn_free(&to_csn);

        if (rc) break;
    }

done:
    slapi_free_search_results_internal(newpb);
    slapi_pblock_destroy(newpb);
    if (filter) {
        PR_smprintf_free(filter);
    }
    return rc;
}

static int
urp_delete_check_conflict (char *sessionid, Slapi_Entry *tombstone_entry, CSN *opcsn)
{
    int rc = 0;
    int op_result = 0;
    char *filter = NULL;
    Slapi_PBlock *newpb = NULL;
    Slapi_Entry **entries = NULL;
    Slapi_Entry *conflict_e = NULL;
    char *validdn = (char*)slapi_entry_attr_get_ref(tombstone_entry, "nscpentrydn");
    char *parent_dn = slapi_dn_parent (validdn);

    filter = slapi_filter_sprintf("(&(objectclass=ldapsubentry)(%s=%s (ADD) %s%s))", ATTR_NSDS5_REPLCONFLICT, REASON_ANNOTATE_DN,
                                  ESC_NEXT_VAL, validdn);
    newpb = slapi_pblock_new();
    slapi_search_internal_set_pb(newpb,
                                 parent_dn,
                                 LDAP_SCOPE_SUBTREE,
                                 filter,
                                 NULL, /* Attrs */
                                 0, /* AttrOnly */
                                 NULL, /* Controls */
                                 NULL, /* UniqueID */
                                 repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
                                 0);
    slapi_search_internal_pb(newpb);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);

    if ((op_result != LDAP_SUCCESS) || (entries == NULL) || entries[0] == NULL) {
        /* Log a message */
        goto done;
    }

    conflict_e = slapi_entry_dup(entries[0]);
    conflict_to_tombstone(sessionid, conflict_e, opcsn);
    slapi_entry_free(conflict_e);
    rc = 1;
done:
    slapi_free_search_results_internal(newpb);
    slapi_pblock_destroy(newpb);
    newpb = NULL;
    if (filter) {
        PR_smprintf_free(filter);
    }

    slapi_ch_free_string(&parent_dn);
    return rc;
}

static char*
urp_find_valid_entry_to_delete(Slapi_PBlock *pb, const Slapi_Entry *deleteentry, char *sessionid, CSN *opcsn)
{
    Slapi_DN *sdnp = NULL;
    const char *dn;
    char *delete_nsuniqueid = NULL;
    Slapi_PBlock *newpb;
    const CSN *dncsn;
    Slapi_Entry **entries = NULL;
    int op_result;

    slapi_pblock_get(pb, SLAPI_DELETE_TARGET_SDN, &sdnp);
    dn = slapi_sdn_get_dn(sdnp);
    newpb = slapi_pblock_new();
    slapi_search_internal_set_pb(newpb,
                                 dn, /* Base DN */
                                 LDAP_SCOPE_BASE,
                                 "objectclass=*",
                                 NULL, /* Attrs */
                                 0, /* AttrOnly */
                                 NULL, /* Controls */
                                 NULL, /* UniqueID */
                                 repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
                                 0);
    slapi_search_internal_pb(newpb);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
    if ((op_result != LDAP_SUCCESS) || (entries == NULL) || entries[0] == NULL) {
        /* Log a message */
        goto done;
    }
    dncsn = entry_get_dncsn(entries[0]);
    if (dncsn && (csn_compare(dncsn, opcsn) < 0 )) {
        delete_nsuniqueid = slapi_entry_attr_get_charptr(entries[0], "nsuniqueid");
        slapi_log_err(SLAPI_LOG_REPL, sessionid,
                    "urp_find_valid_entry_to_delete - found (%s) (%s).\n",
            delete_nsuniqueid, slapi_entry_get_dn_const(entries[0]));
    }

done:
    slapi_free_search_results_internal(newpb);
    slapi_pblock_destroy(newpb);
    return delete_nsuniqueid;
}

static char*
urp_find_tombstone_for_glue (Slapi_PBlock *pb, char *sessionid, const Slapi_Entry *entry, Slapi_DN *parentdn, CSN *opcsn)
{
    char *tombstone_nsuniqueid = NULL;
    int op_result;
    int rc = LDAP_SUCCESS;;
    Slapi_Entry **entries = NULL;
    Slapi_PBlock *newpb;
    const char *basedn = slapi_sdn_get_dn(parentdn);
    char *escaped_filter;
    escaped_filter = slapi_filter_escape_filter_value("nscpentrydn", (char *)basedn);

    char *conflict_csnstr = (char*)slapi_entry_attr_get_ref((Slapi_Entry *)entry, "conflictcsn");
    CSN *conflict_csn = csn_new_by_string(conflict_csnstr);
    CSN *tombstone_csn = NULL;

    char *filter = slapi_filter_sprintf("(&(objectclass=nstombstone)%s)", escaped_filter);
    slapi_ch_free((void **)&escaped_filter);
    newpb = slapi_pblock_new();
    char *parent_dn = slapi_dn_parent (basedn);
    slapi_search_internal_set_pb(newpb,
                                 parent_dn, /* Base DN */
                                 LDAP_SCOPE_SUBTREE,
                                 filter,
                                 NULL, /* Attrs */
                                 0, /* AttrOnly */
                                 NULL, /* Controls */
                                 NULL, /* UniqueID */
                                 repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
                                 0);
    slapi_search_internal_pb(newpb);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
    if ( (op_result != LDAP_SUCCESS) || (entries == NULL) )
    {
        /* Log a message */
        goto done;
    }
    for (int i = 0; entries && (entries[i] != NULL); i++) {
        char *tombstone_csn_value = (char*)slapi_entry_attr_get_ref(entries[i], "nstombstonecsn");
        if (tombstone_csn_value) {
            csn_free(&tombstone_csn);
            tombstone_csn = csn_new_by_string(tombstone_csn_value);
            if( csn_compare(tombstone_csn, conflict_csn) > 0 ) {
                slapi_log_err(SLAPI_LOG_REPL, sessionid,
                               "urp_find_tombstone_for_glue - found tombstone newer than conflict (%s).\n",
                               slapi_entry_get_dn_const(entries[i]));
                tombstone_nsuniqueid = slapi_entry_attr_get_charptr(entries[i], "nsuniqueid");
                rc = tombstone_to_glue (pb, sessionid, entries[i], parentdn, REASON_RESURRECT_ENTRY, opcsn, NULL);
                if (rc) {
                    slapi_log_err(SLAPI_LOG_ERR, sessionid,
                                  "urp_resolve parent entry: failed to create glue from tombstone %s\n", slapi_sdn_get_dn(parentdn));
                    slapi_ch_free_string(&tombstone_nsuniqueid);
                    tombstone_nsuniqueid = NULL;
                }
                break;
            }
        }
    }
    csn_free(&tombstone_csn);

done:
    slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
    csn_free(&conflict_csn);
    slapi_ch_free_string(&parent_dn);
    slapi_free_search_results_internal(newpb);
    slapi_pblock_destroy(newpb);
    if (filter) {
        PR_smprintf_free(filter);
    }
    return tombstone_nsuniqueid;
}

static int
urp_conflict_to_glue (char *sessionid, const Slapi_Entry *entry, Slapi_DN *parentdn, CSN *opcsn)
{
    int rc = 0;
    int op_result;
    const char *newrdn;
    Slapi_RDN *parentrdn = slapi_rdn_new();
    const char *basedn;

    basedn = slapi_entry_get_dn_const (entry);
    slapi_sdn_get_rdn(parentdn, parentrdn);
    newrdn = slapi_rdn_get_rdn(parentrdn);
    if(newrdn) {
        del_replconflict_attr(entry, opcsn, 0);
        slapi_log_err(slapi_log_urp, sessionid,
                      "urp_conflict_to_glue - %s --> %s\n", basedn, newrdn);
        op_result = urp_fixup_rename_entry ( entry, newrdn, NULL, 0 );
        switch(op_result) {
        case LDAP_SUCCESS:
            break;
        case LDAP_NO_SUCH_OBJECT:
            slapi_log_err(slapi_log_urp, sessionid,
                           "urp_conflict_to_glue failed(%d) - %s --> %s\n", op_result, basedn, newrdn);
            rc = LDAP_NO_SUCH_OBJECT;
            break;
        default: 
            slapi_log_err(slapi_log_urp, sessionid,
                           "urp_conflict_to_glue failed(%d) - %s --> %s\n", op_result, basedn, newrdn);
            rc = 1;
        }
    }
    slapi_rdn_free(&parentrdn);
    return rc;
}
/* 
 * urp_annotate_dn:
 * Returns 0 on failure
 * Returns > 0 on success (1 on general conflict resolution success, LDAP_NO_SUCH_OBJECT on no-conflict success)
 *
 * Use this function to annotate an existing entry only. To annotate
 * a new entry (the operation entry) see urp_add_operation.
 */
static int
urp_annotate_dn(char *sessionid, const Slapi_Entry *entry, CSN *opcsn, const char *optype, char **conflict_dn)
{
    int rc = 0; /* Fail */
    int op_result;
    char *newrdn;
    const char *uniqueid;
    const Slapi_DN *basesdn;
    const char *basedn;

    uniqueid = slapi_entry_get_uniqueid(entry);
    basesdn = slapi_entry_get_sdn_const(entry);
    basedn = slapi_entry_get_dn_const(entry);
    newrdn = get_rdn_plus_uniqueid(sessionid, basedn, uniqueid);
    if (conflict_dn) {
        *conflict_dn = NULL;
    }
    if (newrdn) {
        mod_namingconflict_attr(uniqueid, basesdn, basesdn, opcsn, optype);
        mod_objectclass_attr(uniqueid, basesdn, basesdn, opcsn, optype);
        slapi_log_err(slapi_log_urp, sessionid,
                      "urp_annotate_dn - %s --> %s\n", basedn, newrdn);
        op_result = urp_fixup_rename_entry(entry, newrdn, NULL, 0);
        switch (op_result) {
        case LDAP_SUCCESS:
            slapi_log_err(slapi_log_urp, sessionid,
                          "urp_annotate_dn - Naming conflict %s. Renamed existing entry to %s\n",
                          optype, newrdn);
            if (conflict_dn) {
                *conflict_dn = slapi_ch_smprintf("%s,%s",newrdn,slapi_dn_find_parent(basedn));
            }
            rc = 1;
            break;
        case LDAP_NO_SUCH_OBJECT:
            /* This means that entry did not really exist!!!
             * This is clearly indicating that there is a
             * get_copy_of_entry -> dn2entry returned
             * an entry (entry) that was already removed
             * from the ldbm database...
             * This is bad, because it clearly indicates
             * some kind of db or cache corruption. We need to print
             * this fact clearly in the errors log to try
             * to solve this corruption one day.
             * However, as far as the conflict is concerned,
             * this error is completely harmless:
             * if thew entry did not exist in the first place,
             * there was never a room
             * for a conflict!! After fix for 558293, this
             * state can't be reproduced anymore (5-Oct-01)
             */
            slapi_log_err(SLAPI_LOG_ERR, sessionid,
                          "urp_annotate_dn - Entry %s exists in cache but not in DB\n",
                          basedn);
            rc = LDAP_NO_SUCH_OBJECT;
            break;
        default:
            slapi_log_err(slapi_log_urp, sessionid,
                          "urp_annotate_dn - Failed to annotate %s, err=%d\n",
                          newrdn, op_result);
        }
        slapi_ch_free((void **)&newrdn);
    } else {
        slapi_log_err(SLAPI_LOG_ERR, sessionid,
                      "urp_annotate_dn - Failed to create conflict DN from basedn: %s and uniqueid: %s\n",
                      basedn, uniqueid);
    }
    return rc;
}

static char *
urp_get_valid_parent_nsuniqueid (Slapi_DN *parentdn)
{
    Slapi_PBlock *newpb = NULL;
    Slapi_Entry **entries = NULL;
    int op_result = LDAP_SUCCESS;
    char *nsuid = NULL;

    newpb = slapi_pblock_new();
    slapi_search_internal_set_pb(newpb,
                                 slapi_sdn_get_dn(parentdn),
                                 LDAP_SCOPE_BASE,
                                 "objectclass=*",
                                 NULL, /* Attrs */
                                 0, /* AttrOnly */
                                 NULL, /* Controls */
                                 NULL, /* UniqueID */
                                 repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
                                 0);
    slapi_search_internal_pb(newpb);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);

    if ( (op_result != LDAP_SUCCESS) || (entries == NULL) ) {
        /* Log a message */
        goto done;
    }

    nsuid = slapi_entry_attr_get_charptr(entries[0], "nsuniqueid");
done:
    slapi_free_search_results_internal(newpb);
    slapi_pblock_destroy(newpb);
    newpb = NULL;

    return nsuid;
}

static int
urp_rename_conflict_children(const char *old_parent, const Slapi_DN *new_parent)
{
    Slapi_PBlock *newpb = NULL;
    Slapi_Entry **entries = NULL;
    int op_result = LDAP_SUCCESS;
    int i;

    newpb = slapi_pblock_new();
    slapi_search_internal_set_pb(newpb,
                                 old_parent,
                                 LDAP_SCOPE_ONELEVEL,
                                 "(|(objectclass=*)(objectclass=ldapsubentry))",
                                 NULL, /* Attrs */
                                 0, /* AttrOnly */
                                 NULL, /* Controls */
                                 NULL, /* UniqueID */
                                 repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
                                 0);
    slapi_search_internal_pb(newpb);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);

    if ((op_result != LDAP_SUCCESS) || (entries == NULL) || entries[0] == NULL) {
        /* Log a message */
        goto done;
    }
    for (i = 0; NULL != entries[i]; i++) {
        op_result = urp_fixup_move_entry(entries[i],new_parent,0);
        slapi_log_err(SLAPI_LOG_REPL, "session test",
                                     "urp_rename_conflict children - Renaming: %s, Result: %d\n",
                                      slapi_entry_get_dn_const(entries[i]), op_result);
    }

done:
    slapi_free_search_results_internal(newpb);
    slapi_pblock_destroy(newpb);
    newpb = NULL;

    return op_result;
}

/*
 * An URP Naming Collision helper function. Retrieves a list of entries
 * that have the given dn excluding the unique id of the entry. Any
 * entries returned will be entries that have been added with the same
 * dn, but caused a naming conflict when replicated. The URP to fix
 * this constraint violation is to append the unique id of the entry
 * to its RDN.
 */
static Slapi_Entry *
urp_get_min_naming_conflict_entry(Slapi_PBlock *pb, const char *collisiondn, char *sessionid, CSN *opcsn)
{
    Slapi_PBlock *newpb = NULL;
    LDAPControl **server_ctrls = NULL;
    Slapi_Entry **entries = NULL;
    Slapi_Entry *min_naming_conflict_entry = NULL;
    const CSN *min_csn = NULL;
    char *filter = NULL;
    char *parent_dn = NULL;
    const char *basedn;
    int i = 0;
    int min_i = -1;
    int op_result = LDAP_SUCCESS;

    if (collisiondn) {
        basedn = collisiondn;
    } else {
        slapi_pblock_get (pb, SLAPI_URP_NAMING_COLLISION_DN, &basedn);
    }
    if (NULL == basedn || strncmp (basedn, SLAPI_ATTR_UNIQUEID, strlen(SLAPI_ATTR_UNIQUEID)) == 0)
        return NULL;

    slapi_log_err(SLAPI_LOG_REPL, sessionid,
                  "urp_get_min_naming_conflict_entry - %s\n", basedn);

    filter = slapi_filter_sprintf("(&(objectclass=ldapsubentry)(%s=%s (ADD) %s%s))", ATTR_NSDS5_REPLCONFLICT, REASON_ANNOTATE_DN,
                                  ESC_NEXT_VAL, basedn);

    /* server_ctrls will be freed when newpb is destroyed */
    server_ctrls = (LDAPControl **)slapi_ch_calloc (2, sizeof (LDAPControl *));
    server_ctrls[0] = create_managedsait_control();
    server_ctrls[1] = NULL;
    
    newpb = slapi_pblock_new();
    parent_dn = slapi_dn_parent (basedn);
    slapi_search_internal_set_pb(newpb,
                                 parent_dn, /* Base DN */
                                 LDAP_SCOPE_ONELEVEL,
                                 filter,
                                 NULL, /* Attrs */
                                 0, /* AttrOnly */
                                 server_ctrls, /* Controls */
                                 NULL, /* UniqueID */
                                 repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
                                 0);
    slapi_search_internal_pb(newpb);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
    slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
    if ( (op_result != LDAP_SUCCESS) || (entries == NULL) ) {
        /* Log a message */
        goto done;
    }
    /* For all entries, get the one with the smallest dn csn */
    for (i = 0; NULL != entries[i]; i++) {
        const CSN *dncsn;
        dncsn = entry_get_dncsn(entries[i]);
        if ((dncsn != opcsn) && (csn_compare(dncsn, opcsn) > 0 ) &&
            ((min_csn == NULL) || (csn_compare(dncsn, min_csn) < 0)) &&
            !is_tombstone_entry (entries[i])) {
            min_csn = dncsn;
            min_i = i;
        }
        /*
         * If there are too many conflicts, the current urp code has no
         * guarantee for all servers to converge anyway, because the
         * urp and the backend can't be done in one transaction due
         * to either performance or the deadlock problem.
         * Don't sacrifice the performance too much for impossible.
         */
        if (min_csn && i > 5) {
            break;
        }
    }
    
    if (min_csn != NULL) {
        /* Found one entry */
        min_naming_conflict_entry = slapi_entry_dup(entries[min_i]);
    }

done:
    slapi_ch_free_string(&parent_dn);
    if (filter) {
        PR_smprintf_free(filter);
    }
    slapi_free_search_results_internal(newpb);
    slapi_pblock_destroy(newpb);
    newpb = NULL;

    slapi_log_err(SLAPI_LOG_REPL, sessionid,
                  "urp_get_min_naming_conflict_entry - Found %d entries\n", min_csn?1:0);

    return min_naming_conflict_entry;
}

/*
 * If an entry is deleted or renamed, a new winner may be
 * chosen from its naming competitors.
 * The entry with the smallest dncsn restores its original DN.
 */
static int
urp_naming_conflict_removal(Slapi_PBlock *pb, char *sessionid, CSN *opcsn, const char *optype)
{
    Slapi_Entry *min_naming_conflict_entry;
    Slapi_RDN *oldrdn, *newrdn;
    const char *oldrdnstr, *newrdnstr;
    int op_result;

    /*
     * Backend op has set SLAPI_URP_NAMING_COLLISION_DN to the basedn.
     */
    min_naming_conflict_entry = urp_get_min_naming_conflict_entry(pb, NULL, sessionid, opcsn);
    if (min_naming_conflict_entry == NULL) {
        return 0;
    }

    /* EXPERIMENT - do step 2 before 1 */

    /* Step2: Remove ATTR_NSDS5_REPLCONFLICT from the winning entry */
    /*
     * A fixup op will not invoke urp_modrdn_operation(). Even it does,
     * urp_modrdn_operation() will do nothing because of the same CSN.
     */
    op_result = del_replconflict_attr(min_naming_conflict_entry, opcsn, OP_FLAG_ACTION_INVOKE_FOR_REPLOP);
    if (op_result != LDAP_SUCCESS) {
        slapi_log_err(SLAPI_LOG_REPL, sessionid,
                      "urp_naming_conflict_removal - Failed to remove nsds5ReplConflict for %s, err=%d\n",
                      slapi_entry_get_ndn(min_naming_conflict_entry), op_result);
    }
    /* end Step 2 */
    /* Step 1: Restore the entry's original DN */

    oldrdn = slapi_rdn_new_sdn(slapi_entry_get_sdn_const(min_naming_conflict_entry));
    oldrdnstr = slapi_rdn_get_rdn(oldrdn);

    slapi_log_err(SLAPI_LOG_REPL, sessionid,
                   "urp_naming_conflict_removal - Found %s\n", slapi_entry_get_ndn(min_naming_conflict_entry));
    /* newrdnstr is the old rdn of the entry minus the nsuniqueid part */
    newrdn = slapi_rdn_new_rdn ( oldrdn );
    slapi_rdn_remove_attr (newrdn, SLAPI_ATTR_UNIQUEID );
    newrdnstr = slapi_rdn_get_rdn(newrdn);        

    /*
     * Set OP_FLAG_ACTION_INVOKE_FOR_REPLOP since this operation
     * is done after DB lock was released. The backend modrdn
     * will acquire the DB lock if it sees this flag.
     */
    op_result = urp_fixup_rename_entry((const Slapi_Entry *)min_naming_conflict_entry, newrdnstr, NULL, OP_FLAG_ACTION_INVOKE_FOR_REPLOP);
    if ( op_result != LDAP_SUCCESS ) {
        slapi_log_err(slapi_log_urp, sessionid,
            "urp_naming_conflict_removal - Failed to restore RDN of %s, err=%d\n", oldrdnstr, op_result);
        goto bailout;
    }
    slapi_log_err(slapi_log_urp, sessionid,
                  "urp_naming_conflict_removal - Naming conflict removed by %s. RDN of %s was restored\n", optype, oldrdnstr);
            
    /* end Step 1 */

bailout:
    slapi_entry_free(min_naming_conflict_entry);
    slapi_rdn_free(&oldrdn);
    slapi_rdn_free(&newrdn);
    return op_result;
}

/* The returned value is either null or "uniqueid=<uniqueid>+<basedn>" */
static char *
get_dn_plus_uniqueid(char *sessionid, const Slapi_DN *oldsdn, const char *uniqueid)
{
    Slapi_RDN *rdn = slapi_rdn_new();
    char *newdn = NULL;
    int rc = slapi_rdn_init_all_sdn_ext(rdn, oldsdn, 1);
    if (rc) {
        slapi_log_err(SLAPI_LOG_ERR, sessionid,
                      "Failed to convert %s to RDN\n", slapi_sdn_get_dn(oldsdn));
        goto bail;
    }

    PR_ASSERT(uniqueid != NULL);

    /* Check if the RDN already contains the Unique ID */
    if (slapi_rdn_is_conflict(rdn)) {
        /* The Unique ID is already in the RDN.
         * This is a highly improbable collision.
         * It suggests that a duplicate UUID was generated.
         * This will cause replication divergence and will
         * require admin intercession
         */
        slapi_log_err(SLAPI_LOG_WARNING, sessionid,
                      "get_dn_plus_uniqueid - Annotated DN %s has naming conflict\n", slapi_sdn_get_dn(oldsdn));
    } else {
        char *parentdn = slapi_dn_parent(slapi_sdn_get_dn(oldsdn));
        slapi_rdn_add(rdn, SLAPI_ATTR_UNIQUEID, uniqueid);
        /*
         * using slapi_ch_smprintf is okay since ...
         * uniqueid in rdn is normalized and
         * parentdn is normalized by slapi_sdn_get_dn.
         */
        newdn = slapi_ch_smprintf("%s,%s", slapi_rdn_get_rdn(rdn), parentdn);
        slapi_ch_free_string(&parentdn);
    }
bail:
    slapi_rdn_free(&rdn);
    return newdn;
}

char *
get_rdn_plus_uniqueid(char *sessionid, const char *olddn, const char *uniqueid)
{
    char *newrdn = NULL;
    Slapi_RDN *rdn = NULL;
    /* Check if the RDN already contains the Unique ID */
    rdn = slapi_rdn_new_dn(olddn);
    if (rdn == NULL) {
        slapi_log_err(SLAPI_LOG_ERR, sessionid,
                      "Failed to convert %s to RDN\n", olddn);
        goto bail;
    }

    PR_ASSERT(uniqueid!=NULL);
    if (slapi_rdn_is_conflict(rdn)) {
        /* The Unique ID is already in the RDN.
         * This is a highly improbable collision.
         * It suggests that a duplicate UUID was generated.
         * This will cause replication divergence and will
         * require admin intercession
         */
        slapi_log_err(SLAPI_LOG_WARNING, sessionid,
                      "get_rdn_plus_uniqueid - Annotated RDN %s has naming conflict\n", olddn);
    } else {
        slapi_rdn_add(rdn,SLAPI_ATTR_UNIQUEID,uniqueid);
        newrdn = slapi_ch_strdup(slapi_rdn_get_rdn(rdn));
    }
bail:
    slapi_rdn_free(&rdn);
    return newrdn;
}

static int
is_deleted_at_csn(const Slapi_Entry *entry, CSN *opcsn)
{
    int rc = 0;
    char *tombstone_csnstr = (char*)slapi_entry_attr_get_ref((Slapi_Entry *)entry, "nstombstonecsn");
    CSN *tombstone_csn = csn_new_by_string(tombstone_csnstr);

    if (csn_compare (tombstone_csn, opcsn) == 0) rc = 1;

    csn_free(&tombstone_csn);

    return rc;
}

int
is_conflict_entry(const Slapi_Entry *entry)
{
    int is_conflict = 0;
    if (slapi_entry_attr_get_ref((Slapi_Entry *)entry, ATTR_NSDS5_REPLCONFLICT)) {
        is_conflict = 1;
    }
    return is_conflict;
}

static int
is_renamed_entry(Slapi_PBlock *pb, Slapi_Entry *entry, CSN *opcsn)
{
    int rc = 0;
    Slapi_DN *del_dn = NULL;
    slapi_pblock_get(pb, SLAPI_DELETE_TARGET_SDN, &del_dn);
    if (slapi_sdn_compare(del_dn, slapi_entry_get_sdn(entry))) {
        /* the existing entry was renamed, check if it was before the delete */
        if (csn_compare (entry_get_dncsn(entry), opcsn) < 0) rc = 1;
    }

    return rc;
}

static int
is_suffix_entry(Slapi_PBlock *pb, Slapi_Entry *entry, Slapi_DN **parentdn)
{
    return is_suffix_dn(pb, slapi_entry_get_sdn(entry), parentdn);
}

int
is_suffix_dn_ext(Slapi_PBlock *pb, const Slapi_DN *dn, Slapi_DN **parentdn, int is_tombstone)
{
    Slapi_Backend *backend;
    int rc;

    *parentdn = slapi_sdn_new();
    slapi_pblock_get(pb, SLAPI_BACKEND, &backend);
    slapi_sdn_get_backend_parent_ext(dn, *parentdn, backend, is_tombstone);

    /* A suffix entry doesn't have parent dn */
    rc = slapi_sdn_isempty(*parentdn) ? 1 : 0;

    return rc;
}

int
is_suffix_dn(Slapi_PBlock *pb, const Slapi_DN *dn, Slapi_DN **parentdn)
{
    return is_suffix_dn_ext(pb, dn, parentdn, 0);
}

static int
mod_namingconflict_attr(const char *uniqueid, const Slapi_DN *entrysdn, const Slapi_DN *conflictsdn, CSN *opcsn, const char *optype)
{
    Slapi_Mods smods;
    char buf[BUFSIZ];
    int op_result;

    PR_snprintf(buf, sizeof(buf), "%s (%s) %s",
                 REASON_ANNOTATE_DN, optype, slapi_sdn_get_dn(conflictsdn));
    slapi_mods_init(&smods, 2);
    if ( strncmp(slapi_sdn_get_dn(entrysdn), SLAPI_ATTR_UNIQUEID,
         strlen(SLAPI_ATTR_UNIQUEID)) != 0 ) {
        slapi_mods_add(&smods, LDAP_MOD_ADD, ATTR_NSDS5_REPLCONFLICT, strlen(buf), buf);
    } else {
        /*
         * If the existing entry is already a naming conflict loser,
         * the following replace operation should result in the
         * replace of the ATTR_NSDS5_REPLCONFLICT index as well.
         */
        slapi_mods_add(&smods, LDAP_MOD_REPLACE, ATTR_NSDS5_REPLCONFLICT, strlen(buf), buf);
    }
    op_result = urp_fixup_modify_entry(uniqueid, entrysdn, opcsn, &smods, 0);
    slapi_mods_done(&smods);
    return op_result;
}

static int
mod_objectclass_attr(const char *uniqueid, const Slapi_DN *entrysdn, const Slapi_DN *conflictsdn, CSN *opcsn, const char *optype)
{
    Slapi_Mods smods;
    int op_result;
    char csnstr[CSN_STRSIZE+1] = {0};

    slapi_mods_init(&smods, 3);
    slapi_mods_add_string(&smods, LDAP_MOD_ADD, "objectclass", "ldapsubentry");
    slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "conflictcsn", csn_as_string(opcsn, PR_FALSE, csnstr));
    op_result = urp_fixup_modify_entry(uniqueid, entrysdn, opcsn, &smods, 0);
    slapi_mods_done(&smods);
    if (op_result == LDAP_TYPE_OR_VALUE_EXISTS) {
        /* the objectclass was already present */
        op_result = LDAP_SUCCESS;
    }
    return op_result;
}

static int
del_replconflict_attr(const Slapi_Entry *entry, CSN *opcsn, int opflags)
{
    Slapi_Attr *attr;
    int op_result = 0;

    if (slapi_entry_attr_find(entry, ATTR_NSDS5_REPLCONFLICT, &attr) == 0) {
        Slapi_Mods smods;
        const char *uniqueid;
        const Slapi_DN *entrysdn;

        uniqueid = slapi_entry_get_uniqueid(entry);
        entrysdn = slapi_entry_get_sdn_const(entry);
        slapi_mods_init(&smods, 3);
        slapi_mods_add(&smods, LDAP_MOD_DELETE, ATTR_NSDS5_REPLCONFLICT, 0, NULL);
        slapi_mods_add(&smods, LDAP_MOD_DELETE, "objectclass", strlen("ldapsubentry"),"ldapsubentry");
        op_result = urp_fixup_modify_entry(uniqueid, entrysdn, opcsn, &smods, opflags);
        slapi_mods_done(&smods);
    }
    return op_result;
}