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"
34 #define DBGC_CLASS DBGC_RPC_SRV
36 static int pipes_open;
38 static struct pipes_struct *InternalPipes;
41 * the following prototypes are declared here to avoid
42 * code being moved about too much for a patch to be
43 * disrupted / less obvious.
45 * these functions, and associated functions that they
46 * call, should be moved behind a .so module-loading
47 * system _anyway_. so that's the next step...
50 /****************************************************************************
51 Internal Pipe iterator functions.
52 ****************************************************************************/
54 struct pipes_struct *get_first_internal_pipe(void)
59 struct pipes_struct *get_next_internal_pipe(struct pipes_struct *p)
64 static void free_pipe_rpc_context_internal( PIPE_RPC_FNS *list )
66 PIPE_RPC_FNS *tmp = list;
78 bool check_open_pipes(void)
80 struct pipes_struct *p;
82 for (p = InternalPipes; p != NULL; p = p->next) {
83 if (num_pipe_handles(p) != 0) {
90 /****************************************************************************
92 ****************************************************************************/
94 int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
97 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
101 TALLOC_FREE(p->auth.auth_ctx);
103 free_pipe_rpc_context_internal( p->contexts );
105 /* Free the handles database. */
106 close_policy_by_pipe(p);
108 DLIST_REMOVE(InternalPipes, p);
115 /****************************************************************************
116 Make an internal namedpipes structure
117 ****************************************************************************/
119 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
120 const struct ndr_syntax_id *syntax,
121 struct client_address *client_id,
122 const struct auth_serversupplied_info *session_info,
123 struct messaging_context *msg_ctx)
125 struct pipes_struct *p;
127 DEBUG(4,("Create pipe requested %s\n",
128 get_pipe_name_from_syntax(talloc_tos(), syntax)));
130 p = TALLOC_ZERO_P(mem_ctx, struct pipes_struct);
133 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
137 p->mem_ctx = talloc_named(p, 0, "pipe %s %p",
138 get_pipe_name_from_syntax(talloc_tos(),
140 if (p->mem_ctx == NULL) {
141 DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
146 if (!init_pipe_handles(p, syntax)) {
147 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
152 p->session_info = copy_serverinfo(p, session_info);
153 if (p->session_info == NULL) {
154 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
155 close_policy_by_pipe(p);
160 p->msg_ctx = msg_ctx;
162 DLIST_ADD(InternalPipes, p);
164 p->client_id = client_id;
166 p->endian = RPC_LITTLE_ENDIAN;
169 p->transport = NCALRPC;
171 DEBUG(4,("Created internal pipe %s (pipes_open=%d)\n",
172 get_pipe_name_from_syntax(talloc_tos(), syntax), pipes_open));
174 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
179 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
182 const DATA_BLOB *in_data,
185 uint32_t num_cmds = rpc_srv_get_pipe_num_cmds(&p->syntax);
186 const struct api_struct *cmds = rpc_srv_get_pipe_cmds(&p->syntax);
193 for (i = 0; i < num_cmds; i++) {
194 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
200 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
203 p->in_data.data = *in_data;
204 p->out_data.rdata = data_blob_null;
207 p->in_data.data = data_blob_null;
209 data_blob_free(&p->out_data.rdata);
210 talloc_free_children(p->mem_ctx);
211 return NT_STATUS_RPC_CALL_FAILED;
214 if (p->fault_state) {
215 p->fault_state = false;
216 data_blob_free(&p->out_data.rdata);
217 talloc_free_children(p->mem_ctx);
218 return NT_STATUS_RPC_CALL_FAILED;
221 if (p->bad_handle_fault_state) {
222 p->bad_handle_fault_state = false;
223 data_blob_free(&p->out_data.rdata);
224 talloc_free_children(p->mem_ctx);
225 return NT_STATUS_RPC_SS_CONTEXT_MISMATCH;
228 if (p->rng_fault_state) {
229 p->rng_fault_state = false;
230 data_blob_free(&p->out_data.rdata);
231 talloc_free_children(p->mem_ctx);
232 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
235 *out_data = p->out_data.rdata;
236 talloc_steal(mem_ctx, out_data->data);
237 p->out_data.rdata = data_blob_null;
239 talloc_free_children(p->mem_ctx);
243 struct rpcint_bh_state {
244 struct pipes_struct *p;
247 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
249 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
250 struct rpcint_bh_state);
259 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
262 /* TODO: implement timeouts */
266 struct rpcint_bh_raw_call_state {
272 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
273 struct tevent_context *ev,
274 struct dcerpc_binding_handle *h,
275 const struct GUID *object,
278 const uint8_t *in_data,
281 struct rpcint_bh_state *hs =
282 dcerpc_binding_handle_data(h,
283 struct rpcint_bh_state);
284 struct tevent_req *req;
285 struct rpcint_bh_raw_call_state *state;
289 req = tevent_req_create(mem_ctx, &state,
290 struct rpcint_bh_raw_call_state);
294 state->in_data.data = discard_const_p(uint8_t, in_data);
295 state->in_data.length = in_length;
297 ok = rpcint_bh_is_connected(h);
299 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
300 return tevent_req_post(req, ev);
303 /* TODO: allow async */
304 status = rpcint_dispatch(hs->p, state, opnum,
307 if (!NT_STATUS_IS_OK(status)) {
308 tevent_req_nterror(req, status);
309 return tevent_req_post(req, ev);
312 tevent_req_done(req);
313 return tevent_req_post(req, ev);
316 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
322 struct rpcint_bh_raw_call_state *state =
324 struct rpcint_bh_raw_call_state);
327 if (tevent_req_is_nterror(req, &status)) {
328 tevent_req_received(req);
332 *out_data = talloc_move(mem_ctx, &state->out_data.data);
333 *out_length = state->out_data.length;
335 tevent_req_received(req);
339 struct rpcint_bh_disconnect_state {
343 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
344 struct tevent_context *ev,
345 struct dcerpc_binding_handle *h)
347 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
348 struct rpcint_bh_state);
349 struct tevent_req *req;
350 struct rpcint_bh_disconnect_state *state;
353 req = tevent_req_create(mem_ctx, &state,
354 struct rpcint_bh_disconnect_state);
359 ok = rpcint_bh_is_connected(h);
361 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
362 return tevent_req_post(req, ev);
366 * TODO: do a real async disconnect ...
368 * For now the caller needs to free pipes_struct
372 tevent_req_done(req);
373 return tevent_req_post(req, ev);
376 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
380 if (tevent_req_is_nterror(req, &status)) {
381 tevent_req_received(req);
385 tevent_req_received(req);
389 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
394 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
396 const void *_struct_ptr,
397 const struct ndr_interface_call *call)
399 void *struct_ptr = discard_const(_struct_ptr);
401 if (DEBUGLEVEL < 11) {
405 if (ndr_flags & NDR_IN) {
406 ndr_print_function_debug(call->ndr_print,
411 if (ndr_flags & NDR_OUT) {
412 ndr_print_function_debug(call->ndr_print,
419 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
421 .is_connected = rpcint_bh_is_connected,
422 .set_timeout = rpcint_bh_set_timeout,
423 .raw_call_send = rpcint_bh_raw_call_send,
424 .raw_call_recv = rpcint_bh_raw_call_recv,
425 .disconnect_send = rpcint_bh_disconnect_send,
426 .disconnect_recv = rpcint_bh_disconnect_recv,
428 .ref_alloc = rpcint_bh_ref_alloc,
429 .do_ndr_print = rpcint_bh_do_ndr_print,
432 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
433 const struct ndr_syntax_id *abstract_syntax,
434 const struct ndr_interface_table *ndr_table,
435 struct client_address *client_id,
436 const struct auth_serversupplied_info *session_info,
437 struct messaging_context *msg_ctx,
438 struct dcerpc_binding_handle **binding_handle)
440 struct dcerpc_binding_handle *h;
441 struct rpcint_bh_state *hs;
444 abstract_syntax = &ndr_table->syntax_id;
447 h = dcerpc_binding_handle_create(mem_ctx,
452 struct rpcint_bh_state,
455 return NT_STATUS_NO_MEMORY;
457 hs->p = make_internal_rpc_pipe_p(hs,
464 return NT_STATUS_NO_MEMORY;
471 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
473 * @param[in] mem_ctx The memory context to use.
475 * @param[in] ndr_table Normally the ndr_table_<name>.
477 * @param[in] client_id The info about the connected client.
479 * @param[in] serversupplied_info The server supplied authentication function.
481 * @param[in] msg_ctx The messaging context that can be used by the server
483 * @param[out] binding_handle A pointer to store the connected
484 * dcerpc_binding_handle
486 * @return NT_STATUS_OK on success, a corresponding NT status if an
490 * struct dcerpc_binding_handle *winreg_binding;
493 * status = rpcint_binding_handle(tmp_ctx,
501 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
502 const struct ndr_interface_table *ndr_table,
503 struct client_address *client_id,
504 const struct auth_serversupplied_info *session_info,
505 struct messaging_context *msg_ctx,
506 struct dcerpc_binding_handle **binding_handle)
508 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, client_id,
509 session_info, msg_ctx, binding_handle);
513 * @brief Create a new RPC client context which uses a local dispatch function.
515 * @param[in] mem_ctx The memory context to use.
517 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
520 * @param[in] dispatch The corresponding autogenerated dispatch function
521 * rpc_<name>_dispatch.
523 * @param[in] serversupplied_info The server supplied authentication function.
525 * @param[out] presult A pointer to store the connected rpc client pipe.
527 * @return NT_STATUS_OK on success, a corresponding NT status if an
531 * struct rpc_pipe_client *winreg_pipe;
534 * status = rpc_pipe_open_internal(tmp_ctx,
535 * &ndr_table_winreg.syntax_id,
536 * rpc_winreg_dispatch,
541 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
542 const struct ndr_syntax_id *abstract_syntax,
543 const struct auth_serversupplied_info *serversupplied_info,
544 struct client_address *client_id,
545 struct messaging_context *msg_ctx,
546 struct rpc_pipe_client **presult)
548 struct rpc_pipe_client *result;
551 result = TALLOC_ZERO_P(mem_ctx, struct rpc_pipe_client);
552 if (result == NULL) {
553 return NT_STATUS_NO_MEMORY;
556 result->abstract_syntax = *abstract_syntax;
557 result->transfer_syntax = ndr_transfer_syntax;
559 if (client_id == NULL) {
560 static struct client_address unknown;
561 strlcpy(unknown.addr, "<UNKNOWN>", sizeof(unknown.addr));
562 unknown.name = "<UNKNOWN>";
563 client_id = &unknown;
566 result->max_xmit_frag = -1;
567 result->max_recv_frag = -1;
569 status = rpcint_binding_handle_ex(result,
575 &result->binding_handle);
576 if (!NT_STATUS_IS_OK(status)) {
585 /****************************************************************************
586 * External pipes functions
587 ***************************************************************************/
590 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
591 const char *pipe_name,
592 const struct tsocket_address *local_address,
593 const struct tsocket_address *remote_address,
594 const struct auth_serversupplied_info *session_info)
596 struct np_proxy_state *result;
598 const char *socket_dir;
599 struct tevent_context *ev;
600 struct tevent_req *subreq;
601 struct auth_session_info_transport *session_info_t;
602 struct auth_user_info_dc *user_info_dc;
603 union netr_Validation val;
609 result = talloc(mem_ctx, struct np_proxy_state);
610 if (result == NULL) {
611 DEBUG(0, ("talloc failed\n"));
615 result->read_queue = tevent_queue_create(result, "np_read");
616 if (result->read_queue == NULL) {
617 DEBUG(0, ("tevent_queue_create failed\n"));
621 result->write_queue = tevent_queue_create(result, "np_write");
622 if (result->write_queue == NULL) {
623 DEBUG(0, ("tevent_queue_create failed\n"));
627 ev = s3_tevent_context_init(talloc_tos());
629 DEBUG(0, ("s3_tevent_context_init failed\n"));
633 socket_dir = lp_parm_const_string(
634 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
636 if (socket_dir == NULL) {
637 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
640 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
641 if (socket_np_dir == NULL) {
642 DEBUG(0, ("talloc_asprintf failed\n"));
646 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
647 if (session_info_t == NULL) {
648 DEBUG(0, ("talloc failed\n"));
652 /* Send the named_pipe_auth server the user's full token */
653 session_info_t->security_token = session_info->security_token;
654 session_info_t->session_key = session_info->user_session_key;
656 val.sam3 = session_info->info3;
658 /* Convert into something we can build a struct
659 * auth_session_info_transport from. Most of the work here
660 * will be to convert the SIDS, which we will then ignore, but
661 * this is the easier way to handle it */
662 status = make_user_info_dc_netlogon_validation(talloc_tos(), "", 3, &val, &user_info_dc);
663 if (!NT_STATUS_IS_OK(status)) {
664 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
668 session_info_t->info = talloc_move(session_info_t, &user_info_dc->info);
669 talloc_free(user_info_dc);
672 subreq = tstream_npa_connect_send(talloc_tos(), ev,
675 remote_address, /* client_addr */
676 NULL, /* client_name */
677 local_address, /* server_addr */
678 NULL, /* server_name */
680 if (subreq == NULL) {
682 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
683 "user %s\\%s failed\n",
684 socket_np_dir, pipe_name, session_info_t->info->domain_name,
685 session_info_t->info->account_name));
688 ok = tevent_req_poll(subreq, ev);
691 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
692 "failed for tstream_npa_connect: %s\n",
693 socket_np_dir, pipe_name, session_info_t->info->domain_name,
694 session_info_t->info->account_name,
699 ret = tstream_npa_connect_recv(subreq, &sys_errno,
703 &result->device_state,
704 &result->allocation_size);
707 DEBUG(0, ("tstream_npa_connect_recv to %s for pipe %s and "
708 "user %s\\%s failed: %s\n",
709 socket_np_dir, pipe_name, session_info_t->info->domain_name,
710 session_info_t->info->account_name,
711 strerror(sys_errno)));
722 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
723 const char *pipe_name,
724 const struct ndr_syntax_id *abstract_syntax,
725 const struct auth_serversupplied_info *session_info,
726 struct rpc_pipe_client **_result)
728 struct tsocket_address *local, *remote;
729 struct rpc_pipe_client *result = NULL;
730 struct np_proxy_state *proxy_state = NULL;
731 struct pipe_auth_data *auth;
735 /* this is an internal connection, fake up ip addresses */
736 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
739 return NT_STATUS_NO_MEMORY;
741 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
744 return NT_STATUS_NO_MEMORY;
747 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
748 local, remote, session_info);
750 return NT_STATUS_UNSUCCESSFUL;
753 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
754 if (result == NULL) {
755 status = NT_STATUS_NO_MEMORY;
759 result->abstract_syntax = *abstract_syntax;
760 result->transfer_syntax = ndr_transfer_syntax;
762 result->desthost = get_myname(result);
763 result->srv_name_slash = talloc_asprintf_strupper_m(
764 result, "\\\\%s", result->desthost);
765 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
766 status = NT_STATUS_NO_MEMORY;
770 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
771 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
773 status = rpc_transport_tstream_init(result,
776 if (!NT_STATUS_IS_OK(status)) {
780 result->binding_handle = rpccli_bh_create(result);
781 if (result->binding_handle == NULL) {
782 status = NT_STATUS_NO_MEMORY;
783 DEBUG(0, ("Failed to create binding handle.\n"));
787 result->auth = talloc_zero(result, struct pipe_auth_data);
789 status = NT_STATUS_NO_MEMORY;
792 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
793 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
795 status = rpccli_anon_bind_data(result, &auth);
796 if (!NT_STATUS_IS_OK(status)) {
797 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
801 status = rpc_pipe_bind(result, auth);
802 if (!NT_STATUS_IS_OK(status)) {
803 DEBUG(0, ("Failed to bind external pipe.\n"));
808 if (!NT_STATUS_IS_OK(status)) {
811 TALLOC_FREE(proxy_state);
817 * @brief Create a new RPC client context which uses a local dispatch function.
819 * @param mem_ctx The memory context on which thje pipe will ultimately
821 * @param name The pipe name to connect to.
822 * @param session_info Credentials to use for the connection.
823 * @param pipe [in|out] Checks if a pipe is connected, and connects it
826 * @return NT_STATUS_OK on success, a corresponding NT status if
830 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
831 const struct ndr_syntax_id *syntax,
832 const struct auth_serversupplied_info *session_info,
833 struct client_address *client_id,
834 struct messaging_context *msg_ctx,
835 struct rpc_pipe_client **cli_pipe)
837 struct rpc_pipe_client *cli = NULL;
838 const char *server_type;
839 const char *pipe_name;
843 if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
846 TALLOC_FREE(*cli_pipe);
849 tmp_ctx = talloc_stackframe();
850 if (tmp_ctx == NULL) {
851 return NT_STATUS_NO_MEMORY;
854 pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
855 if (pipe_name == NULL) {
856 status = NT_STATUS_INVALID_PARAMETER;
860 while (pipe_name[0] == '\\') {
864 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
866 server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
867 "rpc_server", pipe_name,
870 if (StrCaseCmp(server_type, "embedded") == 0) {
871 status = rpc_pipe_open_internal(tmp_ctx,
872 syntax, session_info,
875 if (!NT_STATUS_IS_OK(status)) {
878 } else if (StrCaseCmp(server_type, "daemon") == 0 ||
879 StrCaseCmp(server_type, "external") == 0) {
880 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
881 * for now we need to use the special proxy setup to connect
884 status = rpc_pipe_open_external(tmp_ctx,
888 if (!NT_STATUS_IS_OK(status)) {
892 status = NT_STATUS_NOT_IMPLEMENTED;
893 DEBUG(0, ("Wrong servertype specified in config file: %s",
898 status = NT_STATUS_OK;
900 if (NT_STATUS_IS_OK(status)) {
901 *cli_pipe = talloc_move(mem_ctx, &cli);
903 TALLOC_FREE(tmp_ctx);