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"
29 #include "lib/util/sys_rw_data.h"
31 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
32 struct tevent_context *ev,
33 struct smbd_smb2_request *smb2req,
34 struct files_struct *in_fsp,
39 uint32_t in_remaining);
40 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
43 uint32_t *out_remaining);
45 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
46 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req)
48 struct smbXsrv_connection *xconn = req->xconn;
50 const uint8_t *inbody;
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 inbody = SMBD_SMB2_IN_BODY_PTR(req);
67 if (xconn->protocol >= PROTOCOL_SMB3_02) {
68 in_flags = CVAL(inbody, 0x03);
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 > xconn->smb2.server.max_read) {
81 DEBUG(2,("smbd_smb2_request_process_read: "
82 "client ignored max read: %s: 0x%08X: 0x%08X\n",
83 __location__, in_length, xconn->smb2.server.max_read));
84 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
87 status = smbd_smb2_request_verify_creditcharge(req, in_length);
88 if (!NT_STATUS_IS_OK(status)) {
89 return smbd_smb2_request_error(req, status);
92 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
94 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
97 subreq = smbd_smb2_read_send(req, req->sconn->ev_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, 500);
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);
118 uint8_t out_data_offset;
119 DATA_BLOB out_data_buffer = data_blob_null;
120 uint32_t out_data_remaining = 0;
122 NTSTATUS error; /* transport error */
124 status = smbd_smb2_read_recv(subreq,
127 &out_data_remaining);
129 if (!NT_STATUS_IS_OK(status)) {
130 error = smbd_smb2_request_error(req, status);
131 if (!NT_STATUS_IS_OK(error)) {
132 smbd_server_connection_terminate(req->xconn,
139 out_data_offset = SMB2_HDR_BODY + 0x10;
141 outbody = smbd_smb2_generate_outbody(req, 0x10);
142 if (outbody.data == NULL) {
143 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
144 if (!NT_STATUS_IS_OK(error)) {
145 smbd_server_connection_terminate(req->xconn,
152 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
153 SCVAL(outbody.data, 0x02,
154 out_data_offset); /* data offset */
155 SCVAL(outbody.data, 0x03, 0); /* reserved */
156 SIVAL(outbody.data, 0x04,
157 out_data_buffer.length); /* data length */
158 SIVAL(outbody.data, 0x08,
159 out_data_remaining); /* data remaining */
160 SIVAL(outbody.data, 0x0C, 0); /* reserved */
162 outdyn = out_data_buffer;
164 error = smbd_smb2_request_done(req, outbody, &outdyn);
165 if (!NT_STATUS_IS_OK(error)) {
166 smbd_server_connection_terminate(req->xconn,
172 struct smbd_smb2_read_state {
173 struct smbd_smb2_request *smb2req;
174 struct smb_request *smbreq;
180 DATA_BLOB out_headers;
181 uint8_t _out_hdr_buf[NBT_HDR_SIZE + SMB2_HDR_BODY + 0x10];
183 uint32_t out_remaining;
186 static int smb2_smb2_read_state_deny_destructor(struct smbd_smb2_read_state *state)
191 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
192 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
194 struct lock_struct lock;
195 uint32_t in_length = state->in_length;
196 uint64_t in_offset = state->in_offset;
197 files_struct *fsp = state->fsp;
198 const DATA_BLOB *hdr = state->smb2req->queue_entry.sendfile_header;
199 NTSTATUS *pstatus = state->smb2req->queue_entry.sendfile_status;
200 struct smbXsrv_connection *xconn = state->smb2req->xconn;
205 nread = SMB_VFS_SENDFILE(xconn->transport.sock,
210 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
218 * Returning ENOSYS means no data at all was sent.
219 Do this as a normal read. */
220 if (errno == ENOSYS) {
224 if (errno == ENOTSUP) {
225 set_use_sendfile(SNUM(fsp->conn), false);
226 DBG_WARNING("Disabling sendfile use as sendfile is "
227 "not supported by the system\n");
231 if (errno == EINTR) {
233 * Special hack for broken Linux with no working sendfile. If we
234 * return EINTR we sent the header but not the rest of the data.
235 * Fake this up by doing read/write calls.
237 set_use_sendfile(SNUM(fsp->conn), false);
238 nread = fake_sendfile(xconn, fsp, in_offset, in_length);
241 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
242 "failed for file %s (%s) for client %s. "
244 fsp_str_dbg(fsp), strerror(saved_errno),
245 smbXsrv_connection_dbg(xconn)));
246 *pstatus = map_nt_error_from_unix_common(saved_errno);
252 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
253 "%s (%s) for client %s. Terminating\n",
254 fsp_str_dbg(fsp), strerror(saved_errno),
255 smbXsrv_connection_dbg(xconn)));
256 *pstatus = map_nt_error_from_unix_common(saved_errno);
258 } else if (nread == 0) {
260 * Some sendfile implementations return 0 to indicate
261 * that there was a short read, but nothing was
262 * actually written to the socket. In this case,
263 * fallback to the normal read path so the header gets
264 * the correct byte count.
266 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
267 "falling back to the normal read: %s\n",
273 * We got a short read
278 /* Send out the header. */
279 ret = write_data(xconn->transport.sock,
280 (const char *)hdr->data, hdr->length);
281 if (ret != hdr->length) {
283 DEBUG(0,("smb2_sendfile_send_data: write_data failed for file "
284 "%s (%s) for client %s. Terminating\n",
285 fsp_str_dbg(fsp), strerror(saved_errno),
286 smbXsrv_connection_dbg(xconn)));
287 *pstatus = map_nt_error_from_unix_common(saved_errno);
290 nread = fake_sendfile(xconn, fsp, in_offset, in_length);
293 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
294 "failed for file %s (%s) for client %s. "
296 fsp_str_dbg(fsp), strerror(saved_errno),
297 smbXsrv_connection_dbg(xconn)));
298 *pstatus = map_nt_error_from_unix_common(saved_errno);
304 if (nread < in_length) {
305 ret = sendfile_short_send(xconn, fsp, nread,
306 hdr->length, in_length);
309 DEBUG(0,("%s: sendfile_short_send "
310 "failed for file %s (%s) for client %s. "
313 fsp_str_dbg(fsp), strerror(saved_errno),
314 smbXsrv_connection_dbg(xconn)));
315 *pstatus = map_nt_error_from_unix_common(saved_errno);
320 init_strict_lock_struct(fsp,
321 fsp->op->global->open_persistent_id,
327 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
329 *pstatus = NT_STATUS_OK;
333 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
334 struct smbd_smb2_read_state *state)
336 files_struct *fsp = state->fsp;
339 * We cannot use sendfile if...
340 * We were not configured to do so OR
341 * Signing is active OR
342 * This is a compound SMB2 operation OR
343 * fsp is a STREAM file OR
344 * We're using a write cache OR
345 * It's not a regular file OR
346 * Requested offset is greater than file size OR
347 * there's not enough data in the file.
348 * Phew :-). Luckily this means most
349 * reads on most normal files. JRA.
352 if (!lp__use_sendfile(SNUM(fsp->conn)) ||
353 smb2req->do_signing ||
354 smb2req->do_encryption ||
355 smb2req->in.vector_count >= (2*SMBD_SMB2_NUM_IOV_PER_REQ) ||
356 (fsp->base_fsp != NULL) ||
357 (fsp->wcp != NULL) ||
358 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
359 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
360 (fsp->fsp_name->st.st_ex_size < state->in_offset + state->in_length))
362 return NT_STATUS_RETRY;
365 /* We've already checked there's this amount of data
367 state->out_data.length = state->in_length;
368 state->out_remaining = 0;
370 state->out_headers = data_blob_const(state->_out_hdr_buf,
371 sizeof(state->_out_hdr_buf));
375 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
377 /*******************************************************************
378 Common read complete processing function for both synchronous and
380 *******************************************************************/
382 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
384 struct smbd_smb2_read_state *state = tevent_req_data(req,
385 struct smbd_smb2_read_state);
386 files_struct *fsp = state->fsp;
389 NTSTATUS status = map_nt_error_from_unix(err);
391 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
392 "Error = %s (NTSTATUS %s)\n",
400 if (nread == 0 && state->in_length != 0) {
401 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
403 return NT_STATUS_END_OF_FILE;
406 if (nread < state->in_minimum) {
407 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
408 "minimum requested %u. Returning end of file\n",
411 (unsigned int)state->in_minimum));
412 return NT_STATUS_END_OF_FILE;
415 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
418 (unsigned long)state->in_length,
419 (unsigned long)state->in_offset,
420 (unsigned long)nread));
422 state->out_data.length = nread;
423 state->out_remaining = 0;
428 static bool smbd_smb2_read_cancel(struct tevent_req *req)
430 struct smbd_smb2_read_state *state =
432 struct smbd_smb2_read_state);
434 return cancel_smb2_aio(state->smbreq);
437 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
438 struct tevent_context *ev,
439 struct smbd_smb2_request *smb2req,
440 struct files_struct *fsp,
445 uint32_t in_remaining)
448 struct tevent_req *req = NULL;
449 struct smbd_smb2_read_state *state = NULL;
450 struct smb_request *smbreq = NULL;
451 connection_struct *conn = smb2req->tcon->compat;
453 struct lock_struct lock;
456 req = tevent_req_create(mem_ctx, &state,
457 struct smbd_smb2_read_state);
461 state->smb2req = smb2req;
462 state->in_flags = in_flags;
463 state->in_length = in_length;
464 state->in_offset = in_offset;
465 state->in_minimum = in_minimum;
466 state->out_data = data_blob_null;
467 state->out_remaining = 0;
469 DEBUG(10,("smbd_smb2_read: %s - %s\n",
470 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
472 smbreq = smbd_smb2_fake_smb_request(smb2req);
473 if (tevent_req_nomem(smbreq, req)) {
474 return tevent_req_post(req, ev);
476 state->smbreq = smbreq;
478 if (fsp->is_directory) {
479 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
480 return tevent_req_post(req, ev);
485 if (IS_IPC(smbreq->conn)) {
486 struct tevent_req *subreq = NULL;
488 state->out_data = data_blob_talloc(state, NULL, in_length);
489 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
490 return tevent_req_post(req, ev);
493 if (!fsp_is_np(fsp)) {
494 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
495 return tevent_req_post(req, ev);
498 subreq = np_read_send(state, ev,
499 fsp->fake_file_handle,
500 state->out_data.data,
501 state->out_data.length);
502 if (tevent_req_nomem(subreq, req)) {
503 return tevent_req_post(req, ev);
505 tevent_req_set_callback(subreq,
506 smbd_smb2_read_pipe_done,
511 if (!CHECK_READ_SMB2(fsp)) {
512 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
513 return tevent_req_post(req, ev);
516 status = schedule_smb2_aio_read(fsp->conn,
524 if (NT_STATUS_IS_OK(status)) {
526 * Doing an async read, allow this
527 * request to be canceled
529 tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
533 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
534 /* Real error in setting up aio. Fail. */
535 tevent_req_nterror(req, status);
536 return tevent_req_post(req, ev);
539 /* Fallback to synchronous. */
541 init_strict_lock_struct(fsp,
542 fsp->op->global->open_persistent_id,
548 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
549 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
550 return tevent_req_post(req, ev);
553 /* Try sendfile in preference. */
554 status = schedule_smb2_sendfile_read(smb2req, state);
555 if (NT_STATUS_IS_OK(status)) {
556 tevent_req_done(req);
557 return tevent_req_post(req, ev);
559 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
560 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
561 tevent_req_nterror(req, status);
562 return tevent_req_post(req, ev);
566 /* Ok, read into memory. Allocate the out buffer. */
567 state->out_data = data_blob_talloc(state, NULL, in_length);
568 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
569 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
570 return tevent_req_post(req, ev);
573 nread = read_file(fsp,
574 (char *)state->out_data.data,
580 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
582 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
583 "len=%llu returned %lld\n",
586 (unsigned long long)in_offset,
587 (unsigned long long)in_length,
590 status = smb2_read_complete(req, nread, saved_errno);
591 if (!NT_STATUS_IS_OK(status)) {
592 tevent_req_nterror(req, status);
595 tevent_req_done(req);
597 return tevent_req_post(req, ev);
600 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
602 struct tevent_req *req = tevent_req_callback_data(subreq,
604 struct smbd_smb2_read_state *state = tevent_req_data(req,
605 struct smbd_smb2_read_state);
608 bool is_data_outstanding;
610 status = np_read_recv(subreq, &nread, &is_data_outstanding);
612 if (!NT_STATUS_IS_OK(status)) {
613 NTSTATUS old = status;
614 status = nt_status_np_pipe(old);
615 tevent_req_nterror(req, status);
619 if (nread == 0 && state->out_data.length != 0) {
620 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
624 state->out_data.length = nread;
625 state->out_remaining = 0;
628 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
629 * handle it in SMB1 pipe_read_andx_done().
632 tevent_req_done(req);
635 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
638 uint32_t *out_remaining)
641 struct smbd_smb2_read_state *state = tevent_req_data(req,
642 struct smbd_smb2_read_state);
644 if (tevent_req_is_nterror(req, &status)) {
645 tevent_req_received(req);
649 *out_data = state->out_data;
650 talloc_steal(mem_ctx, out_data->data);
651 *out_remaining = state->out_remaining;
653 if (state->out_headers.length > 0) {
654 talloc_steal(mem_ctx, state);
655 talloc_set_destructor(state, smb2_smb2_read_state_deny_destructor);
656 tevent_req_received(req);
657 state->smb2req->queue_entry.sendfile_header = &state->out_headers;
658 talloc_set_destructor(state, smb2_sendfile_send_data);
660 tevent_req_received(req);