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 auth3_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 auth3_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 auth3_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 auth3_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 auth3_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;
631 struct auth_session_info *session_info_npa;
637 result = talloc(mem_ctx, struct np_proxy_state);
638 if (result == NULL) {
639 DEBUG(0, ("talloc failed\n"));
643 result->read_queue = tevent_queue_create(result, "np_read");
644 if (result->read_queue == NULL) {
645 DEBUG(0, ("tevent_queue_create failed\n"));
649 result->write_queue = tevent_queue_create(result, "np_write");
650 if (result->write_queue == NULL) {
651 DEBUG(0, ("tevent_queue_create failed\n"));
655 ev = s3_tevent_context_init(talloc_tos());
657 DEBUG(0, ("s3_tevent_context_init failed\n"));
661 socket_dir = lp_parm_const_string(
662 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
664 if (socket_dir == NULL) {
665 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
668 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
669 if (socket_np_dir == NULL) {
670 DEBUG(0, ("talloc_asprintf failed\n"));
674 session_info_npa = talloc_zero(talloc_tos(), struct auth_session_info);
675 if (session_info_npa == NULL) {
676 DEBUG(0, ("talloc failed\n"));
680 /* Send the named_pipe_auth server the user's full token */
681 session_info_npa->security_token = session_info->security_token;
682 session_info_npa->session_key = session_info->session_key;
683 session_info_npa->unix_token = session_info->unix_token;
684 session_info_npa->unix_info = session_info->unix_info;
686 session_info_npa->info = session_info->info;
688 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
689 if (session_info_npa == NULL) {
690 DEBUG(0, ("talloc failed\n"));
694 session_info_t->session_info = talloc_steal(session_info_t, session_info_npa);
697 subreq = tstream_npa_connect_send(talloc_tos(), ev,
700 remote_address, /* client_addr */
701 NULL, /* client_name */
702 local_address, /* server_addr */
703 NULL, /* server_name */
705 if (subreq == NULL) {
707 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
708 "user %s\\%s failed\n",
709 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
710 session_info_t->session_info->info->account_name));
713 ok = tevent_req_poll(subreq, ev);
716 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
717 "failed for tstream_npa_connect: %s\n",
718 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
719 session_info_t->session_info->info->account_name,
724 ret = tstream_npa_connect_recv(subreq, &sys_errno,
728 &result->device_state,
729 &result->allocation_size);
732 DEBUG(0, ("tstream_npa_connect_recv to %s for pipe %s and "
733 "user %s\\%s failed: %s\n",
734 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
735 session_info_t->session_info->info->account_name,
736 strerror(sys_errno)));
747 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
748 const char *pipe_name,
749 const struct ndr_syntax_id *abstract_syntax,
750 const struct auth3_session_info *session_info,
751 struct rpc_pipe_client **_result)
753 struct tsocket_address *local, *remote;
754 struct rpc_pipe_client *result = NULL;
755 struct np_proxy_state *proxy_state = NULL;
756 struct pipe_auth_data *auth;
760 /* this is an internal connection, fake up ip addresses */
761 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
764 return NT_STATUS_NO_MEMORY;
766 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
769 return NT_STATUS_NO_MEMORY;
772 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
773 local, remote, session_info);
775 return NT_STATUS_UNSUCCESSFUL;
778 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
779 if (result == NULL) {
780 status = NT_STATUS_NO_MEMORY;
784 result->abstract_syntax = *abstract_syntax;
785 result->transfer_syntax = ndr_transfer_syntax;
787 result->desthost = get_myname(result);
788 result->srv_name_slash = talloc_asprintf_strupper_m(
789 result, "\\\\%s", result->desthost);
790 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
791 status = NT_STATUS_NO_MEMORY;
795 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
796 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
798 status = rpc_transport_tstream_init(result,
801 if (!NT_STATUS_IS_OK(status)) {
805 result->binding_handle = rpccli_bh_create(result);
806 if (result->binding_handle == NULL) {
807 status = NT_STATUS_NO_MEMORY;
808 DEBUG(0, ("Failed to create binding handle.\n"));
812 result->auth = talloc_zero(result, struct pipe_auth_data);
814 status = NT_STATUS_NO_MEMORY;
817 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
818 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
820 status = rpccli_anon_bind_data(result, &auth);
821 if (!NT_STATUS_IS_OK(status)) {
822 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
826 status = rpc_pipe_bind(result, auth);
827 if (!NT_STATUS_IS_OK(status)) {
828 DEBUG(0, ("Failed to bind external pipe.\n"));
833 if (!NT_STATUS_IS_OK(status)) {
836 TALLOC_FREE(proxy_state);
842 * @brief Create a new RPC client context which uses a local dispatch function
843 * or a remote transport, depending on rpc_server configuration for the
846 * @param[in] mem_ctx The memory context to use.
848 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
851 * @param[in] serversupplied_info The server supplied authentication function.
853 * @param[in] remote_address The client address information.
855 * @param[in] msg_ctx The messaging context to use.
857 * @param[out] presult A pointer to store the connected rpc client pipe.
859 * @return NT_STATUS_OK on success, a corresponding NT status if an
863 * struct rpc_pipe_client *winreg_pipe;
866 * status = rpc_pipe_open_interface(tmp_ctx,
867 * &ndr_table_winreg.syntax_id,
874 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
875 const struct ndr_syntax_id *syntax,
876 const struct auth3_session_info *session_info,
877 const struct tsocket_address *remote_address,
878 struct messaging_context *msg_ctx,
879 struct rpc_pipe_client **cli_pipe)
881 struct rpc_pipe_client *cli = NULL;
882 const char *server_type;
883 const char *pipe_name;
887 if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
890 TALLOC_FREE(*cli_pipe);
893 tmp_ctx = talloc_stackframe();
894 if (tmp_ctx == NULL) {
895 return NT_STATUS_NO_MEMORY;
898 pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
899 if (pipe_name == NULL) {
900 status = NT_STATUS_INVALID_PARAMETER;
904 while (pipe_name[0] == '\\') {
908 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
910 server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
911 "rpc_server", pipe_name,
914 if (strcasecmp_m(server_type, "embedded") == 0) {
915 status = rpc_pipe_open_internal(tmp_ctx,
916 syntax, session_info,
917 remote_address, msg_ctx,
919 if (!NT_STATUS_IS_OK(status)) {
922 } else if (strcasecmp_m(server_type, "daemon") == 0 ||
923 strcasecmp_m(server_type, "external") == 0) {
924 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
925 * for now we need to use the special proxy setup to connect
928 status = rpc_pipe_open_external(tmp_ctx,
932 if (!NT_STATUS_IS_OK(status)) {
936 status = NT_STATUS_NOT_IMPLEMENTED;
937 DEBUG(0, ("Wrong servertype specified in config file: %s",
942 status = NT_STATUS_OK;
944 if (NT_STATUS_IS_OK(status)) {
945 *cli_pipe = talloc_move(mem_ctx, &cli);
947 TALLOC_FREE(tmp_ctx);