Pass the current debuglevel down to the forked smbd
[Samba/gbeck.git] / source3 / rpc_client / rpc_transport_smbd.c
blob0a3787a776da5ca1f16bd2f00802a107c20bfef5
1 /*
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/>.
20 #include "includes.h"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_RPC_CLI
25 /**
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
36 * stdout_df.
39 struct rpc_cli_smbd_conn {
40 /**
41 * The smb connection to handle the named pipe traffic over
43 struct cli_state *cli;
45 /**
46 * Attached to stdout in the forked smbd, this is where smbd will
47 * print its DEBUG.
49 int stdout_fd;
51 /**
52 * Custom callback provided by the owner of the
53 * rpc_cli_smbd_conn. Here we send the smbd DEBUG output. Can be NULL.
55 struct {
56 void (*fn)(char *buf, size_t len, void *priv);
57 void *priv;
58 } stdout_callback ;
61 /**
62 * Event handler to be called whenever the forked smbd prints debugging
63 * output.
66 static void rpc_cli_smbd_stdout_reader(struct event_context *ev,
67 struct fd_event *fde,
68 uint16_t flags, void *priv)
70 struct rpc_cli_smbd_conn *conn = talloc_get_type_abort(
71 priv, struct rpc_cli_smbd_conn);
72 char buf[1024];
73 ssize_t nread;
75 if ((flags & EVENT_FD_READ) == 0) {
76 return;
79 nread = read(conn->stdout_fd, buf, sizeof(buf)-1);
80 if (nread < 0) {
81 DEBUG(0, ("Could not read from smbd stdout: %s\n",
82 strerror(errno)));
83 TALLOC_FREE(fde);
84 return;
86 if (nread == 0) {
87 DEBUG(0, ("EOF from smbd stdout\n"));
88 TALLOC_FREE(fde);
89 return;
92 if (conn->stdout_callback.fn != NULL) {
93 conn->stdout_callback.fn(buf, nread,
94 conn->stdout_callback.priv);
98 /**
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);
112 conn->cli = NULL;
114 if (conn->stdout_fd != -1) {
115 close(conn->stdout_fd);
116 conn->stdout_fd = -1;
118 return 0;
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)) {
143 return NULL;
146 state->ev = ev;
147 state->cli = cli;
149 subreq = cli_negprot_send(state, ev, cli);
150 if (subreq == NULL) {
151 goto fail;
153 subreq->async.fn = get_anon_ipc_negprot_done;
154 subreq->async.priv = result;
155 return result;
156 fail:
157 TALLOC_FREE(result);
158 return NULL;
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);
167 NTSTATUS status;
169 status = cli_negprot_recv(subreq);
170 TALLOC_FREE(subreq);
171 if (!NT_STATUS_IS_OK(status)) {
172 async_req_nterror(req, status);
173 return;
176 subreq = cli_session_setup_guest_send(state, state->ev, state->cli);
177 if (async_req_nomem(subreq, req)) {
178 return;
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);
190 NTSTATUS status;
192 status = cli_session_setup_guest_recv(subreq);
193 TALLOC_FREE(subreq);
194 if (!NT_STATUS_IS_OK(status)) {
195 async_req_nterror(req, status);
196 return;
199 subreq = cli_tcon_andx_send(state, state->ev, state->cli,
200 "IPC$", "IPC", NULL, 0);
201 if (async_req_nomem(subreq, req)) {
202 return;
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);
212 NTSTATUS status;
214 status = cli_tcon_andx_recv(subreq);
215 TALLOC_FREE(subreq);
216 if (!NT_STATUS_IS_OK(status)) {
217 async_req_nterror(req, status);
218 return;
220 async_req_done(req);
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,
238 size_t len,
239 void *priv),
240 void *priv)
242 struct async_req *result, *subreq;
243 struct rpc_cli_smbd_conn_init_state *state;
244 int smb_sock[2];
245 int stdout_pipe[2];
246 NTSTATUS status;
247 pid_t pid;
248 int ret;
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)) {
254 return NULL;
256 state->ev = ev;
258 state->conn = talloc(state, struct rpc_cli_smbd_conn);
259 if (state->conn == NULL) {
260 goto nomem;
263 state->conn->cli = cli_initialise();
264 if (state->conn->cli == NULL) {
265 goto nomem;
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);
273 if (ret == -1) {
274 status = map_nt_error_from_unix(errno);
275 goto post_status;
277 ret = pipe(stdout_pipe);
278 if (ret == -1) {
279 status = map_nt_error_from_unix(errno);
280 goto post_status;
283 pid = sys_fork();
284 if (pid == -1) {
285 status = map_nt_error_from_unix(errno);
286 goto post_status;
288 if (pid == 0) {
289 char *smbd_cmd;
291 close(smb_sock[0]);
292 close(stdout_pipe[0]);
293 close(0);
294 if (dup(smb_sock[1]) == -1) {
295 exit(1);
297 close(smb_sock[1]);
298 close(1);
299 if (dup(stdout_pipe[1]) == -1) {
300 exit(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())
308 == -1)) {
309 printf("no memory");
310 exit(1);
312 if (asprintf(&smbd_cmd, "%s -F -S -d %d", smbd_cmd,
313 DEBUGLEVEL) == -1) {
314 printf("no memory");
315 exit(1);
318 exit(system(smbd_cmd));
321 state->conn->cli->fd = smb_sock[0];
322 smb_sock[0] = -1;
323 close(smb_sock[1]);
324 smb_sock[1] = -1;
326 state->conn->stdout_fd = stdout_pipe[0];
327 stdout_pipe[0] = -1;
328 close(stdout_pipe[1]);
329 stdout_pipe[1] = -1;
331 subreq = get_anon_ipc_send(state, ev, state->conn->cli);
332 if (subreq == NULL) {
333 goto nomem;
336 if (event_add_fd(ev, subreq, state->conn->stdout_fd, EVENT_FD_READ,
337 rpc_cli_smbd_stdout_reader, state->conn) == NULL) {
338 goto nomem;
341 subreq->async.fn = rpc_cli_smbd_conn_init_done;
342 subreq->async.priv = result;
343 return result;
345 nomem:
346 status = NT_STATUS_NO_MEMORY;
347 post_status:
348 if (smb_sock[0] != -1) {
349 close(smb_sock[0]);
351 if (smb_sock[1] != -1) {
352 close(smb_sock[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)) {
361 return result;
363 TALLOC_FREE(result);
364 return NULL;
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);
371 NTSTATUS status;
373 status = get_anon_ipc_recv(subreq);
374 TALLOC_FREE(subreq);
375 if (!NT_STATUS_IS_OK(status)) {
376 async_req_nterror(req, status);
377 return;
379 async_req_done(req);
382 NTSTATUS rpc_cli_smbd_conn_init_recv(struct async_req *req,
383 TALLOC_CTX *mem_ctx,
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);
388 NTSTATUS status;
390 if (async_req_is_nterror(req, &status)) {
391 return status;
393 *pconn = talloc_move(mem_ctx, &state->conn);
394 return NT_STATUS_OK;
397 NTSTATUS rpc_cli_smbd_conn_init(TALLOC_CTX *mem_ctx,
398 struct rpc_cli_smbd_conn **pconn,
399 void (*stdout_callback)(char *buf,
400 size_t len,
401 void *priv),
402 void *priv)
404 TALLOC_CTX *frame = talloc_stackframe();
405 struct event_context *ev;
406 struct async_req *req;
407 NTSTATUS status;
409 ev = event_context_init(frame);
410 if (ev == NULL) {
411 status = NT_STATUS_NO_MEMORY;
412 goto fail;
415 req = rpc_cli_smbd_conn_init_send(frame, ev, stdout_callback, priv);
416 if (req == NULL) {
417 status = NT_STATUS_NO_MEMORY;
418 goto fail;
421 while (req->state < ASYNC_REQ_DONE) {
422 event_loop_once(ev);
425 status = rpc_cli_smbd_conn_init_recv(req, mem_ctx, pconn);
426 fail:
427 TALLOC_FREE(frame);
428 return status;
431 struct rpc_smbd_write_state {
432 struct rpc_cli_transport *sub_transp;
433 ssize_t written;
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,
441 void *priv)
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)) {
450 return NULL;
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) {
457 goto fail;
460 if (event_add_fd(ev, subreq, transp->conn->stdout_fd, EVENT_FD_READ,
461 rpc_cli_smbd_stdout_reader, transp->conn) == NULL) {
462 goto fail;
465 subreq->async.fn = rpc_smbd_write_done;
466 subreq->async.priv = result;
467 return result;
469 fail:
470 TALLOC_FREE(result);
471 return NULL;
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);
480 NTSTATUS status;
482 status = state->sub_transp->write_recv(subreq, &state->written);
483 TALLOC_FREE(subreq);
484 if (!NT_STATUS_IS_OK(status)) {
485 async_req_nterror(req, status);
486 return;
488 async_req_done(req);
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);
495 NTSTATUS status;
497 if (async_req_is_nterror(req, &status)) {
498 return status;
500 *pwritten = state->written;
501 return NT_STATUS_OK;
504 struct rpc_smbd_read_state {
505 struct rpc_cli_transport *sub_transp;
506 ssize_t received;
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,
514 void *priv)
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)) {
523 return NULL;
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) {
530 goto fail;
533 if (event_add_fd(ev, subreq, transp->conn->stdout_fd, EVENT_FD_READ,
534 rpc_cli_smbd_stdout_reader, transp->conn) == NULL) {
535 goto fail;
538 subreq->async.fn = rpc_smbd_read_done;
539 subreq->async.priv = result;
540 return result;
542 fail:
543 TALLOC_FREE(result);
544 return NULL;
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);
553 NTSTATUS status;
555 status = state->sub_transp->read_recv(subreq, &state->received);
556 TALLOC_FREE(subreq);
557 if (!NT_STATUS_IS_OK(status)) {
558 async_req_nterror(req, status);
559 return;
561 async_req_done(req);
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);
568 NTSTATUS status;
570 if (async_req_is_nterror(req, &status)) {
571 return status;
573 *preceived = state->received;
574 return NT_STATUS_OK;
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)) {
594 return NULL;
597 state->transport = talloc(state, struct rpc_cli_transport);
598 if (state->transport == NULL) {
599 goto fail;
601 state->transport_smbd = talloc(state->transport,
602 struct rpc_transport_smbd_state);
603 if (state->transport_smbd == NULL) {
604 goto fail;
606 state->transport_smbd->conn = conn;
607 state->transport->priv = state->transport_smbd;
609 subreq = rpc_transport_np_init_send(state, ev, conn->cli,
610 abstract_syntax);
611 if (subreq == NULL) {
612 goto fail;
614 subreq->async.fn = rpc_transport_smbd_init_done;
615 subreq->async.priv = result;
616 return result;
618 fail:
619 TALLOC_FREE(result);
620 return NULL;
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);
629 NTSTATUS status;
631 status = rpc_transport_np_init_recv(
632 subreq, state->transport_smbd,
633 &state->transport_smbd->sub_transp);
634 TALLOC_FREE(subreq);
635 if (!NT_STATUS_IS_OK(status)) {
636 async_req_nterror(req, status);
637 return;
639 async_req_done(req);
642 NTSTATUS rpc_transport_smbd_init_recv(struct async_req *req,
643 TALLOC_CTX *mem_ctx,
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);
648 NTSTATUS status;
650 if (async_req_is_nterror(req, &status)) {
651 return 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);
662 return NT_STATUS_OK;
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;
673 NTSTATUS status;
675 ev = event_context_init(frame);
676 if (ev == NULL) {
677 status = NT_STATUS_NO_MEMORY;
678 goto fail;
681 req = rpc_transport_smbd_init_send(frame, ev, conn, abstract_syntax);
682 if (req == NULL) {
683 status = NT_STATUS_NO_MEMORY;
684 goto fail;
687 while (req->state < ASYNC_REQ_DONE) {
688 event_loop_once(ev);
691 status = rpc_transport_smbd_init_recv(req, mem_ctx, presult);
692 fail:
693 TALLOC_FREE(frame);
694 return status;