summaryrefslogtreecommitdiff
path: root/source4/torture/krb5/kdc-canon-heimdal.c
blob: 7f806e73e660ffc6f05a727ef347801208ba8b4b (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
/*
   Unix SMB/CIFS implementation.

   Validate the krb5 pac generation routines

   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2015

   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 "system/kerberos.h"
#include "torture/smbtorture.h"
#include "torture/krb5/proto.h"
#include "auth/credentials/credentials.h"
#include "lib/cmdline/popt_common.h"
#include "source4/auth/kerberos/kerberos.h"
#include "source4/auth/kerberos/kerberos_util.h"
#include "lib/util/util_net.h"
#include "auth/auth.h"
#include "auth/auth_sam_reply.h"
#include "auth/gensec/gensec.h"
#include "param/param.h"

#define TEST_CANONICALIZE     0x0000001
#define TEST_ENTERPRISE       0x0000002
#define TEST_UPPER_REALM      0x0000004
#define TEST_UPPER_USERNAME   0x0000008
#define TEST_NETBIOS_REALM    0x0000010
#define TEST_WIN2K            0x0000020
#define TEST_UPN              0x0000040
#define TEST_S4U2SELF         0x0000080
#define TEST_REMOVEDOLLAR     0x0000100
#define TEST_ALL              0x00001FF

struct test_data {
	const char *test_name;
	const char *realm;
	const char *real_realm;
	const char *real_domain;
	const char *username;
	const char *real_username;
	bool canonicalize;
	bool enterprise;
	bool upper_realm;
	bool upper_username;
	bool netbios_realm;
	bool win2k;
	bool upn;
	bool other_upn_suffix;
	bool s4u2self;
	bool removedollar;
	const char *krb5_service;
	const char *krb5_hostname;
};

enum test_stage {
	TEST_AS_REQ = 0,
	TEST_TGS_REQ_KRBTGT_CANON = 1,
	TEST_TGS_REQ_CANON = 2,
	TEST_SELF_TRUST_TGS_REQ = 3,
	TEST_TGS_REQ = 4,
	TEST_TGS_REQ_KRBTGT = 5,
	TEST_TGS_REQ_HOST = 6,
	TEST_TGS_REQ_HOST_SRV_INST = 7,
	TEST_TGS_REQ_HOST_SRV_HST = 8,
	TEST_AS_REQ_SELF = 9,
	TEST_DONE = 10
};

struct torture_krb5_context {
	struct smb_krb5_context *smb_krb5_context;
	struct torture_context *tctx;
	struct addrinfo *server;
	struct test_data *test_data;
	int packet_count;
	enum test_stage test_stage;
	AS_REQ as_req;
	AS_REP as_rep;
	TGS_REQ tgs_req;
	TGS_REP tgs_rep;
};

struct pac_data {
	const char *principal_name;
};

/*
 * A helper function which avoids touching the local databases to
 * generate the session info, as we just want to verify the principal
 * name that we found in the ticket not the full local token
 */
static NTSTATUS test_generate_session_info_pac(struct auth4_context *auth_ctx,
					       TALLOC_CTX *mem_ctx,
					       struct smb_krb5_context *smb_krb5_context,
					       DATA_BLOB *pac_blob,
					       const char *principal_name,
					       const struct tsocket_address *remote_address,
					       uint32_t session_info_flags,
					       struct auth_session_info **session_info)
{
	NTSTATUS nt_status;
	struct auth_user_info_dc *user_info_dc;
	TALLOC_CTX *tmp_ctx;
	struct pac_data *pac_data;

	tmp_ctx = talloc_named(mem_ctx, 0, "gensec_gssapi_session_info context");
	NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);

	auth_ctx->private_data = pac_data = talloc_zero(auth_ctx, struct pac_data);

	pac_data->principal_name = talloc_strdup(pac_data, principal_name);
	if (!pac_data->principal_name) {
		talloc_free(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	nt_status = kerberos_pac_blob_to_user_info_dc(tmp_ctx,
						      *pac_blob,
						      smb_krb5_context->krb5_context,
						      &user_info_dc, NULL, NULL);
	if (!NT_STATUS_IS_OK(nt_status)) {
		talloc_free(tmp_ctx);
		return nt_status;
	}

	if (user_info_dc->info->authenticated) {
		session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
	}

	session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
	nt_status = auth_generate_session_info(mem_ctx,
					       NULL,
					       NULL,
					       user_info_dc, session_info_flags,
					       session_info);
	if (!NT_STATUS_IS_OK(nt_status)) {
		talloc_free(tmp_ctx);
		return nt_status;
	}

	talloc_free(tmp_ctx);
	return NT_STATUS_OK;
}

/* Check to see if we can pass the PAC across to the NETLOGON server for validation */

/* Also happens to be a really good one-step verfication of our Kerberos stack */

static bool test_accept_ticket(struct torture_context *tctx,
			       struct cli_credentials *credentials,
			       const char *principal,
			       DATA_BLOB client_to_server)
{
	NTSTATUS status;
	struct gensec_security *gensec_server_context;
	DATA_BLOB server_to_client;
	struct auth4_context *auth_context;
	struct auth_session_info *session_info;
	struct pac_data *pac_data;
	TALLOC_CTX *tmp_ctx = talloc_new(tctx);

	torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");

	auth_context = talloc_zero(tmp_ctx, struct auth4_context);
	torture_assert(tctx, auth_context != NULL, "talloc_new() failed");

	auth_context->generate_session_info_pac = test_generate_session_info_pac;

	status = gensec_server_start(tctx,
				     lpcfg_gensec_settings(tctx, tctx->lp_ctx),
				     auth_context, &gensec_server_context);
	torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");

	status = gensec_set_credentials(gensec_server_context, credentials);
	torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");

	status = gensec_start_mech_by_name(gensec_server_context, "krb5");
	torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_name (server) failed");

	server_to_client = data_blob(NULL, 0);

	/* Do a client-server update dance */
	status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
	torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");

	/* Extract the PAC using Samba's code */

	status = gensec_session_info(gensec_server_context, gensec_server_context, &session_info);
	torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");

	pac_data = talloc_get_type(auth_context->private_data, struct pac_data);

	torture_assert(tctx, pac_data != NULL, "gensec_update failed to fill in pac_data in auth_context");
	torture_assert(tctx, pac_data->principal_name != NULL, "principal_name not present");
	torture_assert_str_equal(tctx, pac_data->principal_name, principal, "wrong principal name");
	return true;
}

/*
 * TEST_AS_REQ and TEST_AS_REQ_SELF - SEND
 *
 * Confirm that the outgoing packet meets certain expectations.  This
 * should be extended to further assert the correct and expected
 * behaviour of the krb5 libs, so we know what we are sending to the
 * server.
 *
 * Additionally, this CHANGES the request to remove the canonicalize
 * flag automatically added by the krb5 libs when an enterprise
 * principal is used, so we can test what the server does in this
 * combination.
 *
 */

static bool torture_krb5_pre_send_as_req_test(struct torture_krb5_context *test_context,
					      const krb5_data *send_buf,
					      krb5_data *modified_send_buf)
{
	AS_REQ mod_as_req;
	krb5_error_code k5ret;
	size_t used;
	torture_assert_int_equal(test_context->tctx, decode_AS_REQ(send_buf->data, send_buf->length,
					       &test_context->as_req, &used),
				 0, "decode_AS_REQ for TEST_AS_REQ failed");
	mod_as_req = test_context->as_req;
	torture_assert_int_equal(test_context->tctx, used, send_buf->length, "length mismatch");
	torture_assert_int_equal(test_context->tctx, test_context->as_req.pvno,
				 5, "Got wrong as_req->pvno");
	if (test_context->test_data->canonicalize || test_context->test_data->enterprise) {
		torture_assert(test_context->tctx,
			       test_context->as_req.req_body.kdc_options.canonicalize,
			       "krb5 libs did not set canonicalize!");
	} else {
		torture_assert_int_equal(test_context->tctx,
					 test_context->as_req.req_body.kdc_options.canonicalize,
					 false,
					 "krb5 libs unexpectedly set canonicalize!");
	}

	if (test_context->test_data->enterprise) {
		torture_assert_int_equal(test_context->tctx,
					 test_context->as_req.req_body.cname->name_type,
					 KRB5_NT_ENTERPRISE_PRINCIPAL,
					 "krb5 libs did not pass principal as enterprise!");
	} else {
		torture_assert_int_equal(test_context->tctx,
					 test_context->as_req.req_body.cname->name_type,
					 KRB5_NT_PRINCIPAL,
					 "krb5 libs unexpectedly set principal as enterprise!");
	}

	/* Force off canonicalize that was forced on by the krb5 libs */
	if (test_context->test_data->canonicalize == false && test_context->test_data->enterprise) {
		mod_as_req.req_body.kdc_options.canonicalize = false;
	}

	if (test_context->test_stage == TEST_AS_REQ_SELF) {
		/*
		 * Force the server name to match the client name,
		 * including the name type.  This isn't possible with
		 * the krb5 client libs alone
		 */
		mod_as_req.req_body.sname = test_context->as_req.req_body.cname;
	}

	ASN1_MALLOC_ENCODE(AS_REQ, modified_send_buf->data, modified_send_buf->length,
			   &mod_as_req, &used, k5ret);
	torture_assert_int_equal(test_context->tctx,
				 k5ret, 0,
				 "encode_AS_REQ failed");

	if (test_context->test_stage != TEST_AS_REQ_SELF) {
		torture_assert_int_equal(test_context->tctx, used, send_buf->length,
					 "re-encode length mismatch");
	}
	return true;
}

/*
 * TEST_AS_REQ - RECV
 *
 * Confirm that the reply packet from the KDC meets certain
 * expectations as part of TEST_AS_REQ.  This uses a packet count to
 * work out what packet we are up to in the multiple exchanged
 * triggerd by krb5_get_init_creds_password().
 *
 */

static bool torture_krb5_post_recv_as_req_test(struct torture_krb5_context *test_context,
					       const krb5_data *recv_buf)
{
	KRB_ERROR error;
	size_t used;
	if (test_context->packet_count == 0) {
		krb5_error_code k5ret;
		/*
		 * The client libs obtain the salt by attempting to
		 * authenticate without pre-authentication and getting
		 * the correct salt with the
		 * KRB5KDC_ERR_PREAUTH_REQUIRED error.  If we are in
		 * the test (netbios_realm && upn) that deliberatly
		 * has an incorrect principal, we check we get the
		 * correct error.
		 */
		k5ret = decode_KRB_ERROR(recv_buf->data, recv_buf->length,
					 &error, &used);
		if (k5ret != 0) {
			AS_REP as_rep;
			k5ret = decode_AS_REP(recv_buf->data, recv_buf->length,
				      &as_rep, &used);
			if (k5ret == 0) {
				if (test_context->test_data->netbios_realm && test_context->test_data->upn) {
					torture_assert(test_context->tctx, false,
						       "expected to get a KRB_ERROR packet with "
						       "KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN, got valid AS-REP");
				} else {
					torture_assert(test_context->tctx, false,
						       "expected to get a KRB_ERROR packet with "
						       "KRB5KDC_ERR_PREAUTH_REQUIRED, got valid AS-REP");
				}
			} else {
				if (test_context->test_data->netbios_realm && test_context->test_data->upn) {
					torture_assert(test_context->tctx, false,
						       "unable to decode as KRB-ERROR or AS-REP, "
						       "expected to get a KRB_ERROR packet with KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN");
				} else {
					torture_assert(test_context->tctx, false,
						       "unable to decode as KRB-ERROR or AS-REP, "
						       "expected to get a KRB_ERROR packet with KRB5KDC_ERR_PREAUTH_REQUIRED");
				}
			}
		}
		torture_assert_int_equal(test_context->tctx, used, recv_buf->length,
					 "length mismatch");
		torture_assert_int_equal(test_context->tctx, error.pvno, 5,
					 "Got wrong error.pvno");
		if (test_context->test_data->netbios_realm && test_context->test_data->upn) {
			torture_assert_int_equal(test_context->tctx,
						 error.error_code,
						 KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN - KRB5KDC_ERR_NONE,
						 "Got wrong error.error_code");
		} else {
			torture_assert_int_equal(test_context->tctx,
						 error.error_code,
						 KRB5KDC_ERR_PREAUTH_REQUIRED - KRB5KDC_ERR_NONE,
						 "Got wrong error.error_code");
		}

		free_KRB_ERROR(&error);
	} else if ((decode_KRB_ERROR(recv_buf->data, recv_buf->length, &error, &used) == 0)
		   && (test_context->packet_count == 1)) {
		/*
		 * The Windows 2012R2 KDC will always respond with
		 * KRB5KRB_ERR_RESPONSE_TOO_BIG over UDP as the ticket
		 * won't fit, because of the PAC.  (It appears to do
		 * this always, even if it will).  This triggers the
		 * client to try again over TCP.
		 */
		torture_assert_int_equal(test_context->tctx,
					 used, recv_buf->length,
					 "length mismatch");
		torture_assert_int_equal(test_context->tctx,
					 error.pvno, 5,
					 "Got wrong error.pvno");
		torture_assert_int_equal(test_context->tctx,
					 error.error_code,
					 KRB5KRB_ERR_RESPONSE_TOO_BIG - KRB5KDC_ERR_NONE,
					 "Got wrong error.error_code");
		free_KRB_ERROR(&error);
	} else {
		/*
		 * Finally the successful packet.
		 */
		torture_assert_int_equal(test_context->tctx,
					 decode_AS_REP(recv_buf->data, recv_buf->length,
						       &test_context->as_rep, &used), 0,
					 "decode_AS_REP failed");
		torture_assert_int_equal(test_context->tctx, used, recv_buf->length,
					 "length mismatch");
		torture_assert_int_equal(test_context->tctx,
					 test_context->as_rep.pvno, 5,
					 "Got wrong as_rep->pvno");
		torture_assert_int_equal(test_context->tctx,
					 test_context->as_rep.ticket.tkt_vno, 5,
					 "Got wrong as_rep->ticket.tkt_vno");
		torture_assert(test_context->tctx,
			       test_context->as_rep.ticket.enc_part.kvno,
			       "Did not get a KVNO in test_context->as_rep.ticket.enc_part.kvno");

		/*
		 * We can confirm that the correct proxy behaviour is
		 * in use on the KDC by checking the KVNO of the
		 * krbtgt account returned in the reply.
		 *
		 * A packet passed to the full RW DC will not have a
		 * KVNO in the upper bits, while a packet processed
		 * locally on the RODC will have these bits filled in
		 * the msDS-SecondaryKrbTgtNumber
		 */
		if (torture_setting_bool(test_context->tctx, "expect_cached_at_rodc", false)) {
			torture_assert_int_not_equal(test_context->tctx,
						     *test_context->as_rep.ticket.enc_part.kvno & 0xFFFF0000,
						     0, "Did not get a RODC number in the KVNO");
		} else {
			torture_assert_int_equal(test_context->tctx,
						 *test_context->as_rep.ticket.enc_part.kvno & 0xFFFF0000,
						 0, "Unexpecedly got a RODC number in the KVNO");
		}
		free_AS_REP(&test_context->as_rep);
	}
	torture_assert(test_context->tctx, test_context->packet_count < 3, "too many packets");
	free_AS_REQ(&test_context->as_req);
	return true;
}

/*
 * TEST_TGS_REQ_KRBTGT_CANON
 *
 *
 * Confirm that the outgoing TGS-REQ packet from krb5_get_creds()
 * for the krbtgt/realm principal meets certain expectations, like
 * that the canonicalize bit is not set
 *
 */

static bool torture_krb5_pre_send_tgs_req_krbtgt_canon_test(struct torture_krb5_context *test_context, const krb5_data *send_buf, krb5_data *modified_send_buf)
{
	size_t used;
	torture_assert_int_equal(test_context->tctx,
				 decode_TGS_REQ(send_buf->data, send_buf->length,
						&test_context->tgs_req, &used),
				 0, "decode_TGS_REQ for TEST_TGS_REQ test failed");
	torture_assert_int_equal(test_context->tctx,
				 used, send_buf->length,
				 "length mismatch");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.pvno, 5,
				 "Got wrong as_req->pvno");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.req_body.kdc_options.canonicalize,
				 true,
				 "krb5 libs unexpectedly did not set canonicalize!");

	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.req_body.sname->name_type,
				 KRB5_NT_PRINCIPAL,
				 "Mismatch in name_type between request and expected request");

	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_req.req_body.realm,
				 test_context->test_data->real_realm,
				 "Mismatch in realm between request and expected request");

	*modified_send_buf = *send_buf;
	return true;
}

