2 * Unix SMB/CIFS implementation.
3 * RPC client transport over named pipes
4 * Copyright (C) Volker Lendecke 2009
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #define DBGC_CLASS DBGC_RPC_CLI
25 struct rpc_transport_np_state
{
26 struct cli_state
*cli
;
27 const char *pipe_name
;
31 static int rpc_transport_np_state_destructor(struct rpc_transport_np_state
*s
)
34 ret
= cli_close(s
->cli
, s
->fnum
);
36 DEBUG(1, ("rpc_transport_np_state_destructor: cli_close "
37 "failed on pipe %s. Error was %s\n", s
->pipe_name
,
40 DEBUG(10, ("rpc_pipe_destructor: closed %s\n", s
->pipe_name
));
42 * We can't do much on failure
47 struct rpc_np_write_state
{
52 static void rpc_np_write_done(struct async_req
*subreq
);
54 static struct async_req
*rpc_np_write_send(TALLOC_CTX
*mem_ctx
,
55 struct event_context
*ev
,
56 const uint8_t *data
, size_t size
,
59 struct rpc_transport_np_state
*np_transport
= talloc_get_type_abort(
60 priv
, struct rpc_transport_np_state
);
61 struct async_req
*result
, *subreq
;
62 struct rpc_np_write_state
*state
;
64 if (!async_req_setup(mem_ctx
, &result
, &state
,
65 struct rpc_np_write_state
)) {
70 subreq
= cli_write_andx_send(mem_ctx
, ev
, np_transport
->cli
,
72 8, /* 8 means message mode. */
77 subreq
->async
.fn
= rpc_np_write_done
;
78 subreq
->async
.priv
= result
;
85 static void rpc_np_write_done(struct async_req
*subreq
)
87 struct async_req
*req
= talloc_get_type_abort(
88 subreq
->async
.priv
, struct async_req
);
89 struct rpc_np_write_state
*state
= talloc_get_type_abort(
90 req
->private_data
, struct rpc_np_write_state
);
93 status
= cli_write_andx_recv(subreq
, &state
->written
);
95 if (!NT_STATUS_IS_OK(status
)) {
96 async_req_nterror(req
, status
);
102 static NTSTATUS
rpc_np_write_recv(struct async_req
*req
, ssize_t
*pwritten
)
104 struct rpc_np_write_state
*state
= talloc_get_type_abort(
105 req
->private_data
, struct rpc_np_write_state
);
108 if (async_req_is_nterror(req
, &status
)) {
111 *pwritten
= state
->written
;
115 struct rpc_np_read_state
{
121 static void rpc_np_read_done(struct async_req
*subreq
);
123 static struct async_req
*rpc_np_read_send(TALLOC_CTX
*mem_ctx
,
124 struct event_context
*ev
,
125 uint8_t *data
, size_t size
,
128 struct rpc_transport_np_state
*np_transport
= talloc_get_type_abort(
129 priv
, struct rpc_transport_np_state
);
130 struct async_req
*result
, *subreq
;
131 struct rpc_np_read_state
*state
;
133 if (!async_req_setup(mem_ctx
, &result
, &state
,
134 struct rpc_np_read_state
)) {
140 subreq
= cli_read_andx_send(mem_ctx
, ev
, np_transport
->cli
,
141 np_transport
->fnum
, 0, size
);
142 if (subreq
== NULL
) {
145 subreq
->async
.fn
= rpc_np_read_done
;
146 subreq
->async
.priv
= result
;
153 static void rpc_np_read_done(struct async_req
*subreq
)
155 struct async_req
*req
= talloc_get_type_abort(
156 subreq
->async
.priv
, struct async_req
);
157 struct rpc_np_read_state
*state
= talloc_get_type_abort(
158 req
->private_data
, struct rpc_np_read_state
);
162 status
= cli_read_andx_recv(subreq
, &state
->received
, &rcvbuf
);
164 * We can't TALLOC_FREE(subreq) as usual here, as rcvbuf still is a
167 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
168 status
= NT_STATUS_OK
;
170 if (!NT_STATUS_IS_OK(status
)) {
172 async_req_nterror(req
, status
);
176 if (state
->received
> state
->size
) {
178 async_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
182 memcpy(state
->data
, rcvbuf
, state
->received
);
186 static NTSTATUS
rpc_np_read_recv(struct async_req
*req
, ssize_t
*preceived
)
188 struct rpc_np_read_state
*state
= talloc_get_type_abort(
189 req
->private_data
, struct rpc_np_read_state
);
192 if (async_req_is_nterror(req
, &status
)) {
195 *preceived
= state
->received
;
199 struct rpc_np_trans_state
{
205 static void rpc_np_trans_done(struct async_req
*subreq
);
207 static struct async_req
*rpc_np_trans_send(TALLOC_CTX
*mem_ctx
,
208 struct event_context
*ev
,
209 uint8_t *data
, size_t data_len
,
210 uint32_t max_rdata_len
,
213 struct rpc_transport_np_state
*np_transport
= talloc_get_type_abort(
214 priv
, struct rpc_transport_np_state
);
215 struct async_req
*result
, *subreq
;
216 struct rpc_np_trans_state
*state
;
218 if (!async_req_setup(mem_ctx
, &result
, &state
,
219 struct rpc_np_trans_state
)) {
223 SSVAL(state
->setup
+0, 0, TRANSACT_DCERPCCMD
);
224 SSVAL(state
->setup
+1, 0, np_transport
->fnum
);
226 subreq
= cli_trans_send(
227 state
, ev
, np_transport
->cli
, SMBtrans
,
228 "\\PIPE\\", 0, 0, 0, state
->setup
, 2, 0,
229 NULL
, 0, 0, data
, data_len
, max_rdata_len
);
230 if (subreq
== NULL
) {
233 subreq
->async
.fn
= rpc_np_trans_done
;
234 subreq
->async
.priv
= result
;
242 static void rpc_np_trans_done(struct async_req
*subreq
)
244 struct async_req
*req
= talloc_get_type_abort(
245 subreq
->async
.priv
, struct async_req
);
246 struct rpc_np_trans_state
*state
= talloc_get_type_abort(
247 req
->private_data
, struct rpc_np_trans_state
);
250 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
,
251 &state
->rdata
, &state
->rdata_len
);
253 if (!NT_STATUS_IS_OK(status
)) {
254 async_req_nterror(req
, status
);
260 static NTSTATUS
rpc_np_trans_recv(struct async_req
*req
, TALLOC_CTX
*mem_ctx
,
261 uint8_t **prdata
, uint32_t *prdata_len
)
263 struct rpc_np_trans_state
*state
= talloc_get_type_abort(
264 req
->private_data
, struct rpc_np_trans_state
);
267 if (async_req_is_nterror(req
, &status
)) {
270 *prdata
= talloc_move(mem_ctx
, &state
->rdata
);
271 *prdata_len
= state
->rdata_len
;
275 struct rpc_transport_np_init_state
{
276 struct rpc_cli_transport
*transport
;
277 struct rpc_transport_np_state
*transport_np
;
280 static void rpc_transport_np_init_pipe_open(struct async_req
*subreq
);
282 struct async_req
*rpc_transport_np_init_send(TALLOC_CTX
*mem_ctx
,
283 struct event_context
*ev
,
284 struct cli_state
*cli
,
285 const struct ndr_syntax_id
*abstract_syntax
)
287 struct async_req
*result
, *subreq
;
288 struct rpc_transport_np_init_state
*state
;
290 if (!async_req_setup(mem_ctx
, &result
, &state
,
291 struct rpc_transport_np_init_state
)) {
295 state
->transport
= talloc(state
, struct rpc_cli_transport
);
296 if (state
->transport
== NULL
) {
299 state
->transport_np
= talloc(state
->transport
,
300 struct rpc_transport_np_state
);
301 if (state
->transport_np
== NULL
) {
304 state
->transport
->priv
= state
->transport_np
;
306 state
->transport_np
->pipe_name
= get_pipe_name_from_iface(
308 state
->transport_np
->cli
= cli
;
310 subreq
= cli_ntcreate_send(
311 state
, ev
, cli
, state
->transport_np
->pipe_name
, 0,
312 DESIRED_ACCESS_PIPE
, 0, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
314 if (subreq
== NULL
) {
317 subreq
->async
.fn
= rpc_transport_np_init_pipe_open
;
318 subreq
->async
.priv
= result
;
326 static void rpc_transport_np_init_pipe_open(struct async_req
*subreq
)
328 struct async_req
*req
= talloc_get_type_abort(
329 subreq
->async
.priv
, struct async_req
);
330 struct rpc_transport_np_init_state
*state
= talloc_get_type_abort(
331 req
->private_data
, struct rpc_transport_np_init_state
);
334 status
= cli_ntcreate_recv(subreq
, &state
->transport_np
->fnum
);
336 if (!NT_STATUS_IS_OK(status
)) {
337 async_req_nterror(req
, status
);
341 talloc_set_destructor(state
->transport_np
,
342 rpc_transport_np_state_destructor
);
346 NTSTATUS
rpc_transport_np_init_recv(struct async_req
*req
,
348 struct rpc_cli_transport
**presult
)
350 struct rpc_transport_np_init_state
*state
= talloc_get_type_abort(
351 req
->private_data
, struct rpc_transport_np_init_state
);
354 if (async_req_is_nterror(req
, &status
)) {
358 state
->transport
->write_send
= rpc_np_write_send
;
359 state
->transport
->write_recv
= rpc_np_write_recv
;
360 state
->transport
->read_send
= rpc_np_read_send
;
361 state
->transport
->read_recv
= rpc_np_read_recv
;
362 state
->transport
->trans_send
= rpc_np_trans_send
;
363 state
->transport
->trans_recv
= rpc_np_trans_recv
;
365 *presult
= talloc_move(mem_ctx
, &state
->transport
);
369 NTSTATUS
rpc_transport_np_init(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
370 const struct ndr_syntax_id
*abstract_syntax
,
371 struct rpc_cli_transport
**presult
)
373 TALLOC_CTX
*frame
= talloc_stackframe();
374 struct event_context
*ev
;
375 struct async_req
*req
;
378 ev
= event_context_init(frame
);
380 status
= NT_STATUS_NO_MEMORY
;
384 req
= rpc_transport_np_init_send(frame
, ev
, cli
, abstract_syntax
);
386 status
= NT_STATUS_NO_MEMORY
;
390 while (req
->state
< ASYNC_REQ_DONE
) {
394 status
= rpc_transport_np_init_recv(req
, mem_ctx
, presult
);
400 struct cli_state
*rpc_pipe_np_smb_conn(struct rpc_pipe_client
*p
)
402 struct rpc_transport_np_state
*state
= talloc_get_type(
403 p
->transport
->priv
, struct rpc_transport_np_state
);