smbd: Return "blocker_pid" from do_lock()
[Samba.git] / source3 / smbd / smb2_read.c
bloba7d2496bc6f30e4a7f425fa9fe9a0d53116f1b66
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 DATA_BLOB outbody;
120 DATA_BLOB outdyn;
121 uint8_t out_data_offset;
122 DATA_BLOB out_data_buffer = data_blob_null;
123 uint32_t out_data_remaining = 0;
124 NTSTATUS status;
125 NTSTATUS error; /* transport error */
127 status = smbd_smb2_read_recv(subreq,
128 req,
129 &out_data_buffer,
130 &out_data_remaining);
131 TALLOC_FREE(subreq);
132 if (!NT_STATUS_IS_OK(status)) {
133 error = smbd_smb2_request_error(req, status);
134 if (!NT_STATUS_IS_OK(error)) {
135 smbd_server_connection_terminate(req->xconn,
136 nt_errstr(error));
137 return;
139 return;
142 out_data_offset = SMB2_HDR_BODY + 0x10;
144 outbody = smbd_smb2_generate_outbody(req, 0x10);
145 if (outbody.data == NULL) {
146 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
147 if (!NT_STATUS_IS_OK(error)) {
148 smbd_server_connection_terminate(req->xconn,
149 nt_errstr(error));
150 return;
152 return;
155 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
156 SCVAL(outbody.data, 0x02,
157 out_data_offset); /* data offset */
158 SCVAL(outbody.data, 0x03, 0); /* reserved */
159 SIVAL(outbody.data, 0x04,
160 out_data_buffer.length); /* data length */
161 SIVAL(outbody.data, 0x08,
162 out_data_remaining); /* data remaining */
163 SIVAL(outbody.data, 0x0C, 0); /* reserved */
165 outdyn = out_data_buffer;
167 error = smbd_smb2_request_done(req, outbody, &outdyn);
168 if (!NT_STATUS_IS_OK(error)) {
169 smbd_server_connection_terminate(req->xconn,
170 nt_errstr(error));
171 return;
175 struct smbd_smb2_read_state {
176 struct smbd_smb2_request *smb2req;
177 struct smb_request *smbreq;
178 files_struct *fsp;
179 uint8_t in_flags;
180 uint32_t in_length;
181 uint64_t in_offset;
182 uint32_t in_minimum;
183 DATA_BLOB out_headers;
184 uint8_t _out_hdr_buf[NBT_HDR_SIZE + SMB2_HDR_BODY + 0x10];
185 DATA_BLOB out_data;
186 uint32_t out_remaining;
189 static int smb2_smb2_read_state_deny_destructor(struct smbd_smb2_read_state *state)
191 return -1;
194 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
195 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
197 struct lock_struct lock;
198 uint32_t in_length = state->in_length;
199 uint64_t in_offset = state->in_offset;
200 files_struct *fsp = state->fsp;
201 const DATA_BLOB *hdr = state->smb2req->queue_entry.sendfile_header;
202 NTSTATUS *pstatus = state->smb2req->queue_entry.sendfile_status;
203 struct smbXsrv_connection *xconn = state->smb2req->xconn;
204 ssize_t nread;
205 ssize_t ret;
206 int saved_errno;
208 nread = SMB_VFS_SENDFILE(xconn->transport.sock,
209 fsp,
210 hdr,
211 in_offset,
212 in_length);
213 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
214 (int)nread,
215 fsp_str_dbg(fsp) ));
217 if (nread == -1) {
218 saved_errno = errno;
221 * Returning ENOSYS means no data at all was sent.
222 Do this as a normal read. */
223 if (errno == ENOSYS) {
224 goto normal_read;
227 if (errno == ENOTSUP) {
228 set_use_sendfile(SNUM(fsp->conn), false);
229 DBG_WARNING("Disabling sendfile use as sendfile is "
230 "not supported by the system\n");
231 goto normal_read;
234 if (errno == EINTR) {
236 * Special hack for broken Linux with no working sendfile. If we
237 * return EINTR we sent the header but not the rest of the data.
238 * Fake this up by doing read/write calls.
240 set_use_sendfile(SNUM(fsp->conn), false);
241 nread = fake_sendfile(xconn, fsp, in_offset, in_length);
242 if (nread == -1) {
243 saved_errno = errno;
244 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
245 "failed for file %s (%s) for client %s. "
246 "Terminating\n",
247 fsp_str_dbg(fsp), strerror(saved_errno),
248 smbXsrv_connection_dbg(xconn)));
249 *pstatus = map_nt_error_from_unix_common(saved_errno);
250 return 0;
252 goto out;
255 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
256 "%s (%s) for client %s. 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;
261 } else if (nread == 0) {
263 * Some sendfile implementations return 0 to indicate
264 * that there was a short read, but nothing was
265 * actually written to the socket. In this case,
266 * fallback to the normal read path so the header gets
267 * the correct byte count.
269 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
270 "falling back to the normal read: %s\n",
271 fsp_str_dbg(fsp)));
272 goto normal_read;
276 * We got a short read
278 goto out;
280 normal_read:
281 /* Send out the header. */
282 ret = write_data(xconn->transport.sock,
283 (const char *)hdr->data, hdr->length);
284 if (ret != hdr->length) {
285 saved_errno = errno;
286 DEBUG(0,("smb2_sendfile_send_data: write_data failed for file "
287 "%s (%s) for client %s. Terminating\n",
288 fsp_str_dbg(fsp), strerror(saved_errno),
289 smbXsrv_connection_dbg(xconn)));
290 *pstatus = map_nt_error_from_unix_common(saved_errno);
291 return 0;
293 nread = fake_sendfile(xconn, fsp, in_offset, in_length);
294 if (nread == -1) {
295 saved_errno = errno;
296 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
297 "failed for file %s (%s) for client %s. "
298 "Terminating\n",
299 fsp_str_dbg(fsp), strerror(saved_errno),
300 smbXsrv_connection_dbg(xconn)));
301 *pstatus = map_nt_error_from_unix_common(saved_errno);
302 return 0;
305 out:
307 if (nread < in_length) {
308 ret = sendfile_short_send(xconn, fsp, nread,
309 hdr->length, in_length);
310 if (ret == -1) {
311 saved_errno = errno;
312 DEBUG(0,("%s: sendfile_short_send "
313 "failed for file %s (%s) for client %s. "
314 "Terminating\n",
315 __func__,
316 fsp_str_dbg(fsp), strerror(saved_errno),
317 smbXsrv_connection_dbg(xconn)));
318 *pstatus = map_nt_error_from_unix_common(saved_errno);
319 return 0;
323 init_strict_lock_struct(fsp,
324 fsp->op->global->open_persistent_id,
325 in_offset,
326 in_length,
327 READ_LOCK,
328 &lock);
330 *pstatus = NT_STATUS_OK;
331 return 0;
334 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
335 struct smbd_smb2_read_state *state)
337 files_struct *fsp = state->fsp;
340 * We cannot use sendfile if...
341 * We were not configured to do so OR
342 * Signing is active OR
343 * This is a compound SMB2 operation OR
344 * fsp is a STREAM file OR
345 * We're using a write cache OR
346 * It's not a regular file OR
347 * Requested offset is greater than file size OR
348 * there's not enough data in the file.
349 * Phew :-). Luckily this means most
350 * reads on most normal files. JRA.
353 if (!lp__use_sendfile(SNUM(fsp->conn)) ||
354 smb2req->do_signing ||
355 smb2req->do_encryption ||
356 smbd_smb2_is_compound(smb2req) ||
357 (fsp->base_fsp != NULL) ||
358 (fsp->wcp != NULL) ||
359 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
360 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
361 (fsp->fsp_name->st.st_ex_size < state->in_offset + state->in_length))
363 return NT_STATUS_RETRY;
366 /* We've already checked there's this amount of data
367 to read. */
368 state->out_data.length = state->in_length;
369 state->out_remaining = 0;
371 state->out_headers = data_blob_const(state->_out_hdr_buf,
372 sizeof(state->_out_hdr_buf));
373 return NT_STATUS_OK;
376 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
378 /*******************************************************************
379 Common read complete processing function for both synchronous and
380 asynchronous reads.
381 *******************************************************************/
383 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
385 struct smbd_smb2_read_state *state = tevent_req_data(req,
386 struct smbd_smb2_read_state);
387 files_struct *fsp = state->fsp;
389 if (nread < 0) {
390 NTSTATUS status = map_nt_error_from_unix(err);
392 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
393 "Error = %s (NTSTATUS %s)\n",
394 fsp_str_dbg(fsp),
395 (int)nread,
396 strerror(err),
397 nt_errstr(status)));
399 return status;
401 if (nread == 0 && state->in_length != 0) {
402 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
403 fsp_str_dbg(fsp)));
404 return NT_STATUS_END_OF_FILE;
407 if (nread < state->in_minimum) {
408 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
409 "minimum requested %u. Returning end of file\n",
410 fsp_str_dbg(fsp),
411 (int)nread,
412 (unsigned int)state->in_minimum));
413 return NT_STATUS_END_OF_FILE;
416 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
417 fsp_fnum_dbg(fsp),
418 fsp_str_dbg(fsp),
419 (unsigned long)state->in_length,
420 (unsigned long)state->in_offset,
421 (unsigned long)nread));
423 state->out_data.length = nread;
424 state->out_remaining = 0;
426 return NT_STATUS_OK;
429 static bool smbd_smb2_read_cancel(struct tevent_req *req)
431 struct smbd_smb2_read_state *state =
432 tevent_req_data(req,
433 struct smbd_smb2_read_state);
435 return cancel_smb2_aio(state->smbreq);
438 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
439 struct tevent_context *ev,
440 struct smbd_smb2_request *smb2req,
441 struct files_struct *fsp,
442 uint8_t in_flags,
443 uint32_t in_length,
444 uint64_t in_offset,
445 uint32_t in_minimum,
446 uint32_t in_remaining)
448 NTSTATUS status;
449 struct tevent_req *req = NULL;
450 struct smbd_smb2_read_state *state = NULL;
451 struct smb_request *smbreq = NULL;
452 connection_struct *conn = smb2req->tcon->compat;
453 ssize_t nread = -1;
454 struct lock_struct lock;
455 int saved_errno;
457 req = tevent_req_create(mem_ctx, &state,
458 struct smbd_smb2_read_state);
459 if (req == NULL) {
460 return NULL;
462 state->smb2req = smb2req;
463 state->in_flags = in_flags;
464 state->in_length = in_length;
465 state->in_offset = in_offset;
466 state->in_minimum = in_minimum;
467 state->out_data = data_blob_null;
468 state->out_remaining = 0;
470 DEBUG(10,("smbd_smb2_read: %s - %s\n",
471 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
473 smbreq = smbd_smb2_fake_smb_request(smb2req);
474 if (tevent_req_nomem(smbreq, req)) {
475 return tevent_req_post(req, ev);
477 state->smbreq = smbreq;
479 if (fsp->is_directory) {
480 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
481 return tevent_req_post(req, ev);
484 state->fsp = fsp;
486 if (IS_IPC(smbreq->conn)) {
487 struct tevent_req *subreq = NULL;
489 state->out_data = data_blob_talloc(state, NULL, in_length);
490 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
491 return tevent_req_post(req, ev);
494 if (!fsp_is_np(fsp)) {
495 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
496 return tevent_req_post(req, ev);
499 subreq = np_read_send(state, ev,
500 fsp->fake_file_handle,
501 state->out_data.data,
502 state->out_data.length);
503 if (tevent_req_nomem(subreq, req)) {
504 return tevent_req_post(req, ev);
506 tevent_req_set_callback(subreq,
507 smbd_smb2_read_pipe_done,
508 req);
509 return req;
512 if (!CHECK_READ_SMB2(fsp)) {
513 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
514 return tevent_req_post(req, ev);
517 status = schedule_smb2_aio_read(fsp->conn,
518 smbreq,
519 fsp,
520 state,
521 &state->out_data,
522 (off_t)in_offset,
523 (size_t)in_length);
525 if (NT_STATUS_IS_OK(status)) {
527 * Doing an async read, allow this
528 * request to be canceled
530 tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
531 return req;
534 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
535 /* Real error in setting up aio. Fail. */
536 tevent_req_nterror(req, status);
537 return tevent_req_post(req, ev);
540 /* Fallback to synchronous. */
542 init_strict_lock_struct(fsp,
543 fsp->op->global->open_persistent_id,
544 in_offset,
545 in_length,
546 READ_LOCK,
547 &lock);
549 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
550 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
551 return tevent_req_post(req, ev);
554 /* Try sendfile in preference. */
555 status = schedule_smb2_sendfile_read(smb2req, state);
556 if (NT_STATUS_IS_OK(status)) {
557 tevent_req_done(req);
558 return tevent_req_post(req, ev);
559 } else {
560 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
561 tevent_req_nterror(req, status);
562 return tevent_req_post(req, ev);
566 /* Ok, read into memory. Allocate the out buffer. */
567 state->out_data = data_blob_talloc(state, NULL, in_length);
568 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
569 return tevent_req_post(req, ev);
572 nread = read_file(fsp,
573 (char *)state->out_data.data,
574 in_offset,
575 in_length);
577 saved_errno = errno;
579 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
580 "len=%llu returned %lld\n",
581 fsp_str_dbg(fsp),
582 fsp_fnum_dbg(fsp),
583 (unsigned long long)in_offset,
584 (unsigned long long)in_length,
585 (long long)nread));
587 status = smb2_read_complete(req, nread, saved_errno);
588 if (!NT_STATUS_IS_OK(status)) {
589 tevent_req_nterror(req, status);
590 } else {
591 /* Success. */
592 tevent_req_done(req);
594 return tevent_req_post(req, ev);
597 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
599 struct tevent_req *req = tevent_req_callback_data(subreq,
600 struct tevent_req);
601 struct smbd_smb2_read_state *state = tevent_req_data(req,
602 struct smbd_smb2_read_state);
603 NTSTATUS status;
604 ssize_t nread = -1;
605 bool is_data_outstanding;
607 status = np_read_recv(subreq, &nread, &is_data_outstanding);
608 TALLOC_FREE(subreq);
609 if (!NT_STATUS_IS_OK(status)) {
610 NTSTATUS old = status;
611 status = nt_status_np_pipe(old);
612 tevent_req_nterror(req, status);
613 return;
616 if (nread == 0 && state->out_data.length != 0) {
617 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
618 return;
621 state->out_data.length = nread;
622 state->out_remaining = 0;
625 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
626 * handle it in SMB1 pipe_read_andx_done().
629 tevent_req_done(req);
632 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
633 TALLOC_CTX *mem_ctx,
634 DATA_BLOB *out_data,
635 uint32_t *out_remaining)
637 NTSTATUS status;
638 struct smbd_smb2_read_state *state = tevent_req_data(req,
639 struct smbd_smb2_read_state);
641 if (tevent_req_is_nterror(req, &status)) {
642 tevent_req_received(req);
643 return status;
646 *out_data = state->out_data;
647 talloc_steal(mem_ctx, out_data->data);
648 *out_remaining = state->out_remaining;
650 if (state->out_headers.length > 0) {
651 talloc_steal(mem_ctx, state);
652 talloc_set_destructor(state, smb2_smb2_read_state_deny_destructor);
653 tevent_req_received(req);
654 state->smb2req->queue_entry.sendfile_header = &state->out_headers;
655 talloc_set_destructor(state, smb2_sendfile_send_data);
656 } else {
657 tevent_req_received(req);
660 return NT_STATUS_OK;