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
|
/*
* Copyright (c) 2000, Boris Popov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Boris Popov.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: ctx.c,v 1.32.70.2 2005/06/02 00:55:40 lindak Exp $
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/byteorder.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <libintl.h>
#include <assert.h>
#include <nss_dbdefs.h>
#include <kerberosv5/krb5.h>
#include <kerberosv5/com_err.h>
#include <netsmb/smb_lib.h>
#include <netsmb/netbios.h>
#include <netsmb/nb_lib.h>
#include <netsmb/smb_dev.h>
#include <cflib.h>
#include <charsets.h>
#include <spnego.h>
#include "derparse.h"
#include "private.h"
extern MECH_OID g_stcMechOIDList [];
#define POWEROF2(x) (((x) & ((x)-1)) == 0)
/* These two may be set by commands. */
int smb_debug, smb_verbose;
/*
* Give the RPC library a callback hook that will be
* called whenever we destroy or reinit an smb_ctx_t.
* The name rpc_cleanup_smbctx() is legacy, and was
* originally a direct call into the RPC code.
*/
static smb_ctx_close_hook_t close_hook;
static void
rpc_cleanup_smbctx(struct smb_ctx *ctx)
{
if (close_hook)
(*close_hook)(ctx);
}
void
smb_ctx_set_close_hook(smb_ctx_close_hook_t hook)
{
close_hook = hook;
}
void
dump_ctx_flags(int flags)
{
printf(" Flags: ");
if (flags == 0)
printf("0");
if (flags & SMBCF_NOPWD)
printf("NOPWD ");
if (flags & SMBCF_SRIGHTS)
printf("SRIGHTS ");
if (flags & SMBCF_LOCALE)
printf("LOCALE ");
if (flags & SMBCF_CMD_DOM)
printf("CMD_DOM ");
if (flags & SMBCF_CMD_USR)
printf("CMD_USR ");
if (flags & SMBCF_CMD_PW)
printf("CMD_PW ");
if (flags & SMBCF_RESOLVED)
printf("RESOLVED ");
if (flags & SMBCF_KCBAD)
printf("KCBAD ");
if (flags & SMBCF_KCFOUND)
printf("KCFOUND ");
if (flags & SMBCF_BROWSEOK)
printf("BROWSEOK ");
if (flags & SMBCF_AUTHREQ)
printf("AUTHREQ ");
if (flags & SMBCF_KCSAVE)
printf("KCSAVE ");
if (flags & SMBCF_XXX)
printf("XXX ");
if (flags & SMBCF_SSNACTIVE)
printf("SSNACTIVE ");
if (flags & SMBCF_KCDOMAIN)
printf("KCDOMAIN ");
printf("\n");
}
void
dump_ctx_ssn(struct smbioc_ossn *ssn)
{
printf(" srvname=\"%s\", dom=\"%s\", user=\"%s\", password=%s\n",
ssn->ioc_srvname, ssn->ioc_workgroup, ssn->ioc_user,
ssn->ioc_password[0] ? "(non-null)" : "NULL");
printf(" timeout=%d, retry=%d, owner=%d, group=%d\n",
ssn->ioc_timeout, ssn->ioc_retrycount,
ssn->ioc_owner, ssn->ioc_group);
}
void
dump_ctx_sh(struct smbioc_oshare *sh)
{
printf(" share_name=\"%s\", share_pw=\"%s\"\n",
sh->ioc_share, sh->ioc_password);
}
void
dump_ctx(char *where, struct smb_ctx *ctx)
{
printf("context %s:\n", where);
dump_ctx_flags(ctx->ct_flags);
printf(" localname=\"%s\"", ctx->ct_locname);
if (ctx->ct_fullserver)
printf(" fullserver=\"%s\"", ctx->ct_fullserver);
else
printf(" fullserver=NULL");
if (ctx->ct_srvaddr)
printf(" srvaddr=\"%s\"\n", ctx->ct_srvaddr);
else
printf(" srvaddr=NULL\n");
dump_ctx_ssn(&ctx->ct_ssn);
dump_ctx_sh(&ctx->ct_sh);
}
/*
* Initialize an smb_ctx struct.
*
* The sequence for getting all the members filled in
* has some tricky aspects. Here's how it works:
*
* The search order for options is as follows:
* command line options
* values parsed from UNC path (cmd)
* values from RC file (per-user)
* values from SMF (system-wide)
* built-in defaults
*
* Normally, one would simply get all the values starting with
* the bottom of the above list and working to the top, and
* overwriting values as you go. But we need an exception.
*
* In this function, we parse the UNC path and command line options,
* because we need (at least) the server name when we're getting the
* SMF and RC file values. However, values we get from the command
* should not be overwritten by SMF or RC file parsing, so we mark
* values from the command as "from CMD" and the RC file parser
* leaves in place any values so marked. See: SMBCF_CMD_*
*
* The semantics of these flags are: "This value came from the
* current command instance, not from sources that may apply to
* multiple commands." (Different from the old "FROMUSR" flag.)
*
* Note that smb_ctx_opt() is called later to handle the
* remaining options, which should be ignored here.
* The (magic) leading ":" in cf_getopt() makes it
* ignore options not in the options string.
*/
int
smb_ctx_init(struct smb_ctx *ctx, int argc, char *argv[],
int minlevel, int maxlevel, int sharetype)
{
int opt, error = 0;
const char *arg, *cp;
struct passwd pw;
char pwbuf[NSS_BUFLEN_PASSWD];
int aflg = 0, uflg = 0;
bzero(ctx, sizeof (*ctx));
if (sharetype == SMB_ST_DISK)
ctx->ct_flags |= SMBCF_BROWSEOK;
error = nb_ctx_create(&ctx->ct_nb);
if (error)
return (error);
ctx->ct_fd = -1;
ctx->ct_parsedlevel = SMBL_NONE;
ctx->ct_minlevel = minlevel;
ctx->ct_maxlevel = maxlevel;
/* Fill in defaults */
ctx->ct_ssn.ioc_opt = SMBVOPT_CREATE | SMBVOPT_MINAUTH_NTLM;
ctx->ct_ssn.ioc_timeout = 15;
ctx->ct_ssn.ioc_retrycount = 4;
ctx->ct_ssn.ioc_owner = SMBM_ANY_OWNER;
ctx->ct_ssn.ioc_group = SMBM_ANY_GROUP;
ctx->ct_ssn.ioc_mode = SMBM_EXEC;
ctx->ct_ssn.ioc_rights = SMBM_DEFAULT;
ctx->ct_sh.ioc_opt = SMBVOPT_CREATE;
ctx->ct_sh.ioc_owner = SMBM_ANY_OWNER;
ctx->ct_sh.ioc_group = SMBM_ANY_GROUP;
ctx->ct_sh.ioc_mode = SMBM_EXEC;
ctx->ct_sh.ioc_rights = SMBM_DEFAULT;
ctx->ct_sh.ioc_owner = SMBM_ANY_OWNER;
ctx->ct_sh.ioc_group = SMBM_ANY_GROUP;
nb_ctx_setscope(ctx->ct_nb, "");
/*
* if the user name is not specified some other way,
* use the current user name (built-in default)
*/
if (getpwuid_r(geteuid(), &pw, pwbuf, sizeof (pwbuf)) != NULL)
smb_ctx_setuser(ctx, pw.pw_name, 0);
/*
* Set a built-in default domain (workgroup).
* XXX: What's the best default? Use "?" instead?
* Using the Windows/NT default for now.
*/
smb_ctx_setworkgroup(ctx, "WORKGROUP", 0);
/*
* Parse the UNC path. Values from here are
* marked as "from CMD".
*/
if (argv == NULL)
goto done;
for (opt = 1; opt < argc; opt++) {
cp = argv[opt];
if (strncmp(cp, "//", 2) != 0)
continue;
error = smb_ctx_parseunc(ctx, cp, sharetype, &cp);
if (error)
return (error);
break;
}
/*
* Parse options, if any. Values from here too
* are marked as "from CMD".
*/
while (error == 0 && (opt = cf_getopt(argc, argv, ":AU:E:L:")) != -1) {
arg = cf_optarg;
switch (opt) {
case 'A':
aflg = 1;
error = smb_ctx_setuser(ctx, "", TRUE);
error = smb_ctx_setpassword(ctx, "", TRUE);
ctx->ct_flags |= SMBCF_NOPWD;
break;
case 'E':
#if 0 /* We don't support any "charset" stuff. (ignore -E) */
error = smb_ctx_setcharset(ctx, arg);
if (error)
return (error);
#endif
break;
case 'L':
#if 0 /* Use the standard environment variables (ignore -L) */
error = nls_setlocale(optarg);
if (error)
break;
#endif
break;
case 'U':
uflg = 1;
error = smb_ctx_setuser(ctx, arg, TRUE);
break;
}
}
if (aflg && uflg) {
printf(gettext("-A and -U flags are exclusive.\n"));
return (1);
}
cf_optind = cf_optreset = 1;
done:
if (smb_debug)
dump_ctx("after smb_ctx_init", ctx);
return (error);
}
void
smb_ctx_done(struct smb_ctx *ctx)
{
rpc_cleanup_smbctx(ctx);
/* Kerberos stuff. See smb_ctx_krb5init() */
if (ctx->ct_krb5ctx) {
if (ctx->ct_krb5cp)
krb5_free_principal(ctx->ct_krb5ctx, ctx->ct_krb5cp);
krb5_free_context(ctx->ct_krb5ctx);
}
if (ctx->ct_fd != -1)
close(ctx->ct_fd);
#if 0 /* XXX: not pointers anymore */
if (&ctx->ct_ssn.ioc_server)
nb_snbfree(&ctx->ct_ssn.ioc_server);
if (&ctx->ct_ssn.ioc_local)
nb_snbfree(&ctx->ct_ssn.ioc_local);
#endif
if (ctx->ct_srvaddr)
free(ctx->ct_srvaddr);
if (ctx->ct_nb)
nb_ctx_done(ctx->ct_nb);
if (ctx->ct_secblob)
free(ctx->ct_secblob);
if (ctx->ct_origshare)
free(ctx->ct_origshare);
if (ctx->ct_fullserver)
free(ctx->ct_fullserver);
}
static int
getsubstring(const char *p, uchar_t sep, char *dest, int maxlen,
const char **next)
{
int len;
maxlen--;
for (len = 0; len < maxlen && *p != sep; p++, len++, dest++) {
if (*p == 0)
return (EINVAL);
*dest = *p;
}
*dest = 0;
*next = *p ? p + 1 : p;
return (0);
}
/*
* Parse the UNC path. Here we expect something like
* "//[workgroup;][user[:password]@]host[/share[/path]]"
* See http://ietf.org/internet-drafts/draft-crhertel-smb-url-07.txt
* Values found here are marked as "from CMD".
*/
int
smb_ctx_parseunc(struct smb_ctx *ctx, const char *unc, int sharetype,
const char **next)
{
const char *p = unc;
char *p1, *colon, *servername;
char tmp[1024];
char tmp2[1024];
int error;
ctx->ct_parsedlevel = SMBL_NONE;
if (*p++ != '/' || *p++ != '/') {
smb_error(dgettext(TEXT_DOMAIN,
"UNC should start with '//'"), 0);
return (EINVAL);
}
p1 = tmp;
error = getsubstring(p, ';', p1, sizeof (tmp), &p);
if (!error) {
if (*p1 == 0) {
smb_error(dgettext(TEXT_DOMAIN,
"empty workgroup name"), 0);
return (EINVAL);
}
nls_str_upper(tmp, tmp);
error = smb_ctx_setworkgroup(ctx, unpercent(tmp), TRUE);
if (error)
return (error);
}
colon = (char *)p;
error = getsubstring(p, '@', p1, sizeof (tmp), &p);
if (!error) {
if (ctx->ct_maxlevel < SMBL_VC) {
smb_error(dgettext(TEXT_DOMAIN,
"no user name required"), 0);
return (EINVAL);
}
p1 = strchr(tmp, ':');
if (p1) {
colon += p1 - tmp;
*p1++ = (char)0;
error = smb_ctx_setpassword(ctx, unpercent(p1), TRUE);
if (error)
return (error);
if (p - colon > 2)
memset(colon+1, '*', p - colon - 2);
}
p1 = tmp;
if (*p1 == 0) {
smb_error(dgettext(TEXT_DOMAIN,
"empty user name"), 0);
return (EINVAL);
}
error = smb_ctx_setuser(ctx, unpercent(tmp), TRUE);
if (error)
return (error);
ctx->ct_parsedlevel = SMBL_VC;
}
error = getsubstring(p, '/', p1, sizeof (tmp), &p);
if (error) {
error = getsubstring(p, '\0', p1, sizeof (tmp), &p);
if (error) {
smb_error(dgettext(TEXT_DOMAIN,
"no server name found"), 0);
return (error);
}
}
if (*p1 == 0) {
smb_error(dgettext(TEXT_DOMAIN, "empty server name"), 0);
return (EINVAL);
}
/*
* It's safe to uppercase this string, which
* consists of ascii characters that should
* be uppercased, %s, and ascii characters representing
* hex digits 0-9 and A-F (already uppercased, and
* if not uppercased they need to be). However,
* it is NOT safe to uppercase after it has been
* converted, below!
*/
nls_str_upper(tmp2, tmp);
/*
* scan for % in the string.
* If we find one, convert
* to the assumed codepage.
*/
if (strchr(tmp2, '%')) {
/* use the 1st buffer, we don't need the old string */
servername = tmp;
if (!(servername = convert_utf8_to_wincs(unpercent(tmp2)))) {
smb_error(dgettext(TEXT_DOMAIN, "bad server name"), 0);
return (EINVAL);
}
/*
* Converts utf8 to win equivalent of
* what is configured on this machine.
* Note that we are assuming this is the
* encoding used on the server, and that
* assumption might be incorrect. This is
* the best we can do now, and we should
* move to use port 445 to avoid having
* to worry about server codepages.
*/
} else /* no conversion needed */
servername = tmp2;
smb_ctx_setserver(ctx, servername);
error = smb_ctx_setfullserver(ctx, servername);
if (error)
return (error);
if (sharetype == SMB_ST_NONE) {
*next = p;
return (0);
}
if (*p != 0 && ctx->ct_maxlevel < SMBL_SHARE) {
smb_error(dgettext(TEXT_DOMAIN, "no share name required"), 0);
return (EINVAL);
}
error = getsubstring(p, '/', p1, sizeof (tmp), &p);
if (error) {
error = getsubstring(p, '\0', p1, sizeof (tmp), &p);
if (error) {
smb_error(dgettext(TEXT_DOMAIN,
"unexpected end of line"), 0);
return (error);
}
}
if (*p1 == 0 && ctx->ct_minlevel >= SMBL_SHARE &&
!(ctx->ct_flags & SMBCF_BROWSEOK)) {
smb_error(dgettext(TEXT_DOMAIN, "empty share name"), 0);
return (EINVAL);
}
*next = p;
if (*p1 == 0)
return (0);
error = smb_ctx_setshare(ctx, unpercent(p1), sharetype);
return (error);
}
int
smb_ctx_setcharset(struct smb_ctx *ctx, const char *arg)
{
char *cp, *servercs, *localcs;
int cslen = sizeof (ctx->ct_ssn.ioc_localcs);
int scslen, lcslen, error;
cp = strchr(arg, ':');
lcslen = cp ? (cp - arg) : 0;
if (lcslen == 0 || lcslen >= cslen) {
smb_error(dgettext(TEXT_DOMAIN,
"invalid local charset specification (%s)"), 0, arg);
return (EINVAL);
}
scslen = (size_t)strlen(++cp);
if (scslen == 0 || scslen >= cslen) {
smb_error(dgettext(TEXT_DOMAIN,
"invalid server charset specification (%s)"), 0, arg);
return (EINVAL);
}
localcs = memcpy(ctx->ct_ssn.ioc_localcs, arg, lcslen);
localcs[lcslen] = 0;
servercs = strcpy(ctx->ct_ssn.ioc_servercs, cp);
error = nls_setrecode(localcs, servercs);
if (error == 0)
return (0);
smb_error(dgettext(TEXT_DOMAIN,
"can't initialize iconv support (%s:%s)"),
error, localcs, servercs);
localcs[0] = 0;
servercs[0] = 0;
return (error);
}
int
smb_ctx_setfullserver(struct smb_ctx *ctx, const char *name)
{
ctx->ct_fullserver = strdup(name);
if (ctx->ct_fullserver == NULL)
return (ENOMEM);
return (0);
}
/*
* XXX TODO FIXME etc etc
* If the call to nbns_getnodestatus(...) fails we can try one of two other
* methods; use a name of "*SMBSERVER", which is supported by Samba (at least)
* or, as a last resort, try the "truncate-at-dot" heuristic.
* And the heuristic really should attempt truncation at
* each dot in turn, left to right.
*
* These fallback heuristics should be triggered when the attempt to open the
* session fails instead of in the code below.
*
* See http://ietf.org/internet-drafts/draft-crhertel-smb-url-07.txt
*/
int
smb_ctx_getnbname(struct smb_ctx *ctx, struct sockaddr *sap)
{
char server[SMB_MAXSRVNAMELEN + 1];
char workgroup[SMB_MAXUSERNAMELEN + 1];
int error;
#if 0
char *dot;
#endif
server[0] = workgroup[0] = '\0';
error = nbns_getnodestatus(sap, ctx->ct_nb, server, workgroup);
if (error == 0) {
/*
* Used to set our domain name to be the same as
* the server's domain name. Unnecessary at best,
* and wrong for accounts in a trusted domain.
*/
#ifdef APPLE
if (workgroup[0] && !ctx->ct_ssn.ioc_workgroup[0])
smb_ctx_setworkgroup(ctx, workgroup, 0);
#endif
if (server[0])
smb_ctx_setserver(ctx, server);
} else {
if (smb_verbose)
smb_error(dgettext(TEXT_DOMAIN,
"Failed to get NetBIOS node status."), 0);
if (ctx->ct_ssn.ioc_srvname[0] == (char)0)
smb_ctx_setserver(ctx, "*SMBSERVER");
}
#if 0
if (server[0] == (char)0) {
dot = strchr(ctx->ct_fullserver, '.');
if (dot)
*dot = '\0';
if (strlen(ctx->ct_fullserver) <= SMB_MAXSRVNAMELEN) {
/*
* don't uppercase the server name. it comes from
* NBNS and uppercasing can clobber the characters
*/
strcpy(ctx->ct_ssn.ioc_srvname, ctx->ct_fullserver);
error = 0;
} else {
error = -1;
}
if (dot)
*dot = '.';
}
#endif
return (error);
}
/* this routine does not uppercase the server name */
void
smb_ctx_setserver(struct smb_ctx *ctx, const char *name)
{
/* don't uppercase the server name */
if (strlen(name) > SMB_MAXSRVNAMELEN) { /* NB limit is 15 */
ctx->ct_ssn.ioc_srvname[0] = '\0';
} else
strcpy(ctx->ct_ssn.ioc_srvname, name);
}
int
smb_ctx_setuser(struct smb_ctx *ctx, const char *name, int from_cmd)
{
if (strlen(name) >= SMB_MAXUSERNAMELEN) {
smb_error(dgettext(TEXT_DOMAIN,
"user name '%s' too long"), 0, name);
return (ENAMETOOLONG);
}
/*
* Don't overwrite a value from the command line
* with one from anywhere else.
*/
if (!from_cmd && (ctx->ct_flags & SMBCF_CMD_USR))
return (0);
/* don't uppercase the username, just copy it. */
strcpy(ctx->ct_ssn.ioc_user, name);
/* Mark this as "from the command line". */
if (from_cmd)
ctx->ct_flags |= SMBCF_CMD_USR;
return (0);
}
/*
* Never uppercase the workgroup
* name here, because it might come
* from a Windows codepage encoding.
*
* Don't overwrite a domain name from the
* command line with one from anywhere else.
* See smb_ctx_init() for notes about this.
*/
int
smb_ctx_setworkgroup(struct smb_ctx *ctx, const char *name, int from_cmd)
{
if (strlen(name) >= SMB_MAXUSERNAMELEN) {
smb_error(dgettext(TEXT_DOMAIN,
"workgroup name '%s' too long"), 0, name);
return (ENAMETOOLONG);
}
/*
* Don't overwrite a value from the command line
* with one from anywhere else.
*/
if (!from_cmd && (ctx->ct_flags & SMBCF_CMD_DOM))
return (0);
strcpy(ctx->ct_ssn.ioc_workgroup, name);
/* Mark this as "from the command line". */
if (from_cmd)
ctx->ct_flags |= SMBCF_CMD_DOM;
return (0);
}
int
smb_ctx_setpassword(struct smb_ctx *ctx, const char *passwd, int from_cmd)
{
if (passwd == NULL) /* XXX Huh? */
return (EINVAL);
if (strlen(passwd) >= SMB_MAXPASSWORDLEN) {
smb_error(dgettext(TEXT_DOMAIN, "password too long"), 0);
return (ENAMETOOLONG);
}
/*
* Don't overwrite a value from the command line
* with one from anywhere else.
*/
if (!from_cmd && (ctx->ct_flags & SMBCF_CMD_PW))
return (0);
if (strncmp(passwd, "$$1", 3) == 0)
smb_simpledecrypt(ctx->ct_ssn.ioc_password, passwd);
else
strcpy(ctx->ct_ssn.ioc_password, passwd);
strcpy(ctx->ct_sh.ioc_password, ctx->ct_ssn.ioc_password);
/* Mark this as "from the command line". */
if (from_cmd)
ctx->ct_flags |= SMBCF_CMD_PW;
return (0);
}
int
smb_ctx_setshare(struct smb_ctx *ctx, const char *share, int stype)
{
if (strlen(share) >= SMB_MAXSHARENAMELEN) {
smb_error(dgettext(TEXT_DOMAIN,
"share name '%s' too long"), 0, share);
return (ENAMETOOLONG);
}
if (ctx->ct_origshare)
free(ctx->ct_origshare);
if ((ctx->ct_origshare = strdup(share)) == NULL)
return (ENOMEM);
nls_str_upper(ctx->ct_sh.ioc_share, share);
if (share[0] != 0)
ctx->ct_parsedlevel = SMBL_SHARE;
ctx->ct_sh.ioc_stype = stype;
return (0);
}
int
smb_ctx_setsrvaddr(struct smb_ctx *ctx, const char *addr)
{
if (addr == NULL || addr[0] == 0)
return (EINVAL);
if (ctx->ct_srvaddr)
free(ctx->ct_srvaddr);
if ((ctx->ct_srvaddr = strdup(addr)) == NULL)
return (ENOMEM);
return (0);
}
static int
smb_parse_owner(char *pair, uid_t *uid, gid_t *gid)
{
struct group gr;
struct passwd pw;
char buf[NSS_BUFLEN_PASSWD];
char *cp;
cp = strchr(pair, ':');
if (cp) {
*cp++ = '\0';
if (*cp) {
if (getgrnam_r(cp, &gr, buf, sizeof (buf)) != NULL) {
*gid = gr.gr_gid;
} else
smb_error(dgettext(TEXT_DOMAIN,
"Invalid group name %s, ignored"), 0, cp);
}
}
if (*pair) {
if (getpwnam_r(pair, &pw, buf, sizeof (buf)) != NULL) {
*uid = pw.pw_uid;
} else
smb_error(dgettext(TEXT_DOMAIN,
"Invalid user name %s, ignored"), 0, pair);
}
return (0);
}
/*
* Commands use this with getopt. See:
* STDPARAM_OPT, STDPARAM_ARGS
* Called after smb_ctx_readrc().
*/
int
smb_ctx_opt(struct smb_ctx *ctx, int opt, const char *arg)
{
int error = 0;
char *p, *cp;
char tmp[1024];
switch (opt) {
case 'A':
case 'U':
/* Handled in smb_ctx_init() */
break;
case 'I':
error = smb_ctx_setsrvaddr(ctx, arg);
break;
case 'M':
ctx->ct_ssn.ioc_rights = strtol(arg, &cp, 8);
if (*cp == '/') {
ctx->ct_sh.ioc_rights = strtol(cp + 1, &cp, 8);
ctx->ct_flags |= SMBCF_SRIGHTS;
}
break;
case 'N':
ctx->ct_flags |= SMBCF_NOPWD;
break;
case 'O':
p = strdup(arg);
cp = strchr(p, '/');
if (cp) {
*cp++ = '\0';
error = smb_parse_owner(cp, &ctx->ct_sh.ioc_owner,
&ctx->ct_sh.ioc_group);
}
if (*p && error == 0) {
error = smb_parse_owner(cp, &ctx->ct_ssn.ioc_owner,
&ctx->ct_ssn.ioc_group);
}
free(p);
break;
case 'P':
/* ctx->ct_ssn.ioc_opt |= SMBCOPT_PERMANENT; */
break;
case 'R':
ctx->ct_ssn.ioc_retrycount = atoi(arg);
break;
case 'T':
ctx->ct_ssn.ioc_timeout = atoi(arg);
break;
case 'W':
nls_str_upper(tmp, arg);
error = smb_ctx_setworkgroup(ctx, tmp, TRUE);
break;
}
return (error);
}
#if 0
static void
smb_hexdump(const uchar_t *buf, int len) {
int ofs = 0;
while (len--) {
if (ofs % 16 == 0)
printf("\n%02X: ", ofs);
printf("%02x ", *buf++);
ofs++;
}
printf("\n");
}
#endif
static int
smb_addiconvtbl(const char *to, const char *from, const uchar_t *tbl)
{
int error;
/*
* Not able to find out what is the work of this routine till
* now. Still investigating.
* REVISIT
*/
#ifdef KICONV_SUPPORT
error = kiconv_add_xlat_table(to, from, tbl);
if (error && error != EEXIST) {
smb_error(dgettext(TEXT_DOMAIN,
"can not setup kernel iconv table (%s:%s)"),
error, from, to);
return (error);
}
#endif
return (0);
}
/*
* Verify context before connect operation(s),
* lookup specified server and try to fill all forgotten fields.
*/
int
smb_ctx_resolve(struct smb_ctx *ctx)
{
struct smbioc_ossn *ssn = &ctx->ct_ssn;
struct smbioc_oshare *sh = &ctx->ct_sh;
struct nb_name nn;
struct sockaddr *sap;
struct sockaddr_nb *salocal, *saserver;
char *cp;
uchar_t cstbl[256];
uint_t i;
int error = 0;
int browseok = ctx->ct_flags & SMBCF_BROWSEOK;
int renego = 0;
ctx->ct_flags &= ~SMBCF_RESOLVED;
if (isatty(STDIN_FILENO))
browseok = 0;
if (ctx->ct_fullserver == NULL || ctx->ct_fullserver[0] == 0) {
smb_error(dgettext(TEXT_DOMAIN,
"no server name specified"), 0);
return (EINVAL);
}
if (ctx->ct_minlevel >= SMBL_SHARE && sh->ioc_share[0] == 0 &&
!browseok) {
smb_error(dgettext(TEXT_DOMAIN,
"no share name specified for %s@%s"),
0, ssn->ioc_user, ssn->ioc_srvname);
return (EINVAL);
}
error = nb_ctx_resolve(ctx->ct_nb);
if (error)
return (error);
if (ssn->ioc_localcs[0] == 0)
strcpy(ssn->ioc_localcs, "default"); /* XXX: locale name ? */
error = smb_addiconvtbl("tolower", ssn->ioc_localcs, nls_lower);
if (error)
return (error);
error = smb_addiconvtbl("toupper", ssn->ioc_localcs, nls_upper);
if (error)
return (error);
if (ssn->ioc_servercs[0] != 0) {
for (i = 0; i < sizeof (cstbl); i++)
cstbl[i] = i;
nls_mem_toext(cstbl, cstbl, sizeof (cstbl));
error = smb_addiconvtbl(ssn->ioc_servercs, ssn->ioc_localcs,
cstbl);
if (error)
return (error);
for (i = 0; i < sizeof (cstbl); i++)
cstbl[i] = i;
nls_mem_toloc(cstbl, cstbl, sizeof (cstbl));
error = smb_addiconvtbl(ssn->ioc_localcs, ssn->ioc_servercs,
cstbl);
if (error)
return (error);
}
/*
* If we have an explicit address set for the server in
* an "addr=X" setting in .nsmbrc or SMF, just try using a
* gethostbyname() lookup for it.
*/
if (ctx->ct_srvaddr) {
error = nb_resolvehost_in(ctx->ct_srvaddr, &sap);
if (error == 0)
(void) smb_ctx_getnbname(ctx, sap);
} else
error = -1;
/*
* Next try a gethostbyname() lookup on the original user-
* specified server name. This is similar to Windows
* NBT option "Use DNS for name resolution."
*/
if (error && ctx->ct_fullserver) {
error = nb_resolvehost_in(ctx->ct_fullserver, &sap);
if (error == 0)
(void) smb_ctx_getnbname(ctx, sap);
}
/*
* Finally, try the shorter, upper-cased ssn->ioc_srvname
* with a NBNS/WINS lookup if the "nbns_enable" property is
* true (the default). nbns_resolvename() may unicast to the
* "nbns" server or broadcast on the subnet.
*/
if (error && ssn->ioc_srvname[0] &&
ctx->ct_nb->nb_flags & NBCF_NS_ENABLE) {
error = nbns_resolvename(ssn->ioc_srvname,
ctx->ct_nb, &sap);
/*
* Used to get the NetBIOS node status here.
* Not necessary (we have the NetBIOS name).
*/
}
if (error) {
smb_error(dgettext(TEXT_DOMAIN,
"can't get server address"), error);
return (error);
}
/* XXX: no nls_str_upper(ssn->ioc_srvname) here? */
assert(sizeof (nn.nn_name) == sizeof (ssn->ioc_srvname));
memcpy(nn.nn_name, ssn->ioc_srvname, NB_NAMELEN);
nn.nn_type = NBT_SERVER;
nn.nn_scope = ctx->ct_nb->nb_scope;
error = nb_sockaddr(sap, &nn, &saserver);
memcpy(&ctx->ct_srvinaddr, sap, sizeof (struct sockaddr_in));
nb_snbfree(sap);
if (error) {
smb_error(dgettext(TEXT_DOMAIN,
"can't allocate server address"), error);
return (error);
}
/* We know it's a NetBIOS address here. */
bcopy(saserver, &ssn->ioc_server.nb,
sizeof (struct sockaddr_nb));
if (ctx->ct_locname[0] == 0) {
error = nb_getlocalname(ctx->ct_locname,
SMB_MAXUSERNAMELEN + 1);
if (error) {
smb_error(dgettext(TEXT_DOMAIN,
"can't get local name"), error);
return (error);
}
nls_str_upper(ctx->ct_locname, ctx->ct_locname);
}
/* XXX: no nls_str_upper(ctx->ct_locname); here? */
memcpy(nn.nn_name, ctx->ct_locname, NB_NAMELEN);
nn.nn_type = NBT_WKSTA;
nn.nn_scope = ctx->ct_nb->nb_scope;
error = nb_sockaddr(NULL, &nn, &salocal);
if (error) {
nb_snbfree((struct sockaddr *)saserver);
smb_error(dgettext(TEXT_DOMAIN,
"can't allocate local address"), error);
return (error);
}
/* We know it's a NetBIOS address here. */
bcopy(salocal, &ssn->ioc_local.nb,
sizeof (struct sockaddr_nb));
error = smb_ctx_findvc(ctx, SMBL_VC, 0);
if (error == 0) {
/* re-use and existing VC */
ctx->ct_flags |= SMBCF_RESOLVED | SMBCF_SSNACTIVE;
return (0);
}
/* Make a new connection via smb_ctx_negotiate()... */
error = smb_ctx_negotiate(ctx, SMBL_SHARE, SMBLK_CREATE,
ssn->ioc_workgroup);
if (error)
return (error);
ctx->ct_flags &= ~SMBCF_AUTHREQ;
if (!ctx->ct_secblob && browseok && !sh->ioc_share[0] &&
!(ctx->ct_flags & SMBCF_XXX)) {
/* assert: anon share list is subset of overall server shares */
error = smb_browse(ctx, 1);
if (error) /* user cancel or other error? */
return (error);
/*
* A share was selected, authenticate button was pressed,
* or anon-authentication failed getting browse list.
*/
}
if ((ctx->ct_secblob == NULL) && (ctx->ct_flags & SMBCF_AUTHREQ ||
(ssn->ioc_password[0] == '\0' &&
!(ctx->ct_flags & SMBCF_NOPWD)))) {
reauth:
/*
* This function is implemented in both
* ui-apple.c and ui-sun.c so let's try to
* keep the same interface. Not sure why
* they didn't just pass ssn here.
*/
error = smb_get_authentication(
ssn->ioc_workgroup, sizeof (ssn->ioc_workgroup) - 1,
ssn->ioc_user, sizeof (ssn->ioc_user) - 1,
ssn->ioc_password, sizeof (ssn->ioc_password) - 1,
ssn->ioc_srvname, ctx);
if (error)
return (error);
}
/*
* if we have a session it is either anonymous
* or from a stale authentication. re-negotiating
* gets us ready for a fresh session
*/
if (ctx->ct_flags & SMBCF_SSNACTIVE || renego) {
renego = 0;
/* don't clobber workgroup name, pass null arg */
error = smb_ctx_negotiate(ctx, SMBL_SHARE, SMBLK_CREATE, NULL);
if (error)
return (error);
}
if (browseok && !sh->ioc_share[0]) {
ctx->ct_flags &= ~SMBCF_AUTHREQ;
error = smb_browse(ctx, 0);
if (ctx->ct_flags & SMBCF_KCFOUND && smb_autherr(error)) {
smb_error(dgettext(TEXT_DOMAIN,
"smb_ctx_resolve: bad keychain entry"), 0);
ctx->ct_flags |= SMBCF_KCBAD;
renego = 1;
goto reauth;
}
if (error) /* auth, user cancel, or other error */
return (error);
/*
* Re-authenticate button was pressed?
*/
if (ctx->ct_flags & SMBCF_AUTHREQ)
goto reauth;
if (!sh->ioc_share[0] && !(ctx->ct_flags & SMBCF_XXX)) {
smb_error(dgettext(TEXT_DOMAIN,
"no share specified for %s@%s"),
0, ssn->ioc_user, ssn->ioc_srvname);
return (EINVAL);
}
}
ctx->ct_flags |= SMBCF_RESOLVED;
if (smb_debug)
dump_ctx("after smb_ctx_resolve", ctx);
return (0);
}
int
smb_open_driver()
{
char buf[20];
int err, fd, i;
uint32_t version;
/*
* First try to open as clone
*/
fd = open("/dev/"NSMB_NAME, O_RDWR);
if (fd >= 0)
goto opened;
err = errno; /* from open */
#ifdef APPLE
/*
* well, no clone capabilities available - we have to scan
* all devices in order to get free one
*/
for (i = 0; i < 1024; i++) {
snprintf(buf, sizeof (buf), "/dev/%s%d", NSMB_NAME, i);
fd = open(buf, O_RDWR);
if (fd >= 0)
goto opened;
if (i && POWEROF2(i+1))
smb_error(dgettext(TEXT_DOMAIN,
"%d failures to open smb device"), errno, i+1);
}
err = ENOENT;
#endif
smb_error(dgettext(TEXT_DOMAIN,
"failed to open %s"), err, "/dev/" NSMB_NAME);
return (-1);
opened:
/*
* Check the driver version (paranoia)
* Do this BEFORE any other ioctl calls.
*/
if (ioctl(fd, SMBIOC_GETVERS, &version) < 0) {
err = errno;
smb_error(dgettext(TEXT_DOMAIN,
"failed to get driver version"), err);
close(fd);
return (-1);
}
if (version != NSMB_VERSION) {
smb_error(dgettext(TEXT_DOMAIN,
"incorrect driver version"), 0);
close(fd);
return (-1);
}
return (fd);
}
static int
smb_ctx_gethandle(struct smb_ctx *ctx)
{
int err, fd;
if (ctx->ct_fd != -1) {
rpc_cleanup_smbctx(ctx);
close(ctx->ct_fd);
ctx->ct_fd = -1;
ctx->ct_flags &= ~SMBCF_SSNACTIVE;
}
fd = smb_open_driver();
if (fd < 0)
return (ENODEV);
ctx->ct_fd = fd;
return (0);
}
int
smb_ctx_ioctl(struct smb_ctx *ctx, int inum, struct smbioc_lookup *rqp)
{
size_t siz = DEF_SEC_TOKEN_LEN;
int rc = 0;
struct sockaddr sap1, sap2;
int i;
if (rqp->ioc_ssn.ioc_outtok)
free(rqp->ioc_ssn.ioc_outtok);
rqp->ioc_ssn.ioc_outtoklen = siz;
rqp->ioc_ssn.ioc_outtok = malloc(siz+1);
if (rqp->ioc_ssn.ioc_outtok == NULL)
return (ENOMEM);
bzero(rqp->ioc_ssn.ioc_outtok, siz+1);
/* Note: No longer put length in outtok[0] */
/* *((int *)rqp->ioc_ssn.ioc_outtok) = (int)siz; */
if (ioctl(ctx->ct_fd, inum, rqp) == -1) {
rc = errno;
goto out;
}
if (rqp->ioc_ssn.ioc_outtoklen <= siz)
goto out;
/*
* Operation completed, but our output token wasn't large enough.
* The re-call below only pulls the token from the kernel.
*/
siz = rqp->ioc_ssn.ioc_outtoklen;
free(rqp->ioc_ssn.ioc_outtok);
rqp->ioc_ssn.ioc_outtok = malloc(siz + 1);
if (rqp->ioc_ssn.ioc_outtok == NULL) {
rc = ENOMEM;
goto out;
}
bzero(rqp->ioc_ssn.ioc_outtok, siz+1);
/* Note: No longer put length in outtok[0] */
/* *((int *)rqp->ioc_ssn.ioc_outtok) = siz; */
if (ioctl(ctx->ct_fd, inum, rqp) == -1)
rc = errno;
out:
return (rc);
}
int
smb_ctx_findvc(struct smb_ctx *ctx, int level, int flags)
{
struct smbioc_lookup rq;
int error = 0;
if ((error = smb_ctx_gethandle(ctx)))
return (error);
bzero(&rq, sizeof (rq));
bcopy(&ctx->ct_ssn, &rq.ioc_ssn, sizeof (struct smbioc_ossn));
bcopy(&ctx->ct_sh, &rq.ioc_sh, sizeof (struct smbioc_oshare));
rq.ioc_flags = flags;
rq.ioc_level = level;
return (smb_ctx_ioctl(ctx, SMBIOC_FINDVC, &rq));
}
/*
* adds a GSSAPI wrapper
*/
char *
smb_ctx_tkt2gtok(uchar_t *tkt, ulong_t tktlen,
uchar_t **gtokp, ulong_t *gtoklenp)
{
ulong_t bloblen = tktlen;
ulong_t len;
uchar_t krbapreq[2] = "\x01\x00"; /* see RFC 1964 */
char *failure;
uchar_t *blob = NULL; /* result */
uchar_t *b;
bloblen += sizeof (krbapreq);
bloblen += g_stcMechOIDList[spnego_mech_oid_Kerberos_V5].iLen;
len = bloblen;
bloblen = ASNDerCalcTokenLength(bloblen, bloblen);
failure = dgettext(TEXT_DOMAIN, "smb_ctx_tkt2gtok malloc");
if (!(blob = malloc(bloblen)))
goto out;
b = blob;
b += ASNDerWriteToken(b, SPNEGO_NEGINIT_APP_CONSTRUCT, NULL, len);
b += ASNDerWriteOID(b, spnego_mech_oid_Kerberos_V5);
memcpy(b, krbapreq, sizeof (krbapreq));
b += sizeof (krbapreq);
failure = dgettext(TEXT_DOMAIN, "smb_ctx_tkt2gtok insanity check");
if (b + tktlen != blob + bloblen)
goto out;
memcpy(b, tkt, tktlen);
*gtoklenp = bloblen;
*gtokp = blob;
failure = NULL;
out:;
if (blob && failure)
free(blob);
return (failure);
}
/*
* Initialization for Kerberos, pulled out of smb_ctx_principal2tkt.
* This just gets our cached credentials, if we have any.
* Based on the "klist" command.
*/
char *
smb_ctx_krb5init(struct smb_ctx *ctx)
{
char *failure;
krb5_error_code kerr;
krb5_context kctx = NULL;
krb5_ccache kcc = NULL;
krb5_principal kprin = NULL;
kerr = krb5_init_context(&kctx);
if (kerr) {
failure = "krb5_init_context";
goto out;
}
ctx->ct_krb5ctx = kctx;
/* non-default would instead use krb5_cc_resolve */
kerr = krb5_cc_default(kctx, &kcc);
if (kerr) {
failure = "krb5_cc_default";
goto out;
}
ctx->ct_krb5cc = kcc;
/*
* Get the client principal (ticket),
* or find out if we don't have one.
*/
kerr = krb5_cc_get_principal(kctx, kcc, &kprin);
if (kerr) {
failure = "krb5_cc_get_principal";
goto out;
}
ctx->ct_krb5cp = kprin;
if (smb_verbose) {
fprintf(stderr, gettext("Ticket cache: %s:%s\n"),
krb5_cc_get_type(kctx, kcc),
krb5_cc_get_name(kctx, kcc));
}
failure = NULL;
out:
return (failure);
}
/*
* See "Windows 2000 Kerberos Interoperability" paper by
* Christopher Nebergall. RC4 HMAC is the W2K default but
* Samba support lagged (not due to Samba itself, but due to OS'
* Kerberos implementations.)
*
* Only session enc type should matter, not ticket enc type,
* per Sam Hartman on krbdev.
*
* Preauthentication failure topics in krb-protocol may help here...
* try "John Brezak" and/or "Clifford Neuman" too.
*/
static krb5_enctype kenctypes[] = {
ENCTYPE_ARCFOUR_HMAC, /* defined in Tiger krb5.h */
ENCTYPE_DES_CBC_MD5,
ENCTYPE_DES_CBC_CRC,
ENCTYPE_NULL
};
/*
* Obtain a kerberos ticket...
* (if TLD != "gov" then pray first)
*/
char *
smb_ctx_principal2tkt(
struct smb_ctx *ctx, char *prin,
uchar_t **tktp, ulong_t *tktlenp)
{
char *failure;
krb5_context kctx = NULL;
krb5_error_code kerr;
krb5_ccache kcc = NULL;
krb5_principal kprin = NULL, cprn = NULL;
krb5_creds kcreds, *kcredsp = NULL;
krb5_auth_context kauth = NULL;
krb5_data kdata, kdata0;
uchar_t *tkt;
memset((char *)&kcreds, 0, sizeof (kcreds));
kdata0.length = 0;
/* These shoud have been done in smb_ctx_krb5init() */
if (ctx->ct_krb5ctx == NULL ||
ctx->ct_krb5cc == NULL ||
ctx->ct_krb5cp == NULL) {
failure = "smb_ctx_krb5init";
goto out;
}
kctx = ctx->ct_krb5ctx;
kcc = ctx->ct_krb5cc;
cprn = ctx->ct_krb5cp;
failure = "krb5_set_default_tgs_enctypes";
if ((kerr = krb5_set_default_tgs_enctypes(kctx, kenctypes)))
goto out;
/*
* The following is an unrolling of krb5_mk_req. Something like:
* krb5_mk_req(kctx, &kauth, 0, service(prin), hostname(prin),
* &kdata0, kcc, &kdata);)
* ...except we needed krb5_parse_name not krb5_sname_to_principal.
*/
failure = "krb5_parse_name";
if ((kerr = krb5_parse_name(kctx, prin, &kprin)))
goto out;
failure = "krb5_copy_principal(server)";
if ((kerr = krb5_copy_principal(kctx, kprin, &kcreds.server)))
goto out;
failure = "krb5_copy_principal(client)";
if ((kerr = krb5_copy_principal(kctx, cprn, &kcreds.client)))
goto out;
failure = "krb5_get_credentials";
if ((kerr = krb5_get_credentials(kctx, 0, kcc, &kcreds, &kcredsp)))
goto out;
failure = "krb5_mk_req_extended";
if ((kerr = krb5_mk_req_extended(kctx, &kauth, 0, &kdata0, kcredsp,
&kdata)))
goto out;
failure = "malloc";
if (!(tkt = malloc(kdata.length))) {
krb5_free_data_contents(kctx, &kdata);
goto out;
}
*tktlenp = kdata.length;
memcpy(tkt, kdata.data, kdata.length);
krb5_free_data_contents(kctx, &kdata);
*tktp = tkt;
failure = NULL;
out:;
if (kerr) {
if (!failure)
failure = "smb_ctx_principal2tkt";
/*
* Avoid logging the typical "No credentials cache found"
*/
if (kerr != KRB5_FCC_NOFILE ||
strcmp(failure, "krb5_cc_get_principal"))
com_err(__progname, kerr, failure);
}
if (kauth)
krb5_auth_con_free(kctx, kauth);
if (kcredsp)
krb5_free_creds(kctx, kcredsp);
if (kcreds.server || kcreds.client)
krb5_free_cred_contents(kctx, &kcreds);
if (kprin)
krb5_free_principal(kctx, kprin);
/* Free kctx in smb_ctx_done */
return (failure);
}
char *
smb_ctx_principal2blob(
struct smb_ctx *ctx,
smbioc_ossn_t *ssn,
char *prin)
{
int rc = 0;
char *failure;
uchar_t *tkt = NULL;
ulong_t tktlen;
uchar_t *gtok = NULL; /* gssapi token */
ulong_t gtoklen; /* gssapi token length */
SPNEGO_TOKEN_HANDLE stok = NULL; /* spnego token */
void *blob = NULL; /* result */
ulong_t bloblen; /* result length */
if ((failure = smb_ctx_principal2tkt(ctx, prin, &tkt, &tktlen)))
goto out;
if ((failure = smb_ctx_tkt2gtok(tkt, tktlen, >ok, >oklen)))
goto out;
/*
* RFC says to send NegTokenTarg now. So does MS docs. But
* win2k gives ERRbaduid if we do... we must send
* another NegTokenInit now!
*/
failure = "spnegoCreateNegTokenInit";
if ((rc = spnegoCreateNegTokenInit(spnego_mech_oid_Kerberos_V5_Legacy,
0, gtok, gtoklen, NULL, 0, &stok)))
goto out;
failure = "spnegoTokenGetBinary(NULL)";
rc = spnegoTokenGetBinary(stok, NULL, &bloblen);
if (rc != SPNEGO_E_BUFFER_TOO_SMALL)
goto out;
failure = "malloc";
if (!(blob = malloc((size_t)bloblen)))
goto out;
/* No longer store length at start of blob. */
/* *blob = bloblen; */
failure = "spnegoTokenGetBinary";
if ((rc = spnegoTokenGetBinary(stok, blob, &bloblen)))
goto out;
ssn->ioc_intoklen = bloblen;
ssn->ioc_intok = blob;
failure = NULL;
out:;
if (rc) {
/* XXX better is to embed rc in failure */
smb_error(dgettext(TEXT_DOMAIN,
"spnego principal2blob error %d"), 0, -rc);
if (!failure)
failure = "spnego";
}
if (blob && failure)
free(blob);
if (stok)
spnegoFreeData(stok);
if (gtok)
free(gtok);
if (tkt)
free(tkt);
return (failure);
}
#if 0
void
prblob(uchar_t *b, size_t len)
{
while (len--)
fprintf(stderr, "%02x", *b++);
fprintf(stderr, "\n");
}
#endif
/*
* We navigate the SPNEGO & ASN1 encoding to find a kerberos principal
* Note: driver no longer puts length at start of blob.
*/
char *
smb_ctx_blob2principal(
struct smb_ctx *ctx,
smbioc_ossn_t *ssn,
char **prinp)
{
uchar_t *blob = ssn->ioc_outtok;
size_t len = ssn->ioc_outtoklen;
int rc = 0;
SPNEGO_TOKEN_HANDLE stok = NULL;
int indx = 0;
char *failure;
uchar_t flags = 0;
unsigned long plen = 0;
uchar_t *prin;
#if 0
fprintf(stderr, "blob from negotiate:\n");
prblob(blob, len);
#endif
/* Skip the GUID */
assert(len >= SMB_GUIDLEN);
blob += SMB_GUIDLEN;
len -= SMB_GUIDLEN;
failure = "spnegoInitFromBinary";
if ((rc = spnegoInitFromBinary(blob, len, &stok)))
goto out;
/*
* Needn't use new Kerberos OID - the Legacy one is fine.
*/
failure = "spnegoIsMechTypeAvailable";
if (spnegoIsMechTypeAvailable(stok, spnego_mech_oid_Kerberos_V5_Legacy,
&indx))
goto out;
/*
* Ignoring optional context flags for now. May want to pass
* them to krb5 layer. XXX
*/
if (!spnegoGetContextFlags(stok, &flags))
fprintf(stderr, dgettext(TEXT_DOMAIN,
"spnego context flags 0x%x\n"), flags);
failure = "spnegoGetMechListMIC(NULL)";
rc = spnegoGetMechListMIC(stok, NULL, &plen);
if (rc != SPNEGO_E_BUFFER_TOO_SMALL)
goto out;
failure = "malloc";
if (!(prin = malloc(plen + 1)))
goto out;
failure = "spnegoGetMechListMIC";
if ((rc = spnegoGetMechListMIC(stok, prin, &plen))) {
free(prin);
goto out;
}
prin[plen] = '\0';
*prinp = (char *)prin;
failure = NULL;
out:;
if (stok)
spnegoFreeData(stok);
if (rc) {
/* XXX better is to embed rc in failure */
smb_error(dgettext(TEXT_DOMAIN,
"spnego blob2principal error %d"), 0, -rc);
if (!failure)
failure = "spnego";
}
return (failure);
}
int
smb_ctx_negotiate(struct smb_ctx *ctx, int level, int flags, char *workgroup)
{
struct smbioc_lookup rq;
int error = 0;
char *failure = NULL;
char *principal = NULL;
char c;
int i;
ssize_t *outtoklen;
uchar_t *blob;
/*
* We leave ct_secblob set iff extended security
* negotiation succeeds.
*/
if (ctx->ct_secblob) {
free(ctx->ct_secblob);
ctx->ct_secblob = NULL;
}
#ifdef XXX
if ((ctx->ct_flags & SMBCF_RESOLVED) == 0) {
smb_error(dgettext(TEXT_DOMAIN,
"smb_ctx_lookup() data is not resolved"), 0);
return (EINVAL);
}
#endif
if ((error = smb_ctx_gethandle(ctx)))
return (error);
bzero(&rq, sizeof (rq));
bcopy(&ctx->ct_ssn, &rq.ioc_ssn, sizeof (struct smbioc_ossn));
bcopy(&ctx->ct_sh, &rq.ioc_sh, sizeof (struct smbioc_oshare));
/*
* Find out if we have a Kerberos ticket,
* and only offer SPNEGO if we have one.
*/
failure = smb_ctx_krb5init(ctx);
if (failure) {
if (smb_verbose)
smb_error(failure, 0);
goto out;
}
rq.ioc_flags = flags;
rq.ioc_level = level;
rq.ioc_ssn.ioc_opt |= SMBVOPT_EXT_SEC;
error = smb_ctx_ioctl(ctx, SMBIOC_NEGOTIATE, &rq);
if (error) {
failure = dgettext(TEXT_DOMAIN, "negotiate failed");
smb_error(failure, error);
if (error == ETIMEDOUT)
return (error);
goto out;
}
/*
* If the server capabilities did not include
* SMB_CAP_EXT_SECURITY then the driver clears
* the flag SMBVOPT_EXT_SEC for us.
* XXX: should add the capabilities to ioc_ssn
* XXX: see comment in driver - smb_usr.c
*/
failure = dgettext(TEXT_DOMAIN, "SPNEGO unsupported");
if ((rq.ioc_ssn.ioc_opt & SMBVOPT_EXT_SEC) == 0) {
if (smb_verbose)
smb_error(failure, 0);
/*
* Do regular (old style) NTLM or NTLMv2
* Nothing more to do here in negotiate.
*/
return (0);
}
/*
* Capabilities DO include SMB_CAP_EXT_SECURITY,
* so this should be an SPNEGO security blob.
* Parse the ASN.1/DER, prepare response(s).
* XXX: Handle STATUS_MORE_PROCESSING_REQUIRED?
* XXX: Requires additional session setup calls.
*/
if (rq.ioc_ssn.ioc_outtoklen <= SMB_GUIDLEN)
goto out;
/* some servers send padding junk */
blob = rq.ioc_ssn.ioc_outtok;
if (blob[0] == 0)
goto out;
failure = smb_ctx_blob2principal(
ctx, &rq.ioc_ssn, &principal);
if (failure)
goto out;
failure = smb_ctx_principal2blob(
ctx, &rq.ioc_ssn, principal);
if (failure)
goto out;
/* Success! Save the blob to send next. */
ctx->ct_secblob = rq.ioc_ssn.ioc_intok;
ctx->ct_secbloblen = rq.ioc_ssn.ioc_intoklen;
rq.ioc_ssn.ioc_intok = NULL;
out:
if (principal)
free(principal);
if (rq.ioc_ssn.ioc_intok)
free(rq.ioc_ssn.ioc_intok);
if (rq.ioc_ssn.ioc_outtok)
free(rq.ioc_ssn.ioc_outtok);
if (!failure)
return (0); /* Success! */
/*
* Negotiate failed with "extended security".
*
* XXX: If we are doing SPNEGO correctly,
* we should never get here unless the user
* supplied invalid authentication data,
* or we saw some kind of protocol error.
*
* XXX: The error message below should be
* XXX: unconditional (remove "if verbose")
* XXX: but not until we have "NTLMSSP"
* Avoid spew for anticipated failure modes
* but enable this with the verbose flag
*/
if (smb_verbose) {
smb_error(dgettext(TEXT_DOMAIN,
"%s (extended security negotiate)"), error, failure);
}
/*
* XXX: Try again using NTLM (or NTLMv2)
* XXX: Normal clients don't do this.
* XXX: Should just return an error, but
* keep the fall-back to NTLM for now.
*
* Start over with a new connection.
*/
if ((error = smb_ctx_gethandle(ctx)))
return (error);
bzero(&rq, sizeof (rq));
bcopy(&ctx->ct_ssn, &rq.ioc_ssn, sizeof (struct smbioc_ossn));
bcopy(&ctx->ct_sh, &rq.ioc_sh, sizeof (struct smbioc_oshare));
rq.ioc_flags = flags;
rq.ioc_level = level;
/* Note: NO SMBVOPT_EXT_SEC */
error = smb_ctx_ioctl(ctx, SMBIOC_NEGOTIATE, &rq);
if (error) {
failure = dgettext(TEXT_DOMAIN, "negotiate failed");
smb_error(failure, error);
rpc_cleanup_smbctx(ctx);
close(ctx->ct_fd);
ctx->ct_fd = -1;
return (error);
}
/*
* Used to copy the workgroup out of the SMB_NEGOTIATE response
* here, to default our domain name to be the same as the server.
* Not a good idea: Unnecessary at best, and sometimes wrong, i.e.
* when our account is in a trusted domain.
*/
return (error);
}
int
smb_ctx_tdis(struct smb_ctx *ctx)
{
struct smbioc_lookup rq; /* XXX may be used, someday */
int error = 0;
if (ctx->ct_fd < 0) {
smb_error(dgettext(TEXT_DOMAIN,
"tree disconnect without handle?!"), 0);
return (EINVAL);
}
if (!(ctx->ct_flags & SMBCF_SSNACTIVE)) {
smb_error(dgettext(TEXT_DOMAIN,
"tree disconnect without session?!"), 0);
return (EINVAL);
}
bzero(&rq, sizeof (rq));
bcopy(&ctx->ct_ssn, &rq.ioc_ssn, sizeof (struct smbioc_ossn));
bcopy(&ctx->ct_sh, &rq.ioc_sh, sizeof (struct smbioc_oshare));
if (ioctl(ctx->ct_fd, SMBIOC_TDIS, &rq) == -1) {
error = errno;
smb_error(dgettext(TEXT_DOMAIN,
"tree disconnect failed"), error);
}
return (error);
}
int
smb_ctx_lookup(struct smb_ctx *ctx, int level, int flags)
{
struct smbioc_lookup rq;
int error = 0;
char *failure = NULL;
if ((ctx->ct_flags & SMBCF_RESOLVED) == 0) {
smb_error(dgettext(TEXT_DOMAIN,
"smb_ctx_lookup() data is not resolved"), 0);
return (EINVAL);
}
if (ctx->ct_fd < 0) {
smb_error(dgettext(TEXT_DOMAIN,
"handle from smb_ctx_nego() gone?!"), 0);
return (EINVAL);
}
if (!(flags & SMBLK_CREATE))
return (0);
bzero(&rq, sizeof (rq));
bcopy(&ctx->ct_ssn, &rq.ioc_ssn, sizeof (struct smbioc_ossn));
bcopy(&ctx->ct_sh, &rq.ioc_sh, sizeof (struct smbioc_oshare));
rq.ioc_flags = flags;
rq.ioc_level = level;
/*
* Iff we have a security blob, we're using
* extended security...
*/
if (ctx->ct_secblob) {
rq.ioc_ssn.ioc_opt |= SMBVOPT_EXT_SEC;
if (!(ctx->ct_flags & SMBCF_SSNACTIVE)) {
rq.ioc_ssn.ioc_intok = ctx->ct_secblob;
rq.ioc_ssn.ioc_intoklen = ctx->ct_secbloblen;
error = smb_ctx_ioctl(ctx, SMBIOC_SSNSETUP, &rq);
}
rq.ioc_ssn.ioc_intok = NULL;
if (error) {
failure = dgettext(TEXT_DOMAIN,
"session setup failed");
} else {
ctx->ct_flags |= SMBCF_SSNACTIVE;
if ((error = smb_ctx_ioctl(ctx, SMBIOC_TCON, &rq)))
failure = dgettext(TEXT_DOMAIN,
"tree connect failed");
}
if (rq.ioc_ssn.ioc_intok)
free(rq.ioc_ssn.ioc_intok);
if (rq.ioc_ssn.ioc_outtok)
free(rq.ioc_ssn.ioc_outtok);
if (!failure)
return (0);
smb_error(dgettext(TEXT_DOMAIN,
"%s (extended security lookup2)"), error, failure);
/* unwise to failback to NTLM now */
return (error);
}
/*
* Otherwise we're doing plain old NTLM
*/
if ((ctx->ct_flags & SMBCF_SSNACTIVE) == 0) {
/*
* This is the magic that tells the driver to
* copy the password from the keychain, and
* whether to use the system name or the
* account domain to lookup the keychain.
*/
if (ctx->ct_flags & SMBCF_KCFOUND)
rq.ioc_ssn.ioc_opt |= SMBVOPT_USE_KEYCHAIN;
if (ctx->ct_flags & SMBCF_KCDOMAIN)
rq.ioc_ssn.ioc_opt |= SMBVOPT_KC_DOMAIN;
if (ioctl(ctx->ct_fd, SMBIOC_SSNSETUP, &rq) < 0) {
error = errno;
failure = dgettext(TEXT_DOMAIN, "session setup");
goto out;
}
ctx->ct_flags |= SMBCF_SSNACTIVE;
}
if (ioctl(ctx->ct_fd, SMBIOC_TCON, &rq) == -1) {
error = errno;
failure = dgettext(TEXT_DOMAIN, "tree connect");
}
out:
if (failure) {
error = errno;
smb_error(dgettext(TEXT_DOMAIN,
"%s phase failed"), error, failure);
}
return (error);
}
/*
* Return the hflags2 word for an smb_ctx.
*/
int
smb_ctx_flags2(struct smb_ctx *ctx)
{
uint16_t flags2;
if (ioctl(ctx->ct_fd, SMBIOC_FLAGS2, &flags2) == -1) {
smb_error(dgettext(TEXT_DOMAIN,
"can't get flags2 for a session"), errno);
return (-1);
}
return (flags2);
}
/*
* level values:
* 0 - default
* 1 - server
* 2 - server:user
* 3 - server:user:share
*/
static int
smb_ctx_readrcsection(struct smb_ctx *ctx, const char *sname, int level)
{
char *p;
int error;
#ifdef NOT_DEFINED
if (level > 0) {
rc_getstringptr(smb_rc, sname, "charsets", &p);
if (p) {
error = smb_ctx_setcharset(ctx, p);
if (error)
smb_error(dgettext(TEXT_DOMAIN,
"charset specification in the section '%s' ignored"),
error, sname);
}
}
#endif
if (level <= 1) {
/* Section is: [default] or [server] */
rc_getint(smb_rc, sname, "timeout",
&ctx->ct_ssn.ioc_timeout);
#ifdef NOT_DEFINED
rc_getint(smb_rc, sname, "retry_count",
&ctx->ct_ssn.ioc_retrycount);
rc_getstringptr(smb_rc, sname, "use_negprot_domain", &p);
if (p && strcmp(p, "NO") == 0)
ctx->ct_flags |= SMBCF_NONEGDOM;
#endif
rc_getstringptr(smb_rc, sname, "minauth", &p);
if (p) {
/*
* "minauth" was set in this section; override
* the current minimum authentication setting.
*/
ctx->ct_ssn.ioc_opt &= ~SMBVOPT_MINAUTH;
if (strcmp(p, "kerberos") == 0) {
/*
* Don't fall back to NTLMv2, NTLMv1, or
* a clear text password.
*/
ctx->ct_ssn.ioc_opt |= SMBVOPT_MINAUTH_KERBEROS;
} else if (strcmp(p, "ntlmv2") == 0) {
/*
* Don't fall back to NTLMv1 or a clear
* text password.
*/
ctx->ct_ssn.ioc_opt |= SMBVOPT_MINAUTH_NTLMV2;
} else if (strcmp(p, "ntlm") == 0) {
/*
* Don't send the LM response over the wire.
*/
ctx->ct_ssn.ioc_opt |= SMBVOPT_MINAUTH_NTLM;
} else if (strcmp(p, "lm") == 0) {
/*
* Fail if the server doesn't do encrypted
* passwords.
*/
ctx->ct_ssn.ioc_opt |= SMBVOPT_MINAUTH_LM;
} else if (strcmp(p, "none") == 0) {
/*
* Anything goes.
* (The following statement should be
* optimized away.)
*/
/* LINTED */
ctx->ct_ssn.ioc_opt |= SMBVOPT_MINAUTH_NONE;
} else {
/*
* Unknown minimum authentication level.
*/
smb_error(dgettext(TEXT_DOMAIN,
"invalid minimum authentication level \"%s\" specified in the section %s"),
0, p, sname);
return (EINVAL);
}
}
rc_getstringptr(smb_rc, sname, "signing", &p);
if (p) {
/*
* "signing" was set in this section; override
* the current signing settings.
*/
ctx->ct_ssn.ioc_opt &= ~SMBVOPT_SIGNING_MASK;
if (strcmp(p, "disabled") == 0) {
/* leave flags zero (expr for lint) */
(void) ctx->ct_ssn.ioc_opt;
} else if (strcmp(p, "enabled") == 0) {
ctx->ct_ssn.ioc_opt |=
SMBVOPT_SIGNING_ENABLED;
} else if (strcmp(p, "required") == 0) {
ctx->ct_ssn.ioc_opt |=
SMBVOPT_SIGNING_ENABLED |
SMBVOPT_SIGNING_REQUIRED;
} else {
/*
* Unknown "signing" value.
*/
smb_error(dgettext(TEXT_DOMAIN,
"invalid signing policy \"%s\" specified in the section %s"),
0, p, sname);
return (EINVAL);
}
}
/*
* Domain name. Allow both keywords:
* "workgroup", "domain"
*
* Note: these are NOT marked "from CMD".
* See long comment at smb_ctx_init()
*/
rc_getstringptr(smb_rc, sname, "workgroup", &p);
if (p) {
nls_str_upper(p, p);
error = smb_ctx_setworkgroup(ctx, p, 0);
if (error)
smb_error(dgettext(TEXT_DOMAIN,
"workgroup specification in the "
"section '%s' ignored"), error, sname);
}
rc_getstringptr(smb_rc, sname, "domain", &p);
if (p) {
nls_str_upper(p, p);
error = smb_ctx_setworkgroup(ctx, p, 0);
if (error)
smb_error(dgettext(TEXT_DOMAIN,
"domain specification in the "
"section '%s' ignored"), error, sname);
}
rc_getstringptr(smb_rc, sname, "user", &p);
if (p) {
error = smb_ctx_setuser(ctx, p, 0);
if (error)
smb_error(dgettext(TEXT_DOMAIN,
"user specification in the "
"section '%s' ignored"), error, sname);
}
}
if (level == 1) {
/* Section is: [server] */
rc_getstringptr(smb_rc, sname, "addr", &p);
if (p) {
error = smb_ctx_setsrvaddr(ctx, p);
if (error) {
smb_error(dgettext(TEXT_DOMAIN,
"invalid address specified in section %s"),
0, sname);
return (error);
}
}
}
rc_getstringptr(smb_rc, sname, "password", &p);
if (p) {
error = smb_ctx_setpassword(ctx, p, 0);
if (error)
smb_error(dgettext(TEXT_DOMAIN,
"password specification in the section '%s' ignored"),
error, sname);
}
return (0);
}
/*
* read rc file as follows:
* 0: read [default] section
* 1: override with [server] section
* 2: override with [server:user] section
* 3: override with [server:user:share] section
* Since absence of rcfile is not fatal, silently ignore this fact.
* smb_rc file should be closed by caller.
*/
int
smb_ctx_readrc(struct smb_ctx *ctx)
{
char sname[SMB_MAXSRVNAMELEN + SMB_MAXUSERNAMELEN +
SMB_MAXSHARENAMELEN + 4];
if (smb_open_rcfile(ctx) != 0)
goto done;
/*
* default parameters (level=0)
*/
smb_ctx_readrcsection(ctx, "default", 0);
nb_ctx_readrcsection(smb_rc, ctx->ct_nb, "default", 0);
/*
* If we don't have a server name, we can't read any of the
* [server...] sections.
*/
if (ctx->ct_ssn.ioc_srvname[0] == 0)
goto done;
/*
* SERVER parameters.
*/
smb_ctx_readrcsection(ctx, ctx->ct_ssn.ioc_srvname, 1);
/*
* If we don't have a user name, we can't read any of the
* [server:user...] sections.
*/
if (ctx->ct_ssn.ioc_user[0] == 0)
goto done;
/*
* SERVER:USER parameters
*/
snprintf(sname, sizeof (sname), "%s:%s",
ctx->ct_ssn.ioc_srvname,
ctx->ct_ssn.ioc_user);
smb_ctx_readrcsection(ctx, sname, 2);
/*
* If we don't have a share name, we can't read any of the
* [server:user:share] sections.
*/
if (ctx->ct_sh.ioc_share[0] != 0) {
/*
* SERVER:USER:SHARE parameters
*/
snprintf(sname, sizeof (sname), "%s:%s:%s",
ctx->ct_ssn.ioc_srvname,
ctx->ct_ssn.ioc_user,
ctx->ct_sh.ioc_share);
smb_ctx_readrcsection(ctx, sname, 3);
}
done:
if (smb_debug)
dump_ctx("after smb_ctx_readrc", ctx);
return (0);
}
|