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 "../libcli/smb/smb_common.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
!= in_file_id_volatile
) {
92 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
95 subreq
= smbd_smb2_ioctl_send(req
,
96 req
->sconn
->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 return smbd_smb2_request_pending_queue(req
, subreq
);
111 static void smbd_smb2_request_ioctl_done(struct tevent_req
*subreq
)
113 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
114 struct smbd_smb2_request
);
115 const uint8_t *inbody
;
116 int i
= req
->current_idx
;
120 uint32_t in_ctl_code
;
121 uint64_t in_file_id_persistent
;
122 uint64_t in_file_id_volatile
;
123 uint32_t out_input_offset
;
124 uint32_t out_output_offset
;
125 DATA_BLOB out_output_buffer
= data_blob_null
;
127 NTSTATUS error
; /* transport error */
129 status
= smbd_smb2_ioctl_recv(subreq
, req
, &out_output_buffer
);
131 DEBUG(10,("smbd_smb2_request_ioctl_done: smbd_smb2_ioctl_recv returned "
133 (unsigned int)out_output_buffer
.length
,
134 nt_errstr(status
) ));
137 if (NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
139 } else if (!NT_STATUS_IS_OK(status
)) {
140 error
= smbd_smb2_request_error(req
, status
);
141 if (!NT_STATUS_IS_OK(error
)) {
142 smbd_server_connection_terminate(req
->sconn
,
149 out_input_offset
= SMB2_HDR_BODY
+ 0x30;
150 out_output_offset
= SMB2_HDR_BODY
+ 0x30;
152 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
154 in_ctl_code
= IVAL(inbody
, 0x04);
155 in_file_id_persistent
= BVAL(inbody
, 0x08);
156 in_file_id_volatile
= BVAL(inbody
, 0x10);
158 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
160 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x30);
161 if (outbody
.data
== NULL
) {
162 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
163 if (!NT_STATUS_IS_OK(error
)) {
164 smbd_server_connection_terminate(req
->sconn
,
171 SSVAL(outbody
.data
, 0x00, 0x30 + 1); /* struct size */
172 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
173 SIVAL(outbody
.data
, 0x04,
174 in_ctl_code
); /* ctl code */
175 SBVAL(outbody
.data
, 0x08,
176 in_file_id_persistent
); /* file id (persistent) */
177 SBVAL(outbody
.data
, 0x10,
178 in_file_id_volatile
); /* file id (volatile) */
179 SIVAL(outbody
.data
, 0x18,
180 out_input_offset
); /* input offset */
181 SIVAL(outbody
.data
, 0x1C, 0); /* input count */
182 SIVAL(outbody
.data
, 0x20,
183 out_output_offset
); /* output offset */
184 SIVAL(outbody
.data
, 0x24,
185 out_output_buffer
.length
); /* output count */
186 SIVAL(outbody
.data
, 0x28, 0); /* flags */
187 SIVAL(outbody
.data
, 0x2C, 0); /* reserved */
190 * Note: Windows Vista and 2008 send back also the
191 * input from the request. But it was fixed in
194 outdyn
= out_output_buffer
;
196 error
= smbd_smb2_request_done_ex(req
, status
, outbody
, &outdyn
,
198 if (!NT_STATUS_IS_OK(error
)) {
199 smbd_server_connection_terminate(req
->sconn
,
205 struct smbd_smb2_ioctl_state
{
206 struct smbd_smb2_request
*smb2req
;
207 struct smb_request
*smbreq
;
210 uint32_t in_max_output
;
211 DATA_BLOB out_output
;
214 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req
*subreq
);
215 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req
*subreq
);
217 static struct tevent_req
*smbd_smb2_ioctl_send(TALLOC_CTX
*mem_ctx
,
218 struct tevent_context
*ev
,
219 struct smbd_smb2_request
*smb2req
,
220 uint32_t in_ctl_code
,
221 uint64_t in_file_id_volatile
,
223 uint32_t in_max_output
,
226 struct tevent_req
*req
;
227 struct smbd_smb2_ioctl_state
*state
;
228 struct smb_request
*smbreq
;
229 files_struct
*fsp
= NULL
;
230 struct tevent_req
*subreq
;
232 req
= tevent_req_create(mem_ctx
, &state
,
233 struct smbd_smb2_ioctl_state
);
237 state
->smb2req
= smb2req
;
238 state
->smbreq
= NULL
;
240 state
->in_input
= in_input
;
241 state
->in_max_output
= in_max_output
;
242 state
->out_output
= data_blob_null
;
244 DEBUG(10,("smbd_smb2_ioctl: file_id[0x%016llX]\n",
245 (unsigned long long)in_file_id_volatile
));
247 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
248 if (tevent_req_nomem(smbreq
, req
)) {
249 return tevent_req_post(req
, ev
);
251 state
->smbreq
= smbreq
;
253 if (in_file_id_volatile
!= UINT64_MAX
) {
254 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
256 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
257 return tevent_req_post(req
, ev
);
259 if (smbreq
->conn
!= fsp
->conn
) {
260 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
261 return tevent_req_post(req
, ev
);
263 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
264 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
265 return tevent_req_post(req
, ev
);
270 switch (in_ctl_code
) {
271 case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
273 uint16_t in_max_referral_level
;
274 DATA_BLOB in_file_name_buffer
;
275 char *in_file_name_string
;
276 size_t in_file_name_string_size
;
278 bool overflow
= false;
281 char *dfs_data
= NULL
;
283 if (!IS_IPC(smbreq
->conn
)) {
284 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
285 return tevent_req_post(req
, ev
);
288 if (!lp_host_msdfs()) {
289 tevent_req_nterror(req
, NT_STATUS_FS_DRIVER_REQUIRED
);
290 return tevent_req_post(req
, ev
);
293 if (in_input
.length
< (2 + 2)) {
294 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
295 return tevent_req_post(req
, ev
);
298 in_max_referral_level
= SVAL(in_input
.data
, 0);
299 in_file_name_buffer
.data
= in_input
.data
+ 2;
300 in_file_name_buffer
.length
= in_input
.length
- 2;
302 ok
= convert_string_talloc(state
, CH_UTF16
, CH_UNIX
,
303 in_file_name_buffer
.data
,
304 in_file_name_buffer
.length
,
305 &in_file_name_string
,
306 &in_file_name_string_size
, false);
308 tevent_req_nterror(req
, NT_STATUS_ILLEGAL_CHARACTER
);
309 return tevent_req_post(req
, ev
);
312 dfs_size
= setup_dfs_referral(smbreq
->conn
,
314 in_max_referral_level
,
317 tevent_req_nterror(req
, status
);
318 return tevent_req_post(req
, ev
);
321 if (dfs_size
> in_max_output
) {
323 * TODO: we need a testsuite for this
326 dfs_size
= in_max_output
;
329 state
->out_output
= data_blob_talloc(state
,
334 tevent_req_nomem(state
->out_output
.data
, req
)) {
335 return tevent_req_post(req
, ev
);
339 tevent_req_nterror(req
, STATUS_BUFFER_OVERFLOW
);
341 tevent_req_done(req
);
343 return tevent_req_post(req
, ev
);
345 case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
347 if (!IS_IPC(smbreq
->conn
)) {
348 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
349 return tevent_req_post(req
, ev
);
353 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
354 return tevent_req_post(req
, ev
);
357 if (!fsp_is_np(fsp
)) {
358 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
359 return tevent_req_post(req
, ev
);
362 DEBUG(10,("smbd_smb2_ioctl_send: np_write_send of size %u\n",
363 (unsigned int)in_input
.length
));
365 subreq
= np_write_send(state
, ev
,
366 fsp
->fake_file_handle
,
369 if (tevent_req_nomem(subreq
, req
)) {
370 return tevent_req_post(req
, ev
);
372 tevent_req_set_callback(subreq
,
373 smbd_smb2_ioctl_pipe_write_done
,
378 if (IS_IPC(smbreq
->conn
)) {
379 tevent_req_nterror(req
, NT_STATUS_FS_DRIVER_REQUIRED
);
380 return tevent_req_post(req
, ev
);
382 tevent_req_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
383 return tevent_req_post(req
, ev
);
386 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
387 return tevent_req_post(req
, ev
);
390 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req
*subreq
)
392 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
394 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
395 struct smbd_smb2_ioctl_state
);
397 ssize_t nwritten
= -1;
399 status
= np_write_recv(subreq
, &nwritten
);
401 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
402 (long int)nwritten
));
405 if (!NT_STATUS_IS_OK(status
)) {
406 tevent_req_nterror(req
, status
);
410 if (nwritten
!= state
->in_input
.length
) {
411 tevent_req_nterror(req
, NT_STATUS_PIPE_NOT_AVAILABLE
);
415 state
->out_output
= data_blob_talloc(state
, NULL
, state
->in_max_output
);
416 if (state
->in_max_output
> 0 &&
417 tevent_req_nomem(state
->out_output
.data
, req
)) {
421 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
423 (unsigned int)state
->out_output
.length
));
426 subreq
= np_read_send(state
->smbreq
->conn
,
427 state
->smb2req
->sconn
->smb2
.event_ctx
,
428 state
->fsp
->fake_file_handle
,
429 state
->out_output
.data
,
430 state
->out_output
.length
);
431 if (tevent_req_nomem(subreq
, req
)) {
434 tevent_req_set_callback(subreq
, smbd_smb2_ioctl_pipe_read_done
, req
);
437 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req
*subreq
)
439 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
441 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
442 struct smbd_smb2_ioctl_state
);
445 bool is_data_outstanding
= false;
447 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
449 DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
450 "is_data_outstanding = %d, status = %s\n",
452 (int)is_data_outstanding
,
453 nt_errstr(status
) ));
456 if (!NT_STATUS_IS_OK(status
)) {
457 tevent_req_nterror(req
, status
);
461 state
->out_output
.length
= nread
;
463 tevent_req_done(req
);
466 static NTSTATUS
smbd_smb2_ioctl_recv(struct tevent_req
*req
,
468 DATA_BLOB
*out_output
)
470 NTSTATUS status
= NT_STATUS_OK
;
471 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
472 struct smbd_smb2_ioctl_state
);
474 if (tevent_req_is_nterror(req
, &status
)) {
475 if (!NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
476 tevent_req_received(req
);
481 *out_output
= state
->out_output
;
482 talloc_steal(mem_ctx
, out_output
->data
);
484 tevent_req_received(req
);