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
|
/*
Unix SMB/CIFS implementation.
handle SMBsessionsetup
Copyright (C) Andrew Tridgell 1998-2001
Copyright (C) Andrew Bartlett 2001
Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
Copyright (C) Luke Howard 2003
Copyright (C) Volker Lendecke 2007
Copyright (C) Jeremy Allison 2007
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 "smbd/globals.h"
#include "../libcli/auth/spnego.h"
#include "../libcli/auth/ntlmssp.h"
#include "librpc/gen_ndr/messaging.h"
/* For split krb5 SPNEGO blobs. */
struct pending_auth_data {
struct pending_auth_data *prev, *next;
uint16 vuid; /* Tag for this entry. */
uint16 smbpid; /* Alternate tag for this entry. */
size_t needed_len;
DATA_BLOB partial_data;
};
/*
on a logon error possibly map the error to success if "map to guest"
is set approriately
*/
NTSTATUS do_map_to_guest(NTSTATUS status,
struct auth_serversupplied_info **server_info,
const char *user, const char *domain)
{
user = user ? user : "";
domain = domain ? domain : "";
if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
if ((lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_USER) ||
(lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD)) {
DEBUG(3,("No such user %s [%s] - using guest account\n",
user, domain));
status = make_server_info_guest(NULL, server_info);
}
}
if (NT_STATUS_EQUAL(status, NT_STATUS_WRONG_PASSWORD)) {
if (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD) {
DEBUG(3,("Registered username %s for guest access\n",
user));
status = make_server_info_guest(NULL, server_info);
}
}
return status;
}
/****************************************************************************
Add the standard 'Samba' signature to the end of the session setup.
****************************************************************************/
static int push_signature(uint8 **outbuf)
{
char *lanman;
int result, tmp;
result = 0;
tmp = message_push_string(outbuf, "Unix", STR_TERMINATE);
if (tmp == -1) return -1;
result += tmp;
if (asprintf(&lanman, "Samba %s", samba_version_string()) != -1) {
tmp = message_push_string(outbuf, lanman, STR_TERMINATE);
SAFE_FREE(lanman);
}
else {
tmp = message_push_string(outbuf, "Samba", STR_TERMINATE);
}
if (tmp == -1) return -1;
result += tmp;
tmp = message_push_string(outbuf, lp_workgroup(), STR_TERMINATE);
if (tmp == -1) return -1;
result += tmp;
return result;
}
/****************************************************************************
Send a security blob via a session setup reply.
****************************************************************************/
static void reply_sesssetup_blob(struct smb_request *req,
DATA_BLOB blob,
NTSTATUS nt_status)
{
if (!NT_STATUS_IS_OK(nt_status) &&
!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
reply_nterror(req, nt_status_squash(nt_status));
return;
}
nt_status = nt_status_squash(nt_status);
SIVAL(req->outbuf, smb_rcls, NT_STATUS_V(nt_status));
SSVAL(req->outbuf, smb_vwv0, 0xFF); /* no chaining possible */
SSVAL(req->outbuf, smb_vwv3, blob.length);
if ((message_push_blob(&req->outbuf, blob) == -1)
|| (push_signature(&req->outbuf) == -1)) {
reply_nterror(req, NT_STATUS_NO_MEMORY);
}
}
/****************************************************************************
Do a 'guest' logon, getting back the
****************************************************************************/
static NTSTATUS check_guest_password(struct auth_serversupplied_info **server_info)
{
struct auth_context *auth_context;
struct auth_usersupplied_info *user_info = NULL;
NTSTATUS nt_status;
unsigned char chal[8];
ZERO_STRUCT(chal);
DEBUG(3,("Got anonymous request\n"));
if (!NT_STATUS_IS_OK(nt_status = make_auth_context_fixed(&auth_context,
chal))) {
return nt_status;
}
if (!make_user_info_guest(&user_info)) {
(auth_context->free)(&auth_context);
return NT_STATUS_NO_MEMORY;
}
nt_status = auth_context->check_ntlm_password(auth_context,
user_info,
server_info);
(auth_context->free)(&auth_context);
free_user_info(&user_info);
return nt_status;
}
#ifdef HAVE_KRB5
#if 0
/* Experiment that failed. See "only happens with a KDC" comment below. */
/****************************************************************************
Cerate a clock skew error blob for a Windows client.
****************************************************************************/
static bool make_krb5_skew_error(DATA_BLOB *pblob_out)
{
krb5_context context = NULL;
krb5_error_code kerr = 0;
krb5_data reply;
krb5_principal host_princ = NULL;
char *host_princ_s = NULL;
bool ret = False;
*pblob_out = data_blob_null;
initialize_krb5_error_table();
kerr = krb5_init_context(&context);
if (kerr) {
return False;
}
/* Create server principal. */
asprintf(&host_princ_s, "%s$@%s", global_myname(), lp_realm());
if (!host_princ_s) {
goto out;
}
strlower_m(host_princ_s);
kerr = smb_krb5_parse_name(context, host_princ_s, &host_princ);
if (kerr) {
DEBUG(10,("make_krb5_skew_error: smb_krb5_parse_name failed "
"for name %s: Error %s\n",
host_princ_s, error_message(kerr) ));
goto out;
}
kerr = smb_krb5_mk_error(context, KRB5KRB_AP_ERR_SKEW,
host_princ, &reply);
if (kerr) {
DEBUG(10,("make_krb5_skew_error: smb_krb5_mk_error "
"failed: Error %s\n",
error_message(kerr) ));
goto out;
}
*pblob_out = data_blob(reply.data, reply.length);
kerberos_free_data_contents(context,&reply);
ret = True;
out:
if (host_princ_s) {
SAFE_FREE(host_princ_s);
}
if (host_princ) {
krb5_free_principal(context, host_princ);
}
krb5_free_context(context);
return ret;
}
#endif
/****************************************************************************
Reply to a session setup spnego negotiate packet for kerberos.
****************************************************************************/
static void reply_spnego_kerberos(struct smb_request *req,
DATA_BLOB *secblob,
const char *mechOID,
uint16 vuid,
bool *p_invalidate_vuid)
{
TALLOC_CTX *mem_ctx;
DATA_BLOB ticket;
char *client, *p, *domain;
fstring netbios_domain_name;
struct passwd *pw;
fstring user;
int sess_vuid = req->vuid;
NTSTATUS ret = NT_STATUS_OK;
DATA_BLOB ap_rep, ap_rep_wrapped, response;
struct auth_serversupplied_info *server_info = NULL;
DATA_BLOB session_key = data_blob_null;
uint8 tok_id[2];
DATA_BLOB nullblob = data_blob_null;
fstring real_username;
bool map_domainuser_to_guest = False;
bool username_was_mapped;
struct PAC_LOGON_INFO *logon_info = NULL;
struct smbd_server_connection *sconn = req->sconn;
ZERO_STRUCT(ticket);
ZERO_STRUCT(ap_rep);
ZERO_STRUCT(ap_rep_wrapped);
ZERO_STRUCT(response);
/* Normally we will always invalidate the intermediate vuid. */
*p_invalidate_vuid = True;
mem_ctx = talloc_init("reply_spnego_kerberos");
if (mem_ctx == NULL) {
reply_nterror(req, nt_status_squash(NT_STATUS_NO_MEMORY));
return;
}
if (!spnego_parse_krb5_wrap(*secblob, &ticket, tok_id)) {
talloc_destroy(mem_ctx);
reply_nterror(req, nt_status_squash(NT_STATUS_LOGON_FAILURE));
return;
}
ret = ads_verify_ticket(mem_ctx, lp_realm(), 0, &ticket,
&client, &logon_info, &ap_rep,
&session_key, True);
data_blob_free(&ticket);
if (!NT_STATUS_IS_OK(ret)) {
#if 0
/* Experiment that failed.
* See "only happens with a KDC" comment below. */
if (NT_STATUS_EQUAL(ret, NT_STATUS_TIME_DIFFERENCE_AT_DC)) {
/*
* Windows in this case returns
* NT_STATUS_MORE_PROCESSING_REQUIRED
* with a negTokenTarg blob containing an krb5_error
* struct ASN1 encoded containing KRB5KRB_AP_ERR_SKEW.
* The client then fixes its clock and continues rather
* than giving an error. JRA.
* -- Looks like this only happens with a KDC. JRA.
*/
bool ok = make_krb5_skew_error(&ap_rep);
if (!ok) {
talloc_destroy(mem_ctx);
return ERROR_NT(nt_status_squash(
NT_STATUS_LOGON_FAILURE));
}
ap_rep_wrapped = spnego_gen_krb5_wrap(ap_rep,
TOK_ID_KRB_ERROR);
response = spnego_gen_auth_response(&ap_rep_wrapped,
ret, OID_KERBEROS5_OLD);
reply_sesssetup_blob(conn, inbuf, outbuf, response,
NT_STATUS_MORE_PROCESSING_REQUIRED);
/*
* In this one case we don't invalidate the
* intermediate vuid as we're expecting the client
* to re-use it for the next sessionsetupX packet. JRA.
*/
*p_invalidate_vuid = False;
data_blob_free(&ap_rep);
data_blob_free(&ap_rep_wrapped);
data_blob_free(&response);
talloc_destroy(mem_ctx);
return -1; /* already replied */
}
#else
if (!NT_STATUS_EQUAL(ret, NT_STATUS_TIME_DIFFERENCE_AT_DC)) {
ret = NT_STATUS_LOGON_FAILURE;
}
#endif
DEBUG(1,("Failed to verify incoming ticket with error %s!\n",
nt_errstr(ret)));
talloc_destroy(mem_ctx);
reply_nterror(req, nt_status_squash(ret));
return;
}
DEBUG(3,("Ticket name is [%s]\n", client));
p = strchr_m(client, '@');
if (!p) {
DEBUG(3,("Doesn't look like a valid principal\n"));
data_blob_free(&ap_rep);
data_blob_free(&session_key);
talloc_destroy(mem_ctx);
reply_nterror(req,nt_status_squash(NT_STATUS_LOGON_FAILURE));
return;
}
*p = 0;
/* save the PAC data if we have it */
if (logon_info) {
netsamlogon_cache_store( client, &logon_info->info3 );
}
if (!strequal(p+1, lp_realm())) {
DEBUG(3,("Ticket for foreign realm %s@%s\n", client, p+1));
if (!lp_allow_trusted_domains()) {
data_blob_free(&ap_rep);
data_blob_free(&session_key);
talloc_destroy(mem_ctx);
reply_nterror(req, nt_status_squash(
NT_STATUS_LOGON_FAILURE));
return;
}
}
/* this gives a fully qualified user name (ie. with full realm).
that leads to very long usernames, but what else can we do? */
domain = p+1;
if (logon_info && logon_info->info3.base.domain.string) {
fstrcpy(netbios_domain_name,
logon_info->info3.base.domain.string);
domain = netbios_domain_name;
DEBUG(10, ("Mapped to [%s] (using PAC)\n", domain));
} else {
/* If we have winbind running, we can (and must) shorten the
username by using the short netbios name. Otherwise we will
have inconsistent user names. With Kerberos, we get the
fully qualified realm, with ntlmssp we get the short
name. And even w2k3 does use ntlmssp if you for example
connect to an ip address. */
wbcErr wbc_status;
struct wbcDomainInfo *info = NULL;
DEBUG(10, ("Mapping [%s] to short name\n", domain));
wbc_status = wbcDomainInfo(domain, &info);
if (WBC_ERROR_IS_OK(wbc_status)) {
fstrcpy(netbios_domain_name,
info->short_name);
wbcFreeMemory(info);
domain = netbios_domain_name;
DEBUG(10, ("Mapped to [%s] (using Winbind)\n", domain));
} else {
DEBUG(3, ("Could not find short name: %s\n",
wbcErrorString(wbc_status)));
}
}
fstr_sprintf(user, "%s%c%s", domain, *lp_winbind_separator(), client);
/* lookup the passwd struct, create a new user if necessary */
username_was_mapped = map_username(user);
pw = smb_getpwnam( mem_ctx, user, real_username, True );
if (pw) {
/* if a real user check pam account restrictions */
/* only really perfomed if "obey pam restriction" is true */
/* do this before an eventual mapping to guest occurs */
ret = smb_pam_accountcheck(pw->pw_name);
if ( !NT_STATUS_IS_OK(ret)) {
DEBUG(1,("PAM account restriction "
"prevents user login\n"));
data_blob_free(&ap_rep);
data_blob_free(&session_key);
TALLOC_FREE(mem_ctx);
reply_nterror(req, nt_status_squash(ret));
return;
}
}
if (!pw) {
/* this was originally the behavior of Samba 2.2, if a user
did not have a local uid but has been authenticated, then
map them to a guest account */
if (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_UID){
map_domainuser_to_guest = True;
fstrcpy(user,lp_guestaccount());
pw = smb_getpwnam( mem_ctx, user, real_username, True );
}
/* extra sanity check that the guest account is valid */
if ( !pw ) {
DEBUG(1,("Username %s is invalid on this system\n",
user));
data_blob_free(&ap_rep);
data_blob_free(&session_key);
TALLOC_FREE(mem_ctx);
reply_nterror(req, nt_status_squash(
NT_STATUS_LOGON_FAILURE));
return;
}
}
/* setup the string used by %U */
sub_set_smb_name( real_username );
reload_services(True);
if ( map_domainuser_to_guest ) {
make_server_info_guest(NULL, &server_info);
} else if (logon_info) {
/* pass the unmapped username here since map_username()
will be called again from inside make_server_info_info3() */
ret = make_server_info_info3(mem_ctx, client, domain,
&server_info, &logon_info->info3);
if ( !NT_STATUS_IS_OK(ret) ) {
DEBUG(1,("make_server_info_info3 failed: %s!\n",
nt_errstr(ret)));
data_blob_free(&ap_rep);
data_blob_free(&session_key);
TALLOC_FREE(mem_ctx);
reply_nterror(req, nt_status_squash(ret));
return;
}
} else {
/*
* We didn't get a PAC, we have to make up the user
* ourselves. Try to ask the pdb backend to provide
* SID consistency with ntlmssp session setup
*/
struct samu *sampass;
sampass = samu_new(talloc_tos());
if (sampass == NULL) {
ret = NT_STATUS_NO_MEMORY;
data_blob_free(&ap_rep);
data_blob_free(&session_key);
TALLOC_FREE(mem_ctx);
reply_nterror(req, nt_status_squash(ret));
return;
}
if (pdb_getsampwnam(sampass, real_username)) {
DEBUG(10, ("found user %s in passdb, calling "
"make_server_info_sam\n", real_username));
ret = make_server_info_sam(&server_info, sampass);
TALLOC_FREE(sampass);
} else {
/*
* User not in passdb, make it up artificially
*/
TALLOC_FREE(sampass);
DEBUG(10, ("didn't find user %s in passdb, calling "
"make_server_info_pw\n", real_username));
ret = make_server_info_pw(&server_info, real_username,
pw);
}
if ( !NT_STATUS_IS_OK(ret) ) {
DEBUG(1,("make_server_info_[sam|pw] failed: %s!\n",
nt_errstr(ret)));
data_blob_free(&ap_rep);
data_blob_free(&session_key);
TALLOC_FREE(mem_ctx);
reply_nterror(req, nt_status_squash(ret));
return;
}
/* make_server_info_pw does not set the domain. Without this
* we end up with the local netbios name in substitutions for
* %D. */
if (server_info->info3 != NULL) {
server_info->info3->base.domain.string =
talloc_strdup(server_info->info3, domain);
}
}
server_info->nss_token |= username_was_mapped;
/* we need to build the token for the user. make_server_info_guest()
already does this */
if ( !server_info->ptok ) {
ret = create_local_token( server_info );
if ( !NT_STATUS_IS_OK(ret) ) {
DEBUG(10,("failed to create local token: %s\n",
nt_errstr(ret)));
data_blob_free(&ap_rep);
data_blob_free(&session_key);
TALLOC_FREE( mem_ctx );
TALLOC_FREE( server_info );
reply_nterror(req, nt_status_squash(ret));
return;
}
}
if (!is_partial_auth_vuid(sconn, sess_vuid)) {
sess_vuid = register_initial_vuid(sconn);
}
data_blob_free(&server_info->user_session_key);
server_info->user_session_key = session_key;
talloc_steal(server_info, session_key.data);
session_key = data_blob_null;
/* register_existing_vuid keeps the server info */
/* register_existing_vuid takes ownership of session_key on success,
* no need to free after this on success. A better interface would copy
* it.... */
sess_vuid = register_existing_vuid(sconn,
sess_vuid,
server_info,
nullblob,
client);
reply_outbuf(req, 4, 0);
SSVAL(req->outbuf,smb_uid,sess_vuid);
if (sess_vuid == UID_FIELD_INVALID ) {
ret = NT_STATUS_LOGON_FAILURE;
} else {
/* current_user_info is changed on new vuid */
reload_services( True );
SSVAL(req->outbuf, smb_vwv3, 0);
if (server_info->guest) {
SSVAL(req->outbuf,smb_vwv2,1);
}
SSVAL(req->outbuf, smb_uid, sess_vuid);
/* Successful logon. Keep this vuid. */
*p_invalidate_vuid = False;
}
/* wrap that up in a nice GSS-API wrapping */
if (NT_STATUS_IS_OK(ret)) {
ap_rep_wrapped = spnego_gen_krb5_wrap(ap_rep,
TOK_ID_KRB_AP_REP);
} else {
ap_rep_wrapped = data_blob_null;
}
response = spnego_gen_auth_response(&ap_rep_wrapped, ret,
mechOID);
reply_sesssetup_blob(req, response, ret);
data_blob_free(&ap_rep);
data_blob_free(&ap_rep_wrapped);
data_blob_free(&response);
TALLOC_FREE(mem_ctx);
}
#endif
/****************************************************************************
Send a session setup reply, wrapped in SPNEGO.
Get vuid and check first.
End the NTLMSSP exchange context if we are OK/complete fail
This should be split into two functions, one to handle each
leg of the NTLM auth steps.
***************************************************************************/
static void reply_spnego_ntlmssp(struct smb_request *req,
uint16 vuid,
struct auth_ntlmssp_state **auth_ntlmssp_state,
DATA_BLOB *ntlmssp_blob, NTSTATUS nt_status,
const char *OID,
bool wrap)
{
bool do_invalidate = true;
DATA_BLOB response;
struct auth_serversupplied_info *server_info = NULL;
struct smbd_server_connection *sconn = req->sconn;
if (NT_STATUS_IS_OK(nt_status)) {
nt_status = auth_ntlmssp_server_info(talloc_tos(), (*auth_ntlmssp_state), &server_info);
} else {
/* Note that this server_info won't have a session
* key. But for map to guest, that's exactly the right
* thing - we can't reasonably guess the key the
* client wants, as the password was wrong */
nt_status = do_map_to_guest(nt_status,
&server_info,
auth_ntlmssp_get_username(*auth_ntlmssp_state),
auth_ntlmssp_get_domain(*auth_ntlmssp_state));
}
reply_outbuf(req, 4, 0);
SSVAL(req->outbuf, smb_uid, vuid);
if (NT_STATUS_IS_OK(nt_status)) {
DATA_BLOB nullblob = data_blob_null;
if (!is_partial_auth_vuid(sconn, vuid)) {
nt_status = NT_STATUS_LOGON_FAILURE;
goto out;
}
/* register_existing_vuid keeps the server info */
if (register_existing_vuid(sconn, vuid,
server_info, nullblob,
auth_ntlmssp_get_username(*auth_ntlmssp_state)) !=
vuid) {
/* The problem is, *auth_ntlmssp_state points
* into the vuser this will have
* talloc_free()'ed in
* register_existing_vuid() */
do_invalidate = false;
nt_status = NT_STATUS_LOGON_FAILURE;
goto out;
}
/* current_user_info is changed on new vuid */
reload_services( True );
SSVAL(req->outbuf, smb_vwv3, 0);
if (server_info->guest) {
SSVAL(req->outbuf,smb_vwv2,1);
}
}
out:
if (wrap) {
response = spnego_gen_auth_response(ntlmssp_blob,
nt_status, OID);
} else {
response = *ntlmssp_blob;
}
reply_sesssetup_blob(req, response, nt_status);
if (wrap) {
data_blob_free(&response);
}
/* NT_STATUS_MORE_PROCESSING_REQUIRED from our NTLMSSP code tells us,
and the other end, that we are not finished yet. */
if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
/* NB. This is *NOT* an error case. JRA */
if (do_invalidate) {
auth_ntlmssp_end(auth_ntlmssp_state);
if (!NT_STATUS_IS_OK(nt_status)) {
/* Kill the intermediate vuid */
invalidate_vuid(sconn, vuid);
}
}
}
}
/****************************************************************************
Is this a krb5 mechanism ?
****************************************************************************/
NTSTATUS parse_spnego_mechanisms(DATA_BLOB blob_in,
DATA_BLOB *pblob_out,
char **kerb_mechOID)
{
char *OIDs[ASN1_MAX_OIDS];
int i;
NTSTATUS ret = NT_STATUS_OK;
*kerb_mechOID = NULL;
/* parse out the OIDs and the first sec blob */
if (!parse_negTokenTarg(blob_in, OIDs, pblob_out)) {
return NT_STATUS_LOGON_FAILURE;
}
/* only look at the first OID for determining the mechToken --
according to RFC2478, we should choose the one we want
and renegotiate, but i smell a client bug here..
Problem observed when connecting to a member (samba box)
of an AD domain as a user in a Samba domain. Samba member
server sent back krb5/mskrb5/ntlmssp as mechtypes, but the
client (2ksp3) replied with ntlmssp/mskrb5/krb5 and an
NTLMSSP mechtoken. --jerry */
#ifdef HAVE_KRB5
if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 ||
strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
*kerb_mechOID = SMB_STRDUP(OIDs[0]);
if (*kerb_mechOID == NULL) {
ret = NT_STATUS_NO_MEMORY;
}
}
#endif
for (i=0;OIDs[i];i++) {
DEBUG(5,("parse_spnego_mechanisms: Got OID %s\n", OIDs[i]));
talloc_free(OIDs[i]);
}
return ret;
}
/****************************************************************************
Fall back from krb5 to NTLMSSP.
****************************************************************************/
static void reply_spnego_downgrade_to_ntlmssp(struct smb_request *req,
uint16 vuid)
{
DATA_BLOB response;
reply_outbuf(req, 4, 0);
SSVAL(req->outbuf,smb_uid,vuid);
DEBUG(3,("reply_spnego_downgrade_to_ntlmssp: Got krb5 ticket in SPNEGO "
"but set to downgrade to NTLMSSP\n"));
response = spnego_gen_auth_response(NULL,
NT_STATUS_MORE_PROCESSING_REQUIRED,
OID_NTLMSSP);
reply_sesssetup_blob(req, response, NT_STATUS_MORE_PROCESSING_REQUIRED);
data_blob_free(&response);
}
/****************************************************************************
Reply to a session setup spnego negotiate packet.
****************************************************************************/
static void reply_spnego_negotiate(struct smb_request *req,
uint16 vuid,
DATA_BLOB blob1,
struct auth_ntlmssp_state **auth_ntlmssp_state)
{
DATA_BLOB secblob;
DATA_BLOB chal;
char *kerb_mech = NULL;
NTSTATUS status;
struct smbd_server_connection *sconn = req->sconn;
status = parse_spnego_mechanisms(blob1, &secblob, &kerb_mech);
if (!NT_STATUS_IS_OK(status)) {
/* Kill the intermediate vuid */
invalidate_vuid(sconn, vuid);
reply_nterror(req, nt_status_squash(status));
return;
}
DEBUG(3,("reply_spnego_negotiate: Got secblob of size %lu\n",
(unsigned long)secblob.length));
#ifdef HAVE_KRB5
if (kerb_mech && ((lp_security()==SEC_ADS) ||
USE_KERBEROS_KEYTAB) ) {
bool destroy_vuid = True;
reply_spnego_kerberos(req, &secblob, kerb_mech,
vuid, &destroy_vuid);
data_blob_free(&secblob);
if (destroy_vuid) {
/* Kill the intermediate vuid */
invalidate_vuid(sconn, vuid);
}
SAFE_FREE(kerb_mech);
return;
}
#endif
if (*auth_ntlmssp_state) {
auth_ntlmssp_end(auth_ntlmssp_state);
}
if (kerb_mech) {
data_blob_free(&secblob);
/* The mechtoken is a krb5 ticket, but
* we need to fall back to NTLM. */
reply_spnego_downgrade_to_ntlmssp(req, vuid);
SAFE_FREE(kerb_mech);
return;
}
status = auth_ntlmssp_start(auth_ntlmssp_state);
if (!NT_STATUS_IS_OK(status)) {
/* Kill the intermediate vuid */
invalidate_vuid(sconn, vuid);
reply_nterror(req, nt_status_squash(status));
return;
}
status = auth_ntlmssp_update(*auth_ntlmssp_state,
secblob, &chal);
data_blob_free(&secblob);
reply_spnego_ntlmssp(req, vuid, auth_ntlmssp_state,
&chal, status, OID_NTLMSSP, true);
data_blob_free(&chal);
/* already replied */
return;
}
/****************************************************************************
Reply to a session setup spnego auth packet.
****************************************************************************/
static void reply_spnego_auth(struct smb_request *req,
uint16 vuid,
DATA_BLOB blob1,
struct auth_ntlmssp_state **auth_ntlmssp_state)
{
DATA_BLOB auth = data_blob_null;
DATA_BLOB auth_reply = data_blob_null;
DATA_BLOB secblob = data_blob_null;
NTSTATUS status = NT_STATUS_LOGON_FAILURE;
struct smbd_server_connection *sconn = req->sconn;
if (!spnego_parse_auth(blob1, &auth)) {
#if 0
file_save("auth.dat", blob1.data, blob1.length);
#endif
/* Kill the intermediate vuid */
invalidate_vuid(sconn, vuid);
reply_nterror(req, nt_status_squash(
NT_STATUS_LOGON_FAILURE));
return;
}
if (auth.data[0] == ASN1_APPLICATION(0)) {
/* Might be a second negTokenTarg packet */
char *kerb_mech = NULL;
status = parse_spnego_mechanisms(auth, &secblob, &kerb_mech);
if (!NT_STATUS_IS_OK(status)) {
/* Kill the intermediate vuid */
invalidate_vuid(sconn, vuid);
reply_nterror(req, nt_status_squash(status));
return;
}
DEBUG(3,("reply_spnego_auth: Got secblob of size %lu\n",
(unsigned long)secblob.length));
#ifdef HAVE_KRB5
if (kerb_mech && ((lp_security()==SEC_ADS) ||
USE_KERBEROS_KEYTAB)) {
bool destroy_vuid = True;
reply_spnego_kerberos(req, &secblob, kerb_mech,
vuid, &destroy_vuid);
data_blob_free(&secblob);
data_blob_free(&auth);
if (destroy_vuid) {
/* Kill the intermediate vuid */
invalidate_vuid(sconn, vuid);
}
SAFE_FREE(kerb_mech);
return;
}
#endif
/* Can't blunder into NTLMSSP auth if we have
* a krb5 ticket. */
if (kerb_mech) {
/* Kill the intermediate vuid */
invalidate_vuid(sconn, vuid);
DEBUG(3,("reply_spnego_auth: network "
"misconfiguration, client sent us a "
"krb5 ticket and kerberos security "
"not enabled\n"));
reply_nterror(req, nt_status_squash(
NT_STATUS_LOGON_FAILURE));
SAFE_FREE(kerb_mech);
}
}
/* If we get here it wasn't a negTokenTarg auth packet. */
data_blob_free(&secblob);
if (!*auth_ntlmssp_state) {
status = auth_ntlmssp_start(auth_ntlmssp_state);
if (!NT_STATUS_IS_OK(status)) {
/* Kill the intermediate vuid */
invalidate_vuid(sconn, vuid);
reply_nterror(req, nt_status_squash(status));
return;
}
}
status = auth_ntlmssp_update(*auth_ntlmssp_state,
auth, &auth_reply);
data_blob_free(&auth);
/* Don't send the mechid as we've already sent this (RFC4178). */
reply_spnego_ntlmssp(req, vuid,
auth_ntlmssp_state,
&auth_reply, status, NULL, true);
data_blob_free(&auth_reply);
/* and tell smbd that we have already replied to this packet */
return;
}
/****************************************************************************
Delete an entry on the list.
****************************************************************************/
static void delete_partial_auth(struct smbd_server_connection *sconn,
struct pending_auth_data *pad)
{
if (!pad) {
return;
}
DLIST_REMOVE(sconn->smb1.pd_list, pad);
data_blob_free(&pad->partial_data);
SAFE_FREE(pad);
}
/****************************************************************************
Search for a partial SPNEGO auth fragment matching an smbpid.
****************************************************************************/
static struct pending_auth_data *get_pending_auth_data(
struct smbd_server_connection *sconn,
uint16_t smbpid)
{
struct pending_auth_data *pad;
/*
* NOTE: using the smbpid here is completely wrong...
* see [MS-SMB]
* 3.3.5.3 Receiving an SMB_COM_SESSION_SETUP_ANDX Request
*/
for (pad = sconn->smb1.pd_list; pad; pad = pad->next) {
if (pad->smbpid == smbpid) {
break;
}
}
return pad;
}
/****************************************************************************
Check the size of an SPNEGO blob. If we need more return
NT_STATUS_MORE_PROCESSING_REQUIRED, else return NT_STATUS_OK. Don't allow
the blob to be more than 64k.
****************************************************************************/
static NTSTATUS check_spnego_blob_complete(struct smbd_server_connection *sconn,
uint16 smbpid, uint16 vuid,
DATA_BLOB *pblob)
{
struct pending_auth_data *pad = NULL;
ASN1_DATA *data;
size_t needed_len = 0;
pad = get_pending_auth_data(sconn, smbpid);
/* Ensure we have some data. */
if (pblob->length == 0) {
/* Caller can cope. */
DEBUG(2,("check_spnego_blob_complete: zero blob length !\n"));
delete_partial_auth(sconn, pad);
return NT_STATUS_OK;
}
/* Were we waiting for more data ? */
if (pad) {
DATA_BLOB tmp_blob;
size_t copy_len = MIN(65536, pblob->length);
/* Integer wrap paranoia.... */
if (pad->partial_data.length + copy_len <
pad->partial_data.length ||
pad->partial_data.length + copy_len < copy_len) {
DEBUG(2,("check_spnego_blob_complete: integer wrap "
"pad->partial_data.length = %u, "
"copy_len = %u\n",
(unsigned int)pad->partial_data.length,
(unsigned int)copy_len ));
delete_partial_auth(sconn, pad);
return NT_STATUS_INVALID_PARAMETER;
}
DEBUG(10,("check_spnego_blob_complete: "
"pad->partial_data.length = %u, "
"pad->needed_len = %u, "
"copy_len = %u, "
"pblob->length = %u,\n",
(unsigned int)pad->partial_data.length,
(unsigned int)pad->needed_len,
(unsigned int)copy_len,
(unsigned int)pblob->length ));
tmp_blob = data_blob(NULL,
pad->partial_data.length + copy_len);
/* Concatenate the two (up to copy_len) bytes. */
memcpy(tmp_blob.data,
pad->partial_data.data,
pad->partial_data.length);
memcpy(tmp_blob.data + pad->partial_data.length,
pblob->data,
copy_len);
/* Replace the partial data. */
data_blob_free(&pad->partial_data);
pad->partial_data = tmp_blob;
ZERO_STRUCT(tmp_blob);
/* Are we done ? */
if (pblob->length >= pad->needed_len) {
/* Yes, replace pblob. */
data_blob_free(pblob);
*pblob = pad->partial_data;
ZERO_STRUCT(pad->partial_data);
delete_partial_auth(sconn, pad);
return NT_STATUS_OK;
}
/* Still need more data. */
pad->needed_len -= copy_len;
return NT_STATUS_MORE_PROCESSING_REQUIRED;
}
if ((pblob->data[0] != ASN1_APPLICATION(0)) &&
(pblob->data[0] != ASN1_CONTEXT(1))) {
/* Not something we can determine the
* length of.
*/
return NT_STATUS_OK;
}
/* This is a new SPNEGO sessionsetup - see if
* the data given in this blob is enough.
*/
data = asn1_init(NULL);
if (data == NULL) {
return NT_STATUS_NO_MEMORY;
}
asn1_load(data, *pblob);
asn1_start_tag(data, pblob->data[0]);
if (data->has_error || data->nesting == NULL) {
asn1_free(data);
/* Let caller catch. */
return NT_STATUS_OK;
}
/* Integer wrap paranoia.... */
if (data->nesting->taglen + data->nesting->start < data->nesting->taglen ||
data->nesting->taglen + data->nesting->start < data->nesting->start) {
DEBUG(2,("check_spnego_blob_complete: integer wrap "
"data.nesting->taglen = %u, "
"data.nesting->start = %u\n",
(unsigned int)data->nesting->taglen,
(unsigned int)data->nesting->start ));
asn1_free(data);
return NT_STATUS_INVALID_PARAMETER;
}
/* Total length of the needed asn1 is the tag length
* plus the current offset. */
needed_len = data->nesting->taglen + data->nesting->start;
asn1_free(data);
DEBUG(10,("check_spnego_blob_complete: needed_len = %u, "
"pblob->length = %u\n",
(unsigned int)needed_len,
(unsigned int)pblob->length ));
if (needed_len <= pblob->length) {
/* Nothing to do - blob is complete. */
return NT_STATUS_OK;
}
/* Refuse the blob if it's bigger than 64k. */
if (needed_len > 65536) {
DEBUG(2,("check_spnego_blob_complete: needed_len "
"too large (%u)\n",
(unsigned int)needed_len ));
return NT_STATUS_INVALID_PARAMETER;
}
/* We must store this blob until complete. */
if (!(pad = SMB_MALLOC_P(struct pending_auth_data))) {
return NT_STATUS_NO_MEMORY;
}
pad->needed_len = needed_len - pblob->length;
pad->partial_data = data_blob(pblob->data, pblob->length);
if (pad->partial_data.data == NULL) {
SAFE_FREE(pad);
return NT_STATUS_NO_MEMORY;
}
pad->smbpid = smbpid;
pad->vuid = vuid;
DLIST_ADD(sconn->smb1.pd_list, pad);
return NT_STATUS_MORE_PROCESSING_REQUIRED;
}
/****************************************************************************
Reply to a session setup command.
conn POINTER CAN BE NULL HERE !
****************************************************************************/
static void reply_sesssetup_and_X_spnego(struct smb_request *req)
{
const uint8 *p;
DATA_BLOB blob1;
size_t bufrem;
char *tmp;
const char *native_os;
const char *native_lanman;
const char *primary_domain;
const char *p2;
uint16 data_blob_len = SVAL(req->vwv+7, 0);
enum remote_arch_types ra_type = get_remote_arch();
int vuid = req->vuid;
user_struct *vuser = NULL;
NTSTATUS status = NT_STATUS_OK;
uint16 smbpid = req->smbpid;
struct smbd_server_connection *sconn = req->sconn;
DEBUG(3,("Doing spnego session setup\n"));
if (global_client_caps == 0) {
global_client_caps = IVAL(req->vwv+10, 0);
if (!(global_client_caps & CAP_STATUS32)) {
remove_from_common_flags2(FLAGS2_32_BIT_ERROR_CODES);
}
}
p = req->buf;
if (data_blob_len == 0) {
/* an invalid request */
reply_nterror(req, nt_status_squash(NT_STATUS_LOGON_FAILURE));
return;
}
bufrem = smbreq_bufrem(req, p);
/* pull the spnego blob */
blob1 = data_blob(p, MIN(bufrem, data_blob_len));
#if 0
file_save("negotiate.dat", blob1.data, blob1.length);
#endif
p2 = (char *)req->buf + blob1.length;
p2 += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p2,
STR_TERMINATE);
native_os = tmp ? tmp : "";
p2 += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p2,
STR_TERMINATE);
native_lanman = tmp ? tmp : "";
p2 += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p2,
STR_TERMINATE);
primary_domain = tmp ? tmp : "";
DEBUG(3,("NativeOS=[%s] NativeLanMan=[%s] PrimaryDomain=[%s]\n",
native_os, native_lanman, primary_domain));
if ( ra_type == RA_WIN2K ) {
/* Vista sets neither the OS or lanman strings */
if ( !strlen(native_os) && !strlen(native_lanman) )
set_remote_arch(RA_VISTA);
/* Windows 2003 doesn't set the native lanman string,
but does set primary domain which is a bug I think */
if ( !strlen(native_lanman) ) {
ra_lanman_string( primary_domain );
} else {
ra_lanman_string( native_lanman );
}
}
/* Did we get a valid vuid ? */
if (!is_partial_auth_vuid(sconn, vuid)) {
/* No, then try and see if this is an intermediate sessionsetup
* for a large SPNEGO packet. */
struct pending_auth_data *pad;
pad = get_pending_auth_data(sconn, smbpid);
if (pad) {
DEBUG(10,("reply_sesssetup_and_X_spnego: found "
"pending vuid %u\n",
(unsigned int)pad->vuid ));
vuid = pad->vuid;
}
}
/* Do we have a valid vuid now ? */
if (!is_partial_auth_vuid(sconn, vuid)) {
/* No, start a new authentication setup. */
vuid = register_initial_vuid(sconn);
if (vuid == UID_FIELD_INVALID) {
data_blob_free(&blob1);
reply_nterror(req, nt_status_squash(
NT_STATUS_INVALID_PARAMETER));
return;
}
}
vuser = get_partial_auth_user_struct(sconn, vuid);
/* This MUST be valid. */
if (!vuser) {
smb_panic("reply_sesssetup_and_X_spnego: invalid vuid.");
}
/* Large (greater than 4k) SPNEGO blobs are split into multiple
* sessionsetup requests as the Windows limit on the security blob
* field is 4k. Bug #4400. JRA.
*/
status = check_spnego_blob_complete(sconn, smbpid, vuid, &blob1);
if (!NT_STATUS_IS_OK(status)) {
if (!NT_STATUS_EQUAL(status,
NT_STATUS_MORE_PROCESSING_REQUIRED)) {
/* Real error - kill the intermediate vuid */
invalidate_vuid(sconn, vuid);
}
data_blob_free(&blob1);
reply_nterror(req, nt_status_squash(status));
return;
}
if (blob1.data[0] == ASN1_APPLICATION(0)) {
/* its a negTokenTarg packet */
reply_spnego_negotiate(req, vuid, blob1,
&vuser->auth_ntlmssp_state);
data_blob_free(&blob1);
return;
}
if (blob1.data[0] == ASN1_CONTEXT(1)) {
/* its a auth packet */
reply_spnego_auth(req, vuid, blob1,
&vuser->auth_ntlmssp_state);
data_blob_free(&blob1);
return;
}
if (strncmp((char *)(blob1.data), "NTLMSSP", 7) == 0) {
DATA_BLOB chal;
if (!vuser->auth_ntlmssp_state) {
status = auth_ntlmssp_start(&vuser->auth_ntlmssp_state);
if (!NT_STATUS_IS_OK(status)) {
/* Kill the intermediate vuid */
invalidate_vuid(sconn, vuid);
data_blob_free(&blob1);
reply_nterror(req, nt_status_squash(status));
return;
}
}
status = auth_ntlmssp_update(vuser->auth_ntlmssp_state,
blob1, &chal);
data_blob_free(&blob1);
reply_spnego_ntlmssp(req, vuid,
&vuser->auth_ntlmssp_state,
&chal, status, OID_NTLMSSP, false);
data_blob_free(&chal);
return;
}
/* what sort of packet is this? */
DEBUG(1,("Unknown packet in reply_sesssetup_and_X_spnego\n"));
data_blob_free(&blob1);
reply_nterror(req, nt_status_squash(NT_STATUS_LOGON_FAILURE));
}
/****************************************************************************
On new VC == 0, shutdown *all* old connections and users.
It seems that only NT4.x does this. At W2K and above (XP etc.).
a new session setup with VC==0 is ignored.
****************************************************************************/
struct shutdown_state {
const char *ip;
struct messaging_context *msg_ctx;
};
static int shutdown_other_smbds(const struct connections_key *key,
const struct connections_data *crec,
void *private_data)
{
struct shutdown_state *state = (struct shutdown_state *)private_data;
if (!process_exists(crec->pid)) {
return 0;
}
if (procid_is_me(&crec->pid)) {
return 0;
}
if (strcmp(state->ip, crec->addr) != 0) {
return 0;
}
DEBUG(0,("shutdown_other_smbds: shutting down pid %u "
"(IP %s)\n", (unsigned int)procid_to_pid(&crec->pid),
state->ip));
messaging_send(state->msg_ctx, crec->pid, MSG_SHUTDOWN,
&data_blob_null);
return 0;
}
static void setup_new_vc_session(struct messaging_context *msg_ctx)
{
DEBUG(2,("setup_new_vc_session: New VC == 0, if NT4.x "
"compatible we would close all old resources.\n"));
#if 0
conn_close_all();
invalidate_all_vuids();
#endif
if (lp_reset_on_zero_vc()) {
char addr[INET6_ADDRSTRLEN];
struct shutdown_state state;
state.ip = client_addr(get_client_fd(),addr,sizeof(addr));
state.msg_ctx = msg_ctx;
connections_forall_read(shutdown_other_smbds, &state);
}
}
/****************************************************************************
Reply to a session setup command.
****************************************************************************/
void reply_sesssetup_and_X(struct smb_request *req)
{
int sess_vuid;
int smb_bufsize;
DATA_BLOB lm_resp;
DATA_BLOB nt_resp;
DATA_BLOB plaintext_password;
char *tmp;
const char *user;
fstring sub_user; /* Sanitised username for substituion */
const char *domain;
const char *native_os;
const char *native_lanman;
const char *primary_domain;
struct auth_usersupplied_info *user_info = NULL;
struct auth_serversupplied_info *server_info = NULL;
uint16 smb_flag2 = req->flags2;
NTSTATUS nt_status;
struct smbd_server_connection *sconn = req->sconn;
bool doencrypt = sconn->smb1.negprot.encrypted_passwords;
START_PROFILE(SMBsesssetupX);
ZERO_STRUCT(lm_resp);
ZERO_STRUCT(nt_resp);
ZERO_STRUCT(plaintext_password);
DEBUG(3,("wct=%d flg2=0x%x\n", req->wct, req->flags2));
/* a SPNEGO session setup has 12 command words, whereas a normal
NT1 session setup has 13. See the cifs spec. */
if (req->wct == 12 &&
(req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
if (!sconn->smb1.negprot.spnego) {
DEBUG(0,("reply_sesssetup_and_X: Rejecting attempt "
"at SPNEGO session setup when it was not "
"negotiated.\n"));
reply_nterror(req, nt_status_squash(
NT_STATUS_LOGON_FAILURE));
END_PROFILE(SMBsesssetupX);
return;
}
if (SVAL(req->vwv+4, 0) == 0) {
setup_new_vc_session(req->sconn->msg_ctx);
}
reply_sesssetup_and_X_spnego(req);
END_PROFILE(SMBsesssetupX);
return;
}
smb_bufsize = SVAL(req->vwv+2, 0);
if (get_Protocol() < PROTOCOL_NT1) {
uint16 passlen1 = SVAL(req->vwv+7, 0);
/* Never do NT status codes with protocols before NT1 as we
* don't get client caps. */
remove_from_common_flags2(FLAGS2_32_BIT_ERROR_CODES);
if ((passlen1 > MAX_PASS_LEN) || (passlen1 > req->buflen)) {
reply_nterror(req, nt_status_squash(
NT_STATUS_INVALID_PARAMETER));
END_PROFILE(SMBsesssetupX);
return;
}
if (doencrypt) {
lm_resp = data_blob(req->buf, passlen1);
} else {
plaintext_password = data_blob(req->buf, passlen1+1);
/* Ensure null termination */
plaintext_password.data[passlen1] = 0;
}
srvstr_pull_req_talloc(talloc_tos(), req, &tmp,
req->buf + passlen1, STR_TERMINATE);
user = tmp ? tmp : "";
domain = "";
} else {
uint16 passlen1 = SVAL(req->vwv+7, 0);
uint16 passlen2 = SVAL(req->vwv+8, 0);
enum remote_arch_types ra_type = get_remote_arch();
const uint8_t *p = req->buf;
const uint8_t *save_p = req->buf;
uint16 byte_count;
if(global_client_caps == 0) {
global_client_caps = IVAL(req->vwv+11, 0);
if (!(global_client_caps & CAP_STATUS32)) {
remove_from_common_flags2(
FLAGS2_32_BIT_ERROR_CODES);
}
/* client_caps is used as final determination if
* client is NT or Win95. This is needed to return
* the correct error codes in some circumstances.
*/
if(ra_type == RA_WINNT || ra_type == RA_WIN2K ||
ra_type == RA_WIN95) {
if(!(global_client_caps & (CAP_NT_SMBS|
CAP_STATUS32))) {
set_remote_arch( RA_WIN95);
}
}
}
if (!doencrypt) {
/* both Win95 and WinNT stuff up the password
* lengths for non-encrypting systems. Uggh.
if passlen1==24 its a win95 system, and its setting
the password length incorrectly. Luckily it still
works with the default code because Win95 will null
terminate the password anyway
if passlen1>0 and passlen2>0 then maybe its a NT box
and its setting passlen2 to some random value which
really stuffs things up. we need to fix that one. */
if (passlen1 > 0 && passlen2 > 0 && passlen2 != 24 &&
passlen2 != 1) {
passlen2 = 0;
}
}
/* check for nasty tricks */
if (passlen1 > MAX_PASS_LEN
|| passlen1 > smbreq_bufrem(req, p)) {
reply_nterror(req, nt_status_squash(
NT_STATUS_INVALID_PARAMETER));
END_PROFILE(SMBsesssetupX);
return;
}
if (passlen2 > MAX_PASS_LEN
|| passlen2 > smbreq_bufrem(req, p+passlen1)) {
reply_nterror(req, nt_status_squash(
NT_STATUS_INVALID_PARAMETER));
END_PROFILE(SMBsesssetupX);
return;
}
/* Save the lanman2 password and the NT md4 password. */
if ((doencrypt) && (passlen1 != 0) && (passlen1 != 24)) {
doencrypt = False;
}
if (doencrypt) {
lm_resp = data_blob(p, passlen1);
nt_resp = data_blob(p+passlen1, passlen2);
} else if (lp_security() != SEC_SHARE) {
/*
* In share level we should ignore any passwords, so
* only read them if we're not.
*/
char *pass = NULL;
bool unic= smb_flag2 & FLAGS2_UNICODE_STRINGS;
if (unic && (passlen2 == 0) && passlen1) {
/* Only a ascii plaintext password was sent. */
(void)srvstr_pull_talloc(talloc_tos(),
req->inbuf,
req->flags2,
&pass,
req->buf,
passlen1,
STR_TERMINATE|STR_ASCII);
} else {
(void)srvstr_pull_talloc(talloc_tos(),
req->inbuf,
req->flags2,
&pass,
req->buf,
unic ? passlen2 : passlen1,
STR_TERMINATE);
}
if (!pass) {
reply_nterror(req, nt_status_squash(
NT_STATUS_INVALID_PARAMETER));
END_PROFILE(SMBsesssetupX);
return;
}
plaintext_password = data_blob(pass, strlen(pass)+1);
}
p += passlen1 + passlen2;
p += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p,
STR_TERMINATE);
user = tmp ? tmp : "";
p += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p,
STR_TERMINATE);
domain = tmp ? tmp : "";
p += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p,
STR_TERMINATE);
native_os = tmp ? tmp : "";
p += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p,
STR_TERMINATE);
native_lanman = tmp ? tmp : "";
/* not documented or decoded by Ethereal but there is one more
* string in the extra bytes which is the same as the
* PrimaryDomain when using extended security. Windows NT 4
* and 2003 use this string to store the native lanman string.
* Windows 9x does not include a string here at all so we have
* to check if we have any extra bytes left */
byte_count = SVAL(req->vwv+13, 0);
if ( PTR_DIFF(p, save_p) < byte_count) {
p += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p,
STR_TERMINATE);
primary_domain = tmp ? tmp : "";
} else {
primary_domain = talloc_strdup(talloc_tos(), "null");
}
DEBUG(3,("Domain=[%s] NativeOS=[%s] NativeLanMan=[%s] "
"PrimaryDomain=[%s]\n",
domain, native_os, native_lanman, primary_domain));
if ( ra_type == RA_WIN2K ) {
if ( strlen(native_lanman) == 0 )
ra_lanman_string( primary_domain );
else
ra_lanman_string( native_lanman );
}
}
if (SVAL(req->vwv+4, 0) == 0) {
setup_new_vc_session(req->sconn->msg_ctx);
}
DEBUG(3,("sesssetupX:name=[%s]\\[%s]@[%s]\n",
domain, user, get_remote_machine_name()));
if (*user) {
if (sconn->smb1.negprot.spnego) {
/* This has to be here, because this is a perfectly
* valid behaviour for guest logons :-( */
DEBUG(0,("reply_sesssetup_and_X: Rejecting attempt "
"at 'normal' session setup after "
"negotiating spnego.\n"));
reply_nterror(req, nt_status_squash(
NT_STATUS_LOGON_FAILURE));
END_PROFILE(SMBsesssetupX);
return;
}
fstrcpy(sub_user, user);
} else {
fstrcpy(sub_user, lp_guestaccount());
}
sub_set_smb_name(sub_user);
reload_services(True);
if (lp_security() == SEC_SHARE) {
/* In share level we should ignore any passwords */
data_blob_free(&lm_resp);
data_blob_free(&nt_resp);
data_blob_clear_free(&plaintext_password);
map_username(sub_user);
add_session_user(sconn, sub_user);
add_session_workgroup(sconn, domain);
/* Then force it to null for the benfit of the code below */
user = "";
}
if (!*user) {
nt_status = check_guest_password(&server_info);
} else if (doencrypt) {
struct auth_context *negprot_auth_context = NULL;
negprot_auth_context = sconn->smb1.negprot.auth_context;
if (!negprot_auth_context) {
DEBUG(0, ("reply_sesssetup_and_X: Attempted encrypted "
"session setup without negprot denied!\n"));
reply_nterror(req, nt_status_squash(
NT_STATUS_LOGON_FAILURE));
END_PROFILE(SMBsesssetupX);
return;
}
nt_status = make_user_info_for_reply_enc(&user_info, user,
domain,
lm_resp, nt_resp);
if (NT_STATUS_IS_OK(nt_status)) {
nt_status = negprot_auth_context->check_ntlm_password(
negprot_auth_context,
user_info,
&server_info);
}
} else {
struct auth_context *plaintext_auth_context = NULL;
nt_status = make_auth_context_subsystem(
&plaintext_auth_context);
if (NT_STATUS_IS_OK(nt_status)) {
uint8_t chal[8];
plaintext_auth_context->get_ntlm_challenge(
plaintext_auth_context, chal);
if (!make_user_info_for_reply(&user_info,
user, domain, chal,
plaintext_password)) {
nt_status = NT_STATUS_NO_MEMORY;
}
if (NT_STATUS_IS_OK(nt_status)) {
nt_status = plaintext_auth_context->check_ntlm_password(
plaintext_auth_context,
user_info,
&server_info);
(plaintext_auth_context->free)(
&plaintext_auth_context);
}
}
}
free_user_info(&user_info);
if (!NT_STATUS_IS_OK(nt_status)) {
nt_status = do_map_to_guest(nt_status, &server_info,
user, domain);
}
if (!NT_STATUS_IS_OK(nt_status)) {
data_blob_free(&nt_resp);
data_blob_free(&lm_resp);
data_blob_clear_free(&plaintext_password);
reply_nterror(req, nt_status_squash(nt_status));
END_PROFILE(SMBsesssetupX);
return;
}
/* Ensure we can't possible take a code path leading to a
* null defref. */
if (!server_info) {
reply_nterror(req, nt_status_squash(NT_STATUS_LOGON_FAILURE));
END_PROFILE(SMBsesssetupX);
return;
}
if (!server_info->ptok) {
nt_status = create_local_token(server_info);
if (!NT_STATUS_IS_OK(nt_status)) {
DEBUG(10, ("create_local_token failed: %s\n",
nt_errstr(nt_status)));
data_blob_free(&nt_resp);
data_blob_free(&lm_resp);
data_blob_clear_free(&plaintext_password);
reply_nterror(req, nt_status_squash(nt_status));
END_PROFILE(SMBsesssetupX);
return;
}
}
data_blob_clear_free(&plaintext_password);
/* it's ok - setup a reply */
reply_outbuf(req, 3, 0);
if (get_Protocol() >= PROTOCOL_NT1) {
push_signature(&req->outbuf);
/* perhaps grab OS version here?? */
}
if (server_info->guest) {
SSVAL(req->outbuf,smb_vwv2,1);
}
/* register the name and uid as being validated, so further connections
to a uid can get through without a password, on the same VC */
if (lp_security() == SEC_SHARE) {
sess_vuid = UID_FIELD_INVALID;
TALLOC_FREE(server_info);
} else {
/* Ignore the initial vuid. */
sess_vuid = register_initial_vuid(sconn);
if (sess_vuid == UID_FIELD_INVALID) {
data_blob_free(&nt_resp);
data_blob_free(&lm_resp);
reply_nterror(req, nt_status_squash(
NT_STATUS_LOGON_FAILURE));
END_PROFILE(SMBsesssetupX);
return;
}
/* register_existing_vuid keeps the server info */
sess_vuid = register_existing_vuid(sconn, sess_vuid,
server_info,
nt_resp.data ? nt_resp : lm_resp,
sub_user);
if (sess_vuid == UID_FIELD_INVALID) {
data_blob_free(&nt_resp);
data_blob_free(&lm_resp);
reply_nterror(req, nt_status_squash(
NT_STATUS_LOGON_FAILURE));
END_PROFILE(SMBsesssetupX);
return;
}
/* current_user_info is changed on new vuid */
reload_services( True );
}
data_blob_free(&nt_resp);
data_blob_free(&lm_resp);
SSVAL(req->outbuf,smb_uid,sess_vuid);
SSVAL(req->inbuf,smb_uid,sess_vuid);
req->vuid = sess_vuid;
if (!sconn->smb1.sessions.done_sesssetup) {
sconn->smb1.sessions.max_send =
MIN(sconn->smb1.sessions.max_send,smb_bufsize);
}
sconn->smb1.sessions.done_sesssetup = true;
END_PROFILE(SMBsesssetupX);
chain_reply(req);
return;
}
|