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