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