add script to build only the manual pages (not all the docs) - git version
[Samba/gbeck.git] / source3 / rpc_server / rpc_ncacn_np.c
blobea65e042abfb6531a9866bba02a8410ea8f2a12d
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_server/srv_pipe_internal.h"
25 #include "rpc_dce.h"
26 #include "../libcli/named_pipe_auth/npa_tstream.h"
27 #include "rpc_server/rpc_ncacn_np.h"
28 #include "librpc/gen_ndr/netlogon.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_RPC_SRV
33 static int pipes_open;
35 static struct pipes_struct *InternalPipes;
37 /* TODO
38 * the following prototypes are declared here to avoid
39 * code being moved about too much for a patch to be
40 * disrupted / less obvious.
42 * these functions, and associated functions that they
43 * call, should be moved behind a .so module-loading
44 * system _anyway_. so that's the next step...
47 /****************************************************************************
48 Internal Pipe iterator functions.
49 ****************************************************************************/
51 struct pipes_struct *get_first_internal_pipe(void)
53 return InternalPipes;
56 struct pipes_struct *get_next_internal_pipe(struct pipes_struct *p)
58 return p->next;
61 static void free_pipe_rpc_context_internal( PIPE_RPC_FNS *list )
63 PIPE_RPC_FNS *tmp = list;
64 PIPE_RPC_FNS *tmp2;
66 while (tmp) {
67 tmp2 = tmp->next;
68 SAFE_FREE(tmp);
69 tmp = tmp2;
72 return;
75 bool check_open_pipes(void)
77 struct pipes_struct *p;
79 for (p = InternalPipes; p != NULL; p = p->next) {
80 if (num_pipe_handles(p) != 0) {
81 return true;
84 return false;
87 /****************************************************************************
88 Close an rpc pipe.
89 ****************************************************************************/
91 int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
93 if (!p) {
94 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
95 return False;
98 if (p->auth.auth_data_free_func) {
99 (*p->auth.auth_data_free_func)(&p->auth);
102 free_pipe_rpc_context_internal( p->contexts );
104 /* Free the handles database. */
105 close_policy_by_pipe(p);
107 DLIST_REMOVE(InternalPipes, p);
109 ZERO_STRUCTP(p);
111 return 0;
114 /****************************************************************************
115 Make an internal namedpipes structure
116 ****************************************************************************/
118 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
119 const struct ndr_syntax_id *syntax,
120 struct client_address *client_id,
121 const struct auth_serversupplied_info *server_info,
122 struct messaging_context *msg_ctx)
124 struct pipes_struct *p;
126 DEBUG(4,("Create pipe requested %s\n",
127 get_pipe_name_from_syntax(talloc_tos(), syntax)));
129 p = TALLOC_ZERO_P(mem_ctx, struct pipes_struct);
131 if (!p) {
132 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
133 return NULL;
136 p->mem_ctx = talloc_named(p, 0, "pipe %s %p",
137 get_pipe_name_from_syntax(talloc_tos(),
138 syntax), p);
139 if (p->mem_ctx == NULL) {
140 DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
141 TALLOC_FREE(p);
142 return NULL;
145 if (!init_pipe_handles(p, syntax)) {
146 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
147 TALLOC_FREE(p);
148 return NULL;
151 p->server_info = copy_serverinfo(p, server_info);
152 if (p->server_info == NULL) {
153 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
154 close_policy_by_pipe(p);
155 TALLOC_FREE(p);
156 return NULL;
159 p->msg_ctx = msg_ctx;
161 DLIST_ADD(InternalPipes, p);
163 p->client_id = client_id;
165 p->endian = RPC_LITTLE_ENDIAN;
167 p->syntax = *syntax;
169 DEBUG(4,("Created internal pipe %s (pipes_open=%d)\n",
170 get_pipe_name_from_syntax(talloc_tos(), syntax), pipes_open));
172 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
174 return p;
177 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
178 TALLOC_CTX *mem_ctx,
179 uint32_t opnum,
180 const DATA_BLOB *in_data,
181 DATA_BLOB *out_data)
183 uint32_t num_cmds = rpc_srv_get_pipe_num_cmds(&p->syntax);
184 const struct api_struct *cmds = rpc_srv_get_pipe_cmds(&p->syntax);
185 uint32_t i;
186 bool ok;
188 /* set opnum */
189 p->opnum = opnum;
191 for (i = 0; i < num_cmds; i++) {
192 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
193 break;
197 if (i == num_cmds) {
198 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
201 p->in_data.data = *in_data;
202 p->out_data.rdata = data_blob_null;
204 ok = cmds[i].fn(p);
205 p->in_data.data = data_blob_null;
206 if (!ok) {
207 data_blob_free(&p->out_data.rdata);
208 talloc_free_children(p->mem_ctx);
209 return NT_STATUS_RPC_CALL_FAILED;
212 if (p->fault_state) {
213 p->fault_state = false;
214 data_blob_free(&p->out_data.rdata);
215 talloc_free_children(p->mem_ctx);
216 return NT_STATUS_RPC_CALL_FAILED;
219 if (p->bad_handle_fault_state) {
220 p->bad_handle_fault_state = false;
221 data_blob_free(&p->out_data.rdata);
222 talloc_free_children(p->mem_ctx);
223 return NT_STATUS_RPC_SS_CONTEXT_MISMATCH;
226 if (p->rng_fault_state) {
227 p->rng_fault_state = false;
228 data_blob_free(&p->out_data.rdata);
229 talloc_free_children(p->mem_ctx);
230 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
233 *out_data = p->out_data.rdata;
234 talloc_steal(mem_ctx, out_data->data);
235 p->out_data.rdata = data_blob_null;
237 talloc_free_children(p->mem_ctx);
238 return NT_STATUS_OK;
241 struct rpcint_bh_state {
242 struct pipes_struct *p;
245 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
247 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
248 struct rpcint_bh_state);
250 if (!hs->p) {
251 return false;
254 return true;
257 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
258 uint32_t timeout)
260 /* TODO: implement timeouts */
261 return UINT32_MAX;
264 struct rpcint_bh_raw_call_state {
265 DATA_BLOB in_data;
266 DATA_BLOB out_data;
267 uint32_t out_flags;
270 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
271 struct tevent_context *ev,
272 struct dcerpc_binding_handle *h,
273 const struct GUID *object,
274 uint32_t opnum,
275 uint32_t in_flags,
276 const uint8_t *in_data,
277 size_t in_length)
279 struct rpcint_bh_state *hs =
280 dcerpc_binding_handle_data(h,
281 struct rpcint_bh_state);
282 struct tevent_req *req;
283 struct rpcint_bh_raw_call_state *state;
284 bool ok;
285 NTSTATUS status;
287 req = tevent_req_create(mem_ctx, &state,
288 struct rpcint_bh_raw_call_state);
289 if (req == NULL) {
290 return NULL;
292 state->in_data.data = discard_const_p(uint8_t, in_data);
293 state->in_data.length = in_length;
295 ok = rpcint_bh_is_connected(h);
296 if (!ok) {
297 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
298 return tevent_req_post(req, ev);
301 /* TODO: allow async */
302 status = rpcint_dispatch(hs->p, state, opnum,
303 &state->in_data,
304 &state->out_data);
305 if (!NT_STATUS_IS_OK(status)) {
306 tevent_req_nterror(req, status);
307 return tevent_req_post(req, ev);
310 tevent_req_done(req);
311 return tevent_req_post(req, ev);
314 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
315 TALLOC_CTX *mem_ctx,
316 uint8_t **out_data,
317 size_t *out_length,
318 uint32_t *out_flags)
320 struct rpcint_bh_raw_call_state *state =
321 tevent_req_data(req,
322 struct rpcint_bh_raw_call_state);
323 NTSTATUS status;
325 if (tevent_req_is_nterror(req, &status)) {
326 tevent_req_received(req);
327 return status;
330 *out_data = talloc_move(mem_ctx, &state->out_data.data);
331 *out_length = state->out_data.length;
332 *out_flags = 0;
333 tevent_req_received(req);
334 return NT_STATUS_OK;
337 struct rpcint_bh_disconnect_state {
338 uint8_t _dummy;
341 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
342 struct tevent_context *ev,
343 struct dcerpc_binding_handle *h)
345 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
346 struct rpcint_bh_state);
347 struct tevent_req *req;
348 struct rpcint_bh_disconnect_state *state;
349 bool ok;
351 req = tevent_req_create(mem_ctx, &state,
352 struct rpcint_bh_disconnect_state);
353 if (req == NULL) {
354 return NULL;
357 ok = rpcint_bh_is_connected(h);
358 if (!ok) {
359 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
360 return tevent_req_post(req, ev);
364 * TODO: do a real async disconnect ...
366 * For now the caller needs to free pipes_struct
368 hs->p = NULL;
370 tevent_req_done(req);
371 return tevent_req_post(req, ev);
374 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
376 NTSTATUS status;
378 if (tevent_req_is_nterror(req, &status)) {
379 tevent_req_received(req);
380 return status;
383 tevent_req_received(req);
384 return NT_STATUS_OK;
387 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
389 return true;
392 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
393 int ndr_flags,
394 const void *_struct_ptr,
395 const struct ndr_interface_call *call)
397 void *struct_ptr = discard_const(_struct_ptr);
399 if (DEBUGLEVEL < 10) {
400 return;
403 if (ndr_flags & NDR_IN) {
404 ndr_print_function_debug(call->ndr_print,
405 call->name,
406 ndr_flags,
407 struct_ptr);
409 if (ndr_flags & NDR_OUT) {
410 ndr_print_function_debug(call->ndr_print,
411 call->name,
412 ndr_flags,
413 struct_ptr);
417 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
418 .name = "rpcint",
419 .is_connected = rpcint_bh_is_connected,
420 .set_timeout = rpcint_bh_set_timeout,
421 .raw_call_send = rpcint_bh_raw_call_send,
422 .raw_call_recv = rpcint_bh_raw_call_recv,
423 .disconnect_send = rpcint_bh_disconnect_send,
424 .disconnect_recv = rpcint_bh_disconnect_recv,
426 .ref_alloc = rpcint_bh_ref_alloc,
427 .do_ndr_print = rpcint_bh_do_ndr_print,
430 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
431 const struct ndr_syntax_id *abstract_syntax,
432 const struct ndr_interface_table *ndr_table,
433 struct client_address *client_id,
434 const struct auth_serversupplied_info *server_info,
435 struct messaging_context *msg_ctx,
436 struct dcerpc_binding_handle **binding_handle)
438 struct dcerpc_binding_handle *h;
439 struct rpcint_bh_state *hs;
441 if (ndr_table) {
442 abstract_syntax = &ndr_table->syntax_id;
445 h = dcerpc_binding_handle_create(mem_ctx,
446 &rpcint_bh_ops,
447 NULL,
448 ndr_table,
449 &hs,
450 struct rpcint_bh_state,
451 __location__);
452 if (h == NULL) {
453 return NT_STATUS_NO_MEMORY;
455 hs->p = make_internal_rpc_pipe_p(hs,
456 abstract_syntax,
457 client_id,
458 server_info,
459 msg_ctx);
460 if (hs->p == NULL) {
461 TALLOC_FREE(h);
462 return NT_STATUS_NO_MEMORY;
465 *binding_handle = h;
466 return NT_STATUS_OK;
469 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
471 * @param[in] mem_ctx The memory context to use.
473 * @param[in] ndr_table Normally the ndr_table_<name>.
475 * @param[in] client_id The info about the connected client.
477 * @param[in] serversupplied_info The server supplied authentication function.
479 * @param[in] msg_ctx The messaging context that can be used by the server
481 * @param[out] binding_handle A pointer to store the connected
482 * dcerpc_binding_handle
484 * @return NT_STATUS_OK on success, a corresponding NT status if an
485 * error occured.
487 * @code
488 * struct dcerpc_binding_handle *winreg_binding;
489 * NTSTATUS status;
491 * status = rpcint_binding_handle(tmp_ctx,
492 * &ndr_table_winreg,
493 * p->client_id,
494 * p->server_info,
495 * p->msg_ctx
496 * &winreg_binding);
497 * @endcode
499 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
500 const struct ndr_interface_table *ndr_table,
501 struct client_address *client_id,
502 const struct auth_serversupplied_info *server_info,
503 struct messaging_context *msg_ctx,
504 struct dcerpc_binding_handle **binding_handle)
506 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, client_id,
507 server_info, msg_ctx, binding_handle);
511 * @brief Create a new RPC client context which uses a local dispatch function.
513 * @param[in] mem_ctx The memory context to use.
515 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
516 * ndr_table_<name>.
518 * @param[in] dispatch The corresponding autogenerated dispatch function
519 * rpc_<name>_dispatch.
521 * @param[in] serversupplied_info The server supplied authentication function.
523 * @param[out] presult A pointer to store the connected rpc client pipe.
525 * @return NT_STATUS_OK on success, a corresponding NT status if an
526 * error occured.
528 * @code
529 * struct rpc_pipe_client *winreg_pipe;
530 * NTSTATUS status;
532 * status = rpc_pipe_open_internal(tmp_ctx,
533 * &ndr_table_winreg.syntax_id,
534 * rpc_winreg_dispatch,
535 * p->server_info,
536 * &winreg_pipe);
537 * @endcode
539 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
540 const struct ndr_syntax_id *abstract_syntax,
541 struct auth_serversupplied_info *serversupplied_info,
542 struct client_address *client_id,
543 struct messaging_context *msg_ctx,
544 struct rpc_pipe_client **presult)
546 struct rpc_pipe_client *result;
547 NTSTATUS status;
549 result = TALLOC_ZERO_P(mem_ctx, struct rpc_pipe_client);
550 if (result == NULL) {
551 return NT_STATUS_NO_MEMORY;
554 result->abstract_syntax = *abstract_syntax;
555 result->transfer_syntax = ndr_transfer_syntax;
557 if (client_id == NULL) {
558 static struct client_address unknown;
559 strlcpy(unknown.addr, "<UNKNOWN>", sizeof(unknown.addr));
560 unknown.name = "<UNKNOWN>";
561 client_id = &unknown;
564 result->max_xmit_frag = -1;
565 result->max_recv_frag = -1;
567 status = rpcint_binding_handle_ex(result,
568 abstract_syntax,
569 NULL,
570 client_id,
571 serversupplied_info,
572 msg_ctx,
573 &result->binding_handle);
574 if (!NT_STATUS_IS_OK(status)) {
575 TALLOC_FREE(result);
576 return status;
579 *presult = result;
580 return NT_STATUS_OK;
583 /****************************************************************************
584 * External pipes functions
585 ***************************************************************************/
588 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
589 const char *pipe_name,
590 const struct tsocket_address *local_address,
591 const struct tsocket_address *remote_address,
592 struct auth_serversupplied_info *server_info)
594 struct np_proxy_state *result;
595 char *socket_np_dir;
596 const char *socket_dir;
597 struct tevent_context *ev;
598 struct tevent_req *subreq;
599 struct netr_SamInfo3 *info3;
600 NTSTATUS status;
601 bool ok;
602 int ret;
603 int sys_errno;
605 result = talloc(mem_ctx, struct np_proxy_state);
606 if (result == NULL) {
607 DEBUG(0, ("talloc failed\n"));
608 return NULL;
611 result->read_queue = tevent_queue_create(result, "np_read");
612 if (result->read_queue == NULL) {
613 DEBUG(0, ("tevent_queue_create failed\n"));
614 goto fail;
617 result->write_queue = tevent_queue_create(result, "np_write");
618 if (result->write_queue == NULL) {
619 DEBUG(0, ("tevent_queue_create failed\n"));
620 goto fail;
623 ev = s3_tevent_context_init(talloc_tos());
624 if (ev == NULL) {
625 DEBUG(0, ("s3_tevent_context_init failed\n"));
626 goto fail;
629 socket_dir = lp_parm_const_string(
630 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
631 lp_ncalrpc_dir());
632 if (socket_dir == NULL) {
633 DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
634 goto fail;
636 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
637 if (socket_np_dir == NULL) {
638 DEBUG(0, ("talloc_asprintf failed\n"));
639 goto fail;
642 info3 = talloc_zero(talloc_tos(), struct netr_SamInfo3);
643 if (info3 == NULL) {
644 DEBUG(0, ("talloc failed\n"));
645 goto fail;
648 status = serverinfo_to_SamInfo3(server_info, NULL, 0, info3);
649 if (!NT_STATUS_IS_OK(status)) {
650 TALLOC_FREE(info3);
651 DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
652 nt_errstr(status)));
653 goto fail;
656 become_root();
657 subreq = tstream_npa_connect_send(talloc_tos(), ev,
658 socket_np_dir,
659 pipe_name,
660 remote_address, /* client_addr */
661 NULL, /* client_name */
662 local_address, /* server_addr */
663 NULL, /* server_name */
664 info3,
665 server_info->user_session_key,
666 data_blob_null /* delegated_creds */);
667 if (subreq == NULL) {
668 unbecome_root();
669 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
670 "user %s\\%s failed\n",
671 socket_np_dir, pipe_name, info3->base.domain.string,
672 info3->base.account_name.string));
673 goto fail;
675 ok = tevent_req_poll(subreq, ev);
676 unbecome_root();
677 if (!ok) {
678 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
679 "failed for tstream_npa_connect: %s\n",
680 socket_np_dir, pipe_name, info3->base.domain.string,
681 info3->base.account_name.string,
682 strerror(errno)));
683 goto fail;
686 ret = tstream_npa_connect_recv(subreq, &sys_errno,
687 result,
688 &result->npipe,
689 &result->file_type,
690 &result->device_state,
691 &result->allocation_size);
692 TALLOC_FREE(subreq);
693 if (ret != 0) {
694 DEBUG(0, ("tstream_npa_connect_recv to %s for pipe %s and "
695 "user %s\\%s failed: %s\n",
696 socket_np_dir, pipe_name, info3->base.domain.string,
697 info3->base.account_name.string,
698 strerror(sys_errno)));
699 goto fail;
702 return result;
704 fail:
705 TALLOC_FREE(result);
706 return NULL;
709 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
710 const char *pipe_name,
711 const struct ndr_syntax_id *abstract_syntax,
712 struct auth_serversupplied_info *server_info,
713 struct rpc_pipe_client **_result)
715 struct tsocket_address *local, *remote;
716 struct rpc_pipe_client *result = NULL;
717 struct np_proxy_state *proxy_state = NULL;
718 struct pipe_auth_data *auth;
719 NTSTATUS status;
720 int ret;
722 /* this is an internal connection, fake up ip addresses */
723 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
724 NULL, 0, &local);
725 if (ret) {
726 return NT_STATUS_NO_MEMORY;
728 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
729 NULL, 0, &remote);
730 if (ret) {
731 return NT_STATUS_NO_MEMORY;
734 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
735 local, remote, server_info);
736 if (!proxy_state) {
737 return NT_STATUS_UNSUCCESSFUL;
740 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
741 if (result == NULL) {
742 status = NT_STATUS_NO_MEMORY;
743 goto done;
746 result->abstract_syntax = *abstract_syntax;
747 result->transfer_syntax = ndr_transfer_syntax;
749 result->desthost = get_myname(result);
750 result->srv_name_slash = talloc_asprintf_strupper_m(
751 result, "\\\\%s", result->desthost);
752 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
753 status = NT_STATUS_NO_MEMORY;
754 goto done;
757 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
758 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
760 status = rpc_transport_tstream_init(result,
761 proxy_state->npipe,
762 proxy_state->read_queue,
763 proxy_state->write_queue,
764 &result->transport);
765 if (!NT_STATUS_IS_OK(status)) {
766 goto done;
769 result->auth = talloc_zero(result, struct pipe_auth_data);
770 if (!result->auth) {
771 status = NT_STATUS_NO_MEMORY;
772 goto done;
774 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
775 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
777 status = rpccli_anon_bind_data(result, &auth);
778 if (!NT_STATUS_IS_OK(status)) {
779 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
780 goto done;
783 status = rpc_pipe_bind(result, auth);
784 if (!NT_STATUS_IS_OK(status)) {
785 DEBUG(0, ("Failed to bind spoolss pipe.\n"));
786 goto done;
788 done:
789 if (!NT_STATUS_IS_OK(status)) {
790 TALLOC_FREE(result);
792 TALLOC_FREE(proxy_state);
793 *_result = result;
794 return status;
798 * @brief Create a new RPC client context which uses a local dispatch function.
800 * @param mem_ctx The memory context on which thje pipe will ultimately
801 * be allocated
802 * @param name The pipe name to connect to.
803 * @param server_info Credentials to use for the connection.
804 * @param pipe [in|out] Checks if a pipe is connected, and connects it
805 * if not
807 * @return NT_STATUS_OK on success, a corresponding NT status if
808 * an error occured.
811 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
812 const struct ndr_syntax_id *syntax,
813 struct auth_serversupplied_info *server_info,
814 struct client_address *client_id,
815 struct messaging_context *msg_ctx,
816 struct rpc_pipe_client **cli_pipe)
818 struct rpc_pipe_client *cli = NULL;
819 const char *server_type;
820 const char *pipe_name;
821 NTSTATUS status;
822 TALLOC_CTX *tmp_ctx;
824 if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
825 return NT_STATUS_OK;
826 } else {
827 TALLOC_FREE(*cli_pipe);
830 tmp_ctx = talloc_stackframe();
831 if (tmp_ctx == NULL) {
832 return NT_STATUS_NO_MEMORY;
835 pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
836 if (pipe_name == NULL) {
837 status = NT_STATUS_INVALID_PARAMETER;
838 goto done;
841 DEBUG(10, ("Connecting to %s pipe.\n", pipe_name));
843 server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
844 "rpc_server", pipe_name,
845 "embedded");
846 if (StrCaseCmp(server_type, "embedded") == 0) {
847 status = rpc_pipe_open_internal(tmp_ctx,
848 syntax, server_info,
849 client_id, msg_ctx,
850 &cli);
851 if (!NT_STATUS_IS_OK(status)) {
852 goto done;
854 } else {
855 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
856 * for now we need to use the special proxy setup to connect
857 * to spoolssd. */
859 status = rpc_pipe_open_external(tmp_ctx,
860 pipe_name, syntax,
861 server_info,
862 &cli);
863 if (!NT_STATUS_IS_OK(status)) {
864 goto done;
868 status = NT_STATUS_OK;
869 done:
870 if (NT_STATUS_IS_OK(status)) {
871 *cli_pipe = talloc_move(mem_ctx, &cli);
873 TALLOC_FREE(tmp_ctx);
874 return status;