summaryrefslogtreecommitdiffstats
path: root/ldap/servers/plugins/linkedattrs/linked_attrs.c
blob: 5cfef5d38db79e552b4764e6b2092280b7a7936b (plain)
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
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 *
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception.
 *
 *
 * Copyright (C) 2009 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

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

/*
 * Linked attributes plug-in
 */
#include "linked_attrs.h"


/*
 * Plug-in globals
 */
static PRCList *g_link_config = NULL;
static PRCList *g_managed_config_index = NULL;
static PRRWLock *g_config_lock;

static void *_PluginID = NULL;
static char *_PluginDN = NULL;
static int g_plugin_started = 0;

static Slapi_PluginDesc pdesc = { LINK_FEATURE_DESC,
                                  VENDOR,
                                  DS_PACKAGE_VERSION,
                                  LINK_PLUGIN_DESC };

/*
 * Plug-in management functions
 */
int linked_attrs_init(Slapi_PBlock * pb);
static int linked_attrs_start(Slapi_PBlock * pb);
static int linked_attrs_close(Slapi_PBlock * pb);
static int linked_attrs_postop_init(Slapi_PBlock * pb);
static int linked_attrs_internal_postop_init(Slapi_PBlock *pb);

/*
 * Operation callbacks (where the real work is done)
 */
static int linked_attrs_mod_post_op(Slapi_PBlock *pb);
static int linked_attrs_add_post_op(Slapi_PBlock *pb);
static int linked_attrs_del_post_op(Slapi_PBlock *pb);
static int linked_attrs_modrdn_post_op(Slapi_PBlock *pb);
static int linked_attrs_pre_op(Slapi_PBlock *pb, int modop);
static int linked_attrs_mod_pre_op(Slapi_PBlock *pb);
static int linked_attrs_add_pre_op(Slapi_PBlock *pb);

/*
 * Config cache management functions
 */
static int linked_attrs_load_config();
static void linked_attrs_delete_config();
static int linked_attrs_parse_config_entry(Slapi_Entry * e, int apply);
static void linked_attrs_insert_config_index(struct configEntry *entry);
static void linked_attrs_free_config_entry(struct configEntry ** entry);

/*
 * helpers
 */
static char *linked_attrs_get_dn(Slapi_PBlock * pb);
static int linked_attrs_dn_is_config(char *dn);
static void linked_attrs_find_config(const char *dn, const char *type,
    struct configEntry **config);
static void linked_attrs_find_config_reverse(const char *dn,
    const char *type, struct configEntry **config);
static int linked_attrs_config_index_has_type(char *type);
static int linked_attrs_config_exists(struct configEntry *entry);
static int linked_attrs_config_exists_reverse(struct configEntry *entry);
static int linked_attrs_oktodo(Slapi_PBlock *pb);
void linked_attrs_load_array(Slapi_Value **array, Slapi_Attr *attr);
int linked_attrs_compare(const void *a, const void *b);
static void linked_attrs_add_backpointers(char *linkdn, struct configEntry *config,
    Slapi_Mod *smod);
static void linked_attrs_del_backpointers(Slapi_PBlock *pb, char *linkdn,
    struct configEntry *config, Slapi_Mod *smod);
static void linked_attrs_replace_backpointers(Slapi_PBlock *pb, char *linkdn,
    struct configEntry *config, Slapi_Mod *smod);
static void linked_attrs_mod_backpointers(char *linkdn, char *type, char *scope,
    int modop, Slapi_ValueSet *targetvals);

/*
 * Config cache locking functions
 */
void
linked_attrs_read_lock()
{
    PR_RWLock_Rlock(g_config_lock);
}

void
linked_attrs_write_lock()
{
    PR_RWLock_Wlock(g_config_lock);
}

void
linked_attrs_unlock()
{
    PR_RWLock_Unlock(g_config_lock);
}


/*
 * Plugin identity functions
 */
void
linked_attrs_set_plugin_id(void *pluginID)
{
    _PluginID = pluginID;
}

void *
linked_attrs_get_plugin_id()
{
    return _PluginID;
}

void
linked_attrs_set_plugin_dn(char *pluginDN)
{
    _PluginDN = pluginDN;
}

char *
linked_attrs_get_plugin_dn()
{
    return _PluginDN;
}


/*
 * Plug-in initialization functions
 */
int
linked_attrs_init(Slapi_PBlock *pb)
{
    int status = 0;
    char *plugin_identity = NULL;

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_init\n");

    /* Store the plugin identity for later use.
     * Used for internal operations. */
    slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
    PR_ASSERT(plugin_identity);
    linked_attrs_set_plugin_id(plugin_identity);

    /* Register callbacks */
    if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
                         SLAPI_PLUGIN_VERSION_01) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
                         (void *) linked_attrs_start) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
                         (void *) linked_attrs_close) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
                         (void *) &pdesc) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODIFY_FN,
                         (void *) linked_attrs_mod_pre_op) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN,
                         (void *) linked_attrs_add_pre_op) != 0 ||
        slapi_register_plugin("internalpostoperation",  /* op type */
                              1,        /* Enabled */
                              "linked_attrs_init",   /* this function desc */
                              linked_attrs_internal_postop_init,  /* init func */
                              LINK_INT_POSTOP_DESC,      /* plugin desc */
                              NULL,     /* ? */
                              plugin_identity   /* access control */
        ) ||
        slapi_register_plugin("postoperation",  /* op type */
                              1,        /* Enabled */
                              "linked_attrs_init",   /* this function desc */
                              linked_attrs_postop_init,  /* init func for post op */
                              LINK_POSTOP_DESC,      /* plugin desc */
                              NULL,     /* ? */
                              plugin_identity   /* access control */
        )
        ) {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_init: failed to register plugin\n");
        status = -1;
    }

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_init\n");
    return status;
}

static int
linked_attrs_internal_postop_init(Slapi_PBlock *pb)
{
    int status = 0;

    if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
                         SLAPI_PLUGIN_VERSION_01) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
                         (void *) &pdesc) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
                         (void *) linked_attrs_add_post_op) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
                         (void *) linked_attrs_del_post_op) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
                         (void *) linked_attrs_mod_post_op) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
                         (void *) linked_attrs_modrdn_post_op) != 0) {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_internal_postop_init: failed to register plugin\n");
        status = -1;
    }
 
    return status;
}

static int
linked_attrs_postop_init(Slapi_PBlock *pb)
{
    int status = 0;

    if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
                         SLAPI_PLUGIN_VERSION_01) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
                         (void *) &pdesc) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_POST_ADD_FN,
                         (void *) linked_attrs_add_post_op) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_POST_DELETE_FN,
                         (void *) linked_attrs_del_post_op) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODIFY_FN,
                         (void *) linked_attrs_mod_post_op) != 0 ||
        slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODRDN_FN,
                         (void *) linked_attrs_modrdn_post_op) != 0) {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_postop_init: failed to register plugin\n");
        status = -1;
    }

    return status;
}


