s3:smb2_server: use smbd_smb2_request_verify_sizes() in smb2_ioctl.c
[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: ctl_code[0x%08x] file_id[0x%016llX]\n",
241                    (unsigned)in_ctl_code,
242                    (unsigned long long)in_file_id_volatile));
243
244         smbreq = smbd_smb2_fake_smb_request(smb2req);
245         if (tevent_req_nomem(smbreq, req)) {
246                 return tevent_req_post(req, ev);
247         }
248         state->smbreq = smbreq;
249
250         if (in_file_id_volatile != UINT64_MAX) {
251                 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
252                 if (fsp == NULL) {
253                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
254                         return tevent_req_post(req, ev);
255                 }
256                 if (smbreq->conn != fsp->conn) {
257                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
258                         return tevent_req_post(req, ev);
259                 }
260                 if (smb2req->session->vuid != fsp->vuid) {
261                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
262                         return tevent_req_post(req, ev);
263                 }
264                 state->fsp = fsp;
265         }
266
267         switch (in_ctl_code) {
268         case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
269         {
270                 uint16_t in_max_referral_level;
271                 DATA_BLOB in_file_name_buffer;
272                 char *in_file_name_string;
273                 size_t in_file_name_string_size;
274                 bool ok;
275                 bool overflow = false;
276                 NTSTATUS status;
277                 int dfs_size;
278                 char *dfs_data = NULL;
279
280                 if (!IS_IPC(smbreq->conn)) {
281                         tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
282                         return tevent_req_post(req, ev);
283                 }
284
285                 if (!lp_host_msdfs()) {
286                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
287                         return tevent_req_post(req, ev);
288                 }
289
290                 if (in_input.length < (2 + 2)) {
291                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
292                         return tevent_req_post(req, ev);
293                 }
294
295                 in_max_referral_level = SVAL(in_input.data, 0);
296                 in_file_name_buffer.data = in_input.data + 2;
297                 in_file_name_buffer.length = in_input.length - 2;
298
299                 ok = convert_string_talloc(state, CH_UTF16, CH_UNIX,
300                                            in_file_name_buffer.data,
301                                            in_file_name_buffer.length,
302                                            &in_file_name_string,
303                                            &in_file_name_string_size);
304                 if (!ok) {
305                         tevent_req_nterror(req, NT_STATUS_ILLEGAL_CHARACTER);
306                         return tevent_req_post(req, ev);
307                 }
308
309                 dfs_size = setup_dfs_referral(smbreq->conn,
310                                               in_file_name_string,
311                                               in_max_referral_level,
312                                               &dfs_data, &status);
313                 if (dfs_size < 0) {
314                         tevent_req_nterror(req, status);
315                         return tevent_req_post(req, ev);
316                 }
317
318                 if (dfs_size > in_max_output) {
319                         /*
320                          * TODO: we need a testsuite for this
321                          */
322                         overflow = true;
323                         dfs_size = in_max_output;
324                 }
325
326                 state->out_output = data_blob_talloc(state,
327                                                      (uint8_t *)dfs_data,
328                                                      dfs_size);
329                 SAFE_FREE(dfs_data);
330                 if (dfs_size > 0 &&
331                     tevent_req_nomem(state->out_output.data, req)) {
332                         return tevent_req_post(req, ev);
333                 }
334
335                 if (overflow) {
336                         tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
337                 } else {
338                         tevent_req_done(req);
339                 }
340                 return tevent_req_post(req, ev);
341         }
342         case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
343
344                 if (!IS_IPC(smbreq->conn)) {
345                         tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
346                         return tevent_req_post(req, ev);
347                 }
348
349                 if (fsp == NULL) {
350                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
351                         return tevent_req_post(req, ev);
352                 }
353
354                 if (!fsp_is_np(fsp)) {
355                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
356                         return tevent_req_post(req, ev);
357                 }
358
359                 DEBUG(10,("smbd_smb2_ioctl_send: np_write_send of size %u\n",
360                         (unsigned int)in_input.length ));
361
362                 subreq = np_write_send(state, ev,
363                                        fsp->fake_file_handle,
364                                        in_input.data,
365                                        in_input.length);
366                 if (tevent_req_nomem(subreq, req)) {
367                         return tevent_req_post(req, ev);
368                 }
369                 tevent_req_set_callback(subreq,
370                                         smbd_smb2_ioctl_pipe_write_done,
371                                         req);
372                 return req;
373
374         case 0x00144064:        /* FSCTL_SRV_ENUMERATE_SNAPSHOTS */
375         {
376                 /*
377                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
378                  * and return their volume names.  If max_data_count is 16, then it is just
379                  * asking for the number of volumes and length of the combined names.
380                  *
381                  * pdata is the data allocated by our caller, but that uses
382                  * total_data_count (which is 0 in our case) rather than max_data_count.
383                  * Allocate the correct amount and return the pointer to let
384                  * it be deallocated when we return.
385                  */
386                 struct shadow_copy_data *shadow_data = NULL;
387                 bool labels = False;
388                 uint32_t labels_data_count = 0;
389                 uint32_t data_count;
390                 uint32_t i;
391                 char *pdata;
392                 NTSTATUS status;
393
394                 if (fsp == NULL) {
395                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
396                         return tevent_req_post(req, ev);
397                 }
398
399                 if (in_max_output < 16) {
400                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
401                                  "in_max_output(%u) < 16 is invalid!\n",
402                                  in_max_output));
403                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
404                         return tevent_req_post(req, ev);
405                 }
406
407                 if (in_max_output > 16) {
408                         labels = True;
409                 }
410
411                 shadow_data = talloc_zero(talloc_tos(),
412                                             struct shadow_copy_data);
413                 if (tevent_req_nomem(shadow_data, req)) {
414                         DEBUG(0,("TALLOC_ZERO() failed!\n"));
415                         return tevent_req_post(req, ev);
416                 }
417
418                 /*
419                  * Call the VFS routine to actually do the work.
420                  */
421                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)
422                     != 0) {
423                         if (errno == ENOSYS) {
424                                 DEBUG(5, ("FSCTL_GET_SHADOW_COPY_DATA: "
425                                           "connectpath %s, not supported.\n",
426                                           smbreq->conn->connectpath));
427                                 status = NT_STATUS_NOT_SUPPORTED;
428                         } else {
429                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: "
430                                          "connectpath %s, failed.\n",
431                                          smbreq->conn->connectpath));
432                                 status = map_nt_error_from_unix(errno);
433                         }
434                         TALLOC_FREE(shadow_data);
435                         tevent_req_nterror(req, status);
436                         return tevent_req_post(req, ev);
437                 }
438
439                 labels_data_count =
440                         (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))
441                         + 2;
442
443                 if (labels) {
444                         data_count = 12+labels_data_count+4;
445                 } else {
446                         data_count = 16;
447                 }
448
449                 if (labels && (in_max_output < data_count)) {
450                         DEBUG(0, ("FSCTL_GET_SHADOW_COPY_DATA: "
451                                   "in_max_output(%u) too small (%u) bytes "
452                                   "needed!\n", in_max_output, data_count));
453                         TALLOC_FREE(shadow_data);
454                         tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
455                         return tevent_req_post(req, ev);
456                 }
457
458                 state->out_output = data_blob_talloc(state, NULL, data_count);
459                 if (tevent_req_nomem(state->out_output.data, req)) {
460                         return tevent_req_post(req, ev);
461                 }
462
463                 pdata = (char *)state->out_output.data;
464
465                 /* num_volumes 4 bytes */
466                 SIVAL(pdata, 0, shadow_data->num_volumes);
467
468                 if (labels) {
469                         /* num_labels 4 bytes */
470                         SIVAL(pdata, 4, shadow_data->num_volumes);
471                 }
472
473                 /* needed_data_count 4 bytes */
474                 SIVAL(pdata, 8, labels_data_count+4);
475
476                 pdata += 12;
477
478                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for "
479                           "path[%s].\n",
480                           shadow_data->num_volumes, fsp_str_dbg(fsp)));
481                 if (labels && shadow_data->labels) {
482                         for (i=0; i<shadow_data->num_volumes; i++) {
483                                 srvstr_push(pdata, smbreq->flags2,
484                                             pdata, shadow_data->labels[i],
485                                             2*sizeof(SHADOW_COPY_LABEL),
486                                             STR_UNICODE|STR_TERMINATE);
487                                 pdata += 2*sizeof(SHADOW_COPY_LABEL);
488                                 DEBUGADD(10, ("Label[%u]: '%s'\n", i,
489                                               shadow_data->labels[i]));
490                         }
491                 }
492
493                 TALLOC_FREE(shadow_data);
494
495                 tevent_req_done(req);
496                 return tevent_req_post(req, ev);
497         }
498
499         default:
500                 if (IS_IPC(smbreq->conn)) {
501                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
502                         return tevent_req_post(req, ev);
503                 }
504                 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
505                 return tevent_req_post(req, ev);
506         }
507
508         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
509         return tevent_req_post(req, ev);
510 }
511
512 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
513 {
514         struct tevent_req *req = tevent_req_callback_data(subreq,
515                                  struct tevent_req);
516         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
517                                               struct smbd_smb2_ioctl_state);
518         NTSTATUS status;
519         ssize_t nwritten = -1;
520
521         status = np_write_recv(subreq, &nwritten);
522
523         DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
524                 (long int)nwritten ));
525
526         TALLOC_FREE(subreq);
527         if (!NT_STATUS_IS_OK(status)) {
528                 tevent_req_nterror(req, status);
529                 return;
530         }
531
532         if (nwritten != state->in_input.length) {
533                 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
534                 return;
535         }
536
537         state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
538         if (state->in_max_output > 0 &&
539             tevent_req_nomem(state->out_output.data, req)) {
540                 return;
541         }
542
543         DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
544                 "of size %u\n",
545                 (unsigned int)state->out_output.length ));
546
547         TALLOC_FREE(subreq);
548         subreq = np_read_send(state->smbreq->conn,
549                               state->smb2req->sconn->smb2.event_ctx,
550                               state->fsp->fake_file_handle,
551                               state->out_output.data,
552                               state->out_output.length);
553         if (tevent_req_nomem(subreq, req)) {
554                 return;
555         }
556         tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
557 }
558
559 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
560 {
561         struct tevent_req *req = tevent_req_callback_data(subreq,
562                                  struct tevent_req);
563         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
564                                               struct smbd_smb2_ioctl_state);
565         NTSTATUS status;
566         ssize_t nread = -1;
567         bool is_data_outstanding = false;
568
569         status = np_read_recv(subreq, &nread, &is_data_outstanding);
570
571         DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
572                  "is_data_outstanding = %d, status = %s\n",
573                 (int)nread,
574                 (int)is_data_outstanding,
575                 nt_errstr(status) ));
576
577         TALLOC_FREE(subreq);
578         if (!NT_STATUS_IS_OK(status)) {
579                 tevent_req_nterror(req, status);
580                 return;
581         }
582
583         state->out_output.length = nread;
584
585         if (is_data_outstanding) {
586                 tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
587                 return;
588         }
589
590         tevent_req_done(req);
591 }
592
593 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
594                                      TALLOC_CTX *mem_ctx,
595                                      DATA_BLOB *out_output)
596 {
597         NTSTATUS status = NT_STATUS_OK;
598         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
599                                               struct smbd_smb2_ioctl_state);
600
601         if (tevent_req_is_nterror(req, &status)) {
602                 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
603                         tevent_req_received(req);
604                         return status;
605                 }
606         }
607
608         *out_output = state->out_output;
609         talloc_steal(mem_ctx, out_output->data);
610
611         tevent_req_received(req);
612         return status;
613 }