summaryrefslogtreecommitdiffstats
path: root/ldap/servers/plugins/acl/acllas.c
blob: 99b0281b26b0a177b33c0bf439bb03142b038b66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
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
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

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

	
#include	<ipfstruct.h>
#include 	"acl.h"

/*
	A word on this file:

	The various routines here implement each component of the subject of an aci
	eg. "groupdn", "userdn","roledn", "userattr" etc.
	They are responsible for evaluating each individual keyword not for doing
	the boolean combination of these keywords, nor for combining multiple 
	allow()/deny() statements--that's libaccess's job.
	For example, for "groupdn", DS_LASGroupDnEval might have to evaluate 
	something like this: 

	"groupdn = "ldap:///cn=G1,o=sun.com || ldap:///cn=G2,o=sun.com"

	The "=" here may be "!=" as well and these routines take care of the
	comparator.

	These rotuines get called via acl__TestRights(), which calls
	ACL_EvalTestRights() a libaccess routine (the immediately calling routine is
	ACLEvalAce() in oneeval.cpp).

	They should return LAS_EVAL_TRUE, if that keyword component evaluates to
	TRUE, LAS_EVAL_FALSE if it evaluates to FALSE and LAS_EVAL_FAIL if an
	error occurrs during evaluation. Note that once any component of a subject
	returns LAS_EVAL_FAIL, the evaluation in libaccess stops and the whole
	subject does not match and that aci is not applied.
*/

/*

	A word on three-valued logic:

	In general when you do boolean combination of terms some of which
	may evaluate to UNDEFINED then you need to define what the combination 
	means.
	So, for example libaccess implements a scheme which once UNDEFINED
	is returned for a term, it bales out of the
	evaluation and the whole expression evaluates to UNDEFINED.
	In this case the aci will not apply.
	On the other hand LDAP filters (cf. rfc2251 4.5.1) say that for OR,
	an expression will
	evaluate to TRUE if any term is TRUE, even if some terms are UNDEFINED. 	
	Other off the cuff options might be to redefine UNDEFINED to be FALSE,
	or TRUE.

	Which is best ? 

	Well it probably depends on exactly what is to decided based on the
	evaluation of the logical expression.  However, the final suggestion is
	almost certainly
	bad--you are unlikely to want to take an action based on an undefined
	result and
	defining UNDEFINED to be either TRUE or FALSE may result in the overall
	expression
	returning TRUE--a security hole.  The only case this might work is if you
	are dealing with restricted
	expressions eg. terms may only be AND'ed togther--in this case defining
	UNDEFINED to be FALSE would guarantee a result of FALSE.

	The libaccess approach of returning UNDEFINED once an UNDEFINED is
	encountered during
	evaluation is not too bad--at least it guarantees that no aci will apply
	based on an 
	undefined value.  However, with an aci like this "...allow(all) A or B"
	where A returned UNDEFINED, you might be disappointed not to receive the
	rights if it was B that
	was granting you the rights and evaluation of A, which has nothing to do
	with you, returns UNDEFINED.  In the case of an aci like 
	"...deny(all) A or B" then the same
	situation is arguably a security hole. Note that this scheme also makes
	the final result
	dependent on the evaluation order and so if the evaluation engine does
	anything fancy internally (eg. reordering the terms in an OR so that fast
	to evaluate ones came first) then
	this would need to be documented so that a user (or a tool) could look at
	the external syntax and figure out the result of the evaluation.
	Also it breaks commutivity and De Morgans law.

	The LDAP filter scheme is starting to look good--it solves the problems of
	the
	libaccess approach, makes the final result of an expression independent of
	the evaluation order and
	gives you back commutivity of OR and AND. De Morgans is still broken, but
	that's because of the asymmetry of behaviour of UNDEFINED with OR and AND.
	
	So...?

	For acis, in general it can look like this:

	"...allow(rights)(LogicalCombinationofBindRule);
		deny(LogicalCombinationOfBindRule)...." 

	A BindRule is one of the "userdn", "groupdn" or "userattr" things and it
	can look like this:

	"groupdn = "ldap:///cn=G1,o=sun.com || ldap:///cn=G2,o=sun.com"

    The "=" here may be "!=" as well and these routines take care of the
	comparator.

	For "userattr" keywords a mutilvalued attribute amounts a logical OR of the
	individual values.  There is also a logical OR over the different levels
	as specified by the "parent" keyword. 
	
	In fact there are three levels of logical combination:
	
	1.	In the aclplugin:
		The "||" and "!=" combinator for BindRule keywords like userdn and
		groupdn.
		The fact that for the "userattr" keyword, a mutilvalued attribute is
		evaluated as "||".  Same for the different levels.
	2.	In libaccess:
		The logical combination of BindRules.
	3.	In libaccess:
		The evaluation of multiple BindRules seperated by ";", which means OR.		

	The LDAP filter three-valued logic SHOULD be applied to each level but
	here's the way it works right now:

	1.  At this level it depends....

		DS_LASIpGetter			- get attr for IP -
			returns ip address or LAS_EVAL_FAIL for error.
			no logical combination.
		DS_LASDnsGetter			- get attr for DNS-
			 returns dns name or LAS_EVAL_FAIL for error
			no logical combination.
		DS_LASUserDnEval		- LAS Evaluation for USERDN		- 	
			three-valued logic
			logical combination: || and !=
		DS_LASGroupDnEval		- LAS Evaluation for GROUPDN	-
			three-valued logic
			logical combination: || and !=
		DS_LASRoleDnEval		- LAS Evaluation for ROLEDN		-
			three-valued logic
			logical combination: || and !=
		DS_LASUserDnAttrEval	- LAS Evaluation for USERDNATTR -
			three-valued logic  
			logical combination || (over specified attribute values and
			parent keyword levels), !=
		DS_LASAuthMethodEval	- LAS Evaluation for AUTHMETHOD -
			three-valued logic ( logical combinations: !=)
		DS_LASGroupDnAttrEval	- LAS Evaluation for GROUPDNATTR -
			three-valued logic  
			logical combination || (over specified attribute values and
			parent keyword levels), !=
		DS_LASUserAttrEval		- LAS Evaluation for USERATTR -
			USER, GROUPDN and ROLEDN as above.
			LDAPURL -- three-valued logic (logical combinations: || over
						specified attribute vales, !=)
			attrname#attrvalue -- three-valued logic, logical combination:!=

	2. The libaccess scheme applies at this level.
	3. The LDAP filter three-valued logic applies at this level.

	Example of realistic, non-bizarre things that cause evaluation of a
	BindRule to be undefined are exceeding some resource limits (nesting level,
	lookthrough limit) in group membership evaluation, or trying to get ADD
	permission from the "userattr" keyword at "parent" level 0.	
	Note that not everything that might be construed as an error needs to be
	taken as UNDEFINED.  For example, things like not finding a user or an
	attribute in an entry can be defined away as TRUE or FALSE.  eg. in an
	LDAP filter (cn=rob) applied to an entry where cn is not present is FALSE,
	not UNDEFINED.  Similarly, if the number of levels in a parent keyword
	exceeds the allowed limit, we just ignore the rest--though this
	is a syntax error which should be detected at parse time.
	

*/

/* To get around warning: declared in ldapserver/lib/ldaputil/ldaputili.h */
extern int ldapu_member_certificate_match (void* cert, const char* desc);

/****************************************************************************/
/* Defines, Constants, ande Declarations                                    */
/****************************************************************************/
static char* const   	type_objectClass = "objectclass";
static char* const 	filter_groups = "(|(objectclass=groupOfNames) (objectclass=groupOfUniqueNames)(objectclass=groupOfCertificates)(objectclass=groupOfURLs))";
static char* const	type_member = "member";
static char* const	type_uniquemember = "uniquemember";
static char* const	type_memberURL = "memberURL";
static char* const	type_memberCert = "memberCertificateDescription";

/* cache strategy for groups */
#define ACLLAS_CACHE_MEMBER_GROUPS		0x1
#define ACLLAS_CACHE_NOT_MEMBER_GROUPS	0x2
#define ACLLAS_CACHE_ALL_GROUPS			0x3

/****************************************************************************/
/* prototypes                                                               */
/****************************************************************************/
static int		acllas__handle_group_entry(Slapi_Entry *, void *);
static int 		acllas__user_ismember_of_group(struct acl_pblock *aclpb,
							char* groupDN,
							char* clientDN,
							int   cache_status,
							CERTCertificate *clientCert);
static int acllas__user_has_role( struct acl_pblock *aclpb,
									Slapi_DN *roleDN, Slapi_DN *clientDn);
static int 		acllas__add_allgroups (Slapi_Entry* e, void *callback_data);
static int 		acllas__eval_memberGroupDnAttr (char *attrName, 
							Slapi_Entry *e,
							char *n_clientdn, 
							struct acl_pblock *aclpb);
static int 		acllas__verify_client (Slapi_Entry* e, void *callback_data);
static int 		acllas__verify_ldapurl (Slapi_Entry* e, void *callback_data);
static char* 		acllas__dn_parent( char *dn, int level);
static int 		acllas__get_members (Slapi_Entry* e, void *callback_data);
static int 		acllas__client_match_URL (struct acl_pblock *aclpb,
						   char *n_dn, char *url );
static int 		acllas__handle_client_search (Slapi_Entry *e, void *callback_data);
static int 		__acllas_setup ( NSErr_t *errp, char *attr_name, CmpOp_t comparator, int allow_range,
						char *attr_pattern, int *cachable, void **LAS_cookie,
        				PList_t subject, PList_t resource, PList_t auth_info,
        				PList_t global_auth, char *lasType, char *lasName, lasInfo *linfo);
int
aclutil_evaluate_macro( char * user, lasInfo *lasinfo,
						acl_eval_types evalType );
static int
acllas_eval_one_user( struct acl_pblock *aclpb,
						char * clientDN, char *userKeyword);
static int
acllas_eval_one_group(char *group, lasInfo *lasinfo);
static int
acllas_eval_one_role(char *role, lasInfo *lasinfo);
static char **
acllas_replace_dn_macro( char *rule, char *matched_val, lasInfo *lasinfo);
static char **
acllas_replace_attr_macro( char *rule, lasInfo *lasinfo);
static int 
acllas_eval_one_target_filter( char * str, Slapi_Entry *e);

/****************************************************************************/

int
DS_LASIpGetter(NSErr_t *errp, PList_t subject, PList_t resource, PList_t
 		auth_info, PList_t global_auth, void *arg)
{

	struct acl_pblock	*aclpb = NULL;
	IPAddr_t        	ip=0;
	PRNetAddr		client_praddr;
	struct in_addr		client_addr;
	int			rv;


	rv = ACL_GetAttribute(errp, DS_PROP_ACLPB, (void **)&aclpb,
				subject, resource, auth_info, global_auth);
	if ( rv != LAS_EVAL_TRUE  || ( NULL == aclpb )) {
		acl_print_acllib_err(errp, NULL);
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"DS_LASIpGetter:Unable to get the ACLPB(%d)\n", rv);
		return LAS_EVAL_FAIL;
	}

	if ( slapi_pblock_get( aclpb->aclpb_pblock, SLAPI_CONN_CLIENTNETADDR,
														&client_praddr ) != 0 ) {
                slapi_log_error( SLAPI_LOG_FATAL, plugin_name, "Could not get client IP.\n" );
                return( LAS_EVAL_FAIL );
        }

	if ( !PR_IsNetAddrType(&client_praddr, PR_IpAddrV4Mapped) ) {
	        slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
				 "Client address is IPv6. ACLs only support IPv4 addresses so far.\n");
		return( LAS_EVAL_FAIL );
	}
	        	
	client_addr.s_addr = client_praddr.ipv6.ip.pr_s6_addr32[3];

	ip = (IPAddr_t) ntohl( client_addr.s_addr );
	rv = PListInitProp(subject, 0, ACL_ATTR_IP, (void *)ip, NULL);
	
	slapi_log_error( SLAPI_LOG_ACL, plugin_name,
        "Returning client ip address '%s'\n",
        (slapi_is_loglevel_set(SLAPI_LOG_ACL) ?  inet_ntoa(client_addr) : ""));

	return LAS_EVAL_TRUE;

}

/* 
 * This is called from the libaccess code when it needs to find a dns name.
 * It's called from ACL_GetAttribute() when it finds that ACL_ATTR_DNS is
 * not already part of the proplist.
 * 
*/

int 
DS_LASDnsGetter(NSErr_t *errp, PList_t subject, PList_t resource, PList_t
           auth_info, PList_t global_auth, void *arg)
{
	struct acl_pblock	*aclpb = NULL;
	PRNetAddr		client_praddr;
	PRHostEnt		*hp;
	char			*dnsName = NULL;
	int			rv;
	struct berval		**clientDns;


	rv = ACL_GetAttribute(errp, DS_PROP_ACLPB, (void **)&aclpb,
				subject, resource, auth_info, global_auth);
	if ( rv != LAS_EVAL_TRUE  || ( NULL == aclpb )) {
		acl_print_acllib_err(errp, NULL);
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"DS_LASDnsGetter:Unable to get the ACLPB(%d)\n", rv);
		return LAS_EVAL_FAIL;
	}
	
	if ( slapi_pblock_get( aclpb->aclpb_pblock, SLAPI_CLIENT_DNS, &clientDns ) != 0 ) {
                slapi_log_error( SLAPI_LOG_FATAL, plugin_name, "Could not get client IP.\n" );
                return( LAS_EVAL_FAIL );
	}

	/*
	 * If the client hostname has already been put into the pblock then
	 * use that.  Otherwise we work it out and add it ourselves.
	 * This info is connection-lifetime so with multiple operaitons on the same
	 * connection we will only do the calculation once.
	 *
	 * rbyrneXXX surely this code would be better in connection.c so
	 * the name would be just there waiting for us, and everyone else.
	 *
	*/

	if ( clientDns && clientDns[0] != NULL && clientDns[0]->bv_val ) {
		dnsName = clientDns[0]->bv_val;
	} else {
		struct	berval		**dnsList;
		char    		buf[PR_NETDB_BUF_SIZE];

		if ( slapi_pblock_get( aclpb->aclpb_pblock, SLAPI_CONN_CLIENTNETADDR, &client_praddr ) != 0 ) {

				slapi_log_error( SLAPI_LOG_FATAL, plugin_name, "Could not get client IP.\n" );
               return( LAS_EVAL_FAIL );
        	}
		hp = (PRHostEnt *)slapi_ch_malloc( sizeof(PRHostEnt) );
		if ( PR_GetHostByAddr( &(client_praddr), (char *)buf, sizeof(buf), hp ) == PR_SUCCESS ) {
			if ( hp->h_name != NULL ) {
				dnsList = (struct berval**) 
						slapi_ch_calloc (1, sizeof(struct berval*) * (1 + 1));
				*dnsList = (struct berval*) 
						slapi_ch_calloc ( 1, sizeof(struct berval));				
                dnsName = (*dnsList)->bv_val = slapi_ch_strdup( hp->h_name );
				(*dnsList)->bv_len = strlen ( (*dnsList)->bv_val );
				slapi_pblock_set( aclpb->aclpb_pblock, SLAPI_CLIENT_DNS, &dnsList );
			}
		} 
		slapi_ch_free( (void **)&hp );
	}

	if ( NULL == dnsName ) return LAS_EVAL_FAIL;

	rv = PListInitProp(subject, 0, ACL_ATTR_DNS, dnsName, NULL);
        if (rv < 0) {
                slapi_log_error ( SLAPI_LOG_ACL, plugin_name, 
				"DS_LASDnsGetter:Couldn't set the DNS property(%d)\n", rv );
                return LAS_EVAL_FAIL;
        }
	slapi_log_error ( SLAPI_LOG_ACL, plugin_name, "DNS name: %s\n", dnsName );
	return LAS_EVAL_TRUE;

}
/***************************************************************************/
/* New LASes								   */
/* 									   */
/* 1. user, groups. -- stubs to report errors. Not supported.		   */
/* 2. userdn								   */
/* 3. groupdn								   */
/* 4. userdnattr							   */
/* 5. authmethod							   */
/* 6. groupdnattr							   */
/* 7. roledn							*/
/* 									   */
/* 									   */
/***************************************************************************/
int 
DS_LASUserEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, 
		char *attr_pattern, int *cachable, void **LAS_cookie, 
		PList_t subject, PList_t resource, PList_t auth_info,
		PList_t global_auth)
{
	slapi_log_error(SLAPI_LOG_FATAL, plugin_name, 
			"User LAS is not supported in the ACL\n");

	return LAS_EVAL_INVALID;
}

int 
DS_LASGroupEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, 
		char *attr_pattern, int *cachable, void **LAS_cookie, 
		PList_t subject, PList_t resource, PList_t auth_info,
		PList_t global_auth)
{
	slapi_log_error(SLAPI_LOG_FATAL, plugin_name, 
			"Group LAS is not supported in the ACL\n");

	return LAS_EVAL_INVALID;
}

