1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
|
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Copyright 2013 Nexenta Systems, Inc. All rights reserved.
#
include $(SRC)/lib/Makefile.lib
HDRS= libmlrpc.h ndr.h ndrtypes.ndl rpcpdu.ndl
HDRDIR= common
ROOTHDRDIR= $(ROOT)/usr/include/libmlrpc
ROOTHDRS= $(HDRS:%=$(ROOTHDRDIR)/%)
# ISA targets
# Hammerhead: amd64-only
SUBDIRS = $(MACH64)
all : TARGET = all
install : TARGET = install
clean : TARGET = clean
clobber : TARGET = clobber
.KEEP_STATE:
all install clean clobber: $(SUBDIRS)
install_h: $(ROOTHDRDIR) $(ROOTHDRS)
check: $(CHECKHDRS)
$(ROOTHDRDIR)/%: %
$(INS.file)
$(ROOTHDRDIR):
$(INS.dir)
$(SUBDIRS): FRC
@cd $@; pwd; VERSION='$(VERSION)' $(MAKE) $(TARGET)
FRC:
include $(SRC)/lib/Makefile.targ
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Copyright 2020 Tintri by DDN, Inc. All rights reserved.
#
LIBRARY = libmlrpc.a
VERS = .2
OBJS_COMMON = \
mlrpc_clh.o \
ndr_auth.o \
ndr_client.o \
ndr_heap.o \
ndr_marshal.o \
ndr_ops.o \
ndr_process.o \
ndr_server.o \
ndr_svc.o \
ndr_wchar.o
NDLLIST = rpcpdu
OBJECTS= $(OBJS_COMMON) $(NDLLIST:%=%_ndr.o)
CLEANFILES += $(NDLLIST:%=%_ndr.c)
include ../../Makefile.lib
LIBS= $(DYNLIB)
LDLIBS += -lsmbfs -luuid -lc
# Hammerhead: GNU ld needs rpath-link to resolve mech_krb5.so.1 (transitive dep of libsmbfs)
DYNFLAGS += -Wl,-rpath-link,$(ROOT)/usr/lib/gss
SRCDIR= ../common
SRCS= $(OBJS_COMMON:%.o=$(SRCDIR)/%.c)
NDLDIR = $(SRCDIR)
CFLAGS += $(CCVERBOSE)
INCS = -I. -I$(SRCDIR)
CPPFLAGS += $(INCS) -D_REENTRANT
all: $(LIBS)
include ../../Makefile.targ
objs/%_ndr.o: %_ndr.c
pics/%_ndr.o: %_ndr.c
%_ndr.c : $(NDLDIR)/%.ndl
$(NDRGEN) -Y $(ANSI_CPP) $(CPPFLAGS) $<
.KEEP_STATE:
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Hammerhead: 64-bit only - flatten smbsrv library path
MACH_LDLIBS += -L$(ROOT)/usr/lib/smbsrv
include ../Makefile.com
include ../../Makefile.lib.64
install: all $(ROOTLIBS64) $(ROOTLINKS64)
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2020 Tintri by DDN, Inc. All rights reserved.
* Copyright 2023 RackTop Systems, Inc.
*/
#ifndef _LIBMLRPC_H
#define _LIBMLRPC_H
#include <sys/types.h>
#include <sys/uio.h>
#include <smb/wintypes.h>
#include <libmlrpc/ndr.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* An MSRPC compatible implementation of OSF DCE RPC. DCE RPC is derived
* from the Apollo Network Computing Architecture (NCA) RPC implementation.
*
* CAE Specification (1997)
* DCE 1.1: Remote Procedure Call
* Document Number: C706
* The Open Group
* ogspecs@opengroup.org
*
* This implementation is based on the DCE Remote Procedure Call spec with
* enhancements to support Unicode strings. The diagram below shows the
* DCE RPC layers compared against ONC SUN RPC.
*
* NDR RPC Layers Sun RPC Layers Remark
* +---------------+ +---------------+ +---------------+
* +---------------+ +---------------+
* | Application | | Application | The application
* +---------------+ +---------------+
* | Hand coded | | RPCGEN gen'd | Where the real
* | client/server | | client/server | work happens
* | srvsvc.ndl | | *_svc.c *_clnt|
* | srvsvc.c | | |
* +---------------+ +---------------+
* | RPC Library | | RPC Library | Calls/Return
* | ndr_*.c | | | Binding/PMAP
* +---------------+ +---------------+
* | RPC Protocol | | RPC Protocol | Headers, Auth,
* | rpcpdu.ndl | | |
* +---------------+ +---------------+
* | IDL gen'd | | RPCGEN gen'd | Aggregate
* | NDR stubs | | XDR stubs | Composition
* | *__ndr.c | | *_xdr.c |
* +---------------+ +---------------+
* | NDR Represen | | XDR Represen | Byte order, padding
* +---------------+ +---------------+
* | Packet Heaps | | Network Conn | DCERPC does not talk
* | ndo_*.c | | clnt_{tcp,udp}| directly to network.
* +---------------+ +---------------+
*
* There are two major differences between the DCE RPC and ONC RPC:
*
* 1. NDR RPC only generates or processes packets from buffers. Other
* layers must take care of packet transmission and reception.
* The packet heaps are managed through a simple interface provided
* by the Network Data Representation (NDR) module called ndr_stream_t.
* ndo_*.c modules implement the different flavors (operations) of
* packet heaps.
*
* ONC RPC communicates directly with the network. You have to do
* something special for the RPC packet to be placed in a buffer
* rather than sent to the wire.
*
* 2. NDR RPC uses application provided heaps to support operations.
* A heap is a single, monolithic chunk of memory that NDR RPC manages
* as it allocates. When the operation and its result are done, the
* heap is disposed of as a single item. The transaction, which
* is the anchor of most operations, contains the necessary book-
* keeping for the heap.
*
* ONC RPC uses malloc() liberally throughout its run-time system.
* To free results, ONC RPC supports an XDR_FREE operation that
* traverses data structures freeing memory as it goes, whether
* it was malloc'd or not.
*/
/*
* Dispatch Return Code (DRC)
*
* 0x8000 15:01 Set to indicate a fault, clear indicates status
* 0x7F00 08:07 Status/Fault specific
* 0x00FF 00:08 PTYPE_... of PDU, 0xFF for header
*/
#define NDR_DRC_OK 0x0000
#define NDR_DRC_MASK_FAULT 0x8000
#define NDR_DRC_MASK_SPECIFIER 0xFF00
#define NDR_DRC_MASK_PTYPE 0x00FF
/* Fake PTYPE DRC discriminators */
#define NDR_DRC_PTYPE_RPCHDR(DRC) ((DRC) | 0x00FF)
#define NDR_DRC_PTYPE_API(DRC) ((DRC) | 0x00AA)
#define NDR_DRC_PTYPE_SEC(DRC) ((DRC) | 0x00CC)
/* DRC Recognizers */
#define NDR_DRC_IS_OK(DRC) (((DRC) & NDR_DRC_MASK_SPECIFIER) == 0)
#define NDR_DRC_IS_FAULT(DRC) (((DRC) & NDR_DRC_MASK_FAULT) != 0)
/*
* (Un)Marshalling category specifiers
*/
#define NDR_DRC_FAULT_MODE_MISMATCH 0x8100
#define NDR_DRC_RECEIVED 0x0200
#define NDR_DRC_FAULT_RECEIVED_RUNT 0x8300
#define NDR_DRC_FAULT_RECEIVED_MALFORMED 0x8400
#define NDR_DRC_DECODED 0x0500
#define NDR_DRC_FAULT_DECODE_FAILED 0x8600
#define NDR_DRC_ENCODED 0x0700
#define NDR_DRC_FAULT_ENCODE_FAILED 0x8800
#define NDR_DRC_FAULT_ENCODE_TOO_BIG 0x8900
#define NDR_DRC_SENT 0x0A00
#define NDR_DRC_FAULT_SEND_FAILED 0x8B00
/*
* Resource category specifier
*/
#define NDR_DRC_FAULT_RESOURCE_1 0x9100
#define NDR_DRC_FAULT_RESOURCE_2 0x9200
/*
* Parameters. Usually #define'd with useful alias
*/
#define NDR_DRC_FAULT_PARAM_0_INVALID 0xC000
#define NDR_DRC_FAULT_PARAM_0_UNIMPLEMENTED 0xD000
#define NDR_DRC_FAULT_PARAM_1_INVALID 0xC100
#define NDR_DRC_FAULT_PARAM_1_UNIMPLEMENTED 0xD100
#define NDR_DRC_FAULT_PARAM_2_INVALID 0xC200
#define NDR_DRC_FAULT_PARAM_2_UNIMPLEMENTED 0xD200
#define NDR_DRC_FAULT_PARAM_3_INVALID 0xC300
#define NDR_DRC_FAULT_PARAM_3_UNIMPLEMENTED 0xD300
#define NDR_DRC_FAULT_PARAM_4_INVALID 0xC400
#define NDR_DRC_FAULT_PARAM_4_UNIMPLEMENTED 0xD400
#define NDR_DRC_FAULT_PARAM_5_INVALID 0xC500
#define NDR_DRC_FAULT_PARAM_5_UNIMPLEMENTED 0xD500
#define NDR_DRC_FAULT_OUT_OF_MEMORY 0xF000
/* RPCHDR */
#define NDR_DRC_FAULT_RPCHDR_MODE_MISMATCH 0x81FF
#define NDR_DRC_FAULT_RPCHDR_RECEIVED_RUNT 0x83FF
#define NDR_DRC_FAULT_RPCHDR_DECODE_FAILED 0x86FF
#define NDR_DRC_FAULT_RPCHDR_PTYPE_INVALID 0xC0FF /* PARAM_0_INVALID */
#define NDR_DRC_FAULT_RPCHDR_PTYPE_UNIMPLEMENTED 0xD0FF /* PARAM_0_UNIMP */
/* Request */
#define NDR_DRC_FAULT_REQUEST_PCONT_INVALID 0xC000 /* PARAM_0_INVALID */
#define NDR_DRC_FAULT_REQUEST_OPNUM_INVALID 0xC100 /* PARAM_1_INVALID */
/* Bind */
#define NDR_DRC_BINDING_MADE 0x000B /* OK */
#define NDR_DRC_FAULT_BIND_PCONT_BUSY 0xC00B /* PARAM_0_INVALID */
#define NDR_DRC_FAULT_BIND_UNKNOWN_SERVICE 0xC10B /* PARAM_1_INVALID */
#define NDR_DRC_FAULT_BIND_NO_SLOTS 0x910B /* RESOURCE_1 */
/* API */
#define NDR_DRC_FAULT_API_SERVICE_INVALID 0xC0AA /* PARAM_0_INVALID */
#define NDR_DRC_FAULT_API_BIND_NO_SLOTS 0x91AA /* RESOURCE_1 */
#define NDR_DRC_FAULT_API_OPNUM_INVALID 0xC1AA /* PARAM_1_INVALID */
/* Secure RPC and SSPs */
#define NDR_DRC_FAULT_SEC_TYPE_UNIMPLEMENTED \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_PARAM_0_UNIMPLEMENTED)
#define NDR_DRC_FAULT_SEC_LEVEL_UNIMPLEMENTED \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_PARAM_1_UNIMPLEMENTED)
#define NDR_DRC_FAULT_SEC_SSP_FAILED \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_RESOURCE_1)
#define NDR_DRC_FAULT_SEC_ENCODE_TOO_BIG \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_ENCODE_TOO_BIG)
#define NDR_DRC_FAULT_SEC_AUTH_LENGTH_INVALID \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_PARAM_2_INVALID)
#define NDR_DRC_FAULT_SEC_AUTH_TYPE_INVALID \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_PARAM_0_INVALID)
#define NDR_DRC_FAULT_SEC_AUTH_LEVEL_INVALID \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_PARAM_1_INVALID)
#define NDR_DRC_FAULT_SEC_OUT_OF_MEMORY \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_OUT_OF_MEMORY)
#define NDR_DRC_FAULT_SEC_ENCODE_FAILED \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_ENCODE_FAILED)
#define NDR_DRC_FAULT_SEC_META_INVALID \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_PARAM_3_INVALID)
#define NDR_DRC_FAULT_SEC_SEQNUM_INVALID \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_PARAM_4_INVALID)
#define NDR_DRC_FAULT_SEC_SIG_INVALID \
NDR_DRC_PTYPE_SEC(NDR_DRC_FAULT_PARAM_5_INVALID)
struct ndr_xa;
struct ndr_client;
typedef struct ndr_stub_table {
int (*func)(void *, struct ndr_xa *);
unsigned short opnum;
} ndr_stub_table_t;
typedef struct ndr_service {
char *name;
char *desc;
char *endpoint;
char *sec_addr_port;
char *abstract_syntax_uuid;
int abstract_syntax_version;
char *transfer_syntax_uuid;
int transfer_syntax_version;
unsigned bind_instance_size;
int (*bind_req)();
int (*unbind_and_close)();
int (*call_stub)(struct ndr_xa *);
ndr_typeinfo_t *interface_ti;
ndr_stub_table_t *stub_table;
} ndr_service_t;
/*
* The list of bindings is anchored at a connection. Nothing in the
* RPC mechanism allocates them. Binding elements which have service==0
* indicate free elements. When a connection is instantiated, at least
* one free binding entry should also be established. Something like
* this should suffice for most (all) situations:
*
* struct connection {
* ....
* ndr_binding_t *binding_list_head;
* ndr_binding_t binding_pool[N_BINDING_POOL];
* ....
* };
*
* init_connection(struct connection *conn) {
* ....
* ndr_svc_binding_pool_init(&conn->binding_list_head,
* conn->binding_pool, N_BINDING_POOL);
*/
typedef struct ndr_binding {
struct ndr_binding *next;
ndr_p_context_id_t p_cont_id;
unsigned char which_side;
struct ndr_client *clnt;
ndr_service_t *service;
void *instance_specific;
} ndr_binding_t;
#define NDR_BIND_SIDE_CLIENT 1
#define NDR_BIND_SIDE_SERVER 2
#define NDR_BINDING_TO_SPECIFIC(BINDING, TYPE) \
((TYPE *) (BINDING)->instance_specific)
/*
* The binding list space must be provided by the application library
* for use by the underlying RPC library. We need at least two binding
* slots per connection.
*/
#define NDR_N_BINDING_POOL 2
typedef struct ndr_pipe {
void *np_listener;
const char *np_endpoint;
struct smb_netuserinfo *np_user;
int (*np_send)(struct ndr_pipe *, void *, size_t);
int (*np_recv)(struct ndr_pipe *, void *, size_t);
int np_fid;
uint16_t np_max_xmit_frag;
uint16_t np_max_recv_frag;
ndr_binding_t *np_binding;
ndr_binding_t np_binding_pool[NDR_N_BINDING_POOL];
} ndr_pipe_t;
/*
* Number of bytes required to align SIZE on the next dword/4-byte
* boundary.
*/
#define NDR_ALIGN4(SIZE) ((4 - (SIZE)) & 3);
/*
* DCE RPC strings (CAE section 14.3.4) are represented as varying or varying
* and conformant one-dimensional arrays. Characters can be single-byte
* or multi-byte as long as all characters conform to a fixed element size,
* i.e. UCS-2 is okay but UTF-8 is not a valid DCE RPC string format. The
* string is terminated by a null character of the appropriate element size.
*
* MSRPC strings should always be varying/conformant and not null terminated.
* This format uses the size_is, first_is and length_is attributes (CAE
* section 4.2.18).
*
* typedef struct string {
* DWORD size_is;
* DWORD first_is;
* DWORD length_is;
* wchar_t string[ANY_SIZE_ARRAY];
* } string_t;
*
* The size_is attribute is used to specify the number of data elements in
* each dimension of an array.
*
* The first_is attribute is used to define the lower bound for significant
* elements in each dimension of an array. For strings this is always 0.
*
* The length_is attribute is used to define the number of significant
* elements in each dimension of an array. For strings this is typically
* the same as size_is. Although it might be (size_is - 1) if the string
* is null terminated.
*
* 4 bytes 4 bytes 4 bytes 2bytes 2bytes 2bytes 2bytes
* +---------+---------+---------+------+------+------+------+
* |size_is |first_is |length_is| char | char | char | char |
* +---------+---------+---------+------+------+------+------+
*
* Unfortunately, not all MSRPC Unicode strings are null terminated, which
* means that the recipient has to manually null-terminate the string after
* it has been unmarshalled. There may be a wide-char pad following a
* string, and it may sometimes contains zero, but it's not guaranteed.
*
* To deal with this, MSRPC sometimes uses an additional wrapper with two
* more fields, as shown below.
* length: the array length in bytes excluding terminating null bytes
* maxlen: the array length in bytes including null terminator bytes
* LPTSTR: converted to a string_t by NDR
*
* typedef struct ms_string {
* WORD length;
* WORD maxlen;
* LPTSTR str;
* } ms_string_t;
*/
typedef struct ndr_mstring {
uint16_t length;
uint16_t allosize;
LPTSTR str;
} ndr_mstring_t;
/*
* A number of heap areas are used during marshalling and unmarshalling.
* Under some circumstances these areas can be discarded by the library
* code, i.e. on the server side before returning to the client and on
* completion of a client side bind. In the case of a client side RPC
* call, these areas must be preserved after an RPC returns to give the
* caller time to take a copy of the data. In this case the client must
* call ndr_clnt_free_heap to free the memory.
*
* The heap management data definition looks a bit like this:
*
* heap -> +---------------+ +------------+
* | iovec[0].base | --> | data block |
* | iovec[0].len | +------------+
* +---------------+
* ::
* ::
* iov -> +---------------+ +------------+
* | iovec[n].base | --> | data block |
* | iovec[n].len | +------------+
* +---------------+ ^ ^
* | |
* next ----------------------+ |
* top -----------------------------------+
*
*/
/*
* Setting MAXIOV to 384 will use ((8 * 384) + 16) = 3088 bytes
* of the first heap block.
*/
#define NDR_HEAP_MAXIOV 384
#define NDR_HEAP_BLKSZ 8192
typedef struct ndr_heap {
struct iovec iovec[NDR_HEAP_MAXIOV];
struct iovec *iov;
int iovcnt;
char *top;
char *next;
} ndr_heap_t;
/*
* Alternate varying/conformant string definition
* - for non-null-terminated strings.
*/
typedef struct ndr_vcs {
/*
* size_is (actually a copy of length_is) will
* be inserted here by the marshalling library.
*/
uint32_t vc_first_is;
uint32_t vc_length_is;
uint16_t buffer[ANY_SIZE_ARRAY];
} ndr_vcs_t;
typedef struct ndr_vcstr {
uint16_t wclen;
uint16_t wcsize;
ndr_vcs_t *vcs;
} ndr_vcstr_t;
typedef struct ndr_vcb {
/*
* size_is (actually a copy of length_is) will
* be inserted here by the marshalling library.
*/
uint32_t vc_first_is;
uint32_t vc_length_is;
uint8_t buffer[ANY_SIZE_ARRAY];
} ndr_vcb_t;
typedef struct ndr_vcbuf {
uint16_t len;
uint16_t size;
ndr_vcb_t *vcb;
} ndr_vcbuf_t;
ndr_heap_t *ndr_heap_create(void);
void ndr_heap_destroy(ndr_heap_t *);
void *ndr_heap_dupmem(ndr_heap_t *, const void *, size_t);
void *ndr_heap_malloc(ndr_heap_t *, unsigned);
void *ndr_heap_strdup(ndr_heap_t *, const char *);
int ndr_heap_mstring(ndr_heap_t *, const char *, ndr_mstring_t *);
void ndr_heap_mkvcs(ndr_heap_t *, char *, ndr_vcstr_t *);
void ndr_heap_mkvcb(ndr_heap_t *, uint8_t *, uint32_t, ndr_vcbuf_t *);
int ndr_heap_used(ndr_heap_t *);
int ndr_heap_avail(ndr_heap_t *);
#define NDR_MALLOC(XA, SZ) ndr_heap_malloc((XA)->heap, SZ)
#define NDR_NEW(XA, T) ndr_heap_malloc((XA)->heap, sizeof (T))
#define NDR_NEWN(XA, T, N) ndr_heap_malloc((XA)->heap, sizeof (T)*(N))
#define NDR_STRDUP(XA, S) ndr_heap_strdup((XA)->heap, (S))
#define NDR_MSTRING(XA, S, OUT) ndr_heap_mstring((XA)->heap, (S), (OUT))
#define NDR_SIDDUP(XA, S) ndr_heap_dupmem((XA)->heap, (S), smb_sid_len(S))
typedef struct ndr_xa {
unsigned short ptype; /* high bits special */
unsigned short opnum;
ndr_stream_t recv_nds;
ndr_hdr_t recv_hdr;
ndr_sec_t recv_auth;
ndr_stream_t send_nds;
ndr_hdr_t send_hdr;
ndr_sec_t send_auth;
ndr_binding_t *binding; /* what we're using */
ndr_binding_t *binding_list; /* from connection */
ndr_heap_t *heap;
ndr_pipe_t *pipe;
} ndr_xa_t;
typedef struct ndr_auth_ops {
int (*nao_init)(void *, ndr_xa_t *);
int (*nao_recv)(void *, ndr_xa_t *);
int (*nao_sign)(void *, ndr_xa_t *);
int (*nao_verify)(void *, ndr_xa_t *, boolean_t);
int (*nao_encrypt)(void *, ndr_xa_t *);
int (*nao_decrypt)(void *, ndr_xa_t *, boolean_t);
} ndr_auth_ops_t;
/*
* A client provides this structure during bind to indicate
* that the RPC runtime should use "Secure RPC" (RPC-level auth).
*
* Currently, only NETLOGON uses this, and only NETLOGON-based
* Integrity protection is supported.
*/
typedef struct ndr_auth_ctx {
ndr_auth_ops_t auth_ops;
void *auth_ctx; /* SSP-specific context */
uint32_t auth_context_id;
uint8_t auth_type;
uint8_t auth_level;
boolean_t auth_verify_resp;
} ndr_auth_ctx_t;
/*
* 20-byte opaque id used by various RPC services.
*/
CONTEXT_HANDLE(ndr_hdid) ndr_hdid_t;
typedef struct ndr_client {
/* transport stuff (xa_* members) */
int (*xa_init)(struct ndr_client *, ndr_xa_t *);
int (*xa_exchange)(struct ndr_client *, ndr_xa_t *);
int (*xa_read)(struct ndr_client *, ndr_xa_t *);
void (*xa_preserve)(struct ndr_client *, ndr_xa_t *);
void (*xa_destruct)(struct ndr_client *, ndr_xa_t *);
void (*xa_release)(struct ndr_client *);
void *xa_private;
int xa_fd;
ndr_hdid_t *handle;
ndr_binding_t *binding;
ndr_binding_t *binding_list;
ndr_binding_t binding_pool[NDR_N_BINDING_POOL];
boolean_t nonull;
boolean_t heap_preserved;
ndr_heap_t *heap;
ndr_stream_t *recv_nds;
ndr_stream_t *send_nds;
uint32_t next_call_id;
unsigned next_p_cont_id;
ndr_auth_ctx_t auth_ctx;
} ndr_client_t;
typedef struct ndr_handle {
ndr_hdid_t nh_id;
struct ndr_handle *nh_next;
ndr_pipe_t *nh_pipe;
const ndr_service_t *nh_svc;
ndr_client_t *nh_clnt;
void *nh_data;
void (*nh_data_free)(void *);
} ndr_handle_t;
#define NDR_PDU_SIZE_HINT_DEFAULT (16*1024)
#define NDR_BUF_MAGIC 0x4E425546 /* NBUF */
typedef struct ndr_buf {
uint32_t nb_magic;
ndr_stream_t nb_nds;
ndr_heap_t *nb_heap;
ndr_typeinfo_t *nb_ti;
} ndr_buf_t;
/* ndr_ops.c */
int nds_initialize(ndr_stream_t *, unsigned, int, ndr_heap_t *);
void nds_destruct(ndr_stream_t *);
void nds_show_state(ndr_stream_t *);
/* ndr_client.c */
int ndr_clnt_bind(ndr_client_t *, ndr_service_t *, ndr_binding_t **);
int ndr_clnt_call(ndr_binding_t *, int, void *);
void ndr_clnt_free_heap(ndr_client_t *);
/* ndr_marshal.c */
ndr_buf_t *ndr_buf_init(ndr_typeinfo_t *);
void ndr_buf_fini(ndr_buf_t *);
int ndr_buf_decode(ndr_buf_t *, unsigned, unsigned, const char *data, size_t,
void *);
int ndr_decode_call(ndr_xa_t *, void *);
int ndr_encode_return(ndr_xa_t *, void *);
int ndr_encode_call(ndr_xa_t *, void *);
int ndr_decode_return(ndr_xa_t *, void *);
int ndr_decode_pdu_hdr(ndr_xa_t *);
int ndr_encode_pdu_hdr(ndr_xa_t *);
void ndr_decode_frag_hdr(ndr_stream_t *, ndr_common_header_t *);
void ndr_remove_frag_hdr(ndr_stream_t *);
void ndr_show_hdr(ndr_common_header_t *);
unsigned ndr_bind_ack_hdr_size(ndr_xa_t *);
unsigned ndr_alter_context_rsp_hdr_size(void);
int ndr_decode_pdu_auth(ndr_xa_t *);
int ndr_encode_pdu_auth(ndr_xa_t *);
void ndr_show_auth(ndr_sec_t *);
/*
* MS-RPCE "Secure RPC" (RPC-level auth).
* These call the functions in ndr_auth_ops_t, which should be
* GSSAPI (or equivalent) calls.
*/
int ndr_add_sec_context(ndr_auth_ctx_t *, ndr_xa_t *);
int ndr_recv_sec_context(ndr_auth_ctx_t *, ndr_xa_t *);
int ndr_add_auth(ndr_auth_ctx_t *, ndr_xa_t *);
int ndr_check_auth(ndr_auth_ctx_t *, ndr_xa_t *);
/* ndr_server.c */
void ndr_pipe_worker(ndr_pipe_t *);
int ndr_generic_call_stub(ndr_xa_t *);
/* ndr_svc.c */
ndr_stub_table_t *ndr_svc_find_stub(ndr_service_t *, int);
ndr_service_t *ndr_svc_lookup_name(const char *);
ndr_service_t *ndr_svc_lookup_uuid(ndr_uuid_t *, int, ndr_uuid_t *, int);
int ndr_svc_register(ndr_service_t *);
void ndr_svc_unregister(ndr_service_t *);
void ndr_svc_binding_pool_init(ndr_binding_t **, ndr_binding_t pool[], int);
ndr_binding_t *ndr_svc_find_binding(ndr_xa_t *, ndr_p_context_id_t);
ndr_binding_t *ndr_svc_new_binding(ndr_xa_t *);
int ndr_uuid_parse(char *, ndr_uuid_t *);
void ndr_uuid_unparse(ndr_uuid_t *, char *);
ndr_hdid_t *ndr_hdalloc(const ndr_xa_t *, const void *);
void ndr_hdfree(const ndr_xa_t *, const ndr_hdid_t *);
ndr_handle_t *ndr_hdlookup(const ndr_xa_t *, const ndr_hdid_t *);
void ndr_hdclose(ndr_pipe_t *);
ssize_t ndr_uiomove(caddr_t, size_t, enum uio_rw, struct uio *);
/*
* An ndr_client_t is created while binding a client connection to hold
* the context for calls made using that connection.
*
* Handles are RPC call specific and we use an inheritance mechanism to
* ensure that each handle has a pointer to the client_t. When the top
* level (bind) handle is released, we close the connection.
*
* There are some places in libmlsvc where the code assumes that the
* handle member is first in this struct. Careful!
*
* Note that this entire structure is bzero()'d once the ndr_client_t
* has been created.
*/
typedef struct mlrpc_handle {
ndr_hdid_t handle; /* keep first */
ndr_client_t *clnt;
} mlrpc_handle_t;
int mlrpc_clh_create(mlrpc_handle_t *, void *);
uint32_t mlrpc_clh_set_auth(mlrpc_handle_t *, ndr_auth_ctx_t *);
uint32_t mlrpc_clh_bind(mlrpc_handle_t *, ndr_service_t *);
void mlrpc_clh_unbind(mlrpc_handle_t *);
void *mlrpc_clh_free(mlrpc_handle_t *);
int ndr_rpc_call(mlrpc_handle_t *, int, void *);
int ndr_rpc_get_ssnkey(mlrpc_handle_t *, unsigned char *, size_t);
void *ndr_rpc_malloc(mlrpc_handle_t *, size_t);
ndr_heap_t *ndr_rpc_get_heap(mlrpc_handle_t *);
void ndr_rpc_release(mlrpc_handle_t *);
void ndr_rpc_set_nonull(mlrpc_handle_t *);
boolean_t ndr_is_null_handle(mlrpc_handle_t *);
boolean_t ndr_is_bind_handle(mlrpc_handle_t *);
void ndr_inherit_handle(mlrpc_handle_t *, mlrpc_handle_t *);
#ifdef __cplusplus
}
#endif
#endif /* _LIBMLRPC_H */
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
# Copyright 2020 Tintri by DDN, Inc. All rights reserved.
#
#
#
# MAPFILE HEADER START
#
# WARNING: STOP NOW. DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
# usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#
$mapfile_version 2
SYMBOL_VERSION SUNWprivate {
global:
mlrpc_clh_bind;
mlrpc_clh_create;
mlrpc_clh_free;
mlrpc_clh_set_auth;
mlrpc_clh_unbind;
# Allow debug/test programs to provide these.
ndo_printf { FLAGS = NODIRECT; };
ndo_trace { FLAGS = NODIRECT; };
ndr_buf_decode;
ndr_buf_fini;
ndr_buf_init;
ndr_clnt_bind;
ndr_clnt_call;
ndr_clnt_free_heap;
ndr_generic_call_stub;
ndr_heap_avail;
ndr_heap_create;
ndr_heap_destroy;
ndr_heap_dupmem;
ndr_heap_malloc;
ndr_heap_mkvcb;
ndr_heap_mkvcs;
ndr_heap_mstring;
ndr_heap_strdup;
ndr_heap_used;
ndr_hdalloc;
ndr_hdclose;
ndr_hdfree;
ndr_hdlookup;
ndr_inherit_handle;
ndr_inner;
ndr_is_bind_handle;
ndr_is_null_handle;
ndr_mbstowcs;
ndr_params;
ndr_pipe_worker;
ndr_rpc_call;
ndr_rpc_get_heap;
ndr_rpc_get_ssnkey;
ndr_rpc_malloc;
ndr_rpc_release;
ndr_rpc_set_nonull;
ndr_svc_binding_pool_init;
ndr_svc_lookup_name;
ndr_svc_register;
ndr_topmost;
ndr_uuid_parse;
ndr_uuid_unparse;
nds_destruct;
nds_initialize;
$if _ELF32
ndt__char { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt_s_char { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt__uchar { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt_s_uchar { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt__wchar { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt_s_wchar { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt__short { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt_s_short { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt__ushort { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt_s_ushort { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt__long { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt_s_long { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt__ulong { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
ndt_s_ulong { ASSERT = { TYPE = OBJECT; SIZE = 16; }; };
$elif _ELF64
ndt__char { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt_s_char { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt__uchar { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt_s_uchar { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt__wchar { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt_s_wchar { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt__short { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt_s_short { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt__ushort { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt_s_ushort { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt__long { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt_s_long { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt__ulong { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
ndt_s_ulong { ASSERT = { TYPE = OBJECT; SIZE = 24; }; };
$else
$error unknown ELFCLASS
$endif
local:
*;
};
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2020 Tintri by DDN, Inc. All rights reserved.
*/
/*
* ML-RPC Client handle interface and support functions.
*/
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/poll.h>
#include <errno.h>
#include <strings.h>
#include <unistd.h>
#include <netsmb/smbfs_api.h>
#include <smb/ntstatus.h>
#include <libmlrpc.h>
#include <assert.h>
static int ndr_xa_init(ndr_client_t *, ndr_xa_t *);
static int ndr_xa_exchange(ndr_client_t *, ndr_xa_t *);
static int ndr_xa_read(ndr_client_t *, ndr_xa_t *);
static void ndr_xa_preserve(ndr_client_t *, ndr_xa_t *);
static void ndr_xa_destruct(ndr_client_t *, ndr_xa_t *);
static void ndr_xa_release(ndr_client_t *);
/* See notes in mlrpc_clh_bind */
int rpc_pipe_open_retries = 10;
/*
* Create an RPC client binding handle using the given smb_ctx.
* That context must already have a session and tree connected.
*
* Returns zero or an errno value.
*/
int
mlrpc_clh_create(mlrpc_handle_t *handle, void *ctx)
{
ndr_client_t *clnt = NULL;
if (ctx == NULL)
return (EINVAL);
/*
* Allocate...
*/
if ((clnt = calloc(1, sizeof (*clnt))) == NULL)
return (ENOMEM);
clnt->xa_fd = -1;
/*
* Setup the transport functions.
* Always a named pipe (for now).
*/
clnt->xa_private = ctx;
clnt->xa_init = ndr_xa_init;
clnt->xa_exchange = ndr_xa_exchange;
clnt->xa_read = ndr_xa_read;
clnt->xa_preserve = ndr_xa_preserve;
clnt->xa_destruct = ndr_xa_destruct;
clnt->xa_release = ndr_xa_release;
/* See _is_bind_handle */
clnt->handle = &handle->handle;
ndr_svc_binding_pool_init(&clnt->binding_list,
clnt->binding_pool, NDR_N_BINDING_POOL);
if ((clnt->heap = ndr_heap_create()) == NULL)
goto nomem;
/* success! */
bzero(handle, sizeof (*handle));
handle->clnt = clnt;
return (0);
nomem:
free(clnt);
return (ENOMEM);
}
/*
* Set up this handle to perform RPC-level authentication.
*/
uint32_t
mlrpc_clh_set_auth(mlrpc_handle_t *handle, ndr_auth_ctx_t *auth_ctx)
{
ndr_client_t *clnt = NULL;
if ((clnt = handle->clnt) == NULL)
return (NT_STATUS_INTERNAL_ERROR);
if (auth_ctx != NULL) {
/* struct copy */
clnt->auth_ctx = *auth_ctx;
}
return (NT_STATUS_SUCCESS);
}
/*
* This call must be made to initialize an RPC client structure and bind
* to the remote service before any RPCs can be exchanged with that service.
*
* The mlrpc_handle_t is a wrapper that is used to associate an RPC handle
* with the client context for an instance of the interface. The handle
* is zeroed to ensure that it doesn't look like a valid handle -
* handle content is provided by the remove service.
*
* The client points to this top-level handle so that we know when to
* unbind and teardown the connection. As each handle is initialized it
* will inherit a reference to the client context.
*
*
* Similar to MSRPC RpcBindingBind()
*
* Returns 0 or an NT_STATUS: (failed in...)
*
* RPC_NT_SERVER_TOO_BUSY (open pipe)
* RPC_NT_SERVER_UNAVAILABLE (open pipe)
* NT_STATUS_ACCESS_DENIED (open pipe)
* NT_STATUS_INVALID_PARAMETER (rpc bind)
* NT_STATUS_INTERNAL_ERROR (bad args etc)
* NT_STATUS_NO_MEMORY
*/
uint32_t
mlrpc_clh_bind(mlrpc_handle_t *handle, ndr_service_t *svc)
{
ndr_client_t *clnt = NULL;
struct smb_ctx *ctx = NULL;
uint32_t status = 0;
int fd = -1;
int rc, retries;
if ((clnt = handle->clnt) == NULL)
return (NT_STATUS_INTERNAL_ERROR);
if ((ctx = clnt->xa_private) == NULL)
return (NT_STATUS_INTERNAL_ERROR);
if (clnt->xa_fd != -1)
return (NT_STATUS_INTERNAL_ERROR);
/*
* Open the named pipe.
*
* Sometimes a DC may return NT_STATUS_PIPE_NOT_AVAILABLE for
* the first few seconds during service auto-start. The client
* translates that to EBUSY, so when we see that, wait a bit
* and retry the open for up to rpc_pipe_open_retries. If we
* fail even after retries, return RPC_NT_SERVER_TOO_BUSY,
* which is how callers of this layer expect that reported.
* We try up to 10 times, with a 0.5 sec. wait after each
* BUSY failure, giving a total wait here of 5 sec.
*/
retries = rpc_pipe_open_retries;
retry_open:
fd = smb_fh_open(ctx, svc->endpoint, O_RDWR);
if (fd < 0) {
rc = errno;
switch (rc) {
case EBUSY:
if (--retries > 0) {
(void) poll(NULL, 0, 500);
goto retry_open;
}
status = RPC_NT_SERVER_TOO_BUSY;
break;
case EACCES:
status = NT_STATUS_ACCESS_DENIED;
break;
default:
status = RPC_NT_SERVER_UNAVAILABLE;
break;
}
return (status);
}
clnt->xa_fd = fd;
/* Paranoia, in case of re-bind. */
bzero(&handle->handle, sizeof (ndr_hdid_t));
/*
* Do the OtW RPC bind.
*/
rc = ndr_clnt_bind(clnt, svc, &clnt->binding);
switch (rc) {
case NDR_DRC_FAULT_OUT_OF_MEMORY:
status = NT_STATUS_NO_MEMORY;
break;
case NDR_DRC_FAULT_API_SERVICE_INVALID:
/* svc->..._uuid parse errors */
status = NT_STATUS_INTERNAL_ERROR;
break;
default:
if (NDR_DRC_IS_FAULT(rc)) {
status = RPC_NT_PROTOCOL_ERROR;
break;
}
/* FALLTHROUGH */
case NDR_DRC_OK:
status = NT_STATUS_SUCCESS;
}
if (status != 0) {
if (fd != -1)
(void) smb_fh_close(fd);
clnt->xa_fd = -1;
}
return (status);
}
/*
* Unbind and close the pipe to an RPC service.
*
* Similar to MSRPC RpcBindingUnbind()
* This should be called after a dropped connection.
*/
void
mlrpc_clh_unbind(mlrpc_handle_t *handle)
{
ndr_client_t *clnt = handle->clnt;
if (clnt->xa_fd != -1) {
(void) smb_fh_close(clnt->xa_fd);
clnt->xa_fd = -1;
}
}
/*
* If the heap has been preserved we need to go through an xa release.
* The heap is preserved during an RPC call because that's where data
* returned from the server is stored.
*
* Otherwise we destroy the heap directly.
*
* Returns the xa_private pointer (if non-NULL) to inform the caller
* that it can now be destroyed.
*/
void *
mlrpc_clh_free(mlrpc_handle_t *handle)
{
ndr_client_t *clnt = handle->clnt;
void *private;
if (clnt == NULL)
return (NULL);
/*
* Should never get an unbind on inherited handles.
* Callers of ndr_inherit_handle() check handles
* with ndr_is_bind_handle() before calling this.
*
* Maybe make this function more tolerant?
*/
assert(handle->clnt->handle == &handle->handle);
mlrpc_clh_unbind(handle);
if (clnt->heap_preserved)
ndr_clnt_free_heap(clnt); /* xa_release */
else
ndr_heap_destroy(clnt->heap);
/*
* Note: Caller will free the smb_ctx stored in
* clnt->xa_private (or possibly reuse it).
*/
private = clnt->xa_private;
free(clnt);
bzero(handle, sizeof (*handle));
return (private);
}
/*
* Call the RPC function identified by opnum. The remote service is
* identified by the handle, which should have been initialized by
* ndr_rpc_bind.
*
* If the RPC call is successful (returns 0), the caller must call
* ndr_rpc_release to release the heap. Otherwise, we release the
* heap here.
*/
int
ndr_rpc_call(mlrpc_handle_t *handle, int opnum, void *params)
{
ndr_client_t *clnt = handle->clnt;
int rc;
if (ndr_rpc_get_heap(handle) == NULL)
return (-1);
rc = ndr_clnt_call(clnt->binding, opnum, params);
/*
* Always clear the nonull flag to ensure
* it is not applied to subsequent calls.
*/
clnt->nonull = B_FALSE;
if (NDR_DRC_IS_FAULT(rc)) {
ndr_rpc_release(handle);
return (-1);
}
return (0);
}
/*
* Outgoing strings should not be null terminated.
*/
void
ndr_rpc_set_nonull(mlrpc_handle_t *handle)
{
handle->clnt->nonull = B_TRUE;
}
/*
* Get the session key from a bound RPC client handle.
*
* The key returned is the 16-byte "user session key"
* established by the underlying authentication protocol
* (either Kerberos or NTLM). This key is needed for
* SAM RPC calls such as SamrSetInformationUser, etc.
* See [MS-SAMR] sections: 2.2.3.3, 2.2.7.21, 2.2.7.25.
*
* Returns zero (success) or an errno.
*/
int
ndr_rpc_get_ssnkey(mlrpc_handle_t *handle, uchar_t *key, size_t len)
{
ndr_client_t *clnt = handle->clnt;
if (clnt == NULL || clnt->xa_fd == -1)
return (EINVAL);
return (smb_fh_getssnkey(clnt->xa_fd, key, len));
}
void *
ndr_rpc_malloc(mlrpc_handle_t *handle, size_t size)
{
ndr_heap_t *heap;
if ((heap = ndr_rpc_get_heap(handle)) == NULL)
return (NULL);
return (ndr_heap_malloc(heap, size));
}
ndr_heap_t *
ndr_rpc_get_heap(mlrpc_handle_t *handle)
{
ndr_client_t *clnt = handle->clnt;
if (clnt->heap == NULL)
clnt->heap = ndr_heap_create();
return (clnt->heap);
}
/*
* Must be called by RPC clients to free the heap after a successful RPC
* call, i.e. ndr_rpc_call returned 0. The caller should take a copy
* of any data returned by the RPC prior to calling this function because
* returned data is in the heap.
*/
void
ndr_rpc_release(mlrpc_handle_t *handle)
{
ndr_client_t *clnt = handle->clnt;
if (clnt->heap_preserved)
ndr_clnt_free_heap(clnt);
else
ndr_heap_destroy(clnt->heap);
clnt->heap = NULL;
}
/*
* Returns true if the handle is null.
* Otherwise returns false.
*/
boolean_t
ndr_is_null_handle(mlrpc_handle_t *handle)
{
static const ndr_hdid_t hdid0 = {0};
if (handle == NULL || handle->clnt == NULL)
return (B_TRUE);
if (!memcmp(&handle->handle, &hdid0, sizeof (hdid0)))
return (B_TRUE);
return (B_FALSE);
}
/*
* Returns true if the handle is the top level bind handle.
* Otherwise returns false.
*/
boolean_t
ndr_is_bind_handle(mlrpc_handle_t *handle)
{
return (handle->clnt->handle == &handle->handle);
}
/*
* Pass the client reference from parent to child.
*/
void
ndr_inherit_handle(mlrpc_handle_t *child, mlrpc_handle_t *parent)
{
child->clnt = parent->clnt;
}
/*
* ndr_rpc_status remains in libmlsvc mlsvc_client.c
*/
/*
* The following functions provide the client callback interface.
* If the caller hasn't provided a heap, create one here.
*/
static int
ndr_xa_init(ndr_client_t *clnt, ndr_xa_t *mxa)
{
ndr_stream_t *recv_nds = &mxa->recv_nds;
ndr_stream_t *send_nds = &mxa->send_nds;
ndr_heap_t *heap = clnt->heap;
int rc;
if (heap == NULL) {
if ((heap = ndr_heap_create()) == NULL)
return (-1);
clnt->heap = heap;
}
mxa->heap = heap;
rc = nds_initialize(send_nds, 0, NDR_MODE_CALL_SEND, heap);
if (rc == 0)
rc = nds_initialize(recv_nds, NDR_PDU_SIZE_HINT_DEFAULT,
NDR_MODE_RETURN_RECV, heap);
if (rc != 0) {
nds_destruct(&mxa->recv_nds);
nds_destruct(&mxa->send_nds);
ndr_heap_destroy(mxa->heap);
mxa->heap = NULL;
clnt->heap = NULL;
return (-1);
}
if (clnt->nonull)
NDS_SETF(send_nds, NDS_F_NONULL);
return (0);
}
/*
* This is the entry pointy for an RPC client call exchange with
* a server, which will result in an smbrdr SmbTransact request.
*
* SmbTransact should return the number of bytes received, which
* we record as the PDU size, or a negative error code.
*/
static int
ndr_xa_exchange(ndr_client_t *clnt, ndr_xa_t *mxa)
{
ndr_stream_t *recv_nds = &mxa->recv_nds;
ndr_stream_t *send_nds = &mxa->send_nds;
int err, more, nbytes;
nbytes = recv_nds->pdu_max_size;
err = smb_fh_xactnp(clnt->xa_fd,
send_nds->pdu_size, (char *)send_nds->pdu_base_offset,
&nbytes, (char *)recv_nds->pdu_base_offset, &more);
if (err) {
recv_nds->pdu_size = 0;
return (-1);
}
recv_nds->pdu_size = nbytes;
return (0);
}
/*
* This entry point will be invoked if the xa-exchange response contained
* only the first fragment of a multi-fragment response. The RPC client
* code will then make repeated xa-read requests to obtain the remaining
* fragments, which will result in smbrdr SmbReadX requests.
*
* SmbReadX should return the number of bytes received, in which case we
* expand the PDU size to include the received data, or a negative error
* code.
*/
static int
ndr_xa_read(ndr_client_t *clnt, ndr_xa_t *mxa)
{
ndr_stream_t *nds = &mxa->recv_nds;
int len;
int nbytes;
if ((len = (nds->pdu_max_size - nds->pdu_size)) < 0)
return (-1);
nbytes = smb_fh_read(clnt->xa_fd, 0, len,
(char *)nds->pdu_base_offset + nds->pdu_size);
if (nbytes < 0)
return (-1);
nds->pdu_size += nbytes;
if (nds->pdu_size > nds->pdu_max_size) {
nds->pdu_size = nds->pdu_max_size;
return (-1);
}
return (nbytes);
}
/*
* Preserve the heap so that the client application has access to data
* returned from the server after an RPC call.
*/
static void
ndr_xa_preserve(ndr_client_t *clnt, ndr_xa_t *mxa)
{
assert(clnt->heap == mxa->heap);
clnt->heap_preserved = B_TRUE;
mxa->heap = NULL;
}
/*
* Dispose of the transaction streams. If the heap has not been
* preserved, we can destroy it here.
*/
static void
ndr_xa_destruct(ndr_client_t *clnt, ndr_xa_t *mxa)
{
nds_destruct(&mxa->recv_nds);
nds_destruct(&mxa->send_nds);
if (!clnt->heap_preserved) {
ndr_heap_destroy(mxa->heap);
mxa->heap = NULL;
clnt->heap = NULL;
}
}
/*
* Dispose of a preserved heap.
*/
static void
ndr_xa_release(ndr_client_t *clnt)
{
if (clnt->heap_preserved) {
ndr_heap_destroy(clnt->heap);
clnt->heap = NULL;
clnt->heap_preserved = B_FALSE;
}
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2020 Tintri by DDN, Inc. All rights reserved.
*/
#ifndef _SMBSRV_NDR_H
#define _SMBSRV_NDR_H
/*
* Network Data Representation (NDR) is a compatible subset of DCE RPC
* and MSRPC NDR. NDR is used to move parameters consisting of
* complicated trees of data constructs between an RPC client and server.
*
* CAE Specification (1997)
* DCE 1.1: Remote Procedure Call
* Document Number: C706
* The Open Group
* ogspecs@opengroup.org
*/
#include <sys/types.h>
#include <sys/uio.h>
#include <stdlib.h>
#include <string.h>
#include <smb/wintypes.h>
#include <libmlrpc/ndrtypes.ndl>
#include <libmlrpc/rpcpdu.ndl>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Normal sequence:
* - Application calls client-side stub w/ TOP-MOST arg structure
* - client stub performs NDR_M_OP_MARSHALL+NDR_DIR_IN
* - PDU conveyed (request, aka call, aka query)
* - server stub performs NDR_M_OP_UNMARSHALL+NDR_DIR_IN
* - server function called w/ TOP-MOST arg structure
* - server function returns w/ TOP-MOST arg structure modified
* - server stub performs NDR_M_OP_MARSHALL+NDR_DIR_OUT
* - PDU conveyed (reply, aka result, aka response)
* - client stub performs NDR_M_OP_UNMARSHALL+NDR_DIR_OUT
* - return to Application w/ TOP-MOST arg structure modified
*
* An interface is a sequence of top-most constructs. Each top-most
* construct corresponds to one parameter, either argument or return
* value.
*
* A top-most construct is a sequence of outer constructs. The first
* outer construct is the referent of the argument, and the subsequent
* outer constructs are descendents referenced by pointers from prior
* constructs.
*
* An outer construct is a sequence of variable-sized info, fixed-sized
* data, and variable-sized data.
*/
/*
* Terminology
*
* The ALL UPPER CASE terms recur in the DCE/RPC documentation.
* The mixed-case names have been introduced as a reading aid.
*
* Size The size of an array in elements. Think of this
* as the amount to malloc().
*
* Length The number of elements of an array which are significant
* Think of this as the amount to bcopy().
*
* Known Size/length is known at build time.
*
* Determined Size/length is determined at run time.
*
* FIXED The Size and Length are Known.
* Think of this as a string constant or a DOS 8.3 file name.
* char array[] = "A Constant Size/Length";
*
* CONFORMANT The Size is Determined. Length is the same as Size.
* Think of this as strdup().
* char *array = strdup("Something");
*
* VARYING The Size is Known. The Length is determined.
* Think of this as a strcpy() of a variable length string
* into a fixed length buffer:
* char array[100];
* strcpy(array, "very short string");
*
* VARYING/CONFORMANT
* The Size is Determined. The Length is separately Determined.
* Think of this like:
* char *array = malloc(size);
* strcpy(array, "short string");
*
* STRING Strings can be CONFORMANT, VARYING, or CONFORMANT/VARYING.
* A string is fundamentally an array with the last
* significant element some sort of NULL.
*/
#define NDR_F_NONE 0x0000 /* no flags */
#define NDR_F_PARAMS_MASK 0x00FF
#define NDR_F_SIZE_IS 0x0001 /* [size_is(X)] required/given */
#define NDR_F_LENGTH_IS 0x0002 /* not implemented */
#define NDR_F_SWITCH_IS 0x0004 /* [switch_is(X)] req./given */
#define NDR_F_IS_STRING 0x0008 /* [string] req./given */
#define NDR_F_IS_POINTER 0x0010 /* TYPE * ... req./given */
#define NDR_F_IS_REFERENCE 0x0020 /* TYPE & ... req./given */
#define NDR_F_DIMENSION_IS 0x0040 /* TYPE [N] req./given */
#define NDR_F_WHENCE_MASK 0x00F0
#define NDR_F_BACKPTR 0x0010 /* ref cause by pointer */
#define NDR_F_OUTER 0x0020 /* ref caused by outer */
#define NDR_F_TOPMOST 0x0040 /* ref caused by topmost */
#define NDR_F_TYPEOP_MASK 0x0F00
#define NDR_F_ARRAY 0x0100 /* type is array of somethings */
#define NDR_F_POINTER 0x0200 /* type is pointer to something(s) */
#define NDR_F_STRING 0x0300 /* type is string of somethings */
#define NDR_F_UNION 0x0400 /* type is a union */
#define NDR_F_STRUCT 0x0500 /* type is a structure */
#define NDR_F_OPERATION 0x0600 /* type is a structure, special */
#define NDR_F_INTERFACE 0x0700 /* type is a union, special */
#define NDR_F_CONFORMANT 0x1000 /* struct conforming (var-size tail) */
#define NDR_F_VARYING 0x2000 /* not implemented */
#define NDR_F_FAKE 0x4000 /* not a real struct */
struct ndr_heap;
struct ndr_stream;
struct ndr_reference;
typedef uint16_t ndr_wchar_t;
typedef struct ndr_typeinfo {
unsigned char version; /* sanity check */
unsigned char alignment; /* mask */
unsigned short type_flags; /* NDR_F_... */
int (*ndr_func)(struct ndr_reference *);
unsigned short pdu_size_fixed_part;
unsigned short pdu_size_variable_part;
unsigned short c_size_fixed_part;
unsigned short c_size_variable_part;
} ndr_typeinfo_t;
typedef struct ndr_reference {
struct ndr_reference *next; /* queue list (outer only) */
struct ndr_reference *enclosing; /* e.g. struct for this memb */
struct ndr_stream *stream; /* root of NDR */
ndr_typeinfo_t *ti; /* type of data referenced */
char *name; /* name of this member */
unsigned long pdu_offset; /* referent in stub data */
char *datum; /* referent in local memory */
char **backptr; /* referer to set */
unsigned short outer_flags; /* XXX_is() from top level */
unsigned short inner_flags; /* XXX_is() in encapsulated */
unsigned short type_flags; /* "requires" */
unsigned short packed_alignment;
unsigned long size_is; /* conforming constructs */
unsigned long strlen_is; /* strings */
unsigned long switch_is; /* union arg selector */
unsigned long dimension_is; /* fixed-len array size */
unsigned long pdu_end_offset; /* offset for limit of PDU */
} ndr_ref_t;
/*
* For all operations, the ndr_stream, which is the root of NDR processing,
* is the primary object. When available, the appropriate ndr_ref_t
* is passed, NULL otherwise. Functions that return 'int' should return
* TRUE (!0) or FALSE (0). When functions return FALSE, including
* ndo_malloc() returning NULL, they should set the stream->error to an
* appropriate indicator of what went wrong.
*
* Functions ndo_get_pdu(), ndo_put_pdu(), and ndo_pad_pdu() must
* never grow the PDU data. A request for out-of-bounds data is an error.
* The swap_bytes flag is 1 if NDR knows that the byte-order in the PDU
* is different from the local system. ndo_pad_pdu() advised that the
* affected bytes should be zero filled.
*/
typedef struct ndr_stream_ops {
char *(*ndo_malloc)(struct ndr_stream *, unsigned, ndr_ref_t *);
int (*ndo_free)(struct ndr_stream *, char *, ndr_ref_t *);
int (*ndo_grow_pdu)(struct ndr_stream *, unsigned long, ndr_ref_t *);
int (*ndo_pad_pdu)(struct ndr_stream *, unsigned long,
unsigned long, ndr_ref_t *);
int (*ndo_get_pdu)(struct ndr_stream *, unsigned long,
unsigned long, char *, int, ndr_ref_t *);
int (*ndo_put_pdu)(struct ndr_stream *, unsigned long,
unsigned long, char *, int, ndr_ref_t *);
void (*ndo_tattle)(struct ndr_stream *, char *, ndr_ref_t *);
void (*ndo_tattle_error)(struct ndr_stream *, ndr_ref_t *);
int (*ndo_reset)(struct ndr_stream *);
void (*ndo_destruct)(struct ndr_stream *);
} ndr_stream_ops_t;
#define NDS_MALLOC(NDS, LEN, REF) \
(*(NDS)->ndo->ndo_malloc)(NDS, LEN, REF)
#define NDS_GROW_PDU(NDS, WANT_END_OFF, REF) \
(*(NDS)->ndo->ndo_grow_pdu)(NDS, WANT_END_OFF, REF)
#define NDS_PAD_PDU(NDS, PDU_OFFSET, N_BYTES, REF) \
(*(NDS)->ndo->ndo_pad_pdu)(NDS, PDU_OFFSET, N_BYTES, REF)
#define NDS_GET_PDU(NDS, PDU_OFFSET, N_BYTES, BUF, SWAP, REF) \
(*(NDS)->ndo->ndo_get_pdu)(NDS, PDU_OFFSET, N_BYTES, BUF, SWAP, REF)
#define NDS_PUT_PDU(NDS, PDU_OFFSET, N_BYTES, BUF, SWAP, REF) \
(*(NDS)->ndo->ndo_put_pdu)(NDS, PDU_OFFSET, N_BYTES, BUF, SWAP, REF)
#define NDS_TATTLE(NDS, WHAT, REF) \
(*(NDS)->ndo->ndo_tattle)(NDS, WHAT, REF)
#define NDS_TATTLE_ERROR(NDS, WHAT, REF) \
(*(NDS)->ndo->ndo_tattle_error)(NDS, REF)
#define NDS_RESET(NDS) (*(NDS)->ndo->ndo_reset)(NDS)
#define NDS_DESTRUCT(NDS) (*(NDS)->ndo->ndo_destruct)(NDS)
/*
* The ndr_stream_t tracks the state for a particular RPC call or response.
* A single call/response may be transmitted as multiple fragments, where
* each fragment has its own header and sec_trailer. For the layout of
* a fragment, see the comment at the top of ndr_marshal.c.
*
* pdu_base_addr is the buffer in which marshalled/received data is stored.
*
* pdu_base_offset is pdu_base_addr with a different type.
*
* pdu_max_size is the size of the buffer in pdu_base_addr.
*
* pdu_size represents the total amount of data stored in pdu_base_addr.
* It is typically less than pdu_max_size, and may contain more than one
* fragment (when reconstructing fragmented calls/responses, for example).
*
* pdu_body_offset points to the offset from pdu_base_addr of the PDU body
* of the currently encoded/decoded fragment.
*
* pdu_body_size is the size of the PDU body in the currently encoded/decoded
* fragment.
* For what constitutes the PDU Body, see ndr_marshal.c.
*
* pdu_hdr_size is the size of the header for the currently encoded/decoded
* fragment.
*
* pdu_scan_offset points to the next valid byte in pdu_base_addr
* (the next free space for encode, and the next unread byte for decode).
*/
typedef struct ndr_stream {
unsigned long pdu_size;
unsigned long pdu_max_size;
unsigned long pdu_base_offset;
unsigned long pdu_scan_offset;
unsigned long pdu_body_offset;
unsigned long pdu_body_size;
unsigned long pdu_hdr_size;
unsigned char *pdu_base_addr;
ndr_stream_ops_t *ndo;
unsigned char m_op;
unsigned char dir;
unsigned char swap; /* native/net endian swap */
unsigned char flags;
short error;
short error_ref;
ndr_ref_t *outer_queue_head;
ndr_ref_t **outer_queue_tailp;
ndr_ref_t *outer_current;
struct ndr_heap *heap;
} ndr_stream_t;
#define NDR_M_OP_NONE 0x00
#define NDR_M_OP_MARSHALL 0x01 /* data moving from datum to PDU */
#define NDR_M_OP_UNMARSHALL 0x02 /* data moving from PDU to datum */
#define NDR_DIR_NONE 0x00
#define NDR_DIR_IN 0x10 /* data moving from caller to callee */
#define NDR_DIR_OUT 0x20 /* data moving from callee to caller */
#define NDR_MODE_CALL_SEND (NDR_M_OP_MARSHALL + NDR_DIR_IN)
#define NDR_MODE_CALL_RECV (NDR_M_OP_UNMARSHALL + NDR_DIR_IN)
#define NDR_MODE_RETURN_SEND (NDR_M_OP_MARSHALL + NDR_DIR_OUT)
#define NDR_MODE_RETURN_RECV (NDR_M_OP_UNMARSHALL + NDR_DIR_OUT)
#define NDR_MODE_BUF_ENCODE NDR_MODE_CALL_SEND
#define NDR_MODE_BUF_DECODE NDR_MODE_RETURN_RECV
#define NDR_MODE_TO_M_OP(MODE) ((MODE) & 0x0F)
#define NDR_MODE_TO_DIR(MODE) ((MODE) & 0xF0)
#define NDR_M_OP_AND_DIR_TO_MODE(M_OP, DIR) ((M_OP)|(DIR))
#define NDR_MODE_MATCH(NDS, MODE) \
(NDR_M_OP_AND_DIR_TO_MODE((NDS)->m_op, (NDS)->dir) == (MODE))
#define NDR_IS_FIRST_FRAG(F) ((F) & NDR_PFC_FIRST_FRAG)
#define NDR_IS_LAST_FRAG(F) ((F) & NDR_PFC_LAST_FRAG)
#define NDR_IS_SINGLE_FRAG(F) \
(NDR_IS_FIRST_FRAG((F)) && NDR_IS_LAST_FRAG((F)))
#define NDS_F_NONE 0x00
#define NDS_F_NOTERM 0x01 /* strings are not null terminated */
#define NDS_F_NONULL 0x02 /* strings: no null on size_is */
#define NDS_SETF(S, F) ((S)->flags |= (F))
#define NDS_CLEARF(S, F) ((S)->flags &= ~(F))
#define NDR_ERR_MALLOC_FAILED -1
#define NDR_ERR_M_OP_INVALID -2
#define NDR_ERR_UNDERFLOW -3
#define NDR_ERR_GROW_FAILED -4 /* overflow */
#define NDR_ERR_PAD_FAILED -5 /* couldn't possibly happen */
#define NDR_ERR_OUTER_HEADER_BAD -6
#define NDR_ERR_SWITCH_VALUE_ILLEGAL -7
#define NDR_ERR_SWITCH_VALUE_INVALID -8
#define NDR_ERR_SWITCH_VALUE_MISSING -9
#define NDR_ERR_SIZE_IS_MISMATCH_PDU -10
#define NDR_ERR_SIZE_IS_MISMATCH_AFTER -11
#define NDR_ERR_SIZE_IS_UNEXPECTED -12
#define NDR_ERR_SIZE_IS_DUPLICATED -13
#define NDR_ERR_OUTER_PARAMS_MISMATCH -14
#define NDR_ERR_ARRAY_VARLEN_ILLEGAL -15
#define NDR_ERR_ARRAY_UNION_ILLEGAL -16
#define NDR_ERR_OUTER_PARAMS_BAD -17
#define NDR_ERR_OUTER_UNION_ILLEGAL -18
#define NDR_ERR_TOPMOST_UNION_ILLEGAL -19
#define NDR_ERR_TOPMOST_VARLEN_ILLEGAL -20
#define NDR_ERR_INNER_PARAMS_BAD -21
#define NDR_ERR_UNIMPLEMENTED -22
#define NDR_ERR_NOT_AN_INTERFACE -23
#define NDR_ERR_STRLEN -24
#define NDR_ERR_STRING_SIZING -25
#define NDR_ERR_BOUNDS_CHECK -26
#define NDR_SET_ERROR(REF, ERROR) \
((REF)->stream->error = (ERROR), \
(REF)->stream->error_ref = __LINE__, \
NDS_TATTLE_ERROR((REF)->stream, 0, REF))
#define NDR_TATTLE(REF, WHAT) \
(*(REF)->stream->ndo->ndo_tattle)((REF)->stream, WHAT, REF)
#define MEMBER_STR(MEMBER) #MEMBER
#define NDR_DIR_IS_IN (encl_ref->stream->dir == NDR_DIR_IN)
#define NDR_DIR_IS_OUT (encl_ref->stream->dir == NDR_DIR_OUT)
#define NDR_MEMBER_PTR_WITH_ARG(TYPE, MEMBER, OFFSET, \
ARGFLAGS, ARGMEM, ARGVAL) { \
myref.pdu_offset = encl_ref->pdu_offset + (OFFSET); \
myref.name = MEMBER_STR(MEMBER); \
myref.datum = (char *)val->MEMBER; \
myref.inner_flags = ARGFLAGS; \
myref.ti = &ndt_##TYPE; \
myref.ARGMEM = ARGVAL; \
if (!ndr_inner(&myref)) \
return (0); \
}
#define NDR_MEMBER_PTR_WITH_DIMENSION(TYPE, MEMBER, OFFSET, SIZE_IS) \
NDR_MEMBER_PTR_WITH_ARG(TYPE, MEMBER, OFFSET, \
NDR_F_DIMENSION_IS, dimension_is, SIZE_IS)
#define NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \
ARGFLAGS, ARGMEM, ARGVAL) { \
myref.pdu_offset = encl_ref->pdu_offset + (OFFSET); \
myref.name = MEMBER_STR(MEMBER); \
myref.datum = (char *)&val->MEMBER; \
myref.inner_flags = ARGFLAGS; \
myref.ti = &ndt_##TYPE; \
myref.ARGMEM = ARGVAL; \
if (!ndr_inner(&myref)) \
return (0); \
}
#define NDR_MEMBER(TYPE, MEMBER, OFFSET) \
NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \
NDR_F_NONE, size_is, 0)
#define NDR_MEMBER_ARR_WITH_SIZE_IS(TYPE, MEMBER, OFFSET, SIZE_IS) \
NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \
NDR_F_SIZE_IS, size_is, SIZE_IS)
#define NDR_MEMBER_ARR_WITH_DIMENSION(TYPE, MEMBER, OFFSET, SIZE_IS) \
NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \
NDR_F_DIMENSION_IS, dimension_is, SIZE_IS)
#define NDR_MEMBER_PTR_WITH_SIZE_IS(TYPE, MEMBER, OFFSET, SIZE_IS) \
NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \
NDR_F_SIZE_IS+NDR_F_IS_POINTER, size_is, SIZE_IS)
#define NDR_MEMBER_PTR(TYPE, MEMBER, OFFSET) \
NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \
NDR_F_IS_POINTER, size_is, 0)
#define NDR_MEMBER_WITH_SWITCH_IS(TYPE, MEMBER, OFFSET, SWITCH_IS) \
NDR_MEMBER_WITH_ARG(TYPE, MEMBER, OFFSET, \
NDR_F_SWITCH_IS, switch_is, SWITCH_IS)
#define NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \
ARGFLAGS, ARGMEM, ARGVAL) { \
myref.pdu_offset = -1; \
myref.name = MEMBER_STR(MEMBER); \
myref.datum = (char *)&val->MEMBER; \
myref.inner_flags = ARGFLAGS; \
myref.ti = &ndt_##TYPE; \
myref.ARGMEM = ARGVAL; \
if (!ndr_topmost(&myref)) \
return (0); \
}
#define NDR_TOPMOST_MEMBER(TYPE, MEMBER) \
NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \
NDR_F_NONE, size_is, 0)
#define NDR_TOPMOST_MEMBER_ARR_WITH_SIZE_IS(TYPE, MEMBER, SIZE_IS) \
NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \
NDR_F_SIZE_IS, size_is, SIZE_IS)
#define NDR_TOPMOST_MEMBER_ARR_WITH_DIMENSION(TYPE, MEMBER, SIZE_IS) \
NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \
NDR_F_DIMENSION_IS, dimension_is, SIZE_IS)
#define NDR_TOPMOST_MEMBER_PTR_WITH_SIZE_IS(TYPE, MEMBER, SIZE_IS) \
NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \
NDR_F_SIZE_IS+NDR_F_IS_POINTER, size_is, SIZE_IS)
#define NDR_TOPMOST_MEMBER_PTR(TYPE, MEMBER) \
NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \
NDR_F_IS_POINTER, size_is, 0)
#define NDR_TOPMOST_MEMBER_REF(TYPE, MEMBER) \
NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \
NDR_F_IS_REFERENCE, size_is, 0)
#define NDR_TOPMOST_MEMBER_REF_WITH_SIZE_IS(TYPE, MEMBER, SIZE_IS) \
NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \
NDR_F_SIZE_IS+NDR_F_IS_REFERENCE, size_is, SIZE_IS)
#define NDR_TOPMOST_MEMBER_WITH_SWITCH_IS(TYPE, MEMBER, SWITCH_IS) \
NDR_TOPMOST_MEMBER_WITH_ARG(TYPE, MEMBER, \
NDR_F_SWITCH_IS, switch_is, SWITCH_IS)
/* this is assuming offset+0 */
#define NDR_PARAMS_MEMBER_WITH_ARG(TYPE, MEMBER, ARGFLAGS, \
ARGMEM, ARGVAL) { \
myref.pdu_offset = encl_ref->pdu_offset; \
myref.name = MEMBER_STR(MEMBER); \
myref.datum = (char *)&val->MEMBER; \
myref.inner_flags = ARGFLAGS; \
myref.ti = &ndt_##TYPE; \
myref.ARGMEM = ARGVAL; \
if (!ndr_params(&myref)) \
return (0); \
}
#define NDR_PARAMS_MEMBER(TYPE, MEMBER) \
NDR_PARAMS_MEMBER_WITH_ARG(TYPE, MEMBER, \
NDR_F_NONE, size_is, 0)
#define NDR_STRING_DIM 1
#define NDR_ANYSIZE_DIM 1
int ndo_process(struct ndr_stream *, ndr_typeinfo_t *, char *);
int ndo_operation(struct ndr_stream *, ndr_typeinfo_t *, int opnum, char *);
void ndo_printf(struct ndr_stream *, ndr_ref_t *, const char *, ...);
void ndo_trace(const char *);
void ndo_fmt(struct ndr_stream *, ndr_ref_t *, char *);
int ndr_params(ndr_ref_t *);
int ndr_topmost(ndr_ref_t *);
int ndr_run_outer_queue(struct ndr_stream *);
int ndr_outer(ndr_ref_t *);
int ndr_outer_fixed(ndr_ref_t *);
int ndr_outer_fixed_array(ndr_ref_t *);
int ndr_outer_conformant_array(ndr_ref_t *);
int ndr_outer_conformant_construct(ndr_ref_t *);
int ndr_size_is(ndr_ref_t *);
int ndr_outer_string(ndr_ref_t *);
int ndr_outer_peek_sizing(ndr_ref_t *, unsigned, unsigned long *);
int ndr_outer_poke_sizing(ndr_ref_t *, unsigned, unsigned long *);
int ndr_outer_align(ndr_ref_t *);
int ndr_outer_grow(ndr_ref_t *, unsigned);
int ndr_inner(ndr_ref_t *);
int ndr_inner_pointer(ndr_ref_t *);
int ndr_inner_reference(ndr_ref_t *);
int ndr_inner_array(ndr_ref_t *);
size_t ndr_mbstowcs(struct ndr_stream *, ndr_wchar_t *, const char *, size_t);
void nds_bswap(void *src, void *dst, size_t len);
#ifdef __cplusplus
}
#endif
#endif /* _SMBSRV_NDR_H */
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2020 Tintri by DDN, Inc. All Rights Reserved.
* Copyright 2023 RackTop Systems, Inc.
*/
#include <libmlrpc.h>
#include <sys/sysmacros.h>
#include <strings.h>
/*
* Initializes the sec_trailer (ndr_sec_t).
* The actual token is allocated and set later (in the SSP).
*/
int
ndr_add_auth_token(ndr_auth_ctx_t *ctx, ndr_xa_t *mxa)
{
ndr_stream_t *nds = &mxa->send_nds;
ndr_sec_t *secp = &mxa->send_auth;
secp->auth_type = ctx->auth_type;
secp->auth_level = ctx->auth_level;
secp->auth_rsvd = 0;
/*
* [MS-RPCE] 2.2.2.12 "Authentication Tokens"
* auth_pad_len aligns the packet to 16 bytes.
*/
secp->auth_pad_len = P2ROUNDUP(nds->pdu_scan_offset, 16) -
nds->pdu_scan_offset;
if (NDS_PAD_PDU(nds, nds->pdu_scan_offset,
secp->auth_pad_len, NULL) == 0)
return (NDR_DRC_FAULT_SEC_ENCODE_TOO_BIG);
/* PAD_PDU doesn't adjust scan_offset */
nds->pdu_scan_offset += secp->auth_pad_len;
nds->pdu_body_size = nds->pdu_scan_offset -
nds->pdu_body_offset;
secp->auth_context_id = ctx->auth_context_id;
return (NDR_DRC_OK);
}
/*
* Does gss_init_sec_context (or equivalent) and creates
* the sec_trailer and the auth token.
*
* Used during binds (and alter context).
*
* Currently, only NETLOGON auth with Integrity/Privacy protection
* is implemented.
*/
int
ndr_add_sec_context(ndr_auth_ctx_t *ctx, ndr_xa_t *mxa)
{
int rc;
if (ctx->auth_level == NDR_C_AUTHN_NONE ||
ctx->auth_type == NDR_C_AUTHN_NONE)
return (NDR_DRC_OK);
if (ctx->auth_type != NDR_C_AUTHN_GSS_NETLOGON)
return (NDR_DRC_FAULT_SEC_TYPE_UNIMPLEMENTED);
if (ctx->auth_level != NDR_C_AUTHN_LEVEL_PKT_INTEGRITY &&
ctx->auth_level != NDR_C_AUTHN_LEVEL_PKT_PRIVACY)
return (NDR_DRC_FAULT_SEC_LEVEL_UNIMPLEMENTED);
if ((rc = ndr_add_auth_token(ctx, mxa)) != 0)
return (rc);
return (ctx->auth_ops.nao_init(ctx->auth_ctx, mxa));
}
/*
* Does response-side gss_init_sec_context (or equivalent) and validates
* the sec_trailer and the auth token.
*
* Used during bind (and alter context) ACKs.
*/
int
ndr_recv_sec_context(ndr_auth_ctx_t *ctx, ndr_xa_t *mxa)
{
ndr_sec_t *bind_secp = &mxa->send_auth;
ndr_sec_t *ack_secp = &mxa->recv_auth;
if (ctx->auth_level == NDR_C_AUTHN_NONE ||
ctx->auth_type == NDR_C_AUTHN_NONE) {
if (mxa->recv_hdr.common_hdr.auth_length != 0)
return (NDR_DRC_FAULT_SEC_AUTH_LENGTH_INVALID);
return (NDR_DRC_OK);
} else if (mxa->recv_hdr.common_hdr.auth_length == 0) {
return (NDR_DRC_FAULT_SEC_AUTH_LENGTH_INVALID);
}
if (bind_secp->auth_type != ack_secp->auth_type)
return (NDR_DRC_FAULT_SEC_AUTH_TYPE_INVALID);
if (bind_secp->auth_level != ack_secp->auth_level)
return (NDR_DRC_FAULT_SEC_AUTH_LEVEL_INVALID);
return (ctx->auth_ops.nao_recv(ctx->auth_ctx, mxa));
}
/*
* Does gss_MICEx (or equivalent) and creates
* the sec_trailer and the auth token.
*
* Used upon sending a request (client)/response (server) packet.
*/
int
ndr_add_auth(ndr_auth_ctx_t *ctx, ndr_xa_t *mxa)
{
int rc;
if (ctx->auth_level == NDR_C_AUTHN_NONE ||
ctx->auth_type == NDR_C_AUTHN_NONE)
return (NDR_DRC_OK);
if (ctx->auth_type != NDR_C_AUTHN_GSS_NETLOGON)
return (NDR_DRC_FAULT_SEC_TYPE_UNIMPLEMENTED);
if (ctx->auth_level != NDR_C_AUTHN_LEVEL_PKT_INTEGRITY &&
ctx->auth_level != NDR_C_AUTHN_LEVEL_PKT_PRIVACY)
return (NDR_DRC_FAULT_SEC_LEVEL_UNIMPLEMENTED);
if ((rc = ndr_add_auth_token(ctx, mxa)) != 0)
return (rc);
if (ctx->auth_level == NDR_C_AUTHN_LEVEL_PKT_PRIVACY)
return (ctx->auth_ops.nao_encrypt(ctx->auth_ctx, mxa));
return (ctx->auth_ops.nao_sign(ctx->auth_ctx, mxa));
}
/*
* Does gss_VerifyMICEx (or equivalent) and validates
* the sec_trailer and the auth token.
*
* Used upon receiving a request (server)/response (client) packet.
*
* If auth_verify_resp is B_FALSE, this doesn't verify responses (but
* the SSP may still have side-effects).
*/
int
ndr_check_auth(ndr_auth_ctx_t *ctx, ndr_xa_t *mxa)
{
ndr_sec_t *secp = &mxa->recv_auth;
if (ctx->auth_level == NDR_C_AUTHN_NONE ||
ctx->auth_type == NDR_C_AUTHN_NONE) {
if (mxa->recv_hdr.common_hdr.auth_length != 0)
return (NDR_DRC_FAULT_SEC_AUTH_LENGTH_INVALID);
return (NDR_DRC_OK);
} else if (mxa->recv_hdr.common_hdr.auth_length == 0) {
return (NDR_DRC_FAULT_SEC_AUTH_LENGTH_INVALID);
}
if (ctx->auth_type != secp->auth_type ||
ctx->auth_type != NDR_C_AUTHN_GSS_NETLOGON)
return (NDR_DRC_FAULT_SEC_AUTH_TYPE_INVALID);
if (ctx->auth_level != secp->auth_level ||
(ctx->auth_level != NDR_C_AUTHN_LEVEL_PKT_INTEGRITY &&
ctx->auth_level != NDR_C_AUTHN_LEVEL_PKT_PRIVACY))
return (NDR_DRC_FAULT_SEC_AUTH_LEVEL_INVALID);
if (ctx->auth_level == NDR_C_AUTHN_LEVEL_PKT_PRIVACY)
return (ctx->auth_ops.nao_decrypt(ctx->auth_ctx, mxa,
ctx->auth_verify_resp));
return (ctx->auth_ops.nao_verify(ctx->auth_ctx, mxa,
ctx->auth_verify_resp));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2020 Tintri by DDN, Inc. All rights reserved.
*/
#include <sys/errno.h>
#include <string.h>
#include <strings.h>
#include <libmlrpc.h>
#define NDR_DEFAULT_FRAGSZ 8192
#define NDR_MULTI_FRAGSZ (60 * 1024)
static void ndr_clnt_init_hdr(ndr_client_t *, ndr_xa_t *);
static int ndr_clnt_get_frags(ndr_client_t *, ndr_xa_t *);
static int ndr_clnt_get_frag(ndr_client_t *, ndr_xa_t *, ndr_common_header_t *);
int
ndr_clnt_bind(ndr_client_t *clnt, ndr_service_t *msvc,
ndr_binding_t **ret_binding_p)
{
ndr_binding_t *mbind;
ndr_xa_t mxa;
ndr_bind_hdr_t *bhdr;
ndr_common_header_t *hdr;
ndr_p_cont_elem_t *pce;
ndr_bind_ack_hdr_t *bahdr;
ndr_p_result_t *pre;
int rc;
bzero(&mxa, sizeof (mxa));
mxa.binding_list = clnt->binding_list;
if ((mbind = ndr_svc_new_binding(&mxa)) == NULL)
return (NDR_DRC_FAULT_API_BIND_NO_SLOTS);
ndr_clnt_init_hdr(clnt, &mxa);
bhdr = &mxa.send_hdr.bind_hdr;
hdr = &mxa.send_hdr.bind_hdr.common_hdr;
hdr->ptype = NDR_PTYPE_BIND;
bhdr->max_xmit_frag = NDR_DEFAULT_FRAGSZ;
bhdr->max_recv_frag = NDR_DEFAULT_FRAGSZ;
bhdr->assoc_group_id = 0;
bhdr->p_context_elem.n_context_elem = 1;
/* Assign presentation context id */
pce = &bhdr->p_context_elem.p_cont_elem[0];
pce->p_cont_id = clnt->next_p_cont_id++;
pce->n_transfer_syn = 1;
/* Set up UUIDs and versions from the service */
pce->abstract_syntax.if_version = msvc->abstract_syntax_version;
rc = ndr_uuid_parse(msvc->abstract_syntax_uuid,
&pce->abstract_syntax.if_uuid);
if (rc != 0)
return (NDR_DRC_FAULT_API_SERVICE_INVALID);
pce->transfer_syntaxes[0].if_version = msvc->transfer_syntax_version;
rc = ndr_uuid_parse(msvc->transfer_syntax_uuid,
&pce->transfer_syntaxes[0].if_uuid);
if (rc != 0)
return (NDR_DRC_FAULT_API_SERVICE_INVALID);
/* Format and exchange the PDU */
if ((*clnt->xa_init)(clnt, &mxa) < 0)
return (NDR_DRC_FAULT_OUT_OF_MEMORY);
/* Reserve room for hdr */
mxa.send_nds.pdu_scan_offset = sizeof (*bhdr);
/* GSS_Init_sec_context */
rc = ndr_add_sec_context(&clnt->auth_ctx, &mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
rc = ndr_encode_pdu_auth(&mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
/*
* If we have auth data, then pdu_size has been initialized.
* Otherwise, it hasn't.
*/
if (hdr->auth_length == 0)
hdr->frag_length = sizeof (*bhdr);
else
hdr->frag_length = mxa.send_nds.pdu_size;
/* Reset scan_offset to header */
mxa.send_nds.pdu_scan_offset = 0;
rc = ndr_encode_pdu_hdr(&mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
if ((*clnt->xa_exchange)(clnt, &mxa) < 0) {
rc = NDR_DRC_FAULT_SEND_FAILED;
goto fault_exit;
}
rc = ndr_decode_pdu_hdr(&mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
rc = ndr_decode_pdu_auth(&mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
bahdr = &mxa.recv_hdr.bind_ack_hdr;
if (mxa.ptype != NDR_PTYPE_BIND_ACK) {
rc = NDR_DRC_FAULT_RECEIVED_MALFORMED;
goto fault_exit;
}
if (bahdr->p_result_list.n_results != 1) {
rc = NDR_DRC_FAULT_RECEIVED_MALFORMED;
goto fault_exit;
}
pre = &bahdr->p_result_list.p_results[0];
if (pre->result != NDR_PCDR_ACCEPTANCE) {
rc = NDR_DRC_FAULT_RECEIVED_MALFORMED;
goto fault_exit;
}
/* GSS_init_sec_context 2 */
rc = ndr_recv_sec_context(&clnt->auth_ctx, &mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
/* done with buffers */
(*clnt->xa_destruct)(clnt, &mxa);
mbind->p_cont_id = pce->p_cont_id;
mbind->which_side = NDR_BIND_SIDE_CLIENT;
mbind->clnt = clnt;
mbind->service = msvc;
mbind->instance_specific = 0;
*ret_binding_p = mbind;
return (NDR_DRC_OK);
fault_exit:
(*clnt->xa_destruct)(clnt, &mxa);
return (rc);
}
int
ndr_clnt_call(ndr_binding_t *mbind, int opnum, void *params)
{
ndr_client_t *clnt = mbind->clnt;
ndr_xa_t mxa;
ndr_request_hdr_t *reqhdr;
ndr_common_header_t *rsphdr;
unsigned long recv_pdu_scan_offset, recv_pdu_size;
int rc;
bzero(&mxa, sizeof (mxa));
mxa.ptype = NDR_PTYPE_REQUEST;
mxa.opnum = opnum;
mxa.binding = mbind;
ndr_clnt_init_hdr(clnt, &mxa);
reqhdr = &mxa.send_hdr.request_hdr;
reqhdr->common_hdr.ptype = NDR_PTYPE_REQUEST;
reqhdr->p_cont_id = mbind->p_cont_id;
reqhdr->opnum = opnum;
rc = (*clnt->xa_init)(clnt, &mxa);
if (NDR_DRC_IS_FAULT(rc))
return (rc);
/* Reserve room for hdr */
mxa.send_nds.pdu_scan_offset = sizeof (*reqhdr);
/* pdu_scan_offset now points to start of stub */
mxa.send_nds.pdu_body_offset = mxa.send_nds.pdu_scan_offset;
rc = ndr_encode_call(&mxa, params);
if (!NDR_DRC_IS_OK(rc))
goto fault_exit;
/*
* With the Stub data encoded, calculate the alloc_hint
* before we add padding or auth data.
*/
reqhdr->alloc_hint = mxa.send_nds.pdu_size -
sizeof (ndr_request_hdr_t);
/* GSS_WrapEx/VerifyMICEx */
rc = ndr_add_auth(&clnt->auth_ctx, &mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
rc = ndr_encode_pdu_auth(&mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
/*
* Now we have the PDU size, we need to set up the
* frag_length.
* Also reset pdu_scan_offset to header.
*/
mxa.send_hdr.common_hdr.frag_length = mxa.send_nds.pdu_size;
mxa.send_nds.pdu_scan_offset = 0;
rc = ndr_encode_pdu_hdr(&mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
rc = (*clnt->xa_exchange)(clnt, &mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
rc = ndr_decode_pdu_hdr(&mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
if (mxa.ptype != NDR_PTYPE_RESPONSE) {
rc = NDR_DRC_FAULT_RECEIVED_MALFORMED;
goto fault_exit;
}
rc = ndr_decode_pdu_auth(&mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
rc = ndr_check_auth(&clnt->auth_ctx, &mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
rsphdr = &mxa.recv_hdr.common_hdr;
if (!NDR_IS_LAST_FRAG(rsphdr->pfc_flags)) {
/*
* This is a multi-fragment response.
* Preserve the current body offset while getting
* fragments so that we can continue afterward
* as if we had received the entire response as
* a single PDU.
*
* GROW_PDU trashes pdu_size; reset it afterwards.
*/
recv_pdu_size = mxa.recv_nds.pdu_size;
(void) NDS_GROW_PDU(&mxa.recv_nds, NDR_MULTI_FRAGSZ, NULL);
/*
* pdu_scan_offset needs to be the first byte after the first
* fragment in pdu_base_addr (minus the sec_trailer).
*
* pdu_size needs to be all of the (usable) data we've
* received thus far.
*/
recv_pdu_scan_offset = mxa.recv_nds.pdu_body_offset;
mxa.recv_nds.pdu_scan_offset = mxa.recv_nds.pdu_body_offset +
mxa.recv_nds.pdu_body_size - mxa.recv_auth.auth_pad_len;
mxa.recv_nds.pdu_size = recv_pdu_size;
rc = ndr_clnt_get_frags(clnt, &mxa);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
mxa.recv_nds.pdu_scan_offset = recv_pdu_scan_offset;
}
rc = ndr_decode_return(&mxa, params);
if (NDR_DRC_IS_FAULT(rc))
goto fault_exit;
(*clnt->xa_preserve)(clnt, &mxa);
(*clnt->xa_destruct)(clnt, &mxa);
return (NDR_DRC_OK);
fault_exit:
ndr_show_hdr(&mxa.send_hdr.common_hdr);
nds_show_state(&mxa.send_nds);
if (mxa.send_hdr.common_hdr.auth_length != 0)
ndr_show_auth(&mxa.send_auth);
ndr_show_hdr(&mxa.recv_hdr.common_hdr);
nds_show_state(&mxa.recv_nds);
if (mxa.recv_hdr.common_hdr.auth_length != 0)
ndr_show_auth(&mxa.recv_auth);
(*clnt->xa_destruct)(clnt, &mxa);
return (rc);
}
void
ndr_clnt_free_heap(ndr_client_t *clnt)
{
(*clnt->xa_release)(clnt);
}
static void
ndr_clnt_init_hdr(ndr_client_t *clnt, ndr_xa_t *mxa)
{
ndr_common_header_t *hdr = &mxa->send_hdr.common_hdr;
hdr->rpc_vers = 5;
hdr->rpc_vers_minor = 0;
hdr->pfc_flags = NDR_PFC_FIRST_FRAG + NDR_PFC_LAST_FRAG;
hdr->packed_drep.intg_char_rep = NDR_REPLAB_CHAR_ASCII;
#ifndef _BIG_ENDIAN
hdr->packed_drep.intg_char_rep |= NDR_REPLAB_INTG_LITTLE_ENDIAN;
#endif
/* hdr->frag_length */
hdr->auth_length = 0;
hdr->call_id = clnt->next_call_id++;
}
/*
* ndr_clnt_get_frags
*
* A DCE RPC message that is larger than a single fragment is transmitted
* as a series of fragments: 5280 bytes for Windows NT and 4280 bytes for
* both Windows 2000 and 2003.
*
* Collect RPC fragments and append them to the receive stream buffer.
* Each received fragment has a header, which we need to remove as we
* build the full RPC PDU. The scan offset is used to track frag headers.
*/
static int
ndr_clnt_get_frags(ndr_client_t *clnt, ndr_xa_t *mxa)
{
ndr_stream_t *nds = &mxa->recv_nds;
ndr_common_header_t hdr;
int frag_size;
int last_frag;
int rc;
do {
if (ndr_clnt_get_frag(clnt, mxa, &hdr) < 0) {
nds_show_state(nds);
return (NDR_DRC_FAULT_RECEIVED_RUNT);
}
last_frag = NDR_IS_LAST_FRAG(hdr.pfc_flags);
frag_size = hdr.frag_length;
/*
* ndr_clnt_get_frag() doesn't change pdu_scan_offset.
*/
if (frag_size > (nds->pdu_size - nds->pdu_scan_offset)) {
nds_show_state(nds);
return (NDR_DRC_FAULT_RECEIVED_MALFORMED);
}
if (hdr.auth_length != 0 && hdr.auth_length >
(hdr.frag_length - nds->pdu_hdr_size - SEC_TRAILER_SIZE))
return (NDR_DRC_FAULT_RECEIVED_MALFORMED);
rc = ndr_decode_pdu_auth(mxa);
if (NDR_DRC_IS_FAULT(rc))
return (rc);
rc = ndr_check_auth(&clnt->auth_ctx, mxa);
if (NDR_DRC_IS_FAULT(rc))
return (rc);
/*
* Headers, Auth Padding, and auth data shouldn't be kept
* from fragments.
*/
ndr_remove_frag_hdr(nds);
nds->pdu_scan_offset +=
nds->pdu_body_size - mxa->recv_auth.auth_pad_len;
} while (!last_frag);
return (0);
}
/*
* Read the next RPC fragment. The xa_read() calls correspond to SmbReadX
* requests. Note that there is no correspondence between SmbReadX buffering
* and DCE RPC fragment alignment.
*/
static int
ndr_clnt_get_frag(ndr_client_t *clnt, ndr_xa_t *mxa, ndr_common_header_t *hdr)
{
ndr_stream_t *nds = &mxa->recv_nds;
unsigned long available;
int nbytes = 0;
available = nds->pdu_size - nds->pdu_scan_offset;
while (available < NDR_RSP_HDR_SIZE) {
if ((nbytes = (*clnt->xa_read)(clnt, mxa)) <= 0)
return (-1);
available += nbytes;
}
ndr_decode_frag_hdr(nds, hdr);
ndr_show_hdr(hdr);
while (available < hdr->frag_length) {
if ((nbytes = (*clnt->xa_read)(clnt, mxa)) <= 0)
return (-1);
available += nbytes;
}
return (nbytes);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2013 Nexenta Systems, Inc. All rights reserved.
*/
/*
* NDR heap management. The heap is used for temporary storage by
* both the client and server side library routines. In order to
* support the different requirements of the various RPCs, the heap
* can grow dynamically if required. We start with a single block
* and perform sub-allocations from it. If an RPC requires more space
* we will continue to add it a block at a time. This means that we
* don't hog lots of memory on every call to support the few times
* that we actually need a lot heap space.
*
* Note that there is no individual free function. Once space has been
* allocated, it remains allocated until the heap is destroyed. This
* shouldn't be an issue because the heap is being filled with data to
* be marshalled or unmarshalled and we need it all to be there until
* the point that the entire heap is no longer required.
*/
#include <sys/errno.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/uio.h>
#include <libmlrpc.h>
#include <ndr_wchar.h>
/*
* Allocate a heap structure and the first heap block. For many RPC
* operations this will be the only time we need to malloc memory
* in this instance of the heap. The only point of note here is that
* we put the heap management data in the first block to avoid a
* second malloc. Make sure that sizeof(ndr_heap_t) is smaller
* than NDR_HEAP_BLKSZ.
*
* Note that the heap management data is at the start of the first block.
*
* Returns a pointer to the newly created heap, which is used like an
* opaque handle with the rest of the heap management interface..
*/
ndr_heap_t *
ndr_heap_create(void)
{
ndr_heap_t *heap;
char *base;
size_t allocsize = sizeof (ndr_heap_t) + NDR_HEAP_BLKSZ;
if ((heap = malloc(allocsize)) == NULL)
return (NULL);
base = (char *)heap;
bzero(heap, sizeof (ndr_heap_t));
heap->iovcnt = NDR_HEAP_MAXIOV;
heap->iov = heap->iovec;
heap->iov->iov_base = base;
heap->iov->iov_len = sizeof (ndr_heap_t);
heap->top = base + allocsize;
heap->next = base + sizeof (ndr_heap_t);
return (heap);
}
/*
* Deallocate all of the memory associated with a heap. This is the
* only way to deallocate heap memory, it isn't possible to free the
* space obtained by individual malloc calls.
*
* Note that the first block contains the heap management data, which
* is deleted last.
*/
void
ndr_heap_destroy(ndr_heap_t *heap)
{
int i;
char *p;
if (heap) {
for (i = 1; i < NDR_HEAP_MAXIOV; ++i) {
if ((p = heap->iovec[i].iov_base) != NULL)
free(p);
}
free(heap);
}
}
/*
* Allocate space in the specified heap. All requests are padded, if
* required, to ensure dword alignment. If the current iov will be
* exceeded, we allocate a new block and setup the next iov. Otherwise
* all we have to do is move the next pointer and update the current
* iov length.
*
* On success, a pointer to the allocated (dword aligned) area is
* returned. Otherwise a null pointer is returned.
*/
void *
ndr_heap_malloc(ndr_heap_t *heap, unsigned size)
{
char *p;
int incr_size;
size += NDR_ALIGN4(size);
if (heap == NULL || size == 0)
return (NULL);
p = heap->next;
if (p + size > heap->top) {
if ((heap->iovcnt == 0) || ((--heap->iovcnt) == 0))
return (NULL);
incr_size = (size < NDR_HEAP_BLKSZ) ? NDR_HEAP_BLKSZ : size;
if ((p = (char *)malloc(incr_size)) == NULL)
return (NULL);
++heap->iov;
heap->iov->iov_base = p;
heap->iov->iov_len = 0;
heap->top = p + incr_size;
}
heap->next = p + size;
heap->iov->iov_len += size;
return ((void *)p);
}
/*
* Convenience function to copy some memory into the heap.
*/
void *
ndr_heap_dupmem(ndr_heap_t *heap, const void *mem, size_t len)
{
void *p;
if (mem == NULL)
return (NULL);
if ((p = ndr_heap_malloc(heap, len)) != NULL)
(void) memcpy(p, mem, len);
return (p);
}
/*
* Convenience function to do heap strdup.
*/
void *
ndr_heap_strdup(ndr_heap_t *heap, const char *s)
{
int len;
void *p;
if (s == NULL)
return (NULL);
/*
* We don't need to clutter the heap with empty strings.
*/
if ((len = strlen(s)) == 0)
return ("");
p = ndr_heap_dupmem(heap, s, len+1);
return (p);
}
/*
* Make an ndr_mstring_t from a regular string.
*/
int
ndr_heap_mstring(ndr_heap_t *heap, const char *s, ndr_mstring_t *out)
{
size_t slen;
if (s == NULL || out == NULL)
return (-1);
/*
* Determine the WC strlen of s
* Was ndr__wcequiv_strlen(s)
*/
slen = ndr__mbstowcs(NULL, s, NDR_STRING_MAX);
if (slen == (size_t)-1)
return (-1);
out->length = slen * sizeof (ndr_wchar_t);
out->allosize = out->length + sizeof (ndr_wchar_t);
if ((out->str = ndr_heap_strdup(heap, s)) == NULL)
return (-1);
return (0);
}
/*
* Our regular string marshalling always creates null terminated strings
* but some Windows clients and servers are pedantic about the string
* formats they will accept and require non-null terminated strings.
* This function can be used to build a wide-char, non-null terminated
* string in the heap as a varying/conformant array. We need to do the
* wide-char conversion here because the marshalling code won't be
* aware that this is really a string.
*/
void
ndr_heap_mkvcs(ndr_heap_t *heap, char *s, ndr_vcstr_t *vc)
{
size_t slen;
int mlen;
/*
* Determine the WC strlen of s
* Was ndr__wcequiv_strlen(s)
*/
slen = ndr__mbstowcs(NULL, s, NDR_STRING_MAX);
if (slen == (size_t)-1)
slen = 0;
vc->wclen = slen * sizeof (ndr_wchar_t);
vc->wcsize = vc->wclen;
/*
* alloc one extra wchar for a null
* See slen + 1 arg for mbstowcs
*/
mlen = sizeof (ndr_vcs_t) + vc->wcsize + sizeof (ndr_wchar_t);
vc->vcs = ndr_heap_malloc(heap, mlen);
if (vc->vcs) {
vc->vcs->vc_first_is = 0;
vc->vcs->vc_length_is = slen;
(void) ndr__mbstowcs(vc->vcs->buffer, s, slen + 1);
}
}
void
ndr_heap_mkvcb(ndr_heap_t *heap, uint8_t *data, uint32_t datalen,
ndr_vcbuf_t *vcbuf)
{
int mlen;
if (data == NULL || datalen == 0) {
bzero(vcbuf, sizeof (ndr_vcbuf_t));
return;
}
vcbuf->len = datalen;
vcbuf->size = datalen;
mlen = sizeof (ndr_vcbuf_t) + datalen;
vcbuf->vcb = ndr_heap_malloc(heap, mlen);
if (vcbuf->vcb) {
vcbuf->vcb->vc_first_is = 0;
vcbuf->vcb->vc_length_is = datalen;
bcopy(data, vcbuf->vcb->buffer, datalen);
}
}
/*
* Removed ndr_heap_siddup(), now using ndr_heap_dupmem().
*/
int
ndr_heap_used(ndr_heap_t *heap)
{
int used = 0;
int i;
for (i = 0; i < NDR_HEAP_MAXIOV; ++i)
used += heap->iovec[i].iov_len;
return (used);
}
int
ndr_heap_avail(ndr_heap_t *heap)
{
int avail;
int count;
count = (heap->iovcnt == 0) ? 0 : (heap->iovcnt - 1);
avail = count * NDR_HEAP_BLKSZ;
avail += (heap->top - heap->next);
return (avail);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2021 Tintri by DDN, Inc. All rights reserved.
*/
#include <assert.h>
#include <strings.h>
#include <sys/param.h>
#include <sys/debug.h>
#include <libmlrpc.h>
#ifdef _BIG_ENDIAN
static const int ndr_native_byte_order = NDR_REPLAB_INTG_BIG_ENDIAN;
#else
static const int ndr_native_byte_order = NDR_REPLAB_INTG_LITTLE_ENDIAN;
#endif
static int ndr_decode_hdr_common(ndr_stream_t *, ndr_common_header_t *);
static int ndr_decode_pac_hdr(ndr_stream_t *, ndr_pac_hdr_t *);
/*
* This is the layout of an RPC PDU, as shown in
* [MS-RPCE] 2.2.2.13 "Verification Trailer".
*
* +-------------------------------+
* | PDU Header |
* +-------------------------------+ ====
* | Stub Data |
* +-------------------------------+ PDU
* | Stub Padding Octets |
* +-------------------------------+ Body
* | Verification Trailer |
* +-------------------------------+ Here
* | Authentication Padding |
* +-------------------------------+ ====
* | sec_trailer |
* +-------------------------------+
* | Authentication Token |
* +-------------------------------+
*
* We don't use the "Verification Trailer" for anything yet.
* sec_trailer and Authentication Token are for Secure RPC,
* and are collectively the 'auth_verifier_co' in DCERPC.
*
* Each fragment of a multi-fragment response has a unique
* header and, if authentication was requested, a unique
* sec_trailer.
*/
static int
ndr_convert_nds_error(ndr_stream_t *nds)
{
int rc;
switch (nds->error) {
case NDR_ERR_MALLOC_FAILED:
rc = NDR_DRC_FAULT_OUT_OF_MEMORY;
break;
case NDR_ERR_SWITCH_VALUE_INVALID:
rc = NDR_DRC_FAULT_PARAM_0_INVALID;
break;
case NDR_ERR_UNDERFLOW:
rc = NDR_DRC_FAULT_RECEIVED_RUNT;
break;
case NDR_ERR_GROW_FAILED:
rc = NDR_DRC_FAULT_ENCODE_TOO_BIG;
break;
default:
if (nds->m_op == NDR_M_OP_MARSHALL)
rc = NDR_DRC_FAULT_ENCODE_FAILED;
else
rc = NDR_DRC_FAULT_DECODE_FAILED;
break;
}
return (rc);
}
static int
ndr_encode_decode_common(ndr_stream_t *nds, unsigned opnum,
ndr_typeinfo_t *ti, void *datum)
{
/*
* Perform the (un)marshalling
*/
if (ndo_operation(nds, ti, opnum, datum))
return (NDR_DRC_OK);
return (ndr_convert_nds_error(nds));
}
static int
ndr_encode_decode_type(ndr_stream_t *nds, ndr_typeinfo_t *ti, void *datum)
{
/*
* Perform the (un)marshalling
*/
if (ndo_process(nds, ti, datum))
return (NDR_DRC_OK);
return (ndr_convert_nds_error(nds));
}
ndr_buf_t *
ndr_buf_init(ndr_typeinfo_t *ti)
{
ndr_buf_t *nbuf;
if ((nbuf = calloc(1, sizeof (ndr_buf_t))) == NULL)
return (NULL);
if ((nbuf->nb_heap = ndr_heap_create()) == NULL) {
free(nbuf);
return (NULL);
}
nbuf->nb_ti = ti;
nbuf->nb_magic = NDR_BUF_MAGIC;
return (nbuf);
}
void
ndr_buf_fini(ndr_buf_t *nbuf)
{
assert(nbuf->nb_magic == NDR_BUF_MAGIC);
nds_destruct(&nbuf->nb_nds);
ndr_heap_destroy(nbuf->nb_heap);
nbuf->nb_magic = 0;
free(nbuf);
}
/*
* Decode an NDR encoded buffer. The buffer is expected to contain
* a single fragment packet with a valid PDU header followed by NDR
* encoded data. The structure to which result points should be
* of the appropriate type to hold the decoded output. For example:
*
* pac_info_t info;
*
* if ((nbuf = ndr_buf_init(&TYPEINFO(ndr_pac)) != NULL) {
* rc = ndr_decode_buf(nbuf, opnum, data, datalen, &info);
* ...
* ndr_buf_fini(nbuf);
* }
*/
int
ndr_buf_decode(ndr_buf_t *nbuf, unsigned hdr_type, unsigned opnum,
const char *data, size_t datalen, void *result)
{
ndr_common_header_t hdr;
ndr_pac_hdr_t pac_hdr;
unsigned pdu_size_hint;
int rc;
assert(nbuf->nb_magic == NDR_BUF_MAGIC);
assert(nbuf->nb_heap != NULL);
assert(nbuf->nb_ti != NULL);
if (datalen < NDR_PDU_SIZE_HINT_DEFAULT)
pdu_size_hint = NDR_PDU_SIZE_HINT_DEFAULT;
else
pdu_size_hint = datalen;
rc = nds_initialize(&nbuf->nb_nds, pdu_size_hint, NDR_MODE_BUF_DECODE,
nbuf->nb_heap);
if (NDR_DRC_IS_FAULT(rc))
return (rc);
bcopy(data, nbuf->nb_nds.pdu_base_addr, datalen);
nbuf->nb_nds.pdu_size = datalen;
switch (hdr_type) {
case NDR_PTYPE_COMMON:
rc = ndr_decode_hdr_common(&nbuf->nb_nds, &hdr);
if (NDR_DRC_IS_FAULT(rc))
return (rc);
if (!NDR_IS_SINGLE_FRAG(hdr.pfc_flags))
return (NDR_DRC_FAULT_DECODE_FAILED);
break;
case NDR_PTYPE_PAC:
rc = ndr_decode_pac_hdr(&nbuf->nb_nds, &pac_hdr);
if (NDR_DRC_IS_FAULT(rc))
return (rc);
if (pac_hdr.common_hdr.hdrlen != sizeof (ndr_serialtype1_hdr_t))
return (NDR_DRC_FAULT_DECODE_FAILED);
break;
default:
return (NDR_ERR_UNIMPLEMENTED);
}
rc = ndr_encode_decode_common(&nbuf->nb_nds, opnum, nbuf->nb_ti,
result);
return (rc);
}
/*
* Use the receive stream to unmarshall data (NDR_MODE_CALL_RECV).
*/
int
ndr_decode_call(ndr_xa_t *mxa, void *params)
{
ndr_stream_t *nds = &mxa->recv_nds;
int rc;
if (!NDR_MODE_MATCH(nds, NDR_MODE_CALL_RECV))
return (NDR_DRC_FAULT_MODE_MISMATCH);
rc = ndr_encode_decode_common(nds, mxa->opnum,
mxa->binding->service->interface_ti, params);
return (rc + NDR_PTYPE_REQUEST);
}
/*
* Use the send stream to marshall data (NDR_MODE_RETURN_SEND).
*/
int
ndr_encode_return(ndr_xa_t *mxa, void *params)
{
ndr_stream_t *nds = &mxa->send_nds;
int rc;
if (!NDR_MODE_MATCH(nds, NDR_MODE_RETURN_SEND))
return (NDR_DRC_FAULT_MODE_MISMATCH);
rc = ndr_encode_decode_common(nds, mxa->opnum,
mxa->binding->service->interface_ti, params);
return (rc + NDR_PTYPE_RESPONSE);
}
/*
* Use the send stream to marshall data (NDR_MODE_CALL_SEND).
*/
int
ndr_encode_call(ndr_xa_t *mxa, void *params)
{
ndr_stream_t *nds = &mxa->send_nds;
int rc;
if (!NDR_MODE_MATCH(nds, NDR_MODE_CALL_SEND))
return (NDR_DRC_FAULT_MODE_MISMATCH);
rc = ndr_encode_decode_common(nds, mxa->opnum,
mxa->binding->service->interface_ti, params);
return (rc + NDR_PTYPE_REQUEST);
}
/*
* Use the receive stream to unmarshall data (NDR_MODE_RETURN_RECV).
*/
int
ndr_decode_return(ndr_xa_t *mxa, void *params)
{
ndr_stream_t *nds = &mxa->recv_nds;
int rc;
if (!NDR_MODE_MATCH(nds, NDR_MODE_RETURN_RECV))
return (NDR_DRC_FAULT_MODE_MISMATCH);
rc = ndr_encode_decode_common(nds, mxa->opnum,
mxa->binding->service->interface_ti, params);
return (rc + NDR_PTYPE_RESPONSE);
}
int
ndr_decode_pdu_hdr(ndr_xa_t *mxa)
{
ndr_common_header_t *hdr = &mxa->recv_hdr.common_hdr;
ndr_stream_t *nds = &mxa->recv_nds;
int rc;
ulong_t saved_offset;
saved_offset = nds->pdu_scan_offset;
rc = ndr_decode_hdr_common(nds, hdr);
if (NDR_DRC_IS_FAULT(rc))
return (rc);
/*
* Verify the protocol version.
*/
if ((hdr->rpc_vers != 5) || (hdr->rpc_vers_minor != 0))
return (NDR_DRC_FAULT_RPCHDR_DECODE_FAILED);
mxa->ptype = hdr->ptype;
/* pdu_scan_offset now points to (this fragment's) stub data */
nds->pdu_body_offset = nds->pdu_scan_offset;
nds->pdu_hdr_size = nds->pdu_scan_offset - saved_offset;
nds->pdu_body_size = hdr->frag_length - hdr->auth_length -
nds->pdu_hdr_size -
((hdr->auth_length != 0) ? SEC_TRAILER_SIZE : 0);
if (hdr->auth_length != 0 && hdr->auth_length >
(hdr->frag_length - nds->pdu_hdr_size - SEC_TRAILER_SIZE))
return (NDR_DRC_FAULT_RECEIVED_MALFORMED);
return (NDR_DRC_OK);
}
static int
ndr_decode_hdr_common(ndr_stream_t *nds, ndr_common_header_t *hdr)
{
int ptype;
int rc;
int charset;
int byte_order;
ulong_t saved_offset;
if (nds->m_op != NDR_M_OP_UNMARSHALL)
return (NDR_DRC_FAULT_RPCHDR_MODE_MISMATCH);
/*
* All PDU headers are at least this big
*/
saved_offset = nds->pdu_scan_offset;
if ((nds->pdu_size - saved_offset) < sizeof (ndr_common_header_t))
return (NDR_DRC_FAULT_RPCHDR_RECEIVED_RUNT);
/*
* Peek at the first eight bytes to figure out what we're doing.
*/
rc = NDS_GET_PDU(nds, 0, 8, (char *)hdr, 0, 0);
if (!rc)
return (NDR_DRC_FAULT_RPCHDR_DECODE_FAILED);
/*
* Check for ASCII as the character set. This is an ASCII
* versus EBCDIC option and has nothing to do with Unicode.
*/
charset = hdr->packed_drep.intg_char_rep & NDR_REPLAB_CHAR_MASK;
if (charset != NDR_REPLAB_CHAR_ASCII)
return (NDR_DRC_FAULT_RPCHDR_DECODE_FAILED);
/*
* Set the byte swap flag if the PDU byte-order
* is different from the local byte-order.
*/
byte_order = hdr->packed_drep.intg_char_rep & NDR_REPLAB_INTG_MASK;
nds->swap = (byte_order != ndr_native_byte_order) ? 1 : 0;
ptype = hdr->ptype;
if (ptype == NDR_PTYPE_REQUEST &&
(hdr->pfc_flags & NDR_PFC_OBJECT_UUID) != 0) {
ptype = NDR_PTYPE_REQUEST_WITH; /* fake for sizing */
}
rc = ndr_encode_decode_common(nds, ptype, &TYPEINFO(ndr_hdr), hdr);
if (hdr->frag_length > (nds->pdu_size - saved_offset))
rc = NDR_DRC_FAULT_RECEIVED_MALFORMED;
return (NDR_DRC_PTYPE_RPCHDR(rc));
}
static int
ndr_decode_pac_hdr(ndr_stream_t *nds, ndr_pac_hdr_t *hdr)
{
int rc;
if (nds->m_op != NDR_M_OP_UNMARSHALL)
return (NDR_DRC_FAULT_RPCHDR_MODE_MISMATCH);
/*
* All PDU headers are at least this big
*/
if ((nds->pdu_size - nds->pdu_scan_offset) < sizeof (ndr_pac_hdr_t))
return (NDR_DRC_FAULT_RPCHDR_RECEIVED_RUNT);
/*
* Peek at the first eight bytes to figure out what we're doing.
*/
rc = NDS_GET_PDU(nds, 0, 8, (char *)hdr, 0, 0);
if (!rc)
return (NDR_DRC_FAULT_RPCHDR_DECODE_FAILED);
/* Must be set to 1 to indicate type serialization version 1. */
if (hdr->common_hdr.version != 1)
return (NDR_DRC_FAULT_RPCHDR_DECODE_FAILED);
/*
* Set the byte swap flag if the PDU byte-order
* is different from the local byte-order.
*/
nds->swap =
(hdr->common_hdr.endianness != ndr_native_byte_order) ? 1 : 0;
rc = ndr_encode_decode_common(nds, NDR_PTYPE_PAC,
&TYPEINFO(ndr_hdr), hdr);
return (NDR_DRC_PTYPE_RPCHDR(rc));
}
CTASSERT(sizeof (ndr_common_header_t) >= NDR_CMN_HDR_SIZE);
/*
* Decode an RPC fragment header. Use ndr_decode_pdu_hdr() to process
* the first fragment header then this function to process additional
* fragment headers.
*/
void
ndr_decode_frag_hdr(ndr_stream_t *nds, ndr_common_header_t *hdr)
{
ndr_common_header_t *tmp;
uint8_t *pdu;
int byte_order;
pdu = (uint8_t *)nds->pdu_base_offset + nds->pdu_scan_offset;
bcopy(pdu, hdr, NDR_CMN_HDR_SIZE);
/*
* Swap non-byte fields if the PDU byte-order
* is different from the local byte-order.
*/
byte_order = hdr->packed_drep.intg_char_rep & NDR_REPLAB_INTG_MASK;
if (byte_order != ndr_native_byte_order) {
/*LINTED E_BAD_PTR_CAST_ALIGN*/
tmp = (ndr_common_header_t *)pdu;
nds_bswap(&tmp->frag_length, &hdr->frag_length,
sizeof (WORD));
nds_bswap(&tmp->auth_length, &hdr->auth_length,
sizeof (WORD));
nds_bswap(&tmp->call_id, &hdr->call_id, sizeof (DWORD));
}
/* pdu_scan_offset points to byte 0 of this fragment */
nds->pdu_hdr_size = NDR_RSP_HDR_SIZE;
nds->pdu_body_offset = nds->pdu_scan_offset + nds->pdu_hdr_size;
nds->pdu_body_size = hdr->frag_length - hdr->auth_length -
nds->pdu_hdr_size -
((hdr->auth_length != 0) ? SEC_TRAILER_SIZE : 0);
}
/*
* Remove an RPC fragment header from the received data stream.
*
* NDR stream on entry:
*
* |<--- frag --->|
* +-----+--------+-----+--------+-----+---------+-----+
* | hdr | data | hdr | data | hdr | data | ... |
* +-----+--------+-----+--------+-----+---------+-----+
* <----
*
* NDR stream on return:
*
* +-----+----------------+-----+---------+-----+
* | hdr | data | hdr | data | ... |
* +-----+----------------+-----+---------+-----+
*/
void
ndr_remove_frag_hdr(ndr_stream_t *nds)
{
char *hdr;
char *data;
int nbytes;
hdr = (char *)nds->pdu_base_offset + nds->pdu_scan_offset;
data = hdr + NDR_RSP_HDR_SIZE;
nbytes = nds->pdu_size - nds->pdu_scan_offset - NDR_RSP_HDR_SIZE;
/*
* Move all of the data after the header back to where the header began.
*/
memmove(hdr, data, nbytes);
nds->pdu_size -= NDR_RSP_HDR_SIZE;
}
void
ndr_show_hdr(ndr_common_header_t *hdr)
{
char *fragtype;
if (hdr == NULL) {
ndo_printf(NULL, NULL, "ndr hdr: <null>");
return;
}
if (NDR_IS_SINGLE_FRAG(hdr->pfc_flags))
fragtype = "single";
else if (NDR_IS_FIRST_FRAG(hdr->pfc_flags))
fragtype = "first";
else if (NDR_IS_LAST_FRAG(hdr->pfc_flags))
fragtype = "last";
else
fragtype = "intermediate";
ndo_printf(NULL, NULL,
"ndr hdr: %d.%d ptype=%d, %s frag (flags=0x%08x) len=%d "
"auth_len=%d",
hdr->rpc_vers, hdr->rpc_vers_minor, hdr->ptype,
fragtype, hdr->pfc_flags, hdr->frag_length, hdr->auth_length);
}
void
ndr_show_auth(ndr_sec_t *auth)
{
if (auth == NULL) {
ndo_printf(NULL, NULL, "ndr auth: <null>");
return;
}
ndo_printf(NULL, NULL,
"ndr auth: type=0x%x, level=0x%x, pad_len=%d, ctx_id=%d",
auth->auth_type, auth->auth_level, auth->auth_pad_len,
auth->auth_context_id);
}
int
ndr_encode_pdu_hdr(ndr_xa_t *mxa)
{
ndr_common_header_t *hdr = &mxa->send_hdr.common_hdr;
ndr_stream_t *nds = &mxa->send_nds;
int ptype;
int rc;
if (nds->m_op != NDR_M_OP_MARSHALL)
return (NDR_DRC_FAULT_RPCHDR_MODE_MISMATCH);
ptype = hdr->ptype;
if (ptype == NDR_PTYPE_REQUEST &&
(hdr->pfc_flags & NDR_PFC_OBJECT_UUID) != 0) {
ptype = NDR_PTYPE_REQUEST_WITH; /* fake for sizing */
}
rc = ndr_encode_decode_common(nds, ptype, &TYPEINFO(ndr_hdr), hdr);
return (NDR_DRC_PTYPE_RPCHDR(rc));
}
/*
* This is a hand-coded derivative of the automatically generated
* (un)marshalling routine for bind_ack headers. bind_ack headers
* have an interior conformant array, which is inconsistent with
* IDL/NDR rules.
*/
extern struct ndr_typeinfo ndt__uchar;
extern struct ndr_typeinfo ndt__ushort;
extern struct ndr_typeinfo ndt__ulong;
int ndr__ndr_bind_ack_hdr(ndr_ref_t *encl_ref);
ndr_typeinfo_t ndt__ndr_bind_ack_hdr = {
1, /* NDR version */
3, /* alignment */
NDR_F_STRUCT, /* flags */
ndr__ndr_bind_ack_hdr, /* ndr_func */
68, /* pdu_size_fixed_part */
0, /* pdu_size_variable_part */
68, /* c_size_fixed_part */
0, /* c_size_variable_part */
};
/*
* [_no_reorder]
*/
int
ndr__ndr_bind_ack_hdr(ndr_ref_t *encl_ref)
{
ndr_stream_t *nds = encl_ref->stream;
struct ndr_bind_ack_hdr *val = /*LINTED E_BAD_PTR_CAST_ALIGN*/
(struct ndr_bind_ack_hdr *)encl_ref->datum;
ndr_ref_t myref;
unsigned long offset;
bzero(&myref, sizeof (myref));
myref.enclosing = encl_ref;
myref.stream = encl_ref->stream;
myref.packed_alignment = 0;
/* do all members in order */
NDR_MEMBER(_ndr_common_header, common_hdr, 0UL);
NDR_MEMBER(_ushort, max_xmit_frag, 16UL);
NDR_MEMBER(_ushort, max_recv_frag, 18UL);
NDR_MEMBER(_ulong, assoc_group_id, 20UL);
/* port any is the conformant culprit */
offset = 24UL;
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
val->sec_addr.length =
strlen((char *)val->sec_addr.port_spec) + 1;
break;
case NDR_M_OP_UNMARSHALL:
break;
default:
NDR_SET_ERROR(encl_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
NDR_MEMBER(_ushort, sec_addr.length, offset);
NDR_MEMBER_ARR_WITH_DIMENSION(_uchar, sec_addr.port_spec,
offset+2UL, val->sec_addr.length);
offset += 2;
offset += val->sec_addr.length;
offset += NDR_ALIGN4(offset);
NDR_MEMBER(_ndr_p_result_list, p_result_list, offset);
return (1);
}
/*
* Assume a single presentation context element in the result list.
*/
unsigned
ndr_bind_ack_hdr_size(ndr_xa_t *mxa)
{
ndr_bind_ack_hdr_t *bahdr = &mxa->send_hdr.bind_ack_hdr;
unsigned offset;
unsigned length;
/* port any is the conformant culprit */
offset = 24UL;
length = strlen((char *)bahdr->sec_addr.port_spec) + 1;
offset += 2;
offset += length;
offset += NDR_ALIGN4(offset);
offset += sizeof (ndr_p_result_list_t);
return (offset);
}
/*
* This is a hand-coded derivative of the automatically generated
* (un)marshalling routine for alter_context_rsp headers.
* Alter context response headers have an interior conformant array,
* which is inconsistent with IDL/NDR rules.
*/
int ndr__ndr_alter_context_rsp_hdr(ndr_ref_t *encl_ref);
ndr_typeinfo_t ndt__ndr_alter_context_rsp_hdr = {
1, /* NDR version */
3, /* alignment */
NDR_F_STRUCT, /* flags */
ndr__ndr_alter_context_rsp_hdr, /* ndr_func */
56, /* pdu_size_fixed_part */
0, /* pdu_size_variable_part */
56, /* c_size_fixed_part */
0, /* c_size_variable_part */
};
/*
* [_no_reorder]
*/
int
ndr__ndr_alter_context_rsp_hdr(ndr_ref_t *encl_ref)
{
ndr_stream_t *nds = encl_ref->stream;
ndr_alter_context_rsp_hdr_t *val = /*LINTED E_BAD_PTR_CAST_ALIGN*/
(ndr_alter_context_rsp_hdr_t *)encl_ref->datum;
ndr_ref_t myref;
unsigned long offset;
bzero(&myref, sizeof (myref));
myref.enclosing = encl_ref;
myref.stream = encl_ref->stream;
myref.packed_alignment = 0;
/* do all members in order */
NDR_MEMBER(_ndr_common_header, common_hdr, 0UL);
NDR_MEMBER(_ushort, max_xmit_frag, 16UL);
NDR_MEMBER(_ushort, max_recv_frag, 18UL);
NDR_MEMBER(_ulong, assoc_group_id, 20UL);
offset = 24UL; /* offset of sec_addr */
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
val->sec_addr.length = 0;
break;
case NDR_M_OP_UNMARSHALL:
break;
default:
NDR_SET_ERROR(encl_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
NDR_MEMBER(_ushort, sec_addr.length, offset);
NDR_MEMBER_ARR_WITH_DIMENSION(_uchar, sec_addr.port_spec,
offset+2UL, val->sec_addr.length);
offset += 2; /* sizeof (sec_addr.length) */
offset += NDR_ALIGN4(offset);
NDR_MEMBER(_ndr_p_result_list, p_result_list, offset);
return (1);
}
/*
* Assume a single presentation context element in the result list.
*/
unsigned
ndr_alter_context_rsp_hdr_size(void)
{
unsigned offset;
offset = 24UL; /* offset of sec_addr */
offset += 2; /* sizeof (sec_addr.length) */
offset += NDR_ALIGN4(offset);
offset += sizeof (ndr_p_result_list_t);
return (offset);
}
/*
* This is a hand-coded (un)marshalling routine for auth_verifier_co
* (aka ndr_sec_t).
*
* We need to pretend this structure isn't variably sized, until ndrgen
* has been modified to support variable-sized arrays.
* Here, we only account for the fixed-size members (8 bytes), plus
* a pointer for the C structure.
*
* We then convert between a pointer to the auth token (auth_value,
* allocated here during unmarshall) and a flat, 'fixed'-sized array.
*/
int ndr__auth_verifier_co(ndr_ref_t *encl_ref);
ndr_typeinfo_t ndt__auth_verifier_co = {
1, /* NDR version */
3, /* alignment */
NDR_F_STRUCT, /* flags */
ndr__auth_verifier_co, /* ndr_func */
8, /* pdu_size_fixed_part */
0, /* pdu_size_variable_part */
8 + sizeof (void *), /* c_size_fixed_part */
0, /* c_size_variable_part */
};
/*
* [_no_reorder]
*/
int
ndr__auth_verifier_co(ndr_ref_t *encl_ref)
{
ndr_stream_t *nds = encl_ref->stream;
ndr_xa_t *mxa = /*LINTED E_BAD_PTR_CAST_ALIGN*/
(ndr_xa_t *)encl_ref->datum;
ndr_common_header_t *hdr;
ndr_ref_t myref;
ndr_sec_t *val;
/*
* Assumes scan_offset points to the end of PDU body.
* (That's base + frag_len - auth_len - SEC_TRAILER_SIZE)
*
* At some point, NDRGEN could use struct initializers instead of
* bzero() + initialization.
*/
bzero(&myref, sizeof (myref));
myref.enclosing = encl_ref;
myref.stream = encl_ref->stream;
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
val = &mxa->send_auth;
hdr = &mxa->send_hdr.common_hdr;
break;
case NDR_M_OP_UNMARSHALL:
val = &mxa->recv_auth;
hdr = &mxa->recv_hdr.common_hdr;
val->auth_value = (uchar_t *)NDS_MALLOC(nds, hdr->auth_length,
encl_ref);
break;
default:
NDR_SET_ERROR(encl_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
/*
* ndr_topmost() can't account for auth_length (pdu_scan/end_offset).
* This would only matter if any of this struct's members
* are treated as 'outer' constructs, but they aren't.
*/
encl_ref->pdu_end_offset += hdr->auth_length;
nds->pdu_scan_offset += hdr->auth_length;
NDR_MEMBER(_uchar, auth_type, 0UL);
NDR_MEMBER(_uchar, auth_level, 1UL);
NDR_MEMBER(_uchar, auth_pad_len, 2UL);
NDR_MEMBER(_uchar, auth_rsvd, 3UL);
NDR_MEMBER(_ulong, auth_context_id, 4UL);
NDR_MEMBER_PTR_WITH_DIMENSION(_uchar, auth_value, 8UL,
hdr->auth_length);
return (1);
}
int
ndr_encode_pdu_auth(ndr_xa_t *mxa)
{
ndr_common_header_t *hdr = &mxa->send_hdr.common_hdr;
ndr_stream_t *nds = &mxa->send_nds;
int rc;
ulong_t want_size;
if (nds->m_op != NDR_M_OP_MARSHALL)
return (NDR_DRC_FAULT_MODE_MISMATCH);
if (hdr->auth_length == 0)
return (NDR_DRC_OK);
want_size = nds->pdu_scan_offset + hdr->auth_length + SEC_TRAILER_SIZE;
/*
* Make sure we have space for the sec trailer - the marshaller
* doesn't know how large the auth token is.
* Note: ndr_add_auth_token() has already added padding.
*
* NDS_GROW_PDU will adjust pdu_size for us.
*/
if (nds->pdu_max_size < want_size) {
if (NDS_GROW_PDU(nds, want_size, NULL) == 0)
return (NDR_DRC_FAULT_ENCODE_TOO_BIG);
} else {
nds->pdu_size = want_size;
}
rc = ndr_encode_decode_type(nds, &TYPEINFO(auth_verifier_co),
mxa);
return (rc);
}
int
ndr_decode_pdu_auth(ndr_xa_t *mxa)
{
ndr_common_header_t *hdr = &mxa->recv_hdr.common_hdr;
ndr_stream_t *nds = &mxa->recv_nds;
ndr_sec_t *auth = &mxa->recv_auth;
int rc;
ulong_t saved_offset;
size_t auth_size;
if (nds->m_op != NDR_M_OP_UNMARSHALL)
return (NDR_DRC_FAULT_MODE_MISMATCH);
mxa->recv_auth.auth_pad_len = 0;
if (hdr->auth_length == 0)
return (NDR_DRC_OK);
/*
* Save the current offset, and skip to the sec_trailer.
* That's located after the (fragment of) stub data and the auth
* pad bytes (collectively the 'PDU Body').
*/
saved_offset = nds->pdu_scan_offset;
nds->pdu_scan_offset = nds->pdu_body_offset + nds->pdu_body_size;
/* auth_length is all of the data after the sec_trailer */
if (hdr->auth_length >
(nds->pdu_size - nds->pdu_scan_offset - SEC_TRAILER_SIZE)) {
nds->pdu_scan_offset = saved_offset;
return (NDR_DRC_FAULT_RECEIVED_MALFORMED);
}
rc = ndr_encode_decode_type(nds, &TYPEINFO(auth_verifier_co),
mxa);
/*
* Reset the scan_offset for call decode processing.
* If we were successful, remove the sec trailer and padding
* from size accounting.
*/
if (auth->auth_pad_len > nds->pdu_body_size)
rc = NDR_DRC_FAULT_RECEIVED_MALFORMED;
else if (rc == NDR_DRC_OK) {
auth_size = hdr->auth_length + SEC_TRAILER_SIZE +
auth->auth_pad_len;
/*
* After the authenticator has been decoded,
* pdu_scan_offset points to just after the auth token,
* which is the end of the fragment.
*
* If there's no data after the authenticator, then we
* just remove the authenticator from size accounting.
* Otherwise, need to memmove() all of that data back to after
* the stub data. The data we move starts at the beginning of
* the next fragment.
*/
if (nds->pdu_size > nds->pdu_scan_offset) {
uchar_t *next_frag_ptr = nds->pdu_base_addr +
nds->pdu_scan_offset;
memmove(next_frag_ptr - auth_size, next_frag_ptr,
nds->pdu_size - nds->pdu_scan_offset);
}
nds->pdu_size -= auth_size;
}
nds->pdu_scan_offset = saved_offset;
return (rc);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2020 Tintri by DDN, Inc. All rights reserved.
*/
/*
* Server-side NDR stream (PDU) operations. Stream operations should
* return TRUE (non-zero) on success or FALSE (zero or a null pointer)
* on failure. When an operation returns FALSE, including ndo_malloc()
* returning NULL, it should set the nds->error to indicate what went
* wrong.
*
* When available, the relevant ndr reference is passed to the
* operation but keep in mind that it may be a null pointer.
*
* Functions ndo_get_pdu(), ndo_put_pdu(), and ndo_pad_pdu()
* must never grow the PDU data. A request for out-of-bounds data is
* an error. The swap_bytes flag is 1 if NDR knows that the byte-
* order in the PDU is different from the local system.
*/
#include <sys/types.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <assert.h>
#include <libmlrpc.h>
#define NDOBUFSZ 128
#define NDR_PDU_BLOCK_SIZE (4*1024)
#define NDR_PDU_BLOCK_MASK (NDR_PDU_BLOCK_SIZE - 1)
#define NDR_PDU_ALIGN(N) \
(((N) + NDR_PDU_BLOCK_SIZE) & ~NDR_PDU_BLOCK_MASK)
#define NDR_PDU_MAX_SIZE (64*1024*1024)
static char *ndo_malloc(ndr_stream_t *, unsigned, ndr_ref_t *);
static int ndo_free(ndr_stream_t *, char *, ndr_ref_t *);
static int ndo_grow_pdu(ndr_stream_t *, unsigned long, ndr_ref_t *);
static int ndo_pad_pdu(ndr_stream_t *, unsigned long, unsigned long,
ndr_ref_t *);
static int ndo_get_pdu(ndr_stream_t *, unsigned long, unsigned long,
char *, int, ndr_ref_t *);
static int ndo_put_pdu(ndr_stream_t *, unsigned long, unsigned long,
char *, int, ndr_ref_t *);
static void ndo_tattle(ndr_stream_t *, char *, ndr_ref_t *);
static void ndo_tattle_error(ndr_stream_t *, ndr_ref_t *);
static int ndo_reset(ndr_stream_t *);
static void ndo_destruct(ndr_stream_t *);
static void ndo_hexfmt(uint8_t *, int, int, char *, int);
/*
* The ndr stream operations table.
*/
static ndr_stream_ops_t nds_ops = {
ndo_malloc,
ndo_free,
ndo_grow_pdu,
ndo_pad_pdu,
ndo_get_pdu,
ndo_put_pdu,
ndo_tattle,
ndo_tattle_error,
ndo_reset,
ndo_destruct
};
/*
* nds_bswap
*
* Copies len bytes from src to dst such that dst contains the bytes
* from src in reverse order.
*
* We expect to be dealing with bytes, words, dwords etc. So the
* length must be non-zero and a power of 2.
*/
void
nds_bswap(void *srcbuf, void *dstbuf, size_t len)
{
uint8_t *src = (uint8_t *)srcbuf;
uint8_t *dst = (uint8_t *)dstbuf;
if ((len != 0) && ((len & (len - 1)) == 0)) {
src += len;
while (len--)
*dst++ = *(--src);
}
}
/*
* nds_initialize
*
* Initialize a stream. Sets up the PDU parameters and assigns the stream
* operations and the reference to the heap. An external heap is provided
* to the stream, rather than each stream creating its own heap.
*/
int
nds_initialize(ndr_stream_t *nds, unsigned pdu_size_hint,
int composite_op, ndr_heap_t *heap)
{
unsigned size;
assert(nds);
assert(heap);
bzero(nds, sizeof (*nds));
nds->ndo = &nds_ops;
nds->heap = (struct ndr_heap *)heap;
if (pdu_size_hint > NDR_PDU_MAX_SIZE) {
nds->error = NDR_ERR_BOUNDS_CHECK;
nds->error_ref = __LINE__;
NDS_TATTLE_ERROR(nds, NULL, NULL);
return (NDR_DRC_FAULT_RESOURCE_1);
}
size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint;
if ((nds->pdu_base_addr = malloc(size)) == NULL) {
nds->error = NDR_ERR_MALLOC_FAILED;
nds->error_ref = __LINE__;
NDS_TATTLE_ERROR(nds, NULL, NULL);
return (NDR_DRC_FAULT_OUT_OF_MEMORY);
}
nds->pdu_max_size = size;
nds->pdu_size = 0;
nds->pdu_base_offset = (unsigned long)nds->pdu_base_addr;
nds->m_op = NDR_MODE_TO_M_OP(composite_op);
nds->dir = NDR_MODE_TO_DIR(composite_op);
nds->outer_queue_tailp = &nds->outer_queue_head;
return (0);
}
/*
* nds_destruct
*
* Destroy a stream. This is an external interface to provide access to
* the stream's destruct operation.
*/
void
nds_destruct(ndr_stream_t *nds)
{
if ((nds == NULL) || (nds->ndo == NULL))
return;
NDS_DESTRUCT(nds);
}
/*
* Print NDR stream state.
*/
void
nds_show_state(ndr_stream_t *nds)
{
if (nds == NULL) {
ndo_printf(NULL, NULL, "nds: <null");
return;
}
ndo_printf(NULL, NULL, "nds: base=0x%x, size=%d, max=%d, scan=%d, "
"hdr_size=%d, body_size=%d, body_offset=%d",
nds->pdu_base_offset, nds->pdu_size, nds->pdu_max_size,
nds->pdu_scan_offset, nds->pdu_hdr_size, nds->pdu_body_size,
nds->pdu_body_offset);
}
/*
* ndo_malloc
*
* Allocate memory from the stream heap.
*/
/*ARGSUSED*/
static char *
ndo_malloc(ndr_stream_t *nds, unsigned len, ndr_ref_t *ref)
{
return (ndr_heap_malloc((ndr_heap_t *)nds->heap, len));
}
/*
* ndo_free
*
* Always succeeds: cannot free individual stream allocations.
*/
/*ARGSUSED*/
static int
ndo_free(ndr_stream_t *nds, char *p, ndr_ref_t *ref)
{
return (1);
}
/*
* ndo_grow_pdu
*
* This is the only place that should change the size of the PDU. If the
* desired offset is beyond the current PDU size, we realloc the PDU
* buffer to accommodate the request. For efficiency, the PDU is always
* extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU
* beyond NDR_PDU_MAX_SIZE are rejected.
*
* Returns 1 to indicate success. Otherwise 0 to indicate failure.
*/
static int
ndo_grow_pdu(ndr_stream_t *nds, unsigned long want_end_offset, ndr_ref_t *ref)
{
unsigned char *pdu_addr;
unsigned pdu_max_size;
ndo_printf(nds, ref, "grow %d", want_end_offset);
pdu_max_size = nds->pdu_max_size;
if (want_end_offset > pdu_max_size) {
pdu_max_size = NDR_PDU_ALIGN(want_end_offset);
if (pdu_max_size >= NDR_PDU_MAX_SIZE)
return (0);
pdu_addr = realloc(nds->pdu_base_addr, pdu_max_size);
if (pdu_addr == 0)
return (0);
nds->pdu_max_size = pdu_max_size;
nds->pdu_base_addr = pdu_addr;
nds->pdu_base_offset = (unsigned long)pdu_addr;
}
nds->pdu_size = want_end_offset;
return (1);
}
static int
ndo_pad_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
unsigned long n_bytes, ndr_ref_t *ref)
{
unsigned char *data;
data = (unsigned char *)nds->pdu_base_offset;
data += pdu_offset;
ndo_printf(nds, ref, "pad %d@%-3d", n_bytes, pdu_offset);
bzero(data, n_bytes);
return (1);
}
/*
* ndo_get_pdu
*
* The swap flag is 1 if NDR knows that the byte-order in the PDU
* is different from the local system.
*
* Returns 1 on success or 0 to indicate failure.
*/
static int
ndo_get_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
{
unsigned char *data;
char hexbuf[NDOBUFSZ];
data = (unsigned char *)nds->pdu_base_offset;
data += pdu_offset;
ndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ);
ndo_printf(nds, ref, "get %d@%-3d = %s",
n_bytes, pdu_offset, hexbuf);
if (!swap_bytes)
bcopy(data, buf, n_bytes);
else
nds_bswap(data, (unsigned char *)buf, n_bytes);
return (1);
}
/*
* ndo_put_pdu
*
* This is a receiver makes right protocol. So we do not need
* to be concerned about the byte-order of an outgoing PDU.
*/
/*ARGSUSED*/
static int
ndo_put_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
{
unsigned char *data;
char hexbuf[NDOBUFSZ];
data = (unsigned char *)nds->pdu_base_offset;
data += pdu_offset;
ndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ);
ndo_printf(nds, ref, "put %d@%-3d = %s",
n_bytes, pdu_offset, hexbuf);
bcopy(buf, data, n_bytes);
return (1);
}
static void
ndo_tattle(ndr_stream_t *nds, char *what, ndr_ref_t *ref)
{
ndo_printf(nds, ref, what);
}
static void
ndo_tattle_error(ndr_stream_t *nds, ndr_ref_t *ref)
{
unsigned char *data;
char hexbuf[NDOBUFSZ];
if (nds->pdu_base_addr != NULL) {
data = (unsigned char *)nds->pdu_base_offset;
if (ref)
data += ref->pdu_offset;
else
data += nds->pdu_scan_offset;
ndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ);
} else {
bzero(hexbuf, NDOBUFSZ);
}
ndo_printf(nds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d",
nds->error, nds->error_ref, nds->pdu_scan_offset,
nds->pdu_size, nds->pdu_max_size);
ndo_printf(nds, ref, " %s", hexbuf);
}
/*
* ndo_reset
*
* Reset a stream: zap the outer_queue. We don't need to tamper
* with the stream heap: it's handled externally to the stream.
*/
static int
ndo_reset(ndr_stream_t *nds)
{
ndo_printf(nds, 0, "reset");
nds->pdu_size = 0;
nds->pdu_scan_offset = 0;
nds->outer_queue_head = 0;
nds->outer_current = 0;
nds->outer_queue_tailp = &nds->outer_queue_head;
return (1);
}
/*
* ndo_destruct
*
* Destruct a stream: zap the outer_queue.
* Note: heap management (creation/destruction) is external to the stream.
*/
static void
ndo_destruct(ndr_stream_t *nds)
{
ndo_printf(nds, 0, "destruct");
if (nds == NULL)
return;
if (nds->pdu_base_addr != NULL) {
free(nds->pdu_base_addr);
nds->pdu_base_addr = NULL;
nds->pdu_base_offset = 0;
}
nds->outer_queue_head = 0;
nds->outer_current = 0;
nds->outer_queue_tailp = &nds->outer_queue_head;
}
/*
* Printf style formatting for NDR operations.
*/
void
ndo_printf(ndr_stream_t *nds, ndr_ref_t *ref, const char *fmt, ...)
{
va_list ap;
char buf[NDOBUFSZ];
va_start(ap, fmt);
(void) vsnprintf(buf, NDOBUFSZ, fmt, ap);
va_end(ap);
if (nds)
ndo_fmt(nds, ref, buf);
else
ndo_trace(buf);
}
/*
* Main output formatter for NDR operations.
*
* UI 03 ... rpc_vers get 1@0 = 5 {05}
* UI 03 ... rpc_vers_minor get 1@1 = 0 {00}
*
* U Marshalling flag (M=marshal, U=unmarshal)
* I Direction flag (I=in, O=out)
* ... Field name
* get PDU operation (get or put)
* 1@0 Bytes @ offset (i.e. 1 byte at offset 0)
* {05} Value
*/
void
ndo_fmt(ndr_stream_t *nds, ndr_ref_t *ref, char *note)
{
ndr_ref_t *p;
int indent;
char ref_name[NDOBUFSZ];
char buf[NDOBUFSZ];
int m_op_c = '?', dir_c = '?';
switch (nds->m_op) {
case 0: m_op_c = '-'; break;
case NDR_M_OP_MARSHALL: m_op_c = 'M'; break;
case NDR_M_OP_UNMARSHALL: m_op_c = 'U'; break;
default: m_op_c = '?'; break;
}
switch (nds->dir) {
case 0: dir_c = '-'; break;
case NDR_DIR_IN: dir_c = 'I'; break;
case NDR_DIR_OUT: dir_c = 'O'; break;
default: dir_c = '?'; break;
}
for (indent = 0, p = ref; p; p = p->enclosing)
indent++;
if (ref && ref->name) {
if (*ref->name == '[' && ref->enclosing) {
indent--;
(void) snprintf(ref_name, NDOBUFSZ, "%s%s",
ref->enclosing->name, ref->name);
} else {
(void) strlcpy(ref_name, ref->name, NDOBUFSZ);
}
} else {
(void) strlcpy(ref_name, "----", NDOBUFSZ);
}
(void) snprintf(buf, NDOBUFSZ, "%c%c %-.*s %-*s %s",
m_op_c, dir_c, indent,
"....+....+....+....+....+....",
20 - indent, ref_name, note);
ndo_trace(buf);
}
/*ARGSUSED*/
void
ndo_trace(const char *s)
{
/*
* Temporary fbt for dtrace until user space sdt enabled.
*/
}
/*
* Format data as hex bytes (limit is 10 bytes):
*
* 1188689424 {10 f6 d9 46}
*
* If the input data is greater than 10 bytes, an ellipsis will
* be inserted before the closing brace.
*/
static void
ndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len)
{
char *p = buf;
int interp = 1;
uint32_t c;
int n;
int i;
n = (size > 10) ? 10 : size;
if (n > len-1)
n = len-1;
switch (size) {
case 1:
c = *(uint8_t *)data;
break;
case 2:
if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/
c = *(uint16_t *)data;
else
c = (data[0] << 8) | data[1];
break;
case 4:
if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/
c = *(uint32_t *)data;
} else {
c = (data[0] << 24) | (data[1] << 16)
| (data[2] << 8) | data[3];
}
break;
default:
c = 0;
interp = 0;
break;
}
if (interp)
p += sprintf(p, "%4u {", c);
else
p += sprintf(p, " {");
p += sprintf(p, "%02x", data[0]);
for (i = 1; i < n; i++)
p += sprintf(p, " %02x", data[i]);
if (size > 10)
p += sprintf(p, " ...}");
else
p += sprintf(p, "}");
/*
* Show c if it's a printable character or wide-char.
*/
if (size < 4 && isprint((uint8_t)c))
(void) sprintf(p, " %c", (uint8_t)c);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2012 Milan Jurik. All rights reserved.
* Copyright 2020 Tintri by DDN, Inc. All rights reserved.
*/
/*
* Network Data Representation (NDR) is a compatible subset of the DCE RPC
* and MSRPC NDR. NDR is used to move parameters consisting of
* complicated trees of data constructs between an RPC client and server.
*/
#include <sys/byteorder.h>
#include <strings.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libmlrpc.h>
#include <ndr_wchar.h>
#define NDR_IS_UNION(T) \
(((T)->type_flags & NDR_F_TYPEOP_MASK) == NDR_F_UNION)
#define NDR_IS_STRING(T) \
(((T)->type_flags & NDR_F_TYPEOP_MASK) == NDR_F_STRING)
extern ndr_typeinfo_t ndt_s_wchar;
/*
* The following synopsis describes the terms TOP-MOST, OUTER and INNER.
*
* Each parameter (call arguments and return values) is a TOP-MOST item.
* A TOP-MOST item consists of one or more OUTER items. An OUTER item
* consists of one or more INNER items. There are important differences
* between each kind, which, primarily, have to do with the allocation
* of memory to contain data structures and the order of processing.
*
* This is most easily demonstrated with a short example.
* Consider these structures:
*
* struct top_param {
* long level;
* struct list * head;
* long count;
* };
*
* struct list {
* struct list * next;
* char * str; // a string
* };
*
* Now, consider an instance tree like this:
*
* +---------+ +-------+ +-------+
* |top_param| +--->|list #1| +--->|list #2|
* +---------+ | +-------+ | +-------+
* | level | | | next ----+ | next --->(NULL)
* | head ----+ | str -->"foo" | str -->"bar"
* | count | | flag | | flag |
* +---------+ +-------+ +-------+
*
* The DCE(MS)/RPC Stub Data encoding for the tree is the following.
* The vertical bars (|) indicate OUTER construct boundaries.
*
* +-----+----------------------+----------------------+-----+-----+-----+
* |level|#1.next #1.str #1.flag|#2.next #2.str #2.flag|"bar"|"foo"|count|
* +-----+----------------------+----------------------+-----+-----+-----+
* level |<----------------------- head -------------------------->|count
* TOP TOP TOP
*
* Here's what to notice:
*
* - The members of the TOP-MOST construct are scattered through the Stub
* Data in the order they occur. This example shows a TOP-MOST construct
* consisting of atomic types (pointers and integers). A construct
* (struct) within the TOP-MOST construct would be contiguous and not
* scattered.
*
* - The members of OUTER constructs are contiguous, which allows for
* non-copied relocated (fixed-up) data structures at the packet's
* destination. We don't do fix-ups here. The pointers within the
* OUTER constructs are processed depth-first in the order that they
* occur. If they were processed breadth first, the sequence would
* be #1,"foo",#2,"bar". This is tricky because OUTER constructs may
* be variable length, and pointers are often encountered before the
* size(s) is known.
*
* - The INNER constructs are simply the members of an OUTER construct.
*
* For comparison, consider how ONC RPC would handle the same tree of
* data. ONC requires very little buffering, while DCE requires enough
* buffer space for the entire message. ONC does atom-by-atom depth-first
* (de)serialization and copy, while DCE allows for constructs to be
* "fixed-up" (relocated) in place at the destination. The packet data
* for the same tree processed by ONC RPC would look like this:
*
* +---------------------------------------------------------------------+
* |level #1.next #2.next #2.str "bar" #2.flag #1.str "foo" #1.flag count|
* +---------------------------------------------------------------------+
* TOP #1 #2 #2 bar #2 #1 foo #1 TOP
*
* More details about each TOP-MOST, OUTER, and INNER constructs appear
* throughout this source file near where such constructs are processed.
*
* NDR_REFERENCE
*
* The primary object for NDR is the ndr_ref_t.
*
* An ndr reference indicates the local datum (i.e. native "C" data
* format), and the element within the Stub Data (contained within the
* RPC PDU (protocol data unit). An ndr reference also indicates,
* largely as a debugging aid, something about the type of the
* element/datum, and the enclosing construct for the element. The
* ndr reference's are typically allocated on the stack as locals,
* and the chain of ndr-reference.enclosing references is in reverse
* order of the call graph.
*
* The ndr-reference.datum is a pointer to the local memory that
* contains/receives the value. The ndr-reference.pdu_offset indicates
* where in the Stub Data the value is to be stored/retrieved.
*
* The ndr-reference also contains various parameters to the NDR
* process, such as ndr-reference.size_is, which indicates the size
* of variable length data, or ndr-reference.switch_is, which
* indicates the arm of a union to use.
*
* QUEUE OF OUTER REFERENCES
*
* Some OUTER constructs are variable size. Sometimes (often) we don't
* know the size of the OUTER construct until after pointers have been
* encountered. Hence, we can not begin processing the referent of the
* pointer until after the referring OUTER construct is completely
* processed, i.e. we don't know where to find/put the referent in the
* Stub Data until we know the size of all its predecessors.
*
* This is managed using the queue of OUTER references. The queue is
* anchored in ndr_stream.outer_queue_head. At any time,
* ndr_stream.outer_queue_tailp indicates where to put the
* ndr-reference for the next encountered pointer.
*
* Refer to the example above as we illustrate the queue here. In these
* illustrations, the queue entries are not the data structures themselves.
* Rather, they are ndr-reference entries which **refer** to the data
* structures in both the PDU and local memory.
*
* During some point in the processing, the queue looks like this:
*
* outer_current -------v
* outer_queue_head --> list#1 --0
* outer_queue_tailp ---------&
*
* When the pointer #1.next is encountered, and entry is added to the
* queue,
*
* outer_current -------v
* outer_queue_head --> list#1 --> list#2 --0
* outer_queue_tailp --------------------&
*
* and the members of #1 continue to be processed, which encounters
* #1.str:
*
* outer_current -------v
* outer_queue_head --> list#1 --> list#2 --> "foo" --0
* outer_queue_tailp ------------------------------&
*
* Upon the completion of list#1, the processing continues by moving to
* ndr_stream.outer_current->next, and the tail is set to this outer member:
*
* outer_current ------------------v
* outer_queue_head --> list#1 --> list#2 --> "foo" --0
* outer_queue_tailp --------------------&
*
* Space for list#2 is allocated, either in the Stub Data or of local
* memory. When #2.next is encountered, it is found to be the null
* pointer and no reference is added to the queue. When #2.str is
* encountered, it is found to be valid, and a reference is added:
*
* outer_current ------------------v
* outer_queue_head --> list#1 --> list#2 --> "bar" --> "foo" --0
* outer_queue_tailp ------------------------------&
*
* Processing continues in a similar fashion with the string "bar",
* which is variable-length. At this point, memory for "bar" may be
* malloc()ed during NDR_M_OP_UNMARSHALL:
*
* outer_current -----------------------------v
* outer_queue_head --> list#1 --> list#2 --> "bar" --> "foo" --0
* outer_queue_tailp ------------------------------&
*
* And finishes on string "foo". Notice that because "bar" is a
* variable length string, and we don't know the PDU offset for "foo"
* until we reach this point.
*
* When the queue is drained (current->next==0), processing continues
* with the next TOP-MOST member.
*
* The queue of OUTER constructs manages the variable-length semantics
* of OUTER constructs and satisfies the depth-first requirement.
* We allow the queue to linger until the entire TOP-MOST structure is
* processed as an aid to debugging.
*/
static ndr_ref_t *ndr_enter_outer_queue(ndr_ref_t *);
extern int ndr__ulong(ndr_ref_t *);
/*
* TOP-MOST ELEMENTS
*
* This is fundamentally the first OUTER construct of the parameter,
* possibly followed by more OUTER constructs due to pointers. The
* datum (local memory) for TOP-MOST constructs (structs) is allocated
* by the caller of NDR.
*
* After the element is transferred, the outer_queue is drained.
*
* All we have to do is add an entry to the outer_queue for this
* top-most member, and commence the outer_queue processing.
*/
int
ndo_process(ndr_stream_t *nds, ndr_typeinfo_t *ti, char *datum)
{
ndr_ref_t myref;
bzero(&myref, sizeof (myref));
myref.stream = nds;
myref.datum = datum;
myref.name = "PROCESS";
myref.ti = ti;
return (ndr_topmost(&myref));
}
int
ndo_operation(ndr_stream_t *nds, ndr_typeinfo_t *ti, int opnum, char *datum)
{
ndr_ref_t myref;
bzero(&myref, sizeof (myref));
myref.stream = nds;
myref.datum = datum;
myref.name = "OPERATION";
myref.ti = ti;
myref.inner_flags = NDR_F_SWITCH_IS;
myref.switch_is = opnum;
if (ti->type_flags != NDR_F_INTERFACE) {
NDR_SET_ERROR(&myref, NDR_ERR_NOT_AN_INTERFACE);
return (0);
}
return ((*ti->ndr_func)(&myref));
}
int
ndr_params(ndr_ref_t *params_ref)
{
ndr_typeinfo_t *ti = params_ref->ti;
if (ti->type_flags == NDR_F_OPERATION)
return (*ti->ndr_func) (params_ref);
else
return (ndr_topmost(params_ref));
}
int
ndr_topmost(ndr_ref_t *top_ref)
{
ndr_stream_t *nds;
ndr_typeinfo_t *ti;
ndr_ref_t *outer_ref = 0;
int is_varlen;
int is_string;
int error;
int rc;
unsigned n_fixed;
int params;
assert(top_ref);
assert(top_ref->stream);
assert(top_ref->ti);
nds = top_ref->stream;
ti = top_ref->ti;
is_varlen = ti->pdu_size_variable_part;
is_string = NDR_IS_STRING(ti);
assert(nds->outer_queue_tailp && !*nds->outer_queue_tailp);
assert(!nds->outer_current);
params = top_ref->inner_flags & NDR_F_PARAMS_MASK;
switch (params) {
case NDR_F_NONE:
case NDR_F_SWITCH_IS:
if (is_string || is_varlen) {
error = NDR_ERR_TOPMOST_VARLEN_ILLEGAL;
NDR_SET_ERROR(outer_ref, error);
return (0);
}
n_fixed = ti->pdu_size_fixed_part;
break;
case NDR_F_SIZE_IS:
error = NDR_ERR_TOPMOST_VARLEN_ILLEGAL;
NDR_SET_ERROR(outer_ref, error);
return (0);
case NDR_F_DIMENSION_IS:
if (is_varlen) {
error = NDR_ERR_ARRAY_VARLEN_ILLEGAL;
NDR_SET_ERROR(outer_ref, error);
return (0);
}
n_fixed = ti->pdu_size_fixed_part * top_ref->dimension_is;
break;
case NDR_F_IS_POINTER:
case NDR_F_IS_POINTER+NDR_F_SIZE_IS:
n_fixed = 4;
break;
case NDR_F_IS_REFERENCE:
case NDR_F_IS_REFERENCE+NDR_F_SIZE_IS:
n_fixed = 0;
break;
default:
error = NDR_ERR_OUTER_PARAMS_BAD;
NDR_SET_ERROR(outer_ref, error);
return (0);
}
outer_ref = ndr_enter_outer_queue(top_ref);
if (!outer_ref)
return (0); /* error already set */
/*
* Hand-craft the first OUTER construct and directly call
* ndr_inner(). Then, run the outer_queue. We do this
* because ndr_outer() wants to malloc() memory for
* the construct, and we already have the memory.
*/
/* move the flags, etc, around again, undoes enter_outer_queue() */
outer_ref->inner_flags = top_ref->inner_flags;
outer_ref->outer_flags = 0;
outer_ref->datum = top_ref->datum;
/* All outer constructs start on a mod4 (longword) boundary */
if (!ndr_outer_align(outer_ref))
return (0); /* error already set */
/* Regardless of what it is, this is where it starts */
outer_ref->pdu_offset = nds->pdu_scan_offset;
rc = ndr_outer_grow(outer_ref, n_fixed);
if (!rc)
return (0); /* error already set */
outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_fixed;
/* set-up outer_current, as though run_outer_queue() was doing it */
nds->outer_current = outer_ref;
nds->outer_queue_tailp = &nds->outer_current->next;
nds->pdu_scan_offset = outer_ref->pdu_end_offset;
/* do the topmost member */
rc = ndr_inner(outer_ref);
if (!rc)
return (0); /* error already set */
nds->pdu_scan_offset = outer_ref->pdu_end_offset;
/* advance, as though run_outer_queue() was doing it */
nds->outer_current = nds->outer_current->next;
return (ndr_run_outer_queue(nds));
}
static ndr_ref_t *
ndr_enter_outer_queue(ndr_ref_t *arg_ref)
{
ndr_stream_t *nds = arg_ref->stream;
ndr_ref_t *outer_ref;
/*LINTED E_BAD_PTR_CAST_ALIGN*/
outer_ref = (ndr_ref_t *)NDS_MALLOC(nds, sizeof (*outer_ref), arg_ref);
if (!outer_ref) {
NDR_SET_ERROR(arg_ref, NDR_ERR_MALLOC_FAILED);
return (0);
}
*outer_ref = *arg_ref;
/* move advice in inner_flags to outer_flags */
outer_ref->outer_flags = arg_ref->inner_flags & NDR_F_PARAMS_MASK;
outer_ref->inner_flags = 0;
outer_ref->enclosing = nds->outer_current;
outer_ref->backptr = 0;
outer_ref->datum = 0;
assert(nds->outer_queue_tailp);
outer_ref->next = *nds->outer_queue_tailp;
*nds->outer_queue_tailp = outer_ref;
nds->outer_queue_tailp = &outer_ref->next;
return (outer_ref);
}
int
ndr_run_outer_queue(ndr_stream_t *nds)
{
while (nds->outer_current) {
nds->outer_queue_tailp = &nds->outer_current->next;
if (!ndr_outer(nds->outer_current))
return (0);
nds->outer_current = nds->outer_current->next;
}
return (1);
}
/*
* OUTER CONSTRUCTS
*
* OUTER constructs are where the real work is, which stems from the
* variable-length potential.
*
* DCE(MS)/RPC VARIABLE LENGTH -- CONFORMANT, VARYING, VARYING/CONFORMANT
*
* DCE(MS)/RPC provides for three forms of variable length: CONFORMANT,
* VARYING, and VARYING/CONFORMANT.
*
* What makes this so tough is that the variable-length array may be well
* encapsulated within the outer construct. Further, because DCE(MS)/RPC
* tries to keep the constructs contiguous in the data stream, the sizing
* information precedes the entire OUTER construct. The sizing information
* must be used at the appropriate time, which can be after many, many,
* many fixed-length elements. During IDL type analysis, we know in
* advance constructs that encapsulate variable-length constructs. So,
* we know when we have a sizing header and when we don't. The actual
* semantics of the header are largely deferred.
*
* Currently, VARYING constructs are not implemented but they are described
* here in case they have to be implemented in the future. Similarly,
* DCE(MS)/RPC provides for multi-dimensional arrays, which are currently
* not implemented. Only one-dimensional, variable-length arrays are
* supported.
*
* CONFORMANT CONSTRUCTS -- VARIABLE LENGTH ARRAYS START THE SHOW
*
* All variable-length values are arrays. These arrays may be embedded
* well within another construct. However, a variable-length construct
* may ONLY appear as the last member of an enclosing construct. Example:
*
* struct credentials {
* ulong uid, gid;
* ulong n_gids;
* [size_is(n_gids)]
* ulong gids[*]; // variable-length.
* };
*
* CONFORMANT constructs have a dynamic size in local memory and in the
* PDU. The CONFORMANT quality is indicated by the [size_is()] advice.
* CONFORMANT constructs have the following header:
*
* struct conformant_header {
* ulong size_is;
* };
*
* (Multi-dimensional CONFORMANT arrays have a similar header for each
* dimension - not implemented).
*
* Example CONFORMANT construct:
*
* struct user {
* char * name;
* struct credentials cred; // see above
* };
*
* Consider the data tree:
*
* +--------+
* | user |
* +--------+
* | name ----> "fred" (the string is a different OUTER)
* | uid |
* | gid |
* | n_gids | for example, 3
* | gids[0]|
* | gids[1]|
* | gids[2]|
* +--------+
*
* The OUTER construct in the Stub Data would be:
*
* +---+---------+---------------------------------------------+
* |pad|size_is=3 name uid gid n_gids=3 gids[0] gids[1] gids[2]|
* +---+---------+---------------------------------------------+
* szing hdr|user |<-------------- user.cred ------------>|
* |<--- fixed-size ---->|<----- conformant ---->|
*
* The ndr_typeinfo for struct user will have:
* pdu_fixed_size_part = 16 four long words (name uid gid n_gids)
* pdu_variable_size_part = 4 per element, sizeof gids[0]
*
* VARYING CONSTRUCTS -- NOT IMPLEMENTED
*
* VARYING constructs have the following header:
*
* struct varying_header {
* ulong first_is;
* ulong length_is;
* };
*
* This indicates which interval of an array is significant.
* Non-intersecting elements of the array are undefined and usually
* zero-filled. The first_is parameter for C arrays is always 0 for
* the first element.
*
* N.B. Constructs may contain one CONFORMANT element, which is always
* last, but may contain many VARYING elements, which can be anywhere.
*
* VARYING CONFORMANT constructs have the sizing headers arranged like
* this:
*
* struct conformant_header all_conformant[N_CONFORMANT_DIM];
* struct varying_header all_varying[N_VARYING_ELEMS_AND_DIMS];
*
* The sizing header is immediately followed by the values for the
* construct. Again, we don't support more than one dimension and
* we don't support VARYING constructs at this time.
*
* A good example of a VARYING/CONFORMANT data structure is the UNIX
* directory entry:
*
* struct dirent {
* ushort reclen;
* ushort namlen;
* ulong inum;
* [size_is(reclen-8) length_is(namlen+1)] // -(2+2+4), +1 for NUL
* uchar name[*];
* };
*
*
* STRINGS ARE A SPECIAL CASE
*
* Strings are handled specially. MS/RPC uses VARYING/CONFORMANT structures
* for strings. This is a simple one-dimensional variable-length array,
* typically with its last element all zeroes. We handle strings with the
* header:
*
* struct string_header {
* ulong size_is;
* ulong first_is; // always 0
* ulong length_is; // always same as size_is
* };
*
* If general support for VARYING and VARYING/CONFORMANT mechanisms is
* implemented, we probably won't need the strings special case.
*/
int
ndr_outer(ndr_ref_t *outer_ref)
{
ndr_stream_t *nds = outer_ref->stream;
ndr_typeinfo_t *ti = outer_ref->ti;
int is_varlen = ti->pdu_size_variable_part;
int is_union = NDR_IS_UNION(ti);
int is_string = NDR_IS_STRING(ti);
int error = NDR_ERR_OUTER_PARAMS_BAD;
int params;
params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
NDR_TATTLE(outer_ref, "--OUTER--");
/* All outer constructs start on a mod4 (longword) boundary */
if (!ndr_outer_align(outer_ref))
return (0); /* error already set */
/* Regardless of what it is, this is where it starts */
outer_ref->pdu_offset = nds->pdu_scan_offset;
if (is_union) {
error = NDR_ERR_OUTER_UNION_ILLEGAL;
NDR_SET_ERROR(outer_ref, error);
return (0);
}
switch (params) {
case NDR_F_NONE:
if (is_string)
return (ndr_outer_string(outer_ref));
if (is_varlen)
return (ndr_outer_conformant_construct(outer_ref));
return (ndr_outer_fixed(outer_ref));
case NDR_F_SIZE_IS:
case NDR_F_DIMENSION_IS:
case NDR_F_IS_POINTER+NDR_F_SIZE_IS:
case NDR_F_IS_REFERENCE+NDR_F_SIZE_IS:
if (is_varlen) {
error = NDR_ERR_ARRAY_VARLEN_ILLEGAL;
break;
}
if (params & NDR_F_SIZE_IS)
return (ndr_outer_conformant_array(outer_ref));
else
return (ndr_outer_fixed_array(outer_ref));
default:
error = NDR_ERR_OUTER_PARAMS_BAD;
break;
}
/*
* If we get here, something is wrong. Most likely,
* the params flags do not match.
*/
NDR_SET_ERROR(outer_ref, error);
return (0);
}
int
ndr_outer_fixed(ndr_ref_t *outer_ref)
{
ndr_stream_t *nds = outer_ref->stream;
ndr_typeinfo_t *ti = outer_ref->ti;
ndr_ref_t myref;
char *valp = NULL;
int is_varlen = ti->pdu_size_variable_part;
int is_union = NDR_IS_UNION(ti);
int is_string = NDR_IS_STRING(ti);
int rc;
unsigned n_hdr;
unsigned n_fixed;
unsigned n_variable;
unsigned n_alloc;
unsigned n_pdu_total;
int params;
params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
assert(!is_varlen && !is_string && !is_union);
assert(params == NDR_F_NONE);
/* no header for this */
n_hdr = 0;
/* fixed part -- exactly one of these */
n_fixed = ti->pdu_size_fixed_part;
assert(n_fixed > 0);
/* variable part -- exactly none of these */
n_variable = 0;
/* sum them up to determine the PDU space required */
n_pdu_total = n_hdr + n_fixed + n_variable;
/* similar sum to determine how much local memory is required */
n_alloc = n_fixed + n_variable;
rc = ndr_outer_grow(outer_ref, n_pdu_total);
if (!rc)
return (rc); /* error already set */
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
valp = outer_ref->datum;
if (!valp) {
NDR_SET_ERROR(outer_ref, NDR_ERR_OUTER_PARAMS_BAD);
return (0);
}
if (outer_ref->backptr)
assert(valp == *outer_ref->backptr);
break;
case NDR_M_OP_UNMARSHALL:
valp = NDS_MALLOC(nds, n_alloc, outer_ref);
if (!valp) {
NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED);
return (0);
}
if (outer_ref->backptr)
*outer_ref->backptr = valp;
outer_ref->datum = valp;
break;
default:
NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
bzero(&myref, sizeof (myref));
myref.stream = nds;
myref.enclosing = outer_ref;
myref.ti = outer_ref->ti;
myref.datum = outer_ref->datum;
myref.name = "FIXED-VALUE";
myref.outer_flags = NDR_F_NONE;
myref.inner_flags = NDR_F_NONE;
myref.pdu_offset = outer_ref->pdu_offset;
outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total;
rc = ndr_inner(&myref);
if (!rc)
return (rc); /* error already set */
nds->pdu_scan_offset = outer_ref->pdu_end_offset;
return (1);
}
int
ndr_outer_fixed_array(ndr_ref_t *outer_ref)
{
ndr_stream_t *nds = outer_ref->stream;
ndr_typeinfo_t *ti = outer_ref->ti;
ndr_ref_t myref;
char *valp = NULL;
int is_varlen = ti->pdu_size_variable_part;
int is_union = NDR_IS_UNION(ti);
int is_string = NDR_IS_STRING(ti);
int rc;
unsigned n_hdr;
unsigned n_fixed;
unsigned n_variable;
unsigned n_alloc;
unsigned n_pdu_total;
int params;
params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
assert(!is_varlen && !is_string && !is_union);
assert(params == NDR_F_DIMENSION_IS);
/* no header for this */
n_hdr = 0;
/* fixed part -- exactly dimension_is of these */
n_fixed = ti->pdu_size_fixed_part * outer_ref->dimension_is;
assert(n_fixed > 0);
/* variable part -- exactly none of these */
n_variable = 0;
/* sum them up to determine the PDU space required */
n_pdu_total = n_hdr + n_fixed + n_variable;
/* similar sum to determine how much local memory is required */
n_alloc = n_fixed + n_variable;
rc = ndr_outer_grow(outer_ref, n_pdu_total);
if (!rc)
return (rc); /* error already set */
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
valp = outer_ref->datum;
if (!valp) {
NDR_SET_ERROR(outer_ref, NDR_ERR_OUTER_PARAMS_BAD);
return (0);
}
if (outer_ref->backptr)
assert(valp == *outer_ref->backptr);
break;
case NDR_M_OP_UNMARSHALL:
valp = NDS_MALLOC(nds, n_alloc, outer_ref);
if (!valp) {
NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED);
return (0);
}
if (outer_ref->backptr)
*outer_ref->backptr = valp;
outer_ref->datum = valp;
break;
default:
NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
bzero(&myref, sizeof (myref));
myref.stream = nds;
myref.enclosing = outer_ref;
myref.ti = outer_ref->ti;
myref.datum = outer_ref->datum;
myref.name = "FIXED-ARRAY";
myref.outer_flags = NDR_F_NONE;
myref.inner_flags = NDR_F_DIMENSION_IS;
myref.dimension_is = outer_ref->dimension_is;
myref.pdu_offset = outer_ref->pdu_offset;
outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total;
rc = ndr_inner(&myref);
if (!rc)
return (rc); /* error already set */
nds->pdu_scan_offset = outer_ref->pdu_end_offset;
return (1);
}
int
ndr_outer_conformant_array(ndr_ref_t *outer_ref)
{
ndr_stream_t *nds = outer_ref->stream;
ndr_typeinfo_t *ti = outer_ref->ti;
ndr_ref_t myref;
char *valp = NULL;
int is_varlen = ti->pdu_size_variable_part;
int is_union = NDR_IS_UNION(ti);
int is_string = NDR_IS_STRING(ti);
unsigned long size_is;
int rc;
unsigned n_hdr;
unsigned n_fixed;
unsigned n_variable;
unsigned n_alloc;
unsigned n_pdu_total;
unsigned n_ptr_offset;
int params;
params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
assert(!is_varlen && !is_string && !is_union);
assert(params & NDR_F_SIZE_IS);
/* conformant header for this */
n_hdr = 4;
/* fixed part -- exactly none of these */
n_fixed = 0;
/* variable part -- exactly size_of of these */
/* notice that it is the **fixed** size of the ti */
n_variable = ti->pdu_size_fixed_part * outer_ref->size_is;
/* sum them up to determine the PDU space required */
n_pdu_total = n_hdr + n_fixed + n_variable;
/* similar sum to determine how much local memory is required */
n_alloc = n_fixed + n_variable;
rc = ndr_outer_grow(outer_ref, n_pdu_total);
if (!rc)
return (rc); /* error already set */
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
size_is = outer_ref->size_is;
rc = ndr_outer_poke_sizing(outer_ref, 0, &size_is);
if (!rc)
return (0); /* error already set */
valp = outer_ref->datum;
if (!valp) {
NDR_SET_ERROR(outer_ref, NDR_ERR_OUTER_PARAMS_BAD);
return (0);
}
if (outer_ref->backptr)
assert(valp == *outer_ref->backptr);
n_ptr_offset = 4;
break;
case NDR_M_OP_UNMARSHALL:
if (params & NDR_F_IS_REFERENCE) {
size_is = outer_ref->size_is;
n_ptr_offset = 0;
} else {
/* NDR_F_IS_POINTER */
rc = ndr_outer_peek_sizing(outer_ref, 0, &size_is);
if (!rc)
return (0); /* error already set */
if (size_is != outer_ref->size_is) {
NDR_SET_ERROR(outer_ref,
NDR_ERR_SIZE_IS_MISMATCH_PDU);
return (0);
}
n_ptr_offset = 4;
}
if (size_is > 0) {
valp = NDS_MALLOC(nds, n_alloc, outer_ref);
if (!valp) {
NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED);
return (0);
}
}
if (outer_ref->backptr)
*outer_ref->backptr = valp;
outer_ref->datum = valp;
break;
default:
NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total;
outer_ref->type_flags = NDR_F_NONE;
outer_ref->inner_flags = NDR_F_NONE;
if (size_is > 0) {
bzero(&myref, sizeof (myref));
myref.stream = nds;
myref.enclosing = outer_ref;
myref.ti = outer_ref->ti;
myref.datum = outer_ref->datum;
myref.name = "CONFORMANT-ARRAY";
myref.outer_flags = NDR_F_NONE;
myref.inner_flags = NDR_F_SIZE_IS;
myref.size_is = outer_ref->size_is;
myref.inner_flags = NDR_F_DIMENSION_IS; /* convenient */
myref.dimension_is = outer_ref->size_is; /* convenient */
myref.pdu_offset = outer_ref->pdu_offset + n_ptr_offset;
rc = ndr_inner(&myref);
if (!rc)
return (rc); /* error already set */
}
nds->pdu_scan_offset = outer_ref->pdu_end_offset;
return (1);
}
int
ndr_outer_conformant_construct(ndr_ref_t *outer_ref)
{
ndr_stream_t *nds = outer_ref->stream;
ndr_typeinfo_t *ti = outer_ref->ti;
ndr_ref_t myref;
char *valp = NULL;
int is_varlen = ti->pdu_size_variable_part;
int is_union = NDR_IS_UNION(ti);
int is_string = NDR_IS_STRING(ti);
unsigned long size_is;
int rc;
unsigned n_hdr;
unsigned n_fixed;
unsigned n_variable;
unsigned n_alloc;
unsigned n_pdu_total;
int params;
params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
assert(is_varlen && !is_string && !is_union);
assert(params == NDR_F_NONE);
/* conformant header for this */
n_hdr = 4;
/* fixed part -- exactly one of these */
n_fixed = ti->pdu_size_fixed_part;
/* variable part -- exactly size_of of these */
n_variable = 0; /* 0 for the moment */
/* sum them up to determine the PDU space required */
n_pdu_total = n_hdr + n_fixed + n_variable;
/* similar sum to determine how much local memory is required */
n_alloc = n_fixed + n_variable;
/* For the moment, grow enough for the fixed-size part */
rc = ndr_outer_grow(outer_ref, n_pdu_total);
if (!rc)
return (rc); /* error already set */
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
/*
* We don't know the size yet. We have to wait for
* it. Proceed with the fixed-size part, and await
* the call to ndr_size_is().
*/
size_is = 0;
rc = ndr_outer_poke_sizing(outer_ref, 0, &size_is);
if (!rc)
return (0); /* error already set */
valp = outer_ref->datum;
if (!valp) {
NDR_SET_ERROR(outer_ref, NDR_ERR_OUTER_PARAMS_BAD);
return (0);
}
if (outer_ref->backptr)
assert(valp == *outer_ref->backptr);
break;
case NDR_M_OP_UNMARSHALL:
/*
* We know the size of the variable part because
* of the CONFORMANT header. We will verify
* the header against the [size_is(X)] advice
* later when ndr_size_is() is called.
*/
rc = ndr_outer_peek_sizing(outer_ref, 0, &size_is);
if (!rc)
return (0); /* error already set */
/* recalculate metrics */
n_variable = size_is * ti->pdu_size_variable_part;
n_pdu_total = n_hdr + n_fixed + n_variable;
n_alloc = n_fixed + n_variable;
rc = ndr_outer_grow(outer_ref, n_pdu_total);
if (!rc)
return (rc); /* error already set */
outer_ref->size_is = size_is; /* verified later */
valp = NDS_MALLOC(nds, n_alloc, outer_ref);
if (!valp) {
NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED);
return (0);
}
if (outer_ref->backptr)
*outer_ref->backptr = valp;
outer_ref->datum = valp;
break;
default:
NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total;
outer_ref->type_flags = NDR_F_SIZE_IS; /* indicate pending */
outer_ref->inner_flags = NDR_F_NONE; /* indicate pending */
bzero(&myref, sizeof (myref));
myref.stream = nds;
myref.enclosing = outer_ref;
myref.ti = outer_ref->ti;
myref.datum = outer_ref->datum;
myref.name = "CONFORMANT-CONSTRUCT";
myref.outer_flags = NDR_F_NONE;
myref.inner_flags = NDR_F_NONE;
myref.size_is = outer_ref->size_is;
myref.pdu_offset = outer_ref->pdu_offset + 4;
rc = ndr_inner(&myref);
if (!rc)
return (rc); /* error already set */
nds->pdu_scan_offset = outer_ref->pdu_end_offset;
if (outer_ref->inner_flags != NDR_F_SIZE_IS) {
NDR_SET_ERROR(&myref, NDR_ERR_SIZE_IS_MISMATCH_AFTER);
return (0);
}
return (1);
}
int
ndr_size_is(ndr_ref_t *ref)
{
ndr_stream_t *nds = ref->stream;
ndr_ref_t *outer_ref = nds->outer_current;
ndr_typeinfo_t *ti = outer_ref->ti;
unsigned long size_is;
int rc;
unsigned n_hdr;
unsigned n_fixed;
unsigned n_variable;
unsigned n_pdu_total;
assert(ref->inner_flags & NDR_F_SIZE_IS);
size_is = ref->size_is;
if (outer_ref->type_flags != NDR_F_SIZE_IS) {
NDR_SET_ERROR(ref, NDR_ERR_SIZE_IS_UNEXPECTED);
return (0);
}
if (outer_ref->inner_flags & NDR_F_SIZE_IS) {
NDR_SET_ERROR(ref, NDR_ERR_SIZE_IS_DUPLICATED);
return (0);
}
/* repeat metrics, see ndr_conformant_construct() above */
n_hdr = 4;
n_fixed = ti->pdu_size_fixed_part;
n_variable = size_is * ti->pdu_size_variable_part;
n_pdu_total = n_hdr + n_fixed + n_variable;
rc = ndr_outer_grow(outer_ref, n_pdu_total);
if (!rc)
return (rc); /* error already set */
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
/*
* We have to set the sizing header and extend
* the size of the PDU (already done).
*/
rc = ndr_outer_poke_sizing(outer_ref, 0, &size_is);
if (!rc)
return (0); /* error already set */
break;
case NDR_M_OP_UNMARSHALL:
/*
* Allocation done during ndr_conformant_construct().
* All we are doing here is verifying that the
* intended size (ref->size_is) matches the sizing header.
*/
if (size_is != outer_ref->size_is) {
NDR_SET_ERROR(ref, NDR_ERR_SIZE_IS_MISMATCH_PDU);
return (0);
}
break;
default:
NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
outer_ref->inner_flags |= NDR_F_SIZE_IS;
outer_ref->size_is = ref->size_is;
return (1);
}
int
ndr_outer_string(ndr_ref_t *outer_ref)
{
ndr_stream_t *nds = outer_ref->stream;
ndr_typeinfo_t *ti = outer_ref->ti;
ndr_ref_t myref;
char *valp = NULL;
unsigned is_varlen = ti->pdu_size_variable_part;
int is_union = NDR_IS_UNION(ti);
int is_string = NDR_IS_STRING(ti);
int rc;
unsigned n_zeroes;
unsigned ix;
unsigned long size_is;
unsigned long first_is;
unsigned long length_is;
unsigned n_hdr;
unsigned n_fixed;
unsigned n_variable;
unsigned n_alloc;
unsigned n_pdu_total;
int params;
params = outer_ref->outer_flags & NDR_F_PARAMS_MASK;
assert(is_varlen && is_string && !is_union);
assert(params == NDR_F_NONE);
/* string header for this: size_is first_is length_is */
n_hdr = 12;
/* fixed part -- exactly none of these */
n_fixed = 0;
if (!ndr_outer_grow(outer_ref, n_hdr))
return (0); /* error already set */
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
valp = outer_ref->datum;
if (!valp) {
NDR_SET_ERROR(outer_ref, NDR_ERR_OUTER_PARAMS_BAD);
return (0);
}
if (outer_ref->backptr)
assert(valp == *outer_ref->backptr);
if (ti == &ndt_s_wchar) {
/*
* size_is is the number of characters in the
* (multibyte) string, including the null.
* In other words, symbols, not bytes.
*/
size_t wlen;
wlen = ndr__mbstowcs(NULL, valp, NDR_STRING_MAX);
if (wlen == (size_t)-1) {
/* illegal sequence error? */
NDR_SET_ERROR(outer_ref, NDR_ERR_STRLEN);
return (0);
}
if ((nds->flags & NDS_F_NONULL) == 0)
wlen++;
if (wlen > NDR_STRING_MAX) {
NDR_SET_ERROR(outer_ref, NDR_ERR_STRLEN);
return (0);
}
size_is = wlen;
} else {
valp = outer_ref->datum;
n_zeroes = 0;
for (ix = 0; ix < NDR_STRING_MAX; ix++) {
if (valp[ix] == 0) {
n_zeroes++;
if (n_zeroes >= is_varlen &&
ix % is_varlen == 0) {
break;
}
} else {
n_zeroes = 0;
}
}
if (ix >= NDR_STRING_MAX) {
NDR_SET_ERROR(outer_ref, NDR_ERR_STRLEN);
return (0);
}
size_is = ix+1;
}
first_is = 0;
if (nds->flags & NDS_F_NOTERM)
length_is = size_is - 1;
else
length_is = size_is;
if (!ndr_outer_poke_sizing(outer_ref, 0, &size_is) ||
!ndr_outer_poke_sizing(outer_ref, 4, &first_is) ||
!ndr_outer_poke_sizing(outer_ref, 8, &length_is))
return (0); /* error already set */
break;
case NDR_M_OP_UNMARSHALL:
if (!ndr_outer_peek_sizing(outer_ref, 0, &size_is) ||
!ndr_outer_peek_sizing(outer_ref, 4, &first_is) ||
!ndr_outer_peek_sizing(outer_ref, 8, &length_is))
return (0); /* error already set */
/*
* Enforce bounds on: size_is, first_is, length_is
*
* In addition to the first_is check, we used to check that
* size_is or size_is-1 was equal to length_is but Windows95
* doesn't conform to this "rule" (see variable part below).
* The srvmgr tool for Windows95 sent the following values
* for a path string:
*
* size_is = 261 (0x105)
* first_is = 0
* length_is = 53 (0x35)
*
* The length_is was correct (for the given path) but the
* size_is was the maximum path length rather than being
* related to length_is.
*/
if (size_is > NDR_STRING_MAX) {
NDR_SET_ERROR(outer_ref, NDR_ERR_STRING_SIZING);
return (0);
}
if (first_is != 0) {
NDR_SET_ERROR(outer_ref, NDR_ERR_STRING_SIZING);
return (0);
}
if (length_is > size_is) {
NDR_SET_ERROR(outer_ref, NDR_ERR_STRLEN);
return (0);
}
if (ti == &ndt_s_wchar) {
/*
* Decoding Unicode to UTF-8; we need to allow
* for the maximum possible char size. It would
* be nice to use mbequiv_strlen but the string
* may not be null terminated.
*/
n_alloc = (size_is + 1) * NDR_MB_CHAR_MAX;
} else {
n_alloc = (size_is + 1) * is_varlen;
}
valp = NDS_MALLOC(nds, n_alloc, outer_ref);
if (!valp) {
NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED);
return (0);
}
bzero(valp, (size_is+1) * is_varlen);
if (outer_ref->backptr)
*outer_ref->backptr = valp;
outer_ref->datum = valp;
break;
default:
NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
/*
* Variable part - exactly length_is of these.
*
* Usually, length_is is same as size_is and includes nul.
* Some protocols use length_is = size_is-1, and length_is does
* not include the nul (which is more consistent with DCE spec).
* If the length_is is 0, there is no data following the
* sizing header, regardless of size_is.
*/
n_variable = length_is * is_varlen;
/* sum them up to determine the PDU space required */
n_pdu_total = n_hdr + n_fixed + n_variable;
/* similar sum to determine how much local memory is required */
n_alloc = n_fixed + n_variable;
rc = ndr_outer_grow(outer_ref, n_pdu_total);
if (!rc)
return (rc); /* error already set */
if (length_is > 0) {
bzero(&myref, sizeof (myref));
myref.stream = nds;
myref.enclosing = outer_ref;
myref.ti = outer_ref->ti;
myref.datum = outer_ref->datum;
myref.name = "OUTER-STRING";
myref.outer_flags = NDR_F_IS_STRING;
myref.inner_flags = NDR_F_NONE;
/*
* Set up size_is and strlen_is for ndr_s_wchar.
*/
myref.size_is = size_is;
myref.strlen_is = length_is;
}
myref.pdu_offset = outer_ref->pdu_offset + 12;
/*
* Don't try to decode empty strings.
*/
if ((size_is == 0) && (first_is == 0) && (length_is == 0)) {
nds->pdu_scan_offset = outer_ref->pdu_end_offset;
return (1);
}
if ((size_is != 0) && (length_is != 0)) {
rc = ndr_inner(&myref);
if (!rc)
return (rc); /* error already set */
}
nds->pdu_scan_offset = outer_ref->pdu_end_offset;
return (1);
}
int
ndr_outer_peek_sizing(ndr_ref_t *outer_ref, unsigned offset,
unsigned long *sizing_p)
{
ndr_stream_t *nds = outer_ref->stream;
unsigned long pdu_offset;
int rc;
pdu_offset = outer_ref->pdu_offset + offset;
if (pdu_offset < nds->outer_current->pdu_offset ||
pdu_offset > nds->outer_current->pdu_end_offset ||
pdu_offset+4 > nds->outer_current->pdu_end_offset) {
NDR_SET_ERROR(outer_ref, NDR_ERR_BOUNDS_CHECK);
return (0);
}
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
NDR_SET_ERROR(outer_ref, NDR_ERR_UNIMPLEMENTED);
return (0);
case NDR_M_OP_UNMARSHALL:
rc = NDS_GET_PDU(nds, pdu_offset, 4, (char *)sizing_p,
nds->swap, outer_ref);
break;
default:
NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
return (rc);
}
int
ndr_outer_poke_sizing(ndr_ref_t *outer_ref, unsigned offset,
unsigned long *sizing_p)
{
ndr_stream_t *nds = outer_ref->stream;
unsigned long pdu_offset;
int rc;
pdu_offset = outer_ref->pdu_offset + offset;
if (pdu_offset < nds->outer_current->pdu_offset ||
pdu_offset > nds->outer_current->pdu_end_offset ||
pdu_offset+4 > nds->outer_current->pdu_end_offset) {
NDR_SET_ERROR(outer_ref, NDR_ERR_BOUNDS_CHECK);
return (0);
}
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
rc = NDS_PUT_PDU(nds, pdu_offset, 4, (char *)sizing_p,
nds->swap, outer_ref);
break;
case NDR_M_OP_UNMARSHALL:
NDR_SET_ERROR(outer_ref, NDR_ERR_UNIMPLEMENTED);
return (0);
default:
NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
return (rc);
}
/*
* All OUTER constructs begin on a mod4 (dword) boundary - except
* for the ones that don't: some MSRPC calls appear to use word or
* packed alignment. Strings appear to be dword aligned.
*/
int
ndr_outer_align(ndr_ref_t *outer_ref)
{
ndr_stream_t *nds = outer_ref->stream;
int rc;
unsigned n_pad;
unsigned align;
if (outer_ref->packed_alignment && outer_ref->ti != &ndt_s_wchar) {
align = outer_ref->ti->alignment;
n_pad = ((align + 1) - nds->pdu_scan_offset) & align;
} else {
n_pad = NDR_ALIGN4(nds->pdu_scan_offset);
}
if ((outer_ref->ti->type_flags & NDR_F_FAKE) != 0 || n_pad == 0)
return (1); /* already aligned, often the case */
if (!ndr_outer_grow(outer_ref, n_pad))
return (0); /* error already set */
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
rc = NDS_PAD_PDU(nds, nds->pdu_scan_offset, n_pad, outer_ref);
if (!rc) {
NDR_SET_ERROR(outer_ref, NDR_ERR_PAD_FAILED);
return (0);
}
break;
case NDR_M_OP_UNMARSHALL:
break;
default:
NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
nds->pdu_scan_offset += n_pad;
return (1);
}
int
ndr_outer_grow(ndr_ref_t *outer_ref, unsigned n_total)
{
ndr_stream_t *nds = outer_ref->stream;
unsigned long pdu_want_size;
int rc, is_ok = 0;
pdu_want_size = nds->pdu_scan_offset + n_total;
if (pdu_want_size <= nds->pdu_max_size) {
is_ok = 1;
}
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
if (is_ok)
break;
rc = NDS_GROW_PDU(nds, pdu_want_size, outer_ref);
if (!rc) {
NDR_SET_ERROR(outer_ref, NDR_ERR_GROW_FAILED);
return (0);
}
break;
case NDR_M_OP_UNMARSHALL:
if (is_ok)
break;
NDR_SET_ERROR(outer_ref, NDR_ERR_UNDERFLOW);
return (0);
default:
NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
if (nds->pdu_size < pdu_want_size)
nds->pdu_size = pdu_want_size;
outer_ref->pdu_end_offset = pdu_want_size;
return (1);
}
/*
* Some 'outer' constructs incorrectly align the entire construct
* on a 4-byte boundary, when each of its members needs to be
* aligned separately. This function handles aligning 'inner'
* members on their natural alignment boundary.
*
* NOTE: This assumes it is not being used for headers.
* Headers present some unique concerns that this may not
* adequately address (e.g. reserved space, pdu_body_offset).
*/
int
ndr_inner_align(ndr_ref_t *arg_ref)
{
ndr_stream_t *nds = arg_ref->stream;
int rc;
unsigned n_pad;
n_pad = ((arg_ref->ti->alignment + 1) - arg_ref->pdu_offset) &
arg_ref->ti->alignment;
if (n_pad == 0)
return (1); /* already aligned, often the case */
if (!ndr_outer_grow(arg_ref->enclosing, n_pad))
return (0); /* error already set */
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
rc = NDS_PAD_PDU(nds, arg_ref->pdu_offset, n_pad, arg_ref);
if (!rc) {
NDR_SET_ERROR(arg_ref, NDR_ERR_PAD_FAILED);
return (0);
}
break;
case NDR_M_OP_UNMARSHALL:
break;
default:
NDR_SET_ERROR(arg_ref, NDR_ERR_M_OP_INVALID);
return (0);
}
/* All current and future offsets need to advance */
arg_ref->enclosing->pdu_offset += n_pad;
arg_ref->pdu_offset += n_pad;
/* ndr_outer_grow changed pdu_end_offset */
nds->pdu_scan_offset += n_pad;
return (1);
}
/*
* INNER ELEMENTS
*
* The local datum (arg_ref->datum) already exists, there is no need to
* malloc() it. The datum should point at a member of a structure.
*
* For the most part, ndr_inner() and its helpers are just a sanity
* check. The underlying ti->ndr_func() could be called immediately
* for non-pointer elements. For the sake of robustness, we detect
* run-time errors here. Most of the situations this protects against
* have already been checked by the IDL compiler. This is also a
* common point for processing of all data, and so is a convenient
* place to work from for debugging.
*/
int
ndr_inner(ndr_ref_t *arg_ref)
{
ndr_typeinfo_t *ti = arg_ref->ti;
int is_varlen = ti->pdu_size_variable_part;
int is_union = NDR_IS_UNION(ti);
int error = NDR_ERR_INNER_PARAMS_BAD;
int params;
params = arg_ref->inner_flags & NDR_F_PARAMS_MASK;
/*
* Switched unions are meant to be converted to and from encapsulated
* structures on the wire. However, NDRGEN doesn't implement this.
* As a result, our interface definitions use 'fake' structures
* to represent switched unions.
* This causes the structure to be aligned on a struct (4 byte)
* boundary, with none of its members having separate alignment -
* but that's not correct. Each of its members has its own,
* natural alignment, and we need to honor that.
* That happens here.
*/
if (arg_ref->enclosing != NULL &&
(arg_ref->enclosing->ti->type_flags & NDR_F_FAKE) != 0 &&
!ndr_inner_align(arg_ref))
return (0); /* error already set */
switch (params) {
case NDR_F_NONE:
if (is_union) {
error = NDR_ERR_SWITCH_VALUE_MISSING;
break;
}
return (*ti->ndr_func)(arg_ref);
case NDR_F_SIZE_IS:
case NDR_F_DIMENSION_IS:
case NDR_F_IS_POINTER+NDR_F_SIZE_IS: /* pointer to something */
case NDR_F_IS_REFERENCE+NDR_F_SIZE_IS: /* pointer to something */
if (is_varlen) {
error = NDR_ERR_ARRAY_VARLEN_ILLEGAL;
break;
}
if (is_union) {
error = NDR_ERR_ARRAY_UNION_ILLEGAL;
break;
}
if (params & NDR_F_IS_POINTER)
return (ndr_inner_pointer(arg_ref));
else if (params & NDR_F_IS_REFERENCE)
return (ndr_inner_reference(arg_ref));
else
return (ndr_inner_array(arg_ref));
case NDR_F_IS_POINTER: /* type is pointer to one something */
if (is_union) {
error = NDR_ERR_ARRAY_UNION_ILLEGAL;
break;
}
return (ndr_inner_pointer(arg_ref));
case NDR_F_IS_REFERENCE: /* type is pointer to one something */
if (is_union) {
error = NDR_ERR_ARRAY_UNION_ILLEGAL;
break;
}
return (ndr_inner_reference(arg_ref));
case NDR_F_SWITCH_IS:
if (!is_union) {
error = NDR_ERR_SWITCH_VALUE_ILLEGAL;
break;
}
return (*ti->ndr_func)(arg_ref);
default:
error = NDR_ERR_INNER_PARAMS_BAD;
break;
}
/*
* If we get here, something is wrong. Most likely,
* the params flags do not match
*/
NDR_SET_ERROR(arg_ref, error);
return (0);
}
int
ndr_inner_pointer(ndr_ref_t *arg_ref)
{
ndr_stream_t *nds = arg_ref->stream;
/*LINTED E_BAD_PTR_CAST_ALIGN*/
char **valpp = (char **)arg_ref->datum;
ndr_ref_t *outer_ref;
if (!ndr__ulong(arg_ref))
return (0); /* error */
if (!*valpp)
return (1); /* NULL pointer */
outer_ref = ndr_enter_outer_queue(arg_ref);
if (!outer_ref)
return (0); /* error already set */
/*
* Move advice in inner_flags to outer_flags.
* Retain pointer flag for conformant arrays.
*/
outer_ref->outer_flags = arg_ref->inner_flags & NDR_F_PARAMS_MASK;
if ((outer_ref->outer_flags & NDR_F_SIZE_IS) == 0)
outer_ref->outer_flags &= ~NDR_F_IS_POINTER;
#ifdef NDR_INNER_PTR_NOT_YET
outer_ref->outer_flags |= NDR_F_BACKPTR;
if (outer_ref->outer_flags & NDR_F_SIZE_IS) {
outer_ref->outer_flags |= NDR_F_ARRAY+NDR_F_CONFORMANT;
}
#endif /* NDR_INNER_PTR_NOT_YET */
outer_ref->backptr = valpp;
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
outer_ref->datum = *valpp;
break;
case NDR_M_OP_UNMARSHALL:
/*
* This is probably wrong if the application allocated
* memory in advance. Indicate no value for now.
* ONC RPC handles this case.
*/
*valpp = 0;
outer_ref->datum = 0;
break;
}
return (1); /* pointer dereference scheduled */
}
int
ndr_inner_reference(ndr_ref_t *arg_ref)
{
ndr_stream_t *nds = arg_ref->stream;
/*LINTED E_BAD_PTR_CAST_ALIGN*/
char **valpp = (char **)arg_ref->datum;
ndr_ref_t *outer_ref;
outer_ref = ndr_enter_outer_queue(arg_ref);
if (!outer_ref)
return (0); /* error already set */
/*
* Move advice in inner_flags to outer_flags.
* Retain reference flag for conformant arrays.
*/
outer_ref->outer_flags = arg_ref->inner_flags & NDR_F_PARAMS_MASK;
if ((outer_ref->outer_flags & NDR_F_SIZE_IS) == 0)
outer_ref->outer_flags &= ~NDR_F_IS_REFERENCE;
#ifdef NDR_INNER_REF_NOT_YET
outer_ref->outer_flags |= NDR_F_BACKPTR;
if (outer_ref->outer_flags & NDR_F_SIZE_IS) {
outer_ref->outer_flags |= NDR_F_ARRAY+NDR_F_CONFORMANT;
}
#endif /* NDR_INNER_REF_NOT_YET */
outer_ref->backptr = valpp;
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
outer_ref->datum = *valpp;
break;
case NDR_M_OP_UNMARSHALL:
/*
* This is probably wrong if the application allocated
* memory in advance. Indicate no value for now.
* ONC RPC handles this case.
*/
*valpp = 0;
outer_ref->datum = 0;
break;
}
return (1); /* pointer dereference scheduled */
}
int
ndr_inner_array(ndr_ref_t *encl_ref)
{
ndr_typeinfo_t *ti = encl_ref->ti;
ndr_ref_t myref;
unsigned long pdu_offset = encl_ref->pdu_offset;
unsigned long n_elem;
unsigned long i;
char name[30];
if (encl_ref->inner_flags & NDR_F_SIZE_IS) {
/* now is the time to check/set size */
if (!ndr_size_is(encl_ref))
return (0); /* error already set */
n_elem = encl_ref->size_is;
} else {
assert(encl_ref->inner_flags & NDR_F_DIMENSION_IS);
n_elem = encl_ref->dimension_is;
}
bzero(&myref, sizeof (myref));
myref.enclosing = encl_ref;
myref.stream = encl_ref->stream;
myref.packed_alignment = 0;
myref.ti = ti;
myref.inner_flags = NDR_F_NONE;
for (i = 0; i < n_elem; i++) {
(void) snprintf(name, sizeof (name), "[%lu]", i);
myref.name = name;
myref.pdu_offset = pdu_offset + i * ti->pdu_size_fixed_part;
myref.datum = encl_ref->datum + i * ti->c_size_fixed_part;
if (!ndr_inner(&myref))
return (0);
}
return (1);
}
/*
* BASIC TYPES
*/
#define MAKE_BASIC_TYPE_BASE(TYPE, SIZE) \
extern int ndr_##TYPE(struct ndr_reference *encl_ref); \
ndr_typeinfo_t ndt_##TYPE = { \
1, /* NDR version */ \
(SIZE)-1, /* alignment */ \
NDR_F_NONE, /* flags */ \
ndr_##TYPE, /* ndr_func */ \
SIZE, /* pdu_size_fixed_part */ \
0, /* pdu_size_variable_part */ \
SIZE, /* c_size_fixed_part */ \
0, /* c_size_variable_part */ \
}; \
int ndr_##TYPE(struct ndr_reference *ref) { \
return (ndr_basic_integer(ref, SIZE)); \
}
#define MAKE_BASIC_TYPE_STRING(TYPE, SIZE) \
extern int ndr_s##TYPE(struct ndr_reference *encl_ref); \
ndr_typeinfo_t ndt_s##TYPE = { \
1, /* NDR version */ \
(SIZE)-1, /* alignment */ \
NDR_F_STRING, /* flags */ \
ndr_s##TYPE, /* ndr_func */ \
0, /* pdu_size_fixed_part */ \
SIZE, /* pdu_size_variable_part */ \
0, /* c_size_fixed_part */ \
SIZE, /* c_size_variable_part */ \
}; \
int ndr_s##TYPE(struct ndr_reference *ref) { \
return (ndr_string_basic_integer(ref, &ndt_##TYPE)); \
}
#define MAKE_BASIC_TYPE(TYPE, SIZE) \
MAKE_BASIC_TYPE_BASE(TYPE, SIZE) \
MAKE_BASIC_TYPE_STRING(TYPE, SIZE)
int ndr_basic_integer(ndr_ref_t *, unsigned);
int ndr_string_basic_integer(ndr_ref_t *, ndr_typeinfo_t *);
/* BEGIN CSTYLED */
/* Comments to be nice to those searching for these types. */
MAKE_BASIC_TYPE(_char, 1) /* ndt__char, ndt_s_char */
MAKE_BASIC_TYPE(_uchar, 1) /* ndt__uchar, ndt_s_uchar */
MAKE_BASIC_TYPE(_short, 2) /* ndt__short, ndt_s_short */
MAKE_BASIC_TYPE(_ushort, 2) /* ndt__ushort, ndt_s_ushort */
MAKE_BASIC_TYPE(_long, 4) /* ndt__long, ndt_s_long */
MAKE_BASIC_TYPE(_ulong, 4) /* ndt__ulong, ndt_s_ulong */
MAKE_BASIC_TYPE_BASE(_wchar, 2) /* ndt__wchar, ndt_s_wchar */
/* END CSTYLED */
int
ndr_basic_integer(ndr_ref_t *ref, unsigned size)
{
ndr_stream_t *nds = ref->stream;
char *valp = (char *)ref->datum;
int rc;
switch (nds->m_op) {
case NDR_M_OP_MARSHALL:
rc = NDS_PUT_PDU(nds, ref->pdu_offset, size,
valp, nds->swap, ref);
break;
case NDR_M_OP_UNMARSHALL:
rc = NDS_GET_PDU(nds, ref->pdu_offset, size,
valp, nds->swap, ref);
break;
default:
NDR_SET_ERROR(ref, NDR_ERR_M_OP_INVALID);
return (0);
}
return (rc);
}
int
ndr_string_basic_integer(ndr_ref_t *encl_ref, ndr_typeinfo_t *type_under)
{
unsigned long pdu_offset = encl_ref->pdu_offset;
unsigned size = type_under->pdu_size_fixed_part;
char *valp;
ndr_ref_t myref;
unsigned long i;
long sense = 0;
char name[30];
assert(size != 0);
bzero(&myref, sizeof (myref));
myref.enclosing = encl_ref;
myref.stream = encl_ref->stream;
myref.packed_alignment = 0;
myref.ti = type_under;
myref.inner_flags = NDR_F_NONE;
myref.name = name;
for (i = 0; i < NDR_STRING_MAX; i++) {
(void) snprintf(name, sizeof (name), "[%lu]", i);
myref.pdu_offset = pdu_offset + i * size;
valp = encl_ref->datum + i * size;
myref.datum = valp;
if (!ndr_inner(&myref))
return (0);
switch (size) {
case 1: sense = *valp; break;
/*LINTED E_BAD_PTR_CAST_ALIGN*/
case 2: sense = *(short *)valp; break;
/*LINTED E_BAD_PTR_CAST_ALIGN*/
case 4: sense = *(long *)valp; break;
}
if (!sense)
break;
}
return (1);
}
extern int ndr_s_wchar(ndr_ref_t *encl_ref);
ndr_typeinfo_t ndt_s_wchar = {
1, /* NDR version */
2-1, /* alignment */
NDR_F_STRING, /* flags */
ndr_s_wchar, /* ndr_func */
0, /* pdu_size_fixed_part */
2, /* pdu_size_variable_part */
0, /* c_size_fixed_part */
1, /* c_size_variable_part */
};
/*
* Hand coded wchar function because all strings are transported
* as wide characters. During NDR_M_OP_MARSHALL, we convert from
* multi-byte to wide characters. During NDR_M_OP_UNMARSHALL, we
* convert from wide characters to multi-byte.
*
* The most critical thing to get right in this function is to
* marshall or unmarshall _exactly_ the number of elements the
* OtW length specifies, as saved by the caller in: strlen_is.
* Doing otherwise would leave us positioned at the wrong place
* in the data stream for whatever follows this. Note that the
* string data covered by strlen_is may or may not include any
* null termination, but the converted string provided by the
* caller or returned always has a null terminator.
*/
int
ndr_s_wchar(ndr_ref_t *encl_ref)
{
ndr_stream_t *nds = encl_ref->stream;
char *valp = encl_ref->datum;
ndr_ref_t myref;
char name[30];
ndr_wchar_t wcs[NDR_STRING_MAX+1];
size_t i, slen, wlen;
/* This is enforced in ndr_outer_string() */
assert(encl_ref->strlen_is <= NDR_STRING_MAX);
if (nds->m_op == NDR_M_OP_UNMARSHALL) {
/*
* To avoid problems with zero length strings
* we can just null terminate here and be done.
*/
if (encl_ref->strlen_is == 0) {
encl_ref->datum[0] = '\0';
return (1);
}
}
/*
* If we're marshalling, convert the given string
* from UTF-8 into a local UCS-2 string.
*/
if (nds->m_op == NDR_M_OP_MARSHALL) {
wlen = ndr__mbstowcs(wcs, valp, NDR_STRING_MAX);
if (wlen == (size_t)-1)
return (0);
/*
* Add a nulls to make strlen_is.
* (always zero or one of them)
* Then null terminate at wlen,
* just for debug convenience.
*/
while (wlen < encl_ref->strlen_is)
wcs[wlen++] = 0;
wcs[wlen] = 0;
}
/*
* Copy wire data to or from the local wc string.
* Always exactly strlen_is elements.
*/
bzero(&myref, sizeof (myref));
myref.enclosing = encl_ref;
myref.stream = encl_ref->stream;
myref.packed_alignment = 0;
myref.ti = &ndt__wchar;
myref.inner_flags = NDR_F_NONE;
myref.name = name;
myref.pdu_offset = encl_ref->pdu_offset;
myref.datum = (char *)wcs;
wlen = encl_ref->strlen_is;
for (i = 0; i < wlen; i++) {
(void) snprintf(name, sizeof (name), "[%lu]", i);
if (!ndr_inner(&myref))
return (0);
myref.pdu_offset += sizeof (ndr_wchar_t);
myref.datum += sizeof (ndr_wchar_t);
}
/*
* If this is unmarshall, convert the local UCS-2 string
* into a UTF-8 string in the caller's buffer. The caller
* previously determined the space required and provides a
* buffer of sufficient size.
*/
if (nds->m_op == NDR_M_OP_UNMARSHALL) {
wcs[wlen] = 0;
slen = encl_ref->size_is * NDR_MB_CHAR_MAX;
slen = ndr__wcstombs(valp, wcs, slen);
if (slen == (size_t)-1)
return (0);
valp[slen] = '\0';
}
return (1);
}
/*
* Converts a multibyte character string to a little-endian, wide-char
* string. No more than nwchars wide characters are stored.
* A terminating null wide character is appended if there is room.
*
* Returns the number of wide characters converted, not counting
* any terminating null wide character. Returns -1 if an invalid
* multibyte character is encountered.
*/
/* ARGSUSED */
size_t
ndr_mbstowcs(ndr_stream_t *nds, ndr_wchar_t *wcs, const char *mbs,
size_t nwchars)
{
size_t len;
#ifdef _BIG_ENDIAN
if (nds == NULL || NDR_MODE_MATCH(nds, NDR_MODE_RETURN_SEND)) {
/* Make WC string in LE order. */
len = ndr__mbstowcs_le(wcs, mbs, nwchars);
} else
#endif
len = ndr__mbstowcs(wcs, mbs, nwchars);
return (len);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2015 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 2018, Joyent, Inc.
*/
/*
* Server side RPC handler.
*/
#include <sys/byteorder.h>
#include <sys/uio.h>
#include <errno.h>
#include <synch.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <thread.h>
#include <libmlrpc.h>
#define NDR_PIPE_SEND(np, buf, len) \
((np)->np_send)((np), (buf), (len))
#define NDR_PIPE_RECV(np, buf, len) \
((np)->np_recv)((np), (buf), (len))
static int ndr_svc_process(ndr_xa_t *);
static int ndr_svc_bind(ndr_xa_t *);
static int ndr_svc_request(ndr_xa_t *);
static void ndr_reply_prepare_hdr(ndr_xa_t *);
static int ndr_svc_alter_context(ndr_xa_t *);
static void ndr_reply_fault(ndr_xa_t *, unsigned long);
static int ndr_recv_request(ndr_xa_t *mxa);
static int ndr_recv_frag(ndr_xa_t *mxa);
static int ndr_send_reply(ndr_xa_t *);
static int ndr_pipe_process(ndr_pipe_t *, ndr_xa_t *);
/*
* External entry point called by smbd.
*/
void
ndr_pipe_worker(ndr_pipe_t *np)
{
ndr_xa_t *mxa;
int rc;
ndr_svc_binding_pool_init(&np->np_binding, np->np_binding_pool,
NDR_N_BINDING_POOL);
if ((mxa = malloc(sizeof (*mxa))) == NULL)
return;
do {
bzero(mxa, sizeof (*mxa));
rc = ndr_pipe_process(np, mxa);
} while (rc == 0);
free(mxa);
/*
* Ensure that there are no RPC service policy handles
* (associated with this fid) left around.
*/
ndr_hdclose(np);
}
/*
* Process one server-side RPC request.
*/
static int
ndr_pipe_process(ndr_pipe_t *np, ndr_xa_t *mxa)
{
ndr_stream_t *recv_nds;
ndr_stream_t *send_nds;
int rc = ENOMEM;
mxa->pipe = np;
mxa->binding_list = np->np_binding;
if ((mxa->heap = ndr_heap_create()) == NULL)
goto out1;
recv_nds = &mxa->recv_nds;
rc = nds_initialize(recv_nds, 0, NDR_MODE_CALL_RECV, mxa->heap);
if (rc != 0)
goto out2;
send_nds = &mxa->send_nds;
rc = nds_initialize(send_nds, 0, NDR_MODE_RETURN_SEND, mxa->heap);
if (rc != 0)
goto out3;
rc = ndr_recv_request(mxa);
if (rc != 0)
goto out4;
(void) ndr_svc_process(mxa);
(void) ndr_send_reply(mxa);
rc = 0;
out4:
nds_destruct(&mxa->send_nds);
out3:
nds_destruct(&mxa->recv_nds);
out2:
ndr_heap_destroy(mxa->heap);
out1:
return (rc);
}
/*
* Receive an entire RPC request (all fragments)
* Returns zero or an NDR fault code.
*/
static int
ndr_recv_request(ndr_xa_t *mxa)
{
ndr_common_header_t *hdr = &mxa->recv_hdr.common_hdr;
ndr_stream_t *nds = &mxa->recv_nds;
unsigned long saved_size;
int rc;
rc = ndr_recv_frag(mxa);
if (rc != 0)
return (rc);
if (!NDR_IS_FIRST_FRAG(hdr->pfc_flags))
return (NDR_DRC_FAULT_DECODE_FAILED);
while (!NDR_IS_LAST_FRAG(hdr->pfc_flags)) {
rc = ndr_recv_frag(mxa);
if (rc != 0)
return (rc);
}
nds->pdu_scan_offset = 0;
/*
* This whacks nds->pdu_size, so save/restore.
* It leaves scan_offset after the header.
*/
saved_size = nds->pdu_size;
rc = ndr_decode_pdu_hdr(mxa);
nds->pdu_size = saved_size;
return (rc);
}
/*
* Read one fragment, leaving the decoded frag header in
* recv_hdr.common_hdr, and the data in the recv_nds.
*
* Returns zero or an NDR fault code.
*
* If a first frag, the header is included in the data
* placed in recv_nds (because it's not fully decoded
* until later - we only decode the common part here).
* Additional frags are placed in the recv_nds without
* the header, so that after the first frag header,
* the remaining data will be contiguous. We do this
* by simply not advancing the offset in recv_nds after
* reading and decoding these additional fragments, so
* the payload of such frags will overwrite what was
* (temporarily) the frag header.
*/
static int
ndr_recv_frag(ndr_xa_t *mxa)
{
ndr_common_header_t *hdr = &mxa->recv_hdr.common_hdr;
ndr_stream_t *nds = &mxa->recv_nds;
unsigned char *data;
unsigned long next_offset;
unsigned long pay_size;
int rc;
/* Make room for the frag header. */
next_offset = nds->pdu_scan_offset + NDR_RSP_HDR_SIZE;
if (!NDS_GROW_PDU(nds, next_offset, 0))
return (NDR_DRC_FAULT_OUT_OF_MEMORY);
/* Read the frag header. */
data = nds->pdu_base_addr + nds->pdu_scan_offset;
rc = NDR_PIPE_RECV(mxa->pipe, data, NDR_RSP_HDR_SIZE);
if (rc != 0)
return (NDR_DRC_FAULT_RPCHDR_RECEIVED_RUNT);
/*
* Decode the frag header, get the length.
* NB: It uses nds->pdu_scan_offset
*/
ndr_decode_frag_hdr(nds, hdr);
ndr_show_hdr(hdr);
if (hdr->frag_length < NDR_RSP_HDR_SIZE ||
hdr->frag_length > mxa->pipe->np_max_xmit_frag)
return (NDR_DRC_FAULT_DECODE_FAILED);
if (nds->pdu_scan_offset == 0) {
/* First frag: header stays in the data. */
nds->pdu_scan_offset = next_offset;
} /* else overwrite with the payload */
/* Make room for the payload. */
pay_size = hdr->frag_length - NDR_RSP_HDR_SIZE;
next_offset = nds->pdu_scan_offset + pay_size;
if (!NDS_GROW_PDU(nds, next_offset, 0))
return (NDR_DRC_FAULT_OUT_OF_MEMORY);
/* Read the payload. */
data = nds->pdu_base_addr + nds->pdu_scan_offset;
rc = NDR_PIPE_RECV(mxa->pipe, data, pay_size);
if (rc != 0)
return (NDR_DRC_FAULT_RPCHDR_RECEIVED_RUNT);
nds->pdu_scan_offset = next_offset;
return (NDR_DRC_OK);
}
/*
* This is the entry point for all server-side RPC processing.
* It is assumed that the PDU has already been received.
*/
static int
ndr_svc_process(ndr_xa_t *mxa)
{
int rc;
(void) ndr_reply_prepare_hdr(mxa);
switch (mxa->ptype) {
case NDR_PTYPE_BIND:
rc = ndr_svc_bind(mxa);
break;
case NDR_PTYPE_REQUEST:
rc = ndr_svc_request(mxa);
break;
case NDR_PTYPE_ALTER_CONTEXT:
rc = ndr_svc_alter_context(mxa);
break;
default:
rc = NDR_DRC_FAULT_RPCHDR_PTYPE_INVALID;
break;
}
if (NDR_DRC_IS_FAULT(rc))
ndr_reply_fault(mxa, rc);
return (rc);
}
/*
* Multiple p_cont_elem[]s, multiple transfer_syntaxes[] and multiple
* p_results[] not supported.
*/
static int
ndr_svc_bind(ndr_xa_t *mxa)
{
ndr_p_cont_list_t *cont_list;
ndr_p_result_list_t *result_list;
ndr_p_result_t *result;
unsigned p_cont_id;
ndr_binding_t *mbind;
ndr_uuid_t *as_uuid;
ndr_uuid_t *ts_uuid;
int as_vers;
int ts_vers;
ndr_service_t *msvc;
int rc;
ndr_port_any_t *sec_addr;
/* acquire targets */
cont_list = &mxa->recv_hdr.bind_hdr.p_context_elem;
result_list = &mxa->send_hdr.bind_ack_hdr.p_result_list;
result = &result_list->p_results[0];
/*
* Set up temporary secondary address port.
* We will correct this later (below).
*/
sec_addr = &mxa->send_hdr.bind_ack_hdr.sec_addr;
sec_addr->length = 13;
(void) strcpy((char *)sec_addr->port_spec, "\\PIPE\\ntsvcs");
result_list->n_results = 1;
result_list->reserved = 0;
result_list->reserved2 = 0;
result->result = NDR_PCDR_ACCEPTANCE;
result->reason = 0;
bzero(&result->transfer_syntax, sizeof (result->transfer_syntax));
/* sanity check */
if (cont_list->n_context_elem != 1 ||
cont_list->p_cont_elem[0].n_transfer_syn != 1) {
ndo_trace("ndr_svc_bind: warning: multiple p_cont_elem");
}
p_cont_id = cont_list->p_cont_elem[0].p_cont_id;
if ((mbind = ndr_svc_find_binding(mxa, p_cont_id)) != NULL) {
/*
* Duplicate presentation context id.
*/
ndo_trace("ndr_svc_bind: duplicate binding");
return (NDR_DRC_FAULT_BIND_PCONT_BUSY);
}
if ((mbind = ndr_svc_new_binding(mxa)) == NULL) {
/*
* No free binding slot
*/
result->result = NDR_PCDR_PROVIDER_REJECTION;
result->reason = NDR_PPR_LOCAL_LIMIT_EXCEEDED;
ndo_trace("ndr_svc_bind: no resources");
return (NDR_DRC_OK);
}
as_uuid = &cont_list->p_cont_elem[0].abstract_syntax.if_uuid;
as_vers = cont_list->p_cont_elem[0].abstract_syntax.if_version;
ts_uuid = &cont_list->p_cont_elem[0].transfer_syntaxes[0].if_uuid;
ts_vers = cont_list->p_cont_elem[0].transfer_syntaxes[0].if_version;
msvc = ndr_svc_lookup_uuid(as_uuid, as_vers, ts_uuid, ts_vers);
if (msvc == NULL) {
result->result = NDR_PCDR_PROVIDER_REJECTION;
result->reason = NDR_PPR_ABSTRACT_SYNTAX_NOT_SUPPORTED;
return (NDR_DRC_OK);
}
/*
* We can now use the correct secondary address port.
*/
sec_addr = &mxa->send_hdr.bind_ack_hdr.sec_addr;
sec_addr->length = strlen(msvc->sec_addr_port) + 1;
(void) strlcpy((char *)sec_addr->port_spec, msvc->sec_addr_port,
NDR_PORT_ANY_MAX_PORT_SPEC);
mbind->p_cont_id = p_cont_id;
mbind->which_side = NDR_BIND_SIDE_SERVER;
/* mbind->context set by app */
mbind->service = msvc;
mbind->instance_specific = 0;
mxa->binding = mbind;
if (msvc->bind_req) {
/*
* Call the service-specific bind() handler. If
* this fails, we shouild send a specific error
* on the bind ack.
*/
rc = (msvc->bind_req)(mxa);
if (NDR_DRC_IS_FAULT(rc)) {
mbind->service = 0; /* free binding slot */
mbind->which_side = 0;
mbind->p_cont_id = 0;
mbind->instance_specific = 0;
return (rc);
}
}
result->transfer_syntax =
cont_list->p_cont_elem[0].transfer_syntaxes[0];
return (NDR_DRC_BINDING_MADE);
}
/*
* ndr_svc_alter_context
*
* The alter context request is used to request additional presentation
* context for another interface and/or version. It is very similar to
* a bind request.
*/
static int
ndr_svc_alter_context(ndr_xa_t *mxa)
{
ndr_p_result_list_t *result_list;
ndr_p_result_t *result;
ndr_p_cont_list_t *cont_list;
ndr_binding_t *mbind;
ndr_service_t *msvc;
unsigned p_cont_id;
ndr_uuid_t *as_uuid;
ndr_uuid_t *ts_uuid;
int as_vers;
int ts_vers;
ndr_port_any_t *sec_addr;
result_list = &mxa->send_hdr.alter_context_rsp_hdr.p_result_list;
result_list->n_results = 1;
result_list->reserved = 0;
result_list->reserved2 = 0;
result = &result_list->p_results[0];
result->result = NDR_PCDR_ACCEPTANCE;
result->reason = 0;
bzero(&result->transfer_syntax, sizeof (result->transfer_syntax));
cont_list = &mxa->recv_hdr.alter_context_hdr.p_context_elem;
p_cont_id = cont_list->p_cont_elem[0].p_cont_id;
if (ndr_svc_find_binding(mxa, p_cont_id) != NULL)
return (NDR_DRC_FAULT_BIND_PCONT_BUSY);
if ((mbind = ndr_svc_new_binding(mxa)) == NULL) {
result->result = NDR_PCDR_PROVIDER_REJECTION;
result->reason = NDR_PPR_LOCAL_LIMIT_EXCEEDED;
return (NDR_DRC_OK);
}
as_uuid = &cont_list->p_cont_elem[0].abstract_syntax.if_uuid;
as_vers = cont_list->p_cont_elem[0].abstract_syntax.if_version;
ts_uuid = &cont_list->p_cont_elem[0].transfer_syntaxes[0].if_uuid;
ts_vers = cont_list->p_cont_elem[0].transfer_syntaxes[0].if_version;
msvc = ndr_svc_lookup_uuid(as_uuid, as_vers, ts_uuid, ts_vers);
if (msvc == NULL) {
result->result = NDR_PCDR_PROVIDER_REJECTION;
result->reason = NDR_PPR_ABSTRACT_SYNTAX_NOT_SUPPORTED;
return (NDR_DRC_OK);
}
mbind->p_cont_id = p_cont_id;
mbind->which_side = NDR_BIND_SIDE_SERVER;
/* mbind->context set by app */
mbind->service = msvc;
mbind->instance_specific = 0;
mxa->binding = mbind;
sec_addr = &mxa->send_hdr.alter_context_rsp_hdr.sec_addr;
sec_addr->length = 0;
bzero(sec_addr->port_spec, NDR_PORT_ANY_MAX_PORT_SPEC);
result->transfer_syntax =
cont_list->p_cont_elem[0].transfer_syntaxes[0];
return (NDR_DRC_BINDING_MADE);
}
static int
ndr_svc_request(ndr_xa_t *mxa)
{
ndr_binding_t *mbind;
ndr_service_t *msvc;
unsigned p_cont_id;
int rc;
mxa->opnum = mxa->recv_hdr.request_hdr.opnum;
p_cont_id = mxa->recv_hdr.request_hdr.p_cont_id;
if ((mbind = ndr_svc_find_binding(mxa, p_cont_id)) == NULL)
return (NDR_DRC_FAULT_REQUEST_PCONT_INVALID);
mxa->binding = mbind;
msvc = mbind->service;
/*
* Make room for the response hdr.
*/
mxa->send_nds.pdu_scan_offset = NDR_RSP_HDR_SIZE;
if (msvc->call_stub)
rc = (*msvc->call_stub)(mxa);
else
rc = ndr_generic_call_stub(mxa);
if (NDR_DRC_IS_FAULT(rc)) {
ndo_printf(0, 0, "%s[0x%02x]: 0x%04x",
msvc->name, mxa->opnum, rc);
}
return (rc);
}
/*
* The transaction and the two nds streams use the same heap, which
* should already exist at this point. The heap will also be available
* to the stub.
*/
int
ndr_generic_call_stub(ndr_xa_t *mxa)
{
ndr_binding_t *mbind = mxa->binding;
ndr_service_t *msvc = mbind->service;
ndr_typeinfo_t *intf_ti = msvc->interface_ti;
ndr_stub_table_t *ste;
int opnum = mxa->opnum;
unsigned p_len = intf_ti->c_size_fixed_part;
char *param;
int rc;
if (mxa->heap == NULL) {
ndo_printf(0, 0, "%s[0x%02x]: no heap", msvc->name, opnum);
return (NDR_DRC_FAULT_OUT_OF_MEMORY);
}
if ((ste = ndr_svc_find_stub(msvc, opnum)) == NULL) {
ndo_printf(0, 0, "%s[0x%02x]: invalid opnum",
msvc->name, opnum);
return (NDR_DRC_FAULT_REQUEST_OPNUM_INVALID);
}
if ((param = ndr_heap_malloc(mxa->heap, p_len)) == NULL)
return (NDR_DRC_FAULT_OUT_OF_MEMORY);
bzero(param, p_len);
rc = ndr_decode_call(mxa, param);
if (!NDR_DRC_IS_OK(rc))
return (rc);
rc = (*ste->func)(param, mxa);
if (rc == NDR_DRC_OK)
rc = ndr_encode_return(mxa, param);
return (rc);
}
/*
* We can perform some initial setup of the response header here.
* We also need to cache some of the information from the bind
* negotiation for use during subsequent RPC calls.
*/
static void
ndr_reply_prepare_hdr(ndr_xa_t *mxa)
{
ndr_common_header_t *rhdr = &mxa->recv_hdr.common_hdr;
ndr_common_header_t *hdr = &mxa->send_hdr.common_hdr;
hdr->rpc_vers = 5;
hdr->rpc_vers_minor = 0;
hdr->pfc_flags = NDR_PFC_FIRST_FRAG + NDR_PFC_LAST_FRAG;
hdr->packed_drep = rhdr->packed_drep;
hdr->frag_length = 0;
hdr->auth_length = 0;
hdr->call_id = rhdr->call_id;
#ifdef _BIG_ENDIAN
hdr->packed_drep.intg_char_rep = NDR_REPLAB_CHAR_ASCII
| NDR_REPLAB_INTG_BIG_ENDIAN;
#else
hdr->packed_drep.intg_char_rep = NDR_REPLAB_CHAR_ASCII
| NDR_REPLAB_INTG_LITTLE_ENDIAN;
#endif
switch (mxa->ptype) {
case NDR_PTYPE_BIND:
/*
* Compute the maximum fragment sizes for xmit/recv
* and store in the pipe endpoint. Note "xmit" is
* client-to-server; "recv" is server-to-client.
*/
if (mxa->pipe->np_max_xmit_frag >
mxa->recv_hdr.bind_hdr.max_xmit_frag)
mxa->pipe->np_max_xmit_frag =
mxa->recv_hdr.bind_hdr.max_xmit_frag;
if (mxa->pipe->np_max_recv_frag >
mxa->recv_hdr.bind_hdr.max_recv_frag)
mxa->pipe->np_max_recv_frag =
mxa->recv_hdr.bind_hdr.max_recv_frag;
hdr->ptype = NDR_PTYPE_BIND_ACK;
mxa->send_hdr.bind_ack_hdr.max_xmit_frag =
mxa->pipe->np_max_xmit_frag;
mxa->send_hdr.bind_ack_hdr.max_recv_frag =
mxa->pipe->np_max_recv_frag;
/*
* We're supposed to assign a unique "assoc group"
* (identifies this connection for the client).
* Using the pipe address is adequate.
*/
mxa->send_hdr.bind_ack_hdr.assoc_group_id =
mxa->recv_hdr.bind_hdr.assoc_group_id;
if (mxa->send_hdr.bind_ack_hdr.assoc_group_id == 0)
mxa->send_hdr.bind_ack_hdr.assoc_group_id =
(DWORD)(uintptr_t)mxa->pipe;
break;
case NDR_PTYPE_REQUEST:
hdr->ptype = NDR_PTYPE_RESPONSE;
/* mxa->send_hdr.response_hdr.alloc_hint */
mxa->send_hdr.response_hdr.p_cont_id =
mxa->recv_hdr.request_hdr.p_cont_id;
mxa->send_hdr.response_hdr.cancel_count = 0;
mxa->send_hdr.response_hdr.reserved = 0;
break;
case NDR_PTYPE_ALTER_CONTEXT:
hdr->ptype = NDR_PTYPE_ALTER_CONTEXT_RESP;
/*
* The max_xmit_frag, max_recv_frag and assoc_group_id are
* ignored by the client but it's useful to fill them in.
*/
mxa->send_hdr.alter_context_rsp_hdr.max_xmit_frag =
mxa->recv_hdr.alter_context_hdr.max_xmit_frag;
mxa->send_hdr.alter_context_rsp_hdr.max_recv_frag =
mxa->recv_hdr.alter_context_hdr.max_recv_frag;
mxa->send_hdr.alter_context_rsp_hdr.assoc_group_id =
mxa->recv_hdr.alter_context_hdr.assoc_group_id;
break;
default:
hdr->ptype = 0xFF;
}
}
/*
* Signal an RPC fault. The stream is reset and we overwrite whatever
* was in the response header with the fault information.
*/
static void
ndr_reply_fault(ndr_xa_t *mxa, unsigned long drc)
{
ndr_common_header_t *rhdr = &mxa->recv_hdr.common_hdr;
ndr_common_header_t *hdr = &mxa->send_hdr.common_hdr;
ndr_stream_t *nds = &mxa->send_nds;
unsigned long fault_status;
(void) NDS_RESET(nds);
hdr->rpc_vers = 5;
hdr->rpc_vers_minor = 0;
hdr->pfc_flags = NDR_PFC_FIRST_FRAG + NDR_PFC_LAST_FRAG;
hdr->packed_drep = rhdr->packed_drep;
hdr->frag_length = sizeof (mxa->send_hdr.fault_hdr);
hdr->auth_length = 0;
hdr->call_id = rhdr->call_id;
#ifdef _BIG_ENDIAN
hdr->packed_drep.intg_char_rep = NDR_REPLAB_CHAR_ASCII
| NDR_REPLAB_INTG_BIG_ENDIAN;
#else
hdr->packed_drep.intg_char_rep = NDR_REPLAB_CHAR_ASCII
| NDR_REPLAB_INTG_LITTLE_ENDIAN;
#endif
switch (drc & NDR_DRC_MASK_SPECIFIER) {
case NDR_DRC_FAULT_OUT_OF_MEMORY:
case NDR_DRC_FAULT_ENCODE_TOO_BIG:
fault_status = NDR_FAULT_NCA_OUT_ARGS_TOO_BIG;
break;
case NDR_DRC_FAULT_REQUEST_PCONT_INVALID:
fault_status = NDR_FAULT_NCA_INVALID_PRES_CONTEXT_ID;
break;
case NDR_DRC_FAULT_REQUEST_OPNUM_INVALID:
fault_status = NDR_FAULT_NCA_OP_RNG_ERROR;
break;
case NDR_DRC_FAULT_DECODE_FAILED:
case NDR_DRC_FAULT_ENCODE_FAILED:
fault_status = NDR_FAULT_NCA_PROTO_ERROR;
break;
default:
fault_status = NDR_FAULT_NCA_UNSPEC_REJECT;
break;
}
mxa->send_hdr.fault_hdr.common_hdr.ptype = NDR_PTYPE_FAULT;
mxa->send_hdr.fault_hdr.status = fault_status;
mxa->send_hdr.response_hdr.alloc_hint = hdr->frag_length;
}
/*
* Note that the frag_length for bind ack and alter context is
* non-standard.
*/
static int
ndr_send_reply(ndr_xa_t *mxa)
{
ndr_common_header_t *hdr = &mxa->send_hdr.common_hdr;
ndr_stream_t *nds = &mxa->send_nds;
uint8_t *pdu_buf;
unsigned long pdu_size;
unsigned long frag_size;
unsigned long pdu_data_size;
unsigned long frag_data_size;
frag_size = mxa->pipe->np_max_recv_frag;
pdu_size = nds->pdu_size;
pdu_buf = nds->pdu_base_addr;
if (pdu_size <= frag_size) {
/*
* Single fragment response. The PDU size may be zero
* here (i.e. bind or fault response). So don't make
* any assumptions about it until after the header is
* encoded.
*/
switch (hdr->ptype) {
case NDR_PTYPE_BIND_ACK:
hdr->frag_length = ndr_bind_ack_hdr_size(mxa);
break;
case NDR_PTYPE_FAULT:
/* already setup */
break;
case NDR_PTYPE_RESPONSE:
hdr->frag_length = pdu_size;
mxa->send_hdr.response_hdr.alloc_hint =
hdr->frag_length;
break;
case NDR_PTYPE_ALTER_CONTEXT_RESP:
hdr->frag_length = ndr_alter_context_rsp_hdr_size();
break;
default:
hdr->frag_length = pdu_size;
break;
}
nds->pdu_scan_offset = 0;
(void) ndr_encode_pdu_hdr(mxa);
pdu_size = nds->pdu_size;
(void) NDR_PIPE_SEND(mxa->pipe, pdu_buf, pdu_size);
return (0);
}
/*
* Multiple fragment response.
*
* We need to update the RPC header for every fragment.
*
* pdu_data_size: total data remaining to be handled
* frag_size: total fragment size including header
* frag_data_size: data in fragment
* (i.e. frag_size - NDR_RSP_HDR_SIZE)
*/
pdu_data_size = pdu_size - NDR_RSP_HDR_SIZE;
frag_data_size = frag_size - NDR_RSP_HDR_SIZE;
/*
* Send the first frag.
*/
hdr->pfc_flags = NDR_PFC_FIRST_FRAG;
hdr->frag_length = frag_size;
mxa->send_hdr.response_hdr.alloc_hint = pdu_data_size;
nds->pdu_scan_offset = 0;
(void) ndr_encode_pdu_hdr(mxa);
(void) NDR_PIPE_SEND(mxa->pipe, pdu_buf, frag_size);
pdu_data_size -= frag_data_size;
pdu_buf += frag_data_size;
/*
* Send "middle" (full-sized) fragments...
*/
hdr->pfc_flags = 0;
while (pdu_data_size > frag_data_size) {
hdr->frag_length = frag_size;
mxa->send_hdr.response_hdr.alloc_hint = pdu_data_size;
nds->pdu_scan_offset = 0;
(void) ndr_encode_pdu_hdr(mxa);
bcopy(nds->pdu_base_addr, pdu_buf, NDR_RSP_HDR_SIZE);
(void) NDR_PIPE_SEND(mxa->pipe, pdu_buf, frag_size);
pdu_data_size -= frag_data_size;
pdu_buf += frag_data_size;
}
/*
* Last frag (pdu_data_size <= frag_data_size)
*/
hdr->pfc_flags = NDR_PFC_LAST_FRAG;
frag_size = pdu_data_size + NDR_RSP_HDR_SIZE;
hdr->frag_length = frag_size;
mxa->send_hdr.response_hdr.alloc_hint = pdu_data_size;
nds->pdu_scan_offset = 0;
(void) ndr_encode_pdu_hdr(mxa);
bcopy(nds->pdu_base_addr, pdu_buf, NDR_RSP_HDR_SIZE);
(void) NDR_PIPE_SEND(mxa->pipe, pdu_buf, frag_size);
return (0);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2013 Nexenta Systems, Inc. All rights reserved.
*/
#include <uuid/uuid.h>
#include <ctype.h>
#include <synch.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include <libmlrpc.h>
/*
* Global list of allocated handles. Handles are used in various
* server-side RPC functions: typically, issued when a service is
* opened and obsoleted when it is closed. Clients should treat
* handles as opaque data.
*/
static ndr_handle_t *ndr_handle_list;
static mutex_t ndr_handle_lock;
/*
* Table of registered services.
*/
#define NDR_MAX_SERVICES 32
static ndr_service_t *ndr_services[NDR_MAX_SERVICES];
/*
* Register a service.
*
* Returns:
* 0 Success
* -1 Duplicate service
* -2 Duplicate name
* -3 Table overflow
*/
int
ndr_svc_register(ndr_service_t *svc)
{
ndr_service_t *p;
int free_slot = -1;
int i;
for (i = 0; i < NDR_MAX_SERVICES; i++) {
if ((p = ndr_services[i]) == NULL) {
if (free_slot < 0)
free_slot = i;
continue;
}
if (p == svc)
return (-1);
if (strcasecmp(p->name, svc->name) == 0)
return (-2);
}
if (free_slot < 0)
return (-3);
ndr_services[free_slot] = svc;
return (0);
}
void
ndr_svc_unregister(ndr_service_t *svc)
{
int i;
for (i = 0; i < NDR_MAX_SERVICES; i++) {
if (ndr_services[i] == svc)
ndr_services[i] = NULL;
}
}
ndr_stub_table_t *
ndr_svc_find_stub(ndr_service_t *svc, int opnum)
{
ndr_stub_table_t *ste;
for (ste = svc->stub_table; ste->func; ste++) {
if (ste->opnum == opnum)
return (ste);
}
return (NULL);
}
ndr_service_t *
ndr_svc_lookup_name(const char *name)
{
ndr_service_t *svc;
int i;
for (i = 0; i < NDR_MAX_SERVICES; i++) {
if ((svc = ndr_services[i]) == NULL)
continue;
if (strcasecmp(name, svc->name) != 0)
continue;
ndo_printf(0, 0, "%s %s", svc->name, svc->desc);
return (svc);
}
return (NULL);
}
ndr_service_t *
ndr_svc_lookup_uuid(ndr_uuid_t *as_uuid, int as_vers,
ndr_uuid_t *ts_uuid, int ts_vers)
{
ndr_service_t *svc;
char abstract_syntax[UUID_PRINTABLE_STRING_LENGTH];
char transfer_syntax[UUID_PRINTABLE_STRING_LENGTH];
int i;
if (as_uuid)
ndr_uuid_unparse(as_uuid, abstract_syntax);
if (ts_uuid)
ndr_uuid_unparse(ts_uuid, transfer_syntax);
for (i = 0; i < NDR_MAX_SERVICES; i++) {
if ((svc = ndr_services[i]) == NULL)
continue;
if (as_uuid) {
if (svc->abstract_syntax_uuid == 0)
continue;
if (svc->abstract_syntax_version != as_vers)
continue;
if (strcasecmp(abstract_syntax,
svc->abstract_syntax_uuid))
continue;
}
if (ts_uuid) {
if (svc->transfer_syntax_uuid == 0)
continue;
if (svc->transfer_syntax_version != ts_vers)
continue;
if (strcasecmp(transfer_syntax,
svc->transfer_syntax_uuid))
continue;
}
ndo_printf(0, 0, "%s %s", svc->name, svc->desc);
return (svc);
}
ndo_printf(0, 0, "ndr_svc_lookup_uuid: unknown service");
ndo_printf(0, 0, "abstract=%s v%d, transfer=%s v%d",
abstract_syntax, as_vers, transfer_syntax, ts_vers);
return (NULL);
}
/*
* Allocate a handle for use with the server-side RPC functions.
*
* An arbitrary caller context can be associated with the handle
* via data; it will not be dereferenced by the handle API.
*/
ndr_hdid_t *
ndr_hdalloc(const ndr_xa_t *xa, const void *data)
{
static ndr_hdid_t id;
ndr_handle_t *hd;
uuid_t uu;
if ((hd = malloc(sizeof (ndr_handle_t))) == NULL)
return (NULL);
if (id.data2 == 0) {
uuid_generate_random(uu);
bcopy(uu, &id.data2, sizeof (uuid_t));
id.data1 = 0;
id.data2 = 0;
}
++id.data2;
bcopy(&id, &hd->nh_id, sizeof (ndr_hdid_t));
hd->nh_pipe = xa->pipe;
hd->nh_svc = xa->binding->service;
hd->nh_data = (void *)data;
hd->nh_data_free = NULL;
(void) mutex_lock(&ndr_handle_lock);
hd->nh_next = ndr_handle_list;
ndr_handle_list = hd;
(void) mutex_unlock(&ndr_handle_lock);
return (&hd->nh_id);
}
/*
* Remove a handle from the global list and free it.
*/
void
ndr_hdfree(const ndr_xa_t *xa, const ndr_hdid_t *id)
{
ndr_service_t *svc = xa->binding->service;
ndr_handle_t *hd;
ndr_handle_t **pphd;
assert(id);
(void) mutex_lock(&ndr_handle_lock);
pphd = &ndr_handle_list;
while (*pphd) {
hd = *pphd;
if (bcmp(&hd->nh_id, id, sizeof (ndr_hdid_t)) == 0) {
if (hd->nh_svc == svc) {
*pphd = hd->nh_next;
free(hd);
}
break;
}
pphd = &(*pphd)->nh_next;
}
(void) mutex_unlock(&ndr_handle_lock);
}
/*
* Lookup a handle by id. If the handle is in the list and it matches
* the specified service, a pointer to it is returned. Otherwise a null
* pointer is returned.
*/
ndr_handle_t *
ndr_hdlookup(const ndr_xa_t *xa, const ndr_hdid_t *id)
{
ndr_service_t *svc = xa->binding->service;
ndr_handle_t *hd;
assert(id);
(void) mutex_lock(&ndr_handle_lock);
hd = ndr_handle_list;
while (hd) {
if (bcmp(&hd->nh_id, id, sizeof (ndr_hdid_t)) == 0) {
if (hd->nh_svc != svc)
break;
(void) mutex_unlock(&ndr_handle_lock);
return (hd);
}
hd = hd->nh_next;
}
(void) mutex_unlock(&ndr_handle_lock);
return (NULL);
}
/*
* Called when a pipe is closed to release any associated handles.
*/
void
ndr_hdclose(ndr_pipe_t *pipe)
{
ndr_handle_t *hd;
ndr_handle_t **pphd;
(void) mutex_lock(&ndr_handle_lock);
pphd = &ndr_handle_list;
while (*pphd) {
hd = *pphd;
if (hd->nh_pipe == pipe) {
*pphd = hd->nh_next;
if (hd->nh_data_free)
(*hd->nh_data_free)(hd->nh_data);
free(hd);
continue;
}
pphd = &(*pphd)->nh_next;
}
(void) mutex_unlock(&ndr_handle_lock);
}
/*
* Convert a UUID to a string.
*/
void
ndr_uuid_unparse(ndr_uuid_t *uuid, char *out)
{
(void) sprintf(out, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
uuid->data1, uuid->data2, uuid->data3,
uuid->data4[0], uuid->data4[1],
uuid->data4[2], uuid->data4[3],
uuid->data4[4], uuid->data4[5],
uuid->data4[6], uuid->data4[7]);
}
/*
* Convert a string to a UUID.
*/
int
ndr_uuid_parse(char *in, ndr_uuid_t *uuid)
{
char *p = in;
char *q;
char buf[4];
int i;
if (strlen(in) != UUID_PRINTABLE_STRING_LENGTH - 1)
return (-1);
uuid->data1 = strtoul(p, &p, 16);
if (*p != '-')
return (-1);
p++;
uuid->data2 = strtol(p, &p, 16);
if (*p != '-')
return (-1);
p++;
uuid->data3 = strtol(p, &p, 16);
if (*p != '-')
return (-1);
p++;
for (i = 0; i < 8; i++) {
if (*p == '-')
p++;
if (p[0] == 0 || p[1] == 0)
return (-1);
buf[0] = *p++;
buf[1] = *p++;
buf[2] = 0;
uuid->data4[i] = strtol(buf, &q, 16);
if (*q != 0)
return (-1);
}
if (*p != 0)
return (-1);
return (0);
}
void
ndr_svc_binding_pool_init(ndr_binding_t **headpp, ndr_binding_t pool[],
int n_pool)
{
ndr_binding_t *head = NULL;
int ix;
for (ix = n_pool - 1; ix >= 0; ix--) {
pool[ix].next = head;
pool[ix].service = NULL;
pool[ix].p_cont_id = 0xffff;
pool[ix].instance_specific = 0;
head = &pool[ix];
}
*headpp = head;
}
ndr_binding_t *
ndr_svc_find_binding(ndr_xa_t *mxa, ndr_p_context_id_t p_cont_id)
{
ndr_binding_t *mbind;
for (mbind = mxa->binding_list; mbind; mbind = mbind->next) {
if (mbind->service != NULL &&
mbind->which_side == NDR_BIND_SIDE_SERVER &&
mbind->p_cont_id == p_cont_id)
break;
}
return (mbind);
}
ndr_binding_t *
ndr_svc_new_binding(ndr_xa_t *mxa)
{
ndr_binding_t *mbind;
for (mbind = mxa->binding_list; mbind; mbind = mbind->next) {
if (mbind->service == NULL)
break;
}
return (mbind);
}
/*
* Move bytes between a buffer and a uio structure.
* The transfer direction is controlled by rw:
* UIO_READ: transfer from buf to uio
* UIO_WRITE: transfer from uio to buf
*
* Returns the number of bytes moved.
*/
ssize_t
ndr_uiomove(caddr_t buf, size_t buflen, enum uio_rw rw, struct uio *uio)
{
struct iovec *iov;
int reading = (rw == UIO_READ);
size_t nbytes;
size_t nxfer = 0;
assert(rw == UIO_READ || rw == UIO_WRITE);
while (buflen && uio->uio_resid && uio->uio_iovcnt) {
iov = uio->uio_iov;
if ((nbytes = iov->iov_len) == 0) {
uio->uio_iov++;
uio->uio_iovcnt--;
continue;
}
if (nbytes > buflen)
nbytes = buflen;
if (reading)
bcopy(buf, iov->iov_base, nbytes);
else
bcopy(iov->iov_base, buf, nbytes);
iov->iov_base += nbytes;
iov->iov_len -= nbytes;
uio->uio_resid -= nbytes;
uio->uio_offset += nbytes;
buf += nbytes;
buflen -= nbytes;
nxfer += nbytes;
}
return (nxfer);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2013 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Some wchar support functions used by this library.
* Mostlly just wrappers that call sys/u8_textprep.h
* functions: uconv_u8tou16, uconv_u16tou8.
*/
#include <sys/types.h>
#include <sys/u8_textprep.h>
#include <string.h>
#include "ndr_wchar.h"
/*
* When we just want lengths, we need an output buffer to pass to the
* uconv_... functions. Nothing ever reads this output, so we can
* use shared space for the unwanted output.
*/
static uint16_t junk_wcs[NDR_STRING_MAX];
static char junk_mbs[NDR_MB_CUR_MAX * NDR_STRING_MAX];
static size_t
ndr__mbstowcs_x(uint16_t *, const char *, size_t, int);
/*
* Like mbstowcs(3C), but with UCS-2 wchar_t
*/
size_t
ndr__mbstowcs(uint16_t *wcs, const char *mbs, size_t nwchars)
{
return (ndr__mbstowcs_x(wcs, mbs, nwchars,
UCONV_OUT_SYSTEM_ENDIAN));
}
/*
* Like above, but put UCS-2 little-endian.
*/
size_t
ndr__mbstowcs_le(uint16_t *wcs, const char *mbs, size_t nwchars)
{
return (ndr__mbstowcs_x(wcs, mbs, nwchars,
UCONV_OUT_LITTLE_ENDIAN));
}
/*
* Like mbstowcs(3C), but with UCS-2 wchar_t, and
* one extra arg for the byte order flags.
*/
static size_t
ndr__mbstowcs_x(uint16_t *wcs, const char *mbs, size_t nwchars, int flags)
{
size_t obytes, mbslen, wcslen;
int err;
/* NULL or empty input is allowed. */
if (mbs == NULL || *mbs == '\0') {
if (wcs != NULL && nwchars > 0)
*wcs = 0;
return (0);
}
/*
* If wcs == NULL, caller just wants the length.
* Convert into some throw-away space.
*/
obytes = nwchars * 2;
if (wcs == NULL) {
if (obytes > sizeof (junk_wcs))
return ((size_t)-1);
wcs = junk_wcs;
}
mbslen = strlen(mbs);
wcslen = nwchars;
err = uconv_u8tou16((const uchar_t *)mbs, &mbslen,
wcs, &wcslen, flags);
if (err != 0)
return ((size_t)-1);
if (wcslen < nwchars)
wcs[wcslen] = 0;
return (wcslen);
}
/*
* Like wcstombs(3C), but with UCS-2 wchar_t.
*/
size_t
ndr__wcstombs(char *mbs, const uint16_t *wcs, size_t nbytes)
{
size_t mbslen, wcslen;
int err;
/* NULL or empty input is allowed. */
if (wcs == NULL || *wcs == 0) {
if (mbs != NULL && nbytes > 0)
*mbs = '\0';
return (0);
}
/*
* If mbs == NULL, caller just wants the length.
* Convert into some throw-away space.
*/
if (mbs == NULL) {
if (nbytes > sizeof (junk_mbs))
return ((size_t)-1);
mbs = junk_mbs;
}
wcslen = ndr__wcslen(wcs);
mbslen = nbytes;
err = uconv_u16tou8(wcs, &wcslen,
(uchar_t *)mbs, &mbslen, UCONV_IN_SYSTEM_ENDIAN);
if (err != 0)
return ((size_t)-1);
if (mbslen < nbytes)
mbs[mbslen] = '\0';
return (mbslen);
}
/*
* Like wcslen(3C), but with UCS-2 wchar_t.
*/
size_t
ndr__wcslen(const uint16_t *wc)
{
size_t len = 0;
while (*wc++)
len++;
return (len);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2013 Nexenta Systems, Inc. All rights reserved.
*/
#ifndef _NDR_WCHAR_H
#define _NDR_WCHAR_H
/*
* Some ndr_wchar_t support stuff.
*/
#define NDR_MB_CUR_MAX 3
#define NDR_MB_CHAR_MAX NDR_MB_CUR_MAX
#define NDR_STRING_MAX 4096
size_t ndr__mbstowcs(uint16_t *, const char *, size_t);
size_t ndr__mbstowcs_le(uint16_t *, const char *, size_t);
size_t ndr__wcslen(const uint16_t *);
size_t ndr__wcstombs(char *, const uint16_t *, size_t);
#endif /* _NDR_WCHAR_H */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2020 Tintri by DDN, Inc. All rights reserved.
*/
#ifndef _NDRTYPES_NDL_
#define _NDRTYPES_NDL_
/*
* Type definitions (and related) used in NDL files and the
* NDL run-time support libraries. See also: libmlrpc.h
*/
#define TYPEINFO(TYPE) ndt__##TYPE
#ifdef NDRGEN
#define ALIGN(X) [align(X)]
#define OPERATION(X) [operation(X)]
#define IN [in]
#define OUT [out]
#define INOUT [in out]
#define FAKE [fake]
#define STRING [string]
#define SIZE_IS(X) [size_is(X)]
#define SWITCH(X) [switch_is(X)]
#define CASE(X) [case(X)]
#define DEFAULT [default]
#define INTERFACE(X) [interface(X)]
#define UUID(X) [uuid(X)]
#define ARG_IS(X) [arg_is(X)]
#define REFERENCE [reference]
#define REF [reference]
#define UNIQUE [unique]
#define PTR [ptr]
#define POINTER_DEFAULT(X) [pointer_default(X)]
#define ANY_SIZE_ARRAY *
#define IMPORT_EXTERN [extern]
#define BYTE uchar
#define WORD ushort
#define DWORD ulong
#define ntstatus_t ulong
#define LPTSTR STRING wchar *
#define LPBYTE uchar *
#define LPWORD ushort *
#define LPDWORD ulong *
#define EXTERNTYPEINFO(TYPE)
#else /* NDRGEN */
#define ALIGN(X)
#define OPERATION(X)
#define IN
#define OUT
#define INOUT
#define FAKE
#define STRING
#define SIZE_IS(X)
#define SWITCH(X)
#define CASE(X)
#define DEFAULT
#define INTERFACE(X)
#define UUID(X)
#define ARG_IS(X)
#define REFERENCE
#define REF
#define UNIQUE
#define PTR
#define POINTER_DEFAULT(X)
#define IMPORT_EXTERN
/*
* When not using ndrgen, get BYTE, WORD, DWORD definitions from wintypes.h.
*/
#include <smb/wintypes.h>
#define EXTERNTYPEINFO(TYPE) extern struct ndr_typeinfo TYPEINFO(TYPE);
/*
***********************************************************************
* There is a bug in the way that midl and the marshalling code handles
* unions so we need to fix some of the data offsets at runtime. The
* following macros and the fixup function handle the correction.
***********************************************************************
*/
/*
* DECL_FIXUP_STRUCT allows us to declare external references to data
* structures generated by ndrgen in the _ndr.c file.
*/
#define DECL_FIXUP_STRUCT(NAME) extern struct ndr_typeinfo ndt__##NAME
/*
* CASE_INFO_ENT is intended to simplify the declaration of the case
* statement in the fixup function. Assuming you have followed the
* convention for naming the individual structures all you have to do
* is add a single line to the fixup function for each new case.
*/
#define CASE_INFO_ENT(NAME,N) \
case N: size1 = sizeof (struct NAME##N); \
break
/*
* FIXUP_PDU_SIZE is used to patch the appropriate structures (identified
* by DECL_FIXUP_STRUCT) at runtime. The values are based on the
* switch_index.
*/
#define FIXUP_PDU_SIZE(NAME,SIZE) { \
ndt__##NAME.pdu_size_fixed_part = SIZE; \
ndt__##NAME.c_size_fixed_part = SIZE; \
}
#endif /* NDRGEN */
/*
* UNION_INFO_ENT is intended to simplify adding new entries to a union.
* If the entry structures are named using the form FunctionNameX,
* where X is the sitch_value, you can just add a single line. Note
* that you must also update the fixup function in mlsvc_xxx.c.
*/
#define UNION_INFO_ENT(N,NAME) CASE(N) struct NAME##N info##N
#define UNION_INFO_PTR(N,NAME) CASE(N) struct NAME##N *info##N
/*
* Opaque context handle.
*/
#ifndef CONTEXT_HANDLE
#define CONTEXT_HANDLE(NAME) \
struct NAME { \
DWORD data1; \
DWORD data2; \
WORD data3[2]; \
BYTE data4[8]; \
}; \
typedef struct NAME
#endif /* CONTEXT_HANDLE */
#endif /* _NDRTYPES_NDL_ */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2021 Tintri by DDN, Inc. All rights reserved.
*/
#ifndef _RPCPDU_NDL_
#define _RPCPDU_NDL_
#include "ndrtypes.ndl"
/*
* Normally, constructs are (un)marshalled atoms first, then
* constructs, then pointers. This can be confusing sometimes
* when debugging. We know that everything in here can be
* safely (un)marshalled in member order, so we say so.
*/
#ifdef NDRGEN
#define _NO_REORDER_ [_no_reorder]
#else
#define _NO_REORDER_
#endif
#define NDR_TRANSFER_SYNTAX_UUID "8a885d04-1ceb-11c9-9fe8-08002b104860"
/*
* UUID (Universal Unique IDentifier)
*/
/* (X/Open CAE Spec Appendix A) */
struct ndr_dce_uuid {
DWORD time_low;
WORD time_mid;
WORD time_hi_and_version;
BYTE clock_seq_hi_and_reserved;
BYTE clock_seq_low;
BYTE node[6];
};
struct ndr_uuid {
DWORD data1;
WORD data2;
WORD data3;
BYTE data4[8];
};
typedef struct ndr_uuid ndr_uuid_t;
/*
* Representation label -- needed for RPC header
* (X/Open CAE Spec Chapter 14.1)
*
* Bits Data Type Description
* ---- --------- -----------
* 0-3 charset 0=ASCII
* 1=EBCDIC
* 4-7 byte-order 0=big-endian
* 1=little-endian
* 8-15 float 0=IEEE
* 1=VAX
* 2=Cray
* 3=IBM
* 16-31 reserved
*/
#define NDR_REPLAB_CHAR_MASK 0x0F /* low nibble of intg_char */
#define NDR_REPLAB_CHAR_ASCII 0x00 /* ASCII */
#define NDR_REPLAB_CHAR_EBCDIC 0x01 /* EBCDIC (never happen) */
#define NDR_REPLAB_INTG_MASK 0xF0 /* hi nibble of intg_char */
#define NDR_REPLAB_INTG_BIG_ENDIAN 0x00 /* big endian */
#define NDR_REPLAB_INTG_LITTLE_ENDIAN 0x10 /* little endian (x86) */
#define NDR_REPLAB_FLOAT_IEEE 0x00
#define NDR_REPLAB_FLOAT_VAX 0x01
#define NDR_REPLAB_FLOAT_CRAY 0x02
#define NDR_REPLAB_FLOAT_IBM 0x03
struct ndr_representation_label {
BYTE intg_char_rep; /* integer and charset */
BYTE float_rep;
BYTE _spare[2];
};
typedef struct ndr_representation_label ndr_replab_t;
/*
* RPC PDU (Protocol Data Unit) types
****************************************************************
* (X/Open CAE Spec 12.1)
*/
#define NDR_PTYPE_REQUEST 0x00 /* CO/CL */
#define NDR_PTYPE_PING 0x01 /* CL */
#define NDR_PTYPE_RESPONSE 0x02 /* CO/CL */
#define NDR_PTYPE_FAULT 0x03 /* CL/CL */
#define NDR_PTYPE_WORKING 0x04 /* CL */
#define NDR_PTYPE_NOCALL 0x05 /* CL */
#define NDR_PTYPE_REJECT 0x06 /* CL */
#define NDR_PTYPE_ACK 0x07 /* CL */
#define NDR_PTYPE_CL_CANCEL 0x08 /* CL */
#define NDR_PTYPE_FACK 0x09 /* CL */
#define NDR_PTYPE_CANCEL_ACK 0x0A /* CL */
#define NDR_PTYPE_BIND 0x0B /* CO */
#define NDR_PTYPE_BIND_ACK 0x0C /* CO */
#define NDR_PTYPE_BIND_NAK 0x0D /* CO */
#define NDR_PTYPE_ALTER_CONTEXT 0x0E /* CO */
#define NDR_PTYPE_ALTER_CONTEXT_RESP 0x0F /* CO */
/* 0x10 missing from DCE/RPC */
#define NDR_PTYPE_SHUTDOWN 0x11 /* CO */
#define NDR_PTYPE_CO_CANCEL 0x12 /* CO */
#define NDR_PTYPE_ORPHANED 0x13 /* CO */
/*
* Flags in the RPC header for Connection-oriented PDU data types
* (X/Open CAE Spec 12.6.3.1)
*
* MS-RPCE 2.2.2.3 PFC_SUPPORT_HEADER_SIGN
* For PDU types bind, bind_ack, alter_context and alter_context_resp,
* 0x04 means PFC_SUPPORT_HEADER_SIGN.
* For other PDU types 0x04 means PFC_PENDING_CANCEL.
*/
#define NDR_PFC_FIRST_FRAG 0x01 /* First fragment */
#define NDR_PFC_LAST_FRAG 0x02 /* Last framgent */
#define NDR_PFC_PENDING_CANCEL 0x04 /* Cancel was pending@sender*/
#define NDR_PFC_SUPPORT_HEADER_SIGN NDR_PFC_PENDING_CANCEL
#define NDR_PFC_RESERVED_1 0x08 /* */
#define NDR_PFC_CONC_MPX 0x10 /* supports concurrent muxing
* of single connection */
#define NDR_PFC_DID_NOT_EXECUTE 0x20 /* for PTYPE_FAULT, guarantee
* call did not execute */
#define NDR_PFC_MAYBE 0x40 /* "maybe" semantics req'ed*/
#define NDR_PFC_OBJECT_UUID 0x80 /* */
/*
* Security Providers
* MS-RPCE 2.2.1.1.6
*/
#define NDR_C_AUTHN_NONE 0x00 /* No authentication */
#define NDR_C_AUTHN_GSS_NEGOTIATE 0x09 /* SPNEGO */
#define NDR_C_AUTHN_WINNT 0x0A /* NTLM */
#define NDR_C_AUTHN_GSS_KERBEROS 0x10 /* Kerberos */
#define NDR_C_AUTHN_GSS_NETLOGON 0x44 /* Netlogon */
#define NDR_C_AUTHN_GSS_DEFAULT 0xFF /* Default is NTLM */
/*
* Encoding protection levels
* X/Open CAE Spec 13.1.2.1
* MS-RPCE 2.2.1.1.7
*/
#define NDR_C_AUTHN_LEVEL_DEFAULT 0x00 /* Same as Connect */
#define NDR_C_AUTHN_LEVEL_NONE 0x01
#define NDR_C_AUTHN_LEVEL_CONNECT 0x02
#define NDR_C_AUTHN_LEVEL_CALL 0x03
#define NDR_C_AUTHN_LEVEL_PKT 0x04
#define NDR_C_AUTHN_LEVEL_PKT_INTEGRITY 0x05
#define NDR_C_AUTHN_LEVEL_PKT_PRIVACY 0x06
/*
* Header common to all Connection-oriented RPC PDUs
* (X/Open CAE Spec 12.6.3.1)
*/
_NO_REORDER_
struct ndr_p_syntax_id {
ndr_uuid_t if_uuid;
DWORD if_version;
};
typedef struct ndr_p_syntax_id ndr_p_syntax_id_t;
_NO_REORDER_
struct ndr_common_header {
BYTE rpc_vers; /* 00:01 5 */
BYTE rpc_vers_minor; /* 01:01 0 */
BYTE ptype; /* 02:01 NDR_PTYPE_... */
BYTE pfc_flags; /* 03:01 NDR_PFC_... */
struct ndr_representation_label
packed_drep; /* 04:04 NDR representation label */
WORD frag_length; /* 08:02 total length of frag */
WORD auth_length; /* 10:02 length of auth_value */
DWORD call_id; /* 12:04 call identifier */
/* 16: */
};
typedef struct ndr_common_header ndr_common_header_t;
EXTERNTYPEINFO(ndr_common_header)
/*
* MS-RPCE 2.2.6 Type Serialization Version 1 extensions to IDL/+ pickle
* One header per serialization stream: the header must be little endian.
* The filler must be set to 0xcccccccc during marshaling and ignored
* during unmarshaling.
*/
_NO_REORDER_
struct ndr_serialtype1_hdr {
BYTE version; /* 00:01 1 */
BYTE endianness; /* 01:01 0=big, 1=little */
WORD hdrlen; /* 02:02 8 */
DWORD filler; /* 04:04 0xcccccccc */
/* 8: */
};
typedef struct ndr_serialtype1_hdr ndr_serialtype1_hdr_t;
EXTERNTYPEINFO(ndr_serialtype1_hdr)
/*
* Type Serialization Version 1 Private Header.
* A private header must precede a top-level NDR constructed type.
*/
_NO_REORDER_
struct ndr_serialtype1_priv_hdr {
DWORD buflen; /* 00:04 */
DWORD filler; /* 04:04 must be zero */
/* 8: */
};
typedef struct ndr_serialtype1_priv_hdr ndr_serialtype1_priv_hdr_t;
EXTERNTYPEINFO(ndr_serialtype1_priv_hdr)
/*
* MS-RPCE 2.2.7 Type Serialization Version 2 extensions Version 1 (2.2.6).
* The header must be little endian.
* The endianinfo and reserved fields must be set to 0xcccccccc during
* marshaling and ignored during unmarshaling.
*/
_NO_REORDER_
struct ndr_serialtype2_hdr {
BYTE version; /* 00:01 1 */
BYTE endianness; /* 01:01 0=big, 1=little */
WORD hdrlen; /* 02:02 8 */
DWORD endianinfo; /* 04:04 0xcccccccc */
DWORD reserved[4]; /* 08:16 0xcccccccc */
ndr_p_syntax_id_t transfer_syntax; /* 24:20 */
ndr_p_syntax_id_t interface_id; /* 44:20 */
/* 64: */
};
typedef struct ndr_serialtype2_hdr ndr_serialtype2_hdr_t;
EXTERNTYPEINFO(ndr_serialtype2_hdr)
/*
* Type Serialization Version 2 Private Header.
* A private header must precede a top-level NDR constructed type.
*/
_NO_REORDER_
struct ndr_serialtype2_priv_hdr {
DWORD buflen; /* 00:04 */
DWORD filler[3]; /* 04:12 must be zero */
/* 16: */
};
typedef struct ndr_serialtype2_priv_hdr ndr_serialtype2_priv_hdr_t;
EXTERNTYPEINFO(ndr_serialtype2_priv_hdr)
/*
* This header comes before the NDR-encoded KERB_VALIDATION_INFO structure,
* which can be found in one of the info buffers of the PAC.
*/
_NO_REORDER_
struct ndr_pac_hdr {
ndr_serialtype1_hdr_t common_hdr;
ndr_serialtype1_priv_hdr_t priv_hdr;
DWORD ref_pointer;
};
typedef struct ndr_pac_hdr ndr_pac_hdr_t;
EXTERNTYPEINFO(ndr_pac_hdr)
/*
* Supporting types (X/Open CAE Spec 12.6.3.1)
*/
typedef WORD ndr_p_context_id_t;
_NO_REORDER_
struct ndr_p_cont_elem {
ndr_p_context_id_t p_cont_id;
BYTE n_transfer_syn;
BYTE _reserved;
ndr_p_syntax_id_t abstract_syntax;
/*SIZE_IS(n_transfer_syn)*/
ndr_p_syntax_id_t transfer_syntaxes[1];
};
typedef struct ndr_p_cont_elem ndr_p_cont_elem_t;
EXTERNTYPEINFO(ndr_p_cont_elem)
_NO_REORDER_
struct ndr_p_cont_list {
BYTE n_context_elem;
BYTE _reserved;
WORD _reserved2;
/*SIZE_IS(n_context_elem)*/
ndr_p_cont_elem_t p_cont_elem[1];
};
typedef struct ndr_p_cont_list ndr_p_cont_list_t;
EXTERNTYPEINFO(ndr_p_cont_list)
typedef WORD ndr_p_cont_def_result_t;
#define NDR_PCDR_ACCEPTANCE 0
#define NDR_PCDR_USER_REJECTION 1
#define NDR_PCDR_PROVIDER_REJECTION 2
/*
* Reasons for provider rejection.
* X/Open CAE Spec 12.6.3.1
*/
typedef WORD ndr_p_provider_reason_t;
#define NDR_PPR_REASON_NOT_SPECIFIED 0
#define NDR_PPR_ABSTRACT_SYNTAX_NOT_SUPPORTED 1
#define NDR_PPR_PROPOSED_TRANSFER_SYNTAXES_NOT_SUPPORTED 2
#define NDR_PPR_LOCAL_LIMIT_EXCEEDED 3
_NO_REORDER_
struct ndr_p_result {
ndr_p_cont_def_result_t result; /* NDR_PCDR_... */
ndr_p_provider_reason_t reason; /* NDR_PPR_... */
ndr_p_syntax_id_t transfer_syntax; /* 0-fill if result!=ACCEPT */
};
typedef struct ndr_p_result ndr_p_result_t;
EXTERNTYPEINFO(ndr_p_result)
_NO_REORDER_
struct ndr_p_result_list {
BYTE n_results;
BYTE reserved;
WORD reserved2;
/*SIZE_IS(n_results)*/
ndr_p_result_t p_results[1];
};
typedef struct ndr_p_result_list ndr_p_result_list_t;
EXTERNTYPEINFO(ndr_p_result_list)
#define NDR_PORT_ANY_MAX_PORT_SPEC 30
_NO_REORDER_
struct ndr_port_any {
WORD length; /* always 18 */
/*SIZE_IS(length)*/
BYTE port_spec[NDR_PORT_ANY_MAX_PORT_SPEC];
/* \PIPE\ntsvcs */
/* We cheat by using 18, and pad on the right with zeroes */
};
typedef struct ndr_port_any ndr_port_any_t;
EXTERNTYPEINFO(ndr_port_any)
/*
* Reasons for rejecting an association in the bind_nak PDU.
* X/Open CAE Spec 12.6.3.1
* MS-RPCE 2.2.2.5
*/
#define NDR_REASON_NOT_SPECIFIED 0
#define NDR_TEMPORARY_CONGESTION 1
#define NDR_LOCAL_LIMIT_EXCEEDED 2
#define NDR_CALLED_PADDR_UNKNOWN 3
#define NDR_PROTOCOL_VERSION_NOT_SUPPORTED 4
#define NDR_DEFAULT_CONTEXT_NOT_SUPPORTED 5
#define NDR_USER_DATA_NOT_READABLE 6
#define NDR_NO_PSAP_AVAILABLE 7
#define NDR_AUTH_TYPE_NOT_RECOGNIZED 8
#define NDR_INVALID_CHECKSUM 9
/*
* NDRGEN can't handle variable-length arrays, so we provide our own
* marshal/unmarshal routine, so that we can convert to and from
* our pointer and a fixed-size array.
*
* NOTE: MS-RPCE calls this a sec_trailer_t, so we use ndr_sec_t.
* auth_value should also be an ANY_SIZE_ARRAY, but NDRGEN can't handle
* variable-sized arrays.
*/
IMPORT_EXTERN
_NO_REORDER_
struct auth_verifier_co {
/* restore 16 byte alignment */
/* SIZE_IS(auth_pad_len) */
/* BYTE auth_pad[ANY_SIZE_ARRAY] */ /* align(16) */
BYTE auth_type; /* 00:01 */
BYTE auth_level; /* 01:01 */
BYTE auth_pad_len; /* 02:01 */
BYTE auth_rsvd; /* 03:01 */
DWORD auth_context_id; /* 04:04 */
/* SIZE_IS(hdr->auth_length) */
BYTE *auth_value; /* 08:* credentials */
};
typedef struct auth_verifier_co ndr_sec_t;
EXTERNTYPEINFO(auth_verifier_co)
#define SEC_TRAILER_SIZE 8
/*
* Alter Context PDU (0x0E)
* (X/Open CAE Spec 12.6.4.1)
*/
_NO_REORDER_
struct ndr_alter_context_hdr {
ndr_common_header_t common_hdr; /* 00:16 (see above) */
WORD max_xmit_frag; /* 16:02 ignored */
WORD max_recv_frag; /* 18:02 ignored */
DWORD assoc_group_id; /* 20:04 ignored */
/*
* Presentation context list (see bind hdr comments).
*/
ndr_p_cont_list_t p_context_elem; /* 24: */
/* optional authentication verifier if auth_length != 0 */
/* auth_verifier_co_t auth_verifier; */
};
typedef struct ndr_alter_context_hdr ndr_alter_context_hdr_t;
/*
* Alter Context Response PDU (0x0F)
* (X/Open CAE Spec 12.6.4.2)
*
* We can't automatically generate an alter context response header because
* the sec_addr is an interior conformant (variable length) array, which is
* inconsistent with IDL/NDR rules. We mark this import-extern and provide
* a hand-coded marshalling function.
*/
IMPORT_EXTERN
_NO_REORDER_
struct ndr_alter_context_rsp_hdr {
ndr_common_header_t common_hdr; /* 00:16 (see above) */
WORD max_xmit_frag; /* 16:02 ignored */
WORD max_recv_frag; /* 18:02 ignored */
DWORD assoc_group_id; /* 20:04 ignored */
ndr_port_any_t sec_addr; /* 24:20 ignored */
/*
* Presentation context list (see bind hdr comments).
*/
ndr_p_result_list_t p_result_list; /* 44:nn */
/* optional authentication verifier if auth_length != 0 */
/* auth_verifier_co_t auth_verifier; */
};
typedef struct ndr_alter_context_rsp_hdr ndr_alter_context_rsp_hdr_t;
/*
* Bind PDU (0x0B)
* (X/Open CAE Spec 12.6.4.3)
*/
_NO_REORDER_
struct ndr_bind_hdr {
ndr_common_header_t common_hdr; /* 00:16 (see above) */
WORD max_xmit_frag; /* 16:02 max xmit frag size, bytes */
WORD max_recv_frag; /* 18:02 max recv frag size, bytes */
DWORD assoc_group_id; /* 20:04 association group */
/* 24: */
/* presentation, a variable**2 list, of presentation contexts */
ndr_p_cont_list_t p_context_elem;
/*
* This could be followed by more transfer_syntaxes[] for the
* p_cont_elem[0], and subsequently followed by more p_cont_elem[],
* each with one or more transfer_syntaxes[]. A single
* p_cont_elem[] with a single transfer_syntaxes[] is so common,
* though, we embed it in the bind_hdr but the bind processor must
* walk through this tail if there is one.
*/
/* optional authentication verifier iff auth_length != 0 */
/* auth_verifier_co_t auth_verifier; */
};
typedef struct ndr_bind_hdr ndr_bind_hdr_t;
/*
* Bind_Ack PDU (0x0C)
* (X/Open CAE Spec 12.6.4.4)
*
* We can't automatically generate a bind ack header because the sec_addr
* is an interior conformant (variable length) array, which is inconsistent
* with IDL/NDR rules. We mark this import-extern and provide a hand-coded
* marshalling function.
*/
IMPORT_EXTERN
_NO_REORDER_
struct ndr_bind_ack_hdr {
ndr_common_header_t common_hdr; /* 00:16 (see above) */
WORD max_xmit_frag; /* 16:02 max xmit frag size, bytes */
WORD max_recv_frag; /* 18:02 max recv frag size, bytes */
DWORD assoc_group_id; /* 20:04 association group */
ndr_port_any_t sec_addr; /* 24:20 */
ndr_p_result_list_t p_result_list; /* 44:nn */
/* This could be followed by more. See bind_hdr above */
/* optional authentication verifier iff auth_length != 0 */
/* auth_verifier_co_t auth_verifier; */
};
typedef struct ndr_bind_ack_hdr ndr_bind_ack_hdr_t;
/*
* Request PDU (0x00)
****************************************************************
* Two flavors, selected based on PFC_OBJECT_UUID in hdr.pfc_flags
* one without the "object" (flag clear)
* one with the "object" (flag set)
* (X/Open CAE Spec 12.6.4.9)
*/
_NO_REORDER_
struct ndr_request_hdr {
ndr_common_header_t common_hdr; /* 00:16 (see above) */
/* needed for request, response, or fault */
DWORD alloc_hint; /* 16:04 allocation hint */
ndr_p_context_id_t p_cont_id; /* 20:02 pres context, i.e. data rep */
WORD opnum; /* 22:02 op number w/i interface */
/* optional field if PFC_OBJECT_UUID, not present */
/* ndr_uuid_t object; */
/* stub-data, 8-octet aligned */ /* 24:nn */
/* nn = frag_len - sizeof(common_header) - auth_len */
/* optional authentication verifier iff auth_length != 0 */
/* auth_verifier_co_t auth_verifier; */
};
typedef struct ndr_request_hdr ndr_request_hdr_t;
_NO_REORDER_
struct ndr_request_hdr_with_object {
ndr_common_header_t common_hdr; /* 00:16 (see above) */
/* needed for request, response, or fault */
DWORD alloc_hint; /* 16:04 allocation hint */
ndr_p_context_id_t p_cont_id; /* 20:02 pres context, i.e. data rep */
WORD opnum; /* 22:02 op number w/i interface */
/* optional field if PFC_OBJECT_UUID, is present */
ndr_uuid_t object; /* 24:16 object UUID, unknown purpose*/
/* stub-data, 8-octet aligned */ /* 28:nn */
/* nn = frag_len - sizeof(common_header) - auth_len */
/* nn -= sizeof(ndr_uuid_t); */
/* optional authentication verifier iff auth_length != 0 */
/* auth_verifier_co_t auth_verifier; */
};
/*
* Convenient for response header sizing and multi-fragment responses.
* We know the header is going to be 24 bytes, and the 'common' part
* is 16 bytes.
*/
#define NDR_CMN_HDR_SIZE 16
#define NDR_RSP_HDR_SIZE 24
/*
* Response PDU (0x02)
* (X/Open CAE Spec 12.6.4.10)
*/
_NO_REORDER_
struct ndr_response_hdr {
ndr_common_header_t common_hdr; /* 00:16 (see above) */
/* needed for request, response, or fault */
DWORD alloc_hint; /* 16:04 allocation hint */
ndr_p_context_id_t p_cont_id; /* 20:02 pres context, i.e. data rep */
/* needed for response or fault */
BYTE cancel_count; /* 22:01 cancel count */
BYTE reserved; /* 23:01 mbz */
/* stub-data, 8-octet aligned */ /* 24:nn */
/* nn = frag_len - sizeof(common_header) - auth_len */
/* optional authentication verifier iff auth_length != 0 */
/* auth_verifier_co_t auth_verifier; */
};
typedef struct ndr_response_hdr ndr_response_hdr_t;
/*
* Fault PDU (0x03)
* (X/Open CAE Spec 12.6.4.7)
*/
_NO_REORDER_
struct ndr_fault_hdr {
ndr_common_header_t common_hdr; /* 00:16 (see above) */
DWORD alloc_hint; /* 16:04 allocation hint */
ndr_p_context_id_t p_cont_id; /* 20:02 pres context, i.e. data rep */
/* needed for response or fault */
BYTE cancel_count; /* 22:01 cancel count */
BYTE reserved; /* 23:01 mbz */
/* fault code */
DWORD status; /* 24:04 run-time fault code or 0 */
/* pad to 8-byte alignment */
BYTE reserved2[4]; /* 28:04 must-be-zero */
/* stub-data here if status==0. We do not use this mode. */
/* optional authentication verifier iff auth_length != 0 */
/* auth_verifier_co_t auth_verifier; */
};
typedef struct ndr_fault_hdr ndr_fault_hdr_t;
/* Fault status code (X/Open CAE Spec Appendix E) */
#define NDR_FAULT_NCA_RPC_VERSION_MISMATCH 0x1c000008 /* CO/CL */
#define NDR_FAULT_NCA_UNSPEC_REJECT 0x1c000009 /* CO/CL */
#define NDR_FAULT_NCA_S_BAD_ACTID 0x1c00000A /* CL */
#define NDR_FAULT_NCA_WHO_ARE_YOU_FAILED 0x1c00000B /* CL */
#define NDR_FAULT_NCA_MANAGER_NOT_ENTERED 0x1c00000C /* CO/CL */
#define NDR_FAULT_NCA_OP_RNG_ERROR 0x1c010002 /* CO/CL */
#define NDR_FAULT_NCA_UNK_IF 0x1c010003 /* CO/CL */
#define NDR_FAULT_NCA_WRONG_BOOT_TIME 0x1c010006 /* CL */
#define NDR_FAULT_NCA_S_YOU_CRASHED 0x1c010009 /* CL */
#define NDR_FAULT_NCA_PROTO_ERROR 0x1c01000B /* CO/CL */
#define NDR_FAULT_NCA_OUT_ARGS_TOO_BIG 0x1c010013 /* CO/CL */
#define NDR_FAULT_NCA_SERVER_TOO_BUSY 0x1c010014 /* CO/CL */
#define NDR_FAULT_NCA_UNSUPPORTED_TYPE 0x1c010017 /* CO/CL */
#define NDR_FAULT_NCA_INVALID_PRES_CONTEXT_ID 0x1c00001c /* CO */
#define NDR_FAULT_NCA_UNSUPPORTED_AUTHN_LEVEL 0x1c00001d /* CO/CL */
#define NDR_FAULT_NCA_INVALID_CHECKSUM 0x1c00001f /* CO/CL */
#define NDR_FAULT_NCA_INVALID_CRC 0x1c000020 /* CO/CL */
/*
* The Header Union/Switch
****************************************************************
*/
#define NDR_PTYPE_COMMON 999
#define NDR_PTYPE_REQUEST_WITH 998
#define NDR_PTYPE_SERIALTYPE_V1 997
#define NDR_PTYPE_SERIALTYPE_V2 996
#define NDR_PTYPE_PAC 995
INTERFACE(0)
union ndr_hdr {
CASE(NDR_PTYPE_COMMON) /* exceeds BYTE range, obtains common hdr */
struct ndr_common_header common_hdr;
CASE(NDR_PTYPE_BIND)
struct ndr_bind_hdr bind_hdr;
CASE(NDR_PTYPE_BIND_ACK)
struct ndr_bind_ack_hdr bind_ack_hdr;
CASE(NDR_PTYPE_REQUEST)
struct ndr_request_hdr request_hdr;
CASE(NDR_PTYPE_REQUEST_WITH) /* exceeds BYTE range, ... */
struct ndr_request_hdr_with_object request_hdr_with;
CASE(NDR_PTYPE_RESPONSE)
struct ndr_response_hdr response_hdr;
CASE(NDR_PTYPE_ALTER_CONTEXT)
struct ndr_alter_context_hdr alter_context_hdr;
CASE(NDR_PTYPE_ALTER_CONTEXT_RESP)
struct ndr_alter_context_rsp_hdr alter_context_rsp_hdr;
CASE(NDR_PTYPE_SERIALTYPE_V1)
struct ndr_serialtype1_hdr serialtype1_hdr;
CASE(NDR_PTYPE_SERIALTYPE_V2)
struct ndr_serialtype2_hdr serialtype2_hdr;
CASE(NDR_PTYPE_PAC)
struct ndr_pac_hdr pac_hdr;
CASE(NDR_PTYPE_FAULT)
struct ndr_fault_hdr fault_hdr;
};
typedef union ndr_hdr ndr_hdr_t;
EXTERNTYPEINFO(ndr_hdr)
#endif /* _RPCPDU_NDL_ */
|