s3:smbd: SMB2 session ids are 64bit...
[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 = 0x00FFFFFF;
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         NTSTATUS status;
292
293         inhdr = (const uint8_t *)req->in.vector[i].iov_base;
294
295         /* TODO: verify more things */
296
297         opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
298         DEBUG(10,("smbd_smb2_request_dispatch: opcode[%u]\n", opcode));
299         switch (opcode) {
300         case SMB2_OP_NEGPROT:
301                 return smbd_smb2_request_process_negprot(req);
302
303         case SMB2_OP_SESSSETUP:
304                 return smbd_smb2_request_process_sesssetup(req);
305
306         case SMB2_OP_LOGOFF:
307                 status = smbd_smb2_request_check_session(req);
308                 if (!NT_STATUS_IS_OK(status)) {
309                         return smbd_smb2_request_error(req, status);
310                 }
311                 return smbd_smb2_request_process_logoff(req);
312
313         case SMB2_OP_TCON:
314                 status = smbd_smb2_request_check_session(req);
315                 if (!NT_STATUS_IS_OK(status)) {
316                         return smbd_smb2_request_error(req, status);
317                 }
318                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
319
320         case SMB2_OP_TDIS:
321                 status = smbd_smb2_request_check_session(req);
322                 if (!NT_STATUS_IS_OK(status)) {
323                         return smbd_smb2_request_error(req, status);
324                 }
325                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
326
327         case SMB2_OP_CREATE:
328                 status = smbd_smb2_request_check_session(req);
329                 if (!NT_STATUS_IS_OK(status)) {
330                         return smbd_smb2_request_error(req, status);
331                 }
332                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
333
334         case SMB2_OP_CLOSE:
335                 status = smbd_smb2_request_check_session(req);
336                 if (!NT_STATUS_IS_OK(status)) {
337                         return smbd_smb2_request_error(req, status);
338                 }
339                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
340
341         case SMB2_OP_FLUSH:
342                 status = smbd_smb2_request_check_session(req);
343                 if (!NT_STATUS_IS_OK(status)) {
344                         return smbd_smb2_request_error(req, status);
345                 }
346                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
347
348         case SMB2_OP_READ:
349                 status = smbd_smb2_request_check_session(req);
350                 if (!NT_STATUS_IS_OK(status)) {
351                         return smbd_smb2_request_error(req, status);
352                 }
353                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
354
355         case SMB2_OP_WRITE:
356                 status = smbd_smb2_request_check_session(req);
357                 if (!NT_STATUS_IS_OK(status)) {
358                         return smbd_smb2_request_error(req, status);
359                 }
360                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
361
362         case SMB2_OP_LOCK:
363                 status = smbd_smb2_request_check_session(req);
364                 if (!NT_STATUS_IS_OK(status)) {
365                         return smbd_smb2_request_error(req, status);
366                 }
367                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
368
369         case SMB2_OP_IOCTL:
370                 status = smbd_smb2_request_check_session(req);
371                 if (!NT_STATUS_IS_OK(status)) {
372                         return smbd_smb2_request_error(req, status);
373                 }
374                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
375
376         case SMB2_OP_CANCEL:
377                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
378
379         case SMB2_OP_KEEPALIVE:
380                 return smbd_smb2_request_process_keepalive(req);
381
382         case SMB2_OP_FIND:
383                 status = smbd_smb2_request_check_session(req);
384                 if (!NT_STATUS_IS_OK(status)) {
385                         return smbd_smb2_request_error(req, status);
386                 }
387                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
388
389         case SMB2_OP_NOTIFY:
390                 status = smbd_smb2_request_check_session(req);
391                 if (!NT_STATUS_IS_OK(status)) {
392                         return smbd_smb2_request_error(req, status);
393                 }
394                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
395
396         case SMB2_OP_GETINFO:
397                 status = smbd_smb2_request_check_session(req);
398                 if (!NT_STATUS_IS_OK(status)) {
399                         return smbd_smb2_request_error(req, status);
400                 }
401                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
402
403         case SMB2_OP_SETINFO:
404                 status = smbd_smb2_request_check_session(req);
405                 if (!NT_STATUS_IS_OK(status)) {
406                         return smbd_smb2_request_error(req, status);
407                 }
408                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
409
410         case SMB2_OP_BREAK:
411                 status = smbd_smb2_request_check_session(req);
412                 if (!NT_STATUS_IS_OK(status)) {
413                         return smbd_smb2_request_error(req, status);
414                 }
415                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
416         }
417
418         return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
419 }
420
421 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq);
422 static void smbd_smb2_request_writev_done(struct tevent_req *subreq);
423
424 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
425 {
426         struct tevent_req *subreq;
427
428         /* TODO: sign the response here */
429
430         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
431
432         req->current_idx += 3;
433
434         if (req->current_idx > req->in.vector_count) {
435                 struct timeval zero = timeval_zero();
436                 subreq = tevent_wakeup_send(req,
437                                             req->conn->smb2.event_ctx,
438                                             zero);
439                 if (subreq == NULL) {
440                         return NT_STATUS_NO_MEMORY;
441                 }
442                 tevent_req_set_callback(subreq,
443                                         smbd_smb2_request_dispatch_compound,
444                                         req);
445
446                 return NT_STATUS_OK;
447         }
448
449         subreq = tstream_writev_queue_send(req,
450                                            req->conn->smb2.event_ctx,
451                                            req->conn->smb2.stream,
452                                            req->conn->smb2.send_queue,
453                                            req->out.vector,
454                                            req->out.vector_count);
455         if (subreq == NULL) {
456                 return NT_STATUS_NO_MEMORY;
457         }
458         tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
459
460         return NT_STATUS_OK;
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 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
506                                     NTSTATUS status,
507                                     DATA_BLOB *info)
508 {
509         uint8_t *outhdr;
510         uint8_t *outbody;
511         int i = req->current_idx;
512
513         DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s]%s\n",
514                   i, nt_errstr(status), info ? " +info" : ""));
515
516         outhdr = (uint8_t *)req->out.vector[i].iov_base;
517
518         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
519
520         outbody = outhdr + SMB2_HDR_BODY;
521
522         req->out.vector[i+1].iov_base = (void *)outbody;
523         req->out.vector[i+1].iov_len = 8;
524
525         if (info) {
526                 SIVAL(outbody, 0x04, info->length);
527                 req->out.vector[i+2].iov_base   = (void *)info->data;
528                 req->out.vector[i+2].iov_len    = info->length;
529         } else {
530                 req->out.vector[i+2].iov_base = (void *)(outbody + 8);
531                 req->out.vector[i+2].iov_len = 1;
532         }
533
534         /* the error packet is the last response in the chain */
535         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
536         req->out.vector_count = req->current_idx + 3;
537
538         return smbd_smb2_request_reply(req);
539 }
540
541 NTSTATUS smbd_smb2_request_error(struct smbd_smb2_request *req,
542                                  NTSTATUS status)
543 {
544         return smbd_smb2_request_error_ex(req, status, NULL);
545 }
546
547 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
548                                    NTSTATUS status,
549                                    DATA_BLOB body, DATA_BLOB *dyn)
550 {
551         uint8_t *outhdr;
552         uint8_t *outdyn;
553         int i = req->current_idx;
554         uint32_t next_command_ofs;
555
556         DEBUG(10,("smbd_smb2_request_done_ex: "
557                   "idx[%d] status[%s] body[%u] dyn[%s:%u]\n",
558                   i, nt_errstr(status), (unsigned int)body.length,
559                   dyn ? "yes": "no",
560                   (unsigned int)(dyn ? dyn->length : 0)));
561
562         if (body.length < 2) {
563                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
564         }
565
566         if ((body.length % 2) != 0) {
567                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
568         }
569
570         outhdr = (uint8_t *)req->out.vector[i].iov_base;
571         /* the fallback dynamic buffer */
572         outdyn = outhdr + SMB2_HDR_BODY + 8;
573
574         next_command_ofs = IVAL(outhdr, SMB2_HDR_NEXT_COMMAND);
575         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
576
577         req->out.vector[i+1].iov_base = (void *)body.data;
578         req->out.vector[i+1].iov_len = body.length;
579
580         if (dyn) {
581                 if (dyn->length > 0) {
582                         req->out.vector[i+2].iov_base   = (void *)dyn->data;
583                         req->out.vector[i+2].iov_len    = dyn->length;
584                 } else {
585                         req->out.vector[i+2].iov_base   = (void *)outdyn;
586                         req->out.vector[i+2].iov_len    = 1;
587                 }
588         } else {
589                 req->out.vector[i+2].iov_base = NULL;
590                 req->out.vector[i+2].iov_len = 0;
591         }
592
593         /* see if we need to recalculate the offset to the next response */
594         if (next_command_ofs > 0) {
595                 next_command_ofs  = SMB2_HDR_BODY;
596                 next_command_ofs += req->out.vector[i+1].iov_len;
597                 next_command_ofs += req->out.vector[i+2].iov_len;
598         }
599
600         /* TODO: we need to add padding ... */
601         if ((next_command_ofs % 8) != 0) {
602                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
603         }
604
605         /* the error packet is the last response in the chain */
606         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
607
608         return smbd_smb2_request_reply(req);
609 }
610
611 NTSTATUS smbd_smb2_request_done(struct smbd_smb2_request *req,
612                                 DATA_BLOB body, DATA_BLOB *dyn)
613 {
614         return smbd_smb2_request_done_ex(req, NT_STATUS_OK, body, dyn);
615 }
616
617 struct smbd_smb2_request_read_state {
618         size_t missing;
619         bool asked_for_header;
620         struct smbd_smb2_request *smb2_req;
621 };
622
623 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
624                                          void *private_data,
625                                          TALLOC_CTX *mem_ctx,
626                                          struct iovec **_vector,
627                                          size_t *_count);
628 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
629
630 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
631                                         struct tevent_context *ev,
632                                         struct smbd_server_connection *conn)
633 {
634         struct tevent_req *req;
635         struct smbd_smb2_request_read_state *state;
636         struct tevent_req *subreq;
637         TALLOC_CTX *mem_pool;
638
639         req = tevent_req_create(mem_ctx, &state,
640                                 struct smbd_smb2_request_read_state);
641         if (req == NULL) {
642                 return NULL;
643         }
644         state->missing = 0;
645         state->asked_for_header = false;
646
647         mem_pool = talloc_pool(state, 8192);
648         if (tevent_req_nomem(mem_pool, req)) {
649                 return tevent_req_post(req, ev);
650         }
651
652         state->smb2_req = talloc_zero(mem_pool, struct smbd_smb2_request);
653         if (tevent_req_nomem(state->smb2_req, req)) {
654                 return tevent_req_post(req, ev);
655         }
656
657         state->smb2_req->mem_pool       = mem_pool;
658         state->smb2_req->conn           = conn;
659
660         subreq = tstream_readv_pdu_queue_send(state, ev, conn->smb2.stream,
661                                               conn->smb2.recv_queue,
662                                               smbd_smb2_request_next_vector,
663                                               state);
664         if (tevent_req_nomem(subreq, req)) {
665                 return tevent_req_post(req, ev);
666         }
667         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
668
669         return req;
670 }
671
672 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
673                                          void *private_data,
674                                          TALLOC_CTX *mem_ctx,
675                                          struct iovec **_vector,
676                                          size_t *_count)
677 {
678         struct smbd_smb2_request_read_state *state =
679                 talloc_get_type_abort(private_data,
680                 struct smbd_smb2_request_read_state);
681         struct smbd_smb2_request *req = state->smb2_req;
682         struct iovec *vector;
683         int idx = req->in.vector_count;
684         size_t len = 0;
685         uint8_t *buf = NULL;
686
687         if (req->in.vector_count == 0) {
688                 /*
689                  * first we need to get the NBT header
690                  */
691                 req->in.vector = talloc_array(req, struct iovec,
692                                               req->in.vector_count + 1);
693                 if (req->in.vector == NULL) {
694                         return -1;
695                 }
696                 req->in.vector_count += 1;
697
698                 req->in.vector[idx].iov_base    = (void *)req->in.nbt_hdr;
699                 req->in.vector[idx].iov_len     = 4;
700
701                 vector = talloc_array(mem_ctx, struct iovec, 1);
702                 if (vector == NULL) {
703                         return -1;
704                 }
705
706                 vector[0] = req->in.vector[idx];
707
708                 *_vector = vector;
709                 *_count = 1;
710                 return 0;
711         }
712
713         if (req->in.vector_count == 1) {
714                 /*
715                  * Now we analyze the NBT header
716                  */
717                 state->missing = smb2_len(req->in.vector[0].iov_base);
718
719                 if (state->missing == 0) {
720                         /* if there're no remaining bytes, we're done */
721                         *_vector = NULL;
722                         *_count = 0;
723                         return 0;
724                 }
725
726                 req->in.vector = talloc_realloc(req, req->in.vector,
727                                                 struct iovec,
728                                                 req->in.vector_count + 1);
729                 if (req->in.vector == NULL) {
730                         return -1;
731                 }
732                 req->in.vector_count += 1;
733
734                 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
735                         /*
736                          * it's a special NBT message,
737                          * so get all remaining bytes
738                          */
739                         len = state->missing;
740                 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
741                         /*
742                          * it's an invalid message, just read what we can get
743                          * and let the caller handle the error
744                          */
745                         len = state->missing;
746                 } else {
747                         /*
748                          * We assume it's a SMB2 request,
749                          * and we first get the header and the
750                          * first 2 bytes (the struct size) of the body
751                          */
752                         len = SMB2_HDR_BODY + 2;
753
754                         state->asked_for_header = true;
755                 }
756
757                 state->missing -= len;
758
759                 buf = talloc_array(req->in.vector, uint8_t, len);
760                 if (buf == NULL) {
761                         return -1;
762                 }
763
764                 req->in.vector[idx].iov_base    = (void *)buf;
765                 req->in.vector[idx].iov_len     = len;
766
767                 vector = talloc_array(mem_ctx, struct iovec, 1);
768                 if (vector == NULL) {
769                         return -1;
770                 }
771
772                 vector[0] = req->in.vector[idx];
773
774                 *_vector = vector;
775                 *_count = 1;
776                 return 0;
777         }
778
779         if (state->missing == 0) {
780                 /* if there're no remaining bytes, we're done */
781                 *_vector = NULL;
782                 *_count = 0;
783                 return 0;
784         }
785
786         if (state->asked_for_header) {
787                 const uint8_t *hdr;
788                 size_t full_size;
789                 size_t next_command_ofs;
790                 size_t body_size;
791                 uint8_t *body;
792                 size_t dyn_size;
793                 uint8_t *dyn;
794                 bool invalid = false;
795
796                 state->asked_for_header = false;
797
798                 /*
799                  * We got the SMB2 header and the first 2 bytes
800                  * of the body. We fix the size to just the header
801                  * and manually copy the 2 first bytes to the body section
802                  */
803                 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
804                 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
805
806                 /* allocate vectors for body and dynamic areas */
807                 req->in.vector = talloc_realloc(req, req->in.vector,
808                                                 struct iovec,
809                                                 req->in.vector_count + 2);
810                 if (req->in.vector == NULL) {
811                         return -1;
812                 }
813                 req->in.vector_count += 2;
814
815                 full_size = state->missing + SMB2_HDR_BODY + 2;
816                 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
817                 body_size = SVAL(hdr, SMB2_HDR_BODY);
818
819                 if (next_command_ofs != 0) {
820                         if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
821                                 /*
822                                  * this is invalid, just return a zero
823                                  * body and let the caller deal with the error
824                                  */
825                                 invalid = true;
826                         } else if (next_command_ofs > full_size) {
827                                 /*
828                                  * this is invalid, just return a zero
829                                  * body and let the caller deal with the error
830                                  */
831                                 invalid = true;
832                         } else {
833                                 full_size = next_command_ofs;
834                         }
835                 }
836
837                 if (!invalid) {
838                         if (body_size < 2) {
839                                 /*
840                                  * this is invalid, just return a zero
841                                  * body and let the caller deal with the error
842                                  */
843                                 invalid = true;
844                         } else if (body_size > (full_size - SMB2_HDR_BODY)) {
845                                 /*
846                                  * this is invalid, just return a zero
847                                  * body and let the caller deal with the error
848                                  */
849                                 invalid = true;
850                         }
851                 }
852
853                 if (invalid) {
854                         /* the caller should check this */
855                         body_size = 0;
856                 }
857
858                 if ((body_size % 2) != 0) {
859                         body_size -= 1;
860                 }
861
862                 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
863
864                 state->missing -= (body_size - 2) + dyn_size;
865
866                 body = talloc_array(req->in.vector, uint8_t, body_size);
867                 if (body == NULL) {
868                         return -1;
869                 }
870
871                 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
872                 if (dyn == NULL) {
873                         return -1;
874                 }
875
876                 req->in.vector[idx].iov_base    = (void *)body;
877                 req->in.vector[idx].iov_len     = body_size;
878                 req->in.vector[idx+1].iov_base  = (void *)dyn;
879                 req->in.vector[idx+1].iov_len   = dyn_size;
880
881                 vector = talloc_array(mem_ctx, struct iovec, 2);
882                 if (vector == NULL) {
883                         return -1;
884                 }
885
886                 /*
887                  * the first 2 bytes of the body were already fetched
888                  * together with the header
889                  */
890                 memcpy(body, hdr + SMB2_HDR_BODY, 2);
891                 vector[0].iov_base = body + 2;
892                 vector[0].iov_len = req->in.vector[idx].iov_len - 2;
893
894                 vector[1] = req->in.vector[idx+1];
895
896                 *_vector = vector;
897                 *_count = 2;
898                 return 0;
899         }
900
901         /*
902          * when we endup here, we're looking for a new SMB2 request
903          * next. And we ask for its header and the first 2 bytes of
904          * the body (like we did for the first SMB2 request).
905          */
906
907         req->in.vector = talloc_realloc(req, req->in.vector,
908                                         struct iovec,
909                                         req->in.vector_count + 1);
910         if (req->in.vector == NULL) {
911                 return -1;
912         }
913         req->in.vector_count += 1;
914
915         /*
916          * We assume it's a SMB2 request,
917          * and we first get the header and the
918          * first 2 bytes (the struct size) of the body
919          */
920         len = SMB2_HDR_BODY + 2;
921
922         if (len > state->missing) {
923                 /* let the caller handle the error */
924                 len = state->missing;
925         }
926
927         state->missing -= len;
928         state->asked_for_header = true;
929
930         buf = talloc_array(req->in.vector, uint8_t, len);
931         if (buf == NULL) {
932                 return -1;
933         }
934
935         req->in.vector[idx].iov_base    = (void *)buf;
936         req->in.vector[idx].iov_len     = len;
937
938         vector = talloc_array(mem_ctx, struct iovec, 1);
939         if (vector == NULL) {
940                 return -1;
941         }
942
943         vector[0] = req->in.vector[idx];
944
945         *_vector = vector;
946         *_count = 1;
947         return 0;
948 }
949
950 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
951 {
952         struct tevent_req *req =
953                 tevent_req_callback_data(subreq,
954                 struct tevent_req);
955         int ret;
956         int sys_errno;
957         NTSTATUS status;
958
959         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
960         if (ret == -1) {
961                 status = map_nt_error_from_unix(sys_errno);
962                 tevent_req_nterror(req, status);
963                 return;
964         }
965
966         tevent_req_done(req);
967 }
968
969 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
970                                             TALLOC_CTX *mem_ctx,
971                                             struct smbd_smb2_request **_smb2_req)
972 {
973         struct smbd_smb2_request_read_state *state =
974                 tevent_req_data(req,
975                 struct smbd_smb2_request_read_state);
976         NTSTATUS status;
977
978         if (tevent_req_is_nterror(req, &status)) {
979                 tevent_req_received(req);
980                 return status;
981         }
982
983         talloc_steal(mem_ctx, state->smb2_req->mem_pool);
984         *_smb2_req = state->smb2_req;
985         tevent_req_received(req);
986         return NT_STATUS_OK;
987 }
988
989 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
990
991 void smbd_smb2_first_negprot(struct smbd_server_connection *conn,
992                              const uint8_t *inbuf, size_t size)
993 {
994         NTSTATUS status;
995         struct smbd_smb2_request *req;
996         struct tevent_req *subreq;
997
998         DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
999                  (unsigned int)size));
1000
1001         status = smbd_initialize_smb2(conn);
1002         if (!NT_STATUS_IS_OK(status)) {
1003                 smbd_server_connection_terminate(conn, nt_errstr(status));
1004                 return;
1005         }
1006
1007         status = smbd_smb2_request_create(conn, inbuf, size, &req);
1008         if (!NT_STATUS_IS_OK(status)) {
1009                 smbd_server_connection_terminate(conn, nt_errstr(status));
1010                 return;
1011         }
1012
1013         status = smbd_smb2_request_setup_out(req);
1014         if (!NT_STATUS_IS_OK(status)) {
1015                 smbd_server_connection_terminate(conn, nt_errstr(status));
1016                 return;
1017         }
1018
1019         status = smbd_smb2_request_dispatch(req);
1020         if (!NT_STATUS_IS_OK(status)) {
1021                 smbd_server_connection_terminate(conn, nt_errstr(status));
1022                 return;
1023         }
1024
1025         /* ask for the next request */
1026         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
1027         if (subreq == NULL) {
1028                 smbd_server_connection_terminate(conn, "no memory for reading");
1029                 return;
1030         }
1031         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
1032 }
1033
1034 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
1035 {
1036         struct smbd_server_connection *conn = tevent_req_callback_data(subreq,
1037                                               struct smbd_server_connection);
1038         NTSTATUS status;
1039         struct smbd_smb2_request *req;
1040
1041         status = smbd_smb2_request_read_recv(subreq, conn, &req);
1042         TALLOC_FREE(subreq);
1043         if (!NT_STATUS_IS_OK(status)) {
1044                 smbd_server_connection_terminate(conn, nt_errstr(status));
1045                 return;
1046         }
1047
1048         /* TODO: validate the incoming request */
1049         req->current_idx = 1;
1050
1051         DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
1052                  req->current_idx, req->in.vector_count));
1053
1054         status = smbd_smb2_request_setup_out(req);
1055         if (!NT_STATUS_IS_OK(status)) {
1056                 smbd_server_connection_terminate(conn, nt_errstr(status));
1057                 return;
1058         }
1059
1060         status = smbd_smb2_request_dispatch(req);
1061         if (!NT_STATUS_IS_OK(status)) {
1062                 smbd_server_connection_terminate(conn, nt_errstr(status));
1063                 return;
1064         }
1065
1066         /* ask for the next request (this constructs the main loop) */
1067         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
1068         if (subreq == NULL) {
1069                 smbd_server_connection_terminate(conn, "no memory for reading");
1070                 return;
1071         }
1072         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
1073 }