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