/*
 * TEST_TGS_REQ_KRBTGT_CANON
 *
 * Confirm that the reply TGS-REP packet for krb5_get_creds()
 * where the client is behaving as if this is a cross-realm trust due
 * to case or netbios vs dns name differences meets certain
 * expectations, while canonicalize is set
 *
 */

static bool torture_krb5_post_recv_tgs_req_krbtgt_canon_test(struct torture_krb5_context *test_context, const krb5_data *recv_buf)
{
	size_t used;
	torture_assert_int_equal(test_context->tctx,
				 decode_TGS_REP(recv_buf->data, recv_buf->length,
						&test_context->tgs_rep, &used),
				 0,
				 "decode_TGS_REP failed");
	torture_assert_int_equal(test_context->tctx, used, recv_buf->length, "length mismatch");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_rep.pvno, 5,
				 "Got wrong as_rep->pvno");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.tkt_vno, 5,
				 "Got wrong as_rep->ticket.tkt_vno");
	torture_assert(test_context->tctx,
		       test_context->tgs_rep.ticket.enc_part.kvno,
		       "Did not get a KVNO in test_context->as_rep.ticket.enc_part.kvno");
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_req.req_body.realm,
				 test_context->tgs_rep.ticket.realm,
				 "Mismatch in realm between request and ticket response");
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.realm,
				 test_context->test_data->real_realm,
				 "Mismatch in realm between ticket response and expected ticket response");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.sname.name_type,
				 KRB5_NT_SRV_INST,
				 "Mismatch in name_type between ticket response and expected value of KRB5_NT_SRV_INST");

	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.sname.name_string.len,
				 2,
				 "Mismatch in name_type between ticket response and expected value, expected krbtgt/REALM@REALM");

	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.sname.name_string.val[0], "krbtgt",
				 "Mismatch in name between reponse and expected response, expected krbtgt");
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.sname.name_string.val[1], test_context->test_data->real_realm,
				 "Mismatch in realm part of krbtgt/ in expected response, expected krbtgt/REALM@REALM");

	/*
	 * We can confirm that the correct proxy behaviour is
	 * in use on the KDC by checking the KVNO of the
	 * krbtgt account returned in the reply.
	 *
	 * A packet passed to the full RW DC will not have a
	 * KVNO in the upper bits, while a packet processed
	 * locally on the RODC will have these bits filled in
	 * the msDS-SecondaryKrbTgtNumber
	 */
	if (torture_setting_bool(test_context->tctx, "expect_cached_at_rodc", false)) {
		torture_assert_int_not_equal(test_context->tctx,
					     *test_context->tgs_rep.ticket.enc_part.kvno & 0xFFFF0000,
					     0, "Did not get a RODC number in the KVNO");
	} else {
		torture_assert_int_equal(test_context->tctx,
					 *test_context->tgs_rep.ticket.enc_part.kvno & 0xFFFF0000,
					 0, "Unexpecedly got a RODC number in the KVNO");
	}
	free_TGS_REP(&test_context->tgs_rep);
	torture_assert(test_context->tctx,
		       test_context->packet_count < 2,
		       "too many packets");
	free_TGS_REQ(&test_context->tgs_req);
	return true;
}

/*
 * TEST_TGS_REQ_CANON
 *
 * Confirm that the outgoing TGS-REQ packet from krb5_get_creds
 * certain expectations, like that the canonicalize bit is set (this
 * test is to force that handling) and that if an enterprise name was
 * requested, that it was sent.
 *
 */

static bool torture_krb5_pre_send_tgs_req_canon_test(struct torture_krb5_context *test_context,
						     const krb5_data *send_buf,
						     krb5_data *modified_send_buf)
{
	size_t used;
	torture_assert_int_equal(test_context->tctx,
				 decode_TGS_REQ(send_buf->data, send_buf->length,
						&test_context->tgs_req, &used),
				 0, "decode_TGS_REQ for TEST_TGS_REQ_CANON test failed");
	torture_assert_int_equal(test_context->tctx, used, send_buf->length, "length mismatch");
	torture_assert_int_equal(test_context->tctx, test_context->tgs_req.pvno, 5, "Got wrong as_req->pvno");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.req_body.kdc_options.canonicalize,
				 true, "krb5 libs unexpectedly did not set canonicalize!");

	if (test_context->test_data->enterprise) {
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_type, KRB5_NT_ENTERPRISE_PRINCIPAL,
				 "Mismatch in name type between request and expected request, expected  KRB5_NT_ENTERPRISE_PRINCIPAL");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.realm, test_context->test_data->real_realm,
				 "Mismatch in realm between request and expected request");

	} else if (test_context->test_data->canonicalize) {
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_type, KRB5_NT_PRINCIPAL,
					 "Mismatch in name type between request and expected request, expected  KRB5_NT_PRINCIPAL");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.realm, test_context->test_data->real_realm,
				 "Mismatch in realm between request and expected request");

	} else {
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_type, KRB5_NT_PRINCIPAL,
					 "Mismatch in name type between request and expected request, expected  KRB5_NT_PRINCIPAL");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.realm, test_context->test_data->realm,
				 "Mismatch in realm between request and expected request");

	}

	*modified_send_buf = *send_buf;

	return true;
}

