s3:smb2_read: avoid 2 talloc* calls when using sendfile()
[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                                               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 = data_blob_talloc(req->out.vector, NULL, 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         ssize_t nread;
188
189         nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
190                                  fsp,
191                                  state->smb2req->queue_entry.sendfile_header,
192                                  in_offset,
193                                  in_length);
194         DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
195                 (int)nread,
196                 fsp_str_dbg(fsp) ));
197
198         if (nread == -1) {
199                 if (errno == ENOSYS || errno == EINTR) {
200                         /*
201                          * Special hack for broken systems with no working
202                          * sendfile. Fake this up by doing read/write calls.
203                         */
204                         set_use_sendfile(SNUM(fsp->conn), false);
205                         nread = fake_sendfile(fsp, in_offset, in_length);
206                         if (nread == -1) {
207                                 DEBUG(0,("smb2_sendfile_send_data: "
208                                         "fake_sendfile failed for "
209                                         "file %s (%s).\n",
210                                         fsp_str_dbg(fsp),
211                                         strerror(errno)));
212                                 exit_server_cleanly("smb2_sendfile_send_data: "
213                                         "fake_sendfile failed");
214                         }
215                         goto out;
216                 }
217
218                 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
219                         "%s (%s). Terminating\n",
220                         fsp_str_dbg(fsp),
221                         strerror(errno)));
222                 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
223         } else if (nread == 0) {
224                 /*
225                  * Some sendfile implementations return 0 to indicate
226                  * that there was a short read, but nothing was
227                  * actually written to the socket.  In this case,
228                  * fallback to the normal read path so the header gets
229                  * the correct byte count.
230                  */
231                 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
232                         "falling back to the normal read: %s\n",
233                         fsp_str_dbg(fsp)));
234
235                 nread = fake_sendfile(fsp, in_offset, in_length);
236                 if (nread == -1) {
237                         DEBUG(0,("smb2_sendfile_send_data: "
238                                 "fake_sendfile failed for file "
239                                 "%s (%s). Terminating\n",
240                                 fsp_str_dbg(fsp),
241                                 strerror(errno)));
242                         exit_server_cleanly("smb2_sendfile_send_data: "
243                                 "fake_sendfile failed");
244                 }
245         }
246
247   out:
248
249         if (nread < in_length) {
250                 sendfile_short_send(fsp, nread, 0, in_length);
251         }
252
253         init_strict_lock_struct(fsp,
254                                 fsp->op->global->open_persistent_id,
255                                 in_offset,
256                                 in_length,
257                                 READ_LOCK,
258                                 &lock);
259
260         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
261         return 0;
262 }
263
264 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
265                                         struct smbd_smb2_read_state *state)
266 {
267         files_struct *fsp = state->fsp;
268
269         /*
270          * We cannot use sendfile if...
271          * We were not configured to do so OR
272          * Signing is active OR
273          * This is a compound SMB2 operation OR
274          * fsp is a STREAM file OR
275          * We're using a write cache OR
276          * It's not a regular file OR
277          * Requested offset is greater than file size OR
278          * there's not enough data in the file.
279          * Phew :-). Luckily this means most
280          * reads on most normal files. JRA.
281         */
282
283         if (!lp__use_sendfile(SNUM(fsp->conn)) ||
284             smb2req->do_signing ||
285             smb2req->do_encryption ||
286             smb2req->in.vector_count >= (2*SMBD_SMB2_NUM_IOV_PER_REQ) ||
287             (fsp->base_fsp != NULL) ||
288             (fsp->wcp != NULL) ||
289             (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
290             (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
291             (fsp->fsp_name->st.st_ex_size < state->in_offset + state->in_length))
292         {
293                 return NT_STATUS_RETRY;
294         }
295
296         /* We've already checked there's this amount of data
297            to read. */
298         state->out_data.length = state->in_length;
299         state->out_remaining = 0;
300
301         state->out_headers = data_blob_const(state->_out_hdr_buf,
302                                              sizeof(state->_out_hdr_buf));
303         return NT_STATUS_OK;
304 }
305
306 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
307
308 /*******************************************************************
309  Common read complete processing function for both synchronous and
310  asynchronous reads.
311 *******************************************************************/
312
313 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
314 {
315         struct smbd_smb2_read_state *state = tevent_req_data(req,
316                                         struct smbd_smb2_read_state);
317         files_struct *fsp = state->fsp;
318
319         if (nread < 0) {
320                 NTSTATUS status = map_nt_error_from_unix(err);
321
322                 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
323                         "Error = %s (NTSTATUS %s)\n",
324                         fsp_str_dbg(fsp),
325                         (int)nread,
326                         strerror(err),
327                         nt_errstr(status)));
328
329                 return status;
330         }
331         if (nread == 0 && state->in_length != 0) {
332                 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
333                         fsp_str_dbg(fsp)));
334                 return NT_STATUS_END_OF_FILE;
335         }
336
337         if (nread < state->in_minimum) {
338                 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
339                         "minimum requested %u. Returning end of file\n",
340                         fsp_str_dbg(fsp),
341                         (int)nread,
342                         (unsigned int)state->in_minimum));
343                 return NT_STATUS_END_OF_FILE;
344         }
345
346         DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
347                 fsp_fnum_dbg(fsp),
348                 fsp_str_dbg(fsp),
349                 (unsigned long)state->in_length,
350                 (unsigned long)state->in_offset,
351                 (unsigned long)nread));
352
353         state->out_data.length = nread;
354         state->out_remaining = 0;
355
356         return NT_STATUS_OK;
357 }
358
359 static bool smbd_smb2_read_cancel(struct tevent_req *req)
360 {
361         struct smbd_smb2_read_state *state =
362                 tevent_req_data(req,
363                 struct smbd_smb2_read_state);
364
365         return cancel_smb2_aio(state->smbreq);
366 }
367
368 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
369                                               struct tevent_context *ev,
370                                               struct smbd_smb2_request *smb2req,
371                                               struct files_struct *fsp,
372                                               uint32_t in_length,
373                                               uint64_t in_offset,
374                                               uint32_t in_minimum,
375                                               uint32_t in_remaining)
376 {
377         NTSTATUS status;
378         struct tevent_req *req = NULL;
379         struct smbd_smb2_read_state *state = NULL;
380         struct smb_request *smbreq = NULL;
381         connection_struct *conn = smb2req->tcon->compat;
382         ssize_t nread = -1;
383         struct lock_struct lock;
384         int saved_errno;
385
386         req = tevent_req_create(mem_ctx, &state,
387                                 struct smbd_smb2_read_state);
388         if (req == NULL) {
389                 return NULL;
390         }
391         state->smb2req = smb2req;
392         state->in_length = in_length;
393         state->in_offset = in_offset;
394         state->in_minimum = in_minimum;
395         state->out_data = data_blob_null;
396         state->out_remaining = 0;
397
398         DEBUG(10,("smbd_smb2_read: %s - %s\n",
399                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
400
401         smbreq = smbd_smb2_fake_smb_request(smb2req);
402         if (tevent_req_nomem(smbreq, req)) {
403                 return tevent_req_post(req, ev);
404         }
405         state->smbreq = smbreq;
406
407         if (fsp->is_directory) {
408                 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
409                 return tevent_req_post(req, ev);
410         }
411
412         state->fsp = fsp;
413
414         if (IS_IPC(smbreq->conn)) {
415                 struct tevent_req *subreq = NULL;
416
417                 state->out_data = data_blob_talloc(state, NULL, in_length);
418                 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
419                         return tevent_req_post(req, ev);
420                 }
421
422                 if (!fsp_is_np(fsp)) {
423                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
424                         return tevent_req_post(req, ev);
425                 }
426
427                 subreq = np_read_send(state, ev,
428                                       fsp->fake_file_handle,
429                                       state->out_data.data,
430                                       state->out_data.length);
431                 if (tevent_req_nomem(subreq, req)) {
432                         return tevent_req_post(req, ev);
433                 }
434                 tevent_req_set_callback(subreq,
435                                         smbd_smb2_read_pipe_done,
436                                         req);
437                 return req;
438         }
439
440         if (!CHECK_READ(fsp, smbreq)) {
441                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
442                 return tevent_req_post(req, ev);
443         }
444
445         status = schedule_smb2_aio_read(fsp->conn,
446                                 smbreq,
447                                 fsp,
448                                 state,
449                                 &state->out_data,
450                                 (off_t)in_offset,
451                                 (size_t)in_length);
452
453         if (NT_STATUS_IS_OK(status)) {
454                 /*
455                  * Doing an async read, allow this
456                  * request to be canceled
457                  */
458                 tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
459                 return req;
460         }
461
462         if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
463                 /* Real error in setting up aio. Fail. */
464                 tevent_req_nterror(req, status);
465                 return tevent_req_post(req, ev);
466         }
467
468         /* Fallback to synchronous. */
469
470         init_strict_lock_struct(fsp,
471                                 fsp->op->global->open_persistent_id,
472                                 in_offset,
473                                 in_length,
474                                 READ_LOCK,
475                                 &lock);
476
477         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
478                 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
479                 return tevent_req_post(req, ev);
480         }
481
482         /* Try sendfile in preference. */
483         status = schedule_smb2_sendfile_read(smb2req, state);
484         if (NT_STATUS_IS_OK(status)) {
485                 tevent_req_done(req);
486                 return tevent_req_post(req, ev);
487         } else {
488                 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
489                         SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
490                         tevent_req_nterror(req, status);
491                         return tevent_req_post(req, ev);
492                 }
493         }
494
495         /* Ok, read into memory. Allocate the out buffer. */
496         state->out_data = data_blob_talloc(state, NULL, in_length);
497         if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
498                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
499                 return tevent_req_post(req, ev);
500         }
501
502         nread = read_file(fsp,
503                           (char *)state->out_data.data,
504                           in_offset,
505                           in_length);
506
507         saved_errno = errno;
508
509         SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
510
511         DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
512                 "len=%llu returned %lld\n",
513                 fsp_str_dbg(fsp),
514                 fsp_fnum_dbg(fsp),
515                 (unsigned long long)in_offset,
516                 (unsigned long long)in_length,
517                 (long long)nread));
518
519         status = smb2_read_complete(req, nread, saved_errno);
520         if (!NT_STATUS_IS_OK(status)) {
521                 tevent_req_nterror(req, status);
522         } else {
523                 /* Success. */
524                 tevent_req_done(req);
525         }
526         return tevent_req_post(req, ev);
527 }
528
529 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
530 {
531         struct tevent_req *req = tevent_req_callback_data(subreq,
532                                  struct tevent_req);
533         struct smbd_smb2_read_state *state = tevent_req_data(req,
534                                              struct smbd_smb2_read_state);
535         NTSTATUS status;
536         ssize_t nread = -1;
537         bool is_data_outstanding;
538
539         status = np_read_recv(subreq, &nread, &is_data_outstanding);
540         TALLOC_FREE(subreq);
541         if (!NT_STATUS_IS_OK(status)) {
542                 NTSTATUS old = status;
543                 status = nt_status_np_pipe(old);
544                 tevent_req_nterror(req, status);
545                 return;
546         }
547
548         if (nread == 0 && state->out_data.length != 0) {
549                 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
550                 return;
551         }
552
553         state->out_data.length = nread;
554         state->out_remaining = 0;
555
556         /*
557          * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
558          * handle it in SMB1 pipe_read_andx_done().
559          */
560
561         tevent_req_done(req);
562 }
563
564 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
565                                     TALLOC_CTX *mem_ctx,
566                                     DATA_BLOB *out_data,
567                                     uint32_t *out_remaining)
568 {
569         NTSTATUS status;
570         struct smbd_smb2_read_state *state = tevent_req_data(req,
571                                              struct smbd_smb2_read_state);
572
573         if (tevent_req_is_nterror(req, &status)) {
574                 tevent_req_received(req);
575                 return status;
576         }
577
578         *out_data = state->out_data;
579         talloc_steal(mem_ctx, out_data->data);
580         *out_remaining = state->out_remaining;
581
582         if (state->out_headers.length > 0) {
583                 talloc_steal(mem_ctx, state);
584                 talloc_set_destructor(state, smb2_smb2_read_state_deny_destructor);
585                 tevent_req_received(req);
586                 state->smb2req->queue_entry.sendfile_header = &state->out_headers;
587                 talloc_set_destructor(state, smb2_sendfile_send_data);
588         } else {
589                 tevent_req_received(req);
590         }
591
592         return NT_STATUS_OK;
593 }