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