s3:smbd: make sure we pad compounded SMB2 responses to 8 bytes
[nivanova/samba-autobuild/.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         if ((next_command_ofs % 8) != 0) {
755                 size_t pad_size = 8 - (next_command_ofs % 8);
756                 if (req->out.vector[i+2].iov_len == 0) {
757                         /*
758                          * if the dyn buffer is empty
759                          * we can use it to add padding
760                          */
761                         uint8_t *pad;
762
763                         pad = talloc_zero_array(req->out.vector,
764                                                 uint8_t, pad_size);
765                         if (pad == NULL) {
766                                 return smbd_smb2_request_error(req,
767                                                 NT_STATUS_NO_MEMORY);
768                         }
769
770                         req->out.vector[i+2].iov_base = (void *)pad;
771                         req->out.vector[i+2].iov_len = pad_size;
772                 } else {
773                         /*
774                          * For now we copy the dynamic buffer
775                          * and add the padding to the new buffer
776                          */
777                         size_t old_size;
778                         uint8_t *old_dyn;
779                         size_t new_size;
780                         uint8_t *new_dyn;
781
782                         old_size = req->out.vector[i+2].iov_len;
783                         old_dyn = (uint8_t *)req->out.vector[i+2].iov_base;
784
785                         new_size = old_size + pad_size;
786                         new_dyn = talloc_array(req->out.vector,
787                                                uint8_t, new_size);
788                         if (new_dyn == NULL) {
789                                 return smbd_smb2_request_error(req,
790                                                 NT_STATUS_NO_MEMORY);
791                         }
792
793                         memcpy(new_dyn, old_dyn, old_size);
794                         memset(new_dyn + old_size, 0, pad_size);
795
796                         req->out.vector[i+2].iov_base = (void *)new_dyn;
797                         req->out.vector[i+2].iov_len = new_size;
798
799                         TALLOC_FREE(old_dyn);
800                 }
801                 next_command_ofs += pad_size;
802         }
803
804         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
805
806         return smbd_smb2_request_reply(req);
807 }
808
809 struct smbd_smb2_request_read_state {
810         size_t missing;
811         bool asked_for_header;
812         struct smbd_smb2_request *smb2_req;
813 };
814
815 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
816                                          void *private_data,
817                                          TALLOC_CTX *mem_ctx,
818                                          struct iovec **_vector,
819                                          size_t *_count);
820 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
821
822 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
823                                         struct tevent_context *ev,
824                                         struct smbd_server_connection *conn)
825 {
826         struct tevent_req *req;
827         struct smbd_smb2_request_read_state *state;
828         struct tevent_req *subreq;
829         TALLOC_CTX *mem_pool;
830
831         req = tevent_req_create(mem_ctx, &state,
832                                 struct smbd_smb2_request_read_state);
833         if (req == NULL) {
834                 return NULL;
835         }
836         state->missing = 0;
837         state->asked_for_header = false;
838
839         mem_pool = talloc_pool(state, 8192);
840         if (tevent_req_nomem(mem_pool, req)) {
841                 return tevent_req_post(req, ev);
842         }
843
844         state->smb2_req = talloc_zero(mem_pool, struct smbd_smb2_request);
845         if (tevent_req_nomem(state->smb2_req, req)) {
846                 return tevent_req_post(req, ev);
847         }
848
849         state->smb2_req->mem_pool       = mem_pool;
850         state->smb2_req->conn           = conn;
851
852         subreq = tstream_readv_pdu_queue_send(state, ev, conn->smb2.stream,
853                                               conn->smb2.recv_queue,
854                                               smbd_smb2_request_next_vector,
855                                               state);
856         if (tevent_req_nomem(subreq, req)) {
857                 return tevent_req_post(req, ev);
858         }
859         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
860
861         return req;
862 }
863
864 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
865                                          void *private_data,
866                                          TALLOC_CTX *mem_ctx,
867                                          struct iovec **_vector,
868                                          size_t *_count)
869 {
870         struct smbd_smb2_request_read_state *state =
871                 talloc_get_type_abort(private_data,
872                 struct smbd_smb2_request_read_state);
873         struct smbd_smb2_request *req = state->smb2_req;
874         struct iovec *vector;
875         int idx = req->in.vector_count;
876         size_t len = 0;
877         uint8_t *buf = NULL;
878
879         if (req->in.vector_count == 0) {
880                 /*
881                  * first we need to get the NBT header
882                  */
883                 req->in.vector = talloc_array(req, struct iovec,
884                                               req->in.vector_count + 1);
885                 if (req->in.vector == NULL) {
886                         return -1;
887                 }
888                 req->in.vector_count += 1;
889
890                 req->in.vector[idx].iov_base    = (void *)req->in.nbt_hdr;
891                 req->in.vector[idx].iov_len     = 4;
892
893                 vector = talloc_array(mem_ctx, struct iovec, 1);
894                 if (vector == NULL) {
895                         return -1;
896                 }
897
898                 vector[0] = req->in.vector[idx];
899
900                 *_vector = vector;
901                 *_count = 1;
902                 return 0;
903         }
904
905         if (req->in.vector_count == 1) {
906                 /*
907                  * Now we analyze the NBT header
908                  */
909                 state->missing = smb2_len(req->in.vector[0].iov_base);
910
911                 if (state->missing == 0) {
912                         /* if there're no remaining bytes, we're done */
913                         *_vector = NULL;
914                         *_count = 0;
915                         return 0;
916                 }
917
918                 req->in.vector = talloc_realloc(req, req->in.vector,
919                                                 struct iovec,
920                                                 req->in.vector_count + 1);
921                 if (req->in.vector == NULL) {
922                         return -1;
923                 }
924                 req->in.vector_count += 1;
925
926                 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
927                         /*
928                          * it's a special NBT message,
929                          * so get all remaining bytes
930                          */
931                         len = state->missing;
932                 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
933                         /*
934                          * it's an invalid message, just read what we can get
935                          * and let the caller handle the error
936                          */
937                         len = state->missing;
938                 } else {
939                         /*
940                          * We assume it's a SMB2 request,
941                          * and we first get the header and the
942                          * first 2 bytes (the struct size) of the body
943                          */
944                         len = SMB2_HDR_BODY + 2;
945
946                         state->asked_for_header = true;
947                 }
948
949                 state->missing -= len;
950
951                 buf = talloc_array(req->in.vector, uint8_t, len);
952                 if (buf == NULL) {
953                         return -1;
954                 }
955
956                 req->in.vector[idx].iov_base    = (void *)buf;
957                 req->in.vector[idx].iov_len     = len;
958
959                 vector = talloc_array(mem_ctx, struct iovec, 1);
960                 if (vector == NULL) {
961                         return -1;
962                 }
963
964                 vector[0] = req->in.vector[idx];
965
966                 *_vector = vector;
967                 *_count = 1;
968                 return 0;
969         }
970
971         if (state->missing == 0) {
972                 /* if there're no remaining bytes, we're done */
973                 *_vector = NULL;
974                 *_count = 0;
975                 return 0;
976         }
977
978         if (state->asked_for_header) {
979                 const uint8_t *hdr;
980                 size_t full_size;
981                 size_t next_command_ofs;
982                 size_t body_size;
983                 uint8_t *body;
984                 size_t dyn_size;
985                 uint8_t *dyn;
986                 bool invalid = false;
987
988                 state->asked_for_header = false;
989
990                 /*
991                  * We got the SMB2 header and the first 2 bytes
992                  * of the body. We fix the size to just the header
993                  * and manually copy the 2 first bytes to the body section
994                  */
995                 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
996                 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
997
998                 /* allocate vectors for body and dynamic areas */
999                 req->in.vector = talloc_realloc(req, req->in.vector,
1000                                                 struct iovec,
1001                                                 req->in.vector_count + 2);
1002                 if (req->in.vector == NULL) {
1003                         return -1;
1004                 }
1005                 req->in.vector_count += 2;
1006
1007                 full_size = state->missing + SMB2_HDR_BODY + 2;
1008                 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
1009                 body_size = SVAL(hdr, SMB2_HDR_BODY);
1010
1011                 if (next_command_ofs != 0) {
1012                         if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
1013                                 /*
1014                                  * this is invalid, just return a zero
1015                                  * body and let the caller deal with the error
1016                                  */
1017                                 invalid = true;
1018                         } else if (next_command_ofs > full_size) {
1019                                 /*
1020                                  * this is invalid, just return a zero
1021                                  * body and let the caller deal with the error
1022                                  */
1023                                 invalid = true;
1024                         } else {
1025                                 full_size = next_command_ofs;
1026                         }
1027                 }
1028
1029                 if (!invalid) {
1030                         if (body_size < 2) {
1031                                 /*
1032                                  * this is invalid, just return a zero
1033                                  * body and let the caller deal with the error
1034                                  */
1035                                 invalid = true;
1036                         } else if (body_size > (full_size - SMB2_HDR_BODY)) {
1037                                 /*
1038                                  * this is invalid, just return a zero
1039                                  * body and let the caller deal with the error
1040                                  */
1041                                 invalid = true;
1042                         }
1043                 }
1044
1045                 if (invalid) {
1046                         /* the caller should check this */
1047                         body_size = 0;
1048                 }
1049
1050                 if ((body_size % 2) != 0) {
1051                         body_size -= 1;
1052                 }
1053
1054                 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
1055
1056                 state->missing -= (body_size - 2) + dyn_size;
1057
1058                 body = talloc_array(req->in.vector, uint8_t, body_size);
1059                 if (body == NULL) {
1060                         return -1;
1061                 }
1062
1063                 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
1064                 if (dyn == NULL) {
1065                         return -1;
1066                 }
1067
1068                 req->in.vector[idx].iov_base    = (void *)body;
1069                 req->in.vector[idx].iov_len     = body_size;
1070                 req->in.vector[idx+1].iov_base  = (void *)dyn;
1071                 req->in.vector[idx+1].iov_len   = dyn_size;
1072
1073                 vector = talloc_array(mem_ctx, struct iovec, 2);
1074                 if (vector == NULL) {
1075                         return -1;
1076                 }
1077
1078                 /*
1079                  * the first 2 bytes of the body were already fetched
1080                  * together with the header
1081                  */
1082                 memcpy(body, hdr + SMB2_HDR_BODY, 2);
1083                 vector[0].iov_base = body + 2;
1084                 vector[0].iov_len = req->in.vector[idx].iov_len - 2;
1085
1086                 vector[1] = req->in.vector[idx+1];
1087
1088                 *_vector = vector;
1089                 *_count = 2;
1090                 return 0;
1091         }
1092
1093         /*
1094          * when we endup here, we're looking for a new SMB2 request
1095          * next. And we ask for its header and the first 2 bytes of
1096          * the body (like we did for the first SMB2 request).
1097          */
1098
1099         req->in.vector = talloc_realloc(req, req->in.vector,
1100                                         struct iovec,
1101                                         req->in.vector_count + 1);
1102         if (req->in.vector == NULL) {
1103                 return -1;
1104         }
1105         req->in.vector_count += 1;
1106
1107         /*
1108          * We assume it's a SMB2 request,
1109          * and we first get the header and the
1110          * first 2 bytes (the struct size) of the body
1111          */
1112         len = SMB2_HDR_BODY + 2;
1113
1114         if (len > state->missing) {
1115                 /* let the caller handle the error */
1116                 len = state->missing;
1117         }
1118
1119         state->missing -= len;
1120         state->asked_for_header = true;
1121
1122         buf = talloc_array(req->in.vector, uint8_t, len);
1123         if (buf == NULL) {
1124                 return -1;
1125         }
1126
1127         req->in.vector[idx].iov_base    = (void *)buf;
1128         req->in.vector[idx].iov_len     = len;
1129
1130         vector = talloc_array(mem_ctx, struct iovec, 1);
1131         if (vector == NULL) {
1132                 return -1;
1133         }
1134
1135         vector[0] = req->in.vector[idx];
1136
1137         *_vector = vector;
1138         *_count = 1;
1139         return 0;
1140 }
1141
1142 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
1143 {
1144         struct tevent_req *req =
1145                 tevent_req_callback_data(subreq,
1146                 struct tevent_req);
1147         int ret;
1148         int sys_errno;
1149         NTSTATUS status;
1150
1151         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1152         if (ret == -1) {
1153                 status = map_nt_error_from_unix(sys_errno);
1154                 tevent_req_nterror(req, status);
1155                 return;
1156         }
1157
1158         tevent_req_done(req);
1159 }
1160
1161 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
1162                                             TALLOC_CTX *mem_ctx,
1163                                             struct smbd_smb2_request **_smb2_req)
1164 {
1165         struct smbd_smb2_request_read_state *state =
1166                 tevent_req_data(req,
1167                 struct smbd_smb2_request_read_state);
1168         NTSTATUS status;
1169
1170         if (tevent_req_is_nterror(req, &status)) {
1171                 tevent_req_received(req);
1172                 return status;
1173         }
1174
1175         talloc_steal(mem_ctx, state->smb2_req->mem_pool);
1176         *_smb2_req = state->smb2_req;
1177         tevent_req_received(req);
1178         return NT_STATUS_OK;
1179 }
1180
1181 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
1182
1183 void smbd_smb2_first_negprot(struct smbd_server_connection *conn,
1184                              const uint8_t *inbuf, size_t size)
1185 {
1186         NTSTATUS status;
1187         struct smbd_smb2_request *req;
1188         struct tevent_req *subreq;
1189
1190         DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
1191                  (unsigned int)size));
1192
1193         status = smbd_initialize_smb2(conn);
1194         if (!NT_STATUS_IS_OK(status)) {
1195                 smbd_server_connection_terminate(conn, nt_errstr(status));
1196                 return;
1197         }
1198
1199         status = smbd_smb2_request_create(conn, inbuf, size, &req);
1200         if (!NT_STATUS_IS_OK(status)) {
1201                 smbd_server_connection_terminate(conn, nt_errstr(status));
1202                 return;
1203         }
1204
1205         status = smbd_smb2_request_setup_out(req);
1206         if (!NT_STATUS_IS_OK(status)) {
1207                 smbd_server_connection_terminate(conn, nt_errstr(status));
1208                 return;
1209         }
1210
1211         status = smbd_smb2_request_dispatch(req);
1212         if (!NT_STATUS_IS_OK(status)) {
1213                 smbd_server_connection_terminate(conn, nt_errstr(status));
1214                 return;
1215         }
1216
1217         /* ask for the next request */
1218         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
1219         if (subreq == NULL) {
1220                 smbd_server_connection_terminate(conn, "no memory for reading");
1221                 return;
1222         }
1223         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
1224 }
1225
1226 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
1227 {
1228         struct smbd_server_connection *conn = tevent_req_callback_data(subreq,
1229                                               struct smbd_server_connection);
1230         NTSTATUS status;
1231         struct smbd_smb2_request *req;
1232
1233         status = smbd_smb2_request_read_recv(subreq, conn, &req);
1234         TALLOC_FREE(subreq);
1235         if (!NT_STATUS_IS_OK(status)) {
1236                 smbd_server_connection_terminate(conn, nt_errstr(status));
1237                 return;
1238         }
1239
1240         if (req->in.nbt_hdr[0] != 0x00) {
1241                 DEBUG(1,("smbd_smb2_request_incoming: ignore NBT[0x%02X] msg\n",
1242                          req->in.nbt_hdr[0]));
1243                 talloc_free(req->mem_pool);
1244                 req = NULL;
1245                 goto next;
1246         }
1247
1248         req->current_idx = 1;
1249
1250         DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
1251                  req->current_idx, req->in.vector_count));
1252
1253         status = smbd_smb2_request_validate(req);
1254         if (!NT_STATUS_IS_OK(status)) {
1255                 smbd_server_connection_terminate(conn, nt_errstr(status));
1256                 return;
1257         }
1258
1259         status = smbd_smb2_request_setup_out(req);
1260         if (!NT_STATUS_IS_OK(status)) {
1261                 smbd_server_connection_terminate(conn, nt_errstr(status));
1262                 return;
1263         }
1264
1265         status = smbd_smb2_request_dispatch(req);
1266         if (!NT_STATUS_IS_OK(status)) {
1267                 smbd_server_connection_terminate(conn, nt_errstr(status));
1268                 return;
1269         }
1270
1271 next:
1272         /* ask for the next request (this constructs the main loop) */
1273         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
1274         if (subreq == NULL) {
1275                 smbd_server_connection_terminate(conn, "no memory for reading");
1276                 return;
1277         }
1278         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
1279 }