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/>.
25 #include "rpc_client/cli_pipe.h"
26 #include "rpc_server/srv_pipe_internal.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"
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"
44 #define DBGC_CLASS DBGC_RPC_SRV
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);
54 static struct npa_state *npa_state_init(TALLOC_CTX *mem_ctx)
56 struct npa_state *npa;
58 npa = talloc_zero(mem_ctx, struct npa_state);
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"));
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"));
81 NTSTATUS make_internal_rpc_pipe_socketpair(
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)
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;
100 DEBUG(4, ("Create of internal pipe %s requested\n", pipe_name));
102 npa = npa_state_init(tmp_ctx);
104 status = NT_STATUS_NO_MEMORY;
108 npa->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
109 npa->device_state = 0xff | 0x0400 | 0x0100;
110 npa->allocation_size = 4096;
112 status = dcerpc_ncacn_conn_init(npa,
117 NULL, /* termination fn */
118 NULL, /* termination data */
120 if (!NT_STATUS_IS_OK(status)) {
124 npa->private_data = (void*)ncacn_conn;
126 rc = tstream_npa_socketpair(npa->file_type,
130 &ncacn_conn->tstream);
132 status = map_nt_error_from_unix(errno);
136 ncacn_conn->remote_client_addr = tsocket_address_copy(remote_address,
138 if (ncacn_conn->remote_client_addr == NULL) {
139 status = NT_STATUS_NO_MEMORY;
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;
150 ncacn_conn->local_server_addr = tsocket_address_copy(local_address,
152 if (ncacn_conn->local_server_addr == NULL) {
153 status = NT_STATUS_NO_MEMORY;
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;
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;
170 rc = make_server_pipes_struct(ncacn_conn,
173 ncacn_conn->transport,
174 ncacn_conn->remote_client_addr,
175 ncacn_conn->local_server_addr,
176 &ncacn_conn->session_info,
180 status = map_nt_error_from_unix(error);
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;
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;
197 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process,
200 *pnpa = talloc_move(mem_ctx, &npa);
201 status = NT_STATUS_OK;
203 talloc_free(tmp_ctx);
207 /****************************************************************************
208 Make an internal namedpipes structure
209 ****************************************************************************/
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)
219 struct pipes_struct *out;
220 struct pipe_rpc_fns *context_fns;
221 const char *pipe_name;
223 const struct ndr_interface_table *table;
225 table = ndr_table_by_uuid(&syntax->uuid);
227 DBG_ERR("Unknown interface\n");
228 return NT_STATUS_RPC_INTERFACE_NOT_FOUND;
231 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
233 DBG_INFO("Create pipe requested %s\n", pipe_name);
235 ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
236 NCALRPC, RPC_LITTLE_ENDIAN,
237 remote_address, local_address, &out);
239 DBG_ERR("No memory for pipes_struct!\n");
240 return NT_STATUS_NO_MEMORY;
243 if (!init_pipe_handles(out, syntax)) {
244 DBG_ERR("init_pipe_handles failed.\n");
246 return NT_STATUS_UNSUCCESSFUL;
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);
254 return NT_STATUS_UNSUCCESSFUL;
257 context_fns = talloc_zero(out, struct pipe_rpc_fns);
258 if (context_fns == NULL) {
259 DBG_ERR("No memory");
261 return NT_STATUS_NO_MEMORY;
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;
270 /* add to the list of open contexts */
271 DLIST_ADD(out->contexts, context_fns);
273 DEBUG(4,("Created internal pipe %s\n", pipe_name));
280 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
283 const DATA_BLOB *in_data,
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;
295 for (i = 0; i < num_cmds; i++) {
296 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
302 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
305 p->in_data.data = *in_data;
306 p->out_data.rdata = data_blob_null;
309 p->in_data.data = data_blob_null;
311 data_blob_free(&p->out_data.rdata);
312 talloc_free_children(p->mem_ctx);
313 return NT_STATUS_RPC_CALL_FAILED;
316 if (p->fault_state) {
319 status = NT_STATUS(p->fault_state);
321 data_blob_free(&p->out_data.rdata);
322 talloc_free_children(p->mem_ctx);
326 *out_data = p->out_data.rdata;
327 talloc_steal(mem_ctx, out_data->data);
328 p->out_data.rdata = data_blob_null;
330 talloc_free_children(p->mem_ctx);
334 struct rpcint_bh_state {
335 struct pipes_struct *p;
338 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
340 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
341 struct rpcint_bh_state);
350 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
353 /* TODO: implement timeouts */
357 struct rpcint_bh_raw_call_state {
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,
369 const uint8_t *in_data,
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;
380 req = tevent_req_create(mem_ctx, &state,
381 struct rpcint_bh_raw_call_state);
385 state->in_data.data = discard_const_p(uint8_t, in_data);
386 state->in_data.length = in_length;
388 ok = rpcint_bh_is_connected(h);
390 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
391 return tevent_req_post(req, ev);
394 /* TODO: allow async */
395 status = rpcint_dispatch(hs->p, state, opnum,
398 if (!NT_STATUS_IS_OK(status)) {
399 tevent_req_nterror(req, status);
400 return tevent_req_post(req, ev);
403 tevent_req_done(req);
404 return tevent_req_post(req, ev);
407 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
413 struct rpcint_bh_raw_call_state *state =
415 struct rpcint_bh_raw_call_state);
418 if (tevent_req_is_nterror(req, &status)) {
419 tevent_req_received(req);
423 *out_data = talloc_move(mem_ctx, &state->out_data.data);
424 *out_length = state->out_data.length;
426 tevent_req_received(req);
430 struct rpcint_bh_disconnect_state {
434 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
435 struct tevent_context *ev,
436 struct dcerpc_binding_handle *h)
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;
444 req = tevent_req_create(mem_ctx, &state,
445 struct rpcint_bh_disconnect_state);
450 ok = rpcint_bh_is_connected(h);
452 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
453 return tevent_req_post(req, ev);
457 * TODO: do a real async disconnect ...
459 * For now the caller needs to free pipes_struct
463 tevent_req_done(req);
464 return tevent_req_post(req, ev);
467 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
471 if (tevent_req_is_nterror(req, &status)) {
472 tevent_req_received(req);
476 tevent_req_received(req);
480 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
485 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
487 const void *_struct_ptr,
488 const struct ndr_interface_call *call)
490 void *struct_ptr = discard_const(_struct_ptr);
492 if (DEBUGLEVEL < 11) {
496 if (ndr_flags & NDR_IN) {
497 ndr_print_function_debug(call->ndr_print,
502 if (ndr_flags & NDR_OUT) {
503 ndr_print_function_debug(call->ndr_print,
510 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
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,
519 .ref_alloc = rpcint_bh_ref_alloc,
520 .do_ndr_print = rpcint_bh_do_ndr_print,
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)
532 struct dcerpc_binding_handle *h;
533 struct rpcint_bh_state *hs;
537 abstract_syntax = &ndr_table->syntax_id;
540 h = dcerpc_binding_handle_create(mem_ctx,
545 struct rpcint_bh_state,
548 return NT_STATUS_NO_MEMORY;
551 status = make_internal_rpc_pipe_p(hs,
558 if (!NT_STATUS_IS_OK(status)) {
567 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
569 * @param[in] mem_ctx The memory context to use.
571 * @param[in] ndr_table Normally the ndr_table_<name>.
573 * @param[in] remote_address The info about the connected client.
575 * @param[in] serversupplied_info The server supplied authentication function.
577 * @param[in] msg_ctx The messaging context that can be used by the server
579 * @param[out] binding_handle A pointer to store the connected
580 * dcerpc_binding_handle
582 * @return NT_STATUS_OK on success, a corresponding NT status if an
586 * struct dcerpc_binding_handle *winreg_binding;
589 * status = rpcint_binding_handle(tmp_ctx,
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)
605 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
606 local_address, session_info,
607 msg_ctx, binding_handle);
613 * @brief Create a new RPC client context which uses a local transport.
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()
619 * @param[in] mem_ctx The memory context to use.
621 * @param[in] ndr_table the ndr_table_<name> structure.
623 * @param[in] serversupplied_info The server supplied authentication function.
625 * @param[in] remote_address The client address information.
627 * @param[in] msg_ctx The messaging context to use.
629 * @param[out] presult A pointer to store the connected rpc client pipe.
631 * @return NT_STATUS_OK on success, a corresponding NT status if an
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)
642 struct rpc_pipe_client *result;
645 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
646 if (result == NULL) {
647 return NT_STATUS_NO_MEMORY;
650 result->abstract_syntax = ndr_table->syntax_id;
651 result->transfer_syntax = ndr_transfer_syntax_ndr;
653 if (remote_address == NULL) {
654 struct tsocket_address *local;
657 rc = tsocket_address_inet_from_strings(mem_ctx,
664 return NT_STATUS_NO_MEMORY;
667 remote_address = local;
670 result->max_xmit_frag = -1;
672 status = rpcint_binding_handle(result,
678 &result->binding_handle);
679 if (!NT_STATUS_IS_OK(status)) {
688 /****************************************************************************
689 * External pipes functions
690 ***************************************************************************/
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)
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;
711 npa = npa_state_init(tmp_ctx);
713 status = NT_STATUS_NO_MEMORY;
717 socket_dir = lp_parm_const_string(GLOBAL_SECTION_SNUM,
721 if (socket_dir == NULL) {
722 DEBUG(0, ("external_rpc_pipe: socket_dir not set\n"));
723 status = NT_STATUS_PIPE_NOT_AVAILABLE;
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;
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;
742 session_info_t->session_info = copy_session_info(session_info_t,
744 if (session_info_t->session_info == NULL) {
745 DEBUG(0, ("copy_session_info failed\n"));
746 status = NT_STATUS_NO_MEMORY;
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;
758 subreq = tstream_npa_connect_send(tmp_ctx,
762 remote_client_address,
763 NULL, /* client_name */
764 local_server_address,
765 NULL, /* server_name */
767 if (subreq == NULL) {
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;
776 ok = tevent_req_poll(subreq, ev_ctx);
779 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
780 "failed for tstream_npa_connect: %s\n",
783 session_info_t->session_info->info->domain_name,
784 session_info_t->session_info->info->account_name,
786 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
790 rc = tstream_npa_connect_recv(subreq,
796 &npa->allocation_size);
801 if (errno == ENOENT) {
805 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
806 "user %s\\%s failed: %s\n",
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;
816 *pnpa = talloc_steal(mem_ctx, npa);
817 status = NT_STATUS_OK;
819 talloc_free(tmp_ctx);
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)
830 struct np_proxy_state *result;
832 const char *socket_dir;
833 struct tevent_context *ev;
834 struct tevent_req *subreq;
835 struct auth_session_info_transport *session_info_t;
840 result = talloc(mem_ctx, struct np_proxy_state);
841 if (result == NULL) {
842 DEBUG(0, ("talloc failed\n"));
846 result->read_queue = tevent_queue_create(result, "np_read");
847 if (result->read_queue == NULL) {
848 DEBUG(0, ("tevent_queue_create failed\n"));
852 result->write_queue = tevent_queue_create(result, "np_write");
853 if (result->write_queue == NULL) {
854 DEBUG(0, ("tevent_queue_create failed\n"));
858 ev = samba_tevent_context_init(talloc_tos());
860 DEBUG(0, ("samba_tevent_context_init failed\n"));
864 socket_dir = lp_parm_const_string(
865 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
867 if (socket_dir == NULL) {
868 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
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"));
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"));
883 session_info_t->session_info = copy_session_info(session_info_t,
885 if (session_info_t->session_info == NULL) {
886 DEBUG(0, ("copy_session_info failed\n"));
891 subreq = tstream_npa_connect_send(talloc_tos(), ev,
894 remote_client_address,
895 NULL, /* client_name */
896 local_server_address,
897 NULL, /* server_name */
899 if (subreq == NULL) {
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));
907 ok = tevent_req_poll(subreq, ev);
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,
918 ret = tstream_npa_connect_recv(subreq, &sys_errno,
922 &result->device_state,
923 &result->allocation_size);
927 if (errno == ENOENT) {
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)));
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)
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;
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);
966 return NT_STATUS_NO_MEMORY;
968 local_server_address = local_server_addr;
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);
976 return NT_STATUS_NO_MEMORY;
978 remote_client_address = remote_client_addr;
981 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
982 remote_client_address,
983 local_server_address,
986 DEBUG(1, ("Unable to make proxy_state for connection to %s.\n", pipe_name));
987 return NT_STATUS_UNSUCCESSFUL;
990 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
991 if (result == NULL) {
992 status = NT_STATUS_NO_MEMORY;
996 result->abstract_syntax = table->syntax_id;
997 result->transfer_syntax = ndr_transfer_syntax_ndr;
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;
1007 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1009 status = rpc_transport_tstream_init(result,
1010 &proxy_state->npipe,
1011 &result->transport);
1012 if (!NT_STATUS_IS_OK(status)) {
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"));
1023 result->auth = talloc_zero(result, struct pipe_auth_data);
1024 if (!result->auth) {
1025 status = NT_STATUS_NO_MEMORY;
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;
1032 status = rpccli_anon_bind_data(result, &auth);
1033 if (!NT_STATUS_IS_OK(status)) {
1034 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
1038 status = rpc_pipe_bind(result, auth);
1039 if (!NT_STATUS_IS_OK(status)) {
1040 DEBUG(0, ("Failed to bind external pipe.\n"));
1045 if (!NT_STATUS_IS_OK(status)) {
1046 TALLOC_FREE(result);
1048 TALLOC_FREE(proxy_state);
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
1058 * @param[in] mem_ctx The memory context to use.
1060 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
1063 * @param[in] serversupplied_info The server supplied authentication function.
1065 * @param[in] remote_address The client address information.
1067 * @param[in] msg_ctx The messaging context to use.
1069 * @param[out] presult A pointer to store the connected rpc client pipe.
1071 * @return NT_STATUS_OK on success, a corresponding NT status if an
1075 * struct rpc_pipe_client *winreg_pipe;
1078 * status = rpc_pipe_open_interface(tmp_ctx,
1079 * &ndr_table_winreg.syntax_id,
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)
1094 struct rpc_pipe_client *cli = NULL;
1095 enum rpc_service_mode_e pipe_mode;
1096 const char *pipe_name;
1098 TALLOC_CTX *tmp_ctx;
1100 if (cli_pipe != NULL) {
1101 if (rpccli_is_connected(*cli_pipe)) {
1102 return NT_STATUS_OK;
1104 TALLOC_FREE(*cli_pipe);
1108 tmp_ctx = talloc_stackframe();
1109 if (tmp_ctx == NULL) {
1110 return NT_STATUS_NO_MEMORY;
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;
1120 while (pipe_name[0] == '\\') {
1124 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
1126 pipe_mode = rpc_service_mode(pipe_name);
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,
1135 if (!NT_STATUS_IS_OK(status)) {
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
1144 status = rpc_pipe_open_external(tmp_ctx,
1147 remote_address, local_address,
1149 if (!NT_STATUS_IS_OK(status)) {
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)));
1160 status = NT_STATUS_OK;
1162 if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
1163 *cli_pipe = talloc_move(mem_ctx, &cli);
1165 TALLOC_FREE(tmp_ctx);