/*
 * TEST_TGS_REQ_CANON - RECV
 *
 * Confirm that the reply TGS-REP or error packet from the KDC meets
 * certain expectations as part of TEST_TGS_REQ_CANON.
 *
 * This is triggered by krb5_get_creds()
 *
 */

static bool torture_krb5_post_recv_tgs_req_canon_test(struct torture_krb5_context *test_context, const krb5_data *recv_buf)
{
	KRB_ERROR error;
	size_t used;

	/*
	 * If this account did not have a servicePrincipalName, then
	 * we expect a errro packet, not a TGS-REQ
	 */
	if (decode_KRB_ERROR(recv_buf->data, recv_buf->length, &error, &used) == 0) {
		torture_assert_int_equal(test_context->tctx, used, recv_buf->length, "length mismatch");
		torture_assert_int_equal(test_context->tctx,
					 error.pvno, 5,
					 "Got wrong error.pvno");
		torture_assert_int_equal(test_context->tctx,
					 error.error_code,
					 KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN - KRB5KDC_ERR_NONE,
					 "Got wrong error.error_code");
	} else {
		torture_assert_int_equal(test_context->tctx,
					 decode_TGS_REP(recv_buf->data, recv_buf->length,
							&test_context->tgs_rep,
							&used),
					 0,
					 "decode_TGS_REP failed");
		torture_assert_int_equal(test_context->tctx,
					 used, recv_buf->length,
					 "length mismatch");
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_rep.pvno, 5,
					 "Got wrong as_rep->pvno");
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_rep.ticket.tkt_vno, 5,
					 "Got wrong as_rep->ticket.tkt_vno");
		torture_assert(test_context->tctx,
			       test_context->tgs_rep.ticket.enc_part.kvno,
			       "Did not get a KVNO in test_context->as_rep.ticket.enc_part.kvno");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_rep.ticket.realm,
					 test_context->test_data->real_realm,
					 "Mismatch in realm between ticket response and expected upper case REALM");
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_rep.ticket.sname.name_type,
					 test_context->tgs_req.req_body.sname->name_type,
					 "Mismatch in name_type between request and ticket response");
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_rep.ticket.sname.name_string.len,
					 test_context->tgs_req.req_body.sname->name_string.len,
					 "Mismatch in name_string.len between request and ticket response");
		torture_assert(test_context->tctx,
			       test_context->tgs_rep.ticket.sname.name_string.len >= 1,
			       "name_string.len should be >=1 in ticket response");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_rep.ticket.sname.name_string.val[0],
					 test_context->tgs_req.req_body.sname->name_string.val[0],
					 "Mismatch in name between request and expected request");
		torture_assert_int_equal(test_context->tctx,
					 *test_context->tgs_rep.ticket.enc_part.kvno & 0xFFFF0000,
					 0, "Unexpecedly got a RODC number in the KVNO, should just be principal KVNO");
		free_TGS_REP(&test_context->tgs_rep);
	}
	torture_assert(test_context->tctx, test_context->packet_count == 0, "too many packets");
	free_TGS_REQ(&test_context->tgs_req);

	return true;
}

/*
 * TEST_SELF_TRUST_TGS_REQ
 *
 * Confirm that the outgoing TGS-REQ packet from krb5_mk_req_exact()
 * certain expectations, like that the canonicalize bit is set (this
 * test is to force that handling).
 *
 * This test is for the case where the name we ask for, while a valid
 * alternate name for our own realm is used.  The client acts as if
 * this is cross-realm trust.
 *
 */

static bool torture_krb5_pre_send_self_trust_tgs_req_test(struct torture_krb5_context *test_context,
							  const krb5_data *send_buf,
							  krb5_data *modified_send_buf)
{
	size_t used;
	torture_assert_int_equal(test_context->tctx,
				 decode_TGS_REQ(send_buf->data, send_buf->length,
						&test_context->tgs_req, &used),
				 0, "decode_TGS_REQ for TEST_SELF_TRUST_TGS_REQ test failed");
	torture_assert_int_equal(test_context->tctx, used, send_buf->length, "length mismatch");
	torture_assert_int_equal(test_context->tctx, test_context->tgs_req.pvno, 5, "Got wrong as_req->pvno");
	torture_assert_int_equal(test_context->tctx, test_context->tgs_req.req_body.kdc_options.canonicalize, false, "krb5 libs unexpectedly set canonicalize!");

	if (test_context->test_data->canonicalize) {
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.realm,
					 test_context->test_data->real_realm,
					 "Mismatch in realm between request and expected request");
	} else {
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.realm,
					 test_context->test_data->realm,
					 "Mismatch in realm between request and expected request");
	}
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.req_body.sname->name_type, KRB5_NT_PRINCIPAL,
				 "Mismatch in name type between request and expected request, expected  KRB5_NT_PRINCIPAL");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.req_body.sname->name_string.len, 2,
				 "Mismatch in name between request and expected request, expected krbtgt/realm");
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_req.req_body.sname->name_string.val[0], "krbtgt",
				 "Mismatch in name between request and expected request, expected krbtgt");
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_req.req_body.sname->name_string.val[1], test_context->test_data->realm,
				 "Mismatch in realm part of cross-realm request principal between request and expected request");
	*modified_send_buf = *send_buf;

	return true;
}

/*
 * TEST_SELF_TRUST_TGS_REQ and TEST_TGS_REQ_KRBTGT - RECV
 *
 * Confirm that the reply TGS-REP packet for krb5_mk_req_exact(),
 * where the client is behaving as if this is a cross-realm trust due
 * to case or netbios vs dns name differences meets certain
 * expectations.
 *
 */

static bool torture_krb5_post_recv_self_trust_tgs_req_test(struct torture_krb5_context *test_context, const krb5_data *recv_buf)
{
	size_t used;
	torture_assert_int_equal(test_context->tctx,
				 decode_TGS_REP(recv_buf->data, recv_buf->length,
						&test_context->tgs_rep, &used),
				 0,
				 "decode_TGS_REP failed");
	torture_assert_int_equal(test_context->tctx, used, recv_buf->length, "length mismatch");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_rep.pvno, 5,
				 "Got wrong as_rep->pvno");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.tkt_vno, 5,
				 "Got wrong as_rep->ticket.tkt_vno");
	torture_assert(test_context->tctx,
		       test_context->tgs_rep.ticket.enc_part.kvno,
		       "Did not get a KVNO in test_context->as_rep.ticket.enc_part.kvno");
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_req.req_body.realm,
				 test_context->tgs_rep.ticket.realm,
				 "Mismatch in realm between request and ticket response");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.sname.name_type,
				 test_context->tgs_req.req_body.sname->name_type,
				 "Mismatch in name_type between request and ticket response");

	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.sname.name_string.len, 2,
				 "Mismatch in name between request and expected request, expected krbtgt/realm");
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.sname.name_string.val[0], "krbtgt",
				 "Mismatch in name between request and expected request, expected krbtgt");
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.sname.name_string.val[1], test_context->test_data->realm,
				 "Mismatch in realm part of cross-realm request principal between response and expected request");
	/*
	 * We can confirm that the correct proxy behaviour is
	 * in use on the KDC by checking the KVNO of the
	 * krbtgt account returned in the reply.
	 *
	 * A packet passed to the full RW DC will not have a
	 * KVNO in the upper bits, while a packet processed
	 * locally on the RODC will have these bits filled in
	 * the msDS-SecondaryKrbTgtNumber
	 */
	if (torture_setting_bool(test_context->tctx, "expect_cached_at_rodc", false)) {
		torture_assert_int_not_equal(test_context->tctx,
					     *test_context->tgs_rep.ticket.enc_part.kvno & 0xFFFF0000,
					     0, "Did not get a RODC number in the KVNO");
	} else {
		torture_assert_int_equal(test_context->tctx,
					 *test_context->tgs_rep.ticket.enc_part.kvno & 0xFFFF0000,
					 0, "Unexpecedly got a RODC number in the KVNO");
	}
	free_TGS_REP(&test_context->tgs_rep);
	torture_assert_int_equal(test_context->tctx,
				 test_context->packet_count, 0,
				 "too many packets");
	test_context->packet_count = 0;
	test_context->test_stage = TEST_TGS_REQ;
	free_TGS_REQ(&test_context->tgs_req);
	return true;
}

/*
 * TEST_TGS_REQ
 *
 * Confirm that the outgoing TGS-REQ packet from krb5_mk_req_exact()
 * certain expectations, like that the canonicalize bit is set (this
 * test is to force that handling) and that if an enterprise name was
 * requested, that it was sent.
 *
 */

static bool torture_krb5_pre_send_tgs_req_test(struct torture_krb5_context *test_context, const krb5_data *send_buf, krb5_data *modified_send_buf)
{
	size_t used;
	torture_assert_int_equal(test_context->tctx,
				 decode_TGS_REQ(send_buf->data, send_buf->length,
						&test_context->tgs_req, &used),
				 0, "decode_TGS_REQ for TEST_TGS_REQ test failed");
	torture_assert_int_equal(test_context->tctx, used, send_buf->length, "length mismatch");
	torture_assert_int_equal(test_context->tctx, test_context->tgs_req.pvno, 5,
				 "Got wrong as_req->pvno");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.req_body.kdc_options.canonicalize,
				 false,
				 "krb5 libs unexpectedly set canonicalize!");

	if (test_context->test_data->enterprise) {
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_type,
					 KRB5_NT_ENTERPRISE_PRINCIPAL,
					 "Mismatch in name type between request and expected request, expected  KRB5_NT_ENTERPRISE_PRINCIPAL");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.realm,
					 test_context->test_data->real_realm,
					 "Mismatch in realm between request and expected request");

	} else {
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_type,
					 KRB5_NT_PRINCIPAL,
					 "Mismatch in name type between request and expected request, expected  KRB5_NT_PRINCIPAL");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.realm,
					 test_context->test_data->realm,
					 "Mismatch in realm between request and expected request");

	}

	*modified_send_buf = *send_buf;

	return true;
}

/*
 * TEST_TGS_REQ - RECV
 *
 * Confirm that the reply TGS-REP packet for krb5_mk_req_exact(), for
 * the actual target service.
 *
 */

