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"
41 #define DBGC_CLASS DBGC_RPC_SRV
43 /****************************************************************************
44 Make an internal namedpipes structure
45 ****************************************************************************/
47 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
48 const struct ndr_syntax_id *syntax,
49 const struct tsocket_address *remote_address,
50 const struct auth_session_info *session_info,
51 struct messaging_context *msg_ctx)
53 struct pipes_struct *p;
54 struct pipe_rpc_fns *context_fns;
55 const char *pipe_name;
58 pipe_name = get_pipe_name_from_syntax(talloc_tos(), syntax);
60 DEBUG(4,("Create pipe requested %s\n", pipe_name));
62 ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
63 NCALRPC, RPC_LITTLE_ENDIAN, false,
64 remote_address, NULL, &p);
66 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
70 if (!init_pipe_handles(p, syntax)) {
71 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
76 p->session_info = copy_session_info(p, session_info);
77 if (p->session_info == NULL) {
78 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
79 close_policy_by_pipe(p);
84 context_fns = talloc(p, struct pipe_rpc_fns);
85 if (context_fns == NULL) {
86 DEBUG(0,("talloc() failed!\n"));
91 context_fns->next = context_fns->prev = NULL;
92 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
93 context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
94 context_fns->context_id = 0;
95 context_fns->syntax = *syntax;
97 /* add to the list of open contexts */
98 DLIST_ADD(p->contexts, context_fns);
100 DEBUG(4,("Created internal pipe %s\n", pipe_name));
105 struct rpcint_dispatch_state {
106 const struct api_struct *cmd;
108 struct pipes_struct *p;
110 static void rpcint_dispatch_done(struct tevent_req *subreq);
112 static struct tevent_req *rpcint_dispatch_send(struct tevent_context *ev,
114 struct pipes_struct *p,
116 const DATA_BLOB *in_data)
118 struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
119 uint32_t num_cmds = fns->n_cmds;
120 const struct api_struct *cmds = fns->cmds;
122 struct rpcint_dispatch_state *state;
123 struct tevent_req *subreq;
124 struct tevent_req *req = tevent_req_create(mem_ctx, &state,
125 struct rpcint_dispatch_state);
134 for (i = 0; i < num_cmds; i++) {
135 if (cmds[i].opnum == opnum && cmds[i].fn_send != NULL) {
141 tevent_req_nterror(req, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE);
142 return tevent_req_post(req, ev);
145 state->cmd = &cmds[i];
146 p->in_data.data = *in_data;
147 p->out_data.rdata = data_blob_null;
149 subreq = state->cmd->fn_send(ev, state, p);
150 if (tevent_req_nomem(subreq, req)) {
151 return tevent_req_post(req, ev);
153 tevent_req_set_callback(subreq, rpcint_dispatch_done, req);
157 static void rpcint_dispatch_done(struct tevent_req *subreq)
159 struct tevent_req *req = tevent_req_callback_data(subreq,
161 struct rpcint_dispatch_state *state
162 = tevent_req_data(req, struct rpcint_dispatch_state);
163 struct pipes_struct *p = state->p;
166 ok = state->cmd->fn_recv(subreq);
167 p->in_data.data = data_blob_null;
169 data_blob_free(&p->out_data.rdata);
170 talloc_free_children(p->mem_ctx);
171 tevent_req_nterror(req, NT_STATUS_RPC_CALL_FAILED);
175 if (p->fault_state) {
176 NTSTATUS status = NT_STATUS(p->fault_state);
178 data_blob_free(&p->out_data.rdata);
179 talloc_free_children(p->mem_ctx);
180 tevent_req_nterror(req, status);
184 state->out_data = p->out_data.rdata;
185 talloc_steal(state, state->out_data.data);
186 p->out_data.rdata = data_blob_null;
188 talloc_free_children(p->mem_ctx);
189 tevent_req_done(req);
192 static NTSTATUS rpcint_dispatch_recv(struct tevent_req *req,
196 struct rpcint_dispatch_state *state
197 = tevent_req_data(req, struct rpcint_dispatch_state);
200 if (tevent_req_is_nterror(req, &status)) {
201 tevent_req_received(req);
205 *out_data = state->out_data;
206 talloc_steal(mem_ctx, out_data->data);
207 tevent_req_received(req);
211 struct rpcint_bh_state {
212 struct pipes_struct *p;
215 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
217 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
218 struct rpcint_bh_state);
227 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
230 /* TODO: implement timeouts */
234 struct rpcint_bh_raw_call_state {
239 static void rpcint_bh_raw_call_done(struct tevent_req *subreq);
241 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
242 struct tevent_context *ev,
243 struct dcerpc_binding_handle *h,
244 const struct GUID *object,
247 const uint8_t *in_data,
250 struct rpcint_bh_state *hs =
251 dcerpc_binding_handle_data(h,
252 struct rpcint_bh_state);
253 struct tevent_req *req;
254 struct tevent_req *subreq;
255 struct rpcint_bh_raw_call_state *state;
258 req = tevent_req_create(mem_ctx, &state,
259 struct rpcint_bh_raw_call_state);
263 state->in_data.data = discard_const_p(uint8_t, in_data);
264 state->in_data.length = in_length;
266 ok = rpcint_bh_is_connected(h);
268 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
269 return tevent_req_post(req, ev);
272 subreq = rpcint_dispatch_send(ev, state, hs->p, opnum,
274 if (tevent_req_nomem(subreq, req)) {
275 return tevent_req_post(req, ev);
277 tevent_req_set_callback(subreq, rpcint_bh_raw_call_done, req);
282 static void rpcint_bh_raw_call_done(struct tevent_req *subreq)
284 struct tevent_req *req = tevent_req_callback_data(subreq,
286 struct rpcint_bh_raw_call_state *state
287 = tevent_req_data(req, struct rpcint_bh_raw_call_state);
290 status = rpcint_dispatch_recv(subreq, state,
292 if (!NT_STATUS_IS_OK(status)) {
293 tevent_req_nterror(req, status);
296 tevent_req_done(req);
299 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
305 struct rpcint_bh_raw_call_state *state =
307 struct rpcint_bh_raw_call_state);
310 if (tevent_req_is_nterror(req, &status)) {
311 tevent_req_received(req);
315 *out_data = talloc_move(mem_ctx, &state->out_data.data);
316 *out_length = state->out_data.length;
318 tevent_req_received(req);
322 struct rpcint_bh_disconnect_state {
326 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
327 struct tevent_context *ev,
328 struct dcerpc_binding_handle *h)
330 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
331 struct rpcint_bh_state);
332 struct tevent_req *req;
333 struct rpcint_bh_disconnect_state *state;
336 req = tevent_req_create(mem_ctx, &state,
337 struct rpcint_bh_disconnect_state);
342 ok = rpcint_bh_is_connected(h);
344 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
345 return tevent_req_post(req, ev);
349 * TODO: do a real async disconnect ...
351 * For now the caller needs to free pipes_struct
355 tevent_req_done(req);
356 return tevent_req_post(req, ev);
359 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
363 if (tevent_req_is_nterror(req, &status)) {
364 tevent_req_received(req);
368 tevent_req_received(req);
372 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
377 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
379 const void *_struct_ptr,
380 const struct ndr_interface_call *call)
382 void *struct_ptr = discard_const(_struct_ptr);
384 if (DEBUGLEVEL < 11) {
388 if (ndr_flags & NDR_IN) {
389 ndr_print_function_debug(call->ndr_print,
394 if (ndr_flags & NDR_OUT) {
395 ndr_print_function_debug(call->ndr_print,
402 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
404 .is_connected = rpcint_bh_is_connected,
405 .set_timeout = rpcint_bh_set_timeout,
406 .raw_call_send = rpcint_bh_raw_call_send,
407 .raw_call_recv = rpcint_bh_raw_call_recv,
408 .disconnect_send = rpcint_bh_disconnect_send,
409 .disconnect_recv = rpcint_bh_disconnect_recv,
411 .ref_alloc = rpcint_bh_ref_alloc,
412 .do_ndr_print = rpcint_bh_do_ndr_print,
415 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
416 const struct ndr_syntax_id *abstract_syntax,
417 const struct ndr_interface_table *ndr_table,
418 const struct tsocket_address *remote_address,
419 const struct auth_session_info *session_info,
420 struct messaging_context *msg_ctx,
421 struct dcerpc_binding_handle **binding_handle)
423 struct dcerpc_binding_handle *h;
424 struct rpcint_bh_state *hs;
427 abstract_syntax = &ndr_table->syntax_id;
430 h = dcerpc_binding_handle_create(mem_ctx,
435 struct rpcint_bh_state,
438 return NT_STATUS_NO_MEMORY;
440 hs->p = make_internal_rpc_pipe_p(hs,
447 return NT_STATUS_NO_MEMORY;
454 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
456 * @param[in] mem_ctx The memory context to use.
458 * @param[in] ndr_table Normally the ndr_table_<name>.
460 * @param[in] remote_address The info about the connected client.
462 * @param[in] serversupplied_info The server supplied authentication function.
464 * @param[in] msg_ctx The messaging context that can be used by the server
466 * @param[out] binding_handle A pointer to store the connected
467 * dcerpc_binding_handle
469 * @return NT_STATUS_OK on success, a corresponding NT status if an
473 * struct dcerpc_binding_handle *winreg_binding;
476 * status = rpcint_binding_handle(tmp_ctx,
484 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
485 const struct ndr_interface_table *ndr_table,
486 const struct tsocket_address *remote_address,
487 const struct auth_session_info *session_info,
488 struct messaging_context *msg_ctx,
489 struct dcerpc_binding_handle **binding_handle)
491 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
492 session_info, msg_ctx, binding_handle);
498 * @brief Create a new RPC client context which uses a local transport.
500 * This creates a local transport. It is a shortcut to directly call the server
501 * functions and avoid marshalling.
502 * NOTE: this function should be used only by rpc_pipe_open_interface()
504 * @param[in] mem_ctx The memory context to use.
506 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
509 * @param[in] serversupplied_info The server supplied authentication function.
511 * @param[in] remote_address The client address information.
513 * @param[in] msg_ctx The messaging context to use.
515 * @param[out] presult A pointer to store the connected rpc client pipe.
517 * @return NT_STATUS_OK on success, a corresponding NT status if an
520 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
521 const struct ndr_syntax_id *abstract_syntax,
522 const struct auth_session_info *session_info,
523 const struct tsocket_address *remote_address,
524 struct messaging_context *msg_ctx,
525 struct rpc_pipe_client **presult)
527 struct rpc_pipe_client *result;
530 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
531 if (result == NULL) {
532 return NT_STATUS_NO_MEMORY;
535 result->abstract_syntax = *abstract_syntax;
536 result->transfer_syntax = ndr_transfer_syntax_ndr;
538 if (remote_address == NULL) {
539 struct tsocket_address *local;
542 rc = tsocket_address_inet_from_strings(mem_ctx,
549 return NT_STATUS_NO_MEMORY;
552 remote_address = local;
555 result->max_xmit_frag = -1;
556 result->max_recv_frag = -1;
558 status = rpcint_binding_handle_ex(result,
564 &result->binding_handle);
565 if (!NT_STATUS_IS_OK(status)) {
574 /****************************************************************************
575 * External pipes functions
576 ***************************************************************************/
579 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
580 const char *pipe_name,
581 const struct tsocket_address *local_address,
582 const struct tsocket_address *remote_address,
583 const struct auth_session_info *session_info)
585 struct np_proxy_state *result;
587 const char *socket_dir;
588 struct tevent_context *ev;
589 struct tevent_req *subreq;
590 struct auth_session_info_transport *session_info_t;
595 result = talloc(mem_ctx, struct np_proxy_state);
596 if (result == NULL) {
597 DEBUG(0, ("talloc failed\n"));
601 result->read_queue = tevent_queue_create(result, "np_read");
602 if (result->read_queue == NULL) {
603 DEBUG(0, ("tevent_queue_create failed\n"));
607 result->write_queue = tevent_queue_create(result, "np_write");
608 if (result->write_queue == NULL) {
609 DEBUG(0, ("tevent_queue_create failed\n"));
613 ev = s3_tevent_context_init(talloc_tos());
615 DEBUG(0, ("s3_tevent_context_init failed\n"));
619 socket_dir = lp_parm_const_string(
620 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
622 if (socket_dir == NULL) {
623 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
626 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
627 if (socket_np_dir == NULL) {
628 DEBUG(0, ("talloc_asprintf failed\n"));
632 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
633 if (session_info_t == NULL) {
634 DEBUG(0, ("talloc failed\n"));
638 session_info_t->session_info = copy_session_info(session_info_t,
640 if (session_info_t->session_info == NULL) {
641 DEBUG(0, ("copy_session_info failed\n"));
646 subreq = tstream_npa_connect_send(talloc_tos(), ev,
649 remote_address, /* client_addr */
650 NULL, /* client_name */
651 local_address, /* server_addr */
652 NULL, /* server_name */
654 if (subreq == NULL) {
656 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
657 "user %s\\%s failed\n",
658 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
659 session_info_t->session_info->info->account_name));
662 ok = tevent_req_poll(subreq, ev);
665 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
666 "failed for tstream_npa_connect: %s\n",
667 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
668 session_info_t->session_info->info->account_name,
673 ret = tstream_npa_connect_recv(subreq, &sys_errno,
677 &result->device_state,
678 &result->allocation_size);
682 if (errno == ENOENT) {
685 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
686 "user %s\\%s failed: %s\n",
687 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
688 session_info_t->session_info->info->account_name,
689 strerror(sys_errno)));
700 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
701 const char *pipe_name,
702 const struct ndr_syntax_id *abstract_syntax,
703 const struct auth_session_info *session_info,
704 struct rpc_pipe_client **_result)
706 struct tsocket_address *local, *remote;
707 struct rpc_pipe_client *result = NULL;
708 struct np_proxy_state *proxy_state = NULL;
709 struct pipe_auth_data *auth;
713 /* this is an internal connection, fake up ip addresses */
714 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
717 return NT_STATUS_NO_MEMORY;
719 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
722 return NT_STATUS_NO_MEMORY;
725 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
726 local, remote, session_info);
728 return NT_STATUS_UNSUCCESSFUL;
731 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
732 if (result == NULL) {
733 status = NT_STATUS_NO_MEMORY;
737 result->abstract_syntax = *abstract_syntax;
738 result->transfer_syntax = ndr_transfer_syntax_ndr;
740 result->desthost = get_myname(result);
741 result->srv_name_slash = talloc_asprintf_strupper_m(
742 result, "\\\\%s", result->desthost);
743 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
744 status = NT_STATUS_NO_MEMORY;
748 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
749 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
751 status = rpc_transport_tstream_init(result,
754 if (!NT_STATUS_IS_OK(status)) {
758 result->binding_handle = rpccli_bh_create(result);
759 if (result->binding_handle == NULL) {
760 status = NT_STATUS_NO_MEMORY;
761 DEBUG(0, ("Failed to create binding handle.\n"));
765 result->auth = talloc_zero(result, struct pipe_auth_data);
767 status = NT_STATUS_NO_MEMORY;
770 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
771 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
773 status = rpccli_anon_bind_data(result, &auth);
774 if (!NT_STATUS_IS_OK(status)) {
775 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
779 status = rpc_pipe_bind(result, auth);
780 if (!NT_STATUS_IS_OK(status)) {
781 DEBUG(0, ("Failed to bind external pipe.\n"));
786 if (!NT_STATUS_IS_OK(status)) {
789 TALLOC_FREE(proxy_state);
795 * @brief Create a new RPC client context which uses a local dispatch function
796 * or a remote transport, depending on rpc_server configuration for the
799 * @param[in] mem_ctx The memory context to use.
801 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
804 * @param[in] serversupplied_info The server supplied authentication function.
806 * @param[in] remote_address The client address information.
808 * @param[in] msg_ctx The messaging context to use.
810 * @param[out] presult A pointer to store the connected rpc client pipe.
812 * @return NT_STATUS_OK on success, a corresponding NT status if an
816 * struct rpc_pipe_client *winreg_pipe;
819 * status = rpc_pipe_open_interface(tmp_ctx,
820 * &ndr_table_winreg.syntax_id,
827 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
828 const struct ndr_syntax_id *syntax,
829 const struct auth_session_info *session_info,
830 const struct tsocket_address *remote_address,
831 struct messaging_context *msg_ctx,
832 struct rpc_pipe_client **cli_pipe)
834 struct rpc_pipe_client *cli = NULL;
835 enum rpc_service_mode_e pipe_mode;
836 const char *pipe_name;
840 if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
843 TALLOC_FREE(*cli_pipe);
846 tmp_ctx = talloc_stackframe();
847 if (tmp_ctx == NULL) {
848 return NT_STATUS_NO_MEMORY;
851 pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
852 if (pipe_name == NULL) {
853 status = NT_STATUS_INVALID_PARAMETER;
857 while (pipe_name[0] == '\\') {
861 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
863 pipe_mode = rpc_service_mode(pipe_name);
866 case RPC_SERVICE_MODE_EMBEDDED:
867 status = rpc_pipe_open_internal(tmp_ctx,
868 syntax, session_info,
869 remote_address, msg_ctx,
871 if (!NT_STATUS_IS_OK(status)) {
875 case RPC_SERVICE_MODE_EXTERNAL:
876 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
877 * for now we need to use the special proxy setup to connect
880 status = rpc_pipe_open_external(tmp_ctx,
884 if (!NT_STATUS_IS_OK(status)) {
888 case RPC_SERVICE_MODE_DISABLED:
889 status = NT_STATUS_NOT_IMPLEMENTED;
890 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
891 pipe_name, nt_errstr(status)));
895 status = NT_STATUS_OK;
897 if (NT_STATUS_IS_OK(status)) {
898 *cli_pipe = talloc_move(mem_ctx, &cli);
900 TALLOC_FREE(tmp_ctx);