summaryrefslogtreecommitdiff
path: root/source4/torture/rpc/samba3rpc.c
blob: f7968b1c56e663081a36256c937c2d1b1b56c769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
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
/*
   Unix SMB/CIFS implementation.

   dcerpc torture tests, designed to walk Samba3 code paths

   Copyright (C) Volker Lendecke 2006

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "libcli/raw/libcliraw.h"
#include "libcli/raw/raw_proto.h"
#include "torture/util.h"
#include "libcli/rap/rap.h"
#include "librpc/gen_ndr/ndr_lsa_c.h"
#include "librpc/gen_ndr/ndr_samr_c.h"
#include "librpc/gen_ndr/ndr_netlogon_c.h"
#include "librpc/gen_ndr/ndr_srvsvc_c.h"
#include "librpc/gen_ndr/ndr_spoolss_c.h"
#include "librpc/gen_ndr/ndr_winreg_c.h"
#include "librpc/gen_ndr/ndr_wkssvc_c.h"
#include "lib/cmdline/popt_common.h"
#include "torture/rpc/torture_rpc.h"
#include "libcli/libcli.h"
#include "libcli/smb_composite/smb_composite.h"
#include "libcli/auth/libcli_auth.h"
#include "../lib/crypto/crypto.h"
#include "libcli/security/security.h"
#include "param/param.h"
#include "lib/registry/registry.h"
#include "libcli/resolve/resolve.h"
#include "torture/ndr/ndr.h"
#include "libcli/smb2/smb2.h"
#include "libcli/smb2/smb2_calls.h"
#include "librpc/rpc/dcerpc.h"
#include "librpc/rpc/dcerpc_proto.h"
#include "../source3/libsmb/smb2cli.h"
#include "libcli/smb/smbXcli_base.h"

/*
 * This tests a RPC call using an invalid vuid
 */

bool torture_bind_authcontext(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct lsa_ObjectAttribute objectattr;
	struct lsa_OpenPolicy2 openpolicy;
	struct policy_handle handle;
	struct lsa_Close close_handle;
	struct smbcli_session *tmp;
	struct smbcli_session *session2;
	struct smbcli_state *cli;
	struct dcerpc_pipe *lsa_pipe;
	struct dcerpc_binding_handle *lsa_handle;
	struct cli_credentials *anon_creds;
	struct smb_composite_sesssetup setup;
	struct smbcli_options options;
	struct smbcli_session_options session_options;

	mem_ctx = talloc_init("torture_bind_authcontext");

	if (mem_ctx == NULL) {
		torture_comment(torture, "talloc_init failed\n");
		return false;
	}

	lpcfg_smbcli_options(torture->lp_ctx, &options);
	lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);

	status = smbcli_full_connection(mem_ctx, &cli,
					torture_setting_string(torture, "host", NULL),
					lpcfg_smb_ports(torture->lp_ctx),
					"IPC$", NULL,
					lpcfg_socket_options(torture->lp_ctx),
					cmdline_credentials,
					lpcfg_resolve_context(torture->lp_ctx),
					torture->ev, &options, &session_options,
					lpcfg_gensec_settings(torture, torture->lp_ctx));
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "smbcli_full_connection failed: %s\n",
			 nt_errstr(status));
		goto done;
	}

	lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
	if (lsa_pipe == NULL) {
		torture_comment(torture, "dcerpc_pipe_init failed\n");
		goto done;
	}
	lsa_handle = lsa_pipe->binding_handle;

	status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "dcerpc_pipe_open_smb failed: %s\n",
			 nt_errstr(status));
		goto done;
	}

	status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "dcerpc_bind_auth_none failed: %s\n",
			 nt_errstr(status));
		goto done;
	}

	openpolicy.in.system_name =talloc_asprintf(
		mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
	ZERO_STRUCT(objectattr);
	openpolicy.in.attr = &objectattr;
	openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	openpolicy.out.handle = &handle;

	status = dcerpc_lsa_OpenPolicy2_r(lsa_handle, mem_ctx, &openpolicy);

	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "dcerpc_lsa_OpenPolicy2 failed: %s\n",
			 nt_errstr(status));
		goto done;
	}
	if (!NT_STATUS_IS_OK(openpolicy.out.result)) {
		torture_comment(torture, "dcerpc_lsa_OpenPolicy2 failed: %s\n",
			 nt_errstr(openpolicy.out.result));
		goto done;
	}

	close_handle.in.handle = &handle;
	close_handle.out.handle = &handle;

	status = dcerpc_lsa_Close_r(lsa_handle, mem_ctx, &close_handle);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "dcerpc_lsa_Close failed: %s\n",
			 nt_errstr(status));
		goto done;
	}
	if (!NT_STATUS_IS_OK(close_handle.out.result)) {
		torture_comment(torture, "dcerpc_lsa_Close failed: %s\n",
			 nt_errstr(close_handle.out.result));
		goto done;
	}

	session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
	if (session2 == NULL) {
		torture_comment(torture, "smbcli_session_init failed\n");
		goto done;
	}

	if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
		torture_comment(torture, "create_anon_creds failed\n");
		goto done;
	}

	setup.in.sesskey = cli->transport->negotiate.sesskey;
	setup.in.capabilities = cli->transport->negotiate.capabilities;
	setup.in.workgroup = "";
	setup.in.credentials = anon_creds;
	setup.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);

	status = smb_composite_sesssetup(session2, &setup);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "anon session setup failed: %s\n",
			 nt_errstr(status));
		goto done;
	}
	session2->vuid = setup.out.vuid;

	tmp = cli->tree->session;
	cli->tree->session = session2;

	status = dcerpc_lsa_OpenPolicy2_r(lsa_handle, mem_ctx, &openpolicy);

	cli->tree->session = tmp;
	talloc_free(lsa_pipe);
	lsa_pipe = NULL;

	if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
		torture_comment(torture, "dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
			 "expected NT_STATUS_INVALID_HANDLE\n",
			 nt_errstr(status));
		goto done;
	}

	ret = true;
 done:
	talloc_free(mem_ctx);
	return ret;
}

/*
 * Bind to lsa using a specific auth method
 */

static bool bindtest(struct torture_context *tctx,
		     struct smbcli_state *cli,
		     struct cli_credentials *credentials,
		     uint8_t auth_type, uint8_t auth_level)
{
	TALLOC_CTX *mem_ctx;
	bool ret = false;
	NTSTATUS status;

	struct dcerpc_pipe *lsa_pipe;
	struct dcerpc_binding_handle *lsa_handle;
	struct lsa_ObjectAttribute objectattr;
	struct lsa_OpenPolicy2 openpolicy;
	struct lsa_QueryInfoPolicy query;
	union lsa_PolicyInformation *info = NULL;
	struct policy_handle handle;
	struct lsa_Close close_handle;

	if ((mem_ctx = talloc_init("bindtest")) == NULL) {
		torture_comment(tctx, "talloc_init failed\n");
		return false;
	}

	lsa_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
	if (lsa_pipe == NULL) {
		torture_comment(tctx, "dcerpc_pipe_init failed\n");
		goto done;
	}
	lsa_handle = lsa_pipe->binding_handle;

	status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "dcerpc_pipe_open_smb failed: %s\n",
			 nt_errstr(status));
		goto done;
	}

	status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
				  credentials, lpcfg_gensec_settings(tctx->lp_ctx, tctx->lp_ctx), auth_type, auth_level,
				  NULL);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "dcerpc_bind_auth failed: %s\n", nt_errstr(status));
		goto done;
	}

	openpolicy.in.system_name =talloc_asprintf(
		mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
	ZERO_STRUCT(objectattr);
	openpolicy.in.attr = &objectattr;
	openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	openpolicy.out.handle = &handle;

	status = dcerpc_lsa_OpenPolicy2_r(lsa_handle, mem_ctx, &openpolicy);

	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "dcerpc_lsa_OpenPolicy2 failed: %s\n",
			 nt_errstr(status));
		goto done;
	}
	if (!NT_STATUS_IS_OK(openpolicy.out.result)) {
		torture_comment(tctx, "dcerpc_lsa_OpenPolicy2 failed: %s\n",
			 nt_errstr(openpolicy.out.result));
		goto done;
	}

	query.in.handle = &handle;
	query.in.level = LSA_POLICY_INFO_DOMAIN;
	query.out.info = &info;

	status = dcerpc_lsa_QueryInfoPolicy_r(lsa_handle, mem_ctx, &query);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "dcerpc_lsa_QueryInfoPolicy failed: %s\n",
			 nt_errstr(status));
		goto done;
	}
	if (!NT_STATUS_IS_OK(query.out.result)) {
		torture_comment(tctx, "dcerpc_lsa_QueryInfoPolicy failed: %s\n",
			 nt_errstr(query.out.result));
		goto done;
	}

	close_handle.in.handle = &handle;
	close_handle.out.handle = &handle;

	status = dcerpc_lsa_Close_r(lsa_handle, mem_ctx, &close_handle);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "dcerpc_lsa_Close failed: %s\n",
			 nt_errstr(status));
		goto done;
	}
	if (!NT_STATUS_IS_OK(close_handle.out.result)) {
		torture_comment(tctx, "dcerpc_lsa_Close failed: %s\n",
			 nt_errstr(close_handle.out.result));
		goto done;
	}


	ret = true;
 done:
	talloc_free(mem_ctx);
	return ret;
}

/*
 * test authenticated RPC binds with the variants Samba3 does support
 */

static bool torture_bind_samba3(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct smbcli_state *cli;
	struct smbcli_options options;
	struct smbcli_session_options session_options;

	mem_ctx = talloc_init("torture_bind_authcontext");

	if (mem_ctx == NULL) {
		torture_comment(torture, "talloc_init failed\n");
		return false;
	}

	lpcfg_smbcli_options(torture->lp_ctx, &options);
	lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);

	status = smbcli_full_connection(mem_ctx, &cli,
					torture_setting_string(torture, "host", NULL),
					lpcfg_smb_ports(torture->lp_ctx),
					"IPC$", NULL,
					lpcfg_socket_options(torture->lp_ctx),
					cmdline_credentials,
					lpcfg_resolve_context(torture->lp_ctx),
					torture->ev, &options, &session_options,
					lpcfg_gensec_settings(torture, torture->lp_ctx));
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "smbcli_full_connection failed: %s\n",
			 nt_errstr(status));
		goto done;
	}

	ret = true;

	ret &= bindtest(torture, cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
			DCERPC_AUTH_LEVEL_INTEGRITY);
	ret &= bindtest(torture, cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
			DCERPC_AUTH_LEVEL_PRIVACY);
	ret &= bindtest(torture, cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
			DCERPC_AUTH_LEVEL_INTEGRITY);
	ret &= bindtest(torture, cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
			DCERPC_AUTH_LEVEL_PRIVACY);

 done:
	talloc_free(mem_ctx);
	return ret;
}

/*
 * Lookup or create a user and return all necessary info
 */

static bool get_usr_handle(struct torture_context *tctx,
			   struct smbcli_state *cli,
			   TALLOC_CTX *mem_ctx,
			   struct cli_credentials *admin_creds,
			   uint8_t auth_type,
			   uint8_t auth_level,
			   const char *username,
			   char **domain,
			   struct dcerpc_pipe **result_pipe,
			   struct policy_handle **result_handle,
			   struct dom_sid **sid_p)
{
	struct dcerpc_pipe *samr_pipe;
	struct dcerpc_binding_handle *samr_handle;
	NTSTATUS status;
	struct policy_handle conn_handle;
	struct policy_handle domain_handle;
	struct policy_handle *user_handle;
	struct samr_Connect2 conn;
	struct samr_EnumDomains enumdom;
	uint32_t resume_handle = 0;
	uint32_t num_entries = 0;
	struct samr_SamArray *sam = NULL;
	struct samr_LookupDomain l;
	struct dom_sid2 *sid = NULL;
	int dom_idx;
	struct lsa_String domain_name;
	struct lsa_String user_name;
	struct samr_OpenDomain o;
	struct samr_CreateUser2 c;
	uint32_t user_rid,access_granted;

	samr_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
	torture_assert(tctx, samr_pipe, "dcerpc_pipe_init failed");

	samr_handle = samr_pipe->binding_handle;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr"),
		"dcerpc_pipe_open_smb failed");

	if (admin_creds != NULL) {
		torture_assert_ntstatus_ok(tctx,
			dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
					  admin_creds, lpcfg_gensec_settings(tctx->lp_ctx, tctx->lp_ctx), auth_type, auth_level,
					  NULL),
			"dcerpc_bind_auth failed");
	} else {
		/* We must have an authenticated SMB connection */
		torture_assert_ntstatus_ok(tctx,
			dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr),
			"dcerpc_bind_auth_none failed");
	}

	conn.in.system_name = talloc_asprintf(
		mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
	conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	conn.out.connect_handle = &conn_handle;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_samr_Connect2_r(samr_handle, mem_ctx, &conn),
		"samr_Connect2 failed");
	torture_assert_ntstatus_ok(tctx, conn.out.result,
		"samr_Connect2 failed");

	enumdom.in.connect_handle = &conn_handle;
	enumdom.in.resume_handle = &resume_handle;
	enumdom.in.buf_size = (uint32_t)-1;
	enumdom.out.resume_handle = &resume_handle;
	enumdom.out.num_entries = &num_entries;
	enumdom.out.sam = &sam;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_samr_EnumDomains_r(samr_handle, mem_ctx, &enumdom),
		"samr_EnumDomains failed");
	torture_assert_ntstatus_ok(tctx, enumdom.out.result,
		"samr_EnumDomains failed");

	torture_assert_int_equal(tctx, *enumdom.out.num_entries, 2,
		"samr_EnumDomains returned unexpected num_entries");

	dom_idx = strequal(sam->entries[0].name.string,
			   "builtin") ? 1:0;

	l.in.connect_handle = &conn_handle;
	domain_name.string = sam->entries[dom_idx].name.string;
	*domain = talloc_strdup(mem_ctx, domain_name.string);
	l.in.domain_name = &domain_name;
	l.out.sid = &sid;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_samr_LookupDomain_r(samr_handle, mem_ctx, &l),
		"samr_LookupDomain failed");
	torture_assert_ntstatus_ok(tctx, l.out.result,
		"samr_LookupDomain failed");

	o.in.connect_handle = &conn_handle;
	o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	o.in.sid = *l.out.sid;
	o.out.domain_handle = &domain_handle;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_samr_OpenDomain_r(samr_handle, mem_ctx, &o),
		"samr_OpenDomain failed");
	torture_assert_ntstatus_ok(tctx, o.out.result,
		"samr_OpenDomain failed");

	c.in.domain_handle = &domain_handle;
	user_name.string = username;
	c.in.account_name = &user_name;
	c.in.acct_flags = ACB_NORMAL;
	c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	user_handle = talloc(mem_ctx, struct policy_handle);
	c.out.user_handle = user_handle;
	c.out.access_granted = &access_granted;
	c.out.rid = &user_rid;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_samr_CreateUser2_r(samr_handle, mem_ctx, &c),
		"samr_CreateUser2 failed");

	if (NT_STATUS_EQUAL(c.out.result, NT_STATUS_USER_EXISTS)) {
		struct samr_LookupNames ln;
		struct samr_OpenUser ou;
		struct samr_Ids rids, types;

		ln.in.domain_handle = &domain_handle;
		ln.in.num_names = 1;
		ln.in.names = &user_name;
		ln.out.rids = &rids;
		ln.out.types = &types;

		torture_assert_ntstatus_ok(tctx,
			dcerpc_samr_LookupNames_r(samr_handle, mem_ctx, &ln),
			"samr_LookupNames failed");
		torture_assert_ntstatus_ok(tctx, ln.out.result,
			"samr_LookupNames failed");

		ou.in.domain_handle = &domain_handle;
		ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
		user_rid = ou.in.rid = ln.out.rids->ids[0];
		ou.out.user_handle = user_handle;

		torture_assert_ntstatus_ok(tctx,
			dcerpc_samr_OpenUser_r(samr_handle, mem_ctx, &ou),
			"samr_OpenUser failed");
		status = ou.out.result;
	} else {
		status = c.out.result;
	}

	torture_assert_ntstatus_ok(tctx, status,
		"samr_CreateUser failed");

	*result_pipe = samr_pipe;
	*result_handle = user_handle;
	if (sid_p != NULL) {
		*sid_p = dom_sid_add_rid(mem_ctx, *l.out.sid, user_rid);
	}
	return true;

}

/*
 * Create a test user
 */

static bool create_user(struct torture_context *tctx,
			TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
			struct cli_credentials *admin_creds,
			const char *username, const char *password,
			char **domain_name,
			struct dom_sid **user_sid)
{
	TALLOC_CTX *tmp_ctx;
	NTSTATUS status;
	struct dcerpc_pipe *samr_pipe;
	struct dcerpc_binding_handle *samr_handle;
	struct policy_handle *wks_handle;
	bool ret = false;

	if (!(tmp_ctx = talloc_new(mem_ctx))) {
		torture_comment(tctx, "talloc_init failed\n");
		return false;
	}

	ret = get_usr_handle(tctx, cli, tmp_ctx, admin_creds,
			     DCERPC_AUTH_TYPE_NTLMSSP,
			     DCERPC_AUTH_LEVEL_INTEGRITY,
			     username, domain_name, &samr_pipe, &wks_handle,
			     user_sid);
	if (ret == false) {
		torture_comment(tctx, "get_usr_handle failed\n");
		goto done;
	}
	samr_handle = samr_pipe->binding_handle;