static bool torture_krb5_post_recv_tgs_req_test(struct torture_krb5_context *test_context, const krb5_data *recv_buf)
{
	KRB_ERROR error;
	size_t used;
	/*
	 * If this account did not have a servicePrincipalName, then
	 * we expect a errro packet, not a TGS-REQ
	 */
	if (decode_KRB_ERROR(recv_buf->data, recv_buf->length, &error, &used) == 0) {
		torture_assert_int_equal(test_context->tctx,
					 used, recv_buf->length,
					 "length mismatch");
		torture_assert_int_equal(test_context->tctx,
					 error.pvno, 5,
					 "Got wrong error.pvno");
		torture_assert_int_equal(test_context->tctx,
					 error.error_code,
					 KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN - KRB5KDC_ERR_NONE,
					 "Got wrong error.error_code");
	} else {
		torture_assert_int_equal(test_context->tctx,
					 decode_TGS_REP(recv_buf->data, recv_buf->length,
							&test_context->tgs_rep, &used),
					 0,
					 "decode_TGS_REP failed");
		torture_assert_int_equal(test_context->tctx, used, recv_buf->length,
					 "length mismatch");
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_rep.pvno, 5,
					 "Got wrong as_rep->pvno");
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_rep.ticket.tkt_vno, 5,
					 "Got wrong as_rep->ticket.tkt_vno");
		torture_assert(test_context->tctx,
			       test_context->tgs_rep.ticket.enc_part.kvno,
			       "Did not get a KVNO in test_context->as_rep.ticket.enc_part.kvno");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_rep.ticket.realm,
					 test_context->test_data->real_realm,
					 "Mismatch in realm between ticket response and expected upper case REALM");
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_type,
					 test_context->tgs_rep.ticket.sname.name_type, "Mismatch in name_type between request and ticket response");
		torture_assert_int_equal(test_context->tctx,
					 *test_context->tgs_rep.ticket.enc_part.kvno & 0xFFFF0000,
					 0, "Unexpecedly got a RODC number in the KVNO, should just be principal KVNO");
		free_TGS_REP(&test_context->tgs_rep);
	}
	torture_assert(test_context->tctx, test_context->packet_count < 3, "too many packets");
	free_TGS_REQ(&test_context->tgs_req);
	test_context->test_stage = TEST_DONE;
	return true;
}

/*
 * TEST_TGS_REQ_KRBTGT
 *
 *
 * Confirm that the outgoing TGS-REQ packet from krb5_mk_req_exact()
 * for the krbtgt/realm principal meets certain expectations, like
 * that the canonicalize bit is not set
 *
 */

static bool torture_krb5_pre_send_tgs_req_krbtgt_test(struct torture_krb5_context *test_context, const krb5_data *send_buf, krb5_data *modified_send_buf)
{
	size_t used;
	torture_assert_int_equal(test_context->tctx,
				 decode_TGS_REQ(send_buf->data, send_buf->length,
						&test_context->tgs_req, &used),
				 0, "decode_TGS_REQ for TEST_TGS_REQ test failed");
	torture_assert_int_equal(test_context->tctx,
				 used, send_buf->length,
				 "length mismatch");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.pvno, 5,
				 "Got wrong as_req->pvno");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.req_body.kdc_options.canonicalize,
				 false,
				 "krb5 libs unexpectedly set canonicalize!");

	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_req.req_body.realm,
				 test_context->test_data->realm,
				 "Mismatch in realm between request and expected request");

	*modified_send_buf = *send_buf;
	test_context->test_stage = TEST_DONE;
	return true;
}

/*
 * TEST_TGS_REQ_HOST, TEST_TGS_REQ_HOST_SRV_INST and TEST_TGS_REQ_HOST_SRV_HST
 *
 *
 * Confirm that the outgoing TGS-REQ packet from krb5_mk_req_exact()
 * for the krbtgt/realm principal meets certain expectations, like
 * that the canonicalize bit is not set
 *
 */

static bool torture_krb5_pre_send_tgs_req_host_test(struct torture_krb5_context *test_context, const krb5_data *send_buf, krb5_data *modified_send_buf)
{
	size_t used;
	torture_assert_int_equal(test_context->tctx,
				 decode_TGS_REQ(send_buf->data, send_buf->length,
						&test_context->tgs_req, &used),
				 0, "decode_TGS_REQ for TEST_TGS_REQ test failed");
	torture_assert_int_equal(test_context->tctx,
				 used, send_buf->length,
				 "length mismatch");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.pvno, 5,
				 "Got wrong as_req->pvno");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.req_body.sname->name_string.len, 2,
				 "Mismatch in name between request and expected request, expected krbtgt/realm");
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_req.req_body.kdc_options.canonicalize,
					 true,
					 "krb5 libs unexpectedly did not set canonicalize!");

	if (test_context->test_stage == TEST_TGS_REQ_HOST_SRV_INST) {
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_type, KRB5_NT_SRV_INST,
					 "Mismatch in name type between request and expected request, expected KRB5_NT_SRV_INST");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_string.val[0],
					 strupper_talloc(test_context, test_context->test_data->krb5_service),
					 "Mismatch in name between request and expected request, expected service");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_string.val[1],
					 test_context->test_data->krb5_hostname,
					 "Mismatch in hostname part between request and expected request");

	} else if (test_context->test_stage == TEST_TGS_REQ_HOST_SRV_HST) {

		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_type, KRB5_NT_SRV_HST,
					 "Mismatch in name type between request and expected request, expected KRB5_NT_SRV_HST");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_string.val[0],
					 test_context->test_data->krb5_service,
					 "Mismatch in name between request and expected request, expected service");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_string.val[1],
					 strupper_talloc(test_context, test_context->test_data->krb5_hostname),
					 "Mismatch in hostname part between request and expected request");

	} else {
		torture_assert_int_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_type, KRB5_NT_PRINCIPAL,
					 "Mismatch in name type between request and expected request, expected  KRB5_NT_PRINCIPAL");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_string.val[0],
					 test_context->test_data->krb5_service,
					 "Mismatch in name between request and expected request, expected service");
		torture_assert_str_equal(test_context->tctx,
					 test_context->tgs_req.req_body.sname->name_string.val[1],
					 test_context->test_data->krb5_hostname,
					 "Mismatch in hostname part between request and expected request");

	}
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_req.req_body.realm,
				 test_context->test_data->real_realm,
				 "Mismatch in realm between request and expected request");

	*modified_send_buf = *send_buf;
	return true;
}

/*
 * TEST_TGS_REQ_HOST, TEST_TGS_REQ_HOST_SRV_INST, TEST_TGS_REQ_HOST_SRV_HST - RECV
 *
 * Confirm that the reply TGS-REP packet for krb5_mk_req(), for
 * the actual target service, as a SPN, not a any other name type.
 *
 */

static bool torture_krb5_post_recv_tgs_req_host_test(struct torture_krb5_context *test_context, const krb5_data *recv_buf)
{
	size_t used;
	torture_assert_int_equal(test_context->tctx,
				 decode_TGS_REP(recv_buf->data, recv_buf->length,
						&test_context->tgs_rep, &used),
				 0,
				 "decode_TGS_REP failed");
	torture_assert_int_equal(test_context->tctx, used, recv_buf->length,
				 "length mismatch");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_rep.pvno, 5,
				 "Got wrong as_rep->pvno");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.tkt_vno, 5,
				 "Got wrong as_rep->ticket.tkt_vno");
	torture_assert(test_context->tctx,
		       test_context->tgs_rep.ticket.enc_part.kvno,
		       "Did not get a KVNO in test_context->as_rep.ticket.enc_part.kvno");
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.realm,
				 test_context->test_data->real_realm,
				 "Mismatch in realm between ticket response and expected upper case REALM");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_req.req_body.sname->name_type,
				 test_context->tgs_rep.ticket.sname.name_type, "Mismatch in name_type between request and ticket response");
	torture_assert_int_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.sname.name_string.len, 2,
				 "Mismatch in name between request and expected request, expected service/hostname");
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.sname.name_string.val[0],
				 test_context->tgs_req.req_body.sname->name_string.val[0],
				 "Mismatch in name between request and expected request, expected service/hostname");
	torture_assert_str_equal(test_context->tctx,
				 test_context->tgs_rep.ticket.sname.name_string.val[1],
				 test_context->tgs_req.req_body.sname->name_string.val[1],
				 "Mismatch in name between request and expected request, expected service/hostname");

	torture_assert_int_equal(test_context->tctx,
				 *test_context->tgs_rep.ticket.enc_part.kvno & 0xFFFF0000,
				 0, "Unexpecedly got a RODC number in the KVNO, should just be principal KVNO");
	free_TGS_REP(&test_context->tgs_rep);

	torture_assert(test_context->tctx, test_context->packet_count < 2, "too many packets");
	return true;
}

/*
 * TEST_AS_REQ_SELF - RECV
 *
 * Confirm that the reply packet from the KDC meets certain
 * expectations as part of TEST_AS_REQ.  This uses a packet count to
 * work out what packet we are up to in the multiple exchanged
 * triggerd by krb5_get_init_creds_password().
 *
 */

