param: Rename variable used for lp_addprinter_cmd szAddPrinterCommand
[Samba.git] / source3 / rpc_server / rpc_ncacn_np.c
blob5317fd3db3371d066b1aa9a9f25de900e145588a
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 false,
149 npc->server,
150 npc->client,
151 npc->session_info,
152 &npc->p,
153 &error);
154 if (rc == -1) {
155 status = map_nt_error_from_unix(error);
156 goto out;
159 npc->write_queue = tevent_queue_create(npc, "npa_server_write_queue");
160 if (npc->write_queue == NULL) {
161 status = NT_STATUS_NO_MEMORY;
162 goto out;
165 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
166 if (subreq == NULL) {
167 DEBUG(2, ("Failed to start receving packets\n"));
168 status = NT_STATUS_PIPE_BROKEN;
169 goto out;
171 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
173 *pnpa = talloc_steal(mem_ctx, npa);
174 status = NT_STATUS_OK;
175 out:
176 talloc_free(tmp_ctx);
177 return status;
180 /****************************************************************************
181 Make an internal namedpipes structure
182 ****************************************************************************/
184 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
185 const struct ndr_syntax_id *syntax,
186 const struct tsocket_address *remote_address,
187 const struct auth_session_info *session_info,
188 struct messaging_context *msg_ctx)
190 struct pipes_struct *p;
191 struct pipe_rpc_fns *context_fns;
192 const char *pipe_name;
193 int ret;
194 const struct ndr_interface_table *table;
196 table = ndr_table_by_uuid(&syntax->uuid);
197 if (table == NULL) {
198 DEBUG(0,("unknown interface\n"));
199 return NULL;
202 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
204 DEBUG(4,("Create pipe requested %s\n", pipe_name));
206 ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
207 NCALRPC, RPC_LITTLE_ENDIAN, false,
208 remote_address, NULL, &p);
209 if (ret) {
210 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
211 return NULL;
214 if (!init_pipe_handles(p, syntax)) {
215 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
216 TALLOC_FREE(p);
217 return NULL;
220 p->session_info = copy_session_info(p, session_info);
221 if (p->session_info == NULL) {
222 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
223 close_policy_by_pipe(p);
224 TALLOC_FREE(p);
225 return NULL;
228 context_fns = talloc(p, struct pipe_rpc_fns);
229 if (context_fns == NULL) {
230 DEBUG(0,("talloc() failed!\n"));
231 TALLOC_FREE(p);
232 return NULL;
235 context_fns->next = context_fns->prev = NULL;
236 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
237 context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
238 context_fns->context_id = 0;
239 context_fns->syntax = *syntax;
241 /* add to the list of open contexts */
242 DLIST_ADD(p->contexts, context_fns);
244 DEBUG(4,("Created internal pipe %s\n", pipe_name));
246 return p;
249 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
250 TALLOC_CTX *mem_ctx,
251 uint32_t opnum,
252 const DATA_BLOB *in_data,
253 DATA_BLOB *out_data)
255 struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
256 uint32_t num_cmds = fns->n_cmds;
257 const struct api_struct *cmds = fns->cmds;
258 uint32_t i;
259 bool ok;
261 /* set opnum */
262 p->opnum = opnum;
264 for (i = 0; i < num_cmds; i++) {
265 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
266 break;
270 if (i == num_cmds) {
271 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
274 p->in_data.data = *in_data;
275 p->out_data.rdata = data_blob_null;
277 ok = cmds[i].fn(p);
278 p->in_data.data = data_blob_null;
279 if (!ok) {
280 data_blob_free(&p->out_data.rdata);
281 talloc_free_children(p->mem_ctx);
282 return NT_STATUS_RPC_CALL_FAILED;
285 if (p->fault_state) {
286 NTSTATUS status;
288 status = NT_STATUS(p->fault_state);
289 p->fault_state = 0;
290 data_blob_free(&p->out_data.rdata);
291 talloc_free_children(p->mem_ctx);
292 return status;
295 *out_data = p->out_data.rdata;
296 talloc_steal(mem_ctx, out_data->data);
297 p->out_data.rdata = data_blob_null;
299 talloc_free_children(p->mem_ctx);
300 return NT_STATUS_OK;
303 struct rpcint_bh_state {
304 struct pipes_struct *p;
307 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
309 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
310 struct rpcint_bh_state);
312 if (!hs->p) {
313 return false;
316 return true;
319 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
320 uint32_t timeout)
322 /* TODO: implement timeouts */
323 return UINT32_MAX;
326 struct rpcint_bh_raw_call_state {
327 DATA_BLOB in_data;
328 DATA_BLOB out_data;
329 uint32_t out_flags;
332 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
333 struct tevent_context *ev,
334 struct dcerpc_binding_handle *h,
335 const struct GUID *object,
336 uint32_t opnum,
337 uint32_t in_flags,
338 const uint8_t *in_data,
339 size_t in_length)
341 struct rpcint_bh_state *hs =
342 dcerpc_binding_handle_data(h,
343 struct rpcint_bh_state);
344 struct tevent_req *req;
345 struct rpcint_bh_raw_call_state *state;
346 bool ok;
347 NTSTATUS status;
349 req = tevent_req_create(mem_ctx, &state,
350 struct rpcint_bh_raw_call_state);
351 if (req == NULL) {
352 return NULL;
354 state->in_data.data = discard_const_p(uint8_t, in_data);
355 state->in_data.length = in_length;
357 ok = rpcint_bh_is_connected(h);
358 if (!ok) {
359 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
360 return tevent_req_post(req, ev);
363 /* TODO: allow async */
364 status = rpcint_dispatch(hs->p, state, opnum,
365 &state->in_data,
366 &state->out_data);
367 if (!NT_STATUS_IS_OK(status)) {
368 tevent_req_nterror(req, status);
369 return tevent_req_post(req, ev);
372 tevent_req_done(req);
373 return tevent_req_post(req, ev);
376 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
377 TALLOC_CTX *mem_ctx,
378 uint8_t **out_data,
379 size_t *out_length,
380 uint32_t *out_flags)
382 struct rpcint_bh_raw_call_state *state =
383 tevent_req_data(req,
384 struct rpcint_bh_raw_call_state);
385 NTSTATUS status;
387 if (tevent_req_is_nterror(req, &status)) {
388 tevent_req_received(req);
389 return status;
392 *out_data = talloc_move(mem_ctx, &state->out_data.data);
393 *out_length = state->out_data.length;
394 *out_flags = 0;
395 tevent_req_received(req);
396 return NT_STATUS_OK;
399 struct rpcint_bh_disconnect_state {
400 uint8_t _dummy;
403 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
404 struct tevent_context *ev,
405 struct dcerpc_binding_handle *h)
407 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
408 struct rpcint_bh_state);
409 struct tevent_req *req;
410 struct rpcint_bh_disconnect_state *state;
411 bool ok;
413 req = tevent_req_create(mem_ctx, &state,
414 struct rpcint_bh_disconnect_state);
415 if (req == NULL) {
416 return NULL;
419 ok = rpcint_bh_is_connected(h);
420 if (!ok) {
421 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
422 return tevent_req_post(req, ev);
426 * TODO: do a real async disconnect ...
428 * For now the caller needs to free pipes_struct
430 hs->p = NULL;
432 tevent_req_done(req);
433 return tevent_req_post(req, ev);
436 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
438 NTSTATUS status;
440 if (tevent_req_is_nterror(req, &status)) {
441 tevent_req_received(req);
442 return status;
445 tevent_req_received(req);
446 return NT_STATUS_OK;
449 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
451 return true;
454 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
455 int ndr_flags,
456 const void *_struct_ptr,
457 const struct ndr_interface_call *call)
459 void *struct_ptr = discard_const(_struct_ptr);
461 if (DEBUGLEVEL < 11) {
462 return;
465 if (ndr_flags & NDR_IN) {
466 ndr_print_function_debug(call->ndr_print,
467 call->name,
468 ndr_flags,
469 struct_ptr);
471 if (ndr_flags & NDR_OUT) {
472 ndr_print_function_debug(call->ndr_print,
473 call->name,
474 ndr_flags,
475 struct_ptr);
479 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
480 .name = "rpcint",
481 .is_connected = rpcint_bh_is_connected,
482 .set_timeout = rpcint_bh_set_timeout,
483 .raw_call_send = rpcint_bh_raw_call_send,
484 .raw_call_recv = rpcint_bh_raw_call_recv,
485 .disconnect_send = rpcint_bh_disconnect_send,
486 .disconnect_recv = rpcint_bh_disconnect_recv,
488 .ref_alloc = rpcint_bh_ref_alloc,
489 .do_ndr_print = rpcint_bh_do_ndr_print,
492 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
493 const struct ndr_syntax_id *abstract_syntax,
494 const struct ndr_interface_table *ndr_table,
495 const struct tsocket_address *remote_address,
496 const struct auth_session_info *session_info,
497 struct messaging_context *msg_ctx,
498 struct dcerpc_binding_handle **binding_handle)
500 struct dcerpc_binding_handle *h;
501 struct rpcint_bh_state *hs;
503 if (ndr_table) {
504 abstract_syntax = &ndr_table->syntax_id;
507 h = dcerpc_binding_handle_create(mem_ctx,
508 &rpcint_bh_ops,
509 NULL,
510 ndr_table,
511 &hs,
512 struct rpcint_bh_state,
513 __location__);
514 if (h == NULL) {
515 return NT_STATUS_NO_MEMORY;
517 hs->p = make_internal_rpc_pipe_p(hs,
518 abstract_syntax,
519 remote_address,
520 session_info,
521 msg_ctx);
522 if (hs->p == NULL) {
523 TALLOC_FREE(h);
524 return NT_STATUS_NO_MEMORY;
527 *binding_handle = h;
528 return NT_STATUS_OK;
531 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
533 * @param[in] mem_ctx The memory context to use.
535 * @param[in] ndr_table Normally the ndr_table_<name>.
537 * @param[in] remote_address The info about the connected client.
539 * @param[in] serversupplied_info The server supplied authentication function.
541 * @param[in] msg_ctx The messaging context that can be used by the server
543 * @param[out] binding_handle A pointer to store the connected
544 * dcerpc_binding_handle
546 * @return NT_STATUS_OK on success, a corresponding NT status if an
547 * error occured.
549 * @code
550 * struct dcerpc_binding_handle *winreg_binding;
551 * NTSTATUS status;
553 * status = rpcint_binding_handle(tmp_ctx,
554 * &ndr_table_winreg,
555 * p->remote_address,
556 * p->session_info,
557 * p->msg_ctx
558 * &winreg_binding);
559 * @endcode
561 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
562 const struct ndr_interface_table *ndr_table,
563 const struct tsocket_address *remote_address,
564 const struct auth_session_info *session_info,
565 struct messaging_context *msg_ctx,
566 struct dcerpc_binding_handle **binding_handle)
568 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
569 session_info, msg_ctx, binding_handle);
573 * @internal
575 * @brief Create a new RPC client context which uses a local transport.
577 * This creates a local transport. It is a shortcut to directly call the server
578 * functions and avoid marshalling.
579 * NOTE: this function should be used only by rpc_pipe_open_interface()
581 * @param[in] mem_ctx The memory context to use.
583 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
584 * ndr_table_<name>.
586 * @param[in] serversupplied_info The server supplied authentication function.
588 * @param[in] remote_address The client address information.
590 * @param[in] msg_ctx The messaging context to use.
592 * @param[out] presult A pointer to store the connected rpc client pipe.
594 * @return NT_STATUS_OK on success, a corresponding NT status if an
595 * error occured.
597 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
598 const struct ndr_syntax_id *abstract_syntax,
599 const struct auth_session_info *session_info,
600 const struct tsocket_address *remote_address,
601 struct messaging_context *msg_ctx,
602 struct rpc_pipe_client **presult)
604 struct rpc_pipe_client *result;
605 NTSTATUS status;
607 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
608 if (result == NULL) {
609 return NT_STATUS_NO_MEMORY;
612 result->abstract_syntax = *abstract_syntax;
613 result->transfer_syntax = ndr_transfer_syntax_ndr;
615 if (remote_address == NULL) {
616 struct tsocket_address *local;
617 int rc;
619 rc = tsocket_address_inet_from_strings(mem_ctx,
620 "ip",
621 "127.0.0.1",
623 &local);
624 if (rc < 0) {
625 TALLOC_FREE(result);
626 return NT_STATUS_NO_MEMORY;
629 remote_address = local;
632 result->max_xmit_frag = -1;
633 result->max_recv_frag = -1;
635 status = rpcint_binding_handle_ex(result,
636 abstract_syntax,
637 NULL,
638 remote_address,
639 session_info,
640 msg_ctx,
641 &result->binding_handle);
642 if (!NT_STATUS_IS_OK(status)) {
643 TALLOC_FREE(result);
644 return status;
647 *presult = result;
648 return NT_STATUS_OK;
651 /****************************************************************************
652 * External pipes functions
653 ***************************************************************************/
655 NTSTATUS make_external_rpc_pipe(TALLOC_CTX *mem_ctx,
656 const char *pipe_name,
657 const struct tsocket_address *local_address,
658 const struct tsocket_address *remote_address,
659 const struct auth_session_info *session_info,
660 struct npa_state **pnpa)
662 TALLOC_CTX *tmp_ctx = talloc_stackframe();
663 struct auth_session_info_transport *session_info_t;
664 struct tevent_context *ev_ctx;
665 struct tevent_req *subreq;
666 const char *socket_np_dir;
667 const char *socket_dir;
668 struct npa_state *npa;
669 int sys_errno;
670 NTSTATUS status;
671 int rc = -1;
672 bool ok;
674 npa = npa_state_init(tmp_ctx);
675 if (npa == NULL) {
676 status = NT_STATUS_NO_MEMORY;
677 goto out;
680 socket_dir = lp_parm_const_string(GLOBAL_SECTION_SNUM,
681 "external_rpc_pipe",
682 "socket_dir",
683 lp_ncalrpc_dir());
684 if (socket_dir == NULL) {
685 DEBUG(0, ("external_rpc_pipe: socket_dir not set\n"));
686 status = NT_STATUS_PIPE_NOT_AVAILABLE;
687 goto out;
690 socket_np_dir = talloc_asprintf(tmp_ctx, "%s/np", socket_dir);
691 if (socket_np_dir == NULL) {
692 DEBUG(0, ("talloc_asprintf failed\n"));
693 status = NT_STATUS_NO_MEMORY;
694 goto out;
697 session_info_t = talloc_zero(tmp_ctx,
698 struct auth_session_info_transport);
699 if (session_info_t == NULL) {
700 DEBUG(0, ("talloc failed\n"));
701 status = NT_STATUS_NO_MEMORY;
702 goto out;
705 session_info_t->session_info = copy_session_info(session_info_t,
706 session_info);
707 if (session_info_t->session_info == NULL) {
708 DEBUG(0, ("copy_session_info failed\n"));
709 status = NT_STATUS_NO_MEMORY;
710 goto out;
713 ev_ctx = s3_tevent_context_init(tmp_ctx);
714 if (ev_ctx == NULL) {
715 DEBUG(0, ("s3_tevent_context_init failed\n"));
716 status = NT_STATUS_NO_MEMORY;
717 goto out;
720 become_root();
721 subreq = tstream_npa_connect_send(tmp_ctx,
722 ev_ctx,
723 socket_np_dir,
724 pipe_name,
725 remote_address, /* client_addr */
726 NULL, /* client_name */
727 local_address, /* server_addr */
728 NULL, /* server_name */
729 session_info_t);
730 if (subreq == NULL) {
731 unbecome_root();
732 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
733 "user %s\\%s failed\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 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
737 goto out;
739 ok = tevent_req_poll(subreq, ev_ctx);
740 unbecome_root();
741 if (!ok) {
742 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
743 "failed for tstream_npa_connect: %s\n",
744 socket_np_dir,
745 pipe_name,
746 session_info_t->session_info->info->domain_name,
747 session_info_t->session_info->info->account_name,
748 strerror(errno)));
749 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
750 goto out;
753 rc = tstream_npa_connect_recv(subreq,
754 &sys_errno,
755 npa,
756 &npa->stream,
757 &npa->file_type,
758 &npa->device_state,
759 &npa->allocation_size);
760 talloc_free(subreq);
761 if (rc != 0) {
762 int l = 1;
764 if (errno == ENOENT) {
765 l = 2;
768 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
769 "user %s\\%s failed: %s\n",
770 socket_np_dir,
771 pipe_name,
772 session_info_t->session_info->info->domain_name,
773 session_info_t->session_info->info->account_name,
774 strerror(sys_errno)));
775 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
776 goto out;
779 *pnpa = talloc_steal(mem_ctx, npa);
780 status = NT_STATUS_OK;
781 out:
782 talloc_free(tmp_ctx);
784 return status;
787 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
788 const char *pipe_name,
789 const struct tsocket_address *local_address,
790 const struct tsocket_address *remote_address,
791 const struct auth_session_info *session_info)
793 struct np_proxy_state *result;
794 char *socket_np_dir;
795 const char *socket_dir;
796 struct tevent_context *ev;
797 struct tevent_req *subreq;
798 struct auth_session_info_transport *session_info_t;
799 bool ok;
800 int ret;
801 int sys_errno;
803 result = talloc(mem_ctx, struct np_proxy_state);
804 if (result == NULL) {
805 DEBUG(0, ("talloc failed\n"));
806 return NULL;
809 result->read_queue = tevent_queue_create(result, "np_read");
810 if (result->read_queue == NULL) {
811 DEBUG(0, ("tevent_queue_create failed\n"));
812 goto fail;
815 result->write_queue = tevent_queue_create(result, "np_write");
816 if (result->write_queue == NULL) {
817 DEBUG(0, ("tevent_queue_create failed\n"));
818 goto fail;
821 ev = s3_tevent_context_init(talloc_tos());
822 if (ev == NULL) {
823 DEBUG(0, ("s3_tevent_context_init failed\n"));
824 goto fail;
827 socket_dir = lp_parm_const_string(
828 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
829 lp_ncalrpc_dir());
830 if (socket_dir == NULL) {
831 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
832 goto fail;
834 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
835 if (socket_np_dir == NULL) {
836 DEBUG(0, ("talloc_asprintf failed\n"));
837 goto fail;
840 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
841 if (session_info_t == NULL) {
842 DEBUG(0, ("talloc failed\n"));
843 goto fail;
846 session_info_t->session_info = copy_session_info(session_info_t,
847 session_info);
848 if (session_info_t->session_info == NULL) {
849 DEBUG(0, ("copy_session_info failed\n"));
850 goto fail;
853 become_root();
854 subreq = tstream_npa_connect_send(talloc_tos(), ev,
855 socket_np_dir,
856 pipe_name,
857 remote_address, /* client_addr */
858 NULL, /* client_name */
859 local_address, /* server_addr */
860 NULL, /* server_name */
861 session_info_t);
862 if (subreq == NULL) {
863 unbecome_root();
864 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
865 "user %s\\%s failed\n",
866 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
867 session_info_t->session_info->info->account_name));
868 goto fail;
870 ok = tevent_req_poll(subreq, ev);
871 unbecome_root();
872 if (!ok) {
873 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
874 "failed for tstream_npa_connect: %s\n",
875 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
876 session_info_t->session_info->info->account_name,
877 strerror(errno)));
878 goto fail;
881 ret = tstream_npa_connect_recv(subreq, &sys_errno,
882 result,
883 &result->npipe,
884 &result->file_type,
885 &result->device_state,
886 &result->allocation_size);
887 TALLOC_FREE(subreq);
888 if (ret != 0) {
889 int l = 1;
890 if (errno == ENOENT) {
891 l = 2;
893 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
894 "user %s\\%s failed: %s\n",
895 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
896 session_info_t->session_info->info->account_name,
897 strerror(sys_errno)));
898 goto fail;
901 return result;
903 fail:
904 TALLOC_FREE(result);
905 return NULL;
908 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
909 const char *pipe_name,
910 const struct ndr_interface_table *table,
911 const struct auth_session_info *session_info,
912 struct rpc_pipe_client **_result)
914 struct tsocket_address *local, *remote;
915 struct rpc_pipe_client *result = NULL;
916 struct np_proxy_state *proxy_state = NULL;
917 struct pipe_auth_data *auth;
918 NTSTATUS status;
919 int ret;
921 /* this is an internal connection, fake up ip addresses */
922 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
923 NULL, 0, &local);
924 if (ret) {
925 return NT_STATUS_NO_MEMORY;
927 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
928 NULL, 0, &remote);
929 if (ret) {
930 return NT_STATUS_NO_MEMORY;
933 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
934 local, remote, session_info);
935 if (!proxy_state) {
936 return NT_STATUS_UNSUCCESSFUL;
939 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
940 if (result == NULL) {
941 status = NT_STATUS_NO_MEMORY;
942 goto done;
945 result->abstract_syntax = table->syntax_id;
946 result->transfer_syntax = ndr_transfer_syntax_ndr;
948 result->desthost = get_myname(result);
949 result->srv_name_slash = talloc_asprintf_strupper_m(
950 result, "\\\\%s", result->desthost);
951 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
952 status = NT_STATUS_NO_MEMORY;
953 goto done;
956 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
957 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
959 status = rpc_transport_tstream_init(result,
960 &proxy_state->npipe,
961 &result->transport);
962 if (!NT_STATUS_IS_OK(status)) {
963 goto done;
966 result->binding_handle = rpccli_bh_create(result, NULL, table);
967 if (result->binding_handle == NULL) {
968 status = NT_STATUS_NO_MEMORY;
969 DEBUG(0, ("Failed to create binding handle.\n"));
970 goto done;
973 result->auth = talloc_zero(result, struct pipe_auth_data);
974 if (!result->auth) {
975 status = NT_STATUS_NO_MEMORY;
976 goto done;
978 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
979 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
981 status = rpccli_anon_bind_data(result, &auth);
982 if (!NT_STATUS_IS_OK(status)) {
983 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
984 goto done;
987 status = rpc_pipe_bind(result, auth);
988 if (!NT_STATUS_IS_OK(status)) {
989 DEBUG(0, ("Failed to bind external pipe.\n"));
990 goto done;
993 done:
994 if (!NT_STATUS_IS_OK(status)) {
995 TALLOC_FREE(result);
997 TALLOC_FREE(proxy_state);
998 *_result = result;
999 return status;
1003 * @brief Create a new RPC client context which uses a local dispatch function
1004 * or a remote transport, depending on rpc_server configuration for the
1005 * specific service.
1007 * @param[in] mem_ctx The memory context to use.
1009 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
1010 * ndr_table_<name>.
1012 * @param[in] serversupplied_info The server supplied authentication function.
1014 * @param[in] remote_address The client address information.
1016 * @param[in] msg_ctx The messaging context to use.
1018 * @param[out] presult A pointer to store the connected rpc client pipe.
1020 * @return NT_STATUS_OK on success, a corresponding NT status if an
1021 * error occured.
1023 * @code
1024 * struct rpc_pipe_client *winreg_pipe;
1025 * NTSTATUS status;
1027 * status = rpc_pipe_open_interface(tmp_ctx,
1028 * &ndr_table_winreg.syntax_id,
1029 * p->session_info,
1030 * remote_address,
1031 * &winreg_pipe);
1032 * @endcode
1035 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
1036 const struct ndr_interface_table *table,
1037 const struct auth_session_info *session_info,
1038 const struct tsocket_address *remote_address,
1039 struct messaging_context *msg_ctx,
1040 struct rpc_pipe_client **cli_pipe)
1042 struct rpc_pipe_client *cli = NULL;
1043 enum rpc_service_mode_e pipe_mode;
1044 const char *pipe_name;
1045 NTSTATUS status;
1046 TALLOC_CTX *tmp_ctx;
1048 if (cli_pipe != NULL) {
1049 if (rpccli_is_connected(*cli_pipe)) {
1050 return NT_STATUS_OK;
1051 } else {
1052 TALLOC_FREE(*cli_pipe);
1056 tmp_ctx = talloc_stackframe();
1057 if (tmp_ctx == NULL) {
1058 return NT_STATUS_NO_MEMORY;
1061 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
1062 if (pipe_name == NULL) {
1063 status = NT_STATUS_INVALID_PARAMETER;
1064 goto done;
1067 while (pipe_name[0] == '\\') {
1068 pipe_name++;
1071 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
1073 pipe_mode = rpc_service_mode(pipe_name);
1075 switch (pipe_mode) {
1076 case RPC_SERVICE_MODE_EMBEDDED:
1077 status = rpc_pipe_open_internal(tmp_ctx,
1078 &table->syntax_id, session_info,
1079 remote_address, msg_ctx,
1080 &cli);
1081 if (!NT_STATUS_IS_OK(status)) {
1082 goto done;
1084 break;
1085 case RPC_SERVICE_MODE_EXTERNAL:
1086 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
1087 * for now we need to use the special proxy setup to connect
1088 * to spoolssd. */
1090 status = rpc_pipe_open_external(tmp_ctx,
1091 pipe_name, table,
1092 session_info,
1093 &cli);
1094 if (!NT_STATUS_IS_OK(status)) {
1095 goto done;
1097 break;
1098 case RPC_SERVICE_MODE_DISABLED:
1099 status = NT_STATUS_NOT_IMPLEMENTED;
1100 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
1101 pipe_name, nt_errstr(status)));
1102 goto done;
1105 status = NT_STATUS_OK;
1106 done:
1107 if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
1108 *cli_pipe = talloc_move(mem_ctx, &cli);
1110 TALLOC_FREE(tmp_ctx);
1111 return status;