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"
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 struct files_struct
*in_fsp
,
37 uint32_t in_remaining
);
38 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
41 uint32_t *out_remaining
);
43 static void smbd_smb2_request_read_done(struct tevent_req
*subreq
);
44 NTSTATUS
smbd_smb2_request_process_read(struct smbd_smb2_request
*req
)
46 struct smbXsrv_connection
*xconn
= req
->xconn
;
48 const uint8_t *inbody
;
51 uint64_t in_file_id_persistent
;
52 uint64_t in_file_id_volatile
;
53 struct files_struct
*in_fsp
;
54 uint32_t in_minimum_count
;
55 uint32_t in_remaining_bytes
;
56 struct tevent_req
*subreq
;
58 status
= smbd_smb2_request_verify_sizes(req
, 0x31);
59 if (!NT_STATUS_IS_OK(status
)) {
60 return smbd_smb2_request_error(req
, status
);
62 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
64 in_length
= IVAL(inbody
, 0x04);
65 in_offset
= BVAL(inbody
, 0x08);
66 in_file_id_persistent
= BVAL(inbody
, 0x10);
67 in_file_id_volatile
= BVAL(inbody
, 0x18);
68 in_minimum_count
= IVAL(inbody
, 0x20);
69 in_remaining_bytes
= IVAL(inbody
, 0x28);
71 /* check the max read size */
72 if (in_length
> xconn
->smb2
.server
.max_read
) {
73 DEBUG(2,("smbd_smb2_request_process_read: "
74 "client ignored max read: %s: 0x%08X: 0x%08X\n",
75 __location__
, in_length
, xconn
->smb2
.server
.max_read
));
76 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
79 status
= smbd_smb2_request_verify_creditcharge(req
, in_length
);
80 if (!NT_STATUS_IS_OK(status
)) {
81 return smbd_smb2_request_error(req
, status
);
84 in_fsp
= file_fsp_smb2(req
, 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
, req
->sconn
->ev_ctx
,
96 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
98 tevent_req_set_callback(subreq
, smbd_smb2_request_read_done
, req
);
100 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
103 static void smbd_smb2_request_read_done(struct tevent_req
*subreq
)
105 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
106 struct smbd_smb2_request
);
109 uint8_t out_data_offset
;
110 DATA_BLOB out_data_buffer
= data_blob_null
;
111 uint32_t out_data_remaining
= 0;
113 NTSTATUS error
; /* transport error */
115 status
= smbd_smb2_read_recv(subreq
,
118 &out_data_remaining
);
120 if (!NT_STATUS_IS_OK(status
)) {
121 error
= smbd_smb2_request_error(req
, status
);
122 if (!NT_STATUS_IS_OK(error
)) {
123 smbd_server_connection_terminate(req
->xconn
,
130 out_data_offset
= SMB2_HDR_BODY
+ 0x10;
132 outbody
= smbd_smb2_generate_outbody(req
, 0x10);
133 if (outbody
.data
== NULL
) {
134 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
135 if (!NT_STATUS_IS_OK(error
)) {
136 smbd_server_connection_terminate(req
->xconn
,
143 SSVAL(outbody
.data
, 0x00, 0x10 + 1); /* struct size */
144 SCVAL(outbody
.data
, 0x02,
145 out_data_offset
); /* data offset */
146 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
147 SIVAL(outbody
.data
, 0x04,
148 out_data_buffer
.length
); /* data length */
149 SIVAL(outbody
.data
, 0x08,
150 out_data_remaining
); /* data remaining */
151 SIVAL(outbody
.data
, 0x0C, 0); /* reserved */
153 outdyn
= out_data_buffer
;
155 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
156 if (!NT_STATUS_IS_OK(error
)) {
157 smbd_server_connection_terminate(req
->xconn
,
163 struct smbd_smb2_read_state
{
164 struct smbd_smb2_request
*smb2req
;
165 struct smb_request
*smbreq
;
170 DATA_BLOB out_headers
;
171 uint8_t _out_hdr_buf
[NBT_HDR_SIZE
+ SMB2_HDR_BODY
+ 0x10];
173 uint32_t out_remaining
;
176 static int smb2_smb2_read_state_deny_destructor(struct smbd_smb2_read_state
*state
)
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 const DATA_BLOB
*hdr
= state
->smb2req
->queue_entry
.sendfile_header
;
189 NTSTATUS
*pstatus
= state
->smb2req
->queue_entry
.sendfile_status
;
190 struct smbXsrv_connection
*xconn
= state
->smb2req
->xconn
;
195 nread
= SMB_VFS_SENDFILE(xconn
->transport
.sock
,
200 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
208 * Returning ENOSYS means no data at all was sent.
209 Do this as a normal read. */
210 if (errno
== ENOSYS
) {
214 if (errno
== EINTR
) {
216 * Special hack for broken Linux with no working sendfile. If we
217 * return EINTR we sent the header but not the rest of the data.
218 * Fake this up by doing read/write calls.
220 set_use_sendfile(SNUM(fsp
->conn
), false);
221 nread
= fake_sendfile(xconn
, fsp
, in_offset
, in_length
);
224 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
225 "failed for file %s (%s) for client %s. "
227 fsp_str_dbg(fsp
), strerror(saved_errno
),
228 smbXsrv_connection_dbg(xconn
)));
229 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
235 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
236 "%s (%s) for client %s. Terminating\n",
237 fsp_str_dbg(fsp
), strerror(saved_errno
),
238 smbXsrv_connection_dbg(xconn
)));
239 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
241 } else if (nread
== 0) {
243 * Some sendfile implementations return 0 to indicate
244 * that there was a short read, but nothing was
245 * actually written to the socket. In this case,
246 * fallback to the normal read path so the header gets
247 * the correct byte count.
249 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
250 "falling back to the normal read: %s\n",
256 * We got a short read
261 /* Send out the header. */
262 ret
= write_data(xconn
->transport
.sock
,
263 (const char *)hdr
->data
, hdr
->length
);
264 if (ret
!= hdr
->length
) {
266 DEBUG(0,("smb2_sendfile_send_data: write_data failed for file "
267 "%s (%s) for client %s. Terminating\n",
268 fsp_str_dbg(fsp
), strerror(saved_errno
),
269 smbXsrv_connection_dbg(xconn
)));
270 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
273 nread
= fake_sendfile(xconn
, fsp
, in_offset
, in_length
);
276 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
277 "failed for file %s (%s) for client %s. "
279 fsp_str_dbg(fsp
), strerror(saved_errno
),
280 smbXsrv_connection_dbg(xconn
)));
281 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
287 if (nread
< in_length
) {
288 ret
= sendfile_short_send(xconn
, fsp
, nread
,
289 hdr
->length
, in_length
);
292 DEBUG(0,("%s: sendfile_short_send "
293 "failed for file %s (%s) for client %s. "
296 fsp_str_dbg(fsp
), strerror(saved_errno
),
297 smbXsrv_connection_dbg(xconn
)));
298 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
303 init_strict_lock_struct(fsp
,
304 fsp
->op
->global
->open_persistent_id
,
310 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &lock
);
312 *pstatus
= NT_STATUS_OK
;
316 static NTSTATUS
schedule_smb2_sendfile_read(struct smbd_smb2_request
*smb2req
,
317 struct smbd_smb2_read_state
*state
)
319 files_struct
*fsp
= state
->fsp
;
322 * We cannot use sendfile if...
323 * We were not configured to do so OR
324 * Signing is active OR
325 * This is a compound SMB2 operation OR
326 * fsp is a STREAM file OR
327 * We're using a write cache OR
328 * It's not a regular file OR
329 * Requested offset is greater than file size OR
330 * there's not enough data in the file.
331 * Phew :-). Luckily this means most
332 * reads on most normal files. JRA.
335 if (!lp__use_sendfile(SNUM(fsp
->conn
)) ||
336 smb2req
->do_signing
||
337 smb2req
->do_encryption
||
338 smb2req
->in
.vector_count
>= (2*SMBD_SMB2_NUM_IOV_PER_REQ
) ||
339 (fsp
->base_fsp
!= NULL
) ||
340 (fsp
->wcp
!= NULL
) ||
341 (!S_ISREG(fsp
->fsp_name
->st
.st_ex_mode
)) ||
342 (state
->in_offset
>= fsp
->fsp_name
->st
.st_ex_size
) ||
343 (fsp
->fsp_name
->st
.st_ex_size
< state
->in_offset
+ state
->in_length
))
345 return NT_STATUS_RETRY
;
348 /* We've already checked there's this amount of data
350 state
->out_data
.length
= state
->in_length
;
351 state
->out_remaining
= 0;
353 state
->out_headers
= data_blob_const(state
->_out_hdr_buf
,
354 sizeof(state
->_out_hdr_buf
));
358 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
);
360 /*******************************************************************
361 Common read complete processing function for both synchronous and
363 *******************************************************************/
365 NTSTATUS
smb2_read_complete(struct tevent_req
*req
, ssize_t nread
, int err
)
367 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
368 struct smbd_smb2_read_state
);
369 files_struct
*fsp
= state
->fsp
;
372 NTSTATUS status
= map_nt_error_from_unix(err
);
374 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
375 "Error = %s (NTSTATUS %s)\n",
383 if (nread
== 0 && state
->in_length
!= 0) {
384 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
386 return NT_STATUS_END_OF_FILE
;
389 if (nread
< state
->in_minimum
) {
390 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
391 "minimum requested %u. Returning end of file\n",
394 (unsigned int)state
->in_minimum
));
395 return NT_STATUS_END_OF_FILE
;
398 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
401 (unsigned long)state
->in_length
,
402 (unsigned long)state
->in_offset
,
403 (unsigned long)nread
));
405 state
->out_data
.length
= nread
;
406 state
->out_remaining
= 0;
411 static bool smbd_smb2_read_cancel(struct tevent_req
*req
)
413 struct smbd_smb2_read_state
*state
=
415 struct smbd_smb2_read_state
);
417 return cancel_smb2_aio(state
->smbreq
);
420 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
421 struct tevent_context
*ev
,
422 struct smbd_smb2_request
*smb2req
,
423 struct files_struct
*fsp
,
427 uint32_t in_remaining
)
430 struct tevent_req
*req
= NULL
;
431 struct smbd_smb2_read_state
*state
= NULL
;
432 struct smb_request
*smbreq
= NULL
;
433 connection_struct
*conn
= smb2req
->tcon
->compat
;
435 struct lock_struct lock
;
438 req
= tevent_req_create(mem_ctx
, &state
,
439 struct smbd_smb2_read_state
);
443 state
->smb2req
= smb2req
;
444 state
->in_length
= in_length
;
445 state
->in_offset
= in_offset
;
446 state
->in_minimum
= in_minimum
;
447 state
->out_data
= data_blob_null
;
448 state
->out_remaining
= 0;
450 DEBUG(10,("smbd_smb2_read: %s - %s\n",
451 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
453 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
454 if (tevent_req_nomem(smbreq
, req
)) {
455 return tevent_req_post(req
, ev
);
457 state
->smbreq
= smbreq
;
459 if (fsp
->is_directory
) {
460 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
461 return tevent_req_post(req
, ev
);
466 if (IS_IPC(smbreq
->conn
)) {
467 struct tevent_req
*subreq
= NULL
;
469 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
470 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
471 return tevent_req_post(req
, ev
);
474 if (!fsp_is_np(fsp
)) {
475 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
476 return tevent_req_post(req
, ev
);
479 subreq
= np_read_send(state
, ev
,
480 fsp
->fake_file_handle
,
481 state
->out_data
.data
,
482 state
->out_data
.length
);
483 if (tevent_req_nomem(subreq
, req
)) {
484 return tevent_req_post(req
, ev
);
486 tevent_req_set_callback(subreq
,
487 smbd_smb2_read_pipe_done
,
492 if (!CHECK_READ(fsp
, smbreq
)) {
493 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
494 return tevent_req_post(req
, ev
);
497 status
= schedule_smb2_aio_read(fsp
->conn
,
505 if (NT_STATUS_IS_OK(status
)) {
507 * Doing an async read, allow this
508 * request to be canceled
510 tevent_req_set_cancel_fn(req
, smbd_smb2_read_cancel
);
514 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
515 /* Real error in setting up aio. Fail. */
516 tevent_req_nterror(req
, status
);
517 return tevent_req_post(req
, ev
);
520 /* Fallback to synchronous. */
522 init_strict_lock_struct(fsp
,
523 fsp
->op
->global
->open_persistent_id
,
529 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
530 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
531 return tevent_req_post(req
, ev
);
534 /* Try sendfile in preference. */
535 status
= schedule_smb2_sendfile_read(smb2req
, state
);
536 if (NT_STATUS_IS_OK(status
)) {
537 tevent_req_done(req
);
538 return tevent_req_post(req
, ev
);
540 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
541 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
542 tevent_req_nterror(req
, status
);
543 return tevent_req_post(req
, ev
);
547 /* Ok, read into memory. Allocate the out buffer. */
548 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
549 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
550 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
551 return tevent_req_post(req
, ev
);
554 nread
= read_file(fsp
,
555 (char *)state
->out_data
.data
,
561 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
563 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
564 "len=%llu returned %lld\n",
567 (unsigned long long)in_offset
,
568 (unsigned long long)in_length
,
571 status
= smb2_read_complete(req
, nread
, saved_errno
);
572 if (!NT_STATUS_IS_OK(status
)) {
573 tevent_req_nterror(req
, status
);
576 tevent_req_done(req
);
578 return tevent_req_post(req
, ev
);
581 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
)
583 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
585 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
586 struct smbd_smb2_read_state
);
589 bool is_data_outstanding
;
591 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
593 if (!NT_STATUS_IS_OK(status
)) {
594 NTSTATUS old
= status
;
595 status
= nt_status_np_pipe(old
);
596 tevent_req_nterror(req
, status
);
600 if (nread
== 0 && state
->out_data
.length
!= 0) {
601 tevent_req_nterror(req
, NT_STATUS_END_OF_FILE
);
605 state
->out_data
.length
= nread
;
606 state
->out_remaining
= 0;
609 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
610 * handle it in SMB1 pipe_read_andx_done().
613 tevent_req_done(req
);
616 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
619 uint32_t *out_remaining
)
622 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
623 struct smbd_smb2_read_state
);
625 if (tevent_req_is_nterror(req
, &status
)) {
626 tevent_req_received(req
);
630 *out_data
= state
->out_data
;
631 talloc_steal(mem_ctx
, out_data
->data
);
632 *out_remaining
= state
->out_remaining
;
634 if (state
->out_headers
.length
> 0) {
635 talloc_steal(mem_ctx
, state
);
636 talloc_set_destructor(state
, smb2_smb2_read_state_deny_destructor
);
637 tevent_req_received(req
);
638 state
->smb2req
->queue_entry
.sendfile_header
= &state
->out_headers
;
639 talloc_set_destructor(state
, smb2_sendfile_send_data
);
641 tevent_req_received(req
);