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 if (req
->compat_chain_fsp
) {
85 } else if (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
,
99 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
101 tevent_req_set_callback(subreq
, smbd_smb2_request_read_done
, req
);
103 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
106 static void smbd_smb2_request_read_done(struct tevent_req
*subreq
)
108 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
109 struct smbd_smb2_request
);
110 int i
= req
->current_idx
;
114 uint8_t out_data_offset
;
115 DATA_BLOB out_data_buffer
= data_blob_null
;
116 uint32_t out_data_remaining
= 0;
118 NTSTATUS error
; /* transport error */
120 status
= smbd_smb2_read_recv(subreq
,
123 &out_data_remaining
);
125 if (!NT_STATUS_IS_OK(status
)) {
126 error
= smbd_smb2_request_error(req
, status
);
127 if (!NT_STATUS_IS_OK(error
)) {
128 smbd_server_connection_terminate(req
->sconn
,
135 out_data_offset
= SMB2_HDR_BODY
+ 0x10;
137 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
139 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x10);
140 if (outbody
.data
== NULL
) {
141 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
142 if (!NT_STATUS_IS_OK(error
)) {
143 smbd_server_connection_terminate(req
->sconn
,
150 SSVAL(outbody
.data
, 0x00, 0x10 + 1); /* struct size */
151 SCVAL(outbody
.data
, 0x02,
152 out_data_offset
); /* data offset */
153 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
154 SIVAL(outbody
.data
, 0x04,
155 out_data_buffer
.length
); /* data length */
156 SIVAL(outbody
.data
, 0x08,
157 out_data_remaining
); /* data remaining */
158 SIVAL(outbody
.data
, 0x0C, 0); /* reserved */
160 outdyn
= out_data_buffer
;
162 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
163 if (!NT_STATUS_IS_OK(error
)) {
164 smbd_server_connection_terminate(req
->sconn
,
170 struct smbd_smb2_read_state
{
171 struct smbd_smb2_request
*smb2req
;
172 struct smb_request
*smbreq
;
174 uint64_t in_file_id_volatile
;
179 uint32_t out_remaining
;
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
;
191 nread
= SMB_VFS_SENDFILE(fsp
->conn
->sconn
->sock
,
196 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
201 if (errno
== ENOSYS
|| errno
== EINTR
) {
203 * Special hack for broken systems with no working
204 * sendfile. Fake this up by doing read/write calls.
206 set_use_sendfile(SNUM(fsp
->conn
), false);
207 nread
= fake_sendfile(fsp
, in_offset
, in_length
);
209 DEBUG(0,("smb2_sendfile_send_data: "
210 "fake_sendfile failed for "
214 exit_server_cleanly("smb2_sendfile_send_data: "
215 "fake_sendfile failed");
220 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
221 "%s (%s). Terminating\n",
224 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
225 } else if (nread
== 0) {
227 * Some sendfile implementations return 0 to indicate
228 * that there was a short read, but nothing was
229 * actually written to the socket. In this case,
230 * fallback to the normal read path so the header gets
231 * the correct byte count.
233 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
234 "falling back to the normal read: %s\n",
237 nread
= fake_sendfile(fsp
, in_offset
, in_length
);
239 DEBUG(0,("smb2_sendfile_send_data: "
240 "fake_sendfile failed for file "
241 "%s (%s). Terminating\n",
244 exit_server_cleanly("smb2_sendfile_send_data: "
245 "fake_sendfile failed");
251 if (nread
< in_length
) {
252 sendfile_short_send(fsp
, nread
, 0, in_length
);
255 init_strict_lock_struct(fsp
,
256 state
->in_file_id_volatile
,
262 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &lock
);
266 static NTSTATUS
schedule_smb2_sendfile_read(struct smbd_smb2_request
*smb2req
,
267 struct smbd_smb2_read_state
*state
)
269 struct smbd_smb2_read_state
*state_copy
= NULL
;
270 files_struct
*fsp
= state
->fsp
;
273 * We cannot use sendfile if...
274 * We were not configured to do so OR
275 * Signing is active OR
276 * This is a compound SMB2 operation OR
277 * fsp is a STREAM file OR
278 * We're using a write cache OR
279 * It's not a regular file OR
280 * Requested offset is greater than file size OR
281 * there's not enough data in the file.
282 * Phew :-). Luckily this means most
283 * reads on most normal files. JRA.
286 if (!lp__use_sendfile(SNUM(fsp
->conn
)) ||
287 smb2req
->do_signing
||
288 smb2req
->in
.vector_count
!= 4 ||
289 (fsp
->base_fsp
!= NULL
) ||
290 (fsp
->wcp
!= NULL
) ||
291 (!S_ISREG(fsp
->fsp_name
->st
.st_ex_mode
)) ||
292 (state
->in_offset
>= fsp
->fsp_name
->st
.st_ex_size
) ||
293 (fsp
->fsp_name
->st
.st_ex_size
< state
->in_offset
+
295 return NT_STATUS_RETRY
;
298 /* We've already checked there's this amount of data
300 state
->out_data
.length
= state
->in_length
;
301 state
->out_remaining
= 0;
303 /* Make a copy of state attached to the smb2req. Attach
304 the destructor here as this will trigger the sendfile
305 call when the request is destroyed. */
306 state_copy
= talloc(smb2req
, struct smbd_smb2_read_state
);
308 return NT_STATUS_NO_MEMORY
;
310 *state_copy
= *state
;
311 talloc_set_destructor(state_copy
, smb2_sendfile_send_data
);
315 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
);
317 /*******************************************************************
318 Common read complete processing function for both synchronous and
320 *******************************************************************/
322 NTSTATUS
smb2_read_complete(struct tevent_req
*req
, ssize_t nread
, int err
)
324 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
325 struct smbd_smb2_read_state
);
326 files_struct
*fsp
= state
->fsp
;
329 NTSTATUS status
= map_nt_error_from_unix(err
);
331 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
332 "Error = %s (NTSTATUS %s)\n",
340 if (nread
== 0 && state
->in_length
!= 0) {
341 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
343 return NT_STATUS_END_OF_FILE
;
346 if (nread
< state
->in_minimum
) {
347 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
348 "minimum requested %u. Returning end of file\n",
351 (unsigned int)state
->in_minimum
));
352 return NT_STATUS_END_OF_FILE
;
355 DEBUG(3,("smbd_smb2_read: fnum=[%d/%s] length=%lu offset=%lu read=%lu\n",
358 (unsigned long)state
->in_length
,
359 (unsigned long)state
->in_offset
,
360 (unsigned long)nread
));
362 state
->out_data
.length
= nread
;
363 state
->out_remaining
= 0;
368 static bool smbd_smb2_read_cancel(struct tevent_req
*req
)
370 struct smbd_smb2_read_state
*state
=
372 struct smbd_smb2_read_state
);
374 state
->smb2req
->cancelled
= true;
376 return cancel_smb2_aio(state
->smbreq
);
379 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
380 struct tevent_context
*ev
,
381 struct smbd_smb2_request
*smb2req
,
383 uint64_t in_file_id_volatile
,
387 uint32_t in_remaining
)
390 struct tevent_req
*req
= NULL
;
391 struct smbd_smb2_read_state
*state
= NULL
;
392 struct smb_request
*smbreq
= NULL
;
393 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
394 files_struct
*fsp
= NULL
;
396 struct lock_struct lock
;
399 req
= tevent_req_create(mem_ctx
, &state
,
400 struct smbd_smb2_read_state
);
404 state
->smb2req
= smb2req
;
405 state
->in_length
= in_length
;
406 state
->in_offset
= in_offset
;
407 state
->in_minimum
= in_minimum
;
408 state
->out_data
= data_blob_null
;
409 state
->out_remaining
= 0;
411 DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
412 (unsigned long long)in_file_id_volatile
));
414 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
415 if (tevent_req_nomem(smbreq
, req
)) {
416 return tevent_req_post(req
, ev
);
418 state
->smbreq
= smbreq
;
420 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
422 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
423 return tevent_req_post(req
, ev
);
425 if (conn
!= fsp
->conn
) {
426 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
427 return tevent_req_post(req
, ev
);
429 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
430 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
431 return tevent_req_post(req
, ev
);
433 if (fsp
->is_directory
) {
434 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
435 return tevent_req_post(req
, ev
);
439 state
->in_file_id_volatile
= in_file_id_volatile
;
441 if (IS_IPC(smbreq
->conn
)) {
442 struct tevent_req
*subreq
= NULL
;
444 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
445 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
446 return tevent_req_post(req
, ev
);
449 if (!fsp_is_np(fsp
)) {
450 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
451 return tevent_req_post(req
, ev
);
454 subreq
= np_read_send(state
, ev
,
455 fsp
->fake_file_handle
,
456 state
->out_data
.data
,
457 state
->out_data
.length
);
458 if (tevent_req_nomem(subreq
, req
)) {
459 return tevent_req_post(req
, ev
);
461 tevent_req_set_callback(subreq
,
462 smbd_smb2_read_pipe_done
,
467 if (!CHECK_READ(fsp
, smbreq
)) {
468 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
469 return tevent_req_post(req
, ev
);
472 status
= schedule_smb2_aio_read(fsp
->conn
,
477 (SMB_OFF_T
)in_offset
,
480 if (NT_STATUS_IS_OK(status
)) {
482 * Doing an async read, allow this
483 * request to be canceled
485 tevent_req_set_cancel_fn(req
, smbd_smb2_read_cancel
);
489 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
490 /* Real error in setting up aio. Fail. */
491 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
492 return tevent_req_post(req
, ev
);
495 /* Fallback to synchronous. */
497 init_strict_lock_struct(fsp
,
504 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
505 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
506 return tevent_req_post(req
, ev
);
509 /* Try sendfile in preference. */
510 status
= schedule_smb2_sendfile_read(smb2req
, state
);
511 if (NT_STATUS_IS_OK(status
)) {
512 tevent_req_done(req
);
513 return tevent_req_post(req
, ev
);
515 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
516 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
517 tevent_req_nterror(req
, status
);
518 return tevent_req_post(req
, ev
);
522 /* Ok, read into memory. Allocate the out buffer. */
523 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
524 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
525 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
526 return tevent_req_post(req
, ev
);
529 nread
= read_file(fsp
,
530 (char *)state
->out_data
.data
,
536 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
538 DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
539 "len=%llu returned %lld\n",
541 (unsigned long long)in_file_id_volatile
,
542 (unsigned long long)in_offset
,
543 (unsigned long long)in_length
,
546 status
= smb2_read_complete(req
, nread
, saved_errno
);
547 if (!NT_STATUS_IS_OK(status
)) {
548 tevent_req_nterror(req
, status
);
551 tevent_req_done(req
);
553 return tevent_req_post(req
, ev
);
556 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
)
558 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
560 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
561 struct smbd_smb2_read_state
);
564 bool is_data_outstanding
;
566 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
568 if (!NT_STATUS_IS_OK(status
)) {
569 tevent_req_nterror(req
, status
);
573 if (nread
== 0 && state
->out_data
.length
!= 0) {
574 tevent_req_nterror(req
, NT_STATUS_END_OF_FILE
);
578 state
->out_data
.length
= nread
;
579 state
->out_remaining
= 0;
582 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
583 * handle it in SMB1 pipe_read_andx_done().
586 tevent_req_done(req
);
589 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
592 uint32_t *out_remaining
)
595 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
596 struct smbd_smb2_read_state
);
598 if (tevent_req_is_nterror(req
, &status
)) {
599 tevent_req_received(req
);
603 *out_data
= state
->out_data
;
604 talloc_steal(mem_ctx
, out_data
->data
);
605 *out_remaining
= state
->out_remaining
;
607 tevent_req_received(req
);