/***************************************************************************
*
* DS_LASUserDnEval
*	Evaluate the "userdn" LAS. See if the user has rights.
*
* Input:
*	attr_name	The string "userdn" - in lower case.
*	comparator	CMP_OP_EQ or CMP_OP_NE only
*	attr_pattern	A comma-separated list of users
*	cachable	Always set to FALSE.
*	subject		Subject property list
*	resource        Resource property list
*	auth_info	Authentication info, if any
*
* Returns:
*	retcode	        The usual LAS return codes.
*
* Error Handling:
*	None.
*
**************************************************************************/
int 
DS_LASUserDnEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, 
		char *attr_pattern, int *cachable, void **LAS_cookie, 
		PList_t subject, PList_t resource, PList_t auth_info,
		PList_t global_auth)
{

	char		*users = NULL;
	char		*s_user, *user = NULL;
	char		*ptr = NULL;
	char		*end_dn = NULL;
	char		*n_edn = NULL;
	char		*parent_dn = NULL;
	int			matched;
	int			rc;
	short		len;
	const size_t 	LDAP_URL_prefix_len = strlen(LDAP_URL_prefix);
	const size_t 	LDAPS_URL_prefix_len = strlen(LDAPS_URL_prefix);
	lasInfo			lasinfo;
	int			got_undefined = 0;

	if ( 0 !=  (rc = __acllas_setup (errp, attr_name, comparator, 0, /* Don't allow range comparators */
									attr_pattern,cachable,LAS_cookie,
									subject, resource, auth_info,global_auth,
									DS_LAS_USERDN, "DS_LASUserDnEval", &lasinfo )) ) {
		return LAS_EVAL_FAIL;
	}

 	users = slapi_ch_strdup(attr_pattern);
	user = users;
	matched = ACL_FALSE;

	/* check if the clientdn is one of the users */
	while(user != 0 && *user != 0 && matched != ACL_TRUE ) {

		/* ignore leading whitespace */
		while(ldap_utf8isspace(user)) 
			LDAP_UTF8INC(user);

		/* Now we must see the userdn in the following
		** formats:
		**
		** The following formats are supported:
		** 
		**  1. The DN itself: 
		**    	allow (read)  userdn = "ldap:///cn=prasanta, ..." 
		**
		**  2. keyword SELF: 
		**    	allow (write)  
		**         userdn = "ldap:///self"
		**
		**  3. Pattern: 
		**    	deny (read) userdn = "ldap:///cn=*, o=netscape, c = us";
		**
		**  4. Anonymous user
		**    	deny (read, write)    userdn = "ldap:///anyone"
		**
		**  5. All users (All authenticated users)
		**    	allow (search)  **   	   userdn = "ldap:///all"
		**  6. parent "ldap:///parent"
		**  7. Synamic users using the URL
		** 
		**
		** DNs must be separated by "||". Ex:
		** allow (read)
		** userdn = "ldap:///DN1 || ldap:///DN2" 
		*/
	
		/* The DN is now "ldap:///DN" 
		** remove the "ldap:///" part
		*/
		if (strncasecmp (user, LDAP_URL_prefix, LDAP_URL_prefix_len) == 0) {
			s_user = user;
			user += LDAP_URL_prefix_len;
		} else if (strncasecmp (user, LDAPS_URL_prefix, LDAPS_URL_prefix_len) == 0) {
			s_user = user;
			user += LDAPS_URL_prefix_len;
		} else {
			char ebuf[ BUFSIZ ];
			slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
			 	"DS_LASUserDnEval:Syntax error(%s)\n", 
				 escape_string_with_punctuation( user, ebuf  ));
			return LAS_EVAL_FAIL;
		}

		/* Now we have the starting point of the "userdn" */
		if ((end_dn = strstr(user, "||")) != NULL) {
			auto char *t = end_dn;
			LDAP_UTF8INC(end_dn);
			LDAP_UTF8INC(end_dn);
			*t = 0;
		}

		/* Now user is a null terminated string */

		if (*user) {
			while(ldap_utf8isspace(user)) 
				LDAP_UTF8INC(user);
			/* ignore trailing whitespace */
			len = strlen(user);
			ptr = user+len-1;
			while(ptr >= user && ldap_utf8isspace(ptr)) {
				*ptr = '\0';
				LDAP_UTF8DEC(ptr);
			}
		}

		/* 
		** Check , if the user is a anonymous user. In that case
		** We must find the rule "ldap:///anyone"
		*/
		if (lasinfo.anomUser) {
			if (strcasecmp(user, "anyone") == 0 ) {
				/* matches  -- anonymous user */
				matched = ACL_TRUE;
				break;
			} 
		} else {
			/* URL format */
			
			if ((strstr (user, ACL_RULE_MACRO_DN_KEY) != NULL) ||
				(strstr (user, ACL_RULE_MACRO_DN_LEVELS_KEY) != NULL) ||
				(strstr (user, ACL_RULE_MACRO_ATTR_KEY) != NULL)) {			
				
				matched = aclutil_evaluate_macro( s_user, &lasinfo,
													ACL_EVAL_USER);
				if (matched == ACL_TRUE) {
					break;
				}										
				
			} else if (strchr (user, '?') != NULL) {
				/* URL format */
				if (acllas__client_match_URL ( lasinfo.aclpb, lasinfo.clientDn, 
							     s_user) == ACL_TRUE) {
					matched = ACL_TRUE;
					break;
				}
			} else if (strcasecmp(user, "anyone") == 0 ) {
				/* Anyone means anyone in the world */
				matched = ACL_TRUE;
				break;
			} else if (strcasecmp(user, "self") == 0) {
				if (n_edn == NULL) {
					n_edn = slapi_entry_get_ndn (  lasinfo.resourceEntry );
				}
				if (slapi_utf8casecmp((ACLUCHP)lasinfo.clientDn, (ACLUCHP)n_edn) == 0)
					matched = ACL_TRUE;
				break;
			} else if (strcasecmp(user, "parent") == 0) {
				if (n_edn == NULL) {
					n_edn = slapi_entry_get_ndn ( lasinfo.resourceEntry );
				}
				/* get the parent */
				parent_dn = slapi_dn_parent(n_edn);
				if (parent_dn && 
					slapi_utf8casecmp ((ACLUCHP)lasinfo.clientDn, (ACLUCHP)parent_dn) == 0)
					matched = ACL_TRUE;

				if (parent_dn) slapi_ch_free ( (void **) &parent_dn );
				break;
			} else if (strcasecmp(user, "all") == 0) {
				/* matches  -- */
				matched = ACL_TRUE;
				break;
			} else if (strchr(user, '*')) {
				char	line[200];
				char *lineptr = &line[0];
				char *newline = NULL;
				int lenu = 0;
				Slapi_Filter	*f = NULL;
				char		*tt;
				int		filterChoice;

				/* 
				** what we are doing is faking the str2simple()
				** function with a "userdn = "user")
				*/
				for (tt = user; *tt; tt++)
					*tt = TOLOWER ( *tt );

				if ((lenu = strlen(user)) > 190) { /* 200 - 9 for "(userdn=%s)" */
					newline = slapi_ch_malloc(lenu + 10);
					lineptr = newline;
				}

				sprintf (lineptr, "(userdn=%s)", user);
				if ((f = slapi_str2filter (lineptr)) == NULL) {
					if (newline) slapi_ch_free((void **) &newline);
					/* try the next one */
					break;
				}
				if (newline) slapi_ch_free((void **) &newline);
				filterChoice = slapi_filter_get_choice ( f );

				if (( filterChoice != LDAP_FILTER_SUBSTRINGS) &&
			    		( filterChoice != LDAP_FILTER_PRESENT)) {
			   		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			    			 "DS_LASUserDnEval:Error in gen. filter(%s)\n", user);
				}
				if ((rc = acl_match_substring( f, 
							       lasinfo.clientDn,
							       1 /*exact match */)
							     ) == ACL_TRUE) {
					matched = ACL_TRUE;
					slapi_filter_free(f,1);
					break;
				}
				if (rc == ACL_ERR) {
			   		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			    			"DS_LASUserDnEval:Error in matching patteren(%s)\n",
			     			user);
				}
				slapi_filter_free(f,1);
			} else {
				/* Must be a simple dn then */
				char *normed = NULL;
				size_t dnlen = 0;
				rc = slapi_dn_normalize_ext(user, 0, &normed, &dnlen);
				if (rc == 0) { /* user passed in; not terminated */
					*(normed + dnlen) = '\0';
				} else if (rc < 0) { /* normalization failed, user the original */
					normed = user;
				}
				rc = slapi_utf8casecmp((ACLUCHP)lasinfo.clientDn, (ACLUCHP)normed);
				if (normed != user) {
					slapi_ch_free_string(&normed);
				}
				if (0 == rc) {
					matched = ACL_TRUE;
					break;
				}
			}
		}

		if ( matched == ACL_DONT_KNOW ) {
			/* record this but keep going--maybe another user will evaluate to TRUE */
			got_undefined = 1;
		}
		/* Nothing matched -- try the next DN */
		user = end_dn;
	} /* end of while */

	slapi_ch_free ( (void **) &users);

	/*
	 * If no terms were undefined, then evaluate as normal.
	 * If there was an undefined term, but another one was TRUE, then we also evaluate
	 * as normal.  Otherwise, the whole expression is UNDEFINED.
	*/
	if ( matched == ACL_TRUE || !got_undefined ) {
		if (comparator == CMP_OP_EQ) {
			rc = (matched == ACL_TRUE  ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
		} else {
			rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
		}
	} else {
		rc = LAS_EVAL_FAIL;
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			"Returning UNDEFINED for userdn evaluation.\n");
	} 

	return rc;
}

/***************************************************************************
*
* DS_LASGroupDnEval
*
*
* Input:
*	attr_name	The string "userdn" - in lower case.
*	comparator	CMP_OP_EQ or CMP_OP_NE only
*	attr_pattern	A comma-separated list of users
*	cachable	Always set to FALSE.
*	subject		Subject property list
*	resource        Resource property list
*	auth_info	Authentication info, if any
*
* Returns:
*	retcode		The usual LAS return code
*			If the client is in any of the groups mentioned this groupdn keywrod
*			then returns LAS_EVAL_TRUE, if he's not in any LAS_EVAL_FALSE.
*			If any of the membership evaluations fail, then it goes on to evaluate the
*			others.
*
* Error Handling:
*	None.
*
**************************************************************************/
int 
DS_LASGroupDnEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, 
		char *attr_pattern, int *cachable, void **LAS_cookie, 
		PList_t subject, PList_t resource, PList_t auth_info,
		PList_t global_auth)
{

	char			*groups;
	char			*groupNameOrig;
	char			*groupName;
	char			*ptr;
	char			*end_dn;
	int				matched;
	int				rc;
	int				len;
	const size_t 	LDAP_URL_prefix_len = strlen(LDAP_URL_prefix);
	int				any_group = 0;
	lasInfo			lasinfo;
	int 			got_undefined = 0;

	/* the setup should not fail under normal operation */
	if ( 0 !=  (rc = __acllas_setup (errp, attr_name, comparator, 0, /* Don't allow range comparators */
									attr_pattern,cachable,LAS_cookie,
									subject, resource, auth_info,global_auth,
									DS_LAS_GROUPDN, "DS_LASGroupDnEval", &lasinfo )) ) {
		return LAS_EVAL_FAIL;
	}

 	groups = slapi_ch_strdup(attr_pattern);
	groupNameOrig = groupName = groups;
	matched = ACL_FALSE;

	/* check if the groupdn is one of the users */
	while(groupName != 0 && *groupName != 0 && matched != ACL_TRUE) {

		/* ignore leading whitespace */
		while(ldap_utf8isspace(groupName)) 
			LDAP_UTF8INC(groupName);

		/* 
		** The syntax allowed for the groupdn is
		**
		** Example:
		**  groupdn = "ldap:///dn1 ||  ldap:///dn2";
		**
		*/
	
		if (strncasecmp (groupName, LDAP_URL_prefix,
				 LDAP_URL_prefix_len) == 0) {
			groupName += LDAP_URL_prefix_len;
		} else {
			char ebuf[ BUFSIZ ];
			slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
				  "DS_LASGroupDnEval:Syntax error(%s)\n",
				   escape_string_with_punctuation( groupName, ebuf ));
		}
	
		/* Now we have the starting point of the "groupdn" */
		if ((end_dn = strstr(groupName, "||")) != NULL) {
			auto char *t = end_dn;
			LDAP_UTF8INC(end_dn);
			LDAP_UTF8INC(end_dn);
			/* removing trailing spaces */
			LDAP_UTF8DEC(t);
			while (' ' == *t || '\t' == *t) {
				LDAP_UTF8DEC(t);
			}
			LDAP_UTF8INC(t);
			*t = '\0';
			/* removing beginning spaces */
			while (' ' == *end_dn || '\t' == *end_dn) {
				LDAP_UTF8INC(end_dn);
			}
		}

		if (*groupName) {
			while(ldap_utf8isspace(groupName)) 
				LDAP_UTF8INC(groupName);
			/* ignore trailing whitespace */
			len = strlen(groupName);
			ptr = groupName+len-1;
			while(ptr >= groupName && ldap_utf8isspace(ptr)) {
				*ptr = '\0';
				LDAP_UTF8DEC(ptr);
			}
		}

		/* 
		** Now we have the DN of the group. Evaluate the "clientdn"
		** and see if the user is a member of the group.
		*/
		if (0 == (strcasecmp(groupName, "anyone"))) {
			any_group = 1;
		}

		if (any_group) {
			/* anyone in the world */
			matched = ACL_TRUE;
			break;
		} else if ( lasinfo.anomUser && 
				(lasinfo.aclpb->aclpb_clientcert == NULL) && (!any_group)) {
			slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
					"Group not evaluated(%s)\n", groupName);
			break;
		} else {			
			if ((strstr (groupName, ACL_RULE_MACRO_DN_KEY) != NULL) ||
				(strstr (groupName, ACL_RULE_MACRO_DN_LEVELS_KEY) != NULL) ||
				(strstr (groupName, ACL_RULE_MACRO_ATTR_KEY) != NULL)) {			
				matched = aclutil_evaluate_macro( groupName, &lasinfo,
													ACL_EVAL_GROUP);
				slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
						"DS_LASGroupDnEval: Param group name:%s\n",
						groupName);
			} else {
				LDAPURLDesc		*ludp = NULL;
				int             urlerr = 0;
				int				rval;
				Slapi_PBlock	*myPb = NULL;
				Slapi_Entry		**grpentries = NULL;

				/* Groupdn is full ldapurl? */
				if ((0 == (urlerr = slapi_ldap_url_parse(groupNameOrig, &ludp, 0, NULL))) &&
				    NULL != ludp->lud_dn &&
					-1 != ludp->lud_scope &&
					NULL != ludp->lud_filter) {
					/* Yes, it is full ldapurl; Let's run the search */
					myPb = slapi_pblock_new ();
					slapi_search_internal_set_pb(
								myPb,
								ludp->lud_dn,
								ludp->lud_scope,
								ludp->lud_filter,
								NULL,
								0,
								NULL /* controls */,
								NULL /* uniqueid */,
								aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
								0 );	
					slapi_search_internal_pb(myPb);
					slapi_pblock_get(myPb, SLAPI_PLUGIN_INTOP_RESULT, &rval);
					if (rval == LDAP_SUCCESS) {
						Slapi_Entry		**ep;
						slapi_pblock_get(myPb,
								SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &grpentries);
						if ((grpentries != NULL) && (grpentries[0] != NULL)) {
							char *edn = NULL;
							for (ep = grpentries; *ep; ep++) {
								/* groups having ACI */
								edn = slapi_entry_get_ndn(*ep);
								matched = acllas_eval_one_group(edn, &lasinfo);
								if (ACL_TRUE == matched) {
									break; /* matched ! */
								}
							}
						}
					}
					slapi_free_search_results_internal(myPb);
					slapi_pblock_destroy (myPb);
	
				} else {
					if (urlerr) {
						slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
										  "DS_LASGroupDnEval: Groupname [%s] not a valid ldap url: %d (%s)\n",
										  groupNameOrig, urlerr, slapi_urlparse_err2string(urlerr));
					}
					/* normal evaluation */
					matched = acllas_eval_one_group( groupName, &lasinfo );
				}
				if ( ludp ) {
					ldap_free_urldesc( ludp );
				}
			}
			
			if ( matched == ACL_TRUE ) {
				break;
			} else if ( matched == ACL_DONT_KNOW ) {
				/* record this but keep going--maybe another group will evaluate to TRUE */
				got_undefined = 1;
			}
		}
		/* Nothing matched -- try the next DN */
		groupNameOrig = groupName = end_dn;

	} /* end of while */

	/*
	 * If no terms were undefined, then evaluate as normal.
	 * If there was an undefined term, but another one was TRUE, then we also evaluate
	 * as normal.  Otherwise, the whole expression is UNDEFINED.
	*/
	if ( matched == ACL_TRUE || !got_undefined ) {
		if (comparator == CMP_OP_EQ) {
			rc = (matched == ACL_TRUE  ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
		} else {
			rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
		}
	} else {
		rc = LAS_EVAL_FAIL;
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			"Returning UNDEFINED for groupdn evaluation.\n");
	} 

	slapi_ch_free ((void**) &groups);
	return rc;
}
/***************************************************************************
*
* DS_LASRoleDnEval
*
*
* Input:
*	attr_name	The string "roledn" - in lower case.
*	comparator	CMP_OP_EQ or CMP_OP_NE only
*	attr_pattern	A "||" sperated list of roles
*	cachable	Always set to FALSE.
*	subject		Subject property list
*	resource        Resource property list
*	auth_info	Authentication info, if any
*
* Returns:
*	retcode	        The usual LAS return codes.
*
* Error Handling:
*	None.
*
**************************************************************************/
int 
DS_LASRoleDnEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, 
		char *attr_pattern, int *cachable, void **LAS_cookie, 
		PList_t subject, PList_t resource, PList_t auth_info,
		PList_t global_auth)
{

	char			*roles;
	char			*role;
	char			*ptr;
	char			*end_dn;
	int				matched;
	int				rc;
	int				len;
	const size_t 	LDAP_URL_prefix_len = strlen(LDAP_URL_prefix);
	int				any_role = 0;
	lasInfo			lasinfo;
	int				got_undefined = 0;

	if ( 0 !=  (rc = __acllas_setup (errp, attr_name, comparator, 0, /* Don't allow range comparators */
									attr_pattern,cachable,LAS_cookie,
									subject, resource, auth_info,global_auth,
									DS_LAS_ROLEDN, "DS_LASRoleDnEval",
									&lasinfo )) ) {
		return LAS_EVAL_FALSE;
	}


 	roles = slapi_ch_strdup(attr_pattern);
	role = roles;
	matched = ACL_FALSE;

	/* check if the roledn is one of the users */
	while(role != 0 && *role != 0 && matched != ACL_TRUE) {

		/* ignore leading whitespace */
		while(ldap_utf8isspace(role)) 
			LDAP_UTF8INC(role);

		/* 
		** The syntax allowed for the roledn is
		**
		** Example:
		**  roledn = "ldap:///roledn1 ||  ldap:///roledn2";
		**
		*/
	
		if (strncasecmp (role, LDAP_URL_prefix,
				 LDAP_URL_prefix_len) == 0) {
			role += LDAP_URL_prefix_len;
		} else {
			char ebuf[ BUFSIZ ];
			slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
				  "DS_LASRoleDnEval:Syntax error(%s)\n",
				   escape_string_with_punctuation( role, ebuf ));
		}
	
		/* Now we have the starting point of the "roledn" */
		if ((end_dn = strstr(role, "||")) != NULL) {
			auto char *t = end_dn;
			LDAP_UTF8INC(end_dn);
			LDAP_UTF8INC(end_dn);
			*t = 0;
		}


		if (*role) {
			while(ldap_utf8isspace(role)) 
				LDAP_UTF8INC(role);
			/* ignore trailing whitespace */
			len = strlen(role);
			ptr = role+len-1;
			while(ptr >= role && ldap_utf8isspace(ptr)) {
				*ptr = '\0';
				LDAP_UTF8DEC(ptr);
			}
		}

		/* 
		** Now we have the DN of the role. Evaluate the "clientdn"
		** and see if the user has this role.
		*/
		if (0 == (strcasecmp(role, "anyone"))) {
			any_role = 1;
		}

		if (any_role) {
			/* anyone in the world */
			matched = ACL_TRUE;
			break;
		} else if ( lasinfo.anomUser && 
				(lasinfo.aclpb->aclpb_clientcert == NULL) && (!any_role)) {
			slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
					"Role not evaluated(%s) for anon user\n", role);
			break;
		} else {

			/* Take care of param strings */
			if ((strstr (role, ACL_RULE_MACRO_DN_KEY) != NULL) ||
				(strstr (role, ACL_RULE_MACRO_DN_LEVELS_KEY) != NULL) ||
				(strstr (role, ACL_RULE_MACRO_ATTR_KEY) != NULL)) {			
				
				matched = aclutil_evaluate_macro( role, &lasinfo,
													ACL_EVAL_ROLE);
				slapi_log_error ( SLAPI_LOG_ACL, plugin_name,
						"DS_LASRoleDnEval: Param role name:%s\n",
						role);
			} else {/* normal evaluation */

				matched = acllas_eval_one_role( role, &lasinfo);

			}
			
			if ( matched == ACL_TRUE ) {
				break;
			} else if ( matched == ACL_DONT_KNOW ) {
				/* record this but keep going--maybe another role will evaluate to TRUE */
				got_undefined = 1;
			}
		}
		/* Nothing matched -- try the next DN */
		role = end_dn;

	} /* end of while */

	/*
	 * If no terms were undefined, then evaluate as normal.
	 * If there was an undefined term, but another one was TRUE, then we also evaluate
	 * as normal.  Otherwise, the whole expression is UNDEFINED.
	*/
	if ( matched == ACL_TRUE || !got_undefined ) {
		if (comparator == CMP_OP_EQ) {
			rc = (matched == ACL_TRUE  ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
		} else {
			rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
		}
	} else {
		rc = LAS_EVAL_FAIL;
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			"Returning UNDEFINED for roledn evaluation.\n");
	} 

	slapi_ch_free ((void**) &roles);
	return rc;
}
/***************************************************************************
*
* DS_LASUserDnAttrEval
*
*
* Input:
*	attr_name	The string "userdn" - in lower case.
*	comparator	CMP_OP_EQ or CMP_OP_NE only
*	attr_pattern	A comma-separated list of users
*	cachable	Always set to FALSE.
*	subject		Subject property list
*	resource        Resource property list
*	auth_info	Authentication info, if any
*
* Returns:
*	retcode	        The usual LAS return codes.
*
* Error Handling:
*	None.
*
**************************************************************************/
struct userdnattr_info {
	char	*attr;
	int	result;
	char	*clientdn;
	Acl_PBlock	*aclpb;
};
#define ACLLAS_MAX_LEVELS 10
int 
DS_LASUserDnAttrEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, 
		char *attr_pattern, int *cachable, void **LAS_cookie, 
		PList_t subject, PList_t resource, PList_t auth_info,
		PList_t global_auth)
{

	char			*n_currEntryDn = NULL;
	char			*s_attrName, *attrName;
	char			*ptr;
	int				matched;
	int				rc, len, i;
	char			*val;
	Slapi_Attr 		*a;
	int				levels[ACLLAS_MAX_LEVELS];
	int				numOflevels =0;
	struct userdnattr_info	info = {0};
	char			*attrs[2] = { LDAP_ALL_USER_ATTRS, NULL };
	lasInfo			lasinfo;
	int				got_undefined = 0;

	if ( 0 !=  (rc = __acllas_setup (errp, attr_name, comparator, 0, /* Don't allow range comparators */
									attr_pattern,cachable,LAS_cookie,
									subject, resource, auth_info,global_auth,
									DS_LAS_USERDNATTR, "DS_LASUserDnAttrEval", 
									&lasinfo )) ) {
		return LAS_EVAL_FAIL;
	}

	/* 
	** The userdnAttr syntax is
	** 	userdnattr = <attribute> or
	**	userdnattr = parent[0,2,4].attribute"
	**  Ex:
	**	userdnattr = manager; or
	**	userdnattr = "parent[0,2,4].manager";
	**
	**  Here 0 means current level, 2 means grandfather and 
	**   4 (great great grandfather)
	**
	** The function of this LAS is to compare the value of the
	** attribute in the Slapi_Entry with the "userdn".
	**
	** Ex: userdn: "cn=prasanta, o= netscape, c= us"
	** and in the Slapi_Entry the manager attribute  has
	** manager = <value>. Compare the userdn with manager.value to
	** determine the result.
	**
	*/
 	s_attrName = attrName = slapi_ch_strdup (attr_pattern);

	/* ignore leading/trailing whitespace */
	while(ldap_utf8isspace(attrName)) LDAP_UTF8INC(attrName);
	len = strlen(attrName);
	ptr = attrName+len-1;
	while(ptr >= attrName && ldap_utf8isspace(ptr)) {
		*ptr = '\0';
		LDAP_UTF8DEC(ptr);
	}

	
	/* See if we have a  parent[2].attr" rule */
	if (strstr(attrName, "parent[") != NULL) {
		char	*word, *str, *next;
	
		numOflevels = 0;
		n_currEntryDn = slapi_entry_get_ndn ( lasinfo.resourceEntry );
		str = attrName;

		ldap_utf8strtok_r(str, "[],. ",&next);
		/* The first word is "parent[" and so it's not important */

		while ((word= ldap_utf8strtok_r(NULL, "[],.", &next)) != NULL) {
			if (ldap_utf8isdigit(word)) {
				while (word && ldap_utf8isspace(word)) LDAP_UTF8INC(word);
				if (numOflevels < ACLLAS_MAX_LEVELS) 
					levels[numOflevels++] = atoi (word);
				else  {
					/*
					 * Here, ignore the extra levels..it's really
					 * a syntax error which should have been ruled out at parse time
					*/
					slapi_log_error( SLAPI_LOG_FATAL, plugin_name, 
						"DS_LASUserDnattr: Exceeded the ATTR LIMIT:%d: Ignoring extra levels\n",
									ACLLAS_MAX_LEVELS);
				}
			} else {
				/* Must be the attr name. We can goof of by 
				** having parent[1,2,a] but then you have to be
				** stupid to do that.
				*/
				char	*p = word;
				if (*--p == '.')  {
					attrName = word;
					break;
				}
			}
		}
		info.attr = attrName;
		info.clientdn = lasinfo.clientDn;
		info.result = 0;
	} else {
		levels[0] = 0;
		numOflevels = 1;
		
	} 

	/* No attribute name specified--it's a syntax error and so undefined */
	if (attrName == NULL ) {
		slapi_ch_free ( (void**) &s_attrName);
		return LAS_EVAL_FAIL;
	}

	slapi_log_error( SLAPI_LOG_ACL, plugin_name,"Attr:%s\n" , attrName);
	matched = ACL_FALSE;
	for (i=0; i < numOflevels; i++) {
		if ( levels[i] == 0 ) {
			Slapi_Value *sval=NULL;
			const struct berval		*attrVal;
			int j;

			/*
			 * For the add operation, the resource itself (level 0)
			 * must never be allowed to grant access--
			 * This is because access would be granted based on a value
		 	 * of an attribute in the new entry--security hole.
			 * 
			*/

			if ( lasinfo.aclpb->aclpb_optype == SLAPI_OPERATION_ADD) {
				slapi_log_error( SLAPI_LOG_ACL, plugin_name,
					"ACL info: userdnAttr does not allow ADD permission at level 0.\n");
				got_undefined = 1;
				continue;
			}
			slapi_entry_attr_find( lasinfo.resourceEntry, attrName, &a);
			if ( NULL == a ) continue;
			j= slapi_attr_first_value ( a,&sval );
			while ( j != -1 ) {
				attrVal = slapi_value_get_berval ( sval );
				/* Here if atleast 1 value matches then we are done.*/
				val = slapi_create_dn_string("%s", attrVal->bv_val);
				if (NULL == val) {
					slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
							"DS_LASUserDnAttrEval: Invalid syntax: %s\n",
							attrVal->bv_val );
					slapi_ch_free ( (void**) &s_attrName);
					return LAS_EVAL_FAIL;
				}

				if (slapi_utf8casecmp((ACLUCHP)val, (ACLUCHP)lasinfo.clientDn ) == 0) {
					char	ebuf [ BUFSIZ ];
					/* Wow it matches */
					slapi_log_error( SLAPI_LOG_ACL, plugin_name,
						"userdnAttr matches(%s, %s) level (%d)\n",
						val,
				ACL_ESCAPE_STRING_WITH_PUNCTUATION (lasinfo.clientDn, ebuf),
						0);
					matched = ACL_TRUE;
					slapi_ch_free ( (void **) &val);
					break;
				}
				slapi_ch_free ( (void**) &val);
				j = slapi_attr_next_value ( a, j, &sval );
			}
		} else {
			char		*p_dn;	/* parent dn */

			p_dn = acllas__dn_parent (n_currEntryDn, levels[i]);
			if (p_dn == NULL) continue;

			/* use new search internal API */
			{
			Slapi_PBlock *aPb = slapi_pblock_new ();

			/*
			 * This search may be chained if chaining for ACL is
			 * is enabled in the backend and the entry is in
			 * a chained backend.
			*/
			slapi_search_internal_set_pb (  aPb,
							p_dn,
							LDAP_SCOPE_BASE,
							"objectclass=*",
							&attrs[0],
							0,
							NULL /* controls */,
							NULL /* uniqueid */,
							aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
							0 /* actions */);

			slapi_search_internal_callback_pb(aPb,
							  &info /* callback_data */,
							  NULL/* result_callback */,
							  acllas__verify_client,
							  NULL /* referral_callback */);
			slapi_pblock_destroy(aPb);
			}

			/*
			 *  Currently info.result is boolean so
			 * we do not need to check for ACL_DONT_KNOW
			*/
			if (info.result) {
				matched = ACL_TRUE;
				slapi_log_error( SLAPI_LOG_ACL, plugin_name,
						"userdnAttr matches at level (%d)\n", levels[i]);
			}
		}
		if (matched == ACL_TRUE) {				
			break;
		}
	}

	slapi_ch_free ( (void **) &s_attrName);

	/*
	 * If no terms were undefined, then evaluate as normal.
	 * If there was an undefined term, but another one was TRUE, then we also evaluate
	 * as normal.  Otherwise, the whole expression is UNDEFINED.
	*/
	if ( matched == ACL_TRUE || !got_undefined ) {
		if (comparator == CMP_OP_EQ) {
			rc = (matched == ACL_TRUE  ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
		} else {
			rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
		}
	} else {
		rc = LAS_EVAL_FAIL;
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			"Returning UNDEFINED for userdnattr evaluation.\n");
	} 

	return rc;
}