static bool torture_krb5_post_recv_as_req_self_test(struct torture_krb5_context *test_context,
						    const krb5_data *recv_buf)
{
	KRB_ERROR error;
	size_t used;
	if (test_context->packet_count == 0) {
		krb5_error_code k5ret;
		/*
		 * The client libs obtain the salt by attempting to
		 * authenticate without pre-authentication and getting
		 * the correct salt with the
		 * KRB5KDC_ERR_PREAUTH_REQUIRED error.  If we are in
		 * the test (netbios_realm && upn) that deliberatly
		 * has an incorrect principal, we check we get the
		 * correct error.
		 */
		k5ret = decode_KRB_ERROR(recv_buf->data, recv_buf->length,
					 &error, &used);
		if (k5ret != 0) {
			AS_REP as_rep;
			k5ret = decode_AS_REP(recv_buf->data, recv_buf->length,
				      &as_rep, &used);
			if (k5ret == 0) {
				if (torture_setting_bool(test_context->tctx, "expect_machine_account", false) == false
				    || (test_context->test_data->upn == true)) {
					torture_assert(test_context->tctx, false,
						       "expected to get a KRB_ERROR packet with "
						       "KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN or KRB5KDC_ERR_PREAUTH_REQUIRED, got valid AS-REP");
				} else {
					torture_assert(test_context->tctx, false,
						       "expected to get a KRB_ERROR packet with "
						       "KRB5KDC_ERR_PREAUTH_REQUIRED, got valid AS-REP");
				}
			} else {
				if (torture_setting_bool(test_context->tctx, "expect_machine_account", false) == false
				    || (test_context->test_data->upn == true)) {
					torture_assert(test_context->tctx, false,
						       "unable to decode as KRB-ERROR or AS-REP, "
						       "expected to get a KRB_ERROR packet with KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN or KRB5KDC_ERR_PREAUTH_REQUIRED");
				} else {
					torture_assert(test_context->tctx, false,
						       "unable to decode as KRB-ERROR or AS-REP, "
						       "expected to get a KRB_ERROR packet with KRB5KDC_ERR_PREAUTH_REQUIRED");
				}
			}
		}
		torture_assert_int_equal(test_context->tctx, used, recv_buf->length,
					 "length mismatch");
		torture_assert_int_equal(test_context->tctx, error.pvno, 5,
					 "Got wrong error.pvno");
		if ((torture_setting_bool(test_context->tctx, "expect_machine_account", false) == false
		     || (test_context->test_data->upn == true))
		    && error.error_code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN - KRB5KDC_ERR_NONE) {
			/*
			 * IGNORE
			 *
			 * This case is because Samba's Heimdal KDC
			 * checks server and client accounts before
			 * checking for pre-authentication.
			 */
		} else {
			torture_assert_int_equal(test_context->tctx,
						 error.error_code,
						 KRB5KDC_ERR_PREAUTH_REQUIRED - KRB5KDC_ERR_NONE,
						 "Got wrong error.error_code");
		}

		free_KRB_ERROR(&error);
	} else if ((decode_KRB_ERROR(recv_buf->data, recv_buf->length, &error, &used) == 0)
		   && (test_context->packet_count == 1)) {
		/*
		 * The Windows 2012R2 KDC will always respond with
		 * KRB5KRB_ERR_RESPONSE_TOO_BIG over UDP as the ticket
		 * won't fit, because of the PAC.  (It appears to do
		 * this always, even if it will).  This triggers the
		 * client to try again over TCP.
		 */
		torture_assert_int_equal(test_context->tctx,
					 used, recv_buf->length,
					 "length mismatch");
		torture_assert_int_equal(test_context->tctx,
					 error.pvno, 5,
					 "Got wrong error.pvno");
		if ((torture_setting_bool(test_context->tctx, "expect_machine_account", false)
		     && (test_context->test_data->upn == false))) {
			torture_assert_int_equal(test_context->tctx,
						 error.error_code,
						 KRB5KRB_ERR_RESPONSE_TOO_BIG - KRB5KDC_ERR_NONE,
						 "Got wrong error.error_code");
		} else {
			torture_assert_int_equal(test_context->tctx,
						 error.error_code,
						 KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN - KRB5KDC_ERR_NONE,
						 "Got wrong error.error_code");
		}
		free_KRB_ERROR(&error);
	} else {
		/*
		 * Finally the successful packet.
		 */
		torture_assert_int_equal(test_context->tctx,
					 decode_AS_REP(recv_buf->data, recv_buf->length,
						       &test_context->as_rep, &used), 0,
					 "decode_AS_REP failed");
		torture_assert_int_equal(test_context->tctx, used, recv_buf->length,
					 "length mismatch");
		torture_assert_int_equal(test_context->tctx,
					 test_context->as_rep.pvno, 5,
					 "Got wrong as_rep->pvno");
		torture_assert_int_equal(test_context->tctx,
					 test_context->as_rep.ticket.tkt_vno, 5,
					 "Got wrong as_rep->ticket.tkt_vno");
		torture_assert(test_context->tctx,
			       test_context->as_rep.ticket.enc_part.kvno,
			       "Did not get a KVNO in test_context->as_rep.ticket.enc_part.kvno");

		/*
		 * We do not expect an RODC number here in the KVNO,
		 * as this is a ticket to the user's own account.
		 */
		torture_assert_int_equal(test_context->tctx,
					 *test_context->as_rep.ticket.enc_part.kvno & 0xFFFF0000,
					 0, "Unexpecedly got a RODC number in the KVNO");
		free_AS_REP(&test_context->as_rep);
	}
	torture_assert(test_context->tctx, test_context->packet_count < 3, "too many packets");
	free_AS_REQ(&test_context->as_req);
	return true;
}

/*
 * This function is set in torture_krb5_init_context_canon as krb5
 * send_and_recv function.  This allows us to override what server the
 * test is aimed at, and to inspect the packets just before they are
 * sent to the network, and before they are processed on the recv
 * side.
 *
 * The torture_krb5_pre_send_test() and torture_krb5_post_recv_test()
 * functions are implement the actual tests.
 *
 * When this asserts, the caller will get a spurious 'cannot contact
 * any KDC' message.
 *
 */
static krb5_error_code smb_krb5_send_and_recv_func_canon_override(krb5_context context,
								   void *data, /* struct torture_krb5_context */
								   krb5_krbhst_info *hi,
								   time_t timeout,
								   const krb5_data *send_buf,
								   krb5_data *recv_buf)
{
	krb5_error_code k5ret;
	bool ok = false;
	krb5_data modified_send_buf;

	struct torture_krb5_context *test_context
		= talloc_get_type_abort(data, struct torture_krb5_context);

	switch (test_context->test_stage) {
	case TEST_DONE:
		torture_warning(test_context->tctx, "Unexpected outgoing packet from krb5 libs");
		return EINVAL;
	case TEST_AS_REQ:
		ok = torture_krb5_pre_send_as_req_test(test_context, send_buf,
							      &modified_send_buf);
		break;
	case TEST_TGS_REQ_KRBTGT_CANON:
		ok = torture_krb5_pre_send_tgs_req_krbtgt_canon_test(test_context, send_buf,
								     &modified_send_buf);
		break;
	case TEST_TGS_REQ_CANON:
		ok = torture_krb5_pre_send_tgs_req_canon_test(test_context, send_buf,
							      &modified_send_buf);
		break;
	case TEST_SELF_TRUST_TGS_REQ:
		ok = torture_krb5_pre_send_self_trust_tgs_req_test(test_context, send_buf,
								   &modified_send_buf);
		break;
	case TEST_TGS_REQ:
		ok = torture_krb5_pre_send_tgs_req_test(test_context, send_buf,
							&modified_send_buf);
		break;
	case TEST_TGS_REQ_KRBTGT:
		ok = torture_krb5_pre_send_tgs_req_krbtgt_test(test_context, send_buf,
							       &modified_send_buf);
		break;
	case TEST_TGS_REQ_HOST:
	case TEST_TGS_REQ_HOST_SRV_INST:
	case TEST_TGS_REQ_HOST_SRV_HST:
		ok = torture_krb5_pre_send_tgs_req_host_test(test_context, send_buf,
							     &modified_send_buf);
		break;
	case TEST_AS_REQ_SELF:
		ok = torture_krb5_pre_send_as_req_test(test_context, send_buf,
						       &modified_send_buf);
		break;
	}
	if (ok == false) {
		return EINVAL;
	}

	k5ret = smb_krb5_send_and_recv_func_forced(context, test_context->server,
						   hi, timeout, &modified_send_buf,
						   recv_buf);
	if (k5ret != 0) {
		return k5ret;
	}

	switch (test_context->test_stage) {
	case TEST_DONE:
		torture_warning(test_context->tctx, "Unexpected outgoing packet from krb5 libs");
		return EINVAL;
	case TEST_AS_REQ:
		ok = torture_krb5_post_recv_as_req_test(test_context, recv_buf);
		break;
	case TEST_TGS_REQ_KRBTGT_CANON:
		ok = torture_krb5_post_recv_tgs_req_krbtgt_canon_test(test_context, recv_buf);
		break;
	case TEST_TGS_REQ_CANON:
		ok = torture_krb5_post_recv_tgs_req_canon_test(test_context, recv_buf);
		break;
	case TEST_SELF_TRUST_TGS_REQ:
		ok = torture_krb5_post_recv_self_trust_tgs_req_test(test_context, recv_buf);
		break;
	case TEST_TGS_REQ:
		ok = torture_krb5_post_recv_tgs_req_test(test_context, recv_buf);
		break;
	case TEST_TGS_REQ_KRBTGT:
		ok = torture_krb5_post_recv_self_trust_tgs_req_test(test_context, recv_buf);
		break;
	case TEST_TGS_REQ_HOST:
	case TEST_TGS_REQ_HOST_SRV_INST:
	case TEST_TGS_REQ_HOST_SRV_HST:
		ok = torture_krb5_post_recv_tgs_req_host_test(test_context, recv_buf);
		break;
	case TEST_AS_REQ_SELF:
		ok = torture_krb5_post_recv_as_req_self_test(test_context, recv_buf);
		break;
	}
	if (ok == false) {
		KRB_ERROR error;
		size_t used;
		torture_warning(test_context->tctx, "Packet of length %llu failed post-recv checks in test stage %d", (unsigned long long)recv_buf->length, test_context->test_stage);
		if (decode_KRB_ERROR(recv_buf->data, recv_buf->length, &error, &used) == 0) {
			torture_warning(test_context->tctx,
					"STAGE: %d Unexpectedly got a KRB-ERROR packet "
					"with error code %d (%s)",
					test_context->test_stage,
					error.error_code,
					error_message(error.error_code + KRB5KDC_ERR_NONE));
			free_KRB_ERROR(&error);
		}
		return EINVAL;
	}

	test_context->packet_count++;

	return k5ret;
}

static int test_context_destructor(struct torture_krb5_context *test_context)
{
	freeaddrinfo(test_context->server);
	return 0;
}


static bool torture_krb5_init_context_canon(struct torture_context *tctx,
					     struct test_data *test_data,
					     struct torture_krb5_context **torture_krb5_context)
{
	const char *host = torture_setting_string(tctx, "host", NULL);
	krb5_error_code k5ret;
	bool ok;

	struct torture_krb5_context *test_context = talloc_zero(tctx, struct torture_krb5_context);
	torture_assert(tctx, test_context != NULL, "Failed to allocate");

	test_context->test_data = test_data;
	test_context->tctx = tctx;

	k5ret = smb_krb5_init_context(test_context, tctx->lp_ctx, &test_context->smb_krb5_context);
	torture_assert_int_equal(tctx, k5ret, 0, "smb_krb5_init_context failed");

	ok = interpret_string_addr_internal(&test_context->server, host, AI_NUMERICHOST);
	torture_assert(tctx, ok, "Failed to parse target server");

	talloc_set_destructor(test_context, test_context_destructor);

	set_sockaddr_port(test_context->server->ai_addr, 88);

