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"
42 #define DBGC_CLASS DBGC_RPC_SRV
44 static struct npa_state *npa_state_init(TALLOC_CTX *mem_ctx)
46 struct npa_state *npa;
48 npa = talloc_zero(mem_ctx, struct npa_state);
53 npa->read_queue = tevent_queue_create(npa, "npa_cli_read");
54 if (npa->read_queue == NULL) {
55 DEBUG(0, ("tevent_queue_create failed\n"));
59 npa->write_queue = tevent_queue_create(npa, "npa_cli_write");
60 if (npa->write_queue == NULL) {
61 DEBUG(0, ("tevent_queue_create failed\n"));
71 /****************************************************************************
72 Make an internal namedpipes structure
73 ****************************************************************************/
75 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
76 const struct ndr_syntax_id *syntax,
77 const struct tsocket_address *remote_address,
78 const struct auth_session_info *session_info,
79 struct messaging_context *msg_ctx)
81 struct pipes_struct *p;
82 struct pipe_rpc_fns *context_fns;
83 const char *pipe_name;
85 const struct ndr_interface_table *table;
87 table = ndr_table_by_uuid(&syntax->uuid);
89 DEBUG(0,("unknown interface\n"));
93 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
95 DEBUG(4,("Create pipe requested %s\n", pipe_name));
97 ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
98 NCALRPC, RPC_LITTLE_ENDIAN, false,
99 remote_address, NULL, &p);
101 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
105 if (!init_pipe_handles(p, syntax)) {
106 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
111 p->session_info = copy_session_info(p, session_info);
112 if (p->session_info == NULL) {
113 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
114 close_policy_by_pipe(p);
119 context_fns = talloc(p, struct pipe_rpc_fns);
120 if (context_fns == NULL) {
121 DEBUG(0,("talloc() failed!\n"));
126 context_fns->next = context_fns->prev = NULL;
127 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
128 context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
129 context_fns->context_id = 0;
130 context_fns->syntax = *syntax;
132 /* add to the list of open contexts */
133 DLIST_ADD(p->contexts, context_fns);
135 DEBUG(4,("Created internal pipe %s\n", pipe_name));
140 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
143 const DATA_BLOB *in_data,
146 struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
147 uint32_t num_cmds = fns->n_cmds;
148 const struct api_struct *cmds = fns->cmds;
155 for (i = 0; i < num_cmds; i++) {
156 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
162 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
165 p->in_data.data = *in_data;
166 p->out_data.rdata = data_blob_null;
169 p->in_data.data = data_blob_null;
171 data_blob_free(&p->out_data.rdata);
172 talloc_free_children(p->mem_ctx);
173 return NT_STATUS_RPC_CALL_FAILED;
176 if (p->fault_state) {
179 status = NT_STATUS(p->fault_state);
181 data_blob_free(&p->out_data.rdata);
182 talloc_free_children(p->mem_ctx);
186 *out_data = p->out_data.rdata;
187 talloc_steal(mem_ctx, out_data->data);
188 p->out_data.rdata = data_blob_null;
190 talloc_free_children(p->mem_ctx);
194 struct rpcint_bh_state {
195 struct pipes_struct *p;
198 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
200 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
201 struct rpcint_bh_state);
210 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
213 /* TODO: implement timeouts */
217 struct rpcint_bh_raw_call_state {
223 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
224 struct tevent_context *ev,
225 struct dcerpc_binding_handle *h,
226 const struct GUID *object,
229 const uint8_t *in_data,
232 struct rpcint_bh_state *hs =
233 dcerpc_binding_handle_data(h,
234 struct rpcint_bh_state);
235 struct tevent_req *req;
236 struct rpcint_bh_raw_call_state *state;
240 req = tevent_req_create(mem_ctx, &state,
241 struct rpcint_bh_raw_call_state);
245 state->in_data.data = discard_const_p(uint8_t, in_data);
246 state->in_data.length = in_length;
248 ok = rpcint_bh_is_connected(h);
250 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
251 return tevent_req_post(req, ev);
254 /* TODO: allow async */
255 status = rpcint_dispatch(hs->p, state, opnum,
258 if (!NT_STATUS_IS_OK(status)) {
259 tevent_req_nterror(req, status);
260 return tevent_req_post(req, ev);
263 tevent_req_done(req);
264 return tevent_req_post(req, ev);
267 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
273 struct rpcint_bh_raw_call_state *state =
275 struct rpcint_bh_raw_call_state);
278 if (tevent_req_is_nterror(req, &status)) {
279 tevent_req_received(req);
283 *out_data = talloc_move(mem_ctx, &state->out_data.data);
284 *out_length = state->out_data.length;
286 tevent_req_received(req);
290 struct rpcint_bh_disconnect_state {
294 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
295 struct tevent_context *ev,
296 struct dcerpc_binding_handle *h)
298 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
299 struct rpcint_bh_state);
300 struct tevent_req *req;
301 struct rpcint_bh_disconnect_state *state;
304 req = tevent_req_create(mem_ctx, &state,
305 struct rpcint_bh_disconnect_state);
310 ok = rpcint_bh_is_connected(h);
312 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
313 return tevent_req_post(req, ev);
317 * TODO: do a real async disconnect ...
319 * For now the caller needs to free pipes_struct
323 tevent_req_done(req);
324 return tevent_req_post(req, ev);
327 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
331 if (tevent_req_is_nterror(req, &status)) {
332 tevent_req_received(req);
336 tevent_req_received(req);
340 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
345 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
347 const void *_struct_ptr,
348 const struct ndr_interface_call *call)
350 void *struct_ptr = discard_const(_struct_ptr);
352 if (DEBUGLEVEL < 11) {
356 if (ndr_flags & NDR_IN) {
357 ndr_print_function_debug(call->ndr_print,
362 if (ndr_flags & NDR_OUT) {
363 ndr_print_function_debug(call->ndr_print,
370 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
372 .is_connected = rpcint_bh_is_connected,
373 .set_timeout = rpcint_bh_set_timeout,
374 .raw_call_send = rpcint_bh_raw_call_send,
375 .raw_call_recv = rpcint_bh_raw_call_recv,
376 .disconnect_send = rpcint_bh_disconnect_send,
377 .disconnect_recv = rpcint_bh_disconnect_recv,
379 .ref_alloc = rpcint_bh_ref_alloc,
380 .do_ndr_print = rpcint_bh_do_ndr_print,
383 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
384 const struct ndr_syntax_id *abstract_syntax,
385 const struct ndr_interface_table *ndr_table,
386 const struct tsocket_address *remote_address,
387 const struct auth_session_info *session_info,
388 struct messaging_context *msg_ctx,
389 struct dcerpc_binding_handle **binding_handle)
391 struct dcerpc_binding_handle *h;
392 struct rpcint_bh_state *hs;
395 abstract_syntax = &ndr_table->syntax_id;
398 h = dcerpc_binding_handle_create(mem_ctx,
403 struct rpcint_bh_state,
406 return NT_STATUS_NO_MEMORY;
408 hs->p = make_internal_rpc_pipe_p(hs,
415 return NT_STATUS_NO_MEMORY;
422 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
424 * @param[in] mem_ctx The memory context to use.
426 * @param[in] ndr_table Normally the ndr_table_<name>.
428 * @param[in] remote_address The info about the connected client.
430 * @param[in] serversupplied_info The server supplied authentication function.
432 * @param[in] msg_ctx The messaging context that can be used by the server
434 * @param[out] binding_handle A pointer to store the connected
435 * dcerpc_binding_handle
437 * @return NT_STATUS_OK on success, a corresponding NT status if an
441 * struct dcerpc_binding_handle *winreg_binding;
444 * status = rpcint_binding_handle(tmp_ctx,
452 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
453 const struct ndr_interface_table *ndr_table,
454 const struct tsocket_address *remote_address,
455 const struct auth_session_info *session_info,
456 struct messaging_context *msg_ctx,
457 struct dcerpc_binding_handle **binding_handle)
459 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
460 session_info, msg_ctx, binding_handle);
466 * @brief Create a new RPC client context which uses a local transport.
468 * This creates a local transport. It is a shortcut to directly call the server
469 * functions and avoid marshalling.
470 * NOTE: this function should be used only by rpc_pipe_open_interface()
472 * @param[in] mem_ctx The memory context to use.
474 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
477 * @param[in] serversupplied_info The server supplied authentication function.
479 * @param[in] remote_address The client address information.
481 * @param[in] msg_ctx The messaging context to use.
483 * @param[out] presult A pointer to store the connected rpc client pipe.
485 * @return NT_STATUS_OK on success, a corresponding NT status if an
488 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
489 const struct ndr_syntax_id *abstract_syntax,
490 const struct auth_session_info *session_info,
491 const struct tsocket_address *remote_address,
492 struct messaging_context *msg_ctx,
493 struct rpc_pipe_client **presult)
495 struct rpc_pipe_client *result;
498 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
499 if (result == NULL) {
500 return NT_STATUS_NO_MEMORY;
503 result->abstract_syntax = *abstract_syntax;
504 result->transfer_syntax = ndr_transfer_syntax_ndr;
506 if (remote_address == NULL) {
507 struct tsocket_address *local;
510 rc = tsocket_address_inet_from_strings(mem_ctx,
517 return NT_STATUS_NO_MEMORY;
520 remote_address = local;
523 result->max_xmit_frag = -1;
524 result->max_recv_frag = -1;
526 status = rpcint_binding_handle_ex(result,
532 &result->binding_handle);
533 if (!NT_STATUS_IS_OK(status)) {
542 /****************************************************************************
543 * External pipes functions
544 ***************************************************************************/
547 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
548 const char *pipe_name,
549 const struct tsocket_address *local_address,
550 const struct tsocket_address *remote_address,
551 const struct auth_session_info *session_info)
553 struct np_proxy_state *result;
555 const char *socket_dir;
556 struct tevent_context *ev;
557 struct tevent_req *subreq;
558 struct auth_session_info_transport *session_info_t;
563 result = talloc(mem_ctx, struct np_proxy_state);
564 if (result == NULL) {
565 DEBUG(0, ("talloc failed\n"));
569 result->read_queue = tevent_queue_create(result, "np_read");
570 if (result->read_queue == NULL) {
571 DEBUG(0, ("tevent_queue_create failed\n"));
575 result->write_queue = tevent_queue_create(result, "np_write");
576 if (result->write_queue == NULL) {
577 DEBUG(0, ("tevent_queue_create failed\n"));
581 ev = s3_tevent_context_init(talloc_tos());
583 DEBUG(0, ("s3_tevent_context_init failed\n"));
587 socket_dir = lp_parm_const_string(
588 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
590 if (socket_dir == NULL) {
591 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
594 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
595 if (socket_np_dir == NULL) {
596 DEBUG(0, ("talloc_asprintf failed\n"));
600 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
601 if (session_info_t == NULL) {
602 DEBUG(0, ("talloc failed\n"));
606 session_info_t->session_info = copy_session_info(session_info_t,
608 if (session_info_t->session_info == NULL) {
609 DEBUG(0, ("copy_session_info failed\n"));
614 subreq = tstream_npa_connect_send(talloc_tos(), ev,
617 remote_address, /* client_addr */
618 NULL, /* client_name */
619 local_address, /* server_addr */
620 NULL, /* server_name */
622 if (subreq == NULL) {
624 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
625 "user %s\\%s failed\n",
626 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
627 session_info_t->session_info->info->account_name));
630 ok = tevent_req_poll(subreq, ev);
633 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
634 "failed for tstream_npa_connect: %s\n",
635 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
636 session_info_t->session_info->info->account_name,
641 ret = tstream_npa_connect_recv(subreq, &sys_errno,
645 &result->device_state,
646 &result->allocation_size);
650 if (errno == ENOENT) {
653 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
654 "user %s\\%s failed: %s\n",
655 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
656 session_info_t->session_info->info->account_name,
657 strerror(sys_errno)));
668 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
669 const char *pipe_name,
670 const struct ndr_interface_table *table,
671 const struct auth_session_info *session_info,
672 struct rpc_pipe_client **_result)
674 struct tsocket_address *local, *remote;
675 struct rpc_pipe_client *result = NULL;
676 struct np_proxy_state *proxy_state = NULL;
677 struct pipe_auth_data *auth;
681 /* this is an internal connection, fake up ip addresses */
682 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
685 return NT_STATUS_NO_MEMORY;
687 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
690 return NT_STATUS_NO_MEMORY;
693 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
694 local, remote, session_info);
696 return NT_STATUS_UNSUCCESSFUL;
699 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
700 if (result == NULL) {
701 status = NT_STATUS_NO_MEMORY;
705 result->abstract_syntax = table->syntax_id;
706 result->transfer_syntax = ndr_transfer_syntax_ndr;
708 result->desthost = get_myname(result);
709 result->srv_name_slash = talloc_asprintf_strupper_m(
710 result, "\\\\%s", result->desthost);
711 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
712 status = NT_STATUS_NO_MEMORY;
716 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
717 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
719 status = rpc_transport_tstream_init(result,
722 if (!NT_STATUS_IS_OK(status)) {
726 result->binding_handle = rpccli_bh_create(result, NULL, table);
727 if (result->binding_handle == NULL) {
728 status = NT_STATUS_NO_MEMORY;
729 DEBUG(0, ("Failed to create binding handle.\n"));
733 result->auth = talloc_zero(result, struct pipe_auth_data);
735 status = NT_STATUS_NO_MEMORY;
738 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
739 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
741 status = rpccli_anon_bind_data(result, &auth);
742 if (!NT_STATUS_IS_OK(status)) {
743 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
747 status = rpc_pipe_bind(result, auth);
748 if (!NT_STATUS_IS_OK(status)) {
749 DEBUG(0, ("Failed to bind external pipe.\n"));
754 if (!NT_STATUS_IS_OK(status)) {
757 TALLOC_FREE(proxy_state);
763 * @brief Create a new RPC client context which uses a local dispatch function
764 * or a remote transport, depending on rpc_server configuration for the
767 * @param[in] mem_ctx The memory context to use.
769 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
772 * @param[in] serversupplied_info The server supplied authentication function.
774 * @param[in] remote_address The client address information.
776 * @param[in] msg_ctx The messaging context to use.
778 * @param[out] presult A pointer to store the connected rpc client pipe.
780 * @return NT_STATUS_OK on success, a corresponding NT status if an
784 * struct rpc_pipe_client *winreg_pipe;
787 * status = rpc_pipe_open_interface(tmp_ctx,
788 * &ndr_table_winreg.syntax_id,
795 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
796 const struct ndr_interface_table *table,
797 const struct auth_session_info *session_info,
798 const struct tsocket_address *remote_address,
799 struct messaging_context *msg_ctx,
800 struct rpc_pipe_client **cli_pipe)
802 struct rpc_pipe_client *cli = NULL;
803 enum rpc_service_mode_e pipe_mode;
804 const char *pipe_name;
808 if (cli_pipe != NULL) {
809 if (rpccli_is_connected(*cli_pipe)) {
812 TALLOC_FREE(*cli_pipe);
816 tmp_ctx = talloc_stackframe();
817 if (tmp_ctx == NULL) {
818 return NT_STATUS_NO_MEMORY;
821 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
822 if (pipe_name == NULL) {
823 status = NT_STATUS_INVALID_PARAMETER;
827 while (pipe_name[0] == '\\') {
831 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
833 pipe_mode = rpc_service_mode(pipe_name);
836 case RPC_SERVICE_MODE_EMBEDDED:
837 status = rpc_pipe_open_internal(tmp_ctx,
838 &table->syntax_id, session_info,
839 remote_address, msg_ctx,
841 if (!NT_STATUS_IS_OK(status)) {
845 case RPC_SERVICE_MODE_EXTERNAL:
846 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
847 * for now we need to use the special proxy setup to connect
850 status = rpc_pipe_open_external(tmp_ctx,
854 if (!NT_STATUS_IS_OK(status)) {
858 case RPC_SERVICE_MODE_DISABLED:
859 status = NT_STATUS_NOT_IMPLEMENTED;
860 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
861 pipe_name, nt_errstr(status)));
865 status = NT_STATUS_OK;
867 if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
868 *cli_pipe = talloc_move(mem_ctx, &cli);
870 TALLOC_FREE(tmp_ctx);