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