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 "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
24 #include "libcli/security/security.h"
26 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
27 struct tevent_context
*ev
,
28 struct smbd_smb2_request
*smb2req
,
30 uint64_t in_file_id_volatile
,
34 uint32_t in_remaining
);
35 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
38 uint32_t *out_remaining
);
40 static void smbd_smb2_request_read_done(struct tevent_req
*subreq
);
41 NTSTATUS
smbd_smb2_request_process_read(struct smbd_smb2_request
*req
)
44 const uint8_t *inbody
;
45 int i
= req
->current_idx
;
46 size_t expected_body_size
= 0x31;
51 uint64_t in_file_id_persistent
;
52 uint64_t in_file_id_volatile
;
53 uint32_t in_minimum_count
;
54 uint32_t in_remaining_bytes
;
55 struct tevent_req
*subreq
;
57 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
58 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
59 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
62 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
64 body_size
= SVAL(inbody
, 0x00);
65 if (body_size
!= expected_body_size
) {
66 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
69 in_smbpid
= IVAL(inhdr
, SMB2_HDR_PID
);
71 in_length
= IVAL(inbody
, 0x04);
72 in_offset
= BVAL(inbody
, 0x08);
73 in_file_id_persistent
= BVAL(inbody
, 0x10);
74 in_file_id_volatile
= BVAL(inbody
, 0x18);
75 in_minimum_count
= IVAL(inbody
, 0x20);
76 in_remaining_bytes
= IVAL(inbody
, 0x28);
78 /* check the max read size */
79 if (in_length
> lp_smb2_max_read()) {
80 DEBUG(0,("here:%s: 0x%08X: 0x%08X\n",
81 __location__
, in_length
, lp_smb2_max_read()));
82 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
85 if (req
->compat_chain_fsp
) {
87 } else if (in_file_id_persistent
!= in_file_id_volatile
) {
88 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
91 subreq
= smbd_smb2_read_send(req
,
92 req
->sconn
->smb2
.event_ctx
,
100 if (subreq
== NULL
) {
101 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
103 tevent_req_set_callback(subreq
, smbd_smb2_request_read_done
, req
);
105 return smbd_smb2_request_pending_queue(req
, subreq
);
108 static void smbd_smb2_request_read_done(struct tevent_req
*subreq
)
110 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
111 struct smbd_smb2_request
);
112 int i
= req
->current_idx
;
116 uint8_t out_data_offset
;
117 DATA_BLOB out_data_buffer
= data_blob_null
;
118 uint32_t out_data_remaining
= 0;
120 NTSTATUS error
; /* transport error */
122 status
= smbd_smb2_read_recv(subreq
,
125 &out_data_remaining
);
127 if (!NT_STATUS_IS_OK(status
)) {
128 error
= smbd_smb2_request_error(req
, status
);
129 if (!NT_STATUS_IS_OK(error
)) {
130 smbd_server_connection_terminate(req
->sconn
,
137 out_data_offset
= SMB2_HDR_BODY
+ 0x10;
139 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
141 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 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
->sconn
,
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
->sconn
,
172 struct smbd_smb2_read_state
{
173 struct smbd_smb2_request
*smb2req
;
175 uint64_t in_file_id_volatile
;
180 uint32_t out_remaining
;
183 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
184 static int smb2_sendfile_send_data(struct smbd_smb2_read_state
*state
)
186 struct lock_struct lock
;
187 uint32_t in_length
= state
->in_length
;
188 uint64_t in_offset
= state
->in_offset
;
189 files_struct
*fsp
= state
->fsp
;
192 nread
= SMB_VFS_SENDFILE(fsp
->conn
->sconn
->sock
,
197 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
202 if (errno
== ENOSYS
|| errno
== EINTR
) {
204 * Special hack for broken systems with no working
205 * sendfile. Fake this up by doing read/write calls.
207 set_use_sendfile(SNUM(fsp
->conn
), false);
208 nread
= fake_sendfile(fsp
, in_offset
, in_length
);
210 DEBUG(0,("smb2_sendfile_send_data: "
211 "fake_sendfile failed for "
215 exit_server_cleanly("smb2_sendfile_send_data: "
216 "fake_sendfile failed");
221 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
222 "%s (%s). Terminating\n",
225 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
226 } else if (nread
== 0) {
228 * Some sendfile implementations return 0 to indicate
229 * that there was a short read, but nothing was
230 * actually written to the socket. In this case,
231 * fallback to the normal read path so the header gets
232 * the correct byte count.
234 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
235 "falling back to the normal read: %s\n",
238 nread
= fake_sendfile(fsp
, in_offset
, in_length
);
240 DEBUG(0,("smb2_sendfile_send_data: "
241 "fake_sendfile failed for file "
242 "%s (%s). Terminating\n",
245 exit_server_cleanly("smb2_sendfile_send_data: "
246 "fake_sendfile failed");
252 if (nread
< in_length
) {
253 sendfile_short_send(fsp
, nread
, 0, in_length
);
256 init_strict_lock_struct(fsp
,
257 state
->in_file_id_volatile
,
263 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &lock
);
267 static NTSTATUS
schedule_smb2_sendfile_read(struct smbd_smb2_request
*smb2req
,
268 struct smbd_smb2_read_state
*state
)
270 struct smbd_smb2_read_state
*state_copy
= NULL
;
271 files_struct
*fsp
= state
->fsp
;
274 * We cannot use sendfile if...
275 * We were not configured to do so OR
276 * Signing is active OR
277 * This is a compound SMB2 operation OR
278 * fsp is a STREAM file OR
279 * We're using a write cache OR
280 * It's not a regular file OR
281 * Requested offset is greater than file size OR
282 * there's not enough data in the file.
283 * Phew :-). Luckily this means most
284 * reads on most normal files. JRA.
287 if (!_lp_use_sendfile(SNUM(fsp
->conn
)) ||
288 smb2req
->do_signing
||
289 smb2req
->in
.vector_count
!= 4 ||
290 (fsp
->base_fsp
!= NULL
) ||
291 (fsp
->wcp
!= NULL
) ||
292 (!S_ISREG(fsp
->fsp_name
->st
.st_ex_mode
)) ||
293 (state
->in_offset
>= fsp
->fsp_name
->st
.st_ex_size
) ||
294 (fsp
->fsp_name
->st
.st_ex_size
< state
->in_offset
+
296 return NT_STATUS_RETRY
;
299 /* We've already checked there's this amount of data
301 state
->out_data
.length
= state
->in_length
;
302 state
->out_remaining
= 0;
304 /* Make a copy of state attached to the smb2req. Attach
305 the destructor here as this will trigger the sendfile
306 call when the request is destroyed. */
307 state_copy
= TALLOC_P(smb2req
, struct smbd_smb2_read_state
);
309 return NT_STATUS_NO_MEMORY
;
311 *state_copy
= *state
;
312 talloc_set_destructor(state_copy
, smb2_sendfile_send_data
);
316 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
);
318 /*******************************************************************
319 Common read complete processing function for both synchronous and
321 *******************************************************************/
323 NTSTATUS
smb2_read_complete(struct tevent_req
*req
, ssize_t nread
, int err
)
325 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
326 struct smbd_smb2_read_state
);
327 files_struct
*fsp
= state
->fsp
;
330 NTSTATUS status
= map_nt_error_from_unix(err
);
332 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
333 "Error = %s (NTSTATUS %s)\n",
341 if (nread
== 0 && state
->in_length
!= 0) {
342 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
344 return NT_STATUS_END_OF_FILE
;
347 if (nread
< state
->in_minimum
) {
348 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
349 "minimum requested %u. Returning end of file\n",
352 (unsigned int)state
->in_minimum
));
353 return NT_STATUS_END_OF_FILE
;
356 DEBUG(3,("smbd_smb2_read: fnum=[%d/%s] length=%lu offset=%lu read=%lu\n",
359 (unsigned long)state
->in_length
,
360 (unsigned long)state
->in_offset
,
361 (unsigned long)nread
));
363 state
->out_data
.length
= nread
;
364 state
->out_remaining
= 0;
369 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
370 struct tevent_context
*ev
,
371 struct smbd_smb2_request
*smb2req
,
373 uint64_t in_file_id_volatile
,
377 uint32_t in_remaining
)
380 struct tevent_req
*req
= NULL
;
381 struct smbd_smb2_read_state
*state
= NULL
;
382 struct smb_request
*smbreq
= NULL
;
383 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
384 files_struct
*fsp
= NULL
;
386 struct lock_struct lock
;
389 req
= tevent_req_create(mem_ctx
, &state
,
390 struct smbd_smb2_read_state
);
394 state
->smb2req
= smb2req
;
395 state
->in_length
= in_length
;
396 state
->in_offset
= in_offset
;
397 state
->in_minimum
= in_minimum
;
398 state
->out_data
= data_blob_null
;
399 state
->out_remaining
= 0;
401 DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
402 (unsigned long long)in_file_id_volatile
));
404 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
405 if (tevent_req_nomem(smbreq
, req
)) {
406 return tevent_req_post(req
, ev
);
409 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
411 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
412 return tevent_req_post(req
, ev
);
414 if (conn
!= fsp
->conn
) {
415 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
416 return tevent_req_post(req
, ev
);
418 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
419 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
420 return tevent_req_post(req
, ev
);
422 if (fsp
->is_directory
) {
423 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
424 return tevent_req_post(req
, ev
);
428 state
->in_file_id_volatile
= in_file_id_volatile
;
430 if (IS_IPC(smbreq
->conn
)) {
431 struct tevent_req
*subreq
= NULL
;
433 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
434 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
435 return tevent_req_post(req
, ev
);
438 if (!fsp_is_np(fsp
)) {
439 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
440 return tevent_req_post(req
, ev
);
443 subreq
= np_read_send(state
, smbd_event_context(),
444 fsp
->fake_file_handle
,
445 state
->out_data
.data
,
446 state
->out_data
.length
);
447 if (tevent_req_nomem(subreq
, req
)) {
448 return tevent_req_post(req
, ev
);
450 tevent_req_set_callback(subreq
,
451 smbd_smb2_read_pipe_done
,
456 if (!CHECK_READ(fsp
, smbreq
)) {
457 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
458 return tevent_req_post(req
, ev
);
461 status
= schedule_smb2_aio_read(fsp
->conn
,
466 (SMB_OFF_T
)in_offset
,
469 if (NT_STATUS_IS_OK(status
)) {
471 * Doing an async read. Don't
472 * send a "gone async" message
473 * as we expect this to be less
474 * than the client timeout period.
475 * JRA. FIXME for offline files..
476 * FIXME. Add cancel code..
478 smb2req
->async
= true;
482 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
483 /* Real error in setting up aio. Fail. */
484 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
485 return tevent_req_post(req
, ev
);
488 /* Fallback to synchronous. */
490 init_strict_lock_struct(fsp
,
497 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
498 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
499 return tevent_req_post(req
, ev
);
502 /* Try sendfile in preference. */
503 status
= schedule_smb2_sendfile_read(smb2req
, state
);
504 if (NT_STATUS_IS_OK(status
)) {
505 tevent_req_done(req
);
506 return tevent_req_post(req
, ev
);
508 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
509 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
510 tevent_req_nterror(req
, status
);
511 return tevent_req_post(req
, ev
);
515 /* Ok, read into memory. Allocate the out buffer. */
516 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
517 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
518 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
519 return tevent_req_post(req
, ev
);
522 nread
= read_file(fsp
,
523 (char *)state
->out_data
.data
,
529 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
531 DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
532 "len=%llu returned %lld\n",
534 (unsigned long long)in_file_id_volatile
,
535 (unsigned long long)in_offset
,
536 (unsigned long long)in_length
,
539 status
= smb2_read_complete(req
, nread
, saved_errno
);
540 if (!NT_STATUS_IS_OK(status
)) {
541 tevent_req_nterror(req
, status
);
544 tevent_req_done(req
);
546 return tevent_req_post(req
, ev
);
549 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
)
551 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
553 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
554 struct smbd_smb2_read_state
);
557 bool is_data_outstanding
;
559 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
561 if (!NT_STATUS_IS_OK(status
)) {
562 tevent_req_nterror(req
, status
);
566 if (nread
== 0 && state
->out_data
.length
!= 0) {
567 tevent_req_nterror(req
, NT_STATUS_END_OF_FILE
);
571 state
->out_data
.length
= nread
;
572 state
->out_remaining
= 0;
574 tevent_req_done(req
);
577 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
580 uint32_t *out_remaining
)
583 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
584 struct smbd_smb2_read_state
);
586 if (tevent_req_is_nterror(req
, &status
)) {
587 tevent_req_received(req
);
591 *out_data
= state
->out_data
;
592 talloc_steal(mem_ctx
, out_data
->data
);
593 *out_remaining
= state
->out_remaining
;
595 tevent_req_received(req
);