/*
 * linked_attrs_start()
 *
 * Creates config lock and loads config cache.
 */
static int
linked_attrs_start(Slapi_PBlock * pb)
{
    char *plugindn = NULL;

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_start\n");

    /* Check if we're already started */
    if (g_plugin_started) {
        goto done;
    }

    g_config_lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "linkedattrs");

    if (!g_config_lock) {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_start: lock creation failed\n");

        return -1;
    }

    /*
     * Get the plug-in target dn from the system
     * and store it for future use. */
    slapi_pblock_get(pb, SLAPI_TARGET_DN, &plugindn);
    if (NULL == plugindn || 0 == strlen(plugindn)) {
        slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_start: unable to retrieve plugin dn\n");
        return -1;
    }

    linked_attrs_set_plugin_dn(plugindn);

    /*
     * Load the config cache
     */
    g_link_config = (PRCList *)slapi_ch_calloc(1, sizeof(struct configEntry));
    PR_INIT_CLIST(g_link_config);
    g_managed_config_index = (PRCList *)slapi_ch_calloc(1, sizeof(struct configIndex));
    PR_INIT_CLIST(g_managed_config_index);

    if (linked_attrs_load_config() != 0) {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_start: unable to load plug-in configuration\n");
        return -1;
    }

    /*
     * Register our task callback
     */
    slapi_task_register_handler("fixup linked attributes", linked_attrs_fixup_task_add);

    g_plugin_started = 1;
    slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
                    "linked attributes plug-in: ready for service\n");
    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_start\n");

done:
    return 0;
}

/*
 * linked_attrs_close()
 *
 * Cleans up the config cache.
 */
static int
linked_attrs_close(Slapi_PBlock * pb)
{
    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_close\n");

    linked_attrs_delete_config();

    slapi_ch_free((void **)&g_link_config);
    slapi_ch_free((void **)&g_managed_config_index);

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_close\n");

    return 0;
}

PRCList *
linked_attrs_get_config()
{
    return g_link_config;
}

/*
 * config looks like this
 * - cn=myplugin
 * --- cn=manager link
 * --- cn=owner link
 * --- cn=etc
 */
static int
linked_attrs_load_config()
{
    int status = 0;
    int result;
    int i;
    Slapi_PBlock *search_pb;
    Slapi_Entry **entries = NULL;

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_load_config\n");

    /* Clear out any old config. */
    linked_attrs_write_lock();
    linked_attrs_delete_config();

    /* Find the config entries beneath our plugin entry. */
    search_pb = slapi_pblock_new();
    slapi_search_internal_set_pb(search_pb, linked_attrs_get_plugin_dn(),
                                 LDAP_SCOPE_SUBTREE, "objectclass=*",
                                 NULL, 0, NULL, NULL, linked_attrs_get_plugin_id(), 0);
    slapi_search_internal_pb(search_pb);
    slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);

    if (LDAP_SUCCESS != result) {
        status = -1;
        goto cleanup;
    }

    slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
                     &entries);
    if (NULL == entries || NULL == entries[0]) {
        /* If there are no config entries, we're done. */
        goto cleanup;
    }

    /* Loop through all of the entries we found and parse them. */
    for (i = 0; (entries[i] != NULL); i++) {
        /* We don't care about the status here because we may have
         * some invalid config entries, but we just want to continue
         * looking for valid ones. */
        linked_attrs_parse_config_entry(entries[i], 1);
    }

  cleanup:
    slapi_free_search_results_internal(search_pb);
    slapi_pblock_destroy(search_pb);
    linked_attrs_unlock();
    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_load_config\n");

    return status;
}

/*
 * linked_attrs_parse_config_entry()
 *
 * Parses a single config entry.  If apply is non-zero, then
 * we will load and start using the new config.  You can simply
 * validate config without making any changes by setting apply
 * to 0.
 *
 * Returns 0 if the entry is valid and -1 if it is invalid.
 */
