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 static void copychunk_unlock_all(struct files_struct
*src_fsp
,
67 struct files_struct
*dst_fsp
,
68 struct lock_struct
*rd_locks
,
69 struct lock_struct
*wr_locks
,
75 for (i
= 0; i
< num_locks
; i
++) {
76 SMB_VFS_STRICT_UNLOCK(src_fsp
->conn
, src_fsp
, &rd_locks
[i
]);
77 SMB_VFS_STRICT_UNLOCK(dst_fsp
->conn
, dst_fsp
, &wr_locks
[i
]);
81 /* request read and write locks for each chunk */
82 static NTSTATUS
copychunk_lock_all(TALLOC_CTX
*mem_ctx
,
83 struct srv_copychunk_copy
*cc_copy
,
84 struct files_struct
*src_fsp
,
85 struct files_struct
*dst_fsp
,
86 struct lock_struct
**rd_locks
,
87 struct lock_struct
**wr_locks
,
92 struct lock_struct
*rlocks
;
93 struct lock_struct
*wlocks
;
95 rlocks
= talloc_array(mem_ctx
, struct lock_struct
,
96 cc_copy
->chunk_count
);
98 status
= NT_STATUS_NO_MEMORY
;
102 wlocks
= talloc_array(mem_ctx
, struct lock_struct
,
103 cc_copy
->chunk_count
);
104 if (wlocks
== NULL
) {
105 status
= NT_STATUS_NO_MEMORY
;
106 goto err_rlocks_free
;
109 for (i
= 0; i
< cc_copy
->chunk_count
; i
++) {
110 init_strict_lock_struct(src_fsp
,
111 src_fsp
->op
->global
->open_persistent_id
,
112 cc_copy
->chunks
[i
].source_off
,
113 cc_copy
->chunks
[i
].length
,
116 init_strict_lock_struct(dst_fsp
,
117 dst_fsp
->op
->global
->open_persistent_id
,
118 cc_copy
->chunks
[i
].target_off
,
119 cc_copy
->chunks
[i
].length
,
123 if (!SMB_VFS_STRICT_LOCK(src_fsp
->conn
, src_fsp
, &rlocks
[i
])) {
124 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
127 if (!SMB_VFS_STRICT_LOCK(dst_fsp
->conn
, dst_fsp
, &wlocks
[i
])) {
128 /* unlock last rlock, otherwise missed by cleanup */
129 SMB_VFS_STRICT_UNLOCK(src_fsp
->conn
, src_fsp
,
131 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
144 /* cleanup all locks successfully issued so far */
145 copychunk_unlock_all(src_fsp
, dst_fsp
, rlocks
, wlocks
, i
);
154 struct fsctl_srv_copychunk_state
{
155 struct connection_struct
*conn
;
156 uint32_t dispatch_count
;
158 uint32_t bad_recv_count
;
161 struct files_struct
*src_fsp
;
162 struct files_struct
*dst_fsp
;
163 struct lock_struct
*wlocks
;
164 struct lock_struct
*rlocks
;
167 COPYCHUNK_OUT_EMPTY
= 0,
168 COPYCHUNK_OUT_LIMITS
,
172 static void fsctl_srv_copychunk_vfs_done(struct tevent_req
*subreq
);
174 static struct tevent_req
*fsctl_srv_copychunk_send(TALLOC_CTX
*mem_ctx
,
175 struct tevent_context
*ev
,
176 struct files_struct
*dst_fsp
,
178 size_t in_max_output
,
179 struct smbd_smb2_request
*smb2req
)
181 struct tevent_req
*req
;
182 struct srv_copychunk_copy cc_copy
;
183 enum ndr_err_code ndr_ret
;
184 uint64_t src_persistent_h
;
185 uint64_t src_volatile_h
;
187 struct srv_copychunk
*chunk
;
188 struct fsctl_srv_copychunk_state
*state
;
190 req
= tevent_req_create(mem_ctx
, &state
,
191 struct fsctl_srv_copychunk_state
);
195 state
->conn
= dst_fsp
->conn
;
197 if (in_max_output
< sizeof(struct srv_copychunk_rsp
)) {
198 DEBUG(3, ("max output %d not large enough to hold copy chunk "
199 "response %lu\n", (int)in_max_output
,
200 sizeof(struct srv_copychunk_rsp
)));
201 state
->status
= NT_STATUS_INVALID_PARAMETER
;
202 tevent_req_nterror(req
, state
->status
);
203 return tevent_req_post(req
, ev
);
206 ndr_ret
= ndr_pull_struct_blob(in_input
, mem_ctx
, &cc_copy
,
207 (ndr_pull_flags_fn_t
)ndr_pull_srv_copychunk_copy
);
208 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
209 DEBUG(0, ("failed to unmarshall copy chunk req\n"));
210 state
->status
= NT_STATUS_INVALID_PARAMETER
;
211 tevent_req_nterror(req
, state
->status
);
212 return tevent_req_post(req
, ev
);
215 /* persistent/volatile keys sent as the resume key */
216 src_persistent_h
= BVAL(cc_copy
.source_key
, 0);
217 src_volatile_h
= BVAL(cc_copy
.source_key
, 8);
218 state
->src_fsp
= file_fsp_get(smb2req
, src_persistent_h
, src_volatile_h
);
219 if (state
->src_fsp
== NULL
) {
220 DEBUG(3, ("invalid resume key in copy chunk req\n"));
221 state
->status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
222 tevent_req_nterror(req
, state
->status
);
223 return tevent_req_post(req
, ev
);
226 state
->dst_fsp
= dst_fsp
;
228 * [MS-SMB2] 3.3.5.15.6 Handling a Server-Side Data Copy Request
229 * If Open.GrantedAccess of the destination file does not
230 * include FILE_WRITE_DATA, then the request MUST be failed with
231 * STATUS_ACCESS_DENIED. If Open.GrantedAccess of the
232 * destination file does not include FILE_READ_DATA access and
233 * the CtlCode is FSCTL_SRV_COPYCHUNK, then the request MUST be
234 * failed with STATUS_ACCESS_DENIED.
236 if (!CHECK_WRITE(state
->dst_fsp
)) {
237 state
->status
= NT_STATUS_ACCESS_DENIED
;
238 tevent_req_nterror(req
, state
->status
);
239 return tevent_req_post(req
, ev
);
241 if (!CHECK_READ(state
->dst_fsp
, smb2req
->smb1req
)) {
242 state
->status
= NT_STATUS_ACCESS_DENIED
;
243 tevent_req_nterror(req
, state
->status
);
244 return tevent_req_post(req
, ev
);
246 if (!CHECK_READ(state
->src_fsp
, smb2req
->smb1req
)) {
247 state
->status
= NT_STATUS_ACCESS_DENIED
;
248 tevent_req_nterror(req
, state
->status
);
249 return tevent_req_post(req
, ev
);
252 state
->status
= copychunk_check_limits(&cc_copy
);
253 if (tevent_req_nterror(req
, state
->status
)) {
254 DEBUG(3, ("copy chunk req exceeds limits\n"));
255 state
->out_data
= COPYCHUNK_OUT_LIMITS
;
256 return tevent_req_post(req
, ev
);
259 /* any errors from here onwards should carry copychunk response data */
260 state
->out_data
= COPYCHUNK_OUT_RSP
;
262 state
->status
= copychunk_lock_all(state
,
269 if (tevent_req_nterror(req
, state
->status
)) {
270 return tevent_req_post(req
, ev
);
273 for (i
= 0; i
< cc_copy
.chunk_count
; i
++) {
274 struct tevent_req
*vfs_subreq
;
275 chunk
= &cc_copy
.chunks
[i
];
276 vfs_subreq
= SMB_VFS_COPY_CHUNK_SEND(dst_fsp
->conn
,
283 if (vfs_subreq
== NULL
) {
284 DEBUG(0, ("VFS copy chunk send failed\n"));
285 state
->status
= NT_STATUS_NO_MEMORY
;
286 if (state
->dispatch_count
== 0) {
287 /* nothing dispatched, return immediately */
288 copychunk_unlock_all(state
->src_fsp
,
293 tevent_req_nterror(req
, state
->status
);
294 return tevent_req_post(req
, ev
);
297 * wait for dispatched to complete before
298 * returning error, locks held.
303 tevent_req_set_callback(vfs_subreq
,
304 fsctl_srv_copychunk_vfs_done
, req
);
305 state
->dispatch_count
++;
308 /* hold locks until all dispatched requests are completed */
312 static void fsctl_srv_copychunk_vfs_done(struct tevent_req
*subreq
)
314 struct tevent_req
*req
= tevent_req_callback_data(
315 subreq
, struct tevent_req
);
316 struct fsctl_srv_copychunk_state
*state
= tevent_req_data(req
,
317 struct fsctl_srv_copychunk_state
);
318 off_t chunk_nwritten
;
322 status
= SMB_VFS_COPY_CHUNK_RECV(state
->conn
, subreq
,
325 if (NT_STATUS_IS_OK(status
)) {
326 DEBUG(10, ("good copy chunk recv %d of %d\n",
328 state
->dispatch_count
));
329 state
->total_written
+= chunk_nwritten
;
331 DEBUG(0, ("bad status in copy chunk recv %d of %d: %s\n",
333 state
->dispatch_count
,
335 state
->bad_recv_count
++;
336 /* may overwrite previous failed status */
337 state
->status
= status
;
340 if (state
->recv_count
!= state
->dispatch_count
) {
342 * Wait for all VFS copy_chunk requests to complete, even
343 * if an error is received for a specific chunk.
348 /* all VFS copy_chunk requests done */
349 copychunk_unlock_all(state
->src_fsp
,
355 if (!tevent_req_nterror(req
, state
->status
)) {
356 tevent_req_done(req
);
360 static NTSTATUS
fsctl_srv_copychunk_recv(struct tevent_req
*req
,
361 struct srv_copychunk_rsp
*cc_rsp
,
364 struct fsctl_srv_copychunk_state
*state
= tevent_req_data(req
,
365 struct fsctl_srv_copychunk_state
);
368 switch (state
->out_data
) {
369 case COPYCHUNK_OUT_EMPTY
:
372 case COPYCHUNK_OUT_LIMITS
:
373 /* 2.2.32.1 - send back our maximum transfer size limits */
374 copychunk_pack_limits(cc_rsp
);
377 case COPYCHUNK_OUT_RSP
:
378 cc_rsp
->chunks_written
= state
->recv_count
- state
->bad_recv_count
;
379 cc_rsp
->chunk_bytes_written
= 0;
380 cc_rsp
->total_bytes_written
= state
->total_written
;
383 default: /* not reached */
387 status
= state
->status
;
388 tevent_req_received(req
);
393 static NTSTATUS
fsctl_validate_neg_info(TALLOC_CTX
*mem_ctx
,
394 struct tevent_context
*ev
,
395 struct smbXsrv_connection
*conn
,
397 uint32_t in_max_output
,
398 DATA_BLOB
*out_output
,
401 uint32_t in_capabilities
;
402 DATA_BLOB in_guid_blob
;
404 uint16_t in_security_mode
;
405 uint16_t in_num_dialects
;
407 DATA_BLOB out_guid_blob
;
410 if (in_input
->length
< 0x18) {
411 return NT_STATUS_INVALID_PARAMETER
;
414 in_capabilities
= IVAL(in_input
->data
, 0x00);
415 in_guid_blob
= data_blob_const(in_input
->data
+ 0x04, 16);
416 in_security_mode
= SVAL(in_input
->data
, 0x14);
417 in_num_dialects
= SVAL(in_input
->data
, 0x16);
419 if (in_input
->length
< (0x18 + in_num_dialects
*2)) {
420 return NT_STATUS_INVALID_PARAMETER
;
423 if (in_max_output
< 0x18) {
424 return NT_STATUS_BUFFER_TOO_SMALL
;
427 status
= GUID_from_ndr_blob(&in_guid_blob
, &in_guid
);
428 if (!NT_STATUS_IS_OK(status
)) {
432 if (in_num_dialects
!= conn
->smb2
.client
.num_dialects
) {
434 return NT_STATUS_ACCESS_DENIED
;
437 for (i
=0; i
< in_num_dialects
; i
++) {
438 uint16_t v
= SVAL(in_input
->data
, 0x18 + i
*2);
440 if (conn
->smb2
.client
.dialects
[i
] != v
) {
442 return NT_STATUS_ACCESS_DENIED
;
446 if (GUID_compare(&in_guid
, &conn
->smb2
.client
.guid
) != 0) {
448 return NT_STATUS_ACCESS_DENIED
;
451 if (in_security_mode
!= conn
->smb2
.client
.security_mode
) {
453 return NT_STATUS_ACCESS_DENIED
;
456 if (in_capabilities
!= conn
->smb2
.client
.capabilities
) {
458 return NT_STATUS_ACCESS_DENIED
;
461 status
= GUID_to_ndr_blob(&conn
->smb2
.server
.guid
, mem_ctx
,
463 if (!NT_STATUS_IS_OK(status
)) {
467 *out_output
= data_blob_talloc(mem_ctx
, NULL
, 0x18);
468 if (out_output
->data
== NULL
) {
469 return NT_STATUS_NO_MEMORY
;
472 SIVAL(out_output
->data
, 0x00, conn
->smb2
.server
.capabilities
);
473 memcpy(out_output
->data
+0x04, out_guid_blob
.data
, 16);
474 SIVAL(out_output
->data
, 0x14, conn
->smb2
.server
.security_mode
);
475 SIVAL(out_output
->data
, 0x16, conn
->smb2
.server
.dialect
);
480 static NTSTATUS
fsctl_srv_req_resume_key(TALLOC_CTX
*mem_ctx
,
481 struct tevent_context
*ev
,
482 struct files_struct
*fsp
,
483 uint32_t in_max_output
,
484 DATA_BLOB
*out_output
)
486 struct req_resume_key_rsp rkey_rsp
;
487 enum ndr_err_code ndr_ret
;
491 return NT_STATUS_FILE_CLOSED
;
494 ZERO_STRUCT(rkey_rsp
);
495 /* combine persistent and volatile handles for the resume key */
496 SBVAL(rkey_rsp
.resume_key
, 0, fsp
->op
->global
->open_persistent_id
);
497 SBVAL(rkey_rsp
.resume_key
, 8, fsp
->op
->global
->open_volatile_id
);
499 ndr_ret
= ndr_push_struct_blob(&output
, mem_ctx
, &rkey_rsp
,
500 (ndr_push_flags_fn_t
)ndr_push_req_resume_key_rsp
);
501 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
502 return NT_STATUS_INTERNAL_ERROR
;
505 if (in_max_output
< output
.length
) {
506 DEBUG(1, ("max output %u too small for resume key rsp %ld\n",
507 in_max_output
, (long int)output
.length
));
508 return NT_STATUS_INVALID_PARAMETER
;
510 *out_output
= output
;
515 static void smb2_ioctl_network_fs_copychunk_done(struct tevent_req
*subreq
);
517 struct tevent_req
*smb2_ioctl_network_fs(uint32_t ctl_code
,
518 struct tevent_context
*ev
,
519 struct tevent_req
*req
,
520 struct smbd_smb2_ioctl_state
*state
)
522 struct tevent_req
*subreq
;
526 case FSCTL_SRV_COPYCHUNK
:
527 subreq
= fsctl_srv_copychunk_send(state
, ev
, state
->fsp
,
529 state
->in_max_output
,
531 if (tevent_req_nomem(subreq
, req
)) {
532 return tevent_req_post(req
, ev
);
534 tevent_req_set_callback(subreq
,
535 smb2_ioctl_network_fs_copychunk_done
,
539 case FSCTL_VALIDATE_NEGOTIATE_INFO
:
540 status
= fsctl_validate_neg_info(state
, ev
,
541 state
->smbreq
->sconn
->conn
,
543 state
->in_max_output
,
546 if (!tevent_req_nterror(req
, status
)) {
547 tevent_req_done(req
);
549 return tevent_req_post(req
, ev
);
551 case FSCTL_SRV_REQUEST_RESUME_KEY
:
552 status
= fsctl_srv_req_resume_key(state
, ev
, state
->fsp
,
553 state
->in_max_output
,
555 if (!tevent_req_nterror(req
, status
)) {
556 tevent_req_done(req
);
558 return tevent_req_post(req
, ev
);
561 uint8_t *out_data
= NULL
;
562 uint32_t out_data_len
= 0;
564 if (state
->fsp
== NULL
) {
565 status
= NT_STATUS_NOT_SUPPORTED
;
567 status
= SMB_VFS_FSCTL(state
->fsp
,
570 state
->smbreq
->flags2
,
571 state
->in_input
.data
,
572 state
->in_input
.length
,
574 state
->in_max_output
,
576 state
->out_output
= data_blob_const(out_data
, out_data_len
);
577 if (NT_STATUS_IS_OK(status
)) {
578 tevent_req_done(req
);
579 return tevent_req_post(req
, ev
);
583 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
)) {
584 if (IS_IPC(state
->smbreq
->conn
)) {
585 status
= NT_STATUS_FS_DRIVER_REQUIRED
;
587 status
= NT_STATUS_INVALID_DEVICE_REQUEST
;
591 tevent_req_nterror(req
, status
);
592 return tevent_req_post(req
, ev
);
597 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
598 return tevent_req_post(req
, ev
);
601 static void smb2_ioctl_network_fs_copychunk_done(struct tevent_req
*subreq
)
603 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
605 struct smbd_smb2_ioctl_state
*ioctl_state
= tevent_req_data(req
,
606 struct smbd_smb2_ioctl_state
);
607 struct srv_copychunk_rsp cc_rsp
;
609 bool pack_rsp
= false;
612 status
= fsctl_srv_copychunk_recv(subreq
, &cc_rsp
, &pack_rsp
);
614 if (pack_rsp
== true) {
615 enum ndr_err_code ndr_ret
;
616 ndr_ret
= ndr_push_struct_blob(&ioctl_state
->out_output
,
619 (ndr_push_flags_fn_t
)ndr_push_srv_copychunk_rsp
);
620 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
621 status
= NT_STATUS_INTERNAL_ERROR
;
625 if (!tevent_req_nterror(req
, status
)) {
626 tevent_req_done(req
);