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