s3-includes: enable TYPESAFE_QSORT() in s3
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_ioctl.c
blob0041e5f7d000470145ad05cb8c6d236bb5e9aba5
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
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/>.
21 #include "includes.h"
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,
28 uint32_t in_ctl_code,
29 uint64_t in_file_id_volatile,
30 DATA_BLOB in_input,
31 uint32_t in_max_output,
32 uint32_t in_flags);
33 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
34 TALLOC_CTX *mem_ctx,
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)
40 const uint8_t *inhdr;
41 const uint8_t *inbody;
42 int i = req->current_idx;
43 size_t expected_body_size = 0x39;
44 size_t body_size;
45 uint32_t in_ctl_code;
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;
52 uint32_t in_flags;
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) {
87 /* skip check */
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->sconn->smb2.event_ctx,
97 req,
98 in_ctl_code,
99 in_file_id_volatile,
100 in_input_buffer,
101 in_max_output_length,
102 in_flags);
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;
117 uint8_t *outhdr;
118 DATA_BLOB outbody;
119 DATA_BLOB outdyn;
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;
126 NTSTATUS status;
127 NTSTATUS error; /* transport error */
129 status = smbd_smb2_ioctl_recv(subreq, req, &out_output_buffer);
130 TALLOC_FREE(subreq);
131 if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
132 /* also ok */
133 } else if (!NT_STATUS_IS_OK(status)) {
134 error = smbd_smb2_request_error(req, status);
135 if (!NT_STATUS_IS_OK(error)) {
136 smbd_server_connection_terminate(req->sconn,
137 nt_errstr(error));
138 return;
140 return;
143 out_input_offset = SMB2_HDR_BODY + 0x30;
144 out_output_offset = SMB2_HDR_BODY + 0x30;
146 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
148 in_ctl_code = IVAL(inbody, 0x04);
149 in_file_id_persistent = BVAL(inbody, 0x08);
150 in_file_id_volatile = BVAL(inbody, 0x10);
152 outhdr = (uint8_t *)req->out.vector[i].iov_base;
154 outbody = data_blob_talloc(req->out.vector, NULL, 0x30);
155 if (outbody.data == NULL) {
156 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
157 if (!NT_STATUS_IS_OK(error)) {
158 smbd_server_connection_terminate(req->sconn,
159 nt_errstr(error));
160 return;
162 return;
165 SSVAL(outbody.data, 0x00, 0x30 + 1); /* struct size */
166 SSVAL(outbody.data, 0x02, 0); /* reserved */
167 SIVAL(outbody.data, 0x04,
168 in_ctl_code); /* ctl code */
169 SBVAL(outbody.data, 0x08,
170 in_file_id_persistent); /* file id (persistent) */
171 SBVAL(outbody.data, 0x10,
172 in_file_id_volatile); /* file id (volatile) */
173 SIVAL(outbody.data, 0x18,
174 out_input_offset); /* input offset */
175 SIVAL(outbody.data, 0x1C, 0); /* input count */
176 SIVAL(outbody.data, 0x20,
177 out_output_offset); /* output offset */
178 SIVAL(outbody.data, 0x24,
179 out_output_buffer.length); /* output count */
180 SIVAL(outbody.data, 0x28, 0); /* flags */
181 SIVAL(outbody.data, 0x2C, 0); /* reserved */
184 * Note: Windows Vista and 2008 send back also the
185 * input from the request. But it was fixed in
186 * Windows 7.
188 outdyn = out_output_buffer;
190 error = smbd_smb2_request_done_ex(req, status, outbody, &outdyn,
191 __location__);
192 if (!NT_STATUS_IS_OK(error)) {
193 smbd_server_connection_terminate(req->sconn,
194 nt_errstr(error));
195 return;
199 struct smbd_smb2_ioctl_state {
200 struct smbd_smb2_request *smb2req;
201 struct smb_request *smbreq;
202 files_struct *fsp;
203 DATA_BLOB in_input;
204 uint32_t in_max_output;
205 DATA_BLOB out_output;
208 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq);
209 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq);
211 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
212 struct tevent_context *ev,
213 struct smbd_smb2_request *smb2req,
214 uint32_t in_ctl_code,
215 uint64_t in_file_id_volatile,
216 DATA_BLOB in_input,
217 uint32_t in_max_output,
218 uint32_t in_flags)
220 struct tevent_req *req;
221 struct smbd_smb2_ioctl_state *state;
222 struct smb_request *smbreq;
223 files_struct *fsp = NULL;
224 struct tevent_req *subreq;
226 req = tevent_req_create(mem_ctx, &state,
227 struct smbd_smb2_ioctl_state);
228 if (req == NULL) {
229 return NULL;
231 state->smb2req = smb2req;
232 state->smbreq = NULL;
233 state->fsp = NULL;
234 state->in_input = in_input;
235 state->in_max_output = in_max_output;
236 state->out_output = data_blob_null;
238 DEBUG(10,("smbd_smb2_ioctl: file_id[0x%016llX]\n",
239 (unsigned long long)in_file_id_volatile));
241 smbreq = smbd_smb2_fake_smb_request(smb2req);
242 if (tevent_req_nomem(smbreq, req)) {
243 return tevent_req_post(req, ev);
245 state->smbreq = smbreq;
247 if (in_file_id_volatile != UINT64_MAX) {
248 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
249 if (fsp == NULL) {
250 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
251 return tevent_req_post(req, ev);
253 if (smbreq->conn != fsp->conn) {
254 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
255 return tevent_req_post(req, ev);
257 if (smb2req->session->vuid != fsp->vuid) {
258 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
259 return tevent_req_post(req, ev);
261 state->fsp = fsp;
264 switch (in_ctl_code) {
265 case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
267 uint16_t in_max_referral_level;
268 DATA_BLOB in_file_name_buffer;
269 char *in_file_name_string;
270 size_t in_file_name_string_size;
271 bool ok;
272 bool overflow = false;
273 NTSTATUS status;
274 int dfs_size;
275 char *dfs_data = NULL;
277 if (!IS_IPC(smbreq->conn)) {
278 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
279 return tevent_req_post(req, ev);
282 if (!lp_host_msdfs()) {
283 tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
284 return tevent_req_post(req, ev);
287 if (in_input.length < (2 + 2)) {
288 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
289 return tevent_req_post(req, ev);
292 in_max_referral_level = SVAL(in_input.data, 0);
293 in_file_name_buffer.data = in_input.data + 2;
294 in_file_name_buffer.length = in_input.length - 2;
296 ok = convert_string_talloc(state, CH_UTF16, CH_UNIX,
297 in_file_name_buffer.data,
298 in_file_name_buffer.length,
299 &in_file_name_string,
300 &in_file_name_string_size, false);
301 if (!ok) {
302 tevent_req_nterror(req, NT_STATUS_ILLEGAL_CHARACTER);
303 return tevent_req_post(req, ev);
306 dfs_size = setup_dfs_referral(smbreq->conn,
307 in_file_name_string,
308 in_max_referral_level,
309 &dfs_data, &status);
310 if (dfs_size < 0) {
311 tevent_req_nterror(req, status);
312 return tevent_req_post(req, ev);
315 if (dfs_size > in_max_output) {
317 * TODO: we need a testsuite for this
319 overflow = true;
320 dfs_size = in_max_output;
323 state->out_output = data_blob_talloc(state,
324 (uint8_t *)dfs_data,
325 dfs_size);
326 SAFE_FREE(dfs_data);
327 if (dfs_size > 0 &&
328 tevent_req_nomem(state->out_output.data, req)) {
329 return tevent_req_post(req, ev);
332 if (overflow) {
333 tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
334 } else {
335 tevent_req_done(req);
337 return tevent_req_post(req, ev);
339 case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
341 if (!IS_IPC(smbreq->conn)) {
342 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
343 return tevent_req_post(req, ev);
346 if (fsp == NULL) {
347 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
348 return tevent_req_post(req, ev);
351 if (!fsp_is_np(fsp)) {
352 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
353 return tevent_req_post(req, ev);
356 subreq = np_write_send(state, ev,
357 fsp->fake_file_handle,
358 in_input.data,
359 in_input.length);
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,
365 req);
366 return req;
368 default:
369 if (IS_IPC(smbreq->conn)) {
370 tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
371 return tevent_req_post(req, ev);
373 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
374 return tevent_req_post(req, ev);
377 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
378 return tevent_req_post(req, ev);
381 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
383 struct tevent_req *req = tevent_req_callback_data(subreq,
384 struct tevent_req);
385 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
386 struct smbd_smb2_ioctl_state);
387 NTSTATUS status;
388 ssize_t nwritten = -1;
390 status = np_write_recv(subreq, &nwritten);
391 TALLOC_FREE(subreq);
392 if (!NT_STATUS_IS_OK(status)) {
393 tevent_req_nterror(req, status);
394 return;
397 if (nwritten != state->in_input.length) {
398 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
399 return;
402 state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
403 if (state->in_max_output > 0 &&
404 tevent_req_nomem(state->out_output.data, req)) {
405 return;
408 subreq = np_read_send(state->smbreq->conn,
409 state->smb2req->sconn->smb2.event_ctx,
410 state->fsp->fake_file_handle,
411 state->out_output.data,
412 state->out_output.length);
413 if (tevent_req_nomem(subreq, req)) {
414 return;
416 tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
419 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
421 struct tevent_req *req = tevent_req_callback_data(subreq,
422 struct tevent_req);
423 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
424 struct smbd_smb2_ioctl_state);
425 NTSTATUS status;
426 ssize_t nread;
427 bool is_data_outstanding;
429 status = np_read_recv(subreq, &nread, &is_data_outstanding);
430 TALLOC_FREE(subreq);
431 if (!NT_STATUS_IS_OK(status)) {
432 tevent_req_nterror(req, status);
433 return;
436 state->out_output.length = nread;
438 tevent_req_done(req);
441 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
442 TALLOC_CTX *mem_ctx,
443 DATA_BLOB *out_output)
445 NTSTATUS status;
446 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
447 struct smbd_smb2_ioctl_state);
449 if (tevent_req_is_nterror(req, &status)) {
450 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
451 tevent_req_received(req);
452 return status;
456 *out_output = state->out_output;
457 talloc_steal(mem_ctx, out_output->data);
459 tevent_req_received(req);
460 return status;