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"
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"
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
);
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"));
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"));
72 NTSTATUS
make_internal_rpc_pipe_socketpair(
74 struct tevent_context
*ev_ctx
,
75 struct messaging_context
*msg_ctx
,
76 const char *pipe_name
,
77 const struct ndr_syntax_id
*syntax
,
78 const struct tsocket_address
*remote_address
,
79 const struct tsocket_address
*local_address
,
80 const struct auth_session_info
*session_info
,
81 struct npa_state
**pnpa
)
83 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
84 struct named_pipe_client
*npc
;
85 struct tevent_req
*subreq
;
86 struct npa_state
*npa
;
91 DEBUG(4, ("Create of internal pipe %s requested\n", pipe_name
));
93 npa
= npa_state_init(tmp_ctx
);
95 status
= NT_STATUS_NO_MEMORY
;
99 npa
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
100 npa
->device_state
= 0xff | 0x0400 | 0x0100;
101 npa
->allocation_size
= 4096;
103 npc
= named_pipe_client_init(npa
,
110 npa
->allocation_size
,
111 NULL
); /* private_data */
113 status
= NT_STATUS_NO_MEMORY
;
116 npa
->private_data
= (void*) npc
;
118 rc
= tstream_npa_socketpair(npa
->file_type
,
124 status
= map_nt_error_from_unix(errno
);
128 npc
->remote_client_addr
= tsocket_address_copy(remote_address
, npc
);
129 if (npc
->remote_client_addr
== NULL
) {
130 status
= NT_STATUS_NO_MEMORY
;
134 npc
->remote_client_name
= tsocket_address_inet_addr_string(npc
->remote_client_addr
,
136 if (npc
->remote_client_name
== NULL
) {
137 status
= NT_STATUS_NO_MEMORY
;
141 npc
->local_server_addr
= tsocket_address_copy(local_address
, npc
);
142 if (npc
->local_server_addr
== NULL
) {
143 status
= NT_STATUS_NO_MEMORY
;
147 npc
->local_server_name
= tsocket_address_inet_addr_string(
148 npc
->local_server_addr
, npc
);
149 if (npc
->local_server_name
== NULL
) {
150 status
= NT_STATUS_NO_MEMORY
;
154 npc
->session_info
= copy_session_info(npc
, session_info
);
155 if (npc
->session_info
== NULL
) {
156 status
= NT_STATUS_NO_MEMORY
;
160 rc
= make_server_pipes_struct(npc
,
164 npc
->remote_client_addr
,
165 npc
->local_server_addr
,
170 status
= map_nt_error_from_unix(error
);
174 npc
->write_queue
= tevent_queue_create(npc
, "npa_server_write_queue");
175 if (npc
->write_queue
== NULL
) {
176 status
= NT_STATUS_NO_MEMORY
;
180 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
181 if (subreq
== NULL
) {
182 DEBUG(2, ("Failed to start receiving packets\n"));
183 status
= NT_STATUS_PIPE_BROKEN
;
186 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
188 *pnpa
= talloc_steal(mem_ctx
, npa
);
189 status
= NT_STATUS_OK
;
191 talloc_free(tmp_ctx
);
195 /****************************************************************************
196 Make an internal namedpipes structure
197 ****************************************************************************/
199 struct pipes_struct
*make_internal_rpc_pipe_p(TALLOC_CTX
*mem_ctx
,
200 const struct ndr_syntax_id
*syntax
,
201 const struct tsocket_address
*remote_address
,
202 const struct tsocket_address
*local_address
,
203 const struct auth_session_info
*session_info
,
204 struct messaging_context
*msg_ctx
)
206 struct pipes_struct
*p
;
207 struct pipe_rpc_fns
*context_fns
;
208 const char *pipe_name
;
210 const struct ndr_interface_table
*table
;
212 table
= ndr_table_by_uuid(&syntax
->uuid
);
214 DEBUG(0,("unknown interface\n"));
218 pipe_name
= dcerpc_default_transport_endpoint(mem_ctx
, NCACN_NP
, table
);
220 DEBUG(4,("Create pipe requested %s\n", pipe_name
));
222 ret
= make_base_pipes_struct(mem_ctx
, msg_ctx
, pipe_name
,
223 NCALRPC
, RPC_LITTLE_ENDIAN
,
224 remote_address
, local_address
, &p
);
226 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
230 if (!init_pipe_handles(p
, syntax
)) {
231 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
236 p
->session_info
= copy_session_info(p
, session_info
);
237 if (p
->session_info
== NULL
) {
238 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
239 close_policy_by_pipe(p
);
244 context_fns
= talloc_zero(p
, struct pipe_rpc_fns
);
245 if (context_fns
== NULL
) {
246 DEBUG(0,("talloc() failed!\n"));
251 context_fns
->next
= context_fns
->prev
= NULL
;
252 context_fns
->n_cmds
= rpc_srv_get_pipe_num_cmds(syntax
);
253 context_fns
->cmds
= rpc_srv_get_pipe_cmds(syntax
);
254 context_fns
->context_id
= 0;
255 context_fns
->syntax
= *syntax
;
257 /* add to the list of open contexts */
258 DLIST_ADD(p
->contexts
, context_fns
);
260 DEBUG(4,("Created internal pipe %s\n", pipe_name
));
265 static NTSTATUS
rpcint_dispatch(struct pipes_struct
*p
,
268 const DATA_BLOB
*in_data
,
271 struct pipe_rpc_fns
*fns
= find_pipe_fns_by_context(p
->contexts
, 0);
272 uint32_t num_cmds
= fns
->n_cmds
;
273 const struct api_struct
*cmds
= fns
->cmds
;
280 for (i
= 0; i
< num_cmds
; i
++) {
281 if (cmds
[i
].opnum
== opnum
&& cmds
[i
].fn
!= NULL
) {
287 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
;
290 p
->in_data
.data
= *in_data
;
291 p
->out_data
.rdata
= data_blob_null
;
294 p
->in_data
.data
= data_blob_null
;
296 data_blob_free(&p
->out_data
.rdata
);
297 talloc_free_children(p
->mem_ctx
);
298 return NT_STATUS_RPC_CALL_FAILED
;
301 if (p
->fault_state
) {
304 status
= NT_STATUS(p
->fault_state
);
306 data_blob_free(&p
->out_data
.rdata
);
307 talloc_free_children(p
->mem_ctx
);
311 *out_data
= p
->out_data
.rdata
;
312 talloc_steal(mem_ctx
, out_data
->data
);
313 p
->out_data
.rdata
= data_blob_null
;
315 talloc_free_children(p
->mem_ctx
);
319 struct rpcint_bh_state
{
320 struct pipes_struct
*p
;
323 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle
*h
)
325 struct rpcint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
326 struct rpcint_bh_state
);
335 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle
*h
,
338 /* TODO: implement timeouts */
342 struct rpcint_bh_raw_call_state
{
348 static struct tevent_req
*rpcint_bh_raw_call_send(TALLOC_CTX
*mem_ctx
,
349 struct tevent_context
*ev
,
350 struct dcerpc_binding_handle
*h
,
351 const struct GUID
*object
,
354 const uint8_t *in_data
,
357 struct rpcint_bh_state
*hs
=
358 dcerpc_binding_handle_data(h
,
359 struct rpcint_bh_state
);
360 struct tevent_req
*req
;
361 struct rpcint_bh_raw_call_state
*state
;
365 req
= tevent_req_create(mem_ctx
, &state
,
366 struct rpcint_bh_raw_call_state
);
370 state
->in_data
.data
= discard_const_p(uint8_t, in_data
);
371 state
->in_data
.length
= in_length
;
373 ok
= rpcint_bh_is_connected(h
);
375 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
376 return tevent_req_post(req
, ev
);
379 /* TODO: allow async */
380 status
= rpcint_dispatch(hs
->p
, state
, opnum
,
383 if (!NT_STATUS_IS_OK(status
)) {
384 tevent_req_nterror(req
, status
);
385 return tevent_req_post(req
, ev
);
388 tevent_req_done(req
);
389 return tevent_req_post(req
, ev
);
392 static NTSTATUS
rpcint_bh_raw_call_recv(struct tevent_req
*req
,
398 struct rpcint_bh_raw_call_state
*state
=
400 struct rpcint_bh_raw_call_state
);
403 if (tevent_req_is_nterror(req
, &status
)) {
404 tevent_req_received(req
);
408 *out_data
= talloc_move(mem_ctx
, &state
->out_data
.data
);
409 *out_length
= state
->out_data
.length
;
411 tevent_req_received(req
);
415 struct rpcint_bh_disconnect_state
{
419 static struct tevent_req
*rpcint_bh_disconnect_send(TALLOC_CTX
*mem_ctx
,
420 struct tevent_context
*ev
,
421 struct dcerpc_binding_handle
*h
)
423 struct rpcint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
424 struct rpcint_bh_state
);
425 struct tevent_req
*req
;
426 struct rpcint_bh_disconnect_state
*state
;
429 req
= tevent_req_create(mem_ctx
, &state
,
430 struct rpcint_bh_disconnect_state
);
435 ok
= rpcint_bh_is_connected(h
);
437 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
438 return tevent_req_post(req
, ev
);
442 * TODO: do a real async disconnect ...
444 * For now the caller needs to free pipes_struct
448 tevent_req_done(req
);
449 return tevent_req_post(req
, ev
);
452 static NTSTATUS
rpcint_bh_disconnect_recv(struct tevent_req
*req
)
456 if (tevent_req_is_nterror(req
, &status
)) {
457 tevent_req_received(req
);
461 tevent_req_received(req
);
465 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle
*h
)
470 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle
*h
,
472 const void *_struct_ptr
,
473 const struct ndr_interface_call
*call
)
475 void *struct_ptr
= discard_const(_struct_ptr
);
477 if (DEBUGLEVEL
< 11) {
481 if (ndr_flags
& NDR_IN
) {
482 ndr_print_function_debug(call
->ndr_print
,
487 if (ndr_flags
& NDR_OUT
) {
488 ndr_print_function_debug(call
->ndr_print
,
495 static const struct dcerpc_binding_handle_ops rpcint_bh_ops
= {
497 .is_connected
= rpcint_bh_is_connected
,
498 .set_timeout
= rpcint_bh_set_timeout
,
499 .raw_call_send
= rpcint_bh_raw_call_send
,
500 .raw_call_recv
= rpcint_bh_raw_call_recv
,
501 .disconnect_send
= rpcint_bh_disconnect_send
,
502 .disconnect_recv
= rpcint_bh_disconnect_recv
,
504 .ref_alloc
= rpcint_bh_ref_alloc
,
505 .do_ndr_print
= rpcint_bh_do_ndr_print
,
508 static NTSTATUS
rpcint_binding_handle_ex(TALLOC_CTX
*mem_ctx
,
509 const struct ndr_syntax_id
*abstract_syntax
,
510 const struct ndr_interface_table
*ndr_table
,
511 const struct tsocket_address
*remote_address
,
512 const struct tsocket_address
*local_address
,
513 const struct auth_session_info
*session_info
,
514 struct messaging_context
*msg_ctx
,
515 struct dcerpc_binding_handle
**binding_handle
)
517 struct dcerpc_binding_handle
*h
;
518 struct rpcint_bh_state
*hs
;
521 abstract_syntax
= &ndr_table
->syntax_id
;
524 h
= dcerpc_binding_handle_create(mem_ctx
,
529 struct rpcint_bh_state
,
532 return NT_STATUS_NO_MEMORY
;
534 hs
->p
= make_internal_rpc_pipe_p(hs
,
542 return NT_STATUS_NO_MEMORY
;
549 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
551 * @param[in] mem_ctx The memory context to use.
553 * @param[in] ndr_table Normally the ndr_table_<name>.
555 * @param[in] remote_address The info about the connected client.
557 * @param[in] serversupplied_info The server supplied authentication function.
559 * @param[in] msg_ctx The messaging context that can be used by the server
561 * @param[out] binding_handle A pointer to store the connected
562 * dcerpc_binding_handle
564 * @return NT_STATUS_OK on success, a corresponding NT status if an
568 * struct dcerpc_binding_handle *winreg_binding;
571 * status = rpcint_binding_handle(tmp_ctx,
579 NTSTATUS
rpcint_binding_handle(TALLOC_CTX
*mem_ctx
,
580 const struct ndr_interface_table
*ndr_table
,
581 const struct tsocket_address
*remote_address
,
582 const struct tsocket_address
*local_address
,
583 const struct auth_session_info
*session_info
,
584 struct messaging_context
*msg_ctx
,
585 struct dcerpc_binding_handle
**binding_handle
)
587 return rpcint_binding_handle_ex(mem_ctx
, NULL
, ndr_table
, remote_address
,
588 local_address
, session_info
,
589 msg_ctx
, binding_handle
);
595 * @brief Create a new RPC client context which uses a local transport.
597 * This creates a local transport. It is a shortcut to directly call the server
598 * functions and avoid marshalling.
599 * NOTE: this function should be used only by rpc_pipe_open_interface()
601 * @param[in] mem_ctx The memory context to use.
603 * @param[in] ndr_table the ndr_table_<name> structure.
605 * @param[in] serversupplied_info The server supplied authentication function.
607 * @param[in] remote_address The client address information.
609 * @param[in] msg_ctx The messaging context to use.
611 * @param[out] presult A pointer to store the connected rpc client pipe.
613 * @return NT_STATUS_OK on success, a corresponding NT status if an
616 NTSTATUS
rpc_pipe_open_internal(TALLOC_CTX
*mem_ctx
,
617 const struct ndr_interface_table
*ndr_table
,
618 const struct auth_session_info
*session_info
,
619 const struct tsocket_address
*remote_address
,
620 const struct tsocket_address
*local_address
,
621 struct messaging_context
*msg_ctx
,
622 struct rpc_pipe_client
**presult
)
624 struct rpc_pipe_client
*result
;
627 result
= talloc_zero(mem_ctx
, struct rpc_pipe_client
);
628 if (result
== NULL
) {
629 return NT_STATUS_NO_MEMORY
;
632 result
->abstract_syntax
= ndr_table
->syntax_id
;
633 result
->transfer_syntax
= ndr_transfer_syntax_ndr
;
635 if (remote_address
== NULL
) {
636 struct tsocket_address
*local
;
639 rc
= tsocket_address_inet_from_strings(mem_ctx
,
646 return NT_STATUS_NO_MEMORY
;
649 remote_address
= local
;
652 result
->max_xmit_frag
= -1;
654 status
= rpcint_binding_handle(result
,
660 &result
->binding_handle
);
661 if (!NT_STATUS_IS_OK(status
)) {
670 /****************************************************************************
671 * External pipes functions
672 ***************************************************************************/
674 NTSTATUS
make_external_rpc_pipe(TALLOC_CTX
*mem_ctx
,
675 const char *pipe_name
,
676 const struct tsocket_address
*remote_client_address
,
677 const struct tsocket_address
*local_server_address
,
678 const struct auth_session_info
*session_info
,
679 struct npa_state
**pnpa
)
681 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
682 struct auth_session_info_transport
*session_info_t
;
683 struct tevent_context
*ev_ctx
;
684 struct tevent_req
*subreq
;
685 const char *socket_np_dir
;
686 const char *socket_dir
;
687 struct npa_state
*npa
;
693 npa
= npa_state_init(tmp_ctx
);
695 status
= NT_STATUS_NO_MEMORY
;
699 socket_dir
= lp_parm_const_string(GLOBAL_SECTION_SNUM
,
703 if (socket_dir
== NULL
) {
704 DEBUG(0, ("external_rpc_pipe: socket_dir not set\n"));
705 status
= NT_STATUS_PIPE_NOT_AVAILABLE
;
709 socket_np_dir
= talloc_asprintf(tmp_ctx
, "%s/np", socket_dir
);
710 if (socket_np_dir
== NULL
) {
711 DEBUG(0, ("talloc_asprintf failed\n"));
712 status
= NT_STATUS_NO_MEMORY
;
716 session_info_t
= talloc_zero(tmp_ctx
,
717 struct auth_session_info_transport
);
718 if (session_info_t
== NULL
) {
719 DEBUG(0, ("talloc failed\n"));
720 status
= NT_STATUS_NO_MEMORY
;
724 session_info_t
->session_info
= copy_session_info(session_info_t
,
726 if (session_info_t
->session_info
== NULL
) {
727 DEBUG(0, ("copy_session_info failed\n"));
728 status
= NT_STATUS_NO_MEMORY
;
732 ev_ctx
= samba_tevent_context_init(tmp_ctx
);
733 if (ev_ctx
== NULL
) {
734 DEBUG(0, ("samba_tevent_context_init failed\n"));
735 status
= NT_STATUS_NO_MEMORY
;
740 subreq
= tstream_npa_connect_send(tmp_ctx
,
744 remote_client_address
,
745 NULL
, /* client_name */
746 local_server_address
,
747 NULL
, /* server_name */
749 if (subreq
== NULL
) {
751 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
752 "user %s\\%s failed\n",
753 socket_np_dir
, pipe_name
, session_info_t
->session_info
->info
->domain_name
,
754 session_info_t
->session_info
->info
->account_name
));
755 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
758 ok
= tevent_req_poll(subreq
, ev_ctx
);
761 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
762 "failed for tstream_npa_connect: %s\n",
765 session_info_t
->session_info
->info
->domain_name
,
766 session_info_t
->session_info
->info
->account_name
,
768 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
772 rc
= tstream_npa_connect_recv(subreq
,
778 &npa
->allocation_size
);
783 if (errno
== ENOENT
) {
787 DEBUG(l
, ("tstream_npa_connect_recv to %s for pipe %s and "
788 "user %s\\%s failed: %s\n",
791 session_info_t
->session_info
->info
->domain_name
,
792 session_info_t
->session_info
->info
->account_name
,
793 strerror(sys_errno
)));
794 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
798 *pnpa
= talloc_steal(mem_ctx
, npa
);
799 status
= NT_STATUS_OK
;
801 talloc_free(tmp_ctx
);
806 struct np_proxy_state
*make_external_rpc_pipe_p(TALLOC_CTX
*mem_ctx
,
807 const char *pipe_name
,
808 const struct tsocket_address
*remote_client_address
,
809 const struct tsocket_address
*local_server_address
,
810 const struct auth_session_info
*session_info
)
812 struct np_proxy_state
*result
;
814 const char *socket_dir
;
815 struct tevent_context
*ev
;
816 struct tevent_req
*subreq
;
817 struct auth_session_info_transport
*session_info_t
;
822 result
= talloc(mem_ctx
, struct np_proxy_state
);
823 if (result
== NULL
) {
824 DEBUG(0, ("talloc failed\n"));
828 result
->read_queue
= tevent_queue_create(result
, "np_read");
829 if (result
->read_queue
== NULL
) {
830 DEBUG(0, ("tevent_queue_create failed\n"));
834 result
->write_queue
= tevent_queue_create(result
, "np_write");
835 if (result
->write_queue
== NULL
) {
836 DEBUG(0, ("tevent_queue_create failed\n"));
840 ev
= samba_tevent_context_init(talloc_tos());
842 DEBUG(0, ("samba_tevent_context_init failed\n"));
846 socket_dir
= lp_parm_const_string(
847 GLOBAL_SECTION_SNUM
, "external_rpc_pipe", "socket_dir",
849 if (socket_dir
== NULL
) {
850 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
853 socket_np_dir
= talloc_asprintf(talloc_tos(), "%s/np", socket_dir
);
854 if (socket_np_dir
== NULL
) {
855 DEBUG(0, ("talloc_asprintf failed\n"));
859 session_info_t
= talloc_zero(talloc_tos(), struct auth_session_info_transport
);
860 if (session_info_t
== NULL
) {
861 DEBUG(0, ("talloc failed\n"));
865 session_info_t
->session_info
= copy_session_info(session_info_t
,
867 if (session_info_t
->session_info
== NULL
) {
868 DEBUG(0, ("copy_session_info failed\n"));
873 subreq
= tstream_npa_connect_send(talloc_tos(), ev
,
876 remote_client_address
,
877 NULL
, /* client_name */
878 local_server_address
,
879 NULL
, /* server_name */
881 if (subreq
== NULL
) {
883 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
884 "user %s\\%s failed\n",
885 socket_np_dir
, pipe_name
, session_info_t
->session_info
->info
->domain_name
,
886 session_info_t
->session_info
->info
->account_name
));
889 ok
= tevent_req_poll(subreq
, ev
);
892 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
893 "failed for tstream_npa_connect: %s\n",
894 socket_np_dir
, pipe_name
, session_info_t
->session_info
->info
->domain_name
,
895 session_info_t
->session_info
->info
->account_name
,
900 ret
= tstream_npa_connect_recv(subreq
, &sys_errno
,
904 &result
->device_state
,
905 &result
->allocation_size
);
909 if (errno
== ENOENT
) {
912 DEBUG(l
, ("tstream_npa_connect_recv to %s for pipe %s and "
913 "user %s\\%s failed: %s\n",
914 socket_np_dir
, pipe_name
, session_info_t
->session_info
->info
->domain_name
,
915 session_info_t
->session_info
->info
->account_name
,
916 strerror(sys_errno
)));
927 static NTSTATUS
rpc_pipe_open_external(TALLOC_CTX
*mem_ctx
,
928 const char *pipe_name
,
929 const struct ndr_interface_table
*table
,
930 const struct auth_session_info
*session_info
,
931 const struct tsocket_address
*remote_client_address
,
932 const struct tsocket_address
*local_server_address
,
933 struct rpc_pipe_client
**_result
)
935 struct rpc_pipe_client
*result
= NULL
;
936 struct np_proxy_state
*proxy_state
= NULL
;
937 struct pipe_auth_data
*auth
;
938 struct tsocket_address
*remote_client_addr
;
939 struct tsocket_address
*local_server_addr
;
943 if (local_server_address
== NULL
) {
944 /* this is an internal connection, fake up ip addresses */
945 ret
= tsocket_address_inet_from_strings(talloc_tos(), "ip",
946 NULL
, 0, &local_server_addr
);
948 return NT_STATUS_NO_MEMORY
;
950 local_server_address
= local_server_addr
;
953 if (remote_client_address
== NULL
) {
954 /* this is an internal connection, fake up ip addresses */
955 ret
= tsocket_address_inet_from_strings(talloc_tos(), "ip",
956 NULL
, 0, &remote_client_addr
);
958 return NT_STATUS_NO_MEMORY
;
960 remote_client_address
= remote_client_addr
;
963 proxy_state
= make_external_rpc_pipe_p(mem_ctx
, pipe_name
,
964 remote_client_address
,
965 local_server_address
,
968 DEBUG(1, ("Unable to make proxy_state for connection to %s.\n", pipe_name
));
969 return NT_STATUS_UNSUCCESSFUL
;
972 result
= talloc_zero(mem_ctx
, struct rpc_pipe_client
);
973 if (result
== NULL
) {
974 status
= NT_STATUS_NO_MEMORY
;
978 result
->abstract_syntax
= table
->syntax_id
;
979 result
->transfer_syntax
= ndr_transfer_syntax_ndr
;
981 result
->desthost
= get_myname(result
);
982 result
->srv_name_slash
= talloc_asprintf_strupper_m(
983 result
, "\\\\%s", result
->desthost
);
984 if ((result
->desthost
== NULL
) || (result
->srv_name_slash
== NULL
)) {
985 status
= NT_STATUS_NO_MEMORY
;
989 result
->max_xmit_frag
= RPC_MAX_PDU_FRAG_LEN
;
991 status
= rpc_transport_tstream_init(result
,
994 if (!NT_STATUS_IS_OK(status
)) {
998 result
->binding_handle
= rpccli_bh_create(result
, NULL
, table
);
999 if (result
->binding_handle
== NULL
) {
1000 status
= NT_STATUS_NO_MEMORY
;
1001 DEBUG(0, ("Failed to create binding handle.\n"));
1005 result
->auth
= talloc_zero(result
, struct pipe_auth_data
);
1006 if (!result
->auth
) {
1007 status
= NT_STATUS_NO_MEMORY
;
1010 result
->auth
->auth_type
= DCERPC_AUTH_TYPE_NONE
;
1011 result
->auth
->auth_level
= DCERPC_AUTH_LEVEL_NONE
;
1012 result
->auth
->auth_context_id
= 0;
1014 status
= rpccli_anon_bind_data(result
, &auth
);
1015 if (!NT_STATUS_IS_OK(status
)) {
1016 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
1020 status
= rpc_pipe_bind(result
, auth
);
1021 if (!NT_STATUS_IS_OK(status
)) {
1022 DEBUG(0, ("Failed to bind external pipe.\n"));
1027 if (!NT_STATUS_IS_OK(status
)) {
1028 TALLOC_FREE(result
);
1030 TALLOC_FREE(proxy_state
);
1036 * @brief Create a new RPC client context which uses a local dispatch function
1037 * or a remote transport, depending on rpc_server configuration for the
1040 * @param[in] mem_ctx The memory context to use.
1042 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
1045 * @param[in] serversupplied_info The server supplied authentication function.
1047 * @param[in] remote_address The client address information.
1049 * @param[in] msg_ctx The messaging context to use.
1051 * @param[out] presult A pointer to store the connected rpc client pipe.
1053 * @return NT_STATUS_OK on success, a corresponding NT status if an
1057 * struct rpc_pipe_client *winreg_pipe;
1060 * status = rpc_pipe_open_interface(tmp_ctx,
1061 * &ndr_table_winreg.syntax_id,
1068 NTSTATUS
rpc_pipe_open_interface(TALLOC_CTX
*mem_ctx
,
1069 const struct ndr_interface_table
*table
,
1070 const struct auth_session_info
*session_info
,
1071 const struct tsocket_address
*remote_address
,
1072 const struct tsocket_address
*local_address
,
1073 struct messaging_context
*msg_ctx
,
1074 struct rpc_pipe_client
**cli_pipe
)
1076 struct rpc_pipe_client
*cli
= NULL
;
1077 enum rpc_service_mode_e pipe_mode
;
1078 const char *pipe_name
;
1080 TALLOC_CTX
*tmp_ctx
;
1082 if (cli_pipe
!= NULL
) {
1083 if (rpccli_is_connected(*cli_pipe
)) {
1084 return NT_STATUS_OK
;
1086 TALLOC_FREE(*cli_pipe
);
1090 tmp_ctx
= talloc_stackframe();
1091 if (tmp_ctx
== NULL
) {
1092 return NT_STATUS_NO_MEMORY
;
1095 pipe_name
= dcerpc_default_transport_endpoint(mem_ctx
, NCACN_NP
, table
);
1096 if (pipe_name
== NULL
) {
1097 DEBUG(1, ("Unable to find pipe name to forward %s to.\n", table
->name
));
1098 status
= NT_STATUS_INVALID_PARAMETER
;
1102 while (pipe_name
[0] == '\\') {
1106 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name
));
1108 pipe_mode
= rpc_service_mode(pipe_name
);
1110 switch (pipe_mode
) {
1111 case RPC_SERVICE_MODE_EMBEDDED
:
1112 status
= rpc_pipe_open_internal(tmp_ctx
,
1113 table
, session_info
,
1114 remote_address
, local_address
,
1117 if (!NT_STATUS_IS_OK(status
)) {
1121 case RPC_SERVICE_MODE_EXTERNAL
:
1122 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
1123 * for now we need to use the special proxy setup to connect
1126 status
= rpc_pipe_open_external(tmp_ctx
,
1129 remote_address
, local_address
,
1131 if (!NT_STATUS_IS_OK(status
)) {
1135 case RPC_SERVICE_MODE_DISABLED
:
1136 status
= NT_STATUS_NOT_IMPLEMENTED
;
1137 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
1138 pipe_name
, nt_errstr(status
)));
1142 status
= NT_STATUS_OK
;
1144 if (NT_STATUS_IS_OK(status
) && cli_pipe
!= NULL
) {
1145 *cli_pipe
= talloc_move(mem_ctx
, &cli
);
1147 TALLOC_FREE(tmp_ctx
);