s3:smbd: use PROTOCOL_SMB2_02 instead PROTOCOL_SMB2
[Samba/gebeck_regimport.git] / source3 / rpc_server / rpc_ncacn_np.c
blobb2a22295a6d4878378e583abc063f58f931a8373
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
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "rpc_client/cli_pipe.h"
25 #include "rpc_server/srv_pipe_internal.h"
26 #include "rpc_dce.h"
27 #include "../libcli/named_pipe_auth/npa_tstream.h"
28 #include "rpc_server/rpc_ncacn_np.h"
29 #include "librpc/gen_ndr/netlogon.h"
30 #include "librpc/gen_ndr/auth.h"
31 #include "../auth/auth_sam_reply.h"
32 #include "auth.h"
33 #include "ntdomain.h"
34 #include "../lib/tsocket/tsocket.h"
35 #include "../lib/util/tevent_ntstatus.h"
36 #include "rpc_contexts.h"
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_RPC_SRV
41 static struct pipes_struct *InternalPipes;
43 /* TODO
44 * the following prototypes are declared here to avoid
45 * code being moved about too much for a patch to be
46 * disrupted / less obvious.
48 * these functions, and associated functions that they
49 * call, should be moved behind a .so module-loading
50 * system _anyway_. so that's the next step...
53 /****************************************************************************
54 Internal Pipe iterator functions.
55 ****************************************************************************/
57 struct pipes_struct *get_first_internal_pipe(void)
59 return InternalPipes;
62 struct pipes_struct *get_next_internal_pipe(struct pipes_struct *p)
64 return p->next;
67 static void free_pipe_rpc_context_internal( PIPE_RPC_FNS *list )
69 PIPE_RPC_FNS *tmp = list;
70 PIPE_RPC_FNS *tmp2;
72 while (tmp) {
73 tmp2 = tmp->next;
74 SAFE_FREE(tmp);
75 tmp = tmp2;
78 return;
81 bool check_open_pipes(void)
83 struct pipes_struct *p;
85 for (p = InternalPipes; p != NULL; p = p->next) {
86 if (num_pipe_handles(p) != 0) {
87 return true;
90 return false;
93 /****************************************************************************
94 Close an rpc pipe.
95 ****************************************************************************/
97 int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
99 if (!p) {
100 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
101 return False;
104 TALLOC_FREE(p->auth.auth_ctx);
106 /* Free the handles database. */
107 close_policy_by_pipe(p);
109 free_pipe_rpc_context_internal( p->contexts );
111 DLIST_REMOVE(InternalPipes, p);
113 ZERO_STRUCTP(p);
115 return 0;
118 /****************************************************************************
119 Make an internal namedpipes structure
120 ****************************************************************************/
122 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
123 const struct ndr_syntax_id *syntax,
124 const struct tsocket_address *remote_address,
125 const struct auth_serversupplied_info *session_info,
126 struct messaging_context *msg_ctx)
128 struct pipes_struct *p;
129 struct pipe_rpc_fns *context_fns;
131 DEBUG(4,("Create pipe requested %s\n",
132 get_pipe_name_from_syntax(talloc_tos(), syntax)));
134 p = talloc_zero(mem_ctx, struct pipes_struct);
136 if (!p) {
137 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
138 return NULL;
141 p->mem_ctx = talloc_named(p, 0, "pipe %s %p",
142 get_pipe_name_from_syntax(talloc_tos(),
143 syntax), p);
144 if (p->mem_ctx == NULL) {
145 DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
146 TALLOC_FREE(p);
147 return NULL;
150 if (!init_pipe_handles(p, syntax)) {
151 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
152 TALLOC_FREE(p);
153 return NULL;
156 p->session_info = copy_serverinfo(p, session_info);
157 if (p->session_info == NULL) {
158 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
159 close_policy_by_pipe(p);
160 TALLOC_FREE(p);
161 return NULL;
164 p->msg_ctx = msg_ctx;
166 DLIST_ADD(InternalPipes, p);
168 p->remote_address = tsocket_address_copy(remote_address, p);
169 if (p->remote_address == NULL) {
170 return false;
173 p->endian = RPC_LITTLE_ENDIAN;
175 p->transport = NCALRPC;
177 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
178 if (context_fns == NULL) {
179 DEBUG(0,("malloc() failed!\n"));
180 return False;
183 context_fns->next = context_fns->prev = NULL;
184 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
185 context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
186 context_fns->context_id = 0;
187 context_fns->syntax = *syntax;
189 /* add to the list of open contexts */
190 DLIST_ADD(p->contexts, context_fns);
192 DEBUG(4,("Created internal pipe %s\n",
193 get_pipe_name_from_syntax(talloc_tos(), syntax)));
195 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
197 return p;
200 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
201 TALLOC_CTX *mem_ctx,
202 uint32_t opnum,
203 const DATA_BLOB *in_data,
204 DATA_BLOB *out_data)
206 struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
207 uint32_t num_cmds = fns->n_cmds;
208 const struct api_struct *cmds = fns->cmds;
209 uint32_t i;
210 bool ok;
212 /* set opnum */
213 p->opnum = opnum;
215 for (i = 0; i < num_cmds; i++) {
216 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
217 break;
221 if (i == num_cmds) {
222 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
225 p->in_data.data = *in_data;
226 p->out_data.rdata = data_blob_null;
228 ok = cmds[i].fn(p);
229 p->in_data.data = data_blob_null;
230 if (!ok) {
231 data_blob_free(&p->out_data.rdata);
232 talloc_free_children(p->mem_ctx);
233 return NT_STATUS_RPC_CALL_FAILED;
236 if (p->fault_state) {
237 p->fault_state = false;
238 data_blob_free(&p->out_data.rdata);
239 talloc_free_children(p->mem_ctx);
240 return NT_STATUS_RPC_CALL_FAILED;
243 if (p->bad_handle_fault_state) {
244 p->bad_handle_fault_state = false;
245 data_blob_free(&p->out_data.rdata);
246 talloc_free_children(p->mem_ctx);
247 return NT_STATUS_RPC_SS_CONTEXT_MISMATCH;
250 if (p->rng_fault_state) {
251 p->rng_fault_state = false;
252 data_blob_free(&p->out_data.rdata);
253 talloc_free_children(p->mem_ctx);
254 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
257 *out_data = p->out_data.rdata;
258 talloc_steal(mem_ctx, out_data->data);
259 p->out_data.rdata = data_blob_null;
261 talloc_free_children(p->mem_ctx);
262 return NT_STATUS_OK;
265 struct rpcint_bh_state {
266 struct pipes_struct *p;
269 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
271 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
272 struct rpcint_bh_state);
274 if (!hs->p) {
275 return false;
278 return true;
281 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
282 uint32_t timeout)
284 /* TODO: implement timeouts */
285 return UINT32_MAX;
288 struct rpcint_bh_raw_call_state {
289 DATA_BLOB in_data;
290 DATA_BLOB out_data;
291 uint32_t out_flags;
294 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
295 struct tevent_context *ev,
296 struct dcerpc_binding_handle *h,
297 const struct GUID *object,
298 uint32_t opnum,
299 uint32_t in_flags,
300 const uint8_t *in_data,
301 size_t in_length)
303 struct rpcint_bh_state *hs =
304 dcerpc_binding_handle_data(h,
305 struct rpcint_bh_state);
306 struct tevent_req *req;
307 struct rpcint_bh_raw_call_state *state;
308 bool ok;
309 NTSTATUS status;
311 req = tevent_req_create(mem_ctx, &state,
312 struct rpcint_bh_raw_call_state);
313 if (req == NULL) {
314 return NULL;
316 state->in_data.data = discard_const_p(uint8_t, in_data);
317 state->in_data.length = in_length;
319 ok = rpcint_bh_is_connected(h);
320 if (!ok) {
321 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
322 return tevent_req_post(req, ev);
325 /* TODO: allow async */
326 status = rpcint_dispatch(hs->p, state, opnum,
327 &state->in_data,
328 &state->out_data);
329 if (!NT_STATUS_IS_OK(status)) {
330 tevent_req_nterror(req, status);
331 return tevent_req_post(req, ev);
334 tevent_req_done(req);
335 return tevent_req_post(req, ev);
338 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
339 TALLOC_CTX *mem_ctx,
340 uint8_t **out_data,
341 size_t *out_length,
342 uint32_t *out_flags)
344 struct rpcint_bh_raw_call_state *state =
345 tevent_req_data(req,
346 struct rpcint_bh_raw_call_state);
347 NTSTATUS status;
349 if (tevent_req_is_nterror(req, &status)) {
350 tevent_req_received(req);
351 return status;
354 *out_data = talloc_move(mem_ctx, &state->out_data.data);
355 *out_length = state->out_data.length;
356 *out_flags = 0;
357 tevent_req_received(req);
358 return NT_STATUS_OK;
361 struct rpcint_bh_disconnect_state {
362 uint8_t _dummy;
365 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
366 struct tevent_context *ev,
367 struct dcerpc_binding_handle *h)
369 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
370 struct rpcint_bh_state);
371 struct tevent_req *req;
372 struct rpcint_bh_disconnect_state *state;
373 bool ok;
375 req = tevent_req_create(mem_ctx, &state,
376 struct rpcint_bh_disconnect_state);
377 if (req == NULL) {
378 return NULL;
381 ok = rpcint_bh_is_connected(h);
382 if (!ok) {
383 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
384 return tevent_req_post(req, ev);
388 * TODO: do a real async disconnect ...
390 * For now the caller needs to free pipes_struct
392 hs->p = NULL;
394 tevent_req_done(req);
395 return tevent_req_post(req, ev);
398 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
400 NTSTATUS status;
402 if (tevent_req_is_nterror(req, &status)) {
403 tevent_req_received(req);
404 return status;
407 tevent_req_received(req);
408 return NT_STATUS_OK;
411 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
413 return true;
416 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
417 int ndr_flags,
418 const void *_struct_ptr,
419 const struct ndr_interface_call *call)
421 void *struct_ptr = discard_const(_struct_ptr);
423 if (DEBUGLEVEL < 11) {
424 return;
427 if (ndr_flags & NDR_IN) {
428 ndr_print_function_debug(call->ndr_print,
429 call->name,
430 ndr_flags,
431 struct_ptr);
433 if (ndr_flags & NDR_OUT) {
434 ndr_print_function_debug(call->ndr_print,
435 call->name,
436 ndr_flags,
437 struct_ptr);
441 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
442 .name = "rpcint",
443 .is_connected = rpcint_bh_is_connected,
444 .set_timeout = rpcint_bh_set_timeout,
445 .raw_call_send = rpcint_bh_raw_call_send,
446 .raw_call_recv = rpcint_bh_raw_call_recv,
447 .disconnect_send = rpcint_bh_disconnect_send,
448 .disconnect_recv = rpcint_bh_disconnect_recv,
450 .ref_alloc = rpcint_bh_ref_alloc,
451 .do_ndr_print = rpcint_bh_do_ndr_print,
454 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
455 const struct ndr_syntax_id *abstract_syntax,
456 const struct ndr_interface_table *ndr_table,
457 const struct tsocket_address *remote_address,
458 const struct auth_serversupplied_info *session_info,
459 struct messaging_context *msg_ctx,
460 struct dcerpc_binding_handle **binding_handle)
462 struct dcerpc_binding_handle *h;
463 struct rpcint_bh_state *hs;
465 if (ndr_table) {
466 abstract_syntax = &ndr_table->syntax_id;
469 h = dcerpc_binding_handle_create(mem_ctx,
470 &rpcint_bh_ops,
471 NULL,
472 ndr_table,
473 &hs,
474 struct rpcint_bh_state,
475 __location__);
476 if (h == NULL) {
477 return NT_STATUS_NO_MEMORY;
479 hs->p = make_internal_rpc_pipe_p(hs,
480 abstract_syntax,
481 remote_address,
482 session_info,
483 msg_ctx);
484 if (hs->p == NULL) {
485 TALLOC_FREE(h);
486 return NT_STATUS_NO_MEMORY;
489 *binding_handle = h;
490 return NT_STATUS_OK;
493 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
495 * @param[in] mem_ctx The memory context to use.
497 * @param[in] ndr_table Normally the ndr_table_<name>.
499 * @param[in] remote_address The info about the connected client.
501 * @param[in] serversupplied_info The server supplied authentication function.
503 * @param[in] msg_ctx The messaging context that can be used by the server
505 * @param[out] binding_handle A pointer to store the connected
506 * dcerpc_binding_handle
508 * @return NT_STATUS_OK on success, a corresponding NT status if an
509 * error occured.
511 * @code
512 * struct dcerpc_binding_handle *winreg_binding;
513 * NTSTATUS status;
515 * status = rpcint_binding_handle(tmp_ctx,
516 * &ndr_table_winreg,
517 * p->remote_address,
518 * p->session_info,
519 * p->msg_ctx
520 * &winreg_binding);
521 * @endcode
523 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
524 const struct ndr_interface_table *ndr_table,
525 const struct tsocket_address *remote_address,
526 const struct auth_serversupplied_info *session_info,
527 struct messaging_context *msg_ctx,
528 struct dcerpc_binding_handle **binding_handle)
530 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
531 session_info, msg_ctx, binding_handle);
535 * @internal
537 * @brief Create a new RPC client context which uses a local transport.
539 * This creates a local transport. It is a shortcut to directly call the server
540 * functions and avoid marshalling.
541 * NOTE: this function should be used only by rpc_pipe_open_interface()
543 * @param[in] mem_ctx The memory context to use.
545 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
546 * ndr_table_<name>.
548 * @param[in] serversupplied_info The server supplied authentication function.
550 * @param[in] remote_address The client address information.
552 * @param[in] msg_ctx The messaging context to use.
554 * @param[out] presult A pointer to store the connected rpc client pipe.
556 * @return NT_STATUS_OK on success, a corresponding NT status if an
557 * error occured.
559 static NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
560 const struct ndr_syntax_id *abstract_syntax,
561 const struct auth_serversupplied_info *serversupplied_info,
562 const struct tsocket_address *remote_address,
563 struct messaging_context *msg_ctx,
564 struct rpc_pipe_client **presult)
566 struct rpc_pipe_client *result;
567 NTSTATUS status;
569 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
570 if (result == NULL) {
571 return NT_STATUS_NO_MEMORY;
574 result->abstract_syntax = *abstract_syntax;
575 result->transfer_syntax = ndr_transfer_syntax;
577 if (remote_address == NULL) {
578 struct tsocket_address *local;
579 int rc;
581 rc = tsocket_address_inet_from_strings(mem_ctx,
582 "ip",
583 "127.0.0.1",
585 &local);
586 if (rc < 0) {
587 TALLOC_FREE(result);
588 return NT_STATUS_NO_MEMORY;
591 remote_address = local;
594 result->max_xmit_frag = -1;
595 result->max_recv_frag = -1;
597 status = rpcint_binding_handle_ex(result,
598 abstract_syntax,
599 NULL,
600 remote_address,
601 serversupplied_info,
602 msg_ctx,
603 &result->binding_handle);
604 if (!NT_STATUS_IS_OK(status)) {
605 TALLOC_FREE(result);
606 return status;
609 *presult = result;
610 return NT_STATUS_OK;
613 /****************************************************************************
614 * External pipes functions
615 ***************************************************************************/
618 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
619 const char *pipe_name,
620 const struct tsocket_address *local_address,
621 const struct tsocket_address *remote_address,
622 const struct auth_serversupplied_info *session_info)
624 struct np_proxy_state *result;
625 char *socket_np_dir;
626 const char *socket_dir;
627 struct tevent_context *ev;
628 struct tevent_req *subreq;
629 struct auth_session_info_transport *session_info_t;
630 struct auth_session_info *session_info_npa;
631 struct auth_user_info_dc *user_info_dc;
632 union netr_Validation val;
633 NTSTATUS status;
634 bool ok;
635 int ret;
636 int sys_errno;
638 result = talloc(mem_ctx, struct np_proxy_state);
639 if (result == NULL) {
640 DEBUG(0, ("talloc failed\n"));
641 return NULL;
644 result->read_queue = tevent_queue_create(result, "np_read");
645 if (result->read_queue == NULL) {
646 DEBUG(0, ("tevent_queue_create failed\n"));
647 goto fail;
650 result->write_queue = tevent_queue_create(result, "np_write");
651 if (result->write_queue == NULL) {
652 DEBUG(0, ("tevent_queue_create failed\n"));
653 goto fail;
656 ev = s3_tevent_context_init(talloc_tos());
657 if (ev == NULL) {
658 DEBUG(0, ("s3_tevent_context_init failed\n"));
659 goto fail;
662 socket_dir = lp_parm_const_string(
663 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
664 lp_ncalrpc_dir());
665 if (socket_dir == NULL) {
666 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
667 goto fail;
669 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
670 if (socket_np_dir == NULL) {
671 DEBUG(0, ("talloc_asprintf failed\n"));
672 goto fail;
675 session_info_npa = talloc_zero(talloc_tos(), struct auth_session_info);
676 if (session_info_npa == NULL) {
677 DEBUG(0, ("talloc failed\n"));
678 goto fail;
681 /* Send the named_pipe_auth server the user's full token */
682 session_info_npa->security_token = session_info->security_token;
683 session_info_npa->session_key = session_info->session_key;
685 val.sam3 = session_info->info3;
687 /* Convert into something we can build a struct
688 * auth_session_info from. Most of the work here
689 * will be to convert the SIDS, which we will then ignore, but
690 * this is the easier way to handle it */
691 status = make_user_info_dc_netlogon_validation(talloc_tos(), "", 3, &val, &user_info_dc);
692 if (!NT_STATUS_IS_OK(status)) {
693 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
694 goto fail;
697 session_info_npa->info = talloc_move(session_info_npa, &user_info_dc->info);
698 talloc_free(user_info_dc);
700 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
701 if (session_info_npa == NULL) {
702 DEBUG(0, ("talloc failed\n"));
703 goto fail;
706 session_info_t->session_info = talloc_steal(session_info_t, session_info_npa);
708 become_root();
709 subreq = tstream_npa_connect_send(talloc_tos(), ev,
710 socket_np_dir,
711 pipe_name,
712 remote_address, /* client_addr */
713 NULL, /* client_name */
714 local_address, /* server_addr */
715 NULL, /* server_name */
716 session_info_t);
717 if (subreq == NULL) {
718 unbecome_root();
719 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
720 "user %s\\%s failed\n",
721 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
722 session_info_t->session_info->info->account_name));
723 goto fail;
725 ok = tevent_req_poll(subreq, ev);
726 unbecome_root();
727 if (!ok) {
728 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
729 "failed for tstream_npa_connect: %s\n",
730 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
731 session_info_t->session_info->info->account_name,
732 strerror(errno)));
733 goto fail;
736 ret = tstream_npa_connect_recv(subreq, &sys_errno,
737 result,
738 &result->npipe,
739 &result->file_type,
740 &result->device_state,
741 &result->allocation_size);
742 TALLOC_FREE(subreq);
743 if (ret != 0) {
744 DEBUG(0, ("tstream_npa_connect_recv to %s for pipe %s and "
745 "user %s\\%s failed: %s\n",
746 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
747 session_info_t->session_info->info->account_name,
748 strerror(sys_errno)));
749 goto fail;
752 return result;
754 fail:
755 TALLOC_FREE(result);
756 return NULL;
759 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
760 const char *pipe_name,
761 const struct ndr_syntax_id *abstract_syntax,
762 const struct auth_serversupplied_info *session_info,
763 struct rpc_pipe_client **_result)
765 struct tsocket_address *local, *remote;
766 struct rpc_pipe_client *result = NULL;
767 struct np_proxy_state *proxy_state = NULL;
768 struct pipe_auth_data *auth;
769 NTSTATUS status;
770 int ret;
772 /* this is an internal connection, fake up ip addresses */
773 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
774 NULL, 0, &local);
775 if (ret) {
776 return NT_STATUS_NO_MEMORY;
778 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
779 NULL, 0, &remote);
780 if (ret) {
781 return NT_STATUS_NO_MEMORY;
784 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
785 local, remote, session_info);
786 if (!proxy_state) {
787 return NT_STATUS_UNSUCCESSFUL;
790 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
791 if (result == NULL) {
792 status = NT_STATUS_NO_MEMORY;
793 goto done;
796 result->abstract_syntax = *abstract_syntax;
797 result->transfer_syntax = ndr_transfer_syntax;
799 result->desthost = get_myname(result);
800 result->srv_name_slash = talloc_asprintf_strupper_m(
801 result, "\\\\%s", result->desthost);
802 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
803 status = NT_STATUS_NO_MEMORY;
804 goto done;
807 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
808 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
810 status = rpc_transport_tstream_init(result,
811 &proxy_state->npipe,
812 &result->transport);
813 if (!NT_STATUS_IS_OK(status)) {
814 goto done;
817 result->binding_handle = rpccli_bh_create(result);
818 if (result->binding_handle == NULL) {
819 status = NT_STATUS_NO_MEMORY;
820 DEBUG(0, ("Failed to create binding handle.\n"));
821 goto done;
824 result->auth = talloc_zero(result, struct pipe_auth_data);
825 if (!result->auth) {
826 status = NT_STATUS_NO_MEMORY;
827 goto done;
829 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
830 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
832 status = rpccli_anon_bind_data(result, &auth);
833 if (!NT_STATUS_IS_OK(status)) {
834 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
835 goto done;
838 status = rpc_pipe_bind(result, auth);
839 if (!NT_STATUS_IS_OK(status)) {
840 DEBUG(0, ("Failed to bind external pipe.\n"));
841 goto done;
844 done:
845 if (!NT_STATUS_IS_OK(status)) {
846 TALLOC_FREE(result);
848 TALLOC_FREE(proxy_state);
849 *_result = result;
850 return status;
854 * @brief Create a new RPC client context which uses a local dispatch function
855 * or a remote transport, depending on rpc_server configuration for the
856 * specific service.
858 * @param[in] mem_ctx The memory context to use.
860 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
861 * ndr_table_<name>.
863 * @param[in] serversupplied_info The server supplied authentication function.
865 * @param[in] remote_address The client address information.
867 * @param[in] msg_ctx The messaging context to use.
869 * @param[out] presult A pointer to store the connected rpc client pipe.
871 * @return NT_STATUS_OK on success, a corresponding NT status if an
872 * error occured.
874 * @code
875 * struct rpc_pipe_client *winreg_pipe;
876 * NTSTATUS status;
878 * status = rpc_pipe_open_interface(tmp_ctx,
879 * &ndr_table_winreg.syntax_id,
880 * p->session_info,
881 * remote_address,
882 * &winreg_pipe);
883 * @endcode
886 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
887 const struct ndr_syntax_id *syntax,
888 const struct auth_serversupplied_info *session_info,
889 const struct tsocket_address *remote_address,
890 struct messaging_context *msg_ctx,
891 struct rpc_pipe_client **cli_pipe)
893 struct rpc_pipe_client *cli = NULL;
894 const char *server_type;
895 const char *pipe_name;
896 NTSTATUS status;
897 TALLOC_CTX *tmp_ctx;
899 if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
900 return NT_STATUS_OK;
901 } else {
902 TALLOC_FREE(*cli_pipe);
905 tmp_ctx = talloc_stackframe();
906 if (tmp_ctx == NULL) {
907 return NT_STATUS_NO_MEMORY;
910 pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
911 if (pipe_name == NULL) {
912 status = NT_STATUS_INVALID_PARAMETER;
913 goto done;
916 while (pipe_name[0] == '\\') {
917 pipe_name++;
920 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
922 server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
923 "rpc_server", pipe_name,
924 "embedded");
926 if (strcasecmp_m(server_type, "embedded") == 0) {
927 status = rpc_pipe_open_internal(tmp_ctx,
928 syntax, session_info,
929 remote_address, msg_ctx,
930 &cli);
931 if (!NT_STATUS_IS_OK(status)) {
932 goto done;
934 } else if (strcasecmp_m(server_type, "daemon") == 0 ||
935 strcasecmp_m(server_type, "external") == 0) {
936 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
937 * for now we need to use the special proxy setup to connect
938 * to spoolssd. */
940 status = rpc_pipe_open_external(tmp_ctx,
941 pipe_name, syntax,
942 session_info,
943 &cli);
944 if (!NT_STATUS_IS_OK(status)) {
945 goto done;
947 } else {
948 status = NT_STATUS_NOT_IMPLEMENTED;
949 DEBUG(0, ("Wrong servertype specified in config file: %s",
950 nt_errstr(status)));
951 goto done;
954 status = NT_STATUS_OK;
955 done:
956 if (NT_STATUS_IS_OK(status)) {
957 *cli_pipe = talloc_move(mem_ctx, &cli);
959 TALLOC_FREE(tmp_ctx);
960 return status;