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"
39 #include "librpc/ndr/ndr_table.h"
42 #define DBGC_CLASS DBGC_RPC_SRV
44 /****************************************************************************
45 Make an internal namedpipes structure
46 ****************************************************************************/
48 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
49 const struct ndr_syntax_id *syntax,
50 const struct tsocket_address *remote_address,
51 const struct auth_session_info *session_info,
52 struct messaging_context *msg_ctx)
54 struct pipes_struct *p;
55 struct pipe_rpc_fns *context_fns;
56 const char *pipe_name;
58 const struct ndr_interface_table *table;
60 table = ndr_table_by_uuid(&syntax->uuid);
62 DEBUG(0,("unknown interface\n"));
66 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
68 DEBUG(4,("Create pipe requested %s\n", pipe_name));
70 ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
71 NCALRPC, RPC_LITTLE_ENDIAN, false,
72 remote_address, NULL, &p);
74 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
78 if (!init_pipe_handles(p, syntax)) {
79 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
84 p->session_info = copy_session_info(p, session_info);
85 if (p->session_info == NULL) {
86 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
87 close_policy_by_pipe(p);
92 context_fns = talloc(p, struct pipe_rpc_fns);
93 if (context_fns == NULL) {
94 DEBUG(0,("talloc() failed!\n"));
99 context_fns->next = context_fns->prev = NULL;
100 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
101 context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
102 context_fns->context_id = 0;
103 context_fns->syntax = *syntax;
105 /* add to the list of open contexts */
106 DLIST_ADD(p->contexts, context_fns);
108 DEBUG(4,("Created internal pipe %s\n", pipe_name));
113 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
116 const DATA_BLOB *in_data,
119 struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
120 uint32_t num_cmds = fns->n_cmds;
121 const struct api_struct *cmds = fns->cmds;
128 for (i = 0; i < num_cmds; i++) {
129 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
135 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
138 p->in_data.data = *in_data;
139 p->out_data.rdata = data_blob_null;
142 p->in_data.data = data_blob_null;
144 data_blob_free(&p->out_data.rdata);
145 talloc_free_children(p->mem_ctx);
146 return NT_STATUS_RPC_CALL_FAILED;
149 if (p->fault_state) {
152 status = NT_STATUS(p->fault_state);
154 data_blob_free(&p->out_data.rdata);
155 talloc_free_children(p->mem_ctx);
159 *out_data = p->out_data.rdata;
160 talloc_steal(mem_ctx, out_data->data);
161 p->out_data.rdata = data_blob_null;
163 talloc_free_children(p->mem_ctx);
167 struct rpcint_bh_state {
168 struct pipes_struct *p;
171 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
173 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
174 struct rpcint_bh_state);
183 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
186 /* TODO: implement timeouts */
190 struct rpcint_bh_raw_call_state {
196 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
197 struct tevent_context *ev,
198 struct dcerpc_binding_handle *h,
199 const struct GUID *object,
202 const uint8_t *in_data,
205 struct rpcint_bh_state *hs =
206 dcerpc_binding_handle_data(h,
207 struct rpcint_bh_state);
208 struct tevent_req *req;
209 struct rpcint_bh_raw_call_state *state;
213 req = tevent_req_create(mem_ctx, &state,
214 struct rpcint_bh_raw_call_state);
218 state->in_data.data = discard_const_p(uint8_t, in_data);
219 state->in_data.length = in_length;
221 ok = rpcint_bh_is_connected(h);
223 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
224 return tevent_req_post(req, ev);
227 /* TODO: allow async */
228 status = rpcint_dispatch(hs->p, state, opnum,
231 if (!NT_STATUS_IS_OK(status)) {
232 tevent_req_nterror(req, status);
233 return tevent_req_post(req, ev);
236 tevent_req_done(req);
237 return tevent_req_post(req, ev);
240 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
246 struct rpcint_bh_raw_call_state *state =
248 struct rpcint_bh_raw_call_state);
251 if (tevent_req_is_nterror(req, &status)) {
252 tevent_req_received(req);
256 *out_data = talloc_move(mem_ctx, &state->out_data.data);
257 *out_length = state->out_data.length;
259 tevent_req_received(req);
263 struct rpcint_bh_disconnect_state {
267 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
268 struct tevent_context *ev,
269 struct dcerpc_binding_handle *h)
271 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
272 struct rpcint_bh_state);
273 struct tevent_req *req;
274 struct rpcint_bh_disconnect_state *state;
277 req = tevent_req_create(mem_ctx, &state,
278 struct rpcint_bh_disconnect_state);
283 ok = rpcint_bh_is_connected(h);
285 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
286 return tevent_req_post(req, ev);
290 * TODO: do a real async disconnect ...
292 * For now the caller needs to free pipes_struct
296 tevent_req_done(req);
297 return tevent_req_post(req, ev);
300 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
304 if (tevent_req_is_nterror(req, &status)) {
305 tevent_req_received(req);
309 tevent_req_received(req);
313 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
318 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
320 const void *_struct_ptr,
321 const struct ndr_interface_call *call)
323 void *struct_ptr = discard_const(_struct_ptr);
325 if (DEBUGLEVEL < 11) {
329 if (ndr_flags & NDR_IN) {
330 ndr_print_function_debug(call->ndr_print,
335 if (ndr_flags & NDR_OUT) {
336 ndr_print_function_debug(call->ndr_print,
343 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
345 .is_connected = rpcint_bh_is_connected,
346 .set_timeout = rpcint_bh_set_timeout,
347 .raw_call_send = rpcint_bh_raw_call_send,
348 .raw_call_recv = rpcint_bh_raw_call_recv,
349 .disconnect_send = rpcint_bh_disconnect_send,
350 .disconnect_recv = rpcint_bh_disconnect_recv,
352 .ref_alloc = rpcint_bh_ref_alloc,
353 .do_ndr_print = rpcint_bh_do_ndr_print,
356 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
357 const struct ndr_syntax_id *abstract_syntax,
358 const struct ndr_interface_table *ndr_table,
359 const struct tsocket_address *remote_address,
360 const struct auth_session_info *session_info,
361 struct messaging_context *msg_ctx,
362 struct dcerpc_binding_handle **binding_handle)
364 struct dcerpc_binding_handle *h;
365 struct rpcint_bh_state *hs;
368 abstract_syntax = &ndr_table->syntax_id;
371 h = dcerpc_binding_handle_create(mem_ctx,
376 struct rpcint_bh_state,
379 return NT_STATUS_NO_MEMORY;
381 hs->p = make_internal_rpc_pipe_p(hs,
388 return NT_STATUS_NO_MEMORY;
395 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
397 * @param[in] mem_ctx The memory context to use.
399 * @param[in] ndr_table Normally the ndr_table_<name>.
401 * @param[in] remote_address The info about the connected client.
403 * @param[in] serversupplied_info The server supplied authentication function.
405 * @param[in] msg_ctx The messaging context that can be used by the server
407 * @param[out] binding_handle A pointer to store the connected
408 * dcerpc_binding_handle
410 * @return NT_STATUS_OK on success, a corresponding NT status if an
414 * struct dcerpc_binding_handle *winreg_binding;
417 * status = rpcint_binding_handle(tmp_ctx,
425 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
426 const struct ndr_interface_table *ndr_table,
427 const struct tsocket_address *remote_address,
428 const struct auth_session_info *session_info,
429 struct messaging_context *msg_ctx,
430 struct dcerpc_binding_handle **binding_handle)
432 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
433 session_info, msg_ctx, binding_handle);
439 * @brief Create a new RPC client context which uses a local transport.
441 * This creates a local transport. It is a shortcut to directly call the server
442 * functions and avoid marshalling.
443 * NOTE: this function should be used only by rpc_pipe_open_interface()
445 * @param[in] mem_ctx The memory context to use.
447 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
450 * @param[in] serversupplied_info The server supplied authentication function.
452 * @param[in] remote_address The client address information.
454 * @param[in] msg_ctx The messaging context to use.
456 * @param[out] presult A pointer to store the connected rpc client pipe.
458 * @return NT_STATUS_OK on success, a corresponding NT status if an
461 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
462 const struct ndr_syntax_id *abstract_syntax,
463 const struct auth_session_info *session_info,
464 const struct tsocket_address *remote_address,
465 struct messaging_context *msg_ctx,
466 struct rpc_pipe_client **presult)
468 struct rpc_pipe_client *result;
471 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
472 if (result == NULL) {
473 return NT_STATUS_NO_MEMORY;
476 result->abstract_syntax = *abstract_syntax;
477 result->transfer_syntax = ndr_transfer_syntax_ndr;
479 if (remote_address == NULL) {
480 struct tsocket_address *local;
483 rc = tsocket_address_inet_from_strings(mem_ctx,
490 return NT_STATUS_NO_MEMORY;
493 remote_address = local;
496 result->max_xmit_frag = -1;
497 result->max_recv_frag = -1;
499 status = rpcint_binding_handle_ex(result,
505 &result->binding_handle);
506 if (!NT_STATUS_IS_OK(status)) {
515 /****************************************************************************
516 * External pipes functions
517 ***************************************************************************/
520 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
521 const char *pipe_name,
522 const struct tsocket_address *local_address,
523 const struct tsocket_address *remote_address,
524 const struct auth_session_info *session_info)
526 struct np_proxy_state *result;
528 const char *socket_dir;
529 struct tevent_context *ev;
530 struct tevent_req *subreq;
531 struct auth_session_info_transport *session_info_t;
536 result = talloc(mem_ctx, struct np_proxy_state);
537 if (result == NULL) {
538 DEBUG(0, ("talloc failed\n"));
542 result->read_queue = tevent_queue_create(result, "np_read");
543 if (result->read_queue == NULL) {
544 DEBUG(0, ("tevent_queue_create failed\n"));
548 result->write_queue = tevent_queue_create(result, "np_write");
549 if (result->write_queue == NULL) {
550 DEBUG(0, ("tevent_queue_create failed\n"));
554 ev = s3_tevent_context_init(talloc_tos());
556 DEBUG(0, ("s3_tevent_context_init failed\n"));
560 socket_dir = lp_parm_const_string(
561 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
563 if (socket_dir == NULL) {
564 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
567 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
568 if (socket_np_dir == NULL) {
569 DEBUG(0, ("talloc_asprintf failed\n"));
573 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
574 if (session_info_t == NULL) {
575 DEBUG(0, ("talloc failed\n"));
579 session_info_t->session_info = copy_session_info(session_info_t,
581 if (session_info_t->session_info == NULL) {
582 DEBUG(0, ("copy_session_info failed\n"));
587 subreq = tstream_npa_connect_send(talloc_tos(), ev,
590 remote_address, /* client_addr */
591 NULL, /* client_name */
592 local_address, /* server_addr */
593 NULL, /* server_name */
595 if (subreq == NULL) {
597 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
598 "user %s\\%s failed\n",
599 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
600 session_info_t->session_info->info->account_name));
603 ok = tevent_req_poll(subreq, ev);
606 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
607 "failed for tstream_npa_connect: %s\n",
608 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
609 session_info_t->session_info->info->account_name,
614 ret = tstream_npa_connect_recv(subreq, &sys_errno,
618 &result->device_state,
619 &result->allocation_size);
623 if (errno == ENOENT) {
626 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
627 "user %s\\%s failed: %s\n",
628 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
629 session_info_t->session_info->info->account_name,
630 strerror(sys_errno)));
641 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
642 const char *pipe_name,
643 const struct ndr_interface_table *table,
644 const struct auth_session_info *session_info,
645 struct rpc_pipe_client **_result)
647 struct tsocket_address *local, *remote;
648 struct rpc_pipe_client *result = NULL;
649 struct np_proxy_state *proxy_state = NULL;
650 struct pipe_auth_data *auth;
654 /* this is an internal connection, fake up ip addresses */
655 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
658 return NT_STATUS_NO_MEMORY;
660 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
663 return NT_STATUS_NO_MEMORY;
666 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
667 local, remote, session_info);
669 return NT_STATUS_UNSUCCESSFUL;
672 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
673 if (result == NULL) {
674 status = NT_STATUS_NO_MEMORY;
678 result->abstract_syntax = table->syntax_id;
679 result->transfer_syntax = ndr_transfer_syntax_ndr;
681 result->desthost = get_myname(result);
682 result->srv_name_slash = talloc_asprintf_strupper_m(
683 result, "\\\\%s", result->desthost);
684 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
685 status = NT_STATUS_NO_MEMORY;
689 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
690 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
692 status = rpc_transport_tstream_init(result,
695 if (!NT_STATUS_IS_OK(status)) {
699 result->binding_handle = rpccli_bh_create(result, NULL, table);
700 if (result->binding_handle == NULL) {
701 status = NT_STATUS_NO_MEMORY;
702 DEBUG(0, ("Failed to create binding handle.\n"));
706 result->auth = talloc_zero(result, struct pipe_auth_data);
708 status = NT_STATUS_NO_MEMORY;
711 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
712 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
714 status = rpccli_anon_bind_data(result, &auth);
715 if (!NT_STATUS_IS_OK(status)) {
716 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
720 status = rpc_pipe_bind(result, auth);
721 if (!NT_STATUS_IS_OK(status)) {
722 DEBUG(0, ("Failed to bind external pipe.\n"));
727 if (!NT_STATUS_IS_OK(status)) {
730 TALLOC_FREE(proxy_state);
736 * @brief Create a new RPC client context which uses a local dispatch function
737 * or a remote transport, depending on rpc_server configuration for the
740 * @param[in] mem_ctx The memory context to use.
742 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
745 * @param[in] serversupplied_info The server supplied authentication function.
747 * @param[in] remote_address The client address information.
749 * @param[in] msg_ctx The messaging context to use.
751 * @param[out] presult A pointer to store the connected rpc client pipe.
753 * @return NT_STATUS_OK on success, a corresponding NT status if an
757 * struct rpc_pipe_client *winreg_pipe;
760 * status = rpc_pipe_open_interface(tmp_ctx,
761 * &ndr_table_winreg.syntax_id,
768 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
769 const struct ndr_interface_table *table,
770 const struct auth_session_info *session_info,
771 const struct tsocket_address *remote_address,
772 struct messaging_context *msg_ctx,
773 struct rpc_pipe_client **cli_pipe)
775 struct rpc_pipe_client *cli = NULL;
776 enum rpc_service_mode_e pipe_mode;
777 const char *pipe_name;
781 if (cli_pipe != NULL) {
782 if (rpccli_is_connected(*cli_pipe)) {
785 TALLOC_FREE(*cli_pipe);
789 tmp_ctx = talloc_stackframe();
790 if (tmp_ctx == NULL) {
791 return NT_STATUS_NO_MEMORY;
794 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
795 if (pipe_name == NULL) {
796 status = NT_STATUS_INVALID_PARAMETER;
800 while (pipe_name[0] == '\\') {
804 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
806 pipe_mode = rpc_service_mode(pipe_name);
809 case RPC_SERVICE_MODE_EMBEDDED:
810 status = rpc_pipe_open_internal(tmp_ctx,
811 &table->syntax_id, session_info,
812 remote_address, msg_ctx,
814 if (!NT_STATUS_IS_OK(status)) {
818 case RPC_SERVICE_MODE_EXTERNAL:
819 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
820 * for now we need to use the special proxy setup to connect
823 status = rpc_pipe_open_external(tmp_ctx,
827 if (!NT_STATUS_IS_OK(status)) {
831 case RPC_SERVICE_MODE_DISABLED:
832 status = NT_STATUS_NOT_IMPLEMENTED;
833 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
834 pipe_name, nt_errstr(status)));
838 status = NT_STATUS_OK;
840 if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
841 *cli_pipe = talloc_move(mem_ctx, &cli);
843 TALLOC_FREE(tmp_ctx);