wafsamba: remove unused variable from copy_and_fix_python_path
[Samba.git] / source3 / smbd / smb2_read.c
blob7b273f8c62b3193c06eccc8d58e3860d5357d42f
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 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;
57 status = smbd_smb2_request_verify_sizes(req, 0x31);
58 if (!NT_STATUS_IS_OK(status)) {
59 return smbd_smb2_request_error(req, status);
61 inbody = SMBD_SMB2_IN_BODY_PTR(req);
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);
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);
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);
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);
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);
97 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
99 return smbd_smb2_request_pending_queue(req, subreq, 500);
102 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
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 */
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;
126 return;
129 out_data_offset = SMB2_HDR_BODY + 0x10;
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;
139 return;
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 */
152 outdyn = out_data_buffer;
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;
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 DATA_BLOB out_data;
171 uint32_t out_remaining;
174 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
175 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
177 struct lock_struct lock;
178 uint32_t in_length = state->in_length;
179 uint64_t in_offset = state->in_offset;
180 files_struct *fsp = state->fsp;
181 const DATA_BLOB *hdr = state->smb2req->queue_entry.sendfile_header;
182 ssize_t nread;
183 ssize_t ret;
185 nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
186 fsp,
187 hdr,
188 in_offset,
189 in_length);
190 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
191 (int)nread,
192 fsp_str_dbg(fsp) ));
194 if (nread == -1) {
196 * Returning ENOSYS means no data at all was sent.
197 Do this as a normal read. */
198 if (errno == ENOSYS) {
199 goto normal_read;
202 if (errno == EINTR) {
204 * Special hack for broken Linux with no working sendfile. If we
205 * return EINTR we sent the header but not the rest of the data.
206 * Fake this up by doing read/write calls.
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");
219 goto out;
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) {
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.
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 goto normal_read;
242 * We got a short read
244 goto out;
246 normal_read:
247 /* Send out the header. */
248 ret = write_data(fsp->conn->sconn->sock,
249 (const char *)hdr->data, hdr->length);
250 if (ret != hdr->length) {
251 char addr[INET6_ADDRSTRLEN];
253 * Try and give an error message saying what
254 * client failed.
256 DEBUG(0, ("smb2_sendfile_send_data: write_data failed "
257 "for client %s. Error %s\n",
258 get_peer_addr(fsp->conn->sconn->sock, addr,
259 sizeof(addr)),
260 strerror(errno)));
262 DEBUG(0,("smb2_sendfile_send_data: write_data failed for file "
263 "%s (%s). Terminating\n", fsp_str_dbg(fsp),
264 strerror(errno)));
265 exit_server_cleanly("smb2_sendfile_send_data: write_data failed");
267 nread = fake_sendfile(fsp, in_offset, in_length);
268 if (nread == -1) {
269 DEBUG(0,("smb2_sendfile_send_data: "
270 "fake_sendfile failed for file "
271 "%s (%s). Terminating\n",
272 fsp_str_dbg(fsp),
273 strerror(errno)));
274 exit_server_cleanly("smb2_sendfile_send_data: "
275 "fake_sendfile failed");
278 out:
280 if (nread < in_length) {
281 sendfile_short_send(fsp, nread, hdr->length, in_length);
284 init_strict_lock_struct(fsp,
285 fsp->op->global->open_persistent_id,
286 in_offset,
287 in_length,
288 READ_LOCK,
289 &lock);
291 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
292 return 0;
295 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
296 struct smbd_smb2_read_state *state)
298 struct smbd_smb2_read_state *state_copy = NULL;
299 files_struct *fsp = state->fsp;
302 * We cannot use sendfile if...
303 * We were not configured to do so OR
304 * Signing is active OR
305 * This is a compound SMB2 operation OR
306 * fsp is a STREAM file OR
307 * We're using a write cache OR
308 * It's not a regular file OR
309 * Requested offset is greater than file size OR
310 * there's not enough data in the file.
311 * Phew :-). Luckily this means most
312 * reads on most normal files. JRA.
315 if (!lp__use_sendfile(SNUM(fsp->conn)) ||
316 smb2req->do_signing ||
317 smb2req->do_encryption ||
318 smb2req->in.vector_count >= (2*SMBD_SMB2_NUM_IOV_PER_REQ) ||
319 (fsp->base_fsp != NULL) ||
320 (fsp->wcp != NULL) ||
321 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
322 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
323 (fsp->fsp_name->st.st_ex_size < state->in_offset + state->in_length))
325 return NT_STATUS_RETRY;
328 /* We've already checked there's this amount of data
329 to read. */
330 state->out_data.length = state->in_length;
331 state->out_remaining = 0;
333 /* Make a copy of state attached to the smb2req. Attach
334 the destructor here as this will trigger the sendfile
335 call when the request is destroyed. */
336 state_copy = talloc(smb2req, struct smbd_smb2_read_state);
337 if (!state_copy) {
338 return NT_STATUS_NO_MEMORY;
340 *state_copy = *state;
341 talloc_set_destructor(state_copy, smb2_sendfile_send_data);
342 state->smb2req->queue_entry.sendfile_header = &state_copy->out_headers;
343 return NT_STATUS_OK;
346 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
348 /*******************************************************************
349 Common read complete processing function for both synchronous and
350 asynchronous reads.
351 *******************************************************************/
353 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
355 struct smbd_smb2_read_state *state = tevent_req_data(req,
356 struct smbd_smb2_read_state);
357 files_struct *fsp = state->fsp;
359 if (nread < 0) {
360 NTSTATUS status = map_nt_error_from_unix(err);
362 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
363 "Error = %s (NTSTATUS %s)\n",
364 fsp_str_dbg(fsp),
365 (int)nread,
366 strerror(err),
367 nt_errstr(status)));
369 return status;
371 if (nread == 0 && state->in_length != 0) {
372 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
373 fsp_str_dbg(fsp)));
374 return NT_STATUS_END_OF_FILE;
377 if (nread < state->in_minimum) {
378 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
379 "minimum requested %u. Returning end of file\n",
380 fsp_str_dbg(fsp),
381 (int)nread,
382 (unsigned int)state->in_minimum));
383 return NT_STATUS_END_OF_FILE;
386 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
387 fsp_fnum_dbg(fsp),
388 fsp_str_dbg(fsp),
389 (unsigned long)state->in_length,
390 (unsigned long)state->in_offset,
391 (unsigned long)nread));
393 state->out_data.length = nread;
394 state->out_remaining = 0;
396 return NT_STATUS_OK;
399 static bool smbd_smb2_read_cancel(struct tevent_req *req)
401 struct smbd_smb2_read_state *state =
402 tevent_req_data(req,
403 struct smbd_smb2_read_state);
405 return cancel_smb2_aio(state->smbreq);
408 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
409 struct tevent_context *ev,
410 struct smbd_smb2_request *smb2req,
411 struct files_struct *fsp,
412 uint32_t in_length,
413 uint64_t in_offset,
414 uint32_t in_minimum,
415 uint32_t in_remaining)
417 NTSTATUS status;
418 struct tevent_req *req = NULL;
419 struct smbd_smb2_read_state *state = NULL;
420 struct smb_request *smbreq = NULL;
421 connection_struct *conn = smb2req->tcon->compat;
422 ssize_t nread = -1;
423 struct lock_struct lock;
424 int saved_errno;
426 req = tevent_req_create(mem_ctx, &state,
427 struct smbd_smb2_read_state);
428 if (req == NULL) {
429 return NULL;
431 state->smb2req = smb2req;
432 state->in_length = in_length;
433 state->in_offset = in_offset;
434 state->in_minimum = in_minimum;
435 state->out_data = data_blob_null;
436 state->out_remaining = 0;
438 DEBUG(10,("smbd_smb2_read: %s - %s\n",
439 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
441 smbreq = smbd_smb2_fake_smb_request(smb2req);
442 if (tevent_req_nomem(smbreq, req)) {
443 return tevent_req_post(req, ev);
445 state->smbreq = smbreq;
447 if (fsp->is_directory) {
448 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
449 return tevent_req_post(req, ev);
452 state->fsp = fsp;
454 if (IS_IPC(smbreq->conn)) {
455 struct tevent_req *subreq = NULL;
457 state->out_data = data_blob_talloc(state, NULL, in_length);
458 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
459 return tevent_req_post(req, ev);
462 if (!fsp_is_np(fsp)) {
463 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
464 return tevent_req_post(req, ev);
467 subreq = np_read_send(state, ev,
468 fsp->fake_file_handle,
469 state->out_data.data,
470 state->out_data.length);
471 if (tevent_req_nomem(subreq, req)) {
472 return tevent_req_post(req, ev);
474 tevent_req_set_callback(subreq,
475 smbd_smb2_read_pipe_done,
476 req);
477 return req;
480 if (!CHECK_READ(fsp, smbreq)) {
481 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
482 return tevent_req_post(req, ev);
485 status = schedule_smb2_aio_read(fsp->conn,
486 smbreq,
487 fsp,
488 state,
489 &state->out_data,
490 (off_t)in_offset,
491 (size_t)in_length);
493 if (NT_STATUS_IS_OK(status)) {
495 * Doing an async read, allow this
496 * request to be canceled
498 tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
499 return req;
502 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
503 /* Real error in setting up aio. Fail. */
504 tevent_req_nterror(req, status);
505 return tevent_req_post(req, ev);
508 /* Fallback to synchronous. */
510 init_strict_lock_struct(fsp,
511 fsp->op->global->open_persistent_id,
512 in_offset,
513 in_length,
514 READ_LOCK,
515 &lock);
517 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
518 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
519 return tevent_req_post(req, ev);
522 /* Try sendfile in preference. */
523 status = schedule_smb2_sendfile_read(smb2req, state);
524 if (NT_STATUS_IS_OK(status)) {
525 tevent_req_done(req);
526 return tevent_req_post(req, ev);
527 } else {
528 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
529 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
530 tevent_req_nterror(req, status);
531 return tevent_req_post(req, ev);
535 /* Ok, read into memory. Allocate the out buffer. */
536 state->out_data = data_blob_talloc(state, NULL, in_length);
537 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
538 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
539 return tevent_req_post(req, ev);
542 nread = read_file(fsp,
543 (char *)state->out_data.data,
544 in_offset,
545 in_length);
547 saved_errno = errno;
549 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
551 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
552 "len=%llu returned %lld\n",
553 fsp_str_dbg(fsp),
554 fsp_fnum_dbg(fsp),
555 (unsigned long long)in_offset,
556 (unsigned long long)in_length,
557 (long long)nread));
559 status = smb2_read_complete(req, nread, saved_errno);
560 if (!NT_STATUS_IS_OK(status)) {
561 tevent_req_nterror(req, status);
562 } else {
563 /* Success. */
564 tevent_req_done(req);
566 return tevent_req_post(req, ev);
569 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
571 struct tevent_req *req = tevent_req_callback_data(subreq,
572 struct tevent_req);
573 struct smbd_smb2_read_state *state = tevent_req_data(req,
574 struct smbd_smb2_read_state);
575 NTSTATUS status;
576 ssize_t nread = -1;
577 bool is_data_outstanding;
579 status = np_read_recv(subreq, &nread, &is_data_outstanding);
580 TALLOC_FREE(subreq);
581 if (!NT_STATUS_IS_OK(status)) {
582 NTSTATUS old = status;
583 status = nt_status_np_pipe(old);
584 tevent_req_nterror(req, status);
585 return;
588 if (nread == 0 && state->out_data.length != 0) {
589 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
590 return;
593 state->out_data.length = nread;
594 state->out_remaining = 0;
597 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
598 * handle it in SMB1 pipe_read_andx_done().
601 tevent_req_done(req);
604 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
605 TALLOC_CTX *mem_ctx,
606 DATA_BLOB *out_data,
607 uint32_t *out_remaining)
609 NTSTATUS status;
610 struct smbd_smb2_read_state *state = tevent_req_data(req,
611 struct smbd_smb2_read_state);
613 if (tevent_req_is_nterror(req, &status)) {
614 tevent_req_received(req);
615 return status;
618 *out_data = state->out_data;
619 talloc_steal(mem_ctx, out_data->data);
620 *out_remaining = state->out_remaining;
622 tevent_req_received(req);
623 return NT_STATUS_OK;