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"
29 #include "lib/util/sys_rw_data.h"
32 #define DBGC_CLASS DBGC_SMB2
34 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
35 struct tevent_context
*ev
,
36 struct smbd_smb2_request
*smb2req
,
37 struct files_struct
*in_fsp
,
42 uint32_t in_remaining
);
43 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
46 uint32_t *out_remaining
);
48 static void smbd_smb2_request_read_done(struct tevent_req
*subreq
);
49 NTSTATUS
smbd_smb2_request_process_read(struct smbd_smb2_request
*req
)
51 struct smbXsrv_connection
*xconn
= req
->xconn
;
53 const uint8_t *inbody
;
57 uint64_t in_file_id_persistent
;
58 uint64_t in_file_id_volatile
;
59 struct files_struct
*in_fsp
;
60 uint32_t in_minimum_count
;
61 uint32_t in_remaining_bytes
;
62 struct tevent_req
*subreq
;
64 status
= smbd_smb2_request_verify_sizes(req
, 0x31);
65 if (!NT_STATUS_IS_OK(status
)) {
66 return smbd_smb2_request_error(req
, status
);
68 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
70 if (xconn
->protocol
>= PROTOCOL_SMB3_02
) {
71 in_flags
= CVAL(inbody
, 0x03);
75 in_length
= IVAL(inbody
, 0x04);
76 in_offset
= BVAL(inbody
, 0x08);
77 in_file_id_persistent
= BVAL(inbody
, 0x10);
78 in_file_id_volatile
= BVAL(inbody
, 0x18);
79 in_minimum_count
= IVAL(inbody
, 0x20);
80 in_remaining_bytes
= IVAL(inbody
, 0x28);
82 /* check the max read size */
83 if (in_length
> xconn
->smb2
.server
.max_read
) {
84 DEBUG(2,("smbd_smb2_request_process_read: "
85 "client ignored max read: %s: 0x%08X: 0x%08X\n",
86 __location__
, in_length
, xconn
->smb2
.server
.max_read
));
87 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
90 status
= smbd_smb2_request_verify_creditcharge(req
, in_length
);
91 if (!NT_STATUS_IS_OK(status
)) {
92 return smbd_smb2_request_error(req
, status
);
95 in_fsp
= file_fsp_smb2(req
, in_file_id_persistent
, in_file_id_volatile
);
97 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
100 subreq
= smbd_smb2_read_send(req
, req
->sconn
->ev_ctx
,
107 if (subreq
== NULL
) {
108 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
110 tevent_req_set_callback(subreq
, smbd_smb2_request_read_done
, req
);
112 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
115 static void smbd_smb2_request_read_done(struct tevent_req
*subreq
)
117 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
118 struct smbd_smb2_request
);
120 uint8_t body_padding
= req
->xconn
->smb2
.smbtorture
.read_body_padding
;
123 uint8_t out_data_offset
;
124 DATA_BLOB out_data_buffer
= data_blob_null
;
125 uint32_t out_data_remaining
= 0;
127 NTSTATUS error
; /* transport error */
129 status
= smbd_smb2_read_recv(subreq
,
132 &out_data_remaining
);
134 if (!NT_STATUS_IS_OK(status
)) {
135 error
= smbd_smb2_request_error(req
, status
);
136 if (!NT_STATUS_IS_OK(error
)) {
137 smbd_server_connection_terminate(req
->xconn
,
145 * Only FSCTL_SMBTORTURE_GLOBAL_READ_RESPONSE_BODY_PADDING8
146 * sets body_padding to a value different from 0.
148 body_size
= 0x10 + body_padding
;
149 out_data_offset
= SMB2_HDR_BODY
+ body_size
;
151 outbody
= smbd_smb2_generate_outbody(req
, body_size
);
152 if (outbody
.data
== NULL
) {
153 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
154 if (!NT_STATUS_IS_OK(error
)) {
155 smbd_server_connection_terminate(req
->xconn
,
162 SSVAL(outbody
.data
, 0x00, 0x10 + 1); /* struct size */
163 SCVAL(outbody
.data
, 0x02,
164 out_data_offset
); /* data offset */
165 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
166 SIVAL(outbody
.data
, 0x04,
167 out_data_buffer
.length
); /* data length */
168 SIVAL(outbody
.data
, 0x08,
169 out_data_remaining
); /* data remaining */
170 SIVAL(outbody
.data
, 0x0C, 0); /* reserved */
171 if (body_padding
!= 0) {
172 memset(outbody
.data
+ 0x10, 0, body_padding
);
175 outdyn
= out_data_buffer
;
177 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
178 if (!NT_STATUS_IS_OK(error
)) {
179 smbd_server_connection_terminate(req
->xconn
,
185 struct smbd_smb2_read_state
{
186 struct smbd_smb2_request
*smb2req
;
187 struct smb_request
*smbreq
;
193 DATA_BLOB out_headers
;
194 uint8_t _out_hdr_buf
[NBT_HDR_SIZE
+ SMB2_HDR_BODY
+ 0x10];
196 uint32_t out_remaining
;
199 static int smb2_smb2_read_state_deny_destructor(struct smbd_smb2_read_state
*state
)
204 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
205 static int smb2_sendfile_send_data(struct smbd_smb2_read_state
*state
)
207 uint32_t in_length
= state
->in_length
;
208 uint64_t in_offset
= state
->in_offset
;
209 files_struct
*fsp
= state
->fsp
;
210 const DATA_BLOB
*hdr
= state
->smb2req
->queue_entry
.sendfile_header
;
211 NTSTATUS
*pstatus
= state
->smb2req
->queue_entry
.sendfile_status
;
212 struct smbXsrv_connection
*xconn
= state
->smb2req
->xconn
;
217 nread
= SMB_VFS_SENDFILE(xconn
->transport
.sock
,
222 DBG_DEBUG("SMB_VFS_SENDFILE returned %zd on file %s\n",
230 * Returning ENOSYS means no data at all was sent.
231 Do this as a normal read. */
232 if (errno
== ENOSYS
) {
236 if (errno
== ENOTSUP
) {
237 set_use_sendfile(SNUM(fsp
->conn
), false);
238 DBG_WARNING("Disabling sendfile use as sendfile is "
239 "not supported by the system\n");
243 if (errno
== EINTR
) {
245 * Special hack for broken Linux with no working sendfile. If we
246 * return EINTR we sent the header but not the rest of the data.
247 * Fake this up by doing read/write calls.
249 set_use_sendfile(SNUM(fsp
->conn
), false);
250 nread
= fake_sendfile(xconn
, fsp
, in_offset
, in_length
);
253 DBG_ERR("fake_sendfile failed for file %s "
254 "(%s) for client %s. Terminating\n",
256 strerror(saved_errno
),
257 smbXsrv_connection_dbg(xconn
));
258 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
264 DBG_ERR("sendfile failed for file "
265 "%s (%s) for client %s. Terminating\n",
267 strerror(saved_errno
),
268 smbXsrv_connection_dbg(xconn
));
269 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
271 } else if (nread
== 0) {
273 * Some sendfile implementations return 0 to indicate
274 * that there was a short read, but nothing was
275 * actually written to the socket. In this case,
276 * fallback to the normal read path so the header gets
277 * the correct byte count.
279 DBG_NOTICE("sendfile sent zero bytes "
280 "falling back to the normal read: %s\n",
286 * We got a short read
291 /* Send out the header. */
292 ret
= write_data(xconn
->transport
.sock
,
293 (const char *)hdr
->data
, hdr
->length
);
294 if (ret
!= hdr
->length
) {
296 DBG_ERR("write_data failed for file "
297 "%s (%s) for client %s. Terminating\n",
299 strerror(saved_errno
),
300 smbXsrv_connection_dbg(xconn
));
301 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
304 nread
= fake_sendfile(xconn
, fsp
, in_offset
, in_length
);
307 DBG_ERR("fake_sendfile failed for file %s (%s) for client %s. "
310 strerror(saved_errno
),
311 smbXsrv_connection_dbg(xconn
));
312 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
318 if (nread
< in_length
) {
319 ret
= sendfile_short_send(xconn
, fsp
, nread
,
320 hdr
->length
, in_length
);
323 DBG_ERR("sendfile_short_send "
324 "failed for file %s (%s) for client %s. "
327 strerror(saved_errno
),
328 smbXsrv_connection_dbg(xconn
));
329 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
334 *pstatus
= NT_STATUS_OK
;
338 static NTSTATUS
schedule_smb2_sendfile_read(struct smbd_smb2_request
*smb2req
,
339 struct smbd_smb2_read_state
*state
)
341 files_struct
*fsp
= state
->fsp
;
344 * We cannot use sendfile if...
345 * We were not configured to do so OR
346 * Signing is active OR
347 * This is a compound SMB2 operation OR
348 * fsp is a STREAM file OR
349 * It's not a regular file OR
350 * Requested offset is greater than file size OR
351 * there's not enough data in the file.
352 * Phew :-). Luckily this means most
353 * reads on most normal files. JRA.
356 if (!lp__use_sendfile(SNUM(fsp
->conn
)) ||
357 smb2req
->do_signing
||
358 smb2req
->do_encryption
||
359 smbd_smb2_is_compound(smb2req
) ||
360 fsp_is_alternate_stream(fsp
) ||
361 (!S_ISREG(fsp
->fsp_name
->st
.st_ex_mode
)) ||
362 (state
->in_offset
>= fsp
->fsp_name
->st
.st_ex_size
) ||
363 (fsp
->fsp_name
->st
.st_ex_size
< state
->in_offset
+ state
->in_length
))
365 return NT_STATUS_RETRY
;
368 /* We've already checked there's this amount of data
370 state
->out_data
.length
= state
->in_length
;
371 state
->out_remaining
= 0;
373 state
->out_headers
= data_blob_const(state
->_out_hdr_buf
,
374 sizeof(state
->_out_hdr_buf
));
378 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
);
380 /*******************************************************************
381 Common read complete processing function for both synchronous and
383 *******************************************************************/
385 NTSTATUS
smb2_read_complete(struct tevent_req
*req
, ssize_t nread
, int err
)
387 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
388 struct smbd_smb2_read_state
);
389 files_struct
*fsp
= state
->fsp
;
392 NTSTATUS status
= map_nt_error_from_unix(err
);
394 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
395 "Error = %s (NTSTATUS %s)\n",
403 if (nread
== 0 && state
->in_length
!= 0) {
404 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
406 return NT_STATUS_END_OF_FILE
;
409 if (nread
< state
->in_minimum
) {
410 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
411 "minimum requested %u. Returning end of file\n",
414 (unsigned int)state
->in_minimum
));
415 return NT_STATUS_END_OF_FILE
;
418 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
421 (unsigned long)state
->in_length
,
422 (unsigned long)state
->in_offset
,
423 (unsigned long)nread
));
425 state
->out_data
.length
= nread
;
426 state
->out_remaining
= 0;
431 static bool smbd_smb2_read_cancel(struct tevent_req
*req
)
433 struct smbd_smb2_read_state
*state
=
435 struct smbd_smb2_read_state
);
437 return cancel_smb2_aio(state
->smbreq
);
440 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
441 struct tevent_context
*ev
,
442 struct smbd_smb2_request
*smb2req
,
443 struct files_struct
*fsp
,
448 uint32_t in_remaining
)
451 struct tevent_req
*req
= NULL
;
452 struct smbd_smb2_read_state
*state
= NULL
;
453 struct smb_request
*smbreq
= NULL
;
454 connection_struct
*conn
= smb2req
->tcon
->compat
;
456 struct lock_struct lock
;
459 req
= tevent_req_create(mem_ctx
, &state
,
460 struct smbd_smb2_read_state
);
464 state
->smb2req
= smb2req
;
465 state
->in_flags
= in_flags
;
466 state
->in_length
= in_length
;
467 state
->in_offset
= in_offset
;
468 state
->in_minimum
= in_minimum
;
469 state
->out_data
= data_blob_null
;
470 state
->out_remaining
= 0;
472 DEBUG(10,("smbd_smb2_read: %s - %s\n",
473 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
475 smbreq
= smbd_smb2_fake_smb_request(smb2req
, fsp
);
476 if (tevent_req_nomem(smbreq
, req
)) {
477 return tevent_req_post(req
, ev
);
479 state
->smbreq
= smbreq
;
481 if (fsp
->fsp_flags
.is_directory
) {
482 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
483 return tevent_req_post(req
, ev
);
488 if (IS_IPC(smbreq
->conn
)) {
489 struct tevent_req
*subreq
= NULL
;
492 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
493 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
494 return tevent_req_post(req
, ev
);
497 if (!fsp_is_np(fsp
)) {
498 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
499 return tevent_req_post(req
, ev
);
502 subreq
= np_read_send(state
, ev
,
503 fsp
->fake_file_handle
,
504 state
->out_data
.data
,
505 state
->out_data
.length
);
506 if (tevent_req_nomem(subreq
, req
)) {
507 return tevent_req_post(req
, ev
);
509 tevent_req_set_callback(subreq
,
510 smbd_smb2_read_pipe_done
,
514 * Make sure we mark the fsp as having outstanding async
515 * activity so we don't crash on shutdown close.
518 ok
= aio_add_req_to_fsp(fsp
, req
);
520 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
521 return tevent_req_post(req
, ev
);
527 if (!CHECK_READ_SMB2(fsp
)) {
528 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
529 return tevent_req_post(req
, ev
);
532 status
= schedule_smb2_aio_read(fsp
->conn
,
540 if (NT_STATUS_IS_OK(status
)) {
542 * Doing an async read, allow this
543 * request to be canceled
545 tevent_req_set_cancel_fn(req
, smbd_smb2_read_cancel
);
549 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
550 /* Real error in setting up aio. Fail. */
551 tevent_req_nterror(req
, status
);
552 return tevent_req_post(req
, ev
);
555 /* Fallback to synchronous. */
557 init_strict_lock_struct(fsp
,
558 fsp
->op
->global
->open_persistent_id
,
562 lp_posix_cifsu_locktype(fsp
),
565 if (!SMB_VFS_STRICT_LOCK_CHECK(conn
, fsp
, &lock
)) {
566 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
567 return tevent_req_post(req
, ev
);
570 /* Try sendfile in preference. */
571 status
= schedule_smb2_sendfile_read(smb2req
, state
);
572 if (NT_STATUS_IS_OK(status
)) {
573 tevent_req_done(req
);
574 return tevent_req_post(req
, ev
);
576 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
577 tevent_req_nterror(req
, status
);
578 return tevent_req_post(req
, ev
);
581 /* Ok, read into memory. Allocate the out buffer. */
582 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
583 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
584 return tevent_req_post(req
, ev
);
587 nread
= read_file(fsp
,
588 (char *)state
->out_data
.data
,
594 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
595 "len=%llu returned %lld\n",
598 (unsigned long long)in_offset
,
599 (unsigned long long)in_length
,
602 status
= smb2_read_complete(req
, nread
, saved_errno
);
603 if (!NT_STATUS_IS_OK(status
)) {
604 tevent_req_nterror(req
, status
);
607 tevent_req_done(req
);
609 return tevent_req_post(req
, ev
);
612 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
)
614 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
616 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
617 struct smbd_smb2_read_state
);
620 bool is_data_outstanding
;
622 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
624 if (!NT_STATUS_IS_OK(status
)) {
625 NTSTATUS old
= status
;
626 status
= nt_status_np_pipe(old
);
627 tevent_req_nterror(req
, status
);
631 if (nread
== 0 && state
->out_data
.length
!= 0) {
632 tevent_req_nterror(req
, NT_STATUS_END_OF_FILE
);
636 state
->out_data
.length
= nread
;
637 state
->out_remaining
= 0;
640 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
641 * handle it in SMB1 pipe_read_andx_done().
644 tevent_req_done(req
);
647 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
650 uint32_t *out_remaining
)
653 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
654 struct smbd_smb2_read_state
);
656 if (tevent_req_is_nterror(req
, &status
)) {
657 tevent_req_received(req
);
661 *out_data
= state
->out_data
;
662 talloc_steal(mem_ctx
, out_data
->data
);
663 *out_remaining
= state
->out_remaining
;
665 if (state
->out_headers
.length
> 0) {
666 talloc_steal(mem_ctx
, state
);
667 talloc_set_destructor(state
, smb2_smb2_read_state_deny_destructor
);
668 tevent_req_received(req
);
669 state
->smb2req
->queue_entry
.sendfile_header
= &state
->out_headers
;
670 state
->smb2req
->queue_entry
.sendfile_body_size
= state
->in_length
;
671 talloc_set_destructor(state
, smb2_sendfile_send_data
);
673 tevent_req_received(req
);