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"
29 * this is the entry point if SMB2 is selected via
30 * the SMB negprot and the given dialect.
32 static void reply_smb20xx(struct smb_request
*req
, uint16_t dialect
)
38 size_t len
= 4 + SMB2_HDR_BODY
+ 0x24 + 2;
40 smb2_inbuf
= talloc_zero_array(talloc_tos(), uint8_t, len
);
41 if (smb2_inbuf
== NULL
) {
42 DEBUG(0, ("Could not push spnego blob\n"));
43 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
46 smb2_hdr
= smb2_inbuf
+ 4;
47 smb2_body
= smb2_hdr
+ SMB2_HDR_BODY
;
48 smb2_dyn
= smb2_body
+ 0x24;
50 SIVAL(smb2_hdr
, SMB2_HDR_PROTOCOL_ID
, SMB2_MAGIC
);
51 SIVAL(smb2_hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
53 SSVAL(smb2_body
, 0x00, 0x0024); /* struct size */
54 SSVAL(smb2_body
, 0x02, 0x0001); /* dialect count */
56 SSVAL(smb2_dyn
, 0x00, dialect
);
60 smbd_smb2_first_negprot(req
->sconn
, smb2_inbuf
, len
);
65 * this is the entry point if SMB2 is selected via
66 * the SMB negprot and the "SMB 2.002" dialect.
68 void reply_smb2002(struct smb_request
*req
, uint16_t choice
)
70 reply_smb20xx(req
, SMB2_DIALECT_REVISION_202
);
74 * this is the entry point if SMB2 is selected via
75 * the SMB negprot and the "SMB 2.???" dialect.
77 void reply_smb20ff(struct smb_request
*req
, uint16_t choice
)
79 req
->sconn
->smb2
.negprot_2ff
= true;
80 reply_smb20xx(req
, SMB2_DIALECT_REVISION_2FF
);
83 NTSTATUS
smbd_smb2_request_process_negprot(struct smbd_smb2_request
*req
)
86 const uint8_t *inbody
;
87 const uint8_t *indyn
= NULL
;
90 DATA_BLOB negprot_spnego_blob
;
91 uint16_t security_offset
;
92 DATA_BLOB security_buffer
;
93 size_t expected_dyn_size
= 0;
95 uint16_t security_mode
;
96 uint16_t dialect_count
;
97 uint16_t in_security_mode
;
98 uint32_t in_capabilities
;
99 DATA_BLOB in_guid_blob
;
101 uint16_t dialect
= 0;
102 uint32_t capabilities
;
103 DATA_BLOB out_guid_blob
;
104 struct GUID out_guid
;
105 enum protocol_types protocol
= PROTOCOL_NONE
;
107 uint32_t max_trans
= lp_smb2_max_trans();
108 uint32_t max_read
= lp_smb2_max_read();
109 uint32_t max_write
= lp_smb2_max_write();
110 NTTIME now
= timeval_to_nttime(&req
->request_time
);
112 status
= smbd_smb2_request_verify_sizes(req
, 0x24);
113 if (!NT_STATUS_IS_OK(status
)) {
114 return smbd_smb2_request_error(req
, status
);
116 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
118 dialect_count
= SVAL(inbody
, 0x02);
120 in_security_mode
= SVAL(inbody
, 0x04);
121 in_capabilities
= IVAL(inbody
, 0x08);
122 in_guid_blob
= data_blob_const(inbody
+ 0x0C, 16);
124 if (dialect_count
== 0) {
125 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
128 status
= GUID_from_ndr_blob(&in_guid_blob
, &in_guid
);
129 if (!NT_STATUS_IS_OK(status
)) {
130 return smbd_smb2_request_error(req
, status
);
133 expected_dyn_size
= dialect_count
* 2;
134 if (SMBD_SMB2_IN_DYN_LEN(req
) < expected_dyn_size
) {
135 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
137 indyn
= SMBD_SMB2_IN_DYN_PTR(req
);
139 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
140 if (lp_srv_maxprotocol() < PROTOCOL_SMB3_00
) {
143 if (lp_srv_minprotocol() > PROTOCOL_SMB3_00
) {
147 dialect
= SVAL(indyn
, c
*2);
148 if (dialect
== SMB3_DIALECT_REVISION_300
) {
149 protocol
= PROTOCOL_SMB3_00
;
154 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
155 if (lp_srv_maxprotocol() < PROTOCOL_SMB2_24
) {
158 if (lp_srv_minprotocol() > PROTOCOL_SMB2_24
) {
162 dialect
= SVAL(indyn
, c
*2);
163 if (dialect
== SMB2_DIALECT_REVISION_224
) {
164 protocol
= PROTOCOL_SMB2_24
;
169 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
170 if (lp_srv_maxprotocol() < PROTOCOL_SMB2_22
) {
173 if (lp_srv_minprotocol() > PROTOCOL_SMB2_22
) {
177 dialect
= SVAL(indyn
, c
*2);
178 if (dialect
== SMB2_DIALECT_REVISION_222
) {
179 protocol
= PROTOCOL_SMB2_22
;
184 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
185 if (lp_srv_maxprotocol() < PROTOCOL_SMB2_10
) {
188 if (lp_srv_minprotocol() > PROTOCOL_SMB2_10
) {
192 dialect
= SVAL(indyn
, c
*2);
193 if (dialect
== SMB2_DIALECT_REVISION_210
) {
194 protocol
= PROTOCOL_SMB2_10
;
199 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
200 if (lp_srv_maxprotocol() < PROTOCOL_SMB2_02
) {
203 if (lp_srv_minprotocol() > PROTOCOL_SMB2_02
) {
207 dialect
= SVAL(indyn
, c
*2);
208 if (dialect
== SMB2_DIALECT_REVISION_202
) {
209 protocol
= PROTOCOL_SMB2_02
;
214 for (c
=0; protocol
== PROTOCOL_NONE
&& c
< dialect_count
; c
++) {
215 if (lp_srv_maxprotocol() < PROTOCOL_SMB2_10
) {
219 dialect
= SVAL(indyn
, c
*2);
220 if (dialect
== SMB2_DIALECT_REVISION_2FF
) {
221 if (req
->sconn
->smb2
.negprot_2ff
) {
222 req
->sconn
->smb2
.negprot_2ff
= false;
223 protocol
= PROTOCOL_SMB2_10
;
229 if (protocol
== PROTOCOL_NONE
) {
230 return smbd_smb2_request_error(req
, NT_STATUS_NOT_SUPPORTED
);
233 if (get_remote_arch() != RA_SAMBA
) {
234 set_remote_arch(RA_VISTA
);
237 /* negprot_spnego() returns a the server guid in the first 16 bytes */
238 negprot_spnego_blob
= negprot_spnego(req
, req
->sconn
);
239 if (negprot_spnego_blob
.data
== NULL
) {
240 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
243 if (negprot_spnego_blob
.length
< 16) {
244 return smbd_smb2_request_error(req
, NT_STATUS_INTERNAL_ERROR
);
247 security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
248 if (lp_server_signing() == SMB_SIGNING_REQUIRED
) {
249 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
253 if (lp_host_msdfs()) {
254 capabilities
|= SMB2_CAP_DFS
;
257 if ((protocol
>= PROTOCOL_SMB2_24
) &&
258 (lp_smb_encrypt(-1) != SMB_SIGNING_OFF
) &&
259 (in_capabilities
& SMB2_CAP_ENCRYPTION
)) {
260 capabilities
|= SMB2_CAP_ENCRYPTION
;
264 * 0x10000 (65536) is the maximum allowed message size
269 if (protocol
>= PROTOCOL_SMB2_10
) {
272 if (tsocket_address_is_inet(req
->sconn
->local_address
, "ip")) {
273 p
= tsocket_address_inet_port(req
->sconn
->local_address
);
276 /* largeMTU is not supported over NBT (tcp port 139) */
277 if (p
!= NBT_SMB_PORT
) {
278 capabilities
|= SMB2_CAP_LARGE_MTU
;
279 req
->sconn
->smb2
.supports_multicredit
= true;
281 /* SMB >= 2.1 has 1 MB of allowed size */
282 max_limit
= 0x100000; /* 1MB */
287 * the defaults are 1MB, but we'll limit this to max_limit based on
288 * the dialect (64kb for SMB2.0, 1MB for SMB2.1 with LargeMTU)
290 * user configured values exceeding the limits will be overwritten,
291 * only smaller values will be accepted
294 max_trans
= MIN(max_limit
, lp_smb2_max_trans());
295 max_read
= MIN(max_limit
, lp_smb2_max_read());
296 max_write
= MIN(max_limit
, lp_smb2_max_write());
298 security_offset
= SMB2_HDR_BODY
+ 0x40;
301 /* Try SPNEGO auth... */
302 security_buffer
= data_blob_const(negprot_spnego_blob
.data
+ 16,
303 negprot_spnego_blob
.length
- 16);
305 /* for now we want raw NTLMSSP */
306 security_buffer
= data_blob_const(NULL
, 0);
309 out_guid_blob
= data_blob_const(negprot_spnego_blob
.data
, 16);
310 status
= GUID_from_ndr_blob(&out_guid_blob
, &out_guid
);
311 if (!NT_STATUS_IS_OK(status
)) {
312 return smbd_smb2_request_error(req
, status
);
315 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x40);
316 if (outbody
.data
== NULL
) {
317 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
320 SSVAL(outbody
.data
, 0x00, 0x40 + 1); /* struct size */
321 SSVAL(outbody
.data
, 0x02,
322 security_mode
); /* security mode */
323 SSVAL(outbody
.data
, 0x04, dialect
); /* dialect revision */
324 SSVAL(outbody
.data
, 0x06, 0); /* reserved */
325 memcpy(outbody
.data
+ 0x08,
326 out_guid_blob
.data
, 16); /* server guid */
327 SIVAL(outbody
.data
, 0x18,
328 capabilities
); /* capabilities */
329 SIVAL(outbody
.data
, 0x1C, max_trans
); /* max transact size */
330 SIVAL(outbody
.data
, 0x20, max_read
); /* max read size */
331 SIVAL(outbody
.data
, 0x24, max_write
); /* max write size */
332 SBVAL(outbody
.data
, 0x28, now
); /* system time */
333 SBVAL(outbody
.data
, 0x30, 0); /* server start time */
334 SSVAL(outbody
.data
, 0x38,
335 security_offset
); /* security buffer offset */
336 SSVAL(outbody
.data
, 0x3A,
337 security_buffer
.length
); /* security buffer length */
338 SIVAL(outbody
.data
, 0x3C, 0); /* reserved */
340 outdyn
= security_buffer
;
342 req
->sconn
->using_smb2
= true;
344 if (dialect
!= SMB2_DIALECT_REVISION_2FF
) {
345 struct smbXsrv_connection
*conn
= req
->sconn
->conn
;
347 status
= smbXsrv_connection_init_tables(conn
, protocol
);
348 if (!NT_STATUS_IS_OK(status
)) {
349 return smbd_smb2_request_error(req
, status
);
352 conn
->smb2
.client
.capabilities
= in_capabilities
;
353 conn
->smb2
.client
.security_mode
= in_security_mode
;
354 conn
->smb2
.client
.guid
= in_guid
;
355 conn
->smb2
.client
.num_dialects
= dialect_count
;
356 conn
->smb2
.client
.dialects
= talloc_array(conn
,
359 if (conn
->smb2
.client
.dialects
== NULL
) {
360 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
362 for (c
=0; c
< dialect_count
; c
++) {
363 conn
->smb2
.client
.dialects
[c
] = SVAL(indyn
, c
*2);
366 conn
->smb2
.server
.capabilities
= capabilities
;
367 conn
->smb2
.server
.security_mode
= security_mode
;
368 conn
->smb2
.server
.guid
= out_guid
;
369 conn
->smb2
.server
.dialect
= dialect
;
370 conn
->smb2
.server
.max_trans
= max_trans
;
371 conn
->smb2
.server
.max_read
= max_read
;
372 conn
->smb2
.server
.max_write
= max_write
;
374 req
->sconn
->smb2
.max_trans
= max_trans
;
375 req
->sconn
->smb2
.max_read
= max_read
;
376 req
->sconn
->smb2
.max_write
= max_write
;
379 return smbd_smb2_request_done(req
, outbody
, &outdyn
);