/***************************************************************************
*
* DS_LASLdapUrlAttrEval
*
*
* Input:
*	attr_name     The string "ldapurl" - in lower case.
*	comparator    CMP_OP_EQ or CMP_OP_NE only
*	attr_pattern  A comma-separated list of users
*	cachable      Always set to FALSE.
*	subject       Subject property list
*	resource      Resource property list
*	auth_info     Authentication info, if any
*	las_info      LAS info to pass the resource entry
*
* Returns:
*	retcode	      The usual LAS return codes.
*
* Error Handling:
*	None.
*
**************************************************************************/
int 
DS_LASLdapUrlAttrEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, 
		char *attr_pattern, int *cachable, void **LAS_cookie, 
		PList_t subject, PList_t resource, PList_t auth_info,
		PList_t global_auth, lasInfo lasinfo)
{

	char			*n_currEntryDn = NULL;
	char			*s_attrName = NULL, *attrName = NULL;
	char			*ptr;
	int				matched;
	int				rc, len, i;
	int				levels[ACLLAS_MAX_LEVELS];
	int				numOflevels =0;
	struct userdnattr_info	info = {0};
	char			*attrs[2] = { LDAP_ALL_USER_ATTRS, NULL };
	int				got_undefined = 0;

	/* 
	** The ldapurlAttr syntax is
	** 	userdnattr = <attribute> or
	**	userdnattr = parent[0,2,4].attribute"
	**  Ex:
	**	userdnattr = manager; or
	**	userdnattr = "parent[0,2,4].manager";
	**
	**  Here 0 means current level, 2 means grandfather and 
	**   4 (great great grandfather)
	**
	** The function of this LAS is to compare the value of the
	** attribute in the Slapi_Entry with the "ldapurl". 
	**
	** Ex: ldapurl: ldap:///dc=example,dc=com??sub?(l=Mountain View)
	** and in the Slapi_Entry of the bind user has
	** l = Mountain View. Compare the bind user's 'l' and the value to
	** determine the result.
	**
	*/
 	s_attrName = attrName = slapi_ch_strdup(attr_pattern);

	/* ignore leading/trailing whitespace */
	while (ldap_utf8isspace(attrName)) LDAP_UTF8INC(attrName);
	len = strlen(attrName);
	ptr = attrName+len-1;
	while (ptr >= attrName && ldap_utf8isspace(ptr)) {
		*ptr = '\0';
		LDAP_UTF8DEC(ptr);
	}

	/* See if we have a  parent[2].attr" rule */
	if (strstr(attrName, "parent[") != NULL) {
		char	*word, *str, *next;
	
		numOflevels = 0;
		n_currEntryDn = slapi_entry_get_ndn ( lasinfo.resourceEntry );
		str = attrName;

		ldap_utf8strtok_r(str, "[],. ",&next);
		/* The first word is "parent[" and so it's not important */

		while ((word= ldap_utf8strtok_r(NULL, "[],.", &next)) != NULL) {
			if (ldap_utf8isdigit(word)) {
				while (word && ldap_utf8isspace(word)) LDAP_UTF8INC(word);
				if (numOflevels < ACLLAS_MAX_LEVELS) 
					levels[numOflevels++] = atoi (word);
				else  {
					/*
					 * Here, ignore the extra levels..it's really
					 * a syntax error which should have been ruled out at parse time
					*/
					slapi_log_error( SLAPI_LOG_FATAL, plugin_name, 
						"DS_LASLdapUrlattr: Exceeded the ATTR LIMIT:%d: Ignoring extra levels\n",
									ACLLAS_MAX_LEVELS);
				}
			} else {
				/* Must be the attr name. We can goof of by 
				** having parent[1,2,a] but then you have to be
				** stupid to do that.
				*/
				char	*p = word;
				if (*--p == '.')  {
					attrName = word;
					break;
				}
			}
		}
		info.attr = attrName;
		info.clientdn = lasinfo.clientDn;
		info.aclpb = lasinfo.aclpb;
		info.result = 0;
	} else {
		levels[0] = 0;
		numOflevels = 1;
		
	} 

	/* No attribute name specified--it's a syntax error and so undefined */
	if (attrName == NULL ) {
		slapi_ch_free ( (void**) &s_attrName);
		return LAS_EVAL_FAIL;
	}

	slapi_log_error( SLAPI_LOG_ACL, plugin_name,"Attr:%s\n" , attrName);
	matched = ACL_FALSE;
	for (i = 0; i < numOflevels; i++) {
		if ( levels[i] == 0 ) { /* parent[0] or the target itself */
			Slapi_Value             *sval = NULL;
			const struct  berval	*attrVal;
			Slapi_Attr				*attrs;
			int i;
	
			/* Get the attr from the resouce entry */
			if ( 0 == slapi_entry_attr_find (lasinfo.resourceEntry,
											 attrName, &attrs) ) {
				i = slapi_attr_first_value ( attrs, &sval );
				if ( i == -1 ) {
					/* Attr val not there
					 * so it's value cannot equal other one */
					matched = ACL_FALSE;
					continue; /* try next level */
				}
			} else {
				/* Not there  so it cannot equal another one */
				matched = ACL_FALSE;
				continue; /* try next level */
			}
			
			while ( matched != ACL_TRUE && (sval != NULL)) {
				attrVal = slapi_value_get_berval ( sval );
				matched = acllas__client_match_URL ( lasinfo.aclpb, 
													 lasinfo.clientDn, 
								     				 attrVal->bv_val);
				if ( matched != ACL_TRUE ) 
					i = slapi_attr_next_value ( attrs, i, &sval );
				if ( matched == ACL_DONT_KNOW ) {
					got_undefined = 1;
				}
			}
		} else {
			char		*p_dn;	/* parent dn */
			Slapi_PBlock *aPb = NULL;

			p_dn = acllas__dn_parent (n_currEntryDn, levels[i]);
			if (p_dn == NULL) continue;

			/* use new search internal API */
			aPb = slapi_pblock_new ();

			/*
			 * This search may be chained if chaining for ACL is
			 * is enabled in the backend and the entry is in
			 * a chained backend.
			 */
			slapi_search_internal_set_pb (  aPb,
							p_dn,
							LDAP_SCOPE_BASE,
							"objectclass=*",
							&attrs[0],
							0,
							NULL /* controls */,
							NULL /* uniqueid */,
							aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
							0 /* actions */);

			slapi_search_internal_callback_pb(aPb,
							  &info /* callback_data */,
							  NULL/* result_callback */,
							  acllas__verify_ldapurl,
							  NULL /* referral_callback */);
			slapi_pblock_destroy(aPb);

			/*
			 *  Currently info.result is boolean so
			 * we do not need to check for ACL_DONT_KNOW
			*/
			if (info.result) {
				matched = ACL_TRUE;
				slapi_log_error( SLAPI_LOG_ACL, plugin_name,
						"userdnAttr matches at level (%d)\n", levels[i]);
			}
		}
		if (matched == ACL_TRUE) {				
			break;
		}
	}
	slapi_ch_free ( (void **) &s_attrName);

	/*
	 * If no terms were undefined, then evaluate as normal.
	 * If there was an undefined term, but another one was TRUE, 
	 * then we also evaluate as normal.  
	 * Otherwise, the whole expression is UNDEFINED.
	 */
	if ( matched == ACL_TRUE || !got_undefined ) {
		if (comparator == CMP_OP_EQ) {
			rc = (matched == ACL_TRUE  ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
		} else {
			rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
		}
	} else {
		rc = LAS_EVAL_FAIL;
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			"Returning UNDEFINED for userdnattr evaluation.\n");
	} 

	return rc;
}

/***************************************************************************
*
* DS_LASAuthMethodEval
*
*
* Input:
*	attr_name	The string "authmethod" - in lower case.
*	comparator	CMP_OP_EQ or CMP_OP_NE only
*	attr_pattern	A comma-separated list of users
*	cachable	Always set to FALSE.
*	subject		Subject property list
*	resource        Resource property list
*	auth_info	Authentication info, if any
*
* Returns:
*	retcode	        The usual LAS return codes.
*
* Error Handling:
*	None.
*
**************************************************************************/
int 
DS_LASAuthMethodEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, 
		char *attr_pattern, int *cachable, void **LAS_cookie, 
		PList_t subject, PList_t resource, PList_t auth_info,
		PList_t global_auth)
{

	char		*attr;
	char		*ptr;
	int			len;
	int			matched;
	int			rc;
	char		*s = NULL;
	lasInfo			lasinfo;

	if ( 0 !=  (rc = __acllas_setup (errp, attr_name, comparator, 0, /* Don't allow range comparators */
									attr_pattern,cachable,LAS_cookie,
									subject, resource, auth_info,global_auth,
									DS_LAS_AUTHMETHOD, "DS_LASAuthMethodEval", 
									&lasinfo )) ) {
		return LAS_EVAL_FAIL;
	}

 	attr = attr_pattern;

	matched = ACL_FALSE;
	/* ignore leading whitespace */
	s = strstr (attr, SLAPD_AUTH_SASL);
	if ( s) {
		s +=4;
		attr = s;
	}
 
	while(ldap_utf8isspace(attr)) LDAP_UTF8INC(attr);
	len = strlen(attr);
	ptr = attr+len-1;
	while(ptr >= attr && ldap_utf8isspace(ptr)) {
		*ptr = '\0';
		LDAP_UTF8DEC(ptr);
	}

	slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
				"DS_LASAuthMethodEval:authtype:%s authmethod:%s\n", 
				lasinfo.authType, attr);

	/* None method means, we don't care -- otherwise we care */
	if ((strcasecmp(attr, "none") == 0) ||
		(strcasecmp(attr, lasinfo.authType) == 0)) {
		matched = ACL_TRUE;
	}

	if ( matched == ACL_TRUE || matched == ACL_FALSE) {
		if (comparator == CMP_OP_EQ) {
			rc = (matched == ACL_TRUE ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
		} else {
			rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
		}
	} else {
		rc = LAS_EVAL_FAIL;
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			"Returning UNDEFINED for authmethod evaluation.\n");
	}

	return rc;
}

