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
,
34 uint64_t in_file_id_volatile
,
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
)
49 const uint8_t *inbody
;
50 int i
= req
->current_idx
;
54 uint64_t in_file_id_persistent
;
55 uint64_t in_file_id_volatile
;
56 uint32_t in_minimum_count
;
57 uint32_t in_remaining_bytes
;
58 struct tevent_req
*subreq
;
60 status
= smbd_smb2_request_verify_sizes(req
, 0x31);
61 if (!NT_STATUS_IS_OK(status
)) {
62 return smbd_smb2_request_error(req
, status
);
64 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
65 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
67 in_smbpid
= IVAL(inhdr
, SMB2_HDR_PID
);
69 in_length
= IVAL(inbody
, 0x04);
70 in_offset
= BVAL(inbody
, 0x08);
71 in_file_id_persistent
= BVAL(inbody
, 0x10);
72 in_file_id_volatile
= BVAL(inbody
, 0x18);
73 in_minimum_count
= IVAL(inbody
, 0x20);
74 in_remaining_bytes
= IVAL(inbody
, 0x28);
76 /* check the max read size */
77 if (in_length
> req
->sconn
->smb2
.max_read
) {
78 DEBUG(0,("here:%s: 0x%08X: 0x%08X\n",
79 __location__
, in_length
, req
->sconn
->smb2
.max_read
));
80 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
83 status
= smbd_smb2_request_verify_creditcharge(req
, in_length
);
84 if (!NT_STATUS_IS_OK(status
)) {
85 return smbd_smb2_request_error(req
, status
);
88 if (req
->compat_chain_fsp
) {
90 } else if (in_file_id_persistent
!= in_file_id_volatile
) {
91 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
94 subreq
= smbd_smb2_read_send(req
,
103 if (subreq
== NULL
) {
104 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
106 tevent_req_set_callback(subreq
, smbd_smb2_request_read_done
, req
);
108 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
111 static void smbd_smb2_request_read_done(struct tevent_req
*subreq
)
113 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
114 struct smbd_smb2_request
);
117 uint8_t out_data_offset
;
118 DATA_BLOB out_data_buffer
= data_blob_null
;
119 uint32_t out_data_remaining
= 0;
121 NTSTATUS error
; /* transport error */
123 status
= smbd_smb2_read_recv(subreq
,
126 &out_data_remaining
);
128 if (!NT_STATUS_IS_OK(status
)) {
129 error
= smbd_smb2_request_error(req
, status
);
130 if (!NT_STATUS_IS_OK(error
)) {
131 smbd_server_connection_terminate(req
->sconn
,
138 out_data_offset
= SMB2_HDR_BODY
+ 0x10;
140 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x10);
141 if (outbody
.data
== NULL
) {
142 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
143 if (!NT_STATUS_IS_OK(error
)) {
144 smbd_server_connection_terminate(req
->sconn
,
151 SSVAL(outbody
.data
, 0x00, 0x10 + 1); /* struct size */
152 SCVAL(outbody
.data
, 0x02,
153 out_data_offset
); /* data offset */
154 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
155 SIVAL(outbody
.data
, 0x04,
156 out_data_buffer
.length
); /* data length */
157 SIVAL(outbody
.data
, 0x08,
158 out_data_remaining
); /* data remaining */
159 SIVAL(outbody
.data
, 0x0C, 0); /* reserved */
161 outdyn
= out_data_buffer
;
163 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
164 if (!NT_STATUS_IS_OK(error
)) {
165 smbd_server_connection_terminate(req
->sconn
,
171 struct smbd_smb2_read_state
{
172 struct smbd_smb2_request
*smb2req
;
173 struct smb_request
*smbreq
;
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(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 bool smbd_smb2_read_cancel(struct tevent_req
*req
)
371 struct smbd_smb2_read_state
*state
=
373 struct smbd_smb2_read_state
);
375 state
->smb2req
->cancelled
= true;
377 return cancel_smb2_aio(state
->smbreq
);
380 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
381 struct tevent_context
*ev
,
382 struct smbd_smb2_request
*smb2req
,
384 uint64_t in_file_id_volatile
,
388 uint32_t in_remaining
)
391 struct tevent_req
*req
= NULL
;
392 struct smbd_smb2_read_state
*state
= NULL
;
393 struct smb_request
*smbreq
= NULL
;
394 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
395 files_struct
*fsp
= NULL
;
397 struct lock_struct lock
;
400 req
= tevent_req_create(mem_ctx
, &state
,
401 struct smbd_smb2_read_state
);
405 state
->smb2req
= smb2req
;
406 state
->in_length
= in_length
;
407 state
->in_offset
= in_offset
;
408 state
->in_minimum
= in_minimum
;
409 state
->out_data
= data_blob_null
;
410 state
->out_remaining
= 0;
412 DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
413 (unsigned long long)in_file_id_volatile
));
415 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
416 if (tevent_req_nomem(smbreq
, req
)) {
417 return tevent_req_post(req
, ev
);
419 state
->smbreq
= smbreq
;
421 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
423 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
424 return tevent_req_post(req
, ev
);
426 if (conn
!= fsp
->conn
) {
427 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
428 return tevent_req_post(req
, ev
);
430 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
431 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
432 return tevent_req_post(req
, ev
);
434 if (fsp
->is_directory
) {
435 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
436 return tevent_req_post(req
, ev
);
440 state
->in_file_id_volatile
= in_file_id_volatile
;
442 if (IS_IPC(smbreq
->conn
)) {
443 struct tevent_req
*subreq
= NULL
;
445 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
446 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
447 return tevent_req_post(req
, ev
);
450 if (!fsp_is_np(fsp
)) {
451 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
452 return tevent_req_post(req
, ev
);
455 subreq
= np_read_send(state
, ev
,
456 fsp
->fake_file_handle
,
457 state
->out_data
.data
,
458 state
->out_data
.length
);
459 if (tevent_req_nomem(subreq
, req
)) {
460 return tevent_req_post(req
, ev
);
462 tevent_req_set_callback(subreq
,
463 smbd_smb2_read_pipe_done
,
468 if (!CHECK_READ(fsp
, smbreq
)) {
469 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
470 return tevent_req_post(req
, ev
);
473 status
= schedule_smb2_aio_read(fsp
->conn
,
478 (SMB_OFF_T
)in_offset
,
481 if (NT_STATUS_IS_OK(status
)) {
483 * Doing an async read, allow this
484 * request to be canceled
486 tevent_req_set_cancel_fn(req
, smbd_smb2_read_cancel
);
490 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
491 /* Real error in setting up aio. Fail. */
492 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
493 return tevent_req_post(req
, ev
);
496 /* Fallback to synchronous. */
498 init_strict_lock_struct(fsp
,
505 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
506 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
507 return tevent_req_post(req
, ev
);
510 /* Try sendfile in preference. */
511 status
= schedule_smb2_sendfile_read(smb2req
, state
);
512 if (NT_STATUS_IS_OK(status
)) {
513 tevent_req_done(req
);
514 return tevent_req_post(req
, ev
);
516 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
517 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
518 tevent_req_nterror(req
, status
);
519 return tevent_req_post(req
, ev
);
523 /* Ok, read into memory. Allocate the out buffer. */
524 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
525 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
526 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
527 return tevent_req_post(req
, ev
);
530 nread
= read_file(fsp
,
531 (char *)state
->out_data
.data
,
537 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
539 DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
540 "len=%llu returned %lld\n",
542 (unsigned long long)in_file_id_volatile
,
543 (unsigned long long)in_offset
,
544 (unsigned long long)in_length
,
547 status
= smb2_read_complete(req
, nread
, saved_errno
);
548 if (!NT_STATUS_IS_OK(status
)) {
549 tevent_req_nterror(req
, status
);
552 tevent_req_done(req
);
554 return tevent_req_post(req
, ev
);
557 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
)
559 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
561 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
562 struct smbd_smb2_read_state
);
565 bool is_data_outstanding
;
567 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
569 if (!NT_STATUS_IS_OK(status
)) {
570 tevent_req_nterror(req
, status
);
574 if (nread
== 0 && state
->out_data
.length
!= 0) {
575 tevent_req_nterror(req
, NT_STATUS_END_OF_FILE
);
579 state
->out_data
.length
= nread
;
580 state
->out_remaining
= 0;
583 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
584 * handle it in SMB1 pipe_read_andx_done().
587 tevent_req_done(req
);
590 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
593 uint32_t *out_remaining
)
596 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
597 struct smbd_smb2_read_state
);
599 if (tevent_req_is_nterror(req
, &status
)) {
600 tevent_req_received(req
);
604 *out_data
= state
->out_data
;
605 talloc_steal(mem_ctx
, out_data
->data
);
606 *out_remaining
= state
->out_remaining
;
608 tevent_req_received(req
);