	k5ret = krb5_set_send_to_kdc_func(test_context->smb_krb5_context->krb5_context,
					  smb_krb5_send_and_recv_func_canon_override,
					  test_context);
	torture_assert_int_equal(tctx, k5ret, 0, "krb5_set_send_to_kdc_func failed");
	*torture_krb5_context = test_context;
	return true;
}


static bool torture_krb5_as_req_canon(struct torture_context *tctx, const void *tcase_data)
{
	krb5_error_code k5ret;
	krb5_get_init_creds_opt *krb_options = NULL;
	struct test_data *test_data = talloc_get_type_abort(tcase_data, struct test_data);
	krb5_principal principal;
	krb5_principal krbtgt_other;
	krb5_principal expected_principal;
	char *principal_string;
	char *krbtgt_other_string;
	int principal_flags;
	char *expected_principal_string;
	char *expected_unparse_principal_string;
	int expected_principal_flags;
	char *got_principal_string;
	char *assertion_message;
	const char *password = cli_credentials_get_password(
			popt_get_cmdline_credentials());
	krb5_context k5_context;
	struct torture_krb5_context *test_context;
	bool ok;
	krb5_creds my_creds;
	krb5_creds *server_creds;
	krb5_ccache ccache;
	krb5_auth_context auth_context;
	char *cc_name;
	krb5_data in_data, enc_ticket;
	krb5_get_creds_opt opt;

	const char *upn = torture_setting_string(tctx, "krb5-upn", "");
	test_data->krb5_service = torture_setting_string(tctx, "krb5-service", "host");
	test_data->krb5_hostname = torture_setting_string(tctx, "krb5-hostname", "");

	/*
	 * If we have not passed a UPN on the command line,
	 * then skip the UPN tests.
	 */
	if (test_data->upn && upn[0] == '\0') {
		torture_skip(tctx, "This test needs a UPN specified as --option=torture:krb5-upn=user@example.com to run");
	}

	if (test_data->removedollar &&
	    !torture_setting_bool(tctx, "run_removedollar_test", false))
	{
		torture_skip(tctx, "--option=torture:run_removedollar_test=true not specified");
	}

	if (test_data->netbios_realm) {
		test_data->realm = test_data->real_domain;
	} else {
		test_data->realm = test_data->real_realm;
	}

	if (test_data->upn) {
		char *p;
		test_data->username = talloc_strdup(test_data, upn);
		p = strchr(test_data->username, '@');
		if (p) {
			*p = '\0';
			p++;
		}
		/*
		 * Test the UPN behaviour carefully.  We can
		 * test in two different modes, depending on
		 * what UPN has been set up for us.
		 *
		 * If the UPN is in our realm, then we do all the tests with this name also.
		 *
		 * If the UPN is not in our realm, then we
		 * expect the tests that replace the realm to
		 * fail (as it won't match)
		 */
		if (strcasecmp(p, test_data->real_realm) != 0) {
			test_data->other_upn_suffix = true;
		} else {
			test_data->other_upn_suffix = false;
		}

		/*
		 * This lets us test the combination of the UPN prefix
		 * with a valid domain, without adding even more
		 * combinations
		 */
		if (test_data->netbios_realm == false) {
			test_data->realm = p;
		}
	}

	ok = torture_krb5_init_context_canon(tctx, test_data, &test_context);
	torture_assert(tctx, ok, "torture_krb5_init_context failed");
	k5_context = test_context->smb_krb5_context->krb5_context;

	if (test_data->upper_realm) {
		test_data->realm = strupper_talloc(test_data, test_data->realm);
	} else {
		test_data->realm = strlower_talloc(test_data, test_data->realm);
	}
	if (test_data->upper_username) {
		test_data->username = strupper_talloc(test_data, test_data->username);
	} else {
		test_data->username = talloc_strdup(test_data, test_data->username);
	}

	if (test_data->removedollar) {
		char *p;

		p = strchr_m(test_data->username, '$');
		torture_assert(tctx, p != NULL, talloc_asprintf(tctx,
			       "username[%s] contains no '$'\n",
			       test_data->username));
		*p = '\0';
	}

	principal_string = talloc_asprintf(test_data, "%s@%s", test_data->username, test_data->realm);

	/*
	 * If we are set to canonicalize, we get back the fixed UPPER
	 * case realm, and the real username (ie matching LDAP
	 * samAccountName)
	 *
	 * Otherwise, if we are set to enterprise, we
	 * get back the whole principal as-sent
	 *
	 * Finally, if we are not set to canonicalize, we get back the
	 * fixed UPPER case realm, but the as-sent username
	 */
	if (test_data->canonicalize) {
		expected_principal_string = talloc_asprintf(test_data,
							    "%s@%s",
							    test_data->real_username,
							    test_data->real_realm);
	} else if (test_data->enterprise) {
		expected_principal_string = principal_string;
	} else {
		expected_principal_string = talloc_asprintf(test_data,
							    "%s@%s",
							    test_data->username,
							    test_data->real_realm);
	}

	if (test_data->enterprise) {
		principal_flags = KRB5_PRINCIPAL_PARSE_ENTERPRISE;
	} else {
		if (test_data->upn && test_data->other_upn_suffix) {
			torture_skip(tctx, "UPN test for UPN with other UPN suffix only runs with enterprise principals");
		}
		principal_flags = 0;
	}

	if (test_data->canonicalize) {
		expected_principal_flags = 0;
	} else {
		expected_principal_flags = principal_flags;
	}

	torture_assert_int_equal(tctx,
				 krb5_parse_name_flags(k5_context,
						       principal_string,
						       principal_flags,
						       &principal),
					 0, "krb5_parse_name_flags failed");
	torture_assert_int_equal(tctx,
				 krb5_parse_name_flags(k5_context,
						       expected_principal_string,
						       expected_principal_flags,
						       &expected_principal),
				 0, "krb5_parse_name_flags failed");

	torture_assert_int_equal(tctx,
				 krb5_unparse_name(k5_context,
						   expected_principal,
						   &expected_unparse_principal_string),
				 0, "krb5_unparse_name failed");
	/*
	 * Prepare a AS-REQ and run the TEST_AS_REQ tests
	 *
	 */

	test_context->test_stage = TEST_AS_REQ;
	test_context->packet_count = 0;

	/*
	 * Set the canonicalize flag if this test requires it
	 */
	torture_assert_int_equal(tctx,
				 krb5_get_init_creds_opt_alloc(k5_context, &krb_options),
				 0, "krb5_get_init_creds_opt_alloc failed");

	torture_assert_int_equal(tctx,
				 krb5_get_init_creds_opt_set_canonicalize(k5_context,
									  krb_options,
									  test_data->canonicalize),
				 0, "krb5_get_init_creds_opt_set_canonicalize failed");

	torture_assert_int_equal(tctx,
				 krb5_get_init_creds_opt_set_win2k(k5_context,
								   krb_options,
								   test_data->win2k),
				 0, "krb5_get_init_creds_opt_set_win2k failed");

	k5ret = krb5_get_init_creds_password(k5_context, &my_creds, principal,
					     password, NULL, NULL, 0,
					     NULL, krb_options);

	if (test_data->netbios_realm && test_data->upn) {
		torture_assert_int_equal(tctx, k5ret,
					 KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN,
					 "Got wrong error_code from krb5_get_init_creds_password");
		/* We can't proceed with more checks */
		return true;
	} else {
		assertion_message = talloc_asprintf(tctx,
						    "krb5_get_init_creds_password for %s failed: %s",
						    principal_string,
						    smb_get_krb5_error_message(k5_context, k5ret, tctx));
		torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
	}

	torture_assert(tctx,
		       test_context->packet_count > 1,
		       "Expected krb5_get_init_creds_password to send more packets");

	/*
	 * Assert that the reply was with the correct type of
	 * principal, depending on the flags we set
	 */
	if (test_data->canonicalize == false && test_data->enterprise) {
		torture_assert_int_equal(tctx,
					 krb5_principal_get_type(k5_context,
								 my_creds.client),
					 KRB5_NT_ENTERPRISE_PRINCIPAL,
					 "smb_krb5_init_context gave incorrect client->name.name_type");
	} else {
		torture_assert_int_equal(tctx,
					 krb5_principal_get_type(k5_context,
								 my_creds.client),
					 KRB5_NT_PRINCIPAL,
					 "smb_krb5_init_context gave incorrect client->name.name_type");
	}

	torture_assert_int_equal(tctx,
				 krb5_unparse_name(k5_context,
						   my_creds.client, &got_principal_string), 0,
				 "krb5_unparse_name failed");

	assertion_message = talloc_asprintf(tctx,
					    "krb5_get_init_creds_password returned a different principal %s to what was expected %s",
					    got_principal_string, expected_principal_string);
	krb5_free_unparsed_name(k5_context, got_principal_string);

	torture_assert(tctx, krb5_principal_compare(k5_context,
						    my_creds.client, expected_principal),
		       assertion_message);


	torture_assert_int_equal(tctx,
				 krb5_principal_get_type(k5_context,
							 my_creds.server), KRB5_NT_SRV_INST,
				 "smb_krb5_init_context gave incorrect server->name.name_type");

	torture_assert_int_equal(tctx,
				 krb5_principal_get_num_comp(k5_context,
							     my_creds.server), 2,
				 "smb_krb5_init_context gave incorrect number of components in my_creds.server->name");

	torture_assert_str_equal(tctx,
				 krb5_principal_get_comp_string(k5_context,
								my_creds.server, 0),
				 "krbtgt",
				 "smb_krb5_init_context gave incorrect my_creds.server->name.name_string[0]");

	if (test_data->canonicalize || test_data->enterprise) {
		torture_assert_str_equal(tctx,
					 krb5_principal_get_comp_string(k5_context,
									my_creds.server, 1),
					 test_data->real_realm,

					 "smb_krb5_init_context gave incorrect my_creds.server->name.name_string[1]");
	} else {
		torture_assert_str_equal(tctx,
					 krb5_principal_get_comp_string(k5_context,
									my_creds.server, 1),
					 test_data->realm,

					 "smb_krb5_init_context gave incorrect my_creds.server->name.name_string[1]");
	}
	torture_assert_str_equal(tctx,
				 krb5_principal_get_realm(k5_context,
							  my_creds.server),
				 test_data->real_realm,
				 "smb_krb5_init_context gave incorrect my_creds.server->realm");

	/* Store the result of the 'kinit' above into a memory ccache */
	cc_name = talloc_asprintf(tctx, "MEMORY:%s", test_data->test_name);
	torture_assert_int_equal(tctx, krb5_cc_resolve(k5_context, cc_name,
						       &ccache),
				 0, "krb5_cc_resolve failed");

	torture_assert_int_equal(tctx, krb5_cc_initialize(k5_context,
							  ccache, my_creds.client),
				 0, "krb5_cc_initialize failed");

	torture_assert_int_equal(tctx, krb5_cc_store_cred(k5_context,
							  ccache, &my_creds),
				 0, "krb5_cc_store_cred failed");

	/*
	 * Prepare a TGS-REQ and run the TEST_TGS_REQ_KRBTGT_CANON tests
	 *
	 * This tests krb5_get_creds behaviour, which allows us to set
	 * the KRB5_GC_CANONICALIZE option against the krbtgt/ principal
	 */

	krbtgt_other_string = talloc_asprintf(test_data, "krbtgt/%s@%s", test_data->real_domain, test_data->real_realm);
	torture_assert_int_equal(tctx,
				 krb5_make_principal(k5_context, &krbtgt_other,
						     test_data->real_realm, "krbtgt",
						     test_data->real_domain, NULL),
				 0, "krb5_make_principal failed");

	test_context->test_stage = TEST_TGS_REQ_KRBTGT_CANON;
	test_context->packet_count = 0;

	torture_assert_int_equal(tctx,
				 krb5_get_creds_opt_alloc(k5_context, &opt),
				 0, "krb5_get_creds_opt_alloc");

	krb5_get_creds_opt_add_options(k5_context,
				       opt,
				       KRB5_GC_CANONICALIZE);

	krb5_get_creds_opt_add_options(k5_context,
				       opt,
				       KRB5_GC_NO_STORE);

	/* Confirm if we can get a ticket krbtgt/realm that we got back with the initial kinit */
	k5ret = krb5_get_creds(k5_context, opt, ccache, krbtgt_other, &server_creds);

	if (test_data->canonicalize == false && test_data->enterprise == false
	    && test_data->netbios_realm && test_data->upper_realm) {
		/*
		 * In these situations, the code above does store a
		 * principal in the credentials cache matching what
		 * krb5_get_creds() needs, so the test succeds, with no packets.
		 *
		 */
		assertion_message = talloc_asprintf(tctx,
						    "krb5_get_creds for %s failed with: %s",
						    krbtgt_other_string,
						    smb_get_krb5_error_message(k5_context, k5ret,
									       tctx));

		torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
		torture_assert_int_equal(tctx,
					 test_context->packet_count,
					 0, "Expected krb5_get_creds not to send packets");
	} else if (test_data->canonicalize == false && test_data->enterprise == false
		   && (test_data->upper_realm == false || test_data->netbios_realm == true)) {
		torture_assert_int_equal(tctx, k5ret, KRB5_CC_NOTFOUND,
					 "krb5_get_creds should have failed with KRB5_CC_NOTFOUND");
	} else {

		/*
		 * In these situations, the code above does not store a
		 * principal in the credentials cache matching what
		 * krb5_get_creds() needs without talking to the KDC, so the
		 * test fails with looping detected because when we set
		 * canonicalize we confuse the client libs.
		 *
		 */
		assertion_message = talloc_asprintf(tctx,
						    "krb5_get_creds for %s should have failed with looping detected: %s",
						    krbtgt_other_string,
						    smb_get_krb5_error_message(k5_context, k5ret,
									       tctx));

		torture_assert_int_equal(tctx, k5ret, KRB5_GET_IN_TKT_LOOP, assertion_message);
		torture_assert_int_equal(tctx,
					 test_context->packet_count,
					 2, "Expected krb5_get_creds to send packets");
	}

	/*
	 * Prepare a TGS-REQ and run the TEST_TGS_REQ_CANON tests
	 *
	 * This tests krb5_get_creds behaviour, which allows us to set
	 * the KRB5_GC_CANONICALIZE option
	 */

	test_context->test_stage = TEST_TGS_REQ_CANON;
	test_context->packet_count = 0;

	torture_assert_int_equal(tctx,
				 krb5_get_creds_opt_alloc(k5_context, &opt),
				 0, "krb5_get_creds_opt_alloc");

	krb5_get_creds_opt_add_options(k5_context,
				       opt,
				       KRB5_GC_CANONICALIZE);

	krb5_get_creds_opt_add_options(k5_context,
				       opt,
				       KRB5_GC_NO_STORE);

	if (test_data->s4u2self) {
		torture_assert_int_equal(tctx,
					 krb5_get_creds_opt_set_impersonate(k5_context,
									    opt,
									    principal),
					 0, "krb5_get_creds_opt_set_impersonate failed");
	}

	/* Confirm if we can get a ticket to our own name */
	k5ret = krb5_get_creds(k5_context, opt, ccache, principal, &server_creds);

	/*
	 * In these situations, the code above does not store a
	 * principal in the credentials cache matching what
	 * krb5_get_creds() needs, so the test fails.
	 *
	 */
	if (test_data->canonicalize == false && test_data->enterprise == false
	    && (test_data->upper_realm == false || test_data->netbios_realm == true)) {
		torture_assert_int_equal(tctx, k5ret, KRB5_CC_NOTFOUND,
					 "krb5_get_creds should have failed with KRB5_CC_NOTFOUND");
	} else {
		assertion_message = talloc_asprintf(tctx,
						    "krb5_get_creds for %s failed: %s",
						    principal_string,
						    smb_get_krb5_error_message(k5_context, k5ret,
									       tctx));

		/*
		 * Only machine accounts (strictly, accounts with a
		 * servicePrincipalName) can expect this test to succeed
		 */
		if (torture_setting_bool(tctx, "expect_machine_account", false)
		    && (test_data->enterprise || test_data->upn == false)) {
			torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
			torture_assert_int_equal(tctx, krb5_cc_store_cred(k5_context,
									  ccache, server_creds),
						 0, "krb5_cc_store_cred failed");

			torture_assert_int_equal(tctx,
						 krb5_free_creds(k5_context,
								 server_creds),
						 0, "krb5_free_cred_contents failed");

		} else {
			torture_assert_int_equal(tctx, k5ret, KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN,
						 assertion_message);
		}

		torture_assert_int_equal(tctx,
					 test_context->packet_count,
					 1, "Expected krb5_get_creds to send packets");
	}

	/*
	 * Confirm gettting a ticket to pass to the server, running
	 * either the TEST_TGS_REQ or TEST_SELF_TRUST_TGS_REQ stage.
	 *
	 * This triggers the client to attempt to get a
	 * cross-realm ticket between the alternate names of
	 * the server, and we need to confirm that behaviour.
	 *
	 */

	/*
	 * This tries to guess when the krb5 libs will ask for a
	 * cross-realm ticket, and when they will just ask the KDC
	 * directly.
	 */
	if (test_context->test_data->canonicalize == false
	    || test_context->test_data->enterprise
	    || (test_context->test_data->upper_realm
		&& test_context->test_data->netbios_realm == false)) {
		test_context->test_stage = TEST_TGS_REQ;
	} else {
		test_context->test_stage = TEST_SELF_TRUST_TGS_REQ;
	}

	test_context->packet_count = 0;
	torture_assert_int_equal(tctx, krb5_auth_con_init(k5_context, &auth_context),
				 0, "krb5_auth_con_init failed");

	in_data.length = 0;
	k5ret = krb5_mk_req_exact(k5_context,
				  &auth_context,
				  AP_OPTS_USE_SUBKEY,
				  principal,
				  &in_data, ccache,
				  &enc_ticket);
	assertion_message = talloc_asprintf(tctx,
					    "krb5_mk_req_exact for %s failed: %s",
					    principal_string,
					    smb_get_krb5_error_message(k5_context, k5ret, tctx));

	/*
	 * Only machine accounts (strictly, accounts with a
	 * servicePrincipalName) can expect this test to succeed
	 */
	if (torture_setting_bool(tctx, "expect_machine_account", false) && (test_data->enterprise || test_data->upn == false)) {
		DATA_BLOB client_to_server;
		torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
		client_to_server = data_blob_const(enc_ticket.data, enc_ticket.length);
		torture_assert(tctx,
			       test_accept_ticket(tctx,
					popt_get_cmdline_credentials(),
						  expected_unparse_principal_string,
						  client_to_server),
			       "test_accept_ticket failed - failed to accept the ticket we just created");
		krb5_data_free(&enc_ticket);
	} else {
		torture_assert_int_equal(tctx, k5ret, KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN,
					 assertion_message);
	}

	/*
	 * Only in these cases would the above code have needed to
	 * send packets to the network
	 */
	if (test_data->canonicalize == false && test_data->enterprise == false
	    && (test_data->upper_realm == false || test_data->netbios_realm == true)) {
		torture_assert(tctx,
			       test_context->packet_count > 0,
			       "Expected krb5_mk_req_exact to send packets");
	}

	/*
	 * Confirm gettting a ticket to pass to the server, running
	 * the TEST_TGS_REQ_HOST, TEST_TGS_REQ_HOST_SRV_INST, TEST_TGS_REQ_HOST_SRV_HST stage
	 *
	 * This triggers the client to attempt to get a
	 * cross-realm ticket between the alternate names of
	 * the server, and we need to confirm that behaviour.
	 *
	 */

	if (*test_data->krb5_service && *test_data->krb5_hostname) {
		krb5_principal host_principal_srv_inst;
		/*
		 * This tries to guess when the krb5 libs will ask for a
		 * cross-realm ticket, and when they will just ask the KDC
		 * directly.
		 */
		test_context->test_stage = TEST_TGS_REQ_HOST;
		test_context->packet_count = 0;
		torture_assert_int_equal(tctx, krb5_auth_con_init(k5_context, &auth_context),
					 0, "krb5_auth_con_init failed");

		in_data.length = 0;
		k5ret = krb5_mk_req(k5_context,
				    &auth_context,
				    0,
				    test_data->krb5_service,
				    test_data->krb5_hostname,
				    &in_data, ccache,
				    &enc_ticket);

		if (test_data->canonicalize == false && test_data->enterprise == false
		    && (test_data->upper_realm == false || test_data->netbios_realm == true)) {
			torture_assert_int_equal(tctx, k5ret, KRB5_CC_NOTFOUND,
						 "krb5_get_creds should have failed with KRB5_CC_NOTFOUND");
		} else {
			assertion_message = talloc_asprintf(tctx,
							    "krb5_mk_req for %s/%s failed: %s",
							    test_data->krb5_hostname,
							    test_data->krb5_service,
							    smb_get_krb5_error_message(k5_context, k5ret, tctx));

			torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
			/*
			 * Only in these cases would the above code have needed to
			 * send packets to the network
			 */
			torture_assert(tctx,
				       test_context->packet_count > 0,
				       "Expected krb5_get_creds to send packets");
		}


		test_context->test_stage = TEST_TGS_REQ_HOST_SRV_INST;
		test_context->packet_count = 0;

		torture_assert_int_equal(tctx,
					 krb5_make_principal(k5_context, &host_principal_srv_inst,
							     test_data->real_realm,
							     strupper_talloc(tctx, test_data->krb5_service),
							     test_data->krb5_hostname,
							     NULL),
					 0, "krb5_make_principal failed");

		krb5_principal_set_type(k5_context, host_principal_srv_inst, KRB5_NT_SRV_INST);

		torture_assert_int_equal(tctx, krb5_auth_con_init(k5_context, &auth_context),
					 0, "krb5_auth_con_init failed");

		in_data.length = 0;
		k5ret = krb5_mk_req_exact(k5_context,
					  &auth_context,
					  0,
					  host_principal_srv_inst,
					  &in_data, ccache,
					  &enc_ticket);
		krb5_free_principal(k5_context, host_principal_srv_inst);
		if (test_data->canonicalize == false && test_data->enterprise == false
		    && (test_data->upper_realm == false || test_data->netbios_realm == true)) {
			torture_assert_int_equal(tctx, k5ret, KRB5_CC_NOTFOUND,
						 "krb5_get_creds should have failed with KRB5_CC_NOTFOUND");
		} else {
			assertion_message = talloc_asprintf(tctx,
							    "krb5_mk_req for %s/%s KRB5_NT_SRV_INST failed: %s",
							    test_data->krb5_service,
							    test_data->krb5_hostname,
							    smb_get_krb5_error_message(k5_context, k5ret, tctx));

			torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
			/*
			 * Only in these cases would the above code have needed to
			 * send packets to the network
			 */
			torture_assert(tctx,
				       test_context->packet_count > 0,
				       "Expected krb5_get_creds to send packets");
		}


		test_context->test_stage = TEST_TGS_REQ_HOST_SRV_HST;
		test_context->packet_count = 0;

		torture_assert_int_equal(tctx,
					 krb5_make_principal(k5_context, &host_principal_srv_inst,
							     test_data->real_realm,
							     test_data->krb5_service,
							     strupper_talloc(tctx, test_data->krb5_hostname),
							     NULL),
					 0, "krb5_make_principal failed");

		krb5_principal_set_type(k5_context, host_principal_srv_inst, KRB5_NT_SRV_HST);

		torture_assert_int_equal(tctx, krb5_auth_con_init(k5_context, &auth_context),
					 0, "krb5_auth_con_init failed");

		in_data.length = 0;
		k5ret = krb5_mk_req_exact(k5_context,
					  &auth_context,
					  0,
					  host_principal_srv_inst,
					  &in_data, ccache,
					  &enc_ticket);
		krb5_free_principal(k5_context, host_principal_srv_inst);
		if (test_data->canonicalize == false && test_data->enterprise == false
		    && (test_data->upper_realm == false || test_data->netbios_realm == true)) {
			torture_assert_int_equal(tctx, k5ret, KRB5_CC_NOTFOUND,
						 "krb5_get_creds should have failed with KRB5_CC_NOTFOUND");
		} else {
			assertion_message = talloc_asprintf(tctx,
							    "krb5_mk_req for %s/%s KRB5_NT_SRV_INST failed: %s",
							    test_data->krb5_service,
							    test_data->krb5_hostname,
							    smb_get_krb5_error_message(k5_context, k5ret, tctx));

			torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
			/*
			 * Only in these cases would the above code have needed to
			 * send packets to the network
			 */
			torture_assert(tctx,
				       test_context->packet_count > 0,
				       "Expected krb5_get_creds to send packets");
		}
	}

	/*
	 * Confirm gettting a ticket for the same krbtgt/realm that we
	 * got back with the initial ticket, running the
	 * TEST_TGS_REQ_KRBTGT stage.
	 *
	 */

	test_context->test_stage = TEST_TGS_REQ_KRBTGT;
	test_context->packet_count = 0;

	in_data.length = 0;
	k5ret = krb5_mk_req_exact(k5_context,
				  &auth_context,
				  0,
				  my_creds.server,
				  &in_data, ccache,
				  &enc_ticket);

	assertion_message = talloc_asprintf(tctx,
					    "krb5_mk_req_exact for %s failed: %s",
					    principal_string,
					    smb_get_krb5_error_message(k5_context, k5ret, tctx));
	torture_assert_int_equal(tctx, k5ret, 0, assertion_message);

	/*
	 * Confirm gettting a ticket for our own principal that we
	 * got back with the initial ticket, running the
	 * TEST_AS_REQ_SELF stage.
	 *
	 */
	test_context->test_stage = TEST_AS_REQ_SELF;
	test_context->packet_count = 0;

	k5ret = krb5_get_init_creds_password(k5_context, &my_creds, principal,
					     password, NULL, NULL, 0,
					     principal_string, krb_options);

	if (torture_setting_bool(test_context->tctx, "expect_machine_account", false) && (test_data->upn == false)) {
		assertion_message = talloc_asprintf(tctx,
						    "krb5_get_init_creds_password for %s failed: %s",
						    principal_string,
						    smb_get_krb5_error_message(k5_context, k5ret, tctx));
		torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
		torture_assert(tctx,
			       test_context->packet_count >= 2,
			       "Expected krb5_get_init_creds_password to send more packets");

	} else {
		assertion_message = talloc_asprintf(tctx,
						    "Got wrong error_code from krb5_get_init_creds_password, expected KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN trying to get a ticket to %s for %s", principal_string, principal_string);
		torture_assert_int_equal(tctx, k5ret,
					 KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN,
					 assertion_message);
		torture_assert(tctx,
			       test_context->packet_count >= 1,
			       "Expected krb5_get_init_creds_password to send more packets");

		/* We can't proceed with more checks */
		return true;
	}

	/*
	 * Assert that the reply was with the correct type of
	 * principal, depending on the flags we set
	 */
	if (test_data->canonicalize == false && test_data->enterprise) {
		torture_assert_int_equal(tctx,
					 krb5_principal_get_type(k5_context,
								 my_creds.client),
					 KRB5_NT_ENTERPRISE_PRINCIPAL,
					 "smb_krb5_init_context gave incorrect client->name.name_type");
		torture_assert_int_equal(tctx,
					 krb5_principal_get_type(k5_context,
								 my_creds.server),
					 KRB5_NT_ENTERPRISE_PRINCIPAL,
					 "smb_krb5_init_context gave incorrect server->name.name_type");
	} else {
		torture_assert_int_equal(tctx,
					 krb5_principal_get_type(k5_context,
								 my_creds.client),
					 KRB5_NT_PRINCIPAL,
					 "smb_krb5_init_context gave incorrect client->name.name_type");
		torture_assert_int_equal(tctx,
					 krb5_principal_get_type(k5_context,
								 my_creds.server),
					 KRB5_NT_PRINCIPAL,
					 "smb_krb5_init_context gave incorrect server->name.name_type");
	}

	torture_assert_int_equal(tctx,
				 krb5_unparse_name(k5_context,
						   my_creds.client, &got_principal_string), 0,
				 "krb5_unparse_name failed");

	assertion_message = talloc_asprintf(tctx,
					    "krb5_get_init_creds_password returned a different principal %s to what was expected %s",
					    got_principal_string, expected_principal_string);
	krb5_free_unparsed_name(k5_context, got_principal_string);

	torture_assert(tctx, krb5_principal_compare(k5_context,
						    my_creds.client, expected_principal),
		       assertion_message);

	torture_assert_int_equal(tctx,
				 krb5_unparse_name(k5_context,
						   my_creds.client, &got_principal_string), 0,
				 "krb5_unparse_name failed");

	assertion_message = talloc_asprintf(tctx,
					    "krb5_get_init_creds_password returned a different server principal %s to what was expected %s",
					    got_principal_string, expected_principal_string);
	krb5_free_unparsed_name(k5_context, got_principal_string);

	torture_assert(tctx, krb5_principal_compare(k5_context,
						    my_creds.client, expected_principal),
		       assertion_message);

	krb5_free_principal(k5_context, principal);
	krb5_get_init_creds_opt_free(k5_context, krb_options);

	torture_assert_int_equal(tctx, krb5_free_cred_contents(k5_context, &my_creds),
				 0, "krb5_free_cred_contents failed");

	return true;
}

