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(2,("smbd_smb2_request_process_read: "
79 "client ignored max read: %s: 0x%08X: 0x%08X\n",
80 __location__
, in_length
, req
->sconn
->smb2
.max_read
));
81 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
84 status
= smbd_smb2_request_verify_creditcharge(req
, in_length
);
85 if (!NT_STATUS_IS_OK(status
)) {
86 return smbd_smb2_request_error(req
, status
);
89 if (req
->compat_chain_fsp
) {
91 } else if (in_file_id_persistent
!= in_file_id_volatile
) {
92 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
95 subreq
= smbd_smb2_read_send(req
,
104 if (subreq
== NULL
) {
105 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
107 tevent_req_set_callback(subreq
, smbd_smb2_request_read_done
, req
);
109 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
112 static void smbd_smb2_request_read_done(struct tevent_req
*subreq
)
114 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
115 struct smbd_smb2_request
);
118 uint8_t out_data_offset
;
119 DATA_BLOB out_data_buffer
= data_blob_null
;
120 uint32_t out_data_remaining
= 0;
122 NTSTATUS error
; /* transport error */
124 status
= smbd_smb2_read_recv(subreq
,
127 &out_data_remaining
);
129 if (!NT_STATUS_IS_OK(status
)) {
130 error
= smbd_smb2_request_error(req
, status
);
131 if (!NT_STATUS_IS_OK(error
)) {
132 smbd_server_connection_terminate(req
->sconn
,
139 out_data_offset
= SMB2_HDR_BODY
+ 0x10;
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
;
174 struct smb_request
*smbreq
;
176 uint64_t in_file_id_volatile
;
181 uint32_t out_remaining
;
184 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
185 static int smb2_sendfile_send_data(struct smbd_smb2_read_state
*state
)
187 struct lock_struct lock
;
188 uint32_t in_length
= state
->in_length
;
189 uint64_t in_offset
= state
->in_offset
;
190 files_struct
*fsp
= state
->fsp
;
193 nread
= SMB_VFS_SENDFILE(fsp
->conn
->sconn
->sock
,
198 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
203 if (errno
== ENOSYS
|| errno
== EINTR
) {
205 * Special hack for broken systems with no working
206 * sendfile. Fake this up by doing read/write calls.
208 set_use_sendfile(SNUM(fsp
->conn
), false);
209 nread
= fake_sendfile(fsp
, in_offset
, in_length
);
211 DEBUG(0,("smb2_sendfile_send_data: "
212 "fake_sendfile failed for "
216 exit_server_cleanly("smb2_sendfile_send_data: "
217 "fake_sendfile failed");
222 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
223 "%s (%s). Terminating\n",
226 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
227 } else if (nread
== 0) {
229 * Some sendfile implementations return 0 to indicate
230 * that there was a short read, but nothing was
231 * actually written to the socket. In this case,
232 * fallback to the normal read path so the header gets
233 * the correct byte count.
235 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
236 "falling back to the normal read: %s\n",
239 nread
= fake_sendfile(fsp
, in_offset
, in_length
);
241 DEBUG(0,("smb2_sendfile_send_data: "
242 "fake_sendfile failed for file "
243 "%s (%s). Terminating\n",
246 exit_server_cleanly("smb2_sendfile_send_data: "
247 "fake_sendfile failed");
253 if (nread
< in_length
) {
254 sendfile_short_send(fsp
, nread
, 0, in_length
);
257 init_strict_lock_struct(fsp
,
264 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &lock
);
268 static NTSTATUS
schedule_smb2_sendfile_read(struct smbd_smb2_request
*smb2req
,
269 struct smbd_smb2_read_state
*state
)
271 struct smbd_smb2_read_state
*state_copy
= NULL
;
272 files_struct
*fsp
= state
->fsp
;
275 * We cannot use sendfile if...
276 * We were not configured to do so OR
277 * Signing is active OR
278 * This is a compound SMB2 operation OR
279 * fsp is a STREAM file OR
280 * We're using a write cache OR
281 * It's not a regular file OR
282 * Requested offset is greater than file size OR
283 * there's not enough data in the file.
284 * Phew :-). Luckily this means most
285 * reads on most normal files. JRA.
288 if (!lp__use_sendfile(SNUM(fsp
->conn
)) ||
289 smb2req
->do_signing
||
290 smb2req
->in
.vector_count
!= 4 ||
291 (fsp
->base_fsp
!= NULL
) ||
292 (fsp
->wcp
!= NULL
) ||
293 (!S_ISREG(fsp
->fsp_name
->st
.st_ex_mode
)) ||
294 (state
->in_offset
>= fsp
->fsp_name
->st
.st_ex_size
) ||
295 (fsp
->fsp_name
->st
.st_ex_size
< state
->in_offset
+
297 return NT_STATUS_RETRY
;
300 /* We've already checked there's this amount of data
302 state
->out_data
.length
= state
->in_length
;
303 state
->out_remaining
= 0;
305 /* Make a copy of state attached to the smb2req. Attach
306 the destructor here as this will trigger the sendfile
307 call when the request is destroyed. */
308 state_copy
= talloc(smb2req
, struct smbd_smb2_read_state
);
310 return NT_STATUS_NO_MEMORY
;
312 *state_copy
= *state
;
313 talloc_set_destructor(state_copy
, smb2_sendfile_send_data
);
317 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
);
319 /*******************************************************************
320 Common read complete processing function for both synchronous and
322 *******************************************************************/
324 NTSTATUS
smb2_read_complete(struct tevent_req
*req
, ssize_t nread
, int err
)
326 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
327 struct smbd_smb2_read_state
);
328 files_struct
*fsp
= state
->fsp
;
331 NTSTATUS status
= map_nt_error_from_unix(err
);
333 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
334 "Error = %s (NTSTATUS %s)\n",
342 if (nread
== 0 && state
->in_length
!= 0) {
343 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
345 return NT_STATUS_END_OF_FILE
;
348 if (nread
< state
->in_minimum
) {
349 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
350 "minimum requested %u. Returning end of file\n",
353 (unsigned int)state
->in_minimum
));
354 return NT_STATUS_END_OF_FILE
;
357 DEBUG(3,("smbd_smb2_read: fnum=[%d/%s] length=%lu offset=%lu read=%lu\n",
360 (unsigned long)state
->in_length
,
361 (unsigned long)state
->in_offset
,
362 (unsigned long)nread
));
364 state
->out_data
.length
= nread
;
365 state
->out_remaining
= 0;
370 static bool smbd_smb2_read_cancel(struct tevent_req
*req
)
372 struct smbd_smb2_read_state
*state
=
374 struct smbd_smb2_read_state
);
376 state
->smb2req
->cancelled
= true;
378 return cancel_smb2_aio(state
->smbreq
);
381 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
382 struct tevent_context
*ev
,
383 struct smbd_smb2_request
*smb2req
,
385 uint64_t in_file_id_volatile
,
389 uint32_t in_remaining
)
392 struct tevent_req
*req
= NULL
;
393 struct smbd_smb2_read_state
*state
= NULL
;
394 struct smb_request
*smbreq
= NULL
;
395 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
396 files_struct
*fsp
= NULL
;
398 struct lock_struct lock
;
401 req
= tevent_req_create(mem_ctx
, &state
,
402 struct smbd_smb2_read_state
);
406 state
->smb2req
= smb2req
;
407 state
->in_length
= in_length
;
408 state
->in_offset
= in_offset
;
409 state
->in_minimum
= in_minimum
;
410 state
->out_data
= data_blob_null
;
411 state
->out_remaining
= 0;
413 DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
414 (unsigned long long)in_file_id_volatile
));
416 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
417 if (tevent_req_nomem(smbreq
, req
)) {
418 return tevent_req_post(req
, ev
);
420 state
->smbreq
= smbreq
;
422 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
424 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
425 return tevent_req_post(req
, ev
);
427 if (conn
!= fsp
->conn
) {
428 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
429 return tevent_req_post(req
, ev
);
431 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
432 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
433 return tevent_req_post(req
, ev
);
435 if (fsp
->is_directory
) {
436 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
437 return tevent_req_post(req
, ev
);
441 state
->in_file_id_volatile
= in_file_id_volatile
;
443 if (IS_IPC(smbreq
->conn
)) {
444 struct tevent_req
*subreq
= NULL
;
446 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
447 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
448 return tevent_req_post(req
, ev
);
451 if (!fsp_is_np(fsp
)) {
452 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
453 return tevent_req_post(req
, ev
);
456 subreq
= np_read_send(state
, ev
,
457 fsp
->fake_file_handle
,
458 state
->out_data
.data
,
459 state
->out_data
.length
);
460 if (tevent_req_nomem(subreq
, req
)) {
461 return tevent_req_post(req
, ev
);
463 tevent_req_set_callback(subreq
,
464 smbd_smb2_read_pipe_done
,
469 if (!CHECK_READ(fsp
, smbreq
)) {
470 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
471 return tevent_req_post(req
, ev
);
474 status
= schedule_smb2_aio_read(fsp
->conn
,
482 if (NT_STATUS_IS_OK(status
)) {
484 * Doing an async read, allow this
485 * request to be canceled
487 tevent_req_set_cancel_fn(req
, smbd_smb2_read_cancel
);
491 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
492 /* Real error in setting up aio. Fail. */
493 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
494 return tevent_req_post(req
, ev
);
497 /* Fallback to synchronous. */
499 init_strict_lock_struct(fsp
,
506 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
507 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
508 return tevent_req_post(req
, ev
);
511 /* Try sendfile in preference. */
512 status
= schedule_smb2_sendfile_read(smb2req
, state
);
513 if (NT_STATUS_IS_OK(status
)) {
514 tevent_req_done(req
);
515 return tevent_req_post(req
, ev
);
517 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
518 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
519 tevent_req_nterror(req
, status
);
520 return tevent_req_post(req
, ev
);
524 /* Ok, read into memory. Allocate the out buffer. */
525 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
526 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
527 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
528 return tevent_req_post(req
, ev
);
531 nread
= read_file(fsp
,
532 (char *)state
->out_data
.data
,
538 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
540 DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
541 "len=%llu returned %lld\n",
543 (unsigned long long)in_file_id_volatile
,
544 (unsigned long long)in_offset
,
545 (unsigned long long)in_length
,
548 status
= smb2_read_complete(req
, nread
, saved_errno
);
549 if (!NT_STATUS_IS_OK(status
)) {
550 tevent_req_nterror(req
, status
);
553 tevent_req_done(req
);
555 return tevent_req_post(req
, ev
);
558 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
)
560 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
562 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
563 struct smbd_smb2_read_state
);
566 bool is_data_outstanding
;
568 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
570 if (!NT_STATUS_IS_OK(status
)) {
571 NTSTATUS old
= status
;
572 status
= nt_status_np_pipe(old
);
573 tevent_req_nterror(req
, status
);
577 if (nread
== 0 && state
->out_data
.length
!= 0) {
578 tevent_req_nterror(req
, NT_STATUS_END_OF_FILE
);
582 state
->out_data
.length
= nread
;
583 state
->out_remaining
= 0;
586 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
587 * handle it in SMB1 pipe_read_andx_done().
590 tevent_req_done(req
);
593 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
596 uint32_t *out_remaining
)
599 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
600 struct smbd_smb2_read_state
);
602 if (tevent_req_is_nterror(req
, &status
)) {
603 tevent_req_received(req
);
607 *out_data
= state
->out_data
;
608 talloc_steal(mem_ctx
, out_data
->data
);
609 *out_remaining
= state
->out_remaining
;
611 tevent_req_received(req
);