2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/network.h"
22 #include "lib/util/tevent_ntstatus.h"
23 #include "smb_common.h"
24 #include "smbXcli_base.h"
25 #include "librpc/gen_ndr/ndr_ioctl.h"
27 struct smb2cli_ioctl_state
{
30 uint32_t max_input_length
;
31 uint32_t max_output_length
;
32 struct iovec
*recv_iov
;
34 DATA_BLOB out_input_buffer
;
35 DATA_BLOB out_output_buffer
;
39 static void smb2cli_ioctl_done(struct tevent_req
*subreq
);
41 struct tevent_req
*smb2cli_ioctl_send(TALLOC_CTX
*mem_ctx
,
42 struct tevent_context
*ev
,
43 struct smbXcli_conn
*conn
,
44 uint32_t timeout_msec
,
45 struct smbXcli_session
*session
,
46 struct smbXcli_tcon
*tcon
,
47 uint64_t in_fid_persistent
,
48 uint64_t in_fid_volatile
,
50 uint32_t in_max_input_length
,
51 const DATA_BLOB
*in_input_buffer
,
52 uint32_t in_max_output_length
,
53 const DATA_BLOB
*in_output_buffer
,
56 struct tevent_req
*req
, *subreq
;
57 struct smb2cli_ioctl_state
*state
;
61 uint32_t input_buffer_offset
= 0;
62 uint32_t input_buffer_length
= 0;
63 uint32_t output_buffer_offset
= 0;
64 uint32_t output_buffer_length
= 0;
65 uint32_t pad_length
= 0;
67 uint32_t max_dyn_len
= 0;
69 req
= tevent_req_create(mem_ctx
, &state
,
70 struct smb2cli_ioctl_state
);
74 state
->ctl_code
= in_ctl_code
;
75 state
->max_input_length
= in_max_input_length
;
76 state
->max_output_length
= in_max_output_length
;
78 tmp64
= in_max_input_length
;
79 tmp64
+= in_max_output_length
;
80 if (tmp64
> UINT32_MAX
) {
81 max_dyn_len
= UINT32_MAX
;
86 if (in_input_buffer
) {
87 input_buffer_offset
= SMB2_HDR_BODY
+0x38;
88 input_buffer_length
= in_input_buffer
->length
;
91 if (in_output_buffer
) {
92 output_buffer_offset
= SMB2_HDR_BODY
+0x38;
93 output_buffer_length
= in_output_buffer
->length
;
94 if (input_buffer_length
> 0 && output_buffer_length
> 0) {
96 output_buffer_offset
+= input_buffer_length
;
97 tmp
= output_buffer_offset
;
98 output_buffer_offset
= NDR_ROUND(output_buffer_offset
, 8);
99 pad_length
= output_buffer_offset
- tmp
;
103 fixed
= state
->fixed
;
105 SSVAL(fixed
, 0x00, 0x39);
106 SSVAL(fixed
, 0x02, 0); /* reserved */
107 SIVAL(fixed
, 0x04, in_ctl_code
);
108 SBVAL(fixed
, 0x08, in_fid_persistent
);
109 SBVAL(fixed
, 0x10, in_fid_volatile
);
110 SIVAL(fixed
, 0x18, input_buffer_offset
);
111 SIVAL(fixed
, 0x1C, input_buffer_length
);
112 SIVAL(fixed
, 0x20, in_max_input_length
);
113 SIVAL(fixed
, 0x24, output_buffer_offset
);
114 SIVAL(fixed
, 0x28, output_buffer_length
);
115 SIVAL(fixed
, 0x2C, in_max_output_length
);
116 SIVAL(fixed
, 0x30, in_flags
);
117 SIVAL(fixed
, 0x34, 0); /* reserved */
119 if (input_buffer_length
> 0 && output_buffer_length
> 0) {
120 size_t avail
= UINT32_MAX
- (input_buffer_length
+ pad_length
);
121 size_t ofs
= output_buffer_offset
- input_buffer_offset
;
123 if (avail
< output_buffer_length
) {
124 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
125 return tevent_req_post(req
, ev
);
128 dyn_len
= input_buffer_length
+ output_buffer_length
+ pad_length
;
130 dyn
= talloc_zero_array(state
, uint8_t, dyn_len
);
131 if (tevent_req_nomem(dyn
, req
)) {
132 return tevent_req_post(req
, ev
);
134 memcpy(dyn
, in_input_buffer
->data
,
135 in_input_buffer
->length
);
136 memcpy(dyn
+ ofs
, in_output_buffer
->data
,
137 in_output_buffer
->length
);
138 } else if (input_buffer_length
> 0) {
139 dyn
= in_input_buffer
->data
;
140 dyn_len
= in_input_buffer
->length
;
141 } else if (output_buffer_length
> 0) {
142 dyn
= in_output_buffer
->data
;
143 dyn_len
= in_output_buffer
->length
;
145 dyn
= state
->dyn_pad
;
146 dyn_len
= sizeof(state
->dyn_pad
);
149 subreq
= smb2cli_req_send(state
, ev
, conn
, SMB2_OP_IOCTL
,
154 state
->fixed
, sizeof(state
->fixed
),
157 if (tevent_req_nomem(subreq
, req
)) {
158 return tevent_req_post(req
, ev
);
160 tevent_req_set_callback(subreq
, smb2cli_ioctl_done
, req
);
164 static void smb2cli_ioctl_done(struct tevent_req
*subreq
)
166 struct tevent_req
*req
=
167 tevent_req_callback_data(subreq
,
169 struct smb2cli_ioctl_state
*state
=
171 struct smb2cli_ioctl_state
);
176 DATA_BLOB dyn_buffer
= data_blob_null
;
177 uint32_t dyn_ofs
= SMB2_HDR_BODY
+ 0x30;
178 uint32_t input_min_offset
;
179 uint32_t input_buffer_offset
;
180 uint32_t input_buffer_length
;
181 uint32_t input_next_offset
;
182 uint32_t output_min_offset
;
183 uint32_t output_buffer_offset
;
184 uint32_t output_buffer_length
;
185 uint32_t output_next_offset
;
186 static const struct smb2cli_req_expected_response expected
[] = {
188 .status
= NT_STATUS_OK
,
192 .status
= STATUS_BUFFER_OVERFLOW
,
197 * We need to make sure that
198 * a response with NT_STATUS_FILE_CLOSED
199 * without signing generates NT_STATUS_ACCESS_DENIED
200 * if the request was signed.
202 .status
= NT_STATUS_FILE_CLOSED
,
209 .status
= NT_STATUS_INVALID_PARAMETER
,
214 * a special case for FSCTL_SRV_COPYCHUNK_*
216 .status
= NT_STATUS_INVALID_PARAMETER
,
221 status
= smb2cli_req_recv(subreq
, state
, &iov
,
222 expected
, ARRAY_SIZE(expected
));
224 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
225 switch (state
->ctl_code
) {
226 case FSCTL_SRV_COPYCHUNK
:
227 case FSCTL_SRV_COPYCHUNK_WRITE
:
230 tevent_req_nterror(req
, status
);
234 if (iov
[1].iov_len
!= 0x30) {
235 tevent_req_nterror(req
,
236 NT_STATUS_INVALID_NETWORK_RESPONSE
);
239 } else if (NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
242 if (tevent_req_nterror(req
, status
)) {
248 * At this stage we're sure that got a body size of 0x31,
249 * either with NT_STATUS_OK, STATUS_BUFFER_OVERFLOW or
250 * NT_STATUS_INVALID_PARAMETER.
253 state
->recv_iov
= iov
;
254 fixed
= (uint8_t *)iov
[1].iov_base
;
255 dyn_buffer
= data_blob_const((uint8_t *)iov
[2].iov_base
,
258 input_buffer_offset
= IVAL(fixed
, 0x18);
259 input_buffer_length
= IVAL(fixed
, 0x1C);
260 output_buffer_offset
= IVAL(fixed
, 0x20);
261 output_buffer_length
= IVAL(fixed
, 0x24);
263 input_min_offset
= dyn_ofs
;
264 input_next_offset
= dyn_ofs
;
265 error
= smb2cli_parse_dyn_buffer(dyn_ofs
,
270 state
->max_input_length
,
272 &state
->out_input_buffer
);
273 if (tevent_req_nterror(req
, error
)) {
278 * If output data is returned, the output offset MUST be set to
279 * InputOffset + InputCount rounded up to a multiple of 8.
281 output_min_offset
= NDR_ROUND(input_next_offset
, 8);
282 output_next_offset
= 0; /* this variable is completely ignored */
283 error
= smb2cli_parse_dyn_buffer(dyn_ofs
,
286 output_buffer_offset
,
287 output_buffer_length
,
288 state
->max_output_length
,
290 &state
->out_output_buffer
);
291 if (tevent_req_nterror(req
, error
)) {
295 state
->out_valid
= true;
297 if (tevent_req_nterror(req
, status
)) {
301 tevent_req_done(req
);
304 NTSTATUS
smb2cli_ioctl_recv(struct tevent_req
*req
,
306 DATA_BLOB
*out_input_buffer
,
307 DATA_BLOB
*out_output_buffer
)
309 struct smb2cli_ioctl_state
*state
=
311 struct smb2cli_ioctl_state
);
312 NTSTATUS status
= NT_STATUS_OK
;
314 if (tevent_req_is_nterror(req
, &status
) && !state
->out_valid
) {
315 if (out_input_buffer
) {
316 *out_input_buffer
= data_blob_null
;
318 if (out_output_buffer
) {
319 *out_output_buffer
= data_blob_null
;
321 tevent_req_received(req
);
325 talloc_steal(mem_ctx
, state
->recv_iov
);
326 if (out_input_buffer
) {
327 *out_input_buffer
= state
->out_input_buffer
;
329 if (out_output_buffer
) {
330 *out_output_buffer
= state
->out_output_buffer
;
333 tevent_req_received(req
);
337 NTSTATUS
smb2cli_ioctl(struct smbXcli_conn
*conn
,
338 uint32_t timeout_msec
,
339 struct smbXcli_session
*session
,
340 struct smbXcli_tcon
*tcon
,
341 uint64_t in_fid_persistent
,
342 uint64_t in_fid_volatile
,
343 uint32_t in_ctl_code
,
344 uint32_t in_max_input_length
,
345 const DATA_BLOB
*in_input_buffer
,
346 uint32_t in_max_output_length
,
347 const DATA_BLOB
*in_output_buffer
,
350 DATA_BLOB
*out_input_buffer
,
351 DATA_BLOB
*out_output_buffer
)
353 TALLOC_CTX
*frame
= talloc_stackframe();
354 struct tevent_context
*ev
;
355 struct tevent_req
*req
;
356 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
358 if (smbXcli_conn_has_async_calls(conn
)) {
360 * Can't use sync call while an async call is in flight
362 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
365 ev
= samba_tevent_context_init(frame
);
369 req
= smb2cli_ioctl_send(frame
, ev
, conn
, timeout_msec
,
376 in_max_output_length
,
382 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
385 status
= smb2cli_ioctl_recv(req
, mem_ctx
,
393 struct smb2cli_ioctl_pipe_wait_state
{
398 static void smb2cli_ioctl_pipe_wait_done(struct tevent_req
*subreq
);
400 struct tevent_req
*smb2cli_ioctl_pipe_wait_send(TALLOC_CTX
*mem_ctx
,
401 struct tevent_context
*ev
,
402 struct smbXcli_conn
*conn
,
403 uint32_t timeout_msec
,
404 struct smbXcli_session
*session
,
405 struct smbXcli_tcon
*tcon
,
406 const char *pipe_name
,
407 uint64_t pipe_wait_timeout
)
409 struct tevent_req
*req
= NULL
;
410 struct tevent_req
*subreq
= NULL
;
411 struct smb2cli_ioctl_pipe_wait_state
*state
= NULL
;
412 struct fsctl_pipe_wait fsctl
= {0};
413 enum ndr_err_code err
;
415 req
= tevent_req_create(mem_ctx
, &state
,
416 struct smb2cli_ioctl_pipe_wait_state
);
421 state
->out_blob
= data_blob_string_const("");
423 fsctl
.pipe_name
= pipe_name
;
424 fsctl
.timeout
= pipe_wait_timeout
;
425 fsctl
.timeout_specified
= pipe_wait_timeout
> 0 ? 1 : 0;
427 err
= ndr_push_struct_blob(&state
->in_blob
, mem_ctx
, &fsctl
,
428 (ndr_push_flags_fn_t
)ndr_push_fsctl_pipe_wait
);
429 if (!NDR_ERR_CODE_IS_SUCCESS(err
)) {
433 subreq
= smb2cli_ioctl_send(mem_ctx
, ev
, conn
, timeout_msec
,
435 UINT64_MAX
, UINT64_MAX
,
439 SMB2_IOCTL_FLAG_IS_FSCTL
);
440 if (tevent_req_nomem(subreq
, req
)) {
441 return tevent_req_post(subreq
, ev
);
443 tevent_req_set_callback(subreq
, smb2cli_ioctl_pipe_wait_done
, req
);
448 static void smb2cli_ioctl_pipe_wait_done(struct tevent_req
*subreq
)
450 struct tevent_req
*req
= tevent_req_callback_data(
451 subreq
, struct tevent_req
);
452 struct smb2cli_ioctl_pipe_wait_state
*state
= tevent_req_data(
453 req
, struct smb2cli_ioctl_pipe_wait_state
);
456 status
= smb2cli_ioctl_recv(subreq
, state
, NULL
, NULL
);
458 if (tevent_req_nterror(req
, status
)) {
462 tevent_req_done(req
);
466 NTSTATUS
smb2cli_ioctl_pipe_wait_recv(struct tevent_req
*req
)
470 if (tevent_req_is_nterror(req
, &status
)) {
471 tevent_req_received(req
);
475 tevent_req_received(req
);
479 NTSTATUS
smb2cli_ioctl_pipe_wait(struct smbXcli_conn
*conn
,
480 uint32_t timeout_msec
,
481 struct smbXcli_session
*session
,
482 struct smbXcli_tcon
*tcon
,
483 const char *pipe_name
,
484 uint64_t pipe_wait_timeout
)
486 TALLOC_CTX
*frame
= talloc_stackframe();
487 struct tevent_context
*ev
= NULL
;
488 struct tevent_req
*req
= NULL
;
489 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
491 if (smbXcli_conn_has_async_calls(conn
)) {
493 * Can't use sync call while an async call is in flight
495 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
499 ev
= samba_tevent_context_init(frame
);
504 req
= smb2cli_ioctl_pipe_wait_send(frame
, ev
, conn
, timeout_msec
,
506 pipe_name
, pipe_wait_timeout
);
510 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
514 status
= smb2cli_ioctl_pipe_wait_recv(req
);