clean up lib64 linking paths the same way as lib
[Samba/gebeck_regimport.git] / source3 / rpc_client / rpc_transport_smbd.c
blobbf4aa65daec57ed4dc23c1b1056f36b0cac5c24b
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", smbd_cmd) == -1) {
313 printf("no memory");
314 exit(1);
317 exit(system(smbd_cmd));
320 state->conn->cli->fd = smb_sock[0];
321 smb_sock[0] = -1;
322 close(smb_sock[1]);
323 smb_sock[1] = -1;
325 state->conn->stdout_fd = stdout_pipe[0];
326 stdout_pipe[0] = -1;
327 close(stdout_pipe[1]);
328 stdout_pipe[1] = -1;
330 subreq = get_anon_ipc_send(state, ev, state->conn->cli);
331 if (subreq == NULL) {
332 goto nomem;
335 if (event_add_fd(ev, subreq, state->conn->stdout_fd, EVENT_FD_READ,
336 rpc_cli_smbd_stdout_reader, state->conn) == NULL) {
337 goto nomem;
340 subreq->async.fn = rpc_cli_smbd_conn_init_done;
341 subreq->async.priv = result;
342 return result;
344 nomem:
345 status = NT_STATUS_NO_MEMORY;
346 post_status:
347 if (smb_sock[0] != -1) {
348 close(smb_sock[0]);
350 if (smb_sock[1] != -1) {
351 close(smb_sock[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)) {
360 return result;
362 TALLOC_FREE(result);
363 return NULL;
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);
370 NTSTATUS status;
372 status = get_anon_ipc_recv(subreq);
373 TALLOC_FREE(subreq);
374 if (!NT_STATUS_IS_OK(status)) {
375 async_req_nterror(req, status);
376 return;
378 async_req_done(req);
381 NTSTATUS rpc_cli_smbd_conn_init_recv(struct async_req *req,
382 TALLOC_CTX *mem_ctx,
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);
387 NTSTATUS status;
389 if (async_req_is_nterror(req, &status)) {
390 return status;
392 *pconn = talloc_move(mem_ctx, &state->conn);
393 return NT_STATUS_OK;
396 NTSTATUS rpc_cli_smbd_conn_init(TALLOC_CTX *mem_ctx,
397 struct rpc_cli_smbd_conn **pconn,
398 void (*stdout_callback)(char *buf,
399 size_t len,
400 void *priv),
401 void *priv)
403 TALLOC_CTX *frame = talloc_stackframe();
404 struct event_context *ev;
405 struct async_req *req;
406 NTSTATUS status;
408 ev = event_context_init(frame);
409 if (ev == NULL) {
410 status = NT_STATUS_NO_MEMORY;
411 goto fail;
414 req = rpc_cli_smbd_conn_init_send(frame, ev, stdout_callback, priv);
415 if (req == NULL) {
416 status = NT_STATUS_NO_MEMORY;
417 goto fail;
420 while (req->state < ASYNC_REQ_DONE) {
421 event_loop_once(ev);
424 status = rpc_cli_smbd_conn_init_recv(req, mem_ctx, pconn);
425 fail:
426 TALLOC_FREE(frame);
427 return status;
430 struct rpc_smbd_write_state {
431 struct rpc_cli_transport *sub_transp;
432 ssize_t written;
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,
440 void *priv)
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)) {
449 return NULL;
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) {
456 goto fail;
459 if (event_add_fd(ev, subreq, transp->conn->stdout_fd, EVENT_FD_READ,
460 rpc_cli_smbd_stdout_reader, transp->conn) == NULL) {
461 goto fail;
464 subreq->async.fn = rpc_smbd_write_done;
465 subreq->async.priv = result;
466 return result;
468 fail:
469 TALLOC_FREE(result);
470 return NULL;
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);
479 NTSTATUS status;
481 status = state->sub_transp->write_recv(subreq, &state->written);
482 TALLOC_FREE(subreq);
483 if (!NT_STATUS_IS_OK(status)) {
484 async_req_nterror(req, status);
485 return;
487 async_req_done(req);
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);
494 NTSTATUS status;
496 if (async_req_is_nterror(req, &status)) {
497 return status;
499 *pwritten = state->written;
500 return NT_STATUS_OK;
503 struct rpc_smbd_read_state {
504 struct rpc_cli_transport *sub_transp;
505 ssize_t received;
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,
513 void *priv)
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)) {
522 return NULL;
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) {
529 goto fail;
532 if (event_add_fd(ev, subreq, transp->conn->stdout_fd, EVENT_FD_READ,
533 rpc_cli_smbd_stdout_reader, transp->conn) == NULL) {
534 goto fail;
537 subreq->async.fn = rpc_smbd_read_done;
538 subreq->async.priv = result;
539 return result;
541 fail:
542 TALLOC_FREE(result);
543 return NULL;
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);
552 NTSTATUS status;
554 status = state->sub_transp->read_recv(subreq, &state->received);
555 TALLOC_FREE(subreq);
556 if (!NT_STATUS_IS_OK(status)) {
557 async_req_nterror(req, status);
558 return;
560 async_req_done(req);
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);
567 NTSTATUS status;
569 if (async_req_is_nterror(req, &status)) {
570 return status;
572 *preceived = state->received;
573 return NT_STATUS_OK;
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)) {
593 return NULL;
596 state->transport = talloc(state, struct rpc_cli_transport);
597 if (state->transport == NULL) {
598 goto fail;
600 state->transport_smbd = talloc(state->transport,
601 struct rpc_transport_smbd_state);
602 if (state->transport_smbd == NULL) {
603 goto fail;
605 state->transport_smbd->conn = conn;
606 state->transport->priv = state->transport_smbd;
608 subreq = rpc_transport_np_init_send(state, ev, conn->cli,
609 abstract_syntax);
610 if (subreq == NULL) {
611 goto fail;
613 subreq->async.fn = rpc_transport_smbd_init_done;
614 subreq->async.priv = result;
615 return result;
617 fail:
618 TALLOC_FREE(result);
619 return NULL;
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);
628 NTSTATUS status;
630 status = rpc_transport_np_init_recv(
631 subreq, state->transport_smbd,
632 &state->transport_smbd->sub_transp);
633 TALLOC_FREE(subreq);
634 if (!NT_STATUS_IS_OK(status)) {
635 async_req_nterror(req, status);
636 return;
638 async_req_done(req);
641 NTSTATUS rpc_transport_smbd_init_recv(struct async_req *req,
642 TALLOC_CTX *mem_ctx,
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);
647 NTSTATUS status;
649 if (async_req_is_nterror(req, &status)) {
650 return 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);
661 return NT_STATUS_OK;
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;
672 NTSTATUS status;
674 ev = event_context_init(frame);
675 if (ev == NULL) {
676 status = NT_STATUS_NO_MEMORY;
677 goto fail;
680 req = rpc_transport_smbd_init_send(frame, ev, conn, abstract_syntax);
681 if (req == NULL) {
682 status = NT_STATUS_NO_MEMORY;
683 goto fail;
686 while (req->state < ASYNC_REQ_DONE) {
687 event_loop_once(ev);
690 status = rpc_transport_smbd_init_recv(req, mem_ctx, presult);
691 fail:
692 TALLOC_FREE(frame);
693 return status;