s3:vlp: fix build on OS X
[Samba.git] / source3 / smbd / smb2_read.c
blob289cbc020842ec7f75e4027bc8393a0ba5835fe0
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_smbpid,
35 uint32_t in_length,
36 uint64_t in_offset,
37 uint32_t in_minimum,
38 uint32_t in_remaining);
39 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
40 TALLOC_CTX *mem_ctx,
41 DATA_BLOB *out_data,
42 uint32_t *out_remaining);
44 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
45 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req)
47 NTSTATUS status;
48 const uint8_t *inhdr;
49 const uint8_t *inbody;
50 int i = req->current_idx;
51 uint32_t in_smbpid;
52 uint32_t in_length;
53 uint64_t in_offset;
54 uint64_t in_file_id_persistent;
55 uint64_t in_file_id_volatile;
56 struct files_struct *in_fsp;
57 uint32_t in_minimum_count;
58 uint32_t in_remaining_bytes;
59 struct tevent_req *subreq;
61 status = smbd_smb2_request_verify_sizes(req, 0x31);
62 if (!NT_STATUS_IS_OK(status)) {
63 return smbd_smb2_request_error(req, status);
65 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
66 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
68 in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
70 in_length = IVAL(inbody, 0x04);
71 in_offset = BVAL(inbody, 0x08);
72 in_file_id_persistent = BVAL(inbody, 0x10);
73 in_file_id_volatile = BVAL(inbody, 0x18);
74 in_minimum_count = IVAL(inbody, 0x20);
75 in_remaining_bytes = IVAL(inbody, 0x28);
77 /* check the max read size */
78 if (in_length > req->sconn->smb2.max_read) {
79 DEBUG(2,("smbd_smb2_request_process_read: "
80 "client ignored max read: %s: 0x%08X: 0x%08X\n",
81 __location__, in_length, req->sconn->smb2.max_read));
82 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
85 status = smbd_smb2_request_verify_creditcharge(req, in_length);
86 if (!NT_STATUS_IS_OK(status)) {
87 return smbd_smb2_request_error(req, status);
90 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
91 if (in_fsp == NULL) {
92 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
95 subreq = smbd_smb2_read_send(req, req->sconn->ev_ctx,
96 req, in_fsp,
97 in_smbpid,
98 in_length,
99 in_offset,
100 in_minimum_count,
101 in_remaining_bytes);
102 if (subreq == NULL) {
103 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
105 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
107 return smbd_smb2_request_pending_queue(req, subreq, 500);
110 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
112 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
113 struct smbd_smb2_request);
114 DATA_BLOB outbody;
115 DATA_BLOB outdyn;
116 uint8_t out_data_offset;
117 DATA_BLOB out_data_buffer = data_blob_null;
118 uint32_t out_data_remaining = 0;
119 NTSTATUS status;
120 NTSTATUS error; /* transport error */
122 status = smbd_smb2_read_recv(subreq,
123 req,
124 &out_data_buffer,
125 &out_data_remaining);
126 TALLOC_FREE(subreq);
127 if (!NT_STATUS_IS_OK(status)) {
128 error = smbd_smb2_request_error(req, status);
129 if (!NT_STATUS_IS_OK(error)) {
130 smbd_server_connection_terminate(req->sconn,
131 nt_errstr(error));
132 return;
134 return;
137 out_data_offset = SMB2_HDR_BODY + 0x10;
139 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
140 if (outbody.data == NULL) {
141 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
142 if (!NT_STATUS_IS_OK(error)) {
143 smbd_server_connection_terminate(req->sconn,
144 nt_errstr(error));
145 return;
147 return;
150 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
151 SCVAL(outbody.data, 0x02,
152 out_data_offset); /* data offset */
153 SCVAL(outbody.data, 0x03, 0); /* reserved */
154 SIVAL(outbody.data, 0x04,
155 out_data_buffer.length); /* data length */
156 SIVAL(outbody.data, 0x08,
157 out_data_remaining); /* data remaining */
158 SIVAL(outbody.data, 0x0C, 0); /* reserved */
160 outdyn = out_data_buffer;
162 error = smbd_smb2_request_done(req, outbody, &outdyn);
163 if (!NT_STATUS_IS_OK(error)) {
164 smbd_server_connection_terminate(req->sconn,
165 nt_errstr(error));
166 return;
170 struct smbd_smb2_read_state {
171 struct smbd_smb2_request *smb2req;
172 struct smb_request *smbreq;
173 files_struct *fsp;
174 uint32_t in_length;
175 uint64_t in_offset;
176 uint32_t in_minimum;
177 DATA_BLOB out_data;
178 uint32_t out_remaining;
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 ssize_t nread;
190 nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
191 fsp,
192 NULL,
193 in_offset,
194 in_length);
195 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
196 (int)nread,
197 fsp_str_dbg(fsp) ));
199 if (nread == -1) {
200 if (errno == ENOSYS || errno == EINTR) {
202 * Special hack for broken systems with no working
203 * sendfile. Fake this up by doing read/write calls.
205 set_use_sendfile(SNUM(fsp->conn), false);
206 nread = fake_sendfile(fsp, in_offset, in_length);
207 if (nread == -1) {
208 DEBUG(0,("smb2_sendfile_send_data: "
209 "fake_sendfile failed for "
210 "file %s (%s).\n",
211 fsp_str_dbg(fsp),
212 strerror(errno)));
213 exit_server_cleanly("smb2_sendfile_send_data: "
214 "fake_sendfile failed");
216 goto out;
219 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
220 "%s (%s). Terminating\n",
221 fsp_str_dbg(fsp),
222 strerror(errno)));
223 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
224 } else if (nread == 0) {
226 * Some sendfile implementations return 0 to indicate
227 * that there was a short read, but nothing was
228 * actually written to the socket. In this case,
229 * fallback to the normal read path so the header gets
230 * the correct byte count.
232 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
233 "falling back to the normal read: %s\n",
234 fsp_str_dbg(fsp)));
236 nread = fake_sendfile(fsp, in_offset, in_length);
237 if (nread == -1) {
238 DEBUG(0,("smb2_sendfile_send_data: "
239 "fake_sendfile failed for file "
240 "%s (%s). Terminating\n",
241 fsp_str_dbg(fsp),
242 strerror(errno)));
243 exit_server_cleanly("smb2_sendfile_send_data: "
244 "fake_sendfile failed");
248 out:
250 if (nread < in_length) {
251 sendfile_short_send(fsp, nread, 0, in_length);
254 init_strict_lock_struct(fsp,
255 fsp->op->global->open_persistent_id,
256 in_offset,
257 in_length,
258 READ_LOCK,
259 &lock);
261 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
262 return 0;
265 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
266 struct smbd_smb2_read_state *state)
268 struct smbd_smb2_read_state *state_copy = NULL;
269 files_struct *fsp = state->fsp;
272 * We cannot use sendfile if...
273 * We were not configured to do so OR
274 * Signing is active OR
275 * This is a compound SMB2 operation OR
276 * fsp is a STREAM file OR
277 * We're using a write cache OR
278 * It's not a regular file OR
279 * Requested offset is greater than file size OR
280 * there's not enough data in the file.
281 * Phew :-). Luckily this means most
282 * reads on most normal files. JRA.
285 if (!lp__use_sendfile(SNUM(fsp->conn)) ||
286 smb2req->do_signing ||
287 smb2req->in.vector_count != 4 ||
288 (fsp->base_fsp != NULL) ||
289 (fsp->wcp != NULL) ||
290 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
291 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
292 (fsp->fsp_name->st.st_ex_size < state->in_offset +
293 state->in_length)) {
294 return NT_STATUS_RETRY;
297 /* We've already checked there's this amount of data
298 to read. */
299 state->out_data.length = state->in_length;
300 state->out_remaining = 0;
302 /* Make a copy of state attached to the smb2req. Attach
303 the destructor here as this will trigger the sendfile
304 call when the request is destroyed. */
305 state_copy = talloc(smb2req, struct smbd_smb2_read_state);
306 if (!state_copy) {
307 return NT_STATUS_NO_MEMORY;
309 *state_copy = *state;
310 talloc_set_destructor(state_copy, smb2_sendfile_send_data);
311 return NT_STATUS_OK;
314 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
316 /*******************************************************************
317 Common read complete processing function for both synchronous and
318 asynchronous reads.
319 *******************************************************************/
321 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
323 struct smbd_smb2_read_state *state = tevent_req_data(req,
324 struct smbd_smb2_read_state);
325 files_struct *fsp = state->fsp;
327 if (nread < 0) {
328 NTSTATUS status = map_nt_error_from_unix(err);
330 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
331 "Error = %s (NTSTATUS %s)\n",
332 fsp_str_dbg(fsp),
333 (int)nread,
334 strerror(err),
335 nt_errstr(status)));
337 return status;
339 if (nread == 0 && state->in_length != 0) {
340 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
341 fsp_str_dbg(fsp)));
342 return NT_STATUS_END_OF_FILE;
345 if (nread < state->in_minimum) {
346 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
347 "minimum requested %u. Returning end of file\n",
348 fsp_str_dbg(fsp),
349 (int)nread,
350 (unsigned int)state->in_minimum));
351 return NT_STATUS_END_OF_FILE;
354 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
355 fsp_fnum_dbg(fsp),
356 fsp_str_dbg(fsp),
357 (unsigned long)state->in_length,
358 (unsigned long)state->in_offset,
359 (unsigned long)nread));
361 state->out_data.length = nread;
362 state->out_remaining = 0;
364 return NT_STATUS_OK;
367 static bool smbd_smb2_read_cancel(struct tevent_req *req)
369 struct smbd_smb2_read_state *state =
370 tevent_req_data(req,
371 struct smbd_smb2_read_state);
373 state->smb2req->cancelled = true;
375 return cancel_smb2_aio(state->smbreq);
378 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
379 struct tevent_context *ev,
380 struct smbd_smb2_request *smb2req,
381 struct files_struct *fsp,
382 uint32_t in_smbpid,
383 uint32_t in_length,
384 uint64_t in_offset,
385 uint32_t in_minimum,
386 uint32_t in_remaining)
388 NTSTATUS status;
389 struct tevent_req *req = NULL;
390 struct smbd_smb2_read_state *state = NULL;
391 struct smb_request *smbreq = NULL;
392 connection_struct *conn = smb2req->tcon->compat;
393 ssize_t nread = -1;
394 struct lock_struct lock;
395 int saved_errno;
397 req = tevent_req_create(mem_ctx, &state,
398 struct smbd_smb2_read_state);
399 if (req == NULL) {
400 return NULL;
402 state->smb2req = smb2req;
403 state->in_length = in_length;
404 state->in_offset = in_offset;
405 state->in_minimum = in_minimum;
406 state->out_data = data_blob_null;
407 state->out_remaining = 0;
409 DEBUG(10,("smbd_smb2_read: %s - %s\n",
410 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
412 smbreq = smbd_smb2_fake_smb_request(smb2req);
413 if (tevent_req_nomem(smbreq, req)) {
414 return tevent_req_post(req, ev);
416 state->smbreq = smbreq;
418 if (fsp->is_directory) {
419 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
420 return tevent_req_post(req, ev);
423 state->fsp = fsp;
425 if (IS_IPC(smbreq->conn)) {
426 struct tevent_req *subreq = NULL;
428 state->out_data = data_blob_talloc(state, NULL, in_length);
429 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
430 return tevent_req_post(req, ev);
433 if (!fsp_is_np(fsp)) {
434 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
435 return tevent_req_post(req, ev);
438 subreq = np_read_send(state, ev,
439 fsp->fake_file_handle,
440 state->out_data.data,
441 state->out_data.length);
442 if (tevent_req_nomem(subreq, req)) {
443 return tevent_req_post(req, ev);
445 tevent_req_set_callback(subreq,
446 smbd_smb2_read_pipe_done,
447 req);
448 return req;
451 if (!CHECK_READ(fsp, smbreq)) {
452 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
453 return tevent_req_post(req, ev);
456 status = schedule_smb2_aio_read(fsp->conn,
457 smbreq,
458 fsp,
459 state,
460 &state->out_data,
461 (off_t)in_offset,
462 (size_t)in_length);
464 if (NT_STATUS_IS_OK(status)) {
466 * Doing an async read, allow this
467 * request to be canceled
469 tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
470 return req;
473 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
474 /* Real error in setting up aio. Fail. */
475 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
476 return tevent_req_post(req, ev);
479 /* Fallback to synchronous. */
481 init_strict_lock_struct(fsp,
482 fsp->op->global->open_persistent_id,
483 in_offset,
484 in_length,
485 READ_LOCK,
486 &lock);
488 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
489 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
490 return tevent_req_post(req, ev);
493 /* Try sendfile in preference. */
494 status = schedule_smb2_sendfile_read(smb2req, state);
495 if (NT_STATUS_IS_OK(status)) {
496 tevent_req_done(req);
497 return tevent_req_post(req, ev);
498 } else {
499 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
500 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
501 tevent_req_nterror(req, status);
502 return tevent_req_post(req, ev);
506 /* Ok, read into memory. Allocate the out buffer. */
507 state->out_data = data_blob_talloc(state, NULL, in_length);
508 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
509 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
510 return tevent_req_post(req, ev);
513 nread = read_file(fsp,
514 (char *)state->out_data.data,
515 in_offset,
516 in_length);
518 saved_errno = errno;
520 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
522 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
523 "len=%llu returned %lld\n",
524 fsp_str_dbg(fsp),
525 fsp_fnum_dbg(fsp),
526 (unsigned long long)in_offset,
527 (unsigned long long)in_length,
528 (long long)nread));
530 status = smb2_read_complete(req, nread, saved_errno);
531 if (!NT_STATUS_IS_OK(status)) {
532 tevent_req_nterror(req, status);
533 } else {
534 /* Success. */
535 tevent_req_done(req);
537 return tevent_req_post(req, ev);
540 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
542 struct tevent_req *req = tevent_req_callback_data(subreq,
543 struct tevent_req);
544 struct smbd_smb2_read_state *state = tevent_req_data(req,
545 struct smbd_smb2_read_state);
546 NTSTATUS status;
547 ssize_t nread = -1;
548 bool is_data_outstanding;
550 status = np_read_recv(subreq, &nread, &is_data_outstanding);
551 TALLOC_FREE(subreq);
552 if (!NT_STATUS_IS_OK(status)) {
553 NTSTATUS old = status;
554 status = nt_status_np_pipe(old);
555 tevent_req_nterror(req, status);
556 return;
559 if (nread == 0 && state->out_data.length != 0) {
560 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
561 return;
564 state->out_data.length = nread;
565 state->out_remaining = 0;
568 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
569 * handle it in SMB1 pipe_read_andx_done().
572 tevent_req_done(req);
575 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
576 TALLOC_CTX *mem_ctx,
577 DATA_BLOB *out_data,
578 uint32_t *out_remaining)
580 NTSTATUS status;
581 struct smbd_smb2_read_state *state = tevent_req_data(req,
582 struct smbd_smb2_read_state);
584 if (tevent_req_is_nterror(req, &status)) {
585 tevent_req_received(req);
586 return status;
589 *out_data = state->out_data;
590 talloc_steal(mem_ctx, out_data->data);
591 *out_remaining = state->out_remaining;
593 tevent_req_received(req);
594 return NT_STATUS_OK;