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 struct npa_state
*npa_state_init(TALLOC_CTX
*mem_ctx
)
48 struct npa_state
*npa
;
50 npa
= talloc_zero(mem_ctx
, struct npa_state
);
55 npa
->read_queue
= tevent_queue_create(npa
, "npa_cli_read");
56 if (npa
->read_queue
== NULL
) {
57 DEBUG(0, ("tevent_queue_create failed\n"));
61 npa
->write_queue
= tevent_queue_create(npa
, "npa_cli_write");
62 if (npa
->write_queue
== NULL
) {
63 DEBUG(0, ("tevent_queue_create failed\n"));
73 NTSTATUS
make_internal_rpc_pipe_socketpair(
75 struct tevent_context
*ev_ctx
,
76 struct messaging_context
*msg_ctx
,
77 const char *pipe_name
,
78 const struct ndr_syntax_id
*syntax
,
79 const struct tsocket_address
*remote_address
,
80 const struct tsocket_address
*local_address
,
81 const struct auth_session_info
*session_info
,
82 struct npa_state
**pnpa
)
84 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
85 struct named_pipe_client
*npc
;
86 struct tevent_req
*subreq
;
87 struct npa_state
*npa
;
92 DEBUG(4, ("Create of internal pipe %s requested\n", pipe_name
));
94 npa
= npa_state_init(tmp_ctx
);
96 status
= NT_STATUS_NO_MEMORY
;
100 npa
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
101 npa
->device_state
= 0xff | 0x0400 | 0x0100;
102 npa
->allocation_size
= 4096;
104 npc
= named_pipe_client_init(npa
,
111 npa
->allocation_size
,
112 NULL
); /* private_data */
114 status
= NT_STATUS_NO_MEMORY
;
117 npa
->private_data
= (void*) npc
;
119 rc
= tstream_npa_socketpair(npa
->file_type
,
125 status
= map_nt_error_from_unix(errno
);
129 npc
->remote_client_addr
= tsocket_address_copy(remote_address
, npc
);
130 if (npc
->remote_client_addr
== NULL
) {
131 status
= NT_STATUS_NO_MEMORY
;
135 npc
->remote_client_name
= tsocket_address_inet_addr_string(npc
->remote_client_addr
,
137 if (npc
->remote_client_name
== NULL
) {
138 status
= NT_STATUS_NO_MEMORY
;
142 npc
->local_server_addr
= tsocket_address_copy(local_address
, npc
);
143 if (npc
->local_server_addr
== NULL
) {
144 status
= NT_STATUS_NO_MEMORY
;
148 npc
->local_server_name
= tsocket_address_inet_addr_string(
149 npc
->local_server_addr
, npc
);
150 if (npc
->local_server_name
== NULL
) {
151 status
= NT_STATUS_NO_MEMORY
;
155 npc
->session_info
= copy_session_info(npc
, session_info
);
156 if (npc
->session_info
== NULL
) {
157 status
= NT_STATUS_NO_MEMORY
;
161 rc
= make_server_pipes_struct(npc
,
165 npc
->remote_client_addr
,
166 npc
->local_server_addr
,
171 status
= map_nt_error_from_unix(error
);
175 npc
->write_queue
= tevent_queue_create(npc
, "npa_server_write_queue");
176 if (npc
->write_queue
== NULL
) {
177 status
= NT_STATUS_NO_MEMORY
;
181 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
182 if (subreq
== NULL
) {
183 DEBUG(2, ("Failed to start receiving packets\n"));
184 status
= NT_STATUS_PIPE_BROKEN
;
187 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
189 *pnpa
= talloc_steal(mem_ctx
, npa
);
190 status
= NT_STATUS_OK
;
192 talloc_free(tmp_ctx
);
196 /****************************************************************************
197 Make an internal namedpipes structure
198 ****************************************************************************/
200 struct pipes_struct
*make_internal_rpc_pipe_p(TALLOC_CTX
*mem_ctx
,
201 const struct ndr_syntax_id
*syntax
,
202 const struct tsocket_address
*remote_address
,
203 const struct tsocket_address
*local_address
,
204 const struct auth_session_info
*session_info
,
205 struct messaging_context
*msg_ctx
)
207 struct pipes_struct
*p
;
208 struct pipe_rpc_fns
*context_fns
;
209 const char *pipe_name
;
211 const struct ndr_interface_table
*table
;
213 table
= ndr_table_by_uuid(&syntax
->uuid
);
215 DEBUG(0,("unknown interface\n"));
219 pipe_name
= dcerpc_default_transport_endpoint(mem_ctx
, NCACN_NP
, table
);
221 DEBUG(4,("Create pipe requested %s\n", pipe_name
));
223 ret
= make_base_pipes_struct(mem_ctx
, msg_ctx
, pipe_name
,
224 NCALRPC
, RPC_LITTLE_ENDIAN
,
225 remote_address
, local_address
, &p
);
227 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
231 if (!init_pipe_handles(p
, syntax
)) {
232 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
237 p
->session_info
= copy_session_info(p
, session_info
);
238 if (p
->session_info
== NULL
) {
239 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
240 close_policy_by_pipe(p
);
245 context_fns
= talloc_zero(p
, struct pipe_rpc_fns
);
246 if (context_fns
== NULL
) {
247 DEBUG(0,("talloc() failed!\n"));
252 context_fns
->next
= context_fns
->prev
= NULL
;
253 context_fns
->n_cmds
= rpc_srv_get_pipe_num_cmds(syntax
);
254 context_fns
->cmds
= rpc_srv_get_pipe_cmds(syntax
);
255 context_fns
->context_id
= 0;
256 context_fns
->syntax
= *syntax
;
258 /* add to the list of open contexts */
259 DLIST_ADD(p
->contexts
, context_fns
);
261 DEBUG(4,("Created internal pipe %s\n", pipe_name
));
266 static NTSTATUS
rpcint_dispatch(struct pipes_struct
*p
,
269 const DATA_BLOB
*in_data
,
272 struct pipe_rpc_fns
*fns
= find_pipe_fns_by_context(p
->contexts
, 0);
273 uint32_t num_cmds
= fns
->n_cmds
;
274 const struct api_struct
*cmds
= fns
->cmds
;
281 for (i
= 0; i
< num_cmds
; i
++) {
282 if (cmds
[i
].opnum
== opnum
&& cmds
[i
].fn
!= NULL
) {
288 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
;
291 p
->in_data
.data
= *in_data
;
292 p
->out_data
.rdata
= data_blob_null
;
295 p
->in_data
.data
= data_blob_null
;
297 data_blob_free(&p
->out_data
.rdata
);
298 talloc_free_children(p
->mem_ctx
);
299 return NT_STATUS_RPC_CALL_FAILED
;
302 if (p
->fault_state
) {
305 status
= NT_STATUS(p
->fault_state
);
307 data_blob_free(&p
->out_data
.rdata
);
308 talloc_free_children(p
->mem_ctx
);
312 *out_data
= p
->out_data
.rdata
;
313 talloc_steal(mem_ctx
, out_data
->data
);
314 p
->out_data
.rdata
= data_blob_null
;
316 talloc_free_children(p
->mem_ctx
);
320 struct rpcint_bh_state
{
321 struct pipes_struct
*p
;
324 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle
*h
)
326 struct rpcint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
327 struct rpcint_bh_state
);
336 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle
*h
,
339 /* TODO: implement timeouts */
343 struct rpcint_bh_raw_call_state
{
349 static struct tevent_req
*rpcint_bh_raw_call_send(TALLOC_CTX
*mem_ctx
,
350 struct tevent_context
*ev
,
351 struct dcerpc_binding_handle
*h
,
352 const struct GUID
*object
,
355 const uint8_t *in_data
,
358 struct rpcint_bh_state
*hs
=
359 dcerpc_binding_handle_data(h
,
360 struct rpcint_bh_state
);
361 struct tevent_req
*req
;
362 struct rpcint_bh_raw_call_state
*state
;
366 req
= tevent_req_create(mem_ctx
, &state
,
367 struct rpcint_bh_raw_call_state
);
371 state
->in_data
.data
= discard_const_p(uint8_t, in_data
);
372 state
->in_data
.length
= in_length
;
374 ok
= rpcint_bh_is_connected(h
);
376 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
377 return tevent_req_post(req
, ev
);
380 /* TODO: allow async */
381 status
= rpcint_dispatch(hs
->p
, state
, opnum
,
384 if (!NT_STATUS_IS_OK(status
)) {
385 tevent_req_nterror(req
, status
);
386 return tevent_req_post(req
, ev
);
389 tevent_req_done(req
);
390 return tevent_req_post(req
, ev
);
393 static NTSTATUS
rpcint_bh_raw_call_recv(struct tevent_req
*req
,
399 struct rpcint_bh_raw_call_state
*state
=
401 struct rpcint_bh_raw_call_state
);
404 if (tevent_req_is_nterror(req
, &status
)) {
405 tevent_req_received(req
);
409 *out_data
= talloc_move(mem_ctx
, &state
->out_data
.data
);
410 *out_length
= state
->out_data
.length
;
412 tevent_req_received(req
);
416 struct rpcint_bh_disconnect_state
{
420 static struct tevent_req
*rpcint_bh_disconnect_send(TALLOC_CTX
*mem_ctx
,
421 struct tevent_context
*ev
,
422 struct dcerpc_binding_handle
*h
)
424 struct rpcint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
425 struct rpcint_bh_state
);
426 struct tevent_req
*req
;
427 struct rpcint_bh_disconnect_state
*state
;
430 req
= tevent_req_create(mem_ctx
, &state
,
431 struct rpcint_bh_disconnect_state
);
436 ok
= rpcint_bh_is_connected(h
);
438 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
439 return tevent_req_post(req
, ev
);
443 * TODO: do a real async disconnect ...
445 * For now the caller needs to free pipes_struct
449 tevent_req_done(req
);
450 return tevent_req_post(req
, ev
);
453 static NTSTATUS
rpcint_bh_disconnect_recv(struct tevent_req
*req
)
457 if (tevent_req_is_nterror(req
, &status
)) {
458 tevent_req_received(req
);
462 tevent_req_received(req
);
466 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle
*h
)
471 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle
*h
,
473 const void *_struct_ptr
,
474 const struct ndr_interface_call
*call
)
476 void *struct_ptr
= discard_const(_struct_ptr
);
478 if (DEBUGLEVEL
< 11) {
482 if (ndr_flags
& NDR_IN
) {
483 ndr_print_function_debug(call
->ndr_print
,
488 if (ndr_flags
& NDR_OUT
) {
489 ndr_print_function_debug(call
->ndr_print
,
496 static const struct dcerpc_binding_handle_ops rpcint_bh_ops
= {
498 .is_connected
= rpcint_bh_is_connected
,
499 .set_timeout
= rpcint_bh_set_timeout
,
500 .raw_call_send
= rpcint_bh_raw_call_send
,
501 .raw_call_recv
= rpcint_bh_raw_call_recv
,
502 .disconnect_send
= rpcint_bh_disconnect_send
,
503 .disconnect_recv
= rpcint_bh_disconnect_recv
,
505 .ref_alloc
= rpcint_bh_ref_alloc
,
506 .do_ndr_print
= rpcint_bh_do_ndr_print
,
509 static NTSTATUS
rpcint_binding_handle_ex(TALLOC_CTX
*mem_ctx
,
510 const struct ndr_syntax_id
*abstract_syntax
,
511 const struct ndr_interface_table
*ndr_table
,
512 const struct tsocket_address
*remote_address
,
513 const struct tsocket_address
*local_address
,
514 const struct auth_session_info
*session_info
,
515 struct messaging_context
*msg_ctx
,
516 struct dcerpc_binding_handle
**binding_handle
)
518 struct dcerpc_binding_handle
*h
;
519 struct rpcint_bh_state
*hs
;
522 abstract_syntax
= &ndr_table
->syntax_id
;
525 h
= dcerpc_binding_handle_create(mem_ctx
,
530 struct rpcint_bh_state
,
533 return NT_STATUS_NO_MEMORY
;
535 hs
->p
= make_internal_rpc_pipe_p(hs
,
543 return NT_STATUS_NO_MEMORY
;
550 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
552 * @param[in] mem_ctx The memory context to use.
554 * @param[in] ndr_table Normally the ndr_table_<name>.
556 * @param[in] remote_address The info about the connected client.
558 * @param[in] serversupplied_info The server supplied authentication function.
560 * @param[in] msg_ctx The messaging context that can be used by the server
562 * @param[out] binding_handle A pointer to store the connected
563 * dcerpc_binding_handle
565 * @return NT_STATUS_OK on success, a corresponding NT status if an
569 * struct dcerpc_binding_handle *winreg_binding;
572 * status = rpcint_binding_handle(tmp_ctx,
580 NTSTATUS
rpcint_binding_handle(TALLOC_CTX
*mem_ctx
,
581 const struct ndr_interface_table
*ndr_table
,
582 const struct tsocket_address
*remote_address
,
583 const struct tsocket_address
*local_address
,
584 const struct auth_session_info
*session_info
,
585 struct messaging_context
*msg_ctx
,
586 struct dcerpc_binding_handle
**binding_handle
)
588 return rpcint_binding_handle_ex(mem_ctx
, NULL
, ndr_table
, remote_address
,
589 local_address
, session_info
,
590 msg_ctx
, binding_handle
);
596 * @brief Create a new RPC client context which uses a local transport.
598 * This creates a local transport. It is a shortcut to directly call the server
599 * functions and avoid marshalling.
600 * NOTE: this function should be used only by rpc_pipe_open_interface()
602 * @param[in] mem_ctx The memory context to use.
604 * @param[in] ndr_table the ndr_table_<name> structure.
606 * @param[in] serversupplied_info The server supplied authentication function.
608 * @param[in] remote_address The client address information.
610 * @param[in] msg_ctx The messaging context to use.
612 * @param[out] presult A pointer to store the connected rpc client pipe.
614 * @return NT_STATUS_OK on success, a corresponding NT status if an
617 NTSTATUS
rpc_pipe_open_internal(TALLOC_CTX
*mem_ctx
,
618 const struct ndr_interface_table
*ndr_table
,
619 const struct auth_session_info
*session_info
,
620 const struct tsocket_address
*remote_address
,
621 const struct tsocket_address
*local_address
,
622 struct messaging_context
*msg_ctx
,
623 struct rpc_pipe_client
**presult
)
625 struct rpc_pipe_client
*result
;
628 result
= talloc_zero(mem_ctx
, struct rpc_pipe_client
);
629 if (result
== NULL
) {
630 return NT_STATUS_NO_MEMORY
;
633 result
->abstract_syntax
= ndr_table
->syntax_id
;
634 result
->transfer_syntax
= ndr_transfer_syntax_ndr
;
636 if (remote_address
== NULL
) {
637 struct tsocket_address
*local
;
640 rc
= tsocket_address_inet_from_strings(mem_ctx
,
647 return NT_STATUS_NO_MEMORY
;
650 remote_address
= local
;
653 result
->max_xmit_frag
= -1;
655 status
= rpcint_binding_handle(result
,
661 &result
->binding_handle
);
662 if (!NT_STATUS_IS_OK(status
)) {
671 /****************************************************************************
672 * External pipes functions
673 ***************************************************************************/
675 NTSTATUS
make_external_rpc_pipe(TALLOC_CTX
*mem_ctx
,
676 const char *pipe_name
,
677 const struct tsocket_address
*remote_client_address
,
678 const struct tsocket_address
*local_server_address
,
679 const struct auth_session_info
*session_info
,
680 struct npa_state
**pnpa
)
682 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
683 struct auth_session_info_transport
*session_info_t
;
684 struct tevent_context
*ev_ctx
;
685 struct tevent_req
*subreq
;
686 const char *socket_np_dir
;
687 const char *socket_dir
;
688 struct npa_state
*npa
;
694 npa
= npa_state_init(tmp_ctx
);
696 status
= NT_STATUS_NO_MEMORY
;
700 socket_dir
= lp_parm_const_string(GLOBAL_SECTION_SNUM
,
704 if (socket_dir
== NULL
) {
705 DEBUG(0, ("external_rpc_pipe: socket_dir not set\n"));
706 status
= NT_STATUS_PIPE_NOT_AVAILABLE
;
710 socket_np_dir
= talloc_asprintf(tmp_ctx
, "%s/np", socket_dir
);
711 if (socket_np_dir
== NULL
) {
712 DEBUG(0, ("talloc_asprintf failed\n"));
713 status
= NT_STATUS_NO_MEMORY
;
717 session_info_t
= talloc_zero(tmp_ctx
,
718 struct auth_session_info_transport
);
719 if (session_info_t
== NULL
) {
720 DEBUG(0, ("talloc failed\n"));
721 status
= NT_STATUS_NO_MEMORY
;
725 session_info_t
->session_info
= copy_session_info(session_info_t
,
727 if (session_info_t
->session_info
== NULL
) {
728 DEBUG(0, ("copy_session_info failed\n"));
729 status
= NT_STATUS_NO_MEMORY
;
733 ev_ctx
= samba_tevent_context_init(tmp_ctx
);
734 if (ev_ctx
== NULL
) {
735 DEBUG(0, ("samba_tevent_context_init failed\n"));
736 status
= NT_STATUS_NO_MEMORY
;
741 subreq
= tstream_npa_connect_send(tmp_ctx
,
745 remote_client_address
,
746 NULL
, /* client_name */
747 local_server_address
,
748 NULL
, /* server_name */
750 if (subreq
== NULL
) {
752 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
753 "user %s\\%s failed\n",
754 socket_np_dir
, pipe_name
, session_info_t
->session_info
->info
->domain_name
,
755 session_info_t
->session_info
->info
->account_name
));
756 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
759 ok
= tevent_req_poll(subreq
, ev_ctx
);
762 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
763 "failed for tstream_npa_connect: %s\n",
766 session_info_t
->session_info
->info
->domain_name
,
767 session_info_t
->session_info
->info
->account_name
,
769 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
773 rc
= tstream_npa_connect_recv(subreq
,
779 &npa
->allocation_size
);
784 if (errno
== ENOENT
) {
788 DEBUG(l
, ("tstream_npa_connect_recv to %s for pipe %s and "
789 "user %s\\%s failed: %s\n",
792 session_info_t
->session_info
->info
->domain_name
,
793 session_info_t
->session_info
->info
->account_name
,
794 strerror(sys_errno
)));
795 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
799 *pnpa
= talloc_steal(mem_ctx
, npa
);
800 status
= NT_STATUS_OK
;
802 talloc_free(tmp_ctx
);
807 struct np_proxy_state
*make_external_rpc_pipe_p(TALLOC_CTX
*mem_ctx
,
808 const char *pipe_name
,
809 const struct tsocket_address
*remote_client_address
,
810 const struct tsocket_address
*local_server_address
,
811 const struct auth_session_info
*session_info
)
813 struct np_proxy_state
*result
;
815 const char *socket_dir
;
816 struct tevent_context
*ev
;
817 struct tevent_req
*subreq
;
818 struct auth_session_info_transport
*session_info_t
;
823 result
= talloc(mem_ctx
, struct np_proxy_state
);
824 if (result
== NULL
) {
825 DEBUG(0, ("talloc failed\n"));
829 result
->read_queue
= tevent_queue_create(result
, "np_read");
830 if (result
->read_queue
== NULL
) {
831 DEBUG(0, ("tevent_queue_create failed\n"));
835 result
->write_queue
= tevent_queue_create(result
, "np_write");
836 if (result
->write_queue
== NULL
) {
837 DEBUG(0, ("tevent_queue_create failed\n"));
841 ev
= samba_tevent_context_init(talloc_tos());
843 DEBUG(0, ("samba_tevent_context_init failed\n"));
847 socket_dir
= lp_parm_const_string(
848 GLOBAL_SECTION_SNUM
, "external_rpc_pipe", "socket_dir",
850 if (socket_dir
== NULL
) {
851 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
854 socket_np_dir
= talloc_asprintf(talloc_tos(), "%s/np", socket_dir
);
855 if (socket_np_dir
== NULL
) {
856 DEBUG(0, ("talloc_asprintf failed\n"));
860 session_info_t
= talloc_zero(talloc_tos(), struct auth_session_info_transport
);
861 if (session_info_t
== NULL
) {
862 DEBUG(0, ("talloc failed\n"));
866 session_info_t
->session_info
= copy_session_info(session_info_t
,
868 if (session_info_t
->session_info
== NULL
) {
869 DEBUG(0, ("copy_session_info failed\n"));
874 subreq
= tstream_npa_connect_send(talloc_tos(), ev
,
877 remote_client_address
,
878 NULL
, /* client_name */
879 local_server_address
,
880 NULL
, /* server_name */
882 if (subreq
== NULL
) {
884 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
885 "user %s\\%s failed\n",
886 socket_np_dir
, pipe_name
, session_info_t
->session_info
->info
->domain_name
,
887 session_info_t
->session_info
->info
->account_name
));
890 ok
= tevent_req_poll(subreq
, ev
);
893 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
894 "failed for tstream_npa_connect: %s\n",
895 socket_np_dir
, pipe_name
, session_info_t
->session_info
->info
->domain_name
,
896 session_info_t
->session_info
->info
->account_name
,
901 ret
= tstream_npa_connect_recv(subreq
, &sys_errno
,
905 &result
->device_state
,
906 &result
->allocation_size
);
910 if (errno
== ENOENT
) {
913 DEBUG(l
, ("tstream_npa_connect_recv to %s for pipe %s and "
914 "user %s\\%s failed: %s\n",
915 socket_np_dir
, pipe_name
, session_info_t
->session_info
->info
->domain_name
,
916 session_info_t
->session_info
->info
->account_name
,
917 strerror(sys_errno
)));
928 static NTSTATUS
rpc_pipe_open_external(TALLOC_CTX
*mem_ctx
,
929 const char *pipe_name
,
930 const struct ndr_interface_table
*table
,
931 const struct auth_session_info
*session_info
,
932 const struct tsocket_address
*remote_client_address
,
933 const struct tsocket_address
*local_server_address
,
934 struct rpc_pipe_client
**_result
)
936 struct rpc_pipe_client
*result
= NULL
;
937 struct np_proxy_state
*proxy_state
= NULL
;
938 struct pipe_auth_data
*auth
;
939 struct tsocket_address
*remote_client_addr
;
940 struct tsocket_address
*local_server_addr
;
944 if (local_server_address
== NULL
) {
945 /* this is an internal connection, fake up ip addresses */
946 ret
= tsocket_address_inet_from_strings(talloc_tos(), "ip",
947 NULL
, 0, &local_server_addr
);
949 return NT_STATUS_NO_MEMORY
;
951 local_server_address
= local_server_addr
;
954 if (remote_client_address
== NULL
) {
955 /* this is an internal connection, fake up ip addresses */
956 ret
= tsocket_address_inet_from_strings(talloc_tos(), "ip",
957 NULL
, 0, &remote_client_addr
);
959 return NT_STATUS_NO_MEMORY
;
961 remote_client_address
= remote_client_addr
;
964 proxy_state
= make_external_rpc_pipe_p(mem_ctx
, pipe_name
,
965 remote_client_address
,
966 local_server_address
,
969 DEBUG(1, ("Unable to make proxy_state for connection to %s.\n", pipe_name
));
970 return NT_STATUS_UNSUCCESSFUL
;
973 result
= talloc_zero(mem_ctx
, struct rpc_pipe_client
);
974 if (result
== NULL
) {
975 status
= NT_STATUS_NO_MEMORY
;
979 result
->abstract_syntax
= table
->syntax_id
;
980 result
->transfer_syntax
= ndr_transfer_syntax_ndr
;
982 result
->desthost
= get_myname(result
);
983 result
->srv_name_slash
= talloc_asprintf_strupper_m(
984 result
, "\\\\%s", result
->desthost
);
985 if ((result
->desthost
== NULL
) || (result
->srv_name_slash
== NULL
)) {
986 status
= NT_STATUS_NO_MEMORY
;
990 result
->max_xmit_frag
= RPC_MAX_PDU_FRAG_LEN
;
992 status
= rpc_transport_tstream_init(result
,
995 if (!NT_STATUS_IS_OK(status
)) {
999 result
->binding_handle
= rpccli_bh_create(result
, NULL
, table
);
1000 if (result
->binding_handle
== NULL
) {
1001 status
= NT_STATUS_NO_MEMORY
;
1002 DEBUG(0, ("Failed to create binding handle.\n"));
1006 result
->auth
= talloc_zero(result
, struct pipe_auth_data
);
1007 if (!result
->auth
) {
1008 status
= NT_STATUS_NO_MEMORY
;
1011 result
->auth
->auth_type
= DCERPC_AUTH_TYPE_NONE
;
1012 result
->auth
->auth_level
= DCERPC_AUTH_LEVEL_NONE
;
1013 result
->auth
->auth_context_id
= 0;
1015 status
= rpccli_anon_bind_data(result
, &auth
);
1016 if (!NT_STATUS_IS_OK(status
)) {
1017 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
1021 status
= rpc_pipe_bind(result
, auth
);
1022 if (!NT_STATUS_IS_OK(status
)) {
1023 DEBUG(0, ("Failed to bind external pipe.\n"));
1028 if (!NT_STATUS_IS_OK(status
)) {
1029 TALLOC_FREE(result
);
1031 TALLOC_FREE(proxy_state
);
1037 * @brief Create a new RPC client context which uses a local dispatch function
1038 * or a remote transport, depending on rpc_server configuration for the
1041 * @param[in] mem_ctx The memory context to use.
1043 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
1046 * @param[in] serversupplied_info The server supplied authentication function.
1048 * @param[in] remote_address The client address information.
1050 * @param[in] msg_ctx The messaging context to use.
1052 * @param[out] presult A pointer to store the connected rpc client pipe.
1054 * @return NT_STATUS_OK on success, a corresponding NT status if an
1058 * struct rpc_pipe_client *winreg_pipe;
1061 * status = rpc_pipe_open_interface(tmp_ctx,
1062 * &ndr_table_winreg.syntax_id,
1069 NTSTATUS
rpc_pipe_open_interface(TALLOC_CTX
*mem_ctx
,
1070 const struct ndr_interface_table
*table
,
1071 const struct auth_session_info
*session_info
,
1072 const struct tsocket_address
*remote_address
,
1073 const struct tsocket_address
*local_address
,
1074 struct messaging_context
*msg_ctx
,
1075 struct rpc_pipe_client
**cli_pipe
)
1077 struct rpc_pipe_client
*cli
= NULL
;
1078 enum rpc_service_mode_e pipe_mode
;
1079 const char *pipe_name
;
1081 TALLOC_CTX
*tmp_ctx
;
1083 if (cli_pipe
!= NULL
) {
1084 if (rpccli_is_connected(*cli_pipe
)) {
1085 return NT_STATUS_OK
;
1087 TALLOC_FREE(*cli_pipe
);
1091 tmp_ctx
= talloc_stackframe();
1092 if (tmp_ctx
== NULL
) {
1093 return NT_STATUS_NO_MEMORY
;
1096 pipe_name
= dcerpc_default_transport_endpoint(mem_ctx
, NCACN_NP
, table
);
1097 if (pipe_name
== NULL
) {
1098 DEBUG(1, ("Unable to find pipe name to forward %s to.\n", table
->name
));
1099 status
= NT_STATUS_INVALID_PARAMETER
;
1103 while (pipe_name
[0] == '\\') {
1107 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name
));
1109 pipe_mode
= rpc_service_mode(pipe_name
);
1111 switch (pipe_mode
) {
1112 case RPC_SERVICE_MODE_EMBEDDED
:
1113 status
= rpc_pipe_open_internal(tmp_ctx
,
1114 table
, session_info
,
1115 remote_address
, local_address
,
1118 if (!NT_STATUS_IS_OK(status
)) {
1122 case RPC_SERVICE_MODE_EXTERNAL
:
1123 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
1124 * for now we need to use the special proxy setup to connect
1127 status
= rpc_pipe_open_external(tmp_ctx
,
1130 remote_address
, local_address
,
1132 if (!NT_STATUS_IS_OK(status
)) {
1136 case RPC_SERVICE_MODE_DISABLED
:
1137 status
= NT_STATUS_NOT_IMPLEMENTED
;
1138 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
1139 pipe_name
, nt_errstr(status
)));
1143 status
= NT_STATUS_OK
;
1145 if (NT_STATUS_IS_OK(status
) && cli_pipe
!= NULL
) {
1146 *cli_pipe
= talloc_move(mem_ctx
, &cli
);
1148 TALLOC_FREE(tmp_ctx
);