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
,
32 struct files_struct
*in_fsp
,
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 struct files_struct
*in_fsp
= NULL
;
51 uint32_t in_input_offset
;
52 uint32_t in_input_length
;
53 DATA_BLOB in_input_buffer
;
54 uint32_t in_max_output_length
;
56 struct tevent_req
*subreq
;
58 status
= smbd_smb2_request_verify_sizes(req
, 0x39);
59 if (!NT_STATUS_IS_OK(status
)) {
60 return smbd_smb2_request_error(req
, status
);
62 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
64 in_ctl_code
= IVAL(inbody
, 0x04);
65 in_file_id_persistent
= BVAL(inbody
, 0x08);
66 in_file_id_volatile
= BVAL(inbody
, 0x10);
67 in_input_offset
= IVAL(inbody
, 0x18);
68 in_input_length
= IVAL(inbody
, 0x1C);
69 in_max_output_length
= IVAL(inbody
, 0x2C);
70 in_flags
= IVAL(inbody
, 0x30);
73 * InputOffset (4 bytes): The offset, in bytes, from the beginning of
74 * the SMB2 header to the input data buffer. If no input data is
75 * required for the FSCTL/IOCTL command being issued, the client SHOULD
76 * set this value to 0.<49>
77 * <49> If no input data is required for the FSCTL/IOCTL command being
78 * issued, Windows-based clients set this field to any value.
80 if ((in_input_length
> 0)
81 && (in_input_offset
!= (SMB2_HDR_BODY
+ req
->in
.vector
[i
+1].iov_len
))) {
82 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
85 if (in_input_length
> req
->in
.vector
[i
+2].iov_len
) {
86 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
89 in_input_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
90 in_input_buffer
.length
= in_input_length
;
92 if (in_file_id_persistent
== UINT64_MAX
&&
93 in_file_id_volatile
== UINT64_MAX
) {
94 /* without a handle */
96 in_fsp
= file_fsp_smb2(req
, in_file_id_persistent
,
99 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
103 subreq
= smbd_smb2_ioctl_send(req
,
104 req
->sconn
->smb2
.event_ctx
,
108 in_max_output_length
,
110 if (subreq
== NULL
) {
111 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
113 tevent_req_set_callback(subreq
, smbd_smb2_request_ioctl_done
, req
);
115 return smbd_smb2_request_pending_queue(req
, subreq
);
118 static void smbd_smb2_request_ioctl_done(struct tevent_req
*subreq
)
120 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
121 struct smbd_smb2_request
);
122 const uint8_t *inbody
;
123 int i
= req
->current_idx
;
127 uint32_t in_ctl_code
;
128 uint64_t in_file_id_persistent
;
129 uint64_t in_file_id_volatile
;
130 uint32_t out_input_offset
;
131 uint32_t out_output_offset
;
132 DATA_BLOB out_output_buffer
= data_blob_null
;
134 NTSTATUS error
; /* transport error */
136 status
= smbd_smb2_ioctl_recv(subreq
, req
, &out_output_buffer
);
138 DEBUG(10,("smbd_smb2_request_ioctl_done: smbd_smb2_ioctl_recv returned "
140 (unsigned int)out_output_buffer
.length
,
141 nt_errstr(status
) ));
144 if (NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
146 } else if (!NT_STATUS_IS_OK(status
)) {
147 error
= smbd_smb2_request_error(req
, status
);
148 if (!NT_STATUS_IS_OK(error
)) {
149 smbd_server_connection_terminate(req
->sconn
,
156 out_input_offset
= SMB2_HDR_BODY
+ 0x30;
157 out_output_offset
= SMB2_HDR_BODY
+ 0x30;
159 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
161 in_ctl_code
= IVAL(inbody
, 0x04);
162 in_file_id_persistent
= BVAL(inbody
, 0x08);
163 in_file_id_volatile
= BVAL(inbody
, 0x10);
165 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
167 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x30);
168 if (outbody
.data
== NULL
) {
169 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
170 if (!NT_STATUS_IS_OK(error
)) {
171 smbd_server_connection_terminate(req
->sconn
,
178 SSVAL(outbody
.data
, 0x00, 0x30 + 1); /* struct size */
179 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
180 SIVAL(outbody
.data
, 0x04,
181 in_ctl_code
); /* ctl code */
182 SBVAL(outbody
.data
, 0x08,
183 in_file_id_persistent
); /* file id (persistent) */
184 SBVAL(outbody
.data
, 0x10,
185 in_file_id_volatile
); /* file id (volatile) */
186 SIVAL(outbody
.data
, 0x18,
187 out_input_offset
); /* input offset */
188 SIVAL(outbody
.data
, 0x1C, 0); /* input count */
189 SIVAL(outbody
.data
, 0x20,
190 out_output_offset
); /* output offset */
191 SIVAL(outbody
.data
, 0x24,
192 out_output_buffer
.length
); /* output count */
193 SIVAL(outbody
.data
, 0x28, 0); /* flags */
194 SIVAL(outbody
.data
, 0x2C, 0); /* reserved */
197 * Note: Windows Vista and 2008 send back also the
198 * input from the request. But it was fixed in
201 outdyn
= out_output_buffer
;
203 error
= smbd_smb2_request_done_ex(req
, status
, outbody
, &outdyn
,
205 if (!NT_STATUS_IS_OK(error
)) {
206 smbd_server_connection_terminate(req
->sconn
,
212 struct smbd_smb2_ioctl_state
{
213 struct smbd_smb2_request
*smb2req
;
214 struct smb_request
*smbreq
;
217 uint32_t in_max_output
;
218 DATA_BLOB out_output
;
221 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req
*subreq
);
222 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req
*subreq
);
224 static struct tevent_req
*smbd_smb2_ioctl_send(TALLOC_CTX
*mem_ctx
,
225 struct tevent_context
*ev
,
226 struct smbd_smb2_request
*smb2req
,
227 struct files_struct
*fsp
,
228 uint32_t in_ctl_code
,
230 uint32_t in_max_output
,
233 struct tevent_req
*req
;
234 struct smbd_smb2_ioctl_state
*state
;
235 struct smb_request
*smbreq
;
236 struct tevent_req
*subreq
;
238 req
= tevent_req_create(mem_ctx
, &state
,
239 struct smbd_smb2_ioctl_state
);
243 state
->smb2req
= smb2req
;
244 state
->smbreq
= NULL
;
246 state
->in_input
= in_input
;
247 state
->in_max_output
= in_max_output
;
248 state
->out_output
= data_blob_null
;
250 DEBUG(10, ("smbd_smb2_ioctl: ctl_code[0x%08x] %s fnum[%d]\n",
251 (unsigned)in_ctl_code
,
252 fsp
? fsp_str_dbg(fsp
) : "<no handle>",
253 fsp
? fsp
->fnum
: -1));
255 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
256 if (tevent_req_nomem(smbreq
, req
)) {
257 return tevent_req_post(req
, ev
);
259 state
->smbreq
= smbreq
;
261 switch (in_ctl_code
) {
262 case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
264 uint16_t in_max_referral_level
;
265 DATA_BLOB in_file_name_buffer
;
266 char *in_file_name_string
;
267 size_t in_file_name_string_size
;
269 bool overflow
= false;
272 char *dfs_data
= NULL
;
274 if (!IS_IPC(smbreq
->conn
)) {
275 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
276 return tevent_req_post(req
, ev
);
279 if (!lp_host_msdfs()) {
280 tevent_req_nterror(req
, NT_STATUS_FS_DRIVER_REQUIRED
);
281 return tevent_req_post(req
, ev
);
284 if (in_input
.length
< (2 + 2)) {
285 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
286 return tevent_req_post(req
, ev
);
289 in_max_referral_level
= SVAL(in_input
.data
, 0);
290 in_file_name_buffer
.data
= in_input
.data
+ 2;
291 in_file_name_buffer
.length
= in_input
.length
- 2;
293 ok
= convert_string_talloc(state
, CH_UTF16
, CH_UNIX
,
294 in_file_name_buffer
.data
,
295 in_file_name_buffer
.length
,
296 &in_file_name_string
,
297 &in_file_name_string_size
, false);
299 tevent_req_nterror(req
, NT_STATUS_ILLEGAL_CHARACTER
);
300 return tevent_req_post(req
, ev
);
303 dfs_size
= setup_dfs_referral(smbreq
->conn
,
305 in_max_referral_level
,
308 tevent_req_nterror(req
, status
);
309 return tevent_req_post(req
, ev
);
312 if (dfs_size
> in_max_output
) {
314 * TODO: we need a testsuite for this
317 dfs_size
= in_max_output
;
320 state
->out_output
= data_blob_talloc(state
,
325 tevent_req_nomem(state
->out_output
.data
, req
)) {
326 return tevent_req_post(req
, ev
);
330 tevent_req_nterror(req
, STATUS_BUFFER_OVERFLOW
);
332 tevent_req_done(req
);
334 return tevent_req_post(req
, ev
);
336 case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
338 if (!IS_IPC(smbreq
->conn
)) {
339 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
340 return tevent_req_post(req
, ev
);
344 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
345 return tevent_req_post(req
, ev
);
348 if (!fsp_is_np(fsp
)) {
349 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
350 return tevent_req_post(req
, ev
);
353 DEBUG(10,("smbd_smb2_ioctl_send: np_write_send of size %u\n",
354 (unsigned int)in_input
.length
));
356 subreq
= np_write_send(state
, ev
,
357 fsp
->fake_file_handle
,
360 if (tevent_req_nomem(subreq
, req
)) {
361 return tevent_req_post(req
, ev
);
363 tevent_req_set_callback(subreq
,
364 smbd_smb2_ioctl_pipe_write_done
,
369 uint8_t *out_data
= NULL
;
370 uint32_t out_data_len
= 0;
374 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
375 return tevent_req_post(req
, ev
);
378 status
= smb_fsctl(fsp
,
387 state
->out_output
= data_blob_const(out_data
, out_data_len
);
388 if (NT_STATUS_IS_OK(status
)) {
389 tevent_req_done(req
);
390 return tevent_req_post(req
, ev
);
393 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
)) {
394 if (IS_IPC(smbreq
->conn
)) {
395 status
= NT_STATUS_FS_DRIVER_REQUIRED
;
397 status
= NT_STATUS_INVALID_DEVICE_REQUEST
;
401 tevent_req_nterror(req
, status
);
402 return tevent_req_post(req
, ev
);
406 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
407 return tevent_req_post(req
, ev
);
410 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req
*subreq
)
412 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
414 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
415 struct smbd_smb2_ioctl_state
);
417 ssize_t nwritten
= -1;
419 status
= np_write_recv(subreq
, &nwritten
);
421 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
422 (long int)nwritten
));
425 if (!NT_STATUS_IS_OK(status
)) {
426 tevent_req_nterror(req
, status
);
430 if (nwritten
!= state
->in_input
.length
) {
431 tevent_req_nterror(req
, NT_STATUS_PIPE_NOT_AVAILABLE
);
435 state
->out_output
= data_blob_talloc(state
, NULL
, state
->in_max_output
);
436 if (state
->in_max_output
> 0 &&
437 tevent_req_nomem(state
->out_output
.data
, req
)) {
441 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
443 (unsigned int)state
->out_output
.length
));
446 subreq
= np_read_send(state
->smbreq
->conn
,
447 state
->smb2req
->sconn
->smb2
.event_ctx
,
448 state
->fsp
->fake_file_handle
,
449 state
->out_output
.data
,
450 state
->out_output
.length
);
451 if (tevent_req_nomem(subreq
, req
)) {
454 tevent_req_set_callback(subreq
, smbd_smb2_ioctl_pipe_read_done
, req
);
457 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req
*subreq
)
459 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
461 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
462 struct smbd_smb2_ioctl_state
);
465 bool is_data_outstanding
= false;
467 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
469 DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
470 "is_data_outstanding = %d, status = %s\n",
472 (int)is_data_outstanding
,
473 nt_errstr(status
) ));
476 if (!NT_STATUS_IS_OK(status
)) {
477 tevent_req_nterror(req
, status
);
481 state
->out_output
.length
= nread
;
483 if (is_data_outstanding
) {
484 tevent_req_nterror(req
, STATUS_BUFFER_OVERFLOW
);
488 tevent_req_done(req
);
491 static NTSTATUS
smbd_smb2_ioctl_recv(struct tevent_req
*req
,
493 DATA_BLOB
*out_output
)
495 NTSTATUS status
= NT_STATUS_OK
;
496 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
497 struct smbd_smb2_ioctl_state
);
499 if (tevent_req_is_nterror(req
, &status
)) {
500 if (!NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
501 tevent_req_received(req
);
506 *out_output
= state
->out_output
;
507 talloc_steal(mem_ctx
, out_output
->data
);
509 tevent_req_received(req
);