3f76f3f0f518aee4a07c74085bc2235378a87517
[sfrench/samba-autobuild/.git] / source3 / smbd / smb2_negprot.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
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.
11
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.
16
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/>.
19 */
20
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"
27
28 extern fstring remote_proto;
29
30 /*
31  * this is the entry point if SMB2 is selected via
32  * the SMB negprot and the given dialect.
33  */
34 static void reply_smb20xx(struct smb_request *req, uint16_t dialect)
35 {
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;
41
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;
47         }
48         smb2_hdr = smb2_inbuf + 4;
49         smb2_body = smb2_hdr + SMB2_HDR_BODY;
50         smb2_dyn = smb2_body + 0x24;
51
52         SIVAL(smb2_hdr, SMB2_HDR_PROTOCOL_ID,   SMB2_MAGIC);
53         SIVAL(smb2_hdr, SMB2_HDR_LENGTH,        SMB2_HDR_BODY);
54
55         SSVAL(smb2_body, 0x00, 0x0024); /* struct size */
56         SSVAL(smb2_body, 0x02, 0x0001); /* dialect count */
57
58         SSVAL(smb2_dyn,  0x00, dialect);
59
60         req->outbuf = NULL;
61
62         smbd_smb2_first_negprot(req->xconn, smb2_inbuf, len);
63         return;
64 }
65
66 /*
67  * this is the entry point if SMB2 is selected via
68  * the SMB negprot and the "SMB 2.002" dialect.
69  */
70 void reply_smb2002(struct smb_request *req, uint16_t choice)
71 {
72         reply_smb20xx(req, SMB2_DIALECT_REVISION_202);
73 }
74
75 /*
76  * this is the entry point if SMB2 is selected via
77  * the SMB negprot and the "SMB 2.???" dialect.
78  */
79 void reply_smb20ff(struct smb_request *req, uint16_t choice)
80 {
81         struct smbXsrv_connection *xconn = req->sconn->conn;
82         xconn->smb2.allow_2ff = true;
83         reply_smb20xx(req, SMB2_DIALECT_REVISION_2FF);
84 }
85
86 enum protocol_types smbd_smb2_protocol_dialect_match(const uint8_t *indyn,
87                                 const int dialect_count,
88                                 uint16_t *dialect)
89 {
90         size_t c = 0;
91         enum protocol_types protocol = PROTOCOL_NONE;
92
93         for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
94                 if (lp_server_max_protocol() < PROTOCOL_SMB3_00) {
95                         break;
96                 }
97                 if (lp_server_min_protocol() > PROTOCOL_SMB3_00) {
98                         break;
99                 }
100
101                 *dialect = SVAL(indyn, c*2);
102                 if (*dialect == SMB3_DIALECT_REVISION_300) {
103                         protocol = PROTOCOL_SMB3_00;
104                         break;
105                 }
106         }
107
108         for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
109                 if (lp_server_max_protocol() < PROTOCOL_SMB2_24) {
110                         break;
111                 }
112                 if (lp_server_min_protocol() > PROTOCOL_SMB2_24) {
113                         break;
114                 }
115
116                 *dialect = SVAL(indyn, c*2);
117                 if (*dialect == SMB2_DIALECT_REVISION_224) {
118                         protocol = PROTOCOL_SMB2_24;
119                         break;
120                 }
121         }
122
123         for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
124                 if (lp_server_max_protocol() < PROTOCOL_SMB2_22) {
125                         break;
126                 }
127                 if (lp_server_min_protocol() > PROTOCOL_SMB2_22) {
128                         break;
129                 }
130
131                 *dialect = SVAL(indyn, c*2);
132                 if (*dialect == SMB2_DIALECT_REVISION_222) {
133                         protocol = PROTOCOL_SMB2_22;
134                         break;
135                 }
136         }
137
138         for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
139                 if (lp_server_max_protocol() < PROTOCOL_SMB2_10) {
140                         break;
141                 }
142                 if (lp_server_min_protocol() > PROTOCOL_SMB2_10) {
143                         break;
144                 }
145
146                 *dialect = SVAL(indyn, c*2);
147                 if (*dialect == SMB2_DIALECT_REVISION_210) {
148                         protocol = PROTOCOL_SMB2_10;
149                         break;
150                 }
151         }
152
153         for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
154                 if (lp_server_max_protocol() < PROTOCOL_SMB2_02) {
155                         break;
156                 }
157                 if (lp_server_min_protocol() > PROTOCOL_SMB2_02) {
158                         break;
159                 }
160
161                 *dialect = SVAL(indyn, c*2);
162                 if (*dialect == SMB2_DIALECT_REVISION_202) {
163                         protocol = PROTOCOL_SMB2_02;
164                         break;
165                 }
166         }
167
168         return protocol;
169 }
170
171 NTSTATUS smbd_smb2_request_process_negprot(struct smbd_smb2_request *req)
172 {
173         struct smbXsrv_connection *xconn = req->sconn->conn;
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);
200
201         status = smbd_smb2_request_verify_sizes(req, 0x24);
202         if (!NT_STATUS_IS_OK(status)) {
203                 return smbd_smb2_request_error(req, status);
204         }
205         inbody = SMBD_SMB2_IN_BODY_PTR(req);
206
207         dialect_count = SVAL(inbody, 0x02);
208
209         in_security_mode = SVAL(inbody, 0x04);
210         in_capabilities = IVAL(inbody, 0x08);
211         in_guid_blob = data_blob_const(inbody + 0x0C, 16);
212
213         if (dialect_count == 0) {
214                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
215         }
216
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);
220         }
221
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);
225         }
226         indyn = SMBD_SMB2_IN_DYN_PTR(req);
227
228         protocol = smbd_smb2_protocol_dialect_match(indyn,
229                                         dialect_count,
230                                         &dialect);
231
232         for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
233                 if (lp_server_max_protocol() < PROTOCOL_SMB2_10) {
234                         break;
235                 }
236
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;
243                         }
244                 }
245         }
246
247         if (protocol == PROTOCOL_NONE) {
248                 return smbd_smb2_request_error(req, NT_STATUS_NOT_SUPPORTED);
249         }
250
251         if (get_remote_arch() != RA_SAMBA) {
252                 set_remote_arch(RA_VISTA);
253         }
254
255         fstr_sprintf(remote_proto, "SMB%X_%02X",
256                      (dialect >> 8) & 0xFF, dialect & 0xFF);
257
258         reload_services(req->sconn, conn_snum_used, true);
259         DEBUG(3,("Selected protocol %s\n", remote_proto));
260
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);
265         }
266
267         if (negprot_spnego_blob.length < 16) {
268                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
269         }
270
271         security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
272         if (lp_server_signing() == SMB_SIGNING_REQUIRED) {
273                 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
274         }
275
276         capabilities = 0;
277         if (lp_host_msdfs()) {
278                 capabilities |= SMB2_CAP_DFS;
279         }
280
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;
285         }
286
287         /*
288          * 0x10000 (65536) is the maximum allowed message size
289          * for SMB 2.0
290          */
291         max_limit = 0x10000;
292
293         if (protocol >= PROTOCOL_SMB2_10) {
294                 int p = 0;
295
296                 if (tsocket_address_is_inet(req->sconn->local_address, "ip")) {
297                         p = tsocket_address_inet_port(req->sconn->local_address);
298                 }
299
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;
304
305                         /* Windows 2012R2 allows up to 8 MB */
306                         max_limit = 0x800000; /* 8MB */
307                 }
308         }
309
310         /*
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)
313          *
314          * user configured values exceeding the limits will be overwritten,
315          * only smaller values will be accepted
316          */
317
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());
321
322         security_offset = SMB2_HDR_BODY + 0x40;
323
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
332
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);
337         }
338
339         outbody = smbd_smb2_generate_outbody(req, 0x40);
340         if (outbody.data == NULL) {
341                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
342         }
343
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 */
363
364         outdyn = security_buffer;
365
366         req->sconn->using_smb2 = true;
367
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);
372                 }
373
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);
383                 }
384                 for (c=0; c < dialect_count; c++) {
385                         xconn->smb2.client.dialects[c] = SVAL(indyn, c*2);
386                 }
387
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;
395         }
396
397         return smbd_smb2_request_done(req, outbody, &outdyn);
398 }