	{
		struct samr_SetUserInfo2 sui2;
		struct samr_SetUserInfo sui;
		struct samr_QueryUserInfo qui;
		union samr_UserInfo u_info;
		union samr_UserInfo *info;
		DATA_BLOB session_key;


		ZERO_STRUCT(u_info);
		encode_pw_buffer(u_info.info23.password.data, password,
				 STR_UNICODE);

		status = dcerpc_fetch_session_key(samr_pipe, &session_key);
		if (!NT_STATUS_IS_OK(status)) {
			torture_comment(tctx, "dcerpc_fetch_session_key failed\n");
			goto done;
		}
		arcfour_crypt_blob(u_info.info23.password.data, 516,
				   &session_key);
		u_info.info23.info.password_expired = 0;
		u_info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
						    SAMR_FIELD_LM_PASSWORD_PRESENT |
						    SAMR_FIELD_EXPIRED_FLAG;
		sui2.in.user_handle = wks_handle;
		sui2.in.info = &u_info;
		sui2.in.level = 23;

		status = dcerpc_samr_SetUserInfo2_r(samr_handle, tmp_ctx, &sui2);
		if (!NT_STATUS_IS_OK(status)) {
			torture_comment(tctx, "samr_SetUserInfo(23) failed: %s\n",
				 nt_errstr(status));
			goto done;
		}
		if (!NT_STATUS_IS_OK(sui2.out.result)) {
			torture_comment(tctx, "samr_SetUserInfo(23) failed: %s\n",
				 nt_errstr(sui2.out.result));
			goto done;
		}

		u_info.info16.acct_flags = ACB_NORMAL;
		sui.in.user_handle = wks_handle;
		sui.in.info = &u_info;
		sui.in.level = 16;

		status = dcerpc_samr_SetUserInfo_r(samr_handle, tmp_ctx, &sui);
		if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(sui.out.result)) {
			torture_comment(tctx, "samr_SetUserInfo(16) failed\n");
			goto done;
		}

		qui.in.user_handle = wks_handle;
		qui.in.level = 21;
		qui.out.info = &info;

		status = dcerpc_samr_QueryUserInfo_r(samr_handle, tmp_ctx, &qui);
		if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(qui.out.result)) {
			torture_comment(tctx, "samr_QueryUserInfo(21) failed\n");
			goto done;
		}

		info->info21.allow_password_change = 0;
		info->info21.force_password_change = 0;
		info->info21.account_name.string = NULL;
		info->info21.rid = 0;
		info->info21.acct_expiry = 0;
		info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */

		u_info.info21 = info->info21;
		sui.in.user_handle = wks_handle;
		sui.in.info = &u_info;
		sui.in.level = 21;

		status = dcerpc_samr_SetUserInfo_r(samr_handle, tmp_ctx, &sui);
		if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(sui.out.result)) {
			torture_comment(tctx, "samr_SetUserInfo(21) failed\n");
			goto done;
		}
	}

	*domain_name= talloc_steal(mem_ctx, *domain_name);
	*user_sid = talloc_steal(mem_ctx, *user_sid);
	ret = true;
 done:
	talloc_free(tmp_ctx);
	return ret;
}

/*
 * Delete a test user
 */

static bool delete_user(struct torture_context *tctx,
			struct smbcli_state *cli,
			struct cli_credentials *admin_creds,
			const char *username)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	char *dom_name;
	struct dcerpc_pipe *samr_pipe;
	struct dcerpc_binding_handle *samr_handle;
	struct policy_handle *user_handle;
	bool ret = false;

	if ((mem_ctx = talloc_init("leave")) == NULL) {
		torture_comment(tctx, "talloc_init failed\n");
		return false;
	}

	ret = get_usr_handle(tctx, cli, mem_ctx, admin_creds,
			     DCERPC_AUTH_TYPE_NTLMSSP,
			     DCERPC_AUTH_LEVEL_INTEGRITY,
			     username, &dom_name, &samr_pipe,
			     &user_handle, NULL);
	if (ret == false) {
		torture_comment(tctx, "get_wks_handle failed\n");
		goto done;
	}
	samr_handle = samr_pipe->binding_handle;

	{
		struct samr_DeleteUser d;

		d.in.user_handle = user_handle;
		d.out.user_handle = user_handle;

		status = dcerpc_samr_DeleteUser_r(samr_handle, mem_ctx, &d);
		if (!NT_STATUS_IS_OK(status)) {
			torture_comment(tctx, "samr_DeleteUser failed %s\n", nt_errstr(status));
			goto done;
		}
		if (!NT_STATUS_IS_OK(d.out.result)) {
			torture_comment(tctx, "samr_DeleteUser failed %s\n", nt_errstr(d.out.result));
			goto done;
		}

	}

	ret = true;

 done:
	talloc_free(mem_ctx);
	return ret;
}

/*
 * Do a Samba3-style join
 */

static bool join3(struct torture_context *tctx,
		  struct smbcli_state *cli,
		  bool use_level25,
		  struct cli_credentials *admin_creds,
		  struct cli_credentials *wks_creds)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	char *dom_name;
	struct dcerpc_pipe *samr_pipe;
	struct dcerpc_binding_handle *samr_handle;
	struct policy_handle *wks_handle;
	bool ret = false;
	NTTIME last_password_change;

	if ((mem_ctx = talloc_init("join3")) == NULL) {
		torture_comment(tctx, "talloc_init failed\n");
		return false;
	}

	ret = get_usr_handle(
		tctx, cli, mem_ctx, admin_creds,
		DCERPC_AUTH_TYPE_NTLMSSP,
		DCERPC_AUTH_LEVEL_PRIVACY,
		talloc_asprintf(mem_ctx, "%s$",
				cli_credentials_get_workstation(wks_creds)),
		&dom_name, &samr_pipe, &wks_handle, NULL);
	if (ret == false) {
		torture_comment(tctx, "get_wks_handle failed\n");
		goto done;
	}
	samr_handle = samr_pipe->binding_handle;

	{
		struct samr_QueryUserInfo q;
		union samr_UserInfo *info;

		q.in.user_handle = wks_handle;
		q.in.level = 21;
		q.out.info = &info;

		status = dcerpc_samr_QueryUserInfo_r(samr_handle, mem_ctx, &q);
		if (!NT_STATUS_IS_OK(status)) {
			torture_warning(tctx, "QueryUserInfo failed: %s\n",
				  nt_errstr(status));
			goto done;
		}
		if (!NT_STATUS_IS_OK(q.out.result)) {
			torture_warning(tctx, "QueryUserInfo failed: %s\n",
				  nt_errstr(q.out.result));
			goto done;
		}


		last_password_change = info->info21.last_password_change;
	}

	cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);

	if (use_level25) {
		struct samr_SetUserInfo2 sui2;
		union samr_UserInfo u_info;
		struct samr_UserInfo21 *i21 = &u_info.info25.info;
		DATA_BLOB session_key;
		DATA_BLOB confounded_session_key = data_blob_talloc(
			mem_ctx, NULL, 16);
		struct MD5Context ctx;
		uint8_t confounder[16];

		ZERO_STRUCT(u_info);

		i21->full_name.string = talloc_asprintf(
			mem_ctx, "%s$",
			cli_credentials_get_workstation(wks_creds));
		i21->acct_flags = ACB_WSTRUST;
		i21->fields_present = SAMR_FIELD_FULL_NAME |
			SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_NT_PASSWORD_PRESENT;
		/* this would break the test result expectations
		i21->fields_present |= SAMR_FIELD_EXPIRED_FLAG;
		i21->password_expired = 1;
		*/

		encode_pw_buffer(u_info.info25.password.data,
				 cli_credentials_get_password(wks_creds),
				 STR_UNICODE);
		status = dcerpc_fetch_session_key(samr_pipe, &session_key);
		if (!NT_STATUS_IS_OK(status)) {
			torture_comment(tctx, "dcerpc_fetch_session_key failed: %s\n",
				 nt_errstr(status));
			goto done;
		}
		generate_random_buffer((uint8_t *)confounder, 16);

		MD5Init(&ctx);
		MD5Update(&ctx, confounder, 16);
		MD5Update(&ctx, session_key.data, session_key.length);
		MD5Final(confounded_session_key.data, &ctx);

		arcfour_crypt_blob(u_info.info25.password.data, 516,
				   &confounded_session_key);
		memcpy(&u_info.info25.password.data[516], confounder, 16);

		sui2.in.user_handle = wks_handle;
		sui2.in.level = 25;
		sui2.in.info = &u_info;

		status = dcerpc_samr_SetUserInfo2_r(samr_handle, mem_ctx, &sui2);
		if (!NT_STATUS_IS_OK(status)) {
			torture_comment(tctx, "samr_SetUserInfo2(25) failed: %s\n",
				 nt_errstr(status));
			goto done;
		}
		if (!NT_STATUS_IS_OK(sui2.out.result)) {
			torture_comment(tctx, "samr_SetUserInfo2(25) failed: %s\n",
				 nt_errstr(sui2.out.result));
			goto done;
		}
	} else {
		struct samr_SetUserInfo2 sui2;
		struct samr_SetUserInfo sui;
		union samr_UserInfo u_info;
		DATA_BLOB session_key;

		encode_pw_buffer(u_info.info24.password.data,
				 cli_credentials_get_password(wks_creds),
				 STR_UNICODE);
		/* just to make this test pass */
		u_info.info24.password_expired = 1;

		status = dcerpc_fetch_session_key(samr_pipe, &session_key);
		if (!NT_STATUS_IS_OK(status)) {
			torture_comment(tctx, "dcerpc_fetch_session_key failed\n");
			goto done;
		}
		arcfour_crypt_blob(u_info.info24.password.data, 516,
				   &session_key);
		sui2.in.user_handle = wks_handle;
		sui2.in.info = &u_info;
		sui2.in.level = 24;

		status = dcerpc_samr_SetUserInfo2_r(samr_handle, mem_ctx, &sui2);
		if (!NT_STATUS_IS_OK(status)) {
			torture_comment(tctx, "samr_SetUserInfo(24) failed: %s\n",
				 nt_errstr(status));
			goto done;
		}
		if (!NT_STATUS_IS_OK(sui2.out.result)) {
			torture_comment(tctx, "samr_SetUserInfo(24) failed: %s\n",
				 nt_errstr(sui2.out.result));
			goto done;
		}

		u_info.info16.acct_flags = ACB_WSTRUST;
		sui.in.user_handle = wks_handle;
		sui.in.info = &u_info;
		sui.in.level = 16;

		status = dcerpc_samr_SetUserInfo_r(samr_handle, mem_ctx, &sui);
		if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(sui.out.result)) {
			torture_comment(tctx, "samr_SetUserInfo(16) failed\n");
			goto done;
		}
	}

	{
		struct samr_QueryUserInfo q;
		union samr_UserInfo *info;

		q.in.user_handle = wks_handle;
		q.in.level = 21;
		q.out.info = &info;

		status = dcerpc_samr_QueryUserInfo_r(samr_handle, mem_ctx, &q);
		if (!NT_STATUS_IS_OK(status)) {
			torture_warning(tctx, "QueryUserInfo failed: %s\n",
				  nt_errstr(status));
			goto done;
		}
		if (!NT_STATUS_IS_OK(q.out.result)) {
			torture_warning(tctx, "QueryUserInfo failed: %s\n",
				  nt_errstr(q.out.result));
			goto done;
		}

		if (use_level25) {
			if (last_password_change
			    == info->info21.last_password_change) {
				torture_warning(tctx, "last_password_change unchanged "
					 "during join, level25 must change "
					 "it\n");
				goto done;
			}
		}
		else {
			if (last_password_change
			    != info->info21.last_password_change) {
				torture_warning(tctx, "last_password_change changed "
					 "during join, level24 doesn't "
					 "change it\n");
				goto done;
			}
		}
	}

	ret = true;

 done:
	talloc_free(mem_ctx);
	return ret;
}

/*
 * Do a ReqChallenge/Auth2 and get the wks creds
 */

static bool auth2(struct torture_context *tctx,
		  struct smbcli_state *cli,
		  struct cli_credentials *wks_cred)
{
	TALLOC_CTX *mem_ctx;
	struct dcerpc_pipe *net_pipe;
	struct dcerpc_binding_handle *net_handle;
	bool result = false;
	NTSTATUS status;
	struct netr_ServerReqChallenge r;
	struct netr_Credential netr_cli_creds;
	struct netr_Credential netr_srv_creds;
	uint32_t negotiate_flags;
	struct netr_ServerAuthenticate2 a;
	struct netlogon_creds_CredentialState *creds_state;
	struct netr_Credential netr_cred;
	struct samr_Password mach_pw;

	mem_ctx = talloc_new(NULL);
	if (mem_ctx == NULL) {
		torture_comment(tctx, "talloc_new failed\n");
		return false;
	}

	net_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
	if (net_pipe == NULL) {
		torture_comment(tctx, "dcerpc_pipe_init failed\n");
		goto done;
	}
	net_handle = net_pipe->binding_handle;

	status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "dcerpc_pipe_open_smb failed: %s\n",
			 nt_errstr(status));
		goto done;
	}

	status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "dcerpc_bind_auth_none failed: %s\n",
			 nt_errstr(status));
		goto done;
	}

	r.in.computer_name = cli_credentials_get_workstation(wks_cred);
	r.in.server_name = talloc_asprintf(
		mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
	if (r.in.server_name == NULL) {
		torture_comment(tctx, "talloc_asprintf failed\n");
		goto done;
	}
	generate_random_buffer(netr_cli_creds.data,
			       sizeof(netr_cli_creds.data));
	r.in.credentials = &netr_cli_creds;
	r.out.return_credentials = &netr_srv_creds;

	status = dcerpc_netr_ServerReqChallenge_r(net_handle, mem_ctx, &r);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "netr_ServerReqChallenge failed: %s\n",
			 nt_errstr(status));
		goto done;
	}
	if (!NT_STATUS_IS_OK(r.out.result)) {
		torture_comment(tctx, "netr_ServerReqChallenge failed: %s\n",
			 nt_errstr(r.out.result));
		goto done;
	}

	negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
	E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);

	a.in.server_name = talloc_asprintf(
		mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
	a.in.account_name = talloc_asprintf(
		mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
	a.in.computer_name = cli_credentials_get_workstation(wks_cred);
	a.in.secure_channel_type = SEC_CHAN_WKSTA;
	a.in.negotiate_flags = &negotiate_flags;
	a.out.negotiate_flags = &negotiate_flags;
	a.in.credentials = &netr_cred;
	a.out.return_credentials = &netr_cred;

	creds_state = netlogon_creds_client_init(mem_ctx,
						 a.in.account_name,
						 a.in.computer_name,
						 r.in.credentials,
						 r.out.return_credentials, &mach_pw,
						 &netr_cred, negotiate_flags);
	torture_assert(tctx, (creds_state != NULL), "memory allocation failed");

	status = dcerpc_netr_ServerAuthenticate2_r(net_handle, mem_ctx, &a);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "netr_ServerServerAuthenticate2 failed: %s\n",
			 nt_errstr(status));
		goto done;
	}
	if (!NT_STATUS_IS_OK(a.out.result)) {
		torture_comment(tctx, "netr_ServerServerAuthenticate2 failed: %s\n",
			 nt_errstr(a.out.result));
		goto done;
	}

	if (!netlogon_creds_client_check(creds_state, a.out.return_credentials)) {
		torture_comment(tctx, "creds_client_check failed\n");
		goto done;
	}

	cli_credentials_set_netlogon_creds(wks_cred, creds_state);

	result = true;

 done:
	talloc_free(mem_ctx);
	return result;
}

/*
 * Do a couple of schannel protected Netlogon ops: Interactive and Network
 * login, and change the wks password
 */

static bool schan(struct torture_context *tctx,
		  struct smbcli_state *cli,
		  struct cli_credentials *wks_creds,
		  struct cli_credentials *user_creds)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct dcerpc_pipe *net_pipe;
	struct dcerpc_binding_handle *net_handle;
	int i;

	mem_ctx = talloc_new(NULL);
	if (mem_ctx == NULL) {
		torture_comment(tctx, "talloc_new failed\n");
		return false;
	}

	net_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
	if (net_pipe == NULL) {
		torture_comment(tctx, "dcerpc_pipe_init failed\n");
		goto done;
	}
	net_handle = net_pipe->binding_handle;

	status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "dcerpc_pipe_open_smb failed: %s\n",
			 nt_errstr(status));
		goto done;
	}

#if 0
	net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
		DCERPC_DEBUG_PRINT_OUT;
#endif
#if 1
	net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
	status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
				  wks_creds, lpcfg_gensec_settings(tctx->lp_ctx, tctx->lp_ctx), DCERPC_AUTH_TYPE_SCHANNEL,
				  DCERPC_AUTH_LEVEL_PRIVACY,
				  NULL);
#else
	status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
