s3:smb2_server: use smbd_smb2_request_verify_sizes() in smb2_ioctl.c
[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         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);
73         }
74
75         if (in_input_length > req->in.vector[i+2].iov_len) {
76                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77         }
78
79         in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
80         in_input_buffer.length = in_input_length;
81
82         if (req->compat_chain_fsp) {
83                 /* skip check */
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);
89         }
90
91         subreq = smbd_smb2_ioctl_send(req,
92                                       req->sconn->smb2.event_ctx,
93                                       req,
94                                       in_ctl_code,
95                                       in_file_id_volatile,
96                                       in_input_buffer,
97                                       in_max_output_length,
98                                       in_flags);
99         if (subreq == NULL) {
100                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
101         }
102         tevent_req_set_callback(subreq, smbd_smb2_request_ioctl_done, req);
103
104         return smbd_smb2_request_pending_queue(req, subreq);
105 }
106
107 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq)
108 {
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;
113         uint8_t *outhdr;
114         DATA_BLOB outbody;
115         DATA_BLOB outdyn;
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;
122         NTSTATUS status;
123         NTSTATUS error; /* transport error */
124
125         status = smbd_smb2_ioctl_recv(subreq, req, &out_output_buffer);
126
127         DEBUG(10,("smbd_smb2_request_ioctl_done: smbd_smb2_ioctl_recv returned "
128                 "%u status %s\n",
129                 (unsigned int)out_output_buffer.length,
130                 nt_errstr(status) ));
131
132         TALLOC_FREE(subreq);
133         if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
134                 /* also ok */
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,
139                                                          nt_errstr(error));
140                         return;
141                 }
142                 return;
143         }
144
145         out_input_offset = SMB2_HDR_BODY + 0x30;
146         out_output_offset = SMB2_HDR_BODY + 0x30;
147
148         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
149
150         in_ctl_code             = IVAL(inbody, 0x04);
151         in_file_id_persistent   = BVAL(inbody, 0x08);
152         in_file_id_volatile     = BVAL(inbody, 0x10);
153
154         outhdr = (uint8_t *)req->out.vector[i].iov_base;
155
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,
161                                                          nt_errstr(error));
162                         return;
163                 }
164                 return;
165         }
166
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 */
184
185         /*
186          * Note: Windows Vista and 2008 send back also the
187          *       input from the request. But it was fixed in
188          *       Windows 7.
189          */
190         outdyn = out_output_buffer;
191
192         error = smbd_smb2_request_done_ex(req, status, outbody, &outdyn,
193                                           __location__);
194         if (!NT_STATUS_IS_OK(error)) {
195                 smbd_server_connection_terminate(req->sconn,
196                                                  nt_errstr(error));
197                 return;
198         }
199 }
200
201 struct smbd_smb2_ioctl_state {
202         struct smbd_smb2_request *smb2req;
203         struct smb_request *smbreq;
204         files_struct *fsp;
205         DATA_BLOB in_input;
206         uint32_t in_max_output;
207         DATA_BLOB out_output;
208 };
209
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);
212
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,
218                                                DATA_BLOB in_input,
219                                                uint32_t in_max_output,
220                                                uint32_t in_flags)
221 {
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;
227
228         req = tevent_req_create(mem_ctx, &state,
229                                 struct smbd_smb2_ioctl_state);
230         if (req == NULL) {
231                 return NULL;
232         }
233         state->smb2req = smb2req;
234         state->smbreq = NULL;
235         state->fsp = NULL;
236         state->in_input = in_input;
237         state->in_max_output = in_max_output;
238         state->out_output = data_blob_null;
239
240         DEBUG(10,("smbd_smb2_ioctl: file_id[0x%016llX]\n",
241                   (unsigned long long)in_file_id_volatile));
242
243         smbreq = smbd_smb2_fake_smb_request(smb2req);
244         if (tevent_req_nomem(smbreq, req)) {
245                 return tevent_req_post(req, ev);
246         }
247         state->smbreq = smbreq;
248
249         if (in_file_id_volatile != UINT64_MAX) {
250                 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
251                 if (fsp == NULL) {
252                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
253                         return tevent_req_post(req, ev);
254                 }
255                 if (smbreq->conn != fsp->conn) {
256                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
257                         return tevent_req_post(req, ev);
258                 }
259                 if (smb2req->session->vuid != fsp->vuid) {
260                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
261                         return tevent_req_post(req, ev);
262                 }
263                 state->fsp = fsp;
264         }
265
266         switch (in_ctl_code) {
267         case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
268         {
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;
273                 bool ok;
274                 bool overflow = false;
275                 NTSTATUS status;
276                 int dfs_size;
277                 char *dfs_data = NULL;
278
279                 if (!IS_IPC(smbreq->conn)) {
280                         tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
281                         return tevent_req_post(req, ev);
282                 }
283
284                 if (!lp_host_msdfs()) {
285                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
286                         return tevent_req_post(req, ev);
287                 }
288
289                 if (in_input.length < (2 + 2)) {
290                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
291                         return tevent_req_post(req, ev);
292                 }
293
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;
297
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);
303                 if (!ok) {
304                         tevent_req_nterror(req, NT_STATUS_ILLEGAL_CHARACTER);
305                         return tevent_req_post(req, ev);
306                 }
307
308                 dfs_size = setup_dfs_referral(smbreq->conn,
309                                               in_file_name_string,
310                                               in_max_referral_level,
311                                               &dfs_data, &status);
312                 if (dfs_size < 0) {
313                         tevent_req_nterror(req, status);
314                         return tevent_req_post(req, ev);
315                 }
316
317                 if (dfs_size > in_max_output) {
318                         /*
319                          * TODO: we need a testsuite for this
320                          */
321                         overflow = true;
322                         dfs_size = in_max_output;
323                 }
324
325                 state->out_output = data_blob_talloc(state,
326                                                      (uint8_t *)dfs_data,
327                                                      dfs_size);
328                 SAFE_FREE(dfs_data);
329                 if (dfs_size > 0 &&
330                     tevent_req_nomem(state->out_output.data, req)) {
331                         return tevent_req_post(req, ev);
332                 }
333
334                 if (overflow) {
335                         tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
336                 } else {
337                         tevent_req_done(req);
338                 }
339                 return tevent_req_post(req, ev);
340         }
341         case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
342
343                 if (!IS_IPC(smbreq->conn)) {
344                         tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
345                         return tevent_req_post(req, ev);
346                 }
347
348                 if (fsp == NULL) {
349                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
350                         return tevent_req_post(req, ev);
351                 }
352
353                 if (!fsp_is_np(fsp)) {
354                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
355                         return tevent_req_post(req, ev);
356                 }
357
358                 DEBUG(10,("smbd_smb2_ioctl_send: np_write_send of size %u\n",
359                         (unsigned int)in_input.length ));
360
361                 subreq = np_write_send(state, ev,
362                                        fsp->fake_file_handle,
363                                        in_input.data,
364                                        in_input.length);
365                 if (tevent_req_nomem(subreq, req)) {
366                         return tevent_req_post(req, ev);
367                 }
368                 tevent_req_set_callback(subreq,
369                                         smbd_smb2_ioctl_pipe_write_done,
370                                         req);
371                 return req;
372
373         case 0x00144064:        /* FSCTL_SRV_ENUMERATE_SNAPSHOTS */
374         {
375                 /*
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.
379                  *
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.
384                  */
385                 struct shadow_copy_data *shadow_data = NULL;
386                 bool labels = False;
387                 uint32_t labels_data_count = 0;
388                 uint32_t data_count;
389                 uint32_t i;
390                 char *pdata;
391                 NTSTATUS status;
392
393                 if (fsp == NULL) {
394                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
395                         return tevent_req_post(req, ev);
396                 }
397
398                 if (in_max_output < 16) {
399                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
400                                  "in_max_output(%u) < 16 is invalid!\n",
401                                  in_max_output));
402                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
403                         return tevent_req_post(req, ev);
404                 }
405
406                 if (in_max_output > 16) {
407                         labels = True;
408                 }
409
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);
415                 }
416
417                 /*
418                  * Call the VFS routine to actually do the work.
419                  */
420                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)
421                     != 0) {
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;
427                         } else {
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);
432                         }
433                         TALLOC_FREE(shadow_data);
434                         tevent_req_nterror(req, status);
435                         return tevent_req_post(req, ev);
436                 }
437
438                 labels_data_count =
439                         (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))
440                         + 2;
441
442                 if (labels) {
443                         data_count = 12+labels_data_count+4;
444                 } else {
445                         data_count = 16;
446                 }
447
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);
455                 }
456
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);
460                 }
461
462                 pdata = (char *)state->out_output.data;
463
464                 /* num_volumes 4 bytes */
465                 SIVAL(pdata, 0, shadow_data->num_volumes);
466
467                 if (labels) {
468                         /* num_labels 4 bytes */
469                         SIVAL(pdata, 4, shadow_data->num_volumes);
470                 }
471
472                 /* needed_data_count 4 bytes */
473                 SIVAL(pdata, 8, labels_data_count+4);
474
475                 pdata += 12;
476
477                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for "
478                           "path[%s].\n",
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]));
489                         }
490                 }
491
492                 TALLOC_FREE(shadow_data);
493
494                 tevent_req_done(req);
495                 return tevent_req_post(req, ev);
496         }
497
498         default:
499                 if (IS_IPC(smbreq->conn)) {
500                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
501                         return tevent_req_post(req, ev);
502                 }
503                 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
504                 return tevent_req_post(req, ev);
505         }
506
507         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
508         return tevent_req_post(req, ev);
509 }
510
511 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
512 {
513         struct tevent_req *req = tevent_req_callback_data(subreq,
514                                  struct tevent_req);
515         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
516                                               struct smbd_smb2_ioctl_state);
517         NTSTATUS status;
518         ssize_t nwritten = -1;
519
520         status = np_write_recv(subreq, &nwritten);
521
522         DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
523                 (long int)nwritten ));
524
525         TALLOC_FREE(subreq);
526         if (!NT_STATUS_IS_OK(status)) {
527                 tevent_req_nterror(req, status);
528                 return;
529         }
530
531         if (nwritten != state->in_input.length) {
532                 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
533                 return;
534         }
535
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)) {
539                 return;
540         }
541
542         DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
543                 "of size %u\n",
544                 (unsigned int)state->out_output.length ));
545
546         TALLOC_FREE(subreq);
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)) {
553                 return;
554         }
555         tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
556 }
557
558 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
559 {
560         struct tevent_req *req = tevent_req_callback_data(subreq,
561                                  struct tevent_req);
562         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
563                                               struct smbd_smb2_ioctl_state);
564         NTSTATUS status;
565         ssize_t nread = -1;
566         bool is_data_outstanding = false;
567
568         status = np_read_recv(subreq, &nread, &is_data_outstanding);
569
570         DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
571                  "is_data_outstanding = %d, status = %s\n",
572                 (int)nread,
573                 (int)is_data_outstanding,
574                 nt_errstr(status) ));
575
576         TALLOC_FREE(subreq);
577         if (!NT_STATUS_IS_OK(status)) {
578                 tevent_req_nterror(req, status);
579                 return;
580         }
581
582         state->out_output.length = nread;
583
584         if (is_data_outstanding) {
585                 tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
586                 return;
587         }
588
589         tevent_req_done(req);
590 }
591
592 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
593                                      TALLOC_CTX *mem_ctx,
594                                      DATA_BLOB *out_output)
595 {
596         NTSTATUS status = NT_STATUS_OK;
597         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
598                                               struct smbd_smb2_ioctl_state);
599
600         if (tevent_req_is_nterror(req, &status)) {
601                 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
602                         tevent_req_received(req);
603                         return status;
604                 }
605         }
606
607         *out_output = state->out_output;
608         talloc_steal(mem_ctx, out_output->data);
609
610         tevent_req_received(req);
611         return status;
612 }