s3:smbd: add infrastructure for SMB2 support
[ddiss/samba.git] / source3 / smbd / smb2_server.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
24 #include "../lib/tsocket/tsocket.h"
25
26 bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size)
27 {
28         if (size < (4 + SMB2_HDR_BODY)) {
29                 return false;
30         }
31
32         if (IVAL(inbuf, 4) != SMB2_MAGIC) {
33                 return false;
34         }
35
36         return true;
37 }
38
39 static NTSTATUS smbd_initialize_smb2(struct smbd_server_connection *conn)
40 {
41         NTSTATUS status;
42         int ret;
43
44         TALLOC_FREE(conn->fde);
45
46         conn->smb2.event_ctx = smbd_event_context();
47
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;
51         }
52
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;
56         }
57
58         ret = tstream_bsd_existing_socket(conn, smbd_server_fd(),
59                                           &conn->smb2.stream);
60         if (ret == -1) {
61                 status = map_nt_error_from_unix(errno);
62                 return status;
63         }
64
65         /* Ensure child is set to non-blocking mode */
66         set_blocking(smbd_server_fd(),false);
67         return NT_STATUS_OK;
68 }
69
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; \
73         buf[0] = 0; \
74         buf[1] = ((len)&0xFF0000)>>16; \
75         buf[2] = ((len)&0xFF00)>>8; \
76         buf[3] = (len)&0xFF; \
77 } while (0)
78
79 static void smb2_setup_nbt_length(struct iovec *vector, int count)
80 {
81         size_t len = 0;
82         int i;
83
84         for (i=1; i < count; i++) {
85                 len += vector[i].iov_len;
86         }
87
88         _smb2_setlen(vector[0].iov_base, len);
89 }
90
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)
94 {
95         TALLOC_CTX *mem_pool;
96         struct smbd_smb2_request *req;
97         uint32_t protocol_version;
98         const uint8_t *inhdr = NULL;
99         off_t ofs = 0;
100         uint16_t cmd;
101         uint32_t next_command_ofs;
102
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;
106         }
107
108         inhdr = inbuf + 4;
109
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",
113                          protocol_version));
114                 return NT_STATUS_INVALID_PARAMETER;
115         }
116
117         cmd = SVAL(inhdr, SMB2_HDR_OPCODE);
118         if (cmd != SMB2_OP_NEGPROT) {
119                 DEBUG(0,("Invalid SMB packet: first request: 0x%04X\n",
120                          cmd));
121                 return NT_STATUS_INVALID_PARAMETER;
122         }
123
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",
127                          next_command_ofs));
128                 return NT_STATUS_INVALID_PARAMETER;
129         }
130
131         mem_pool = talloc_pool(conn, 8192);
132         if (mem_pool == NULL) {
133                 return NT_STATUS_NO_MEMORY;
134         }
135
136         req = talloc_zero(mem_pool, struct smbd_smb2_request);
137         if (req == NULL) {
138                 talloc_free(mem_pool);
139                 return NT_STATUS_NO_MEMORY;
140         }
141         req->mem_pool   = mem_pool;
142         req->conn       = conn;
143
144         talloc_steal(req, inbuf);
145
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;
150         }
151         req->in.vector_count = 4;
152
153         memcpy(req->in.nbt_hdr, inbuf, 4);
154
155         ofs = 0;
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;
159
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;
163
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;
167
168         if (ofs > size) {
169                 return NT_STATUS_INVALID_PARAMETER;
170         }
171
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;
175
176         req->current_idx = 1;
177
178         *_req = req;
179         return NT_STATUS_OK;
180 }
181
182 static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req)
183 {
184         struct iovec *vector;
185         int count;
186         int idx;
187
188         count = req->in.vector_count;
189         vector = talloc_array(req, struct iovec, count);
190         if (vector == NULL) {
191                 return NT_STATUS_NO_MEMORY;
192         }
193
194         vector[0].iov_base      = req->out.nbt_hdr;
195         vector[0].iov_len       = 4;
196         SIVAL(req->out.nbt_hdr, 0, 0);
197
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];
206
207                 if ((idx + 3) < count) {
208                         /* we have a next command */
209                         next_command_ofs = SMB2_HDR_BODY + 8 + 8;
210                         outdyn_size = 8;
211                 }
212
213                 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
214
215                 outhdr = talloc_array(vector, uint8_t,
216                                       SMB2_HDR_BODY + 8 + outdyn_size);
217                 if (outhdr == NULL) {
218                         return NT_STATUS_NO_MEMORY;
219                 }
220
221                 outbody = outhdr + SMB2_HDR_BODY;
222                 outdyn = outbody + 8;
223
224                 current[0].iov_base     = (void *)outhdr;
225                 current[0].iov_len      = SMB2_HDR_BODY;
226
227                 current[1].iov_base     = (void *)outbody;
228                 current[1].iov_len      = 8;
229
230                 current[2].iov_base     = (void *)outdyn;
231                 current[2].iov_len      = outdyn_size;
232
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                 SIVAL(outhdr, SMB2_HDR_MESSAGE_ID,
245                       IVAL(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                 SIVAL(outhdr, SMB2_HDR_SESSION_ID,
251                       IVAL(inhdr, SMB2_HDR_SESSION_ID));
252                 memset(outhdr + SMB2_HDR_SIGNATURE, 0, 16);
253
254                 /* setup error body header */
255                 SSVAL(outbody, 0x00, 9);
256                 SSVAL(outbody, 0x02, 0);
257                 SIVAL(outbody, 0x04, 0);
258
259                 /* setup the dynamic part */
260                 SCVAL(outdyn, 0x00, 0);
261         }
262
263         req->out.vector = vector;
264         req->out.vector_count = count;
265
266         /* setup the length of the NBT packet */
267         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
268
269         return NT_STATUS_OK;
270 }
271
272 static void smbd_server_connection_terminate(struct smbd_server_connection *conn,
273                                              const char *reason)
274 {
275         DEBUG(10,("smbd_server_connection_terminate: reason[%s]\n", reason));
276         exit_server_cleanly(reason);
277 }
278
279 static NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
280 {
281         const uint8_t *inhdr;
282         int i = req->current_idx;
283         uint16_t opcode;
284
285         inhdr = (const uint8_t *)req->in.vector[i].iov_base;
286
287         /* TODO: verify more things */
288
289         opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
290         DEBUG(10,("smbd_smb2_request_dispatch: opcode[%u]\n", opcode));
291         switch (opcode) {
292         case SMB2_OP_NEGPROT:
293         case SMB2_OP_SESSSETUP:
294         case SMB2_OP_LOGOFF:
295         case SMB2_OP_TCON:
296         case SMB2_OP_TDIS:
297         case SMB2_OP_CREATE:
298         case SMB2_OP_CLOSE:
299         case SMB2_OP_FLUSH:
300         case SMB2_OP_READ:
301         case SMB2_OP_WRITE:
302         case SMB2_OP_LOCK:
303         case SMB2_OP_IOCTL:
304         case SMB2_OP_CANCEL:
305         case SMB2_OP_KEEPALIVE:
306         case SMB2_OP_FIND:
307         case SMB2_OP_NOTIFY:
308         case SMB2_OP_GETINFO:
309         case SMB2_OP_SETINFO:
310         case SMB2_OP_BREAK:
311                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
312         }
313
314         return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
315 }
316
317 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq);
318 static void smbd_smb2_request_writev_done(struct tevent_req *subreq);
319
320 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
321 {
322         struct tevent_req *subreq;
323
324         /* TODO: sign the response here */
325
326         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
327
328         req->current_idx += 3;
329
330         if (req->current_idx > req->in.vector_count) {
331                 struct timeval zero = timeval_zero();
332                 subreq = tevent_wakeup_send(req,
333                                             req->conn->smb2.event_ctx,
334                                             zero);
335                 if (subreq == NULL) {
336                         return NT_STATUS_NO_MEMORY;
337                 }
338                 tevent_req_set_callback(subreq,
339                                         smbd_smb2_request_dispatch_compound,
340                                         req);
341
342                 return NT_STATUS_OK;
343         }
344
345         subreq = tstream_writev_queue_send(req,
346                                            req->conn->smb2.event_ctx,
347                                            req->conn->smb2.stream,
348                                            req->conn->smb2.send_queue,
349                                            req->out.vector,
350                                            req->out.vector_count);
351         if (subreq == NULL) {
352                 return NT_STATUS_NO_MEMORY;
353         }
354         tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
355
356         return NT_STATUS_OK;
357 }
358
359 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
360                                     NTSTATUS status,
361                                     DATA_BLOB *info)
362 {
363         uint8_t *outhdr;
364         uint8_t *outbody;
365         int i = req->current_idx;
366
367         DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s]%s\n",
368                   i, nt_errstr(status), info ? " +info" : ""));
369
370         outhdr = (uint8_t *)req->out.vector[i].iov_base;
371
372         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
373
374         outbody = outhdr + SMB2_HDR_BODY;
375
376         req->out.vector[i+1].iov_base = (void *)outbody;
377         req->out.vector[i+1].iov_len = 8;
378
379         if (info) {
380                 SIVAL(outbody, 0x04, info->length);
381                 req->out.vector[i+2].iov_base   = (void *)info->data;
382                 req->out.vector[i+2].iov_len    = info->length;
383         } else {
384                 req->out.vector[i+2].iov_base = (void *)(outbody + 8);
385                 req->out.vector[i+2].iov_len = 1;
386         }
387
388         /* the error packet is the last response in the chain */
389         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
390         req->out.vector_count = req->current_idx + 3;
391
392         return smbd_smb2_request_reply(req);
393 }
394
395 NTSTATUS smbd_smb2_request_error(struct smbd_smb2_request *req,
396                                  NTSTATUS status)
397 {
398         return smbd_smb2_request_error_ex(req, status, NULL);
399 }
400
401 NTSTATUS smbd_smb2_request_done(struct smbd_smb2_request *req,
402                                 DATA_BLOB body, DATA_BLOB *dyn)
403 {
404         uint8_t *outhdr;
405         uint8_t *outdyn;
406         int i = req->current_idx;
407         uint32_t next_command_ofs;
408
409         DEBUG(10,("smbd_smb2_request_done: idx[%d] body[%u] dyn[%s:%u]\n",
410                   i, (unsigned int)body.length,
411                   dyn ? "yes": "no",
412                   (unsigned int)(dyn ? dyn->length : 0)));
413
414         if (body.length < 2) {
415                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
416         }
417
418         if ((body.length % 2) != 0) {
419                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
420         }
421
422         outhdr = (uint8_t *)req->out.vector[i].iov_base;
423         /* the fallback dynamic buffer */
424         outdyn = outhdr + SMB2_HDR_BODY + 8;
425
426         next_command_ofs = SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
427         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(NT_STATUS_OK));
428
429         req->out.vector[i+1].iov_base = (void *)body.data;
430         req->out.vector[i+1].iov_len = body.length;
431
432         if (dyn) {
433                 if (dyn->length > 0) {
434                         req->out.vector[i+2].iov_base   = (void *)dyn->data;
435                         req->out.vector[i+2].iov_len    = dyn->length;
436                 } else {
437                         req->out.vector[i+2].iov_base   = (void *)outdyn;
438                         req->out.vector[i+2].iov_len    = 1;
439                 }
440         } else {
441                 req->out.vector[i+2].iov_base = NULL;
442                 req->out.vector[i+2].iov_len = 0;
443         }
444
445         /* see if we need to recalculate the offset to the next response */
446         if (next_command_ofs > 0) {
447                 next_command_ofs  = SMB2_HDR_BODY;
448                 next_command_ofs += req->out.vector[i+1].iov_len;
449                 next_command_ofs += req->out.vector[i+2].iov_len;
450         }
451
452         /* TODO: we need to add padding ... */
453         if ((next_command_ofs % 8) != 0) {
454                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
455         }
456
457         /* the error packet is the last response in the chain */
458         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
459
460         return smbd_smb2_request_reply(req);
461 }
462
463 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq)
464 {
465         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
466                                         struct smbd_smb2_request);
467         struct smbd_server_connection *conn = req->conn;
468         NTSTATUS status;
469
470         tevent_wakeup_recv(subreq);
471         TALLOC_FREE(subreq);
472
473         DEBUG(10,("smbd_smb2_request_dispatch_compound: idx[%d] of %d vectors\n",
474                   req->current_idx, req->in.vector_count));
475
476         status = smbd_smb2_request_dispatch(req);
477         if (!NT_STATUS_IS_OK(status)) {
478                 smbd_server_connection_terminate(conn, nt_errstr(status));
479                 return;
480         }
481 }
482
483 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
484 {
485         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
486                                         struct smbd_smb2_request);
487         struct smbd_server_connection *conn = req->conn;
488         int ret;
489         int sys_errno;
490         TALLOC_CTX *mem_pool;
491
492         ret = tstream_writev_queue_recv(subreq, &sys_errno);
493         TALLOC_FREE(subreq);
494         if (ret == -1) {
495                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
496                 smbd_server_connection_terminate(conn, nt_errstr(status));
497                 return;
498         }
499
500         mem_pool = req->mem_pool;
501         req = NULL;
502         talloc_free(mem_pool);
503 }
504
505 struct smbd_smb2_request_read_state {
506         size_t missing;
507         bool asked_for_header;
508         struct smbd_smb2_request *smb2_req;
509 };
510
511 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
512                                          void *private_data,
513                                          TALLOC_CTX *mem_ctx,
514                                          struct iovec **_vector,
515                                          size_t *_count);
516 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
517
518 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
519                                         struct tevent_context *ev,
520                                         struct smbd_server_connection *conn)
521 {
522         struct tevent_req *req;
523         struct smbd_smb2_request_read_state *state;
524         struct tevent_req *subreq;
525         TALLOC_CTX *mem_pool;
526
527         req = tevent_req_create(mem_ctx, &state,
528                                 struct smbd_smb2_request_read_state);
529         if (req == NULL) {
530                 return NULL;
531         }
532         state->missing = 0;
533         state->asked_for_header = false;
534
535         mem_pool = talloc_pool(state, 8192);
536         if (tevent_req_nomem(mem_pool, req)) {
537                 return tevent_req_post(req, ev);
538         }
539
540         state->smb2_req = talloc_zero(mem_pool, struct smbd_smb2_request);
541         if (tevent_req_nomem(state->smb2_req, req)) {
542                 return tevent_req_post(req, ev);
543         }
544
545         state->smb2_req->mem_pool       = mem_pool;
546         state->smb2_req->conn           = conn;
547
548         subreq = tstream_readv_pdu_queue_send(state, ev, conn->smb2.stream,
549                                               conn->smb2.recv_queue,
550                                               smbd_smb2_request_next_vector,
551                                               state);
552         if (tevent_req_nomem(subreq, req)) {
553                 return tevent_req_post(req, ev);
554         }
555         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
556
557         return req;
558 }
559
560 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
561                                          void *private_data,
562                                          TALLOC_CTX *mem_ctx,
563                                          struct iovec **_vector,
564                                          size_t *_count)
565 {
566         struct smbd_smb2_request_read_state *state =
567                 talloc_get_type_abort(private_data,
568                 struct smbd_smb2_request_read_state);
569         struct smbd_smb2_request *req = state->smb2_req;
570         struct iovec *vector;
571         int idx = req->in.vector_count;
572         size_t len = 0;
573         uint8_t *buf = NULL;
574
575         if (req->in.vector_count == 0) {
576                 /*
577                  * first we need to get the NBT header
578                  */
579                 req->in.vector = talloc_array(req, struct iovec,
580                                               req->in.vector_count + 1);
581                 if (req->in.vector == NULL) {
582                         return -1;
583                 }
584                 req->in.vector_count += 1;
585
586                 req->in.vector[idx].iov_base    = (void *)req->in.nbt_hdr;
587                 req->in.vector[idx].iov_len     = 4;
588
589                 vector = talloc_array(mem_ctx, struct iovec, 1);
590                 if (vector == NULL) {
591                         return -1;
592                 }
593
594                 vector[0] = req->in.vector[idx];
595
596                 *_vector = vector;
597                 *_count = 1;
598                 return 0;
599         }
600
601         if (req->in.vector_count == 1) {
602                 /*
603                  * Now we analyze the NBT header
604                  */
605                 state->missing = smb2_len(req->in.vector[0].iov_base);
606
607                 if (state->missing == 0) {
608                         /* if there're no remaining bytes, we're done */
609                         *_vector = NULL;
610                         *_count = 0;
611                         return 0;
612                 }
613
614                 req->in.vector = talloc_realloc(req, req->in.vector,
615                                                 struct iovec,
616                                                 req->in.vector_count + 1);
617                 if (req->in.vector == NULL) {
618                         return -1;
619                 }
620                 req->in.vector_count += 1;
621
622                 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
623                         /*
624                          * it's a special NBT message,
625                          * so get all remaining bytes
626                          */
627                         len = state->missing;
628                 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
629                         /*
630                          * it's an invalid message, just read what we can get
631                          * and let the caller handle the error
632                          */
633                         len = state->missing;
634                 } else {
635                         /*
636                          * We assume it's a SMB2 request,
637                          * and we first get the header and the
638                          * first 2 bytes (the struct size) of the body
639                          */
640                         len = SMB2_HDR_BODY + 2;
641
642                         state->asked_for_header = true;
643                 }
644
645                 state->missing -= len;
646
647                 buf = talloc_array(req->in.vector, uint8_t, len);
648                 if (buf == NULL) {
649                         return -1;
650                 }
651
652                 req->in.vector[idx].iov_base    = (void *)buf;
653                 req->in.vector[idx].iov_len     = len;
654
655                 vector = talloc_array(mem_ctx, struct iovec, 1);
656                 if (vector == NULL) {
657                         return -1;
658                 }
659
660                 vector[0] = req->in.vector[idx];
661
662                 *_vector = vector;
663                 *_count = 1;
664                 return 0;
665         }
666
667         if (state->missing == 0) {
668                 /* if there're no remaining bytes, we're done */
669                 *_vector = NULL;
670                 *_count = 0;
671                 return 0;
672         }
673
674         if (state->asked_for_header) {
675                 const uint8_t *hdr;
676                 size_t full_size;
677                 size_t next_command_ofs;
678                 size_t body_size;
679                 uint8_t *body;
680                 size_t dyn_size;
681                 uint8_t *dyn;
682                 bool invalid = false;
683
684                 state->asked_for_header = false;
685
686                 /*
687                  * We got the SMB2 header and the first 2 bytes
688                  * of the body. We fix the size to just the header
689                  * and manually copy the 2 first bytes to the body section
690                  */
691                 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
692                 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
693
694                 /* allocate vectors for body and dynamic areas */
695                 req->in.vector = talloc_realloc(req, req->in.vector,
696                                                 struct iovec,
697                                                 req->in.vector_count + 2);
698                 if (req->in.vector == NULL) {
699                         return -1;
700                 }
701                 req->in.vector_count += 2;
702
703                 full_size = state->missing + SMB2_HDR_BODY + 2;
704                 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
705                 body_size = SVAL(hdr, SMB2_HDR_BODY);
706
707                 if (next_command_ofs != 0) {
708                         if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
709                                 /*
710                                  * this is invalid, just return a zero
711                                  * body and let the caller deal with the error
712                                  */
713                                 invalid = true;
714                         } else if (next_command_ofs > full_size) {
715                                 /*
716                                  * this is invalid, just return a zero
717                                  * body and let the caller deal with the error
718                                  */
719                                 invalid = true;
720                         } else {
721                                 full_size = next_command_ofs;
722                         }
723                 }
724
725                 if (!invalid) {
726                         if (body_size < 2) {
727                                 /*
728                                  * this is invalid, just return a zero
729                                  * body and let the caller deal with the error
730                                  */
731                                 invalid = true;
732                         } else if (body_size > (full_size - SMB2_HDR_BODY)) {
733                                 /*
734                                  * this is invalid, just return a zero
735                                  * body and let the caller deal with the error
736                                  */
737                                 invalid = true;
738                         }
739                 }
740
741                 if (invalid) {
742                         /* the caller should check this */
743                         body_size = 0;
744                 }
745
746                 if ((body_size % 2) != 0) {
747                         body_size -= 1;
748                 }
749
750                 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
751
752                 state->missing -= (body_size - 2) + dyn_size;
753
754                 body = talloc_array(req->in.vector, uint8_t, body_size);
755                 if (body == NULL) {
756                         return -1;
757                 }
758
759                 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
760                 if (dyn == NULL) {
761                         return -1;
762                 }
763
764                 req->in.vector[idx].iov_base    = (void *)body;
765                 req->in.vector[idx].iov_len     = body_size;
766                 req->in.vector[idx+1].iov_base  = (void *)dyn;
767                 req->in.vector[idx+1].iov_len   = dyn_size;
768
769                 vector = talloc_array(mem_ctx, struct iovec, 2);
770                 if (vector == NULL) {
771                         return -1;
772                 }
773
774                 /*
775                  * the first 2 bytes of the body were already fetched
776                  * together with the header
777                  */
778                 memcpy(body, hdr + SMB2_HDR_BODY, 2);
779                 vector[0].iov_base = body + 2;
780                 vector[0].iov_len = req->in.vector[idx].iov_len - 2;
781
782                 vector[1] = req->in.vector[idx+1];
783
784                 *_vector = vector;
785                 *_count = 2;
786                 return 0;
787         }
788
789         /*
790          * when we endup here, we're looking for a new SMB2 request
791          * next. And we ask for its header and the first 2 bytes of
792          * the body (like we did for the first SMB2 request).
793          */
794
795         req->in.vector = talloc_realloc(req, req->in.vector,
796                                         struct iovec,
797                                         req->in.vector_count + 1);
798         if (req->in.vector == NULL) {
799                 return -1;
800         }
801         req->in.vector_count += 1;
802
803         /*
804          * We assume it's a SMB2 request,
805          * and we first get the header and the
806          * first 2 bytes (the struct size) of the body
807          */
808         len = SMB2_HDR_BODY + 2;
809
810         if (len > state->missing) {
811                 /* let the caller handle the error */
812                 len = state->missing;
813         }
814
815         state->missing -= len;
816         state->asked_for_header = true;
817
818         buf = talloc_array(req->in.vector, uint8_t, len);
819         if (buf == NULL) {
820                 return -1;
821         }
822
823         req->in.vector[idx].iov_base    = (void *)buf;
824         req->in.vector[idx].iov_len     = len;
825
826         vector = talloc_array(mem_ctx, struct iovec, 1);
827         if (vector == NULL) {
828                 return -1;
829         }
830
831         vector[0] = req->in.vector[idx];
832
833         *_vector = vector;
834         *_count = 1;
835         return 0;
836 }
837
838 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
839 {
840         struct tevent_req *req =
841                 tevent_req_callback_data(subreq,
842                 struct tevent_req);
843         int ret;
844         int sys_errno;
845         NTSTATUS status;
846
847         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
848         if (ret == -1) {
849                 status = map_nt_error_from_unix(sys_errno);
850                 tevent_req_nterror(req, status);
851                 return;
852         }
853
854         tevent_req_done(req);
855 }
856
857 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
858                                             TALLOC_CTX *mem_ctx,
859                                             struct smbd_smb2_request **_smb2_req)
860 {
861         struct smbd_smb2_request_read_state *state =
862                 tevent_req_data(req,
863                 struct smbd_smb2_request_read_state);
864         NTSTATUS status;
865
866         if (tevent_req_is_nterror(req, &status)) {
867                 tevent_req_received(req);
868                 return status;
869         }
870
871         talloc_steal(mem_ctx, state->smb2_req->mem_pool);
872         *_smb2_req = state->smb2_req;
873         tevent_req_received(req);
874         return NT_STATUS_OK;
875 }
876
877 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
878
879 void smbd_smb2_first_negprot(struct smbd_server_connection *conn,
880                              const uint8_t *inbuf, size_t size)
881 {
882         NTSTATUS status;
883         struct smbd_smb2_request *req;
884         struct tevent_req *subreq;
885
886         DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
887                  (unsigned int)size));
888
889         status = smbd_initialize_smb2(conn);
890         if (!NT_STATUS_IS_OK(status)) {
891                 smbd_server_connection_terminate(conn, nt_errstr(status));
892                 return;
893         }
894
895         status = smbd_smb2_request_create(conn, inbuf, size, &req);
896         if (!NT_STATUS_IS_OK(status)) {
897                 smbd_server_connection_terminate(conn, nt_errstr(status));
898                 return;
899         }
900
901         status = smbd_smb2_request_setup_out(req);
902         if (!NT_STATUS_IS_OK(status)) {
903                 smbd_server_connection_terminate(conn, nt_errstr(status));
904                 return;
905         }
906
907         status = smbd_smb2_request_dispatch(req);
908         if (!NT_STATUS_IS_OK(status)) {
909                 smbd_server_connection_terminate(conn, nt_errstr(status));
910                 return;
911         }
912
913         /* ask for the next request */
914         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
915         if (subreq == NULL) {
916                 smbd_server_connection_terminate(conn, "no memory for reading");
917                 return;
918         }
919         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
920 }
921
922 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
923 {
924         struct smbd_server_connection *conn = tevent_req_callback_data(subreq,
925                                               struct smbd_server_connection);
926         NTSTATUS status;
927         struct smbd_smb2_request *req;
928
929         status = smbd_smb2_request_read_recv(subreq, conn, &req);
930         TALLOC_FREE(subreq);
931         if (!NT_STATUS_IS_OK(status)) {
932                 smbd_server_connection_terminate(conn, nt_errstr(status));
933                 return;
934         }
935
936         /* TODO: validate the incoming request */
937         req->current_idx = 1;
938
939         DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
940                  req->current_idx, req->in.vector_count));
941
942         status = smbd_smb2_request_setup_out(req);
943         if (!NT_STATUS_IS_OK(status)) {
944                 smbd_server_connection_terminate(conn, nt_errstr(status));
945                 return;
946         }
947
948         status = smbd_smb2_request_dispatch(req);
949         if (!NT_STATUS_IS_OK(status)) {
950                 smbd_server_connection_terminate(conn, nt_errstr(status));
951                 return;
952         }
953
954         /* ask for the next request (this constructs the main loop) */
955         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
956         if (subreq == NULL) {
957                 smbd_server_connection_terminate(conn, "no memory for reading");
958                 return;
959         }
960         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
961 }