#endif
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "schannel bind failed: %s\n", nt_errstr(status));
		goto done;
	}


	for (i=2; i<4; i++) {
		int flags;
		DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
		struct netlogon_creds_CredentialState *creds_state;
		struct netr_Authenticator netr_auth, netr_auth2;
		struct netr_NetworkInfo ninfo;
		struct netr_PasswordInfo pinfo;
		struct netr_LogonSamLogon r;
		union netr_LogonLevel logon;
		union netr_Validation validation;
		uint8_t authoritative;
		struct netr_Authenticator return_authenticator;

		flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
			CLI_CRED_NTLMv2_AUTH;

		chal = data_blob_talloc(mem_ctx, NULL, 8);
		if (chal.data == NULL) {
			torture_comment(tctx, "data_blob_talloc failed\n");
			goto done;
		}

		generate_random_buffer(chal.data, chal.length);
		names_blob = NTLMv2_generate_names_blob(
			mem_ctx,
			cli_credentials_get_workstation(user_creds),
			cli_credentials_get_domain(user_creds));
		status = cli_credentials_get_ntlm_response(
			user_creds, mem_ctx, &flags, chal, names_blob,
			&lm_resp, &nt_resp, NULL, NULL);
		if (!NT_STATUS_IS_OK(status)) {
			torture_comment(tctx, "cli_credentials_get_ntlm_response failed:"
				 " %s\n", nt_errstr(status));
			goto done;
		}

		creds_state = cli_credentials_get_netlogon_creds(wks_creds);
		netlogon_creds_client_authenticator(creds_state, &netr_auth);

		ninfo.identity_info.account_name.string =
			cli_credentials_get_username(user_creds);
		ninfo.identity_info.domain_name.string =
			cli_credentials_get_domain(user_creds);
		ninfo.identity_info.parameter_control = 0;
		ninfo.identity_info.logon_id_low = 0;
		ninfo.identity_info.logon_id_high = 0;
		ninfo.identity_info.workstation.string =
			cli_credentials_get_workstation(user_creds);
		memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
		ninfo.nt.length = nt_resp.length;
		ninfo.nt.data = nt_resp.data;
		ninfo.lm.length = lm_resp.length;
		ninfo.lm.data = lm_resp.data;

		logon.network = &ninfo;

		r.in.server_name = talloc_asprintf(
			mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
		ZERO_STRUCT(netr_auth2);
		r.in.computer_name =
			cli_credentials_get_workstation(wks_creds);
		r.in.credential = &netr_auth;
		r.in.return_authenticator = &netr_auth2;
		r.in.logon_level = 2;
		r.in.validation_level = i;
		r.in.logon = &logon;
		r.out.validation = &validation;
		r.out.authoritative = &authoritative;
		r.out.return_authenticator = &return_authenticator;

		status = dcerpc_netr_LogonSamLogon_r(net_handle, mem_ctx, &r);
		if (!NT_STATUS_IS_OK(status)) {
			torture_comment(tctx, "netr_LogonSamLogon failed: %s\n",
				 nt_errstr(status));
			goto done;
		}
		if (!NT_STATUS_IS_OK(r.out.result)) {
			torture_comment(tctx, "netr_LogonSamLogon failed: %s\n",
				 nt_errstr(r.out.result));
			goto done;
		}

		if ((r.out.return_authenticator == NULL) ||
		    (!netlogon_creds_client_check(creds_state,
					 &r.out.return_authenticator->cred))) {
			torture_comment(tctx, "Credentials check failed!\n");
			goto done;
		}

		netlogon_creds_client_authenticator(creds_state, &netr_auth);

		pinfo.identity_info = ninfo.identity_info;
		ZERO_STRUCT(pinfo.lmpassword.hash);
		E_md4hash(cli_credentials_get_password(user_creds),
			  pinfo.ntpassword.hash);
		session_key = data_blob_talloc(mem_ctx,
					       creds_state->session_key, 16);
		arcfour_crypt_blob(pinfo.ntpassword.hash,
				   sizeof(pinfo.ntpassword.hash),
				   &session_key);

		logon.password = &pinfo;

		r.in.logon_level = 1;
		r.in.logon = &logon;
		r.out.return_authenticator = &return_authenticator;

		status = dcerpc_netr_LogonSamLogon_r(net_handle, mem_ctx, &r);
		if (!NT_STATUS_IS_OK(status)) {
			torture_comment(tctx, "netr_LogonSamLogon failed: %s\n",
				 nt_errstr(status));
			goto done;
		}
		if (!NT_STATUS_IS_OK(r.out.result)) {
			torture_comment(tctx, "netr_LogonSamLogon failed: %s\n",
				 nt_errstr(r.out.result));
			goto done;
		}

		if ((r.out.return_authenticator == NULL) ||
		    (!netlogon_creds_client_check(creds_state,
					 &r.out.return_authenticator->cred))) {
			torture_comment(tctx, "Credentials check failed!\n");
			goto done;
		}
	}

	{
		struct netr_ServerPasswordSet s;
		char *password = generate_random_password(wks_creds, 8, 255);
		struct netlogon_creds_CredentialState *creds_state;
		struct netr_Authenticator credential, return_authenticator;
		struct samr_Password new_password;

		s.in.server_name = talloc_asprintf(
			mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
		s.in.computer_name = cli_credentials_get_workstation(wks_creds);
		s.in.account_name = talloc_asprintf(
			mem_ctx, "%s$", s.in.computer_name);
		s.in.secure_channel_type = SEC_CHAN_WKSTA;
		s.in.credential = &credential;
		s.in.new_password = &new_password;
		s.out.return_authenticator = &return_authenticator;

		E_md4hash(password, new_password.hash);

		creds_state = cli_credentials_get_netlogon_creds(wks_creds);
		netlogon_creds_des_encrypt(creds_state, &new_password);
		netlogon_creds_client_authenticator(creds_state, &credential);

		status = dcerpc_netr_ServerPasswordSet_r(net_handle, mem_ctx, &s);
		if (!NT_STATUS_IS_OK(status)) {
			torture_comment(tctx, "ServerPasswordSet - %s\n", nt_errstr(status));
			goto done;
		}
		if (!NT_STATUS_IS_OK(s.out.result)) {
			torture_comment(tctx, "ServerPasswordSet - %s\n", nt_errstr(s.out.result));
			goto done;
		}

		if (!netlogon_creds_client_check(creds_state,
						 &s.out.return_authenticator->cred)) {
			torture_comment(tctx, "Credential chaining failed\n");
		}

		cli_credentials_set_password(wks_creds, password,
					     CRED_SPECIFIED);
	}

	ret = true;
 done:
	talloc_free(mem_ctx);
	return ret;
}

/*
 * Delete the wks account again
 */

static bool leave(struct torture_context *tctx,
		  struct smbcli_state *cli,
		  struct cli_credentials *admin_creds,
		  struct cli_credentials *wks_creds)
{
	char *wks_name = talloc_asprintf(
		NULL, "%s$", cli_credentials_get_workstation(wks_creds));
	bool ret;

	ret = delete_user(tctx, cli, admin_creds, wks_name);
	talloc_free(wks_name);
	return ret;
}

/*
 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
 */

static bool torture_netlogon_samba3(struct torture_context *torture)
{
	NTSTATUS status;
	struct smbcli_state *cli;
	struct cli_credentials *anon_creds;
	struct cli_credentials *wks_creds;
	const char *wks_name;
	int i;
	struct smbcli_options options;
	struct smbcli_session_options session_options;

	wks_name = torture_setting_string(torture, "wksname", NULL);
	if (wks_name == NULL) {
		wks_name = get_myname(torture);
	}

	if (!(anon_creds = cli_credentials_init_anon(torture))) {
		torture_fail(torture, "create_anon_creds failed\n");
	}

	lpcfg_smbcli_options(torture->lp_ctx, &options);
	lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);

	status = smbcli_full_connection(torture, &cli,
					torture_setting_string(torture, "host", NULL),
					lpcfg_smb_ports(torture->lp_ctx),
					"IPC$", NULL,
					lpcfg_socket_options(torture->lp_ctx),
					anon_creds,
					lpcfg_resolve_context(torture->lp_ctx),
					torture->ev, &options, &session_options,
					lpcfg_gensec_settings(torture, torture->lp_ctx));
	torture_assert_ntstatus_ok(torture, status, "smbcli_full_connection failed\n");

	wks_creds = cli_credentials_init(torture);
	if (wks_creds == NULL) {
		torture_fail(torture, "cli_credentials_init failed\n");
	}

	cli_credentials_set_conf(wks_creds, torture->lp_ctx);
	cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
	cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
	cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
	cli_credentials_set_password(wks_creds,
				     generate_random_password(wks_creds, 8, 255),
				     CRED_SPECIFIED);

	torture_assert(torture,
		join3(torture, cli, false, cmdline_credentials, wks_creds),
		"join failed");

	cli_credentials_set_domain(
		cmdline_credentials, cli_credentials_get_domain(wks_creds),
		CRED_SPECIFIED);

	for (i=0; i<2; i++) {

		/* Do this more than once, the routine "schan" changes
		 * the workstation password using the netlogon
		 * password change routine */

		int j;

		torture_assert(torture,
			auth2(torture, cli, wks_creds),
			"auth2 failed");

		for (j=0; j<2; j++) {
			torture_assert(torture,
				schan(torture, cli, wks_creds, cmdline_credentials),
				"schan failed");
		}
	}

	torture_assert(torture,
		leave(torture, cli, cmdline_credentials, wks_creds),
		"leave failed");

	return true;
}

/*
 * Do a simple join, testjoin and leave using specified smb and samr
 * credentials
 */

static bool test_join3(struct torture_context *tctx,
		       bool use_level25,
		       struct cli_credentials *smb_creds,
		       struct cli_credentials *samr_creds,
		       const char *wks_name)
{
	NTSTATUS status;
	struct smbcli_state *cli;
	struct cli_credentials *wks_creds;
	struct smbcli_options options;
	struct smbcli_session_options session_options;

	lpcfg_smbcli_options(tctx->lp_ctx, &options);
	lpcfg_smbcli_session_options(tctx->lp_ctx, &session_options);

	status = smbcli_full_connection(tctx, &cli,
					torture_setting_string(tctx, "host", NULL),
					lpcfg_smb_ports(tctx->lp_ctx),
					"IPC$", NULL, lpcfg_socket_options(tctx->lp_ctx),
					smb_creds, lpcfg_resolve_context(tctx->lp_ctx),
					tctx->ev, &options, &session_options,
					lpcfg_gensec_settings(tctx, tctx->lp_ctx));
	torture_assert_ntstatus_ok(tctx, status,
		"smbcli_full_connection failed");

	wks_creds = cli_credentials_init(cli);
	torture_assert(tctx, wks_creds, "cli_credentials_init failed");

	cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
	cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
	cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
	cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
	cli_credentials_set_password(wks_creds,
				     generate_random_password(wks_creds, 8, 255),
				     CRED_SPECIFIED);

	torture_assert(tctx,
		join3(tctx, cli, use_level25, samr_creds, wks_creds),
		"join failed");

	cli_credentials_set_domain(
		cmdline_credentials, cli_credentials_get_domain(wks_creds),
		CRED_SPECIFIED);

	torture_assert(tctx,
		auth2(tctx, cli, wks_creds),
		"auth2 failed");

	torture_assert(tctx,
		leave(tctx, cli, samr_creds, wks_creds),
		"leave failed");

	talloc_free(cli);

	return true;
}

/*
 * Test the different session key variants. Do it by joining, this uses the
 * session key in the setpassword routine. Test the join by doing the auth2.
 */

static bool torture_samba3_sessionkey(struct torture_context *torture)
{
	struct cli_credentials *anon_creds;
	const char *wks_name;

	wks_name = torture_setting_string(torture, "wksname", get_myname(torture));

	if (!(anon_creds = cli_credentials_init_anon(torture))) {
		torture_fail(torture, "create_anon_creds failed\n");
	}

	cli_credentials_set_workstation(anon_creds, wks_name, CRED_SPECIFIED);


	if (!torture_setting_bool(torture, "samba3", false)) {

		/* Samba3 in the build farm right now does this happily. Need
		 * to fix :-) */

		if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
			torture_fail(torture, "join using anonymous bind on an anonymous smb "
				 "connection succeeded -- HUH??\n");
		}
	}

	torture_assert(torture,
		test_join3(torture, false, anon_creds, cmdline_credentials, wks_name),
		"join using ntlmssp bind on an anonymous smb connection failed");

	torture_assert(torture,
		test_join3(torture, false, cmdline_credentials, NULL, wks_name),
		"join using anonymous bind on an authenticated smb connection failed");

	torture_assert(torture,
		test_join3(torture, false, cmdline_credentials, cmdline_credentials, wks_name),
		"join using ntlmssp bind on an authenticated smb connection failed");

	/*
	 * The following two are tests for setuserinfolevel 25
	 */

	torture_assert(torture,
		test_join3(torture, true, anon_creds, cmdline_credentials, wks_name),
		"join using ntlmssp bind on an anonymous smb connection failed");

	torture_assert(torture,
		test_join3(torture, true, cmdline_credentials, NULL, wks_name),
		"join using anonymous bind on an authenticated smb connection failed");

	return true;
}

/*
 * open pipe and bind, given an IPC$ context
 */

static NTSTATUS pipe_bind_smb(struct torture_context *tctx,
			      TALLOC_CTX *mem_ctx,
			      struct smbcli_tree *tree,
			      const char *pipe_name,
			      const struct ndr_interface_table *iface,
			      struct dcerpc_pipe **p)
{
	struct dcerpc_pipe *result;
	NTSTATUS status;

	if (!(result = dcerpc_pipe_init(mem_ctx, tctx->ev))) {
		return NT_STATUS_NO_MEMORY;
	}

	status = dcerpc_pipe_open_smb(result, tree, pipe_name);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "dcerpc_pipe_open_smb failed: %s\n",
			 nt_errstr(status));
		talloc_free(result);
		return status;
	}

	status = dcerpc_bind_auth_none(result, iface);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "schannel bind failed: %s\n", nt_errstr(status));
		talloc_free(result);
		return status;
	}

	*p = result;
	return NT_STATUS_OK;
}

/*
 * Sane wrapper around lsa_LookupNames
 */

static struct dom_sid *name2sid(struct torture_context *tctx,
				TALLOC_CTX *mem_ctx,
				struct dcerpc_pipe *p,
				const char *name,
				const char *domain)
{
	struct lsa_ObjectAttribute attr;
	struct lsa_QosInfo qos;
	struct lsa_OpenPolicy2 r;
	struct lsa_Close c;
	NTSTATUS status;
	struct policy_handle handle;
	struct lsa_LookupNames l;
	struct lsa_TransSidArray sids;
	struct lsa_RefDomainList *domains = NULL;
	struct lsa_String lsa_name;
	uint32_t count = 0;
	struct dom_sid *result;
	TALLOC_CTX *tmp_ctx;
	struct dcerpc_binding_handle *b = p->binding_handle;

	if (!(tmp_ctx = talloc_new(mem_ctx))) {
		return NULL;
	}

	qos.len = 0;
	qos.impersonation_level = 2;
	qos.context_mode = 1;
	qos.effective_only = 0;

	attr.len = 0;
	attr.root_dir = NULL;
	attr.object_name = NULL;
	attr.attributes = 0;
	attr.sec_desc = NULL;
	attr.sec_qos = &qos;

	r.in.system_name = "\\";
	r.in.attr = &attr;
	r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	r.out.handle = &handle;

	status = dcerpc_lsa_OpenPolicy2_r(b, tmp_ctx, &r);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "OpenPolicy2 failed - %s\n", nt_errstr(status));
		talloc_free(tmp_ctx);
		return NULL;
	}
	if (!NT_STATUS_IS_OK(r.out.result)) {
		torture_comment(tctx, "OpenPolicy2 failed - %s\n", nt_errstr(r.out.result));
		talloc_free(tmp_ctx);
		return NULL;
	}

	sids.count = 0;
	sids.sids = NULL;

	lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);

	l.in.handle = &handle;
	l.in.num_names = 1;
	l.in.names = &lsa_name;
	l.in.sids = &sids;
	l.in.level = 1;
	l.in.count = &count;
	l.out.count = &count;
	l.out.sids = &sids;
	l.out.domains = &domains;

	status = dcerpc_lsa_LookupNames_r(b, tmp_ctx, &l);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "LookupNames of %s failed - %s\n", lsa_name.string,
		       nt_errstr(status));
		talloc_free(tmp_ctx);
		return NULL;
	}
	if (!NT_STATUS_IS_OK(l.out.result)) {
		torture_comment(tctx, "LookupNames of %s failed - %s\n", lsa_name.string,
		       nt_errstr(l.out.result));
		talloc_free(tmp_ctx);
		return NULL;
	}

	result = dom_sid_add_rid(mem_ctx, domains->domains[0].sid,
				 l.out.sids->sids[0].rid);

	c.in.handle = &handle;
	c.out.handle = &handle;

	status = dcerpc_lsa_Close_r(b, tmp_ctx, &c);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
		talloc_free(tmp_ctx);
		return NULL;
	}
	if (!NT_STATUS_IS_OK(c.out.result)) {
		torture_comment(tctx, "dcerpc_lsa_Close failed - %s\n", nt_errstr(c.out.result));
		talloc_free(tmp_ctx);
		return NULL;
	}

	talloc_free(tmp_ctx);
	return result;
}

/*
 * Find out the user SID on this connection
 */

static struct dom_sid *whoami(struct torture_context *tctx,
			      TALLOC_CTX *mem_ctx,
			      struct smbcli_tree *tree)
{
	struct dcerpc_pipe *lsa;
	struct dcerpc_binding_handle *lsa_handle;
	struct lsa_GetUserName r;
	NTSTATUS status;
	struct lsa_String *authority_name_p = NULL;
	struct lsa_String *account_name_p = NULL;
	struct dom_sid *result;

