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/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/util/tevent_ntstatus.h"
26 #include "rpc_server/srv_pipe_hnd.h"
27 #include "include/ntioctl.h"
29 static struct tevent_req
*smbd_smb2_ioctl_send(TALLOC_CTX
*mem_ctx
,
30 struct tevent_context
*ev
,
31 struct smbd_smb2_request
*smb2req
,
33 uint64_t in_file_id_volatile
,
35 uint32_t in_max_output
,
37 static NTSTATUS
smbd_smb2_ioctl_recv(struct tevent_req
*req
,
39 DATA_BLOB
*out_output
);
41 static void smbd_smb2_request_ioctl_done(struct tevent_req
*subreq
);
42 NTSTATUS
smbd_smb2_request_process_ioctl(struct smbd_smb2_request
*req
)
45 const uint8_t *inbody
;
46 int i
= req
->current_idx
;
48 uint64_t in_file_id_persistent
;
49 uint64_t in_file_id_volatile
;
50 uint32_t in_input_offset
;
51 uint32_t in_input_length
;
52 DATA_BLOB in_input_buffer
;
53 uint32_t in_max_output_length
;
55 struct tevent_req
*subreq
;
57 status
= smbd_smb2_request_verify_sizes(req
, 0x39);
58 if (!NT_STATUS_IS_OK(status
)) {
59 return smbd_smb2_request_error(req
, status
);
61 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
63 in_ctl_code
= IVAL(inbody
, 0x04);
64 in_file_id_persistent
= BVAL(inbody
, 0x08);
65 in_file_id_volatile
= BVAL(inbody
, 0x10);
66 in_input_offset
= IVAL(inbody
, 0x18);
67 in_input_length
= IVAL(inbody
, 0x1C);
68 in_max_output_length
= IVAL(inbody
, 0x2C);
69 in_flags
= IVAL(inbody
, 0x30);
72 * InputOffset (4 bytes): The offset, in bytes, from the beginning of
73 * the SMB2 header to the input data buffer. If no input data is
74 * required for the FSCTL/IOCTL command being issued, the client SHOULD
75 * set this value to 0.<49>
76 * <49> If no input data is required for the FSCTL/IOCTL command being
77 * issued, Windows-based clients set this field to any value.
79 if ((in_input_length
> 0)
80 && (in_input_offset
!= (SMB2_HDR_BODY
+ req
->in
.vector
[i
+1].iov_len
))) {
81 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
84 if (in_input_length
> req
->in
.vector
[i
+2].iov_len
) {
85 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
88 in_input_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
89 in_input_buffer
.length
= in_input_length
;
91 if (req
->compat_chain_fsp
) {
93 } else if (in_file_id_persistent
== UINT64_MAX
&&
94 in_file_id_volatile
== UINT64_MAX
) {
95 /* without a handle */
96 } else if (in_file_id_persistent
!= in_file_id_volatile
) {
97 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
100 subreq
= smbd_smb2_ioctl_send(req
,
101 req
->sconn
->smb2
.event_ctx
,
106 in_max_output_length
,
108 if (subreq
== NULL
) {
109 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
111 tevent_req_set_callback(subreq
, smbd_smb2_request_ioctl_done
, req
);
113 return smbd_smb2_request_pending_queue(req
, subreq
);
116 static void smbd_smb2_request_ioctl_done(struct tevent_req
*subreq
)
118 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
119 struct smbd_smb2_request
);
120 const uint8_t *inbody
;
121 int i
= req
->current_idx
;
125 uint32_t in_ctl_code
;
126 uint64_t in_file_id_persistent
;
127 uint64_t in_file_id_volatile
;
128 uint32_t out_input_offset
;
129 uint32_t out_output_offset
;
130 DATA_BLOB out_output_buffer
= data_blob_null
;
132 NTSTATUS error
; /* transport error */
134 status
= smbd_smb2_ioctl_recv(subreq
, req
, &out_output_buffer
);
136 DEBUG(10,("smbd_smb2_request_ioctl_done: smbd_smb2_ioctl_recv returned "
138 (unsigned int)out_output_buffer
.length
,
139 nt_errstr(status
) ));
142 if (NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
144 } else if (!NT_STATUS_IS_OK(status
)) {
145 error
= smbd_smb2_request_error(req
, status
);
146 if (!NT_STATUS_IS_OK(error
)) {
147 smbd_server_connection_terminate(req
->sconn
,
154 out_input_offset
= SMB2_HDR_BODY
+ 0x30;
155 out_output_offset
= SMB2_HDR_BODY
+ 0x30;
157 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
159 in_ctl_code
= IVAL(inbody
, 0x04);
160 in_file_id_persistent
= BVAL(inbody
, 0x08);
161 in_file_id_volatile
= BVAL(inbody
, 0x10);
163 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
165 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x30);
166 if (outbody
.data
== NULL
) {
167 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
168 if (!NT_STATUS_IS_OK(error
)) {
169 smbd_server_connection_terminate(req
->sconn
,
176 SSVAL(outbody
.data
, 0x00, 0x30 + 1); /* struct size */
177 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
178 SIVAL(outbody
.data
, 0x04,
179 in_ctl_code
); /* ctl code */
180 SBVAL(outbody
.data
, 0x08,
181 in_file_id_persistent
); /* file id (persistent) */
182 SBVAL(outbody
.data
, 0x10,
183 in_file_id_volatile
); /* file id (volatile) */
184 SIVAL(outbody
.data
, 0x18,
185 out_input_offset
); /* input offset */
186 SIVAL(outbody
.data
, 0x1C, 0); /* input count */
187 SIVAL(outbody
.data
, 0x20,
188 out_output_offset
); /* output offset */
189 SIVAL(outbody
.data
, 0x24,
190 out_output_buffer
.length
); /* output count */
191 SIVAL(outbody
.data
, 0x28, 0); /* flags */
192 SIVAL(outbody
.data
, 0x2C, 0); /* reserved */
195 * Note: Windows Vista and 2008 send back also the
196 * input from the request. But it was fixed in
199 outdyn
= out_output_buffer
;
201 error
= smbd_smb2_request_done_ex(req
, status
, outbody
, &outdyn
,
203 if (!NT_STATUS_IS_OK(error
)) {
204 smbd_server_connection_terminate(req
->sconn
,
210 struct smbd_smb2_ioctl_state
{
211 struct smbd_smb2_request
*smb2req
;
212 struct smb_request
*smbreq
;
215 uint32_t in_max_output
;
216 DATA_BLOB out_output
;
219 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req
*subreq
);
220 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req
*subreq
);
222 static struct tevent_req
*smbd_smb2_ioctl_send(TALLOC_CTX
*mem_ctx
,
223 struct tevent_context
*ev
,
224 struct smbd_smb2_request
*smb2req
,
225 uint32_t in_ctl_code
,
226 uint64_t in_file_id_volatile
,
228 uint32_t in_max_output
,
231 struct tevent_req
*req
;
232 struct smbd_smb2_ioctl_state
*state
;
233 struct smb_request
*smbreq
;
234 files_struct
*fsp
= NULL
;
235 struct tevent_req
*subreq
;
237 req
= tevent_req_create(mem_ctx
, &state
,
238 struct smbd_smb2_ioctl_state
);
242 state
->smb2req
= smb2req
;
243 state
->smbreq
= NULL
;
245 state
->in_input
= in_input
;
246 state
->in_max_output
= in_max_output
;
247 state
->out_output
= data_blob_null
;
249 DEBUG(10, ("smbd_smb2_ioctl: ctl_code[0x%08x] file_id[0x%016llX]\n",
250 (unsigned)in_ctl_code
,
251 (unsigned long long)in_file_id_volatile
));
253 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
254 if (tevent_req_nomem(smbreq
, req
)) {
255 return tevent_req_post(req
, ev
);
257 state
->smbreq
= smbreq
;
259 if (in_file_id_volatile
!= UINT64_MAX
) {
260 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
262 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
263 return tevent_req_post(req
, ev
);
265 if (smbreq
->conn
!= fsp
->conn
) {
266 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
267 return tevent_req_post(req
, ev
);
269 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
270 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
271 return tevent_req_post(req
, ev
);
276 switch (in_ctl_code
) {
277 case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
279 uint16_t in_max_referral_level
;
280 DATA_BLOB in_file_name_buffer
;
281 char *in_file_name_string
;
282 size_t in_file_name_string_size
;
284 bool overflow
= false;
287 char *dfs_data
= NULL
;
289 if (!IS_IPC(smbreq
->conn
)) {
290 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
291 return tevent_req_post(req
, ev
);
294 if (!lp_host_msdfs()) {
295 tevent_req_nterror(req
, NT_STATUS_FS_DRIVER_REQUIRED
);
296 return tevent_req_post(req
, ev
);
299 if (in_input
.length
< (2 + 2)) {
300 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
301 return tevent_req_post(req
, ev
);
304 in_max_referral_level
= SVAL(in_input
.data
, 0);
305 in_file_name_buffer
.data
= in_input
.data
+ 2;
306 in_file_name_buffer
.length
= in_input
.length
- 2;
308 ok
= convert_string_talloc(state
, CH_UTF16
, CH_UNIX
,
309 in_file_name_buffer
.data
,
310 in_file_name_buffer
.length
,
311 &in_file_name_string
,
312 &in_file_name_string_size
);
314 tevent_req_nterror(req
, NT_STATUS_ILLEGAL_CHARACTER
);
315 return tevent_req_post(req
, ev
);
318 dfs_size
= setup_dfs_referral(smbreq
->conn
,
320 in_max_referral_level
,
323 tevent_req_nterror(req
, status
);
324 return tevent_req_post(req
, ev
);
327 if (dfs_size
> in_max_output
) {
329 * TODO: we need a testsuite for this
332 dfs_size
= in_max_output
;
335 state
->out_output
= data_blob_talloc(state
,
340 tevent_req_nomem(state
->out_output
.data
, req
)) {
341 return tevent_req_post(req
, ev
);
345 tevent_req_nterror(req
, STATUS_BUFFER_OVERFLOW
);
347 tevent_req_done(req
);
349 return tevent_req_post(req
, ev
);
351 case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
353 if (!IS_IPC(smbreq
->conn
)) {
354 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
355 return tevent_req_post(req
, ev
);
359 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
360 return tevent_req_post(req
, ev
);
363 if (!fsp_is_np(fsp
)) {
364 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
365 return tevent_req_post(req
, ev
);
368 DEBUG(10,("smbd_smb2_ioctl_send: np_write_send of size %u\n",
369 (unsigned int)in_input
.length
));
371 subreq
= np_write_send(state
, ev
,
372 fsp
->fake_file_handle
,
375 if (tevent_req_nomem(subreq
, req
)) {
376 return tevent_req_post(req
, ev
);
378 tevent_req_set_callback(subreq
,
379 smbd_smb2_ioctl_pipe_write_done
,
383 case 0x00144064: /* FSCTL_SRV_ENUMERATE_SNAPSHOTS */
386 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
387 * and return their volume names. If max_data_count is 16, then it is just
388 * asking for the number of volumes and length of the combined names.
390 * pdata is the data allocated by our caller, but that uses
391 * total_data_count (which is 0 in our case) rather than max_data_count.
392 * Allocate the correct amount and return the pointer to let
393 * it be deallocated when we return.
395 struct shadow_copy_data
*shadow_data
= NULL
;
397 uint32_t labels_data_count
= 0;
404 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
405 return tevent_req_post(req
, ev
);
408 if (in_max_output
< 16) {
409 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
410 "in_max_output(%u) < 16 is invalid!\n",
412 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
413 return tevent_req_post(req
, ev
);
416 if (in_max_output
> 16) {
420 shadow_data
= talloc_zero(talloc_tos(),
421 struct shadow_copy_data
);
422 if (tevent_req_nomem(shadow_data
, req
)) {
423 DEBUG(0,("TALLOC_ZERO() failed!\n"));
424 return tevent_req_post(req
, ev
);
428 * Call the VFS routine to actually do the work.
430 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp
, shadow_data
, labels
)
432 if (errno
== ENOSYS
) {
433 DEBUG(5, ("FSCTL_GET_SHADOW_COPY_DATA: "
434 "connectpath %s, not supported.\n",
435 smbreq
->conn
->connectpath
));
436 status
= NT_STATUS_NOT_SUPPORTED
;
438 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
439 "connectpath %s, failed.\n",
440 smbreq
->conn
->connectpath
));
441 status
= map_nt_error_from_unix(errno
);
443 TALLOC_FREE(shadow_data
);
444 tevent_req_nterror(req
, status
);
445 return tevent_req_post(req
, ev
);
449 (shadow_data
->num_volumes
*2*sizeof(SHADOW_COPY_LABEL
))
453 data_count
= 12+labels_data_count
+4;
458 if (labels
&& (in_max_output
< data_count
)) {
459 DEBUG(0, ("FSCTL_GET_SHADOW_COPY_DATA: "
460 "in_max_output(%u) too small (%u) bytes "
461 "needed!\n", in_max_output
, data_count
));
462 TALLOC_FREE(shadow_data
);
463 tevent_req_nterror(req
, NT_STATUS_BUFFER_TOO_SMALL
);
464 return tevent_req_post(req
, ev
);
467 state
->out_output
= data_blob_talloc(state
, NULL
, data_count
);
468 if (tevent_req_nomem(state
->out_output
.data
, req
)) {
469 return tevent_req_post(req
, ev
);
472 pdata
= (char *)state
->out_output
.data
;
474 /* num_volumes 4 bytes */
475 SIVAL(pdata
, 0, shadow_data
->num_volumes
);
478 /* num_labels 4 bytes */
479 SIVAL(pdata
, 4, shadow_data
->num_volumes
);
482 /* needed_data_count 4 bytes */
483 SIVAL(pdata
, 8, labels_data_count
+4);
487 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for "
489 shadow_data
->num_volumes
, fsp_str_dbg(fsp
)));
490 if (labels
&& shadow_data
->labels
) {
491 for (i
=0; i
<shadow_data
->num_volumes
; i
++) {
492 srvstr_push(pdata
, smbreq
->flags2
,
493 pdata
, shadow_data
->labels
[i
],
494 2*sizeof(SHADOW_COPY_LABEL
),
495 STR_UNICODE
|STR_TERMINATE
);
496 pdata
+= 2*sizeof(SHADOW_COPY_LABEL
);
497 DEBUGADD(10, ("Label[%u]: '%s'\n", i
,
498 shadow_data
->labels
[i
]));
502 TALLOC_FREE(shadow_data
);
504 tevent_req_done(req
);
505 return tevent_req_post(req
, ev
);
509 if (IS_IPC(smbreq
->conn
)) {
510 tevent_req_nterror(req
, NT_STATUS_FS_DRIVER_REQUIRED
);
511 return tevent_req_post(req
, ev
);
513 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
514 return tevent_req_post(req
, ev
);
517 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
518 return tevent_req_post(req
, ev
);
521 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req
*subreq
)
523 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
525 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
526 struct smbd_smb2_ioctl_state
);
528 ssize_t nwritten
= -1;
530 status
= np_write_recv(subreq
, &nwritten
);
532 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
533 (long int)nwritten
));
536 if (!NT_STATUS_IS_OK(status
)) {
537 tevent_req_nterror(req
, status
);
541 if (nwritten
!= state
->in_input
.length
) {
542 tevent_req_nterror(req
, NT_STATUS_PIPE_NOT_AVAILABLE
);
546 state
->out_output
= data_blob_talloc(state
, NULL
, state
->in_max_output
);
547 if (state
->in_max_output
> 0 &&
548 tevent_req_nomem(state
->out_output
.data
, req
)) {
552 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
554 (unsigned int)state
->out_output
.length
));
557 subreq
= np_read_send(state
->smbreq
->conn
,
558 state
->smb2req
->sconn
->smb2
.event_ctx
,
559 state
->fsp
->fake_file_handle
,
560 state
->out_output
.data
,
561 state
->out_output
.length
);
562 if (tevent_req_nomem(subreq
, req
)) {
565 tevent_req_set_callback(subreq
, smbd_smb2_ioctl_pipe_read_done
, req
);
568 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req
*subreq
)
570 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
572 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
573 struct smbd_smb2_ioctl_state
);
576 bool is_data_outstanding
= false;
578 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
580 DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
581 "is_data_outstanding = %d, status = %s\n",
583 (int)is_data_outstanding
,
584 nt_errstr(status
) ));
587 if (!NT_STATUS_IS_OK(status
)) {
588 tevent_req_nterror(req
, status
);
592 state
->out_output
.length
= nread
;
594 if (is_data_outstanding
) {
595 tevent_req_nterror(req
, STATUS_BUFFER_OVERFLOW
);
599 tevent_req_done(req
);
602 static NTSTATUS
smbd_smb2_ioctl_recv(struct tevent_req
*req
,
604 DATA_BLOB
*out_output
)
606 NTSTATUS status
= NT_STATUS_OK
;
607 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
608 struct smbd_smb2_ioctl_state
);
610 if (tevent_req_is_nterror(req
, &status
)) {
611 if (!NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
612 tevent_req_received(req
);
617 *out_output
= state
->out_output
;
618 talloc_steal(mem_ctx
, out_output
->data
);
620 tevent_req_received(req
);