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