	status = pipe_bind_smb(tctx, mem_ctx, tree, "\\pipe\\lsarpc",
			       &ndr_table_lsarpc, &lsa);
	if (!NT_STATUS_IS_OK(status)) {
		torture_warning(tctx, "Could not bind to LSA: %s\n",
			 nt_errstr(status));
		return NULL;
	}
	lsa_handle = lsa->binding_handle;

	r.in.system_name = "\\";
	r.in.account_name = &account_name_p;
	r.in.authority_name = &authority_name_p;
	r.out.account_name = &account_name_p;

	status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);

	authority_name_p = *r.out.authority_name;

	if (!NT_STATUS_IS_OK(status)) {
		torture_warning(tctx, "GetUserName failed - %s\n",
		       nt_errstr(status));
		talloc_free(lsa);
		return NULL;
	}
	if (!NT_STATUS_IS_OK(r.out.result)) {
		torture_warning(tctx, "GetUserName failed - %s\n",
		       nt_errstr(r.out.result));
		talloc_free(lsa);
		return NULL;
	}

	result = name2sid(tctx, mem_ctx, lsa, account_name_p->string,
			  authority_name_p->string);

	talloc_free(lsa);
	return result;
}

static int destroy_tree(struct smbcli_tree *tree)
{
	smb_tree_disconnect(tree);
	return 0;
}

/*
 * Do a tcon, given a session
 */

static NTSTATUS secondary_tcon(struct torture_context *tctx,
			       TALLOC_CTX *mem_ctx,
			       struct smbcli_session *session,
			       const char *sharename,
			       struct smbcli_tree **res)
{
	struct smbcli_tree *result;
	TALLOC_CTX *tmp_ctx;
	union smb_tcon tcon;
	NTSTATUS status;

	if (!(tmp_ctx = talloc_new(mem_ctx))) {
		return NT_STATUS_NO_MEMORY;
	}

	if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
		talloc_free(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	tcon.generic.level = RAW_TCON_TCONX;
	tcon.tconx.in.flags = TCONX_FLAG_EXTENDED_RESPONSE;
	tcon.tconx.in.flags |= TCONX_FLAG_EXTENDED_SIGNATURES;
	tcon.tconx.in.password = data_blob(NULL, 0);
	tcon.tconx.in.path = sharename;
	tcon.tconx.in.device = "?????";

	status = smb_raw_tcon(result, tmp_ctx, &tcon);
	if (!NT_STATUS_IS_OK(status)) {
		torture_warning(tctx, "smb_raw_tcon failed: %s\n",
			 nt_errstr(status));
		talloc_free(tmp_ctx);
		return status;
	}

	result->tid = tcon.tconx.out.tid;

	if (tcon.tconx.out.options & SMB_EXTENDED_SIGNATURES) {
		smb1cli_session_protect_session_key(result->session->smbXcli);
	}

	result = talloc_steal(mem_ctx, result);
	talloc_set_destructor(result, destroy_tree);
	talloc_free(tmp_ctx);
	*res = result;
	return NT_STATUS_OK;
}

/*
 * Test the getusername behaviour
 */

static bool torture_samba3_rpc_getusername(struct torture_context *torture)
{
	NTSTATUS status;
	struct smbcli_state *cli;
	bool ret = true;
	struct dom_sid *user_sid;
	struct dom_sid *created_sid;
	struct cli_credentials *anon_creds;
	struct cli_credentials *user_creds;
	char *domain_name;
	struct smbcli_options options;
	struct smbcli_session_options session_options;

	lpcfg_smbcli_options(torture->lp_ctx, &options);
	lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);

	status = smbcli_full_connection(
		torture, &cli, torture_setting_string(torture, "host", NULL),
		lpcfg_smb_ports(torture->lp_ctx),
		"IPC$", NULL, lpcfg_socket_options(torture->lp_ctx), cmdline_credentials,
		lpcfg_resolve_context(torture->lp_ctx), torture->ev, &options,
		&session_options, lpcfg_gensec_settings(torture, torture->lp_ctx));
	torture_assert_ntstatus_ok(torture, status, "smbcli_full_connection failed\n");

	if (!(user_sid = whoami(torture, torture, cli->tree))) {
		torture_fail(torture, "whoami on auth'ed connection failed\n");
	}

	talloc_free(cli);

	if (!(anon_creds = cli_credentials_init_anon(torture))) {
		torture_fail(torture, "create_anon_creds failed\n");
	}

	status = smbcli_full_connection(
		torture, &cli, torture_setting_string(torture, "host", NULL),
		lpcfg_smb_ports(torture->lp_ctx), "IPC$", NULL,
		lpcfg_socket_options(torture->lp_ctx), anon_creds,
		lpcfg_resolve_context(torture->lp_ctx),
		torture->ev, &options, &session_options,
		lpcfg_gensec_settings(torture, torture->lp_ctx));
	torture_assert_ntstatus_ok(torture, status, "anon smbcli_full_connection failed\n");

	if (!(user_sid = whoami(torture, torture, cli->tree))) {
		torture_fail(torture, "whoami on anon connection failed\n");
	}

	torture_assert_sid_equal(torture, user_sid, dom_sid_parse_talloc(torture, "s-1-5-7"),
		"Anon lsa_GetUserName returned unexpected SID");

	if (!(user_creds = cli_credentials_init(torture))) {
		torture_fail(torture, "cli_credentials_init failed\n");
	}

	cli_credentials_set_conf(user_creds, torture->lp_ctx);
	cli_credentials_set_username(user_creds, "torture_username",
				     CRED_SPECIFIED);
	cli_credentials_set_password(user_creds,
				     generate_random_password(user_creds, 8, 255),
				     CRED_SPECIFIED);

	if (!create_user(torture, torture, cli, cmdline_credentials,
			 cli_credentials_get_username(user_creds),
			 cli_credentials_get_password(user_creds),
			 &domain_name, &created_sid)) {
		torture_fail(torture, "create_user failed\n");
	}

	cli_credentials_set_domain(user_creds, domain_name,
				   CRED_SPECIFIED);

	{
		struct smbcli_session *session2;
		struct smb_composite_sesssetup setup;
		struct smbcli_tree *tree;

		session2 = smbcli_session_init(cli->transport, torture, false, session_options);
		if (session2 == NULL) {
			torture_fail(torture, "smbcli_session_init failed\n");
		}

		setup.in.sesskey = cli->transport->negotiate.sesskey;
		setup.in.capabilities = cli->transport->negotiate.capabilities;
		setup.in.workgroup = "";
		setup.in.credentials = user_creds;
		setup.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);

		status = smb_composite_sesssetup(session2, &setup);
		torture_assert_ntstatus_ok(torture, status, "session setup with new user failed");

		session2->vuid = setup.out.vuid;

		if (!NT_STATUS_IS_OK(secondary_tcon(torture, torture, session2,
						    "IPC$", &tree))) {
			torture_fail(torture, "secondary_tcon failed\n");
		}

		if (!(user_sid = whoami(torture, torture, tree))) {
			torture_fail_goto(torture, del, "whoami on user connection failed\n");
			ret = false;
			goto del;
		}

		talloc_free(tree);
	}

	torture_comment(torture, "Created %s, found %s\n",
		 dom_sid_string(torture, created_sid),
		 dom_sid_string(torture, user_sid));

	if (!dom_sid_equal(created_sid, user_sid)) {
		ret = false;
	}

 del:
	if (!delete_user(torture, cli,
			 cmdline_credentials,
			 cli_credentials_get_username(user_creds))) {
		torture_fail(torture, "delete_user failed\n");
	}

	return ret;
}

static bool test_NetShareGetInfo(struct torture_context *tctx,
				 struct dcerpc_pipe *p,
				 const char *sharename)
{
	NTSTATUS status;
	struct srvsvc_NetShareGetInfo r;
	union srvsvc_NetShareInfo info;
	uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
	int i;
	bool ret = true;
	struct dcerpc_binding_handle *b = p->binding_handle;

	r.in.server_unc = talloc_asprintf(tctx, "\\\\%s",
					  dcerpc_server_name(p));
	r.in.share_name = sharename;
	r.out.info = &info;

	for (i=0;i<ARRAY_SIZE(levels);i++) {
		r.in.level = levels[i];

		torture_comment(tctx, "Testing NetShareGetInfo level %u on share '%s'\n",
		       r.in.level, r.in.share_name);

		status = dcerpc_srvsvc_NetShareGetInfo_r(b, tctx, &r);
		if (!NT_STATUS_IS_OK(status)) {
			torture_warning(tctx, "NetShareGetInfo level %u on share '%s' failed"
			       " - %s\n", r.in.level, r.in.share_name,
			       nt_errstr(status));
			ret = false;
			continue;
		}
		if (!W_ERROR_IS_OK(r.out.result)) {
			torture_warning(tctx, "NetShareGetInfo level %u on share '%s' failed "
			       "- %s\n", r.in.level, r.in.share_name,
			       win_errstr(r.out.result));
			ret = false;
			continue;
		}
	}

	return ret;
}

static bool test_NetShareEnum(struct torture_context *tctx,
			      struct dcerpc_pipe *p,
			      const char **one_sharename)
{
	NTSTATUS status;
	struct srvsvc_NetShareEnum r;
	struct srvsvc_NetShareInfoCtr info_ctr;
	struct srvsvc_NetShareCtr0 c0;
	struct srvsvc_NetShareCtr1 c1;
	struct srvsvc_NetShareCtr2 c2;
	struct srvsvc_NetShareCtr501 c501;
	struct srvsvc_NetShareCtr502 c502;
	struct srvsvc_NetShareCtr1004 c1004;
	struct srvsvc_NetShareCtr1005 c1005;
	struct srvsvc_NetShareCtr1006 c1006;
	struct srvsvc_NetShareCtr1007 c1007;
	uint32_t totalentries = 0;
	uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
	int i;
	bool ret = true;
	struct dcerpc_binding_handle *b = p->binding_handle;

	ZERO_STRUCT(info_ctr);

	r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
	r.in.info_ctr = &info_ctr;
	r.in.max_buffer = (uint32_t)-1;
	r.in.resume_handle = NULL;
	r.out.totalentries = &totalentries;
	r.out.info_ctr = &info_ctr;

	for (i=0;i<ARRAY_SIZE(levels);i++) {
		info_ctr.level = levels[i];

		switch (info_ctr.level) {
		case 0:
			ZERO_STRUCT(c0);
			info_ctr.ctr.ctr0 = &c0;
			break;
		case 1:
			ZERO_STRUCT(c1);
			info_ctr.ctr.ctr1 = &c1;
			break;
		case 2:
			ZERO_STRUCT(c2);
			info_ctr.ctr.ctr2 = &c2;
			break;
		case 501:
			ZERO_STRUCT(c501);
			info_ctr.ctr.ctr501 = &c501;
			break;
		case 502:
			ZERO_STRUCT(c502);
			info_ctr.ctr.ctr502 = &c502;
			break;
		case 1004:
			ZERO_STRUCT(c1004);
			info_ctr.ctr.ctr1004 = &c1004;
			break;
		case 1005:
			ZERO_STRUCT(c1005);
			info_ctr.ctr.ctr1005 = &c1005;
			break;
		case 1006:
			ZERO_STRUCT(c1006);
			info_ctr.ctr.ctr1006 = &c1006;
			break;
		case 1007:
			ZERO_STRUCT(c1007);
			info_ctr.ctr.ctr1007 = &c1007;
			break;
		}

		torture_comment(tctx, "Testing NetShareEnum level %u\n", info_ctr.level);

		status = dcerpc_srvsvc_NetShareEnum_r(b, tctx, &r);
		if (!NT_STATUS_IS_OK(status)) {
			torture_warning(tctx, "NetShareEnum level %u failed - %s\n",
			       info_ctr.level, nt_errstr(status));
			ret = false;
			continue;
		}
		if (!W_ERROR_IS_OK(r.out.result)) {
			torture_warning(tctx, "NetShareEnum level %u failed - %s\n",
			       info_ctr.level, win_errstr(r.out.result));
			continue;
		}
		if (info_ctr.level == 0) {
			struct srvsvc_NetShareCtr0 *ctr = r.out.info_ctr->ctr.ctr0;
			if (ctr->count > 0) {
				*one_sharename = ctr->array[0].name;
			}
		}
	}

	return ret;
}

static bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
{
	struct dcerpc_pipe *p;
	const char *sharename = NULL;
	bool ret = true;

	torture_assert_ntstatus_ok(torture,
		torture_rpc_connection(torture, &p, &ndr_table_srvsvc),
		"failed to open srvsvc");

	ret &= test_NetShareEnum(torture, p, &sharename);
	if (sharename == NULL) {
		torture_comment(torture, "did not get sharename\n");
	} else {
		ret &= test_NetShareGetInfo(torture, p, sharename);
	}

	return ret;
}

/*
 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
 * NT_STATUS_NO_SAM_ACCOUNT
 */

static bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	struct dcerpc_pipe *net_pipe;
	struct dcerpc_binding_handle *net_handle;
	char *wksname;
	bool result = false;
	NTSTATUS status;
	struct netr_ServerReqChallenge r;
	struct netr_Credential netr_cli_creds;
	struct netr_Credential netr_srv_creds;
	uint32_t negotiate_flags;
	struct netr_ServerAuthenticate2 a;
	struct netlogon_creds_CredentialState *creds_state;
	struct netr_Credential netr_cred;
	struct samr_Password mach_pw;
	struct smbcli_state *cli;

	if (!(mem_ctx = talloc_new(torture))) {
		torture_comment(torture, "talloc_new failed\n");
		return false;
	}

	if (!(wksname = generate_random_str_list(
		      mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
		torture_comment(torture, "generate_random_str_list failed\n");
		goto done;
	}

	if (!(torture_open_connection_share(
		      mem_ctx, &cli,
		      torture, torture_setting_string(torture, "host", NULL),
		      "IPC$", torture->ev))) {
		torture_comment(torture, "IPC$ connection failed\n");
		goto done;
	}

	if (!(net_pipe = dcerpc_pipe_init(mem_ctx, torture->ev))) {
		torture_comment(torture, "dcerpc_pipe_init failed\n");
		goto done;
	}
	net_handle = net_pipe->binding_handle;

	status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "dcerpc_pipe_open_smb failed: %s\n",
			 nt_errstr(status));
		goto done;
	}

	status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "dcerpc_bind_auth_none failed: %s\n",
			 nt_errstr(status));
		goto done;
	}

	r.in.computer_name = wksname;
	r.in.server_name = talloc_asprintf(
		mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
	if (r.in.server_name == NULL) {
		torture_comment(torture, "talloc_asprintf failed\n");
		goto done;
	}
	generate_random_buffer(netr_cli_creds.data,
			       sizeof(netr_cli_creds.data));
	r.in.credentials = &netr_cli_creds;
	r.out.return_credentials = &netr_srv_creds;

	status = dcerpc_netr_ServerReqChallenge_r(net_handle, mem_ctx, &r);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(torture, "netr_ServerReqChallenge failed: %s\n",
			 nt_errstr(status));
		goto done;
	}
	if (!NT_STATUS_IS_OK(r.out.result)) {
		torture_comment(torture, "netr_ServerReqChallenge failed: %s\n",
			 nt_errstr(r.out.result));
		goto done;
	}

	negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
	E_md4hash("foobar", mach_pw.hash);

	a.in.server_name = talloc_asprintf(
		mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
	a.in.account_name = talloc_asprintf(
		mem_ctx, "%s$", wksname);
	a.in.computer_name = wksname;
	a.in.secure_channel_type = SEC_CHAN_WKSTA;
	a.in.negotiate_flags = &negotiate_flags;
	a.out.negotiate_flags = &negotiate_flags;
	a.in.credentials = &netr_cred;
	a.out.return_credentials = &netr_cred;

	creds_state = netlogon_creds_client_init(mem_ctx,
						 a.in.account_name,
						 a.in.computer_name,
						 r.in.credentials,
						 r.out.return_credentials, &mach_pw,
						 &netr_cred, negotiate_flags);
	torture_assert(torture, (creds_state != NULL), "memory allocation failed");

	status = dcerpc_netr_ServerAuthenticate2_r(net_handle, mem_ctx, &a);
	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}
	if (!NT_STATUS_EQUAL(a.out.result, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
		torture_comment(torture, "dcerpc_netr_ServerAuthenticate2 returned %s, "
			 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
			 nt_errstr(a.out.result));
		goto done;
	}

	result = true;
 done:
	talloc_free(mem_ctx);
	return result;
}

