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,
33 struct files_struct *in_fsp,
37 uint32_t in_remaining);
38 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
41 uint32_t *out_remaining);
43 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
44 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req)
47 const uint8_t *inbody;
50 uint64_t in_file_id_persistent;
51 uint64_t in_file_id_volatile;
52 struct files_struct *in_fsp;
53 uint32_t in_minimum_count;
54 uint32_t in_remaining_bytes;
55 struct tevent_req *subreq;
57 status = NT_STATUS_OK;//smbd_smb2_request_verify_sizes(req, 0x31);
58 if (!NT_STATUS_IS_OK(status)) {
59 return smbd_smb2_request_error(req, status);
61 inbody = SMBD_SMB2_IN_BODY_PTR(req);
63 in_length = IVAL(inbody, 0x04);
64 in_offset = BVAL(inbody, 0x08);
65 in_file_id_persistent = BVAL(inbody, 0x10);
66 in_file_id_volatile = BVAL(inbody, 0x18);
67 in_minimum_count = IVAL(inbody, 0x20);
68 in_remaining_bytes = IVAL(inbody, 0x28);
70 /* check the max read size */
71 if (in_length > req->sconn->smb2.max_read) {
72 DEBUG(2,("smbd_smb2_request_process_read: "
73 "client ignored max read: %s: 0x%08X: 0x%08X\n",
74 __location__, in_length, req->sconn->smb2.max_read));
75 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
78 status = NT_STATUS_OK;//smbd_smb2_request_verify_creditcharge(req, in_length);
79 if (!NT_STATUS_IS_OK(status)) {
80 return smbd_smb2_request_error(req, status);
83 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
85 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
88 subreq = smbd_smb2_read_send(req, req->sconn->ev_ctx,
95 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
97 //tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
99 smbd_smb2_request_read_done(req);
101 return smbd_smb2_request_pending_queue(req, subreq, 500);
104 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
106 struct smbd_smb2_request *req = subreq;//tevent_req_callback_data(subreq,
107 //struct smbd_smb2_request);
111 uint8_t out_data_offset;
112 DATA_BLOB out_data_buffer = data_blob_null;
113 uint32_t out_data_remaining = 0;
115 NTSTATUS error; /* transport error */
117 status = smbd_smb2_read_recv(subreq,
120 &out_data_remaining);
122 if (!NT_STATUS_IS_OK(status)) {
123 error = smbd_smb2_request_error(req, status);
124 if (!NT_STATUS_IS_OK(error)) {
125 smbd_server_connection_terminate(req->sconn,
132 out_data_offset = SMB2_HDR_BODY + 0x10;
134 outbody = smbd_smb2_generate_outbody(req, 0x10);
135 if (outbody.data == NULL) {
136 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
137 if (!NT_STATUS_IS_OK(error)) {
138 smbd_server_connection_terminate(req->sconn,
145 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
146 SCVAL(outbody.data, 0x02,
147 out_data_offset); /* data offset */
148 SCVAL(outbody.data, 0x03, 0); /* reserved */
149 SIVAL(outbody.data, 0x04,
150 out_data_buffer.length); /* data length */
151 SIVAL(outbody.data, 0x08,
152 out_data_remaining); /* data remaining */
153 SIVAL(outbody.data, 0x0C, 0); /* reserved */
155 outdyn = out_data_buffer;
157 error = smbd_smb2_request_done(req, outbody, &outdyn);
158 if (!NT_STATUS_IS_OK(error)) {
159 smbd_server_connection_terminate(req->sconn,
165 struct smbd_smb2_read_state {
166 struct smbd_smb2_request *smb2req;
167 struct smb_request *smbreq;
172 DATA_BLOB out_headers;
173 uint8_t _out_hdr_buf[NBT_HDR_SIZE + SMB2_HDR_BODY + 0x10];
175 uint32_t out_remaining;
178 static struct smbd_smb2_read_state _state;
180 static int smb2_smb2_read_state_deny_destructor(struct smbd_smb2_read_state *state)
185 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
186 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
188 struct lock_struct lock;
189 uint32_t in_length = state->in_length;
190 uint64_t in_offset = state->in_offset;
191 files_struct *fsp = state->fsp;
194 nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
196 state->smb2req->queue_entry.sendfile_header,
199 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
204 if (errno == ENOSYS || errno == EINTR) {
206 * Special hack for broken systems with no working
207 * sendfile. Fake this up by doing read/write calls.
209 set_use_sendfile(SNUM(fsp->conn), false);
210 nread = fake_sendfile(fsp, in_offset, in_length);
212 DEBUG(0,("smb2_sendfile_send_data: "
213 "fake_sendfile failed for "
217 exit_server_cleanly("smb2_sendfile_send_data: "
218 "fake_sendfile failed");
223 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
224 "%s (%s). Terminating\n",
227 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
228 } else if (nread == 0) {
230 * Some sendfile implementations return 0 to indicate
231 * that there was a short read, but nothing was
232 * actually written to the socket. In this case,
233 * fallback to the normal read path so the header gets
234 * the correct byte count.
236 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
237 "falling back to the normal read: %s\n",
240 nread = fake_sendfile(fsp, in_offset, in_length);
242 DEBUG(0,("smb2_sendfile_send_data: "
243 "fake_sendfile failed for file "
244 "%s (%s). Terminating\n",
247 exit_server_cleanly("smb2_sendfile_send_data: "
248 "fake_sendfile failed");
254 if (nread < in_length) {
255 sendfile_short_send(fsp, nread, 0, in_length);
258 init_strict_lock_struct(fsp,
259 fsp->op->global->open_persistent_id,
265 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
269 static void smb2_sendfile_fn(struct smbd_smb2_read_state *state)
271 smb2_sendfile_send_data(state);
275 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
276 struct smbd_smb2_read_state *state)
278 files_struct *fsp = state->fsp;
281 * We cannot use sendfile if...
282 * We were not configured to do so OR
283 * Signing is active OR
284 * This is a compound SMB2 operation OR
285 * fsp is a STREAM file OR
286 * We're using a write cache OR
287 * It's not a regular file OR
288 * Requested offset is greater than file size OR
289 * there's not enough data in the file.
290 * Phew :-). Luckily this means most
291 * reads on most normal files. JRA.
294 if (!lp__use_sendfile(SNUM(fsp->conn)) ||
295 smb2req->do_signing ||
296 smb2req->do_encryption ||
297 smb2req->in.vector_count >= (2*SMBD_SMB2_NUM_IOV_PER_REQ) ||
298 (fsp->base_fsp != NULL) ||
299 (fsp->wcp != NULL) ||
300 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
301 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
302 (fsp->fsp_name->st.st_ex_size < state->in_offset + state->in_length))
304 return NT_STATUS_RETRY;
307 /* We've already checked there's this amount of data
309 state->out_data.length = state->in_length;
310 state->out_remaining = 0;
312 state->out_headers = data_blob_const(state->_out_hdr_buf,
313 sizeof(state->_out_hdr_buf));
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 = &_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: %s, file %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 bool smbd_smb2_read_cancel(struct tevent_req *req)
372 struct smbd_smb2_read_state *state = &_state;
373 //tevent_req_data(req,
374 //struct smbd_smb2_read_state);
376 return cancel_smb2_aio(state->smbreq);
379 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
380 struct tevent_context *ev,
381 struct smbd_smb2_request *smb2req,
382 struct files_struct *fsp,
386 uint32_t in_remaining)
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;
394 struct lock_struct lock;
397 //req = tevent_req_create(mem_ctx, &state,
398 // struct smbd_smb2_read_state);
404 state->smb2req = smb2req;
405 state->in_length = in_length;
406 state->in_offset = in_offset;
407 state->in_minimum = in_minimum;
408 state->out_data = data_blob_null;
409 state->out_remaining = 0;
411 DEBUG(10,("smbd_smb2_read: %s - %s\n",
412 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
414 smbreq = smbd_smb2_fake_smb_request(smb2req);
415 //if (tevent_req_nomem(smbreq, req)) {
416 // return tevent_req_post(req, ev);
418 state->smbreq = smbreq;
420 //if (fsp->is_directory) {
421 // tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
422 // return tevent_req_post(req, ev);
427 //if (IS_IPC(smbreq->conn)) {
428 // struct tevent_req *subreq = NULL;
430 // state->out_data = data_blob_talloc(state, NULL, in_length);
431 // if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
432 // return tevent_req_post(req, ev);
435 // if (!fsp_is_np(fsp)) {
436 // tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
437 // return tevent_req_post(req, ev);
440 // subreq = np_read_send(state, ev,
441 // fsp->fake_file_handle,
442 // state->out_data.data,
443 // state->out_data.length);
444 // if (tevent_req_nomem(subreq, req)) {
445 // return tevent_req_post(req, ev);
447 // tevent_req_set_callback(subreq,
448 // smbd_smb2_read_pipe_done,
453 //if (!CHECK_READ(fsp, smbreq)) {
454 // tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
455 // return tevent_req_post(req, ev);
458 status = schedule_smb2_aio_read(fsp->conn,
466 if (NT_STATUS_IS_OK(status)) {
468 * Doing an async read, allow this
469 * request to be canceled
471 //tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
476 //if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
477 // /* Real error in setting up aio. Fail. */
478 // tevent_req_nterror(req, status);
479 // return tevent_req_post(req, ev);
482 /* Fallback to synchronous. */
484 init_strict_lock_struct(fsp,
485 fsp->op->global->open_persistent_id,
491 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
492 // tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
493 // return tevent_req_post(req, ev);
496 /* Try sendfile in preference. */
497 status = schedule_smb2_sendfile_read(smb2req, state);
498 if (NT_STATUS_IS_OK(status)) {
499 //tevent_req_done(req);
500 return state;//req;//tevent_req_post(req, ev);
502 // if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
503 // SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
504 // tevent_req_nterror(req, status);
505 // return tevent_req_post(req, ev);
509 /* Ok, read into memory. Allocate the out buffer. */
510 state->out_data = data_blob_talloc(state, NULL, in_length);
511 // if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
512 // SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
513 // return tevent_req_post(req, ev);
516 nread = read_file(fsp,
517 (char *)state->out_data.data,
523 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
525 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
526 "len=%llu returned %lld\n",
529 (unsigned long long)in_offset,
530 (unsigned long long)in_length,
533 status = smb2_read_complete(NULL, nread, saved_errno);
534 // if (!NT_STATUS_IS_OK(status)) {
535 // tevent_req_nterror(req, status);
538 // tevent_req_done(req);
540 return state;//req;//tevent_req_post(req, ev);
543 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
545 struct tevent_req *req = tevent_req_callback_data(subreq,
547 struct smbd_smb2_read_state *state = &_state;//tevent_req_data(req,
548 //struct smbd_smb2_read_state);
551 bool is_data_outstanding;
553 status = np_read_recv(subreq, &nread, &is_data_outstanding);
555 if (!NT_STATUS_IS_OK(status)) {
556 NTSTATUS old = status;
557 status = nt_status_np_pipe(old);
558 tevent_req_nterror(req, status);
562 if (nread == 0 && state->out_data.length != 0) {
563 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
567 state->out_data.length = nread;
568 state->out_remaining = 0;
571 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
572 * handle it in SMB1 pipe_read_andx_done().
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 = &_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 if (state->out_headers.length > 0) {
597 //talloc_steal(mem_ctx, state);
598 //talloc_set_destructor(state, smb2_smb2_read_state_deny_destructor);
599 //tevent_req_received(req);
600 state->smb2req->queue_entry.sendfile_header = &state->out_headers;
601 state->smb2req->queue_entry.sendfile_private = state;
602 state->smb2req->queue_entry.sendfile_fn = smb2_sendfile_fn;
603 //talloc_set_destructor(state, smb2_sendfile_send_data);
605 //tevent_req_received(req);