xref: /titanic_50/usr/src/lib/libsmbfs/smb/negprot.c (revision 02d09e03eb27f3a2dc299de704e45dae5173f43f)
1613a2f6bSGordon Ross /*
2613a2f6bSGordon Ross  * Copyright (c) 2000-2001 Boris Popov
3613a2f6bSGordon Ross  * All rights reserved.
4613a2f6bSGordon Ross  *
5613a2f6bSGordon Ross  * Redistribution and use in source and binary forms, with or without
6613a2f6bSGordon Ross  * modification, are permitted provided that the following conditions
7613a2f6bSGordon Ross  * are met:
8613a2f6bSGordon Ross  * 1. Redistributions of source code must retain the above copyright
9613a2f6bSGordon Ross  *    notice, this list of conditions and the following disclaimer.
10613a2f6bSGordon Ross  * 2. Redistributions in binary form must reproduce the above copyright
11613a2f6bSGordon Ross  *    notice, this list of conditions and the following disclaimer in the
12613a2f6bSGordon Ross  *    documentation and/or other materials provided with the distribution.
13613a2f6bSGordon Ross  * 3. All advertising materials mentioning features or use of this software
14613a2f6bSGordon Ross  *    must display the following acknowledgement:
15613a2f6bSGordon Ross  *    This product includes software developed by Boris Popov.
16613a2f6bSGordon Ross  * 4. Neither the name of the author nor the names of any co-contributors
17613a2f6bSGordon Ross  *    may be used to endorse or promote products derived from this software
18613a2f6bSGordon Ross  *    without specific prior written permission.
19613a2f6bSGordon Ross  *
20613a2f6bSGordon Ross  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21613a2f6bSGordon Ross  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22613a2f6bSGordon Ross  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23613a2f6bSGordon Ross  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24613a2f6bSGordon Ross  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25613a2f6bSGordon Ross  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26613a2f6bSGordon Ross  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27613a2f6bSGordon Ross  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28613a2f6bSGordon Ross  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29613a2f6bSGordon Ross  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30613a2f6bSGordon Ross  * SUCH DAMAGE.
31613a2f6bSGordon Ross  */
32613a2f6bSGordon Ross 
33613a2f6bSGordon Ross /*
34613a2f6bSGordon Ross  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
35613a2f6bSGordon Ross  * Use is subject to license terms.
36613a2f6bSGordon Ross  */
37613a2f6bSGordon Ross 
38613a2f6bSGordon Ross /*
39613a2f6bSGordon Ross  * SMB Negotiate Protocol, and related.
40613a2f6bSGordon Ross  * Copied from the driver: smb_smb.c
41613a2f6bSGordon Ross  */
42613a2f6bSGordon Ross 
43613a2f6bSGordon Ross #include <errno.h>
44613a2f6bSGordon Ross #include <stdio.h>
45613a2f6bSGordon Ross #include <stdlib.h>
46613a2f6bSGordon Ross #include <unistd.h>
47613a2f6bSGordon Ross #include <strings.h>
48613a2f6bSGordon Ross #include <netdb.h>
49613a2f6bSGordon Ross #include <libintl.h>
50613a2f6bSGordon Ross #include <xti.h>
51613a2f6bSGordon Ross #include <assert.h>
52613a2f6bSGordon Ross 
53613a2f6bSGordon Ross #include <sys/types.h>
54613a2f6bSGordon Ross #include <sys/time.h>
55613a2f6bSGordon Ross #include <sys/byteorder.h>
56613a2f6bSGordon Ross #include <sys/socket.h>
57613a2f6bSGordon Ross #include <sys/fcntl.h>
58613a2f6bSGordon Ross 
59613a2f6bSGordon Ross #include <netinet/in.h>
60613a2f6bSGordon Ross #include <netinet/tcp.h>
61613a2f6bSGordon Ross #include <arpa/inet.h>
62613a2f6bSGordon Ross 
63613a2f6bSGordon Ross #include <netsmb/smb.h>
64613a2f6bSGordon Ross #include <netsmb/smb_lib.h>
65613a2f6bSGordon Ross #include <netsmb/netbios.h>
66613a2f6bSGordon Ross #include <netsmb/nb_lib.h>
67613a2f6bSGordon Ross #include <netsmb/smb_dev.h>
68613a2f6bSGordon Ross 
69613a2f6bSGordon Ross #include "charsets.h"
70613a2f6bSGordon Ross #include "private.h"
71613a2f6bSGordon Ross 
72613a2f6bSGordon Ross /*
73613a2f6bSGordon Ross  * SMB dialects that we know about.
74613a2f6bSGordon Ross  */
75613a2f6bSGordon Ross struct smb_dialect {
76613a2f6bSGordon Ross 	int		d_id;
77613a2f6bSGordon Ross 	const char	*d_name;
78613a2f6bSGordon Ross };
79613a2f6bSGordon Ross static struct smb_dialect smb_dialects[] = {
80613a2f6bSGordon Ross 	{SMB_DIALECT_CORE,	"PC NETWORK PROGRAM 1.0"},
81613a2f6bSGordon Ross 	{SMB_DIALECT_LANMAN1_0,	"LANMAN1.0"},
82613a2f6bSGordon Ross 	{SMB_DIALECT_LANMAN2_0,	"LM1.2X002"},
83613a2f6bSGordon Ross 	{SMB_DIALECT_LANMAN2_1,	"LANMAN2.1"},
84613a2f6bSGordon Ross 	{SMB_DIALECT_NTLM0_12,	"NT LM 0.12"},
85613a2f6bSGordon Ross 	{-1,			NULL}
86613a2f6bSGordon Ross };
87613a2f6bSGordon Ross 
88613a2f6bSGordon Ross #define	SMB_DIALECT_MAX \
89613a2f6bSGordon Ross 	(sizeof (smb_dialects) / sizeof (struct smb_dialect) - 2)
90613a2f6bSGordon Ross 
91613a2f6bSGordon Ross /*
92613a2f6bSGordon Ross  * SMB Negotiate Protocol
93613a2f6bSGordon Ross  * Based on code from the driver: smb_smb.c
94613a2f6bSGordon Ross  *
95613a2f6bSGordon Ross  * If using Extended Security, oblob (output)
96613a2f6bSGordon Ross  * will hold the initial security "hint".
97613a2f6bSGordon Ross  */
98613a2f6bSGordon Ross int
99613a2f6bSGordon Ross smb_negprot(struct smb_ctx *ctx, struct mbdata *oblob)
100613a2f6bSGordon Ross {
101613a2f6bSGordon Ross 	struct smb_sopt *sv = &ctx->ct_sopt;
102613a2f6bSGordon Ross 	struct smb_iods *is = &ctx->ct_iods;
103613a2f6bSGordon Ross 	struct smb_rq	*rqp;
104613a2f6bSGordon Ross 	struct mbdata	*mbp;
105613a2f6bSGordon Ross 	struct smb_dialect *dp;
106613a2f6bSGordon Ross 	int err, len;
107*02d09e03SGordon Ross 	uint8_t wc, eklen;
108613a2f6bSGordon Ross 	uint16_t dindex, bc;
109613a2f6bSGordon Ross 	int will_sign = 0;
110613a2f6bSGordon Ross 
111613a2f6bSGordon Ross 	/*
112613a2f6bSGordon Ross 	 * Initialize: vc_hflags and vc_hflags2.
113613a2f6bSGordon Ross 	 * Note: ctx->ct_hflags* are copied into the
114613a2f6bSGordon Ross 	 * (per request) rqp->rq_hflags* by smb_rq_init,
115613a2f6bSGordon Ross 	 * so changing them after that call will not
116613a2f6bSGordon Ross 	 * affect THIS request.
117613a2f6bSGordon Ross 	 */
118613a2f6bSGordon Ross 	ctx->ct_hflags = SMB_FLAGS_CASELESS;
119613a2f6bSGordon Ross 	ctx->ct_hflags2 = (SMB_FLAGS2_ERR_STATUS |
120613a2f6bSGordon Ross 	    SMB_FLAGS2_KNOWS_LONG_NAMES);
121613a2f6bSGordon Ross 
122613a2f6bSGordon Ross 	/*
123613a2f6bSGordon Ross 	 * Sould we offer extended security?
124613a2f6bSGordon Ross 	 * We'll turn this back off below if
125613a2f6bSGordon Ross 	 * the server doesn't support it.
126613a2f6bSGordon Ross 	 */
127613a2f6bSGordon Ross 	if (ctx->ct_vopt & SMBVOPT_EXT_SEC)
128613a2f6bSGordon Ross 		ctx->ct_hflags2 |= SMB_FLAGS2_EXT_SEC;
129613a2f6bSGordon Ross 
130613a2f6bSGordon Ross 	/*
131613a2f6bSGordon Ross 	 * The initial UID needs to be zero,
132613a2f6bSGordon Ross 	 * or Windows XP says "bad user".
133613a2f6bSGordon Ross 	 * The initial TID is all ones, but
134613a2f6bSGordon Ross 	 * we don't use it or store it here
135613a2f6bSGordon Ross 	 * because the driver handles that.
136613a2f6bSGordon Ross 	 */
137613a2f6bSGordon Ross 	is->is_smbuid = 0;
138613a2f6bSGordon Ross 
139613a2f6bSGordon Ross 	/*
140613a2f6bSGordon Ross 	 * In case we're reconnecting,
141613a2f6bSGordon Ross 	 * free previous stuff.
142613a2f6bSGordon Ross 	 */
143613a2f6bSGordon Ross 	ctx->ct_mac_seqno = 0;
144613a2f6bSGordon Ross 	if (ctx->ct_mackey != NULL) {
145613a2f6bSGordon Ross 		free(ctx->ct_mackey);
146613a2f6bSGordon Ross 		ctx->ct_mackey = NULL;
147613a2f6bSGordon Ross 		ctx->ct_mackeylen = 0;
148613a2f6bSGordon Ross 	}
149613a2f6bSGordon Ross 
150613a2f6bSGordon Ross 	sv = &ctx->ct_sopt;
151613a2f6bSGordon Ross 	bzero(sv, sizeof (struct smb_sopt));
152613a2f6bSGordon Ross 
153613a2f6bSGordon Ross 	err = smb_rq_init(ctx, SMB_COM_NEGOTIATE, &rqp);
154613a2f6bSGordon Ross 	if (err)
155613a2f6bSGordon Ross 		return (err);
156613a2f6bSGordon Ross 
157613a2f6bSGordon Ross 	/*
158613a2f6bSGordon Ross 	 * Build the SMB request.
159613a2f6bSGordon Ross 	 */
160613a2f6bSGordon Ross 	mbp = &rqp->rq_rq;
161613a2f6bSGordon Ross 	mb_put_uint8(mbp, 0);			/* word count */
162613a2f6bSGordon Ross 	smb_rq_bstart(rqp);
163613a2f6bSGordon Ross 	for (dp = smb_dialects; dp->d_id != -1; dp++) {
164613a2f6bSGordon Ross 		mb_put_uint8(mbp, SMB_DT_DIALECT);
165613a2f6bSGordon Ross 		mb_put_astring(mbp, dp->d_name);
166613a2f6bSGordon Ross 	}
167613a2f6bSGordon Ross 	smb_rq_bend(rqp);
168613a2f6bSGordon Ross 
169613a2f6bSGordon Ross 	/*
170613a2f6bSGordon Ross 	 * This does the OTW call
171613a2f6bSGordon Ross 	 */
172613a2f6bSGordon Ross 	err = smb_rq_internal(ctx, rqp);
173613a2f6bSGordon Ross 	if (err) {
174613a2f6bSGordon Ross 		DPRINT("call failed, err %d", err);
175613a2f6bSGordon Ross 		goto errout;
176613a2f6bSGordon Ross 	}
177613a2f6bSGordon Ross 	if (rqp->rq_status != 0) {
178613a2f6bSGordon Ross 		DPRINT("nt status 0x%x", rqp->rq_status);
179613a2f6bSGordon Ross 		err = EBADRPC;
180613a2f6bSGordon Ross 		goto errout;
181613a2f6bSGordon Ross 	}
182613a2f6bSGordon Ross 
183613a2f6bSGordon Ross 	/*
184613a2f6bSGordon Ross 	 * Decode the response
185613a2f6bSGordon Ross 	 *
186613a2f6bSGordon Ross 	 * Comments to right show names as described in
187613a2f6bSGordon Ross 	 * The Microsoft SMB Protocol spec. [MS-SMB]
188613a2f6bSGordon Ross 	 * section 2.2.3
189613a2f6bSGordon Ross 	 */
190613a2f6bSGordon Ross 	mbp = &rqp->rq_rp;
191*02d09e03SGordon Ross 	(void) md_get_uint8(mbp, &wc);
192*02d09e03SGordon Ross 	err = md_get_uint16le(mbp, &dindex);
193613a2f6bSGordon Ross 	if (err || dindex > SMB_DIALECT_MAX) {
194613a2f6bSGordon Ross 		DPRINT("err %d dindex %d", err, (int)dindex);
195613a2f6bSGordon Ross 		goto errout;
196613a2f6bSGordon Ross 	}
197613a2f6bSGordon Ross 	dp = smb_dialects + dindex;
198613a2f6bSGordon Ross 	sv->sv_proto = dp->d_id;
199613a2f6bSGordon Ross 	DPRINT("Dialect %s", dp->d_name);
200613a2f6bSGordon Ross 	if (dp->d_id < SMB_DIALECT_NTLM0_12) {
201613a2f6bSGordon Ross 		/* XXX: User-visible warning too? */
202613a2f6bSGordon Ross 		DPRINT("old dialect %s", dp->d_name);
203613a2f6bSGordon Ross 		goto errout;
204613a2f6bSGordon Ross 	}
205613a2f6bSGordon Ross 	if (wc != 17) {
206613a2f6bSGordon Ross 		DPRINT("bad wc %d", (int)wc);
207613a2f6bSGordon Ross 		goto errout;
208613a2f6bSGordon Ross 	}
209*02d09e03SGordon Ross 	md_get_uint8(mbp, &sv->sv_sm);		/* SecurityMode */
210*02d09e03SGordon Ross 	md_get_uint16le(mbp, &sv->sv_maxmux);	/* MaxMpxCount */
211*02d09e03SGordon Ross 	md_get_uint16le(mbp, &sv->sv_maxvcs);	/* MaxCountVCs */
212*02d09e03SGordon Ross 	md_get_uint32le(mbp, &sv->sv_maxtx);	/* MaxBufferSize */
213*02d09e03SGordon Ross 	md_get_uint32le(mbp, &sv->sv_maxraw);	/* MaxRawSize */
214*02d09e03SGordon Ross 	md_get_uint32le(mbp, &sv->sv_skey);	/* SessionKey */
215*02d09e03SGordon Ross 	md_get_uint32le(mbp, &sv->sv_caps);	/* Capabilities */
216*02d09e03SGordon Ross 	md_get_mem(mbp, NULL, 8, MB_MSYSTEM);	/* SystemTime(s) */
217*02d09e03SGordon Ross 	md_get_uint16le(mbp, (uint16_t *)&sv->sv_tz);
218*02d09e03SGordon Ross 	md_get_uint8(mbp, &eklen);	/* EncryptionKeyLength */
219*02d09e03SGordon Ross 	err = md_get_uint16le(mbp, &bc);	/* ByteCount */
220613a2f6bSGordon Ross 	if (err)
221613a2f6bSGordon Ross 		goto errout;
222613a2f6bSGordon Ross 
223613a2f6bSGordon Ross 	/* BEGIN CSTYLED */
224613a2f6bSGordon Ross 	/*
225613a2f6bSGordon Ross 	 * Will we do SMB signing?  Or block the connection?
226613a2f6bSGordon Ross 	 * The table below describes this logic.  References:
227613a2f6bSGordon Ross 	 * [Windows Server Protocols: MS-SMB, sec. 3.2.4.2.3]
228613a2f6bSGordon Ross 	 * http://msdn.microsoft.com/en-us/library/cc212511.aspx
229613a2f6bSGordon Ross 	 * http://msdn.microsoft.com/en-us/library/cc212929.aspx
230613a2f6bSGordon Ross 	 *
231613a2f6bSGordon Ross 	 * Srv/Cli     | Required | Enabled    | If Required | Disabled
232613a2f6bSGordon Ross 	 * ------------+----------+------------+-------------+-----------
233613a2f6bSGordon Ross 	 * Required    | Signed   | Signed     | Signed      | Blocked [1]
234613a2f6bSGordon Ross 	 * ------------+----------+------------+-------------+-----------
235613a2f6bSGordon Ross 	 * Enabled     | Signed   | Signed     | Not Signed  | Not Signed
236613a2f6bSGordon Ross 	 * ------------+----------+------------+-------------+-----------
237613a2f6bSGordon Ross 	 * If Required | Signed   | Not Signed | Not Signed  | Not Signed
238613a2f6bSGordon Ross 	 * ------------+----------+------------+-------------+-----------
239613a2f6bSGordon Ross 	 * Disabled    | Blocked  | Not Signed | Not Signed  | Not Signed
240613a2f6bSGordon Ross 	 *
241613a2f6bSGordon Ross 	 * [1] Like Windows 2003 and later, we don't really implement
242613a2f6bSGordon Ross 	 * the "Disabled" setting.  Instead we implement "If Required",
243613a2f6bSGordon Ross 	 * so we always sign if the server requires signing.
244613a2f6bSGordon Ross 	 */
245613a2f6bSGordon Ross 	/* END CSTYLED */
246613a2f6bSGordon Ross 
247613a2f6bSGordon Ross 	if (sv->sv_sm & SMB_SM_SIGS_REQUIRE) {
248613a2f6bSGordon Ross 		/*
249613a2f6bSGordon Ross 		 * Server requires signing.  We will sign,
250613a2f6bSGordon Ross 		 * even if local setting is "disabled".
251613a2f6bSGordon Ross 		 */
252613a2f6bSGordon Ross 		will_sign = 1;
253613a2f6bSGordon Ross 	} else if (sv->sv_sm & SMB_SM_SIGS) {
254613a2f6bSGordon Ross 		/*
255613a2f6bSGordon Ross 		 * Server enables signing (client's option).
256613a2f6bSGordon Ross 		 * If enabled locally, do signing.
257613a2f6bSGordon Ross 		 */
258613a2f6bSGordon Ross 		if (ctx->ct_vopt & SMBVOPT_SIGNING_ENABLED)
259613a2f6bSGordon Ross 			will_sign = 1;
260613a2f6bSGordon Ross 		/* else not signing. */
261613a2f6bSGordon Ross 	} else {
262613a2f6bSGordon Ross 		/*
263613a2f6bSGordon Ross 		 * Server does not support signing.
264613a2f6bSGordon Ross 		 * If we "require" it, bail now.
265613a2f6bSGordon Ross 		 */
266613a2f6bSGordon Ross 		if (ctx->ct_vopt & SMBVOPT_SIGNING_REQUIRED) {
267613a2f6bSGordon Ross 			DPRINT("Client requires signing "
268613a2f6bSGordon Ross 			    "but server has it disabled.");
269613a2f6bSGordon Ross 			err = EBADRPC;
270613a2f6bSGordon Ross 			goto errout;
271613a2f6bSGordon Ross 		}
272613a2f6bSGordon Ross 	}
273613a2f6bSGordon Ross 
274613a2f6bSGordon Ross 	if (will_sign) {
275613a2f6bSGordon Ross 		ctx->ct_vcflags |= SMBV_WILL_SIGN;
276613a2f6bSGordon Ross 	}
277613a2f6bSGordon Ross 	DPRINT("Security signatures: %d", will_sign);
278613a2f6bSGordon Ross 
279613a2f6bSGordon Ross 	if (sv->sv_caps & SMB_CAP_UNICODE) {
280613a2f6bSGordon Ross 		ctx->ct_vcflags |= SMBV_UNICODE;
281613a2f6bSGordon Ross 		ctx->ct_hflags2 |= SMB_FLAGS2_UNICODE;
282613a2f6bSGordon Ross 
283613a2f6bSGordon Ross 	}
284613a2f6bSGordon Ross 	if ((sv->sv_caps & SMB_CAP_STATUS32) == 0) {
285613a2f6bSGordon Ross 		/*
286613a2f6bSGordon Ross 		 * They don't do NT error codes.
287613a2f6bSGordon Ross 		 *
288613a2f6bSGordon Ross 		 * If we send requests with
289613a2f6bSGordon Ross 		 * SMB_FLAGS2_ERR_STATUS set in
290613a2f6bSGordon Ross 		 * Flags2, Windows 98, at least,
291613a2f6bSGordon Ross 		 * appears to send replies with that
292613a2f6bSGordon Ross 		 * bit set even though it sends back
293613a2f6bSGordon Ross 		 * DOS error codes.  (They probably
294613a2f6bSGordon Ross 		 * just use the request header as
295613a2f6bSGordon Ross 		 * a template for the reply header,
296613a2f6bSGordon Ross 		 * and don't bother clearing that bit.)
297613a2f6bSGordon Ross 		 *
298613a2f6bSGordon Ross 		 * Therefore, we clear that bit in
299613a2f6bSGordon Ross 		 * our vc_hflags2 field.
300613a2f6bSGordon Ross 		 */
301613a2f6bSGordon Ross 		ctx->ct_hflags2 &= ~SMB_FLAGS2_ERR_STATUS;
302613a2f6bSGordon Ross 	}
303613a2f6bSGordon Ross 	if (dp->d_id == SMB_DIALECT_NTLM0_12 &&
304613a2f6bSGordon Ross 	    sv->sv_maxtx < 4096 &&
305613a2f6bSGordon Ross 	    (sv->sv_caps & SMB_CAP_NT_SMBS) == 0) {
306613a2f6bSGordon Ross 		ctx->ct_vcflags |= SMBV_WIN95;
307613a2f6bSGordon Ross 		DPRINT("Win95 detected");
308613a2f6bSGordon Ross 	}
309613a2f6bSGordon Ross 
310613a2f6bSGordon Ross 	/*
311613a2f6bSGordon Ross 	 * The rest of the message varies depending on
312613a2f6bSGordon Ross 	 * whether we've negotiated "extended security".
313613a2f6bSGordon Ross 	 *
314613a2f6bSGordon Ross 	 * With extended security, we have:
315613a2f6bSGordon Ross 	 *	Server_GUID	(length 16)
316613a2f6bSGordon Ross 	 *	Security_BLOB
317613a2f6bSGordon Ross 	 * Otherwise we have:
318613a2f6bSGordon Ross 	 *	EncryptionKey (length is eklen)
319613a2f6bSGordon Ross 	 *	PrimaryDomain
320613a2f6bSGordon Ross 	 */
321613a2f6bSGordon Ross 	if (sv->sv_caps & SMB_CAP_EXT_SECURITY) {
322613a2f6bSGordon Ross 		struct mbuf *m;
323613a2f6bSGordon Ross 		DPRINT("Ext.Security: yes");
324613a2f6bSGordon Ross 
325613a2f6bSGordon Ross 		/*
326613a2f6bSGordon Ross 		 * Skip the server GUID.
327613a2f6bSGordon Ross 		 */
328*02d09e03SGordon Ross 		err = md_get_mem(mbp, NULL, SMB_GUIDLEN, MB_MSYSTEM);
329613a2f6bSGordon Ross 		if (err)
330613a2f6bSGordon Ross 			goto errout;
331613a2f6bSGordon Ross 		/*
332613a2f6bSGordon Ross 		 * Remainder is the security blob.
333613a2f6bSGordon Ross 		 * Note: eklen "must be ignored" [MS-SMB]
334613a2f6bSGordon Ross 		 */
335613a2f6bSGordon Ross 		len = (int)bc - SMB_GUIDLEN;
336613a2f6bSGordon Ross 		if (len < 0)
337613a2f6bSGordon Ross 			goto errout;
338613a2f6bSGordon Ross 
339613a2f6bSGordon Ross 		/*
340613a2f6bSGordon Ross 		 * Get the (optional) SPNEGO "hint".
341613a2f6bSGordon Ross 		 */
342*02d09e03SGordon Ross 		err = md_get_mbuf(mbp, len, &m);
343613a2f6bSGordon Ross 		if (err)
344613a2f6bSGordon Ross 			goto errout;
345613a2f6bSGordon Ross 		mb_initm(oblob, m);
346613a2f6bSGordon Ross 		oblob->mb_count = len;
347613a2f6bSGordon Ross 	} else {
348613a2f6bSGordon Ross 		DPRINT("Ext.Security: no");
349613a2f6bSGordon Ross 		ctx->ct_hflags2 &= ~SMB_FLAGS2_EXT_SEC;
350613a2f6bSGordon Ross 
351613a2f6bSGordon Ross 		/*
352613a2f6bSGordon Ross 		 * Save the "Encryption Key" (the challenge).
353613a2f6bSGordon Ross 		 *
354613a2f6bSGordon Ross 		 * Sanity check: make sure the sec. blob length
355613a2f6bSGordon Ross 		 * isn't bigger than the byte count.
356613a2f6bSGordon Ross 		 */
357613a2f6bSGordon Ross 		if (bc < eklen || eklen < NTLM_CHAL_SZ) {
358613a2f6bSGordon Ross 			err = EBADRPC;
359613a2f6bSGordon Ross 			goto errout;
360613a2f6bSGordon Ross 		}
361*02d09e03SGordon Ross 		err = md_get_mem(mbp, ctx->ct_ntlm_chal,
362*02d09e03SGordon Ross 		    NTLM_CHAL_SZ, MB_MSYSTEM);
363613a2f6bSGordon Ross 		/*
364613a2f6bSGordon Ross 		 * Server domain follows (ignored)
365613a2f6bSGordon Ross 		 * Note: NOT aligned(2) - unusual!
366613a2f6bSGordon Ross 		 */
367613a2f6bSGordon Ross 	}
368613a2f6bSGordon Ross 
369613a2f6bSGordon Ross 	smb_rq_done(rqp);
370613a2f6bSGordon Ross 
371613a2f6bSGordon Ross 	/*
372613a2f6bSGordon Ross 	 * A few sanity checks on what we received,
373613a2f6bSGordon Ross 	 * becuse we will send these in ssnsetup.
374613a2f6bSGordon Ross 	 *
375613a2f6bSGordon Ross 	 * Maximum outstanding requests (we care),
376613a2f6bSGordon Ross 	 * and Max. VCs (we only use one).  Also,
377613a2f6bSGordon Ross 	 * MaxBufferSize lower limit per spec.
378613a2f6bSGordon Ross 	 */
379613a2f6bSGordon Ross 	if (sv->sv_maxmux < 1)
380613a2f6bSGordon Ross 		sv->sv_maxmux = 1;
381613a2f6bSGordon Ross 	if (sv->sv_maxvcs < 1)
382613a2f6bSGordon Ross 		sv->sv_maxvcs = 1;
383613a2f6bSGordon Ross 	if (sv->sv_maxtx < 1024)
384613a2f6bSGordon Ross 		sv->sv_maxtx = 1024;
385613a2f6bSGordon Ross 
386613a2f6bSGordon Ross 	/*
387613a2f6bSGordon Ross 	 * Maximum transfer size.
388613a2f6bSGordon Ross 	 * Sanity checks:
389613a2f6bSGordon Ross 	 *
390613a2f6bSGordon Ross 	 * Let's be conservative about an upper limit here.
391613a2f6bSGordon Ross 	 * Win2k uses 16644 (and others) so 32k should be a
392613a2f6bSGordon Ross 	 * reasonable sanity limit for this value.
393613a2f6bSGordon Ross 	 *
394613a2f6bSGordon Ross 	 * Note that this limit does NOT affect READX/WRITEX
395613a2f6bSGordon Ross 	 * with CAP_LARGE_..., which we nearly always use.
396613a2f6bSGordon Ross 	 */
397613a2f6bSGordon Ross 	is->is_txmax = sv->sv_maxtx;
398613a2f6bSGordon Ross 	if (is->is_txmax > 0x8000)
399613a2f6bSGordon Ross 		is->is_txmax = 0x8000;
400613a2f6bSGordon Ross 
401613a2f6bSGordon Ross 	/*
402613a2f6bSGordon Ross 	 * Max read/write sizes, WITHOUT overhead.
403613a2f6bSGordon Ross 	 * This is just the payload size, so we must
404613a2f6bSGordon Ross 	 * leave room for the SMB headers, etc.
405613a2f6bSGordon Ross 	 * This is just the ct_txmax value, but
406613a2f6bSGordon Ross 	 * reduced and rounded down.  Tricky bit:
407613a2f6bSGordon Ross 	 *
408613a2f6bSGordon Ross 	 * Servers typically give us a value that's
409613a2f6bSGordon Ross 	 * some nice "round" number, i.e 0x4000 plus
410613a2f6bSGordon Ross 	 * some overhead, i.e. Win2k: 16644==0x4104
411613a2f6bSGordon Ross 	 * Subtract for the SMB header (32) and the
412613a2f6bSGordon Ross 	 * SMB command word and byte vectors (34?),
413613a2f6bSGordon Ross 	 * then round down to a 512 byte multiple.
414613a2f6bSGordon Ross 	 */
415613a2f6bSGordon Ross 	len = is->is_txmax - 68;
416613a2f6bSGordon Ross 	len &= 0xFE00;
417613a2f6bSGordon Ross 	/* XXX: Not sure yet which of these to keep. */
418613a2f6bSGordon Ross 	is->is_rwmax = len;
419613a2f6bSGordon Ross 	is->is_rxmax = len;
420613a2f6bSGordon Ross 	is->is_wxmax = len;
421613a2f6bSGordon Ross 
422613a2f6bSGordon Ross 	return (0);
423613a2f6bSGordon Ross 
424613a2f6bSGordon Ross errout:
425613a2f6bSGordon Ross 	smb_rq_done(rqp);
426613a2f6bSGordon Ross 	if (err == 0)
427613a2f6bSGordon Ross 		err = EBADRPC;
428613a2f6bSGordon Ross 	return (err);
429613a2f6bSGordon Ross }
430