s3:smb2_server: remember the max_{trans,read,write} sizes we negotiated (bug #8473)
[Samba.git] / source3 / smbd / smb2_read.c
blob21082e67cd8aef244711c989838d56442d2fc4f1
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(0,("here:%s: 0x%08X: 0x%08X\n",
79 __location__, in_length, req->sconn->smb2.max_read));
80 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
83 if (req->compat_chain_fsp) {
84 /* skip check */
85 } else if (in_file_id_persistent != in_file_id_volatile) {
86 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
89 subreq = smbd_smb2_read_send(req,
90 req->sconn->smb2.event_ctx,
91 req,
92 in_smbpid,
93 in_file_id_volatile,
94 in_length,
95 in_offset,
96 in_minimum_count,
97 in_remaining_bytes);
98 if (subreq == NULL) {
99 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
101 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
103 return smbd_smb2_request_pending_queue(req, subreq);
106 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
108 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
109 struct smbd_smb2_request);
110 int i = req->current_idx;
111 uint8_t *outhdr;
112 DATA_BLOB outbody;
113 DATA_BLOB outdyn;
114 uint8_t out_data_offset;
115 DATA_BLOB out_data_buffer = data_blob_null;
116 uint32_t out_data_remaining = 0;
117 NTSTATUS status;
118 NTSTATUS error; /* transport error */
120 status = smbd_smb2_read_recv(subreq,
121 req,
122 &out_data_buffer,
123 &out_data_remaining);
124 TALLOC_FREE(subreq);
125 if (!NT_STATUS_IS_OK(status)) {
126 error = smbd_smb2_request_error(req, status);
127 if (!NT_STATUS_IS_OK(error)) {
128 smbd_server_connection_terminate(req->sconn,
129 nt_errstr(error));
130 return;
132 return;
135 out_data_offset = SMB2_HDR_BODY + 0x10;
137 outhdr = (uint8_t *)req->out.vector[i].iov_base;
139 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
140 if (outbody.data == NULL) {
141 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
142 if (!NT_STATUS_IS_OK(error)) {
143 smbd_server_connection_terminate(req->sconn,
144 nt_errstr(error));
145 return;
147 return;
150 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
151 SCVAL(outbody.data, 0x02,
152 out_data_offset); /* data offset */
153 SCVAL(outbody.data, 0x03, 0); /* reserved */
154 SIVAL(outbody.data, 0x04,
155 out_data_buffer.length); /* data length */
156 SIVAL(outbody.data, 0x08,
157 out_data_remaining); /* data remaining */
158 SIVAL(outbody.data, 0x0C, 0); /* reserved */
160 outdyn = out_data_buffer;
162 error = smbd_smb2_request_done(req, outbody, &outdyn);
163 if (!NT_STATUS_IS_OK(error)) {
164 smbd_server_connection_terminate(req->sconn,
165 nt_errstr(error));
166 return;
170 struct smbd_smb2_read_state {
171 struct smbd_smb2_request *smb2req;
172 files_struct *fsp;
173 uint64_t in_file_id_volatile;
174 uint32_t in_length;
175 uint64_t in_offset;
176 uint32_t in_minimum;
177 DATA_BLOB out_data;
178 uint32_t out_remaining;
181 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
182 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
184 struct lock_struct lock;
185 uint32_t in_length = state->in_length;
186 uint64_t in_offset = state->in_offset;
187 files_struct *fsp = state->fsp;
188 ssize_t nread;
190 nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
191 fsp,
192 NULL,
193 in_offset,
194 in_length);
195 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
196 (int)nread,
197 fsp_str_dbg(fsp) ));
199 if (nread == -1) {
200 if (errno == ENOSYS || errno == EINTR) {
202 * Special hack for broken systems with no working
203 * sendfile. Fake this up by doing read/write calls.
205 set_use_sendfile(SNUM(fsp->conn), false);
206 nread = fake_sendfile(fsp, in_offset, in_length);
207 if (nread == -1) {
208 DEBUG(0,("smb2_sendfile_send_data: "
209 "fake_sendfile failed for "
210 "file %s (%s).\n",
211 fsp_str_dbg(fsp),
212 strerror(errno)));
213 exit_server_cleanly("smb2_sendfile_send_data: "
214 "fake_sendfile failed");
216 goto out;
219 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
220 "%s (%s). Terminating\n",
221 fsp_str_dbg(fsp),
222 strerror(errno)));
223 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
224 } else if (nread == 0) {
226 * Some sendfile implementations return 0 to indicate
227 * that there was a short read, but nothing was
228 * actually written to the socket. In this case,
229 * fallback to the normal read path so the header gets
230 * the correct byte count.
232 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
233 "falling back to the normal read: %s\n",
234 fsp_str_dbg(fsp)));
236 nread = fake_sendfile(fsp, in_offset, in_length);
237 if (nread == -1) {
238 DEBUG(0,("smb2_sendfile_send_data: "
239 "fake_sendfile failed for file "
240 "%s (%s). Terminating\n",
241 fsp_str_dbg(fsp),
242 strerror(errno)));
243 exit_server_cleanly("smb2_sendfile_send_data: "
244 "fake_sendfile failed");
248 out:
250 if (nread < in_length) {
251 sendfile_short_send(fsp, nread, 0, in_length);
254 init_strict_lock_struct(fsp,
255 state->in_file_id_volatile,
256 in_offset,
257 in_length,
258 READ_LOCK,
259 &lock);
261 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
262 return 0;
265 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
266 struct smbd_smb2_read_state *state)
268 struct smbd_smb2_read_state *state_copy = NULL;
269 files_struct *fsp = state->fsp;
272 * We cannot use sendfile if...
273 * We were not configured to do so OR
274 * Signing is active OR
275 * This is a compound SMB2 operation OR
276 * fsp is a STREAM file OR
277 * We're using a write cache OR
278 * It's not a regular file OR
279 * Requested offset is greater than file size OR
280 * there's not enough data in the file.
281 * Phew :-). Luckily this means most
282 * reads on most normal files. JRA.
285 if (!_lp_use_sendfile(SNUM(fsp->conn)) ||
286 smb2req->do_signing ||
287 smb2req->in.vector_count != 4 ||
288 (fsp->base_fsp != NULL) ||
289 (fsp->wcp != NULL) ||
290 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
291 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
292 (fsp->fsp_name->st.st_ex_size < state->in_offset +
293 state->in_length)) {
294 return NT_STATUS_RETRY;
297 /* We've already checked there's this amount of data
298 to read. */
299 state->out_data.length = state->in_length;
300 state->out_remaining = 0;
302 /* Make a copy of state attached to the smb2req. Attach
303 the destructor here as this will trigger the sendfile
304 call when the request is destroyed. */
305 state_copy = TALLOC_P(smb2req, struct smbd_smb2_read_state);
306 if (!state_copy) {
307 return NT_STATUS_NO_MEMORY;
309 *state_copy = *state;
310 talloc_set_destructor(state_copy, smb2_sendfile_send_data);
311 return NT_STATUS_OK;
314 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
316 /*******************************************************************
317 Common read complete processing function for both synchronous and
318 asynchronous reads.
319 *******************************************************************/
321 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
323 struct smbd_smb2_read_state *state = tevent_req_data(req,
324 struct smbd_smb2_read_state);
325 files_struct *fsp = state->fsp;
327 if (nread < 0) {
328 NTSTATUS status = map_nt_error_from_unix(err);
330 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
331 "Error = %s (NTSTATUS %s)\n",
332 fsp_str_dbg(fsp),
333 (int)nread,
334 strerror(err),
335 nt_errstr(status)));
337 return status;
339 if (nread == 0 && state->in_length != 0) {
340 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
341 fsp_str_dbg(fsp)));
342 return NT_STATUS_END_OF_FILE;
345 if (nread < state->in_minimum) {
346 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
347 "minimum requested %u. Returning end of file\n",
348 fsp_str_dbg(fsp),
349 (int)nread,
350 (unsigned int)state->in_minimum));
351 return NT_STATUS_END_OF_FILE;
354 DEBUG(3,("smbd_smb2_read: fnum=[%d/%s] length=%lu offset=%lu read=%lu\n",
355 fsp->fnum,
356 fsp_str_dbg(fsp),
357 (unsigned long)state->in_length,
358 (unsigned long)state->in_offset,
359 (unsigned long)nread));
361 state->out_data.length = nread;
362 state->out_remaining = 0;
364 return NT_STATUS_OK;
367 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
368 struct tevent_context *ev,
369 struct smbd_smb2_request *smb2req,
370 uint32_t in_smbpid,
371 uint64_t in_file_id_volatile,
372 uint32_t in_length,
373 uint64_t in_offset,
374 uint32_t in_minimum,
375 uint32_t in_remaining)
377 NTSTATUS status;
378 struct tevent_req *req = NULL;
379 struct smbd_smb2_read_state *state = NULL;
380 struct smb_request *smbreq = NULL;
381 connection_struct *conn = smb2req->tcon->compat_conn;
382 files_struct *fsp = NULL;
383 ssize_t nread = -1;
384 struct lock_struct lock;
385 int saved_errno;
387 req = tevent_req_create(mem_ctx, &state,
388 struct smbd_smb2_read_state);
389 if (req == NULL) {
390 return NULL;
392 state->smb2req = smb2req;
393 state->in_length = in_length;
394 state->in_offset = in_offset;
395 state->in_minimum = in_minimum;
396 state->out_data = data_blob_null;
397 state->out_remaining = 0;
399 DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
400 (unsigned long long)in_file_id_volatile));
402 smbreq = smbd_smb2_fake_smb_request(smb2req);
403 if (tevent_req_nomem(smbreq, req)) {
404 return tevent_req_post(req, ev);
407 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
408 if (fsp == NULL) {
409 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
410 return tevent_req_post(req, ev);
412 if (conn != fsp->conn) {
413 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
414 return tevent_req_post(req, ev);
416 if (smb2req->session->vuid != fsp->vuid) {
417 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
418 return tevent_req_post(req, ev);
420 if (fsp->is_directory) {
421 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
422 return tevent_req_post(req, ev);
425 state->fsp = fsp;
426 state->in_file_id_volatile = in_file_id_volatile;
428 if (IS_IPC(smbreq->conn)) {
429 struct tevent_req *subreq = NULL;
431 state->out_data = data_blob_talloc(state, NULL, in_length);
432 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
433 return tevent_req_post(req, ev);
436 if (!fsp_is_np(fsp)) {
437 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
438 return tevent_req_post(req, ev);
441 subreq = np_read_send(state, smbd_event_context(),
442 fsp->fake_file_handle,
443 state->out_data.data,
444 state->out_data.length);
445 if (tevent_req_nomem(subreq, req)) {
446 return tevent_req_post(req, ev);
448 tevent_req_set_callback(subreq,
449 smbd_smb2_read_pipe_done,
450 req);
451 return req;
454 if (!CHECK_READ(fsp, smbreq)) {
455 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
456 return tevent_req_post(req, ev);
459 status = schedule_smb2_aio_read(fsp->conn,
460 smbreq,
461 fsp,
462 state,
463 &state->out_data,
464 (SMB_OFF_T)in_offset,
465 (size_t)in_length);
467 if (NT_STATUS_IS_OK(status)) {
469 * Doing an async read. Don't
470 * send a "gone async" message
471 * as we expect this to be less
472 * than the client timeout period.
473 * JRA. FIXME for offline files..
474 * FIXME. Add cancel code..
476 smb2req->async = true;
477 return req;
480 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
481 /* Real error in setting up aio. Fail. */
482 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
483 return tevent_req_post(req, ev);
486 /* Fallback to synchronous. */
488 init_strict_lock_struct(fsp,
489 in_file_id_volatile,
490 in_offset,
491 in_length,
492 READ_LOCK,
493 &lock);
495 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
496 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
497 return tevent_req_post(req, ev);
500 /* Try sendfile in preference. */
501 status = schedule_smb2_sendfile_read(smb2req, state);
502 if (NT_STATUS_IS_OK(status)) {
503 tevent_req_done(req);
504 return tevent_req_post(req, ev);
505 } else {
506 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
507 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
508 tevent_req_nterror(req, status);
509 return tevent_req_post(req, ev);
513 /* Ok, read into memory. Allocate the out buffer. */
514 state->out_data = data_blob_talloc(state, NULL, in_length);
515 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
516 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
517 return tevent_req_post(req, ev);
520 nread = read_file(fsp,
521 (char *)state->out_data.data,
522 in_offset,
523 in_length);
525 saved_errno = errno;
527 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
529 DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
530 "len=%llu returned %lld\n",
531 fsp_str_dbg(fsp),
532 (unsigned long long)in_file_id_volatile,
533 (unsigned long long)in_offset,
534 (unsigned long long)in_length,
535 (long long)nread));
537 status = smb2_read_complete(req, nread, saved_errno);
538 if (!NT_STATUS_IS_OK(status)) {
539 tevent_req_nterror(req, status);
540 } else {
541 /* Success. */
542 tevent_req_done(req);
544 return tevent_req_post(req, ev);
547 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
549 struct tevent_req *req = tevent_req_callback_data(subreq,
550 struct tevent_req);
551 struct smbd_smb2_read_state *state = tevent_req_data(req,
552 struct smbd_smb2_read_state);
553 NTSTATUS status;
554 ssize_t nread = -1;
555 bool is_data_outstanding;
557 status = np_read_recv(subreq, &nread, &is_data_outstanding);
558 TALLOC_FREE(subreq);
559 if (!NT_STATUS_IS_OK(status)) {
560 tevent_req_nterror(req, status);
561 return;
564 if (nread == 0 && state->out_data.length != 0) {
565 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
566 return;
569 state->out_data.length = nread;
570 state->out_remaining = 0;
573 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
574 * handle it in SMB1 pipe_read_andx_done().
577 tevent_req_done(req);
580 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
581 TALLOC_CTX *mem_ctx,
582 DATA_BLOB *out_data,
583 uint32_t *out_remaining)
585 NTSTATUS status;
586 struct smbd_smb2_read_state *state = tevent_req_data(req,
587 struct smbd_smb2_read_state);
589 if (tevent_req_is_nterror(req, &status)) {
590 tevent_req_received(req);
591 return status;
594 *out_data = state->out_data;
595 talloc_steal(mem_ctx, out_data->data);
596 *out_remaining = state->out_remaining;
598 tevent_req_received(req);
599 return NT_STATUS_OK;