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"));
93 if (conn
->stdout_callback
.fn
!= NULL
) {
94 conn
->stdout_callback
.fn(buf
, nread
,
95 conn
->stdout_callback
.priv
);
100 * struct rpc_transport_smbd_state is the link from a struct rpc_pipe_client
101 * to the rpc_cli_smbd_conn. We use a named pipe transport as a subtransport.
104 struct rpc_transport_smbd_state
{
105 struct rpc_cli_smbd_conn
*conn
;
106 struct rpc_cli_transport
*sub_transp
;
109 static int rpc_cli_smbd_conn_destructor(struct rpc_cli_smbd_conn
*conn
)
111 if (conn
->cli
!= NULL
) {
112 cli_shutdown(conn
->cli
);
115 if (conn
->stdout_fd
!= -1) {
116 close(conn
->stdout_fd
);
117 conn
->stdout_fd
= -1;
123 * Do the negprot/sesssetup/tcon to an anonymous ipc$ connection
126 struct get_anon_ipc_state
{
127 struct event_context
*ev
;
128 struct cli_state
*cli
;
131 static void get_anon_ipc_negprot_done(struct tevent_req
*subreq
);
132 static void get_anon_ipc_sesssetup_done(struct tevent_req
*subreq
);
133 static void get_anon_ipc_tcon_done(struct tevent_req
*subreq
);
135 static struct tevent_req
*get_anon_ipc_send(TALLOC_CTX
*mem_ctx
,
136 struct event_context
*ev
,
137 struct cli_state
*cli
)
139 struct tevent_req
*req
, *subreq
;
140 struct get_anon_ipc_state
*state
;
142 req
= tevent_req_create(mem_ctx
, &state
, struct get_anon_ipc_state
);
149 subreq
= cli_negprot_send(state
, ev
, cli
);
150 if (tevent_req_nomem(subreq
, req
)) {
151 return tevent_req_post(req
, ev
);
153 tevent_req_set_callback(subreq
, get_anon_ipc_negprot_done
, req
);
157 static void get_anon_ipc_negprot_done(struct tevent_req
*subreq
)
159 struct tevent_req
*req
= tevent_req_callback_data(
160 subreq
, struct tevent_req
);
161 struct get_anon_ipc_state
*state
= tevent_req_data(
162 req
, struct get_anon_ipc_state
);
165 status
= cli_negprot_recv(subreq
);
167 if (!NT_STATUS_IS_OK(status
)) {
168 tevent_req_nterror(req
, status
);
172 subreq
= cli_session_setup_guest_send(state
, state
->ev
, state
->cli
);
173 if (tevent_req_nomem(subreq
, req
)) {
176 tevent_req_set_callback(subreq
, get_anon_ipc_sesssetup_done
, req
);
179 static void get_anon_ipc_sesssetup_done(struct tevent_req
*subreq
)
181 struct tevent_req
*req
= tevent_req_callback_data(
182 subreq
, struct tevent_req
);
183 struct get_anon_ipc_state
*state
= tevent_req_data(
184 req
, struct get_anon_ipc_state
);
187 status
= cli_session_setup_guest_recv(subreq
);
189 if (!NT_STATUS_IS_OK(status
)) {
190 tevent_req_nterror(req
, status
);
194 subreq
= cli_tcon_andx_send(state
, state
->ev
, state
->cli
,
195 "IPC$", "IPC", NULL
, 0);
196 if (tevent_req_nomem(subreq
, req
)) {
199 tevent_req_set_callback(subreq
, get_anon_ipc_tcon_done
, req
);
202 static void get_anon_ipc_tcon_done(struct tevent_req
*subreq
)
204 struct tevent_req
*req
= tevent_req_callback_data(
205 subreq
, struct tevent_req
);
208 status
= cli_tcon_andx_recv(subreq
);
210 if (!NT_STATUS_IS_OK(status
)) {
211 tevent_req_nterror(req
, status
);
214 tevent_req_done(req
);
217 static NTSTATUS
get_anon_ipc_recv(struct tevent_req
*req
)
219 return tevent_req_simple_recv_ntstatus(req
);
222 struct rpc_cli_smbd_conn_init_state
{
223 struct event_context
*ev
;
224 struct rpc_cli_smbd_conn
*conn
;
227 static void rpc_cli_smbd_conn_init_done(struct tevent_req
*subreq
);
229 struct tevent_req
*rpc_cli_smbd_conn_init_send(TALLOC_CTX
*mem_ctx
,
230 struct event_context
*ev
,
231 void (*stdout_callback
)(char *buf
,
236 struct tevent_req
*req
, *subreq
;
237 struct rpc_cli_smbd_conn_init_state
*state
;
244 smb_sock
[0] = smb_sock
[1] = stdout_pipe
[0] = stdout_pipe
[1] = -1;
246 req
= tevent_req_create(mem_ctx
, &state
,
247 struct rpc_cli_smbd_conn_init_state
);
253 state
->conn
= talloc(state
, struct rpc_cli_smbd_conn
);
254 if (tevent_req_nomem(state
->conn
, req
)) {
255 return tevent_req_post(req
, ev
);
258 state
->conn
->cli
= cli_initialise();
259 if (tevent_req_nomem(state
->conn
->cli
, req
)) {
260 return tevent_req_post(req
, ev
);
262 state
->conn
->stdout_fd
= -1;
263 state
->conn
->stdout_callback
.fn
= stdout_callback
;
264 state
->conn
->stdout_callback
.priv
= priv
;
265 talloc_set_destructor(state
->conn
, rpc_cli_smbd_conn_destructor
);
267 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, smb_sock
);
269 status
= map_nt_error_from_unix(errno
);
272 ret
= pipe(stdout_pipe
);
274 status
= map_nt_error_from_unix(errno
);
280 status
= map_nt_error_from_unix(errno
);
287 close(stdout_pipe
[0]);
289 if (dup(smb_sock
[1]) == -1) {
294 if (dup(stdout_pipe
[1]) == -1) {
297 close(stdout_pipe
[1]);
299 smbd_cmd
= getenv("SMB_PATH");
301 if ((smbd_cmd
== NULL
)
302 && (asprintf(&smbd_cmd
, "%s/smbd", get_dyn_SBINDIR())
307 if (asprintf(&smbd_cmd
, "%s -F -S -d %d", smbd_cmd
,
313 exit(system(smbd_cmd
));
316 state
->conn
->cli
->fd
= smb_sock
[0];
321 state
->conn
->stdout_fd
= stdout_pipe
[0];
323 close(stdout_pipe
[1]);
326 subreq
= get_anon_ipc_send(state
, ev
, state
->conn
->cli
);
327 if (tevent_req_nomem(subreq
, req
)) {
328 return tevent_req_post(req
, ev
);
331 if (event_add_fd(ev
, state
, state
->conn
->stdout_fd
, EVENT_FD_READ
,
332 rpc_cli_smbd_stdout_reader
, state
->conn
) == NULL
) {
333 status
= NT_STATUS_NO_MEMORY
;
337 tevent_req_set_callback(subreq
, rpc_cli_smbd_conn_init_done
, req
);
341 if (smb_sock
[0] != -1) {
344 if (smb_sock
[1] != -1) {
347 if (stdout_pipe
[0] != -1) {
348 close(stdout_pipe
[0]);
350 if (stdout_pipe
[1] != -1) {
351 close(stdout_pipe
[1]);
353 tevent_req_nterror(req
, status
);
354 return tevent_req_post(req
, ev
);
357 static void rpc_cli_smbd_conn_init_done(struct tevent_req
*subreq
)
359 struct tevent_req
*req
= tevent_req_callback_data(
360 subreq
, struct tevent_req
);
363 status
= get_anon_ipc_recv(subreq
);
365 if (!NT_STATUS_IS_OK(status
)) {
366 tevent_req_nterror(req
, status
);
369 tevent_req_done(req
);
372 NTSTATUS
rpc_cli_smbd_conn_init_recv(struct tevent_req
*req
,
374 struct rpc_cli_smbd_conn
**pconn
)
376 struct rpc_cli_smbd_conn_init_state
*state
= tevent_req_data(
377 req
, struct rpc_cli_smbd_conn_init_state
);
380 if (tevent_req_is_nterror(req
, &status
)) {
383 *pconn
= talloc_move(mem_ctx
, &state
->conn
);
387 NTSTATUS
rpc_cli_smbd_conn_init(TALLOC_CTX
*mem_ctx
,
388 struct rpc_cli_smbd_conn
**pconn
,
389 void (*stdout_callback
)(char *buf
,
394 TALLOC_CTX
*frame
= talloc_stackframe();
395 struct event_context
*ev
;
396 struct tevent_req
*req
;
399 ev
= event_context_init(frame
);
401 status
= NT_STATUS_NO_MEMORY
;
405 req
= rpc_cli_smbd_conn_init_send(frame
, ev
, stdout_callback
, priv
);
407 status
= NT_STATUS_NO_MEMORY
;
411 if (!tevent_req_poll(req
, ev
)) {
412 status
= map_nt_error_from_unix(errno
);
416 status
= rpc_cli_smbd_conn_init_recv(req
, mem_ctx
, pconn
);
422 struct rpc_smbd_write_state
{
423 struct rpc_cli_transport
*sub_transp
;
427 static void rpc_smbd_write_done(struct tevent_req
*subreq
);
429 static struct tevent_req
*rpc_smbd_write_send(TALLOC_CTX
*mem_ctx
,
430 struct event_context
*ev
,
431 const uint8_t *data
, size_t size
,
434 struct rpc_transport_smbd_state
*transp
= talloc_get_type_abort(
435 priv
, struct rpc_transport_smbd_state
);
436 struct tevent_req
*req
, *subreq
;
437 struct rpc_smbd_write_state
*state
;
439 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_smbd_write_state
);
443 state
->sub_transp
= transp
->sub_transp
;
445 subreq
= transp
->sub_transp
->write_send(state
, ev
, data
, size
,
446 transp
->sub_transp
->priv
);
447 if (subreq
== NULL
) {
451 if (event_add_fd(ev
, state
, transp
->conn
->stdout_fd
, EVENT_FD_READ
,
452 rpc_cli_smbd_stdout_reader
, transp
->conn
) == NULL
) {
455 tevent_req_set_callback(subreq
, rpc_smbd_write_done
, req
);
463 static void rpc_smbd_write_done(struct tevent_req
*subreq
)
465 struct tevent_req
*req
= tevent_req_callback_data(
466 subreq
, struct tevent_req
);
467 struct rpc_smbd_write_state
*state
= tevent_req_data(
468 req
, struct rpc_smbd_write_state
);
471 status
= state
->sub_transp
->write_recv(subreq
, &state
->written
);
473 if (!NT_STATUS_IS_OK(status
)) {
474 tevent_req_nterror(req
, status
);
477 tevent_req_done(req
);
480 static NTSTATUS
rpc_smbd_write_recv(struct tevent_req
*req
, ssize_t
*pwritten
)
482 struct rpc_smbd_write_state
*state
= tevent_req_data(
483 req
, struct rpc_smbd_write_state
);
486 if (tevent_req_is_nterror(req
, &status
)) {
489 *pwritten
= state
->written
;
493 struct rpc_smbd_read_state
{
494 struct rpc_cli_transport
*sub_transp
;
498 static void rpc_smbd_read_done(struct tevent_req
*subreq
);
500 static struct tevent_req
*rpc_smbd_read_send(TALLOC_CTX
*mem_ctx
,
501 struct event_context
*ev
,
502 uint8_t *data
, size_t size
,
505 struct rpc_transport_smbd_state
*transp
= talloc_get_type_abort(
506 priv
, struct rpc_transport_smbd_state
);
507 struct tevent_req
*req
, *subreq
;
508 struct rpc_smbd_read_state
*state
;
510 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_smbd_read_state
);
514 state
->sub_transp
= transp
->sub_transp
;
516 subreq
= transp
->sub_transp
->read_send(state
, ev
, data
, size
,
517 transp
->sub_transp
->priv
);
518 if (subreq
== NULL
) {
522 if (event_add_fd(ev
, state
, transp
->conn
->stdout_fd
, EVENT_FD_READ
,
523 rpc_cli_smbd_stdout_reader
, transp
->conn
) == NULL
) {
526 tevent_req_set_callback(subreq
, rpc_smbd_read_done
, req
);
533 static void rpc_smbd_read_done(struct tevent_req
*subreq
)
535 struct tevent_req
*req
= tevent_req_callback_data(
536 subreq
, struct tevent_req
);
537 struct rpc_smbd_read_state
*state
= tevent_req_data(
538 req
, struct rpc_smbd_read_state
);
541 status
= state
->sub_transp
->read_recv(subreq
, &state
->received
);
543 if (!NT_STATUS_IS_OK(status
)) {
544 tevent_req_nterror(req
, status
);
547 tevent_req_done(req
);
550 static NTSTATUS
rpc_smbd_read_recv(struct tevent_req
*req
, ssize_t
*preceived
)
552 struct rpc_smbd_read_state
*state
= tevent_req_data(
553 req
, struct rpc_smbd_read_state
);
556 if (tevent_req_is_nterror(req
, &status
)) {
559 *preceived
= state
->received
;
563 struct rpc_transport_smbd_init_state
{
564 struct rpc_cli_transport
*transport
;
565 struct rpc_transport_smbd_state
*transport_smbd
;
568 static void rpc_transport_smbd_init_done(struct tevent_req
*subreq
);
570 struct tevent_req
*rpc_transport_smbd_init_send(TALLOC_CTX
*mem_ctx
,
571 struct event_context
*ev
,
572 struct rpc_cli_smbd_conn
*conn
,
573 const struct ndr_syntax_id
*abstract_syntax
)
575 struct tevent_req
*req
, *subreq
;
576 struct rpc_transport_smbd_init_state
*state
;
578 req
= tevent_req_create(mem_ctx
, &state
,
579 struct rpc_transport_smbd_init_state
);
584 state
->transport
= talloc(state
, struct rpc_cli_transport
);
585 if (tevent_req_nomem(state
->transport
, req
)) {
586 return tevent_req_post(req
, ev
);
588 state
->transport_smbd
= talloc(state
->transport
,
589 struct rpc_transport_smbd_state
);
590 if (tevent_req_nomem(state
->transport_smbd
, req
)) {
591 return tevent_req_post(req
, ev
);
593 state
->transport_smbd
->conn
= conn
;
594 state
->transport
->priv
= state
->transport_smbd
;
596 if (event_add_fd(ev
, state
, conn
->stdout_fd
, EVENT_FD_READ
,
597 rpc_cli_smbd_stdout_reader
, conn
) == NULL
) {
598 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
599 return tevent_req_post(req
, ev
);
602 subreq
= rpc_transport_np_init_send(state
, ev
, conn
->cli
,
604 if (tevent_req_nomem(subreq
, req
)) {
605 return tevent_req_post(req
, ev
);
607 tevent_req_set_callback(subreq
, rpc_transport_smbd_init_done
, req
);
611 static void rpc_transport_smbd_init_done(struct tevent_req
*subreq
)
613 struct tevent_req
*req
= tevent_req_callback_data(
614 subreq
, struct tevent_req
);
615 struct rpc_transport_smbd_init_state
*state
= tevent_req_data(
616 req
, struct rpc_transport_smbd_init_state
);
619 status
= rpc_transport_np_init_recv(
620 subreq
, state
->transport_smbd
,
621 &state
->transport_smbd
->sub_transp
);
623 if (!NT_STATUS_IS_OK(status
)) {
624 tevent_req_nterror(req
, status
);
627 tevent_req_done(req
);
630 NTSTATUS
rpc_transport_smbd_init_recv(struct tevent_req
*req
,
632 struct rpc_cli_transport
**presult
)
634 struct rpc_transport_smbd_init_state
*state
= tevent_req_data(
635 req
, struct rpc_transport_smbd_init_state
);
638 if (tevent_req_is_nterror(req
, &status
)) {
642 state
->transport
->write_send
= rpc_smbd_write_send
;
643 state
->transport
->write_recv
= rpc_smbd_write_recv
;
644 state
->transport
->read_send
= rpc_smbd_read_send
;
645 state
->transport
->read_recv
= rpc_smbd_read_recv
;
646 state
->transport
->trans_send
= NULL
;
647 state
->transport
->trans_recv
= NULL
;
649 *presult
= talloc_move(mem_ctx
, &state
->transport
);
653 NTSTATUS
rpc_transport_smbd_init(TALLOC_CTX
*mem_ctx
,
654 struct rpc_cli_smbd_conn
*conn
,
655 const struct ndr_syntax_id
*abstract_syntax
,
656 struct rpc_cli_transport
**presult
)
658 TALLOC_CTX
*frame
= talloc_stackframe();
659 struct event_context
*ev
;
660 struct tevent_req
*req
;
663 ev
= event_context_init(frame
);
665 status
= NT_STATUS_NO_MEMORY
;
669 req
= rpc_transport_smbd_init_send(frame
, ev
, conn
, abstract_syntax
);
671 status
= NT_STATUS_NO_MEMORY
;
675 if (!tevent_req_poll(req
, ev
)) {
676 status
= map_nt_error_from_unix(errno
);
680 status
= rpc_transport_smbd_init_recv(req
, mem_ctx
, presult
);