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"
40 #define DBGC_CLASS DBGC_RPC_SRV
42 /****************************************************************************
43 Make an internal namedpipes structure
44 ****************************************************************************/
46 struct pipes_struct
*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 auth_session_info
*session_info
,
50 struct messaging_context
*msg_ctx
)
52 struct pipes_struct
*p
;
53 struct pipe_rpc_fns
*context_fns
;
54 const char *pipe_name
;
57 pipe_name
= get_pipe_name_from_syntax(talloc_tos(), syntax
);
59 DEBUG(4,("Create pipe requested %s\n", pipe_name
));
61 ret
= make_base_pipes_struct(mem_ctx
, msg_ctx
, pipe_name
,
62 NCALRPC
, RPC_LITTLE_ENDIAN
, false,
63 remote_address
, NULL
, &p
);
65 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
69 if (!init_pipe_handles(p
, syntax
)) {
70 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
75 p
->session_info
= copy_session_info(p
, session_info
);
76 if (p
->session_info
== NULL
) {
77 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
78 close_policy_by_pipe(p
);
83 context_fns
= SMB_MALLOC_P(struct pipe_rpc_fns
);
84 if (context_fns
== NULL
) {
85 DEBUG(0,("malloc() failed!\n"));
89 context_fns
->next
= context_fns
->prev
= NULL
;
90 context_fns
->n_cmds
= rpc_srv_get_pipe_num_cmds(syntax
);
91 context_fns
->cmds
= rpc_srv_get_pipe_cmds(syntax
);
92 context_fns
->context_id
= 0;
93 context_fns
->syntax
= *syntax
;
95 /* add to the list of open contexts */
96 DLIST_ADD(p
->contexts
, context_fns
);
98 DEBUG(4,("Created internal pipe %s\n", pipe_name
));
103 static NTSTATUS
rpcint_dispatch(struct pipes_struct
*p
,
106 const DATA_BLOB
*in_data
,
109 struct pipe_rpc_fns
*fns
= find_pipe_fns_by_context(p
->contexts
, 0);
110 uint32_t num_cmds
= fns
->n_cmds
;
111 const struct api_struct
*cmds
= fns
->cmds
;
118 for (i
= 0; i
< num_cmds
; i
++) {
119 if (cmds
[i
].opnum
== opnum
&& cmds
[i
].fn
!= NULL
) {
125 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
;
128 p
->in_data
.data
= *in_data
;
129 p
->out_data
.rdata
= data_blob_null
;
132 p
->in_data
.data
= data_blob_null
;
134 data_blob_free(&p
->out_data
.rdata
);
135 talloc_free_children(p
->mem_ctx
);
136 return NT_STATUS_RPC_CALL_FAILED
;
139 if (p
->fault_state
) {
140 p
->fault_state
= false;
141 data_blob_free(&p
->out_data
.rdata
);
142 talloc_free_children(p
->mem_ctx
);
143 return NT_STATUS_RPC_CALL_FAILED
;
146 if (p
->bad_handle_fault_state
) {
147 p
->bad_handle_fault_state
= false;
148 data_blob_free(&p
->out_data
.rdata
);
149 talloc_free_children(p
->mem_ctx
);
150 return NT_STATUS_RPC_SS_CONTEXT_MISMATCH
;
153 if (p
->rng_fault_state
) {
154 p
->rng_fault_state
= false;
155 data_blob_free(&p
->out_data
.rdata
);
156 talloc_free_children(p
->mem_ctx
);
157 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
;
160 *out_data
= p
->out_data
.rdata
;
161 talloc_steal(mem_ctx
, out_data
->data
);
162 p
->out_data
.rdata
= data_blob_null
;
164 talloc_free_children(p
->mem_ctx
);
168 struct rpcint_bh_state
{
169 struct pipes_struct
*p
;
172 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle
*h
)
174 struct rpcint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
175 struct rpcint_bh_state
);
184 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle
*h
,
187 /* TODO: implement timeouts */
191 struct rpcint_bh_raw_call_state
{
197 static struct tevent_req
*rpcint_bh_raw_call_send(TALLOC_CTX
*mem_ctx
,
198 struct tevent_context
*ev
,
199 struct dcerpc_binding_handle
*h
,
200 const struct GUID
*object
,
203 const uint8_t *in_data
,
206 struct rpcint_bh_state
*hs
=
207 dcerpc_binding_handle_data(h
,
208 struct rpcint_bh_state
);
209 struct tevent_req
*req
;
210 struct rpcint_bh_raw_call_state
*state
;
214 req
= tevent_req_create(mem_ctx
, &state
,
215 struct rpcint_bh_raw_call_state
);
219 state
->in_data
.data
= discard_const_p(uint8_t, in_data
);
220 state
->in_data
.length
= in_length
;
222 ok
= rpcint_bh_is_connected(h
);
224 tevent_req_nterror(req
, NT_STATUS_INVALID_CONNECTION
);
225 return tevent_req_post(req
, ev
);
228 /* TODO: allow async */
229 status
= rpcint_dispatch(hs
->p
, state
, opnum
,
232 if (!NT_STATUS_IS_OK(status
)) {
233 tevent_req_nterror(req
, status
);
234 return tevent_req_post(req
, ev
);
237 tevent_req_done(req
);
238 return tevent_req_post(req
, ev
);
241 static NTSTATUS
rpcint_bh_raw_call_recv(struct tevent_req
*req
,
247 struct rpcint_bh_raw_call_state
*state
=
249 struct rpcint_bh_raw_call_state
);
252 if (tevent_req_is_nterror(req
, &status
)) {
253 tevent_req_received(req
);
257 *out_data
= talloc_move(mem_ctx
, &state
->out_data
.data
);
258 *out_length
= state
->out_data
.length
;
260 tevent_req_received(req
);
264 struct rpcint_bh_disconnect_state
{
268 static struct tevent_req
*rpcint_bh_disconnect_send(TALLOC_CTX
*mem_ctx
,
269 struct tevent_context
*ev
,
270 struct dcerpc_binding_handle
*h
)
272 struct rpcint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
273 struct rpcint_bh_state
);
274 struct tevent_req
*req
;
275 struct rpcint_bh_disconnect_state
*state
;
278 req
= tevent_req_create(mem_ctx
, &state
,
279 struct rpcint_bh_disconnect_state
);
284 ok
= rpcint_bh_is_connected(h
);
286 tevent_req_nterror(req
, NT_STATUS_INVALID_CONNECTION
);
287 return tevent_req_post(req
, ev
);
291 * TODO: do a real async disconnect ...
293 * For now the caller needs to free pipes_struct
297 tevent_req_done(req
);
298 return tevent_req_post(req
, ev
);
301 static NTSTATUS
rpcint_bh_disconnect_recv(struct tevent_req
*req
)
305 if (tevent_req_is_nterror(req
, &status
)) {
306 tevent_req_received(req
);
310 tevent_req_received(req
);
314 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle
*h
)
319 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle
*h
,
321 const void *_struct_ptr
,
322 const struct ndr_interface_call
*call
)
324 void *struct_ptr
= discard_const(_struct_ptr
);
326 if (DEBUGLEVEL
< 11) {
330 if (ndr_flags
& NDR_IN
) {
331 ndr_print_function_debug(call
->ndr_print
,
336 if (ndr_flags
& NDR_OUT
) {
337 ndr_print_function_debug(call
->ndr_print
,
344 static const struct dcerpc_binding_handle_ops rpcint_bh_ops
= {
346 .is_connected
= rpcint_bh_is_connected
,
347 .set_timeout
= rpcint_bh_set_timeout
,
348 .raw_call_send
= rpcint_bh_raw_call_send
,
349 .raw_call_recv
= rpcint_bh_raw_call_recv
,
350 .disconnect_send
= rpcint_bh_disconnect_send
,
351 .disconnect_recv
= rpcint_bh_disconnect_recv
,
353 .ref_alloc
= rpcint_bh_ref_alloc
,
354 .do_ndr_print
= rpcint_bh_do_ndr_print
,
357 static NTSTATUS
rpcint_binding_handle_ex(TALLOC_CTX
*mem_ctx
,
358 const struct ndr_syntax_id
*abstract_syntax
,
359 const struct ndr_interface_table
*ndr_table
,
360 const struct tsocket_address
*remote_address
,
361 const struct auth_session_info
*session_info
,
362 struct messaging_context
*msg_ctx
,
363 struct dcerpc_binding_handle
**binding_handle
)
365 struct dcerpc_binding_handle
*h
;
366 struct rpcint_bh_state
*hs
;
369 abstract_syntax
= &ndr_table
->syntax_id
;
372 h
= dcerpc_binding_handle_create(mem_ctx
,
377 struct rpcint_bh_state
,
380 return NT_STATUS_NO_MEMORY
;
382 hs
->p
= make_internal_rpc_pipe_p(hs
,
389 return NT_STATUS_NO_MEMORY
;
396 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
398 * @param[in] mem_ctx The memory context to use.
400 * @param[in] ndr_table Normally the ndr_table_<name>.
402 * @param[in] remote_address The info about the connected client.
404 * @param[in] serversupplied_info The server supplied authentication function.
406 * @param[in] msg_ctx The messaging context that can be used by the server
408 * @param[out] binding_handle A pointer to store the connected
409 * dcerpc_binding_handle
411 * @return NT_STATUS_OK on success, a corresponding NT status if an
415 * struct dcerpc_binding_handle *winreg_binding;
418 * status = rpcint_binding_handle(tmp_ctx,
426 NTSTATUS
rpcint_binding_handle(TALLOC_CTX
*mem_ctx
,
427 const struct ndr_interface_table
*ndr_table
,
428 const struct tsocket_address
*remote_address
,
429 const struct auth_session_info
*session_info
,
430 struct messaging_context
*msg_ctx
,
431 struct dcerpc_binding_handle
**binding_handle
)
433 return rpcint_binding_handle_ex(mem_ctx
, NULL
, ndr_table
, remote_address
,
434 session_info
, msg_ctx
, binding_handle
);
440 * @brief Create a new RPC client context which uses a local transport.
442 * This creates a local transport. It is a shortcut to directly call the server
443 * functions and avoid marshalling.
444 * NOTE: this function should be used only by rpc_pipe_open_interface()
446 * @param[in] mem_ctx The memory context to use.
448 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
451 * @param[in] serversupplied_info The server supplied authentication function.
453 * @param[in] remote_address The client address information.
455 * @param[in] msg_ctx The messaging context to use.
457 * @param[out] presult A pointer to store the connected rpc client pipe.
459 * @return NT_STATUS_OK on success, a corresponding NT status if an
462 static NTSTATUS
rpc_pipe_open_internal(TALLOC_CTX
*mem_ctx
,
463 const struct ndr_syntax_id
*abstract_syntax
,
464 const struct auth_session_info
*session_info
,
465 const struct tsocket_address
*remote_address
,
466 struct messaging_context
*msg_ctx
,
467 struct rpc_pipe_client
**presult
)
469 struct rpc_pipe_client
*result
;
472 result
= talloc_zero(mem_ctx
, struct rpc_pipe_client
);
473 if (result
== NULL
) {
474 return NT_STATUS_NO_MEMORY
;
477 result
->abstract_syntax
= *abstract_syntax
;
478 result
->transfer_syntax
= ndr_transfer_syntax
;
480 if (remote_address
== NULL
) {
481 struct tsocket_address
*local
;
484 rc
= tsocket_address_inet_from_strings(mem_ctx
,
491 return NT_STATUS_NO_MEMORY
;
494 remote_address
= local
;
497 result
->max_xmit_frag
= -1;
498 result
->max_recv_frag
= -1;
500 status
= rpcint_binding_handle_ex(result
,
506 &result
->binding_handle
);
507 if (!NT_STATUS_IS_OK(status
)) {
516 /****************************************************************************
517 * External pipes functions
518 ***************************************************************************/
521 struct np_proxy_state
*make_external_rpc_pipe_p(TALLOC_CTX
*mem_ctx
,
522 const char *pipe_name
,
523 const struct tsocket_address
*local_address
,
524 const struct tsocket_address
*remote_address
,
525 const struct auth_session_info
*session_info
)
527 struct np_proxy_state
*result
;
529 const char *socket_dir
;
530 struct tevent_context
*ev
;
531 struct tevent_req
*subreq
;
532 struct auth_session_info_transport
*session_info_t
;
537 result
= talloc(mem_ctx
, struct np_proxy_state
);
538 if (result
== NULL
) {
539 DEBUG(0, ("talloc failed\n"));
543 result
->read_queue
= tevent_queue_create(result
, "np_read");
544 if (result
->read_queue
== NULL
) {
545 DEBUG(0, ("tevent_queue_create failed\n"));
549 result
->write_queue
= tevent_queue_create(result
, "np_write");
550 if (result
->write_queue
== NULL
) {
551 DEBUG(0, ("tevent_queue_create failed\n"));
555 ev
= s3_tevent_context_init(talloc_tos());
557 DEBUG(0, ("s3_tevent_context_init failed\n"));
561 socket_dir
= lp_parm_const_string(
562 GLOBAL_SECTION_SNUM
, "external_rpc_pipe", "socket_dir",
564 if (socket_dir
== NULL
) {
565 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
568 socket_np_dir
= talloc_asprintf(talloc_tos(), "%s/np", socket_dir
);
569 if (socket_np_dir
== NULL
) {
570 DEBUG(0, ("talloc_asprintf failed\n"));
574 session_info_t
= talloc_zero(talloc_tos(), struct auth_session_info_transport
);
575 if (session_info_t
== NULL
) {
576 DEBUG(0, ("talloc failed\n"));
580 session_info_t
->session_info
= copy_session_info(session_info_t
,
582 if (session_info_t
->session_info
== NULL
) {
583 DEBUG(0, ("copy_session_info failed\n"));
588 subreq
= tstream_npa_connect_send(talloc_tos(), ev
,
591 remote_address
, /* client_addr */
592 NULL
, /* client_name */
593 local_address
, /* server_addr */
594 NULL
, /* server_name */
596 if (subreq
== NULL
) {
598 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
599 "user %s\\%s failed\n",
600 socket_np_dir
, pipe_name
, session_info_t
->session_info
->info
->domain_name
,
601 session_info_t
->session_info
->info
->account_name
));
604 ok
= tevent_req_poll(subreq
, ev
);
607 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
608 "failed for tstream_npa_connect: %s\n",
609 socket_np_dir
, pipe_name
, session_info_t
->session_info
->info
->domain_name
,
610 session_info_t
->session_info
->info
->account_name
,
615 ret
= tstream_npa_connect_recv(subreq
, &sys_errno
,
619 &result
->device_state
,
620 &result
->allocation_size
);
623 DEBUG(0, ("tstream_npa_connect_recv to %s for pipe %s and "
624 "user %s\\%s failed: %s\n",
625 socket_np_dir
, pipe_name
, session_info_t
->session_info
->info
->domain_name
,
626 session_info_t
->session_info
->info
->account_name
,
627 strerror(sys_errno
)));
638 static NTSTATUS
rpc_pipe_open_external(TALLOC_CTX
*mem_ctx
,
639 const char *pipe_name
,
640 const struct ndr_syntax_id
*abstract_syntax
,
641 const struct auth_session_info
*session_info
,
642 struct rpc_pipe_client
**_result
)
644 struct tsocket_address
*local
, *remote
;
645 struct rpc_pipe_client
*result
= NULL
;
646 struct np_proxy_state
*proxy_state
= NULL
;
647 struct pipe_auth_data
*auth
;
651 /* this is an internal connection, fake up ip addresses */
652 ret
= tsocket_address_inet_from_strings(talloc_tos(), "ip",
655 return NT_STATUS_NO_MEMORY
;
657 ret
= tsocket_address_inet_from_strings(talloc_tos(), "ip",
660 return NT_STATUS_NO_MEMORY
;
663 proxy_state
= make_external_rpc_pipe_p(mem_ctx
, pipe_name
,
664 local
, remote
, session_info
);
666 return NT_STATUS_UNSUCCESSFUL
;
669 result
= talloc_zero(mem_ctx
, struct rpc_pipe_client
);
670 if (result
== NULL
) {
671 status
= NT_STATUS_NO_MEMORY
;
675 result
->abstract_syntax
= *abstract_syntax
;
676 result
->transfer_syntax
= ndr_transfer_syntax
;
678 result
->desthost
= get_myname(result
);
679 result
->srv_name_slash
= talloc_asprintf_strupper_m(
680 result
, "\\\\%s", result
->desthost
);
681 if ((result
->desthost
== NULL
) || (result
->srv_name_slash
== NULL
)) {
682 status
= NT_STATUS_NO_MEMORY
;
686 result
->max_xmit_frag
= RPC_MAX_PDU_FRAG_LEN
;
687 result
->max_recv_frag
= RPC_MAX_PDU_FRAG_LEN
;
689 status
= rpc_transport_tstream_init(result
,
692 if (!NT_STATUS_IS_OK(status
)) {
696 result
->binding_handle
= rpccli_bh_create(result
);
697 if (result
->binding_handle
== NULL
) {
698 status
= NT_STATUS_NO_MEMORY
;
699 DEBUG(0, ("Failed to create binding handle.\n"));
703 result
->auth
= talloc_zero(result
, struct pipe_auth_data
);
705 status
= NT_STATUS_NO_MEMORY
;
708 result
->auth
->auth_type
= DCERPC_AUTH_TYPE_NONE
;
709 result
->auth
->auth_level
= DCERPC_AUTH_LEVEL_NONE
;
711 status
= rpccli_anon_bind_data(result
, &auth
);
712 if (!NT_STATUS_IS_OK(status
)) {
713 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
717 status
= rpc_pipe_bind(result
, auth
);
718 if (!NT_STATUS_IS_OK(status
)) {
719 DEBUG(0, ("Failed to bind external pipe.\n"));
724 if (!NT_STATUS_IS_OK(status
)) {
727 TALLOC_FREE(proxy_state
);
733 * @brief Create a new RPC client context which uses a local dispatch function
734 * or a remote transport, depending on rpc_server configuration for the
737 * @param[in] mem_ctx The memory context to use.
739 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
742 * @param[in] serversupplied_info The server supplied authentication function.
744 * @param[in] remote_address The client address information.
746 * @param[in] msg_ctx The messaging context to use.
748 * @param[out] presult A pointer to store the connected rpc client pipe.
750 * @return NT_STATUS_OK on success, a corresponding NT status if an
754 * struct rpc_pipe_client *winreg_pipe;
757 * status = rpc_pipe_open_interface(tmp_ctx,
758 * &ndr_table_winreg.syntax_id,
765 NTSTATUS
rpc_pipe_open_interface(TALLOC_CTX
*mem_ctx
,
766 const struct ndr_syntax_id
*syntax
,
767 const struct auth_session_info
*session_info
,
768 const struct tsocket_address
*remote_address
,
769 struct messaging_context
*msg_ctx
,
770 struct rpc_pipe_client
**cli_pipe
)
772 struct rpc_pipe_client
*cli
= NULL
;
773 const char *server_type
;
774 const char *pipe_name
;
778 if (cli_pipe
&& rpccli_is_connected(*cli_pipe
)) {
781 TALLOC_FREE(*cli_pipe
);
784 tmp_ctx
= talloc_stackframe();
785 if (tmp_ctx
== NULL
) {
786 return NT_STATUS_NO_MEMORY
;
789 pipe_name
= get_pipe_name_from_syntax(tmp_ctx
, syntax
);
790 if (pipe_name
== NULL
) {
791 status
= NT_STATUS_INVALID_PARAMETER
;
795 while (pipe_name
[0] == '\\') {
799 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name
));
801 server_type
= lp_parm_const_string(GLOBAL_SECTION_SNUM
,
802 "rpc_server", pipe_name
,
805 if (strcasecmp_m(server_type
, "embedded") == 0) {
806 status
= rpc_pipe_open_internal(tmp_ctx
,
807 syntax
, session_info
,
808 remote_address
, msg_ctx
,
810 if (!NT_STATUS_IS_OK(status
)) {
813 } else if (strcasecmp_m(server_type
, "daemon") == 0 ||
814 strcasecmp_m(server_type
, "external") == 0) {
815 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
816 * for now we need to use the special proxy setup to connect
819 status
= rpc_pipe_open_external(tmp_ctx
,
823 if (!NT_STATUS_IS_OK(status
)) {
827 status
= NT_STATUS_NOT_IMPLEMENTED
;
828 DEBUG(0, ("Wrong servertype specified in config file: %s",
833 status
= NT_STATUS_OK
;
835 if (NT_STATUS_IS_OK(status
)) {
836 *cli_pipe
= talloc_move(mem_ctx
, &cli
);
838 TALLOC_FREE(tmp_ctx
);