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
|
/*
* Unix SMB implementation.
* Functions for understanding conditional ACEs
*
* 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; either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "librpc/gen_ndr/ndr_security.h"
#include "librpc/gen_ndr/conditional_ace.h"
#include "libcli/security/security.h"
#include "libcli/security/conditional_ace.h"
#include "libcli/security/claims-conversions.h"
#include "lib/util/tsort.h"
#include "lib/util/bytearray.h"
/* We're only dealing with utf-8 here. Honestly. */
#undef strncasecmp
#define SDDL_FLAG_EXPECTING_UNARY_OP 1
#define SDDL_FLAG_EXPECTING_BINARY_OP 2
#define SDDL_FLAG_EXPECTING_BINARY_LOGIC_OP 4
#define SDDL_FLAG_EXPECTING_LOCAL_ATTR 8
#define SDDL_FLAG_EXPECTING_NON_LOCAL_ATTR 16
#define SDDL_FLAG_EXPECTING_LITERAL 32
#define SDDL_FLAG_EXPECTING_PAREN 64
#define SDDL_FLAG_EXPECTING_PAREN_LITERAL 128
#define SDDL_FLAG_NOT_EXPECTING_END_PAREN 256
#define SDDL_FLAG_DEVICE 512
#define SDDL_FLAG_IS_UNARY_OP (1 << 20)
#define SDDL_FLAG_IS_BINARY_OP (1 << 21)
#define SDDL_FLAGS_EXPR_START (SDDL_FLAG_EXPECTING_UNARY_OP | \
SDDL_FLAG_EXPECTING_LOCAL_ATTR | \
SDDL_FLAG_EXPECTING_NON_LOCAL_ATTR | \
SDDL_FLAG_EXPECTING_PAREN)
#define SDDL_FLAGS_MEMBER_OP (SDDL_FLAG_EXPECTING_LITERAL | \
SDDL_FLAG_EXPECTING_PAREN_LITERAL | \
SDDL_FLAG_IS_UNARY_OP)
#define SDDL_FLAGS_RELATIONAL_OP (SDDL_FLAG_EXPECTING_LITERAL | \
SDDL_FLAG_EXPECTING_PAREN_LITERAL | \
SDDL_FLAG_EXPECTING_NON_LOCAL_ATTR | \
SDDL_FLAG_IS_BINARY_OP)
#define SDDL_FLAGS_CONTAINS_OP (SDDL_FLAG_EXPECTING_LITERAL | \
SDDL_FLAG_EXPECTING_NON_LOCAL_ATTR | \
SDDL_FLAG_IS_BINARY_OP)
#define SDDL_FLAGS_EXISTS_OP (SDDL_FLAG_EXPECTING_LOCAL_ATTR | \
SDDL_FLAG_EXPECTING_NON_LOCAL_ATTR | \
SDDL_FLAG_IS_UNARY_OP)
#define SDDL_FLAGS_LOGIC_OP (SDDL_FLAG_EXPECTING_LOCAL_ATTR | \
SDDL_FLAG_EXPECTING_NON_LOCAL_ATTR | \
SDDL_FLAG_EXPECTING_PAREN | \
SDDL_FLAG_EXPECTING_UNARY_OP | \
SDDL_FLAG_IS_BINARY_OP)
#define SDDL_FLAGS_ATTRIBUTE (SDDL_FLAG_EXPECTING_BINARY_OP | \
SDDL_FLAG_EXPECTING_BINARY_LOGIC_OP)
#define SDDL_FLAGS_LITERAL SDDL_FLAG_EXPECTING_BINARY_LOGIC_OP
#define SDDL_FLAGS_PAREN_END (SDDL_FLAG_EXPECTING_BINARY_LOGIC_OP | \
SDDL_FLAG_EXPECTING_BINARY_OP)
enum {
SDDL_NOT_AN_OP = 0,
SDDL_PRECEDENCE_EXISTS,
SDDL_PRECEDENCE_COMMON,
SDDL_PRECEDENCE_NOT,
SDDL_PRECEDENCE_AND,
SDDL_PRECEDENCE_OR,
SDDL_PRECEDENCE_PAREN_END,
SDDL_PRECEDENCE_PAREN_START,
};
struct ace_condition_sddl_compiler_context {
TALLOC_CTX *mem_ctx;
const uint8_t *sddl;
uint32_t length;
uint32_t offset;
uint32_t stack_depth;
uint32_t max_program_length;
uint32_t approx_size;
struct ace_condition_script *program;
struct ace_condition_token *stack;
struct ace_condition_token *target;
uint32_t *target_len;
const char *message;
uint32_t message_offset;
struct dom_sid *domain_sid;
uint32_t state;
uint8_t last_token_type;
bool allow_device;
};
struct sddl_data {
const char *name;
uint32_t flags;
uint8_t op_precedence;
uint8_t nargs;
};
static const struct sddl_data sddl_strings[256] = {
/* operators */
[CONDITIONAL_ACE_TOKEN_MEMBER_OF] = {
"Member_of",
SDDL_FLAGS_MEMBER_OP,
SDDL_PRECEDENCE_COMMON,
1
},
[CONDITIONAL_ACE_TOKEN_DEVICE_MEMBER_OF] = {
"Device_Member_of",
SDDL_FLAGS_MEMBER_OP|SDDL_FLAG_DEVICE,
SDDL_PRECEDENCE_COMMON,
1
},
[CONDITIONAL_ACE_TOKEN_MEMBER_OF_ANY] = {
/* [MS-DTYP] says "_Any", but windows prefers '_any' */
"Member_of_any",
SDDL_FLAGS_MEMBER_OP,
SDDL_PRECEDENCE_COMMON,
1
},
[CONDITIONAL_ACE_TOKEN_DEVICE_MEMBER_OF_ANY] = {
"Device_Member_of_Any",
SDDL_FLAGS_MEMBER_OP|SDDL_FLAG_DEVICE,
SDDL_PRECEDENCE_COMMON,
1
},
[CONDITIONAL_ACE_TOKEN_NOT_MEMBER_OF] = {
"Not_Member_of",
SDDL_FLAGS_MEMBER_OP,
SDDL_PRECEDENCE_COMMON,
1
},
[CONDITIONAL_ACE_TOKEN_NOT_DEVICE_MEMBER_OF] = {
"Not_Device_Member_of",
SDDL_FLAGS_MEMBER_OP|SDDL_FLAG_DEVICE,
SDDL_PRECEDENCE_COMMON,
1
},
[CONDITIONAL_ACE_TOKEN_NOT_MEMBER_OF_ANY] = {
"Not_Member_of_Any",
SDDL_FLAGS_MEMBER_OP,
SDDL_PRECEDENCE_COMMON,
1
},
[CONDITIONAL_ACE_TOKEN_NOT_DEVICE_MEMBER_OF_ANY] = {
"Not_Device_Member_of_Any",
SDDL_FLAGS_MEMBER_OP|SDDL_FLAG_DEVICE,
SDDL_PRECEDENCE_COMMON,
1
},
[CONDITIONAL_ACE_TOKEN_EQUAL] = {
"==",
SDDL_FLAGS_RELATIONAL_OP,
SDDL_PRECEDENCE_COMMON,
2
},
[CONDITIONAL_ACE_TOKEN_NOT_EQUAL] = {
"!=",
SDDL_FLAGS_RELATIONAL_OP,
SDDL_PRECEDENCE_COMMON,
2
},
[CONDITIONAL_ACE_TOKEN_LESS_THAN] = {
"<",
SDDL_FLAGS_RELATIONAL_OP,
SDDL_PRECEDENCE_COMMON,
2
},
[CONDITIONAL_ACE_TOKEN_LESS_OR_EQUAL] = {
"<=",
SDDL_FLAGS_RELATIONAL_OP,
SDDL_PRECEDENCE_COMMON,
2
},
[CONDITIONAL_ACE_TOKEN_GREATER_THAN] = {
">",
SDDL_FLAGS_RELATIONAL_OP,
SDDL_PRECEDENCE_COMMON,
2
},
[CONDITIONAL_ACE_TOKEN_GREATER_OR_EQUAL] = {
">=",
SDDL_FLAGS_RELATIONAL_OP,
SDDL_PRECEDENCE_COMMON,
2
},
[CONDITIONAL_ACE_TOKEN_CONTAINS] = {
"Contains",
SDDL_FLAGS_CONTAINS_OP,
SDDL_PRECEDENCE_COMMON,
2
},
[CONDITIONAL_ACE_TOKEN_ANY_OF] = {
"Any_of",
SDDL_FLAGS_CONTAINS_OP,
SDDL_PRECEDENCE_COMMON,
2
},
[CONDITIONAL_ACE_TOKEN_NOT_CONTAINS] = {
"Not_Contains",
SDDL_FLAGS_CONTAINS_OP,
SDDL_PRECEDENCE_COMMON,
2
},
[CONDITIONAL_ACE_TOKEN_NOT_ANY_OF] = {
"Not_Any_of",
SDDL_FLAGS_CONTAINS_OP,
SDDL_PRECEDENCE_COMMON,
2
},
[CONDITIONAL_ACE_TOKEN_AND] = {
"&&",
SDDL_FLAGS_LOGIC_OP,
SDDL_PRECEDENCE_AND,
2
},
[CONDITIONAL_ACE_TOKEN_OR] = {
"||",
SDDL_FLAGS_LOGIC_OP,
SDDL_PRECEDENCE_OR,
2
},
[CONDITIONAL_ACE_TOKEN_NOT] = {
"!",
(SDDL_FLAG_EXPECTING_PAREN |
SDDL_FLAG_EXPECTING_NON_LOCAL_ATTR |
SDDL_FLAG_IS_UNARY_OP),
SDDL_PRECEDENCE_NOT,
1
},
[CONDITIONAL_ACE_TOKEN_EXISTS] = {
"Exists",
SDDL_FLAGS_EXISTS_OP,
SDDL_PRECEDENCE_EXISTS,
1
},
[CONDITIONAL_ACE_TOKEN_NOT_EXISTS] = {
"Not_Exists",
SDDL_FLAGS_EXISTS_OP,
SDDL_PRECEDENCE_EXISTS,
1
},
/* pseudo-operator pseudo-tokens */
[CONDITIONAL_ACE_SAMBA_SDDL_PAREN] = {
"(",
0,
SDDL_PRECEDENCE_PAREN_START,
0
},
[CONDITIONAL_ACE_SAMBA_SDDL_PAREN_END] = {
")",
SDDL_FLAGS_PAREN_END,
SDDL_PRECEDENCE_PAREN_END,
0
},
/*
* non-operators.
* The names here are only used for error messages.
*
* some of them will never actually be encountered (e.g. 8-bit
* integers).
*/
[CONDITIONAL_ACE_TOKEN_INT8] = {
.name = "8-bit integer",
.flags = SDDL_FLAGS_LITERAL,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_TOKEN_INT16] = {
"16-bit integer",
SDDL_FLAGS_LITERAL,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_TOKEN_INT32] = {
"32-bit integer",
SDDL_FLAGS_LITERAL,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_TOKEN_INT64] = {
"64-bit integer",
SDDL_FLAGS_LITERAL,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_TOKEN_UNICODE] = {
"unicode",
SDDL_FLAGS_LITERAL,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_TOKEN_OCTET_STRING] = {
"byte string",
SDDL_FLAGS_LITERAL,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_TOKEN_COMPOSITE] = {
"composite list",
SDDL_FLAGS_LITERAL,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_TOKEN_SID] = {
"SID",
SDDL_FLAGS_LITERAL,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_LOCAL_ATTRIBUTE] = {
"local attribute",
SDDL_FLAGS_ATTRIBUTE,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_USER_ATTRIBUTE] = {
"user attribute",
SDDL_FLAGS_ATTRIBUTE,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_RESOURCE_ATTRIBUTE] = {
"resource attribute",
SDDL_FLAGS_ATTRIBUTE,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_DEVICE_ATTRIBUTE] = {
"device attribute",
SDDL_FLAGS_ATTRIBUTE|SDDL_FLAG_DEVICE,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_SAMBA_RESULT_BOOL] = {
"boolean result",
0,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_SAMBA_RESULT_NULL] = {
"null result",
0,
SDDL_NOT_AN_OP,
0
},
[CONDITIONAL_ACE_SAMBA_RESULT_ERROR] = {
"error result",
0,
SDDL_NOT_AN_OP,
0
},
};
struct sddl_attr_type{
const char *name;
uint8_t code;
};
/*
* These are the prefixes for non-local attribute types. [MS-DTYP]
* styles them in title case ("@User."), but Windows itself seems to
* prefer all-caps, so that is how we render them.
*/
static const struct sddl_attr_type sddl_attr_types[] = {
{"USER.", CONDITIONAL_ACE_USER_ATTRIBUTE},
{"RESOURCE.", CONDITIONAL_ACE_RESOURCE_ATTRIBUTE},
{"DEVICE.", CONDITIONAL_ACE_DEVICE_ATTRIBUTE},
};
struct sddl_write_context {
TALLOC_CTX *mem_ctx;
char *sddl;
size_t len;
size_t alloc_len;
};
static bool sddl_write(struct sddl_write_context *ctx,
const char *s)
{
size_t len = strlen(s);
if (ctx->alloc_len - ctx->len <= len ||
ctx->sddl == NULL) {
size_t old = ctx->alloc_len;
ctx->alloc_len = old + MAX(old / 2, len + 50);
if (ctx->alloc_len <= old ||
ctx->alloc_len - ctx->len <= len) {
return false;
}
ctx->sddl = talloc_realloc(ctx->mem_ctx, ctx->sddl,
char, ctx->alloc_len);
if (ctx->sddl == NULL) {
return false;
}
}
memcpy(ctx->sddl + ctx->len, s, len);
ctx->len += len;
ctx->sddl[ctx->len] = 0;
return true;
}
/*
* This is a helper function to create a representation of a
* conditional ACE. This is not SDDL, more like a disassembly,
* but it uses some of the same tables.
*/
char *debug_conditional_ace(TALLOC_CTX *mem_ctx,
struct ace_condition_script *program)
{
size_t i;
size_t depth = 0;
char stack[] = " ";
char line[120];
struct sddl_write_context ctx = {
.mem_ctx = mem_ctx
};
for (i = 0; i < program->length; i++) {
struct ace_condition_token *tok = &program->tokens[i];
struct sddl_data s = sddl_strings[tok->type];
char hex[21];
char *utf8 = NULL;
int utf8_len;
char type;
char nom[40];
snprintf(nom, sizeof(nom), "\033[1;33m%20s\033[0m", s.name);
switch (tok->type) {
case CONDITIONAL_ACE_TOKEN_INT8:
case CONDITIONAL_ACE_TOKEN_INT16:
case CONDITIONAL_ACE_TOKEN_INT32:
case CONDITIONAL_ACE_TOKEN_INT64:
if (tok->data.int64.sign > 3 ||
tok->data.int64.base > 3) {
goto error;
}
snprintf(line, sizeof(line),
"%s %"PRIi64" %c%c\n",
nom,
tok->data.int64.value,
"?+-_"[tok->data.int64.sign],
"?odh"[tok->data.int64.base]
);
type = 'i';
break;
case CONDITIONAL_ACE_TOKEN_MEMBER_OF:
case CONDITIONAL_ACE_TOKEN_DEVICE_MEMBER_OF:
case CONDITIONAL_ACE_TOKEN_MEMBER_OF_ANY:
case CONDITIONAL_ACE_TOKEN_DEVICE_MEMBER_OF_ANY:
case CONDITIONAL_ACE_TOKEN_NOT_MEMBER_OF:
case CONDITIONAL_ACE_TOKEN_NOT_DEVICE_MEMBER_OF:
case CONDITIONAL_ACE_TOKEN_NOT_MEMBER_OF_ANY:
case CONDITIONAL_ACE_TOKEN_NOT_DEVICE_MEMBER_OF_ANY:
snprintf(line, sizeof(line),
"%s bool\n",
nom
);
type = 'b';
break;
case CONDITIONAL_ACE_TOKEN_EQUAL:
case CONDITIONAL_ACE_TOKEN_NOT_EQUAL:
case CONDITIONAL_ACE_TOKEN_LESS_THAN:
case CONDITIONAL_ACE_TOKEN_LESS_OR_EQUAL:
case CONDITIONAL_ACE_TOKEN_GREATER_THAN:
case CONDITIONAL_ACE_TOKEN_GREATER_OR_EQUAL:
case CONDITIONAL_ACE_TOKEN_CONTAINS:
case CONDITIONAL_ACE_TOKEN_ANY_OF:
case CONDITIONAL_ACE_TOKEN_NOT_CONTAINS:
case CONDITIONAL_ACE_TOKEN_NOT_ANY_OF:
case CONDITIONAL_ACE_TOKEN_AND:
case CONDITIONAL_ACE_TOKEN_OR:
snprintf(line, sizeof(line),
"%s bool\n",
nom
);
type = 'b';
break;
case CONDITIONAL_ACE_TOKEN_EXISTS:
case CONDITIONAL_ACE_TOKEN_NOT_EXISTS:
case CONDITIONAL_ACE_TOKEN_NOT:
snprintf(line, sizeof(line),
"%s bool\n",
nom
);
type = 'b';
break;
case CONDITIONAL_ACE_LOCAL_ATTRIBUTE:
case CONDITIONAL_ACE_USER_ATTRIBUTE:
case CONDITIONAL_ACE_RESOURCE_ATTRIBUTE:
case CONDITIONAL_ACE_DEVICE_ATTRIBUTE:
snprintf(line, sizeof(line),
"%s.%s (any type)\n",
nom,
tok->data.unicode.value
);
type = '?';
break;
case CONDITIONAL_ACE_TOKEN_UNICODE:
snprintf(line, sizeof(line),
"%s.%s (any type)\n",
nom,
tok->data.unicode.value
);
type = 'u';
break;
case CONDITIONAL_ACE_TOKEN_OCTET_STRING:
utf8_len = MIN(tok->data.bytes.length, 9);
hex_encode_buf(hex, tok->data.bytes.data, utf8_len);
snprintf(line, sizeof(line),
"%s %.*s (%d)\n",
nom, utf8_len * 2, hex, utf8_len);
type = 'o';
break;
case CONDITIONAL_ACE_TOKEN_SID:
utf8 = sddl_encode_sid(mem_ctx,
&tok->data.sid.sid,
NULL);
snprintf(line, sizeof(line),
"%s (%s)\n",
nom, utf8);
type = 'S';
break;
case CONDITIONAL_ACE_TOKEN_COMPOSITE:
snprintf(line, sizeof(line),
"%s %"PRIu32" direct members\n",
nom, tok->data.composite.n_members);
type = 'C';
break;
case CONDITIONAL_ACE_TOKEN_INVALID_OR_PADDING:
snprintf(line, sizeof(line),
"%s\n", nom);
type = '0';
break;
default:
snprintf(line, sizeof(line),
"unknown opcode %#02x\n", tok->type);
type = '!';
break;
}
if (s.nargs > depth) {
snprintf(nom, sizeof(nom),
"UNDER: -%zu", s.nargs - depth);
depth = 0;
sddl_write(&ctx, nom);
} else if (depth >= strlen(stack)) {
snprintf(nom, sizeof(nom),
"depth %zu", s.nargs - depth);
depth -= (s.nargs - 1);
sddl_write(&ctx, nom);
} else {
depth -= s.nargs;
stack[depth] = type;
depth++;
if (depth < strlen(stack)) {
stack[depth] = ' ';
}
sddl_write(&ctx, stack);
}
sddl_write(&ctx, line);
}
if (depth == 1 && stack[0] == 'b') {
snprintf(line, sizeof(line),
"\033[1;32mGOOD: finishes on a single bool\033[0m\n");
} else {
snprintf(line, sizeof(line),
"\033[1;31mBAD: should finish with a bool\033[0m\n");
}
sddl_write(&ctx, line);
return ctx.sddl;
error:
TALLOC_FREE(ctx.sddl);
return NULL;
}
struct sddl_node {
struct ace_condition_token *tok;
struct sddl_node *lhs;
struct sddl_node *rhs;
bool wants_parens;
};
static bool sddl_write_int(struct sddl_write_context *ctx,
const struct ace_condition_token *tok)
{
int64_t v = tok->data.int64.value;
uint8_t sign = tok->data.int64.sign;
uint8_t base = tok->data.int64.base;
char buf[26]; /* oct(1<<63) + sign + \0 */
char sign_char;
if (sign > CONDITIONAL_ACE_INT_SIGN_NONE ||
base > CONDITIONAL_ACE_INT_BASE_16) {
return false;
}
/*
* we have 9 combinations of base/sign (+ some invalid combinations of
* actual sign vs claimed sign).
*/
if (sign == CONDITIONAL_ACE_INT_SIGN_NONE) {
/* octal and hex will end up unsigned! */
if (base == CONDITIONAL_ACE_INT_BASE_8) {
snprintf(buf, sizeof(buf), "0%"PRIo64, v);
} else if (base == CONDITIONAL_ACE_INT_BASE_10) {
snprintf(buf, sizeof(buf), "%"PRId64, v);
} else {
snprintf(buf, sizeof(buf), "0x%"PRIx64, v);
}
return sddl_write(ctx, buf);
}
if (sign == CONDITIONAL_ACE_INT_SIGN_POSITIVE && v < 0) {
return false;
}
if (sign == CONDITIONAL_ACE_INT_SIGN_NEGATIVE && v > 0) {
/* note we allow "-0", because we will parse it. */
return false;
}
sign_char = (sign == CONDITIONAL_ACE_INT_SIGN_NEGATIVE) ? '-' : '+';
/*
* We can use "%+ld" for the decimal sign (except -0), but
* "%+lx" and "%+lo" are invalid because %o and %x are
* unsigned.
*/
if (base == CONDITIONAL_ACE_INT_BASE_10) {
if (v == 0) {
snprintf(buf, sizeof(buf), "%c0", sign_char);
} else {
snprintf(buf, sizeof(buf), "%+"PRId64, v);
}
return sddl_write(ctx, buf);
}
if (v == INT64_MIN) {
/*
* llabs(INT64_MIN) will be undefined.
* The lengths we must go to to round trip!
*/
if (base == CONDITIONAL_ACE_INT_BASE_8) {
return sddl_write(ctx, "-01000000000000000000000");
}
return sddl_write(ctx, "-0x8000000000000000");
}
if (base == CONDITIONAL_ACE_INT_BASE_8) {
snprintf(buf, sizeof(buf), "%c0%llo", sign_char, llabs(v));
} else {
snprintf(buf, sizeof(buf), "%c0x%llx", sign_char, llabs(v));
}
return sddl_write(ctx, buf);
}
static bool sddl_should_escape_utf16(uint16_t c)
{
if (c <= ' ' || c > 126) {
return true;
}
switch (c) {
case '!':
case '"':
case '&':
case '(':
case ')':
case '<':
case '=':
case '>':
case '|':
case '%':
return true;
}
return false;
}
static bool sddl_encode_attr_name(TALLOC_CTX *mem_ctx,
const char *src,
char **dest,
size_t *dest_len)
{
size_t i, j;
bool ok;
uint16_t *utf16 = NULL;
char *escaped = NULL;
size_t utf16_byte_len;
size_t utf16_len;
size_t src_len = strlen(src);
size_t escapees;
size_t required;
*dest = NULL;
/*
* Writing the string escapes can only really happen in
* utf-16.
*/
ok = convert_string_talloc(mem_ctx,
CH_UTF8, CH_UTF16LE,
src, src_len,
&utf16, &utf16_byte_len);
if (!ok) {
return false;
}
utf16_len = utf16_byte_len / 2;
escapees = 0;
for (i = 0; i < utf16_len; i++) {
uint16_t c = utf16[i];
if (sddl_should_escape_utf16(c)) {
escapees++;
}
if (c == 0) {
/* we can't have '\0' (or "%0000") in a name. */
TALLOC_FREE(utf16);
return false;
}
}
required = src_len + escapees * 5;
escaped = talloc_size(mem_ctx, required + 1);
if (escaped == NULL) {
TALLOC_FREE(utf16);
return false;
}
if (escapees == 0) {
/* there is nothing to escape: the original string is fine */
memcpy(escaped, src, src_len);
escaped[src_len] = '\0';
*dest = escaped;
*dest_len = src_len;
TALLOC_FREE(utf16);
return true;
}
for (i = 0, j = 0; i < utf16_len && j < required; i++) {
uint16_t c = utf16[i];
if (sddl_should_escape_utf16(c)) {
if (j + 5 >= required) {
TALLOC_FREE(escaped);
TALLOC_FREE(utf16);
return false;
}
snprintf(escaped + j, 6, "%%%04x", c);
j += 5;
} else {
escaped[j] = c;
j++;
}
}
escaped[j] = '\0';
*dest = escaped;
*dest_len = j;
TALLOC_FREE(utf16);
return true;
}
static bool sddl_write_attr(struct sddl_write_context *ctx,
struct ace_condition_token *tok)
{
char *name = NULL;
size_t name_len;
size_t i;
bool ok = sddl_encode_attr_name(ctx->mem_ctx,
tok->data.local_attr.value,
&name, &name_len);
if (!ok) {
return false;
}
for (i = 0; i < ARRAY_SIZE(sddl_attr_types); i++) {
struct sddl_attr_type x = sddl_attr_types[i];
if (x.code == tok->type) {
ok = sddl_write(ctx, "@");
if (! ok) {
return false;
}
ok = sddl_write(ctx, x.name);
if (! ok) {
return false;
}
break;
}
}
ok = sddl_write(ctx, name);
talloc_free(name);
return ok;
}
static bool sddl_write_unicode(struct sddl_write_context *ctx,
const struct ace_condition_token *tok)
{
char *quoted = NULL;
bool ok;
/*
* We rely on tok->data.unicode.value being
* nul-terminated.
*/
if (strchr(tok->data.unicode.value, '"') != NULL) {
/*
* There is a double quote in this string, but SDDL
* has no mechanism for escaping these (or anything
* else) in unicode strings.
*
* The only thing to do is fail.
*
* This cannot happen with an ACE created from SDDL,
* because the same no-escapes rule applies on the way
* in.
*/
return false;
}
quoted = talloc_asprintf(ctx->mem_ctx, "\"%s\"",
tok->data.unicode.value);
if (quoted == NULL) {
return false;
}
ok = sddl_write(ctx, quoted);
TALLOC_FREE(quoted);
return ok;
}
static bool sddl_write_octet_string(struct sddl_write_context *ctx,
const struct ace_condition_token *tok)
{
bool ok;
char *hex = hex_encode_talloc(ctx->mem_ctx,
tok->data.bytes.data,
tok->data.bytes.length);
ok = sddl_write(ctx, "#");
if (!ok) {
return false;
}
ok = sddl_write(ctx, hex);
talloc_free(hex);
return ok;
}
/*
* For octet strings, the Resource attribute ACE SDDL differs from conditional
* ACE SDDL, lacking the leading '#'.
*/
static bool sddl_write_ra_octet_string(struct sddl_write_context *ctx,
const struct ace_condition_token *tok)
{
bool ok;
char *hex = hex_encode_talloc(ctx->mem_ctx,
tok->data.bytes.data,
tok->data.bytes.length);
ok = sddl_write(ctx, hex);
talloc_free(hex);
return ok;
}
static bool sddl_write_sid(struct sddl_write_context *ctx,
const struct ace_condition_token *tok)
{
bool ok;
char *sddl = NULL;
char *sid = sddl_encode_sid(ctx->mem_ctx,
&tok->data.sid.sid,
NULL);
if (sid == NULL) {
return false;
}
sddl = talloc_asprintf(ctx->mem_ctx, "SID(%s)", sid);
if (sddl == NULL) {
talloc_free(sid);
return false;
}
ok = sddl_write(ctx, sddl);
talloc_free(sid);
talloc_free(sddl);
return ok;
}
static bool sddl_write_composite(struct sddl_write_context *ctx,
struct ace_condition_token *tok)
{
/*
* Looks like {1, 2, 3, "four", {"woah, nesting", {6}}, SID(BA)}.
*/
struct ace_condition_composite *c = &tok->data.composite;
uint32_t i;
bool ok;
ok = sddl_write(ctx, "{");
if (!ok) {
return false;
}
for (i = 0; i < c->n_members; i++) {
struct ace_condition_token *t = &c->tokens[i];
if (i > 0) {
ok = sddl_write(ctx, ", ");
if (!ok) {
return false;
}
}
switch (t->type) {
case CONDITIONAL_ACE_TOKEN_INT8:
case CONDITIONAL_ACE_TOKEN_INT16:
case CONDITIONAL_ACE_TOKEN_INT32:
case CONDITIONAL_ACE_TOKEN_INT64:
ok = sddl_write_int(ctx, t);
break;
case CONDITIONAL_ACE_TOKEN_UNICODE:
ok = sddl_write_unicode(ctx, t);
break;
case CONDITIONAL_ACE_TOKEN_OCTET_STRING:
ok = sddl_write_octet_string(ctx, t);
break;
case CONDITIONAL_ACE_TOKEN_SID:
ok = sddl_write_sid(ctx, t);
break;
case CONDITIONAL_ACE_TOKEN_COMPOSITE:
return false;
default:
return false;
}
if (!ok) {
return false;
}
}
ok = sddl_write(ctx, "}");
return ok;
}
static bool sddl_write_node(struct sddl_write_context *ctx,
struct sddl_node *node)
{
struct ace_condition_token *tok = node->tok;
switch (tok->type) {
case CONDITIONAL_ACE_TOKEN_INT8:
case CONDITIONAL_ACE_TOKEN_INT16:
case CONDITIONAL_ACE_TOKEN_INT32:
case CONDITIONAL_ACE_TOKEN_INT64:
return sddl_write_int(ctx, tok);
case CONDITIONAL_ACE_TOKEN_MEMBER_OF:
case CONDITIONAL_ACE_TOKEN_DEVICE_MEMBER_OF:
case CONDITIONAL_ACE_TOKEN_MEMBER_OF_ANY:
case CONDITIONAL_ACE_TOKEN_DEVICE_MEMBER_OF_ANY:
case CONDITIONAL_ACE_TOKEN_NOT_MEMBER_OF:
case CONDITIONAL_ACE_TOKEN_NOT_DEVICE_MEMBER_OF:
case CONDITIONAL_ACE_TOKEN_NOT_MEMBER_OF_ANY:
case CONDITIONAL_ACE_TOKEN_NOT_DEVICE_MEMBER_OF_ANY:
case CONDITIONAL_ACE_TOKEN_EQUAL:
case CONDITIONAL_ACE_TOKEN_NOT_EQUAL:
case CONDITIONAL_ACE_TOKEN_LESS_THAN:
case CONDITIONAL_ACE_TOKEN_LESS_OR_EQUAL:
case CONDITIONAL_ACE_TOKEN_GREATER_THAN:
case CONDITIONAL_ACE_TOKEN_GREATER_OR_EQUAL:
case CONDITIONAL_ACE_TOKEN_CONTAINS:
case CONDITIONAL_ACE_TOKEN_ANY_OF:
case CONDITIONAL_ACE_TOKEN_NOT_CONTAINS:
case CONDITIONAL_ACE_TOKEN_NOT_ANY_OF:
case CONDITIONAL_ACE_TOKEN_AND:
case CONDITIONAL_ACE_TOKEN_OR:
case CONDITIONAL_ACE_TOKEN_EXISTS:
case CONDITIONAL_ACE_TOKEN_NOT_EXISTS:
case CONDITIONAL_ACE_TOKEN_NOT:
return sddl_write(ctx, sddl_strings[tok->type].name);
case CONDITIONAL_ACE_LOCAL_ATTRIBUTE:
case CONDITIONAL_ACE_USER_ATTRIBUTE:
case CONDITIONAL_ACE_RESOURCE_ATTRIBUTE:
case CONDITIONAL_ACE_DEVICE_ATTRIBUTE:
return sddl_write_attr(ctx, tok);
case CONDITIONAL_ACE_TOKEN_UNICODE:
return sddl_write_unicode(ctx, tok);
case CONDITIONAL_ACE_TOKEN_OCTET_STRING:
return sddl_write_octet_string(ctx, tok);
case CONDITIONAL_ACE_TOKEN_SID:
return sddl_write_sid(ctx, tok);
case CONDITIONAL_ACE_TOKEN_COMPOSITE:
return sddl_write_composite(ctx, tok);
case CONDITIONAL_ACE_TOKEN_INVALID_OR_PADDING:
/*
* This is only expected at the very end, which we
* can't (and don't need to) check here, but we can at
* least ensure it's the end of a sub-expression.
*/
return (node->rhs == NULL);
default:
return false;
}
/* not expecting to get here */
return false;
}
static inline bool sddl_wants_outer_parens(struct sddl_node *node)
{
/*
* Binary ops (having a LHS) are always parenthesised "(a == 2)"
*
* Member-of ops are too, for some reason.
*/
return (node->lhs != NULL ||
node->tok->type == CONDITIONAL_ACE_TOKEN_MEMBER_OF ||
node->tok->type == CONDITIONAL_ACE_TOKEN_NOT_MEMBER_OF ||
node->tok->type == CONDITIONAL_ACE_TOKEN_MEMBER_OF_ANY ||
node->tok->type == CONDITIONAL_ACE_TOKEN_NOT_MEMBER_OF_ANY ||
node->tok->type == CONDITIONAL_ACE_TOKEN_DEVICE_MEMBER_OF ||
node->tok->type == CONDITIONAL_ACE_TOKEN_NOT_DEVICE_MEMBER_OF ||
node->tok->type == CONDITIONAL_ACE_TOKEN_DEVICE_MEMBER_OF_ANY ||
node->tok->type == CONDITIONAL_ACE_TOKEN_NOT_DEVICE_MEMBER_OF_ANY);
}
static inline bool sddl_wants_inner_parens(struct sddl_node *node,
struct sddl_node *child)
{
/*
* logical operators are serialised with parentheses around their
* arguments (for NOT it is obligatory).
*/
if (node->tok->type != CONDITIONAL_ACE_TOKEN_NOT &&
node->tok->type != CONDITIONAL_ACE_TOKEN_AND &&
node->tok->type != CONDITIONAL_ACE_TOKEN_OR) {
return false;
}
if (sddl_wants_outer_parens(child)) {
return false;
}
return true;
}
static void sddl_tree_resolve_parens(struct sddl_node *node)
{
if (sddl_wants_outer_parens(node)) {
node->wants_parens = true;
}
if (node->lhs != NULL) {
bool p = sddl_wants_inner_parens(node, node->lhs);
node->lhs->wants_parens = p;
sddl_tree_resolve_parens(node->lhs);
}
if (node->rhs != NULL) {
bool p = sddl_wants_inner_parens(node, node->rhs);
node->rhs->wants_parens = p;
sddl_tree_resolve_parens(node->rhs);
}
}
static bool sddl_tree_to_sddl(struct sddl_write_context *ctx,
struct sddl_node *node)
{
bool ok;
if (node->wants_parens) {
ok = sddl_write(ctx, "(");
if (! ok) {
return false;
}
}
if (node->lhs != NULL) {
ok = sddl_tree_to_sddl(ctx, node->lhs);
if (! ok) {
return false;
}
ok = sddl_write(ctx, " ");
if (!ok) {
return false;
}
}
ok = sddl_write_node(ctx, node);
if (!ok) {
return false;
}
if (node->rhs != NULL) {
/* NOT is a special case: "!(x)", not "! (x)" */
if (node->tok->type != CONDITIONAL_ACE_TOKEN_NOT) {
ok = sddl_write(ctx, " ");
if (!ok) {
return false;
}
}
ok = sddl_tree_to_sddl(ctx, node->rhs);
if (! ok) {
return false;
}
}
if (node->wants_parens) {
ok = sddl_write(ctx, ")");
if (!ok) {
return false;
}
}
return true;
}
/*
* Convert conditional ACE conditions into SDDL conditions.
*
* @param mem_ctx
* @param program
* @return a string or NULL on error.
*/
char *sddl_from_conditional_ace(TALLOC_CTX *mem_ctx,
struct ace_condition_script *program)
{
size_t i;
char *sddl = NULL;
struct sddl_node *nodes = NULL;
struct sddl_node **trees = NULL;
size_t n_trees = 0;
struct ace_condition_token *tok = NULL;
struct sddl_data s;
bool ok;
struct sddl_write_context ctx = {
.mem_ctx = mem_ctx
};
if (program->length == 0) {
/*
* The empty program is a special case.
*/
return talloc_strdup(mem_ctx, "()");
}
nodes = talloc_zero_array(mem_ctx,
struct sddl_node,
program->length);
if (nodes == NULL) {
talloc_free(sddl);
return NULL;
}
trees = talloc_array(mem_ctx,
struct sddl_node*,
program->length);
if (trees == NULL) {
talloc_free(sddl);
talloc_free(nodes);
return NULL;
}
/*
* This loop constructs a tree, which we then traverse to get the
* SDDL. Consider this transformation:
*
* {A, B, ==, C, D, ==, &&} => "((A == B) && (C == D))"
*
* We keep an array of sub-trees, and add to it in sequence. When the
* thing we're adding takes arguments, we pop those off the tree list.
* So it would go through this sequence:
*
* len items
* 1: A
* 2: A, B
* 1: ==(A, B)
* 2: ==(A, B), C
* 3: ==(A, B), C, D
* 2: ==(A, B), ==(C, D)
* 1 &&(==(A, B), ==(C, D))
*
* Without building a tree it would be difficult to know how many
* parentheses to put before A.
*
* (A == B == C) should become
* {A B == C ==} which should be the same as
* ((A == B) == C)
*/
for (i = 0; i < program->length; i++) {
tok = &program->tokens[i];
s = sddl_strings[tok->type];
nodes[i].tok = tok;
if (s.nargs > n_trees) {
goto error;
}
if (s.nargs >= 1) {
/*
* Read this note if you're trying to follow
* [MS-DTYP]. MS-DTYP uses 'LHS' to describe the
* operand of unary operators even though they are
* always displayed on the right of the operator. It
* makes everything much simpler to use rhs
* instead.
*/
n_trees--;
nodes[i].rhs = trees[n_trees];
if (s.nargs == 2) {
n_trees--;
nodes[i].lhs = trees[n_trees];
}
}
trees[n_trees] = &nodes[i];
n_trees++;
}
if (n_trees != 1) {
goto error;
}
/*
* First we walk the tree to work out where to put parentheses (to
* match the canonical Windows representation).
*
* Doing it in the same traverse as the writing would be possible but
* trickier to get right.
*/
sddl_tree_resolve_parens(trees[0]);
trees[0]->wants_parens = true;
/*
* Clamber over the tree, writing the string.
*/
ok = sddl_tree_to_sddl(&ctx, trees[0]);
if (! ok) {
goto error;
}
talloc_free(trees);
talloc_free(nodes);
return ctx.sddl;
error:
talloc_free(sddl);
talloc_free(trees);
talloc_free(nodes);
return NULL;
}
static void comp_error(struct ace_condition_sddl_compiler_context *comp,
const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
static void comp_error(struct ace_condition_sddl_compiler_context *comp,
const char *fmt, ...)
{
char *msg = NULL;
va_list ap;
va_start(ap, fmt);
msg = talloc_vasprintf(comp->mem_ctx, fmt, ap);
va_end(ap);
if (msg == NULL) {
goto fail;
}
if (comp->message == NULL) {
/*
* Previously unset message; prepend the position.
*
* This is the common case.
*/
comp->message_offset = comp->offset;
comp->message = msg;
return;
}
/*
* There's a message already so we'll try to append.
* This is unlikely to happen.
*/
comp->message = talloc_asprintf(comp->mem_ctx,
"%s AND THEN %s",
comp->message,
msg);
TALLOC_FREE(msg);
if (comp->message == NULL) {
goto fail;
}
DBG_NOTICE("%s\n", comp->message);
return;
fail:
comp->message = talloc_strdup(comp->mem_ctx,
"failed to set error message");
DBG_WARNING("%s\n", comp->message);
}
/*
conditional-ace = "(" conditional-ace-type ";" [ace-flag-string] ";" ace-rights
";" [object- guid] ";" [inherit-object-guid] ";" sid-string ";" "(" cond-expr
")" ")"
wspace = 1*(%x09-0D / %x20)
literal-SID = "SID(" sid-string ")"
term = [wspace] (memberof-op / exists-op / rel-op / contains-op / anyof-op /
attr-name / rel- op2) [wspace]
cond-expr = term / term [wspace] ("||" / "&&" ) [wspace] cond-expr / (["!"]
[wspace] "(" cond-expr ")")
memberof-op = ( "Member_of" / "Not_Member_of" / "Member_of_Any" /
"Not_Member_of_Any" / "Device_Member_of" / "Device_Member_of_Any" /
"Not_Device_Member_of" / "Not_Device_Member_of_Any" ) wspace sid-array
exists-op = ( "Exists" / "Not_Exists") wspace attr-name
rel-op = attr-name [wspace] ("<" / "<=" / ">" / ">=") [wspace] (attr-name2 /
value) ; only scalars
rel-op2 = attr-name [wspace] ("==" / "!=") [wspace] ( attr-name2 / value-array )
; scalar or list
contains-op = attr-name wspace ("Contains" / "Not_Contains") wspace (attr-name2
/ value- array)
anyof-op = attr-name wspace ("Any_of" / "Not_Any_of") wspace (attr-name2 /
value-array)
attr-name1 = attr-char1 *(attr-char1 / "@")
attr-char1 = 1*(ALPHA / DIGIT / ":" / "." / "/" / "_")
attr-name2 = ("@user." / "@device." / "@resource.") 1*attr-char2
; new prefixed name form
attr-char2 = attr-char1 / lit-char
attr-name = attr-name1 / attr-name2
*/
static inline bool is_wspace(uint8_t c)
{
/* wspace := %x09-0D | %x20 */
return (c == ' ' || c == '\x09' || c == '\x0A' ||
c == '\x0B' || c == '\x0C' || c == '\x0D');
}
static inline bool is_attr_char1(uint8_t c)
{
/*
* attr-char1 = 1*(ALPHA / DIGIT / ":" / "." / "/" / "_")
* (ALPHA and DIGIT being ASCII only).
*
* These are used for local attributes, which we don't really
* expect to see in Samba AD.
*
* One example is "WIN://SYSAPPID", which is used in conditional ACEs
* that seem to relate to software installers; another is
* "APPID://PATH", used by Windows Applocker.
*/
return (((c >= 'a') && (c <= 'z')) ||
((c >= 'A') && (c <= 'Z')) ||
((c >= '0') && (c <= '9')) ||
c == ':' || c == '.' || c == '/' || c == '_');
}
static ssize_t read_attr2_string(
struct ace_condition_sddl_compiler_context *comp,
struct ace_condition_unicode *dest)
{
/*
* our SDDL is utf-8, but we need to convert to utf-16 and
* parse the escapes, then back to utf-8, because that's how
* the claims will appear.
*
* attr_char2 is used for attribute names that follow "@Class."
* specifiers. They can consume 5 characters to specify a single code
* unit, using "%1234" style escapes. Certain characters must be
* encoded this way, while others must be literal values. Because the
* %1234 refers to a utf-16 code unit, we really need to do the work
* in that codespace.
*/
bool ok;
uint16_t *utf16 = NULL;
size_t utf16_byte_len;
size_t utf16_chars;
size_t utf8_len;
size_t src_len;
ssize_t i, j;
ssize_t max_len = comp->length - comp->offset;
const uint8_t *src = comp->sddl + comp->offset;
for (i = 0; i < max_len; i++) {
uint8_t c = src[i];
/*
* A double‐byte that must be escaped but isn't tells us that
* the attribute name has ended.
*
* The exception is '%', which must also be escaped
* (as "%0025"), but is obviously still expected in
* the escaped string.
*/
if (strchr("!&()><=| \"", c) != NULL || is_wspace(c)) {
break;
}
}
if (i == max_len) {
/* too long, because we need at least one ')' */
comp_error(comp, "interminable attribute name");
return -1;
}
if (i == 0) {
/* too short! like "User.>= 4" */
comp_error(comp, "empty attribute name");
return -1;
}
if (unlikely(i > CONDITIONAL_ACE_MAX_LENGTH)) {
/*
* This is imprecise; the limit for the whole ACL is 64k.
* However there could be many escapes in the SDDL name which
* would reduce down to single utf16 code units in the
* compiled string.
*/
comp_error(comp, "attribute is way too long (%zu)", i);
return -1;
}
src_len = i;
ok = convert_string_talloc(comp->mem_ctx,
CH_UTF8, CH_UTF16LE,
src, src_len,
&utf16, &utf16_byte_len);
if (!ok) {
comp_error(comp, "could not convert to utf-16");
return -1;
}
/*
* utf16_byte_len is in bytes, we want to count uint16s.
*/
utf16_chars = utf16_byte_len / 2;
/* now the escapes. */
for (i = 0, j = 0;
j < utf16_chars && i < utf16_chars;
j++) {
uint16_t c = utf16[i];
if (c == '%') {
uint16_t v = 0;
size_t end = i + 5;
/*
* we need to read 4 hex characters.
* hex_byte() won't help because that is 8-bit.
*/
if (end > utf16_chars) {
comp_error(comp,
"insufficient room for %% escape");
talloc_free(utf16);
return -1;
}
for (i++; i < end; i++) {
v <<= 4;
c = utf16[i];
if (c >= '0' && c <= '9') {
v += c - '0';
} else if (c >= 'A' && c <= 'F') {
v += c - 'A' + 10;
} else if (c >= 'a' && c <= 'f') {
v += c - 'a' + 10;
} else {
comp_error(comp, "invalid %% escape");
talloc_free(utf16);
return -1;
}
}
/*
* from MS-DTYP 2.5.1.1 Syntax (text, not ABNF), some
* characters must be literals, not escaped.
*/
if ((v >= '0' && v <= '9') ||
(v >= 'A' && v <= 'Z') ||
(v >= 'a' && v <= 'z') ||
(v < 127 &&
strchr("#$'*+-;?@[\\]^_`{}~:/.", v) != NULL)) {
comp_error(comp, "invalid %% escape: "
"'%%%04x' should be literal '%c'",
v, v);
talloc_free(utf16);
return -1;
}
utf16[j] = v;
continue;
}
/*
* Note the characters "!&()><=|% \"" must be escaped per
* [MS-DTYP], but as we found the bounds of this string using
* those in utf-8 at the top of this function, we are not
* going to find them in the utf-16 now.
*
* Also, per [MS-DTYP], un-escaped whitespace is allowed, but
* effectively disallowed by Samba.
*/
utf16[j] = utf16[i];
i++;
}
ok = convert_string_talloc(comp->mem_ctx,
CH_UTF16LE, CH_UTF8,
utf16, j * 2,
&dest->value, &utf8_len);
TALLOC_FREE(utf16);
if (!ok) {
comp_error(comp, "could not convert to utf-16");
return -1;
}
/* returning bytes consumed, not necessarily the length of token */
return src_len;
}
static bool eat_whitespace(struct ace_condition_sddl_compiler_context *comp,
bool trailing)
{
/*
* Advance the offset to the first non-whitespace character.
*
* If trailing is false, there has to be something before the end of
* the string.
*/
while (comp->offset < comp->length) {
if (! is_wspace(comp->sddl[comp->offset])) {
break;
}
comp->offset++;
}
if ((!trailing) && comp->offset == comp->length) {
comp_error(comp, "input ends unexpectedly");
return false;
}
return true;
}
static bool pop_sddl_token(struct ace_condition_sddl_compiler_context *comp,
struct ace_condition_token *token);
static bool write_sddl_token(struct ace_condition_sddl_compiler_context *comp,
struct ace_condition_token token);
static bool pop_write_sddl_token(
struct ace_condition_sddl_compiler_context *comp);
static bool flush_stack_tokens(struct ace_condition_sddl_compiler_context *comp,
uint8_t type)
{
bool ok;
uint8_t precedence = sddl_strings[type].op_precedence;
if (precedence == SDDL_PRECEDENCE_PAREN_START) {
/* paren has a special role */
return true;
}
/*
* Any operators on the top of the stack that have a "higher"
* precedence (tighter binding) to this one get popped off and written
* to the output. "higher" is in quotes because it means lower enum
* value.
*
* This works for binary operators, for example, with "(a == b == c)"
* (which is equivalent to "((a == b) == c)" via the left-to-right
* rule), we have:
* TOKEN dest PROGRAM STACK
* (
* a p
* == s a
* b p a ==
* == s a b ==
* flush stack
* s->p a b == ==
* c p a b ==
* ) a b == c ==
* flush stack
* a b == c ==
*
* but it is not right for unary operators, as in "(!(!(Exists
* a)))". As it turns out though, >= works for the unary
* operators and syntactic rules we have.
*/
while (comp->stack_depth > 0) {
struct ace_condition_token *op =
&comp->stack[comp->stack_depth - 1];
if(sddl_strings[op->type].op_precedence > precedence) {
break;
}
if(sddl_strings[op->type].op_precedence == precedence &&
sddl_strings[op->type].flags & SDDL_FLAG_IS_UNARY_OP) {
break;
}
ok = pop_write_sddl_token(comp);
if (! ok) {
comp_error(comp,
"could not flush '%s' to program",
sddl_strings[op->type].name);
return false;
}
}
return true;
}
static bool push_sddl_token(struct ace_condition_sddl_compiler_context *comp,
struct ace_condition_token token)
{
if (comp->stack_depth >= CONDITIONAL_ACE_MAX_TOKENS - 1) {
comp_error(comp, "excessive recursion");
return false;
}
if (sddl_strings[token.type].op_precedence == SDDL_NOT_AN_OP) {
comp_error(comp,
"wrong kind of token for the SDDL stack: %s",
sddl_strings[token.type].name);
return false;
}
/*
* Any operators on the top of the stack that have a "greater" or
* equal precedence to this one get popped off and written to the
* output.
*/
flush_stack_tokens(comp, token.type);
token.data.op.sddl_position = comp->offset;
comp->stack[comp->stack_depth] = token;
comp->stack_depth++;
if (token.type != CONDITIONAL_ACE_SAMBA_SDDL_PAREN) {
comp->last_token_type = token.type;
}
return true;
}
static bool pop_sddl_token(struct ace_condition_sddl_compiler_context *comp,
struct ace_condition_token *token)
{
if (comp->stack_depth == 0) {
comp_error(comp, "misbalanced expression");
return false;
}
comp->stack_depth--;
*token = comp->stack[comp->stack_depth];
return true;
}
static bool write_sddl_token(struct ace_condition_sddl_compiler_context *comp,
struct ace_condition_token token)
{
/*
* This is adding a token to the program. Normally it will be to the
* main program list, but if we are constructing a composite list, then
* will be redirected there (via comp->target).
*
* We also conservatively track the overall size, so we don't waste
* time compiling something that is way too big.
*/
DBG_INFO("writing %"PRIu32" %x %s\n",
*comp->target_len,
token.type,
sddl_strings[token.type].name);
comp->approx_size++;
if (comp->approx_size > CONDITIONAL_ACE_MAX_TOKENS) {
comp_error(comp, "program is too long "
"(over %d tokens)",
CONDITIONAL_ACE_MAX_TOKENS);
return false;
}
if (token.type != CONDITIONAL_ACE_SAMBA_SDDL_PAREN) {
comp->last_token_type = token.type;
}
comp->target[*comp->target_len] = token;
(*comp->target_len)++;
return true;
}
static bool pop_write_sddl_token(
struct ace_condition_sddl_compiler_context *comp)
{
bool ok;
struct ace_condition_token token = {};
ok = pop_sddl_token(comp, &token);
if (!ok) {
comp_error(comp, "could not pop from op stack");
return false;
}
if (comp->target != comp->program->tokens) {
comp_error(comp, "compiler is seriously confused");
return false;
}
ok = write_sddl_token(comp, token);
if (!ok) {
comp_error(comp,
"could not write '%s' to program",
sddl_strings[token.type].name);
return false;
}
DBG_INFO(" written '%s'\n", sddl_strings[token.type].name);
return true;
}
static bool parse_expression(struct ace_condition_sddl_compiler_context *comp);
static bool parse_composite(struct ace_condition_sddl_compiler_context *comp);
static bool parse_oppy_op(struct ace_condition_sddl_compiler_context *comp)
{
/*
* These ones look like operators and are operators.
*/
bool ok;
struct ace_condition_token token = {};
uint8_t c, d;
uint32_t flag = SDDL_FLAG_EXPECTING_BINARY_OP;
if (comp->offset + 1 >= comp->length) {
comp_error(comp, "syntax error");
return false;
}
token.data.sddl_op.start = comp->offset;
/*
* These are all one or two characters long, and we always have room
* to peek ahead.
*/
c = comp->sddl[comp->offset];
d = comp->sddl[comp->offset + 1];
if (c == '!') {
if (d == '=') {
comp->offset++;
token.type = CONDITIONAL_ACE_TOKEN_NOT_EQUAL;
} else {
token.type = CONDITIONAL_ACE_TOKEN_NOT;
flag = SDDL_FLAG_EXPECTING_UNARY_OP;
}
} else if (c == '=' && d == '=') {
comp->offset++;
token.type = CONDITIONAL_ACE_TOKEN_EQUAL;
} else if (c == '>') {
if (d == '=') {
comp->offset++;
token.type = CONDITIONAL_ACE_TOKEN_GREATER_OR_EQUAL;
} else {
token.type = CONDITIONAL_ACE_TOKEN_GREATER_THAN;
}
} else if (c == '<') {
if (d == '=') {
comp->offset++;
token.type = CONDITIONAL_ACE_TOKEN_LESS_OR_EQUAL;
} else {
token.type = CONDITIONAL_ACE_TOKEN_LESS_THAN;
}
} else if (c == '&' && d == '&') {
comp->offset++;
token.type = CONDITIONAL_ACE_TOKEN_AND;
flag = SDDL_FLAG_EXPECTING_BINARY_LOGIC_OP;
} else if (c == '|' && d == '|') {
comp->offset++;
token.type = CONDITIONAL_ACE_TOKEN_OR;
flag = SDDL_FLAG_EXPECTING_BINARY_LOGIC_OP;
} else {
comp_error(comp, "unknown operator");
return false;
}
if ((comp->state & flag) == 0) {
comp_error(comp, "unexpected operator");
return false;
}
comp->offset++;
ok = push_sddl_token(comp, token);
if (!ok) {
return false;
}
ok = eat_whitespace(comp, true);
return ok;
}
static bool parse_unicode(struct ace_condition_sddl_compiler_context *comp)
{
/*
* This looks like "hello" (including the double quotes).
*
* Fortunately (for now), there is no mechanism for escaping
* double quotes in conditional ace strings, so we can simply
* look for the second quote without worrying about things
* like «\\\"».
*/
struct ace_condition_token token = {};
char *s = NULL;
const uint8_t *src = NULL;
char *utf16 = NULL;
size_t len, max_len;
bool ok;
if (comp->sddl[comp->offset] != '"') {
comp_error(comp, "was expecting '\"' for Unicode string");
return false;
}
comp->offset++;
src = comp->sddl + comp->offset;
max_len = comp->length - comp->offset;
/* strnchr */
for (len = 0; len < max_len; len++) {
if (src[len] == '"') {
break;
}
}
if (len == max_len) {
comp_error(comp, "unterminated unicode string");
return false;
}
/*
* Look, this is wasteful, but it probably doesn't matter. We want to
* check that the string we're putting into the descriptor is valid,
* or we'll see errors down the track.
*/
ok = convert_string_talloc(comp->mem_ctx,
CH_UTF8, CH_UTF16LE,
src, len,
&utf16, NULL);
if (!ok) {
comp_error(comp, "not valid unicode");
return false;
}
TALLOC_FREE(utf16);
s = talloc_array_size(comp->mem_ctx, 1, len + 1);
if (s == NULL) {
comp_error(comp, "allocation error");
return false;
}
memcpy(s, src, len);
s[len] = 0;
comp->offset += len + 1; /* +1 for the final quote */
token.type = CONDITIONAL_ACE_TOKEN_UNICODE;
token.data.unicode.value = s;
return write_sddl_token(comp, token);
}
static bool parse_octet_string(struct ace_condition_sddl_compiler_context *comp)
{
/*
* This looks like '#hhhh...', where each 'hh' is hex for a byte, with
* the weird and annoying complication that '#' can be used to mean
* '0'.
*/
struct ace_condition_token token = {};
size_t length, i;
if (comp->sddl[comp->offset] != '#') {
comp_error(comp, "was expecting '#' for octet string");
return false;
}
comp->offset++;
length = strspn((const char*)(comp->sddl + comp->offset),
"#0123456789abcdefABCDEF");
if (length & 1) {
comp_error(comp, "octet string has odd number of hex digits");
return false;
}
length /= 2;
token.data.bytes = data_blob_talloc_zero(comp->mem_ctx, length);
token.type = CONDITIONAL_ACE_TOKEN_OCTET_STRING;
for (i = 0; i < length; i++) {
/*
* Why not just strhex_to_str()?
*
* Because we need to treat '#' as '0' in octet string values,
* so all of the following are the same
* (equaling {0x10, 0x20, 0x30, 0x0}).
*
* #10203000
* #10203###
* #1#2#3###
* #10203#00
*/
bool ok;
char pair[2];
size_t j = comp->offset + i * 2;
pair[0] = (comp->sddl[j] == '#') ? '0' : comp->sddl[j];
pair[1] = (comp->sddl[j + 1] == '#') ? '0' : comp->sddl[j + 1];
ok = hex_byte(pair, &token.data.bytes.data[i]);
if (!ok) {
talloc_free(token.data.bytes.data);
comp_error(comp, "inexplicable error in octet string");
return false;
}
}
comp->offset += length * 2;
return write_sddl_token(comp, token);
}
static bool parse_ra_octet_string(struct ace_condition_sddl_compiler_context *comp)
{
/*
* Resource attribute octet strings resemble conditional ace octet
* strings, but have some important differences:
*
* 1. The '#' at the start is optional, and if present is
* counted as a zero.
*
* 2. An odd number of characters is implicitly left-padded with a zero.
*
* That is, "abc" means "0abc", "#12" means "0012", "f##"
* means "0f00", and "##" means 00.
*/
struct ace_condition_token token = {};
size_t string_length, bytes_length, i, j;
bool ok;
char pair[2];
string_length = strspn((const char*)(comp->sddl + comp->offset),
"#0123456789abcdefABCDEF");
bytes_length = (string_length + 1) / 2;
if (bytes_length == 0) {
comp_error(comp, "zero length octet bytes");
return false;
}
token.data.bytes = data_blob_talloc_zero(comp->mem_ctx, bytes_length);
if (token.data.bytes.data == NULL) {
return false;
}
token.type = CONDITIONAL_ACE_TOKEN_OCTET_STRING;
j = comp->offset;
i = 0;
if (string_length & 1) {
/*
* An odd number of characters means the first
* character gains an implicit 0 for the high nybble.
*/
pair[0] = 0;
pair[1] = (comp->sddl[0] == '#') ? '0' : comp->sddl[0];
ok = hex_byte(pair, &token.data.bytes.data[i]);
if (!ok) {
goto fail;
}
j++;
i++;
}
for (; i < bytes_length; i++) {
/*
* Why not just strhex_to_str() ?
*
* Because we need to treat '#' as '0' in octet string values.
*/
if (comp->length - j < 2) {
goto fail;
}
pair[0] = (comp->sddl[j] == '#') ? '0' : comp->sddl[j];
pair[1] = (comp->sddl[j + 1] == '#') ? '0' : comp->sddl[j + 1];
ok = hex_byte(pair, &token.data.bytes.data[i]);
if (!ok) {
goto fail;
}
j += 2;
}
comp->offset = j;
return write_sddl_token(comp, token);
fail:
comp_error(comp, "inexplicable error in octet string");
talloc_free(token.data.bytes.data);
return false;
}
static bool parse_sid(struct ace_condition_sddl_compiler_context *comp)
{
struct dom_sid *sid = NULL;
const uint8_t *sidstr = NULL;
struct ace_condition_token token = {};
size_t end;
if (comp->length - comp->offset < 7) {
/* minimum: "SID(AA)" */
comp_error(comp, "no room for a complete SID");
return false;
}
/* conditional ACE SID string */
if (comp->sddl[comp->offset ] != 'S' ||
comp->sddl[comp->offset + 1] != 'I' ||
comp->sddl[comp->offset + 2] != 'D' ||
comp->sddl[comp->offset + 3] != '(') {
comp_error(comp, "malformed SID() constructor");
return false;
} else {
comp->offset += 4;
}
sidstr = comp->sddl + comp->offset;
sid = sddl_decode_sid(comp->mem_ctx,
(const char **)&sidstr,
comp->domain_sid);
if (sid == NULL) {
comp_error(comp, "could not parse SID");
return false;
}
end = sidstr - comp->sddl;
if (end >= comp->length || end < comp->offset) {
comp_error(comp, "apparent overflow in SID parsing");
return false;
}
comp->offset = end;
/*
* offset is now at the end of the SID, but we need to account
* for the ')'.
*/
if (comp->sddl[comp->offset] != ')') {
comp_error(comp, "expected ')' to follow SID");
return false;
}
comp->offset++;
token.type = CONDITIONAL_ACE_TOKEN_SID;
token.data.sid.sid = *sid;
return write_sddl_token(comp, token);
}
static bool parse_ra_sid(struct ace_condition_sddl_compiler_context *comp)
{
struct dom_sid *sid = NULL;
const uint8_t *sidstr = NULL;
struct ace_condition_token token = {};
size_t end;
if ((comp->state & SDDL_FLAG_EXPECTING_LITERAL) == 0) {
comp_error(comp, "did not expect a SID here");
return false;
}
/*
* Here we are parsing a resource attribute ACE which doesn't
* have the SID() wrapper around the SID string (unlike a
* conditional ACE).
*
* The resource ACE doesn't need this because there is no
* ambiguity with local attribute names, besides which the
* type has already been specified earlier in the ACE.
*/
if (comp->length - comp->offset < 2){
comp_error(comp, "no room for a complete SID");
return false;
}
sidstr = comp->sddl + comp->offset;
sid = sddl_decode_sid(comp->mem_ctx,
(const char **)&sidstr,
comp->domain_sid);
if (sid == NULL) {
comp_error(comp, "could not parse SID");
return false;
}
end = sidstr - comp->sddl;
if (end >= comp->length || end < comp->offset) {
comp_error(comp, "apparent overflow in SID parsing");
return false;
}
comp->offset = end;
token.type = CONDITIONAL_ACE_TOKEN_SID;
token.data.sid.sid = *sid;
return write_sddl_token(comp, token);
}
static bool parse_int(struct ace_condition_sddl_compiler_context *comp)
{
/*
* This one is relatively simple. strtoll() does the work.
*/
long long v;
struct ace_condition_token token = {};
const char *start = (const char *)comp->sddl + comp->offset;
char *end = NULL;
const char *first_digit = start;
size_t len;
errno = 0;
v = strtoll(start, &end, 0);
if (errno != 0) {
comp_error(comp, "bad integer: %s", strerror(errno));
return false;
}
len = end - start;
if (len == 0) {
comp_error(comp, "unexpected non-integer");
return false;
}
if (comp->offset + len > comp->length) {
comp_error(comp, "impossible integer length: %zu!", len);
return false;
}
comp->offset += len;
/*
* Record the base and sign, which are used for recreating the SDDL.
*
* 'Sign' indicates whether there is a '+' or '-' sign. Base indicates
* whether the number was in hex, octal, or decimal. These make no
* difference to the evaluation of the ACE, just the display.
*
* This would not work reliably if eat_whitespace() is not called
* before parse_int(), but a) we know it is, and b) we don't *really*
* care if we lose these display hints.
*/
if (*start == '-') {
token.data.int64.sign = CONDITIONAL_ACE_INT_SIGN_NEGATIVE;
first_digit++;
} else if (*start == '+') {
token.data.int64.sign = CONDITIONAL_ACE_INT_SIGN_POSITIVE;
first_digit++;
} else {
token.data.int64.sign = CONDITIONAL_ACE_INT_SIGN_NONE;
}
if (*first_digit == '0' && (end - first_digit) > 1) {
if ((end - first_digit > 2) &&
(first_digit[1] == 'x' ||
first_digit[1] == 'X')) {
token.data.int64.base = CONDITIONAL_ACE_INT_BASE_16;
} else {
token.data.int64.base = CONDITIONAL_ACE_INT_BASE_8;
}
} else {
token.data.int64.base = CONDITIONAL_ACE_INT_BASE_10;
}
token.data.int64.value = v;
token.type = CONDITIONAL_ACE_TOKEN_INT64;
return write_sddl_token(comp, token);
}
static bool parse_uint(struct ace_condition_sddl_compiler_context *comp)
{
struct ace_condition_token *tok = NULL;
bool ok = parse_int(comp);
if (ok == false) {
return false;
}
/*
* check that the token's value is positive.
*/
if (comp->target_len == 0) {
return false;
}
tok = &comp->target[*comp->target_len - 1];
if (tok->type != CONDITIONAL_ACE_TOKEN_INT64) {
return false;
}
if (tok->data.int64.value < 0) {
comp_error(comp, "invalid resource ACE value for unsigned TU claim");
return false;
}
return true;
}
static bool parse_bool(struct ace_condition_sddl_compiler_context *comp)
{
struct ace_condition_token *tok = NULL;
bool ok = parse_int(comp);
if (ok == false || comp->target_len == 0) {
return false;
}
/*
* check that the token is 0 or 1.
*/
tok = &comp->target[*comp->target_len - 1];
if (tok->type != CONDITIONAL_ACE_TOKEN_INT64) {
return false;
}
if (tok->data.int64.value != 0 && tok->data.int64.value != 1) {
comp_error(comp, "invalid resource ACE Boolean value");
return false;
}
return true;
}
static bool could_be_an_int(struct ace_condition_sddl_compiler_context *comp)
{
const char *start = (const char*)(comp->sddl + comp->offset);
char* end = NULL;
if ((comp->state & SDDL_FLAG_EXPECTING_LITERAL) == 0) {
return false;
}
errno = 0;
/*
* See, we don't care about the strtoll return value, only
* whether it succeeds or not and what it finds at the end. If
* it succeeds, parse_int() will do it again for the value.
*
* Note that an out of range int will raise ERANGE (probably
* 34), so it will be read as a local attribute.
*/
strtoll(start, &end, 0);
if (errno != 0 ||
end == start ||
end >= (const char*)comp->sddl + comp->length) {
return false;
}
/*
* We know *some* characters form an int, but if we run right
* into other attr1 characters (basically, letters), we won't
* count it as an int.
*
* For example, the "17" in "17p" is not an int. The "17" in
* "17||" is.
*/
if (is_attr_char1(*end)) {
return false;
}
return true;
}
static bool parse_word(struct ace_condition_sddl_compiler_context *comp)
{
/*
* Sometimes a bare word must be a local attribute, while in other
* cases it could also be a member-of or exists operator. Sometimes it
* could actually be a SID, which we discover when we've read as far
* as "SID(". Sometimes it might be a literal integer (attribute
* names can also consist entirely of digits).
*
* When it is an operator name, we have the complication that a match
* does not necessarily end the token. Consider "Member_of_Any" which
* contains the operator "Member_of". According to [MS-DTYP], a space
* is not necessary between the operator and the next token, but it
* does seem to be required for Windows 2022.
*
* Also, "Member_of" et. al. *could* be valid local attributes, which
* would make "(Member_of == 123)" a valid expression that we will
* fail to parse. This is not much of an issue for Samba AD where
* local attributes are not used.
*
* Operators are matched case-insensitively.
*
* There's another kind of attribute that starts with a '@', which we
* deal with in parse_attr2(). Those ones have full unicode glory;
* these ones are ASCII only.
*/
size_t i, j, k;
bool ok;
uint8_t candidates[8];
size_t n_candidates = 0;
struct ace_condition_token token = {};
bool expecting_unary = comp->state & SDDL_FLAG_EXPECTING_UNARY_OP;
bool expecting_binary = comp->state & SDDL_FLAG_EXPECTING_BINARY_OP;
bool expecting_attr = comp->state & SDDL_FLAG_EXPECTING_LOCAL_ATTR;
bool expecting_literal = comp->state & SDDL_FLAG_EXPECTING_LITERAL;
const uint8_t *start = comp->sddl + comp->offset;
uint8_t c = start[0];
char *s = NULL;
if (! is_attr_char1(*start)) {
/* we shouldn't get here, because we peeked first */
return false;
}
/*
* We'll look for a SID first, because it simplifies the rest.
*/
if (expecting_literal &&
comp->offset + 4 < comp->length &&
start[0] == 'S' &&
start[1] == 'I' &&
start[2] == 'D' &&
start[3] == '(') {
/* actually, we are parsing a SID. */
return parse_sid(comp);
}
if (expecting_binary || expecting_unary) {
/*
* Collect up the operators that can possibly be used
* here, including only those that start with the
* current letter and have the right arity/syntax.
*
* We don't expect more than 5 (for 'N', beginning the
* "Not_..." unary ops), and we'll winnow them down as
* we progress through the word.
*/
int uc = toupper(c);
for (i = 0; i < 256; i++) {
const struct sddl_data *d = &sddl_strings[i];
if (sddl_strings[i].op_precedence != SDDL_NOT_AN_OP &&
uc == toupper((unsigned char)d->name[0])) {
if (d->flags & SDDL_FLAG_IS_UNARY_OP) {
if (!expecting_unary) {
continue;
}
} else if (!expecting_binary) {
continue;
}
candidates[n_candidates] = i;
n_candidates++;
if (n_candidates == ARRAY_SIZE(candidates)) {
/* impossible, really. */
return false;
}
}
}
} else if (could_be_an_int(comp)) {
/*
* if looks like an integer, and we expect an integer, it is
* an integer. If we don't expect an integer, it is a local
* attribute with a STUPID NAME. Or an error.
*/
return parse_int(comp);
} else if (! expecting_attr) {
comp_error(comp, "did not expect this word here");
return false;
}
i = 1;
while (comp->offset + i < comp->length) {
c = start[i];
if (! is_attr_char1(c)) {
break;
}
if (n_candidates != 0) {
/*
* Filter out candidate operators that no longer
* match.
*/
int uc = toupper(c);
k = 0;
for (j = 0; j < n_candidates; j++) {
size_t o = candidates[j];
uint8_t c2 = sddl_strings[o].name[i];
if (uc == toupper(c2)) {
candidates[k] = candidates[j];
k++;
}
}
n_candidates = k;
}
i++;
}
/*
* We have finished and there is a complete word. If it could be an
* operator we'll assume it is one.
*
* A complication is we could have matched more than one operator, for
* example "Member_of" and "Member_of_Any", so we have to look through
* the list of candidates for the one that ends.
*/
if (n_candidates != 0) {
for (j = 0; j < n_candidates; j++) {
size_t o = candidates[j];
if (sddl_strings[o].name[i] == '\0') {
/* it is this one */
if (!comp->allow_device &&
(sddl_strings[o].flags & SDDL_FLAG_DEVICE))
{
comp_error(
comp,
"a device‐relative expression "
"will never evaluate to true "
"in this context (did you "
"intend a user‐relative "
"expression?)");
return false;
}
token.type = o;
token.data.sddl_op.start = comp->offset;
comp->offset += i;
ok = push_sddl_token(comp, token);
return ok;
}
}
}
/*
* if looks like an integer, and we expect an integer, it is
* an integer. If we don't expect an integer, it is a local
* attribute with a STUPID NAME.
*/
if (could_be_an_int(comp)) {
return parse_int(comp);
}
if (! expecting_attr) {
comp_error(comp, "word makes no sense here");
return false;
}
/* it's definitely an attribute name */
token.type = CONDITIONAL_ACE_LOCAL_ATTRIBUTE;
if (comp->offset + i >= comp->length) {
comp_error(comp, "missing trailing ')'?");
return false;
}
s = talloc_memdup(comp->mem_ctx, start, i + 1);
if (s == NULL) {
comp_error(comp, "allocation error");
return false;
}
s[i] = 0;
token.data.local_attr.value = s;
comp->offset += i;
return write_sddl_token(comp, token);
}
static bool parse_attr2(struct ace_condition_sddl_compiler_context *comp)
{
/*
* Attributes in the form @class.attr
*
* class can be "User", "Device", or "Resource", case insensitive.
*/
size_t i;
bool ok;
size_t len;
struct ace_condition_token token = {};
if ((comp->state & SDDL_FLAG_EXPECTING_NON_LOCAL_ATTR) == 0) {
comp_error(comp, "did not expect @attr here");
return false;
}
if (comp->sddl[comp->offset] != '@') {
comp_error(comp, "Expected '@'");
return false;
}
comp->offset++;
for (i = 0; i < ARRAY_SIZE(sddl_attr_types); i++) {
int ret;
size_t attr_len = strlen(sddl_attr_types[i].name);
if (attr_len >= comp->length - comp->offset) {
continue;
}
ret = strncasecmp(sddl_attr_types[i].name,
(const char *) (comp->sddl + comp->offset),
attr_len);
if (ret == 0) {
const uint8_t code = sddl_attr_types[i].code;
if (!comp->allow_device &&
(sddl_strings[code].flags & SDDL_FLAG_DEVICE))
{
comp_error(comp,
"a device attribute is not "
"applicable in this context (did "
"you intend a user attribute?)");
return false;
}
token.type = code;
comp->offset += attr_len;
break;
}
}
if (i == ARRAY_SIZE(sddl_attr_types)) {
comp_error(comp, "unknown attribute class");
return false;
}
/*
* Now we are past the class and the '.', and into the
* attribute name. The attribute name can be almost
* anything, but some characters need to be escaped.
*/
len = read_attr2_string(comp, &token.data.unicode);
if (len == -1) {
/* read_attr2_string has set a message */
return false;
}
ok = write_sddl_token(comp, token);
if (! ok) {
return false;
}
comp->offset += len;
ok = eat_whitespace(comp, false);
return ok;
}
static bool parse_literal(struct ace_condition_sddl_compiler_context *comp,
bool in_composite)
{
uint8_t c = comp->sddl[comp->offset];
if (!(comp->state & SDDL_FLAG_EXPECTING_LITERAL)) {
comp_error(comp, "did not expect to be parsing a literal now");
return false;
}
switch(c) {
case '#':
return parse_octet_string(comp);
case '"':
return parse_unicode(comp);
case 'S':
return parse_sid(comp);
case '{':
if (in_composite) {
/* nested composites are not supported */
return false;
} else {
return parse_composite(comp);
}
default:
if (strchr("1234567890-+", c) != NULL) {
return parse_int(comp);
}
}
if (c > 31 && c < 127) {
comp_error(comp,
"unexpected byte 0x%02x '%c' parsing literal", c, c);
} else {
comp_error(comp, "unexpected byte 0x%02x parsing literal", c);
}
return false;
}
static bool parse_composite(struct ace_condition_sddl_compiler_context *comp)
{
/*
* This jumps into a different parser, expecting a comma separated
* list of literal values, which might include nested literal
* composites.
*
* To handle the nesting, we redirect the pointers that determine
* where write_sddl_token() writes.
*/
bool ok;
bool first = true;
struct ace_condition_token token = {
.type = CONDITIONAL_ACE_TOKEN_COMPOSITE
};
uint32_t start = comp->offset;
size_t alloc_size;
struct ace_condition_token *old_target = comp->target;
uint32_t *old_target_len = comp->target_len;
if (comp->sddl[start] != '{') {
comp_error(comp, "expected '{' for composite list");
return false;
}
if (!(comp->state & SDDL_FLAG_EXPECTING_LITERAL)) {
comp_error(comp, "did not expect '{' for composite list");
return false;
}
comp->offset++; /* past '{' */
/*
* the worst case is one token for every two bytes: {1,1,1}, and we
* allocate for that (counting commas and finding '}' gets hard because
* string literals).
*/
alloc_size = MIN((comp->length - start) / 2 + 1,
CONDITIONAL_ACE_MAX_LENGTH);
token.data.composite.tokens = talloc_array(
comp->mem_ctx,
struct ace_condition_token,
alloc_size);
if (token.data.composite.tokens == NULL) {
comp_error(comp, "allocation failure");
return false;
}
comp->target = token.data.composite.tokens;
comp->target_len = &token.data.composite.n_members;
/*
* in this loop we are looking for:
*
* a) possible whitespace.
* b) a comma (or terminating '}')
* c) more possible whitespace
* d) a literal
*
* Failures use a goto to reset comp->target, just in case we ever try
* continuing after error.
*/
while (comp->offset < comp->length) {
uint8_t c;
ok = eat_whitespace(comp, false);
if (! ok) {
goto fail;
}
c = comp->sddl[comp->offset];
if (c == '}') {
comp->offset++;
break;
}
if (!first) {
if (c != ',') {
comp_error(comp,
"malformed composite (expected comma)");
goto fail;
}
comp->offset++;
ok = eat_whitespace(comp, false);
if (! ok) {
goto fail;
}
}
first = false;
if (*comp->target_len >= alloc_size) {
comp_error(comp,
"Too many tokens in composite "
"(>= %"PRIu32" tokens)",
*comp->target_len);
goto fail;
}
ok = parse_literal(comp, true);
if (!ok) {
goto fail;
}
}
comp->target = old_target;
comp->target_len = old_target_len;
write_sddl_token(comp, token);
return true;
fail:
talloc_free(token.data.composite.tokens);
comp->target = old_target;
comp->target_len = old_target_len;
return false;
}
static bool parse_paren_literal(struct ace_condition_sddl_compiler_context *comp)
{
bool ok;
if (comp->sddl[comp->offset] != '(') {
comp_error(comp, "expected '('");
return false;
}
comp->offset++;
ok = parse_literal(comp, false);
if (!ok) {
return false;
}
if (comp->sddl[comp->offset] != ')') {
comp_error(comp, "expected ')'");
return false;
}
comp->offset++;
return true;
}
static bool parse_expression(struct ace_condition_sddl_compiler_context *comp)
{
/*
* This expects a parenthesised expression.
*/
bool ok;
struct ace_condition_token token = {};
uint32_t start = comp->offset;
if (comp->state & SDDL_FLAG_EXPECTING_PAREN_LITERAL) {
/*
* Syntactically we allow parentheses to wrap a
* literal value after a Member_of or >= op, but we
* want to remember that it just wants a single
* literal, not a general expression.
*/
return parse_paren_literal(comp);
}
if (comp->sddl[start] != '(') {
comp_error(comp, "expected '('");
return false;
}
if (!(comp->state & SDDL_FLAG_EXPECTING_PAREN)) {
comp_error(comp, "did not expect '('");
return false;
}
token.type = CONDITIONAL_ACE_SAMBA_SDDL_PAREN;
token.data.sddl_op.start = start;
ok = push_sddl_token(comp, token);
if (!ok) {
return false;
}
comp->offset++; /* over the '(' */
comp->state = SDDL_FLAGS_EXPR_START;
DBG_INFO("%3"PRIu32": (\n", comp->offset);
comp->state |= SDDL_FLAG_NOT_EXPECTING_END_PAREN;
while (comp->offset < comp->length) {
uint8_t c;
ok = eat_whitespace(comp, false);
if (! ok) {
return false;
}
c = comp->sddl[comp->offset];
if (c == '(') {
ok = parse_expression(comp);
} else if (c == ')') {
if (comp->state & (SDDL_FLAG_IS_BINARY_OP |
SDDL_FLAG_IS_UNARY_OP)) {
/*
* You can't have "(a ==)" or "(!)"
*/
comp_error(comp,
"operator lacks right hand argument");
return false;
}
if (comp->state & SDDL_FLAG_NOT_EXPECTING_END_PAREN) {
/*
* You can't have "( )"
*/
comp_error(comp, "empty expression");
return false;
}
break;
} else if (c == '@') {
ok = parse_attr2(comp);
} else if (strchr("!<>=&|", c)) {
ok = parse_oppy_op(comp);
} else if (is_attr_char1(c)) {
ok = parse_word(comp);
} else if (comp->state & SDDL_FLAG_EXPECTING_LITERAL) {
ok = parse_literal(comp, false);
} else {
if (c > 31 && c < 127) {
comp_error(comp,
"unexpected byte 0x%02x '%c'", c, c);
} else {
comp_error(comp, "unexpected byte 0x%02x", c);
}
ok = false;
}
if (! ok) {
return false;
}
/*
* what did we just find? Set what we expect accordingly.
*/
comp->state = sddl_strings[comp->last_token_type].flags;
DBG_INFO("%3"PRIu32": %s\n",
comp->offset,
sddl_strings[comp->last_token_type].name);
}
ok = eat_whitespace(comp, false);
if (!ok) {
return false;
}
if (comp->sddl[comp->offset] != ')') {
comp_error(comp, "expected ')' to match '(' at %"PRIu32, start);
return false;
}
/*
* we won't comp->offset++ until after these other error checks, so
* that their messages have consistent locations.
*/
ok = flush_stack_tokens(comp, CONDITIONAL_ACE_SAMBA_SDDL_PAREN_END);
if (!ok) {
return false;
}
if (comp->stack_depth == 0) {
comp_error(comp, "mysterious nesting error between %"
PRIu32" and here",
start);
return false;
}
token = comp->stack[comp->stack_depth - 1];
if (token.type != CONDITIONAL_ACE_SAMBA_SDDL_PAREN) {
comp_error(comp, "nesting error between %"PRIu32" and here",
start);
return false;
}
if (token.data.sddl_op.start != start) {
comp_error(comp, "')' should match '(' at %"PRIu32
", not %"PRIu32,
token.data.sddl_op.start, start);
return false;
}
comp->stack_depth--;
DBG_INFO("%3"PRIu32": )\n", comp->offset);
comp->offset++; /* for the ')' */
comp->last_token_type = CONDITIONAL_ACE_SAMBA_SDDL_PAREN_END;
comp->state = sddl_strings[comp->last_token_type].flags;
ok = eat_whitespace(comp, true);
return ok;
}
static bool init_compiler_context(
TALLOC_CTX *mem_ctx,
struct ace_condition_sddl_compiler_context *comp,
const enum ace_condition_flags ace_condition_flags,
const char *sddl,
size_t max_length,
size_t max_stack)
{
struct ace_condition_script *program = NULL;
comp->sddl = (const uint8_t*)sddl;
comp->mem_ctx = mem_ctx;
program = talloc_zero(mem_ctx, struct ace_condition_script);
if (program == NULL) {
return false;
}
/*
* For the moment, we allocate for the worst case up front.
*/
program->tokens = talloc_array(program,
struct ace_condition_token,
max_length);
if (program->tokens == NULL) {
TALLOC_FREE(program);
return false;
}
program->stack = talloc_array(program,
struct ace_condition_token,
max_stack + 1);
if (program->stack == NULL) {
TALLOC_FREE(program);
return false;
}
comp->program = program;
/* we can borrow the program stack for the operator stack */
comp->stack = program->stack;
comp->target = program->tokens;
comp->target_len = &program->length;
comp->length = strlen(sddl);
comp->state = SDDL_FLAG_EXPECTING_PAREN;
comp->allow_device = ace_condition_flags & ACE_CONDITION_FLAG_ALLOW_DEVICE;
return true;
}
/*
* Compile SDDL conditional ACE conditions.
*
* @param mem_ctx
* @param sddl - the string to be parsed
* @param ace_condition_flags - flags controlling compiler behaviour
* @param message - on error, a pointer to a compiler message
* @param message_offset - where the error occurred
* @param consumed_length - how much of the SDDL was used
* @return a struct ace_condition_script (or NULL).
*/
struct ace_condition_script * ace_conditions_compile_sddl(
TALLOC_CTX *mem_ctx,
const enum ace_condition_flags ace_condition_flags,
const char *sddl,
const char **message,
size_t *message_offset,
size_t *consumed_length)
{
bool ok;
struct ace_condition_sddl_compiler_context comp = {};
*message = NULL;
*message_offset = 0;
ok = init_compiler_context(mem_ctx,
&comp,
ace_condition_flags,
sddl,
CONDITIONAL_ACE_MAX_LENGTH,
CONDITIONAL_ACE_MAX_TOKENS);
if (!ok) {
return NULL;
}
ok = parse_expression(&comp);
if (!ok) {
goto error;
}
if (comp.stack_depth != 0) {
comp_error(&comp, "incomplete expression");
goto error;
}
if (consumed_length != NULL) {
*consumed_length = comp.offset;
}
*message = comp.message;
*message_offset = comp.message_offset;
return comp.program;
error:
*message = comp.message;
*message_offset = comp.message_offset;
TALLOC_FREE(comp.program);
return NULL;
}
static bool parse_resource_attr_list(
struct ace_condition_sddl_compiler_context *comp,
char attr_type_char)
{
/*
* This is a bit like parse_composite() above, but with the following
* differences:
*
* - it doesn't want '{...}' around the list.
* - if there is just one value, it is not a composite
* - all the values must be the expected type.
* - there is no nesting.
* - SIDs are not written with SID(...) around them.
*/
bool ok;
bool first = true;
struct ace_condition_token composite = {
.type = CONDITIONAL_ACE_TOKEN_COMPOSITE
};
uint32_t start = comp->offset;
size_t alloc_size;
struct ace_condition_token *old_target = comp->target;
uint32_t *old_target_len = comp->target_len;
comp->state = SDDL_FLAG_EXPECTING_LITERAL;
/*
* the worst case is one token for every two bytes: {1,1,1}, and we
* allocate for that (counting commas and finding '}' gets hard because
* string literals).
*/
alloc_size = MIN((comp->length - start) / 2 + 1,
CONDITIONAL_ACE_MAX_LENGTH);
composite.data.composite.tokens = talloc_array(
comp->mem_ctx,
struct ace_condition_token,
alloc_size);
if (composite.data.composite.tokens == NULL) {
comp_error(comp, "allocation failure");
return false;
}
comp->target = composite.data.composite.tokens;
comp->target_len = &composite.data.composite.n_members;
/*
* in this loop we are looking for:
*
* a) possible whitespace.
* b) a comma (or terminating ')')
* c) more possible whitespace
* d) a literal, of the right type (checked after)
*
* Failures use a goto to reset comp->target, just in case we ever try
* continuing after error.
*/
while (comp->offset < comp->length) {
uint8_t c;
ok = eat_whitespace(comp, false);
if (! ok) {
goto fail;
}
c = comp->sddl[comp->offset];
if (c == ')') {
break;
}
if (!first) {
if (c != ',') {
comp_error(comp,
"malformed resource attribute ACE "
"(expected comma)");
goto fail;
}
comp->offset++;
ok = eat_whitespace(comp, false);
if (! ok) {
goto fail;
}
}
first = false;
if (*comp->target_len >= alloc_size) {
comp_error(comp,
"Too many tokens in resource attribute ACE "
"(>= %"PRIu32" tokens)",
*comp->target_len);
goto fail;
}
switch(attr_type_char) {
case 'X':
ok = parse_ra_octet_string(comp);
break;
case 'S':
ok = parse_unicode(comp);
break;
case 'U':
ok = parse_uint(comp);
break;
case 'B':
ok = parse_bool(comp);
break;
case 'I':
ok = parse_int(comp);
break;
case 'D':
ok = parse_ra_sid(comp);
break;
default:
/* it's a mystery we got this far */
comp_error(comp,
"unknown attribute type T%c",
attr_type_char);
goto fail;
}
if (!ok) {
goto fail;
}
if (*comp->target_len == 0) {
goto fail;
}
}
comp->target = old_target;
comp->target_len = old_target_len;
/*
* If we only ended up collecting one token into the composite, we
* write that instead.
*/
if (composite.data.composite.n_members == 1) {
ok = write_sddl_token(comp, composite.data.composite.tokens[0]);
talloc_free(composite.data.composite.tokens);
} else {
ok = write_sddl_token(comp, composite);
}
if (! ok) {
goto fail;
}
return true;
fail:
comp->target = old_target;
comp->target_len = old_target_len;
TALLOC_FREE(composite.data.composite.tokens);
return false;
}
struct CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1 *sddl_decode_resource_attr (
TALLOC_CTX *mem_ctx,
const char *str,
size_t *length)
{
/*
* Resource attribute ACEs define claims in object SACLs. They look like
*
* "(RA; «flags» ;;;;WD;( «attribute-data» ))"
*
* attribute-data = DQUOTE 1*attr-char2 DQUOTE "," \
* ( TI-attr / TU-attr / TS-attr / TD-attr / TX-attr / TB-attr )
* TI-attr = "TI" "," attr-flags *("," int-64)
* TU-attr = "TU" "," attr-flags *("," uint-64)
* TS-attr = "TS" "," attr-flags *("," char-string)
* TD-attr = "TD" "," attr-flags *("," sid-string)
* TX-attr = "TX" "," attr-flags *("," octet-string)
* TB-attr = "TB" "," attr-flags *("," ( "0" / "1" ) )
*
* and the data types are *mostly* parsed in the SDDL way,
* though there are significant differences for octet-strings.
*
* At this point we only have the "(«attribute-data»)".
*
* What we do is set up a conditional ACE compiler to be expecting a
* literal, and ask it to parse the strings between the commas. It's a
* hack.
*/
bool ok;
struct CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1 *claim = NULL;
struct ace_condition_sddl_compiler_context comp = {};
char attr_type;
struct ace_condition_token *tok;
uint32_t flags;
size_t len;
struct ace_condition_unicode attr_name = {};
ok = init_compiler_context(mem_ctx,
&comp,
ACE_CONDITION_FLAG_ALLOW_DEVICE,
str,
3,
3);
if (!ok) {
return NULL;
}
if (comp.length < 6 || comp.length > CONDITIONAL_ACE_MAX_LENGTH) {
DBG_WARNING("invalid resource attribute: '%s'\n", str);
goto error;
}
/*
* Resource attribute ACEs list SIDs in a bare form "S-1-2-3", while
* conditional ACEs use a wrapper syntax "SID(S-1-2-3)". As almost
* everything is the same, we are reusing the conditional ACE parser,
* with a flag set to tell the SID parser which form to expect.
*/
/* Most examples on the web have leading whitespace */
ok = eat_whitespace(&comp, false);
if (!ok) {
return NULL;
}
if (comp.sddl[comp.offset] != '(' ||
comp.sddl[comp.offset + 1] != '"') {
DBG_WARNING("invalid resource attribute -- expected '(\"'\n");
goto error;
}
comp.offset += 2;
/*
* Read the name. Here we are not reading a token into comp->program,
* just into a unicode blob.
*/
len = read_attr2_string(&comp, &attr_name);
if (len == -1) {
DBG_WARNING("invalid resource attr name: %s\n", str);
goto error;
}
comp.offset += len;
ok = eat_whitespace(&comp, false);
if (comp.offset + 6 > comp.length) {
DBG_WARNING("invalid resource attribute (too short): '%s'\n",
str);
goto error;
}
/*
* now we have the name. Next comes '",«T[IUSDXB]»,' followed
* by the flags, which are a 32 bit number.
*/
if (comp.sddl[comp.offset] != '"' ||
comp.sddl[comp.offset + 1] != ','||
comp.sddl[comp.offset + 2] != 'T') {
DBG_WARNING("expected '\",T[IUSDXB]' after attr name\n");
goto error;
}
attr_type = comp.sddl[comp.offset + 3];
if (comp.sddl[comp.offset + 4] != ',') {
DBG_WARNING("expected ',' after attr type\n");
goto error;
}
comp.offset += 5;
comp.state = SDDL_FLAG_EXPECTING_LITERAL;
ok = parse_literal(&comp, false);
if (!ok ||
comp.program->length != 1) {
DBG_WARNING("invalid attr flags: %s\n", str);
goto error;
}
tok = &comp.program->tokens[0];
if (tok->type != CONDITIONAL_ACE_TOKEN_INT64 ||
tok->data.int64.value < 0 ||
tok->data.int64.value > UINT32_MAX) {
DBG_WARNING("invalid attr flags (want 32 bit int): %s\n", str);
goto error;
}
flags = tok->data.int64.value;
if (flags & 0xff00) {
DBG_WARNING("invalid attr flags, "
"stepping on reserved 0xff00 range: %s\n",
str);
goto error;
}
if (comp.offset + 3 > comp.length) {
DBG_WARNING("invalid resource attribute (too short): '%s'\n",
str);
goto error;
}
if (comp.sddl[comp.offset] != ',') {
DBG_WARNING("invalid resource attribute ace\n");
goto error;
}
comp.offset++;
ok = parse_resource_attr_list(&comp, attr_type);
if (!ok || comp.program->length != 2) {
DBG_WARNING("invalid attribute type or value: T%c, %s\n",
attr_type, str);
goto error;
}
if (comp.sddl[comp.offset] != ')') {
DBG_WARNING("expected trailing ')'\n");
goto error;
}
comp.offset++;
*length = comp.offset;
ok = ace_token_to_claim_v1(mem_ctx,
attr_name.value,
&comp.program->tokens[1],
&claim,
flags);
if (!ok) {
goto error;
}
TALLOC_FREE(comp.program);
return claim;
error:
TALLOC_FREE(comp.program);
return NULL;
}
static bool write_resource_attr_from_token(struct sddl_write_context *ctx,
const struct ace_condition_token *tok)
{
/*
* this is a helper for sddl_resource_attr_from_claim(),
* recursing into composites if necessary.
*/
bool ok;
char *sid = NULL;
size_t i;
const struct ace_condition_composite *c = NULL;
switch (tok->type) {
case CONDITIONAL_ACE_TOKEN_INT64:
/*
* Note that this includes uint and bool claim types,
* but we don't check the validity of the ranges (0|1
* and >=0, respectively), rather we trust the claim
* to be self-consistent in this regard. Going the
* other way, string-to-claim, we do check.
*/
return sddl_write_int(ctx, tok);
case CONDITIONAL_ACE_TOKEN_UNICODE:
return sddl_write_unicode(ctx, tok);
case CONDITIONAL_ACE_TOKEN_SID:
/* unlike conditional ACE, SID does not have a "SID()" wrapper. */
sid = sddl_encode_sid(ctx->mem_ctx, &tok->data.sid.sid, NULL);
if (sid == NULL) {
return false;
}
return sddl_write(ctx, sid);
case CONDITIONAL_ACE_TOKEN_OCTET_STRING:
return sddl_write_ra_octet_string(ctx, tok);
case CONDITIONAL_ACE_TOKEN_COMPOSITE:
/*
* write each token, separated by commas. If there
* were nested composites, this would flatten them,
* but that isn't really possible because the token we
* are dealing with came from a claim, which has no
* facility for nesting.
*/
c = &tok->data.composite;
for(i = 0; i < c->n_members; i++) {
ok = write_resource_attr_from_token(ctx, &c->tokens[i]);
if (!ok) {
return false;
}
if (i != c->n_members - 1) {
ok = sddl_write(ctx, ",");
if (!ok) {
return false;
}
}
}
return true;
default:
/* We really really don't expect to get here */
return false;
}
}
char *sddl_resource_attr_from_claim(
TALLOC_CTX *mem_ctx,
const struct CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1 *claim)
{
char *s = NULL;
char attr_type;
bool ok;
struct ace_condition_token tok = {};
struct sddl_write_context ctx = {};
TALLOC_CTX *tmp_ctx = NULL;
char *name = NULL;
size_t name_len;
switch(claim->value_type) {
case CLAIM_SECURITY_ATTRIBUTE_TYPE_INT64:
attr_type = 'I';
break;
case CLAIM_SECURITY_ATTRIBUTE_TYPE_UINT64:
attr_type = 'U';
break;
case CLAIM_SECURITY_ATTRIBUTE_TYPE_STRING:
attr_type = 'S';
break;
case CLAIM_SECURITY_ATTRIBUTE_TYPE_SID:
attr_type = 'D';
break;
case CLAIM_SECURITY_ATTRIBUTE_TYPE_BOOLEAN:
attr_type = 'B';
break;
case CLAIM_SECURITY_ATTRIBUTE_TYPE_OCTET_STRING:
attr_type = 'X';
break;
default:
return NULL;
}
tmp_ctx = talloc_new(mem_ctx);
if (tmp_ctx == NULL) {
return NULL;
}
ctx.mem_ctx = tmp_ctx;
ok = claim_v1_to_ace_composite_unchecked(tmp_ctx, claim, &tok);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NULL;
}
/* this will construct the proper string in ctx.sddl */
ok = write_resource_attr_from_token(&ctx, &tok);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NULL;
}
/* escape the claim name */
ok = sddl_encode_attr_name(tmp_ctx,
claim->name,
&name, &name_len);
if (!ok) {
TALLOC_FREE(tmp_ctx);
return NULL;
}
s = talloc_asprintf(mem_ctx,
"(\"%s\",T%c,0x%x,%s)",
name,
attr_type,
claim->flags,
ctx.sddl);
TALLOC_FREE(tmp_ctx);
return s;
}
struct CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1 *parse_sddl_literal_as_claim(
TALLOC_CTX *mem_ctx,
const char *name,
const char *str)
{
/*
* For testing purposes (and possibly for client tools), we
* want to be able to create claim literals, and we might as
* well use the SDDL syntax. So we pretend to be parsing SDDL
* for one literal.
*/
bool ok;
struct CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1 *claim = NULL;
struct ace_condition_sddl_compiler_context comp = {};
ok = init_compiler_context(mem_ctx,
&comp,
ACE_CONDITION_FLAG_ALLOW_DEVICE,
str,
2,
2);
if (!ok) {
return NULL;
}
comp.state = SDDL_FLAG_EXPECTING_LITERAL;
ok = parse_literal(&comp, false);
if (!ok) {
goto error;
}
if (comp.program->length != 1) {
goto error;
}
ok = ace_token_to_claim_v1(mem_ctx,
name,
&comp.program->tokens[0],
&claim,
0);
if (!ok) {
goto error;
}
TALLOC_FREE(comp.program);
return claim;
error:
TALLOC_FREE(comp.program);
return NULL;
}
|