333616ab6143b6552c3e04df69edbe34f7e8cd3f
[metze/samba/wip.git] / source3 / smbd / smb2_ioctl.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
24
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);
36
37 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq);
38 NTSTATUS smbd_smb2_request_process_ioctl(struct smbd_smb2_request *req)
39 {
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;
54
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);
58         }
59
60         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
61
62         body_size = SVAL(inbody, 0x00);
63         if (body_size != expected_body_size) {
64                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
65         }
66
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);
74
75         if (in_input_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
76                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77         }
78
79         if (in_input_length > req->in.vector[i+2].iov_len) {
80                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
81         }
82
83         in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
84         in_input_buffer.length = in_input_length;
85
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);
93         }
94
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);
105         }
106         tevent_req_set_callback(subreq, smbd_smb2_request_ioctl_done, req);
107
108         if (tevent_req_is_in_progress(subreq)) {
109                 return smbd_smb2_request_pending_queue(req);
110         }
111
112         return NT_STATUS_OK;
113 }
114
115 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq)
116 {
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 */
132
133         status = smbd_smb2_ioctl_recv(subreq, req, &out_output_buffer);
134         TALLOC_FREE(subreq);
135         if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
136                 /* also ok */
137         } else if (!NT_STATUS_IS_OK(status)) {
138                 error = smbd_smb2_request_error(req, status);
139                 if (!NT_STATUS_IS_OK(error)) {
140                         smbd_server_connection_terminate(req->sconn,
141                                                          nt_errstr(error));
142                         return;
143                 }
144                 return;
145         }
146
147         out_input_offset = SMB2_HDR_BODY + 0x30;
148         out_output_offset = SMB2_HDR_BODY + 0x30;
149
150         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
151
152         in_ctl_code             = IVAL(inbody, 0x04);
153         in_file_id_persistent   = BVAL(inbody, 0x08);
154         in_file_id_volatile     = BVAL(inbody, 0x10);
155
156         outhdr = (uint8_t *)req->out.vector[i].iov_base;
157
158         outbody = data_blob_talloc(req->out.vector, NULL, 0x30);
159         if (outbody.data == NULL) {
160                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
161                 if (!NT_STATUS_IS_OK(error)) {
162                         smbd_server_connection_terminate(req->sconn,
163                                                          nt_errstr(error));
164                         return;
165                 }
166                 return;
167         }
168
169         SSVAL(outbody.data, 0x00, 0x30 + 1);    /* struct size */
170         SSVAL(outbody.data, 0x02, 0);           /* reserved */
171         SIVAL(outbody.data, 0x04,
172               in_ctl_code);                     /* ctl code */
173         SBVAL(outbody.data, 0x08,
174               in_file_id_persistent);           /* file id (persistent) */
175         SBVAL(outbody.data, 0x10,
176               in_file_id_volatile);             /* file id (volatile) */
177         SIVAL(outbody.data, 0x18,
178               out_input_offset);                /* input offset */
179         SIVAL(outbody.data, 0x1C, 0);           /* input count */
180         SIVAL(outbody.data, 0x20,
181               out_output_offset);               /* output offset */
182         SIVAL(outbody.data, 0x24,
183               out_output_buffer.length);        /* output count */
184         SIVAL(outbody.data, 0x28, 0);           /* flags */
185         SIVAL(outbody.data, 0x2C, 0);           /* reserved */
186
187         /*
188          * Note: Windows Vista and 2008 send back also the
189          *       input from the request. But it was fixed in
190          *       Windows 7.
191          */
192         outdyn = out_output_buffer;
193
194         error = smbd_smb2_request_done_ex(req, status, outbody, &outdyn,
195                                           __location__);
196         if (!NT_STATUS_IS_OK(error)) {
197                 smbd_server_connection_terminate(req->sconn,
198                                                  nt_errstr(error));
199                 return;
200         }
201 }
202
203 struct smbd_smb2_ioctl_state {
204         struct smbd_smb2_request *smb2req;
205         struct smb_request *smbreq;
206         files_struct *fsp;
207         DATA_BLOB in_input;
208         uint32_t in_max_output;
209         DATA_BLOB out_output;
210 };
211
212 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq);
213 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq);
214
215 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
216                                                struct tevent_context *ev,
217                                                struct smbd_smb2_request *smb2req,
218                                                uint32_t in_ctl_code,
219                                                uint64_t in_file_id_volatile,
220                                                DATA_BLOB in_input,
221                                                uint32_t in_max_output,
222                                                uint32_t in_flags)
223 {
224         struct tevent_req *req;
225         struct smbd_smb2_ioctl_state *state;
226         struct smb_request *smbreq;
227         files_struct *fsp = NULL;
228         struct tevent_req *subreq;
229
230         req = tevent_req_create(mem_ctx, &state,
231                                 struct smbd_smb2_ioctl_state);
232         if (req == NULL) {
233                 return NULL;
234         }
235         state->smb2req = smb2req;
236         state->smbreq = NULL;
237         state->fsp = NULL;
238         state->in_input = in_input;
239         state->in_max_output = in_max_output;
240         state->out_output = data_blob_null;
241
242         DEBUG(10,("smbd_smb2_ioctl: file_id[0x%016llX]\n",
243                   (unsigned long long)in_file_id_volatile));
244
245         smbreq = smbd_smb2_fake_smb_request(smb2req);
246         if (tevent_req_nomem(smbreq, req)) {
247                 return tevent_req_post(req, ev);
248         }
249         state->smbreq = smbreq;
250
251         if (in_file_id_volatile != UINT64_MAX) {
252                 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
253                 if (fsp == NULL) {
254                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
255                         return tevent_req_post(req, ev);
256                 }
257                 if (smbreq->conn != fsp->conn) {
258                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
259                         return tevent_req_post(req, ev);
260                 }
261                 if (smb2req->session->vuid != fsp->vuid) {
262                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
263                         return tevent_req_post(req, ev);
264                 }
265                 state->fsp = fsp;
266         }
267
268         switch (in_ctl_code) {
269         case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
270         {
271                 uint16_t in_max_referral_level;
272                 DATA_BLOB in_file_name_buffer;
273                 char *in_file_name_string;
274                 size_t in_file_name_string_size;
275                 bool ok;
276                 bool overflow = false;
277                 NTSTATUS status;
278                 int dfs_size;
279                 char *dfs_data = NULL;
280
281                 if (!IS_IPC(smbreq->conn)) {
282                         tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
283                         return tevent_req_post(req, ev);
284                 }
285
286                 if (!lp_host_msdfs()) {
287                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
288                         return tevent_req_post(req, ev);
289                 }
290
291                 if (in_input.length < (2 + 2)) {
292                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
293                         return tevent_req_post(req, ev);
294                 }
295
296                 in_max_referral_level = SVAL(in_input.data, 0);
297                 in_file_name_buffer.data = in_input.data + 2;
298                 in_file_name_buffer.length = in_input.length - 2;
299
300                 ok = convert_string_talloc(state, CH_UTF16, CH_UNIX,
301                                            in_file_name_buffer.data,
302                                            in_file_name_buffer.length,
303                                            &in_file_name_string,
304                                            &in_file_name_string_size, false);
305                 if (!ok) {
306                         tevent_req_nterror(req, NT_STATUS_ILLEGAL_CHARACTER);
307                         return tevent_req_post(req, ev);
308                 }
309
310                 dfs_size = setup_dfs_referral(smbreq->conn,
311                                               in_file_name_string,
312                                               in_max_referral_level,
313                                               &dfs_data, &status);
314                 if (dfs_size < 0) {
315                         tevent_req_nterror(req, status);
316                         return tevent_req_post(req, ev);
317                 }
318
319                 if (dfs_size > in_max_output) {
320                         /*
321                          * TODO: we need a testsuite for this
322                          */
323                         overflow = true;
324                         dfs_size = in_max_output;
325                 }
326
327                 state->out_output = data_blob_talloc(state,
328                                                      (uint8_t *)dfs_data,
329                                                      dfs_size);
330                 SAFE_FREE(dfs_data);
331                 if (dfs_size > 0 &&
332                     tevent_req_nomem(state->out_output.data, req)) {
333                         return tevent_req_post(req, ev);
334                 }
335
336                 if (overflow) {
337                         tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
338                 } else {
339                         tevent_req_done(req);
340                 }
341                 return tevent_req_post(req, ev);
342         }
343         case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
344
345                 if (!IS_IPC(smbreq->conn)) {
346                         tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
347                         return tevent_req_post(req, ev);
348                 }
349
350                 if (fsp == NULL) {
351                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
352                         return tevent_req_post(req, ev);
353                 }
354
355                 if (!fsp_is_np(fsp)) {
356                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
357                         return tevent_req_post(req, ev);
358                 }
359
360                 subreq = np_write_send(state, ev,
361                                        fsp->fake_file_handle,
362                                        in_input.data,
363                                        in_input.length);
364                 if (tevent_req_nomem(subreq, req)) {
365                         return tevent_req_post(req, ev);
366                 }
367                 tevent_req_set_callback(subreq,
368                                         smbd_smb2_ioctl_pipe_write_done,
369                                         req);
370                 return req;
371
372         default:
373                 if (IS_IPC(smbreq->conn)) {
374                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
375                         return tevent_req_post(req, ev);
376                 }
377                 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
378                 return tevent_req_post(req, ev);
379         }
380
381         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
382         return tevent_req_post(req, ev);
383 }
384
385 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
386 {
387         struct tevent_req *req = tevent_req_callback_data(subreq,
388                                  struct tevent_req);
389         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
390                                               struct smbd_smb2_ioctl_state);
391         NTSTATUS status;
392         ssize_t nwritten = -1;
393
394         status = np_write_recv(subreq, &nwritten);
395         TALLOC_FREE(subreq);
396         if (!NT_STATUS_IS_OK(status)) {
397                 tevent_req_nterror(req, status);
398                 return;
399         }
400
401         if (nwritten != state->in_input.length) {
402                 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
403                 return;
404         }
405
406         state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
407         if (state->in_max_output > 0 &&
408             tevent_req_nomem(state->out_output.data, req)) {
409                 return;
410         }
411
412         subreq = np_read_send(state->smbreq->conn,
413                               state->smb2req->sconn->smb2.event_ctx,
414                               state->fsp->fake_file_handle,
415                               state->out_output.data,
416                               state->out_output.length);
417         if (tevent_req_nomem(subreq, req)) {
418                 return;
419         }
420         tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
421 }
422
423 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
424 {
425         struct tevent_req *req = tevent_req_callback_data(subreq,
426                                  struct tevent_req);
427         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
428                                               struct smbd_smb2_ioctl_state);
429         NTSTATUS status;
430         ssize_t nread;
431         bool is_data_outstanding;
432
433         status = np_read_recv(subreq, &nread, &is_data_outstanding);
434         TALLOC_FREE(subreq);
435         if (!NT_STATUS_IS_OK(status)) {
436                 tevent_req_nterror(req, status);
437                 return;
438         }
439
440         state->out_output.length = nread;
441
442         tevent_req_done(req);
443 }
444
445 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
446                                      TALLOC_CTX *mem_ctx,
447                                      DATA_BLOB *out_output)
448 {
449         NTSTATUS status;
450         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
451                                               struct smbd_smb2_ioctl_state);
452
453         if (tevent_req_is_nterror(req, &status)) {
454                 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
455                         tevent_req_received(req);
456                         return status;
457                 }
458         }
459
460         *out_output = state->out_output;
461         talloc_steal(mem_ctx, out_output->data);
462
463         tevent_req_received(req);
464         return status;
465 }