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 "include/ntioctl.h"
29 #include "../librpc/ndr/libndr.h"
30 #include "librpc/gen_ndr/ndr_ioctl.h"
31 #include "smb2_ioctl_private.h"
32 #include "../lib/tsocket/tsocket.h"
34 static void copychunk_pack_limits(struct srv_copychunk_rsp *cc_rsp)
36 cc_rsp->chunks_written = COPYCHUNK_MAX_CHUNKS;
37 cc_rsp->chunk_bytes_written = COPYCHUNK_MAX_CHUNK_LEN;
38 cc_rsp->total_bytes_written = COPYCHUNK_MAX_TOTAL_LEN;
41 static NTSTATUS copychunk_check_limits(struct srv_copychunk_copy *cc_copy)
44 uint32_t total_len = 0;
47 * [MS-SMB2] 3.3.5.15.6 Handling a Server-Side Data Copy Request
48 * Send and invalid parameter response if:
49 * - The ChunkCount value is greater than
50 * ServerSideCopyMaxNumberofChunks
52 if (cc_copy->chunk_count > COPYCHUNK_MAX_CHUNKS) {
53 return NT_STATUS_INVALID_PARAMETER;
56 for (i = 0; i < cc_copy->chunk_count; i++) {
58 * - The Length value in a single chunk is greater than
59 * ServerSideCopyMaxChunkSize or equal to zero.
61 if ((cc_copy->chunks[i].length == 0)
62 || (cc_copy->chunks[i].length > COPYCHUNK_MAX_CHUNK_LEN)) {
63 return NT_STATUS_INVALID_PARAMETER;
65 total_len += cc_copy->chunks[i].length;
68 * - Sum of Lengths in all chunks is greater than
69 * ServerSideCopyMaxDataSize
71 if (total_len > COPYCHUNK_MAX_TOTAL_LEN) {
72 return NT_STATUS_INVALID_PARAMETER;
78 struct fsctl_srv_copychunk_state {
79 struct tevent_context *ev;
80 struct connection_struct *conn;
81 struct srv_copychunk_copy cc_copy;
82 uint32_t current_chunk;
86 struct files_struct *src_fsp;
87 struct files_struct *dst_fsp;
89 COPYCHUNK_OUT_EMPTY = 0,
95 static void fsctl_srv_copychunk_vfs_done(struct tevent_req *subreq);
97 static NTSTATUS copychunk_check_handles(uint32_t ctl_code,
98 struct files_struct *src_fsp,
99 struct files_struct *dst_fsp,
100 struct smb_request *smb1req)
103 * [MS-SMB2] 3.3.5.15.6 Handling a Server-Side Data Copy Request
104 * The server MUST fail the request with STATUS_ACCESS_DENIED if any of
105 * the following are true:
106 * - The Open.GrantedAccess of the destination file does not include
107 * FILE_WRITE_DATA or FILE_APPEND_DATA.
109 if (!CHECK_WRITE(dst_fsp)) {
110 DEBUG(5, ("copy chunk no write on dest handle (%s).\n",
111 smb_fname_str_dbg(dst_fsp->fsp_name) ));
112 return NT_STATUS_ACCESS_DENIED;
115 * - The Open.GrantedAccess of the destination file does not include
116 * FILE_READ_DATA, and the CtlCode is FSCTL_SRV_COPYCHUNK.
118 if ((ctl_code == FSCTL_SRV_COPYCHUNK) &&
119 !CHECK_READ_IOCTL(dst_fsp, smb1req)) {
120 DEBUG(5, ("copy chunk no read on dest handle (%s).\n",
121 smb_fname_str_dbg(dst_fsp->fsp_name) ));
122 return NT_STATUS_ACCESS_DENIED;
125 * - The Open.GrantedAccess of the source file does not include
126 * FILE_READ_DATA access.
128 if (!CHECK_READ(src_fsp, smb1req)) {
129 DEBUG(5, ("copy chunk no read on src handle (%s).\n",
130 smb_fname_str_dbg(src_fsp->fsp_name) ));
131 return NT_STATUS_ACCESS_DENIED;
134 if (src_fsp->is_directory) {
135 DEBUG(5, ("copy chunk no read on src directory handle (%s).\n",
136 smb_fname_str_dbg(src_fsp->fsp_name) ));
137 return NT_STATUS_ACCESS_DENIED;
140 if (dst_fsp->is_directory) {
141 DEBUG(5, ("copy chunk no read on dst directory handle (%s).\n",
142 smb_fname_str_dbg(dst_fsp->fsp_name) ));
143 return NT_STATUS_ACCESS_DENIED;
146 if (IS_IPC(src_fsp->conn) || IS_IPC(dst_fsp->conn)) {
147 DEBUG(5, ("copy chunk no access on IPC$ handle.\n"));
148 return NT_STATUS_ACCESS_DENIED;
151 if (IS_PRINT(src_fsp->conn) || IS_PRINT(dst_fsp->conn)) {
152 DEBUG(5, ("copy chunk no access on PRINT handle.\n"));
153 return NT_STATUS_ACCESS_DENIED;
159 static NTSTATUS fsctl_srv_copychunk_loop(struct tevent_req *req);
161 static struct tevent_req *fsctl_srv_copychunk_send(TALLOC_CTX *mem_ctx,
162 struct tevent_context *ev,
164 struct files_struct *dst_fsp,
166 size_t in_max_output,
167 struct smbd_smb2_request *smb2req)
169 struct tevent_req *req = NULL;
170 struct fsctl_srv_copychunk_state *state = NULL;
171 uint64_t src_persistent_h;
172 uint64_t src_volatile_h;
173 enum ndr_err_code ndr_ret;
176 /* handler for both copy-chunk variants */
177 SMB_ASSERT((ctl_code == FSCTL_SRV_COPYCHUNK)
178 || (ctl_code == FSCTL_SRV_COPYCHUNK_WRITE));
180 req = tevent_req_create(mem_ctx, &state,
181 struct fsctl_srv_copychunk_state);
185 *state = (struct fsctl_srv_copychunk_state) {
186 .conn = dst_fsp->conn,
190 if (in_max_output < sizeof(struct srv_copychunk_rsp)) {
191 DEBUG(3, ("max output %d not large enough to hold copy chunk "
192 "response %lu\n", (int)in_max_output,
193 (unsigned long)sizeof(struct srv_copychunk_rsp)));
194 state->status = NT_STATUS_INVALID_PARAMETER;
195 tevent_req_nterror(req, state->status);
196 return tevent_req_post(req, ev);
199 ndr_ret = ndr_pull_struct_blob(in_input, mem_ctx, &state->cc_copy,
200 (ndr_pull_flags_fn_t)ndr_pull_srv_copychunk_copy);
201 if (ndr_ret != NDR_ERR_SUCCESS) {
202 DEBUG(0, ("failed to unmarshall copy chunk req\n"));
203 state->status = NT_STATUS_INVALID_PARAMETER;
204 tevent_req_nterror(req, state->status);
205 return tevent_req_post(req, ev);
208 /* persistent/volatile keys sent as the resume key */
209 src_persistent_h = BVAL(state->cc_copy.source_key, 0);
210 src_volatile_h = BVAL(state->cc_copy.source_key, 8);
211 state->src_fsp = file_fsp_get(smb2req, src_persistent_h, src_volatile_h);
212 if (state->src_fsp == NULL) {
213 DEBUG(3, ("invalid resume key in copy chunk req\n"));
214 state->status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
215 tevent_req_nterror(req, state->status);
216 return tevent_req_post(req, ev);
219 state->dst_fsp = dst_fsp;
221 state->status = copychunk_check_handles(ctl_code,
225 if (!NT_STATUS_IS_OK(state->status)) {
226 tevent_req_nterror(req, state->status);
227 return tevent_req_post(req, ev);
230 state->status = copychunk_check_limits(&state->cc_copy);
231 if (!NT_STATUS_IS_OK(state->status)) {
232 DEBUG(3, ("copy chunk req exceeds limits\n"));
233 state->out_data = COPYCHUNK_OUT_LIMITS;
234 tevent_req_nterror(req, state->status);
235 return tevent_req_post(req, ev);
238 /* any errors from here onwards should carry copychunk response data */
239 state->out_data = COPYCHUNK_OUT_RSP;
241 status = fsctl_srv_copychunk_loop(req);
242 if (tevent_req_nterror(req, status)) {
243 return tevent_req_post(req, ev);
249 static NTSTATUS fsctl_srv_copychunk_loop(struct tevent_req *req)
251 struct fsctl_srv_copychunk_state *state = tevent_req_data(
252 req, struct fsctl_srv_copychunk_state);
253 struct tevent_req *subreq = NULL;
254 struct srv_copychunk *chunk = NULL;
256 if (state->next_chunk > state->cc_copy.chunk_count) {
257 DBG_ERR("Copy-chunk loop next_chunk [%d] chunk_count [%d]\n",
258 state->next_chunk, state->cc_copy.chunk_count);
259 return NT_STATUS_INTERNAL_ERROR;
262 if (state->cc_copy.chunk_count == 0) {
264 * Process as OS X copyfile request. This is currently
265 * the only copychunk request with a chunk count of 0
268 if (!state->src_fsp->aapl_copyfile_supported ||
269 !state->dst_fsp->aapl_copyfile_supported)
272 * This must not return an error but just a chunk count
273 * of 0 in the response.
275 tevent_req_done(req);
276 tevent_req_post(req, state->ev);
279 state->aapl_copyfile = true;
281 subreq = SMB_VFS_COPY_CHUNK_SEND(state->dst_fsp->conn,
289 if (subreq == NULL) {
290 return NT_STATUS_NO_MEMORY;
292 tevent_req_set_callback(subreq,
293 fsctl_srv_copychunk_vfs_done, req);
297 chunk = &state->cc_copy.chunks[state->current_chunk];
300 * Doing the increment and calculation for the next chunk here and not
301 * in the done function, as a later commit will make this a more
302 * sophisticated logic.
306 subreq = SMB_VFS_COPY_CHUNK_SEND(state->dst_fsp->conn,
314 if (tevent_req_nomem(subreq, req)) {
315 return NT_STATUS_NO_MEMORY;
317 tevent_req_set_callback(subreq, fsctl_srv_copychunk_vfs_done, req);
322 static void fsctl_srv_copychunk_vfs_done(struct tevent_req *subreq)
324 struct tevent_req *req = tevent_req_callback_data(
325 subreq, struct tevent_req);
326 struct fsctl_srv_copychunk_state *state = tevent_req_data(
327 req, struct fsctl_srv_copychunk_state);
328 off_t chunk_nwritten;
331 status = SMB_VFS_COPY_CHUNK_RECV(state->conn, subreq,
334 if (!NT_STATUS_IS_OK(status)) {
335 DBG_ERR("copy chunk failed [%s] chunk [%u] of [%u]\n",
337 (unsigned int)state->next_chunk,
338 (unsigned int)state->cc_copy.chunk_count);
339 tevent_req_nterror(req, status);
343 DBG_DEBUG("good copy chunk [%u] of [%u]\n",
344 (unsigned int)state->next_chunk,
345 (unsigned int)state->cc_copy.chunk_count);
346 state->total_written += chunk_nwritten;
348 state->current_chunk = state->next_chunk;
350 if (state->next_chunk == state->cc_copy.chunk_count) {
351 tevent_req_done(req);
355 status = fsctl_srv_copychunk_loop(req);
356 if (tevent_req_nterror(req, status)) {
361 static NTSTATUS fsctl_srv_copychunk_recv(struct tevent_req *req,
362 struct srv_copychunk_rsp *cc_rsp,
365 struct fsctl_srv_copychunk_state *state = tevent_req_data(req,
366 struct fsctl_srv_copychunk_state);
369 switch (state->out_data) {
370 case COPYCHUNK_OUT_EMPTY:
373 case COPYCHUNK_OUT_LIMITS:
374 /* 2.2.32.1 - send back our maximum transfer size limits */
375 copychunk_pack_limits(cc_rsp);
378 case COPYCHUNK_OUT_RSP:
379 if (state->aapl_copyfile == true) {
380 cc_rsp->chunks_written = 0;
382 cc_rsp->chunks_written = state->current_chunk;
384 cc_rsp->chunk_bytes_written = 0;
385 cc_rsp->total_bytes_written = state->total_written;
388 default: /* not reached */
392 status = tevent_req_simple_recv_ntstatus(req);
396 static NTSTATUS fsctl_network_iface_info(TALLOC_CTX *mem_ctx,
397 struct tevent_context *ev,
398 struct smbXsrv_connection *xconn,
400 uint32_t in_max_output,
401 DATA_BLOB *out_output)
403 struct fsctl_net_iface_info *array = NULL;
404 struct fsctl_net_iface_info *first = NULL;
405 struct fsctl_net_iface_info *last = NULL;
407 size_t num_ifaces = iface_count();
408 enum ndr_err_code ndr_err;
410 if (in_input->length != 0) {
411 return NT_STATUS_INVALID_PARAMETER;
414 *out_output = data_blob_null;
416 array = talloc_zero_array(mem_ctx,
417 struct fsctl_net_iface_info,
420 return NT_STATUS_NO_MEMORY;
423 for (i=0; i < num_ifaces; i++) {
424 struct fsctl_net_iface_info *cur = &array[i];
425 const struct interface *iface = get_interface(i);
426 const struct sockaddr_storage *ifss = &iface->ip;
427 const void *ifptr = ifss;
428 const struct sockaddr *ifsa = (const struct sockaddr *)ifptr;
429 struct tsocket_address *a = NULL;
434 ret = tsocket_address_bsd_from_sockaddr(array,
435 ifsa, sizeof(struct sockaddr_storage),
438 return map_nt_error_from_unix_common(errno);
441 ok = tsocket_address_is_inet(a, "ip");
446 addr = tsocket_address_inet_addr_string(a, array);
449 return NT_STATUS_NO_MEMORY;
452 cur->ifindex = iface->if_index;
453 if (cur->ifindex == 0) {
455 * Did not get interface index from kernel,
456 * nor from the config. ==> Apply a common
457 * default value for these cases.
459 cur->ifindex = UINT32_MAX;
461 cur->capability = iface->capability;
462 cur->linkspeed = iface->linkspeed;
463 if (cur->linkspeed == 0) {
464 DBG_DEBUG("Link speed 0 on interface [%s] - skipping "
465 "address [%s].\n", iface->name, addr);
469 ok = tsocket_address_is_inet(a, "ipv4");
471 cur->sockaddr.family = FSCTL_NET_IFACE_AF_INET;
472 cur->sockaddr.saddr.saddr_in.ipv4 = addr;
474 ok = tsocket_address_is_inet(a, "ipv6");
476 cur->sockaddr.family = FSCTL_NET_IFACE_AF_INET6;
477 cur->sockaddr.saddr.saddr_in6.ipv6 = addr;
494 if (DEBUGLEVEL >= 10) {
495 NDR_PRINT_DEBUG(fsctl_net_iface_info, first);
498 ndr_err = ndr_push_struct_blob(out_output, mem_ctx, first,
499 (ndr_push_flags_fn_t)ndr_push_fsctl_net_iface_info);
501 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
502 return ndr_map_error2ntstatus(ndr_err);
508 static NTSTATUS fsctl_validate_neg_info(TALLOC_CTX *mem_ctx,
509 struct tevent_context *ev,
510 struct smbXsrv_connection *conn,
512 uint32_t in_max_output,
513 DATA_BLOB *out_output,
516 uint32_t in_capabilities;
517 DATA_BLOB in_guid_blob;
519 uint16_t in_security_mode;
520 uint16_t in_num_dialects;
522 DATA_BLOB out_guid_blob;
524 enum protocol_types protocol = PROTOCOL_NONE;
526 if (in_input->length < 0x18) {
527 return NT_STATUS_INVALID_PARAMETER;
530 in_capabilities = IVAL(in_input->data, 0x00);
531 in_guid_blob = data_blob_const(in_input->data + 0x04, 16);
532 in_security_mode = SVAL(in_input->data, 0x14);
533 in_num_dialects = SVAL(in_input->data, 0x16);
535 if (in_input->length < (0x18 + in_num_dialects*2)) {
536 return NT_STATUS_INVALID_PARAMETER;
539 if (in_max_output < 0x18) {
540 return NT_STATUS_BUFFER_TOO_SMALL;
543 status = GUID_from_ndr_blob(&in_guid_blob, &in_guid);
544 if (!NT_STATUS_IS_OK(status)) {
550 * 3.3.5.15.12 Handling a Validate Negotiate Info Request
552 * The server MUST determine the greatest common dialect
553 * between the dialects it implements and the Dialects array
554 * of the VALIDATE_NEGOTIATE_INFO request. If no dialect is
555 * matched, or if the value is not equal to Connection.Dialect,
556 * the server MUST terminate the transport connection
557 * and free the Connection object.
559 protocol = smbd_smb2_protocol_dialect_match(in_input->data + 0x18,
562 if (conn->protocol != protocol) {
564 return NT_STATUS_ACCESS_DENIED;
567 if (!GUID_equal(&in_guid, &conn->smb2.client.guid)) {
569 return NT_STATUS_ACCESS_DENIED;
572 if (in_security_mode != conn->smb2.client.security_mode) {
574 return NT_STATUS_ACCESS_DENIED;
577 if (in_capabilities != conn->smb2.client.capabilities) {
579 return NT_STATUS_ACCESS_DENIED;
582 status = GUID_to_ndr_blob(&conn->smb2.server.guid, mem_ctx,
584 if (!NT_STATUS_IS_OK(status)) {
588 *out_output = data_blob_talloc(mem_ctx, NULL, 0x18);
589 if (out_output->data == NULL) {
590 return NT_STATUS_NO_MEMORY;
593 SIVAL(out_output->data, 0x00, conn->smb2.server.capabilities);
594 memcpy(out_output->data+0x04, out_guid_blob.data, 16);
595 SSVAL(out_output->data, 0x14, conn->smb2.server.security_mode);
596 SSVAL(out_output->data, 0x16, conn->smb2.server.dialect);
601 static NTSTATUS fsctl_srv_req_resume_key(TALLOC_CTX *mem_ctx,
602 struct tevent_context *ev,
603 struct files_struct *fsp,
604 uint32_t in_max_output,
605 DATA_BLOB *out_output)
607 struct req_resume_key_rsp rkey_rsp;
608 enum ndr_err_code ndr_ret;
612 return NT_STATUS_FILE_CLOSED;
615 ZERO_STRUCT(rkey_rsp);
616 /* combine persistent and volatile handles for the resume key */
617 SBVAL(rkey_rsp.resume_key, 0, fsp->op->global->open_persistent_id);
618 SBVAL(rkey_rsp.resume_key, 8, fsp->op->global->open_volatile_id);
620 ndr_ret = ndr_push_struct_blob(&output, mem_ctx, &rkey_rsp,
621 (ndr_push_flags_fn_t)ndr_push_req_resume_key_rsp);
622 if (ndr_ret != NDR_ERR_SUCCESS) {
623 return NT_STATUS_INTERNAL_ERROR;
626 if (in_max_output < output.length) {
627 DEBUG(1, ("max output %u too small for resume key rsp %ld\n",
628 (unsigned int)in_max_output, (long int)output.length));
629 return NT_STATUS_INVALID_PARAMETER;
631 *out_output = output;
636 static void smb2_ioctl_network_fs_copychunk_done(struct tevent_req *subreq);
638 struct tevent_req *smb2_ioctl_network_fs(uint32_t ctl_code,
639 struct tevent_context *ev,
640 struct tevent_req *req,
641 struct smbd_smb2_ioctl_state *state)
643 struct tevent_req *subreq;
649 * FSCTL_SRV_COPYCHUNK is issued when a handle has
650 * FILE_READ_DATA and FILE_WRITE_DATA access to the file;
651 * FSCTL_SRV_COPYCHUNK_WRITE is issued when a handle only has
652 * FILE_WRITE_DATA access.
654 case FSCTL_SRV_COPYCHUNK_WRITE: /* FALL THROUGH */
655 case FSCTL_SRV_COPYCHUNK:
656 subreq = fsctl_srv_copychunk_send(state, ev,
660 state->in_max_output,
662 if (tevent_req_nomem(subreq, req)) {
663 return tevent_req_post(req, ev);
665 tevent_req_set_callback(subreq,
666 smb2_ioctl_network_fs_copychunk_done,
670 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
671 if (!state->smbreq->xconn->client->server_multi_channel_enabled)
673 if (IS_IPC(state->smbreq->conn)) {
674 status = NT_STATUS_FS_DRIVER_REQUIRED;
676 status = NT_STATUS_INVALID_DEVICE_REQUEST;
679 tevent_req_nterror(req, status);
680 return tevent_req_post(req, ev);
683 status = fsctl_network_iface_info(state, ev,
684 state->smbreq->xconn,
686 state->in_max_output,
688 if (!tevent_req_nterror(req, status)) {
689 tevent_req_done(req);
691 return tevent_req_post(req, ev);
693 case FSCTL_VALIDATE_NEGOTIATE_INFO:
694 status = fsctl_validate_neg_info(state, ev,
695 state->smbreq->xconn,
697 state->in_max_output,
700 if (!tevent_req_nterror(req, status)) {
701 tevent_req_done(req);
703 return tevent_req_post(req, ev);
705 case FSCTL_SRV_REQUEST_RESUME_KEY:
706 status = fsctl_srv_req_resume_key(state, ev, state->fsp,
707 state->in_max_output,
709 if (!tevent_req_nterror(req, status)) {
710 tevent_req_done(req);
712 return tevent_req_post(req, ev);
715 uint8_t *out_data = NULL;
716 uint32_t out_data_len = 0;
718 if (state->fsp == NULL) {
719 status = NT_STATUS_NOT_SUPPORTED;
721 status = SMB_VFS_FSCTL(state->fsp,
724 state->smbreq->flags2,
725 state->in_input.data,
726 state->in_input.length,
728 state->in_max_output,
730 state->out_output = data_blob_const(out_data, out_data_len);
731 if (NT_STATUS_IS_OK(status)) {
732 tevent_req_done(req);
733 return tevent_req_post(req, ev);
737 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
738 if (IS_IPC(state->smbreq->conn)) {
739 status = NT_STATUS_FS_DRIVER_REQUIRED;
741 status = NT_STATUS_INVALID_DEVICE_REQUEST;
745 tevent_req_nterror(req, status);
746 return tevent_req_post(req, ev);
751 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
752 return tevent_req_post(req, ev);
755 static void smb2_ioctl_network_fs_copychunk_done(struct tevent_req *subreq)
757 struct tevent_req *req = tevent_req_callback_data(subreq,
759 struct smbd_smb2_ioctl_state *ioctl_state = tevent_req_data(req,
760 struct smbd_smb2_ioctl_state);
761 struct srv_copychunk_rsp cc_rsp;
763 bool pack_rsp = false;
766 status = fsctl_srv_copychunk_recv(subreq, &cc_rsp, &pack_rsp);
768 if (pack_rsp == true) {
769 enum ndr_err_code ndr_ret;
770 ndr_ret = ndr_push_struct_blob(&ioctl_state->out_output,
773 (ndr_push_flags_fn_t)ndr_push_srv_copychunk_rsp);
774 if (ndr_ret != NDR_ERR_SUCCESS) {
775 status = NT_STATUS_INTERNAL_ERROR;
779 if (!tevent_req_nterror(req, status)) {
780 tevent_req_done(req);