smb2_ioctl: split ioctl handler code on device type
[mat/samba.git] / source3 / smbd / smb2_ioctl_named_pipe.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 "rpc_server/srv_pipe_hnd.h"
27 #include "include/ntioctl.h"
28 #include "smb2_ioctl_private.h"
29
30 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq);
31 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq);
32
33 struct tevent_req *smb2_ioctl_named_pipe(uint32_t ctl_code,
34                                          struct tevent_context *ev,
35                                          struct tevent_req *req,
36                                          struct smbd_smb2_ioctl_state *state)
37 {
38         struct tevent_req *subreq;
39
40         switch (ctl_code) {
41         case FSCTL_PIPE_TRANSCEIVE:
42         {
43                 if (!IS_IPC(state->smbreq->conn)) {
44                         tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
45                         return tevent_req_post(req, ev);
46                 }
47
48                 if (state->fsp == NULL) {
49                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
50                         return tevent_req_post(req, ev);
51                 }
52
53                 if (!fsp_is_np(state->fsp)) {
54                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
55                         return tevent_req_post(req, ev);
56                 }
57
58                 DEBUG(10,("smbd_smb2_ioctl_send: np_write_send of size %u\n",
59                         (unsigned int)state->in_input.length ));
60
61                 subreq = np_write_send(state, ev,
62                                        state->fsp->fake_file_handle,
63                                        state->in_input.data,
64                                        state->in_input.length);
65                 if (tevent_req_nomem(subreq, req)) {
66                         return tevent_req_post(req, ev);
67                 }
68                 tevent_req_set_callback(subreq,
69                                         smbd_smb2_ioctl_pipe_write_done,
70                                         req);
71                 return req;
72                 break;
73         }
74         default: {
75                 NTSTATUS status;
76                 uint8_t *out_data = NULL;
77                 uint32_t out_data_len = 0;
78
79                 if (state->fsp == NULL) {
80                         status = NT_STATUS_NOT_SUPPORTED;
81                 } else {
82                         status = SMB_VFS_FSCTL(state->fsp,
83                                                state,
84                                                ctl_code,
85                                                state->smbreq->flags2,
86                                                state->in_input.data,
87                                                state->in_input.length,
88                                                &out_data,
89                                                state->in_max_output,
90                                                &out_data_len);
91                         state->out_output = data_blob_const(out_data, out_data_len);
92                         if (NT_STATUS_IS_OK(status)) {
93                                 tevent_req_done(req);
94                                 return tevent_req_post(req, ev);
95                         }
96                 }
97
98                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
99                         if (IS_IPC(state->smbreq->conn)) {
100                                 status = NT_STATUS_FS_DRIVER_REQUIRED;
101                         } else {
102                                 status = NT_STATUS_INVALID_DEVICE_REQUEST;
103                         }
104                 }
105
106                 tevent_req_nterror(req, status);
107                 return tevent_req_post(req, ev);
108                 break;
109         }
110         }
111
112         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
113         return tevent_req_post(req, ev);
114 }
115
116 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req *subreq)
117 {
118         struct tevent_req *req = tevent_req_callback_data(subreq,
119                                  struct tevent_req);
120         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
121                                               struct smbd_smb2_ioctl_state);
122         NTSTATUS status;
123         ssize_t nwritten = -1;
124
125         status = np_write_recv(subreq, &nwritten);
126
127         DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
128                 (long int)nwritten ));
129
130         TALLOC_FREE(subreq);
131         if (!NT_STATUS_IS_OK(status)) {
132                 tevent_req_nterror(req, status);
133                 return;
134         }
135
136         if (nwritten != state->in_input.length) {
137                 tevent_req_nterror(req, NT_STATUS_PIPE_NOT_AVAILABLE);
138                 return;
139         }
140
141         state->out_output = data_blob_talloc(state, NULL, state->in_max_output);
142         if (state->in_max_output > 0 &&
143             tevent_req_nomem(state->out_output.data, req)) {
144                 return;
145         }
146
147         DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
148                 "of size %u\n",
149                 (unsigned int)state->out_output.length ));
150
151         subreq = np_read_send(state->smbreq->conn,
152                               state->smb2req->sconn->ev_ctx,
153                               state->fsp->fake_file_handle,
154                               state->out_output.data,
155                               state->out_output.length);
156         if (tevent_req_nomem(subreq, req)) {
157                 return;
158         }
159         tevent_req_set_callback(subreq, smbd_smb2_ioctl_pipe_read_done, req);
160 }
161
162 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req *subreq)
163 {
164         struct tevent_req *req = tevent_req_callback_data(subreq,
165                                  struct tevent_req);
166         struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
167                                               struct smbd_smb2_ioctl_state);
168         NTSTATUS status;
169         ssize_t nread = -1;
170         bool is_data_outstanding = false;
171
172         status = np_read_recv(subreq, &nread, &is_data_outstanding);
173
174         DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
175                  "is_data_outstanding = %d, status = %s\n",
176                 (int)nread,
177                 (int)is_data_outstanding,
178                 nt_errstr(status) ));
179
180         TALLOC_FREE(subreq);
181         if (!NT_STATUS_IS_OK(status)) {
182                 tevent_req_nterror(req, status);
183                 return;
184         }
185
186         state->out_output.length = nread;
187
188         if (is_data_outstanding) {
189                 tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
190                 return;
191         }
192
193         tevent_req_done(req);
194 }