/***************************************************************************
*
* DS_LASSSFEval
*
*
* Input:
*       attr_name       The string "ssf" - in lower case.
*       comparator      CMP_OP_EQ, CMP_OP_NE, CMP_OP_GT, CMP_OP_LT, CMP_OP_GE, CMP_OP_LE
*       attr_pattern    An integer representing the SSF
*       cachable        Always set to FALSE.
*       subject         Subject property list
*       resource        Resource property list
*       auth_info       Authentication info, if any
*
* Returns:
*       retcode         The usual LAS return codes.
*
* Error Handling:
*       None.
*
**************************************************************************/
int
DS_LASSSFEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
                char *attr_pattern, int *cachable, void **LAS_cookie,
                PList_t subject, PList_t resource, PList_t auth_info,
                PList_t global_auth)
{
	char            *attr;
	char            *ptr;
	int             len;
	int             rc;
	lasInfo         lasinfo;
	int		aclssf;

	if ( 0 !=  (rc = __acllas_setup (errp, attr_name, comparator, 1, /* Allow range comparators */
					attr_pattern,cachable,LAS_cookie,
					subject, resource, auth_info,global_auth,
					DS_LAS_SSF, "DS_LASSSFEval",
					&lasinfo )) ) {
		return LAS_EVAL_FAIL;
	}

	attr = attr_pattern;

	/* ignore leading and trailing whitespace */
	while(ldap_utf8isspace(attr)) LDAP_UTF8INC(attr);
	len = strlen(attr);
	ptr = attr+len-1;
	while(ptr >= attr && ldap_utf8isspace(ptr)) {
		*ptr = '\0';
		LDAP_UTF8DEC(ptr);
	}

	/* Convert SSF from bind rule to an int. */
	aclssf = (int) strtol(attr, &ptr, 10);
	if (*ptr != '\0') {
		rc = LAS_EVAL_FAIL;
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"Error parsing numeric SSF from bind rule.\n");
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"Returning UNDEFINED for ssf evaluation.\n");
	}

	/* Check for negative values or a value overflow. */
	if ((aclssf < 0) || (((aclssf == INT_MAX) || (aclssf == INT_MIN)) && (errno == ERANGE))){
		rc = LAS_EVAL_FAIL;
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"SSF \"%s\" is invalid. Value must range from 0 to %d",
			attr, INT_MAX);
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"Returning UNDEFINED for ssf evaluation.\n");
	}

	slapi_log_error( SLAPI_LOG_ACL, plugin_name,
		"DS_LASSSFEval: aclssf:%d, ssf:%d\n",
		aclssf, lasinfo.ssf);

	switch ((int)comparator) {
		case CMP_OP_EQ:
			if (lasinfo.ssf == aclssf) {
				rc = LAS_EVAL_TRUE;
			} else {
				rc = LAS_EVAL_FALSE;
			}
			break;
		case CMP_OP_NE:
			if (lasinfo.ssf != aclssf) {
				rc = LAS_EVAL_TRUE;
			} else {
				rc = LAS_EVAL_FALSE;
			}
			break;
		case CMP_OP_GT:
			if (lasinfo.ssf > aclssf) {
				rc = LAS_EVAL_TRUE;
			} else {
				rc = LAS_EVAL_FALSE;
			}
			break;
		case CMP_OP_LT:
			if (lasinfo.ssf < aclssf) {
				rc = LAS_EVAL_TRUE;
			} else {
				rc = LAS_EVAL_FALSE;
			}
			break;
		case CMP_OP_GE:
			if (lasinfo.ssf >= aclssf) {
				rc = LAS_EVAL_TRUE;
			} else {
				rc = LAS_EVAL_FALSE;
			}
			break;
		case CMP_OP_LE:
			if (lasinfo.ssf <= aclssf) {
				rc = LAS_EVAL_TRUE;
			} else {
				rc = LAS_EVAL_FALSE;
			}
			break;
		default:
			/* This should never happen since the comparator is
			 * validated by __acllas_setup(), but better safe
			 * than sorry. */
			rc = LAS_EVAL_FAIL;
			slapi_log_error( SLAPI_LOG_ACL, plugin_name,
				"Invalid comparator \"%d\" evaluating SSF.\n",
				(int)comparator);
			slapi_log_error( SLAPI_LOG_ACL, plugin_name,
				"Returning UNDEFINED for ssf evaluation.\n");
	}

	return rc;
}


/****************************************************************************
* Struct to evaluate and keep the current members being evaluated
*
*		 0 1 2 3 4 5
*	member: [a,b,c,d,e,f]
* 	c_idx may point to 2 i.e to "c" if "c" is being evaluated to
*		see if any of "c" members is the clientDN.
*	lu_idx points to the last used spot i.e 5. 
*	lu_idx++ is the next free spot.
*
*	We allocate ACLLAS_MAX_GRP_MEMBER ptr first and then we add if it 
*	is required. 
*
***************************************************************************/
#define ACLLAS_MAX_GRP_MEMBER 50
struct member_info 
{
	char			*member;		/* member DN */
	int				parentId;		/* parent of this member */
};

struct eval_info
{
	int					result;		/* result status */
	char				*userDN;	/* client's normalized DN */
	int 				c_idx;		/* Index to the current member being processed */
	int					lu_idx;		/* Index to the slot where the last member is stored */
	char				**member;	/* mmebers list */
	struct member_info 	**memberInfo;/* array of memberInfo  */
	CERTCertificate		*clientCert;	/* ptr to cert */
	struct acl_pblock 	*aclpb;	/*aclpblock */
};

#ifdef FOR_DEBUGGING
static void
dump_member_info ( struct eval_info *info, struct member_info *minfo, char *buf )
{
	if ( minfo )
	{
		if ( minfo->parentId >= 0 )
		{
			dump_member_info ( info, minfo->parentId, buf );
		}
		else
		{
			strcat ( buf, "<nil>" );
		}
		strcat ( buf, "->" );
		strcat ( buf, minfo->member );
	}
}

static void
dump_eval_info (char *caller, struct eval_info *info, int idx)
{
	char buf[1024];
	int len;
	int i;

	if ( idx < 0 )
	{
		sprintf ( buf, "\nuserDN=\"%s\"\nmember=", info->userDN);
		if (info->member && *info->member)
		{
			len = strlen (buf);
			/* member is a char ** */
			sprintf ( &(buf[len]), "\"%s\"", *info->member );
		}
		len = strlen (buf);
		sprintf ( &(buf[len]), "\nmemberinfo[%d]-[%d]:", info->c_idx, info->lu_idx );
		if ( info->memberInfo )
		for (i = 0; i <= info->lu_idx; i++)
		{
			len = strlen(buf);
			sprintf ( &buf[len], "\n  [%d]: ", i );
			dump_member_info ( info, info->memberInfo[i], buf );
		}
		slapi_log_error ( SLAPI_LOG_FATAL, NULL, "\n======== candidate member info in eval_info ========%s\n\n", buf );
	}
	else
	{
		sprintf (buf, "evaluated candidate [%d]=", idx);
		switch (info->result)
		{
			case ACL_TRUE:
				strcat (buf, "ACL_TRUE\n");
				break;
			case ACL_FALSE:
				strcat (buf, "ACL_FALSE\n");
				break;
			case ACL_DONT_KNOW:
				strcat (buf, "ACL_DONT_KNOW\n");
				break;
			default:
				len = strlen (buf);
				sprintf ( &(buf[len]), "%d\n", info->result );
				break;
		}
		dump_member_info ( info, info->memberInfo[idx], buf );
		slapi_log_error ( SLAPI_LOG_FATAL, NULL, "%s\n", buf );
	}
}
#endif

/***************************************************************************
*
* acllas__user_ismember_of_group
*
* 	Check if the user is a member of the group and nested groups..
*
* Input:
*	char	*groupdn	- DN of the group
*	char	*clientDN	- Dn of the client
*
* Returns:
*	ACL_TRUE		- the user is a member of the group.
*	ACL_FALSE		- Not a member
*	ACL_DONT_KNOW	- Any errors eg. resource limits exceeded and we could
*					not compelte the evaluation.
*
* Error Handling:
*	None.
*
**************************************************************************/
static int
acllas__user_ismember_of_group( struct acl_pblock *aclpb,
				char* groupDN, 
				char* clientDN,
				int cache_status,
				CERTCertificate	*clientCert)
{
 

 	char			*attrs[5];
	char			*currDN;
	int			i,j;
	int			result = ACL_FALSE;
	struct eval_info	info = {0};
	int			nesting_level;
	int 			numOfMembersAtCurrentLevel;
	int			numOfMembersVisited;
	int			totalMembersVisited;
	int			numOfMembers;
	int			max_nestlevel;
	int			max_memberlimit;
	aclUserGroup		*u_group;
	struct member_info	*groupMember = NULL;
	struct member_info 	*parentGroup = NULL;

	/* 
	** First, Let's look thru the cached list and determine if the client is
	** a member of the cached list of groups.
	*/
	if ( (u_group = aclg_get_usersGroup ( aclpb , clientDN )) == NULL) {
		 slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"Failed to find/allocate a usergroup--aborting evaluation\n");
		return(ACL_DONT_KNOW);
	}

	slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Evaluating user %s in group %s?\n",
		clientDN, groupDN );

	/* Before I start using, get a reader lock on the group cache */
	aclg_lock_groupCache ( 1 /* reader */ );
	for ( i= 0; i < u_group->aclug_numof_member_group; i++) {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, "-- In %s\n",
		u_group->aclug_member_groups[i] );
		if ( slapi_utf8casecmp((ACLUCHP)groupDN, (ACLUCHP)u_group->aclug_member_groups[i]) == 0){
			aclg_unlock_groupCache ( 1 /* reader */ );
			slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Evaluated ACL_TRUE\n");
			return ACL_TRUE;
		}
	}

	/* see if we know the client is not a member of a group. */
	for ( i= 0; i < u_group->aclug_numof_notmember_group; i++) {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, "-- Not in %s\n",
		u_group->aclug_notmember_groups[i] );
		if ( slapi_utf8casecmp((ACLUCHP)groupDN, (ACLUCHP)u_group->aclug_notmember_groups[i]) == 0){
			aclg_unlock_groupCache ( 1 /* reader */ );
			slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Evaluated ACL_FALSE\n");
			return ACL_FALSE;
		}
	}

	/* 
	** That means we didn't find the the group in the cache. -- we have to add it
	** so no need for READ lock - need to get a WRITE lock. We will get it just before
	** modifying it.
	*/
	aclg_unlock_groupCache ( 1 /* reader */ );

	/* Indicate the initialization handler  -- this module will be 
	** called by the backend to evaluate the entry.
	*/
	info.result = ACL_FALSE;
	if (clientDN && *clientDN != '\0') 
		info.userDN = clientDN;
	else 
		info.userDN = NULL;

	info.c_idx = 0;
	info.memberInfo = (struct member_info **) slapi_ch_malloc (ACLLAS_MAX_GRP_MEMBER * sizeof(struct member_info *));
	groupMember = (struct member_info *) slapi_ch_malloc ( sizeof (struct member_info) );
	groupMember->member = slapi_ch_strdup(groupDN);
	groupMember->parentId = -1;
	info.memberInfo[0] = groupMember;
	info.lu_idx = 0;

	attrs[0] = type_member;
	attrs[1] = type_uniquemember;
	attrs[2] = type_memberURL;
	attrs[3] = type_memberCert;
	attrs[4] = NULL;

	currDN = groupMember->member;

	/* nesting level is 0 to begin with */
	nesting_level = 0;
	numOfMembersVisited = 0;
	totalMembersVisited = 0;
	numOfMembersAtCurrentLevel = 1;

	if (clientCert)
		info.clientCert = clientCert;
	else 
		info.clientCert = NULL;
	info.aclpb = aclpb;

	max_memberlimit = aclpb->aclpb_max_member_sizelimit;
	max_nestlevel = aclpb->aclpb_max_nesting_level;

#ifdef FOR_DEBUGGING
	dump_eval_info ( "acllas__user_ismember_of_group", &info, -1 );
#endif

eval_another_member:

	numOfMembers = info.lu_idx - info.c_idx;

	/* Use new search internal API */
	{
		Slapi_PBlock * aPb = slapi_pblock_new ();
		
		/*
		 * This search may NOT be chained--we demand that group
		 * definition be local.
		*/
		slapi_search_internal_set_pb (  aPb,
						currDN,
						LDAP_SCOPE_BASE,
						filter_groups,
						&attrs[0],
						0,
						NULL /* controls */,
						NULL /* uniqueid */,
						aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
						SLAPI_OP_FLAG_NEVER_CHAIN /* actions */);
		slapi_search_internal_callback_pb(aPb,
						  &info /* callback_data */,
						  NULL/* result_callback */,
						  acllas__handle_group_entry,
						  NULL /* referral_callback */); 

		if ( info.result == ACL_TRUE )
			slapi_log_error( SLAPI_LOG_ACL, plugin_name,"-- In %s\n", info.memberInfo[info.c_idx]->member ); 
		else if ( info.result == ACL_FALSE )
			slapi_log_error( SLAPI_LOG_ACL, plugin_name,"-- Not in %s\n", info.memberInfo[info.c_idx]->member ); 

		slapi_pblock_destroy (aPb);
	}

	if (info.result == ACL_TRUE) {
		/* 
		** that means the client is a member of the
		** group or one of the nested groups. We are done.
		*/
		result = ACL_TRUE;
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Evaluated ACL_TRUE\n");
		goto free_and_return;
	}
	numOfMembersVisited++;

	if (numOfMembersVisited == numOfMembersAtCurrentLevel) {
		/* This means we have looked at all the members for this level */
		numOfMembersVisited = 0;
		
		/* Now we are ready to look at the next level */
		nesting_level++;
	
		/* So, far we have visited ... */
		totalMembersVisited += numOfMembersAtCurrentLevel;

		/* How many members in the next level ? */
		numOfMembersAtCurrentLevel = 
			info.lu_idx - totalMembersVisited +1;
	}

	if ((nesting_level > max_nestlevel)) {
		 slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"GroupEval:Member not found within the allowed nesting level (Allowed:%d Looked at:%d)\n", 
			max_nestlevel, nesting_level);

		result = ACL_DONT_KNOW; /* don't try to cache info based on this result */
		goto free_and_return;
	}

	/* limit of -1 means "no limit */
	if (info.c_idx > max_memberlimit && 
			max_memberlimit != -1 ) {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			"GroupEval:Looked at too many entries:(%d, %d)\n",
				info.c_idx, info.lu_idx);
		result = ACL_DONT_KNOW; /* don't try to cache info based on this result */
		goto free_and_return;
	}
	if (info.lu_idx > info.c_idx) {
		if (numOfMembers == (info.lu_idx - info.c_idx)) {
			/* That means it's not a GROUP. It is just another
			** useless member which doesn't match. Remove  the BAD dude.
			*/
			groupMember = info.memberInfo[info.c_idx];

			if  (groupMember ) {
				if ( groupMember->member )  slapi_ch_free ( (void **) &groupMember->member );
				slapi_ch_free ( (void **) &groupMember );
				info.memberInfo[info.c_idx] = NULL;
			}
		}
		info.c_idx++;

		/* Go thru the stack and see if we have already 
		** evaluated this group. If we have, then skip it.
		*/
		while (1) {
			int	evalNext=0;
			int	j;
			if (info.c_idx >  info.lu_idx)  {
				/* That means we have crossed the limit. We
				** may end of in this situation if we 
				** have circular groups
				*/
				info.c_idx = info.lu_idx;	
				goto free_and_return;
			}
		
			/* Break out of the loop if we have searched to the end */
			groupMember = info.memberInfo[info.c_idx];
			if ( (NULL == groupMember) || ((currDN = groupMember->member)!= NULL))
				break;

			for (j = 0; j < info.c_idx; j++) {
				groupMember = info.memberInfo[j];
				if (groupMember->member && 
					(slapi_utf8casecmp((ACLUCHP)currDN, (ACLUCHP)groupMember->member) == 0)) {
					/* Don't need the duplicate */
					groupMember = info.memberInfo[info.c_idx];
					slapi_ch_free ( (void **) &groupMember->member );
					slapi_ch_free ( (void **) &groupMember );
					info.memberInfo[info.c_idx] = NULL;
					info.c_idx++;
					evalNext=1;
					break;
				}
			}
			if (!evalNext) break;
		}
		/* Make sure that we have a valid DN to chug along */
		groupMember = info.memberInfo[info.c_idx];
		if ((info.c_idx <= info.lu_idx) && ((currDN = groupMember->member) != NULL))
			goto eval_another_member;
	} 

