xref: /freebsd/sys/netsmb/smb_smb.c (revision 2d494bc6f5bfcf0867a50a8b542160ec7b99cbd2)
1c398230bSWarner Losh /*-
2681a5bbeSBoris Popov  * Copyright (c) 2000-2001 Boris Popov
3681a5bbeSBoris Popov  * All rights reserved.
4681a5bbeSBoris Popov  *
5681a5bbeSBoris Popov  * Redistribution and use in source and binary forms, with or without
6681a5bbeSBoris Popov  * modification, are permitted provided that the following conditions
7681a5bbeSBoris Popov  * are met:
8681a5bbeSBoris Popov  * 1. Redistributions of source code must retain the above copyright
9681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer.
10681a5bbeSBoris Popov  * 2. Redistributions in binary form must reproduce the above copyright
11681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer in the
12681a5bbeSBoris Popov  *    documentation and/or other materials provided with the distribution.
13681a5bbeSBoris Popov  * 3. All advertising materials mentioning features or use of this software
14681a5bbeSBoris Popov  *    must display the following acknowledgement:
15681a5bbeSBoris Popov  *    This product includes software developed by Boris Popov.
16681a5bbeSBoris Popov  * 4. Neither the name of the author nor the names of any co-contributors
17681a5bbeSBoris Popov  *    may be used to endorse or promote products derived from this software
18681a5bbeSBoris Popov  *    without specific prior written permission.
19681a5bbeSBoris Popov  *
20681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30681a5bbeSBoris Popov  * SUCH DAMAGE.
31681a5bbeSBoris Popov  */
32ab0de15bSDavid E. O'Brien 
33681a5bbeSBoris Popov /*
34681a5bbeSBoris Popov  * various SMB requests. Most of the routines merely packs data into mbufs.
35681a5bbeSBoris Popov  */
36ab0de15bSDavid E. O'Brien 
37ab0de15bSDavid E. O'Brien #include <sys/cdefs.h>
38ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$");
39ab0de15bSDavid E. O'Brien 
40681a5bbeSBoris Popov #include <sys/param.h>
41681a5bbeSBoris Popov #include <sys/systm.h>
42681a5bbeSBoris Popov #include <sys/kernel.h>
43681a5bbeSBoris Popov #include <sys/malloc.h>
44681a5bbeSBoris Popov #include <sys/proc.h>
45681a5bbeSBoris Popov #include <sys/lock.h>
46681a5bbeSBoris Popov #include <sys/sysctl.h>
47681a5bbeSBoris Popov #include <sys/socket.h>
48681a5bbeSBoris Popov #include <sys/uio.h>
49681a5bbeSBoris Popov 
50681a5bbeSBoris Popov #include <sys/iconv.h>
51681a5bbeSBoris Popov 
52681a5bbeSBoris Popov #include <netsmb/smb.h>
53681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
54681a5bbeSBoris Popov #include <netsmb/smb_rq.h>
55681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
56681a5bbeSBoris Popov #include <netsmb/smb_tran.h>
57681a5bbeSBoris Popov 
58190b2c4fSTim J. Robbins #include "opt_netsmb.h"
59190b2c4fSTim J. Robbins 
60681a5bbeSBoris Popov struct smb_dialect {
61681a5bbeSBoris Popov 	int		d_id;
62681a5bbeSBoris Popov 	const char *	d_name;
63681a5bbeSBoris Popov };
64681a5bbeSBoris Popov 
65681a5bbeSBoris Popov static struct smb_dialect smb_dialects[] = {
66681a5bbeSBoris Popov 	{SMB_DIALECT_CORE,	"PC NETWORK PROGRAM 1.0"},
67681a5bbeSBoris Popov 	{SMB_DIALECT_COREPLUS,	"MICROSOFT NETWORKS 1.03"},
68681a5bbeSBoris Popov 	{SMB_DIALECT_LANMAN1_0,	"MICROSOFT NETWORKS 3.0"},
69681a5bbeSBoris Popov 	{SMB_DIALECT_LANMAN1_0,	"LANMAN1.0"},
70681a5bbeSBoris Popov 	{SMB_DIALECT_LANMAN2_0,	"LM1.2X002"},
71681a5bbeSBoris Popov 	{SMB_DIALECT_LANMAN2_0,	"Samba"},
72681a5bbeSBoris Popov 	{SMB_DIALECT_NTLM0_12,	"NT LANMAN 1.0"},
73681a5bbeSBoris Popov 	{SMB_DIALECT_NTLM0_12,	"NT LM 0.12"},
74681a5bbeSBoris Popov 	{-1,			NULL}
75681a5bbeSBoris Popov };
76681a5bbeSBoris Popov 
77681a5bbeSBoris Popov #define	SMB_DIALECT_MAX	(sizeof(smb_dialects) / sizeof(struct smb_dialect) - 2)
78681a5bbeSBoris Popov 
79e2a70cffSBoris Popov static u_int32_t
80e2a70cffSBoris Popov smb_vc_maxread(struct smb_vc *vcp)
81e2a70cffSBoris Popov {
82e2a70cffSBoris Popov 	/*
83e2a70cffSBoris Popov 	 * Specs say up to 64k data bytes, but Windows traffic
84e2a70cffSBoris Popov 	 * uses 60k... no doubt for some good reason.
85190b2c4fSTim J. Robbins 	 *
86190b2c4fSTim J. Robbins 	 * Don't exceed the server's buffer size if signatures
87190b2c4fSTim J. Robbins 	 * are enabled otherwise Windows 2003 chokes. Allow space
88190b2c4fSTim J. Robbins 	 * for the SMB header & a little bit extra.
89e2a70cffSBoris Popov 	 */
90190b2c4fSTim J. Robbins 	if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_READX) &&
91190b2c4fSTim J. Robbins 	    (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0)
92e2a70cffSBoris Popov 		return (60*1024);
93e2a70cffSBoris Popov 	else
94190b2c4fSTim J. Robbins 		return (vcp->vc_sopt.sv_maxtx - SMB_HDRLEN - 64);
95e2a70cffSBoris Popov }
96e2a70cffSBoris Popov 
97e2a70cffSBoris Popov static u_int32_t
98e2a70cffSBoris Popov smb_vc_maxwrite(struct smb_vc *vcp)
99e2a70cffSBoris Popov {
100e2a70cffSBoris Popov 	/*
101190b2c4fSTim J. Robbins 	 * See comment above.
102e2a70cffSBoris Popov 	 */
103190b2c4fSTim J. Robbins 	if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX) &&
104190b2c4fSTim J. Robbins 	    (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0)
105e2a70cffSBoris Popov 		return (60*1024);
106e2a70cffSBoris Popov 	else
107190b2c4fSTim J. Robbins 		return (vcp->vc_sopt.sv_maxtx - SMB_HDRLEN - 64);
108e2a70cffSBoris Popov }
109e2a70cffSBoris Popov 
110681a5bbeSBoris Popov static int
111681a5bbeSBoris Popov smb_smb_nomux(struct smb_vc *vcp, struct smb_cred *scred, const char *name)
112681a5bbeSBoris Popov {
113fce6fbfaSBoris Popov 	if (scred->scr_td->td_proc == vcp->vc_iod->iod_p)
114681a5bbeSBoris Popov 		return 0;
115681a5bbeSBoris Popov 	SMBERROR("wrong function called(%s)\n", name);
116681a5bbeSBoris Popov 	return EINVAL;
117681a5bbeSBoris Popov }
118681a5bbeSBoris Popov 
119681a5bbeSBoris Popov int
120681a5bbeSBoris Popov smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred)
121681a5bbeSBoris Popov {
122681a5bbeSBoris Popov 	struct smb_dialect *dp;
123681a5bbeSBoris Popov 	struct smb_sopt *sp = NULL;
124681a5bbeSBoris Popov 	struct smb_rq *rqp;
125681a5bbeSBoris Popov 	struct mbchain *mbp;
126681a5bbeSBoris Popov 	struct mdchain *mdp;
127681a5bbeSBoris Popov 	u_int8_t wc, stime[8], sblen;
128681a5bbeSBoris Popov 	u_int16_t dindex, tw, tw1, swlen, bc;
129681a5bbeSBoris Popov 	int error, maxqsz;
130681a5bbeSBoris Popov 
1316e551fb6SDavid E. O'Brien 	if (smb_smb_nomux(vcp, scred, __func__) != 0)
132681a5bbeSBoris Popov 		return EINVAL;
133681a5bbeSBoris Popov 	vcp->vc_hflags = 0;
134681a5bbeSBoris Popov 	vcp->vc_hflags2 = 0;
135681a5bbeSBoris Popov 	vcp->obj.co_flags &= ~(SMBV_ENCRYPT);
136681a5bbeSBoris Popov 	sp = &vcp->vc_sopt;
137681a5bbeSBoris Popov 	bzero(sp, sizeof(struct smb_sopt));
138681a5bbeSBoris Popov 	error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_NEGOTIATE, scred, &rqp);
139681a5bbeSBoris Popov 	if (error)
140681a5bbeSBoris Popov 		return error;
141681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
142681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
143681a5bbeSBoris Popov 	smb_rq_wend(rqp);
144681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
145681a5bbeSBoris Popov 	for(dp = smb_dialects; dp->d_id != -1; dp++) {
146681a5bbeSBoris Popov 		mb_put_uint8(mbp, SMB_DT_DIALECT);
147681a5bbeSBoris Popov 		smb_put_dstring(mbp, vcp, dp->d_name, SMB_CS_NONE);
148681a5bbeSBoris Popov 	}
149681a5bbeSBoris Popov 	smb_rq_bend(rqp);
150681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
151681a5bbeSBoris Popov 	SMBSDEBUG("%d\n", error);
152681a5bbeSBoris Popov 	if (error)
153681a5bbeSBoris Popov 		goto bad;
154681a5bbeSBoris Popov 	smb_rq_getreply(rqp, &mdp);
155681a5bbeSBoris Popov 	do {
156681a5bbeSBoris Popov 		error = md_get_uint8(mdp, &wc);
157681a5bbeSBoris Popov 		if (error)
158681a5bbeSBoris Popov 			break;
159681a5bbeSBoris Popov 		error = md_get_uint16le(mdp, &dindex);
160681a5bbeSBoris Popov 		if (error)
161681a5bbeSBoris Popov 			break;
162681a5bbeSBoris Popov 		if (dindex > 7) {
163681a5bbeSBoris Popov 			SMBERROR("Don't know how to talk with server %s (%d)\n", "xxx", dindex);
164681a5bbeSBoris Popov 			error = EBADRPC;
165681a5bbeSBoris Popov 			break;
166681a5bbeSBoris Popov 		}
167681a5bbeSBoris Popov 		dp = smb_dialects + dindex;
168681a5bbeSBoris Popov 		sp->sv_proto = dp->d_id;
169681a5bbeSBoris Popov 		SMBSDEBUG("Dialect %s (%d, %d)\n", dp->d_name, dindex, wc);
170681a5bbeSBoris Popov 		error = EBADRPC;
171681a5bbeSBoris Popov 		if (dp->d_id >= SMB_DIALECT_NTLM0_12) {
172681a5bbeSBoris Popov 			if (wc != 17)
173681a5bbeSBoris Popov 				break;
174681a5bbeSBoris Popov 			md_get_uint8(mdp, &sp->sv_sm);
175681a5bbeSBoris Popov 			md_get_uint16le(mdp, &sp->sv_maxmux);
176681a5bbeSBoris Popov 			md_get_uint16le(mdp, &sp->sv_maxvcs);
177681a5bbeSBoris Popov 			md_get_uint32le(mdp, &sp->sv_maxtx);
178681a5bbeSBoris Popov 			md_get_uint32le(mdp, &sp->sv_maxraw);
179681a5bbeSBoris Popov 			md_get_uint32le(mdp, &sp->sv_skey);
180681a5bbeSBoris Popov 			md_get_uint32le(mdp, &sp->sv_caps);
181681a5bbeSBoris Popov 			md_get_mem(mdp, stime, 8, MB_MSYSTEM);
182681a5bbeSBoris Popov 			md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz);
183681a5bbeSBoris Popov 			md_get_uint8(mdp, &sblen);
184681a5bbeSBoris Popov 			if (sblen && (sp->sv_sm & SMB_SM_ENCRYPT)) {
185681a5bbeSBoris Popov 				if (sblen != SMB_MAXCHALLENGELEN) {
186681a5bbeSBoris Popov 					SMBERROR("Unexpected length of security blob (%d)\n", sblen);
187681a5bbeSBoris Popov 					break;
188681a5bbeSBoris Popov 				}
189681a5bbeSBoris Popov 				error = md_get_uint16(mdp, &bc);
190681a5bbeSBoris Popov 				if (error)
191681a5bbeSBoris Popov 					break;
192681a5bbeSBoris Popov 				if (sp->sv_caps & SMB_CAP_EXT_SECURITY)
193681a5bbeSBoris Popov 					md_get_mem(mdp, NULL, 16, MB_MSYSTEM);
194681a5bbeSBoris Popov 				error = md_get_mem(mdp, vcp->vc_ch, sblen, MB_MSYSTEM);
195681a5bbeSBoris Popov 				if (error)
196681a5bbeSBoris Popov 					break;
197681a5bbeSBoris Popov 				vcp->vc_chlen = sblen;
198681a5bbeSBoris Popov 				vcp->obj.co_flags |= SMBV_ENCRYPT;
199681a5bbeSBoris Popov 			}
200190b2c4fSTim J. Robbins 			if (sp->sv_sm & SMB_SM_SIGS_REQUIRE)
201190b2c4fSTim J. Robbins 				vcp->vc_hflags2 |= SMB_FLAGS2_SECURITY_SIGNATURE;
202681a5bbeSBoris Popov 			vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES;
203681a5bbeSBoris Popov 			if (dp->d_id == SMB_DIALECT_NTLM0_12 &&
204681a5bbeSBoris Popov 			    sp->sv_maxtx < 4096 &&
205681a5bbeSBoris Popov 			    (sp->sv_caps & SMB_CAP_NT_SMBS) == 0) {
206681a5bbeSBoris Popov 				vcp->obj.co_flags |= SMBV_WIN95;
207681a5bbeSBoris Popov 				SMBSDEBUG("Win95 detected\n");
208681a5bbeSBoris Popov 			}
209681a5bbeSBoris Popov 		} else if (dp->d_id > SMB_DIALECT_CORE) {
210681a5bbeSBoris Popov 			md_get_uint16le(mdp, &tw);
211681a5bbeSBoris Popov 			sp->sv_sm = tw;
212681a5bbeSBoris Popov 			md_get_uint16le(mdp, &tw);
213681a5bbeSBoris Popov 			sp->sv_maxtx = tw;
214681a5bbeSBoris Popov 			md_get_uint16le(mdp, &sp->sv_maxmux);
215681a5bbeSBoris Popov 			md_get_uint16le(mdp, &sp->sv_maxvcs);
216681a5bbeSBoris Popov 			md_get_uint16le(mdp, &tw);	/* rawmode */
217681a5bbeSBoris Popov 			md_get_uint32le(mdp, &sp->sv_skey);
218681a5bbeSBoris Popov 			if (wc == 13) {		/* >= LANMAN1 */
219681a5bbeSBoris Popov 				md_get_uint16(mdp, &tw);		/* time */
220681a5bbeSBoris Popov 				md_get_uint16(mdp, &tw1);		/* date */
221681a5bbeSBoris Popov 				md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz);
222681a5bbeSBoris Popov 				md_get_uint16le(mdp, &swlen);
223681a5bbeSBoris Popov 				if (swlen > SMB_MAXCHALLENGELEN)
224681a5bbeSBoris Popov 					break;
225681a5bbeSBoris Popov 				md_get_uint16(mdp, NULL);	/* mbz */
226681a5bbeSBoris Popov 				if (md_get_uint16(mdp, &bc) != 0)
227681a5bbeSBoris Popov 					break;
228681a5bbeSBoris Popov 				if (bc < swlen)
229681a5bbeSBoris Popov 					break;
230681a5bbeSBoris Popov 				if (swlen && (sp->sv_sm & SMB_SM_ENCRYPT)) {
231681a5bbeSBoris Popov 					error = md_get_mem(mdp, vcp->vc_ch, swlen, MB_MSYSTEM);
232681a5bbeSBoris Popov 					if (error)
233681a5bbeSBoris Popov 						break;
234681a5bbeSBoris Popov 					vcp->vc_chlen = swlen;
235681a5bbeSBoris Popov 					vcp->obj.co_flags |= SMBV_ENCRYPT;
236681a5bbeSBoris Popov 				}
237681a5bbeSBoris Popov 			}
238681a5bbeSBoris Popov 			vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES;
239681a5bbeSBoris Popov 		} else {	/* an old CORE protocol */
240681a5bbeSBoris Popov 			sp->sv_maxmux = 1;
241681a5bbeSBoris Popov 		}
242681a5bbeSBoris Popov 		error = 0;
243681a5bbeSBoris Popov 	} while (0);
244681a5bbeSBoris Popov 	if (error == 0) {
245681a5bbeSBoris Popov 		vcp->vc_maxvcs = sp->sv_maxvcs;
246681a5bbeSBoris Popov 		if (vcp->vc_maxvcs <= 1) {
247681a5bbeSBoris Popov 			if (vcp->vc_maxvcs == 0)
248681a5bbeSBoris Popov 				vcp->vc_maxvcs = 1;
249681a5bbeSBoris Popov 		}
250681a5bbeSBoris Popov 		if (sp->sv_maxtx <= 0 || sp->sv_maxtx > 0xffff)
251681a5bbeSBoris Popov 			sp->sv_maxtx = 1024;
252e2a70cffSBoris Popov 		else
253e2a70cffSBoris Popov 			sp->sv_maxtx = min(sp->sv_maxtx,
254e2a70cffSBoris Popov 					   63*1024 + SMB_HDRLEN + 16);
255e2a70cffSBoris Popov 		SMB_TRAN_GETPARAM(vcp, SMBTP_RCVSZ, &maxqsz);
256e2a70cffSBoris Popov 		vcp->vc_rxmax = min(smb_vc_maxread(vcp), maxqsz - 1024);
257681a5bbeSBoris Popov 		SMB_TRAN_GETPARAM(vcp, SMBTP_SNDSZ, &maxqsz);
258e2a70cffSBoris Popov 		vcp->vc_wxmax = min(smb_vc_maxwrite(vcp), maxqsz - 1024);
259681a5bbeSBoris Popov 		vcp->vc_txmax = min(sp->sv_maxtx, maxqsz);
260681a5bbeSBoris Popov 		SMBSDEBUG("TZ = %d\n", sp->sv_tz);
261681a5bbeSBoris Popov 		SMBSDEBUG("CAPS = %x\n", sp->sv_caps);
262681a5bbeSBoris Popov 		SMBSDEBUG("MAXMUX = %d\n", sp->sv_maxmux);
263681a5bbeSBoris Popov 		SMBSDEBUG("MAXVCS = %d\n", sp->sv_maxvcs);
264681a5bbeSBoris Popov 		SMBSDEBUG("MAXRAW = %d\n", sp->sv_maxraw);
265681a5bbeSBoris Popov 		SMBSDEBUG("MAXTX = %d\n", sp->sv_maxtx);
266681a5bbeSBoris Popov 	}
267681a5bbeSBoris Popov bad:
268681a5bbeSBoris Popov 	smb_rq_done(rqp);
269681a5bbeSBoris Popov 	return error;
270681a5bbeSBoris Popov }
271681a5bbeSBoris Popov 
272681a5bbeSBoris Popov int
273681a5bbeSBoris Popov smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred)
274681a5bbeSBoris Popov {
275681a5bbeSBoris Popov 	struct smb_rq *rqp;
276681a5bbeSBoris Popov 	struct mbchain *mbp;
277681a5bbeSBoris Popov /*	u_int8_t wc;
278681a5bbeSBoris Popov 	u_int16_t tw, tw1;*/
279681a5bbeSBoris Popov 	smb_uniptr unipp, ntencpass = NULL;
280681a5bbeSBoris Popov 	char *pp, *up, *pbuf, *encpass;
28170f9b9d9SBoris Popov 	int error, plen, uniplen, ulen, upper;
28270f9b9d9SBoris Popov 
28370f9b9d9SBoris Popov 	upper = 0;
28470f9b9d9SBoris Popov 
28570f9b9d9SBoris Popov again:
286681a5bbeSBoris Popov 
287681a5bbeSBoris Popov 	vcp->vc_smbuid = SMB_UID_UNKNOWN;
288681a5bbeSBoris Popov 
2896e551fb6SDavid E. O'Brien 	if (smb_smb_nomux(vcp, scred, __func__) != 0)
290681a5bbeSBoris Popov 		return EINVAL;
291681a5bbeSBoris Popov 
292681a5bbeSBoris Popov 	error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_SESSION_SETUP_ANDX, scred, &rqp);
293681a5bbeSBoris Popov 	if (error)
294681a5bbeSBoris Popov 		return error;
295a163d034SWarner Losh 	pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK);
296a163d034SWarner Losh 	encpass = malloc(24, M_SMBTEMP, M_WAITOK);
297681a5bbeSBoris Popov 	if (vcp->vc_sopt.sv_sm & SMB_SM_USER) {
29870f9b9d9SBoris Popov 		/*
29970f9b9d9SBoris Popov 		 * We try w/o uppercasing first so Samba mixed case
30070f9b9d9SBoris Popov 		 * passwords work.  If that fails we come back and try
30170f9b9d9SBoris Popov 		 * uppercasing to satisfy OS/2 and Windows for Workgroups.
30270f9b9d9SBoris Popov 		 */
30370f9b9d9SBoris Popov 		if (upper++) {
30470f9b9d9SBoris Popov 			iconv_convstr(vcp->vc_toupper, pbuf,
30570f9b9d9SBoris Popov 				      smb_vc_getpass(vcp)/*, SMB_MAXPASSWORDLEN*/);
30670f9b9d9SBoris Popov 		} else {
30770f9b9d9SBoris Popov 			strncpy(pbuf, smb_vc_getpass(vcp), SMB_MAXPASSWORDLEN);
30870f9b9d9SBoris Popov 			pbuf[SMB_MAXPASSWORDLEN] = '\0';
30970f9b9d9SBoris Popov 		}
31070f9b9d9SBoris Popov 		if (!SMB_UNICODE_STRINGS(vcp))
31170f9b9d9SBoris Popov 			iconv_convstr(vcp->vc_toserver, pbuf, pbuf/*,
31270f9b9d9SBoris Popov 				      SMB_MAXPASSWORDLEN*/);
31370f9b9d9SBoris Popov 
314681a5bbeSBoris Popov 		if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) {
315681a5bbeSBoris Popov 			uniplen = plen = 24;
316681a5bbeSBoris Popov 			smb_encrypt(pbuf, vcp->vc_ch, encpass);
317a163d034SWarner Losh 			ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK);
31870f9b9d9SBoris Popov 			if (SMB_UNICODE_STRINGS(vcp)) {
31970f9b9d9SBoris Popov 				strncpy(pbuf, smb_vc_getpass(vcp),
32070f9b9d9SBoris Popov 					SMB_MAXPASSWORDLEN);
32170f9b9d9SBoris Popov 				pbuf[SMB_MAXPASSWORDLEN] = '\0';
32270f9b9d9SBoris Popov 			} else
32370f9b9d9SBoris Popov 				iconv_convstr(vcp->vc_toserver, pbuf,
32470f9b9d9SBoris Popov 					      smb_vc_getpass(vcp)/*,
32570f9b9d9SBoris Popov 					      SMB_MAXPASSWORDLEN*/);
326681a5bbeSBoris Popov 			smb_ntencrypt(pbuf, vcp->vc_ch, (u_char*)ntencpass);
327681a5bbeSBoris Popov 			pp = encpass;
328681a5bbeSBoris Popov 			unipp = ntencpass;
329681a5bbeSBoris Popov 		} else {
330681a5bbeSBoris Popov 			plen = strlen(pbuf) + 1;
331681a5bbeSBoris Popov 			pp = pbuf;
332681a5bbeSBoris Popov 			uniplen = plen * 2;
333a163d034SWarner Losh 			ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK);
334681a5bbeSBoris Popov 			smb_strtouni(ntencpass, smb_vc_getpass(vcp));
335681a5bbeSBoris Popov 			plen--;
336daa35dedSBoris Popov 
337daa35dedSBoris Popov 			/*
338daa35dedSBoris Popov 			 * The uniplen is zeroed because Samba cannot deal
339daa35dedSBoris Popov 			 * with this 2nd cleartext password.  This Samba
340daa35dedSBoris Popov 			 * "bug" is actually a workaround for problems in
341daa35dedSBoris Popov 			 * Microsoft clients.
342daa35dedSBoris Popov 			 */
343681a5bbeSBoris Popov 			uniplen = 0/*-= 2*/;
344681a5bbeSBoris Popov 			unipp = ntencpass;
345681a5bbeSBoris Popov 		}
346681a5bbeSBoris Popov 	} else {
347681a5bbeSBoris Popov 		/*
348681a5bbeSBoris Popov 		 * In the share security mode password will be used
349681a5bbeSBoris Popov 		 * only in the tree authentication
350681a5bbeSBoris Popov 		 */
351681a5bbeSBoris Popov 		 pp = "";
352681a5bbeSBoris Popov 		 plen = 1;
353681a5bbeSBoris Popov 		 unipp = &smb_unieol;
354cf37bfb0SMax Khon 		 uniplen = 0 /* sizeof(smb_unieol) */;
355681a5bbeSBoris Popov 	}
356681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
357681a5bbeSBoris Popov 	mbp = &rqp->sr_rq;
358681a5bbeSBoris Popov 	up = vcp->vc_username;
359681a5bbeSBoris Popov 	ulen = strlen(up) + 1;
36070f9b9d9SBoris Popov 	/*
36170f9b9d9SBoris Popov 	 * If userid is null we are attempting anonymous browse login
36270f9b9d9SBoris Popov 	 * so passwords must be zero length.
36370f9b9d9SBoris Popov 	 */
36470f9b9d9SBoris Popov 	if (ulen == 1)
36570f9b9d9SBoris Popov 		plen = uniplen = 0;
366681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0xff);
367681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0);
368681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);
369681a5bbeSBoris Popov 	mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxtx);
370681a5bbeSBoris Popov 	mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxmux);
371681a5bbeSBoris Popov 	mb_put_uint16le(mbp, vcp->vc_number);
372681a5bbeSBoris Popov 	mb_put_uint32le(mbp, vcp->vc_sopt.sv_skey);
373681a5bbeSBoris Popov 	mb_put_uint16le(mbp, plen);
374681a5bbeSBoris Popov 	if (SMB_DIALECT(vcp) < SMB_DIALECT_NTLM0_12) {
375681a5bbeSBoris Popov 		mb_put_uint32le(mbp, 0);
376681a5bbeSBoris Popov 		smb_rq_wend(rqp);
377681a5bbeSBoris Popov 		smb_rq_bstart(rqp);
378681a5bbeSBoris Popov 		mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
379681a5bbeSBoris Popov 		smb_put_dstring(mbp, vcp, up, SMB_CS_NONE);
380681a5bbeSBoris Popov 	} else {
381681a5bbeSBoris Popov 		mb_put_uint16le(mbp, uniplen);
382681a5bbeSBoris Popov 		mb_put_uint32le(mbp, 0);		/* reserved */
383daa35dedSBoris Popov 		mb_put_uint32le(mbp, vcp->obj.co_flags & SMBV_UNICODE ?
384daa35dedSBoris Popov 				     SMB_CAP_UNICODE : 0);
385681a5bbeSBoris Popov 		smb_rq_wend(rqp);
386681a5bbeSBoris Popov 		smb_rq_bstart(rqp);
387681a5bbeSBoris Popov 		mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
388681a5bbeSBoris Popov 		mb_put_mem(mbp, (caddr_t)unipp, uniplen, MB_MSYSTEM);
389681a5bbeSBoris Popov 		smb_put_dstring(mbp, vcp, up, SMB_CS_NONE);		/* AccountName */
390681a5bbeSBoris Popov 		smb_put_dstring(mbp, vcp, vcp->vc_domain, SMB_CS_NONE);	/* PrimaryDomain */
391681a5bbeSBoris Popov 		smb_put_dstring(mbp, vcp, "FreeBSD", SMB_CS_NONE);	/* Client's OS */
392681a5bbeSBoris Popov 		smb_put_dstring(mbp, vcp, "NETSMB", SMB_CS_NONE);		/* Client name */
393681a5bbeSBoris Popov 	}
394681a5bbeSBoris Popov 	smb_rq_bend(rqp);
395681a5bbeSBoris Popov 	if (ntencpass)
396681a5bbeSBoris Popov 		free(ntencpass, M_SMBTEMP);
397190b2c4fSTim J. Robbins 	if (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE)
398190b2c4fSTim J. Robbins 		smb_calcmackey(vcp);
399681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
400681a5bbeSBoris Popov 	SMBSDEBUG("%d\n", error);
401681a5bbeSBoris Popov 	if (error) {
402681a5bbeSBoris Popov 		if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnoaccess)
403681a5bbeSBoris Popov 			error = EAUTH;
404681a5bbeSBoris Popov 		goto bad;
405681a5bbeSBoris Popov 	}
406681a5bbeSBoris Popov 	vcp->vc_smbuid = rqp->sr_rpuid;
407681a5bbeSBoris Popov bad:
408681a5bbeSBoris Popov 	free(encpass, M_SMBTEMP);
409681a5bbeSBoris Popov 	free(pbuf, M_SMBTEMP);
410681a5bbeSBoris Popov 	smb_rq_done(rqp);
41170f9b9d9SBoris Popov 	if (error && upper == 1 && vcp->vc_sopt.sv_sm & SMB_SM_USER)
41270f9b9d9SBoris Popov 		goto again;
413681a5bbeSBoris Popov 	return error;
414681a5bbeSBoris Popov }
415681a5bbeSBoris Popov 
416681a5bbeSBoris Popov int
417681a5bbeSBoris Popov smb_smb_ssnclose(struct smb_vc *vcp, struct smb_cred *scred)
418681a5bbeSBoris Popov {
419681a5bbeSBoris Popov 	struct smb_rq *rqp;
420681a5bbeSBoris Popov 	struct mbchain *mbp;
421681a5bbeSBoris Popov 	int error;
422681a5bbeSBoris Popov 
423681a5bbeSBoris Popov 	if (vcp->vc_smbuid == SMB_UID_UNKNOWN)
424681a5bbeSBoris Popov 		return 0;
425681a5bbeSBoris Popov 
4266e551fb6SDavid E. O'Brien 	if (smb_smb_nomux(vcp, scred, __func__) != 0)
427681a5bbeSBoris Popov 		return EINVAL;
428681a5bbeSBoris Popov 
429681a5bbeSBoris Popov 	error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_LOGOFF_ANDX, scred, &rqp);
430681a5bbeSBoris Popov 	if (error)
431681a5bbeSBoris Popov 		return error;
432681a5bbeSBoris Popov 	mbp = &rqp->sr_rq;
433681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
434681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0xff);
435681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0);
436681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);
437681a5bbeSBoris Popov 	smb_rq_wend(rqp);
438681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
439681a5bbeSBoris Popov 	smb_rq_bend(rqp);
440681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
441681a5bbeSBoris Popov 	SMBSDEBUG("%d\n", error);
442681a5bbeSBoris Popov 	smb_rq_done(rqp);
443681a5bbeSBoris Popov 	return error;
444681a5bbeSBoris Popov }
445681a5bbeSBoris Popov 
446681a5bbeSBoris Popov static char smb_any_share[] = "?????";
447681a5bbeSBoris Popov 
448681a5bbeSBoris Popov static char *
449681a5bbeSBoris Popov smb_share_typename(int stype)
450681a5bbeSBoris Popov {
451681a5bbeSBoris Popov 	char *pp;
452681a5bbeSBoris Popov 
453681a5bbeSBoris Popov 	switch (stype) {
454681a5bbeSBoris Popov 	    case SMB_ST_DISK:
455681a5bbeSBoris Popov 		pp = "A:";
456681a5bbeSBoris Popov 		break;
457681a5bbeSBoris Popov 	    case SMB_ST_PRINTER:
458681a5bbeSBoris Popov 		pp = smb_any_share;		/* can't use LPT: here... */
459681a5bbeSBoris Popov 		break;
460681a5bbeSBoris Popov 	    case SMB_ST_PIPE:
461681a5bbeSBoris Popov 		pp = "IPC";
462681a5bbeSBoris Popov 		break;
463681a5bbeSBoris Popov 	    case SMB_ST_COMM:
464681a5bbeSBoris Popov 		pp = "COMM";
465681a5bbeSBoris Popov 		break;
466681a5bbeSBoris Popov 	    case SMB_ST_ANY:
467681a5bbeSBoris Popov 	    default:
468681a5bbeSBoris Popov 		pp = smb_any_share;
469681a5bbeSBoris Popov 		break;
470681a5bbeSBoris Popov 	}
471681a5bbeSBoris Popov 	return pp;
472681a5bbeSBoris Popov }
473681a5bbeSBoris Popov 
474681a5bbeSBoris Popov int
475681a5bbeSBoris Popov smb_smb_treeconnect(struct smb_share *ssp, struct smb_cred *scred)
476681a5bbeSBoris Popov {
477681a5bbeSBoris Popov 	struct smb_vc *vcp;
478681a5bbeSBoris Popov 	struct smb_rq rq, *rqp = &rq;
479681a5bbeSBoris Popov 	struct mbchain *mbp;
480681a5bbeSBoris Popov 	char *pp, *pbuf, *encpass;
48170f9b9d9SBoris Popov 	int error, plen, caseopt, upper;
48270f9b9d9SBoris Popov 
48370f9b9d9SBoris Popov 	upper = 0;
48470f9b9d9SBoris Popov 
48570f9b9d9SBoris Popov again:
48670f9b9d9SBoris Popov 
48770f9b9d9SBoris Popov #if 0
48870f9b9d9SBoris Popov 	/* Disable Unicode for SMB_COM_TREE_CONNECT_ANDX requests */
48970f9b9d9SBoris Popov 	if (SSTOVC(ssp)->vc_hflags2 & SMB_FLAGS2_UNICODE) {
49070f9b9d9SBoris Popov 		vcp = SSTOVC(ssp);
49170f9b9d9SBoris Popov 		if (vcp->vc_toserver) {
49270f9b9d9SBoris Popov 			iconv_close(vcp->vc_toserver);
49370f9b9d9SBoris Popov 			/* Use NULL until UTF-8 -> ASCII works */
49470f9b9d9SBoris Popov 			vcp->vc_toserver = NULL;
49570f9b9d9SBoris Popov 		}
49670f9b9d9SBoris Popov 		if (vcp->vc_tolocal) {
49770f9b9d9SBoris Popov 			iconv_close(vcp->vc_tolocal);
49870f9b9d9SBoris Popov 			/* Use NULL until ASCII -> UTF-8 works*/
49970f9b9d9SBoris Popov 			vcp->vc_tolocal = NULL;
50070f9b9d9SBoris Popov 		}
50170f9b9d9SBoris Popov 		vcp->vc_hflags2 &= ~SMB_FLAGS2_UNICODE;
50270f9b9d9SBoris Popov 	}
50370f9b9d9SBoris Popov #endif
504681a5bbeSBoris Popov 
505681a5bbeSBoris Popov 	ssp->ss_tid = SMB_TID_UNKNOWN;
506681a5bbeSBoris Popov 	error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_CONNECT_ANDX, scred, &rqp);
507681a5bbeSBoris Popov 	if (error)
508681a5bbeSBoris Popov 		return error;
509681a5bbeSBoris Popov 	vcp = rqp->sr_vc;
510681a5bbeSBoris Popov 	caseopt = SMB_CS_NONE;
511681a5bbeSBoris Popov 	if (vcp->vc_sopt.sv_sm & SMB_SM_USER) {
512681a5bbeSBoris Popov 		plen = 1;
513681a5bbeSBoris Popov 		pp = "";
514681a5bbeSBoris Popov 		pbuf = NULL;
515681a5bbeSBoris Popov 		encpass = NULL;
516681a5bbeSBoris Popov 	} else {
517a163d034SWarner Losh 		pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK);
518a163d034SWarner Losh 		encpass = malloc(24, M_SMBTEMP, M_WAITOK);
51970f9b9d9SBoris Popov 		/*
52070f9b9d9SBoris Popov 		 * We try w/o uppercasing first so Samba mixed case
52170f9b9d9SBoris Popov 		 * passwords work.  If that fails we come back and try
52270f9b9d9SBoris Popov 		 * uppercasing to satisfy OS/2 and Windows for Workgroups.
52370f9b9d9SBoris Popov 		 */
52470f9b9d9SBoris Popov 		if (upper++) {
52570f9b9d9SBoris Popov 			iconv_convstr(vcp->vc_toupper, pbuf,
52670f9b9d9SBoris Popov 				      smb_share_getpass(ssp)/*,
52770f9b9d9SBoris Popov 				      SMB_MAXPASSWORDLEN*/);
52870f9b9d9SBoris Popov 		} else {
52970f9b9d9SBoris Popov 			strncpy(pbuf, smb_share_getpass(ssp),
53070f9b9d9SBoris Popov 				SMB_MAXPASSWORDLEN);
53170f9b9d9SBoris Popov 			pbuf[SMB_MAXPASSWORDLEN] = '\0';
53270f9b9d9SBoris Popov 		}
533681a5bbeSBoris Popov 		if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) {
534681a5bbeSBoris Popov 			plen = 24;
535681a5bbeSBoris Popov 			smb_encrypt(pbuf, vcp->vc_ch, encpass);
536681a5bbeSBoris Popov 			pp = encpass;
537681a5bbeSBoris Popov 		} else {
538681a5bbeSBoris Popov 			plen = strlen(pbuf) + 1;
539681a5bbeSBoris Popov 			pp = pbuf;
540681a5bbeSBoris Popov 		}
541681a5bbeSBoris Popov 	}
542681a5bbeSBoris Popov 	mbp = &rqp->sr_rq;
543681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
544681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0xff);
545681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0);
546681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);
547681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);		/* Flags */
548681a5bbeSBoris Popov 	mb_put_uint16le(mbp, plen);
549681a5bbeSBoris Popov 	smb_rq_wend(rqp);
550681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
551681a5bbeSBoris Popov 	mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
552681a5bbeSBoris Popov 	smb_put_dmem(mbp, vcp, "\\\\", 2, caseopt);
553681a5bbeSBoris Popov 	pp = vcp->vc_srvname;
554681a5bbeSBoris Popov 	smb_put_dmem(mbp, vcp, pp, strlen(pp), caseopt);
555681a5bbeSBoris Popov 	smb_put_dmem(mbp, vcp, "\\", 1, caseopt);
556681a5bbeSBoris Popov 	pp = ssp->ss_name;
557681a5bbeSBoris Popov 	smb_put_dstring(mbp, vcp, pp, caseopt);
558681a5bbeSBoris Popov 	pp = smb_share_typename(ssp->ss_type);
559681a5bbeSBoris Popov 	smb_put_dstring(mbp, vcp, pp, caseopt);
560681a5bbeSBoris Popov 	smb_rq_bend(rqp);
561681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
562681a5bbeSBoris Popov 	SMBSDEBUG("%d\n", error);
563681a5bbeSBoris Popov 	if (error)
564681a5bbeSBoris Popov 		goto bad;
565681a5bbeSBoris Popov 	ssp->ss_tid = rqp->sr_rptid;
566681a5bbeSBoris Popov 	ssp->ss_vcgenid = vcp->vc_genid;
567681a5bbeSBoris Popov 	ssp->ss_flags |= SMBS_CONNECTED;
568681a5bbeSBoris Popov bad:
569681a5bbeSBoris Popov 	if (encpass)
570681a5bbeSBoris Popov 		free(encpass, M_SMBTEMP);
571681a5bbeSBoris Popov 	if (pbuf)
572681a5bbeSBoris Popov 		free(pbuf, M_SMBTEMP);
573681a5bbeSBoris Popov 	smb_rq_done(rqp);
57470f9b9d9SBoris Popov 	if (error && upper == 1)
57570f9b9d9SBoris Popov 		goto again;
576681a5bbeSBoris Popov 	return error;
577681a5bbeSBoris Popov }
578681a5bbeSBoris Popov 
579681a5bbeSBoris Popov int
580681a5bbeSBoris Popov smb_smb_treedisconnect(struct smb_share *ssp, struct smb_cred *scred)
581681a5bbeSBoris Popov {
582681a5bbeSBoris Popov 	struct smb_rq *rqp;
583681a5bbeSBoris Popov 	struct mbchain *mbp;
584681a5bbeSBoris Popov 	int error;
585681a5bbeSBoris Popov 
586681a5bbeSBoris Popov 	if (ssp->ss_tid == SMB_TID_UNKNOWN)
587681a5bbeSBoris Popov 		return 0;
588681a5bbeSBoris Popov 	error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_DISCONNECT, scred, &rqp);
589681a5bbeSBoris Popov 	if (error)
590681a5bbeSBoris Popov 		return error;
591681a5bbeSBoris Popov 	mbp = &rqp->sr_rq;
592681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
593681a5bbeSBoris Popov 	smb_rq_wend(rqp);
594681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
595681a5bbeSBoris Popov 	smb_rq_bend(rqp);
596681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
597681a5bbeSBoris Popov 	SMBSDEBUG("%d\n", error);
598681a5bbeSBoris Popov 	smb_rq_done(rqp);
599681a5bbeSBoris Popov 	ssp->ss_tid = SMB_TID_UNKNOWN;
600681a5bbeSBoris Popov 	return error;
601681a5bbeSBoris Popov }
602681a5bbeSBoris Popov 
603681a5bbeSBoris Popov static __inline int
604e2a70cffSBoris Popov smb_smb_readx(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
605e2a70cffSBoris Popov 	      struct uio *uio, struct smb_cred *scred)
606e2a70cffSBoris Popov {
607e2a70cffSBoris Popov 	struct smb_rq *rqp;
608e2a70cffSBoris Popov 	struct mbchain *mbp;
609e2a70cffSBoris Popov 	struct mdchain *mdp;
610e2a70cffSBoris Popov 	u_int8_t wc;
611e2a70cffSBoris Popov 	int error;
612e2a70cffSBoris Popov 	u_int16_t residhi, residlo, off, doff;
613e2a70cffSBoris Popov 	u_int32_t resid;
614e2a70cffSBoris Popov 
615e2a70cffSBoris Popov 	error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ_ANDX, scred, &rqp);
616e2a70cffSBoris Popov 	if (error)
617e2a70cffSBoris Popov 		return error;
618e2a70cffSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
619e2a70cffSBoris Popov 	smb_rq_wstart(rqp);
620e2a70cffSBoris Popov 	mb_put_uint8(mbp, 0xff);	/* no secondary command */
621e2a70cffSBoris Popov 	mb_put_uint8(mbp, 0);		/* MBZ */
622e2a70cffSBoris Popov 	mb_put_uint16le(mbp, 0);	/* offset to secondary */
623e2a70cffSBoris Popov 	mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
624e2a70cffSBoris Popov 	mb_put_uint32le(mbp, uio->uio_offset);
625e2a70cffSBoris Popov 	*len = min(SSTOVC(ssp)->vc_rxmax, *len);
626e2a70cffSBoris Popov 	mb_put_uint16le(mbp, *len);	/* MaxCount */
627e2a70cffSBoris Popov 	mb_put_uint16le(mbp, *len);	/* MinCount (only indicates blocking) */
628e2a70cffSBoris Popov 	mb_put_uint32le(mbp, (unsigned)*len >> 16);	/* MaxCountHigh */
629e2a70cffSBoris Popov 	mb_put_uint16le(mbp, *len);	/* Remaining ("obsolete") */
630e2a70cffSBoris Popov 	mb_put_uint32le(mbp, uio->uio_offset >> 32);	/* OffsetHigh */
631e2a70cffSBoris Popov 	smb_rq_wend(rqp);
632e2a70cffSBoris Popov 	smb_rq_bstart(rqp);
633e2a70cffSBoris Popov 	smb_rq_bend(rqp);
634e2a70cffSBoris Popov 	do {
635e2a70cffSBoris Popov 		error = smb_rq_simple(rqp);
636e2a70cffSBoris Popov 		if (error)
637e2a70cffSBoris Popov 			break;
638e2a70cffSBoris Popov 		smb_rq_getreply(rqp, &mdp);
639e2a70cffSBoris Popov 		off = SMB_HDRLEN;
640e2a70cffSBoris Popov 		md_get_uint8(mdp, &wc);
641e2a70cffSBoris Popov 		off++;
642e2a70cffSBoris Popov 		if (wc != 12) {
643e2a70cffSBoris Popov 			error = EBADRPC;
644e2a70cffSBoris Popov 			break;
645e2a70cffSBoris Popov 		}
646e2a70cffSBoris Popov 		md_get_uint8(mdp, NULL);
647e2a70cffSBoris Popov 		off++;
648e2a70cffSBoris Popov 		md_get_uint8(mdp, NULL);
649e2a70cffSBoris Popov 		off++;
650e2a70cffSBoris Popov 		md_get_uint16le(mdp, NULL);
651e2a70cffSBoris Popov 		off += 2;
652e2a70cffSBoris Popov 		md_get_uint16le(mdp, NULL);
653e2a70cffSBoris Popov 		off += 2;
654e2a70cffSBoris Popov 		md_get_uint16le(mdp, NULL);	/* data compaction mode */
655e2a70cffSBoris Popov 		off += 2;
656e2a70cffSBoris Popov 		md_get_uint16le(mdp, NULL);
657e2a70cffSBoris Popov 		off += 2;
658e2a70cffSBoris Popov 		md_get_uint16le(mdp, &residlo);
659e2a70cffSBoris Popov 		off += 2;
660e2a70cffSBoris Popov 		md_get_uint16le(mdp, &doff);	/* data offset */
661e2a70cffSBoris Popov 		off += 2;
662e2a70cffSBoris Popov 		md_get_uint16le(mdp, &residhi);
663e2a70cffSBoris Popov 		off += 2;
664e2a70cffSBoris Popov 		resid = (residhi << 16) | residlo;
665e2a70cffSBoris Popov 		md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM);
666e2a70cffSBoris Popov 		off += 4*2;
667e2a70cffSBoris Popov 		md_get_uint16le(mdp, NULL);	/* ByteCount */
668e2a70cffSBoris Popov 		off += 2;
669e2a70cffSBoris Popov 		if (doff > off)	/* pad byte(s)? */
670e2a70cffSBoris Popov 			md_get_mem(mdp, NULL, doff - off, MB_MSYSTEM);
671e2a70cffSBoris Popov 		if (resid == 0) {
672e2a70cffSBoris Popov 			*rresid = resid;
673e2a70cffSBoris Popov 			break;
674e2a70cffSBoris Popov 		}
675e2a70cffSBoris Popov 		error = md_get_uio(mdp, uio, resid);
676e2a70cffSBoris Popov 		if (error)
677e2a70cffSBoris Popov 			break;
678e2a70cffSBoris Popov 		*rresid = resid;
679e2a70cffSBoris Popov 	} while(0);
680e2a70cffSBoris Popov 	smb_rq_done(rqp);
681e2a70cffSBoris Popov 	return (error);
682e2a70cffSBoris Popov }
683e2a70cffSBoris Popov 
684e2a70cffSBoris Popov static __inline int
685e2a70cffSBoris Popov smb_smb_writex(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
686e2a70cffSBoris Popov 	struct uio *uio, struct smb_cred *scred)
687e2a70cffSBoris Popov {
688e2a70cffSBoris Popov 	struct smb_rq *rqp;
689e2a70cffSBoris Popov 	struct mbchain *mbp;
690e2a70cffSBoris Popov 	struct mdchain *mdp;
691e2a70cffSBoris Popov 	int error;
692e2a70cffSBoris Popov 	u_int8_t wc;
693e2a70cffSBoris Popov 	u_int16_t resid;
694e2a70cffSBoris Popov 
695e2a70cffSBoris Popov 	error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE_ANDX, scred, &rqp);
696e2a70cffSBoris Popov 	if (error)
697e2a70cffSBoris Popov 		return (error);
698e2a70cffSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
699e2a70cffSBoris Popov 	smb_rq_wstart(rqp);
700e2a70cffSBoris Popov 	mb_put_uint8(mbp, 0xff);	/* no secondary command */
701e2a70cffSBoris Popov 	mb_put_uint8(mbp, 0);		/* MBZ */
702e2a70cffSBoris Popov 	mb_put_uint16le(mbp, 0);	/* offset to secondary */
703e2a70cffSBoris Popov 	mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
704e2a70cffSBoris Popov 	mb_put_uint32le(mbp, uio->uio_offset);
705e2a70cffSBoris Popov 	mb_put_uint32le(mbp, 0);	/* MBZ (timeout) */
706e2a70cffSBoris Popov 	mb_put_uint16le(mbp, 0);	/* !write-thru */
707e2a70cffSBoris Popov 	mb_put_uint16le(mbp, 0);
708e2a70cffSBoris Popov 	*len = min(SSTOVC(ssp)->vc_wxmax, *len);
709e2a70cffSBoris Popov 	mb_put_uint16le(mbp, (unsigned)*len >> 16);
710e2a70cffSBoris Popov 	mb_put_uint16le(mbp, *len);
711e2a70cffSBoris Popov 	mb_put_uint16le(mbp, 64);	/* data offset from header start */
712e2a70cffSBoris Popov 	mb_put_uint32le(mbp, uio->uio_offset >> 32);	/* OffsetHigh */
713e2a70cffSBoris Popov 	smb_rq_wend(rqp);
714e2a70cffSBoris Popov 	smb_rq_bstart(rqp);
715e2a70cffSBoris Popov 	do {
716e2a70cffSBoris Popov 		mb_put_uint8(mbp, 0xee);	/* mimic xp pad byte! */
717e2a70cffSBoris Popov 		error = mb_put_uio(mbp, uio, *len);
718e2a70cffSBoris Popov 		if (error)
719e2a70cffSBoris Popov 			break;
720e2a70cffSBoris Popov 		smb_rq_bend(rqp);
721e2a70cffSBoris Popov 		error = smb_rq_simple(rqp);
722e2a70cffSBoris Popov 		if (error)
723e2a70cffSBoris Popov 			break;
724e2a70cffSBoris Popov 		smb_rq_getreply(rqp, &mdp);
725e2a70cffSBoris Popov 		md_get_uint8(mdp, &wc);
726e2a70cffSBoris Popov 		if (wc != 6) {
727e2a70cffSBoris Popov 			error = EBADRPC;
728e2a70cffSBoris Popov 			break;
729e2a70cffSBoris Popov 		}
730e2a70cffSBoris Popov 		md_get_uint8(mdp, NULL);
731e2a70cffSBoris Popov 		md_get_uint8(mdp, NULL);
732e2a70cffSBoris Popov 		md_get_uint16le(mdp, NULL);
733e2a70cffSBoris Popov 		md_get_uint16le(mdp, &resid);
734e2a70cffSBoris Popov 		*rresid = resid;
735e2a70cffSBoris Popov 	} while(0);
736e2a70cffSBoris Popov 
737e2a70cffSBoris Popov 	smb_rq_done(rqp);
738e2a70cffSBoris Popov 	return (error);
739e2a70cffSBoris Popov }
740e2a70cffSBoris Popov 
741e2a70cffSBoris Popov static __inline int
742681a5bbeSBoris Popov smb_smb_read(struct smb_share *ssp, u_int16_t fid,
743681a5bbeSBoris Popov 	int *len, int *rresid, struct uio *uio, struct smb_cred *scred)
744681a5bbeSBoris Popov {
745681a5bbeSBoris Popov 	struct smb_rq *rqp;
746681a5bbeSBoris Popov 	struct mbchain *mbp;
747681a5bbeSBoris Popov 	struct mdchain *mdp;
748681a5bbeSBoris Popov 	u_int16_t resid, bc;
749681a5bbeSBoris Popov 	u_int8_t wc;
750681a5bbeSBoris Popov 	int error, rlen, blksz;
751681a5bbeSBoris Popov 
752e2a70cffSBoris Popov 	if (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_READX)
753e2a70cffSBoris Popov 		return (smb_smb_readx(ssp, fid, len, rresid, uio, scred));
754e2a70cffSBoris Popov 
755681a5bbeSBoris Popov 	error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ, scred, &rqp);
756681a5bbeSBoris Popov 	if (error)
757681a5bbeSBoris Popov 		return error;
758681a5bbeSBoris Popov 
759681a5bbeSBoris Popov 	blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16;
760681a5bbeSBoris Popov 	rlen = *len = min(blksz, *len);
761681a5bbeSBoris Popov 
762681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
763681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
764681a5bbeSBoris Popov 	mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
765681a5bbeSBoris Popov 	mb_put_uint16le(mbp, rlen);
766681a5bbeSBoris Popov 	mb_put_uint32le(mbp, uio->uio_offset);
767681a5bbeSBoris Popov 	mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff));
768681a5bbeSBoris Popov 	smb_rq_wend(rqp);
769681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
770681a5bbeSBoris Popov 	smb_rq_bend(rqp);
771681a5bbeSBoris Popov 	do {
772681a5bbeSBoris Popov 		error = smb_rq_simple(rqp);
773681a5bbeSBoris Popov 		if (error)
774681a5bbeSBoris Popov 			break;
775681a5bbeSBoris Popov 		smb_rq_getreply(rqp, &mdp);
776681a5bbeSBoris Popov 		md_get_uint8(mdp, &wc);
777681a5bbeSBoris Popov 		if (wc != 5) {
778681a5bbeSBoris Popov 			error = EBADRPC;
779681a5bbeSBoris Popov 			break;
780681a5bbeSBoris Popov 		}
781681a5bbeSBoris Popov 		md_get_uint16le(mdp, &resid);
782681a5bbeSBoris Popov 		md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM);
783681a5bbeSBoris Popov 		md_get_uint16le(mdp, &bc);
784681a5bbeSBoris Popov 		md_get_uint8(mdp, NULL);		/* ignore buffer type */
785681a5bbeSBoris Popov 		md_get_uint16le(mdp, &resid);
786681a5bbeSBoris Popov 		if (resid == 0) {
787681a5bbeSBoris Popov 			*rresid = resid;
788681a5bbeSBoris Popov 			break;
789681a5bbeSBoris Popov 		}
790681a5bbeSBoris Popov 		error = md_get_uio(mdp, uio, resid);
791681a5bbeSBoris Popov 		if (error)
792681a5bbeSBoris Popov 			break;
793681a5bbeSBoris Popov 		*rresid = resid;
794681a5bbeSBoris Popov 	} while(0);
795681a5bbeSBoris Popov 	smb_rq_done(rqp);
796681a5bbeSBoris Popov 	return error;
797681a5bbeSBoris Popov }
798681a5bbeSBoris Popov 
799681a5bbeSBoris Popov int
800681a5bbeSBoris Popov smb_read(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
801681a5bbeSBoris Popov 	struct smb_cred *scred)
802681a5bbeSBoris Popov {
803681a5bbeSBoris Popov 	int tsize, len, resid;
804681a5bbeSBoris Popov 	int error = 0;
805681a5bbeSBoris Popov 
806681a5bbeSBoris Popov 	tsize = uio->uio_resid;
807681a5bbeSBoris Popov 	while (tsize > 0) {
8082d494bc6SMatt Jacob 		resid = 0;
809681a5bbeSBoris Popov 		len = tsize;
810681a5bbeSBoris Popov 		error = smb_smb_read(ssp, fid, &len, &resid, uio, scred);
811681a5bbeSBoris Popov 		if (error)
812681a5bbeSBoris Popov 			break;
813681a5bbeSBoris Popov 		tsize -= resid;
814681a5bbeSBoris Popov 		if (resid < len)
815681a5bbeSBoris Popov 			break;
816681a5bbeSBoris Popov 	}
817681a5bbeSBoris Popov 	return error;
818681a5bbeSBoris Popov }
819681a5bbeSBoris Popov 
820681a5bbeSBoris Popov static __inline int
821681a5bbeSBoris Popov smb_smb_write(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
822681a5bbeSBoris Popov 	struct uio *uio, struct smb_cred *scred)
823681a5bbeSBoris Popov {
824681a5bbeSBoris Popov 	struct smb_rq *rqp;
825681a5bbeSBoris Popov 	struct mbchain *mbp;
826681a5bbeSBoris Popov 	struct mdchain *mdp;
827681a5bbeSBoris Popov 	u_int16_t resid;
828681a5bbeSBoris Popov 	u_int8_t wc;
829681a5bbeSBoris Popov 	int error, blksz;
830681a5bbeSBoris Popov 
831e2a70cffSBoris Popov 	if (*len && SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX)
832e2a70cffSBoris Popov 		return (smb_smb_writex(ssp, fid, len, rresid, uio, scred));
833e2a70cffSBoris Popov 
834681a5bbeSBoris Popov 	blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16;
835681a5bbeSBoris Popov 	if (blksz > 0xffff)
836681a5bbeSBoris Popov 		blksz = 0xffff;
837681a5bbeSBoris Popov 
838681a5bbeSBoris Popov 	resid = *len = min(blksz, *len);
839681a5bbeSBoris Popov 
840681a5bbeSBoris Popov 	error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp);
841681a5bbeSBoris Popov 	if (error)
842681a5bbeSBoris Popov 		return error;
843681a5bbeSBoris Popov 	smb_rq_getrequest(rqp, &mbp);
844681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
845681a5bbeSBoris Popov 	mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
846681a5bbeSBoris Popov 	mb_put_uint16le(mbp, resid);
847681a5bbeSBoris Popov 	mb_put_uint32le(mbp, uio->uio_offset);
848681a5bbeSBoris Popov 	mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff));
849681a5bbeSBoris Popov 	smb_rq_wend(rqp);
850681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
851681a5bbeSBoris Popov 	mb_put_uint8(mbp, SMB_DT_DATA);
852681a5bbeSBoris Popov 	mb_put_uint16le(mbp, resid);
853681a5bbeSBoris Popov 	do {
854681a5bbeSBoris Popov 		error = mb_put_uio(mbp, uio, resid);
855681a5bbeSBoris Popov 		if (error)
856681a5bbeSBoris Popov 			break;
857681a5bbeSBoris Popov 		smb_rq_bend(rqp);
858681a5bbeSBoris Popov 		error = smb_rq_simple(rqp);
859681a5bbeSBoris Popov 		if (error)
860681a5bbeSBoris Popov 			break;
861681a5bbeSBoris Popov 		smb_rq_getreply(rqp, &mdp);
862681a5bbeSBoris Popov 		md_get_uint8(mdp, &wc);
863681a5bbeSBoris Popov 		if (wc != 1) {
864681a5bbeSBoris Popov 			error = EBADRPC;
865681a5bbeSBoris Popov 			break;
866681a5bbeSBoris Popov 		}
867681a5bbeSBoris Popov 		md_get_uint16le(mdp, &resid);
868681a5bbeSBoris Popov 		*rresid = resid;
869681a5bbeSBoris Popov 	} while(0);
870681a5bbeSBoris Popov 	smb_rq_done(rqp);
871681a5bbeSBoris Popov 	return error;
872681a5bbeSBoris Popov }
873681a5bbeSBoris Popov 
874681a5bbeSBoris Popov int
875681a5bbeSBoris Popov smb_write(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
876681a5bbeSBoris Popov 	struct smb_cred *scred)
877681a5bbeSBoris Popov {
878681a5bbeSBoris Popov 	int error = 0, len, tsize, resid;
879681a5bbeSBoris Popov 	struct uio olduio;
880681a5bbeSBoris Popov 
881681a5bbeSBoris Popov 	tsize = uio->uio_resid;
882681a5bbeSBoris Popov 	olduio = *uio;
883681a5bbeSBoris Popov 	while (tsize > 0) {
8842d494bc6SMatt Jacob 		resid = 0;
885681a5bbeSBoris Popov 		len = tsize;
886681a5bbeSBoris Popov 		error = smb_smb_write(ssp, fid, &len, &resid, uio, scred);
887681a5bbeSBoris Popov 		if (error)
888681a5bbeSBoris Popov 			break;
889681a5bbeSBoris Popov 		if (resid < len) {
890681a5bbeSBoris Popov 			error = EIO;
891681a5bbeSBoris Popov 			break;
892681a5bbeSBoris Popov 		}
893681a5bbeSBoris Popov 		tsize -= resid;
894681a5bbeSBoris Popov 	}
895681a5bbeSBoris Popov 	if (error) {
8966cd9842fSBoris Popov 		/*
8976cd9842fSBoris Popov 		 * Errors can happen on the copyin, the rpc, etc.  So they
8986cd9842fSBoris Popov 		 * imply resid is unreliable.  The only safe thing is
8996cd9842fSBoris Popov 		 * to pretend zero bytes made it.  We needn't restore the
9006cd9842fSBoris Popov 		 * iovs because callers don't depend on them in error
9016cd9842fSBoris Popov 		 * paths - uio_resid and uio_offset are what matter.
9026cd9842fSBoris Popov 		 */
903681a5bbeSBoris Popov 		*uio = olduio;
904681a5bbeSBoris Popov 	}
905681a5bbeSBoris Popov 	return error;
906681a5bbeSBoris Popov }
907681a5bbeSBoris Popov 
908681a5bbeSBoris Popov int
909681a5bbeSBoris Popov smb_smb_echo(struct smb_vc *vcp, struct smb_cred *scred)
910681a5bbeSBoris Popov {
911681a5bbeSBoris Popov 	struct smb_rq *rqp;
912681a5bbeSBoris Popov 	struct mbchain *mbp;
913681a5bbeSBoris Popov 	int error;
914681a5bbeSBoris Popov 
915681a5bbeSBoris Popov 	error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_ECHO, scred, &rqp);
916681a5bbeSBoris Popov 	if (error)
917681a5bbeSBoris Popov 		return error;
918681a5bbeSBoris Popov 	mbp = &rqp->sr_rq;
919681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
920681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 1);
921681a5bbeSBoris Popov 	smb_rq_wend(rqp);
922681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
923681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);
924681a5bbeSBoris Popov 	smb_rq_bend(rqp);
925681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
926681a5bbeSBoris Popov 	SMBSDEBUG("%d\n", error);
927681a5bbeSBoris Popov 	smb_rq_done(rqp);
928681a5bbeSBoris Popov 	return error;
929681a5bbeSBoris Popov }
930