winbindd: queryuser - only get group name if needed
[Samba.git] / source3 / smbd / smb2_read.c
blobecedf1ef0bf14acf57e60c770024eae200899c9e
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"
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,
35 uint8_t in_flags,
36 uint32_t in_length,
37 uint64_t in_offset,
38 uint32_t in_minimum,
39 uint32_t in_remaining);
40 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
41 TALLOC_CTX *mem_ctx,
42 DATA_BLOB *out_data,
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;
49 NTSTATUS status;
50 const uint8_t *inbody;
51 uint8_t in_flags;
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 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);
69 } else {
70 in_flags = 0;
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);
93 if (in_fsp == NULL) {
94 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
97 subreq = smbd_smb2_read_send(req, req->sconn->ev_ctx,
98 req, in_fsp,
99 in_flags,
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->xconn,
133 nt_errstr(error));
134 return;
136 return;
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,
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->xconn,
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 uint8_t in_flags;
177 uint32_t in_length;
178 uint64_t in_offset;
179 uint32_t in_minimum;
180 DATA_BLOB out_headers;
181 uint8_t _out_hdr_buf[NBT_HDR_SIZE + SMB2_HDR_BODY + 0x10];
182 DATA_BLOB out_data;
183 uint32_t out_remaining;
186 static int smb2_smb2_read_state_deny_destructor(struct smbd_smb2_read_state *state)
188 return -1;
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;
201 ssize_t nread;
202 ssize_t ret;
203 int saved_errno;
205 nread = SMB_VFS_SENDFILE(xconn->transport.sock,
206 fsp,
207 hdr,
208 in_offset,
209 in_length);
210 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
211 (int)nread,
212 fsp_str_dbg(fsp) ));
214 if (nread == -1) {
215 saved_errno = errno;
218 * Returning ENOSYS means no data at all was sent.
219 Do this as a normal read. */
220 if (errno == ENOSYS) {
221 goto normal_read;
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");
228 goto normal_read;
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);
239 if (nread == -1) {
240 saved_errno = errno;
241 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
242 "failed for file %s (%s) for client %s. "
243 "Terminating\n",
244 fsp_str_dbg(fsp), strerror(saved_errno),
245 smbXsrv_connection_dbg(xconn)));
246 *pstatus = map_nt_error_from_unix_common(saved_errno);
247 return 0;
249 goto out;
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);
257 return 0;
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",
268 fsp_str_dbg(fsp)));
269 goto normal_read;
273 * We got a short read
275 goto out;
277 normal_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) {
282 saved_errno = errno;
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);
288 return 0;
290 nread = fake_sendfile(xconn, fsp, in_offset, in_length);
291 if (nread == -1) {
292 saved_errno = errno;
293 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
294 "failed for file %s (%s) for client %s. "
295 "Terminating\n",
296 fsp_str_dbg(fsp), strerror(saved_errno),
297 smbXsrv_connection_dbg(xconn)));
298 *pstatus = map_nt_error_from_unix_common(saved_errno);
299 return 0;
302 out:
304 if (nread < in_length) {
305 ret = sendfile_short_send(xconn, fsp, nread,
306 hdr->length, in_length);
307 if (ret == -1) {
308 saved_errno = errno;
309 DEBUG(0,("%s: sendfile_short_send "
310 "failed for file %s (%s) for client %s. "
311 "Terminating\n",
312 __func__,
313 fsp_str_dbg(fsp), strerror(saved_errno),
314 smbXsrv_connection_dbg(xconn)));
315 *pstatus = map_nt_error_from_unix_common(saved_errno);
316 return 0;
320 init_strict_lock_struct(fsp,
321 fsp->op->global->open_persistent_id,
322 in_offset,
323 in_length,
324 READ_LOCK,
325 &lock);
327 *pstatus = NT_STATUS_OK;
328 return 0;
331 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
332 struct smbd_smb2_read_state *state)
334 files_struct *fsp = state->fsp;
337 * We cannot use sendfile if...
338 * We were not configured to do so OR
339 * Signing is active OR
340 * This is a compound SMB2 operation OR
341 * fsp is a STREAM file OR
342 * We're using a write cache OR
343 * It's not a regular file OR
344 * Requested offset is greater than file size OR
345 * there's not enough data in the file.
346 * Phew :-). Luckily this means most
347 * reads on most normal files. JRA.
350 if (!lp__use_sendfile(SNUM(fsp->conn)) ||
351 smb2req->do_signing ||
352 smb2req->do_encryption ||
353 smb2req->in.vector_count >= (2*SMBD_SMB2_NUM_IOV_PER_REQ) ||
354 (fsp->base_fsp != NULL) ||
355 (fsp->wcp != NULL) ||
356 (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
357 (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
358 (fsp->fsp_name->st.st_ex_size < state->in_offset + state->in_length))
360 return NT_STATUS_RETRY;
363 /* We've already checked there's this amount of data
364 to read. */
365 state->out_data.length = state->in_length;
366 state->out_remaining = 0;
368 state->out_headers = data_blob_const(state->_out_hdr_buf,
369 sizeof(state->_out_hdr_buf));
370 return NT_STATUS_OK;
373 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
375 /*******************************************************************
376 Common read complete processing function for both synchronous and
377 asynchronous reads.
378 *******************************************************************/
380 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
382 struct smbd_smb2_read_state *state = tevent_req_data(req,
383 struct smbd_smb2_read_state);
384 files_struct *fsp = state->fsp;
386 if (nread < 0) {
387 NTSTATUS status = map_nt_error_from_unix(err);
389 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
390 "Error = %s (NTSTATUS %s)\n",
391 fsp_str_dbg(fsp),
392 (int)nread,
393 strerror(err),
394 nt_errstr(status)));
396 return status;
398 if (nread == 0 && state->in_length != 0) {
399 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
400 fsp_str_dbg(fsp)));
401 return NT_STATUS_END_OF_FILE;
404 if (nread < state->in_minimum) {
405 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
406 "minimum requested %u. Returning end of file\n",
407 fsp_str_dbg(fsp),
408 (int)nread,
409 (unsigned int)state->in_minimum));
410 return NT_STATUS_END_OF_FILE;
413 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
414 fsp_fnum_dbg(fsp),
415 fsp_str_dbg(fsp),
416 (unsigned long)state->in_length,
417 (unsigned long)state->in_offset,
418 (unsigned long)nread));
420 state->out_data.length = nread;
421 state->out_remaining = 0;
423 return NT_STATUS_OK;
426 static bool smbd_smb2_read_cancel(struct tevent_req *req)
428 struct smbd_smb2_read_state *state =
429 tevent_req_data(req,
430 struct smbd_smb2_read_state);
432 return cancel_smb2_aio(state->smbreq);
435 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
436 struct tevent_context *ev,
437 struct smbd_smb2_request *smb2req,
438 struct files_struct *fsp,
439 uint8_t in_flags,
440 uint32_t in_length,
441 uint64_t in_offset,
442 uint32_t in_minimum,
443 uint32_t in_remaining)
445 NTSTATUS status;
446 struct tevent_req *req = NULL;
447 struct smbd_smb2_read_state *state = NULL;
448 struct smb_request *smbreq = NULL;
449 connection_struct *conn = smb2req->tcon->compat;
450 ssize_t nread = -1;
451 struct lock_struct lock;
452 int saved_errno;
454 req = tevent_req_create(mem_ctx, &state,
455 struct smbd_smb2_read_state);
456 if (req == NULL) {
457 return NULL;
459 state->smb2req = smb2req;
460 state->in_flags = in_flags;
461 state->in_length = in_length;
462 state->in_offset = in_offset;
463 state->in_minimum = in_minimum;
464 state->out_data = data_blob_null;
465 state->out_remaining = 0;
467 DEBUG(10,("smbd_smb2_read: %s - %s\n",
468 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
470 smbreq = smbd_smb2_fake_smb_request(smb2req);
471 if (tevent_req_nomem(smbreq, req)) {
472 return tevent_req_post(req, ev);
474 state->smbreq = smbreq;
476 if (fsp->is_directory) {
477 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
478 return tevent_req_post(req, ev);
481 state->fsp = fsp;
483 if (IS_IPC(smbreq->conn)) {
484 struct tevent_req *subreq = NULL;
486 state->out_data = data_blob_talloc(state, NULL, in_length);
487 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
488 return tevent_req_post(req, ev);
491 if (!fsp_is_np(fsp)) {
492 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
493 return tevent_req_post(req, ev);
496 subreq = np_read_send(state, ev,
497 fsp->fake_file_handle,
498 state->out_data.data,
499 state->out_data.length);
500 if (tevent_req_nomem(subreq, req)) {
501 return tevent_req_post(req, ev);
503 tevent_req_set_callback(subreq,
504 smbd_smb2_read_pipe_done,
505 req);
506 return req;
509 if (!CHECK_READ_SMB2(fsp)) {
510 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
511 return tevent_req_post(req, ev);
514 status = schedule_smb2_aio_read(fsp->conn,
515 smbreq,
516 fsp,
517 state,
518 &state->out_data,
519 (off_t)in_offset,
520 (size_t)in_length);
522 if (NT_STATUS_IS_OK(status)) {
524 * Doing an async read, allow this
525 * request to be canceled
527 tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
528 return req;
531 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
532 /* Real error in setting up aio. Fail. */
533 tevent_req_nterror(req, status);
534 return tevent_req_post(req, ev);
537 /* Fallback to synchronous. */
539 init_strict_lock_struct(fsp,
540 fsp->op->global->open_persistent_id,
541 in_offset,
542 in_length,
543 READ_LOCK,
544 &lock);
546 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
547 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
548 return tevent_req_post(req, ev);
551 /* Try sendfile in preference. */
552 status = schedule_smb2_sendfile_read(smb2req, state);
553 if (NT_STATUS_IS_OK(status)) {
554 tevent_req_done(req);
555 return tevent_req_post(req, ev);
556 } else {
557 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
558 tevent_req_nterror(req, status);
559 return tevent_req_post(req, ev);
563 /* Ok, read into memory. Allocate the out buffer. */
564 state->out_data = data_blob_talloc(state, NULL, in_length);
565 if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
566 return tevent_req_post(req, ev);
569 nread = read_file(fsp,
570 (char *)state->out_data.data,
571 in_offset,
572 in_length);
574 saved_errno = errno;
576 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
577 "len=%llu returned %lld\n",
578 fsp_str_dbg(fsp),
579 fsp_fnum_dbg(fsp),
580 (unsigned long long)in_offset,
581 (unsigned long long)in_length,
582 (long long)nread));
584 status = smb2_read_complete(req, nread, saved_errno);
585 if (!NT_STATUS_IS_OK(status)) {
586 tevent_req_nterror(req, status);
587 } else {
588 /* Success. */
589 tevent_req_done(req);
591 return tevent_req_post(req, ev);
594 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
596 struct tevent_req *req = tevent_req_callback_data(subreq,
597 struct tevent_req);
598 struct smbd_smb2_read_state *state = tevent_req_data(req,
599 struct smbd_smb2_read_state);
600 NTSTATUS status;
601 ssize_t nread = -1;
602 bool is_data_outstanding;
604 status = np_read_recv(subreq, &nread, &is_data_outstanding);
605 TALLOC_FREE(subreq);
606 if (!NT_STATUS_IS_OK(status)) {
607 NTSTATUS old = status;
608 status = nt_status_np_pipe(old);
609 tevent_req_nterror(req, status);
610 return;
613 if (nread == 0 && state->out_data.length != 0) {
614 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
615 return;
618 state->out_data.length = nread;
619 state->out_remaining = 0;
622 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
623 * handle it in SMB1 pipe_read_andx_done().
626 tevent_req_done(req);
629 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
630 TALLOC_CTX *mem_ctx,
631 DATA_BLOB *out_data,
632 uint32_t *out_remaining)
634 NTSTATUS status;
635 struct smbd_smb2_read_state *state = tevent_req_data(req,
636 struct smbd_smb2_read_state);
638 if (tevent_req_is_nterror(req, &status)) {
639 tevent_req_received(req);
640 return status;
643 *out_data = state->out_data;
644 talloc_steal(mem_ctx, out_data->data);
645 *out_remaining = state->out_remaining;
647 if (state->out_headers.length > 0) {
648 talloc_steal(mem_ctx, state);
649 talloc_set_destructor(state, smb2_smb2_read_state_deny_destructor);
650 tevent_req_received(req);
651 state->smb2req->queue_entry.sendfile_header = &state->out_headers;
652 talloc_set_destructor(state, smb2_sendfile_send_data);
653 } else {
654 tevent_req_received(req);
657 return NT_STATUS_OK;