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
;
179 uint32_t out_remaining
;
182 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
);
184 /*******************************************************************
185 Common read complete processing function for both synchronous and
187 *******************************************************************/
189 NTSTATUS
smb2_read_complete(struct tevent_req
*req
, ssize_t nread
, int err
)
191 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
192 struct smbd_smb2_read_state
);
193 files_struct
*fsp
= state
->fsp
;
196 NTSTATUS status
= map_nt_error_from_unix(err
);
198 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
199 "Error = %s (NTSTATUS %s)\n",
207 if (nread
== 0 && state
->in_length
!= 0) {
208 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
210 return NT_STATUS_END_OF_FILE
;
213 if (nread
< state
->in_minimum
) {
214 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
215 "minimum requested %u. Returning end of file\n",
218 (unsigned int)state
->in_minimum
));
219 return NT_STATUS_END_OF_FILE
;
222 DEBUG(3,("smbd_smb2_read: fnum=[%d/%s] length=%lu offset=%lu read=%lu\n",
225 (unsigned long)state
->in_length
,
226 (unsigned long)state
->in_offset
,
227 (unsigned long)nread
));
229 state
->out_data
.length
= nread
;
230 state
->out_remaining
= 0;
235 static struct tevent_req
*smbd_smb2_read_send(TALLOC_CTX
*mem_ctx
,
236 struct tevent_context
*ev
,
237 struct smbd_smb2_request
*smb2req
,
239 uint64_t in_file_id_volatile
,
243 uint32_t in_remaining
)
246 struct tevent_req
*req
= NULL
;
247 struct smbd_smb2_read_state
*state
= NULL
;
248 struct smb_request
*smbreq
= NULL
;
249 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
250 files_struct
*fsp
= NULL
;
252 struct lock_struct lock
;
255 req
= tevent_req_create(mem_ctx
, &state
,
256 struct smbd_smb2_read_state
);
260 state
->smb2req
= smb2req
;
261 state
->in_length
= in_length
;
262 state
->in_offset
= in_offset
;
263 state
->in_minimum
= in_minimum
;
264 state
->out_data
= data_blob_null
;
265 state
->out_remaining
= 0;
267 DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
268 (unsigned long long)in_file_id_volatile
));
270 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
271 if (tevent_req_nomem(smbreq
, req
)) {
272 return tevent_req_post(req
, ev
);
275 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
277 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
278 return tevent_req_post(req
, ev
);
280 if (conn
!= fsp
->conn
) {
281 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
282 return tevent_req_post(req
, ev
);
284 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
285 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
286 return tevent_req_post(req
, ev
);
288 if (fsp
->is_directory
) {
289 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
290 return tevent_req_post(req
, ev
);
293 state
->out_data
= data_blob_talloc(state
, NULL
, in_length
);
294 if (in_length
> 0 && tevent_req_nomem(state
->out_data
.data
, req
)) {
295 return tevent_req_post(req
, ev
);
300 if (IS_IPC(smbreq
->conn
)) {
301 struct tevent_req
*subreq
= NULL
;
303 if (!fsp_is_np(fsp
)) {
304 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
305 return tevent_req_post(req
, ev
);
308 subreq
= np_read_send(state
, smbd_event_context(),
309 fsp
->fake_file_handle
,
310 state
->out_data
.data
,
311 state
->out_data
.length
);
312 if (tevent_req_nomem(subreq
, req
)) {
313 return tevent_req_post(req
, ev
);
315 tevent_req_set_callback(subreq
,
316 smbd_smb2_read_pipe_done
,
321 if (!CHECK_READ(fsp
, smbreq
)) {
322 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
323 return tevent_req_post(req
, ev
);
326 status
= schedule_smb2_aio_read(fsp
->conn
,
329 (char *)state
->out_data
.data
,
330 (SMB_OFF_T
)in_offset
,
333 if (NT_STATUS_IS_OK(status
)) {
335 * Doing an async read. Don't
336 * send a "gone async" message
337 * as we expect this to be less
338 * than the client timeout period.
339 * JRA. FIXME for offline files..
340 * FIXME. Add cancel code..
342 smb2req
->async
= true;
346 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
347 /* Real error in setting up aio. Fail. */
348 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
349 return tevent_req_post(req
, ev
);
352 /* Fallback to synchronous. */
354 init_strict_lock_struct(fsp
,
361 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
362 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
363 return tevent_req_post(req
, ev
);
366 nread
= read_file(fsp
,
367 (char *)state
->out_data
.data
,
373 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
375 DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
376 "len=%llu returned %lld\n",
378 (unsigned long long)in_file_id_volatile
,
379 (unsigned long long)in_offset
,
380 (unsigned long long)in_length
,
383 status
= smb2_read_complete(req
, nread
, saved_errno
);
384 if (!NT_STATUS_IS_OK(status
)) {
385 tevent_req_nterror(req
, status
);
388 tevent_req_done(req
);
390 return tevent_req_post(req
, ev
);
393 static void smbd_smb2_read_pipe_done(struct tevent_req
*subreq
)
395 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
397 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
398 struct smbd_smb2_read_state
);
401 bool is_data_outstanding
;
403 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
405 if (!NT_STATUS_IS_OK(status
)) {
406 tevent_req_nterror(req
, status
);
410 if (nread
== 0 && state
->out_data
.length
!= 0) {
411 tevent_req_nterror(req
, NT_STATUS_END_OF_FILE
);
415 state
->out_data
.length
= nread
;
416 state
->out_remaining
= 0;
418 tevent_req_done(req
);
421 static NTSTATUS
smbd_smb2_read_recv(struct tevent_req
*req
,
424 uint32_t *out_remaining
)
427 struct smbd_smb2_read_state
*state
= tevent_req_data(req
,
428 struct smbd_smb2_read_state
);
430 if (tevent_req_is_nterror(req
, &status
)) {
431 tevent_req_received(req
);
435 *out_data
= state
->out_data
;
436 talloc_steal(mem_ctx
, out_data
->data
);
437 *out_remaining
= state
->out_remaining
;
439 tevent_req_received(req
);