s3:smb2_read: pass fsp->fnum to init_strict_lock_struct()
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_read.c
blobabaf4c66c214df39c8b5cbd9b35b9646fa4dca4a
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 NTSTATUS status;
48 const uint8_t *inhdr;
49 const uint8_t *inbody;
50 int i = req->current_idx;
51 uint32_t in_smbpid;
52 uint32_t in_length;
53 uint64_t in_offset;
54 uint64_t in_file_id_persistent;
55 uint64_t in_file_id_volatile;
56 uint32_t in_minimum_count;
57 uint32_t in_remaining_bytes;
58 struct tevent_req *subreq;
60 status = smbd_smb2_request_verify_sizes(req, 0x31);
61 if (!NT_STATUS_IS_OK(status)) {
62 return smbd_smb2_request_error(req, status);
64 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
65 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
67 in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
69 in_length = IVAL(inbody, 0x04);
70 in_offset = BVAL(inbody, 0x08);
71 in_file_id_persistent = BVAL(inbody, 0x10);
72 in_file_id_volatile = BVAL(inbody, 0x18);
73 in_minimum_count = IVAL(inbody, 0x20);
74 in_remaining_bytes = IVAL(inbody, 0x28);
76 /* check the max read size */
77 if (in_length > req->sconn->smb2.max_read) {
78 DEBUG(2,("smbd_smb2_request_process_read: "
79 "client ignored max read: %s: 0x%08X: 0x%08X\n",
80 __location__, in_length, req->sconn->smb2.max_read));
81 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
84 status = smbd_smb2_request_verify_creditcharge(req, in_length);
85 if (!NT_STATUS_IS_OK(status)) {
86 return smbd_smb2_request_error(req, status);
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->ev_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, 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);
116 DATA_BLOB outbody;
117 DATA_BLOB outdyn;
118 uint8_t out_data_offset;
119 DATA_BLOB out_data_buffer = data_blob_null;
120 uint32_t out_data_remaining = 0;
121 NTSTATUS status;
122 NTSTATUS error; /* transport error */
124 status = smbd_smb2_read_recv(subreq,
125 req,
126 &out_data_buffer,
127 &out_data_remaining);
128 TALLOC_FREE(subreq);
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->sconn,
133 nt_errstr(error));
134 return;
136 return;
139 out_data_offset = SMB2_HDR_BODY + 0x10;
141 outbody = data_blob_talloc(req->out.vector, NULL, 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->sconn,
146 nt_errstr(error));
147 return;
149 return;
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->sconn,
167 nt_errstr(error));
168 return;
172 struct smbd_smb2_read_state {
173 struct smbd_smb2_request *smb2req;
174 struct smb_request *smbreq;
175 files_struct *fsp;
176 uint64_t in_file_id_volatile;
177 uint32_t in_length;
178 uint64_t in_offset;
179 uint32_t in_minimum;
180 DATA_BLOB out_data;
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;
191 ssize_t nread;
193 nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
194 fsp,
195 NULL,
196 in_offset,
197 in_length);
198 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
199 (int)nread,
200 fsp_str_dbg(fsp) ));
202 if (nread == -1) {
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);
210 if (nread == -1) {
211 DEBUG(0,("smb2_sendfile_send_data: "
212 "fake_sendfile failed for "
213 "file %s (%s).\n",
214 fsp_str_dbg(fsp),
215 strerror(errno)));
216 exit_server_cleanly("smb2_sendfile_send_data: "
217 "fake_sendfile failed");
219 goto out;
222 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
223 "%s (%s). Terminating\n",
224 fsp_str_dbg(fsp),
225 strerror(errno)));
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",
237 fsp_str_dbg(fsp)));
239 nread = fake_sendfile(fsp, in_offset, in_length);
240 if (nread == -1) {
241 DEBUG(0,("smb2_sendfile_send_data: "
242 "fake_sendfile failed for file "
243 "%s (%s). Terminating\n",
244 fsp_str_dbg(fsp),
245 strerror(errno)));
246 exit_server_cleanly("smb2_sendfile_send_data: "
247 "fake_sendfile failed");
251 out:
253 if (nread < in_length) {
254 sendfile_short_send(fsp, nread, 0, in_length);
257 init_strict_lock_struct(fsp,
258 fsp->fnum,
259 in_offset,
260 in_length,
261 READ_LOCK,
262 &lock);
264 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
265 return 0;
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 +
296 state->in_length)) {
297 return NT_STATUS_RETRY;
300 /* We've already checked there's this amount of data
301 to read. */
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(smb2req, struct smbd_smb2_read_state);
309 if (!state_copy) {
310 return NT_STATUS_NO_MEMORY;
312 *state_copy = *state;
313 talloc_set_destructor(state_copy, smb2_sendfile_send_data);
314 return NT_STATUS_OK;
317 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
319 /*******************************************************************
320 Common read complete processing function for both synchronous and
321 asynchronous reads.
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;
330 if (nread < 0) {
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",
335 fsp_str_dbg(fsp),
336 (int)nread,
337 strerror(err),
338 nt_errstr(status)));
340 return status;
342 if (nread == 0 && state->in_length != 0) {
343 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
344 fsp_str_dbg(fsp)));
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",
351 fsp_str_dbg(fsp),
352 (int)nread,
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",
358 fsp->fnum,
359 fsp_str_dbg(fsp),
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;
367 return NT_STATUS_OK;
370 static bool smbd_smb2_read_cancel(struct tevent_req *req)
372 struct smbd_smb2_read_state *state =
373 tevent_req_data(req,
374 struct smbd_smb2_read_state);
376 state->smb2req->cancelled = true;
378 return cancel_smb2_aio(state->smbreq);
381 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
382 struct tevent_context *ev,
383 struct smbd_smb2_request *smb2req,
384 uint32_t in_smbpid,
385 uint64_t in_file_id_volatile,
386 uint32_t in_length,
387 uint64_t in_offset,
388 uint32_t in_minimum,
389 uint32_t in_remaining)
391 NTSTATUS status;
392 struct tevent_req *req = NULL;
393 struct smbd_smb2_read_state *state = NULL;
394 struct smb_request *smbreq = NULL;
395 connection_struct *conn = smb2req->tcon->compat_conn;
396 files_struct *fsp = NULL;
397 ssize_t nread = -1;
398 struct lock_struct lock;
399 int saved_errno;
401 req = tevent_req_create(mem_ctx, &state,
402 struct smbd_smb2_read_state);
403 if (req == NULL) {
404 return NULL;
406 state->smb2req = smb2req;
407 state->in_length = in_length;
408 state->in_offset = in_offset;
409 state->in_minimum = in_minimum;
410 state->out_data = data_blob_null;
411 state->out_remaining = 0;
413 DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
414 (unsigned long long)in_file_id_volatile));
416 smbreq = smbd_smb2_fake_smb_request(smb2req);
417 if (tevent_req_nomem(smbreq, req)) {
418 return tevent_req_post(req, ev);
420 state->smbreq = smbreq;
422 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
423 if (fsp == NULL) {
424 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
425 return tevent_req_post(req, ev);
427 if (conn != fsp->conn) {
428 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
429 return tevent_req_post(req, ev);
431 if (smb2req->session->vuid != fsp->vuid) {
432 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
433 return tevent_req_post(req, ev);
435 if (fsp->is_directory) {
436 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
437 return tevent_req_post(req, ev);
440 state->fsp = fsp;
441 state->in_file_id_volatile = in_file_id_volatile;
443 if (IS_IPC(smbreq->conn)) {
444 struct tevent_req *subreq = NULL;
446 state->out_data = data_blob_talloc(state, NULL, in_length);
447 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
448 return tevent_req_post(req, ev);
451 if (!fsp_is_np(fsp)) {
452 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
453 return tevent_req_post(req, ev);
456 subreq = np_read_send(state, ev,
457 fsp->fake_file_handle,
458 state->out_data.data,
459 state->out_data.length);
460 if (tevent_req_nomem(subreq, req)) {
461 return tevent_req_post(req, ev);
463 tevent_req_set_callback(subreq,
464 smbd_smb2_read_pipe_done,
465 req);
466 return req;
469 if (!CHECK_READ(fsp, smbreq)) {
470 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
471 return tevent_req_post(req, ev);
474 status = schedule_smb2_aio_read(fsp->conn,
475 smbreq,
476 fsp,
477 state,
478 &state->out_data,
479 (off_t)in_offset,
480 (size_t)in_length);
482 if (NT_STATUS_IS_OK(status)) {
484 * Doing an async read, allow this
485 * request to be canceled
487 tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
488 return req;
491 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
492 /* Real error in setting up aio. Fail. */
493 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
494 return tevent_req_post(req, ev);
497 /* Fallback to synchronous. */
499 init_strict_lock_struct(fsp,
500 fsp->fnum,
501 in_offset,
502 in_length,
503 READ_LOCK,
504 &lock);
506 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
507 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
508 return tevent_req_post(req, ev);
511 /* Try sendfile in preference. */
512 status = schedule_smb2_sendfile_read(smb2req, state);
513 if (NT_STATUS_IS_OK(status)) {
514 tevent_req_done(req);
515 return tevent_req_post(req, ev);
516 } else {
517 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
518 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
519 tevent_req_nterror(req, status);
520 return tevent_req_post(req, ev);
524 /* Ok, read into memory. Allocate the out buffer. */
525 state->out_data = data_blob_talloc(state, NULL, in_length);
526 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
527 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
528 return tevent_req_post(req, ev);
531 nread = read_file(fsp,
532 (char *)state->out_data.data,
533 in_offset,
534 in_length);
536 saved_errno = errno;
538 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
540 DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
541 "len=%llu returned %lld\n",
542 fsp_str_dbg(fsp),
543 (unsigned long long)in_file_id_volatile,
544 (unsigned long long)in_offset,
545 (unsigned long long)in_length,
546 (long long)nread));
548 status = smb2_read_complete(req, nread, saved_errno);
549 if (!NT_STATUS_IS_OK(status)) {
550 tevent_req_nterror(req, status);
551 } else {
552 /* Success. */
553 tevent_req_done(req);
555 return tevent_req_post(req, ev);
558 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
560 struct tevent_req *req = tevent_req_callback_data(subreq,
561 struct tevent_req);
562 struct smbd_smb2_read_state *state = tevent_req_data(req,
563 struct smbd_smb2_read_state);
564 NTSTATUS status;
565 ssize_t nread = -1;
566 bool is_data_outstanding;
568 status = np_read_recv(subreq, &nread, &is_data_outstanding);
569 TALLOC_FREE(subreq);
570 if (!NT_STATUS_IS_OK(status)) {
571 NTSTATUS old = status;
572 status = nt_status_np_pipe(old);
573 tevent_req_nterror(req, status);
574 return;
577 if (nread == 0 && state->out_data.length != 0) {
578 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
579 return;
582 state->out_data.length = nread;
583 state->out_remaining = 0;
586 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
587 * handle it in SMB1 pipe_read_andx_done().
590 tevent_req_done(req);
593 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
594 TALLOC_CTX *mem_ctx,
595 DATA_BLOB *out_data,
596 uint32_t *out_remaining)
598 NTSTATUS status;
599 struct smbd_smb2_read_state *state = tevent_req_data(req,
600 struct smbd_smb2_read_state);
602 if (tevent_req_is_nterror(req, &status)) {
603 tevent_req_received(req);
604 return status;
607 *out_data = state->out_data;
608 talloc_steal(mem_ctx, out_data->data);
609 *out_remaining = state->out_remaining;
611 tevent_req_received(req);
612 return NT_STATUS_OK;