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