vfs_ceph: fix strict_allocate_ftruncate()
[Samba.git] / source3 / rpc_server / rpc_ncacn_np.c
blob5ee98a10d7094ddb66dec78437d50f13915e7db7
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/auth_util.h"
34 #include "auth.h"
35 #include "rpc_server/rpc_pipes.h"
36 #include "../lib/tsocket/tsocket.h"
37 #include "../lib/util/tevent_ntstatus.h"
38 #include "rpc_contexts.h"
39 #include "rpc_server/rpc_config.h"
40 #include "librpc/ndr/ndr_table.h"
41 #include "rpc_server/rpc_server.h"
43 #undef DBGC_CLASS
44 #define DBGC_CLASS DBGC_RPC_SRV
46 static struct npa_state *npa_state_init(TALLOC_CTX *mem_ctx)
48 struct npa_state *npa;
50 npa = talloc_zero(mem_ctx, struct npa_state);
51 if (npa == NULL) {
52 return NULL;
55 npa->read_queue = tevent_queue_create(npa, "npa_cli_read");
56 if (npa->read_queue == NULL) {
57 DEBUG(0, ("tevent_queue_create failed\n"));
58 goto fail;
61 npa->write_queue = tevent_queue_create(npa, "npa_cli_write");
62 if (npa->write_queue == NULL) {
63 DEBUG(0, ("tevent_queue_create failed\n"));
64 goto fail;
67 return npa;
68 fail:
69 talloc_free(npa);
70 return NULL;
73 NTSTATUS make_internal_rpc_pipe_socketpair(
74 TALLOC_CTX *mem_ctx,
75 struct tevent_context *ev_ctx,
76 struct messaging_context *msg_ctx,
77 const char *pipe_name,
78 const struct ndr_syntax_id *syntax,
79 const struct tsocket_address *remote_address,
80 const struct tsocket_address *local_address,
81 const struct auth_session_info *session_info,
82 struct npa_state **pnpa)
84 TALLOC_CTX *tmp_ctx = talloc_stackframe();
85 struct named_pipe_client *npc;
86 struct tevent_req *subreq;
87 struct npa_state *npa;
88 NTSTATUS status;
89 int error;
90 int rc;
92 DEBUG(4, ("Create of internal pipe %s requested\n", pipe_name));
94 npa = npa_state_init(tmp_ctx);
95 if (npa == NULL) {
96 status = NT_STATUS_NO_MEMORY;
97 goto out;
100 npa->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
101 npa->device_state = 0xff | 0x0400 | 0x0100;
102 npa->allocation_size = 4096;
104 npc = named_pipe_client_init(npa,
105 ev_ctx,
106 msg_ctx,
107 pipe_name,
108 NULL, /* term_fn */
109 npa->file_type,
110 npa->device_state,
111 npa->allocation_size,
112 NULL); /* private_data */
113 if (npc == NULL) {
114 status = NT_STATUS_NO_MEMORY;
115 goto out;
117 npa->private_data = (void*) npc;
119 rc = tstream_npa_socketpair(npa->file_type,
120 npa,
121 &npa->stream,
122 npc,
123 &npc->tstream);
124 if (rc == -1) {
125 status = map_nt_error_from_unix(errno);
126 goto out;
129 npc->remote_client_addr = tsocket_address_copy(remote_address, npc);
130 if (npc->remote_client_addr == NULL) {
131 status = NT_STATUS_NO_MEMORY;
132 goto out;
135 npc->remote_client_name = tsocket_address_inet_addr_string(npc->remote_client_addr,
136 npc);
137 if (npc->remote_client_name == NULL) {
138 status = NT_STATUS_NO_MEMORY;
139 goto out;
142 npc->local_server_addr = tsocket_address_copy(local_address, npc);
143 if (npc->local_server_addr == NULL) {
144 status = NT_STATUS_NO_MEMORY;
145 goto out;
148 npc->local_server_name = tsocket_address_inet_addr_string(
149 npc->local_server_addr, npc);
150 if (npc->local_server_name == NULL) {
151 status = NT_STATUS_NO_MEMORY;
152 goto out;
155 npc->session_info = copy_session_info(npc, session_info);
156 if (npc->session_info == NULL) {
157 status = NT_STATUS_NO_MEMORY;
158 goto out;
161 rc = make_server_pipes_struct(npc,
162 npc->msg_ctx,
163 npc->pipe_name,
164 NCACN_NP,
165 npc->remote_client_addr,
166 npc->local_server_addr,
167 npc->session_info,
168 &npc->p,
169 &error);
170 if (rc == -1) {
171 status = map_nt_error_from_unix(error);
172 goto out;
175 npc->write_queue = tevent_queue_create(npc, "npa_server_write_queue");
176 if (npc->write_queue == NULL) {
177 status = NT_STATUS_NO_MEMORY;
178 goto out;
181 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
182 if (subreq == NULL) {
183 DEBUG(2, ("Failed to start receiving packets\n"));
184 status = NT_STATUS_PIPE_BROKEN;
185 goto out;
187 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
189 *pnpa = talloc_steal(mem_ctx, npa);
190 status = NT_STATUS_OK;
191 out:
192 talloc_free(tmp_ctx);
193 return status;
196 /****************************************************************************
197 Make an internal namedpipes structure
198 ****************************************************************************/
200 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
201 const struct ndr_syntax_id *syntax,
202 const struct tsocket_address *remote_address,
203 const struct tsocket_address *local_address,
204 const struct auth_session_info *session_info,
205 struct messaging_context *msg_ctx)
207 struct pipes_struct *p;
208 struct pipe_rpc_fns *context_fns;
209 const char *pipe_name;
210 int ret;
211 const struct ndr_interface_table *table;
213 table = ndr_table_by_uuid(&syntax->uuid);
214 if (table == NULL) {
215 DEBUG(0,("unknown interface\n"));
216 return NULL;
219 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
221 DEBUG(4,("Create pipe requested %s\n", pipe_name));
223 ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
224 NCALRPC, RPC_LITTLE_ENDIAN,
225 remote_address, local_address, &p);
226 if (ret) {
227 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
228 return NULL;
231 if (!init_pipe_handles(p, syntax)) {
232 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
233 TALLOC_FREE(p);
234 return NULL;
237 p->session_info = copy_session_info(p, session_info);
238 if (p->session_info == NULL) {
239 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
240 close_policy_by_pipe(p);
241 TALLOC_FREE(p);
242 return NULL;
245 context_fns = talloc_zero(p, struct pipe_rpc_fns);
246 if (context_fns == NULL) {
247 DEBUG(0,("talloc() failed!\n"));
248 TALLOC_FREE(p);
249 return NULL;
252 context_fns->next = context_fns->prev = NULL;
253 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
254 context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
255 context_fns->context_id = 0;
256 context_fns->syntax = *syntax;
258 /* add to the list of open contexts */
259 DLIST_ADD(p->contexts, context_fns);
261 DEBUG(4,("Created internal pipe %s\n", pipe_name));
263 return p;
266 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
267 TALLOC_CTX *mem_ctx,
268 uint32_t opnum,
269 const DATA_BLOB *in_data,
270 DATA_BLOB *out_data)
272 struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
273 uint32_t num_cmds = fns->n_cmds;
274 const struct api_struct *cmds = fns->cmds;
275 uint32_t i;
276 bool ok;
278 /* set opnum */
279 p->opnum = opnum;
281 for (i = 0; i < num_cmds; i++) {
282 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
283 break;
287 if (i == num_cmds) {
288 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
291 p->in_data.data = *in_data;
292 p->out_data.rdata = data_blob_null;
294 ok = cmds[i].fn(p);
295 p->in_data.data = data_blob_null;
296 if (!ok) {
297 data_blob_free(&p->out_data.rdata);
298 talloc_free_children(p->mem_ctx);
299 return NT_STATUS_RPC_CALL_FAILED;
302 if (p->fault_state) {
303 NTSTATUS status;
305 status = NT_STATUS(p->fault_state);
306 p->fault_state = 0;
307 data_blob_free(&p->out_data.rdata);
308 talloc_free_children(p->mem_ctx);
309 return status;
312 *out_data = p->out_data.rdata;
313 talloc_steal(mem_ctx, out_data->data);
314 p->out_data.rdata = data_blob_null;
316 talloc_free_children(p->mem_ctx);
317 return NT_STATUS_OK;
320 struct rpcint_bh_state {
321 struct pipes_struct *p;
324 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
326 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
327 struct rpcint_bh_state);
329 if (!hs->p) {
330 return false;
333 return true;
336 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
337 uint32_t timeout)
339 /* TODO: implement timeouts */
340 return UINT32_MAX;
343 struct rpcint_bh_raw_call_state {
344 DATA_BLOB in_data;
345 DATA_BLOB out_data;
346 uint32_t out_flags;
349 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
350 struct tevent_context *ev,
351 struct dcerpc_binding_handle *h,
352 const struct GUID *object,
353 uint32_t opnum,
354 uint32_t in_flags,
355 const uint8_t *in_data,
356 size_t in_length)
358 struct rpcint_bh_state *hs =
359 dcerpc_binding_handle_data(h,
360 struct rpcint_bh_state);
361 struct tevent_req *req;
362 struct rpcint_bh_raw_call_state *state;
363 bool ok;
364 NTSTATUS status;
366 req = tevent_req_create(mem_ctx, &state,
367 struct rpcint_bh_raw_call_state);
368 if (req == NULL) {
369 return NULL;
371 state->in_data.data = discard_const_p(uint8_t, in_data);
372 state->in_data.length = in_length;
374 ok = rpcint_bh_is_connected(h);
375 if (!ok) {
376 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
377 return tevent_req_post(req, ev);
380 /* TODO: allow async */
381 status = rpcint_dispatch(hs->p, state, opnum,
382 &state->in_data,
383 &state->out_data);
384 if (!NT_STATUS_IS_OK(status)) {
385 tevent_req_nterror(req, status);
386 return tevent_req_post(req, ev);
389 tevent_req_done(req);
390 return tevent_req_post(req, ev);
393 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
394 TALLOC_CTX *mem_ctx,
395 uint8_t **out_data,
396 size_t *out_length,
397 uint32_t *out_flags)
399 struct rpcint_bh_raw_call_state *state =
400 tevent_req_data(req,
401 struct rpcint_bh_raw_call_state);
402 NTSTATUS status;
404 if (tevent_req_is_nterror(req, &status)) {
405 tevent_req_received(req);
406 return status;
409 *out_data = talloc_move(mem_ctx, &state->out_data.data);
410 *out_length = state->out_data.length;
411 *out_flags = 0;
412 tevent_req_received(req);
413 return NT_STATUS_OK;
416 struct rpcint_bh_disconnect_state {
417 uint8_t _dummy;
420 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
421 struct tevent_context *ev,
422 struct dcerpc_binding_handle *h)
424 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
425 struct rpcint_bh_state);
426 struct tevent_req *req;
427 struct rpcint_bh_disconnect_state *state;
428 bool ok;
430 req = tevent_req_create(mem_ctx, &state,
431 struct rpcint_bh_disconnect_state);
432 if (req == NULL) {
433 return NULL;
436 ok = rpcint_bh_is_connected(h);
437 if (!ok) {
438 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
439 return tevent_req_post(req, ev);
443 * TODO: do a real async disconnect ...
445 * For now the caller needs to free pipes_struct
447 hs->p = NULL;
449 tevent_req_done(req);
450 return tevent_req_post(req, ev);
453 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
455 NTSTATUS status;
457 if (tevent_req_is_nterror(req, &status)) {
458 tevent_req_received(req);
459 return status;
462 tevent_req_received(req);
463 return NT_STATUS_OK;
466 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
468 return true;
471 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
472 int ndr_flags,
473 const void *_struct_ptr,
474 const struct ndr_interface_call *call)
476 void *struct_ptr = discard_const(_struct_ptr);
478 if (DEBUGLEVEL < 11) {
479 return;
482 if (ndr_flags & NDR_IN) {
483 ndr_print_function_debug(call->ndr_print,
484 call->name,
485 ndr_flags,
486 struct_ptr);
488 if (ndr_flags & NDR_OUT) {
489 ndr_print_function_debug(call->ndr_print,
490 call->name,
491 ndr_flags,
492 struct_ptr);
496 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
497 .name = "rpcint",
498 .is_connected = rpcint_bh_is_connected,
499 .set_timeout = rpcint_bh_set_timeout,
500 .raw_call_send = rpcint_bh_raw_call_send,
501 .raw_call_recv = rpcint_bh_raw_call_recv,
502 .disconnect_send = rpcint_bh_disconnect_send,
503 .disconnect_recv = rpcint_bh_disconnect_recv,
505 .ref_alloc = rpcint_bh_ref_alloc,
506 .do_ndr_print = rpcint_bh_do_ndr_print,
509 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
510 const struct ndr_syntax_id *abstract_syntax,
511 const struct ndr_interface_table *ndr_table,
512 const struct tsocket_address *remote_address,
513 const struct tsocket_address *local_address,
514 const struct auth_session_info *session_info,
515 struct messaging_context *msg_ctx,
516 struct dcerpc_binding_handle **binding_handle)
518 struct dcerpc_binding_handle *h;
519 struct rpcint_bh_state *hs;
521 if (ndr_table) {
522 abstract_syntax = &ndr_table->syntax_id;
525 h = dcerpc_binding_handle_create(mem_ctx,
526 &rpcint_bh_ops,
527 NULL,
528 ndr_table,
529 &hs,
530 struct rpcint_bh_state,
531 __location__);
532 if (h == NULL) {
533 return NT_STATUS_NO_MEMORY;
535 hs->p = make_internal_rpc_pipe_p(hs,
536 abstract_syntax,
537 remote_address,
538 local_address,
539 session_info,
540 msg_ctx);
541 if (hs->p == NULL) {
542 TALLOC_FREE(h);
543 return NT_STATUS_NO_MEMORY;
546 *binding_handle = h;
547 return NT_STATUS_OK;
550 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
552 * @param[in] mem_ctx The memory context to use.
554 * @param[in] ndr_table Normally the ndr_table_<name>.
556 * @param[in] remote_address The info about the connected client.
558 * @param[in] serversupplied_info The server supplied authentication function.
560 * @param[in] msg_ctx The messaging context that can be used by the server
562 * @param[out] binding_handle A pointer to store the connected
563 * dcerpc_binding_handle
565 * @return NT_STATUS_OK on success, a corresponding NT status if an
566 * error occurred.
568 * @code
569 * struct dcerpc_binding_handle *winreg_binding;
570 * NTSTATUS status;
572 * status = rpcint_binding_handle(tmp_ctx,
573 * &ndr_table_winreg,
574 * p->remote_address,
575 * p->session_info,
576 * p->msg_ctx
577 * &winreg_binding);
578 * @endcode
580 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
581 const struct ndr_interface_table *ndr_table,
582 const struct tsocket_address *remote_address,
583 const struct tsocket_address *local_address,
584 const struct auth_session_info *session_info,
585 struct messaging_context *msg_ctx,
586 struct dcerpc_binding_handle **binding_handle)
588 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
589 local_address, session_info,
590 msg_ctx, binding_handle);
594 * @internal
596 * @brief Create a new RPC client context which uses a local transport.
598 * This creates a local transport. It is a shortcut to directly call the server
599 * functions and avoid marshalling.
600 * NOTE: this function should be used only by rpc_pipe_open_interface()
602 * @param[in] mem_ctx The memory context to use.
604 * @param[in] ndr_table the ndr_table_<name> structure.
606 * @param[in] serversupplied_info The server supplied authentication function.
608 * @param[in] remote_address The client address information.
610 * @param[in] msg_ctx The messaging context to use.
612 * @param[out] presult A pointer to store the connected rpc client pipe.
614 * @return NT_STATUS_OK on success, a corresponding NT status if an
615 * error occurred.
617 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
618 const struct ndr_interface_table *ndr_table,
619 const struct auth_session_info *session_info,
620 const struct tsocket_address *remote_address,
621 const struct tsocket_address *local_address,
622 struct messaging_context *msg_ctx,
623 struct rpc_pipe_client **presult)
625 struct rpc_pipe_client *result;
626 NTSTATUS status;
628 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
629 if (result == NULL) {
630 return NT_STATUS_NO_MEMORY;
633 result->abstract_syntax = ndr_table->syntax_id;
634 result->transfer_syntax = ndr_transfer_syntax_ndr;
636 if (remote_address == NULL) {
637 struct tsocket_address *local;
638 int rc;
640 rc = tsocket_address_inet_from_strings(mem_ctx,
641 "ip",
642 "127.0.0.1",
644 &local);
645 if (rc < 0) {
646 TALLOC_FREE(result);
647 return NT_STATUS_NO_MEMORY;
650 remote_address = local;
653 result->max_xmit_frag = -1;
655 status = rpcint_binding_handle(result,
656 ndr_table,
657 remote_address,
658 local_address,
659 session_info,
660 msg_ctx,
661 &result->binding_handle);
662 if (!NT_STATUS_IS_OK(status)) {
663 TALLOC_FREE(result);
664 return status;
667 *presult = result;
668 return NT_STATUS_OK;
671 /****************************************************************************
672 * External pipes functions
673 ***************************************************************************/
675 NTSTATUS make_external_rpc_pipe(TALLOC_CTX *mem_ctx,
676 const char *pipe_name,
677 const struct tsocket_address *remote_client_address,
678 const struct tsocket_address *local_server_address,
679 const struct auth_session_info *session_info,
680 struct npa_state **pnpa)
682 TALLOC_CTX *tmp_ctx = talloc_stackframe();
683 struct auth_session_info_transport *session_info_t;
684 struct tevent_context *ev_ctx;
685 struct tevent_req *subreq;
686 const char *socket_np_dir;
687 const char *socket_dir;
688 struct npa_state *npa;
689 int sys_errno;
690 NTSTATUS status;
691 int rc = -1;
692 bool ok;
694 npa = npa_state_init(tmp_ctx);
695 if (npa == NULL) {
696 status = NT_STATUS_NO_MEMORY;
697 goto out;
700 socket_dir = lp_parm_const_string(GLOBAL_SECTION_SNUM,
701 "external_rpc_pipe",
702 "socket_dir",
703 lp_ncalrpc_dir());
704 if (socket_dir == NULL) {
705 DEBUG(0, ("external_rpc_pipe: socket_dir not set\n"));
706 status = NT_STATUS_PIPE_NOT_AVAILABLE;
707 goto out;
710 socket_np_dir = talloc_asprintf(tmp_ctx, "%s/np", socket_dir);
711 if (socket_np_dir == NULL) {
712 DEBUG(0, ("talloc_asprintf failed\n"));
713 status = NT_STATUS_NO_MEMORY;
714 goto out;
717 session_info_t = talloc_zero(tmp_ctx,
718 struct auth_session_info_transport);
719 if (session_info_t == NULL) {
720 DEBUG(0, ("talloc failed\n"));
721 status = NT_STATUS_NO_MEMORY;
722 goto out;
725 session_info_t->session_info = copy_session_info(session_info_t,
726 session_info);
727 if (session_info_t->session_info == NULL) {
728 DEBUG(0, ("copy_session_info failed\n"));
729 status = NT_STATUS_NO_MEMORY;
730 goto out;
733 ev_ctx = samba_tevent_context_init(tmp_ctx);
734 if (ev_ctx == NULL) {
735 DEBUG(0, ("samba_tevent_context_init failed\n"));
736 status = NT_STATUS_NO_MEMORY;
737 goto out;
740 become_root();
741 subreq = tstream_npa_connect_send(tmp_ctx,
742 ev_ctx,
743 socket_np_dir,
744 pipe_name,
745 remote_client_address,
746 NULL, /* client_name */
747 local_server_address,
748 NULL, /* server_name */
749 session_info_t);
750 if (subreq == NULL) {
751 unbecome_root();
752 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
753 "user %s\\%s failed\n",
754 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
755 session_info_t->session_info->info->account_name));
756 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
757 goto out;
759 ok = tevent_req_poll(subreq, ev_ctx);
760 unbecome_root();
761 if (!ok) {
762 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
763 "failed for tstream_npa_connect: %s\n",
764 socket_np_dir,
765 pipe_name,
766 session_info_t->session_info->info->domain_name,
767 session_info_t->session_info->info->account_name,
768 strerror(errno)));
769 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
770 goto out;
773 rc = tstream_npa_connect_recv(subreq,
774 &sys_errno,
775 npa,
776 &npa->stream,
777 &npa->file_type,
778 &npa->device_state,
779 &npa->allocation_size);
780 talloc_free(subreq);
781 if (rc != 0) {
782 int l = 1;
784 if (errno == ENOENT) {
785 l = 2;
788 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
789 "user %s\\%s failed: %s\n",
790 socket_np_dir,
791 pipe_name,
792 session_info_t->session_info->info->domain_name,
793 session_info_t->session_info->info->account_name,
794 strerror(sys_errno)));
795 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
796 goto out;
799 *pnpa = talloc_steal(mem_ctx, npa);
800 status = NT_STATUS_OK;
801 out:
802 talloc_free(tmp_ctx);
804 return status;
807 struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
808 const char *pipe_name,
809 const struct tsocket_address *remote_client_address,
810 const struct tsocket_address *local_server_address,
811 const struct auth_session_info *session_info)
813 struct np_proxy_state *result;
814 char *socket_np_dir;
815 const char *socket_dir;
816 struct tevent_context *ev;
817 struct tevent_req *subreq;
818 struct auth_session_info_transport *session_info_t;
819 bool ok;
820 int ret;
821 int sys_errno;
823 result = talloc(mem_ctx, struct np_proxy_state);
824 if (result == NULL) {
825 DEBUG(0, ("talloc failed\n"));
826 return NULL;
829 result->read_queue = tevent_queue_create(result, "np_read");
830 if (result->read_queue == NULL) {
831 DEBUG(0, ("tevent_queue_create failed\n"));
832 goto fail;
835 result->write_queue = tevent_queue_create(result, "np_write");
836 if (result->write_queue == NULL) {
837 DEBUG(0, ("tevent_queue_create failed\n"));
838 goto fail;
841 ev = samba_tevent_context_init(talloc_tos());
842 if (ev == NULL) {
843 DEBUG(0, ("samba_tevent_context_init failed\n"));
844 goto fail;
847 socket_dir = lp_parm_const_string(
848 GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
849 lp_ncalrpc_dir());
850 if (socket_dir == NULL) {
851 DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
852 goto fail;
854 socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
855 if (socket_np_dir == NULL) {
856 DEBUG(0, ("talloc_asprintf failed\n"));
857 goto fail;
860 session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
861 if (session_info_t == NULL) {
862 DEBUG(0, ("talloc failed\n"));
863 goto fail;
866 session_info_t->session_info = copy_session_info(session_info_t,
867 session_info);
868 if (session_info_t->session_info == NULL) {
869 DEBUG(0, ("copy_session_info failed\n"));
870 goto fail;
873 become_root();
874 subreq = tstream_npa_connect_send(talloc_tos(), ev,
875 socket_np_dir,
876 pipe_name,
877 remote_client_address,
878 NULL, /* client_name */
879 local_server_address,
880 NULL, /* server_name */
881 session_info_t);
882 if (subreq == NULL) {
883 unbecome_root();
884 DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
885 "user %s\\%s failed\n",
886 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
887 session_info_t->session_info->info->account_name));
888 goto fail;
890 ok = tevent_req_poll(subreq, ev);
891 unbecome_root();
892 if (!ok) {
893 DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
894 "failed for tstream_npa_connect: %s\n",
895 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
896 session_info_t->session_info->info->account_name,
897 strerror(errno)));
898 goto fail;
901 ret = tstream_npa_connect_recv(subreq, &sys_errno,
902 result,
903 &result->npipe,
904 &result->file_type,
905 &result->device_state,
906 &result->allocation_size);
907 TALLOC_FREE(subreq);
908 if (ret != 0) {
909 int l = 1;
910 if (errno == ENOENT) {
911 l = 2;
913 DEBUG(l, ("tstream_npa_connect_recv to %s for pipe %s and "
914 "user %s\\%s failed: %s\n",
915 socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
916 session_info_t->session_info->info->account_name,
917 strerror(sys_errno)));
918 goto fail;
921 return result;
923 fail:
924 TALLOC_FREE(result);
925 return NULL;
928 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
929 const char *pipe_name,
930 const struct ndr_interface_table *table,
931 const struct auth_session_info *session_info,
932 const struct tsocket_address *remote_client_address,
933 const struct tsocket_address *local_server_address,
934 struct rpc_pipe_client **_result)
936 struct rpc_pipe_client *result = NULL;
937 struct np_proxy_state *proxy_state = NULL;
938 struct pipe_auth_data *auth;
939 struct tsocket_address *remote_client_addr;
940 struct tsocket_address *local_server_addr;
941 NTSTATUS status;
942 int ret;
944 if (local_server_address == NULL) {
945 /* this is an internal connection, fake up ip addresses */
946 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
947 NULL, 0, &local_server_addr);
948 if (ret) {
949 return NT_STATUS_NO_MEMORY;
951 local_server_address = local_server_addr;
954 if (remote_client_address == NULL) {
955 /* this is an internal connection, fake up ip addresses */
956 ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
957 NULL, 0, &remote_client_addr);
958 if (ret) {
959 return NT_STATUS_NO_MEMORY;
961 remote_client_address = remote_client_addr;
964 proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
965 remote_client_address,
966 local_server_address,
967 session_info);
968 if (!proxy_state) {
969 DEBUG(1, ("Unable to make proxy_state for connection to %s.\n", pipe_name));
970 return NT_STATUS_UNSUCCESSFUL;
973 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
974 if (result == NULL) {
975 status = NT_STATUS_NO_MEMORY;
976 goto done;
979 result->abstract_syntax = table->syntax_id;
980 result->transfer_syntax = ndr_transfer_syntax_ndr;
982 result->desthost = get_myname(result);
983 result->srv_name_slash = talloc_asprintf_strupper_m(
984 result, "\\\\%s", result->desthost);
985 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
986 status = NT_STATUS_NO_MEMORY;
987 goto done;
990 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
992 status = rpc_transport_tstream_init(result,
993 &proxy_state->npipe,
994 &result->transport);
995 if (!NT_STATUS_IS_OK(status)) {
996 goto done;
999 result->binding_handle = rpccli_bh_create(result, NULL, table);
1000 if (result->binding_handle == NULL) {
1001 status = NT_STATUS_NO_MEMORY;
1002 DEBUG(0, ("Failed to create binding handle.\n"));
1003 goto done;
1006 result->auth = talloc_zero(result, struct pipe_auth_data);
1007 if (!result->auth) {
1008 status = NT_STATUS_NO_MEMORY;
1009 goto done;
1011 result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
1012 result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
1013 result->auth->auth_context_id = 0;
1015 status = rpccli_anon_bind_data(result, &auth);
1016 if (!NT_STATUS_IS_OK(status)) {
1017 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
1018 goto done;
1021 status = rpc_pipe_bind(result, auth);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 DEBUG(0, ("Failed to bind external pipe.\n"));
1024 goto done;
1027 done:
1028 if (!NT_STATUS_IS_OK(status)) {
1029 TALLOC_FREE(result);
1031 TALLOC_FREE(proxy_state);
1032 *_result = result;
1033 return status;
1037 * @brief Create a new RPC client context which uses a local dispatch function
1038 * or a remote transport, depending on rpc_server configuration for the
1039 * specific service.
1041 * @param[in] mem_ctx The memory context to use.
1043 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
1044 * ndr_table_<name>.
1046 * @param[in] serversupplied_info The server supplied authentication function.
1048 * @param[in] remote_address The client address information.
1050 * @param[in] msg_ctx The messaging context to use.
1052 * @param[out] presult A pointer to store the connected rpc client pipe.
1054 * @return NT_STATUS_OK on success, a corresponding NT status if an
1055 * error occurred.
1057 * @code
1058 * struct rpc_pipe_client *winreg_pipe;
1059 * NTSTATUS status;
1061 * status = rpc_pipe_open_interface(tmp_ctx,
1062 * &ndr_table_winreg.syntax_id,
1063 * p->session_info,
1064 * remote_address,
1065 * &winreg_pipe);
1066 * @endcode
1069 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
1070 const struct ndr_interface_table *table,
1071 const struct auth_session_info *session_info,
1072 const struct tsocket_address *remote_address,
1073 const struct tsocket_address *local_address,
1074 struct messaging_context *msg_ctx,
1075 struct rpc_pipe_client **cli_pipe)
1077 struct rpc_pipe_client *cli = NULL;
1078 enum rpc_service_mode_e pipe_mode;
1079 const char *pipe_name;
1080 NTSTATUS status;
1081 TALLOC_CTX *tmp_ctx;
1083 if (cli_pipe != NULL) {
1084 if (rpccli_is_connected(*cli_pipe)) {
1085 return NT_STATUS_OK;
1086 } else {
1087 TALLOC_FREE(*cli_pipe);
1091 tmp_ctx = talloc_stackframe();
1092 if (tmp_ctx == NULL) {
1093 return NT_STATUS_NO_MEMORY;
1096 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
1097 if (pipe_name == NULL) {
1098 DEBUG(1, ("Unable to find pipe name to forward %s to.\n", table->name));
1099 status = NT_STATUS_INVALID_PARAMETER;
1100 goto done;
1103 while (pipe_name[0] == '\\') {
1104 pipe_name++;
1107 DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
1109 pipe_mode = rpc_service_mode(pipe_name);
1111 switch (pipe_mode) {
1112 case RPC_SERVICE_MODE_EMBEDDED:
1113 status = rpc_pipe_open_internal(tmp_ctx,
1114 table, session_info,
1115 remote_address, local_address,
1116 msg_ctx,
1117 &cli);
1118 if (!NT_STATUS_IS_OK(status)) {
1119 goto done;
1121 break;
1122 case RPC_SERVICE_MODE_EXTERNAL:
1123 /* It would be nice to just use rpc_pipe_open_ncalrpc() but
1124 * for now we need to use the special proxy setup to connect
1125 * to spoolssd. */
1127 status = rpc_pipe_open_external(tmp_ctx,
1128 pipe_name, table,
1129 session_info,
1130 remote_address, local_address,
1131 &cli);
1132 if (!NT_STATUS_IS_OK(status)) {
1133 goto done;
1135 break;
1136 case RPC_SERVICE_MODE_DISABLED:
1137 status = NT_STATUS_NOT_IMPLEMENTED;
1138 DEBUG(0, ("Service pipe %s is disabled in config file: %s",
1139 pipe_name, nt_errstr(status)));
1140 goto done;
1143 status = NT_STATUS_OK;
1144 done:
1145 if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
1146 *cli_pipe = talloc_move(mem_ctx, &cli);
1148 TALLOC_FREE(tmp_ctx);
1149 return status;