s3-dcerpc: use dcerpc_push_ncacn_packet() in create_rpc_bind_auth3()
[ira/wip.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         buf[nread] = '\0';
92
93         if (conn->stdout_callback.fn != NULL) {
94                 conn->stdout_callback.fn(buf, nread,
95                                          conn->stdout_callback.priv);
96         }
97 }
98
99 /**
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.
102  */
103
104 struct rpc_transport_smbd_state {
105         struct rpc_cli_smbd_conn *conn;
106         struct rpc_cli_transport *sub_transp;
107 };
108
109 static int rpc_cli_smbd_conn_destructor(struct rpc_cli_smbd_conn *conn)
110 {
111         if (conn->cli != NULL) {
112                 cli_shutdown(conn->cli);
113                 conn->cli = NULL;
114         }
115         if (conn->stdout_fd != -1) {
116                 close(conn->stdout_fd);
117                 conn->stdout_fd = -1;
118         }
119         return 0;
120 }
121
122 /*
123  * Do the negprot/sesssetup/tcon to an anonymous ipc$ connection
124  */
125
126 struct get_anon_ipc_state {
127         struct event_context *ev;
128         struct cli_state *cli;
129 };
130
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);
134
135 static struct tevent_req *get_anon_ipc_send(TALLOC_CTX *mem_ctx,
136                                             struct event_context *ev,
137                                             struct cli_state *cli)
138 {
139         struct tevent_req *req, *subreq;
140         struct get_anon_ipc_state *state;
141
142         req = tevent_req_create(mem_ctx, &state, struct get_anon_ipc_state);
143         if (req == NULL) {
144                 return NULL;
145         }
146         state->ev = ev;
147         state->cli = cli;
148
149         subreq = cli_negprot_send(state, ev, cli);
150         if (tevent_req_nomem(subreq, req)) {
151                 return tevent_req_post(req, ev);
152         }
153         tevent_req_set_callback(subreq, get_anon_ipc_negprot_done, req);
154         return req;
155 }
156
157 static void get_anon_ipc_negprot_done(struct tevent_req *subreq)
158 {
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);
163         NTSTATUS status;
164
165         status = cli_negprot_recv(subreq);
166         TALLOC_FREE(subreq);
167         if (!NT_STATUS_IS_OK(status)) {
168                 tevent_req_nterror(req, status);
169                 return;
170         }
171
172         subreq = cli_session_setup_guest_send(state, state->ev, state->cli);
173         if (tevent_req_nomem(subreq, req)) {
174                 return;
175         }
176         tevent_req_set_callback(subreq, get_anon_ipc_sesssetup_done, req);
177 }
178
179 static void get_anon_ipc_sesssetup_done(struct tevent_req *subreq)
180 {
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);
185         NTSTATUS status;
186
187         status = cli_session_setup_guest_recv(subreq);
188         TALLOC_FREE(subreq);
189         if (!NT_STATUS_IS_OK(status)) {
190                 tevent_req_nterror(req, status);
191                 return;
192         }
193
194         subreq = cli_tcon_andx_send(state, state->ev, state->cli,
195                                     "IPC$", "IPC", NULL, 0);
196         if (tevent_req_nomem(subreq, req)) {
197                 return;
198         }
199         tevent_req_set_callback(subreq, get_anon_ipc_tcon_done, req);
200 }
201
202 static void get_anon_ipc_tcon_done(struct tevent_req *subreq)
203 {
204         struct tevent_req *req = tevent_req_callback_data(
205                 subreq, struct tevent_req);
206         NTSTATUS status;
207
208         status = cli_tcon_andx_recv(subreq);
209         TALLOC_FREE(subreq);
210         if (!NT_STATUS_IS_OK(status)) {
211                 tevent_req_nterror(req, status);
212                 return;
213         }
214         tevent_req_done(req);
215 }
216
217 static NTSTATUS get_anon_ipc_recv(struct tevent_req *req)
218 {
219         return tevent_req_simple_recv_ntstatus(req);
220 }
221
222 struct rpc_cli_smbd_conn_init_state {
223         struct event_context *ev;
224         struct rpc_cli_smbd_conn *conn;
225 };
226
227 static void rpc_cli_smbd_conn_init_done(struct tevent_req *subreq);
228
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,
232                                                                        size_t len,
233                                                                        void *priv),
234                                                void *priv)
235 {
236         struct tevent_req *req, *subreq;
237         struct rpc_cli_smbd_conn_init_state *state;
238         int smb_sock[2];
239         int stdout_pipe[2];
240         NTSTATUS status;
241         pid_t pid;
242         int ret;
243
244         smb_sock[0] = smb_sock[1] = stdout_pipe[0] = stdout_pipe[1] = -1;
245
246         req = tevent_req_create(mem_ctx, &state,
247                                 struct rpc_cli_smbd_conn_init_state);
248         if (req == NULL) {
249                 return NULL;
250         }
251         state->ev = ev;
252
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);
256         }
257
258         state->conn->cli = cli_initialise();
259         if (tevent_req_nomem(state->conn->cli, req)) {
260                 return tevent_req_post(req, ev);
261         }
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);
266
267         ret = socketpair(AF_UNIX, SOCK_STREAM, 0, smb_sock);
268         if (ret == -1) {
269                 status = map_nt_error_from_unix(errno);
270                 goto post_status;
271         }
272         ret = pipe(stdout_pipe);
273         if (ret == -1) {
274                 status = map_nt_error_from_unix(errno);
275                 goto post_status;
276         }
277
278         pid = sys_fork();
279         if (pid == -1) {
280                 status = map_nt_error_from_unix(errno);
281                 goto post_status;
282         }
283         if (pid == 0) {
284                 char *smbd_cmd;
285
286                 close(smb_sock[0]);
287                 close(stdout_pipe[0]);
288                 close(0);
289                 if (dup(smb_sock[1]) == -1) {
290                         exit(1);
291                 }
292                 close(smb_sock[1]);
293                 close(1);
294                 if (dup(stdout_pipe[1]) == -1) {
295                         exit(1);
296                 }
297                 close(stdout_pipe[1]);
298
299                 smbd_cmd = getenv("SMB_PATH");
300
301                 if ((smbd_cmd == NULL)
302                     && (asprintf(&smbd_cmd, "%s/smbd", get_dyn_SBINDIR())
303                         == -1)) {
304                         printf("no memory");
305                         exit(1);
306                 }
307                 if (asprintf(&smbd_cmd, "%s -F -S -d %d", smbd_cmd,
308                              DEBUGLEVEL) == -1) {
309                         printf("no memory");
310                         exit(1);
311                 }
312
313                 exit(system(smbd_cmd));
314         }
315
316         state->conn->cli->fd = smb_sock[0];
317         smb_sock[0] = -1;
318         close(smb_sock[1]);
319         smb_sock[1] = -1;
320
321         state->conn->stdout_fd = stdout_pipe[0];
322         stdout_pipe[0] = -1;
323         close(stdout_pipe[1]);
324         stdout_pipe[1] = -1;
325
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);
329         }
330
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;
334                 goto post_status;
335         }
336
337         tevent_req_set_callback(subreq, rpc_cli_smbd_conn_init_done, req);
338         return req;
339
340  post_status:
341         if (smb_sock[0] != -1) {
342                 close(smb_sock[0]);
343         }
344         if (smb_sock[1] != -1) {
345                 close(smb_sock[1]);
346         }
347         if (stdout_pipe[0] != -1) {
348                 close(stdout_pipe[0]);
349         }
350         if (stdout_pipe[1] != -1) {
351                 close(stdout_pipe[1]);
352         }
353         tevent_req_nterror(req, status);
354         return tevent_req_post(req, ev);
355 }
356
357 static void rpc_cli_smbd_conn_init_done(struct tevent_req *subreq)
358 {
359         struct tevent_req *req = tevent_req_callback_data(
360                 subreq, struct tevent_req);
361         NTSTATUS status;
362
363         status = get_anon_ipc_recv(subreq);
364         TALLOC_FREE(subreq);
365         if (!NT_STATUS_IS_OK(status)) {
366                 tevent_req_nterror(req, status);
367                 return;
368         }
369         tevent_req_done(req);
370 }
371
372 NTSTATUS rpc_cli_smbd_conn_init_recv(struct tevent_req *req,
373                                      TALLOC_CTX *mem_ctx,
374                                      struct rpc_cli_smbd_conn **pconn)
375 {
376         struct rpc_cli_smbd_conn_init_state *state = tevent_req_data(
377                 req, struct rpc_cli_smbd_conn_init_state);
378         NTSTATUS status;
379
380         if (tevent_req_is_nterror(req, &status)) {
381                 return status;
382         }
383         *pconn = talloc_move(mem_ctx, &state->conn);
384         return NT_STATUS_OK;
385 }
386
387 NTSTATUS rpc_cli_smbd_conn_init(TALLOC_CTX *mem_ctx,
388                                 struct rpc_cli_smbd_conn **pconn,
389                                 void (*stdout_callback)(char *buf,
390                                                         size_t len,
391                                                         void *priv),
392                                 void *priv)
393 {
394         TALLOC_CTX *frame = talloc_stackframe();
395         struct event_context *ev;
396         struct tevent_req *req;
397         NTSTATUS status;
398
399         ev = event_context_init(frame);
400         if (ev == NULL) {
401                 status = NT_STATUS_NO_MEMORY;
402                 goto fail;
403         }
404
405         req = rpc_cli_smbd_conn_init_send(frame, ev, stdout_callback, priv);
406         if (req == NULL) {
407                 status = NT_STATUS_NO_MEMORY;
408                 goto fail;
409         }
410
411         if (!tevent_req_poll(req, ev)) {
412                 status = map_nt_error_from_unix(errno);
413                 goto fail;
414         }
415
416         status = rpc_cli_smbd_conn_init_recv(req, mem_ctx, pconn);
417  fail:
418         TALLOC_FREE(frame);
419         return status;
420 }
421
422 static void rpc_smbd_disconnect(struct rpc_transport_smbd_state *transp)
423 {
424         if (transp == NULL) {
425                 return;
426         }
427
428         if (transp->conn == NULL) {
429                 return;
430         }
431
432         if (transp->conn->cli == NULL) {
433                 return;
434         }
435
436         if (transp->conn->cli->fd != -1) {
437                 close(transp->conn->cli->fd);
438                 transp->conn->cli->fd = -1;
439         }
440
441         transp->conn = NULL;
442 }
443
444 static bool rpc_smbd_is_connected(void *priv)
445 {
446         struct rpc_transport_smbd_state *transp = talloc_get_type_abort(
447                 priv, struct rpc_transport_smbd_state);
448         bool ok;
449
450         if (transp->conn == NULL) {
451                 return false;
452         }
453
454         if (transp->sub_transp == NULL) {
455                 return false;
456         }
457
458         ok = transp->sub_transp->is_connected(transp->sub_transp->priv);
459         if (!ok) {
460                 rpc_smbd_disconnect(transp);
461                 return false;
462         }
463
464         return true;
465 }
466
467 static unsigned int rpc_smbd_set_timeout(void *priv, unsigned int timeout)
468 {
469         struct rpc_transport_smbd_state *transp = talloc_get_type_abort(
470                 priv, struct rpc_transport_smbd_state);
471         bool ok;
472
473         ok = rpc_smbd_is_connected(transp);
474         if (!ok) {
475                 return 0;
476         }
477
478         if (transp->sub_transp->set_timeout == NULL) {
479                 return 0;
480         }
481
482         return transp->sub_transp->set_timeout(transp->sub_transp->priv, timeout);
483 }
484
485 struct rpc_smbd_write_state {
486         struct rpc_transport_smbd_state *transp;
487         ssize_t written;
488 };
489
490 static void rpc_smbd_write_done(struct tevent_req *subreq);
491
492 static struct tevent_req *rpc_smbd_write_send(TALLOC_CTX *mem_ctx,
493                                               struct event_context *ev,
494                                               const uint8_t *data, size_t size,
495                                               void *priv)
496 {
497         struct rpc_transport_smbd_state *transp = talloc_get_type_abort(
498                 priv, struct rpc_transport_smbd_state);
499         struct tevent_req *req, *subreq;
500         struct rpc_smbd_write_state *state;
501         bool ok;
502
503         req = tevent_req_create(mem_ctx, &state, struct rpc_smbd_write_state);
504         if (req == NULL) {
505                 return NULL;
506         }
507
508         ok = rpc_smbd_is_connected(transp);
509         if (!ok) {
510                 tevent_req_nterror(req, NT_STATUS_CONNECTION_INVALID);
511                 return tevent_req_post(req, ev);
512         }
513
514         state->transp = transp;
515
516         subreq = transp->sub_transp->write_send(state, ev, data, size,
517                                                 transp->sub_transp->priv);
518         if (subreq == NULL) {
519                 goto fail;
520         }
521
522         if (event_add_fd(ev, state, transp->conn->stdout_fd, EVENT_FD_READ,
523                          rpc_cli_smbd_stdout_reader, transp->conn) == NULL) {
524                 goto fail;
525         }
526         tevent_req_set_callback(subreq, rpc_smbd_write_done, req);
527         return req;
528
529  fail:
530         TALLOC_FREE(req);
531         return NULL;
532 }
533
534 static void rpc_smbd_write_done(struct tevent_req *subreq)
535 {
536         struct tevent_req *req = tevent_req_callback_data(
537                 subreq, struct tevent_req);
538         struct rpc_smbd_write_state *state = tevent_req_data(
539                 req, struct rpc_smbd_write_state);
540         NTSTATUS status;
541
542         status = state->transp->sub_transp->write_recv(subreq, &state->written);
543         TALLOC_FREE(subreq);
544         if (!NT_STATUS_IS_OK(status)) {
545                 rpc_smbd_disconnect(state->transp);
546                 tevent_req_nterror(req, status);
547                 return;
548         }
549         tevent_req_done(req);
550 }
551
552 static NTSTATUS rpc_smbd_write_recv(struct tevent_req *req, ssize_t *pwritten)
553 {
554         struct rpc_smbd_write_state *state = tevent_req_data(
555                 req, struct rpc_smbd_write_state);
556         NTSTATUS status;
557
558         if (tevent_req_is_nterror(req, &status)) {
559                 return status;
560         }
561         *pwritten = state->written;
562         return NT_STATUS_OK;
563 }
564
565 struct rpc_smbd_read_state {
566         struct rpc_transport_smbd_state *transp;
567         ssize_t received;
568 };
569
570 static void rpc_smbd_read_done(struct tevent_req *subreq);
571
572 static struct tevent_req *rpc_smbd_read_send(TALLOC_CTX *mem_ctx,
573                                              struct event_context *ev,
574                                              uint8_t *data, size_t size,
575                                              void *priv)
576 {
577         struct rpc_transport_smbd_state *transp = talloc_get_type_abort(
578                 priv, struct rpc_transport_smbd_state);
579         struct tevent_req *req, *subreq;
580         struct rpc_smbd_read_state *state;
581         bool ok;
582
583         req = tevent_req_create(mem_ctx, &state, struct rpc_smbd_read_state);
584         if (req == NULL) {
585                 return NULL;
586         }
587
588         ok = rpc_smbd_is_connected(transp);
589         if (!ok) {
590                 tevent_req_nterror(req, NT_STATUS_CONNECTION_INVALID);
591                 return tevent_req_post(req, ev);
592         }
593
594         state->transp = transp;
595
596         subreq = transp->sub_transp->read_send(state, ev, data, size,
597                                                 transp->sub_transp->priv);
598         if (subreq == NULL) {
599                 goto fail;
600         }
601
602         if (event_add_fd(ev, state, transp->conn->stdout_fd, EVENT_FD_READ,
603                          rpc_cli_smbd_stdout_reader, transp->conn) == NULL) {
604                 goto fail;
605         }
606         tevent_req_set_callback(subreq, rpc_smbd_read_done, req);
607         return req;
608  fail:
609         TALLOC_FREE(req);
610         return NULL;
611 }
612
613 static void rpc_smbd_read_done(struct tevent_req *subreq)
614 {
615         struct tevent_req *req = tevent_req_callback_data(
616                 subreq, struct tevent_req);
617         struct rpc_smbd_read_state *state = tevent_req_data(
618                 req, struct rpc_smbd_read_state);
619         NTSTATUS status;
620
621         status = state->transp->sub_transp->read_recv(subreq, &state->received);
622         TALLOC_FREE(subreq);
623         if (!NT_STATUS_IS_OK(status)) {
624                 rpc_smbd_disconnect(state->transp);
625                 tevent_req_nterror(req, status);
626                 return;
627         }
628         tevent_req_done(req);
629 }
630
631 static NTSTATUS rpc_smbd_read_recv(struct tevent_req *req, ssize_t *preceived)
632 {
633         struct rpc_smbd_read_state *state = tevent_req_data(
634                 req, struct rpc_smbd_read_state);
635         NTSTATUS status;
636
637         if (tevent_req_is_nterror(req, &status)) {
638                 return status;
639         }
640         *preceived = state->received;
641         return NT_STATUS_OK;
642 }
643
644 struct rpc_transport_smbd_init_state {
645         struct rpc_cli_transport *transport;
646         struct rpc_transport_smbd_state *transport_smbd;
647 };
648
649 static void rpc_transport_smbd_init_done(struct tevent_req *subreq);
650
651 struct tevent_req *rpc_transport_smbd_init_send(TALLOC_CTX *mem_ctx,
652                                                 struct event_context *ev,
653                                                 struct rpc_cli_smbd_conn *conn,
654                                                 const struct ndr_syntax_id *abstract_syntax)
655 {
656         struct tevent_req *req, *subreq;
657         struct rpc_transport_smbd_init_state *state;
658
659         req = tevent_req_create(mem_ctx, &state,
660                                 struct rpc_transport_smbd_init_state);
661         if (req == NULL) {
662                 return NULL;
663         }
664
665         state->transport = talloc(state, struct rpc_cli_transport);
666         if (tevent_req_nomem(state->transport, req)) {
667                 return tevent_req_post(req, ev);
668         }
669         state->transport_smbd = talloc(state->transport,
670                                        struct rpc_transport_smbd_state);
671         if (tevent_req_nomem(state->transport_smbd, req)) {
672                 return tevent_req_post(req, ev);
673         }
674         state->transport_smbd->conn = conn;
675         state->transport->priv = state->transport_smbd;
676
677         if (event_add_fd(ev, state, conn->stdout_fd, EVENT_FD_READ,
678                          rpc_cli_smbd_stdout_reader, conn) == NULL) {
679                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
680                 return tevent_req_post(req, ev);
681         }
682
683         subreq = rpc_transport_np_init_send(state, ev, conn->cli,
684                                             abstract_syntax);
685         if (tevent_req_nomem(subreq, req)) {
686                 return tevent_req_post(req, ev);
687         }
688         tevent_req_set_callback(subreq, rpc_transport_smbd_init_done, req);
689         return req;
690 }
691
692 static void rpc_transport_smbd_init_done(struct tevent_req *subreq)
693 {
694         struct tevent_req *req = tevent_req_callback_data(
695                 subreq, struct tevent_req);
696         struct rpc_transport_smbd_init_state *state = tevent_req_data(
697                 req, struct rpc_transport_smbd_init_state);
698         NTSTATUS status;
699
700         status = rpc_transport_np_init_recv(
701                 subreq, state->transport_smbd,
702                 &state->transport_smbd->sub_transp);
703         TALLOC_FREE(subreq);
704         if (!NT_STATUS_IS_OK(status)) {
705                 tevent_req_nterror(req, status);
706                 return;
707         }
708         tevent_req_done(req);
709 }
710
711 NTSTATUS rpc_transport_smbd_init_recv(struct tevent_req *req,
712                                       TALLOC_CTX *mem_ctx,
713                                       struct rpc_cli_transport **presult)
714 {
715         struct rpc_transport_smbd_init_state *state = tevent_req_data(
716                 req, struct rpc_transport_smbd_init_state);
717         NTSTATUS status;
718
719         if (tevent_req_is_nterror(req, &status)) {
720                 return status;
721         }
722
723         state->transport->write_send = rpc_smbd_write_send;
724         state->transport->write_recv = rpc_smbd_write_recv;
725         state->transport->read_send = rpc_smbd_read_send;
726         state->transport->read_recv = rpc_smbd_read_recv;
727         state->transport->trans_send = NULL;
728         state->transport->trans_recv = NULL;
729         state->transport->is_connected = rpc_smbd_is_connected;
730         state->transport->set_timeout = rpc_smbd_set_timeout;
731
732         *presult = talloc_move(mem_ctx, &state->transport);
733         return NT_STATUS_OK;
734 }
735
736 NTSTATUS rpc_transport_smbd_init(TALLOC_CTX *mem_ctx,
737                                  struct rpc_cli_smbd_conn *conn,
738                                  const struct ndr_syntax_id *abstract_syntax,
739                                  struct rpc_cli_transport **presult)
740 {
741         TALLOC_CTX *frame = talloc_stackframe();
742         struct event_context *ev;
743         struct tevent_req *req;
744         NTSTATUS status;
745
746         ev = event_context_init(frame);
747         if (ev == NULL) {
748                 status = NT_STATUS_NO_MEMORY;
749                 goto fail;
750         }
751
752         req = rpc_transport_smbd_init_send(frame, ev, conn, abstract_syntax);
753         if (req == NULL) {
754                 status = NT_STATUS_NO_MEMORY;
755                 goto fail;
756         }
757
758         if (!tevent_req_poll(req, ev)) {
759                 status = map_nt_error_from_unix(errno);
760                 goto fail;
761         }
762
763         status = rpc_transport_smbd_init_recv(req, mem_ctx, presult);
764  fail:
765         TALLOC_FREE(frame);
766         return status;
767 }