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
; /* if valid also talloc (grant) parent */
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_ref 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 winbindd_domain_ref_set(&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 ((hs
->domain
!= NULL
)
119 && wcache_fetch_ndr(state
, hs
->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
, hs
->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 tevent_req_done(req
);
182 static void wbint_bh_raw_call_domain_done(struct tevent_req
*subreq
)
184 struct tevent_req
*req
=
185 tevent_req_callback_data(subreq
,
187 struct wbint_bh_raw_call_state
*state
=
189 struct wbint_bh_raw_call_state
);
191 struct winbindd_domain
*domain
= NULL
;
194 ret
= wb_domain_request_recv(subreq
, state
, &state
->response
, &err
);
197 NTSTATUS status
= map_nt_error_from_unix(err
);
198 tevent_req_nterror(req
, status
);
202 state
->out_data
= data_blob_talloc(state
,
203 state
->response
->extra_data
.data
,
204 state
->response
->length
- sizeof(struct winbindd_response
));
205 if (state
->response
->extra_data
.data
&& !state
->out_data
.data
) {
210 valid
= winbindd_domain_ref_get(&state
->domain
, &domain
);
213 * winbindd_domain_ref_get() already generated
214 * a debug message for the stale domain!
216 * Just don't fill the cache
219 wcache_store_ndr(domain
, state
->opnum
,
220 &state
->in_data
, &state
->out_data
);
223 tevent_req_done(req
);
226 static NTSTATUS
wbint_bh_raw_call_recv(struct tevent_req
*req
,
232 struct wbint_bh_raw_call_state
*state
=
234 struct wbint_bh_raw_call_state
);
237 if (tevent_req_is_nterror(req
, &status
)) {
238 tevent_req_received(req
);
242 *out_data
= talloc_move(mem_ctx
, &state
->out_data
.data
);
243 *out_length
= state
->out_data
.length
;
245 tevent_req_received(req
);
249 struct wbint_bh_disconnect_state
{
253 static struct tevent_req
*wbint_bh_disconnect_send(TALLOC_CTX
*mem_ctx
,
254 struct tevent_context
*ev
,
255 struct dcerpc_binding_handle
*h
)
257 struct wbint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
258 struct wbint_bh_state
);
259 struct tevent_req
*req
;
260 struct wbint_bh_disconnect_state
*state
;
263 req
= tevent_req_create(mem_ctx
, &state
,
264 struct wbint_bh_disconnect_state
);
269 ok
= wbint_bh_is_connected(h
);
271 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
272 return tevent_req_post(req
, ev
);
276 * TODO: do a real async disconnect ...
281 tevent_req_done(req
);
282 return tevent_req_post(req
, ev
);
285 static NTSTATUS
wbint_bh_disconnect_recv(struct tevent_req
*req
)
289 if (tevent_req_is_nterror(req
, &status
)) {
290 tevent_req_received(req
);
294 tevent_req_received(req
);
298 static bool wbint_bh_ref_alloc(struct dcerpc_binding_handle
*h
)
303 static void wbint_bh_do_ndr_print(struct dcerpc_binding_handle
*h
,
304 ndr_flags_type ndr_flags
,
305 const void *_struct_ptr
,
306 const struct ndr_interface_call
*call
)
308 void *struct_ptr
= discard_const(_struct_ptr
);
310 if (!CHECK_DEBUGLVLC(DBGC_RPC_PARSE
, 10)) {
314 if (ndr_flags
& NDR_IN
) {
315 ndr_print_function_debug(call
->ndr_print
,
320 if (ndr_flags
& NDR_OUT
) {
321 ndr_print_function_debug(call
->ndr_print
,
328 static const struct dcerpc_binding_handle_ops wbint_bh_ops
= {
330 .get_binding
= wbint_bh_get_binding
,
331 .is_connected
= wbint_bh_is_connected
,
332 .set_timeout
= wbint_bh_set_timeout
,
333 .raw_call_send
= wbint_bh_raw_call_send
,
334 .raw_call_recv
= wbint_bh_raw_call_recv
,
335 .disconnect_send
= wbint_bh_disconnect_send
,
336 .disconnect_recv
= wbint_bh_disconnect_recv
,
338 .ref_alloc
= wbint_bh_ref_alloc
,
339 .do_ndr_print
= wbint_bh_do_ndr_print
,
342 static NTSTATUS
make_internal_ncacn_conn(TALLOC_CTX
*mem_ctx
,
343 const struct ndr_interface_table
*table
,
344 struct dcerpc_ncacn_conn
**_out
)
346 struct dcerpc_ncacn_conn
*ncacn_conn
= NULL
;
348 ncacn_conn
= talloc_zero(mem_ctx
, struct dcerpc_ncacn_conn
);
349 if (ncacn_conn
== NULL
) {
350 return NT_STATUS_NO_MEMORY
;
353 ncacn_conn
->p
.mem_ctx
= mem_ctx
;
360 static NTSTATUS
find_ncalrpc_default_endpoint(struct dcesrv_context
*dce_ctx
,
361 struct dcesrv_endpoint
**ep
)
363 TALLOC_CTX
*tmp_ctx
= NULL
;
364 struct dcerpc_binding
*binding
= NULL
;
367 tmp_ctx
= talloc_new(dce_ctx
);
368 if (tmp_ctx
== NULL
) {
369 return NT_STATUS_NO_MEMORY
;
373 * Some services use a rpcint binding handle in their initialization,
374 * before the server is fully initialized. Search the NCALRPC endpoint
375 * with and without endpoint
377 status
= dcerpc_parse_binding(tmp_ctx
, "ncalrpc:", &binding
);
378 if (!NT_STATUS_IS_OK(status
)) {
382 status
= dcesrv_find_endpoint(dce_ctx
, binding
, ep
);
383 if (NT_STATUS_IS_OK(status
)) {
387 status
= dcerpc_parse_binding(tmp_ctx
, "ncalrpc:[WINBIND]", &binding
);
388 if (!NT_STATUS_IS_OK(status
)) {
392 status
= dcesrv_find_endpoint(dce_ctx
, binding
, ep
);
393 if (NT_STATUS_IS_OK(status
)) {
397 status
= dcerpc_parse_binding(tmp_ctx
, "ncalrpc:[DEFAULT]", &binding
);
398 if (!NT_STATUS_IS_OK(status
)) {
402 status
= dcesrv_find_endpoint(dce_ctx
, binding
, ep
);
403 if (!NT_STATUS_IS_OK(status
)) {
408 talloc_free(tmp_ctx
);
412 static NTSTATUS
make_internal_dcesrv_connection(TALLOC_CTX
*mem_ctx
,
413 const struct ndr_interface_table
*ndr_table
,
414 struct dcerpc_ncacn_conn
*ncacn_conn
,
415 struct dcesrv_connection
**_out
)
417 struct dcesrv_connection
*conn
= NULL
;
418 struct dcesrv_connection_context
*context
= NULL
;
419 struct dcesrv_endpoint
*endpoint
= NULL
;
422 conn
= talloc_zero(mem_ctx
, struct dcesrv_connection
);
424 return NT_STATUS_NO_MEMORY
;
426 conn
->dce_ctx
= global_dcesrv_context();
427 conn
->preferred_transfer
= &ndr_transfer_syntax_ndr
;
428 conn
->transport
.private_data
= ncacn_conn
;
430 status
= find_ncalrpc_default_endpoint(conn
->dce_ctx
, &endpoint
);
431 if (!NT_STATUS_IS_OK(status
)) {
434 conn
->endpoint
= endpoint
;
436 conn
->default_auth_state
= talloc_zero(conn
, struct dcesrv_auth
);
437 if (conn
->default_auth_state
== NULL
) {
438 status
= NT_STATUS_NO_MEMORY
;
441 conn
->default_auth_state
->auth_finished
= true;
443 context
= talloc_zero(conn
, struct dcesrv_connection_context
);
444 if (context
== NULL
) {
445 status
= NT_STATUS_NO_MEMORY
;
448 context
->conn
= conn
;
449 context
->context_id
= 0;
450 context
->transfer_syntax
= *(conn
->preferred_transfer
);
451 context
->iface
= find_interface_by_syntax_id(
452 conn
->endpoint
, &ndr_table
->syntax_id
);
453 if (context
->iface
== NULL
) {
454 status
= NT_STATUS_RPC_INTERFACE_NOT_FOUND
;
458 DLIST_ADD(conn
->contexts
, context
);
468 static NTSTATUS
set_remote_addresses(struct dcesrv_connection
*conn
,
471 struct samba_sockaddr ssa
;
472 struct tsocket_address
*remote
= NULL
;
473 struct tsocket_address
*local
= NULL
;
477 ssa
= (struct samba_sockaddr
) { .sa_socklen
= sizeof(ssa
.u
.ss
), };
478 ret
= getpeername(sock
, &ssa
.u
.sa
, &ssa
.sa_socklen
);
480 status
= map_nt_error_from_unix(ret
);
481 DBG_ERR("getpeername failed: %s\n", nt_errstr(status
));
485 ret
= tsocket_address_bsd_from_sockaddr(conn
,
490 status
= map_nt_error_from_unix(ret
);
491 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
496 ssa
= (struct samba_sockaddr
) { .sa_socklen
= sizeof(ssa
.u
.ss
), };
497 ret
= getsockname(sock
, &ssa
.u
.sa
, &ssa
.sa_socklen
);
499 status
= map_nt_error_from_unix(ret
);
500 DBG_ERR("getsockname failed: %s\n", nt_errstr(status
));
504 ret
= tsocket_address_bsd_from_sockaddr(conn
,
509 status
= map_nt_error_from_unix(ret
);
510 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
515 conn
->local_address
= talloc_move(conn
, &local
);
516 conn
->remote_address
= talloc_move(conn
, &remote
);
521 /* initialise a wbint binding handle */
522 struct dcerpc_binding_handle
*wbint_binding_handle(TALLOC_CTX
*mem_ctx
,
523 struct winbindd_domain
*domain
,
524 struct winbindd_child
*child
)
526 struct dcerpc_binding_handle
*h
= NULL
;
527 struct wbint_bh_state
*hs
= NULL
;
528 struct dcerpc_binding
*b
= NULL
;
531 SMB_ASSERT(mem_ctx
!= NULL
);
533 SMB_ASSERT((domain
!= NULL
&& child
== NULL
) ||
534 (domain
== NULL
&& child
!= NULL
));
536 h
= dcerpc_binding_handle_create(mem_ctx
,
541 struct wbint_bh_state
,
549 status
= dcerpc_parse_binding(hs
, "", &b
);
550 if (!NT_STATUS_IS_OK(status
)) {
554 status
= dcerpc_binding_set_transport(b
, NCACN_INTERNAL
);
555 if (!NT_STATUS_IS_OK(status
)) {
559 status
= dcerpc_binding_set_string_option(b
, "host", "localhost");
560 if (!NT_STATUS_IS_OK(status
)) {
564 status
= dcerpc_binding_set_string_option(b
,
566 "winbindd_dual_ndrcmd");
567 if (!NT_STATUS_IS_OK(status
)) {
571 status
= dcerpc_binding_set_abstract_syntax(b
,
572 &ndr_table_winbind
.syntax_id
);
573 if (!NT_STATUS_IS_OK(status
)) {
583 enum winbindd_result
winbindd_dual_ndrcmd(struct winbindd_domain
*domain
,
584 struct winbindd_cli_state
*state
)
586 struct dcerpc_ncacn_conn
*ncacn_conn
= NULL
;
587 struct dcesrv_connection
*dcesrv_conn
= NULL
;
588 struct dcesrv_call_state
*dcesrv_call
= NULL
;
589 struct data_blob_list_item
*rep
= NULL
;
590 struct dcesrv_context_callbacks
*cb
= NULL
;
591 uint32_t opnum
= state
->request
->data
.ndrcmd
;
595 DBG_DEBUG("Running command %s (domain '%s')\n",
596 ndr_table_winbind
.calls
[opnum
].name
,
597 domain
? domain
->name
: "(null)");
599 mem_ctx
= talloc_stackframe();
600 if (mem_ctx
== NULL
) {
601 DBG_ERR("No memory\n");
602 return WINBINDD_ERROR
;
605 status
= make_internal_ncacn_conn(mem_ctx
,
608 if (!NT_STATUS_IS_OK(status
)) {
612 status
= make_internal_dcesrv_connection(ncacn_conn
,
616 if (!NT_STATUS_IS_OK(status
)) {
620 status
= set_remote_addresses(dcesrv_conn
, state
->sock
);
621 if (!NT_STATUS_IS_OK(status
)) {
625 dcesrv_call
= talloc_zero(dcesrv_conn
, struct dcesrv_call_state
);
626 if (dcesrv_call
== NULL
) {
627 status
= NT_STATUS_NO_MEMORY
;
631 dcesrv_call
->conn
= dcesrv_conn
;
632 dcesrv_call
->context
= dcesrv_conn
->contexts
;
633 dcesrv_call
->auth_state
= dcesrv_conn
->default_auth_state
;
635 ZERO_STRUCT(dcesrv_call
->pkt
);
636 dcesrv_call
->pkt
.u
.bind
.assoc_group_id
= 0;
638 cb
= dcesrv_call
->conn
->dce_ctx
->callbacks
;
639 status
= cb
->assoc_group
.find(
640 dcesrv_call
, cb
->assoc_group
.private_data
);
641 if (!NT_STATUS_IS_OK(status
)) {
645 ZERO_STRUCT(dcesrv_call
->pkt
);
646 dcesrv_call
->pkt
.u
.request
.opnum
= opnum
;
647 dcesrv_call
->pkt
.u
.request
.context_id
= 0;
648 dcesrv_call
->pkt
.u
.request
.stub_and_verifier
=
649 data_blob_const(state
->request
->extra_data
.data
,
650 state
->request
->extra_len
);
652 status
= dcesrv_call_dispatch_local(dcesrv_call
);
653 if (!NT_STATUS_IS_OK(status
)) {
657 rep
= dcesrv_call
->replies
;
658 DLIST_REMOVE(dcesrv_call
->replies
, rep
);
660 state
->response
->extra_data
.data
= talloc_steal(state
->mem_ctx
,
662 state
->response
->length
+= rep
->blob
.length
;
667 talloc_free(mem_ctx
);
668 if (NT_STATUS_IS_OK(status
)) {
671 return WINBINDD_ERROR
;