static struct security_descriptor *get_sharesec(struct torture_context *tctx,
						TALLOC_CTX *mem_ctx,
						struct smbcli_session *sess,
						const char *sharename)
{
	struct smbcli_tree *tree;
	TALLOC_CTX *tmp_ctx;
	struct dcerpc_pipe *p;
	struct dcerpc_binding_handle *b;
	NTSTATUS status;
	struct srvsvc_NetShareGetInfo r;
	union srvsvc_NetShareInfo info;
	struct security_descriptor *result;

	if (!(tmp_ctx = talloc_new(mem_ctx))) {
		torture_comment(tctx, "talloc_new failed\n");
		return NULL;
	}

	if (!NT_STATUS_IS_OK(secondary_tcon(tctx, tmp_ctx, sess, "IPC$", &tree))) {
		torture_comment(tctx, "secondary_tcon failed\n");
		talloc_free(tmp_ctx);
		return NULL;
	}

	status = pipe_bind_smb(tctx, mem_ctx, tree, "\\pipe\\srvsvc",
			       &ndr_table_srvsvc, &p);
	if (!NT_STATUS_IS_OK(status)) {
		torture_warning(tctx, "could not bind to srvsvc pipe: %s\n",
			 nt_errstr(status));
		talloc_free(tmp_ctx);
		return NULL;
	}
	b = p->binding_handle;

#if 0
	p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
#endif

	r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
					  dcerpc_server_name(p));
	r.in.share_name = sharename;
	r.in.level = 502;
	r.out.info = &info;

	status = dcerpc_srvsvc_NetShareGetInfo_r(b, tmp_ctx, &r);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "srvsvc_NetShareGetInfo failed: %s\n",
			 nt_errstr(status));
		talloc_free(tmp_ctx);
		return NULL;
	}
	if (!W_ERROR_IS_OK(r.out.result)) {
		torture_comment(tctx, "srvsvc_NetShareGetInfo failed: %s\n",
			 win_errstr(r.out.result));
		talloc_free(tmp_ctx);
		return NULL;
	}

	result = talloc_steal(mem_ctx, info.info502->sd_buf.sd);
	talloc_free(tmp_ctx);
	return result;
}

static NTSTATUS set_sharesec(struct torture_context *tctx,
			     TALLOC_CTX *mem_ctx,
			     struct smbcli_session *sess,
			     const char *sharename,
			     struct security_descriptor *sd)
{
	struct smbcli_tree *tree;
	TALLOC_CTX *tmp_ctx;
	struct dcerpc_pipe *p;
	struct dcerpc_binding_handle *b;
	NTSTATUS status;
	struct sec_desc_buf i;
	struct srvsvc_NetShareSetInfo r;
	union srvsvc_NetShareInfo info;
	uint32_t error = 0;

	if (!(tmp_ctx = talloc_new(mem_ctx))) {
		torture_comment(tctx, "talloc_new failed\n");
		return NT_STATUS_NO_MEMORY;
	}

	if (!NT_STATUS_IS_OK(secondary_tcon(tctx, tmp_ctx, sess, "IPC$", &tree))) {
		torture_comment(tctx, "secondary_tcon failed\n");
		talloc_free(tmp_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}

	status = pipe_bind_smb(tctx, mem_ctx, tree, "\\pipe\\srvsvc",
			       &ndr_table_srvsvc, &p);
	if (!NT_STATUS_IS_OK(status)) {
		torture_warning(tctx, "could not bind to srvsvc pipe: %s\n",
			 nt_errstr(status));
		talloc_free(tmp_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}
	b = p->binding_handle;

#if 0
	p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
#endif

	r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
					  dcerpc_server_name(p));
	r.in.share_name = sharename;
	r.in.level = 1501;
	i.sd = sd;
	info.info1501 = &i;
	r.in.info = &info;
	r.in.parm_error = &error;

	status = dcerpc_srvsvc_NetShareSetInfo_r(b, tmp_ctx, &r);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "srvsvc_NetShareSetInfo failed: %s\n",
			 nt_errstr(status));
	}
	if (!W_ERROR_IS_OK(r.out.result)) {
		torture_comment(tctx, "srvsvc_NetShareSetInfo failed: %s\n",
			win_errstr(r.out.result));
		status = werror_to_ntstatus(r.out.result);
	}
	talloc_free(tmp_ctx);
	return status;
}

bool try_tcon(struct torture_context *tctx,
	      TALLOC_CTX *mem_ctx,
	      struct security_descriptor *orig_sd,
	      struct smbcli_session *session,
	      const char *sharename, const struct dom_sid *user_sid,
	      unsigned int access_mask, NTSTATUS expected_tcon,
	      NTSTATUS expected_mkdir)
{
	TALLOC_CTX *tmp_ctx;
	struct smbcli_tree *rmdir_tree, *tree;
	struct dom_sid *domain_sid;
	uint32_t rid;
	struct security_descriptor *sd;
	NTSTATUS status;
	bool ret = true;

	if (!(tmp_ctx = talloc_new(mem_ctx))) {
		torture_comment(tctx, "talloc_new failed\n");
		return false;
	}

	status = secondary_tcon(tctx, tmp_ctx, session, sharename, &rmdir_tree);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "first tcon to delete dir failed\n");
		talloc_free(tmp_ctx);
		return false;
	}

	smbcli_rmdir(rmdir_tree, "sharesec_testdir");

	if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
					       &domain_sid, &rid))) {
		torture_comment(tctx, "dom_sid_split_rid failed\n");
		talloc_free(tmp_ctx);
		return false;
	}

	sd = security_descriptor_dacl_create(
		tmp_ctx, 0, "S-1-5-32-544",
		dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
							DOMAIN_RID_USERS)),
		dom_sid_string(mem_ctx, user_sid),
		SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
	if (sd == NULL) {
		torture_comment(tctx, "security_descriptor_dacl_create failed\n");
		talloc_free(tmp_ctx);
                return false;
        }

	status = set_sharesec(tctx, mem_ctx, session, sharename, sd);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "custom set_sharesec failed: %s\n",
			 nt_errstr(status));
		talloc_free(tmp_ctx);
                return false;
	}

	status = secondary_tcon(tctx, tmp_ctx, session, sharename, &tree);
	if (!NT_STATUS_EQUAL(status, expected_tcon)) {
		torture_comment(tctx, "Expected %s, got %s\n", nt_errstr(expected_tcon),
			 nt_errstr(status));
		ret = false;
		goto done;
	}

	if (!NT_STATUS_IS_OK(status)) {
		/* An expected non-access, no point in trying to write */
		goto done;
	}

	status = smbcli_mkdir(tree, "sharesec_testdir");
	if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
		torture_warning(tctx, "Expected %s, got %s\n",
			 nt_errstr(expected_mkdir), nt_errstr(status));
		ret = false;
	}

 done:
	smbcli_rmdir(rmdir_tree, "sharesec_testdir");

	status = set_sharesec(tctx, mem_ctx, session, sharename, orig_sd);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "custom set_sharesec failed: %s\n",
			 nt_errstr(status));
		talloc_free(tmp_ctx);
                return false;
	}

	talloc_free(tmp_ctx);
	return ret;
}

static bool torture_samba3_rpc_sharesec(struct torture_context *torture)
{
	struct smbcli_state *cli = NULL;
	struct security_descriptor *sd = NULL;
	struct dom_sid *user_sid = NULL;
	const char *testuser_passwd = NULL;
	struct cli_credentials *test_credentials = NULL;
	struct smbcli_options options;
	struct smbcli_session_options session_options;
	NTSTATUS status;
	struct test_join *tj = NULL;
	struct dcerpc_pipe *lsa_pipe = NULL;
	const char *priv_array[1];

	/* Create a new user. The normal user has SeBackup and SeRestore
	   privs so we can't lock them out with a share security descriptor. */
	tj = torture_create_testuser(torture,
					"sharesec_user",
					torture_setting_string(torture, "workgroup", NULL),
					ACB_NORMAL,
					&testuser_passwd);
	if (!tj) {
		torture_fail(torture, "Creating sharesec_user failed\n");
	}

	/* Give them SeDiskOperatorPrivilege but no other privs. */
	status = torture_rpc_connection(torture, &lsa_pipe, &ndr_table_lsarpc);
	if (!NT_STATUS_IS_OK(status)) {
		torture_delete_testuser(torture, tj, "sharesec_user");
		talloc_free(tj);
		torture_fail(torture, "Error connecting to LSA pipe");
	}

	priv_array[0] = "SeDiskOperatorPrivilege";
	if (!torture_setup_privs(torture,
				lsa_pipe,
				1,
				priv_array,
				torture_join_user_sid(tj))) {
		talloc_free(lsa_pipe);
		torture_delete_testuser(torture, tj, "sharesec_user");
		talloc_free(tj);
		torture_fail(torture, "Failed to setup privs\n");
	}
	talloc_free(lsa_pipe);

	test_credentials = cli_credentials_init(torture);
	cli_credentials_set_workstation(test_credentials, "localhost", CRED_SPECIFIED);
	cli_credentials_set_domain(test_credentials, lpcfg_workgroup(torture->lp_ctx),
			CRED_SPECIFIED);
	cli_credentials_set_username(test_credentials, "sharesec_user", CRED_SPECIFIED);
	cli_credentials_set_password(test_credentials, testuser_passwd, CRED_SPECIFIED);

	ZERO_STRUCT(options);
	ZERO_STRUCT(session_options);
	lpcfg_smbcli_options(torture->lp_ctx, &options);
	lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);

	status = smbcli_full_connection(torture,
					&cli,
					torture_setting_string(torture, "host", NULL),
					lpcfg_smb_ports(torture->lp_ctx),
					"IPC$",
					NULL,
					lpcfg_socket_options(torture->lp_ctx),
					test_credentials,
					lpcfg_resolve_context(torture->lp_ctx),
					torture->ev,
					&options,
					&session_options,
					lpcfg_gensec_settings(torture, torture->lp_ctx));
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(cli);
		torture_delete_testuser(torture, tj, "sharesec_user");
		talloc_free(tj);
		torture_fail(torture, "Failed to open connection\n");
	}

	if (!(user_sid = whoami(torture, torture, cli->tree))) {
		talloc_free(cli);
		torture_delete_testuser(torture, tj, "sharesec_user");
		talloc_free(tj);
		torture_fail(torture, "whoami failed\n");
	}

	sd = get_sharesec(torture, torture, cli->session,
			  torture_setting_string(torture, "share", NULL));

	if (!try_tcon(torture, torture, sd, cli->session,
			torture_setting_string(torture, "share", NULL),
			user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK)) {
		talloc_free(cli);
		torture_delete_testuser(torture, tj, "sharesec_user");
		talloc_free(tj);
		torture_fail(torture, "failed to test tcon with 0 access_mask");
	}

	if (!try_tcon(torture, torture, sd, cli->session,
			torture_setting_string(torture, "share", NULL),
			user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
			NT_STATUS_MEDIA_WRITE_PROTECTED)) {
		talloc_free(cli);
		torture_delete_testuser(torture, tj, "sharesec_user");
		talloc_free(tj);
		torture_fail(torture, "failed to test tcon with SEC_FILE_READ_DATA access_mask");
	}

	/* sharesec_user doesn't have any rights on the underlying file system.
	   Go back to the normal user. */

	talloc_free(cli);
	cli = NULL;
	torture_delete_testuser(torture, tj, "sharesec_user");
	talloc_free(tj);
	tj = NULL;

	if (!(torture_open_connection_share(
		      torture, &cli, torture, torture_setting_string(torture, "host", NULL),
		      "IPC$", torture->ev))) {
		torture_fail(torture, "IPC$ connection failed\n");
	}

	if (!(user_sid = whoami(torture, torture, cli->tree))) {
		torture_fail(torture, "whoami failed\n");
	}
	torture_assert(torture, try_tcon(
			torture, torture, sd, cli->session,
			torture_setting_string(torture, "share", NULL),
			user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK),
			"failed to test tcon with SEC_FILE_ALL access_mask")

	return true;
}

static bool torture_samba3_rpc_lsa(struct torture_context *torture)
{
	struct dcerpc_pipe *p;
	struct dcerpc_binding_handle *b;
	struct policy_handle lsa_handle;

	torture_assert_ntstatus_ok(torture,
		torture_rpc_connection(torture, &p, &ndr_table_lsarpc),
		"failed to setup lsarpc");

	b = p->binding_handle;

	{
		struct lsa_ObjectAttribute attr;
		struct lsa_OpenPolicy2 o;
		o.in.system_name = talloc_asprintf(
			torture, "\\\\%s", dcerpc_server_name(p));
		ZERO_STRUCT(attr);
		o.in.attr = &attr;
		o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
		o.out.handle = &lsa_handle;

		torture_assert_ntstatus_ok(torture,
			dcerpc_lsa_OpenPolicy2_r(b, torture, &o),
			"dcerpc_lsa_OpenPolicy2 failed");
		torture_assert_ntstatus_ok(torture, o.out.result,
			"dcerpc_lsa_OpenPolicy2 failed");
	}

	{
		int i;
		int levels[] = { 2,3,5,6 };

		for (i=0; i<ARRAY_SIZE(levels); i++) {
			struct lsa_QueryInfoPolicy r;
			union lsa_PolicyInformation *info = NULL;
			r.in.handle = &lsa_handle;
			r.in.level = levels[i];
			r.out.info = &info;

			torture_assert_ntstatus_ok(torture,
				dcerpc_lsa_QueryInfoPolicy_r(b, torture, &r),
				talloc_asprintf(torture, "dcerpc_lsa_QueryInfoPolicy level %d failed", levels[i]));
			torture_assert_ntstatus_ok(torture, r.out.result,
				talloc_asprintf(torture, "dcerpc_lsa_QueryInfoPolicy level %d failed", levels[i]));
		}
	}

	return true;
}

static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
			       char **name)
{
	struct rap_WserverGetInfo r;
	NTSTATUS status;
	char servername[17];
	size_t converted_size;

	r.in.level = 0;
	r.in.bufsize = 0xffff;

	status = smbcli_rap_netservergetinfo(tree, mem_ctx, &r);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	memcpy(servername, r.out.info.info0.name, 16);
	servername[16] = '\0';

	if (!pull_ascii_talloc(mem_ctx, name, servername, &converted_size)) {
		return NT_STATUS_NO_MEMORY;
	}

	return NT_STATUS_OK;
}

static bool rap_get_servername(struct torture_context *tctx,
			       char **servername)
{
	struct smbcli_state *cli;

	torture_assert(tctx,
		torture_open_connection_share(tctx, &cli, tctx, torture_setting_string(tctx, "host", NULL),
					      "IPC$", tctx->ev),
		"IPC$ connection failed");

	torture_assert_ntstatus_ok(tctx,
		get_servername(tctx, cli->tree, servername),
		"get_servername failed");

	talloc_free(cli);

	return true;
}

static bool find_printers(struct torture_context *tctx,
			  struct dcerpc_pipe *p,
			  const char ***printers,
			  int *num_printers)
{
	struct srvsvc_NetShareEnum r;
	struct srvsvc_NetShareInfoCtr info_ctr;
	struct srvsvc_NetShareCtr1 c1_in;
	struct srvsvc_NetShareCtr1 *c1;
	uint32_t totalentries = 0;
	int i;
	struct dcerpc_binding_handle *b = p->binding_handle;

	ZERO_STRUCT(c1_in);
	info_ctr.level = 1;
	info_ctr.ctr.ctr1 = &c1_in;

	r.in.server_unc = talloc_asprintf(
		tctx, "\\\\%s", dcerpc_server_name(p));
	r.in.info_ctr = &info_ctr;
	r.in.max_buffer = (uint32_t)-1;
	r.in.resume_handle = NULL;
	r.out.totalentries = &totalentries;
	r.out.info_ctr = &info_ctr;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_srvsvc_NetShareEnum_r(b, tctx, &r),
		"NetShareEnum level 1 failed");
	torture_assert_werr_ok(tctx, r.out.result,
		"NetShareEnum level 1 failed");

	*printers = NULL;
	*num_printers = 0;
	c1 = r.out.info_ctr->ctr.ctr1;
	for (i=0; i<c1->count; i++) {
		if (c1->array[i].type != STYPE_PRINTQ) {
			continue;
		}
		if (!add_string_to_array(tctx, c1->array[i].name,
					 printers, num_printers)) {
			return false;
		}
	}

	return true;
}

static bool enumprinters(struct torture_context *tctx,
			 struct dcerpc_binding_handle *b,
			 const char *servername, int level, int *num_printers)
{
	struct spoolss_EnumPrinters r;
	DATA_BLOB blob;
	uint32_t needed;
	uint32_t count;
	union spoolss_PrinterInfo *info;

	r.in.flags = PRINTER_ENUM_LOCAL;
	r.in.server = talloc_asprintf(tctx, "\\\\%s", servername);
	r.in.level = level;
	r.in.buffer = NULL;
	r.in.offered = 0;
	r.out.needed = &needed;
	r.out.count = &count;
	r.out.info = &info;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_spoolss_EnumPrinters_r(b, tctx, &r),
		"dcerpc_spoolss_EnumPrinters failed");
	torture_assert_werr_equal(tctx, r.out.result, WERR_INSUFFICIENT_BUFFER,
		"EnumPrinters unexpected return code should be WERR_INSUFFICIENT_BUFFER");

	blob = data_blob_talloc_zero(tctx, needed);
	if (blob.data == NULL) {
		return false;
	}

	r.in.buffer = &blob;
	r.in.offered = needed;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_spoolss_EnumPrinters_r(b, tctx, &r),
		"dcerpc_spoolss_EnumPrinters failed");
	torture_assert_werr_ok(tctx, r.out.result,
		"dcerpc_spoolss_EnumPrinters failed");

	*num_printers = count;

	return true;
}

static bool getprinterinfo(struct torture_context *tctx,
			   struct dcerpc_binding_handle *b,
			   struct policy_handle *handle, int level,
			   union spoolss_PrinterInfo **res)
{
	struct spoolss_GetPrinter r;
	DATA_BLOB blob;
	uint32_t needed;

