0fa8e83329500d82fe39767ed760ac7b7158d0c1
[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  *  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         struct auth_user_info_dc *user_info_dc;
633         union netr_Validation val;
634         NTSTATUS status;
635         bool ok;
636         int ret;
637         int sys_errno;
638
639         result = talloc(mem_ctx, struct np_proxy_state);
640         if (result == NULL) {
641                 DEBUG(0, ("talloc failed\n"));
642                 return NULL;
643         }
644
645         result->read_queue = tevent_queue_create(result, "np_read");
646         if (result->read_queue == NULL) {
647                 DEBUG(0, ("tevent_queue_create failed\n"));
648                 goto fail;
649         }
650
651         result->write_queue = tevent_queue_create(result, "np_write");
652         if (result->write_queue == NULL) {
653                 DEBUG(0, ("tevent_queue_create failed\n"));
654                 goto fail;
655         }
656
657         ev = s3_tevent_context_init(talloc_tos());
658         if (ev == NULL) {
659                 DEBUG(0, ("s3_tevent_context_init failed\n"));
660                 goto fail;
661         }
662
663         socket_dir = lp_parm_const_string(
664                 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
665                 lp_ncalrpc_dir());
666         if (socket_dir == NULL) {
667                 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
668                 goto fail;
669         }
670         socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
671         if (socket_np_dir == NULL) {
672                 DEBUG(0, ("talloc_asprintf failed\n"));
673                 goto fail;
674         }
675
676         session_info_npa = talloc_zero(talloc_tos(), struct auth_session_info);
677         if (session_info_npa == NULL) {
678                 DEBUG(0, ("talloc failed\n"));
679                 goto fail;
680         }
681
682         /* Send the named_pipe_auth server the user's full token */
683         session_info_npa->security_token = session_info->security_token;
684         session_info_npa->session_key = session_info->session_key;
685
686         val.sam3 = session_info->info3;
687
688         /* Convert into something we can build a struct
689          * auth_session_info from.  Most of the work here
690          * will be to convert the SIDS, which we will then ignore, but
691          * this is the easier way to handle it */
692         status = make_user_info_dc_netlogon_validation(talloc_tos(), "", 3, &val, &user_info_dc);
693         if (!NT_STATUS_IS_OK(status)) {
694                 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
695                 goto fail;
696         }
697
698         session_info_npa->info = talloc_move(session_info_npa, &user_info_dc->info);
699         talloc_free(user_info_dc);
700
701         session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
702         if (session_info_npa == NULL) {
703                 DEBUG(0, ("talloc failed\n"));
704                 goto fail;
705         }
706
707         session_info_t->session_info = talloc_steal(session_info_t, session_info_npa);
708
709         become_root();
710         subreq = tstream_npa_connect_send(talloc_tos(), ev,
711                                           socket_np_dir,
712                                           pipe_name,
713                                           remote_address, /* client_addr */
714                                           NULL, /* client_name */
715                                           local_address, /* server_addr */
716                                           NULL, /* server_name */
717                                           session_info_t);
718         if (subreq == NULL) {
719                 unbecome_root();
720                 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
721                           "user %s\\%s failed\n",
722                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
723                           session_info_t->session_info->info->account_name));
724                 goto fail;
725         }
726         ok = tevent_req_poll(subreq, ev);
727         unbecome_root();
728         if (!ok) {
729                 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
730                           "failed for tstream_npa_connect: %s\n",
731                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
732                           session_info_t->session_info->info->account_name,
733                           strerror(errno)));
734                 goto fail;
735
736         }
737         ret = tstream_npa_connect_recv(subreq, &sys_errno,
738                                        result,
739                                        &result->npipe,
740                                        &result->file_type,
741                                        &result->device_state,
742                                        &result->allocation_size);
743         TALLOC_FREE(subreq);
744         if (ret != 0) {
745                 DEBUG(0, ("tstream_npa_connect_recv  to %s for pipe %s and "
746                           "user %s\\%s failed: %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(sys_errno)));
750                 goto fail;
751         }
752
753         return result;
754
755  fail:
756         TALLOC_FREE(result);
757         return NULL;
758 }
759
760 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
761                                 const char *pipe_name,
762                                 const struct ndr_syntax_id *abstract_syntax,
763                                 const struct auth3_session_info *session_info,
764                                 struct rpc_pipe_client **_result)
765 {
766         struct tsocket_address *local, *remote;
767         struct rpc_pipe_client *result = NULL;
768         struct np_proxy_state *proxy_state = NULL;
769         struct pipe_auth_data *auth;
770         NTSTATUS status;
771         int ret;
772
773         /* this is an internal connection, fake up ip addresses */
774         ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
775                                                 NULL, 0, &local);
776         if (ret) {
777                 return NT_STATUS_NO_MEMORY;
778         }
779         ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
780                                                 NULL, 0, &remote);
781         if (ret) {
782                 return NT_STATUS_NO_MEMORY;
783         }
784
785         proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
786                                                 local, remote, session_info);
787         if (!proxy_state) {
788                 return NT_STATUS_UNSUCCESSFUL;
789         }
790
791         result = talloc_zero(mem_ctx, struct rpc_pipe_client);
792         if (result == NULL) {
793                 status = NT_STATUS_NO_MEMORY;
794                 goto done;
795         }
796
797         result->abstract_syntax = *abstract_syntax;
798         result->transfer_syntax = ndr_transfer_syntax;
799
800         result->desthost = get_myname(result);
801         result->srv_name_slash = talloc_asprintf_strupper_m(
802                 result, "\\\\%s", result->desthost);
803         if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
804                 status = NT_STATUS_NO_MEMORY;
805                 goto done;
806         }
807
808         result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
809         result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
810
811         status = rpc_transport_tstream_init(result,
812                                             &proxy_state->npipe,
813                                             &result->transport);
814         if (!NT_STATUS_IS_OK(status)) {
815                 goto done;
816         }
817
818         result->binding_handle = rpccli_bh_create(result);
819         if (result->binding_handle == NULL) {
820                 status = NT_STATUS_NO_MEMORY;
821                 DEBUG(0, ("Failed to create binding handle.\n"));
822                 goto done;
823         }
824
825         result->auth = talloc_zero(result, struct pipe_auth_data);
826         if (!result->auth) {
827                 status = NT_STATUS_NO_MEMORY;
828                 goto done;
829         }
830         result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
831         result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
832
833         status = rpccli_anon_bind_data(result, &auth);
834         if (!NT_STATUS_IS_OK(status)) {
835                 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
836                 goto done;
837         }
838
839         status = rpc_pipe_bind(result, auth);
840         if (!NT_STATUS_IS_OK(status)) {
841                 DEBUG(0, ("Failed to bind external pipe.\n"));
842                 goto done;
843         }
844
845 done:
846         if (!NT_STATUS_IS_OK(status)) {
847                 TALLOC_FREE(result);
848         }
849         TALLOC_FREE(proxy_state);
850         *_result = result;
851         return status;
852 }
853
854 /**
855  * @brief Create a new RPC client context which uses a local dispatch function
856  *        or a remote transport, depending on rpc_server configuration for the
857  *        specific service.
858  *
859  * @param[in]  mem_ctx  The memory context to use.
860  *
861  * @param[in]  abstract_syntax Normally the syntax_id of the autogenerated
862  *                             ndr_table_<name>.
863  *
864  * @param[in]  serversupplied_info The server supplied authentication function.
865  *
866  * @param[in]  remote_address The client address information.
867  *
868  * @param[in]  msg_ctx  The messaging context to use.
869  *
870  * @param[out] presult  A pointer to store the connected rpc client pipe.
871  *
872  * @return              NT_STATUS_OK on success, a corresponding NT status if an
873  *                      error occured.
874  *
875  * @code
876  *   struct rpc_pipe_client *winreg_pipe;
877  *   NTSTATUS status;
878  *
879  *   status = rpc_pipe_open_interface(tmp_ctx,
880  *                                    &ndr_table_winreg.syntax_id,
881  *                                    p->session_info,
882  *                                    remote_address,
883  *                                    &winreg_pipe);
884  * @endcode
885  */
886
887 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
888                                  const struct ndr_syntax_id *syntax,
889                                  const struct auth3_session_info *session_info,
890                                  const struct tsocket_address *remote_address,
891                                  struct messaging_context *msg_ctx,
892                                  struct rpc_pipe_client **cli_pipe)
893 {
894         struct rpc_pipe_client *cli = NULL;
895         const char *server_type;
896         const char *pipe_name;
897         NTSTATUS status;
898         TALLOC_CTX *tmp_ctx;
899
900         if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
901                 return NT_STATUS_OK;
902         } else {
903                 TALLOC_FREE(*cli_pipe);
904         }
905
906         tmp_ctx = talloc_stackframe();
907         if (tmp_ctx == NULL) {
908                 return NT_STATUS_NO_MEMORY;
909         }
910
911         pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
912         if (pipe_name == NULL) {
913                 status = NT_STATUS_INVALID_PARAMETER;
914                 goto done;
915         }
916
917         while (pipe_name[0] == '\\') {
918                 pipe_name++;
919         }
920
921         DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
922
923         server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
924                                            "rpc_server", pipe_name,
925                                            "embedded");
926
927         if (strcasecmp_m(server_type, "embedded") == 0) {
928                 status = rpc_pipe_open_internal(tmp_ctx,
929                                                 syntax, session_info,
930                                                 remote_address, msg_ctx,
931                                                 &cli);
932                 if (!NT_STATUS_IS_OK(status)) {
933                         goto done;
934                 }
935         } else if (strcasecmp_m(server_type, "daemon") == 0 ||
936                    strcasecmp_m(server_type, "external") == 0) {
937                 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
938                  * for now we need to use the special proxy setup to connect
939                  * to spoolssd. */
940
941                 status = rpc_pipe_open_external(tmp_ctx,
942                                                 pipe_name, syntax,
943                                                 session_info,
944                                                 &cli);
945                 if (!NT_STATUS_IS_OK(status)) {
946                         goto done;
947                 }
948         } else {
949                 status = NT_STATUS_NOT_IMPLEMENTED;
950                 DEBUG(0, ("Wrong servertype specified in config file: %s",
951                           nt_errstr(status)));
952                 goto done;
953         }
954
955         status = NT_STATUS_OK;
956 done:
957         if (NT_STATUS_IS_OK(status)) {
958                 *cli_pipe = talloc_move(mem_ctx, &cli);
959         }
960         TALLOC_FREE(tmp_ctx);
961         return status;
962 }