Fix bug #8311 - Winzip occasionally can not read files out of an open winzip dialog.
[ddiss/samba.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/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"
28
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);
40
41 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq);
42 NTSTATUS smbd_smb2_request_process_ioctl(struct smbd_smb2_request *req)
43 {
44         NTSTATUS status;
45         const uint8_t *inbody;
46         int i = req->current_idx;
47         uint32_t in_ctl_code;
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;
54         uint32_t in_flags;
55         struct tevent_req *subreq;
56
57         status = smbd_smb2_request_verify_sizes(req, 0x39);
58         if (!NT_STATUS_IS_OK(status)) {
59                 return smbd_smb2_request_error(req, status);
60         }
61         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
62
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);
70
71         /*
72          * InputOffset (4 bytes): The offset, in bytes, from the beginning of
73          * the SMB2 header to the input data buffer. If no input data is
74          * required for the FSCTL/IOCTL command being issued, the client SHOULD
75          * set this value to 0.<49>
76          * <49> If no input data is required for the FSCTL/IOCTL command being
77          * issued, Windows-based clients set this field to any value.
78          */
79         if ((in_input_length > 0)
80          && (in_input_offset != (SMB2_HDR_BODY + req->in.vector[i+1].iov_len))) {
81                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
82         }
83
84         if (in_input_length > req->in.vector[i+2].iov_len) {
85                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
86         }
87
88         in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
89         in_input_buffer.length = in_input_length;
90
91         if (req->compat_chain_fsp) {
92                 /* skip check */
93         } else if (in_file_id_persistent == UINT64_MAX &&
94                    in_file_id_volatile == UINT64_MAX) {
95                 /* without a handle */
96         } else if (in_file_id_persistent != in_file_id_volatile) {
97                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
98         }
99
100         subreq = smbd_smb2_ioctl_send(req,
101                                       req->sconn->smb2.event_ctx,
102                                       req,
103                                       in_ctl_code,
104                                       in_file_id_volatile,
105                                       in_input_buffer,
106                                       in_max_output_length,
107                                       in_flags);
108         if (subreq == NULL) {
109                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
110         }
111         tevent_req_set_callback(subreq, smbd_smb2_request_ioctl_done, req);
112
113         return smbd_smb2_request_pending_queue(req, subreq);
114 }
115
116 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq)
117 {
118         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
119                                         struct smbd_smb2_request);
120         const uint8_t *inbody;
121         int i = req->current_idx;
122         uint8_t *outhdr;
123         DATA_BLOB outbody;
124         DATA_BLOB outdyn;
125         uint32_t in_ctl_code;
126         uint64_t in_file_id_persistent;
127         uint64_t in_file_id_volatile;
128         uint32_t out_input_offset;
129         uint32_t out_output_offset;
130         DATA_BLOB out_output_buffer = data_blob_null;
131         NTSTATUS status;
132         NTSTATUS error; /* transport error */
133
134         status = smbd_smb2_ioctl_recv(subreq, req, &out_output_buffer);
135
136         DEBUG(10,("smbd_smb2_request_ioctl_done: smbd_smb2_ioctl_recv returned "
137                 "%u status %s\n",
138                 (unsigned int)out_output_buffer.length,
139                 nt_errstr(status) ));
140
141         TALLOC_FREE(subreq);
142         if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
143                 /* also ok */
144         } else if (!NT_STATUS_IS_OK(status)) {
145                 error = smbd_smb2_request_error(req, status);
146                 if (!NT_STATUS_IS_OK(error)) {
147                         smbd_server_connection_terminate(req->sconn,
148                                                          nt_errstr(error));
149                         return;
150                 }
151                 return;
152         }
153
154         out_input_offset = SMB2_HDR_BODY + 0x30;
155         out_output_offset = SMB2_HDR_BODY + 0x30;
156
157         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
158
159         in_ctl_code             = IVAL(inbody, 0x04);
160         in_file_id_persistent   = BVAL(inbody, 0x08);
161         in_file_id_volatile     = BVAL(inbody, 0x10);
162
163         outhdr = (uint8_t *)req->out.vector[i].iov_base;
164
165         outbody = data_blob_talloc(req->out.vector, NULL, 0x30);
166         if (outbody.data == NULL) {
167                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
168                 if (!NT_STATUS_IS_OK(error)) {
169                         smbd_server_connection_terminate(req->sconn,
170                                                          nt_errstr(error));
171                         return;
172                 }
173                 return;
174         }
175
176         SSVAL(outbody.data, 0x00, 0x30 + 1);    /* struct size */
177         SSVAL(outbody.data, 0x02, 0);           /* reserved */
178         SIVAL(outbody.data, 0x04,
179               in_ctl_code);                     /* ctl code */
180         SBVAL(outbody.data, 0x08,
181               in_file_id_persistent);           /* file id (persistent) */
182         SBVAL(outbody.data, 0x10,
183               in_file_id_volatile);             /* file id (volatile) */
184         SIVAL(outbody.data, 0x18,
185               out_input_offset);                /* input offset */
186         SIVAL(outbody.data, 0x1C, 0);           /* input count */
187         SIVAL(outbody.data, 0x20,
188               out_output_offset);               /* output offset */
189         SIVAL(outbody.data, 0x24,
190               out_output_buffer.length);        /* output count */
191         SIVAL(outbody.data, 0x28, 0);           /* flags */
192         SIVAL(outbody.data, 0x2C, 0);           /* reserved */
193
194         /*
195          * Note: Windows Vista and 2008 send back also the
196          *       input from the request. But it was fixed in
197          *       Windows 7.
198          */
199         outdyn = out_output_buffer;
200
201         error = smbd_smb2_request_done_ex(req, status, outbody, &outdyn,
202                                           __location__);
203         if (!NT_STATUS_IS_OK(error)) {
204                 smbd_server_connection_terminate(req->sconn,
205                                                  nt_errstr(error));
206                 return;
207         }
208 }
209
210 struct smbd_smb2_ioctl_state {
211         struct smbd_smb2_request *smb2req;
212         struct smb_request *smbreq;
213         files_struct *fsp;
214         DATA_BLOB in_input;
215         uint32_t in_max_output;
216         DATA_BLOB out_output;
217 };
218
219 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq);
220 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq);
221
222 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
223                                                struct tevent_context *ev,
224                                                struct smbd_smb2_request *smb2req,
225                                                uint32_t in_ctl_code,
226                                                uint64_t in_file_id_volatile,
227                                                DATA_BLOB in_input,
228                                                uint32_t in_max_output,
229                                                uint32_t in_flags)
230 {
231         struct tevent_req *req;
232         struct smbd_smb2_ioctl_state *state;
233         struct smb_request *smbreq;
234         files_struct *fsp = NULL;
235         struct tevent_req *subreq;
236
237         req = tevent_req_create(mem_ctx, &state,
238                                 struct smbd_smb2_ioctl_state);
239         if (req == NULL) {
240                 return NULL;
241         }
242         state->smb2req = smb2req;
243         state->smbreq = NULL;
244         state->fsp = NULL;
245         state->in_input = in_input;
246         state->in_max_output = in_max_output;
247         state->out_output = data_blob_null;
248
249         DEBUG(10,("smbd_smb2_ioctl: file_id[0x%016llX]\n",
250                   (unsigned long long)in_file_id_volatile));
251
252         smbreq = smbd_smb2_fake_smb_request(smb2req);
253         if (tevent_req_nomem(smbreq, req)) {
254                 return tevent_req_post(req, ev);
255         }
256         state->smbreq = smbreq;
257
258         if (in_file_id_volatile != UINT64_MAX) {
259                 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
260                 if (fsp == NULL) {
261                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
262                         return tevent_req_post(req, ev);
263                 }
264                 if (smbreq->conn != fsp->conn) {
265                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
266                         return tevent_req_post(req, ev);
267                 }
268                 if (smb2req->session->vuid != fsp->vuid) {
269                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
270                         return tevent_req_post(req, ev);
271                 }
272                 state->fsp = fsp;
273         }
274
275         switch (in_ctl_code) {
276         case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
277         {
278                 uint16_t in_max_referral_level;
279                 DATA_BLOB in_file_name_buffer;
280                 char *in_file_name_string;
281                 size_t in_file_name_string_size;
282                 bool ok;
283                 bool overflow = false;
284                 NTSTATUS status;
285                 int dfs_size;
286                 char *dfs_data = NULL;
287
288                 if (!IS_IPC(smbreq->conn)) {
289                         tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
290                         return tevent_req_post(req, ev);
291                 }
292
293                 if (!lp_host_msdfs()) {
294                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
295                         return tevent_req_post(req, ev);
296                 }
297
298                 if (in_input.length < (2 + 2)) {
299                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
300                         return tevent_req_post(req, ev);
301                 }
302
303                 in_max_referral_level = SVAL(in_input.data, 0);
304                 in_file_name_buffer.data = in_input.data + 2;
305                 in_file_name_buffer.length = in_input.length - 2;
306
307                 ok = convert_string_talloc(state, CH_UTF16, CH_UNIX,
308                                            in_file_name_buffer.data,
309                                            in_file_name_buffer.length,
310                                            &in_file_name_string,
311                                            &in_file_name_string_size, false);
312                 if (!ok) {
313                         tevent_req_nterror(req, NT_STATUS_ILLEGAL_CHARACTER);
314                         return tevent_req_post(req, ev);
315                 }
316
317                 dfs_size = setup_dfs_referral(smbreq->conn,
318                                               in_file_name_string,
319                                               in_max_referral_level,
320                                               &dfs_data, &status);
321                 if (dfs_size < 0) {
322                         tevent_req_nterror(req, status);
323                         return tevent_req_post(req, ev);
324                 }
325
326                 if (dfs_size > in_max_output) {
327                         /*
328                          * TODO: we need a testsuite for this
329                          */
330                         overflow = true;
331                         dfs_size = in_max_output;
332                 }
333
334                 state->out_output = data_blob_talloc(state,
335                                                      (uint8_t *)dfs_data,
336                                                      dfs_size);
337                 SAFE_FREE(dfs_data);
338                 if (dfs_size > 0 &&
339                     tevent_req_nomem(state->out_output.data, req)) {
340                         return tevent_req_post(req, ev);
341                 }
342
343                 if (overflow) {
344                         tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
345                 } else {
346                         tevent_req_done(req);
347                 }
348                 return tevent_req_post(req, ev);
349         }
350         case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
351
352                 if (!IS_IPC(smbreq->conn)) {
353                         tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
354                         return tevent_req_post(req, ev);
355                 }
356
357                 if (fsp == NULL) {
358                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
359                         return tevent_req_post(req, ev);
360                 }
361
362                 if (!fsp_is_np(fsp)) {
363                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
364                         return tevent_req_post(req, ev);
365                 }
366
367                 DEBUG(10,("smbd_smb2_ioctl_send: np_write_send of size %u\n",
368                         (unsigned int)in_input.length ));
369
370                 subreq = np_write_send(state, ev,
371                                        fsp->fake_file_handle,
372                                        in_input.data,
373                                        in_input.length);
374                 if (tevent_req_nomem(subreq, req)) {
375                         return tevent_req_post(req, ev);
376                 }
377                 tevent_req_set_callback(subreq,
378                                         smbd_smb2_ioctl_pipe_write_done,
379                                         req);
380                 return req;
381
382         default: {
383                 uint8_t *out_data = NULL;
384                 uint32_t out_data_len = 0;
385                 NTSTATUS status;
386
387                 if (fsp == NULL) {
388                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
389                         return tevent_req_post(req, ev);
390                 }
391
392                 status = smb_fsctl(fsp,
393                                        state,
394                                        in_ctl_code,
395                                        smbreq->flags2,
396                                        in_input.data,
397                                        in_input.length,
398                                        &out_data,
399                                        in_max_output,
400                                        &out_data_len);
401                 state->out_output = data_blob_const(out_data, out_data_len);
402                 if (NT_STATUS_IS_OK(status)) {
403                         tevent_req_done(req);
404                         return tevent_req_post(req, ev);
405                 }
406
407                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
408                         if (IS_IPC(smbreq->conn)) {
409                                 status = NT_STATUS_FS_DRIVER_REQUIRED;
410                         } else {
411                                 status = NT_STATUS_INVALID_DEVICE_REQUEST;
412                         }
413                 }
414
415                 tevent_req_nterror(req, status);
416                 return tevent_req_post(req, ev);
417         }
418         }
419
420         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
421         return tevent_req_post(req, ev);
422 }
423
424 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
425 {
426         struct tevent_req *req = tevent_req_callback_data(subreq,
427                                  struct tevent_req);
428         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
429                                               struct smbd_smb2_ioctl_state);
430         NTSTATUS status;
431         ssize_t nwritten = -1;
432
433         status = np_write_recv(subreq, &nwritten);
434
435         DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
436                 (long int)nwritten ));
437
438         TALLOC_FREE(subreq);
439         if (!NT_STATUS_IS_OK(status)) {
440                 tevent_req_nterror(req, status);
441                 return;
442         }
443
444         if (nwritten != state->in_input.length) {
445                 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
446                 return;
447         }
448
449         state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
450         if (state->in_max_output > 0 &&
451             tevent_req_nomem(state->out_output.data, req)) {
452                 return;
453         }
454
455         DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
456                 "of size %u\n",
457                 (unsigned int)state->out_output.length ));
458
459         TALLOC_FREE(subreq);
460         subreq = np_read_send(state->smbreq->conn,
461                               state->smb2req->sconn->smb2.event_ctx,
462                               state->fsp->fake_file_handle,
463                               state->out_output.data,
464                               state->out_output.length);
465         if (tevent_req_nomem(subreq, req)) {
466                 return;
467         }
468         tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
469 }
470
471 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
472 {
473         struct tevent_req *req = tevent_req_callback_data(subreq,
474                                  struct tevent_req);
475         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
476                                               struct smbd_smb2_ioctl_state);
477         NTSTATUS status;
478         ssize_t nread = -1;
479         bool is_data_outstanding = false;
480
481         status = np_read_recv(subreq, &nread, &is_data_outstanding);
482
483         DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
484                  "is_data_outstanding = %d, status = %s\n",
485                 (int)nread,
486                 (int)is_data_outstanding,
487                 nt_errstr(status) ));
488
489         TALLOC_FREE(subreq);
490         if (!NT_STATUS_IS_OK(status)) {
491                 tevent_req_nterror(req, status);
492                 return;
493         }
494
495         state->out_output.length = nread;
496
497         if (is_data_outstanding) {
498                 tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
499                 return;
500         }
501
502         tevent_req_done(req);
503 }
504
505 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
506                                      TALLOC_CTX *mem_ctx,
507                                      DATA_BLOB *out_output)
508 {
509         NTSTATUS status = NT_STATUS_OK;
510         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
511                                               struct smbd_smb2_ioctl_state);
512
513         if (tevent_req_is_nterror(req, &status)) {
514                 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
515                         tevent_req_received(req);
516                         return status;
517                 }
518         }
519
520         *out_output = state->out_output;
521         talloc_steal(mem_ctx, out_output->data);
522
523         tevent_req_received(req);
524         return status;
525 }