s3:rpc_client: pass object and table to rpccli_bh_create()
[mat/samba.git] / source3 / rpc_server / rpc_ncacn_np.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-1998,
5  *  Largely re-written : 2005
6  *  Copyright (C) Jeremy Allison                1998 - 2005
7  *  Copyright (C) Simo Sorce                    2010
8  *  Copyright (C) Andrew Bartlett               2011
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 3 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include "includes.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "rpc_server/srv_pipe_internal.h"
27 #include "rpc_dce.h"
28 #include "../libcli/named_pipe_auth/npa_tstream.h"
29 #include "rpc_server/rpc_ncacn_np.h"
30 #include "librpc/gen_ndr/netlogon.h"
31 #include "librpc/gen_ndr/auth.h"
32 #include "../auth/auth_sam_reply.h"
33 #include "auth.h"
34 #include "rpc_server/rpc_pipes.h"
35 #include "../lib/tsocket/tsocket.h"
36 #include "../lib/util/tevent_ntstatus.h"
37 #include "rpc_contexts.h"
38 #include "rpc_server/rpc_config.h"
39 #include "librpc/ndr/ndr_table.h"
40
41 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_RPC_SRV
43
44 /****************************************************************************
45  Make an internal namedpipes structure
46 ****************************************************************************/
47
48 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
49                                               const struct ndr_syntax_id *syntax,
50                                               const struct tsocket_address *remote_address,
51                                               const struct auth_session_info *session_info,
52                                               struct messaging_context *msg_ctx)
53 {
54         struct pipes_struct *p;
55         struct pipe_rpc_fns *context_fns;
56         const char *pipe_name;
57         int ret;
58         const struct ndr_interface_table *table;
59
60         table = ndr_table_by_uuid(&syntax->uuid);
61         if (table == NULL) {
62                 DEBUG(0,("unknown interface\n"));
63                 return NULL;
64         }
65
66         pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
67
68         DEBUG(4,("Create pipe requested %s\n", pipe_name));
69
70         ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
71                                      NCALRPC, RPC_LITTLE_ENDIAN, false,
72                                      remote_address, NULL, &p);
73         if (ret) {
74                 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
75                 return NULL;
76         }
77
78         if (!init_pipe_handles(p, syntax)) {
79                 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
80                 TALLOC_FREE(p);
81                 return NULL;
82         }
83
84         p->session_info = copy_session_info(p, session_info);
85         if (p->session_info == NULL) {
86                 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
87                 close_policy_by_pipe(p);
88                 TALLOC_FREE(p);
89                 return NULL;
90         }
91
92         context_fns = talloc(p, struct pipe_rpc_fns);
93         if (context_fns == NULL) {
94                 DEBUG(0,("talloc() failed!\n"));
95                 TALLOC_FREE(p);
96                 return NULL;
97         }
98
99         context_fns->next = context_fns->prev = NULL;
100         context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
101         context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
102         context_fns->context_id = 0;
103         context_fns->syntax = *syntax;
104
105         /* add to the list of open contexts */
106         DLIST_ADD(p->contexts, context_fns);
107
108         DEBUG(4,("Created internal pipe %s\n", pipe_name));
109
110         return p;
111 }
112
113 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
114                                 TALLOC_CTX *mem_ctx,
115                                 uint32_t opnum,
116                                 const DATA_BLOB *in_data,
117                                 DATA_BLOB *out_data)
118 {
119         struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
120         uint32_t num_cmds = fns->n_cmds;
121         const struct api_struct *cmds = fns->cmds;
122         uint32_t i;
123         bool ok;
124
125         /* set opnum */
126         p->opnum = opnum;
127
128         for (i = 0; i < num_cmds; i++) {
129                 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
130                         break;
131                 }
132         }
133
134         if (i == num_cmds) {
135                 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
136         }
137
138         p->in_data.data = *in_data;
139         p->out_data.rdata = data_blob_null;
140
141         ok = cmds[i].fn(p);
142         p->in_data.data = data_blob_null;
143         if (!ok) {
144                 data_blob_free(&p->out_data.rdata);
145                 talloc_free_children(p->mem_ctx);
146                 return NT_STATUS_RPC_CALL_FAILED;
147         }
148
149         if (p->fault_state) {
150                 NTSTATUS status;
151
152                 status = NT_STATUS(p->fault_state);
153                 p->fault_state = 0;
154                 data_blob_free(&p->out_data.rdata);
155                 talloc_free_children(p->mem_ctx);
156                 return status;
157         }
158
159         *out_data = p->out_data.rdata;
160         talloc_steal(mem_ctx, out_data->data);
161         p->out_data.rdata = data_blob_null;
162
163         talloc_free_children(p->mem_ctx);
164         return NT_STATUS_OK;
165 }
166
167 struct rpcint_bh_state {
168         struct pipes_struct *p;
169 };
170
171 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
172 {
173         struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
174                                      struct rpcint_bh_state);
175
176         if (!hs->p) {
177                 return false;
178         }
179
180         return true;
181 }
182
183 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
184                                       uint32_t timeout)
185 {
186         /* TODO: implement timeouts */
187         return UINT32_MAX;
188 }
189
190 struct rpcint_bh_raw_call_state {
191         DATA_BLOB in_data;
192         DATA_BLOB out_data;
193         uint32_t out_flags;
194 };
195
196 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
197                                                   struct tevent_context *ev,
198                                                   struct dcerpc_binding_handle *h,
199                                                   const struct GUID *object,
200                                                   uint32_t opnum,
201                                                   uint32_t in_flags,
202                                                   const uint8_t *in_data,
203                                                   size_t in_length)
204 {
205         struct rpcint_bh_state *hs =
206                 dcerpc_binding_handle_data(h,
207                 struct rpcint_bh_state);
208         struct tevent_req *req;
209         struct rpcint_bh_raw_call_state *state;
210         bool ok;
211         NTSTATUS status;
212
213         req = tevent_req_create(mem_ctx, &state,
214                                 struct rpcint_bh_raw_call_state);
215         if (req == NULL) {
216                 return NULL;
217         }
218         state->in_data.data = discard_const_p(uint8_t, in_data);
219         state->in_data.length = in_length;
220
221         ok = rpcint_bh_is_connected(h);
222         if (!ok) {
223                 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
224                 return tevent_req_post(req, ev);
225         }
226
227         /* TODO: allow async */
228         status = rpcint_dispatch(hs->p, state, opnum,
229                                  &state->in_data,
230                                  &state->out_data);
231         if (!NT_STATUS_IS_OK(status)) {
232                 tevent_req_nterror(req, status);
233                 return tevent_req_post(req, ev);
234         }
235
236         tevent_req_done(req);
237         return tevent_req_post(req, ev);
238 }
239
240 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
241                                         TALLOC_CTX *mem_ctx,
242                                         uint8_t **out_data,
243                                         size_t *out_length,
244                                         uint32_t *out_flags)
245 {
246         struct rpcint_bh_raw_call_state *state =
247                 tevent_req_data(req,
248                 struct rpcint_bh_raw_call_state);
249         NTSTATUS status;
250
251         if (tevent_req_is_nterror(req, &status)) {
252                 tevent_req_received(req);
253                 return status;
254         }
255
256         *out_data = talloc_move(mem_ctx, &state->out_data.data);
257         *out_length = state->out_data.length;
258         *out_flags = 0;
259         tevent_req_received(req);
260         return NT_STATUS_OK;
261 }
262
263 struct rpcint_bh_disconnect_state {
264         uint8_t _dummy;
265 };
266
267 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
268                                                 struct tevent_context *ev,
269                                                 struct dcerpc_binding_handle *h)
270 {
271         struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
272                                      struct rpcint_bh_state);
273         struct tevent_req *req;
274         struct rpcint_bh_disconnect_state *state;
275         bool ok;
276
277         req = tevent_req_create(mem_ctx, &state,
278                                 struct rpcint_bh_disconnect_state);
279         if (req == NULL) {
280                 return NULL;
281         }
282
283         ok = rpcint_bh_is_connected(h);
284         if (!ok) {
285                 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
286                 return tevent_req_post(req, ev);
287         }
288
289         /*
290          * TODO: do a real async disconnect ...
291          *
292          * For now the caller needs to free pipes_struct
293          */
294         hs->p = NULL;
295
296         tevent_req_done(req);
297         return tevent_req_post(req, ev);
298 }
299
300 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
301 {
302         NTSTATUS status;
303
304         if (tevent_req_is_nterror(req, &status)) {
305                 tevent_req_received(req);
306                 return status;
307         }
308
309         tevent_req_received(req);
310         return NT_STATUS_OK;
311 }
312
313 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
314 {
315         return true;
316 }
317
318 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
319                                    int ndr_flags,
320                                    const void *_struct_ptr,
321                                    const struct ndr_interface_call *call)
322 {
323         void *struct_ptr = discard_const(_struct_ptr);
324
325         if (DEBUGLEVEL < 11) {
326                 return;
327         }
328
329         if (ndr_flags & NDR_IN) {
330                 ndr_print_function_debug(call->ndr_print,
331                                          call->name,
332                                          ndr_flags,
333                                          struct_ptr);
334         }
335         if (ndr_flags & NDR_OUT) {
336                 ndr_print_function_debug(call->ndr_print,
337                                          call->name,
338                                          ndr_flags,
339                                          struct_ptr);
340         }
341 }
342
343 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
344         .name                   = "rpcint",
345         .is_connected           = rpcint_bh_is_connected,
346         .set_timeout            = rpcint_bh_set_timeout,
347         .raw_call_send          = rpcint_bh_raw_call_send,
348         .raw_call_recv          = rpcint_bh_raw_call_recv,
349         .disconnect_send        = rpcint_bh_disconnect_send,
350         .disconnect_recv        = rpcint_bh_disconnect_recv,
351
352         .ref_alloc              = rpcint_bh_ref_alloc,
353         .do_ndr_print           = rpcint_bh_do_ndr_print,
354 };
355
356 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
357                         const struct ndr_syntax_id *abstract_syntax,
358                         const struct ndr_interface_table *ndr_table,
359                         const struct tsocket_address *remote_address,
360                         const struct auth_session_info *session_info,
361                         struct messaging_context *msg_ctx,
362                         struct dcerpc_binding_handle **binding_handle)
363 {
364         struct dcerpc_binding_handle *h;
365         struct rpcint_bh_state *hs;
366
367         if (ndr_table) {
368                 abstract_syntax = &ndr_table->syntax_id;
369         }
370
371         h = dcerpc_binding_handle_create(mem_ctx,
372                                          &rpcint_bh_ops,
373                                          NULL,
374                                          ndr_table,
375                                          &hs,
376                                          struct rpcint_bh_state,
377                                          __location__);
378         if (h == NULL) {
379                 return NT_STATUS_NO_MEMORY;
380         }
381         hs->p = make_internal_rpc_pipe_p(hs,
382                                          abstract_syntax,
383                                          remote_address,
384                                          session_info,
385                                          msg_ctx);
386         if (hs->p == NULL) {
387                 TALLOC_FREE(h);
388                 return NT_STATUS_NO_MEMORY;
389         }
390
391         *binding_handle = h;
392         return NT_STATUS_OK;
393 }
394 /**
395  * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
396  *
397  * @param[in]  mem_ctx  The memory context to use.
398  *
399  * @param[in]  ndr_table Normally the ndr_table_<name>.
400  *
401  * @param[in]  remote_address The info about the connected client.
402  *
403  * @param[in]  serversupplied_info The server supplied authentication function.
404  *
405  * @param[in]  msg_ctx   The messaging context that can be used by the server
406  *
407  * @param[out] binding_handle  A pointer to store the connected
408  *                             dcerpc_binding_handle
409  *
410  * @return              NT_STATUS_OK on success, a corresponding NT status if an
411  *                      error occured.
412  *
413  * @code
414  *   struct dcerpc_binding_handle *winreg_binding;
415  *   NTSTATUS status;
416  *
417  *   status = rpcint_binding_handle(tmp_ctx,
418  *                                  &ndr_table_winreg,
419  *                                  p->remote_address,
420  *                                  p->session_info,
421  *                                  p->msg_ctx
422  *                                  &winreg_binding);
423  * @endcode
424  */
425 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
426                                const struct ndr_interface_table *ndr_table,
427                                const struct tsocket_address *remote_address,
428                                const struct auth_session_info *session_info,
429                                struct messaging_context *msg_ctx,
430                                struct dcerpc_binding_handle **binding_handle)
431 {
432         return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
433                                         session_info, msg_ctx, binding_handle);
434 }
435
436 /**
437  * @internal
438  *
439  * @brief Create a new RPC client context which uses a local transport.
440  *
441  * This creates a local transport. It is a shortcut to directly call the server
442  * functions and avoid marshalling.
443  * NOTE: this function should be used only by rpc_pipe_open_interface()
444  *
445  * @param[in]  mem_ctx  The memory context to use.
446  *
447  * @param[in]  abstract_syntax Normally the syntax_id of the autogenerated
448  *                             ndr_table_<name>.
449  *
450  * @param[in]  serversupplied_info The server supplied authentication function.
451  *
452  * @param[in]  remote_address The client address information.
453  *
454  * @param[in]  msg_ctx  The messaging context to use.
455  *
456  * @param[out] presult  A pointer to store the connected rpc client pipe.
457  *
458  * @return              NT_STATUS_OK on success, a corresponding NT status if an
459  *                      error occured.
460  */
461 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
462                                 const struct ndr_syntax_id *abstract_syntax,
463                                 const struct auth_session_info *session_info,
464                                 const struct tsocket_address *remote_address,
465                                 struct messaging_context *msg_ctx,
466                                 struct rpc_pipe_client **presult)
467 {
468         struct rpc_pipe_client *result;
469         NTSTATUS status;
470
471         result = talloc_zero(mem_ctx, struct rpc_pipe_client);
472         if (result == NULL) {
473                 return NT_STATUS_NO_MEMORY;
474         }
475
476         result->abstract_syntax = *abstract_syntax;
477         result->transfer_syntax = ndr_transfer_syntax_ndr;
478
479         if (remote_address == NULL) {
480                 struct tsocket_address *local;
481                 int rc;
482
483                 rc = tsocket_address_inet_from_strings(mem_ctx,
484                                                        "ip",
485                                                        "127.0.0.1",
486                                                        0,
487                                                        &local);
488                 if (rc < 0) {
489                         TALLOC_FREE(result);
490                         return NT_STATUS_NO_MEMORY;
491                 }
492
493                 remote_address = local;
494         }
495
496         result->max_xmit_frag = -1;
497         result->max_recv_frag = -1;
498
499         status = rpcint_binding_handle_ex(result,
500                                           abstract_syntax,
501                                           NULL,
502                                           remote_address,
503                                           session_info,
504                                           msg_ctx,
505                                           &result->binding_handle);
506         if (!NT_STATUS_IS_OK(status)) {
507                 TALLOC_FREE(result);
508                 return status;
509         }
510
511         *presult = result;
512         return NT_STATUS_OK;
513 }
514
515 /****************************************************************************
516  * External pipes functions
517  ***************************************************************************/
518
519
520 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
521                                 const char *pipe_name,
522                                 const struct tsocket_address *local_address,
523                                 const struct tsocket_address *remote_address,
524                                 const struct auth_session_info *session_info)
525 {
526         struct np_proxy_state *result;
527         char *socket_np_dir;
528         const char *socket_dir;
529         struct tevent_context *ev;
530         struct tevent_req *subreq;
531         struct auth_session_info_transport *session_info_t;
532         bool ok;
533         int ret;
534         int sys_errno;
535
536         result = talloc(mem_ctx, struct np_proxy_state);
537         if (result == NULL) {
538                 DEBUG(0, ("talloc failed\n"));
539                 return NULL;
540         }
541
542         result->read_queue = tevent_queue_create(result, "np_read");
543         if (result->read_queue == NULL) {
544                 DEBUG(0, ("tevent_queue_create failed\n"));
545                 goto fail;
546         }
547
548         result->write_queue = tevent_queue_create(result, "np_write");
549         if (result->write_queue == NULL) {
550                 DEBUG(0, ("tevent_queue_create failed\n"));
551                 goto fail;
552         }
553
554         ev = s3_tevent_context_init(talloc_tos());
555         if (ev == NULL) {
556                 DEBUG(0, ("s3_tevent_context_init failed\n"));
557                 goto fail;
558         }
559
560         socket_dir = lp_parm_const_string(
561                 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
562                 lp_ncalrpc_dir());
563         if (socket_dir == NULL) {
564                 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
565                 goto fail;
566         }
567         socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
568         if (socket_np_dir == NULL) {
569                 DEBUG(0, ("talloc_asprintf failed\n"));
570                 goto fail;
571         }
572
573         session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
574         if (session_info_t == NULL) {
575                 DEBUG(0, ("talloc failed\n"));
576                 goto fail;
577         }
578
579         session_info_t->session_info = copy_session_info(session_info_t,
580                                                          session_info);
581         if (session_info_t->session_info == NULL) {
582                 DEBUG(0, ("copy_session_info failed\n"));
583                 goto fail;
584         }
585
586         become_root();
587         subreq = tstream_npa_connect_send(talloc_tos(), ev,
588                                           socket_np_dir,
589                                           pipe_name,
590                                           remote_address, /* client_addr */
591                                           NULL, /* client_name */
592                                           local_address, /* server_addr */
593                                           NULL, /* server_name */
594                                           session_info_t);
595         if (subreq == NULL) {
596                 unbecome_root();
597                 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
598                           "user %s\\%s failed\n",
599                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
600                           session_info_t->session_info->info->account_name));
601                 goto fail;
602         }
603         ok = tevent_req_poll(subreq, ev);
604         unbecome_root();
605         if (!ok) {
606                 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
607                           "failed for tstream_npa_connect: %s\n",
608                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
609                           session_info_t->session_info->info->account_name,
610                           strerror(errno)));
611                 goto fail;
612
613         }
614         ret = tstream_npa_connect_recv(subreq, &sys_errno,
615                                        result,
616                                        &result->npipe,
617                                        &result->file_type,
618                                        &result->device_state,
619                                        &result->allocation_size);
620         TALLOC_FREE(subreq);
621         if (ret != 0) {
622                 int l = 1;
623                 if (errno == ENOENT) {
624                         l = 2;
625                 }
626                 DEBUG(l, ("tstream_npa_connect_recv  to %s for pipe %s and "
627                           "user %s\\%s failed: %s\n",
628                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
629                           session_info_t->session_info->info->account_name,
630                           strerror(sys_errno)));
631                 goto fail;
632         }
633
634         return result;
635
636  fail:
637         TALLOC_FREE(result);
638         return NULL;
639 }
640
641 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
642                                 const char *pipe_name,
643                                 const struct ndr_interface_table *table,
644                                 const struct auth_session_info *session_info,
645                                 struct rpc_pipe_client **_result)
646 {
647         struct tsocket_address *local, *remote;
648         struct rpc_pipe_client *result = NULL;
649         struct np_proxy_state *proxy_state = NULL;
650         struct pipe_auth_data *auth;
651         NTSTATUS status;
652         int ret;
653
654         /* this is an internal connection, fake up ip addresses */
655         ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
656                                                 NULL, 0, &local);
657         if (ret) {
658                 return NT_STATUS_NO_MEMORY;
659         }
660         ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
661                                                 NULL, 0, &remote);
662         if (ret) {
663                 return NT_STATUS_NO_MEMORY;
664         }
665
666         proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
667                                                 local, remote, session_info);
668         if (!proxy_state) {
669                 return NT_STATUS_UNSUCCESSFUL;
670         }
671
672         result = talloc_zero(mem_ctx, struct rpc_pipe_client);
673         if (result == NULL) {
674                 status = NT_STATUS_NO_MEMORY;
675                 goto done;
676         }
677
678         result->abstract_syntax = table->syntax_id;
679         result->transfer_syntax = ndr_transfer_syntax_ndr;
680
681         result->desthost = get_myname(result);
682         result->srv_name_slash = talloc_asprintf_strupper_m(
683                 result, "\\\\%s", result->desthost);
684         if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
685                 status = NT_STATUS_NO_MEMORY;
686                 goto done;
687         }
688
689         result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
690         result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
691
692         status = rpc_transport_tstream_init(result,
693                                             &proxy_state->npipe,
694                                             &result->transport);
695         if (!NT_STATUS_IS_OK(status)) {
696                 goto done;
697         }
698
699         result->binding_handle = rpccli_bh_create(result, NULL, table);
700         if (result->binding_handle == NULL) {
701                 status = NT_STATUS_NO_MEMORY;
702                 DEBUG(0, ("Failed to create binding handle.\n"));
703                 goto done;
704         }
705
706         result->auth = talloc_zero(result, struct pipe_auth_data);
707         if (!result->auth) {
708                 status = NT_STATUS_NO_MEMORY;
709                 goto done;
710         }
711         result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
712         result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
713
714         status = rpccli_anon_bind_data(result, &auth);
715         if (!NT_STATUS_IS_OK(status)) {
716                 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
717                 goto done;
718         }
719
720         status = rpc_pipe_bind(result, auth);
721         if (!NT_STATUS_IS_OK(status)) {
722                 DEBUG(0, ("Failed to bind external pipe.\n"));
723                 goto done;
724         }
725
726 done:
727         if (!NT_STATUS_IS_OK(status)) {
728                 TALLOC_FREE(result);
729         }
730         TALLOC_FREE(proxy_state);
731         *_result = result;
732         return status;
733 }
734
735 /**
736  * @brief Create a new RPC client context which uses a local dispatch function
737  *        or a remote transport, depending on rpc_server configuration for the
738  *        specific service.
739  *
740  * @param[in]  mem_ctx  The memory context to use.
741  *
742  * @param[in]  abstract_syntax Normally the syntax_id of the autogenerated
743  *                             ndr_table_<name>.
744  *
745  * @param[in]  serversupplied_info The server supplied authentication function.
746  *
747  * @param[in]  remote_address The client address information.
748  *
749  * @param[in]  msg_ctx  The messaging context to use.
750  *
751  * @param[out] presult  A pointer to store the connected rpc client pipe.
752  *
753  * @return              NT_STATUS_OK on success, a corresponding NT status if an
754  *                      error occured.
755  *
756  * @code
757  *   struct rpc_pipe_client *winreg_pipe;
758  *   NTSTATUS status;
759  *
760  *   status = rpc_pipe_open_interface(tmp_ctx,
761  *                                    &ndr_table_winreg.syntax_id,
762  *                                    p->session_info,
763  *                                    remote_address,
764  *                                    &winreg_pipe);
765  * @endcode
766  */
767
768 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
769                                  const struct ndr_interface_table *table,
770                                  const struct auth_session_info *session_info,
771                                  const struct tsocket_address *remote_address,
772                                  struct messaging_context *msg_ctx,
773                                  struct rpc_pipe_client **cli_pipe)
774 {
775         struct rpc_pipe_client *cli = NULL;
776         enum rpc_service_mode_e pipe_mode;
777         const char *pipe_name;
778         NTSTATUS status;
779         TALLOC_CTX *tmp_ctx;
780
781         if (cli_pipe != NULL) {
782                 if (rpccli_is_connected(*cli_pipe)) {
783                         return NT_STATUS_OK;
784                 } else {
785                         TALLOC_FREE(*cli_pipe);
786                 }
787         }
788
789         tmp_ctx = talloc_stackframe();
790         if (tmp_ctx == NULL) {
791                 return NT_STATUS_NO_MEMORY;
792         }
793
794         pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
795         if (pipe_name == NULL) {
796                 status = NT_STATUS_INVALID_PARAMETER;
797                 goto done;
798         }
799
800         while (pipe_name[0] == '\\') {
801                 pipe_name++;
802         }
803
804         DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
805
806         pipe_mode = rpc_service_mode(pipe_name);
807
808         switch (pipe_mode) {
809         case RPC_SERVICE_MODE_EMBEDDED:
810                 status = rpc_pipe_open_internal(tmp_ctx,
811                                                 &table->syntax_id, session_info,
812                                                 remote_address, msg_ctx,
813                                                 &cli);
814                 if (!NT_STATUS_IS_OK(status)) {
815                         goto done;
816                 }
817                 break;
818         case RPC_SERVICE_MODE_EXTERNAL:
819                 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
820                  * for now we need to use the special proxy setup to connect
821                  * to spoolssd. */
822
823                 status = rpc_pipe_open_external(tmp_ctx,
824                                                 pipe_name, table,
825                                                 session_info,
826                                                 &cli);
827                 if (!NT_STATUS_IS_OK(status)) {
828                         goto done;
829                 }
830                 break;
831         case RPC_SERVICE_MODE_DISABLED:
832                 status = NT_STATUS_NOT_IMPLEMENTED;
833                 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
834                           pipe_name, nt_errstr(status)));
835                 goto done;
836         }
837
838         status = NT_STATUS_OK;
839 done:
840         if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
841                 *cli_pipe = talloc_move(mem_ctx, &cli);
842         }
843         TALLOC_FREE(tmp_ctx);
844         return status;
845 }