s3:smb2_negprot: remove NBT_HDR_SIZE from smbd_smb2_first_negprot()
[Samba.git] / source3 / smbd / smb2_negprot.c
blobe9d17e283b4f68e3596ea78e562b4b791390b5aa
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_inpdu;
37 uint8_t *smb2_hdr;
38 uint8_t *smb2_body;
39 uint8_t *smb2_dyn;
40 size_t len = SMB2_HDR_BODY + 0x24 + 2;
42 smb2_inpdu = talloc_zero_array(talloc_tos(), uint8_t, len);
43 if (smb2_inpdu == NULL) {
44 DEBUG(0, ("Could not push spnego blob\n"));
45 reply_nterror(req, NT_STATUS_NO_MEMORY);
46 return;
48 smb2_hdr = smb2_inpdu;
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->xconn, smb2_inpdu, 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 struct smbXsrv_connection *xconn = req->xconn;
82 xconn->smb2.allow_2ff = true;
83 reply_smb20xx(req, SMB2_DIALECT_REVISION_2FF);
86 enum protocol_types smbd_smb2_protocol_dialect_match(const uint8_t *indyn,
87 const int dialect_count,
88 uint16_t *dialect)
90 size_t c = 0;
91 enum protocol_types protocol = PROTOCOL_NONE;
93 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
94 if (lp_server_max_protocol() < PROTOCOL_SMB3_00) {
95 break;
97 if (lp_server_min_protocol() > PROTOCOL_SMB3_00) {
98 break;
101 *dialect = SVAL(indyn, c*2);
102 if (*dialect == SMB3_DIALECT_REVISION_300) {
103 protocol = PROTOCOL_SMB3_00;
104 break;
108 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
109 if (lp_server_max_protocol() < PROTOCOL_SMB2_24) {
110 break;
112 if (lp_server_min_protocol() > PROTOCOL_SMB2_24) {
113 break;
116 *dialect = SVAL(indyn, c*2);
117 if (*dialect == SMB2_DIALECT_REVISION_224) {
118 protocol = PROTOCOL_SMB2_24;
119 break;
123 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
124 if (lp_server_max_protocol() < PROTOCOL_SMB2_22) {
125 break;
127 if (lp_server_min_protocol() > PROTOCOL_SMB2_22) {
128 break;
131 *dialect = SVAL(indyn, c*2);
132 if (*dialect == SMB2_DIALECT_REVISION_222) {
133 protocol = PROTOCOL_SMB2_22;
134 break;
138 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
139 if (lp_server_max_protocol() < PROTOCOL_SMB2_10) {
140 break;
142 if (lp_server_min_protocol() > PROTOCOL_SMB2_10) {
143 break;
146 *dialect = SVAL(indyn, c*2);
147 if (*dialect == SMB2_DIALECT_REVISION_210) {
148 protocol = PROTOCOL_SMB2_10;
149 break;
153 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
154 if (lp_server_max_protocol() < PROTOCOL_SMB2_02) {
155 break;
157 if (lp_server_min_protocol() > PROTOCOL_SMB2_02) {
158 break;
161 *dialect = SVAL(indyn, c*2);
162 if (*dialect == SMB2_DIALECT_REVISION_202) {
163 protocol = PROTOCOL_SMB2_02;
164 break;
168 return protocol;
171 NTSTATUS smbd_smb2_request_process_negprot(struct smbd_smb2_request *req)
173 struct smbXsrv_connection *xconn = req->xconn;
174 NTSTATUS status;
175 const uint8_t *inbody;
176 const uint8_t *indyn = NULL;
177 DATA_BLOB outbody;
178 DATA_BLOB outdyn;
179 DATA_BLOB negprot_spnego_blob;
180 uint16_t security_offset;
181 DATA_BLOB security_buffer;
182 size_t expected_dyn_size = 0;
183 size_t c;
184 uint16_t security_mode;
185 uint16_t dialect_count;
186 uint16_t in_security_mode;
187 uint32_t in_capabilities;
188 DATA_BLOB in_guid_blob;
189 struct GUID in_guid;
190 uint16_t dialect = 0;
191 uint32_t capabilities;
192 DATA_BLOB out_guid_blob;
193 struct GUID out_guid;
194 enum protocol_types protocol = PROTOCOL_NONE;
195 uint32_t max_limit;
196 uint32_t max_trans = lp_smb2_max_trans();
197 uint32_t max_read = lp_smb2_max_read();
198 uint32_t max_write = lp_smb2_max_write();
199 NTTIME now = timeval_to_nttime(&req->request_time);
201 status = smbd_smb2_request_verify_sizes(req, 0x24);
202 if (!NT_STATUS_IS_OK(status)) {
203 return smbd_smb2_request_error(req, status);
205 inbody = SMBD_SMB2_IN_BODY_PTR(req);
207 dialect_count = SVAL(inbody, 0x02);
209 in_security_mode = SVAL(inbody, 0x04);
210 in_capabilities = IVAL(inbody, 0x08);
211 in_guid_blob = data_blob_const(inbody + 0x0C, 16);
213 if (dialect_count == 0) {
214 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
217 status = GUID_from_ndr_blob(&in_guid_blob, &in_guid);
218 if (!NT_STATUS_IS_OK(status)) {
219 return smbd_smb2_request_error(req, status);
222 expected_dyn_size = dialect_count * 2;
223 if (SMBD_SMB2_IN_DYN_LEN(req) < expected_dyn_size) {
224 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
226 indyn = SMBD_SMB2_IN_DYN_PTR(req);
228 protocol = smbd_smb2_protocol_dialect_match(indyn,
229 dialect_count,
230 &dialect);
232 for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
233 if (lp_server_max_protocol() < PROTOCOL_SMB2_10) {
234 break;
237 dialect = SVAL(indyn, c*2);
238 if (dialect == SMB2_DIALECT_REVISION_2FF) {
239 if (xconn->smb2.allow_2ff) {
240 xconn->smb2.allow_2ff = false;
241 protocol = PROTOCOL_SMB2_10;
242 break;
247 if (protocol == PROTOCOL_NONE) {
248 return smbd_smb2_request_error(req, NT_STATUS_NOT_SUPPORTED);
251 if (get_remote_arch() != RA_SAMBA) {
252 set_remote_arch(RA_VISTA);
255 fstr_sprintf(remote_proto, "SMB%X_%02X",
256 (dialect >> 8) & 0xFF, dialect & 0xFF);
258 reload_services(req->sconn, conn_snum_used, true);
259 DEBUG(3,("Selected protocol %s\n", remote_proto));
261 /* negprot_spnego() returns a the server guid in the first 16 bytes */
262 negprot_spnego_blob = negprot_spnego(req, req->sconn);
263 if (negprot_spnego_blob.data == NULL) {
264 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
267 if (negprot_spnego_blob.length < 16) {
268 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
271 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
272 if (lp_server_signing() == SMB_SIGNING_REQUIRED) {
273 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
276 capabilities = 0;
277 if (lp_host_msdfs()) {
278 capabilities |= SMB2_CAP_DFS;
281 if ((protocol >= PROTOCOL_SMB2_24) &&
282 (lp_smb_encrypt(-1) != SMB_SIGNING_OFF) &&
283 (in_capabilities & SMB2_CAP_ENCRYPTION)) {
284 capabilities |= SMB2_CAP_ENCRYPTION;
288 * 0x10000 (65536) is the maximum allowed message size
289 * for SMB 2.0
291 max_limit = 0x10000;
293 if (protocol >= PROTOCOL_SMB2_10) {
294 int p = 0;
296 if (tsocket_address_is_inet(req->sconn->local_address, "ip")) {
297 p = tsocket_address_inet_port(req->sconn->local_address);
300 /* largeMTU is not supported over NBT (tcp port 139) */
301 if (p != NBT_SMB_PORT) {
302 capabilities |= SMB2_CAP_LARGE_MTU;
303 xconn->smb2.credits.multicredit = true;
305 /* Windows 2012R2 allows up to 8 MB */
306 max_limit = 0x800000; /* 8MB */
311 * the defaults are 8MB, but we'll limit this to max_limit based on
312 * the dialect (64kb for SMB 2.0, 8MB for SMB >= 2.1 with LargeMTU)
314 * user configured values exceeding the limits will be overwritten,
315 * only smaller values will be accepted
318 max_trans = MIN(max_limit, lp_smb2_max_trans());
319 max_read = MIN(max_limit, lp_smb2_max_read());
320 max_write = MIN(max_limit, lp_smb2_max_write());
322 security_offset = SMB2_HDR_BODY + 0x40;
324 #if 1
325 /* Try SPNEGO auth... */
326 security_buffer = data_blob_const(negprot_spnego_blob.data + 16,
327 negprot_spnego_blob.length - 16);
328 #else
329 /* for now we want raw NTLMSSP */
330 security_buffer = data_blob_const(NULL, 0);
331 #endif
333 out_guid_blob = data_blob_const(negprot_spnego_blob.data, 16);
334 status = GUID_from_ndr_blob(&out_guid_blob, &out_guid);
335 if (!NT_STATUS_IS_OK(status)) {
336 return smbd_smb2_request_error(req, status);
339 outbody = smbd_smb2_generate_outbody(req, 0x40);
340 if (outbody.data == NULL) {
341 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
344 SSVAL(outbody.data, 0x00, 0x40 + 1); /* struct size */
345 SSVAL(outbody.data, 0x02,
346 security_mode); /* security mode */
347 SSVAL(outbody.data, 0x04, dialect); /* dialect revision */
348 SSVAL(outbody.data, 0x06, 0); /* reserved */
349 memcpy(outbody.data + 0x08,
350 out_guid_blob.data, 16); /* server guid */
351 SIVAL(outbody.data, 0x18,
352 capabilities); /* capabilities */
353 SIVAL(outbody.data, 0x1C, max_trans); /* max transact size */
354 SIVAL(outbody.data, 0x20, max_read); /* max read size */
355 SIVAL(outbody.data, 0x24, max_write); /* max write size */
356 SBVAL(outbody.data, 0x28, now); /* system time */
357 SBVAL(outbody.data, 0x30, 0); /* server start time */
358 SSVAL(outbody.data, 0x38,
359 security_offset); /* security buffer offset */
360 SSVAL(outbody.data, 0x3A,
361 security_buffer.length); /* security buffer length */
362 SIVAL(outbody.data, 0x3C, 0); /* reserved */
364 outdyn = security_buffer;
366 req->sconn->using_smb2 = true;
368 if (dialect != SMB2_DIALECT_REVISION_2FF) {
369 status = smbXsrv_connection_init_tables(xconn, protocol);
370 if (!NT_STATUS_IS_OK(status)) {
371 return smbd_smb2_request_error(req, status);
374 xconn->smb2.client.capabilities = in_capabilities;
375 xconn->smb2.client.security_mode = in_security_mode;
376 xconn->smb2.client.guid = in_guid;
377 xconn->smb2.client.num_dialects = dialect_count;
378 xconn->smb2.client.dialects = talloc_array(xconn,
379 uint16_t,
380 dialect_count);
381 if (xconn->smb2.client.dialects == NULL) {
382 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
384 for (c=0; c < dialect_count; c++) {
385 xconn->smb2.client.dialects[c] = SVAL(indyn, c*2);
388 xconn->smb2.server.capabilities = capabilities;
389 xconn->smb2.server.security_mode = security_mode;
390 xconn->smb2.server.guid = out_guid;
391 xconn->smb2.server.dialect = dialect;
392 xconn->smb2.server.max_trans = max_trans;
393 xconn->smb2.server.max_read = max_read;
394 xconn->smb2.server.max_write = max_write;
397 return smbd_smb2_request_done(req, outbody, &outdyn);