s3/smbd: allow GET_DFS_REFERRAL fsctl on any smb2 connexion
[metze/samba/wip.git] / source3 / smbd / smb2_ioctl_dfs.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 "include/ntioctl.h"
27 #include "smb2_ioctl_private.h"
28
29 static NTSTATUS fsctl_dfs_get_refers(TALLOC_CTX *mem_ctx,
30                                      struct tevent_context *ev,
31                                      struct connection_struct *conn,
32                                      DATA_BLOB *in_input,
33                                      uint32_t in_max_output,
34                                      DATA_BLOB *out_output)
35 {
36         uint16_t in_max_referral_level;
37         DATA_BLOB in_file_name_buffer;
38         char *in_file_name_string;
39         size_t in_file_name_string_size;
40         bool ok;
41         bool overflow = false;
42         NTSTATUS status;
43         int dfs_size;
44         char *dfs_data = NULL;
45         DATA_BLOB output;
46
47         if (!lp_host_msdfs()) {
48                 return NT_STATUS_FS_DRIVER_REQUIRED;
49         }
50
51         if (in_input->length < (2 + 2)) {
52                 return NT_STATUS_INVALID_PARAMETER;
53         }
54
55         in_max_referral_level = SVAL(in_input->data, 0);
56         in_file_name_buffer.data = in_input->data + 2;
57         in_file_name_buffer.length = in_input->length - 2;
58
59         ok = convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
60                                    in_file_name_buffer.data,
61                                    in_file_name_buffer.length,
62                                    &in_file_name_string,
63                                    &in_file_name_string_size);
64         if (!ok) {
65                 return NT_STATUS_ILLEGAL_CHARACTER;
66         }
67
68         dfs_size = setup_dfs_referral(conn,
69                                       in_file_name_string,
70                                       in_max_referral_level,
71                                       &dfs_data, &status);
72         if (dfs_size < 0) {
73                 return status;
74         }
75
76         if (dfs_size > in_max_output) {
77                 /*
78                  * TODO: we need a testsuite for this
79                  */
80                 overflow = true;
81                 dfs_size = in_max_output;
82         }
83
84         output = data_blob_talloc(mem_ctx, (uint8_t *)dfs_data, dfs_size);
85         SAFE_FREE(dfs_data);
86         if ((dfs_size > 0) && (output.data == NULL)) {
87                 return NT_STATUS_NO_MEMORY;
88         }
89         *out_output = output;
90
91         if (overflow) {
92                 return STATUS_BUFFER_OVERFLOW;
93         }
94         return NT_STATUS_OK;
95 }
96
97 struct tevent_req *smb2_ioctl_dfs(uint32_t ctl_code,
98                                   struct tevent_context *ev,
99                                   struct tevent_req *req,
100                                   struct smbd_smb2_ioctl_state *state)
101 {
102         NTSTATUS status;
103
104         switch (ctl_code) {
105         case FSCTL_DFS_GET_REFERRALS:
106                 status = fsctl_dfs_get_refers(state, ev, state->smbreq->conn,
107                                               &state->in_input,
108                                               state->in_max_output,
109                                               &state->out_output);
110                 if (!tevent_req_nterror(req, status)) {
111                         tevent_req_done(req);
112                 }
113                 return tevent_req_post(req, ev);
114                 break;
115         default: {
116                 uint8_t *out_data = NULL;
117                 uint32_t out_data_len = 0;
118
119                 if (state->fsp == NULL) {
120                         status = NT_STATUS_NOT_SUPPORTED;
121                 } else {
122                         status = SMB_VFS_FSCTL(state->fsp,
123                                                state,
124                                                ctl_code,
125                                                state->smbreq->flags2,
126                                                state->in_input.data,
127                                                state->in_input.length,
128                                                &out_data,
129                                                state->in_max_output,
130                                                &out_data_len);
131                         state->out_output = data_blob_const(out_data, out_data_len);
132                         if (NT_STATUS_IS_OK(status)) {
133                                 tevent_req_done(req);
134                                 return tevent_req_post(req, ev);
135                         }
136                 }
137
138                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
139                         if (IS_IPC(state->smbreq->conn)) {
140                                 status = NT_STATUS_FS_DRIVER_REQUIRED;
141                         } else {
142                                 status = NT_STATUS_INVALID_DEVICE_REQUEST;
143                         }
144                 }
145
146                 tevent_req_nterror(req, status);
147                 return tevent_req_post(req, ev);
148                 break;
149         }
150         }
151
152         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
153         return tevent_req_post(req, ev);
154 }