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