2 Unix SMB/CIFS implementation.
4 Provide parent->child communication based on NDR marshalling
6 Copyright (C) Volker Lendecke 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * This file implements an RPC between winbind parent and child processes,
24 * leveraging the autogenerated marshalling routines for MSRPC. This is not
25 * MSRPC, as it does not go through the whole DCERPC fragmentation, we just
26 * leverage much the same infrastructure we already have for it.
30 #include "winbindd/winbindd.h"
31 #include "winbindd/winbindd_proto.h"
33 #include "librpc/rpc/dcesrv_core.h"
34 #include "librpc/gen_ndr/ndr_winbind.h"
35 #include "rpc_server/rpc_config.h"
36 #include "rpc_server/rpc_server.h"
38 #include "lib/tsocket/tsocket.h"
40 struct wbint_bh_state
{
41 struct winbindd_domain
*domain
;
42 struct winbindd_child
*child
;
43 const struct dcerpc_binding
*binding
;
46 static const struct dcerpc_binding
*wbint_bh_get_binding(struct dcerpc_binding_handle
*h
)
48 struct wbint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
49 struct wbint_bh_state
);
54 static bool wbint_bh_is_connected(struct dcerpc_binding_handle
*h
)
56 struct wbint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
57 struct wbint_bh_state
);
59 if ((hs
->domain
== NULL
) && (hs
->child
== NULL
)) {
66 static uint32_t wbint_bh_set_timeout(struct dcerpc_binding_handle
*h
,
69 /* TODO: implement timeouts */
73 struct wbint_bh_raw_call_state
{
74 struct winbindd_domain
*domain
;
77 struct winbindd_request request
;
78 struct winbindd_response
*response
;
82 static void wbint_bh_raw_call_child_done(struct tevent_req
*subreq
);
83 static void wbint_bh_raw_call_domain_done(struct tevent_req
*subreq
);
85 static struct tevent_req
*wbint_bh_raw_call_send(TALLOC_CTX
*mem_ctx
,
86 struct tevent_context
*ev
,
87 struct dcerpc_binding_handle
*h
,
88 const struct GUID
*object
,
91 const uint8_t *in_data
,
94 struct wbint_bh_state
*hs
=
95 dcerpc_binding_handle_data(h
,
96 struct wbint_bh_state
);
97 struct tevent_req
*req
;
98 struct wbint_bh_raw_call_state
*state
;
100 struct tevent_req
*subreq
;
102 req
= tevent_req_create(mem_ctx
, &state
,
103 struct wbint_bh_raw_call_state
);
107 state
->domain
= hs
->domain
;
108 state
->opnum
= opnum
;
109 state
->in_data
.data
= discard_const_p(uint8_t, in_data
);
110 state
->in_data
.length
= in_length
;
112 ok
= wbint_bh_is_connected(h
);
114 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
115 return tevent_req_post(req
, ev
);
118 if ((state
->domain
!= NULL
)
119 && wcache_fetch_ndr(state
, state
->domain
, state
->opnum
,
120 &state
->in_data
, &state
->out_data
)) {
121 DBG_DEBUG("Got opnum %"PRIu32
" for domain %s from cache\n",
122 state
->opnum
, state
->domain
->name
);
123 tevent_req_done(req
);
124 return tevent_req_post(req
, ev
);
127 state
->request
.cmd
= WINBINDD_DUAL_NDRCMD
;
128 state
->request
.data
.ndrcmd
= state
->opnum
;
129 state
->request
.extra_data
.data
= (char *)state
->in_data
.data
;
130 state
->request
.extra_len
= state
->in_data
.length
;
132 if (hs
->child
!= NULL
) {
133 subreq
= wb_child_request_send(state
, ev
, hs
->child
,
135 if (tevent_req_nomem(subreq
, req
)) {
136 return tevent_req_post(req
, ev
);
138 tevent_req_set_callback(
139 subreq
, wbint_bh_raw_call_child_done
, req
);
143 subreq
= wb_domain_request_send(state
, ev
, hs
->domain
,
145 if (tevent_req_nomem(subreq
, req
)) {
146 return tevent_req_post(req
, ev
);
148 tevent_req_set_callback(subreq
, wbint_bh_raw_call_domain_done
, req
);
153 static void wbint_bh_raw_call_child_done(struct tevent_req
*subreq
)
155 struct tevent_req
*req
=
156 tevent_req_callback_data(subreq
,
158 struct wbint_bh_raw_call_state
*state
=
160 struct wbint_bh_raw_call_state
);
163 ret
= wb_child_request_recv(subreq
, state
, &state
->response
, &err
);
166 NTSTATUS status
= map_nt_error_from_unix(err
);
167 tevent_req_nterror(req
, status
);
171 state
->out_data
= data_blob_talloc(state
,
172 state
->response
->extra_data
.data
,
173 state
->response
->length
- sizeof(struct winbindd_response
));
174 if (state
->response
->extra_data
.data
&& !state
->out_data
.data
) {
179 if (state
->domain
!= NULL
) {
180 wcache_store_ndr(state
->domain
, state
->opnum
,
181 &state
->in_data
, &state
->out_data
);
184 tevent_req_done(req
);
187 static void wbint_bh_raw_call_domain_done(struct tevent_req
*subreq
)
189 struct tevent_req
*req
=
190 tevent_req_callback_data(subreq
,
192 struct wbint_bh_raw_call_state
*state
=
194 struct wbint_bh_raw_call_state
);
197 ret
= wb_domain_request_recv(subreq
, state
, &state
->response
, &err
);
200 NTSTATUS status
= map_nt_error_from_unix(err
);
201 tevent_req_nterror(req
, status
);
205 state
->out_data
= data_blob_talloc(state
,
206 state
->response
->extra_data
.data
,
207 state
->response
->length
- sizeof(struct winbindd_response
));
208 if (state
->response
->extra_data
.data
&& !state
->out_data
.data
) {
213 if (state
->domain
!= NULL
) {
214 wcache_store_ndr(state
->domain
, state
->opnum
,
215 &state
->in_data
, &state
->out_data
);
218 tevent_req_done(req
);
221 static NTSTATUS
wbint_bh_raw_call_recv(struct tevent_req
*req
,
227 struct wbint_bh_raw_call_state
*state
=
229 struct wbint_bh_raw_call_state
);
232 if (tevent_req_is_nterror(req
, &status
)) {
233 tevent_req_received(req
);
237 *out_data
= talloc_move(mem_ctx
, &state
->out_data
.data
);
238 *out_length
= state
->out_data
.length
;
240 tevent_req_received(req
);
244 struct wbint_bh_disconnect_state
{
248 static struct tevent_req
*wbint_bh_disconnect_send(TALLOC_CTX
*mem_ctx
,
249 struct tevent_context
*ev
,
250 struct dcerpc_binding_handle
*h
)
252 struct wbint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
253 struct wbint_bh_state
);
254 struct tevent_req
*req
;
255 struct wbint_bh_disconnect_state
*state
;
258 req
= tevent_req_create(mem_ctx
, &state
,
259 struct wbint_bh_disconnect_state
);
264 ok
= wbint_bh_is_connected(h
);
266 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
267 return tevent_req_post(req
, ev
);
271 * TODO: do a real async disconnect ...
276 tevent_req_done(req
);
277 return tevent_req_post(req
, ev
);
280 static NTSTATUS
wbint_bh_disconnect_recv(struct tevent_req
*req
)
284 if (tevent_req_is_nterror(req
, &status
)) {
285 tevent_req_received(req
);
289 tevent_req_received(req
);
293 static bool wbint_bh_ref_alloc(struct dcerpc_binding_handle
*h
)
298 static void wbint_bh_do_ndr_print(struct dcerpc_binding_handle
*h
,
299 ndr_flags_type ndr_flags
,
300 const void *_struct_ptr
,
301 const struct ndr_interface_call
*call
)
303 void *struct_ptr
= discard_const(_struct_ptr
);
305 if (DEBUGLEVEL
< 10) {
309 if (ndr_flags
& NDR_IN
) {
310 ndr_print_function_debug(call
->ndr_print
,
315 if (ndr_flags
& NDR_OUT
) {
316 ndr_print_function_debug(call
->ndr_print
,
323 static const struct dcerpc_binding_handle_ops wbint_bh_ops
= {
325 .get_binding
= wbint_bh_get_binding
,
326 .is_connected
= wbint_bh_is_connected
,
327 .set_timeout
= wbint_bh_set_timeout
,
328 .raw_call_send
= wbint_bh_raw_call_send
,
329 .raw_call_recv
= wbint_bh_raw_call_recv
,
330 .disconnect_send
= wbint_bh_disconnect_send
,
331 .disconnect_recv
= wbint_bh_disconnect_recv
,
333 .ref_alloc
= wbint_bh_ref_alloc
,
334 .do_ndr_print
= wbint_bh_do_ndr_print
,
337 static NTSTATUS
make_internal_ncacn_conn(TALLOC_CTX
*mem_ctx
,
338 const struct ndr_interface_table
*table
,
339 struct dcerpc_ncacn_conn
**_out
)
341 struct dcerpc_ncacn_conn
*ncacn_conn
= NULL
;
343 ncacn_conn
= talloc_zero(mem_ctx
, struct dcerpc_ncacn_conn
);
344 if (ncacn_conn
== NULL
) {
345 return NT_STATUS_NO_MEMORY
;
348 ncacn_conn
->p
.mem_ctx
= mem_ctx
;
355 static NTSTATUS
find_ncalrpc_default_endpoint(struct dcesrv_context
*dce_ctx
,
356 struct dcesrv_endpoint
**ep
)
358 TALLOC_CTX
*tmp_ctx
= NULL
;
359 struct dcerpc_binding
*binding
= NULL
;
362 tmp_ctx
= talloc_new(dce_ctx
);
363 if (tmp_ctx
== NULL
) {
364 return NT_STATUS_NO_MEMORY
;
368 * Some services use a rpcint binding handle in their initialization,
369 * before the server is fully initialized. Search the NCALRPC endpoint
370 * with and without endpoint
372 status
= dcerpc_parse_binding(tmp_ctx
, "ncalrpc:", &binding
);
373 if (!NT_STATUS_IS_OK(status
)) {
377 status
= dcesrv_find_endpoint(dce_ctx
, binding
, ep
);
378 if (NT_STATUS_IS_OK(status
)) {
382 status
= dcerpc_parse_binding(tmp_ctx
, "ncalrpc:[WINBIND]", &binding
);
383 if (!NT_STATUS_IS_OK(status
)) {
387 status
= dcesrv_find_endpoint(dce_ctx
, binding
, ep
);
388 if (NT_STATUS_IS_OK(status
)) {
392 status
= dcerpc_parse_binding(tmp_ctx
, "ncalrpc:[DEFAULT]", &binding
);
393 if (!NT_STATUS_IS_OK(status
)) {
397 status
= dcesrv_find_endpoint(dce_ctx
, binding
, ep
);
398 if (!NT_STATUS_IS_OK(status
)) {
403 talloc_free(tmp_ctx
);
407 static NTSTATUS
make_internal_dcesrv_connection(TALLOC_CTX
*mem_ctx
,
408 const struct ndr_interface_table
*ndr_table
,
409 struct dcerpc_ncacn_conn
*ncacn_conn
,
410 struct dcesrv_connection
**_out
)
412 struct dcesrv_connection
*conn
= NULL
;
413 struct dcesrv_connection_context
*context
= NULL
;
414 struct dcesrv_endpoint
*endpoint
= NULL
;
417 conn
= talloc_zero(mem_ctx
, struct dcesrv_connection
);
419 return NT_STATUS_NO_MEMORY
;
421 conn
->dce_ctx
= global_dcesrv_context();
422 conn
->preferred_transfer
= &ndr_transfer_syntax_ndr
;
423 conn
->transport
.private_data
= ncacn_conn
;
425 status
= find_ncalrpc_default_endpoint(conn
->dce_ctx
, &endpoint
);
426 if (!NT_STATUS_IS_OK(status
)) {
429 conn
->endpoint
= endpoint
;
431 conn
->default_auth_state
= talloc_zero(conn
, struct dcesrv_auth
);
432 if (conn
->default_auth_state
== NULL
) {
433 status
= NT_STATUS_NO_MEMORY
;
436 conn
->default_auth_state
->auth_finished
= true;
438 context
= talloc_zero(conn
, struct dcesrv_connection_context
);
439 if (context
== NULL
) {
440 status
= NT_STATUS_NO_MEMORY
;
443 context
->conn
= conn
;
444 context
->context_id
= 0;
445 context
->transfer_syntax
= *(conn
->preferred_transfer
);
446 context
->iface
= find_interface_by_syntax_id(
447 conn
->endpoint
, &ndr_table
->syntax_id
);
448 if (context
->iface
== NULL
) {
449 status
= NT_STATUS_RPC_INTERFACE_NOT_FOUND
;
453 DLIST_ADD(conn
->contexts
, context
);
463 static NTSTATUS
set_remote_addresses(struct dcesrv_connection
*conn
,
466 struct samba_sockaddr ssa
;
467 struct tsocket_address
*remote
= NULL
;
468 struct tsocket_address
*local
= NULL
;
472 ssa
= (struct samba_sockaddr
) { .sa_socklen
= sizeof(ssa
.u
.ss
), };
473 ret
= getpeername(sock
, &ssa
.u
.sa
, &ssa
.sa_socklen
);
475 status
= map_nt_error_from_unix(ret
);
476 DBG_ERR("getpeername failed: %s\n", nt_errstr(status
));
480 ret
= tsocket_address_bsd_from_sockaddr(conn
,
485 status
= map_nt_error_from_unix(ret
);
486 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
491 ssa
= (struct samba_sockaddr
) { .sa_socklen
= sizeof(ssa
.u
.ss
), };
492 ret
= getsockname(sock
, &ssa
.u
.sa
, &ssa
.sa_socklen
);
494 status
= map_nt_error_from_unix(ret
);
495 DBG_ERR("getsockname failed: %s\n", nt_errstr(status
));
499 ret
= tsocket_address_bsd_from_sockaddr(conn
,
504 status
= map_nt_error_from_unix(ret
);
505 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
510 conn
->local_address
= talloc_move(conn
, &local
);
511 conn
->remote_address
= talloc_move(conn
, &remote
);
516 /* initialise a wbint binding handle */
517 struct dcerpc_binding_handle
*wbint_binding_handle(TALLOC_CTX
*mem_ctx
,
518 struct winbindd_domain
*domain
,
519 struct winbindd_child
*child
)
521 struct dcerpc_binding_handle
*h
= NULL
;
522 struct wbint_bh_state
*hs
= NULL
;
523 struct dcerpc_binding
*b
= NULL
;
526 h
= dcerpc_binding_handle_create(mem_ctx
,
531 struct wbint_bh_state
,
539 status
= dcerpc_parse_binding(hs
, "", &b
);
540 if (!NT_STATUS_IS_OK(status
)) {
544 status
= dcerpc_binding_set_transport(b
, NCACN_INTERNAL
);
545 if (!NT_STATUS_IS_OK(status
)) {
549 status
= dcerpc_binding_set_string_option(b
, "host", "localhost");
550 if (!NT_STATUS_IS_OK(status
)) {
554 status
= dcerpc_binding_set_string_option(b
,
556 "winbindd_dual_ndrcmd");
557 if (!NT_STATUS_IS_OK(status
)) {
561 status
= dcerpc_binding_set_abstract_syntax(b
,
562 &ndr_table_winbind
.syntax_id
);
563 if (!NT_STATUS_IS_OK(status
)) {
573 enum winbindd_result
winbindd_dual_ndrcmd(struct winbindd_domain
*domain
,
574 struct winbindd_cli_state
*state
)
576 struct dcerpc_ncacn_conn
*ncacn_conn
= NULL
;
577 struct dcesrv_connection
*dcesrv_conn
= NULL
;
578 struct dcesrv_call_state
*dcesrv_call
= NULL
;
579 struct data_blob_list_item
*rep
= NULL
;
580 struct dcesrv_context_callbacks
*cb
= NULL
;
581 uint32_t opnum
= state
->request
->data
.ndrcmd
;
585 DBG_DEBUG("Running command %s (domain '%s')\n",
586 ndr_table_winbind
.calls
[opnum
].name
,
587 domain
? domain
->name
: "(null)");
589 mem_ctx
= talloc_stackframe();
590 if (mem_ctx
== NULL
) {
591 DBG_ERR("No memory\n");
592 return WINBINDD_ERROR
;
595 status
= make_internal_ncacn_conn(mem_ctx
,
598 if (!NT_STATUS_IS_OK(status
)) {
602 status
= make_internal_dcesrv_connection(ncacn_conn
,
606 if (!NT_STATUS_IS_OK(status
)) {
610 status
= set_remote_addresses(dcesrv_conn
, state
->sock
);
611 if (!NT_STATUS_IS_OK(status
)) {
615 dcesrv_call
= talloc_zero(dcesrv_conn
, struct dcesrv_call_state
);
616 if (dcesrv_call
== NULL
) {
617 status
= NT_STATUS_NO_MEMORY
;
621 dcesrv_call
->conn
= dcesrv_conn
;
622 dcesrv_call
->context
= dcesrv_conn
->contexts
;
623 dcesrv_call
->auth_state
= dcesrv_conn
->default_auth_state
;
625 ZERO_STRUCT(dcesrv_call
->pkt
);
626 dcesrv_call
->pkt
.u
.bind
.assoc_group_id
= 0;
628 cb
= dcesrv_call
->conn
->dce_ctx
->callbacks
;
629 status
= cb
->assoc_group
.find(
630 dcesrv_call
, cb
->assoc_group
.private_data
);
631 if (!NT_STATUS_IS_OK(status
)) {
635 ZERO_STRUCT(dcesrv_call
->pkt
);
636 dcesrv_call
->pkt
.u
.request
.opnum
= opnum
;
637 dcesrv_call
->pkt
.u
.request
.context_id
= 0;
638 dcesrv_call
->pkt
.u
.request
.stub_and_verifier
=
639 data_blob_const(state
->request
->extra_data
.data
,
640 state
->request
->extra_len
);
642 status
= dcesrv_call_dispatch_local(dcesrv_call
);
643 if (!NT_STATUS_IS_OK(status
)) {
647 rep
= dcesrv_call
->replies
;
648 DLIST_REMOVE(dcesrv_call
->replies
, rep
);
650 state
->response
->extra_data
.data
= talloc_steal(state
->mem_ctx
,
652 state
->response
->length
+= rep
->blob
.length
;
657 talloc_free(mem_ctx
);
658 if (NT_STATUS_IS_OK(status
)) {
661 return WINBINDD_ERROR
;