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
;
45 static bool wbint_bh_is_connected(struct dcerpc_binding_handle
*h
)
47 struct wbint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
48 struct wbint_bh_state
);
50 if ((hs
->domain
== NULL
) && (hs
->child
== NULL
)) {
57 static uint32_t wbint_bh_set_timeout(struct dcerpc_binding_handle
*h
,
60 /* TODO: implement timeouts */
64 struct wbint_bh_raw_call_state
{
65 struct winbindd_domain
*domain
;
68 struct winbindd_request request
;
69 struct winbindd_response
*response
;
73 static void wbint_bh_raw_call_child_done(struct tevent_req
*subreq
);
74 static void wbint_bh_raw_call_domain_done(struct tevent_req
*subreq
);
76 static struct tevent_req
*wbint_bh_raw_call_send(TALLOC_CTX
*mem_ctx
,
77 struct tevent_context
*ev
,
78 struct dcerpc_binding_handle
*h
,
79 const struct GUID
*object
,
82 const uint8_t *in_data
,
85 struct wbint_bh_state
*hs
=
86 dcerpc_binding_handle_data(h
,
87 struct wbint_bh_state
);
88 struct tevent_req
*req
;
89 struct wbint_bh_raw_call_state
*state
;
91 struct tevent_req
*subreq
;
93 req
= tevent_req_create(mem_ctx
, &state
,
94 struct wbint_bh_raw_call_state
);
98 state
->domain
= hs
->domain
;
100 state
->in_data
.data
= discard_const_p(uint8_t, in_data
);
101 state
->in_data
.length
= in_length
;
103 ok
= wbint_bh_is_connected(h
);
105 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
106 return tevent_req_post(req
, ev
);
109 if ((state
->domain
!= NULL
)
110 && wcache_fetch_ndr(state
, state
->domain
, state
->opnum
,
111 &state
->in_data
, &state
->out_data
)) {
112 DBG_DEBUG("Got opnum %"PRIu32
" for domain %s from cache\n",
113 state
->opnum
, state
->domain
->name
);
114 tevent_req_done(req
);
115 return tevent_req_post(req
, ev
);
118 state
->request
.cmd
= WINBINDD_DUAL_NDRCMD
;
119 state
->request
.data
.ndrcmd
= state
->opnum
;
120 state
->request
.extra_data
.data
= (char *)state
->in_data
.data
;
121 state
->request
.extra_len
= state
->in_data
.length
;
123 if (hs
->child
!= NULL
) {
124 subreq
= wb_child_request_send(state
, ev
, hs
->child
,
126 if (tevent_req_nomem(subreq
, req
)) {
127 return tevent_req_post(req
, ev
);
129 tevent_req_set_callback(
130 subreq
, wbint_bh_raw_call_child_done
, req
);
134 subreq
= wb_domain_request_send(state
, ev
, hs
->domain
,
136 if (tevent_req_nomem(subreq
, req
)) {
137 return tevent_req_post(req
, ev
);
139 tevent_req_set_callback(subreq
, wbint_bh_raw_call_domain_done
, req
);
144 static void wbint_bh_raw_call_child_done(struct tevent_req
*subreq
)
146 struct tevent_req
*req
=
147 tevent_req_callback_data(subreq
,
149 struct wbint_bh_raw_call_state
*state
=
151 struct wbint_bh_raw_call_state
);
154 ret
= wb_child_request_recv(subreq
, state
, &state
->response
, &err
);
157 NTSTATUS status
= map_nt_error_from_unix(err
);
158 tevent_req_nterror(req
, status
);
162 state
->out_data
= data_blob_talloc(state
,
163 state
->response
->extra_data
.data
,
164 state
->response
->length
- sizeof(struct winbindd_response
));
165 if (state
->response
->extra_data
.data
&& !state
->out_data
.data
) {
170 if (state
->domain
!= NULL
) {
171 wcache_store_ndr(state
->domain
, state
->opnum
,
172 &state
->in_data
, &state
->out_data
);
175 tevent_req_done(req
);
178 static void wbint_bh_raw_call_domain_done(struct tevent_req
*subreq
)
180 struct tevent_req
*req
=
181 tevent_req_callback_data(subreq
,
183 struct wbint_bh_raw_call_state
*state
=
185 struct wbint_bh_raw_call_state
);
188 ret
= wb_domain_request_recv(subreq
, state
, &state
->response
, &err
);
191 NTSTATUS status
= map_nt_error_from_unix(err
);
192 tevent_req_nterror(req
, status
);
196 state
->out_data
= data_blob_talloc(state
,
197 state
->response
->extra_data
.data
,
198 state
->response
->length
- sizeof(struct winbindd_response
));
199 if (state
->response
->extra_data
.data
&& !state
->out_data
.data
) {
204 if (state
->domain
!= NULL
) {
205 wcache_store_ndr(state
->domain
, state
->opnum
,
206 &state
->in_data
, &state
->out_data
);
209 tevent_req_done(req
);
212 static NTSTATUS
wbint_bh_raw_call_recv(struct tevent_req
*req
,
218 struct wbint_bh_raw_call_state
*state
=
220 struct wbint_bh_raw_call_state
);
223 if (tevent_req_is_nterror(req
, &status
)) {
224 tevent_req_received(req
);
228 *out_data
= talloc_move(mem_ctx
, &state
->out_data
.data
);
229 *out_length
= state
->out_data
.length
;
231 tevent_req_received(req
);
235 struct wbint_bh_disconnect_state
{
239 static struct tevent_req
*wbint_bh_disconnect_send(TALLOC_CTX
*mem_ctx
,
240 struct tevent_context
*ev
,
241 struct dcerpc_binding_handle
*h
)
243 struct wbint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
244 struct wbint_bh_state
);
245 struct tevent_req
*req
;
246 struct wbint_bh_disconnect_state
*state
;
249 req
= tevent_req_create(mem_ctx
, &state
,
250 struct wbint_bh_disconnect_state
);
255 ok
= wbint_bh_is_connected(h
);
257 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
258 return tevent_req_post(req
, ev
);
262 * TODO: do a real async disconnect ...
267 tevent_req_done(req
);
268 return tevent_req_post(req
, ev
);
271 static NTSTATUS
wbint_bh_disconnect_recv(struct tevent_req
*req
)
275 if (tevent_req_is_nterror(req
, &status
)) {
276 tevent_req_received(req
);
280 tevent_req_received(req
);
284 static bool wbint_bh_ref_alloc(struct dcerpc_binding_handle
*h
)
289 static void wbint_bh_do_ndr_print(struct dcerpc_binding_handle
*h
,
290 ndr_flags_type ndr_flags
,
291 const void *_struct_ptr
,
292 const struct ndr_interface_call
*call
)
294 void *struct_ptr
= discard_const(_struct_ptr
);
296 if (DEBUGLEVEL
< 10) {
300 if (ndr_flags
& NDR_IN
) {
301 ndr_print_function_debug(call
->ndr_print
,
306 if (ndr_flags
& NDR_OUT
) {
307 ndr_print_function_debug(call
->ndr_print
,
314 static const struct dcerpc_binding_handle_ops wbint_bh_ops
= {
316 .is_connected
= wbint_bh_is_connected
,
317 .set_timeout
= wbint_bh_set_timeout
,
318 .raw_call_send
= wbint_bh_raw_call_send
,
319 .raw_call_recv
= wbint_bh_raw_call_recv
,
320 .disconnect_send
= wbint_bh_disconnect_send
,
321 .disconnect_recv
= wbint_bh_disconnect_recv
,
323 .ref_alloc
= wbint_bh_ref_alloc
,
324 .do_ndr_print
= wbint_bh_do_ndr_print
,
327 static NTSTATUS
make_internal_ncacn_conn(TALLOC_CTX
*mem_ctx
,
328 const struct ndr_interface_table
*table
,
329 struct dcerpc_ncacn_conn
**_out
)
331 struct dcerpc_ncacn_conn
*ncacn_conn
= NULL
;
333 ncacn_conn
= talloc_zero(mem_ctx
, struct dcerpc_ncacn_conn
);
334 if (ncacn_conn
== NULL
) {
335 return NT_STATUS_NO_MEMORY
;
338 ncacn_conn
->p
.mem_ctx
= mem_ctx
;
345 static NTSTATUS
find_ncalrpc_default_endpoint(struct dcesrv_context
*dce_ctx
,
346 struct dcesrv_endpoint
**ep
)
348 TALLOC_CTX
*tmp_ctx
= NULL
;
349 struct dcerpc_binding
*binding
= NULL
;
352 tmp_ctx
= talloc_new(dce_ctx
);
353 if (tmp_ctx
== NULL
) {
354 return NT_STATUS_NO_MEMORY
;
358 * Some services use a rpcint binding handle in their initialization,
359 * before the server is fully initialized. Search the NCALRPC endpoint
360 * with and without endpoint
362 status
= dcerpc_parse_binding(tmp_ctx
, "ncalrpc:", &binding
);
363 if (!NT_STATUS_IS_OK(status
)) {
367 status
= dcesrv_find_endpoint(dce_ctx
, binding
, ep
);
368 if (NT_STATUS_IS_OK(status
)) {
372 status
= dcerpc_parse_binding(tmp_ctx
, "ncalrpc:[WINBIND]", &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:[DEFAULT]", &binding
);
383 if (!NT_STATUS_IS_OK(status
)) {
387 status
= dcesrv_find_endpoint(dce_ctx
, binding
, ep
);
388 if (!NT_STATUS_IS_OK(status
)) {
393 talloc_free(tmp_ctx
);
397 static NTSTATUS
make_internal_dcesrv_connection(TALLOC_CTX
*mem_ctx
,
398 const struct ndr_interface_table
*ndr_table
,
399 struct dcerpc_ncacn_conn
*ncacn_conn
,
400 struct dcesrv_connection
**_out
)
402 struct dcesrv_connection
*conn
= NULL
;
403 struct dcesrv_connection_context
*context
= NULL
;
404 struct dcesrv_endpoint
*endpoint
= NULL
;
407 conn
= talloc_zero(mem_ctx
, struct dcesrv_connection
);
409 return NT_STATUS_NO_MEMORY
;
411 conn
->dce_ctx
= global_dcesrv_context();
412 conn
->preferred_transfer
= &ndr_transfer_syntax_ndr
;
413 conn
->transport
.private_data
= ncacn_conn
;
415 status
= find_ncalrpc_default_endpoint(conn
->dce_ctx
, &endpoint
);
416 if (!NT_STATUS_IS_OK(status
)) {
419 conn
->endpoint
= endpoint
;
421 conn
->default_auth_state
= talloc_zero(conn
, struct dcesrv_auth
);
422 if (conn
->default_auth_state
== NULL
) {
423 status
= NT_STATUS_NO_MEMORY
;
426 conn
->default_auth_state
->auth_finished
= true;
428 context
= talloc_zero(conn
, struct dcesrv_connection_context
);
429 if (context
== NULL
) {
430 status
= NT_STATUS_NO_MEMORY
;
433 context
->conn
= conn
;
434 context
->context_id
= 0;
435 context
->transfer_syntax
= *(conn
->preferred_transfer
);
436 context
->iface
= find_interface_by_syntax_id(
437 conn
->endpoint
, &ndr_table
->syntax_id
);
438 if (context
->iface
== NULL
) {
439 status
= NT_STATUS_RPC_INTERFACE_NOT_FOUND
;
443 DLIST_ADD(conn
->contexts
, context
);
453 static NTSTATUS
set_remote_addresses(struct dcesrv_connection
*conn
,
456 struct sockaddr_storage st
= { 0 };
457 struct sockaddr
*sar
= (struct sockaddr
*)&st
;
458 struct tsocket_address
*remote
= NULL
;
459 struct tsocket_address
*local
= NULL
;
460 socklen_t sa_len
= sizeof(st
);
465 ret
= getpeername(sock
, sar
, &sa_len
);
467 status
= map_nt_error_from_unix(ret
);
468 DBG_ERR("getpeername failed: %s\n", nt_errstr(status
));
472 ret
= tsocket_address_bsd_from_sockaddr(conn
, sar
, sa_len
, &remote
);
474 status
= map_nt_error_from_unix(ret
);
475 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
481 ret
= getsockname(sock
, sar
, &sa_len
);
483 status
= map_nt_error_from_unix(ret
);
484 DBG_ERR("getsockname failed: %s\n", nt_errstr(status
));
488 ret
= tsocket_address_bsd_from_sockaddr(conn
, sar
, sa_len
, &local
);
490 status
= map_nt_error_from_unix(ret
);
491 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
496 conn
->local_address
= talloc_move(conn
, &local
);
497 conn
->remote_address
= talloc_move(conn
, &remote
);
502 /* initialise a wbint binding handle */
503 struct dcerpc_binding_handle
*wbint_binding_handle(TALLOC_CTX
*mem_ctx
,
504 struct winbindd_domain
*domain
,
505 struct winbindd_child
*child
)
507 struct dcerpc_binding_handle
*h
;
508 struct wbint_bh_state
*hs
;
510 h
= dcerpc_binding_handle_create(mem_ctx
,
515 struct wbint_bh_state
,
526 enum winbindd_result
winbindd_dual_ndrcmd(struct winbindd_domain
*domain
,
527 struct winbindd_cli_state
*state
)
529 struct dcerpc_ncacn_conn
*ncacn_conn
= NULL
;
530 struct dcesrv_connection
*dcesrv_conn
= NULL
;
531 struct dcesrv_call_state
*dcesrv_call
= NULL
;
532 struct data_blob_list_item
*rep
= NULL
;
533 struct dcesrv_context_callbacks
*cb
= NULL
;
534 uint32_t opnum
= state
->request
->data
.ndrcmd
;
538 DBG_DEBUG("Running command %s (domain '%s')\n",
539 ndr_table_winbind
.calls
[opnum
].name
,
540 domain
? domain
->name
: "(null)");
542 mem_ctx
= talloc_stackframe();
543 if (mem_ctx
== NULL
) {
544 DBG_ERR("No memory\n");
545 return WINBINDD_ERROR
;
548 status
= make_internal_ncacn_conn(mem_ctx
,
551 if (!NT_STATUS_IS_OK(status
)) {
555 status
= make_internal_dcesrv_connection(ncacn_conn
,
559 if (!NT_STATUS_IS_OK(status
)) {
563 status
= set_remote_addresses(dcesrv_conn
, state
->sock
);
564 if (!NT_STATUS_IS_OK(status
)) {
568 dcesrv_call
= talloc_zero(dcesrv_conn
, struct dcesrv_call_state
);
569 if (dcesrv_call
== NULL
) {
570 status
= NT_STATUS_NO_MEMORY
;
574 dcesrv_call
->conn
= dcesrv_conn
;
575 dcesrv_call
->context
= dcesrv_conn
->contexts
;
576 dcesrv_call
->auth_state
= dcesrv_conn
->default_auth_state
;
578 ZERO_STRUCT(dcesrv_call
->pkt
);
579 dcesrv_call
->pkt
.u
.bind
.assoc_group_id
= 0;
581 cb
= dcesrv_call
->conn
->dce_ctx
->callbacks
;
582 status
= cb
->assoc_group
.find(
583 dcesrv_call
, cb
->assoc_group
.private_data
);
584 if (!NT_STATUS_IS_OK(status
)) {
588 ZERO_STRUCT(dcesrv_call
->pkt
);
589 dcesrv_call
->pkt
.u
.request
.opnum
= opnum
;
590 dcesrv_call
->pkt
.u
.request
.context_id
= 0;
591 dcesrv_call
->pkt
.u
.request
.stub_and_verifier
=
592 data_blob_const(state
->request
->extra_data
.data
,
593 state
->request
->extra_len
);
595 status
= dcesrv_call_dispatch_local(dcesrv_call
);
596 if (!NT_STATUS_IS_OK(status
)) {
600 rep
= dcesrv_call
->replies
;
601 DLIST_REMOVE(dcesrv_call
->replies
, rep
);
603 state
->response
->extra_data
.data
= talloc_steal(state
->mem_ctx
,
605 state
->response
->length
+= rep
->blob
.length
;
610 talloc_free(mem_ctx
);
611 if (NT_STATUS_IS_OK(status
)) {
614 return WINBINDD_ERROR
;