CREATE in a compound CREATE/NOTIFY sequence was being passed through set_operation_cr...
[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         req->current_idx += 3;
1447
1448         if (req->current_idx < req->out.vector_count) {
1449                 /*
1450                  * We must process the remaining compound
1451                  * SMB2 requests before any new incoming SMB2
1452                  * requests. This is because incoming SMB2
1453                  * requests may include a cancel for a
1454                  * compound request we haven't processed
1455                  * yet.
1456                  */
1457                 struct tevent_immediate *im = tevent_create_immediate(req);
1458                 if (!im) {
1459                         return NT_STATUS_NO_MEMORY;
1460                 }
1461                 tevent_schedule_immediate(im,
1462                                         req->sconn->smb2.event_ctx,
1463                                         smbd_smb2_request_dispatch_immediate,
1464                                         req);
1465                 return NT_STATUS_OK;
1466         }
1467
1468         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
1469
1470         /* Set credit for this operation (zero credits if this
1471            is a final reply for an async operation). */
1472         smb2_set_operation_credit(req->sconn,
1473                         req->async ? NULL : &req->in.vector[i],
1474                         &req->out.vector[i]);
1475
1476         if (req->do_signing) {
1477                 NTSTATUS status;
1478                 status = smb2_signing_sign_pdu(req->session->session_key,
1479                                                &req->out.vector[i], 3);
1480                 if (!NT_STATUS_IS_OK(status)) {
1481                         return status;
1482                 }
1483         }
1484
1485         if (DEBUGLEVEL >= 10) {
1486                 dbgtext("smbd_smb2_request_reply: sending...\n");
1487                 print_req_vectors(req);
1488         }
1489
1490         /* I am a sick, sick man... :-). Sendfile hack ... JRA. */
1491         if (req->out.vector_count == 4 &&
1492                         req->out.vector[3].iov_base == NULL &&
1493                         req->out.vector[3].iov_len != 0) {
1494                 /* Dynamic part is NULL. Chop it off,
1495                    We're going to send it via sendfile. */
1496                 req->out.vector_count -= 1;
1497         }
1498
1499         subreq = tstream_writev_queue_send(req,
1500                                            req->sconn->smb2.event_ctx,
1501                                            req->sconn->smb2.stream,
1502                                            req->sconn->smb2.send_queue,
1503                                            req->out.vector,
1504                                            req->out.vector_count);
1505         if (subreq == NULL) {
1506                 return NT_STATUS_NO_MEMORY;
1507         }
1508         tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
1509         /*
1510          * We're done with this request -
1511          * move it off the "being processed" queue.
1512          */
1513         DLIST_REMOVE(req->sconn->smb2.requests, req);
1514
1515         return NT_STATUS_OK;
1516 }
1517
1518 void smbd_smb2_request_dispatch_immediate(struct tevent_context *ctx,
1519                                         struct tevent_immediate *im,
1520                                         void *private_data)
1521 {
1522         struct smbd_smb2_request *req = talloc_get_type_abort(private_data,
1523                                         struct smbd_smb2_request);
1524         struct smbd_server_connection *sconn = req->sconn;
1525         NTSTATUS status;
1526
1527         TALLOC_FREE(im);
1528
1529         if (DEBUGLEVEL >= 10) {
1530                 DEBUG(10,("smbd_smb2_request_dispatch_immediate: idx[%d] of %d vectors\n",
1531                         req->current_idx, req->in.vector_count));
1532                 print_req_vectors(req);
1533         }
1534
1535         status = smbd_smb2_request_dispatch(req);
1536         if (!NT_STATUS_IS_OK(status)) {
1537                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1538                 return;
1539         }
1540 }
1541
1542 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
1543 {
1544         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
1545                                         struct smbd_smb2_request);
1546         struct smbd_server_connection *sconn = req->sconn;
1547         int ret;
1548         int sys_errno;
1549
1550         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1551         TALLOC_FREE(subreq);
1552         TALLOC_FREE(req);
1553         if (ret == -1) {
1554                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1555                 DEBUG(2,("smbd_smb2_request_writev_done: client write error %s\n",
1556                         nt_errstr(status)));
1557                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1558                 return;
1559         }
1560 }
1561
1562 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
1563                                    NTSTATUS status,
1564                                    DATA_BLOB body, DATA_BLOB *dyn,
1565                                    const char *location)
1566 {
1567         uint8_t *outhdr;
1568         int i = req->current_idx;
1569         uint32_t next_command_ofs;
1570
1571         DEBUG(10,("smbd_smb2_request_done_ex: "
1572                   "idx[%d] status[%s] body[%u] dyn[%s:%u] at %s\n",
1573                   i, nt_errstr(status), (unsigned int)body.length,
1574                   dyn ? "yes": "no",
1575                   (unsigned int)(dyn ? dyn->length : 0),
1576                   location));
1577
1578         if (body.length < 2) {
1579                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1580         }
1581
1582         if ((body.length % 2) != 0) {
1583                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1584         }
1585
1586         outhdr = (uint8_t *)req->out.vector[i].iov_base;
1587
1588         next_command_ofs = IVAL(outhdr, SMB2_HDR_NEXT_COMMAND);
1589         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
1590
1591         req->out.vector[i+1].iov_base = (void *)body.data;
1592         req->out.vector[i+1].iov_len = body.length;
1593
1594         if (dyn) {
1595                 req->out.vector[i+2].iov_base   = (void *)dyn->data;
1596                 req->out.vector[i+2].iov_len    = dyn->length;
1597         } else {
1598                 req->out.vector[i+2].iov_base = NULL;
1599                 req->out.vector[i+2].iov_len = 0;
1600         }
1601
1602         /* see if we need to recalculate the offset to the next response */
1603         if (next_command_ofs > 0) {
1604                 next_command_ofs  = SMB2_HDR_BODY;
1605                 next_command_ofs += req->out.vector[i+1].iov_len;
1606                 next_command_ofs += req->out.vector[i+2].iov_len;
1607         }
1608
1609         if ((next_command_ofs % 8) != 0) {
1610                 size_t pad_size = 8 - (next_command_ofs % 8);
1611                 if (req->out.vector[i+2].iov_len == 0) {
1612                         /*
1613                          * if the dyn buffer is empty
1614                          * we can use it to add padding
1615                          */
1616                         uint8_t *pad;
1617
1618                         pad = talloc_zero_array(req->out.vector,
1619                                                 uint8_t, pad_size);
1620                         if (pad == NULL) {
1621                                 return smbd_smb2_request_error(req,
1622                                                 NT_STATUS_NO_MEMORY);
1623                         }
1624
1625                         req->out.vector[i+2].iov_base = (void *)pad;
1626                         req->out.vector[i+2].iov_len = pad_size;
1627                 } else {
1628                         /*
1629                          * For now we copy the dynamic buffer
1630                          * and add the padding to the new buffer
1631                          */
1632                         size_t old_size;
1633                         uint8_t *old_dyn;
1634                         size_t new_size;
1635                         uint8_t *new_dyn;
1636
1637                         old_size = req->out.vector[i+2].iov_len;
1638                         old_dyn = (uint8_t *)req->out.vector[i+2].iov_base;
1639
1640                         new_size = old_size + pad_size;
1641                         new_dyn = talloc_zero_array(req->out.vector,
1642                                                uint8_t, new_size);
1643                         if (new_dyn == NULL) {
1644                                 return smbd_smb2_request_error(req,
1645                                                 NT_STATUS_NO_MEMORY);
1646                         }
1647
1648                         memcpy(new_dyn, old_dyn, old_size);
1649                         memset(new_dyn + old_size, 0, pad_size);
1650
1651                         req->out.vector[i+2].iov_base = (void *)new_dyn;
1652                         req->out.vector[i+2].iov_len = new_size;
1653                 }
1654                 next_command_ofs += pad_size;
1655         }
1656
1657         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
1658
1659         return smbd_smb2_request_reply(req);
1660 }
1661
1662 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
1663                                     NTSTATUS status,
1664                                     DATA_BLOB *info,
1665                                     const char *location)
1666 {
1667         DATA_BLOB body;
1668         int i = req->current_idx;
1669         uint8_t *outhdr = (uint8_t *)req->out.vector[i].iov_base;
1670
1671         DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s] |%s| at %s\n",
1672                   i, nt_errstr(status), info ? " +info" : "",
1673                   location));
1674
1675         body.data = outhdr + SMB2_HDR_BODY;
1676         body.length = 8;
1677         SSVAL(body.data, 0, 9);
1678
1679         if (info) {
1680                 SIVAL(body.data, 0x04, info->length);
1681         } else {
1682                 /* Allocated size of req->out.vector[i].iov_base
1683                  * *MUST BE* OUTVEC_ALLOC_SIZE. So we have room for
1684                  * 1 byte without having to do an alloc.
1685                  */
1686                 info = talloc_zero_array(req->out.vector,
1687                                         DATA_BLOB,
1688                                         1);
1689                 if (!info) {
1690                         return NT_STATUS_NO_MEMORY;
1691                 }
1692                 info->data = ((uint8_t *)outhdr) +
1693                         OUTVEC_ALLOC_SIZE - 1;
1694                 info->length = 1;
1695                 SCVAL(info->data, 0, 0);
1696         }
1697
1698         /*
1699          * if a request fails, all other remaining
1700          * compounded requests should fail too
1701          */
1702         req->next_status = NT_STATUS_INVALID_PARAMETER;
1703
1704         return smbd_smb2_request_done_ex(req, status, body, info, __location__);
1705 }
1706
1707
1708 struct smbd_smb2_send_oplock_break_state {
1709         struct smbd_server_connection *sconn;
1710         uint8_t buf[4 + SMB2_HDR_BODY + 0x18];
1711         struct iovec vector;
1712 };
1713
1714 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq);
1715
1716 NTSTATUS smbd_smb2_send_oplock_break(struct smbd_server_connection *sconn,
1717                                      uint64_t file_id_persistent,
1718                                      uint64_t file_id_volatile,
1719                                      uint8_t oplock_level)
1720 {
1721         struct smbd_smb2_send_oplock_break_state *state;
1722         struct tevent_req *subreq;
1723         uint8_t *hdr;
1724         uint8_t *body;
1725
1726         state = talloc(sconn, struct smbd_smb2_send_oplock_break_state);
1727         if (state == NULL) {
1728                 return NT_STATUS_NO_MEMORY;
1729         }
1730         state->sconn = sconn;
1731
1732         state->vector.iov_base = (void *)state->buf;
1733         state->vector.iov_len = sizeof(state->buf);
1734
1735         _smb2_setlen(state->buf, sizeof(state->buf) - 4);
1736         hdr = state->buf + 4;
1737         body = hdr + SMB2_HDR_BODY;
1738
1739         SIVAL(hdr, 0,                           SMB2_MAGIC);
1740         SSVAL(hdr, SMB2_HDR_LENGTH,             SMB2_HDR_BODY);
1741         SSVAL(hdr, SMB2_HDR_EPOCH,              0);
1742         SIVAL(hdr, SMB2_HDR_STATUS,             0);
1743         SSVAL(hdr, SMB2_HDR_OPCODE,             SMB2_OP_BREAK);
1744         SSVAL(hdr, SMB2_HDR_CREDIT,             0);
1745         SIVAL(hdr, SMB2_HDR_FLAGS,              SMB2_HDR_FLAG_REDIRECT);
1746         SIVAL(hdr, SMB2_HDR_NEXT_COMMAND,       0);
1747         SBVAL(hdr, SMB2_HDR_MESSAGE_ID,         UINT64_MAX);
1748         SIVAL(hdr, SMB2_HDR_PID,                0);
1749         SIVAL(hdr, SMB2_HDR_TID,                0);
1750         SBVAL(hdr, SMB2_HDR_SESSION_ID,         0);
1751         memset(hdr+SMB2_HDR_SIGNATURE, 0, 16);
1752
1753         SSVAL(body, 0x00, 0x18);
1754
1755         SCVAL(body, 0x02, oplock_level);
1756         SCVAL(body, 0x03, 0);           /* reserved */
1757         SIVAL(body, 0x04, 0);           /* reserved */
1758         SBVAL(body, 0x08, file_id_persistent);
1759         SBVAL(body, 0x10, file_id_volatile);
1760
1761         subreq = tstream_writev_queue_send(state,
1762                                            sconn->smb2.event_ctx,
1763                                            sconn->smb2.stream,
1764                                            sconn->smb2.send_queue,
1765                                            &state->vector, 1);
1766         if (subreq == NULL) {
1767                 return NT_STATUS_NO_MEMORY;
1768         }
1769         tevent_req_set_callback(subreq,
1770                                 smbd_smb2_oplock_break_writev_done,
1771                                 state);
1772
1773         return NT_STATUS_OK;
1774 }
1775
1776 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq)
1777 {
1778         struct smbd_smb2_send_oplock_break_state *state =
1779                 tevent_req_callback_data(subreq,
1780                 struct smbd_smb2_send_oplock_break_state);
1781         struct smbd_server_connection *sconn = state->sconn;
1782         int ret;
1783         int sys_errno;
1784
1785         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1786         TALLOC_FREE(subreq);
1787         if (ret == -1) {
1788                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1789                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1790                 return;
1791         }
1792
1793         TALLOC_FREE(state);
1794 }
1795
1796 struct smbd_smb2_request_read_state {
1797         size_t missing;
1798         bool asked_for_header;
1799         struct smbd_smb2_request *smb2_req;
1800 };
1801
1802 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1803                                          void *private_data,
1804                                          TALLOC_CTX *mem_ctx,
1805                                          struct iovec **_vector,
1806                                          size_t *_count);
1807 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
1808
1809 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
1810                                         struct tevent_context *ev,
1811                                         struct smbd_server_connection *sconn)
1812 {
1813         struct tevent_req *req;
1814         struct smbd_smb2_request_read_state *state;
1815         struct tevent_req *subreq;
1816
1817         req = tevent_req_create(mem_ctx, &state,
1818                                 struct smbd_smb2_request_read_state);
1819         if (req == NULL) {
1820                 return NULL;
1821         }
1822         state->missing = 0;
1823         state->asked_for_header = false;
1824
1825         state->smb2_req = smbd_smb2_request_allocate(state);
1826         if (tevent_req_nomem(state->smb2_req, req)) {
1827                 return tevent_req_post(req, ev);
1828         }
1829         state->smb2_req->sconn = sconn;
1830
1831         subreq = tstream_readv_pdu_queue_send(state, ev, sconn->smb2.stream,
1832                                               sconn->smb2.recv_queue,
1833                                               smbd_smb2_request_next_vector,
1834                                               state);
1835         if (tevent_req_nomem(subreq, req)) {
1836                 return tevent_req_post(req, ev);
1837         }
1838         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
1839
1840         return req;
1841 }
1842
1843 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1844                                          void *private_data,
1845                                          TALLOC_CTX *mem_ctx,
1846                                          struct iovec **_vector,
1847                                          size_t *_count)
1848 {
1849         struct smbd_smb2_request_read_state *state =
1850                 talloc_get_type_abort(private_data,
1851                 struct smbd_smb2_request_read_state);
1852         struct smbd_smb2_request *req = state->smb2_req;
1853         struct iovec *vector;
1854         int idx = req->in.vector_count;
1855         size_t len = 0;
1856         uint8_t *buf = NULL;
1857
1858         if (req->in.vector_count == 0) {
1859                 /*
1860                  * first we need to get the NBT header
1861                  */
1862                 req->in.vector = talloc_array(req, struct iovec,
1863                                               req->in.vector_count + 1);
1864                 if (req->in.vector == NULL) {
1865                         return -1;
1866                 }
1867                 req->in.vector_count += 1;
1868
1869                 req->in.vector[idx].iov_base    = (void *)req->in.nbt_hdr;
1870                 req->in.vector[idx].iov_len     = 4;
1871
1872                 vector = talloc_array(mem_ctx, struct iovec, 1);
1873                 if (vector == NULL) {
1874                         return -1;
1875                 }
1876
1877                 vector[0] = req->in.vector[idx];
1878
1879                 *_vector = vector;
1880                 *_count = 1;
1881                 return 0;
1882         }
1883
1884         if (req->in.vector_count == 1) {
1885                 /*
1886                  * Now we analyze the NBT header
1887                  */
1888                 state->missing = smb2_len(req->in.vector[0].iov_base);
1889
1890                 if (state->missing == 0) {
1891                         /* if there're no remaining bytes, we're done */
1892                         *_vector = NULL;
1893                         *_count = 0;
1894                         return 0;
1895                 }
1896
1897                 req->in.vector = talloc_realloc(req, req->in.vector,
1898                                                 struct iovec,
1899                                                 req->in.vector_count + 1);
1900                 if (req->in.vector == NULL) {
1901                         return -1;
1902                 }
1903                 req->in.vector_count += 1;
1904
1905                 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
1906                         /*
1907                          * it's a special NBT message,
1908                          * so get all remaining bytes
1909                          */
1910                         len = state->missing;
1911                 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
1912                         /*
1913                          * it's an invalid message, just read what we can get
1914                          * and let the caller handle the error
1915                          */
1916                         len = state->missing;
1917                 } else {
1918                         /*
1919                          * We assume it's a SMB2 request,
1920                          * and we first get the header and the
1921                          * first 2 bytes (the struct size) of the body
1922                          */
1923                         len = SMB2_HDR_BODY + 2;
1924
1925                         state->asked_for_header = true;
1926                 }
1927
1928                 state->missing -= len;
1929
1930                 buf = talloc_array(req->in.vector, uint8_t, len);
1931                 if (buf == NULL) {
1932                         return -1;
1933                 }
1934
1935                 req->in.vector[idx].iov_base    = (void *)buf;
1936                 req->in.vector[idx].iov_len     = len;
1937
1938                 vector = talloc_array(mem_ctx, struct iovec, 1);
1939                 if (vector == NULL) {
1940                         return -1;
1941                 }
1942
1943                 vector[0] = req->in.vector[idx];
1944
1945                 *_vector = vector;
1946                 *_count = 1;
1947                 return 0;
1948         }
1949
1950         if (state->missing == 0) {
1951                 /* if there're no remaining bytes, we're done */
1952                 *_vector = NULL;
1953                 *_count = 0;
1954                 return 0;
1955         }
1956
1957         if (state->asked_for_header) {
1958                 const uint8_t *hdr;
1959                 size_t full_size;
1960                 size_t next_command_ofs;
1961                 size_t body_size;
1962                 uint8_t *body;
1963                 size_t dyn_size;
1964                 uint8_t *dyn;
1965                 bool invalid = false;
1966
1967                 state->asked_for_header = false;
1968
1969                 /*
1970                  * We got the SMB2 header and the first 2 bytes
1971                  * of the body. We fix the size to just the header
1972                  * and manually copy the 2 first bytes to the body section
1973                  */
1974                 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
1975                 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
1976
1977                 /* allocate vectors for body and dynamic areas */
1978                 req->in.vector = talloc_realloc(req, req->in.vector,
1979                                                 struct iovec,
1980                                                 req->in.vector_count + 2);
1981                 if (req->in.vector == NULL) {
1982                         return -1;
1983                 }
1984                 req->in.vector_count += 2;
1985
1986                 full_size = state->missing + SMB2_HDR_BODY + 2;
1987                 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
1988                 body_size = SVAL(hdr, SMB2_HDR_BODY);
1989
1990                 if (next_command_ofs != 0) {
1991                         if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
1992                                 /*
1993                                  * this is invalid, just return a zero
1994                                  * body and let the caller deal with the error
1995                                  */
1996                                 invalid = true;
1997                         } else if (next_command_ofs > full_size) {
1998                                 /*
1999                                  * this is invalid, just return a zero
2000                                  * body and let the caller deal with the error
2001                                  */
2002                                 invalid = true;
2003                         } else {
2004                                 full_size = next_command_ofs;
2005                         }
2006                 }
2007
2008                 if (!invalid) {
2009                         if (body_size < 2) {
2010                                 /*
2011                                  * this is invalid, just return a zero
2012                                  * body and let the caller deal with the error
2013                                  */
2014                                 invalid = true;
2015                         }
2016
2017                         if ((body_size % 2) != 0) {
2018                                 body_size -= 1;
2019                         }
2020
2021                         if (body_size > (full_size - SMB2_HDR_BODY)) {
2022                                 /*
2023                                  * this is invalid, just return a zero
2024                                  * body and let the caller deal with the error
2025                                  */
2026                                 invalid = true;
2027                         }
2028                 }
2029
2030                 if (invalid) {
2031                         /* the caller should check this */
2032                         body_size = 2;
2033                 }
2034
2035                 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
2036
2037                 state->missing -= (body_size - 2) + dyn_size;
2038
2039                 body = talloc_array(req->in.vector, uint8_t, body_size);
2040                 if (body == NULL) {
2041                         return -1;
2042                 }
2043
2044                 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
2045                 if (dyn == NULL) {
2046                         return -1;
2047                 }
2048
2049                 req->in.vector[idx].iov_base    = (void *)body;
2050                 req->in.vector[idx].iov_len     = body_size;
2051                 req->in.vector[idx+1].iov_base  = (void *)dyn;
2052                 req->in.vector[idx+1].iov_len   = dyn_size;
2053
2054                 vector = talloc_array(mem_ctx, struct iovec, 2);
2055                 if (vector == NULL) {
2056                         return -1;
2057                 }
2058
2059                 /*
2060                  * the first 2 bytes of the body were already fetched
2061                  * together with the header
2062                  */
2063                 memcpy(body, hdr + SMB2_HDR_BODY, 2);
2064                 vector[0].iov_base = body + 2;
2065                 vector[0].iov_len = body_size - 2;
2066
2067                 vector[1] = req->in.vector[idx+1];
2068
2069                 *_vector = vector;
2070                 *_count = 2;
2071                 return 0;
2072         }
2073
2074         /*
2075          * when we endup here, we're looking for a new SMB2 request
2076          * next. And we ask for its header and the first 2 bytes of
2077          * the body (like we did for the first SMB2 request).
2078          */
2079
2080         req->in.vector = talloc_realloc(req, req->in.vector,
2081                                         struct iovec,
2082                                         req->in.vector_count + 1);
2083         if (req->in.vector == NULL) {
2084                 return -1;
2085         }
2086         req->in.vector_count += 1;
2087
2088         /*
2089          * We assume it's a SMB2 request,
2090          * and we first get the header and the
2091          * first 2 bytes (the struct size) of the body
2092          */
2093         len = SMB2_HDR_BODY + 2;
2094
2095         if (len > state->missing) {
2096                 /* let the caller handle the error */
2097                 len = state->missing;
2098         }
2099
2100         state->missing -= len;
2101         state->asked_for_header = true;
2102
2103         buf = talloc_array(req->in.vector, uint8_t, len);
2104         if (buf == NULL) {
2105                 return -1;
2106         }
2107
2108         req->in.vector[idx].iov_base    = (void *)buf;
2109         req->in.vector[idx].iov_len     = len;
2110
2111         vector = talloc_array(mem_ctx, struct iovec, 1);
2112         if (vector == NULL) {
2113                 return -1;
2114         }
2115
2116         vector[0] = req->in.vector[idx];
2117
2118         *_vector = vector;
2119         *_count = 1;
2120         return 0;
2121 }
2122
2123 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
2124 {
2125         struct tevent_req *req =
2126                 tevent_req_callback_data(subreq,
2127                 struct tevent_req);
2128         int ret;
2129         int sys_errno;
2130         NTSTATUS status;
2131
2132         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
2133         if (ret == -1) {
2134                 status = map_nt_error_from_unix(sys_errno);
2135                 tevent_req_nterror(req, status);
2136                 return;
2137         }
2138
2139         tevent_req_done(req);
2140 }
2141
2142 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
2143                                             TALLOC_CTX *mem_ctx,
2144                                             struct smbd_smb2_request **_smb2_req)
2145 {
2146         struct smbd_smb2_request_read_state *state =
2147                 tevent_req_data(req,
2148                 struct smbd_smb2_request_read_state);
2149         NTSTATUS status;
2150
2151         if (tevent_req_is_nterror(req, &status)) {
2152                 tevent_req_received(req);
2153                 return status;
2154         }
2155
2156         talloc_steal(mem_ctx, state->smb2_req->mem_pool);
2157         *_smb2_req = state->smb2_req;
2158         tevent_req_received(req);
2159         return NT_STATUS_OK;
2160 }
2161
2162 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
2163
2164 void smbd_smb2_first_negprot(struct smbd_server_connection *sconn,
2165                              const uint8_t *inbuf, size_t size)
2166 {
2167         NTSTATUS status;
2168         struct smbd_smb2_request *req = NULL;
2169         struct tevent_req *subreq;
2170
2171         if (lp_security() == SEC_SHARE) {
2172                 DEBUG(2,("WARNING!!: \"security = share\" is deprecated for "
2173                         "SMB2 servers. Mapping to \"security = user\" and "
2174                         "\"map to guest = Bad User\"\n" ));
2175                 lp_do_parameter(-1, "security", "user");
2176                 lp_do_parameter(-1, "map to guest", "Bad User");
2177         }
2178
2179         DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
2180                  (unsigned int)size));
2181
2182         status = smbd_initialize_smb2(sconn);
2183         if (!NT_STATUS_IS_OK(status)) {
2184                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2185                 return;
2186         }
2187
2188         status = smbd_smb2_request_create(sconn, inbuf, size, &req);
2189         if (!NT_STATUS_IS_OK(status)) {
2190                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2191                 return;
2192         }
2193
2194         status = smbd_smb2_request_setup_out(req);
2195         if (!NT_STATUS_IS_OK(status)) {
2196                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2197                 return;
2198         }
2199
2200         status = smbd_smb2_request_dispatch(req);
2201         if (!NT_STATUS_IS_OK(status)) {
2202                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2203                 return;
2204         }
2205
2206         /* ask for the next request */
2207         subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
2208         if (subreq == NULL) {
2209                 smbd_server_connection_terminate(sconn, "no memory for reading");
2210                 return;
2211         }
2212         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);
2213 }
2214
2215 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
2216 {
2217         struct smbd_server_connection *sconn = tevent_req_callback_data(subreq,
2218                                                struct smbd_server_connection);
2219         NTSTATUS status;
2220         struct smbd_smb2_request *req = NULL;
2221
2222         status = smbd_smb2_request_read_recv(subreq, sconn, &req);
2223         TALLOC_FREE(subreq);
2224         if (!NT_STATUS_IS_OK(status)) {
2225                 DEBUG(2,("smbd_smb2_request_incoming: client read error %s\n",
2226                         nt_errstr(status)));
2227                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2228                 return;
2229         }
2230
2231         if (req->in.nbt_hdr[0] != 0x00) {
2232                 DEBUG(1,("smbd_smb2_request_incoming: ignore NBT[0x%02X] msg\n",
2233                          req->in.nbt_hdr[0]));
2234                 TALLOC_FREE(req);
2235                 goto next;
2236         }
2237
2238         req->current_idx = 1;
2239
2240         DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
2241                  req->current_idx, req->in.vector_count));
2242
2243         status = smbd_smb2_request_validate(req);
2244         if (!NT_STATUS_IS_OK(status)) {
2245                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2246                 return;
2247         }
2248
2249         status = smbd_smb2_request_setup_out(req);
2250         if (!NT_STATUS_IS_OK(status)) {
2251                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2252                 return;
2253         }
2254
2255         status = smbd_smb2_request_dispatch(req);
2256         if (!NT_STATUS_IS_OK(status)) {
2257                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2258                 return;
2259         }
2260
2261 next:
2262         /* ask for the next request (this constructs the main loop) */
2263         subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
2264         if (subreq == NULL) {
2265                 smbd_server_connection_terminate(sconn, "no memory for reading");
2266                 return;
2267         }
2268         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);
2269 }