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