	r.in.handle = handle;
	r.in.level = level;
	r.in.buffer = NULL;
	r.in.offered = 0;
	r.out.needed = &needed;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_spoolss_GetPrinter_r(b, tctx, &r),
		"dcerpc_spoolss_GetPrinter failed");
	torture_assert_werr_equal(tctx, r.out.result, WERR_INSUFFICIENT_BUFFER,
		"GetPrinter unexpected return code should be WERR_INSUFFICIENT_BUFFER");

	r.in.handle = handle;
	r.in.level = level;
	blob = data_blob_talloc_zero(tctx, needed);
	if (blob.data == NULL) {
		return false;
	}
	r.in.buffer = &blob;
	r.in.offered = needed;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_spoolss_GetPrinter_r(b, tctx, &r),
		"dcerpc_spoolss_GetPrinter failed");
	torture_assert_werr_ok(tctx, r.out.result,
		"dcerpc_spoolss_GetPrinter failed");

	if (res != NULL) {
		*res = talloc_steal(tctx, r.out.info);
	}

	return true;
}

static bool torture_samba3_rpc_spoolss(struct torture_context *torture)
{
	struct dcerpc_pipe *p, *p2;
	struct dcerpc_binding_handle *b;
	struct policy_handle server_handle, printer_handle;
	const char **printers;
	int num_printers;
	struct spoolss_UserLevel1 userlevel1;
	char *servername;

	torture_assert(torture,
		rap_get_servername(torture, &servername),
		"failed to rap servername");

	torture_assert_ntstatus_ok(torture,
		torture_rpc_connection(torture, &p2, &ndr_table_srvsvc),
		"failed to setup srvsvc");

	torture_assert(torture,
		find_printers(torture, p2, &printers, &num_printers),
		"failed to find printers via srvsvc");

	talloc_free(p2);

	if (num_printers == 0) {
		torture_skip(torture, "Did not find printers\n");
		return true;
	}

	torture_assert_ntstatus_ok(torture,
		torture_rpc_connection(torture, &p, &ndr_table_spoolss),
		"failed to setup spoolss");

	b = p->binding_handle;

	ZERO_STRUCT(userlevel1);
	userlevel1.client = talloc_asprintf(
		torture, "\\\\%s", lpcfg_netbios_name(torture->lp_ctx));
	userlevel1.user = cli_credentials_get_username(cmdline_credentials);
	userlevel1.build = 2600;
	userlevel1.major = 3;
	userlevel1.minor = 0;
	userlevel1.processor = 0;

	{
		struct spoolss_OpenPrinterEx r;

		ZERO_STRUCT(r);
		r.in.printername = talloc_asprintf(torture, "\\\\%s",
						   servername);
		r.in.datatype = NULL;
		r.in.access_mask = 0;
		r.in.level = 1;
		r.in.userlevel.level1 = &userlevel1;
		r.out.handle = &server_handle;

		torture_assert_ntstatus_ok(torture,
			dcerpc_spoolss_OpenPrinterEx_r(b, torture, &r),
			"dcerpc_spoolss_OpenPrinterEx failed");
		torture_assert_werr_ok(torture, r.out.result,
			"dcerpc_spoolss_OpenPrinterEx failed");
	}

	{
		struct spoolss_ClosePrinter r;

		r.in.handle = &server_handle;
		r.out.handle = &server_handle;

		torture_assert_ntstatus_ok(torture,
			dcerpc_spoolss_ClosePrinter_r(b, torture, &r),
			"dcerpc_spoolss_ClosePrinter failed");
		torture_assert_werr_ok(torture, r.out.result,
			"dcerpc_spoolss_ClosePrinter failed");
	}

	{
		struct spoolss_OpenPrinterEx r;

		ZERO_STRUCT(r);
		r.in.printername = talloc_asprintf(
			torture, "\\\\%s\\%s", servername, printers[0]);
		r.in.datatype = NULL;
		r.in.access_mask = 0;
		r.in.level = 1;
		r.in.userlevel.level1 = &userlevel1;
		r.out.handle = &printer_handle;

		torture_assert_ntstatus_ok(torture,
			dcerpc_spoolss_OpenPrinterEx_r(b, torture, &r),
			"dcerpc_spoolss_OpenPrinterEx failed");
		torture_assert_werr_ok(torture, r.out.result,
			"dcerpc_spoolss_OpenPrinterEx failed");
	}

	{
		int i;

		for (i=0; i<8; i++) {
			torture_assert(torture,
				getprinterinfo(torture, b, &printer_handle, i, NULL),
				talloc_asprintf(torture, "getprinterinfo %d failed", i));
		}
	}

	{
		struct spoolss_ClosePrinter r;

		r.in.handle = &printer_handle;
		r.out.handle = &printer_handle;

		torture_assert_ntstatus_ok(torture,
			dcerpc_spoolss_ClosePrinter_r(b, torture, &r),
			"dcerpc_spoolss_ClosePrinter failed");
		torture_assert_werr_ok(torture, r.out.result,
			"dcerpc_spoolss_ClosePrinter failed");
	}

	{
		int num_enumerated;

		torture_assert(torture,
			enumprinters(torture, b, servername, 1, &num_enumerated),
			"enumprinters failed");

		torture_assert_int_equal(torture, num_printers, num_enumerated,
			"netshareenum / enumprinters lvl 1 numprinter mismatch");
	}

	{
		int num_enumerated;

		torture_assert(torture,
			enumprinters(torture, b, servername, 2, &num_enumerated),
			"enumprinters failed");

		torture_assert_int_equal(torture, num_printers, num_enumerated,
			"netshareenum / enumprinters lvl 2 numprinter mismatch");
	}

	return true;
}

static bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
{
	struct dcerpc_pipe *p;
	struct dcerpc_binding_handle *b;
	char *servername;

	torture_assert(torture,
		rap_get_servername(torture, &servername),
		"failed to rap servername");

	torture_assert_ntstatus_ok(torture,
		torture_rpc_connection(torture, &p, &ndr_table_wkssvc),
		"failed to setup wkssvc");

	b = p->binding_handle;

	{
		struct wkssvc_NetWkstaInfo100 wks100;
		union wkssvc_NetWkstaInfo info;
		struct wkssvc_NetWkstaGetInfo r;

		r.in.server_name = "\\foo";
		r.in.level = 100;
		info.info100 = &wks100;
		r.out.info = &info;

		torture_assert_ntstatus_ok(torture,
			dcerpc_wkssvc_NetWkstaGetInfo_r(b, torture, &r),
			"dcerpc_wkssvc_NetWksGetInfo failed");
		torture_assert_werr_ok(torture, r.out.result,
			"dcerpc_wkssvc_NetWksGetInfo failed");

		torture_assert_str_equal(torture, servername, r.out.info->info100->server_name,
			"servername RAP / DCERPC inconsistency");
	}

	return true;
}

static bool winreg_close(struct torture_context *tctx,
			 struct dcerpc_binding_handle *b,
			 struct policy_handle *handle)
{
	struct winreg_CloseKey c;

	c.in.handle = c.out.handle = handle;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_winreg_CloseKey_r(b, tctx, &c),
		"winreg_CloseKey failed");
	torture_assert_werr_ok(tctx, c.out.result,
		"winreg_CloseKey failed");

	return true;
}

static bool enumvalues(struct torture_context *tctx,
		       struct dcerpc_binding_handle *b,
		       struct policy_handle *handle)
{
	uint32_t enum_index = 0;

	while (1) {
		struct winreg_EnumValue r;
		struct winreg_ValNameBuf name;
		enum winreg_Type type = 0;
		uint8_t buf8[1024];
		NTSTATUS status;
		uint32_t size, length;

		r.in.handle = handle;
		r.in.enum_index = enum_index;
		name.name = "";
		name.size = 1024;
		r.in.name = r.out.name = &name;
		size = 1024;
		length = 5;
		r.in.type = &type;
		r.in.value = buf8;
		r.in.size = &size;
		r.in.length = &length;

		status = dcerpc_winreg_EnumValue_r(b, tctx, &r);
		if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
			return true;
		}
		enum_index += 1;
	}
}

static bool enumkeys(struct torture_context *tctx,
		     struct dcerpc_binding_handle *b,
		     struct policy_handle *handle,
		     int depth)
{
	struct winreg_EnumKey r;
	struct winreg_StringBuf kclass, name;
	NTSTATUS status;
	NTTIME t = 0;

	if (depth <= 0) {
		return true;
	}

	kclass.name   = "";
	kclass.size   = 1024;

	r.in.handle = handle;
	r.in.enum_index = 0;
	r.in.name = &name;
	r.in.keyclass = &kclass;
	r.out.name = &name;
	r.in.last_changed_time = &t;

	do {
		struct winreg_OpenKey o;
		struct policy_handle key_handle;
		int i;

		name.name = NULL;
		name.size = 1024;

		status = dcerpc_winreg_EnumKey_r(b, tctx, &r);
		if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
			/* We're done enumerating */
			return true;
		}

		for (i=0; i<10-depth; i++) {
			torture_comment(tctx, " ");
		}
		torture_comment(tctx, "%s\n", r.out.name->name);

		o.in.parent_handle = handle;
		o.in.keyname.name = r.out.name->name;
		o.in.options = 0;
		o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
		o.out.handle = &key_handle;

		status = dcerpc_winreg_OpenKey_r(b, tctx, &o);
		if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
			enumkeys(tctx, b, &key_handle, depth-1);
			enumvalues(tctx, b, &key_handle);
			torture_assert(tctx, winreg_close(tctx, b, &key_handle), "");
		}

		r.in.enum_index += 1;
	} while(true);

	return true;
}

typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_binding_handle *, TALLOC_CTX *, void *);

static bool test_Open3(struct torture_context *tctx,
		       struct dcerpc_binding_handle *b,
		       const char *name, winreg_open_fn open_fn)
{
	struct policy_handle handle;
	struct winreg_OpenHKLM r;

	r.in.system_name = 0;
	r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	r.out.handle = &handle;

	torture_assert_ntstatus_ok(tctx,
		open_fn(b, tctx, &r),
		talloc_asprintf(tctx, "%s failed", name));
	torture_assert_werr_ok(tctx, r.out.result,
		talloc_asprintf(tctx, "%s failed", name));

	enumkeys(tctx, b, &handle, 4);

	torture_assert(tctx,
		winreg_close(tctx, b, &handle),
		"dcerpc_CloseKey failed");

	return true;
}

static bool torture_samba3_rpc_winreg(struct torture_context *torture)
{
	struct dcerpc_pipe *p;
	struct dcerpc_binding_handle *b;
	bool ret = true;
	struct {
		const char *name;
		winreg_open_fn fn;
	} open_fns[] = {
		{"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM_r },
		{"OpenHKU",  (winreg_open_fn)dcerpc_winreg_OpenHKU_r },
		{"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD_r },
		{"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT_r },
		{"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR_r }};
#if 0
	int i;
#endif

	torture_assert_ntstatus_ok(torture,
		torture_rpc_connection(torture, &p, &ndr_table_winreg),
		"failed to setup winreg");

	b = p->binding_handle;

#if 1
	ret = test_Open3(torture, b, open_fns[0].name, open_fns[0].fn);
#else
	for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
		if (!test_Open3(torture, b, open_fns[i].name, open_fns[i].fn))
			ret = false;
	}
#endif
	return ret;
}

static bool get_shareinfo(struct torture_context *tctx,
			  struct dcerpc_binding_handle *b,
			  const char *servername,
			  const char *share,
			  struct srvsvc_NetShareInfo502 **info502)
{
	struct srvsvc_NetShareGetInfo r;
	union srvsvc_NetShareInfo info;

	r.in.server_unc = talloc_asprintf(tctx, "\\\\%s", servername);
	r.in.share_name = share;
	r.in.level = 502;
	r.out.info = &info;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_srvsvc_NetShareGetInfo_r(b, tctx, &r),
		"srvsvc_NetShareGetInfo failed");
	torture_assert_werr_ok(tctx, r.out.result,
		"srvsvc_NetShareGetInfo failed");

	*info502 = talloc_move(tctx, &info.info502);

	return true;
}

/*
 * Get us a handle on HKLM\
 */

static bool get_hklm_handle(struct torture_context *tctx,
			    struct dcerpc_binding_handle *b,
			    struct policy_handle *handle)
{
	struct winreg_OpenHKLM r;
	struct policy_handle result;

	r.in.system_name = 0;
        r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
        r.out.handle = &result;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_winreg_OpenHKLM_r(b, tctx, &r),
		"OpenHKLM failed");
	torture_assert_werr_ok(tctx, r.out.result,
		"OpenHKLM failed");

	*handle = result;

	return true;
}

static bool torture_samba3_createshare(struct torture_context *tctx,
				       struct dcerpc_binding_handle *b,
				       const char *sharename)
{
	struct policy_handle hklm;
	struct policy_handle new_handle;
	struct winreg_CreateKey c;
	struct winreg_CloseKey cl;
	enum winreg_CreateAction action_taken;

	c.in.handle = &hklm;
	c.in.name.name = talloc_asprintf(
		tctx, "software\\samba\\smbconf\\%s", sharename);
	torture_assert(tctx, c.in.name.name, "talloc_asprintf failed");

	c.in.keyclass.name = "";
	c.in.options = 0;
	c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	c.in.secdesc = NULL;
	c.in.action_taken = &action_taken;
	c.out.new_handle = &new_handle;
	c.out.action_taken = &action_taken;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_winreg_CreateKey_r(b, tctx, &c),
		"OpenKey failed");
	torture_assert_werr_ok(tctx, c.out.result,
		"OpenKey failed");

	cl.in.handle = &new_handle;
	cl.out.handle = &new_handle;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_winreg_CloseKey_r(b, tctx, &cl),
		"CloseKey failed");
	torture_assert_werr_ok(tctx, cl.out.result,
		"CloseKey failed");

	return true;
}

static bool torture_samba3_deleteshare(struct torture_context *tctx,
				       struct dcerpc_binding_handle *b,
				       const char *sharename)
{
	struct policy_handle hklm;
	struct winreg_DeleteKey d;

	torture_assert(tctx,
		get_hklm_handle(tctx, b, &hklm),
		"get_hklm_handle failed");

	d.in.handle = &hklm;
	d.in.key.name = talloc_asprintf(
		tctx, "software\\samba\\smbconf\\%s", sharename);
	torture_assert(tctx, d.in.key.name, "talloc_asprintf failed");

	torture_assert_ntstatus_ok(tctx,
		dcerpc_winreg_DeleteKey_r(b, tctx, &d),
		"DeleteKey failed");
	torture_assert_werr_ok(tctx, d.out.result,
		"DeleteKey failed");

	return true;
}

static bool torture_samba3_setconfig(struct torture_context *tctx,
				     struct dcerpc_binding_handle *b,
				     const char *sharename,
				     const char *parameter,
				     const char *value)
{
	struct policy_handle hklm, key_handle;
	struct winreg_OpenKey o;
	struct winreg_SetValue s;
	uint32_t type;
	DATA_BLOB val;

	torture_assert(tctx,
		get_hklm_handle(tctx, b, &hklm),
		"get_hklm_handle failed");

	o.in.parent_handle = &hklm;
	o.in.keyname.name = talloc_asprintf(
		tctx, "software\\samba\\smbconf\\%s", sharename);
	torture_assert(tctx, o.in.keyname.name, "talloc_asprintf failed");

	o.in.options = 0;
	o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	o.out.handle = &key_handle;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_winreg_OpenKey_r(b, tctx, &o),
		"OpenKey failed");
	torture_assert_werr_ok(tctx, o.out.result,
		"OpenKey failed");

	torture_assert(tctx,
		reg_string_to_val(tctx, "REG_SZ", value, &type, &val),
		"reg_string_to_val failed");

	s.in.handle = &key_handle;
	s.in.name.name = parameter;
	s.in.type = type;
	s.in.data = val.data;
	s.in.size = val.length;

	torture_assert_ntstatus_ok(tctx,
		dcerpc_winreg_SetValue_r(b, tctx, &s),
		"SetValue failed");
	torture_assert_werr_ok(tctx, s.out.result,
		"SetValue failed");

	return true;
}

static bool torture_samba3_regconfig(struct torture_context *torture)
{
	struct srvsvc_NetShareInfo502 *i = NULL;
	const char *comment = "Dummer Kommentar";
	struct dcerpc_pipe *srvsvc_pipe, *winreg_pipe;

	torture_assert_ntstatus_ok(torture,
		torture_rpc_connection(torture, &srvsvc_pipe, &ndr_table_srvsvc),
		"failed to setup srvsvc");

	torture_assert_ntstatus_ok(torture,
		torture_rpc_connection(torture, &winreg_pipe, &ndr_table_winreg),
		"failed to setup winreg");

	torture_assert(torture,
		torture_samba3_createshare(torture, winreg_pipe->binding_handle, "blubber"),
		"torture_samba3_createshare failed");

	torture_assert(torture,
		torture_samba3_setconfig(torture, winreg_pipe->binding_handle, "blubber", "comment", comment),
		"torture_samba3_setconfig failed");

	torture_assert(torture,
		get_shareinfo(torture, srvsvc_pipe->binding_handle, dcerpc_server_name(srvsvc_pipe), "blubber", &i),
		"get_shareinfo failed");

	torture_assert_str_equal(torture, comment, i->comment,
		"got unexpected comment");

	torture_assert(torture,
		torture_samba3_deleteshare(torture, winreg_pipe->binding_handle, "blubber"),
		"torture_samba3_deleteshare failed");

	return true;
}

/*
 * Test that even with a result of 0 rids the array is returned as a
 * non-NULL pointer. Yes, XP does notice.
 */