static int
linked_attrs_parse_config_entry(Slapi_Entry * e, int apply)
{
    char *value;
    struct configEntry *entry = NULL;
    struct configEntry *config_entry;
    PRCList *list;
    int entry_added = 0;
    int ret = 0;

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_parse_config_entry\n");

    /* If this is the main plug-in
     * config entry, just bail. */
    if (strcasecmp(linked_attrs_get_plugin_dn(), slapi_entry_get_ndn(e)) == 0) {
        ret = -1;
        goto bail;
    }

    entry = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
    if (NULL == entry) {
        ret = -1;
        goto bail;
    }

    value = slapi_entry_get_ndn(e);
    if (value) {
        entry->dn = slapi_ch_strdup(value);
    } else {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_parse_config_entry: Error "
                        "reading dn from config entry\n");
        ret = -1;
        goto bail;
    }

    slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
                    "----------> dn [%s]\n", entry->dn);

    value = slapi_entry_attr_get_charptr(e, LINK_LINK_TYPE);
    if (value) {
        int not_dn_syntax = 0;
        char *syntaxoid = NULL;
        Slapi_Attr *attr = slapi_attr_new();

        /* Set this first so we free it if we encounter an error */
        entry->linktype = value;

        /* Gather some information about this attribute. */
        slapi_attr_init(attr, value);
        slapi_attr_get_syntax_oid_copy(attr, &syntaxoid );
        not_dn_syntax = strcmp(syntaxoid, DN_SYNTAX_OID);
        slapi_ch_free_string(&syntaxoid);
        slapi_attr_free(&attr);

        /* Check if the link type's syntax is Distinguished Name.
         * We only treat this as a warning. */
        if (not_dn_syntax) {
            slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                            "linked_attrs_parse_config_entry: The %s config "
                            "setting must be set to an attribute with the "
                            "Distinguished Name syntax for linked attribute "
                            "pair \"%s\".\n", LINK_LINK_TYPE, entry->dn);
        }
    } else {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_parse_config_entry: The %s config "
                        "setting is required for linked attribute pair \"%s\".\n",
                        LINK_LINK_TYPE, entry->dn);
        ret = -1;
        goto bail;
    }

    slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
                    "----------> %s [%s]\n", LINK_LINK_TYPE, entry->linktype);

    value = slapi_entry_attr_get_charptr(e, LINK_MANAGED_TYPE);
    if (value) {
        int single_valued = 0;
        int not_dn_syntax = 0;
        char *syntaxoid = NULL;
        Slapi_Attr *attr = slapi_attr_new();

	/* Set this first so we free it if we encounter an error */
        entry->managedtype = value;

        /* Gather some information about this attribute. */
        slapi_attr_init(attr, value);
        slapi_attr_get_syntax_oid_copy(attr, &syntaxoid );
        not_dn_syntax = strcmp(syntaxoid, DN_SYNTAX_OID);
        single_valued = slapi_attr_flag_is_set(attr, SLAPI_ATTR_FLAG_SINGLE);
        slapi_ch_free_string(&syntaxoid);
        slapi_attr_free(&attr);

        /* Ensure that the managed type is a multi-valued attribute. */
        if (single_valued) {
            slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                            "linked_attrs_parse_config_entry: The %s config "
                            "setting must be set to a multi-valued attribute "
                            "for linked attribute pair \"%s\".\n",
                            LINK_MANAGED_TYPE, entry->dn);
            ret = -1;
            goto bail;
        /* Check if the link type's syntax is Distinguished Name.
         * We only treat this as a warning. */
        } else if (not_dn_syntax) {
            slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                            "linked_attrs_parse_config_entry: The %s config "
                            "setting must be set to an attribute with the "
                            "Distinguished Name syntax for linked attribute "
                            "pair \"%s\".\n", LINK_MANAGED_TYPE, entry->dn);
        }
    } else {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_parse_config_entry: The %s config "
                        "setting is required for linked attribute pair \"%s\".\n",
                        LINK_MANAGED_TYPE, entry->dn);
        ret = -1;
        goto bail;
    }

    slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
                    "----------> %s [%s]\n", LINK_MANAGED_TYPE,
                    entry->managedtype);

    /* A scope is not required.  No scope means it
     * applies to any part of the DIT. */
    value = slapi_entry_attr_get_charptr(e, LINK_SCOPE);
    if (value) {
        entry->scope = value;
    }

    slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
                    "----------> %s [%s]\n", LINK_SCOPE,
                    entry->scope ? entry->scope : "NULL");

    /* Check if config already exists for
     * the link type at the same scope. */
    if (linked_attrs_config_exists(entry)) {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_parse_config_entry: A config "
                        "entry for the link attribute %s already "
                        "exists at a scope of \"%s\".\n", entry->linktype,
                        entry->scope);
        ret = -1;
        goto bail;
    }

    /* Check if config already exists for
     * the managed type at the same scope. */
    if (linked_attrs_config_exists_reverse(entry)) {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_parse_config_entry: A config "
                        "entry for the managed attribute %s already "
                        "exists at a scope of \"%s\".\n", entry->managedtype,
                        entry->scope);
        ret = -1;
        goto bail;
    }

    /* If we were only called to validate config, we can
     * just bail out before applying the config changes */
    if (apply == 0) {
        goto bail;
    }

    /* Create a lock for this attribute pair. */
    entry->lock = slapi_new_mutex();
    if (!entry->lock) {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_parse_config_entry: Unable to create "
                        "lock for linked attribute pair \"%s\".\n", entry->dn);
        ret = -1;
        goto bail;
    }

    /* Add the entry to the list.  We group by link type. We
     * also maintain a reverse list grouped by managed type. */
    if (!PR_CLIST_IS_EMPTY(g_link_config)) {
        list = PR_LIST_HEAD(g_link_config);
        while (list != g_link_config) {
            config_entry = (struct configEntry *) list;

            /* See if the types match.  We want to group
             * entries for the same link type together. */
            if (slapi_attr_type_cmp(config_entry->linktype, entry->linktype, 1) == 0) {
                PR_INSERT_BEFORE(&(entry->list), list);
                slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
                                "store [%s] before [%s] \n", entry->dn,
                                config_entry->dn);

                /* add to managed type index */
                linked_attrs_insert_config_index(entry);

                entry_added = 1;
                break;
            }

            list = PR_NEXT_LINK(list);

            if (g_link_config == list) {
                /* add to tail */
                PR_INSERT_BEFORE(&(entry->list), list);
                slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
                                "store [%s] at tail\n", entry->dn);

                /* add to managed type index */
                linked_attrs_insert_config_index(entry);

                entry_added = 1;
                break;
            }
        }
    } else {
        /* first entry */
        PR_INSERT_LINK(&(entry->list), g_link_config);
        slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
                        "store [%s] at head \n", entry->dn);

        /* add to managed type index */
        linked_attrs_insert_config_index(entry);

        entry_added = 1;
    }

  bail:
    if (0 == entry_added) {
        /* Don't log error if we weren't asked to apply config */
        if ((apply != 0) && (entry != NULL)) {
            slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                            "linked_attrs_parse_config_entry: Invalid config entry "
                            "[%s] skipped\n", entry->dn);
        }
        linked_attrs_free_config_entry(&entry);
    } else {
        ret = 0;
    }

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_parse_config_entry\n");

    return ret;
}

/*
 * linked_attrs_insert_config_index()
 *
 * Adds an entry to the ordered config index.  We maintain
 * an list of pointers to the cached config entries that is
 * grouped by managed type. We use this index to find the
 * appropriate config entry when given a backpointer.  This
 * is useful for the case where an entry with backpointers
 * is renamed and we need to updated the forward link.
 */
static void 
linked_attrs_insert_config_index(struct configEntry *entry)
{
    struct configEntry *config_entry = NULL;
    struct configIndex *index_entry = NULL;
    PRCList *list = PR_LIST_HEAD(g_managed_config_index);

    index_entry = (struct configIndex *)slapi_ch_calloc(1, sizeof(struct configIndex));
    index_entry->config = entry;

    if (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
        while (list != g_managed_config_index) {
            config_entry = ((struct configIndex *)list)->config;
    
            /* See if the types match. */
            if (slapi_attr_type_cmp(config_entry->managedtype, entry->managedtype, 1) == 0) {
                PR_INSERT_BEFORE(&(index_entry->list), list);
                slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
                                "store [%s] before [%s] \n", entry->dn,
                                config_entry->dn);
                break;
            }
    
            list = PR_NEXT_LINK(list);

            if (g_managed_config_index == list) {
                /* add to tail */
                PR_INSERT_BEFORE(&(index_entry->list), list);
                slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
                                "store [%s] at tail\n", entry->dn);
                break;
            }
        }
    } else {
        /* first entry */
        slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
                        "store [%s] at head \n", entry->dn);
        PR_INSERT_LINK(&(index_entry->list), g_managed_config_index);
    }
}

static void
linked_attrs_free_config_entry(struct configEntry ** entry)
{
    struct configEntry *e = *entry;

    if (e == NULL)
        return;

    if (e->dn) {
        slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
                        "freeing config entry [%s]\n", e->dn);
        slapi_ch_free_string(&e->dn);
    }

    if (e->linktype)
        slapi_ch_free_string(&e->linktype);

    if (e->managedtype)
        slapi_ch_free_string(&e->managedtype);

    if (e->scope)
        slapi_ch_free_string(&e->scope);

    if (e->lock)
        slapi_destroy_mutex(e->lock);

    slapi_ch_free((void **) entry);
}

static void
linked_attrs_delete_configEntry(PRCList *entry)
{
    PR_REMOVE_LINK(entry);
    linked_attrs_free_config_entry((struct configEntry **) &entry);
}

static void
linked_attrs_delete_config()
{
    PRCList *list;

    /* Delete the config cache. */
    while (!PR_CLIST_IS_EMPTY(g_link_config)) {
        list = PR_LIST_HEAD(g_link_config);
        linked_attrs_delete_configEntry(list);
    }

    /* Delete the reverse index. */
    while (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
        list = PR_LIST_HEAD(g_managed_config_index);
        PR_REMOVE_LINK(list);
        slapi_ch_free((void **)&list);
    }

    return;
}


