Merge tag 'samba-4.1.10' into v4-1-test
[Samba.git] / source3 / smbd / smb2_negprot.c
blob963a5572532491461a65e34044d950fe5043f47f
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 NTSTATUS smbd_smb2_request_process_negprot(struct smbd_smb2_request *req)
87 NTSTATUS status;
88 const uint8_t *inbody;
89 const uint8_t *indyn = NULL;
90 DATA_BLOB outbody;
91 DATA_BLOB outdyn;
92 DATA_BLOB negprot_spnego_blob;
93 uint16_t security_offset;
94 DATA_BLOB security_buffer;
95 size_t expected_dyn_size = 0;
96 size_t c;
97 uint16_t security_mode;
98 uint16_t dialect_count;
99 uint16_t in_security_mode;
100 uint32_t in_capabilities;
101 DATA_BLOB in_guid_blob;
102 struct GUID in_guid;
103 uint16_t dialect = 0;
104 uint32_t capabilities;
105 DATA_BLOB out_guid_blob;
106 struct GUID out_guid;
107 enum protocol_types protocol = PROTOCOL_NONE;
108 uint32_t max_limit;
109 uint32_t max_trans = lp_smb2_max_trans();
110 uint32_t max_read = lp_smb2_max_read();
111 uint32_t max_write = lp_smb2_max_write();
112 NTTIME now = timeval_to_nttime(&req->request_time);
114 status = smbd_smb2_request_verify_sizes(req, 0x24);
115 if (!NT_STATUS_IS_OK(status)) {
116 return smbd_smb2_request_error(req, status);
118 inbody = SMBD_SMB2_IN_BODY_PTR(req);
120 dialect_count = SVAL(inbody, 0x02);
122 in_security_mode = SVAL(inbody, 0x04);
123 in_capabilities = IVAL(inbody, 0x08);
124 in_guid_blob = data_blob_const(inbody + 0x0C, 16);
126 if (dialect_count == 0) {
127 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
130 status = GUID_from_ndr_blob(&in_guid_blob, &in_guid);
131 if (!NT_STATUS_IS_OK(status)) {
132 return smbd_smb2_request_error(req, status);
135 expected_dyn_size = dialect_count * 2;
136 if (SMBD_SMB2_IN_DYN_LEN(req) < expected_dyn_size) {
137 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
139 indyn = SMBD_SMB2_IN_DYN_PTR(req);
141 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
142 if (lp_srv_maxprotocol() < PROTOCOL_SMB3_00) {
143 break;
145 if (lp_srv_minprotocol() > PROTOCOL_SMB3_00) {
146 break;
149 dialect = SVAL(indyn, c*2);
150 if (dialect == SMB3_DIALECT_REVISION_300) {
151 protocol = PROTOCOL_SMB3_00;
152 break;
156 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
157 if (lp_srv_maxprotocol() < PROTOCOL_SMB2_24) {
158 break;
160 if (lp_srv_minprotocol() > PROTOCOL_SMB2_24) {
161 break;
164 dialect = SVAL(indyn, c*2);
165 if (dialect == SMB2_DIALECT_REVISION_224) {
166 protocol = PROTOCOL_SMB2_24;
167 break;
171 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
172 if (lp_srv_maxprotocol() < PROTOCOL_SMB2_22) {
173 break;
175 if (lp_srv_minprotocol() > PROTOCOL_SMB2_22) {
176 break;
179 dialect = SVAL(indyn, c*2);
180 if (dialect == SMB2_DIALECT_REVISION_222) {
181 protocol = PROTOCOL_SMB2_22;
182 break;
186 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
187 if (lp_srv_maxprotocol() < PROTOCOL_SMB2_10) {
188 break;
190 if (lp_srv_minprotocol() > PROTOCOL_SMB2_10) {
191 break;
194 dialect = SVAL(indyn, c*2);
195 if (dialect == SMB2_DIALECT_REVISION_210) {
196 protocol = PROTOCOL_SMB2_10;
197 break;
201 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
202 if (lp_srv_maxprotocol() < PROTOCOL_SMB2_02) {
203 break;
205 if (lp_srv_minprotocol() > PROTOCOL_SMB2_02) {
206 break;
209 dialect = SVAL(indyn, c*2);
210 if (dialect == SMB2_DIALECT_REVISION_202) {
211 protocol = PROTOCOL_SMB2_02;
212 break;
216 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
217 if (lp_srv_maxprotocol() < PROTOCOL_SMB2_10) {
218 break;
221 dialect = SVAL(indyn, c*2);
222 if (dialect == SMB2_DIALECT_REVISION_2FF) {
223 if (req->sconn->smb2.negprot_2ff) {
224 req->sconn->smb2.negprot_2ff = false;
225 protocol = PROTOCOL_SMB2_10;
226 break;
231 if (protocol == PROTOCOL_NONE) {
232 return smbd_smb2_request_error(req, NT_STATUS_NOT_SUPPORTED);
235 if (get_remote_arch() != RA_SAMBA) {
236 set_remote_arch(RA_VISTA);
239 fstr_sprintf(remote_proto, "SMB%X_%02X",
240 (dialect >> 8) & 0xFF, dialect & 0xFF);
242 reload_services(req->sconn, conn_snum_used, true);
243 DEBUG(3,("Selected protocol %s\n", remote_proto));
245 /* negprot_spnego() returns a the server guid in the first 16 bytes */
246 negprot_spnego_blob = negprot_spnego(req, req->sconn);
247 if (negprot_spnego_blob.data == NULL) {
248 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
251 if (negprot_spnego_blob.length < 16) {
252 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
255 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
256 if (lp_server_signing() == SMB_SIGNING_REQUIRED) {
257 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
260 capabilities = 0;
261 if (lp_host_msdfs()) {
262 capabilities |= SMB2_CAP_DFS;
265 if ((protocol >= PROTOCOL_SMB2_24) &&
266 (lp_smb_encrypt(-1) != SMB_SIGNING_OFF) &&
267 (in_capabilities & SMB2_CAP_ENCRYPTION)) {
268 capabilities |= SMB2_CAP_ENCRYPTION;
272 * 0x10000 (65536) is the maximum allowed message size
273 * for SMB 2.0
275 max_limit = 0x10000;
277 if (protocol >= PROTOCOL_SMB2_10) {
278 int p = 0;
280 if (tsocket_address_is_inet(req->sconn->local_address, "ip")) {
281 p = tsocket_address_inet_port(req->sconn->local_address);
284 /* largeMTU is not supported over NBT (tcp port 139) */
285 if (p != NBT_SMB_PORT) {
286 capabilities |= SMB2_CAP_LARGE_MTU;
287 req->sconn->smb2.supports_multicredit = true;
289 /* SMB >= 2.1 has 1 MB of allowed size */
290 max_limit = 0x100000; /* 1MB */
295 * the defaults are 1MB, but we'll limit this to max_limit based on
296 * the dialect (64kb for SMB2.0, 1MB for SMB2.1 with LargeMTU)
298 * user configured values exceeding the limits will be overwritten,
299 * only smaller values will be accepted
302 max_trans = MIN(max_limit, lp_smb2_max_trans());
303 max_read = MIN(max_limit, lp_smb2_max_read());
304 max_write = MIN(max_limit, lp_smb2_max_write());
306 security_offset = SMB2_HDR_BODY + 0x40;
308 #if 1
309 /* Try SPNEGO auth... */
310 security_buffer = data_blob_const(negprot_spnego_blob.data + 16,
311 negprot_spnego_blob.length - 16);
312 #else
313 /* for now we want raw NTLMSSP */
314 security_buffer = data_blob_const(NULL, 0);
315 #endif
317 out_guid_blob = data_blob_const(negprot_spnego_blob.data, 16);
318 status = GUID_from_ndr_blob(&out_guid_blob, &out_guid);
319 if (!NT_STATUS_IS_OK(status)) {
320 return smbd_smb2_request_error(req, status);
323 outbody = data_blob_talloc(req->out.vector, NULL, 0x40);
324 if (outbody.data == NULL) {
325 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
328 SSVAL(outbody.data, 0x00, 0x40 + 1); /* struct size */
329 SSVAL(outbody.data, 0x02,
330 security_mode); /* security mode */
331 SSVAL(outbody.data, 0x04, dialect); /* dialect revision */
332 SSVAL(outbody.data, 0x06, 0); /* reserved */
333 memcpy(outbody.data + 0x08,
334 out_guid_blob.data, 16); /* server guid */
335 SIVAL(outbody.data, 0x18,
336 capabilities); /* capabilities */
337 SIVAL(outbody.data, 0x1C, max_trans); /* max transact size */
338 SIVAL(outbody.data, 0x20, max_read); /* max read size */
339 SIVAL(outbody.data, 0x24, max_write); /* max write size */
340 SBVAL(outbody.data, 0x28, now); /* system time */
341 SBVAL(outbody.data, 0x30, 0); /* server start time */
342 SSVAL(outbody.data, 0x38,
343 security_offset); /* security buffer offset */
344 SSVAL(outbody.data, 0x3A,
345 security_buffer.length); /* security buffer length */
346 SIVAL(outbody.data, 0x3C, 0); /* reserved */
348 outdyn = security_buffer;
350 req->sconn->using_smb2 = true;
352 if (dialect != SMB2_DIALECT_REVISION_2FF) {
353 struct smbXsrv_connection *conn = req->sconn->conn;
355 status = smbXsrv_connection_init_tables(conn, protocol);
356 if (!NT_STATUS_IS_OK(status)) {
357 return smbd_smb2_request_error(req, status);
360 conn->smb2.client.capabilities = in_capabilities;
361 conn->smb2.client.security_mode = in_security_mode;
362 conn->smb2.client.guid = in_guid;
363 conn->smb2.client.num_dialects = dialect_count;
364 conn->smb2.client.dialects = talloc_array(conn,
365 uint16_t,
366 dialect_count);
367 if (conn->smb2.client.dialects == NULL) {
368 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
370 for (c=0; c < dialect_count; c++) {
371 conn->smb2.client.dialects[c] = SVAL(indyn, c*2);
374 conn->smb2.server.capabilities = capabilities;
375 conn->smb2.server.security_mode = security_mode;
376 conn->smb2.server.guid = out_guid;
377 conn->smb2.server.dialect = dialect;
378 conn->smb2.server.max_trans = max_trans;
379 conn->smb2.server.max_read = max_read;
380 conn->smb2.server.max_write = max_write;
382 req->sconn->smb2.max_trans = max_trans;
383 req->sconn->smb2.max_read = max_read;
384 req->sconn->smb2.max_write = max_write;
387 return smbd_smb2_request_done(req, outbody, &outdyn);