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