/*
 * Helper functions
 */
static char *
linked_attrs_get_dn(Slapi_PBlock * pb)
{
    char *dn = 0;
    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_get_dn\n");

    if (slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn)) {
        slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_get_dn: failed to get dn of changed entry");
        goto bail;
    }

  bail:
    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_get_dn\n");

    return dn;
}

/*
 * linked_attrs_dn_is_config()
 *
 * Checks if dn is a linked attribute config entry.
 */
static int
linked_attrs_dn_is_config(char *dn)
{
    int ret = 0;

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_dn_is_config\n");

    /* Return 1 if the passed in dn is a child of the main
     * plugin config entry. */
    if (slapi_dn_issuffix(dn, linked_attrs_get_plugin_dn()) &&
        strcasecmp(dn, linked_attrs_get_plugin_dn())) {
        ret = 1;
    }

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_dn_is_config\n");

    return ret;
}

/*
 * linked_attrs_find_config()
 *
 * Finds the appropriate config entry for a given dn and
 * link type.  A read lock must be held on the config
 * before calling this function.  The configEntry that is
 * returned is a pointer to the actual config entry in
 * the config cache.  It should not be modified in any
 * way.  The read lock should not be released until you
 * are finished with the config entry that is returned.

 * Returns NULL if no applicable config entry is found.
 */
static void
linked_attrs_find_config(const char *dn,
    const char *type, struct configEntry **config)
{
    int found_type = 0;
    PRCList *list = NULL;

    *config = NULL;

    if (!PR_CLIST_IS_EMPTY(g_link_config)) {
        list = PR_LIST_HEAD(g_link_config);
        while (list != g_link_config) {
            if (slapi_attr_type_cmp(((struct configEntry *)list)->linktype,
                type, 1) == 0) {
                /* Set a flag indicating that we found a config entry
                 * for this type. We use this flag so we can stop
                 * processing early if we don't find a matching scope. */
                found_type = 1;

                /* Check if the dn is in the scope of this config
                 * entry.  If the config entry doesn't have a scope
                 * (global), consider it a match.  If we have a match,
                 * we can stop processing the config. */
                if ((((struct configEntry *)list)->scope == NULL) ||
                    (slapi_dn_issuffix(dn, ((struct configEntry *)list)->scope))) {
                    *config = (struct configEntry *)list;
                    break;
                }
            } else {
                /* If flag is set, we're done.  We have configured links
                 * for this type, but none of the scopes match. */
                if (found_type) {
                    break;
                }
            }

            list = PR_NEXT_LINK(list);
        }
    }
}

/*
 * linked_attrs_find_config_reverse()
 *
 * Finds the appropriate config entry for a given dn and
 * managed type.  A read lock must be held on the config 
 * before calling this function.  The configEntry that is
 * returned is a pointer to the actual config entry in
 * the config cache.  It should not be modified in any
 * way.  The read lock should not be released until you
 * are finished with the config entry that is returned.

 * Returns NULL if no applicable config entry is found.
 */ 
static void
linked_attrs_find_config_reverse(const char *dn,
    const char *type, struct configEntry **config)
{   
    int found_type = 0;
    PRCList *list = NULL;

    *config = NULL;

    if (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
        list = PR_LIST_HEAD(g_managed_config_index);
        while (list != g_managed_config_index) {
            if (slapi_attr_type_cmp(((struct configIndex *)list)->config->managedtype,
                                    type, 1) == 0) {
                /* Set a flag indicating that we found a config entry
                 * for this type. We use this flag so we can stop
                 * processing early if we don't find a matching scope. */
                found_type = 1;

                /* Check if the dn is in the scope of this config
                 * entry.  If the config entry doesn't have a scope
                 * (global), consider it a match.  If we have a match,
                 * we can stop processing the config. */
                if ((((struct configIndex *)list)->config->scope == NULL) ||
                    (slapi_dn_issuffix(dn, ((struct configIndex *)list)->config->scope))) {
                    *config = ((struct configIndex *)list)->config;
                    break;
                }
            } else {
                /* If flag is set, we're done.  We have configured links
                 * for this type, but none of the scopes match. */
                if (found_type) {
                    break;
                }
            }

            list = PR_NEXT_LINK(list);
        }
    }
}

/*
 * linked_attrs_config_index_has_type()
 *
 * Returns 1 if a config entry exists with the passed
 * in managed type.
 *
 * A read lock on the config must be held before calling
 * this function.
 */
static int
linked_attrs_config_index_has_type(char *type)
{
    int rc = 0;
    PRCList *list = NULL;

    if (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
        list = PR_LIST_HEAD(g_managed_config_index);
        while (list != g_managed_config_index) {
            if (slapi_attr_type_cmp(((struct configIndex *)list)->config->managedtype,
                                    type, 1) == 0) {
                rc = 1;
                break;
            }

            list = PR_NEXT_LINK(list);
        }
    }

    return rc;
}

/*
 * linked_attrs_config_exists()
 *
 * Returns 1 if a config entry exists in the cache
 * already for the given link type at the given scope.
 * This will detect if the cached config entry is really
 * the same one as the passed in entry by comparing the
 * dn of the config entry.  We will still return 0 in
 * this case as it's one and the same config entry. We
 * really want to use this to prevent multiple config
 * entries for the same link type at the same scope.
 *
 * A read lock on the config must be held before calling
 * this function.
 */
static int
linked_attrs_config_exists(struct configEntry *entry)
{
    int rc = 0;
    int found_type = 0;
    PRCList *list = NULL;

    if (!PR_CLIST_IS_EMPTY(g_link_config)) {
        list = PR_LIST_HEAD(g_link_config);
        while (list != g_link_config) {
            if (slapi_attr_type_cmp(((struct configEntry *)list)->linktype,
                                    entry->linktype, 1) == 0) {
                found_type = 1;
                /* We don't allow nested config for the same type.  We
                 * need to check for nesting in both directions here. 
                 * If no scope is set, we consider the entry global. */
                if ((((struct configEntry *)list)->scope == NULL) ||
                    slapi_dn_issuffix(entry->scope, ((struct configEntry *)list)->scope) ||
                    slapi_dn_issuffix(((struct configEntry *)list)->scope, entry->scope)) {
                    /* Make sure that this isn't the same exact entry
                     * in the list already.  This can happen if a config
                     * entry is being modified.  Both of these were already
                     * normalized when the config struct was filled in. */
                    if (strcasecmp(entry->dn, ((struct configEntry *)list)->dn) != 0) {
                        rc = 1;
                        break;
                    }
                }
            } else {
                if (found_type) {
                    /* Since the list is sorted by link type, we
                     * are finished if we get here since we found
                     * the type but didn't match the scope. */
                    break;
                }
            }

            list = PR_NEXT_LINK(list);
        }
    }

    return rc;
}

