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
)
33 if (s
->cli
->fd
== -1) {
34 DEBUG(10, ("socket was closed, no need to send close request.\n"));
38 if (!NT_STATUS_IS_OK(cli_close(s
->cli
, s
->fnum
))) {
39 DEBUG(1, ("rpc_transport_np_state_destructor: cli_close "
40 "failed on pipe %s. Error was %s\n", s
->pipe_name
,
43 DEBUG(10, ("rpc_pipe_destructor: closed %s\n", s
->pipe_name
));
45 * We can't do much on failure
50 struct rpc_np_write_state
{
55 static void rpc_np_write_done(struct tevent_req
*subreq
);
57 static struct tevent_req
*rpc_np_write_send(TALLOC_CTX
*mem_ctx
,
58 struct event_context
*ev
,
59 const uint8_t *data
, size_t size
,
62 struct rpc_transport_np_state
*np_transport
= talloc_get_type_abort(
63 priv
, struct rpc_transport_np_state
);
64 struct tevent_req
*req
, *subreq
;
65 struct rpc_np_write_state
*state
;
67 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_np_write_state
);
73 subreq
= cli_write_andx_send(mem_ctx
, ev
, np_transport
->cli
,
75 8, /* 8 means message mode. */
77 if (tevent_req_nomem(subreq
, req
)) {
78 return tevent_req_post(req
, ev
);
80 tevent_req_set_callback(subreq
, rpc_np_write_done
, req
);
84 static void rpc_np_write_done(struct tevent_req
*subreq
)
86 struct tevent_req
*req
= tevent_req_callback_data(
87 subreq
, struct tevent_req
);
88 struct rpc_np_write_state
*state
= tevent_req_data(
89 req
, struct rpc_np_write_state
);
92 status
= cli_write_andx_recv(subreq
, &state
->written
);
94 if (!NT_STATUS_IS_OK(status
)) {
95 tevent_req_nterror(req
, status
);
101 static NTSTATUS
rpc_np_write_recv(struct tevent_req
*req
, ssize_t
*pwritten
)
103 struct rpc_np_write_state
*state
= tevent_req_data(
104 req
, struct rpc_np_write_state
);
107 if (tevent_req_is_nterror(req
, &status
)) {
110 *pwritten
= state
->written
;
114 struct rpc_np_read_state
{
120 static void rpc_np_read_done(struct tevent_req
*subreq
);
122 static struct tevent_req
*rpc_np_read_send(TALLOC_CTX
*mem_ctx
,
123 struct event_context
*ev
,
124 uint8_t *data
, size_t size
,
127 struct rpc_transport_np_state
*np_transport
= talloc_get_type_abort(
128 priv
, struct rpc_transport_np_state
);
129 struct tevent_req
*req
, *subreq
;
130 struct rpc_np_read_state
*state
;
132 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_np_read_state
);
139 subreq
= cli_read_andx_send(mem_ctx
, ev
, np_transport
->cli
,
140 np_transport
->fnum
, 0, size
);
141 if (subreq
== NULL
) {
144 tevent_req_set_callback(subreq
, rpc_np_read_done
, req
);
151 static void rpc_np_read_done(struct tevent_req
*subreq
)
153 struct tevent_req
*req
= tevent_req_callback_data(
154 subreq
, struct tevent_req
);
155 struct rpc_np_read_state
*state
= tevent_req_data(
156 req
, struct rpc_np_read_state
);
160 /* We must free subreq in this function as there is
161 a timer event attached to it. */
163 status
= cli_read_andx_recv(subreq
, &state
->received
, &rcvbuf
);
165 * We can't TALLOC_FREE(subreq) as usual here, as rcvbuf still is a
168 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
169 status
= NT_STATUS_OK
;
171 if (!NT_STATUS_IS_OK(status
)) {
173 tevent_req_nterror(req
, status
);
177 if (state
->received
> state
->size
) {
179 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
183 if (state
->received
== 0) {
185 tevent_req_nterror(req
, NT_STATUS_PIPE_BROKEN
);
189 memcpy(state
->data
, rcvbuf
, state
->received
);
191 tevent_req_done(req
);
194 static NTSTATUS
rpc_np_read_recv(struct tevent_req
*req
, ssize_t
*preceived
)
196 struct rpc_np_read_state
*state
= tevent_req_data(
197 req
, struct rpc_np_read_state
);
200 if (tevent_req_is_nterror(req
, &status
)) {
203 *preceived
= state
->received
;
207 struct rpc_np_trans_state
{
209 uint32_t max_rdata_len
;
214 static void rpc_np_trans_done(struct tevent_req
*subreq
);
216 static struct tevent_req
*rpc_np_trans_send(TALLOC_CTX
*mem_ctx
,
217 struct event_context
*ev
,
218 uint8_t *data
, size_t data_len
,
219 uint32_t max_rdata_len
,
222 struct rpc_transport_np_state
*np_transport
= talloc_get_type_abort(
223 priv
, struct rpc_transport_np_state
);
224 struct tevent_req
*req
, *subreq
;
225 struct rpc_np_trans_state
*state
;
227 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_np_trans_state
);
232 state
->max_rdata_len
= max_rdata_len
;
234 SSVAL(state
->setup
+0, 0, TRANSACT_DCERPCCMD
);
235 SSVAL(state
->setup
+1, 0, np_transport
->fnum
);
237 subreq
= cli_trans_send(
238 state
, ev
, np_transport
->cli
, SMBtrans
,
239 "\\PIPE\\", 0, 0, 0, state
->setup
, 2, 0,
240 NULL
, 0, 0, data
, data_len
, max_rdata_len
);
241 if (subreq
== NULL
) {
244 tevent_req_set_callback(subreq
, rpc_np_trans_done
, req
);
252 static void rpc_np_trans_done(struct tevent_req
*subreq
)
254 struct tevent_req
*req
= tevent_req_callback_data(
255 subreq
, struct tevent_req
);
256 struct rpc_np_trans_state
*state
= tevent_req_data(
257 req
, struct rpc_np_trans_state
);
260 status
= cli_trans_recv(subreq
, state
, NULL
, 0, NULL
, NULL
, 0, NULL
,
261 &state
->rdata
, 0, &state
->rdata_len
);
263 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
264 status
= NT_STATUS_OK
;
266 if (!NT_STATUS_IS_OK(status
)) {
267 tevent_req_nterror(req
, status
);
271 if (state
->rdata_len
> state
->max_rdata_len
) {
272 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
276 if (state
->rdata_len
== 0) {
277 tevent_req_nterror(req
, NT_STATUS_PIPE_BROKEN
);
281 tevent_req_done(req
);
284 static NTSTATUS
rpc_np_trans_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
285 uint8_t **prdata
, uint32_t *prdata_len
)
287 struct rpc_np_trans_state
*state
= tevent_req_data(
288 req
, struct rpc_np_trans_state
);
291 if (tevent_req_is_nterror(req
, &status
)) {
294 *prdata
= talloc_move(mem_ctx
, &state
->rdata
);
295 *prdata_len
= state
->rdata_len
;
299 struct rpc_transport_np_init_state
{
300 struct rpc_cli_transport
*transport
;
301 struct rpc_transport_np_state
*transport_np
;
304 static void rpc_transport_np_init_pipe_open(struct tevent_req
*subreq
);
306 struct tevent_req
*rpc_transport_np_init_send(TALLOC_CTX
*mem_ctx
,
307 struct event_context
*ev
,
308 struct cli_state
*cli
,
309 const struct ndr_syntax_id
*abstract_syntax
)
311 struct tevent_req
*req
, *subreq
;
312 struct rpc_transport_np_init_state
*state
;
314 req
= tevent_req_create(mem_ctx
, &state
,
315 struct rpc_transport_np_init_state
);
320 state
->transport
= talloc(state
, struct rpc_cli_transport
);
321 if (tevent_req_nomem(state
->transport
, req
)) {
322 return tevent_req_post(req
, ev
);
324 state
->transport_np
= talloc(state
->transport
,
325 struct rpc_transport_np_state
);
326 if (tevent_req_nomem(state
->transport_np
, req
)) {
327 return tevent_req_post(req
, ev
);
329 state
->transport
->priv
= state
->transport_np
;
331 state
->transport_np
->pipe_name
= get_pipe_name_from_syntax(
332 state
->transport_np
, abstract_syntax
);
333 state
->transport_np
->cli
= cli
;
335 subreq
= cli_ntcreate_send(
336 state
, ev
, cli
, state
->transport_np
->pipe_name
, 0,
337 DESIRED_ACCESS_PIPE
, 0, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
339 if (tevent_req_nomem(subreq
, req
)) {
340 return tevent_req_post(req
, ev
);
342 tevent_req_set_callback(subreq
, rpc_transport_np_init_pipe_open
,
347 static void rpc_transport_np_init_pipe_open(struct tevent_req
*subreq
)
349 struct tevent_req
*req
= tevent_req_callback_data(
350 subreq
, struct tevent_req
);
351 struct rpc_transport_np_init_state
*state
= tevent_req_data(
352 req
, struct rpc_transport_np_init_state
);
355 status
= cli_ntcreate_recv(subreq
, &state
->transport_np
->fnum
);
357 if (!NT_STATUS_IS_OK(status
)) {
358 tevent_req_nterror(req
, status
);
362 talloc_set_destructor(state
->transport_np
,
363 rpc_transport_np_state_destructor
);
364 tevent_req_done(req
);
367 NTSTATUS
rpc_transport_np_init_recv(struct tevent_req
*req
,
369 struct rpc_cli_transport
**presult
)
371 struct rpc_transport_np_init_state
*state
= tevent_req_data(
372 req
, struct rpc_transport_np_init_state
);
375 if (tevent_req_is_nterror(req
, &status
)) {
379 state
->transport
->write_send
= rpc_np_write_send
;
380 state
->transport
->write_recv
= rpc_np_write_recv
;
381 state
->transport
->read_send
= rpc_np_read_send
;
382 state
->transport
->read_recv
= rpc_np_read_recv
;
383 state
->transport
->trans_send
= rpc_np_trans_send
;
384 state
->transport
->trans_recv
= rpc_np_trans_recv
;
386 *presult
= talloc_move(mem_ctx
, &state
->transport
);
390 NTSTATUS
rpc_transport_np_init(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
391 const struct ndr_syntax_id
*abstract_syntax
,
392 struct rpc_cli_transport
**presult
)
394 TALLOC_CTX
*frame
= talloc_stackframe();
395 struct event_context
*ev
;
396 struct tevent_req
*req
;
397 NTSTATUS status
= NT_STATUS_OK
;
399 ev
= event_context_init(frame
);
401 status
= NT_STATUS_NO_MEMORY
;
405 req
= rpc_transport_np_init_send(frame
, ev
, cli
, abstract_syntax
);
407 status
= NT_STATUS_NO_MEMORY
;
411 if (!tevent_req_poll(req
, ev
)) {
412 status
= map_nt_error_from_unix(errno
);
416 status
= rpc_transport_np_init_recv(req
, mem_ctx
, presult
);
422 struct cli_state
*rpc_pipe_np_smb_conn(struct rpc_pipe_client
*p
)
424 struct rpc_transport_np_state
*state
= talloc_get_type(
425 p
->transport
->priv
, struct rpc_transport_np_state
);
433 void rpccli_close_np_fd(struct rpc_pipe_client
*p
)
435 struct cli_state
*cli
= rpc_pipe_np_smb_conn(p
);