s3:smbd: more validation of the incoming SMB2 requests
[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
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
24 #include "../lib/tsocket/tsocket.h"
25
26 bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size)
27 {
28         if (size < (4 + SMB2_HDR_BODY)) {
29                 return false;
30         }
31
32         if (IVAL(inbuf, 4) != SMB2_MAGIC) {
33                 return false;
34         }
35
36         return true;
37 }
38
39 static NTSTATUS smbd_initialize_smb2(struct smbd_server_connection *conn)
40 {
41         NTSTATUS status;
42         int ret;
43
44         TALLOC_FREE(conn->smb1.fde);
45
46         conn->smb2.event_ctx = smbd_event_context();
47
48         conn->smb2.recv_queue = tevent_queue_create(conn, "smb2 recv queue");
49         if (conn->smb2.recv_queue == NULL) {
50                 return NT_STATUS_NO_MEMORY;
51         }
52
53         conn->smb2.send_queue = tevent_queue_create(conn, "smb2 send queue");
54         if (conn->smb2.send_queue == NULL) {
55                 return NT_STATUS_NO_MEMORY;
56         }
57
58         conn->smb2.sessions.idtree = idr_init(conn);
59         if (conn->smb2.sessions.idtree == NULL) {
60                 return NT_STATUS_NO_MEMORY;
61         }
62         conn->smb2.sessions.limit = 0x0000FFFE;
63         conn->smb2.sessions.list = NULL;
64
65         ret = tstream_bsd_existing_socket(conn, smbd_server_fd(),
66                                           &conn->smb2.stream);
67         if (ret == -1) {
68                 status = map_nt_error_from_unix(errno);
69                 return status;
70         }
71
72         /* Ensure child is set to non-blocking mode */
73         set_blocking(smbd_server_fd(),false);
74         return NT_STATUS_OK;
75 }
76
77 #define smb2_len(buf) (PVAL(buf,3)|(PVAL(buf,2)<<8)|(PVAL(buf,1)<<16))
78 #define _smb2_setlen(_buf,len) do { \
79         uint8_t *buf = (uint8_t *)_buf; \
80         buf[0] = 0; \
81         buf[1] = ((len)&0xFF0000)>>16; \
82         buf[2] = ((len)&0xFF00)>>8; \
83         buf[3] = (len)&0xFF; \
84 } while (0)
85
86 static void smb2_setup_nbt_length(struct iovec *vector, int count)
87 {
88         size_t len = 0;
89         int i;
90
91         for (i=1; i < count; i++) {
92                 len += vector[i].iov_len;
93         }
94
95         _smb2_setlen(vector[0].iov_base, len);
96 }
97
98 static NTSTATUS smbd_smb2_request_create(struct smbd_server_connection *conn,
99                                          const uint8_t *inbuf, size_t size,
100                                          struct smbd_smb2_request **_req)
101 {
102         TALLOC_CTX *mem_pool;
103         struct smbd_smb2_request *req;
104         uint32_t protocol_version;
105         const uint8_t *inhdr = NULL;
106         off_t ofs = 0;
107         uint16_t cmd;
108         uint32_t next_command_ofs;
109
110         if (size < (4 + SMB2_HDR_BODY + 2)) {
111                 DEBUG(0,("Invalid SMB2 packet length count %ld\n", (long)size));
112                 return NT_STATUS_INVALID_PARAMETER;
113         }
114
115         inhdr = inbuf + 4;
116
117         protocol_version = IVAL(inhdr, SMB2_HDR_PROTOCOL_ID);
118         if (protocol_version != SMB2_MAGIC) {
119                 DEBUG(0,("Invalid SMB packet: protocol prefix: 0x%08X\n",
120                          protocol_version));
121                 return NT_STATUS_INVALID_PARAMETER;
122         }
123
124         cmd = SVAL(inhdr, SMB2_HDR_OPCODE);
125         if (cmd != SMB2_OP_NEGPROT) {
126                 DEBUG(0,("Invalid SMB packet: first request: 0x%04X\n",
127                          cmd));
128                 return NT_STATUS_INVALID_PARAMETER;
129         }
130
131         next_command_ofs = IVAL(inhdr, SMB2_HDR_NEXT_COMMAND);
132         if (next_command_ofs != 0) {
133                 DEBUG(0,("Invalid SMB packet: next_command: 0x%08X\n",
134                          next_command_ofs));
135                 return NT_STATUS_INVALID_PARAMETER;
136         }
137
138         mem_pool = talloc_pool(conn, 8192);
139         if (mem_pool == NULL) {
140                 return NT_STATUS_NO_MEMORY;
141         }
142
143         req = talloc_zero(mem_pool, struct smbd_smb2_request);
144         if (req == NULL) {
145                 talloc_free(mem_pool);
146                 return NT_STATUS_NO_MEMORY;
147         }
148         req->mem_pool   = mem_pool;
149         req->conn       = conn;
150
151         talloc_steal(req, inbuf);
152
153         req->in.vector = talloc_array(req, struct iovec, 4);
154         if (req->in.vector == NULL) {
155                 talloc_free(mem_pool);
156                 return NT_STATUS_NO_MEMORY;
157         }
158         req->in.vector_count = 4;
159
160         memcpy(req->in.nbt_hdr, inbuf, 4);
161
162         ofs = 0;
163         req->in.vector[0].iov_base      = (void *)req->in.nbt_hdr;
164         req->in.vector[0].iov_len       = 4;
165         ofs += req->in.vector[0].iov_len;
166
167         req->in.vector[1].iov_base      = (void *)(inbuf + ofs);
168         req->in.vector[1].iov_len       = SMB2_HDR_BODY;
169         ofs += req->in.vector[1].iov_len;
170
171         req->in.vector[2].iov_base      = (void *)(inbuf + ofs);
172         req->in.vector[2].iov_len       = SVAL(inbuf, ofs) & 0xFFFE;
173         ofs += req->in.vector[2].iov_len;
174
175         if (ofs > size) {
176                 return NT_STATUS_INVALID_PARAMETER;
177         }
178
179         req->in.vector[3].iov_base      = (void *)(inbuf + ofs);
180         req->in.vector[3].iov_len       = size - ofs;
181         ofs += req->in.vector[3].iov_len;
182
183         req->current_idx = 1;
184
185         *_req = req;
186         return NT_STATUS_OK;
187 }
188
189 static NTSTATUS smbd_smb2_request_validate(struct smbd_smb2_request *req)
190 {
191         int count;
192         int idx;
193         bool compound_related = false;
194
195         count = req->in.vector_count;
196
197         if (count < 4) {
198                 /* It's not a SMB2 request */
199                 return NT_STATUS_INVALID_PARAMETER;
200         }
201
202         for (idx=1; idx < count; idx += 3) {
203                 const uint8_t *inhdr = NULL;
204                 uint32_t flags;
205
206                 if (req->in.vector[idx].iov_len != SMB2_HDR_BODY) {
207                         return NT_STATUS_INVALID_PARAMETER;
208                 }
209
210                 if (req->in.vector[idx+1].iov_len < 2) {
211                         return NT_STATUS_INVALID_PARAMETER;
212                 }
213
214                 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
215
216                 /* setup the SMB2 header */
217                 if (IVAL(inhdr, SMB2_HDR_PROTOCOL_ID) != SMB2_MAGIC) {
218                         return NT_STATUS_INVALID_PARAMETER;
219                 }
220
221                 flags = IVAL(inhdr, SMB2_HDR_FLAGS);
222                 if (idx == 1) {
223                         /*
224                          * the 1st request should never have the
225                          * SMB2_HDR_FLAG_CHAINED flag set
226                          */
227                         if (flags & SMB2_HDR_FLAG_CHAINED) {
228                                 req->next_status = NT_STATUS_INVALID_PARAMETER;
229                                 return NT_STATUS_OK;
230                         }
231                 } else if (idx == 4) {
232                         /*
233                          * the 2nd request triggers related vs. unrelated
234                          * compounded requests
235                          */
236                         if (flags & SMB2_HDR_FLAG_CHAINED) {
237                                 compound_related = true;
238                         }
239                 } else if (idx > 4) {
240                         /*
241                          * all other requests should match the 2nd one
242                          */
243                         if (flags & SMB2_HDR_FLAG_CHAINED) {
244                                 if (!compound_related) {
245                                         req->next_status =
246                                                 NT_STATUS_INVALID_PARAMETER;
247                                         return NT_STATUS_OK;
248                                 }
249                         } else {
250                                 if (compound_related) {
251                                         req->next_status =
252                                                 NT_STATUS_INVALID_PARAMETER;
253                                         return NT_STATUS_OK;
254                                 }
255                         }
256                 }
257         }
258
259         return NT_STATUS_OK;
260 }
261
262 static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req)
263 {
264         struct iovec *vector;
265         int count;
266         int idx;
267
268         count = req->in.vector_count;
269         vector = talloc_array(req, struct iovec, count);
270         if (vector == NULL) {
271                 return NT_STATUS_NO_MEMORY;
272         }
273
274         vector[0].iov_base      = req->out.nbt_hdr;
275         vector[0].iov_len       = 4;
276         SIVAL(req->out.nbt_hdr, 0, 0);
277
278         for (idx=1; idx < count; idx += 3) {
279                 const uint8_t *inhdr = NULL;
280                 uint8_t *outhdr = NULL;
281                 uint8_t *outbody = NULL;
282                 uint32_t next_command_ofs = 0;
283                 struct iovec *current = &vector[idx];
284
285                 if ((idx + 3) < count) {
286                         /* we have a next command */
287                         next_command_ofs = SMB2_HDR_BODY + 8;
288                 }
289
290                 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
291
292                 outhdr = talloc_array(vector, uint8_t,
293                                       SMB2_HDR_BODY + 8);
294                 if (outhdr == NULL) {
295                         return NT_STATUS_NO_MEMORY;
296                 }
297
298                 outbody = outhdr + SMB2_HDR_BODY;
299
300                 current[0].iov_base     = (void *)outhdr;
301                 current[0].iov_len      = SMB2_HDR_BODY;
302
303                 current[1].iov_base     = (void *)outbody;
304                 current[1].iov_len      = 8;
305
306                 current[2].iov_base     = NULL;
307                 current[2].iov_len      = 0;
308
309                 /* setup the SMB2 header */
310                 SIVAL(outhdr, SMB2_HDR_PROTOCOL_ID,     SMB2_MAGIC);
311                 SSVAL(outhdr, SMB2_HDR_LENGTH,          SMB2_HDR_BODY);
312                 SSVAL(outhdr, SMB2_HDR_EPOCH,           0);
313                 SIVAL(outhdr, SMB2_HDR_STATUS,
314                       NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));
315                 SSVAL(outhdr, SMB2_HDR_OPCODE,
316                       SVAL(inhdr, SMB2_HDR_OPCODE));
317                 /* Make up a number for now... JRA. FIXME ! FIXME !*/
318                 SSVAL(outhdr, SMB2_HDR_CREDIT,          20);
319                 SIVAL(outhdr, SMB2_HDR_FLAGS,           SMB2_HDR_FLAG_REDIRECT);
320                 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND,    next_command_ofs);
321                 SBVAL(outhdr, SMB2_HDR_MESSAGE_ID,
322                       BVAL(inhdr, SMB2_HDR_MESSAGE_ID));
323                 SIVAL(outhdr, SMB2_HDR_PID,
324                       IVAL(inhdr, SMB2_HDR_PID));
325                 SIVAL(outhdr, SMB2_HDR_TID,
326                       IVAL(inhdr, SMB2_HDR_TID));
327                 SBVAL(outhdr, SMB2_HDR_SESSION_ID,
328                       BVAL(inhdr, SMB2_HDR_SESSION_ID));
329                 memset(outhdr + SMB2_HDR_SIGNATURE, 0, 16);
330
331                 /* setup error body header */
332                 SSVAL(outbody, 0x00, 0x08 + 1);
333                 SSVAL(outbody, 0x02, 0);
334                 SIVAL(outbody, 0x04, 0);
335         }
336
337         req->out.vector = vector;
338         req->out.vector_count = count;
339
340         /* setup the length of the NBT packet */
341         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
342
343         return NT_STATUS_OK;
344 }
345
346 void smbd_server_connection_terminate_ex(struct smbd_server_connection *sconn,
347                                          const char *reason,
348                                          const char *location)
349 {
350         DEBUG(10,("smbd_server_connection_terminate_ex: reason[%s] at %s\n",
351                   reason, location));
352         exit_server_cleanly(reason);
353 }
354
355 static NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
356 {
357         const uint8_t *inhdr;
358         int i = req->current_idx;
359         uint16_t opcode;
360         uint32_t flags;
361         NTSTATUS status;
362         NTSTATUS session_status;
363
364         inhdr = (const uint8_t *)req->in.vector[i].iov_base;
365
366         /* TODO: verify more things */
367
368         flags = IVAL(inhdr, SMB2_HDR_FLAGS);
369         opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
370         DEBUG(10,("smbd_smb2_request_dispatch: opcode[%u]\n", opcode));
371
372 #define TMP_SMB2_ALLOWED_FLAGS ( \
373         SMB2_HDR_FLAG_CHAINED | \
374         SMB2_HDR_FLAG_SIGNED | \
375         SMB2_HDR_FLAG_DFS)
376         if ((flags & ~TMP_SMB2_ALLOWED_FLAGS) != 0) {
377                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
378         }
379 #undef TMP_SMB2_ALLOWED_FLAGS
380
381         session_status = smbd_smb2_request_check_session(req);
382
383         req->do_signing = false;
384         if (flags & SMB2_HDR_FLAG_SIGNED) {
385                 if (!NT_STATUS_IS_OK(session_status)) {
386                         return smbd_smb2_request_error(req, session_status);
387                 }
388
389                 req->do_signing = true;
390                 status = smb2_signing_check_pdu(req->session->session_key,
391                                                 &req->in.vector[i], 3);
392                 if (!NT_STATUS_IS_OK(status)) {
393                         return smbd_smb2_request_error(req, status);
394                 }
395         } else if (req->session && req->session->do_signing) {
396                 return smbd_smb2_request_error(req, NT_STATUS_ACCESS_DENIED);
397         }
398
399         /*
400          * This check is mostly for giving the correct error code
401          * for compounded requests.
402          *
403          * TODO: we may need to move this after the session and tcon checks.
404          */
405         if (!NT_STATUS_IS_OK(req->next_status)) {
406                 return smbd_smb2_request_error(req, req->next_status);
407         }
408
409         switch (opcode) {
410         case SMB2_OP_NEGPROT:
411                 return smbd_smb2_request_process_negprot(req);
412
413         case SMB2_OP_SESSSETUP:
414                 return smbd_smb2_request_process_sesssetup(req);
415
416         case SMB2_OP_LOGOFF:
417                 if (!NT_STATUS_IS_OK(session_status)) {
418                         return smbd_smb2_request_error(req, session_status);
419                 }
420                 return smbd_smb2_request_process_logoff(req);
421
422         case SMB2_OP_TCON:
423                 if (!NT_STATUS_IS_OK(session_status)) {
424                         return smbd_smb2_request_error(req, session_status);
425                 }
426                 status = smbd_smb2_request_check_session(req);
427                 if (!NT_STATUS_IS_OK(status)) {
428                         return smbd_smb2_request_error(req, status);
429                 }
430                 return smbd_smb2_request_process_tcon(req);
431
432         case SMB2_OP_TDIS:
433                 if (!NT_STATUS_IS_OK(session_status)) {
434                         return smbd_smb2_request_error(req, session_status);
435                 }
436                 status = smbd_smb2_request_check_tcon(req);
437                 if (!NT_STATUS_IS_OK(status)) {
438                         return smbd_smb2_request_error(req, status);
439                 }
440                 return smbd_smb2_request_process_tdis(req);
441
442         case SMB2_OP_CREATE:
443                 if (!NT_STATUS_IS_OK(session_status)) {
444                         return smbd_smb2_request_error(req, session_status);
445                 }
446                 status = smbd_smb2_request_check_tcon(req);
447                 if (!NT_STATUS_IS_OK(status)) {
448                         return smbd_smb2_request_error(req, status);
449                 }
450                 return smbd_smb2_request_process_create(req);
451
452         case SMB2_OP_CLOSE:
453                 if (!NT_STATUS_IS_OK(session_status)) {
454                         return smbd_smb2_request_error(req, session_status);
455                 }
456                 status = smbd_smb2_request_check_tcon(req);
457                 if (!NT_STATUS_IS_OK(status)) {
458                         return smbd_smb2_request_error(req, status);
459                 }
460                 return smbd_smb2_request_process_close(req);
461
462         case SMB2_OP_FLUSH:
463                 if (!NT_STATUS_IS_OK(session_status)) {
464                         return smbd_smb2_request_error(req, session_status);
465                 }
466                 status = smbd_smb2_request_check_tcon(req);
467                 if (!NT_STATUS_IS_OK(status)) {
468                         return smbd_smb2_request_error(req, status);
469                 }
470                 return smbd_smb2_request_process_flush(req);
471
472         case SMB2_OP_READ:
473                 if (!NT_STATUS_IS_OK(session_status)) {
474                         return smbd_smb2_request_error(req, session_status);
475                 }
476                 status = smbd_smb2_request_check_tcon(req);
477                 if (!NT_STATUS_IS_OK(status)) {
478                         return smbd_smb2_request_error(req, status);
479                 }
480                 return smbd_smb2_request_process_read(req);
481
482         case SMB2_OP_WRITE:
483                 if (!NT_STATUS_IS_OK(session_status)) {
484                         return smbd_smb2_request_error(req, session_status);
485                 }
486                 status = smbd_smb2_request_check_tcon(req);
487                 if (!NT_STATUS_IS_OK(status)) {
488                         return smbd_smb2_request_error(req, status);
489                 }
490                 return smbd_smb2_request_process_write(req);
491
492         case SMB2_OP_LOCK:
493                 if (!NT_STATUS_IS_OK(session_status)) {
494                         return smbd_smb2_request_error(req, session_status);
495                 }
496                 status = smbd_smb2_request_check_tcon(req);
497                 if (!NT_STATUS_IS_OK(status)) {
498                         return smbd_smb2_request_error(req, status);
499                 }
500                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
501
502         case SMB2_OP_IOCTL:
503                 if (!NT_STATUS_IS_OK(session_status)) {
504                         return smbd_smb2_request_error(req, session_status);
505                 }
506                 status = smbd_smb2_request_check_tcon(req);
507                 if (!NT_STATUS_IS_OK(status)) {
508                         return smbd_smb2_request_error(req, status);
509                 }
510                 return smbd_smb2_request_process_ioctl(req);
511
512         case SMB2_OP_CANCEL:
513                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
514
515         case SMB2_OP_KEEPALIVE:
516                 return smbd_smb2_request_process_keepalive(req);
517
518         case SMB2_OP_FIND:
519                 if (!NT_STATUS_IS_OK(session_status)) {
520                         return smbd_smb2_request_error(req, session_status);
521                 }
522                 status = smbd_smb2_request_check_tcon(req);
523                 if (!NT_STATUS_IS_OK(status)) {
524                         return smbd_smb2_request_error(req, status);
525                 }
526                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
527
528         case SMB2_OP_NOTIFY:
529                 if (!NT_STATUS_IS_OK(session_status)) {
530                         return smbd_smb2_request_error(req, session_status);
531                 }
532                 status = smbd_smb2_request_check_tcon(req);
533                 if (!NT_STATUS_IS_OK(status)) {
534                         return smbd_smb2_request_error(req, status);
535                 }
536                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
537
538         case SMB2_OP_GETINFO:
539                 if (!NT_STATUS_IS_OK(session_status)) {
540                         return smbd_smb2_request_error(req, session_status);
541                 }
542                 status = smbd_smb2_request_check_tcon(req);
543                 if (!NT_STATUS_IS_OK(status)) {
544                         return smbd_smb2_request_error(req, status);
545                 }
546                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
547
548         case SMB2_OP_SETINFO:
549                 if (!NT_STATUS_IS_OK(session_status)) {
550                         return smbd_smb2_request_error(req, session_status);
551                 }
552                 status = smbd_smb2_request_check_tcon(req);
553                 if (!NT_STATUS_IS_OK(status)) {
554                         return smbd_smb2_request_error(req, status);
555                 }
556                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
557
558         case SMB2_OP_BREAK:
559                 if (!NT_STATUS_IS_OK(session_status)) {
560                         return smbd_smb2_request_error(req, session_status);
561                 }
562                 status = smbd_smb2_request_check_tcon(req);
563                 if (!NT_STATUS_IS_OK(status)) {
564                         return smbd_smb2_request_error(req, status);
565                 }
566                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
567         }
568
569         return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
570 }
571
572 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq);
573 static void smbd_smb2_request_writev_done(struct tevent_req *subreq);
574
575 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
576 {
577         struct tevent_req *subreq;
578
579         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
580
581         if (req->do_signing) {
582                 int i = req->current_idx;
583                 NTSTATUS status;
584                 status = smb2_signing_sign_pdu(req->session->session_key,
585                                                &req->out.vector[i], 3);
586                 if (!NT_STATUS_IS_OK(status)) {
587                         return status;
588                 }
589         }
590
591         req->current_idx += 3;
592
593         if (req->current_idx < req->out.vector_count) {
594                 struct timeval zero = timeval_zero();
595                 subreq = tevent_wakeup_send(req,
596                                             req->conn->smb2.event_ctx,
597                                             zero);
598                 if (subreq == NULL) {
599                         return NT_STATUS_NO_MEMORY;
600                 }
601                 tevent_req_set_callback(subreq,
602                                         smbd_smb2_request_dispatch_compound,
603                                         req);
604
605                 return NT_STATUS_OK;
606         }
607
608         subreq = tstream_writev_queue_send(req,
609                                            req->conn->smb2.event_ctx,
610                                            req->conn->smb2.stream,
611                                            req->conn->smb2.send_queue,
612                                            req->out.vector,
613                                            req->out.vector_count);
614         if (subreq == NULL) {
615                 return NT_STATUS_NO_MEMORY;
616         }
617         tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
618
619         return NT_STATUS_OK;
620 }
621
622 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq)
623 {
624         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
625                                         struct smbd_smb2_request);
626         struct smbd_server_connection *conn = req->conn;
627         NTSTATUS status;
628
629         tevent_wakeup_recv(subreq);
630         TALLOC_FREE(subreq);
631
632         DEBUG(10,("smbd_smb2_request_dispatch_compound: idx[%d] of %d vectors\n",
633                   req->current_idx, req->in.vector_count));
634
635         status = smbd_smb2_request_dispatch(req);
636         if (!NT_STATUS_IS_OK(status)) {
637                 smbd_server_connection_terminate(conn, nt_errstr(status));
638                 return;
639         }
640 }
641
642 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
643 {
644         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
645                                         struct smbd_smb2_request);
646         struct smbd_server_connection *conn = req->conn;
647         int ret;
648         int sys_errno;
649         TALLOC_CTX *mem_pool;
650
651         ret = tstream_writev_queue_recv(subreq, &sys_errno);
652         TALLOC_FREE(subreq);
653         if (ret == -1) {
654                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
655                 smbd_server_connection_terminate(conn, nt_errstr(status));
656                 return;
657         }
658
659         mem_pool = req->mem_pool;
660         req = NULL;
661         talloc_free(mem_pool);
662 }
663
664 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
665                                     NTSTATUS status,
666                                     DATA_BLOB *info,
667                                     const char *location)
668 {
669         uint8_t *outhdr;
670         uint8_t *outbody;
671         int i = req->current_idx;
672
673         DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s] |%s| at %s\n",
674                   i, nt_errstr(status), info ? " +info" : "",
675                   location));
676
677         outhdr = (uint8_t *)req->out.vector[i].iov_base;
678
679         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
680
681         outbody = outhdr + SMB2_HDR_BODY;
682
683         req->out.vector[i+1].iov_base = (void *)outbody;
684         req->out.vector[i+1].iov_len = 8;
685
686         if (info) {
687                 SIVAL(outbody, 0x04, info->length);
688                 req->out.vector[i+2].iov_base   = (void *)info->data;
689                 req->out.vector[i+2].iov_len    = info->length;
690         } else {
691                 req->out.vector[i+2].iov_base = NULL;
692                 req->out.vector[i+2].iov_len = 0;
693         }
694
695         /*
696          * if a request fails, all other remaining
697          * compounded requests should fail too
698          */
699         req->next_status = NT_STATUS_INVALID_PARAMETER;
700
701         return smbd_smb2_request_reply(req);
702 }
703
704 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
705                                    NTSTATUS status,
706                                    DATA_BLOB body, DATA_BLOB *dyn,
707                                    const char *location)
708 {
709         uint8_t *outhdr;
710         uint8_t *outdyn;
711         int i = req->current_idx;
712         uint32_t next_command_ofs;
713
714         DEBUG(10,("smbd_smb2_request_done_ex: "
715                   "idx[%d] status[%s] body[%u] dyn[%s:%u] at %s\n",
716                   i, nt_errstr(status), (unsigned int)body.length,
717                   dyn ? "yes": "no",
718                   (unsigned int)(dyn ? dyn->length : 0),
719                   location));
720
721         if (body.length < 2) {
722                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
723         }
724
725         if ((body.length % 2) != 0) {
726                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
727         }
728
729         outhdr = (uint8_t *)req->out.vector[i].iov_base;
730         /* the fallback dynamic buffer */
731         outdyn = outhdr + SMB2_HDR_BODY + 8;
732
733         next_command_ofs = IVAL(outhdr, SMB2_HDR_NEXT_COMMAND);
734         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
735
736         req->out.vector[i+1].iov_base = (void *)body.data;
737         req->out.vector[i+1].iov_len = body.length;
738
739         if (dyn) {
740                 req->out.vector[i+2].iov_base   = (void *)dyn->data;
741                 req->out.vector[i+2].iov_len    = dyn->length;
742         } else {
743                 req->out.vector[i+2].iov_base = NULL;
744                 req->out.vector[i+2].iov_len = 0;
745         }
746
747         /* see if we need to recalculate the offset to the next response */
748         if (next_command_ofs > 0) {
749                 next_command_ofs  = SMB2_HDR_BODY;
750                 next_command_ofs += req->out.vector[i+1].iov_len;
751                 next_command_ofs += req->out.vector[i+2].iov_len;
752         }
753
754         /* TODO: we need to add padding ... */
755         if ((next_command_ofs % 8) != 0) {
756                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
757         }
758
759         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
760
761         return smbd_smb2_request_reply(req);
762 }
763
764 struct smbd_smb2_request_read_state {
765         size_t missing;
766         bool asked_for_header;
767         struct smbd_smb2_request *smb2_req;
768 };
769
770 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
771                                          void *private_data,
772                                          TALLOC_CTX *mem_ctx,
773                                          struct iovec **_vector,
774                                          size_t *_count);
775 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
776
777 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
778                                         struct tevent_context *ev,
779                                         struct smbd_server_connection *conn)
780 {
781         struct tevent_req *req;
782         struct smbd_smb2_request_read_state *state;
783         struct tevent_req *subreq;
784         TALLOC_CTX *mem_pool;
785
786         req = tevent_req_create(mem_ctx, &state,
787                                 struct smbd_smb2_request_read_state);
788         if (req == NULL) {
789                 return NULL;
790         }
791         state->missing = 0;
792         state->asked_for_header = false;
793
794         mem_pool = talloc_pool(state, 8192);
795         if (tevent_req_nomem(mem_pool, req)) {
796                 return tevent_req_post(req, ev);
797         }
798
799         state->smb2_req = talloc_zero(mem_pool, struct smbd_smb2_request);
800         if (tevent_req_nomem(state->smb2_req, req)) {
801                 return tevent_req_post(req, ev);
802         }
803
804         state->smb2_req->mem_pool       = mem_pool;
805         state->smb2_req->conn           = conn;
806
807         subreq = tstream_readv_pdu_queue_send(state, ev, conn->smb2.stream,
808                                               conn->smb2.recv_queue,
809                                               smbd_smb2_request_next_vector,
810                                               state);
811         if (tevent_req_nomem(subreq, req)) {
812                 return tevent_req_post(req, ev);
813         }
814         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
815
816         return req;
817 }
818
819 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
820                                          void *private_data,
821                                          TALLOC_CTX *mem_ctx,
822                                          struct iovec **_vector,
823                                          size_t *_count)
824 {
825         struct smbd_smb2_request_read_state *state =
826                 talloc_get_type_abort(private_data,
827                 struct smbd_smb2_request_read_state);
828         struct smbd_smb2_request *req = state->smb2_req;
829         struct iovec *vector;
830         int idx = req->in.vector_count;
831         size_t len = 0;
832         uint8_t *buf = NULL;
833
834         if (req->in.vector_count == 0) {
835                 /*
836                  * first we need to get the NBT header
837                  */
838                 req->in.vector = talloc_array(req, struct iovec,
839                                               req->in.vector_count + 1);
840                 if (req->in.vector == NULL) {
841                         return -1;
842                 }
843                 req->in.vector_count += 1;
844
845                 req->in.vector[idx].iov_base    = (void *)req->in.nbt_hdr;
846                 req->in.vector[idx].iov_len     = 4;
847
848                 vector = talloc_array(mem_ctx, struct iovec, 1);
849                 if (vector == NULL) {
850                         return -1;
851                 }
852
853                 vector[0] = req->in.vector[idx];
854
855                 *_vector = vector;
856                 *_count = 1;
857                 return 0;
858         }
859
860         if (req->in.vector_count == 1) {
861                 /*
862                  * Now we analyze the NBT header
863                  */
864                 state->missing = smb2_len(req->in.vector[0].iov_base);
865
866                 if (state->missing == 0) {
867                         /* if there're no remaining bytes, we're done */
868                         *_vector = NULL;
869                         *_count = 0;
870                         return 0;
871                 }
872
873                 req->in.vector = talloc_realloc(req, req->in.vector,
874                                                 struct iovec,
875                                                 req->in.vector_count + 1);
876                 if (req->in.vector == NULL) {
877                         return -1;
878                 }
879                 req->in.vector_count += 1;
880
881                 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
882                         /*
883                          * it's a special NBT message,
884                          * so get all remaining bytes
885                          */
886                         len = state->missing;
887                 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
888                         /*
889                          * it's an invalid message, just read what we can get
890                          * and let the caller handle the error
891                          */
892                         len = state->missing;
893                 } else {
894                         /*
895                          * We assume it's a SMB2 request,
896                          * and we first get the header and the
897                          * first 2 bytes (the struct size) of the body
898                          */
899                         len = SMB2_HDR_BODY + 2;
900
901                         state->asked_for_header = true;
902                 }
903
904                 state->missing -= len;
905
906                 buf = talloc_array(req->in.vector, uint8_t, len);
907                 if (buf == NULL) {
908                         return -1;
909                 }
910
911                 req->in.vector[idx].iov_base    = (void *)buf;
912                 req->in.vector[idx].iov_len     = len;
913
914                 vector = talloc_array(mem_ctx, struct iovec, 1);
915                 if (vector == NULL) {
916                         return -1;
917                 }
918
919                 vector[0] = req->in.vector[idx];
920
921                 *_vector = vector;
922                 *_count = 1;
923                 return 0;
924         }
925
926         if (state->missing == 0) {
927                 /* if there're no remaining bytes, we're done */
928                 *_vector = NULL;
929                 *_count = 0;
930                 return 0;
931         }
932
933         if (state->asked_for_header) {
934                 const uint8_t *hdr;
935                 size_t full_size;
936                 size_t next_command_ofs;
937                 size_t body_size;
938                 uint8_t *body;
939                 size_t dyn_size;
940                 uint8_t *dyn;
941                 bool invalid = false;
942
943                 state->asked_for_header = false;
944
945                 /*
946                  * We got the SMB2 header and the first 2 bytes
947                  * of the body. We fix the size to just the header
948                  * and manually copy the 2 first bytes to the body section
949                  */
950                 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
951                 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
952
953                 /* allocate vectors for body and dynamic areas */
954                 req->in.vector = talloc_realloc(req, req->in.vector,
955                                                 struct iovec,
956                                                 req->in.vector_count + 2);
957                 if (req->in.vector == NULL) {
958                         return -1;
959                 }
960                 req->in.vector_count += 2;
961
962                 full_size = state->missing + SMB2_HDR_BODY + 2;
963                 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
964                 body_size = SVAL(hdr, SMB2_HDR_BODY);
965
966                 if (next_command_ofs != 0) {
967                         if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
968                                 /*
969                                  * this is invalid, just return a zero
970                                  * body and let the caller deal with the error
971                                  */
972                                 invalid = true;
973                         } else if (next_command_ofs > full_size) {
974                                 /*
975                                  * this is invalid, just return a zero
976                                  * body and let the caller deal with the error
977                                  */
978                                 invalid = true;
979                         } else {
980                                 full_size = next_command_ofs;
981                         }
982                 }
983
984                 if (!invalid) {
985                         if (body_size < 2) {
986                                 /*
987                                  * this is invalid, just return a zero
988                                  * body and let the caller deal with the error
989                                  */
990                                 invalid = true;
991                         } else if (body_size > (full_size - SMB2_HDR_BODY)) {
992                                 /*
993                                  * this is invalid, just return a zero
994                                  * body and let the caller deal with the error
995                                  */
996                                 invalid = true;
997                         }
998                 }
999
1000                 if (invalid) {
1001                         /* the caller should check this */
1002                         body_size = 0;
1003                 }
1004
1005                 if ((body_size % 2) != 0) {
1006                         body_size -= 1;
1007                 }
1008
1009                 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
1010
1011                 state->missing -= (body_size - 2) + dyn_size;
1012
1013                 body = talloc_array(req->in.vector, uint8_t, body_size);
1014                 if (body == NULL) {
1015                         return -1;
1016                 }
1017
1018                 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
1019                 if (dyn == NULL) {
1020                         return -1;
1021                 }
1022
1023                 req->in.vector[idx].iov_base    = (void *)body;
1024                 req->in.vector[idx].iov_len     = body_size;
1025                 req->in.vector[idx+1].iov_base  = (void *)dyn;
1026                 req->in.vector[idx+1].iov_len   = dyn_size;
1027
1028                 vector = talloc_array(mem_ctx, struct iovec, 2);
1029                 if (vector == NULL) {
1030                         return -1;
1031                 }
1032
1033                 /*
1034                  * the first 2 bytes of the body were already fetched
1035                  * together with the header
1036                  */
1037                 memcpy(body, hdr + SMB2_HDR_BODY, 2);
1038                 vector[0].iov_base = body + 2;
1039                 vector[0].iov_len = req->in.vector[idx].iov_len - 2;
1040
1041                 vector[1] = req->in.vector[idx+1];
1042
1043                 *_vector = vector;
1044                 *_count = 2;
1045                 return 0;
1046         }
1047
1048         /*
1049          * when we endup here, we're looking for a new SMB2 request
1050          * next. And we ask for its header and the first 2 bytes of
1051          * the body (like we did for the first SMB2 request).
1052          */
1053
1054         req->in.vector = talloc_realloc(req, req->in.vector,
1055                                         struct iovec,
1056                                         req->in.vector_count + 1);
1057         if (req->in.vector == NULL) {
1058                 return -1;
1059         }
1060         req->in.vector_count += 1;
1061
1062         /*
1063          * We assume it's a SMB2 request,
1064          * and we first get the header and the
1065          * first 2 bytes (the struct size) of the body
1066          */
1067         len = SMB2_HDR_BODY + 2;
1068
1069         if (len > state->missing) {
1070                 /* let the caller handle the error */
1071                 len = state->missing;
1072         }
1073
1074         state->missing -= len;
1075         state->asked_for_header = true;
1076
1077         buf = talloc_array(req->in.vector, uint8_t, len);
1078         if (buf == NULL) {
1079                 return -1;
1080         }
1081
1082         req->in.vector[idx].iov_base    = (void *)buf;
1083         req->in.vector[idx].iov_len     = len;
1084
1085         vector = talloc_array(mem_ctx, struct iovec, 1);
1086         if (vector == NULL) {
1087                 return -1;
1088         }
1089
1090         vector[0] = req->in.vector[idx];
1091
1092         *_vector = vector;
1093         *_count = 1;
1094         return 0;
1095 }
1096
1097 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
1098 {
1099         struct tevent_req *req =
1100                 tevent_req_callback_data(subreq,
1101                 struct tevent_req);
1102         int ret;
1103         int sys_errno;
1104         NTSTATUS status;
1105
1106         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1107         if (ret == -1) {
1108                 status = map_nt_error_from_unix(sys_errno);
1109                 tevent_req_nterror(req, status);
1110                 return;
1111         }
1112
1113         tevent_req_done(req);
1114 }
1115
1116 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
1117                                             TALLOC_CTX *mem_ctx,
1118                                             struct smbd_smb2_request **_smb2_req)
1119 {
1120         struct smbd_smb2_request_read_state *state =
1121                 tevent_req_data(req,
1122                 struct smbd_smb2_request_read_state);
1123         NTSTATUS status;
1124
1125         if (tevent_req_is_nterror(req, &status)) {
1126                 tevent_req_received(req);
1127                 return status;
1128         }
1129
1130         talloc_steal(mem_ctx, state->smb2_req->mem_pool);
1131         *_smb2_req = state->smb2_req;
1132         tevent_req_received(req);
1133         return NT_STATUS_OK;
1134 }
1135
1136 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
1137
1138 void smbd_smb2_first_negprot(struct smbd_server_connection *conn,
1139                              const uint8_t *inbuf, size_t size)
1140 {
1141         NTSTATUS status;
1142         struct smbd_smb2_request *req;
1143         struct tevent_req *subreq;
1144
1145         DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
1146                  (unsigned int)size));
1147
1148         status = smbd_initialize_smb2(conn);
1149         if (!NT_STATUS_IS_OK(status)) {
1150                 smbd_server_connection_terminate(conn, nt_errstr(status));
1151                 return;
1152         }
1153
1154         status = smbd_smb2_request_create(conn, inbuf, size, &req);
1155         if (!NT_STATUS_IS_OK(status)) {
1156                 smbd_server_connection_terminate(conn, nt_errstr(status));
1157                 return;
1158         }
1159
1160         status = smbd_smb2_request_setup_out(req);
1161         if (!NT_STATUS_IS_OK(status)) {
1162                 smbd_server_connection_terminate(conn, nt_errstr(status));
1163                 return;
1164         }
1165
1166         status = smbd_smb2_request_dispatch(req);
1167         if (!NT_STATUS_IS_OK(status)) {
1168                 smbd_server_connection_terminate(conn, nt_errstr(status));
1169                 return;
1170         }
1171
1172         /* ask for the next request */
1173         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
1174         if (subreq == NULL) {
1175                 smbd_server_connection_terminate(conn, "no memory for reading");
1176                 return;
1177         }
1178         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
1179 }
1180
1181 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
1182 {
1183         struct smbd_server_connection *conn = tevent_req_callback_data(subreq,
1184                                               struct smbd_server_connection);
1185         NTSTATUS status;
1186         struct smbd_smb2_request *req;
1187
1188         status = smbd_smb2_request_read_recv(subreq, conn, &req);
1189         TALLOC_FREE(subreq);
1190         if (!NT_STATUS_IS_OK(status)) {
1191                 smbd_server_connection_terminate(conn, nt_errstr(status));
1192                 return;
1193         }
1194
1195         if (req->in.nbt_hdr[0] != 0x00) {
1196                 DEBUG(1,("smbd_smb2_request_incoming: ignore NBT[0x%02X] msg\n",
1197                          req->in.nbt_hdr[0]));
1198                 talloc_free(req->mem_pool);
1199                 req = NULL;
1200                 goto next;
1201         }
1202
1203         req->current_idx = 1;
1204
1205         DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
1206                  req->current_idx, req->in.vector_count));
1207
1208         status = smbd_smb2_request_validate(req);
1209         if (!NT_STATUS_IS_OK(status)) {
1210                 smbd_server_connection_terminate(conn, nt_errstr(status));
1211                 return;
1212         }
1213
1214         status = smbd_smb2_request_setup_out(req);
1215         if (!NT_STATUS_IS_OK(status)) {
1216                 smbd_server_connection_terminate(conn, nt_errstr(status));
1217                 return;
1218         }
1219
1220         status = smbd_smb2_request_dispatch(req);
1221         if (!NT_STATUS_IS_OK(status)) {
1222                 smbd_server_connection_terminate(conn, nt_errstr(status));
1223                 return;
1224         }
1225
1226 next:
1227         /* ask for the next request (this constructs the main loop) */
1228         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
1229         if (subreq == NULL) {
1230                 smbd_server_connection_terminate(conn, "no memory for reading");
1231                 return;
1232         }
1233         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
1234 }