/*
 * linked_attrs_config_exists_reverse()
 *
 * Returns 1 if a config entry exists in the cache
 * already for the given managed type at the given scope.
 * This will detect if the cached config entry is really
 * the same one as the passed in entry by comparing the
 * dn of the config entry.  We will still return 0 in
 * this case as it's one and the same config entry. We
 * really want to use this to prevent multiple config
 * entries for the same managed type at the same scope.
 *
 * A read lock on the config must be held before calling
 * this function.
 */
static int
linked_attrs_config_exists_reverse(struct configEntry *entry)
{
    int rc = 0;
    int found_type = 0;
    PRCList *list = NULL;

    if (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
        list = PR_LIST_HEAD(g_managed_config_index);
        while (list != g_managed_config_index) {
            if (slapi_attr_type_cmp(((struct configIndex *)list)->config->managedtype,
                                    entry->managedtype, 1) == 0) {
                found_type = 1;
                /* We don't allow nested config for the same type.  We
                 * need to check for nesting in both directions here. */
                if ((((struct configIndex *)list)->config->scope == NULL) ||
                    slapi_dn_issuffix(entry->scope,
                    ((struct configIndex *)list)->config->scope) ||
                    slapi_dn_issuffix(((struct configIndex *)list)->config->scope,
                    entry->scope)) {
                    /* Make sure that this isn't the same exact entry
                     * in the list already.  This can happen if a config
                     * entry is being modified.  Both of these were already
                     * normalized when the config struct was filled in. */
                    if (strcasecmp(entry->dn, ((struct configIndex *)list)->config->dn) != 0) {
                        rc = 1;
                        break;
                    }
                }
            } else {
                if (found_type) {
                    /* Since the list is sorted by link type, we
                     * are finished if we get here since we found
                     * the type but didn't match the scope. */
                    break;
                }
            }

            list = PR_NEXT_LINK(list);
        }
    }

    return rc;
}

/*
 * linked_attrs_oktodo()
 *
 * Check if we want to process this operation.  We need to be
 * sure that the operation succeeded.  We also respond to replicated
 * ops so we don't test for that.  This does require that the managed
 * attributes not be replicated.
 */
static int
linked_attrs_oktodo(Slapi_PBlock *pb)
{
        int ret = 1;
        int oprc = 0;

        slapi_log_error( SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                     "--> linked_attrs_oktodo\n" );

        if(slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0)
        {
                slapi_log_error( SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_oktodo: could not get parameters\n" );
                ret = -1;
        }

        /* This plugin should only execute if the operation succeeded. */
        if(oprc != 0)
        {
                ret = 0;
        }

        slapi_log_error( SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                     "<-- linked_attrs_oktodo\n" );

        return ret;
}

/* linked_attrs_load_array()
 * 
 * put attribute values in array structure
 */
void
linked_attrs_load_array(Slapi_Value **array, Slapi_Attr *attr)
{
        Slapi_Value *val = 0;
        int hint = slapi_attr_first_value(attr, &val);

        while(val)
        {
                *array = val;
                array++;
                hint = slapi_attr_next_value(attr, hint, &val);
        }
}

/* linked_attrs_compare()
 * 
 * Compare two attr values using the DN syntax.
 */
int
linked_attrs_compare(const void *a, const void *b)
{
        int rc = 0;
        Slapi_Value *val1 = *((Slapi_Value **)a);
        Slapi_Value *val2 = *((Slapi_Value **)b);
        Slapi_Attr *linkattr = slapi_attr_new();

        slapi_attr_init(linkattr, "distinguishedName");

        rc = slapi_attr_value_cmp(linkattr,
                slapi_value_get_berval(val1),
                slapi_value_get_berval(val2));

        slapi_attr_free(&linkattr);

        return rc;
}

/*
 * linked_attrs_add_backpointers()
 *
 * Adds backpointers pointing to dn to the entries referred to
 * by the values in smod.
 */
static void
linked_attrs_add_backpointers(char *linkdn, struct configEntry *config,
    Slapi_Mod *smod)
{
    Slapi_ValueSet *vals = slapi_valueset_new();

    slapi_valueset_set_from_smod(vals, smod);
    linked_attrs_mod_backpointers(linkdn, config->managedtype, config->scope,
                                  LDAP_MOD_ADD, vals);

    slapi_valueset_free(vals);
}

/*
 * linked_attrs_del_backpointers()
 *
 * Remove backpointers pointing to linkdn in the entries referred
 * to by the values in smod.
 */
static void
linked_attrs_del_backpointers(Slapi_PBlock *pb, char *linkdn,
    struct configEntry *config, Slapi_Mod *smod)
{
    Slapi_ValueSet *vals = NULL;

    /* If no values are listed in the smod, we need to get
     * a list of all of the values that were deleted by
     * looking at the pre-op copy of the entry. */
    if (slapi_mod_get_num_values(smod) == 0) {
        Slapi_Entry *pre_e = NULL;
        Slapi_Attr *pre_attr = NULL;

        slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &pre_e );
        slapi_entry_attr_find( pre_e, config->linktype, &pre_attr );
        slapi_attr_get_valueset(pre_attr, &vals);
    } else {
        vals = slapi_valueset_new();
        slapi_valueset_set_from_smod(vals, smod);
    }

    linked_attrs_mod_backpointers(linkdn, config->managedtype, config->scope,
                                  LDAP_MOD_DELETE, vals);

    slapi_valueset_free(vals);
}

/*
 * linked_attrs_replace_backpointers()
 *
 * Remove backpointers pointing to linkdn from the entries
 * whose values were deleted in smod and add backpointers
 * for any new values that were added as a part of the
 * replace operation.
 */
static void
linked_attrs_replace_backpointers(Slapi_PBlock *pb, char *linkdn,
    struct configEntry *config, Slapi_Mod *smod)
{
    Slapi_Entry *pre_e = NULL;
    Slapi_Entry *post_e = NULL;
    Slapi_Attr *pre_attr = 0;
    Slapi_Attr *post_attr = 0;

