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