2 Unix SMB/CIFS implementation.
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/>.
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,
34 uint64_t in_file_id_volatile,
38 uint32_t in_remaining);
39 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
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)
48 const uint8_t *inbody;
49 int i = req->current_idx;
50 size_t expected_body_size = 0x31;
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) {
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,
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;
120 uint8_t out_data_offset;
121 DATA_BLOB out_data_buffer = data_blob_null;
122 uint32_t out_data_remaining = 0;
124 NTSTATUS error; /* transport error */
126 status = smbd_smb2_read_recv(subreq,
129 &out_data_remaining);
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,
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,
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,
176 struct smbd_smb2_read_state {
177 struct smbd_smb2_request *smb2req;
179 uint64_t in_file_id_volatile;
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;
196 nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
201 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
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);
214 DEBUG(0,("smb2_sendfile_send_data: "
215 "fake_sendfile failed for "
219 exit_server_cleanly("smb2_sendfile_send_data: "
220 "fake_sendfile failed");
225 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
226 "%s (%s). Terminating\n",
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",
242 nread = fake_sendfile(fsp, in_offset, in_length);
244 DEBUG(0,("smb2_sendfile_send_data: "
245 "fake_sendfile failed for file "
246 "%s (%s). Terminating\n",
249 exit_server_cleanly("smb2_sendfile_send_data: "
250 "fake_sendfile failed");
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,
267 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
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 +
300 return NT_STATUS_RETRY;
303 /* We've already checked there's this amount of data
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);
313 return NT_STATUS_NO_MEMORY;
315 *state_copy = *state;
316 talloc_set_destructor(state_copy, smb2_sendfile_send_data);
320 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
322 /*******************************************************************
323 Common read complete processing function for both synchronous and
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;
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",
345 if (nread == 0 && state->in_length != 0) {
346 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
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",
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",
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;
373 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
374 struct tevent_context *ev,
375 struct smbd_smb2_request *smb2req,
377 uint64_t in_file_id_volatile,
381 uint32_t in_remaining)
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;
390 struct lock_struct lock;
393 req = tevent_req_create(mem_ctx, &state,
394 struct smbd_smb2_read_state);
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);
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);
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,
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,
470 (SMB_OFF_T)in_offset,
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;
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,
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);
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,
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",
538 (unsigned long long)in_file_id_volatile,
539 (unsigned long long)in_offset,
540 (unsigned long long)in_length,
543 status = smb2_read_complete(req, nread, saved_errno);
544 if (!NT_STATUS_IS_OK(status)) {
545 tevent_req_nterror(req, status);
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,
557 struct smbd_smb2_read_state *state = tevent_req_data(req,
558 struct smbd_smb2_read_state);
561 bool is_data_outstanding;
563 status = np_read_recv(subreq, &nread, &is_data_outstanding);
565 if (!NT_STATUS_IS_OK(status)) {
566 tevent_req_nterror(req, status);
570 if (nread == 0 && state->out_data.length != 0) {
571 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
575 state->out_data.length = nread;
576 state->out_remaining = 0;
578 tevent_req_done(req);
581 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
584 uint32_t *out_remaining)
587 struct smbd_smb2_read_state *state = tevent_req_data(req,
588 struct smbd_smb2_read_state);
590 if (tevent_req_is_nterror(req, &status)) {
591 tevent_req_received(req);
595 *out_data = state->out_data;
596 talloc_steal(mem_ctx, out_data->data);
597 *out_remaining = state->out_remaining;
599 tevent_req_received(req);