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