Fix some typos.
[Samba.git] / source3 / rpc_server / rpc_ncacn_np.c
blob867af207723749c29b2d79414c70d666a3d9375c
1 /*
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/>.
24 #include "includes.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "rpc_server/srv_pipe_internal.h"
27 #include "rpc_dce.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"
33 #include "auth.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"
40 #include "rpc_server/rpc_server.h"
42 #undef DBGC_CLASS
43 #define DBGC_CLASS DBGC_RPC_SRV
45 static struct npa_state *npa_state_init(TALLOC_CTX *mem_ctx)
47 struct npa_state *npa;
49 npa = talloc_zero(mem_ctx, struct npa_state);
50 if (npa == NULL) {
51 return NULL;
54 npa->read_queue = tevent_queue_create(npa, "npa_cli_read");
55 if (npa->read_queue == NULL) {
56 DEBUG(0, ("tevent_queue_create failed\n"));
57 goto fail;
60 npa->write_queue = tevent_queue_create(npa, "npa_cli_write");
61 if (npa->write_queue == NULL) {
62 DEBUG(0, ("tevent_queue_create failed\n"));
63 goto fail;
66 return npa;
67 fail:
68 talloc_free(npa);
69 return NULL;
72 NTSTATUS make_internal_rpc_pipe_socketpair(TALLOC_CTX *mem_ctx,
73 struct tevent_context *ev_ctx,
74 struct messaging_context *msg_ctx,
75 const char *pipe_name,
76 const struct ndr_syntax_id *syntax,
77 const struct tsocket_address *remote_address,
78 const struct auth_session_info *session_info,
79 struct npa_state **pnpa)
81 TALLOC_CTX *tmp_ctx = talloc_stackframe();
82 struct named_pipe_client *npc;
83 struct tevent_req *subreq;
84 struct npa_state *npa;
85 NTSTATUS status;
86 int error;
87 int rc;
89 DEBUG(4, ("Create of internal pipe %s requested\n", pipe_name));
91 npa = npa_state_init(tmp_ctx);
92 if (npa == NULL) {
93 status = NT_STATUS_NO_MEMORY;
94 goto out;
97 npa->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
98 npa->device_state = 0xff | 0x0400 | 0x0100;
99 npa->allocation_size = 4096;
101 npc = named_pipe_client_init(npa,
102 ev_ctx,
103 msg_ctx,
104 pipe_name,
105 NULL, /* term_fn */
106 npa->file_type,
107 npa->device_state,
108 npa->allocation_size,
109 NULL); /* private_data */
110 if (npc == NULL) {
111 status = NT_STATUS_NO_MEMORY;
112 goto out;
114 npa->private_data = (void*) npc;
116 rc = tstream_npa_socketpair(npa->file_type,
117 npa,
118 &npa->stream,
119 npc,
120 &npc->tstream);
121 if (rc == -1) {
122 status = map_nt_error_from_unix(errno);
123 goto out;
126 npc->client = tsocket_address_copy(remote_address, npc);
127 if (npc->client == NULL) {
128 status = NT_STATUS_NO_MEMORY;
129 goto out;
132 npc->client_name = tsocket_address_inet_addr_string(npc->client, npc);
133 if (npc->client_name == NULL) {
134 status = NT_STATUS_NO_MEMORY;
135 goto out;
138 npc->session_info = copy_session_info(npc, session_info);
139 if (npc->session_info == NULL) {
140 status = NT_STATUS_NO_MEMORY;
141 goto out;
144 rc = make_server_pipes_struct(npc,
145 npc->msg_ctx,
146 npc->pipe_name,
147 NCACN_NP,
148 npc->server,
149 npc->client,
150 npc->session_info,
151 &npc->p,
152 &error);
153 if (rc == -1) {
154 status = map_nt_error_from_unix(error);
155 goto out;
158 npc->write_queue = tevent_queue_create(npc, "npa_server_write_queue");
159 if (npc->write_queue == NULL) {
160 status = NT_STATUS_NO_MEMORY;
161 goto out;
164 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
165 if (subreq == NULL) {
166 DEBUG(2, ("Failed to start receving packets\n"));
167 status = NT_STATUS_PIPE_BROKEN;
168 goto out;
170 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
172 *pnpa = talloc_steal(mem_ctx, npa);
173 status = NT_STATUS_OK;
174 out:
175 talloc_free(tmp_ctx);
176 return status;
179 /****************************************************************************
180 Make an internal namedpipes structure
181 ****************************************************************************/
183 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
184 const struct ndr_syntax_id *syntax,
185 const struct tsocket_address *remote_address,
186 const struct auth_session_info *session_info,
187 struct messaging_context *msg_ctx)
189 struct pipes_struct *p;
190 struct pipe_rpc_fns *context_fns;
191 const char *pipe_name;
192 int ret;
193 const struct ndr_interface_table *table;
195 table = ndr_table_by_uuid(&syntax->uuid);
196 if (table == NULL) {
197 DEBUG(0,("unknown interface\n"));
198 return NULL;
201 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
203 DEBUG(4,("Create pipe requested %s\n", pipe_name));
205 ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
206 NCALRPC, RPC_LITTLE_ENDIAN,
207 remote_address, NULL, &p);
208 if (ret) {
209 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
210 return NULL;
213 if (!init_pipe_handles(p, syntax)) {
214 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
215 TALLOC_FREE(p);
216 return NULL;
219 p->session_info = copy_session_info(p, session_info);
220 if (p->session_info == NULL) {
221 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
222 close_policy_by_pipe(p);
223 TALLOC_FREE(p);
224 return NULL;
227 context_fns = talloc(p, struct pipe_rpc_fns);
228 if (context_fns == NULL) {
229 DEBUG(0,("talloc() failed!\n"));
230 TALLOC_FREE(p);
231 return NULL;
234 context_fns->next = context_fns->prev = NULL;
235 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
236 context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
237 context_fns->context_id = 0;
238 context_fns->syntax = *syntax;
240 /* add to the list of open contexts */
241 DLIST_ADD(p->contexts, context_fns);
243 DEBUG(4,("Created internal pipe %s\n", pipe_name));
245 return p;
248 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
249 TALLOC_CTX *mem_ctx,
250 uint32_t opnum,
251 const DATA_BLOB *in_data,
252 DATA_BLOB *out_data)
254 struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
255 uint32_t num_cmds = fns->n_cmds;
256 const struct api_struct *cmds = fns->cmds;
257 uint32_t i;
258 bool ok;
260 /* set opnum */
261 p->opnum = opnum;
263 for (i = 0; i < num_cmds; i++) {
264 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
265 break;
269 if (i == num_cmds) {
270 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
273 p->in_data.data = *in_data;
274 p->out_data.rdata = data_blob_null;
276 ok = cmds[i].fn(p);
277 p->in_data.data = data_blob_null;
278 if (!ok) {
279 data_blob_free(&p->out_data.rdata);
280 talloc_free_children(p->mem_ctx);
281 return NT_STATUS_RPC_CALL_FAILED;
284 if (p->fault_state) {
285 NTSTATUS status;
287 status = NT_STATUS(p->fault_state);
288 p->fault_state = 0;
289 data_blob_free(&p->out_data.rdata);
290 talloc_free_children(p->mem_ctx);
291 return status;
294 *out_data = p->out_data.rdata;
295 talloc_steal(mem_ctx, out_data->data);
296 p->out_data.rdata = data_blob_null;
298 talloc_free_children(p->mem_ctx);
299 return NT_STATUS_OK;
302 struct rpcint_bh_state {
303 struct pipes_struct *p;
306 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
308 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
309 struct rpcint_bh_state);
311 if (!hs->p) {
312 return false;
315 return true;
318 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
319 uint32_t timeout)
321 /* TODO: implement timeouts */
322 return UINT32_MAX;
325 struct rpcint_bh_raw_call_state {
326 DATA_BLOB in_data;
327 DATA_BLOB out_data;
328 uint32_t out_flags;
331 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
332 struct tevent_context *ev,
333 struct dcerpc_binding_handle *h,
334 const struct GUID *object,
335 uint32_t opnum,
336 uint32_t in_flags,
337 const uint8_t *in_data,
338 size_t in_length)
340 struct rpcint_bh_state *hs =
341 dcerpc_binding_handle_data(h,
342 struct rpcint_bh_state);
343 struct tevent_req *req;
344 struct rpcint_bh_raw_call_state *state;
345 bool ok;
346 NTSTATUS status;
348 req = tevent_req_create(mem_ctx, &state,
349 struct rpcint_bh_raw_call_state);
350 if (req == NULL) {
351 return NULL;
353 state->in_data.data = discard_const_p(uint8_t, in_data);
354 state->in_data.length = in_length;
356 ok = rpcint_bh_is_connected(h);
357 if (!ok) {
358 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
359 return tevent_req_post(req, ev);
362 /* TODO: allow async */
363 status = rpcint_dispatch(hs->p, state, opnum,
364 &state->in_data,
365 &state->out_data);
366 if (!NT_STATUS_IS_OK(status)) {
367 tevent_req_nterror(req, status);
368 return tevent_req_post(req, ev);
371 tevent_req_done(req);
372 return tevent_req_post(req, ev);
375 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
376 TALLOC_CTX *mem_ctx,
377 uint8_t **out_data,
378 size_t *out_length,
379 uint32_t *out_flags)
381 struct rpcint_bh_raw_call_state *state =
382 tevent_req_data(req,
383 struct rpcint_bh_raw_call_state);
384 NTSTATUS status;
386 if (tevent_req_is_nterror(req, &status)) {
387 tevent_req_received(req);
388 return status;
391 *out_data = talloc_move(mem_ctx, &state->out_data.data);
392 *out_length = state->out_data.length;
393 *out_flags = 0;
394 tevent_req_received(req);
395 return NT_STATUS_OK;
398 struct rpcint_bh_disconnect_state {
399 uint8_t _dummy;
402 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
403 struct tevent_context *ev,
404 struct dcerpc_binding_handle *h)
406 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
407 struct rpcint_bh_state);
408 struct tevent_req *req;
409 struct rpcint_bh_disconnect_state *state;
410 bool ok;
412 req = tevent_req_create(mem_ctx, &state,
413 struct rpcint_bh_disconnect_state);
414 if (req == NULL) {
415 return NULL;
418 ok = rpcint_bh_is_connected(h);
419 if (!ok) {
420 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
421 return tevent_req_post(req, ev);
425 * TODO: do a real async disconnect ...
427 * For now the caller needs to free pipes_struct
429 hs->p = NULL;
431 tevent_req_done(req);
432 return tevent_req_post(req, ev);
435 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
437 NTSTATUS status;
439 if (tevent_req_is_nterror(req, &status)) {
440 tevent_req_received(req);
441 return status;
444 tevent_req_received(req);
445 return NT_STATUS_OK;
448 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
450 return true;
453 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
454 int ndr_flags,
455 const void *_struct_ptr,
456 const struct ndr_interface_call *call)
458 void *struct_ptr = discard_const(_struct_ptr);
460 if (DEBUGLEVEL < 11) {
461 return;
464 if (ndr_flags & NDR_IN) {
465 ndr_print_function_debug(call->ndr_print,
466 call->name,
467 ndr_flags,
468 struct_ptr);
470 if (ndr_flags & NDR_OUT) {
471 ndr_print_function_debug(call->ndr_print,
472 call->name,
473 ndr_flags,
474 struct_ptr);
478 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
479 .name = "rpcint",
480 .is_connected = rpcint_bh_is_connected,
481 .set_timeout = rpcint_bh_set_timeout,
482 .raw_call_send = rpcint_bh_raw_call_send,
483 .raw_call_recv = rpcint_bh_raw_call_recv,
484 .disconnect_send = rpcint_bh_disconnect_send,
485 .disconnect_recv = rpcint_bh_disconnect_recv,
487 .ref_alloc = rpcint_bh_ref_alloc,
488 .do_ndr_print = rpcint_bh_do_ndr_print,
491 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
492 const struct ndr_syntax_id *abstract_syntax,
493 const struct ndr_interface_table *ndr_table,
494 const struct tsocket_address *remote_address,
495 const struct auth_session_info *session_info,
496 struct messaging_context *msg_ctx,
497 struct dcerpc_binding_handle **binding_handle)
499 struct dcerpc_binding_handle *h;
500 struct rpcint_bh_state *hs;
502 if (ndr_table) {
503 abstract_syntax = &ndr_table->syntax_id;
506 h = dcerpc_binding_handle_create(mem_ctx,
507 &rpcint_bh_ops,
508 NULL,
509 ndr_table,
510 &hs,
511 struct rpcint_bh_state,
512 __location__);
513 if (h == NULL) {
514 return NT_STATUS_NO_MEMORY;
516 hs->p = make_internal_rpc_pipe_p(hs,
517 abstract_syntax,
518 remote_address,
519 session_info,
520 msg_ctx);
521 if (hs->p == NULL) {
522 TALLOC_FREE(h);
523 return NT_STATUS_NO_MEMORY;
526 *binding_handle = h;
527 return NT_STATUS_OK;
530 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
532 * @param[in] mem_ctx The memory context to use.
534 * @param[in] ndr_table Normally the ndr_table_<name>.
536 * @param[in] remote_address The info about the connected client.
538 * @param[in] serversupplied_info The server supplied authentication function.
540 * @param[in] msg_ctx The messaging context that can be used by the server
542 * @param[out] binding_handle A pointer to store the connected
543 * dcerpc_binding_handle
545 * @return NT_STATUS_OK on success, a corresponding NT status if an
546 * error occured.
548 * @code
549 * struct dcerpc_binding_handle *winreg_binding;
550 * NTSTATUS status;
552 * status = rpcint_binding_handle(tmp_ctx,
553 * &ndr_table_winreg,
554 * p->remote_address,
555 * p->session_info,
556 * p->msg_ctx
557 * &winreg_binding);
558 * @endcode
560 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
561 const struct ndr_interface_table *ndr_table,
562 const struct tsocket_address *remote_address,
563 const struct auth_session_info *session_info,
564 struct messaging_context *msg_ctx,
565 struct dcerpc_binding_handle **binding_handle)
567 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
568 session_info, msg_ctx, binding_handle);
572 * @internal
574 * @brief Create a new RPC client context which uses a local transport.
576 * This creates a local transport. It is a shortcut to directly call the server
577 * functions and avoid marshalling.
578 * NOTE: this function should be used only by rpc_pipe_open_interface()
580 * @param[in] mem_ctx The memory context to use.
582 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
583 * ndr_table_<name>.
585 * @param[in] serversupplied_info The server supplied authentication function.
587 * @param[in] remote_address The client address information.
589 * @param[in] msg_ctx The messaging context to use.
591 * @param[out] presult A pointer to store the connected rpc client pipe.
593 * @return NT_STATUS_OK on success, a corresponding NT status if an
594 * error occured.
596 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
597 const struct ndr_syntax_id *abstract_syntax,
598 const struct auth_session_info *session_info,
599 const struct tsocket_address *remote_address,
600 struct messaging_context *msg_ctx,
601 struct rpc_pipe_client **presult)
603 struct rpc_pipe_client *result;
604 NTSTATUS status;
606 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
607 if (result == NULL) {
608 return NT_STATUS_NO_MEMORY;
611 result->abstract_syntax = *abstract_syntax;
612 result->transfer_syntax = ndr_transfer_syntax_ndr;
614 if (remote_address == NULL) {
615 struct tsocket_address *local;
616 int rc;
618 rc = tsocket_address_inet_from_strings(mem_ctx,
619 "ip",
620 "127.0.0.1",
622 &local);
623 if (rc < 0) {
624 TALLOC_FREE(result);
625 return NT_STATUS_NO_MEMORY;
628 remote_address = local;
631 result->max_xmit_frag = -1;
632 result->max_recv_frag = -1;
634 status = rpcint_binding_handle_ex(result,
635 abstract_syntax,
636 NULL,
637 remote_address,
638 session_info,
639 msg_ctx,
640 &result->binding_handle);
641 if (!NT_STATUS_IS_OK(status)) {
642 TALLOC_FREE(result);
643 return status;
646 *presult = result;
647 return NT_STATUS_OK;
650 /****************************************************************************
651 * External pipes functions
652 ***************************************************************************/
654 NTSTATUS make_external_rpc_pipe(TALLOC_CTX *mem_ctx,
655 const char *pipe_name,
656 const struct tsocket_address *local_address,
657 const struct tsocket_address *remote_address,
658 const struct auth_session_info *session_info,
659 struct npa_state **pnpa)
661 TALLOC_CTX *tmp_ctx = talloc_stackframe();
662 struct auth_session_info_transport *session_info_t;
663 struct tevent_context *ev_ctx;
664 struct tevent_req *subreq;
665 const char *socket_np_dir;
666 const char *socket_dir;
667 struct npa_state *npa;
668 int sys_errno;
669 NTSTATUS status;
670 int rc = -1;
671 bool ok;
673 npa = npa_state_init(tmp_ctx);
674 if (npa == NULL) {
675 status = NT_STATUS_NO_MEMORY;
676 goto out;
679 socket_dir = lp_parm_const_string(GLOBAL_SECTION_SNUM,
680 "external_rpc_pipe",
681 "socket_dir",
682 lp_ncalrpc_dir());
683 if (socket_dir == NULL) {
684 DEBUG(0, ("external_rpc_pipe: socket_dir not set\n"));
685 status = NT_STATUS_PIPE_NOT_AVAILABLE;
686 goto out;
689 socket_np_dir = talloc_asprintf(tmp_ctx, "%s/np", socket_dir);
690 if (socket_np_dir == NULL) {
691 DEBUG(0, ("talloc_asprintf failed\n"));
692 status = NT_STATUS_NO_MEMORY;
693 goto out;
696 session_info_t = talloc_zero(tmp_ctx,
697 struct auth_session_info_transport);
698 if (session_info_t == NULL) {
699 DEBUG(0, ("talloc failed\n"));
700 status = NT_STATUS_NO_MEMORY;
701 goto out;
704 session_info_t->session_info = copy_session_info(session_info_t,
705 session_info);
706 if (session_info_t->session_info == NULL) {
707 DEBUG(0, ("copy_session_info failed\n"));
708 status = NT_STATUS_NO_MEMORY;
709 goto out;
712 ev_ctx = s3_tevent_context_init(tmp_ctx);
713 if (ev_ctx == NULL) {
714 DEBUG(0, ("s3_tevent_context_init failed\n"));
715 status = NT_STATUS_NO_MEMORY;
716 goto out;
719 become_root();
720 subreq = tstream_npa_connect_send(tmp_ctx,
721 ev_ctx,
722 socket_np_dir,
723 pipe_name,
724 remote_address, /* client_addr */
725 NULL, /* client_name */
726 local_address, /* server_addr */
727 NULL, /* server_name */
728 session_info_t);
729 if (subreq == NULL) {
730 unbecome_root();
731 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
732 "user %s\\%s failed\n",
733 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
734 session_info_t->session_info->info->account_name));
735 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
736 goto out;
738 ok = tevent_req_poll(subreq, ev_ctx);
739 unbecome_root();
740 if (!ok) {
741 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
742 "failed for tstream_npa_connect: %s\n",
743 socket_np_dir,
744 pipe_name,
745 session_info_t->session_info->info->domain_name,
746 session_info_t->session_info->info->account_name,
747 strerror(errno)));
748 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
749 goto out;
752 rc = tstream_npa_connect_recv(subreq,
753 &sys_errno,
754 npa,
755 &npa->stream,
756 &npa->file_type,
757 &npa->device_state,
758 &npa->allocation_size);
759 talloc_free(subreq);
760 if (rc != 0) {
761 int l = 1;
763 if (errno == ENOENT) {
764 l = 2;
767 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
768 "user %s\\%s failed: %s\n",
769 socket_np_dir,
770 pipe_name,
771 session_info_t->session_info->info->domain_name,
772 session_info_t->session_info->info->account_name,
773 strerror(sys_errno)));
774 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
775 goto out;
778 *pnpa = talloc_steal(mem_ctx, npa);
779 status = NT_STATUS_OK;
780 out:
781 talloc_free(tmp_ctx);
783 return status;
786 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
787 const char *pipe_name,
788 const struct tsocket_address *local_address,
789 const struct tsocket_address *remote_address,
790 const struct auth_session_info *session_info)
792 struct np_proxy_state *result;
793 char *socket_np_dir;
794 const char *socket_dir;
795 struct tevent_context *ev;
796 struct tevent_req *subreq;
797 struct auth_session_info_transport *session_info_t;
798 bool ok;
799 int ret;
800 int sys_errno;
802 result = talloc(mem_ctx, struct np_proxy_state);
803 if (result == NULL) {
804 DEBUG(0, ("talloc failed\n"));
805 return NULL;
808 result->read_queue = tevent_queue_create(result, "np_read");
809 if (result->read_queue == NULL) {
810 DEBUG(0, ("tevent_queue_create failed\n"));
811 goto fail;
814 result->write_queue = tevent_queue_create(result, "np_write");
815 if (result->write_queue == NULL) {
816 DEBUG(0, ("tevent_queue_create failed\n"));
817 goto fail;
820 ev = s3_tevent_context_init(talloc_tos());
821 if (ev == NULL) {
822 DEBUG(0, ("s3_tevent_context_init failed\n"));
823 goto fail;
826 socket_dir = lp_parm_const_string(
827 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
828 lp_ncalrpc_dir());
829 if (socket_dir == NULL) {
830 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
831 goto fail;
833 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
834 if (socket_np_dir == NULL) {
835 DEBUG(0, ("talloc_asprintf failed\n"));
836 goto fail;
839 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
840 if (session_info_t == NULL) {
841 DEBUG(0, ("talloc failed\n"));
842 goto fail;
845 session_info_t->session_info = copy_session_info(session_info_t,
846 session_info);
847 if (session_info_t->session_info == NULL) {
848 DEBUG(0, ("copy_session_info failed\n"));
849 goto fail;
852 become_root();
853 subreq = tstream_npa_connect_send(talloc_tos(), ev,
854 socket_np_dir,
855 pipe_name,
856 remote_address, /* client_addr */
857 NULL, /* client_name */
858 local_address, /* server_addr */
859 NULL, /* server_name */
860 session_info_t);
861 if (subreq == NULL) {
862 unbecome_root();
863 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
864 "user %s\\%s failed\n",
865 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
866 session_info_t->session_info->info->account_name));
867 goto fail;
869 ok = tevent_req_poll(subreq, ev);
870 unbecome_root();
871 if (!ok) {
872 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
873 "failed for tstream_npa_connect: %s\n",
874 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
875 session_info_t->session_info->info->account_name,
876 strerror(errno)));
877 goto fail;
880 ret = tstream_npa_connect_recv(subreq, &sys_errno,
881 result,
882 &result->npipe,
883 &result->file_type,
884 &result->device_state,
885 &result->allocation_size);
886 TALLOC_FREE(subreq);
887 if (ret != 0) {
888 int l = 1;
889 if (errno == ENOENT) {
890 l = 2;
892 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
893 "user %s\\%s failed: %s\n",
894 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
895 session_info_t->session_info->info->account_name,
896 strerror(sys_errno)));
897 goto fail;
900 return result;
902 fail:
903 TALLOC_FREE(result);
904 return NULL;
907 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
908 const char *pipe_name,
909 const struct ndr_interface_table *table,
910 const struct auth_session_info *session_info,
911 struct rpc_pipe_client **_result)
913 struct tsocket_address *local, *remote;
914 struct rpc_pipe_client *result = NULL;
915 struct np_proxy_state *proxy_state = NULL;
916 struct pipe_auth_data *auth;
917 NTSTATUS status;
918 int ret;
920 /* this is an internal connection, fake up ip addresses */
921 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
922 NULL, 0, &local);
923 if (ret) {
924 return NT_STATUS_NO_MEMORY;
926 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
927 NULL, 0, &remote);
928 if (ret) {
929 return NT_STATUS_NO_MEMORY;
932 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
933 local, remote, session_info);
934 if (!proxy_state) {
935 return NT_STATUS_UNSUCCESSFUL;
938 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
939 if (result == NULL) {
940 status = NT_STATUS_NO_MEMORY;
941 goto done;
944 result->abstract_syntax = table->syntax_id;
945 result->transfer_syntax = ndr_transfer_syntax_ndr;
947 result->desthost = get_myname(result);
948 result->srv_name_slash = talloc_asprintf_strupper_m(
949 result, "\\\\%s", result->desthost);
950 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
951 status = NT_STATUS_NO_MEMORY;
952 goto done;
955 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
956 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
958 status = rpc_transport_tstream_init(result,
959 &proxy_state->npipe,
960 &result->transport);
961 if (!NT_STATUS_IS_OK(status)) {
962 goto done;
965 result->binding_handle = rpccli_bh_create(result, NULL, table);
966 if (result->binding_handle == NULL) {
967 status = NT_STATUS_NO_MEMORY;
968 DEBUG(0, ("Failed to create binding handle.\n"));
969 goto done;
972 result->auth = talloc_zero(result, struct pipe_auth_data);
973 if (!result->auth) {
974 status = NT_STATUS_NO_MEMORY;
975 goto done;
977 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
978 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
980 status = rpccli_anon_bind_data(result, &auth);
981 if (!NT_STATUS_IS_OK(status)) {
982 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
983 goto done;
986 status = rpc_pipe_bind(result, auth);
987 if (!NT_STATUS_IS_OK(status)) {
988 DEBUG(0, ("Failed to bind external pipe.\n"));
989 goto done;
992 done:
993 if (!NT_STATUS_IS_OK(status)) {
994 TALLOC_FREE(result);
996 TALLOC_FREE(proxy_state);
997 *_result = result;
998 return status;
1002 * @brief Create a new RPC client context which uses a local dispatch function
1003 * or a remote transport, depending on rpc_server configuration for the
1004 * specific service.
1006 * @param[in] mem_ctx The memory context to use.
1008 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
1009 * ndr_table_<name>.
1011 * @param[in] serversupplied_info The server supplied authentication function.
1013 * @param[in] remote_address The client address information.
1015 * @param[in] msg_ctx The messaging context to use.
1017 * @param[out] presult A pointer to store the connected rpc client pipe.
1019 * @return NT_STATUS_OK on success, a corresponding NT status if an
1020 * error occured.
1022 * @code
1023 * struct rpc_pipe_client *winreg_pipe;
1024 * NTSTATUS status;
1026 * status = rpc_pipe_open_interface(tmp_ctx,
1027 * &ndr_table_winreg.syntax_id,
1028 * p->session_info,
1029 * remote_address,
1030 * &winreg_pipe);
1031 * @endcode
1034 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
1035 const struct ndr_interface_table *table,
1036 const struct auth_session_info *session_info,
1037 const struct tsocket_address *remote_address,
1038 struct messaging_context *msg_ctx,
1039 struct rpc_pipe_client **cli_pipe)
1041 struct rpc_pipe_client *cli = NULL;
1042 enum rpc_service_mode_e pipe_mode;
1043 const char *pipe_name;
1044 NTSTATUS status;
1045 TALLOC_CTX *tmp_ctx;
1047 if (cli_pipe != NULL) {
1048 if (rpccli_is_connected(*cli_pipe)) {
1049 return NT_STATUS_OK;
1050 } else {
1051 TALLOC_FREE(*cli_pipe);
1055 tmp_ctx = talloc_stackframe();
1056 if (tmp_ctx == NULL) {
1057 return NT_STATUS_NO_MEMORY;
1060 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
1061 if (pipe_name == NULL) {
1062 status = NT_STATUS_INVALID_PARAMETER;
1063 goto done;
1066 while (pipe_name[0] == '\\') {
1067 pipe_name++;
1070 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
1072 pipe_mode = rpc_service_mode(pipe_name);
1074 switch (pipe_mode) {
1075 case RPC_SERVICE_MODE_EMBEDDED:
1076 status = rpc_pipe_open_internal(tmp_ctx,
1077 &table->syntax_id, session_info,
1078 remote_address, msg_ctx,
1079 &cli);
1080 if (!NT_STATUS_IS_OK(status)) {
1081 goto done;
1083 break;
1084 case RPC_SERVICE_MODE_EXTERNAL:
1085 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
1086 * for now we need to use the special proxy setup to connect
1087 * to spoolssd. */
1089 status = rpc_pipe_open_external(tmp_ctx,
1090 pipe_name, table,
1091 session_info,
1092 &cli);
1093 if (!NT_STATUS_IS_OK(status)) {
1094 goto done;
1096 break;
1097 case RPC_SERVICE_MODE_DISABLED:
1098 status = NT_STATUS_NOT_IMPLEMENTED;
1099 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
1100 pipe_name, nt_errstr(status)));
1101 goto done;
1104 status = NT_STATUS_OK;
1105 done:
1106 if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
1107 *cli_pipe = talloc_move(mem_ctx, &cli);
1109 TALLOC_FREE(tmp_ctx);
1110 return status;