2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/tsocket/tsocket.h"
26 #include "../librpc/ndr/libndr.h"
28 extern fstring remote_proto
;
31 * this is the entry point if SMB2 is selected via
32 * the SMB negprot and the given dialect.
34 static void reply_smb20xx(struct smb_request
*req
, uint16_t dialect
)
40 size_t len
= 4 + SMB2_HDR_BODY
+ 0x24 + 2;
42 smb2_inbuf
= talloc_zero_array(talloc_tos(), uint8_t, len
);
43 if (smb2_inbuf
== NULL
) {
44 DEBUG(0, ("Could not push spnego blob\n"));
45 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
48 smb2_hdr
= smb2_inbuf
+ 4;
49 smb2_body
= smb2_hdr
+ SMB2_HDR_BODY
;
50 smb2_dyn
= smb2_body
+ 0x24;
52 SIVAL(smb2_hdr
, SMB2_HDR_PROTOCOL_ID
, SMB2_MAGIC
);
53 SIVAL(smb2_hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
55 SSVAL(smb2_body
, 0x00, 0x0024); /* struct size */
56 SSVAL(smb2_body
, 0x02, 0x0001); /* dialect count */
58 SSVAL(smb2_dyn
, 0x00, dialect
);
62 smbd_smb2_first_negprot(req
->sconn
, smb2_inbuf
, len
);
67 * this is the entry point if SMB2 is selected via
68 * the SMB negprot and the "SMB 2.002" dialect.
70 void reply_smb2002(struct smb_request
*req
, uint16_t choice
)
72 reply_smb20xx(req
, SMB2_DIALECT_REVISION_202
);
76 * this is the entry point if SMB2 is selected via
77 * the SMB negprot and the "SMB 2.???" dialect.
79 void reply_smb20ff(struct smb_request
*req
, uint16_t choice
)
81 req
->sconn
->smb2
.negprot_2ff
= true;
82 reply_smb20xx(req
, SMB2_DIALECT_REVISION_2FF
);
85 enum protocol_types
smbd_smb2_protocol_dialect_match(const uint8_t *indyn
,
86 const int dialect_count
,
90 enum protocol_types protocol
= PROTOCOL_NONE
;
92 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
93 if (lp_server_max_protocol() < PROTOCOL_SMB3_00
) {
96 if (lp_server_min_protocol() > PROTOCOL_SMB3_00
) {
100 *dialect
= SVAL(indyn
, c
*2);
101 if (*dialect
== SMB3_DIALECT_REVISION_300
) {
102 protocol
= PROTOCOL_SMB3_00
;
107 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
108 if (lp_server_max_protocol() < PROTOCOL_SMB2_24
) {
111 if (lp_server_min_protocol() > PROTOCOL_SMB2_24
) {
115 *dialect
= SVAL(indyn
, c
*2);
116 if (*dialect
== SMB2_DIALECT_REVISION_224
) {
117 protocol
= PROTOCOL_SMB2_24
;
122 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
123 if (lp_server_max_protocol() < PROTOCOL_SMB2_22
) {
126 if (lp_server_min_protocol() > PROTOCOL_SMB2_22
) {
130 *dialect
= SVAL(indyn
, c
*2);
131 if (*dialect
== SMB2_DIALECT_REVISION_222
) {
132 protocol
= PROTOCOL_SMB2_22
;
137 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
138 if (lp_server_max_protocol() < PROTOCOL_SMB2_10
) {
141 if (lp_server_min_protocol() > PROTOCOL_SMB2_10
) {
145 *dialect
= SVAL(indyn
, c
*2);
146 if (*dialect
== SMB2_DIALECT_REVISION_210
) {
147 protocol
= PROTOCOL_SMB2_10
;
152 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
153 if (lp_server_max_protocol() < PROTOCOL_SMB2_02
) {
156 if (lp_server_min_protocol() > PROTOCOL_SMB2_02
) {
160 *dialect
= SVAL(indyn
, c
*2);
161 if (*dialect
== SMB2_DIALECT_REVISION_202
) {
162 protocol
= PROTOCOL_SMB2_02
;
170 NTSTATUS
smbd_smb2_request_process_negprot(struct smbd_smb2_request
*req
)
173 const uint8_t *inbody
;
174 const uint8_t *indyn
= NULL
;
177 DATA_BLOB negprot_spnego_blob
;
178 uint16_t security_offset
;
179 DATA_BLOB security_buffer
;
180 size_t expected_dyn_size
= 0;
182 uint16_t security_mode
;
183 uint16_t dialect_count
;
184 uint16_t in_security_mode
;
185 uint32_t in_capabilities
;
186 DATA_BLOB in_guid_blob
;
188 uint16_t dialect
= 0;
189 uint32_t capabilities
;
190 DATA_BLOB out_guid_blob
;
191 struct GUID out_guid
;
192 enum protocol_types protocol
= PROTOCOL_NONE
;
194 uint32_t max_trans
= lp_smb2_max_trans();
195 uint32_t max_read
= lp_smb2_max_read();
196 uint32_t max_write
= lp_smb2_max_write();
197 NTTIME now
= timeval_to_nttime(&req
->request_time
);
199 status
= smbd_smb2_request_verify_sizes(req
, 0x24);
200 if (!NT_STATUS_IS_OK(status
)) {
201 return smbd_smb2_request_error(req
, status
);
203 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
205 dialect_count
= SVAL(inbody
, 0x02);
207 in_security_mode
= SVAL(inbody
, 0x04);
208 in_capabilities
= IVAL(inbody
, 0x08);
209 in_guid_blob
= data_blob_const(inbody
+ 0x0C, 16);
211 if (dialect_count
== 0) {
212 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
215 status
= GUID_from_ndr_blob(&in_guid_blob
, &in_guid
);
216 if (!NT_STATUS_IS_OK(status
)) {
217 return smbd_smb2_request_error(req
, status
);
220 expected_dyn_size
= dialect_count
* 2;
221 if (SMBD_SMB2_IN_DYN_LEN(req
) < expected_dyn_size
) {
222 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
224 indyn
= SMBD_SMB2_IN_DYN_PTR(req
);
226 protocol
= smbd_smb2_protocol_dialect_match(indyn
,
230 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
231 if (lp_server_max_protocol() < PROTOCOL_SMB2_10
) {
235 dialect
= SVAL(indyn
, c
*2);
236 if (dialect
== SMB2_DIALECT_REVISION_2FF
) {
237 if (req
->sconn
->smb2
.negprot_2ff
) {
238 req
->sconn
->smb2
.negprot_2ff
= false;
239 protocol
= PROTOCOL_SMB2_10
;
245 if (protocol
== PROTOCOL_NONE
) {
246 return smbd_smb2_request_error(req
, NT_STATUS_NOT_SUPPORTED
);
249 if (get_remote_arch() != RA_SAMBA
) {
250 set_remote_arch(RA_VISTA
);
253 fstr_sprintf(remote_proto
, "SMB%X_%02X",
254 (dialect
>> 8) & 0xFF, dialect
& 0xFF);
256 reload_services(req
->sconn
, conn_snum_used
, true);
257 DEBUG(3,("Selected protocol %s\n", remote_proto
));
259 /* negprot_spnego() returns a the server guid in the first 16 bytes */
260 negprot_spnego_blob
= negprot_spnego(req
, req
->sconn
);
261 if (negprot_spnego_blob
.data
== NULL
) {
262 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
265 if (negprot_spnego_blob
.length
< 16) {
266 return smbd_smb2_request_error(req
, NT_STATUS_INTERNAL_ERROR
);
269 security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
270 if (lp_server_signing() == SMB_SIGNING_REQUIRED
) {
271 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
275 if (lp_host_msdfs()) {
276 capabilities
|= SMB2_CAP_DFS
;
279 if ((protocol
>= PROTOCOL_SMB2_24
) &&
280 (lp_smb_encrypt(-1) != SMB_SIGNING_OFF
) &&
281 (in_capabilities
& SMB2_CAP_ENCRYPTION
)) {
282 capabilities
|= SMB2_CAP_ENCRYPTION
;
286 * 0x10000 (65536) is the maximum allowed message size
291 if (protocol
>= PROTOCOL_SMB2_10
) {
294 if (tsocket_address_is_inet(req
->sconn
->local_address
, "ip")) {
295 p
= tsocket_address_inet_port(req
->sconn
->local_address
);
298 /* largeMTU is not supported over NBT (tcp port 139) */
299 if (p
!= NBT_SMB_PORT
) {
300 capabilities
|= SMB2_CAP_LARGE_MTU
;
301 req
->sconn
->smb2
.supports_multicredit
= true;
303 /* Windows 2012R2 allows up to 8 MB */
304 max_limit
= 0x800000; /* 8MB */
309 * the defaults are 8MB, but we'll limit this to max_limit based on
310 * the dialect (64kb for SMB 2.0, 8MB for SMB >= 2.1 with LargeMTU)
312 * user configured values exceeding the limits will be overwritten,
313 * only smaller values will be accepted
316 max_trans
= MIN(max_limit
, lp_smb2_max_trans());
317 max_read
= MIN(max_limit
, lp_smb2_max_read());
318 max_write
= MIN(max_limit
, lp_smb2_max_write());
320 security_offset
= SMB2_HDR_BODY
+ 0x40;
323 /* Try SPNEGO auth... */
324 security_buffer
= data_blob_const(negprot_spnego_blob
.data
+ 16,
325 negprot_spnego_blob
.length
- 16);
327 /* for now we want raw NTLMSSP */
328 security_buffer
= data_blob_const(NULL
, 0);
331 out_guid_blob
= data_blob_const(negprot_spnego_blob
.data
, 16);
332 status
= GUID_from_ndr_blob(&out_guid_blob
, &out_guid
);
333 if (!NT_STATUS_IS_OK(status
)) {
334 return smbd_smb2_request_error(req
, status
);
337 outbody
= smbd_smb2_generate_outbody(req
, 0x40);
338 if (outbody
.data
== NULL
) {
339 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
342 SSVAL(outbody
.data
, 0x00, 0x40 + 1); /* struct size */
343 SSVAL(outbody
.data
, 0x02,
344 security_mode
); /* security mode */
345 SSVAL(outbody
.data
, 0x04, dialect
); /* dialect revision */
346 SSVAL(outbody
.data
, 0x06, 0); /* reserved */
347 memcpy(outbody
.data
+ 0x08,
348 out_guid_blob
.data
, 16); /* server guid */
349 SIVAL(outbody
.data
, 0x18,
350 capabilities
); /* capabilities */
351 SIVAL(outbody
.data
, 0x1C, max_trans
); /* max transact size */
352 SIVAL(outbody
.data
, 0x20, max_read
); /* max read size */
353 SIVAL(outbody
.data
, 0x24, max_write
); /* max write size */
354 SBVAL(outbody
.data
, 0x28, now
); /* system time */
355 SBVAL(outbody
.data
, 0x30, 0); /* server start time */
356 SSVAL(outbody
.data
, 0x38,
357 security_offset
); /* security buffer offset */
358 SSVAL(outbody
.data
, 0x3A,
359 security_buffer
.length
); /* security buffer length */
360 SIVAL(outbody
.data
, 0x3C, 0); /* reserved */
362 outdyn
= security_buffer
;
364 req
->sconn
->using_smb2
= true;
366 if (dialect
!= SMB2_DIALECT_REVISION_2FF
) {
367 struct smbXsrv_connection
*conn
= req
->sconn
->conn
;
369 status
= smbXsrv_connection_init_tables(conn
, protocol
);
370 if (!NT_STATUS_IS_OK(status
)) {
371 return smbd_smb2_request_error(req
, status
);
374 conn
->smb2
.client
.capabilities
= in_capabilities
;
375 conn
->smb2
.client
.security_mode
= in_security_mode
;
376 conn
->smb2
.client
.guid
= in_guid
;
377 conn
->smb2
.client
.num_dialects
= dialect_count
;
378 conn
->smb2
.client
.dialects
= talloc_array(conn
,
381 if (conn
->smb2
.client
.dialects
== NULL
) {
382 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
384 for (c
=0; c
< dialect_count
; c
++) {
385 conn
->smb2
.client
.dialects
[c
] = SVAL(indyn
, c
*2);
388 conn
->smb2
.server
.capabilities
= capabilities
;
389 conn
->smb2
.server
.security_mode
= security_mode
;
390 conn
->smb2
.server
.guid
= out_guid
;
391 conn
->smb2
.server
.dialect
= dialect
;
392 conn
->smb2
.server
.max_trans
= max_trans
;
393 conn
->smb2
.server
.max_read
= max_read
;
394 conn
->smb2
.server
.max_write
= max_write
;
396 req
->sconn
->smb2
.max_trans
= max_trans
;
397 req
->sconn
->smb2
.max_read
= max_read
;
398 req
->sconn
->smb2
.max_write
= max_write
;
401 return smbd_smb2_request_done(req
, outbody
, &outdyn
);