da22c24b638f5d5b83226b0e8923804e6b6c7447
[npower/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/auth_util.h"
34 #include "auth.h"
35 #include "rpc_server/rpc_pipes.h"
36 #include "../lib/tsocket/tsocket.h"
37 #include "../lib/util/tevent_ntstatus.h"
38 #include "rpc_contexts.h"
39 #include "rpc_server/rpc_config.h"
40 #include "librpc/ndr/ndr_table.h"
41 #include "rpc_server/rpc_server.h"
42
43 #undef DBGC_CLASS
44 #define DBGC_CLASS DBGC_RPC_SRV
45
46 static NTSTATUS make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
47                                 const struct ndr_syntax_id *syntax,
48                                 const struct tsocket_address *remote_address,
49                                 const struct tsocket_address *local_address,
50                                 const struct auth_session_info *session_info,
51                                 struct messaging_context *msg_ctx,
52                                 struct pipes_struct **p);
53
54 static struct npa_state *npa_state_init(TALLOC_CTX *mem_ctx)
55 {
56         struct npa_state *npa;
57
58         npa = talloc_zero(mem_ctx, struct npa_state);
59         if (npa == NULL) {
60                 return NULL;
61         }
62
63         npa->read_queue = tevent_queue_create(npa, "npa_cli_read");
64         if (npa->read_queue == NULL) {
65                 DEBUG(0, ("tevent_queue_create failed\n"));
66                 goto fail;
67         }
68
69         npa->write_queue = tevent_queue_create(npa, "npa_cli_write");
70         if (npa->write_queue == NULL) {
71                 DEBUG(0, ("tevent_queue_create failed\n"));
72                 goto fail;
73         }
74
75         return npa;
76 fail:
77         talloc_free(npa);
78         return NULL;
79 }
80
81 NTSTATUS make_internal_rpc_pipe_socketpair(
82         TALLOC_CTX *mem_ctx,
83         struct tevent_context *ev_ctx,
84         struct messaging_context *msg_ctx,
85         const char *pipe_name,
86         const struct ndr_syntax_id *syntax,
87         const struct tsocket_address *remote_address,
88         const struct tsocket_address *local_address,
89         const struct auth_session_info *session_info,
90         struct npa_state **pnpa)
91 {
92         TALLOC_CTX *tmp_ctx = talloc_stackframe();
93         struct dcerpc_ncacn_conn *ncacn_conn = NULL;
94         struct tevent_req *subreq;
95         struct npa_state *npa;
96         NTSTATUS status;
97         int error;
98         int rc;
99
100         DEBUG(4, ("Create of internal pipe %s requested\n", pipe_name));
101
102         npa = npa_state_init(tmp_ctx);
103         if (npa == NULL) {
104                 status = NT_STATUS_NO_MEMORY;
105                 goto out;
106         }
107
108         npa->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
109         npa->device_state = 0xff | 0x0400 | 0x0100;
110         npa->allocation_size = 4096;
111
112         status = dcerpc_ncacn_conn_init(npa,
113                                         ev_ctx,
114                                         msg_ctx,
115                                         NCACN_NP,
116                                         pipe_name,
117                                         NULL, /* termination fn */
118                                         NULL, /* termination data */
119                                         &ncacn_conn);
120         if (!NT_STATUS_IS_OK(status)) {
121                 goto out;
122         }
123
124         npa->private_data = (void*)ncacn_conn;
125
126         rc = tstream_npa_socketpair(npa->file_type,
127                                     npa,
128                                     &npa->stream,
129                                     ncacn_conn,
130                                     &ncacn_conn->tstream);
131         if (rc == -1) {
132                 status = map_nt_error_from_unix(errno);
133                 goto out;
134         }
135
136         ncacn_conn->remote_client_addr = tsocket_address_copy(remote_address,
137                         ncacn_conn);
138         if (ncacn_conn->remote_client_addr == NULL) {
139                 status = NT_STATUS_NO_MEMORY;
140                 goto out;
141         }
142
143         ncacn_conn->remote_client_name = tsocket_address_inet_addr_string(
144                         ncacn_conn->remote_client_addr, ncacn_conn);
145         if (ncacn_conn->remote_client_name == NULL) {
146                 status = NT_STATUS_NO_MEMORY;
147                 goto out;
148         }
149
150         ncacn_conn->local_server_addr = tsocket_address_copy(local_address,
151                         ncacn_conn);
152         if (ncacn_conn->local_server_addr == NULL) {
153                 status = NT_STATUS_NO_MEMORY;
154                 goto out;
155         }
156
157         ncacn_conn->local_server_name = tsocket_address_inet_addr_string(
158                 ncacn_conn->local_server_addr, ncacn_conn);
159         if (ncacn_conn->local_server_name == NULL) {
160                 status = NT_STATUS_NO_MEMORY;
161                 goto out;
162         }
163
164         ncacn_conn->session_info = copy_session_info(ncacn_conn, session_info);
165         if (ncacn_conn->session_info == NULL) {
166                 status = NT_STATUS_NO_MEMORY;
167                 goto out;
168         }
169
170         rc = make_server_pipes_struct(ncacn_conn,
171                                       ncacn_conn->msg_ctx,
172                                       ncacn_conn->name,
173                                       ncacn_conn->transport,
174                                       ncacn_conn->remote_client_addr,
175                                       ncacn_conn->local_server_addr,
176                                       &ncacn_conn->session_info,
177                                       &ncacn_conn->p,
178                                       &error);
179         if (rc == -1) {
180                 status = map_nt_error_from_unix(error);
181                 goto out;
182         }
183
184         ncacn_conn->send_queue = tevent_queue_create(ncacn_conn, "npa_server_write_queue");
185         if (ncacn_conn->send_queue == NULL) {
186                 status = NT_STATUS_NO_MEMORY;
187                 goto out;
188         }
189
190         subreq = dcerpc_read_ncacn_packet_send(ncacn_conn, ncacn_conn->ev_ctx,
191                                                ncacn_conn->tstream);
192         if (subreq == NULL) {
193                 DEBUG(2, ("Failed to start receiving packets\n"));
194                 status = NT_STATUS_PIPE_BROKEN;
195                 goto out;
196         }
197         tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process,
198                                 ncacn_conn);
199
200         *pnpa = talloc_move(mem_ctx, &npa);
201         status = NT_STATUS_OK;
202 out:
203         talloc_free(tmp_ctx);
204         return status;
205 }
206
207 /****************************************************************************
208  Make an internal namedpipes structure
209 ****************************************************************************/
210
211 static NTSTATUS make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
212                                 const struct ndr_syntax_id *syntax,
213                                 const struct tsocket_address *remote_address,
214                                 const struct tsocket_address *local_address,
215                                 const struct auth_session_info *session_info,
216                                 struct messaging_context *msg_ctx,
217                                 struct pipes_struct **p)
218 {
219         struct pipes_struct *out;
220         struct pipe_rpc_fns *context_fns;
221         const char *pipe_name;
222         int ret;
223         const struct ndr_interface_table *table;
224
225         table = ndr_table_by_uuid(&syntax->uuid);
226         if (table == NULL) {
227                 DBG_ERR("Unknown interface\n");
228                 return NT_STATUS_RPC_INTERFACE_NOT_FOUND;
229         }
230
231         pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
232
233         DBG_INFO("Create pipe requested %s\n", pipe_name);
234
235         ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
236                                      NCALRPC, RPC_LITTLE_ENDIAN,
237                                      remote_address, local_address, &out);
238         if (ret) {
239                 DBG_ERR("No memory for pipes_struct!\n");
240                 return NT_STATUS_NO_MEMORY;
241         }
242
243         if (!init_pipe_handles(out, syntax)) {
244                 DBG_ERR("init_pipe_handles failed.\n");
245                 TALLOC_FREE(out);
246                 return NT_STATUS_UNSUCCESSFUL;
247         }
248
249         out->session_info = copy_session_info(out, session_info);
250         if (out->session_info == NULL) {
251                 DBG_ERR("copy_serverinfo failed\n");
252                 close_policy_by_pipe(out);
253                 TALLOC_FREE(out);
254                 return NT_STATUS_UNSUCCESSFUL;
255         }
256
257         context_fns = talloc_zero(out, struct pipe_rpc_fns);
258         if (context_fns == NULL) {
259                 DBG_ERR("No memory");
260                 TALLOC_FREE(out);
261                 return NT_STATUS_NO_MEMORY;
262         }
263
264         context_fns->next = context_fns->prev = NULL;
265         context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
266         context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
267         context_fns->context_id = 0;
268         context_fns->syntax = *syntax;
269
270         /* add to the list of open contexts */
271         DLIST_ADD(out->contexts, context_fns);
272
273         DEBUG(4,("Created internal pipe %s\n", pipe_name));
274
275         *p = out;
276
277         return NT_STATUS_OK;
278 }
279
280 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
281                                 TALLOC_CTX *mem_ctx,
282                                 uint32_t opnum,
283                                 const DATA_BLOB *in_data,
284                                 DATA_BLOB *out_data)
285 {
286         struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
287         uint32_t num_cmds = fns->n_cmds;
288         const struct api_struct *cmds = fns->cmds;
289         uint32_t i;
290         bool ok;
291
292         /* set opnum */
293         p->opnum = opnum;
294
295         for (i = 0; i < num_cmds; i++) {
296                 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
297                         break;
298                 }
299         }
300
301         if (i == num_cmds) {
302                 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
303         }
304
305         p->in_data.data = *in_data;
306         p->out_data.rdata = data_blob_null;
307
308         ok = cmds[i].fn(p);
309         p->in_data.data = data_blob_null;
310         if (!ok) {
311                 data_blob_free(&p->out_data.rdata);
312                 talloc_free_children(p->mem_ctx);
313                 return NT_STATUS_RPC_CALL_FAILED;
314         }
315
316         if (p->fault_state) {
317                 NTSTATUS status;
318
319                 status = NT_STATUS(p->fault_state);
320                 p->fault_state = 0;
321                 data_blob_free(&p->out_data.rdata);
322                 talloc_free_children(p->mem_ctx);
323                 return status;
324         }
325
326         *out_data = p->out_data.rdata;
327         talloc_steal(mem_ctx, out_data->data);
328         p->out_data.rdata = data_blob_null;
329
330         talloc_free_children(p->mem_ctx);
331         return NT_STATUS_OK;
332 }
333
334 struct rpcint_bh_state {
335         struct pipes_struct *p;
336 };
337
338 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
339 {
340         struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
341                                      struct rpcint_bh_state);
342
343         if (!hs->p) {
344                 return false;
345         }
346
347         return true;
348 }
349
350 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
351                                       uint32_t timeout)
352 {
353         /* TODO: implement timeouts */
354         return UINT32_MAX;
355 }
356
357 struct rpcint_bh_raw_call_state {
358         DATA_BLOB in_data;
359         DATA_BLOB out_data;
360         uint32_t out_flags;
361 };
362
363 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
364                                                   struct tevent_context *ev,
365                                                   struct dcerpc_binding_handle *h,
366                                                   const struct GUID *object,
367                                                   uint32_t opnum,
368                                                   uint32_t in_flags,
369                                                   const uint8_t *in_data,
370                                                   size_t in_length)
371 {
372         struct rpcint_bh_state *hs =
373                 dcerpc_binding_handle_data(h,
374                 struct rpcint_bh_state);
375         struct tevent_req *req;
376         struct rpcint_bh_raw_call_state *state;
377         bool ok;
378         NTSTATUS status;
379
380         req = tevent_req_create(mem_ctx, &state,
381                                 struct rpcint_bh_raw_call_state);
382         if (req == NULL) {
383                 return NULL;
384         }
385         state->in_data.data = discard_const_p(uint8_t, in_data);
386         state->in_data.length = in_length;
387
388         ok = rpcint_bh_is_connected(h);
389         if (!ok) {
390                 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
391                 return tevent_req_post(req, ev);
392         }
393
394         /* TODO: allow async */
395         status = rpcint_dispatch(hs->p, state, opnum,
396                                  &state->in_data,
397                                  &state->out_data);
398         if (!NT_STATUS_IS_OK(status)) {
399                 tevent_req_nterror(req, status);
400                 return tevent_req_post(req, ev);
401         }
402
403         tevent_req_done(req);
404         return tevent_req_post(req, ev);
405 }
406
407 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
408                                         TALLOC_CTX *mem_ctx,
409                                         uint8_t **out_data,
410                                         size_t *out_length,
411                                         uint32_t *out_flags)
412 {
413         struct rpcint_bh_raw_call_state *state =
414                 tevent_req_data(req,
415                 struct rpcint_bh_raw_call_state);
416         NTSTATUS status;
417
418         if (tevent_req_is_nterror(req, &status)) {
419                 tevent_req_received(req);
420                 return status;
421         }
422
423         *out_data = talloc_move(mem_ctx, &state->out_data.data);
424         *out_length = state->out_data.length;
425         *out_flags = 0;
426         tevent_req_received(req);
427         return NT_STATUS_OK;
428 }
429
430 struct rpcint_bh_disconnect_state {
431         uint8_t _dummy;
432 };
433
434 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
435                                                 struct tevent_context *ev,
436                                                 struct dcerpc_binding_handle *h)
437 {
438         struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
439                                      struct rpcint_bh_state);
440         struct tevent_req *req;
441         struct rpcint_bh_disconnect_state *state;
442         bool ok;
443
444         req = tevent_req_create(mem_ctx, &state,
445                                 struct rpcint_bh_disconnect_state);
446         if (req == NULL) {
447                 return NULL;
448         }
449
450         ok = rpcint_bh_is_connected(h);
451         if (!ok) {
452                 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
453                 return tevent_req_post(req, ev);
454         }
455
456         /*
457          * TODO: do a real async disconnect ...
458          *
459          * For now the caller needs to free pipes_struct
460          */
461         hs->p = NULL;
462
463         tevent_req_done(req);
464         return tevent_req_post(req, ev);
465 }
466
467 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
468 {
469         NTSTATUS status;
470
471         if (tevent_req_is_nterror(req, &status)) {
472                 tevent_req_received(req);
473                 return status;
474         }
475
476         tevent_req_received(req);
477         return NT_STATUS_OK;
478 }
479
480 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
481 {
482         return true;
483 }
484
485 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
486                                    int ndr_flags,
487                                    const void *_struct_ptr,
488                                    const struct ndr_interface_call *call)
489 {
490         void *struct_ptr = discard_const(_struct_ptr);
491
492         if (DEBUGLEVEL < 11) {
493                 return;
494         }
495
496         if (ndr_flags & NDR_IN) {
497                 ndr_print_function_debug(call->ndr_print,
498                                          call->name,
499                                          ndr_flags,
500                                          struct_ptr);
501         }
502         if (ndr_flags & NDR_OUT) {
503                 ndr_print_function_debug(call->ndr_print,
504                                          call->name,
505                                          ndr_flags,
506                                          struct_ptr);
507         }
508 }
509
510 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
511         .name                   = "rpcint",
512         .is_connected           = rpcint_bh_is_connected,
513         .set_timeout            = rpcint_bh_set_timeout,
514         .raw_call_send          = rpcint_bh_raw_call_send,
515         .raw_call_recv          = rpcint_bh_raw_call_recv,
516         .disconnect_send        = rpcint_bh_disconnect_send,
517         .disconnect_recv        = rpcint_bh_disconnect_recv,
518
519         .ref_alloc              = rpcint_bh_ref_alloc,
520         .do_ndr_print           = rpcint_bh_do_ndr_print,
521 };
522
523 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
524                         const struct ndr_syntax_id *abstract_syntax,
525                         const struct ndr_interface_table *ndr_table,
526                         const struct tsocket_address *remote_address,
527                         const struct tsocket_address *local_address,
528                         const struct auth_session_info *session_info,
529                         struct messaging_context *msg_ctx,
530                         struct dcerpc_binding_handle **binding_handle)
531 {
532         struct dcerpc_binding_handle *h;
533         struct rpcint_bh_state *hs;
534         NTSTATUS status;
535
536         if (ndr_table) {
537                 abstract_syntax = &ndr_table->syntax_id;
538         }
539
540         h = dcerpc_binding_handle_create(mem_ctx,
541                                          &rpcint_bh_ops,
542                                          NULL,
543                                          ndr_table,
544                                          &hs,
545                                          struct rpcint_bh_state,
546                                          __location__);
547         if (h == NULL) {
548                 return NT_STATUS_NO_MEMORY;
549         }
550
551         status = make_internal_rpc_pipe_p(hs,
552                                           abstract_syntax,
553                                           remote_address,
554                                           local_address,
555                                           session_info,
556                                           msg_ctx,
557                                           &hs->p);
558         if (!NT_STATUS_IS_OK(status)) {
559                 TALLOC_FREE(h);
560                 return status;
561         }
562
563         *binding_handle = h;
564         return NT_STATUS_OK;
565 }
566 /**
567  * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
568  *
569  * @param[in]  mem_ctx  The memory context to use.
570  *
571  * @param[in]  ndr_table Normally the ndr_table_<name>.
572  *
573  * @param[in]  remote_address The info about the connected client.
574  *
575  * @param[in]  serversupplied_info The server supplied authentication function.
576  *
577  * @param[in]  msg_ctx   The messaging context that can be used by the server
578  *
579  * @param[out] binding_handle  A pointer to store the connected
580  *                             dcerpc_binding_handle
581  *
582  * @return              NT_STATUS_OK on success, a corresponding NT status if an
583  *                      error occurred.
584  *
585  * @code
586  *   struct dcerpc_binding_handle *winreg_binding;
587  *   NTSTATUS status;
588  *
589  *   status = rpcint_binding_handle(tmp_ctx,
590  *                                  &ndr_table_winreg,
591  *                                  p->remote_address,
592  *                                  p->session_info,
593  *                                  p->msg_ctx
594  *                                  &winreg_binding);
595  * @endcode
596  */
597 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
598                                const struct ndr_interface_table *ndr_table,
599                                const struct tsocket_address *remote_address,
600                                const struct tsocket_address *local_address,
601                                const struct auth_session_info *session_info,
602                                struct messaging_context *msg_ctx,
603                                struct dcerpc_binding_handle **binding_handle)
604 {
605         return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
606                                         local_address, session_info,
607                                         msg_ctx, binding_handle);
608 }
609
610 /**
611  * @internal
612  *
613  * @brief Create a new RPC client context which uses a local transport.
614  *
615  * This creates a local transport. It is a shortcut to directly call the server
616  * functions and avoid marshalling.
617  * NOTE: this function should be used only by rpc_pipe_open_interface()
618  *
619  * @param[in]  mem_ctx  The memory context to use.
620  *
621  * @param[in]  ndr_table the ndr_table_<name> structure.
622  *
623  * @param[in]  serversupplied_info The server supplied authentication function.
624  *
625  * @param[in]  remote_address The client address information.
626  *
627  * @param[in]  msg_ctx  The messaging context to use.
628  *
629  * @param[out] presult  A pointer to store the connected rpc client pipe.
630  *
631  * @return              NT_STATUS_OK on success, a corresponding NT status if an
632  *                      error occurred.
633  */
634 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
635                                 const struct ndr_interface_table *ndr_table,
636                                 const struct auth_session_info *session_info,
637                                 const struct tsocket_address *remote_address,
638                                 const struct tsocket_address *local_address,
639                                 struct messaging_context *msg_ctx,
640                                 struct rpc_pipe_client **presult)
641 {
642         struct rpc_pipe_client *result;
643         NTSTATUS status;
644
645         result = talloc_zero(mem_ctx, struct rpc_pipe_client);
646         if (result == NULL) {
647                 return NT_STATUS_NO_MEMORY;
648         }
649
650         result->abstract_syntax = ndr_table->syntax_id;
651         result->transfer_syntax = ndr_transfer_syntax_ndr;
652
653         if (remote_address == NULL) {
654                 struct tsocket_address *local;
655                 int rc;
656
657                 rc = tsocket_address_inet_from_strings(mem_ctx,
658                                                        "ip",
659                                                        "127.0.0.1",
660                                                        0,
661                                                        &local);
662                 if (rc < 0) {
663                         TALLOC_FREE(result);
664                         return NT_STATUS_NO_MEMORY;
665                 }
666
667                 remote_address = local;
668         }
669
670         result->max_xmit_frag = -1;
671
672         status = rpcint_binding_handle(result,
673                                        ndr_table,
674                                        remote_address,
675                                        local_address,
676                                        session_info,
677                                        msg_ctx,
678                                        &result->binding_handle);
679         if (!NT_STATUS_IS_OK(status)) {
680                 TALLOC_FREE(result);
681                 return status;
682         }
683
684         *presult = result;
685         return NT_STATUS_OK;
686 }
687
688 /****************************************************************************
689  * External pipes functions
690  ***************************************************************************/
691
692 NTSTATUS make_external_rpc_pipe(TALLOC_CTX *mem_ctx,
693                                 const char *pipe_name,
694                                 const struct tsocket_address *remote_client_address,
695                                 const struct tsocket_address *local_server_address,
696                                 const struct auth_session_info *session_info,
697                                 struct npa_state **pnpa)
698 {
699         TALLOC_CTX *tmp_ctx = talloc_stackframe();
700         struct auth_session_info_transport *session_info_t;
701         struct tevent_context *ev_ctx;
702         struct tevent_req *subreq;
703         const char *socket_np_dir;
704         const char *socket_dir;
705         struct npa_state *npa;
706         int sys_errno;
707         NTSTATUS status;
708         int rc = -1;
709         bool ok;
710
711         npa = npa_state_init(tmp_ctx);
712         if (npa == NULL) {
713                 status = NT_STATUS_NO_MEMORY;
714                 goto out;
715         }
716
717         socket_dir = lp_parm_const_string(GLOBAL_SECTION_SNUM,
718                                           "external_rpc_pipe",
719                                           "socket_dir",
720                                           lp_ncalrpc_dir());
721         if (socket_dir == NULL) {
722                 DEBUG(0, ("external_rpc_pipe: socket_dir not set\n"));
723                 status = NT_STATUS_PIPE_NOT_AVAILABLE;
724                 goto out;
725         }
726
727         socket_np_dir = talloc_asprintf(tmp_ctx, "%s/np", socket_dir);
728         if (socket_np_dir == NULL) {
729                 DEBUG(0, ("talloc_asprintf failed\n"));
730                 status = NT_STATUS_NO_MEMORY;
731                 goto out;
732         }
733
734         session_info_t = talloc_zero(tmp_ctx,
735                                      struct auth_session_info_transport);
736         if (session_info_t == NULL) {
737                 DEBUG(0, ("talloc failed\n"));
738                 status = NT_STATUS_NO_MEMORY;
739                 goto out;
740         }
741
742         session_info_t->session_info = copy_session_info(session_info_t,
743                                                          session_info);
744         if (session_info_t->session_info == NULL) {
745                 DEBUG(0, ("copy_session_info failed\n"));
746                 status = NT_STATUS_NO_MEMORY;
747                 goto out;
748         }
749
750         ev_ctx = samba_tevent_context_init(tmp_ctx);
751         if (ev_ctx == NULL) {
752                 DEBUG(0, ("samba_tevent_context_init failed\n"));
753                 status = NT_STATUS_NO_MEMORY;
754                 goto out;
755         }
756
757         become_root();
758         subreq = tstream_npa_connect_send(tmp_ctx,
759                                           ev_ctx,
760                                           socket_np_dir,
761                                           pipe_name,
762                                           remote_client_address,
763                                           NULL, /* client_name */
764                                           local_server_address,
765                                           NULL, /* server_name */
766                                           session_info_t);
767         if (subreq == NULL) {
768                 unbecome_root();
769                 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
770                           "user %s\\%s failed\n",
771                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
772                           session_info_t->session_info->info->account_name));
773                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
774                 goto out;
775         }
776         ok = tevent_req_poll(subreq, ev_ctx);
777         unbecome_root();
778         if (!ok) {
779                 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
780                           "failed for tstream_npa_connect: %s\n",
781                           socket_np_dir,
782                           pipe_name,
783                           session_info_t->session_info->info->domain_name,
784                           session_info_t->session_info->info->account_name,
785                           strerror(errno)));
786                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
787                 goto out;
788         }
789
790         rc = tstream_npa_connect_recv(subreq,
791                                       &sys_errno,
792                                       npa,
793                                       &npa->stream,
794                                       &npa->file_type,
795                                       &npa->device_state,
796                                       &npa->allocation_size);
797         talloc_free(subreq);
798         if (rc != 0) {
799                 int l = 1;
800
801                 if (errno == ENOENT) {
802                         l = 2;
803                 }
804
805                 DEBUG(l, ("tstream_npa_connect_recv  to %s for pipe %s and "
806                           "user %s\\%s failed: %s\n",
807                           socket_np_dir,
808                           pipe_name,
809                           session_info_t->session_info->info->domain_name,
810                           session_info_t->session_info->info->account_name,
811                           strerror(sys_errno)));
812                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
813                 goto out;
814         }
815
816         *pnpa = talloc_steal(mem_ctx, npa);
817         status = NT_STATUS_OK;
818 out:
819         talloc_free(tmp_ctx);
820
821         return status;
822 }
823
824 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
825                                 const char *pipe_name,
826                                 const struct tsocket_address *remote_client_address,
827                                 const struct tsocket_address *local_server_address,
828                                 const struct auth_session_info *session_info)
829 {
830         struct np_proxy_state *result;
831         char *socket_np_dir;
832         const char *socket_dir;
833         struct tevent_context *ev;
834         struct tevent_req *subreq;
835         struct auth_session_info_transport *session_info_t;
836         bool ok;
837         int ret;
838         int sys_errno;
839
840         result = talloc(mem_ctx, struct np_proxy_state);
841         if (result == NULL) {
842                 DEBUG(0, ("talloc failed\n"));
843                 return NULL;
844         }
845
846         result->read_queue = tevent_queue_create(result, "np_read");
847         if (result->read_queue == NULL) {
848                 DEBUG(0, ("tevent_queue_create failed\n"));
849                 goto fail;
850         }
851
852         result->write_queue = tevent_queue_create(result, "np_write");
853         if (result->write_queue == NULL) {
854                 DEBUG(0, ("tevent_queue_create failed\n"));
855                 goto fail;
856         }
857
858         ev = samba_tevent_context_init(talloc_tos());
859         if (ev == NULL) {
860                 DEBUG(0, ("samba_tevent_context_init failed\n"));
861                 goto fail;
862         }
863
864         socket_dir = lp_parm_const_string(
865                 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
866                 lp_ncalrpc_dir());
867         if (socket_dir == NULL) {
868                 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
869                 goto fail;
870         }
871         socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
872         if (socket_np_dir == NULL) {
873                 DEBUG(0, ("talloc_asprintf failed\n"));
874                 goto fail;
875         }
876
877         session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
878         if (session_info_t == NULL) {
879                 DEBUG(0, ("talloc failed\n"));
880                 goto fail;
881         }
882
883         session_info_t->session_info = copy_session_info(session_info_t,
884                                                          session_info);
885         if (session_info_t->session_info == NULL) {
886                 DEBUG(0, ("copy_session_info failed\n"));
887                 goto fail;
888         }
889
890         become_root();
891         subreq = tstream_npa_connect_send(talloc_tos(), ev,
892                                           socket_np_dir,
893                                           pipe_name,
894                                           remote_client_address,
895                                           NULL, /* client_name */
896                                           local_server_address,
897                                           NULL, /* server_name */
898                                           session_info_t);
899         if (subreq == NULL) {
900                 unbecome_root();
901                 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
902                           "user %s\\%s failed\n",
903                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
904                           session_info_t->session_info->info->account_name));
905                 goto fail;
906         }
907         ok = tevent_req_poll(subreq, ev);
908         unbecome_root();
909         if (!ok) {
910                 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
911                           "failed for tstream_npa_connect: %s\n",
912                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
913                           session_info_t->session_info->info->account_name,
914                           strerror(errno)));
915                 goto fail;
916
917         }
918         ret = tstream_npa_connect_recv(subreq, &sys_errno,
919                                        result,
920                                        &result->npipe,
921                                        &result->file_type,
922                                        &result->device_state,
923                                        &result->allocation_size);
924         TALLOC_FREE(subreq);
925         if (ret != 0) {
926                 int l = 1;
927                 if (errno == ENOENT) {
928                         l = 2;
929                 }
930                 DEBUG(l, ("tstream_npa_connect_recv  to %s for pipe %s and "
931                           "user %s\\%s failed: %s\n",
932                           socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
933                           session_info_t->session_info->info->account_name,
934                           strerror(sys_errno)));
935                 goto fail;
936         }
937
938         return result;
939
940  fail:
941         TALLOC_FREE(result);
942         return NULL;
943 }
944
945 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
946                                        const char *pipe_name,
947                                        const struct ndr_interface_table *table,
948                                        const struct auth_session_info *session_info,
949                                        const struct tsocket_address *remote_client_address,
950                                        const struct tsocket_address *local_server_address,
951                                        struct rpc_pipe_client **_result)
952 {
953         struct rpc_pipe_client *result = NULL;
954         struct np_proxy_state *proxy_state = NULL;
955         struct pipe_auth_data *auth;
956         struct tsocket_address *remote_client_addr;
957         struct tsocket_address *local_server_addr;
958         NTSTATUS status;
959         int ret;
960
961         if (local_server_address == NULL) {
962                 /* this is an internal connection, fake up ip addresses */
963                 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
964                                                         NULL, 0, &local_server_addr);
965                 if (ret) {
966                         return NT_STATUS_NO_MEMORY;
967                 }
968                 local_server_address = local_server_addr;
969         }
970
971         if (remote_client_address == NULL) {
972                 /* this is an internal connection, fake up ip addresses */
973                 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
974                                                         NULL, 0, &remote_client_addr);
975                 if (ret) {
976                         return NT_STATUS_NO_MEMORY;
977                 }
978                 remote_client_address = remote_client_addr;
979         }
980
981         proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
982                                                remote_client_address,
983                                                local_server_address,
984                                                session_info);
985         if (!proxy_state) {
986                 DEBUG(1, ("Unable to make proxy_state for connection to %s.\n", pipe_name));
987                 return NT_STATUS_UNSUCCESSFUL;
988         }
989
990         result = talloc_zero(mem_ctx, struct rpc_pipe_client);
991         if (result == NULL) {
992                 status = NT_STATUS_NO_MEMORY;
993                 goto done;
994         }
995
996         result->abstract_syntax = table->syntax_id;
997         result->transfer_syntax = ndr_transfer_syntax_ndr;
998
999         result->desthost = get_myname(result);
1000         result->srv_name_slash = talloc_asprintf_strupper_m(
1001                 result, "\\\\%s", result->desthost);
1002         if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
1003                 status = NT_STATUS_NO_MEMORY;
1004                 goto done;
1005         }
1006
1007         result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1008
1009         status = rpc_transport_tstream_init(result,
1010                                             &proxy_state->npipe,
1011                                             &result->transport);
1012         if (!NT_STATUS_IS_OK(status)) {
1013                 goto done;
1014         }
1015
1016         result->binding_handle = rpccli_bh_create(result, NULL, table);
1017         if (result->binding_handle == NULL) {
1018                 status = NT_STATUS_NO_MEMORY;
1019                 DEBUG(0, ("Failed to create binding handle.\n"));
1020                 goto done;
1021         }
1022
1023         result->auth = talloc_zero(result, struct pipe_auth_data);
1024         if (!result->auth) {
1025                 status = NT_STATUS_NO_MEMORY;
1026                 goto done;
1027         }
1028         result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
1029         result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
1030         result->auth->auth_context_id = 0;
1031
1032         status = rpccli_anon_bind_data(result, &auth);
1033         if (!NT_STATUS_IS_OK(status)) {
1034                 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
1035                 goto done;
1036         }
1037
1038         status = rpc_pipe_bind(result, auth);
1039         if (!NT_STATUS_IS_OK(status)) {
1040                 DEBUG(0, ("Failed to bind external pipe.\n"));
1041                 goto done;
1042         }
1043
1044 done:
1045         if (!NT_STATUS_IS_OK(status)) {
1046                 TALLOC_FREE(result);
1047         }
1048         TALLOC_FREE(proxy_state);
1049         *_result = result;
1050         return status;
1051 }
1052
1053 /**
1054  * @brief Create a new RPC client context which uses a local dispatch function
1055  *        or a remote transport, depending on rpc_server configuration for the
1056  *        specific service.
1057  *
1058  * @param[in]  mem_ctx  The memory context to use.
1059  *
1060  * @param[in]  abstract_syntax Normally the syntax_id of the autogenerated
1061  *                             ndr_table_<name>.
1062  *
1063  * @param[in]  serversupplied_info The server supplied authentication function.
1064  *
1065  * @param[in]  remote_address The client address information.
1066  *
1067  * @param[in]  msg_ctx  The messaging context to use.
1068  *
1069  * @param[out] presult  A pointer to store the connected rpc client pipe.
1070  *
1071  * @return              NT_STATUS_OK on success, a corresponding NT status if an
1072  *                      error occurred.
1073  *
1074  * @code
1075  *   struct rpc_pipe_client *winreg_pipe;
1076  *   NTSTATUS status;
1077  *
1078  *   status = rpc_pipe_open_interface(tmp_ctx,
1079  *                                    &ndr_table_winreg.syntax_id,
1080  *                                    p->session_info,
1081  *                                    remote_address,
1082  *                                    &winreg_pipe);
1083  * @endcode
1084  */
1085
1086 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
1087                                  const struct ndr_interface_table *table,
1088                                  const struct auth_session_info *session_info,
1089                                  const struct tsocket_address *remote_address,
1090                                  const struct tsocket_address *local_address,
1091                                  struct messaging_context *msg_ctx,
1092                                  struct rpc_pipe_client **cli_pipe)
1093 {
1094         struct rpc_pipe_client *cli = NULL;
1095         enum rpc_service_mode_e pipe_mode;
1096         const char *pipe_name;
1097         NTSTATUS status;
1098         TALLOC_CTX *tmp_ctx;
1099
1100         if (cli_pipe != NULL) {
1101                 if (rpccli_is_connected(*cli_pipe)) {
1102                         return NT_STATUS_OK;
1103                 } else {
1104                         TALLOC_FREE(*cli_pipe);
1105                 }
1106         }
1107
1108         tmp_ctx = talloc_stackframe();
1109         if (tmp_ctx == NULL) {
1110                 return NT_STATUS_NO_MEMORY;
1111         }
1112
1113         pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
1114         if (pipe_name == NULL) {
1115                 DEBUG(1, ("Unable to find pipe name to forward %s to.\n", table->name));
1116                 status = NT_STATUS_INVALID_PARAMETER;
1117                 goto done;
1118         }
1119
1120         while (pipe_name[0] == '\\') {
1121                 pipe_name++;
1122         }
1123
1124         DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
1125
1126         pipe_mode = rpc_service_mode(pipe_name);
1127
1128         switch (pipe_mode) {
1129         case RPC_SERVICE_MODE_EMBEDDED:
1130                 status = rpc_pipe_open_internal(tmp_ctx,
1131                                                 table, session_info,
1132                                                 remote_address, local_address,
1133                                                 msg_ctx,
1134                                                 &cli);
1135                 if (!NT_STATUS_IS_OK(status)) {
1136                         goto done;
1137                 }
1138                 break;
1139         case RPC_SERVICE_MODE_EXTERNAL:
1140                 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
1141                  * for now we need to use the special proxy setup to connect
1142                  * to spoolssd. */
1143
1144                 status = rpc_pipe_open_external(tmp_ctx,
1145                                                 pipe_name, table,
1146                                                 session_info,
1147                                                 remote_address, local_address,
1148                                                 &cli);
1149                 if (!NT_STATUS_IS_OK(status)) {
1150                         goto done;
1151                 }
1152                 break;
1153         case RPC_SERVICE_MODE_DISABLED:
1154                 status = NT_STATUS_NOT_IMPLEMENTED;
1155                 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
1156                           pipe_name, nt_errstr(status)));
1157                 goto done;
1158         }
1159
1160         status = NT_STATUS_OK;
1161 done:
1162         if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
1163                 *cli_pipe = talloc_move(mem_ctx, &cli);
1164         }
1165         TALLOC_FREE(tmp_ctx);
1166         return status;
1167 }