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/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
,
38 uint32_t in_remaining
);
39 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
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 struct smbXsrv_connection
*xconn
= req
->xconn
;
49 const uint8_t *inbody
;
52 uint64_t in_file_id_persistent
;
53 uint64_t in_file_id_volatile
;
54 struct files_struct
*in_fsp
;
55 uint32_t in_minimum_count
;
56 uint32_t in_remaining_bytes
;
57 struct tevent_req
*subreq
;
59 status
= smbd_smb2_request_verify_sizes(req
, 0x31);
60 if (!NT_STATUS_IS_OK(status
)) {
61 return smbd_smb2_request_error(req
, status
);
63 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
65 in_length
= IVAL(inbody
, 0x04);
66 in_offset
= BVAL(inbody
, 0x08);
67 in_file_id_persistent
= BVAL(inbody
, 0x10);
68 in_file_id_volatile
= BVAL(inbody
, 0x18);
69 in_minimum_count
= IVAL(inbody
, 0x20);
70 in_remaining_bytes
= IVAL(inbody
, 0x28);
72 /* check the max read size */
73 if (in_length
> xconn
->smb2
.server
.max_read
) {
74 DEBUG(2,("smbd_smb2_request_process_read: "
75 "client ignored max read: %s: 0x%08X: 0x%08X\n",
76 __location__
, in_length
, xconn
->smb2
.server
.max_read
));
77 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
80 status
= smbd_smb2_request_verify_creditcharge(req
, in_length
);
81 if (!NT_STATUS_IS_OK(status
)) {
82 return smbd_smb2_request_error(req
, status
);
85 in_fsp
= file_fsp_smb2(req
, in_file_id_persistent
, in_file_id_volatile
);
87 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
90 subreq
= smbd_smb2_read_send(req
, req
->sconn
->ev_ctx
,
97 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
99 tevent_req_set_callback(subreq
, smbd_smb2_request_read_done
, req
);
101 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
104 static void smbd_smb2_request_read_done(struct tevent_req
*subreq
)
106 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
107 struct smbd_smb2_request
);
110 uint8_t out_data_offset
;
111 DATA_BLOB out_data_buffer
= data_blob_null
;
112 uint32_t out_data_remaining
= 0;
114 NTSTATUS error
; /* transport error */
116 status
= smbd_smb2_read_recv(subreq
,
119 &out_data_remaining
);
121 if (!NT_STATUS_IS_OK(status
)) {
122 error
= smbd_smb2_request_error(req
, status
);
123 if (!NT_STATUS_IS_OK(error
)) {
124 smbd_server_connection_terminate(req
->xconn
,
131 out_data_offset
= SMB2_HDR_BODY
+ 0x10;
133 outbody
= smbd_smb2_generate_outbody(req
, 0x10);
134 if (outbody
.data
== NULL
) {
135 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
136 if (!NT_STATUS_IS_OK(error
)) {
137 smbd_server_connection_terminate(req
->xconn
,
144 SSVAL(outbody
.data
, 0x00, 0x10 + 1); /* struct size */
145 SCVAL(outbody
.data
, 0x02,
146 out_data_offset
); /* data offset */
147 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
148 SIVAL(outbody
.data
, 0x04,
149 out_data_buffer
.length
); /* data length */
150 SIVAL(outbody
.data
, 0x08,
151 out_data_remaining
); /* data remaining */
152 SIVAL(outbody
.data
, 0x0C, 0); /* reserved */
154 outdyn
= out_data_buffer
;
156 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
157 if (!NT_STATUS_IS_OK(error
)) {
158 smbd_server_connection_terminate(req
->xconn
,
164 struct smbd_smb2_read_state
{
165 struct smbd_smb2_request
*smb2req
;
166 struct smb_request
*smbreq
;
171 DATA_BLOB out_headers
;
172 uint8_t _out_hdr_buf
[NBT_HDR_SIZE
+ SMB2_HDR_BODY
+ 0x10];
174 uint32_t out_remaining
;
177 static int smb2_smb2_read_state_deny_destructor(struct smbd_smb2_read_state
*state
)
182 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
183 static int smb2_sendfile_send_data(struct smbd_smb2_read_state
*state
)
185 struct lock_struct lock
;
186 uint32_t in_length
= state
->in_length
;
187 uint64_t in_offset
= state
->in_offset
;
188 files_struct
*fsp
= state
->fsp
;
189 const DATA_BLOB
*hdr
= state
->smb2req
->queue_entry
.sendfile_header
;
190 NTSTATUS
*pstatus
= state
->smb2req
->queue_entry
.sendfile_status
;
191 struct smbXsrv_connection
*xconn
= state
->smb2req
->xconn
;
196 nread
= SMB_VFS_SENDFILE(xconn
->transport
.sock
,
201 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
209 * Returning ENOSYS means no data at all was sent.
210 Do this as a normal read. */
211 if (errno
== ENOSYS
) {
215 if (errno
== EINTR
) {
217 * Special hack for broken Linux with no working sendfile. If we
218 * return EINTR we sent the header but not the rest of the data.
219 * Fake this up by doing read/write calls.
221 set_use_sendfile(SNUM(fsp
->conn
), false);
222 nread
= fake_sendfile(xconn
, fsp
, in_offset
, in_length
);
225 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
226 "failed for file %s (%s) for client %s. "
228 fsp_str_dbg(fsp
), strerror(saved_errno
),
229 smbXsrv_connection_dbg(xconn
)));
230 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
236 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
237 "%s (%s) for client %s. Terminating\n",
238 fsp_str_dbg(fsp
), strerror(saved_errno
),
239 smbXsrv_connection_dbg(xconn
)));
240 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
242 } else if (nread
== 0) {
244 * Some sendfile implementations return 0 to indicate
245 * that there was a short read, but nothing was
246 * actually written to the socket. In this case,
247 * fallback to the normal read path so the header gets
248 * the correct byte count.
250 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
251 "falling back to the normal read: %s\n",
257 * We got a short read
262 /* Send out the header. */
263 ret
= write_data(xconn
->transport
.sock
,
264 (const char *)hdr
->data
, hdr
->length
);
265 if (ret
!= hdr
->length
) {
267 DEBUG(0,("smb2_sendfile_send_data: write_data failed for file "
268 "%s (%s) for client %s. Terminating\n",
269 fsp_str_dbg(fsp
), strerror(saved_errno
),
270 smbXsrv_connection_dbg(xconn
)));
271 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
274 nread
= fake_sendfile(xconn
, fsp
, in_offset
, in_length
);
277 DEBUG(0,("smb2_sendfile_send_data: fake_sendfile "
278 "failed for file %s (%s) for client %s. "
280 fsp_str_dbg(fsp
), strerror(saved_errno
),
281 smbXsrv_connection_dbg(xconn
)));
282 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
288 if (nread
< in_length
) {
289 ret
= sendfile_short_send(xconn
, fsp
, nread
,
290 hdr
->length
, in_length
);
293 DEBUG(0,("%s: sendfile_short_send "
294 "failed for file %s (%s) for client %s. "
297 fsp_str_dbg(fsp
), strerror(saved_errno
),
298 smbXsrv_connection_dbg(xconn
)));
299 *pstatus
= map_nt_error_from_unix_common(saved_errno
);
304 init_strict_lock_struct(fsp
,
305 fsp
->op
->global
->open_persistent_id
,
311 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &lock
);
313 *pstatus
= NT_STATUS_OK
;
317 static NTSTATUS
schedule_smb2_sendfile_read(struct smbd_smb2_request
*smb2req
,
318 struct smbd_smb2_read_state
*state
)
320 files_struct
*fsp
= state
->fsp
;
323 * We cannot use sendfile if...
324 * We were not configured to do so OR
325 * Signing is active OR
326 * This is a compound SMB2 operation OR
327 * fsp is a STREAM file OR
328 * We're using a write cache OR
329 * It's not a regular file OR
330 * Requested offset is greater than file size OR
331 * there's not enough data in the file.
332 * Phew :-). Luckily this means most
333 * reads on most normal files. JRA.
336 if (!lp__use_sendfile(SNUM(fsp
->conn
)) ||
337 smb2req
->do_signing
||
338 smb2req
->do_encryption
||
339 smb2req
->in
.vector_count
>= (2*SMBD_SMB2_NUM_IOV_PER_REQ
) ||
340 (fsp
->base_fsp
!= NULL
) ||
341 (fsp
->wcp
!= NULL
) ||
342 (!S_ISREG(fsp
->fsp_name
->st
.st_ex_mode
)) ||
343 (state
->in_offset
>= fsp
->fsp_name
->st
.st_ex_size
) ||
344 (fsp
->fsp_name
->st
.st_ex_size
< state
->in_offset
+ state
->in_length
))
346 return NT_STATUS_RETRY
;
349 /* We've already checked there's this amount of data
351 state
->out_data
.length
= state
->in_length
;
352 state
->out_remaining
= 0;
354 state
->out_headers
= data_blob_const(state
->_out_hdr_buf
,
355 sizeof(state
->_out_hdr_buf
));
359 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
);
361 /*******************************************************************
362 Common read complete processing function for both synchronous and
364 *******************************************************************/
366 NTSTATUS
smb2_read_complete(struct tevent_req
*req
, ssize_t nread
, int err
)
368 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
369 struct smbd_smb2_read_state
);
370 files_struct
*fsp
= state
->fsp
;
373 NTSTATUS status
= map_nt_error_from_unix(err
);
375 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
376 "Error = %s (NTSTATUS %s)\n",
384 if (nread
== 0 && state
->in_length
!= 0) {
385 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
387 return NT_STATUS_END_OF_FILE
;
390 if (nread
< state
->in_minimum
) {
391 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
392 "minimum requested %u. Returning end of file\n",
395 (unsigned int)state
->in_minimum
));
396 return NT_STATUS_END_OF_FILE
;
399 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
402 (unsigned long)state
->in_length
,
403 (unsigned long)state
->in_offset
,
404 (unsigned long)nread
));
406 state
->out_data
.length
= nread
;
407 state
->out_remaining
= 0;
412 static bool smbd_smb2_read_cancel(struct tevent_req
*req
)
414 struct smbd_smb2_read_state
*state
=
416 struct smbd_smb2_read_state
);
418 return cancel_smb2_aio(state
->smbreq
);
421 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
422 struct tevent_context
*ev
,
423 struct smbd_smb2_request
*smb2req
,
424 struct files_struct
*fsp
,
428 uint32_t in_remaining
)
431 struct tevent_req
*req
= NULL
;
432 struct smbd_smb2_read_state
*state
= NULL
;
433 struct smb_request
*smbreq
= NULL
;
434 connection_struct
*conn
= smb2req
->tcon
->compat
;
436 struct lock_struct lock
;
439 req
= tevent_req_create(mem_ctx
, &state
,
440 struct smbd_smb2_read_state
);
444 state
->smb2req
= smb2req
;
445 state
->in_length
= in_length
;
446 state
->in_offset
= in_offset
;
447 state
->in_minimum
= in_minimum
;
448 state
->out_data
= data_blob_null
;
449 state
->out_remaining
= 0;
451 DEBUG(10,("smbd_smb2_read: %s - %s\n",
452 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
454 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
455 if (tevent_req_nomem(smbreq
, req
)) {
456 return tevent_req_post(req
, ev
);
458 state
->smbreq
= smbreq
;
460 if (fsp
->is_directory
) {
461 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
462 return tevent_req_post(req
, ev
);
467 if (IS_IPC(smbreq
->conn
)) {
468 struct tevent_req
*subreq
= NULL
;
470 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
471 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
472 return tevent_req_post(req
, ev
);
475 if (!fsp_is_np(fsp
)) {
476 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
477 return tevent_req_post(req
, ev
);
480 subreq
= np_read_send(state
, ev
,
481 fsp
->fake_file_handle
,
482 state
->out_data
.data
,
483 state
->out_data
.length
);
484 if (tevent_req_nomem(subreq
, req
)) {
485 return tevent_req_post(req
, ev
);
487 tevent_req_set_callback(subreq
,
488 smbd_smb2_read_pipe_done
,
493 if (!CHECK_READ(fsp
, smbreq
)) {
494 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
495 return tevent_req_post(req
, ev
);
498 status
= schedule_smb2_aio_read(fsp
->conn
,
506 if (NT_STATUS_IS_OK(status
)) {
508 * Doing an async read, allow this
509 * request to be canceled
511 tevent_req_set_cancel_fn(req
, smbd_smb2_read_cancel
);
515 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
516 /* Real error in setting up aio. Fail. */
517 tevent_req_nterror(req
, status
);
518 return tevent_req_post(req
, ev
);
521 /* Fallback to synchronous. */
523 init_strict_lock_struct(fsp
,
524 fsp
->op
->global
->open_persistent_id
,
530 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
531 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
532 return tevent_req_post(req
, ev
);
535 /* Try sendfile in preference. */
536 status
= schedule_smb2_sendfile_read(smb2req
, state
);
537 if (NT_STATUS_IS_OK(status
)) {
538 tevent_req_done(req
);
539 return tevent_req_post(req
, ev
);
541 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
542 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
543 tevent_req_nterror(req
, status
);
544 return tevent_req_post(req
, ev
);
548 /* Ok, read into memory. Allocate the out buffer. */
549 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
550 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
551 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
552 return tevent_req_post(req
, ev
);
555 nread
= read_file(fsp
,
556 (char *)state
->out_data
.data
,
562 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
564 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
565 "len=%llu returned %lld\n",
568 (unsigned long long)in_offset
,
569 (unsigned long long)in_length
,
572 status
= smb2_read_complete(req
, nread
, saved_errno
);
573 if (!NT_STATUS_IS_OK(status
)) {
574 tevent_req_nterror(req
, status
);
577 tevent_req_done(req
);
579 return tevent_req_post(req
, ev
);
582 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
)
584 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
586 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
587 struct smbd_smb2_read_state
);
590 bool is_data_outstanding
;
592 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
594 if (!NT_STATUS_IS_OK(status
)) {
595 NTSTATUS old
= status
;
596 status
= nt_status_np_pipe(old
);
597 tevent_req_nterror(req
, status
);
601 if (nread
== 0 && state
->out_data
.length
!= 0) {
602 tevent_req_nterror(req
, NT_STATUS_END_OF_FILE
);
606 state
->out_data
.length
= nread
;
607 state
->out_remaining
= 0;
610 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
611 * handle it in SMB1 pipe_read_andx_done().
614 tevent_req_done(req
);
617 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
620 uint32_t *out_remaining
)
623 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
624 struct smbd_smb2_read_state
);
626 if (tevent_req_is_nterror(req
, &status
)) {
627 tevent_req_received(req
);
631 *out_data
= state
->out_data
;
632 talloc_steal(mem_ctx
, out_data
->data
);
633 *out_remaining
= state
->out_remaining
;
635 if (state
->out_headers
.length
> 0) {
636 talloc_steal(mem_ctx
, state
);
637 talloc_set_destructor(state
, smb2_smb2_read_state_deny_destructor
);
638 tevent_req_received(req
);
639 state
->smb2req
->queue_entry
.sendfile_header
= &state
->out_headers
;
640 talloc_set_destructor(state
, smb2_sendfile_send_data
);
642 tevent_req_received(req
);