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
)
35 if (s
->cli
->fd
== -1) {
36 DEBUG(10, ("socket was closed, no need to send close request.\n"));
40 ret
= cli_close(s
->cli
, s
->fnum
);
42 DEBUG(1, ("rpc_transport_np_state_destructor: cli_close "
43 "failed on pipe %s. Error was %s\n", s
->pipe_name
,
46 DEBUG(10, ("rpc_pipe_destructor: closed %s\n", s
->pipe_name
));
48 * We can't do much on failure
53 struct rpc_np_write_state
{
58 static void rpc_np_write_done(struct async_req
*subreq
);
60 static struct async_req
*rpc_np_write_send(TALLOC_CTX
*mem_ctx
,
61 struct event_context
*ev
,
62 const uint8_t *data
, size_t size
,
65 struct rpc_transport_np_state
*np_transport
= talloc_get_type_abort(
66 priv
, struct rpc_transport_np_state
);
67 struct async_req
*result
, *subreq
;
68 struct rpc_np_write_state
*state
;
70 if (!async_req_setup(mem_ctx
, &result
, &state
,
71 struct rpc_np_write_state
)) {
76 subreq
= cli_write_andx_send(mem_ctx
, ev
, np_transport
->cli
,
78 8, /* 8 means message mode. */
83 subreq
->async
.fn
= rpc_np_write_done
;
84 subreq
->async
.priv
= result
;
91 static void rpc_np_write_done(struct async_req
*subreq
)
93 struct async_req
*req
= talloc_get_type_abort(
94 subreq
->async
.priv
, struct async_req
);
95 struct rpc_np_write_state
*state
= talloc_get_type_abort(
96 req
->private_data
, struct rpc_np_write_state
);
99 status
= cli_write_andx_recv(subreq
, &state
->written
);
101 if (!NT_STATUS_IS_OK(status
)) {
102 async_req_nterror(req
, status
);
108 static NTSTATUS
rpc_np_write_recv(struct async_req
*req
, ssize_t
*pwritten
)
110 struct rpc_np_write_state
*state
= talloc_get_type_abort(
111 req
->private_data
, struct rpc_np_write_state
);
114 if (async_req_is_nterror(req
, &status
)) {
117 *pwritten
= state
->written
;
121 struct rpc_np_read_state
{
127 static void rpc_np_read_done(struct async_req
*subreq
);
129 static struct async_req
*rpc_np_read_send(TALLOC_CTX
*mem_ctx
,
130 struct event_context
*ev
,
131 uint8_t *data
, size_t size
,
134 struct rpc_transport_np_state
*np_transport
= talloc_get_type_abort(
135 priv
, struct rpc_transport_np_state
);
136 struct async_req
*result
, *subreq
;
137 struct rpc_np_read_state
*state
;
139 if (!async_req_setup(mem_ctx
, &result
, &state
,
140 struct rpc_np_read_state
)) {
146 subreq
= cli_read_andx_send(mem_ctx
, ev
, np_transport
->cli
,
147 np_transport
->fnum
, 0, size
);
148 if (subreq
== NULL
) {
151 subreq
->async
.fn
= rpc_np_read_done
;
152 subreq
->async
.priv
= result
;
159 static void rpc_np_read_done(struct async_req
*subreq
)
161 struct async_req
*req
= talloc_get_type_abort(
162 subreq
->async
.priv
, struct async_req
);
163 struct rpc_np_read_state
*state
= talloc_get_type_abort(
164 req
->private_data
, struct rpc_np_read_state
);
168 /* We must free subreq in this function as there is
169 a timer event attached to it. */
171 status
= cli_read_andx_recv(subreq
, &state
->received
, &rcvbuf
);
173 * We can't TALLOC_FREE(subreq) as usual here, as rcvbuf still is a
176 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
177 status
= NT_STATUS_OK
;
179 if (!NT_STATUS_IS_OK(status
)) {
181 async_req_nterror(req
, status
);
185 if (state
->received
> state
->size
) {
187 async_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
191 if (state
->received
== 0) {
193 async_req_nterror(req
, NT_STATUS_PIPE_BROKEN
);
197 memcpy(state
->data
, rcvbuf
, state
->received
);
202 static NTSTATUS
rpc_np_read_recv(struct async_req
*req
, ssize_t
*preceived
)
204 struct rpc_np_read_state
*state
= talloc_get_type_abort(
205 req
->private_data
, struct rpc_np_read_state
);
208 if (async_req_is_nterror(req
, &status
)) {
211 *preceived
= state
->received
;
215 struct rpc_np_trans_state
{
217 uint32_t max_rdata_len
;
222 static void rpc_np_trans_done(struct async_req
*subreq
);
224 static struct async_req
*rpc_np_trans_send(TALLOC_CTX
*mem_ctx
,
225 struct event_context
*ev
,
226 uint8_t *data
, size_t data_len
,
227 uint32_t max_rdata_len
,
230 struct rpc_transport_np_state
*np_transport
= talloc_get_type_abort(
231 priv
, struct rpc_transport_np_state
);
232 struct async_req
*result
, *subreq
;
233 struct rpc_np_trans_state
*state
;
235 if (!async_req_setup(mem_ctx
, &result
, &state
,
236 struct rpc_np_trans_state
)) {
240 state
->max_rdata_len
= max_rdata_len
;
242 SSVAL(state
->setup
+0, 0, TRANSACT_DCERPCCMD
);
243 SSVAL(state
->setup
+1, 0, np_transport
->fnum
);
245 subreq
= cli_trans_send(
246 state
, ev
, np_transport
->cli
, SMBtrans
,
247 "\\PIPE\\", 0, 0, 0, state
->setup
, 2, 0,
248 NULL
, 0, 0, data
, data_len
, max_rdata_len
);
249 if (subreq
== NULL
) {
252 subreq
->async
.fn
= rpc_np_trans_done
;
253 subreq
->async
.priv
= result
;
261 static void rpc_np_trans_done(struct async_req
*subreq
)
263 struct async_req
*req
= talloc_get_type_abort(
264 subreq
->async
.priv
, struct async_req
);
265 struct rpc_np_trans_state
*state
= talloc_get_type_abort(
266 req
->private_data
, struct rpc_np_trans_state
);
269 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
,
270 &state
->rdata
, &state
->rdata_len
);
272 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
273 status
= NT_STATUS_OK
;
275 if (!NT_STATUS_IS_OK(status
)) {
276 async_req_nterror(req
, status
);
280 if (state
->rdata_len
> state
->max_rdata_len
) {
281 async_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
285 if (state
->rdata_len
== 0) {
286 async_req_nterror(req
, NT_STATUS_PIPE_BROKEN
);
293 static NTSTATUS
rpc_np_trans_recv(struct async_req
*req
, TALLOC_CTX
*mem_ctx
,
294 uint8_t **prdata
, uint32_t *prdata_len
)
296 struct rpc_np_trans_state
*state
= talloc_get_type_abort(
297 req
->private_data
, struct rpc_np_trans_state
);
300 if (async_req_is_nterror(req
, &status
)) {
303 *prdata
= talloc_move(mem_ctx
, &state
->rdata
);
304 *prdata_len
= state
->rdata_len
;
308 struct rpc_transport_np_init_state
{
309 struct rpc_cli_transport
*transport
;
310 struct rpc_transport_np_state
*transport_np
;
313 static void rpc_transport_np_init_pipe_open(struct async_req
*subreq
);
315 struct async_req
*rpc_transport_np_init_send(TALLOC_CTX
*mem_ctx
,
316 struct event_context
*ev
,
317 struct cli_state
*cli
,
318 const struct ndr_syntax_id
*abstract_syntax
)
320 struct async_req
*result
, *subreq
;
321 struct rpc_transport_np_init_state
*state
;
323 if (!async_req_setup(mem_ctx
, &result
, &state
,
324 struct rpc_transport_np_init_state
)) {
328 state
->transport
= talloc(state
, struct rpc_cli_transport
);
329 if (state
->transport
== NULL
) {
332 state
->transport_np
= talloc(state
->transport
,
333 struct rpc_transport_np_state
);
334 if (state
->transport_np
== NULL
) {
337 state
->transport
->priv
= state
->transport_np
;
339 state
->transport_np
->pipe_name
= get_pipe_name_from_iface(
341 state
->transport_np
->cli
= cli
;
343 subreq
= cli_ntcreate_send(
344 state
, ev
, cli
, state
->transport_np
->pipe_name
, 0,
345 DESIRED_ACCESS_PIPE
, 0, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
347 if (subreq
== NULL
) {
350 subreq
->async
.fn
= rpc_transport_np_init_pipe_open
;
351 subreq
->async
.priv
= result
;
359 static void rpc_transport_np_init_pipe_open(struct async_req
*subreq
)
361 struct async_req
*req
= talloc_get_type_abort(
362 subreq
->async
.priv
, struct async_req
);
363 struct rpc_transport_np_init_state
*state
= talloc_get_type_abort(
364 req
->private_data
, struct rpc_transport_np_init_state
);
367 status
= cli_ntcreate_recv(subreq
, &state
->transport_np
->fnum
);
369 if (!NT_STATUS_IS_OK(status
)) {
370 async_req_nterror(req
, status
);
374 talloc_set_destructor(state
->transport_np
,
375 rpc_transport_np_state_destructor
);
379 NTSTATUS
rpc_transport_np_init_recv(struct async_req
*req
,
381 struct rpc_cli_transport
**presult
)
383 struct rpc_transport_np_init_state
*state
= talloc_get_type_abort(
384 req
->private_data
, struct rpc_transport_np_init_state
);
387 if (async_req_is_nterror(req
, &status
)) {
391 state
->transport
->write_send
= rpc_np_write_send
;
392 state
->transport
->write_recv
= rpc_np_write_recv
;
393 state
->transport
->read_send
= rpc_np_read_send
;
394 state
->transport
->read_recv
= rpc_np_read_recv
;
395 state
->transport
->trans_send
= rpc_np_trans_send
;
396 state
->transport
->trans_recv
= rpc_np_trans_recv
;
398 *presult
= talloc_move(mem_ctx
, &state
->transport
);
402 NTSTATUS
rpc_transport_np_init(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
403 const struct ndr_syntax_id
*abstract_syntax
,
404 struct rpc_cli_transport
**presult
)
406 TALLOC_CTX
*frame
= talloc_stackframe();
407 struct event_context
*ev
;
408 struct async_req
*req
;
411 ev
= event_context_init(frame
);
413 status
= NT_STATUS_NO_MEMORY
;
417 req
= rpc_transport_np_init_send(frame
, ev
, cli
, abstract_syntax
);
419 status
= NT_STATUS_NO_MEMORY
;
423 while (req
->state
< ASYNC_REQ_DONE
) {
427 status
= rpc_transport_np_init_recv(req
, mem_ctx
, presult
);
433 struct cli_state
*rpc_pipe_np_smb_conn(struct rpc_pipe_client
*p
)
435 struct rpc_transport_np_state
*state
= talloc_get_type(
436 p
->transport
->priv
, struct rpc_transport_np_state
);