s3:smbd: only check the next_status for related 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                 uint32_t in_flags;
281                 uint8_t *outhdr = NULL;
282                 uint8_t *outbody = NULL;
283                 uint32_t next_command_ofs = 0;
284                 struct iovec *current = &vector[idx];
285
286                 if ((idx + 3) < count) {
287                         /* we have a next command */
288                         next_command_ofs = SMB2_HDR_BODY + 8;
289                 }
290
291                 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
292                 in_flags = IVAL(inhdr, SMB2_HDR_FLAGS);
293
294                 outhdr = talloc_array(vector, uint8_t,
295                                       SMB2_HDR_BODY + 8);
296                 if (outhdr == NULL) {
297                         return NT_STATUS_NO_MEMORY;
298                 }
299
300                 outbody = outhdr + SMB2_HDR_BODY;
301
302                 current[0].iov_base     = (void *)outhdr;
303                 current[0].iov_len      = SMB2_HDR_BODY;
304
305                 current[1].iov_base     = (void *)outbody;
306                 current[1].iov_len      = 8;
307
308                 current[2].iov_base     = NULL;
309                 current[2].iov_len      = 0;
310
311                 /* setup the SMB2 header */
312                 SIVAL(outhdr, SMB2_HDR_PROTOCOL_ID,     SMB2_MAGIC);
313                 SSVAL(outhdr, SMB2_HDR_LENGTH,          SMB2_HDR_BODY);
314                 SSVAL(outhdr, SMB2_HDR_EPOCH,           0);
315                 SIVAL(outhdr, SMB2_HDR_STATUS,
316                       NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));
317                 SSVAL(outhdr, SMB2_HDR_OPCODE,
318                       SVAL(inhdr, SMB2_HDR_OPCODE));
319                 /* Make up a number for now... JRA. FIXME ! FIXME !*/
320                 SSVAL(outhdr, SMB2_HDR_CREDIT,          20);
321                 SIVAL(outhdr, SMB2_HDR_FLAGS,
322                       IVAL(inhdr, SMB2_HDR_FLAGS) | SMB2_HDR_FLAG_REDIRECT);
323                 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND,    next_command_ofs);
324                 SBVAL(outhdr, SMB2_HDR_MESSAGE_ID,
325                       BVAL(inhdr, SMB2_HDR_MESSAGE_ID));
326                 SIVAL(outhdr, SMB2_HDR_PID,
327                       IVAL(inhdr, SMB2_HDR_PID));
328                 SIVAL(outhdr, SMB2_HDR_TID,
329                       IVAL(inhdr, SMB2_HDR_TID));
330                 SBVAL(outhdr, SMB2_HDR_SESSION_ID,
331                       BVAL(inhdr, SMB2_HDR_SESSION_ID));
332                 memset(outhdr + SMB2_HDR_SIGNATURE, 0, 16);
333
334                 /* setup error body header */
335                 SSVAL(outbody, 0x00, 0x08 + 1);
336                 SSVAL(outbody, 0x02, 0);
337                 SIVAL(outbody, 0x04, 0);
338         }
339
340         req->out.vector = vector;
341         req->out.vector_count = count;
342
343         /* setup the length of the NBT packet */
344         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
345
346         return NT_STATUS_OK;
347 }
348
349 void smbd_server_connection_terminate_ex(struct smbd_server_connection *sconn,
350                                          const char *reason,
351                                          const char *location)
352 {
353         DEBUG(10,("smbd_server_connection_terminate_ex: reason[%s] at %s\n",
354                   reason, location));
355         exit_server_cleanly(reason);
356 }
357
358 static NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
359 {
360         const uint8_t *inhdr;
361         int i = req->current_idx;
362         uint16_t opcode;
363         uint32_t flags;
364         NTSTATUS status;
365         NTSTATUS session_status;
366
367         inhdr = (const uint8_t *)req->in.vector[i].iov_base;
368
369         /* TODO: verify more things */
370
371         flags = IVAL(inhdr, SMB2_HDR_FLAGS);
372         opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
373         DEBUG(10,("smbd_smb2_request_dispatch: opcode[%u]\n", opcode));
374
375 #define TMP_SMB2_ALLOWED_FLAGS ( \
376         SMB2_HDR_FLAG_CHAINED | \
377         SMB2_HDR_FLAG_SIGNED | \
378         SMB2_HDR_FLAG_DFS)
379         if ((flags & ~TMP_SMB2_ALLOWED_FLAGS) != 0) {
380                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
381         }
382 #undef TMP_SMB2_ALLOWED_FLAGS
383
384         session_status = smbd_smb2_request_check_session(req);
385
386         req->do_signing = false;
387         if (flags & SMB2_HDR_FLAG_SIGNED) {
388                 if (!NT_STATUS_IS_OK(session_status)) {
389                         return smbd_smb2_request_error(req, session_status);
390                 }
391
392                 req->do_signing = true;
393                 status = smb2_signing_check_pdu(req->session->session_key,
394                                                 &req->in.vector[i], 3);
395                 if (!NT_STATUS_IS_OK(status)) {
396                         return smbd_smb2_request_error(req, status);
397                 }
398         } else if (req->session && req->session->do_signing) {
399                 return smbd_smb2_request_error(req, NT_STATUS_ACCESS_DENIED);
400         }
401
402         if (flags & SMB2_HDR_FLAG_CHAINED) {
403                 /*
404                  * This check is mostly for giving the correct error code
405                  * for compounded requests.
406                  *
407                  * TODO: we may need to move this after the session
408                  *       and tcon checks.
409                  */
410                 if (!NT_STATUS_IS_OK(req->next_status)) {
411                         return smbd_smb2_request_error(req, req->next_status);
412                 }
413         } else {
414                 req->compat_chain_fsp = NULL;
415         }
416
417         switch (opcode) {
418         case SMB2_OP_NEGPROT:
419                 return smbd_smb2_request_process_negprot(req);
420
421         case SMB2_OP_SESSSETUP:
422                 return smbd_smb2_request_process_sesssetup(req);
423
424         case SMB2_OP_LOGOFF:
425                 if (!NT_STATUS_IS_OK(session_status)) {
426                         return smbd_smb2_request_error(req, session_status);
427                 }
428                 return smbd_smb2_request_process_logoff(req);
429
430         case SMB2_OP_TCON:
431                 if (!NT_STATUS_IS_OK(session_status)) {
432                         return smbd_smb2_request_error(req, session_status);
433                 }
434                 status = smbd_smb2_request_check_session(req);
435                 if (!NT_STATUS_IS_OK(status)) {
436                         return smbd_smb2_request_error(req, status);
437                 }
438                 return smbd_smb2_request_process_tcon(req);
439
440         case SMB2_OP_TDIS:
441                 if (!NT_STATUS_IS_OK(session_status)) {
442                         return smbd_smb2_request_error(req, session_status);
443                 }
444                 status = smbd_smb2_request_check_tcon(req);
445                 if (!NT_STATUS_IS_OK(status)) {
446                         return smbd_smb2_request_error(req, status);
447                 }
448                 return smbd_smb2_request_process_tdis(req);
449
450         case SMB2_OP_CREATE:
451                 if (!NT_STATUS_IS_OK(session_status)) {
452                         return smbd_smb2_request_error(req, session_status);
453                 }
454                 status = smbd_smb2_request_check_tcon(req);
455                 if (!NT_STATUS_IS_OK(status)) {
456                         return smbd_smb2_request_error(req, status);
457                 }
458                 return smbd_smb2_request_process_create(req);
459
460         case SMB2_OP_CLOSE:
461                 if (!NT_STATUS_IS_OK(session_status)) {
462                         return smbd_smb2_request_error(req, session_status);
463                 }
464                 status = smbd_smb2_request_check_tcon(req);
465                 if (!NT_STATUS_IS_OK(status)) {
466                         return smbd_smb2_request_error(req, status);
467                 }
468                 return smbd_smb2_request_process_close(req);
469
470         case SMB2_OP_FLUSH:
471                 if (!NT_STATUS_IS_OK(session_status)) {
472                         return smbd_smb2_request_error(req, session_status);
473                 }
474                 status = smbd_smb2_request_check_tcon(req);
475                 if (!NT_STATUS_IS_OK(status)) {
476                         return smbd_smb2_request_error(req, status);
477                 }
478                 return smbd_smb2_request_process_flush(req);
479
480         case SMB2_OP_READ:
481                 if (!NT_STATUS_IS_OK(session_status)) {
482                         return smbd_smb2_request_error(req, session_status);
483                 }
484                 status = smbd_smb2_request_check_tcon(req);
485                 if (!NT_STATUS_IS_OK(status)) {
486                         return smbd_smb2_request_error(req, status);
487                 }
488                 return smbd_smb2_request_process_read(req);
489
490         case SMB2_OP_WRITE:
491                 if (!NT_STATUS_IS_OK(session_status)) {
492                         return smbd_smb2_request_error(req, session_status);
493                 }
494                 status = smbd_smb2_request_check_tcon(req);
495                 if (!NT_STATUS_IS_OK(status)) {
496                         return smbd_smb2_request_error(req, status);
497                 }
498                 return smbd_smb2_request_process_write(req);
499
500         case SMB2_OP_LOCK:
501                 if (!NT_STATUS_IS_OK(session_status)) {
502                         return smbd_smb2_request_error(req, session_status);
503                 }
504                 status = smbd_smb2_request_check_tcon(req);
505                 if (!NT_STATUS_IS_OK(status)) {
506                         return smbd_smb2_request_error(req, status);
507                 }
508                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
509
510         case SMB2_OP_IOCTL:
511                 if (!NT_STATUS_IS_OK(session_status)) {
512                         return smbd_smb2_request_error(req, session_status);
513                 }
514                 status = smbd_smb2_request_check_tcon(req);
515                 if (!NT_STATUS_IS_OK(status)) {
516                         return smbd_smb2_request_error(req, status);
517                 }
518                 return smbd_smb2_request_process_ioctl(req);
519
520         case SMB2_OP_CANCEL:
521                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
522
523         case SMB2_OP_KEEPALIVE:
524                 return smbd_smb2_request_process_keepalive(req);
525
526         case SMB2_OP_FIND:
527                 if (!NT_STATUS_IS_OK(session_status)) {
528                         return smbd_smb2_request_error(req, session_status);
529                 }
530                 status = smbd_smb2_request_check_tcon(req);
531                 if (!NT_STATUS_IS_OK(status)) {
532                         return smbd_smb2_request_error(req, status);
533                 }
534                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
535
536         case SMB2_OP_NOTIFY:
537                 if (!NT_STATUS_IS_OK(session_status)) {
538                         return smbd_smb2_request_error(req, session_status);
539                 }
540                 status = smbd_smb2_request_check_tcon(req);
541                 if (!NT_STATUS_IS_OK(status)) {
542                         return smbd_smb2_request_error(req, status);
543                 }
544                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
545
546         case SMB2_OP_GETINFO:
547                 if (!NT_STATUS_IS_OK(session_status)) {
548                         return smbd_smb2_request_error(req, session_status);
549                 }
550                 status = smbd_smb2_request_check_tcon(req);
551                 if (!NT_STATUS_IS_OK(status)) {
552                         return smbd_smb2_request_error(req, status);
553                 }
554                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
555
556         case SMB2_OP_SETINFO:
557                 if (!NT_STATUS_IS_OK(session_status)) {
558                         return smbd_smb2_request_error(req, session_status);
559                 }
560                 status = smbd_smb2_request_check_tcon(req);
561                 if (!NT_STATUS_IS_OK(status)) {
562                         return smbd_smb2_request_error(req, status);
563                 }
564                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
565
566         case SMB2_OP_BREAK:
567                 if (!NT_STATUS_IS_OK(session_status)) {
568                         return smbd_smb2_request_error(req, session_status);
569                 }
570                 status = smbd_smb2_request_check_tcon(req);
571                 if (!NT_STATUS_IS_OK(status)) {
572                         return smbd_smb2_request_error(req, status);
573                 }
574                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
575         }
576
577         return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
578 }
579
580 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq);
581 static void smbd_smb2_request_writev_done(struct tevent_req *subreq);
582
583 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
584 {
585         struct tevent_req *subreq;
586
587         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
588
589         if (req->do_signing) {
590                 int i = req->current_idx;
591                 NTSTATUS status;
592                 status = smb2_signing_sign_pdu(req->session->session_key,
593                                                &req->out.vector[i], 3);
594                 if (!NT_STATUS_IS_OK(status)) {
595                         return status;
596                 }
597         }
598
599         req->current_idx += 3;
600
601         if (req->current_idx < req->out.vector_count) {
602                 struct timeval zero = timeval_zero();
603                 subreq = tevent_wakeup_send(req,
604                                             req->conn->smb2.event_ctx,
605                                             zero);
606                 if (subreq == NULL) {
607                         return NT_STATUS_NO_MEMORY;
608                 }
609                 tevent_req_set_callback(subreq,
610                                         smbd_smb2_request_dispatch_compound,
611                                         req);
612
613                 return NT_STATUS_OK;
614         }
615
616         subreq = tstream_writev_queue_send(req,
617                                            req->conn->smb2.event_ctx,
618                                            req->conn->smb2.stream,
619                                            req->conn->smb2.send_queue,
620                                            req->out.vector,
621                                            req->out.vector_count);
622         if (subreq == NULL) {
623                 return NT_STATUS_NO_MEMORY;
624         }
625         tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
626
627         return NT_STATUS_OK;
628 }
629
630 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq)
631 {
632         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
633                                         struct smbd_smb2_request);
634         struct smbd_server_connection *conn = req->conn;
635         NTSTATUS status;
636
637         tevent_wakeup_recv(subreq);
638         TALLOC_FREE(subreq);
639
640         DEBUG(10,("smbd_smb2_request_dispatch_compound: idx[%d] of %d vectors\n",
641                   req->current_idx, req->in.vector_count));
642
643         status = smbd_smb2_request_dispatch(req);
644         if (!NT_STATUS_IS_OK(status)) {
645                 smbd_server_connection_terminate(conn, nt_errstr(status));
646                 return;
647         }
648 }
649
650 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
651 {
652         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
653                                         struct smbd_smb2_request);
654         struct smbd_server_connection *conn = req->conn;
655         int ret;
656         int sys_errno;
657         TALLOC_CTX *mem_pool;
658
659         ret = tstream_writev_queue_recv(subreq, &sys_errno);
660         TALLOC_FREE(subreq);
661         if (ret == -1) {
662                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
663                 smbd_server_connection_terminate(conn, nt_errstr(status));
664                 return;
665         }
666
667         mem_pool = req->mem_pool;
668         req = NULL;
669         talloc_free(mem_pool);
670 }
671
672 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
673                                     NTSTATUS status,
674                                     DATA_BLOB *info,
675                                     const char *location)
676 {
677         uint8_t *outhdr;
678         uint8_t *outbody;
679         int i = req->current_idx;
680
681         DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s] |%s| at %s\n",
682                   i, nt_errstr(status), info ? " +info" : "",
683                   location));
684
685         outhdr = (uint8_t *)req->out.vector[i].iov_base;
686
687         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
688
689         outbody = outhdr + SMB2_HDR_BODY;
690
691         req->out.vector[i+1].iov_base = (void *)outbody;
692         req->out.vector[i+1].iov_len = 8;
693
694         if (info) {
695                 SIVAL(outbody, 0x04, info->length);
696                 req->out.vector[i+2].iov_base   = (void *)info->data;
697                 req->out.vector[i+2].iov_len    = info->length;
698         } else {
699                 req->out.vector[i+2].iov_base = NULL;
700                 req->out.vector[i+2].iov_len = 0;
701         }
702
703         /*
704          * if a request fails, all other remaining
705          * compounded requests should fail too
706          */
707         req->next_status = NT_STATUS_INVALID_PARAMETER;
708
709         return smbd_smb2_request_reply(req);
710 }
711
712 NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
713                                    NTSTATUS status,
714                                    DATA_BLOB body, DATA_BLOB *dyn,
715                                    const char *location)
716 {
717         uint8_t *outhdr;
718         uint8_t *outdyn;
719         int i = req->current_idx;
720         uint32_t next_command_ofs;
721
722         DEBUG(10,("smbd_smb2_request_done_ex: "
723                   "idx[%d] status[%s] body[%u] dyn[%s:%u] at %s\n",
724                   i, nt_errstr(status), (unsigned int)body.length,
725                   dyn ? "yes": "no",
726                   (unsigned int)(dyn ? dyn->length : 0),
727                   location));
728
729         if (body.length < 2) {
730                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
731         }
732
733         if ((body.length % 2) != 0) {
734                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
735         }
736
737         outhdr = (uint8_t *)req->out.vector[i].iov_base;
738         /* the fallback dynamic buffer */
739         outdyn = outhdr + SMB2_HDR_BODY + 8;
740
741         next_command_ofs = IVAL(outhdr, SMB2_HDR_NEXT_COMMAND);
742         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
743
744         req->out.vector[i+1].iov_base = (void *)body.data;
745         req->out.vector[i+1].iov_len = body.length;
746
747         if (dyn) {
748                 req->out.vector[i+2].iov_base   = (void *)dyn->data;
749                 req->out.vector[i+2].iov_len    = dyn->length;
750         } else {
751                 req->out.vector[i+2].iov_base = NULL;
752                 req->out.vector[i+2].iov_len = 0;
753         }
754
755         /* see if we need to recalculate the offset to the next response */
756         if (next_command_ofs > 0) {
757                 next_command_ofs  = SMB2_HDR_BODY;
758                 next_command_ofs += req->out.vector[i+1].iov_len;
759                 next_command_ofs += req->out.vector[i+2].iov_len;
760         }
761
762         if ((next_command_ofs % 8) != 0) {
763                 size_t pad_size = 8 - (next_command_ofs % 8);
764                 if (req->out.vector[i+2].iov_len == 0) {
765                         /*
766                          * if the dyn buffer is empty
767                          * we can use it to add padding
768                          */
769                         uint8_t *pad;
770
771                         pad = talloc_zero_array(req->out.vector,
772                                                 uint8_t, pad_size);
773                         if (pad == NULL) {
774                                 return smbd_smb2_request_error(req,
775                                                 NT_STATUS_NO_MEMORY);
776                         }
777
778                         req->out.vector[i+2].iov_base = (void *)pad;
779                         req->out.vector[i+2].iov_len = pad_size;
780                 } else {
781                         /*
782                          * For now we copy the dynamic buffer
783                          * and add the padding to the new buffer
784                          */
785                         size_t old_size;
786                         uint8_t *old_dyn;
787                         size_t new_size;
788                         uint8_t *new_dyn;
789
790                         old_size = req->out.vector[i+2].iov_len;
791                         old_dyn = (uint8_t *)req->out.vector[i+2].iov_base;
792
793                         new_size = old_size + pad_size;
794                         new_dyn = talloc_array(req->out.vector,
795                                                uint8_t, new_size);
796                         if (new_dyn == NULL) {
797                                 return smbd_smb2_request_error(req,
798                                                 NT_STATUS_NO_MEMORY);
799                         }
800
801                         memcpy(new_dyn, old_dyn, old_size);
802                         memset(new_dyn + old_size, 0, pad_size);
803
804                         req->out.vector[i+2].iov_base = (void *)new_dyn;
805                         req->out.vector[i+2].iov_len = new_size;
806
807                         TALLOC_FREE(old_dyn);
808                 }
809                 next_command_ofs += pad_size;
810         }
811
812         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
813
814         return smbd_smb2_request_reply(req);
815 }
816
817 struct smbd_smb2_request_read_state {
818         size_t missing;
819         bool asked_for_header;
820         struct smbd_smb2_request *smb2_req;
821 };
822
823 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
824                                          void *private_data,
825                                          TALLOC_CTX *mem_ctx,
826                                          struct iovec **_vector,
827                                          size_t *_count);
828 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
829
830 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
831                                         struct tevent_context *ev,
832                                         struct smbd_server_connection *conn)
833 {
834         struct tevent_req *req;
835         struct smbd_smb2_request_read_state *state;
836         struct tevent_req *subreq;
837         TALLOC_CTX *mem_pool;
838
839         req = tevent_req_create(mem_ctx, &state,
840                                 struct smbd_smb2_request_read_state);
841         if (req == NULL) {
842                 return NULL;
843         }
844         state->missing = 0;
845         state->asked_for_header = false;
846
847         mem_pool = talloc_pool(state, 8192);
848         if (tevent_req_nomem(mem_pool, req)) {
849                 return tevent_req_post(req, ev);
850         }
851
852         state->smb2_req = talloc_zero(mem_pool, struct smbd_smb2_request);
853         if (tevent_req_nomem(state->smb2_req, req)) {
854                 return tevent_req_post(req, ev);
855         }
856
857         state->smb2_req->mem_pool       = mem_pool;
858         state->smb2_req->conn           = conn;
859
860         subreq = tstream_readv_pdu_queue_send(state, ev, conn->smb2.stream,
861                                               conn->smb2.recv_queue,
862                                               smbd_smb2_request_next_vector,
863                                               state);
864         if (tevent_req_nomem(subreq, req)) {
865                 return tevent_req_post(req, ev);
866         }
867         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
868
869         return req;
870 }
871
872 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
873                                          void *private_data,
874                                          TALLOC_CTX *mem_ctx,
875                                          struct iovec **_vector,
876                                          size_t *_count)
877 {
878         struct smbd_smb2_request_read_state *state =
879                 talloc_get_type_abort(private_data,
880                 struct smbd_smb2_request_read_state);
881         struct smbd_smb2_request *req = state->smb2_req;
882         struct iovec *vector;
883         int idx = req->in.vector_count;
884         size_t len = 0;
885         uint8_t *buf = NULL;
886
887         if (req->in.vector_count == 0) {
888                 /*
889                  * first we need to get the NBT header
890                  */
891                 req->in.vector = talloc_array(req, struct iovec,
892                                               req->in.vector_count + 1);
893                 if (req->in.vector == NULL) {
894                         return -1;
895                 }
896                 req->in.vector_count += 1;
897
898                 req->in.vector[idx].iov_base    = (void *)req->in.nbt_hdr;
899                 req->in.vector[idx].iov_len     = 4;
900
901                 vector = talloc_array(mem_ctx, struct iovec, 1);
902                 if (vector == NULL) {
903                         return -1;
904                 }
905
906                 vector[0] = req->in.vector[idx];
907
908                 *_vector = vector;
909                 *_count = 1;
910                 return 0;
911         }
912
913         if (req->in.vector_count == 1) {
914                 /*
915                  * Now we analyze the NBT header
916                  */
917                 state->missing = smb2_len(req->in.vector[0].iov_base);
918
919                 if (state->missing == 0) {
920                         /* if there're no remaining bytes, we're done */
921                         *_vector = NULL;
922                         *_count = 0;
923                         return 0;
924                 }
925
926                 req->in.vector = talloc_realloc(req, req->in.vector,
927                                                 struct iovec,
928                                                 req->in.vector_count + 1);
929                 if (req->in.vector == NULL) {
930                         return -1;
931                 }
932                 req->in.vector_count += 1;
933
934                 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
935                         /*
936                          * it's a special NBT message,
937                          * so get all remaining bytes
938                          */
939                         len = state->missing;
940                 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
941                         /*
942                          * it's an invalid message, just read what we can get
943                          * and let the caller handle the error
944                          */
945                         len = state->missing;
946                 } else {
947                         /*
948                          * We assume it's a SMB2 request,
949                          * and we first get the header and the
950                          * first 2 bytes (the struct size) of the body
951                          */
952                         len = SMB2_HDR_BODY + 2;
953
954                         state->asked_for_header = true;
955                 }
956
957                 state->missing -= len;
958
959                 buf = talloc_array(req->in.vector, uint8_t, len);
960                 if (buf == NULL) {
961                         return -1;
962                 }
963
964                 req->in.vector[idx].iov_base    = (void *)buf;
965                 req->in.vector[idx].iov_len     = len;
966
967                 vector = talloc_array(mem_ctx, struct iovec, 1);
968                 if (vector == NULL) {
969                         return -1;
970                 }
971
972                 vector[0] = req->in.vector[idx];
973
974                 *_vector = vector;
975                 *_count = 1;
976                 return 0;
977         }
978
979         if (state->missing == 0) {
980                 /* if there're no remaining bytes, we're done */
981                 *_vector = NULL;
982                 *_count = 0;
983                 return 0;
984         }
985
986         if (state->asked_for_header) {
987                 const uint8_t *hdr;
988                 size_t full_size;
989                 size_t next_command_ofs;
990                 size_t body_size;
991                 uint8_t *body;
992                 size_t dyn_size;
993                 uint8_t *dyn;
994                 bool invalid = false;
995
996                 state->asked_for_header = false;
997
998                 /*
999                  * We got the SMB2 header and the first 2 bytes
1000                  * of the body. We fix the size to just the header
1001                  * and manually copy the 2 first bytes to the body section
1002                  */
1003                 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
1004                 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
1005
1006                 /* allocate vectors for body and dynamic areas */
1007                 req->in.vector = talloc_realloc(req, req->in.vector,
1008                                                 struct iovec,
1009                                                 req->in.vector_count + 2);
1010                 if (req->in.vector == NULL) {
1011                         return -1;
1012                 }
1013                 req->in.vector_count += 2;
1014
1015                 full_size = state->missing + SMB2_HDR_BODY + 2;
1016                 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
1017                 body_size = SVAL(hdr, SMB2_HDR_BODY);
1018
1019                 if (next_command_ofs != 0) {
1020                         if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
1021                                 /*
1022                                  * this is invalid, just return a zero
1023                                  * body and let the caller deal with the error
1024                                  */
1025                                 invalid = true;
1026                         } else if (next_command_ofs > full_size) {
1027                                 /*
1028                                  * this is invalid, just return a zero
1029                                  * body and let the caller deal with the error
1030                                  */
1031                                 invalid = true;
1032                         } else {
1033                                 full_size = next_command_ofs;
1034                         }
1035                 }
1036
1037                 if (!invalid) {
1038                         if (body_size < 2) {
1039                                 /*
1040                                  * this is invalid, just return a zero
1041                                  * body and let the caller deal with the error
1042                                  */
1043                                 invalid = true;
1044                         } else if (body_size > (full_size - SMB2_HDR_BODY)) {
1045                                 /*
1046                                  * this is invalid, just return a zero
1047                                  * body and let the caller deal with the error
1048                                  */
1049                                 invalid = true;
1050                         }
1051                 }
1052
1053                 if (invalid) {
1054                         /* the caller should check this */
1055                         body_size = 0;
1056                 }
1057
1058                 if ((body_size % 2) != 0) {
1059                         body_size -= 1;
1060                 }
1061
1062                 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
1063
1064                 state->missing -= (body_size - 2) + dyn_size;
1065
1066                 body = talloc_array(req->in.vector, uint8_t, body_size);
1067                 if (body == NULL) {
1068                         return -1;
1069                 }
1070
1071                 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
1072                 if (dyn == NULL) {
1073                         return -1;
1074                 }
1075
1076                 req->in.vector[idx].iov_base    = (void *)body;
1077                 req->in.vector[idx].iov_len     = body_size;
1078                 req->in.vector[idx+1].iov_base  = (void *)dyn;
1079                 req->in.vector[idx+1].iov_len   = dyn_size;
1080
1081                 vector = talloc_array(mem_ctx, struct iovec, 2);
1082                 if (vector == NULL) {
1083                         return -1;
1084                 }
1085
1086                 /*
1087                  * the first 2 bytes of the body were already fetched
1088                  * together with the header
1089                  */
1090                 memcpy(body, hdr + SMB2_HDR_BODY, 2);
1091                 vector[0].iov_base = body + 2;
1092                 vector[0].iov_len = req->in.vector[idx].iov_len - 2;
1093
1094                 vector[1] = req->in.vector[idx+1];
1095
1096                 *_vector = vector;
1097                 *_count = 2;
1098                 return 0;
1099         }
1100
1101         /*
1102          * when we endup here, we're looking for a new SMB2 request
1103          * next. And we ask for its header and the first 2 bytes of
1104          * the body (like we did for the first SMB2 request).
1105          */
1106
1107         req->in.vector = talloc_realloc(req, req->in.vector,
1108                                         struct iovec,
1109                                         req->in.vector_count + 1);
1110         if (req->in.vector == NULL) {
1111                 return -1;
1112         }
1113         req->in.vector_count += 1;
1114
1115         /*
1116          * We assume it's a SMB2 request,
1117          * and we first get the header and the
1118          * first 2 bytes (the struct size) of the body
1119          */
1120         len = SMB2_HDR_BODY + 2;
1121
1122         if (len > state->missing) {
1123                 /* let the caller handle the error */
1124                 len = state->missing;
1125         }
1126
1127         state->missing -= len;
1128         state->asked_for_header = true;
1129
1130         buf = talloc_array(req->in.vector, uint8_t, len);
1131         if (buf == NULL) {
1132                 return -1;
1133         }
1134
1135         req->in.vector[idx].iov_base    = (void *)buf;
1136         req->in.vector[idx].iov_len     = len;
1137
1138         vector = talloc_array(mem_ctx, struct iovec, 1);
1139         if (vector == NULL) {
1140                 return -1;
1141         }
1142
1143         vector[0] = req->in.vector[idx];
1144
1145         *_vector = vector;
1146         *_count = 1;
1147         return 0;
1148 }
1149
1150 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
1151 {
1152         struct tevent_req *req =
1153                 tevent_req_callback_data(subreq,
1154                 struct tevent_req);
1155         int ret;
1156         int sys_errno;
1157         NTSTATUS status;
1158
1159         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1160         if (ret == -1) {
1161                 status = map_nt_error_from_unix(sys_errno);
1162                 tevent_req_nterror(req, status);
1163                 return;
1164         }
1165
1166         tevent_req_done(req);
1167 }
1168
1169 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
1170                                             TALLOC_CTX *mem_ctx,
1171                                             struct smbd_smb2_request **_smb2_req)
1172 {
1173         struct smbd_smb2_request_read_state *state =
1174                 tevent_req_data(req,
1175                 struct smbd_smb2_request_read_state);
1176         NTSTATUS status;
1177
1178         if (tevent_req_is_nterror(req, &status)) {
1179                 tevent_req_received(req);
1180                 return status;
1181         }
1182
1183         talloc_steal(mem_ctx, state->smb2_req->mem_pool);
1184         *_smb2_req = state->smb2_req;
1185         tevent_req_received(req);
1186         return NT_STATUS_OK;
1187 }
1188
1189 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
1190
1191 void smbd_smb2_first_negprot(struct smbd_server_connection *conn,
1192                              const uint8_t *inbuf, size_t size)
1193 {
1194         NTSTATUS status;
1195         struct smbd_smb2_request *req;
1196         struct tevent_req *subreq;
1197
1198         DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
1199                  (unsigned int)size));
1200
1201         status = smbd_initialize_smb2(conn);
1202         if (!NT_STATUS_IS_OK(status)) {
1203                 smbd_server_connection_terminate(conn, nt_errstr(status));
1204                 return;
1205         }
1206
1207         status = smbd_smb2_request_create(conn, inbuf, size, &req);
1208         if (!NT_STATUS_IS_OK(status)) {
1209                 smbd_server_connection_terminate(conn, nt_errstr(status));
1210                 return;
1211         }
1212
1213         status = smbd_smb2_request_setup_out(req);
1214         if (!NT_STATUS_IS_OK(status)) {
1215                 smbd_server_connection_terminate(conn, nt_errstr(status));
1216                 return;
1217         }
1218
1219         status = smbd_smb2_request_dispatch(req);
1220         if (!NT_STATUS_IS_OK(status)) {
1221                 smbd_server_connection_terminate(conn, nt_errstr(status));
1222                 return;
1223         }
1224
1225         /* ask for the next request */
1226         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
1227         if (subreq == NULL) {
1228                 smbd_server_connection_terminate(conn, "no memory for reading");
1229                 return;
1230         }
1231         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
1232 }
1233
1234 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
1235 {
1236         struct smbd_server_connection *conn = tevent_req_callback_data(subreq,
1237                                               struct smbd_server_connection);
1238         NTSTATUS status;
1239         struct smbd_smb2_request *req;
1240
1241         status = smbd_smb2_request_read_recv(subreq, conn, &req);
1242         TALLOC_FREE(subreq);
1243         if (!NT_STATUS_IS_OK(status)) {
1244                 smbd_server_connection_terminate(conn, nt_errstr(status));
1245                 return;
1246         }
1247
1248         if (req->in.nbt_hdr[0] != 0x00) {
1249                 DEBUG(1,("smbd_smb2_request_incoming: ignore NBT[0x%02X] msg\n",
1250                          req->in.nbt_hdr[0]));
1251                 talloc_free(req->mem_pool);
1252                 req = NULL;
1253                 goto next;
1254         }
1255
1256         req->current_idx = 1;
1257
1258         DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
1259                  req->current_idx, req->in.vector_count));
1260
1261         status = smbd_smb2_request_validate(req);
1262         if (!NT_STATUS_IS_OK(status)) {
1263                 smbd_server_connection_terminate(conn, nt_errstr(status));
1264                 return;
1265         }
1266
1267         status = smbd_smb2_request_setup_out(req);
1268         if (!NT_STATUS_IS_OK(status)) {
1269                 smbd_server_connection_terminate(conn, nt_errstr(status));
1270                 return;
1271         }
1272
1273         status = smbd_smb2_request_dispatch(req);
1274         if (!NT_STATUS_IS_OK(status)) {
1275                 smbd_server_connection_terminate(conn, nt_errstr(status));
1276                 return;
1277         }
1278
1279 next:
1280         /* ask for the next request (this constructs the main loop) */
1281         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
1282         if (subreq == NULL) {
1283                 smbd_server_connection_terminate(conn, "no memory for reading");
1284                 return;
1285         }
1286         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
1287 }