s3: smbd: Correctly set smb2req->smb1req->posix_pathnames from the calling fsp on...
[Samba.git] / source3 / smbd / smb2_read.c
blobeb12dbd72693c184787688437dcfa865e7fe05d0
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
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.
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.
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/>.
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 #include "lib/util/sys_rw_data.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_SMB2
34 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
35 struct tevent_context *ev,
36 struct smbd_smb2_request *smb2req,
37 struct files_struct *in_fsp,
38 uint8_t in_flags,
39 uint32_t in_length,
40 uint64_t in_offset,
41 uint32_t in_minimum,
42 uint32_t in_remaining);
43 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
44 TALLOC_CTX *mem_ctx,
45 DATA_BLOB *out_data,
46 uint32_t *out_remaining);
48 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
49 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req)
51 struct smbXsrv_connection *xconn = req->xconn;
52 NTSTATUS status;
53 const uint8_t *inbody;
54 uint8_t in_flags;
55 uint32_t in_length;
56 uint64_t in_offset;
57 uint64_t in_file_id_persistent;
58 uint64_t in_file_id_volatile;
59 struct files_struct *in_fsp;
60 uint32_t in_minimum_count;
61 uint32_t in_remaining_bytes;
62 struct tevent_req *subreq;
64 status = smbd_smb2_request_verify_sizes(req, 0x31);
65 if (!NT_STATUS_IS_OK(status)) {
66 return smbd_smb2_request_error(req, status);
68 inbody = SMBD_SMB2_IN_BODY_PTR(req);
70 if (xconn->protocol >= PROTOCOL_SMB3_02) {
71 in_flags = CVAL(inbody, 0x03);
72 } else {
73 in_flags = 0;
75 in_length = IVAL(inbody, 0x04);
76 in_offset = BVAL(inbody, 0x08);
77 in_file_id_persistent = BVAL(inbody, 0x10);
78 in_file_id_volatile = BVAL(inbody, 0x18);
79 in_minimum_count = IVAL(inbody, 0x20);
80 in_remaining_bytes = IVAL(inbody, 0x28);
82 /* check the max read size */
83 if (in_length > xconn->smb2.server.max_read) {
84 DEBUG(2,("smbd_smb2_request_process_read: "
85 "client ignored max read: %s: 0x%08X: 0x%08X\n",
86 __location__, in_length, xconn->smb2.server.max_read));
87 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
90 status = smbd_smb2_request_verify_creditcharge(req, in_length);
91 if (!NT_STATUS_IS_OK(status)) {
92 return smbd_smb2_request_error(req, status);
95 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
96 if (in_fsp == NULL) {
97 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
100 subreq = smbd_smb2_read_send(req, req->sconn->ev_ctx,
101 req, in_fsp,
102 in_flags,
103 in_length,
104 in_offset,
105 in_minimum_count,
106 in_remaining_bytes);
107 if (subreq == NULL) {
108 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
110 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
112 return smbd_smb2_request_pending_queue(req, subreq, 500);
115 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
117 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
118 struct smbd_smb2_request);
119 uint16_t body_size;
120 uint8_t body_padding = req->xconn->smb2.smbtorture.read_body_padding;
121 DATA_BLOB outbody;
122 DATA_BLOB outdyn;
123 uint8_t out_data_offset;
124 DATA_BLOB out_data_buffer = data_blob_null;
125 uint32_t out_data_remaining = 0;
126 NTSTATUS status;
127 NTSTATUS error; /* transport error */
129 status = smbd_smb2_read_recv(subreq,
130 req,
131 &out_data_buffer,
132 &out_data_remaining);
133 TALLOC_FREE(subreq);
134 if (!NT_STATUS_IS_OK(status)) {
135 error = smbd_smb2_request_error(req, status);
136 if (!NT_STATUS_IS_OK(error)) {
137 smbd_server_connection_terminate(req->xconn,
138 nt_errstr(error));
139 return;
141 return;
145 * Only FSCTL_SMBTORTURE_GLOBAL_READ_RESPONSE_BODY_PADDING8
146 * sets body_padding to a value different from 0.
148 body_size = 0x10 + body_padding;
149 out_data_offset = SMB2_HDR_BODY + body_size;
151 outbody = smbd_smb2_generate_outbody(req, body_size);
152 if (outbody.data == NULL) {
153 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
154 if (!NT_STATUS_IS_OK(error)) {
155 smbd_server_connection_terminate(req->xconn,
156 nt_errstr(error));
157 return;
159 return;
162 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
163 SCVAL(outbody.data, 0x02,
164 out_data_offset); /* data offset */
165 SCVAL(outbody.data, 0x03, 0); /* reserved */
166 SIVAL(outbody.data, 0x04,
167 out_data_buffer.length); /* data length */
168 SIVAL(outbody.data, 0x08,
169 out_data_remaining); /* data remaining */
170 SIVAL(outbody.data, 0x0C, 0); /* reserved */
171 if (body_padding != 0) {
172 memset(outbody.data + 0x10, 0, body_padding);
175 outdyn = out_data_buffer;
177 error = smbd_smb2_request_done(req, outbody, &outdyn);
178 if (!NT_STATUS_IS_OK(error)) {
179 smbd_server_connection_terminate(req->xconn,
180 nt_errstr(error));
181 return;
185 struct smbd_smb2_read_state {
186 struct smbd_smb2_request *smb2req;
187 struct smb_request *smbreq;
188 files_struct *fsp;
189 uint8_t in_flags;
190 uint32_t in_length;
191 uint64_t in_offset;
192 uint32_t in_minimum;
193 DATA_BLOB out_headers;
194 uint8_t _out_hdr_buf[NBT_HDR_SIZE + SMB2_HDR_BODY + 0x10];
195 DATA_BLOB out_data;
196 uint32_t out_remaining;
199 static int smb2_smb2_read_state_deny_destructor(struct smbd_smb2_read_state *state)
201 return -1;
204 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
205 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
207 struct lock_struct lock;
208 uint32_t in_length = state->in_length;
209 uint64_t in_offset = state->in_offset;
210 files_struct *fsp = state->fsp;
211 const DATA_BLOB *hdr = state->smb2req->queue_entry.sendfile_header;
212 NTSTATUS *pstatus = state->smb2req->queue_entry.sendfile_status;
213 struct smbXsrv_connection *xconn = state->smb2req->xconn;
214 ssize_t nread;
215 ssize_t ret;
216 int saved_errno;
218 nread = SMB_VFS_SENDFILE(xconn->transport.sock,
219 fsp,
220 hdr,
221 in_offset,
222 in_length);
223 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
224 (int)nread,
225 fsp_str_dbg(fsp) ));
227 if (nread == -1) {
228 saved_errno = errno;
231 * Returning ENOSYS means no data at all was sent.
232 Do this as a normal read. */
233 if (errno == ENOSYS) {
234 goto normal_read;
237 if (errno == ENOTSUP) {
238 set_use_sendfile(SNUM(fsp->conn), false);
239 DBG_WARNING("Disabling sendfile use as sendfile is "
240 "not supported by the system\n");
241 goto normal_read;
244 if (errno == EINTR) {
246 * Special hack for broken Linux with no working sendfile. If we
247 * return EINTR we sent the header but not the rest of the data.
248 * Fake this up by doing read/write calls.
250 set_use_sendfile(SNUM(fsp->conn), false);
251 nread = fake_sendfile(xconn, fsp, in_offset, in_length);
252 if (nread == -1) {
253 saved_errno = errno;
254 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
255 "failed for file %s (%s) for client %s. "
256 "Terminating\n",
257 fsp_str_dbg(fsp), strerror(saved_errno),
258 smbXsrv_connection_dbg(xconn)));
259 *pstatus = map_nt_error_from_unix_common(saved_errno);
260 return 0;
262 goto out;
265 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
266 "%s (%s) for client %s. Terminating\n",
267 fsp_str_dbg(fsp), strerror(saved_errno),
268 smbXsrv_connection_dbg(xconn)));
269 *pstatus = map_nt_error_from_unix_common(saved_errno);
270 return 0;
271 } else if (nread == 0) {
273 * Some sendfile implementations return 0 to indicate
274 * that there was a short read, but nothing was
275 * actually written to the socket. In this case,
276 * fallback to the normal read path so the header gets
277 * the correct byte count.
279 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
280 "falling back to the normal read: %s\n",
281 fsp_str_dbg(fsp)));
282 goto normal_read;
286 * We got a short read
288 goto out;
290 normal_read:
291 /* Send out the header. */
292 ret = write_data(xconn->transport.sock,
293 (const char *)hdr->data, hdr->length);
294 if (ret != hdr->length) {
295 saved_errno = errno;
296 DEBUG(0,("smb2_sendfile_send_data: write_data failed for file "
297 "%s (%s) for client %s. Terminating\n",
298 fsp_str_dbg(fsp), strerror(saved_errno),
299 smbXsrv_connection_dbg(xconn)));
300 *pstatus = map_nt_error_from_unix_common(saved_errno);
301 return 0;
303 nread = fake_sendfile(xconn, fsp, in_offset, in_length);
304 if (nread == -1) {
305 saved_errno = errno;
306 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
307 "failed for file %s (%s) for client %s. "
308 "Terminating\n",
309 fsp_str_dbg(fsp), strerror(saved_errno),
310 smbXsrv_connection_dbg(xconn)));
311 *pstatus = map_nt_error_from_unix_common(saved_errno);
312 return 0;
315 out:
317 if (nread < in_length) {
318 ret = sendfile_short_send(xconn, fsp, nread,
319 hdr->length, in_length);
320 if (ret == -1) {
321 saved_errno = errno;
322 DEBUG(0,("%s: sendfile_short_send "
323 "failed for file %s (%s) for client %s. "
324 "Terminating\n",
325 __func__,
326 fsp_str_dbg(fsp), strerror(saved_errno),
327 smbXsrv_connection_dbg(xconn)));
328 *pstatus = map_nt_error_from_unix_common(saved_errno);
329 return 0;
333 init_strict_lock_struct(fsp,
334 fsp->op->global->open_persistent_id,
335 in_offset,
336 in_length,
337 READ_LOCK,
338 lp_posix_cifsu_locktype(fsp),
339 &lock);
341 *pstatus = NT_STATUS_OK;
342 return 0;
345 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
346 struct smbd_smb2_read_state *state)
348 files_struct *fsp = state->fsp;
351 * We cannot use sendfile if...
352 * We were not configured to do so OR
353 * Signing is active OR
354 * This is a compound SMB2 operation OR
355 * fsp is a STREAM file OR
356 * We're using a write cache OR
357 * It's not a regular file OR
358 * Requested offset is greater than file size OR
359 * there's not enough data in the file.
360 * Phew :-). Luckily this means most
361 * reads on most normal files. JRA.
364 if (!lp__use_sendfile(SNUM(fsp->conn)) ||
365 smb2req->do_signing ||
366 smb2req->do_encryption ||
367 smbd_smb2_is_compound(smb2req) ||
368 fsp_is_alternate_stream(fsp) ||
369 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
370 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
371 (fsp->fsp_name->st.st_ex_size < state->in_offset + state->in_length))
373 return NT_STATUS_RETRY;
376 /* We've already checked there's this amount of data
377 to read. */
378 state->out_data.length = state->in_length;
379 state->out_remaining = 0;
381 state->out_headers = data_blob_const(state->_out_hdr_buf,
382 sizeof(state->_out_hdr_buf));
383 return NT_STATUS_OK;
386 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
388 /*******************************************************************
389 Common read complete processing function for both synchronous and
390 asynchronous reads.
391 *******************************************************************/
393 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
395 struct smbd_smb2_read_state *state = tevent_req_data(req,
396 struct smbd_smb2_read_state);
397 files_struct *fsp = state->fsp;
399 if (nread < 0) {
400 NTSTATUS status = map_nt_error_from_unix(err);
402 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
403 "Error = %s (NTSTATUS %s)\n",
404 fsp_str_dbg(fsp),
405 (int)nread,
406 strerror(err),
407 nt_errstr(status)));
409 return status;
411 if (nread == 0 && state->in_length != 0) {
412 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
413 fsp_str_dbg(fsp)));
414 return NT_STATUS_END_OF_FILE;
417 if (nread < state->in_minimum) {
418 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
419 "minimum requested %u. Returning end of file\n",
420 fsp_str_dbg(fsp),
421 (int)nread,
422 (unsigned int)state->in_minimum));
423 return NT_STATUS_END_OF_FILE;
426 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
427 fsp_fnum_dbg(fsp),
428 fsp_str_dbg(fsp),
429 (unsigned long)state->in_length,
430 (unsigned long)state->in_offset,
431 (unsigned long)nread));
433 state->out_data.length = nread;
434 state->out_remaining = 0;
436 return NT_STATUS_OK;
439 static bool smbd_smb2_read_cancel(struct tevent_req *req)
441 struct smbd_smb2_read_state *state =
442 tevent_req_data(req,
443 struct smbd_smb2_read_state);
445 return cancel_smb2_aio(state->smbreq);
448 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
449 struct tevent_context *ev,
450 struct smbd_smb2_request *smb2req,
451 struct files_struct *fsp,
452 uint8_t in_flags,
453 uint32_t in_length,
454 uint64_t in_offset,
455 uint32_t in_minimum,
456 uint32_t in_remaining)
458 NTSTATUS status;
459 struct tevent_req *req = NULL;
460 struct smbd_smb2_read_state *state = NULL;
461 struct smb_request *smbreq = NULL;
462 connection_struct *conn = smb2req->tcon->compat;
463 ssize_t nread = -1;
464 struct lock_struct lock;
465 int saved_errno;
467 req = tevent_req_create(mem_ctx, &state,
468 struct smbd_smb2_read_state);
469 if (req == NULL) {
470 return NULL;
472 state->smb2req = smb2req;
473 state->in_flags = in_flags;
474 state->in_length = in_length;
475 state->in_offset = in_offset;
476 state->in_minimum = in_minimum;
477 state->out_data = data_blob_null;
478 state->out_remaining = 0;
480 DEBUG(10,("smbd_smb2_read: %s - %s\n",
481 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
483 smbreq = smbd_smb2_fake_smb_request(smb2req, fsp);
484 if (tevent_req_nomem(smbreq, req)) {
485 return tevent_req_post(req, ev);
487 state->smbreq = smbreq;
489 if (fsp->fsp_flags.is_directory) {
490 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
491 return tevent_req_post(req, ev);
494 state->fsp = fsp;
496 if (IS_IPC(smbreq->conn)) {
497 struct tevent_req *subreq = NULL;
499 state->out_data = data_blob_talloc(state, NULL, in_length);
500 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
501 return tevent_req_post(req, ev);
504 if (!fsp_is_np(fsp)) {
505 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
506 return tevent_req_post(req, ev);
509 subreq = np_read_send(state, ev,
510 fsp->fake_file_handle,
511 state->out_data.data,
512 state->out_data.length);
513 if (tevent_req_nomem(subreq, req)) {
514 return tevent_req_post(req, ev);
516 tevent_req_set_callback(subreq,
517 smbd_smb2_read_pipe_done,
518 req);
519 return req;
522 if (!CHECK_READ_SMB2(fsp)) {
523 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
524 return tevent_req_post(req, ev);
527 status = schedule_smb2_aio_read(fsp->conn,
528 smbreq,
529 fsp,
530 state,
531 &state->out_data,
532 (off_t)in_offset,
533 (size_t)in_length);
535 if (NT_STATUS_IS_OK(status)) {
537 * Doing an async read, allow this
538 * request to be canceled
540 tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
541 return req;
544 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
545 /* Real error in setting up aio. Fail. */
546 tevent_req_nterror(req, status);
547 return tevent_req_post(req, ev);
550 /* Fallback to synchronous. */
552 init_strict_lock_struct(fsp,
553 fsp->op->global->open_persistent_id,
554 in_offset,
555 in_length,
556 READ_LOCK,
557 lp_posix_cifsu_locktype(fsp),
558 &lock);
560 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
561 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
562 return tevent_req_post(req, ev);
565 /* Try sendfile in preference. */
566 status = schedule_smb2_sendfile_read(smb2req, state);
567 if (NT_STATUS_IS_OK(status)) {
568 tevent_req_done(req);
569 return tevent_req_post(req, ev);
570 } else {
571 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
572 tevent_req_nterror(req, status);
573 return tevent_req_post(req, ev);
577 /* Ok, read into memory. Allocate the out buffer. */
578 state->out_data = data_blob_talloc(state, NULL, in_length);
579 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
580 return tevent_req_post(req, ev);
583 nread = read_file(fsp,
584 (char *)state->out_data.data,
585 in_offset,
586 in_length);
588 saved_errno = errno;
590 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
591 "len=%llu returned %lld\n",
592 fsp_str_dbg(fsp),
593 fsp_fnum_dbg(fsp),
594 (unsigned long long)in_offset,
595 (unsigned long long)in_length,
596 (long long)nread));
598 status = smb2_read_complete(req, nread, saved_errno);
599 if (!NT_STATUS_IS_OK(status)) {
600 tevent_req_nterror(req, status);
601 } else {
602 /* Success. */
603 tevent_req_done(req);
605 return tevent_req_post(req, ev);
608 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
610 struct tevent_req *req = tevent_req_callback_data(subreq,
611 struct tevent_req);
612 struct smbd_smb2_read_state *state = tevent_req_data(req,
613 struct smbd_smb2_read_state);
614 NTSTATUS status;
615 ssize_t nread = -1;
616 bool is_data_outstanding;
618 status = np_read_recv(subreq, &nread, &is_data_outstanding);
619 TALLOC_FREE(subreq);
620 if (!NT_STATUS_IS_OK(status)) {
621 NTSTATUS old = status;
622 status = nt_status_np_pipe(old);
623 tevent_req_nterror(req, status);
624 return;
627 if (nread == 0 && state->out_data.length != 0) {
628 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
629 return;
632 state->out_data.length = nread;
633 state->out_remaining = 0;
636 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
637 * handle it in SMB1 pipe_read_andx_done().
640 tevent_req_done(req);
643 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
644 TALLOC_CTX *mem_ctx,
645 DATA_BLOB *out_data,
646 uint32_t *out_remaining)
648 NTSTATUS status;
649 struct smbd_smb2_read_state *state = tevent_req_data(req,
650 struct smbd_smb2_read_state);
652 if (tevent_req_is_nterror(req, &status)) {
653 tevent_req_received(req);
654 return status;
657 *out_data = state->out_data;
658 talloc_steal(mem_ctx, out_data->data);
659 *out_remaining = state->out_remaining;
661 if (state->out_headers.length > 0) {
662 talloc_steal(mem_ctx, state);
663 talloc_set_destructor(state, smb2_smb2_read_state_deny_destructor);
664 tevent_req_received(req);
665 state->smb2req->queue_entry.sendfile_header = &state->out_headers;
666 state->smb2req->queue_entry.sendfile_body_size = state->in_length;
667 talloc_set_destructor(state, smb2_sendfile_send_data);
668 } else {
669 tevent_req_received(req);
672 return NT_STATUS_OK;