s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / rpc_server / rpc_ncacn_np.c
blob2ed4a017043bae31da93f78d9c30d6fb82e0e8c7
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"
39 #undef DBGC_CLASS
40 #define DBGC_CLASS DBGC_RPC_SRV
42 /****************************************************************************
43 Make an internal namedpipes structure
44 ****************************************************************************/
46 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
47 const struct ndr_syntax_id *syntax,
48 const struct tsocket_address *remote_address,
49 const struct auth_session_info *session_info,
50 struct messaging_context *msg_ctx)
52 struct pipes_struct *p;
53 struct pipe_rpc_fns *context_fns;
54 const char *pipe_name;
55 int ret;
57 pipe_name = get_pipe_name_from_syntax(talloc_tos(), syntax);
59 DEBUG(4,("Create pipe requested %s\n", pipe_name));
61 ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
62 NCALRPC, RPC_LITTLE_ENDIAN, false,
63 remote_address, NULL, &p);
64 if (ret) {
65 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
66 return NULL;
69 if (!init_pipe_handles(p, syntax)) {
70 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
71 TALLOC_FREE(p);
72 return NULL;
75 p->session_info = copy_session_info(p, session_info);
76 if (p->session_info == NULL) {
77 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
78 close_policy_by_pipe(p);
79 TALLOC_FREE(p);
80 return NULL;
83 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
84 if (context_fns == NULL) {
85 DEBUG(0,("malloc() failed!\n"));
86 return False;
89 context_fns->next = context_fns->prev = NULL;
90 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
91 context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
92 context_fns->context_id = 0;
93 context_fns->syntax = *syntax;
95 /* add to the list of open contexts */
96 DLIST_ADD(p->contexts, context_fns);
98 DEBUG(4,("Created internal pipe %s\n", pipe_name));
100 return p;
103 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
104 TALLOC_CTX *mem_ctx,
105 uint32_t opnum,
106 const DATA_BLOB *in_data,
107 DATA_BLOB *out_data)
109 struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
110 uint32_t num_cmds = fns->n_cmds;
111 const struct api_struct *cmds = fns->cmds;
112 uint32_t i;
113 bool ok;
115 /* set opnum */
116 p->opnum = opnum;
118 for (i = 0; i < num_cmds; i++) {
119 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
120 break;
124 if (i == num_cmds) {
125 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
128 p->in_data.data = *in_data;
129 p->out_data.rdata = data_blob_null;
131 ok = cmds[i].fn(p);
132 p->in_data.data = data_blob_null;
133 if (!ok) {
134 data_blob_free(&p->out_data.rdata);
135 talloc_free_children(p->mem_ctx);
136 return NT_STATUS_RPC_CALL_FAILED;
139 if (p->fault_state) {
140 p->fault_state = false;
141 data_blob_free(&p->out_data.rdata);
142 talloc_free_children(p->mem_ctx);
143 return NT_STATUS_RPC_CALL_FAILED;
146 if (p->bad_handle_fault_state) {
147 p->bad_handle_fault_state = false;
148 data_blob_free(&p->out_data.rdata);
149 talloc_free_children(p->mem_ctx);
150 return NT_STATUS_RPC_SS_CONTEXT_MISMATCH;
153 if (p->rng_fault_state) {
154 p->rng_fault_state = false;
155 data_blob_free(&p->out_data.rdata);
156 talloc_free_children(p->mem_ctx);
157 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
160 *out_data = p->out_data.rdata;
161 talloc_steal(mem_ctx, out_data->data);
162 p->out_data.rdata = data_blob_null;
164 talloc_free_children(p->mem_ctx);
165 return NT_STATUS_OK;
168 struct rpcint_bh_state {
169 struct pipes_struct *p;
172 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
174 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
175 struct rpcint_bh_state);
177 if (!hs->p) {
178 return false;
181 return true;
184 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
185 uint32_t timeout)
187 /* TODO: implement timeouts */
188 return UINT32_MAX;
191 struct rpcint_bh_raw_call_state {
192 DATA_BLOB in_data;
193 DATA_BLOB out_data;
194 uint32_t out_flags;
197 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
198 struct tevent_context *ev,
199 struct dcerpc_binding_handle *h,
200 const struct GUID *object,
201 uint32_t opnum,
202 uint32_t in_flags,
203 const uint8_t *in_data,
204 size_t in_length)
206 struct rpcint_bh_state *hs =
207 dcerpc_binding_handle_data(h,
208 struct rpcint_bh_state);
209 struct tevent_req *req;
210 struct rpcint_bh_raw_call_state *state;
211 bool ok;
212 NTSTATUS status;
214 req = tevent_req_create(mem_ctx, &state,
215 struct rpcint_bh_raw_call_state);
216 if (req == NULL) {
217 return NULL;
219 state->in_data.data = discard_const_p(uint8_t, in_data);
220 state->in_data.length = in_length;
222 ok = rpcint_bh_is_connected(h);
223 if (!ok) {
224 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
225 return tevent_req_post(req, ev);
228 /* TODO: allow async */
229 status = rpcint_dispatch(hs->p, state, opnum,
230 &state->in_data,
231 &state->out_data);
232 if (!NT_STATUS_IS_OK(status)) {
233 tevent_req_nterror(req, status);
234 return tevent_req_post(req, ev);
237 tevent_req_done(req);
238 return tevent_req_post(req, ev);
241 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
242 TALLOC_CTX *mem_ctx,
243 uint8_t **out_data,
244 size_t *out_length,
245 uint32_t *out_flags)
247 struct rpcint_bh_raw_call_state *state =
248 tevent_req_data(req,
249 struct rpcint_bh_raw_call_state);
250 NTSTATUS status;
252 if (tevent_req_is_nterror(req, &status)) {
253 tevent_req_received(req);
254 return status;
257 *out_data = talloc_move(mem_ctx, &state->out_data.data);
258 *out_length = state->out_data.length;
259 *out_flags = 0;
260 tevent_req_received(req);
261 return NT_STATUS_OK;
264 struct rpcint_bh_disconnect_state {
265 uint8_t _dummy;
268 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
269 struct tevent_context *ev,
270 struct dcerpc_binding_handle *h)
272 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
273 struct rpcint_bh_state);
274 struct tevent_req *req;
275 struct rpcint_bh_disconnect_state *state;
276 bool ok;
278 req = tevent_req_create(mem_ctx, &state,
279 struct rpcint_bh_disconnect_state);
280 if (req == NULL) {
281 return NULL;
284 ok = rpcint_bh_is_connected(h);
285 if (!ok) {
286 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
287 return tevent_req_post(req, ev);
291 * TODO: do a real async disconnect ...
293 * For now the caller needs to free pipes_struct
295 hs->p = NULL;
297 tevent_req_done(req);
298 return tevent_req_post(req, ev);
301 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
303 NTSTATUS status;
305 if (tevent_req_is_nterror(req, &status)) {
306 tevent_req_received(req);
307 return status;
310 tevent_req_received(req);
311 return NT_STATUS_OK;
314 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
316 return true;
319 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
320 int ndr_flags,
321 const void *_struct_ptr,
322 const struct ndr_interface_call *call)
324 void *struct_ptr = discard_const(_struct_ptr);
326 if (DEBUGLEVEL < 11) {
327 return;
330 if (ndr_flags & NDR_IN) {
331 ndr_print_function_debug(call->ndr_print,
332 call->name,
333 ndr_flags,
334 struct_ptr);
336 if (ndr_flags & NDR_OUT) {
337 ndr_print_function_debug(call->ndr_print,
338 call->name,
339 ndr_flags,
340 struct_ptr);
344 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
345 .name = "rpcint",
346 .is_connected = rpcint_bh_is_connected,
347 .set_timeout = rpcint_bh_set_timeout,
348 .raw_call_send = rpcint_bh_raw_call_send,
349 .raw_call_recv = rpcint_bh_raw_call_recv,
350 .disconnect_send = rpcint_bh_disconnect_send,
351 .disconnect_recv = rpcint_bh_disconnect_recv,
353 .ref_alloc = rpcint_bh_ref_alloc,
354 .do_ndr_print = rpcint_bh_do_ndr_print,
357 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
358 const struct ndr_syntax_id *abstract_syntax,
359 const struct ndr_interface_table *ndr_table,
360 const struct tsocket_address *remote_address,
361 const struct auth_session_info *session_info,
362 struct messaging_context *msg_ctx,
363 struct dcerpc_binding_handle **binding_handle)
365 struct dcerpc_binding_handle *h;
366 struct rpcint_bh_state *hs;
368 if (ndr_table) {
369 abstract_syntax = &ndr_table->syntax_id;
372 h = dcerpc_binding_handle_create(mem_ctx,
373 &rpcint_bh_ops,
374 NULL,
375 ndr_table,
376 &hs,
377 struct rpcint_bh_state,
378 __location__);
379 if (h == NULL) {
380 return NT_STATUS_NO_MEMORY;
382 hs->p = make_internal_rpc_pipe_p(hs,
383 abstract_syntax,
384 remote_address,
385 session_info,
386 msg_ctx);
387 if (hs->p == NULL) {
388 TALLOC_FREE(h);
389 return NT_STATUS_NO_MEMORY;
392 *binding_handle = h;
393 return NT_STATUS_OK;
396 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
398 * @param[in] mem_ctx The memory context to use.
400 * @param[in] ndr_table Normally the ndr_table_<name>.
402 * @param[in] remote_address The info about the connected client.
404 * @param[in] serversupplied_info The server supplied authentication function.
406 * @param[in] msg_ctx The messaging context that can be used by the server
408 * @param[out] binding_handle A pointer to store the connected
409 * dcerpc_binding_handle
411 * @return NT_STATUS_OK on success, a corresponding NT status if an
412 * error occured.
414 * @code
415 * struct dcerpc_binding_handle *winreg_binding;
416 * NTSTATUS status;
418 * status = rpcint_binding_handle(tmp_ctx,
419 * &ndr_table_winreg,
420 * p->remote_address,
421 * p->session_info,
422 * p->msg_ctx
423 * &winreg_binding);
424 * @endcode
426 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
427 const struct ndr_interface_table *ndr_table,
428 const struct tsocket_address *remote_address,
429 const struct auth_session_info *session_info,
430 struct messaging_context *msg_ctx,
431 struct dcerpc_binding_handle **binding_handle)
433 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
434 session_info, msg_ctx, binding_handle);
438 * @internal
440 * @brief Create a new RPC client context which uses a local transport.
442 * This creates a local transport. It is a shortcut to directly call the server
443 * functions and avoid marshalling.
444 * NOTE: this function should be used only by rpc_pipe_open_interface()
446 * @param[in] mem_ctx The memory context to use.
448 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
449 * ndr_table_<name>.
451 * @param[in] serversupplied_info The server supplied authentication function.
453 * @param[in] remote_address The client address information.
455 * @param[in] msg_ctx The messaging context to use.
457 * @param[out] presult A pointer to store the connected rpc client pipe.
459 * @return NT_STATUS_OK on success, a corresponding NT status if an
460 * error occured.
462 static NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
463 const struct ndr_syntax_id *abstract_syntax,
464 const struct auth_session_info *session_info,
465 const struct tsocket_address *remote_address,
466 struct messaging_context *msg_ctx,
467 struct rpc_pipe_client **presult)
469 struct rpc_pipe_client *result;
470 NTSTATUS status;
472 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
473 if (result == NULL) {
474 return NT_STATUS_NO_MEMORY;
477 result->abstract_syntax = *abstract_syntax;
478 result->transfer_syntax = ndr_transfer_syntax;
480 if (remote_address == NULL) {
481 struct tsocket_address *local;
482 int rc;
484 rc = tsocket_address_inet_from_strings(mem_ctx,
485 "ip",
486 "127.0.0.1",
488 &local);
489 if (rc < 0) {
490 TALLOC_FREE(result);
491 return NT_STATUS_NO_MEMORY;
494 remote_address = local;
497 result->max_xmit_frag = -1;
498 result->max_recv_frag = -1;
500 status = rpcint_binding_handle_ex(result,
501 abstract_syntax,
502 NULL,
503 remote_address,
504 session_info,
505 msg_ctx,
506 &result->binding_handle);
507 if (!NT_STATUS_IS_OK(status)) {
508 TALLOC_FREE(result);
509 return status;
512 *presult = result;
513 return NT_STATUS_OK;
516 /****************************************************************************
517 * External pipes functions
518 ***************************************************************************/
521 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
522 const char *pipe_name,
523 const struct tsocket_address *local_address,
524 const struct tsocket_address *remote_address,
525 const struct auth_session_info *session_info)
527 struct np_proxy_state *result;
528 char *socket_np_dir;
529 const char *socket_dir;
530 struct tevent_context *ev;
531 struct tevent_req *subreq;
532 struct auth_session_info_transport *session_info_t;
533 bool ok;
534 int ret;
535 int sys_errno;
537 result = talloc(mem_ctx, struct np_proxy_state);
538 if (result == NULL) {
539 DEBUG(0, ("talloc failed\n"));
540 return NULL;
543 result->read_queue = tevent_queue_create(result, "np_read");
544 if (result->read_queue == NULL) {
545 DEBUG(0, ("tevent_queue_create failed\n"));
546 goto fail;
549 result->write_queue = tevent_queue_create(result, "np_write");
550 if (result->write_queue == NULL) {
551 DEBUG(0, ("tevent_queue_create failed\n"));
552 goto fail;
555 ev = s3_tevent_context_init(talloc_tos());
556 if (ev == NULL) {
557 DEBUG(0, ("s3_tevent_context_init failed\n"));
558 goto fail;
561 socket_dir = lp_parm_const_string(
562 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
563 lp_ncalrpc_dir());
564 if (socket_dir == NULL) {
565 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
566 goto fail;
568 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
569 if (socket_np_dir == NULL) {
570 DEBUG(0, ("talloc_asprintf failed\n"));
571 goto fail;
574 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
575 if (session_info_t == NULL) {
576 DEBUG(0, ("talloc failed\n"));
577 goto fail;
580 session_info_t->session_info = copy_session_info(session_info_t,
581 session_info);
582 if (session_info_t->session_info == NULL) {
583 DEBUG(0, ("copy_session_info failed\n"));
584 goto fail;
587 become_root();
588 subreq = tstream_npa_connect_send(talloc_tos(), ev,
589 socket_np_dir,
590 pipe_name,
591 remote_address, /* client_addr */
592 NULL, /* client_name */
593 local_address, /* server_addr */
594 NULL, /* server_name */
595 session_info_t);
596 if (subreq == NULL) {
597 unbecome_root();
598 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
599 "user %s\\%s failed\n",
600 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
601 session_info_t->session_info->info->account_name));
602 goto fail;
604 ok = tevent_req_poll(subreq, ev);
605 unbecome_root();
606 if (!ok) {
607 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
608 "failed for tstream_npa_connect: %s\n",
609 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
610 session_info_t->session_info->info->account_name,
611 strerror(errno)));
612 goto fail;
615 ret = tstream_npa_connect_recv(subreq, &sys_errno,
616 result,
617 &result->npipe,
618 &result->file_type,
619 &result->device_state,
620 &result->allocation_size);
621 TALLOC_FREE(subreq);
622 if (ret != 0) {
623 DEBUG(0, ("tstream_npa_connect_recv to %s for pipe %s and "
624 "user %s\\%s failed: %s\n",
625 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
626 session_info_t->session_info->info->account_name,
627 strerror(sys_errno)));
628 goto fail;
631 return result;
633 fail:
634 TALLOC_FREE(result);
635 return NULL;
638 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
639 const char *pipe_name,
640 const struct ndr_syntax_id *abstract_syntax,
641 const struct auth_session_info *session_info,
642 struct rpc_pipe_client **_result)
644 struct tsocket_address *local, *remote;
645 struct rpc_pipe_client *result = NULL;
646 struct np_proxy_state *proxy_state = NULL;
647 struct pipe_auth_data *auth;
648 NTSTATUS status;
649 int ret;
651 /* this is an internal connection, fake up ip addresses */
652 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
653 NULL, 0, &local);
654 if (ret) {
655 return NT_STATUS_NO_MEMORY;
657 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
658 NULL, 0, &remote);
659 if (ret) {
660 return NT_STATUS_NO_MEMORY;
663 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
664 local, remote, session_info);
665 if (!proxy_state) {
666 return NT_STATUS_UNSUCCESSFUL;
669 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
670 if (result == NULL) {
671 status = NT_STATUS_NO_MEMORY;
672 goto done;
675 result->abstract_syntax = *abstract_syntax;
676 result->transfer_syntax = ndr_transfer_syntax;
678 result->desthost = get_myname(result);
679 result->srv_name_slash = talloc_asprintf_strupper_m(
680 result, "\\\\%s", result->desthost);
681 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
682 status = NT_STATUS_NO_MEMORY;
683 goto done;
686 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
687 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
689 status = rpc_transport_tstream_init(result,
690 &proxy_state->npipe,
691 &result->transport);
692 if (!NT_STATUS_IS_OK(status)) {
693 goto done;
696 result->binding_handle = rpccli_bh_create(result);
697 if (result->binding_handle == NULL) {
698 status = NT_STATUS_NO_MEMORY;
699 DEBUG(0, ("Failed to create binding handle.\n"));
700 goto done;
703 result->auth = talloc_zero(result, struct pipe_auth_data);
704 if (!result->auth) {
705 status = NT_STATUS_NO_MEMORY;
706 goto done;
708 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
709 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
711 status = rpccli_anon_bind_data(result, &auth);
712 if (!NT_STATUS_IS_OK(status)) {
713 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
714 goto done;
717 status = rpc_pipe_bind(result, auth);
718 if (!NT_STATUS_IS_OK(status)) {
719 DEBUG(0, ("Failed to bind external pipe.\n"));
720 goto done;
723 done:
724 if (!NT_STATUS_IS_OK(status)) {
725 TALLOC_FREE(result);
727 TALLOC_FREE(proxy_state);
728 *_result = result;
729 return status;
733 * @brief Create a new RPC client context which uses a local dispatch function
734 * or a remote transport, depending on rpc_server configuration for the
735 * specific service.
737 * @param[in] mem_ctx The memory context to use.
739 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
740 * ndr_table_<name>.
742 * @param[in] serversupplied_info The server supplied authentication function.
744 * @param[in] remote_address The client address information.
746 * @param[in] msg_ctx The messaging context to use.
748 * @param[out] presult A pointer to store the connected rpc client pipe.
750 * @return NT_STATUS_OK on success, a corresponding NT status if an
751 * error occured.
753 * @code
754 * struct rpc_pipe_client *winreg_pipe;
755 * NTSTATUS status;
757 * status = rpc_pipe_open_interface(tmp_ctx,
758 * &ndr_table_winreg.syntax_id,
759 * p->session_info,
760 * remote_address,
761 * &winreg_pipe);
762 * @endcode
765 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
766 const struct ndr_syntax_id *syntax,
767 const struct auth_session_info *session_info,
768 const struct tsocket_address *remote_address,
769 struct messaging_context *msg_ctx,
770 struct rpc_pipe_client **cli_pipe)
772 struct rpc_pipe_client *cli = NULL;
773 const char *server_type;
774 const char *pipe_name;
775 NTSTATUS status;
776 TALLOC_CTX *tmp_ctx;
778 if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
779 return NT_STATUS_OK;
780 } else {
781 TALLOC_FREE(*cli_pipe);
784 tmp_ctx = talloc_stackframe();
785 if (tmp_ctx == NULL) {
786 return NT_STATUS_NO_MEMORY;
789 pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
790 if (pipe_name == NULL) {
791 status = NT_STATUS_INVALID_PARAMETER;
792 goto done;
795 while (pipe_name[0] == '\\') {
796 pipe_name++;
799 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
801 server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
802 "rpc_server", pipe_name,
803 "embedded");
805 if (strcasecmp_m(server_type, "embedded") == 0) {
806 status = rpc_pipe_open_internal(tmp_ctx,
807 syntax, session_info,
808 remote_address, msg_ctx,
809 &cli);
810 if (!NT_STATUS_IS_OK(status)) {
811 goto done;
813 } else if (strcasecmp_m(server_type, "daemon") == 0 ||
814 strcasecmp_m(server_type, "external") == 0) {
815 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
816 * for now we need to use the special proxy setup to connect
817 * to spoolssd. */
819 status = rpc_pipe_open_external(tmp_ctx,
820 pipe_name, syntax,
821 session_info,
822 &cli);
823 if (!NT_STATUS_IS_OK(status)) {
824 goto done;
826 } else {
827 status = NT_STATUS_NOT_IMPLEMENTED;
828 DEBUG(0, ("Wrong servertype specified in config file: %s",
829 nt_errstr(status)));
830 goto done;
833 status = NT_STATUS_OK;
834 done:
835 if (NT_STATUS_IS_OK(status)) {
836 *cli_pipe = talloc_move(mem_ctx, &cli);
838 TALLOC_FREE(tmp_ctx);
839 return status;