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"
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 static struct pipes_struct *InternalPipes;
45 * the following prototypes are declared here to avoid
46 * code being moved about too much for a patch to be
47 * disrupted / less obvious.
49 * these functions, and associated functions that they
50 * call, should be moved behind a .so module-loading
51 * system _anyway_. so that's the next step...
54 /****************************************************************************
55 Internal Pipe iterator functions.
56 ****************************************************************************/
58 struct pipes_struct *get_first_internal_pipe(void)
63 struct pipes_struct *get_next_internal_pipe(struct pipes_struct *p)
68 static void free_pipe_rpc_context_internal( PIPE_RPC_FNS *list )
70 PIPE_RPC_FNS *tmp = list;
82 bool check_open_pipes(void)
84 struct pipes_struct *p;
86 for (p = InternalPipes; p != NULL; p = p->next) {
87 if (num_pipe_handles(p) != 0) {
94 /****************************************************************************
96 ****************************************************************************/
98 int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
101 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
105 TALLOC_FREE(p->auth.auth_ctx);
107 /* Free the handles database. */
108 close_policy_by_pipe(p);
110 free_pipe_rpc_context_internal( p->contexts );
112 DLIST_REMOVE(InternalPipes, p);
119 /****************************************************************************
120 Make an internal namedpipes structure
121 ****************************************************************************/
123 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
124 const struct ndr_syntax_id *syntax,
125 const struct tsocket_address *remote_address,
126 const struct auth_session_info *session_info,
127 struct messaging_context *msg_ctx)
129 struct pipes_struct *p;
130 struct pipe_rpc_fns *context_fns;
132 DEBUG(4,("Create pipe requested %s\n",
133 get_pipe_name_from_syntax(talloc_tos(), syntax)));
135 p = talloc_zero(mem_ctx, struct pipes_struct);
138 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
142 p->mem_ctx = talloc_named(p, 0, "pipe %s %p",
143 get_pipe_name_from_syntax(talloc_tos(),
145 if (p->mem_ctx == NULL) {
146 DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
151 if (!init_pipe_handles(p, syntax)) {
152 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
157 p->session_info = copy_session_info(p, session_info);
158 if (p->session_info == NULL) {
159 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
160 close_policy_by_pipe(p);
165 p->msg_ctx = msg_ctx;
167 DLIST_ADD(InternalPipes, p);
169 p->remote_address = tsocket_address_copy(remote_address, p);
170 if (p->remote_address == NULL) {
174 p->endian = RPC_LITTLE_ENDIAN;
176 p->transport = NCALRPC;
178 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
179 if (context_fns == NULL) {
180 DEBUG(0,("malloc() failed!\n"));
184 context_fns->next = context_fns->prev = NULL;
185 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
186 context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
187 context_fns->context_id = 0;
188 context_fns->syntax = *syntax;
190 /* add to the list of open contexts */
191 DLIST_ADD(p->contexts, context_fns);
193 DEBUG(4,("Created internal pipe %s\n",
194 get_pipe_name_from_syntax(talloc_tos(), syntax)));
196 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
201 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
204 const DATA_BLOB *in_data,
207 struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
208 uint32_t num_cmds = fns->n_cmds;
209 const struct api_struct *cmds = fns->cmds;
216 for (i = 0; i < num_cmds; i++) {
217 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
223 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
226 p->in_data.data = *in_data;
227 p->out_data.rdata = data_blob_null;
230 p->in_data.data = data_blob_null;
232 data_blob_free(&p->out_data.rdata);
233 talloc_free_children(p->mem_ctx);
234 return NT_STATUS_RPC_CALL_FAILED;
237 if (p->fault_state) {
238 p->fault_state = false;
239 data_blob_free(&p->out_data.rdata);
240 talloc_free_children(p->mem_ctx);
241 return NT_STATUS_RPC_CALL_FAILED;
244 if (p->bad_handle_fault_state) {
245 p->bad_handle_fault_state = false;
246 data_blob_free(&p->out_data.rdata);
247 talloc_free_children(p->mem_ctx);
248 return NT_STATUS_RPC_SS_CONTEXT_MISMATCH;
251 if (p->rng_fault_state) {
252 p->rng_fault_state = false;
253 data_blob_free(&p->out_data.rdata);
254 talloc_free_children(p->mem_ctx);
255 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
258 *out_data = p->out_data.rdata;
259 talloc_steal(mem_ctx, out_data->data);
260 p->out_data.rdata = data_blob_null;
262 talloc_free_children(p->mem_ctx);
266 struct rpcint_bh_state {
267 struct pipes_struct *p;
270 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
272 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
273 struct rpcint_bh_state);
282 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
285 /* TODO: implement timeouts */
289 struct rpcint_bh_raw_call_state {
295 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
296 struct tevent_context *ev,
297 struct dcerpc_binding_handle *h,
298 const struct GUID *object,
301 const uint8_t *in_data,
304 struct rpcint_bh_state *hs =
305 dcerpc_binding_handle_data(h,
306 struct rpcint_bh_state);
307 struct tevent_req *req;
308 struct rpcint_bh_raw_call_state *state;
312 req = tevent_req_create(mem_ctx, &state,
313 struct rpcint_bh_raw_call_state);
317 state->in_data.data = discard_const_p(uint8_t, in_data);
318 state->in_data.length = in_length;
320 ok = rpcint_bh_is_connected(h);
322 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
323 return tevent_req_post(req, ev);
326 /* TODO: allow async */
327 status = rpcint_dispatch(hs->p, state, opnum,
330 if (!NT_STATUS_IS_OK(status)) {
331 tevent_req_nterror(req, status);
332 return tevent_req_post(req, ev);
335 tevent_req_done(req);
336 return tevent_req_post(req, ev);
339 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
345 struct rpcint_bh_raw_call_state *state =
347 struct rpcint_bh_raw_call_state);
350 if (tevent_req_is_nterror(req, &status)) {
351 tevent_req_received(req);
355 *out_data = talloc_move(mem_ctx, &state->out_data.data);
356 *out_length = state->out_data.length;
358 tevent_req_received(req);
362 struct rpcint_bh_disconnect_state {
366 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
367 struct tevent_context *ev,
368 struct dcerpc_binding_handle *h)
370 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
371 struct rpcint_bh_state);
372 struct tevent_req *req;
373 struct rpcint_bh_disconnect_state *state;
376 req = tevent_req_create(mem_ctx, &state,
377 struct rpcint_bh_disconnect_state);
382 ok = rpcint_bh_is_connected(h);
384 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
385 return tevent_req_post(req, ev);
389 * TODO: do a real async disconnect ...
391 * For now the caller needs to free pipes_struct
395 tevent_req_done(req);
396 return tevent_req_post(req, ev);
399 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
403 if (tevent_req_is_nterror(req, &status)) {
404 tevent_req_received(req);
408 tevent_req_received(req);
412 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
417 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
419 const void *_struct_ptr,
420 const struct ndr_interface_call *call)
422 void *struct_ptr = discard_const(_struct_ptr);
424 if (DEBUGLEVEL < 11) {
428 if (ndr_flags & NDR_IN) {
429 ndr_print_function_debug(call->ndr_print,
434 if (ndr_flags & NDR_OUT) {
435 ndr_print_function_debug(call->ndr_print,
442 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
444 .is_connected = rpcint_bh_is_connected,
445 .set_timeout = rpcint_bh_set_timeout,
446 .raw_call_send = rpcint_bh_raw_call_send,
447 .raw_call_recv = rpcint_bh_raw_call_recv,
448 .disconnect_send = rpcint_bh_disconnect_send,
449 .disconnect_recv = rpcint_bh_disconnect_recv,
451 .ref_alloc = rpcint_bh_ref_alloc,
452 .do_ndr_print = rpcint_bh_do_ndr_print,
455 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
456 const struct ndr_syntax_id *abstract_syntax,
457 const struct ndr_interface_table *ndr_table,
458 const struct tsocket_address *remote_address,
459 const struct auth_session_info *session_info,
460 struct messaging_context *msg_ctx,
461 struct dcerpc_binding_handle **binding_handle)
463 struct dcerpc_binding_handle *h;
464 struct rpcint_bh_state *hs;
467 abstract_syntax = &ndr_table->syntax_id;
470 h = dcerpc_binding_handle_create(mem_ctx,
475 struct rpcint_bh_state,
478 return NT_STATUS_NO_MEMORY;
480 hs->p = make_internal_rpc_pipe_p(hs,
487 return NT_STATUS_NO_MEMORY;
494 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
496 * @param[in] mem_ctx The memory context to use.
498 * @param[in] ndr_table Normally the ndr_table_<name>.
500 * @param[in] remote_address The info about the connected client.
502 * @param[in] serversupplied_info The server supplied authentication function.
504 * @param[in] msg_ctx The messaging context that can be used by the server
506 * @param[out] binding_handle A pointer to store the connected
507 * dcerpc_binding_handle
509 * @return NT_STATUS_OK on success, a corresponding NT status if an
513 * struct dcerpc_binding_handle *winreg_binding;
516 * status = rpcint_binding_handle(tmp_ctx,
524 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
525 const struct ndr_interface_table *ndr_table,
526 const struct tsocket_address *remote_address,
527 const struct auth_session_info *session_info,
528 struct messaging_context *msg_ctx,
529 struct dcerpc_binding_handle **binding_handle)
531 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
532 session_info, msg_ctx, binding_handle);
538 * @brief Create a new RPC client context which uses a local transport.
540 * This creates a local transport. It is a shortcut to directly call the server
541 * functions and avoid marshalling.
542 * NOTE: this function should be used only by rpc_pipe_open_interface()
544 * @param[in] mem_ctx The memory context to use.
546 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
549 * @param[in] serversupplied_info The server supplied authentication function.
551 * @param[in] remote_address The client address information.
553 * @param[in] msg_ctx The messaging context to use.
555 * @param[out] presult A pointer to store the connected rpc client pipe.
557 * @return NT_STATUS_OK on success, a corresponding NT status if an
560 static NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
561 const struct ndr_syntax_id *abstract_syntax,
562 const struct auth_session_info *session_info,
563 const struct tsocket_address *remote_address,
564 struct messaging_context *msg_ctx,
565 struct rpc_pipe_client **presult)
567 struct rpc_pipe_client *result;
570 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
571 if (result == NULL) {
572 return NT_STATUS_NO_MEMORY;
575 result->abstract_syntax = *abstract_syntax;
576 result->transfer_syntax = ndr_transfer_syntax;
578 if (remote_address == NULL) {
579 struct tsocket_address *local;
582 rc = tsocket_address_inet_from_strings(mem_ctx,
589 return NT_STATUS_NO_MEMORY;
592 remote_address = local;
595 result->max_xmit_frag = -1;
596 result->max_recv_frag = -1;
598 status = rpcint_binding_handle_ex(result,
604 &result->binding_handle);
605 if (!NT_STATUS_IS_OK(status)) {
614 /****************************************************************************
615 * External pipes functions
616 ***************************************************************************/
619 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
620 const char *pipe_name,
621 const struct tsocket_address *local_address,
622 const struct tsocket_address *remote_address,
623 const struct auth_session_info *session_info)
625 struct np_proxy_state *result;
627 const char *socket_dir;
628 struct tevent_context *ev;
629 struct tevent_req *subreq;
630 struct auth_session_info_transport *session_info_t;
635 result = talloc(mem_ctx, struct np_proxy_state);
636 if (result == NULL) {
637 DEBUG(0, ("talloc failed\n"));
641 result->read_queue = tevent_queue_create(result, "np_read");
642 if (result->read_queue == NULL) {
643 DEBUG(0, ("tevent_queue_create failed\n"));
647 result->write_queue = tevent_queue_create(result, "np_write");
648 if (result->write_queue == NULL) {
649 DEBUG(0, ("tevent_queue_create failed\n"));
653 ev = s3_tevent_context_init(talloc_tos());
655 DEBUG(0, ("s3_tevent_context_init failed\n"));
659 socket_dir = lp_parm_const_string(
660 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
662 if (socket_dir == NULL) {
663 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
666 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
667 if (socket_np_dir == NULL) {
668 DEBUG(0, ("talloc_asprintf failed\n"));
672 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
673 if (session_info_t == NULL) {
674 DEBUG(0, ("talloc failed\n"));
678 session_info_t->session_info = copy_session_info(session_info_t,
680 if (session_info_t->session_info == NULL) {
681 DEBUG(0, ("copy_session_info failed\n"));
686 subreq = tstream_npa_connect_send(talloc_tos(), ev,
689 remote_address, /* client_addr */
690 NULL, /* client_name */
691 local_address, /* server_addr */
692 NULL, /* server_name */
694 if (subreq == NULL) {
696 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
697 "user %s\\%s failed\n",
698 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
699 session_info_t->session_info->info->account_name));
702 ok = tevent_req_poll(subreq, ev);
705 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
706 "failed for tstream_npa_connect: %s\n",
707 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
708 session_info_t->session_info->info->account_name,
713 ret = tstream_npa_connect_recv(subreq, &sys_errno,
717 &result->device_state,
718 &result->allocation_size);
721 DEBUG(0, ("tstream_npa_connect_recv to %s for pipe %s and "
722 "user %s\\%s failed: %s\n",
723 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
724 session_info_t->session_info->info->account_name,
725 strerror(sys_errno)));
736 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
737 const char *pipe_name,
738 const struct ndr_syntax_id *abstract_syntax,
739 const struct auth_session_info *session_info,
740 struct rpc_pipe_client **_result)
742 struct tsocket_address *local, *remote;
743 struct rpc_pipe_client *result = NULL;
744 struct np_proxy_state *proxy_state = NULL;
745 struct pipe_auth_data *auth;
749 /* this is an internal connection, fake up ip addresses */
750 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
753 return NT_STATUS_NO_MEMORY;
755 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
758 return NT_STATUS_NO_MEMORY;
761 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
762 local, remote, session_info);
764 return NT_STATUS_UNSUCCESSFUL;
767 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
768 if (result == NULL) {
769 status = NT_STATUS_NO_MEMORY;
773 result->abstract_syntax = *abstract_syntax;
774 result->transfer_syntax = ndr_transfer_syntax;
776 result->desthost = get_myname(result);
777 result->srv_name_slash = talloc_asprintf_strupper_m(
778 result, "\\\\%s", result->desthost);
779 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
780 status = NT_STATUS_NO_MEMORY;
784 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
785 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
787 status = rpc_transport_tstream_init(result,
790 if (!NT_STATUS_IS_OK(status)) {
794 result->binding_handle = rpccli_bh_create(result);
795 if (result->binding_handle == NULL) {
796 status = NT_STATUS_NO_MEMORY;
797 DEBUG(0, ("Failed to create binding handle.\n"));
801 result->auth = talloc_zero(result, struct pipe_auth_data);
803 status = NT_STATUS_NO_MEMORY;
806 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
807 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
809 status = rpccli_anon_bind_data(result, &auth);
810 if (!NT_STATUS_IS_OK(status)) {
811 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
815 status = rpc_pipe_bind(result, auth);
816 if (!NT_STATUS_IS_OK(status)) {
817 DEBUG(0, ("Failed to bind external pipe.\n"));
822 if (!NT_STATUS_IS_OK(status)) {
825 TALLOC_FREE(proxy_state);
831 * @brief Create a new RPC client context which uses a local dispatch function
832 * or a remote transport, depending on rpc_server configuration for the
835 * @param[in] mem_ctx The memory context to use.
837 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
840 * @param[in] serversupplied_info The server supplied authentication function.
842 * @param[in] remote_address The client address information.
844 * @param[in] msg_ctx The messaging context to use.
846 * @param[out] presult A pointer to store the connected rpc client pipe.
848 * @return NT_STATUS_OK on success, a corresponding NT status if an
852 * struct rpc_pipe_client *winreg_pipe;
855 * status = rpc_pipe_open_interface(tmp_ctx,
856 * &ndr_table_winreg.syntax_id,
863 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
864 const struct ndr_syntax_id *syntax,
865 const struct auth_session_info *session_info,
866 const struct tsocket_address *remote_address,
867 struct messaging_context *msg_ctx,
868 struct rpc_pipe_client **cli_pipe)
870 struct rpc_pipe_client *cli = NULL;
871 const char *server_type;
872 const char *pipe_name;
876 if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
879 TALLOC_FREE(*cli_pipe);
882 tmp_ctx = talloc_stackframe();
883 if (tmp_ctx == NULL) {
884 return NT_STATUS_NO_MEMORY;
887 pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
888 if (pipe_name == NULL) {
889 status = NT_STATUS_INVALID_PARAMETER;
893 while (pipe_name[0] == '\\') {
897 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
899 server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
900 "rpc_server", pipe_name,
903 if (strcasecmp_m(server_type, "embedded") == 0) {
904 status = rpc_pipe_open_internal(tmp_ctx,
905 syntax, session_info,
906 remote_address, msg_ctx,
908 if (!NT_STATUS_IS_OK(status)) {
911 } else if (strcasecmp_m(server_type, "daemon") == 0 ||
912 strcasecmp_m(server_type, "external") == 0) {
913 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
914 * for now we need to use the special proxy setup to connect
917 status = rpc_pipe_open_external(tmp_ctx,
921 if (!NT_STATUS_IS_OK(status)) {
925 status = NT_STATUS_NOT_IMPLEMENTED;
926 DEBUG(0, ("Wrong servertype specified in config file: %s",
931 status = NT_STATUS_OK;
933 if (NT_STATUS_IS_OK(status)) {
934 *cli_pipe = talloc_move(mem_ctx, &cli);
936 TALLOC_FREE(tmp_ctx);