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: file_id[0x%016llX]\n",
241 (unsigned long long)in_file_id_volatile));
243 smbreq = smbd_smb2_fake_smb_request(smb2req);
244 if (tevent_req_nomem(smbreq, req)) {
245 return tevent_req_post(req, ev);
247 state->smbreq = smbreq;
249 if (in_file_id_volatile != UINT64_MAX) {
250 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
252 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
253 return tevent_req_post(req, ev);
255 if (smbreq->conn != fsp->conn) {
256 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
257 return tevent_req_post(req, ev);
259 if (smb2req->session->vuid != fsp->vuid) {
260 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
261 return tevent_req_post(req, ev);
266 switch (in_ctl_code) {
267 case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
269 uint16_t in_max_referral_level;
270 DATA_BLOB in_file_name_buffer;
271 char *in_file_name_string;
272 size_t in_file_name_string_size;
274 bool overflow = false;
277 char *dfs_data = NULL;
279 if (!IS_IPC(smbreq->conn)) {
280 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
281 return tevent_req_post(req, ev);
284 if (!lp_host_msdfs()) {
285 tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
286 return tevent_req_post(req, ev);
289 if (in_input.length < (2 + 2)) {
290 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
291 return tevent_req_post(req, ev);
294 in_max_referral_level = SVAL(in_input.data, 0);
295 in_file_name_buffer.data = in_input.data + 2;
296 in_file_name_buffer.length = in_input.length - 2;
298 ok = convert_string_talloc(state, CH_UTF16, CH_UNIX,
299 in_file_name_buffer.data,
300 in_file_name_buffer.length,
301 &in_file_name_string,
302 &in_file_name_string_size, false);
304 tevent_req_nterror(req, NT_STATUS_ILLEGAL_CHARACTER);
305 return tevent_req_post(req, ev);
308 dfs_size = setup_dfs_referral(smbreq->conn,
310 in_max_referral_level,
313 tevent_req_nterror(req, status);
314 return tevent_req_post(req, ev);
317 if (dfs_size > in_max_output) {
319 * TODO: we need a testsuite for this
322 dfs_size = in_max_output;
325 state->out_output = data_blob_talloc(state,
330 tevent_req_nomem(state->out_output.data, req)) {
331 return tevent_req_post(req, ev);
335 tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
337 tevent_req_done(req);
339 return tevent_req_post(req, ev);
341 case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
343 if (!IS_IPC(smbreq->conn)) {
344 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
345 return tevent_req_post(req, ev);
349 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
350 return tevent_req_post(req, ev);
353 if (!fsp_is_np(fsp)) {
354 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
355 return tevent_req_post(req, ev);
358 DEBUG(10,("smbd_smb2_ioctl_send: np_write_send of size %u\n",
359 (unsigned int)in_input.length ));
361 subreq = np_write_send(state, ev,
362 fsp->fake_file_handle,
365 if (tevent_req_nomem(subreq, req)) {
366 return tevent_req_post(req, ev);
368 tevent_req_set_callback(subreq,
369 smbd_smb2_ioctl_pipe_write_done,
373 case 0x00144064: /* FSCTL_SRV_ENUMERATE_SNAPSHOTS */
376 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
377 * and return their volume names. If max_data_count is 16, then it is just
378 * asking for the number of volumes and length of the combined names.
380 * pdata is the data allocated by our caller, but that uses
381 * total_data_count (which is 0 in our case) rather than max_data_count.
382 * Allocate the correct amount and return the pointer to let
383 * it be deallocated when we return.
385 struct shadow_copy_data *shadow_data = NULL;
387 uint32_t labels_data_count = 0;
394 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
395 return tevent_req_post(req, ev);
398 if (in_max_output < 16) {
399 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
400 "in_max_output(%u) < 16 is invalid!\n",
402 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
403 return tevent_req_post(req, ev);
406 if (in_max_output > 16) {
410 shadow_data = TALLOC_ZERO_P(talloc_tos(),
411 struct shadow_copy_data);
412 if (tevent_req_nomem(shadow_data, req)) {
413 DEBUG(0,("TALLOC_ZERO() failed!\n"));
414 return tevent_req_post(req, ev);
418 * Call the VFS routine to actually do the work.
420 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)
422 if (errno == ENOSYS) {
423 DEBUG(5, ("FSCTL_GET_SHADOW_COPY_DATA: "
424 "connectpath %s, not supported.\n",
425 smbreq->conn->connectpath));
426 status = NT_STATUS_NOT_SUPPORTED;
428 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
429 "connectpath %s, failed.\n",
430 smbreq->conn->connectpath));
431 status = map_nt_error_from_unix(errno);
433 TALLOC_FREE(shadow_data);
434 tevent_req_nterror(req, status);
435 return tevent_req_post(req, ev);
439 (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))
443 data_count = 12+labels_data_count+4;
448 if (labels && (in_max_output < data_count)) {
449 DEBUG(0, ("FSCTL_GET_SHADOW_COPY_DATA: "
450 "in_max_output(%u) too small (%u) bytes "
451 "needed!\n", in_max_output, data_count));
452 TALLOC_FREE(shadow_data);
453 tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
454 return tevent_req_post(req, ev);
457 state->out_output = data_blob_talloc(state, NULL, data_count);
458 if (tevent_req_nomem(state->out_output.data, req)) {
459 return tevent_req_post(req, ev);
462 pdata = (char *)state->out_output.data;
464 /* num_volumes 4 bytes */
465 SIVAL(pdata, 0, shadow_data->num_volumes);
468 /* num_labels 4 bytes */
469 SIVAL(pdata, 4, shadow_data->num_volumes);
472 /* needed_data_count 4 bytes */
473 SIVAL(pdata, 8, labels_data_count+4);
477 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for "
479 shadow_data->num_volumes, fsp_str_dbg(fsp)));
480 if (labels && shadow_data->labels) {
481 for (i=0; i<shadow_data->num_volumes; i++) {
482 srvstr_push(pdata, smbreq->flags2,
483 pdata, shadow_data->labels[i],
484 2*sizeof(SHADOW_COPY_LABEL),
485 STR_UNICODE|STR_TERMINATE);
486 pdata += 2*sizeof(SHADOW_COPY_LABEL);
487 DEBUGADD(10, ("Label[%u]: '%s'\n", i,
488 shadow_data->labels[i]));
492 TALLOC_FREE(shadow_data);
494 tevent_req_done(req);
495 return tevent_req_post(req, ev);
499 if (IS_IPC(smbreq->conn)) {
500 tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
501 return tevent_req_post(req, ev);
503 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
504 return tevent_req_post(req, ev);
507 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
508 return tevent_req_post(req, ev);
511 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
513 struct tevent_req *req = tevent_req_callback_data(subreq,
515 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
516 struct smbd_smb2_ioctl_state);
518 ssize_t nwritten = -1;
520 status = np_write_recv(subreq, &nwritten);
522 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
523 (long int)nwritten ));
526 if (!NT_STATUS_IS_OK(status)) {
527 tevent_req_nterror(req, status);
531 if (nwritten != state->in_input.length) {
532 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
536 state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
537 if (state->in_max_output > 0 &&
538 tevent_req_nomem(state->out_output.data, req)) {
542 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
544 (unsigned int)state->out_output.length ));
547 subreq = np_read_send(state->smbreq->conn,
548 state->smb2req->sconn->smb2.event_ctx,
549 state->fsp->fake_file_handle,
550 state->out_output.data,
551 state->out_output.length);
552 if (tevent_req_nomem(subreq, req)) {
555 tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
558 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
560 struct tevent_req *req = tevent_req_callback_data(subreq,
562 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
563 struct smbd_smb2_ioctl_state);
566 bool is_data_outstanding = false;
568 status = np_read_recv(subreq, &nread, &is_data_outstanding);
570 DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
571 "is_data_outstanding = %d, status = %s\n",
573 (int)is_data_outstanding,
574 nt_errstr(status) ));
577 if (!NT_STATUS_IS_OK(status)) {
578 tevent_req_nterror(req, status);
582 state->out_output.length = nread;
584 if (is_data_outstanding) {
585 tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
589 tevent_req_done(req);
592 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
594 DATA_BLOB *out_output)
596 NTSTATUS status = NT_STATUS_OK;
597 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
598 struct smbd_smb2_ioctl_state);
600 if (tevent_req_is_nterror(req, &status)) {
601 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
602 tevent_req_received(req);
607 *out_output = state->out_output;
608 talloc_steal(mem_ctx, out_output->data);
610 tevent_req_received(req);