2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) David Disseldorp 2012
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
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 "../lib/ccan/build_assert/build_assert.h"
29 #include "include/ntioctl.h"
30 #include "../librpc/ndr/libndr.h"
31 #include "librpc/gen_ndr/ndr_ioctl.h"
32 #include "smb2_ioctl_private.h"
34 #define COPYCHUNK_MAX_CHUNKS 256 /* 2k8r2 & win8 = 256 */
35 #define COPYCHUNK_MAX_CHUNK_LEN 1048576 /* 2k8r2 & win8 = 1048576 */
36 #define COPYCHUNK_MAX_TOTAL_LEN 16777216 /* 2k8r2 & win8 = 16777216 */
37 static void copychunk_pack_limits(struct srv_copychunk_rsp
*cc_rsp
)
39 cc_rsp
->chunks_written
= COPYCHUNK_MAX_CHUNKS
;
40 cc_rsp
->chunk_bytes_written
= COPYCHUNK_MAX_CHUNK_LEN
;
41 cc_rsp
->total_bytes_written
= COPYCHUNK_MAX_TOTAL_LEN
;
44 static NTSTATUS
copychunk_check_limits(struct srv_copychunk_copy
*cc_copy
)
47 uint32_t total_len
= 0;
49 if (cc_copy
->chunk_count
> COPYCHUNK_MAX_CHUNKS
) {
50 return NT_STATUS_INVALID_PARAMETER
;
53 for (i
= 0; i
< cc_copy
->chunk_count
; i
++) {
54 if (cc_copy
->chunks
[i
].length
> COPYCHUNK_MAX_CHUNK_LEN
) {
55 return NT_STATUS_INVALID_PARAMETER
;
57 total_len
+= cc_copy
->chunks
[i
].length
;
59 if (total_len
> COPYCHUNK_MAX_TOTAL_LEN
) {
60 return NT_STATUS_INVALID_PARAMETER
;
66 struct fsctl_srv_copychunk_state
{
67 struct connection_struct
*conn
;
68 uint32_t dispatch_count
;
70 uint32_t bad_recv_count
;
73 struct files_struct
*src_fsp
;
74 struct files_struct
*dst_fsp
;
76 COPYCHUNK_OUT_EMPTY
= 0,
81 static void fsctl_srv_copychunk_vfs_done(struct tevent_req
*subreq
);
83 static NTSTATUS
copychunk_check_handles(uint32_t ctl_code
,
84 struct files_struct
*src_fsp
,
85 struct files_struct
*dst_fsp
,
86 struct smb_request
*smb1req
)
89 * [MS-SMB2] 3.3.5.15.6 Handling a Server-Side Data Copy Request
90 * The server MUST fail the request with STATUS_ACCESS_DENIED if any of
91 * the following are true:
92 * - The Open.GrantedAccess of the destination file does not include
93 * FILE_WRITE_DATA or FILE_APPEND_DATA.
95 if (!CHECK_WRITE(dst_fsp
)) {
96 DEBUG(5, ("copy chunk no write on dest handle (%s).\n",
97 smb_fname_str_dbg(dst_fsp
->fsp_name
) ));
98 return NT_STATUS_ACCESS_DENIED
;
101 * - The Open.GrantedAccess of the destination file does not include
102 * FILE_READ_DATA, and the CtlCode is FSCTL_SRV_COPYCHUNK.
104 if ((ctl_code
== FSCTL_SRV_COPYCHUNK
)
105 && !CHECK_READ(dst_fsp
, smb1req
)) {
106 DEBUG(5, ("copy chunk no read on dest handle (%s).\n",
107 smb_fname_str_dbg(dst_fsp
->fsp_name
) ));
108 return NT_STATUS_ACCESS_DENIED
;
111 * - The Open.GrantedAccess of the source file does not include
112 * FILE_READ_DATA access.
114 if (!CHECK_READ(src_fsp
, smb1req
)) {
115 DEBUG(5, ("copy chunk no read on src handle (%s).\n",
116 smb_fname_str_dbg(src_fsp
->fsp_name
) ));
117 return NT_STATUS_ACCESS_DENIED
;
120 if (src_fsp
->is_directory
) {
121 DEBUG(5, ("copy chunk no read on src directory handle (%s).\n",
122 smb_fname_str_dbg(src_fsp
->fsp_name
) ));
123 return NT_STATUS_ACCESS_DENIED
;
126 if (dst_fsp
->is_directory
) {
127 DEBUG(5, ("copy chunk no read on dst directory handle (%s).\n",
128 smb_fname_str_dbg(dst_fsp
->fsp_name
) ));
129 return NT_STATUS_ACCESS_DENIED
;
132 if (IS_IPC(src_fsp
->conn
) || IS_IPC(dst_fsp
->conn
)) {
133 DEBUG(5, ("copy chunk no access on IPC$ handle.\n"));
134 return NT_STATUS_ACCESS_DENIED
;
137 if (IS_PRINT(src_fsp
->conn
) || IS_PRINT(dst_fsp
->conn
)) {
138 DEBUG(5, ("copy chunk no access on PRINT handle.\n"));
139 return NT_STATUS_ACCESS_DENIED
;
145 static struct tevent_req
*fsctl_srv_copychunk_send(TALLOC_CTX
*mem_ctx
,
146 struct tevent_context
*ev
,
148 struct files_struct
*dst_fsp
,
150 size_t in_max_output
,
151 struct smbd_smb2_request
*smb2req
)
153 struct tevent_req
*req
;
154 struct srv_copychunk_copy cc_copy
;
155 enum ndr_err_code ndr_ret
;
156 uint64_t src_persistent_h
;
157 uint64_t src_volatile_h
;
159 struct srv_copychunk
*chunk
;
160 struct fsctl_srv_copychunk_state
*state
;
162 /* handler for both copy-chunk variants */
163 SMB_ASSERT((ctl_code
== FSCTL_SRV_COPYCHUNK
)
164 || (ctl_code
== FSCTL_SRV_COPYCHUNK_WRITE
));
166 req
= tevent_req_create(mem_ctx
, &state
,
167 struct fsctl_srv_copychunk_state
);
171 state
->conn
= dst_fsp
->conn
;
173 if (in_max_output
< sizeof(struct srv_copychunk_rsp
)) {
174 DEBUG(3, ("max output %d not large enough to hold copy chunk "
175 "response %lu\n", (int)in_max_output
,
176 (unsigned long)sizeof(struct srv_copychunk_rsp
)));
177 state
->status
= NT_STATUS_INVALID_PARAMETER
;
178 tevent_req_nterror(req
, state
->status
);
179 return tevent_req_post(req
, ev
);
182 ndr_ret
= ndr_pull_struct_blob(in_input
, mem_ctx
, &cc_copy
,
183 (ndr_pull_flags_fn_t
)ndr_pull_srv_copychunk_copy
);
184 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
185 DEBUG(0, ("failed to unmarshall copy chunk req\n"));
186 state
->status
= NT_STATUS_INVALID_PARAMETER
;
187 tevent_req_nterror(req
, state
->status
);
188 return tevent_req_post(req
, ev
);
191 /* persistent/volatile keys sent as the resume key */
192 src_persistent_h
= BVAL(cc_copy
.source_key
, 0);
193 src_volatile_h
= BVAL(cc_copy
.source_key
, 8);
194 state
->src_fsp
= file_fsp_get(smb2req
, src_persistent_h
, src_volatile_h
);
195 if (state
->src_fsp
== NULL
) {
196 DEBUG(3, ("invalid resume key in copy chunk req\n"));
197 state
->status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
198 tevent_req_nterror(req
, state
->status
);
199 return tevent_req_post(req
, ev
);
202 state
->dst_fsp
= dst_fsp
;
204 state
->status
= copychunk_check_handles(ctl_code
,
208 if (!NT_STATUS_IS_OK(state
->status
)) {
209 tevent_req_nterror(req
, state
->status
);
210 return tevent_req_post(req
, ev
);
213 state
->status
= copychunk_check_limits(&cc_copy
);
214 if (tevent_req_nterror(req
, state
->status
)) {
215 DEBUG(3, ("copy chunk req exceeds limits\n"));
216 state
->out_data
= COPYCHUNK_OUT_LIMITS
;
217 return tevent_req_post(req
, ev
);
220 /* any errors from here onwards should carry copychunk response data */
221 state
->out_data
= COPYCHUNK_OUT_RSP
;
223 for (i
= 0; i
< cc_copy
.chunk_count
; i
++) {
224 struct tevent_req
*vfs_subreq
;
225 chunk
= &cc_copy
.chunks
[i
];
226 vfs_subreq
= SMB_VFS_COPY_CHUNK_SEND(dst_fsp
->conn
,
233 if (vfs_subreq
== NULL
) {
234 DEBUG(0, ("VFS copy chunk send failed\n"));
235 state
->status
= NT_STATUS_NO_MEMORY
;
236 if (state
->dispatch_count
== 0) {
237 /* nothing dispatched, return immediately */
238 tevent_req_nterror(req
, state
->status
);
239 return tevent_req_post(req
, ev
);
242 * wait for dispatched to complete before
248 tevent_req_set_callback(vfs_subreq
,
249 fsctl_srv_copychunk_vfs_done
, req
);
250 state
->dispatch_count
++;
256 static void fsctl_srv_copychunk_vfs_done(struct tevent_req
*subreq
)
258 struct tevent_req
*req
= tevent_req_callback_data(
259 subreq
, struct tevent_req
);
260 struct fsctl_srv_copychunk_state
*state
= tevent_req_data(req
,
261 struct fsctl_srv_copychunk_state
);
262 off_t chunk_nwritten
;
266 status
= SMB_VFS_COPY_CHUNK_RECV(state
->conn
, subreq
,
269 if (NT_STATUS_IS_OK(status
)) {
270 DEBUG(10, ("good copy chunk recv %u of %u\n",
271 (unsigned int)state
->recv_count
,
272 (unsigned int)state
->dispatch_count
));
273 state
->total_written
+= chunk_nwritten
;
275 DEBUG(0, ("bad status in copy chunk recv %u of %u: %s\n",
276 (unsigned int)state
->recv_count
,
277 (unsigned int)state
->dispatch_count
,
279 state
->bad_recv_count
++;
280 /* may overwrite previous failed status */
281 state
->status
= status
;
284 if (state
->recv_count
!= state
->dispatch_count
) {
286 * Wait for all VFS copy_chunk requests to complete, even
287 * if an error is received for a specific chunk.
292 if (!tevent_req_nterror(req
, state
->status
)) {
293 tevent_req_done(req
);
297 static NTSTATUS
fsctl_srv_copychunk_recv(struct tevent_req
*req
,
298 struct srv_copychunk_rsp
*cc_rsp
,
301 struct fsctl_srv_copychunk_state
*state
= tevent_req_data(req
,
302 struct fsctl_srv_copychunk_state
);
305 switch (state
->out_data
) {
306 case COPYCHUNK_OUT_EMPTY
:
309 case COPYCHUNK_OUT_LIMITS
:
310 /* 2.2.32.1 - send back our maximum transfer size limits */
311 copychunk_pack_limits(cc_rsp
);
314 case COPYCHUNK_OUT_RSP
:
315 cc_rsp
->chunks_written
= state
->recv_count
- state
->bad_recv_count
;
316 cc_rsp
->chunk_bytes_written
= 0;
317 cc_rsp
->total_bytes_written
= state
->total_written
;
320 default: /* not reached */
324 status
= state
->status
;
325 tevent_req_received(req
);
330 static NTSTATUS
fsctl_validate_neg_info(TALLOC_CTX
*mem_ctx
,
331 struct tevent_context
*ev
,
332 struct smbXsrv_connection
*conn
,
334 uint32_t in_max_output
,
335 DATA_BLOB
*out_output
,
338 uint32_t in_capabilities
;
339 DATA_BLOB in_guid_blob
;
341 uint16_t in_security_mode
;
342 uint16_t in_num_dialects
;
344 DATA_BLOB out_guid_blob
;
347 if (in_input
->length
< 0x18) {
348 return NT_STATUS_INVALID_PARAMETER
;
351 in_capabilities
= IVAL(in_input
->data
, 0x00);
352 in_guid_blob
= data_blob_const(in_input
->data
+ 0x04, 16);
353 in_security_mode
= SVAL(in_input
->data
, 0x14);
354 in_num_dialects
= SVAL(in_input
->data
, 0x16);
356 if (in_input
->length
< (0x18 + in_num_dialects
*2)) {
357 return NT_STATUS_INVALID_PARAMETER
;
360 if (in_max_output
< 0x18) {
361 return NT_STATUS_BUFFER_TOO_SMALL
;
364 status
= GUID_from_ndr_blob(&in_guid_blob
, &in_guid
);
365 if (!NT_STATUS_IS_OK(status
)) {
369 if (in_num_dialects
!= conn
->smb2
.client
.num_dialects
) {
371 return NT_STATUS_ACCESS_DENIED
;
374 for (i
=0; i
< in_num_dialects
; i
++) {
375 uint16_t v
= SVAL(in_input
->data
, 0x18 + i
*2);
377 if (conn
->smb2
.client
.dialects
[i
] != v
) {
379 return NT_STATUS_ACCESS_DENIED
;
383 if (GUID_compare(&in_guid
, &conn
->smb2
.client
.guid
) != 0) {
385 return NT_STATUS_ACCESS_DENIED
;
388 if (in_security_mode
!= conn
->smb2
.client
.security_mode
) {
390 return NT_STATUS_ACCESS_DENIED
;
393 if (in_capabilities
!= conn
->smb2
.client
.capabilities
) {
395 return NT_STATUS_ACCESS_DENIED
;
398 status
= GUID_to_ndr_blob(&conn
->smb2
.server
.guid
, mem_ctx
,
400 if (!NT_STATUS_IS_OK(status
)) {
404 *out_output
= data_blob_talloc(mem_ctx
, NULL
, 0x18);
405 if (out_output
->data
== NULL
) {
406 return NT_STATUS_NO_MEMORY
;
409 SIVAL(out_output
->data
, 0x00, conn
->smb2
.server
.capabilities
);
410 memcpy(out_output
->data
+0x04, out_guid_blob
.data
, 16);
411 SIVAL(out_output
->data
, 0x14, conn
->smb2
.server
.security_mode
);
412 SIVAL(out_output
->data
, 0x16, conn
->smb2
.server
.dialect
);
417 static NTSTATUS
fsctl_srv_req_resume_key(TALLOC_CTX
*mem_ctx
,
418 struct tevent_context
*ev
,
419 struct files_struct
*fsp
,
420 uint32_t in_max_output
,
421 DATA_BLOB
*out_output
)
423 struct req_resume_key_rsp rkey_rsp
;
424 enum ndr_err_code ndr_ret
;
428 return NT_STATUS_FILE_CLOSED
;
431 ZERO_STRUCT(rkey_rsp
);
432 /* combine persistent and volatile handles for the resume key */
433 SBVAL(rkey_rsp
.resume_key
, 0, fsp
->op
->global
->open_persistent_id
);
434 SBVAL(rkey_rsp
.resume_key
, 8, fsp
->op
->global
->open_volatile_id
);
436 ndr_ret
= ndr_push_struct_blob(&output
, mem_ctx
, &rkey_rsp
,
437 (ndr_push_flags_fn_t
)ndr_push_req_resume_key_rsp
);
438 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
439 return NT_STATUS_INTERNAL_ERROR
;
442 if (in_max_output
< output
.length
) {
443 DEBUG(1, ("max output %u too small for resume key rsp %ld\n",
444 (unsigned int)in_max_output
, (long int)output
.length
));
445 return NT_STATUS_INVALID_PARAMETER
;
447 *out_output
= output
;
452 static void smb2_ioctl_network_fs_copychunk_done(struct tevent_req
*subreq
);
454 struct tevent_req
*smb2_ioctl_network_fs(uint32_t ctl_code
,
455 struct tevent_context
*ev
,
456 struct tevent_req
*req
,
457 struct smbd_smb2_ioctl_state
*state
)
459 struct tevent_req
*subreq
;
465 * FSCTL_SRV_COPYCHUNK is issued when a handle has
466 * FILE_READ_DATA and FILE_WRITE_DATA access to the file;
467 * FSCTL_SRV_COPYCHUNK_WRITE is issued when a handle only has
468 * FILE_WRITE_DATA access.
470 case FSCTL_SRV_COPYCHUNK_WRITE
: /* FALL THROUGH */
471 case FSCTL_SRV_COPYCHUNK
:
472 subreq
= fsctl_srv_copychunk_send(state
, ev
,
476 state
->in_max_output
,
478 if (tevent_req_nomem(subreq
, req
)) {
479 return tevent_req_post(req
, ev
);
481 tevent_req_set_callback(subreq
,
482 smb2_ioctl_network_fs_copychunk_done
,
486 case FSCTL_VALIDATE_NEGOTIATE_INFO
:
487 status
= fsctl_validate_neg_info(state
, ev
,
488 state
->smbreq
->sconn
->conn
,
490 state
->in_max_output
,
493 if (!tevent_req_nterror(req
, status
)) {
494 tevent_req_done(req
);
496 return tevent_req_post(req
, ev
);
498 case FSCTL_SRV_REQUEST_RESUME_KEY
:
499 status
= fsctl_srv_req_resume_key(state
, ev
, state
->fsp
,
500 state
->in_max_output
,
502 if (!tevent_req_nterror(req
, status
)) {
503 tevent_req_done(req
);
505 return tevent_req_post(req
, ev
);
508 uint8_t *out_data
= NULL
;
509 uint32_t out_data_len
= 0;
511 if (state
->fsp
== NULL
) {
512 status
= NT_STATUS_NOT_SUPPORTED
;
514 status
= SMB_VFS_FSCTL(state
->fsp
,
517 state
->smbreq
->flags2
,
518 state
->in_input
.data
,
519 state
->in_input
.length
,
521 state
->in_max_output
,
523 state
->out_output
= data_blob_const(out_data
, out_data_len
);
524 if (NT_STATUS_IS_OK(status
)) {
525 tevent_req_done(req
);
526 return tevent_req_post(req
, ev
);
530 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
)) {
531 if (IS_IPC(state
->smbreq
->conn
)) {
532 status
= NT_STATUS_FS_DRIVER_REQUIRED
;
534 status
= NT_STATUS_INVALID_DEVICE_REQUEST
;
538 tevent_req_nterror(req
, status
);
539 return tevent_req_post(req
, ev
);
544 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
545 return tevent_req_post(req
, ev
);
548 static void smb2_ioctl_network_fs_copychunk_done(struct tevent_req
*subreq
)
550 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
552 struct smbd_smb2_ioctl_state
*ioctl_state
= tevent_req_data(req
,
553 struct smbd_smb2_ioctl_state
);
554 struct srv_copychunk_rsp cc_rsp
;
556 bool pack_rsp
= false;
559 status
= fsctl_srv_copychunk_recv(subreq
, &cc_rsp
, &pack_rsp
);
561 if (pack_rsp
== true) {
562 enum ndr_err_code ndr_ret
;
563 ndr_ret
= ndr_push_struct_blob(&ioctl_state
->out_output
,
566 (ndr_push_flags_fn_t
)ndr_push_srv_copychunk_rsp
);
567 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
568 status
= NT_STATUS_INTERNAL_ERROR
;
572 if (!tevent_req_nterror(req
, status
)) {
573 tevent_req_done(req
);