s3:smbd: FSCTL_PIPE_TRANSCEIVE on a none IPC$ share should give NOT_SUPPORTED
[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         const uint8_t *inbody;
113         int i = req->current_idx;
114         uint8_t *outhdr;
115         DATA_BLOB outbody;
116         DATA_BLOB outdyn;
117         uint32_t in_ctl_code;
118         uint64_t in_file_id_persistent;
119         uint64_t in_file_id_volatile;
120         uint32_t out_input_offset;
121         uint32_t out_output_offset;
122         DATA_BLOB out_output_buffer;
123         NTSTATUS status;
124         NTSTATUS error; /* transport error */
125
126         status = smbd_smb2_ioctl_recv(subreq, req, &out_output_buffer);
127         TALLOC_FREE(subreq);
128         if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
129                 /* also ok */
130         } else if (!NT_STATUS_IS_OK(status)) {
131                 error = smbd_smb2_request_error(req, status);
132                 if (!NT_STATUS_IS_OK(error)) {
133                         smbd_server_connection_terminate(req->conn,
134                                                          nt_errstr(error));
135                         return;
136                 }
137                 return;
138         }
139
140         out_input_offset = SMB2_HDR_BODY + 0x30;
141         out_output_offset = SMB2_HDR_BODY + 0x30;
142
143         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
144
145         in_ctl_code             = IVAL(inbody, 0x04);
146         in_file_id_persistent   = BVAL(inbody, 0x08);
147         in_file_id_volatile     = BVAL(inbody, 0x10);
148
149         outhdr = (uint8_t *)req->out.vector[i].iov_base;
150
151         outbody = data_blob_talloc(req->out.vector, NULL, 0x30);
152         if (outbody.data == NULL) {
153                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
154                 if (!NT_STATUS_IS_OK(error)) {
155                         smbd_server_connection_terminate(req->conn,
156                                                          nt_errstr(error));
157                         return;
158                 }
159                 return;
160         }
161
162         SSVAL(outbody.data, 0x00, 0x30 + 1);    /* struct size */
163         SSVAL(outbody.data, 0x02, 0);           /* reserved */
164         SIVAL(outbody.data, 0x04,
165               in_ctl_code);                     /* ctl code */
166         SBVAL(outbody.data, 0x08,
167               in_file_id_persistent);           /* file id (persistent) */
168         SBVAL(outbody.data, 0x10,
169               in_file_id_volatile);             /* file id (volatile) */
170         SIVAL(outbody.data, 0x18,
171               out_input_offset);                /* input offset */
172         SIVAL(outbody.data, 0x1C, 0);           /* input count */
173         SIVAL(outbody.data, 0x20,
174               out_output_offset);               /* output offset */
175         SIVAL(outbody.data, 0x24,
176               out_output_buffer.length);        /* output count */
177         SIVAL(outbody.data, 0x28, 0);           /* flags */
178         SIVAL(outbody.data, 0x2C, 0);           /* reserved */
179
180         /*
181          * Note: Windows Vista and 2008 send back also the
182          *       input from the request. But it was fixed in
183          *       Windows 7.
184          */
185         outdyn = out_output_buffer;
186
187         error = smbd_smb2_request_done_ex(req, status, outbody, &outdyn,
188                                           __location__);
189         if (!NT_STATUS_IS_OK(error)) {
190                 smbd_server_connection_terminate(req->conn,
191                                                  nt_errstr(error));
192                 return;
193         }
194 }
195
196 struct smbd_smb2_ioctl_state {
197         struct smbd_smb2_request *smb2req;
198         struct smb_request *smbreq;
199         files_struct *fsp;
200         DATA_BLOB in_input;
201         uint32_t in_max_output;
202         DATA_BLOB out_output;
203 };
204
205 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq);
206 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq);
207
208 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
209                                                struct tevent_context *ev,
210                                                struct smbd_smb2_request *smb2req,
211                                                uint32_t in_ctl_code,
212                                                uint64_t in_file_id_volatile,
213                                                DATA_BLOB in_input,
214                                                uint32_t in_max_output,
215                                                uint32_t in_flags)
216 {
217         struct tevent_req *req;
218         struct smbd_smb2_ioctl_state *state;
219         struct smb_request *smbreq;
220         files_struct *fsp = NULL;
221         struct tevent_req *subreq;
222
223         req = tevent_req_create(mem_ctx, &state,
224                                 struct smbd_smb2_ioctl_state);
225         if (req == NULL) {
226                 return NULL;
227         }
228         state->smb2req = smb2req;
229         state->smbreq = NULL;
230         state->fsp = NULL;
231         state->in_input = in_input;
232         state->in_max_output = in_max_output;
233         state->out_output = data_blob_null;
234
235         DEBUG(10,("smbd_smb2_ioctl: file_id[0x%016llX]\n",
236                   (unsigned long long)in_file_id_volatile));
237
238         smbreq = smbd_smb2_fake_smb_request(smb2req);
239         if (tevent_req_nomem(smbreq, req)) {
240                 return tevent_req_post(req, ev);
241         }
242         state->smbreq = smbreq;
243
244         if (in_file_id_volatile != UINT64_MAX) {
245                 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
246                 if (fsp == NULL) {
247                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
248                         return tevent_req_post(req, ev);
249                 }
250                 if (smbreq->conn != fsp->conn) {
251                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
252                         return tevent_req_post(req, ev);
253                 }
254                 if (smb2req->session->vuid != fsp->vuid) {
255                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
256                         return tevent_req_post(req, ev);
257                 }
258                 state->fsp = fsp;
259         }
260
261         switch (in_ctl_code) {
262         case 0x00060194: /* FSCTL_DFS_GET_REFERRALS */
263         {
264                 uint16_t in_max_referral_level;
265                 DATA_BLOB in_file_name_buffer;
266                 char *in_file_name_string;
267                 size_t in_file_name_string_size;
268                 bool ok;
269                 bool overflow = false;
270                 NTSTATUS status;
271                 int dfs_size;
272                 char *dfs_data = NULL;
273
274                 if (!IS_IPC(smbreq->conn)) {
275                         tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
276                         return tevent_req_post(req, ev);
277                 }
278
279                 if (!lp_host_msdfs()) {
280                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
281                         return tevent_req_post(req, ev);
282                 }
283
284                 if (in_input.length < (2 + 2)) {
285                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
286                         return tevent_req_post(req, ev);
287                 }
288
289                 in_max_referral_level = SVAL(in_input.data, 0);
290                 in_file_name_buffer.data = in_input.data + 2;
291                 in_file_name_buffer.length = in_input.length - 2;
292
293                 ok = convert_string_talloc(state, CH_UTF16, CH_UNIX,
294                                            in_file_name_buffer.data,
295                                            in_file_name_buffer.length,
296                                            &in_file_name_string,
297                                            &in_file_name_string_size, false);
298                 if (!ok) {
299                         tevent_req_nterror(req, NT_STATUS_ILLEGAL_CHARACTER);
300                         return tevent_req_post(req, ev);
301                 }
302
303                 dfs_size = setup_dfs_referral(smbreq->conn,
304                                               in_file_name_string,
305                                               in_max_referral_level,
306                                               &dfs_data, &status);
307                 if (dfs_size < 0) {
308                         tevent_req_nterror(req, status);
309                         return tevent_req_post(req, ev);
310                 }
311
312                 if (dfs_size > in_max_output) {
313                         /*
314                          * TODO: we need a testsuite for this
315                          */
316                         overflow = true;
317                         dfs_size = in_max_output;
318                 }
319
320                 state->out_output = data_blob_talloc(state,
321                                                      (uint8_t *)dfs_data,
322                                                      dfs_size);
323                 SAFE_FREE(dfs_data);
324                 if (dfs_size > 0 &&
325                     tevent_req_nomem(state->out_output.data, req)) {
326                         return tevent_req_post(req, ev);
327                 }
328
329                 if (overflow) {
330                         tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
331                 } else {
332                         tevent_req_done(req);
333                 }
334                 return tevent_req_post(req, ev);
335         }
336         case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
337
338                 if (!IS_IPC(smbreq->conn)) {
339                         tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
340                         return tevent_req_post(req, ev);
341                 }
342
343                 if (fsp == NULL) {
344                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
345                         return tevent_req_post(req, ev);
346                 }
347
348                 if (!fsp_is_np(fsp)) {
349                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
350                         return tevent_req_post(req, ev);
351                 }
352
353                 subreq = np_write_send(state, ev,
354                                        fsp->fake_file_handle,
355                                        in_input.data,
356                                        in_input.length);
357                 if (tevent_req_nomem(subreq, req)) {
358                         return tevent_req_post(req, ev);
359                 }
360                 tevent_req_set_callback(subreq,
361                                         smbd_smb2_ioctl_pipe_write_done,
362                                         req);
363                 return req;
364
365         default:
366                 if (IS_IPC(smbreq->conn)) {
367                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
368                         return tevent_req_post(req, ev);
369                 }
370                 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
371                 return tevent_req_post(req, ev);
372         }
373
374         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
375         return tevent_req_post(req, ev);
376 }
377
378 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
379 {
380         struct tevent_req *req = tevent_req_callback_data(subreq,
381                                  struct tevent_req);
382         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
383                                               struct smbd_smb2_ioctl_state);
384         NTSTATUS status;
385         ssize_t nwritten = -1;
386
387         status = np_write_recv(subreq, &nwritten);
388         TALLOC_FREE(subreq);
389         if (!NT_STATUS_IS_OK(status)) {
390                 tevent_req_nterror(req, status);
391                 return;
392         }
393
394         if (nwritten != state->in_input.length) {
395                 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
396                 return;
397         }
398
399         state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
400         if (state->in_max_output > 0 &&
401             tevent_req_nomem(state->out_output.data, req)) {
402                 return;
403         }
404
405         subreq = np_read_send(state->smbreq->conn,
406                               state->smb2req->conn->smb2.event_ctx,
407                               state->fsp->fake_file_handle,
408                               state->out_output.data,
409                               state->out_output.length);
410         if (tevent_req_nomem(subreq, req)) {
411                 return;
412         }
413         tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
414 }
415
416 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
417 {
418         struct tevent_req *req = tevent_req_callback_data(subreq,
419                                  struct tevent_req);
420         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
421                                               struct smbd_smb2_ioctl_state);
422         NTSTATUS status;
423         ssize_t nread;
424         bool is_data_outstanding;
425
426         status = np_read_recv(subreq, &nread, &is_data_outstanding);
427         TALLOC_FREE(subreq);
428         if (!NT_STATUS_IS_OK(status)) {
429                 tevent_req_nterror(req, status);
430                 return;
431         }
432
433         state->out_output.length = nread;
434
435         tevent_req_done(req);
436 }
437
438 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
439                                      TALLOC_CTX *mem_ctx,
440                                      DATA_BLOB *out_output)
441 {
442         NTSTATUS status;
443         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
444                                               struct smbd_smb2_ioctl_state);
445
446         if (tevent_req_is_nterror(req, &status)) {
447                 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
448                         tevent_req_received(req);
449                         return status;
450                 }
451         }
452
453         *out_output = state->out_output;
454         talloc_steal(mem_ctx, out_output->data);
455
456         tevent_req_received(req);
457         return status;
458 }