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 "../libcli/smb/smb_common.h"
24 #include "../lib/tsocket/tsocket.h"
26 #define OUTVEC_ALLOC_SIZE (SMB2_HDR_BODY + 9)
28 static const char *smb2_names[] = {
50 const char *smb2_opcode_name(uint16_t opcode)
53 return "Bad SMB2 opcode";
55 return smb2_names[opcode];
58 static void print_req_vectors(struct smbd_smb2_request *req)
62 for (i = 0; i < req->in.vector_count; i++) {
63 dbgtext("\treq->in.vector[%u].iov_len = %u\n",
65 (unsigned int)req->in.vector[i].iov_len);
67 for (i = 0; i < req->out.vector_count; i++) {
68 dbgtext("\treq->out.vector[%u].iov_len = %u\n",
70 (unsigned int)req->out.vector[i].iov_len);
74 bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size)
76 if (size < (4 + SMB2_HDR_BODY)) {
80 if (IVAL(inbuf, 4) != SMB2_MAGIC) {
87 static NTSTATUS smbd_initialize_smb2(struct smbd_server_connection *sconn)
92 TALLOC_FREE(sconn->smb1.fde);
94 sconn->smb2.event_ctx = smbd_event_context();
96 sconn->smb2.recv_queue = tevent_queue_create(sconn, "smb2 recv queue");
97 if (sconn->smb2.recv_queue == NULL) {
98 return NT_STATUS_NO_MEMORY;
101 sconn->smb2.send_queue = tevent_queue_create(sconn, "smb2 send queue");
102 if (sconn->smb2.send_queue == NULL) {
103 return NT_STATUS_NO_MEMORY;
106 sconn->smb2.sessions.idtree = idr_init(sconn);
107 if (sconn->smb2.sessions.idtree == NULL) {
108 return NT_STATUS_NO_MEMORY;
110 sconn->smb2.sessions.limit = 0x0000FFFE;
111 sconn->smb2.sessions.list = NULL;
113 ret = tstream_bsd_existing_socket(sconn, smbd_server_fd(),
114 &sconn->smb2.stream);
116 status = map_nt_error_from_unix(errno);
120 /* Ensure child is set to non-blocking mode */
121 set_blocking(smbd_server_fd(),false);
125 #define smb2_len(buf) (PVAL(buf,3)|(PVAL(buf,2)<<8)|(PVAL(buf,1)<<16))
126 #define _smb2_setlen(_buf,len) do { \
127 uint8_t *buf = (uint8_t *)_buf; \
129 buf[1] = ((len)&0xFF0000)>>16; \
130 buf[2] = ((len)&0xFF00)>>8; \
131 buf[3] = (len)&0xFF; \
134 static void smb2_setup_nbt_length(struct iovec *vector, int count)
139 for (i=1; i < count; i++) {
140 len += vector[i].iov_len;
143 _smb2_setlen(vector[0].iov_base, len);
146 static int smbd_smb2_request_parent_destructor(struct smbd_smb2_request **req)
149 (*req)->parent = NULL;
150 (*req)->mem_pool = NULL;
156 static int smbd_smb2_request_destructor(struct smbd_smb2_request *req)
158 if (req->out.vector) {
159 DLIST_REMOVE(req->sconn->smb2.requests, req);
164 talloc_free(req->mem_pool);
170 static struct smbd_smb2_request *smbd_smb2_request_allocate(TALLOC_CTX *mem_ctx)
172 TALLOC_CTX *mem_pool;
173 struct smbd_smb2_request **parent;
174 struct smbd_smb2_request *req;
176 mem_pool = talloc_pool(mem_ctx, 8192);
177 if (mem_pool == NULL) {
181 parent = talloc(mem_pool, struct smbd_smb2_request *);
182 if (parent == NULL) {
183 talloc_free(mem_pool);
187 req = talloc_zero(parent, struct smbd_smb2_request);
189 talloc_free(mem_pool);
193 req->mem_pool = mem_pool;
194 req->parent = parent;
196 talloc_set_destructor(parent, smbd_smb2_request_parent_destructor);
197 talloc_set_destructor(req, smbd_smb2_request_destructor);
202 static NTSTATUS smbd_smb2_request_create(struct smbd_server_connection *sconn,
203 const uint8_t *inbuf, size_t size,
204 struct smbd_smb2_request **_req)
206 struct smbd_smb2_request *req;
207 uint32_t protocol_version;
208 const uint8_t *inhdr = NULL;
211 uint32_t next_command_ofs;
213 if (size < (4 + SMB2_HDR_BODY + 2)) {
214 DEBUG(0,("Invalid SMB2 packet length count %ld\n", (long)size));
215 return NT_STATUS_INVALID_PARAMETER;
220 protocol_version = IVAL(inhdr, SMB2_HDR_PROTOCOL_ID);
221 if (protocol_version != SMB2_MAGIC) {
222 DEBUG(0,("Invalid SMB packet: protocol prefix: 0x%08X\n",
224 return NT_STATUS_INVALID_PARAMETER;
227 cmd = SVAL(inhdr, SMB2_HDR_OPCODE);
228 if (cmd != SMB2_OP_NEGPROT) {
229 DEBUG(0,("Invalid SMB packet: first request: 0x%04X\n",
231 return NT_STATUS_INVALID_PARAMETER;
234 next_command_ofs = IVAL(inhdr, SMB2_HDR_NEXT_COMMAND);
235 if (next_command_ofs != 0) {
236 DEBUG(0,("Invalid SMB packet: next_command: 0x%08X\n",
238 return NT_STATUS_INVALID_PARAMETER;
241 req = smbd_smb2_request_allocate(sconn);
243 return NT_STATUS_NO_MEMORY;
247 talloc_steal(req, inbuf);
249 req->in.vector = talloc_array(req, struct iovec, 4);
250 if (req->in.vector == NULL) {
252 return NT_STATUS_NO_MEMORY;
254 req->in.vector_count = 4;
256 memcpy(req->in.nbt_hdr, inbuf, 4);
259 req->in.vector[0].iov_base = (void *)req->in.nbt_hdr;
260 req->in.vector[0].iov_len = 4;
261 ofs += req->in.vector[0].iov_len;
263 req->in.vector[1].iov_base = (void *)(inbuf + ofs);
264 req->in.vector[1].iov_len = SMB2_HDR_BODY;
265 ofs += req->in.vector[1].iov_len;
267 req->in.vector[2].iov_base = (void *)(inbuf + ofs);
268 req->in.vector[2].iov_len = SVAL(inbuf, ofs) & 0xFFFE;
269 ofs += req->in.vector[2].iov_len;
272 return NT_STATUS_INVALID_PARAMETER;
275 req->in.vector[3].iov_base = (void *)(inbuf + ofs);
276 req->in.vector[3].iov_len = size - ofs;
277 ofs += req->in.vector[3].iov_len;
279 req->current_idx = 1;
285 static NTSTATUS smbd_smb2_request_validate(struct smbd_smb2_request *req,
286 uint16_t *p_creds_requested)
290 bool compound_related = false;
292 *p_creds_requested = 0;
293 count = req->in.vector_count;
296 /* It's not a SMB2 request */
297 return NT_STATUS_INVALID_PARAMETER;
300 for (idx=1; idx < count; idx += 3) {
301 uint16_t creds_requested = 0;
302 const uint8_t *inhdr = NULL;
305 if (req->in.vector[idx].iov_len != SMB2_HDR_BODY) {
306 return NT_STATUS_INVALID_PARAMETER;
309 if (req->in.vector[idx+1].iov_len < 2) {
310 return NT_STATUS_INVALID_PARAMETER;
313 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
315 /* setup the SMB2 header */
316 if (IVAL(inhdr, SMB2_HDR_PROTOCOL_ID) != SMB2_MAGIC) {
317 return NT_STATUS_INVALID_PARAMETER;
320 creds_requested = SVAL(inhdr, SMB2_HDR_CREDIT);
321 if (*p_creds_requested + creds_requested < creds_requested) {
322 *p_creds_requested = 65535;
324 *p_creds_requested += creds_requested;
327 flags = IVAL(inhdr, SMB2_HDR_FLAGS);
330 * the 1st request should never have the
331 * SMB2_HDR_FLAG_CHAINED flag set
333 if (flags & SMB2_HDR_FLAG_CHAINED) {
334 req->next_status = NT_STATUS_INVALID_PARAMETER;
337 } else if (idx == 4) {
339 * the 2nd request triggers related vs. unrelated
340 * compounded requests
342 if (flags & SMB2_HDR_FLAG_CHAINED) {
343 compound_related = true;
345 } else if (idx > 4) {
348 * It seems the this tests are wrong
349 * see the SMB2-COMPOUND test
353 * all other requests should match the 2nd one
355 if (flags & SMB2_HDR_FLAG_CHAINED) {
356 if (!compound_related) {
358 NT_STATUS_INVALID_PARAMETER;
362 if (compound_related) {
364 NT_STATUS_INVALID_PARAMETER;
375 static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req, uint16_t creds)
377 struct iovec *vector;
381 count = req->in.vector_count;
382 vector = talloc_zero_array(req, struct iovec, count);
383 if (vector == NULL) {
384 return NT_STATUS_NO_MEMORY;
387 vector[0].iov_base = req->out.nbt_hdr;
388 vector[0].iov_len = 4;
389 SIVAL(req->out.nbt_hdr, 0, 0);
391 for (idx=1; idx < count; idx += 3) {
392 const uint8_t *inhdr = NULL;
394 uint8_t *outhdr = NULL;
395 uint8_t *outbody = NULL;
396 uint32_t next_command_ofs = 0;
397 struct iovec *current = &vector[idx];
399 if ((idx + 3) < count) {
400 /* we have a next command -
401 * setup for the error case. */
402 next_command_ofs = SMB2_HDR_BODY + 9;
405 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
406 in_flags = IVAL(inhdr, SMB2_HDR_FLAGS);
408 outhdr = talloc_zero_array(vector, uint8_t,
410 if (outhdr == NULL) {
411 return NT_STATUS_NO_MEMORY;
414 outbody = outhdr + SMB2_HDR_BODY;
416 current[0].iov_base = (void *)outhdr;
417 current[0].iov_len = SMB2_HDR_BODY;
419 current[1].iov_base = (void *)outbody;
420 current[1].iov_len = 8;
422 current[2].iov_base = NULL;
423 current[2].iov_len = 0;
425 /* setup the SMB2 header */
426 SIVAL(outhdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
427 SSVAL(outhdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
428 SSVAL(outhdr, SMB2_HDR_EPOCH, 0);
429 SIVAL(outhdr, SMB2_HDR_STATUS,
430 NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));
431 SSVAL(outhdr, SMB2_HDR_OPCODE,
432 SVAL(inhdr, SMB2_HDR_OPCODE));
433 SSVAL(outhdr, SMB2_HDR_CREDIT, creds);
434 SIVAL(outhdr, SMB2_HDR_FLAGS,
435 IVAL(inhdr, SMB2_HDR_FLAGS) | SMB2_HDR_FLAG_REDIRECT);
436 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
437 SBVAL(outhdr, SMB2_HDR_MESSAGE_ID,
438 BVAL(inhdr, SMB2_HDR_MESSAGE_ID));
439 SIVAL(outhdr, SMB2_HDR_PID,
440 IVAL(inhdr, SMB2_HDR_PID));
441 SIVAL(outhdr, SMB2_HDR_TID,
442 IVAL(inhdr, SMB2_HDR_TID));
443 SBVAL(outhdr, SMB2_HDR_SESSION_ID,
444 BVAL(inhdr, SMB2_HDR_SESSION_ID));
445 memset(outhdr + SMB2_HDR_SIGNATURE, 0, 16);
447 /* setup error body header */
448 SSVAL(outbody, 0x00, 0x08 + 1);
449 SSVAL(outbody, 0x02, 0);
450 SIVAL(outbody, 0x04, 0);
453 req->out.vector = vector;
454 req->out.vector_count = count;
456 /* setup the length of the NBT packet */
457 smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
459 DLIST_ADD_END(req->sconn->smb2.requests, req, struct smbd_smb2_request *);
464 void smbd_server_connection_terminate_ex(struct smbd_server_connection *sconn,
466 const char *location)
468 DEBUG(10,("smbd_server_connection_terminate_ex: reason[%s] at %s\n",
470 exit_server_cleanly(reason);
473 static bool dup_smb2_vec(struct iovec *dstvec,
474 const struct iovec *srcvec,
478 if (srcvec[offset].iov_len &&
479 srcvec[offset].iov_base) {
480 dstvec[offset].iov_base = talloc_memdup(dstvec,
481 srcvec[offset].iov_base,
482 srcvec[offset].iov_len);
483 if (!dstvec[offset].iov_base) {
486 dstvec[offset].iov_len = srcvec[offset].iov_len;
488 dstvec[offset].iov_base = NULL;
489 dstvec[offset].iov_len = 0;
494 static struct smbd_smb2_request *dup_smb2_req(const struct smbd_smb2_request *req)
496 struct smbd_smb2_request *newreq = NULL;
497 struct iovec *outvec = NULL;
498 int count = req->out.vector_count;
501 newreq = smbd_smb2_request_allocate(req->sconn);
506 newreq->sconn = req->sconn;
507 newreq->do_signing = req->do_signing;
508 newreq->current_idx = req->current_idx;
509 newreq->async = false;
510 newreq->cancelled = false;
512 outvec = talloc_zero_array(newreq, struct iovec, count);
517 newreq->out.vector = outvec;
518 newreq->out.vector_count = count;
520 /* Setup the outvec's identically to req. */
521 outvec[0].iov_base = newreq->out.nbt_hdr;
522 outvec[0].iov_len = 4;
523 memcpy(newreq->out.nbt_hdr, req->out.nbt_hdr, 4);
525 for (i = 1; i < count; i += 3) {
526 /* i + 0 and i + 1 are always
528 outvec[i].iov_base = talloc_memdup(outvec,
529 req->out.vector[i].iov_base,
531 if (!outvec[i].iov_base) {
534 outvec[i].iov_len = SMB2_HDR_BODY;
536 outvec[i+1].iov_base = ((uint8_t *)outvec[i].iov_base) +
538 outvec[i+1].iov_len = 8;
540 if (req->out.vector[i+2].iov_base ==
541 ((uint8_t *)req->out.vector[i].iov_base) +
542 (OUTVEC_ALLOC_SIZE - 1) &&
543 req->out.vector[i+2].iov_len == 1) {
544 /* Common SMB2 error packet case. */
545 outvec[i+2].iov_base = ((uint8_t *)outvec[i].iov_base) +
546 (OUTVEC_ALLOC_SIZE - 1);
547 outvec[i+2].iov_len = 1;
548 } else if (!dup_smb2_vec(outvec,
561 smb2_setup_nbt_length(newreq->out.vector,
562 newreq->out.vector_count);
567 static void smbd_smb2_request_writev_done(struct tevent_req *subreq);
569 static NTSTATUS smb2_send_async_interim_response(const struct smbd_smb2_request *req)
572 uint8_t *outhdr = NULL;
573 struct smbd_smb2_request *nreq = NULL;
575 /* Create a new smb2 request we'll use
576 for the interim return. */
577 nreq = dup_smb2_req(req);
579 return NT_STATUS_NO_MEMORY;
582 /* Lose the last 3 out vectors. They're the
583 ones we'll be using for the async reply. */
584 nreq->out.vector_count -= 3;
586 smb2_setup_nbt_length(nreq->out.vector,
587 nreq->out.vector_count);
589 /* Step back to the previous reply. */
590 i = nreq->current_idx - 3;
591 outhdr = (uint8_t *)nreq->out.vector[i].iov_base;
592 /* And end the chain. */
593 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
595 /* Re-sign if needed. */
596 if (nreq->do_signing) {
598 status = smb2_signing_sign_pdu(nreq->session->session_key,
599 &nreq->out.vector[i], 3);
600 if (!NT_STATUS_IS_OK(status)) {
604 if (DEBUGLEVEL >= 10) {
605 dbgtext("smb2_send_async_interim_response: nreq->current_idx = %u\n",
606 (unsigned int)nreq->current_idx );
607 dbgtext("smb2_send_async_interim_response: returning %u vectors\n",
608 (unsigned int)nreq->out.vector_count );
609 print_req_vectors(nreq);
611 nreq->subreq = tstream_writev_queue_send(nreq,
612 nreq->sconn->smb2.event_ctx,
613 nreq->sconn->smb2.stream,
614 nreq->sconn->smb2.send_queue,
616 nreq->out.vector_count);
618 if (nreq->subreq == NULL) {
619 return NT_STATUS_NO_MEMORY;
622 tevent_req_set_callback(nreq->subreq,
623 smbd_smb2_request_writev_done,
629 struct smbd_smb2_request_pending_state {
630 struct smbd_server_connection *sconn;
631 uint8_t buf[4 + SMB2_HDR_BODY + 0x08 + 1];
632 struct iovec vector[3];
635 static void smbd_smb2_request_pending_writev_done(struct tevent_req *subreq)
637 struct smbd_smb2_request_pending_state *state =
638 tevent_req_callback_data(subreq,
639 struct smbd_smb2_request_pending_state);
640 struct smbd_server_connection *sconn = state->sconn;
644 ret = tstream_writev_queue_recv(subreq, &sys_errno);
647 NTSTATUS status = map_nt_error_from_unix(sys_errno);
648 smbd_server_connection_terminate(sconn, nt_errstr(status));
655 NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
656 struct tevent_req *subreq)
659 struct smbd_smb2_request_pending_state *state = NULL;
660 int i = req->current_idx;
661 uint8_t *reqhdr = NULL;
663 uint8_t *body = NULL;
665 uint64_t message_id = 0;
666 uint64_t async_id = 0;
667 struct iovec *outvec = NULL;
669 if (!tevent_req_is_in_progress(subreq)) {
674 /* We're already async. */
678 if (req->in.vector_count > i + 3) {
680 * We're trying to go async in a compound
681 * request chain. This is not allowed.
682 * Cancel the outstanding request.
684 tevent_req_cancel(subreq);
685 return smbd_smb2_request_error(req,
686 NT_STATUS_INSUFFICIENT_RESOURCES);
689 req->subreq = subreq;
692 if (DEBUGLEVEL >= 10) {
693 dbgtext("smbd_smb2_request_pending_queue: req->current_idx = %u\n",
694 (unsigned int)req->current_idx );
695 print_req_vectors(req);
698 if (req->out.vector_count > 4) {
699 /* This is a compound reply. We
700 * must do an interim response
701 * followed by the async response
704 status = smb2_send_async_interim_response(req);
705 if (!NT_STATUS_IS_OK(status)) {
710 reqhdr = (uint8_t *)req->out.vector[i].iov_base;
711 flags = IVAL(reqhdr, SMB2_HDR_FLAGS);
712 message_id = BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
713 async_id = message_id; /* keep it simple for now... */
716 * What we send is identical to a smbd_smb2_request_error
717 * packet with an error status of STATUS_PENDING. Make use
718 * of this fact sometime when refactoring. JRA.
721 state = talloc_zero(req->sconn, struct smbd_smb2_request_pending_state);
723 return NT_STATUS_NO_MEMORY;
725 state->sconn = req->sconn;
727 state->vector[0].iov_base = (void *)state->buf;
728 state->vector[0].iov_len = 4;
730 state->vector[1].iov_base = state->buf + 4;
731 state->vector[1].iov_len = SMB2_HDR_BODY;
733 state->vector[2].iov_base = state->buf + 4 + SMB2_HDR_BODY;
734 state->vector[2].iov_len = 9;
736 smb2_setup_nbt_length(state->vector, 3);
738 hdr = (uint8_t *)state->vector[1].iov_base;
739 body = (uint8_t *)state->vector[2].iov_base;
741 SIVAL(hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
742 SSVAL(hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
743 SSVAL(hdr, SMB2_HDR_EPOCH, 0);
744 SIVAL(hdr, SMB2_HDR_STATUS, NT_STATUS_V(STATUS_PENDING));
745 SSVAL(hdr, SMB2_HDR_OPCODE, SVAL(reqhdr, SMB2_HDR_OPCODE));
746 SSVAL(hdr, SMB2_HDR_CREDIT, 5);
747 SIVAL(hdr, SMB2_HDR_FLAGS, flags | SMB2_HDR_FLAG_ASYNC);
748 SIVAL(hdr, SMB2_HDR_NEXT_COMMAND, 0);
749 SBVAL(hdr, SMB2_HDR_MESSAGE_ID, message_id);
750 SBVAL(hdr, SMB2_HDR_PID, async_id);
751 SBVAL(hdr, SMB2_HDR_SESSION_ID,
752 BVAL(reqhdr, SMB2_HDR_SESSION_ID));
753 memset(hdr+SMB2_HDR_SIGNATURE, 0, 16);
755 SSVAL(body, 0x00, 0x08 + 1);
757 SCVAL(body, 0x02, 0);
758 SCVAL(body, 0x03, 0);
759 SIVAL(body, 0x04, 0);
760 /* Match W2K8R2... */
761 SCVAL(body, 0x08, 0x21);
763 if (req->do_signing) {
764 status = smb2_signing_sign_pdu(req->session->session_key,
766 if (!NT_STATUS_IS_OK(status)) {
771 subreq = tstream_writev_queue_send(state,
772 req->sconn->smb2.event_ctx,
773 req->sconn->smb2.stream,
774 req->sconn->smb2.send_queue,
778 if (subreq == NULL) {
779 return NT_STATUS_NO_MEMORY;
782 tevent_req_set_callback(subreq,
783 smbd_smb2_request_pending_writev_done,
786 /* Note we're going async with this request. */
790 * Now manipulate req so that the outstanding async request
791 * is the only one left in the struct smbd_smb2_request.
794 if (req->current_idx == 1) {
795 /* There was only one. */
799 /* Re-arrange the in.vectors. */
800 req->in.vector[1] = req->in.vector[i];
801 req->in.vector[2] = req->in.vector[i+1];
802 req->in.vector[3] = req->in.vector[i+2];
803 req->in.vector_count = 4;
804 /* Reset the new in size. */
805 smb2_setup_nbt_length(req->in.vector, 4);
807 /* Now recreate the out.vectors. */
808 outvec = talloc_zero_array(req, struct iovec, 4);
810 return NT_STATUS_NO_MEMORY;
812 outvec[0].iov_base = req->out.nbt_hdr;
813 outvec[0].iov_len = 4;
814 SIVAL(req->out.nbt_hdr, 0, 0);
816 outvec[1].iov_base = talloc_memdup(outvec,
817 req->out.vector[i].iov_base,
819 if (!outvec[1].iov_base) {
820 return NT_STATUS_NO_MEMORY;
822 outvec[1].iov_len = SMB2_HDR_BODY;
824 outvec[2].iov_base = ((uint8_t *)outvec[1].iov_base) +
826 outvec[2].iov_len = 8;
828 if (req->out.vector[i+2].iov_base &&
829 req->out.vector[i+2].iov_len) {
830 if (req->out.vector[i+2].iov_base ==
831 ((uint8_t *)req->out.vector[i].iov_base) +
832 (OUTVEC_ALLOC_SIZE - 1) &&
833 req->out.vector[i].iov_len == 1) {
834 /* Common SMB2 error packet case. */
835 outvec[3].iov_base = ((uint8_t *)outvec[1].iov_base) +
836 (OUTVEC_ALLOC_SIZE - 1);
838 outvec[3].iov_base = talloc_memdup(outvec,
839 req->out.vector[i+2].iov_base,
840 req->out.vector[i+2].iov_len);
841 if (!outvec[3].iov_base) {
842 return NT_STATUS_NO_MEMORY;
845 outvec[3].iov_len = req->out.vector[i+2].iov_len;
847 outvec[3].iov_base = NULL;
848 outvec[3].iov_len = 0;
851 TALLOC_FREE(req->out.vector);
853 req->out.vector = outvec;
855 req->current_idx = 1;
856 req->out.vector_count = 4;
860 smb2_setup_nbt_length(req->out.vector,
861 req->out.vector_count);
863 /* Ensure our final reply matches the interim one. */
864 reqhdr = (uint8_t *)req->out.vector[1].iov_base;
865 SIVAL(reqhdr, SMB2_HDR_FLAGS, flags | SMB2_HDR_FLAG_ASYNC);
866 SBVAL(reqhdr, SMB2_HDR_PID, async_id);
869 const uint8_t *inhdr =
870 (const uint8_t *)req->in.vector[1].iov_base;
871 DEBUG(10,("smbd_smb2_request_pending_queue: opcode[%s] mid %llu "
873 smb2_opcode_name((uint16_t)IVAL(inhdr, SMB2_HDR_OPCODE)),
874 (unsigned long long)async_id ));
879 static NTSTATUS smbd_smb2_request_process_cancel(struct smbd_smb2_request *req)
881 struct smbd_server_connection *sconn = req->sconn;
882 struct smbd_smb2_request *cur;
883 const uint8_t *inhdr;
884 int i = req->current_idx;
886 uint64_t search_message_id;
887 uint64_t search_async_id;
890 inhdr = (const uint8_t *)req->in.vector[i].iov_base;
892 flags = IVAL(inhdr, SMB2_HDR_FLAGS);
893 search_message_id = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
894 search_async_id = BVAL(inhdr, SMB2_HDR_PID);
897 * we don't need the request anymore
898 * cancel requests never have a response
902 for (cur = sconn->smb2.requests; cur; cur = cur->next) {
903 const uint8_t *outhdr;
907 i = cur->current_idx;
909 outhdr = (const uint8_t *)cur->out.vector[i].iov_base;
911 message_id = BVAL(outhdr, SMB2_HDR_MESSAGE_ID);
912 async_id = BVAL(outhdr, SMB2_HDR_PID);
914 if (flags & SMB2_HDR_FLAG_ASYNC) {
915 if (search_async_id == async_id) {
920 if (search_message_id == message_id) {
921 found_id = message_id;
927 if (cur && cur->subreq) {
928 inhdr = (const uint8_t *)cur->in.vector[i].iov_base;
929 DEBUG(10,("smbd_smb2_request_process_cancel: attempting to "
930 "cancel opcode[%s] mid %llu\n",
931 smb2_opcode_name((uint16_t)IVAL(inhdr, SMB2_HDR_OPCODE)),
932 (unsigned long long)found_id ));
933 tevent_req_cancel(cur->subreq);
939 static NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
941 const uint8_t *inhdr;
942 int i = req->current_idx;
947 NTSTATUS session_status;
948 uint32_t allowed_flags;
950 inhdr = (const uint8_t *)req->in.vector[i].iov_base;
952 /* TODO: verify more things */
954 flags = IVAL(inhdr, SMB2_HDR_FLAGS);
955 opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
956 mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
957 DEBUG(10,("smbd_smb2_request_dispatch: opcode[%s] mid = %llu\n",
958 smb2_opcode_name(opcode),
959 (unsigned long long)mid));
961 allowed_flags = SMB2_HDR_FLAG_CHAINED |
962 SMB2_HDR_FLAG_SIGNED |
964 if (opcode == SMB2_OP_CANCEL) {
965 allowed_flags |= SMB2_HDR_FLAG_ASYNC;
967 if ((flags & ~allowed_flags) != 0) {
968 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
971 session_status = smbd_smb2_request_check_session(req);
973 req->do_signing = false;
974 if (flags & SMB2_HDR_FLAG_SIGNED) {
975 if (!NT_STATUS_IS_OK(session_status)) {
976 return smbd_smb2_request_error(req, session_status);
979 req->do_signing = true;
980 status = smb2_signing_check_pdu(req->session->session_key,
981 &req->in.vector[i], 3);
982 if (!NT_STATUS_IS_OK(status)) {
983 return smbd_smb2_request_error(req, status);
985 } else if (req->session && req->session->do_signing) {
986 return smbd_smb2_request_error(req, NT_STATUS_ACCESS_DENIED);
989 if (flags & SMB2_HDR_FLAG_CHAINED) {
991 * This check is mostly for giving the correct error code
992 * for compounded requests.
994 * TODO: we may need to move this after the session
997 if (!NT_STATUS_IS_OK(req->next_status)) {
998 return smbd_smb2_request_error(req, req->next_status);
1001 req->compat_chain_fsp = NULL;
1005 case SMB2_OP_NEGPROT:
1006 return smbd_smb2_request_process_negprot(req);
1008 case SMB2_OP_SESSSETUP:
1009 return smbd_smb2_request_process_sesssetup(req);
1011 case SMB2_OP_LOGOFF:
1012 if (!NT_STATUS_IS_OK(session_status)) {
1013 return smbd_smb2_request_error(req, session_status);
1015 return smbd_smb2_request_process_logoff(req);
1018 if (!NT_STATUS_IS_OK(session_status)) {
1019 return smbd_smb2_request_error(req, session_status);
1021 status = smbd_smb2_request_check_session(req);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 return smbd_smb2_request_error(req, status);
1025 return smbd_smb2_request_process_tcon(req);
1028 if (!NT_STATUS_IS_OK(session_status)) {
1029 return smbd_smb2_request_error(req, session_status);
1031 status = smbd_smb2_request_check_tcon(req);
1032 if (!NT_STATUS_IS_OK(status)) {
1033 return smbd_smb2_request_error(req, status);
1035 return smbd_smb2_request_process_tdis(req);
1037 case SMB2_OP_CREATE:
1038 if (!NT_STATUS_IS_OK(session_status)) {
1039 return smbd_smb2_request_error(req, session_status);
1041 status = smbd_smb2_request_check_tcon(req);
1042 if (!NT_STATUS_IS_OK(status)) {
1043 return smbd_smb2_request_error(req, status);
1045 return smbd_smb2_request_process_create(req);
1048 if (!NT_STATUS_IS_OK(session_status)) {
1049 return smbd_smb2_request_error(req, session_status);
1051 status = smbd_smb2_request_check_tcon(req);
1052 if (!NT_STATUS_IS_OK(status)) {
1053 return smbd_smb2_request_error(req, status);
1055 return smbd_smb2_request_process_close(req);
1058 if (!NT_STATUS_IS_OK(session_status)) {
1059 return smbd_smb2_request_error(req, session_status);
1061 status = smbd_smb2_request_check_tcon(req);
1062 if (!NT_STATUS_IS_OK(status)) {
1063 return smbd_smb2_request_error(req, status);
1065 return smbd_smb2_request_process_flush(req);
1068 if (!NT_STATUS_IS_OK(session_status)) {
1069 return smbd_smb2_request_error(req, session_status);
1071 status = smbd_smb2_request_check_tcon(req);
1072 if (!NT_STATUS_IS_OK(status)) {
1073 return smbd_smb2_request_error(req, status);
1075 return smbd_smb2_request_process_read(req);
1078 if (!NT_STATUS_IS_OK(session_status)) {
1079 return smbd_smb2_request_error(req, session_status);
1081 status = smbd_smb2_request_check_tcon(req);
1082 if (!NT_STATUS_IS_OK(status)) {
1083 return smbd_smb2_request_error(req, status);
1085 return smbd_smb2_request_process_write(req);
1088 if (!NT_STATUS_IS_OK(session_status)) {
1089 return smbd_smb2_request_error(req, session_status);
1091 status = smbd_smb2_request_check_tcon(req);
1092 if (!NT_STATUS_IS_OK(status)) {
1093 return smbd_smb2_request_error(req, status);
1095 return smbd_smb2_request_process_lock(req);
1098 if (!NT_STATUS_IS_OK(session_status)) {
1099 return smbd_smb2_request_error(req, session_status);
1101 status = smbd_smb2_request_check_tcon(req);
1102 if (!NT_STATUS_IS_OK(status)) {
1103 return smbd_smb2_request_error(req, status);
1105 return smbd_smb2_request_process_ioctl(req);
1107 case SMB2_OP_CANCEL:
1108 return smbd_smb2_request_process_cancel(req);
1110 case SMB2_OP_KEEPALIVE:
1111 return smbd_smb2_request_process_keepalive(req);
1114 if (!NT_STATUS_IS_OK(session_status)) {
1115 return smbd_smb2_request_error(req, session_status);
1117 status = smbd_smb2_request_check_tcon(req);
1118 if (!NT_STATUS_IS_OK(status)) {
1119 return smbd_smb2_request_error(req, status);
1121 return smbd_smb2_request_process_find(req);
1123 case SMB2_OP_NOTIFY:
1124 if (!NT_STATUS_IS_OK(session_status)) {
1125 return smbd_smb2_request_error(req, session_status);
1127 status = smbd_smb2_request_check_tcon(req);
1128 if (!NT_STATUS_IS_OK(status)) {
1129 return smbd_smb2_request_error(req, status);
1131 return smbd_smb2_request_process_notify(req);
1133 case SMB2_OP_GETINFO:
1134 if (!NT_STATUS_IS_OK(session_status)) {
1135 return smbd_smb2_request_error(req, session_status);
1137 status = smbd_smb2_request_check_tcon(req);
1138 if (!NT_STATUS_IS_OK(status)) {
1139 return smbd_smb2_request_error(req, status);
1141 return smbd_smb2_request_process_getinfo(req);
1143 case SMB2_OP_SETINFO:
1144 if (!NT_STATUS_IS_OK(session_status)) {
1145 return smbd_smb2_request_error(req, session_status);
1147 status = smbd_smb2_request_check_tcon(req);
1148 if (!NT_STATUS_IS_OK(status)) {
1149 return smbd_smb2_request_error(req, status);
1151 return smbd_smb2_request_process_setinfo(req);
1154 if (!NT_STATUS_IS_OK(session_status)) {
1155 return smbd_smb2_request_error(req, session_status);
1157 status = smbd_smb2_request_check_tcon(req);
1158 if (!NT_STATUS_IS_OK(status)) {
1159 return smbd_smb2_request_error(req, status);
1161 return smbd_smb2_request_process_break(req);
1164 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
1167 static void smbd_smb2_request_dispatch_compound(struct tevent_context *ctx,
1168 struct tevent_immediate *im,
1169 void *private_data);
1171 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
1173 struct tevent_req *subreq;
1177 smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
1179 if (req->do_signing) {
1180 int i = req->current_idx;
1182 status = smb2_signing_sign_pdu(req->session->session_key,
1183 &req->out.vector[i], 3);
1184 if (!NT_STATUS_IS_OK(status)) {
1189 req->current_idx += 3;
1191 if (req->current_idx < req->out.vector_count) {
1193 * We must process the remaining compound
1194 * SMB2 requests before any new incoming SMB2
1195 * requests. This is because incoming SMB2
1196 * requests may include a cancel for a
1197 * compound request we haven't processed
1200 struct tevent_immediate *im = tevent_create_immediate(req);
1202 return NT_STATUS_NO_MEMORY;
1204 tevent_schedule_immediate(im,
1205 req->sconn->smb2.event_ctx,
1206 smbd_smb2_request_dispatch_compound,
1208 return NT_STATUS_OK;
1211 if (DEBUGLEVEL >= 10) {
1212 dbgtext("smbd_smb2_request_reply: sending...\n");
1213 print_req_vectors(req);
1216 subreq = tstream_writev_queue_send(req,
1217 req->sconn->smb2.event_ctx,
1218 req->sconn->smb2.stream,
1219 req->sconn->smb2.send_queue,
1221 req->out.vector_count);
1222 if (subreq == NULL) {
1223 return NT_STATUS_NO_MEMORY;
1225 tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
1227 return NT_STATUS_OK;
1230 static void smbd_smb2_request_dispatch_compound(struct tevent_context *ctx,
1231 struct tevent_immediate *im,
1234 struct smbd_smb2_request *req = talloc_get_type_abort(private_data,
1235 struct smbd_smb2_request);
1236 struct smbd_server_connection *sconn = req->sconn;
1241 if (DEBUGLEVEL >= 10) {
1242 DEBUG(10,("smbd_smb2_request_dispatch_compound: idx[%d] of %d vectors\n",
1243 req->current_idx, req->in.vector_count));
1244 print_req_vectors(req);
1247 status = smbd_smb2_request_dispatch(req);
1248 if (!NT_STATUS_IS_OK(status)) {
1249 smbd_server_connection_terminate(sconn, nt_errstr(status));
1254 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
1256 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
1257 struct smbd_smb2_request);
1258 struct smbd_server_connection *sconn = req->sconn;
1262 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1263 TALLOC_FREE(subreq);
1266 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1267 DEBUG(2,("smbd_smb2_request_writev_done: client write error %s\n",
1268 nt_errstr(status)));
1269 smbd_server_connection_terminate(sconn, nt_errstr(status));
1274 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
1276 DATA_BLOB body, DATA_BLOB *dyn,
1277 const char *location)
1280 int i = req->current_idx;
1281 uint32_t next_command_ofs;
1283 DEBUG(10,("smbd_smb2_request_done_ex: "
1284 "idx[%d] status[%s] body[%u] dyn[%s:%u] at %s\n",
1285 i, nt_errstr(status), (unsigned int)body.length,
1287 (unsigned int)(dyn ? dyn->length : 0),
1290 if (body.length < 2) {
1291 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1294 if ((body.length % 2) != 0) {
1295 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1298 outhdr = (uint8_t *)req->out.vector[i].iov_base;
1300 next_command_ofs = IVAL(outhdr, SMB2_HDR_NEXT_COMMAND);
1301 SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
1303 req->out.vector[i+1].iov_base = (void *)body.data;
1304 req->out.vector[i+1].iov_len = body.length;
1307 req->out.vector[i+2].iov_base = (void *)dyn->data;
1308 req->out.vector[i+2].iov_len = dyn->length;
1310 req->out.vector[i+2].iov_base = NULL;
1311 req->out.vector[i+2].iov_len = 0;
1314 /* see if we need to recalculate the offset to the next response */
1315 if (next_command_ofs > 0) {
1316 next_command_ofs = SMB2_HDR_BODY;
1317 next_command_ofs += req->out.vector[i+1].iov_len;
1318 next_command_ofs += req->out.vector[i+2].iov_len;
1321 if ((next_command_ofs % 8) != 0) {
1322 size_t pad_size = 8 - (next_command_ofs % 8);
1323 if (req->out.vector[i+2].iov_len == 0) {
1325 * if the dyn buffer is empty
1326 * we can use it to add padding
1330 pad = talloc_zero_array(req->out.vector,
1333 return smbd_smb2_request_error(req,
1334 NT_STATUS_NO_MEMORY);
1337 req->out.vector[i+2].iov_base = (void *)pad;
1338 req->out.vector[i+2].iov_len = pad_size;
1341 * For now we copy the dynamic buffer
1342 * and add the padding to the new buffer
1349 old_size = req->out.vector[i+2].iov_len;
1350 old_dyn = (uint8_t *)req->out.vector[i+2].iov_base;
1352 new_size = old_size + pad_size;
1353 new_dyn = talloc_zero_array(req->out.vector,
1355 if (new_dyn == NULL) {
1356 return smbd_smb2_request_error(req,
1357 NT_STATUS_NO_MEMORY);
1360 memcpy(new_dyn, old_dyn, old_size);
1361 memset(new_dyn + old_size, 0, pad_size);
1363 req->out.vector[i+2].iov_base = (void *)new_dyn;
1364 req->out.vector[i+2].iov_len = new_size;
1366 next_command_ofs += pad_size;
1369 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
1371 return smbd_smb2_request_reply(req);
1374 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
1377 const char *location)
1380 int i = req->current_idx;
1381 uint8_t *outhdr = (uint8_t *)req->out.vector[i].iov_base;
1383 DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s] |%s| at %s\n",
1384 i, nt_errstr(status), info ? " +info" : "",
1387 body.data = outhdr + SMB2_HDR_BODY;
1389 SSVAL(body.data, 0, 9);
1392 SIVAL(body.data, 0x04, info->length);
1394 /* Allocated size of req->out.vector[i].iov_base
1395 * *MUST BE* OUTVEC_ALLOC_SIZE. So we have room for
1396 * 1 byte without having to do an alloc.
1398 info = talloc_zero_array(req->out.vector,
1402 return NT_STATUS_NO_MEMORY;
1404 info->data = ((uint8_t *)outhdr) +
1405 OUTVEC_ALLOC_SIZE - 1;
1407 SCVAL(info->data, 0, 0);
1411 * if a request fails, all other remaining
1412 * compounded requests should fail too
1414 req->next_status = NT_STATUS_INVALID_PARAMETER;
1416 return smbd_smb2_request_done_ex(req, status, body, info, __location__);
1420 struct smbd_smb2_send_oplock_break_state {
1421 struct smbd_server_connection *sconn;
1422 uint8_t buf[4 + SMB2_HDR_BODY + 0x18];
1423 struct iovec vector;
1426 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq);
1428 NTSTATUS smbd_smb2_send_oplock_break(struct smbd_server_connection *sconn,
1429 uint64_t file_id_persistent,
1430 uint64_t file_id_volatile,
1431 uint8_t oplock_level)
1433 struct smbd_smb2_send_oplock_break_state *state;
1434 struct tevent_req *subreq;
1438 state = talloc(sconn, struct smbd_smb2_send_oplock_break_state);
1439 if (state == NULL) {
1440 return NT_STATUS_NO_MEMORY;
1442 state->sconn = sconn;
1444 state->vector.iov_base = (void *)state->buf;
1445 state->vector.iov_len = sizeof(state->buf);
1447 _smb2_setlen(state->buf, sizeof(state->buf) - 4);
1448 hdr = state->buf + 4;
1449 body = hdr + SMB2_HDR_BODY;
1451 SIVAL(hdr, 0, SMB2_MAGIC);
1452 SSVAL(hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
1453 SSVAL(hdr, SMB2_HDR_EPOCH, 0);
1454 SIVAL(hdr, SMB2_HDR_STATUS, 0);
1455 SSVAL(hdr, SMB2_HDR_OPCODE, SMB2_OP_BREAK);
1456 SSVAL(hdr, SMB2_HDR_CREDIT, 0);
1457 SIVAL(hdr, SMB2_HDR_FLAGS, SMB2_HDR_FLAG_REDIRECT);
1458 SIVAL(hdr, SMB2_HDR_NEXT_COMMAND, 0);
1459 SBVAL(hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
1460 SIVAL(hdr, SMB2_HDR_PID, 0);
1461 SIVAL(hdr, SMB2_HDR_TID, 0);
1462 SBVAL(hdr, SMB2_HDR_SESSION_ID, 0);
1463 memset(hdr+SMB2_HDR_SIGNATURE, 0, 16);
1465 SSVAL(body, 0x00, 0x18);
1467 SCVAL(body, 0x02, oplock_level);
1468 SCVAL(body, 0x03, 0); /* reserved */
1469 SIVAL(body, 0x04, 0); /* reserved */
1470 SBVAL(body, 0x08, file_id_persistent);
1471 SBVAL(body, 0x10, file_id_volatile);
1473 subreq = tstream_writev_queue_send(state,
1474 sconn->smb2.event_ctx,
1476 sconn->smb2.send_queue,
1478 if (subreq == NULL) {
1479 return NT_STATUS_NO_MEMORY;
1481 tevent_req_set_callback(subreq,
1482 smbd_smb2_oplock_break_writev_done,
1485 return NT_STATUS_OK;
1488 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq)
1490 struct smbd_smb2_send_oplock_break_state *state =
1491 tevent_req_callback_data(subreq,
1492 struct smbd_smb2_send_oplock_break_state);
1493 struct smbd_server_connection *sconn = state->sconn;
1497 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1498 TALLOC_FREE(subreq);
1500 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1501 smbd_server_connection_terminate(sconn, nt_errstr(status));
1508 struct smbd_smb2_request_read_state {
1510 bool asked_for_header;
1511 struct smbd_smb2_request *smb2_req;
1514 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1516 TALLOC_CTX *mem_ctx,
1517 struct iovec **_vector,
1519 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
1521 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
1522 struct tevent_context *ev,
1523 struct smbd_server_connection *sconn)
1525 struct tevent_req *req;
1526 struct smbd_smb2_request_read_state *state;
1527 struct tevent_req *subreq;
1529 req = tevent_req_create(mem_ctx, &state,
1530 struct smbd_smb2_request_read_state);
1535 state->asked_for_header = false;
1537 state->smb2_req = smbd_smb2_request_allocate(state);
1538 if (tevent_req_nomem(state->smb2_req, req)) {
1539 return tevent_req_post(req, ev);
1541 state->smb2_req->sconn = sconn;
1543 subreq = tstream_readv_pdu_queue_send(state, ev, sconn->smb2.stream,
1544 sconn->smb2.recv_queue,
1545 smbd_smb2_request_next_vector,
1547 if (tevent_req_nomem(subreq, req)) {
1548 return tevent_req_post(req, ev);
1550 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
1555 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1557 TALLOC_CTX *mem_ctx,
1558 struct iovec **_vector,
1561 struct smbd_smb2_request_read_state *state =
1562 talloc_get_type_abort(private_data,
1563 struct smbd_smb2_request_read_state);
1564 struct smbd_smb2_request *req = state->smb2_req;
1565 struct iovec *vector;
1566 int idx = req->in.vector_count;
1568 uint8_t *buf = NULL;
1570 if (req->in.vector_count == 0) {
1572 * first we need to get the NBT header
1574 req->in.vector = talloc_array(req, struct iovec,
1575 req->in.vector_count + 1);
1576 if (req->in.vector == NULL) {
1579 req->in.vector_count += 1;
1581 req->in.vector[idx].iov_base = (void *)req->in.nbt_hdr;
1582 req->in.vector[idx].iov_len = 4;
1584 vector = talloc_array(mem_ctx, struct iovec, 1);
1585 if (vector == NULL) {
1589 vector[0] = req->in.vector[idx];
1596 if (req->in.vector_count == 1) {
1598 * Now we analyze the NBT header
1600 state->missing = smb2_len(req->in.vector[0].iov_base);
1602 if (state->missing == 0) {
1603 /* if there're no remaining bytes, we're done */
1609 req->in.vector = talloc_realloc(req, req->in.vector,
1611 req->in.vector_count + 1);
1612 if (req->in.vector == NULL) {
1615 req->in.vector_count += 1;
1617 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
1619 * it's a special NBT message,
1620 * so get all remaining bytes
1622 len = state->missing;
1623 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
1625 * it's an invalid message, just read what we can get
1626 * and let the caller handle the error
1628 len = state->missing;
1631 * We assume it's a SMB2 request,
1632 * and we first get the header and the
1633 * first 2 bytes (the struct size) of the body
1635 len = SMB2_HDR_BODY + 2;
1637 state->asked_for_header = true;
1640 state->missing -= len;
1642 buf = talloc_array(req->in.vector, uint8_t, len);
1647 req->in.vector[idx].iov_base = (void *)buf;
1648 req->in.vector[idx].iov_len = len;
1650 vector = talloc_array(mem_ctx, struct iovec, 1);
1651 if (vector == NULL) {
1655 vector[0] = req->in.vector[idx];
1662 if (state->missing == 0) {
1663 /* if there're no remaining bytes, we're done */
1669 if (state->asked_for_header) {
1672 size_t next_command_ofs;
1677 bool invalid = false;
1679 state->asked_for_header = false;
1682 * We got the SMB2 header and the first 2 bytes
1683 * of the body. We fix the size to just the header
1684 * and manually copy the 2 first bytes to the body section
1686 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
1687 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
1689 /* allocate vectors for body and dynamic areas */
1690 req->in.vector = talloc_realloc(req, req->in.vector,
1692 req->in.vector_count + 2);
1693 if (req->in.vector == NULL) {
1696 req->in.vector_count += 2;
1698 full_size = state->missing + SMB2_HDR_BODY + 2;
1699 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
1700 body_size = SVAL(hdr, SMB2_HDR_BODY);
1702 if (next_command_ofs != 0) {
1703 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
1705 * this is invalid, just return a zero
1706 * body and let the caller deal with the error
1709 } else if (next_command_ofs > full_size) {
1711 * this is invalid, just return a zero
1712 * body and let the caller deal with the error
1716 full_size = next_command_ofs;
1721 if (body_size < 2) {
1723 * this is invalid, just return a zero
1724 * body and let the caller deal with the error
1729 if ((body_size % 2) != 0) {
1733 if (body_size > (full_size - SMB2_HDR_BODY)) {
1735 * this is invalid, just return a zero
1736 * body and let the caller deal with the error
1743 /* the caller should check this */
1747 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
1749 state->missing -= (body_size - 2) + dyn_size;
1751 body = talloc_array(req->in.vector, uint8_t, body_size);
1756 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
1761 req->in.vector[idx].iov_base = (void *)body;
1762 req->in.vector[idx].iov_len = body_size;
1763 req->in.vector[idx+1].iov_base = (void *)dyn;
1764 req->in.vector[idx+1].iov_len = dyn_size;
1766 vector = talloc_array(mem_ctx, struct iovec, 2);
1767 if (vector == NULL) {
1772 * the first 2 bytes of the body were already fetched
1773 * together with the header
1775 memcpy(body, hdr + SMB2_HDR_BODY, 2);
1776 vector[0].iov_base = body + 2;
1777 vector[0].iov_len = body_size - 2;
1779 vector[1] = req->in.vector[idx+1];
1787 * when we endup here, we're looking for a new SMB2 request
1788 * next. And we ask for its header and the first 2 bytes of
1789 * the body (like we did for the first SMB2 request).
1792 req->in.vector = talloc_realloc(req, req->in.vector,
1794 req->in.vector_count + 1);
1795 if (req->in.vector == NULL) {
1798 req->in.vector_count += 1;
1801 * We assume it's a SMB2 request,
1802 * and we first get the header and the
1803 * first 2 bytes (the struct size) of the body
1805 len = SMB2_HDR_BODY + 2;
1807 if (len > state->missing) {
1808 /* let the caller handle the error */
1809 len = state->missing;
1812 state->missing -= len;
1813 state->asked_for_header = true;
1815 buf = talloc_array(req->in.vector, uint8_t, len);
1820 req->in.vector[idx].iov_base = (void *)buf;
1821 req->in.vector[idx].iov_len = len;
1823 vector = talloc_array(mem_ctx, struct iovec, 1);
1824 if (vector == NULL) {
1828 vector[0] = req->in.vector[idx];
1835 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
1837 struct tevent_req *req =
1838 tevent_req_callback_data(subreq,
1844 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1846 status = map_nt_error_from_unix(sys_errno);
1847 tevent_req_nterror(req, status);
1851 tevent_req_done(req);
1854 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
1855 TALLOC_CTX *mem_ctx,
1856 struct smbd_smb2_request **_smb2_req)
1858 struct smbd_smb2_request_read_state *state =
1859 tevent_req_data(req,
1860 struct smbd_smb2_request_read_state);
1863 if (tevent_req_is_nterror(req, &status)) {
1864 tevent_req_received(req);
1868 talloc_steal(mem_ctx, state->smb2_req->mem_pool);
1869 *_smb2_req = state->smb2_req;
1870 tevent_req_received(req);
1871 return NT_STATUS_OK;
1874 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
1876 void smbd_smb2_first_negprot(struct smbd_server_connection *sconn,
1877 const uint8_t *inbuf, size_t size)
1880 struct smbd_smb2_request *req;
1881 struct tevent_req *subreq;
1883 DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
1884 (unsigned int)size));
1886 status = smbd_initialize_smb2(sconn);
1887 if (!NT_STATUS_IS_OK(status)) {
1888 smbd_server_connection_terminate(sconn, nt_errstr(status));
1892 status = smbd_smb2_request_create(sconn, inbuf, size, &req);
1893 if (!NT_STATUS_IS_OK(status)) {
1894 smbd_server_connection_terminate(sconn, nt_errstr(status));
1898 status = smbd_smb2_request_setup_out(req, (uint16_t)lp_maxmux());
1899 if (!NT_STATUS_IS_OK(status)) {
1900 smbd_server_connection_terminate(sconn, nt_errstr(status));
1904 status = smbd_smb2_request_dispatch(req);
1905 if (!NT_STATUS_IS_OK(status)) {
1906 smbd_server_connection_terminate(sconn, nt_errstr(status));
1910 /* ask for the next request */
1911 subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
1912 if (subreq == NULL) {
1913 smbd_server_connection_terminate(sconn, "no memory for reading");
1916 tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);
1919 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
1921 uint16_t creds_requested = 0;
1922 struct smbd_server_connection *sconn = tevent_req_callback_data(subreq,
1923 struct smbd_server_connection);
1925 struct smbd_smb2_request *req = NULL;
1927 status = smbd_smb2_request_read_recv(subreq, sconn, &req);
1928 TALLOC_FREE(subreq);
1929 if (!NT_STATUS_IS_OK(status)) {
1930 DEBUG(2,("smbd_smb2_request_incoming: client read error %s\n",
1931 nt_errstr(status)));
1932 smbd_server_connection_terminate(sconn, nt_errstr(status));
1936 if (req->in.nbt_hdr[0] != 0x00) {
1937 DEBUG(1,("smbd_smb2_request_incoming: ignore NBT[0x%02X] msg\n",
1938 req->in.nbt_hdr[0]));
1943 req->current_idx = 1;
1945 DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
1946 req->current_idx, req->in.vector_count));
1948 status = smbd_smb2_request_validate(req, &creds_requested);
1949 if (!NT_STATUS_IS_OK(status)) {
1950 smbd_server_connection_terminate(sconn, nt_errstr(status));
1954 status = smbd_smb2_request_setup_out(req, 5);
1955 if (!NT_STATUS_IS_OK(status)) {
1956 smbd_server_connection_terminate(sconn, nt_errstr(status));
1960 status = smbd_smb2_request_dispatch(req);
1961 if (!NT_STATUS_IS_OK(status)) {
1962 smbd_server_connection_terminate(sconn, nt_errstr(status));
1967 /* ask for the next request (this constructs the main loop) */
1968 subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
1969 if (subreq == NULL) {
1970 smbd_server_connection_terminate(sconn, "no memory for reading");
1973 tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);