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