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