    /* Get the pre and post copy of the entry to see
     * what values have been added and removed. */
    slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &pre_e);
    slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);

    if(pre_e && post_e) {
        slapi_entry_attr_find(pre_e, config->linktype, &pre_attr);
        slapi_entry_attr_find(post_e, config->linktype, &post_attr);
    }

    if(pre_attr || post_attr) {
        int pre_total = 0;
        int post_total = 0;
        Slapi_Value **pre_array = 0;
        Slapi_Value **post_array = 0;
        int pre_index = 0;
        int post_index = 0;
        Slapi_ValueSet *addvals = NULL;
        Slapi_ValueSet *delvals = NULL;

        /* create arrays of values */
        if(pre_attr) {
            slapi_attr_get_numvalues(pre_attr, &pre_total);
        }

        if(post_attr) {
            slapi_attr_get_numvalues(post_attr, &post_total);
        }

        if(pre_total) {
            pre_array = (Slapi_Value**) slapi_ch_malloc(sizeof(Slapi_Value*)*pre_total);
            linked_attrs_load_array(pre_array, pre_attr);
            qsort(pre_array, pre_total, sizeof(Slapi_Value*), linked_attrs_compare);
        }

        if(post_total) {
            post_array = (Slapi_Value**) slapi_ch_malloc(sizeof(Slapi_Value*)*post_total);
            linked_attrs_load_array(post_array, post_attr);
            qsort(post_array, post_total, sizeof(Slapi_Value*), linked_attrs_compare);
        }

        /* Work through arrays, following these rules:
         *   - in pre, in post, do nothing
         *   - in pre, not in post, delete from entry
         *   - not in pre, in post, add to entry
         */
        while(pre_index < pre_total || post_index < post_total) {
            if(pre_index == pre_total) {
                /* add the rest of post */
                if (addvals == NULL) {
                    addvals = slapi_valueset_new();
                }

                slapi_valueset_add_value(addvals, post_array[post_index]);
                post_index++;
            } else if(post_index == post_total) {
                /* delete the rest of pre */
                if (delvals == NULL) {
                    delvals = slapi_valueset_new();
                }

                slapi_valueset_add_value(delvals, pre_array[pre_index]);
                pre_index++;
            } else {
                /* decide what to do */
                int cmp = linked_attrs_compare(&(pre_array[pre_index]),
                                               &(post_array[post_index]));

                if(cmp < 0) {
                    /* delete pre array */
                    if (delvals == NULL) {
                        delvals = slapi_valueset_new();
                    }

                    slapi_valueset_add_value(delvals, pre_array[pre_index]);
                    pre_index++;
                } else if(cmp > 0) {
                    /* add post array */
                    if (addvals == NULL) {
                        addvals = slapi_valueset_new();
                    }

                    slapi_valueset_add_value(addvals, post_array[post_index]);
                    post_index++;
                } else {
                    /* do nothing, advance */
                    pre_index++;
                    post_index++;
                }
            }
        }

        /* Perform the actual updates to the target entries. */
        if (delvals) {
            linked_attrs_mod_backpointers(linkdn, config->managedtype,
                                          config->scope, LDAP_MOD_DELETE, delvals);
            slapi_valueset_free(delvals);
        }

        if (addvals) {
            linked_attrs_mod_backpointers(linkdn, config->managedtype,
                                          config->scope, LDAP_MOD_ADD, addvals);
            slapi_valueset_free(addvals);
        }

        slapi_ch_free((void **)&pre_array);
        slapi_ch_free((void **)&post_array);
    }
}

/*
 * linked_attrs_mod_backpointers()
 *
 * Performs backpointer management.
 */
static void
linked_attrs_mod_backpointers(char *linkdn, char *type,
    char *scope, int modop, Slapi_ValueSet *targetvals)
{
    char *val[2];
    int i = 0;
    Slapi_PBlock *mod_pb = slapi_pblock_new();
    LDAPMod mod;
    LDAPMod *mods[2];
    Slapi_Value *targetval = NULL;

    /* Setup the modify operation.  Only the target will
     * change, so we only need to do this once. */
    val[0] = linkdn;
    val[1] = 0;

    mod.mod_op = modop;
    mod.mod_type = type;
    mod.mod_values = val;

    mods[0] = &mod;
    mods[1] = 0;

    i = slapi_valueset_first_value(targetvals, &targetval);
    while(targetval)
    {
        int perform_update = 0;
        const char *targetdn = slapi_value_get_string(targetval);

        /* If we have a scope, only update the target if it is within
         * the scope.  If we don't have a scope, only update the target
         * if it is in the same backend as the linkdn. */
        if (scope) {
            perform_update = slapi_dn_issuffix(targetdn, scope);
        } else {
            Slapi_Backend *be = NULL;
            Slapi_DN *linksdn = slapi_sdn_new_dn_byref(linkdn);
            Slapi_DN *targetsdn = slapi_sdn_new_dn_byref(targetdn);

            if ((be = slapi_be_select(linksdn))) {
                perform_update = slapi_sdn_issuffix(targetsdn, slapi_be_getsuffix(be, 0));
            }

            slapi_sdn_free(&linksdn);
            slapi_sdn_free(&targetsdn);
        }

        if (perform_update) {
            slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
                            "%s backpointer (%s) in entry (%s)\n",
                            (modop == LDAP_MOD_ADD) ? "Adding" : "Removing",
                            linkdn, targetdn);

            /* Perform the modify operation. */
            slapi_modify_internal_set_pb(mod_pb, targetdn, mods, 0, 0,
                                         linked_attrs_get_plugin_id(), 0);
            slapi_modify_internal_pb(mod_pb);

            /* Initialize the pblock so we can reuse it. */
            slapi_pblock_init(mod_pb);
        }

        i = slapi_valueset_next_value(targetvals, i, &targetval);
    }

    slapi_pblock_destroy(mod_pb);
}


/*
 * Operation callback functions
 */

/*
 * linked_attrs_pre_op()
 *
 * Checks if an operation is modifying the linked
 * attribute config and validates the config changes.
 */
static int
linked_attrs_pre_op(Slapi_PBlock * pb, int modop)
{
    char *dn = 0;
    Slapi_Entry *e = 0;
    Slapi_Mods *smods = 0;
    LDAPMod **mods;
    int free_entry = 0;
    char *errstr = NULL;
    int ret = 0;

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_pre_op\n");

    /* Just bail if we aren't ready to service requests yet. */
    if (!g_plugin_started)
        goto bail;

    if (0 == (dn = linked_attrs_get_dn(pb)))
        goto bail;

    if (linked_attrs_dn_is_config(dn)) {
        /* Validate config changes, but don't apply them.
         * This allows us to reject invalid config changes
         * here at the pre-op stage.  Applying the config
         * needs to be done at the post-op stage. */

        if (LDAP_CHANGETYPE_ADD == modop) {
            slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
        } else {
            /* Fetch the entry being modified so we can
             * create the resulting entry for validation. */
            Slapi_DN *tmp_dn = slapi_sdn_new_dn_byref(dn);
            if (tmp_dn) {
                slapi_search_internal_get_entry(tmp_dn, 0, &e, linked_attrs_get_plugin_id());
                slapi_sdn_free(&tmp_dn);
                free_entry = 1;
            }

            /* If the entry doesn't exist, just bail and
             * let the server handle it. */
            if (e == NULL) {
                goto bail;
            }

            /* Grab the mods. */
            slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
            smods = slapi_mods_new();
            slapi_mods_init_passin(smods, mods);

            /* Apply the  mods to create the resulting entry. */
            if (mods && (slapi_entry_apply_mods(e, mods) != LDAP_SUCCESS)) {
                /* The mods don't apply cleanly, so we just let this op go
                 * to let the main server handle it. */
                goto bailmod;
            }
        }

        if (linked_attrs_parse_config_entry(e, 0) != 0) {
            /* Refuse the operation if config parsing failed. */
            ret = LDAP_UNWILLING_TO_PERFORM;
            if (LDAP_CHANGETYPE_ADD == modop) {
                errstr = slapi_ch_smprintf("Not a valid linked attribute configuration entry.");
            } else {
                errstr = slapi_ch_smprintf("Changes result in an invalid "
                                           "linked attribute configuration.");
            }
        }

      bailmod:
        /* Clean up smods. */
        if (LDAP_CHANGETYPE_MODIFY == modop) {
            mods = slapi_mods_get_ldapmods_passout(smods);
            slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
            slapi_mods_free(&smods);
        }
    }

  bail:
    if (free_entry && e)
        slapi_entry_free(e);

    if (ret) {
        slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_pre_op: operation failure [%d]\n", ret);
        slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
        slapi_ch_free((void **)&errstr);
        ret = -1;
    }

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_pre_op\n");

    return ret;
}

