s3-rpc_server: Add make_external_rpc_pipe() 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 NTSTATUS make_external_rpc_pipe(TALLOC_CTX *mem_ctx,
547                                 const char *pipe_name,
548                                 const struct tsocket_address *local_address,
549                                 const struct tsocket_address *remote_address,
550                                 const struct auth_session_info *session_info,
551                                 struct npa_state **pnpa)
552 {
553         TALLOC_CTX *tmp_ctx = talloc_stackframe();
554         struct auth_session_info_transport *session_info_t;
555         struct tevent_context *ev_ctx;
556         struct tevent_req *subreq;
557         const char *socket_np_dir;
558         const char *socket_dir;
559         struct npa_state *npa;
560         int sys_errno;
561         NTSTATUS status;
562         int rc = -1;
563         bool ok;
564
565         npa = npa_state_init(tmp_ctx);
566         if (npa == NULL) {
567                 status = NT_STATUS_NO_MEMORY;
568                 goto out;
569         }
570
571         socket_dir = lp_parm_const_string(GLOBAL_SECTION_SNUM,
572                                           "external_rpc_pipe",
573                                           "socket_dir",
574                                           lp_ncalrpc_dir());
575         if (socket_dir == NULL) {
576                 DEBUG(0, ("external_rpc_pipe: socket_dir not set\n"));
577                 status = NT_STATUS_PIPE_NOT_AVAILABLE;
578                 goto out;
579         }
580
581         socket_np_dir = talloc_asprintf(tmp_ctx, "%s/np", socket_dir);
582         if (socket_np_dir == NULL) {
583                 DEBUG(0, ("talloc_asprintf failed\n"));
584                 status = NT_STATUS_NO_MEMORY;
585                 goto out;
586         }
587
588         session_info_t = talloc_zero(tmp_ctx,
589                                      struct auth_session_info_transport);
590         if (session_info_t == NULL) {
591                 DEBUG(0, ("talloc failed\n"));
592                 status = NT_STATUS_NO_MEMORY;
593                 goto out;
594         }
595
596         session_info_t->session_info = copy_session_info(session_info_t,
597                                                          session_info);
598         if (session_info_t->session_info == NULL) {
599                 DEBUG(0, ("copy_session_info failed\n"));
600                 status = NT_STATUS_NO_MEMORY;
601                 goto out;
602         }
603
604         ev_ctx = s3_tevent_context_init(tmp_ctx);
605         if (ev_ctx == NULL) {
606                 DEBUG(0, ("s3_tevent_context_init failed\n"));
607                 status = NT_STATUS_NO_MEMORY;
608                 goto out;
609         }
610
611         become_root();
612         subreq = tstream_npa_connect_send(tmp_ctx,
613                                           ev_ctx,
614                                           socket_np_dir,
615                                           pipe_name,
616                                           remote_address, /* client_addr */
617                                           NULL, /* client_name */
618                                           local_address, /* server_addr */
619                                           NULL, /* server_name */
620                                           session_info_t);
621         if (subreq == NULL) {
622                 unbecome_root();
623                 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
624                           "user %s\\%s failed\n",
625                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
626                           session_info_t->session_info->info->account_name));
627                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
628                 goto out;
629         }
630         ok = tevent_req_poll(subreq, ev_ctx);
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,
636                           pipe_name,
637                           session_info_t->session_info->info->domain_name,
638                           session_info_t->session_info->info->account_name,
639                           strerror(errno)));
640                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
641                 goto out;
642         }
643
644         rc = tstream_npa_connect_recv(subreq,
645                                       &sys_errno,
646                                       npa,
647                                       &npa->stream,
648                                       &npa->file_type,
649                                       &npa->device_state,
650                                       &npa->allocation_size);
651         talloc_free(subreq);
652         if (rc != 0) {
653                 int l = 1;
654
655                 if (errno == ENOENT) {
656                         l = 2;
657                 }
658
659                 DEBUG(l, ("tstream_npa_connect_recv  to %s for pipe %s and "
660                           "user %s\\%s failed: %s\n",
661                           socket_np_dir,
662                           pipe_name,
663                           session_info_t->session_info->info->domain_name,
664                           session_info_t->session_info->info->account_name,
665                           strerror(sys_errno)));
666                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
667                 goto out;
668         }
669
670         *pnpa = talloc_steal(mem_ctx, npa);
671         status = NT_STATUS_OK;
672 out:
673         talloc_free(tmp_ctx);
674
675         return status;
676 }
677
678 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
679                                 const char *pipe_name,
680                                 const struct tsocket_address *local_address,
681                                 const struct tsocket_address *remote_address,
682                                 const struct auth_session_info *session_info)
683 {
684         struct np_proxy_state *result;
685         char *socket_np_dir;
686         const char *socket_dir;
687         struct tevent_context *ev;
688         struct tevent_req *subreq;
689         struct auth_session_info_transport *session_info_t;
690         bool ok;
691         int ret;
692         int sys_errno;
693
694         result = talloc(mem_ctx, struct np_proxy_state);
695         if (result == NULL) {
696                 DEBUG(0, ("talloc failed\n"));
697                 return NULL;
698         }
699
700         result->read_queue = tevent_queue_create(result, "np_read");
701         if (result->read_queue == NULL) {
702                 DEBUG(0, ("tevent_queue_create failed\n"));
703                 goto fail;
704         }
705
706         result->write_queue = tevent_queue_create(result, "np_write");
707         if (result->write_queue == NULL) {
708                 DEBUG(0, ("tevent_queue_create failed\n"));
709                 goto fail;
710         }
711
712         ev = s3_tevent_context_init(talloc_tos());
713         if (ev == NULL) {
714                 DEBUG(0, ("s3_tevent_context_init failed\n"));
715                 goto fail;
716         }
717
718         socket_dir = lp_parm_const_string(
719                 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
720                 lp_ncalrpc_dir());
721         if (socket_dir == NULL) {
722                 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
723                 goto fail;
724         }
725         socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
726         if (socket_np_dir == NULL) {
727                 DEBUG(0, ("talloc_asprintf failed\n"));
728                 goto fail;
729         }
730
731         session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
732         if (session_info_t == NULL) {
733                 DEBUG(0, ("talloc failed\n"));
734                 goto fail;
735         }
736
737         session_info_t->session_info = copy_session_info(session_info_t,
738                                                          session_info);
739         if (session_info_t->session_info == NULL) {
740                 DEBUG(0, ("copy_session_info failed\n"));
741                 goto fail;
742         }
743
744         become_root();
745         subreq = tstream_npa_connect_send(talloc_tos(), ev,
746                                           socket_np_dir,
747                                           pipe_name,
748                                           remote_address, /* client_addr */
749                                           NULL, /* client_name */
750                                           local_address, /* server_addr */
751                                           NULL, /* server_name */
752                                           session_info_t);
753         if (subreq == NULL) {
754                 unbecome_root();
755                 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
756                           "user %s\\%s failed\n",
757                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
758                           session_info_t->session_info->info->account_name));
759                 goto fail;
760         }
761         ok = tevent_req_poll(subreq, ev);
762         unbecome_root();
763         if (!ok) {
764                 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
765                           "failed for tstream_npa_connect: %s\n",
766                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
767                           session_info_t->session_info->info->account_name,
768                           strerror(errno)));
769                 goto fail;
770
771         }
772         ret = tstream_npa_connect_recv(subreq, &sys_errno,
773                                        result,
774                                        &result->npipe,
775                                        &result->file_type,
776                                        &result->device_state,
777                                        &result->allocation_size);
778         TALLOC_FREE(subreq);
779         if (ret != 0) {
780                 int l = 1;
781                 if (errno == ENOENT) {
782                         l = 2;
783                 }
784                 DEBUG(l, ("tstream_npa_connect_recv  to %s for pipe %s and "
785                           "user %s\\%s failed: %s\n",
786                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
787                           session_info_t->session_info->info->account_name,
788                           strerror(sys_errno)));
789                 goto fail;
790         }
791
792         return result;
793
794  fail:
795         TALLOC_FREE(result);
796         return NULL;
797 }
798
799 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
800                                 const char *pipe_name,
801                                 const struct ndr_interface_table *table,
802                                 const struct auth_session_info *session_info,
803                                 struct rpc_pipe_client **_result)
804 {
805         struct tsocket_address *local, *remote;
806         struct rpc_pipe_client *result = NULL;
807         struct np_proxy_state *proxy_state = NULL;
808         struct pipe_auth_data *auth;
809         NTSTATUS status;
810         int ret;
811
812         /* this is an internal connection, fake up ip addresses */
813         ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
814                                                 NULL, 0, &local);
815         if (ret) {
816                 return NT_STATUS_NO_MEMORY;
817         }
818         ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
819                                                 NULL, 0, &remote);
820         if (ret) {
821                 return NT_STATUS_NO_MEMORY;
822         }
823
824         proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
825                                                 local, remote, session_info);
826         if (!proxy_state) {
827                 return NT_STATUS_UNSUCCESSFUL;
828         }
829
830         result = talloc_zero(mem_ctx, struct rpc_pipe_client);
831         if (result == NULL) {
832                 status = NT_STATUS_NO_MEMORY;
833                 goto done;
834         }
835
836         result->abstract_syntax = table->syntax_id;
837         result->transfer_syntax = ndr_transfer_syntax_ndr;
838
839         result->desthost = get_myname(result);
840         result->srv_name_slash = talloc_asprintf_strupper_m(
841                 result, "\\\\%s", result->desthost);
842         if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
843                 status = NT_STATUS_NO_MEMORY;
844                 goto done;
845         }
846
847         result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
848         result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
849
850         status = rpc_transport_tstream_init(result,
851                                             &proxy_state->npipe,
852                                             &result->transport);
853         if (!NT_STATUS_IS_OK(status)) {
854                 goto done;
855         }
856
857         result->binding_handle = rpccli_bh_create(result, NULL, table);
858         if (result->binding_handle == NULL) {
859                 status = NT_STATUS_NO_MEMORY;
860                 DEBUG(0, ("Failed to create binding handle.\n"));
861                 goto done;
862         }
863
864         result->auth = talloc_zero(result, struct pipe_auth_data);
865         if (!result->auth) {
866                 status = NT_STATUS_NO_MEMORY;
867                 goto done;
868         }
869         result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
870         result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
871
872         status = rpccli_anon_bind_data(result, &auth);
873         if (!NT_STATUS_IS_OK(status)) {
874                 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
875                 goto done;
876         }
877
878         status = rpc_pipe_bind(result, auth);
879         if (!NT_STATUS_IS_OK(status)) {
880                 DEBUG(0, ("Failed to bind external pipe.\n"));
881                 goto done;
882         }
883
884 done:
885         if (!NT_STATUS_IS_OK(status)) {
886                 TALLOC_FREE(result);
887         }
888         TALLOC_FREE(proxy_state);
889         *_result = result;
890         return status;
891 }
892
893 /**
894  * @brief Create a new RPC client context which uses a local dispatch function
895  *        or a remote transport, depending on rpc_server configuration for the
896  *        specific service.
897  *
898  * @param[in]  mem_ctx  The memory context to use.
899  *
900  * @param[in]  abstract_syntax Normally the syntax_id of the autogenerated
901  *                             ndr_table_<name>.
902  *
903  * @param[in]  serversupplied_info The server supplied authentication function.
904  *
905  * @param[in]  remote_address The client address information.
906  *
907  * @param[in]  msg_ctx  The messaging context to use.
908  *
909  * @param[out] presult  A pointer to store the connected rpc client pipe.
910  *
911  * @return              NT_STATUS_OK on success, a corresponding NT status if an
912  *                      error occured.
913  *
914  * @code
915  *   struct rpc_pipe_client *winreg_pipe;
916  *   NTSTATUS status;
917  *
918  *   status = rpc_pipe_open_interface(tmp_ctx,
919  *                                    &ndr_table_winreg.syntax_id,
920  *                                    p->session_info,
921  *                                    remote_address,
922  *                                    &winreg_pipe);
923  * @endcode
924  */
925
926 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
927                                  const struct ndr_interface_table *table,
928                                  const struct auth_session_info *session_info,
929                                  const struct tsocket_address *remote_address,
930                                  struct messaging_context *msg_ctx,
931                                  struct rpc_pipe_client **cli_pipe)
932 {
933         struct rpc_pipe_client *cli = NULL;
934         enum rpc_service_mode_e pipe_mode;
935         const char *pipe_name;
936         NTSTATUS status;
937         TALLOC_CTX *tmp_ctx;
938
939         if (cli_pipe != NULL) {
940                 if (rpccli_is_connected(*cli_pipe)) {
941                         return NT_STATUS_OK;
942                 } else {
943                         TALLOC_FREE(*cli_pipe);
944                 }
945         }
946
947         tmp_ctx = talloc_stackframe();
948         if (tmp_ctx == NULL) {
949                 return NT_STATUS_NO_MEMORY;
950         }
951
952         pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
953         if (pipe_name == NULL) {
954                 status = NT_STATUS_INVALID_PARAMETER;
955                 goto done;
956         }
957
958         while (pipe_name[0] == '\\') {
959                 pipe_name++;
960         }
961
962         DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
963
964         pipe_mode = rpc_service_mode(pipe_name);
965
966         switch (pipe_mode) {
967         case RPC_SERVICE_MODE_EMBEDDED:
968                 status = rpc_pipe_open_internal(tmp_ctx,
969                                                 &table->syntax_id, session_info,
970                                                 remote_address, msg_ctx,
971                                                 &cli);
972                 if (!NT_STATUS_IS_OK(status)) {
973                         goto done;
974                 }
975                 break;
976         case RPC_SERVICE_MODE_EXTERNAL:
977                 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
978                  * for now we need to use the special proxy setup to connect
979                  * to spoolssd. */
980
981                 status = rpc_pipe_open_external(tmp_ctx,
982                                                 pipe_name, table,
983                                                 session_info,
984                                                 &cli);
985                 if (!NT_STATUS_IS_OK(status)) {
986                         goto done;
987                 }
988                 break;
989         case RPC_SERVICE_MODE_DISABLED:
990                 status = NT_STATUS_NOT_IMPLEMENTED;
991                 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
992                           pipe_name, nt_errstr(status)));
993                 goto done;
994         }
995
996         status = NT_STATUS_OK;
997 done:
998         if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
999                 *cli_pipe = talloc_move(mem_ctx, &cli);
1000         }
1001         TALLOC_FREE(tmp_ctx);
1002         return status;
1003 }