Add a SMB2 crediting algorithm, by default the same as Windows. Defaults to 128 credits.
[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 = 1;
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                 /* Ensure the client credits can never drop to zero. */
457                 credits_granted = 1;
458         }
459
460         SSVAL(outhdr, SMB2_HDR_CREDIT, credits_granted);
461         sconn->smb2.credits_granted += credits_granted;
462
463         DEBUG(10,("smb2_set_operation_credit: requested %u, "
464                 "granted %u, total granted %u\n",
465                 (unsigned int)credits_requested,
466                 (unsigned int)credits_granted,
467                 (unsigned int)sconn->smb2.credits_granted ));
468 }
469
470 static void smb2_calculate_credits(const struct smbd_smb2_request *inreq,
471                                 struct smbd_smb2_request *outreq)
472 {
473         int count, idx;
474
475         count = outreq->out.vector_count;
476
477         for (idx=1; idx < count; idx += 3) {
478                 smb2_set_operation_credit(outreq->sconn,
479                         &inreq->in.vector[idx],
480                         &outreq->out.vector[idx]);
481         }
482 }
483
484 static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req, uint16_t creds)
485 {
486         struct iovec *vector;
487         int count;
488         int idx;
489
490         count = req->in.vector_count;
491         vector = talloc_zero_array(req, struct iovec, count);
492         if (vector == NULL) {
493                 return NT_STATUS_NO_MEMORY;
494         }
495
496         vector[0].iov_base      = req->out.nbt_hdr;
497         vector[0].iov_len       = 4;
498         SIVAL(req->out.nbt_hdr, 0, 0);
499
500         for (idx=1; idx < count; idx += 3) {
501                 const uint8_t *inhdr = NULL;
502                 uint32_t in_flags;
503                 uint8_t *outhdr = NULL;
504                 uint8_t *outbody = NULL;
505                 uint32_t next_command_ofs = 0;
506                 struct iovec *current = &vector[idx];
507
508                 if ((idx + 3) < count) {
509                         /* we have a next command -
510                          * setup for the error case. */
511                         next_command_ofs = SMB2_HDR_BODY + 9;
512                 }
513
514                 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
515                 in_flags = IVAL(inhdr, SMB2_HDR_FLAGS);
516
517                 outhdr = talloc_zero_array(vector, uint8_t,
518                                       OUTVEC_ALLOC_SIZE);
519                 if (outhdr == NULL) {
520                         return NT_STATUS_NO_MEMORY;
521                 }
522
523                 outbody = outhdr + SMB2_HDR_BODY;
524
525                 current[0].iov_base     = (void *)outhdr;
526                 current[0].iov_len      = SMB2_HDR_BODY;
527
528                 current[1].iov_base     = (void *)outbody;
529                 current[1].iov_len      = 8;
530
531                 current[2].iov_base     = NULL;
532                 current[2].iov_len      = 0;
533
534                 /* setup the SMB2 header */
535                 SIVAL(outhdr, SMB2_HDR_PROTOCOL_ID,     SMB2_MAGIC);
536                 SSVAL(outhdr, SMB2_HDR_LENGTH,          SMB2_HDR_BODY);
537                 SSVAL(outhdr, SMB2_HDR_EPOCH,           0);
538                 SIVAL(outhdr, SMB2_HDR_STATUS,
539                       NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));
540                 SSVAL(outhdr, SMB2_HDR_OPCODE,
541                       SVAL(inhdr, SMB2_HDR_OPCODE));
542                 SIVAL(outhdr, SMB2_HDR_FLAGS,
543                       IVAL(inhdr, SMB2_HDR_FLAGS) | SMB2_HDR_FLAG_REDIRECT);
544                 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND,    next_command_ofs);
545                 SBVAL(outhdr, SMB2_HDR_MESSAGE_ID,
546                       BVAL(inhdr, SMB2_HDR_MESSAGE_ID));
547                 SIVAL(outhdr, SMB2_HDR_PID,
548                       IVAL(inhdr, SMB2_HDR_PID));
549                 SIVAL(outhdr, SMB2_HDR_TID,
550                       IVAL(inhdr, SMB2_HDR_TID));
551                 SBVAL(outhdr, SMB2_HDR_SESSION_ID,
552                       BVAL(inhdr, SMB2_HDR_SESSION_ID));
553                 memset(outhdr + SMB2_HDR_SIGNATURE, 0, 16);
554
555                 /* setup error body header */
556                 SSVAL(outbody, 0x00, 0x08 + 1);
557                 SSVAL(outbody, 0x02, 0);
558                 SIVAL(outbody, 0x04, 0);
559         }
560
561         req->out.vector = vector;
562         req->out.vector_count = count;
563
564         /* setup the length of the NBT packet */
565         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
566
567         DLIST_ADD_END(req->sconn->smb2.requests, req, struct smbd_smb2_request *);
568
569         return NT_STATUS_OK;
570 }
571
572 void smbd_server_connection_terminate_ex(struct smbd_server_connection *sconn,
573                                          const char *reason,
574                                          const char *location)
575 {
576         DEBUG(10,("smbd_server_connection_terminate_ex: reason[%s] at %s\n",
577                   reason, location));
578         exit_server_cleanly(reason);
579 }
580
581 static bool dup_smb2_vec3(TALLOC_CTX *ctx,
582                         struct iovec *outvec,
583                         const struct iovec *srcvec)
584 {
585         /* vec[0] is always boilerplate and must
586          * be allocated with size OUTVEC_ALLOC_SIZE. */
587
588         outvec[0].iov_base = talloc_memdup(ctx,
589                                 srcvec[0].iov_base,
590                                 OUTVEC_ALLOC_SIZE);
591         if (!outvec[0].iov_base) {
592                 return false;
593         }
594         outvec[0].iov_len = SMB2_HDR_BODY;
595
596         /*
597          * If this is a "standard" vec[1] of length 8,
598          * pointing to srcvec[0].iov_base + SMB2_HDR_BODY,
599          * then duplicate this. Else use talloc_memdup().
600          */
601
602         if (srcvec[1].iov_len == 8 &&
603                         srcvec[1].iov_base ==
604                                 ((uint8_t *)srcvec[0].iov_base) +
605                                         SMB2_HDR_BODY) {
606                 outvec[1].iov_base = ((uint8_t *)outvec[1].iov_base) +
607                                         SMB2_HDR_BODY;
608                 outvec[1].iov_len = 8;
609         } else {
610                 outvec[1].iov_base = talloc_memdup(ctx,
611                                 srcvec[1].iov_base,
612                                 srcvec[1].iov_len);
613                 if (!outvec[1].iov_base) {
614                         return false;
615                 }
616                 outvec[1].iov_len = srcvec[1].iov_len;
617         }
618
619         /*
620          * If this is a "standard" vec[2] of length 1,
621          * pointing to srcvec[0].iov_base + (OUTVEC_ALLOC_SIZE - 1)
622          * then duplicate this. Else use talloc_memdup().
623          */
624
625         if (srcvec[2].iov_base &&
626                         srcvec[2].iov_len) {
627                 if (srcvec[2].iov_base ==
628                                 ((uint8_t *)srcvec[0].iov_base) +
629                                         (OUTVEC_ALLOC_SIZE - 1) &&
630                                 srcvec[2].iov_len == 1) {
631                         /* Common SMB2 error packet case. */
632                         outvec[2].iov_base = ((uint8_t *)outvec[0].iov_base) +
633                                 (OUTVEC_ALLOC_SIZE - 1);
634                 } else {
635                         outvec[2].iov_base = talloc_memdup(ctx,
636                                         srcvec[2].iov_base,
637                                         srcvec[2].iov_len);
638                         if (!outvec[2].iov_base) {
639                                 return false;
640                         }
641                 }
642                 outvec[2].iov_len = srcvec[2].iov_len;
643         } else {
644                 outvec[2].iov_base = NULL;
645                 outvec[2].iov_len = 0;
646         }
647         return true;
648 }
649
650 static struct smbd_smb2_request *dup_smb2_req(const struct smbd_smb2_request *req)
651 {
652         struct smbd_smb2_request *newreq = NULL;
653         struct iovec *outvec = NULL;
654         int count = req->out.vector_count;
655         int i;
656
657         newreq = smbd_smb2_request_allocate(req->sconn);
658         if (!newreq) {
659                 return NULL;
660         }
661
662         newreq->sconn = req->sconn;
663         newreq->do_signing = req->do_signing;
664         newreq->current_idx = req->current_idx;
665         newreq->async = false;
666         newreq->cancelled = false;
667
668         outvec = talloc_zero_array(newreq, struct iovec, count);
669         if (!outvec) {
670                 TALLOC_FREE(newreq);
671                 return NULL;
672         }
673         newreq->out.vector = outvec;
674         newreq->out.vector_count = count;
675
676         /* Setup the outvec's identically to req. */
677         outvec[0].iov_base = newreq->out.nbt_hdr;
678         outvec[0].iov_len = 4;
679         memcpy(newreq->out.nbt_hdr, req->out.nbt_hdr, 4);
680
681         /* Setup the vectors identically to the ones in req. */
682         for (i = 1; i < count; i += 3) {
683                 if (!dup_smb2_vec3(outvec, &outvec[i], &req->out.vector[i])) {
684                         break;
685                 }
686         }
687
688         if (i < count) {
689                 /* Alloc failed. */
690                 TALLOC_FREE(newreq);
691                 return NULL;
692         }
693
694         smb2_setup_nbt_length(newreq->out.vector,
695                 newreq->out.vector_count);
696
697         return newreq;
698 }
699
700 static void smbd_smb2_request_writev_done(struct tevent_req *subreq);
701
702 static NTSTATUS smb2_send_async_interim_response(const struct smbd_smb2_request *req)
703 {
704         int i = 0;
705         uint8_t *outhdr = NULL;
706         struct smbd_smb2_request *nreq = NULL;
707
708         /* Create a new smb2 request we'll use
709            for the interim return. */
710         nreq = dup_smb2_req(req);
711         if (!nreq) {
712                 return NT_STATUS_NO_MEMORY;
713         }
714
715         /* Lose the last 3 out vectors. They're the
716            ones we'll be using for the async reply. */
717         nreq->out.vector_count -= 3;
718
719         smb2_setup_nbt_length(nreq->out.vector,
720                 nreq->out.vector_count);
721
722         /* Step back to the previous reply. */
723         i = nreq->current_idx - 3;
724         outhdr = (uint8_t *)nreq->out.vector[i].iov_base;
725         /* And end the chain. */
726         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
727
728         /* Calculate outgoing credits */
729         smb2_calculate_credits(req, nreq);
730
731         /* Re-sign if needed. */
732         if (nreq->do_signing) {
733                 NTSTATUS status;
734                 status = smb2_signing_sign_pdu(nreq->session->session_key,
735                                         &nreq->out.vector[i], 3);
736                 if (!NT_STATUS_IS_OK(status)) {
737                         return status;
738                 }
739         }
740         if (DEBUGLEVEL >= 10) {
741                 dbgtext("smb2_send_async_interim_response: nreq->current_idx = %u\n",
742                         (unsigned int)nreq->current_idx );
743                 dbgtext("smb2_send_async_interim_response: returning %u vectors\n",
744                         (unsigned int)nreq->out.vector_count );
745                 print_req_vectors(nreq);
746         }
747         nreq->subreq = tstream_writev_queue_send(nreq,
748                                         nreq->sconn->smb2.event_ctx,
749                                         nreq->sconn->smb2.stream,
750                                         nreq->sconn->smb2.send_queue,
751                                         nreq->out.vector,
752                                         nreq->out.vector_count);
753
754         if (nreq->subreq == NULL) {
755                 return NT_STATUS_NO_MEMORY;
756         }
757
758         tevent_req_set_callback(nreq->subreq,
759                         smbd_smb2_request_writev_done,
760                         nreq);
761
762         return NT_STATUS_OK;
763 }
764
765 struct smbd_smb2_request_pending_state {
766         struct smbd_server_connection *sconn;
767         uint8_t buf[4 + SMB2_HDR_BODY + 0x08 + 1];
768         struct iovec vector[3];
769 };
770
771 static void smbd_smb2_request_pending_writev_done(struct tevent_req *subreq)
772 {
773         struct smbd_smb2_request_pending_state *state =
774                 tevent_req_callback_data(subreq,
775                         struct smbd_smb2_request_pending_state);
776         struct smbd_server_connection *sconn = state->sconn;
777         int ret;
778         int sys_errno;
779
780         ret = tstream_writev_queue_recv(subreq, &sys_errno);
781         TALLOC_FREE(subreq);
782         if (ret == -1) {
783                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
784                 smbd_server_connection_terminate(sconn, nt_errstr(status));
785                 return;
786         }
787
788         TALLOC_FREE(state);
789 }
790
791 NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
792                                          struct tevent_req *subreq)
793 {
794         NTSTATUS status;
795         struct smbd_smb2_request_pending_state *state = NULL;
796         int i = req->current_idx;
797         uint8_t *reqhdr = NULL;
798         uint8_t *hdr = NULL;
799         uint8_t *body = NULL;
800         uint32_t flags = 0;
801         uint64_t message_id = 0;
802         uint64_t async_id = 0;
803         struct iovec *outvec = NULL;
804
805         if (!tevent_req_is_in_progress(subreq)) {
806                 return NT_STATUS_OK;
807         }
808
809         req->subreq = subreq;
810         subreq = NULL;
811
812         if (req->async) {
813                 /* We're already async. */
814                 return NT_STATUS_OK;
815         }
816
817         if (req->in.vector_count > i + 3) {
818                 /*
819                  * We're trying to go async in a compound
820                  * request chain. This is not allowed.
821                  * Cancel the outstanding request.
822                  */
823                 tevent_req_cancel(req->subreq);
824                 return smbd_smb2_request_error(req,
825                         NT_STATUS_INSUFFICIENT_RESOURCES);
826         }
827
828         if (DEBUGLEVEL >= 10) {
829                 dbgtext("smbd_smb2_request_pending_queue: req->current_idx = %u\n",
830                         (unsigned int)req->current_idx );
831                 print_req_vectors(req);
832         }
833
834         if (req->out.vector_count > 4) {
835                 /* This is a compound reply. We
836                  * must do an interim response
837                  * followed by the async response
838                  * to match W2K8R2.
839                  */
840                 status = smb2_send_async_interim_response(req);
841                 if (!NT_STATUS_IS_OK(status)) {
842                         return status;
843                 }
844         }
845
846         /* Don't return an intermediate packet on a pipe read/write. */
847         if (req->tcon && req->tcon->compat_conn && IS_IPC(req->tcon->compat_conn)) {
848                 return NT_STATUS_OK;
849         }
850
851         reqhdr = (uint8_t *)req->out.vector[i].iov_base;
852         flags = (IVAL(reqhdr, SMB2_HDR_FLAGS) & ~SMB2_HDR_FLAG_CHAINED);
853         message_id = BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
854         async_id = message_id; /* keep it simple for now... */
855
856         /*
857          * What we send is identical to a smbd_smb2_request_error
858          * packet with an error status of STATUS_PENDING. Make use
859          * of this fact sometime when refactoring. JRA.
860          */
861
862         state = talloc_zero(req->sconn, struct smbd_smb2_request_pending_state);
863         if (state == NULL) {
864                 return NT_STATUS_NO_MEMORY;
865         }
866         state->sconn = req->sconn;
867
868         state->vector[0].iov_base = (void *)state->buf;
869         state->vector[0].iov_len = 4;
870
871         state->vector[1].iov_base = state->buf + 4;
872         state->vector[1].iov_len = SMB2_HDR_BODY;
873
874         state->vector[2].iov_base = state->buf + 4 + SMB2_HDR_BODY;
875         state->vector[2].iov_len = 9;
876
877         smb2_setup_nbt_length(state->vector, 3);
878
879         hdr = (uint8_t *)state->vector[1].iov_base;
880         body = (uint8_t *)state->vector[2].iov_base;
881
882         SIVAL(hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
883         SSVAL(hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
884         SSVAL(hdr, SMB2_HDR_EPOCH, 0);
885         SIVAL(hdr, SMB2_HDR_STATUS, NT_STATUS_V(STATUS_PENDING));
886         SSVAL(hdr, SMB2_HDR_OPCODE, SVAL(reqhdr, SMB2_HDR_OPCODE));
887
888         SIVAL(hdr, SMB2_HDR_FLAGS, flags | SMB2_HDR_FLAG_ASYNC);
889         SIVAL(hdr, SMB2_HDR_NEXT_COMMAND, 0);
890         SBVAL(hdr, SMB2_HDR_MESSAGE_ID, message_id);
891         SBVAL(hdr, SMB2_HDR_PID, async_id);
892         SBVAL(hdr, SMB2_HDR_SESSION_ID,
893                 BVAL(reqhdr, SMB2_HDR_SESSION_ID));
894         memset(hdr+SMB2_HDR_SIGNATURE, 0, 16);
895
896         SSVAL(body, 0x00, 0x08 + 1);
897
898         SCVAL(body, 0x02, 0);
899         SCVAL(body, 0x03, 0);
900         SIVAL(body, 0x04, 0);
901         /* Match W2K8R2... */
902         SCVAL(body, 0x08, 0x21);
903
904         /* Ensure we correctly go through crediting. */
905         smb2_set_operation_credit(req->sconn,
906                         NULL,
907                         &state->vector[1]);
908
909         if (req->do_signing) {
910                 status = smb2_signing_sign_pdu(req->session->session_key,
911                                         state->vector, 3);
912                 if (!NT_STATUS_IS_OK(status)) {
913                         return status;
914                 }
915         }
916
917         subreq = tstream_writev_queue_send(state,
918                                         req->sconn->smb2.event_ctx,
919                                         req->sconn->smb2.stream,
920                                         req->sconn->smb2.send_queue,
921                                         state->vector,
922                                         3);
923
924         if (subreq == NULL) {
925                 return NT_STATUS_NO_MEMORY;
926         }
927
928         tevent_req_set_callback(subreq,
929                         smbd_smb2_request_pending_writev_done,
930                         state);
931
932         /* Note we're going async with this request. */
933         req->async = true;
934
935         /*
936          * Now manipulate req so that the outstanding async request
937          * is the only one left in the struct smbd_smb2_request.
938          */
939
940         if (req->current_idx == 1) {
941                 /* There was only one. */
942                 goto out;
943         }
944
945         /* Re-arrange the in.vectors. */
946         req->in.vector[1] = req->in.vector[i];
947         req->in.vector[2] = req->in.vector[i+1];
948         req->in.vector[3] = req->in.vector[i+2];
949         req->in.vector_count = 4;
950         /* Reset the new in size. */
951         smb2_setup_nbt_length(req->in.vector, 4);
952
953         /* Now recreate the out.vectors. */
954         outvec = talloc_zero_array(req, struct iovec, 4);
955         if (!outvec) {
956                 return NT_STATUS_NO_MEMORY;
957         }
958
959         /* 0 is always boilerplate and must
960          * be of size 4 for the length field. */
961
962         outvec[0].iov_base = req->out.nbt_hdr;
963         outvec[0].iov_len = 4;
964         SIVAL(req->out.nbt_hdr, 0, 0);
965
966         if (!dup_smb2_vec3(outvec, &outvec[1], &req->out.vector[i])) {
967                 return NT_STATUS_NO_MEMORY;
968         }
969
970         TALLOC_FREE(req->out.vector);
971
972         req->out.vector = outvec;
973
974         req->current_idx = 1;
975         req->out.vector_count = 4;
976
977   out:
978
979         smb2_setup_nbt_length(req->out.vector,
980                 req->out.vector_count);
981
982         /* Ensure our final reply matches the interim one. */
983         reqhdr = (uint8_t *)req->out.vector[1].iov_base;
984         SIVAL(reqhdr, SMB2_HDR_FLAGS, flags | SMB2_HDR_FLAG_ASYNC);
985         SBVAL(reqhdr, SMB2_HDR_PID, async_id);
986
987         {
988                 const uint8_t *inhdr =
989                         (const uint8_t *)req->in.vector[1].iov_base;
990                 DEBUG(10,("smbd_smb2_request_pending_queue: opcode[%s] mid %llu "
991                         "going async\n",
992                         smb2_opcode_name((uint16_t)IVAL(inhdr, SMB2_HDR_OPCODE)),
993                         (unsigned long long)async_id ));
994         }
995         return NT_STATUS_OK;
996 }
997
998 static NTSTATUS smbd_smb2_request_process_cancel(struct smbd_smb2_request *req)
999 {
1000         struct smbd_server_connection *sconn = req->sconn;
1001         struct smbd_smb2_request *cur;
1002         const uint8_t *inhdr;
1003         int i = req->current_idx;
1004         uint32_t flags;
1005         uint64_t search_message_id;
1006         uint64_t search_async_id;
1007         uint64_t found_id;
1008
1009         inhdr = (const uint8_t *)req->in.vector[i].iov_base;
1010
1011         flags = IVAL(inhdr, SMB2_HDR_FLAGS);
1012         search_message_id = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
1013         search_async_id = BVAL(inhdr, SMB2_HDR_PID);
1014
1015         /*
1016          * we don't need the request anymore
1017          * cancel requests never have a response
1018          */
1019         DLIST_REMOVE(req->sconn->smb2.requests, req);
1020         TALLOC_FREE(req);
1021
1022         for (cur = sconn->smb2.requests; cur; cur = cur->next) {
1023                 const uint8_t *outhdr;
1024                 uint64_t message_id;
1025                 uint64_t async_id;
1026
1027                 i = cur->current_idx;
1028
1029                 outhdr = (const uint8_t *)cur->out.vector[i].iov_base;
1030
1031                 message_id = BVAL(outhdr, SMB2_HDR_MESSAGE_ID);
1032                 async_id = BVAL(outhdr, SMB2_HDR_PID);
1033
1034                 if (flags & SMB2_HDR_FLAG_ASYNC) {
1035                         if (search_async_id == async_id) {
1036                                 found_id = async_id;
1037                                 break;
1038                         }
1039                 } else {
1040                         if (search_message_id == message_id) {
1041                                 found_id = message_id;
1042                                 break;
1043                         }
1044                 }
1045         }
1046
1047         if (cur && cur->subreq) {
1048                 inhdr = (const uint8_t *)cur->in.vector[i].iov_base;
1049                 DEBUG(10,("smbd_smb2_request_process_cancel: attempting to "
1050                         "cancel opcode[%s] mid %llu\n",
1051                         smb2_opcode_name((uint16_t)IVAL(inhdr, SMB2_HDR_OPCODE)),
1052                         (unsigned long long)found_id ));
1053                 tevent_req_cancel(cur->subreq);
1054         }
1055
1056         return NT_STATUS_OK;
1057 }
1058
1059 NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
1060 {
1061         const uint8_t *inhdr;
1062         int i = req->current_idx;
1063         uint16_t opcode;
1064         uint32_t flags;
1065         uint64_t mid;
1066         NTSTATUS status;
1067         NTSTATUS session_status;
1068         uint32_t allowed_flags;
1069         NTSTATUS return_value;
1070
1071         inhdr = (const uint8_t *)req->in.vector[i].iov_base;
1072
1073         /* TODO: verify more things */
1074
1075         flags = IVAL(inhdr, SMB2_HDR_FLAGS);
1076         opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
1077         mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
1078         DEBUG(10,("smbd_smb2_request_dispatch: opcode[%s] mid = %llu\n",
1079                 smb2_opcode_name(opcode),
1080                 (unsigned long long)mid));
1081
1082         allowed_flags = SMB2_HDR_FLAG_CHAINED |
1083                         SMB2_HDR_FLAG_SIGNED |
1084                         SMB2_HDR_FLAG_DFS;
1085         if (opcode == SMB2_OP_CANCEL) {
1086                 allowed_flags |= SMB2_HDR_FLAG_ASYNC;
1087         }
1088         if ((flags & ~allowed_flags) != 0) {
1089                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
1090         }
1091
1092         session_status = smbd_smb2_request_check_session(req);
1093
1094         req->do_signing = false;
1095         if (flags & SMB2_HDR_FLAG_SIGNED) {
1096                 if (!NT_STATUS_IS_OK(session_status)) {
1097                         return smbd_smb2_request_error(req, session_status);
1098                 }
1099
1100                 req->do_signing = true;
1101                 status = smb2_signing_check_pdu(req->session->session_key,
1102                                                 &req->in.vector[i], 3);
1103                 if (!NT_STATUS_IS_OK(status)) {
1104                         return smbd_smb2_request_error(req, status);
1105                 }
1106         } else if (req->session && req->session->do_signing) {
1107                 return smbd_smb2_request_error(req, NT_STATUS_ACCESS_DENIED);
1108         }
1109
1110         if (flags & SMB2_HDR_FLAG_CHAINED) {
1111                 /*
1112                  * This check is mostly for giving the correct error code
1113                  * for compounded requests.
1114                  *
1115                  * TODO: we may need to move this after the session
1116                  *       and tcon checks.
1117                  */
1118                 if (!NT_STATUS_IS_OK(req->next_status)) {
1119                         return smbd_smb2_request_error(req, req->next_status);
1120                 }
1121         } else {
1122                 req->compat_chain_fsp = NULL;
1123         }
1124
1125         switch (opcode) {
1126         case SMB2_OP_NEGPROT:
1127                 {
1128                         START_PROFILE(smb2_negprot);
1129                         return_value = smbd_smb2_request_process_negprot(req);
1130                         END_PROFILE(smb2_negprot);
1131                 }
1132                 break;
1133
1134         case SMB2_OP_SESSSETUP:
1135                 {
1136                         START_PROFILE(smb2_sesssetup);
1137                         return_value = smbd_smb2_request_process_sesssetup(req);
1138                         END_PROFILE(smb2_sesssetup);
1139                 }
1140                 break;
1141
1142         case SMB2_OP_LOGOFF:
1143                 if (!NT_STATUS_IS_OK(session_status)) {
1144                         return_value = smbd_smb2_request_error(req, session_status);
1145                         break;
1146                 }
1147
1148                 {
1149                         START_PROFILE(smb2_logoff);
1150                         return_value = smbd_smb2_request_process_logoff(req);
1151                         END_PROFILE(smb2_logoff);
1152                 }
1153                 break;
1154
1155         case SMB2_OP_TCON:
1156                 if (!NT_STATUS_IS_OK(session_status)) {
1157                         return_value = smbd_smb2_request_error(req, session_status);
1158                         break;
1159                 }
1160                 status = smbd_smb2_request_check_session(req);
1161                 if (!NT_STATUS_IS_OK(status)) {
1162                         return_value = smbd_smb2_request_error(req, status);
1163                         break;
1164                 }
1165
1166                 {
1167                         START_PROFILE(smb2_tcon);
1168                         return_value = smbd_smb2_request_process_tcon(req);
1169                         END_PROFILE(smb2_tcon);
1170                 }
1171                 break;
1172
1173         case SMB2_OP_TDIS:
1174                 if (!NT_STATUS_IS_OK(session_status)) {
1175                         return_value = smbd_smb2_request_error(req, session_status);
1176                         break;
1177                 }
1178                 status = smbd_smb2_request_check_tcon(req);
1179                 if (!NT_STATUS_IS_OK(status)) {
1180                         return_value = smbd_smb2_request_error(req, status);
1181                         break;
1182                 }
1183
1184                 {
1185                         START_PROFILE(smb2_tdis);
1186                         return_value = smbd_smb2_request_process_tdis(req);
1187                         END_PROFILE(smb2_tdis);
1188                 }
1189                 break;
1190
1191         case SMB2_OP_CREATE:
1192                 if (!NT_STATUS_IS_OK(session_status)) {
1193                         return_value = smbd_smb2_request_error(req, session_status);
1194                         break;
1195                 }
1196                 status = smbd_smb2_request_check_tcon(req);
1197                 if (!NT_STATUS_IS_OK(status)) {
1198                         return_value = smbd_smb2_request_error(req, status);
1199                         break;
1200                 }
1201
1202                 {
1203                         START_PROFILE(smb2_create);
1204                         return_value = smbd_smb2_request_process_create(req);
1205                         END_PROFILE(smb2_create);
1206                 }
1207                 break;
1208
1209         case SMB2_OP_CLOSE:
1210                 if (!NT_STATUS_IS_OK(session_status)) {
1211                         return_value = smbd_smb2_request_error(req, session_status);
1212                         break;
1213                 }
1214                 status = smbd_smb2_request_check_tcon(req);
1215                 if (!NT_STATUS_IS_OK(status)) {
1216                         return_value = smbd_smb2_request_error(req, status);
1217                         break;
1218                 }
1219
1220                 {
1221                         START_PROFILE(smb2_close);
1222                         return_value = smbd_smb2_request_process_close(req);
1223                         END_PROFILE(smb2_close);
1224                 }
1225                 break;
1226
1227         case SMB2_OP_FLUSH:
1228                 if (!NT_STATUS_IS_OK(session_status)) {
1229                         return_value = smbd_smb2_request_error(req, session_status);
1230                         break;
1231                 }
1232                 status = smbd_smb2_request_check_tcon(req);
1233                 if (!NT_STATUS_IS_OK(status)) {
1234                         return_value = smbd_smb2_request_error(req, status);
1235                         break;
1236                 }
1237
1238                 {
1239                         START_PROFILE(smb2_flush);
1240                         return_value = smbd_smb2_request_process_flush(req);
1241                         END_PROFILE(smb2_flush);
1242                 }
1243                 break;
1244
1245         case SMB2_OP_READ:
1246                 if (!NT_STATUS_IS_OK(session_status)) {
1247                         return_value = smbd_smb2_request_error(req, session_status);
1248                         break;
1249                 }
1250                 status = smbd_smb2_request_check_tcon(req);
1251                 if (!NT_STATUS_IS_OK(status)) {
1252                         return_value = smbd_smb2_request_error(req, status);
1253                         break;
1254                 }
1255
1256                 {
1257                         START_PROFILE(smb2_read);
1258                         return_value = smbd_smb2_request_process_read(req);
1259                         END_PROFILE(smb2_read);
1260                 }
1261                 break;
1262
1263         case SMB2_OP_WRITE:
1264                 if (!NT_STATUS_IS_OK(session_status)) {
1265                         return_value = smbd_smb2_request_error(req, session_status);
1266                         break;
1267                 }
1268                 status = smbd_smb2_request_check_tcon(req);
1269                 if (!NT_STATUS_IS_OK(status)) {
1270                         return_value = smbd_smb2_request_error(req, status);
1271                         break;
1272                 }
1273
1274                 {
1275                         START_PROFILE(smb2_write);
1276                         return_value = smbd_smb2_request_process_write(req);
1277                         END_PROFILE(smb2_write);
1278                 }
1279                 break;
1280
1281         case SMB2_OP_LOCK:
1282                 if (!NT_STATUS_IS_OK(session_status)) {
1283                         /* Too ugly to live ? JRA. */
1284                         if (NT_STATUS_EQUAL(session_status,NT_STATUS_USER_SESSION_DELETED)) {
1285                                 session_status = NT_STATUS_FILE_CLOSED;
1286                         }
1287                         return_value = smbd_smb2_request_error(req, session_status);
1288                         break;
1289                 }
1290                 status = smbd_smb2_request_check_tcon(req);
1291                 if (!NT_STATUS_IS_OK(status)) {
1292                         /* Too ugly to live ? JRA. */
1293                         if (NT_STATUS_EQUAL(status,NT_STATUS_NETWORK_NAME_DELETED)) {
1294                                 status = NT_STATUS_FILE_CLOSED;
1295                         }
1296                         return_value = smbd_smb2_request_error(req, status);
1297                         break;
1298                 }
1299
1300                 {
1301                         START_PROFILE(smb2_lock);
1302                         return_value = smbd_smb2_request_process_lock(req);
1303                         END_PROFILE(smb2_lock);
1304                 }
1305                 break;
1306
1307         case SMB2_OP_IOCTL:
1308                 if (!NT_STATUS_IS_OK(session_status)) {
1309                         return_value = smbd_smb2_request_error(req, session_status);
1310                         break;
1311                 }
1312                 status = smbd_smb2_request_check_tcon(req);
1313                 if (!NT_STATUS_IS_OK(status)) {
1314                         return_value = smbd_smb2_request_error(req, status);
1315                         break;
1316                 }
1317
1318                 {
1319                         START_PROFILE(smb2_ioctl);
1320                         return_value = smbd_smb2_request_process_ioctl(req);
1321                         END_PROFILE(smb2_ioctl);
1322                 }
1323                 break;
1324
1325         case SMB2_OP_CANCEL:
1326                 {
1327                         START_PROFILE(smb2_cancel);
1328                         return_value = smbd_smb2_request_process_cancel(req);
1329                         END_PROFILE(smb2_cancel);
1330                 }
1331                 break;
1332
1333         case SMB2_OP_KEEPALIVE:
1334                 {START_PROFILE(smb2_keepalive);
1335                 return_value = smbd_smb2_request_process_keepalive(req);
1336                 END_PROFILE(smb2_keepalive);}
1337                 break;
1338
1339         case SMB2_OP_FIND:
1340                 if (!NT_STATUS_IS_OK(session_status)) {
1341                         return_value = smbd_smb2_request_error(req, session_status);
1342                         break;
1343                 }
1344                 status = smbd_smb2_request_check_tcon(req);
1345                 if (!NT_STATUS_IS_OK(status)) {
1346                         return_value = smbd_smb2_request_error(req, status);
1347                         break;
1348                 }
1349
1350                 {
1351                         START_PROFILE(smb2_find);
1352                         return_value = smbd_smb2_request_process_find(req);
1353                         END_PROFILE(smb2_find);
1354                 }
1355                 break;
1356
1357         case SMB2_OP_NOTIFY:
1358                 if (!NT_STATUS_IS_OK(session_status)) {
1359                         return_value = smbd_smb2_request_error(req, session_status);
1360                         break;
1361                 }
1362                 status = smbd_smb2_request_check_tcon(req);
1363                 if (!NT_STATUS_IS_OK(status)) {
1364                         return_value = smbd_smb2_request_error(req, status);
1365                         break;
1366                 }
1367
1368                 {
1369                         START_PROFILE(smb2_notify);
1370                         return_value = smbd_smb2_request_process_notify(req);
1371                         END_PROFILE(smb2_notify);
1372                 }
1373                 break;
1374
1375         case SMB2_OP_GETINFO:
1376                 if (!NT_STATUS_IS_OK(session_status)) {
1377                         return_value = smbd_smb2_request_error(req, session_status);
1378                         break;
1379                 }
1380                 status = smbd_smb2_request_check_tcon(req);
1381                 if (!NT_STATUS_IS_OK(status)) {
1382                         return_value = smbd_smb2_request_error(req, status);
1383                         break;
1384                 }
1385
1386                 {
1387                         START_PROFILE(smb2_getinfo);
1388                         return_value = smbd_smb2_request_process_getinfo(req);
1389                         END_PROFILE(smb2_getinfo);
1390                 }
1391                 break;
1392
1393         case SMB2_OP_SETINFO:
1394                 if (!NT_STATUS_IS_OK(session_status)) {
1395                         return_value = smbd_smb2_request_error(req, session_status);
1396                         break;
1397                 }
1398                 status = smbd_smb2_request_check_tcon(req);
1399                 if (!NT_STATUS_IS_OK(status)) {
1400                         return_value = smbd_smb2_request_error(req, status);
1401                         break;
1402                 }
1403
1404                 {
1405                         START_PROFILE(smb2_setinfo);
1406                         return_value = smbd_smb2_request_process_setinfo(req);
1407                         END_PROFILE(smb2_setinfo);
1408                 }
1409                 break;
1410
1411         case SMB2_OP_BREAK:
1412                 if (!NT_STATUS_IS_OK(session_status)) {
1413                         return_value = smbd_smb2_request_error(req, session_status);
1414                         break;
1415                 }
1416                 status = smbd_smb2_request_check_tcon(req);
1417                 if (!NT_STATUS_IS_OK(status)) {
1418                         return_value = smbd_smb2_request_error(req, status);
1419                         break;
1420                 }
1421
1422                 {
1423                         START_PROFILE(smb2_break);
1424                         return_value = smbd_smb2_request_process_break(req);
1425                         END_PROFILE(smb2_break);
1426                 }
1427                 break;
1428
1429         default:
1430                 return_value = smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
1431                 break;
1432         }
1433         return return_value;
1434 }
1435
1436 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
1437 {
1438         struct tevent_req *subreq;
1439         int i = req->current_idx;
1440
1441         req->subreq = NULL;
1442
1443         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
1444
1445         /* Set credit for this operation. */
1446         smb2_set_operation_credit(req->sconn,
1447                         &req->in.vector[i],
1448                         &req->out.vector[i]);
1449
1450         if (req->do_signing) {
1451                 NTSTATUS status;
1452                 status = smb2_signing_sign_pdu(req->session->session_key,
1453                                                &req->out.vector[i], 3);
1454                 if (!NT_STATUS_IS_OK(status)) {
1455                         return status;
1456                 }
1457         }
1458
1459         req->current_idx += 3;
1460
1461         if (req->current_idx < req->out.vector_count) {
1462                 /*
1463                  * We must process the remaining compound
1464                  * SMB2 requests before any new incoming SMB2
1465                  * requests. This is because incoming SMB2
1466                  * requests may include a cancel for a
1467                  * compound request we haven't processed
1468                  * yet.
1469                  */
1470                 struct tevent_immediate *im = tevent_create_immediate(req);
1471                 if (!im) {
1472                         return NT_STATUS_NO_MEMORY;
1473                 }
1474                 tevent_schedule_immediate(im,
1475                                         req->sconn->smb2.event_ctx,
1476                                         smbd_smb2_request_dispatch_immediate,
1477                                         req);
1478                 return NT_STATUS_OK;
1479         }
1480
1481         if (DEBUGLEVEL >= 10) {
1482                 dbgtext("smbd_smb2_request_reply: sending...\n");
1483                 print_req_vectors(req);
1484         }
1485
1486         subreq = tstream_writev_queue_send(req,
1487                                            req->sconn->smb2.event_ctx,
1488                                            req->sconn->smb2.stream,
1489                                            req->sconn->smb2.send_queue,
1490                                            req->out.vector,
1491                                            req->out.vector_count);
1492         if (subreq == NULL) {
1493                 return NT_STATUS_NO_MEMORY;
1494         }
1495         tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
1496         /*
1497          * We're done with this request -
1498          * move it off the "being processed" queue.
1499          */
1500         DLIST_REMOVE(req->sconn->smb2.requests, req);
1501
1502         return NT_STATUS_OK;
1503 }
1504
1505 void smbd_smb2_request_dispatch_immediate(struct tevent_context *ctx,
1506                                         struct tevent_immediate *im,
1507                                         void *private_data)
1508 {
1509         struct smbd_smb2_request *req = talloc_get_type_abort(private_data,
1510                                         struct smbd_smb2_request);
1511         struct smbd_server_connection *sconn = req->sconn;
1512         NTSTATUS status;
1513
1514         TALLOC_FREE(im);
1515
1516         if (DEBUGLEVEL >= 10) {
1517                 DEBUG(10,("smbd_smb2_request_dispatch_immediate: idx[%d] of %d vectors\n",
1518                         req->current_idx, req->in.vector_count));
1519                 print_req_vectors(req);
1520         }
1521
1522         status = smbd_smb2_request_dispatch(req);
1523         if (!NT_STATUS_IS_OK(status)) {
1524                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1525                 return;
1526         }
1527 }
1528
1529 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
1530 {
1531         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
1532                                         struct smbd_smb2_request);
1533         struct smbd_server_connection *sconn = req->sconn;
1534         int ret;
1535         int sys_errno;
1536
1537         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1538         TALLOC_FREE(subreq);
1539         TALLOC_FREE(req);
1540         if (ret == -1) {
1541                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1542                 DEBUG(2,("smbd_smb2_request_writev_done: client write error %s\n",
1543                         nt_errstr(status)));
1544                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1545                 return;
1546         }
1547 }
1548
1549 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
1550                                    NTSTATUS status,
1551                                    DATA_BLOB body, DATA_BLOB *dyn,
1552                                    const char *location)
1553 {
1554         uint8_t *outhdr;
1555         int i = req->current_idx;
1556         uint32_t next_command_ofs;
1557
1558         DEBUG(10,("smbd_smb2_request_done_ex: "
1559                   "idx[%d] status[%s] body[%u] dyn[%s:%u] at %s\n",
1560                   i, nt_errstr(status), (unsigned int)body.length,
1561                   dyn ? "yes": "no",
1562                   (unsigned int)(dyn ? dyn->length : 0),
1563                   location));
1564
1565         if (body.length < 2) {
1566                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1567         }
1568
1569         if ((body.length % 2) != 0) {
1570                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1571         }
1572
1573         outhdr = (uint8_t *)req->out.vector[i].iov_base;
1574
1575         next_command_ofs = IVAL(outhdr, SMB2_HDR_NEXT_COMMAND);
1576         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
1577
1578         req->out.vector[i+1].iov_base = (void *)body.data;
1579         req->out.vector[i+1].iov_len = body.length;
1580
1581         if (dyn) {
1582                 req->out.vector[i+2].iov_base   = (void *)dyn->data;
1583                 req->out.vector[i+2].iov_len    = dyn->length;
1584         } else {
1585                 req->out.vector[i+2].iov_base = NULL;
1586                 req->out.vector[i+2].iov_len = 0;
1587         }
1588
1589         /* see if we need to recalculate the offset to the next response */
1590         if (next_command_ofs > 0) {
1591                 next_command_ofs  = SMB2_HDR_BODY;
1592                 next_command_ofs += req->out.vector[i+1].iov_len;
1593                 next_command_ofs += req->out.vector[i+2].iov_len;
1594         }
1595
1596         if ((next_command_ofs % 8) != 0) {
1597                 size_t pad_size = 8 - (next_command_ofs % 8);
1598                 if (req->out.vector[i+2].iov_len == 0) {
1599                         /*
1600                          * if the dyn buffer is empty
1601                          * we can use it to add padding
1602                          */
1603                         uint8_t *pad;
1604
1605                         pad = talloc_zero_array(req->out.vector,
1606                                                 uint8_t, pad_size);
1607                         if (pad == NULL) {
1608                                 return smbd_smb2_request_error(req,
1609                                                 NT_STATUS_NO_MEMORY);
1610                         }
1611
1612                         req->out.vector[i+2].iov_base = (void *)pad;
1613                         req->out.vector[i+2].iov_len = pad_size;
1614                 } else {
1615                         /*
1616                          * For now we copy the dynamic buffer
1617                          * and add the padding to the new buffer
1618                          */
1619                         size_t old_size;
1620                         uint8_t *old_dyn;
1621                         size_t new_size;
1622                         uint8_t *new_dyn;
1623
1624                         old_size = req->out.vector[i+2].iov_len;
1625                         old_dyn = (uint8_t *)req->out.vector[i+2].iov_base;
1626
1627                         new_size = old_size + pad_size;
1628                         new_dyn = talloc_zero_array(req->out.vector,
1629                                                uint8_t, new_size);
1630                         if (new_dyn == NULL) {
1631                                 return smbd_smb2_request_error(req,
1632                                                 NT_STATUS_NO_MEMORY);
1633                         }
1634
1635                         memcpy(new_dyn, old_dyn, old_size);
1636                         memset(new_dyn + old_size, 0, pad_size);
1637
1638                         req->out.vector[i+2].iov_base = (void *)new_dyn;
1639                         req->out.vector[i+2].iov_len = new_size;
1640                 }
1641                 next_command_ofs += pad_size;
1642         }
1643
1644         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
1645
1646         return smbd_smb2_request_reply(req);
1647 }
1648
1649 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
1650                                     NTSTATUS status,
1651                                     DATA_BLOB *info,
1652                                     const char *location)
1653 {
1654         DATA_BLOB body;
1655         int i = req->current_idx;
1656         uint8_t *outhdr = (uint8_t *)req->out.vector[i].iov_base;
1657
1658         DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s] |%s| at %s\n",
1659                   i, nt_errstr(status), info ? " +info" : "",
1660                   location));
1661
1662         body.data = outhdr + SMB2_HDR_BODY;
1663         body.length = 8;
1664         SSVAL(body.data, 0, 9);
1665
1666         if (info) {
1667                 SIVAL(body.data, 0x04, info->length);
1668         } else {
1669                 /* Allocated size of req->out.vector[i].iov_base
1670                  * *MUST BE* OUTVEC_ALLOC_SIZE. So we have room for
1671                  * 1 byte without having to do an alloc.
1672                  */
1673                 info = talloc_zero_array(req->out.vector,
1674                                         DATA_BLOB,
1675                                         1);
1676                 if (!info) {
1677                         return NT_STATUS_NO_MEMORY;
1678                 }
1679                 info->data = ((uint8_t *)outhdr) +
1680                         OUTVEC_ALLOC_SIZE - 1;
1681                 info->length = 1;
1682                 SCVAL(info->data, 0, 0);
1683         }
1684
1685         /*
1686          * if a request fails, all other remaining
1687          * compounded requests should fail too
1688          */
1689         req->next_status = NT_STATUS_INVALID_PARAMETER;
1690
1691         return smbd_smb2_request_done_ex(req, status, body, info, __location__);
1692 }
1693
1694
1695 struct smbd_smb2_send_oplock_break_state {
1696         struct smbd_server_connection *sconn;
1697         uint8_t buf[4 + SMB2_HDR_BODY + 0x18];
1698         struct iovec vector;
1699 };
1700
1701 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq);
1702
1703 NTSTATUS smbd_smb2_send_oplock_break(struct smbd_server_connection *sconn,
1704                                      uint64_t file_id_persistent,
1705                                      uint64_t file_id_volatile,
1706                                      uint8_t oplock_level)
1707 {
1708         struct smbd_smb2_send_oplock_break_state *state;
1709         struct tevent_req *subreq;
1710         uint8_t *hdr;
1711         uint8_t *body;
1712
1713         state = talloc(sconn, struct smbd_smb2_send_oplock_break_state);
1714         if (state == NULL) {
1715                 return NT_STATUS_NO_MEMORY;
1716         }
1717         state->sconn = sconn;
1718
1719         state->vector.iov_base = (void *)state->buf;
1720         state->vector.iov_len = sizeof(state->buf);
1721
1722         _smb2_setlen(state->buf, sizeof(state->buf) - 4);
1723         hdr = state->buf + 4;
1724         body = hdr + SMB2_HDR_BODY;
1725
1726         SIVAL(hdr, 0,                           SMB2_MAGIC);
1727         SSVAL(hdr, SMB2_HDR_LENGTH,             SMB2_HDR_BODY);
1728         SSVAL(hdr, SMB2_HDR_EPOCH,              0);
1729         SIVAL(hdr, SMB2_HDR_STATUS,             0);
1730         SSVAL(hdr, SMB2_HDR_OPCODE,             SMB2_OP_BREAK);
1731         SSVAL(hdr, SMB2_HDR_CREDIT,             0);
1732         SIVAL(hdr, SMB2_HDR_FLAGS,              SMB2_HDR_FLAG_REDIRECT);
1733         SIVAL(hdr, SMB2_HDR_NEXT_COMMAND,       0);
1734         SBVAL(hdr, SMB2_HDR_MESSAGE_ID,         UINT64_MAX);
1735         SIVAL(hdr, SMB2_HDR_PID,                0);
1736         SIVAL(hdr, SMB2_HDR_TID,                0);
1737         SBVAL(hdr, SMB2_HDR_SESSION_ID,         0);
1738         memset(hdr+SMB2_HDR_SIGNATURE, 0, 16);
1739
1740         SSVAL(body, 0x00, 0x18);
1741
1742         SCVAL(body, 0x02, oplock_level);
1743         SCVAL(body, 0x03, 0);           /* reserved */
1744         SIVAL(body, 0x04, 0);           /* reserved */
1745         SBVAL(body, 0x08, file_id_persistent);
1746         SBVAL(body, 0x10, file_id_volatile);
1747
1748         subreq = tstream_writev_queue_send(state,
1749                                            sconn->smb2.event_ctx,
1750                                            sconn->smb2.stream,
1751                                            sconn->smb2.send_queue,
1752                                            &state->vector, 1);
1753         if (subreq == NULL) {
1754                 return NT_STATUS_NO_MEMORY;
1755         }
1756         tevent_req_set_callback(subreq,
1757                                 smbd_smb2_oplock_break_writev_done,
1758                                 state);
1759
1760         return NT_STATUS_OK;
1761 }
1762
1763 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq)
1764 {
1765         struct smbd_smb2_send_oplock_break_state *state =
1766                 tevent_req_callback_data(subreq,
1767                 struct smbd_smb2_send_oplock_break_state);
1768         struct smbd_server_connection *sconn = state->sconn;
1769         int ret;
1770         int sys_errno;
1771
1772         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1773         TALLOC_FREE(subreq);
1774         if (ret == -1) {
1775                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1776                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1777                 return;
1778         }
1779
1780         TALLOC_FREE(state);
1781 }
1782
1783 struct smbd_smb2_request_read_state {
1784         size_t missing;
1785         bool asked_for_header;
1786         struct smbd_smb2_request *smb2_req;
1787 };
1788
1789 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1790                                          void *private_data,
1791                                          TALLOC_CTX *mem_ctx,
1792                                          struct iovec **_vector,
1793                                          size_t *_count);
1794 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
1795
1796 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
1797                                         struct tevent_context *ev,
1798                                         struct smbd_server_connection *sconn)
1799 {
1800         struct tevent_req *req;
1801         struct smbd_smb2_request_read_state *state;
1802         struct tevent_req *subreq;
1803
1804         req = tevent_req_create(mem_ctx, &state,
1805                                 struct smbd_smb2_request_read_state);
1806         if (req == NULL) {
1807                 return NULL;
1808         }
1809         state->missing = 0;
1810         state->asked_for_header = false;
1811
1812         state->smb2_req = smbd_smb2_request_allocate(state);
1813         if (tevent_req_nomem(state->smb2_req, req)) {
1814                 return tevent_req_post(req, ev);
1815         }
1816         state->smb2_req->sconn = sconn;
1817
1818         subreq = tstream_readv_pdu_queue_send(state, ev, sconn->smb2.stream,
1819                                               sconn->smb2.recv_queue,
1820                                               smbd_smb2_request_next_vector,
1821                                               state);
1822         if (tevent_req_nomem(subreq, req)) {
1823                 return tevent_req_post(req, ev);
1824         }
1825         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
1826
1827         return req;
1828 }
1829
1830 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1831                                          void *private_data,
1832                                          TALLOC_CTX *mem_ctx,
1833                                          struct iovec **_vector,
1834                                          size_t *_count)
1835 {
1836         struct smbd_smb2_request_read_state *state =
1837                 talloc_get_type_abort(private_data,
1838                 struct smbd_smb2_request_read_state);
1839         struct smbd_smb2_request *req = state->smb2_req;
1840         struct iovec *vector;
1841         int idx = req->in.vector_count;
1842         size_t len = 0;
1843         uint8_t *buf = NULL;
1844
1845         if (req->in.vector_count == 0) {
1846                 /*
1847                  * first we need to get the NBT header
1848                  */
1849                 req->in.vector = talloc_array(req, struct iovec,
1850                                               req->in.vector_count + 1);
1851                 if (req->in.vector == NULL) {
1852                         return -1;
1853                 }
1854                 req->in.vector_count += 1;
1855
1856                 req->in.vector[idx].iov_base    = (void *)req->in.nbt_hdr;
1857                 req->in.vector[idx].iov_len     = 4;
1858
1859                 vector = talloc_array(mem_ctx, struct iovec, 1);
1860                 if (vector == NULL) {
1861                         return -1;
1862                 }
1863
1864                 vector[0] = req->in.vector[idx];
1865
1866                 *_vector = vector;
1867                 *_count = 1;
1868                 return 0;
1869         }
1870
1871         if (req->in.vector_count == 1) {
1872                 /*
1873                  * Now we analyze the NBT header
1874                  */
1875                 state->missing = smb2_len(req->in.vector[0].iov_base);
1876
1877                 if (state->missing == 0) {
1878                         /* if there're no remaining bytes, we're done */
1879                         *_vector = NULL;
1880                         *_count = 0;
1881                         return 0;
1882                 }
1883
1884                 req->in.vector = talloc_realloc(req, req->in.vector,
1885                                                 struct iovec,
1886                                                 req->in.vector_count + 1);
1887                 if (req->in.vector == NULL) {
1888                         return -1;
1889                 }
1890                 req->in.vector_count += 1;
1891
1892                 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
1893                         /*
1894                          * it's a special NBT message,
1895                          * so get all remaining bytes
1896                          */
1897                         len = state->missing;
1898                 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
1899                         /*
1900                          * it's an invalid message, just read what we can get
1901                          * and let the caller handle the error
1902                          */
1903                         len = state->missing;
1904                 } else {
1905                         /*
1906                          * We assume it's a SMB2 request,
1907                          * and we first get the header and the
1908                          * first 2 bytes (the struct size) of the body
1909                          */
1910                         len = SMB2_HDR_BODY + 2;
1911
1912                         state->asked_for_header = true;
1913                 }
1914
1915                 state->missing -= len;
1916
1917                 buf = talloc_array(req->in.vector, uint8_t, len);
1918                 if (buf == NULL) {
1919                         return -1;
1920                 }
1921
1922                 req->in.vector[idx].iov_base    = (void *)buf;
1923                 req->in.vector[idx].iov_len     = len;
1924
1925                 vector = talloc_array(mem_ctx, struct iovec, 1);
1926                 if (vector == NULL) {
1927                         return -1;
1928                 }
1929
1930                 vector[0] = req->in.vector[idx];
1931
1932                 *_vector = vector;
1933                 *_count = 1;
1934                 return 0;
1935         }
1936
1937         if (state->missing == 0) {
1938                 /* if there're no remaining bytes, we're done */
1939                 *_vector = NULL;
1940                 *_count = 0;
1941                 return 0;
1942         }
1943
1944         if (state->asked_for_header) {
1945                 const uint8_t *hdr;
1946                 size_t full_size;
1947                 size_t next_command_ofs;
1948                 size_t body_size;
1949                 uint8_t *body;
1950                 size_t dyn_size;
1951                 uint8_t *dyn;
1952                 bool invalid = false;
1953
1954                 state->asked_for_header = false;
1955
1956                 /*
1957                  * We got the SMB2 header and the first 2 bytes
1958                  * of the body. We fix the size to just the header
1959                  * and manually copy the 2 first bytes to the body section
1960                  */
1961                 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
1962                 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
1963
1964                 /* allocate vectors for body and dynamic areas */
1965                 req->in.vector = talloc_realloc(req, req->in.vector,
1966                                                 struct iovec,
1967                                                 req->in.vector_count + 2);
1968                 if (req->in.vector == NULL) {
1969                         return -1;
1970                 }
1971                 req->in.vector_count += 2;
1972
1973                 full_size = state->missing + SMB2_HDR_BODY + 2;
1974                 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
1975                 body_size = SVAL(hdr, SMB2_HDR_BODY);
1976
1977                 if (next_command_ofs != 0) {
1978                         if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
1979                                 /*
1980                                  * this is invalid, just return a zero
1981                                  * body and let the caller deal with the error
1982                                  */
1983                                 invalid = true;
1984                         } else if (next_command_ofs > full_size) {
1985                                 /*
1986                                  * this is invalid, just return a zero
1987                                  * body and let the caller deal with the error
1988                                  */
1989                                 invalid = true;
1990                         } else {
1991                                 full_size = next_command_ofs;
1992                         }
1993                 }
1994
1995                 if (!invalid) {
1996                         if (body_size < 2) {
1997                                 /*
1998                                  * this is invalid, just return a zero
1999                                  * body and let the caller deal with the error
2000                                  */
2001                                 invalid = true;
2002                         }
2003
2004                         if ((body_size % 2) != 0) {
2005                                 body_size -= 1;
2006                         }
2007
2008                         if (body_size > (full_size - SMB2_HDR_BODY)) {
2009                                 /*
2010                                  * this is invalid, just return a zero
2011                                  * body and let the caller deal with the error
2012                                  */
2013                                 invalid = true;
2014                         }
2015                 }
2016
2017                 if (invalid) {
2018                         /* the caller should check this */
2019                         body_size = 2;
2020                 }
2021
2022                 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
2023
2024                 state->missing -= (body_size - 2) + dyn_size;
2025
2026                 body = talloc_array(req->in.vector, uint8_t, body_size);
2027                 if (body == NULL) {
2028                         return -1;
2029                 }
2030
2031                 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
2032                 if (dyn == NULL) {
2033                         return -1;
2034                 }
2035
2036                 req->in.vector[idx].iov_base    = (void *)body;
2037                 req->in.vector[idx].iov_len     = body_size;
2038                 req->in.vector[idx+1].iov_base  = (void *)dyn;
2039                 req->in.vector[idx+1].iov_len   = dyn_size;
2040
2041                 vector = talloc_array(mem_ctx, struct iovec, 2);
2042                 if (vector == NULL) {
2043                         return -1;
2044                 }
2045
2046                 /*
2047                  * the first 2 bytes of the body were already fetched
2048                  * together with the header
2049                  */
2050                 memcpy(body, hdr + SMB2_HDR_BODY, 2);
2051                 vector[0].iov_base = body + 2;
2052                 vector[0].iov_len = body_size - 2;
2053
2054                 vector[1] = req->in.vector[idx+1];
2055
2056                 *_vector = vector;
2057                 *_count = 2;
2058                 return 0;
2059         }
2060
2061         /*
2062          * when we endup here, we're looking for a new SMB2 request
2063          * next. And we ask for its header and the first 2 bytes of
2064          * the body (like we did for the first SMB2 request).
2065          */
2066
2067         req->in.vector = talloc_realloc(req, req->in.vector,
2068                                         struct iovec,
2069                                         req->in.vector_count + 1);
2070         if (req->in.vector == NULL) {
2071                 return -1;
2072         }
2073         req->in.vector_count += 1;
2074
2075         /*
2076          * We assume it's a SMB2 request,
2077          * and we first get the header and the
2078          * first 2 bytes (the struct size) of the body
2079          */
2080         len = SMB2_HDR_BODY + 2;
2081
2082         if (len > state->missing) {
2083                 /* let the caller handle the error */
2084                 len = state->missing;
2085         }
2086
2087         state->missing -= len;
2088         state->asked_for_header = true;
2089
2090         buf = talloc_array(req->in.vector, uint8_t, len);
2091         if (buf == NULL) {
2092                 return -1;
2093         }
2094
2095         req->in.vector[idx].iov_base    = (void *)buf;
2096         req->in.vector[idx].iov_len     = len;
2097
2098         vector = talloc_array(mem_ctx, struct iovec, 1);
2099         if (vector == NULL) {
2100                 return -1;
2101         }
2102
2103         vector[0] = req->in.vector[idx];
2104
2105         *_vector = vector;
2106         *_count = 1;
2107         return 0;
2108 }
2109
2110 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
2111 {
2112         struct tevent_req *req =
2113                 tevent_req_callback_data(subreq,
2114                 struct tevent_req);
2115         int ret;
2116         int sys_errno;
2117         NTSTATUS status;
2118
2119         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
2120         if (ret == -1) {
2121                 status = map_nt_error_from_unix(sys_errno);
2122                 tevent_req_nterror(req, status);
2123                 return;
2124         }
2125
2126         tevent_req_done(req);
2127 }
2128
2129 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
2130                                             TALLOC_CTX *mem_ctx,
2131                                             struct smbd_smb2_request **_smb2_req)
2132 {
2133         struct smbd_smb2_request_read_state *state =
2134                 tevent_req_data(req,
2135                 struct smbd_smb2_request_read_state);
2136         NTSTATUS status;
2137
2138         if (tevent_req_is_nterror(req, &status)) {
2139                 tevent_req_received(req);
2140                 return status;
2141         }
2142
2143         talloc_steal(mem_ctx, state->smb2_req->mem_pool);
2144         *_smb2_req = state->smb2_req;
2145         tevent_req_received(req);
2146         return NT_STATUS_OK;
2147 }
2148
2149 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
2150
2151 void smbd_smb2_first_negprot(struct smbd_server_connection *sconn,
2152                              const uint8_t *inbuf, size_t size)
2153 {
2154         NTSTATUS status;
2155         struct smbd_smb2_request *req = NULL;
2156         struct tevent_req *subreq;
2157
2158         if (lp_security() == SEC_SHARE) {
2159                 DEBUG(2,("WARNING!!: \"security = share\" is deprecated for "
2160                         "SMB2 servers. Mapping to \"security = user\" and "
2161                         "\"map to guest = Bad User\"\n" ));
2162                 lp_do_parameter(-1, "security", "user");
2163                 lp_do_parameter(-1, "map to guest", "Bad User");
2164         }
2165
2166         DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
2167                  (unsigned int)size));
2168
2169         status = smbd_initialize_smb2(sconn);
2170         if (!NT_STATUS_IS_OK(status)) {
2171                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2172                 return;
2173         }
2174
2175         status = smbd_smb2_request_create(sconn, inbuf, size, &req);
2176         if (!NT_STATUS_IS_OK(status)) {
2177                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2178                 return;
2179         }
2180
2181         status = smbd_smb2_request_setup_out(req, 1);
2182         if (!NT_STATUS_IS_OK(status)) {
2183                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2184                 return;
2185         }
2186
2187         status = smbd_smb2_request_dispatch(req);
2188         if (!NT_STATUS_IS_OK(status)) {
2189                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2190                 return;
2191         }
2192
2193         /* ask for the next request */
2194         subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
2195         if (subreq == NULL) {
2196                 smbd_server_connection_terminate(sconn, "no memory for reading");
2197                 return;
2198         }
2199         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);
2200 }
2201
2202 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
2203 {
2204         struct smbd_server_connection *sconn = tevent_req_callback_data(subreq,
2205                                                struct smbd_server_connection);
2206         NTSTATUS status;
2207         struct smbd_smb2_request *req = NULL;
2208
2209         status = smbd_smb2_request_read_recv(subreq, sconn, &req);
2210         TALLOC_FREE(subreq);
2211         if (!NT_STATUS_IS_OK(status)) {
2212                 DEBUG(2,("smbd_smb2_request_incoming: client read error %s\n",
2213                         nt_errstr(status)));
2214                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2215                 return;
2216         }
2217
2218         if (req->in.nbt_hdr[0] != 0x00) {
2219                 DEBUG(1,("smbd_smb2_request_incoming: ignore NBT[0x%02X] msg\n",
2220                          req->in.nbt_hdr[0]));
2221                 TALLOC_FREE(req);
2222                 goto next;
2223         }
2224
2225         req->current_idx = 1;
2226
2227         DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
2228                  req->current_idx, req->in.vector_count));
2229
2230         status = smbd_smb2_request_validate(req);
2231         if (!NT_STATUS_IS_OK(status)) {
2232                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2233                 return;
2234         }
2235
2236         status = smbd_smb2_request_setup_out(req, 5);
2237         if (!NT_STATUS_IS_OK(status)) {
2238                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2239                 return;
2240         }
2241
2242         status = smbd_smb2_request_dispatch(req);
2243         if (!NT_STATUS_IS_OK(status)) {
2244                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2245                 return;
2246         }
2247
2248 next:
2249         /* ask for the next request (this constructs the main loop) */
2250         subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
2251         if (subreq == NULL) {
2252                 smbd_server_connection_terminate(sconn, "no memory for reading");
2253                 return;
2254         }
2255         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);
2256 }