2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
24 #include "../lib/tsocket/tsocket.h"
26 bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size)
28 if (size < (4 + SMB2_HDR_BODY)) {
32 if (IVAL(inbuf, 4) != SMB2_MAGIC) {
39 static NTSTATUS smbd_initialize_smb2(struct smbd_server_connection *conn)
44 TALLOC_FREE(conn->fde);
46 conn->smb2.event_ctx = smbd_event_context();
48 conn->smb2.recv_queue = tevent_queue_create(conn, "smb2 recv queue");
49 if (conn->smb2.recv_queue == NULL) {
50 return NT_STATUS_NO_MEMORY;
53 conn->smb2.send_queue = tevent_queue_create(conn, "smb2 send queue");
54 if (conn->smb2.send_queue == NULL) {
55 return NT_STATUS_NO_MEMORY;
58 ret = tstream_bsd_existing_socket(conn, smbd_server_fd(),
61 status = map_nt_error_from_unix(errno);
65 /* Ensure child is set to non-blocking mode */
66 set_blocking(smbd_server_fd(),false);
70 #define smb2_len(buf) (PVAL(buf,3)|(PVAL(buf,2)<<8)|(PVAL(buf,1)<<16))
71 #define _smb2_setlen(_buf,len) do { \
72 uint8_t *buf = (uint8_t *)_buf; \
74 buf[1] = ((len)&0xFF0000)>>16; \
75 buf[2] = ((len)&0xFF00)>>8; \
76 buf[3] = (len)&0xFF; \
79 static void smb2_setup_nbt_length(struct iovec *vector, int count)
84 for (i=1; i < count; i++) {
85 len += vector[i].iov_len;
88 _smb2_setlen(vector[0].iov_base, len);
91 static NTSTATUS smbd_smb2_request_create(struct smbd_server_connection *conn,
92 const uint8_t *inbuf, size_t size,
93 struct smbd_smb2_request **_req)
96 struct smbd_smb2_request *req;
97 uint32_t protocol_version;
98 const uint8_t *inhdr = NULL;
101 uint32_t next_command_ofs;
103 if (size < (4 + SMB2_HDR_BODY + 2)) {
104 DEBUG(0,("Invalid SMB2 packet length count %ld\n", (long)size));
105 return NT_STATUS_INVALID_PARAMETER;
110 protocol_version = IVAL(inhdr, SMB2_HDR_PROTOCOL_ID);
111 if (protocol_version != SMB2_MAGIC) {
112 DEBUG(0,("Invalid SMB packet: protocol prefix: 0x%08X\n",
114 return NT_STATUS_INVALID_PARAMETER;
117 cmd = SVAL(inhdr, SMB2_HDR_OPCODE);
118 if (cmd != SMB2_OP_NEGPROT) {
119 DEBUG(0,("Invalid SMB packet: first request: 0x%04X\n",
121 return NT_STATUS_INVALID_PARAMETER;
124 next_command_ofs = IVAL(inhdr, SMB2_HDR_NEXT_COMMAND);
125 if (next_command_ofs != 0) {
126 DEBUG(0,("Invalid SMB packet: next_command: 0x%08X\n",
128 return NT_STATUS_INVALID_PARAMETER;
131 mem_pool = talloc_pool(conn, 8192);
132 if (mem_pool == NULL) {
133 return NT_STATUS_NO_MEMORY;
136 req = talloc_zero(mem_pool, struct smbd_smb2_request);
138 talloc_free(mem_pool);
139 return NT_STATUS_NO_MEMORY;
141 req->mem_pool = mem_pool;
144 talloc_steal(req, inbuf);
146 req->in.vector = talloc_array(req, struct iovec, 4);
147 if (req->in.vector == NULL) {
148 talloc_free(mem_pool);
149 return NT_STATUS_NO_MEMORY;
151 req->in.vector_count = 4;
153 memcpy(req->in.nbt_hdr, inbuf, 4);
156 req->in.vector[0].iov_base = (void *)req->in.nbt_hdr;
157 req->in.vector[0].iov_len = 4;
158 ofs += req->in.vector[0].iov_len;
160 req->in.vector[1].iov_base = (void *)(inbuf + ofs);
161 req->in.vector[1].iov_len = SMB2_HDR_BODY;
162 ofs += req->in.vector[1].iov_len;
164 req->in.vector[2].iov_base = (void *)(inbuf + ofs);
165 req->in.vector[2].iov_len = SVAL(inbuf, ofs) & 0xFFFE;
166 ofs += req->in.vector[2].iov_len;
169 return NT_STATUS_INVALID_PARAMETER;
172 req->in.vector[3].iov_base = (void *)(inbuf + ofs);
173 req->in.vector[3].iov_len = size - ofs;
174 ofs += req->in.vector[3].iov_len;
176 req->current_idx = 1;
182 static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req)
184 struct iovec *vector;
188 count = req->in.vector_count;
189 vector = talloc_array(req, struct iovec, count);
190 if (vector == NULL) {
191 return NT_STATUS_NO_MEMORY;
194 vector[0].iov_base = req->out.nbt_hdr;
195 vector[0].iov_len = 4;
196 SIVAL(req->out.nbt_hdr, 0, 0);
198 for (idx=1; idx < count; idx += 3) {
199 const uint8_t *inhdr = NULL;
200 uint8_t *outhdr = NULL;
201 uint8_t *outbody = NULL;
202 uint8_t *outdyn = NULL;
203 size_t outdyn_size = 1;
204 uint32_t next_command_ofs = 0;
205 struct iovec *current = &vector[idx];
207 if ((idx + 3) < count) {
208 /* we have a next command */
209 next_command_ofs = SMB2_HDR_BODY + 8 + 8;
213 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
215 outhdr = talloc_array(vector, uint8_t,
216 SMB2_HDR_BODY + 8 + outdyn_size);
217 if (outhdr == NULL) {
218 return NT_STATUS_NO_MEMORY;
221 outbody = outhdr + SMB2_HDR_BODY;
222 outdyn = outbody + 8;
224 current[0].iov_base = (void *)outhdr;
225 current[0].iov_len = SMB2_HDR_BODY;
227 current[1].iov_base = (void *)outbody;
228 current[1].iov_len = 8;
230 current[2].iov_base = (void *)outdyn;
231 current[2].iov_len = outdyn_size;
233 /* setup the SMB2 header */
234 SIVAL(outhdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
235 SSVAL(outhdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
236 SSVAL(outhdr, SMB2_HDR_EPOCH, 0);
237 SIVAL(outhdr, SMB2_HDR_STATUS,
238 NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));
239 SSVAL(outhdr, SMB2_HDR_OPCODE,
240 SVAL(inhdr, SMB2_HDR_OPCODE));
241 SSVAL(outhdr, SMB2_HDR_CREDIT, 0);
242 SIVAL(outhdr, SMB2_HDR_FLAGS, SMB2_HDR_FLAG_REDIRECT);
243 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
244 SBVAL(outhdr, SMB2_HDR_MESSAGE_ID,
245 BVAL(inhdr, SMB2_HDR_MESSAGE_ID));
246 SIVAL(outhdr, SMB2_HDR_PID,
247 IVAL(inhdr, SMB2_HDR_PID));
248 SIVAL(outhdr, SMB2_HDR_TID,
249 IVAL(inhdr, SMB2_HDR_TID));
250 SBVAL(outhdr, SMB2_HDR_SESSION_ID,
251 BVAL(inhdr, SMB2_HDR_SESSION_ID));
252 memset(outhdr + SMB2_HDR_SIGNATURE, 0, 16);
254 /* setup error body header */
255 SSVAL(outbody, 0x00, 9);
256 SSVAL(outbody, 0x02, 0);
257 SIVAL(outbody, 0x04, 0);
259 /* setup the dynamic part */
260 SCVAL(outdyn, 0x00, 0);
263 req->out.vector = vector;
264 req->out.vector_count = count;
266 /* setup the length of the NBT packet */
267 smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
272 static void smbd_server_connection_terminate(struct smbd_server_connection *conn,
275 DEBUG(10,("smbd_server_connection_terminate: reason[%s]\n", reason));
276 exit_server_cleanly(reason);
279 static NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
281 const uint8_t *inhdr;
282 int i = req->current_idx;
285 inhdr = (const uint8_t *)req->in.vector[i].iov_base;
287 /* TODO: verify more things */
289 opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
290 DEBUG(10,("smbd_smb2_request_dispatch: opcode[%u]\n", opcode));
292 case SMB2_OP_NEGPROT:
293 return smbd_smb2_request_process_negprot(req);
295 case SMB2_OP_SESSSETUP:
296 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
299 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
301 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
303 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
305 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
307 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
310 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
313 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
316 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
319 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
322 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
325 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
328 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
331 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
333 case SMB2_OP_KEEPALIVE:
334 return smbd_smb2_request_process_keepalive(req);
337 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
340 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
342 case SMB2_OP_GETINFO:
343 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
345 case SMB2_OP_SETINFO:
346 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
349 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
352 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
355 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq);
356 static void smbd_smb2_request_writev_done(struct tevent_req *subreq);
358 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
360 struct tevent_req *subreq;
362 /* TODO: sign the response here */
364 smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
366 req->current_idx += 3;
368 if (req->current_idx > req->in.vector_count) {
369 struct timeval zero = timeval_zero();
370 subreq = tevent_wakeup_send(req,
371 req->conn->smb2.event_ctx,
373 if (subreq == NULL) {
374 return NT_STATUS_NO_MEMORY;
376 tevent_req_set_callback(subreq,
377 smbd_smb2_request_dispatch_compound,
383 subreq = tstream_writev_queue_send(req,
384 req->conn->smb2.event_ctx,
385 req->conn->smb2.stream,
386 req->conn->smb2.send_queue,
388 req->out.vector_count);
389 if (subreq == NULL) {
390 return NT_STATUS_NO_MEMORY;
392 tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
397 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq)
399 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
400 struct smbd_smb2_request);
401 struct smbd_server_connection *conn = req->conn;
404 tevent_wakeup_recv(subreq);
407 DEBUG(10,("smbd_smb2_request_dispatch_compound: idx[%d] of %d vectors\n",
408 req->current_idx, req->in.vector_count));
410 status = smbd_smb2_request_dispatch(req);
411 if (!NT_STATUS_IS_OK(status)) {
412 smbd_server_connection_terminate(conn, nt_errstr(status));
417 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
419 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
420 struct smbd_smb2_request);
421 struct smbd_server_connection *conn = req->conn;
424 TALLOC_CTX *mem_pool;
426 ret = tstream_writev_queue_recv(subreq, &sys_errno);
429 NTSTATUS status = map_nt_error_from_unix(sys_errno);
430 smbd_server_connection_terminate(conn, nt_errstr(status));
434 mem_pool = req->mem_pool;
436 talloc_free(mem_pool);
439 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
445 int i = req->current_idx;
447 DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s]%s\n",
448 i, nt_errstr(status), info ? " +info" : ""));
450 outhdr = (uint8_t *)req->out.vector[i].iov_base;
452 SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
454 outbody = outhdr + SMB2_HDR_BODY;
456 req->out.vector[i+1].iov_base = (void *)outbody;
457 req->out.vector[i+1].iov_len = 8;
460 SIVAL(outbody, 0x04, info->length);
461 req->out.vector[i+2].iov_base = (void *)info->data;
462 req->out.vector[i+2].iov_len = info->length;
464 req->out.vector[i+2].iov_base = (void *)(outbody + 8);
465 req->out.vector[i+2].iov_len = 1;
468 /* the error packet is the last response in the chain */
469 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
470 req->out.vector_count = req->current_idx + 3;
472 return smbd_smb2_request_reply(req);
475 NTSTATUS smbd_smb2_request_error(struct smbd_smb2_request *req,
478 return smbd_smb2_request_error_ex(req, status, NULL);
481 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
483 DATA_BLOB body, DATA_BLOB *dyn)
487 int i = req->current_idx;
488 uint32_t next_command_ofs;
490 DEBUG(10,("smbd_smb2_request_done_ex: "
491 "idx[%d] status[%s] body[%u] dyn[%s:%u]\n",
492 i, nt_errstr(status), (unsigned int)body.length,
494 (unsigned int)(dyn ? dyn->length : 0)));
496 if (body.length < 2) {
497 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
500 if ((body.length % 2) != 0) {
501 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
504 outhdr = (uint8_t *)req->out.vector[i].iov_base;
505 /* the fallback dynamic buffer */
506 outdyn = outhdr + SMB2_HDR_BODY + 8;
508 next_command_ofs = SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
509 SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
511 req->out.vector[i+1].iov_base = (void *)body.data;
512 req->out.vector[i+1].iov_len = body.length;
515 if (dyn->length > 0) {
516 req->out.vector[i+2].iov_base = (void *)dyn->data;
517 req->out.vector[i+2].iov_len = dyn->length;
519 req->out.vector[i+2].iov_base = (void *)outdyn;
520 req->out.vector[i+2].iov_len = 1;
523 req->out.vector[i+2].iov_base = NULL;
524 req->out.vector[i+2].iov_len = 0;
527 /* see if we need to recalculate the offset to the next response */
528 if (next_command_ofs > 0) {
529 next_command_ofs = SMB2_HDR_BODY;
530 next_command_ofs += req->out.vector[i+1].iov_len;
531 next_command_ofs += req->out.vector[i+2].iov_len;
534 /* TODO: we need to add padding ... */
535 if ((next_command_ofs % 8) != 0) {
536 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
539 /* the error packet is the last response in the chain */
540 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
542 return smbd_smb2_request_reply(req);
545 NTSTATUS smbd_smb2_request_done(struct smbd_smb2_request *req,
546 DATA_BLOB body, DATA_BLOB *dyn)
548 return smbd_smb2_request_done_ex(req, NT_STATUS_OK, body, dyn);
551 struct smbd_smb2_request_read_state {
553 bool asked_for_header;
554 struct smbd_smb2_request *smb2_req;
557 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
560 struct iovec **_vector,
562 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
564 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
565 struct tevent_context *ev,
566 struct smbd_server_connection *conn)
568 struct tevent_req *req;
569 struct smbd_smb2_request_read_state *state;
570 struct tevent_req *subreq;
571 TALLOC_CTX *mem_pool;
573 req = tevent_req_create(mem_ctx, &state,
574 struct smbd_smb2_request_read_state);
579 state->asked_for_header = false;
581 mem_pool = talloc_pool(state, 8192);
582 if (tevent_req_nomem(mem_pool, req)) {
583 return tevent_req_post(req, ev);
586 state->smb2_req = talloc_zero(mem_pool, struct smbd_smb2_request);
587 if (tevent_req_nomem(state->smb2_req, req)) {
588 return tevent_req_post(req, ev);
591 state->smb2_req->mem_pool = mem_pool;
592 state->smb2_req->conn = conn;
594 subreq = tstream_readv_pdu_queue_send(state, ev, conn->smb2.stream,
595 conn->smb2.recv_queue,
596 smbd_smb2_request_next_vector,
598 if (tevent_req_nomem(subreq, req)) {
599 return tevent_req_post(req, ev);
601 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
606 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
609 struct iovec **_vector,
612 struct smbd_smb2_request_read_state *state =
613 talloc_get_type_abort(private_data,
614 struct smbd_smb2_request_read_state);
615 struct smbd_smb2_request *req = state->smb2_req;
616 struct iovec *vector;
617 int idx = req->in.vector_count;
621 if (req->in.vector_count == 0) {
623 * first we need to get the NBT header
625 req->in.vector = talloc_array(req, struct iovec,
626 req->in.vector_count + 1);
627 if (req->in.vector == NULL) {
630 req->in.vector_count += 1;
632 req->in.vector[idx].iov_base = (void *)req->in.nbt_hdr;
633 req->in.vector[idx].iov_len = 4;
635 vector = talloc_array(mem_ctx, struct iovec, 1);
636 if (vector == NULL) {
640 vector[0] = req->in.vector[idx];
647 if (req->in.vector_count == 1) {
649 * Now we analyze the NBT header
651 state->missing = smb2_len(req->in.vector[0].iov_base);
653 if (state->missing == 0) {
654 /* if there're no remaining bytes, we're done */
660 req->in.vector = talloc_realloc(req, req->in.vector,
662 req->in.vector_count + 1);
663 if (req->in.vector == NULL) {
666 req->in.vector_count += 1;
668 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
670 * it's a special NBT message,
671 * so get all remaining bytes
673 len = state->missing;
674 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
676 * it's an invalid message, just read what we can get
677 * and let the caller handle the error
679 len = state->missing;
682 * We assume it's a SMB2 request,
683 * and we first get the header and the
684 * first 2 bytes (the struct size) of the body
686 len = SMB2_HDR_BODY + 2;
688 state->asked_for_header = true;
691 state->missing -= len;
693 buf = talloc_array(req->in.vector, uint8_t, len);
698 req->in.vector[idx].iov_base = (void *)buf;
699 req->in.vector[idx].iov_len = len;
701 vector = talloc_array(mem_ctx, struct iovec, 1);
702 if (vector == NULL) {
706 vector[0] = req->in.vector[idx];
713 if (state->missing == 0) {
714 /* if there're no remaining bytes, we're done */
720 if (state->asked_for_header) {
723 size_t next_command_ofs;
728 bool invalid = false;
730 state->asked_for_header = false;
733 * We got the SMB2 header and the first 2 bytes
734 * of the body. We fix the size to just the header
735 * and manually copy the 2 first bytes to the body section
737 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
738 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
740 /* allocate vectors for body and dynamic areas */
741 req->in.vector = talloc_realloc(req, req->in.vector,
743 req->in.vector_count + 2);
744 if (req->in.vector == NULL) {
747 req->in.vector_count += 2;
749 full_size = state->missing + SMB2_HDR_BODY + 2;
750 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
751 body_size = SVAL(hdr, SMB2_HDR_BODY);
753 if (next_command_ofs != 0) {
754 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
756 * this is invalid, just return a zero
757 * body and let the caller deal with the error
760 } else if (next_command_ofs > full_size) {
762 * this is invalid, just return a zero
763 * body and let the caller deal with the error
767 full_size = next_command_ofs;
774 * this is invalid, just return a zero
775 * body and let the caller deal with the error
778 } else if (body_size > (full_size - SMB2_HDR_BODY)) {
780 * this is invalid, just return a zero
781 * body and let the caller deal with the error
788 /* the caller should check this */
792 if ((body_size % 2) != 0) {
796 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
798 state->missing -= (body_size - 2) + dyn_size;
800 body = talloc_array(req->in.vector, uint8_t, body_size);
805 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
810 req->in.vector[idx].iov_base = (void *)body;
811 req->in.vector[idx].iov_len = body_size;
812 req->in.vector[idx+1].iov_base = (void *)dyn;
813 req->in.vector[idx+1].iov_len = dyn_size;
815 vector = talloc_array(mem_ctx, struct iovec, 2);
816 if (vector == NULL) {
821 * the first 2 bytes of the body were already fetched
822 * together with the header
824 memcpy(body, hdr + SMB2_HDR_BODY, 2);
825 vector[0].iov_base = body + 2;
826 vector[0].iov_len = req->in.vector[idx].iov_len - 2;
828 vector[1] = req->in.vector[idx+1];
836 * when we endup here, we're looking for a new SMB2 request
837 * next. And we ask for its header and the first 2 bytes of
838 * the body (like we did for the first SMB2 request).
841 req->in.vector = talloc_realloc(req, req->in.vector,
843 req->in.vector_count + 1);
844 if (req->in.vector == NULL) {
847 req->in.vector_count += 1;
850 * We assume it's a SMB2 request,
851 * and we first get the header and the
852 * first 2 bytes (the struct size) of the body
854 len = SMB2_HDR_BODY + 2;
856 if (len > state->missing) {
857 /* let the caller handle the error */
858 len = state->missing;
861 state->missing -= len;
862 state->asked_for_header = true;
864 buf = talloc_array(req->in.vector, uint8_t, len);
869 req->in.vector[idx].iov_base = (void *)buf;
870 req->in.vector[idx].iov_len = len;
872 vector = talloc_array(mem_ctx, struct iovec, 1);
873 if (vector == NULL) {
877 vector[0] = req->in.vector[idx];
884 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
886 struct tevent_req *req =
887 tevent_req_callback_data(subreq,
893 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
895 status = map_nt_error_from_unix(sys_errno);
896 tevent_req_nterror(req, status);
900 tevent_req_done(req);
903 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
905 struct smbd_smb2_request **_smb2_req)
907 struct smbd_smb2_request_read_state *state =
909 struct smbd_smb2_request_read_state);
912 if (tevent_req_is_nterror(req, &status)) {
913 tevent_req_received(req);
917 talloc_steal(mem_ctx, state->smb2_req->mem_pool);
918 *_smb2_req = state->smb2_req;
919 tevent_req_received(req);
923 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
925 void smbd_smb2_first_negprot(struct smbd_server_connection *conn,
926 const uint8_t *inbuf, size_t size)
929 struct smbd_smb2_request *req;
930 struct tevent_req *subreq;
932 DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
933 (unsigned int)size));
935 status = smbd_initialize_smb2(conn);
936 if (!NT_STATUS_IS_OK(status)) {
937 smbd_server_connection_terminate(conn, nt_errstr(status));
941 status = smbd_smb2_request_create(conn, inbuf, size, &req);
942 if (!NT_STATUS_IS_OK(status)) {
943 smbd_server_connection_terminate(conn, nt_errstr(status));
947 status = smbd_smb2_request_setup_out(req);
948 if (!NT_STATUS_IS_OK(status)) {
949 smbd_server_connection_terminate(conn, nt_errstr(status));
953 status = smbd_smb2_request_dispatch(req);
954 if (!NT_STATUS_IS_OK(status)) {
955 smbd_server_connection_terminate(conn, nt_errstr(status));
959 /* ask for the next request */
960 subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
961 if (subreq == NULL) {
962 smbd_server_connection_terminate(conn, "no memory for reading");
965 tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
968 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
970 struct smbd_server_connection *conn = tevent_req_callback_data(subreq,
971 struct smbd_server_connection);
973 struct smbd_smb2_request *req;
975 status = smbd_smb2_request_read_recv(subreq, conn, &req);
977 if (!NT_STATUS_IS_OK(status)) {
978 smbd_server_connection_terminate(conn, nt_errstr(status));
982 /* TODO: validate the incoming request */
983 req->current_idx = 1;
985 DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
986 req->current_idx, req->in.vector_count));
988 status = smbd_smb2_request_setup_out(req);
989 if (!NT_STATUS_IS_OK(status)) {
990 smbd_server_connection_terminate(conn, nt_errstr(status));
994 status = smbd_smb2_request_dispatch(req);
995 if (!NT_STATUS_IS_OK(status)) {
996 smbd_server_connection_terminate(conn, nt_errstr(status));
1000 /* ask for the next request (this constructs the main loop) */
1001 subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
1002 if (subreq == NULL) {
1003 smbd_server_connection_terminate(conn, "no memory for reading");
1006 tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);