static int
linked_attrs_add_pre_op(Slapi_PBlock * pb)
{
    return linked_attrs_pre_op(pb, LDAP_CHANGETYPE_ADD);
}

static int
linked_attrs_mod_pre_op(Slapi_PBlock * pb)
{
    return linked_attrs_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
}

static int
linked_attrs_mod_post_op(Slapi_PBlock *pb)
{
    Slapi_Mods *smods = NULL;
    Slapi_Mod *smod = NULL;
    LDAPMod **mods;
    Slapi_Mod *next_mod = NULL;
    char *dn = NULL;
    struct configEntry *config = NULL;
    void *caller_id = NULL;

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_mod_post_op\n");

    /* Just bail if we aren't ready to service requests yet. */
    if (!g_plugin_started)
        return 0;

    /* We don't want to process internal modify
     * operations that originate from this plugin.
     * Doing so could cause a deadlock. */
    slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &caller_id);

    if (caller_id == linked_attrs_get_plugin_id()) {
        /* Just return without processing */
        return 0;
    }

    if (linked_attrs_oktodo(pb) &&
        (dn = linked_attrs_get_dn(pb))) {
        /* First check if the config is being modified. */
        if (linked_attrs_dn_is_config(dn)) {
            linked_attrs_load_config();
        }

        /* get the mod set */
        slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
        smods = slapi_mods_new();
        slapi_mods_init_byref(smods, mods);

        next_mod = slapi_mod_new();
        smod = slapi_mods_get_first_smod(smods, next_mod);
        while(smod) {
            char *type = (char *)slapi_mod_get_type(smod);

            /* See if there is an applicable link configured. */
            linked_attrs_read_lock();
            linked_attrs_find_config(dn, type, &config);

            /* If we have a matching config entry, we have
             * work to do. If not, we can go to the next smod. */
            if (config) {
                int op = slapi_mod_get_operation(smod);

                /* Prevent other threads from managing 
                 * this specific link at the same time. */
                slapi_lock_mutex(config->lock);

                switch(op & ~LDAP_MOD_BVALUES) {
                case LDAP_MOD_ADD:
                    /* Find the entries pointed to by the new
                     * values and add the backpointers. */
                    linked_attrs_add_backpointers(dn, config, smod);
                    break;
                case LDAP_MOD_DELETE:
                    /* Find the entries pointed to by the deleted
                     * values and remove the backpointers. */
                    linked_attrs_del_backpointers(pb, dn, config, smod);
                    break;
                case LDAP_MOD_REPLACE:
                    /* Find the entries pointed to by the deleted
                     * values and remove the backpointers.  If
                     * any new values are being added, find those
                     * entries and add the backpointers. */
                    linked_attrs_replace_backpointers(pb, dn, config, smod);
                    break;
                default:
                    slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
                                    "linked_attrs_mod_post_op: unknown mod type\n" );
                    break;
                }

                slapi_unlock_mutex(config->lock);
            }

            config = NULL;
            linked_attrs_unlock();
            slapi_mod_done(next_mod);
            smod = slapi_mods_get_next_smod(smods, next_mod);
        }

        slapi_mod_free(&next_mod);
        slapi_mods_free(&smods);
    }

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_mod_post_op\n");

    return 0;
}

static int
linked_attrs_add_post_op(Slapi_PBlock *pb)
{
    Slapi_Entry *e = NULL;
    char *dn = NULL;

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_add_post_op\n");

    /* Just bail if we aren't ready to service requests yet. */
    if (!g_plugin_started || !linked_attrs_oktodo(pb))
        return 0;

    /* Reload config if a config entry was added. */
    if ((dn = linked_attrs_get_dn(pb))) {
        if (linked_attrs_dn_is_config(dn))
            linked_attrs_load_config();
    } else {
        slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_add_post_op: Error "
                        "retrieving dn\n");
    }

    /* Get the newly added entry. */
    slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);

    if (e) {
        Slapi_Attr *attr = NULL;
        char *type = NULL;
        struct configEntry *config = NULL;

        slapi_entry_first_attr(e, &attr);
        while (attr) {
            slapi_attr_get_type(attr, &type);

            /* See if there is an applicable link configured. */
            linked_attrs_read_lock();
            linked_attrs_find_config(dn, type, &config);

            /* If config was found, add the backpointers to this entry. */
            if (config) {
                Slapi_ValueSet *vals = NULL;

                slapi_attr_get_valueset(attr, &vals);

                slapi_lock_mutex(config->lock);

                linked_attrs_mod_backpointers(dn, config->managedtype,
                                              config->scope, LDAP_MOD_ADD, vals);

                slapi_unlock_mutex(config->lock);

                slapi_valueset_free(vals);
            }

            config = NULL;
            linked_attrs_unlock();

            slapi_entry_next_attr(e, attr, &attr);
        }
    } else {
        slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_add_post_op: Error "
                        "retrieving post-op entry %s\n", dn);
    }

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_add_post_op\n");

    return 0;
}

