2 * Unix SMB/CIFS implementation.
3 * RPC client transport over named pipes to a child smbd
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
26 * struct rpc_cli_smbd_conn represents a forked smbd. This structure should
27 * exist only once per process which does the rpc calls.
29 * RPC pipe handles can be attached to this smbd connection with
30 * rpc_pipe_open_local().
32 * For this to work right, we can not use rpc_transport_np directly, because
33 * the child smbd wants to write its DEBUG output somewhere. We redirect the
34 * child's output to rpc_cli_smbd_conn->stdout_fd. While the RPC calls are
35 * active, we have an event context available and attach a fd event to the
39 struct rpc_cli_smbd_conn
{
41 * The smb connection to handle the named pipe traffic over
43 struct cli_state
*cli
;
46 * Attached to stdout in the forked smbd, this is where smbd will
52 * Custom callback provided by the owner of the
53 * rpc_cli_smbd_conn. Here we send the smbd DEBUG output. Can be NULL.
56 void (*fn
)(char *buf
, size_t len
, void *priv
);
62 * Event handler to be called whenever the forked smbd prints debugging
66 static void rpc_cli_smbd_stdout_reader(struct event_context
*ev
,
68 uint16_t flags
, void *priv
)
70 struct rpc_cli_smbd_conn
*conn
= talloc_get_type_abort(
71 priv
, struct rpc_cli_smbd_conn
);
75 if ((flags
& EVENT_FD_READ
) == 0) {
79 nread
= read(conn
->stdout_fd
, buf
, sizeof(buf
)-1);
81 DEBUG(0, ("Could not read from smbd stdout: %s\n",
87 DEBUG(0, ("EOF from smbd stdout\n"));
92 if (conn
->stdout_callback
.fn
!= NULL
) {
93 conn
->stdout_callback
.fn(buf
, nread
,
94 conn
->stdout_callback
.priv
);
99 * struct rpc_transport_smbd_state is the link from a struct rpc_pipe_client
100 * to the rpc_cli_smbd_conn. We use a named pipe transport as a subtransport.
103 struct rpc_transport_smbd_state
{
104 struct rpc_cli_smbd_conn
*conn
;
105 struct rpc_cli_transport
*sub_transp
;
108 static int rpc_cli_smbd_conn_destructor(struct rpc_cli_smbd_conn
*conn
)
110 if (conn
->cli
!= NULL
) {
111 cli_shutdown(conn
->cli
);
114 if (conn
->stdout_fd
!= -1) {
115 close(conn
->stdout_fd
);
116 conn
->stdout_fd
= -1;
122 * Do the negprot/sesssetup/tcon to an anonymous ipc$ connection
125 struct get_anon_ipc_state
{
126 struct event_context
*ev
;
127 struct cli_state
*cli
;
130 static void get_anon_ipc_negprot_done(struct async_req
*subreq
);
131 static void get_anon_ipc_sesssetup_done(struct async_req
*subreq
);
132 static void get_anon_ipc_tcon_done(struct async_req
*subreq
);
134 static struct async_req
*get_anon_ipc_send(TALLOC_CTX
*mem_ctx
,
135 struct event_context
*ev
,
136 struct cli_state
*cli
)
138 struct async_req
*result
, *subreq
;
139 struct get_anon_ipc_state
*state
;
141 if (!async_req_setup(mem_ctx
, &result
, &state
,
142 struct get_anon_ipc_state
)) {
149 subreq
= cli_negprot_send(state
, ev
, cli
);
150 if (subreq
== NULL
) {
153 subreq
->async
.fn
= get_anon_ipc_negprot_done
;
154 subreq
->async
.priv
= result
;
161 static void get_anon_ipc_negprot_done(struct async_req
*subreq
)
163 struct async_req
*req
= talloc_get_type_abort(
164 subreq
->async
.priv
, struct async_req
);
165 struct get_anon_ipc_state
*state
= talloc_get_type_abort(
166 req
->private_data
, struct get_anon_ipc_state
);
169 status
= cli_negprot_recv(subreq
);
171 if (!NT_STATUS_IS_OK(status
)) {
172 async_req_nterror(req
, status
);
176 subreq
= cli_session_setup_guest_send(state
, state
->ev
, state
->cli
);
177 if (async_req_nomem(subreq
, req
)) {
180 subreq
->async
.fn
= get_anon_ipc_sesssetup_done
;
181 subreq
->async
.priv
= req
;
184 static void get_anon_ipc_sesssetup_done(struct async_req
*subreq
)
186 struct async_req
*req
= talloc_get_type_abort(
187 subreq
->async
.priv
, struct async_req
);
188 struct get_anon_ipc_state
*state
= talloc_get_type_abort(
189 req
->private_data
, struct get_anon_ipc_state
);
192 status
= cli_session_setup_guest_recv(subreq
);
194 if (!NT_STATUS_IS_OK(status
)) {
195 async_req_nterror(req
, status
);
199 subreq
= cli_tcon_andx_send(state
, state
->ev
, state
->cli
,
200 "IPC$", "IPC", NULL
, 0);
201 if (async_req_nomem(subreq
, req
)) {
204 subreq
->async
.fn
= get_anon_ipc_tcon_done
;
205 subreq
->async
.priv
= req
;
208 static void get_anon_ipc_tcon_done(struct async_req
*subreq
)
210 struct async_req
*req
= talloc_get_type_abort(
211 subreq
->async
.priv
, struct async_req
);
214 status
= cli_tcon_andx_recv(subreq
);
216 if (!NT_STATUS_IS_OK(status
)) {
217 async_req_nterror(req
, status
);
223 static NTSTATUS
get_anon_ipc_recv(struct async_req
*req
)
225 return async_req_simple_recv_ntstatus(req
);
228 struct rpc_cli_smbd_conn_init_state
{
229 struct event_context
*ev
;
230 struct rpc_cli_smbd_conn
*conn
;
233 static void rpc_cli_smbd_conn_init_done(struct async_req
*subreq
);
235 struct async_req
*rpc_cli_smbd_conn_init_send(TALLOC_CTX
*mem_ctx
,
236 struct event_context
*ev
,
237 void (*stdout_callback
)(char *buf
,
242 struct async_req
*result
, *subreq
;
243 struct rpc_cli_smbd_conn_init_state
*state
;
250 smb_sock
[0] = smb_sock
[1] = stdout_pipe
[0] = stdout_pipe
[1] = -1;
252 if (!async_req_setup(mem_ctx
, &result
, &state
,
253 struct rpc_cli_smbd_conn_init_state
)) {
258 state
->conn
= talloc(state
, struct rpc_cli_smbd_conn
);
259 if (state
->conn
== NULL
) {
263 state
->conn
->cli
= cli_initialise();
264 if (state
->conn
->cli
== NULL
) {
267 state
->conn
->stdout_fd
= -1;
268 state
->conn
->stdout_callback
.fn
= stdout_callback
;
269 state
->conn
->stdout_callback
.priv
= priv
;
270 talloc_set_destructor(state
->conn
, rpc_cli_smbd_conn_destructor
);
272 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, smb_sock
);
274 status
= map_nt_error_from_unix(errno
);
277 ret
= pipe(stdout_pipe
);
279 status
= map_nt_error_from_unix(errno
);
285 status
= map_nt_error_from_unix(errno
);
292 close(stdout_pipe
[0]);
294 if (dup(smb_sock
[1]) == -1) {
299 if (dup(stdout_pipe
[1]) == -1) {
302 close(stdout_pipe
[1]);
304 smbd_cmd
= getenv("SMB_PATH");
306 if ((smbd_cmd
== NULL
)
307 && (asprintf(&smbd_cmd
, "%s/smbd", get_dyn_SBINDIR())
312 if (asprintf(&smbd_cmd
, "%s -F -S -d %d", smbd_cmd
,
318 exit(system(smbd_cmd
));
321 state
->conn
->cli
->fd
= smb_sock
[0];
326 state
->conn
->stdout_fd
= stdout_pipe
[0];
328 close(stdout_pipe
[1]);
331 subreq
= get_anon_ipc_send(state
, ev
, state
->conn
->cli
);
332 if (subreq
== NULL
) {
336 if (event_add_fd(ev
, subreq
, state
->conn
->stdout_fd
, EVENT_FD_READ
,
337 rpc_cli_smbd_stdout_reader
, state
->conn
) == NULL
) {
341 subreq
->async
.fn
= rpc_cli_smbd_conn_init_done
;
342 subreq
->async
.priv
= result
;
346 status
= NT_STATUS_NO_MEMORY
;
348 if (smb_sock
[0] != -1) {
351 if (smb_sock
[1] != -1) {
354 if (stdout_pipe
[0] != -1) {
355 close(stdout_pipe
[0]);
357 if (stdout_pipe
[1] != -1) {
358 close(stdout_pipe
[1]);
360 if (async_post_ntstatus(result
, ev
, status
)) {
367 static void rpc_cli_smbd_conn_init_done(struct async_req
*subreq
)
369 struct async_req
*req
= talloc_get_type_abort(
370 subreq
->async
.priv
, struct async_req
);
373 status
= get_anon_ipc_recv(subreq
);
375 if (!NT_STATUS_IS_OK(status
)) {
376 async_req_nterror(req
, status
);
382 NTSTATUS
rpc_cli_smbd_conn_init_recv(struct async_req
*req
,
384 struct rpc_cli_smbd_conn
**pconn
)
386 struct rpc_cli_smbd_conn_init_state
*state
= talloc_get_type_abort(
387 req
->private_data
, struct rpc_cli_smbd_conn_init_state
);
390 if (async_req_is_nterror(req
, &status
)) {
393 *pconn
= talloc_move(mem_ctx
, &state
->conn
);
397 NTSTATUS
rpc_cli_smbd_conn_init(TALLOC_CTX
*mem_ctx
,
398 struct rpc_cli_smbd_conn
**pconn
,
399 void (*stdout_callback
)(char *buf
,
404 TALLOC_CTX
*frame
= talloc_stackframe();
405 struct event_context
*ev
;
406 struct async_req
*req
;
409 ev
= event_context_init(frame
);
411 status
= NT_STATUS_NO_MEMORY
;
415 req
= rpc_cli_smbd_conn_init_send(frame
, ev
, stdout_callback
, priv
);
417 status
= NT_STATUS_NO_MEMORY
;
421 while (req
->state
< ASYNC_REQ_DONE
) {
425 status
= rpc_cli_smbd_conn_init_recv(req
, mem_ctx
, pconn
);
431 struct rpc_smbd_write_state
{
432 struct rpc_cli_transport
*sub_transp
;
436 static void rpc_smbd_write_done(struct async_req
*subreq
);
438 static struct async_req
*rpc_smbd_write_send(TALLOC_CTX
*mem_ctx
,
439 struct event_context
*ev
,
440 const uint8_t *data
, size_t size
,
443 struct rpc_transport_smbd_state
*transp
= talloc_get_type_abort(
444 priv
, struct rpc_transport_smbd_state
);
445 struct async_req
*result
, *subreq
;
446 struct rpc_smbd_write_state
*state
;
448 if (!async_req_setup(mem_ctx
, &result
, &state
,
449 struct rpc_smbd_write_state
)) {
452 state
->sub_transp
= transp
->sub_transp
;
454 subreq
= transp
->sub_transp
->write_send(state
, ev
, data
, size
,
455 transp
->sub_transp
->priv
);
456 if (subreq
== NULL
) {
460 if (event_add_fd(ev
, subreq
, transp
->conn
->stdout_fd
, EVENT_FD_READ
,
461 rpc_cli_smbd_stdout_reader
, transp
->conn
) == NULL
) {
465 subreq
->async
.fn
= rpc_smbd_write_done
;
466 subreq
->async
.priv
= result
;
474 static void rpc_smbd_write_done(struct async_req
*subreq
)
476 struct async_req
*req
= talloc_get_type_abort(
477 subreq
->async
.priv
, struct async_req
);
478 struct rpc_smbd_write_state
*state
= talloc_get_type_abort(
479 req
->private_data
, struct rpc_smbd_write_state
);
482 status
= state
->sub_transp
->write_recv(subreq
, &state
->written
);
484 if (!NT_STATUS_IS_OK(status
)) {
485 async_req_nterror(req
, status
);
491 static NTSTATUS
rpc_smbd_write_recv(struct async_req
*req
, ssize_t
*pwritten
)
493 struct rpc_smbd_write_state
*state
= talloc_get_type_abort(
494 req
->private_data
, struct rpc_smbd_write_state
);
497 if (async_req_is_nterror(req
, &status
)) {
500 *pwritten
= state
->written
;
504 struct rpc_smbd_read_state
{
505 struct rpc_cli_transport
*sub_transp
;
509 static void rpc_smbd_read_done(struct async_req
*subreq
);
511 static struct async_req
*rpc_smbd_read_send(TALLOC_CTX
*mem_ctx
,
512 struct event_context
*ev
,
513 uint8_t *data
, size_t size
,
516 struct rpc_transport_smbd_state
*transp
= talloc_get_type_abort(
517 priv
, struct rpc_transport_smbd_state
);
518 struct async_req
*result
, *subreq
;
519 struct rpc_smbd_read_state
*state
;
521 if (!async_req_setup(mem_ctx
, &result
, &state
,
522 struct rpc_smbd_read_state
)) {
525 state
->sub_transp
= transp
->sub_transp
;
527 subreq
= transp
->sub_transp
->read_send(state
, ev
, data
, size
,
528 transp
->sub_transp
->priv
);
529 if (subreq
== NULL
) {
533 if (event_add_fd(ev
, subreq
, transp
->conn
->stdout_fd
, EVENT_FD_READ
,
534 rpc_cli_smbd_stdout_reader
, transp
->conn
) == NULL
) {
538 subreq
->async
.fn
= rpc_smbd_read_done
;
539 subreq
->async
.priv
= result
;
547 static void rpc_smbd_read_done(struct async_req
*subreq
)
549 struct async_req
*req
= talloc_get_type_abort(
550 subreq
->async
.priv
, struct async_req
);
551 struct rpc_smbd_read_state
*state
= talloc_get_type_abort(
552 req
->private_data
, struct rpc_smbd_read_state
);
555 status
= state
->sub_transp
->read_recv(subreq
, &state
->received
);
557 if (!NT_STATUS_IS_OK(status
)) {
558 async_req_nterror(req
, status
);
564 static NTSTATUS
rpc_smbd_read_recv(struct async_req
*req
, ssize_t
*preceived
)
566 struct rpc_smbd_read_state
*state
= talloc_get_type_abort(
567 req
->private_data
, struct rpc_smbd_read_state
);
570 if (async_req_is_nterror(req
, &status
)) {
573 *preceived
= state
->received
;
577 struct rpc_transport_smbd_init_state
{
578 struct rpc_cli_transport
*transport
;
579 struct rpc_transport_smbd_state
*transport_smbd
;
582 static void rpc_transport_smbd_init_done(struct async_req
*subreq
);
584 struct async_req
*rpc_transport_smbd_init_send(TALLOC_CTX
*mem_ctx
,
585 struct event_context
*ev
,
586 struct rpc_cli_smbd_conn
*conn
,
587 const struct ndr_syntax_id
*abstract_syntax
)
589 struct async_req
*result
, *subreq
;
590 struct rpc_transport_smbd_init_state
*state
;
592 if (!async_req_setup(mem_ctx
, &result
, &state
,
593 struct rpc_transport_smbd_init_state
)) {
597 state
->transport
= talloc(state
, struct rpc_cli_transport
);
598 if (state
->transport
== NULL
) {
601 state
->transport_smbd
= talloc(state
->transport
,
602 struct rpc_transport_smbd_state
);
603 if (state
->transport_smbd
== NULL
) {
606 state
->transport_smbd
->conn
= conn
;
607 state
->transport
->priv
= state
->transport_smbd
;
609 subreq
= rpc_transport_np_init_send(state
, ev
, conn
->cli
,
611 if (subreq
== NULL
) {
614 subreq
->async
.fn
= rpc_transport_smbd_init_done
;
615 subreq
->async
.priv
= result
;
623 static void rpc_transport_smbd_init_done(struct async_req
*subreq
)
625 struct async_req
*req
= talloc_get_type_abort(
626 subreq
->async
.priv
, struct async_req
);
627 struct rpc_transport_smbd_init_state
*state
= talloc_get_type_abort(
628 req
->private_data
, struct rpc_transport_smbd_init_state
);
631 status
= rpc_transport_np_init_recv(
632 subreq
, state
->transport_smbd
,
633 &state
->transport_smbd
->sub_transp
);
635 if (!NT_STATUS_IS_OK(status
)) {
636 async_req_nterror(req
, status
);
642 NTSTATUS
rpc_transport_smbd_init_recv(struct async_req
*req
,
644 struct rpc_cli_transport
**presult
)
646 struct rpc_transport_smbd_init_state
*state
= talloc_get_type_abort(
647 req
->private_data
, struct rpc_transport_smbd_init_state
);
650 if (async_req_is_nterror(req
, &status
)) {
654 state
->transport
->write_send
= rpc_smbd_write_send
;
655 state
->transport
->write_recv
= rpc_smbd_write_recv
;
656 state
->transport
->read_send
= rpc_smbd_read_send
;
657 state
->transport
->read_recv
= rpc_smbd_read_recv
;
658 state
->transport
->trans_send
= NULL
;
659 state
->transport
->trans_recv
= NULL
;
661 *presult
= talloc_move(mem_ctx
, &state
->transport
);
665 NTSTATUS
rpc_transport_smbd_init(TALLOC_CTX
*mem_ctx
,
666 struct rpc_cli_smbd_conn
*conn
,
667 const struct ndr_syntax_id
*abstract_syntax
,
668 struct rpc_cli_transport
**presult
)
670 TALLOC_CTX
*frame
= talloc_stackframe();
671 struct event_context
*ev
;
672 struct async_req
*req
;
675 ev
= event_context_init(frame
);
677 status
= NT_STATUS_NO_MEMORY
;
681 req
= rpc_transport_smbd_init_send(frame
, ev
, conn
, abstract_syntax
);
683 status
= NT_STATUS_NO_MEMORY
;
687 while (req
->state
< ASYNC_REQ_DONE
) {
691 status
= rpc_transport_smbd_init_recv(req
, mem_ctx
, presult
);