Change crediting so that the credits are returned on the interim async response.
[metze/samba/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    Copyright (C) Jeremy Allison 2010
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/tsocket/tsocket.h"
26
27 #define OUTVEC_ALLOC_SIZE (SMB2_HDR_BODY + 9)
28
29 static const char *smb2_names[] = {
30         "SMB2_NEGPROT",
31         "SMB2_SESSSETUP",
32         "SMB2_LOGOFF",
33         "SMB2_TCON",
34         "SMB2_TDIS",
35         "SMB2_CREATE",
36         "SMB2_CLOSE",
37         "SMB2_FLUSH",
38         "SMB2_READ",
39         "SMB2_WRITE",
40         "SMB2_LOCK",
41         "SMB2_IOCTL",
42         "SMB2_CANCEL",
43         "SMB2_KEEPALIVE",
44         "SMB2_FIND",
45         "SMB2_NOTIFY",
46         "SMB2_GETINFO",
47         "SMB2_SETINFO",
48         "SMB2_BREAK"
49 };
50
51 const char *smb2_opcode_name(uint16_t opcode)
52 {
53         if (opcode > 0x12) {
54                 return "Bad SMB2 opcode";
55         }
56         return smb2_names[opcode];
57 }
58
59 static void print_req_vectors(struct smbd_smb2_request *req)
60 {
61         int i;
62
63         for (i = 0; i < req->in.vector_count; i++) {
64                 dbgtext("\treq->in.vector[%u].iov_len = %u\n",
65                         (unsigned int)i,
66                         (unsigned int)req->in.vector[i].iov_len);
67         }
68         for (i = 0; i < req->out.vector_count; i++) {
69                 dbgtext("\treq->out.vector[%u].iov_len = %u\n",
70                         (unsigned int)i,
71                         (unsigned int)req->out.vector[i].iov_len);
72         }
73 }
74
75 bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size)
76 {
77         if (size < (4 + SMB2_HDR_BODY)) {
78                 return false;
79         }
80
81         if (IVAL(inbuf, 4) != SMB2_MAGIC) {
82                 return false;
83         }
84
85         return true;
86 }
87
88 static NTSTATUS smbd_initialize_smb2(struct smbd_server_connection *sconn)
89 {
90         NTSTATUS status;
91         int ret;
92
93         TALLOC_FREE(sconn->smb1.fde);
94
95         sconn->smb2.event_ctx = smbd_event_context();
96
97         sconn->smb2.recv_queue = tevent_queue_create(sconn, "smb2 recv queue");
98         if (sconn->smb2.recv_queue == NULL) {
99                 return NT_STATUS_NO_MEMORY;
100         }
101
102         sconn->smb2.send_queue = tevent_queue_create(sconn, "smb2 send queue");
103         if (sconn->smb2.send_queue == NULL) {
104                 return NT_STATUS_NO_MEMORY;
105         }
106
107         sconn->smb2.sessions.idtree = idr_init(sconn);
108         if (sconn->smb2.sessions.idtree == NULL) {
109                 return NT_STATUS_NO_MEMORY;
110         }
111         sconn->smb2.sessions.limit = 0x0000FFFE;
112         sconn->smb2.sessions.list = NULL;
113         sconn->smb2.seqnum_low = 0;
114         sconn->smb2.credits_granted = 0;
115         sconn->smb2.max_credits = lp_smb2_max_credits();
116         sconn->smb2.credits_bitmap = bitmap_talloc(sconn, 2*sconn->smb2.max_credits);
117         if (sconn->smb2.credits_bitmap == NULL) {
118                 return NT_STATUS_NO_MEMORY;
119         }
120
121         ret = tstream_bsd_existing_socket(sconn, sconn->sock,
122                                           &sconn->smb2.stream);
123         if (ret == -1) {
124                 status = map_nt_error_from_unix(errno);
125                 return status;
126         }
127
128         /* Ensure child is set to non-blocking mode */
129         set_blocking(sconn->sock, false);
130         return NT_STATUS_OK;
131 }
132
133 #define smb2_len(buf) (PVAL(buf,3)|(PVAL(buf,2)<<8)|(PVAL(buf,1)<<16))
134 #define _smb2_setlen(_buf,len) do { \
135         uint8_t *buf = (uint8_t *)_buf; \
136         buf[0] = 0; \
137         buf[1] = ((len)&0xFF0000)>>16; \
138         buf[2] = ((len)&0xFF00)>>8; \
139         buf[3] = (len)&0xFF; \
140 } while (0)
141
142 static void smb2_setup_nbt_length(struct iovec *vector, int count)
143 {
144         size_t len = 0;
145         int i;
146
147         for (i=1; i < count; i++) {
148                 len += vector[i].iov_len;
149         }
150
151         _smb2_setlen(vector[0].iov_base, len);
152 }
153
154 static int smbd_smb2_request_parent_destructor(struct smbd_smb2_request **req)
155 {
156         if (*req) {
157                 (*req)->parent = NULL;
158                 (*req)->mem_pool = NULL;
159         }
160
161         return 0;
162 }
163
164 static int smbd_smb2_request_destructor(struct smbd_smb2_request *req)
165 {
166         if (req->parent) {
167                 *req->parent = NULL;
168                 talloc_free(req->mem_pool);
169         }
170
171         return 0;
172 }
173
174 static struct smbd_smb2_request *smbd_smb2_request_allocate(TALLOC_CTX *mem_ctx)
175 {
176         TALLOC_CTX *mem_pool;
177         struct smbd_smb2_request **parent;
178         struct smbd_smb2_request *req;
179
180 #if 0
181         /* Enable this to find subtle valgrind errors. */
182         mem_pool = talloc_init("smbd_smb2_request_allocate");
183 #else
184         mem_pool = talloc_pool(mem_ctx, 8192);
185 #endif
186         if (mem_pool == NULL) {
187                 return NULL;
188         }
189
190         parent = talloc(mem_pool, struct smbd_smb2_request *);
191         if (parent == NULL) {
192                 talloc_free(mem_pool);
193                 return NULL;
194         }
195
196         req = talloc_zero(parent, struct smbd_smb2_request);
197         if (req == NULL) {
198                 talloc_free(mem_pool);
199                 return NULL;
200         }
201         *parent         = req;
202         req->mem_pool   = mem_pool;
203         req->parent     = parent;
204
205         talloc_set_destructor(parent, smbd_smb2_request_parent_destructor);
206         talloc_set_destructor(req, smbd_smb2_request_destructor);
207
208         return req;
209 }
210
211 static NTSTATUS smbd_smb2_request_create(struct smbd_server_connection *sconn,
212                                          const uint8_t *inbuf, size_t size,
213                                          struct smbd_smb2_request **_req)
214 {
215         struct smbd_smb2_request *req;
216         uint32_t protocol_version;
217         const uint8_t *inhdr = NULL;
218         off_t ofs = 0;
219         uint16_t cmd;
220         uint32_t next_command_ofs;
221
222         if (size < (4 + SMB2_HDR_BODY + 2)) {
223                 DEBUG(0,("Invalid SMB2 packet length count %ld\n", (long)size));
224                 return NT_STATUS_INVALID_PARAMETER;
225         }
226
227         inhdr = inbuf + 4;
228
229         protocol_version = IVAL(inhdr, SMB2_HDR_PROTOCOL_ID);
230         if (protocol_version != SMB2_MAGIC) {
231                 DEBUG(0,("Invalid SMB packet: protocol prefix: 0x%08X\n",
232                          protocol_version));
233                 return NT_STATUS_INVALID_PARAMETER;
234         }
235
236         cmd = SVAL(inhdr, SMB2_HDR_OPCODE);
237         if (cmd != SMB2_OP_NEGPROT) {
238                 DEBUG(0,("Invalid SMB packet: first request: 0x%04X\n",
239                          cmd));
240                 return NT_STATUS_INVALID_PARAMETER;
241         }
242
243         next_command_ofs = IVAL(inhdr, SMB2_HDR_NEXT_COMMAND);
244         if (next_command_ofs != 0) {
245                 DEBUG(0,("Invalid SMB packet: next_command: 0x%08X\n",
246                          next_command_ofs));
247                 return NT_STATUS_INVALID_PARAMETER;
248         }
249
250         req = smbd_smb2_request_allocate(sconn);
251         if (req == NULL) {
252                 return NT_STATUS_NO_MEMORY;
253         }
254         req->sconn = sconn;
255
256         talloc_steal(req, inbuf);
257
258         req->in.vector = talloc_array(req, struct iovec, 4);
259         if (req->in.vector == NULL) {
260                 TALLOC_FREE(req);
261                 return NT_STATUS_NO_MEMORY;
262         }
263         req->in.vector_count = 4;
264
265         memcpy(req->in.nbt_hdr, inbuf, 4);
266
267         ofs = 0;
268         req->in.vector[0].iov_base      = (void *)req->in.nbt_hdr;
269         req->in.vector[0].iov_len       = 4;
270         ofs += req->in.vector[0].iov_len;
271
272         req->in.vector[1].iov_base      = (void *)(inbuf + ofs);
273         req->in.vector[1].iov_len       = SMB2_HDR_BODY;
274         ofs += req->in.vector[1].iov_len;
275
276         req->in.vector[2].iov_base      = (void *)(inbuf + ofs);
277         req->in.vector[2].iov_len       = SVAL(inbuf, ofs) & 0xFFFE;
278         ofs += req->in.vector[2].iov_len;
279
280         if (ofs > size) {
281                 return NT_STATUS_INVALID_PARAMETER;
282         }
283
284         req->in.vector[3].iov_base      = (void *)(inbuf + ofs);
285         req->in.vector[3].iov_len       = size - ofs;
286         ofs += req->in.vector[3].iov_len;
287
288         req->current_idx = 1;
289
290         *_req = req;
291         return NT_STATUS_OK;
292 }
293
294 static bool smb2_validate_message_id(struct smbd_server_connection *sconn,
295                                 const uint8_t *inhdr)
296 {
297         uint64_t message_id = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
298         struct bitmap *credits_bm = sconn->smb2.credits_bitmap;
299         uint16_t opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
300         unsigned int bitmap_offset;
301
302         if (opcode == SMB2_OP_CANCEL) {
303                 /* SMB2_CANCEL requests by definition resend messageids. */
304                 return true;
305         }
306
307         if (message_id < sconn->smb2.seqnum_low ||
308                         message_id > (sconn->smb2.seqnum_low +
309                         (2*sconn->smb2.credits_granted))) {
310                 DEBUG(0,("smb2_validate_message_id: bad message_id "
311                         "%llu (low = %llu, granted = %lu)\n",
312                         (unsigned long long)message_id,
313                         (unsigned long long)sconn->smb2.seqnum_low,
314                         (unsigned long)sconn->smb2.credits_granted ));
315                 return false;
316         }
317
318         /* client just used a credit. */
319         SMB_ASSERT(sconn->smb2.credits_granted > 0);
320         sconn->smb2.credits_granted -= 1;
321
322         /* Mark the message_id as seen in the bitmap. */
323         bitmap_offset = (unsigned int)(message_id %
324                         (uint64_t)(sconn->smb2.max_credits * 2));
325         if (bitmap_query(credits_bm, bitmap_offset)) {
326                 DEBUG(0,("smb2_validate_message_id: duplicate message_id "
327                         "%llu (bm offset %u)\n",
328                         (unsigned long long)message_id,
329                         bitmap_offset));
330                 return false;
331         }
332         bitmap_set(credits_bm, bitmap_offset);
333
334         if (message_id == sconn->smb2.seqnum_low + 1) {
335                 /* Move the window forward by all the message_id's
336                    already seen. */
337                 while (bitmap_query(credits_bm, bitmap_offset)) {
338                         DEBUG(10,("smb2_validate_message_id: clearing "
339                                 "id %llu (position %u) from bitmap\n",
340                                 (unsigned long long)(sconn->smb2.seqnum_low + 1),
341                                 bitmap_offset ));
342                         bitmap_clear(credits_bm, bitmap_offset);
343                         sconn->smb2.seqnum_low += 1;
344                         bitmap_offset = (bitmap_offset + 1) %
345                                 (sconn->smb2.max_credits * 2);
346                 }
347         }
348
349         return true;
350 }
351
352 static NTSTATUS smbd_smb2_request_validate(struct smbd_smb2_request *req)
353 {
354         int count;
355         int idx;
356         bool compound_related = false;
357
358         count = req->in.vector_count;
359
360         if (count < 4) {
361                 /* It's not a SMB2 request */
362                 return NT_STATUS_INVALID_PARAMETER;
363         }
364
365         for (idx=1; idx < count; idx += 3) {
366                 const uint8_t *inhdr = NULL;
367                 uint32_t flags;
368
369                 if (req->in.vector[idx].iov_len != SMB2_HDR_BODY) {
370                         return NT_STATUS_INVALID_PARAMETER;
371                 }
372
373                 if (req->in.vector[idx+1].iov_len < 2) {
374                         return NT_STATUS_INVALID_PARAMETER;
375                 }
376
377                 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
378
379                 /* Check the SMB2 header */
380                 if (IVAL(inhdr, SMB2_HDR_PROTOCOL_ID) != SMB2_MAGIC) {
381                         return NT_STATUS_INVALID_PARAMETER;
382                 }
383
384                 if (!smb2_validate_message_id(req->sconn, inhdr)) {
385                         return NT_STATUS_INVALID_PARAMETER;
386                 }
387
388                 flags = IVAL(inhdr, SMB2_HDR_FLAGS);
389                 if (idx == 1) {
390                         /*
391                          * the 1st request should never have the
392                          * SMB2_HDR_FLAG_CHAINED flag set
393                          */
394                         if (flags & SMB2_HDR_FLAG_CHAINED) {
395                                 req->next_status = NT_STATUS_INVALID_PARAMETER;
396                                 return NT_STATUS_OK;
397                         }
398                 } else if (idx == 4) {
399                         /*
400                          * the 2nd request triggers related vs. unrelated
401                          * compounded requests
402                          */
403                         if (flags & SMB2_HDR_FLAG_CHAINED) {
404                                 compound_related = true;
405                         }
406                 } else if (idx > 4) {
407 #if 0
408                         /*
409                          * It seems the this tests are wrong
410                          * see the SMB2-COMPOUND test
411                          */
412
413                         /*
414                          * all other requests should match the 2nd one
415                          */
416                         if (flags & SMB2_HDR_FLAG_CHAINED) {
417                                 if (!compound_related) {
418                                         req->next_status =
419                                                 NT_STATUS_INVALID_PARAMETER;
420                                         return NT_STATUS_OK;
421                                 }
422                         } else {
423                                 if (compound_related) {
424                                         req->next_status =
425                                                 NT_STATUS_INVALID_PARAMETER;
426                                         return NT_STATUS_OK;
427                                 }
428                         }
429 #endif
430                 }
431         }
432
433         return NT_STATUS_OK;
434 }
435
436 static void smb2_set_operation_credit(struct smbd_server_connection *sconn,
437                         const struct iovec *in_vector,
438                         struct iovec *out_vector)
439 {
440         uint8_t *outhdr = out_vector->iov_base;
441         uint16_t credits_requested = 0;
442         uint16_t credits_granted = 0;
443
444         if (in_vector != NULL) {
445                 const uint8_t *inhdr = (const uint8_t *)in_vector->iov_base;
446                 credits_requested = SVAL(inhdr, SMB2_HDR_CREDIT);
447         }
448
449         SMB_ASSERT(sconn->smb2.max_credits >= sconn->smb2.credits_granted);
450
451         /* Remember what we gave out. */
452         credits_granted = MIN(credits_requested, (sconn->smb2.max_credits -
453                 sconn->smb2.credits_granted));
454
455         if (credits_granted == 0 && sconn->smb2.credits_granted == 0) {
456                 /* First negprot packet, or ensure the client credits can
457                    never drop to zero. */
458                 credits_granted = 1;
459         }
460
461         SSVAL(outhdr, SMB2_HDR_CREDIT, credits_granted);
462         sconn->smb2.credits_granted += credits_granted;
463
464         DEBUG(10,("smb2_set_operation_credit: requested %u, "
465                 "granted %u, total granted %u\n",
466                 (unsigned int)credits_requested,
467                 (unsigned int)credits_granted,
468                 (unsigned int)sconn->smb2.credits_granted ));
469 }
470
471 static void smb2_calculate_credits(const struct smbd_smb2_request *inreq,
472                                 struct smbd_smb2_request *outreq)
473 {
474         int count, idx;
475
476         count = outreq->out.vector_count;
477
478         for (idx=1; idx < count; idx += 3) {
479                 smb2_set_operation_credit(outreq->sconn,
480                         &inreq->in.vector[idx],
481                         &outreq->out.vector[idx]);
482         }
483 }
484
485 static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req)
486 {
487         struct iovec *vector;
488         int count;
489         int idx;
490
491         count = req->in.vector_count;
492         vector = talloc_zero_array(req, struct iovec, count);
493         if (vector == NULL) {
494                 return NT_STATUS_NO_MEMORY;
495         }
496
497         vector[0].iov_base      = req->out.nbt_hdr;
498         vector[0].iov_len       = 4;
499         SIVAL(req->out.nbt_hdr, 0, 0);
500
501         for (idx=1; idx < count; idx += 3) {
502                 const uint8_t *inhdr = NULL;
503                 uint32_t in_flags;
504                 uint8_t *outhdr = NULL;
505                 uint8_t *outbody = NULL;
506                 uint32_t next_command_ofs = 0;
507                 struct iovec *current = &vector[idx];
508
509                 if ((idx + 3) < count) {
510                         /* we have a next command -
511                          * setup for the error case. */
512                         next_command_ofs = SMB2_HDR_BODY + 9;
513                 }
514
515                 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
516                 in_flags = IVAL(inhdr, SMB2_HDR_FLAGS);
517
518                 outhdr = talloc_zero_array(vector, uint8_t,
519                                       OUTVEC_ALLOC_SIZE);
520                 if (outhdr == NULL) {
521                         return NT_STATUS_NO_MEMORY;
522                 }
523
524                 outbody = outhdr + SMB2_HDR_BODY;
525
526                 current[0].iov_base     = (void *)outhdr;
527                 current[0].iov_len      = SMB2_HDR_BODY;
528
529                 current[1].iov_base     = (void *)outbody;
530                 current[1].iov_len      = 8;
531
532                 current[2].iov_base     = NULL;
533                 current[2].iov_len      = 0;
534
535                 /* setup the SMB2 header */
536                 SIVAL(outhdr, SMB2_HDR_PROTOCOL_ID,     SMB2_MAGIC);
537                 SSVAL(outhdr, SMB2_HDR_LENGTH,          SMB2_HDR_BODY);
538                 SSVAL(outhdr, SMB2_HDR_EPOCH,           0);
539                 SIVAL(outhdr, SMB2_HDR_STATUS,
540                       NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));
541                 SSVAL(outhdr, SMB2_HDR_OPCODE,
542                       SVAL(inhdr, SMB2_HDR_OPCODE));
543                 SIVAL(outhdr, SMB2_HDR_FLAGS,
544                       IVAL(inhdr, SMB2_HDR_FLAGS) | SMB2_HDR_FLAG_REDIRECT);
545                 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND,    next_command_ofs);
546                 SBVAL(outhdr, SMB2_HDR_MESSAGE_ID,
547                       BVAL(inhdr, SMB2_HDR_MESSAGE_ID));
548                 SIVAL(outhdr, SMB2_HDR_PID,
549                       IVAL(inhdr, SMB2_HDR_PID));
550                 SIVAL(outhdr, SMB2_HDR_TID,
551                       IVAL(inhdr, SMB2_HDR_TID));
552                 SBVAL(outhdr, SMB2_HDR_SESSION_ID,
553                       BVAL(inhdr, SMB2_HDR_SESSION_ID));
554                 memset(outhdr + SMB2_HDR_SIGNATURE, 0, 16);
555
556                 /* setup error body header */
557                 SSVAL(outbody, 0x00, 0x08 + 1);
558                 SSVAL(outbody, 0x02, 0);
559                 SIVAL(outbody, 0x04, 0);
560         }
561
562         req->out.vector = vector;
563         req->out.vector_count = count;
564
565         /* setup the length of the NBT packet */
566         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
567
568         DLIST_ADD_END(req->sconn->smb2.requests, req, struct smbd_smb2_request *);
569
570         return NT_STATUS_OK;
571 }
572
573 void smbd_server_connection_terminate_ex(struct smbd_server_connection *sconn,
574                                          const char *reason,
575                                          const char *location)
576 {
577         DEBUG(10,("smbd_server_connection_terminate_ex: reason[%s] at %s\n",
578                   reason, location));
579         exit_server_cleanly(reason);
580 }
581
582 static bool dup_smb2_vec3(TALLOC_CTX *ctx,
583                         struct iovec *outvec,
584                         const struct iovec *srcvec)
585 {
586         /* vec[0] is always boilerplate and must
587          * be allocated with size OUTVEC_ALLOC_SIZE. */
588
589         outvec[0].iov_base = talloc_memdup(ctx,
590                                 srcvec[0].iov_base,
591                                 OUTVEC_ALLOC_SIZE);
592         if (!outvec[0].iov_base) {
593                 return false;
594         }
595         outvec[0].iov_len = SMB2_HDR_BODY;
596
597         /*
598          * If this is a "standard" vec[1] of length 8,
599          * pointing to srcvec[0].iov_base + SMB2_HDR_BODY,
600          * then duplicate this. Else use talloc_memdup().
601          */
602
603         if (srcvec[1].iov_len == 8 &&
604                         srcvec[1].iov_base ==
605                                 ((uint8_t *)srcvec[0].iov_base) +
606                                         SMB2_HDR_BODY) {
607                 outvec[1].iov_base = ((uint8_t *)outvec[1].iov_base) +
608                                         SMB2_HDR_BODY;
609                 outvec[1].iov_len = 8;
610         } else {
611                 outvec[1].iov_base = talloc_memdup(ctx,
612                                 srcvec[1].iov_base,
613                                 srcvec[1].iov_len);
614                 if (!outvec[1].iov_base) {
615                         return false;
616                 }
617                 outvec[1].iov_len = srcvec[1].iov_len;
618         }
619
620         /*
621          * If this is a "standard" vec[2] of length 1,
622          * pointing to srcvec[0].iov_base + (OUTVEC_ALLOC_SIZE - 1)
623          * then duplicate this. Else use talloc_memdup().
624          */
625
626         if (srcvec[2].iov_base &&
627                         srcvec[2].iov_len) {
628                 if (srcvec[2].iov_base ==
629                                 ((uint8_t *)srcvec[0].iov_base) +
630                                         (OUTVEC_ALLOC_SIZE - 1) &&
631                                 srcvec[2].iov_len == 1) {
632                         /* Common SMB2 error packet case. */
633                         outvec[2].iov_base = ((uint8_t *)outvec[0].iov_base) +
634                                 (OUTVEC_ALLOC_SIZE - 1);
635                 } else {
636                         outvec[2].iov_base = talloc_memdup(ctx,
637                                         srcvec[2].iov_base,
638                                         srcvec[2].iov_len);
639                         if (!outvec[2].iov_base) {
640                                 return false;
641                         }
642                 }
643                 outvec[2].iov_len = srcvec[2].iov_len;
644         } else {
645                 outvec[2].iov_base = NULL;
646                 outvec[2].iov_len = 0;
647         }
648         return true;
649 }
650
651 static struct smbd_smb2_request *dup_smb2_req(const struct smbd_smb2_request *req)
652 {
653         struct smbd_smb2_request *newreq = NULL;
654         struct iovec *outvec = NULL;
655         int count = req->out.vector_count;
656         int i;
657
658         newreq = smbd_smb2_request_allocate(req->sconn);
659         if (!newreq) {
660                 return NULL;
661         }
662
663         newreq->sconn = req->sconn;
664         newreq->do_signing = req->do_signing;
665         newreq->current_idx = req->current_idx;
666         newreq->async = false;
667         newreq->cancelled = false;
668
669         outvec = talloc_zero_array(newreq, struct iovec, count);
670         if (!outvec) {
671                 TALLOC_FREE(newreq);
672                 return NULL;
673         }
674         newreq->out.vector = outvec;
675         newreq->out.vector_count = count;
676
677         /* Setup the outvec's identically to req. */
678         outvec[0].iov_base = newreq->out.nbt_hdr;
679         outvec[0].iov_len = 4;
680         memcpy(newreq->out.nbt_hdr, req->out.nbt_hdr, 4);
681
682         /* Setup the vectors identically to the ones in req. */
683         for (i = 1; i < count; i += 3) {
684                 if (!dup_smb2_vec3(outvec, &outvec[i], &req->out.vector[i])) {
685                         break;
686                 }
687         }
688
689         if (i < count) {
690                 /* Alloc failed. */
691                 TALLOC_FREE(newreq);
692                 return NULL;
693         }
694
695         smb2_setup_nbt_length(newreq->out.vector,
696                 newreq->out.vector_count);
697
698         return newreq;
699 }
700
701 static void smbd_smb2_request_writev_done(struct tevent_req *subreq);
702
703 static NTSTATUS smb2_send_async_interim_response(const struct smbd_smb2_request *req)
704 {
705         int i = 0;
706         uint8_t *outhdr = NULL;
707         struct smbd_smb2_request *nreq = NULL;
708
709         /* Create a new smb2 request we'll use
710            for the interim return. */
711         nreq = dup_smb2_req(req);
712         if (!nreq) {
713                 return NT_STATUS_NO_MEMORY;
714         }
715
716         /* Lose the last 3 out vectors. They're the
717            ones we'll be using for the async reply. */
718         nreq->out.vector_count -= 3;
719
720         smb2_setup_nbt_length(nreq->out.vector,
721                 nreq->out.vector_count);
722
723         /* Step back to the previous reply. */
724         i = nreq->current_idx - 3;
725         outhdr = (uint8_t *)nreq->out.vector[i].iov_base;
726         /* And end the chain. */
727         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
728
729         /* Calculate outgoing credits */
730         smb2_calculate_credits(req, nreq);
731
732         /* Re-sign if needed. */
733         if (nreq->do_signing) {
734                 NTSTATUS status;
735                 status = smb2_signing_sign_pdu(nreq->session->session_key,
736                                         &nreq->out.vector[i], 3);
737                 if (!NT_STATUS_IS_OK(status)) {
738                         return status;
739                 }
740         }
741         if (DEBUGLEVEL >= 10) {
742                 dbgtext("smb2_send_async_interim_response: nreq->current_idx = %u\n",
743                         (unsigned int)nreq->current_idx );
744                 dbgtext("smb2_send_async_interim_response: returning %u vectors\n",
745                         (unsigned int)nreq->out.vector_count );
746                 print_req_vectors(nreq);
747         }
748         nreq->subreq = tstream_writev_queue_send(nreq,
749                                         nreq->sconn->smb2.event_ctx,
750                                         nreq->sconn->smb2.stream,
751                                         nreq->sconn->smb2.send_queue,
752                                         nreq->out.vector,
753                                         nreq->out.vector_count);
754
755         if (nreq->subreq == NULL) {
756                 return NT_STATUS_NO_MEMORY;
757         }
758
759         tevent_req_set_callback(nreq->subreq,
760                         smbd_smb2_request_writev_done,
761                         nreq);
762
763         return NT_STATUS_OK;
764 }
765
766 struct smbd_smb2_request_pending_state {
767         struct smbd_server_connection *sconn;
768         uint8_t buf[4 + SMB2_HDR_BODY + 0x08 + 1];
769         struct iovec vector[3];
770 };
771
772 static void smbd_smb2_request_pending_writev_done(struct tevent_req *subreq)
773 {
774         struct smbd_smb2_request_pending_state *state =
775                 tevent_req_callback_data(subreq,
776                         struct smbd_smb2_request_pending_state);
777         struct smbd_server_connection *sconn = state->sconn;
778         int ret;
779         int sys_errno;
780
781         ret = tstream_writev_queue_recv(subreq, &sys_errno);
782         TALLOC_FREE(subreq);
783         if (ret == -1) {
784                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
785                 smbd_server_connection_terminate(sconn, nt_errstr(status));
786                 return;
787         }
788
789         TALLOC_FREE(state);
790 }
791
792 NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
793                                          struct tevent_req *subreq)
794 {
795         NTSTATUS status;
796         struct smbd_smb2_request_pending_state *state = NULL;
797         int i = req->current_idx;
798         uint8_t *reqhdr = NULL;
799         uint8_t *hdr = NULL;
800         uint8_t *body = NULL;
801         uint32_t flags = 0;
802         uint64_t message_id = 0;
803         uint64_t async_id = 0;
804         struct iovec *outvec = NULL;
805
806         if (!tevent_req_is_in_progress(subreq)) {
807                 return NT_STATUS_OK;
808         }
809
810         req->subreq = subreq;
811         subreq = NULL;
812
813         if (req->async) {
814                 /* We're already async. */
815                 return NT_STATUS_OK;
816         }
817
818         if (req->in.vector_count > i + 3) {
819                 /*
820                  * We're trying to go async in a compound
821                  * request chain. This is not allowed.
822                  * Cancel the outstanding request.
823                  */
824                 tevent_req_cancel(req->subreq);
825                 return smbd_smb2_request_error(req,
826                         NT_STATUS_INSUFFICIENT_RESOURCES);
827         }
828
829         if (DEBUGLEVEL >= 10) {
830                 dbgtext("smbd_smb2_request_pending_queue: req->current_idx = %u\n",
831                         (unsigned int)req->current_idx );
832                 print_req_vectors(req);
833         }
834
835         if (req->out.vector_count > 4) {
836                 /* This is a compound reply. We
837                  * must do an interim response
838                  * followed by the async response
839                  * to match W2K8R2.
840                  */
841                 status = smb2_send_async_interim_response(req);
842                 if (!NT_STATUS_IS_OK(status)) {
843                         return status;
844                 }
845         }
846
847         /* Don't return an intermediate packet on a pipe read/write. */
848         if (req->tcon && req->tcon->compat_conn && IS_IPC(req->tcon->compat_conn)) {
849                 return NT_STATUS_OK;
850         }
851
852         reqhdr = (uint8_t *)req->out.vector[i].iov_base;
853         flags = (IVAL(reqhdr, SMB2_HDR_FLAGS) & ~SMB2_HDR_FLAG_CHAINED);
854         message_id = BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
855         async_id = message_id; /* keep it simple for now... */
856
857         /*
858          * What we send is identical to a smbd_smb2_request_error
859          * packet with an error status of STATUS_PENDING. Make use
860          * of this fact sometime when refactoring. JRA.
861          */
862
863         state = talloc_zero(req->sconn, struct smbd_smb2_request_pending_state);
864         if (state == NULL) {
865                 return NT_STATUS_NO_MEMORY;
866         }
867         state->sconn = req->sconn;
868
869         state->vector[0].iov_base = (void *)state->buf;
870         state->vector[0].iov_len = 4;
871
872         state->vector[1].iov_base = state->buf + 4;
873         state->vector[1].iov_len = SMB2_HDR_BODY;
874
875         state->vector[2].iov_base = state->buf + 4 + SMB2_HDR_BODY;
876         state->vector[2].iov_len = 9;
877
878         smb2_setup_nbt_length(state->vector, 3);
879
880         hdr = (uint8_t *)state->vector[1].iov_base;
881         body = (uint8_t *)state->vector[2].iov_base;
882
883         SIVAL(hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
884         SSVAL(hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
885         SSVAL(hdr, SMB2_HDR_EPOCH, 0);
886         SIVAL(hdr, SMB2_HDR_STATUS, NT_STATUS_V(STATUS_PENDING));
887         SSVAL(hdr, SMB2_HDR_OPCODE, SVAL(reqhdr, SMB2_HDR_OPCODE));
888
889         SIVAL(hdr, SMB2_HDR_FLAGS, flags | SMB2_HDR_FLAG_ASYNC);
890         SIVAL(hdr, SMB2_HDR_NEXT_COMMAND, 0);
891         SBVAL(hdr, SMB2_HDR_MESSAGE_ID, message_id);
892         SBVAL(hdr, SMB2_HDR_PID, async_id);
893         SBVAL(hdr, SMB2_HDR_SESSION_ID,
894                 BVAL(reqhdr, SMB2_HDR_SESSION_ID));
895         memset(hdr+SMB2_HDR_SIGNATURE, 0, 16);
896
897         SSVAL(body, 0x00, 0x08 + 1);
898
899         SCVAL(body, 0x02, 0);
900         SCVAL(body, 0x03, 0);
901         SIVAL(body, 0x04, 0);
902         /* Match W2K8R2... */
903         SCVAL(body, 0x08, 0x21);
904
905         /* Ensure we correctly go through crediting. Grant
906            the credits now, and zero credits on the final
907            response. */
908         smb2_set_operation_credit(req->sconn,
909                         &req->in.vector[i],
910                         &state->vector[1]);
911
912         if (req->do_signing) {
913                 status = smb2_signing_sign_pdu(req->session->session_key,
914                                         state->vector, 3);
915                 if (!NT_STATUS_IS_OK(status)) {
916                         return status;
917                 }
918         }
919
920         subreq = tstream_writev_queue_send(state,
921                                         req->sconn->smb2.event_ctx,
922                                         req->sconn->smb2.stream,
923                                         req->sconn->smb2.send_queue,
924                                         state->vector,
925                                         3);
926
927         if (subreq == NULL) {
928                 return NT_STATUS_NO_MEMORY;
929         }
930
931         tevent_req_set_callback(subreq,
932                         smbd_smb2_request_pending_writev_done,
933                         state);
934
935         /* Note we're going async with this request. */
936         req->async = true;
937
938         /*
939          * Now manipulate req so that the outstanding async request
940          * is the only one left in the struct smbd_smb2_request.
941          */
942
943         if (req->current_idx == 1) {
944                 /* There was only one. */
945                 goto out;
946         }
947
948         /* Re-arrange the in.vectors. */
949         req->in.vector[1] = req->in.vector[i];
950         req->in.vector[2] = req->in.vector[i+1];
951         req->in.vector[3] = req->in.vector[i+2];
952         req->in.vector_count = 4;
953         /* Reset the new in size. */
954         smb2_setup_nbt_length(req->in.vector, 4);
955
956         /* Now recreate the out.vectors. */
957         outvec = talloc_zero_array(req, struct iovec, 4);
958         if (!outvec) {
959                 return NT_STATUS_NO_MEMORY;
960         }
961
962         /* 0 is always boilerplate and must
963          * be of size 4 for the length field. */
964
965         outvec[0].iov_base = req->out.nbt_hdr;
966         outvec[0].iov_len = 4;
967         SIVAL(req->out.nbt_hdr, 0, 0);
968
969         if (!dup_smb2_vec3(outvec, &outvec[1], &req->out.vector[i])) {
970                 return NT_STATUS_NO_MEMORY;
971         }
972
973         TALLOC_FREE(req->out.vector);
974
975         req->out.vector = outvec;
976
977         req->current_idx = 1;
978         req->out.vector_count = 4;
979
980   out:
981
982         smb2_setup_nbt_length(req->out.vector,
983                 req->out.vector_count);
984
985         /* Ensure our final reply matches the interim one. */
986         reqhdr = (uint8_t *)req->out.vector[1].iov_base;
987         SIVAL(reqhdr, SMB2_HDR_FLAGS, flags | SMB2_HDR_FLAG_ASYNC);
988         SBVAL(reqhdr, SMB2_HDR_PID, async_id);
989
990         {
991                 const uint8_t *inhdr =
992                         (const uint8_t *)req->in.vector[1].iov_base;
993                 DEBUG(10,("smbd_smb2_request_pending_queue: opcode[%s] mid %llu "
994                         "going async\n",
995                         smb2_opcode_name((uint16_t)IVAL(inhdr, SMB2_HDR_OPCODE)),
996                         (unsigned long long)async_id ));
997         }
998         return NT_STATUS_OK;
999 }
1000
1001 static NTSTATUS smbd_smb2_request_process_cancel(struct smbd_smb2_request *req)
1002 {
1003         struct smbd_server_connection *sconn = req->sconn;
1004         struct smbd_smb2_request *cur;
1005         const uint8_t *inhdr;
1006         int i = req->current_idx;
1007         uint32_t flags;
1008         uint64_t search_message_id;
1009         uint64_t search_async_id;
1010         uint64_t found_id;
1011
1012         inhdr = (const uint8_t *)req->in.vector[i].iov_base;
1013
1014         flags = IVAL(inhdr, SMB2_HDR_FLAGS);
1015         search_message_id = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
1016         search_async_id = BVAL(inhdr, SMB2_HDR_PID);
1017
1018         /*
1019          * we don't need the request anymore
1020          * cancel requests never have a response
1021          */
1022         DLIST_REMOVE(req->sconn->smb2.requests, req);
1023         TALLOC_FREE(req);
1024
1025         for (cur = sconn->smb2.requests; cur; cur = cur->next) {
1026                 const uint8_t *outhdr;
1027                 uint64_t message_id;
1028                 uint64_t async_id;
1029
1030                 i = cur->current_idx;
1031
1032                 outhdr = (const uint8_t *)cur->out.vector[i].iov_base;
1033
1034                 message_id = BVAL(outhdr, SMB2_HDR_MESSAGE_ID);
1035                 async_id = BVAL(outhdr, SMB2_HDR_PID);
1036
1037                 if (flags & SMB2_HDR_FLAG_ASYNC) {
1038                         if (search_async_id == async_id) {
1039                                 found_id = async_id;
1040                                 break;
1041                         }
1042                 } else {
1043                         if (search_message_id == message_id) {
1044                                 found_id = message_id;
1045                                 break;
1046                         }
1047                 }
1048         }
1049
1050         if (cur && cur->subreq) {
1051                 inhdr = (const uint8_t *)cur->in.vector[i].iov_base;
1052                 DEBUG(10,("smbd_smb2_request_process_cancel: attempting to "
1053                         "cancel opcode[%s] mid %llu\n",
1054                         smb2_opcode_name((uint16_t)IVAL(inhdr, SMB2_HDR_OPCODE)),
1055                         (unsigned long long)found_id ));
1056                 tevent_req_cancel(cur->subreq);
1057         }
1058
1059         return NT_STATUS_OK;
1060 }
1061
1062 NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
1063 {
1064         const uint8_t *inhdr;
1065         int i = req->current_idx;
1066         uint16_t opcode;
1067         uint32_t flags;
1068         uint64_t mid;
1069         NTSTATUS status;
1070         NTSTATUS session_status;
1071         uint32_t allowed_flags;
1072         NTSTATUS return_value;
1073
1074         inhdr = (const uint8_t *)req->in.vector[i].iov_base;
1075
1076         /* TODO: verify more things */
1077
1078         flags = IVAL(inhdr, SMB2_HDR_FLAGS);
1079         opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
1080         mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
1081         DEBUG(10,("smbd_smb2_request_dispatch: opcode[%s] mid = %llu\n",
1082                 smb2_opcode_name(opcode),
1083                 (unsigned long long)mid));
1084
1085         allowed_flags = SMB2_HDR_FLAG_CHAINED |
1086                         SMB2_HDR_FLAG_SIGNED |
1087                         SMB2_HDR_FLAG_DFS;
1088         if (opcode == SMB2_OP_CANCEL) {
1089                 allowed_flags |= SMB2_HDR_FLAG_ASYNC;
1090         }
1091         if ((flags & ~allowed_flags) != 0) {
1092                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
1093         }
1094
1095         session_status = smbd_smb2_request_check_session(req);
1096
1097         req->do_signing = false;
1098         if (flags & SMB2_HDR_FLAG_SIGNED) {
1099                 if (!NT_STATUS_IS_OK(session_status)) {
1100                         return smbd_smb2_request_error(req, session_status);
1101                 }
1102
1103                 req->do_signing = true;
1104                 status = smb2_signing_check_pdu(req->session->session_key,
1105                                                 &req->in.vector[i], 3);
1106                 if (!NT_STATUS_IS_OK(status)) {
1107                         return smbd_smb2_request_error(req, status);
1108                 }
1109         } else if (req->session && req->session->do_signing) {
1110                 return smbd_smb2_request_error(req, NT_STATUS_ACCESS_DENIED);
1111         }
1112
1113         if (flags & SMB2_HDR_FLAG_CHAINED) {
1114                 /*
1115                  * This check is mostly for giving the correct error code
1116                  * for compounded requests.
1117                  *
1118                  * TODO: we may need to move this after the session
1119                  *       and tcon checks.
1120                  */
1121                 if (!NT_STATUS_IS_OK(req->next_status)) {
1122                         return smbd_smb2_request_error(req, req->next_status);
1123                 }
1124         } else {
1125                 req->compat_chain_fsp = NULL;
1126         }
1127
1128         switch (opcode) {
1129         case SMB2_OP_NEGPROT:
1130                 {
1131                         START_PROFILE(smb2_negprot);
1132                         return_value = smbd_smb2_request_process_negprot(req);
1133                         END_PROFILE(smb2_negprot);
1134                 }
1135                 break;
1136
1137         case SMB2_OP_SESSSETUP:
1138                 {
1139                         START_PROFILE(smb2_sesssetup);
1140                         return_value = smbd_smb2_request_process_sesssetup(req);
1141                         END_PROFILE(smb2_sesssetup);
1142                 }
1143                 break;
1144
1145         case SMB2_OP_LOGOFF:
1146                 if (!NT_STATUS_IS_OK(session_status)) {
1147                         return_value = smbd_smb2_request_error(req, session_status);
1148                         break;
1149                 }
1150
1151                 {
1152                         START_PROFILE(smb2_logoff);
1153                         return_value = smbd_smb2_request_process_logoff(req);
1154                         END_PROFILE(smb2_logoff);
1155                 }
1156                 break;
1157
1158         case SMB2_OP_TCON:
1159                 if (!NT_STATUS_IS_OK(session_status)) {
1160                         return_value = smbd_smb2_request_error(req, session_status);
1161                         break;
1162                 }
1163                 status = smbd_smb2_request_check_session(req);
1164                 if (!NT_STATUS_IS_OK(status)) {
1165                         return_value = smbd_smb2_request_error(req, status);
1166                         break;
1167                 }
1168
1169                 {
1170                         START_PROFILE(smb2_tcon);
1171                         return_value = smbd_smb2_request_process_tcon(req);
1172                         END_PROFILE(smb2_tcon);
1173                 }
1174                 break;
1175
1176         case SMB2_OP_TDIS:
1177                 if (!NT_STATUS_IS_OK(session_status)) {
1178                         return_value = smbd_smb2_request_error(req, session_status);
1179                         break;
1180                 }
1181                 status = smbd_smb2_request_check_tcon(req);
1182                 if (!NT_STATUS_IS_OK(status)) {
1183                         return_value = smbd_smb2_request_error(req, status);
1184                         break;
1185                 }
1186
1187                 {
1188                         START_PROFILE(smb2_tdis);
1189                         return_value = smbd_smb2_request_process_tdis(req);
1190                         END_PROFILE(smb2_tdis);
1191                 }
1192                 break;
1193
1194         case SMB2_OP_CREATE:
1195                 if (!NT_STATUS_IS_OK(session_status)) {
1196                         return_value = smbd_smb2_request_error(req, session_status);
1197                         break;
1198                 }
1199                 status = smbd_smb2_request_check_tcon(req);
1200                 if (!NT_STATUS_IS_OK(status)) {
1201                         return_value = smbd_smb2_request_error(req, status);
1202                         break;
1203                 }
1204
1205                 {
1206                         START_PROFILE(smb2_create);
1207                         return_value = smbd_smb2_request_process_create(req);
1208                         END_PROFILE(smb2_create);
1209                 }
1210                 break;
1211
1212         case SMB2_OP_CLOSE:
1213                 if (!NT_STATUS_IS_OK(session_status)) {
1214                         return_value = smbd_smb2_request_error(req, session_status);
1215                         break;
1216                 }
1217                 status = smbd_smb2_request_check_tcon(req);
1218                 if (!NT_STATUS_IS_OK(status)) {
1219                         return_value = smbd_smb2_request_error(req, status);
1220                         break;
1221                 }
1222
1223                 {
1224                         START_PROFILE(smb2_close);
1225                         return_value = smbd_smb2_request_process_close(req);
1226                         END_PROFILE(smb2_close);
1227                 }
1228                 break;
1229
1230         case SMB2_OP_FLUSH:
1231                 if (!NT_STATUS_IS_OK(session_status)) {
1232                         return_value = smbd_smb2_request_error(req, session_status);
1233                         break;
1234                 }
1235                 status = smbd_smb2_request_check_tcon(req);
1236                 if (!NT_STATUS_IS_OK(status)) {
1237                         return_value = smbd_smb2_request_error(req, status);
1238                         break;
1239                 }
1240
1241                 {
1242                         START_PROFILE(smb2_flush);
1243                         return_value = smbd_smb2_request_process_flush(req);
1244                         END_PROFILE(smb2_flush);
1245                 }
1246                 break;
1247
1248         case SMB2_OP_READ:
1249                 if (!NT_STATUS_IS_OK(session_status)) {
1250                         return_value = smbd_smb2_request_error(req, session_status);
1251                         break;
1252                 }
1253                 status = smbd_smb2_request_check_tcon(req);
1254                 if (!NT_STATUS_IS_OK(status)) {
1255                         return_value = smbd_smb2_request_error(req, status);
1256                         break;
1257                 }
1258
1259                 {
1260                         START_PROFILE(smb2_read);
1261                         return_value = smbd_smb2_request_process_read(req);
1262                         END_PROFILE(smb2_read);
1263                 }
1264                 break;
1265
1266         case SMB2_OP_WRITE:
1267                 if (!NT_STATUS_IS_OK(session_status)) {
1268                         return_value = smbd_smb2_request_error(req, session_status);
1269                         break;
1270                 }
1271                 status = smbd_smb2_request_check_tcon(req);
1272                 if (!NT_STATUS_IS_OK(status)) {
1273                         return_value = smbd_smb2_request_error(req, status);
1274                         break;
1275                 }
1276
1277                 {
1278                         START_PROFILE(smb2_write);
1279                         return_value = smbd_smb2_request_process_write(req);
1280                         END_PROFILE(smb2_write);
1281                 }
1282                 break;
1283
1284         case SMB2_OP_LOCK:
1285                 if (!NT_STATUS_IS_OK(session_status)) {
1286                         /* Too ugly to live ? JRA. */
1287                         if (NT_STATUS_EQUAL(session_status,NT_STATUS_USER_SESSION_DELETED)) {
1288                                 session_status = NT_STATUS_FILE_CLOSED;
1289                         }
1290                         return_value = smbd_smb2_request_error(req, session_status);
1291                         break;
1292                 }
1293                 status = smbd_smb2_request_check_tcon(req);
1294                 if (!NT_STATUS_IS_OK(status)) {
1295                         /* Too ugly to live ? JRA. */
1296                         if (NT_STATUS_EQUAL(status,NT_STATUS_NETWORK_NAME_DELETED)) {
1297                                 status = NT_STATUS_FILE_CLOSED;
1298                         }
1299                         return_value = smbd_smb2_request_error(req, status);
1300                         break;
1301                 }
1302
1303                 {
1304                         START_PROFILE(smb2_lock);
1305                         return_value = smbd_smb2_request_process_lock(req);
1306                         END_PROFILE(smb2_lock);
1307                 }
1308                 break;
1309
1310         case SMB2_OP_IOCTL:
1311                 if (!NT_STATUS_IS_OK(session_status)) {
1312                         return_value = smbd_smb2_request_error(req, session_status);
1313                         break;
1314                 }
1315                 status = smbd_smb2_request_check_tcon(req);
1316                 if (!NT_STATUS_IS_OK(status)) {
1317                         return_value = smbd_smb2_request_error(req, status);
1318                         break;
1319                 }
1320
1321                 {
1322                         START_PROFILE(smb2_ioctl);
1323                         return_value = smbd_smb2_request_process_ioctl(req);
1324                         END_PROFILE(smb2_ioctl);
1325                 }
1326                 break;
1327
1328         case SMB2_OP_CANCEL:
1329                 {
1330                         START_PROFILE(smb2_cancel);
1331                         return_value = smbd_smb2_request_process_cancel(req);
1332                         END_PROFILE(smb2_cancel);
1333                 }
1334                 break;
1335
1336         case SMB2_OP_KEEPALIVE:
1337                 {START_PROFILE(smb2_keepalive);
1338                 return_value = smbd_smb2_request_process_keepalive(req);
1339                 END_PROFILE(smb2_keepalive);}
1340                 break;
1341
1342         case SMB2_OP_FIND:
1343                 if (!NT_STATUS_IS_OK(session_status)) {
1344                         return_value = smbd_smb2_request_error(req, session_status);
1345                         break;
1346                 }
1347                 status = smbd_smb2_request_check_tcon(req);
1348                 if (!NT_STATUS_IS_OK(status)) {
1349                         return_value = smbd_smb2_request_error(req, status);
1350                         break;
1351                 }
1352
1353                 {
1354                         START_PROFILE(smb2_find);
1355                         return_value = smbd_smb2_request_process_find(req);
1356                         END_PROFILE(smb2_find);
1357                 }
1358                 break;
1359
1360         case SMB2_OP_NOTIFY:
1361                 if (!NT_STATUS_IS_OK(session_status)) {
1362                         return_value = smbd_smb2_request_error(req, session_status);
1363                         break;
1364                 }
1365                 status = smbd_smb2_request_check_tcon(req);
1366                 if (!NT_STATUS_IS_OK(status)) {
1367                         return_value = smbd_smb2_request_error(req, status);
1368                         break;
1369                 }
1370
1371                 {
1372                         START_PROFILE(smb2_notify);
1373                         return_value = smbd_smb2_request_process_notify(req);
1374                         END_PROFILE(smb2_notify);
1375                 }
1376                 break;
1377
1378         case SMB2_OP_GETINFO:
1379                 if (!NT_STATUS_IS_OK(session_status)) {
1380                         return_value = smbd_smb2_request_error(req, session_status);
1381                         break;
1382                 }
1383                 status = smbd_smb2_request_check_tcon(req);
1384                 if (!NT_STATUS_IS_OK(status)) {
1385                         return_value = smbd_smb2_request_error(req, status);
1386                         break;
1387                 }
1388
1389                 {
1390                         START_PROFILE(smb2_getinfo);
1391                         return_value = smbd_smb2_request_process_getinfo(req);
1392                         END_PROFILE(smb2_getinfo);
1393                 }
1394                 break;
1395
1396         case SMB2_OP_SETINFO:
1397                 if (!NT_STATUS_IS_OK(session_status)) {
1398                         return_value = smbd_smb2_request_error(req, session_status);
1399                         break;
1400                 }
1401                 status = smbd_smb2_request_check_tcon(req);
1402                 if (!NT_STATUS_IS_OK(status)) {
1403                         return_value = smbd_smb2_request_error(req, status);
1404                         break;
1405                 }
1406
1407                 {
1408                         START_PROFILE(smb2_setinfo);
1409                         return_value = smbd_smb2_request_process_setinfo(req);
1410                         END_PROFILE(smb2_setinfo);
1411                 }
1412                 break;
1413
1414         case SMB2_OP_BREAK:
1415                 if (!NT_STATUS_IS_OK(session_status)) {
1416                         return_value = smbd_smb2_request_error(req, session_status);
1417                         break;
1418                 }
1419                 status = smbd_smb2_request_check_tcon(req);
1420                 if (!NT_STATUS_IS_OK(status)) {
1421                         return_value = smbd_smb2_request_error(req, status);
1422                         break;
1423                 }
1424
1425                 {
1426                         START_PROFILE(smb2_break);
1427                         return_value = smbd_smb2_request_process_break(req);
1428                         END_PROFILE(smb2_break);
1429                 }
1430                 break;
1431
1432         default:
1433                 return_value = smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
1434                 break;
1435         }
1436         return return_value;
1437 }
1438
1439 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
1440 {
1441         struct tevent_req *subreq;
1442         int i = req->current_idx;
1443
1444         req->subreq = NULL;
1445
1446         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
1447
1448         /* Set credit for this operation (zero credits if this
1449            is a final reply for an async operation). */
1450         smb2_set_operation_credit(req->sconn,
1451                         req->async ? NULL : &req->in.vector[i],
1452                         &req->out.vector[i]);
1453
1454         if (req->do_signing) {
1455                 NTSTATUS status;
1456                 status = smb2_signing_sign_pdu(req->session->session_key,
1457                                                &req->out.vector[i], 3);
1458                 if (!NT_STATUS_IS_OK(status)) {
1459                         return status;
1460                 }
1461         }
1462
1463         req->current_idx += 3;
1464
1465         if (req->current_idx < req->out.vector_count) {
1466                 /*
1467                  * We must process the remaining compound
1468                  * SMB2 requests before any new incoming SMB2
1469                  * requests. This is because incoming SMB2
1470                  * requests may include a cancel for a
1471                  * compound request we haven't processed
1472                  * yet.
1473                  */
1474                 struct tevent_immediate *im = tevent_create_immediate(req);
1475                 if (!im) {
1476                         return NT_STATUS_NO_MEMORY;
1477                 }
1478                 tevent_schedule_immediate(im,
1479                                         req->sconn->smb2.event_ctx,
1480                                         smbd_smb2_request_dispatch_immediate,
1481                                         req);
1482                 return NT_STATUS_OK;
1483         }
1484
1485         if (DEBUGLEVEL >= 10) {
1486                 dbgtext("smbd_smb2_request_reply: sending...\n");
1487                 print_req_vectors(req);
1488         }
1489
1490         subreq = tstream_writev_queue_send(req,
1491                                            req->sconn->smb2.event_ctx,
1492                                            req->sconn->smb2.stream,
1493                                            req->sconn->smb2.send_queue,
1494                                            req->out.vector,
1495                                            req->out.vector_count);
1496         if (subreq == NULL) {
1497                 return NT_STATUS_NO_MEMORY;
1498         }
1499         tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
1500         /*
1501          * We're done with this request -
1502          * move it off the "being processed" queue.
1503          */
1504         DLIST_REMOVE(req->sconn->smb2.requests, req);
1505
1506         return NT_STATUS_OK;
1507 }
1508
1509 void smbd_smb2_request_dispatch_immediate(struct tevent_context *ctx,
1510                                         struct tevent_immediate *im,
1511                                         void *private_data)
1512 {
1513         struct smbd_smb2_request *req = talloc_get_type_abort(private_data,
1514                                         struct smbd_smb2_request);
1515         struct smbd_server_connection *sconn = req->sconn;
1516         NTSTATUS status;
1517
1518         TALLOC_FREE(im);
1519
1520         if (DEBUGLEVEL >= 10) {
1521                 DEBUG(10,("smbd_smb2_request_dispatch_immediate: idx[%d] of %d vectors\n",
1522                         req->current_idx, req->in.vector_count));
1523                 print_req_vectors(req);
1524         }
1525
1526         status = smbd_smb2_request_dispatch(req);
1527         if (!NT_STATUS_IS_OK(status)) {
1528                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1529                 return;
1530         }
1531 }
1532
1533 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
1534 {
1535         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
1536                                         struct smbd_smb2_request);
1537         struct smbd_server_connection *sconn = req->sconn;
1538         int ret;
1539         int sys_errno;
1540
1541         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1542         TALLOC_FREE(subreq);
1543         TALLOC_FREE(req);
1544         if (ret == -1) {
1545                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1546                 DEBUG(2,("smbd_smb2_request_writev_done: client write error %s\n",
1547                         nt_errstr(status)));
1548                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1549                 return;
1550         }
1551 }
1552
1553 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
1554                                    NTSTATUS status,
1555                                    DATA_BLOB body, DATA_BLOB *dyn,
1556                                    const char *location)
1557 {
1558         uint8_t *outhdr;
1559         int i = req->current_idx;
1560         uint32_t next_command_ofs;
1561
1562         DEBUG(10,("smbd_smb2_request_done_ex: "
1563                   "idx[%d] status[%s] body[%u] dyn[%s:%u] at %s\n",
1564                   i, nt_errstr(status), (unsigned int)body.length,
1565                   dyn ? "yes": "no",
1566                   (unsigned int)(dyn ? dyn->length : 0),
1567                   location));
1568
1569         if (body.length < 2) {
1570                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1571         }
1572
1573         if ((body.length % 2) != 0) {
1574                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1575         }
1576
1577         outhdr = (uint8_t *)req->out.vector[i].iov_base;
1578
1579         next_command_ofs = IVAL(outhdr, SMB2_HDR_NEXT_COMMAND);
1580         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
1581
1582         req->out.vector[i+1].iov_base = (void *)body.data;
1583         req->out.vector[i+1].iov_len = body.length;
1584
1585         if (dyn) {
1586                 req->out.vector[i+2].iov_base   = (void *)dyn->data;
1587                 req->out.vector[i+2].iov_len    = dyn->length;
1588         } else {
1589                 req->out.vector[i+2].iov_base = NULL;
1590                 req->out.vector[i+2].iov_len = 0;
1591         }
1592
1593         /* see if we need to recalculate the offset to the next response */
1594         if (next_command_ofs > 0) {
1595                 next_command_ofs  = SMB2_HDR_BODY;
1596                 next_command_ofs += req->out.vector[i+1].iov_len;
1597                 next_command_ofs += req->out.vector[i+2].iov_len;
1598         }
1599
1600         if ((next_command_ofs % 8) != 0) {
1601                 size_t pad_size = 8 - (next_command_ofs % 8);
1602                 if (req->out.vector[i+2].iov_len == 0) {
1603                         /*
1604                          * if the dyn buffer is empty
1605                          * we can use it to add padding
1606                          */
1607                         uint8_t *pad;
1608
1609                         pad = talloc_zero_array(req->out.vector,
1610                                                 uint8_t, pad_size);
1611                         if (pad == NULL) {
1612                                 return smbd_smb2_request_error(req,
1613                                                 NT_STATUS_NO_MEMORY);
1614                         }
1615
1616                         req->out.vector[i+2].iov_base = (void *)pad;
1617                         req->out.vector[i+2].iov_len = pad_size;
1618                 } else {
1619                         /*
1620                          * For now we copy the dynamic buffer
1621                          * and add the padding to the new buffer
1622                          */
1623                         size_t old_size;
1624                         uint8_t *old_dyn;
1625                         size_t new_size;
1626                         uint8_t *new_dyn;
1627
1628                         old_size = req->out.vector[i+2].iov_len;
1629                         old_dyn = (uint8_t *)req->out.vector[i+2].iov_base;
1630
1631                         new_size = old_size + pad_size;
1632                         new_dyn = talloc_zero_array(req->out.vector,
1633                                                uint8_t, new_size);
1634                         if (new_dyn == NULL) {
1635                                 return smbd_smb2_request_error(req,
1636                                                 NT_STATUS_NO_MEMORY);
1637                         }
1638
1639                         memcpy(new_dyn, old_dyn, old_size);
1640                         memset(new_dyn + old_size, 0, pad_size);
1641
1642                         req->out.vector[i+2].iov_base = (void *)new_dyn;
1643                         req->out.vector[i+2].iov_len = new_size;
1644                 }
1645                 next_command_ofs += pad_size;
1646         }
1647
1648         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
1649
1650         return smbd_smb2_request_reply(req);
1651 }
1652
1653 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
1654                                     NTSTATUS status,
1655                                     DATA_BLOB *info,
1656                                     const char *location)
1657 {
1658         DATA_BLOB body;
1659         int i = req->current_idx;
1660         uint8_t *outhdr = (uint8_t *)req->out.vector[i].iov_base;
1661
1662         DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s] |%s| at %s\n",
1663                   i, nt_errstr(status), info ? " +info" : "",
1664                   location));
1665
1666         body.data = outhdr + SMB2_HDR_BODY;
1667         body.length = 8;
1668         SSVAL(body.data, 0, 9);
1669
1670         if (info) {
1671                 SIVAL(body.data, 0x04, info->length);
1672         } else {
1673                 /* Allocated size of req->out.vector[i].iov_base
1674                  * *MUST BE* OUTVEC_ALLOC_SIZE. So we have room for
1675                  * 1 byte without having to do an alloc.
1676                  */
1677                 info = talloc_zero_array(req->out.vector,
1678                                         DATA_BLOB,
1679                                         1);
1680                 if (!info) {
1681                         return NT_STATUS_NO_MEMORY;
1682                 }
1683                 info->data = ((uint8_t *)outhdr) +
1684                         OUTVEC_ALLOC_SIZE - 1;
1685                 info->length = 1;
1686                 SCVAL(info->data, 0, 0);
1687         }
1688
1689         /*
1690          * if a request fails, all other remaining
1691          * compounded requests should fail too
1692          */
1693         req->next_status = NT_STATUS_INVALID_PARAMETER;
1694
1695         return smbd_smb2_request_done_ex(req, status, body, info, __location__);
1696 }
1697
1698
1699 struct smbd_smb2_send_oplock_break_state {
1700         struct smbd_server_connection *sconn;
1701         uint8_t buf[4 + SMB2_HDR_BODY + 0x18];
1702         struct iovec vector;
1703 };
1704
1705 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq);
1706
1707 NTSTATUS smbd_smb2_send_oplock_break(struct smbd_server_connection *sconn,
1708                                      uint64_t file_id_persistent,
1709                                      uint64_t file_id_volatile,
1710                                      uint8_t oplock_level)
1711 {
1712         struct smbd_smb2_send_oplock_break_state *state;
1713         struct tevent_req *subreq;
1714         uint8_t *hdr;
1715         uint8_t *body;
1716
1717         state = talloc(sconn, struct smbd_smb2_send_oplock_break_state);
1718         if (state == NULL) {
1719                 return NT_STATUS_NO_MEMORY;
1720         }
1721         state->sconn = sconn;
1722
1723         state->vector.iov_base = (void *)state->buf;
1724         state->vector.iov_len = sizeof(state->buf);
1725
1726         _smb2_setlen(state->buf, sizeof(state->buf) - 4);
1727         hdr = state->buf + 4;
1728         body = hdr + SMB2_HDR_BODY;
1729
1730         SIVAL(hdr, 0,                           SMB2_MAGIC);
1731         SSVAL(hdr, SMB2_HDR_LENGTH,             SMB2_HDR_BODY);
1732         SSVAL(hdr, SMB2_HDR_EPOCH,              0);
1733         SIVAL(hdr, SMB2_HDR_STATUS,             0);
1734         SSVAL(hdr, SMB2_HDR_OPCODE,             SMB2_OP_BREAK);
1735         SSVAL(hdr, SMB2_HDR_CREDIT,             0);
1736         SIVAL(hdr, SMB2_HDR_FLAGS,              SMB2_HDR_FLAG_REDIRECT);
1737         SIVAL(hdr, SMB2_HDR_NEXT_COMMAND,       0);
1738         SBVAL(hdr, SMB2_HDR_MESSAGE_ID,         UINT64_MAX);
1739         SIVAL(hdr, SMB2_HDR_PID,                0);
1740         SIVAL(hdr, SMB2_HDR_TID,                0);
1741         SBVAL(hdr, SMB2_HDR_SESSION_ID,         0);
1742         memset(hdr+SMB2_HDR_SIGNATURE, 0, 16);
1743
1744         SSVAL(body, 0x00, 0x18);
1745
1746         SCVAL(body, 0x02, oplock_level);
1747         SCVAL(body, 0x03, 0);           /* reserved */
1748         SIVAL(body, 0x04, 0);           /* reserved */
1749         SBVAL(body, 0x08, file_id_persistent);
1750         SBVAL(body, 0x10, file_id_volatile);
1751
1752         subreq = tstream_writev_queue_send(state,
1753                                            sconn->smb2.event_ctx,
1754                                            sconn->smb2.stream,
1755                                            sconn->smb2.send_queue,
1756                                            &state->vector, 1);
1757         if (subreq == NULL) {
1758                 return NT_STATUS_NO_MEMORY;
1759         }
1760         tevent_req_set_callback(subreq,
1761                                 smbd_smb2_oplock_break_writev_done,
1762                                 state);
1763
1764         return NT_STATUS_OK;
1765 }
1766
1767 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq)
1768 {
1769         struct smbd_smb2_send_oplock_break_state *state =
1770                 tevent_req_callback_data(subreq,
1771                 struct smbd_smb2_send_oplock_break_state);
1772         struct smbd_server_connection *sconn = state->sconn;
1773         int ret;
1774         int sys_errno;
1775
1776         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1777         TALLOC_FREE(subreq);
1778         if (ret == -1) {
1779                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1780                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1781                 return;
1782         }
1783
1784         TALLOC_FREE(state);
1785 }
1786
1787 struct smbd_smb2_request_read_state {
1788         size_t missing;
1789         bool asked_for_header;
1790         struct smbd_smb2_request *smb2_req;
1791 };
1792
1793 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1794                                          void *private_data,
1795                                          TALLOC_CTX *mem_ctx,
1796                                          struct iovec **_vector,
1797                                          size_t *_count);
1798 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
1799
1800 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
1801                                         struct tevent_context *ev,
1802                                         struct smbd_server_connection *sconn)
1803 {
1804         struct tevent_req *req;
1805         struct smbd_smb2_request_read_state *state;
1806         struct tevent_req *subreq;
1807
1808         req = tevent_req_create(mem_ctx, &state,
1809                                 struct smbd_smb2_request_read_state);
1810         if (req == NULL) {
1811                 return NULL;
1812         }
1813         state->missing = 0;
1814         state->asked_for_header = false;
1815
1816         state->smb2_req = smbd_smb2_request_allocate(state);
1817         if (tevent_req_nomem(state->smb2_req, req)) {
1818                 return tevent_req_post(req, ev);
1819         }
1820         state->smb2_req->sconn = sconn;
1821
1822         subreq = tstream_readv_pdu_queue_send(state, ev, sconn->smb2.stream,
1823                                               sconn->smb2.recv_queue,
1824                                               smbd_smb2_request_next_vector,
1825                                               state);
1826         if (tevent_req_nomem(subreq, req)) {
1827                 return tevent_req_post(req, ev);
1828         }
1829         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
1830
1831         return req;
1832 }
1833
1834 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1835                                          void *private_data,
1836                                          TALLOC_CTX *mem_ctx,
1837                                          struct iovec **_vector,
1838                                          size_t *_count)
1839 {
1840         struct smbd_smb2_request_read_state *state =
1841                 talloc_get_type_abort(private_data,
1842                 struct smbd_smb2_request_read_state);
1843         struct smbd_smb2_request *req = state->smb2_req;
1844         struct iovec *vector;
1845         int idx = req->in.vector_count;
1846         size_t len = 0;
1847         uint8_t *buf = NULL;
1848
1849         if (req->in.vector_count == 0) {
1850                 /*
1851                  * first we need to get the NBT header
1852                  */
1853                 req->in.vector = talloc_array(req, struct iovec,
1854                                               req->in.vector_count + 1);
1855                 if (req->in.vector == NULL) {
1856                         return -1;
1857                 }
1858                 req->in.vector_count += 1;
1859
1860                 req->in.vector[idx].iov_base    = (void *)req->in.nbt_hdr;
1861                 req->in.vector[idx].iov_len     = 4;
1862
1863                 vector = talloc_array(mem_ctx, struct iovec, 1);
1864                 if (vector == NULL) {
1865                         return -1;
1866                 }
1867
1868                 vector[0] = req->in.vector[idx];
1869
1870                 *_vector = vector;
1871                 *_count = 1;
1872                 return 0;
1873         }
1874
1875         if (req->in.vector_count == 1) {
1876                 /*
1877                  * Now we analyze the NBT header
1878                  */
1879                 state->missing = smb2_len(req->in.vector[0].iov_base);
1880
1881                 if (state->missing == 0) {
1882                         /* if there're no remaining bytes, we're done */
1883                         *_vector = NULL;
1884                         *_count = 0;
1885                         return 0;
1886                 }
1887
1888                 req->in.vector = talloc_realloc(req, req->in.vector,
1889                                                 struct iovec,
1890                                                 req->in.vector_count + 1);
1891                 if (req->in.vector == NULL) {
1892                         return -1;
1893                 }
1894                 req->in.vector_count += 1;
1895
1896                 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
1897                         /*
1898                          * it's a special NBT message,
1899                          * so get all remaining bytes
1900                          */
1901                         len = state->missing;
1902                 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
1903                         /*
1904                          * it's an invalid message, just read what we can get
1905                          * and let the caller handle the error
1906                          */
1907                         len = state->missing;
1908                 } else {
1909                         /*
1910                          * We assume it's a SMB2 request,
1911                          * and we first get the header and the
1912                          * first 2 bytes (the struct size) of the body
1913                          */
1914                         len = SMB2_HDR_BODY + 2;
1915
1916                         state->asked_for_header = true;
1917                 }
1918
1919                 state->missing -= len;
1920
1921                 buf = talloc_array(req->in.vector, uint8_t, len);
1922                 if (buf == NULL) {
1923                         return -1;
1924                 }
1925
1926                 req->in.vector[idx].iov_base    = (void *)buf;
1927                 req->in.vector[idx].iov_len     = len;
1928
1929                 vector = talloc_array(mem_ctx, struct iovec, 1);
1930                 if (vector == NULL) {
1931                         return -1;
1932                 }
1933
1934                 vector[0] = req->in.vector[idx];
1935
1936                 *_vector = vector;
1937                 *_count = 1;
1938                 return 0;
1939         }
1940
1941         if (state->missing == 0) {
1942                 /* if there're no remaining bytes, we're done */
1943                 *_vector = NULL;
1944                 *_count = 0;
1945                 return 0;
1946         }
1947
1948         if (state->asked_for_header) {
1949                 const uint8_t *hdr;
1950                 size_t full_size;
1951                 size_t next_command_ofs;
1952                 size_t body_size;
1953                 uint8_t *body;
1954                 size_t dyn_size;
1955                 uint8_t *dyn;
1956                 bool invalid = false;
1957
1958                 state->asked_for_header = false;
1959
1960                 /*
1961                  * We got the SMB2 header and the first 2 bytes
1962                  * of the body. We fix the size to just the header
1963                  * and manually copy the 2 first bytes to the body section
1964                  */
1965                 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
1966                 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
1967
1968                 /* allocate vectors for body and dynamic areas */
1969                 req->in.vector = talloc_realloc(req, req->in.vector,
1970                                                 struct iovec,
1971                                                 req->in.vector_count + 2);
1972                 if (req->in.vector == NULL) {
1973                         return -1;
1974                 }
1975                 req->in.vector_count += 2;
1976
1977                 full_size = state->missing + SMB2_HDR_BODY + 2;
1978                 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
1979                 body_size = SVAL(hdr, SMB2_HDR_BODY);
1980
1981                 if (next_command_ofs != 0) {
1982                         if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
1983                                 /*
1984                                  * this is invalid, just return a zero
1985                                  * body and let the caller deal with the error
1986                                  */
1987                                 invalid = true;
1988                         } else if (next_command_ofs > full_size) {
1989                                 /*
1990                                  * this is invalid, just return a zero
1991                                  * body and let the caller deal with the error
1992                                  */
1993                                 invalid = true;
1994                         } else {
1995                                 full_size = next_command_ofs;
1996                         }
1997                 }
1998
1999                 if (!invalid) {
2000                         if (body_size < 2) {
2001                                 /*
2002                                  * this is invalid, just return a zero
2003                                  * body and let the caller deal with the error
2004                                  */
2005                                 invalid = true;
2006                         }
2007
2008                         if ((body_size % 2) != 0) {
2009                                 body_size -= 1;
2010                         }
2011
2012                         if (body_size > (full_size - SMB2_HDR_BODY)) {
2013                                 /*
2014                                  * this is invalid, just return a zero
2015                                  * body and let the caller deal with the error
2016                                  */
2017                                 invalid = true;
2018                         }
2019                 }
2020
2021                 if (invalid) {
2022                         /* the caller should check this */
2023                         body_size = 2;
2024                 }
2025
2026                 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
2027
2028                 state->missing -= (body_size - 2) + dyn_size;
2029
2030                 body = talloc_array(req->in.vector, uint8_t, body_size);
2031                 if (body == NULL) {
2032                         return -1;
2033                 }
2034
2035                 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
2036                 if (dyn == NULL) {
2037                         return -1;
2038                 }
2039
2040                 req->in.vector[idx].iov_base    = (void *)body;
2041                 req->in.vector[idx].iov_len     = body_size;
2042                 req->in.vector[idx+1].iov_base  = (void *)dyn;
2043                 req->in.vector[idx+1].iov_len   = dyn_size;
2044
2045                 vector = talloc_array(mem_ctx, struct iovec, 2);
2046                 if (vector == NULL) {
2047                         return -1;
2048                 }
2049
2050                 /*
2051                  * the first 2 bytes of the body were already fetched
2052                  * together with the header
2053                  */
2054                 memcpy(body, hdr + SMB2_HDR_BODY, 2);
2055                 vector[0].iov_base = body + 2;
2056                 vector[0].iov_len = body_size - 2;
2057
2058                 vector[1] = req->in.vector[idx+1];
2059
2060                 *_vector = vector;
2061                 *_count = 2;
2062                 return 0;
2063         }
2064
2065         /*
2066          * when we endup here, we're looking for a new SMB2 request
2067          * next. And we ask for its header and the first 2 bytes of
2068          * the body (like we did for the first SMB2 request).
2069          */
2070
2071         req->in.vector = talloc_realloc(req, req->in.vector,
2072                                         struct iovec,
2073                                         req->in.vector_count + 1);
2074         if (req->in.vector == NULL) {
2075                 return -1;
2076         }
2077         req->in.vector_count += 1;
2078
2079         /*
2080          * We assume it's a SMB2 request,
2081          * and we first get the header and the
2082          * first 2 bytes (the struct size) of the body
2083          */
2084         len = SMB2_HDR_BODY + 2;
2085
2086         if (len > state->missing) {
2087                 /* let the caller handle the error */
2088                 len = state->missing;
2089         }
2090
2091         state->missing -= len;
2092         state->asked_for_header = true;
2093
2094         buf = talloc_array(req->in.vector, uint8_t, len);
2095         if (buf == NULL) {
2096                 return -1;
2097         }
2098
2099         req->in.vector[idx].iov_base    = (void *)buf;
2100         req->in.vector[idx].iov_len     = len;
2101
2102         vector = talloc_array(mem_ctx, struct iovec, 1);
2103         if (vector == NULL) {
2104                 return -1;
2105         }
2106
2107         vector[0] = req->in.vector[idx];
2108
2109         *_vector = vector;
2110         *_count = 1;
2111         return 0;
2112 }
2113
2114 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
2115 {
2116         struct tevent_req *req =
2117                 tevent_req_callback_data(subreq,
2118                 struct tevent_req);
2119         int ret;
2120         int sys_errno;
2121         NTSTATUS status;
2122
2123         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
2124         if (ret == -1) {
2125                 status = map_nt_error_from_unix(sys_errno);
2126                 tevent_req_nterror(req, status);
2127                 return;
2128         }
2129
2130         tevent_req_done(req);
2131 }
2132
2133 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
2134                                             TALLOC_CTX *mem_ctx,
2135                                             struct smbd_smb2_request **_smb2_req)
2136 {
2137         struct smbd_smb2_request_read_state *state =
2138                 tevent_req_data(req,
2139                 struct smbd_smb2_request_read_state);
2140         NTSTATUS status;
2141
2142         if (tevent_req_is_nterror(req, &status)) {
2143                 tevent_req_received(req);
2144                 return status;
2145         }
2146
2147         talloc_steal(mem_ctx, state->smb2_req->mem_pool);
2148         *_smb2_req = state->smb2_req;
2149         tevent_req_received(req);
2150         return NT_STATUS_OK;
2151 }
2152
2153 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
2154
2155 void smbd_smb2_first_negprot(struct smbd_server_connection *sconn,
2156                              const uint8_t *inbuf, size_t size)
2157 {
2158         NTSTATUS status;
2159         struct smbd_smb2_request *req = NULL;
2160         struct tevent_req *subreq;
2161
2162         if (lp_security() == SEC_SHARE) {
2163                 DEBUG(2,("WARNING!!: \"security = share\" is deprecated for "
2164                         "SMB2 servers. Mapping to \"security = user\" and "
2165                         "\"map to guest = Bad User\"\n" ));
2166                 lp_do_parameter(-1, "security", "user");
2167                 lp_do_parameter(-1, "map to guest", "Bad User");
2168         }
2169
2170         DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
2171                  (unsigned int)size));
2172
2173         status = smbd_initialize_smb2(sconn);
2174         if (!NT_STATUS_IS_OK(status)) {
2175                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2176                 return;
2177         }
2178
2179         status = smbd_smb2_request_create(sconn, inbuf, size, &req);
2180         if (!NT_STATUS_IS_OK(status)) {
2181                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2182                 return;
2183         }
2184
2185         status = smbd_smb2_request_setup_out(req);
2186         if (!NT_STATUS_IS_OK(status)) {
2187                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2188                 return;
2189         }
2190
2191         status = smbd_smb2_request_dispatch(req);
2192         if (!NT_STATUS_IS_OK(status)) {
2193                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2194                 return;
2195         }
2196
2197         /* ask for the next request */
2198         subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
2199         if (subreq == NULL) {
2200                 smbd_server_connection_terminate(sconn, "no memory for reading");
2201                 return;
2202         }
2203         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);
2204 }
2205
2206 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
2207 {
2208         struct smbd_server_connection *sconn = tevent_req_callback_data(subreq,
2209                                                struct smbd_server_connection);
2210         NTSTATUS status;
2211         struct smbd_smb2_request *req = NULL;
2212
2213         status = smbd_smb2_request_read_recv(subreq, sconn, &req);
2214         TALLOC_FREE(subreq);
2215         if (!NT_STATUS_IS_OK(status)) {
2216                 DEBUG(2,("smbd_smb2_request_incoming: client read error %s\n",
2217                         nt_errstr(status)));
2218                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2219                 return;
2220         }
2221
2222         if (req->in.nbt_hdr[0] != 0x00) {
2223                 DEBUG(1,("smbd_smb2_request_incoming: ignore NBT[0x%02X] msg\n",
2224                          req->in.nbt_hdr[0]));
2225                 TALLOC_FREE(req);
2226                 goto next;
2227         }
2228
2229         req->current_idx = 1;
2230
2231         DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
2232                  req->current_idx, req->in.vector_count));
2233
2234         status = smbd_smb2_request_validate(req);
2235         if (!NT_STATUS_IS_OK(status)) {
2236                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2237                 return;
2238         }
2239
2240         status = smbd_smb2_request_setup_out(req);
2241         if (!NT_STATUS_IS_OK(status)) {
2242                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2243                 return;
2244         }
2245
2246         status = smbd_smb2_request_dispatch(req);
2247         if (!NT_STATUS_IS_OK(status)) {
2248                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2249                 return;
2250         }
2251
2252 next:
2253         /* ask for the next request (this constructs the main loop) */
2254         subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
2255         if (subreq == NULL) {
2256                 smbd_server_connection_terminate(sconn, "no memory for reading");
2257                 return;
2258         }
2259         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);
2260 }