s3-rpc_server: pass remote and local address to rpc_pipe_open_external
[Samba.git] / source3 / rpc_server / rpc_ncacn_np.c
blobfe55be55b8e8b07edfe4b3dd328b589af5b5dc3d
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
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.
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.
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/>.
24 #include "includes.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "rpc_server/srv_pipe_internal.h"
27 #include "rpc_dce.h"
28 #include "../libcli/named_pipe_auth/npa_tstream.h"
29 #include "rpc_server/rpc_ncacn_np.h"
30 #include "librpc/gen_ndr/netlogon.h"
31 #include "librpc/gen_ndr/auth.h"
32 #include "../auth/auth_sam_reply.h"
33 #include "auth.h"
34 #include "rpc_server/rpc_pipes.h"
35 #include "../lib/tsocket/tsocket.h"
36 #include "../lib/util/tevent_ntstatus.h"
37 #include "rpc_contexts.h"
38 #include "rpc_server/rpc_config.h"
39 #include "librpc/ndr/ndr_table.h"
40 #include "rpc_server/rpc_server.h"
42 #undef DBGC_CLASS
43 #define DBGC_CLASS DBGC_RPC_SRV
45 static struct npa_state *npa_state_init(TALLOC_CTX *mem_ctx)
47 struct npa_state *npa;
49 npa = talloc_zero(mem_ctx, struct npa_state);
50 if (npa == NULL) {
51 return NULL;
54 npa->read_queue = tevent_queue_create(npa, "npa_cli_read");
55 if (npa->read_queue == NULL) {
56 DEBUG(0, ("tevent_queue_create failed\n"));
57 goto fail;
60 npa->write_queue = tevent_queue_create(npa, "npa_cli_write");
61 if (npa->write_queue == NULL) {
62 DEBUG(0, ("tevent_queue_create failed\n"));
63 goto fail;
66 return npa;
67 fail:
68 talloc_free(npa);
69 return NULL;
72 NTSTATUS make_internal_rpc_pipe_socketpair(TALLOC_CTX *mem_ctx,
73 struct tevent_context *ev_ctx,
74 struct messaging_context *msg_ctx,
75 const char *pipe_name,
76 const struct ndr_syntax_id *syntax,
77 const struct tsocket_address *remote_address,
78 const struct auth_session_info *session_info,
79 struct npa_state **pnpa)
81 TALLOC_CTX *tmp_ctx = talloc_stackframe();
82 struct named_pipe_client *npc;
83 struct tevent_req *subreq;
84 struct npa_state *npa;
85 NTSTATUS status;
86 int error;
87 int rc;
89 DEBUG(4, ("Create of internal pipe %s requested\n", pipe_name));
91 npa = npa_state_init(tmp_ctx);
92 if (npa == NULL) {
93 status = NT_STATUS_NO_MEMORY;
94 goto out;
97 npa->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
98 npa->device_state = 0xff | 0x0400 | 0x0100;
99 npa->allocation_size = 4096;
101 npc = named_pipe_client_init(npa,
102 ev_ctx,
103 msg_ctx,
104 pipe_name,
105 NULL, /* term_fn */
106 npa->file_type,
107 npa->device_state,
108 npa->allocation_size,
109 NULL); /* private_data */
110 if (npc == NULL) {
111 status = NT_STATUS_NO_MEMORY;
112 goto out;
114 npa->private_data = (void*) npc;
116 rc = tstream_npa_socketpair(npa->file_type,
117 npa,
118 &npa->stream,
119 npc,
120 &npc->tstream);
121 if (rc == -1) {
122 status = map_nt_error_from_unix(errno);
123 goto out;
126 npc->remote_client_addr = tsocket_address_copy(remote_address, npc);
127 if (npc->remote_client_addr == NULL) {
128 status = NT_STATUS_NO_MEMORY;
129 goto out;
132 npc->remote_client_name = tsocket_address_inet_addr_string(npc->remote_client_addr,
133 npc);
134 if (npc->remote_client_name == NULL) {
135 status = NT_STATUS_NO_MEMORY;
136 goto out;
139 npc->session_info = copy_session_info(npc, session_info);
140 if (npc->session_info == NULL) {
141 status = NT_STATUS_NO_MEMORY;
142 goto out;
145 rc = make_server_pipes_struct(npc,
146 npc->msg_ctx,
147 npc->pipe_name,
148 NCACN_NP,
149 npc->remote_client_addr,
150 npc->local_server_addr,
151 npc->session_info,
152 &npc->p,
153 &error);
154 if (rc == -1) {
155 status = map_nt_error_from_unix(error);
156 goto out;
159 npc->write_queue = tevent_queue_create(npc, "npa_server_write_queue");
160 if (npc->write_queue == NULL) {
161 status = NT_STATUS_NO_MEMORY;
162 goto out;
165 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
166 if (subreq == NULL) {
167 DEBUG(2, ("Failed to start receving packets\n"));
168 status = NT_STATUS_PIPE_BROKEN;
169 goto out;
171 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
173 *pnpa = talloc_steal(mem_ctx, npa);
174 status = NT_STATUS_OK;
175 out:
176 talloc_free(tmp_ctx);
177 return status;
180 /****************************************************************************
181 Make an internal namedpipes structure
182 ****************************************************************************/
184 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
185 const struct ndr_syntax_id *syntax,
186 const struct tsocket_address *remote_address,
187 const struct tsocket_address *local_address,
188 const struct auth_session_info *session_info,
189 struct messaging_context *msg_ctx)
191 struct pipes_struct *p;
192 struct pipe_rpc_fns *context_fns;
193 const char *pipe_name;
194 int ret;
195 const struct ndr_interface_table *table;
197 table = ndr_table_by_uuid(&syntax->uuid);
198 if (table == NULL) {
199 DEBUG(0,("unknown interface\n"));
200 return NULL;
203 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
205 DEBUG(4,("Create pipe requested %s\n", pipe_name));
207 ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
208 NCALRPC, RPC_LITTLE_ENDIAN,
209 remote_address, local_address, &p);
210 if (ret) {
211 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
212 return NULL;
215 if (!init_pipe_handles(p, syntax)) {
216 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
217 TALLOC_FREE(p);
218 return NULL;
221 p->session_info = copy_session_info(p, session_info);
222 if (p->session_info == NULL) {
223 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
224 close_policy_by_pipe(p);
225 TALLOC_FREE(p);
226 return NULL;
229 context_fns = talloc_zero(p, struct pipe_rpc_fns);
230 if (context_fns == NULL) {
231 DEBUG(0,("talloc() failed!\n"));
232 TALLOC_FREE(p);
233 return NULL;
236 context_fns->next = context_fns->prev = NULL;
237 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
238 context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
239 context_fns->context_id = 0;
240 context_fns->syntax = *syntax;
242 /* add to the list of open contexts */
243 DLIST_ADD(p->contexts, context_fns);
245 DEBUG(4,("Created internal pipe %s\n", pipe_name));
247 return p;
250 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
251 TALLOC_CTX *mem_ctx,
252 uint32_t opnum,
253 const DATA_BLOB *in_data,
254 DATA_BLOB *out_data)
256 struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
257 uint32_t num_cmds = fns->n_cmds;
258 const struct api_struct *cmds = fns->cmds;
259 uint32_t i;
260 bool ok;
262 /* set opnum */
263 p->opnum = opnum;
265 for (i = 0; i < num_cmds; i++) {
266 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
267 break;
271 if (i == num_cmds) {
272 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
275 p->in_data.data = *in_data;
276 p->out_data.rdata = data_blob_null;
278 ok = cmds[i].fn(p);
279 p->in_data.data = data_blob_null;
280 if (!ok) {
281 data_blob_free(&p->out_data.rdata);
282 talloc_free_children(p->mem_ctx);
283 return NT_STATUS_RPC_CALL_FAILED;
286 if (p->fault_state) {
287 NTSTATUS status;
289 status = NT_STATUS(p->fault_state);
290 p->fault_state = 0;
291 data_blob_free(&p->out_data.rdata);
292 talloc_free_children(p->mem_ctx);
293 return status;
296 *out_data = p->out_data.rdata;
297 talloc_steal(mem_ctx, out_data->data);
298 p->out_data.rdata = data_blob_null;
300 talloc_free_children(p->mem_ctx);
301 return NT_STATUS_OK;
304 struct rpcint_bh_state {
305 struct pipes_struct *p;
308 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
310 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
311 struct rpcint_bh_state);
313 if (!hs->p) {
314 return false;
317 return true;
320 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
321 uint32_t timeout)
323 /* TODO: implement timeouts */
324 return UINT32_MAX;
327 struct rpcint_bh_raw_call_state {
328 DATA_BLOB in_data;
329 DATA_BLOB out_data;
330 uint32_t out_flags;
333 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
334 struct tevent_context *ev,
335 struct dcerpc_binding_handle *h,
336 const struct GUID *object,
337 uint32_t opnum,
338 uint32_t in_flags,
339 const uint8_t *in_data,
340 size_t in_length)
342 struct rpcint_bh_state *hs =
343 dcerpc_binding_handle_data(h,
344 struct rpcint_bh_state);
345 struct tevent_req *req;
346 struct rpcint_bh_raw_call_state *state;
347 bool ok;
348 NTSTATUS status;
350 req = tevent_req_create(mem_ctx, &state,
351 struct rpcint_bh_raw_call_state);
352 if (req == NULL) {
353 return NULL;
355 state->in_data.data = discard_const_p(uint8_t, in_data);
356 state->in_data.length = in_length;
358 ok = rpcint_bh_is_connected(h);
359 if (!ok) {
360 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
361 return tevent_req_post(req, ev);
364 /* TODO: allow async */
365 status = rpcint_dispatch(hs->p, state, opnum,
366 &state->in_data,
367 &state->out_data);
368 if (!NT_STATUS_IS_OK(status)) {
369 tevent_req_nterror(req, status);
370 return tevent_req_post(req, ev);
373 tevent_req_done(req);
374 return tevent_req_post(req, ev);
377 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
378 TALLOC_CTX *mem_ctx,
379 uint8_t **out_data,
380 size_t *out_length,
381 uint32_t *out_flags)
383 struct rpcint_bh_raw_call_state *state =
384 tevent_req_data(req,
385 struct rpcint_bh_raw_call_state);
386 NTSTATUS status;
388 if (tevent_req_is_nterror(req, &status)) {
389 tevent_req_received(req);
390 return status;
393 *out_data = talloc_move(mem_ctx, &state->out_data.data);
394 *out_length = state->out_data.length;
395 *out_flags = 0;
396 tevent_req_received(req);
397 return NT_STATUS_OK;
400 struct rpcint_bh_disconnect_state {
401 uint8_t _dummy;
404 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
405 struct tevent_context *ev,
406 struct dcerpc_binding_handle *h)
408 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
409 struct rpcint_bh_state);
410 struct tevent_req *req;
411 struct rpcint_bh_disconnect_state *state;
412 bool ok;
414 req = tevent_req_create(mem_ctx, &state,
415 struct rpcint_bh_disconnect_state);
416 if (req == NULL) {
417 return NULL;
420 ok = rpcint_bh_is_connected(h);
421 if (!ok) {
422 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
423 return tevent_req_post(req, ev);
427 * TODO: do a real async disconnect ...
429 * For now the caller needs to free pipes_struct
431 hs->p = NULL;
433 tevent_req_done(req);
434 return tevent_req_post(req, ev);
437 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
439 NTSTATUS status;
441 if (tevent_req_is_nterror(req, &status)) {
442 tevent_req_received(req);
443 return status;
446 tevent_req_received(req);
447 return NT_STATUS_OK;
450 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
452 return true;
455 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
456 int ndr_flags,
457 const void *_struct_ptr,
458 const struct ndr_interface_call *call)
460 void *struct_ptr = discard_const(_struct_ptr);
462 if (DEBUGLEVEL < 11) {
463 return;
466 if (ndr_flags & NDR_IN) {
467 ndr_print_function_debug(call->ndr_print,
468 call->name,
469 ndr_flags,
470 struct_ptr);
472 if (ndr_flags & NDR_OUT) {
473 ndr_print_function_debug(call->ndr_print,
474 call->name,
475 ndr_flags,
476 struct_ptr);
480 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
481 .name = "rpcint",
482 .is_connected = rpcint_bh_is_connected,
483 .set_timeout = rpcint_bh_set_timeout,
484 .raw_call_send = rpcint_bh_raw_call_send,
485 .raw_call_recv = rpcint_bh_raw_call_recv,
486 .disconnect_send = rpcint_bh_disconnect_send,
487 .disconnect_recv = rpcint_bh_disconnect_recv,
489 .ref_alloc = rpcint_bh_ref_alloc,
490 .do_ndr_print = rpcint_bh_do_ndr_print,
493 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
494 const struct ndr_syntax_id *abstract_syntax,
495 const struct ndr_interface_table *ndr_table,
496 const struct tsocket_address *remote_address,
497 const struct tsocket_address *local_address,
498 const struct auth_session_info *session_info,
499 struct messaging_context *msg_ctx,
500 struct dcerpc_binding_handle **binding_handle)
502 struct dcerpc_binding_handle *h;
503 struct rpcint_bh_state *hs;
505 if (ndr_table) {
506 abstract_syntax = &ndr_table->syntax_id;
509 h = dcerpc_binding_handle_create(mem_ctx,
510 &rpcint_bh_ops,
511 NULL,
512 ndr_table,
513 &hs,
514 struct rpcint_bh_state,
515 __location__);
516 if (h == NULL) {
517 return NT_STATUS_NO_MEMORY;
519 hs->p = make_internal_rpc_pipe_p(hs,
520 abstract_syntax,
521 remote_address,
522 local_address,
523 session_info,
524 msg_ctx);
525 if (hs->p == NULL) {
526 TALLOC_FREE(h);
527 return NT_STATUS_NO_MEMORY;
530 *binding_handle = h;
531 return NT_STATUS_OK;
534 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
536 * @param[in] mem_ctx The memory context to use.
538 * @param[in] ndr_table Normally the ndr_table_<name>.
540 * @param[in] remote_address The info about the connected client.
542 * @param[in] serversupplied_info The server supplied authentication function.
544 * @param[in] msg_ctx The messaging context that can be used by the server
546 * @param[out] binding_handle A pointer to store the connected
547 * dcerpc_binding_handle
549 * @return NT_STATUS_OK on success, a corresponding NT status if an
550 * error occurred.
552 * @code
553 * struct dcerpc_binding_handle *winreg_binding;
554 * NTSTATUS status;
556 * status = rpcint_binding_handle(tmp_ctx,
557 * &ndr_table_winreg,
558 * p->remote_address,
559 * p->session_info,
560 * p->msg_ctx
561 * &winreg_binding);
562 * @endcode
564 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
565 const struct ndr_interface_table *ndr_table,
566 const struct tsocket_address *remote_address,
567 const struct tsocket_address *local_address,
568 const struct auth_session_info *session_info,
569 struct messaging_context *msg_ctx,
570 struct dcerpc_binding_handle **binding_handle)
572 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
573 local_address, session_info,
574 msg_ctx, binding_handle);
578 * @internal
580 * @brief Create a new RPC client context which uses a local transport.
582 * This creates a local transport. It is a shortcut to directly call the server
583 * functions and avoid marshalling.
584 * NOTE: this function should be used only by rpc_pipe_open_interface()
586 * @param[in] mem_ctx The memory context to use.
588 * @param[in] ndr_table the ndr_table_<name> structure.
590 * @param[in] serversupplied_info The server supplied authentication function.
592 * @param[in] remote_address The client address information.
594 * @param[in] msg_ctx The messaging context to use.
596 * @param[out] presult A pointer to store the connected rpc client pipe.
598 * @return NT_STATUS_OK on success, a corresponding NT status if an
599 * error occurred.
601 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
602 const struct ndr_interface_table *ndr_table,
603 const struct auth_session_info *session_info,
604 const struct tsocket_address *remote_address,
605 const struct tsocket_address *local_address,
606 struct messaging_context *msg_ctx,
607 struct rpc_pipe_client **presult)
609 struct rpc_pipe_client *result;
610 NTSTATUS status;
612 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
613 if (result == NULL) {
614 return NT_STATUS_NO_MEMORY;
617 result->abstract_syntax = ndr_table->syntax_id;
618 result->transfer_syntax = ndr_transfer_syntax_ndr;
620 if (remote_address == NULL) {
621 struct tsocket_address *local;
622 int rc;
624 rc = tsocket_address_inet_from_strings(mem_ctx,
625 "ip",
626 "127.0.0.1",
628 &local);
629 if (rc < 0) {
630 TALLOC_FREE(result);
631 return NT_STATUS_NO_MEMORY;
634 remote_address = local;
637 result->max_xmit_frag = -1;
639 status = rpcint_binding_handle(result,
640 ndr_table,
641 remote_address,
642 local_address,
643 session_info,
644 msg_ctx,
645 &result->binding_handle);
646 if (!NT_STATUS_IS_OK(status)) {
647 TALLOC_FREE(result);
648 return status;
651 *presult = result;
652 return NT_STATUS_OK;
655 /****************************************************************************
656 * External pipes functions
657 ***************************************************************************/
659 NTSTATUS make_external_rpc_pipe(TALLOC_CTX *mem_ctx,
660 const char *pipe_name,
661 const struct tsocket_address *local_address,
662 const struct tsocket_address *remote_address,
663 const struct auth_session_info *session_info,
664 struct npa_state **pnpa)
666 TALLOC_CTX *tmp_ctx = talloc_stackframe();
667 struct auth_session_info_transport *session_info_t;
668 struct tevent_context *ev_ctx;
669 struct tevent_req *subreq;
670 const char *socket_np_dir;
671 const char *socket_dir;
672 struct npa_state *npa;
673 int sys_errno;
674 NTSTATUS status;
675 int rc = -1;
676 bool ok;
678 npa = npa_state_init(tmp_ctx);
679 if (npa == NULL) {
680 status = NT_STATUS_NO_MEMORY;
681 goto out;
684 socket_dir = lp_parm_const_string(GLOBAL_SECTION_SNUM,
685 "external_rpc_pipe",
686 "socket_dir",
687 lp_ncalrpc_dir());
688 if (socket_dir == NULL) {
689 DEBUG(0, ("external_rpc_pipe: socket_dir not set\n"));
690 status = NT_STATUS_PIPE_NOT_AVAILABLE;
691 goto out;
694 socket_np_dir = talloc_asprintf(tmp_ctx, "%s/np", socket_dir);
695 if (socket_np_dir == NULL) {
696 DEBUG(0, ("talloc_asprintf failed\n"));
697 status = NT_STATUS_NO_MEMORY;
698 goto out;
701 session_info_t = talloc_zero(tmp_ctx,
702 struct auth_session_info_transport);
703 if (session_info_t == NULL) {
704 DEBUG(0, ("talloc failed\n"));
705 status = NT_STATUS_NO_MEMORY;
706 goto out;
709 session_info_t->session_info = copy_session_info(session_info_t,
710 session_info);
711 if (session_info_t->session_info == NULL) {
712 DEBUG(0, ("copy_session_info failed\n"));
713 status = NT_STATUS_NO_MEMORY;
714 goto out;
717 ev_ctx = samba_tevent_context_init(tmp_ctx);
718 if (ev_ctx == NULL) {
719 DEBUG(0, ("samba_tevent_context_init failed\n"));
720 status = NT_STATUS_NO_MEMORY;
721 goto out;
724 become_root();
725 subreq = tstream_npa_connect_send(tmp_ctx,
726 ev_ctx,
727 socket_np_dir,
728 pipe_name,
729 remote_address, /* client_addr */
730 NULL, /* client_name */
731 local_address, /* server_addr */
732 NULL, /* server_name */
733 session_info_t);
734 if (subreq == NULL) {
735 unbecome_root();
736 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
737 "user %s\\%s failed\n",
738 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
739 session_info_t->session_info->info->account_name));
740 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
741 goto out;
743 ok = tevent_req_poll(subreq, ev_ctx);
744 unbecome_root();
745 if (!ok) {
746 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
747 "failed for tstream_npa_connect: %s\n",
748 socket_np_dir,
749 pipe_name,
750 session_info_t->session_info->info->domain_name,
751 session_info_t->session_info->info->account_name,
752 strerror(errno)));
753 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
754 goto out;
757 rc = tstream_npa_connect_recv(subreq,
758 &sys_errno,
759 npa,
760 &npa->stream,
761 &npa->file_type,
762 &npa->device_state,
763 &npa->allocation_size);
764 talloc_free(subreq);
765 if (rc != 0) {
766 int l = 1;
768 if (errno == ENOENT) {
769 l = 2;
772 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
773 "user %s\\%s failed: %s\n",
774 socket_np_dir,
775 pipe_name,
776 session_info_t->session_info->info->domain_name,
777 session_info_t->session_info->info->account_name,
778 strerror(sys_errno)));
779 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
780 goto out;
783 *pnpa = talloc_steal(mem_ctx, npa);
784 status = NT_STATUS_OK;
785 out:
786 talloc_free(tmp_ctx);
788 return status;
791 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
792 const char *pipe_name,
793 const struct tsocket_address *local_address,
794 const struct tsocket_address *remote_address,
795 const struct auth_session_info *session_info)
797 struct np_proxy_state *result;
798 char *socket_np_dir;
799 const char *socket_dir;
800 struct tevent_context *ev;
801 struct tevent_req *subreq;
802 struct auth_session_info_transport *session_info_t;
803 bool ok;
804 int ret;
805 int sys_errno;
807 result = talloc(mem_ctx, struct np_proxy_state);
808 if (result == NULL) {
809 DEBUG(0, ("talloc failed\n"));
810 return NULL;
813 result->read_queue = tevent_queue_create(result, "np_read");
814 if (result->read_queue == NULL) {
815 DEBUG(0, ("tevent_queue_create failed\n"));
816 goto fail;
819 result->write_queue = tevent_queue_create(result, "np_write");
820 if (result->write_queue == NULL) {
821 DEBUG(0, ("tevent_queue_create failed\n"));
822 goto fail;
825 ev = samba_tevent_context_init(talloc_tos());
826 if (ev == NULL) {
827 DEBUG(0, ("samba_tevent_context_init failed\n"));
828 goto fail;
831 socket_dir = lp_parm_const_string(
832 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
833 lp_ncalrpc_dir());
834 if (socket_dir == NULL) {
835 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
836 goto fail;
838 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
839 if (socket_np_dir == NULL) {
840 DEBUG(0, ("talloc_asprintf failed\n"));
841 goto fail;
844 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
845 if (session_info_t == NULL) {
846 DEBUG(0, ("talloc failed\n"));
847 goto fail;
850 session_info_t->session_info = copy_session_info(session_info_t,
851 session_info);
852 if (session_info_t->session_info == NULL) {
853 DEBUG(0, ("copy_session_info failed\n"));
854 goto fail;
857 become_root();
858 subreq = tstream_npa_connect_send(talloc_tos(), ev,
859 socket_np_dir,
860 pipe_name,
861 remote_address, /* client_addr */
862 NULL, /* client_name */
863 local_address, /* server_addr */
864 NULL, /* server_name */
865 session_info_t);
866 if (subreq == NULL) {
867 unbecome_root();
868 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
869 "user %s\\%s failed\n",
870 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
871 session_info_t->session_info->info->account_name));
872 goto fail;
874 ok = tevent_req_poll(subreq, ev);
875 unbecome_root();
876 if (!ok) {
877 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
878 "failed for tstream_npa_connect: %s\n",
879 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
880 session_info_t->session_info->info->account_name,
881 strerror(errno)));
882 goto fail;
885 ret = tstream_npa_connect_recv(subreq, &sys_errno,
886 result,
887 &result->npipe,
888 &result->file_type,
889 &result->device_state,
890 &result->allocation_size);
891 TALLOC_FREE(subreq);
892 if (ret != 0) {
893 int l = 1;
894 if (errno == ENOENT) {
895 l = 2;
897 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
898 "user %s\\%s failed: %s\n",
899 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
900 session_info_t->session_info->info->account_name,
901 strerror(sys_errno)));
902 goto fail;
905 return result;
907 fail:
908 TALLOC_FREE(result);
909 return NULL;
912 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
913 const char *pipe_name,
914 const struct ndr_interface_table *table,
915 const struct auth_session_info *session_info,
916 const struct tsocket_address *remote_client_address,
917 const struct tsocket_address *local_server_address,
918 struct rpc_pipe_client **_result)
920 struct rpc_pipe_client *result = NULL;
921 struct np_proxy_state *proxy_state = NULL;
922 struct pipe_auth_data *auth;
923 struct tsocket_address *remote_client_addr;
924 struct tsocket_address *local_server_addr;
925 NTSTATUS status;
926 int ret;
928 if (local_server_address == NULL) {
929 /* this is an internal connection, fake up ip addresses */
930 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
931 NULL, 0, &local_server_addr);
932 if (ret) {
933 return NT_STATUS_NO_MEMORY;
935 local_server_address = local_server_addr;
938 if (remote_client_address == NULL) {
939 /* this is an internal connection, fake up ip addresses */
940 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
941 NULL, 0, &remote_client_addr);
942 if (ret) {
943 return NT_STATUS_NO_MEMORY;
945 remote_client_address = remote_client_addr;
948 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
949 local_server_address,
950 remote_client_address,
951 session_info);
952 if (!proxy_state) {
953 DEBUG(1, ("Unable to make proxy_state for connection to %s.\n", pipe_name));
954 return NT_STATUS_UNSUCCESSFUL;
957 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
958 if (result == NULL) {
959 status = NT_STATUS_NO_MEMORY;
960 goto done;
963 result->abstract_syntax = table->syntax_id;
964 result->transfer_syntax = ndr_transfer_syntax_ndr;
966 result->desthost = get_myname(result);
967 result->srv_name_slash = talloc_asprintf_strupper_m(
968 result, "\\\\%s", result->desthost);
969 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
970 status = NT_STATUS_NO_MEMORY;
971 goto done;
974 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
976 status = rpc_transport_tstream_init(result,
977 &proxy_state->npipe,
978 &result->transport);
979 if (!NT_STATUS_IS_OK(status)) {
980 goto done;
983 result->binding_handle = rpccli_bh_create(result, NULL, table);
984 if (result->binding_handle == NULL) {
985 status = NT_STATUS_NO_MEMORY;
986 DEBUG(0, ("Failed to create binding handle.\n"));
987 goto done;
990 result->auth = talloc_zero(result, struct pipe_auth_data);
991 if (!result->auth) {
992 status = NT_STATUS_NO_MEMORY;
993 goto done;
995 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
996 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
997 result->auth->auth_context_id = 0;
999 status = rpccli_anon_bind_data(result, &auth);
1000 if (!NT_STATUS_IS_OK(status)) {
1001 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
1002 goto done;
1005 status = rpc_pipe_bind(result, auth);
1006 if (!NT_STATUS_IS_OK(status)) {
1007 DEBUG(0, ("Failed to bind external pipe.\n"));
1008 goto done;
1011 done:
1012 if (!NT_STATUS_IS_OK(status)) {
1013 TALLOC_FREE(result);
1015 TALLOC_FREE(proxy_state);
1016 *_result = result;
1017 return status;
1021 * @brief Create a new RPC client context which uses a local dispatch function
1022 * or a remote transport, depending on rpc_server configuration for the
1023 * specific service.
1025 * @param[in] mem_ctx The memory context to use.
1027 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
1028 * ndr_table_<name>.
1030 * @param[in] serversupplied_info The server supplied authentication function.
1032 * @param[in] remote_address The client address information.
1034 * @param[in] msg_ctx The messaging context to use.
1036 * @param[out] presult A pointer to store the connected rpc client pipe.
1038 * @return NT_STATUS_OK on success, a corresponding NT status if an
1039 * error occurred.
1041 * @code
1042 * struct rpc_pipe_client *winreg_pipe;
1043 * NTSTATUS status;
1045 * status = rpc_pipe_open_interface(tmp_ctx,
1046 * &ndr_table_winreg.syntax_id,
1047 * p->session_info,
1048 * remote_address,
1049 * &winreg_pipe);
1050 * @endcode
1053 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
1054 const struct ndr_interface_table *table,
1055 const struct auth_session_info *session_info,
1056 const struct tsocket_address *remote_address,
1057 const struct tsocket_address *local_address,
1058 struct messaging_context *msg_ctx,
1059 struct rpc_pipe_client **cli_pipe)
1061 struct rpc_pipe_client *cli = NULL;
1062 enum rpc_service_mode_e pipe_mode;
1063 const char *pipe_name;
1064 NTSTATUS status;
1065 TALLOC_CTX *tmp_ctx;
1067 if (cli_pipe != NULL) {
1068 if (rpccli_is_connected(*cli_pipe)) {
1069 return NT_STATUS_OK;
1070 } else {
1071 TALLOC_FREE(*cli_pipe);
1075 tmp_ctx = talloc_stackframe();
1076 if (tmp_ctx == NULL) {
1077 return NT_STATUS_NO_MEMORY;
1080 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
1081 if (pipe_name == NULL) {
1082 DEBUG(1, ("Unable to find pipe name to forward %s to.\n", table->name));
1083 status = NT_STATUS_INVALID_PARAMETER;
1084 goto done;
1087 while (pipe_name[0] == '\\') {
1088 pipe_name++;
1091 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
1093 pipe_mode = rpc_service_mode(pipe_name);
1095 switch (pipe_mode) {
1096 case RPC_SERVICE_MODE_EMBEDDED:
1097 status = rpc_pipe_open_internal(tmp_ctx,
1098 table, session_info,
1099 remote_address, local_address,
1100 msg_ctx,
1101 &cli);
1102 if (!NT_STATUS_IS_OK(status)) {
1103 goto done;
1105 break;
1106 case RPC_SERVICE_MODE_EXTERNAL:
1107 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
1108 * for now we need to use the special proxy setup to connect
1109 * to spoolssd. */
1111 status = rpc_pipe_open_external(tmp_ctx,
1112 pipe_name, table,
1113 session_info,
1114 remote_address, local_address,
1115 &cli);
1116 if (!NT_STATUS_IS_OK(status)) {
1117 goto done;
1119 break;
1120 case RPC_SERVICE_MODE_DISABLED:
1121 status = NT_STATUS_NOT_IMPLEMENTED;
1122 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
1123 pipe_name, nt_errstr(status)));
1124 goto done;
1127 status = NT_STATUS_OK;
1128 done:
1129 if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
1130 *cli_pipe = talloc_move(mem_ctx, &cli);
1132 TALLOC_FREE(tmp_ctx);
1133 return status;