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