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