2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/tsocket/tsocket.h"
27 #define OUTVEC_ALLOC_SIZE (SMB2_HDR_BODY + 9)
29 static const char *smb2_names[] = {
51 const char *smb2_opcode_name(uint16_t opcode)
54 return "Bad SMB2 opcode";
56 return smb2_names[opcode];
59 static void print_req_vectors(struct smbd_smb2_request *req)
63 for (i = 0; i < req->in.vector_count; i++) {
64 dbgtext("\treq->in.vector[%u].iov_len = %u\n",
66 (unsigned int)req->in.vector[i].iov_len);
68 for (i = 0; i < req->out.vector_count; i++) {
69 dbgtext("\treq->out.vector[%u].iov_len = %u\n",
71 (unsigned int)req->out.vector[i].iov_len);
75 bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size)
77 if (size < (4 + SMB2_HDR_BODY)) {
81 if (IVAL(inbuf, 4) != SMB2_MAGIC) {
88 static NTSTATUS smbd_initialize_smb2(struct smbd_server_connection *sconn)
93 TALLOC_FREE(sconn->smb1.fde);
95 sconn->smb2.event_ctx = smbd_event_context();
97 sconn->smb2.recv_queue = tevent_queue_create(sconn, "smb2 recv queue");
98 if (sconn->smb2.recv_queue == NULL) {
99 return NT_STATUS_NO_MEMORY;
102 sconn->smb2.send_queue = tevent_queue_create(sconn, "smb2 send queue");
103 if (sconn->smb2.send_queue == NULL) {
104 return NT_STATUS_NO_MEMORY;
107 sconn->smb2.sessions.idtree = idr_init(sconn);
108 if (sconn->smb2.sessions.idtree == NULL) {
109 return NT_STATUS_NO_MEMORY;
111 sconn->smb2.sessions.limit = 0x0000FFFE;
112 sconn->smb2.sessions.list = NULL;
113 sconn->smb2.seqnum_low = 0;
114 sconn->smb2.credits_granted = 0;
115 sconn->smb2.max_credits = lp_smb2_max_credits();
116 sconn->smb2.credits_bitmap = bitmap_talloc(sconn,
117 DEFAULT_SMB2_MAX_CREDIT_BITMAP_FACTOR*sconn->smb2.max_credits);
118 if (sconn->smb2.credits_bitmap == NULL) {
119 return NT_STATUS_NO_MEMORY;
122 ret = tstream_bsd_existing_socket(sconn, sconn->sock,
123 &sconn->smb2.stream);
125 status = map_nt_error_from_unix(errno);
129 /* Ensure child is set to non-blocking mode */
130 set_blocking(sconn->sock, false);
134 #define smb2_len(buf) (PVAL(buf,3)|(PVAL(buf,2)<<8)|(PVAL(buf,1)<<16))
135 #define _smb2_setlen(_buf,len) do { \
136 uint8_t *buf = (uint8_t *)_buf; \
138 buf[1] = ((len)&0xFF0000)>>16; \
139 buf[2] = ((len)&0xFF00)>>8; \
140 buf[3] = (len)&0xFF; \
143 static void smb2_setup_nbt_length(struct iovec *vector, int count)
148 for (i=1; i < count; i++) {
149 len += vector[i].iov_len;
152 _smb2_setlen(vector[0].iov_base, len);
155 static int smbd_smb2_request_parent_destructor(struct smbd_smb2_request **req)
158 (*req)->parent = NULL;
159 (*req)->mem_pool = NULL;
165 static int smbd_smb2_request_destructor(struct smbd_smb2_request *req)
169 talloc_free(req->mem_pool);
175 static struct smbd_smb2_request *smbd_smb2_request_allocate(TALLOC_CTX *mem_ctx)
177 TALLOC_CTX *mem_pool;
178 struct smbd_smb2_request **parent;
179 struct smbd_smb2_request *req;
182 /* Enable this to find subtle valgrind errors. */
183 mem_pool = talloc_init("smbd_smb2_request_allocate");
185 mem_pool = talloc_pool(mem_ctx, 8192);
187 if (mem_pool == NULL) {
191 parent = talloc(mem_pool, struct smbd_smb2_request *);
192 if (parent == NULL) {
193 talloc_free(mem_pool);
197 req = talloc_zero(parent, struct smbd_smb2_request);
199 talloc_free(mem_pool);
203 req->mem_pool = mem_pool;
204 req->parent = parent;
206 talloc_set_destructor(parent, smbd_smb2_request_parent_destructor);
207 talloc_set_destructor(req, smbd_smb2_request_destructor);
212 static NTSTATUS smbd_smb2_request_create(struct smbd_server_connection *sconn,
213 const uint8_t *inbuf, size_t size,
214 struct smbd_smb2_request **_req)
216 struct smbd_smb2_request *req;
217 uint32_t protocol_version;
218 const uint8_t *inhdr = NULL;
221 uint32_t next_command_ofs;
223 if (size < (4 + SMB2_HDR_BODY + 2)) {
224 DEBUG(0,("Invalid SMB2 packet length count %ld\n", (long)size));
225 return NT_STATUS_INVALID_PARAMETER;
230 protocol_version = IVAL(inhdr, SMB2_HDR_PROTOCOL_ID);
231 if (protocol_version != SMB2_MAGIC) {
232 DEBUG(0,("Invalid SMB packet: protocol prefix: 0x%08X\n",
234 return NT_STATUS_INVALID_PARAMETER;
237 cmd = SVAL(inhdr, SMB2_HDR_OPCODE);
238 if (cmd != SMB2_OP_NEGPROT) {
239 DEBUG(0,("Invalid SMB packet: first request: 0x%04X\n",
241 return NT_STATUS_INVALID_PARAMETER;
244 next_command_ofs = IVAL(inhdr, SMB2_HDR_NEXT_COMMAND);
245 if (next_command_ofs != 0) {
246 DEBUG(0,("Invalid SMB packet: next_command: 0x%08X\n",
248 return NT_STATUS_INVALID_PARAMETER;
251 req = smbd_smb2_request_allocate(sconn);
253 return NT_STATUS_NO_MEMORY;
257 talloc_steal(req, inbuf);
259 req->in.vector = talloc_array(req, struct iovec, 4);
260 if (req->in.vector == NULL) {
262 return NT_STATUS_NO_MEMORY;
264 req->in.vector_count = 4;
266 memcpy(req->in.nbt_hdr, inbuf, 4);
269 req->in.vector[0].iov_base = (void *)req->in.nbt_hdr;
270 req->in.vector[0].iov_len = 4;
271 ofs += req->in.vector[0].iov_len;
273 req->in.vector[1].iov_base = (void *)(inbuf + ofs);
274 req->in.vector[1].iov_len = SMB2_HDR_BODY;
275 ofs += req->in.vector[1].iov_len;
277 req->in.vector[2].iov_base = (void *)(inbuf + ofs);
278 req->in.vector[2].iov_len = SVAL(inbuf, ofs) & 0xFFFE;
279 ofs += req->in.vector[2].iov_len;
282 return NT_STATUS_INVALID_PARAMETER;
285 req->in.vector[3].iov_base = (void *)(inbuf + ofs);
286 req->in.vector[3].iov_len = size - ofs;
287 ofs += req->in.vector[3].iov_len;
289 req->current_idx = 1;
295 static bool smb2_validate_message_id(struct smbd_server_connection *sconn,
296 const uint8_t *inhdr)
298 uint64_t message_id = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
299 struct bitmap *credits_bm = sconn->smb2.credits_bitmap;
300 uint16_t opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
301 unsigned int bitmap_offset;
303 if (opcode == SMB2_OP_CANCEL) {
304 /* SMB2_CANCEL requests by definition resend messageids. */
308 if (message_id < sconn->smb2.seqnum_low ||
309 message_id > (sconn->smb2.seqnum_low +
310 (sconn->smb2.max_credits * DEFAULT_SMB2_MAX_CREDIT_BITMAP_FACTOR))) {
311 DEBUG(0,("smb2_validate_message_id: bad message_id "
312 "%llu (low = %llu, max = %lu)\n",
313 (unsigned long long)message_id,
314 (unsigned long long)sconn->smb2.seqnum_low,
315 (unsigned long)sconn->smb2.max_credits ));
319 /* client just used a credit. */
320 SMB_ASSERT(sconn->smb2.credits_granted > 0);
321 sconn->smb2.credits_granted -= 1;
323 /* Mark the message_id as seen in the bitmap. */
324 bitmap_offset = (unsigned int)(message_id %
325 (uint64_t)(sconn->smb2.max_credits * DEFAULT_SMB2_MAX_CREDIT_BITMAP_FACTOR));
326 if (bitmap_query(credits_bm, bitmap_offset)) {
327 DEBUG(0,("smb2_validate_message_id: duplicate message_id "
328 "%llu (bm offset %u)\n",
329 (unsigned long long)message_id,
333 bitmap_set(credits_bm, bitmap_offset);
335 if (message_id == sconn->smb2.seqnum_low + 1) {
336 /* Move the window forward by all the message_id's
338 while (bitmap_query(credits_bm, bitmap_offset)) {
339 DEBUG(10,("smb2_validate_message_id: clearing "
340 "id %llu (position %u) from bitmap\n",
341 (unsigned long long)(sconn->smb2.seqnum_low + 1),
343 bitmap_clear(credits_bm, bitmap_offset);
344 sconn->smb2.seqnum_low += 1;
345 bitmap_offset = (bitmap_offset + 1) %
346 (sconn->smb2.max_credits * DEFAULT_SMB2_MAX_CREDIT_BITMAP_FACTOR);
353 static NTSTATUS smbd_smb2_request_validate(struct smbd_smb2_request *req)
357 bool compound_related = false;
359 count = req->in.vector_count;
362 /* It's not a SMB2 request */
363 return NT_STATUS_INVALID_PARAMETER;
366 for (idx=1; idx < count; idx += 3) {
367 const uint8_t *inhdr = NULL;
370 if (req->in.vector[idx].iov_len != SMB2_HDR_BODY) {
371 return NT_STATUS_INVALID_PARAMETER;
374 if (req->in.vector[idx+1].iov_len < 2) {
375 return NT_STATUS_INVALID_PARAMETER;
378 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
380 /* Check the SMB2 header */
381 if (IVAL(inhdr, SMB2_HDR_PROTOCOL_ID) != SMB2_MAGIC) {
382 return NT_STATUS_INVALID_PARAMETER;
385 if (!smb2_validate_message_id(req->sconn, inhdr)) {
386 return NT_STATUS_INVALID_PARAMETER;
389 flags = IVAL(inhdr, SMB2_HDR_FLAGS);
392 * the 1st request should never have the
393 * SMB2_HDR_FLAG_CHAINED flag set
395 if (flags & SMB2_HDR_FLAG_CHAINED) {
396 req->next_status = NT_STATUS_INVALID_PARAMETER;
399 } else if (idx == 4) {
401 * the 2nd request triggers related vs. unrelated
402 * compounded requests
404 if (flags & SMB2_HDR_FLAG_CHAINED) {
405 compound_related = true;
407 } else if (idx > 4) {
410 * It seems the this tests are wrong
411 * see the SMB2-COMPOUND test
415 * all other requests should match the 2nd one
417 if (flags & SMB2_HDR_FLAG_CHAINED) {
418 if (!compound_related) {
420 NT_STATUS_INVALID_PARAMETER;
424 if (compound_related) {
426 NT_STATUS_INVALID_PARAMETER;
437 static void smb2_set_operation_credit(struct smbd_server_connection *sconn,
438 const struct iovec *in_vector,
439 struct iovec *out_vector)
441 uint8_t *outhdr = out_vector->iov_base;
442 uint16_t credits_requested = 0;
443 uint16_t credits_granted = 0;
445 if (in_vector != NULL) {
446 const uint8_t *inhdr = (const uint8_t *)in_vector->iov_base;
447 credits_requested = SVAL(inhdr, SMB2_HDR_CREDIT);
450 SMB_ASSERT(sconn->smb2.max_credits >= sconn->smb2.credits_granted);
452 /* Remember what we gave out. */
453 credits_granted = MIN(credits_requested, (sconn->smb2.max_credits -
454 sconn->smb2.credits_granted));
456 if (credits_granted == 0 && sconn->smb2.credits_granted == 0) {
457 /* First negprot packet, or ensure the client credits can
458 never drop to zero. */
462 SSVAL(outhdr, SMB2_HDR_CREDIT, credits_granted);
463 sconn->smb2.credits_granted += credits_granted;
465 DEBUG(10,("smb2_set_operation_credit: requested %u, "
466 "granted %u, total granted %u\n",
467 (unsigned int)credits_requested,
468 (unsigned int)credits_granted,
469 (unsigned int)sconn->smb2.credits_granted ));
472 static void smb2_calculate_credits(const struct smbd_smb2_request *inreq,
473 struct smbd_smb2_request *outreq)
477 count = outreq->out.vector_count;
479 for (idx=1; idx < count; idx += 3) {
480 smb2_set_operation_credit(outreq->sconn,
481 &inreq->in.vector[idx],
482 &outreq->out.vector[idx]);
486 static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req)
488 struct iovec *vector;
492 count = req->in.vector_count;
493 vector = talloc_zero_array(req, struct iovec, count);
494 if (vector == NULL) {
495 return NT_STATUS_NO_MEMORY;
498 vector[0].iov_base = req->out.nbt_hdr;
499 vector[0].iov_len = 4;
500 SIVAL(req->out.nbt_hdr, 0, 0);
502 for (idx=1; idx < count; idx += 3) {
503 const uint8_t *inhdr = NULL;
505 uint8_t *outhdr = NULL;
506 uint8_t *outbody = NULL;
507 uint32_t next_command_ofs = 0;
508 struct iovec *current = &vector[idx];
510 if ((idx + 3) < count) {
511 /* we have a next command -
512 * setup for the error case. */
513 next_command_ofs = SMB2_HDR_BODY + 9;
516 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
517 in_flags = IVAL(inhdr, SMB2_HDR_FLAGS);
519 outhdr = talloc_zero_array(vector, uint8_t,
521 if (outhdr == NULL) {
522 return NT_STATUS_NO_MEMORY;
525 outbody = outhdr + SMB2_HDR_BODY;
527 current[0].iov_base = (void *)outhdr;
528 current[0].iov_len = SMB2_HDR_BODY;
530 current[1].iov_base = (void *)outbody;
531 current[1].iov_len = 8;
533 current[2].iov_base = NULL;
534 current[2].iov_len = 0;
536 /* setup the SMB2 header */
537 SIVAL(outhdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
538 SSVAL(outhdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
539 SSVAL(outhdr, SMB2_HDR_EPOCH, 0);
540 SIVAL(outhdr, SMB2_HDR_STATUS,
541 NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));
542 SSVAL(outhdr, SMB2_HDR_OPCODE,
543 SVAL(inhdr, SMB2_HDR_OPCODE));
544 SIVAL(outhdr, SMB2_HDR_FLAGS,
545 IVAL(inhdr, SMB2_HDR_FLAGS) | SMB2_HDR_FLAG_REDIRECT);
546 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
547 SBVAL(outhdr, SMB2_HDR_MESSAGE_ID,
548 BVAL(inhdr, SMB2_HDR_MESSAGE_ID));
549 SIVAL(outhdr, SMB2_HDR_PID,
550 IVAL(inhdr, SMB2_HDR_PID));
551 SIVAL(outhdr, SMB2_HDR_TID,
552 IVAL(inhdr, SMB2_HDR_TID));
553 SBVAL(outhdr, SMB2_HDR_SESSION_ID,
554 BVAL(inhdr, SMB2_HDR_SESSION_ID));
555 memset(outhdr + SMB2_HDR_SIGNATURE, 0, 16);
557 /* setup error body header */
558 SSVAL(outbody, 0x00, 0x08 + 1);
559 SSVAL(outbody, 0x02, 0);
560 SIVAL(outbody, 0x04, 0);
563 req->out.vector = vector;
564 req->out.vector_count = count;
566 /* setup the length of the NBT packet */
567 smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
569 DLIST_ADD_END(req->sconn->smb2.requests, req, struct smbd_smb2_request *);
574 void smbd_server_connection_terminate_ex(struct smbd_server_connection *sconn,
576 const char *location)
578 DEBUG(10,("smbd_server_connection_terminate_ex: reason[%s] at %s\n",
580 exit_server_cleanly(reason);
583 static bool dup_smb2_vec3(TALLOC_CTX *ctx,
584 struct iovec *outvec,
585 const struct iovec *srcvec)
587 /* vec[0] is always boilerplate and must
588 * be allocated with size OUTVEC_ALLOC_SIZE. */
590 outvec[0].iov_base = talloc_memdup(ctx,
593 if (!outvec[0].iov_base) {
596 outvec[0].iov_len = SMB2_HDR_BODY;
599 * If this is a "standard" vec[1] of length 8,
600 * pointing to srcvec[0].iov_base + SMB2_HDR_BODY,
601 * then duplicate this. Else use talloc_memdup().
604 if (srcvec[1].iov_len == 8 &&
605 srcvec[1].iov_base ==
606 ((uint8_t *)srcvec[0].iov_base) +
608 outvec[1].iov_base = ((uint8_t *)outvec[1].iov_base) +
610 outvec[1].iov_len = 8;
612 outvec[1].iov_base = talloc_memdup(ctx,
615 if (!outvec[1].iov_base) {
618 outvec[1].iov_len = srcvec[1].iov_len;
622 * If this is a "standard" vec[2] of length 1,
623 * pointing to srcvec[0].iov_base + (OUTVEC_ALLOC_SIZE - 1)
624 * then duplicate this. Else use talloc_memdup().
627 if (srcvec[2].iov_base &&
629 if (srcvec[2].iov_base ==
630 ((uint8_t *)srcvec[0].iov_base) +
631 (OUTVEC_ALLOC_SIZE - 1) &&
632 srcvec[2].iov_len == 1) {
633 /* Common SMB2 error packet case. */
634 outvec[2].iov_base = ((uint8_t *)outvec[0].iov_base) +
635 (OUTVEC_ALLOC_SIZE - 1);
637 outvec[2].iov_base = talloc_memdup(ctx,
640 if (!outvec[2].iov_base) {
644 outvec[2].iov_len = srcvec[2].iov_len;
646 outvec[2].iov_base = NULL;
647 outvec[2].iov_len = 0;
652 static struct smbd_smb2_request *dup_smb2_req(const struct smbd_smb2_request *req)
654 struct smbd_smb2_request *newreq = NULL;
655 struct iovec *outvec = NULL;
656 int count = req->out.vector_count;
659 newreq = smbd_smb2_request_allocate(req->sconn);
664 newreq->sconn = req->sconn;
665 newreq->do_signing = req->do_signing;
666 newreq->current_idx = req->current_idx;
667 newreq->async = false;
668 newreq->cancelled = false;
670 outvec = talloc_zero_array(newreq, struct iovec, count);
675 newreq->out.vector = outvec;
676 newreq->out.vector_count = count;
678 /* Setup the outvec's identically to req. */
679 outvec[0].iov_base = newreq->out.nbt_hdr;
680 outvec[0].iov_len = 4;
681 memcpy(newreq->out.nbt_hdr, req->out.nbt_hdr, 4);
683 /* Setup the vectors identically to the ones in req. */
684 for (i = 1; i < count; i += 3) {
685 if (!dup_smb2_vec3(outvec, &outvec[i], &req->out.vector[i])) {
696 smb2_setup_nbt_length(newreq->out.vector,
697 newreq->out.vector_count);
702 static void smbd_smb2_request_writev_done(struct tevent_req *subreq);
704 static NTSTATUS smb2_send_async_interim_response(const struct smbd_smb2_request *req)
707 uint8_t *outhdr = NULL;
708 struct smbd_smb2_request *nreq = NULL;
710 /* Create a new smb2 request we'll use
711 for the interim return. */
712 nreq = dup_smb2_req(req);
714 return NT_STATUS_NO_MEMORY;
717 /* Lose the last 3 out vectors. They're the
718 ones we'll be using for the async reply. */
719 nreq->out.vector_count -= 3;
721 smb2_setup_nbt_length(nreq->out.vector,
722 nreq->out.vector_count);
724 /* Step back to the previous reply. */
725 i = nreq->current_idx - 3;
726 outhdr = (uint8_t *)nreq->out.vector[i].iov_base;
727 /* And end the chain. */
728 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
730 /* Calculate outgoing credits */
731 smb2_calculate_credits(req, nreq);
733 /* Re-sign if needed. */
734 if (nreq->do_signing) {
736 status = smb2_signing_sign_pdu(nreq->session->session_key,
737 &nreq->out.vector[i], 3);
738 if (!NT_STATUS_IS_OK(status)) {
742 if (DEBUGLEVEL >= 10) {
743 dbgtext("smb2_send_async_interim_response: nreq->current_idx = %u\n",
744 (unsigned int)nreq->current_idx );
745 dbgtext("smb2_send_async_interim_response: returning %u vectors\n",
746 (unsigned int)nreq->out.vector_count );
747 print_req_vectors(nreq);
749 nreq->subreq = tstream_writev_queue_send(nreq,
750 nreq->sconn->smb2.event_ctx,
751 nreq->sconn->smb2.stream,
752 nreq->sconn->smb2.send_queue,
754 nreq->out.vector_count);
756 if (nreq->subreq == NULL) {
757 return NT_STATUS_NO_MEMORY;
760 tevent_req_set_callback(nreq->subreq,
761 smbd_smb2_request_writev_done,
767 struct smbd_smb2_request_pending_state {
768 struct smbd_server_connection *sconn;
769 uint8_t buf[4 + SMB2_HDR_BODY + 0x08 + 1];
770 struct iovec vector[3];
773 static void smbd_smb2_request_pending_writev_done(struct tevent_req *subreq)
775 struct smbd_smb2_request_pending_state *state =
776 tevent_req_callback_data(subreq,
777 struct smbd_smb2_request_pending_state);
778 struct smbd_server_connection *sconn = state->sconn;
782 ret = tstream_writev_queue_recv(subreq, &sys_errno);
785 NTSTATUS status = map_nt_error_from_unix(sys_errno);
786 smbd_server_connection_terminate(sconn, nt_errstr(status));
793 NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
794 struct tevent_req *subreq)
797 struct smbd_smb2_request_pending_state *state = NULL;
798 int i = req->current_idx;
799 uint8_t *reqhdr = NULL;
801 uint8_t *body = NULL;
803 uint64_t message_id = 0;
804 uint64_t async_id = 0;
805 struct iovec *outvec = NULL;
807 if (!tevent_req_is_in_progress(subreq)) {
811 req->subreq = subreq;
815 /* We're already async. */
819 if (req->in.vector_count > i + 3) {
821 * We're trying to go async in a compound
822 * request chain. This is not allowed.
823 * Cancel the outstanding request.
825 tevent_req_cancel(req->subreq);
826 return smbd_smb2_request_error(req,
827 NT_STATUS_INSUFFICIENT_RESOURCES);
830 if (DEBUGLEVEL >= 10) {
831 dbgtext("smbd_smb2_request_pending_queue: req->current_idx = %u\n",
832 (unsigned int)req->current_idx );
833 print_req_vectors(req);
836 if (req->out.vector_count > 4) {
837 /* This is a compound reply. We
838 * must do an interim response
839 * followed by the async response
842 status = smb2_send_async_interim_response(req);
843 if (!NT_STATUS_IS_OK(status)) {
848 /* Don't return an intermediate packet on a pipe read/write. */
849 if (req->tcon && req->tcon->compat_conn && IS_IPC(req->tcon->compat_conn)) {
853 reqhdr = (uint8_t *)req->out.vector[i].iov_base;
854 flags = (IVAL(reqhdr, SMB2_HDR_FLAGS) & ~SMB2_HDR_FLAG_CHAINED);
855 message_id = BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
856 async_id = message_id; /* keep it simple for now... */
859 * What we send is identical to a smbd_smb2_request_error
860 * packet with an error status of STATUS_PENDING. Make use
861 * of this fact sometime when refactoring. JRA.
864 state = talloc_zero(req->sconn, struct smbd_smb2_request_pending_state);
866 return NT_STATUS_NO_MEMORY;
868 state->sconn = req->sconn;
870 state->vector[0].iov_base = (void *)state->buf;
871 state->vector[0].iov_len = 4;
873 state->vector[1].iov_base = state->buf + 4;
874 state->vector[1].iov_len = SMB2_HDR_BODY;
876 state->vector[2].iov_base = state->buf + 4 + SMB2_HDR_BODY;
877 state->vector[2].iov_len = 9;
879 smb2_setup_nbt_length(state->vector, 3);
881 hdr = (uint8_t *)state->vector[1].iov_base;
882 body = (uint8_t *)state->vector[2].iov_base;
884 SIVAL(hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
885 SSVAL(hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
886 SSVAL(hdr, SMB2_HDR_EPOCH, 0);
887 SIVAL(hdr, SMB2_HDR_STATUS, NT_STATUS_V(STATUS_PENDING));
888 SSVAL(hdr, SMB2_HDR_OPCODE, SVAL(reqhdr, SMB2_HDR_OPCODE));
890 SIVAL(hdr, SMB2_HDR_FLAGS, flags | SMB2_HDR_FLAG_ASYNC);
891 SIVAL(hdr, SMB2_HDR_NEXT_COMMAND, 0);
892 SBVAL(hdr, SMB2_HDR_MESSAGE_ID, message_id);
893 SBVAL(hdr, SMB2_HDR_PID, async_id);
894 SBVAL(hdr, SMB2_HDR_SESSION_ID,
895 BVAL(reqhdr, SMB2_HDR_SESSION_ID));
896 memset(hdr+SMB2_HDR_SIGNATURE, 0, 16);
898 SSVAL(body, 0x00, 0x08 + 1);
900 SCVAL(body, 0x02, 0);
901 SCVAL(body, 0x03, 0);
902 SIVAL(body, 0x04, 0);
903 /* Match W2K8R2... */
904 SCVAL(body, 0x08, 0x21);
906 /* Ensure we correctly go through crediting. Grant
907 the credits now, and zero credits on the final
909 smb2_set_operation_credit(req->sconn,
913 if (req->do_signing) {
914 status = smb2_signing_sign_pdu(req->session->session_key,
916 if (!NT_STATUS_IS_OK(status)) {
921 subreq = tstream_writev_queue_send(state,
922 req->sconn->smb2.event_ctx,
923 req->sconn->smb2.stream,
924 req->sconn->smb2.send_queue,
928 if (subreq == NULL) {
929 return NT_STATUS_NO_MEMORY;
932 tevent_req_set_callback(subreq,
933 smbd_smb2_request_pending_writev_done,
936 /* Note we're going async with this request. */
940 * Now manipulate req so that the outstanding async request
941 * is the only one left in the struct smbd_smb2_request.
944 if (req->current_idx == 1) {
945 /* There was only one. */
949 /* Re-arrange the in.vectors. */
950 req->in.vector[1] = req->in.vector[i];
951 req->in.vector[2] = req->in.vector[i+1];
952 req->in.vector[3] = req->in.vector[i+2];
953 req->in.vector_count = 4;
954 /* Reset the new in size. */
955 smb2_setup_nbt_length(req->in.vector, 4);
957 /* Now recreate the out.vectors. */
958 outvec = talloc_zero_array(req, struct iovec, 4);
960 return NT_STATUS_NO_MEMORY;
963 /* 0 is always boilerplate and must
964 * be of size 4 for the length field. */
966 outvec[0].iov_base = req->out.nbt_hdr;
967 outvec[0].iov_len = 4;
968 SIVAL(req->out.nbt_hdr, 0, 0);
970 if (!dup_smb2_vec3(outvec, &outvec[1], &req->out.vector[i])) {
971 return NT_STATUS_NO_MEMORY;
974 TALLOC_FREE(req->out.vector);
976 req->out.vector = outvec;
978 req->current_idx = 1;
979 req->out.vector_count = 4;
983 smb2_setup_nbt_length(req->out.vector,
984 req->out.vector_count);
986 /* Ensure our final reply matches the interim one. */
987 reqhdr = (uint8_t *)req->out.vector[1].iov_base;
988 SIVAL(reqhdr, SMB2_HDR_FLAGS, flags | SMB2_HDR_FLAG_ASYNC);
989 SBVAL(reqhdr, SMB2_HDR_PID, async_id);
992 const uint8_t *inhdr =
993 (const uint8_t *)req->in.vector[1].iov_base;
994 DEBUG(10,("smbd_smb2_request_pending_queue: opcode[%s] mid %llu "
996 smb2_opcode_name((uint16_t)IVAL(inhdr, SMB2_HDR_OPCODE)),
997 (unsigned long long)async_id ));
1002 static NTSTATUS smbd_smb2_request_process_cancel(struct smbd_smb2_request *req)
1004 struct smbd_server_connection *sconn = req->sconn;
1005 struct smbd_smb2_request *cur;
1006 const uint8_t *inhdr;
1007 int i = req->current_idx;
1009 uint64_t search_message_id;
1010 uint64_t search_async_id;
1013 inhdr = (const uint8_t *)req->in.vector[i].iov_base;
1015 flags = IVAL(inhdr, SMB2_HDR_FLAGS);
1016 search_message_id = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
1017 search_async_id = BVAL(inhdr, SMB2_HDR_PID);
1020 * we don't need the request anymore
1021 * cancel requests never have a response
1023 DLIST_REMOVE(req->sconn->smb2.requests, req);
1026 for (cur = sconn->smb2.requests; cur; cur = cur->next) {
1027 const uint8_t *outhdr;
1028 uint64_t message_id;
1031 i = cur->current_idx;
1033 outhdr = (const uint8_t *)cur->out.vector[i].iov_base;
1035 message_id = BVAL(outhdr, SMB2_HDR_MESSAGE_ID);
1036 async_id = BVAL(outhdr, SMB2_HDR_PID);
1038 if (flags & SMB2_HDR_FLAG_ASYNC) {
1039 if (search_async_id == async_id) {
1040 found_id = async_id;
1044 if (search_message_id == message_id) {
1045 found_id = message_id;
1051 if (cur && cur->subreq) {
1052 inhdr = (const uint8_t *)cur->in.vector[i].iov_base;
1053 DEBUG(10,("smbd_smb2_request_process_cancel: attempting to "
1054 "cancel opcode[%s] mid %llu\n",
1055 smb2_opcode_name((uint16_t)IVAL(inhdr, SMB2_HDR_OPCODE)),
1056 (unsigned long long)found_id ));
1057 tevent_req_cancel(cur->subreq);
1060 return NT_STATUS_OK;
1063 NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
1065 const uint8_t *inhdr;
1066 int i = req->current_idx;
1071 NTSTATUS session_status;
1072 uint32_t allowed_flags;
1073 NTSTATUS return_value;
1075 inhdr = (const uint8_t *)req->in.vector[i].iov_base;
1077 /* TODO: verify more things */
1079 flags = IVAL(inhdr, SMB2_HDR_FLAGS);
1080 opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
1081 mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
1082 DEBUG(10,("smbd_smb2_request_dispatch: opcode[%s] mid = %llu\n",
1083 smb2_opcode_name(opcode),
1084 (unsigned long long)mid));
1086 allowed_flags = SMB2_HDR_FLAG_CHAINED |
1087 SMB2_HDR_FLAG_SIGNED |
1089 if (opcode == SMB2_OP_CANCEL) {
1090 allowed_flags |= SMB2_HDR_FLAG_ASYNC;
1092 if ((flags & ~allowed_flags) != 0) {
1093 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
1096 session_status = smbd_smb2_request_check_session(req);
1098 req->do_signing = false;
1099 if (flags & SMB2_HDR_FLAG_SIGNED) {
1100 if (!NT_STATUS_IS_OK(session_status)) {
1101 return smbd_smb2_request_error(req, session_status);
1104 req->do_signing = true;
1105 status = smb2_signing_check_pdu(req->session->session_key,
1106 &req->in.vector[i], 3);
1107 if (!NT_STATUS_IS_OK(status)) {
1108 return smbd_smb2_request_error(req, status);
1110 } else if (req->session && req->session->do_signing) {
1111 return smbd_smb2_request_error(req, NT_STATUS_ACCESS_DENIED);
1114 if (flags & SMB2_HDR_FLAG_CHAINED) {
1116 * This check is mostly for giving the correct error code
1117 * for compounded requests.
1119 * TODO: we may need to move this after the session
1122 if (!NT_STATUS_IS_OK(req->next_status)) {
1123 return smbd_smb2_request_error(req, req->next_status);
1126 req->compat_chain_fsp = NULL;
1130 case SMB2_OP_NEGPROT:
1132 START_PROFILE(smb2_negprot);
1133 return_value = smbd_smb2_request_process_negprot(req);
1134 END_PROFILE(smb2_negprot);
1138 case SMB2_OP_SESSSETUP:
1140 START_PROFILE(smb2_sesssetup);
1141 return_value = smbd_smb2_request_process_sesssetup(req);
1142 END_PROFILE(smb2_sesssetup);
1146 case SMB2_OP_LOGOFF:
1147 if (!NT_STATUS_IS_OK(session_status)) {
1148 return_value = smbd_smb2_request_error(req, session_status);
1153 START_PROFILE(smb2_logoff);
1154 return_value = smbd_smb2_request_process_logoff(req);
1155 END_PROFILE(smb2_logoff);
1160 if (!NT_STATUS_IS_OK(session_status)) {
1161 return_value = smbd_smb2_request_error(req, session_status);
1164 status = smbd_smb2_request_check_session(req);
1165 if (!NT_STATUS_IS_OK(status)) {
1166 return_value = smbd_smb2_request_error(req, status);
1171 START_PROFILE(smb2_tcon);
1172 return_value = smbd_smb2_request_process_tcon(req);
1173 END_PROFILE(smb2_tcon);
1178 if (!NT_STATUS_IS_OK(session_status)) {
1179 return_value = smbd_smb2_request_error(req, session_status);
1182 status = smbd_smb2_request_check_tcon(req);
1183 if (!NT_STATUS_IS_OK(status)) {
1184 return_value = smbd_smb2_request_error(req, status);
1189 START_PROFILE(smb2_tdis);
1190 return_value = smbd_smb2_request_process_tdis(req);
1191 END_PROFILE(smb2_tdis);
1195 case SMB2_OP_CREATE:
1196 if (!NT_STATUS_IS_OK(session_status)) {
1197 return_value = smbd_smb2_request_error(req, session_status);
1200 status = smbd_smb2_request_check_tcon(req);
1201 if (!NT_STATUS_IS_OK(status)) {
1202 return_value = smbd_smb2_request_error(req, status);
1207 START_PROFILE(smb2_create);
1208 return_value = smbd_smb2_request_process_create(req);
1209 END_PROFILE(smb2_create);
1214 if (!NT_STATUS_IS_OK(session_status)) {
1215 return_value = smbd_smb2_request_error(req, session_status);
1218 status = smbd_smb2_request_check_tcon(req);
1219 if (!NT_STATUS_IS_OK(status)) {
1220 return_value = smbd_smb2_request_error(req, status);
1225 START_PROFILE(smb2_close);
1226 return_value = smbd_smb2_request_process_close(req);
1227 END_PROFILE(smb2_close);
1232 if (!NT_STATUS_IS_OK(session_status)) {
1233 return_value = smbd_smb2_request_error(req, session_status);
1236 status = smbd_smb2_request_check_tcon(req);
1237 if (!NT_STATUS_IS_OK(status)) {
1238 return_value = smbd_smb2_request_error(req, status);
1243 START_PROFILE(smb2_flush);
1244 return_value = smbd_smb2_request_process_flush(req);
1245 END_PROFILE(smb2_flush);
1250 if (!NT_STATUS_IS_OK(session_status)) {
1251 return_value = smbd_smb2_request_error(req, session_status);
1254 status = smbd_smb2_request_check_tcon(req);
1255 if (!NT_STATUS_IS_OK(status)) {
1256 return_value = smbd_smb2_request_error(req, status);
1261 START_PROFILE(smb2_read);
1262 return_value = smbd_smb2_request_process_read(req);
1263 END_PROFILE(smb2_read);
1268 if (!NT_STATUS_IS_OK(session_status)) {
1269 return_value = smbd_smb2_request_error(req, session_status);
1272 status = smbd_smb2_request_check_tcon(req);
1273 if (!NT_STATUS_IS_OK(status)) {
1274 return_value = smbd_smb2_request_error(req, status);
1279 START_PROFILE(smb2_write);
1280 return_value = smbd_smb2_request_process_write(req);
1281 END_PROFILE(smb2_write);
1286 if (!NT_STATUS_IS_OK(session_status)) {
1287 /* Too ugly to live ? JRA. */
1288 if (NT_STATUS_EQUAL(session_status,NT_STATUS_USER_SESSION_DELETED)) {
1289 session_status = NT_STATUS_FILE_CLOSED;
1291 return_value = smbd_smb2_request_error(req, session_status);
1294 status = smbd_smb2_request_check_tcon(req);
1295 if (!NT_STATUS_IS_OK(status)) {
1296 /* Too ugly to live ? JRA. */
1297 if (NT_STATUS_EQUAL(status,NT_STATUS_NETWORK_NAME_DELETED)) {
1298 status = NT_STATUS_FILE_CLOSED;
1300 return_value = smbd_smb2_request_error(req, status);
1305 START_PROFILE(smb2_lock);
1306 return_value = smbd_smb2_request_process_lock(req);
1307 END_PROFILE(smb2_lock);
1312 if (!NT_STATUS_IS_OK(session_status)) {
1313 return_value = smbd_smb2_request_error(req, session_status);
1316 status = smbd_smb2_request_check_tcon(req);
1317 if (!NT_STATUS_IS_OK(status)) {
1318 return_value = smbd_smb2_request_error(req, status);
1323 START_PROFILE(smb2_ioctl);
1324 return_value = smbd_smb2_request_process_ioctl(req);
1325 END_PROFILE(smb2_ioctl);
1329 case SMB2_OP_CANCEL:
1331 START_PROFILE(smb2_cancel);
1332 return_value = smbd_smb2_request_process_cancel(req);
1333 END_PROFILE(smb2_cancel);
1337 case SMB2_OP_KEEPALIVE:
1338 {START_PROFILE(smb2_keepalive);
1339 return_value = smbd_smb2_request_process_keepalive(req);
1340 END_PROFILE(smb2_keepalive);}
1344 if (!NT_STATUS_IS_OK(session_status)) {
1345 return_value = smbd_smb2_request_error(req, session_status);
1348 status = smbd_smb2_request_check_tcon(req);
1349 if (!NT_STATUS_IS_OK(status)) {
1350 return_value = smbd_smb2_request_error(req, status);
1355 START_PROFILE(smb2_find);
1356 return_value = smbd_smb2_request_process_find(req);
1357 END_PROFILE(smb2_find);
1361 case SMB2_OP_NOTIFY:
1362 if (!NT_STATUS_IS_OK(session_status)) {
1363 return_value = smbd_smb2_request_error(req, session_status);
1366 status = smbd_smb2_request_check_tcon(req);
1367 if (!NT_STATUS_IS_OK(status)) {
1368 return_value = smbd_smb2_request_error(req, status);
1373 START_PROFILE(smb2_notify);
1374 return_value = smbd_smb2_request_process_notify(req);
1375 END_PROFILE(smb2_notify);
1379 case SMB2_OP_GETINFO:
1380 if (!NT_STATUS_IS_OK(session_status)) {
1381 return_value = smbd_smb2_request_error(req, session_status);
1384 status = smbd_smb2_request_check_tcon(req);
1385 if (!NT_STATUS_IS_OK(status)) {
1386 return_value = smbd_smb2_request_error(req, status);
1391 START_PROFILE(smb2_getinfo);
1392 return_value = smbd_smb2_request_process_getinfo(req);
1393 END_PROFILE(smb2_getinfo);
1397 case SMB2_OP_SETINFO:
1398 if (!NT_STATUS_IS_OK(session_status)) {
1399 return_value = smbd_smb2_request_error(req, session_status);
1402 status = smbd_smb2_request_check_tcon(req);
1403 if (!NT_STATUS_IS_OK(status)) {
1404 return_value = smbd_smb2_request_error(req, status);
1409 START_PROFILE(smb2_setinfo);
1410 return_value = smbd_smb2_request_process_setinfo(req);
1411 END_PROFILE(smb2_setinfo);
1416 if (!NT_STATUS_IS_OK(session_status)) {
1417 return_value = smbd_smb2_request_error(req, session_status);
1420 status = smbd_smb2_request_check_tcon(req);
1421 if (!NT_STATUS_IS_OK(status)) {
1422 return_value = smbd_smb2_request_error(req, status);
1427 START_PROFILE(smb2_break);
1428 return_value = smbd_smb2_request_process_break(req);
1429 END_PROFILE(smb2_break);
1434 return_value = smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
1437 return return_value;
1440 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
1442 struct tevent_req *subreq;
1443 int i = req->current_idx;
1447 req->current_idx += 3;
1449 if (req->current_idx < req->out.vector_count) {
1451 * We must process the remaining compound
1452 * SMB2 requests before any new incoming SMB2
1453 * requests. This is because incoming SMB2
1454 * requests may include a cancel for a
1455 * compound request we haven't processed
1458 struct tevent_immediate *im = tevent_create_immediate(req);
1460 return NT_STATUS_NO_MEMORY;
1462 tevent_schedule_immediate(im,
1463 req->sconn->smb2.event_ctx,
1464 smbd_smb2_request_dispatch_immediate,
1466 return NT_STATUS_OK;
1469 smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
1471 /* Set credit for this operation (zero credits if this
1472 is a final reply for an async operation). */
1473 smb2_set_operation_credit(req->sconn,
1474 req->async ? NULL : &req->in.vector[i],
1475 &req->out.vector[i]);
1477 if (req->do_signing) {
1479 status = smb2_signing_sign_pdu(req->session->session_key,
1480 &req->out.vector[i], 3);
1481 if (!NT_STATUS_IS_OK(status)) {
1486 if (DEBUGLEVEL >= 10) {
1487 dbgtext("smbd_smb2_request_reply: sending...\n");
1488 print_req_vectors(req);
1491 /* I am a sick, sick man... :-). Sendfile hack ... JRA. */
1492 if (req->out.vector_count == 4 &&
1493 req->out.vector[3].iov_base == NULL &&
1494 req->out.vector[3].iov_len != 0) {
1495 /* Dynamic part is NULL. Chop it off,
1496 We're going to send it via sendfile. */
1497 req->out.vector_count -= 1;
1500 subreq = tstream_writev_queue_send(req,
1501 req->sconn->smb2.event_ctx,
1502 req->sconn->smb2.stream,
1503 req->sconn->smb2.send_queue,
1505 req->out.vector_count);
1506 if (subreq == NULL) {
1507 return NT_STATUS_NO_MEMORY;
1509 tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
1511 * We're done with this request -
1512 * move it off the "being processed" queue.
1514 DLIST_REMOVE(req->sconn->smb2.requests, req);
1516 return NT_STATUS_OK;
1519 void smbd_smb2_request_dispatch_immediate(struct tevent_context *ctx,
1520 struct tevent_immediate *im,
1523 struct smbd_smb2_request *req = talloc_get_type_abort(private_data,
1524 struct smbd_smb2_request);
1525 struct smbd_server_connection *sconn = req->sconn;
1530 if (DEBUGLEVEL >= 10) {
1531 DEBUG(10,("smbd_smb2_request_dispatch_immediate: idx[%d] of %d vectors\n",
1532 req->current_idx, req->in.vector_count));
1533 print_req_vectors(req);
1536 status = smbd_smb2_request_dispatch(req);
1537 if (!NT_STATUS_IS_OK(status)) {
1538 smbd_server_connection_terminate(sconn, nt_errstr(status));
1543 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
1545 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
1546 struct smbd_smb2_request);
1547 struct smbd_server_connection *sconn = req->sconn;
1551 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1552 TALLOC_FREE(subreq);
1555 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1556 DEBUG(2,("smbd_smb2_request_writev_done: client write error %s\n",
1557 nt_errstr(status)));
1558 smbd_server_connection_terminate(sconn, nt_errstr(status));
1563 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
1565 DATA_BLOB body, DATA_BLOB *dyn,
1566 const char *location)
1569 int i = req->current_idx;
1570 uint32_t next_command_ofs;
1572 DEBUG(10,("smbd_smb2_request_done_ex: "
1573 "idx[%d] status[%s] body[%u] dyn[%s:%u] at %s\n",
1574 i, nt_errstr(status), (unsigned int)body.length,
1576 (unsigned int)(dyn ? dyn->length : 0),
1579 if (body.length < 2) {
1580 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1583 if ((body.length % 2) != 0) {
1584 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1587 outhdr = (uint8_t *)req->out.vector[i].iov_base;
1589 next_command_ofs = IVAL(outhdr, SMB2_HDR_NEXT_COMMAND);
1590 SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
1592 req->out.vector[i+1].iov_base = (void *)body.data;
1593 req->out.vector[i+1].iov_len = body.length;
1596 req->out.vector[i+2].iov_base = (void *)dyn->data;
1597 req->out.vector[i+2].iov_len = dyn->length;
1599 req->out.vector[i+2].iov_base = NULL;
1600 req->out.vector[i+2].iov_len = 0;
1603 /* see if we need to recalculate the offset to the next response */
1604 if (next_command_ofs > 0) {
1605 next_command_ofs = SMB2_HDR_BODY;
1606 next_command_ofs += req->out.vector[i+1].iov_len;
1607 next_command_ofs += req->out.vector[i+2].iov_len;
1610 if ((next_command_ofs % 8) != 0) {
1611 size_t pad_size = 8 - (next_command_ofs % 8);
1612 if (req->out.vector[i+2].iov_len == 0) {
1614 * if the dyn buffer is empty
1615 * we can use it to add padding
1619 pad = talloc_zero_array(req->out.vector,
1622 return smbd_smb2_request_error(req,
1623 NT_STATUS_NO_MEMORY);
1626 req->out.vector[i+2].iov_base = (void *)pad;
1627 req->out.vector[i+2].iov_len = pad_size;
1630 * For now we copy the dynamic buffer
1631 * and add the padding to the new buffer
1638 old_size = req->out.vector[i+2].iov_len;
1639 old_dyn = (uint8_t *)req->out.vector[i+2].iov_base;
1641 new_size = old_size + pad_size;
1642 new_dyn = talloc_zero_array(req->out.vector,
1644 if (new_dyn == NULL) {
1645 return smbd_smb2_request_error(req,
1646 NT_STATUS_NO_MEMORY);
1649 memcpy(new_dyn, old_dyn, old_size);
1650 memset(new_dyn + old_size, 0, pad_size);
1652 req->out.vector[i+2].iov_base = (void *)new_dyn;
1653 req->out.vector[i+2].iov_len = new_size;
1655 next_command_ofs += pad_size;
1658 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
1660 return smbd_smb2_request_reply(req);
1663 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
1666 const char *location)
1669 int i = req->current_idx;
1670 uint8_t *outhdr = (uint8_t *)req->out.vector[i].iov_base;
1672 DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s] |%s| at %s\n",
1673 i, nt_errstr(status), info ? " +info" : "",
1676 body.data = outhdr + SMB2_HDR_BODY;
1678 SSVAL(body.data, 0, 9);
1681 SIVAL(body.data, 0x04, info->length);
1683 /* Allocated size of req->out.vector[i].iov_base
1684 * *MUST BE* OUTVEC_ALLOC_SIZE. So we have room for
1685 * 1 byte without having to do an alloc.
1687 info = talloc_zero_array(req->out.vector,
1691 return NT_STATUS_NO_MEMORY;
1693 info->data = ((uint8_t *)outhdr) +
1694 OUTVEC_ALLOC_SIZE - 1;
1696 SCVAL(info->data, 0, 0);
1700 * if a request fails, all other remaining
1701 * compounded requests should fail too
1703 req->next_status = NT_STATUS_INVALID_PARAMETER;
1705 return smbd_smb2_request_done_ex(req, status, body, info, __location__);
1709 struct smbd_smb2_send_oplock_break_state {
1710 struct smbd_server_connection *sconn;
1711 uint8_t buf[4 + SMB2_HDR_BODY + 0x18];
1712 struct iovec vector;
1715 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq);
1717 NTSTATUS smbd_smb2_send_oplock_break(struct smbd_server_connection *sconn,
1718 uint64_t file_id_persistent,
1719 uint64_t file_id_volatile,
1720 uint8_t oplock_level)
1722 struct smbd_smb2_send_oplock_break_state *state;
1723 struct tevent_req *subreq;
1727 state = talloc(sconn, struct smbd_smb2_send_oplock_break_state);
1728 if (state == NULL) {
1729 return NT_STATUS_NO_MEMORY;
1731 state->sconn = sconn;
1733 state->vector.iov_base = (void *)state->buf;
1734 state->vector.iov_len = sizeof(state->buf);
1736 _smb2_setlen(state->buf, sizeof(state->buf) - 4);
1737 hdr = state->buf + 4;
1738 body = hdr + SMB2_HDR_BODY;
1740 SIVAL(hdr, 0, SMB2_MAGIC);
1741 SSVAL(hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
1742 SSVAL(hdr, SMB2_HDR_EPOCH, 0);
1743 SIVAL(hdr, SMB2_HDR_STATUS, 0);
1744 SSVAL(hdr, SMB2_HDR_OPCODE, SMB2_OP_BREAK);
1745 SSVAL(hdr, SMB2_HDR_CREDIT, 0);
1746 SIVAL(hdr, SMB2_HDR_FLAGS, SMB2_HDR_FLAG_REDIRECT);
1747 SIVAL(hdr, SMB2_HDR_NEXT_COMMAND, 0);
1748 SBVAL(hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
1749 SIVAL(hdr, SMB2_HDR_PID, 0);
1750 SIVAL(hdr, SMB2_HDR_TID, 0);
1751 SBVAL(hdr, SMB2_HDR_SESSION_ID, 0);
1752 memset(hdr+SMB2_HDR_SIGNATURE, 0, 16);
1754 SSVAL(body, 0x00, 0x18);
1756 SCVAL(body, 0x02, oplock_level);
1757 SCVAL(body, 0x03, 0); /* reserved */
1758 SIVAL(body, 0x04, 0); /* reserved */
1759 SBVAL(body, 0x08, file_id_persistent);
1760 SBVAL(body, 0x10, file_id_volatile);
1762 subreq = tstream_writev_queue_send(state,
1763 sconn->smb2.event_ctx,
1765 sconn->smb2.send_queue,
1767 if (subreq == NULL) {
1768 return NT_STATUS_NO_MEMORY;
1770 tevent_req_set_callback(subreq,
1771 smbd_smb2_oplock_break_writev_done,
1774 return NT_STATUS_OK;
1777 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq)
1779 struct smbd_smb2_send_oplock_break_state *state =
1780 tevent_req_callback_data(subreq,
1781 struct smbd_smb2_send_oplock_break_state);
1782 struct smbd_server_connection *sconn = state->sconn;
1786 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1787 TALLOC_FREE(subreq);
1789 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1790 smbd_server_connection_terminate(sconn, nt_errstr(status));
1797 struct smbd_smb2_request_read_state {
1799 bool asked_for_header;
1800 struct smbd_smb2_request *smb2_req;
1803 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1805 TALLOC_CTX *mem_ctx,
1806 struct iovec **_vector,
1808 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
1810 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
1811 struct tevent_context *ev,
1812 struct smbd_server_connection *sconn)
1814 struct tevent_req *req;
1815 struct smbd_smb2_request_read_state *state;
1816 struct tevent_req *subreq;
1818 req = tevent_req_create(mem_ctx, &state,
1819 struct smbd_smb2_request_read_state);
1824 state->asked_for_header = false;
1826 state->smb2_req = smbd_smb2_request_allocate(state);
1827 if (tevent_req_nomem(state->smb2_req, req)) {
1828 return tevent_req_post(req, ev);
1830 state->smb2_req->sconn = sconn;
1832 subreq = tstream_readv_pdu_queue_send(state, ev, sconn->smb2.stream,
1833 sconn->smb2.recv_queue,
1834 smbd_smb2_request_next_vector,
1836 if (tevent_req_nomem(subreq, req)) {
1837 return tevent_req_post(req, ev);
1839 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
1844 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1846 TALLOC_CTX *mem_ctx,
1847 struct iovec **_vector,
1850 struct smbd_smb2_request_read_state *state =
1851 talloc_get_type_abort(private_data,
1852 struct smbd_smb2_request_read_state);
1853 struct smbd_smb2_request *req = state->smb2_req;
1854 struct iovec *vector;
1855 int idx = req->in.vector_count;
1857 uint8_t *buf = NULL;
1859 if (req->in.vector_count == 0) {
1861 * first we need to get the NBT header
1863 req->in.vector = talloc_array(req, struct iovec,
1864 req->in.vector_count + 1);
1865 if (req->in.vector == NULL) {
1868 req->in.vector_count += 1;
1870 req->in.vector[idx].iov_base = (void *)req->in.nbt_hdr;
1871 req->in.vector[idx].iov_len = 4;
1873 vector = talloc_array(mem_ctx, struct iovec, 1);
1874 if (vector == NULL) {
1878 vector[0] = req->in.vector[idx];
1885 if (req->in.vector_count == 1) {
1887 * Now we analyze the NBT header
1889 state->missing = smb2_len(req->in.vector[0].iov_base);
1891 if (state->missing == 0) {
1892 /* if there're no remaining bytes, we're done */
1898 req->in.vector = talloc_realloc(req, req->in.vector,
1900 req->in.vector_count + 1);
1901 if (req->in.vector == NULL) {
1904 req->in.vector_count += 1;
1906 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
1908 * it's a special NBT message,
1909 * so get all remaining bytes
1911 len = state->missing;
1912 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
1914 * it's an invalid message, just read what we can get
1915 * and let the caller handle the error
1917 len = state->missing;
1920 * We assume it's a SMB2 request,
1921 * and we first get the header and the
1922 * first 2 bytes (the struct size) of the body
1924 len = SMB2_HDR_BODY + 2;
1926 state->asked_for_header = true;
1929 state->missing -= len;
1931 buf = talloc_array(req->in.vector, uint8_t, len);
1936 req->in.vector[idx].iov_base = (void *)buf;
1937 req->in.vector[idx].iov_len = len;
1939 vector = talloc_array(mem_ctx, struct iovec, 1);
1940 if (vector == NULL) {
1944 vector[0] = req->in.vector[idx];
1951 if (state->missing == 0) {
1952 /* if there're no remaining bytes, we're done */
1958 if (state->asked_for_header) {
1961 size_t next_command_ofs;
1966 bool invalid = false;
1968 state->asked_for_header = false;
1971 * We got the SMB2 header and the first 2 bytes
1972 * of the body. We fix the size to just the header
1973 * and manually copy the 2 first bytes to the body section
1975 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
1976 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
1978 /* allocate vectors for body and dynamic areas */
1979 req->in.vector = talloc_realloc(req, req->in.vector,
1981 req->in.vector_count + 2);
1982 if (req->in.vector == NULL) {
1985 req->in.vector_count += 2;
1987 full_size = state->missing + SMB2_HDR_BODY + 2;
1988 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
1989 body_size = SVAL(hdr, SMB2_HDR_BODY);
1991 if (next_command_ofs != 0) {
1992 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
1994 * this is invalid, just return a zero
1995 * body and let the caller deal with the error
1998 } else if (next_command_ofs > full_size) {
2000 * this is invalid, just return a zero
2001 * body and let the caller deal with the error
2005 full_size = next_command_ofs;
2010 if (body_size < 2) {
2012 * this is invalid, just return a zero
2013 * body and let the caller deal with the error
2018 if ((body_size % 2) != 0) {
2022 if (body_size > (full_size - SMB2_HDR_BODY)) {
2024 * this is invalid, just return a zero
2025 * body and let the caller deal with the error
2032 /* the caller should check this */
2036 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
2038 state->missing -= (body_size - 2) + dyn_size;
2040 body = talloc_array(req->in.vector, uint8_t, body_size);
2045 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
2050 req->in.vector[idx].iov_base = (void *)body;
2051 req->in.vector[idx].iov_len = body_size;
2052 req->in.vector[idx+1].iov_base = (void *)dyn;
2053 req->in.vector[idx+1].iov_len = dyn_size;
2055 vector = talloc_array(mem_ctx, struct iovec, 2);
2056 if (vector == NULL) {
2061 * the first 2 bytes of the body were already fetched
2062 * together with the header
2064 memcpy(body, hdr + SMB2_HDR_BODY, 2);
2065 vector[0].iov_base = body + 2;
2066 vector[0].iov_len = body_size - 2;
2068 vector[1] = req->in.vector[idx+1];
2076 * when we endup here, we're looking for a new SMB2 request
2077 * next. And we ask for its header and the first 2 bytes of
2078 * the body (like we did for the first SMB2 request).
2081 req->in.vector = talloc_realloc(req, req->in.vector,
2083 req->in.vector_count + 1);
2084 if (req->in.vector == NULL) {
2087 req->in.vector_count += 1;
2090 * We assume it's a SMB2 request,
2091 * and we first get the header and the
2092 * first 2 bytes (the struct size) of the body
2094 len = SMB2_HDR_BODY + 2;
2096 if (len > state->missing) {
2097 /* let the caller handle the error */
2098 len = state->missing;
2101 state->missing -= len;
2102 state->asked_for_header = true;
2104 buf = talloc_array(req->in.vector, uint8_t, len);
2109 req->in.vector[idx].iov_base = (void *)buf;
2110 req->in.vector[idx].iov_len = len;
2112 vector = talloc_array(mem_ctx, struct iovec, 1);
2113 if (vector == NULL) {
2117 vector[0] = req->in.vector[idx];
2124 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
2126 struct tevent_req *req =
2127 tevent_req_callback_data(subreq,
2133 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
2135 status = map_nt_error_from_unix(sys_errno);
2136 tevent_req_nterror(req, status);
2140 tevent_req_done(req);
2143 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
2144 TALLOC_CTX *mem_ctx,
2145 struct smbd_smb2_request **_smb2_req)
2147 struct smbd_smb2_request_read_state *state =
2148 tevent_req_data(req,
2149 struct smbd_smb2_request_read_state);
2152 if (tevent_req_is_nterror(req, &status)) {
2153 tevent_req_received(req);
2157 talloc_steal(mem_ctx, state->smb2_req->mem_pool);
2158 *_smb2_req = state->smb2_req;
2159 tevent_req_received(req);
2160 return NT_STATUS_OK;
2163 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
2165 void smbd_smb2_first_negprot(struct smbd_server_connection *sconn,
2166 const uint8_t *inbuf, size_t size)
2169 struct smbd_smb2_request *req = NULL;
2170 struct tevent_req *subreq;
2172 if (lp_security() == SEC_SHARE) {
2173 DEBUG(2,("WARNING!!: \"security = share\" is deprecated for "
2174 "SMB2 servers. Mapping to \"security = user\" and "
2175 "\"map to guest = Bad User\"\n" ));
2176 lp_do_parameter(-1, "security", "user");
2177 lp_do_parameter(-1, "map to guest", "Bad User");
2180 DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
2181 (unsigned int)size));
2183 status = smbd_initialize_smb2(sconn);
2184 if (!NT_STATUS_IS_OK(status)) {
2185 smbd_server_connection_terminate(sconn, nt_errstr(status));
2189 status = smbd_smb2_request_create(sconn, inbuf, size, &req);
2190 if (!NT_STATUS_IS_OK(status)) {
2191 smbd_server_connection_terminate(sconn, nt_errstr(status));
2195 status = smbd_smb2_request_setup_out(req);
2196 if (!NT_STATUS_IS_OK(status)) {
2197 smbd_server_connection_terminate(sconn, nt_errstr(status));
2201 status = smbd_smb2_request_dispatch(req);
2202 if (!NT_STATUS_IS_OK(status)) {
2203 smbd_server_connection_terminate(sconn, nt_errstr(status));
2207 /* ask for the next request */
2208 subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
2209 if (subreq == NULL) {
2210 smbd_server_connection_terminate(sconn, "no memory for reading");
2213 tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);
2216 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
2218 struct smbd_server_connection *sconn = tevent_req_callback_data(subreq,
2219 struct smbd_server_connection);
2221 struct smbd_smb2_request *req = NULL;
2223 status = smbd_smb2_request_read_recv(subreq, sconn, &req);
2224 TALLOC_FREE(subreq);
2225 if (!NT_STATUS_IS_OK(status)) {
2226 DEBUG(2,("smbd_smb2_request_incoming: client read error %s\n",
2227 nt_errstr(status)));
2228 smbd_server_connection_terminate(sconn, nt_errstr(status));
2232 if (req->in.nbt_hdr[0] != 0x00) {
2233 DEBUG(1,("smbd_smb2_request_incoming: ignore NBT[0x%02X] msg\n",
2234 req->in.nbt_hdr[0]));
2239 req->current_idx = 1;
2241 DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
2242 req->current_idx, req->in.vector_count));
2244 status = smbd_smb2_request_validate(req);
2245 if (!NT_STATUS_IS_OK(status)) {
2246 smbd_server_connection_terminate(sconn, nt_errstr(status));
2250 status = smbd_smb2_request_setup_out(req);
2251 if (!NT_STATUS_IS_OK(status)) {
2252 smbd_server_connection_terminate(sconn, nt_errstr(status));
2256 status = smbd_smb2_request_dispatch(req);
2257 if (!NT_STATUS_IS_OK(status)) {
2258 smbd_server_connection_terminate(sconn, nt_errstr(status));
2263 /* ask for the next request (this constructs the main loop) */
2264 subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
2265 if (subreq == NULL) {
2266 smbd_server_connection_terminate(sconn, "no memory for reading");
2269 tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);