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,
33 uint64_t in_file_id_volatile,
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 uint32_t in_input_offset;
51 uint32_t in_input_length;
52 DATA_BLOB in_input_buffer;
53 uint32_t in_max_output_length;
55 struct tevent_req *subreq;
57 status = smbd_smb2_request_verify_sizes(req, 0x39);
58 if (!NT_STATUS_IS_OK(status)) {
59 return smbd_smb2_request_error(req, status);
61 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
63 in_ctl_code = IVAL(inbody, 0x04);
64 in_file_id_persistent = BVAL(inbody, 0x08);
65 in_file_id_volatile = BVAL(inbody, 0x10);
66 in_input_offset = IVAL(inbody, 0x18);
67 in_input_length = IVAL(inbody, 0x1C);
68 in_max_output_length = IVAL(inbody, 0x2C);
69 in_flags = IVAL(inbody, 0x30);
71 if (in_input_offset != (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
72 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
75 if (in_input_length > req->in.vector[i+2].iov_len) {
76 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
79 in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
80 in_input_buffer.length = in_input_length;
82 if (req->compat_chain_fsp) {
84 } else if (in_file_id_persistent == UINT64_MAX &&
85 in_file_id_volatile == UINT64_MAX) {
86 /* without a handle */
87 } else if (in_file_id_persistent != in_file_id_volatile) {
88 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
91 subreq = smbd_smb2_ioctl_send(req,
92 req->sconn->smb2.event_ctx,
100 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
102 tevent_req_set_callback(subreq, smbd_smb2_request_ioctl_done, req);
104 return smbd_smb2_request_pending_queue(req, subreq);
107 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq)
109 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
110 struct smbd_smb2_request);
111 const uint8_t *inbody;
112 int i = req->current_idx;
116 uint32_t in_ctl_code;
117 uint64_t in_file_id_persistent;
118 uint64_t in_file_id_volatile;
119 uint32_t out_input_offset;
120 uint32_t out_output_offset;
121 DATA_BLOB out_output_buffer = data_blob_null;
123 NTSTATUS error; /* transport error */
125 status = smbd_smb2_ioctl_recv(subreq, req, &out_output_buffer);
127 DEBUG(10,("smbd_smb2_request_ioctl_done: smbd_smb2_ioctl_recv returned "
129 (unsigned int)out_output_buffer.length,
130 nt_errstr(status) ));
133 if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
135 } else if (!NT_STATUS_IS_OK(status)) {
136 error = smbd_smb2_request_error(req, status);
137 if (!NT_STATUS_IS_OK(error)) {
138 smbd_server_connection_terminate(req->sconn,
145 out_input_offset = SMB2_HDR_BODY + 0x30;
146 out_output_offset = SMB2_HDR_BODY + 0x30;
148 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
150 in_ctl_code = IVAL(inbody, 0x04);
151 in_file_id_persistent = BVAL(inbody, 0x08);
152 in_file_id_volatile = BVAL(inbody, 0x10);
154 outhdr = (uint8_t *)req->out.vector[i].iov_base;
156 outbody = data_blob_talloc(req->out.vector, NULL, 0x30);
157 if (outbody.data == NULL) {
158 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
159 if (!NT_STATUS_IS_OK(error)) {
160 smbd_server_connection_terminate(req->sconn,
167 SSVAL(outbody.data, 0x00, 0x30 + 1); /* struct size */
168 SSVAL(outbody.data, 0x02, 0); /* reserved */
169 SIVAL(outbody.data, 0x04,
170 in_ctl_code); /* ctl code */
171 SBVAL(outbody.data, 0x08,
172 in_file_id_persistent); /* file id (persistent) */
173 SBVAL(outbody.data, 0x10,
174 in_file_id_volatile); /* file id (volatile) */
175 SIVAL(outbody.data, 0x18,
176 out_input_offset); /* input offset */
177 SIVAL(outbody.data, 0x1C, 0); /* input count */
178 SIVAL(outbody.data, 0x20,
179 out_output_offset); /* output offset */
180 SIVAL(outbody.data, 0x24,
181 out_output_buffer.length); /* output count */
182 SIVAL(outbody.data, 0x28, 0); /* flags */
183 SIVAL(outbody.data, 0x2C, 0); /* reserved */
186 * Note: Windows Vista and 2008 send back also the
187 * input from the request. But it was fixed in
190 outdyn = out_output_buffer;
192 error = smbd_smb2_request_done_ex(req, status, outbody, &outdyn,
194 if (!NT_STATUS_IS_OK(error)) {
195 smbd_server_connection_terminate(req->sconn,
201 struct smbd_smb2_ioctl_state {
202 struct smbd_smb2_request *smb2req;
203 struct smb_request *smbreq;
206 uint32_t in_max_output;
207 DATA_BLOB out_output;
210 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq);
211 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq);
213 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
214 struct tevent_context *ev,
215 struct smbd_smb2_request *smb2req,
216 uint32_t in_ctl_code,
217 uint64_t in_file_id_volatile,
219 uint32_t in_max_output,
222 struct tevent_req *req;
223 struct smbd_smb2_ioctl_state *state;
224 struct smb_request *smbreq;
225 files_struct *fsp = NULL;
226 struct tevent_req *subreq;
228 req = tevent_req_create(mem_ctx, &state,
229 struct smbd_smb2_ioctl_state);
233 state->smb2req = smb2req;
234 state->smbreq = NULL;
236 state->in_input = in_input;
237 state->in_max_output = in_max_output;
238 state->out_output = data_blob_null;
240 DEBUG(10, ("smbd_smb2_ioctl: ctl_code[0x%08x] file_id[0x%016llX]\n",
241 (unsigned)in_ctl_code,
242 (unsigned long long)in_file_id_volatile));
244 smbreq = smbd_smb2_fake_smb_request(smb2req);
245 if (tevent_req_nomem(smbreq, req)) {
246 return tevent_req_post(req, ev);
248 state->smbreq = smbreq;
250 if (in_file_id_volatile != UINT64_MAX) {
251 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
253 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
254 return tevent_req_post(req, ev);
256 if (smbreq->conn != fsp->conn) {
257 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
258 return tevent_req_post(req, ev);
260 if (smb2req->session->vuid != fsp->vuid) {
261 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
262 return tevent_req_post(req, ev);
267 switch (in_ctl_code) {
268 case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
270 uint16_t in_max_referral_level;
271 DATA_BLOB in_file_name_buffer;
272 char *in_file_name_string;
273 size_t in_file_name_string_size;
275 bool overflow = false;
278 char *dfs_data = NULL;
280 if (!IS_IPC(smbreq->conn)) {
281 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
282 return tevent_req_post(req, ev);
285 if (!lp_host_msdfs()) {
286 tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
287 return tevent_req_post(req, ev);
290 if (in_input.length < (2 + 2)) {
291 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
292 return tevent_req_post(req, ev);
295 in_max_referral_level = SVAL(in_input.data, 0);
296 in_file_name_buffer.data = in_input.data + 2;
297 in_file_name_buffer.length = in_input.length - 2;
299 ok = convert_string_talloc(state, CH_UTF16, CH_UNIX,
300 in_file_name_buffer.data,
301 in_file_name_buffer.length,
302 &in_file_name_string,
303 &in_file_name_string_size);
305 tevent_req_nterror(req, NT_STATUS_ILLEGAL_CHARACTER);
306 return tevent_req_post(req, ev);
309 dfs_size = setup_dfs_referral(smbreq->conn,
311 in_max_referral_level,
314 tevent_req_nterror(req, status);
315 return tevent_req_post(req, ev);
318 if (dfs_size > in_max_output) {
320 * TODO: we need a testsuite for this
323 dfs_size = in_max_output;
326 state->out_output = data_blob_talloc(state,
331 tevent_req_nomem(state->out_output.data, req)) {
332 return tevent_req_post(req, ev);
336 tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
338 tevent_req_done(req);
340 return tevent_req_post(req, ev);
342 case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
344 if (!IS_IPC(smbreq->conn)) {
345 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
346 return tevent_req_post(req, ev);
350 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
351 return tevent_req_post(req, ev);
354 if (!fsp_is_np(fsp)) {
355 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
356 return tevent_req_post(req, ev);
359 DEBUG(10,("smbd_smb2_ioctl_send: np_write_send of size %u\n",
360 (unsigned int)in_input.length ));
362 subreq = np_write_send(state, ev,
363 fsp->fake_file_handle,
366 if (tevent_req_nomem(subreq, req)) {
367 return tevent_req_post(req, ev);
369 tevent_req_set_callback(subreq,
370 smbd_smb2_ioctl_pipe_write_done,
374 case 0x00144064: /* FSCTL_SRV_ENUMERATE_SNAPSHOTS */
377 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
378 * and return their volume names. If max_data_count is 16, then it is just
379 * asking for the number of volumes and length of the combined names.
381 * pdata is the data allocated by our caller, but that uses
382 * total_data_count (which is 0 in our case) rather than max_data_count.
383 * Allocate the correct amount and return the pointer to let
384 * it be deallocated when we return.
386 struct shadow_copy_data *shadow_data = NULL;
388 uint32_t labels_data_count = 0;
395 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
396 return tevent_req_post(req, ev);
399 if (in_max_output < 16) {
400 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
401 "in_max_output(%u) < 16 is invalid!\n",
403 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
404 return tevent_req_post(req, ev);
407 if (in_max_output > 16) {
411 shadow_data = talloc_zero(talloc_tos(),
412 struct shadow_copy_data);
413 if (tevent_req_nomem(shadow_data, req)) {
414 DEBUG(0,("TALLOC_ZERO() failed!\n"));
415 return tevent_req_post(req, ev);
419 * Call the VFS routine to actually do the work.
421 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)
423 if (errno == ENOSYS) {
424 DEBUG(5, ("FSCTL_GET_SHADOW_COPY_DATA: "
425 "connectpath %s, not supported.\n",
426 smbreq->conn->connectpath));
427 status = NT_STATUS_NOT_SUPPORTED;
429 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
430 "connectpath %s, failed.\n",
431 smbreq->conn->connectpath));
432 status = map_nt_error_from_unix(errno);
434 TALLOC_FREE(shadow_data);
435 tevent_req_nterror(req, status);
436 return tevent_req_post(req, ev);
440 (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))
444 data_count = 12+labels_data_count+4;
449 if (labels && (in_max_output < data_count)) {
450 DEBUG(0, ("FSCTL_GET_SHADOW_COPY_DATA: "
451 "in_max_output(%u) too small (%u) bytes "
452 "needed!\n", in_max_output, data_count));
453 TALLOC_FREE(shadow_data);
454 tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
455 return tevent_req_post(req, ev);
458 state->out_output = data_blob_talloc(state, NULL, data_count);
459 if (tevent_req_nomem(state->out_output.data, req)) {
460 return tevent_req_post(req, ev);
463 pdata = (char *)state->out_output.data;
465 /* num_volumes 4 bytes */
466 SIVAL(pdata, 0, shadow_data->num_volumes);
469 /* num_labels 4 bytes */
470 SIVAL(pdata, 4, shadow_data->num_volumes);
473 /* needed_data_count 4 bytes */
474 SIVAL(pdata, 8, labels_data_count+4);
478 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for "
480 shadow_data->num_volumes, fsp_str_dbg(fsp)));
481 if (labels && shadow_data->labels) {
482 for (i=0; i<shadow_data->num_volumes; i++) {
483 srvstr_push(pdata, smbreq->flags2,
484 pdata, shadow_data->labels[i],
485 2*sizeof(SHADOW_COPY_LABEL),
486 STR_UNICODE|STR_TERMINATE);
487 pdata += 2*sizeof(SHADOW_COPY_LABEL);
488 DEBUGADD(10, ("Label[%u]: '%s'\n", i,
489 shadow_data->labels[i]));
493 TALLOC_FREE(shadow_data);
495 tevent_req_done(req);
496 return tevent_req_post(req, ev);
500 if (IS_IPC(smbreq->conn)) {
501 tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
502 return tevent_req_post(req, ev);
504 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
505 return tevent_req_post(req, ev);
508 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
509 return tevent_req_post(req, ev);
512 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
514 struct tevent_req *req = tevent_req_callback_data(subreq,
516 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
517 struct smbd_smb2_ioctl_state);
519 ssize_t nwritten = -1;
521 status = np_write_recv(subreq, &nwritten);
523 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
524 (long int)nwritten ));
527 if (!NT_STATUS_IS_OK(status)) {
528 tevent_req_nterror(req, status);
532 if (nwritten != state->in_input.length) {
533 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
537 state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
538 if (state->in_max_output > 0 &&
539 tevent_req_nomem(state->out_output.data, req)) {
543 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
545 (unsigned int)state->out_output.length ));
548 subreq = np_read_send(state->smbreq->conn,
549 state->smb2req->sconn->smb2.event_ctx,
550 state->fsp->fake_file_handle,
551 state->out_output.data,
552 state->out_output.length);
553 if (tevent_req_nomem(subreq, req)) {
556 tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
559 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
561 struct tevent_req *req = tevent_req_callback_data(subreq,
563 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
564 struct smbd_smb2_ioctl_state);
567 bool is_data_outstanding = false;
569 status = np_read_recv(subreq, &nread, &is_data_outstanding);
571 DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
572 "is_data_outstanding = %d, status = %s\n",
574 (int)is_data_outstanding,
575 nt_errstr(status) ));
578 if (!NT_STATUS_IS_OK(status)) {
579 tevent_req_nterror(req, status);
583 state->out_output.length = nread;
585 if (is_data_outstanding) {
586 tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
590 tevent_req_done(req);
593 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
595 DATA_BLOB *out_output)
597 NTSTATUS status = NT_STATUS_OK;
598 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
599 struct smbd_smb2_ioctl_state);
601 if (tevent_req_is_nterror(req, &status)) {
602 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
603 tevent_req_received(req);
608 *out_output = state->out_output;
609 talloc_steal(mem_ctx, out_output->data);
611 tevent_req_received(req);