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