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/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "libcli/security/security.h"
27 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
28 struct tevent_context *ev,
29 struct smbd_smb2_request *smb2req,
31 uint64_t in_file_id_volatile,
35 uint32_t in_remaining);
36 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
39 uint32_t *out_remaining);
41 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
42 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req)
45 const uint8_t *inbody;
46 int i = req->current_idx;
47 size_t expected_body_size = 0x31;
52 uint64_t in_file_id_persistent;
53 uint64_t in_file_id_volatile;
54 uint32_t in_minimum_count;
55 uint32_t in_remaining_bytes;
56 struct tevent_req *subreq;
58 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
59 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
60 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
63 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
65 body_size = SVAL(inbody, 0x00);
66 if (body_size != expected_body_size) {
67 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
70 in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
72 in_length = IVAL(inbody, 0x04);
73 in_offset = BVAL(inbody, 0x08);
74 in_file_id_persistent = BVAL(inbody, 0x10);
75 in_file_id_volatile = BVAL(inbody, 0x18);
76 in_minimum_count = IVAL(inbody, 0x20);
77 in_remaining_bytes = IVAL(inbody, 0x28);
79 /* check the max read size */
80 if (in_length > lp_smb2_max_read()) {
81 DEBUG(0,("here:%s: 0x%08X: 0x%08X\n",
82 __location__, in_length, lp_smb2_max_read()));
83 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
86 if (req->compat_chain_fsp) {
88 } else if (in_file_id_persistent != in_file_id_volatile) {
89 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
92 subreq = smbd_smb2_read_send(req,
93 req->sconn->smb2.event_ctx,
101 if (subreq == NULL) {
102 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
104 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
106 return smbd_smb2_request_pending_queue(req, subreq);
109 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
111 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
112 struct smbd_smb2_request);
113 int i = req->current_idx;
117 uint8_t out_data_offset;
118 DATA_BLOB out_data_buffer = data_blob_null;
119 uint32_t out_data_remaining = 0;
121 NTSTATUS error; /* transport error */
123 status = smbd_smb2_read_recv(subreq,
126 &out_data_remaining);
128 if (!NT_STATUS_IS_OK(status)) {
129 error = smbd_smb2_request_error(req, status);
130 if (!NT_STATUS_IS_OK(error)) {
131 smbd_server_connection_terminate(req->sconn,
138 out_data_offset = SMB2_HDR_BODY + 0x10;
140 outhdr = (uint8_t *)req->out.vector[i].iov_base;
142 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
143 if (outbody.data == NULL) {
144 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
145 if (!NT_STATUS_IS_OK(error)) {
146 smbd_server_connection_terminate(req->sconn,
153 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
154 SCVAL(outbody.data, 0x02,
155 out_data_offset); /* data offset */
156 SCVAL(outbody.data, 0x03, 0); /* reserved */
157 SIVAL(outbody.data, 0x04,
158 out_data_buffer.length); /* data length */
159 SIVAL(outbody.data, 0x08,
160 out_data_remaining); /* data remaining */
161 SIVAL(outbody.data, 0x0C, 0); /* reserved */
163 outdyn = out_data_buffer;
165 error = smbd_smb2_request_done(req, outbody, &outdyn);
166 if (!NT_STATUS_IS_OK(error)) {
167 smbd_server_connection_terminate(req->sconn,
173 struct smbd_smb2_read_state {
174 struct smbd_smb2_request *smb2req;
176 uint64_t in_file_id_volatile;
181 uint32_t out_remaining;
184 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
185 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
187 struct lock_struct lock;
188 uint32_t in_length = state->in_length;
189 uint64_t in_offset = state->in_offset;
190 files_struct *fsp = state->fsp;
193 nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
198 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
203 if (errno == ENOSYS || errno == EINTR) {
205 * Special hack for broken systems with no working
206 * sendfile. 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);
211 DEBUG(0,("smb2_sendfile_send_data: "
212 "fake_sendfile failed for "
216 exit_server_cleanly("smb2_sendfile_send_data: "
217 "fake_sendfile failed");
222 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
223 "%s (%s). Terminating\n",
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",
239 nread = fake_sendfile(fsp, in_offset, in_length);
241 DEBUG(0,("smb2_sendfile_send_data: "
242 "fake_sendfile failed for file "
243 "%s (%s). Terminating\n",
246 exit_server_cleanly("smb2_sendfile_send_data: "
247 "fake_sendfile failed");
253 if (nread < in_length) {
254 sendfile_short_send(fsp, nread, 0, in_length);
257 init_strict_lock_struct(fsp,
258 state->in_file_id_volatile,
264 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
268 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
269 struct smbd_smb2_read_state *state)
271 struct smbd_smb2_read_state *state_copy = NULL;
272 files_struct *fsp = state->fsp;
275 * We cannot use sendfile if...
276 * We were not configured to do so OR
277 * Signing is active OR
278 * This is a compound SMB2 operation OR
279 * fsp is a STREAM file OR
280 * We're using a write cache OR
281 * It's not a regular file OR
282 * Requested offset is greater than file size OR
283 * there's not enough data in the file.
284 * Phew :-). Luckily this means most
285 * reads on most normal files. JRA.
288 if (!_lp_use_sendfile(SNUM(fsp->conn)) ||
289 smb2req->do_signing ||
290 smb2req->in.vector_count != 4 ||
291 (fsp->base_fsp != NULL) ||
292 (fsp->wcp != NULL) ||
293 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
294 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
295 (fsp->fsp_name->st.st_ex_size < state->in_offset +
297 return NT_STATUS_RETRY;
300 /* We've already checked there's this amount of data
302 state->out_data.length = state->in_length;
303 state->out_remaining = 0;
305 /* Make a copy of state attached to the smb2req. Attach
306 the destructor here as this will trigger the sendfile
307 call when the request is destroyed. */
308 state_copy = TALLOC_P(smb2req, struct smbd_smb2_read_state);
310 return NT_STATUS_NO_MEMORY;
312 *state_copy = *state;
313 talloc_set_destructor(state_copy, smb2_sendfile_send_data);
317 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
319 /*******************************************************************
320 Common read complete processing function for both synchronous and
322 *******************************************************************/
324 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
326 struct smbd_smb2_read_state *state = tevent_req_data(req,
327 struct smbd_smb2_read_state);
328 files_struct *fsp = state->fsp;
331 NTSTATUS status = map_nt_error_from_unix(err);
333 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
334 "Error = %s (NTSTATUS %s)\n",
342 if (nread == 0 && state->in_length != 0) {
343 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
345 return NT_STATUS_END_OF_FILE;
348 if (nread < state->in_minimum) {
349 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
350 "minimum requested %u. Returning end of file\n",
353 (unsigned int)state->in_minimum));
354 return NT_STATUS_END_OF_FILE;
357 DEBUG(3,("smbd_smb2_read: fnum=[%d/%s] length=%lu offset=%lu read=%lu\n",
360 (unsigned long)state->in_length,
361 (unsigned long)state->in_offset,
362 (unsigned long)nread));
364 state->out_data.length = nread;
365 state->out_remaining = 0;
370 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
371 struct tevent_context *ev,
372 struct smbd_smb2_request *smb2req,
374 uint64_t in_file_id_volatile,
378 uint32_t in_remaining)
381 struct tevent_req *req = NULL;
382 struct smbd_smb2_read_state *state = NULL;
383 struct smb_request *smbreq = NULL;
384 connection_struct *conn = smb2req->tcon->compat_conn;
385 files_struct *fsp = NULL;
387 struct lock_struct lock;
390 req = tevent_req_create(mem_ctx, &state,
391 struct smbd_smb2_read_state);
395 state->smb2req = smb2req;
396 state->in_length = in_length;
397 state->in_offset = in_offset;
398 state->in_minimum = in_minimum;
399 state->out_data = data_blob_null;
400 state->out_remaining = 0;
402 DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
403 (unsigned long long)in_file_id_volatile));
405 smbreq = smbd_smb2_fake_smb_request(smb2req);
406 if (tevent_req_nomem(smbreq, req)) {
407 return tevent_req_post(req, ev);
410 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
412 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
413 return tevent_req_post(req, ev);
415 if (conn != fsp->conn) {
416 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
417 return tevent_req_post(req, ev);
419 if (smb2req->session->vuid != fsp->vuid) {
420 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
421 return tevent_req_post(req, ev);
423 if (fsp->is_directory) {
424 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
425 return tevent_req_post(req, ev);
429 state->in_file_id_volatile = in_file_id_volatile;
431 if (IS_IPC(smbreq->conn)) {
432 struct tevent_req *subreq = NULL;
434 state->out_data = data_blob_talloc(state, NULL, in_length);
435 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
436 return tevent_req_post(req, ev);
439 if (!fsp_is_np(fsp)) {
440 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
441 return tevent_req_post(req, ev);
444 subreq = np_read_send(state, smbd_event_context(),
445 fsp->fake_file_handle,
446 state->out_data.data,
447 state->out_data.length);
448 if (tevent_req_nomem(subreq, req)) {
449 return tevent_req_post(req, ev);
451 tevent_req_set_callback(subreq,
452 smbd_smb2_read_pipe_done,
457 if (!CHECK_READ(fsp, smbreq)) {
458 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
459 return tevent_req_post(req, ev);
462 status = schedule_smb2_aio_read(fsp->conn,
467 (SMB_OFF_T)in_offset,
470 if (NT_STATUS_IS_OK(status)) {
472 * Doing an async read. Don't
473 * send a "gone async" message
474 * as we expect this to be less
475 * than the client timeout period.
476 * JRA. FIXME for offline files..
477 * FIXME. Add cancel code..
479 smb2req->async = true;
483 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
484 /* Real error in setting up aio. Fail. */
485 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
486 return tevent_req_post(req, ev);
489 /* Fallback to synchronous. */
491 init_strict_lock_struct(fsp,
498 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
499 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
500 return tevent_req_post(req, ev);
503 /* Try sendfile in preference. */
504 status = schedule_smb2_sendfile_read(smb2req, state);
505 if (NT_STATUS_IS_OK(status)) {
506 tevent_req_done(req);
507 return tevent_req_post(req, ev);
509 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
510 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
511 tevent_req_nterror(req, status);
512 return tevent_req_post(req, ev);
516 /* Ok, read into memory. Allocate the out buffer. */
517 state->out_data = data_blob_talloc(state, NULL, in_length);
518 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
519 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
520 return tevent_req_post(req, ev);
523 nread = read_file(fsp,
524 (char *)state->out_data.data,
530 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
532 DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
533 "len=%llu returned %lld\n",
535 (unsigned long long)in_file_id_volatile,
536 (unsigned long long)in_offset,
537 (unsigned long long)in_length,
540 status = smb2_read_complete(req, nread, saved_errno);
541 if (!NT_STATUS_IS_OK(status)) {
542 tevent_req_nterror(req, status);
545 tevent_req_done(req);
547 return tevent_req_post(req, ev);
550 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
552 struct tevent_req *req = tevent_req_callback_data(subreq,
554 struct smbd_smb2_read_state *state = tevent_req_data(req,
555 struct smbd_smb2_read_state);
558 bool is_data_outstanding;
560 status = np_read_recv(subreq, &nread, &is_data_outstanding);
562 if (!NT_STATUS_IS_OK(status)) {
563 tevent_req_nterror(req, status);
567 if (nread == 0 && state->out_data.length != 0) {
568 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
572 state->out_data.length = nread;
573 state->out_remaining = 0;
575 tevent_req_done(req);
578 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
581 uint32_t *out_remaining)
584 struct smbd_smb2_read_state *state = tevent_req_data(req,
585 struct smbd_smb2_read_state);
587 if (tevent_req_is_nterror(req, &status)) {
588 tevent_req_received(req);
592 *out_data = state->out_data;
593 talloc_steal(mem_ctx, out_data->data);
594 *out_remaining = state->out_remaining;
596 tevent_req_received(req);