xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_negotiate.c (revision cd3e933325e68e23516a196a8fea7f49b1e497c3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  * Notes on the virtual circuit (VC) values in the SMB Negotiate
27  * response and SessionSetupAndx request.
28  *
29  * A virtual circuit (VC) represents a connection between a client and a
30  * server using a reliable, session oriented transport protocol, such as
31  * NetBIOS or TCP/IP. Originally, each SMB session was restricted to a
32  * single underlying transport connection, i.e. a single NetBIOS session,
33  * which limited performance for raw data transfers.
34  *
35  * The intention behind multiple VCs was to improve performance by
36  * allowing parallelism over each NetBIOS session. For example, raw data
37  * could be transmitted using a different VC from other types of SMB
38  * requests to remove the interleaving restriction while a raw transfer
39  * is in progress. So the MaxNumberVcs field was added to the negotiate
40  * response to make the number of VCs configurable and to allow servers
41  * to specify how many they were prepared to support per session
42  * connection. This turned out to be difficult to manage and, with
43  * technology improvements, it has become obsolete.
44  *
45  * Servers should set the MaxNumberVcs value in the Negotiate response
46  * to 1. Clients should probably ignore it. If a server receives a
47  * SessionSetupAndx with a VC value of 0, it should close all other
48  * VCs to that client. If it receives a non-zero VC, it should leave
49  * other VCs in tact.
50  *
51  */
52 
53 /*
54  * SMB: negotiate
55  *
56  * Client Request                Description
57  * ============================  =======================================
58  *
59  * UCHAR WordCount;              Count of parameter words = 0
60  * USHORT ByteCount;             Count of data bytes; min = 2
61  * struct {
62  *    UCHAR BufferFormat;        0x02 -- Dialect
63  *    UCHAR DialectName[];       ASCII null-terminated string
64  * } Dialects[];
65  *
66  * The Client sends a list of dialects that it can communicate with.  The
67  * response is a selection of one of those dialects (numbered 0 through n)
68  * or -1 (hex FFFF) indicating that none of the dialects were acceptable.
69  * The negotiate message is binding on the virtual circuit and must be
70  * sent.  One and only one negotiate message may be sent, subsequent
71  * negotiate requests will be rejected with an error response and no action
72  * will be taken.
73  *
74  * The protocol does not impose any particular structure to the dialect
75  * strings.  Implementors of particular protocols may choose to include,
76  * for example, version numbers in the string.
77  *
78  * If the server does not understand any of the dialect strings, or if PC
79  * NETWORK PROGRAM 1.0 is the chosen dialect, the response format is
80  *
81  * Server Response               Description
82  * ============================  =======================================
83  *
84  * UCHAR WordCount;              Count of parameter words = 1
85  * USHORT DialectIndex;          Index of selected dialect
86  * USHORT ByteCount;             Count of data bytes = 0
87  *
88  * If the chosen dialect is greater than core up to and including
89  * LANMAN2.1, the protocol response format is
90  *
91  * Server Response               Description
92  * ============================  =======================================
93  *
94  * UCHAR WordCount;              Count of parameter words = 13
95  * USHORT  DialectIndex;         Index of selected dialect
96  * USHORT  SecurityMode;         Security mode:
97  *                               bit 0: 0 = share, 1 = user
98  *                               bit 1: 1 = use challenge/response
99  *                               authentication
100  * USHORT  MaxBufferSize;        Max transmit buffer size (>= 1024)
101  * USHORT  MaxMpxCount;          Max pending multiplexed requests
102  * USHORT  MaxNumberVcs;         Max VCs between client and server
103  * USHORT  RawMode;              Raw modes supported:
104  *                                bit 0: 1 = Read Raw supported
105  *                                bit 1: 1 = Write Raw supported
106  * ULONG SessionKey;             Unique token identifying this session
107  * SMB_TIME ServerTime;          Current time at server
108  * SMB_DATE ServerDate;          Current date at server
109  * USHORT ServerTimeZone;        Current time zone at server
110  * USHORT  EncryptionKeyLength;  MBZ if this is not LM2.1
111  * USHORT  Reserved;             MBZ
112  * USHORT  ByteCount             Count of data bytes
113  * UCHAR EncryptionKey[];        The challenge encryption key
114  * STRING PrimaryDomain[];       The server's primary domain
115  *
116  * MaxBufferSize is the size of the largest message which the client can
117  * legitimately send to the server
118  *
119  * If  bit0 of the Flags field is set in the negotiate response, this
120  * indicates the server supports the SMB_COM_LOCK_AND_READ and
121  * SMB_COM_WRITE_AND_UNLOCK client requests.
122  *
123  * If the SecurityMode field indicates the server is running in user mode,
124  * the client must send appropriate SMB_COM_SESSION_SETUP_ANDX requests
125  * before the server will allow the client to access resources.   If the
126  * SecurityMode fields indicates the client should use challenge/response
127  * authentication, the client should use the authentication mechanism
128  * specified in section 2.10.
129  *
130  * Clients should submit no more than MaxMpxCount distinct unanswered SMBs
131  * to the server when using multiplexed reads or writes (see sections 5.13
132  * and 5.25)
133  *
134  * Clients using the  "MICROSOFT NETWORKS 1.03" dialect use a different
135  * form of raw reads than documented here, and servers are better off
136  * setting RawMode in this response to 0 for such sessions.
137  *
138  * If the negotiated dialect is "DOS LANMAN2.1" or "LANMAN2.1", then
139  * PrimaryDomain string should be included in this response.
140  *
141  * If the negotiated dialect is NT LM 0.12, the response format is
142  *
143  * Server Response            Description
144  * ========================== =========================================
145  *
146  * UCHAR WordCount;           Count of parameter words = 17
147  * USHORT DialectIndex;       Index of selected dialect
148  * UCHAR SecurityMode;        Security mode:
149  *                             bit 0: 0 = share, 1 = user
150  *                             bit 1: 1 = encrypt passwords
151  * USHORT MaxMpxCount;        Max pending multiplexed requests
152  * USHORT MaxNumberVcs;       Max VCs between client and server
153  * ULONG MaxBufferSize;       Max transmit buffer size
154  * ULONG MaxRawSize;          Maximum raw buffer size
155  * ULONG SessionKey;          Unique token identifying this session
156  * ULONG Capabilities;        Server capabilities
157  * ULONG SystemTimeLow;       System (UTC) time of the server (low).
158  * ULONG SystemTimeHigh;      System (UTC) time of the server (high).
159  * USHORT ServerTimeZone;     Time zone of server (min from UTC)
160  * UCHAR EncryptionKeyLength; Length of encryption key.
161  * USHORT ByteCount;          Count of data bytes
162  * UCHAR EncryptionKey[];     The challenge encryption key
163  * UCHAR OemDomainName[];     The name of the domain (in OEM chars)
164  *
165  * In addition to the definitions above, MaxBufferSize is the size of the
166  * largest message which the client can legitimately send to the server.
167  * If the client is using a connectionless protocol,  MaxBufferSize must be
168  * set to the smaller of the server's internal buffer size and the amount
169  * of data which can be placed in a response packet.
170  *
171  * MaxRawSize specifies the maximum message size the server can send or
172  * receive for SMB_COM_WRITE_RAW or SMB_COM_READ_RAW.
173  *
174  * Connectionless clients must set Sid to 0 in the SMB request header.
175  *
176  * Capabilities allows the server to tell the client what it supports.
177  * The bit definitions defined in smb.h. Bit 0x2000 used to be set in
178  * the negotiate response capabilities but it caused problems with
179  * Windows 2000. It is probably not valid, it doesn't appear in the
180  * CIFS spec.
181  *
182  * 4.1.1.1   Errors
183  *
184  * SUCCESS/SUCCESS
185  * ERRSRV/ERRerror
186  */
187 #include <sys/types.h>
188 #include <sys/strsubr.h>
189 #include <sys/socketvar.h>
190 #include <sys/socket.h>
191 #include <sys/random.h>
192 #include <netinet/in.h>
193 #include <smbsrv/smb_kproto.h>
194 #include <smbsrv/smbinfo.h>
195 
196 static smb_xlate_t smb_dialect[] = {
197 	{ DIALECT_UNKNOWN,		"DIALECT_UNKNOWN" },
198 	{ PC_NETWORK_PROGRAM_1_0,	"PC NETWORK PROGRAM 1.0" },
199 	{ PCLAN1_0,			"PCLAN1.0" },
200 	{ MICROSOFT_NETWORKS_1_03,	"MICROSOFT NETWORKS 1.03" },
201 	{ MICROSOFT_NETWORKS_3_0,	"MICROSOFT NETWORKS 3.0" },
202 	{ LANMAN1_0,			"LANMAN1.0" },
203 	{ LM1_2X002,			"LM1.2X002" },
204 	{ DOS_LM1_2X002,		"DOS LM1.2X002" },
205 	{ DOS_LANMAN2_1,		"DOS LANMAN2.1" },
206 	{ LANMAN2_1,			"LANMAN2.1" },
207 	{ Windows_for_Workgroups_3_1a,	"Windows for Workgroups 3.1a" },
208 	{ NT_LM_0_12,			"NT LM 0.12" }
209 };
210 
211 /*
212  * Maximum buffer size for DOS: chosen to be the same as NT.
213  * Do not change this value, DOS is very sensitive to it.
214  */
215 #define	SMB_DOS_MAXBUF			0x1104
216 
217 /*
218  * The DOS TCP rcvbuf is set to 8700 because DOS 6.1 seems to have problems
219  * with other values. DOS 6.1 seems to depend on a window value of 8700 to
220  * send the next set of data. If we return a window value of 40KB, after
221  * sending 8700 bytes of data, it will start the next set of data from 40KB
222  * instead of 8.7k. Why 8.7k? We have no idea; it is the value that NT uses.
223  * September 2000.
224  *
225  * IR104720 Increased smb_nt_tcp_rcvbuf from 40KB to just under 1MB to allow
226  * for a larger TCP window sizei based on observations of Windows 2000 and
227  * performance testing. March 2003.
228  */
229 static uint32_t	smb_dos_tcp_rcvbuf = 8700;
230 static uint32_t	smb_nt_tcp_rcvbuf = 1048560;	/* scale factor of 4 */
231 
232 static void smb_negotiate_genkey(smb_request_t *);
233 static int smb_xlate_dialect(const char *);
234 
235 int smb_cap_passthru = 1;
236 
237 smb_sdrc_t
238 smb_pre_negotiate(smb_request_t *sr)
239 {
240 	smb_arg_negotiate_t	*negprot;
241 	int			dialect;
242 	int			pos;
243 	int			rc = 0;
244 
245 	negprot = smb_srm_zalloc(sr, sizeof (smb_arg_negotiate_t));
246 	negprot->ni_index = -1;
247 	sr->sr_negprot = negprot;
248 
249 	for (pos = 0; smbsr_decode_data_avail(sr); pos++) {
250 		if (smbsr_decode_data(sr, "%L", sr, &negprot->ni_name) != 0) {
251 			smbsr_error(sr, 0, ERRSRV, ERRerror);
252 			rc = -1;
253 			break;
254 		}
255 
256 		if ((dialect = smb_xlate_dialect(negprot->ni_name)) < 0)
257 			continue;
258 
259 		if (negprot->ni_dialect < dialect) {
260 			negprot->ni_dialect = dialect;
261 			negprot->ni_index = pos;
262 		}
263 	}
264 
265 	DTRACE_SMB_2(op__Negotiate__start, smb_request_t *, sr,
266 	    smb_arg_negotiate_t, negprot);
267 	smb_rwx_rwenter(&sr->session->s_lock, RW_WRITER);
268 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
269 }
270 
271 void
272 smb_post_negotiate(smb_request_t *sr)
273 {
274 	smb_arg_negotiate_t	*negprot = sr->sr_negprot;
275 
276 	DTRACE_SMB_2(op__Negotiate__done, smb_request_t *, sr,
277 	    smb_arg_negotiate_t, negprot);
278 	smb_rwx_rwexit(&sr->session->s_lock);
279 
280 	bzero(negprot, sizeof (smb_arg_negotiate_t));
281 }
282 
283 smb_sdrc_t
284 smb_com_negotiate(smb_request_t *sr)
285 {
286 	smb_arg_negotiate_t	*negprot = sr->sr_negprot;
287 	uint16_t		secmode;
288 	uint32_t		sesskey;
289 	char			ipaddr_buf[INET6_ADDRSTRLEN];
290 	char			*nbdomain;
291 	uint8_t			*wcbuf;
292 	int			wclen;
293 	smb_msgbuf_t		mb;
294 	int			rc;
295 
296 	if (sr->session->s_state != SMB_SESSION_STATE_ESTABLISHED) {
297 		/* The protocol has already been negotiated. */
298 		smbsr_error(sr, 0, ERRSRV, ERRerror);
299 		return (SDRC_ERROR);
300 	}
301 
302 	sr->session->secmode = NEGOTIATE_SECURITY_CHALLENGE_RESPONSE |
303 	    NEGOTIATE_SECURITY_USER_LEVEL;
304 	secmode = sr->session->secmode;
305 
306 	smb_negotiate_genkey(sr);
307 	sesskey = sr->session->sesskey;
308 
309 	(void) microtime(&negprot->ni_servertime);
310 	negprot->ni_tzcorrection = sr->sr_gmtoff / 60;
311 	negprot->ni_maxmpxcount = sr->sr_cfg->skc_maxworkers;
312 	nbdomain = sr->sr_cfg->skc_nbdomain;
313 
314 	/*
315 	 * UNICODE support is required for long share names,
316 	 * long file names and streams.
317 	 */
318 	negprot->ni_capabilities = CAP_LARGE_FILES
319 	    | CAP_UNICODE
320 	    | CAP_NT_SMBS
321 	    | CAP_STATUS32
322 	    | CAP_NT_FIND
323 	    | CAP_RAW_MODE
324 	    | CAP_LEVEL_II_OPLOCKS
325 	    | CAP_LOCK_AND_READ
326 	    | CAP_RPC_REMOTE_APIS
327 	    | CAP_LARGE_READX
328 	    | CAP_LARGE_WRITEX
329 	    | CAP_DFS;
330 
331 	if (smb_cap_passthru)
332 		negprot->ni_capabilities |= CAP_INFOLEVEL_PASSTHRU;
333 	else
334 		cmn_err(CE_NOTE, "smbsrv: cap passthru is %s",
335 		    (negprot->ni_capabilities & CAP_INFOLEVEL_PASSTHRU) ?
336 		    "enabled" : "disabled");
337 
338 	(void) smb_inet_ntop(&sr->session->ipaddr, ipaddr_buf,
339 	    SMB_IPSTRLEN(sr->session->ipaddr.a_family));
340 
341 	switch (negprot->ni_dialect) {
342 	case PC_NETWORK_PROGRAM_1_0:	/* core */
343 		(void) ksocket_setsockopt(sr->session->sock, SOL_SOCKET,
344 		    SO_RCVBUF, (const void *)&smb_dos_tcp_rcvbuf,
345 		    sizeof (smb_dos_tcp_rcvbuf), CRED());
346 		rc = smbsr_encode_result(sr, 1, 0, "bww", 1,
347 		    negprot->ni_index, 0);
348 		break;
349 
350 	case Windows_for_Workgroups_3_1a:
351 	case PCLAN1_0:
352 	case MICROSOFT_NETWORKS_1_03:
353 	case MICROSOFT_NETWORKS_3_0:
354 	case LANMAN1_0:
355 	case LM1_2X002:
356 	case DOS_LM1_2X002:
357 		(void) ksocket_setsockopt(sr->session->sock, SOL_SOCKET,
358 		    SO_RCVBUF, (const void *)&smb_dos_tcp_rcvbuf,
359 		    sizeof (smb_dos_tcp_rcvbuf), CRED());
360 		sr->smb_flg |= SMB_FLAGS_LOCK_AND_READ_OK;
361 		rc = smbsr_encode_result(sr, 13, VAR_BCC,
362 		    "bwwwwwwlYww2.w#c",
363 		    13,				/* wct */
364 		    negprot->ni_index,		/* dialect index */
365 		    secmode,			/* security mode */
366 		    SMB_DOS_MAXBUF,		/* max buffer size */
367 		    1,				/* max MPX */
368 		    1,				/* max VCs */
369 		    3,				/* raw mode (s/b 3) */
370 		    sesskey,			/* session key */
371 		    negprot->ni_servertime.tv_sec, /* server date/time */
372 		    negprot->ni_tzcorrection,
373 		    (uint16_t)negprot->ni_keylen, /* encryption key length */
374 						/* reserved field handled 2. */
375 		    VAR_BCC,
376 		    (int)negprot->ni_keylen,
377 		    negprot->ni_key);		/* encryption key */
378 		break;
379 
380 	case DOS_LANMAN2_1:
381 	case LANMAN2_1:
382 		(void) ksocket_setsockopt(sr->session->sock, SOL_SOCKET,
383 		    SO_RCVBUF, (const void *)&smb_dos_tcp_rcvbuf,
384 		    sizeof (smb_dos_tcp_rcvbuf), CRED());
385 		sr->smb_flg |= SMB_FLAGS_LOCK_AND_READ_OK;
386 		rc = smbsr_encode_result(sr, 13, VAR_BCC,
387 		    "bwwwwwwlYww2.w#cs",
388 		    13,				/* wct */
389 		    negprot->ni_index,		/* dialect index */
390 		    secmode,			/* security mode */
391 		    SMB_DOS_MAXBUF,		/* max buffer size */
392 		    1,				/* max MPX */
393 		    1,				/* max VCs */
394 		    3,				/* raw mode (s/b 3) */
395 		    sesskey,			/* session key */
396 		    negprot->ni_servertime.tv_sec, /* server date/time */
397 		    negprot->ni_tzcorrection,
398 		    (uint16_t)negprot->ni_keylen, /* encryption key length */
399 						/* reserved field handled 2. */
400 		    VAR_BCC,
401 		    (int)negprot->ni_keylen,
402 		    negprot->ni_key,		/* encryption key */
403 		    nbdomain);
404 		break;
405 
406 	case NT_LM_0_12:
407 		(void) ksocket_setsockopt(sr->session->sock, SOL_SOCKET,
408 		    SO_RCVBUF, (const void *)&smb_nt_tcp_rcvbuf,
409 		    sizeof (smb_nt_tcp_rcvbuf), CRED());
410 
411 		/*
412 		 * Turn off Extended Security Negotiation
413 		 */
414 		sr->smb_flg2 &= ~SMB_FLAGS2_EXT_SEC;
415 
416 		/*
417 		 * Allow SMB signatures if security challenge response enabled
418 		 */
419 		if ((secmode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) &&
420 		    sr->sr_cfg->skc_signing_enable) {
421 			secmode |= NEGOTIATE_SECURITY_SIGNATURES_ENABLED;
422 			if (sr->sr_cfg->skc_signing_required)
423 				secmode |=
424 				    NEGOTIATE_SECURITY_SIGNATURES_REQUIRED;
425 
426 			sr->session->secmode = secmode;
427 		}
428 
429 		/*
430 		 * nbdomain is not expected to be aligned.
431 		 * Use temporary buffer to avoid alignment padding
432 		 */
433 		wclen = smb_wcequiv_strlen(nbdomain) + sizeof (smb_wchar_t);
434 		wcbuf = smb_srm_zalloc(sr, wclen);
435 		smb_msgbuf_init(&mb, wcbuf, wclen, SMB_MSGBUF_UNICODE);
436 		if (smb_msgbuf_encode(&mb, "U", nbdomain) < 0) {
437 			smb_msgbuf_term(&mb);
438 			smbsr_error(sr, 0, ERRSRV, ERRerror);
439 			return (SDRC_ERROR);
440 		}
441 
442 		rc = smbsr_encode_result(sr, 17, VAR_BCC,
443 		    "bwbwwllllTwbw#c#c",
444 		    17,				/* wct */
445 		    negprot->ni_index,		/* dialect index */
446 		    secmode,			/* security mode */
447 		    negprot->ni_maxmpxcount,	/* max MPX */
448 		    1,				/* max VCs */
449 		    (DWORD)smb_maxbufsize,	/* max buffer size */
450 		    0xFFFF,			/* max raw size */
451 		    sesskey,			/* session key */
452 		    negprot->ni_capabilities,
453 		    &negprot->ni_servertime,	/* system time */
454 		    negprot->ni_tzcorrection,
455 		    negprot->ni_keylen,		/* encryption key length */
456 		    VAR_BCC,
457 		    (int)negprot->ni_keylen,
458 		    negprot->ni_key,		/* encryption key */
459 		    wclen,
460 		    wcbuf);			/* nbdomain (unicode) */
461 
462 		smb_msgbuf_term(&mb);
463 		break;
464 
465 	default:
466 		rc = smbsr_encode_result(sr, 1, 0, "bww", 1, -1, 0);
467 		return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
468 	}
469 
470 	if (rc != 0)
471 		return (SDRC_ERROR);
472 
473 	/*
474 	 * Save the agreed dialect. Note that this value is also
475 	 * used to detect and reject attempts to re-negotiate.
476 	 */
477 	sr->session->dialect = negprot->ni_dialect;
478 	sr->session->s_state = SMB_SESSION_STATE_NEGOTIATED;
479 	return (SDRC_SUCCESS);
480 }
481 
482 static void
483 smb_negotiate_genkey(smb_request_t *sr)
484 {
485 	smb_arg_negotiate_t	*negprot = sr->sr_negprot;
486 	uint8_t			tmp_key[8];
487 
488 	(void) random_get_pseudo_bytes(tmp_key, 8);
489 	bcopy(tmp_key, &sr->session->challenge_key, 8);
490 	sr->session->challenge_len = 8;
491 	negprot->ni_keylen = 8;
492 	bcopy(tmp_key, negprot->ni_key, 8);
493 
494 	(void) random_get_pseudo_bytes(tmp_key, 4);
495 	sr->session->sesskey = tmp_key[0] | tmp_key[1] << 8 |
496 	    tmp_key[2] << 16 | tmp_key[3] << 24;
497 }
498 
499 static int
500 smb_xlate_dialect(const char *dialect)
501 {
502 	smb_xlate_t	*dp;
503 	int		i;
504 
505 	for (i = 0; i < sizeof (smb_dialect) / sizeof (smb_dialect[0]); ++i) {
506 		dp = &smb_dialect[i];
507 
508 		if (strcmp(dp->str, dialect) == 0)
509 			return (dp->code);
510 	}
511 
512 	return (-1);
513 }
514