s3:utils: Fix 'Usage:' for 'net ads enctypes'
[samba4-gss.git] / source3 / smbd / smb2_ioctl.c
blobe31627126f4cc5147e2405e9965223e110af08c6
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/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/util/tevent_ntstatus.h"
26 #include "include/ntioctl.h"
27 #include "smb2_ioctl_private.h"
28 #include "librpc/gen_ndr/ioctl.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_SMB2
33 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
34 struct tevent_context *ev,
35 struct smbd_smb2_request *smb2req,
36 struct files_struct *in_fsp,
37 uint32_t in_ctl_code,
38 DATA_BLOB in_input,
39 uint32_t in_max_output,
40 uint32_t in_flags);
41 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
42 TALLOC_CTX *mem_ctx,
43 DATA_BLOB *out_output,
44 uint8_t *body_padding,
45 bool *disconnect);
47 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq);
48 NTSTATUS smbd_smb2_request_process_ioctl(struct smbd_smb2_request *req)
50 NTSTATUS status;
51 const uint8_t *inbody;
52 uint32_t min_buffer_offset;
53 uint32_t max_buffer_offset;
54 uint32_t min_output_offset;
55 uint32_t allowed_length_in;
56 uint32_t allowed_length_out;
57 uint32_t in_ctl_code;
58 uint64_t in_file_id_persistent;
59 uint64_t in_file_id_volatile;
60 struct files_struct *in_fsp = NULL;
61 uint32_t in_input_offset;
62 uint32_t in_input_length;
63 DATA_BLOB in_input_buffer = data_blob_null;
64 uint32_t in_max_input_length;
65 uint32_t in_output_offset;
66 uint32_t in_output_length;
67 DATA_BLOB in_output_buffer = data_blob_null;
68 uint32_t in_max_output_length;
69 uint32_t in_flags;
70 uint32_t data_length_in;
71 uint32_t data_length_out;
72 uint32_t data_length_tmp;
73 uint32_t data_length_max;
74 struct tevent_req *subreq;
76 status = smbd_smb2_request_verify_sizes(req, 0x39);
77 if (!NT_STATUS_IS_OK(status)) {
78 return smbd_smb2_request_error(req, status);
80 inbody = SMBD_SMB2_IN_BODY_PTR(req);
82 in_ctl_code = IVAL(inbody, 0x04);
83 in_file_id_persistent = BVAL(inbody, 0x08);
84 in_file_id_volatile = BVAL(inbody, 0x10);
85 in_input_offset = IVAL(inbody, 0x18);
86 in_input_length = IVAL(inbody, 0x1C);
87 in_max_input_length = IVAL(inbody, 0x20);
88 in_output_offset = IVAL(inbody, 0x24);
89 in_output_length = IVAL(inbody, 0x28);
90 in_max_output_length = IVAL(inbody, 0x2C);
91 in_flags = IVAL(inbody, 0x30);
93 min_buffer_offset = SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req);
94 max_buffer_offset = min_buffer_offset + SMBD_SMB2_IN_DYN_LEN(req);
95 min_output_offset = min_buffer_offset;
98 * InputOffset (4 bytes): The offset, in bytes, from the beginning of
99 * the SMB2 header to the input data buffer. If no input data is
100 * required for the FSCTL/IOCTL command being issued, the client SHOULD
101 * set this value to 0.<49>
102 * <49> If no input data is required for the FSCTL/IOCTL command being
103 * issued, Windows-based clients set this field to any value.
105 allowed_length_in = 0;
106 if ((in_input_offset > 0) && (in_input_length > 0)) {
107 uint32_t tmp_ofs;
109 if (in_input_offset < min_buffer_offset) {
110 return smbd_smb2_request_error(req,
111 NT_STATUS_INVALID_PARAMETER);
113 if (in_input_offset > max_buffer_offset) {
114 return smbd_smb2_request_error(req,
115 NT_STATUS_INVALID_PARAMETER);
117 allowed_length_in = max_buffer_offset - in_input_offset;
119 tmp_ofs = in_input_offset - min_buffer_offset;
120 in_input_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
121 in_input_buffer.data += tmp_ofs;
122 in_input_buffer.length = in_input_length;
123 min_output_offset += tmp_ofs;
124 min_output_offset += in_input_length;
127 if (in_input_length > allowed_length_in) {
128 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
131 allowed_length_out = 0;
132 if (in_output_offset > 0) {
133 if (in_output_offset < min_buffer_offset) {
134 return smbd_smb2_request_error(req,
135 NT_STATUS_INVALID_PARAMETER);
137 if (in_output_offset > max_buffer_offset) {
138 return smbd_smb2_request_error(req,
139 NT_STATUS_INVALID_PARAMETER);
141 allowed_length_out = max_buffer_offset - in_output_offset;
144 if (in_output_length > allowed_length_out) {
145 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
148 if (in_output_length > 0) {
149 uint32_t tmp_ofs;
151 if (in_output_offset < min_output_offset) {
152 return smbd_smb2_request_error(req,
153 NT_STATUS_INVALID_PARAMETER);
156 tmp_ofs = in_output_offset - min_buffer_offset;
157 in_output_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
158 in_output_buffer.data += tmp_ofs;
159 in_output_buffer.length = in_output_length;
163 * verify the credits and avoid overflows
164 * in_input_buffer.length and in_output_buffer.length
165 * are already verified.
167 data_length_in = in_input_buffer.length + in_output_buffer.length;
169 data_length_out = in_max_input_length;
170 data_length_tmp = UINT32_MAX - data_length_out;
171 if (data_length_tmp < in_max_output_length) {
172 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
174 data_length_out += in_max_output_length;
176 data_length_max = MAX(data_length_in, data_length_out);
178 status = smbd_smb2_request_verify_creditcharge(req, data_length_max);
179 if (!NT_STATUS_IS_OK(status)) {
180 return smbd_smb2_request_error(req, status);
184 * If the Flags field of the request is not SMB2_0_IOCTL_IS_FSCTL the
185 * server MUST fail the request with STATUS_NOT_SUPPORTED.
187 if (in_flags != SMB2_IOCTL_FLAG_IS_FSCTL) {
188 return smbd_smb2_request_error(req, NT_STATUS_NOT_SUPPORTED);
191 switch (in_ctl_code) {
192 case FSCTL_DFS_GET_REFERRALS:
193 case FSCTL_DFS_GET_REFERRALS_EX:
194 case FSCTL_PIPE_WAIT:
195 case FSCTL_VALIDATE_NEGOTIATE_INFO_224:
196 case FSCTL_VALIDATE_NEGOTIATE_INFO:
197 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
198 case FSCTL_SMBTORTURE_FORCE_UNACKED_TIMEOUT:
199 case FSCTL_SMBTORTURE_IOCTL_RESPONSE_BODY_PADDING8:
200 case FSCTL_SMBTORTURE_GLOBAL_READ_RESPONSE_BODY_PADDING8:
202 * Some SMB2 specific CtlCodes like FSCTL_DFS_GET_REFERRALS or
203 * FSCTL_PIPE_WAIT does not take a file handle.
205 * If FileId in the SMB2 Header of the request is not
206 * 0xFFFFFFFFFFFFFFFF, then the server MUST fail the request
207 * with STATUS_INVALID_PARAMETER.
209 if (in_file_id_persistent != UINT64_MAX ||
210 in_file_id_volatile != UINT64_MAX) {
211 return smbd_smb2_request_error(req,
212 NT_STATUS_INVALID_PARAMETER);
214 break;
215 default:
216 in_fsp = file_fsp_smb2(req, in_file_id_persistent,
217 in_file_id_volatile);
218 if (in_fsp == NULL) {
219 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
221 break;
224 subreq = smbd_smb2_ioctl_send(req, req->sconn->ev_ctx,
225 req, in_fsp,
226 in_ctl_code,
227 in_input_buffer,
228 in_max_output_length,
229 in_flags);
230 if (subreq == NULL) {
231 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
235 * If the FSCTL has gone async on a file handle, remember
236 * to add it to the list of async requests we need to wait
237 * for on file handle close.
239 if (in_fsp != NULL && tevent_req_is_in_progress(subreq)) {
240 bool ok;
242 ok = aio_add_req_to_fsp(in_fsp, subreq);
243 if (!ok) {
244 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
248 tevent_req_set_callback(subreq, smbd_smb2_request_ioctl_done, req);
250 return smbd_smb2_request_pending_queue(req, subreq, 1000);
254 * 3.3.4.4 Sending an Error Response
255 * An error code other than one of the following indicates a failure:
257 static bool smbd_smb2_ioctl_is_failure(uint32_t ctl_code, NTSTATUS status,
258 size_t data_size)
260 if (NT_STATUS_IS_OK(status)) {
261 return false;
265 * STATUS_BUFFER_OVERFLOW in a FSCTL_PIPE_TRANSCEIVE, FSCTL_PIPE_PEEK or
266 * FSCTL_DFS_GET_REFERRALS Response specified in section 2.2.32.<153>
268 if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)
269 && ((ctl_code == FSCTL_PIPE_TRANSCEIVE)
270 || (ctl_code == FSCTL_PIPE_PEEK)
271 || (ctl_code == FSCTL_DFS_GET_REFERRALS)
272 || (ctl_code == FSCTL_QUERY_ALLOCATED_RANGES))) {
273 return false;
277 * Any status other than STATUS_SUCCESS in a FSCTL_SRV_COPYCHUNK or
278 * FSCTL_SRV_COPYCHUNK_WRITE Response, when returning an
279 * SRV_COPYCHUNK_RESPONSE as described in section 2.2.32.1.
281 if (((ctl_code == FSCTL_SRV_COPYCHUNK)
282 || (ctl_code == FSCTL_SRV_COPYCHUNK_WRITE))
283 && (data_size == sizeof(struct srv_copychunk_rsp))) {
284 return false;
287 return true;
290 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq)
292 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
293 struct smbd_smb2_request);
294 const uint8_t *inbody;
295 DATA_BLOB outbody;
296 DATA_BLOB outdyn;
297 uint32_t in_ctl_code;
298 uint64_t in_file_id_persistent;
299 uint64_t in_file_id_volatile;
300 uint32_t in_max_output_length;
301 uint32_t out_input_offset;
302 uint32_t out_output_offset;
303 DATA_BLOB out_output_buffer = data_blob_null;
304 NTSTATUS status;
305 NTSTATUS error; /* transport error */
306 bool disconnect = false;
307 uint16_t body_size;
308 uint8_t body_padding = 0;
310 status = smbd_smb2_ioctl_recv(subreq, req,
311 &out_output_buffer,
312 &body_padding,
313 &disconnect);
315 DEBUG(10,("smbd_smb2_request_ioctl_done: smbd_smb2_ioctl_recv returned "
316 "%u status %s\n",
317 (unsigned int)out_output_buffer.length,
318 nt_errstr(status) ));
320 TALLOC_FREE(subreq);
321 if (disconnect) {
322 error = status;
323 smbd_server_connection_terminate(req->xconn,
324 nt_errstr(error));
325 return;
328 inbody = SMBD_SMB2_IN_BODY_PTR(req);
330 in_ctl_code = IVAL(inbody, 0x04);
331 in_file_id_persistent = BVAL(inbody, 0x08);
332 in_file_id_volatile = BVAL(inbody, 0x10);
333 in_max_output_length = IVAL(inbody, 0x2C);
335 if (out_output_buffer.length > in_max_output_length) {
337 * Return NT_STATUS_BUFFER_TOO_SMALL by
338 * default if the provided buffer doesn't
339 * fit.
341 * If someone wants truncated data
342 * together with STATUS_BUFFER_OVERFLOW
343 * it needs to be done explicitly in
344 * the backends. We currently do that
345 * in:
346 * - fsctl_dfs_get_refers()
347 * - smbd_smb2_ioctl_pipe_read_done()
348 * - fsctl_qar()
350 status = NT_STATUS_BUFFER_TOO_SMALL;
353 if (smbd_smb2_ioctl_is_failure(in_ctl_code, status,
354 out_output_buffer.length)) {
355 error = smbd_smb2_request_error(req, status);
356 if (!NT_STATUS_IS_OK(error)) {
357 smbd_server_connection_terminate(req->xconn,
358 nt_errstr(error));
359 return;
361 return;
365 * Only FSCTL_SMBTORTURE_IOCTL_RESPONSE_BODY_PADDING8
366 * sets body_padding to a value different from 0.
368 body_size = 0x30 + body_padding;
369 out_input_offset = SMB2_HDR_BODY + body_size;
370 out_output_offset = SMB2_HDR_BODY + body_size;
372 outbody = smbd_smb2_generate_outbody(req, body_size);
373 if (outbody.data == NULL) {
374 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
375 if (!NT_STATUS_IS_OK(error)) {
376 smbd_server_connection_terminate(req->xconn,
377 nt_errstr(error));
378 return;
380 return;
383 SSVAL(outbody.data, 0x00, 0x30 + 1); /* struct size */
384 SSVAL(outbody.data, 0x02, 0); /* reserved */
385 SIVAL(outbody.data, 0x04,
386 in_ctl_code); /* ctl code */
387 SBVAL(outbody.data, 0x08,
388 in_file_id_persistent); /* file id (persistent) */
389 SBVAL(outbody.data, 0x10,
390 in_file_id_volatile); /* file id (volatile) */
391 SIVAL(outbody.data, 0x18,
392 out_input_offset); /* input offset */
393 SIVAL(outbody.data, 0x1C, 0); /* input count */
394 SIVAL(outbody.data, 0x20,
395 out_output_offset); /* output offset */
396 SIVAL(outbody.data, 0x24,
397 out_output_buffer.length); /* output count */
398 SIVAL(outbody.data, 0x28, 0); /* flags */
399 SIVAL(outbody.data, 0x2C, 0); /* reserved */
400 if (body_padding != 0) {
401 memset(outbody.data + 0x30, 0, body_padding);
405 * Note: Windows Vista and 2008 send back also the
406 * input from the request. But it was fixed in
407 * Windows 7.
409 outdyn = out_output_buffer;
411 error = smbd_smb2_request_done_ex(req, status, outbody, &outdyn,
412 __location__);
413 if (!NT_STATUS_IS_OK(error)) {
414 smbd_server_connection_terminate(req->xconn,
415 nt_errstr(error));
416 return;
420 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
421 struct tevent_context *ev,
422 struct smbd_smb2_request *smb2req,
423 struct files_struct *fsp,
424 uint32_t in_ctl_code,
425 DATA_BLOB in_input,
426 uint32_t in_max_output,
427 uint32_t in_flags)
429 struct tevent_req *req;
430 struct smbd_smb2_ioctl_state *state;
431 struct smb_request *smbreq;
433 req = tevent_req_create(mem_ctx, &state,
434 struct smbd_smb2_ioctl_state);
435 if (req == NULL) {
436 return NULL;
438 state->smb2req = smb2req;
439 state->smbreq = NULL;
440 state->fsp = fsp;
441 state->in_input = in_input;
442 state->in_max_output = in_max_output;
443 state->out_output = data_blob_null;
445 DEBUG(10, ("smbd_smb2_ioctl: ctl_code[0x%08x] %s, %s\n",
446 (unsigned)in_ctl_code,
447 fsp ? fsp_str_dbg(fsp) : "<no handle>",
448 fsp_fnum_dbg(fsp)));
450 smbreq = smbd_smb2_fake_smb_request(smb2req, fsp);
451 if (tevent_req_nomem(smbreq, req)) {
452 return tevent_req_post(req, ev);
454 state->smbreq = smbreq;
456 switch (in_ctl_code & IOCTL_DEV_TYPE_MASK) {
457 case FSCTL_DFS:
458 return smb2_ioctl_dfs(in_ctl_code, ev, req, state);
459 case FSCTL_FILESYSTEM:
460 return smb2_ioctl_filesys(in_ctl_code, ev, req, state);
461 case FSCTL_NAMED_PIPE:
462 return smb2_ioctl_named_pipe(in_ctl_code, ev, req, state);
463 case FSCTL_NETWORK_FILESYSTEM:
464 return smb2_ioctl_network_fs(in_ctl_code, ev, req, state);
465 case FSCTL_SMBTORTURE:
466 return smb2_ioctl_smbtorture(in_ctl_code, ev, req, state);
467 default:
468 if (IS_IPC(smbreq->conn)) {
469 tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
470 } else {
471 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
474 return tevent_req_post(req, ev);
478 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
479 TALLOC_CTX *mem_ctx,
480 DATA_BLOB *out_output,
481 uint8_t *body_padding,
482 bool *disconnect)
484 NTSTATUS status = NT_STATUS_OK;
485 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
486 struct smbd_smb2_ioctl_state);
487 enum tevent_req_state req_state;
488 uint64_t err;
490 *body_padding = state->body_padding;
491 *disconnect = state->disconnect;
493 if ((tevent_req_is_error(req, &req_state, &err) == false)
494 || (req_state == TEVENT_REQ_USER_ERROR)) {
496 * Return output buffer to caller if the ioctl was successfully
497 * processed, even if a user error occurred. Some ioctls return
498 * data on failure.
500 *out_output = state->out_output;
501 talloc_steal(mem_ctx, out_output->data);
504 tevent_req_is_nterror(req, &status);
505 tevent_req_received(req);
506 return status;