s3:smb2_server: remember the max_{trans,read,write} sizes we negotiated (bug #8473)
[rusty/samba.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
26 /*
27  * this is the entry point if SMB2 is selected via
28  * the SMB negprot and the given dialect.
29  */
30 static void reply_smb20xx(struct smb_request *req, uint16_t dialect)
31 {
32         uint8_t *smb2_inbuf;
33         uint8_t *smb2_hdr;
34         uint8_t *smb2_body;
35         uint8_t *smb2_dyn;
36         size_t len = 4 + SMB2_HDR_BODY + 0x24 + 2;
37
38         smb2_inbuf = talloc_zero_array(talloc_tos(), uint8_t, len);
39         if (smb2_inbuf == NULL) {
40                 DEBUG(0, ("Could not push spnego blob\n"));
41                 reply_nterror(req, NT_STATUS_NO_MEMORY);
42                 return;
43         }
44         smb2_hdr = smb2_inbuf + 4;
45         smb2_body = smb2_hdr + SMB2_HDR_BODY;
46         smb2_dyn = smb2_body + 0x24;
47
48         SIVAL(smb2_hdr, SMB2_HDR_PROTOCOL_ID,   SMB2_MAGIC);
49         SIVAL(smb2_hdr, SMB2_HDR_LENGTH,        SMB2_HDR_BODY);
50
51         SSVAL(smb2_body, 0x00, 0x0024); /* struct size */
52         SSVAL(smb2_body, 0x02, 0x0001); /* dialect count */
53
54         SSVAL(smb2_dyn,  0x00, dialect);
55
56         req->outbuf = NULL;
57
58         smbd_smb2_first_negprot(req->sconn, smb2_inbuf, len);
59         return;
60 }
61
62 /*
63  * this is the entry point if SMB2 is selected via
64  * the SMB negprot and the "SMB 2.002" dialect.
65  */
66 void reply_smb2002(struct smb_request *req, uint16_t choice)
67 {
68         reply_smb20xx(req, SMB2_DIALECT_REVISION_202);
69 }
70
71 /*
72  * this is the entry point if SMB2 is selected via
73  * the SMB negprot and the "SMB 2.???" dialect.
74  */
75 void reply_smb20ff(struct smb_request *req, uint16_t choice)
76 {
77         req->sconn->smb2.negprot_2ff = true;
78         reply_smb20xx(req, SMB2_DIALECT_REVISION_2FF);
79 }
80
81 NTSTATUS smbd_smb2_request_process_negprot(struct smbd_smb2_request *req)
82 {
83         NTSTATUS status;
84         const uint8_t *inbody;
85         const uint8_t *indyn = NULL;
86         int i = req->current_idx;
87         DATA_BLOB outbody;
88         DATA_BLOB outdyn;
89         DATA_BLOB negprot_spnego_blob;
90         uint16_t security_offset;
91         DATA_BLOB security_buffer;
92         size_t expected_dyn_size = 0;
93         size_t c;
94         uint16_t security_mode;
95         uint16_t dialect_count;
96         uint16_t dialect = 0;
97         uint32_t capabilities;
98         enum protocol_types protocol = PROTOCOL_NONE;
99         uint32_t max_limit;
100         uint32_t max_trans = lp_smb2_max_trans();
101         uint32_t max_read = lp_smb2_max_read();
102         uint32_t max_write = lp_smb2_max_write();
103
104         status = smbd_smb2_request_verify_sizes(req, 0x24);
105         if (!NT_STATUS_IS_OK(status)) {
106                 return smbd_smb2_request_error(req, status);
107         }
108         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
109
110         dialect_count = SVAL(inbody, 0x02);
111         if (dialect_count == 0) {
112                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
113         }
114
115         expected_dyn_size = dialect_count * 2;
116         if (req->in.vector[i+2].iov_len < expected_dyn_size) {
117                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
118         }
119         indyn = (const uint8_t *)req->in.vector[i+2].iov_base;
120
121         for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
122                 if (lp_maxprotocol() < PROTOCOL_SMB2_10) {
123                         break;
124                 }
125                 if (lp_minprotocol() > PROTOCOL_SMB2_10) {
126                         break;
127                 }
128
129                 dialect = SVAL(indyn, c*2);
130                 if (dialect == SMB2_DIALECT_REVISION_210) {
131                         protocol = PROTOCOL_SMB2_10;
132                         break;
133                 }
134         }
135
136         for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
137                 if (lp_maxprotocol() < PROTOCOL_SMB2_02) {
138                         break;
139                 }
140                 if (lp_minprotocol() > PROTOCOL_SMB2_02) {
141                         break;
142                 }
143
144                 dialect = SVAL(indyn, c*2);
145                 if (dialect == SMB2_DIALECT_REVISION_202) {
146                         protocol = PROTOCOL_SMB2_02;
147                         break;
148                 }
149         }
150
151         for (c=0; protocol == PROTOCOL_NONE && c < dialect_count; c++) {
152                 if (lp_maxprotocol() < PROTOCOL_SMB2_10) {
153                         break;
154                 }
155
156                 dialect = SVAL(indyn, c*2);
157                 if (dialect == SMB2_DIALECT_REVISION_2FF) {
158                         if (req->sconn->smb2.negprot_2ff) {
159                                 req->sconn->smb2.negprot_2ff = false;
160                                 protocol = PROTOCOL_SMB2_10;
161                                 break;
162                         }
163                 }
164         }
165
166         if (protocol == PROTOCOL_NONE) {
167                 return smbd_smb2_request_error(req, NT_STATUS_NOT_SUPPORTED);
168         }
169
170         if (dialect != SMB2_DIALECT_REVISION_2FF) {
171                 set_Protocol(protocol);
172         }
173
174         if (get_remote_arch() != RA_SAMBA) {
175                 set_remote_arch(RA_VISTA);
176         }
177
178         /* negprot_spnego() returns a the server guid in the first 16 bytes */
179         negprot_spnego_blob = negprot_spnego(req, req->sconn);
180         if (negprot_spnego_blob.data == NULL) {
181                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
182         }
183
184         if (negprot_spnego_blob.length < 16) {
185                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
186         }
187
188         security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
189         if (lp_server_signing() == Required) {
190                 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
191         }
192
193         capabilities = 0;
194         if (lp_host_msdfs()) {
195                 capabilities |= SMB2_CAP_DFS;
196         }
197
198         /*
199          * Unless we implement SMB2_CAP_LARGE_MTU,
200          * 0x10000 (65536) is the maximum allowed message size
201          */
202         max_limit = 0x10000;
203
204         max_trans = MIN(max_limit, max_trans);
205         max_read  = MIN(max_limit, max_read);
206         max_write = MIN(max_limit, max_write);
207
208         security_offset = SMB2_HDR_BODY + 0x40;
209
210 #if 1
211         /* Try SPNEGO auth... */
212         security_buffer = data_blob_const(negprot_spnego_blob.data + 16,
213                                           negprot_spnego_blob.length - 16);
214 #else
215         /* for now we want raw NTLMSSP */
216         security_buffer = data_blob_const(NULL, 0);
217 #endif
218
219         outbody = data_blob_talloc(req->out.vector, NULL, 0x40);
220         if (outbody.data == NULL) {
221                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
222         }
223
224         SSVAL(outbody.data, 0x00, 0x40 + 1);    /* struct size */
225         SSVAL(outbody.data, 0x02,
226               security_mode);                   /* security mode */
227         SSVAL(outbody.data, 0x04, dialect);     /* dialect revision */
228         SSVAL(outbody.data, 0x06, 0);           /* reserved */
229         memcpy(outbody.data + 0x08,
230                negprot_spnego_blob.data, 16);   /* server guid */
231         SIVAL(outbody.data, 0x18,
232               capabilities);                    /* capabilities */
233         SIVAL(outbody.data, 0x1C, max_trans);   /* max transact size */
234         SIVAL(outbody.data, 0x20, max_trans);   /* max read size */
235         SIVAL(outbody.data, 0x24, max_trans);   /* max write size */
236         SBVAL(outbody.data, 0x28, 0);           /* system time */
237         SBVAL(outbody.data, 0x30, 0);           /* server start time */
238         SSVAL(outbody.data, 0x38,
239               security_offset);                 /* security buffer offset */
240         SSVAL(outbody.data, 0x3A,
241               security_buffer.length);          /* security buffer length */
242         SIVAL(outbody.data, 0x3C, 0);           /* reserved */
243
244         outdyn = security_buffer;
245
246         req->sconn->using_smb2 = true;
247         req->sconn->smb2.max_trans = max_trans;
248         req->sconn->smb2.max_read  = max_read;
249         req->sconn->smb2.max_write = max_write;
250
251         return smbd_smb2_request_done(req, outbody, &outdyn);
252 }