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