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