bool torture_samba3_getaliasmembership_0(struct torture_context *torture)
{
	struct dcerpc_pipe *p;
	struct dcerpc_binding_handle *b;
	struct samr_Connect2 c;
	struct samr_OpenDomain o;
	struct dom_sid sid;
	struct lsa_SidPtr ptr;
	struct lsa_SidArray sids;
	struct samr_GetAliasMembership g;
	struct samr_Ids rids;
	struct policy_handle samr, domain;

	torture_assert_ntstatus_ok(torture,
		torture_rpc_connection(torture, &p, &ndr_table_samr),
		"failed to setup samr");

	b = p->binding_handle;

	c.in.system_name = NULL;
	c.in.access_mask = SAMR_ACCESS_LOOKUP_DOMAIN;
	c.out.connect_handle = &samr;
	torture_assert_ntstatus_ok(torture,
		dcerpc_samr_Connect2_r(b, torture, &c),
		"");
	torture_assert_ntstatus_ok(torture, c.out.result,
		"");
	dom_sid_parse("S-1-5-32", &sid);
	o.in.connect_handle = &samr;
	o.in.access_mask = SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS;
	o.in.sid = &sid;
	o.out.domain_handle = &domain;
	torture_assert_ntstatus_ok(torture,
		dcerpc_samr_OpenDomain_r(b, torture, &o),
		"");
	torture_assert_ntstatus_ok(torture, o.out.result,
		"");
	dom_sid_parse("S-1-2-3-4-5", &sid);
	ptr.sid = &sid;
	sids.num_sids = 1;
	sids.sids = &ptr;
	g.in.domain_handle = &domain;
	g.in.sids = &sids;
	g.out.rids = &rids;
	torture_assert_ntstatus_ok(torture,
		dcerpc_samr_GetAliasMembership_r(b, torture, &g),
		"");
	torture_assert_ntstatus_ok(torture, g.out.result,
		"");
	if (rids.ids == NULL) {
		/* This is the piece to test here */
		torture_fail(torture,
			"torture_samba3_getaliasmembership_0: "
			"Server returns NULL rids array\n");
	}

	return true;
}

/**
 * Test smb reauthentication while rpc pipe is in use.
 */
static bool torture_rpc_smb_reauth1(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct smbcli_state *cli;
	struct smbcli_options options;
	struct smbcli_session_options session_options;

	struct dcerpc_pipe *lsa_pipe;
	struct dcerpc_binding_handle *lsa_handle;
	struct lsa_GetUserName r;
	struct lsa_String *authority_name_p = NULL;
	char *authority_name_saved = NULL;
	struct lsa_String *account_name_p = NULL;
	char *account_name_saved = NULL;
	struct cli_credentials *anon_creds = NULL;
	struct smb_composite_sesssetup io;

	mem_ctx = talloc_init("torture_samba3_reauth");
	torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");

	lpcfg_smbcli_options(torture->lp_ctx, &options);
	lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);

	status = smbcli_full_connection(mem_ctx, &cli,
					torture_setting_string(torture, "host", NULL),
					lpcfg_smb_ports(torture->lp_ctx),
					"IPC$", NULL,
					lpcfg_socket_options(torture->lp_ctx),
					cmdline_credentials,
					lpcfg_resolve_context(torture->lp_ctx),
					torture->ev, &options, &session_options,
					lpcfg_gensec_settings(torture, torture->lp_ctx));
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smbcli_full_connection failed");

	lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
	torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
			    "dcerpc_pipe_init failed");
	lsa_handle = lsa_pipe->binding_handle;

	status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"dcerpc_pipe_open failed");

	status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"dcerpc_bind_auth_none failed");

	/* lsa getusername */

	ZERO_STRUCT(r);
	r.in.system_name = "\\";
	r.in.account_name = &account_name_p;
	r.in.authority_name = &authority_name_p;
	r.out.account_name = &account_name_p;

	status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);

	authority_name_p = *r.out.authority_name;

	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"GetUserName failed");
	torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
					"GetUserName failed");

	torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
			authority_name_p->string,
			account_name_p->string);

	account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
	torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
			    "talloc failed");
	authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
	torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
			    "talloc failed");

	/* smb re-authenticate as anonymous */

	anon_creds = cli_credentials_init_anon(mem_ctx);

	ZERO_STRUCT(io);
	io.in.sesskey         = cli->transport->negotiate.sesskey;
	io.in.capabilities    = cli->transport->negotiate.capabilities;
	io.in.credentials     = anon_creds;
	io.in.workgroup       = lpcfg_workgroup(torture->lp_ctx);
	io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);

	status = smb_composite_sesssetup(cli->session, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"session reauth to anon failed");

	/* re-do lsa getusername after reauth */

	TALLOC_FREE(authority_name_p);
	TALLOC_FREE(account_name_p);
	ZERO_STRUCT(r);
	r.in.system_name = "\\";
	r.in.account_name = &account_name_p;
	r.in.authority_name = &authority_name_p;
	r.out.account_name = &account_name_p;

	status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);

	authority_name_p = *r.out.authority_name;

	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"GetUserName failed");
	torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
					"GetUserName failed");

	torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
			    ret, done, "authority_name not equal after reauth to anon");
	torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
			    ret, done, "account_name not equal after reauth to anon");

	/* smb re-auth again to the original user */

	ZERO_STRUCT(io);
	io.in.sesskey         = cli->transport->negotiate.sesskey;
	io.in.capabilities    = cli->transport->negotiate.capabilities;
	io.in.credentials     = cmdline_credentials;
	io.in.workgroup       = lpcfg_workgroup(torture->lp_ctx);
	io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);

	status = smb_composite_sesssetup(cli->session, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"session reauth to anon failed");

	/* re-do lsa getusername */

	TALLOC_FREE(authority_name_p);
	TALLOC_FREE(account_name_p);
	ZERO_STRUCT(r);
	r.in.system_name = "\\";
	r.in.account_name = &account_name_p;
	r.in.authority_name = &authority_name_p;
	r.out.account_name = &account_name_p;

	status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);

	authority_name_p = *r.out.authority_name;

	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"GetUserName failed");
	torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
					"GetUserName failed");

	torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
			    ret, done, "authority_name not equal after reauth to anon");
	torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
			    ret, done, "account_name not equal after reauth to anon");

	ret = true;

done:
	talloc_free(mem_ctx);
	return ret;
}

/**
 * Test smb reauthentication while rpc pipe is in use.
 * Open a second lsa bind after reauth to anon.
 * Do lsa getusername on that second bind.
 */
static bool torture_rpc_smb_reauth2(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct smbcli_state *cli;
	struct smbcli_options options;
	struct smbcli_session_options session_options;

	struct dcerpc_pipe *lsa_pipe;
	struct dcerpc_binding_handle *lsa_handle;
	struct lsa_GetUserName r;
	struct lsa_String *authority_name_p = NULL;
	char *authority_name_saved = NULL;
	struct lsa_String *account_name_p = NULL;
	char *account_name_saved = NULL;
	struct cli_credentials *anon_creds = NULL;
	struct smb_composite_sesssetup io;

	mem_ctx = talloc_init("torture_samba3_reauth");
	torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");

	lpcfg_smbcli_options(torture->lp_ctx, &options);
	lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);

	status = smbcli_full_connection(mem_ctx, &cli,
					torture_setting_string(torture, "host", NULL),
					lpcfg_smb_ports(torture->lp_ctx),
					"IPC$", NULL,
					lpcfg_socket_options(torture->lp_ctx),
					cmdline_credentials,
					lpcfg_resolve_context(torture->lp_ctx),
					torture->ev, &options, &session_options,
					lpcfg_gensec_settings(torture, torture->lp_ctx));
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smbcli_full_connection failed");

	/* smb re-authenticate as anonymous */

	anon_creds = cli_credentials_init_anon(mem_ctx);

	ZERO_STRUCT(io);
	io.in.sesskey         = cli->transport->negotiate.sesskey;
	io.in.capabilities    = cli->transport->negotiate.capabilities;
	io.in.credentials     = anon_creds;
	io.in.workgroup       = lpcfg_workgroup(torture->lp_ctx);
	io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);

	status = smb_composite_sesssetup(cli->session, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"session reauth to anon failed");

	/* open the lsa pipe */

	lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
	torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
			    "dcerpc_pipe_init failed");
	lsa_handle = lsa_pipe->binding_handle;

	status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"dcerpc_pipe_open failed");

	status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"dcerpc_bind_auth_none failed");

	/* lsa getusername */

	ZERO_STRUCT(r);
	r.in.system_name = "\\";
	r.in.account_name = &account_name_p;
	r.in.authority_name = &authority_name_p;
	r.out.account_name = &account_name_p;

	status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);

	authority_name_p = *r.out.authority_name;

	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"GetUserName failed");
	torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
					"GetUserName failed");

	torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
			authority_name_p->string,
			account_name_p->string);

	account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
	torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
			    "talloc failed");
	authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
	torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
			    "talloc failed");

	/* smb re-auth again to the original user */

	ZERO_STRUCT(io);
	io.in.sesskey         = cli->transport->negotiate.sesskey;
	io.in.capabilities    = cli->transport->negotiate.capabilities;
	io.in.credentials     = cmdline_credentials;
	io.in.workgroup       = lpcfg_workgroup(torture->lp_ctx);
	io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);

	status = smb_composite_sesssetup(cli->session, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"session reauth to anon failed");

	/* re-do lsa getusername after reauth */

	TALLOC_FREE(authority_name_p);
	TALLOC_FREE(account_name_p);
	ZERO_STRUCT(r);
	r.in.system_name = "\\";
	r.in.account_name = &account_name_p;
	r.in.authority_name = &authority_name_p;
	r.out.account_name = &account_name_p;

	status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);

	authority_name_p = *r.out.authority_name;

	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"GetUserName failed");
	torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
					"GetUserName failed");

	torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
			    ret, done, "authority_name not equal after reauth to anon");
	torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
			    ret, done, "account_name not equal after reauth to anon");

	ret = true;

done:
	talloc_free(mem_ctx);
	return ret;
}

/**
 * Test smb2 reauthentication while rpc pipe is in use.
 */
static bool torture_rpc_smb2_reauth1(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct smbcli_options options;

	struct dcerpc_pipe *lsa_pipe;
	struct dcerpc_binding_handle *lsa_handle;
	struct lsa_GetUserName r;
	struct lsa_String *authority_name_p = NULL;
	char *authority_name_saved = NULL;
	struct lsa_String *account_name_p = NULL;
	char *account_name_saved = NULL;
	struct cli_credentials *anon_creds = NULL;
	const char *host = torture_setting_string(torture, "host", NULL);
	struct smb2_tree *tree;

	mem_ctx = talloc_init("torture_samba3_reauth");
	torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");

	lpcfg_smbcli_options(torture->lp_ctx, &options);

	status = smb2_connect(mem_ctx,
			      host,
			      lpcfg_smb_ports(torture->lp_ctx),
			      "IPC$",
			      lpcfg_resolve_context(torture->lp_ctx),
			      cmdline_credentials,
			      &tree,
			      torture->ev,
			      &options,
			      lpcfg_socket_options(torture->lp_ctx),
			      lpcfg_gensec_settings(torture, torture->lp_ctx)
			      );
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_connect failed");

	lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
	torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
			    "dcerpc_pipe_init failed");
	lsa_handle = lsa_pipe->binding_handle;

	status = dcerpc_pipe_open_smb2(lsa_pipe, tree, "lsarpc");
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"dcerpc_pipe_open_smb2 failed");

	status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"dcerpc_bind_auth_none failed");

	/* lsa getusername */

	ZERO_STRUCT(r);
	r.in.system_name = "\\";
	r.in.account_name = &account_name_p;
	r.in.authority_name = &authority_name_p;
	r.out.account_name = &account_name_p;

	status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);

	authority_name_p = *r.out.authority_name;

	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"GetUserName failed");
	torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
					"GetUserName failed");

	torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
			authority_name_p->string,
			account_name_p->string);

	account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
	torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
			    "talloc failed");
	authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
	torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
			    "talloc failed");

	/* smb re-authenticate as anonymous */

	anon_creds = cli_credentials_init_anon(mem_ctx);

	status = smb2_session_setup_spnego(tree->session,
					   anon_creds,
					   0 /* previous_session_id */);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"session reauth to anon failed");

	/* re-do lsa getusername after reauth */

	TALLOC_FREE(authority_name_p);
	TALLOC_FREE(account_name_p);
	ZERO_STRUCT(r);
	r.in.system_name = "\\";
	r.in.account_name = &account_name_p;
	r.in.authority_name = &authority_name_p;
	r.out.account_name = &account_name_p;

	status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);

	authority_name_p = *r.out.authority_name;

	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"GetUserName failed");
	torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
					"GetUserName failed");

	torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
			    ret, done, "authority_name not equal after reauth to anon");
	torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
			    ret, done, "account_name not equal after reauth to anon");

	/* smb re-auth again to the original user */

	status = smb2_session_setup_spnego(tree->session,
					   cmdline_credentials,
					   0 /* previous_session_id */);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"session reauth to anon failed");

	/* re-do lsa getusername */

	TALLOC_FREE(authority_name_p);
	TALLOC_FREE(account_name_p);
	ZERO_STRUCT(r);
	r.in.system_name = "\\";
	r.in.account_name = &account_name_p;
	r.in.authority_name = &authority_name_p;
	r.out.account_name = &account_name_p;

	status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);

	authority_name_p = *r.out.authority_name;

	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"GetUserName failed");
	torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
					"GetUserName failed");

	torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
			    ret, done, "authority_name not equal after reauth to anon");
	torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
			    ret, done, "account_name not equal after reauth to anon");

	ret = true;

done:
	talloc_free(mem_ctx);
	return ret;
}

/**
 * Test smb2 reauthentication while rpc pipe is in use.
 * Open a second lsa bind after reauth to anon.
 * Do lsa getusername on that second bind.
 */
static bool torture_rpc_smb2_reauth2(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct smbcli_options options;

	struct dcerpc_pipe *lsa_pipe;
	struct dcerpc_binding_handle *lsa_handle;
	struct lsa_GetUserName r;
	struct lsa_String *authority_name_p = NULL;
	char *authority_name_saved = NULL;
	struct lsa_String *account_name_p = NULL;
	char *account_name_saved = NULL;
	struct cli_credentials *anon_creds = NULL;
	const char *host = torture_setting_string(torture, "host", NULL);
	struct smb2_tree *tree;

	mem_ctx = talloc_init("torture_samba3_reauth");
	torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");

	lpcfg_smbcli_options(torture->lp_ctx, &options);

	status = smb2_connect(mem_ctx,
			      host,
			      lpcfg_smb_ports(torture->lp_ctx),
			      "IPC$",
			      lpcfg_resolve_context(torture->lp_ctx),
			      cmdline_credentials,
			      &tree,
			      torture->ev,
			      &options,
			      lpcfg_socket_options(torture->lp_ctx),
			      lpcfg_gensec_settings(torture, torture->lp_ctx)
			      );
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_connect failed");

	/* smb re-authenticate as anonymous */

	anon_creds = cli_credentials_init_anon(mem_ctx);

	status = smb2_session_setup_spnego(tree->session,
					   anon_creds,
					   0 /* previous_session_id */);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"session reauth to anon failed");

	/* open the lsa pipe */

	lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
	torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
			    "dcerpc_pipe_init failed");
	lsa_handle = lsa_pipe->binding_handle;

	status = dcerpc_pipe_open_smb2(lsa_pipe, tree, "lsarpc");
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"dcerpc_pipe_open_smb2 failed");

	status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"dcerpc_bind_auth_none failed");

	/* lsa getusername */

	ZERO_STRUCT(r);
	r.in.system_name = "\\";
	r.in.account_name = &account_name_p;
	r.in.authority_name = &authority_name_p;
	r.out.account_name = &account_name_p;

	status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);

	authority_name_p = *r.out.authority_name;

	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"GetUserName failed");
	torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
					"GetUserName failed");

	torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
			authority_name_p->string,
			account_name_p->string);

	account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
	torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
			    "talloc failed");
	authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
	torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
			    "talloc failed");

	/* smb re-auth again to the original user */

	status = smb2_session_setup_spnego(tree->session,
					   cmdline_credentials,
					   0 /* previous_session_id */);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"session reauth to anon failed");

	/* re-do lsa getusername */

	TALLOC_FREE(authority_name_p);
	TALLOC_FREE(account_name_p);
	ZERO_STRUCT(r);
	r.in.system_name = "\\";
	r.in.account_name = &account_name_p;
	r.in.authority_name = &authority_name_p;
	r.out.account_name = &account_name_p;

	status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);

	authority_name_p = *r.out.authority_name;

	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"GetUserName failed");
	torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
					"GetUserName failed");

	torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
			    ret, done, "authority_name not equal after reauth to anon");
	torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
			    ret, done, "account_name not equal after reauth to anon");

	ret = true;

done:
	talloc_free(mem_ctx);
	return ret;
}

