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