2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
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.
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.
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/>.
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"
31 #define DBGC_CLASS DBGC_SMB2
33 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req
*subreq
);
34 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req
*subreq
);
36 struct tevent_req
*smb2_ioctl_named_pipe(uint32_t ctl_code
,
37 struct tevent_context
*ev
,
38 struct tevent_req
*req
,
39 struct smbd_smb2_ioctl_state
*state
)
42 uint8_t *out_data
= NULL
;
43 uint32_t out_data_len
= 0;
45 if (ctl_code
== FSCTL_PIPE_TRANSCEIVE
) {
46 struct tevent_req
*subreq
;
48 if (!IS_IPC(state
->smbreq
->conn
)) {
49 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
50 return tevent_req_post(req
, ev
);
53 if (state
->fsp
== NULL
) {
54 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
55 return tevent_req_post(req
, ev
);
58 if (!fsp_is_np(state
->fsp
)) {
59 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
60 return tevent_req_post(req
, ev
);
63 DEBUG(10,("smbd_smb2_ioctl_send: np_write_send of size %u\n",
64 (unsigned int)state
->in_input
.length
));
66 subreq
= np_write_send(state
, ev
,
67 state
->fsp
->fake_file_handle
,
69 state
->in_input
.length
);
70 if (tevent_req_nomem(subreq
, req
)) {
71 return tevent_req_post(req
, ev
);
73 tevent_req_set_callback(subreq
,
74 smbd_smb2_ioctl_pipe_write_done
,
79 if (state
->fsp
== NULL
) {
80 status
= NT_STATUS_NOT_SUPPORTED
;
82 status
= SMB_VFS_FSCTL(state
->fsp
,
85 state
->smbreq
->flags2
,
87 state
->in_input
.length
,
91 state
->out_output
= data_blob_const(out_data
, out_data_len
);
92 if (NT_STATUS_IS_OK(status
)) {
94 return tevent_req_post(req
, ev
);
98 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
)) {
99 if (IS_IPC(state
->smbreq
->conn
)) {
100 status
= NT_STATUS_FS_DRIVER_REQUIRED
;
102 status
= NT_STATUS_INVALID_DEVICE_REQUEST
;
106 tevent_req_nterror(req
, status
);
107 return tevent_req_post(req
, ev
);
110 static void smbd_smb2_ioctl_pipe_write_done(struct tevent_req
*subreq
)
112 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
114 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
115 struct smbd_smb2_ioctl_state
);
117 ssize_t nwritten
= -1;
119 status
= np_write_recv(subreq
, &nwritten
);
121 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: received %ld\n",
122 (long int)nwritten
));
125 if (!NT_STATUS_IS_OK(status
)) {
126 tevent_req_nterror(req
, status
);
130 if (nwritten
!= state
->in_input
.length
) {
131 tevent_req_nterror(req
, NT_STATUS_PIPE_NOT_AVAILABLE
);
135 state
->out_output
= data_blob_talloc(state
, NULL
, state
->in_max_output
);
136 if (state
->in_max_output
> 0 &&
137 tevent_req_nomem(state
->out_output
.data
, req
)) {
141 DEBUG(10,("smbd_smb2_ioctl_pipe_write_done: issuing np_read_send "
143 (unsigned int)state
->out_output
.length
));
145 subreq
= np_read_send(state
->smbreq
->conn
,
146 state
->smb2req
->sconn
->ev_ctx
,
147 state
->fsp
->fake_file_handle
,
148 state
->out_output
.data
,
149 state
->out_output
.length
);
150 if (tevent_req_nomem(subreq
, req
)) {
153 tevent_req_set_callback(subreq
, smbd_smb2_ioctl_pipe_read_done
, req
);
156 static void smbd_smb2_ioctl_pipe_read_done(struct tevent_req
*subreq
)
158 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
160 struct smbd_smb2_ioctl_state
*state
= tevent_req_data(req
,
161 struct smbd_smb2_ioctl_state
);
164 bool is_data_outstanding
= false;
166 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
168 DEBUG(10,("smbd_smb2_ioctl_pipe_read_done: np_read_recv nread = %d "
169 "is_data_outstanding = %d, status = %s\n",
171 (int)is_data_outstanding
,
172 nt_errstr(status
) ));
175 if (!NT_STATUS_IS_OK(status
)) {
176 tevent_req_nterror(req
, status
);
180 state
->out_output
.length
= nread
;
182 if (is_data_outstanding
) {
183 tevent_req_nterror(req
, STATUS_BUFFER_OVERFLOW
);
187 tevent_req_done(req
);