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
);
121 uint8_t out_data_offset
;
122 DATA_BLOB out_data_buffer
= data_blob_null
;
123 uint32_t out_data_remaining
= 0;
125 NTSTATUS error
; /* transport error */
127 status
= smbd_smb2_read_recv(subreq
,
130 &out_data_remaining
);
132 if (!NT_STATUS_IS_OK(status
)) {
133 error
= smbd_smb2_request_error(req
, status
);
134 if (!NT_STATUS_IS_OK(error
)) {
135 smbd_server_connection_terminate(req
->xconn
,
142 out_data_offset
= SMB2_HDR_BODY
+ 0x10;
144 outbody
= smbd_smb2_generate_outbody(req
, 0x10);
145 if (outbody
.data
== NULL
) {
146 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
147 if (!NT_STATUS_IS_OK(error
)) {
148 smbd_server_connection_terminate(req
->xconn
,
155 SSVAL(outbody
.data
, 0x00, 0x10 + 1); /* struct size */
156 SCVAL(outbody
.data
, 0x02,
157 out_data_offset
); /* data offset */
158 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
159 SIVAL(outbody
.data
, 0x04,
160 out_data_buffer
.length
); /* data length */
161 SIVAL(outbody
.data
, 0x08,
162 out_data_remaining
); /* data remaining */
163 SIVAL(outbody
.data
, 0x0C, 0); /* reserved */
165 outdyn
= out_data_buffer
;
167 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
168 if (!NT_STATUS_IS_OK(error
)) {
169 smbd_server_connection_terminate(req
->xconn
,
175 struct smbd_smb2_read_state
{
176 struct smbd_smb2_request
*smb2req
;
177 struct smb_request
*smbreq
;
183 DATA_BLOB out_headers
;
184 uint8_t _out_hdr_buf
[NBT_HDR_SIZE
+ SMB2_HDR_BODY
+ 0x10];
186 uint32_t out_remaining
;
189 static int smb2_smb2_read_state_deny_destructor(struct smbd_smb2_read_state
*state
)
194 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
195 static int smb2_sendfile_send_data(struct smbd_smb2_read_state
*state
)
197 struct lock_struct lock
;
198 uint32_t in_length
= state
->in_length
;
199 uint64_t in_offset
= state
->in_offset
;
200 files_struct
*fsp
= state
->fsp
;
201 const DATA_BLOB
*hdr
= state
->smb2req
->queue_entry
.sendfile_header
;
202 NTSTATUS
*pstatus
= state
->smb2req
->queue_entry
.sendfile_status
;
203 struct smbXsrv_connection
*xconn
= state
->smb2req
->xconn
;
208 nread
= SMB_VFS_SENDFILE(xconn
->transport
.sock
,
213 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
221 * Returning ENOSYS means no data at all was sent.
222 Do this as a normal read. */
223 if (errno
== ENOSYS
) {
227 if (errno
== ENOTSUP
) {
228 set_use_sendfile(SNUM(fsp
->conn
), false);
229 DBG_WARNING("Disabling sendfile use as sendfile is "
230 "not supported by the system\n");
234 if (errno
== EINTR
) {
236 * Special hack for broken Linux with no working sendfile. If we
237 * return EINTR we sent the header but not the rest of the data.
238 * Fake this up by doing read/write calls.
240 set_use_sendfile(SNUM(fsp
->conn
), false);
241 nread
= fake_sendfile(xconn
, fsp
, in_offset
, in_length
);
244 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
245 "failed for file %s (%s) for client %s. "
247 fsp_str_dbg(fsp
), strerror(saved_errno
),
248 smbXsrv_connection_dbg(xconn
)));
249 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
255 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
256 "%s (%s) for client %s. Terminating\n",
257 fsp_str_dbg(fsp
), strerror(saved_errno
),
258 smbXsrv_connection_dbg(xconn
)));
259 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
261 } else if (nread
== 0) {
263 * Some sendfile implementations return 0 to indicate
264 * that there was a short read, but nothing was
265 * actually written to the socket. In this case,
266 * fallback to the normal read path so the header gets
267 * the correct byte count.
269 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
270 "falling back to the normal read: %s\n",
276 * We got a short read
281 /* Send out the header. */
282 ret
= write_data(xconn
->transport
.sock
,
283 (const char *)hdr
->data
, hdr
->length
);
284 if (ret
!= hdr
->length
) {
286 DEBUG(0,("smb2_sendfile_send_data: write_data failed for file "
287 "%s (%s) for client %s. Terminating\n",
288 fsp_str_dbg(fsp
), strerror(saved_errno
),
289 smbXsrv_connection_dbg(xconn
)));
290 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
293 nread
= fake_sendfile(xconn
, fsp
, in_offset
, in_length
);
296 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
297 "failed for file %s (%s) for client %s. "
299 fsp_str_dbg(fsp
), strerror(saved_errno
),
300 smbXsrv_connection_dbg(xconn
)));
301 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
307 if (nread
< in_length
) {
308 ret
= sendfile_short_send(xconn
, fsp
, nread
,
309 hdr
->length
, in_length
);
312 DEBUG(0,("%s: sendfile_short_send "
313 "failed for file %s (%s) for client %s. "
316 fsp_str_dbg(fsp
), strerror(saved_errno
),
317 smbXsrv_connection_dbg(xconn
)));
318 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
323 init_strict_lock_struct(fsp
,
324 fsp
->op
->global
->open_persistent_id
,
330 *pstatus
= NT_STATUS_OK
;
334 static NTSTATUS
schedule_smb2_sendfile_read(struct smbd_smb2_request
*smb2req
,
335 struct smbd_smb2_read_state
*state
)
337 files_struct
*fsp
= state
->fsp
;
340 * We cannot use sendfile if...
341 * We were not configured to do so OR
342 * Signing is active OR
343 * This is a compound SMB2 operation OR
344 * fsp is a STREAM file OR
345 * We're using a write cache OR
346 * It's not a regular file OR
347 * Requested offset is greater than file size OR
348 * there's not enough data in the file.
349 * Phew :-). Luckily this means most
350 * reads on most normal files. JRA.
353 if (!lp__use_sendfile(SNUM(fsp
->conn
)) ||
354 smb2req
->do_signing
||
355 smb2req
->do_encryption
||
356 smbd_smb2_is_compound(smb2req
) ||
357 (fsp
->base_fsp
!= NULL
) ||
358 (fsp
->wcp
!= NULL
) ||
359 (!S_ISREG(fsp
->fsp_name
->st
.st_ex_mode
)) ||
360 (state
->in_offset
>= fsp
->fsp_name
->st
.st_ex_size
) ||
361 (fsp
->fsp_name
->st
.st_ex_size
< state
->in_offset
+ state
->in_length
))
363 return NT_STATUS_RETRY
;
366 /* We've already checked there's this amount of data
368 state
->out_data
.length
= state
->in_length
;
369 state
->out_remaining
= 0;
371 state
->out_headers
= data_blob_const(state
->_out_hdr_buf
,
372 sizeof(state
->_out_hdr_buf
));
376 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
);
378 /*******************************************************************
379 Common read complete processing function for both synchronous and
381 *******************************************************************/
383 NTSTATUS
smb2_read_complete(struct tevent_req
*req
, ssize_t nread
, int err
)
385 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
386 struct smbd_smb2_read_state
);
387 files_struct
*fsp
= state
->fsp
;
390 NTSTATUS status
= map_nt_error_from_unix(err
);
392 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
393 "Error = %s (NTSTATUS %s)\n",
401 if (nread
== 0 && state
->in_length
!= 0) {
402 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
404 return NT_STATUS_END_OF_FILE
;
407 if (nread
< state
->in_minimum
) {
408 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
409 "minimum requested %u. Returning end of file\n",
412 (unsigned int)state
->in_minimum
));
413 return NT_STATUS_END_OF_FILE
;
416 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
419 (unsigned long)state
->in_length
,
420 (unsigned long)state
->in_offset
,
421 (unsigned long)nread
));
423 state
->out_data
.length
= nread
;
424 state
->out_remaining
= 0;
429 static bool smbd_smb2_read_cancel(struct tevent_req
*req
)
431 struct smbd_smb2_read_state
*state
=
433 struct smbd_smb2_read_state
);
435 return cancel_smb2_aio(state
->smbreq
);
438 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
439 struct tevent_context
*ev
,
440 struct smbd_smb2_request
*smb2req
,
441 struct files_struct
*fsp
,
446 uint32_t in_remaining
)
449 struct tevent_req
*req
= NULL
;
450 struct smbd_smb2_read_state
*state
= NULL
;
451 struct smb_request
*smbreq
= NULL
;
452 connection_struct
*conn
= smb2req
->tcon
->compat
;
454 struct lock_struct lock
;
457 req
= tevent_req_create(mem_ctx
, &state
,
458 struct smbd_smb2_read_state
);
462 state
->smb2req
= smb2req
;
463 state
->in_flags
= in_flags
;
464 state
->in_length
= in_length
;
465 state
->in_offset
= in_offset
;
466 state
->in_minimum
= in_minimum
;
467 state
->out_data
= data_blob_null
;
468 state
->out_remaining
= 0;
470 DEBUG(10,("smbd_smb2_read: %s - %s\n",
471 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
473 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
474 if (tevent_req_nomem(smbreq
, req
)) {
475 return tevent_req_post(req
, ev
);
477 state
->smbreq
= smbreq
;
479 if (fsp
->is_directory
) {
480 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
481 return tevent_req_post(req
, ev
);
486 if (IS_IPC(smbreq
->conn
)) {
487 struct tevent_req
*subreq
= NULL
;
489 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
490 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
491 return tevent_req_post(req
, ev
);
494 if (!fsp_is_np(fsp
)) {
495 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
496 return tevent_req_post(req
, ev
);
499 subreq
= np_read_send(state
, ev
,
500 fsp
->fake_file_handle
,
501 state
->out_data
.data
,
502 state
->out_data
.length
);
503 if (tevent_req_nomem(subreq
, req
)) {
504 return tevent_req_post(req
, ev
);
506 tevent_req_set_callback(subreq
,
507 smbd_smb2_read_pipe_done
,
512 if (!CHECK_READ_SMB2(fsp
)) {
513 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
514 return tevent_req_post(req
, ev
);
517 status
= schedule_smb2_aio_read(fsp
->conn
,
525 if (NT_STATUS_IS_OK(status
)) {
527 * Doing an async read, allow this
528 * request to be canceled
530 tevent_req_set_cancel_fn(req
, smbd_smb2_read_cancel
);
534 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
535 /* Real error in setting up aio. Fail. */
536 tevent_req_nterror(req
, status
);
537 return tevent_req_post(req
, ev
);
540 /* Fallback to synchronous. */
542 init_strict_lock_struct(fsp
,
543 fsp
->op
->global
->open_persistent_id
,
549 if (!SMB_VFS_STRICT_LOCK_CHECK(conn
, fsp
, &lock
)) {
550 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
551 return tevent_req_post(req
, ev
);
554 /* Try sendfile in preference. */
555 status
= schedule_smb2_sendfile_read(smb2req
, state
);
556 if (NT_STATUS_IS_OK(status
)) {
557 tevent_req_done(req
);
558 return tevent_req_post(req
, ev
);
560 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
561 tevent_req_nterror(req
, status
);
562 return tevent_req_post(req
, ev
);
566 /* Ok, read into memory. Allocate the out buffer. */
567 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
568 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
569 return tevent_req_post(req
, ev
);
572 nread
= read_file(fsp
,
573 (char *)state
->out_data
.data
,
579 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
580 "len=%llu returned %lld\n",
583 (unsigned long long)in_offset
,
584 (unsigned long long)in_length
,
587 status
= smb2_read_complete(req
, nread
, saved_errno
);
588 if (!NT_STATUS_IS_OK(status
)) {
589 tevent_req_nterror(req
, status
);
592 tevent_req_done(req
);
594 return tevent_req_post(req
, ev
);
597 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
)
599 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
601 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
602 struct smbd_smb2_read_state
);
605 bool is_data_outstanding
;
607 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
609 if (!NT_STATUS_IS_OK(status
)) {
610 NTSTATUS old
= status
;
611 status
= nt_status_np_pipe(old
);
612 tevent_req_nterror(req
, status
);
616 if (nread
== 0 && state
->out_data
.length
!= 0) {
617 tevent_req_nterror(req
, NT_STATUS_END_OF_FILE
);
621 state
->out_data
.length
= nread
;
622 state
->out_remaining
= 0;
625 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
626 * handle it in SMB1 pipe_read_andx_done().
629 tevent_req_done(req
);
632 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
635 uint32_t *out_remaining
)
638 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
639 struct smbd_smb2_read_state
);
641 if (tevent_req_is_nterror(req
, &status
)) {
642 tevent_req_received(req
);
646 *out_data
= state
->out_data
;
647 talloc_steal(mem_ctx
, out_data
->data
);
648 *out_remaining
= state
->out_remaining
;
650 if (state
->out_headers
.length
> 0) {
651 talloc_steal(mem_ctx
, state
);
652 talloc_set_destructor(state
, smb2_smb2_read_state_deny_destructor
);
653 tevent_req_received(req
);
654 state
->smb2req
->queue_entry
.sendfile_header
= &state
->out_headers
;
655 talloc_set_destructor(state
, smb2_sendfile_send_data
);
657 tevent_req_received(req
);