static bool torture_rpc_smb1_pipe_name(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct smbcli_state *cli;
	struct smbcli_options options;
	struct smbcli_session_options session_options;
	union smb_open io;
	union smb_close cl;
	uint16_t fnum;

	mem_ctx = talloc_init("torture_samba3_smb1_pipe_name");
	torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");

	lpcfg_smbcli_options(torture->lp_ctx, &options);
	lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);

	status = smbcli_full_connection(mem_ctx, &cli,
					torture_setting_string(torture, "host", NULL),
					lpcfg_smb_ports(torture->lp_ctx),
					"IPC$", NULL,
					lpcfg_socket_options(torture->lp_ctx),
					cmdline_credentials,
					lpcfg_resolve_context(torture->lp_ctx),
					torture->ev, &options, &session_options,
					lpcfg_gensec_settings(torture, torture->lp_ctx));
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smbcli_full_connection failed");

	ZERO_STRUCT(io);
	io.generic.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.access_mask = DESIRED_ACCESS_PIPE;
	io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
				       NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
	io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
	io.ntcreatex.in.security_flags = 0;

	io.ntcreatex.in.fname = "__none__";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb_raw_open for '__none__'");

	io.ntcreatex.in.fname = "pipe\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb_raw_open for 'pipe\\srvsvc'");

	io.ntcreatex.in.fname = "\\pipe\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb_raw_open for '\\pipe\\srvsvc'");

	io.ntcreatex.in.fname = "srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for 'srvsvc'");
	fnum = io.ntcreatex.out.file.fnum;
	ZERO_STRUCT(cl);
	cl.generic.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	status = smb_raw_close(cli->tree, &cl);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb_raw_close failed");

	io.ntcreatex.in.fname = "\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for '\\srvsvc'");
	fnum = io.ntcreatex.out.file.fnum;
	ZERO_STRUCT(cl);
	cl.generic.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	status = smb_raw_close(cli->tree, &cl);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb_raw_close failed");

	io.ntcreatex.in.fname = "\\\\\\\\\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for '\\\\\\\\\\srvsvc'");
	fnum = io.ntcreatex.out.file.fnum;
	ZERO_STRUCT(cl);
	cl.generic.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	status = smb_raw_close(cli->tree, &cl);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb_raw_close failed");

	ZERO_STRUCT(io);
	io.generic.level = RAW_OPEN_NTTRANS_CREATE;
	io.nttrans.in.access_mask = DESIRED_ACCESS_PIPE;
	io.nttrans.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
				       NTCREATEX_SHARE_ACCESS_WRITE;
	io.nttrans.in.open_disposition = NTCREATEX_DISP_OPEN;
	io.nttrans.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
	io.nttrans.in.security_flags = 0;

	io.nttrans.in.fname = "__none__";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb_raw_open for '__none__'");

	io.nttrans.in.fname = "pipe\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb_raw_open for 'pipe\\srvsvc'");

	io.nttrans.in.fname = "\\pipe\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb_raw_open for '\\pipe\\srvsvc'");

	io.nttrans.in.fname = "srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for 'srvsvc'");
	fnum = io.nttrans.out.file.fnum;
	ZERO_STRUCT(cl);
	cl.generic.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	status = smb_raw_close(cli->tree, &cl);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb_raw_close failed");

	io.nttrans.in.fname = "\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for '\\srvsvc'");
	fnum = io.nttrans.out.file.fnum;
	ZERO_STRUCT(cl);
	cl.generic.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	status = smb_raw_close(cli->tree, &cl);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb_raw_close failed");

	io.nttrans.in.fname = "\\\\\\\\\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for '\\\\\\\\\\srvsvc'");
	fnum = io.nttrans.out.file.fnum;
	ZERO_STRUCT(cl);
	cl.generic.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	status = smb_raw_close(cli->tree, &cl);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb_raw_close failed");

	ZERO_STRUCT(io);
	io.generic.level = RAW_OPEN_OPENX;
	io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
	io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;

	io.openx.in.fname = "__none__";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD,
					   ret, done,
					   "smb_raw_open for '__none__'");

	io.openx.in.fname = "srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD,
					   ret, done,
					   "smb_raw_open for 'srvsvc'");

	io.openx.in.fname = "\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD,
					   ret, done,
					   "smb_raw_open for '\\srvsvc'");

	io.openx.in.fname = "\\pipesrvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD,
					   ret, done,
					   "smb_raw_open for '\\pipesrvsvc'");

	io.openx.in.fname = "pipe\\__none__";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb_raw_open for 'pipe\\__none__'");

	io.openx.in.fname = "\\pipe\\__none__";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb_raw_open for '\\pipe\\__none__'");

	io.openx.in.fname = "pipe\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for 'pipe\\srvsvc'");
	fnum = io.openx.out.file.fnum;
	ZERO_STRUCT(cl);
	cl.generic.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	status = smb_raw_close(cli->tree, &cl);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb_raw_close failed");

	io.openx.in.fname = "\\pipe\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for '\\pipe\\srvsvc'");
	fnum = io.openx.out.file.fnum;
	ZERO_STRUCT(cl);
	cl.generic.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	status = smb_raw_close(cli->tree, &cl);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb_raw_close failed");

	io.openx.in.fname = "\\\\\\\\\\pipe\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for '\\\\\\\\\\pipe\\srvsvc'");
	fnum = io.openx.out.file.fnum;
	ZERO_STRUCT(cl);
	cl.generic.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	status = smb_raw_close(cli->tree, &cl);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb_raw_close failed");

	io.openx.in.fname = "\\\\\\\\\\pipe\\\\\\\\\\srvsvc";
	status = smb_raw_open(cli->tree, torture, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for '\\\\\\\\\\pipe\\\\\\\\\\srvsvc'");
	fnum = io.openx.out.file.fnum;
	ZERO_STRUCT(cl);
	cl.generic.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	status = smb_raw_close(cli->tree, &cl);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb_raw_close failed");
	ret = true;

done:
	talloc_free(mem_ctx);
	return ret;
}

static bool torture_rpc_smb2_pipe_name(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct smbcli_options options;
	const char *host = torture_setting_string(torture, "host", NULL);
	struct smb2_tree *tree;
	struct smb2_handle h;
	struct smb2_create io;

	mem_ctx = talloc_init("torture_samba3_smb2_pipe_name");
	torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");

	lpcfg_smbcli_options(torture->lp_ctx, &options);

	status = smb2_connect(mem_ctx,
			      host,
			      lpcfg_smb_ports(torture->lp_ctx),
			      "IPC$",
			      lpcfg_resolve_context(torture->lp_ctx),
			      cmdline_credentials,
			      &tree,
			      torture->ev,
			      &options,
			      lpcfg_socket_options(torture->lp_ctx),
			      lpcfg_gensec_settings(torture, torture->lp_ctx)
			      );
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_connect failed");

	ZERO_STRUCT(io);
	io.in.oplock_level = 0;
	io.in.desired_access = DESIRED_ACCESS_PIPE;
	io.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
	io.in.file_attributes = 0;
	io.in.create_disposition = NTCREATEX_DISP_OPEN;
	io.in.share_access =
		NTCREATEX_SHARE_ACCESS_READ|
		NTCREATEX_SHARE_ACCESS_WRITE;
	io.in.create_options = 0;

	io.in.fname = "__none__";
	status = smb2_create(tree, tree, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb2_create for '__none__'");

	io.in.fname = "\\srvsvc";
	status = smb2_create(tree, tree, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb2_create for '\\srvsvc'");

	io.in.fname = "\\pipe\\srvsvc";
	status = smb2_create(tree, tree, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb2_create for '\\pipe\\srvsvc'");

	io.in.fname = "pipe\\srvsvc";
	status = smb2_create(tree, tree, &io);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
					   ret, done,
					   "smb2_create for 'pipe\\srvsvc'");

	io.in.fname = "srvsvc";
	status = smb2_create(tree, tree, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for 'srvsvc'");

	h = io.out.file.handle;

	status = smb2_util_close(tree, h);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_util_close failed");

	ret = true;
done:
	talloc_free(mem_ctx);
	return ret;
}

/**
 * Test behaviour of a waiting read call on a pipe when
 * the pipe handle is closed:
 * - open a pipe via smb2
 * - trigger a read which hangs since there is nothing to read
 * - close the pipe file handle
 * - wait for the read to return and check the status
 *   (STATUS_PIPE_BROKEN)
 */
static bool torture_rpc_smb2_pipe_read_close(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct smbcli_options options;
	const char *host = torture_setting_string(torture, "host", NULL);
	struct smb2_tree *tree;
	struct smb2_handle h;
	struct smb2_request *smb2req;
	struct smb2_create io;
	struct smb2_read rd;

	mem_ctx = talloc_init("torture_samba3_pipe_read_close");
	torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");

	lpcfg_smbcli_options(torture->lp_ctx, &options);

	status = smb2_connect(mem_ctx,
			      host,
			      lpcfg_smb_ports(torture->lp_ctx),
			      "IPC$",
			      lpcfg_resolve_context(torture->lp_ctx),
			      cmdline_credentials,
			      &tree,
			      torture->ev,
			      &options,
			      lpcfg_socket_options(torture->lp_ctx),
			      lpcfg_gensec_settings(torture, torture->lp_ctx)
			      );
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_connect failed");

	ZERO_STRUCT(io);
	io.in.oplock_level = 0;
	io.in.desired_access = DESIRED_ACCESS_PIPE;
	io.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
	io.in.file_attributes = 0;
	io.in.create_disposition = NTCREATEX_DISP_OPEN;
	io.in.share_access =
		NTCREATEX_SHARE_ACCESS_READ|
		NTCREATEX_SHARE_ACCESS_WRITE;
	io.in.create_options = 0;
	io.in.fname = "lsarpc";

	status = smb2_create(tree, tree, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for 'lsarpc'");

	h = io.out.file.handle;

	ZERO_STRUCT(rd);
	rd.in.file.handle = h;
	rd.in.length = 1024;
	rd.in.offset = 0;
	rd.in.min_count = 0;

	smb2req = smb2_read_send(tree, &rd);
	torture_assert_goto(torture, (smb2req != NULL), ret, done,
			    "smb2_read_send failed");

	status = smb2_util_close(tree, h);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_util_close failed");

	status = smb2_read_recv(smb2req, mem_ctx, &rd);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_PIPE_BROKEN, ret, done,
					   "smb2_read_recv: unexpected return code");

	ret = true;
done:
	talloc_free(mem_ctx);
	return ret;
}

/**
 * Test behaviour of a waiting read call on a pipe when
 * the tree is disconnected.
 * - open a pipe via smb2
 * - trigger a read which hangs since there is nothing to read
 * - do a tree disconnect
 * - wait for the read to return and check the status
 *   (STATUS_PIPE_BROKEN)
 */
static bool torture_rpc_smb2_pipe_read_tdis(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct smbcli_options options;
	const char *host = torture_setting_string(torture, "host", NULL);
	struct smb2_tree *tree;
	struct smb2_handle h;
	struct smb2_request *smb2req;
	struct smb2_create io;
	struct smb2_read rd;

	mem_ctx = talloc_init("torture_samba3_pipe_read_tdis");
	torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");

	lpcfg_smbcli_options(torture->lp_ctx, &options);

	status = smb2_connect(mem_ctx,
			      host,
			      lpcfg_smb_ports(torture->lp_ctx),
			      "IPC$",
			      lpcfg_resolve_context(torture->lp_ctx),
			      cmdline_credentials,
			      &tree,
			      torture->ev,
			      &options,
			      lpcfg_socket_options(torture->lp_ctx),
			      lpcfg_gensec_settings(torture, torture->lp_ctx)
			      );
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_connect failed");

	ZERO_STRUCT(io);
	io.in.oplock_level = 0;
	io.in.desired_access = DESIRED_ACCESS_PIPE;
	io.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
	io.in.file_attributes = 0;
	io.in.create_disposition = NTCREATEX_DISP_OPEN;
	io.in.share_access =
		NTCREATEX_SHARE_ACCESS_READ|
		NTCREATEX_SHARE_ACCESS_WRITE;
	io.in.create_options = 0;
	io.in.fname = "lsarpc";

	status = smb2_create(tree, tree, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for 'lsarpc'");

	h = io.out.file.handle;

	ZERO_STRUCT(rd);
	rd.in.file.handle = h;
	rd.in.length = 1024;
	rd.in.offset = 0;
	rd.in.min_count = 0;

	smb2req = smb2_read_send(tree, &rd);
	torture_assert_goto(torture, (smb2req != NULL), ret, done,
			    "smb2_read_send failed");

	status = smb2_tdis(tree);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_tdis failed");

	status = smb2_read_recv(smb2req, mem_ctx, &rd);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_PIPE_BROKEN, ret, done,
					   "smb2_read_recv: unexpected return code");

	ret = true;
done:
	talloc_free(mem_ctx);
	return ret;
}

/**
 * Test behaviour of a waiting read call on a pipe when
 * the user logs off
 * - open a pipe via smb2
 * - trigger a read which hangs since there is nothing to read
 * - do a logoff
 * - wait for the read to return and check the status
 *   (STATUS_PIPE_BROKEN)
 */
static bool torture_rpc_smb2_pipe_read_logoff(struct torture_context *torture)
{
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;
	bool ret = false;
	struct smbcli_options options;
	const char *host = torture_setting_string(torture, "host", NULL);
	struct smb2_tree *tree;
	struct smb2_handle h;
	struct smb2_request *smb2req;
	struct smb2_create io;
	struct smb2_read rd;

	mem_ctx = talloc_init("torture_samba3_pipe_read_tdis");
	torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");

	lpcfg_smbcli_options(torture->lp_ctx, &options);

	status = smb2_connect(mem_ctx,
			      host,
			      lpcfg_smb_ports(torture->lp_ctx),
			      "IPC$",
			      lpcfg_resolve_context(torture->lp_ctx),
			      cmdline_credentials,
			      &tree,
			      torture->ev,
			      &options,
			      lpcfg_socket_options(torture->lp_ctx),
			      lpcfg_gensec_settings(torture, torture->lp_ctx)
			      );
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_connect failed");

	ZERO_STRUCT(io);
	io.in.oplock_level = 0;
	io.in.desired_access = DESIRED_ACCESS_PIPE;
	io.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
	io.in.file_attributes = 0;
	io.in.create_disposition = NTCREATEX_DISP_OPEN;
	io.in.share_access =
		NTCREATEX_SHARE_ACCESS_READ|
		NTCREATEX_SHARE_ACCESS_WRITE;
	io.in.create_options = 0;
	io.in.fname = "lsarpc";

	status = smb2_create(tree, tree, &io);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_create failed for 'lsarpc'");

	h = io.out.file.handle;

	ZERO_STRUCT(rd);
	rd.in.file.handle = h;
	rd.in.length = 1024;
	rd.in.offset = 0;
	rd.in.min_count = 0;

	smb2req = smb2_read_send(tree, &rd);
	torture_assert_goto(torture, (smb2req != NULL), ret, done,
			    "smb2_read_send failed");

	status = smb2_logoff(tree->session);
	torture_assert_ntstatus_ok_goto(torture, status, ret, done,
					"smb2_logoff failed");

	status = smb2_read_recv(smb2req, mem_ctx, &rd);
	torture_assert_ntstatus_equal_goto(torture, status, NT_STATUS_PIPE_BROKEN, ret, done,
					   "smb2_read_recv: unexpected return code");

	ret = true;
done:
	talloc_free(mem_ctx);
	return ret;
}


struct torture_suite *torture_rpc_samba3(TALLOC_CTX *mem_ctx)
{
	struct torture_suite *suite = torture_suite_create(mem_ctx, "samba3");

	torture_suite_add_simple_test(suite, "bind", torture_bind_samba3);
	torture_suite_add_simple_test(suite, "netlogon", torture_netlogon_samba3);
	torture_suite_add_simple_test(suite, "sessionkey", torture_samba3_sessionkey);
	torture_suite_add_simple_test(suite, "srvsvc", torture_samba3_rpc_srvsvc);
	torture_suite_add_simple_test(suite, "sharesec", torture_samba3_rpc_sharesec);
	torture_suite_add_simple_test(suite, "getusername", torture_samba3_rpc_getusername);
	torture_suite_add_simple_test(suite, "randomauth2", torture_samba3_rpc_randomauth2);
	torture_suite_add_simple_test(suite, "lsa", torture_samba3_rpc_lsa);
	torture_suite_add_simple_test(suite, "spoolss", torture_samba3_rpc_spoolss);
	torture_suite_add_simple_test(suite, "wkssvc", torture_samba3_rpc_wkssvc);
	torture_suite_add_simple_test(suite, "winreg", torture_samba3_rpc_winreg);
	torture_suite_add_simple_test(suite, "getaliasmembership-0", torture_samba3_getaliasmembership_0);
	torture_suite_add_simple_test(suite, "regconfig", torture_samba3_regconfig);
	torture_suite_add_simple_test(suite, "smb-reauth1", torture_rpc_smb_reauth1);
	torture_suite_add_simple_test(suite, "smb-reauth2", torture_rpc_smb_reauth2);
	torture_suite_add_simple_test(suite, "smb2-reauth1", torture_rpc_smb2_reauth1);
	torture_suite_add_simple_test(suite, "smb2-reauth2", torture_rpc_smb2_reauth2);
	torture_suite_add_simple_test(suite, "smb1-pipe-name", torture_rpc_smb1_pipe_name);
	torture_suite_add_simple_test(suite, "smb2-pipe-name", torture_rpc_smb2_pipe_name);
	torture_suite_add_simple_test(suite, "smb2-pipe-read-close", torture_rpc_smb2_pipe_read_close);
	torture_suite_add_simple_test(suite, "smb2-pipe-read-tdis", torture_rpc_smb2_pipe_read_tdis);
	torture_suite_add_simple_test(suite, "smb2-pipe-read-logoff", torture_rpc_smb2_pipe_read_logoff);

	suite->description = talloc_strdup(suite, "samba3 DCERPC interface tests");

	return suite;
}