s3:rpc_client: let rpc_transport_tstream_init() create read and write queue
[amitay/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  *
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         ev = s3_tevent_context_init(talloc_tos());
610         if (ev == NULL) {
611                 DEBUG(0, ("s3_tevent_context_init failed\n"));
612                 goto fail;
613         }
614
615         socket_dir = lp_parm_const_string(
616                 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
617                 lp_ncalrpc_dir());
618         if (socket_dir == NULL) {
619                 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
620                 goto fail;
621         }
622         socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
623         if (socket_np_dir == NULL) {
624                 DEBUG(0, ("talloc_asprintf failed\n"));
625                 goto fail;
626         }
627
628         info3 = talloc_zero(talloc_tos(), struct netr_SamInfo3);
629         if (info3 == NULL) {
630                 DEBUG(0, ("talloc failed\n"));
631                 goto fail;
632         }
633
634         status = serverinfo_to_SamInfo3(server_info, NULL, 0, info3);
635         if (!NT_STATUS_IS_OK(status)) {
636                 TALLOC_FREE(info3);
637                 DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
638                           nt_errstr(status)));
639                 goto fail;
640         }
641
642         become_root();
643         subreq = tstream_npa_connect_send(talloc_tos(), ev,
644                                           socket_np_dir,
645                                           pipe_name,
646                                           remote_address, /* client_addr */
647                                           NULL, /* client_name */
648                                           local_address, /* server_addr */
649                                           NULL, /* server_name */
650                                           info3,
651                                           server_info->user_session_key,
652                                           data_blob_null /* delegated_creds */);
653         if (subreq == NULL) {
654                 unbecome_root();
655                 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
656                           "user %s\\%s failed\n",
657                           socket_np_dir, pipe_name, info3->base.domain.string,
658                           info3->base.account_name.string));
659                 goto fail;
660         }
661         ok = tevent_req_poll(subreq, ev);
662         unbecome_root();
663         if (!ok) {
664                 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
665                           "failed for tstream_npa_connect: %s\n",
666                           socket_np_dir, pipe_name, info3->base.domain.string,
667                           info3->base.account_name.string,
668                           strerror(errno)));
669                 goto fail;
670
671         }
672         ret = tstream_npa_connect_recv(subreq, &sys_errno,
673                                        result,
674                                        &result->npipe,
675                                        &result->file_type,
676                                        &result->device_state,
677                                        &result->allocation_size);
678         TALLOC_FREE(subreq);
679         if (ret != 0) {
680                 DEBUG(0, ("tstream_npa_connect_recv  to %s for pipe %s and "
681                           "user %s\\%s failed: %s\n",
682                           socket_np_dir, pipe_name, info3->base.domain.string,
683                           info3->base.account_name.string,
684                           strerror(sys_errno)));
685                 goto fail;
686         }
687
688         return result;
689
690  fail:
691         TALLOC_FREE(result);
692         return NULL;
693 }
694
695 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
696                                 const char *pipe_name,
697                                 const struct ndr_syntax_id *abstract_syntax,
698                                 const struct auth_serversupplied_info *server_info,
699                                 struct rpc_pipe_client **_result)
700 {
701         struct tsocket_address *local, *remote;
702         struct rpc_pipe_client *result = NULL;
703         struct np_proxy_state *proxy_state = NULL;
704         struct pipe_auth_data *auth;
705         NTSTATUS status;
706         int ret;
707
708         /* this is an internal connection, fake up ip addresses */
709         ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
710                                                 NULL, 0, &local);
711         if (ret) {
712                 return NT_STATUS_NO_MEMORY;
713         }
714         ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
715                                                 NULL, 0, &remote);
716         if (ret) {
717                 return NT_STATUS_NO_MEMORY;
718         }
719
720         proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
721                                                 local, remote, server_info);
722         if (!proxy_state) {
723                 return NT_STATUS_UNSUCCESSFUL;
724         }
725
726         result = talloc_zero(mem_ctx, struct rpc_pipe_client);
727         if (result == NULL) {
728                 status = NT_STATUS_NO_MEMORY;
729                 goto done;
730         }
731
732         result->abstract_syntax = *abstract_syntax;
733         result->transfer_syntax = ndr_transfer_syntax;
734
735         result->desthost = get_myname(result);
736         result->srv_name_slash = talloc_asprintf_strupper_m(
737                 result, "\\\\%s", result->desthost);
738         if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
739                 status = NT_STATUS_NO_MEMORY;
740                 goto done;
741         }
742
743         result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
744         result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
745
746         status = rpc_transport_tstream_init(result,
747                                             &proxy_state->npipe,
748                                             &result->transport);
749         if (!NT_STATUS_IS_OK(status)) {
750                 goto done;
751         }
752
753         result->auth = talloc_zero(result, struct pipe_auth_data);
754         if (!result->auth) {
755                 status = NT_STATUS_NO_MEMORY;
756                 goto done;
757         }
758         result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
759         result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
760
761         status = rpccli_anon_bind_data(result, &auth);
762         if (!NT_STATUS_IS_OK(status)) {
763                 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
764                 goto done;
765         }
766
767         status = rpc_pipe_bind(result, auth);
768         if (!NT_STATUS_IS_OK(status)) {
769                 DEBUG(0, ("Failed to bind spoolss pipe.\n"));
770                 goto done;
771         }
772 done:
773         if (!NT_STATUS_IS_OK(status)) {
774                 TALLOC_FREE(result);
775         }
776         TALLOC_FREE(proxy_state);
777         *_result = result;
778         return status;
779 }
780
781 /**
782  * @brief Create a new RPC client context which uses a local dispatch function.
783  *
784  * @param mem_ctx       The memory context on which thje pipe will ultimately
785  *                      be allocated
786  * @param name          The pipe name to connect to.
787  * @param server_info   Credentials to use for the connection.
788  * @param pipe          [in|out] Checks if a pipe is connected, and connects it
789  *                               if not
790  *
791  * @return              NT_STATUS_OK on success, a corresponding NT status if
792  *                      an error occured.
793  */
794
795 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
796                                  const struct ndr_syntax_id *syntax,
797                                  const struct auth_serversupplied_info *server_info,
798                                  struct client_address *client_id,
799                                  struct messaging_context *msg_ctx,
800                                  struct rpc_pipe_client **cli_pipe)
801 {
802         struct rpc_pipe_client *cli = NULL;
803         const char *server_type;
804         const char *pipe_name;
805         NTSTATUS status;
806         TALLOC_CTX *tmp_ctx;
807
808         if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
809                 return NT_STATUS_OK;
810         } else {
811                 TALLOC_FREE(*cli_pipe);
812         }
813
814         tmp_ctx = talloc_stackframe();
815         if (tmp_ctx == NULL) {
816                 return NT_STATUS_NO_MEMORY;
817         }
818
819         pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
820         if (pipe_name == NULL) {
821                 status = NT_STATUS_INVALID_PARAMETER;
822                 goto done;
823         }
824
825         while (pipe_name[0] == '\\') {
826                 pipe_name++;
827         }
828
829         DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
830
831         server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
832                                            "rpc_server", pipe_name,
833                                            "embedded");
834
835         if (StrCaseCmp(server_type, "embedded") == 0) {
836                 status = rpc_pipe_open_internal(tmp_ctx,
837                                                 syntax, server_info,
838                                                 client_id, msg_ctx,
839                                                 &cli);
840                 if (!NT_STATUS_IS_OK(status)) {
841                         goto done;
842                 }
843         } else {
844                 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
845                  * for now we need to use the special proxy setup to connect
846                  * to spoolssd. */
847
848                 status = rpc_pipe_open_external(tmp_ctx,
849                                                 pipe_name, syntax,
850                                                 server_info,
851                                                 &cli);
852                 if (!NT_STATUS_IS_OK(status)) {
853                         goto done;
854                 }
855         }
856
857         status = NT_STATUS_OK;
858 done:
859         if (NT_STATUS_IS_OK(status)) {
860                 *cli_pipe = talloc_move(mem_ctx, &cli);
861         }
862         TALLOC_FREE(tmp_ctx);
863         return status;
864 }