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
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "rpc_client/cli_pipe.h"
25 #include "rpc_server/srv_pipe_internal.h"
27 #include "../libcli/named_pipe_auth/npa_tstream.h"
28 #include "rpc_server/rpc_ncacn_np.h"
29 #include "librpc/gen_ndr/netlogon.h"
30 #include "librpc/gen_ndr/auth.h"
31 #include "../auth/auth_sam_reply.h"
35 #define DBGC_CLASS DBGC_RPC_SRV
37 static int pipes_open;
39 static struct pipes_struct *InternalPipes;
42 * the following prototypes are declared here to avoid
43 * code being moved about too much for a patch to be
44 * disrupted / less obvious.
46 * these functions, and associated functions that they
47 * call, should be moved behind a .so module-loading
48 * system _anyway_. so that's the next step...
51 /****************************************************************************
52 Internal Pipe iterator functions.
53 ****************************************************************************/
55 struct pipes_struct *get_first_internal_pipe(void)
60 struct pipes_struct *get_next_internal_pipe(struct pipes_struct *p)
65 static void free_pipe_rpc_context_internal( PIPE_RPC_FNS *list )
67 PIPE_RPC_FNS *tmp = list;
79 bool check_open_pipes(void)
81 struct pipes_struct *p;
83 for (p = InternalPipes; p != NULL; p = p->next) {
84 if (num_pipe_handles(p) != 0) {
91 /****************************************************************************
93 ****************************************************************************/
95 int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
98 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
102 TALLOC_FREE(p->auth.auth_ctx);
104 free_pipe_rpc_context_internal( p->contexts );
106 /* Free the handles database. */
107 close_policy_by_pipe(p);
109 DLIST_REMOVE(InternalPipes, p);
116 /****************************************************************************
117 Make an internal namedpipes structure
118 ****************************************************************************/
120 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
121 const struct ndr_syntax_id *syntax,
122 struct client_address *client_id,
123 const struct auth_serversupplied_info *session_info,
124 struct messaging_context *msg_ctx)
126 struct pipes_struct *p;
128 DEBUG(4,("Create pipe requested %s\n",
129 get_pipe_name_from_syntax(talloc_tos(), syntax)));
131 p = TALLOC_ZERO_P(mem_ctx, struct pipes_struct);
134 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
138 p->mem_ctx = talloc_named(p, 0, "pipe %s %p",
139 get_pipe_name_from_syntax(talloc_tos(),
141 if (p->mem_ctx == NULL) {
142 DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
147 if (!init_pipe_handles(p, syntax)) {
148 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
153 p->session_info = copy_serverinfo(p, session_info);
154 if (p->session_info == NULL) {
155 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
156 close_policy_by_pipe(p);
161 p->msg_ctx = msg_ctx;
163 DLIST_ADD(InternalPipes, p);
165 p->client_id = client_id;
167 p->endian = RPC_LITTLE_ENDIAN;
170 p->transport = NCALRPC;
172 DEBUG(4,("Created internal pipe %s (pipes_open=%d)\n",
173 get_pipe_name_from_syntax(talloc_tos(), syntax), pipes_open));
175 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
180 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
183 const DATA_BLOB *in_data,
186 uint32_t num_cmds = rpc_srv_get_pipe_num_cmds(&p->syntax);
187 const struct api_struct *cmds = rpc_srv_get_pipe_cmds(&p->syntax);
194 for (i = 0; i < num_cmds; i++) {
195 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
201 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
204 p->in_data.data = *in_data;
205 p->out_data.rdata = data_blob_null;
208 p->in_data.data = data_blob_null;
210 data_blob_free(&p->out_data.rdata);
211 talloc_free_children(p->mem_ctx);
212 return NT_STATUS_RPC_CALL_FAILED;
215 if (p->fault_state) {
216 p->fault_state = false;
217 data_blob_free(&p->out_data.rdata);
218 talloc_free_children(p->mem_ctx);
219 return NT_STATUS_RPC_CALL_FAILED;
222 if (p->bad_handle_fault_state) {
223 p->bad_handle_fault_state = false;
224 data_blob_free(&p->out_data.rdata);
225 talloc_free_children(p->mem_ctx);
226 return NT_STATUS_RPC_SS_CONTEXT_MISMATCH;
229 if (p->rng_fault_state) {
230 p->rng_fault_state = false;
231 data_blob_free(&p->out_data.rdata);
232 talloc_free_children(p->mem_ctx);
233 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
236 *out_data = p->out_data.rdata;
237 talloc_steal(mem_ctx, out_data->data);
238 p->out_data.rdata = data_blob_null;
240 talloc_free_children(p->mem_ctx);
244 struct rpcint_bh_state {
245 struct pipes_struct *p;
248 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
250 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
251 struct rpcint_bh_state);
260 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
263 /* TODO: implement timeouts */
267 struct rpcint_bh_raw_call_state {
273 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
274 struct tevent_context *ev,
275 struct dcerpc_binding_handle *h,
276 const struct GUID *object,
279 const uint8_t *in_data,
282 struct rpcint_bh_state *hs =
283 dcerpc_binding_handle_data(h,
284 struct rpcint_bh_state);
285 struct tevent_req *req;
286 struct rpcint_bh_raw_call_state *state;
290 req = tevent_req_create(mem_ctx, &state,
291 struct rpcint_bh_raw_call_state);
295 state->in_data.data = discard_const_p(uint8_t, in_data);
296 state->in_data.length = in_length;
298 ok = rpcint_bh_is_connected(h);
300 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
301 return tevent_req_post(req, ev);
304 /* TODO: allow async */
305 status = rpcint_dispatch(hs->p, state, opnum,
308 if (!NT_STATUS_IS_OK(status)) {
309 tevent_req_nterror(req, status);
310 return tevent_req_post(req, ev);
313 tevent_req_done(req);
314 return tevent_req_post(req, ev);
317 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
323 struct rpcint_bh_raw_call_state *state =
325 struct rpcint_bh_raw_call_state);
328 if (tevent_req_is_nterror(req, &status)) {
329 tevent_req_received(req);
333 *out_data = talloc_move(mem_ctx, &state->out_data.data);
334 *out_length = state->out_data.length;
336 tevent_req_received(req);
340 struct rpcint_bh_disconnect_state {
344 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
345 struct tevent_context *ev,
346 struct dcerpc_binding_handle *h)
348 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
349 struct rpcint_bh_state);
350 struct tevent_req *req;
351 struct rpcint_bh_disconnect_state *state;
354 req = tevent_req_create(mem_ctx, &state,
355 struct rpcint_bh_disconnect_state);
360 ok = rpcint_bh_is_connected(h);
362 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
363 return tevent_req_post(req, ev);
367 * TODO: do a real async disconnect ...
369 * For now the caller needs to free pipes_struct
373 tevent_req_done(req);
374 return tevent_req_post(req, ev);
377 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
381 if (tevent_req_is_nterror(req, &status)) {
382 tevent_req_received(req);
386 tevent_req_received(req);
390 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
395 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
397 const void *_struct_ptr,
398 const struct ndr_interface_call *call)
400 void *struct_ptr = discard_const(_struct_ptr);
402 if (DEBUGLEVEL < 11) {
406 if (ndr_flags & NDR_IN) {
407 ndr_print_function_debug(call->ndr_print,
412 if (ndr_flags & NDR_OUT) {
413 ndr_print_function_debug(call->ndr_print,
420 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
422 .is_connected = rpcint_bh_is_connected,
423 .set_timeout = rpcint_bh_set_timeout,
424 .raw_call_send = rpcint_bh_raw_call_send,
425 .raw_call_recv = rpcint_bh_raw_call_recv,
426 .disconnect_send = rpcint_bh_disconnect_send,
427 .disconnect_recv = rpcint_bh_disconnect_recv,
429 .ref_alloc = rpcint_bh_ref_alloc,
430 .do_ndr_print = rpcint_bh_do_ndr_print,
433 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
434 const struct ndr_syntax_id *abstract_syntax,
435 const struct ndr_interface_table *ndr_table,
436 struct client_address *client_id,
437 const struct auth_serversupplied_info *session_info,
438 struct messaging_context *msg_ctx,
439 struct dcerpc_binding_handle **binding_handle)
441 struct dcerpc_binding_handle *h;
442 struct rpcint_bh_state *hs;
445 abstract_syntax = &ndr_table->syntax_id;
448 h = dcerpc_binding_handle_create(mem_ctx,
453 struct rpcint_bh_state,
456 return NT_STATUS_NO_MEMORY;
458 hs->p = make_internal_rpc_pipe_p(hs,
465 return NT_STATUS_NO_MEMORY;
472 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
474 * @param[in] mem_ctx The memory context to use.
476 * @param[in] ndr_table Normally the ndr_table_<name>.
478 * @param[in] client_id The info about the connected client.
480 * @param[in] serversupplied_info The server supplied authentication function.
482 * @param[in] msg_ctx The messaging context that can be used by the server
484 * @param[out] binding_handle A pointer to store the connected
485 * dcerpc_binding_handle
487 * @return NT_STATUS_OK on success, a corresponding NT status if an
491 * struct dcerpc_binding_handle *winreg_binding;
494 * status = rpcint_binding_handle(tmp_ctx,
502 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
503 const struct ndr_interface_table *ndr_table,
504 struct client_address *client_id,
505 const struct auth_serversupplied_info *session_info,
506 struct messaging_context *msg_ctx,
507 struct dcerpc_binding_handle **binding_handle)
509 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, client_id,
510 session_info, msg_ctx, binding_handle);
516 * @brief Create a new RPC client context which uses a local transport.
518 * This creates a local transport. It is a shortcut to directly call the server
519 * functions and avoid marschalling.
521 * @param[in] mem_ctx The memory context to use.
523 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
526 * @param[in] serversupplied_info The server supplied authentication function.
528 * @param[in] client_id The client address information.
530 * @param[in] msg_ctx The messaging context to use.
532 * @param[out] presult A pointer to store the connected rpc client pipe.
534 * @return NT_STATUS_OK on success, a corresponding NT status if an
538 * struct rpc_pipe_client *winreg_pipe;
541 * status = rpc_pipe_open_internal(tmp_ctx,
542 * &ndr_table_winreg.syntax_id,
548 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
549 const struct ndr_syntax_id *abstract_syntax,
550 const struct auth_serversupplied_info *serversupplied_info,
551 struct client_address *client_id,
552 struct messaging_context *msg_ctx,
553 struct rpc_pipe_client **presult)
555 struct rpc_pipe_client *result;
558 result = TALLOC_ZERO_P(mem_ctx, struct rpc_pipe_client);
559 if (result == NULL) {
560 return NT_STATUS_NO_MEMORY;
563 result->abstract_syntax = *abstract_syntax;
564 result->transfer_syntax = ndr_transfer_syntax;
566 if (client_id == NULL) {
567 static struct client_address unknown;
568 strlcpy(unknown.addr, "<UNKNOWN>", sizeof(unknown.addr));
569 unknown.name = "<UNKNOWN>";
570 client_id = &unknown;
573 result->max_xmit_frag = -1;
574 result->max_recv_frag = -1;
576 status = rpcint_binding_handle_ex(result,
582 &result->binding_handle);
583 if (!NT_STATUS_IS_OK(status)) {
592 /****************************************************************************
593 * External pipes functions
594 ***************************************************************************/
597 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
598 const char *pipe_name,
599 const struct tsocket_address *local_address,
600 const struct tsocket_address *remote_address,
601 const struct auth_serversupplied_info *session_info)
603 struct np_proxy_state *result;
605 const char *socket_dir;
606 struct tevent_context *ev;
607 struct tevent_req *subreq;
608 struct auth_session_info_transport *session_info_t;
609 struct auth_user_info_dc *user_info_dc;
610 union netr_Validation val;
616 result = talloc(mem_ctx, struct np_proxy_state);
617 if (result == NULL) {
618 DEBUG(0, ("talloc failed\n"));
622 result->read_queue = tevent_queue_create(result, "np_read");
623 if (result->read_queue == NULL) {
624 DEBUG(0, ("tevent_queue_create failed\n"));
628 result->write_queue = tevent_queue_create(result, "np_write");
629 if (result->write_queue == NULL) {
630 DEBUG(0, ("tevent_queue_create failed\n"));
634 ev = s3_tevent_context_init(talloc_tos());
636 DEBUG(0, ("s3_tevent_context_init failed\n"));
640 socket_dir = lp_parm_const_string(
641 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
643 if (socket_dir == NULL) {
644 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
647 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
648 if (socket_np_dir == NULL) {
649 DEBUG(0, ("talloc_asprintf failed\n"));
653 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
654 if (session_info_t == NULL) {
655 DEBUG(0, ("talloc failed\n"));
659 /* Send the named_pipe_auth server the user's full token */
660 session_info_t->security_token = session_info->security_token;
661 session_info_t->session_key = session_info->user_session_key;
663 val.sam3 = session_info->info3;
665 /* Convert into something we can build a struct
666 * auth_session_info_transport from. Most of the work here
667 * will be to convert the SIDS, which we will then ignore, but
668 * this is the easier way to handle it */
669 status = make_user_info_dc_netlogon_validation(talloc_tos(), "", 3, &val, &user_info_dc);
670 if (!NT_STATUS_IS_OK(status)) {
671 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
675 session_info_t->info = talloc_move(session_info_t, &user_info_dc->info);
676 talloc_free(user_info_dc);
679 subreq = tstream_npa_connect_send(talloc_tos(), ev,
682 remote_address, /* client_addr */
683 NULL, /* client_name */
684 local_address, /* server_addr */
685 NULL, /* server_name */
687 if (subreq == NULL) {
689 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
690 "user %s\\%s failed\n",
691 socket_np_dir, pipe_name, session_info_t->info->domain_name,
692 session_info_t->info->account_name));
695 ok = tevent_req_poll(subreq, ev);
698 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
699 "failed for tstream_npa_connect: %s\n",
700 socket_np_dir, pipe_name, session_info_t->info->domain_name,
701 session_info_t->info->account_name,
706 ret = tstream_npa_connect_recv(subreq, &sys_errno,
710 &result->device_state,
711 &result->allocation_size);
714 DEBUG(0, ("tstream_npa_connect_recv to %s for pipe %s and "
715 "user %s\\%s failed: %s\n",
716 socket_np_dir, pipe_name, session_info_t->info->domain_name,
717 session_info_t->info->account_name,
718 strerror(sys_errno)));
729 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
730 const char *pipe_name,
731 const struct ndr_syntax_id *abstract_syntax,
732 const struct auth_serversupplied_info *session_info,
733 struct rpc_pipe_client **_result)
735 struct tsocket_address *local, *remote;
736 struct rpc_pipe_client *result = NULL;
737 struct np_proxy_state *proxy_state = NULL;
738 struct pipe_auth_data *auth;
742 /* this is an internal connection, fake up ip addresses */
743 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
746 return NT_STATUS_NO_MEMORY;
748 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
751 return NT_STATUS_NO_MEMORY;
754 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
755 local, remote, session_info);
757 return NT_STATUS_UNSUCCESSFUL;
760 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
761 if (result == NULL) {
762 status = NT_STATUS_NO_MEMORY;
766 result->abstract_syntax = *abstract_syntax;
767 result->transfer_syntax = ndr_transfer_syntax;
769 result->desthost = get_myname(result);
770 result->srv_name_slash = talloc_asprintf_strupper_m(
771 result, "\\\\%s", result->desthost);
772 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
773 status = NT_STATUS_NO_MEMORY;
777 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
778 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
780 status = rpc_transport_tstream_init(result,
783 if (!NT_STATUS_IS_OK(status)) {
787 result->binding_handle = rpccli_bh_create(result);
788 if (result->binding_handle == NULL) {
789 status = NT_STATUS_NO_MEMORY;
790 DEBUG(0, ("Failed to create binding handle.\n"));
794 result->auth = talloc_zero(result, struct pipe_auth_data);
796 status = NT_STATUS_NO_MEMORY;
799 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
800 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
802 status = rpccli_anon_bind_data(result, &auth);
803 if (!NT_STATUS_IS_OK(status)) {
804 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
808 status = rpc_pipe_bind(result, auth);
809 if (!NT_STATUS_IS_OK(status)) {
810 DEBUG(0, ("Failed to bind external pipe.\n"));
815 if (!NT_STATUS_IS_OK(status)) {
818 TALLOC_FREE(proxy_state);
824 * @brief Create a new RPC client context which uses a local dispatch function.
826 * @param mem_ctx The memory context on which thje pipe will ultimately
828 * @param name The pipe name to connect to.
829 * @param session_info Credentials to use for the connection.
830 * @param pipe [in|out] Checks if a pipe is connected, and connects it
833 * @return NT_STATUS_OK on success, a corresponding NT status if
837 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
838 const struct ndr_syntax_id *syntax,
839 const struct auth_serversupplied_info *session_info,
840 struct client_address *client_id,
841 struct messaging_context *msg_ctx,
842 struct rpc_pipe_client **cli_pipe)
844 struct rpc_pipe_client *cli = NULL;
845 const char *server_type;
846 const char *pipe_name;
850 if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
853 TALLOC_FREE(*cli_pipe);
856 tmp_ctx = talloc_stackframe();
857 if (tmp_ctx == NULL) {
858 return NT_STATUS_NO_MEMORY;
861 pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
862 if (pipe_name == NULL) {
863 status = NT_STATUS_INVALID_PARAMETER;
867 while (pipe_name[0] == '\\') {
871 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
873 server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
874 "rpc_server", pipe_name,
877 if (StrCaseCmp(server_type, "embedded") == 0) {
878 status = rpc_pipe_open_internal(tmp_ctx,
879 syntax, session_info,
882 if (!NT_STATUS_IS_OK(status)) {
885 } else if (StrCaseCmp(server_type, "daemon") == 0 ||
886 StrCaseCmp(server_type, "external") == 0) {
887 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
888 * for now we need to use the special proxy setup to connect
891 status = rpc_pipe_open_external(tmp_ctx,
895 if (!NT_STATUS_IS_OK(status)) {
899 status = NT_STATUS_NOT_IMPLEMENTED;
900 DEBUG(0, ("Wrong servertype specified in config file: %s",
905 status = NT_STATUS_OK;
907 if (NT_STATUS_IS_OK(status)) {
908 *cli_pipe = talloc_move(mem_ctx, &cli);
910 TALLOC_FREE(tmp_ctx);