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", smbd_cmd
) == -1) {
317 exit(system(smbd_cmd
));
320 state
->conn
->cli
->fd
= smb_sock
[0];
325 state
->conn
->stdout_fd
= stdout_pipe
[0];
327 close(stdout_pipe
[1]);
330 subreq
= get_anon_ipc_send(state
, ev
, state
->conn
->cli
);
331 if (subreq
== NULL
) {
335 if (event_add_fd(ev
, subreq
, state
->conn
->stdout_fd
, EVENT_FD_READ
,
336 rpc_cli_smbd_stdout_reader
, state
->conn
) == NULL
) {
340 subreq
->async
.fn
= rpc_cli_smbd_conn_init_done
;
341 subreq
->async
.priv
= result
;
345 status
= NT_STATUS_NO_MEMORY
;
347 if (smb_sock
[0] != -1) {
350 if (smb_sock
[1] != -1) {
353 if (stdout_pipe
[0] != -1) {
354 close(stdout_pipe
[0]);
356 if (stdout_pipe
[1] != -1) {
357 close(stdout_pipe
[1]);
359 if (async_post_ntstatus(result
, ev
, status
)) {
366 static void rpc_cli_smbd_conn_init_done(struct async_req
*subreq
)
368 struct async_req
*req
= talloc_get_type_abort(
369 subreq
->async
.priv
, struct async_req
);
372 status
= get_anon_ipc_recv(subreq
);
374 if (!NT_STATUS_IS_OK(status
)) {
375 async_req_nterror(req
, status
);
381 NTSTATUS
rpc_cli_smbd_conn_init_recv(struct async_req
*req
,
383 struct rpc_cli_smbd_conn
**pconn
)
385 struct rpc_cli_smbd_conn_init_state
*state
= talloc_get_type_abort(
386 req
->private_data
, struct rpc_cli_smbd_conn_init_state
);
389 if (async_req_is_nterror(req
, &status
)) {
392 *pconn
= talloc_move(mem_ctx
, &state
->conn
);
396 NTSTATUS
rpc_cli_smbd_conn_init(TALLOC_CTX
*mem_ctx
,
397 struct rpc_cli_smbd_conn
**pconn
,
398 void (*stdout_callback
)(char *buf
,
403 TALLOC_CTX
*frame
= talloc_stackframe();
404 struct event_context
*ev
;
405 struct async_req
*req
;
408 ev
= event_context_init(frame
);
410 status
= NT_STATUS_NO_MEMORY
;
414 req
= rpc_cli_smbd_conn_init_send(frame
, ev
, stdout_callback
, priv
);
416 status
= NT_STATUS_NO_MEMORY
;
420 while (req
->state
< ASYNC_REQ_DONE
) {
424 status
= rpc_cli_smbd_conn_init_recv(req
, mem_ctx
, pconn
);
430 struct rpc_smbd_write_state
{
431 struct rpc_cli_transport
*sub_transp
;
435 static void rpc_smbd_write_done(struct async_req
*subreq
);
437 static struct async_req
*rpc_smbd_write_send(TALLOC_CTX
*mem_ctx
,
438 struct event_context
*ev
,
439 const uint8_t *data
, size_t size
,
442 struct rpc_transport_smbd_state
*transp
= talloc_get_type_abort(
443 priv
, struct rpc_transport_smbd_state
);
444 struct async_req
*result
, *subreq
;
445 struct rpc_smbd_write_state
*state
;
447 if (!async_req_setup(mem_ctx
, &result
, &state
,
448 struct rpc_smbd_write_state
)) {
451 state
->sub_transp
= transp
->sub_transp
;
453 subreq
= transp
->sub_transp
->write_send(state
, ev
, data
, size
,
454 transp
->sub_transp
->priv
);
455 if (subreq
== NULL
) {
459 if (event_add_fd(ev
, subreq
, transp
->conn
->stdout_fd
, EVENT_FD_READ
,
460 rpc_cli_smbd_stdout_reader
, transp
->conn
) == NULL
) {
464 subreq
->async
.fn
= rpc_smbd_write_done
;
465 subreq
->async
.priv
= result
;
473 static void rpc_smbd_write_done(struct async_req
*subreq
)
475 struct async_req
*req
= talloc_get_type_abort(
476 subreq
->async
.priv
, struct async_req
);
477 struct rpc_smbd_write_state
*state
= talloc_get_type_abort(
478 req
->private_data
, struct rpc_smbd_write_state
);
481 status
= state
->sub_transp
->write_recv(subreq
, &state
->written
);
483 if (!NT_STATUS_IS_OK(status
)) {
484 async_req_nterror(req
, status
);
490 static NTSTATUS
rpc_smbd_write_recv(struct async_req
*req
, ssize_t
*pwritten
)
492 struct rpc_smbd_write_state
*state
= talloc_get_type_abort(
493 req
->private_data
, struct rpc_smbd_write_state
);
496 if (async_req_is_nterror(req
, &status
)) {
499 *pwritten
= state
->written
;
503 struct rpc_smbd_read_state
{
504 struct rpc_cli_transport
*sub_transp
;
508 static void rpc_smbd_read_done(struct async_req
*subreq
);
510 static struct async_req
*rpc_smbd_read_send(TALLOC_CTX
*mem_ctx
,
511 struct event_context
*ev
,
512 uint8_t *data
, size_t size
,
515 struct rpc_transport_smbd_state
*transp
= talloc_get_type_abort(
516 priv
, struct rpc_transport_smbd_state
);
517 struct async_req
*result
, *subreq
;
518 struct rpc_smbd_read_state
*state
;
520 if (!async_req_setup(mem_ctx
, &result
, &state
,
521 struct rpc_smbd_read_state
)) {
524 state
->sub_transp
= transp
->sub_transp
;
526 subreq
= transp
->sub_transp
->read_send(state
, ev
, data
, size
,
527 transp
->sub_transp
->priv
);
528 if (subreq
== NULL
) {
532 if (event_add_fd(ev
, subreq
, transp
->conn
->stdout_fd
, EVENT_FD_READ
,
533 rpc_cli_smbd_stdout_reader
, transp
->conn
) == NULL
) {
537 subreq
->async
.fn
= rpc_smbd_read_done
;
538 subreq
->async
.priv
= result
;
546 static void rpc_smbd_read_done(struct async_req
*subreq
)
548 struct async_req
*req
= talloc_get_type_abort(
549 subreq
->async
.priv
, struct async_req
);
550 struct rpc_smbd_read_state
*state
= talloc_get_type_abort(
551 req
->private_data
, struct rpc_smbd_read_state
);
554 status
= state
->sub_transp
->read_recv(subreq
, &state
->received
);
556 if (!NT_STATUS_IS_OK(status
)) {
557 async_req_nterror(req
, status
);
563 static NTSTATUS
rpc_smbd_read_recv(struct async_req
*req
, ssize_t
*preceived
)
565 struct rpc_smbd_read_state
*state
= talloc_get_type_abort(
566 req
->private_data
, struct rpc_smbd_read_state
);
569 if (async_req_is_nterror(req
, &status
)) {
572 *preceived
= state
->received
;
576 struct rpc_transport_smbd_init_state
{
577 struct rpc_cli_transport
*transport
;
578 struct rpc_transport_smbd_state
*transport_smbd
;
581 static void rpc_transport_smbd_init_done(struct async_req
*subreq
);
583 struct async_req
*rpc_transport_smbd_init_send(TALLOC_CTX
*mem_ctx
,
584 struct event_context
*ev
,
585 struct rpc_cli_smbd_conn
*conn
,
586 const struct ndr_syntax_id
*abstract_syntax
)
588 struct async_req
*result
, *subreq
;
589 struct rpc_transport_smbd_init_state
*state
;
591 if (!async_req_setup(mem_ctx
, &result
, &state
,
592 struct rpc_transport_smbd_init_state
)) {
596 state
->transport
= talloc(state
, struct rpc_cli_transport
);
597 if (state
->transport
== NULL
) {
600 state
->transport_smbd
= talloc(state
->transport
,
601 struct rpc_transport_smbd_state
);
602 if (state
->transport_smbd
== NULL
) {
605 state
->transport_smbd
->conn
= conn
;
606 state
->transport
->priv
= state
->transport_smbd
;
608 subreq
= rpc_transport_np_init_send(state
, ev
, conn
->cli
,
610 if (subreq
== NULL
) {
613 subreq
->async
.fn
= rpc_transport_smbd_init_done
;
614 subreq
->async
.priv
= result
;
622 static void rpc_transport_smbd_init_done(struct async_req
*subreq
)
624 struct async_req
*req
= talloc_get_type_abort(
625 subreq
->async
.priv
, struct async_req
);
626 struct rpc_transport_smbd_init_state
*state
= talloc_get_type_abort(
627 req
->private_data
, struct rpc_transport_smbd_init_state
);
630 status
= rpc_transport_np_init_recv(
631 subreq
, state
->transport_smbd
,
632 &state
->transport_smbd
->sub_transp
);
634 if (!NT_STATUS_IS_OK(status
)) {
635 async_req_nterror(req
, status
);
641 NTSTATUS
rpc_transport_smbd_init_recv(struct async_req
*req
,
643 struct rpc_cli_transport
**presult
)
645 struct rpc_transport_smbd_init_state
*state
= talloc_get_type_abort(
646 req
->private_data
, struct rpc_transport_smbd_init_state
);
649 if (async_req_is_nterror(req
, &status
)) {
653 state
->transport
->write_send
= rpc_smbd_write_send
;
654 state
->transport
->write_recv
= rpc_smbd_write_recv
;
655 state
->transport
->read_send
= rpc_smbd_read_send
;
656 state
->transport
->read_recv
= rpc_smbd_read_recv
;
657 state
->transport
->trans_send
= NULL
;
658 state
->transport
->trans_recv
= NULL
;
660 *presult
= talloc_move(mem_ctx
, &state
->transport
);
664 NTSTATUS
rpc_transport_smbd_init(TALLOC_CTX
*mem_ctx
,
665 struct rpc_cli_smbd_conn
*conn
,
666 const struct ndr_syntax_id
*abstract_syntax
,
667 struct rpc_cli_transport
**presult
)
669 TALLOC_CTX
*frame
= talloc_stackframe();
670 struct event_context
*ev
;
671 struct async_req
*req
;
674 ev
= event_context_init(frame
);
676 status
= NT_STATUS_NO_MEMORY
;
680 req
= rpc_transport_smbd_init_send(frame
, ev
, conn
, abstract_syntax
);
682 status
= NT_STATUS_NO_MEMORY
;
686 while (req
->state
< ASYNC_REQ_DONE
) {
690 status
= rpc_transport_smbd_init_recv(req
, mem_ctx
, presult
);