1c398230bSWarner Losh /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
4681a5bbeSBoris Popov * Copyright (c) 2000-2001 Boris Popov
5681a5bbeSBoris Popov * All rights reserved.
6681a5bbeSBoris Popov *
7681a5bbeSBoris Popov * Redistribution and use in source and binary forms, with or without
8681a5bbeSBoris Popov * modification, are permitted provided that the following conditions
9681a5bbeSBoris Popov * are met:
10681a5bbeSBoris Popov * 1. Redistributions of source code must retain the above copyright
11681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer.
12681a5bbeSBoris Popov * 2. Redistributions in binary form must reproduce the above copyright
13681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer in the
14681a5bbeSBoris Popov * documentation and/or other materials provided with the distribution.
15681a5bbeSBoris Popov *
16681a5bbeSBoris Popov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17681a5bbeSBoris Popov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18681a5bbeSBoris Popov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19681a5bbeSBoris Popov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20681a5bbeSBoris Popov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21681a5bbeSBoris Popov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22681a5bbeSBoris Popov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23681a5bbeSBoris Popov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24681a5bbeSBoris Popov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25681a5bbeSBoris Popov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26681a5bbeSBoris Popov * SUCH DAMAGE.
27681a5bbeSBoris Popov */
28ab0de15bSDavid E. O'Brien
29681a5bbeSBoris Popov /*
30681a5bbeSBoris Popov * various SMB requests. Most of the routines merely packs data into mbufs.
31681a5bbeSBoris Popov */
32ab0de15bSDavid E. O'Brien
33681a5bbeSBoris Popov #include <sys/param.h>
34681a5bbeSBoris Popov #include <sys/systm.h>
35681a5bbeSBoris Popov #include <sys/kernel.h>
36681a5bbeSBoris Popov #include <sys/malloc.h>
37681a5bbeSBoris Popov #include <sys/proc.h>
38681a5bbeSBoris Popov #include <sys/lock.h>
39681a5bbeSBoris Popov #include <sys/sysctl.h>
40681a5bbeSBoris Popov #include <sys/socket.h>
41681a5bbeSBoris Popov #include <sys/uio.h>
42681a5bbeSBoris Popov
43681a5bbeSBoris Popov #include <sys/iconv.h>
44681a5bbeSBoris Popov
45681a5bbeSBoris Popov #include <netsmb/smb.h>
46681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
47681a5bbeSBoris Popov #include <netsmb/smb_rq.h>
48681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
49681a5bbeSBoris Popov #include <netsmb/smb_tran.h>
50681a5bbeSBoris Popov
51190b2c4fSTim J. Robbins #include "opt_netsmb.h"
52190b2c4fSTim J. Robbins
53681a5bbeSBoris Popov struct smb_dialect {
54681a5bbeSBoris Popov int d_id;
55681a5bbeSBoris Popov const char * d_name;
56681a5bbeSBoris Popov };
57681a5bbeSBoris Popov
58681a5bbeSBoris Popov static struct smb_dialect smb_dialects[] = {
59681a5bbeSBoris Popov {SMB_DIALECT_CORE, "PC NETWORK PROGRAM 1.0"},
60681a5bbeSBoris Popov {SMB_DIALECT_COREPLUS, "MICROSOFT NETWORKS 1.03"},
61681a5bbeSBoris Popov {SMB_DIALECT_LANMAN1_0, "MICROSOFT NETWORKS 3.0"},
62681a5bbeSBoris Popov {SMB_DIALECT_LANMAN1_0, "LANMAN1.0"},
63681a5bbeSBoris Popov {SMB_DIALECT_LANMAN2_0, "LM1.2X002"},
64681a5bbeSBoris Popov {SMB_DIALECT_LANMAN2_0, "Samba"},
65681a5bbeSBoris Popov {SMB_DIALECT_NTLM0_12, "NT LANMAN 1.0"},
66681a5bbeSBoris Popov {SMB_DIALECT_NTLM0_12, "NT LM 0.12"},
67681a5bbeSBoris Popov {-1, NULL}
68681a5bbeSBoris Popov };
69681a5bbeSBoris Popov
70e2a70cffSBoris Popov static u_int32_t
smb_vc_maxread(struct smb_vc * vcp)71e2a70cffSBoris Popov smb_vc_maxread(struct smb_vc *vcp)
72e2a70cffSBoris Popov {
73e2a70cffSBoris Popov /*
74e2a70cffSBoris Popov * Specs say up to 64k data bytes, but Windows traffic
75e2a70cffSBoris Popov * uses 60k... no doubt for some good reason.
76190b2c4fSTim J. Robbins *
77190b2c4fSTim J. Robbins * Don't exceed the server's buffer size if signatures
78190b2c4fSTim J. Robbins * are enabled otherwise Windows 2003 chokes. Allow space
79190b2c4fSTim J. Robbins * for the SMB header & a little bit extra.
80e2a70cffSBoris Popov */
81190b2c4fSTim J. Robbins if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_READX) &&
82190b2c4fSTim J. Robbins (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0)
83e2a70cffSBoris Popov return (60*1024);
84e2a70cffSBoris Popov else
85190b2c4fSTim J. Robbins return (vcp->vc_sopt.sv_maxtx - SMB_HDRLEN - 64);
86e2a70cffSBoris Popov }
87e2a70cffSBoris Popov
88e2a70cffSBoris Popov static u_int32_t
smb_vc_maxwrite(struct smb_vc * vcp)89e2a70cffSBoris Popov smb_vc_maxwrite(struct smb_vc *vcp)
90e2a70cffSBoris Popov {
91e2a70cffSBoris Popov /*
92190b2c4fSTim J. Robbins * See comment above.
93e2a70cffSBoris Popov */
94190b2c4fSTim J. Robbins if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX) &&
95190b2c4fSTim J. Robbins (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0)
96e2a70cffSBoris Popov return (60*1024);
97e2a70cffSBoris Popov else
98190b2c4fSTim J. Robbins return (vcp->vc_sopt.sv_maxtx - SMB_HDRLEN - 64);
99e2a70cffSBoris Popov }
100e2a70cffSBoris Popov
101681a5bbeSBoris Popov static int
smb_smb_nomux(struct smb_vc * vcp,struct smb_cred * scred,const char * name)102681a5bbeSBoris Popov smb_smb_nomux(struct smb_vc *vcp, struct smb_cred *scred, const char *name)
103681a5bbeSBoris Popov {
104fce6fbfaSBoris Popov if (scred->scr_td->td_proc == vcp->vc_iod->iod_p)
105681a5bbeSBoris Popov return 0;
106681a5bbeSBoris Popov SMBERROR("wrong function called(%s)\n", name);
107681a5bbeSBoris Popov return EINVAL;
108681a5bbeSBoris Popov }
109681a5bbeSBoris Popov
110681a5bbeSBoris Popov int
smb_smb_negotiate(struct smb_vc * vcp,struct smb_cred * scred)111681a5bbeSBoris Popov smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred)
112681a5bbeSBoris Popov {
113681a5bbeSBoris Popov struct smb_dialect *dp;
114681a5bbeSBoris Popov struct smb_sopt *sp = NULL;
115681a5bbeSBoris Popov struct smb_rq *rqp;
116681a5bbeSBoris Popov struct mbchain *mbp;
117681a5bbeSBoris Popov struct mdchain *mdp;
118681a5bbeSBoris Popov u_int8_t wc, stime[8], sblen;
119681a5bbeSBoris Popov u_int16_t dindex, tw, tw1, swlen, bc;
120681a5bbeSBoris Popov int error, maxqsz;
12141f1dcccSKevin Lo int unicode = SMB_UNICODE_STRINGS(vcp);
12241f1dcccSKevin Lo void * servercharset = vcp->vc_toserver;
12341f1dcccSKevin Lo void * localcharset = vcp->vc_tolocal;
124681a5bbeSBoris Popov
1256e551fb6SDavid E. O'Brien if (smb_smb_nomux(vcp, scred, __func__) != 0)
126681a5bbeSBoris Popov return EINVAL;
12741f1dcccSKevin Lo /* Disable Unicode for SMB_COM_NEGOTIATE requests */
12841f1dcccSKevin Lo if (unicode) {
12941f1dcccSKevin Lo vcp->vc_toserver = vcp->vc_cp_toserver;
13041f1dcccSKevin Lo vcp->vc_tolocal = vcp->vc_cp_tolocal;
13141f1dcccSKevin Lo }
132681a5bbeSBoris Popov vcp->vc_hflags = 0;
133681a5bbeSBoris Popov vcp->vc_hflags2 = 0;
134681a5bbeSBoris Popov vcp->obj.co_flags &= ~(SMBV_ENCRYPT);
135681a5bbeSBoris Popov sp = &vcp->vc_sopt;
136681a5bbeSBoris Popov bzero(sp, sizeof(struct smb_sopt));
137681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_NEGOTIATE, scred, &rqp);
138681a5bbeSBoris Popov if (error)
139681a5bbeSBoris Popov return error;
140681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp);
141681a5bbeSBoris Popov smb_rq_wstart(rqp);
142681a5bbeSBoris Popov smb_rq_wend(rqp);
143681a5bbeSBoris Popov smb_rq_bstart(rqp);
144681a5bbeSBoris Popov for(dp = smb_dialects; dp->d_id != -1; dp++) {
145681a5bbeSBoris Popov mb_put_uint8(mbp, SMB_DT_DIALECT);
146681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, dp->d_name, SMB_CS_NONE);
147681a5bbeSBoris Popov }
148681a5bbeSBoris Popov smb_rq_bend(rqp);
149681a5bbeSBoris Popov error = smb_rq_simple(rqp);
150681a5bbeSBoris Popov SMBSDEBUG("%d\n", error);
151681a5bbeSBoris Popov if (error)
152681a5bbeSBoris Popov goto bad;
153681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp);
154681a5bbeSBoris Popov do {
155681a5bbeSBoris Popov error = md_get_uint8(mdp, &wc);
156681a5bbeSBoris Popov if (error)
157681a5bbeSBoris Popov break;
158681a5bbeSBoris Popov error = md_get_uint16le(mdp, &dindex);
159681a5bbeSBoris Popov if (error)
160681a5bbeSBoris Popov break;
161681a5bbeSBoris Popov if (dindex > 7) {
162681a5bbeSBoris Popov SMBERROR("Don't know how to talk with server %s (%d)\n", "xxx", dindex);
163681a5bbeSBoris Popov error = EBADRPC;
164681a5bbeSBoris Popov break;
165681a5bbeSBoris Popov }
166681a5bbeSBoris Popov dp = smb_dialects + dindex;
167681a5bbeSBoris Popov sp->sv_proto = dp->d_id;
168681a5bbeSBoris Popov SMBSDEBUG("Dialect %s (%d, %d)\n", dp->d_name, dindex, wc);
169681a5bbeSBoris Popov error = EBADRPC;
170681a5bbeSBoris Popov if (dp->d_id >= SMB_DIALECT_NTLM0_12) {
171681a5bbeSBoris Popov if (wc != 17)
172681a5bbeSBoris Popov break;
173681a5bbeSBoris Popov md_get_uint8(mdp, &sp->sv_sm);
174681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxmux);
175681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxvcs);
176681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_maxtx);
177681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_maxraw);
178681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_skey);
179681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_caps);
180681a5bbeSBoris Popov md_get_mem(mdp, stime, 8, MB_MSYSTEM);
181681a5bbeSBoris Popov md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz);
182681a5bbeSBoris Popov md_get_uint8(mdp, &sblen);
183681a5bbeSBoris Popov if (sblen && (sp->sv_sm & SMB_SM_ENCRYPT)) {
184681a5bbeSBoris Popov if (sblen != SMB_MAXCHALLENGELEN) {
185681a5bbeSBoris Popov SMBERROR("Unexpected length of security blob (%d)\n", sblen);
186681a5bbeSBoris Popov break;
187681a5bbeSBoris Popov }
18841f1dcccSKevin Lo error = md_get_uint16le(mdp, &bc);
189681a5bbeSBoris Popov if (error)
190681a5bbeSBoris Popov break;
191681a5bbeSBoris Popov if (sp->sv_caps & SMB_CAP_EXT_SECURITY)
192681a5bbeSBoris Popov md_get_mem(mdp, NULL, 16, MB_MSYSTEM);
193681a5bbeSBoris Popov error = md_get_mem(mdp, vcp->vc_ch, sblen, MB_MSYSTEM);
194681a5bbeSBoris Popov if (error)
195681a5bbeSBoris Popov break;
196681a5bbeSBoris Popov vcp->vc_chlen = sblen;
197681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_ENCRYPT;
198681a5bbeSBoris Popov }
199190b2c4fSTim J. Robbins if (sp->sv_sm & SMB_SM_SIGS_REQUIRE)
200190b2c4fSTim J. Robbins vcp->vc_hflags2 |= SMB_FLAGS2_SECURITY_SIGNATURE;
20141f1dcccSKevin Lo if (vcp->vc_ucs_toserver &&
20241f1dcccSKevin Lo sp->sv_caps & SMB_CAP_UNICODE) {
20341f1dcccSKevin Lo /*
20441f1dcccSKevin Lo * They do Unicode.
20541f1dcccSKevin Lo */
20641f1dcccSKevin Lo vcp->obj.co_flags |= SMBV_UNICODE;
20741f1dcccSKevin Lo }
208681a5bbeSBoris Popov vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES;
209681a5bbeSBoris Popov if (dp->d_id == SMB_DIALECT_NTLM0_12 &&
210681a5bbeSBoris Popov sp->sv_maxtx < 4096 &&
211681a5bbeSBoris Popov (sp->sv_caps & SMB_CAP_NT_SMBS) == 0) {
212681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_WIN95;
213681a5bbeSBoris Popov SMBSDEBUG("Win95 detected\n");
214681a5bbeSBoris Popov }
21541f1dcccSKevin Lo error = 0;
21641f1dcccSKevin Lo break;
21741f1dcccSKevin Lo }
21841f1dcccSKevin Lo vcp->vc_hflags2 &= ~(SMB_FLAGS2_EXT_SEC|SMB_FLAGS2_DFS|
21941f1dcccSKevin Lo SMB_FLAGS2_ERR_STATUS|SMB_FLAGS2_UNICODE);
22041f1dcccSKevin Lo unicode = 0;
22141f1dcccSKevin Lo if (dp->d_id > SMB_DIALECT_CORE) {
222681a5bbeSBoris Popov md_get_uint16le(mdp, &tw);
223681a5bbeSBoris Popov sp->sv_sm = tw;
224681a5bbeSBoris Popov md_get_uint16le(mdp, &tw);
225681a5bbeSBoris Popov sp->sv_maxtx = tw;
226681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxmux);
227681a5bbeSBoris Popov md_get_uint16le(mdp, &sp->sv_maxvcs);
228681a5bbeSBoris Popov md_get_uint16le(mdp, &tw); /* rawmode */
229681a5bbeSBoris Popov md_get_uint32le(mdp, &sp->sv_skey);
230681a5bbeSBoris Popov if (wc == 13) { /* >= LANMAN1 */
231681a5bbeSBoris Popov md_get_uint16(mdp, &tw); /* time */
232681a5bbeSBoris Popov md_get_uint16(mdp, &tw1); /* date */
233681a5bbeSBoris Popov md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz);
234681a5bbeSBoris Popov md_get_uint16le(mdp, &swlen);
235681a5bbeSBoris Popov if (swlen > SMB_MAXCHALLENGELEN)
236681a5bbeSBoris Popov break;
237681a5bbeSBoris Popov md_get_uint16(mdp, NULL); /* mbz */
23841f1dcccSKevin Lo if (md_get_uint16le(mdp, &bc) != 0)
239681a5bbeSBoris Popov break;
240681a5bbeSBoris Popov if (bc < swlen)
241681a5bbeSBoris Popov break;
242681a5bbeSBoris Popov if (swlen && (sp->sv_sm & SMB_SM_ENCRYPT)) {
243681a5bbeSBoris Popov error = md_get_mem(mdp, vcp->vc_ch, swlen, MB_MSYSTEM);
244681a5bbeSBoris Popov if (error)
245681a5bbeSBoris Popov break;
246681a5bbeSBoris Popov vcp->vc_chlen = swlen;
247681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_ENCRYPT;
248681a5bbeSBoris Popov }
249681a5bbeSBoris Popov }
250681a5bbeSBoris Popov vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES;
251681a5bbeSBoris Popov } else { /* an old CORE protocol */
252681a5bbeSBoris Popov sp->sv_maxmux = 1;
253681a5bbeSBoris Popov }
254681a5bbeSBoris Popov error = 0;
255681a5bbeSBoris Popov } while (0);
256681a5bbeSBoris Popov if (error == 0) {
257681a5bbeSBoris Popov vcp->vc_maxvcs = sp->sv_maxvcs;
258681a5bbeSBoris Popov if (vcp->vc_maxvcs <= 1) {
259681a5bbeSBoris Popov if (vcp->vc_maxvcs == 0)
260681a5bbeSBoris Popov vcp->vc_maxvcs = 1;
261681a5bbeSBoris Popov }
262681a5bbeSBoris Popov if (sp->sv_maxtx <= 0 || sp->sv_maxtx > 0xffff)
263681a5bbeSBoris Popov sp->sv_maxtx = 1024;
264e2a70cffSBoris Popov else
265e2a70cffSBoris Popov sp->sv_maxtx = min(sp->sv_maxtx,
266e2a70cffSBoris Popov 63*1024 + SMB_HDRLEN + 16);
267e2a70cffSBoris Popov SMB_TRAN_GETPARAM(vcp, SMBTP_RCVSZ, &maxqsz);
268e2a70cffSBoris Popov vcp->vc_rxmax = min(smb_vc_maxread(vcp), maxqsz - 1024);
269681a5bbeSBoris Popov SMB_TRAN_GETPARAM(vcp, SMBTP_SNDSZ, &maxqsz);
270e2a70cffSBoris Popov vcp->vc_wxmax = min(smb_vc_maxwrite(vcp), maxqsz - 1024);
271681a5bbeSBoris Popov vcp->vc_txmax = min(sp->sv_maxtx, maxqsz);
272681a5bbeSBoris Popov SMBSDEBUG("TZ = %d\n", sp->sv_tz);
273681a5bbeSBoris Popov SMBSDEBUG("CAPS = %x\n", sp->sv_caps);
274681a5bbeSBoris Popov SMBSDEBUG("MAXMUX = %d\n", sp->sv_maxmux);
275681a5bbeSBoris Popov SMBSDEBUG("MAXVCS = %d\n", sp->sv_maxvcs);
276681a5bbeSBoris Popov SMBSDEBUG("MAXRAW = %d\n", sp->sv_maxraw);
277681a5bbeSBoris Popov SMBSDEBUG("MAXTX = %d\n", sp->sv_maxtx);
278681a5bbeSBoris Popov }
279681a5bbeSBoris Popov bad:
28041f1dcccSKevin Lo /* Restore Unicode conversion state */
28141f1dcccSKevin Lo if (unicode) {
28241f1dcccSKevin Lo vcp->vc_toserver = servercharset;
28341f1dcccSKevin Lo vcp->vc_tolocal = localcharset;
28441f1dcccSKevin Lo vcp->vc_hflags2 |= SMB_FLAGS2_UNICODE;
28541f1dcccSKevin Lo }
286681a5bbeSBoris Popov smb_rq_done(rqp);
287681a5bbeSBoris Popov return error;
288681a5bbeSBoris Popov }
289681a5bbeSBoris Popov
290681a5bbeSBoris Popov int
smb_smb_ssnsetup(struct smb_vc * vcp,struct smb_cred * scred)291681a5bbeSBoris Popov smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred)
292681a5bbeSBoris Popov {
293681a5bbeSBoris Popov struct smb_rq *rqp;
294681a5bbeSBoris Popov struct mbchain *mbp;
295681a5bbeSBoris Popov /* u_int8_t wc;
296681a5bbeSBoris Popov u_int16_t tw, tw1;*/
297681a5bbeSBoris Popov smb_uniptr unipp, ntencpass = NULL;
298681a5bbeSBoris Popov char *pp, *up, *pbuf, *encpass;
29970f9b9d9SBoris Popov int error, plen, uniplen, ulen, upper;
30041f1dcccSKevin Lo u_int32_t caps = 0;
30170f9b9d9SBoris Popov
30270f9b9d9SBoris Popov upper = 0;
30370f9b9d9SBoris Popov
30441f1dcccSKevin Lo if (vcp->obj.co_flags & SMBV_UNICODE)
30541f1dcccSKevin Lo caps |= SMB_CAP_UNICODE;
30641f1dcccSKevin Lo
30770f9b9d9SBoris Popov again:
308681a5bbeSBoris Popov
309681a5bbeSBoris Popov vcp->vc_smbuid = SMB_UID_UNKNOWN;
310681a5bbeSBoris Popov
3116e551fb6SDavid E. O'Brien if (smb_smb_nomux(vcp, scred, __func__) != 0)
312681a5bbeSBoris Popov return EINVAL;
313681a5bbeSBoris Popov
314681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_SESSION_SETUP_ANDX, scred, &rqp);
315681a5bbeSBoris Popov if (error)
316681a5bbeSBoris Popov return error;
317a163d034SWarner Losh pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK);
318a163d034SWarner Losh encpass = malloc(24, M_SMBTEMP, M_WAITOK);
319681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_USER) {
32070f9b9d9SBoris Popov /*
32170f9b9d9SBoris Popov * We try w/o uppercasing first so Samba mixed case
32270f9b9d9SBoris Popov * passwords work. If that fails we come back and try
32370f9b9d9SBoris Popov * uppercasing to satisfy OS/2 and Windows for Workgroups.
32470f9b9d9SBoris Popov */
32570f9b9d9SBoris Popov if (upper++) {
32670f9b9d9SBoris Popov iconv_convstr(vcp->vc_toupper, pbuf,
32770f9b9d9SBoris Popov smb_vc_getpass(vcp)/*, SMB_MAXPASSWORDLEN*/);
32870f9b9d9SBoris Popov } else {
32970f9b9d9SBoris Popov strncpy(pbuf, smb_vc_getpass(vcp), SMB_MAXPASSWORDLEN);
33070f9b9d9SBoris Popov pbuf[SMB_MAXPASSWORDLEN] = '\0';
33170f9b9d9SBoris Popov }
33270f9b9d9SBoris Popov if (!SMB_UNICODE_STRINGS(vcp))
33370f9b9d9SBoris Popov iconv_convstr(vcp->vc_toserver, pbuf, pbuf/*,
33470f9b9d9SBoris Popov SMB_MAXPASSWORDLEN*/);
33570f9b9d9SBoris Popov
336681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) {
337681a5bbeSBoris Popov uniplen = plen = 24;
338681a5bbeSBoris Popov smb_encrypt(pbuf, vcp->vc_ch, encpass);
339a163d034SWarner Losh ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK);
34070f9b9d9SBoris Popov if (SMB_UNICODE_STRINGS(vcp)) {
34170f9b9d9SBoris Popov strncpy(pbuf, smb_vc_getpass(vcp),
34270f9b9d9SBoris Popov SMB_MAXPASSWORDLEN);
34370f9b9d9SBoris Popov pbuf[SMB_MAXPASSWORDLEN] = '\0';
34470f9b9d9SBoris Popov } else
34570f9b9d9SBoris Popov iconv_convstr(vcp->vc_toserver, pbuf,
34670f9b9d9SBoris Popov smb_vc_getpass(vcp)/*,
34770f9b9d9SBoris Popov SMB_MAXPASSWORDLEN*/);
348681a5bbeSBoris Popov smb_ntencrypt(pbuf, vcp->vc_ch, (u_char*)ntencpass);
349681a5bbeSBoris Popov pp = encpass;
350681a5bbeSBoris Popov unipp = ntencpass;
351681a5bbeSBoris Popov } else {
352681a5bbeSBoris Popov plen = strlen(pbuf) + 1;
353681a5bbeSBoris Popov pp = pbuf;
354681a5bbeSBoris Popov uniplen = plen * 2;
355a163d034SWarner Losh ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK);
356681a5bbeSBoris Popov smb_strtouni(ntencpass, smb_vc_getpass(vcp));
357681a5bbeSBoris Popov plen--;
358daa35dedSBoris Popov
359daa35dedSBoris Popov /*
360daa35dedSBoris Popov * The uniplen is zeroed because Samba cannot deal
361daa35dedSBoris Popov * with this 2nd cleartext password. This Samba
362daa35dedSBoris Popov * "bug" is actually a workaround for problems in
363daa35dedSBoris Popov * Microsoft clients.
364daa35dedSBoris Popov */
365681a5bbeSBoris Popov uniplen = 0/*-= 2*/;
366681a5bbeSBoris Popov unipp = ntencpass;
367681a5bbeSBoris Popov }
368681a5bbeSBoris Popov } else {
369681a5bbeSBoris Popov /*
370681a5bbeSBoris Popov * In the share security mode password will be used
371681a5bbeSBoris Popov * only in the tree authentication
372681a5bbeSBoris Popov */
373681a5bbeSBoris Popov pp = "";
374681a5bbeSBoris Popov plen = 1;
375681a5bbeSBoris Popov unipp = &smb_unieol;
376cf37bfb0SMax Khon uniplen = 0 /* sizeof(smb_unieol) */;
377681a5bbeSBoris Popov }
378681a5bbeSBoris Popov smb_rq_wstart(rqp);
379681a5bbeSBoris Popov mbp = &rqp->sr_rq;
380681a5bbeSBoris Popov up = vcp->vc_username;
381681a5bbeSBoris Popov ulen = strlen(up) + 1;
38270f9b9d9SBoris Popov /*
38370f9b9d9SBoris Popov * If userid is null we are attempting anonymous browse login
38470f9b9d9SBoris Popov * so passwords must be zero length.
38570f9b9d9SBoris Popov */
38670f9b9d9SBoris Popov if (ulen == 1)
38770f9b9d9SBoris Popov plen = uniplen = 0;
388681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff);
389681a5bbeSBoris Popov mb_put_uint8(mbp, 0);
390681a5bbeSBoris Popov mb_put_uint16le(mbp, 0);
391681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxtx);
392681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxmux);
393681a5bbeSBoris Popov mb_put_uint16le(mbp, vcp->vc_number);
394681a5bbeSBoris Popov mb_put_uint32le(mbp, vcp->vc_sopt.sv_skey);
395681a5bbeSBoris Popov mb_put_uint16le(mbp, plen);
396681a5bbeSBoris Popov if (SMB_DIALECT(vcp) < SMB_DIALECT_NTLM0_12) {
397681a5bbeSBoris Popov mb_put_uint32le(mbp, 0);
398681a5bbeSBoris Popov smb_rq_wend(rqp);
399681a5bbeSBoris Popov smb_rq_bstart(rqp);
400681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
401681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, up, SMB_CS_NONE);
402681a5bbeSBoris Popov } else {
403681a5bbeSBoris Popov mb_put_uint16le(mbp, uniplen);
404681a5bbeSBoris Popov mb_put_uint32le(mbp, 0); /* reserved */
40541f1dcccSKevin Lo mb_put_uint32le(mbp, caps);
406681a5bbeSBoris Popov smb_rq_wend(rqp);
407681a5bbeSBoris Popov smb_rq_bstart(rqp);
408681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
409681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)unipp, uniplen, MB_MSYSTEM);
410681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, up, SMB_CS_NONE); /* AccountName */
411681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, vcp->vc_domain, SMB_CS_NONE); /* PrimaryDomain */
412681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, "FreeBSD", SMB_CS_NONE); /* Client's OS */
413681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, "NETSMB", SMB_CS_NONE); /* Client name */
414681a5bbeSBoris Popov }
415681a5bbeSBoris Popov smb_rq_bend(rqp);
416681a5bbeSBoris Popov if (ntencpass)
417681a5bbeSBoris Popov free(ntencpass, M_SMBTEMP);
418190b2c4fSTim J. Robbins if (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE)
419190b2c4fSTim J. Robbins smb_calcmackey(vcp);
420681a5bbeSBoris Popov error = smb_rq_simple(rqp);
421681a5bbeSBoris Popov SMBSDEBUG("%d\n", error);
422681a5bbeSBoris Popov if (error) {
423681a5bbeSBoris Popov if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnoaccess)
424681a5bbeSBoris Popov error = EAUTH;
425681a5bbeSBoris Popov goto bad;
426681a5bbeSBoris Popov }
427681a5bbeSBoris Popov vcp->vc_smbuid = rqp->sr_rpuid;
428681a5bbeSBoris Popov bad:
429681a5bbeSBoris Popov free(encpass, M_SMBTEMP);
430681a5bbeSBoris Popov free(pbuf, M_SMBTEMP);
431681a5bbeSBoris Popov smb_rq_done(rqp);
43270f9b9d9SBoris Popov if (error && upper == 1 && vcp->vc_sopt.sv_sm & SMB_SM_USER)
43370f9b9d9SBoris Popov goto again;
434681a5bbeSBoris Popov return error;
435681a5bbeSBoris Popov }
436681a5bbeSBoris Popov
437681a5bbeSBoris Popov int
smb_smb_ssnclose(struct smb_vc * vcp,struct smb_cred * scred)438681a5bbeSBoris Popov smb_smb_ssnclose(struct smb_vc *vcp, struct smb_cred *scred)
439681a5bbeSBoris Popov {
440681a5bbeSBoris Popov struct smb_rq *rqp;
441681a5bbeSBoris Popov struct mbchain *mbp;
442681a5bbeSBoris Popov int error;
443681a5bbeSBoris Popov
444681a5bbeSBoris Popov if (vcp->vc_smbuid == SMB_UID_UNKNOWN)
445681a5bbeSBoris Popov return 0;
446681a5bbeSBoris Popov
4476e551fb6SDavid E. O'Brien if (smb_smb_nomux(vcp, scred, __func__) != 0)
448681a5bbeSBoris Popov return EINVAL;
449681a5bbeSBoris Popov
450681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_LOGOFF_ANDX, scred, &rqp);
451681a5bbeSBoris Popov if (error)
452681a5bbeSBoris Popov return error;
453681a5bbeSBoris Popov mbp = &rqp->sr_rq;
454681a5bbeSBoris Popov smb_rq_wstart(rqp);
455681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff);
456681a5bbeSBoris Popov mb_put_uint8(mbp, 0);
457681a5bbeSBoris Popov mb_put_uint16le(mbp, 0);
458681a5bbeSBoris Popov smb_rq_wend(rqp);
459681a5bbeSBoris Popov smb_rq_bstart(rqp);
460681a5bbeSBoris Popov smb_rq_bend(rqp);
461681a5bbeSBoris Popov error = smb_rq_simple(rqp);
462681a5bbeSBoris Popov SMBSDEBUG("%d\n", error);
463681a5bbeSBoris Popov smb_rq_done(rqp);
464681a5bbeSBoris Popov return error;
465681a5bbeSBoris Popov }
466681a5bbeSBoris Popov
467681a5bbeSBoris Popov static char smb_any_share[] = "?????";
468681a5bbeSBoris Popov
469681a5bbeSBoris Popov static char *
smb_share_typename(int stype)470681a5bbeSBoris Popov smb_share_typename(int stype)
471681a5bbeSBoris Popov {
472681a5bbeSBoris Popov char *pp;
473681a5bbeSBoris Popov
474681a5bbeSBoris Popov switch (stype) {
475681a5bbeSBoris Popov case SMB_ST_DISK:
476681a5bbeSBoris Popov pp = "A:";
477681a5bbeSBoris Popov break;
478681a5bbeSBoris Popov case SMB_ST_PRINTER:
479681a5bbeSBoris Popov pp = smb_any_share; /* can't use LPT: here... */
480681a5bbeSBoris Popov break;
481681a5bbeSBoris Popov case SMB_ST_PIPE:
482681a5bbeSBoris Popov pp = "IPC";
483681a5bbeSBoris Popov break;
484681a5bbeSBoris Popov case SMB_ST_COMM:
485681a5bbeSBoris Popov pp = "COMM";
486681a5bbeSBoris Popov break;
487681a5bbeSBoris Popov case SMB_ST_ANY:
488681a5bbeSBoris Popov default:
489681a5bbeSBoris Popov pp = smb_any_share;
490681a5bbeSBoris Popov break;
491681a5bbeSBoris Popov }
492681a5bbeSBoris Popov return pp;
493681a5bbeSBoris Popov }
494681a5bbeSBoris Popov
495681a5bbeSBoris Popov int
smb_smb_treeconnect(struct smb_share * ssp,struct smb_cred * scred)496681a5bbeSBoris Popov smb_smb_treeconnect(struct smb_share *ssp, struct smb_cred *scred)
497681a5bbeSBoris Popov {
498681a5bbeSBoris Popov struct smb_vc *vcp;
499681a5bbeSBoris Popov struct smb_rq rq, *rqp = &rq;
500681a5bbeSBoris Popov struct mbchain *mbp;
501681a5bbeSBoris Popov char *pp, *pbuf, *encpass;
50270f9b9d9SBoris Popov int error, plen, caseopt, upper;
50370f9b9d9SBoris Popov
50470f9b9d9SBoris Popov upper = 0;
50570f9b9d9SBoris Popov
50670f9b9d9SBoris Popov again:
50770f9b9d9SBoris Popov /* Disable Unicode for SMB_COM_TREE_CONNECT_ANDX requests */
50870f9b9d9SBoris Popov if (SSTOVC(ssp)->vc_hflags2 & SMB_FLAGS2_UNICODE) {
50970f9b9d9SBoris Popov vcp = SSTOVC(ssp);
51041f1dcccSKevin Lo vcp->vc_toserver = vcp->vc_cp_toserver;
51141f1dcccSKevin Lo vcp->vc_tolocal = vcp->vc_cp_tolocal;
51270f9b9d9SBoris Popov vcp->vc_hflags2 &= ~SMB_FLAGS2_UNICODE;
51370f9b9d9SBoris Popov }
514681a5bbeSBoris Popov
515681a5bbeSBoris Popov ssp->ss_tid = SMB_TID_UNKNOWN;
516681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_CONNECT_ANDX, scred, &rqp);
517681a5bbeSBoris Popov if (error)
518681a5bbeSBoris Popov return error;
519681a5bbeSBoris Popov vcp = rqp->sr_vc;
520681a5bbeSBoris Popov caseopt = SMB_CS_NONE;
521681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_USER) {
522681a5bbeSBoris Popov plen = 1;
523681a5bbeSBoris Popov pp = "";
524681a5bbeSBoris Popov pbuf = NULL;
525681a5bbeSBoris Popov encpass = NULL;
526681a5bbeSBoris Popov } else {
527a163d034SWarner Losh pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK);
528a163d034SWarner Losh encpass = malloc(24, M_SMBTEMP, M_WAITOK);
52970f9b9d9SBoris Popov /*
53070f9b9d9SBoris Popov * We try w/o uppercasing first so Samba mixed case
53170f9b9d9SBoris Popov * passwords work. If that fails we come back and try
53270f9b9d9SBoris Popov * uppercasing to satisfy OS/2 and Windows for Workgroups.
53370f9b9d9SBoris Popov */
53470f9b9d9SBoris Popov if (upper++) {
53570f9b9d9SBoris Popov iconv_convstr(vcp->vc_toupper, pbuf,
53670f9b9d9SBoris Popov smb_share_getpass(ssp)/*,
53770f9b9d9SBoris Popov SMB_MAXPASSWORDLEN*/);
53870f9b9d9SBoris Popov } else {
53970f9b9d9SBoris Popov strncpy(pbuf, smb_share_getpass(ssp),
54070f9b9d9SBoris Popov SMB_MAXPASSWORDLEN);
54170f9b9d9SBoris Popov pbuf[SMB_MAXPASSWORDLEN] = '\0';
54270f9b9d9SBoris Popov }
543681a5bbeSBoris Popov if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) {
544681a5bbeSBoris Popov plen = 24;
545681a5bbeSBoris Popov smb_encrypt(pbuf, vcp->vc_ch, encpass);
546681a5bbeSBoris Popov pp = encpass;
547681a5bbeSBoris Popov } else {
548681a5bbeSBoris Popov plen = strlen(pbuf) + 1;
549681a5bbeSBoris Popov pp = pbuf;
550681a5bbeSBoris Popov }
551681a5bbeSBoris Popov }
552681a5bbeSBoris Popov mbp = &rqp->sr_rq;
553681a5bbeSBoris Popov smb_rq_wstart(rqp);
554681a5bbeSBoris Popov mb_put_uint8(mbp, 0xff);
555681a5bbeSBoris Popov mb_put_uint8(mbp, 0);
556681a5bbeSBoris Popov mb_put_uint16le(mbp, 0);
557681a5bbeSBoris Popov mb_put_uint16le(mbp, 0); /* Flags */
558681a5bbeSBoris Popov mb_put_uint16le(mbp, plen);
559681a5bbeSBoris Popov smb_rq_wend(rqp);
560681a5bbeSBoris Popov smb_rq_bstart(rqp);
561681a5bbeSBoris Popov mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
562681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, "\\\\", 2, caseopt);
563681a5bbeSBoris Popov pp = vcp->vc_srvname;
564681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, pp, strlen(pp), caseopt);
565681a5bbeSBoris Popov smb_put_dmem(mbp, vcp, "\\", 1, caseopt);
566681a5bbeSBoris Popov pp = ssp->ss_name;
567681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, pp, caseopt);
568681a5bbeSBoris Popov pp = smb_share_typename(ssp->ss_type);
569681a5bbeSBoris Popov smb_put_dstring(mbp, vcp, pp, caseopt);
570681a5bbeSBoris Popov smb_rq_bend(rqp);
571681a5bbeSBoris Popov error = smb_rq_simple(rqp);
572681a5bbeSBoris Popov SMBSDEBUG("%d\n", error);
573681a5bbeSBoris Popov if (error)
574681a5bbeSBoris Popov goto bad;
575681a5bbeSBoris Popov ssp->ss_tid = rqp->sr_rptid;
576681a5bbeSBoris Popov ssp->ss_vcgenid = vcp->vc_genid;
577681a5bbeSBoris Popov ssp->ss_flags |= SMBS_CONNECTED;
57841f1dcccSKevin Lo /*
57941f1dcccSKevin Lo * If the server can speak Unicode then switch
58041f1dcccSKevin Lo * our converters to do Unicode <--> Local
58141f1dcccSKevin Lo */
58241f1dcccSKevin Lo if (vcp->obj.co_flags & SMBV_UNICODE) {
58341f1dcccSKevin Lo vcp->vc_toserver = vcp->vc_ucs_toserver;
58441f1dcccSKevin Lo vcp->vc_tolocal = vcp->vc_ucs_tolocal;
58541f1dcccSKevin Lo vcp->vc_hflags2 |= SMB_FLAGS2_UNICODE;
58641f1dcccSKevin Lo }
587681a5bbeSBoris Popov bad:
588681a5bbeSBoris Popov if (encpass)
589681a5bbeSBoris Popov free(encpass, M_SMBTEMP);
590681a5bbeSBoris Popov if (pbuf)
591681a5bbeSBoris Popov free(pbuf, M_SMBTEMP);
592681a5bbeSBoris Popov smb_rq_done(rqp);
59370f9b9d9SBoris Popov if (error && upper == 1)
59470f9b9d9SBoris Popov goto again;
595681a5bbeSBoris Popov return error;
596681a5bbeSBoris Popov }
597681a5bbeSBoris Popov
598681a5bbeSBoris Popov int
smb_smb_treedisconnect(struct smb_share * ssp,struct smb_cred * scred)599681a5bbeSBoris Popov smb_smb_treedisconnect(struct smb_share *ssp, struct smb_cred *scred)
600681a5bbeSBoris Popov {
601681a5bbeSBoris Popov struct smb_rq *rqp;
602681a5bbeSBoris Popov int error;
603681a5bbeSBoris Popov
604681a5bbeSBoris Popov if (ssp->ss_tid == SMB_TID_UNKNOWN)
605681a5bbeSBoris Popov return 0;
606681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_DISCONNECT, scred, &rqp);
607681a5bbeSBoris Popov if (error)
608681a5bbeSBoris Popov return error;
609681a5bbeSBoris Popov smb_rq_wstart(rqp);
610681a5bbeSBoris Popov smb_rq_wend(rqp);
611681a5bbeSBoris Popov smb_rq_bstart(rqp);
612681a5bbeSBoris Popov smb_rq_bend(rqp);
613681a5bbeSBoris Popov error = smb_rq_simple(rqp);
614681a5bbeSBoris Popov SMBSDEBUG("%d\n", error);
615681a5bbeSBoris Popov smb_rq_done(rqp);
616681a5bbeSBoris Popov ssp->ss_tid = SMB_TID_UNKNOWN;
617681a5bbeSBoris Popov return error;
618681a5bbeSBoris Popov }
619681a5bbeSBoris Popov
620681a5bbeSBoris Popov static __inline int
smb_smb_readx(struct smb_share * ssp,u_int16_t fid,int * len,int * rresid,struct uio * uio,struct smb_cred * scred)621e2a70cffSBoris Popov smb_smb_readx(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
622e2a70cffSBoris Popov struct uio *uio, struct smb_cred *scred)
623e2a70cffSBoris Popov {
624e2a70cffSBoris Popov struct smb_rq *rqp;
625e2a70cffSBoris Popov struct mbchain *mbp;
626e2a70cffSBoris Popov struct mdchain *mdp;
627e2a70cffSBoris Popov u_int8_t wc;
628e2a70cffSBoris Popov int error;
629e2a70cffSBoris Popov u_int16_t residhi, residlo, off, doff;
630e2a70cffSBoris Popov u_int32_t resid;
631e2a70cffSBoris Popov
632e2a70cffSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ_ANDX, scred, &rqp);
633e2a70cffSBoris Popov if (error)
634e2a70cffSBoris Popov return error;
635e2a70cffSBoris Popov smb_rq_getrequest(rqp, &mbp);
636e2a70cffSBoris Popov smb_rq_wstart(rqp);
637e2a70cffSBoris Popov mb_put_uint8(mbp, 0xff); /* no secondary command */
638e2a70cffSBoris Popov mb_put_uint8(mbp, 0); /* MBZ */
639e2a70cffSBoris Popov mb_put_uint16le(mbp, 0); /* offset to secondary */
640e2a70cffSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
641e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset);
642e2a70cffSBoris Popov *len = min(SSTOVC(ssp)->vc_rxmax, *len);
643e2a70cffSBoris Popov mb_put_uint16le(mbp, *len); /* MaxCount */
644e2a70cffSBoris Popov mb_put_uint16le(mbp, *len); /* MinCount (only indicates blocking) */
645e2a70cffSBoris Popov mb_put_uint32le(mbp, (unsigned)*len >> 16); /* MaxCountHigh */
646e2a70cffSBoris Popov mb_put_uint16le(mbp, *len); /* Remaining ("obsolete") */
647e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */
648e2a70cffSBoris Popov smb_rq_wend(rqp);
649e2a70cffSBoris Popov smb_rq_bstart(rqp);
650e2a70cffSBoris Popov smb_rq_bend(rqp);
651e2a70cffSBoris Popov do {
652e2a70cffSBoris Popov error = smb_rq_simple(rqp);
653e2a70cffSBoris Popov if (error)
654e2a70cffSBoris Popov break;
655e2a70cffSBoris Popov smb_rq_getreply(rqp, &mdp);
656e2a70cffSBoris Popov off = SMB_HDRLEN;
657e2a70cffSBoris Popov md_get_uint8(mdp, &wc);
658e2a70cffSBoris Popov off++;
659e2a70cffSBoris Popov if (wc != 12) {
660e2a70cffSBoris Popov error = EBADRPC;
661e2a70cffSBoris Popov break;
662e2a70cffSBoris Popov }
663e2a70cffSBoris Popov md_get_uint8(mdp, NULL);
664e2a70cffSBoris Popov off++;
665e2a70cffSBoris Popov md_get_uint8(mdp, NULL);
666e2a70cffSBoris Popov off++;
667e2a70cffSBoris Popov md_get_uint16le(mdp, NULL);
668e2a70cffSBoris Popov off += 2;
669e2a70cffSBoris Popov md_get_uint16le(mdp, NULL);
670e2a70cffSBoris Popov off += 2;
671e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); /* data compaction mode */
672e2a70cffSBoris Popov off += 2;
673e2a70cffSBoris Popov md_get_uint16le(mdp, NULL);
674e2a70cffSBoris Popov off += 2;
675e2a70cffSBoris Popov md_get_uint16le(mdp, &residlo);
676e2a70cffSBoris Popov off += 2;
677e2a70cffSBoris Popov md_get_uint16le(mdp, &doff); /* data offset */
678e2a70cffSBoris Popov off += 2;
679e2a70cffSBoris Popov md_get_uint16le(mdp, &residhi);
680e2a70cffSBoris Popov off += 2;
681e2a70cffSBoris Popov resid = (residhi << 16) | residlo;
682e2a70cffSBoris Popov md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM);
683e2a70cffSBoris Popov off += 4*2;
684e2a70cffSBoris Popov md_get_uint16le(mdp, NULL); /* ByteCount */
685e2a70cffSBoris Popov off += 2;
686e2a70cffSBoris Popov if (doff > off) /* pad byte(s)? */
687e2a70cffSBoris Popov md_get_mem(mdp, NULL, doff - off, MB_MSYSTEM);
688e2a70cffSBoris Popov if (resid == 0) {
689e2a70cffSBoris Popov *rresid = resid;
690e2a70cffSBoris Popov break;
691e2a70cffSBoris Popov }
692e2a70cffSBoris Popov error = md_get_uio(mdp, uio, resid);
693e2a70cffSBoris Popov if (error)
694e2a70cffSBoris Popov break;
695e2a70cffSBoris Popov *rresid = resid;
696e2a70cffSBoris Popov } while(0);
697e2a70cffSBoris Popov smb_rq_done(rqp);
698e2a70cffSBoris Popov return (error);
699e2a70cffSBoris Popov }
700e2a70cffSBoris Popov
701e2a70cffSBoris Popov static __inline int
smb_smb_writex(struct smb_share * ssp,u_int16_t fid,int * len,int * rresid,struct uio * uio,struct smb_cred * scred)702e2a70cffSBoris Popov smb_smb_writex(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
703e2a70cffSBoris Popov struct uio *uio, struct smb_cred *scred)
704e2a70cffSBoris Popov {
705e2a70cffSBoris Popov struct smb_rq *rqp;
706e2a70cffSBoris Popov struct mbchain *mbp;
707e2a70cffSBoris Popov struct mdchain *mdp;
708e2a70cffSBoris Popov int error;
709e2a70cffSBoris Popov u_int8_t wc;
710e2a70cffSBoris Popov u_int16_t resid;
711e2a70cffSBoris Popov
712e2a70cffSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE_ANDX, scred, &rqp);
713e2a70cffSBoris Popov if (error)
714e2a70cffSBoris Popov return (error);
715e2a70cffSBoris Popov smb_rq_getrequest(rqp, &mbp);
716e2a70cffSBoris Popov smb_rq_wstart(rqp);
717e2a70cffSBoris Popov mb_put_uint8(mbp, 0xff); /* no secondary command */
718e2a70cffSBoris Popov mb_put_uint8(mbp, 0); /* MBZ */
719e2a70cffSBoris Popov mb_put_uint16le(mbp, 0); /* offset to secondary */
720e2a70cffSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
721e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset);
722e2a70cffSBoris Popov mb_put_uint32le(mbp, 0); /* MBZ (timeout) */
723e2a70cffSBoris Popov mb_put_uint16le(mbp, 0); /* !write-thru */
724e2a70cffSBoris Popov mb_put_uint16le(mbp, 0);
725e2a70cffSBoris Popov *len = min(SSTOVC(ssp)->vc_wxmax, *len);
726e2a70cffSBoris Popov mb_put_uint16le(mbp, (unsigned)*len >> 16);
727e2a70cffSBoris Popov mb_put_uint16le(mbp, *len);
728e2a70cffSBoris Popov mb_put_uint16le(mbp, 64); /* data offset from header start */
729e2a70cffSBoris Popov mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */
730e2a70cffSBoris Popov smb_rq_wend(rqp);
731e2a70cffSBoris Popov smb_rq_bstart(rqp);
732e2a70cffSBoris Popov do {
733e2a70cffSBoris Popov mb_put_uint8(mbp, 0xee); /* mimic xp pad byte! */
734e2a70cffSBoris Popov error = mb_put_uio(mbp, uio, *len);
735e2a70cffSBoris Popov if (error)
736e2a70cffSBoris Popov break;
737e2a70cffSBoris Popov smb_rq_bend(rqp);
738e2a70cffSBoris Popov error = smb_rq_simple(rqp);
739e2a70cffSBoris Popov if (error)
740e2a70cffSBoris Popov break;
741e2a70cffSBoris Popov smb_rq_getreply(rqp, &mdp);
742e2a70cffSBoris Popov md_get_uint8(mdp, &wc);
743e2a70cffSBoris Popov if (wc != 6) {
744e2a70cffSBoris Popov error = EBADRPC;
745e2a70cffSBoris Popov break;
746e2a70cffSBoris Popov }
747e2a70cffSBoris Popov md_get_uint8(mdp, NULL);
748e2a70cffSBoris Popov md_get_uint8(mdp, NULL);
749e2a70cffSBoris Popov md_get_uint16le(mdp, NULL);
750e2a70cffSBoris Popov md_get_uint16le(mdp, &resid);
751e2a70cffSBoris Popov *rresid = resid;
752e2a70cffSBoris Popov } while(0);
753e2a70cffSBoris Popov
754e2a70cffSBoris Popov smb_rq_done(rqp);
755e2a70cffSBoris Popov return (error);
756e2a70cffSBoris Popov }
757e2a70cffSBoris Popov
758e2a70cffSBoris Popov static __inline int
smb_smb_read(struct smb_share * ssp,u_int16_t fid,int * len,int * rresid,struct uio * uio,struct smb_cred * scred)759681a5bbeSBoris Popov smb_smb_read(struct smb_share *ssp, u_int16_t fid,
760681a5bbeSBoris Popov int *len, int *rresid, struct uio *uio, struct smb_cred *scred)
761681a5bbeSBoris Popov {
762681a5bbeSBoris Popov struct smb_rq *rqp;
763681a5bbeSBoris Popov struct mbchain *mbp;
764681a5bbeSBoris Popov struct mdchain *mdp;
765681a5bbeSBoris Popov u_int16_t resid, bc;
766681a5bbeSBoris Popov u_int8_t wc;
767681a5bbeSBoris Popov int error, rlen, blksz;
768681a5bbeSBoris Popov
769e2a70cffSBoris Popov if (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_READX)
770e2a70cffSBoris Popov return (smb_smb_readx(ssp, fid, len, rresid, uio, scred));
771e2a70cffSBoris Popov
772681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ, scred, &rqp);
773681a5bbeSBoris Popov if (error)
774681a5bbeSBoris Popov return error;
775681a5bbeSBoris Popov
776681a5bbeSBoris Popov blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16;
777681a5bbeSBoris Popov rlen = *len = min(blksz, *len);
778681a5bbeSBoris Popov
779681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp);
780681a5bbeSBoris Popov smb_rq_wstart(rqp);
781681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
782681a5bbeSBoris Popov mb_put_uint16le(mbp, rlen);
783681a5bbeSBoris Popov mb_put_uint32le(mbp, uio->uio_offset);
784681a5bbeSBoris Popov mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff));
785681a5bbeSBoris Popov smb_rq_wend(rqp);
786681a5bbeSBoris Popov smb_rq_bstart(rqp);
787681a5bbeSBoris Popov smb_rq_bend(rqp);
788681a5bbeSBoris Popov do {
789681a5bbeSBoris Popov error = smb_rq_simple(rqp);
790681a5bbeSBoris Popov if (error)
791681a5bbeSBoris Popov break;
792681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp);
793681a5bbeSBoris Popov md_get_uint8(mdp, &wc);
794681a5bbeSBoris Popov if (wc != 5) {
795681a5bbeSBoris Popov error = EBADRPC;
796681a5bbeSBoris Popov break;
797681a5bbeSBoris Popov }
798681a5bbeSBoris Popov md_get_uint16le(mdp, &resid);
799681a5bbeSBoris Popov md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM);
800681a5bbeSBoris Popov md_get_uint16le(mdp, &bc);
801681a5bbeSBoris Popov md_get_uint8(mdp, NULL); /* ignore buffer type */
802681a5bbeSBoris Popov md_get_uint16le(mdp, &resid);
803681a5bbeSBoris Popov if (resid == 0) {
804681a5bbeSBoris Popov *rresid = resid;
805681a5bbeSBoris Popov break;
806681a5bbeSBoris Popov }
807681a5bbeSBoris Popov error = md_get_uio(mdp, uio, resid);
808681a5bbeSBoris Popov if (error)
809681a5bbeSBoris Popov break;
810681a5bbeSBoris Popov *rresid = resid;
811681a5bbeSBoris Popov } while(0);
812681a5bbeSBoris Popov smb_rq_done(rqp);
813681a5bbeSBoris Popov return error;
814681a5bbeSBoris Popov }
815681a5bbeSBoris Popov
816681a5bbeSBoris Popov int
smb_read(struct smb_share * ssp,u_int16_t fid,struct uio * uio,struct smb_cred * scred)817681a5bbeSBoris Popov smb_read(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
818681a5bbeSBoris Popov struct smb_cred *scred)
819681a5bbeSBoris Popov {
820681a5bbeSBoris Popov int tsize, len, resid;
821681a5bbeSBoris Popov int error = 0;
822681a5bbeSBoris Popov
823681a5bbeSBoris Popov tsize = uio->uio_resid;
824681a5bbeSBoris Popov while (tsize > 0) {
8252d494bc6SMatt Jacob resid = 0;
826681a5bbeSBoris Popov len = tsize;
827681a5bbeSBoris Popov error = smb_smb_read(ssp, fid, &len, &resid, uio, scred);
828681a5bbeSBoris Popov if (error)
829681a5bbeSBoris Popov break;
830681a5bbeSBoris Popov tsize -= resid;
831681a5bbeSBoris Popov if (resid < len)
832681a5bbeSBoris Popov break;
833681a5bbeSBoris Popov }
834681a5bbeSBoris Popov return error;
835681a5bbeSBoris Popov }
836681a5bbeSBoris Popov
837681a5bbeSBoris Popov static __inline int
smb_smb_write(struct smb_share * ssp,u_int16_t fid,int * len,int * rresid,struct uio * uio,struct smb_cred * scred)838681a5bbeSBoris Popov smb_smb_write(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
839681a5bbeSBoris Popov struct uio *uio, struct smb_cred *scred)
840681a5bbeSBoris Popov {
841681a5bbeSBoris Popov struct smb_rq *rqp;
842681a5bbeSBoris Popov struct mbchain *mbp;
843681a5bbeSBoris Popov struct mdchain *mdp;
844681a5bbeSBoris Popov u_int16_t resid;
845681a5bbeSBoris Popov u_int8_t wc;
846681a5bbeSBoris Popov int error, blksz;
847681a5bbeSBoris Popov
848e2a70cffSBoris Popov if (*len && SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX)
849e2a70cffSBoris Popov return (smb_smb_writex(ssp, fid, len, rresid, uio, scred));
850e2a70cffSBoris Popov
851681a5bbeSBoris Popov blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16;
852681a5bbeSBoris Popov if (blksz > 0xffff)
853681a5bbeSBoris Popov blksz = 0xffff;
854681a5bbeSBoris Popov
855681a5bbeSBoris Popov resid = *len = min(blksz, *len);
856681a5bbeSBoris Popov
857681a5bbeSBoris Popov error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp);
858681a5bbeSBoris Popov if (error)
859681a5bbeSBoris Popov return error;
860681a5bbeSBoris Popov smb_rq_getrequest(rqp, &mbp);
861681a5bbeSBoris Popov smb_rq_wstart(rqp);
862681a5bbeSBoris Popov mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
863681a5bbeSBoris Popov mb_put_uint16le(mbp, resid);
864681a5bbeSBoris Popov mb_put_uint32le(mbp, uio->uio_offset);
865681a5bbeSBoris Popov mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff));
866681a5bbeSBoris Popov smb_rq_wend(rqp);
867681a5bbeSBoris Popov smb_rq_bstart(rqp);
868681a5bbeSBoris Popov mb_put_uint8(mbp, SMB_DT_DATA);
869681a5bbeSBoris Popov mb_put_uint16le(mbp, resid);
870681a5bbeSBoris Popov do {
871681a5bbeSBoris Popov error = mb_put_uio(mbp, uio, resid);
872681a5bbeSBoris Popov if (error)
873681a5bbeSBoris Popov break;
874681a5bbeSBoris Popov smb_rq_bend(rqp);
875681a5bbeSBoris Popov error = smb_rq_simple(rqp);
876681a5bbeSBoris Popov if (error)
877681a5bbeSBoris Popov break;
878681a5bbeSBoris Popov smb_rq_getreply(rqp, &mdp);
879681a5bbeSBoris Popov md_get_uint8(mdp, &wc);
880681a5bbeSBoris Popov if (wc != 1) {
881681a5bbeSBoris Popov error = EBADRPC;
882681a5bbeSBoris Popov break;
883681a5bbeSBoris Popov }
884681a5bbeSBoris Popov md_get_uint16le(mdp, &resid);
885681a5bbeSBoris Popov *rresid = resid;
886681a5bbeSBoris Popov } while(0);
887681a5bbeSBoris Popov smb_rq_done(rqp);
888681a5bbeSBoris Popov return error;
889681a5bbeSBoris Popov }
890681a5bbeSBoris Popov
891681a5bbeSBoris Popov int
smb_write(struct smb_share * ssp,u_int16_t fid,struct uio * uio,struct smb_cred * scred)892681a5bbeSBoris Popov smb_write(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
893681a5bbeSBoris Popov struct smb_cred *scred)
894681a5bbeSBoris Popov {
895681a5bbeSBoris Popov int error = 0, len, tsize, resid;
896681a5bbeSBoris Popov struct uio olduio;
897681a5bbeSBoris Popov
898681a5bbeSBoris Popov tsize = uio->uio_resid;
899681a5bbeSBoris Popov olduio = *uio;
900681a5bbeSBoris Popov while (tsize > 0) {
9012d494bc6SMatt Jacob resid = 0;
902681a5bbeSBoris Popov len = tsize;
903681a5bbeSBoris Popov error = smb_smb_write(ssp, fid, &len, &resid, uio, scred);
904681a5bbeSBoris Popov if (error)
905681a5bbeSBoris Popov break;
906681a5bbeSBoris Popov if (resid < len) {
907681a5bbeSBoris Popov error = EIO;
908681a5bbeSBoris Popov break;
909681a5bbeSBoris Popov }
910681a5bbeSBoris Popov tsize -= resid;
911681a5bbeSBoris Popov }
912681a5bbeSBoris Popov if (error) {
9136cd9842fSBoris Popov /*
9146cd9842fSBoris Popov * Errors can happen on the copyin, the rpc, etc. So they
9156cd9842fSBoris Popov * imply resid is unreliable. The only safe thing is
9166cd9842fSBoris Popov * to pretend zero bytes made it. We needn't restore the
9176cd9842fSBoris Popov * iovs because callers don't depend on them in error
9186cd9842fSBoris Popov * paths - uio_resid and uio_offset are what matter.
9196cd9842fSBoris Popov */
920681a5bbeSBoris Popov *uio = olduio;
921681a5bbeSBoris Popov }
922681a5bbeSBoris Popov return error;
923681a5bbeSBoris Popov }
924681a5bbeSBoris Popov
925681a5bbeSBoris Popov int
smb_smb_echo(struct smb_vc * vcp,struct smb_cred * scred)926681a5bbeSBoris Popov smb_smb_echo(struct smb_vc *vcp, struct smb_cred *scred)
927681a5bbeSBoris Popov {
928681a5bbeSBoris Popov struct smb_rq *rqp;
929681a5bbeSBoris Popov struct mbchain *mbp;
930681a5bbeSBoris Popov int error;
931681a5bbeSBoris Popov
932681a5bbeSBoris Popov error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_ECHO, scred, &rqp);
933681a5bbeSBoris Popov if (error)
934681a5bbeSBoris Popov return error;
935681a5bbeSBoris Popov mbp = &rqp->sr_rq;
936681a5bbeSBoris Popov smb_rq_wstart(rqp);
937681a5bbeSBoris Popov mb_put_uint16le(mbp, 1);
938681a5bbeSBoris Popov smb_rq_wend(rqp);
939681a5bbeSBoris Popov smb_rq_bstart(rqp);
940681a5bbeSBoris Popov mb_put_uint32le(mbp, 0);
941681a5bbeSBoris Popov smb_rq_bend(rqp);
942681a5bbeSBoris Popov error = smb_rq_simple(rqp);
943681a5bbeSBoris Popov SMBSDEBUG("%d\n", error);
944681a5bbeSBoris Popov smb_rq_done(rqp);
945681a5bbeSBoris Popov return error;
946681a5bbeSBoris Popov }
947