Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : Core SMB2 server
4 :
5 : Copyright (C) Stefan Metzmacher 2009
6 : Copyright (C) David Disseldorp 2012
7 :
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.
12 :
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.
17 :
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/>.
20 : */
21 :
22 : #include "includes.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 "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"
33 : #include "lib/messages_ctdb.h"
34 : #include "ctdbd_conn.h"
35 :
36 : #undef DBGC_CLASS
37 : #define DBGC_CLASS DBGC_SMB2
38 :
39 0 : static void copychunk_pack_limits(struct srv_copychunk_rsp *cc_rsp)
40 : {
41 0 : cc_rsp->chunks_written = COPYCHUNK_MAX_CHUNKS;
42 0 : cc_rsp->chunk_bytes_written = COPYCHUNK_MAX_CHUNK_LEN;
43 0 : cc_rsp->total_bytes_written = COPYCHUNK_MAX_TOTAL_LEN;
44 0 : }
45 :
46 0 : static NTSTATUS copychunk_check_limits(struct srv_copychunk_copy *cc_copy)
47 : {
48 : uint32_t i;
49 0 : uint32_t total_len = 0;
50 :
51 : /*
52 : * [MS-SMB2] 3.3.5.15.6 Handling a Server-Side Data Copy Request
53 : * Send and invalid parameter response if:
54 : * - The ChunkCount value is greater than
55 : * ServerSideCopyMaxNumberofChunks
56 : */
57 0 : if (cc_copy->chunk_count > COPYCHUNK_MAX_CHUNKS) {
58 0 : return NT_STATUS_INVALID_PARAMETER;
59 : }
60 :
61 0 : for (i = 0; i < cc_copy->chunk_count; i++) {
62 : /*
63 : * - The Length value in a single chunk is greater than
64 : * ServerSideCopyMaxChunkSize or equal to zero.
65 : */
66 0 : if ((cc_copy->chunks[i].length == 0)
67 0 : || (cc_copy->chunks[i].length > COPYCHUNK_MAX_CHUNK_LEN)) {
68 0 : return NT_STATUS_INVALID_PARAMETER;
69 : }
70 0 : total_len += cc_copy->chunks[i].length;
71 : }
72 : /*
73 : * - Sum of Lengths in all chunks is greater than
74 : * ServerSideCopyMaxDataSize
75 : */
76 0 : if (total_len > COPYCHUNK_MAX_TOTAL_LEN) {
77 0 : return NT_STATUS_INVALID_PARAMETER;
78 : }
79 :
80 0 : return NT_STATUS_OK;
81 : }
82 :
83 : struct fsctl_srv_copychunk_state {
84 : struct tevent_context *ev;
85 : struct connection_struct *conn;
86 : struct srv_copychunk_copy cc_copy;
87 : uint32_t current_chunk;
88 : NTSTATUS status;
89 : off_t total_written;
90 : uint32_t ctl_code;
91 : DATA_BLOB token;
92 : struct files_struct *src_fsp;
93 : struct files_struct *dst_fsp;
94 : enum {
95 : COPYCHUNK_OUT_EMPTY = 0,
96 : COPYCHUNK_OUT_LIMITS,
97 : COPYCHUNK_OUT_RSP,
98 : } out_data;
99 : };
100 : static void fsctl_srv_copychunk_vfs_done(struct tevent_req *subreq);
101 :
102 : static NTSTATUS fsctl_srv_copychunk_loop(struct tevent_req *req);
103 :
104 0 : static struct tevent_req *fsctl_srv_copychunk_send(TALLOC_CTX *mem_ctx,
105 : struct tevent_context *ev,
106 : uint32_t ctl_code,
107 : struct files_struct *dst_fsp,
108 : DATA_BLOB *in_input,
109 : size_t in_max_output,
110 : struct smbd_smb2_request *smb2req)
111 : {
112 0 : struct tevent_req *req = NULL;
113 0 : struct fsctl_srv_copychunk_state *state = NULL;
114 : enum ndr_err_code ndr_ret;
115 : NTSTATUS status;
116 :
117 : /* handler for both copy-chunk variants */
118 0 : SMB_ASSERT((ctl_code == FSCTL_SRV_COPYCHUNK)
119 : || (ctl_code == FSCTL_SRV_COPYCHUNK_WRITE));
120 :
121 0 : req = tevent_req_create(mem_ctx, &state,
122 : struct fsctl_srv_copychunk_state);
123 0 : if (req == NULL) {
124 0 : return NULL;
125 : }
126 0 : *state = (struct fsctl_srv_copychunk_state) {
127 0 : .conn = dst_fsp->conn,
128 : .ev = ev,
129 : .ctl_code = ctl_code,
130 : .dst_fsp = dst_fsp,
131 : };
132 :
133 0 : if (in_max_output < sizeof(struct srv_copychunk_rsp)) {
134 0 : DEBUG(3, ("max output %d not large enough to hold copy chunk "
135 : "response %lu\n", (int)in_max_output,
136 : (unsigned long)sizeof(struct srv_copychunk_rsp)));
137 0 : state->status = NT_STATUS_INVALID_PARAMETER;
138 0 : tevent_req_nterror(req, state->status);
139 0 : return tevent_req_post(req, ev);
140 : }
141 :
142 0 : ndr_ret = ndr_pull_struct_blob(in_input, mem_ctx, &state->cc_copy,
143 : (ndr_pull_flags_fn_t)ndr_pull_srv_copychunk_copy);
144 0 : if (ndr_ret != NDR_ERR_SUCCESS) {
145 0 : DEBUG(0, ("failed to unmarshall copy chunk req\n"));
146 0 : state->status = NT_STATUS_INVALID_PARAMETER;
147 0 : tevent_req_nterror(req, state->status);
148 0 : return tevent_req_post(req, ev);
149 : }
150 :
151 0 : state->token = data_blob_const(state->cc_copy.source_key,
152 : sizeof(state->cc_copy.source_key));
153 :
154 0 : state->status = copychunk_check_limits(&state->cc_copy);
155 0 : if (!NT_STATUS_IS_OK(state->status)) {
156 0 : DEBUG(3, ("copy chunk req exceeds limits\n"));
157 0 : state->out_data = COPYCHUNK_OUT_LIMITS;
158 0 : tevent_req_nterror(req, state->status);
159 0 : return tevent_req_post(req, ev);
160 : }
161 :
162 : /* any errors from here onwards should carry copychunk response data */
163 0 : state->out_data = COPYCHUNK_OUT_RSP;
164 :
165 0 : status = fsctl_srv_copychunk_loop(req);
166 0 : if (tevent_req_nterror(req, status)) {
167 0 : return tevent_req_post(req, ev);
168 : }
169 :
170 0 : return req;
171 : }
172 :
173 0 : static NTSTATUS fsctl_srv_copychunk_loop(struct tevent_req *req)
174 : {
175 0 : struct fsctl_srv_copychunk_state *state = tevent_req_data(
176 : req, struct fsctl_srv_copychunk_state);
177 0 : struct tevent_req *subreq = NULL;
178 0 : uint32_t length = 0;
179 0 : off_t source_off = 0;
180 0 : off_t target_off = 0;
181 :
182 : /*
183 : * chunk_count can be 0 which must either just do nothing returning
184 : * success saying number of copied chunks is 0 (verified against
185 : * Windows).
186 : *
187 : * Or it can be a special macOS copyfile request, so we send this into
188 : * the VFS, vfs_fruit if loaded implements the macOS copyile semantics.
189 : */
190 0 : if (state->cc_copy.chunk_count > 0) {
191 0 : struct srv_copychunk *chunk = NULL;
192 :
193 0 : chunk = &state->cc_copy.chunks[state->current_chunk];
194 0 : length = chunk->length;
195 0 : source_off = chunk->source_off;
196 0 : target_off = chunk->target_off;
197 : }
198 :
199 0 : subreq = SMB_VFS_OFFLOAD_WRITE_SEND(state->dst_fsp->conn,
200 : state,
201 : state->ev,
202 : state->ctl_code,
203 : &state->token,
204 : source_off,
205 : state->dst_fsp,
206 : target_off,
207 : length);
208 0 : if (tevent_req_nomem(subreq, req)) {
209 0 : return NT_STATUS_NO_MEMORY;
210 : }
211 0 : tevent_req_set_callback(subreq, fsctl_srv_copychunk_vfs_done, req);
212 :
213 0 : return NT_STATUS_OK;
214 : }
215 :
216 0 : static void fsctl_srv_copychunk_vfs_done(struct tevent_req *subreq)
217 : {
218 0 : struct tevent_req *req = tevent_req_callback_data(
219 : subreq, struct tevent_req);
220 0 : struct fsctl_srv_copychunk_state *state = tevent_req_data(
221 : req, struct fsctl_srv_copychunk_state);
222 : off_t chunk_nwritten;
223 : NTSTATUS status;
224 :
225 0 : status = SMB_VFS_OFFLOAD_WRITE_RECV(state->conn, subreq,
226 : &chunk_nwritten);
227 0 : TALLOC_FREE(subreq);
228 0 : if (!NT_STATUS_IS_OK(status)) {
229 0 : DBG_ERR("copy chunk failed [%s] chunk [%u] of [%u]\n",
230 : nt_errstr(status),
231 : (unsigned int)state->current_chunk,
232 : (unsigned int)state->cc_copy.chunk_count);
233 0 : tevent_req_nterror(req, status);
234 0 : return;
235 : }
236 :
237 0 : DBG_DEBUG("good copy chunk [%u] of [%u]\n",
238 : (unsigned int)state->current_chunk,
239 : (unsigned int)state->cc_copy.chunk_count);
240 0 : state->total_written += chunk_nwritten;
241 :
242 0 : if (state->cc_copy.chunk_count == 0) {
243 : /*
244 : * This must not produce an error but just return a chunk count
245 : * of 0 in the response.
246 : */
247 0 : tevent_req_done(req);
248 0 : return;
249 : }
250 :
251 0 : state->current_chunk++;
252 0 : if (state->current_chunk == state->cc_copy.chunk_count) {
253 0 : tevent_req_done(req);
254 0 : return;
255 : }
256 :
257 0 : status = fsctl_srv_copychunk_loop(req);
258 0 : if (tevent_req_nterror(req, status)) {
259 0 : return;
260 : }
261 : }
262 :
263 0 : static NTSTATUS fsctl_srv_copychunk_recv(struct tevent_req *req,
264 : struct srv_copychunk_rsp *cc_rsp,
265 : bool *pack_rsp)
266 : {
267 0 : struct fsctl_srv_copychunk_state *state = tevent_req_data(req,
268 : struct fsctl_srv_copychunk_state);
269 : NTSTATUS status;
270 :
271 0 : switch (state->out_data) {
272 0 : case COPYCHUNK_OUT_EMPTY:
273 0 : *pack_rsp = false;
274 0 : break;
275 0 : case COPYCHUNK_OUT_LIMITS:
276 : /* 2.2.32.1 - send back our maximum transfer size limits */
277 0 : copychunk_pack_limits(cc_rsp);
278 0 : *pack_rsp = true;
279 0 : break;
280 0 : case COPYCHUNK_OUT_RSP:
281 0 : cc_rsp->chunks_written = state->current_chunk;
282 0 : cc_rsp->chunk_bytes_written = 0;
283 0 : cc_rsp->total_bytes_written = state->total_written;
284 0 : *pack_rsp = true;
285 0 : break;
286 0 : default: /* not reached */
287 : assert(1);
288 0 : break;
289 : }
290 0 : status = tevent_req_simple_recv_ntstatus(req);
291 0 : return status;
292 : }
293 :
294 : struct cluster_movable_ips {
295 : uint32_t array_len;
296 : uint32_t array_index;
297 : struct sockaddr_storage *ips;
298 : };
299 :
300 0 : static int stash_cluster_movable_ips(uint32_t total_ip_count,
301 : const struct sockaddr_storage *ip,
302 : bool is_movable_ip,
303 : void *private_data)
304 : {
305 : struct cluster_movable_ips *cluster_movable_ips
306 0 : = talloc_get_type_abort(private_data,
307 : struct cluster_movable_ips);
308 :
309 0 : if (!is_movable_ip) {
310 0 : return 0;
311 : }
312 :
313 0 : if (cluster_movable_ips->array_len == 0) {
314 0 : SMB_ASSERT(total_ip_count < INT_MAX);
315 : cluster_movable_ips->ips
316 0 : = talloc_zero_array(cluster_movable_ips,
317 : struct sockaddr_storage,
318 : total_ip_count);
319 0 : if (cluster_movable_ips->ips == NULL) {
320 0 : return ENOMEM;
321 : }
322 0 : cluster_movable_ips->array_len = total_ip_count;
323 : }
324 :
325 0 : SMB_ASSERT(cluster_movable_ips->array_index
326 : < cluster_movable_ips->array_len);
327 :
328 0 : cluster_movable_ips->ips[cluster_movable_ips->array_index] = *ip;
329 0 : cluster_movable_ips->array_index++;
330 :
331 0 : return 0;
332 : }
333 :
334 0 : static bool find_in_cluster_movable_ips(
335 : struct cluster_movable_ips *cluster_movable_ips,
336 : const struct sockaddr_storage *ifss)
337 : {
338 0 : struct samba_sockaddr srv_ip = {
339 : .u = {
340 : .ss = *ifss,
341 : },
342 : };
343 : uint32_t i;
344 :
345 0 : for (i = 0; i < cluster_movable_ips->array_index; i++) {
346 0 : struct samba_sockaddr pub_ip = {
347 : .u = {
348 0 : .ss = cluster_movable_ips->ips[i],
349 : },
350 : };
351 0 : if (sockaddr_equal(&pub_ip.u.sa, &srv_ip.u.sa)) {
352 0 : return true;
353 : }
354 : }
355 0 : return false;
356 : }
357 :
358 0 : static NTSTATUS fsctl_network_iface_info(TALLOC_CTX *mem_ctx,
359 : struct tevent_context *ev,
360 : struct smbXsrv_connection *xconn,
361 : DATA_BLOB *in_input,
362 : uint32_t in_max_output,
363 : DATA_BLOB *out_output)
364 : {
365 0 : struct fsctl_net_iface_info *array = NULL;
366 0 : struct fsctl_net_iface_info *first = NULL;
367 0 : struct fsctl_net_iface_info *last = NULL;
368 : size_t i;
369 : size_t num_ifaces;
370 : enum ndr_err_code ndr_err;
371 0 : struct cluster_movable_ips *cluster_movable_ips = NULL;
372 : int ret;
373 :
374 0 : if (in_input->length != 0) {
375 0 : return NT_STATUS_INVALID_PARAMETER;
376 : }
377 :
378 : /*
379 : * The list of probed interfaces might have changed, we might need to
380 : * refresh local_interfaces to get up-to-date network information, and
381 : * respond to clients which sent FSCTL_QUERY_NETWORK_INTERFACE_INFO.
382 : * For example, network speed is changed, interfaces count is changed
383 : * (some link down or link up), and etc.
384 : */
385 0 : load_interfaces();
386 0 : num_ifaces = iface_count();
387 :
388 0 : *out_output = data_blob_null;
389 :
390 0 : array = talloc_zero_array(mem_ctx,
391 : struct fsctl_net_iface_info,
392 : num_ifaces);
393 0 : if (array == NULL) {
394 0 : return NT_STATUS_NO_MEMORY;
395 : }
396 :
397 0 : if (lp_clustering()) {
398 0 : cluster_movable_ips = talloc_zero(array,
399 : struct cluster_movable_ips);
400 0 : if (cluster_movable_ips == NULL) {
401 0 : TALLOC_FREE(array);
402 0 : return NT_STATUS_NO_MEMORY;
403 : }
404 0 : ret = ctdbd_public_ip_foreach(messaging_ctdb_connection(),
405 : stash_cluster_movable_ips,
406 : cluster_movable_ips);
407 0 : if (ret != 0) {
408 0 : TALLOC_FREE(array);
409 0 : return NT_STATUS_INTERNAL_ERROR;
410 : }
411 : }
412 :
413 0 : for (i=0; i < num_ifaces; i++) {
414 0 : struct fsctl_net_iface_info *cur = &array[i];
415 0 : const struct interface *iface = get_interface(i);
416 0 : const struct sockaddr_storage *ifss = &iface->ip;
417 0 : const void *ifptr = ifss;
418 0 : const struct sockaddr *ifsa = (const struct sockaddr *)ifptr;
419 0 : struct tsocket_address *a = NULL;
420 : char *addr;
421 : bool ok;
422 :
423 0 : ret = tsocket_address_bsd_from_sockaddr(array,
424 : ifsa, sizeof(struct sockaddr_storage),
425 : &a);
426 0 : if (ret != 0) {
427 0 : NTSTATUS status = map_nt_error_from_unix_common(errno);
428 0 : TALLOC_FREE(array);
429 0 : return status;
430 : }
431 :
432 0 : ok = tsocket_address_is_inet(a, "ip");
433 0 : if (!ok) {
434 0 : continue;
435 : }
436 :
437 0 : addr = tsocket_address_inet_addr_string(a, array);
438 0 : if (addr == NULL) {
439 0 : TALLOC_FREE(array);
440 0 : return NT_STATUS_NO_MEMORY;
441 : }
442 :
443 0 : if (cluster_movable_ips != NULL) {
444 0 : bool is_movable_ip = find_in_cluster_movable_ips(
445 : cluster_movable_ips,
446 : ifss);
447 0 : if (is_movable_ip) {
448 0 : DBG_DEBUG("Interface [%s] - "
449 : "has movable public ip - "
450 : "skipping address [%s].\n",
451 : iface->name, addr);
452 0 : continue;
453 : }
454 : }
455 :
456 0 : cur->ifindex = iface->if_index;
457 0 : if (cur->ifindex == 0) {
458 : /*
459 : * Did not get interface index from kernel,
460 : * nor from the config. ==> Apply a common
461 : * default value for these cases.
462 : */
463 0 : cur->ifindex = UINT32_MAX;
464 : }
465 0 : cur->capability = iface->capability;
466 0 : cur->linkspeed = iface->linkspeed;
467 0 : if (cur->linkspeed == 0) {
468 0 : DBG_DEBUG("Link speed 0 on interface [%s] - skipping "
469 : "address [%s].\n", iface->name, addr);
470 0 : continue;
471 : }
472 :
473 0 : ok = tsocket_address_is_inet(a, "ipv4");
474 0 : if (ok) {
475 0 : cur->sockaddr.family = FSCTL_NET_IFACE_AF_INET;
476 0 : cur->sockaddr.saddr.saddr_in.ipv4 = addr;
477 : }
478 0 : ok = tsocket_address_is_inet(a, "ipv6");
479 0 : if (ok) {
480 0 : cur->sockaddr.family = FSCTL_NET_IFACE_AF_INET6;
481 0 : cur->sockaddr.saddr.saddr_in6.ipv6 = addr;
482 : }
483 :
484 0 : if (first == NULL) {
485 0 : first = cur;
486 : }
487 0 : if (last != NULL) {
488 0 : last->next = cur;
489 : }
490 0 : last = cur;
491 : }
492 :
493 0 : if (first == NULL) {
494 0 : TALLOC_FREE(array);
495 0 : return NT_STATUS_OK;
496 : }
497 :
498 0 : if (DEBUGLEVEL >= 10) {
499 0 : NDR_PRINT_DEBUG(fsctl_net_iface_info, first);
500 : }
501 :
502 0 : ndr_err = ndr_push_struct_blob(out_output, mem_ctx, first,
503 : (ndr_push_flags_fn_t)ndr_push_fsctl_net_iface_info);
504 0 : TALLOC_FREE(array);
505 0 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
506 0 : return ndr_map_error2ntstatus(ndr_err);
507 : }
508 :
509 0 : return NT_STATUS_OK;
510 : }
511 :
512 2316 : static NTSTATUS fsctl_validate_neg_info(TALLOC_CTX *mem_ctx,
513 : struct tevent_context *ev,
514 : struct smbXsrv_connection *conn,
515 : DATA_BLOB *in_input,
516 : uint32_t in_max_output,
517 : DATA_BLOB *out_output,
518 : bool *disconnect)
519 : {
520 : uint32_t in_capabilities;
521 : DATA_BLOB in_guid_blob;
522 : struct GUID in_guid;
523 : uint16_t in_security_mode;
524 : uint16_t in_num_dialects;
525 : uint16_t dialect;
526 2316 : struct GUID_ndr_buf out_guid_buf = { .buf = {0}, };
527 : NTSTATUS status;
528 2316 : enum protocol_types protocol = PROTOCOL_NONE;
529 :
530 2316 : if (lp_server_max_protocol() <= PROTOCOL_SMB2_02) {
531 : /*
532 : * With SMB 2.02 we didn't get the
533 : * capabitities, client guid, security mode
534 : * and dialects the client would have offered.
535 : *
536 : * So we behave compatible with a true
537 : * SMB 2.02 server and return NT_STATUS_FILE_CLOSED.
538 : *
539 : * As SMB >= 2.10 offers the two phase SMB2 Negotiate
540 : * we keep supporting FSCTL_VALIDATE_NEGOTIATE_INFO
541 : * starting with SMB 2.10, while Windows only supports
542 : * it starting with SMB > 2.10.
543 : */
544 0 : return NT_STATUS_FILE_CLOSED;
545 : }
546 :
547 2316 : if (in_input->length < 0x18) {
548 0 : return NT_STATUS_INVALID_PARAMETER;
549 : }
550 :
551 2316 : in_capabilities = IVAL(in_input->data, 0x00);
552 2316 : in_guid_blob = data_blob_const(in_input->data + 0x04, 16);
553 2316 : in_security_mode = SVAL(in_input->data, 0x14);
554 2316 : in_num_dialects = SVAL(in_input->data, 0x16);
555 :
556 2316 : if (in_input->length < (0x18 + in_num_dialects*2)) {
557 0 : return NT_STATUS_INVALID_PARAMETER;
558 : }
559 :
560 2316 : if (in_max_output < 0x18) {
561 0 : return NT_STATUS_BUFFER_TOO_SMALL;
562 : }
563 :
564 2316 : status = GUID_from_ndr_blob(&in_guid_blob, &in_guid);
565 2316 : if (!NT_STATUS_IS_OK(status)) {
566 0 : return status;
567 : }
568 :
569 : /*
570 : * From: [MS-SMB2]
571 : * 3.3.5.15.12 Handling a Validate Negotiate Info Request
572 : *
573 : * The server MUST determine the greatest common dialect
574 : * between the dialects it implements and the Dialects array
575 : * of the VALIDATE_NEGOTIATE_INFO request. If no dialect is
576 : * matched, or if the value is not equal to Connection.Dialect,
577 : * the server MUST terminate the transport connection
578 : * and free the Connection object.
579 : */
580 2316 : protocol = smbd_smb2_protocol_dialect_match(in_input->data + 0x18,
581 : in_num_dialects,
582 : &dialect);
583 2316 : if (conn->protocol != protocol) {
584 0 : *disconnect = true;
585 0 : return NT_STATUS_ACCESS_DENIED;
586 : }
587 :
588 2316 : if (!GUID_equal(&in_guid, &conn->smb2.client.guid)) {
589 0 : *disconnect = true;
590 0 : return NT_STATUS_ACCESS_DENIED;
591 : }
592 :
593 2316 : if (in_security_mode != conn->smb2.client.security_mode) {
594 0 : *disconnect = true;
595 0 : return NT_STATUS_ACCESS_DENIED;
596 : }
597 :
598 2316 : if (in_capabilities != conn->smb2.client.capabilities) {
599 0 : *disconnect = true;
600 0 : return NT_STATUS_ACCESS_DENIED;
601 : }
602 :
603 2316 : status = GUID_to_ndr_buf(&conn->smb2.server.guid, &out_guid_buf);
604 2316 : if (!NT_STATUS_IS_OK(status)) {
605 0 : return status;
606 : }
607 :
608 2316 : *out_output = data_blob_talloc(mem_ctx, NULL, 0x18);
609 2316 : if (out_output->data == NULL) {
610 0 : return NT_STATUS_NO_MEMORY;
611 : }
612 :
613 2316 : SIVAL(out_output->data, 0x00, conn->smb2.server.capabilities);
614 2316 : memcpy(out_output->data+0x04, out_guid_buf.buf, 16);
615 2316 : SSVAL(out_output->data, 0x14, conn->smb2.server.security_mode);
616 2316 : SSVAL(out_output->data, 0x16, conn->smb2.server.dialect);
617 :
618 2316 : return NT_STATUS_OK;
619 : }
620 :
621 : static void smb2_ioctl_network_fs_copychunk_done(struct tevent_req *subreq);
622 : static void smb2_ioctl_network_fs_offload_read_done(struct tevent_req *subreq);
623 :
624 2360 : struct tevent_req *smb2_ioctl_network_fs(uint32_t ctl_code,
625 : struct tevent_context *ev,
626 : struct tevent_req *req,
627 : struct smbd_smb2_ioctl_state *state)
628 : {
629 : struct tevent_req *subreq;
630 : NTSTATUS status;
631 :
632 2360 : switch (ctl_code) {
633 : /*
634 : * [MS-SMB2] 2.2.31
635 : * FSCTL_SRV_COPYCHUNK is issued when a handle has
636 : * FILE_READ_DATA and FILE_WRITE_DATA access to the file;
637 : * FSCTL_SRV_COPYCHUNK_WRITE is issued when a handle only has
638 : * FILE_WRITE_DATA access.
639 : */
640 0 : case FSCTL_SRV_COPYCHUNK_WRITE: /* FALL THROUGH */
641 : case FSCTL_SRV_COPYCHUNK:
642 0 : subreq = fsctl_srv_copychunk_send(state, ev,
643 : ctl_code,
644 0 : state->fsp,
645 : &state->in_input,
646 0 : state->in_max_output,
647 : state->smb2req);
648 0 : if (tevent_req_nomem(subreq, req)) {
649 0 : return tevent_req_post(req, ev);
650 : }
651 0 : tevent_req_set_callback(subreq,
652 : smb2_ioctl_network_fs_copychunk_done,
653 : req);
654 0 : return req;
655 : break;
656 0 : case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
657 0 : if (!state->smbreq->xconn->client->server_multi_channel_enabled)
658 : {
659 0 : if (IS_IPC(state->smbreq->conn)) {
660 0 : status = NT_STATUS_FS_DRIVER_REQUIRED;
661 : } else {
662 0 : status = NT_STATUS_INVALID_DEVICE_REQUEST;
663 : }
664 :
665 0 : tevent_req_nterror(req, status);
666 0 : return tevent_req_post(req, ev);
667 : }
668 :
669 0 : status = fsctl_network_iface_info(state, ev,
670 0 : state->smbreq->xconn,
671 : &state->in_input,
672 : state->in_max_output,
673 : &state->out_output);
674 0 : if (!tevent_req_nterror(req, status)) {
675 0 : tevent_req_done(req);
676 : }
677 0 : return tevent_req_post(req, ev);
678 : break;
679 2316 : case FSCTL_VALIDATE_NEGOTIATE_INFO:
680 2316 : status = fsctl_validate_neg_info(state, ev,
681 2316 : state->smbreq->xconn,
682 : &state->in_input,
683 : state->in_max_output,
684 : &state->out_output,
685 : &state->disconnect);
686 2316 : if (!tevent_req_nterror(req, status)) {
687 2316 : tevent_req_done(req);
688 : }
689 2316 : return tevent_req_post(req, ev);
690 : break;
691 0 : case FSCTL_SRV_REQUEST_RESUME_KEY:
692 0 : subreq = SMB_VFS_OFFLOAD_READ_SEND(state,
693 : ev,
694 : state->fsp,
695 : FSCTL_SRV_REQUEST_RESUME_KEY,
696 : 0, 0, 0);
697 0 : if (tevent_req_nomem(subreq, req)) {
698 0 : return tevent_req_post(req, ev);
699 : }
700 0 : tevent_req_set_callback(
701 : subreq, smb2_ioctl_network_fs_offload_read_done, req);
702 0 : return req;
703 :
704 44 : default: {
705 44 : uint8_t *out_data = NULL;
706 44 : uint32_t out_data_len = 0;
707 :
708 44 : if (state->fsp == NULL) {
709 0 : status = NT_STATUS_NOT_SUPPORTED;
710 : } else {
711 44 : status = SMB_VFS_FSCTL(state->fsp,
712 : state,
713 : ctl_code,
714 : state->smbreq->flags2,
715 : state->in_input.data,
716 : state->in_input.length,
717 : &out_data,
718 : state->in_max_output,
719 : &out_data_len);
720 44 : state->out_output = data_blob_const(out_data, out_data_len);
721 44 : if (NT_STATUS_IS_OK(status)) {
722 0 : tevent_req_done(req);
723 0 : return tevent_req_post(req, ev);
724 : }
725 : }
726 :
727 44 : if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
728 44 : if (IS_IPC(state->smbreq->conn)) {
729 0 : status = NT_STATUS_FS_DRIVER_REQUIRED;
730 : } else {
731 44 : status = NT_STATUS_INVALID_DEVICE_REQUEST;
732 : }
733 : }
734 :
735 44 : tevent_req_nterror(req, status);
736 44 : return tevent_req_post(req, ev);
737 : break;
738 : }
739 : }
740 :
741 : tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
742 : return tevent_req_post(req, ev);
743 : }
744 :
745 0 : static void smb2_ioctl_network_fs_copychunk_done(struct tevent_req *subreq)
746 : {
747 0 : struct tevent_req *req = tevent_req_callback_data(subreq,
748 : struct tevent_req);
749 0 : struct smbd_smb2_ioctl_state *ioctl_state = tevent_req_data(req,
750 : struct smbd_smb2_ioctl_state);
751 : struct srv_copychunk_rsp cc_rsp;
752 : NTSTATUS status;
753 0 : bool pack_rsp = false;
754 :
755 0 : ZERO_STRUCT(cc_rsp);
756 0 : status = fsctl_srv_copychunk_recv(subreq, &cc_rsp, &pack_rsp);
757 0 : TALLOC_FREE(subreq);
758 0 : if (pack_rsp == true) {
759 : enum ndr_err_code ndr_ret;
760 0 : ndr_ret = ndr_push_struct_blob(&ioctl_state->out_output,
761 : ioctl_state,
762 : &cc_rsp,
763 : (ndr_push_flags_fn_t)ndr_push_srv_copychunk_rsp);
764 0 : if (ndr_ret != NDR_ERR_SUCCESS) {
765 0 : status = NT_STATUS_INTERNAL_ERROR;
766 : }
767 : }
768 :
769 0 : if (!tevent_req_nterror(req, status)) {
770 0 : tevent_req_done(req);
771 : }
772 0 : }
773 :
774 0 : static void smb2_ioctl_network_fs_offload_read_done(struct tevent_req *subreq)
775 : {
776 0 : struct tevent_req *req = tevent_req_callback_data(
777 : subreq, struct tevent_req);
778 0 : struct smbd_smb2_ioctl_state *state = tevent_req_data(
779 : req, struct smbd_smb2_ioctl_state);
780 : struct req_resume_key_rsp rkey_rsp;
781 : enum ndr_err_code ndr_ret;
782 : uint32_t flags;
783 : uint64_t xferlen;
784 : DATA_BLOB token;
785 : NTSTATUS status;
786 :
787 : /*
788 : * Note that both flags and xferlen are not used with copy-chunk.
789 : */
790 0 : status = SMB_VFS_OFFLOAD_READ_RECV(subreq,
791 : state->fsp->conn,
792 : state,
793 : &flags,
794 : &xferlen,
795 : &token);
796 0 : TALLOC_FREE(subreq);
797 0 : if (tevent_req_nterror(req, status)) {
798 0 : return;
799 : }
800 :
801 0 : if (token.length != sizeof(rkey_rsp.resume_key)) {
802 0 : tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
803 0 : return;
804 : }
805 :
806 0 : ZERO_STRUCT(rkey_rsp);
807 0 : memcpy(rkey_rsp.resume_key, token.data, token.length);
808 :
809 0 : ndr_ret = ndr_push_struct_blob(&state->out_output, state, &rkey_rsp,
810 : (ndr_push_flags_fn_t)ndr_push_req_resume_key_rsp);
811 0 : if (ndr_ret != NDR_ERR_SUCCESS) {
812 0 : tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
813 0 : return;
814 : }
815 :
816 0 : tevent_req_done(req);
817 0 : return;
818 : }
|