static int
linked_attrs_del_post_op(Slapi_PBlock *pb)
{
    char *dn = NULL;
    Slapi_Entry *e = NULL;

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_del_post_op\n");

    /* Just bail if we aren't ready to service requests yet. */
    if (!g_plugin_started || !linked_attrs_oktodo(pb))
        return 0;

    /* Reload config if a config entry was deleted. */
    if ((dn = linked_attrs_get_dn(pb))) {
        if (linked_attrs_dn_is_config(dn))
            linked_attrs_load_config();
    } else {
        slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_del_post_op: Error "
                        "retrieving dn\n");
    }

    /* Get deleted entry, then go through types to find config. */
    slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &e );

    if (e) {
        Slapi_Attr *attr = NULL;
        char *type = NULL;
        struct configEntry *config = NULL;

        slapi_entry_first_attr(e, &attr);
        while (attr) {
            slapi_attr_get_type(attr, &type);

            /* See if there is an applicable link configured. */
            linked_attrs_read_lock();
            linked_attrs_find_config(dn, type, &config);

            /* If config was found, delete the backpointers to this entry. */
            if (config) {
                Slapi_ValueSet *vals = NULL;

                slapi_attr_get_valueset(attr, &vals);

                slapi_lock_mutex(config->lock);

                linked_attrs_mod_backpointers(dn, config->managedtype,
                                              config->scope, LDAP_MOD_DELETE, vals);

                slapi_unlock_mutex(config->lock);

                slapi_valueset_free(vals);
            }

            config = NULL;

            /* See if any of the values for this attribute are managed
             * backpointers.  We need to remove the forward link if so. */
            if (linked_attrs_config_index_has_type(type)) {
                int hint = 0;
                Slapi_Value *val = NULL;

                /* Loop through values and see if we have matching config */
                hint = slapi_attr_first_value(attr, &val);
                while (val) {
                    linked_attrs_find_config_reverse(slapi_value_get_string(val),
                                                      type, &config);

                    if (config) {
                        Slapi_ValueSet *vals = slapi_valueset_new();
                        slapi_valueset_add_value(vals, val);

                        slapi_lock_mutex(config->lock);

                        /* Delete forward link value. */
                        linked_attrs_mod_backpointers(dn, config->linktype,
                                                      config->scope, LDAP_MOD_DELETE, vals);

                        slapi_unlock_mutex(config->lock);

                        slapi_valueset_free(vals);
                        config = NULL;
                    }

                    hint = slapi_attr_next_value(attr, hint, &val);
                }
            }

            linked_attrs_unlock();

            slapi_entry_next_attr(e, attr, &attr);
        }
    } else {
        slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_del_post_op: Error "
                        "retrieving pre-op entry %s\n", dn);
    }

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_del_post_op\n");

    return 0;
}

static int
linked_attrs_modrdn_post_op(Slapi_PBlock *pb)
{
    char *old_dn = NULL;
    char *new_dn = NULL;
    Slapi_Entry *post_e = NULL;
    Slapi_Attr *attr = NULL;
    char *type = NULL;
    struct configEntry *config = NULL;
    int rc = 0;

    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "--> linked_attrs_modrdn_post_op\n");

    /* Just bail if we aren't ready to service requests yet. */
    if (!g_plugin_started || !linked_attrs_oktodo(pb)) {
        goto done;
    }

    /* Reload config if an existing config entry was renamed,
     * or if the new dn brings an entry into the scope of the
     * config entries. */
    slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
    if (post_e) {
        new_dn = slapi_entry_get_ndn(post_e);
    } else {
        slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_modrdn_post_op: Error "
                        "retrieving post-op entry\n");
        rc = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    if ((old_dn = linked_attrs_get_dn(pb))) {
        if (linked_attrs_dn_is_config(old_dn) || linked_attrs_dn_is_config(new_dn))
            linked_attrs_load_config();
    } else {
        slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
                        "linked_attrs_modrdn_post_op: Error "
                        "retrieving dn\n");
        rc = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    /* Check if this operation requires any updates to links. */
    slapi_entry_first_attr(post_e, &attr);
    while (attr) {
        slapi_attr_get_type(attr, &type);

        /* See if there is an applicable link configured. */
        linked_attrs_read_lock();
        linked_attrs_find_config(old_dn, type, &config);

        /* If config was found for the old dn, delete the backpointers
         * to this entry. */
        if (config) {
            Slapi_ValueSet *vals = NULL;

            slapi_attr_get_valueset(attr, &vals);

            slapi_lock_mutex(config->lock);

            /* Delete old dn value. */
            linked_attrs_mod_backpointers(old_dn, config->managedtype,
                                          config->scope, LDAP_MOD_DELETE, vals);

            slapi_unlock_mutex(config->lock);

            slapi_valueset_free(vals);
            config = NULL;
        }

        linked_attrs_find_config(new_dn, type, &config);

        /* If config was found for the new dn, add the backpointers
         * to this entry.  We do this separate check for both dn's
         * to catch an entry that comes into or goes out of scope
         * from the MODRDN operation. */
        if (config) {
            Slapi_ValueSet *vals = NULL;

            slapi_attr_get_valueset(attr, &vals);

            slapi_lock_mutex(config->lock);

            /* Add new dn value. */
            linked_attrs_mod_backpointers(new_dn, config->managedtype,
                                          config->scope, LDAP_MOD_ADD, vals);

            slapi_unlock_mutex(config->lock);

            slapi_valueset_free(vals);
            config = NULL;
        }

        /* See if any of the values for this attribute are managed
         * backpointers.  We need to update the forward link if so. */
        if (linked_attrs_config_index_has_type(type)) {
            int hint = 0;
            Slapi_Value *val = NULL;

            /* Loop through values and see if we have matching config */
            hint = slapi_attr_first_value(attr, &val);
            while (val) {
                linked_attrs_find_config_reverse(slapi_value_get_string(val),
                                                  type, &config);

                /* If the new DN is within scope, we should fixup the forward links. */
                if (config && slapi_dn_issuffix(new_dn, (config->scope))) {
                    Slapi_ValueSet *vals = slapi_valueset_new();
                    slapi_valueset_add_value(vals, val);

                    slapi_lock_mutex(config->lock);

                    /* Delete old dn value. */
                    linked_attrs_mod_backpointers(old_dn, config->linktype,
                                                  config->scope, LDAP_MOD_DELETE, vals);

                    /* Add new dn value. */
                    linked_attrs_mod_backpointers(new_dn, config->linktype,
                                                  config->scope, LDAP_MOD_ADD, vals);

                    slapi_unlock_mutex(config->lock);

                    slapi_valueset_free(vals);
                    config = NULL;
                }

                hint = slapi_attr_next_value(attr, hint, &val);
            }
        }

        linked_attrs_unlock();

        slapi_entry_next_attr(post_e, attr, &attr);
    }
done:
    slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
                    "<-- linked_attrs_modrdn_post_op\n");

    return rc;
}


/*
 * Debug functions to print config
 */
void
linked_attrs_dump_config()
{
    PRCList *list;

    linked_attrs_read_lock();

    if (!PR_CLIST_IS_EMPTY(g_link_config)) {
        list = PR_LIST_HEAD(g_link_config);
        while (list != g_link_config) {
            linked_attrs_dump_config_entry((struct configEntry *)list);
            list = PR_NEXT_LINK(list);
        }
    }

    linked_attrs_unlock();
}

void
linked_attrs_dump_config_index()
{
    PRCList *list;

    linked_attrs_read_lock();

    if (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
        list = PR_LIST_HEAD(g_managed_config_index);
        while (list != g_managed_config_index) {
            linked_attrs_dump_config_entry(((struct configIndex *)list)->config);
            list = PR_NEXT_LINK(list);
        }
    }

    linked_attrs_unlock();
}


void
linked_attrs_dump_config_entry(struct configEntry * entry)
{
    slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                    "<==== Linked Attribute Pair =====>\n");
    slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                    "<---- config entry dn -----> %s\n", entry->dn);
    slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                    "<---- link type -----------> %s\n", entry->linktype);
    slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                    "<---- managed type --------> %s\n", entry->managedtype);
    slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
                    "<---- scope ---------------> %s\n", entry->scope);
}