2 Unix SMB/CIFS implementation.
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/>.
22 #include "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
24 #include "../lib/tsocket/tsocket.h"
26 bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size)
28 if (size < (4 + SMB2_HDR_BODY)) {
32 if (IVAL(inbuf, 4) != SMB2_MAGIC) {
39 static NTSTATUS smbd_initialize_smb2(struct smbd_server_connection *conn)
44 TALLOC_FREE(conn->smb1.fde);
46 conn->smb2.event_ctx = smbd_event_context();
48 conn->smb2.recv_queue = tevent_queue_create(conn, "smb2 recv queue");
49 if (conn->smb2.recv_queue == NULL) {
50 return NT_STATUS_NO_MEMORY;
53 conn->smb2.send_queue = tevent_queue_create(conn, "smb2 send queue");
54 if (conn->smb2.send_queue == NULL) {
55 return NT_STATUS_NO_MEMORY;
58 conn->smb2.sessions.idtree = idr_init(conn);
59 if (conn->smb2.sessions.idtree == NULL) {
60 return NT_STATUS_NO_MEMORY;
62 conn->smb2.sessions.limit = 0x0000FFFE;
63 conn->smb2.sessions.list = NULL;
65 ret = tstream_bsd_existing_socket(conn, smbd_server_fd(),
68 status = map_nt_error_from_unix(errno);
72 /* Ensure child is set to non-blocking mode */
73 set_blocking(smbd_server_fd(),false);
77 #define smb2_len(buf) (PVAL(buf,3)|(PVAL(buf,2)<<8)|(PVAL(buf,1)<<16))
78 #define _smb2_setlen(_buf,len) do { \
79 uint8_t *buf = (uint8_t *)_buf; \
81 buf[1] = ((len)&0xFF0000)>>16; \
82 buf[2] = ((len)&0xFF00)>>8; \
83 buf[3] = (len)&0xFF; \
86 static void smb2_setup_nbt_length(struct iovec *vector, int count)
91 for (i=1; i < count; i++) {
92 len += vector[i].iov_len;
95 _smb2_setlen(vector[0].iov_base, len);
98 static NTSTATUS smbd_smb2_request_create(struct smbd_server_connection *conn,
99 const uint8_t *inbuf, size_t size,
100 struct smbd_smb2_request **_req)
102 TALLOC_CTX *mem_pool;
103 struct smbd_smb2_request *req;
104 uint32_t protocol_version;
105 const uint8_t *inhdr = NULL;
108 uint32_t next_command_ofs;
110 if (size < (4 + SMB2_HDR_BODY + 2)) {
111 DEBUG(0,("Invalid SMB2 packet length count %ld\n", (long)size));
112 return NT_STATUS_INVALID_PARAMETER;
117 protocol_version = IVAL(inhdr, SMB2_HDR_PROTOCOL_ID);
118 if (protocol_version != SMB2_MAGIC) {
119 DEBUG(0,("Invalid SMB packet: protocol prefix: 0x%08X\n",
121 return NT_STATUS_INVALID_PARAMETER;
124 cmd = SVAL(inhdr, SMB2_HDR_OPCODE);
125 if (cmd != SMB2_OP_NEGPROT) {
126 DEBUG(0,("Invalid SMB packet: first request: 0x%04X\n",
128 return NT_STATUS_INVALID_PARAMETER;
131 next_command_ofs = IVAL(inhdr, SMB2_HDR_NEXT_COMMAND);
132 if (next_command_ofs != 0) {
133 DEBUG(0,("Invalid SMB packet: next_command: 0x%08X\n",
135 return NT_STATUS_INVALID_PARAMETER;
138 mem_pool = talloc_pool(conn, 8192);
139 if (mem_pool == NULL) {
140 return NT_STATUS_NO_MEMORY;
143 req = talloc_zero(mem_pool, struct smbd_smb2_request);
145 talloc_free(mem_pool);
146 return NT_STATUS_NO_MEMORY;
148 req->mem_pool = mem_pool;
151 talloc_steal(req, inbuf);
153 req->in.vector = talloc_array(req, struct iovec, 4);
154 if (req->in.vector == NULL) {
155 talloc_free(mem_pool);
156 return NT_STATUS_NO_MEMORY;
158 req->in.vector_count = 4;
160 memcpy(req->in.nbt_hdr, inbuf, 4);
163 req->in.vector[0].iov_base = (void *)req->in.nbt_hdr;
164 req->in.vector[0].iov_len = 4;
165 ofs += req->in.vector[0].iov_len;
167 req->in.vector[1].iov_base = (void *)(inbuf + ofs);
168 req->in.vector[1].iov_len = SMB2_HDR_BODY;
169 ofs += req->in.vector[1].iov_len;
171 req->in.vector[2].iov_base = (void *)(inbuf + ofs);
172 req->in.vector[2].iov_len = SVAL(inbuf, ofs) & 0xFFFE;
173 ofs += req->in.vector[2].iov_len;
176 return NT_STATUS_INVALID_PARAMETER;
179 req->in.vector[3].iov_base = (void *)(inbuf + ofs);
180 req->in.vector[3].iov_len = size - ofs;
181 ofs += req->in.vector[3].iov_len;
183 req->current_idx = 1;
189 static NTSTATUS smbd_smb2_request_validate(struct smbd_smb2_request *req)
193 bool compound_related = false;
195 count = req->in.vector_count;
198 /* It's not a SMB2 request */
199 return NT_STATUS_INVALID_PARAMETER;
202 for (idx=1; idx < count; idx += 3) {
203 const uint8_t *inhdr = NULL;
206 if (req->in.vector[idx].iov_len != SMB2_HDR_BODY) {
207 return NT_STATUS_INVALID_PARAMETER;
210 if (req->in.vector[idx+1].iov_len < 2) {
211 return NT_STATUS_INVALID_PARAMETER;
214 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
216 /* setup the SMB2 header */
217 if (IVAL(inhdr, SMB2_HDR_PROTOCOL_ID) != SMB2_MAGIC) {
218 return NT_STATUS_INVALID_PARAMETER;
221 flags = IVAL(inhdr, SMB2_HDR_FLAGS);
224 * the 1st request should never have the
225 * SMB2_HDR_FLAG_CHAINED flag set
227 if (flags & SMB2_HDR_FLAG_CHAINED) {
228 req->next_status = NT_STATUS_INVALID_PARAMETER;
231 } else if (idx == 4) {
233 * the 2nd request triggers related vs. unrelated
234 * compounded requests
236 if (flags & SMB2_HDR_FLAG_CHAINED) {
237 compound_related = true;
239 } else if (idx > 4) {
241 * all other requests should match the 2nd one
243 if (flags & SMB2_HDR_FLAG_CHAINED) {
244 if (!compound_related) {
246 NT_STATUS_INVALID_PARAMETER;
250 if (compound_related) {
252 NT_STATUS_INVALID_PARAMETER;
262 static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req)
264 struct iovec *vector;
268 count = req->in.vector_count;
269 vector = talloc_array(req, struct iovec, count);
270 if (vector == NULL) {
271 return NT_STATUS_NO_MEMORY;
274 vector[0].iov_base = req->out.nbt_hdr;
275 vector[0].iov_len = 4;
276 SIVAL(req->out.nbt_hdr, 0, 0);
278 for (idx=1; idx < count; idx += 3) {
279 const uint8_t *inhdr = NULL;
280 uint8_t *outhdr = NULL;
281 uint8_t *outbody = NULL;
282 uint32_t next_command_ofs = 0;
283 struct iovec *current = &vector[idx];
285 if ((idx + 3) < count) {
286 /* we have a next command */
287 next_command_ofs = SMB2_HDR_BODY + 8;
290 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
292 outhdr = talloc_array(vector, uint8_t,
294 if (outhdr == NULL) {
295 return NT_STATUS_NO_MEMORY;
298 outbody = outhdr + SMB2_HDR_BODY;
300 current[0].iov_base = (void *)outhdr;
301 current[0].iov_len = SMB2_HDR_BODY;
303 current[1].iov_base = (void *)outbody;
304 current[1].iov_len = 8;
306 current[2].iov_base = NULL;
307 current[2].iov_len = 0;
309 /* setup the SMB2 header */
310 SIVAL(outhdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
311 SSVAL(outhdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
312 SSVAL(outhdr, SMB2_HDR_EPOCH, 0);
313 SIVAL(outhdr, SMB2_HDR_STATUS,
314 NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));
315 SSVAL(outhdr, SMB2_HDR_OPCODE,
316 SVAL(inhdr, SMB2_HDR_OPCODE));
317 /* Make up a number for now... JRA. FIXME ! FIXME !*/
318 SSVAL(outhdr, SMB2_HDR_CREDIT, 20);
319 SIVAL(outhdr, SMB2_HDR_FLAGS, SMB2_HDR_FLAG_REDIRECT);
320 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
321 SBVAL(outhdr, SMB2_HDR_MESSAGE_ID,
322 BVAL(inhdr, SMB2_HDR_MESSAGE_ID));
323 SIVAL(outhdr, SMB2_HDR_PID,
324 IVAL(inhdr, SMB2_HDR_PID));
325 SIVAL(outhdr, SMB2_HDR_TID,
326 IVAL(inhdr, SMB2_HDR_TID));
327 SBVAL(outhdr, SMB2_HDR_SESSION_ID,
328 BVAL(inhdr, SMB2_HDR_SESSION_ID));
329 memset(outhdr + SMB2_HDR_SIGNATURE, 0, 16);
331 /* setup error body header */
332 SSVAL(outbody, 0x00, 0x08 + 1);
333 SSVAL(outbody, 0x02, 0);
334 SIVAL(outbody, 0x04, 0);
337 req->out.vector = vector;
338 req->out.vector_count = count;
340 /* setup the length of the NBT packet */
341 smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
346 void smbd_server_connection_terminate_ex(struct smbd_server_connection *sconn,
348 const char *location)
350 DEBUG(10,("smbd_server_connection_terminate_ex: reason[%s] at %s\n",
352 exit_server_cleanly(reason);
355 static NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
357 const uint8_t *inhdr;
358 int i = req->current_idx;
362 NTSTATUS session_status;
364 inhdr = (const uint8_t *)req->in.vector[i].iov_base;
366 /* TODO: verify more things */
368 flags = IVAL(inhdr, SMB2_HDR_FLAGS);
369 opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
370 DEBUG(10,("smbd_smb2_request_dispatch: opcode[%u]\n", opcode));
372 #define TMP_SMB2_ALLOWED_FLAGS ( \
373 SMB2_HDR_FLAG_CHAINED | \
374 SMB2_HDR_FLAG_SIGNED | \
376 if ((flags & ~TMP_SMB2_ALLOWED_FLAGS) != 0) {
377 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
379 #undef TMP_SMB2_ALLOWED_FLAGS
381 session_status = smbd_smb2_request_check_session(req);
383 req->do_signing = false;
384 if (flags & SMB2_HDR_FLAG_SIGNED) {
385 if (!NT_STATUS_IS_OK(session_status)) {
386 return smbd_smb2_request_error(req, session_status);
389 req->do_signing = true;
390 status = smb2_signing_check_pdu(req->session->session_key,
391 &req->in.vector[i], 3);
392 if (!NT_STATUS_IS_OK(status)) {
393 return smbd_smb2_request_error(req, status);
395 } else if (req->session && req->session->do_signing) {
396 return smbd_smb2_request_error(req, NT_STATUS_ACCESS_DENIED);
400 * This check is mostly for giving the correct error code
401 * for compounded requests.
403 * TODO: we may need to move this after the session and tcon checks.
405 if (!NT_STATUS_IS_OK(req->next_status)) {
406 return smbd_smb2_request_error(req, req->next_status);
410 case SMB2_OP_NEGPROT:
411 return smbd_smb2_request_process_negprot(req);
413 case SMB2_OP_SESSSETUP:
414 return smbd_smb2_request_process_sesssetup(req);
417 if (!NT_STATUS_IS_OK(session_status)) {
418 return smbd_smb2_request_error(req, session_status);
420 return smbd_smb2_request_process_logoff(req);
423 if (!NT_STATUS_IS_OK(session_status)) {
424 return smbd_smb2_request_error(req, session_status);
426 status = smbd_smb2_request_check_session(req);
427 if (!NT_STATUS_IS_OK(status)) {
428 return smbd_smb2_request_error(req, status);
430 return smbd_smb2_request_process_tcon(req);
433 if (!NT_STATUS_IS_OK(session_status)) {
434 return smbd_smb2_request_error(req, session_status);
436 status = smbd_smb2_request_check_tcon(req);
437 if (!NT_STATUS_IS_OK(status)) {
438 return smbd_smb2_request_error(req, status);
440 return smbd_smb2_request_process_tdis(req);
443 if (!NT_STATUS_IS_OK(session_status)) {
444 return smbd_smb2_request_error(req, session_status);
446 status = smbd_smb2_request_check_tcon(req);
447 if (!NT_STATUS_IS_OK(status)) {
448 return smbd_smb2_request_error(req, status);
450 return smbd_smb2_request_process_create(req);
453 if (!NT_STATUS_IS_OK(session_status)) {
454 return smbd_smb2_request_error(req, session_status);
456 status = smbd_smb2_request_check_tcon(req);
457 if (!NT_STATUS_IS_OK(status)) {
458 return smbd_smb2_request_error(req, status);
460 return smbd_smb2_request_process_close(req);
463 if (!NT_STATUS_IS_OK(session_status)) {
464 return smbd_smb2_request_error(req, session_status);
466 status = smbd_smb2_request_check_tcon(req);
467 if (!NT_STATUS_IS_OK(status)) {
468 return smbd_smb2_request_error(req, status);
470 return smbd_smb2_request_process_flush(req);
473 if (!NT_STATUS_IS_OK(session_status)) {
474 return smbd_smb2_request_error(req, session_status);
476 status = smbd_smb2_request_check_tcon(req);
477 if (!NT_STATUS_IS_OK(status)) {
478 return smbd_smb2_request_error(req, status);
480 return smbd_smb2_request_process_read(req);
483 if (!NT_STATUS_IS_OK(session_status)) {
484 return smbd_smb2_request_error(req, session_status);
486 status = smbd_smb2_request_check_tcon(req);
487 if (!NT_STATUS_IS_OK(status)) {
488 return smbd_smb2_request_error(req, status);
490 return smbd_smb2_request_process_write(req);
493 if (!NT_STATUS_IS_OK(session_status)) {
494 return smbd_smb2_request_error(req, session_status);
496 status = smbd_smb2_request_check_tcon(req);
497 if (!NT_STATUS_IS_OK(status)) {
498 return smbd_smb2_request_error(req, status);
500 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
503 if (!NT_STATUS_IS_OK(session_status)) {
504 return smbd_smb2_request_error(req, session_status);
506 status = smbd_smb2_request_check_tcon(req);
507 if (!NT_STATUS_IS_OK(status)) {
508 return smbd_smb2_request_error(req, status);
510 return smbd_smb2_request_process_ioctl(req);
513 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
515 case SMB2_OP_KEEPALIVE:
516 return smbd_smb2_request_process_keepalive(req);
519 if (!NT_STATUS_IS_OK(session_status)) {
520 return smbd_smb2_request_error(req, session_status);
522 status = smbd_smb2_request_check_tcon(req);
523 if (!NT_STATUS_IS_OK(status)) {
524 return smbd_smb2_request_error(req, status);
526 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
529 if (!NT_STATUS_IS_OK(session_status)) {
530 return smbd_smb2_request_error(req, session_status);
532 status = smbd_smb2_request_check_tcon(req);
533 if (!NT_STATUS_IS_OK(status)) {
534 return smbd_smb2_request_error(req, status);
536 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
538 case SMB2_OP_GETINFO:
539 if (!NT_STATUS_IS_OK(session_status)) {
540 return smbd_smb2_request_error(req, session_status);
542 status = smbd_smb2_request_check_tcon(req);
543 if (!NT_STATUS_IS_OK(status)) {
544 return smbd_smb2_request_error(req, status);
546 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
548 case SMB2_OP_SETINFO:
549 if (!NT_STATUS_IS_OK(session_status)) {
550 return smbd_smb2_request_error(req, session_status);
552 status = smbd_smb2_request_check_tcon(req);
553 if (!NT_STATUS_IS_OK(status)) {
554 return smbd_smb2_request_error(req, status);
556 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
559 if (!NT_STATUS_IS_OK(session_status)) {
560 return smbd_smb2_request_error(req, session_status);
562 status = smbd_smb2_request_check_tcon(req);
563 if (!NT_STATUS_IS_OK(status)) {
564 return smbd_smb2_request_error(req, status);
566 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
569 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
572 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq);
573 static void smbd_smb2_request_writev_done(struct tevent_req *subreq);
575 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
577 struct tevent_req *subreq;
579 smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
581 if (req->do_signing) {
582 int i = req->current_idx;
584 status = smb2_signing_sign_pdu(req->session->session_key,
585 &req->out.vector[i], 3);
586 if (!NT_STATUS_IS_OK(status)) {
591 req->current_idx += 3;
593 if (req->current_idx < req->out.vector_count) {
594 struct timeval zero = timeval_zero();
595 subreq = tevent_wakeup_send(req,
596 req->conn->smb2.event_ctx,
598 if (subreq == NULL) {
599 return NT_STATUS_NO_MEMORY;
601 tevent_req_set_callback(subreq,
602 smbd_smb2_request_dispatch_compound,
608 subreq = tstream_writev_queue_send(req,
609 req->conn->smb2.event_ctx,
610 req->conn->smb2.stream,
611 req->conn->smb2.send_queue,
613 req->out.vector_count);
614 if (subreq == NULL) {
615 return NT_STATUS_NO_MEMORY;
617 tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
622 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq)
624 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
625 struct smbd_smb2_request);
626 struct smbd_server_connection *conn = req->conn;
629 tevent_wakeup_recv(subreq);
632 DEBUG(10,("smbd_smb2_request_dispatch_compound: idx[%d] of %d vectors\n",
633 req->current_idx, req->in.vector_count));
635 status = smbd_smb2_request_dispatch(req);
636 if (!NT_STATUS_IS_OK(status)) {
637 smbd_server_connection_terminate(conn, nt_errstr(status));
642 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
644 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
645 struct smbd_smb2_request);
646 struct smbd_server_connection *conn = req->conn;
649 TALLOC_CTX *mem_pool;
651 ret = tstream_writev_queue_recv(subreq, &sys_errno);
654 NTSTATUS status = map_nt_error_from_unix(sys_errno);
655 smbd_server_connection_terminate(conn, nt_errstr(status));
659 mem_pool = req->mem_pool;
661 talloc_free(mem_pool);
664 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
667 const char *location)
671 int i = req->current_idx;
673 DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s] |%s| at %s\n",
674 i, nt_errstr(status), info ? " +info" : "",
677 outhdr = (uint8_t *)req->out.vector[i].iov_base;
679 SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
681 outbody = outhdr + SMB2_HDR_BODY;
683 req->out.vector[i+1].iov_base = (void *)outbody;
684 req->out.vector[i+1].iov_len = 8;
687 SIVAL(outbody, 0x04, info->length);
688 req->out.vector[i+2].iov_base = (void *)info->data;
689 req->out.vector[i+2].iov_len = info->length;
691 req->out.vector[i+2].iov_base = NULL;
692 req->out.vector[i+2].iov_len = 0;
696 * if a request fails, all other remaining
697 * compounded requests should fail too
699 req->next_status = NT_STATUS_INVALID_PARAMETER;
701 return smbd_smb2_request_reply(req);
704 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
706 DATA_BLOB body, DATA_BLOB *dyn,
707 const char *location)
711 int i = req->current_idx;
712 uint32_t next_command_ofs;
714 DEBUG(10,("smbd_smb2_request_done_ex: "
715 "idx[%d] status[%s] body[%u] dyn[%s:%u] at %s\n",
716 i, nt_errstr(status), (unsigned int)body.length,
718 (unsigned int)(dyn ? dyn->length : 0),
721 if (body.length < 2) {
722 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
725 if ((body.length % 2) != 0) {
726 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
729 outhdr = (uint8_t *)req->out.vector[i].iov_base;
730 /* the fallback dynamic buffer */
731 outdyn = outhdr + SMB2_HDR_BODY + 8;
733 next_command_ofs = IVAL(outhdr, SMB2_HDR_NEXT_COMMAND);
734 SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
736 req->out.vector[i+1].iov_base = (void *)body.data;
737 req->out.vector[i+1].iov_len = body.length;
740 req->out.vector[i+2].iov_base = (void *)dyn->data;
741 req->out.vector[i+2].iov_len = dyn->length;
743 req->out.vector[i+2].iov_base = NULL;
744 req->out.vector[i+2].iov_len = 0;
747 /* see if we need to recalculate the offset to the next response */
748 if (next_command_ofs > 0) {
749 next_command_ofs = SMB2_HDR_BODY;
750 next_command_ofs += req->out.vector[i+1].iov_len;
751 next_command_ofs += req->out.vector[i+2].iov_len;
754 if ((next_command_ofs % 8) != 0) {
755 size_t pad_size = 8 - (next_command_ofs % 8);
756 if (req->out.vector[i+2].iov_len == 0) {
758 * if the dyn buffer is empty
759 * we can use it to add padding
763 pad = talloc_zero_array(req->out.vector,
766 return smbd_smb2_request_error(req,
767 NT_STATUS_NO_MEMORY);
770 req->out.vector[i+2].iov_base = (void *)pad;
771 req->out.vector[i+2].iov_len = pad_size;
774 * For now we copy the dynamic buffer
775 * and add the padding to the new buffer
782 old_size = req->out.vector[i+2].iov_len;
783 old_dyn = (uint8_t *)req->out.vector[i+2].iov_base;
785 new_size = old_size + pad_size;
786 new_dyn = talloc_array(req->out.vector,
788 if (new_dyn == NULL) {
789 return smbd_smb2_request_error(req,
790 NT_STATUS_NO_MEMORY);
793 memcpy(new_dyn, old_dyn, old_size);
794 memset(new_dyn + old_size, 0, pad_size);
796 req->out.vector[i+2].iov_base = (void *)new_dyn;
797 req->out.vector[i+2].iov_len = new_size;
799 TALLOC_FREE(old_dyn);
801 next_command_ofs += pad_size;
804 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
806 return smbd_smb2_request_reply(req);
809 struct smbd_smb2_request_read_state {
811 bool asked_for_header;
812 struct smbd_smb2_request *smb2_req;
815 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
818 struct iovec **_vector,
820 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
822 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
823 struct tevent_context *ev,
824 struct smbd_server_connection *conn)
826 struct tevent_req *req;
827 struct smbd_smb2_request_read_state *state;
828 struct tevent_req *subreq;
829 TALLOC_CTX *mem_pool;
831 req = tevent_req_create(mem_ctx, &state,
832 struct smbd_smb2_request_read_state);
837 state->asked_for_header = false;
839 mem_pool = talloc_pool(state, 8192);
840 if (tevent_req_nomem(mem_pool, req)) {
841 return tevent_req_post(req, ev);
844 state->smb2_req = talloc_zero(mem_pool, struct smbd_smb2_request);
845 if (tevent_req_nomem(state->smb2_req, req)) {
846 return tevent_req_post(req, ev);
849 state->smb2_req->mem_pool = mem_pool;
850 state->smb2_req->conn = conn;
852 subreq = tstream_readv_pdu_queue_send(state, ev, conn->smb2.stream,
853 conn->smb2.recv_queue,
854 smbd_smb2_request_next_vector,
856 if (tevent_req_nomem(subreq, req)) {
857 return tevent_req_post(req, ev);
859 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
864 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
867 struct iovec **_vector,
870 struct smbd_smb2_request_read_state *state =
871 talloc_get_type_abort(private_data,
872 struct smbd_smb2_request_read_state);
873 struct smbd_smb2_request *req = state->smb2_req;
874 struct iovec *vector;
875 int idx = req->in.vector_count;
879 if (req->in.vector_count == 0) {
881 * first we need to get the NBT header
883 req->in.vector = talloc_array(req, struct iovec,
884 req->in.vector_count + 1);
885 if (req->in.vector == NULL) {
888 req->in.vector_count += 1;
890 req->in.vector[idx].iov_base = (void *)req->in.nbt_hdr;
891 req->in.vector[idx].iov_len = 4;
893 vector = talloc_array(mem_ctx, struct iovec, 1);
894 if (vector == NULL) {
898 vector[0] = req->in.vector[idx];
905 if (req->in.vector_count == 1) {
907 * Now we analyze the NBT header
909 state->missing = smb2_len(req->in.vector[0].iov_base);
911 if (state->missing == 0) {
912 /* if there're no remaining bytes, we're done */
918 req->in.vector = talloc_realloc(req, req->in.vector,
920 req->in.vector_count + 1);
921 if (req->in.vector == NULL) {
924 req->in.vector_count += 1;
926 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
928 * it's a special NBT message,
929 * so get all remaining bytes
931 len = state->missing;
932 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
934 * it's an invalid message, just read what we can get
935 * and let the caller handle the error
937 len = state->missing;
940 * We assume it's a SMB2 request,
941 * and we first get the header and the
942 * first 2 bytes (the struct size) of the body
944 len = SMB2_HDR_BODY + 2;
946 state->asked_for_header = true;
949 state->missing -= len;
951 buf = talloc_array(req->in.vector, uint8_t, len);
956 req->in.vector[idx].iov_base = (void *)buf;
957 req->in.vector[idx].iov_len = len;
959 vector = talloc_array(mem_ctx, struct iovec, 1);
960 if (vector == NULL) {
964 vector[0] = req->in.vector[idx];
971 if (state->missing == 0) {
972 /* if there're no remaining bytes, we're done */
978 if (state->asked_for_header) {
981 size_t next_command_ofs;
986 bool invalid = false;
988 state->asked_for_header = false;
991 * We got the SMB2 header and the first 2 bytes
992 * of the body. We fix the size to just the header
993 * and manually copy the 2 first bytes to the body section
995 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
996 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
998 /* allocate vectors for body and dynamic areas */
999 req->in.vector = talloc_realloc(req, req->in.vector,
1001 req->in.vector_count + 2);
1002 if (req->in.vector == NULL) {
1005 req->in.vector_count += 2;
1007 full_size = state->missing + SMB2_HDR_BODY + 2;
1008 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
1009 body_size = SVAL(hdr, SMB2_HDR_BODY);
1011 if (next_command_ofs != 0) {
1012 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
1014 * this is invalid, just return a zero
1015 * body and let the caller deal with the error
1018 } else if (next_command_ofs > full_size) {
1020 * this is invalid, just return a zero
1021 * body and let the caller deal with the error
1025 full_size = next_command_ofs;
1030 if (body_size < 2) {
1032 * this is invalid, just return a zero
1033 * body and let the caller deal with the error
1036 } else if (body_size > (full_size - SMB2_HDR_BODY)) {
1038 * this is invalid, just return a zero
1039 * body and let the caller deal with the error
1046 /* the caller should check this */
1050 if ((body_size % 2) != 0) {
1054 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
1056 state->missing -= (body_size - 2) + dyn_size;
1058 body = talloc_array(req->in.vector, uint8_t, body_size);
1063 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
1068 req->in.vector[idx].iov_base = (void *)body;
1069 req->in.vector[idx].iov_len = body_size;
1070 req->in.vector[idx+1].iov_base = (void *)dyn;
1071 req->in.vector[idx+1].iov_len = dyn_size;
1073 vector = talloc_array(mem_ctx, struct iovec, 2);
1074 if (vector == NULL) {
1079 * the first 2 bytes of the body were already fetched
1080 * together with the header
1082 memcpy(body, hdr + SMB2_HDR_BODY, 2);
1083 vector[0].iov_base = body + 2;
1084 vector[0].iov_len = req->in.vector[idx].iov_len - 2;
1086 vector[1] = req->in.vector[idx+1];
1094 * when we endup here, we're looking for a new SMB2 request
1095 * next. And we ask for its header and the first 2 bytes of
1096 * the body (like we did for the first SMB2 request).
1099 req->in.vector = talloc_realloc(req, req->in.vector,
1101 req->in.vector_count + 1);
1102 if (req->in.vector == NULL) {
1105 req->in.vector_count += 1;
1108 * We assume it's a SMB2 request,
1109 * and we first get the header and the
1110 * first 2 bytes (the struct size) of the body
1112 len = SMB2_HDR_BODY + 2;
1114 if (len > state->missing) {
1115 /* let the caller handle the error */
1116 len = state->missing;
1119 state->missing -= len;
1120 state->asked_for_header = true;
1122 buf = talloc_array(req->in.vector, uint8_t, len);
1127 req->in.vector[idx].iov_base = (void *)buf;
1128 req->in.vector[idx].iov_len = len;
1130 vector = talloc_array(mem_ctx, struct iovec, 1);
1131 if (vector == NULL) {
1135 vector[0] = req->in.vector[idx];
1142 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
1144 struct tevent_req *req =
1145 tevent_req_callback_data(subreq,
1151 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1153 status = map_nt_error_from_unix(sys_errno);
1154 tevent_req_nterror(req, status);
1158 tevent_req_done(req);
1161 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
1162 TALLOC_CTX *mem_ctx,
1163 struct smbd_smb2_request **_smb2_req)
1165 struct smbd_smb2_request_read_state *state =
1166 tevent_req_data(req,
1167 struct smbd_smb2_request_read_state);
1170 if (tevent_req_is_nterror(req, &status)) {
1171 tevent_req_received(req);
1175 talloc_steal(mem_ctx, state->smb2_req->mem_pool);
1176 *_smb2_req = state->smb2_req;
1177 tevent_req_received(req);
1178 return NT_STATUS_OK;
1181 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
1183 void smbd_smb2_first_negprot(struct smbd_server_connection *conn,
1184 const uint8_t *inbuf, size_t size)
1187 struct smbd_smb2_request *req;
1188 struct tevent_req *subreq;
1190 DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
1191 (unsigned int)size));
1193 status = smbd_initialize_smb2(conn);
1194 if (!NT_STATUS_IS_OK(status)) {
1195 smbd_server_connection_terminate(conn, nt_errstr(status));
1199 status = smbd_smb2_request_create(conn, inbuf, size, &req);
1200 if (!NT_STATUS_IS_OK(status)) {
1201 smbd_server_connection_terminate(conn, nt_errstr(status));
1205 status = smbd_smb2_request_setup_out(req);
1206 if (!NT_STATUS_IS_OK(status)) {
1207 smbd_server_connection_terminate(conn, nt_errstr(status));
1211 status = smbd_smb2_request_dispatch(req);
1212 if (!NT_STATUS_IS_OK(status)) {
1213 smbd_server_connection_terminate(conn, nt_errstr(status));
1217 /* ask for the next request */
1218 subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
1219 if (subreq == NULL) {
1220 smbd_server_connection_terminate(conn, "no memory for reading");
1223 tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
1226 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
1228 struct smbd_server_connection *conn = tevent_req_callback_data(subreq,
1229 struct smbd_server_connection);
1231 struct smbd_smb2_request *req;
1233 status = smbd_smb2_request_read_recv(subreq, conn, &req);
1234 TALLOC_FREE(subreq);
1235 if (!NT_STATUS_IS_OK(status)) {
1236 smbd_server_connection_terminate(conn, nt_errstr(status));
1240 if (req->in.nbt_hdr[0] != 0x00) {
1241 DEBUG(1,("smbd_smb2_request_incoming: ignore NBT[0x%02X] msg\n",
1242 req->in.nbt_hdr[0]));
1243 talloc_free(req->mem_pool);
1248 req->current_idx = 1;
1250 DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
1251 req->current_idx, req->in.vector_count));
1253 status = smbd_smb2_request_validate(req);
1254 if (!NT_STATUS_IS_OK(status)) {
1255 smbd_server_connection_terminate(conn, nt_errstr(status));
1259 status = smbd_smb2_request_setup_out(req);
1260 if (!NT_STATUS_IS_OK(status)) {
1261 smbd_server_connection_terminate(conn, nt_errstr(status));
1265 status = smbd_smb2_request_dispatch(req);
1266 if (!NT_STATUS_IS_OK(status)) {
1267 smbd_server_connection_terminate(conn, nt_errstr(status));
1272 /* ask for the next request (this constructs the main loop) */
1273 subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
1274 if (subreq == NULL) {
1275 smbd_server_connection_terminate(conn, "no memory for reading");
1278 tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);