free_and_return:
	/* Remove the unnecessary members from the list  which
	** we might have accumulated during the last execution
	** and we don't need to look at them.
	*/
	i = info.c_idx;
	i++;
	while (i <= info.lu_idx) {
		groupMember = info.memberInfo[i];
		slapi_ch_free ( (void **) &groupMember->member );
		slapi_ch_free ( (void **) &groupMember );
		info.memberInfo[i] = NULL;
		i++;
	}

	/* 
	** Now we have a list which has all the groups 
	** which we  need to cache
	*/ 
	info.lu_idx = info.c_idx;

	/* since we are updating the groupcache, get a write lock */
	aclg_lock_groupCache ( 2 /* writer */ );

	/* 
	** Keep the result of the evaluation in the cache.
	** We have 2 lists: member_of and not_member_of. We can use this 
	** cached information next time we evaluate groups.
	*/
	if (result == ACL_TRUE && 
		(cache_status & ACLLAS_CACHE_MEMBER_GROUPS)) {
		int	ngr = 0;
	
		/* get the last group which the user is a member of */	
		groupMember = info.memberInfo[info.c_idx];

		while ( groupMember ) {
			int already_cached = 0;

			parentGroup = (groupMember->parentId<0)?NULL:info.memberInfo[groupMember->parentId];
			for (j=0; j < u_group->aclug_numof_member_group;j++){
				if (slapi_utf8casecmp( (ACLUCHP)groupMember->member,
                                 (ACLUCHP)u_group->aclug_member_groups[j]) == 0) {
					already_cached = 1;
                   	break;
  				}
			}
			if (already_cached)  {
				groupMember = parentGroup;
				parentGroup = NULL;
				continue;
			}

			ngr = u_group->aclug_numof_member_group++;
			if (u_group->aclug_numof_member_group >= 
					u_group->aclug_member_group_size){
				u_group->aclug_member_groups = 
					(char **) slapi_ch_realloc (
						(void *) u_group->aclug_member_groups,
						(u_group->aclug_member_group_size +
						ACLUG_INCR_GROUPS_LIST) *
						sizeof (char *));
				u_group->aclug_member_group_size +=
							ACLUG_INCR_GROUPS_LIST;
			} 
			u_group->aclug_member_groups[ngr] = slapi_ch_strdup ( groupMember->member );
			slapi_log_error ( SLAPI_LOG_ACL, plugin_name, 
					"Adding Group (%s) ParentGroup (%s) to the IN GROUP List\n",
					groupMember->member , parentGroup ? parentGroup->member: "NULL");

			groupMember = parentGroup;
			parentGroup = NULL;
		}
	} else if (result == ACL_FALSE && 
			(cache_status & ACLLAS_CACHE_NOT_MEMBER_GROUPS)) {
		int	ngr = 0;

		/* NOT IN THE GROUP LIST */	
		/* get the last group which the user is a member of */	
		groupMember = info.memberInfo[info.c_idx];

		while ( groupMember ) {
			int already_cached = 0;

			parentGroup = (groupMember->parentId<0)?NULL:info.memberInfo[groupMember->parentId];
			for (j=0; j < u_group->aclug_numof_notmember_group;j++){
				if (slapi_utf8casecmp( (ACLUCHP)groupMember->member,
                               	(ACLUCHP)u_group->aclug_notmember_groups[j]) == 0) {
					already_cached = 1;
                   	break;
 				}
			}
			if (already_cached)  {
				groupMember = parentGroup;
				parentGroup = NULL;
				continue;
			}

			ngr = u_group->aclug_numof_notmember_group++;
			if (u_group->aclug_numof_notmember_group >= 
					u_group->aclug_notmember_group_size){
				u_group->aclug_notmember_groups = 
			     		(char **) slapi_ch_realloc (
					(void *) u_group->aclug_notmember_groups,
					(u_group->aclug_notmember_group_size +
					 ACLUG_INCR_GROUPS_LIST) *
					 sizeof (char *));
				u_group->aclug_notmember_group_size +=
							ACLUG_INCR_GROUPS_LIST;
			} 
			u_group->aclug_notmember_groups[ngr] = slapi_ch_strdup ( groupMember->member );
			slapi_log_error ( SLAPI_LOG_ACL, plugin_name, 
					"Adding Group (%s) ParentGroup (%s) to the NOT IN GROUP List\n",
					groupMember->member , parentGroup ? parentGroup->member: "NULL");

			groupMember = parentGroup;
			parentGroup = NULL;
		}
	} else if ( result == ACL_DONT_KNOW ) {

		/*
		 * We terminated the search without reaching a conclusion--so
		 * don't cache any info based on this evaluation.
		*/
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, "Evaluated ACL_DONT_KNOW\n");
	} 

	/* Unlock the group cache, we are done with updating */
	aclg_unlock_groupCache ( 2 /* writer */ );

	for (i=0; i <= info.lu_idx; i++) {
		groupMember = info.memberInfo[i];
		if ( NULL == groupMember ) continue;

		slapi_ch_free ( (void **) &groupMember->member );
		slapi_ch_free ( (void **) &groupMember );
	}

	/* free the pointer array.*/
	slapi_ch_free ( (void **) &info.memberInfo);
	return result;
}

/***************************************************************************
*
* acllas__handle_group_entry
*	
*	handler called. Compares the userdn value and determines if it's
*	a member of not.
*
* Input:
*
*
* Returns:
*
* Error Handling:
*
**************************************************************************/
static int
acllas__handle_group_entry (Slapi_Entry* e, void *callback_data)
{
	struct eval_info	*info;
 	Slapi_Attr		*currAttr, *nextAttr;
	char			*n_dn, *attrType;
	int				n;
	int				i;

	info = (struct eval_info *) callback_data;
	info->result = ACL_FALSE;
 
 	if (e == NULL) {
		return 0;
	}

	slapi_entry_first_attr ( e,  &currAttr);
	if ( NULL == currAttr ) return 0;

	slapi_attr_get_type ( currAttr, &attrType );
	if (NULL == attrType ) return 0;

	do {
		Slapi_Value *sval = NULL;
		const struct berval		*attrVal;

 		if ((strcasecmp (attrType, type_member) == 0) ||
 				(strcasecmp (attrType, type_uniquemember) == 0 ))  {

			i = slapi_attr_first_value ( currAttr,&sval );
			while ( i != -1 ) {
				struct member_info	*groupMember = NULL;
				attrVal = slapi_value_get_berval ( sval );
				n_dn = slapi_create_dn_string( attrVal->bv_val );
				if (NULL == n_dn) {
					slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
						"acllas__handle_group_entry: Invalid syntax: %s\n",
						attrVal->bv_val );
					return 0;
				}
				n = ++info->lu_idx;
				if (n < 0) {
					slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
						  "acllas__handle_group_entry: last member index lu_idx is overflown:%d: Too many group ACL members\n", n);
					return 0;
				}
				if (!(n % ACLLAS_MAX_GRP_MEMBER)) {
					struct member_info **orig_memberInfo = info->memberInfo;
					info->memberInfo = (struct member_info **)slapi_ch_realloc(
							(char *)info->memberInfo,
							(n + ACLLAS_MAX_GRP_MEMBER) *
							sizeof(struct member_info *));
					if (!info->memberInfo) {
						slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
										 "acllas__handle_group_entry: out of memory - could not allocate space for %d group members\n",
										 n + ACLLAS_MAX_GRP_MEMBER );
						info->memberInfo = orig_memberInfo;
						return 0;
					}
				}

				/* allocate the space for the member and attch it to the list */
				groupMember = (struct member_info *)slapi_ch_malloc(
								sizeof ( struct member_info ) );
				groupMember->member = n_dn;
				groupMember->parentId = info->c_idx;
				info->memberInfo[n] = groupMember;

				if (info->userDN && 
				    slapi_utf8casecmp((ACLUCHP)n_dn, (ACLUCHP)info->userDN) == 0) {
					info->result = ACL_TRUE;	
					return 0;
				}
				i = slapi_attr_next_value ( currAttr, i, &sval );
			}
		/* Evaluate Dynamic groups */
		} else if (strcasecmp ( attrType, type_memberURL) == 0) {
			char		*memberURL, *savURL;

			if (!info->userDN) continue;

			i= slapi_attr_first_value ( currAttr,&sval );
			while ( i != -1 ) {
			        attrVal = slapi_value_get_berval ( sval );
				/*
				 * memberURL may start with "ldap:///" or "ldap://host:port"
				 * ldap://localhost:11000/o=ace industry,c=us??
				 * or
				 * ldap:///o=ace industry,c=us??
				 */
				if (strncasecmp( attrVal->bv_val, "ldap://",7) == 0 ||
					strncasecmp( attrVal->bv_val, "ldaps://",8) == 0) {
					savURL = memberURL = 
							slapi_create_dn_string("%s", attrVal->bv_val);
					if (NULL == savURL) {
						slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
							"acllas__handle_group_entry: Invalid syntax: %s\n",
							attrVal->bv_val );
						return 0;
					}
					slapi_log_error( SLAPI_LOG_ACL, plugin_name,
						  "ACL Group Eval:MemberURL:%s\n", memberURL);
					info->result = acllas__client_match_URL (
									info->aclpb, 
									info->userDN,
									memberURL);
					slapi_ch_free ( (void**) &savURL);
					if (info->result == ACL_TRUE)
						return 0;
				} else {
					/* This means that the URL is ill-formed */
					slapi_log_error( SLAPI_LOG_ACL, plugin_name,
						"ACL Group Eval:Badly Formed MemberURL:%s\n", attrVal->bv_val);
				}
				i = slapi_attr_next_value ( currAttr, i, &sval );
			}
		/* Evaluate Fortezza groups */
		} else if ((strcasecmp (attrType, type_memberCert) == 0) ) {
			/* Do we have the certificate around */
			if (!info->clientCert) {
			      slapi_log_error( SLAPI_LOG_ACL, plugin_name,
				" acllas__handle_group_entry:Client Cert missing\n" );
				continue;
			}
			i = slapi_attr_first_value ( currAttr,&sval );
			while ( i != -1 ) {
			        attrVal = slapi_value_get_berval ( sval );
			 	if (ldapu_member_certificate_match (
							info->clientCert,
						        attrVal->bv_val) == LDAP_SUCCESS) {
					info->result = ACL_TRUE;
					return 0;
				}
				i = slapi_attr_next_value ( currAttr, i, &sval );
			}
		}
	
		attrType = NULL;	
		/* get the next attr */
		slapi_entry_next_attr ( e, currAttr, &nextAttr );
		if ( NULL == nextAttr ) break;
	
		currAttr = nextAttr;	
		slapi_attr_get_type ( currAttr, &attrType );
		
	} while ( NULL != attrType );

	return 0;
}
/***************************************************************************
*
* DS_LASGroupDnAttrEval
*
*
* Input:
*	attr_name	The string "groupdnattr" - in lower case.
*	comparator	CMP_OP_EQ or CMP_OP_NE only
*	attr_pattern	A comma-separated list of users
*	cachable	Always set to FALSE.
*	subject		Subject property list
*	resource        Resource property list
*	auth_info	Authentication info, if any
*
* Returns:
*	retcode	        The usual LAS return codes.
*
* Error Handling:
*	None.
*
**************************************************************************/
struct groupdnattr_info
{
        char            *attrName;      /* name of the attribute */
        int             numofGroups;    /* number of groups */
        char            **member;
};
int 
DS_LASGroupDnAttrEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, 
		char *attr_pattern, int *cachable, void **LAS_cookie, 
		PList_t subject, PList_t resource, PList_t auth_info,
		PList_t global_auth)
{

	char			*s_attrName = NULL;
	char			*attrName;
	char			*ptr;
	int				matched;
	int				rc;
	int				len;
	Slapi_Attr 		*attr;
	int				levels[ACLLAS_MAX_LEVELS];
	int				numOflevels = 0;
	char			*n_currEntryDn = NULL;
	lasInfo			lasinfo;
	int				got_undefined = 0;

	if ( 0 !=  (rc = __acllas_setup (errp, attr_name, comparator, 0, /* Don't allow range comparators */
									attr_pattern,cachable,LAS_cookie,
									subject, resource, auth_info,global_auth,
									DS_LAS_GROUPDNATTR, "DS_LASGroupDnAttrEval", 
									&lasinfo )) ) {
		return LAS_EVAL_FAIL;
	}

	/* For anonymous client, the answer is XXX come back to this */
	if ( lasinfo.anomUser )
		return LAS_EVAL_FALSE;

	/* 
	** The groupdnAttr syntax is
	** 	groupdnattr = <attribute>
	**  Ex:
	**	groupdnattr = SIEmanager;
	**
	** The function of this LAS is to  find out if the client belongs
	** to any group  that is specified in the attr.
	*/
	attrName = attr_pattern;
	if (strstr(attrName, LDAP_URL_prefix)) {

		/* In this case "grppupdnattr="ldap:///base??attr" */

		if ((strstr (attrName, ACL_RULE_MACRO_DN_KEY) != NULL) ||
			(strstr (attrName, ACL_RULE_MACRO_DN_LEVELS_KEY) != NULL) ||
			(strstr (attrName, ACL_RULE_MACRO_ATTR_KEY) != NULL)) {			
				
				matched = aclutil_evaluate_macro( attrName, &lasinfo,
													ACL_EVAL_GROUPDNATTR);
		} else{

			matched = acllas__eval_memberGroupDnAttr(attrName, 
												lasinfo.resourceEntry, 
												lasinfo.clientDn, 
												lasinfo.aclpb);
		}
		if ( matched == ACL_DONT_KNOW) {
			got_undefined = 1;
		}
	} else {
		int	i;
		char	*n_groupdn;

		/* ignore leading/trailing whitespace */
		while(ldap_utf8isspace(attrName)) LDAP_UTF8INC(attrName);
		len = strlen(attrName);
		ptr = attrName+len-1;
		while(ptr >= attrName && ldap_utf8isspace(ptr)) {
			*ptr = '\0';
			LDAP_UTF8DEC(ptr);
		}

		slapi_log_error( SLAPI_LOG_ACL, plugin_name,"Attr:%s\n" , attrName);

		/* See if we have a  parent[2].attr" rule */
		if (strstr(attrName, "parent[") != NULL) {
			char	*word, *str, *next;

			numOflevels = 0;
			n_currEntryDn = slapi_entry_get_ndn ( lasinfo.resourceEntry ) ;
			s_attrName = attrName = slapi_ch_strdup ( attr_pattern );
			str = attrName;

			ldap_utf8strtok_r(str, "[],. ",&next);
			/* The first word is "parent[" and so it's not important */

			while ((word= ldap_utf8strtok_r(NULL, "[],.", &next)) != NULL) {
				if (ldap_utf8isdigit(word)) {
					while (word && ldap_utf8isspace(word)) LDAP_UTF8INC(word);
					if (numOflevels < ACLLAS_MAX_LEVELS) 
						levels[numOflevels++] = atoi (word);
					else  {
						/*
						 * Here, ignore the extra levels..it's really
						 * a syntax error which should have been ruled out at parse time
						*/
						slapi_log_error( SLAPI_LOG_FATAL, plugin_name, 
						"DS_LASGroupDnattr: Exceeded the ATTR LIMIT:%d: Ignoring extra levels\n",
						ACLLAS_MAX_LEVELS);
					}
				} else {
					/* Must be the attr name. We can goof of by 
					** having parent[1,2,a] but then you have to be
					** stupid to do that.
					*/
					char	*p = word;
					if (*--p == '.')  {
						attrName = word;
						break;
					}
				}
			}
		} else {
			levels[0] = 0;
			numOflevels = 1;
		} 
	
		matched = ACL_FALSE;
		for (i=0; i < numOflevels; i++) {
		    if ( levels[i] == 0 ) {
				Slapi_Value *sval=NULL;
				const struct berval		*attrVal;
				int attr_i;
				
				/*
			 	* For the add operation, the resource itself (level 0)
			 	* must never be allowed to grant access--
			 	* This is because access would be granted based on a value
		 	 	* of an attribute in the new entry--security hole.
				* XXX is this therefore FALSE or DONT_KNOW ?
				*/

				if ( lasinfo.aclpb->aclpb_optype == SLAPI_OPERATION_ADD) {
					slapi_log_error( SLAPI_LOG_ACL, plugin_name,
					"ACL info: groupdnAttr does not allow ADD permission at level 0.\n");
					got_undefined = 1;
					continue;
				}
				slapi_entry_attr_find ( lasinfo.resourceEntry, attrName, &attr);
				if ( !attr) continue;
				attr_i= slapi_attr_first_value ( attr,&sval );
				while ( attr_i != -1 ) {
			        attrVal = slapi_value_get_berval ( sval );
					n_groupdn = slapi_create_dn_string("%s", attrVal->bv_val);
					if (NULL == n_groupdn) {
						slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
							"DS_LASGroupDnAttrEval: Invalid syntax: %s\n",
							attrVal->bv_val );
						return 0;
					}
					matched =  acllas__user_ismember_of_group (
										lasinfo.aclpb, n_groupdn, lasinfo.clientDn,
										ACLLAS_CACHE_MEMBER_GROUPS, 
										lasinfo.aclpb->aclpb_clientcert);
					slapi_ch_free ( (void **) &n_groupdn);
					if (matched == ACL_TRUE ) {
						slapi_log_error( SLAPI_LOG_ACL, plugin_name,
						"groupdnattr matches at level (%d)\n", levels[i]);
						break;
					} else if ( matched == ACL_DONT_KNOW ) {
                		/* record this but keep going--maybe another group will evaluate to TRUE */
						got_undefined = 1;
					}
					attr_i= slapi_attr_next_value ( attr, attr_i, &sval );
				}
		    } else {
				char			*p_dn;
				struct groupdnattr_info	info;
				char			*attrs[2];
				int			j;

				info.numofGroups = 0;
				attrs[0] = info.attrName = attrName;
				attrs[1] = NULL;
			
				p_dn = acllas__dn_parent (n_currEntryDn, levels[i]);

				if (p_dn == NULL) continue;

				/* Use new search internal API */
				{

				Slapi_PBlock *aPb = slapi_pblock_new ();
				/*
			 	 * This search may NOT  be chained--if the user's definition is
				 * remote and the group is dynamic and the user entry
				 * changes then we would not notice--so don't go
				 * find the user entry in the first place.
				*/
				slapi_search_internal_set_pb (  aPb,
								p_dn,
								LDAP_SCOPE_BASE,
								"objectclass=*",
								&attrs[0],
								0,
								NULL /* controls */,
								NULL /* uniqueid */,
								aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
								SLAPI_OP_FLAG_NEVER_CHAIN /* actions */);
				slapi_search_internal_callback_pb(aPb,
								  &info /* callback_data */,
								  NULL/* result_callback */,
								  acllas__get_members,
								  NULL /* referral_callback */);
				slapi_pblock_destroy (aPb);
				}

				if (info.numofGroups <= 0) {
					continue;
				}
				for (j=0; j <info.numofGroups; j++) {
					if (slapi_utf8casecmp((ACLUCHP)info.member[j], 
											(ACLUCHP)lasinfo.clientDn) == 0) {
						matched = ACL_TRUE;
						break;
					}
					matched = acllas__user_ismember_of_group (
							lasinfo.aclpb, info.member[j],
							lasinfo.clientDn, ACLLAS_CACHE_ALL_GROUPS,
							lasinfo.aclpb->aclpb_clientcert); 
					if (matched == ACL_TRUE) {
                		break;
            		} else if ( matched == ACL_DONT_KNOW ) {
                		/* record this but keep going--maybe another group will evaluate to TRUE */
                		got_undefined = 1;
            		}
				}
				/* Deallocate the member array and the member struct */
				for (j=0; j < info.numofGroups; j++)
					slapi_ch_free ((void **) &info.member[j]);
				slapi_ch_free ((void **) &info.member);
		   	}
		   	if (matched == ACL_TRUE) {
				slapi_log_error( SLAPI_LOG_ACL, plugin_name,
						"groupdnattr matches at level (%d)\n", levels[i]);
				break;
			} else if ( matched == ACL_DONT_KNOW ) {
                /* record this but keep going--maybe another group at another level
				 * will evaluate to TRUE.
				*/
                got_undefined = 1;
            }

		} /* NumofLevels */
	}
	if (s_attrName) slapi_ch_free ((void**) &s_attrName );

	/*
	 * If no terms were undefined, then evaluate as normal.
	 * If there was an undefined term, but another one was TRUE, then we also evaluate
	 * as normal.  Otherwise, the whole expression is UNDEFINED.
	*/
	if ( matched == ACL_TRUE || !got_undefined ) {
		if (comparator == CMP_OP_EQ) {
			rc = (matched == ACL_TRUE  ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
		} else {
			rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
		}
	} else {
		rc = LAS_EVAL_FAIL;
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			"Returning UNDEFINED for groupdnattr evaluation.\n");
	} 

	return rc;
}

/*
 * acllas__eval_memberGroupDnAttr
 *
 * return ACL_TRUE, ACL_FALSE or ACL_DONT_KNOW
 * 
 *	Inverse group evaluation. Find all the groups that the user is a 
 *	member of. Find all teh groups that contain those groups. Do an
 *	upward nested level search. By the end of it, we will know all the
 *	groups that the clinet is a member of under that search scope.
 *
 *	This model seems to be very fast if we have few groups at the
 *	leaf level.
 *
 */
