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