s3: Fix a winbind race leading to 100% CPU
[Samba.git] / source3 / smbd / smb2_ioctl.c
blob88775b4ad7778eac8c378e8615dfc5fdb61e7edf
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 "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,
32 uint32_t in_ctl_code,
33 uint64_t in_file_id_volatile,
34 DATA_BLOB in_input,
35 uint32_t in_max_output,
36 uint32_t in_flags);
37 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
38 TALLOC_CTX *mem_ctx,
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)
44 const uint8_t *inhdr;
45 const uint8_t *inbody;
46 int i = req->current_idx;
47 size_t expected_body_size = 0x39;
48 size_t body_size;
49 uint32_t in_ctl_code;
50 uint64_t in_file_id_persistent;
51 uint64_t in_file_id_volatile;
52 uint32_t in_input_offset;
53 uint32_t in_input_length;
54 DATA_BLOB in_input_buffer;
55 uint32_t in_max_output_length;
56 uint32_t in_flags;
57 struct tevent_req *subreq;
59 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
60 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
61 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
64 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
66 body_size = SVAL(inbody, 0x00);
67 if (body_size != expected_body_size) {
68 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
71 in_ctl_code = IVAL(inbody, 0x04);
72 in_file_id_persistent = BVAL(inbody, 0x08);
73 in_file_id_volatile = BVAL(inbody, 0x10);
74 in_input_offset = IVAL(inbody, 0x18);
75 in_input_length = IVAL(inbody, 0x1C);
76 in_max_output_length = IVAL(inbody, 0x2C);
77 in_flags = IVAL(inbody, 0x30);
79 if (in_input_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
80 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
83 if (in_input_length > req->in.vector[i+2].iov_len) {
84 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
87 in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
88 in_input_buffer.length = in_input_length;
90 if (req->compat_chain_fsp) {
91 /* skip check */
92 } else if (in_file_id_persistent == UINT64_MAX &&
93 in_file_id_volatile == UINT64_MAX) {
94 /* without a handle */
95 } else if (in_file_id_persistent != in_file_id_volatile) {
96 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
99 subreq = smbd_smb2_ioctl_send(req,
100 req->sconn->smb2.event_ctx,
101 req,
102 in_ctl_code,
103 in_file_id_volatile,
104 in_input_buffer,
105 in_max_output_length,
106 in_flags);
107 if (subreq == NULL) {
108 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
110 tevent_req_set_callback(subreq, smbd_smb2_request_ioctl_done, req);
112 return smbd_smb2_request_pending_queue(req, subreq);
115 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq)
117 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
118 struct smbd_smb2_request);
119 const uint8_t *inbody;
120 int i = req->current_idx;
121 uint8_t *outhdr;
122 DATA_BLOB outbody;
123 DATA_BLOB outdyn;
124 uint32_t in_ctl_code;
125 uint64_t in_file_id_persistent;
126 uint64_t in_file_id_volatile;
127 uint32_t out_input_offset;
128 uint32_t out_output_offset;
129 DATA_BLOB out_output_buffer = data_blob_null;
130 NTSTATUS status;
131 NTSTATUS error; /* transport error */
133 status = smbd_smb2_ioctl_recv(subreq, req, &out_output_buffer);
135 DEBUG(10,("smbd_smb2_request_ioctl_done: smbd_smb2_ioctl_recv returned "
136 "%u status %s\n",
137 (unsigned int)out_output_buffer.length,
138 nt_errstr(status) ));
140 TALLOC_FREE(subreq);
141 if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
142 /* also ok */
143 } else if (!NT_STATUS_IS_OK(status)) {
144 error = smbd_smb2_request_error(req, status);
145 if (!NT_STATUS_IS_OK(error)) {
146 smbd_server_connection_terminate(req->sconn,
147 nt_errstr(error));
148 return;
150 return;
153 out_input_offset = SMB2_HDR_BODY + 0x30;
154 out_output_offset = SMB2_HDR_BODY + 0x30;
156 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
158 in_ctl_code = IVAL(inbody, 0x04);
159 in_file_id_persistent = BVAL(inbody, 0x08);
160 in_file_id_volatile = BVAL(inbody, 0x10);
162 outhdr = (uint8_t *)req->out.vector[i].iov_base;
164 outbody = data_blob_talloc(req->out.vector, NULL, 0x30);
165 if (outbody.data == NULL) {
166 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
167 if (!NT_STATUS_IS_OK(error)) {
168 smbd_server_connection_terminate(req->sconn,
169 nt_errstr(error));
170 return;
172 return;
175 SSVAL(outbody.data, 0x00, 0x30 + 1); /* struct size */
176 SSVAL(outbody.data, 0x02, 0); /* reserved */
177 SIVAL(outbody.data, 0x04,
178 in_ctl_code); /* ctl code */
179 SBVAL(outbody.data, 0x08,
180 in_file_id_persistent); /* file id (persistent) */
181 SBVAL(outbody.data, 0x10,
182 in_file_id_volatile); /* file id (volatile) */
183 SIVAL(outbody.data, 0x18,
184 out_input_offset); /* input offset */
185 SIVAL(outbody.data, 0x1C, 0); /* input count */
186 SIVAL(outbody.data, 0x20,
187 out_output_offset); /* output offset */
188 SIVAL(outbody.data, 0x24,
189 out_output_buffer.length); /* output count */
190 SIVAL(outbody.data, 0x28, 0); /* flags */
191 SIVAL(outbody.data, 0x2C, 0); /* reserved */
194 * Note: Windows Vista and 2008 send back also the
195 * input from the request. But it was fixed in
196 * Windows 7.
198 outdyn = out_output_buffer;
200 error = smbd_smb2_request_done_ex(req, status, outbody, &outdyn,
201 __location__);
202 if (!NT_STATUS_IS_OK(error)) {
203 smbd_server_connection_terminate(req->sconn,
204 nt_errstr(error));
205 return;
209 struct smbd_smb2_ioctl_state {
210 struct smbd_smb2_request *smb2req;
211 struct smb_request *smbreq;
212 files_struct *fsp;
213 DATA_BLOB in_input;
214 uint32_t in_max_output;
215 DATA_BLOB out_output;
218 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq);
219 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq);
221 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
222 struct tevent_context *ev,
223 struct smbd_smb2_request *smb2req,
224 uint32_t in_ctl_code,
225 uint64_t in_file_id_volatile,
226 DATA_BLOB in_input,
227 uint32_t in_max_output,
228 uint32_t in_flags)
230 struct tevent_req *req;
231 struct smbd_smb2_ioctl_state *state;
232 struct smb_request *smbreq;
233 files_struct *fsp = NULL;
234 struct tevent_req *subreq;
236 req = tevent_req_create(mem_ctx, &state,
237 struct smbd_smb2_ioctl_state);
238 if (req == NULL) {
239 return NULL;
241 state->smb2req = smb2req;
242 state->smbreq = NULL;
243 state->fsp = NULL;
244 state->in_input = in_input;
245 state->in_max_output = in_max_output;
246 state->out_output = data_blob_null;
248 DEBUG(10,("smbd_smb2_ioctl: file_id[0x%016llX]\n",
249 (unsigned long long)in_file_id_volatile));
251 smbreq = smbd_smb2_fake_smb_request(smb2req);
252 if (tevent_req_nomem(smbreq, req)) {
253 return tevent_req_post(req, ev);
255 state->smbreq = smbreq;
257 if (in_file_id_volatile != UINT64_MAX) {
258 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
259 if (fsp == NULL) {
260 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
261 return tevent_req_post(req, ev);
263 if (smbreq->conn != fsp->conn) {
264 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
265 return tevent_req_post(req, ev);
267 if (smb2req->session->vuid != fsp->vuid) {
268 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
269 return tevent_req_post(req, ev);
271 state->fsp = fsp;
274 switch (in_ctl_code) {
275 case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
277 uint16_t in_max_referral_level;
278 DATA_BLOB in_file_name_buffer;
279 char *in_file_name_string;
280 size_t in_file_name_string_size;
281 bool ok;
282 bool overflow = false;
283 NTSTATUS status;
284 int dfs_size;
285 char *dfs_data = NULL;
287 if (!IS_IPC(smbreq->conn)) {
288 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
289 return tevent_req_post(req, ev);
292 if (!lp_host_msdfs()) {
293 tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
294 return tevent_req_post(req, ev);
297 if (in_input.length < (2 + 2)) {
298 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
299 return tevent_req_post(req, ev);
302 in_max_referral_level = SVAL(in_input.data, 0);
303 in_file_name_buffer.data = in_input.data + 2;
304 in_file_name_buffer.length = in_input.length - 2;
306 ok = convert_string_talloc(state, CH_UTF16, CH_UNIX,
307 in_file_name_buffer.data,
308 in_file_name_buffer.length,
309 &in_file_name_string,
310 &in_file_name_string_size, false);
311 if (!ok) {
312 tevent_req_nterror(req, NT_STATUS_ILLEGAL_CHARACTER);
313 return tevent_req_post(req, ev);
316 dfs_size = setup_dfs_referral(smbreq->conn,
317 in_file_name_string,
318 in_max_referral_level,
319 &dfs_data, &status);
320 if (dfs_size < 0) {
321 tevent_req_nterror(req, status);
322 return tevent_req_post(req, ev);
325 if (dfs_size > in_max_output) {
327 * TODO: we need a testsuite for this
329 overflow = true;
330 dfs_size = in_max_output;
333 state->out_output = data_blob_talloc(state,
334 (uint8_t *)dfs_data,
335 dfs_size);
336 SAFE_FREE(dfs_data);
337 if (dfs_size > 0 &&
338 tevent_req_nomem(state->out_output.data, req)) {
339 return tevent_req_post(req, ev);
342 if (overflow) {
343 tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
344 } else {
345 tevent_req_done(req);
347 return tevent_req_post(req, ev);
349 case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
351 if (!IS_IPC(smbreq->conn)) {
352 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
353 return tevent_req_post(req, ev);
356 if (fsp == NULL) {
357 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
358 return tevent_req_post(req, ev);
361 if (!fsp_is_np(fsp)) {
362 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
363 return tevent_req_post(req, ev);
366 DEBUG(10,("smbd_smb2_ioctl_send: np_write_send of size %u\n",
367 (unsigned int)in_input.length ));
369 subreq = np_write_send(state, ev,
370 fsp->fake_file_handle,
371 in_input.data,
372 in_input.length);
373 if (tevent_req_nomem(subreq, req)) {
374 return tevent_req_post(req, ev);
376 tevent_req_set_callback(subreq,
377 smbd_smb2_ioctl_pipe_write_done,
378 req);
379 return req;
381 case 0x00144064: /* FSCTL_SRV_ENUMERATE_SNAPSHOTS */
384 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
385 * and return their volume names. If max_data_count is 16, then it is just
386 * asking for the number of volumes and length of the combined names.
388 * pdata is the data allocated by our caller, but that uses
389 * total_data_count (which is 0 in our case) rather than max_data_count.
390 * Allocate the correct amount and return the pointer to let
391 * it be deallocated when we return.
393 struct shadow_copy_data *shadow_data = NULL;
394 bool labels = False;
395 uint32_t labels_data_count = 0;
396 uint32_t data_count;
397 uint32_t i;
398 char *pdata;
399 NTSTATUS status;
401 if (fsp == NULL) {
402 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
403 return tevent_req_post(req, ev);
406 if (in_max_output < 16) {
407 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
408 "in_max_output(%u) < 16 is invalid!\n",
409 in_max_output));
410 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
411 return tevent_req_post(req, ev);
414 if (in_max_output > 16) {
415 labels = True;
418 shadow_data = TALLOC_ZERO_P(talloc_tos(),
419 struct shadow_copy_data);
420 if (tevent_req_nomem(shadow_data, req)) {
421 DEBUG(0,("TALLOC_ZERO() failed!\n"));
422 return tevent_req_post(req, ev);
426 * Call the VFS routine to actually do the work.
428 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)
429 != 0) {
430 if (errno == ENOSYS) {
431 DEBUG(5, ("FSCTL_GET_SHADOW_COPY_DATA: "
432 "connectpath %s, not supported.\n",
433 smbreq->conn->connectpath));
434 status = NT_STATUS_NOT_SUPPORTED;
435 } else {
436 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
437 "connectpath %s, failed.\n",
438 smbreq->conn->connectpath));
439 status = map_nt_error_from_unix(errno);
441 TALLOC_FREE(shadow_data);
442 tevent_req_nterror(req, status);
443 return tevent_req_post(req, ev);
446 labels_data_count =
447 (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))
448 + 2;
450 if (labels) {
451 data_count = 12+labels_data_count+4;
452 } else {
453 data_count = 16;
456 if (labels && (in_max_output < data_count)) {
457 DEBUG(0, ("FSCTL_GET_SHADOW_COPY_DATA: "
458 "in_max_output(%u) too small (%u) bytes "
459 "needed!\n", in_max_output, data_count));
460 TALLOC_FREE(shadow_data);
461 tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
462 return tevent_req_post(req, ev);
465 state->out_output = data_blob_talloc(state, NULL, data_count);
466 if (tevent_req_nomem(state->out_output.data, req)) {
467 return tevent_req_post(req, ev);
470 pdata = (char *)state->out_output.data;
472 /* num_volumes 4 bytes */
473 SIVAL(pdata, 0, shadow_data->num_volumes);
475 if (labels) {
476 /* num_labels 4 bytes */
477 SIVAL(pdata, 4, shadow_data->num_volumes);
480 /* needed_data_count 4 bytes */
481 SIVAL(pdata, 8, labels_data_count+4);
483 pdata += 12;
485 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for "
486 "path[%s].\n",
487 shadow_data->num_volumes, fsp_str_dbg(fsp)));
488 if (labels && shadow_data->labels) {
489 for (i=0; i<shadow_data->num_volumes; i++) {
490 srvstr_push(pdata, smbreq->flags2,
491 pdata, shadow_data->labels[i],
492 2*sizeof(SHADOW_COPY_LABEL),
493 STR_UNICODE|STR_TERMINATE);
494 pdata += 2*sizeof(SHADOW_COPY_LABEL);
495 DEBUGADD(10, ("Label[%u]: '%s'\n", i,
496 shadow_data->labels[i]));
500 TALLOC_FREE(shadow_data);
502 tevent_req_done(req);
503 return tevent_req_post(req, ev);
506 default:
507 if (IS_IPC(smbreq->conn)) {
508 tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
509 return tevent_req_post(req, ev);
511 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
512 return tevent_req_post(req, ev);
515 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
516 return tevent_req_post(req, ev);
519 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
521 struct tevent_req *req = tevent_req_callback_data(subreq,
522 struct tevent_req);
523 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
524 struct smbd_smb2_ioctl_state);
525 NTSTATUS status;
526 ssize_t nwritten = -1;
528 status = np_write_recv(subreq, &nwritten);
530 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
531 (long int)nwritten ));
533 TALLOC_FREE(subreq);
534 if (!NT_STATUS_IS_OK(status)) {
535 tevent_req_nterror(req, status);
536 return;
539 if (nwritten != state->in_input.length) {
540 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
541 return;
544 state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
545 if (state->in_max_output > 0 &&
546 tevent_req_nomem(state->out_output.data, req)) {
547 return;
550 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
551 "of size %u\n",
552 (unsigned int)state->out_output.length ));
554 TALLOC_FREE(subreq);
555 subreq = np_read_send(state->smbreq->conn,
556 state->smb2req->sconn->smb2.event_ctx,
557 state->fsp->fake_file_handle,
558 state->out_output.data,
559 state->out_output.length);
560 if (tevent_req_nomem(subreq, req)) {
561 return;
563 tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
566 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
568 struct tevent_req *req = tevent_req_callback_data(subreq,
569 struct tevent_req);
570 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
571 struct smbd_smb2_ioctl_state);
572 NTSTATUS status;
573 ssize_t nread = -1;
574 bool is_data_outstanding = false;
576 status = np_read_recv(subreq, &nread, &is_data_outstanding);
578 DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
579 "is_data_outstanding = %d, status = %s\n",
580 (int)nread,
581 (int)is_data_outstanding,
582 nt_errstr(status) ));
584 TALLOC_FREE(subreq);
585 if (!NT_STATUS_IS_OK(status)) {
586 tevent_req_nterror(req, status);
587 return;
590 state->out_output.length = nread;
592 if (is_data_outstanding) {
593 tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
594 return;
597 tevent_req_done(req);
600 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
601 TALLOC_CTX *mem_ctx,
602 DATA_BLOB *out_output)
604 NTSTATUS status = NT_STATUS_OK;
605 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
606 struct smbd_smb2_ioctl_state);
608 if (tevent_req_is_nterror(req, &status)) {
609 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
610 tevent_req_received(req);
611 return status;
615 *out_output = state->out_output;
616 talloc_steal(mem_ctx, out_output->data);
618 tevent_req_received(req);
619 return status;