static int
acllas__eval_memberGroupDnAttr (char *attrName, Slapi_Entry *e,
				char *n_clientdn, struct acl_pblock *aclpb)
{

	Slapi_Attr		*attr;
	char			*s, *p;
	char			*str, *s_str, *base, *groupattr = NULL;
	int				i,j,k,matched, enumerate_groups;
	aclUserGroup	*u_group;
	char			ebuf [ BUFSIZ ];
	Slapi_Value     *sval=NULL;
	const struct berval	*attrVal;

	/* Parse the URL -- getting the group attr and counting up '?'s.
	 * If there is no group attr and there are 3 '?' marks,
	 * we parse the URL with ldap_url_parse to get base dn and filter.
	 */ 
	s_str = str = slapi_ch_strdup(attrName);
	while (str && ldap_utf8isspace(str)) LDAP_UTF8INC( str );
	str +=8;
	s = strchr (str, '?');
	if (s) {
		p = s;
		p++;
		*s = '\0';
		base = str;
		s = strchr (p, '?');
		if (s) *s = '\0';

		groupattr = p;
	} else {
		slapi_ch_free ( (void **)&s_str );
		return ACL_FALSE;
	}

	if ( (u_group = aclg_get_usersGroup ( aclpb , n_clientdn )) == NULL) {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"Failed to find/allocate a usergroup--aborting evaluation\n");
		slapi_ch_free ( (void **)&s_str );
		return(ACL_DONT_KNOW);
	}

	/*
	** First find out if we have already searched this base or 
	** if we are searching a subtree to an already enumerated base.
	*/
	enumerate_groups = 1;
	for (j=0; j < aclpb->aclpb_numof_bases; j++) {
		if (slapi_dn_issuffix(aclpb->aclpb_grpsearchbase[j], base)) {
			enumerate_groups = 0;
			break;
		}
	}
			
	
	/* See if we have already enumerated all the groups which the
	** client is a member of.
	*/
	if (enumerate_groups) {
		char			filter_str[BUFSIZ];
		char			*attrs[3];
		struct eval_info	info = {0};
		char			*curMemberDn;
		int			Done = 0;
		int			ngr, tt;
		char		*normed = NULL;

		/* Add the scope to the list of scopes */
		if (aclpb->aclpb_numof_bases >= (aclpb->aclpb_grpsearchbase_size-1)) {
			aclpb->aclpb_grpsearchbase = (char **)
					slapi_ch_realloc ( 
					   (void *) aclpb->aclpb_grpsearchbase,
					   (aclpb->aclpb_grpsearchbase_size +
					         ACLPB_INCR_BASES) * 
					    sizeof (char *));
			aclpb->aclpb_grpsearchbase_size += ACLPB_INCR_BASES;
		}
		normed = slapi_create_dn_string("%s", base);
		if (NULL == normed) {
			slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
						"acllas__eval_memberGroupDnAttr: Invalid syntax: %s\n",
						base );
			slapi_ch_free ( (void **)&s_str );
			return ACL_FALSE;
		}
		aclpb->aclpb_grpsearchbase[aclpb->aclpb_numof_bases++] = normed;
		/* Set up info to do a search */
		attrs[0] = type_member;
		attrs[1] = type_uniquemember;
		attrs[2] = NULL;

		info.c_idx = info.lu_idx = 0;
		info.member = 
			(char **) slapi_ch_malloc (ACLLAS_MAX_GRP_MEMBER * sizeof(char *));
		curMemberDn = n_clientdn;

		while (!Done) {
			char *filter_str_ptr = &filter_str[0];
			char *new_filter_str = NULL;
			int lenf = strlen(curMemberDn)<<1;

			if (lenf > (BUFSIZ - 28)) { /* 28 for "(|(uniquemember=%s)(member=%s))" */
				new_filter_str = slapi_ch_malloc(lenf + 28);
				filter_str_ptr = new_filter_str;
			}

			/*
			** Search the db for groups that the client is a member of.
			** Once found cache it. cache only unique groups.
			*/
			tt = info.lu_idx;
			sprintf (filter_str_ptr,"(|(uniquemember=%s)(member=%s))", 
						curMemberDn, curMemberDn); 

			/* Use new search internal API */
			{
				Slapi_PBlock *aPb = slapi_pblock_new ();
				/*
		 		 * This search may NOT be chained--we demand that group
		 		 * definition be local.
				*/			
				slapi_search_internal_set_pb (  aPb,
								base,
								LDAP_SCOPE_SUBTREE,
								filter_str_ptr,
								&attrs[0],
								0,
								NULL /* controls */,
								NULL /* uniqueid */,
								aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
								SLAPI_OP_FLAG_NEVER_CHAIN /* actions */);	
				slapi_search_internal_callback_pb(aPb,
								  &info /* callback_data */,
								  NULL/* result_callback */,
								  acllas__add_allgroups,
								  NULL /* referral_callback */);
				slapi_pblock_destroy (aPb);
			}

			if (new_filter_str) slapi_ch_free((void **) &new_filter_str);

			if (tt == info.lu_idx) {
				slapi_log_error( SLAPI_LOG_ACL, plugin_name, "currDn:(%s) \n\tNO MEMBER ADDED\n", 
								ACL_ESCAPE_STRING_WITH_PUNCTUATION (curMemberDn, ebuf));
			} else {
				for (i=tt; i < info.lu_idx; i++)
					slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
						"currDn:(%s) \n\tADDED MEMBER[%d]=%s\n", 
						ACL_ESCAPE_STRING_WITH_PUNCTUATION (curMemberDn, ebuf), i, info.member[i]);
			}

			if (info.c_idx >= info.lu_idx) {
				for (i=0; i < info.lu_idx; i++) {
				   int already_cached = 0;
				   for (j=0; j < u_group->aclug_numof_member_group; 
									j++){
					if (slapi_utf8casecmp(
								(ACLUCHP)info.member[i],
								(ACLUCHP)u_group->aclug_member_groups[j]) == 0) {
						slapi_ch_free ((void **) &info.member[i] );
						info.member[i] = NULL;
						already_cached = 1;
						break;
					}
					
                                   }

				   if (already_cached) continue;

				   ngr = u_group->aclug_numof_member_group++;
				   if (u_group->aclug_numof_member_group >= 
					   u_group->aclug_member_group_size){
					   u_group->aclug_member_groups =
					       (char **) slapi_ch_realloc (
				                 (void *) u_group->aclug_member_groups,
				                 (u_group->aclug_member_group_size +
				                   ACLUG_INCR_GROUPS_LIST) * sizeof(char *));

					   u_group->aclug_member_group_size += 
							    ACLUG_INCR_GROUPS_LIST;
				   }
				   u_group->aclug_member_groups[ngr] = info.member[i];
				   info.member[i] = NULL;
				}
				slapi_ch_free ((void **) &info.member);
				Done = 1;
			} else {
				curMemberDn = info.member[info.c_idx];
				info.c_idx++;
			}
		}
	}

	for (j=0; j < u_group->aclug_numof_member_group; j++)
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
				"acllas__eval_memberGroupDnAttr:GROUP[%d] IN CACHE:%s\n", 
					j, ACL_ESCAPE_STRING_WITH_PUNCTUATION (u_group->aclug_member_groups[j], ebuf));

	matched = ACL_FALSE;
	slapi_entry_attr_find( e, groupattr, &attr);
	if (attr == NULL) {
		slapi_ch_free ( (void **)&s_str );
		return ACL_FALSE;
	}
	k = slapi_attr_first_value ( attr,&sval );
	while ( k != -1 ) {
        char *n_attrval;
		attrVal = slapi_value_get_berval ( sval );
		n_attrval = slapi_create_dn_string("%s", attrVal->bv_val);
		if (NULL == n_attrval) {
			slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
						"acllas__eval_memberGroupDnAttr: Invalid syntax: %s\n",
						attrVal->bv_val );
			slapi_ch_free ( (void **)&s_str );
			return ACL_FALSE;
		}

		/*  We support: The attribute value can be a USER or a GROUP.
		** Let's compare with the client, thi might be just an user. If it is not
		** then we test it against the list of groups.
		*/
		if (slapi_utf8casecmp ((ACLUCHP)n_attrval, (ACLUCHP)n_clientdn) == 0 ) {
			matched = ACL_TRUE;
			slapi_ch_free ( (void **)&n_attrval );
			break;
		}
		for (j=0; j <u_group->aclug_numof_member_group; j++) {
			if ( slapi_utf8casecmp((ACLUCHP)n_attrval, 
									(ACLUCHP)u_group->aclug_member_groups[j]) == 0) {
				matched = ACL_TRUE;
				break;
			}
		}
		slapi_ch_free ( (void **)&n_attrval );
		if (matched == ACL_TRUE) break;
		k= slapi_attr_next_value ( attr, k, &sval );
	}
	slapi_ch_free ( (void **)&s_str );
	return matched;
}

static int
acllas__add_allgroups (Slapi_Entry* e, void *callback_data)
{
	int						i, n, m;
	struct eval_info        *info;
	char					*n_dn;

	info = (struct eval_info *) callback_data;

	/*
	** Once we are here means this is a valid group. First see
	** If we have already seen this group. If not, add it to the
	** member list.
	*/
	n_dn = slapi_ch_strdup ( slapi_entry_get_ndn ( e ) );
	for (i=0; i < info->lu_idx; i++) {
		if (slapi_utf8casecmp((ACLUCHP)n_dn, (ACLUCHP)info->member[i]) == 0) {
			slapi_ch_free ( (void **) &n_dn);
			return 0;
		}
	}

	m = info->lu_idx;
	n = ++info->lu_idx;
	if (!(n % ACLLAS_MAX_GRP_MEMBER)) {
		info->member = (char **) slapi_ch_realloc (
					(void *) info->member,
					(n+ACLLAS_MAX_GRP_MEMBER) * sizeof(char *));
	}
	info->member[m] = n_dn;
	return 0;
}
/*
 * 
 * acllas__dn_parent
 * 
 *   This code should belong to dn.c. However this is specific to acl and I had 
 *   2 choices 1) create a new API or 2) reuse the slapi_dN_parent
 *
 *   Returns a ptr to the parent based on the level.
 *
 */
#define DNSEPARATOR(c)  (c == ',' || c == ';')
static char*
acllas__dn_parent( char *dn, int level)
{
	char	*s, *dnstr;
	int	inquote;
	int	curLevel;
	int	lastLoop = 0;

	if ( dn == NULL || *dn == '\0' ) {
		return( NULL );
	}

	/* An X.500-style name, which looks like  foo=bar,sha=baz,... */
	/* Do we have any dn seprator or not */
	if ((strchr(dn,',') == NULL) && (strchr(dn,';') == NULL))
		return (NULL);

	inquote = 0;
	curLevel = 1;
	dnstr = dn;
	while ( curLevel <= level) {
		if (lastLoop) break;
		if (curLevel == level) lastLoop = 1;
		for ( s = dnstr; *s; s++ ) {
			if ( *s == '\\' ) {
				if ( *(s + 1) )
					s++;
				continue;
			}
			if ( inquote ) {
				if ( *s == '"' )
					inquote = 0;
			} else {
				if ( *s == '"' )
					inquote = 1;
				else if ( DNSEPARATOR( *s ) ) {
					if (curLevel == level)
						return(  s + 1 );
					dnstr = s + 1;
					curLevel++;
					break;
				}
			}
		}
		if ( *s == '\0') {
			/* Got to the end of the string without reaching level,
			 * so return NULL.
			*/
			return(NULL);
		}
	}

	return( NULL );
}
/*
 * acllas__verify_client
 *
 * returns 1 if the attribute exists in the entry and
 * it's value is equal to the client Dn.
 * If the attribute is not in the entry, or it is and the
 * value differs from the clientDn then returns FALSE.
 *  
 *  Verify if client's DN is stored in the attrbute or not.
 *  This is a handler from a search being done at
 *  DS_LASUserDnAttrEval().
 *
 */
static int
acllas__verify_client (Slapi_Entry* e, void *callback_data)
{

	Slapi_Attr		*attr;
	char			*val;
	struct userdnattr_info *info;
	Slapi_Value             *sval;
	const struct berval		*attrVal;
	int i;

	info = (struct userdnattr_info *) callback_data;
	
	slapi_entry_attr_find( e, info->attr, &attr);
	if (attr == NULL) return 0;

	i = slapi_attr_first_value ( attr,&sval );
	while ( i != -1 ) {
		attrVal = slapi_value_get_berval ( sval );
		val = slapi_create_dn_string("%s", attrVal->bv_val);
		if (NULL == val) {
			slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
							"acllas__verify_client: Invalid syntax: %s\n",
							attrVal->bv_val );
			return 0;
		}

		if (slapi_utf8casecmp((ACLUCHP)val, (ACLUCHP)info->clientdn ) == 0) {
			info->result = 1;
			slapi_ch_free ( (void **) &val);
			return 0;
		}
		slapi_ch_free ( (void **) &val);
		i = slapi_attr_next_value ( attr, i, &sval );
	}
	return 0;
}

/*
 * acllas__verify_ldapurl
 *
 * returns 1 if the attribute exists in the entry and
 * it's value is equal to the client Dn.
 * If the attribute is not in the entry, or it is and the
 * value differs from the clientDn then returns FALSE.
 *  
 *  Verify if client's entry includes the attribute value that
 *  matches the filter in LDAPURL
 *  This is a handler from a search being done at DS_LASLdapUrlAttrEval().
 *
 */
static int
acllas__verify_ldapurl(Slapi_Entry* e, void *callback_data)
{

	Slapi_Attr		*attr;
	struct userdnattr_info *info;
	Slapi_Value             *sval;
	const struct berval		*attrVal;
	int rc;

	info = (struct userdnattr_info *) callback_data;
	info->result = ACL_FALSE;

	rc = slapi_entry_attr_find( e, info->attr, &attr);
	if (rc != 0 || attr == NULL) {
		return 0;
	}

	rc = slapi_attr_first_value ( attr, &sval );
	if ( rc == -1 ) {
		return 0;
	}

	while (rc != -1 && sval != NULL) {
		attrVal = slapi_value_get_berval ( sval );
		info->result = acllas__client_match_URL ( info->aclpb, 
												  info->clientdn, 
							     				  attrVal->bv_val);
		if ( info->result == ACL_TRUE ) {
				return 0;
		}
		rc = slapi_attr_next_value ( attr, rc, &sval );
	}
	return 0;
}

/*
 *
 * acllas__get_members
 *
 *	Collects all the values of the specified attribute which should be group names.
 */
static int
acllas__get_members (Slapi_Entry* e, void *callback_data)
{

	Slapi_Attr		*attr;
	struct groupdnattr_info	*info;
	Slapi_Value             *sval=NULL;
	const struct berval		*attrVal;
	int			i;

	info = (struct groupdnattr_info *) callback_data;
	slapi_entry_attr_find (e, info->attrName, &attr);
	if ( !attr ) return 0;

	slapi_attr_get_numvalues ( attr, &info->numofGroups );
	    
	info->member = (char **) slapi_ch_malloc (info->numofGroups * sizeof(char *));
	i = slapi_attr_first_value ( attr,&sval );
	while ( i != -1 ) {
	    attrVal =slapi_value_get_berval ( sval );
	    info->member[i] = slapi_create_dn_string ("%s", attrVal->bv_val);
		if (NULL == info->member[i]) {
			slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
							"acllas__get_members: Invalid syntax: %s\n",
							attrVal->bv_val );
		}
	    i = slapi_attr_next_value ( attr, i, &sval );
	}
	return 0;	
}

/*
 * DS_LASUserAttrEval
 *	LAS to evaluate the userattr rule
 *
 *   userAttr = "attrName#Type"
 *
 *   <Type> ::= "USERDN" | "GROUPDN" | "ROLEDN" | "LDAPURL" | <value>
 *   <value>::== <any printable String>
 *
 *   Example:
 *		userAttr = "manager#USERDN"    --- same as userdnattr
 *		userAttr = "owner#GROUPDN"     --- same as groupdnattr
 *				 = "ldap:///o=sun.com?owner#GROUPDN 
 * 		userAttr = "attr#ROLEDN"       --- The value of attr contains a roledn
 * 		userAttr = "myattr#LDAPURL"    --- The value contains a LDAP URL
 *											which can have scope and filter
 *											bits.
 *	    userAttr = "OU#Directory Server"
 * 									--- In this case the client's OU and the
 * 									    resource entry's OU must have
 * 										"Directory Server" value.
 *
 * Returns:
 *	retcode	        The usual LAS return codes.
 */
