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