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
|
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 2002-2018 The OpenLDAP Foundation.
* Portions Copyright 1997,2002-2003 IBM Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/* ACKNOWLEDGEMENTS:
* This work was initially developed by IBM Corporation for use in
* IBM products and subsequently ported to OpenLDAP Software by
* Steve Omrani. Additional significant contributors include:
* Luke Howard
*/
#include "portable.h"
#include <ac/string.h>
#include <ac/stdarg.h>
#include <ac/ctype.h>
#include <ac/unistd.h>
#include <lutil.h>
#include <slap.h>
#include <slapi.h>
#include <netdb.h>
#ifdef LDAP_SLAPI
/*
* server start time (should we use a struct timeval also in slapd?
*/
static struct timeval base_time;
ldap_pvt_thread_mutex_t slapi_hn_mutex;
ldap_pvt_thread_mutex_t slapi_time_mutex;
struct slapi_mutex {
ldap_pvt_thread_mutex_t mutex;
};
struct slapi_condvar {
ldap_pvt_thread_cond_t cond;
ldap_pvt_thread_mutex_t mutex;
};
static int checkBVString(const struct berval *bv)
{
ber_len_t i;
for ( i = 0; i < bv->bv_len; i++ ) {
if ( bv->bv_val[i] == '\0' )
return 0;
}
if ( bv->bv_val[i] != '\0' )
return 0;
return 1;
}
/*
* This function converts an array of pointers to berval objects to
* an array of berval objects.
*/
int
bvptr2obj(
struct berval **bvptr,
BerVarray *bvobj,
unsigned *num )
{
int rc = LDAP_SUCCESS;
int i;
BerVarray tmpberval;
if ( bvptr == NULL || *bvptr == NULL ) {
return LDAP_OTHER;
}
for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
; /* EMPTY */
}
if ( num )
*num = i;
tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
if ( tmpberval == NULL ) {
return LDAP_NO_MEMORY;
}
for ( i = 0; bvptr[i] != NULL; i++ ) {
tmpberval[i].bv_val = bvptr[i]->bv_val;
tmpberval[i].bv_len = bvptr[i]->bv_len;
}
tmpberval[i].bv_val = NULL;
tmpberval[i].bv_len = 0;
if ( rc == LDAP_SUCCESS ) {
*bvobj = tmpberval;
}
return rc;
}
Slapi_Entry *
slapi_str2entry(
char *s,
int flags )
{
return str2entry( s );
}
char *
slapi_entry2str(
Slapi_Entry *e,
int *len )
{
char *ret = NULL;
char *s;
ldap_pvt_thread_mutex_lock( &entry2str_mutex );
s = entry2str( e, len );
if ( s != NULL )
ret = slapi_ch_strdup( s );
ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
return ret;
}
char *
slapi_entry_get_dn( Slapi_Entry *e )
{
return e->e_name.bv_val;
}
int
slapi_x_entry_get_id( Slapi_Entry *e )
{
return e->e_id;
}
static int
slapi_int_dn_pretty( struct berval *in, struct berval *out )
{
Syntax *syntax = slap_schema.si_syn_distinguishedName;
assert( syntax != NULL );
return (syntax->ssyn_pretty)( syntax, in, out, NULL );
}
static int
slapi_int_dn_normalize( struct berval *in, struct berval *out )
{
MatchingRule *mr = slap_schema.si_mr_distinguishedNameMatch;
Syntax *syntax = slap_schema.si_syn_distinguishedName;
assert( mr != NULL );
return (mr->smr_normalize)( 0, syntax, mr, in, out, NULL );
}
void
slapi_entry_set_dn(
Slapi_Entry *e,
char *ldn )
{
struct berval dn = BER_BVNULL;
dn.bv_val = ldn;
dn.bv_len = strlen( ldn );
slapi_int_dn_pretty( &dn, &e->e_name );
slapi_int_dn_normalize( &dn, &e->e_nname );
}
Slapi_Entry *
slapi_entry_dup( Slapi_Entry *e )
{
return entry_dup( e );
}
int
slapi_entry_attr_delete(
Slapi_Entry *e,
char *type )
{
AttributeDescription *ad = NULL;
const char *text;
if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
}
if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
return 0; /* attribute is deleted */
} else {
return -1; /* something went wrong */
}
}
Slapi_Entry *
slapi_entry_alloc( void )
{
return (Slapi_Entry *)entry_alloc();
}
void
slapi_entry_free( Slapi_Entry *e )
{
if ( e != NULL )
entry_free( e );
}
int
slapi_entry_attr_merge(
Slapi_Entry *e,
char *type,
struct berval **vals )
{
AttributeDescription *ad = NULL;
const char *text;
BerVarray bv;
int rc;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return -1;
}
rc = bvptr2obj( vals, &bv, NULL );
if ( rc != LDAP_SUCCESS ) {
return -1;
}
rc = attr_merge_normalize( e, ad, bv, NULL );
ch_free( bv );
return rc;
}
int
slapi_entry_attr_find(
Slapi_Entry *e,
char *type,
Slapi_Attr **attr )
{
AttributeDescription *ad = NULL;
const char *text;
int rc;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return -1;
}
*attr = attr_find( e->e_attrs, ad );
if ( *attr == NULL ) {
return -1;
}
return 0;
}
char *
slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
{
AttributeDescription *ad = NULL;
const char *text;
int rc;
Attribute *attr;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return NULL;
}
attr = attr_find( e->e_attrs, ad );
if ( attr == NULL ) {
return NULL;
}
if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
const char *p;
p = slapi_value_get_string( &attr->a_vals[0] );
if ( p != NULL ) {
return slapi_ch_strdup( p );
}
}
return NULL;
}
int
slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
{
AttributeDescription *ad = NULL;
const char *text;
int rc;
Attribute *attr;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return 0;
}
attr = attr_find( e->e_attrs, ad );
if ( attr == NULL ) {
return 0;
}
return slapi_value_get_int( attr->a_vals );
}
long
slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
{
AttributeDescription *ad = NULL;
const char *text;
int rc;
Attribute *attr;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return 0;
}
attr = attr_find( e->e_attrs, ad );
if ( attr == NULL ) {
return 0;
}
return slapi_value_get_long( attr->a_vals );
}
unsigned int
slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
{
AttributeDescription *ad = NULL;
const char *text;
int rc;
Attribute *attr;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return 0;
}
attr = attr_find( e->e_attrs, ad );
if ( attr == NULL ) {
return 0;
}
return slapi_value_get_uint( attr->a_vals );
}
unsigned long
slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
{
AttributeDescription *ad = NULL;
const char *text;
int rc;
Attribute *attr;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return 0;
}
attr = attr_find( e->e_attrs, ad );
if ( attr == NULL ) {
return 0;
}
return slapi_value_get_ulong( attr->a_vals );
}
int
slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
{
struct berval bv;
AttributeDescription *ad = NULL;
const char *text;
int rc;
Attribute *attr;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return 0;
}
attr = attr_find( e->e_attrs, ad );
if ( attr == NULL ) {
return 0;
}
bv.bv_val = (char *)value;
bv.bv_len = strlen( value );
return ( slapi_attr_value_find( attr, &bv ) != -1 );
}
void
slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
{
AttributeDescription *ad = NULL;
const char *text;
int rc;
struct berval bv;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return;
}
attr_delete ( &e->e_attrs, ad );
if ( value != NULL ) {
bv.bv_val = (char *)value;
bv.bv_len = strlen(value);
attr_merge_normalize_one( e, ad, &bv, NULL );
}
}
void
slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
{
char buf[64];
snprintf( buf, sizeof( buf ), "%d", l );
slapi_entry_attr_set_charptr( e, type, buf );
}
void
slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
{
char buf[64];
snprintf( buf, sizeof( buf ), "%u", l );
slapi_entry_attr_set_charptr( e, type, buf );
}
void
slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
{
char buf[64];
snprintf( buf, sizeof( buf ), "%ld", l );
slapi_entry_attr_set_charptr( e, type, buf );
}
void
slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
{
char buf[64];
snprintf( buf, sizeof( buf ), "%lu", l );
slapi_entry_attr_set_charptr( e, type, buf );
}
int
slapi_is_rootdse( const char *dn )
{
return ( dn == NULL || dn[0] == '\0' );
}
int
slapi_entry_has_children( const Slapi_Entry *e )
{
Slapi_PBlock *pb;
Backend *be = select_backend( (struct berval *)&e->e_nname, 0 );
int rc, hasSubordinates = 0;
if ( be == NULL || be->be_has_subordinates == 0 ) {
return 0;
}
pb = slapi_pblock_new();
if ( pb == NULL ) {
return 0;
}
slapi_int_connection_init_pb( pb, LDAP_REQ_SEARCH );
rc = slapi_pblock_set( pb, SLAPI_TARGET_DN, slapi_entry_get_dn(
(Entry *) e ));
if ( rc == LDAP_SUCCESS ) {
pb->pb_op->o_bd = be;
rc = be->be_has_subordinates( pb->pb_op, (Entry *) e,
&hasSubordinates );
}
slapi_pblock_destroy( pb );
return ( rc == LDAP_SUCCESS && hasSubordinates == LDAP_COMPARE_TRUE );
}
/*
* Return approximate size of the entry rounded to the nearest
* 1K. Only the size of the attribute values are counted in the
* Sun implementation.
*
* http://docs.sun.com/source/816-6701-10/funcref.html#1017388
*/
size_t slapi_entry_size(Slapi_Entry *e)
{
size_t size;
Attribute *a;
int i;
for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
size += a->a_vals[i].bv_len + 1;
}
}
size += 1023;
size -= (size % 1024);
return size;
}
/*
* Add values to entry.
*
* Returns:
* LDAP_SUCCESS Values added to entry
* LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
* LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
*/
int
slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
{
Modification mod;
const char *text;
int rc;
char textbuf[SLAP_TEXT_BUFLEN];
mod.sm_op = LDAP_MOD_ADD;
mod.sm_flags = 0;
mod.sm_desc = NULL;
mod.sm_type.bv_val = (char *)type;
mod.sm_type.bv_len = strlen( type );
rc = slap_str2ad( type, &mod.sm_desc, &text );
if ( rc != LDAP_SUCCESS ) {
return rc;
}
if ( vals == NULL ) {
/* Apparently vals can be NULL
* FIXME: sm_values = NULL ? */
mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
mod.sm_values->bv_val = NULL;
mod.sm_numvals = 0;
} else {
rc = bvptr2obj( vals, &mod.sm_values, &mod.sm_numvals );
if ( rc != LDAP_SUCCESS ) {
return LDAP_CONSTRAINT_VIOLATION;
}
}
mod.sm_nvalues = NULL;
rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
slapi_ch_free( (void **)&mod.sm_values );
return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
}
int
slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
{
return slapi_entry_add_values( e, type, vals );
}
int
slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
{
AttributeDescription *ad = NULL;
const char *text;
int rc;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return -1;
}
return attr_merge_normalize( e, ad, *vs, NULL );
}
int
slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
{
Modification mod;
const char *text;
int rc;
char textbuf[SLAP_TEXT_BUFLEN];
mod.sm_op = LDAP_MOD_DELETE;
mod.sm_flags = 0;
mod.sm_desc = NULL;
mod.sm_type.bv_val = (char *)type;
mod.sm_type.bv_len = strlen( type );
if ( vals == NULL ) {
/* If vals is NULL, this is a NOOP. */
return LDAP_SUCCESS;
}
rc = slap_str2ad( type, &mod.sm_desc, &text );
if ( rc != LDAP_SUCCESS ) {
return rc;
}
if ( vals[0] == NULL ) {
/* SLAPI doco says LDApb_opERATIONS_ERROR but LDAP_OTHER is better */
return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
}
rc = bvptr2obj( vals, &mod.sm_values, &mod.sm_numvals );
if ( rc != LDAP_SUCCESS ) {
return LDAP_CONSTRAINT_VIOLATION;
}
mod.sm_nvalues = NULL;
rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
slapi_ch_free( (void **)&mod.sm_values );
return rc;
}
int
slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
{
return slapi_entry_delete_values( e, type, vals );
}
int
slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
{
return slapi_entry_attr_merge( e, (char *)type, vals );
}
int
slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
{
AttributeDescription *ad = NULL;
int rc;
const char *text;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return -1;
}
rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
if ( rc != LDAP_SUCCESS ) {
return -1;
}
return 0;
}
int
slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
{
Slapi_Value val;
val.bv_val = (char *)value;
val.bv_len = strlen( value );
return slapi_entry_add_value( e, type, &val );
}
int
slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
{
Slapi_Value *vals[2];
Slapi_Value val;
val.bv_val = (char *)value;
val.bv_len = strlen( value );
vals[0] = &val;
vals[1] = NULL;
return slapi_entry_delete_values_sv( e, type, vals );
}
int
slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
{
return slapi_entry_attr_merge( e, (char *)type, vals );
}
int
slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
{
if ( e == NULL ) {
return -1;
}
*attr = e->e_attrs;
return ( *attr != NULL ) ? 0 : -1;
}
int
slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
{
if ( e == NULL ) {
return -1;
}
if ( prevattr == NULL ) {
return -1;
}
*attr = prevattr->a_next;
return ( *attr != NULL ) ? 0 : -1;
}
int
slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
{
AttributeDescription *ad = NULL;
const char *text;
int rc;
BerVarray bv;
rc = slap_str2ad( type, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return 0;
}
attr_delete( &e->e_attrs, ad );
rc = bvptr2obj( vals, &bv, NULL );
if ( rc != LDAP_SUCCESS ) {
return -1;
}
rc = attr_merge_normalize( e, ad, bv, NULL );
slapi_ch_free( (void **)&bv );
if ( rc != LDAP_SUCCESS ) {
return -1;
}
return 0;
}
/*
* FIXME -- The caller must free the allocated memory.
* In Netscape they do not have to.
*/
int
slapi_attr_get_values(
Slapi_Attr *attr,
struct berval ***vals )
{
int i, j;
struct berval **bv;
if ( attr == NULL ) {
return 1;
}
for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
; /* EMPTY */
}
bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
for ( j = 0; j < i; j++ ) {
bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
}
bv[j] = NULL;
*vals = (struct berval **)bv;
return 0;
}
char *
slapi_dn_normalize( char *dn )
{
struct berval bdn;
struct berval pdn;
assert( dn != NULL );
bdn.bv_val = dn;
bdn.bv_len = strlen( dn );
if ( slapi_int_dn_pretty( &bdn, &pdn ) != LDAP_SUCCESS ) {
return NULL;
}
return pdn.bv_val;
}
char *
slapi_dn_normalize_case( char *dn )
{
struct berval bdn;
struct berval ndn;
assert( dn != NULL );
bdn.bv_val = dn;
bdn.bv_len = strlen( dn );
if ( slapi_int_dn_normalize( &bdn, &ndn ) != LDAP_SUCCESS ) {
return NULL;
}
return ndn.bv_val;
}
int
slapi_dn_issuffix(
char *dn,
char *suffix )
{
struct berval bdn, ndn;
struct berval bsuffix, nsuffix;
int rc;
assert( dn != NULL );
assert( suffix != NULL );
bdn.bv_val = dn;
bdn.bv_len = strlen( dn );
bsuffix.bv_val = suffix;
bsuffix.bv_len = strlen( suffix );
if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
return 0;
}
if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
!= LDAP_SUCCESS )
{
slapi_ch_free( (void **)&ndn.bv_val );
return 0;
}
rc = dnIsSuffix( &ndn, &nsuffix );
slapi_ch_free( (void **)&ndn.bv_val );
slapi_ch_free( (void **)&nsuffix.bv_val );
return rc;
}
int
slapi_dn_isparent(
const char *parentdn,
const char *childdn )
{
struct berval assertedParentDN, normalizedAssertedParentDN;
struct berval childDN, normalizedChildDN;
struct berval normalizedParentDN;
int match;
assert( parentdn != NULL );
assert( childdn != NULL );
assertedParentDN.bv_val = (char *)parentdn;
assertedParentDN.bv_len = strlen( parentdn );
if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
&normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
{
return 0;
}
childDN.bv_val = (char *)childdn;
childDN.bv_len = strlen( childdn );
if ( dnNormalize( 0, NULL, NULL, &childDN,
&normalizedChildDN, NULL ) != LDAP_SUCCESS )
{
slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
return 0;
}
dnParent( &normalizedChildDN, &normalizedParentDN );
if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
&normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
{
match = -1;
}
slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
slapi_ch_free( (void **)&normalizedChildDN.bv_val );
return ( match == 0 );
}
/*
* Returns DN of the parent entry, or NULL if the DN is
* an empty string or NULL, or has no parent.
*/
char *
slapi_dn_parent( const char *_dn )
{
struct berval dn, prettyDN;
struct berval parentDN;
char *ret;
if ( _dn == NULL ) {
return NULL;
}
dn.bv_val = (char *)_dn;
dn.bv_len = strlen( _dn );
if ( dn.bv_len == 0 ) {
return NULL;
}
if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
return NULL;
}
dnParent( &prettyDN, &parentDN ); /* in-place */
if ( parentDN.bv_len == 0 ) {
slapi_ch_free_string( &prettyDN.bv_val );
return NULL;
}
ret = slapi_ch_strdup( parentDN.bv_val );
slapi_ch_free_string( &prettyDN.bv_val );
return ret;
}
int slapi_dn_isbesuffix( Slapi_PBlock *pb, char *ldn )
{
struct berval ndn;
Backend *be;
if ( slapi_is_rootdse( ldn ) ) {
return 0;
}
/* according to spec should already be normalized */
ndn.bv_len = strlen( ldn );
ndn.bv_val = ldn;
be = select_backend( &pb->pb_op->o_req_ndn, 0 );
if ( be == NULL ) {
return 0;
}
return be_issuffix( be, &ndn );
}
/*
* Returns DN of the parent entry; or NULL if the DN is
* an empty string, if the DN has no parent, or if the
* DN is the suffix of the backend database
*/
char *slapi_dn_beparent( Slapi_PBlock *pb, const char *ldn )
{
Backend *be;
struct berval dn, prettyDN;
struct berval normalizedDN, parentDN;
char *parent = NULL;
if ( pb == NULL ) {
return NULL;
}
PBLOCK_ASSERT_OP( pb, 0 );
if ( slapi_is_rootdse( ldn ) ) {
return NULL;
}
dn.bv_val = (char *)ldn;
dn.bv_len = strlen( ldn );
if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
return NULL;
}
be = select_backend( &pb->pb_op->o_req_ndn, 0 );
if ( be == NULL || be_issuffix( be, &normalizedDN ) == 0 ) {
dnParent( &prettyDN, &parentDN );
if ( parentDN.bv_len != 0 )
parent = slapi_ch_strdup( parentDN.bv_val );
}
slapi_ch_free_string( &prettyDN.bv_val );
slapi_ch_free_string( &normalizedDN.bv_val );
return parent;
}
char *
slapi_dn_ignore_case( char *dn )
{
return slapi_dn_normalize_case( dn );
}
char *
slapi_ch_malloc( unsigned long size )
{
return ch_malloc( size );
}
void
slapi_ch_free( void **ptr )
{
if ( ptr == NULL || *ptr == NULL )
return;
ch_free( *ptr );
*ptr = NULL;
}
void
slapi_ch_free_string( char **ptr )
{
slapi_ch_free( (void **)ptr );
}
void
slapi_ch_array_free( char **arrayp )
{
char **p;
if ( arrayp != NULL ) {
for ( p = arrayp; *p != NULL; p++ ) {
slapi_ch_free( (void **)p );
}
slapi_ch_free( (void **)&arrayp );
}
}
struct berval *
slapi_ch_bvdup(const struct berval *v)
{
return ber_dupbv(NULL, (struct berval *)v);
}
struct berval **
slapi_ch_bvecdup(const struct berval **v)
{
int i;
struct berval **rv;
if ( v == NULL ) {
return NULL;
}
for ( i = 0; v[i] != NULL; i++ )
;
rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
for ( i = 0; v[i] != NULL; i++ ) {
rv[i] = slapi_ch_bvdup( v[i] );
}
rv[i] = NULL;
return rv;
}
char *
slapi_ch_calloc(
unsigned long nelem,
unsigned long size )
{
return ch_calloc( nelem, size );
}
char *
slapi_ch_realloc(
char *block,
unsigned long size )
{
return ch_realloc( block, size );
}
char *
slapi_ch_strdup( const char *s )
{
return ch_strdup( s );
}
size_t
slapi_ch_stlen( const char *s )
{
return strlen( s );
}
int
slapi_control_present(
LDAPControl **controls,
char *oid,
struct berval **val,
int *iscritical )
{
int i;
int rc = 0;
if ( val ) {
*val = NULL;
}
if ( iscritical ) {
*iscritical = 0;
}
for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
continue;
}
rc = 1;
if ( controls[i]->ldctl_value.bv_len != 0 ) {
if ( val ) {
*val = &controls[i]->ldctl_value;
}
}
if ( iscritical ) {
*iscritical = controls[i]->ldctl_iscritical;
}
break;
}
return rc;
}
static void
slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
unsigned long *slapi_mask)
{
*slapi_mask = SLAPI_OPERATION_NONE;
if ( slap_mask & SLAP_CTRL_ABANDON )
*slapi_mask |= SLAPI_OPERATION_ABANDON;
if ( slap_mask & SLAP_CTRL_ADD )
*slapi_mask |= SLAPI_OPERATION_ADD;
if ( slap_mask & SLAP_CTRL_BIND )
*slapi_mask |= SLAPI_OPERATION_BIND;
if ( slap_mask & SLAP_CTRL_COMPARE )
*slapi_mask |= SLAPI_OPERATION_COMPARE;
if ( slap_mask & SLAP_CTRL_DELETE )
*slapi_mask |= SLAPI_OPERATION_DELETE;
if ( slap_mask & SLAP_CTRL_MODIFY )
*slapi_mask |= SLAPI_OPERATION_MODIFY;
if ( slap_mask & SLAP_CTRL_RENAME )
*slapi_mask |= SLAPI_OPERATION_MODDN;
if ( slap_mask & SLAP_CTRL_SEARCH )
*slapi_mask |= SLAPI_OPERATION_SEARCH;
if ( slap_mask & SLAP_CTRL_UNBIND )
*slapi_mask |= SLAPI_OPERATION_UNBIND;
}
static void
slapiControlOp2SlapControlMask(unsigned long slapi_mask,
slap_mask_t *slap_mask)
{
*slap_mask = 0;
if ( slapi_mask & SLAPI_OPERATION_BIND )
*slap_mask |= SLAP_CTRL_BIND;
if ( slapi_mask & SLAPI_OPERATION_UNBIND )
*slap_mask |= SLAP_CTRL_UNBIND;
if ( slapi_mask & SLAPI_OPERATION_SEARCH )
*slap_mask |= SLAP_CTRL_SEARCH;
if ( slapi_mask & SLAPI_OPERATION_MODIFY )
*slap_mask |= SLAP_CTRL_MODIFY;
if ( slapi_mask & SLAPI_OPERATION_ADD )
*slap_mask |= SLAP_CTRL_ADD;
if ( slapi_mask & SLAPI_OPERATION_DELETE )
*slap_mask |= SLAP_CTRL_DELETE;
if ( slapi_mask & SLAPI_OPERATION_MODDN )
*slap_mask |= SLAP_CTRL_RENAME;
if ( slapi_mask & SLAPI_OPERATION_COMPARE )
*slap_mask |= SLAP_CTRL_COMPARE;
if ( slapi_mask & SLAPI_OPERATION_ABANDON )
*slap_mask |= SLAP_CTRL_ABANDON;
*slap_mask |= SLAP_CTRL_GLOBAL;
}
static int
slapi_int_parse_control(
Operation *op,
SlapReply *rs,
LDAPControl *ctrl )
{
/* Plugins must deal with controls themselves. */
return LDAP_SUCCESS;
}
void
slapi_register_supported_control(
char *controloid,
unsigned long controlops )
{
slap_mask_t controlmask;
slapiControlOp2SlapControlMask( controlops, &controlmask );
register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
}
int
slapi_get_supported_controls(
char ***ctrloidsp,
unsigned long **ctrlopsp )
{
int i, rc;
rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
if ( rc != LDAP_SUCCESS ) {
return rc;
}
for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
/* In place, naughty. */
slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
}
return LDAP_SUCCESS;
}
LDAPControl *
slapi_dup_control( LDAPControl *ctrl )
{
LDAPControl *ret;
ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
ret->ldctl_iscritical = ctrl->ldctl_iscritical;
return ret;
}
void
slapi_register_supported_saslmechanism( char *mechanism )
{
/* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
"OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
}
char **
slapi_get_supported_saslmechanisms( void )
{
/* FIXME -- can not get the saslmechanism without a connection. */
slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
"can not get the SASL mechanism list "
"without a connection\n" );
return NULL;
}
char **
slapi_get_supported_extended_ops( void )
{
int i, j, k;
char **ppExtOpOID = NULL;
int numExtOps = 0;
for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
;
}
for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
;
}
numExtOps = i + j;
if ( numExtOps == 0 ) {
return NULL;
}
ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
for ( k = 0; k < i; k++ ) {
struct berval *bv;
bv = get_supported_extop( k );
assert( bv != NULL );
ppExtOpOID[ k ] = bv->bv_val;
}
for ( ; k < j; k++ ) {
struct berval *bv;
bv = slapi_int_get_supported_extop( k );
assert( bv != NULL );
ppExtOpOID[ i + k ] = bv->bv_val;
}
ppExtOpOID[ i + k ] = NULL;
return ppExtOpOID;
}
void
slapi_send_ldap_result(
Slapi_PBlock *pb,
int err,
char *matched,
char *text,
int nentries,
struct berval **urls )
{
SlapReply *rs;
PBLOCK_ASSERT_OP( pb, 0 );
rs = pb->pb_rs;
rs->sr_err = err;
rs->sr_matched = matched;
rs->sr_text = text;
rs->sr_ref = NULL;
if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
send_ldap_sasl( pb->pb_op, rs );
} else if ( rs->sr_rspoid != NULL ) {
send_ldap_extended( pb->pb_op, rs );
} else {
if ( pb->pb_op->o_tag == LDAP_REQ_SEARCH )
rs->sr_nentries = nentries;
if ( urls != NULL )
bvptr2obj( urls, &rs->sr_ref, NULL );
send_ldap_result( pb->pb_op, rs );
if ( urls != NULL )
slapi_ch_free( (void **)&rs->sr_ref );
}
}
int
slapi_send_ldap_search_entry(
Slapi_PBlock *pb,
Slapi_Entry *e,
LDAPControl **ectrls,
char **attrs,
int attrsonly )
{
SlapReply rs = { REP_SEARCH };
int i = 0, j = 0;
AttributeName *an = NULL;
const char *text;
int rc;
assert( pb->pb_op != NULL );
if ( attrs != NULL ) {
for ( i = 0; attrs[ i ] != NULL; i++ ) {
; /* empty */
}
}
if ( i ) {
an = (AttributeName *) slapi_ch_calloc( i + 1, sizeof(AttributeName) );
for ( i = 0; attrs[i] != NULL; i++ ) {
an[j].an_name.bv_val = attrs[i];
an[j].an_name.bv_len = strlen( attrs[i] );
an[j].an_desc = NULL;
if ( slap_bv2ad( &an[j].an_name, &an[j].an_desc, &text ) == LDAP_SUCCESS) {
j++;
}
}
an[j].an_name.bv_len = 0;
an[j].an_name.bv_val = NULL;
}
rs.sr_err = LDAP_SUCCESS;
rs.sr_matched = NULL;
rs.sr_text = NULL;
rs.sr_ref = NULL;
rs.sr_ctrls = ectrls;
rs.sr_attrs = an;
rs.sr_operational_attrs = NULL;
rs.sr_entry = e;
rs.sr_v2ref = NULL;
rs.sr_flags = 0;
rc = send_search_entry( pb->pb_op, &rs );
slapi_ch_free( (void **)&an );
return rc;
}
int
slapi_send_ldap_search_reference(
Slapi_PBlock *pb,
Slapi_Entry *e,
struct berval **references,
LDAPControl **ectrls,
struct berval **v2refs
)
{
SlapReply rs = { REP_SEARCHREF };
int rc;
rs.sr_err = LDAP_SUCCESS;
rs.sr_matched = NULL;
rs.sr_text = NULL;
rc = bvptr2obj( references, &rs.sr_ref, NULL );
if ( rc != LDAP_SUCCESS ) {
return rc;
}
rs.sr_ctrls = ectrls;
rs.sr_attrs = NULL;
rs.sr_operational_attrs = NULL;
rs.sr_entry = e;
if ( v2refs != NULL ) {
rc = bvptr2obj( v2refs, &rs.sr_v2ref, NULL );
if ( rc != LDAP_SUCCESS ) {
slapi_ch_free( (void **)&rs.sr_ref );
return rc;
}
} else {
rs.sr_v2ref = NULL;
}
rc = send_search_reference( pb->pb_op, &rs );
slapi_ch_free( (void **)&rs.sr_ref );
slapi_ch_free( (void **)&rs.sr_v2ref );
return rc;
}
Slapi_Filter *
slapi_str2filter( char *str )
{
return str2filter( str );
}
void
slapi_filter_free(
Slapi_Filter *f,
int recurse )
{
filter_free( f );
}
Slapi_Filter *
slapi_filter_dup( Slapi_Filter *filter )
{
return filter_dup( filter, NULL );
}
int
slapi_filter_get_choice( Slapi_Filter *f )
{
int rc;
if ( f != NULL ) {
rc = f->f_choice;
} else {
rc = 0;
}
return rc;
}
int
slapi_filter_get_ava(
Slapi_Filter *f,
char **type,
struct berval **bval )
{
int ftype;
int rc = LDAP_SUCCESS;
assert( type != NULL );
assert( bval != NULL );
*type = NULL;
*bval = NULL;
ftype = f->f_choice;
if ( ftype == LDAP_FILTER_EQUALITY
|| ftype == LDAP_FILTER_GE
|| ftype == LDAP_FILTER_LE
|| ftype == LDAP_FILTER_APPROX ) {
/*
* According to the SLAPI Reference Manual these are
* not duplicated.
*/
*type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
*bval = &f->f_un.f_un_ava->aa_value;
} else { /* filter type not supported */
rc = -1;
}
return rc;
}
Slapi_Filter *
slapi_filter_list_first( Slapi_Filter *f )
{
int ftype;
if ( f == NULL ) {
return NULL;
}
ftype = f->f_choice;
if ( ftype == LDAP_FILTER_AND
|| ftype == LDAP_FILTER_OR
|| ftype == LDAP_FILTER_NOT ) {
return (Slapi_Filter *)f->f_list;
} else {
return NULL;
}
}
Slapi_Filter *
slapi_filter_list_next(
Slapi_Filter *f,
Slapi_Filter *fprev )
{
int ftype;
if ( f == NULL ) {
return NULL;
}
ftype = f->f_choice;
if ( ftype == LDAP_FILTER_AND
|| ftype == LDAP_FILTER_OR
|| ftype == LDAP_FILTER_NOT )
{
return fprev->f_next;
}
return NULL;
}
int
slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
{
if ( f == NULL ) {
return -1;
}
switch ( f->f_choice ) {
case LDAP_FILTER_GE:
case LDAP_FILTER_LE:
case LDAP_FILTER_EQUALITY:
case LDAP_FILTER_APPROX:
*type = f->f_av_desc->ad_cname.bv_val;
break;
case LDAP_FILTER_SUBSTRINGS:
*type = f->f_sub_desc->ad_cname.bv_val;
break;
case LDAP_FILTER_PRESENT:
*type = f->f_desc->ad_cname.bv_val;
break;
case LDAP_FILTER_EXT:
*type = f->f_mr_desc->ad_cname.bv_val;
break;
default:
/* Complex filters need not apply. */
*type = NULL;
return -1;
}
return 0;
}
int
slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type )
{
AttributeDescription **adp, *ad = NULL;
const char *text;
int rc;
if ( f == NULL ) {
return -1;
}
switch ( f->f_choice ) {
case LDAP_FILTER_GE:
case LDAP_FILTER_LE:
case LDAP_FILTER_EQUALITY:
case LDAP_FILTER_APPROX:
adp = &f->f_av_desc;
break;
case LDAP_FILTER_SUBSTRINGS:
adp = &f->f_sub_desc;
break;
case LDAP_FILTER_PRESENT:
adp = &f->f_desc;
break;
case LDAP_FILTER_EXT:
adp = &f->f_mr_desc;
break;
default:
/* Complex filters need not apply. */
return -1;
}
rc = slap_str2ad( type, &ad, &text );
if ( rc == LDAP_SUCCESS )
*adp = ad;
return ( rc == LDAP_SUCCESS ) ? 0 : -1;
}
int
slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
char ***any, char **final )
{
int i;
if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
return -1;
}
/*
* The caller shouldn't free but we can't return an
* array of char *s from an array of bervals without
* allocating memory, so we may as well be consistent.
* XXX
*/
*type = f->f_sub_desc->ad_cname.bv_val;
*initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
if ( f->f_sub_any != NULL ) {
for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
;
*any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
(*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
}
(*any)[i] = NULL;
} else {
*any = NULL;
}
*final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
return 0;
}
Slapi_Filter *
slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
{
Slapi_Filter *f = NULL;
if ( ftype == LDAP_FILTER_AND ||
ftype == LDAP_FILTER_OR ||
ftype == LDAP_FILTER_NOT )
{
f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
f->f_choice = ftype;
f->f_list = f1;
f->f_list->f_next = f2;
f->f_next = NULL;
}
return f;
}
int
slapi_x_filter_append( int ftype,
Slapi_Filter **pContainingFilter, /* NULL on first call */
Slapi_Filter **pNextFilter,
Slapi_Filter *filterToAppend )
{
if ( ftype == LDAP_FILTER_AND ||
ftype == LDAP_FILTER_OR ||
ftype == LDAP_FILTER_NOT )
{
if ( *pContainingFilter == NULL ) {
*pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
(*pContainingFilter)->f_choice = ftype;
(*pContainingFilter)->f_list = filterToAppend;
(*pContainingFilter)->f_next = NULL;
} else {
if ( (*pContainingFilter)->f_choice != ftype ) {
/* Sanity check */
return -1;
}
(*pNextFilter)->f_next = filterToAppend;
}
*pNextFilter = filterToAppend;
return 0;
}
return -1;
}
int
slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
int verify_access )
{
Operation *op;
int rc;
if ( f == NULL ) {
/* spec says return zero if no filter. */
return 0;
}
if ( verify_access ) {
op = pb->pb_op;
if ( op == NULL )
return LDAP_PARAM_ERROR;
} else {
op = NULL;
}
/*
* According to acl.c it is safe to call test_filter() with
* NULL arguments...
*/
rc = test_filter( op, e, f );
switch (rc) {
case LDAP_COMPARE_TRUE:
rc = 0;
break;
case LDAP_COMPARE_FALSE:
break;
case SLAPD_COMPARE_UNDEFINED:
rc = LDAP_OTHER;
break;
case LDAP_PROTOCOL_ERROR:
/* filter type unknown: spec says return -1 */
rc = -1;
break;
}
return rc;
}
int
slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
{
return slapi_filter_test( NULL, e, f, 0 );
}
int
slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
{
switch ( f->f_choice ) {
case LDAP_FILTER_AND:
case LDAP_FILTER_NOT:
case LDAP_FILTER_OR: {
int rc;
/*
* FIXME: altering f; should we use a temporary?
*/
for ( f = f->f_list; f != NULL; f = f->f_next ) {
rc = slapi_filter_apply( f, fn, arg, error_code );
if ( rc != 0 ) {
return rc;
}
if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
break;
}
}
break;
}
case LDAP_FILTER_EQUALITY:
case LDAP_FILTER_SUBSTRINGS:
case LDAP_FILTER_GE:
case LDAP_FILTER_LE:
case LDAP_FILTER_PRESENT:
case LDAP_FILTER_APPROX:
case LDAP_FILTER_EXT:
*error_code = fn( f, arg );
break;
default:
*error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
}
if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
*error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
return 0;
}
return -1;
}
int
slapi_pw_find(
struct berval **vals,
struct berval *v )
{
int i;
if( ( vals == NULL ) || ( v == NULL ) )
return 1;
for ( i = 0; vals[i] != NULL; i++ ) {
if ( !lutil_passwd( vals[i], v, NULL, NULL ) )
return 0;
}
return 1;
}
/* Get connected client IP address.
*
* The user must free the returned client IP after its use.
* Compatible with IBM Tivoli call.
*
* Errors:
* * LDAP_PARAM_ERROR - If the pb parameter is null.
* * LDAP_OPERATIONS_ERROR - If the API encounters error processing the request.
* * LDAP_NO_MEMORY - Failed to allocate required memory.
*/
int
slapi_get_client_ip(Slapi_PBlock *pb, char **clientIP)
{
char *s = NULL;
if(pb == NULL || pb->pb_conn == NULL) return(LDAP_PARAM_ERROR);
if((s = (char *) slapi_ch_malloc(pb->pb_conn->c_peer_name.bv_len + 1)) == NULL) {
return(LDAP_NO_MEMORY);
}
memcpy(s, pb->pb_conn->c_peer_name.bv_val, pb->pb_conn->c_peer_name.bv_len);
s[pb->pb_conn->c_peer_name.bv_len] = 0;
*clientIP = s;
return(LDAP_SUCCESS);
}
/* Free previously allocated client IP address. */
void
slapi_free_client_ip(char **clientIP)
{
slapi_ch_free((void **) clientIP);
}
#define MAX_HOSTNAME 512
char *
slapi_get_hostname( void )
{
char *hn = NULL;
static int been_here = 0;
static char *static_hn = NULL;
ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
if ( !been_here ) {
static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
if ( static_hn == NULL) {
slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
"Cannot allocate memory for hostname\n" );
static_hn = NULL;
ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
return hn;
} else {
if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
slapi_log_error( SLAPI_LOG_FATAL,
"SLAPI",
"can't get hostname\n" );
slapi_ch_free( (void **)&static_hn );
static_hn = NULL;
ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
return hn;
} else {
been_here = 1;
}
}
}
ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
hn = ch_strdup( static_hn );
return hn;
}
/*
* FIXME: this should go in an appropriate header ...
*/
extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
int
slapi_log_error(
int severity,
char *subsystem,
char *fmt,
... )
{
int rc = LDAP_SUCCESS;
va_list arglist;
va_start( arglist, fmt );
rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
va_end( arglist );
return rc;
}
unsigned long
slapi_timer_current_time( void )
{
static int first_time = 1;
#if !defined (_WIN32)
struct timeval now;
unsigned long ret;
ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
if (first_time) {
first_time = 0;
gettimeofday( &base_time, NULL );
}
gettimeofday( &now, NULL );
ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
(now.tv_usec - base_time.tv_usec);
ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
return ret;
/*
* Ain't it better?
return (slap_get_time() - starttime) * 1000000;
*/
#else /* _WIN32 */
LARGE_INTEGER now;
if ( first_time ) {
first_time = 0;
performance_counter_present = QueryPerformanceCounter( &base_time );
QueryPerformanceFrequency( &performance_freq );
}
if ( !performance_counter_present )
return 0;
QueryPerformanceCounter( &now );
return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
#endif /* _WIN32 */
}
/*
* FIXME ?
*/
unsigned long
slapi_timer_get_time( char *label )
{
unsigned long start = slapi_timer_current_time();
printf("%10ld %10d usec %s\n", start, 0, label);
return start;
}
/*
* FIXME ?
*/
void
slapi_timer_elapsed_time(
char *label,
unsigned long start )
{
unsigned long stop = slapi_timer_current_time();
printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
}
void
slapi_free_search_results_internal( Slapi_PBlock *pb )
{
Slapi_Entry **entries;
int k = 0, nEnt = 0;
slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
if ( nEnt == 0 || entries == NULL ) {
return;
}
for ( k = 0; k < nEnt; k++ ) {
slapi_entry_free( entries[k] );
entries[k] = NULL;
}
slapi_ch_free( (void **)&entries );
}
int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
{
if ( pb == NULL )
return LDAP_PARAM_ERROR;
if ( pb->pb_conn == NULL )
return LDAP_PARAM_ERROR;
#ifdef HAVE_TLS
*isSSL = pb->pb_conn->c_is_tls;
#else
*isSSL = 0;
#endif
return LDAP_SUCCESS;
}
/*
* DS 5.x compatability API follow
*/
int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
{
AttributeType *at;
if ( attr == NULL )
return LDAP_PARAM_ERROR;
at = attr->a_desc->ad_type;
*flags = SLAPI_ATTR_FLAG_STD_ATTR;
if ( is_at_single_value( at ) )
*flags |= SLAPI_ATTR_FLAG_SINGLE;
if ( is_at_operational( at ) )
*flags |= SLAPI_ATTR_FLAG_OPATTR;
if ( is_at_obsolete( at ) )
*flags |= SLAPI_ATTR_FLAG_OBSOLETE;
if ( is_at_collective( at ) )
*flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
if ( is_at_no_user_mod( at ) )
*flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
return LDAP_SUCCESS;
}
int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
{
unsigned long flags;
if ( slapi_attr_get_flags( attr, &flags ) != 0 )
return 0;
return (flags & flag) ? 1 : 0;
}
Slapi_Attr *slapi_attr_new( void )
{
Attribute *ad;
ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
return ad;
}
Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
{
const char *text;
AttributeDescription *ad = NULL;
if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
return NULL;
}
a->a_desc = ad;
a->a_vals = NULL;
a->a_nvals = NULL;
a->a_next = NULL;
a->a_flags = 0;
return a;
}
void slapi_attr_free( Slapi_Attr **a )
{
attr_free( *a );
*a = NULL;
}
Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
{
return attr_dup( (Slapi_Attr *)attr );
}
int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
{
struct berval nval;
struct berval *nvalp;
int rc;
AttributeDescription *desc = a->a_desc;
if ( desc->ad_type->sat_equality &&
desc->ad_type->sat_equality->smr_normalize ) {
rc = (*desc->ad_type->sat_equality->smr_normalize)(
SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
desc->ad_type->sat_syntax,
desc->ad_type->sat_equality,
(Slapi_Value *)v, &nval, NULL );
if ( rc != LDAP_SUCCESS ) {
return rc;
}
nvalp = &nval;
} else {
nvalp = NULL;
}
rc = attr_valadd( a, (Slapi_Value *)v, nvalp, 1 );
if ( nvalp != NULL ) {
slapi_ch_free_string( &nval.bv_val );
}
return rc;
}
int slapi_attr_type2plugin( const char *type, void **pi )
{
*pi = NULL;
return LDAP_OTHER;
}
int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
{
if ( attr == NULL ) {
return LDAP_PARAM_ERROR;
}
*type = attr->a_desc->ad_cname.bv_val;
return LDAP_SUCCESS;
}
int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
{
if ( attr == NULL ) {
return LDAP_PARAM_ERROR;
}
*oidp = attr->a_desc->ad_type->sat_oid;
return LDAP_SUCCESS;
}
int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
{
MatchingRule *mr;
int ret;
int rc;
const char *text;
mr = a->a_desc->ad_type->sat_equality;
rc = value_match( &ret, a->a_desc, mr,
SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
(struct berval *)v1, (void *)v2, &text );
if ( rc != LDAP_SUCCESS )
return -1;
return ( ret == 0 ) ? 0 : -1;
}
int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
{
int rc;
if ( a ->a_vals == NULL ) {
return -1;
}
rc = attr_valfind( (Attribute *)a, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, v,
NULL, NULL );
return rc == 0 ? 0 : -1;
}
int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
{
AttributeDescription *a1 = NULL;
AttributeDescription *a2 = NULL;
const char *text;
int ret;
if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
return -1;
}
if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
return 1;
}
#define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
switch ( opt ) {
case SLAPI_TYPE_CMP_EXACT:
ret = ad_cmp( a1, a2 );
break;
case SLAPI_TYPE_CMP_BASE:
ret = ad_base_cmp( a1, a2 );
break;
case SLAPI_TYPE_CMP_SUBTYPE:
ret = is_ad_subtype( a2, a2 );
break;
default:
ret = -1;
break;
}
return ret;
}
int slapi_attr_types_equivalent( const char *t1, const char *t2 )
{
return ( slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT ) == 0 );
}
int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
{
return slapi_valueset_first_value( &a->a_vals, v );
}
int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
{
return slapi_valueset_next_value( &a->a_vals, hint, v );
}
int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
{
*numValues = slapi_valueset_count( &a->a_vals );
return 0;
}
int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
{
*vs = &((Slapi_Attr *)a)->a_vals;
return 0;
}
int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
{
return slapi_attr_get_values( a, vals );
}
char *slapi_attr_syntax_normalize( const char *s )
{
AttributeDescription *ad = NULL;
const char *text;
if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
return NULL;
}
return ad->ad_cname.bv_val;
}
Slapi_Value *slapi_value_new( void )
{
struct berval *bv;
bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
return bv;
}
Slapi_Value *slapi_value_new_berval(const struct berval *bval)
{
return ber_dupbv( NULL, (struct berval *)bval );
}
Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
{
return slapi_value_new_berval( v );
}
Slapi_Value *slapi_value_new_string(const char *s)
{
struct berval bv;
bv.bv_val = (char *)s;
bv.bv_len = strlen( s );
return slapi_value_new_berval( &bv );
}
Slapi_Value *slapi_value_init(Slapi_Value *val)
{
val->bv_val = NULL;
val->bv_len = 0;
return val;
}
Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
{
return ber_dupbv( v, bval );
}
Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
{
v->bv_val = slapi_ch_strdup( s );
v->bv_len = strlen( s );
return v;
}
Slapi_Value *slapi_value_dup(const Slapi_Value *v)
{
return slapi_value_new_value( v );
}
void slapi_value_free(Slapi_Value **value)
{
if ( value == NULL ) {
return;
}
if ( (*value) != NULL ) {
slapi_ch_free( (void **)&(*value)->bv_val );
slapi_ch_free( (void **)value );
}
}
const struct berval *slapi_value_get_berval( const Slapi_Value *value )
{
return value;
}
Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
{
if ( value == NULL ) {
return NULL;
}
if ( value->bv_val != NULL ) {
slapi_ch_free( (void **)&value->bv_val );
}
slapi_value_init_berval( value, (struct berval *)bval );
return value;
}
Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
{
if ( value == NULL ) {
return NULL;
}
return slapi_value_set_berval( value, vfrom );
}
Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
{
if ( value == NULL ) {
return NULL;
}
if ( value->bv_val != NULL ) {
slapi_ch_free( (void **)&value->bv_val );
}
value->bv_val = slapi_ch_malloc( len );
value->bv_len = len;
AC_MEMCPY( value->bv_val, val, len );
return value;
}
int slapi_value_set_string(Slapi_Value *value, const char *strVal)
{
if ( value == NULL ) {
return -1;
}
slapi_value_set( value, (void *)strVal, strlen( strVal ) );
return 0;
}
int slapi_value_set_int(Slapi_Value *value, int intVal)
{
char buf[64];
snprintf( buf, sizeof( buf ), "%d", intVal );
return slapi_value_set_string( value, buf );
}
const char *slapi_value_get_string(const Slapi_Value *value)
{
if ( value == NULL ) return NULL;
if ( value->bv_val == NULL ) return NULL;
if ( !checkBVString( value ) ) return NULL;
return value->bv_val;
}
int slapi_value_get_int(const Slapi_Value *value)
{
if ( value == NULL ) return 0;
if ( value->bv_val == NULL ) return 0;
if ( !checkBVString( value ) ) return 0;
return (int)strtol( value->bv_val, NULL, 10 );
}
unsigned int slapi_value_get_uint(const Slapi_Value *value)
{
if ( value == NULL ) return 0;
if ( value->bv_val == NULL ) return 0;
if ( !checkBVString( value ) ) return 0;
return (unsigned int)strtoul( value->bv_val, NULL, 10 );
}
long slapi_value_get_long(const Slapi_Value *value)
{
if ( value == NULL ) return 0;
if ( value->bv_val == NULL ) return 0;
if ( !checkBVString( value ) ) return 0;
return strtol( value->bv_val, NULL, 10 );
}
unsigned long slapi_value_get_ulong(const Slapi_Value *value)
{
if ( value == NULL ) return 0;
if ( value->bv_val == NULL ) return 0;
if ( !checkBVString( value ) ) return 0;
return strtoul( value->bv_val, NULL, 10 );
}
size_t slapi_value_get_length(const Slapi_Value *value)
{
if ( value == NULL )
return 0;
return (size_t) value->bv_len;
}
int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
{
return slapi_attr_value_cmp( a, v1, v2 );
}
/* A ValueSet is a container for a BerVarray. */
Slapi_ValueSet *slapi_valueset_new( void )
{
Slapi_ValueSet *vs;
vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
*vs = NULL;
return vs;
}
void slapi_valueset_free(Slapi_ValueSet *vs)
{
if ( vs != NULL ) {
BerVarray vp = *vs;
ber_bvarray_free( vp );
vp = NULL;
slapi_ch_free( (void **)&vp );
}
}
void slapi_valueset_init(Slapi_ValueSet *vs)
{
if ( vs != NULL && *vs == NULL ) {
*vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
(*vs)->bv_val = NULL;
(*vs)->bv_len = 0;
}
}
void slapi_valueset_done(Slapi_ValueSet *vs)
{
BerVarray vp;
if ( vs == NULL )
return;
for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
vp->bv_len = 0;
slapi_ch_free( (void **)&vp->bv_val );
}
/* but don't free *vs or vs */
}
void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
{
struct berval bv;
ber_dupbv( &bv, (Slapi_Value *)addval );
ber_bvarray_add( vs, &bv );
}
int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
{
return slapi_valueset_next_value( vs, 0, v );
}
int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
{
int i;
BerVarray vp;
if ( vs == NULL )
return -1;
vp = *vs;
for ( i = 0; vp[i].bv_val != NULL; i++ ) {
if ( i == index ) {
*v = &vp[i];
return index + 1;
}
}
return -1;
}
int slapi_valueset_count( const Slapi_ValueSet *vs )
{
int i;
BerVarray vp;
if ( vs == NULL )
return 0;
vp = *vs;
if ( vp == NULL )
return 0;
for ( i = 0; vp[i].bv_val != NULL; i++ )
;
return i;
}
void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
{
BerVarray vp;
for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
slapi_valueset_add_value( vs1, vp );
}
}
int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
struct berval *val, int access )
{
int rc;
slap_access_t slap_access;
AttributeDescription *ad = NULL;
const char *text;
rc = slap_str2ad( attr, &ad, &text );
if ( rc != LDAP_SUCCESS ) {
return rc;
}
/*
* Whilst the SLAPI access types are arranged as a bitmask, the
* documentation indicates that they are to be used separately.
*/
switch ( access & SLAPI_ACL_ALL ) {
case SLAPI_ACL_COMPARE:
slap_access = ACL_COMPARE;
break;
case SLAPI_ACL_SEARCH:
slap_access = ACL_SEARCH;
break;
case SLAPI_ACL_READ:
slap_access = ACL_READ;
break;
case SLAPI_ACL_WRITE:
slap_access = ACL_WRITE;
break;
case SLAPI_ACL_DELETE:
slap_access = ACL_WDEL;
break;
case SLAPI_ACL_ADD:
slap_access = ACL_WADD;
break;
case SLAPI_ACL_SELF: /* not documented */
case SLAPI_ACL_PROXY: /* not documented */
default:
return LDAP_INSUFFICIENT_ACCESS;
break;
}
assert( pb->pb_op != NULL );
if ( access_allowed( pb->pb_op, e, ad, val, slap_access, NULL ) ) {
return LDAP_SUCCESS;
}
return LDAP_INSUFFICIENT_ACCESS;
}
int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
{
int rc = LDAP_SUCCESS;
Modifications *ml;
if ( pb == NULL || pb->pb_op == NULL )
return LDAP_PARAM_ERROR;
ml = slapi_int_ldapmods2modifications( pb->pb_op, mods );
if ( ml == NULL ) {
return LDAP_OTHER;
}
if ( rc == LDAP_SUCCESS ) {
rc = acl_check_modlist( pb->pb_op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
}
slap_mods_free( ml, 1 );
return rc;
}
/*
* Synthesise an LDAPMod array from a Modifications list to pass
* to SLAPI.
*/
LDAPMod **slapi_int_modifications2ldapmods( Modifications *modlist )
{
Modifications *ml;
LDAPMod **mods, *modp;
int i, j;
for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
;
mods = (LDAPMod **)slapi_ch_malloc( (i + 1) * sizeof(LDAPMod *) );
for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
mods[i] = (LDAPMod *)slapi_ch_malloc( sizeof(LDAPMod) );
modp = mods[i];
modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
if ( BER_BVISNULL( &ml->sml_type ) ) {
/* may happen for internally generated mods */
assert( ml->sml_desc != NULL );
modp->mod_type = slapi_ch_strdup( ml->sml_desc->ad_cname.bv_val );
} else {
modp->mod_type = slapi_ch_strdup( ml->sml_type.bv_val );
}
if ( ml->sml_values != NULL ) {
for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
;
modp->mod_bvalues = (struct berval **)slapi_ch_malloc( (j + 1) *
sizeof(struct berval *) );
for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
modp->mod_bvalues[j] = (struct berval *)slapi_ch_malloc(
sizeof(struct berval) );
ber_dupbv( modp->mod_bvalues[j], &ml->sml_values[j] );
}
modp->mod_bvalues[j] = NULL;
} else {
modp->mod_bvalues = NULL;
}
i++;
}
mods[i] = NULL;
return mods;
}
/*
* Convert a potentially modified array of LDAPMods back to a
* Modification list. Unfortunately the values need to be
* duplicated because slap_mods_check() will try to free them
* before prettying (and we can't easily get out of calling
* slap_mods_check() because we need normalized values).
*/
Modifications *slapi_int_ldapmods2modifications ( Operation *op, LDAPMod **mods )
{
Modifications *modlist = NULL, **modtail;
LDAPMod **modp;
char textbuf[SLAP_TEXT_BUFLEN];
const char *text;
if ( mods == NULL ) {
return NULL;
}
modtail = &modlist;
for ( modp = mods; *modp != NULL; modp++ ) {
Modifications *mod;
LDAPMod *lmod = *modp;
int i;
const char *text;
AttributeDescription *ad = NULL;
if ( slap_str2ad( lmod->mod_type, &ad, &text ) != LDAP_SUCCESS ) {
continue;
}
mod = (Modifications *) slapi_ch_malloc( sizeof(Modifications) );
mod->sml_op = lmod->mod_op & ~(LDAP_MOD_BVALUES);
mod->sml_flags = 0;
mod->sml_type = ad->ad_cname;
mod->sml_desc = ad;
mod->sml_next = NULL;
i = 0;
if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
if ( lmod->mod_bvalues != NULL ) {
while ( lmod->mod_bvalues[i] != NULL )
i++;
}
} else {
if ( lmod->mod_values != NULL ) {
while ( lmod->mod_values[i] != NULL )
i++;
}
}
mod->sml_numvals = i;
if ( i == 0 ) {
mod->sml_values = NULL;
} else {
mod->sml_values = (BerVarray) slapi_ch_malloc( (i + 1) * sizeof(struct berval) );
/* NB: This implicitly trusts a plugin to return valid modifications. */
if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
for ( i = 0; lmod->mod_bvalues[i] != NULL; i++ ) {
ber_dupbv( &mod->sml_values[i], lmod->mod_bvalues[i] );
}
} else {
for ( i = 0; lmod->mod_values[i] != NULL; i++ ) {
mod->sml_values[i].bv_val = slapi_ch_strdup( lmod->mod_values[i] );
mod->sml_values[i].bv_len = strlen( lmod->mod_values[i] );
}
}
mod->sml_values[i].bv_val = NULL;
mod->sml_values[i].bv_len = 0;
}
mod->sml_nvalues = NULL;
*modtail = mod;
modtail = &mod->sml_next;
}
if ( slap_mods_check( op, modlist, &text, textbuf, sizeof( textbuf ), NULL ) != LDAP_SUCCESS ) {
slap_mods_free( modlist, 1 );
modlist = NULL;
}
return modlist;
}
/*
* Sun ONE DS 5.x computed attribute support. Computed attributes
* allow for dynamically generated operational attributes, a very
* useful thing indeed.
*/
/*
* For some reason Sun don't use the normal plugin mechanism
* registration path to register an "evaluator" function (an
* "evaluator" is responsible for adding computed attributes;
* the nomenclature is somewhat confusing).
*
* As such slapi_compute_add_evaluator() registers the
* function directly.
*/
int slapi_compute_add_evaluator(slapi_compute_callback_t function)
{
Slapi_PBlock *pPlugin = NULL;
int rc;
int type = SLAPI_PLUGIN_OBJECT;
pPlugin = slapi_pblock_new();
if ( pPlugin == NULL ) {
rc = LDAP_NO_MEMORY;
goto done;
}
rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
if ( rc != LDAP_SUCCESS ) {
goto done;
}
rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
if ( rc != LDAP_SUCCESS ) {
goto done;
}
rc = slapi_int_register_plugin( frontendDB, pPlugin );
if ( rc != 0 ) {
rc = LDAP_OTHER;
goto done;
}
done:
if ( rc != LDAP_SUCCESS ) {
if ( pPlugin != NULL ) {
slapi_pblock_destroy( pPlugin );
}
return -1;
}
return 0;
}
/*
* See notes above regarding slapi_compute_add_evaluator().
*/
int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
{
Slapi_PBlock *pPlugin = NULL;
int rc;
int type = SLAPI_PLUGIN_OBJECT;
pPlugin = slapi_pblock_new();
if ( pPlugin == NULL ) {
rc = LDAP_NO_MEMORY;
goto done;
}
rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
if ( rc != LDAP_SUCCESS ) {
goto done;
}
rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
if ( rc != LDAP_SUCCESS ) {
goto done;
}
rc = slapi_int_register_plugin( frontendDB, pPlugin );
if ( rc != 0 ) {
rc = LDAP_OTHER;
goto done;
}
done:
if ( rc != LDAP_SUCCESS ) {
if ( pPlugin != NULL ) {
slapi_pblock_destroy( pPlugin );
}
return -1;
}
return 0;
}
/*
* Call compute evaluators
*/
int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
{
int rc = 0;
slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
/* Nothing to do; front-end should ignore. */
return 0;
}
for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
/*
* -1: no attribute matched requested type
* 0: one attribute matched
* >0: error happened
*/
rc = (*pGetPlugin)( c, type, e, outputfn );
if ( rc > 0 ) {
break;
}
}
slapi_ch_free( (void **)&tmpPlugin );
return rc;
}
int
compute_rewrite_search_filter( Slapi_PBlock *pb )
{
if ( pb == NULL || pb->pb_op == NULL )
return LDAP_PARAM_ERROR;
return slapi_int_call_plugins( pb->pb_op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
}
/*
* New API to provide the plugin with access to the search
* pblock. Have informed Sun DS team.
*/
int
slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
{
if ( c == NULL )
return -1;
if ( c->cac_pb == NULL )
return -1;
*pb = c->cac_pb;
return 0;
}
Slapi_Mutex *slapi_new_mutex( void )
{
Slapi_Mutex *m;
m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
slapi_ch_free( (void **)&m );
return NULL;
}
return m;
}
void slapi_destroy_mutex( Slapi_Mutex *mutex )
{
if ( mutex != NULL ) {
ldap_pvt_thread_mutex_destroy( &mutex->mutex );
slapi_ch_free( (void **)&mutex);
}
}
void slapi_lock_mutex( Slapi_Mutex *mutex )
{
ldap_pvt_thread_mutex_lock( &mutex->mutex );
}
int slapi_unlock_mutex( Slapi_Mutex *mutex )
{
return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
}
Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
{
Slapi_CondVar *cv;
if ( mutex == NULL ) {
return NULL;
}
cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
slapi_ch_free( (void **)&cv );
return NULL;
}
cv->mutex = mutex->mutex;
return cv;
}
void slapi_destroy_condvar( Slapi_CondVar *cvar )
{
if ( cvar != NULL ) {
ldap_pvt_thread_cond_destroy( &cvar->cond );
slapi_ch_free( (void **)&cvar );
}
}
int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
{
if ( cvar == NULL ) {
return -1;
}
return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
}
int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
{
if ( cvar == NULL ) {
return -1;
}
if ( notify_all ) {
return ldap_pvt_thread_cond_broadcast( &cvar->cond );
}
return ldap_pvt_thread_cond_signal( &cvar->cond );
}
int slapi_int_access_allowed( Operation *op,
Entry *entry,
AttributeDescription *desc,
struct berval *val,
slap_access_t access,
AccessControlState *state )
{
int rc, slap_access = 0;
slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
Slapi_PBlock *pb;
pb = SLAPI_OPERATION_PBLOCK( op );
if ( pb == NULL ) {
/* internal operation */
return 1;
}
switch ( access ) {
case ACL_COMPARE:
slap_access |= SLAPI_ACL_COMPARE;
break;
case ACL_SEARCH:
slap_access |= SLAPI_ACL_SEARCH;
break;
case ACL_READ:
slap_access |= SLAPI_ACL_READ;
break;
case ACL_WRITE:
slap_access |= SLAPI_ACL_WRITE;
break;
case ACL_WDEL:
slap_access |= SLAPI_ACL_DELETE;
break;
case ACL_WADD:
slap_access |= SLAPI_ACL_ADD;
break;
default:
break;
}
rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
/* nothing to do; allowed access */
return 1;
}
rc = 1; /* default allow policy */
for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
/*
* 0 access denied
* 1 access granted
*/
rc = (*pGetPlugin)( pb, entry, desc->ad_cname.bv_val,
val, slap_access, (void *)state );
if ( rc == 0 ) {
break;
}
}
slapi_ch_free( (void **)&tmpPlugin );
return rc;
}
/*
* There is no documentation for this.
*/
int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
{
LDAPRDN lrdn;
LDAPAVA *ava;
int rc;
char *p;
*type = NULL;
bv->bv_len = 0;
bv->bv_val = NULL;
rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
if ( rc != LDAP_SUCCESS ) {
return -1;
}
if ( lrdn[1] != NULL ) {
return -1; /* not single valued */
}
ava = lrdn[0];
*type = slapi_ch_strdup( ava->la_attr.bv_val );
ber_dupbv( bv, &ava->la_value );
ldap_rdnfree(lrdn);
return 0;
}
char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
{
struct berval new_dn, parent_dn, newrdn;
new_dn.bv_val = NULL;
parent_dn.bv_val = (char *)dn;
parent_dn.bv_len = strlen( dn );
newrdn.bv_val = (char *)rdn;
newrdn.bv_len = strlen( rdn );
build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
return new_dn.bv_val;
}
int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
{
Backend *be_orig;
const char *text;
char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
size_t textlen = sizeof textbuf;
int rc = LDAP_SUCCESS;
PBLOCK_ASSERT_OP( pb, 0 );
be_orig = pb->pb_op->o_bd;
pb->pb_op->o_bd = select_backend( &e->e_nname, 0 );
if ( pb->pb_op->o_bd != NULL ) {
rc = entry_schema_check( pb->pb_op, e, NULL, 0, 0, NULL,
&text, textbuf, textlen );
}
pb->pb_op->o_bd = be_orig;
return ( rc == LDAP_SUCCESS ) ? 0 : 1;
}
int slapi_entry_rdn_values_present( const Slapi_Entry *e )
{
LDAPDN dn;
int rc;
int i = 0, match = 0;
rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
if ( rc != LDAP_SUCCESS ) {
return 0;
}
if ( dn[0] != NULL ) {
LDAPRDN rdn = dn[0];
for ( i = 0; rdn[i] != NULL; i++ ) {
LDAPAVA *ava = &rdn[0][i];
Slapi_Attr *a = NULL;
if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
slapi_attr_value_find( a, &ava->la_value ) == 0 )
match++;
}
}
ldap_dnfree( dn );
return ( i == match );
}
int slapi_entry_add_rdn_values( Slapi_Entry *e )
{
LDAPDN dn;
int i, rc;
rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
if ( rc != LDAP_SUCCESS ) {
return rc;
}
if ( dn[0] != NULL ) {
LDAPRDN rdn = dn[0];
struct berval *vals[2];
for ( i = 0; rdn[i] != NULL; i++ ) {
LDAPAVA *ava = &rdn[0][i];
Slapi_Attr *a = NULL;
if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
slapi_attr_value_find( a, &ava->la_value ) == 0 )
continue;
vals[0] = &ava->la_value;
vals[1] = NULL;
slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
}
}
ldap_dnfree( dn );
return LDAP_SUCCESS;
}
const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
{
Attribute *attr;
attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
if ( attr == NULL ) {
return NULL;
}
if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
return slapi_value_get_string( &attr->a_vals[0] );
}
return NULL;
}
void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
{
struct berval bv;
attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
bv.bv_val = uniqueid;
bv.bv_len = strlen( uniqueid );
attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
}
LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
{
LDAP *ld;
char *url;
size_t size;
int rc;
size = sizeof("ldap:///");
if ( secure ) {
size++;
}
size += strlen( ldaphost );
if ( ldapport != 0 ) {
size += 32;
}
url = slapi_ch_malloc( size );
if ( ldapport != 0 ) {
rc = snprintf( url, size, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
} else {
rc = snprintf( url, size, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
}
if ( rc > 0 && (size_t) rc < size ) {
rc = ldap_initialize( &ld, url );
} else {
ld = NULL;
}
slapi_ch_free_string( &url );
return ( rc == LDAP_SUCCESS ) ? ld : NULL;
}
void slapi_ldap_unbind( LDAP *ld )
{
ldap_unbind_ext_s( ld, NULL, NULL );
}
int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
{
if ( be == NULL )
return LDAP_PARAM_ERROR;
*flags = SLAP_DBFLAGS(be);
return LDAP_SUCCESS;
}
int
slapi_int_count_controls( LDAPControl **ctrls )
{
size_t i;
if ( ctrls == NULL )
return 0;
for ( i = 0; ctrls[i] != NULL; i++ )
;
return i;
}
int
slapi_op_abandoned( Slapi_PBlock *pb )
{
if ( pb->pb_op == NULL )
return 0;
return ( pb->pb_op->o_abandon );
}
char *
slapi_op_type_to_string(unsigned long type)
{
char *str;
switch (type) {
case SLAPI_OPERATION_BIND:
str = "bind";
break;
case SLAPI_OPERATION_UNBIND:
str = "unbind";
break;
case SLAPI_OPERATION_SEARCH:
str = "search";
break;
case SLAPI_OPERATION_MODIFY:
str = "modify";
break;
case SLAPI_OPERATION_ADD:
str = "add";
break;
case SLAPI_OPERATION_DELETE:
str = "delete";
break;
case SLAPI_OPERATION_MODDN:
str = "modrdn";
break;
case SLAPI_OPERATION_COMPARE:
str = "compare";
break;
case SLAPI_OPERATION_ABANDON:
str = "abandon";
break;
case SLAPI_OPERATION_EXTENDED:
str = "extended";
break;
default:
str = "unknown operation type";
break;
}
return str;
}
unsigned long
slapi_op_get_type(Slapi_Operation * op)
{
unsigned long type;
switch ( op->o_tag ) {
case LDAP_REQ_BIND:
type = SLAPI_OPERATION_BIND;
break;
case LDAP_REQ_UNBIND:
type = SLAPI_OPERATION_UNBIND;
break;
case LDAP_REQ_SEARCH:
type = SLAPI_OPERATION_SEARCH;
break;
case LDAP_REQ_MODIFY:
type = SLAPI_OPERATION_MODIFY;
break;
case LDAP_REQ_ADD:
type = SLAPI_OPERATION_ADD;
break;
case LDAP_REQ_DELETE:
type = SLAPI_OPERATION_DELETE;
break;
case LDAP_REQ_MODRDN:
type = SLAPI_OPERATION_MODDN;
break;
case LDAP_REQ_COMPARE:
type = SLAPI_OPERATION_COMPARE;
break;
case LDAP_REQ_ABANDON:
type = SLAPI_OPERATION_ABANDON;
break;
case LDAP_REQ_EXTENDED:
type = SLAPI_OPERATION_EXTENDED;
break;
default:
type = SLAPI_OPERATION_NONE;
break;
}
return type;
}
void slapi_be_set_readonly( Slapi_Backend *be, int readonly )
{
if ( be == NULL )
return;
if ( readonly )
be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
else
be->be_restrictops &= ~(SLAP_RESTRICT_OP_WRITES);
}
int slapi_be_get_readonly( Slapi_Backend *be )
{
if ( be == NULL )
return 0;
return ( (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) == SLAP_RESTRICT_OP_WRITES );
}
const char *slapi_x_be_get_updatedn( Slapi_Backend *be )
{
if ( be == NULL )
return NULL;
return be->be_update_ndn.bv_val;
}
Slapi_Backend *slapi_be_select( const Slapi_DN *sdn )
{
Slapi_Backend *be;
slapi_sdn_get_ndn( sdn );
be = select_backend( (struct berval *)&sdn->ndn, 0 );
return be;
}
#if 0
void
slapi_operation_set_flag(Slapi_Operation *op, unsigned long flag)
{
}
void
slapi_operation_clear_flag(Slapi_Operation *op, unsigned long flag)
{
}
int
slapi_operation_is_flag_set(Slapi_Operation *op, unsigned long flag)
{
}
#endif
#endif /* LDAP_SLAPI */
|