s3-talloc Change TALLOC_ZERO_P() to talloc_zero()
[nivanova/samba-autobuild/.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         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;
58
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);
62         }
63
64         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
65
66         body_size = SVAL(inbody, 0x00);
67         if (body_size != expected_body_size) {
68                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
69         }
70
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);
78
79         if (in_input_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
80                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
81         }
82
83         if (in_input_length > req->in.vector[i+2].iov_len) {
84                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
85         }
86
87         in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
88         in_input_buffer.length = in_input_length;
89
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);
97         }
98
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);
109         }
110         tevent_req_set_callback(subreq, smbd_smb2_request_ioctl_done, req);
111
112         return smbd_smb2_request_pending_queue(req, subreq);
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
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) ));
139
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;
149                 }
150                 return;
151         }
152
153         out_input_offset = SMB2_HDR_BODY + 0x30;
154         out_output_offset = SMB2_HDR_BODY + 0x30;
155
156         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
157
158         in_ctl_code             = IVAL(inbody, 0x04);
159         in_file_id_persistent   = BVAL(inbody, 0x08);
160         in_file_id_volatile     = BVAL(inbody, 0x10);
161
162         outhdr = (uint8_t *)req->out.vector[i].iov_base;
163
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;
171                 }
172                 return;
173         }
174
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 */
192
193         /*
194          * Note: Windows Vista and 2008 send back also the
195          *       input from the request. But it was fixed in
196          *       Windows 7.
197          */
198         outdyn = out_output_buffer;
199
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;
206         }
207 }
208
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;
216 };
217
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);
220
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)
229 {
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;
235
236         req = tevent_req_create(mem_ctx, &state,
237                                 struct smbd_smb2_ioctl_state);
238         if (req == NULL) {
239                 return NULL;
240         }
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;
247
248         DEBUG(10, ("smbd_smb2_ioctl: ctl_code[0x%08x] file_id[0x%016llX]\n",
249                    (unsigned)in_ctl_code,
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);
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         case 0x00144064:        /* FSCTL_SRV_ENUMERATE_SNAPSHOTS */
383         {
384                 /*
385                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
386                  * and return their volume names.  If max_data_count is 16, then it is just
387                  * asking for the number of volumes and length of the combined names.
388                  *
389                  * pdata is the data allocated by our caller, but that uses
390                  * total_data_count (which is 0 in our case) rather than max_data_count.
391                  * Allocate the correct amount and return the pointer to let
392                  * it be deallocated when we return.
393                  */
394                 struct shadow_copy_data *shadow_data = NULL;
395                 bool labels = False;
396                 uint32_t labels_data_count = 0;
397                 uint32_t data_count;
398                 uint32_t i;
399                 char *pdata;
400                 NTSTATUS status;
401
402                 if (in_max_output < 16) {
403                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
404                                  "in_max_output(%u) < 16 is invalid!\n",
405                                  in_max_output));
406                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
407                         return tevent_req_post(req, ev);
408                 }
409
410                 if (in_max_output > 16) {
411                         labels = True;
412                 }
413
414                 shadow_data = talloc_zero(talloc_tos(),
415                                             struct shadow_copy_data);
416                 if (tevent_req_nomem(shadow_data, req)) {
417                         DEBUG(0,("TALLOC_ZERO() failed!\n"));
418                         return tevent_req_post(req, ev);
419                 }
420
421                 /*
422                  * Call the VFS routine to actually do the work.
423                  */
424                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)
425                     != 0) {
426                         if (errno == ENOSYS) {
427                                 DEBUG(5, ("FSCTL_GET_SHADOW_COPY_DATA: "
428                                           "connectpath %s, not supported.\n",
429                                           smbreq->conn->connectpath));
430                                 status = NT_STATUS_NOT_SUPPORTED;
431                         } else {
432                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
433                                          "connectpath %s, failed.\n",
434                                          smbreq->conn->connectpath));
435                                 status = map_nt_error_from_unix(errno);
436                         }
437                         TALLOC_FREE(shadow_data);
438                         tevent_req_nterror(req, status);
439                         return tevent_req_post(req, ev);
440                 }
441
442                 labels_data_count =
443                         (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))
444                         + 2;
445
446                 if (labels) {
447                         data_count = 12+labels_data_count+4;
448                 } else {
449                         data_count = 16;
450                 }
451
452                 if (labels && (in_max_output < data_count)) {
453                         DEBUG(0, ("FSCTL_GET_SHADOW_COPY_DATA: "
454                                   "in_max_output(%u) too small (%u) bytes "
455                                   "needed!\n", in_max_output, data_count));
456                         TALLOC_FREE(shadow_data);
457                         tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
458                         return tevent_req_post(req, ev);
459                 }
460
461                 state->out_output = data_blob_talloc(state, NULL, data_count);
462                 if (tevent_req_nomem(state->out_output.data, req)) {
463                         return tevent_req_post(req, ev);
464                 }
465
466                 pdata = (char *)state->out_output.data;
467
468                 /* num_volumes 4 bytes */
469                 SIVAL(pdata, 0, shadow_data->num_volumes);
470
471                 if (labels) {
472                         /* num_labels 4 bytes */
473                         SIVAL(pdata, 4, shadow_data->num_volumes);
474                 }
475
476                 /* needed_data_count 4 bytes */
477                 SIVAL(pdata, 8, labels_data_count+4);
478
479                 pdata += 12;
480
481                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for "
482                           "path[%s].\n",
483                           shadow_data->num_volumes, fsp_str_dbg(fsp)));
484                 if (labels && shadow_data->labels) {
485                         for (i=0; i<shadow_data->num_volumes; i++) {
486                                 srvstr_push(pdata, smbreq->flags2,
487                                             pdata, shadow_data->labels[i],
488                                             2*sizeof(SHADOW_COPY_LABEL),
489                                             STR_UNICODE|STR_TERMINATE);
490                                 pdata += 2*sizeof(SHADOW_COPY_LABEL);
491                                 DEBUGADD(10, ("Label[%u]: '%s'\n", i,
492                                               shadow_data->labels[i]));
493                         }
494                 }
495
496                 TALLOC_FREE(shadow_data);
497
498                 tevent_req_done(req);
499                 return tevent_req_post(req, ev);
500         }
501
502         default:
503                 if (IS_IPC(smbreq->conn)) {
504                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
505                         return tevent_req_post(req, ev);
506                 }
507                 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
508                 return tevent_req_post(req, ev);
509         }
510
511         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
512         return tevent_req_post(req, ev);
513 }
514
515 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
516 {
517         struct tevent_req *req = tevent_req_callback_data(subreq,
518                                  struct tevent_req);
519         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
520                                               struct smbd_smb2_ioctl_state);
521         NTSTATUS status;
522         ssize_t nwritten = -1;
523
524         status = np_write_recv(subreq, &nwritten);
525
526         DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
527                 (long int)nwritten ));
528
529         TALLOC_FREE(subreq);
530         if (!NT_STATUS_IS_OK(status)) {
531                 tevent_req_nterror(req, status);
532                 return;
533         }
534
535         if (nwritten != state->in_input.length) {
536                 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
537                 return;
538         }
539
540         state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
541         if (state->in_max_output > 0 &&
542             tevent_req_nomem(state->out_output.data, req)) {
543                 return;
544         }
545
546         DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
547                 "of size %u\n",
548                 (unsigned int)state->out_output.length ));
549
550         TALLOC_FREE(subreq);
551         subreq = np_read_send(state->smbreq->conn,
552                               state->smb2req->sconn->smb2.event_ctx,
553                               state->fsp->fake_file_handle,
554                               state->out_output.data,
555                               state->out_output.length);
556         if (tevent_req_nomem(subreq, req)) {
557                 return;
558         }
559         tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
560 }
561
562 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
563 {
564         struct tevent_req *req = tevent_req_callback_data(subreq,
565                                  struct tevent_req);
566         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
567                                               struct smbd_smb2_ioctl_state);
568         NTSTATUS status;
569         ssize_t nread = -1;
570         bool is_data_outstanding = false;
571
572         status = np_read_recv(subreq, &nread, &is_data_outstanding);
573
574         DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
575                  "is_data_outstanding = %d, status = %s\n",
576                 (int)nread,
577                 (int)is_data_outstanding,
578                 nt_errstr(status) ));
579
580         TALLOC_FREE(subreq);
581         if (!NT_STATUS_IS_OK(status)) {
582                 tevent_req_nterror(req, status);
583                 return;
584         }
585
586         state->out_output.length = nread;
587
588         tevent_req_done(req);
589 }
590
591 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
592                                      TALLOC_CTX *mem_ctx,
593                                      DATA_BLOB *out_output)
594 {
595         NTSTATUS status = NT_STATUS_OK;
596         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
597                                               struct smbd_smb2_ioctl_state);
598
599         if (tevent_req_is_nterror(req, &status)) {
600                 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
601                         tevent_req_received(req);
602                         return status;
603                 }
604         }
605
606         *out_output = state->out_output;
607         talloc_steal(mem_ctx, out_output->data);
608
609         tevent_req_received(req);
610         return status;
611 }