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
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
|
/*
* Copyright 2008 Red Hat, Inc.
*
* 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
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <fnmatch.h>
#include <regex.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_DIRSRV_SLAPI_PLUGIN_H
#include <nspr.h>
#include <nss.h>
#include <dirsrv/slapi-plugin.h>
#else
#include <slapi-plugin.h>
#endif
#include <rpcsvc/yp.h>
#include "backend.h"
#include "back-shr.h"
#include "format.h"
#include "plugin.h"
#define DEFAULT_BUFFER_SIZE 0x1000
#define MAX_BUFFER_SIZE 0x100000
struct format_choice {
char *offset;
int n_values;
struct berval **values;
struct format_choice *next;
};
static int format_expand(struct plugin_state *state,
Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *fmt, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs,
struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list);
static char *
xstrndup(const char *start, size_t length)
{
char *ret, *end;
end = memchr(start, '\0', length);
if (end != NULL) {
length = end - start;
}
ret = malloc(length + 1);
if (ret != NULL) {
memcpy(ret, start, length);
ret[length] = '\0';
}
return ret;
}
static char *
xstrndupp(const char *start, const char *end)
{
return xstrndup(start, end - start);
}
static void *
xmemdup(char *region, int size)
{
char *ret;
ret = malloc(size + 1);
if (ret != NULL) {
if (size > 0) {
memcpy(ret, region, size);
}
ret[size] = '\0';
}
return ret;
}
/* Maintain a DN list, which is list of distinguished names. */
void
format_free_sdn_list(struct slapi_dn **list)
{
unsigned int i;
if (list != NULL) {
for (i = 0; list[i] != NULL; i++) {
slapi_sdn_free(&(list[i]));
}
free(list);
}
}
struct slapi_dn **
format_dup_sdn_list(struct slapi_dn **list)
{
struct slapi_dn **ret;
unsigned int i;
for (i = 0; (list != NULL) && (list[i] != NULL); i++) {
continue;
}
ret = malloc((i + 1) * sizeof(struct slapi_dn*));
if (ret != NULL) {
for (i = 0; (list != NULL) && (list[i] != NULL); i++) {
ret[i] = slapi_sdn_dup(list[i]);
}
ret[i] = NULL;
}
return ret;
}
struct slapi_dn **
format_make_sdn_list(char **list)
{
struct slapi_dn **ret;
unsigned int i;
for (i = 0; (list != NULL) && (list[i] != NULL); i++) {
continue;
}
ret = malloc((i + 1) * sizeof(struct slapi_dn*));
if (ret != NULL) {
for (i = 0; (list != NULL) && (list[i] != NULL); i++) {
ret[i] = slapi_sdn_new_dn_byval(list[i]);
}
ret[i] = NULL;
}
return ret;
}
struct slapi_dn **
format_add_sdn_list(struct slapi_dn ***list, const char *dn)
{
struct slapi_dn **ret, *sdn;
unsigned int i;
sdn = slapi_sdn_new_dn_byval(dn);
for (i = 0;
(list != NULL) && (*list != NULL) && ((*list)[i] != NULL);
i++) {
if (slapi_sdn_compare(sdn, (*list)[i]) == 0) {
slapi_sdn_free(&sdn);
return *list;
}
continue;
}
ret = malloc((i + 2) * sizeof(struct slapi_dn*));
if (ret != NULL) {
for (i = 0;
(list != NULL) && (*list != NULL) && ((*list)[i] != NULL);
i++) {
ret[i] = slapi_sdn_dup((*list)[i]);
}
ret[i++] = sdn;
ret[i] = NULL;
format_free_sdn_list(*list);
*list = ret;
}
return *list;
}
/* Maintain a reference attribute list, which is group of lists of attribute
* names and search bases. */
void
format_free_ref_attr_list(struct format_ref_attr_list **list)
{
Slapi_DN **sdn_list;
unsigned int i;
int j;
if (list != NULL) {
for (i = 0; list[i] != NULL; i++) {
for (j = 0; j < list[i]->n_links; j++) {
free(list[i]->links[j].attribute);
sdn_list = list[i]->links[j].base_sdn_list;
format_free_sdn_list(sdn_list);
}
free(list[i]->links);
free(list[i]->set);
free(list[i]->group);
free(list[i]);
}
free(list);
}
}
struct format_ref_attr_list **
format_dup_ref_attr_list(struct format_ref_attr_list **list)
{
struct format_ref_attr_list **ret;
struct slapi_dn **sdn_list;
unsigned int i;
int j;
for (i = 0; (list != NULL) && (list[i] != NULL); i++) {
continue;
}
ret = malloc((i + 1) * sizeof(struct format_ref_attr_list*));
if (ret != NULL) {
memset(ret, 0, (i + 1) * sizeof(struct format_ref_attr_list*));
for (i = 0; (list != NULL) && (list[i] != NULL); i++) {
ret[i] = malloc(sizeof(*(list[i])));
if (ret[i] == NULL) {
format_free_ref_attr_list(ret);
return NULL;
}
memset(ret[i], 0, sizeof(*(ret[i])));
ret[i]->links = malloc(sizeof(*(ret[i]->links)) *
list[i]->n_links);
if (ret[i]->links == NULL) {
format_free_ref_attr_list(ret);
return NULL;
}
for (j = 0; j < list[i]->n_links; j++) {
ret[i]->links[j].attribute =
strdup(list[i]->links[j].attribute);
if (ret[i]->links[j].attribute == NULL) {
format_free_ref_attr_list(ret);
return NULL;
}
sdn_list = list[i]->links[j].base_sdn_list;
ret[i]->links[j].base_sdn_list =
format_dup_sdn_list(sdn_list);
ret[i]->n_links++;
}
ret[i]->group = strdup(list[i]->group);
ret[i]->set = strdup(list[i]->set);
}
ret[i] = NULL;
}
return ret;
}
static struct format_ref_attr_list *
format_find_ref_attr_list(struct format_ref_attr_list **list,
const char *group, const char *set,
const char **names)
{
unsigned int i;
int j;
const char *attribute;
for (i = 0; (list != NULL) && (list[i] != NULL); i++) {
for (j = 0; names[j] != NULL; j++) {
if (j < list[i]->n_links) {
attribute = list[i]->links[j].attribute;
if (strcmp(names[j], attribute) != 0) {
break;
}
}
}
if ((j == list[i]->n_links) && (names[j] == NULL)) {
return list[i];
}
}
return NULL;
}
static struct format_ref_attr_list **
format_add_ref_attr_list(struct format_ref_attr_list ***list,
const char *group, const char *set,
const char **names)
{
struct format_ref_attr_list **ret;
unsigned int i;
int j;
if (format_find_ref_attr_list(*list, group, set, names) != NULL) {
return *list;
}
for (i = 0;
(list != NULL) && (*list != NULL) && ((*list)[i] != NULL);
i++) {
continue;
}
ret = malloc((i + 2) * sizeof(struct format_ref_attr_list*));
if (ret != NULL) {
memcpy(ret, *list, i * sizeof(struct format_ref_attr_list*));
free(*list);
*list = NULL;
ret[i] = malloc(sizeof(*(ret[i])));
if (ret[i] == NULL) {
format_free_ref_attr_list(ret);
return NULL;
}
memset(ret[i], 0, sizeof(*(ret[i])));
for (j = 0; names[j] != NULL; j++) {
continue;
}
ret[i]->links = malloc(sizeof(*(ret[i]->links)) * j);
if (ret[i]->links == NULL) {
format_free_ref_attr_list(ret);
return NULL;
}
memset(ret[i]->links, 0, sizeof(*(ret[i]->links)) * j);
ret[i]->n_links = j;
for (j = 0; j < ret[i]->n_links; j++) {
ret[i]->links[j].attribute = strdup(names[j]);
if (ret[i]->links[j].attribute == NULL) {
format_free_ref_attr_list(ret);
return NULL;
}
ret[i]->links[j].base_sdn_list = NULL;
}
ret[i]->group = strdup(group);
ret[i]->set = strdup(set);
i++;
ret[i] = NULL;
*list = ret;
}
return ret;
}
/* Maintain an entry list. The list takes ownership of entries which are
* added to it. */
struct entry_list {
Slapi_Entry *entry;
struct entry_list *next;
};
static struct entry_list *
format_entry_list_remove_one(struct entry_list *list)
{
struct entry_list *next;
if (list == NULL) {
next = NULL;
} else {
next = list->next;
slapi_entry_free(list->entry);
free(list);
}
return next;
}
static struct entry_list *
format_add_entry_list(struct entry_list *list, Slapi_Entry *entry)
{
struct entry_list *next, *tail;
next = malloc(sizeof(*next));
if (next == NULL) {
return list;
} else {
next->entry = entry;
next->next = NULL;
if (list == NULL) {
return next;
} else {
for (tail = list;
tail->next != NULL;
tail = tail->next) {
continue;
}
tail->next = next;
return list;
}
}
}
static struct entry_list *
format_add_entry_list_dn(struct entry_list *list,
const char *dn, char **attributes,
void *plugin_identity)
{
Slapi_Entry *entry;
Slapi_DN *sdn;
sdn = slapi_sdn_new_dn_byval(dn);
if (sdn != NULL) {
entry = NULL;
wrap_search_internal_get_entry(sdn, attributes, &entry,
plugin_identity);
slapi_sdn_free(&sdn);
if (entry != NULL) {
return format_add_entry_list(list, entry);
} else {
return list;
}
} else {
return NULL;
}
}
/* Maintain an attribute list, which is really just a string list. Entries
* named by an attribute in the list carry "interesting" information. */
char **
format_dup_attr_list(char **attr_list)
{
return backend_shr_dup_strlist(attr_list);
}
void
format_free_attr_list(char **attr_list)
{
return backend_shr_free_strlist(attr_list);
}
void
format_add_attrlist(char ***attrlist, const char *attribute)
{
backend_shr_add_strlist(attrlist, attribute);
}
/* Maintain an inref attribute list, which tracks a group and set name and an
* attribute. If an entry in the group and set contains this entry's name in
* the named attribute, then it's "interesting". */
struct format_inref_attr **
format_dup_inref_attrs(struct format_inref_attr **attrs)
{
int i, j, elements;
struct format_inref_attr **ret;
elements = 0;
ret = NULL;
if (attrs != NULL) {
for (i = 0; attrs[i] != NULL; i++) {
continue;
}
elements = i;
ret = malloc(sizeof(*ret) * (elements + 1));
if (ret != NULL) {
for (i = 0, j = 0; i < elements; i++) {
ret[j] = malloc(sizeof(**attrs));
if (ret[j] != NULL) {
ret[j]->group =
strdup(attrs[i]->group);
ret[j]->set = strdup(attrs[i]->set);
ret[j]->attribute =
strdup(attrs[i]->attribute);
if ((ret[j]->group != NULL) &&
(ret[j]->set != NULL) &&
(ret[j]->attribute != NULL)) {
j++;
}
}
}
ret[j] = NULL;
}
}
return ret;
}
void
format_add_inref_attrs(struct format_inref_attr ***attrs,
const char *group, const char *set,
const char *attribute)
{
struct format_inref_attr **ret;
int i, elements;
elements = 0;
ret = NULL;
if (*attrs != NULL) {
for (i = 0; (*attrs)[i] != NULL; i++) {
if ((strcmp((*attrs)[i]->group, group) == 0) &&
(strcmp((*attrs)[i]->set, set) == 0) &&
(strcmp((*attrs)[i]->attribute, attribute) == 0)) {
return;
}
}
elements = i;
}
ret = malloc(sizeof(*ret) * (elements + 2));
if (ret != NULL) {
memcpy(ret, *attrs, elements * sizeof(**attrs));
ret[elements] = malloc(sizeof(**ret));
if (ret[elements] != NULL) {
ret[elements]->group = strdup(group);
ret[elements]->set = strdup(set);
ret[elements]->attribute = strdup(attribute);
ret[elements + 1] = NULL;
}
free(*attrs);
*attrs = ret;
}
}
void
format_free_inref_attrs(struct format_inref_attr **attrs)
{
int i;
if (attrs != NULL) {
for (i = 0; attrs[i] != NULL; i++) {
free(attrs[i]->group);
free(attrs[i]->set);
free(attrs[i]->attribute);
free(attrs[i]);
}
free(attrs);
}
}
/* Maintain berval lists. */
static int
format_count_bv_list(struct berval **bvlist)
{
int i;
if (bvlist != NULL) {
for (i = 0; bvlist[i] != NULL; i++) {
continue;
}
return i;
}
return 0;
}
static void
format_free_bv_list(struct berval **bvlist)
{
int i;
if (bvlist != NULL) {
for (i = 0; bvlist[i] != NULL; i++) {
free(bvlist[i]->bv_val);
free(bvlist[i]);
}
free(bvlist);
}
}
static struct berval **
format_dup_bv_list(struct berval **bvlist)
{
struct berval **ret, *bv;
int i;
ret = NULL;
if (bvlist != NULL) {
for (i = 0; bvlist[i] != NULL; i++) {
continue;
}
if (i == 0) {
return NULL;
}
ret = malloc((i + 1) * sizeof(struct berval *));
if (ret != NULL) {
for (i = 0; bvlist[i] != NULL; i++) {
ret[i] = malloc(sizeof(struct berval));
if (ret[i] != NULL) {
bv = bvlist[i];
ret[i]->bv_val = xmemdup(bv->bv_val,
bv->bv_len);
ret[i]->bv_len = bv->bv_len;
}
}
ret[i] = NULL;
}
}
return ret;
}
static void
format_add_bv_list(struct berval ***bvlist, const struct berval *bv)
{
struct berval **list;
int i;
if (bvlist == NULL) {
return;
}
if (*bvlist != NULL) {
for (i = 0; (*bvlist)[i] != NULL; i++) {
continue;
}
} else {
i = 0;
}
list = malloc((i + 2) * sizeof(struct berval *));
if (list != NULL) {
if (i > 0) {
memcpy(list, *bvlist, i * sizeof(struct berval *));
}
list[i] = malloc(sizeof(struct berval));
if (list[i] != NULL) {
list[i]->bv_val = xmemdup(bv->bv_val, bv->bv_len);
if (list[i]->bv_val != NULL) {
list[i]->bv_len = bv->bv_len;
list[i + 1] = NULL;
free(*bvlist);
*bvlist = list;
} else {
free(list[i]);
free(list);
format_free_bv_list(*bvlist);
*bvlist = NULL;
}
} else {
format_free_bv_list(*bvlist);
*bvlist = NULL;
}
} else {
format_free_bv_list(*bvlist);
*bvlist = NULL;
}
}
/* Maintain "choices" lists. */
static void
format_retarget_choicesp(struct format_choice **choices, char *oldt, char *newt)
{
struct format_choice *this_choice;
int offset;
if (choices != NULL) {
for (this_choice = *choices;
this_choice != NULL;
this_choice = this_choice->next) {
offset = this_choice->offset - oldt;
this_choice->offset = newt + offset;
}
}
}
static void
format_free_choices(struct format_choice *choices)
{
struct format_choice *next;
while (choices != NULL) {
next = choices->next;
format_free_bv_list(choices->values);
free(choices);
choices = next;
}
}
static void
format_free_choicesp(struct format_choice **choices)
{
if (choices) {
format_free_choices(*choices);
*choices = NULL;
}
}
static void
format_append_choice(struct format_choice **choices,
struct format_choice *choice)
{
struct format_choice *here;
if (choices == NULL) {
return;
}
if (*choices == NULL) {
*choices = choice;
} else {
here = *choices;
while (here->next != NULL) {
here = here->next;
}
choice->next = here->next;
here->next = choice;
}
}
static void
format_add_choice(struct format_choice **choices, char *offset,
struct berval **values)
{
struct format_choice *choice;
int i;
if (values != NULL) {
choice = malloc(sizeof(*choice));
if (choice != NULL) {
choice->offset = offset;
choice->next = NULL;
for (i = 0; values[i] != NULL; i++) {
continue;
}
choice->n_values = i;
choice->values = format_dup_bv_list(values);
if (choice->values != NULL) {
format_append_choice(choices, choice);
} else {
free(choice);
}
}
}
}
/* Convert a strlist to a berval list. */
static struct berval **
format_strlist_to_bv_list(char **values)
{
struct berval **val;
int i;
char *p;
if (values != NULL) {
for (i = 0; values[i] != NULL; i++) {
continue;
}
val = malloc(sizeof(struct berval *) * (i + 1));
if (val != NULL) {
for (i = 0; values[i] != NULL; i++) {
val[i] = malloc(sizeof(struct berval));
if (val[i] != NULL) {
p = values[i];
val[i]->bv_val = xmemdup(p, strlen(p));
if (val[i] != NULL) {
val[i]->bv_len = strlen(p);
}
}
}
val[i] = NULL;
return val;
}
}
return NULL;
}
static void
format_add_choice_str(struct format_choice **choices, char *offset,
char **values)
{
struct berval **vals;
vals = format_strlist_to_bv_list(values);
format_add_choice(choices, offset, vals);
format_free_bv_list(vals);
}
/* Parse an argument string into an array of arguments. */
static void
format_free_parsed_args(char **argv)
{
free(argv);
}
static int
format_parse_args(struct plugin_state *state, const char *args,
int *pargc, char ***pargv)
{
int i, dq, argc;
char *out, **argv;
*pargc = 0;
*pargv = NULL;
argv = malloc((sizeof(char *) + 1) * (strlen(args) + 1));
if (argv == NULL) {
return -1;
}
memset(argv, 0, (sizeof(char *) + 1) * (strlen(args) + 1));
out = (char *) argv;
out += sizeof(char *) * (strlen(args) + 1);
argc = 0;
i = 0;
dq = 0;
while (args[i] != '\0') {
switch (args[i]) {
case '"':
dq = !dq;
if (dq) {
argv[argc++] = out;
} else {
*out++ = '\0';
}
i++;
break;
case '\\':
i++;
/* fall through */
default:
*out++ = args[i++];
break;
}
}
argv[argc] = NULL;
*out = '\0';
out = malloc((argc * 3) + strlen(args));
if (out != NULL) {
*out = '\0';
for (i = 0; i < argc; i++) {
if (i > 0) {
strcat(out, ",");
}
strcat(out, "'");
strcat(out, argv[i]);
strcat(out, "'");
}
free(out);
}
*pargc = argc;
*pargv = argv;
return 0;
}
/* Choose the first value of the set of results for the first argument, and if
* we get no results, return the second argument. */
static int
format_first(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
int ret, i, argc, first, common_length;
char **argv, **values;
const char *value_format, *default_value;
unsigned int *lengths;
ret = format_parse_args(state, args, &argc, &argv);
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"first: error parsing arguments\n");
return -EINVAL;
}
if (argc < 1) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"first: error parsing arguments\n");
format_free_parsed_args(argv);
return -EINVAL;
}
if (argc < 2) {
value_format = argv[0];
default_value = NULL;
} else {
value_format = argv[0];
default_value = argv[1];
}
ret = -ENOENT;
values = format_get_data_set(state, e, group, set,
value_format, disallowed,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list,
&lengths);
if (values == NULL) {
if (default_value == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"first: no values for ->%s<-, "
"and no default value provided\n",
value_format);
ret = -ENOENT;
} else {
i = format_expand(state, pb, e,
group, set,
default_value, NULL,
outbuf, outbuf_len,
outbuf_choices,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list);
ret = i;
}
} else {
first = 0;
for (i = 1; values[i] != NULL; i++) {
/* Check if this entry sorts "earlier" than the current
* "first" entry. If it does, note its position as the
* "first". */
common_length = lengths[first] < lengths[i] ?
lengths[first] : lengths[i];
ret = memcmp(values[i], values[first], common_length);
if ((ret < 0) ||
((ret == 0) && (lengths[i] < lengths[first]))) {
first = i;
}
}
if ((int) lengths[first] > outbuf_len) {
ret = -ENOBUFS;
} else {
memcpy(outbuf, values[first], lengths[first]);
ret = lengths[first];
}
format_free_data_set(values, lengths);
}
format_free_parsed_args(argv);
return ret;
}
/* Look up the entries matching DNs stored in the attribute named by the first
* argument, pull out the values for the attribute named by the second
* argument, and return a list of those values. */
static int
format_deref(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
int i, j, ret, argc;
Slapi_Entry *ref;
Slapi_DN *refdn;
Slapi_ValueSet *ref_values, *values;
Slapi_Value *ref_value, *value;
int disposition, ref_disposition, buffer_flags, ref_buffer_flags;
char **argv, *attrs[3], *actual_attr, *actual_ref_attr;
const char *cref;
const struct berval *val;
char *ref_attr, *target_attr;
struct berval **choices;
ret = format_parse_args(state, args, &argc, &argv);
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"deref: error parsing arguments\n");
return -EINVAL;
}
if (argc != 2) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"deref: requires two arguments\n");
format_free_parsed_args(argv);
return -EINVAL;
}
if (outbuf_choices == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"deref: returns a list, but a list "
"would not be appropriate\n");
format_free_parsed_args(argv);
return -EINVAL;
}
ref_attr = argv[0];
target_attr = argv[1];
/* Note that the attribute in this entry refers to other entries. */
if (ref_attrs != NULL) {
format_add_attrlist(ref_attrs, ref_attr);
}
/* Get the values of the reference attribute from this entry. */
if (slapi_vattr_values_get(e, ref_attr, &ref_values,
&ref_disposition, &actual_ref_attr,
0, &ref_buffer_flags) != 0) {
/* No references means we're done. */
format_free_parsed_args(argv);
return 0;
}
/* Retrieve these attributes from the referred-to entries. */
attrs[0] = ref_attr;
attrs[1] = target_attr;
attrs[2] = NULL;
/* Iterate through the names of the referred-to entries. */
choices = NULL;
for (i = slapi_valueset_first_value(ref_values, &ref_value);
i != -1;
i = slapi_valueset_next_value(ref_values, i, &ref_value)) {
/* Pull up the referred-to entry. */
cref = slapi_value_get_string(ref_value);
if (cref == NULL) {
continue;
}
refdn = slapi_sdn_new_dn_byval(cref);
if (refdn == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"deref: internal error parsing name "
"\"%s\"\n", cref);
continue;
}
ref = NULL;
wrap_search_internal_get_entry(refdn, attrs, &ref,
state->plugin_identity);
if (ref == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"deref: failure reading entry \"%s\"\n",
slapi_sdn_get_ndn(refdn));
slapi_sdn_free(&refdn);
continue;
}
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"deref: reading \"%s\" from \"%s\"\n",
target_attr, slapi_sdn_get_ndn(refdn));
slapi_sdn_free(&refdn);
/* Pull out the attribute from the referred-to entry. */
if (slapi_vattr_values_get(ref, target_attr, &values,
&disposition, &actual_attr,
0, &buffer_flags) != 0) {
slapi_entry_free(ref);
continue;
}
for (j = slapi_valueset_first_value(values, &value);
j != -1;
j = slapi_valueset_next_value(values, j, &value)) {
/* Get the value. */
val = slapi_value_get_berval(value);
/* If the value is empty, skip it. */
if (val->bv_len == 0) {
continue;
}
format_add_bv_list(&choices, val);
}
slapi_vattr_values_free(&values, &actual_attr, buffer_flags);
slapi_entry_free(ref);
}
slapi_vattr_values_free(&ref_values,
&actual_ref_attr,
ref_buffer_flags);
format_free_parsed_args(argv);
/* Return any values we found. */
if (choices != NULL) {
format_add_choice(outbuf_choices, outbuf, choices);
format_free_bv_list(choices);
return 0;
} else {
return -ENOENT;
}
}
/* For the first N-1 arguments, look up the entries matching DNs stored in the
* attribute named by the argument, following the chain of named entries, and
* at last, pull out the values for the attribute named by the last argument,
* and return a list of those values. */
static int
format_deref_r(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
int i, j, k, ret, argc;
Slapi_Entry *entry;
Slapi_DN **these, **next;
Slapi_ValueSet *values;
Slapi_Value *value;
int disposition, buffer_flags;
char **argv, *attrs[2], *actual_attr;
const char *dn, *cvalue;
const struct berval *bval;
struct berval **choices;
struct format_ref_attr_list *list;
ret = format_parse_args(state, args, &argc, &argv);
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"deref_r: error parsing arguments\n");
return -EINVAL;
}
if (argc < 2) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"deref_r: requires at least two arguments\n");
format_free_parsed_args(argv);
return -EINVAL;
}
if (outbuf_choices == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"deref_r: returns a list, but a list "
"would not be appropriate\n");
format_free_parsed_args(argv);
return -EINVAL;
}
/* Note that this list of attributes is used for pulling up data. */
format_add_ref_attr_list(ref_attr_list, group, set,
(const char **) argv);
list = format_find_ref_attr_list(*ref_attr_list, group, set,
(const char **) argv);
/* Follow the chain: set up the first link. */
these = NULL;
choices = NULL;
dn = slapi_entry_get_dn(e);
format_add_sdn_list(&these, dn);
/* For the first N-1 links, read the contents of the named attribute
* from each entry we're examining at this point, and use the values
* to build a list of entries to visit the next time. For the last
* link, pull the values out and prep them to be returned to our
* caller. */
for (i = 0; (these != NULL) && (i < list->n_links); i++) {
next = NULL;
attrs[0] = list->links[i].attribute;
attrs[1] = NULL;
/* Walk the set of entries for this iteration. */
for (j = 0; these[j] != NULL; j++) {
/* For heuristic use later, note the parent of the
* entry from which we're reading this attribute. */
dn = slapi_sdn_get_ndn(these[j]);
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"deref_r: noting parent "
"\"%s\" for \"%s\"\n",
slapi_dn_parent(dn), attrs[0]);
format_add_sdn_list(&list->links[i].base_sdn_list,
slapi_dn_parent(dn));
/* Pull up the named entry. */
entry = NULL;
wrap_search_internal_get_entry(these[j], attrs, &entry,
state->plugin_identity);
if (entry == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"deref_r: error reading entry "
"\"%s\"\n",
slapi_sdn_get_dn(these[j]));
continue;
} else {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"deref_r: reading entry "
"\"%s\" (%d)\n",
slapi_sdn_get_dn(these[j]), i);
}
/* Pull up the value set. */
if (slapi_vattr_values_get(entry, attrs[0], &values,
&disposition,
&actual_attr,
0, &buffer_flags) != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"deref_r: entry \"%s\" has no "
"values for \"%s\"\n",
slapi_sdn_get_dn(these[j]),
attrs[0]);
slapi_entry_free(entry);
continue;
}
for (k = slapi_valueset_first_value(values, &value);
k != -1;
k = slapi_valueset_next_value(values, k, &value)){
if (i < list->n_links - 1) {
/* Get the text. */
cvalue = slapi_value_get_string(value);
/* If the value is empty, skip it. */
if (cvalue == NULL) {
continue;
}
/* Let's visit the named entry this
* time, in case we're nesting. */
format_add_sdn_list(&these, cvalue);
/* We need to visit the named entry
* next time. */
format_add_sdn_list(&next, cvalue);
} else {
/* Get the value. */
bval = slapi_value_get_berval(value);
/* If the value is empty, skip it. */
if (bval->bv_len == 0) {
continue;
}
format_add_bv_list(&choices, bval);
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"deref_r: found value "
"\"%.*s\" in \"%s\"\n",
bval->bv_len,
bval->bv_val, dn);
}
}
slapi_vattr_values_free(&values, &actual_attr,
buffer_flags);
slapi_entry_free(entry);
}
/* Replace the list of entries we're examining now with the
* list of entries we need to examine next. */
format_free_sdn_list(these);
these = next;
next = NULL;
}
/* Clean up and return any values we found. */
format_free_sdn_list(these);
format_free_parsed_args(argv);
if (choices != NULL) {
format_add_choice(outbuf_choices, outbuf, choices);
format_free_bv_list(choices);
return 0;
} else {
return -ENOENT;
}
}
/* Look up entries in the set named by the first argument, which have this
* entry's DN stored in the attribute named by the second argument, pull out
* the values for the attribute named by the third argument, and return a list
* of those values. */
struct format_referred_cbdata {
struct plugin_state *state;
char *attr;
struct berval **choices;
};
static int
format_referred_entry_cb(Slapi_Entry *e, void *callback_data)
{
Slapi_ValueSet *values;
Slapi_Value *value;
int i, disposition, buffer_flags;
char *actual_attr;
const struct berval *val;
struct format_referred_cbdata *cbdata;
cbdata = callback_data;
slapi_log_error(SLAPI_LOG_PLUGIN,
cbdata->state->plugin_desc->spd_id,
"referred: examining \"%s\" in \%s\"\n",
cbdata->attr, slapi_entry_get_ndn(e));
/* Iterate through the values for the specified attribute. */
if (slapi_vattr_values_get(e, cbdata->attr, &values,
&disposition, &actual_attr,
0, &buffer_flags) != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN,
cbdata->state->plugin_desc->spd_id,
"referred: no values for \"%s\" in \"%s\"\n",
cbdata->attr, slapi_entry_get_ndn(e));
return 0;
}
for (i = slapi_valueset_first_value(values, &value);
i != -1;
i = slapi_valueset_next_value(values, i, &value)) {
/* Pull up the value. */
val = slapi_value_get_berval(value);
if (val->bv_len == 0) {
continue;
}
slapi_log_error(SLAPI_LOG_PLUGIN,
cbdata->state->plugin_desc->spd_id,
"referred: got %d-byte value for \"%s\"\n",
val->bv_len, actual_attr);
/* Add it to the list of values we've retrieved. */
format_add_bv_list(&cbdata->choices, val);
}
slapi_vattr_values_free(&values, &actual_attr, buffer_flags);
return 0;
}
static int
format_referred(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
int i, ret, argc;
Slapi_PBlock *local_pb;
char **argv, *attrs[2], *filter, *tndn, *attr, *other_attr;
char *other_set, *set_filter, **set_bases, *use_filter;
struct format_referred_cbdata cbdata;
ret = format_parse_args(state, args, &argc, &argv);
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"referred: error parsing arguments\n");
return -EINVAL;
}
if (argc != 3) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"referred: requires 3 arguments\n");
format_free_parsed_args(argv);
return -EINVAL;
}
if (outbuf_choices == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"referred: returns a list, but a list would "
"not be appropriate here\n");
format_free_parsed_args(argv);
return -EINVAL;
}
other_set = argv[0];
other_attr = argv[1];
attr = argv[2];
/* Set up to search for matches. */
local_pb = slapi_pblock_new();
if (local_pb == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"referred: out of memory\n");
format_free_parsed_args(argv);
return -ENOMEM;
}
cbdata.state = state;
cbdata.attr = attr;
cbdata.choices = NULL;
/* Retrieve the set-specific paramaters to determine which entries to
* examine. */
set_filter = NULL;
set_bases = NULL;
backend_get_set_config(state, group, other_set,
&set_bases, &set_filter);
if (set_bases == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"no search bases defined for \"%s\"/\"%s\"?\n",
group, other_set);
backend_free_set_config(set_bases, set_filter);
slapi_pblock_destroy(local_pb);
format_free_parsed_args(argv);
return 0;
}
/* Note that the attribute in this map refers to this entry. */
if (inref_attrs != NULL) {
format_add_inref_attrs(inref_attrs, group,
other_set, other_attr);
}
/* Escape the current entry's DN in case it's necessary, and build a
* search filter. */
tndn = format_escape_for_filter(slapi_entry_get_ndn(e));
if (tndn == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"referred: out of memory\n");
backend_free_set_config(set_bases, set_filter);
slapi_pblock_destroy(local_pb);
format_free_parsed_args(argv);
return -ENOMEM;
}
use_filter = set_filter ? set_filter : "(objectClass=*)";
filter = malloc(strlen(use_filter) + strlen(other_attr) +
strlen(tndn) + 7);
if (filter == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"referred: out of memory\n");
backend_free_set_config(set_bases, set_filter);
slapi_pblock_destroy(local_pb);
format_free_parsed_args(argv);
return -ENOMEM;
}
sprintf(filter, "(&(%s=%s)%s)", other_attr, tndn, use_filter);
free(tndn);
/* Search through the entries used for the set. */
attrs[0] = attr;
attrs[1] = NULL;
for (i = 0; (set_bases != NULL) && (set_bases[i] != NULL); i++) {
/* Set up the search. */
slapi_search_internal_set_pb(local_pb,
set_bases[i], LDAP_SCOPE_SUBTREE,
filter, attrs, FALSE,
NULL, NULL,
state->plugin_identity, 0);
/* Let the callback do the work of saving a value. */
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"searching under \"%s\" for \"%s\"\n",
set_bases[i], filter);
slapi_search_internal_callback_pb(local_pb, &cbdata,
NULL,
format_referred_entry_cb,
NULL);
}
free(filter);
/* Return any values we found. */
if (cbdata.choices != NULL) {
format_add_choice(outbuf_choices, outbuf, cbdata.choices);
format_free_bv_list(cbdata.choices);
}
backend_free_set_config(set_bases, set_filter);
slapi_pblock_destroy(local_pb);
format_free_parsed_args(argv);
return 0;
}
/* Add the name of this entry to the DN list in the cbdata. */
struct format_referred_r_entry_cbdata {
struct plugin_state *state;
char *attribute;
struct berval ***choices;
Slapi_DN ***sdn_list;
};
static int
format_referred_r_entry_cb(Slapi_Entry *e, void *cbdata_ptr)
{
struct format_referred_r_entry_cbdata *cbdata = cbdata_ptr;
const struct berval *bval;
Slapi_DN *sdn;
Slapi_ValueSet *values;
Slapi_Value *value;
int i, disposition, buffer_flags;
char *actual_attr;
/* Note that we visited this entry. */
slapi_log_error(SLAPI_LOG_PLUGIN, cbdata->state->plugin_desc->spd_id,
"search matched entry \"%s\"\n", slapi_entry_get_dn(e));
format_add_sdn_list(cbdata->sdn_list, slapi_entry_get_dn(e));
sdn = slapi_entry_get_sdn(e);
/* If we're also being asked to pull values out of the entry... */
if ((cbdata->attribute != NULL) && (cbdata->choices != NULL)) {
/* Pull up the value set. */
if (slapi_vattr_values_get(e, cbdata->attribute, &values,
&disposition,
&actual_attr,
0, &buffer_flags) != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN,
cbdata->state->plugin_desc->spd_id,
"referred_r: entry \"%s\" has no "
"values for \"%s\"\n",
slapi_sdn_get_dn(sdn),
cbdata->attribute);
} else {
/* Walk the value set. */
for (i = slapi_valueset_first_value(values, &value);
i != -1;
i = slapi_valueset_next_value(values, i, &value)){
/* Get the value. */
bval = slapi_value_get_berval(value);
/* If the value is empty, skip it. */
if (bval->bv_len == 0) {
continue;
}
format_add_bv_list(cbdata->choices, bval);
slapi_log_error(SLAPI_LOG_PLUGIN,
cbdata->state->plugin_desc->spd_id,
"referred_r: found value "
"\"%.*s\" in \"%s\"\n",
bval->bv_len,
bval->bv_val,
slapi_sdn_get_dn(sdn));
}
slapi_vattr_values_free(&values, &actual_attr,
buffer_flags);
}
}
return 0;
}
/* For the first N-1 arguments, treat them as pairs, looking entries in the
* map named by the first part of the pair which refer to this entry using the
* attribute named by the second part in the pair, following links until we
* get to the last argument, at which point we return the value of the
* attribute named by the final argument. */
static int
format_referred_r(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
int i, j, k, ret, argc, attrs_list_length;
Slapi_PBlock *local_pb;
Slapi_DN **these_bases, **next_bases, **these_entries, **next_entries;
struct berval **choices;
struct format_referred_r_entry_cbdata entry_cbdata;
struct format_ref_attr_list *list;
char **argv, *attrs[2], *filter, *tndn, *attr;
char *set_filter, **set_bases;
const char **attr_links, *ndn;
ret = format_parse_args(state, args, &argc, &argv);
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"referred_r: error parsing arguments\n");
return -EINVAL;
}
if (argc < 3) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"referred_r: requires at least 3 arguments\n");
format_free_parsed_args(argv);
return -EINVAL;
}
if ((argc % 2) != 1) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"referred_r: requires an odd number of "
"arguments\n");
format_free_parsed_args(argv);
return -EINVAL;
}
if (outbuf_choices == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"referred_r: returns a list, but a list would "
"not be appropriate here\n");
format_free_parsed_args(argv);
return -EINVAL;
}
attr = argv[argc - 1];
/* Build the list of attributes which we can use to select the list of
* references. */
attrs_list_length = (argc + 1) / 2;
attr_links = malloc((attrs_list_length + 1) * sizeof(char *));
if (attr_links == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"referred_r: out of memory\n");
format_free_parsed_args(argv);
return -ENOMEM;
}
for (i = 0; i < attrs_list_length; i++) {
if (i < (attrs_list_length - 1)) {
attr_links[i] = argv[i * 2 + 1];
} else {
attr_links[i] = argv[i * 2];
}
}
attr_links[i] = NULL;
/* Note this list of attributes. */
format_add_ref_attr_list(inref_attr_list, group, set, attr_links);
list = format_find_ref_attr_list(*inref_attr_list, group, set,
attr_links);
free(attr_links);
/* Set up to search for referrers. */
local_pb = slapi_pblock_new();
if (local_pb == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"referred_r: out of memory\n");
format_free_parsed_args(argv);
return -ENOMEM;
}
/* Get the searching parameters for the set which contains the entry,
* and all of the referred-to sets, and save them for use at the last
* link in the chain. */
backend_get_set_config(state, group, set,
&set_bases, &set_filter);
for (i = 0; (set_bases != NULL) && (set_bases[i] != NULL); i++) {
format_add_sdn_list(&(list->links[0].base_sdn_list),
set_bases[i]);
}
backend_free_set_config(set_bases, set_filter);
for (i = 0; i < list->n_links - 1; i++) {
backend_get_set_config(state, group, argv[i * 2],
&set_bases, &set_filter);
for (j = 0;
(set_bases != NULL) && (set_bases[j] != NULL);
j++) {
format_add_sdn_list(&(list->links[i + 1].base_sdn_list),
set_bases[j]);
}
backend_free_set_config(set_bases, set_filter);
}
/* Walk the chain, searching for entries which refer to entries at
* this point in the chain. */
these_entries = NULL;
format_add_sdn_list(&these_entries, slapi_entry_get_dn(e));
choices = NULL;
next_entries = NULL;
attrs[0] = attr;
attrs[1] = NULL;
for (i = 0; i < list->n_links - 1; i++) {
these_bases = list->links[i].base_sdn_list;
if (i < list->n_links - 1) {
next_bases = list->links[i + 1].base_sdn_list;
} else {
next_bases = NULL;
}
/* Perform the search for entries which refer to each entry we
* see here. */
for (j = 0;
(these_entries != NULL) && (these_entries[j] != NULL);
j++) {
ndn = slapi_sdn_get_ndn(these_entries[j]);
tndn = format_escape_for_filter(ndn);
if (tndn == NULL) {
continue;
}
/* Walk the set of search bases for this link. */
filter = malloc(strlen(list->links[i].attribute) +
strlen(tndn) + 4);
if (filter == NULL) {
free(tndn);
continue;
}
sprintf(filter, "(%s=%s)",
list->links[i].attribute, tndn);
for (k = 0;
(these_bases != NULL) && (these_bases[k] != NULL);
k++) {
ndn = slapi_sdn_get_dn(these_bases[k]);
/* Search for referrers under this tree. */
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"referred_r: searching under %s"
" for \"%s\" (link=1.%d)\n",
ndn, filter, i);
slapi_search_internal_set_pb(local_pb,
ndn,
LDAP_SCOPE_SUBTREE,
filter, attrs,
FALSE,
NULL, NULL,
state->plugin_identity,
0);
entry_cbdata.state = state;
entry_cbdata.attribute = attr;
entry_cbdata.choices = &choices;
entry_cbdata.sdn_list = &these_entries;
slapi_search_internal_callback_pb(local_pb,
&entry_cbdata,
NULL,
format_referred_r_entry_cb,
NULL);
}
free(filter);
/* Walk the set of search bases for the next link. */
filter = malloc(strlen(list->links[i].attribute) +
strlen(tndn) + 4);
if (filter == NULL) {
free(tndn);
continue;
}
sprintf(filter, "(%s=%s)",
list->links[i].attribute, tndn);
for (k = 0;
(next_bases != NULL) && (next_bases[k] != NULL);
k++) {
ndn = slapi_sdn_get_dn(next_bases[k]);
/* Search for referrers under that tree. */
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"referred_r: searching under %s"
" for \"%s\" (link=2.%d)\n",
ndn, filter, i);
slapi_search_internal_set_pb(local_pb,
ndn,
LDAP_SCOPE_SUBTREE,
filter, attrs,
FALSE,
NULL, NULL,
state->plugin_identity,
0);
entry_cbdata.state = state;
entry_cbdata.attribute = attr;
entry_cbdata.choices = &choices;
entry_cbdata.sdn_list = &next_entries;
slapi_search_internal_callback_pb(local_pb,
&entry_cbdata,
NULL,
format_referred_r_entry_cb,
NULL);
}
free(filter);
free(tndn);
}
/* Set up for the next iteration. */
format_free_sdn_list(these_entries);
these_entries = next_entries;
next_entries = NULL;
}
format_free_sdn_list(these_entries);
slapi_pblock_destroy(local_pb);
format_free_parsed_args(argv);
/* Return any values we found. */
if (choices != NULL) {
format_add_choice(outbuf_choices, outbuf, choices);
format_free_bv_list(choices);
return 0;
} else {
return -ENOENT;
}
}
/* Evaluate each argument's list of results, after the first, in turn, and
* merge them, using the first argument as a separator. */
static int
format_merge(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
int ret, i, j, argc, slen, count;
unsigned int *lengths;
char **argv, **values;
const char *sep;
struct format_choice *these_choices;
ret = format_parse_args(state, args, &argc, &argv);
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"merge: error parsing arguments\n");
return -EINVAL;
}
if (argc < 1) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"merge: requires at least one argument\n");
format_free_parsed_args(argv);
return -EINVAL;
}
sep = argv[0];
slen = strlen(sep);
for (i = 1, ret = 0, count = 0; i < argc; i++) {
/* Expand this argument. */
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"merge: expanding ->%s<-\n", argv[i]);
these_choices = NULL;
values = format_get_data_set(state, e, group, set,
argv[i], disallowed,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list,
&lengths);
if (values == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"merge: no values for ->%s<-\n",
argv[i]);
continue;
}
for (j = 0; values[j] != NULL; j++) {
/* Check if there's space for this value. */
if (ret + lengths[j] + (count ? slen : 0) >
(unsigned int) outbuf_len) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"merge: out of space\n");
format_free_data_set(values, lengths);
format_free_parsed_args(argv);
return -ENOBUFS;
}
/* Log this value. */
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"merge: got %d-byte value for ->%s<\n",
lengths[j], argv[i]);
/* If this isn't the first result, fill in the
* separator. Then fill in the value. */
if (count > 0) {
memcpy(outbuf + ret, sep, slen);
ret += slen;
}
memcpy(outbuf + ret, values[j], lengths[j]);
ret += lengths[j];
count++;
}
format_free_data_set(values, lengths);
}
format_free_parsed_args(argv);
return ret;
}
/* Look up the entry's values for the attribute named by the first argument,
* and use the callback to check if they match the second argument. If we find
* exactly one match, store it in the output buffer, otherwise store the text
* of the default_arg'th argument if given, or return an error if no
* default_arg'th argument was given. */
static int
format_match_generic(struct plugin_state *state,
Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, int min_args, int default_arg,
const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs,
struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list,
const char *fnname,
char * (*match_fn)(const char *pattern, const char *value,
char **argv))
{
char *cvalue, **argv, **matches, **values, *plugin_id, *default_value;
struct berval *defaults[2], default_bv;
int i, count, argc, ret, len;
unsigned int *lengths, default_length;
plugin_id = state->plugin_desc->spd_id;
ret = format_parse_args(state, args, &argc, &argv);
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, plugin_id,
"%s: error parsing arguments\n", fnname);
return -EINVAL;
}
if (argc < min_args) {
slapi_log_error(SLAPI_LOG_PLUGIN, plugin_id,
"%s: requires at least %d arguments\n",
fnname, min_args);
format_free_parsed_args(argv);
return -EINVAL;
}
/* Evaluate the expression as a list, then walk it. */
matches = NULL;
count = 0;
lengths = NULL;
values = format_get_data_set(state, e, group, set,
argv[0], disallowed,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list,
&lengths);
if (values != NULL) {
for (i = 0; values[i] != NULL; i++) {
continue;
}
matches = malloc(sizeof(char *) * (i + 1));
if (matches != NULL) {
for (i = 0; values[i] != NULL; i++) {
cvalue = xstrndup(values[i], lengths[i]);
matches[count] = match_fn(argv[1], cvalue,
argv + 2);
free(cvalue);
if (matches[count] != NULL) {
count++;
}
}
matches[count] = NULL;
}
format_free_data_set(values, lengths);
}
/* Make sure matched is either the single match, the default, or NULL
* if we had no default. */
switch (count) {
case 0:
/* No matches. Try to generate the default value. */
default_value = NULL;
if ((default_arg >= 0) && (argv[default_arg] != NULL)) {
default_value = format_get_data(state, e, group, set,
argv[default_arg],
disallowed,
ref_attrs, inref_attrs,
ref_attr_list,
inref_attr_list,
&default_length);
}
if (default_value != NULL) {
/* Try to store the default value. */
slapi_log_error(SLAPI_LOG_PLUGIN, plugin_id,
"%s: no matching values for "
"\"%s\", using default value \"%s\"\n",
fnname, argv[1], default_value);
if (outbuf_choices != NULL) {
/* Return the default value as a list. */
default_bv.bv_len = default_length;
default_bv.bv_val = default_value;
defaults[0] = &default_bv;
defaults[1] = NULL;
format_add_choice(outbuf_choices, outbuf,
defaults);
len = 0;
} else {
/* Store the default directly in the buffer. */
len = default_length;
if (len > outbuf_len) {
slapi_log_error(SLAPI_LOG_PLUGIN,
plugin_id,
"%s: out of space\n",
fnname);
format_free_data(default_value);
format_free_parsed_args(argv);
free(matches);
return -ENOBUFS;
}
memcpy(outbuf, default_value, default_length);
}
format_free_data(default_value);
} else {
slapi_log_error(SLAPI_LOG_PLUGIN, plugin_id,
"%s: no matching values for "
"\"%s\", and no default value "
"generated by \"%s\"\n",
fnname, argv[1]);
format_free_parsed_args(argv);
free(matches);
return -ENOENT;
}
break;
case 1:
/* Single result. */
if (outbuf_choices != NULL) {
/* Return the one match as a list. */
format_add_choice_str(outbuf_choices, outbuf, matches);
len = 0;
} else {
/* Store the one match directly in the buffer. */
len = strlen(matches[0]);
if (len > outbuf_len) {
slapi_log_error(SLAPI_LOG_PLUGIN, plugin_id,
"%s: out of space\n", fnname);
free(matches[0]);
free(matches);
format_free_parsed_args(argv);
return -ENOBUFS;
}
memcpy(outbuf, matches[0], len);
}
break;
default:
if (outbuf_choices != NULL) {
/* Record the set of matching values as choices at this
* location. */
format_add_choice_str(outbuf_choices, outbuf, matches);
len = 0;
} else {
/* Too many matches to store. */
default_value = NULL;
if ((default_arg >= 0) && (argv[default_arg] != NULL)) {
default_value = format_get_data(state, e,
group, set,
argv[default_arg],
disallowed,
ref_attrs,
inref_attrs,
ref_attr_list,
inref_attr_list,
&default_length);
}
if (default_value != NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN, plugin_id,
"%s: too many matching values "
"for \"%s\", using default "
"value \"%s\"\n",
fnname, argv[1], default_value);
len = default_length;
if (len > outbuf_len) {
slapi_log_error(SLAPI_LOG_PLUGIN,
plugin_id,
"%s: out of space\n",
fnname);
format_free_data(default_value);
for (i = 0; i < count; i++) {
free(matches[i]);
}
free(matches);
format_free_parsed_args(argv);
return -ENOBUFS;
}
memcpy(outbuf, default_value, default_length);
format_free_data(default_value);
} else {
slapi_log_error(SLAPI_LOG_PLUGIN, plugin_id,
"%s: too many matching values "
"for \"%s\", and no default "
"value provided by \"%s\"\n",
fnname, argv[1]);
for (i = 0; i < count; i++) {
free(matches[i]);
}
free(matches);
format_free_parsed_args(argv);
return -ENOENT;
}
}
break;
}
if (matches != NULL) {
for (i = 0; i < count; i++) {
free(matches[i]);
}
free(matches);
}
format_free_parsed_args(argv);
return len;
}
/* Check for glob-style matched values. */
static char *
format_match_cb(const char *pattern, const char *value, char **argv)
{
return (fnmatch(pattern, value, 0) == 0) ? strdup(value) : NULL;
}
static int
format_match(struct plugin_state *state,
Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
return format_match_generic(state, pb, e, group, set, args, 2, 2,
disallowed,
outbuf, outbuf_len, outbuf_choices,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list,
"format_match", format_match_cb);
}
/* Check for a regex match. */
static char *
format_regmatch_cb(const char *pattern, const char *value, char **argv)
{
regex_t reg;
regmatch_t matches;
bool_t matched;
memset(®, 0, sizeof(reg));
if (regcomp(®, pattern, REG_EXTENDED | REG_NOSUB) != 0) {
return NULL;
}
matched = (regexec(®, value, 1, &matches, 0) == 0);
regfree(®);
return matched ? strdup(value) : NULL;
}
static int
format_regmatch(struct plugin_state *state,
Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
return format_match_generic(state, pb, e, group, set, args, 2, 2,
disallowed,
outbuf, outbuf_len, outbuf_choices,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list,
"format_regmatch", format_regmatch_cb);
}
/* Check for a regex match and build a custom result from the matching value. */
static char *
format_regsub_cb(const char *pattern, const char *value, char **argv)
{
regex_t reg;
regmatch_t matches[10];
bool_t matched;
int i, j, m, len;
char *template, *ret;
memset(®, 0, sizeof(reg));
if (regcomp(®, pattern, REG_EXTENDED) != 0) {
return NULL;
}
memset(&matches, 0, sizeof(matches));
i = regexec(®, value,
sizeof(matches) / sizeof(matches[0]), &matches[0],
0);
matched = (i == 0);
regfree(®);
if (!matched) {
return NULL;
}
template = argv[0];
for (i = 0, len = 0; (template != NULL) && (template[i] != '\0'); i++) {
switch (template[i]) {
case '%':
i++;
switch (template[i]) {
case '%':
len++;
continue;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
m = template[i] - '0';
if (matches[m].rm_so != -1) {
len += (matches[m].rm_eo -
matches[m].rm_so);
}
continue;
break;
default:
len++;
break;
}
break;
default:
len++;
break;
}
}
ret = malloc(len + 1);
if (ret == NULL) {
return NULL;
}
for (i = 0, j = 0;
(template != NULL) && (template[i] != '\0') && (j < len);
i++) {
switch (template[i]) {
case '%':
i++;
switch (template[i]) {
case '%':
ret[j++] = template[i];
continue;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
m = template[i] - '0';
if (matches[m].rm_so != -1) {
memcpy(ret + j,
value + matches[m].rm_so,
matches[m].rm_eo -
matches[m].rm_so);
j += (matches[m].rm_eo -
matches[m].rm_so);
}
continue;
break;
default:
ret[j++] = template[i];
break;
}
break;
default:
ret[j++] = template[i];
break;
}
}
ret[j] = '\0';
return ret;
}
static int
format_regsub(struct plugin_state *state,
Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
return format_match_generic(state, pb, e, group, set, args, 3, 3,
disallowed,
outbuf, outbuf_len, outbuf_choices,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list,
"format_regsub", format_regsub_cb);
}
/* If the attribute given by the first argument is equal to the value given in
* the second, return the third, else return the fourth. We used to allow an
* expression instead of an attribute name, but then it was never clear how
* exactly we needed to do the comparison check (specifically,
* case-sensitivity). */
static int
format_ifeq(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
int ret, argc, i;
unsigned int *lengths;
char **argv, **values;
bool_t matched;
struct berval bv, **choices;
Slapi_Value *value;
ret = format_parse_args(state, args, &argc, &argv);
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"ifeq: error parsing arguments\n");
return -EINVAL;
}
if (argc < 1) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"ifeq: error parsing arguments\n");
format_free_parsed_args(argv);
return -EINVAL;
}
if (argc != 4) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"ifeq: expected four arguments (got %d)\n",
argc);
format_free_parsed_args(argv);
return -EINVAL;
}
/* Evaluate the value expression to get a list of candidate values. */
values = format_get_data_set(state, e, group, set,
argv[1], disallowed,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list,
&lengths);
if (values == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"ifeq: error evaluating \"%s\"\n", argv[1]);
format_free_parsed_args(argv);
return -EINVAL;
}
/* Check if any of the value expression's values match the entry's
* value for the named attribute. */
matched = FALSE;
value = slapi_value_new();
for (i = 0; values[i] != NULL; i++) {
bv.bv_val = values[i];
bv.bv_len = lengths[i];
slapi_value_set_berval(value, &bv);
if ((slapi_vattr_value_compare(e, argv[0], value,
&ret, 0) == 0) &&
(ret == 1)) {
matched = TRUE;
break;
}
}
slapi_value_free(&value);
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"ifeq: \"%s\" %s \"%s\"\n",
argv[0], matched ? "matches" : "doesn't match",
argv[1]);
format_free_data_set(values, lengths);
/* Evaluate the argument which corresponds to the output expression and
* return its values. */
ret = format_expand(state, pb, e, group, set,
argv[matched ? 2 : 3], disallowed,
outbuf, outbuf_len,
outbuf_choices,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list);
format_free_parsed_args(argv);
return ret;
}
/* Evaluate all of the arguments, and concatentate all of the lists of results
* to produce one long list. */
static int
format_collect(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
int ret, argc, i, j;
unsigned int *lengths;
char **argv, **values;
bool_t matched;
struct berval bv, **choices;
Slapi_Value *value;
ret = format_parse_args(state, args, &argc, &argv);
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"collect: error parsing arguments\n");
return -EINVAL;
}
if (argc < 1) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"collect: error parsing arguments\n");
format_free_parsed_args(argv);
return -EINVAL;
}
/* Walk the list of the arguments. */
choices = NULL;
for (i = 0; i < argc; i++) {
/* Evaluate this argument. */
values = format_get_data_set(state, e, group, set,
argv[i], disallowed,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list,
&lengths);
if (values != NULL) {
/* Walk the list of values. */
for (j = 0; values[j] != NULL; j++) {
/* Add it to the list. */
bv.bv_val = values[j];
bv.bv_len = lengths[j];
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"collect: \"%.*s\"\n",
bv.bv_len, bv.bv_val);
format_add_bv_list(&choices, &bv);
}
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"collect: expanded \"%s\" to produce "
"%d values for \"%s\"\n", argv[i], j,
slapi_entry_get_dn(e));
format_free_data_set(values, lengths);
} else {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"collect: expanding \"%s\" produced "
"no values for \"%s\"\n", argv[i],
slapi_entry_get_dn(e));
}
}
if (choices != NULL) {
for (i = 0; choices[i] != NULL; i++) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"collect: returning \"%.*s\" for "
"\"%s\"\n",
choices[i]->bv_len,
choices[i]->bv_val,
slapi_entry_get_dn(e));
continue;
}
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"collect: returning %d values for \"%s\"\n", i,
slapi_entry_get_dn(e));
format_add_choice(outbuf_choices, outbuf, choices);
format_free_bv_list(choices);
ret = 0;
} else {
ret = -ENOENT;
}
format_free_parsed_args(argv);
return ret;
}
/* Evaluate all of the arguments, and concatentate sets of entries from each
* list, separating them with an optional separator, padding the lists with a
* specified value until all of the elements of all lists have been used. */
static int
format_link(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
int ret, argc, i, *j, l, n_lists, n_done;
unsigned int **lengths, length, max_length;
char **argv, ***values, *buffer, *p;
struct berval bv, **choices;
ret = format_parse_args(state, args, &argc, &argv);
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"link: error parsing arguments\n");
return -EINVAL;
}
if (argc < 1) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"link: error parsing arguments\n");
format_free_parsed_args(argv);
return -EINVAL;
}
if (((argc + 1) % 3) != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"link: wrong number of arguments\n");
format_free_parsed_args(argv);
return -EINVAL;
}
/* Allocate space to store the information. */
values = malloc(sizeof(char **) * ((argc + 1) / 3));
lengths = malloc(sizeof(int *) * ((argc + 1) / 3));
j = malloc(sizeof(int) * ((argc + 1) / 3));
if ((values == NULL) || (lengths == NULL)) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"link: out of memory\n");
format_free_parsed_args(argv);
free(values);
free(lengths);
free(j);
return -ENOMEM;
}
/* Walk the list of the arguments, building a list of lists. */
choices = NULL;
n_lists = 0;
for (i = 0; i < argc; i += 3) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"link: evaluating \"%s\"\n", argv[i]);
values[i / 3] = format_get_data_set(state, e, group, set,
argv[i], disallowed,
ref_attrs, inref_attrs,
ref_attr_list,
inref_attr_list,
&lengths[i / 3]);
if (values[i / 3] != NULL) {
n_lists++;
}
}
/* If we got _no_ answers for anything, that's a NULL result. */
if (n_lists == 0) {
format_free_parsed_args(argv);
free(values);
free(lengths);
free(j);
return -ENOENT;
}
/* Walk the lists, building the output data items. */
n_lists = (argc + 1) / 3;
for (i = 0; i < n_lists; i++) {
j[i] = 0;
}
n_done = 0;
length = 0;
max_length = 0;
buffer = NULL;
do {
/* Calculate how much space we need. */
length = 0;
for (i = 0; i < n_lists; i++) {
if ((values[i] != NULL) && (values[i][j[i]] != NULL)) {
length += lengths[i][j[i]];
} else {
length += strlen(argv[i * 3 + 1]);
}
if (i < (n_lists - 1)) {
length += strlen(argv[i * 3 + 2]);
}
}
/* Make sure the buffer is large enough. */
if (length > max_length) {
free(buffer);
buffer = malloc(length);
if (buffer == NULL) {
format_free_bv_list(choices);
format_free_parsed_args(argv);
for (i = 0; i < n_lists; i += 3) {
format_free_data_set(values[i],
lengths[i]);
}
free(values);
free(lengths);
free(j);
return -ENOMEM;
}
max_length = length;
}
/* Build the output value. */
p = buffer;
for (i = 0; i < n_lists; i++) {
if ((values[i] != NULL) && (values[i][j[i]] != NULL)) {
l = lengths[i][j[i]];
memcpy(p, values[i][j[i]], l);
p += l;
(j[i])++;
} else {
l = strlen(argv[i * 3 + 1]);
memcpy(p, argv[i * 3 + 1], l);
p += l;
}
if (i < (n_lists - 1)) {
l = strlen(argv[i * 3 + 2]);
memcpy(p, argv[i * 3 + 2], l);
p += l;
}
}
/* Add it to the result list. */
bv.bv_val = buffer;
bv.bv_len = length;
if ((p - buffer) != length) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"link: internal error\n");
break;
} else {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"link: \"%.*s\"\n",
bv.bv_len, bv.bv_val);
format_add_bv_list(&choices, &bv);
}
/* Figure out if we're done yet. */
for (i = 0, n_done = 0; i < n_lists; i++) {
if ((values[i] == NULL) || (values[i][j[i]] == NULL)) {
n_done++;
}
}
} while (n_done != n_lists);
if (choices != NULL) {
format_add_choice(outbuf_choices, outbuf, choices);
format_free_bv_list(choices);
ret = 0;
} else {
ret = -ENOENT;
}
format_free_parsed_args(argv);
for (i = 0; i < argc; i += 3) {
format_free_data_set(values[i / 3], lengths[i / 3]);
}
free(buffer);
free(values);
free(lengths);
free(j);
return ret;
}
/* Choose a formatting function by name. */
static void *
format_lookup_fn(const char *fnname)
{
unsigned int i;
struct {
const char *name;
int (*fct_ptr)(struct plugin_state *state,
Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs,
struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list);
} fns[] = {
{"first", format_first},
{"deref", format_deref},
{"deref_r", format_deref_r},
{"referred", format_referred},
{"referred_r", format_referred_r},
{"merge", format_merge},
{"match", format_match},
{"regmatch", format_regmatch},
{"regsub", format_regsub},
{"ifeq", format_ifeq},
{"collect", format_collect},
{"link", format_link},
};
for (i = 0; i < sizeof(fns) / sizeof(fns[0]); i++) {
if ((fns[i].name != NULL) &&
(strcmp(fns[i].name, fnname) == 0)) {
return fns[i].fct_ptr;
}
}
return NULL;
}
/* Check for the presence of any character in the disallowed list in the
* passed-in value. */
static char *
format_check_disallowed(const struct berval *bv, const char *disallowed)
{
int i;
char *p;
if (disallowed != NULL) {
for (i = 0; disallowed[i] != '\0'; i++) {
p = memchr(bv->bv_val, disallowed[i], bv->bv_len);
if (p != NULL) {
return p;
}
}
}
return NULL;
}
/* Retrieve a single value for an attribute. If there are no values, or more
* than one, return NULL. If there's more than one, store the values in the
* array argument. */
static struct berval
format_single(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
const char *attr, const char *disallowed,
struct berval ***values)
{
Slapi_ValueSet *value_set;
Slapi_Value *value;
char *ret, *actual_attr;
const char *d;
const struct berval *val;
struct berval bv;
int count, disposition, buffer_flags, i;
memset(&bv, 0, sizeof(bv));
if (slapi_vattr_values_get(e, (char *) attr, &value_set,
&disposition, &actual_attr,
0, &buffer_flags) != 0) {
return bv;
}
count = slapi_valueset_count(value_set);
ret = NULL;
if (count == 1) {
if (slapi_valueset_first_value(value_set, &value) != -1) {
val = slapi_value_get_berval(value);
d = format_check_disallowed(val, disallowed);
if (d != NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"value for \"%s\" "
"contains disallowed "
"character \"%c\", "
"ignoring\n",
attr, *d);
} else {
bv.bv_val = xmemdup(val->bv_val, val->bv_len);
if (bv.bv_val != NULL) {
bv.bv_len = val->bv_len;
} else {
bv.bv_len = 0;
}
}
}
} else {
if ((count == 0) || (values == NULL)) {
/* Either no results, or too many results with no place
* to put them. */
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"%d values for \"%s\"\n", count, attr);
} else {
/* Return the list of values. */
if (values != NULL) {
for (i = slapi_valueset_first_value(value_set,
&value);
i != -1;
i = slapi_valueset_next_value(value_set, i,
&value)) {
val = slapi_value_get_berval(value);
if (val->bv_len == 0) {
continue;
}
d = format_check_disallowed(val,
disallowed);
if (d == NULL) {
format_add_bv_list(values, val);
}
}
} else {
bv.bv_len = -EINVAL;
}
}
}
slapi_vattr_values_free(&value_set, &actual_attr, buffer_flags);
return bv;
}
/* Find the matching closing marker -- assumes that the first character in
* pattern is the opener. */
static const char *
format_find_closer(const char *pair, const char *pattern)
{
int i, dq, level = 0;
for (i = 0, dq = 0; pattern[i] != '\0'; i++) {
if (pattern[i] == '\\') {
i++;
continue;
}
if (pattern[i] == '"') {
dq = !dq;
continue;
}
if (!dq) {
if (pattern[i] == pair[0]) {
level++;
} else {
if (pattern[i] == pair[1]) {
level--;
}
}
if (level == 0) {
return &pattern[i];
}
}
}
return NULL;
}
/* Trim off prefixes or suffixes which match the given patterns, free the
* original, and return the result. */
static struct berval
format_trim_value(struct plugin_state *state, struct berval input,
const char *shortstart, const char *longstart,
const char *shortend, const char *longend,
const char *replace, const char *replaceall,
const char *replaceval)
{
struct berval ret;
char *buf;
unsigned int i, len;
buf = xmemdup(input.bv_val, input.bv_len);
len = input.bv_len;
if (buf != NULL) {
if (shortstart) {
/* The shortest initial subsection which matches gets
* trimmed off. */
for (i = 0; i <= input.bv_len; i++) {
memcpy(buf, input.bv_val, i);
buf[i] = '\0';
if (fnmatch(shortstart, buf, 0) == 0) {
memcpy(buf, input.bv_val + i,
input.bv_len - i);
buf[input.bv_len - i] = '\0';
ret.bv_val = buf;
ret.bv_len = input.bv_len - i;
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"trim-ss: ->%.*s<- => "
"->%.*s<-\n",
input.bv_len,
input.bv_val,
ret.bv_len,
ret.bv_val);
free(input.bv_val);
return ret;
}
}
}
if (shortend) {
/* The shortest ending substring which matches gets
* snipped. */
for (i = 0; i <= input.bv_len; i++) {
memcpy(buf,
input.bv_val + input.bv_len - i,
i);
buf[i] = '\0';
if (fnmatch(shortend, buf, 0) == 0) {
memcpy(buf, input.bv_val,
input.bv_len - i);
buf[input.bv_len - i] = '\0';
ret.bv_val = buf;
ret.bv_len = input.bv_len - i;
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"trim-se: ->%.*s<- => "
"->%.*s<-\n",
input.bv_len,
input.bv_val,
ret.bv_len,
ret.bv_val);
free(input.bv_val);
return ret;
}
}
}
if (longstart) {
/* The longest initial substring which matches gets
* skipped. */
for (i = 0; i <= len; i++) {
memcpy(buf, input.bv_val, (input.bv_len - i));
buf[input.bv_len - i] = '\0';
if (fnmatch(longstart, buf, 0) == 0) {
memcpy(buf, input.bv_val + i,
input.bv_len - i);
buf[input.bv_len - i] = '\0';
ret.bv_val = buf;
ret.bv_len = input.bv_len - i;
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"trim-ls: ->%.*s<- => "
"->%.*s<-\n",
input.bv_len,
input.bv_val,
ret.bv_len,
ret.bv_val);
free(input.bv_val);
return ret;
}
}
}
if (longend) {
/* The longest ending substring which matches gets
* snipped. */
for (i = 0; i <= len; i++) {
memcpy(buf, input.bv_val + i, input.bv_len - i);
buf[input.bv_len - i] = '\0';
if (fnmatch(longend, buf, 0) == 0) {
memcpy(buf, input.bv_val,
input.bv_len - i);
buf[i] = '\0';
ret.bv_val = buf;
ret.bv_len = input.bv_len - i;
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"trim-le: ->%.*s<- => "
"->%.*s<-\n",
input.bv_len,
input.bv_val,
ret.bv_len,
ret.bv_val);
free(input.bv_val);
return ret;
}
}
}
if (replaceval == NULL) {
replaceval = "";
}
if (replace) {
/* FIXME */
}
if (replaceall) {
/* FIXME */
}
free(buf);
}
return input;
}
/* Expand the simple expression into the output buffer. This is limited to one
* attribute value, perhaps with a default or alternate value, perhaps with a
* prefix or suffix stripped, perhaps with internal replacements made. */
static int
format_expand_simple(struct plugin_state *state,
Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *fmt, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs,
struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
char *shortstart, *longstart, *shortend, *longend;
struct berval tmp, **values;
char *replace, *replaceall, *replaceval, *expr, *p;
const char *attribute, *default_value, *alternate_value;
size_t spn;
int i;
shortstart = NULL;
longstart = NULL;
shortend = NULL;
longend = NULL;
replace = NULL;
replaceall = NULL;
replaceval = NULL;
expr = strdup(fmt);
/* It's a simple expression, so evaluate it. Check for substitutions
* and text to be stripped if the magic character occurs before the
* default/alternate signals. */
if (strcspn(expr, "#%/!") < strcspn(expr, ":")) {
spn = strcspn(expr, "#%/!");
p = expr + spn;
if (strncmp(p, "##", 2) == 0) {
longstart = p + 2;
}
else if (strncmp(p, "%%", 2) == 0) {
longend = p + 2;
}
else if (strncmp(p, "//", 2) == 0) {
replaceall = p + 2;
}
else if (strncmp(p, "#", 1) == 0) {
shortstart = p + 1;
}
else if (strncmp(p, "%", 1) == 0) {
shortend = p + 1;
}
else if (strncmp(p, "/", 1) == 0) {
replace = p + 1;
}
expr[spn] = '\0';
if ((replace != NULL) || (replaceall != NULL)) {
replaceval = NULL;
if (replace != NULL) {
spn = strcspn(replace, "/");
replaceval = replace + spn;
}
if (replaceall != NULL) {
spn = strcspn(replaceall, "/");
replaceval = replaceall + spn;
}
if ((replaceval != NULL) &&
(*replaceval != '\0')) {
*replaceval = '\0';
replaceval++;
}
}
attribute = expr;
alternate_value = NULL;
default_value = NULL;
} else {
/* Check if it uses a default/alternate value. */
spn = strcspn(expr, ":");
if (spn == strlen(expr)) {
/* Plain old variable, no alternate or default value.
* */
attribute = expr;
alternate_value = NULL;
default_value = NULL;
} else {
/* Make a copy of the attribute name. */
expr[spn] = '\0';
attribute = expr;
alternate_value = NULL;
default_value = NULL;
/* Figure out if there's an alternate or default value
* given. */
switch (expr[spn + 1]) {
case '+':
alternate_value = expr + spn + 2;
break;
case '-':
default_value = expr + spn + 2;
break;
default:
default_value = expr + spn + 1;
break;
}
}
}
/* Retrieve the value. */
values = NULL;
tmp = format_single(state, pb, e, attribute, disallowed,
outbuf_choices ? &values : NULL);
if (tmp.bv_val == NULL) {
/* The attribute is undefined, or we're treating it as if it
* is. */
if (values == NULL) {
if (default_value != NULL) {
/* Supply the default value, expanding it if
* needed. */
i = format_expand(state, pb, e,
group, set,
default_value, NULL,
outbuf, outbuf_len,
outbuf_choices,
ref_attrs, inref_attrs,
ref_attr_list,
inref_attr_list);
free(expr);
return i;
} else {
/* No value, and no default: FAIL. */
free(expr);
return -ENOENT;
}
} else {
if (alternate_value != NULL) {
/* Supply the alternate value. */
i = format_expand(state, pb, e,
group, set,
alternate_value, NULL,
outbuf, outbuf_len,
outbuf_choices,
ref_attrs, inref_attrs,
ref_attr_list,
inref_attr_list);
free(expr);
format_free_bv_list(values);
return i;
} else {
/* Store nothing in the immediate position, but
* return a note that any of these values would
* be fine at this point in the output string.
* */
format_add_choice(outbuf_choices,
outbuf, values);
free(expr);
format_free_bv_list(values);
return 0;
}
}
} else {
if (values != NULL) {
format_free_bv_list(values);
}
/* There's a suitable single value available. */
if (alternate_value != NULL) {
/* Supply the alternate value. */
i = format_expand(state, pb, e,
group, set, alternate_value, NULL,
outbuf, outbuf_len, outbuf_choices,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list);
free(tmp.bv_val);
free(expr);
return i;
} else {
/* Munge up the looked-up value. */
tmp = format_trim_value(state, tmp,
shortstart, longstart,
shortend, longend,
replace, replaceall,
replaceval);
/* Supply the looked-up value. */
if (tmp.bv_val != NULL) {
if (tmp.bv_len <= (unsigned int) outbuf_len) {
memcpy(outbuf, tmp.bv_val, tmp.bv_len);
}
free(tmp.bv_val);
free(expr);
return tmp.bv_len;
} else {
/* No useful value: FAIL. */
free(expr);
return -ENOENT;
}
}
}
/* not reached */
}
/* Recursively expand the expression into the output buffer. If the result
* will also be an expression, treat the entire result as an attribute
* specifier and evaluate it, otherwise return it. */
static int
format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *fmt, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list)
{
int i, j, used;
const char *fmtstart, *fmtend, *match, *pair;
char *subexp, *fnname, *params, *spd_id;
const char *paramstart, *paramend;
int (*formatfn)(struct plugin_state *state,
Slapi_PBlock *pb, Slapi_Entry *e,
const char *group, const char *set,
const char *args, const char *disallowed,
char *outbuf, int outbuf_len,
struct format_choice **outbuf_choices,
char ***ref_attrs,
struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list);
spd_id = state->plugin_desc->spd_id;
/* Expand any subexpressions and call any "functions". */
i = 0;
j = 0;
while ((fmt[i] != '\0') && (j < outbuf_len)) {
switch (fmt[i]) {
case '%':
/* This might be a subexpression, a "function" call, or
* an escaped character. */
switch (fmt[i + 1]) {
case '%':
/* It's just an escaped "%". */
outbuf[j++] = '%';
i += 2;
continue;
break;
case '{':
/* Find the beginning of the simple expression.
*/
fmtstart = fmt + i;
/* Find the end of the simple expression. */
match = format_find_closer("{}", fmtstart + 1);
if (match == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion failed: "
"no closing brace\n");
return -EINVAL;
} else {
/* Track the first character after the
* simple expression. */
fmtend = match + 1;
/* Make a copy of the simple
* expression. */
subexp = xstrndupp(fmtstart + 2,
fmtend - 1);
if (subexp == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion "
"failed: out "
"of memory\n");
return -ENOMEM;
}
/* Expand the simple expression. */
used = format_expand_simple(state,
pb, e,
group,
set,
subexp,
disallowed,
outbuf + j,
outbuf_len - j,
outbuf_choices,
ref_attrs,
inref_attrs,
ref_attr_list,
inref_attr_list);
if (used < 0) {
/* Some failure, FAIL. */
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"error "
"expanding "
"expression "
"->%s<-: %s\n",
subexp,
strerror(-used));
free(subexp);
return used;
}
free(subexp);
subexp = NULL;
if (used + j >= outbuf_len) {
/* Out of space, or would be,
* so return a failure. */
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion "
"failed: result"
" would be too "
"big\n");
return -ENOBUFS;
} else {
/* It fit, so keep going. */
i = (match + 1) - fmt;
j += used;
}
}
continue;
break;
default:
/* Assume it's a "function" call. Pick out the
* name of the function. */
paramstart = strpbrk(fmt + i + 1, "{(");
if (paramstart == NULL) {
/* No start? Bad format. */
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion failed: "
"bad function "
"invocation\n");
return -EINVAL;
}
if (*paramstart == '{') {
pair = "{}";
} else {
pair = "()";
}
paramend = format_find_closer(pair, paramstart);
if (paramend == NULL) {
/* No matching end? Bad format. */
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion failed: "
"bad function "
"invocation\n");
return -EINVAL;
}
fnname = xstrndupp(fmt + i + 1, paramstart);
if (fnname == NULL) {
/* Out of memory, FAIL. */
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion failed: "
"out of memory\n");
return -ENOMEM;
}
/* Pick out the parameter list. */
params = xstrndupp(paramstart + 1, paramend);
if (params == NULL) {
/* Out of memory, FAIL. */
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion failed: "
"out of memory\n");
free(fnname);
return -ENOMEM;
}
/* Find the "function". */
formatfn = format_lookup_fn(fnname);
if (formatfn == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion "
"failed: no function "
"named \"%s\" is "
"defined\n", fnname);
free(fnname);
free(params);
return -ENOSYS;
}
/* Call the "function". */
used = (*formatfn)(state, pb, e,
group, set,
params, disallowed,
outbuf + j, outbuf_len - j,
outbuf_choices,
ref_attrs, inref_attrs,
ref_attr_list,
inref_attr_list);
free(params);
params = NULL;
if (used < 0) {
/* Error in function, FAIL. */
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion "
"failed: function "
"'%s' failed: %s\n",
fnname,
strerror(-used));
free(fnname);
return used;
}
free(fnname);
fnname = NULL;
if (used + j >= outbuf_len) {
/* We'd be out of space, fail. */
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion "
"failed: result"
" would be too "
"big\n");
return -ENOBUFS;
}
i = (paramend - fmt) + 1;
j += used;
continue;
break;
}
break;
default:
/* Default is just a literal character. */
outbuf[j++] = fmt[i++];
break;
}
}
outbuf[j] = '\0';
if (j > outbuf_len) {
return -ENOBUFS;
} else {
return j;
}
}
static char *
format_format(struct plugin_state *state, Slapi_Entry *e,
const char *group, const char *set,
const char *fmt, const char *disallowed,
struct format_choice **choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list,
unsigned int *data_length)
{
Slapi_PBlock *pb;
char *buf, *ret, *spd_id;
int i, buflen;
spd_id = state->plugin_desc->spd_id;
buflen = DEFAULT_BUFFER_SIZE;
pb = slapi_pblock_new();
do {
buf = malloc(buflen);
if (buf == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion of \"%s\" "
"for \"%s\" failing: out of memory\n",
fmt,
slapi_entry_get_ndn(e));
slapi_pblock_destroy(pb);
return NULL;
}
i = format_expand(state, pb, e, group, set,
fmt, disallowed,
buf, buflen, choices,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list);
if ((i >= 0) && (i < buflen)) {
buf[i] = '\0';
ret = xmemdup(buf, i);
*data_length = i;
format_retarget_choicesp(choices, buf, ret);
} else {
if (i == -ENOBUFS) {
if (buflen < MAX_BUFFER_SIZE) {
buflen *= 2;
#if 0
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion of \"%s\" "
"for \"%s\" failed: %s "
"(will try again)\n",
fmt,
slapi_entry_get_ndn(e),
strerror(-i));
#endif
} else {
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion of \"%s\" "
"for \"%s\" failed: %s "
"(giving up)\n",
fmt,
slapi_entry_get_ndn(e),
strerror(-i));
}
} else {
slapi_log_error(SLAPI_LOG_PLUGIN, spd_id,
"expansion of \"%s\" "
"for \"%s\" failed: %s\n",
fmt, slapi_entry_get_ndn(e),
strerror(-i));
}
format_free_choicesp(choices);
ret = NULL;
}
free(buf);
} while (i == -ENOBUFS);
slapi_pblock_destroy(pb);
return ret;
}
void
format_free_data(char *data)
{
if (data != NULL) {
free(data);
}
}
char *
format_get_data(struct plugin_state *state, Slapi_Entry *e,
const char *group, const char *set,
const char *fmt, const char *disallowed,
char ***ref_attrs,
struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list,
unsigned int *data_length)
{
unsigned int ignored;
return format_format(state, e, group, set, fmt, disallowed,
NULL, ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list,
data_length ? data_length : &ignored);
}
void
format_free_data_set(char **data, unsigned int *data_lengths)
{
int i;
if (data != NULL) {
for (i = 0; data[i] != NULL; i++) {
free(data[i]);
}
free(data);
}
free(data_lengths);
}
char **
format_get_data_set(struct plugin_state *state, Slapi_Entry *e,
const char *group, const char *set,
const char *fmt, const char *disallowed,
char ***ref_attrs,
struct format_inref_attr ***inref_attrs,
struct format_ref_attr_list ***ref_attr_list,
struct format_ref_attr_list ***inref_attr_list,
unsigned int **data_lengths)
{
struct format_choice *choices, *this_choice;
struct berval *val;
char **ret, *template;
int combinations, groupsize, i, j, k, offset, length;
unsigned int template_len;
choices = NULL;
template = format_format(state, e, group, set, fmt, disallowed,
&choices,
ref_attrs, inref_attrs,
ref_attr_list, inref_attr_list,
&template_len);
if (template == NULL) {
format_free_choices(choices);
return NULL;
}
/* Figure out how many results we're going to have. */
combinations = 1;
for (this_choice = choices;
this_choice != NULL;
this_choice = this_choice->next) {
for (i = 0; i < this_choice->n_values; i++) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"choice: %d at %d\n",
this_choice->values[i]->bv_len,
this_choice->offset - template);
}
combinations *= this_choice->n_values;
}
if (combinations == 0) {
format_free_choices(choices);
return NULL;
}
ret = malloc((combinations + 1) * sizeof(char *));
*data_lengths = malloc(sizeof(**data_lengths) * combinations);
if ((ret != NULL) && (*data_lengths != NULL)) {
/* Work out all of the results. */
for (i = 0, j = 0; i < combinations; i++) {
/* First figure out how long this result will be. */
groupsize = combinations;
length = template_len;
for (this_choice = choices;
this_choice != NULL;
this_choice = this_choice->next) {
/* Add the length of the value used here. */
groupsize /= this_choice->n_values;
val = this_choice->values[(i / groupsize) %
this_choice->n_values];
length += val->bv_len;
}
/* Allocate memory for this result. */
ret[j] = malloc(length + 1);
if (ret[j] == NULL) {
continue;
}
/* Build the result's value. */
offset = 0;
k = 0;
groupsize = combinations;
for (this_choice = choices;
this_choice != NULL;
this_choice = this_choice->next) {
/* Copy any part of the template that should be
* in the result by now. */
length = (this_choice->offset - template) -
offset;
memcpy(ret[j] + k, template + offset, length);
k += length;
offset += length;
groupsize /= this_choice->n_values;
val = this_choice->values[(i / groupsize) %
this_choice->n_values];
memcpy(ret[j] + k, val->bv_val, val->bv_len);
k += val->bv_len;
}
/* Copy any part of the template which trails the
* choices. */
length = template_len - offset;
memcpy(ret[j] + k, template + offset, length);
ret[j][k + length] = '\0';
(*data_lengths)[j] = k + length;
j++;
}
ret[j] = NULL;
} else {
free(ret);
free(*data_lengths);
ret = NULL;
*data_lengths = NULL;
}
format_free_choices(choices);
free(template);
return ret;
}
char *
format_escape_for_filter(const char *unescaped)
{
int i, j, count;
char *ret;
for (i = 0, count = 0; unescaped[i] != 0; i++) {
switch (unescaped[i]) {
case '*':
case '(':
case ')':
case '\\':
count++;
break;
default:
break;
}
}
ret = malloc(i + (2 * count) + 1);
if (ret != NULL) {
for (i = 0, j = 0; unescaped[i] != 0; i++) {
switch (unescaped[i]) {
case '*':
ret[j++] = '\\';
ret[j++] = '2';
ret[j++] = 'a';
break;
case '(':
ret[j++] = '\\';
ret[j++] = '2';
ret[j++] = '8';
break;
case ')':
ret[j++] = '\\';
ret[j++] = '2';
ret[j++] = '9';
break;
case '\\':
ret[j++] = '\\';
ret[j++] = '5';
ret[j++] = 'c';
break;
default:
ret[j++] = unescaped[i];
break;
}
}
ret[j] = '\0';
}
return ret;
}
|