s3:smbd: fix the build in smb2_ioctl.c
[ira/wip.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/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
24
25 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
26                                                struct tevent_context *ev,
27                                                struct smbd_smb2_request *smb2req,
28                                                uint32_t in_ctl_code,
29                                                uint64_t in_file_id_volatile,
30                                                DATA_BLOB in_input,
31                                                uint32_t in_max_output,
32                                                uint32_t in_flags);
33 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
34                                      TALLOC_CTX *mem_ctx,
35                                      DATA_BLOB *out_output);
36
37 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq);
38 NTSTATUS smbd_smb2_request_process_ioctl(struct smbd_smb2_request *req)
39 {
40         const uint8_t *inhdr;
41         const uint8_t *inbody;
42         int i = req->current_idx;
43         size_t expected_body_size = 0x39;
44         size_t body_size;
45         uint32_t in_ctl_code;
46         uint64_t in_file_id_persistent;
47         uint64_t in_file_id_volatile;
48         uint32_t in_input_offset;
49         uint32_t in_input_length;
50         DATA_BLOB in_input_buffer;
51         uint32_t in_max_output_length;
52         uint32_t in_flags;
53         struct tevent_req *subreq;
54
55         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
56         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
57                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
58         }
59
60         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
61
62         body_size = SVAL(inbody, 0x00);
63         if (body_size != expected_body_size) {
64                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
65         }
66
67         in_ctl_code             = IVAL(inbody, 0x04);
68         in_file_id_persistent   = BVAL(inbody, 0x08);
69         in_file_id_volatile     = BVAL(inbody, 0x10);
70         in_input_offset         = IVAL(inbody, 0x18);
71         in_input_length         = IVAL(inbody, 0x1C);
72         in_max_output_length    = IVAL(inbody, 0x2C);
73         in_flags                = IVAL(inbody, 0x30);
74
75         if (in_input_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
76                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77         }
78
79         if (in_input_length > req->in.vector[i+2].iov_len) {
80                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
81         }
82
83         in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
84         in_input_buffer.length = in_input_length;
85
86         if (in_file_id_persistent == UINT64_MAX &&
87             in_file_id_volatile == UINT64_MAX) {
88                 /* without a handle */
89         } else if (in_file_id_persistent != 0) {
90                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
91         }
92
93         subreq = smbd_smb2_ioctl_send(req,
94                                       req->conn->smb2.event_ctx,
95                                       req,
96                                       in_ctl_code,
97                                       in_file_id_volatile,
98                                       in_input_buffer,
99                                       in_max_output_length,
100                                       in_flags);
101         if (subreq == NULL) {
102                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
103         }
104         tevent_req_set_callback(subreq, smbd_smb2_request_ioctl_done, req);
105         return NT_STATUS_OK;
106 }
107
108 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq)
109 {
110         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
111                                         struct smbd_smb2_request);
112         int i = req->current_idx;
113         uint8_t *outhdr;
114         DATA_BLOB outbody;
115         DATA_BLOB outdyn;
116         uint32_t out_output_offset;
117         DATA_BLOB out_output_buffer;
118         NTSTATUS status;
119         NTSTATUS error; /* transport error */
120
121         status = smbd_smb2_ioctl_recv(subreq, req, &out_output_buffer);
122         TALLOC_FREE(subreq);
123         if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
124                 /* also ok */
125         } else if (!NT_STATUS_IS_OK(status)) {
126                 error = smbd_smb2_request_error(req, status);
127                 if (!NT_STATUS_IS_OK(error)) {
128                         smbd_server_connection_terminate(req->conn,
129                                                          nt_errstr(error));
130                         return;
131                 }
132                 return;
133         }
134
135         out_output_offset = SMB2_HDR_BODY + 0x30;
136
137         outhdr = (uint8_t *)req->out.vector[i].iov_base;
138
139         outbody = data_blob_talloc(req->out.vector, NULL, 0x30);
140         if (outbody.data == NULL) {
141                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
142                 if (!NT_STATUS_IS_OK(error)) {
143                         smbd_server_connection_terminate(req->conn,
144                                                          nt_errstr(error));
145                         return;
146                 }
147                 return;
148         }
149
150         SSVAL(outbody.data, 0x00, 0x30 + 1);    /* struct size */
151         SSVAL(outbody.data, 0x02, 0);           /* reserved */
152         SIVAL(outbody.data, 0x04, 0);           /* ctl code */
153         SBVAL(outbody.data, 0x08, 0);           /* file id (persistent) */
154         SBVAL(outbody.data, 0x10, 0);           /* file id (volatile) */
155         SIVAL(outbody.data, 0x18, 0);           /* input offset */
156         SIVAL(outbody.data, 0x1C, 0);           /* input count */
157         SIVAL(outbody.data, 0x20,
158               out_output_offset);               /* output offset */
159         SIVAL(outbody.data, 0x24,
160               out_output_buffer.length);        /* output count */
161         SIVAL(outbody.data, 0x28, 0);           /* flags */
162         SIVAL(outbody.data, 0x2C, 0);           /* reserved */
163
164         /*
165          * Note: Windows sends back also the input from the request.
166          *       I think this is stupid and I hope not required.
167          *       For now we avoid a talloc + memcopy here...
168          */
169         outdyn = out_output_buffer;
170
171         error = smbd_smb2_request_done_ex(req, status, outbody, &outdyn,
172                                           __location__);
173         if (!NT_STATUS_IS_OK(error)) {
174                 smbd_server_connection_terminate(req->conn,
175                                                  nt_errstr(error));
176                 return;
177         }
178 }
179
180 struct smbd_smb2_ioctl_state {
181         struct smbd_smb2_request *smb2req;
182         struct smb_request *smbreq;
183         files_struct *fsp;
184         DATA_BLOB in_input;
185         uint32_t in_max_output;
186         DATA_BLOB out_output;
187 };
188
189 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq);
190 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq);
191
192 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
193                                                struct tevent_context *ev,
194                                                struct smbd_smb2_request *smb2req,
195                                                uint32_t in_ctl_code,
196                                                uint64_t in_file_id_volatile,
197                                                DATA_BLOB in_input,
198                                                uint32_t in_max_output,
199                                                uint32_t in_flags)
200 {
201         struct tevent_req *req;
202         struct smbd_smb2_ioctl_state *state;
203         struct smb_request *smbreq;
204         files_struct *fsp = NULL;
205         struct tevent_req *subreq;
206
207         req = tevent_req_create(mem_ctx, &state,
208                                 struct smbd_smb2_ioctl_state);
209         if (req == NULL) {
210                 return NULL;
211         }
212         state->smb2req = smb2req;
213         state->smbreq = NULL;
214         state->fsp = NULL;
215         state->in_input = in_input;
216         state->in_max_output = in_max_output;
217         state->out_output = data_blob_null;
218
219         DEBUG(10,("smbd_smb2_ioctl: file_id[0x%016llX]\n",
220                   (unsigned long long)in_file_id_volatile));
221
222         smbreq = smbd_smb2_fake_smb_request(smb2req);
223         if (tevent_req_nomem(smbreq, req)) {
224                 return tevent_req_post(req, ev);
225         }
226         state->smbreq = smbreq;
227
228         if (in_file_id_volatile != UINT64_MAX) {
229                 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
230                 if (fsp == NULL) {
231                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
232                         return tevent_req_post(req, ev);
233                 }
234                 if (smbreq->conn != fsp->conn) {
235                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
236                         return tevent_req_post(req, ev);
237                 }
238                 if (smb2req->session->vuid != fsp->vuid) {
239                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
240                         return tevent_req_post(req, ev);
241                 }
242                 state->fsp = fsp;
243         }
244
245         switch (in_ctl_code) {
246         case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
247         {
248                 uint16_t in_max_referral_level;
249                 DATA_BLOB in_file_name_buffer;
250                 char *in_file_name_string;
251                 size_t in_file_name_string_size;
252                 bool ok;
253                 bool overflow = false;
254                 NTSTATUS status;
255                 int dfs_size;
256                 char *dfs_data = NULL;
257
258                 if (!IS_IPC(smbreq->conn)) {
259                         tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
260                         return tevent_req_post(req, ev);
261                 }
262
263                 if (!lp_host_msdfs()) {
264                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
265                         return tevent_req_post(req, ev);
266                 }
267
268                 if (in_input.length < (2 + 2)) {
269                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
270                         return tevent_req_post(req, ev);
271                 }
272
273                 in_max_referral_level = SVAL(in_input.data, 0);
274                 in_file_name_buffer.data = in_input.data + 2;
275                 in_file_name_buffer.length = in_input.length - 2;
276
277                 ok = convert_string_talloc(state, CH_UTF16, CH_UNIX,
278                                            in_file_name_buffer.data,
279                                            in_file_name_buffer.length,
280                                            &in_file_name_string,
281                                            &in_file_name_string_size, false);
282                 if (!ok) {
283                         tevent_req_nterror(req, NT_STATUS_ILLEGAL_CHARACTER);
284                         return tevent_req_post(req, ev);
285                 }
286
287                 dfs_size = setup_dfs_referral(smbreq->conn,
288                                               in_file_name_string,
289                                               in_max_referral_level,
290                                               &dfs_data, &status);
291                 if (dfs_size < 0) {
292                         tevent_req_nterror(req, status);
293                         return tevent_req_post(req, ev);
294                 }
295
296                 if (dfs_size > in_max_output) {
297                         /*
298                          * TODO: we need a testsuite for this
299                          */
300                         overflow = true;
301                         dfs_size = in_max_output;
302                 }
303
304                 state->out_output = data_blob_talloc(state,
305                                                      (uint8_t *)dfs_data,
306                                                      dfs_size);
307                 SAFE_FREE(dfs_data);
308                 if (dfs_size > 0 &&
309                     tevent_req_nomem(state->out_output.data, req)) {
310                         return tevent_req_post(req, ev);
311                 }
312
313                 if (overflow) {
314                         tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
315                 } else {
316                         tevent_req_done(req);
317                 }
318                 return tevent_req_post(req, ev);
319         }
320         case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
321
322                 if (!IS_IPC(smbreq->conn)) {
323                         tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
324                         return tevent_req_post(req, ev);
325                 }
326
327                 if (fsp == NULL) {
328                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
329                         return tevent_req_post(req, ev);
330                 }
331
332                 if (!fsp_is_np(fsp)) {
333                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
334                         return tevent_req_post(req, ev);
335                 }
336
337                 subreq = np_write_send(state, ev,
338                                        fsp->fake_file_handle,
339                                        in_input.data,
340                                        in_input.length);
341                 if (tevent_req_nomem(subreq, req)) {
342                         return tevent_req_post(req, ev);
343                 }
344                 tevent_req_set_callback(subreq,
345                                         smbd_smb2_ioctl_pipe_write_done,
346                                         req);
347                 return req;
348
349         default:
350                 if (IS_IPC(smbreq->conn)) {
351                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
352                         return tevent_req_post(req, ev);
353                 }
354                 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
355                 return tevent_req_post(req, ev);
356         }
357
358         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
359         return tevent_req_post(req, ev);
360 }
361
362 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
363 {
364         struct tevent_req *req = tevent_req_callback_data(subreq,
365                                  struct tevent_req);
366         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
367                                               struct smbd_smb2_ioctl_state);
368         NTSTATUS status;
369         ssize_t nwritten = -1;
370
371         status = np_write_recv(subreq, &nwritten);
372         TALLOC_FREE(subreq);
373         if (!NT_STATUS_IS_OK(status)) {
374                 tevent_req_nterror(req, status);
375                 return;
376         }
377
378         if (nwritten != state->in_input.length) {
379                 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
380                 return;
381         }
382
383         state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
384         if (state->in_max_output > 0 &&
385             tevent_req_nomem(state->out_output.data, req)) {
386                 return;
387         }
388
389         subreq = np_read_send(state->smbreq->conn,
390                               state->smb2req->conn->smb2.event_ctx,
391                               state->fsp->fake_file_handle,
392                               state->out_output.data,
393                               state->out_output.length);
394         if (tevent_req_nomem(subreq, req)) {
395                 return;
396         }
397         tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
398 }
399
400 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
401 {
402         struct tevent_req *req = tevent_req_callback_data(subreq,
403                                  struct tevent_req);
404         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
405                                               struct smbd_smb2_ioctl_state);
406         NTSTATUS status;
407         ssize_t nread;
408         bool is_data_outstanding;
409
410         status = np_read_recv(subreq, &nread, &is_data_outstanding);
411         TALLOC_FREE(subreq);
412         if (!NT_STATUS_IS_OK(status)) {
413                 tevent_req_nterror(req, status);
414                 return;
415         }
416
417         state->out_output.length = nread;
418
419         tevent_req_done(req);
420 }
421
422 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
423                                      TALLOC_CTX *mem_ctx,
424                                      DATA_BLOB *out_output)
425 {
426         NTSTATUS status;
427         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
428                                               struct smbd_smb2_ioctl_state);
429
430         if (tevent_req_is_nterror(req, &status)) {
431                 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
432                         tevent_req_received(req);
433                         return status;
434                 }
435         }
436
437         *out_output = state->out_output;
438         talloc_steal(mem_ctx, out_output->data);
439
440         tevent_req_received(req);
441         return status;
442 }