struct torture_suite *torture_krb5_canon(TALLOC_CTX *mem_ctx)
{
	unsigned int i;
	struct torture_suite *suite = torture_suite_create(mem_ctx, "canon");
	suite->description = talloc_strdup(suite, "Kerberos Canonicalisation tests");

	for (i = 0; i < TEST_ALL; i++) {
		char *name = talloc_asprintf(suite, "%s.%s.%s.%s.%s.%s.%s.%s.%s",
					     (i & TEST_CANONICALIZE) ? "canon" : "no-canon",
					     (i & TEST_ENTERPRISE) ? "enterprise" : "no-enterprise",
					     (i & TEST_UPPER_REALM) ? "uc-realm" : "lc-realm",
					     (i & TEST_UPPER_USERNAME) ? "uc-user" : "lc-user",
					     (i & TEST_NETBIOS_REALM) ? "netbios-realm" : "krb5-realm",
					     (i & TEST_WIN2K) ? "win2k" : "no-win2k",
					     (i & TEST_UPN) ? "upn" : "no-upn",
					     (i & TEST_S4U2SELF) ? "s4u2self" : "normal",
					     (i & TEST_REMOVEDOLLAR) ? "removedollar" : "keepdollar");

		struct test_data *test_data = talloc_zero(suite, struct test_data);

		test_data->test_name = name;
		test_data->real_realm
			= strupper_talloc(test_data,
				cli_credentials_get_realm(
					popt_get_cmdline_credentials()));
		test_data->real_domain = cli_credentials_get_domain(
						popt_get_cmdline_credentials());
		test_data->username = cli_credentials_get_username(
						popt_get_cmdline_credentials());
		test_data->real_username = cli_credentials_get_username(
						popt_get_cmdline_credentials());
		test_data->canonicalize = (i & TEST_CANONICALIZE) != 0;
		test_data->enterprise = (i & TEST_ENTERPRISE) != 0;
		test_data->upper_realm = (i & TEST_UPPER_REALM) != 0;
		test_data->upper_username = (i & TEST_UPPER_USERNAME) != 0;
		test_data->netbios_realm = (i & TEST_NETBIOS_REALM) != 0;
		test_data->win2k = (i & TEST_WIN2K) != 0;
		test_data->upn = (i & TEST_UPN) != 0;
		test_data->s4u2self = (i & TEST_S4U2SELF) != 0;
		test_data->removedollar = (i & TEST_REMOVEDOLLAR) != 0;
		torture_suite_add_simple_tcase_const(suite, name, torture_krb5_as_req_canon,
						     test_data);

	}
	return suite;
}