int 
DS_LASUserAttrEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, 
		char *attr_pattern, int *cachable, void **LAS_cookie, 
		PList_t subject, PList_t resource, PList_t auth_info,
		PList_t global_auth)
{

	char			*attrName;
	char			*attrValue = NULL;
	int				rc;
	int				matched = ACL_FALSE;
	char			*p;
	lasInfo			lasinfo;
	int				got_undefined = 0;

	if ( 0 !=  (rc = __acllas_setup (errp, attr_name, comparator, 0, /* Don't allow range comparators */
									attr_pattern,cachable,LAS_cookie,
									subject, resource, auth_info,global_auth,
									DS_LAS_USERATTR, "DS_LASUserAttrEval", 
									&lasinfo )) ) {
		return LAS_EVAL_FAIL;
	}

	/* Which rule are we evaluating ? */
	attrName = slapi_ch_strdup (attr_pattern );
	if ( NULL  == (p = strchr ( attrName, '#' ))) {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			  "DS_LASUserAttrEval:Invalid value(%s)\n", attr_pattern);
		slapi_ch_free ( (void **) &attrName );
		return LAS_EVAL_FAIL;
	}
	attrValue = p;
	attrValue++; /* skip the # */
	*p = '\0';  /* null terminate the attr name */

	if  ( 0 == strncasecmp ( attrValue, "USERDN", 6)) {
		matched = DS_LASUserDnAttrEval (errp,DS_LAS_USERDNATTR, comparator,
							attrName, cachable, LAS_cookie,
							subject, resource, auth_info, global_auth);
		goto done_las;
	} else if  ( 0 == strncasecmp ( attrValue, "GROUPDN", 7)) {
		matched = DS_LASGroupDnAttrEval (errp,DS_LAS_GROUPDNATTR, comparator,
							attrName, cachable, LAS_cookie,
							subject, resource, auth_info, global_auth);
		goto done_las;
	} else if  ( 0 == strncasecmp ( attrValue, "LDAPURL", 7) ) {
		matched = DS_LASLdapUrlAttrEval(errp, DS_LAS_USERATTR, comparator,
							attrName, cachable, LAS_cookie,
							subject, resource, auth_info, global_auth, lasinfo);
		goto done_las;
	} else if  ( 0 == strncasecmp ( attrValue, "ROLEDN", 6)) {
		matched = DS_LASRoleDnAttrEval (errp,DS_LAS_ROLEDN, comparator,
							attrName, cachable, LAS_cookie,
							subject, resource, auth_info, global_auth);
		goto done_las;
	}

	if ( lasinfo.aclpb && ( NULL == lasinfo.aclpb->aclpb_client_entry )) {
		/* SD 00/16/03 pass NULL in case the req is chained */
		char **attrs=NULL;

		/* Use new search internal API */
		Slapi_PBlock *aPb = slapi_pblock_new ();
		/*
		 * This search may be chained if chaining for ACL is
		 * is enabled in the backend and the entry is in
		 * a chained backend.
		 */
		slapi_search_internal_set_pb (  aPb,
						lasinfo.clientDn,
						LDAP_SCOPE_BASE,
						"objectclass=*",
						attrs,
						0,
						NULL /* controls */,
						NULL /* uniqueid */,
						aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
						0 /* actions */);
		slapi_search_internal_callback_pb(aPb,
						  lasinfo.aclpb /* callback_data */, 
						  NULL/* result_callback */,
						  acllas__handle_client_search,
						  NULL /* referral_callback */);
		slapi_pblock_destroy (aPb);
						
	}

	slapi_log_error ( SLAPI_LOG_ACL, plugin_name, 
				"DS_LASUserAttrEval: AttrName:%s, attrVal:%s\n", attrName, attrValue );

	/*
	 * Here it's the userAttr = "OU#Directory Server" case.
	 * Allocate the Slapi_Value on the stack and init it by reference
	 * to avoid having to malloc and free memory.
	*/
	Slapi_Value v;
	
	slapi_value_init_string_passin(&v, attrValue);
	rc = slapi_entry_attr_has_syntax_value ( lasinfo.resourceEntry, attrName,
									 &v );
	if (rc) {
	   rc = slapi_entry_attr_has_syntax_value ( 
									lasinfo.aclpb->aclpb_client_entry, 
									attrName, &v );
		if (rc) matched = ACL_TRUE;
	}
	/* Nothing to free--cool */				

	/*
	 * Find out what the result is, in
	 * this case matched is one of ACL_TRUE, ACL_FALSE or ACL_DONT_KNOW
	 * and got_undefined says whether a logical term evaluated to ACL_DONT_KNOW.
	 *
	 */
	if ( matched == ACL_TRUE || !got_undefined) {
		if (comparator == CMP_OP_EQ) {
			rc = (matched == ACL_TRUE ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
		} else {
			rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
		}
	} else {
		rc = LAS_EVAL_FAIL;
	}

	slapi_ch_free ( (void **) &attrName );
	return rc;

done_las:
	/*
	 * In this case matched is already LAS_EVAL_TRUE or LAS_EVAL_FALSE or
	 * LAS_EVAL_FAIL.
	*/
	if ( matched != LAS_EVAL_FAIL ) {
		if (comparator == CMP_OP_EQ) {
			rc = matched;
		} else {
			rc = (matched == LAS_EVAL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
		}
	} 

	slapi_ch_free ( (void **) &attrName );
	return rc;
}

/*
 * acllas__client_match_URL
 * 	Match a client to a URL.
 *
 * Returns:
 *	ACL_TRUE 		- matched the URL
 *	ACL_FALSE		- Sorry; no match
 *
 */
static int
acllas__client_match_URL (struct acl_pblock *aclpb, char *n_clientdn, char *url )
{

	LDAPURLDesc	*ludp = NULL;
	int		rc = 0;
	Slapi_Filter	*f = NULL;
	char *rawdn = NULL;
	char *dn = NULL;
	size_t dnlen = 0;
	char *p = NULL;
	char *normed = NULL;
	/* ldap(s)://host:port/suffix?attrs?scope?filter */
	const size_t 	LDAP_URL_prefix_len = strlen(LDAP_URL_prefix_core);
	const size_t 	LDAPS_URL_prefix_len = strlen(LDAPS_URL_prefix_core);
	size_t 	prefix_len = 0;
	char Q = '?';
	char *hostport = NULL;
	int result = ACL_FALSE;

	if ( NULL == aclpb ) {
		slapi_log_error (SLAPI_LOG_ACL, plugin_name, 
			"acllas__client_match_URL: NULL acl pblock\n");
		return ACL_FALSE;
	}

	/* Get the client's entry if we don't have already */
	if ( NULL == aclpb->aclpb_client_entry ) {
		/* SD 00/16/03 Get every attr in case req chained */
		char **attrs=NULL;

		/* Use new search internal API */
		Slapi_PBlock *  aPb = slapi_pblock_new ();
		/*
		 * This search may be chained if chaining for ACL is
		 * is enabled in the backend and the entry is in
		 * a chained backend.
		*/	
		slapi_search_internal_set_pb (  aPb,
						n_clientdn,
						LDAP_SCOPE_BASE,
						"objectclass=*",
						attrs,
						0,
						NULL /* controls */,
						NULL /* uniqueid */,
						aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
						0 /* actions */);
		slapi_search_internal_callback_pb(aPb,
						  aclpb /* callback_data */,
						  NULL/* result_callback */,
						  acllas__handle_client_search,
						  NULL /* referral_callback */);
		slapi_pblock_destroy (aPb);
	}

	if ( NULL == aclpb->aclpb_client_entry ) {
		slapi_log_error (SLAPI_LOG_ACL, plugin_name, 
			"acllas__client_match_URL: Unable to get client's entry\n");
		goto done;
	}

	/* DN potion of URL must be normalized before calling ldap_url_parse.
	 * lud_dn is pointing at the middle of lud_string.
	 * lud_dn won't be freed in ldap_free_urldesc.
	 */
	/* remove the "ldap{s}:///" part */
	if (strncasecmp (url, LDAP_URL_prefix, LDAP_URL_prefix_len) == 0) {
		prefix_len = LDAP_URL_prefix_len;
	} else if (strncasecmp (url, LDAPS_URL_prefix, LDAPS_URL_prefix_len) == 0) {
		prefix_len = LDAPS_URL_prefix_len;
	} else {
		slapi_log_error (SLAPI_LOG_ACL, plugin_name, 
			"acllas__client_match_URL: url %s does not have a recognized ldap protocol prefix\n", url);
		goto done;
	}
	rawdn = url + prefix_len; /* ldap(s)://host:port/... or ldap(s):///... */
	                          /* rawdn at  ^             or           ^    */
	/* let rawdn point the suffix */
	if ('/' == *(rawdn+1)) { /* ldap(s):/// */
		rawdn += 2;
	} else {
		char *tmpp = rawdn;
		rawdn = strchr(tmpp, '/');
		size_t hostport_len = 0;
		if (NULL == rawdn) {
			slapi_log_error (SLAPI_LOG_ACL, plugin_name, 
				"acllas__client_match_URL: url %s does not have a valid ldap protocol prefix\n", url);
			goto done;
		}
		hostport_len = ++rawdn - tmpp; /* ldap(s)://host:port/... */
		                               /*           <-------->    */
		hostport = (char *)slapi_ch_malloc(hostport_len + 1);
		memcpy(hostport, tmpp, hostport_len);
		*(hostport+hostport_len) = '\0';
	}
	p = strchr(rawdn, Q);
	if (p) { 
		/* url has scope and/or filter: ldap(s):///suffix?attr?scope?filter */
		*p = '\0'; /* null terminate the dn part of rawdn */
	}
	rc = slapi_dn_normalize_ext(rawdn, 0, &dn, &dnlen);
	if (rc < 0) {
		slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
						 "acllas__client_match_URL: error normalizing dn [%s] part of URL [%s]\n", rawdn, url);
		goto done;
	} else if (rc == 0) { /* url is passed in and not terminated with NULL*/
		*(dn + dnlen) = '\0';
	}
	/* else - rawdn normalized in place */
	normed = slapi_ch_smprintf("%s%s%s%s%s", 
			 (prefix_len==LDAP_URL_prefix_len)?
			  LDAP_URL_prefix_core:LDAPS_URL_prefix_core,
							   hostport?hostport:"", dn, p?"?":"",p?p+1:"");
	if (p) {
		*p = Q; /* put the Q back in rawdn which will un-null terminate the DN part */
	}
	if (rc > 0) {
		/* dn was allocated in slapi_dn_normalize_ext */
		slapi_ch_free_string(&dn);
	}
	rc = slapi_ldap_url_parse(normed, &ludp, 1, NULL);
	if (rc) {
		slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
						 "acllas__client_match_URL: url [%s] is invalid: %d (%s)\n",
						 normed, rc, slapi_urlparse_err2string(rc));
		goto done;
	}
	if ( ( NULL == ludp->lud_dn) || ( NULL == ludp->lud_filter) ) {
		slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
						 "acllas__client_match_URL: url [%s] has no base dn [%s] or filter [%s]\n",
						 normed,
						 NULL == ludp->lud_dn ? "null" : ludp->lud_dn,
						 NULL == ludp->lud_filter ? "null" :  ludp->lud_filter );
		goto done;
	}

	/* Check the scope */
	if ( ludp->lud_scope == LDAP_SCOPE_SUBTREE ) {
		if (!slapi_dn_issuffix(n_clientdn, ludp->lud_dn)) {
			slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
							 "acllas__client_match_URL: url [%s] scope is subtree but dn [%s] "
							 "is not a suffix of [%s]\n",
							 normed, ludp->lud_dn, n_clientdn );
			goto done;
		}
	} else if ( ludp->lud_scope == LDAP_SCOPE_ONELEVEL ) {
		char    *parent = slapi_dn_parent (n_clientdn);

		if (slapi_utf8casecmp ((ACLUCHP)parent, (ACLUCHP)ludp->lud_dn) != 0 ) {
			slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
							 "acllas__client_match_URL: url [%s] scope is onelevel but dn [%s] "
							 "is not a direct child of [%s]\n",
							 normed, ludp->lud_dn, parent );
			slapi_ch_free_string(&parent);
			goto done;
		}
		slapi_ch_free_string(&parent);
	} else  { /* default */
		if (slapi_utf8casecmp ( (ACLUCHP)n_clientdn, (ACLUCHP)ludp->lud_dn) != 0 ) {
			slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
							 "acllas__client_match_URL: url [%s] scope is base but dn [%s] "
							 "does not match [%s]\n",
							 normed, ludp->lud_dn, n_clientdn );
			goto done;
		}

	} 

	/* Convert the filter string */
	f = slapi_str2filter ( ludp->lud_filter );

	if (ludp->lud_filter && (f == NULL)) { /* bogus filter */
		slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
						"DS_LASUserAttrEval: The member URL [%s] search filter in entry [%s] is not valid: [%s]\n",
						normed, n_clientdn, ludp->lud_filter);
		goto done;
    }

	result = ACL_TRUE;
	if (f && (0 != slapi_vattr_filter_test ( aclpb->aclpb_pblock, 
				aclpb->aclpb_client_entry, f, 0 /* no acces chk */ )))
		result = ACL_FALSE;

done:
	slapi_ch_free_string(&hostport);
	ldap_free_urldesc( ludp );
	slapi_ch_free_string(&normed);
	slapi_filter_free ( f, 1 ) ;

	return result;
}
static int
acllas__handle_client_search ( Slapi_Entry *e, void *callback_data )
{
        struct acl_pblock *aclpb = (struct acl_pblock *) callback_data;

        /* If we are here means we have found the entry */
	if ( NULL == aclpb-> aclpb_client_entry)
		aclpb->aclpb_client_entry = slapi_entry_dup ( e );
        return 0;
}
/*
*
* Do all the necessary setup for all the
* LASes.
* It will only fail if it's passed garbage (which should not happen) or
* if the data it needs to stock the lasinfo is not available, which
* also should not happen.
*
*
* Return value: 0 or one of these
* 	#define LAS_EVAL_TRUE       -1
* 	#define LAS_EVAL_FALSE      -2
* 	#define LAS_EVAL_DECLINE    -3
* 	#define LAS_EVAL_FAIL       -4
* 	#define LAS_EVAL_INVALID    -5
*/

static int
__acllas_setup ( NSErr_t *errp, char *attr_name, CmpOp_t comparator,
		int allow_range, char *attr_pattern, int *cachable, void **LAS_cookie,
        PList_t subject, PList_t resource, PList_t auth_info,
        PList_t global_auth, char *lasType, char*lasName, lasInfo *linfo)
{

	int		rc;
	memset ( linfo, 0, sizeof ( lasInfo) );

  	*cachable = 0;
  	*LAS_cookie = (void *)0;

	if (strcmp(attr_name, lasType) != 0) {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			  "%s:Invalid LAS(%s)\n", lasName, attr_name);
		return LAS_EVAL_INVALID;
	}

	/* Validate the comparator */
	if (allow_range && (comparator != CMP_OP_EQ) && (comparator != CMP_OP_NE) &&
	    (comparator != CMP_OP_GT) && (comparator != CMP_OP_LT) &&
	    (comparator != CMP_OP_GE) && (comparator != CMP_OP_LE)) {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"%s:Invalid comparator(%d)\n", lasName, (int)comparator);
		return LAS_EVAL_INVALID;
	} else if (!allow_range && (comparator != CMP_OP_EQ) && (comparator != CMP_OP_NE)) {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			"%s:Invalid comparator(%d)\n", lasName, (int)comparator);
		return LAS_EVAL_INVALID;
	}

    
	/* Get the client DN */
	rc = ACL_GetAttribute(errp, DS_ATTR_USERDN, (void **)&linfo->clientDn,
			      subject, resource, auth_info, global_auth);

	if ( rc != LAS_EVAL_TRUE ) {
		acl_print_acllib_err(errp, NULL);
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
		    "%s:Unable to get the clientdn attribute(%d)\n",lasName, rc);
		return LAS_EVAL_FAIL;
	}

	/* Check if we have a user or not */
	if (linfo->clientDn) {
		/* See if it's a anonymous user */
		if (*(linfo->clientDn) == '\0') 
			linfo->anomUser = ACL_TRUE;
	} else {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
			   "%s: No user\n",lasName);
		return LAS_EVAL_FAIL;
	}

	if ((rc = PListFindValue(subject, DS_ATTR_ENTRY, 
					(void **)&linfo->resourceEntry, NULL)) < 0)	{
		acl_print_acllib_err(errp, NULL);
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
		          "%s:Unable to get the Slapi_Entry attr(%d)\n",lasName, rc);
		return LAS_EVAL_FAIL;
	}

	/* Get ACLPB */
	rc = ACL_GetAttribute(errp, DS_PROP_ACLPB, (void **)&linfo->aclpb,
				subject, resource, auth_info, global_auth);
	if ( rc != LAS_EVAL_TRUE ) {
		acl_print_acllib_err(errp, NULL);
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"%s:Unable to get the ACLPB(%d)\n", lasName, rc);
		return LAS_EVAL_FAIL;
	}
	if (NULL == attr_pattern ) {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
		          "%s:No rule value in the ACL\n", lasName);
	
		return LAS_EVAL_FAIL;
	}
	/* get the  authentication type */
	if ((rc = PListFindValue(subject, DS_ATTR_AUTHTYPE, 
					(void **)&linfo->authType, NULL)) < 0) {
		acl_print_acllib_err(errp, NULL);
		slapi_log_error( SLAPI_LOG_ACL, plugin_name, 
		          "%s:Unable to get the auth type(%d)\n", lasName, rc);
		return LAS_EVAL_FAIL;
	}

	/* get the SSF */
	if ((rc = PListFindValue(subject, DS_ATTR_SSF,
					(void **)&linfo->ssf, NULL)) < 0) {
		acl_print_acllib_err(errp, NULL);
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
			"%s:Unable to get the ssf(%d)\n", lasName, rc);
	}
	return 0;	
}

/*
 * See if clientDN has role roleDN.
 * Here we know the user is not anon and that the role
 * is not the anyone role ie. it's actually worth invoking the roles code.
*/

static int acllas__user_has_role( struct acl_pblock *aclpb,
								  Slapi_DN *roleDN, Slapi_DN *clientDn) {

	int present = 0;

	if ( NULL == aclpb ) {
		slapi_log_error (  SLAPI_LOG_ACL, plugin_name, 
			"acllas__user_has_role: NULL acl pblock\n");
		return ACL_FALSE;
	}

	/* Get the client's entry if we don't have already */
	if ( NULL == aclpb->aclpb_client_entry ) {
		/* SD 00/16/03 Get every attr in case req chained */
		char **attrs=NULL;

		/* Use new search internal API */
		Slapi_PBlock *  aPb = slapi_pblock_new ();
		/*
		 * This search may NOT be chained--the user and the role definition
		 * must be co-located (chaining is not supported for the roles
		 * plugin in 5.0
		*/
		slapi_search_internal_set_pb (  aPb,
						slapi_sdn_get_ndn(clientDn),
						LDAP_SCOPE_BASE,
						"objectclass=*",
						attrs,
						0,
						NULL /* controls */,
						NULL /* uniqueid */,
						aclplugin_get_identity (ACL_PLUGIN_IDENTITY),
						SLAPI_OP_FLAG_NEVER_CHAIN /* actions */);
		slapi_search_internal_callback_pb(aPb,
						  aclpb /* callback_data */,
						  NULL/* result_callback */,
						  acllas__handle_client_search,
						  NULL /* referral_callback */);
		slapi_pblock_destroy (aPb);
	}

	if ( NULL == aclpb->aclpb_client_entry ) {
		slapi_log_error (  SLAPI_LOG_ACL, plugin_name, 
			"acllas__user_has_role: Unable to get client's entry\n");
		return ACL_FALSE;
	}

	/* If the client has the role then it's a match, otherwise no */

	slapi_role_check( aclpb->aclpb_client_entry, roleDN, &present);
	if ( present ) {
		return(ACL_TRUE);
	}							

	return(ACL_FALSE);
}

