s3-torture/denytest.c: replace cli_read_old() with cli_read()
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_read.c
blob8c3a8fd8777ea9ff327ac4f582264bd70b99fdd1
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 uint32_t in_smbpid,
34 uint64_t in_file_id_volatile,
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 const uint8_t *inhdr;
48 const uint8_t *inbody;
49 int i = req->current_idx;
50 size_t expected_body_size = 0x31;
51 size_t body_size;
52 uint32_t in_smbpid;
53 uint32_t in_length;
54 uint64_t in_offset;
55 uint64_t in_file_id_persistent;
56 uint64_t in_file_id_volatile;
57 uint32_t in_minimum_count;
58 uint32_t in_remaining_bytes;
59 struct tevent_req *subreq;
61 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
62 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
63 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
66 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
68 body_size = SVAL(inbody, 0x00);
69 if (body_size != expected_body_size) {
70 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
73 in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
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 > lp_smb2_max_read()) {
84 DEBUG(0,("here:%s: 0x%08X: 0x%08X\n",
85 __location__, in_length, lp_smb2_max_read()));
86 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
89 if (req->compat_chain_fsp) {
90 /* skip check */
91 } else if (in_file_id_persistent != in_file_id_volatile) {
92 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
95 subreq = smbd_smb2_read_send(req,
96 req->sconn->smb2.event_ctx,
97 req,
98 in_smbpid,
99 in_file_id_volatile,
100 in_length,
101 in_offset,
102 in_minimum_count,
103 in_remaining_bytes);
104 if (subreq == NULL) {
105 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
107 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
109 return smbd_smb2_request_pending_queue(req, subreq);
112 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
114 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
115 struct smbd_smb2_request);
116 int i = req->current_idx;
117 uint8_t *outhdr;
118 DATA_BLOB outbody;
119 DATA_BLOB outdyn;
120 uint8_t out_data_offset;
121 DATA_BLOB out_data_buffer = data_blob_null;
122 uint32_t out_data_remaining = 0;
123 NTSTATUS status;
124 NTSTATUS error; /* transport error */
126 status = smbd_smb2_read_recv(subreq,
127 req,
128 &out_data_buffer,
129 &out_data_remaining);
130 TALLOC_FREE(subreq);
131 if (!NT_STATUS_IS_OK(status)) {
132 error = smbd_smb2_request_error(req, status);
133 if (!NT_STATUS_IS_OK(error)) {
134 smbd_server_connection_terminate(req->sconn,
135 nt_errstr(error));
136 return;
138 return;
141 out_data_offset = SMB2_HDR_BODY + 0x10;
143 outhdr = (uint8_t *)req->out.vector[i].iov_base;
145 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
146 if (outbody.data == NULL) {
147 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
148 if (!NT_STATUS_IS_OK(error)) {
149 smbd_server_connection_terminate(req->sconn,
150 nt_errstr(error));
151 return;
153 return;
156 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
157 SCVAL(outbody.data, 0x02,
158 out_data_offset); /* data offset */
159 SCVAL(outbody.data, 0x03, 0); /* reserved */
160 SIVAL(outbody.data, 0x04,
161 out_data_buffer.length); /* data length */
162 SIVAL(outbody.data, 0x08,
163 out_data_remaining); /* data remaining */
164 SIVAL(outbody.data, 0x0C, 0); /* reserved */
166 outdyn = out_data_buffer;
168 error = smbd_smb2_request_done(req, outbody, &outdyn);
169 if (!NT_STATUS_IS_OK(error)) {
170 smbd_server_connection_terminate(req->sconn,
171 nt_errstr(error));
172 return;
176 struct smbd_smb2_read_state {
177 struct smbd_smb2_request *smb2req;
178 files_struct *fsp;
179 uint64_t in_file_id_volatile;
180 uint32_t in_length;
181 uint64_t in_offset;
182 uint32_t in_minimum;
183 DATA_BLOB out_data;
184 uint32_t out_remaining;
187 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
188 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
190 struct lock_struct lock;
191 uint32_t in_length = state->in_length;
192 uint64_t in_offset = state->in_offset;
193 files_struct *fsp = state->fsp;
194 ssize_t nread;
196 nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
197 fsp,
198 NULL,
199 in_offset,
200 in_length);
201 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
202 (int)nread,
203 fsp_str_dbg(fsp) ));
205 if (nread == -1) {
206 if (errno == ENOSYS || errno == EINTR) {
208 * Special hack for broken systems with no working
209 * sendfile. Fake this up by doing read/write calls.
211 set_use_sendfile(SNUM(fsp->conn), false);
212 nread = fake_sendfile(fsp, in_offset, in_length);
213 if (nread == -1) {
214 DEBUG(0,("smb2_sendfile_send_data: "
215 "fake_sendfile failed for "
216 "file %s (%s).\n",
217 fsp_str_dbg(fsp),
218 strerror(errno)));
219 exit_server_cleanly("smb2_sendfile_send_data: "
220 "fake_sendfile failed");
222 goto out;
225 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
226 "%s (%s). Terminating\n",
227 fsp_str_dbg(fsp),
228 strerror(errno)));
229 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
230 } else if (nread == 0) {
232 * Some sendfile implementations return 0 to indicate
233 * that there was a short read, but nothing was
234 * actually written to the socket. In this case,
235 * fallback to the normal read path so the header gets
236 * the correct byte count.
238 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
239 "falling back to the normal read: %s\n",
240 fsp_str_dbg(fsp)));
242 nread = fake_sendfile(fsp, in_offset, in_length);
243 if (nread == -1) {
244 DEBUG(0,("smb2_sendfile_send_data: "
245 "fake_sendfile failed for file "
246 "%s (%s). Terminating\n",
247 fsp_str_dbg(fsp),
248 strerror(errno)));
249 exit_server_cleanly("smb2_sendfile_send_data: "
250 "fake_sendfile failed");
254 out:
256 if (nread < in_length) {
257 sendfile_short_send(fsp, nread, 0, in_length);
260 init_strict_lock_struct(fsp,
261 state->in_file_id_volatile,
262 in_offset,
263 in_length,
264 READ_LOCK,
265 &lock);
267 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
268 return 0;
271 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
272 struct smbd_smb2_read_state *state)
274 struct smbd_smb2_read_state *state_copy = NULL;
275 files_struct *fsp = state->fsp;
278 * We cannot use sendfile if...
279 * We were not configured to do so OR
280 * Signing is active OR
281 * This is a compound SMB2 operation OR
282 * fsp is a STREAM file OR
283 * We're using a write cache OR
284 * It's not a regular file OR
285 * Requested offset is greater than file size OR
286 * there's not enough data in the file.
287 * Phew :-). Luckily this means most
288 * reads on most normal files. JRA.
291 if (!lp__use_sendfile(SNUM(fsp->conn)) ||
292 smb2req->do_signing ||
293 smb2req->in.vector_count != 4 ||
294 (fsp->base_fsp != NULL) ||
295 (fsp->wcp != NULL) ||
296 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
297 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
298 (fsp->fsp_name->st.st_ex_size < state->in_offset +
299 state->in_length)) {
300 return NT_STATUS_RETRY;
303 /* We've already checked there's this amount of data
304 to read. */
305 state->out_data.length = state->in_length;
306 state->out_remaining = 0;
308 /* Make a copy of state attached to the smb2req. Attach
309 the destructor here as this will trigger the sendfile
310 call when the request is destroyed. */
311 state_copy = talloc(smb2req, struct smbd_smb2_read_state);
312 if (!state_copy) {
313 return NT_STATUS_NO_MEMORY;
315 *state_copy = *state;
316 talloc_set_destructor(state_copy, smb2_sendfile_send_data);
317 return NT_STATUS_OK;
320 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
322 /*******************************************************************
323 Common read complete processing function for both synchronous and
324 asynchronous reads.
325 *******************************************************************/
327 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
329 struct smbd_smb2_read_state *state = tevent_req_data(req,
330 struct smbd_smb2_read_state);
331 files_struct *fsp = state->fsp;
333 if (nread < 0) {
334 NTSTATUS status = map_nt_error_from_unix(err);
336 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
337 "Error = %s (NTSTATUS %s)\n",
338 fsp_str_dbg(fsp),
339 (int)nread,
340 strerror(err),
341 nt_errstr(status)));
343 return status;
345 if (nread == 0 && state->in_length != 0) {
346 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
347 fsp_str_dbg(fsp)));
348 return NT_STATUS_END_OF_FILE;
351 if (nread < state->in_minimum) {
352 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
353 "minimum requested %u. Returning end of file\n",
354 fsp_str_dbg(fsp),
355 (int)nread,
356 (unsigned int)state->in_minimum));
357 return NT_STATUS_END_OF_FILE;
360 DEBUG(3,("smbd_smb2_read: fnum=[%d/%s] length=%lu offset=%lu read=%lu\n",
361 fsp->fnum,
362 fsp_str_dbg(fsp),
363 (unsigned long)state->in_length,
364 (unsigned long)state->in_offset,
365 (unsigned long)nread));
367 state->out_data.length = nread;
368 state->out_remaining = 0;
370 return NT_STATUS_OK;
373 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
374 struct tevent_context *ev,
375 struct smbd_smb2_request *smb2req,
376 uint32_t in_smbpid,
377 uint64_t in_file_id_volatile,
378 uint32_t in_length,
379 uint64_t in_offset,
380 uint32_t in_minimum,
381 uint32_t in_remaining)
383 NTSTATUS status;
384 struct tevent_req *req = NULL;
385 struct smbd_smb2_read_state *state = NULL;
386 struct smb_request *smbreq = NULL;
387 connection_struct *conn = smb2req->tcon->compat_conn;
388 files_struct *fsp = NULL;
389 ssize_t nread = -1;
390 struct lock_struct lock;
391 int saved_errno;
393 req = tevent_req_create(mem_ctx, &state,
394 struct smbd_smb2_read_state);
395 if (req == NULL) {
396 return NULL;
398 state->smb2req = smb2req;
399 state->in_length = in_length;
400 state->in_offset = in_offset;
401 state->in_minimum = in_minimum;
402 state->out_data = data_blob_null;
403 state->out_remaining = 0;
405 DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
406 (unsigned long long)in_file_id_volatile));
408 smbreq = smbd_smb2_fake_smb_request(smb2req);
409 if (tevent_req_nomem(smbreq, req)) {
410 return tevent_req_post(req, ev);
413 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
414 if (fsp == NULL) {
415 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
416 return tevent_req_post(req, ev);
418 if (conn != fsp->conn) {
419 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
420 return tevent_req_post(req, ev);
422 if (smb2req->session->vuid != fsp->vuid) {
423 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
424 return tevent_req_post(req, ev);
426 if (fsp->is_directory) {
427 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
428 return tevent_req_post(req, ev);
431 state->fsp = fsp;
432 state->in_file_id_volatile = in_file_id_volatile;
434 if (IS_IPC(smbreq->conn)) {
435 struct tevent_req *subreq = NULL;
437 state->out_data = data_blob_talloc(state, NULL, in_length);
438 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
439 return tevent_req_post(req, ev);
442 if (!fsp_is_np(fsp)) {
443 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
444 return tevent_req_post(req, ev);
447 subreq = np_read_send(state, server_event_context(),
448 fsp->fake_file_handle,
449 state->out_data.data,
450 state->out_data.length);
451 if (tevent_req_nomem(subreq, req)) {
452 return tevent_req_post(req, ev);
454 tevent_req_set_callback(subreq,
455 smbd_smb2_read_pipe_done,
456 req);
457 return req;
460 if (!CHECK_READ(fsp, smbreq)) {
461 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
462 return tevent_req_post(req, ev);
465 status = schedule_smb2_aio_read(fsp->conn,
466 smbreq,
467 fsp,
468 state,
469 &state->out_data,
470 (SMB_OFF_T)in_offset,
471 (size_t)in_length);
473 if (NT_STATUS_IS_OK(status)) {
475 * Doing an async read. Don't
476 * send a "gone async" message
477 * as we expect this to be less
478 * than the client timeout period.
479 * JRA. FIXME for offline files..
480 * FIXME. Add cancel code..
482 smb2req->async = true;
483 return req;
486 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
487 /* Real error in setting up aio. Fail. */
488 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
489 return tevent_req_post(req, ev);
492 /* Fallback to synchronous. */
494 init_strict_lock_struct(fsp,
495 in_file_id_volatile,
496 in_offset,
497 in_length,
498 READ_LOCK,
499 &lock);
501 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
502 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
503 return tevent_req_post(req, ev);
506 /* Try sendfile in preference. */
507 status = schedule_smb2_sendfile_read(smb2req, state);
508 if (NT_STATUS_IS_OK(status)) {
509 tevent_req_done(req);
510 return tevent_req_post(req, ev);
511 } else {
512 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
513 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
514 tevent_req_nterror(req, status);
515 return tevent_req_post(req, ev);
519 /* Ok, read into memory. Allocate the out buffer. */
520 state->out_data = data_blob_talloc(state, NULL, in_length);
521 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
522 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
523 return tevent_req_post(req, ev);
526 nread = read_file(fsp,
527 (char *)state->out_data.data,
528 in_offset,
529 in_length);
531 saved_errno = errno;
533 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
535 DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
536 "len=%llu returned %lld\n",
537 fsp_str_dbg(fsp),
538 (unsigned long long)in_file_id_volatile,
539 (unsigned long long)in_offset,
540 (unsigned long long)in_length,
541 (long long)nread));
543 status = smb2_read_complete(req, nread, saved_errno);
544 if (!NT_STATUS_IS_OK(status)) {
545 tevent_req_nterror(req, status);
546 } else {
547 /* Success. */
548 tevent_req_done(req);
550 return tevent_req_post(req, ev);
553 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
555 struct tevent_req *req = tevent_req_callback_data(subreq,
556 struct tevent_req);
557 struct smbd_smb2_read_state *state = tevent_req_data(req,
558 struct smbd_smb2_read_state);
559 NTSTATUS status;
560 ssize_t nread = -1;
561 bool is_data_outstanding;
563 status = np_read_recv(subreq, &nread, &is_data_outstanding);
564 TALLOC_FREE(subreq);
565 if (!NT_STATUS_IS_OK(status)) {
566 tevent_req_nterror(req, status);
567 return;
570 if (nread == 0 && state->out_data.length != 0) {
571 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
572 return;
575 state->out_data.length = nread;
576 state->out_remaining = 0;
579 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
580 * handle it in SMB1 pipe_read_andx_done().
583 tevent_req_done(req);
586 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
587 TALLOC_CTX *mem_ctx,
588 DATA_BLOB *out_data,
589 uint32_t *out_remaining)
591 NTSTATUS status;
592 struct smbd_smb2_read_state *state = tevent_req_data(req,
593 struct smbd_smb2_read_state);
595 if (tevent_req_is_nterror(req, &status)) {
596 tevent_req_received(req);
597 return status;
600 *out_data = state->out_data;
601 talloc_steal(mem_ctx, out_data->data);
602 *out_remaining = state->out_remaining;
604 tevent_req_received(req);
605 return NT_STATUS_OK;