We need to start off with smb2.credits_granted == 0. That way
[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. */
906         smb2_set_operation_credit(req->sconn,
907                         NULL,
908                         &state->vector[1]);
909
910         if (req->do_signing) {
911                 status = smb2_signing_sign_pdu(req->session->session_key,
912                                         state->vector, 3);
913                 if (!NT_STATUS_IS_OK(status)) {
914                         return status;
915                 }
916         }
917
918         subreq = tstream_writev_queue_send(state,
919                                         req->sconn->smb2.event_ctx,
920                                         req->sconn->smb2.stream,
921                                         req->sconn->smb2.send_queue,
922                                         state->vector,
923                                         3);
924
925         if (subreq == NULL) {
926                 return NT_STATUS_NO_MEMORY;
927         }
928
929         tevent_req_set_callback(subreq,
930                         smbd_smb2_request_pending_writev_done,
931                         state);
932
933         /* Note we're going async with this request. */
934         req->async = true;
935
936         /*
937          * Now manipulate req so that the outstanding async request
938          * is the only one left in the struct smbd_smb2_request.
939          */
940
941         if (req->current_idx == 1) {
942                 /* There was only one. */
943                 goto out;
944         }
945
946         /* Re-arrange the in.vectors. */
947         req->in.vector[1] = req->in.vector[i];
948         req->in.vector[2] = req->in.vector[i+1];
949         req->in.vector[3] = req->in.vector[i+2];
950         req->in.vector_count = 4;
951         /* Reset the new in size. */
952         smb2_setup_nbt_length(req->in.vector, 4);
953
954         /* Now recreate the out.vectors. */
955         outvec = talloc_zero_array(req, struct iovec, 4);
956         if (!outvec) {
957                 return NT_STATUS_NO_MEMORY;
958         }
959
960         /* 0 is always boilerplate and must
961          * be of size 4 for the length field. */
962
963         outvec[0].iov_base = req->out.nbt_hdr;
964         outvec[0].iov_len = 4;
965         SIVAL(req->out.nbt_hdr, 0, 0);
966
967         if (!dup_smb2_vec3(outvec, &outvec[1], &req->out.vector[i])) {
968                 return NT_STATUS_NO_MEMORY;
969         }
970
971         TALLOC_FREE(req->out.vector);
972
973         req->out.vector = outvec;
974
975         req->current_idx = 1;
976         req->out.vector_count = 4;
977
978   out:
979
980         smb2_setup_nbt_length(req->out.vector,
981                 req->out.vector_count);
982
983         /* Ensure our final reply matches the interim one. */
984         reqhdr = (uint8_t *)req->out.vector[1].iov_base;
985         SIVAL(reqhdr, SMB2_HDR_FLAGS, flags | SMB2_HDR_FLAG_ASYNC);
986         SBVAL(reqhdr, SMB2_HDR_PID, async_id);
987
988         {
989                 const uint8_t *inhdr =
990                         (const uint8_t *)req->in.vector[1].iov_base;
991                 DEBUG(10,("smbd_smb2_request_pending_queue: opcode[%s] mid %llu "
992                         "going async\n",
993                         smb2_opcode_name((uint16_t)IVAL(inhdr, SMB2_HDR_OPCODE)),
994                         (unsigned long long)async_id ));
995         }
996         return NT_STATUS_OK;
997 }
998
999 static NTSTATUS smbd_smb2_request_process_cancel(struct smbd_smb2_request *req)
1000 {
1001         struct smbd_server_connection *sconn = req->sconn;
1002         struct smbd_smb2_request *cur;
1003         const uint8_t *inhdr;
1004         int i = req->current_idx;
1005         uint32_t flags;
1006         uint64_t search_message_id;
1007         uint64_t search_async_id;
1008         uint64_t found_id;
1009
1010         inhdr = (const uint8_t *)req->in.vector[i].iov_base;
1011
1012         flags = IVAL(inhdr, SMB2_HDR_FLAGS);
1013         search_message_id = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
1014         search_async_id = BVAL(inhdr, SMB2_HDR_PID);
1015
1016         /*
1017          * we don't need the request anymore
1018          * cancel requests never have a response
1019          */
1020         DLIST_REMOVE(req->sconn->smb2.requests, req);
1021         TALLOC_FREE(req);
1022
1023         for (cur = sconn->smb2.requests; cur; cur = cur->next) {
1024                 const uint8_t *outhdr;
1025                 uint64_t message_id;
1026                 uint64_t async_id;
1027
1028                 i = cur->current_idx;
1029
1030                 outhdr = (const uint8_t *)cur->out.vector[i].iov_base;
1031
1032                 message_id = BVAL(outhdr, SMB2_HDR_MESSAGE_ID);
1033                 async_id = BVAL(outhdr, SMB2_HDR_PID);
1034
1035                 if (flags & SMB2_HDR_FLAG_ASYNC) {
1036                         if (search_async_id == async_id) {
1037                                 found_id = async_id;
1038                                 break;
1039                         }
1040                 } else {
1041                         if (search_message_id == message_id) {
1042                                 found_id = message_id;
1043                                 break;
1044                         }
1045                 }
1046         }
1047
1048         if (cur && cur->subreq) {
1049                 inhdr = (const uint8_t *)cur->in.vector[i].iov_base;
1050                 DEBUG(10,("smbd_smb2_request_process_cancel: attempting to "
1051                         "cancel opcode[%s] mid %llu\n",
1052                         smb2_opcode_name((uint16_t)IVAL(inhdr, SMB2_HDR_OPCODE)),
1053                         (unsigned long long)found_id ));
1054                 tevent_req_cancel(cur->subreq);
1055         }
1056
1057         return NT_STATUS_OK;
1058 }
1059
1060 NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
1061 {
1062         const uint8_t *inhdr;
1063         int i = req->current_idx;
1064         uint16_t opcode;
1065         uint32_t flags;
1066         uint64_t mid;
1067         NTSTATUS status;
1068         NTSTATUS session_status;
1069         uint32_t allowed_flags;
1070         NTSTATUS return_value;
1071
1072         inhdr = (const uint8_t *)req->in.vector[i].iov_base;
1073
1074         /* TODO: verify more things */
1075
1076         flags = IVAL(inhdr, SMB2_HDR_FLAGS);
1077         opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
1078         mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
1079         DEBUG(10,("smbd_smb2_request_dispatch: opcode[%s] mid = %llu\n",
1080                 smb2_opcode_name(opcode),
1081                 (unsigned long long)mid));
1082
1083         allowed_flags = SMB2_HDR_FLAG_CHAINED |
1084                         SMB2_HDR_FLAG_SIGNED |
1085                         SMB2_HDR_FLAG_DFS;
1086         if (opcode == SMB2_OP_CANCEL) {
1087                 allowed_flags |= SMB2_HDR_FLAG_ASYNC;
1088         }
1089         if ((flags & ~allowed_flags) != 0) {
1090                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
1091         }
1092
1093         session_status = smbd_smb2_request_check_session(req);
1094
1095         req->do_signing = false;
1096         if (flags & SMB2_HDR_FLAG_SIGNED) {
1097                 if (!NT_STATUS_IS_OK(session_status)) {
1098                         return smbd_smb2_request_error(req, session_status);
1099                 }
1100
1101                 req->do_signing = true;
1102                 status = smb2_signing_check_pdu(req->session->session_key,
1103                                                 &req->in.vector[i], 3);
1104                 if (!NT_STATUS_IS_OK(status)) {
1105                         return smbd_smb2_request_error(req, status);
1106                 }
1107         } else if (req->session && req->session->do_signing) {
1108                 return smbd_smb2_request_error(req, NT_STATUS_ACCESS_DENIED);
1109         }
1110
1111         if (flags & SMB2_HDR_FLAG_CHAINED) {
1112                 /*
1113                  * This check is mostly for giving the correct error code
1114                  * for compounded requests.
1115                  *
1116                  * TODO: we may need to move this after the session
1117                  *       and tcon checks.
1118                  */
1119                 if (!NT_STATUS_IS_OK(req->next_status)) {
1120                         return smbd_smb2_request_error(req, req->next_status);
1121                 }
1122         } else {
1123                 req->compat_chain_fsp = NULL;
1124         }
1125
1126         switch (opcode) {
1127         case SMB2_OP_NEGPROT:
1128                 {
1129                         START_PROFILE(smb2_negprot);
1130                         return_value = smbd_smb2_request_process_negprot(req);
1131                         END_PROFILE(smb2_negprot);
1132                 }
1133                 break;
1134
1135         case SMB2_OP_SESSSETUP:
1136                 {
1137                         START_PROFILE(smb2_sesssetup);
1138                         return_value = smbd_smb2_request_process_sesssetup(req);
1139                         END_PROFILE(smb2_sesssetup);
1140                 }
1141                 break;
1142
1143         case SMB2_OP_LOGOFF:
1144                 if (!NT_STATUS_IS_OK(session_status)) {
1145                         return_value = smbd_smb2_request_error(req, session_status);
1146                         break;
1147                 }
1148
1149                 {
1150                         START_PROFILE(smb2_logoff);
1151                         return_value = smbd_smb2_request_process_logoff(req);
1152                         END_PROFILE(smb2_logoff);
1153                 }
1154                 break;
1155
1156         case SMB2_OP_TCON:
1157                 if (!NT_STATUS_IS_OK(session_status)) {
1158                         return_value = smbd_smb2_request_error(req, session_status);
1159                         break;
1160                 }
1161                 status = smbd_smb2_request_check_session(req);
1162                 if (!NT_STATUS_IS_OK(status)) {
1163                         return_value = smbd_smb2_request_error(req, status);
1164                         break;
1165                 }
1166
1167                 {
1168                         START_PROFILE(smb2_tcon);
1169                         return_value = smbd_smb2_request_process_tcon(req);
1170                         END_PROFILE(smb2_tcon);
1171                 }
1172                 break;
1173
1174         case SMB2_OP_TDIS:
1175                 if (!NT_STATUS_IS_OK(session_status)) {
1176                         return_value = smbd_smb2_request_error(req, session_status);
1177                         break;
1178                 }
1179                 status = smbd_smb2_request_check_tcon(req);
1180                 if (!NT_STATUS_IS_OK(status)) {
1181                         return_value = smbd_smb2_request_error(req, status);
1182                         break;
1183                 }
1184
1185                 {
1186                         START_PROFILE(smb2_tdis);
1187                         return_value = smbd_smb2_request_process_tdis(req);
1188                         END_PROFILE(smb2_tdis);
1189                 }
1190                 break;
1191
1192         case SMB2_OP_CREATE:
1193                 if (!NT_STATUS_IS_OK(session_status)) {
1194                         return_value = smbd_smb2_request_error(req, session_status);
1195                         break;
1196                 }
1197                 status = smbd_smb2_request_check_tcon(req);
1198                 if (!NT_STATUS_IS_OK(status)) {
1199                         return_value = smbd_smb2_request_error(req, status);
1200                         break;
1201                 }
1202
1203                 {
1204                         START_PROFILE(smb2_create);
1205                         return_value = smbd_smb2_request_process_create(req);
1206                         END_PROFILE(smb2_create);
1207                 }
1208                 break;
1209
1210         case SMB2_OP_CLOSE:
1211                 if (!NT_STATUS_IS_OK(session_status)) {
1212                         return_value = smbd_smb2_request_error(req, session_status);
1213                         break;
1214                 }
1215                 status = smbd_smb2_request_check_tcon(req);
1216                 if (!NT_STATUS_IS_OK(status)) {
1217                         return_value = smbd_smb2_request_error(req, status);
1218                         break;
1219                 }
1220
1221                 {
1222                         START_PROFILE(smb2_close);
1223                         return_value = smbd_smb2_request_process_close(req);
1224                         END_PROFILE(smb2_close);
1225                 }
1226                 break;
1227
1228         case SMB2_OP_FLUSH:
1229                 if (!NT_STATUS_IS_OK(session_status)) {
1230                         return_value = smbd_smb2_request_error(req, session_status);
1231                         break;
1232                 }
1233                 status = smbd_smb2_request_check_tcon(req);
1234                 if (!NT_STATUS_IS_OK(status)) {
1235                         return_value = smbd_smb2_request_error(req, status);
1236                         break;
1237                 }
1238
1239                 {
1240                         START_PROFILE(smb2_flush);
1241                         return_value = smbd_smb2_request_process_flush(req);
1242                         END_PROFILE(smb2_flush);
1243                 }
1244                 break;
1245
1246         case SMB2_OP_READ:
1247                 if (!NT_STATUS_IS_OK(session_status)) {
1248                         return_value = smbd_smb2_request_error(req, session_status);
1249                         break;
1250                 }
1251                 status = smbd_smb2_request_check_tcon(req);
1252                 if (!NT_STATUS_IS_OK(status)) {
1253                         return_value = smbd_smb2_request_error(req, status);
1254                         break;
1255                 }
1256
1257                 {
1258                         START_PROFILE(smb2_read);
1259                         return_value = smbd_smb2_request_process_read(req);
1260                         END_PROFILE(smb2_read);
1261                 }
1262                 break;
1263
1264         case SMB2_OP_WRITE:
1265                 if (!NT_STATUS_IS_OK(session_status)) {
1266                         return_value = smbd_smb2_request_error(req, session_status);
1267                         break;
1268                 }
1269                 status = smbd_smb2_request_check_tcon(req);
1270                 if (!NT_STATUS_IS_OK(status)) {
1271                         return_value = smbd_smb2_request_error(req, status);
1272                         break;
1273                 }
1274
1275                 {
1276                         START_PROFILE(smb2_write);
1277                         return_value = smbd_smb2_request_process_write(req);
1278                         END_PROFILE(smb2_write);
1279                 }
1280                 break;
1281
1282         case SMB2_OP_LOCK:
1283                 if (!NT_STATUS_IS_OK(session_status)) {
1284                         /* Too ugly to live ? JRA. */
1285                         if (NT_STATUS_EQUAL(session_status,NT_STATUS_USER_SESSION_DELETED)) {
1286                                 session_status = NT_STATUS_FILE_CLOSED;
1287                         }
1288                         return_value = smbd_smb2_request_error(req, session_status);
1289                         break;
1290                 }
1291                 status = smbd_smb2_request_check_tcon(req);
1292                 if (!NT_STATUS_IS_OK(status)) {
1293                         /* Too ugly to live ? JRA. */
1294                         if (NT_STATUS_EQUAL(status,NT_STATUS_NETWORK_NAME_DELETED)) {
1295                                 status = NT_STATUS_FILE_CLOSED;
1296                         }
1297                         return_value = smbd_smb2_request_error(req, status);
1298                         break;
1299                 }
1300
1301                 {
1302                         START_PROFILE(smb2_lock);
1303                         return_value = smbd_smb2_request_process_lock(req);
1304                         END_PROFILE(smb2_lock);
1305                 }
1306                 break;
1307
1308         case SMB2_OP_IOCTL:
1309                 if (!NT_STATUS_IS_OK(session_status)) {
1310                         return_value = smbd_smb2_request_error(req, session_status);
1311                         break;
1312                 }
1313                 status = smbd_smb2_request_check_tcon(req);
1314                 if (!NT_STATUS_IS_OK(status)) {
1315                         return_value = smbd_smb2_request_error(req, status);
1316                         break;
1317                 }
1318
1319                 {
1320                         START_PROFILE(smb2_ioctl);
1321                         return_value = smbd_smb2_request_process_ioctl(req);
1322                         END_PROFILE(smb2_ioctl);
1323                 }
1324                 break;
1325
1326         case SMB2_OP_CANCEL:
1327                 {
1328                         START_PROFILE(smb2_cancel);
1329                         return_value = smbd_smb2_request_process_cancel(req);
1330                         END_PROFILE(smb2_cancel);
1331                 }
1332                 break;
1333
1334         case SMB2_OP_KEEPALIVE:
1335                 {START_PROFILE(smb2_keepalive);
1336                 return_value = smbd_smb2_request_process_keepalive(req);
1337                 END_PROFILE(smb2_keepalive);}
1338                 break;
1339
1340         case SMB2_OP_FIND:
1341                 if (!NT_STATUS_IS_OK(session_status)) {
1342                         return_value = smbd_smb2_request_error(req, session_status);
1343                         break;
1344                 }
1345                 status = smbd_smb2_request_check_tcon(req);
1346                 if (!NT_STATUS_IS_OK(status)) {
1347                         return_value = smbd_smb2_request_error(req, status);
1348                         break;
1349                 }
1350
1351                 {
1352                         START_PROFILE(smb2_find);
1353                         return_value = smbd_smb2_request_process_find(req);
1354                         END_PROFILE(smb2_find);
1355                 }
1356                 break;
1357
1358         case SMB2_OP_NOTIFY:
1359                 if (!NT_STATUS_IS_OK(session_status)) {
1360                         return_value = smbd_smb2_request_error(req, session_status);
1361                         break;
1362                 }
1363                 status = smbd_smb2_request_check_tcon(req);
1364                 if (!NT_STATUS_IS_OK(status)) {
1365                         return_value = smbd_smb2_request_error(req, status);
1366                         break;
1367                 }
1368
1369                 {
1370                         START_PROFILE(smb2_notify);
1371                         return_value = smbd_smb2_request_process_notify(req);
1372                         END_PROFILE(smb2_notify);
1373                 }
1374                 break;
1375
1376         case SMB2_OP_GETINFO:
1377                 if (!NT_STATUS_IS_OK(session_status)) {
1378                         return_value = smbd_smb2_request_error(req, session_status);
1379                         break;
1380                 }
1381                 status = smbd_smb2_request_check_tcon(req);
1382                 if (!NT_STATUS_IS_OK(status)) {
1383                         return_value = smbd_smb2_request_error(req, status);
1384                         break;
1385                 }
1386
1387                 {
1388                         START_PROFILE(smb2_getinfo);
1389                         return_value = smbd_smb2_request_process_getinfo(req);
1390                         END_PROFILE(smb2_getinfo);
1391                 }
1392                 break;
1393
1394         case SMB2_OP_SETINFO:
1395                 if (!NT_STATUS_IS_OK(session_status)) {
1396                         return_value = smbd_smb2_request_error(req, session_status);
1397                         break;
1398                 }
1399                 status = smbd_smb2_request_check_tcon(req);
1400                 if (!NT_STATUS_IS_OK(status)) {
1401                         return_value = smbd_smb2_request_error(req, status);
1402                         break;
1403                 }
1404
1405                 {
1406                         START_PROFILE(smb2_setinfo);
1407                         return_value = smbd_smb2_request_process_setinfo(req);
1408                         END_PROFILE(smb2_setinfo);
1409                 }
1410                 break;
1411
1412         case SMB2_OP_BREAK:
1413                 if (!NT_STATUS_IS_OK(session_status)) {
1414                         return_value = smbd_smb2_request_error(req, session_status);
1415                         break;
1416                 }
1417                 status = smbd_smb2_request_check_tcon(req);
1418                 if (!NT_STATUS_IS_OK(status)) {
1419                         return_value = smbd_smb2_request_error(req, status);
1420                         break;
1421                 }
1422
1423                 {
1424                         START_PROFILE(smb2_break);
1425                         return_value = smbd_smb2_request_process_break(req);
1426                         END_PROFILE(smb2_break);
1427                 }
1428                 break;
1429
1430         default:
1431                 return_value = smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
1432                 break;
1433         }
1434         return return_value;
1435 }
1436
1437 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
1438 {
1439         struct tevent_req *subreq;
1440         int i = req->current_idx;
1441
1442         req->subreq = NULL;
1443
1444         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
1445
1446         /* Set credit for this operation. */
1447         smb2_set_operation_credit(req->sconn,
1448                         &req->in.vector[i],
1449                         &req->out.vector[i]);
1450
1451         if (req->do_signing) {
1452                 NTSTATUS status;
1453                 status = smb2_signing_sign_pdu(req->session->session_key,
1454                                                &req->out.vector[i], 3);
1455                 if (!NT_STATUS_IS_OK(status)) {
1456                         return status;
1457                 }
1458         }
1459
1460         req->current_idx += 3;
1461
1462         if (req->current_idx < req->out.vector_count) {
1463                 /*
1464                  * We must process the remaining compound
1465                  * SMB2 requests before any new incoming SMB2
1466                  * requests. This is because incoming SMB2
1467                  * requests may include a cancel for a
1468                  * compound request we haven't processed
1469                  * yet.
1470                  */
1471                 struct tevent_immediate *im = tevent_create_immediate(req);
1472                 if (!im) {
1473                         return NT_STATUS_NO_MEMORY;
1474                 }
1475                 tevent_schedule_immediate(im,
1476                                         req->sconn->smb2.event_ctx,
1477                                         smbd_smb2_request_dispatch_immediate,
1478                                         req);
1479                 return NT_STATUS_OK;
1480         }
1481
1482         if (DEBUGLEVEL >= 10) {
1483                 dbgtext("smbd_smb2_request_reply: sending...\n");
1484                 print_req_vectors(req);
1485         }
1486
1487         subreq = tstream_writev_queue_send(req,
1488                                            req->sconn->smb2.event_ctx,
1489                                            req->sconn->smb2.stream,
1490                                            req->sconn->smb2.send_queue,
1491                                            req->out.vector,
1492                                            req->out.vector_count);
1493         if (subreq == NULL) {
1494                 return NT_STATUS_NO_MEMORY;
1495         }
1496         tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
1497         /*
1498          * We're done with this request -
1499          * move it off the "being processed" queue.
1500          */
1501         DLIST_REMOVE(req->sconn->smb2.requests, req);
1502
1503         return NT_STATUS_OK;
1504 }
1505
1506 void smbd_smb2_request_dispatch_immediate(struct tevent_context *ctx,
1507                                         struct tevent_immediate *im,
1508                                         void *private_data)
1509 {
1510         struct smbd_smb2_request *req = talloc_get_type_abort(private_data,
1511                                         struct smbd_smb2_request);
1512         struct smbd_server_connection *sconn = req->sconn;
1513         NTSTATUS status;
1514
1515         TALLOC_FREE(im);
1516
1517         if (DEBUGLEVEL >= 10) {
1518                 DEBUG(10,("smbd_smb2_request_dispatch_immediate: idx[%d] of %d vectors\n",
1519                         req->current_idx, req->in.vector_count));
1520                 print_req_vectors(req);
1521         }
1522
1523         status = smbd_smb2_request_dispatch(req);
1524         if (!NT_STATUS_IS_OK(status)) {
1525                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1526                 return;
1527         }
1528 }
1529
1530 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
1531 {
1532         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
1533                                         struct smbd_smb2_request);
1534         struct smbd_server_connection *sconn = req->sconn;
1535         int ret;
1536         int sys_errno;
1537
1538         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1539         TALLOC_FREE(subreq);
1540         TALLOC_FREE(req);
1541         if (ret == -1) {
1542                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1543                 DEBUG(2,("smbd_smb2_request_writev_done: client write error %s\n",
1544                         nt_errstr(status)));
1545                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1546                 return;
1547         }
1548 }
1549
1550 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
1551                                    NTSTATUS status,
1552                                    DATA_BLOB body, DATA_BLOB *dyn,
1553                                    const char *location)
1554 {
1555         uint8_t *outhdr;
1556         int i = req->current_idx;
1557         uint32_t next_command_ofs;
1558
1559         DEBUG(10,("smbd_smb2_request_done_ex: "
1560                   "idx[%d] status[%s] body[%u] dyn[%s:%u] at %s\n",
1561                   i, nt_errstr(status), (unsigned int)body.length,
1562                   dyn ? "yes": "no",
1563                   (unsigned int)(dyn ? dyn->length : 0),
1564                   location));
1565
1566         if (body.length < 2) {
1567                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1568         }
1569
1570         if ((body.length % 2) != 0) {
1571                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
1572         }
1573
1574         outhdr = (uint8_t *)req->out.vector[i].iov_base;
1575
1576         next_command_ofs = IVAL(outhdr, SMB2_HDR_NEXT_COMMAND);
1577         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
1578
1579         req->out.vector[i+1].iov_base = (void *)body.data;
1580         req->out.vector[i+1].iov_len = body.length;
1581
1582         if (dyn) {
1583                 req->out.vector[i+2].iov_base   = (void *)dyn->data;
1584                 req->out.vector[i+2].iov_len    = dyn->length;
1585         } else {
1586                 req->out.vector[i+2].iov_base = NULL;
1587                 req->out.vector[i+2].iov_len = 0;
1588         }
1589
1590         /* see if we need to recalculate the offset to the next response */
1591         if (next_command_ofs > 0) {
1592                 next_command_ofs  = SMB2_HDR_BODY;
1593                 next_command_ofs += req->out.vector[i+1].iov_len;
1594                 next_command_ofs += req->out.vector[i+2].iov_len;
1595         }
1596
1597         if ((next_command_ofs % 8) != 0) {
1598                 size_t pad_size = 8 - (next_command_ofs % 8);
1599                 if (req->out.vector[i+2].iov_len == 0) {
1600                         /*
1601                          * if the dyn buffer is empty
1602                          * we can use it to add padding
1603                          */
1604                         uint8_t *pad;
1605
1606                         pad = talloc_zero_array(req->out.vector,
1607                                                 uint8_t, pad_size);
1608                         if (pad == NULL) {
1609                                 return smbd_smb2_request_error(req,
1610                                                 NT_STATUS_NO_MEMORY);
1611                         }
1612
1613                         req->out.vector[i+2].iov_base = (void *)pad;
1614                         req->out.vector[i+2].iov_len = pad_size;
1615                 } else {
1616                         /*
1617                          * For now we copy the dynamic buffer
1618                          * and add the padding to the new buffer
1619                          */
1620                         size_t old_size;
1621                         uint8_t *old_dyn;
1622                         size_t new_size;
1623                         uint8_t *new_dyn;
1624
1625                         old_size = req->out.vector[i+2].iov_len;
1626                         old_dyn = (uint8_t *)req->out.vector[i+2].iov_base;
1627
1628                         new_size = old_size + pad_size;
1629                         new_dyn = talloc_zero_array(req->out.vector,
1630                                                uint8_t, new_size);
1631                         if (new_dyn == NULL) {
1632                                 return smbd_smb2_request_error(req,
1633                                                 NT_STATUS_NO_MEMORY);
1634                         }
1635
1636                         memcpy(new_dyn, old_dyn, old_size);
1637                         memset(new_dyn + old_size, 0, pad_size);
1638
1639                         req->out.vector[i+2].iov_base = (void *)new_dyn;
1640                         req->out.vector[i+2].iov_len = new_size;
1641                 }
1642                 next_command_ofs += pad_size;
1643         }
1644
1645         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
1646
1647         return smbd_smb2_request_reply(req);
1648 }
1649
1650 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
1651                                     NTSTATUS status,
1652                                     DATA_BLOB *info,
1653                                     const char *location)
1654 {
1655         DATA_BLOB body;
1656         int i = req->current_idx;
1657         uint8_t *outhdr = (uint8_t *)req->out.vector[i].iov_base;
1658
1659         DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s] |%s| at %s\n",
1660                   i, nt_errstr(status), info ? " +info" : "",
1661                   location));
1662
1663         body.data = outhdr + SMB2_HDR_BODY;
1664         body.length = 8;
1665         SSVAL(body.data, 0, 9);
1666
1667         if (info) {
1668                 SIVAL(body.data, 0x04, info->length);
1669         } else {
1670                 /* Allocated size of req->out.vector[i].iov_base
1671                  * *MUST BE* OUTVEC_ALLOC_SIZE. So we have room for
1672                  * 1 byte without having to do an alloc.
1673                  */
1674                 info = talloc_zero_array(req->out.vector,
1675                                         DATA_BLOB,
1676                                         1);
1677                 if (!info) {
1678                         return NT_STATUS_NO_MEMORY;
1679                 }
1680                 info->data = ((uint8_t *)outhdr) +
1681                         OUTVEC_ALLOC_SIZE - 1;
1682                 info->length = 1;
1683                 SCVAL(info->data, 0, 0);
1684         }
1685
1686         /*
1687          * if a request fails, all other remaining
1688          * compounded requests should fail too
1689          */
1690         req->next_status = NT_STATUS_INVALID_PARAMETER;
1691
1692         return smbd_smb2_request_done_ex(req, status, body, info, __location__);
1693 }
1694
1695
1696 struct smbd_smb2_send_oplock_break_state {
1697         struct smbd_server_connection *sconn;
1698         uint8_t buf[4 + SMB2_HDR_BODY + 0x18];
1699         struct iovec vector;
1700 };
1701
1702 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq);
1703
1704 NTSTATUS smbd_smb2_send_oplock_break(struct smbd_server_connection *sconn,
1705                                      uint64_t file_id_persistent,
1706                                      uint64_t file_id_volatile,
1707                                      uint8_t oplock_level)
1708 {
1709         struct smbd_smb2_send_oplock_break_state *state;
1710         struct tevent_req *subreq;
1711         uint8_t *hdr;
1712         uint8_t *body;
1713
1714         state = talloc(sconn, struct smbd_smb2_send_oplock_break_state);
1715         if (state == NULL) {
1716                 return NT_STATUS_NO_MEMORY;
1717         }
1718         state->sconn = sconn;
1719
1720         state->vector.iov_base = (void *)state->buf;
1721         state->vector.iov_len = sizeof(state->buf);
1722
1723         _smb2_setlen(state->buf, sizeof(state->buf) - 4);
1724         hdr = state->buf + 4;
1725         body = hdr + SMB2_HDR_BODY;
1726
1727         SIVAL(hdr, 0,                           SMB2_MAGIC);
1728         SSVAL(hdr, SMB2_HDR_LENGTH,             SMB2_HDR_BODY);
1729         SSVAL(hdr, SMB2_HDR_EPOCH,              0);
1730         SIVAL(hdr, SMB2_HDR_STATUS,             0);
1731         SSVAL(hdr, SMB2_HDR_OPCODE,             SMB2_OP_BREAK);
1732         SSVAL(hdr, SMB2_HDR_CREDIT,             0);
1733         SIVAL(hdr, SMB2_HDR_FLAGS,              SMB2_HDR_FLAG_REDIRECT);
1734         SIVAL(hdr, SMB2_HDR_NEXT_COMMAND,       0);
1735         SBVAL(hdr, SMB2_HDR_MESSAGE_ID,         UINT64_MAX);
1736         SIVAL(hdr, SMB2_HDR_PID,                0);
1737         SIVAL(hdr, SMB2_HDR_TID,                0);
1738         SBVAL(hdr, SMB2_HDR_SESSION_ID,         0);
1739         memset(hdr+SMB2_HDR_SIGNATURE, 0, 16);
1740
1741         SSVAL(body, 0x00, 0x18);
1742
1743         SCVAL(body, 0x02, oplock_level);
1744         SCVAL(body, 0x03, 0);           /* reserved */
1745         SIVAL(body, 0x04, 0);           /* reserved */
1746         SBVAL(body, 0x08, file_id_persistent);
1747         SBVAL(body, 0x10, file_id_volatile);
1748
1749         subreq = tstream_writev_queue_send(state,
1750                                            sconn->smb2.event_ctx,
1751                                            sconn->smb2.stream,
1752                                            sconn->smb2.send_queue,
1753                                            &state->vector, 1);
1754         if (subreq == NULL) {
1755                 return NT_STATUS_NO_MEMORY;
1756         }
1757         tevent_req_set_callback(subreq,
1758                                 smbd_smb2_oplock_break_writev_done,
1759                                 state);
1760
1761         return NT_STATUS_OK;
1762 }
1763
1764 static void smbd_smb2_oplock_break_writev_done(struct tevent_req *subreq)
1765 {
1766         struct smbd_smb2_send_oplock_break_state *state =
1767                 tevent_req_callback_data(subreq,
1768                 struct smbd_smb2_send_oplock_break_state);
1769         struct smbd_server_connection *sconn = state->sconn;
1770         int ret;
1771         int sys_errno;
1772
1773         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1774         TALLOC_FREE(subreq);
1775         if (ret == -1) {
1776                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
1777                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1778                 return;
1779         }
1780
1781         TALLOC_FREE(state);
1782 }
1783
1784 struct smbd_smb2_request_read_state {
1785         size_t missing;
1786         bool asked_for_header;
1787         struct smbd_smb2_request *smb2_req;
1788 };
1789
1790 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1791                                          void *private_data,
1792                                          TALLOC_CTX *mem_ctx,
1793                                          struct iovec **_vector,
1794                                          size_t *_count);
1795 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
1796
1797 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
1798                                         struct tevent_context *ev,
1799                                         struct smbd_server_connection *sconn)
1800 {
1801         struct tevent_req *req;
1802         struct smbd_smb2_request_read_state *state;
1803         struct tevent_req *subreq;
1804
1805         req = tevent_req_create(mem_ctx, &state,
1806                                 struct smbd_smb2_request_read_state);
1807         if (req == NULL) {
1808                 return NULL;
1809         }
1810         state->missing = 0;
1811         state->asked_for_header = false;
1812
1813         state->smb2_req = smbd_smb2_request_allocate(state);
1814         if (tevent_req_nomem(state->smb2_req, req)) {
1815                 return tevent_req_post(req, ev);
1816         }
1817         state->smb2_req->sconn = sconn;
1818
1819         subreq = tstream_readv_pdu_queue_send(state, ev, sconn->smb2.stream,
1820                                               sconn->smb2.recv_queue,
1821                                               smbd_smb2_request_next_vector,
1822                                               state);
1823         if (tevent_req_nomem(subreq, req)) {
1824                 return tevent_req_post(req, ev);
1825         }
1826         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
1827
1828         return req;
1829 }
1830
1831 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
1832                                          void *private_data,
1833                                          TALLOC_CTX *mem_ctx,
1834                                          struct iovec **_vector,
1835                                          size_t *_count)
1836 {
1837         struct smbd_smb2_request_read_state *state =
1838                 talloc_get_type_abort(private_data,
1839                 struct smbd_smb2_request_read_state);
1840         struct smbd_smb2_request *req = state->smb2_req;
1841         struct iovec *vector;
1842         int idx = req->in.vector_count;
1843         size_t len = 0;
1844         uint8_t *buf = NULL;
1845
1846         if (req->in.vector_count == 0) {
1847                 /*
1848                  * first we need to get the NBT header
1849                  */
1850                 req->in.vector = talloc_array(req, struct iovec,
1851                                               req->in.vector_count + 1);
1852                 if (req->in.vector == NULL) {
1853                         return -1;
1854                 }
1855                 req->in.vector_count += 1;
1856
1857                 req->in.vector[idx].iov_base    = (void *)req->in.nbt_hdr;
1858                 req->in.vector[idx].iov_len     = 4;
1859
1860                 vector = talloc_array(mem_ctx, struct iovec, 1);
1861                 if (vector == NULL) {
1862                         return -1;
1863                 }
1864
1865                 vector[0] = req->in.vector[idx];
1866
1867                 *_vector = vector;
1868                 *_count = 1;
1869                 return 0;
1870         }
1871
1872         if (req->in.vector_count == 1) {
1873                 /*
1874                  * Now we analyze the NBT header
1875                  */
1876                 state->missing = smb2_len(req->in.vector[0].iov_base);
1877
1878                 if (state->missing == 0) {
1879                         /* if there're no remaining bytes, we're done */
1880                         *_vector = NULL;
1881                         *_count = 0;
1882                         return 0;
1883                 }
1884
1885                 req->in.vector = talloc_realloc(req, req->in.vector,
1886                                                 struct iovec,
1887                                                 req->in.vector_count + 1);
1888                 if (req->in.vector == NULL) {
1889                         return -1;
1890                 }
1891                 req->in.vector_count += 1;
1892
1893                 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
1894                         /*
1895                          * it's a special NBT message,
1896                          * so get all remaining bytes
1897                          */
1898                         len = state->missing;
1899                 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
1900                         /*
1901                          * it's an invalid message, just read what we can get
1902                          * and let the caller handle the error
1903                          */
1904                         len = state->missing;
1905                 } else {
1906                         /*
1907                          * We assume it's a SMB2 request,
1908                          * and we first get the header and the
1909                          * first 2 bytes (the struct size) of the body
1910                          */
1911                         len = SMB2_HDR_BODY + 2;
1912
1913                         state->asked_for_header = true;
1914                 }
1915
1916                 state->missing -= len;
1917
1918                 buf = talloc_array(req->in.vector, uint8_t, len);
1919                 if (buf == NULL) {
1920                         return -1;
1921                 }
1922
1923                 req->in.vector[idx].iov_base    = (void *)buf;
1924                 req->in.vector[idx].iov_len     = len;
1925
1926                 vector = talloc_array(mem_ctx, struct iovec, 1);
1927                 if (vector == NULL) {
1928                         return -1;
1929                 }
1930
1931                 vector[0] = req->in.vector[idx];
1932
1933                 *_vector = vector;
1934                 *_count = 1;
1935                 return 0;
1936         }
1937
1938         if (state->missing == 0) {
1939                 /* if there're no remaining bytes, we're done */
1940                 *_vector = NULL;
1941                 *_count = 0;
1942                 return 0;
1943         }
1944
1945         if (state->asked_for_header) {
1946                 const uint8_t *hdr;
1947                 size_t full_size;
1948                 size_t next_command_ofs;
1949                 size_t body_size;
1950                 uint8_t *body;
1951                 size_t dyn_size;
1952                 uint8_t *dyn;
1953                 bool invalid = false;
1954
1955                 state->asked_for_header = false;
1956
1957                 /*
1958                  * We got the SMB2 header and the first 2 bytes
1959                  * of the body. We fix the size to just the header
1960                  * and manually copy the 2 first bytes to the body section
1961                  */
1962                 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
1963                 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
1964
1965                 /* allocate vectors for body and dynamic areas */
1966                 req->in.vector = talloc_realloc(req, req->in.vector,
1967                                                 struct iovec,
1968                                                 req->in.vector_count + 2);
1969                 if (req->in.vector == NULL) {
1970                         return -1;
1971                 }
1972                 req->in.vector_count += 2;
1973
1974                 full_size = state->missing + SMB2_HDR_BODY + 2;
1975                 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
1976                 body_size = SVAL(hdr, SMB2_HDR_BODY);
1977
1978                 if (next_command_ofs != 0) {
1979                         if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
1980                                 /*
1981                                  * this is invalid, just return a zero
1982                                  * body and let the caller deal with the error
1983                                  */
1984                                 invalid = true;
1985                         } else if (next_command_ofs > full_size) {
1986                                 /*
1987                                  * this is invalid, just return a zero
1988                                  * body and let the caller deal with the error
1989                                  */
1990                                 invalid = true;
1991                         } else {
1992                                 full_size = next_command_ofs;
1993                         }
1994                 }
1995
1996                 if (!invalid) {
1997                         if (body_size < 2) {
1998                                 /*
1999                                  * this is invalid, just return a zero
2000                                  * body and let the caller deal with the error
2001                                  */
2002                                 invalid = true;
2003                         }
2004
2005                         if ((body_size % 2) != 0) {
2006                                 body_size -= 1;
2007                         }
2008
2009                         if (body_size > (full_size - SMB2_HDR_BODY)) {
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
2018                 if (invalid) {
2019                         /* the caller should check this */
2020                         body_size = 2;
2021                 }
2022
2023                 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
2024
2025                 state->missing -= (body_size - 2) + dyn_size;
2026
2027                 body = talloc_array(req->in.vector, uint8_t, body_size);
2028                 if (body == NULL) {
2029                         return -1;
2030                 }
2031
2032                 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
2033                 if (dyn == NULL) {
2034                         return -1;
2035                 }
2036
2037                 req->in.vector[idx].iov_base    = (void *)body;
2038                 req->in.vector[idx].iov_len     = body_size;
2039                 req->in.vector[idx+1].iov_base  = (void *)dyn;
2040                 req->in.vector[idx+1].iov_len   = dyn_size;
2041
2042                 vector = talloc_array(mem_ctx, struct iovec, 2);
2043                 if (vector == NULL) {
2044                         return -1;
2045                 }
2046
2047                 /*
2048                  * the first 2 bytes of the body were already fetched
2049                  * together with the header
2050                  */
2051                 memcpy(body, hdr + SMB2_HDR_BODY, 2);
2052                 vector[0].iov_base = body + 2;
2053                 vector[0].iov_len = body_size - 2;
2054
2055                 vector[1] = req->in.vector[idx+1];
2056
2057                 *_vector = vector;
2058                 *_count = 2;
2059                 return 0;
2060         }
2061
2062         /*
2063          * when we endup here, we're looking for a new SMB2 request
2064          * next. And we ask for its header and the first 2 bytes of
2065          * the body (like we did for the first SMB2 request).
2066          */
2067
2068         req->in.vector = talloc_realloc(req, req->in.vector,
2069                                         struct iovec,
2070                                         req->in.vector_count + 1);
2071         if (req->in.vector == NULL) {
2072                 return -1;
2073         }
2074         req->in.vector_count += 1;
2075
2076         /*
2077          * We assume it's a SMB2 request,
2078          * and we first get the header and the
2079          * first 2 bytes (the struct size) of the body
2080          */
2081         len = SMB2_HDR_BODY + 2;
2082
2083         if (len > state->missing) {
2084                 /* let the caller handle the error */
2085                 len = state->missing;
2086         }
2087
2088         state->missing -= len;
2089         state->asked_for_header = true;
2090
2091         buf = talloc_array(req->in.vector, uint8_t, len);
2092         if (buf == NULL) {
2093                 return -1;
2094         }
2095
2096         req->in.vector[idx].iov_base    = (void *)buf;
2097         req->in.vector[idx].iov_len     = len;
2098
2099         vector = talloc_array(mem_ctx, struct iovec, 1);
2100         if (vector == NULL) {
2101                 return -1;
2102         }
2103
2104         vector[0] = req->in.vector[idx];
2105
2106         *_vector = vector;
2107         *_count = 1;
2108         return 0;
2109 }
2110
2111 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
2112 {
2113         struct tevent_req *req =
2114                 tevent_req_callback_data(subreq,
2115                 struct tevent_req);
2116         int ret;
2117         int sys_errno;
2118         NTSTATUS status;
2119
2120         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
2121         if (ret == -1) {
2122                 status = map_nt_error_from_unix(sys_errno);
2123                 tevent_req_nterror(req, status);
2124                 return;
2125         }
2126
2127         tevent_req_done(req);
2128 }
2129
2130 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
2131                                             TALLOC_CTX *mem_ctx,
2132                                             struct smbd_smb2_request **_smb2_req)
2133 {
2134         struct smbd_smb2_request_read_state *state =
2135                 tevent_req_data(req,
2136                 struct smbd_smb2_request_read_state);
2137         NTSTATUS status;
2138
2139         if (tevent_req_is_nterror(req, &status)) {
2140                 tevent_req_received(req);
2141                 return status;
2142         }
2143
2144         talloc_steal(mem_ctx, state->smb2_req->mem_pool);
2145         *_smb2_req = state->smb2_req;
2146         tevent_req_received(req);
2147         return NT_STATUS_OK;
2148 }
2149
2150 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
2151
2152 void smbd_smb2_first_negprot(struct smbd_server_connection *sconn,
2153                              const uint8_t *inbuf, size_t size)
2154 {
2155         NTSTATUS status;
2156         struct smbd_smb2_request *req = NULL;
2157         struct tevent_req *subreq;
2158
2159         if (lp_security() == SEC_SHARE) {
2160                 DEBUG(2,("WARNING!!: \"security = share\" is deprecated for "
2161                         "SMB2 servers. Mapping to \"security = user\" and "
2162                         "\"map to guest = Bad User\"\n" ));
2163                 lp_do_parameter(-1, "security", "user");
2164                 lp_do_parameter(-1, "map to guest", "Bad User");
2165         }
2166
2167         DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
2168                  (unsigned int)size));
2169
2170         status = smbd_initialize_smb2(sconn);
2171         if (!NT_STATUS_IS_OK(status)) {
2172                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2173                 return;
2174         }
2175
2176         status = smbd_smb2_request_create(sconn, inbuf, size, &req);
2177         if (!NT_STATUS_IS_OK(status)) {
2178                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2179                 return;
2180         }
2181
2182         status = smbd_smb2_request_setup_out(req);
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_dispatch(req);
2189         if (!NT_STATUS_IS_OK(status)) {
2190                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2191                 return;
2192         }
2193
2194         /* ask for the next request */
2195         subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
2196         if (subreq == NULL) {
2197                 smbd_server_connection_terminate(sconn, "no memory for reading");
2198                 return;
2199         }
2200         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);
2201 }
2202
2203 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
2204 {
2205         struct smbd_server_connection *sconn = tevent_req_callback_data(subreq,
2206                                                struct smbd_server_connection);
2207         NTSTATUS status;
2208         struct smbd_smb2_request *req = NULL;
2209
2210         status = smbd_smb2_request_read_recv(subreq, sconn, &req);
2211         TALLOC_FREE(subreq);
2212         if (!NT_STATUS_IS_OK(status)) {
2213                 DEBUG(2,("smbd_smb2_request_incoming: client read error %s\n",
2214                         nt_errstr(status)));
2215                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2216                 return;
2217         }
2218
2219         if (req->in.nbt_hdr[0] != 0x00) {
2220                 DEBUG(1,("smbd_smb2_request_incoming: ignore NBT[0x%02X] msg\n",
2221                          req->in.nbt_hdr[0]));
2222                 TALLOC_FREE(req);
2223                 goto next;
2224         }
2225
2226         req->current_idx = 1;
2227
2228         DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
2229                  req->current_idx, req->in.vector_count));
2230
2231         status = smbd_smb2_request_validate(req);
2232         if (!NT_STATUS_IS_OK(status)) {
2233                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2234                 return;
2235         }
2236
2237         status = smbd_smb2_request_setup_out(req);
2238         if (!NT_STATUS_IS_OK(status)) {
2239                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2240                 return;
2241         }
2242
2243         status = smbd_smb2_request_dispatch(req);
2244         if (!NT_STATUS_IS_OK(status)) {
2245                 smbd_server_connection_terminate(sconn, nt_errstr(status));
2246                 return;
2247         }
2248
2249 next:
2250         /* ask for the next request (this constructs the main loop) */
2251         subreq = smbd_smb2_request_read_send(sconn, sconn->smb2.event_ctx, sconn);
2252         if (subreq == NULL) {
2253                 smbd_server_connection_terminate(sconn, "no memory for reading");
2254                 return;
2255         }
2256         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, sconn);
2257 }