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