1 | /*
|
---|
2 | Unix SMB2 implementation.
|
---|
3 |
|
---|
4 | Copyright (C) Andrew Tridgell 2005
|
---|
5 | Copyright (C) Stefan Metzmacher 2005
|
---|
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 "system/time.h"
|
---|
23 | #include "libcli/smb2/smb2.h"
|
---|
24 | #include "libcli/smb2/smb2_calls.h"
|
---|
25 | #include "smb_server/smb_server.h"
|
---|
26 | #include "smb_server/smb2/smb2_server.h"
|
---|
27 | #include "smbd/service_stream.h"
|
---|
28 | #include "lib/stream/packet.h"
|
---|
29 | #include "ntvfs/ntvfs.h"
|
---|
30 | #include "param/param.h"
|
---|
31 | #include "auth/auth.h"
|
---|
32 |
|
---|
33 |
|
---|
34 | /* fill in the bufinfo */
|
---|
35 | void smb2srv_setup_bufinfo(struct smb2srv_request *req)
|
---|
36 | {
|
---|
37 | req->in.bufinfo.mem_ctx = req;
|
---|
38 | req->in.bufinfo.flags = BUFINFO_FLAG_UNICODE | BUFINFO_FLAG_SMB2;
|
---|
39 | req->in.bufinfo.align_base = req->in.buffer;
|
---|
40 | if (req->in.dynamic) {
|
---|
41 | req->in.bufinfo.data = req->in.dynamic;
|
---|
42 | req->in.bufinfo.data_size = req->in.body_size - req->in.body_fixed;
|
---|
43 | } else {
|
---|
44 | req->in.bufinfo.data = NULL;
|
---|
45 | req->in.bufinfo.data_size = 0;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | static int smb2srv_request_destructor(struct smb2srv_request *req)
|
---|
50 | {
|
---|
51 | DLIST_REMOVE(req->smb_conn->requests2.list, req);
|
---|
52 | if (req->pending_id) {
|
---|
53 | idr_remove(req->smb_conn->requests2.idtree_req, req->pending_id);
|
---|
54 | }
|
---|
55 | return 0;
|
---|
56 | }
|
---|
57 |
|
---|
58 | static int smb2srv_request_deny_destructor(struct smb2srv_request *req)
|
---|
59 | {
|
---|
60 | return -1;
|
---|
61 | }
|
---|
62 |
|
---|
63 | struct smb2srv_request *smb2srv_init_request(struct smbsrv_connection *smb_conn)
|
---|
64 | {
|
---|
65 | struct smb2srv_request *req;
|
---|
66 |
|
---|
67 | req = talloc_zero(smb_conn, struct smb2srv_request);
|
---|
68 | if (!req) return NULL;
|
---|
69 |
|
---|
70 | req->smb_conn = smb_conn;
|
---|
71 |
|
---|
72 | talloc_set_destructor(req, smb2srv_request_destructor);
|
---|
73 |
|
---|
74 | return req;
|
---|
75 | }
|
---|
76 |
|
---|
77 | NTSTATUS smb2srv_setup_reply(struct smb2srv_request *req, uint16_t body_fixed_size,
|
---|
78 | bool body_dynamic_present, uint32_t body_dynamic_size)
|
---|
79 | {
|
---|
80 | uint32_t flags = SMB2_HDR_FLAG_REDIRECT;
|
---|
81 | uint32_t pid = IVAL(req->in.hdr, SMB2_HDR_PID);
|
---|
82 | uint32_t tid = IVAL(req->in.hdr, SMB2_HDR_TID);
|
---|
83 |
|
---|
84 | if (req->pending_id) {
|
---|
85 | flags |= SMB2_HDR_FLAG_ASYNC;
|
---|
86 | pid = req->pending_id;
|
---|
87 | tid = 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (body_dynamic_present) {
|
---|
91 | if (body_dynamic_size == 0) {
|
---|
92 | body_dynamic_size = 1;
|
---|
93 | }
|
---|
94 | } else {
|
---|
95 | body_dynamic_size = 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | req->out.size = SMB2_HDR_BODY+NBT_HDR_SIZE+body_fixed_size;
|
---|
99 |
|
---|
100 | req->out.allocated = req->out.size + body_dynamic_size;
|
---|
101 | req->out.buffer = talloc_array(req, uint8_t,
|
---|
102 | req->out.allocated);
|
---|
103 | NT_STATUS_HAVE_NO_MEMORY(req->out.buffer);
|
---|
104 |
|
---|
105 | req->out.hdr = req->out.buffer + NBT_HDR_SIZE;
|
---|
106 | req->out.body = req->out.hdr + SMB2_HDR_BODY;
|
---|
107 | req->out.body_fixed = body_fixed_size;
|
---|
108 | req->out.body_size = body_fixed_size;
|
---|
109 | req->out.dynamic = (body_dynamic_size ? req->out.body + body_fixed_size : NULL);
|
---|
110 |
|
---|
111 | SIVAL(req->out.hdr, 0, SMB2_MAGIC);
|
---|
112 | SSVAL(req->out.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
|
---|
113 | SSVAL(req->out.hdr, SMB2_HDR_EPOCH, 0);
|
---|
114 | SIVAL(req->out.hdr, SMB2_HDR_STATUS, NT_STATUS_V(req->status));
|
---|
115 | SSVAL(req->out.hdr, SMB2_HDR_OPCODE, SVAL(req->in.hdr, SMB2_HDR_OPCODE));
|
---|
116 | SSVAL(req->out.hdr, SMB2_HDR_CREDIT, 0x0001);
|
---|
117 | SIVAL(req->out.hdr, SMB2_HDR_FLAGS, flags);
|
---|
118 | SIVAL(req->out.hdr, SMB2_HDR_NEXT_COMMAND, 0);
|
---|
119 | SBVAL(req->out.hdr, SMB2_HDR_MESSAGE_ID, req->seqnum);
|
---|
120 | SIVAL(req->out.hdr, SMB2_HDR_PID, pid);
|
---|
121 | SIVAL(req->out.hdr, SMB2_HDR_TID, tid);
|
---|
122 | SBVAL(req->out.hdr, SMB2_HDR_SESSION_ID, BVAL(req->in.hdr, SMB2_HDR_SESSION_ID));
|
---|
123 | memset(req->out.hdr+SMB2_HDR_SIGNATURE, 0, 16);
|
---|
124 |
|
---|
125 | /* set the length of the fixed body part and +1 if there's a dynamic part also */
|
---|
126 | SSVAL(req->out.body, 0, body_fixed_size + (body_dynamic_size?1:0));
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * if we have a dynamic part, make sure the first byte
|
---|
130 | * which is always be part of the packet is initialized
|
---|
131 | */
|
---|
132 | if (body_dynamic_size) {
|
---|
133 | req->out.size += 1;
|
---|
134 | SCVAL(req->out.dynamic, 0, 0);
|
---|
135 | }
|
---|
136 |
|
---|
137 | return NT_STATUS_OK;
|
---|
138 | }
|
---|
139 |
|
---|
140 | static NTSTATUS smb2srv_reply(struct smb2srv_request *req);
|
---|
141 |
|
---|
142 | static void smb2srv_chain_reply(struct smb2srv_request *p_req)
|
---|
143 | {
|
---|
144 | NTSTATUS status;
|
---|
145 | struct smb2srv_request *req;
|
---|
146 | uint32_t chain_offset;
|
---|
147 | uint32_t protocol_version;
|
---|
148 | uint16_t buffer_code;
|
---|
149 | uint32_t dynamic_size;
|
---|
150 | uint32_t flags;
|
---|
151 | uint32_t last_hdr_offset;
|
---|
152 |
|
---|
153 | last_hdr_offset = p_req->in.hdr - p_req->in.buffer;
|
---|
154 |
|
---|
155 | chain_offset = p_req->chain_offset;
|
---|
156 | p_req->chain_offset = 0;
|
---|
157 |
|
---|
158 | if (p_req->in.size < (last_hdr_offset + chain_offset + SMB2_MIN_SIZE_NO_BODY)) {
|
---|
159 | DEBUG(2,("Invalid SMB2 chained packet at offset 0x%X from last hdr 0x%X\n",
|
---|
160 | chain_offset, last_hdr_offset));
|
---|
161 | smbsrv_terminate_connection(p_req->smb_conn, "Invalid SMB2 chained packet");
|
---|
162 | return;
|
---|
163 | }
|
---|
164 |
|
---|
165 | protocol_version = IVAL(p_req->in.buffer, last_hdr_offset + chain_offset);
|
---|
166 | if (protocol_version != SMB2_MAGIC) {
|
---|
167 | DEBUG(2,("Invalid SMB chained packet: protocol prefix: 0x%08X\n",
|
---|
168 | protocol_version));
|
---|
169 | smbsrv_terminate_connection(p_req->smb_conn, "NON-SMB2 chained packet");
|
---|
170 | return;
|
---|
171 | }
|
---|
172 |
|
---|
173 | req = smb2srv_init_request(p_req->smb_conn);
|
---|
174 | if (!req) {
|
---|
175 | smbsrv_terminate_connection(p_req->smb_conn, "SMB2 chained packet - no memory");
|
---|
176 | return;
|
---|
177 | }
|
---|
178 |
|
---|
179 | req->in.buffer = talloc_steal(req, p_req->in.buffer);
|
---|
180 | req->in.size = p_req->in.size;
|
---|
181 | req->request_time = p_req->request_time;
|
---|
182 | req->in.allocated = req->in.size;
|
---|
183 |
|
---|
184 | req->in.hdr = req->in.buffer+ last_hdr_offset + chain_offset;
|
---|
185 | req->in.body = req->in.hdr + SMB2_HDR_BODY;
|
---|
186 | req->in.body_size = req->in.size - (last_hdr_offset+ chain_offset + SMB2_HDR_BODY);
|
---|
187 | req->in.dynamic = NULL;
|
---|
188 |
|
---|
189 | req->seqnum = BVAL(req->in.hdr, SMB2_HDR_MESSAGE_ID);
|
---|
190 |
|
---|
191 | if (req->in.body_size < 2) {
|
---|
192 | /* error handling for this is different for negprot to
|
---|
193 | other packet types */
|
---|
194 | uint16_t opcode = SVAL(req->in.hdr, SMB2_HDR_OPCODE);
|
---|
195 | if (opcode == SMB2_OP_NEGPROT) {
|
---|
196 | smbsrv_terminate_connection(req->smb_conn, "Bad body size in SMB2 negprot");
|
---|
197 | } else {
|
---|
198 | smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | buffer_code = SVAL(req->in.body, 0);
|
---|
203 | req->in.body_fixed = (buffer_code & ~1);
|
---|
204 | dynamic_size = req->in.body_size - req->in.body_fixed;
|
---|
205 |
|
---|
206 | if (dynamic_size != 0 && (buffer_code & 1)) {
|
---|
207 | req->in.dynamic = req->in.body + req->in.body_fixed;
|
---|
208 | if (smb2_oob(&req->in, req->in.dynamic, dynamic_size)) {
|
---|
209 | DEBUG(1,("SMB2 chained request invalid dynamic size 0x%x\n",
|
---|
210 | dynamic_size));
|
---|
211 | smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
|
---|
212 | return;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | smb2srv_setup_bufinfo(req);
|
---|
217 |
|
---|
218 | flags = IVAL(req->in.hdr, SMB2_HDR_FLAGS);
|
---|
219 | if (flags & SMB2_HDR_FLAG_CHAINED) {
|
---|
220 | if (p_req->chained_file_handle) {
|
---|
221 | memcpy(req->_chained_file_handle,
|
---|
222 | p_req->_chained_file_handle,
|
---|
223 | sizeof(req->_chained_file_handle));
|
---|
224 | req->chained_file_handle = req->_chained_file_handle;
|
---|
225 | }
|
---|
226 | req->chain_status = p_req->chain_status;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * TODO: - make sure the length field is 64
|
---|
231 | * - make sure it's a request
|
---|
232 | */
|
---|
233 |
|
---|
234 | status = smb2srv_reply(req);
|
---|
235 | if (!NT_STATUS_IS_OK(status)) {
|
---|
236 | smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
|
---|
237 | talloc_free(req);
|
---|
238 | return;
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | void smb2srv_send_reply(struct smb2srv_request *req)
|
---|
243 | {
|
---|
244 | DATA_BLOB blob;
|
---|
245 | NTSTATUS status;
|
---|
246 |
|
---|
247 | if (req->smb_conn->connection->event.fde == NULL) {
|
---|
248 | /* the socket has been destroyed - no point trying to send a reply! */
|
---|
249 | talloc_free(req);
|
---|
250 | return;
|
---|
251 | }
|
---|
252 |
|
---|
253 | if (req->out.size > NBT_HDR_SIZE) {
|
---|
254 | _smb2_setlen(req->out.buffer, req->out.size - NBT_HDR_SIZE);
|
---|
255 | }
|
---|
256 |
|
---|
257 | /* if signing is active on the session then sign the packet */
|
---|
258 | if (req->is_signed) {
|
---|
259 | status = smb2_sign_message(&req->out,
|
---|
260 | req->session->session_info->session_key);
|
---|
261 | if (!NT_STATUS_IS_OK(status)) {
|
---|
262 | smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
|
---|
263 | return;
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | blob = data_blob_const(req->out.buffer, req->out.size);
|
---|
269 | status = packet_send(req->smb_conn->packet, blob);
|
---|
270 | if (!NT_STATUS_IS_OK(status)) {
|
---|
271 | smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
|
---|
272 | }
|
---|
273 | if (req->chain_offset) {
|
---|
274 | smb2srv_chain_reply(req);
|
---|
275 | return;
|
---|
276 | }
|
---|
277 | talloc_free(req);
|
---|
278 | }
|
---|
279 |
|
---|
280 | void smb2srv_send_error(struct smb2srv_request *req, NTSTATUS error)
|
---|
281 | {
|
---|
282 | NTSTATUS status;
|
---|
283 |
|
---|
284 | if (req->smb_conn->connection->event.fde == NULL) {
|
---|
285 | /* the socket has been destroyed - no point trying to send an error! */
|
---|
286 | talloc_free(req);
|
---|
287 | return;
|
---|
288 | }
|
---|
289 |
|
---|
290 | status = smb2srv_setup_reply(req, 8, true, 0);
|
---|
291 | if (!NT_STATUS_IS_OK(status)) {
|
---|
292 | smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
|
---|
293 | talloc_free(req);
|
---|
294 | return;
|
---|
295 | }
|
---|
296 |
|
---|
297 | SIVAL(req->out.hdr, SMB2_HDR_STATUS, NT_STATUS_V(error));
|
---|
298 |
|
---|
299 | SSVAL(req->out.body, 0x02, 0);
|
---|
300 | SIVAL(req->out.body, 0x04, 0);
|
---|
301 |
|
---|
302 | req->chain_status = NT_STATUS_INVALID_PARAMETER;
|
---|
303 |
|
---|
304 | smb2srv_send_reply(req);
|
---|
305 | }
|
---|
306 |
|
---|
307 | static NTSTATUS smb2srv_reply(struct smb2srv_request *req)
|
---|
308 | {
|
---|
309 | uint16_t opcode;
|
---|
310 | uint32_t tid;
|
---|
311 | uint64_t uid;
|
---|
312 | uint32_t flags;
|
---|
313 |
|
---|
314 | if (SVAL(req->in.hdr, SMB2_HDR_LENGTH) != SMB2_HDR_BODY) {
|
---|
315 | smbsrv_terminate_connection(req->smb_conn, "Invalid SMB2 header length");
|
---|
316 | return NT_STATUS_INVALID_PARAMETER;
|
---|
317 | }
|
---|
318 | opcode = SVAL(req->in.hdr, SMB2_HDR_OPCODE);
|
---|
319 | req->chain_offset = IVAL(req->in.hdr, SMB2_HDR_NEXT_COMMAND);
|
---|
320 | req->seqnum = BVAL(req->in.hdr, SMB2_HDR_MESSAGE_ID);
|
---|
321 | tid = IVAL(req->in.hdr, SMB2_HDR_TID);
|
---|
322 | uid = BVAL(req->in.hdr, SMB2_HDR_SESSION_ID);
|
---|
323 | flags = IVAL(req->in.hdr, SMB2_HDR_FLAGS);
|
---|
324 |
|
---|
325 | if (req->smb_conn->highest_smb2_seqnum != 0 &&
|
---|
326 | req->seqnum <= req->smb_conn->highest_smb2_seqnum) {
|
---|
327 | smbsrv_terminate_connection(req->smb_conn, "Invalid SMB2 sequence number");
|
---|
328 | return NT_STATUS_INVALID_PARAMETER;
|
---|
329 | }
|
---|
330 | req->smb_conn->highest_smb2_seqnum = req->seqnum;
|
---|
331 |
|
---|
332 | req->session = smbsrv_session_find(req->smb_conn, uid, req->request_time);
|
---|
333 | req->tcon = smbsrv_smb2_tcon_find(req->session, tid, req->request_time);
|
---|
334 |
|
---|
335 | errno = 0;
|
---|
336 |
|
---|
337 | /* supporting signing is mandatory in SMB2, and is per-packet. So we
|
---|
338 | should check the signature on any incoming packet that is signed, and
|
---|
339 | should give a signed reply to any signed request */
|
---|
340 | if (flags & SMB2_HDR_FLAG_SIGNED) {
|
---|
341 | NTSTATUS status;
|
---|
342 |
|
---|
343 | if (!req->session) goto nosession;
|
---|
344 |
|
---|
345 | req->is_signed = true;
|
---|
346 | status = smb2_check_signature(&req->in,
|
---|
347 | req->session->session_info->session_key);
|
---|
348 | if (!NT_STATUS_IS_OK(status)) {
|
---|
349 | smb2srv_send_error(req, status);
|
---|
350 | return NT_STATUS_OK;
|
---|
351 | }
|
---|
352 | } else if (req->session && req->session->smb2_signing.active) {
|
---|
353 | /* we require signing and this request was not signed */
|
---|
354 | smb2srv_send_error(req, NT_STATUS_ACCESS_DENIED);
|
---|
355 | return NT_STATUS_OK;
|
---|
356 | }
|
---|
357 |
|
---|
358 | if (!NT_STATUS_IS_OK(req->chain_status)) {
|
---|
359 | smb2srv_send_error(req, req->chain_status);
|
---|
360 | return NT_STATUS_OK;
|
---|
361 | }
|
---|
362 |
|
---|
363 | switch (opcode) {
|
---|
364 | case SMB2_OP_NEGPROT:
|
---|
365 | smb2srv_negprot_recv(req);
|
---|
366 | return NT_STATUS_OK;
|
---|
367 | case SMB2_OP_SESSSETUP:
|
---|
368 | smb2srv_sesssetup_recv(req);
|
---|
369 | return NT_STATUS_OK;
|
---|
370 | case SMB2_OP_LOGOFF:
|
---|
371 | if (!req->session) goto nosession;
|
---|
372 | smb2srv_logoff_recv(req);
|
---|
373 | return NT_STATUS_OK;
|
---|
374 | case SMB2_OP_TCON:
|
---|
375 | if (!req->session) goto nosession;
|
---|
376 | smb2srv_tcon_recv(req);
|
---|
377 | return NT_STATUS_OK;
|
---|
378 | case SMB2_OP_TDIS:
|
---|
379 | if (!req->session) goto nosession;
|
---|
380 | if (!req->tcon) goto notcon;
|
---|
381 | smb2srv_tdis_recv(req);
|
---|
382 | return NT_STATUS_OK;
|
---|
383 | case SMB2_OP_CREATE:
|
---|
384 | if (!req->session) goto nosession;
|
---|
385 | if (!req->tcon) goto notcon;
|
---|
386 | smb2srv_create_recv(req);
|
---|
387 | return NT_STATUS_OK;
|
---|
388 | case SMB2_OP_CLOSE:
|
---|
389 | if (!req->session) goto nosession;
|
---|
390 | if (!req->tcon) goto notcon;
|
---|
391 | smb2srv_close_recv(req);
|
---|
392 | return NT_STATUS_OK;
|
---|
393 | case SMB2_OP_FLUSH:
|
---|
394 | if (!req->session) goto nosession;
|
---|
395 | if (!req->tcon) goto notcon;
|
---|
396 | smb2srv_flush_recv(req);
|
---|
397 | return NT_STATUS_OK;
|
---|
398 | case SMB2_OP_READ:
|
---|
399 | if (!req->session) goto nosession;
|
---|
400 | if (!req->tcon) goto notcon;
|
---|
401 | smb2srv_read_recv(req);
|
---|
402 | return NT_STATUS_OK;
|
---|
403 | case SMB2_OP_WRITE:
|
---|
404 | if (!req->session) goto nosession;
|
---|
405 | if (!req->tcon) goto notcon;
|
---|
406 | smb2srv_write_recv(req);
|
---|
407 | return NT_STATUS_OK;
|
---|
408 | case SMB2_OP_LOCK:
|
---|
409 | if (!req->session) goto nosession;
|
---|
410 | if (!req->tcon) goto notcon;
|
---|
411 | smb2srv_lock_recv(req);
|
---|
412 | return NT_STATUS_OK;
|
---|
413 | case SMB2_OP_IOCTL:
|
---|
414 | if (!req->session) goto nosession;
|
---|
415 | if (!req->tcon) goto notcon;
|
---|
416 | smb2srv_ioctl_recv(req);
|
---|
417 | return NT_STATUS_OK;
|
---|
418 | case SMB2_OP_CANCEL:
|
---|
419 | smb2srv_cancel_recv(req);
|
---|
420 | return NT_STATUS_OK;
|
---|
421 | case SMB2_OP_KEEPALIVE:
|
---|
422 | smb2srv_keepalive_recv(req);
|
---|
423 | return NT_STATUS_OK;
|
---|
424 | case SMB2_OP_FIND:
|
---|
425 | if (!req->session) goto nosession;
|
---|
426 | if (!req->tcon) goto notcon;
|
---|
427 | smb2srv_find_recv(req);
|
---|
428 | return NT_STATUS_OK;
|
---|
429 | case SMB2_OP_NOTIFY:
|
---|
430 | if (!req->session) goto nosession;
|
---|
431 | if (!req->tcon) goto notcon;
|
---|
432 | smb2srv_notify_recv(req);
|
---|
433 | return NT_STATUS_OK;
|
---|
434 | case SMB2_OP_GETINFO:
|
---|
435 | if (!req->session) goto nosession;
|
---|
436 | if (!req->tcon) goto notcon;
|
---|
437 | smb2srv_getinfo_recv(req);
|
---|
438 | return NT_STATUS_OK;
|
---|
439 | case SMB2_OP_SETINFO:
|
---|
440 | if (!req->session) goto nosession;
|
---|
441 | if (!req->tcon) goto notcon;
|
---|
442 | smb2srv_setinfo_recv(req);
|
---|
443 | return NT_STATUS_OK;
|
---|
444 | case SMB2_OP_BREAK:
|
---|
445 | if (!req->session) goto nosession;
|
---|
446 | if (!req->tcon) goto notcon;
|
---|
447 | smb2srv_break_recv(req);
|
---|
448 | return NT_STATUS_OK;
|
---|
449 | }
|
---|
450 |
|
---|
451 | DEBUG(1,("Invalid SMB2 opcode: 0x%04X\n", opcode));
|
---|
452 | smbsrv_terminate_connection(req->smb_conn, "Invalid SMB2 opcode");
|
---|
453 | return NT_STATUS_OK;
|
---|
454 |
|
---|
455 | nosession:
|
---|
456 | smb2srv_send_error(req, NT_STATUS_USER_SESSION_DELETED);
|
---|
457 | return NT_STATUS_OK;
|
---|
458 | notcon:
|
---|
459 | smb2srv_send_error(req, NT_STATUS_NETWORK_NAME_DELETED);
|
---|
460 | return NT_STATUS_OK;
|
---|
461 | }
|
---|
462 |
|
---|
463 | NTSTATUS smbsrv_recv_smb2_request(void *private_data, DATA_BLOB blob)
|
---|
464 | {
|
---|
465 | struct smbsrv_connection *smb_conn = talloc_get_type(private_data, struct smbsrv_connection);
|
---|
466 | struct smb2srv_request *req;
|
---|
467 | struct timeval cur_time = timeval_current();
|
---|
468 | uint32_t protocol_version;
|
---|
469 | uint16_t buffer_code;
|
---|
470 | uint32_t dynamic_size;
|
---|
471 | uint32_t flags;
|
---|
472 |
|
---|
473 | smb_conn->statistics.last_request_time = cur_time;
|
---|
474 |
|
---|
475 | /* see if its a special NBT packet */
|
---|
476 | if (CVAL(blob.data,0) != 0) {
|
---|
477 | DEBUG(2,("Special NBT packet on SMB2 connection"));
|
---|
478 | smbsrv_terminate_connection(smb_conn, "Special NBT packet on SMB2 connection");
|
---|
479 | return NT_STATUS_OK;
|
---|
480 | }
|
---|
481 |
|
---|
482 | if (blob.length < (NBT_HDR_SIZE + SMB2_MIN_SIZE_NO_BODY)) {
|
---|
483 | DEBUG(2,("Invalid SMB2 packet length count %ld\n", (long)blob.length));
|
---|
484 | smbsrv_terminate_connection(smb_conn, "Invalid SMB2 packet");
|
---|
485 | return NT_STATUS_OK;
|
---|
486 | }
|
---|
487 |
|
---|
488 | protocol_version = IVAL(blob.data, NBT_HDR_SIZE);
|
---|
489 | if (protocol_version != SMB2_MAGIC) {
|
---|
490 | DEBUG(2,("Invalid SMB packet: protocol prefix: 0x%08X\n",
|
---|
491 | protocol_version));
|
---|
492 | smbsrv_terminate_connection(smb_conn, "NON-SMB2 packet");
|
---|
493 | return NT_STATUS_OK;
|
---|
494 | }
|
---|
495 |
|
---|
496 | req = smb2srv_init_request(smb_conn);
|
---|
497 | NT_STATUS_HAVE_NO_MEMORY(req);
|
---|
498 |
|
---|
499 | req->in.buffer = talloc_steal(req, blob.data);
|
---|
500 | req->in.size = blob.length;
|
---|
501 | req->request_time = cur_time;
|
---|
502 | req->in.allocated = req->in.size;
|
---|
503 |
|
---|
504 | req->in.hdr = req->in.buffer+ NBT_HDR_SIZE;
|
---|
505 | req->in.body = req->in.hdr + SMB2_HDR_BODY;
|
---|
506 | req->in.body_size = req->in.size - (SMB2_HDR_BODY+NBT_HDR_SIZE);
|
---|
507 | req->in.dynamic = NULL;
|
---|
508 |
|
---|
509 | req->seqnum = BVAL(req->in.hdr, SMB2_HDR_MESSAGE_ID);
|
---|
510 |
|
---|
511 | if (req->in.body_size < 2) {
|
---|
512 | /* error handling for this is different for negprot to
|
---|
513 | other packet types */
|
---|
514 | uint16_t opcode = SVAL(req->in.hdr, SMB2_HDR_OPCODE);
|
---|
515 | if (opcode == SMB2_OP_NEGPROT) {
|
---|
516 | smbsrv_terminate_connection(req->smb_conn, "Bad body size in SMB2 negprot");
|
---|
517 | return NT_STATUS_OK;
|
---|
518 | } else {
|
---|
519 | smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
|
---|
520 | return NT_STATUS_OK;
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 | buffer_code = SVAL(req->in.body, 0);
|
---|
525 | req->in.body_fixed = (buffer_code & ~1);
|
---|
526 | dynamic_size = req->in.body_size - req->in.body_fixed;
|
---|
527 |
|
---|
528 | if (dynamic_size != 0 && (buffer_code & 1)) {
|
---|
529 | req->in.dynamic = req->in.body + req->in.body_fixed;
|
---|
530 | if (smb2_oob(&req->in, req->in.dynamic, dynamic_size)) {
|
---|
531 | DEBUG(1,("SMB2 request invalid dynamic size 0x%x\n",
|
---|
532 | dynamic_size));
|
---|
533 | smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
|
---|
534 | return NT_STATUS_OK;
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | smb2srv_setup_bufinfo(req);
|
---|
539 |
|
---|
540 | /*
|
---|
541 | * TODO: - make sure the length field is 64
|
---|
542 | * - make sure it's a request
|
---|
543 | */
|
---|
544 |
|
---|
545 | flags = IVAL(req->in.hdr, SMB2_HDR_FLAGS);
|
---|
546 | /* the first request should never have the related flag set */
|
---|
547 | if (flags & SMB2_HDR_FLAG_CHAINED) {
|
---|
548 | req->chain_status = NT_STATUS_INVALID_PARAMETER;
|
---|
549 | }
|
---|
550 |
|
---|
551 | return smb2srv_reply(req);
|
---|
552 | }
|
---|
553 |
|
---|
554 | static NTSTATUS smb2srv_init_pending(struct smbsrv_connection *smb_conn)
|
---|
555 | {
|
---|
556 | smb_conn->requests2.idtree_req = idr_init(smb_conn);
|
---|
557 | NT_STATUS_HAVE_NO_MEMORY(smb_conn->requests2.idtree_req);
|
---|
558 | smb_conn->requests2.idtree_limit = 0x00FFFFFF & (UINT32_MAX - 1);
|
---|
559 | smb_conn->requests2.list = NULL;
|
---|
560 |
|
---|
561 | return NT_STATUS_OK;
|
---|
562 | }
|
---|
563 |
|
---|
564 | NTSTATUS smb2srv_queue_pending(struct smb2srv_request *req)
|
---|
565 | {
|
---|
566 | NTSTATUS status;
|
---|
567 | bool signing_used = false;
|
---|
568 | int id;
|
---|
569 |
|
---|
570 | if (req->pending_id) {
|
---|
571 | return NT_STATUS_INTERNAL_ERROR;
|
---|
572 | }
|
---|
573 |
|
---|
574 | id = idr_get_new_above(req->smb_conn->requests2.idtree_req, req,
|
---|
575 | 1, req->smb_conn->requests2.idtree_limit);
|
---|
576 | if (id == -1) {
|
---|
577 | return NT_STATUS_INSUFFICIENT_RESOURCES;
|
---|
578 | }
|
---|
579 |
|
---|
580 | DLIST_ADD_END(req->smb_conn->requests2.list, req, struct smb2srv_request *);
|
---|
581 | req->pending_id = id;
|
---|
582 |
|
---|
583 | if (req->smb_conn->connection->event.fde == NULL) {
|
---|
584 | /* the socket has been destroyed - no point trying to send an error! */
|
---|
585 | return NT_STATUS_REMOTE_DISCONNECT;
|
---|
586 | }
|
---|
587 |
|
---|
588 | talloc_set_destructor(req, smb2srv_request_deny_destructor);
|
---|
589 |
|
---|
590 | status = smb2srv_setup_reply(req, 8, true, 0);
|
---|
591 | if (!NT_STATUS_IS_OK(status)) {
|
---|
592 | return status;
|
---|
593 | }
|
---|
594 |
|
---|
595 | SIVAL(req->out.hdr, SMB2_HDR_STATUS, NT_STATUS_V(STATUS_PENDING));
|
---|
596 |
|
---|
597 | SSVAL(req->out.body, 0x02, 0);
|
---|
598 | SIVAL(req->out.body, 0x04, 0);
|
---|
599 |
|
---|
600 | /* if the real reply will be signed set the signed flags, but don't sign */
|
---|
601 | if (req->is_signed) {
|
---|
602 | SIVAL(req->out.hdr, SMB2_HDR_FLAGS, IVAL(req->out.hdr, SMB2_HDR_FLAGS) | SMB2_HDR_FLAG_SIGNED);
|
---|
603 | signing_used = req->is_signed;
|
---|
604 | req->is_signed = false;
|
---|
605 | }
|
---|
606 |
|
---|
607 | smb2srv_send_reply(req);
|
---|
608 |
|
---|
609 | req->is_signed = signing_used;
|
---|
610 |
|
---|
611 | talloc_set_destructor(req, smb2srv_request_destructor);
|
---|
612 | return NT_STATUS_OK;
|
---|
613 | }
|
---|
614 |
|
---|
615 | void smb2srv_cancel_recv(struct smb2srv_request *req)
|
---|
616 | {
|
---|
617 | uint32_t pending_id;
|
---|
618 | uint32_t flags;
|
---|
619 | void *p;
|
---|
620 | struct smb2srv_request *r;
|
---|
621 |
|
---|
622 | if (!req->session) goto done;
|
---|
623 |
|
---|
624 | flags = IVAL(req->in.hdr, SMB2_HDR_FLAGS);
|
---|
625 | pending_id = IVAL(req->in.hdr, SMB2_HDR_PID);
|
---|
626 |
|
---|
627 | if (!(flags & SMB2_HDR_FLAG_ASYNC)) {
|
---|
628 | /* TODO: what to do here? */
|
---|
629 | goto done;
|
---|
630 | }
|
---|
631 |
|
---|
632 | p = idr_find(req->smb_conn->requests2.idtree_req, pending_id);
|
---|
633 | if (!p) goto done;
|
---|
634 |
|
---|
635 | r = talloc_get_type(p, struct smb2srv_request);
|
---|
636 | if (!r) goto done;
|
---|
637 |
|
---|
638 | if (!r->ntvfs) goto done;
|
---|
639 |
|
---|
640 | ntvfs_cancel(r->ntvfs);
|
---|
641 |
|
---|
642 | done:
|
---|
643 | /* we never generate a reply for a SMB2 Cancel */
|
---|
644 | talloc_free(req);
|
---|
645 | }
|
---|
646 |
|
---|
647 | /*
|
---|
648 | * init the SMB2 protocol related stuff
|
---|
649 | */
|
---|
650 | NTSTATUS smbsrv_init_smb2_connection(struct smbsrv_connection *smb_conn)
|
---|
651 | {
|
---|
652 | NTSTATUS status;
|
---|
653 |
|
---|
654 | /* now initialise a few default values associated with this smb socket */
|
---|
655 | smb_conn->negotiate.max_send = 0xFFFF;
|
---|
656 |
|
---|
657 | /* this is the size that w2k uses, and it appears to be important for
|
---|
658 | good performance */
|
---|
659 | smb_conn->negotiate.max_recv = lp_max_xmit(smb_conn->lp_ctx);
|
---|
660 |
|
---|
661 | smb_conn->negotiate.zone_offset = get_time_zone(time(NULL));
|
---|
662 |
|
---|
663 | smb_conn->config.security = SEC_USER;
|
---|
664 | smb_conn->config.nt_status_support = true;
|
---|
665 |
|
---|
666 | status = smbsrv_init_sessions(smb_conn, UINT64_MAX);
|
---|
667 | NT_STATUS_NOT_OK_RETURN(status);
|
---|
668 |
|
---|
669 | status = smb2srv_init_pending(smb_conn);
|
---|
670 | NT_STATUS_NOT_OK_RETURN(status);
|
---|
671 |
|
---|
672 | return NT_STATUS_OK;
|
---|
673 |
|
---|
674 | }
|
---|