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
,
33 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
)
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 struct files_struct
*in_fsp
;
57 uint32_t in_minimum_count
;
58 uint32_t in_remaining_bytes
;
59 struct tevent_req
*subreq
;
61 status
= smbd_smb2_request_verify_sizes(req
, 0x31);
62 if (!NT_STATUS_IS_OK(status
)) {
63 return smbd_smb2_request_error(req
, status
);
65 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
66 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
68 in_smbpid
= IVAL(inhdr
, SMB2_HDR_PID
);
70 in_length
= IVAL(inbody
, 0x04);
71 in_offset
= BVAL(inbody
, 0x08);
72 in_file_id_persistent
= BVAL(inbody
, 0x10);
73 in_file_id_volatile
= BVAL(inbody
, 0x18);
74 in_minimum_count
= IVAL(inbody
, 0x20);
75 in_remaining_bytes
= IVAL(inbody
, 0x28);
77 /* check the max read size */
78 if (in_length
> req
->sconn
->smb2
.max_read
) {
79 DEBUG(2,("smbd_smb2_request_process_read: "
80 "client ignored max read: %s: 0x%08X: 0x%08X\n",
81 __location__
, in_length
, req
->sconn
->smb2
.max_read
));
82 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
85 status
= smbd_smb2_request_verify_creditcharge(req
, in_length
);
86 if (!NT_STATUS_IS_OK(status
)) {
87 return smbd_smb2_request_error(req
, status
);
90 in_fsp
= file_fsp_smb2(req
, 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
, req
->sconn
->ev_ctx
,
102 if (subreq
== NULL
) {
103 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
105 tevent_req_set_callback(subreq
, smbd_smb2_request_read_done
, req
);
107 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
110 static void smbd_smb2_request_read_done(struct tevent_req
*subreq
)
112 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
113 struct smbd_smb2_request
);
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 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
;
178 uint32_t out_remaining
;
181 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
182 static int smb2_sendfile_send_data(struct smbd_smb2_read_state
*state
)
184 struct lock_struct lock
;
185 uint32_t in_length
= state
->in_length
;
186 uint64_t in_offset
= state
->in_offset
;
187 files_struct
*fsp
= state
->fsp
;
190 nread
= SMB_VFS_SENDFILE(fsp
->conn
->sconn
->sock
,
195 DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
200 if (errno
== ENOSYS
|| errno
== EINTR
) {
202 * Special hack for broken systems with no working
203 * sendfile. Fake this up by doing read/write calls.
205 set_use_sendfile(SNUM(fsp
->conn
), false);
206 nread
= fake_sendfile(fsp
, in_offset
, in_length
);
208 DEBUG(0,("smb2_sendfile_send_data: "
209 "fake_sendfile failed for "
213 exit_server_cleanly("smb2_sendfile_send_data: "
214 "fake_sendfile failed");
219 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
220 "%s (%s). Terminating\n",
223 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
224 } else if (nread
== 0) {
226 * Some sendfile implementations return 0 to indicate
227 * that there was a short read, but nothing was
228 * actually written to the socket. In this case,
229 * fallback to the normal read path so the header gets
230 * the correct byte count.
232 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
233 "falling back to the normal read: %s\n",
236 nread
= fake_sendfile(fsp
, in_offset
, in_length
);
238 DEBUG(0,("smb2_sendfile_send_data: "
239 "fake_sendfile failed for file "
240 "%s (%s). Terminating\n",
243 exit_server_cleanly("smb2_sendfile_send_data: "
244 "fake_sendfile failed");
250 if (nread
< in_length
) {
251 sendfile_short_send(fsp
, nread
, 0, in_length
);
254 init_strict_lock_struct(fsp
,
255 fsp
->op
->global
->open_persistent_id
,
261 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &lock
);
265 static NTSTATUS
schedule_smb2_sendfile_read(struct smbd_smb2_request
*smb2req
,
266 struct smbd_smb2_read_state
*state
)
268 struct smbd_smb2_read_state
*state_copy
= NULL
;
269 files_struct
*fsp
= state
->fsp
;
272 * We cannot use sendfile if...
273 * We were not configured to do so OR
274 * Signing is active OR
275 * This is a compound SMB2 operation OR
276 * fsp is a STREAM file OR
277 * We're using a write cache OR
278 * It's not a regular file OR
279 * Requested offset is greater than file size OR
280 * there's not enough data in the file.
281 * Phew :-). Luckily this means most
282 * reads on most normal files. JRA.
285 if (!lp__use_sendfile(SNUM(fsp
->conn
)) ||
286 smb2req
->do_signing
||
287 smb2req
->in
.vector_count
!= 4 ||
288 (fsp
->base_fsp
!= NULL
) ||
289 (fsp
->wcp
!= NULL
) ||
290 (!S_ISREG(fsp
->fsp_name
->st
.st_ex_mode
)) ||
291 (state
->in_offset
>= fsp
->fsp_name
->st
.st_ex_size
) ||
292 (fsp
->fsp_name
->st
.st_ex_size
< state
->in_offset
+
294 return NT_STATUS_RETRY
;
297 /* We've already checked there's this amount of data
299 state
->out_data
.length
= state
->in_length
;
300 state
->out_remaining
= 0;
302 /* Make a copy of state attached to the smb2req. Attach
303 the destructor here as this will trigger the sendfile
304 call when the request is destroyed. */
305 state_copy
= talloc(smb2req
, struct smbd_smb2_read_state
);
307 return NT_STATUS_NO_MEMORY
;
309 *state_copy
= *state
;
310 talloc_set_destructor(state_copy
, smb2_sendfile_send_data
);
314 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
);
316 /*******************************************************************
317 Common read complete processing function for both synchronous and
319 *******************************************************************/
321 NTSTATUS
smb2_read_complete(struct tevent_req
*req
, ssize_t nread
, int err
)
323 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
324 struct smbd_smb2_read_state
);
325 files_struct
*fsp
= state
->fsp
;
328 NTSTATUS status
= map_nt_error_from_unix(err
);
330 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
331 "Error = %s (NTSTATUS %s)\n",
339 if (nread
== 0 && state
->in_length
!= 0) {
340 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
342 return NT_STATUS_END_OF_FILE
;
345 if (nread
< state
->in_minimum
) {
346 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
347 "minimum requested %u. Returning end of file\n",
350 (unsigned int)state
->in_minimum
));
351 return NT_STATUS_END_OF_FILE
;
354 DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
357 (unsigned long)state
->in_length
,
358 (unsigned long)state
->in_offset
,
359 (unsigned long)nread
));
361 state
->out_data
.length
= nread
;
362 state
->out_remaining
= 0;
367 static bool smbd_smb2_read_cancel(struct tevent_req
*req
)
369 struct smbd_smb2_read_state
*state
=
371 struct smbd_smb2_read_state
);
373 state
->smb2req
->cancelled
= true;
375 return cancel_smb2_aio(state
->smbreq
);
378 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
379 struct tevent_context
*ev
,
380 struct smbd_smb2_request
*smb2req
,
381 struct files_struct
*fsp
,
386 uint32_t in_remaining
)
389 struct tevent_req
*req
= NULL
;
390 struct smbd_smb2_read_state
*state
= NULL
;
391 struct smb_request
*smbreq
= NULL
;
392 connection_struct
*conn
= smb2req
->tcon
->compat
;
394 struct lock_struct lock
;
397 req
= tevent_req_create(mem_ctx
, &state
,
398 struct smbd_smb2_read_state
);
402 state
->smb2req
= smb2req
;
403 state
->in_length
= in_length
;
404 state
->in_offset
= in_offset
;
405 state
->in_minimum
= in_minimum
;
406 state
->out_data
= data_blob_null
;
407 state
->out_remaining
= 0;
409 DEBUG(10,("smbd_smb2_read: %s - %s\n",
410 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
412 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
413 if (tevent_req_nomem(smbreq
, req
)) {
414 return tevent_req_post(req
, ev
);
416 state
->smbreq
= smbreq
;
418 if (fsp
->is_directory
) {
419 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
420 return tevent_req_post(req
, ev
);
425 if (IS_IPC(smbreq
->conn
)) {
426 struct tevent_req
*subreq
= NULL
;
428 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
429 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
430 return tevent_req_post(req
, ev
);
433 if (!fsp_is_np(fsp
)) {
434 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
435 return tevent_req_post(req
, ev
);
438 subreq
= np_read_send(state
, ev
,
439 fsp
->fake_file_handle
,
440 state
->out_data
.data
,
441 state
->out_data
.length
);
442 if (tevent_req_nomem(subreq
, req
)) {
443 return tevent_req_post(req
, ev
);
445 tevent_req_set_callback(subreq
,
446 smbd_smb2_read_pipe_done
,
451 if (!CHECK_READ(fsp
, smbreq
)) {
452 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
453 return tevent_req_post(req
, ev
);
456 status
= schedule_smb2_aio_read(fsp
->conn
,
464 if (NT_STATUS_IS_OK(status
)) {
466 * Doing an async read, allow this
467 * request to be canceled
469 tevent_req_set_cancel_fn(req
, smbd_smb2_read_cancel
);
473 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
474 /* Real error in setting up aio. Fail. */
475 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
476 return tevent_req_post(req
, ev
);
479 /* Fallback to synchronous. */
481 init_strict_lock_struct(fsp
,
482 fsp
->op
->global
->open_persistent_id
,
488 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
489 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
490 return tevent_req_post(req
, ev
);
493 /* Try sendfile in preference. */
494 status
= schedule_smb2_sendfile_read(smb2req
, state
);
495 if (NT_STATUS_IS_OK(status
)) {
496 tevent_req_done(req
);
497 return tevent_req_post(req
, ev
);
499 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
500 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
501 tevent_req_nterror(req
, status
);
502 return tevent_req_post(req
, ev
);
506 /* Ok, read into memory. Allocate the out buffer. */
507 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
508 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
509 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
510 return tevent_req_post(req
, ev
);
513 nread
= read_file(fsp
,
514 (char *)state
->out_data
.data
,
520 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
522 DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
523 "len=%llu returned %lld\n",
526 (unsigned long long)in_offset
,
527 (unsigned long long)in_length
,
530 status
= smb2_read_complete(req
, nread
, saved_errno
);
531 if (!NT_STATUS_IS_OK(status
)) {
532 tevent_req_nterror(req
, status
);
535 tevent_req_done(req
);
537 return tevent_req_post(req
, ev
);
540 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
)
542 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
544 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
545 struct smbd_smb2_read_state
);
548 bool is_data_outstanding
;
550 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
552 if (!NT_STATUS_IS_OK(status
)) {
553 NTSTATUS old
= status
;
554 status
= nt_status_np_pipe(old
);
555 tevent_req_nterror(req
, status
);
559 if (nread
== 0 && state
->out_data
.length
!= 0) {
560 tevent_req_nterror(req
, NT_STATUS_END_OF_FILE
);
564 state
->out_data
.length
= nread
;
565 state
->out_remaining
= 0;
568 * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
569 * handle it in SMB1 pipe_read_andx_done().
572 tevent_req_done(req
);
575 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
578 uint32_t *out_remaining
)
581 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
582 struct smbd_smb2_read_state
);
584 if (tevent_req_is_nterror(req
, &status
)) {
585 tevent_req_received(req
);
589 *out_data
= state
->out_data
;
590 talloc_steal(mem_ctx
, out_data
->data
);
591 *out_remaining
= state
->out_remaining
;
593 tevent_req_received(req
);