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