s3:smbd: add support for SMB2 Ioctl FSCTL_PIPE_TRANSCEIVE on IPC$
[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_IS_OK(status)) {
124                 error = smbd_smb2_request_error(req, status);
125                 if (!NT_STATUS_IS_OK(error)) {
126                         smbd_server_connection_terminate(req->conn,
127                                                          nt_errstr(error));
128                         return;
129                 }
130                 return;
131         }
132
133         out_output_offset = SMB2_HDR_BODY + 0x30;
134
135         outhdr = (uint8_t *)req->out.vector[i].iov_base;
136
137         outbody = data_blob_talloc(req->out.vector, NULL, 0x30);
138         if (outbody.data == NULL) {
139                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
140                 if (!NT_STATUS_IS_OK(error)) {
141                         smbd_server_connection_terminate(req->conn,
142                                                          nt_errstr(error));
143                         return;
144                 }
145                 return;
146         }
147
148         SSVAL(outbody.data, 0x00, 0x30 + 1);    /* struct size */
149         SSVAL(outbody.data, 0x02, 0);           /* reserved */
150         SIVAL(outbody.data, 0x04, 0);           /* ctl code */
151         SBVAL(outbody.data, 0x08, 0);           /* file id (persistent) */
152         SBVAL(outbody.data, 0x10, 0);           /* file id (volatile) */
153         SIVAL(outbody.data, 0x18, 0);           /* input offset */
154         SIVAL(outbody.data, 0x1C, 0);           /* input count */
155         SIVAL(outbody.data, 0x20,
156               out_output_offset);               /* output offset */
157         SIVAL(outbody.data, 0x24,
158               out_output_buffer.length);        /* output count */
159         SIVAL(outbody.data, 0x28, 0);           /* flags */
160         SIVAL(outbody.data, 0x2C, 0);           /* reserved */
161
162         /*
163          * Note: Windows sends back also the input from the request.
164          *       I think this is stupid and I hope not required.
165          *       For now we avoid a talloc + memcopy here...
166          */
167         outdyn = out_output_buffer;
168
169         error = smbd_smb2_request_done(req, outbody, &outdyn);
170         if (!NT_STATUS_IS_OK(error)) {
171                 smbd_server_connection_terminate(req->conn,
172                                                  nt_errstr(error));
173                 return;
174         }
175 }
176
177 struct smbd_smb2_ioctl_state {
178         struct smbd_smb2_request *smb2req;
179         struct smb_request *smbreq;
180         files_struct *fsp;
181         DATA_BLOB in_input;
182         uint32_t in_max_output;
183         DATA_BLOB out_output;
184 };
185
186 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq);
187 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq);
188
189 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
190                                                struct tevent_context *ev,
191                                                struct smbd_smb2_request *smb2req,
192                                                uint32_t in_ctl_code,
193                                                uint64_t in_file_id_volatile,
194                                                DATA_BLOB in_input,
195                                                uint32_t in_max_output,
196                                                uint32_t in_flags)
197 {
198         struct tevent_req *req;
199         struct smbd_smb2_ioctl_state *state;
200         struct smb_request *smbreq;
201         files_struct *fsp = NULL;
202         struct tevent_req *subreq;
203
204         req = tevent_req_create(mem_ctx, &state,
205                                 struct smbd_smb2_ioctl_state);
206         if (req == NULL) {
207                 return NULL;
208         }
209         state->smb2req = smb2req;
210         state->smbreq = NULL;
211         state->fsp = NULL;
212         state->in_input = in_input;
213         state->in_max_output = in_max_output;
214         state->out_output = data_blob_null;
215
216         DEBUG(10,("smbd_smb2_ioctl: file_id[0x%016llX]\n",
217                   (unsigned long long)in_file_id_volatile));
218
219         smbreq = smbd_smb2_fake_smb_request(smb2req);
220         if (tevent_req_nomem(smbreq, req)) {
221                 return tevent_req_post(req, ev);
222         }
223         state->smbreq = smbreq;
224
225         if (in_file_id_volatile != UINT64_MAX) {
226                 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
227                 if (fsp == NULL) {
228                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
229                         return tevent_req_post(req, ev);
230                 }
231                 if (smbreq->conn != fsp->conn) {
232                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
233                         return tevent_req_post(req, ev);
234                 }
235                 if (smb2req->session->vuid != fsp->vuid) {
236                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
237                         return tevent_req_post(req, ev);
238                 }
239                 state->fsp = fsp;
240         }
241
242         switch (in_ctl_code) {
243         case 0x0011C017: /* FSCTL_PIPE_TRANSCEIVE */
244
245                 if (!IS_IPC(smbreq->conn)) {
246                         tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
247                         return tevent_req_post(req, ev);
248                 }
249
250                 if (fsp == NULL) {
251                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
252                         return tevent_req_post(req, ev);
253                 }
254
255                 if (!fsp_is_np(fsp)) {
256                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
257                         return tevent_req_post(req, ev);
258                 }
259
260                 subreq = np_write_send(state, ev,
261                                        fsp->fake_file_handle,
262                                        in_input.data,
263                                        in_input.length);
264                 if (tevent_req_nomem(subreq, req)) {
265                         return tevent_req_post(req, ev);
266                 }
267                 tevent_req_set_callback(subreq,
268                                         smbd_smb2_ioctl_pipe_write_done,
269                                         req);
270                 return req;
271
272         default:
273                 if (IS_IPC(smbreq->conn)) {
274                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
275                         return tevent_req_post(req, ev);
276                 }
277                 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
278                 return tevent_req_post(req, ev);
279         }
280
281         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
282         return tevent_req_post(req, ev);
283 }
284
285 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
286 {
287         struct tevent_req *req = tevent_req_callback_data(subreq,
288                                  struct tevent_req);
289         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
290                                               struct smbd_smb2_ioctl_state);
291         NTSTATUS status;
292         ssize_t nwritten = -1;
293
294         status = np_write_recv(subreq, &nwritten);
295         TALLOC_FREE(subreq);
296         if (!NT_STATUS_IS_OK(status)) {
297                 tevent_req_nterror(req, status);
298                 return;
299         }
300
301         if (nwritten != state->in_input.length) {
302                 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
303                 return;
304         }
305
306         state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
307         if (state->in_max_output > 0 &&
308             tevent_req_nomem(state->out_output.data, req)) {
309                 return;
310         }
311
312         subreq = np_read_send(state->smbreq->conn,
313                               state->smb2req->conn->smb2.event_ctx,
314                               state->fsp->fake_file_handle,
315                               state->out_output.data,
316                               state->out_output.length);
317         if (tevent_req_nomem(subreq, req)) {
318                 return;
319         }
320         tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
321 }
322
323 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
324 {
325         struct tevent_req *req = tevent_req_callback_data(subreq,
326                                  struct tevent_req);
327         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
328                                               struct smbd_smb2_ioctl_state);
329         NTSTATUS status;
330         ssize_t nread;
331         bool is_data_outstanding;
332
333         status = np_read_recv(subreq, &nread, &is_data_outstanding);
334         TALLOC_FREE(subreq);
335         if (!NT_STATUS_IS_OK(status)) {
336                 tevent_req_nterror(req, status);
337                 return;
338         }
339
340         state->out_output.length = nread;
341
342         tevent_req_done(req);
343 }
344
345 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
346                                      TALLOC_CTX *mem_ctx,
347                                      DATA_BLOB *out_output)
348 {
349         NTSTATUS status;
350         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
351                                               struct smbd_smb2_ioctl_state);
352
353         if (tevent_req_is_nterror(req, &status)) {
354                 tevent_req_received(req);
355                 return status;
356         }
357
358         *out_output = state->out_output;
359         talloc_steal(mem_ctx, out_output->data);
360
361         tevent_req_received(req);
362         return NT_STATUS_OK;
363 }