s3-lib Replace StrCaseCmp() with strcasecmp_m()
[Samba.git] / source3 / rpc_server / rpc_ncacn_np.c
blob36ee3038e5741530a7670724edb54e53017be65d
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"
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_RPC_SRV
40 static int pipes_open;
42 static struct pipes_struct *InternalPipes;
44 /* TODO
45 * the following prototypes are declared here to avoid
46 * code being moved about too much for a patch to be
47 * disrupted / less obvious.
49 * these functions, and associated functions that they
50 * call, should be moved behind a .so module-loading
51 * system _anyway_. so that's the next step...
54 /****************************************************************************
55 Internal Pipe iterator functions.
56 ****************************************************************************/
58 struct pipes_struct *get_first_internal_pipe(void)
60 return InternalPipes;
63 struct pipes_struct *get_next_internal_pipe(struct pipes_struct *p)
65 return p->next;
68 static void free_pipe_rpc_context_internal( PIPE_RPC_FNS *list )
70 PIPE_RPC_FNS *tmp = list;
71 PIPE_RPC_FNS *tmp2;
73 while (tmp) {
74 tmp2 = tmp->next;
75 SAFE_FREE(tmp);
76 tmp = tmp2;
79 return;
82 bool check_open_pipes(void)
84 struct pipes_struct *p;
86 for (p = InternalPipes; p != NULL; p = p->next) {
87 if (num_pipe_handles(p) != 0) {
88 return true;
91 return false;
94 /****************************************************************************
95 Close an rpc pipe.
96 ****************************************************************************/
98 int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
100 if (!p) {
101 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
102 return False;
105 TALLOC_FREE(p->auth.auth_ctx);
107 free_pipe_rpc_context_internal( p->contexts );
109 /* Free the handles database. */
110 close_policy_by_pipe(p);
112 DLIST_REMOVE(InternalPipes, p);
114 ZERO_STRUCTP(p);
116 return 0;
119 /****************************************************************************
120 Make an internal namedpipes structure
121 ****************************************************************************/
123 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
124 const struct ndr_syntax_id *syntax,
125 struct client_address *client_id,
126 const struct auth_serversupplied_info *session_info,
127 struct messaging_context *msg_ctx)
129 struct pipes_struct *p;
131 DEBUG(4,("Create pipe requested %s\n",
132 get_pipe_name_from_syntax(talloc_tos(), syntax)));
134 p = TALLOC_ZERO_P(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->client_id = client_id;
170 p->endian = RPC_LITTLE_ENDIAN;
172 p->syntax = *syntax;
173 p->transport = NCALRPC;
175 DEBUG(4,("Created internal pipe %s (pipes_open=%d)\n",
176 get_pipe_name_from_syntax(talloc_tos(), syntax), pipes_open));
178 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
180 return p;
183 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
184 TALLOC_CTX *mem_ctx,
185 uint32_t opnum,
186 const DATA_BLOB *in_data,
187 DATA_BLOB *out_data)
189 uint32_t num_cmds = rpc_srv_get_pipe_num_cmds(&p->syntax);
190 const struct api_struct *cmds = rpc_srv_get_pipe_cmds(&p->syntax);
191 uint32_t i;
192 bool ok;
194 /* set opnum */
195 p->opnum = opnum;
197 for (i = 0; i < num_cmds; i++) {
198 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
199 break;
203 if (i == num_cmds) {
204 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
207 p->in_data.data = *in_data;
208 p->out_data.rdata = data_blob_null;
210 ok = cmds[i].fn(p);
211 p->in_data.data = data_blob_null;
212 if (!ok) {
213 data_blob_free(&p->out_data.rdata);
214 talloc_free_children(p->mem_ctx);
215 return NT_STATUS_RPC_CALL_FAILED;
218 if (p->fault_state) {
219 p->fault_state = false;
220 data_blob_free(&p->out_data.rdata);
221 talloc_free_children(p->mem_ctx);
222 return NT_STATUS_RPC_CALL_FAILED;
225 if (p->bad_handle_fault_state) {
226 p->bad_handle_fault_state = false;
227 data_blob_free(&p->out_data.rdata);
228 talloc_free_children(p->mem_ctx);
229 return NT_STATUS_RPC_SS_CONTEXT_MISMATCH;
232 if (p->rng_fault_state) {
233 p->rng_fault_state = false;
234 data_blob_free(&p->out_data.rdata);
235 talloc_free_children(p->mem_ctx);
236 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
239 *out_data = p->out_data.rdata;
240 talloc_steal(mem_ctx, out_data->data);
241 p->out_data.rdata = data_blob_null;
243 talloc_free_children(p->mem_ctx);
244 return NT_STATUS_OK;
247 struct rpcint_bh_state {
248 struct pipes_struct *p;
251 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
253 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
254 struct rpcint_bh_state);
256 if (!hs->p) {
257 return false;
260 return true;
263 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
264 uint32_t timeout)
266 /* TODO: implement timeouts */
267 return UINT32_MAX;
270 struct rpcint_bh_raw_call_state {
271 DATA_BLOB in_data;
272 DATA_BLOB out_data;
273 uint32_t out_flags;
276 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
277 struct tevent_context *ev,
278 struct dcerpc_binding_handle *h,
279 const struct GUID *object,
280 uint32_t opnum,
281 uint32_t in_flags,
282 const uint8_t *in_data,
283 size_t in_length)
285 struct rpcint_bh_state *hs =
286 dcerpc_binding_handle_data(h,
287 struct rpcint_bh_state);
288 struct tevent_req *req;
289 struct rpcint_bh_raw_call_state *state;
290 bool ok;
291 NTSTATUS status;
293 req = tevent_req_create(mem_ctx, &state,
294 struct rpcint_bh_raw_call_state);
295 if (req == NULL) {
296 return NULL;
298 state->in_data.data = discard_const_p(uint8_t, in_data);
299 state->in_data.length = in_length;
301 ok = rpcint_bh_is_connected(h);
302 if (!ok) {
303 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
304 return tevent_req_post(req, ev);
307 /* TODO: allow async */
308 status = rpcint_dispatch(hs->p, state, opnum,
309 &state->in_data,
310 &state->out_data);
311 if (!NT_STATUS_IS_OK(status)) {
312 tevent_req_nterror(req, status);
313 return tevent_req_post(req, ev);
316 tevent_req_done(req);
317 return tevent_req_post(req, ev);
320 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
321 TALLOC_CTX *mem_ctx,
322 uint8_t **out_data,
323 size_t *out_length,
324 uint32_t *out_flags)
326 struct rpcint_bh_raw_call_state *state =
327 tevent_req_data(req,
328 struct rpcint_bh_raw_call_state);
329 NTSTATUS status;
331 if (tevent_req_is_nterror(req, &status)) {
332 tevent_req_received(req);
333 return status;
336 *out_data = talloc_move(mem_ctx, &state->out_data.data);
337 *out_length = state->out_data.length;
338 *out_flags = 0;
339 tevent_req_received(req);
340 return NT_STATUS_OK;
343 struct rpcint_bh_disconnect_state {
344 uint8_t _dummy;
347 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
348 struct tevent_context *ev,
349 struct dcerpc_binding_handle *h)
351 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
352 struct rpcint_bh_state);
353 struct tevent_req *req;
354 struct rpcint_bh_disconnect_state *state;
355 bool ok;
357 req = tevent_req_create(mem_ctx, &state,
358 struct rpcint_bh_disconnect_state);
359 if (req == NULL) {
360 return NULL;
363 ok = rpcint_bh_is_connected(h);
364 if (!ok) {
365 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
366 return tevent_req_post(req, ev);
370 * TODO: do a real async disconnect ...
372 * For now the caller needs to free pipes_struct
374 hs->p = NULL;
376 tevent_req_done(req);
377 return tevent_req_post(req, ev);
380 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
382 NTSTATUS status;
384 if (tevent_req_is_nterror(req, &status)) {
385 tevent_req_received(req);
386 return status;
389 tevent_req_received(req);
390 return NT_STATUS_OK;
393 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
395 return true;
398 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
399 int ndr_flags,
400 const void *_struct_ptr,
401 const struct ndr_interface_call *call)
403 void *struct_ptr = discard_const(_struct_ptr);
405 if (DEBUGLEVEL < 11) {
406 return;
409 if (ndr_flags & NDR_IN) {
410 ndr_print_function_debug(call->ndr_print,
411 call->name,
412 ndr_flags,
413 struct_ptr);
415 if (ndr_flags & NDR_OUT) {
416 ndr_print_function_debug(call->ndr_print,
417 call->name,
418 ndr_flags,
419 struct_ptr);
423 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
424 .name = "rpcint",
425 .is_connected = rpcint_bh_is_connected,
426 .set_timeout = rpcint_bh_set_timeout,
427 .raw_call_send = rpcint_bh_raw_call_send,
428 .raw_call_recv = rpcint_bh_raw_call_recv,
429 .disconnect_send = rpcint_bh_disconnect_send,
430 .disconnect_recv = rpcint_bh_disconnect_recv,
432 .ref_alloc = rpcint_bh_ref_alloc,
433 .do_ndr_print = rpcint_bh_do_ndr_print,
436 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
437 const struct ndr_syntax_id *abstract_syntax,
438 const struct ndr_interface_table *ndr_table,
439 struct client_address *client_id,
440 const struct auth_serversupplied_info *session_info,
441 struct messaging_context *msg_ctx,
442 struct dcerpc_binding_handle **binding_handle)
444 struct dcerpc_binding_handle *h;
445 struct rpcint_bh_state *hs;
447 if (ndr_table) {
448 abstract_syntax = &ndr_table->syntax_id;
451 h = dcerpc_binding_handle_create(mem_ctx,
452 &rpcint_bh_ops,
453 NULL,
454 ndr_table,
455 &hs,
456 struct rpcint_bh_state,
457 __location__);
458 if (h == NULL) {
459 return NT_STATUS_NO_MEMORY;
461 hs->p = make_internal_rpc_pipe_p(hs,
462 abstract_syntax,
463 client_id,
464 session_info,
465 msg_ctx);
466 if (hs->p == NULL) {
467 TALLOC_FREE(h);
468 return NT_STATUS_NO_MEMORY;
471 *binding_handle = h;
472 return NT_STATUS_OK;
475 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
477 * @param[in] mem_ctx The memory context to use.
479 * @param[in] ndr_table Normally the ndr_table_<name>.
481 * @param[in] client_id The info about the connected client.
483 * @param[in] serversupplied_info The server supplied authentication function.
485 * @param[in] msg_ctx The messaging context that can be used by the server
487 * @param[out] binding_handle A pointer to store the connected
488 * dcerpc_binding_handle
490 * @return NT_STATUS_OK on success, a corresponding NT status if an
491 * error occured.
493 * @code
494 * struct dcerpc_binding_handle *winreg_binding;
495 * NTSTATUS status;
497 * status = rpcint_binding_handle(tmp_ctx,
498 * &ndr_table_winreg,
499 * p->client_id,
500 * p->session_info,
501 * p->msg_ctx
502 * &winreg_binding);
503 * @endcode
505 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
506 const struct ndr_interface_table *ndr_table,
507 struct client_address *client_id,
508 const struct auth_serversupplied_info *session_info,
509 struct messaging_context *msg_ctx,
510 struct dcerpc_binding_handle **binding_handle)
512 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, client_id,
513 session_info, msg_ctx, binding_handle);
517 * @internal
519 * @brief Create a new RPC client context which uses a local transport.
521 * This creates a local transport. It is a shortcut to directly call the server
522 * functions and avoid marshalling.
523 * NOTE: this function should be used only by rpc_pipe_open_interface()
525 * @param[in] mem_ctx The memory context to use.
527 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
528 * ndr_table_<name>.
530 * @param[in] serversupplied_info The server supplied authentication function.
532 * @param[in] client_id The client address information.
534 * @param[in] msg_ctx The messaging context to use.
536 * @param[out] presult A pointer to store the connected rpc client pipe.
538 * @return NT_STATUS_OK on success, a corresponding NT status if an
539 * error occured.
541 static NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
542 const struct ndr_syntax_id *abstract_syntax,
543 const struct auth_serversupplied_info *serversupplied_info,
544 struct client_address *client_id,
545 struct messaging_context *msg_ctx,
546 struct rpc_pipe_client **presult)
548 struct rpc_pipe_client *result;
549 NTSTATUS status;
551 result = TALLOC_ZERO_P(mem_ctx, struct rpc_pipe_client);
552 if (result == NULL) {
553 return NT_STATUS_NO_MEMORY;
556 result->abstract_syntax = *abstract_syntax;
557 result->transfer_syntax = ndr_transfer_syntax;
559 if (client_id == NULL) {
560 static struct client_address unknown;
561 strlcpy(unknown.addr, "<UNKNOWN>", sizeof(unknown.addr));
562 unknown.name = "<UNKNOWN>";
563 client_id = &unknown;
566 result->max_xmit_frag = -1;
567 result->max_recv_frag = -1;
569 status = rpcint_binding_handle_ex(result,
570 abstract_syntax,
571 NULL,
572 client_id,
573 serversupplied_info,
574 msg_ctx,
575 &result->binding_handle);
576 if (!NT_STATUS_IS_OK(status)) {
577 TALLOC_FREE(result);
578 return status;
581 *presult = result;
582 return NT_STATUS_OK;
585 /****************************************************************************
586 * External pipes functions
587 ***************************************************************************/
590 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
591 const char *pipe_name,
592 const struct tsocket_address *local_address,
593 const struct tsocket_address *remote_address,
594 const struct auth_serversupplied_info *session_info)
596 struct np_proxy_state *result;
597 char *socket_np_dir;
598 const char *socket_dir;
599 struct tevent_context *ev;
600 struct tevent_req *subreq;
601 struct auth_session_info_transport *session_info_t;
602 struct auth_session_info *session_info_npa;
603 struct auth_user_info_dc *user_info_dc;
604 union netr_Validation val;
605 NTSTATUS status;
606 bool ok;
607 int ret;
608 int sys_errno;
610 result = talloc(mem_ctx, struct np_proxy_state);
611 if (result == NULL) {
612 DEBUG(0, ("talloc failed\n"));
613 return NULL;
616 result->read_queue = tevent_queue_create(result, "np_read");
617 if (result->read_queue == NULL) {
618 DEBUG(0, ("tevent_queue_create failed\n"));
619 goto fail;
622 result->write_queue = tevent_queue_create(result, "np_write");
623 if (result->write_queue == NULL) {
624 DEBUG(0, ("tevent_queue_create failed\n"));
625 goto fail;
628 ev = s3_tevent_context_init(talloc_tos());
629 if (ev == NULL) {
630 DEBUG(0, ("s3_tevent_context_init failed\n"));
631 goto fail;
634 socket_dir = lp_parm_const_string(
635 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
636 lp_ncalrpc_dir());
637 if (socket_dir == NULL) {
638 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
639 goto fail;
641 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
642 if (socket_np_dir == NULL) {
643 DEBUG(0, ("talloc_asprintf failed\n"));
644 goto fail;
647 session_info_npa = talloc_zero(talloc_tos(), struct auth_session_info);
648 if (session_info_npa == NULL) {
649 DEBUG(0, ("talloc failed\n"));
650 goto fail;
653 /* Send the named_pipe_auth server the user's full token */
654 session_info_npa->security_token = session_info->security_token;
655 session_info_npa->session_key = session_info->session_key;
657 val.sam3 = session_info->info3;
659 /* Convert into something we can build a struct
660 * auth_session_info from. Most of the work here
661 * will be to convert the SIDS, which we will then ignore, but
662 * this is the easier way to handle it */
663 status = make_user_info_dc_netlogon_validation(talloc_tos(), "", 3, &val, &user_info_dc);
664 if (!NT_STATUS_IS_OK(status)) {
665 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
666 goto fail;
669 session_info_npa->info = talloc_move(session_info_npa, &user_info_dc->info);
670 talloc_free(user_info_dc);
672 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
673 if (session_info_npa == NULL) {
674 DEBUG(0, ("talloc failed\n"));
675 goto fail;
678 session_info_t->session_info = talloc_steal(session_info_t, session_info_npa);
680 become_root();
681 subreq = tstream_npa_connect_send(talloc_tos(), ev,
682 socket_np_dir,
683 pipe_name,
684 remote_address, /* client_addr */
685 NULL, /* client_name */
686 local_address, /* server_addr */
687 NULL, /* server_name */
688 session_info_t);
689 if (subreq == NULL) {
690 unbecome_root();
691 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
692 "user %s\\%s failed\n",
693 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
694 session_info_t->session_info->info->account_name));
695 goto fail;
697 ok = tevent_req_poll(subreq, ev);
698 unbecome_root();
699 if (!ok) {
700 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
701 "failed for tstream_npa_connect: %s\n",
702 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
703 session_info_t->session_info->info->account_name,
704 strerror(errno)));
705 goto fail;
708 ret = tstream_npa_connect_recv(subreq, &sys_errno,
709 result,
710 &result->npipe,
711 &result->file_type,
712 &result->device_state,
713 &result->allocation_size);
714 TALLOC_FREE(subreq);
715 if (ret != 0) {
716 DEBUG(0, ("tstream_npa_connect_recv to %s for pipe %s and "
717 "user %s\\%s failed: %s\n",
718 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
719 session_info_t->session_info->info->account_name,
720 strerror(sys_errno)));
721 goto fail;
724 return result;
726 fail:
727 TALLOC_FREE(result);
728 return NULL;
731 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
732 const char *pipe_name,
733 const struct ndr_syntax_id *abstract_syntax,
734 const struct auth_serversupplied_info *session_info,
735 struct rpc_pipe_client **_result)
737 struct tsocket_address *local, *remote;
738 struct rpc_pipe_client *result = NULL;
739 struct np_proxy_state *proxy_state = NULL;
740 struct pipe_auth_data *auth;
741 NTSTATUS status;
742 int ret;
744 /* this is an internal connection, fake up ip addresses */
745 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
746 NULL, 0, &local);
747 if (ret) {
748 return NT_STATUS_NO_MEMORY;
750 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
751 NULL, 0, &remote);
752 if (ret) {
753 return NT_STATUS_NO_MEMORY;
756 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
757 local, remote, session_info);
758 if (!proxy_state) {
759 return NT_STATUS_UNSUCCESSFUL;
762 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
763 if (result == NULL) {
764 status = NT_STATUS_NO_MEMORY;
765 goto done;
768 result->abstract_syntax = *abstract_syntax;
769 result->transfer_syntax = ndr_transfer_syntax;
771 result->desthost = get_myname(result);
772 result->srv_name_slash = talloc_asprintf_strupper_m(
773 result, "\\\\%s", result->desthost);
774 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
775 status = NT_STATUS_NO_MEMORY;
776 goto done;
779 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
780 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
782 status = rpc_transport_tstream_init(result,
783 &proxy_state->npipe,
784 &result->transport);
785 if (!NT_STATUS_IS_OK(status)) {
786 goto done;
789 result->binding_handle = rpccli_bh_create(result);
790 if (result->binding_handle == NULL) {
791 status = NT_STATUS_NO_MEMORY;
792 DEBUG(0, ("Failed to create binding handle.\n"));
793 goto done;
796 result->auth = talloc_zero(result, struct pipe_auth_data);
797 if (!result->auth) {
798 status = NT_STATUS_NO_MEMORY;
799 goto done;
801 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
802 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
804 status = rpccli_anon_bind_data(result, &auth);
805 if (!NT_STATUS_IS_OK(status)) {
806 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
807 goto done;
810 status = rpc_pipe_bind(result, auth);
811 if (!NT_STATUS_IS_OK(status)) {
812 DEBUG(0, ("Failed to bind external pipe.\n"));
813 goto done;
816 done:
817 if (!NT_STATUS_IS_OK(status)) {
818 TALLOC_FREE(result);
820 TALLOC_FREE(proxy_state);
821 *_result = result;
822 return status;
826 * @brief Create a new RPC client context which uses a local dispatch function
827 * or a remote transport, depending on rpc_server configuration for the
828 * specific service.
830 * @param[in] mem_ctx The memory context to use.
832 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
833 * ndr_table_<name>.
835 * @param[in] serversupplied_info The server supplied authentication function.
837 * @param[in] client_id The client address information.
839 * @param[in] msg_ctx The messaging context to use.
841 * @param[out] presult A pointer to store the connected rpc client pipe.
843 * @return NT_STATUS_OK on success, a corresponding NT status if an
844 * error occured.
846 * @code
847 * struct rpc_pipe_client *winreg_pipe;
848 * NTSTATUS status;
850 * status = rpc_pipe_open_interface(tmp_ctx,
851 * &ndr_table_winreg.syntax_id,
852 * p->session_info,
853 * client_id,
854 * &winreg_pipe);
855 * @endcode
858 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
859 const struct ndr_syntax_id *syntax,
860 const struct auth_serversupplied_info *session_info,
861 struct client_address *client_id,
862 struct messaging_context *msg_ctx,
863 struct rpc_pipe_client **cli_pipe)
865 struct rpc_pipe_client *cli = NULL;
866 const char *server_type;
867 const char *pipe_name;
868 NTSTATUS status;
869 TALLOC_CTX *tmp_ctx;
871 if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
872 return NT_STATUS_OK;
873 } else {
874 TALLOC_FREE(*cli_pipe);
877 tmp_ctx = talloc_stackframe();
878 if (tmp_ctx == NULL) {
879 return NT_STATUS_NO_MEMORY;
882 pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
883 if (pipe_name == NULL) {
884 status = NT_STATUS_INVALID_PARAMETER;
885 goto done;
888 while (pipe_name[0] == '\\') {
889 pipe_name++;
892 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
894 server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
895 "rpc_server", pipe_name,
896 "embedded");
898 if (strcasecmp_m(server_type, "embedded") == 0) {
899 status = rpc_pipe_open_internal(tmp_ctx,
900 syntax, session_info,
901 client_id, msg_ctx,
902 &cli);
903 if (!NT_STATUS_IS_OK(status)) {
904 goto done;
906 } else if (strcasecmp_m(server_type, "daemon") == 0 ||
907 strcasecmp_m(server_type, "external") == 0) {
908 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
909 * for now we need to use the special proxy setup to connect
910 * to spoolssd. */
912 status = rpc_pipe_open_external(tmp_ctx,
913 pipe_name, syntax,
914 session_info,
915 &cli);
916 if (!NT_STATUS_IS_OK(status)) {
917 goto done;
919 } else {
920 status = NT_STATUS_NOT_IMPLEMENTED;
921 DEBUG(0, ("Wrong servertype specified in config file: %s",
922 nt_errstr(status)));
923 goto done;
926 status = NT_STATUS_OK;
927 done:
928 if (NT_STATUS_IS_OK(status)) {
929 *cli_pipe = talloc_move(mem_ctx, &cli);
931 TALLOC_FREE(tmp_ctx);
932 return status;