torture: Add a check to verify MS-SMB2 3.3.5.14.2
[Samba.git] / source3 / smbd / smb2_negprot.c
blob5fa1fbbe008bdbf90e5f746e97925ea246e7e9ca
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
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/>.
21 #include "includes.h"
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)
36 uint8_t *smb2_inbuf;
37 uint8_t *smb2_hdr;
38 uint8_t *smb2_body;
39 uint8_t *smb2_dyn;
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);
46 return;
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);
60 req->outbuf = NULL;
62 smbd_smb2_first_negprot(req->sconn, smb2_inbuf, len);
63 return;
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,
87 uint16_t *dialect)
89 size_t c = 0;
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) {
94 break;
96 if (lp_server_min_protocol() > PROTOCOL_SMB3_00) {
97 break;
100 *dialect = SVAL(indyn, c*2);
101 if (*dialect == SMB3_DIALECT_REVISION_300) {
102 protocol = PROTOCOL_SMB3_00;
103 break;
107 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
108 if (lp_server_max_protocol() < PROTOCOL_SMB2_24) {
109 break;
111 if (lp_server_min_protocol() > PROTOCOL_SMB2_24) {
112 break;
115 *dialect = SVAL(indyn, c*2);
116 if (*dialect == SMB2_DIALECT_REVISION_224) {
117 protocol = PROTOCOL_SMB2_24;
118 break;
122 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
123 if (lp_server_max_protocol() < PROTOCOL_SMB2_22) {
124 break;
126 if (lp_server_min_protocol() > PROTOCOL_SMB2_22) {
127 break;
130 *dialect = SVAL(indyn, c*2);
131 if (*dialect == SMB2_DIALECT_REVISION_222) {
132 protocol = PROTOCOL_SMB2_22;
133 break;
137 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
138 if (lp_server_max_protocol() < PROTOCOL_SMB2_10) {
139 break;
141 if (lp_server_min_protocol() > PROTOCOL_SMB2_10) {
142 break;
145 *dialect = SVAL(indyn, c*2);
146 if (*dialect == SMB2_DIALECT_REVISION_210) {
147 protocol = PROTOCOL_SMB2_10;
148 break;
152 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
153 if (lp_server_max_protocol() < PROTOCOL_SMB2_02) {
154 break;
156 if (lp_server_min_protocol() > PROTOCOL_SMB2_02) {
157 break;
160 *dialect = SVAL(indyn, c*2);
161 if (*dialect == SMB2_DIALECT_REVISION_202) {
162 protocol = PROTOCOL_SMB2_02;
163 break;
167 return protocol;
170 NTSTATUS smbd_smb2_request_process_negprot(struct smbd_smb2_request *req)
172 NTSTATUS status;
173 const uint8_t *inbody;
174 const uint8_t *indyn = NULL;
175 DATA_BLOB outbody;
176 DATA_BLOB outdyn;
177 DATA_BLOB negprot_spnego_blob;
178 uint16_t security_offset;
179 DATA_BLOB security_buffer;
180 size_t expected_dyn_size = 0;
181 size_t c;
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;
187 struct GUID in_guid;
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;
193 uint32_t max_limit;
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,
227 dialect_count,
228 &dialect);
230 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
231 if (lp_server_max_protocol() < PROTOCOL_SMB2_10) {
232 break;
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;
240 break;
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;
274 capabilities = 0;
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
287 * for SMB 2.0
289 max_limit = 0x10000;
291 if (protocol >= PROTOCOL_SMB2_10) {
292 int p = 0;
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 /* SMB >= 2.1 has 1 MB of allowed size */
304 max_limit = 0x100000; /* 1MB */
309 * the defaults are 1MB, but we'll limit this to max_limit based on
310 * the dialect (64kb for SMB2.0, 1MB for SMB2.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;
322 #if 1
323 /* Try SPNEGO auth... */
324 security_buffer = data_blob_const(negprot_spnego_blob.data + 16,
325 negprot_spnego_blob.length - 16);
326 #else
327 /* for now we want raw NTLMSSP */
328 security_buffer = data_blob_const(NULL, 0);
329 #endif
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,
379 uint16_t,
380 dialect_count);
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);