Split up async_req into a generic and a NTSTATUS specific part
[jra/samba/.git] / source3 / rpc_client / rpc_transport_smbd.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC client transport over named pipes to a child smbd
4  *  Copyright (C) Volker Lendecke 2009
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "includes.h"
21
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_RPC_CLI
24
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.
28  *
29  * RPC pipe handles can be attached to this smbd connection with
30  * rpc_pipe_open_local().
31  *
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.
37  */
38
39 struct rpc_cli_smbd_conn {
40         /**
41          * The smb connection to handle the named pipe traffic over
42          */
43         struct cli_state *cli;
44
45         /**
46          * Attached to stdout in the forked smbd, this is where smbd will
47          * print its DEBUG.
48          */
49         int stdout_fd;
50
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.
54          */
55         struct {
56                 void (*fn)(char *buf, size_t len, void *priv);
57                 void *priv;
58         } stdout_callback ;
59 };
60
61 /**
62  * Event handler to be called whenever the forked smbd prints debugging
63  * output.
64  */
65
66 static void rpc_cli_smbd_stdout_reader(struct event_context *ev,
67                                        struct fd_event *fde,
68                                        uint16_t flags, void *priv)
69 {
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;
74
75         if ((flags & EVENT_FD_READ) == 0) {
76                 return;
77         }
78
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;
85         }
86         if (nread == 0) {
87                 DEBUG(0, ("EOF from smbd stdout\n"));
88                 TALLOC_FREE(fde);
89                 return;
90         }
91
92         if (conn->stdout_callback.fn != NULL) {
93                 conn->stdout_callback.fn(buf, nread,
94                                          conn->stdout_callback.priv);
95         }
96 }
97
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.
101  */
102
103 struct rpc_transport_smbd_state {
104         struct rpc_cli_smbd_conn *conn;
105         struct rpc_cli_transport *sub_transp;
106 };
107
108 static int rpc_cli_smbd_conn_destructor(struct rpc_cli_smbd_conn *conn)
109 {
110         if (conn->cli != NULL) {
111                 cli_shutdown(conn->cli);
112                 conn->cli = NULL;
113         }
114         if (conn->stdout_fd != -1) {
115                 close(conn->stdout_fd);
116                 conn->stdout_fd = -1;
117         }
118         return 0;
119 }
120
121 /*
122  * Do the negprot/sesssetup/tcon to an anonymous ipc$ connection
123  */
124
125 struct get_anon_ipc_state {
126         struct event_context *ev;
127         struct cli_state *cli;
128 };
129
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);
133
134 static struct async_req *get_anon_ipc_send(TALLOC_CTX *mem_ctx,
135                                            struct event_context *ev,
136                                            struct cli_state *cli)
137 {
138         struct async_req *result, *subreq;
139         struct get_anon_ipc_state *state;
140
141         if (!async_req_setup(mem_ctx, &result, &state,
142                              struct get_anon_ipc_state)) {
143                 return NULL;
144         }
145
146         state->ev = ev;
147         state->cli = cli;
148
149         subreq = cli_negprot_send(state, ev, cli);
150         if (subreq == NULL) {
151                 goto fail;
152         }
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;
159 }
160
161 static void get_anon_ipc_negprot_done(struct async_req *subreq)
162 {
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;
168
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;
174         }
175
176         subreq = cli_session_setup_guest_send(state, state->ev, state->cli);
177         if (async_req_ntnomem(subreq, req)) {
178                 return;
179         }
180         subreq->async.fn = get_anon_ipc_sesssetup_done;
181         subreq->async.priv = req;
182 }
183
184 static void get_anon_ipc_sesssetup_done(struct async_req *subreq)
185 {
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;
191
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;
197         }
198
199         subreq = cli_tcon_andx_send(state, state->ev, state->cli,
200                                     "IPC$", "IPC", NULL, 0);
201         if (async_req_ntnomem(subreq, req)) {
202                 return;
203         }
204         subreq->async.fn = get_anon_ipc_tcon_done;
205         subreq->async.priv = req;
206 }
207
208 static void get_anon_ipc_tcon_done(struct async_req *subreq)
209 {
210         struct async_req *req = talloc_get_type_abort(
211                 subreq->async.priv, struct async_req);
212         NTSTATUS status;
213
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;
219         }
220         async_req_done(req);
221 }
222
223 static NTSTATUS get_anon_ipc_recv(struct async_req *req)
224 {
225         return async_req_simple_recv_ntstatus(req);
226 }
227
228 struct rpc_cli_smbd_conn_init_state {
229         struct event_context *ev;
230         struct rpc_cli_smbd_conn *conn;
231 };
232
233 static void rpc_cli_smbd_conn_init_done(struct async_req *subreq);
234
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)
241 {
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;
249
250         smb_sock[0] = smb_sock[1] = stdout_pipe[0] = stdout_pipe[1] = -1;
251
252         if (!async_req_setup(mem_ctx, &result, &state,
253                              struct rpc_cli_smbd_conn_init_state)) {
254                 return NULL;
255         }
256         state->ev = ev;
257
258         state->conn = talloc(state, struct rpc_cli_smbd_conn);
259         if (state->conn == NULL) {
260                 goto nomem;
261         }
262
263         state->conn->cli = cli_initialise();
264         if (state->conn->cli == NULL) {
265                 goto nomem;
266         }
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);
271
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;
276         }
277         ret = pipe(stdout_pipe);
278         if (ret == -1) {
279                 status = map_nt_error_from_unix(errno);
280                 goto post_status;
281         }
282
283         pid = sys_fork();
284         if (pid == -1) {
285                 status = map_nt_error_from_unix(errno);
286                 goto post_status;
287         }
288         if (pid == 0) {
289                 char *smbd_cmd;
290
291                 close(smb_sock[0]);
292                 close(stdout_pipe[0]);
293                 close(0);
294                 if (dup(smb_sock[1]) == -1) {
295                         exit(1);
296                 }
297                 close(smb_sock[1]);
298                 close(1);
299                 if (dup(stdout_pipe[1]) == -1) {
300                         exit(1);
301                 }
302                 close(stdout_pipe[1]);
303
304                 smbd_cmd = getenv("SMB_PATH");
305
306                 if ((smbd_cmd == NULL)
307                     && (asprintf(&smbd_cmd, "%s/smbd", get_dyn_SBINDIR())
308                         == -1)) {
309                         printf("no memory");
310                         exit(1);
311                 }
312                 if (asprintf(&smbd_cmd, "%s -F -S", smbd_cmd) == -1) {
313                         printf("no memory");
314                         exit(1);
315                 }
316
317                 exit(system(smbd_cmd));
318         }
319
320         state->conn->cli->fd = smb_sock[0];
321         smb_sock[0] = -1;
322         close(smb_sock[1]);
323         smb_sock[1] = -1;
324
325         state->conn->stdout_fd = stdout_pipe[0];
326         stdout_pipe[0] = -1;
327         close(stdout_pipe[1]);
328         stdout_pipe[1] = -1;
329
330         subreq = get_anon_ipc_send(state, ev, state->conn->cli);
331         if (subreq == NULL) {
332                 goto nomem;
333         }
334
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;
338         }
339
340         subreq->async.fn = rpc_cli_smbd_conn_init_done;
341         subreq->async.priv = result;
342         return result;
343
344  nomem:
345         status = NT_STATUS_NO_MEMORY;
346  post_status:
347         if (smb_sock[0] != -1) {
348                 close(smb_sock[0]);
349         }
350         if (smb_sock[1] != -1) {
351                 close(smb_sock[1]);
352         }
353         if (stdout_pipe[0] != -1) {
354                 close(stdout_pipe[0]);
355         }
356         if (stdout_pipe[1] != -1) {
357                 close(stdout_pipe[1]);
358         }
359         if (async_post_ntstatus(result, ev, status)) {
360                 return result;
361         }
362         TALLOC_FREE(result);
363         return NULL;
364 }
365
366 static void rpc_cli_smbd_conn_init_done(struct async_req *subreq)
367 {
368         struct async_req *req = talloc_get_type_abort(
369                 subreq->async.priv, struct async_req);
370         NTSTATUS status;
371
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;
377         }
378         async_req_done(req);
379 }
380
381 NTSTATUS rpc_cli_smbd_conn_init_recv(struct async_req *req,
382                                      TALLOC_CTX *mem_ctx,
383                                      struct rpc_cli_smbd_conn **pconn)
384 {
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;
388
389         if (async_req_is_nterror(req, &status)) {
390                 return status;
391         }
392         *pconn = talloc_move(mem_ctx, &state->conn);
393         return NT_STATUS_OK;
394 }
395
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)
402 {
403         TALLOC_CTX *frame = talloc_stackframe();
404         struct event_context *ev;
405         struct async_req *req;
406         NTSTATUS status;
407
408         ev = event_context_init(frame);
409         if (ev == NULL) {
410                 status = NT_STATUS_NO_MEMORY;
411                 goto fail;
412         }
413
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;
418         }
419
420         while (req->state < ASYNC_REQ_DONE) {
421                 event_loop_once(ev);
422         }
423
424         status = rpc_cli_smbd_conn_init_recv(req, mem_ctx, pconn);
425  fail:
426         TALLOC_FREE(frame);
427         return status;
428 }
429
430 struct rpc_smbd_write_state {
431         struct rpc_cli_transport *sub_transp;
432         ssize_t written;
433 };
434
435 static void rpc_smbd_write_done(struct async_req *subreq);
436
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)
441 {
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;
446
447         if (!async_req_setup(mem_ctx, &result, &state,
448                              struct rpc_smbd_write_state)) {
449                 return NULL;
450         }
451         state->sub_transp = transp->sub_transp;
452
453         subreq = transp->sub_transp->write_send(state, ev, data, size,
454                                                 transp->sub_transp->priv);
455         if (subreq == NULL) {
456                 goto fail;
457         }
458
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;
462         }
463
464         subreq->async.fn = rpc_smbd_write_done;
465         subreq->async.priv = result;
466         return result;
467
468  fail:
469         TALLOC_FREE(result);
470         return NULL;
471 }
472
473 static void rpc_smbd_write_done(struct async_req *subreq)
474 {
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;
480
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;
486         }
487         async_req_done(req);
488 }
489
490 static NTSTATUS rpc_smbd_write_recv(struct async_req *req, ssize_t *pwritten)
491 {
492         struct rpc_smbd_write_state *state = talloc_get_type_abort(
493                 req->private_data, struct rpc_smbd_write_state);
494         NTSTATUS status;
495
496         if (async_req_is_nterror(req, &status)) {
497                 return status;
498         }
499         *pwritten = state->written;
500         return NT_STATUS_OK;
501 }
502
503 struct rpc_smbd_read_state {
504         struct rpc_cli_transport *sub_transp;
505         ssize_t received;
506 };
507
508 static void rpc_smbd_read_done(struct async_req *subreq);
509
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)
514 {
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;
519
520         if (!async_req_setup(mem_ctx, &result, &state,
521                              struct rpc_smbd_read_state)) {
522                 return NULL;
523         }
524         state->sub_transp = transp->sub_transp;
525
526         subreq = transp->sub_transp->read_send(state, ev, data, size,
527                                                 transp->sub_transp->priv);
528         if (subreq == NULL) {
529                 goto fail;
530         }
531
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;
535         }
536
537         subreq->async.fn = rpc_smbd_read_done;
538         subreq->async.priv = result;
539         return result;
540
541  fail:
542         TALLOC_FREE(result);
543         return NULL;
544 }
545
546 static void rpc_smbd_read_done(struct async_req *subreq)
547 {
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;
553
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;
559         }
560         async_req_done(req);
561 }
562
563 static NTSTATUS rpc_smbd_read_recv(struct async_req *req, ssize_t *preceived)
564 {
565         struct rpc_smbd_read_state *state = talloc_get_type_abort(
566                 req->private_data, struct rpc_smbd_read_state);
567         NTSTATUS status;
568
569         if (async_req_is_nterror(req, &status)) {
570                 return status;
571         }
572         *preceived = state->received;
573         return NT_STATUS_OK;
574 }
575
576 struct rpc_transport_smbd_init_state {
577         struct rpc_cli_transport *transport;
578         struct rpc_transport_smbd_state *transport_smbd;
579 };
580
581 static void rpc_transport_smbd_init_done(struct async_req *subreq);
582
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)
587 {
588         struct async_req *result, *subreq;
589         struct rpc_transport_smbd_init_state *state;
590
591         if (!async_req_setup(mem_ctx, &result, &state,
592                              struct rpc_transport_smbd_init_state)) {
593                 return NULL;
594         }
595
596         state->transport = talloc(state, struct rpc_cli_transport);
597         if (state->transport == NULL) {
598                 goto fail;
599         }
600         state->transport_smbd = talloc(state->transport,
601                                        struct rpc_transport_smbd_state);
602         if (state->transport_smbd == NULL) {
603                 goto fail;
604         }
605         state->transport_smbd->conn = conn;
606         state->transport->priv = state->transport_smbd;
607
608         subreq = rpc_transport_np_init_send(state, ev, conn->cli,
609                                             abstract_syntax);
610         if (subreq == NULL) {
611                 goto fail;
612         }
613         subreq->async.fn = rpc_transport_smbd_init_done;
614         subreq->async.priv = result;
615         return result;
616
617  fail:
618         TALLOC_FREE(result);
619         return NULL;
620 }
621
622 static void rpc_transport_smbd_init_done(struct async_req *subreq)
623 {
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;
629
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;
637         }
638         async_req_done(req);
639 }
640
641 NTSTATUS rpc_transport_smbd_init_recv(struct async_req *req,
642                                       TALLOC_CTX *mem_ctx,
643                                       struct rpc_cli_transport **presult)
644 {
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;
648
649         if (async_req_is_nterror(req, &status)) {
650                 return status;
651         }
652
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;
659
660         *presult = talloc_move(mem_ctx, &state->transport);
661         return NT_STATUS_OK;
662 }
663
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)
668 {
669         TALLOC_CTX *frame = talloc_stackframe();
670         struct event_context *ev;
671         struct async_req *req;
672         NTSTATUS status;
673
674         ev = event_context_init(frame);
675         if (ev == NULL) {
676                 status = NT_STATUS_NO_MEMORY;
677                 goto fail;
678         }
679
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;
684         }
685
686         while (req->state < ASYNC_REQ_DONE) {
687                 event_loop_once(ev);
688         }
689
690         status = rpc_transport_smbd_init_recv(req, mem_ctx, presult);
691  fail:
692         TALLOC_FREE(frame);
693         return status;
694 }