s4 dns: Only forward for zones we don't own
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_read.c
blob0b6e2ee4618b8749c442b98291237c31d7c1c2c2
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 status = smbd_smb2_request_verify_creditcharge(req, in_length);
84 if (!NT_STATUS_IS_OK(status)) {
85 return smbd_smb2_request_error(req, status);
88 if (req->compat_chain_fsp) {
89 /* skip check */
90 } else if (in_file_id_persistent != in_file_id_volatile) {
91 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
94 subreq = smbd_smb2_read_send(req,
95 req->sconn->ev_ctx,
96 req,
97 in_smbpid,
98 in_file_id_volatile,
99 in_length,
100 in_offset,
101 in_minimum_count,
102 in_remaining_bytes);
103 if (subreq == NULL) {
104 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
106 tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
108 return smbd_smb2_request_pending_queue(req, subreq, 500);
111 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
113 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
114 struct smbd_smb2_request);
115 DATA_BLOB outbody;
116 DATA_BLOB outdyn;
117 uint8_t out_data_offset;
118 DATA_BLOB out_data_buffer = data_blob_null;
119 uint32_t out_data_remaining = 0;
120 NTSTATUS status;
121 NTSTATUS error; /* transport error */
123 status = smbd_smb2_read_recv(subreq,
124 req,
125 &out_data_buffer,
126 &out_data_remaining);
127 TALLOC_FREE(subreq);
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,
132 nt_errstr(error));
133 return;
135 return;
138 out_data_offset = SMB2_HDR_BODY + 0x10;
140 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
141 if (outbody.data == NULL) {
142 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
143 if (!NT_STATUS_IS_OK(error)) {
144 smbd_server_connection_terminate(req->sconn,
145 nt_errstr(error));
146 return;
148 return;
151 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
152 SCVAL(outbody.data, 0x02,
153 out_data_offset); /* data offset */
154 SCVAL(outbody.data, 0x03, 0); /* reserved */
155 SIVAL(outbody.data, 0x04,
156 out_data_buffer.length); /* data length */
157 SIVAL(outbody.data, 0x08,
158 out_data_remaining); /* data remaining */
159 SIVAL(outbody.data, 0x0C, 0); /* reserved */
161 outdyn = out_data_buffer;
163 error = smbd_smb2_request_done(req, outbody, &outdyn);
164 if (!NT_STATUS_IS_OK(error)) {
165 smbd_server_connection_terminate(req->sconn,
166 nt_errstr(error));
167 return;
171 struct smbd_smb2_read_state {
172 struct smbd_smb2_request *smb2req;
173 struct smb_request *smbreq;
174 files_struct *fsp;
175 uint64_t in_file_id_volatile;
176 uint32_t in_length;
177 uint64_t in_offset;
178 uint32_t in_minimum;
179 DATA_BLOB out_data;
180 uint32_t out_remaining;
183 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
184 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
186 struct lock_struct lock;
187 uint32_t in_length = state->in_length;
188 uint64_t in_offset = state->in_offset;
189 files_struct *fsp = state->fsp;
190 ssize_t nread;
192 nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
193 fsp,
194 NULL,
195 in_offset,
196 in_length);
197 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
198 (int)nread,
199 fsp_str_dbg(fsp) ));
201 if (nread == -1) {
202 if (errno == ENOSYS || errno == EINTR) {
204 * Special hack for broken systems with no working
205 * sendfile. Fake this up by doing read/write calls.
207 set_use_sendfile(SNUM(fsp->conn), false);
208 nread = fake_sendfile(fsp, in_offset, in_length);
209 if (nread == -1) {
210 DEBUG(0,("smb2_sendfile_send_data: "
211 "fake_sendfile failed for "
212 "file %s (%s).\n",
213 fsp_str_dbg(fsp),
214 strerror(errno)));
215 exit_server_cleanly("smb2_sendfile_send_data: "
216 "fake_sendfile failed");
218 goto out;
221 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
222 "%s (%s). Terminating\n",
223 fsp_str_dbg(fsp),
224 strerror(errno)));
225 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
226 } else if (nread == 0) {
228 * Some sendfile implementations return 0 to indicate
229 * that there was a short read, but nothing was
230 * actually written to the socket. In this case,
231 * fallback to the normal read path so the header gets
232 * the correct byte count.
234 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
235 "falling back to the normal read: %s\n",
236 fsp_str_dbg(fsp)));
238 nread = fake_sendfile(fsp, in_offset, in_length);
239 if (nread == -1) {
240 DEBUG(0,("smb2_sendfile_send_data: "
241 "fake_sendfile failed for file "
242 "%s (%s). Terminating\n",
243 fsp_str_dbg(fsp),
244 strerror(errno)));
245 exit_server_cleanly("smb2_sendfile_send_data: "
246 "fake_sendfile failed");
250 out:
252 if (nread < in_length) {
253 sendfile_short_send(fsp, nread, 0, in_length);
256 init_strict_lock_struct(fsp,
257 state->in_file_id_volatile,
258 in_offset,
259 in_length,
260 READ_LOCK,
261 &lock);
263 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
264 return 0;
267 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
268 struct smbd_smb2_read_state *state)
270 struct smbd_smb2_read_state *state_copy = NULL;
271 files_struct *fsp = state->fsp;
274 * We cannot use sendfile if...
275 * We were not configured to do so OR
276 * Signing is active OR
277 * This is a compound SMB2 operation OR
278 * fsp is a STREAM file OR
279 * We're using a write cache OR
280 * It's not a regular file OR
281 * Requested offset is greater than file size OR
282 * there's not enough data in the file.
283 * Phew :-). Luckily this means most
284 * reads on most normal files. JRA.
287 if (!lp__use_sendfile(SNUM(fsp->conn)) ||
288 smb2req->do_signing ||
289 smb2req->in.vector_count != 4 ||
290 (fsp->base_fsp != NULL) ||
291 (fsp->wcp != NULL) ||
292 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
293 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
294 (fsp->fsp_name->st.st_ex_size < state->in_offset +
295 state->in_length)) {
296 return NT_STATUS_RETRY;
299 /* We've already checked there's this amount of data
300 to read. */
301 state->out_data.length = state->in_length;
302 state->out_remaining = 0;
304 /* Make a copy of state attached to the smb2req. Attach
305 the destructor here as this will trigger the sendfile
306 call when the request is destroyed. */
307 state_copy = talloc(smb2req, struct smbd_smb2_read_state);
308 if (!state_copy) {
309 return NT_STATUS_NO_MEMORY;
311 *state_copy = *state;
312 talloc_set_destructor(state_copy, smb2_sendfile_send_data);
313 return NT_STATUS_OK;
316 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
318 /*******************************************************************
319 Common read complete processing function for both synchronous and
320 asynchronous reads.
321 *******************************************************************/
323 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
325 struct smbd_smb2_read_state *state = tevent_req_data(req,
326 struct smbd_smb2_read_state);
327 files_struct *fsp = state->fsp;
329 if (nread < 0) {
330 NTSTATUS status = map_nt_error_from_unix(err);
332 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
333 "Error = %s (NTSTATUS %s)\n",
334 fsp_str_dbg(fsp),
335 (int)nread,
336 strerror(err),
337 nt_errstr(status)));
339 return status;
341 if (nread == 0 && state->in_length != 0) {
342 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
343 fsp_str_dbg(fsp)));
344 return NT_STATUS_END_OF_FILE;
347 if (nread < state->in_minimum) {
348 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
349 "minimum requested %u. Returning end of file\n",
350 fsp_str_dbg(fsp),
351 (int)nread,
352 (unsigned int)state->in_minimum));
353 return NT_STATUS_END_OF_FILE;
356 DEBUG(3,("smbd_smb2_read: fnum=[%d/%s] length=%lu offset=%lu read=%lu\n",
357 fsp->fnum,
358 fsp_str_dbg(fsp),
359 (unsigned long)state->in_length,
360 (unsigned long)state->in_offset,
361 (unsigned long)nread));
363 state->out_data.length = nread;
364 state->out_remaining = 0;
366 return NT_STATUS_OK;
369 static bool smbd_smb2_read_cancel(struct tevent_req *req)
371 struct smbd_smb2_read_state *state =
372 tevent_req_data(req,
373 struct smbd_smb2_read_state);
375 state->smb2req->cancelled = true;
377 return cancel_smb2_aio(state->smbreq);
380 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
381 struct tevent_context *ev,
382 struct smbd_smb2_request *smb2req,
383 uint32_t in_smbpid,
384 uint64_t in_file_id_volatile,
385 uint32_t in_length,
386 uint64_t in_offset,
387 uint32_t in_minimum,
388 uint32_t in_remaining)
390 NTSTATUS status;
391 struct tevent_req *req = NULL;
392 struct smbd_smb2_read_state *state = NULL;
393 struct smb_request *smbreq = NULL;
394 connection_struct *conn = smb2req->tcon->compat_conn;
395 files_struct *fsp = NULL;
396 ssize_t nread = -1;
397 struct lock_struct lock;
398 int saved_errno;
400 req = tevent_req_create(mem_ctx, &state,
401 struct smbd_smb2_read_state);
402 if (req == NULL) {
403 return NULL;
405 state->smb2req = smb2req;
406 state->in_length = in_length;
407 state->in_offset = in_offset;
408 state->in_minimum = in_minimum;
409 state->out_data = data_blob_null;
410 state->out_remaining = 0;
412 DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
413 (unsigned long long)in_file_id_volatile));
415 smbreq = smbd_smb2_fake_smb_request(smb2req);
416 if (tevent_req_nomem(smbreq, req)) {
417 return tevent_req_post(req, ev);
419 state->smbreq = smbreq;
421 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
422 if (fsp == NULL) {
423 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
424 return tevent_req_post(req, ev);
426 if (conn != fsp->conn) {
427 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
428 return tevent_req_post(req, ev);
430 if (smb2req->session->vuid != fsp->vuid) {
431 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
432 return tevent_req_post(req, ev);
434 if (fsp->is_directory) {
435 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
436 return tevent_req_post(req, ev);
439 state->fsp = fsp;
440 state->in_file_id_volatile = in_file_id_volatile;
442 if (IS_IPC(smbreq->conn)) {
443 struct tevent_req *subreq = NULL;
445 state->out_data = data_blob_talloc(state, NULL, in_length);
446 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
447 return tevent_req_post(req, ev);
450 if (!fsp_is_np(fsp)) {
451 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
452 return tevent_req_post(req, ev);
455 subreq = np_read_send(state, ev,
456 fsp->fake_file_handle,
457 state->out_data.data,
458 state->out_data.length);
459 if (tevent_req_nomem(subreq, req)) {
460 return tevent_req_post(req, ev);
462 tevent_req_set_callback(subreq,
463 smbd_smb2_read_pipe_done,
464 req);
465 return req;
468 if (!CHECK_READ(fsp, smbreq)) {
469 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
470 return tevent_req_post(req, ev);
473 status = schedule_smb2_aio_read(fsp->conn,
474 smbreq,
475 fsp,
476 state,
477 &state->out_data,
478 (SMB_OFF_T)in_offset,
479 (size_t)in_length);
481 if (NT_STATUS_IS_OK(status)) {
483 * Doing an async read, allow this
484 * request to be canceled
486 tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
487 return req;
490 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
491 /* Real error in setting up aio. Fail. */
492 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
493 return tevent_req_post(req, ev);
496 /* Fallback to synchronous. */
498 init_strict_lock_struct(fsp,
499 in_file_id_volatile,
500 in_offset,
501 in_length,
502 READ_LOCK,
503 &lock);
505 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
506 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
507 return tevent_req_post(req, ev);
510 /* Try sendfile in preference. */
511 status = schedule_smb2_sendfile_read(smb2req, state);
512 if (NT_STATUS_IS_OK(status)) {
513 tevent_req_done(req);
514 return tevent_req_post(req, ev);
515 } else {
516 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
517 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
518 tevent_req_nterror(req, status);
519 return tevent_req_post(req, ev);
523 /* Ok, read into memory. Allocate the out buffer. */
524 state->out_data = data_blob_talloc(state, NULL, in_length);
525 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
526 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
527 return tevent_req_post(req, ev);
530 nread = read_file(fsp,
531 (char *)state->out_data.data,
532 in_offset,
533 in_length);
535 saved_errno = errno;
537 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
539 DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
540 "len=%llu returned %lld\n",
541 fsp_str_dbg(fsp),
542 (unsigned long long)in_file_id_volatile,
543 (unsigned long long)in_offset,
544 (unsigned long long)in_length,
545 (long long)nread));
547 status = smb2_read_complete(req, nread, saved_errno);
548 if (!NT_STATUS_IS_OK(status)) {
549 tevent_req_nterror(req, status);
550 } else {
551 /* Success. */
552 tevent_req_done(req);
554 return tevent_req_post(req, ev);
557 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
559 struct tevent_req *req = tevent_req_callback_data(subreq,
560 struct tevent_req);
561 struct smbd_smb2_read_state *state = tevent_req_data(req,
562 struct smbd_smb2_read_state);
563 NTSTATUS status;
564 ssize_t nread = -1;
565 bool is_data_outstanding;
567 status = np_read_recv(subreq, &nread, &is_data_outstanding);
568 TALLOC_FREE(subreq);
569 if (!NT_STATUS_IS_OK(status)) {
570 tevent_req_nterror(req, status);
571 return;
574 if (nread == 0 && state->out_data.length != 0) {
575 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
576 return;
579 state->out_data.length = nread;
580 state->out_remaining = 0;
583 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
584 * handle it in SMB1 pipe_read_andx_done().
587 tevent_req_done(req);
590 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
591 TALLOC_CTX *mem_ctx,
592 DATA_BLOB *out_data,
593 uint32_t *out_remaining)
595 NTSTATUS status;
596 struct smbd_smb2_read_state *state = tevent_req_data(req,
597 struct smbd_smb2_read_state);
599 if (tevent_req_is_nterror(req, &status)) {
600 tevent_req_received(req);
601 return status;
604 *out_data = state->out_data;
605 talloc_steal(mem_ctx, out_data->data);
606 *out_remaining = state->out_remaining;
608 tevent_req_received(req);
609 return NT_STATUS_OK;