s3:smbd: add support for SMB2 Keepalive (SMB2 Echo)
[ira/wip.git] / source3 / smbd / smb2_server.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
24 #include "../lib/tsocket/tsocket.h"
25
26 bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size)
27 {
28         if (size < (4 + SMB2_HDR_BODY)) {
29                 return false;
30         }
31
32         if (IVAL(inbuf, 4) != SMB2_MAGIC) {
33                 return false;
34         }
35
36         return true;
37 }
38
39 static NTSTATUS smbd_initialize_smb2(struct smbd_server_connection *conn)
40 {
41         NTSTATUS status;
42         int ret;
43
44         TALLOC_FREE(conn->fde);
45
46         conn->smb2.event_ctx = smbd_event_context();
47
48         conn->smb2.recv_queue = tevent_queue_create(conn, "smb2 recv queue");
49         if (conn->smb2.recv_queue == NULL) {
50                 return NT_STATUS_NO_MEMORY;
51         }
52
53         conn->smb2.send_queue = tevent_queue_create(conn, "smb2 send queue");
54         if (conn->smb2.send_queue == NULL) {
55                 return NT_STATUS_NO_MEMORY;
56         }
57
58         ret = tstream_bsd_existing_socket(conn, smbd_server_fd(),
59                                           &conn->smb2.stream);
60         if (ret == -1) {
61                 status = map_nt_error_from_unix(errno);
62                 return status;
63         }
64
65         /* Ensure child is set to non-blocking mode */
66         set_blocking(smbd_server_fd(),false);
67         return NT_STATUS_OK;
68 }
69
70 #define smb2_len(buf) (PVAL(buf,3)|(PVAL(buf,2)<<8)|(PVAL(buf,1)<<16))
71 #define _smb2_setlen(_buf,len) do { \
72         uint8_t *buf = (uint8_t *)_buf; \
73         buf[0] = 0; \
74         buf[1] = ((len)&0xFF0000)>>16; \
75         buf[2] = ((len)&0xFF00)>>8; \
76         buf[3] = (len)&0xFF; \
77 } while (0)
78
79 static void smb2_setup_nbt_length(struct iovec *vector, int count)
80 {
81         size_t len = 0;
82         int i;
83
84         for (i=1; i < count; i++) {
85                 len += vector[i].iov_len;
86         }
87
88         _smb2_setlen(vector[0].iov_base, len);
89 }
90
91 static NTSTATUS smbd_smb2_request_create(struct smbd_server_connection *conn,
92                                          const uint8_t *inbuf, size_t size,
93                                          struct smbd_smb2_request **_req)
94 {
95         TALLOC_CTX *mem_pool;
96         struct smbd_smb2_request *req;
97         uint32_t protocol_version;
98         const uint8_t *inhdr = NULL;
99         off_t ofs = 0;
100         uint16_t cmd;
101         uint32_t next_command_ofs;
102
103         if (size < (4 + SMB2_HDR_BODY + 2)) {
104                 DEBUG(0,("Invalid SMB2 packet length count %ld\n", (long)size));
105                 return NT_STATUS_INVALID_PARAMETER;
106         }
107
108         inhdr = inbuf + 4;
109
110         protocol_version = IVAL(inhdr, SMB2_HDR_PROTOCOL_ID);
111         if (protocol_version != SMB2_MAGIC) {
112                 DEBUG(0,("Invalid SMB packet: protocol prefix: 0x%08X\n",
113                          protocol_version));
114                 return NT_STATUS_INVALID_PARAMETER;
115         }
116
117         cmd = SVAL(inhdr, SMB2_HDR_OPCODE);
118         if (cmd != SMB2_OP_NEGPROT) {
119                 DEBUG(0,("Invalid SMB packet: first request: 0x%04X\n",
120                          cmd));
121                 return NT_STATUS_INVALID_PARAMETER;
122         }
123
124         next_command_ofs = IVAL(inhdr, SMB2_HDR_NEXT_COMMAND);
125         if (next_command_ofs != 0) {
126                 DEBUG(0,("Invalid SMB packet: next_command: 0x%08X\n",
127                          next_command_ofs));
128                 return NT_STATUS_INVALID_PARAMETER;
129         }
130
131         mem_pool = talloc_pool(conn, 8192);
132         if (mem_pool == NULL) {
133                 return NT_STATUS_NO_MEMORY;
134         }
135
136         req = talloc_zero(mem_pool, struct smbd_smb2_request);
137         if (req == NULL) {
138                 talloc_free(mem_pool);
139                 return NT_STATUS_NO_MEMORY;
140         }
141         req->mem_pool   = mem_pool;
142         req->conn       = conn;
143
144         talloc_steal(req, inbuf);
145
146         req->in.vector = talloc_array(req, struct iovec, 4);
147         if (req->in.vector == NULL) {
148                 talloc_free(mem_pool);
149                 return NT_STATUS_NO_MEMORY;
150         }
151         req->in.vector_count = 4;
152
153         memcpy(req->in.nbt_hdr, inbuf, 4);
154
155         ofs = 0;
156         req->in.vector[0].iov_base      = (void *)req->in.nbt_hdr;
157         req->in.vector[0].iov_len       = 4;
158         ofs += req->in.vector[0].iov_len;
159
160         req->in.vector[1].iov_base      = (void *)(inbuf + ofs);
161         req->in.vector[1].iov_len       = SMB2_HDR_BODY;
162         ofs += req->in.vector[1].iov_len;
163
164         req->in.vector[2].iov_base      = (void *)(inbuf + ofs);
165         req->in.vector[2].iov_len       = SVAL(inbuf, ofs) & 0xFFFE;
166         ofs += req->in.vector[2].iov_len;
167
168         if (ofs > size) {
169                 return NT_STATUS_INVALID_PARAMETER;
170         }
171
172         req->in.vector[3].iov_base      = (void *)(inbuf + ofs);
173         req->in.vector[3].iov_len       = size - ofs;
174         ofs += req->in.vector[3].iov_len;
175
176         req->current_idx = 1;
177
178         *_req = req;
179         return NT_STATUS_OK;
180 }
181
182 static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req)
183 {
184         struct iovec *vector;
185         int count;
186         int idx;
187
188         count = req->in.vector_count;
189         vector = talloc_array(req, struct iovec, count);
190         if (vector == NULL) {
191                 return NT_STATUS_NO_MEMORY;
192         }
193
194         vector[0].iov_base      = req->out.nbt_hdr;
195         vector[0].iov_len       = 4;
196         SIVAL(req->out.nbt_hdr, 0, 0);
197
198         for (idx=1; idx < count; idx += 3) {
199                 const uint8_t *inhdr = NULL;
200                 uint8_t *outhdr = NULL;
201                 uint8_t *outbody = NULL;
202                 uint8_t *outdyn = NULL;
203                 size_t outdyn_size = 1;
204                 uint32_t next_command_ofs = 0;
205                 struct iovec *current = &vector[idx];
206
207                 if ((idx + 3) < count) {
208                         /* we have a next command */
209                         next_command_ofs = SMB2_HDR_BODY + 8 + 8;
210                         outdyn_size = 8;
211                 }
212
213                 inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
214
215                 outhdr = talloc_array(vector, uint8_t,
216                                       SMB2_HDR_BODY + 8 + outdyn_size);
217                 if (outhdr == NULL) {
218                         return NT_STATUS_NO_MEMORY;
219                 }
220
221                 outbody = outhdr + SMB2_HDR_BODY;
222                 outdyn = outbody + 8;
223
224                 current[0].iov_base     = (void *)outhdr;
225                 current[0].iov_len      = SMB2_HDR_BODY;
226
227                 current[1].iov_base     = (void *)outbody;
228                 current[1].iov_len      = 8;
229
230                 current[2].iov_base     = (void *)outdyn;
231                 current[2].iov_len      = outdyn_size;
232
233                 /* setup the SMB2 header */
234                 SIVAL(outhdr, SMB2_HDR_PROTOCOL_ID,     SMB2_MAGIC);
235                 SSVAL(outhdr, SMB2_HDR_LENGTH,          SMB2_HDR_BODY);
236                 SSVAL(outhdr, SMB2_HDR_EPOCH,           0);
237                 SIVAL(outhdr, SMB2_HDR_STATUS,
238                       NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));
239                 SSVAL(outhdr, SMB2_HDR_OPCODE,
240                       SVAL(inhdr, SMB2_HDR_OPCODE));
241                 SSVAL(outhdr, SMB2_HDR_CREDIT,          0);
242                 SIVAL(outhdr, SMB2_HDR_FLAGS,           SMB2_HDR_FLAG_REDIRECT);
243                 SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND,    next_command_ofs);
244                 SIVAL(outhdr, SMB2_HDR_MESSAGE_ID,
245                       IVAL(inhdr, SMB2_HDR_MESSAGE_ID));
246                 SIVAL(outhdr, SMB2_HDR_PID,
247                       IVAL(inhdr, SMB2_HDR_PID));
248                 SIVAL(outhdr, SMB2_HDR_TID,
249                       IVAL(inhdr, SMB2_HDR_TID));
250                 SIVAL(outhdr, SMB2_HDR_SESSION_ID,
251                       IVAL(inhdr, SMB2_HDR_SESSION_ID));
252                 memset(outhdr + SMB2_HDR_SIGNATURE, 0, 16);
253
254                 /* setup error body header */
255                 SSVAL(outbody, 0x00, 9);
256                 SSVAL(outbody, 0x02, 0);
257                 SIVAL(outbody, 0x04, 0);
258
259                 /* setup the dynamic part */
260                 SCVAL(outdyn, 0x00, 0);
261         }
262
263         req->out.vector = vector;
264         req->out.vector_count = count;
265
266         /* setup the length of the NBT packet */
267         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
268
269         return NT_STATUS_OK;
270 }
271
272 static void smbd_server_connection_terminate(struct smbd_server_connection *conn,
273                                              const char *reason)
274 {
275         DEBUG(10,("smbd_server_connection_terminate: reason[%s]\n", reason));
276         exit_server_cleanly(reason);
277 }
278
279 static NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
280 {
281         const uint8_t *inhdr;
282         int i = req->current_idx;
283         uint16_t opcode;
284
285         inhdr = (const uint8_t *)req->in.vector[i].iov_base;
286
287         /* TODO: verify more things */
288
289         opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
290         DEBUG(10,("smbd_smb2_request_dispatch: opcode[%u]\n", opcode));
291         switch (opcode) {
292         case SMB2_OP_NEGPROT:
293                 return smbd_smb2_request_process_negprot(req);
294
295         case SMB2_OP_SESSSETUP:
296                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
297
298         case SMB2_OP_LOGOFF:
299                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
300
301                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
302         case SMB2_OP_TCON:
303                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
304
305                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
306         case SMB2_OP_TDIS:
307                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
308
309         case SMB2_OP_CREATE:
310                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
311
312         case SMB2_OP_CLOSE:
313                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
314
315         case SMB2_OP_FLUSH:
316                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
317
318         case SMB2_OP_READ:
319                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
320
321         case SMB2_OP_WRITE:
322                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
323
324         case SMB2_OP_LOCK:
325                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
326
327         case SMB2_OP_IOCTL:
328                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
329
330         case SMB2_OP_CANCEL:
331                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
332
333         case SMB2_OP_KEEPALIVE:
334                 return smbd_smb2_request_process_keepalive(req);
335
336         case SMB2_OP_FIND:
337                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
338
339         case SMB2_OP_NOTIFY:
340                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
341
342         case SMB2_OP_GETINFO:
343                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
344
345         case SMB2_OP_SETINFO:
346                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
347
348         case SMB2_OP_BREAK:
349                 return smbd_smb2_request_error(req, NT_STATUS_NOT_IMPLEMENTED);
350         }
351
352         return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
353 }
354
355 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq);
356 static void smbd_smb2_request_writev_done(struct tevent_req *subreq);
357
358 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
359 {
360         struct tevent_req *subreq;
361
362         /* TODO: sign the response here */
363
364         smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
365
366         req->current_idx += 3;
367
368         if (req->current_idx > req->in.vector_count) {
369                 struct timeval zero = timeval_zero();
370                 subreq = tevent_wakeup_send(req,
371                                             req->conn->smb2.event_ctx,
372                                             zero);
373                 if (subreq == NULL) {
374                         return NT_STATUS_NO_MEMORY;
375                 }
376                 tevent_req_set_callback(subreq,
377                                         smbd_smb2_request_dispatch_compound,
378                                         req);
379
380                 return NT_STATUS_OK;
381         }
382
383         subreq = tstream_writev_queue_send(req,
384                                            req->conn->smb2.event_ctx,
385                                            req->conn->smb2.stream,
386                                            req->conn->smb2.send_queue,
387                                            req->out.vector,
388                                            req->out.vector_count);
389         if (subreq == NULL) {
390                 return NT_STATUS_NO_MEMORY;
391         }
392         tevent_req_set_callback(subreq, smbd_smb2_request_writev_done, req);
393
394         return NT_STATUS_OK;
395 }
396
397 NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
398                                     NTSTATUS status,
399                                     DATA_BLOB *info)
400 {
401         uint8_t *outhdr;
402         uint8_t *outbody;
403         int i = req->current_idx;
404
405         DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s]%s\n",
406                   i, nt_errstr(status), info ? " +info" : ""));
407
408         outhdr = (uint8_t *)req->out.vector[i].iov_base;
409
410         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(status));
411
412         outbody = outhdr + SMB2_HDR_BODY;
413
414         req->out.vector[i+1].iov_base = (void *)outbody;
415         req->out.vector[i+1].iov_len = 8;
416
417         if (info) {
418                 SIVAL(outbody, 0x04, info->length);
419                 req->out.vector[i+2].iov_base   = (void *)info->data;
420                 req->out.vector[i+2].iov_len    = info->length;
421         } else {
422                 req->out.vector[i+2].iov_base = (void *)(outbody + 8);
423                 req->out.vector[i+2].iov_len = 1;
424         }
425
426         /* the error packet is the last response in the chain */
427         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
428         req->out.vector_count = req->current_idx + 3;
429
430         return smbd_smb2_request_reply(req);
431 }
432
433 NTSTATUS smbd_smb2_request_error(struct smbd_smb2_request *req,
434                                  NTSTATUS status)
435 {
436         return smbd_smb2_request_error_ex(req, status, NULL);
437 }
438
439 NTSTATUS smbd_smb2_request_done(struct smbd_smb2_request *req,
440                                 DATA_BLOB body, DATA_BLOB *dyn)
441 {
442         uint8_t *outhdr;
443         uint8_t *outdyn;
444         int i = req->current_idx;
445         uint32_t next_command_ofs;
446
447         DEBUG(10,("smbd_smb2_request_done: idx[%d] body[%u] dyn[%s:%u]\n",
448                   i, (unsigned int)body.length,
449                   dyn ? "yes": "no",
450                   (unsigned int)(dyn ? dyn->length : 0)));
451
452         if (body.length < 2) {
453                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
454         }
455
456         if ((body.length % 2) != 0) {
457                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
458         }
459
460         outhdr = (uint8_t *)req->out.vector[i].iov_base;
461         /* the fallback dynamic buffer */
462         outdyn = outhdr + SMB2_HDR_BODY + 8;
463
464         next_command_ofs = SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
465         SIVAL(outhdr, SMB2_HDR_STATUS, NT_STATUS_V(NT_STATUS_OK));
466
467         req->out.vector[i+1].iov_base = (void *)body.data;
468         req->out.vector[i+1].iov_len = body.length;
469
470         if (dyn) {
471                 if (dyn->length > 0) {
472                         req->out.vector[i+2].iov_base   = (void *)dyn->data;
473                         req->out.vector[i+2].iov_len    = dyn->length;
474                 } else {
475                         req->out.vector[i+2].iov_base   = (void *)outdyn;
476                         req->out.vector[i+2].iov_len    = 1;
477                 }
478         } else {
479                 req->out.vector[i+2].iov_base = NULL;
480                 req->out.vector[i+2].iov_len = 0;
481         }
482
483         /* see if we need to recalculate the offset to the next response */
484         if (next_command_ofs > 0) {
485                 next_command_ofs  = SMB2_HDR_BODY;
486                 next_command_ofs += req->out.vector[i+1].iov_len;
487                 next_command_ofs += req->out.vector[i+2].iov_len;
488         }
489
490         /* TODO: we need to add padding ... */
491         if ((next_command_ofs % 8) != 0) {
492                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
493         }
494
495         /* the error packet is the last response in the chain */
496         SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
497
498         return smbd_smb2_request_reply(req);
499 }
500
501 static void smbd_smb2_request_dispatch_compound(struct tevent_req *subreq)
502 {
503         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
504                                         struct smbd_smb2_request);
505         struct smbd_server_connection *conn = req->conn;
506         NTSTATUS status;
507
508         tevent_wakeup_recv(subreq);
509         TALLOC_FREE(subreq);
510
511         DEBUG(10,("smbd_smb2_request_dispatch_compound: idx[%d] of %d vectors\n",
512                   req->current_idx, req->in.vector_count));
513
514         status = smbd_smb2_request_dispatch(req);
515         if (!NT_STATUS_IS_OK(status)) {
516                 smbd_server_connection_terminate(conn, nt_errstr(status));
517                 return;
518         }
519 }
520
521 static void smbd_smb2_request_writev_done(struct tevent_req *subreq)
522 {
523         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
524                                         struct smbd_smb2_request);
525         struct smbd_server_connection *conn = req->conn;
526         int ret;
527         int sys_errno;
528         TALLOC_CTX *mem_pool;
529
530         ret = tstream_writev_queue_recv(subreq, &sys_errno);
531         TALLOC_FREE(subreq);
532         if (ret == -1) {
533                 NTSTATUS status = map_nt_error_from_unix(sys_errno);
534                 smbd_server_connection_terminate(conn, nt_errstr(status));
535                 return;
536         }
537
538         mem_pool = req->mem_pool;
539         req = NULL;
540         talloc_free(mem_pool);
541 }
542
543 struct smbd_smb2_request_read_state {
544         size_t missing;
545         bool asked_for_header;
546         struct smbd_smb2_request *smb2_req;
547 };
548
549 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
550                                          void *private_data,
551                                          TALLOC_CTX *mem_ctx,
552                                          struct iovec **_vector,
553                                          size_t *_count);
554 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
555
556 static struct tevent_req *smbd_smb2_request_read_send(TALLOC_CTX *mem_ctx,
557                                         struct tevent_context *ev,
558                                         struct smbd_server_connection *conn)
559 {
560         struct tevent_req *req;
561         struct smbd_smb2_request_read_state *state;
562         struct tevent_req *subreq;
563         TALLOC_CTX *mem_pool;
564
565         req = tevent_req_create(mem_ctx, &state,
566                                 struct smbd_smb2_request_read_state);
567         if (req == NULL) {
568                 return NULL;
569         }
570         state->missing = 0;
571         state->asked_for_header = false;
572
573         mem_pool = talloc_pool(state, 8192);
574         if (tevent_req_nomem(mem_pool, req)) {
575                 return tevent_req_post(req, ev);
576         }
577
578         state->smb2_req = talloc_zero(mem_pool, struct smbd_smb2_request);
579         if (tevent_req_nomem(state->smb2_req, req)) {
580                 return tevent_req_post(req, ev);
581         }
582
583         state->smb2_req->mem_pool       = mem_pool;
584         state->smb2_req->conn           = conn;
585
586         subreq = tstream_readv_pdu_queue_send(state, ev, conn->smb2.stream,
587                                               conn->smb2.recv_queue,
588                                               smbd_smb2_request_next_vector,
589                                               state);
590         if (tevent_req_nomem(subreq, req)) {
591                 return tevent_req_post(req, ev);
592         }
593         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
594
595         return req;
596 }
597
598 static int smbd_smb2_request_next_vector(struct tstream_context *stream,
599                                          void *private_data,
600                                          TALLOC_CTX *mem_ctx,
601                                          struct iovec **_vector,
602                                          size_t *_count)
603 {
604         struct smbd_smb2_request_read_state *state =
605                 talloc_get_type_abort(private_data,
606                 struct smbd_smb2_request_read_state);
607         struct smbd_smb2_request *req = state->smb2_req;
608         struct iovec *vector;
609         int idx = req->in.vector_count;
610         size_t len = 0;
611         uint8_t *buf = NULL;
612
613         if (req->in.vector_count == 0) {
614                 /*
615                  * first we need to get the NBT header
616                  */
617                 req->in.vector = talloc_array(req, struct iovec,
618                                               req->in.vector_count + 1);
619                 if (req->in.vector == NULL) {
620                         return -1;
621                 }
622                 req->in.vector_count += 1;
623
624                 req->in.vector[idx].iov_base    = (void *)req->in.nbt_hdr;
625                 req->in.vector[idx].iov_len     = 4;
626
627                 vector = talloc_array(mem_ctx, struct iovec, 1);
628                 if (vector == NULL) {
629                         return -1;
630                 }
631
632                 vector[0] = req->in.vector[idx];
633
634                 *_vector = vector;
635                 *_count = 1;
636                 return 0;
637         }
638
639         if (req->in.vector_count == 1) {
640                 /*
641                  * Now we analyze the NBT header
642                  */
643                 state->missing = smb2_len(req->in.vector[0].iov_base);
644
645                 if (state->missing == 0) {
646                         /* if there're no remaining bytes, we're done */
647                         *_vector = NULL;
648                         *_count = 0;
649                         return 0;
650                 }
651
652                 req->in.vector = talloc_realloc(req, req->in.vector,
653                                                 struct iovec,
654                                                 req->in.vector_count + 1);
655                 if (req->in.vector == NULL) {
656                         return -1;
657                 }
658                 req->in.vector_count += 1;
659
660                 if (CVAL(req->in.vector[0].iov_base, 0) != 0) {
661                         /*
662                          * it's a special NBT message,
663                          * so get all remaining bytes
664                          */
665                         len = state->missing;
666                 } else if (state->missing < (SMB2_HDR_BODY + 2)) {
667                         /*
668                          * it's an invalid message, just read what we can get
669                          * and let the caller handle the error
670                          */
671                         len = state->missing;
672                 } else {
673                         /*
674                          * We assume it's a SMB2 request,
675                          * and we first get the header and the
676                          * first 2 bytes (the struct size) of the body
677                          */
678                         len = SMB2_HDR_BODY + 2;
679
680                         state->asked_for_header = true;
681                 }
682
683                 state->missing -= len;
684
685                 buf = talloc_array(req->in.vector, uint8_t, len);
686                 if (buf == NULL) {
687                         return -1;
688                 }
689
690                 req->in.vector[idx].iov_base    = (void *)buf;
691                 req->in.vector[idx].iov_len     = len;
692
693                 vector = talloc_array(mem_ctx, struct iovec, 1);
694                 if (vector == NULL) {
695                         return -1;
696                 }
697
698                 vector[0] = req->in.vector[idx];
699
700                 *_vector = vector;
701                 *_count = 1;
702                 return 0;
703         }
704
705         if (state->missing == 0) {
706                 /* if there're no remaining bytes, we're done */
707                 *_vector = NULL;
708                 *_count = 0;
709                 return 0;
710         }
711
712         if (state->asked_for_header) {
713                 const uint8_t *hdr;
714                 size_t full_size;
715                 size_t next_command_ofs;
716                 size_t body_size;
717                 uint8_t *body;
718                 size_t dyn_size;
719                 uint8_t *dyn;
720                 bool invalid = false;
721
722                 state->asked_for_header = false;
723
724                 /*
725                  * We got the SMB2 header and the first 2 bytes
726                  * of the body. We fix the size to just the header
727                  * and manually copy the 2 first bytes to the body section
728                  */
729                 req->in.vector[idx-1].iov_len = SMB2_HDR_BODY;
730                 hdr = (const uint8_t *)req->in.vector[idx-1].iov_base;
731
732                 /* allocate vectors for body and dynamic areas */
733                 req->in.vector = talloc_realloc(req, req->in.vector,
734                                                 struct iovec,
735                                                 req->in.vector_count + 2);
736                 if (req->in.vector == NULL) {
737                         return -1;
738                 }
739                 req->in.vector_count += 2;
740
741                 full_size = state->missing + SMB2_HDR_BODY + 2;
742                 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
743                 body_size = SVAL(hdr, SMB2_HDR_BODY);
744
745                 if (next_command_ofs != 0) {
746                         if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
747                                 /*
748                                  * this is invalid, just return a zero
749                                  * body and let the caller deal with the error
750                                  */
751                                 invalid = true;
752                         } else if (next_command_ofs > full_size) {
753                                 /*
754                                  * this is invalid, just return a zero
755                                  * body and let the caller deal with the error
756                                  */
757                                 invalid = true;
758                         } else {
759                                 full_size = next_command_ofs;
760                         }
761                 }
762
763                 if (!invalid) {
764                         if (body_size < 2) {
765                                 /*
766                                  * this is invalid, just return a zero
767                                  * body and let the caller deal with the error
768                                  */
769                                 invalid = true;
770                         } else if (body_size > (full_size - SMB2_HDR_BODY)) {
771                                 /*
772                                  * this is invalid, just return a zero
773                                  * body and let the caller deal with the error
774                                  */
775                                 invalid = true;
776                         }
777                 }
778
779                 if (invalid) {
780                         /* the caller should check this */
781                         body_size = 0;
782                 }
783
784                 if ((body_size % 2) != 0) {
785                         body_size -= 1;
786                 }
787
788                 dyn_size = full_size - (SMB2_HDR_BODY + body_size);
789
790                 state->missing -= (body_size - 2) + dyn_size;
791
792                 body = talloc_array(req->in.vector, uint8_t, body_size);
793                 if (body == NULL) {
794                         return -1;
795                 }
796
797                 dyn = talloc_array(req->in.vector, uint8_t, dyn_size);
798                 if (dyn == NULL) {
799                         return -1;
800                 }
801
802                 req->in.vector[idx].iov_base    = (void *)body;
803                 req->in.vector[idx].iov_len     = body_size;
804                 req->in.vector[idx+1].iov_base  = (void *)dyn;
805                 req->in.vector[idx+1].iov_len   = dyn_size;
806
807                 vector = talloc_array(mem_ctx, struct iovec, 2);
808                 if (vector == NULL) {
809                         return -1;
810                 }
811
812                 /*
813                  * the first 2 bytes of the body were already fetched
814                  * together with the header
815                  */
816                 memcpy(body, hdr + SMB2_HDR_BODY, 2);
817                 vector[0].iov_base = body + 2;
818                 vector[0].iov_len = req->in.vector[idx].iov_len - 2;
819
820                 vector[1] = req->in.vector[idx+1];
821
822                 *_vector = vector;
823                 *_count = 2;
824                 return 0;
825         }
826
827         /*
828          * when we endup here, we're looking for a new SMB2 request
829          * next. And we ask for its header and the first 2 bytes of
830          * the body (like we did for the first SMB2 request).
831          */
832
833         req->in.vector = talloc_realloc(req, req->in.vector,
834                                         struct iovec,
835                                         req->in.vector_count + 1);
836         if (req->in.vector == NULL) {
837                 return -1;
838         }
839         req->in.vector_count += 1;
840
841         /*
842          * We assume it's a SMB2 request,
843          * and we first get the header and the
844          * first 2 bytes (the struct size) of the body
845          */
846         len = SMB2_HDR_BODY + 2;
847
848         if (len > state->missing) {
849                 /* let the caller handle the error */
850                 len = state->missing;
851         }
852
853         state->missing -= len;
854         state->asked_for_header = true;
855
856         buf = talloc_array(req->in.vector, uint8_t, len);
857         if (buf == NULL) {
858                 return -1;
859         }
860
861         req->in.vector[idx].iov_base    = (void *)buf;
862         req->in.vector[idx].iov_len     = len;
863
864         vector = talloc_array(mem_ctx, struct iovec, 1);
865         if (vector == NULL) {
866                 return -1;
867         }
868
869         vector[0] = req->in.vector[idx];
870
871         *_vector = vector;
872         *_count = 1;
873         return 0;
874 }
875
876 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
877 {
878         struct tevent_req *req =
879                 tevent_req_callback_data(subreq,
880                 struct tevent_req);
881         int ret;
882         int sys_errno;
883         NTSTATUS status;
884
885         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
886         if (ret == -1) {
887                 status = map_nt_error_from_unix(sys_errno);
888                 tevent_req_nterror(req, status);
889                 return;
890         }
891
892         tevent_req_done(req);
893 }
894
895 static NTSTATUS smbd_smb2_request_read_recv(struct tevent_req *req,
896                                             TALLOC_CTX *mem_ctx,
897                                             struct smbd_smb2_request **_smb2_req)
898 {
899         struct smbd_smb2_request_read_state *state =
900                 tevent_req_data(req,
901                 struct smbd_smb2_request_read_state);
902         NTSTATUS status;
903
904         if (tevent_req_is_nterror(req, &status)) {
905                 tevent_req_received(req);
906                 return status;
907         }
908
909         talloc_steal(mem_ctx, state->smb2_req->mem_pool);
910         *_smb2_req = state->smb2_req;
911         tevent_req_received(req);
912         return NT_STATUS_OK;
913 }
914
915 static void smbd_smb2_request_incoming(struct tevent_req *subreq);
916
917 void smbd_smb2_first_negprot(struct smbd_server_connection *conn,
918                              const uint8_t *inbuf, size_t size)
919 {
920         NTSTATUS status;
921         struct smbd_smb2_request *req;
922         struct tevent_req *subreq;
923
924         DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
925                  (unsigned int)size));
926
927         status = smbd_initialize_smb2(conn);
928         if (!NT_STATUS_IS_OK(status)) {
929                 smbd_server_connection_terminate(conn, nt_errstr(status));
930                 return;
931         }
932
933         status = smbd_smb2_request_create(conn, inbuf, size, &req);
934         if (!NT_STATUS_IS_OK(status)) {
935                 smbd_server_connection_terminate(conn, nt_errstr(status));
936                 return;
937         }
938
939         status = smbd_smb2_request_setup_out(req);
940         if (!NT_STATUS_IS_OK(status)) {
941                 smbd_server_connection_terminate(conn, nt_errstr(status));
942                 return;
943         }
944
945         status = smbd_smb2_request_dispatch(req);
946         if (!NT_STATUS_IS_OK(status)) {
947                 smbd_server_connection_terminate(conn, nt_errstr(status));
948                 return;
949         }
950
951         /* ask for the next request */
952         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
953         if (subreq == NULL) {
954                 smbd_server_connection_terminate(conn, "no memory for reading");
955                 return;
956         }
957         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
958 }
959
960 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
961 {
962         struct smbd_server_connection *conn = tevent_req_callback_data(subreq,
963                                               struct smbd_server_connection);
964         NTSTATUS status;
965         struct smbd_smb2_request *req;
966
967         status = smbd_smb2_request_read_recv(subreq, conn, &req);
968         TALLOC_FREE(subreq);
969         if (!NT_STATUS_IS_OK(status)) {
970                 smbd_server_connection_terminate(conn, nt_errstr(status));
971                 return;
972         }
973
974         /* TODO: validate the incoming request */
975         req->current_idx = 1;
976
977         DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
978                  req->current_idx, req->in.vector_count));
979
980         status = smbd_smb2_request_setup_out(req);
981         if (!NT_STATUS_IS_OK(status)) {
982                 smbd_server_connection_terminate(conn, nt_errstr(status));
983                 return;
984         }
985
986         status = smbd_smb2_request_dispatch(req);
987         if (!NT_STATUS_IS_OK(status)) {
988                 smbd_server_connection_terminate(conn, nt_errstr(status));
989                 return;
990         }
991
992         /* ask for the next request (this constructs the main loop) */
993         subreq = smbd_smb2_request_read_send(conn,conn->smb2.event_ctx, conn);
994         if (subreq == NULL) {
995                 smbd_server_connection_terminate(conn, "no memory for reading");
996                 return;
997         }
998         tevent_req_set_callback(subreq, smbd_smb2_request_incoming, conn);
999 }