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