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