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/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
25 static struct tevent_req
*smbd_smb2_ioctl_send(TALLOC_CTX
*mem_ctx
,
26 struct tevent_context
*ev
,
27 struct smbd_smb2_request
*smb2req
,
29 uint64_t in_file_id_volatile
,
31 uint32_t in_max_output
,
33 static NTSTATUS
smbd_smb2_ioctl_recv(struct tevent_req
*req
,
35 DATA_BLOB
*out_output
);
37 static void smbd_smb2_request_ioctl_done(struct tevent_req
*subreq
);
38 NTSTATUS
smbd_smb2_request_process_ioctl(struct smbd_smb2_request
*req
)
41 const uint8_t *inbody
;
42 int i
= req
->current_idx
;
43 size_t expected_body_size
= 0x39;
46 uint64_t in_file_id_persistent
;
47 uint64_t in_file_id_volatile
;
48 uint32_t in_input_offset
;
49 uint32_t in_input_length
;
50 DATA_BLOB in_input_buffer
;
51 uint32_t in_max_output_length
;
53 struct tevent_req
*subreq
;
55 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
56 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
57 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
60 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
62 body_size
= SVAL(inbody
, 0x00);
63 if (body_size
!= expected_body_size
) {
64 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
67 in_ctl_code
= IVAL(inbody
, 0x04);
68 in_file_id_persistent
= BVAL(inbody
, 0x08);
69 in_file_id_volatile
= BVAL(inbody
, 0x10);
70 in_input_offset
= IVAL(inbody
, 0x18);
71 in_input_length
= IVAL(inbody
, 0x1C);
72 in_max_output_length
= IVAL(inbody
, 0x2C);
73 in_flags
= IVAL(inbody
, 0x30);
75 if (in_input_offset
!= (SMB2_HDR_BODY
+ (body_size
& 0xFFFFFFFE))) {
76 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
79 if (in_input_length
> req
->in
.vector
[i
+2].iov_len
) {
80 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
83 in_input_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
84 in_input_buffer
.length
= in_input_length
;
86 if (req
->compat_chain_fsp
) {
88 } else if (in_file_id_persistent
== UINT64_MAX
&&
89 in_file_id_volatile
== UINT64_MAX
) {
90 /* without a handle */
91 } else if (in_file_id_persistent
!= 0) {
92 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
95 subreq
= smbd_smb2_ioctl_send(req
,
96 req
->conn
->smb2
.event_ctx
,
101 in_max_output_length
,
103 if (subreq
== NULL
) {
104 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
106 tevent_req_set_callback(subreq
, smbd_smb2_request_ioctl_done
, req
);
108 if (tevent_req_is_in_progress(subreq
)) {
109 return smbd_smb2_request_pending_queue(req
);
115 static void smbd_smb2_request_ioctl_done(struct tevent_req
*subreq
)
117 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
118 struct smbd_smb2_request
);
119 const uint8_t *inbody
;
120 int i
= req
->current_idx
;
124 uint32_t in_ctl_code
;
125 uint64_t in_file_id_persistent
;
126 uint64_t in_file_id_volatile
;
127 uint32_t out_input_offset
;
128 uint32_t out_output_offset
;
129 DATA_BLOB out_output_buffer
;
131 NTSTATUS error
; /* transport error */
133 status
= smbd_smb2_ioctl_recv(subreq
, req
, &out_output_buffer
);
135 if (NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
137 } else if (!NT_STATUS_IS_OK(status
)) {
138 error
= smbd_smb2_request_error(req
, status
);
139 if (!NT_STATUS_IS_OK(error
)) {
140 smbd_server_connection_terminate(req
->conn
,
147 out_input_offset
= SMB2_HDR_BODY
+ 0x30;
148 out_output_offset
= SMB2_HDR_BODY
+ 0x30;
150 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
152 in_ctl_code
= IVAL(inbody
, 0x04);
153 in_file_id_persistent
= BVAL(inbody
, 0x08);
154 in_file_id_volatile
= BVAL(inbody
, 0x10);
156 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
158 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x30);
159 if (outbody
.data
== NULL
) {
160 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
161 if (!NT_STATUS_IS_OK(error
)) {
162 smbd_server_connection_terminate(req
->conn
,
169 SSVAL(outbody
.data
, 0x00, 0x30 + 1); /* struct size */
170 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
171 SIVAL(outbody
.data
, 0x04,
172 in_ctl_code
); /* ctl code */
173 SBVAL(outbody
.data
, 0x08,
174 in_file_id_persistent
); /* file id (persistent) */
175 SBVAL(outbody
.data
, 0x10,
176 in_file_id_volatile
); /* file id (volatile) */
177 SIVAL(outbody
.data
, 0x18,
178 out_input_offset
); /* input offset */
179 SIVAL(outbody
.data
, 0x1C, 0); /* input count */
180 SIVAL(outbody
.data
, 0x20,
181 out_output_offset
); /* output offset */
182 SIVAL(outbody
.data
, 0x24,
183 out_output_buffer
.length
); /* output count */
184 SIVAL(outbody
.data
, 0x28, 0); /* flags */
185 SIVAL(outbody
.data
, 0x2C, 0); /* reserved */
188 * Note: Windows Vista and 2008 send back also the
189 * input from the request. But it was fixed in
192 outdyn
= out_output_buffer
;
194 error
= smbd_smb2_request_done_ex(req
, status
, outbody
, &outdyn
,
196 if (!NT_STATUS_IS_OK(error
)) {
197 smbd_server_connection_terminate(req
->conn
,
203 struct smbd_smb2_ioctl_state
{
204 struct smbd_smb2_request
*smb2req
;
205 struct smb_request
*smbreq
;
208 uint32_t in_max_output
;
209 DATA_BLOB out_output
;
212 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req
*subreq
);
213 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req
*subreq
);
215 static struct tevent_req
*smbd_smb2_ioctl_send(TALLOC_CTX
*mem_ctx
,
216 struct tevent_context
*ev
,
217 struct smbd_smb2_request
*smb2req
,
218 uint32_t in_ctl_code
,
219 uint64_t in_file_id_volatile
,
221 uint32_t in_max_output
,
224 struct tevent_req
*req
;
225 struct smbd_smb2_ioctl_state
*state
;
226 struct smb_request
*smbreq
;
227 files_struct
*fsp
= NULL
;
228 struct tevent_req
*subreq
;
230 req
= tevent_req_create(mem_ctx
, &state
,
231 struct smbd_smb2_ioctl_state
);
235 state
->smb2req
= smb2req
;
236 state
->smbreq
= NULL
;
238 state
->in_input
= in_input
;
239 state
->in_max_output
= in_max_output
;
240 state
->out_output
= data_blob_null
;
242 DEBUG(10,("smbd_smb2_ioctl: file_id[0x%016llX]\n",
243 (unsigned long long)in_file_id_volatile
));
245 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
246 if (tevent_req_nomem(smbreq
, req
)) {
247 return tevent_req_post(req
, ev
);
249 state
->smbreq
= smbreq
;
251 if (in_file_id_volatile
!= UINT64_MAX
) {
252 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
254 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
255 return tevent_req_post(req
, ev
);
257 if (smbreq
->conn
!= fsp
->conn
) {
258 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
259 return tevent_req_post(req
, ev
);
261 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
262 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
263 return tevent_req_post(req
, ev
);
268 switch (in_ctl_code
) {
269 case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
271 uint16_t in_max_referral_level
;
272 DATA_BLOB in_file_name_buffer
;
273 char *in_file_name_string
;
274 size_t in_file_name_string_size
;
276 bool overflow
= false;
279 char *dfs_data
= NULL
;
281 if (!IS_IPC(smbreq
->conn
)) {
282 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
283 return tevent_req_post(req
, ev
);
286 if (!lp_host_msdfs()) {
287 tevent_req_nterror(req
, NT_STATUS_FS_DRIVER_REQUIRED
);
288 return tevent_req_post(req
, ev
);
291 if (in_input
.length
< (2 + 2)) {
292 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
293 return tevent_req_post(req
, ev
);
296 in_max_referral_level
= SVAL(in_input
.data
, 0);
297 in_file_name_buffer
.data
= in_input
.data
+ 2;
298 in_file_name_buffer
.length
= in_input
.length
- 2;
300 ok
= convert_string_talloc(state
, CH_UTF16
, CH_UNIX
,
301 in_file_name_buffer
.data
,
302 in_file_name_buffer
.length
,
303 &in_file_name_string
,
304 &in_file_name_string_size
, false);
306 tevent_req_nterror(req
, NT_STATUS_ILLEGAL_CHARACTER
);
307 return tevent_req_post(req
, ev
);
310 dfs_size
= setup_dfs_referral(smbreq
->conn
,
312 in_max_referral_level
,
315 tevent_req_nterror(req
, status
);
316 return tevent_req_post(req
, ev
);
319 if (dfs_size
> in_max_output
) {
321 * TODO: we need a testsuite for this
324 dfs_size
= in_max_output
;
327 state
->out_output
= data_blob_talloc(state
,
332 tevent_req_nomem(state
->out_output
.data
, req
)) {
333 return tevent_req_post(req
, ev
);
337 tevent_req_nterror(req
, STATUS_BUFFER_OVERFLOW
);
339 tevent_req_done(req
);
341 return tevent_req_post(req
, ev
);
343 case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
345 if (!IS_IPC(smbreq
->conn
)) {
346 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
347 return tevent_req_post(req
, ev
);
351 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
352 return tevent_req_post(req
, ev
);
355 if (!fsp_is_np(fsp
)) {
356 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
357 return tevent_req_post(req
, ev
);
360 subreq
= np_write_send(state
, ev
,
361 fsp
->fake_file_handle
,
364 if (tevent_req_nomem(subreq
, req
)) {
365 return tevent_req_post(req
, ev
);
367 tevent_req_set_callback(subreq
,
368 smbd_smb2_ioctl_pipe_write_done
,
373 if (IS_IPC(smbreq
->conn
)) {
374 tevent_req_nterror(req
, NT_STATUS_FS_DRIVER_REQUIRED
);
375 return tevent_req_post(req
, ev
);
377 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
378 return tevent_req_post(req
, ev
);
381 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
382 return tevent_req_post(req
, ev
);
385 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req
*subreq
)
387 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
389 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
390 struct smbd_smb2_ioctl_state
);
392 ssize_t nwritten
= -1;
394 status
= np_write_recv(subreq
, &nwritten
);
396 if (!NT_STATUS_IS_OK(status
)) {
397 tevent_req_nterror(req
, status
);
401 if (nwritten
!= state
->in_input
.length
) {
402 tevent_req_nterror(req
, NT_STATUS_PIPE_NOT_AVAILABLE
);
406 state
->out_output
= data_blob_talloc(state
, NULL
, state
->in_max_output
);
407 if (state
->in_max_output
> 0 &&
408 tevent_req_nomem(state
->out_output
.data
, req
)) {
412 subreq
= np_read_send(state
->smbreq
->conn
,
413 state
->smb2req
->conn
->smb2
.event_ctx
,
414 state
->fsp
->fake_file_handle
,
415 state
->out_output
.data
,
416 state
->out_output
.length
);
417 if (tevent_req_nomem(subreq
, req
)) {
420 tevent_req_set_callback(subreq
, smbd_smb2_ioctl_pipe_read_done
, req
);
423 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req
*subreq
)
425 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
427 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
428 struct smbd_smb2_ioctl_state
);
431 bool is_data_outstanding
;
433 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
435 if (!NT_STATUS_IS_OK(status
)) {
436 tevent_req_nterror(req
, status
);
440 state
->out_output
.length
= nread
;
442 tevent_req_done(req
);
445 static NTSTATUS
smbd_smb2_ioctl_recv(struct tevent_req
*req
,
447 DATA_BLOB
*out_output
)
450 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
451 struct smbd_smb2_ioctl_state
);
453 if (tevent_req_is_nterror(req
, &status
)) {
454 if (!NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
455 tevent_req_received(req
);
460 *out_output
= state
->out_output
;
461 talloc_steal(mem_ctx
, out_output
->data
);
463 tevent_req_received(req
);