int
DS_LASRoleDnAttrEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator, 
		char *attr_pattern, int *cachable, void **LAS_cookie, 
		PList_t subject, PList_t resource, PList_t auth_info,
		PList_t global_auth)
{

	char			*attrName;
	int				matched;
	int				rc;
	Slapi_Attr 		*attr;
	lasInfo			lasinfo;
	Slapi_Value     *sval=NULL;
	const struct berval	*attrVal;
	int				k=0;
	int				got_undefined = 0;

	if ( 0 !=  (rc = __acllas_setup (errp, attr_name, comparator, 0, /* Don't allow range comparators */
									attr_pattern,cachable,LAS_cookie,
									subject, resource, auth_info,global_auth,
									DS_LAS_ROLEDN, "DS_LASRoleDnAttrEval", 
									&lasinfo )) ) {
		return LAS_EVAL_FAIL;
	}

	/* For anonymous client, they have no roles so the match is false. */
	if ( lasinfo.anomUser )
		return LAS_EVAL_FALSE;

	/* 
	**
	** The function of this LAS is to  find out if the client has
	** the role specified in the attr.
	** attr_pattern looks like: "ROLEDN cn=role1,o=sun.com"
	*/
	attrName = attr_pattern;

	matched = ACL_FALSE;
	slapi_entry_attr_find( lasinfo.resourceEntry, attrName, &attr);
	if (attr == NULL) {
		/*
		 * Here the entry does not contain the attribute so the user
		 * cannot have this "null" role 
		*/
		return LAS_EVAL_FALSE;
	}

	if (lasinfo.aclpb->aclpb_optype == SLAPI_OPERATION_ADD) {
		/*
		 * Here the entry does not contain the attribute so the user
		 * cannot have this "null" role or
		 * For the add operation, the resource itself 
		 * must never be allowed to grant access--
		 * This is because access would be granted based on a value
		 * of an attribute in the new entry--security hole.
		 * XXX is this therefore FALSE or DONT_KNOW ? 
		 *
		 * 
		*/
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
				"ACL info: userattr=XXX#ROLEDN does not allow ADD permission.\n");
		got_undefined = 1;
	} else {

		/*
		 * Got the first value.
		 * Test all the values of this attribute--if the client has _any_
		 * of the roles then it's a match.
		*/
		k = slapi_attr_first_value ( attr,&sval );
		while ( k != -1 ) {
	        char *n_attrval;
			Slapi_DN *roleDN;

			attrVal = slapi_value_get_berval ( sval );
			n_attrval = slapi_create_dn_string("%s", attrVal->bv_val);
			if (NULL == n_attrval) {
				slapi_log_error( SLAPI_LOG_FATAL, plugin_name,
							"DS_LASRoleDnAttrEval: Invalid syntax: %s\n",
							attrVal->bv_val );
				return LAS_EVAL_FAIL;
			}
			roleDN = slapi_sdn_new_dn_byval(n_attrval);

			/*  We support: The attribute value can be a USER or a GROUP.
			** Let's compare with the client, thi might be just an user. If it is not
			** then we test it against the list of groups.
			*/
			matched = acllas__user_has_role(lasinfo.aclpb,
							roleDN, lasinfo.aclpb->aclpb_authorization_sdn);
			slapi_ch_free ( (void **)&n_attrval );
			slapi_sdn_free(&roleDN);
			if (matched == ACL_TRUE) {
				break;
			} else if ( matched == ACL_DONT_KNOW ) {
				/* record this but keep going--maybe another group will evaluate to TRUE */
				got_undefined = 1;
			}
			k= slapi_attr_next_value ( attr, k, &sval );
		}/* while */
	}

	/*
	 * If no terms were undefined, then evaluate as normal.
	 * If there was an undefined term, but another one was TRUE, then we also evaluate
	 * as normal.  Otherwise, the whole expression is UNDEFINED.
	*/
	if ( matched == ACL_TRUE || !got_undefined ) {
		if (comparator == CMP_OP_EQ) {
			rc = (matched == ACL_TRUE  ? LAS_EVAL_TRUE : LAS_EVAL_FALSE);
		} else {
			rc = (matched == ACL_TRUE ? LAS_EVAL_FALSE : LAS_EVAL_TRUE);
		}
	} else {
		rc = LAS_EVAL_FAIL;
	} 
	return (rc);
}

/*
 * Here, determine if lasinfo->clientDn matches user (which contains
 * a ($dn) or a $attr component or both.) As defined in the aci 
 * lasinfo->aclpb->aclpb_curr_aci,
 * which is the current aci being evaluated.
 * 
 * returns: ACL_TRUE for matched,
 * 			ACL_FALSE for matched.
 *			ACL_DONT_KNOW otherwise.
 */

int
aclutil_evaluate_macro( char * rule, lasInfo *lasinfo,
						acl_eval_types evalType )
{
	int matched = 0;
	aci_t *aci;
	char *matched_val = NULL;
	char **candidate_list = NULL;
	char **inner_list = NULL;	
	char **sptr = NULL;
	char **tptr = NULL;
	char *t = NULL;
	char *s = NULL;
	struct acl_pblock *aclpb = lasinfo->aclpb;

	aci = lasinfo->aclpb->aclpb_curr_aci;
	/* Get a pointer to the ndn in the resouirce */
	slapi_entry_get_ndn (  lasinfo->resourceEntry );

	/*
	 * First, get the matched value from the target resource.
	 * We have alredy done this matching once beofer at tasrget match time.
	 */

	slapi_log_error(SLAPI_LOG_ACL, plugin_name,
	 				"aclutil_evaluate_macro for aci '%s' index '%d'\n",
					aci->aclName, aci->aci_index );
	if ( aci->aci_macro == NULL ) {
		/* No $dn in the target, it's a $attr type subject rule */
		matched_val = NULL;
	} else {
	
		/*
		 * Look up the matched_val value calculated
		 * from the target and stored judiciously there for us.
		 */

		if ( (matched_val = (char *)acl_ht_lookup( aclpb->aclpb_macro_ht,
								(PLHashNumber)aci->aci_index)) == NULL) {
			slapi_log_error(SLAPI_LOG_ACL, plugin_name,
				"ACL info: failed to locate the calculated target"
				"macro for aci '%s' index '%d'\n",
				aci->aclName, aci->aci_index );
			return(ACL_FALSE); /* Not a match */
		} else {
			slapi_log_error(SLAPI_LOG_ACL, plugin_name,
				"ACL info: found matched_val (%s) for aci index %d"
				"in macro ht\n", 
				aci->aclName, aci->aci_index );
		}
	}

	/*
	 * Now, make a candidate
	 * list of strings to match against the client.
	 * This involves replacing ($dn) or [$dn] by either the matched
	 * value, or all the suffix substrings of matched_val.
	 * If there is no $dn then the candidate list is just
	 * user itself.
	 * 
	*/

	candidate_list = acllas_replace_dn_macro( rule, matched_val, lasinfo);

	sptr= candidate_list;
	while( *sptr != NULL && !matched) {

		s = *sptr;

		/*
		 * Now s may contain some $attr macros.
		 * So, make a candidate list, got by replacing each occurence
		 * of $attr with all the values that attribute has in
		 * the resource entry.
		*/

		inner_list = acllas_replace_attr_macro( s, lasinfo);

		tptr = inner_list;
		while( tptr && *tptr != NULL && (matched != ACL_TRUE) ){

			t = *tptr;

			/*
			 * Now, at last t is a candidate string we can
			 * match agains the client.
			 *
			 * $dn and $attr can appear in userdn, graoupdn and roledn
			 * rules, so we we need to decide which type we
			 * currently evaluating and evaluate that.
			 *
			 * If the string generated was undefined, eg it contained
			 * ($attr.ou) and the entry did not have an ou attribute,then
			 * the empty string is returned for this.  So it we find
			 * an empty string in the list, skip it--it does not match.
			*/
		
			if ( *t != '\0') {
				if ( evalType == ACL_EVAL_USER ) {
				
					matched = acllas_eval_one_user( lasinfo->aclpb,
												lasinfo->clientDn, t);
				} else if (evalType == ACL_EVAL_GROUP) {

					matched = acllas_eval_one_group(t, lasinfo);
				} else if (evalType == ACL_EVAL_ROLE) {
					matched = acllas_eval_one_role(t, lasinfo);
				} else if (evalType == ACL_EVAL_GROUPDNATTR) {
					matched = acllas__eval_memberGroupDnAttr(t, 
												lasinfo->resourceEntry, 
												lasinfo->clientDn, 
												lasinfo->aclpb);
				} else if ( evalType == ACL_EVAL_TARGET_FILTER) {

					matched = acllas_eval_one_target_filter(t,
									lasinfo->resourceEntry);
					
				}
			}
					
			tptr++;			

		}/*inner while*/
		charray_free(inner_list);

		sptr++;
	}/* outer while */

	charray_free(candidate_list);

	return(matched);

}

/*
 * Here, replace the first occurrence of $(dn) with matched_val.
 * replace any occurrence of $[dn] with each of the suffix substrings
 * of matched_val.
 * Each of these strings is returned in a NULL terminated list of strings. 
 *
 * If there is no $dn thing then the returned list just contains rule itself.
 *
 * eg. rule: cn=fred,ou=*, ($dn), o=sun.com
 *     matched_val: ou=People,o=icnc
 *
 * Then we return the list
 * cn=fred,ou=*,ou=People,o=icnc,o=sun.com NULL
 * 
 * eg. rule: cn=fred,ou=*,[$dn], o=sun.com
 *     matched_val: ou=People,o=icnc
 *
 * Then we return the list
 * cn=fred,ou=*,ou=People,o=icnc,o=sun.com 
 * cn=fred,ou=*,o=icnc,o=sun.com
 * NULL
 * 
 * 
*/

static char **
acllas_replace_dn_macro( char *rule, char *matched_val, lasInfo *lasinfo) {
	
	char **a = NULL;
	char *patched_rule = NULL;
	char *rule_to_use = NULL;
	char *new_patched_rule = NULL;	
	int	matched_val_len = 0;
	int j = 0;
	int has_macro_dn = 0;
	int has_macro_levels = 0;
	
	/* Determine what the rule's got once */
	if ( strstr(rule, ACL_RULE_MACRO_DN_KEY) != NULL) {
		/* ($dn) exists */
		has_macro_dn = 1;
	}

	if ( strstr(rule, ACL_RULE_MACRO_DN_LEVELS_KEY) != NULL) {
		/* [$dn] exists */
		has_macro_levels = 1;
	}

	if ( (!has_macro_dn && !has_macro_levels) || !matched_val ) { /* No ($dn) and no [$dn] ... */
		/* ... or no value to replace */
		/*
		 * No $dn thing, just return a list with two elements, rule and NULL.
		 * charray_add will create the list and null terminate it.		
		 */

		charray_add( &a, slapi_ch_strdup(rule));
		return(a);
	} else {

		/*
		 * Have an occurrence of the macro rules
		 *
		 * First, replace all occurrencers of ($dn) with the matched_val
		 */
		if ( has_macro_dn) {
			patched_rule =
				acl_replace_str(rule, ACL_RULE_MACRO_DN_KEY, matched_val);
		}

		/* If there are no [$dn] we're done */

		if ( !has_macro_levels ) {			
			charray_add( &a, patched_rule);
			return(a);
		} else {

			/*
		 	 * It's a [$dn] type, so walk matched_val, splicing in all
		 	 * the suffix substrings and adding each such string to
		 	 * to the returned list.
			 * get_next_component() does not return the commas--the
			 * prefix and suffix should come with their commas.
			 *
			 * All occurrences of each [$dn] are replaced with each level.
			 * 
			 * If has_macro_dn then patched_rule is the rule to strart with,
			 * and this needs to be freed at the end, otherwise
			 * just use rule.
			 */
	
			if (patched_rule) {
				rule_to_use = patched_rule;
			} else {
				rule_to_use = rule;
			}

			matched_val_len = strlen(matched_val);
			j = 0;
			
			while( j <  matched_val_len) {

				new_patched_rule = 
					acl_replace_str(rule_to_use, ACL_RULE_MACRO_DN_LEVELS_KEY, 
										&matched_val[j]);								
				charray_add( &a, new_patched_rule);										
	
				j += acl_find_comp_end(&matched_val[j]);
			}
 						
			if (patched_rule) {
					slapi_ch_free((void**)&patched_rule);
			}
			
			return(a);					
		} 
	}
}

/*
 * Here, replace any occurrence of $attr.attrname with the
 * value of attrname from lasinfo->resourceEntry.
 * 
 *
 * If there is no $attr thing then the returned list just contains rule
 * itself.
 *
 * eg. rule: cn=fred,ou=*,ou=$attr.ou,o=sun.com
 *     ou: People
 *	   ou: icnc 
 *
 * Then we return the list
 * cn=fred,ou=*,ou=People,o=sun.com
 * cn=fred,ou=*,ou=icnc,o=sun.com
 *  
*/

static char **
acllas_replace_attr_macro( char *rule, lasInfo *lasinfo)
{
	char **a = NULL;
	char **working_list = NULL;
	Slapi_Entry *e = lasinfo->resourceEntry;
	char *str, *working_rule;
	char *macro_str, *macro_attr_name;
	int l;
	Slapi_Attr *attr = NULL;
	
	str = strstr(rule, ACL_RULE_MACRO_ATTR_KEY);
	if ( str == NULL ) {

		charray_add(&a, slapi_ch_strdup(rule));
	    return(a);

	} else {
	
		working_rule = slapi_ch_strdup(rule);
		str = strstr(working_rule, ACL_RULE_MACRO_ATTR_KEY);
		charray_add(&working_list, working_rule );
		
		while( str != NULL) {

			/*
			 * working_rule is the first member of working_list.
			 * str points to the next $attr.attrName in working_rule.
			 * each member of working_list needs to have each occurence of
			 * $attr.atrName replaced with the value of attrName in e.
			 * If attrName is multi valued then this generates another
			 * list which replaces the old one.
			 */
			l = acl_strstr(&str[0], ")");
			macro_str = slapi_ch_malloc(l+2);
			strncpy( macro_str, &str[0], l+1);
			macro_str[l+1] = '\0';

			str = strstr(macro_str, ".");
			if (!str) {
				slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
						"acllas_replace_attr_macro: Invalid macro \"%s\".",
						macro_str);
				slapi_ch_free_string(&macro_str);
				charray_free(working_list);
				return NULL;
			}
 
			str++;								/* skip the . */
			l = acl_strstr(&str[0], ")");
			macro_attr_name = slapi_ch_malloc(l+1);
			strncpy( macro_attr_name, &str[0], l);
			macro_attr_name[l] = '\0';

        	slapi_entry_attr_find ( e, macro_attr_name, &attr );
        	if ( NULL == attr ) {
            	
				/*
				 * Here, if a $attr.attrName is such that the attrName
				 * does not occur in the entry then return a ""--
				 * this will go back to the matching code in 
				 * aclutil_evaluate_macro() where "" will
				 * be taken as the candidate.
				*/
				
				slapi_ch_free_string(&macro_str);
				slapi_ch_free_string(&macro_attr_name);
			
				charray_free(working_list);
				return NULL;
				
			} else{

				const struct berval *attrValue;
				Slapi_Value *sval;
               	int i, j;
				char *patched_rule;

	            i= slapi_attr_first_value ( attr, &sval );
    	        while(i != -1) {
        	    	attrValue = slapi_value_get_berval(sval);

					j = 0;
					while( working_list[j] != NULL) {
	 
						patched_rule =
							acl_replace_str(working_list[j], 
									macro_str, attrValue->bv_val);
                	    charray_add(&a, patched_rule);
						j++;
					}
							
                   i= slapi_attr_next_value( attr, i, &sval );
				}/* while */

				/*
				 * Here, a is working_list, where each member has had
				 * macro_str replaced with attrVal.  We hand a over,
				 * so we must set it to NULL since the working list
				 * may be free'd later. */

				charray_free(working_list);
				if (a == NULL) {
					/* This shouldn't happen, but we play
					 * if safe to avoid any problems. */
					slapi_ch_free_string(&macro_str);
					slapi_ch_free_string(&macro_attr_name);
					charray_add(&a, slapi_ch_strdup(""));
					return(a);
				} else {
					working_list = a;
					working_rule = a[0];
					a = NULL;
				}
			}
			slapi_ch_free_string(&macro_str);
			slapi_ch_free_string(&macro_attr_name);
			
			str = strstr(working_rule, ACL_RULE_MACRO_ATTR_KEY);
		
        }/* while */
		
		return(working_list);
	}
}

/*
 * returns ACL_TRUE, ACL_FALSE or ACL_DONT_KNOW.
 *
 * user is a string from the userdn keyword which may contain
 * * components.  This routine does the compare component by component, so
 * that * behaves differently to "normal".
 * Any ($dn) or $attr must have been removed from user before this is called.
*/
static int
acllas_eval_one_user( struct acl_pblock *aclpb, char * clientDN, char *rule) {

	int exact_match = 0;
	const size_t 	LDAP_URL_prefix_len = strlen(LDAP_URL_prefix);

	/* URL format */
	if (strchr (rule, '?') != NULL) {
				/* URL format */
				if (acllas__client_match_URL ( aclpb, clientDN, 
							     rule) == ACL_TRUE) {
					exact_match = 1;					
				}			
	} else if ( strstr(rule, "=*") == NULL ) {
		/* Just a straight compare */
			/* skip the ldap:/// part */
			rule += LDAP_URL_prefix_len;
			exact_match = !slapi_utf8casecmp((ACLUCHP)clientDN,
											(ACLUCHP)rule);
	} else{
		/* Here, contains a =*, so need to match comp by comp */
		/* skip the ldap:/// part */
		rule += LDAP_URL_prefix_len;
		acl_match_prefix( rule, clientDN, &exact_match);
	}
	if ( exact_match) {
		return( ACL_TRUE);
	} else {
		return(ACL_FALSE);
	}
}

/*
 * returns ACL_TRUE, ACL_FALSE and ACL_DONT_KNOW.
 *
 * The user string has had all ($dn) and $attr replaced
 * so the only dodgy thing left is a *.
 *
 * If * appears in such a user string, then it matches only that
 * component, not .*, like it would otherwise.
 * 
*/
static int
acllas_eval_one_group(char *groupbuf, lasInfo *lasinfo) {

	if (groupbuf) {
		return( acllas__user_ismember_of_group (
				      						lasinfo->aclpb,
				      						groupbuf, 
				      						lasinfo->clientDn,
											ACLLAS_CACHE_ALL_GROUPS,
											lasinfo->aclpb->aclpb_clientcert
				   						));
	} else {
		return(ACL_FALSE);	/* not in the empty group */
	}
}

/*
 * returns ACL_TRUE for match, ACL_FALSE for not a match, ACL_DONT_KNOW otherwise.
*/
static int
acllas_eval_one_role(char *role, lasInfo *lasinfo) {
	
	Slapi_DN *roleDN = NULL;
	int rc = ACL_FALSE;	
	char    ebuf [ BUFSIZ ];

	/*
	 * See if lasinfo.clientDn has role rolebuf.
	 * Here we know it's not an anom user nor 
	 * a an anyone user--the client dn must be matched against
	 * a real role.
	*/ 

	roleDN = slapi_sdn_new_dn_byval(role);
	if (role) {
		rc = acllas__user_has_role(									
								lasinfo->aclpb,	      						
			      				roleDN, 
			      				lasinfo->aclpb->aclpb_authorization_sdn);
	} else {	/* The user does not have the empty role */
		rc = ACL_FALSE;
	}
	slapi_sdn_free(&roleDN );

	/* Some useful logging */
	if (rc == ACL_TRUE ) {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
                        "role evaluation: user '%s' does have role '%s'\n",
                ACL_ESCAPE_STRING_WITH_PUNCTUATION (lasinfo->clientDn, ebuf),
                        role);
	} else {
		slapi_log_error( SLAPI_LOG_ACL, plugin_name,
                        "role evaluation: user '%s' does NOT have role '%s'\n",
                ACL_ESCAPE_STRING_WITH_PUNCTUATION (lasinfo->clientDn, ebuf),
				role);
	}
	return(rc);
}

/*
 * returns ACL_TRUE if e matches the filter str, ACL_FALSE if not,
 * ACL_DONT_KNOW otherwise.
*/
static int acllas_eval_one_target_filter( char * str, Slapi_Entry *e) {
	
	int rc = ACL_FALSE;
	Slapi_Filter *f = NULL;							

	PR_ASSERT(str);

	if ((f = slapi_str2filter(str)) == NULL) {
		slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
        	"Warning: Bad targetfilter(%s) in aci: does not match\n", str);       	
		return(ACL_DONT_KNOW);
	}

	if (slapi_vattr_filter_test(NULL, e, f, 0 /*don't do acess chk*/)!= 0) {
			rc = ACL_FALSE;	/* Filter does not match */
	} else {
			rc = ACL_TRUE;  /* filter does match */
	}
	slapi_filter_free(f, 1);

	return(rc);

}





/***************************************************************************/
/*				E	N	D			   */
/***************************************************************************/