TODO anon? s3:smb3_sesssetup: close the previous SMB2 session if requested and allowed
[metze/samba/wip.git] / source3 / smbd / smb2_read.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 "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "libcli/security/security.h"
27 #include "../lib/util/tevent_ntstatus.h"
28 #include "rpc_server/srv_pipe_hnd.h"
29
30 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
31                                               struct tevent_context *ev,
32                                               struct smbd_smb2_request *smb2req,
33                                               uint32_t in_smbpid,
34                                               uint64_t in_file_id_volatile,
35                                               uint32_t in_length,
36                                               uint64_t in_offset,
37                                               uint32_t in_minimum,
38                                               uint32_t in_remaining);
39 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
40                                     TALLOC_CTX *mem_ctx,
41                                     DATA_BLOB *out_data,
42                                     uint32_t *out_remaining);
43
44 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
45 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req)
46 {
47         NTSTATUS status;
48         const uint8_t *inhdr;
49         const uint8_t *inbody;
50         int i = req->current_idx;
51         uint32_t in_smbpid;
52         uint32_t in_length;
53         uint64_t in_offset;
54         uint64_t in_file_id_persistent;
55         uint64_t in_file_id_volatile;
56         uint32_t in_minimum_count;
57         uint32_t in_remaining_bytes;
58         struct tevent_req *subreq;
59
60         status = smbd_smb2_request_verify_sizes(req, 0x31);
61         if (!NT_STATUS_IS_OK(status)) {
62                 return smbd_smb2_request_error(req, status);
63         }
64         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
65         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
66
67         in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
68
69         in_length               = IVAL(inbody, 0x04);
70         in_offset               = BVAL(inbody, 0x08);
71         in_file_id_persistent   = BVAL(inbody, 0x10);
72         in_file_id_volatile     = BVAL(inbody, 0x18);
73         in_minimum_count        = IVAL(inbody, 0x20);
74         in_remaining_bytes      = IVAL(inbody, 0x28);
75
76         /* check the max read size */
77         if (in_length > req->sconn->smb2.max_read) {
78                 DEBUG(0,("here:%s: 0x%08X: 0x%08X\n",
79                         __location__, in_length, req->sconn->smb2.max_read));
80                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
81         }
82
83         status = smbd_smb2_request_verify_creditcharge(req, in_length);
84         if (!NT_STATUS_IS_OK(status)) {
85                 return smbd_smb2_request_error(req, status);
86         }
87
88         if (req->compat_chain_fsp) {
89                 /* skip check */
90         } else if (in_file_id_persistent != in_file_id_volatile) {
91                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
92         }
93
94         subreq = smbd_smb2_read_send(req,
95                                      req->sconn->ev_ctx,
96                                      req,
97                                      in_smbpid,
98                                      in_file_id_volatile,
99                                      in_length,
100                                      in_offset,
101                                      in_minimum_count,
102                                      in_remaining_bytes);
103         if (subreq == NULL) {
104                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
105         }
106         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
107
108         return smbd_smb2_request_pending_queue(req, subreq, 500);
109 }
110
111 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
112 {
113         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
114                                         struct smbd_smb2_request);
115         DATA_BLOB outbody;
116         DATA_BLOB outdyn;
117         uint8_t out_data_offset;
118         DATA_BLOB out_data_buffer = data_blob_null;
119         uint32_t out_data_remaining = 0;
120         NTSTATUS status;
121         NTSTATUS error; /* transport error */
122
123         status = smbd_smb2_read_recv(subreq,
124                                      req,
125                                      &out_data_buffer,
126                                      &out_data_remaining);
127         TALLOC_FREE(subreq);
128         if (!NT_STATUS_IS_OK(status)) {
129                 error = smbd_smb2_request_error(req, status);
130                 if (!NT_STATUS_IS_OK(error)) {
131                         smbd_server_connection_terminate(req->sconn,
132                                                          nt_errstr(error));
133                         return;
134                 }
135                 return;
136         }
137
138         out_data_offset = SMB2_HDR_BODY + 0x10;
139
140         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
141         if (outbody.data == NULL) {
142                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
143                 if (!NT_STATUS_IS_OK(error)) {
144                         smbd_server_connection_terminate(req->sconn,
145                                                          nt_errstr(error));
146                         return;
147                 }
148                 return;
149         }
150
151         SSVAL(outbody.data, 0x00, 0x10 + 1);    /* struct size */
152         SCVAL(outbody.data, 0x02,
153               out_data_offset);                 /* data offset */
154         SCVAL(outbody.data, 0x03, 0);           /* reserved */
155         SIVAL(outbody.data, 0x04,
156               out_data_buffer.length);          /* data length */
157         SIVAL(outbody.data, 0x08,
158               out_data_remaining);              /* data remaining */
159         SIVAL(outbody.data, 0x0C, 0);           /* reserved */
160
161         outdyn = out_data_buffer;
162
163         error = smbd_smb2_request_done(req, outbody, &outdyn);
164         if (!NT_STATUS_IS_OK(error)) {
165                 smbd_server_connection_terminate(req->sconn,
166                                                  nt_errstr(error));
167                 return;
168         }
169 }
170
171 struct smbd_smb2_read_state {
172         struct smbd_smb2_request *smb2req;
173         struct smb_request *smbreq;
174         files_struct *fsp;
175         uint64_t in_file_id_volatile;
176         uint32_t in_length;
177         uint64_t in_offset;
178         uint32_t in_minimum;
179         DATA_BLOB out_data;
180         uint32_t out_remaining;
181 };
182
183 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
184 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
185 {
186         struct lock_struct lock;
187         uint32_t in_length = state->in_length;
188         uint64_t in_offset = state->in_offset;
189         files_struct *fsp = state->fsp;
190         ssize_t nread;
191
192         nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
193                                         fsp,
194                                         NULL,
195                                         in_offset,
196                                         in_length);
197         DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
198                 (int)nread,
199                 fsp_str_dbg(fsp) ));
200
201         if (nread == -1) {
202                 if (errno == ENOSYS || errno == EINTR) {
203                         /*
204                          * Special hack for broken systems with no working
205                          * sendfile. Fake this up by doing read/write calls.
206                         */
207                         set_use_sendfile(SNUM(fsp->conn), false);
208                         nread = fake_sendfile(fsp, in_offset, in_length);
209                         if (nread == -1) {
210                                 DEBUG(0,("smb2_sendfile_send_data: "
211                                         "fake_sendfile failed for "
212                                         "file %s (%s).\n",
213                                         fsp_str_dbg(fsp),
214                                         strerror(errno)));
215                                 exit_server_cleanly("smb2_sendfile_send_data: "
216                                         "fake_sendfile failed");
217                         }
218                         goto out;
219                 }
220
221                 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
222                         "%s (%s). Terminating\n",
223                         fsp_str_dbg(fsp),
224                         strerror(errno)));
225                 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
226         } else if (nread == 0) {
227                 /*
228                  * Some sendfile implementations return 0 to indicate
229                  * that there was a short read, but nothing was
230                  * actually written to the socket.  In this case,
231                  * fallback to the normal read path so the header gets
232                  * the correct byte count.
233                  */
234                 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
235                         "falling back to the normal read: %s\n",
236                         fsp_str_dbg(fsp)));
237
238                 nread = fake_sendfile(fsp, in_offset, in_length);
239                 if (nread == -1) {
240                         DEBUG(0,("smb2_sendfile_send_data: "
241                                 "fake_sendfile failed for file "
242                                 "%s (%s). Terminating\n",
243                                 fsp_str_dbg(fsp),
244                                 strerror(errno)));
245                         exit_server_cleanly("smb2_sendfile_send_data: "
246                                 "fake_sendfile failed");
247                 }
248         }
249
250   out:
251
252         if (nread < in_length) {
253                 sendfile_short_send(fsp, nread, 0, in_length);
254         }
255
256         init_strict_lock_struct(fsp,
257                                 state->in_file_id_volatile,
258                                 in_offset,
259                                 in_length,
260                                 READ_LOCK,
261                                 &lock);
262
263         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
264         return 0;
265 }
266
267 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
268                                         struct smbd_smb2_read_state *state)
269 {
270         struct smbd_smb2_read_state *state_copy = NULL;
271         files_struct *fsp = state->fsp;
272
273         /*
274          * We cannot use sendfile if...
275          * We were not configured to do so OR
276          * Signing is active OR
277          * This is a compound SMB2 operation OR
278          * fsp is a STREAM file OR
279          * We're using a write cache OR
280          * It's not a regular file OR
281          * Requested offset is greater than file size OR
282          * there's not enough data in the file.
283          * Phew :-). Luckily this means most
284          * reads on most normal files. JRA.
285         */
286
287         if (!lp__use_sendfile(SNUM(fsp->conn)) ||
288                         smb2req->do_signing ||
289                         smb2req->in.vector_count != 4 ||
290                         (fsp->base_fsp != NULL) ||
291                         (fsp->wcp != NULL) ||
292                         (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
293                         (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
294                         (fsp->fsp_name->st.st_ex_size < state->in_offset +
295                                 state->in_length)) {
296                 return NT_STATUS_RETRY;
297         }
298
299         /* We've already checked there's this amount of data
300            to read. */
301         state->out_data.length = state->in_length;
302         state->out_remaining = 0;
303
304         /* Make a copy of state attached to the smb2req. Attach
305            the destructor here as this will trigger the sendfile
306            call when the request is destroyed. */
307         state_copy = talloc(smb2req, struct smbd_smb2_read_state);
308         if (!state_copy) {
309                 return NT_STATUS_NO_MEMORY;
310         }
311         *state_copy = *state;
312         talloc_set_destructor(state_copy, smb2_sendfile_send_data);
313         return NT_STATUS_OK;
314 }
315
316 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
317
318 /*******************************************************************
319  Common read complete processing function for both synchronous and
320  asynchronous reads.
321 *******************************************************************/
322
323 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
324 {
325         struct smbd_smb2_read_state *state = tevent_req_data(req,
326                                         struct smbd_smb2_read_state);
327         files_struct *fsp = state->fsp;
328
329         if (nread < 0) {
330                 NTSTATUS status = map_nt_error_from_unix(err);
331
332                 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
333                         "Error = %s (NTSTATUS %s)\n",
334                         fsp_str_dbg(fsp),
335                         (int)nread,
336                         strerror(err),
337                         nt_errstr(status)));
338
339                 return status;
340         }
341         if (nread == 0 && state->in_length != 0) {
342                 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
343                         fsp_str_dbg(fsp)));
344                 return NT_STATUS_END_OF_FILE;
345         }
346
347         if (nread < state->in_minimum) {
348                 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
349                         "minimum requested %u. Returning end of file\n",
350                         fsp_str_dbg(fsp),
351                         (int)nread,
352                         (unsigned int)state->in_minimum));
353                 return NT_STATUS_END_OF_FILE;
354         }
355
356         DEBUG(3,("smbd_smb2_read: fnum=[%d/%s] length=%lu offset=%lu read=%lu\n",
357                 fsp->fnum,
358                 fsp_str_dbg(fsp),
359                 (unsigned long)state->in_length,
360                 (unsigned long)state->in_offset,
361                 (unsigned long)nread));
362
363         state->out_data.length = nread;
364         state->out_remaining = 0;
365
366         return NT_STATUS_OK;
367 }
368
369 static bool smbd_smb2_read_cancel(struct tevent_req *req)
370 {
371         struct smbd_smb2_read_state *state =
372                 tevent_req_data(req,
373                 struct smbd_smb2_read_state);
374
375         state->smb2req->cancelled = true;
376
377         return cancel_smb2_aio(state->smbreq);
378 }
379
380 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
381                                               struct tevent_context *ev,
382                                               struct smbd_smb2_request *smb2req,
383                                               uint32_t in_smbpid,
384                                               uint64_t in_file_id_volatile,
385                                               uint32_t in_length,
386                                               uint64_t in_offset,
387                                               uint32_t in_minimum,
388                                               uint32_t in_remaining)
389 {
390         NTSTATUS status;
391         struct tevent_req *req = NULL;
392         struct smbd_smb2_read_state *state = NULL;
393         struct smb_request *smbreq = NULL;
394         connection_struct *conn = smb2req->tcon->compat;
395         files_struct *fsp = NULL;
396         ssize_t nread = -1;
397         struct lock_struct lock;
398         int saved_errno;
399
400         req = tevent_req_create(mem_ctx, &state,
401                                 struct smbd_smb2_read_state);
402         if (req == NULL) {
403                 return NULL;
404         }
405         state->smb2req = smb2req;
406         state->in_length = in_length;
407         state->in_offset = in_offset;
408         state->in_minimum = in_minimum;
409         state->out_data = data_blob_null;
410         state->out_remaining = 0;
411
412         DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
413                   (unsigned long long)in_file_id_volatile));
414
415         smbreq = smbd_smb2_fake_smb_request(smb2req);
416         if (tevent_req_nomem(smbreq, req)) {
417                 return tevent_req_post(req, ev);
418         }
419         state->smbreq = smbreq;
420
421         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
422         if (fsp == NULL) {
423                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
424                 return tevent_req_post(req, ev);
425         }
426         if (conn != fsp->conn) {
427                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
428                 return tevent_req_post(req, ev);
429         }
430         if (smb2req->session->compat->vuid != fsp->vuid) {
431                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
432                 return tevent_req_post(req, ev);
433         }
434         if (fsp->is_directory) {
435                 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
436                 return tevent_req_post(req, ev);
437         }
438
439         state->fsp = fsp;
440         state->in_file_id_volatile = in_file_id_volatile;
441
442         if (IS_IPC(smbreq->conn)) {
443                 struct tevent_req *subreq = NULL;
444
445                 state->out_data = data_blob_talloc(state, NULL, in_length);
446                 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
447                         return tevent_req_post(req, ev);
448                 }
449
450                 if (!fsp_is_np(fsp)) {
451                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
452                         return tevent_req_post(req, ev);
453                 }
454
455                 subreq = np_read_send(state, ev,
456                                       fsp->fake_file_handle,
457                                       state->out_data.data,
458                                       state->out_data.length);
459                 if (tevent_req_nomem(subreq, req)) {
460                         return tevent_req_post(req, ev);
461                 }
462                 tevent_req_set_callback(subreq,
463                                         smbd_smb2_read_pipe_done,
464                                         req);
465                 return req;
466         }
467
468         if (!CHECK_READ(fsp, smbreq)) {
469                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
470                 return tevent_req_post(req, ev);
471         }
472
473         status = schedule_smb2_aio_read(fsp->conn,
474                                 smbreq,
475                                 fsp,
476                                 state,
477                                 &state->out_data,
478                                 (off_t)in_offset,
479                                 (size_t)in_length);
480
481         if (NT_STATUS_IS_OK(status)) {
482                 /*
483                  * Doing an async read, allow this
484                  * request to be canceled
485                  */
486                 tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
487                 return req;
488         }
489
490         if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
491                 /* Real error in setting up aio. Fail. */
492                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
493                 return tevent_req_post(req, ev);
494         }
495
496         /* Fallback to synchronous. */
497
498         init_strict_lock_struct(fsp,
499                                 in_file_id_volatile,
500                                 in_offset,
501                                 in_length,
502                                 READ_LOCK,
503                                 &lock);
504
505         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
506                 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
507                 return tevent_req_post(req, ev);
508         }
509
510         /* Try sendfile in preference. */
511         status = schedule_smb2_sendfile_read(smb2req, state);
512         if (NT_STATUS_IS_OK(status)) {
513                 tevent_req_done(req);
514                 return tevent_req_post(req, ev);
515         } else {
516                 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
517                         SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
518                         tevent_req_nterror(req, status);
519                         return tevent_req_post(req, ev);
520                 }
521         }
522
523         /* Ok, read into memory. Allocate the out buffer. */
524         state->out_data = data_blob_talloc(state, NULL, in_length);
525         if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
526                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
527                 return tevent_req_post(req, ev);
528         }
529
530         nread = read_file(fsp,
531                           (char *)state->out_data.data,
532                           in_offset,
533                           in_length);
534
535         saved_errno = errno;
536
537         SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
538
539         DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
540                 "len=%llu returned %lld\n",
541                 fsp_str_dbg(fsp),
542                 (unsigned long long)in_file_id_volatile,
543                 (unsigned long long)in_offset,
544                 (unsigned long long)in_length,
545                 (long long)nread));
546
547         status = smb2_read_complete(req, nread, saved_errno);
548         if (!NT_STATUS_IS_OK(status)) {
549                 tevent_req_nterror(req, status);
550         } else {
551                 /* Success. */
552                 tevent_req_done(req);
553         }
554         return tevent_req_post(req, ev);
555 }
556
557 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
558 {
559         struct tevent_req *req = tevent_req_callback_data(subreq,
560                                  struct tevent_req);
561         struct smbd_smb2_read_state *state = tevent_req_data(req,
562                                              struct smbd_smb2_read_state);
563         NTSTATUS status;
564         ssize_t nread = -1;
565         bool is_data_outstanding;
566
567         status = np_read_recv(subreq, &nread, &is_data_outstanding);
568         TALLOC_FREE(subreq);
569         if (!NT_STATUS_IS_OK(status)) {
570                 tevent_req_nterror(req, status);
571                 return;
572         }
573
574         if (nread == 0 && state->out_data.length != 0) {
575                 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
576                 return;
577         }
578
579         state->out_data.length = nread;
580         state->out_remaining = 0;
581
582         /*
583          * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
584          * handle it in SMB1 pipe_read_andx_done().
585          */
586
587         tevent_req_done(req);
588 }
589
590 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
591                                     TALLOC_CTX *mem_ctx,
592                                     DATA_BLOB *out_data,
593                                     uint32_t *out_remaining)
594 {
595         NTSTATUS status;
596         struct smbd_smb2_read_state *state = tevent_req_data(req,
597                                              struct smbd_smb2_read_state);
598
599         if (tevent_req_is_nterror(req, &status)) {
600                 tevent_req_received(req);
601                 return status;
602         }
603
604         *out_data = state->out_data;
605         talloc_steal(mem_ctx, out_data->data);
606         *out_remaining = state->out_remaining;
607
608         tevent_req_received(req);
609         return NT_STATUS_OK;
610 }