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"
39 struct wbint_bh_state
{
40 struct winbindd_domain
*domain
;
41 struct winbindd_child
*child
;
44 static bool wbint_bh_is_connected(struct dcerpc_binding_handle
*h
)
46 struct wbint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
47 struct wbint_bh_state
);
49 if ((hs
->domain
== NULL
) && (hs
->child
== NULL
)) {
56 static uint32_t wbint_bh_set_timeout(struct dcerpc_binding_handle
*h
,
59 /* TODO: implement timeouts */
63 struct wbint_bh_raw_call_state
{
64 struct winbindd_domain
*domain
;
67 struct winbindd_request request
;
68 struct winbindd_response
*response
;
72 static void wbint_bh_raw_call_child_done(struct tevent_req
*subreq
);
73 static void wbint_bh_raw_call_domain_done(struct tevent_req
*subreq
);
75 static struct tevent_req
*wbint_bh_raw_call_send(TALLOC_CTX
*mem_ctx
,
76 struct tevent_context
*ev
,
77 struct dcerpc_binding_handle
*h
,
78 const struct GUID
*object
,
81 const uint8_t *in_data
,
84 struct wbint_bh_state
*hs
=
85 dcerpc_binding_handle_data(h
,
86 struct wbint_bh_state
);
87 struct tevent_req
*req
;
88 struct wbint_bh_raw_call_state
*state
;
90 struct tevent_req
*subreq
;
92 req
= tevent_req_create(mem_ctx
, &state
,
93 struct wbint_bh_raw_call_state
);
97 state
->domain
= hs
->domain
;
99 state
->in_data
.data
= discard_const_p(uint8_t, in_data
);
100 state
->in_data
.length
= in_length
;
102 ok
= wbint_bh_is_connected(h
);
104 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
105 return tevent_req_post(req
, ev
);
108 if ((state
->domain
!= NULL
)
109 && wcache_fetch_ndr(state
, state
->domain
, state
->opnum
,
110 &state
->in_data
, &state
->out_data
)) {
111 DBG_DEBUG("Got opnum %"PRIu32
" for domain %s from cache\n",
112 state
->opnum
, state
->domain
->name
);
113 tevent_req_done(req
);
114 return tevent_req_post(req
, ev
);
117 state
->request
.cmd
= WINBINDD_DUAL_NDRCMD
;
118 state
->request
.data
.ndrcmd
= state
->opnum
;
119 state
->request
.extra_data
.data
= (char *)state
->in_data
.data
;
120 state
->request
.extra_len
= state
->in_data
.length
;
122 if (hs
->child
!= NULL
) {
123 subreq
= wb_child_request_send(state
, ev
, hs
->child
,
125 if (tevent_req_nomem(subreq
, req
)) {
126 return tevent_req_post(req
, ev
);
128 tevent_req_set_callback(
129 subreq
, wbint_bh_raw_call_child_done
, req
);
133 subreq
= wb_domain_request_send(state
, ev
, hs
->domain
,
135 if (tevent_req_nomem(subreq
, req
)) {
136 return tevent_req_post(req
, ev
);
138 tevent_req_set_callback(subreq
, wbint_bh_raw_call_domain_done
, req
);
143 static void wbint_bh_raw_call_child_done(struct tevent_req
*subreq
)
145 struct tevent_req
*req
=
146 tevent_req_callback_data(subreq
,
148 struct wbint_bh_raw_call_state
*state
=
150 struct wbint_bh_raw_call_state
);
153 ret
= wb_child_request_recv(subreq
, state
, &state
->response
, &err
);
156 NTSTATUS status
= map_nt_error_from_unix(err
);
157 tevent_req_nterror(req
, status
);
161 state
->out_data
= data_blob_talloc(state
,
162 state
->response
->extra_data
.data
,
163 state
->response
->length
- sizeof(struct winbindd_response
));
164 if (state
->response
->extra_data
.data
&& !state
->out_data
.data
) {
169 if (state
->domain
!= NULL
) {
170 wcache_store_ndr(state
->domain
, state
->opnum
,
171 &state
->in_data
, &state
->out_data
);
174 tevent_req_done(req
);
177 static void wbint_bh_raw_call_domain_done(struct tevent_req
*subreq
)
179 struct tevent_req
*req
=
180 tevent_req_callback_data(subreq
,
182 struct wbint_bh_raw_call_state
*state
=
184 struct wbint_bh_raw_call_state
);
187 ret
= wb_domain_request_recv(subreq
, state
, &state
->response
, &err
);
190 NTSTATUS status
= map_nt_error_from_unix(err
);
191 tevent_req_nterror(req
, status
);
195 state
->out_data
= data_blob_talloc(state
,
196 state
->response
->extra_data
.data
,
197 state
->response
->length
- sizeof(struct winbindd_response
));
198 if (state
->response
->extra_data
.data
&& !state
->out_data
.data
) {
203 if (state
->domain
!= NULL
) {
204 wcache_store_ndr(state
->domain
, state
->opnum
,
205 &state
->in_data
, &state
->out_data
);
208 tevent_req_done(req
);
211 static NTSTATUS
wbint_bh_raw_call_recv(struct tevent_req
*req
,
217 struct wbint_bh_raw_call_state
*state
=
219 struct wbint_bh_raw_call_state
);
222 if (tevent_req_is_nterror(req
, &status
)) {
223 tevent_req_received(req
);
227 *out_data
= talloc_move(mem_ctx
, &state
->out_data
.data
);
228 *out_length
= state
->out_data
.length
;
230 tevent_req_received(req
);
234 struct wbint_bh_disconnect_state
{
238 static struct tevent_req
*wbint_bh_disconnect_send(TALLOC_CTX
*mem_ctx
,
239 struct tevent_context
*ev
,
240 struct dcerpc_binding_handle
*h
)
242 struct wbint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
243 struct wbint_bh_state
);
244 struct tevent_req
*req
;
245 struct wbint_bh_disconnect_state
*state
;
248 req
= tevent_req_create(mem_ctx
, &state
,
249 struct wbint_bh_disconnect_state
);
254 ok
= wbint_bh_is_connected(h
);
256 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
257 return tevent_req_post(req
, ev
);
261 * TODO: do a real async disconnect ...
266 tevent_req_done(req
);
267 return tevent_req_post(req
, ev
);
270 static NTSTATUS
wbint_bh_disconnect_recv(struct tevent_req
*req
)
274 if (tevent_req_is_nterror(req
, &status
)) {
275 tevent_req_received(req
);
279 tevent_req_received(req
);
283 static bool wbint_bh_ref_alloc(struct dcerpc_binding_handle
*h
)
288 static void wbint_bh_do_ndr_print(struct dcerpc_binding_handle
*h
,
290 const void *_struct_ptr
,
291 const struct ndr_interface_call
*call
)
293 void *struct_ptr
= discard_const(_struct_ptr
);
295 if (DEBUGLEVEL
< 10) {
299 if (ndr_flags
& NDR_IN
) {
300 ndr_print_function_debug(call
->ndr_print
,
305 if (ndr_flags
& NDR_OUT
) {
306 ndr_print_function_debug(call
->ndr_print
,
313 static const struct dcerpc_binding_handle_ops wbint_bh_ops
= {
315 .is_connected
= wbint_bh_is_connected
,
316 .set_timeout
= wbint_bh_set_timeout
,
317 .raw_call_send
= wbint_bh_raw_call_send
,
318 .raw_call_recv
= wbint_bh_raw_call_recv
,
319 .disconnect_send
= wbint_bh_disconnect_send
,
320 .disconnect_recv
= wbint_bh_disconnect_recv
,
322 .ref_alloc
= wbint_bh_ref_alloc
,
323 .do_ndr_print
= wbint_bh_do_ndr_print
,
326 static NTSTATUS
make_internal_ncacn_conn(TALLOC_CTX
*mem_ctx
,
327 const struct ndr_interface_table
*table
,
328 struct dcerpc_ncacn_conn
**_out
)
330 struct dcerpc_ncacn_conn
*ncacn_conn
= NULL
;
332 ncacn_conn
= talloc_zero(mem_ctx
, struct dcerpc_ncacn_conn
);
333 if (ncacn_conn
== NULL
) {
334 return NT_STATUS_NO_MEMORY
;
337 ncacn_conn
->p
.mem_ctx
= mem_ctx
;
344 static NTSTATUS
find_ncalrpc_default_endpoint(struct dcesrv_context
*dce_ctx
,
345 struct dcesrv_endpoint
**ep
)
347 TALLOC_CTX
*tmp_ctx
= NULL
;
348 struct dcerpc_binding
*binding
= NULL
;
351 tmp_ctx
= talloc_new(dce_ctx
);
352 if (tmp_ctx
== NULL
) {
353 return NT_STATUS_NO_MEMORY
;
357 * Some services use a rpcint binding handle in their initialization,
358 * before the server is fully initialized. Search the NCALRPC endpoint
359 * with and without endpoint
361 status
= dcerpc_parse_binding(tmp_ctx
, "ncalrpc:", &binding
);
362 if (!NT_STATUS_IS_OK(status
)) {
366 status
= dcesrv_find_endpoint(dce_ctx
, binding
, ep
);
367 if (NT_STATUS_IS_OK(status
)) {
371 status
= dcerpc_parse_binding(tmp_ctx
, "ncalrpc:[WINBIND]", &binding
);
372 if (!NT_STATUS_IS_OK(status
)) {
376 status
= dcesrv_find_endpoint(dce_ctx
, binding
, ep
);
377 if (NT_STATUS_IS_OK(status
)) {
381 status
= dcerpc_parse_binding(tmp_ctx
, "ncalrpc:[DEFAULT]", &binding
);
382 if (!NT_STATUS_IS_OK(status
)) {
386 status
= dcesrv_find_endpoint(dce_ctx
, binding
, ep
);
387 if (!NT_STATUS_IS_OK(status
)) {
392 talloc_free(tmp_ctx
);
396 static NTSTATUS
make_internal_dcesrv_connection(TALLOC_CTX
*mem_ctx
,
397 const struct ndr_interface_table
*ndr_table
,
398 struct dcerpc_ncacn_conn
*ncacn_conn
,
399 struct dcesrv_connection
**_out
)
401 struct dcesrv_connection
*conn
= NULL
;
402 struct dcesrv_connection_context
*context
= NULL
;
403 struct dcesrv_endpoint
*endpoint
= NULL
;
406 conn
= talloc_zero(mem_ctx
, struct dcesrv_connection
);
408 return NT_STATUS_NO_MEMORY
;
410 conn
->dce_ctx
= global_dcesrv_context();
411 conn
->preferred_transfer
= &ndr_transfer_syntax_ndr
;
412 conn
->transport
.private_data
= ncacn_conn
;
414 status
= find_ncalrpc_default_endpoint(conn
->dce_ctx
, &endpoint
);
415 if (!NT_STATUS_IS_OK(status
)) {
418 conn
->endpoint
= endpoint
;
420 conn
->default_auth_state
= talloc_zero(conn
, struct dcesrv_auth
);
421 if (conn
->default_auth_state
== NULL
) {
422 status
= NT_STATUS_NO_MEMORY
;
425 conn
->default_auth_state
->auth_finished
= true;
427 context
= talloc_zero(conn
, struct dcesrv_connection_context
);
428 if (context
== NULL
) {
429 status
= NT_STATUS_NO_MEMORY
;
432 context
->conn
= conn
;
433 context
->context_id
= 0;
434 context
->transfer_syntax
= *(conn
->preferred_transfer
);
435 context
->iface
= find_interface_by_syntax_id(
436 conn
->endpoint
, &ndr_table
->syntax_id
);
437 if (context
->iface
== NULL
) {
438 status
= NT_STATUS_RPC_INTERFACE_NOT_FOUND
;
442 DLIST_ADD(conn
->contexts
, context
);
452 /* initialise a wbint binding handle */
453 struct dcerpc_binding_handle
*wbint_binding_handle(TALLOC_CTX
*mem_ctx
,
454 struct winbindd_domain
*domain
,
455 struct winbindd_child
*child
)
457 struct dcerpc_binding_handle
*h
;
458 struct wbint_bh_state
*hs
;
460 h
= dcerpc_binding_handle_create(mem_ctx
,
465 struct wbint_bh_state
,
476 enum winbindd_result
winbindd_dual_ndrcmd(struct winbindd_domain
*domain
,
477 struct winbindd_cli_state
*state
)
479 struct dcerpc_ncacn_conn
*ncacn_conn
= NULL
;
480 struct dcesrv_connection
*dcesrv_conn
= NULL
;
481 struct dcesrv_call_state
*dcesrv_call
= NULL
;
482 struct data_blob_list_item
*rep
= NULL
;
483 struct dcesrv_context_callbacks
*cb
= NULL
;
484 uint32_t opnum
= state
->request
->data
.ndrcmd
;
488 DBG_DEBUG("Running command %s (domain '%s')\n",
489 ndr_table_winbind
.calls
[opnum
].name
,
490 domain
? domain
->name
: "(null)");
492 mem_ctx
= talloc_stackframe();
493 if (mem_ctx
== NULL
) {
494 DBG_ERR("No memory");
495 return WINBINDD_ERROR
;
498 status
= make_internal_ncacn_conn(mem_ctx
,
501 if (!NT_STATUS_IS_OK(status
)) {
505 status
= make_internal_dcesrv_connection(ncacn_conn
,
509 if (!NT_STATUS_IS_OK(status
)) {
513 dcesrv_call
= talloc_zero(dcesrv_conn
, struct dcesrv_call_state
);
514 if (dcesrv_call
== NULL
) {
515 status
= NT_STATUS_NO_MEMORY
;
519 dcesrv_call
->conn
= dcesrv_conn
;
520 dcesrv_call
->context
= dcesrv_conn
->contexts
;
521 dcesrv_call
->auth_state
= dcesrv_conn
->default_auth_state
;
523 ZERO_STRUCT(dcesrv_call
->pkt
);
524 dcesrv_call
->pkt
.u
.bind
.assoc_group_id
= 0;
526 cb
= dcesrv_call
->conn
->dce_ctx
->callbacks
;
527 status
= cb
->assoc_group
.find(
528 dcesrv_call
, cb
->assoc_group
.private_data
);
529 if (!NT_STATUS_IS_OK(status
)) {
533 ZERO_STRUCT(dcesrv_call
->pkt
);
534 dcesrv_call
->pkt
.u
.request
.opnum
= opnum
;
535 dcesrv_call
->pkt
.u
.request
.context_id
= 0;
536 dcesrv_call
->pkt
.u
.request
.stub_and_verifier
=
537 data_blob_const(state
->request
->extra_data
.data
,
538 state
->request
->extra_len
);
540 status
= dcesrv_call_dispatch_local(dcesrv_call
);
541 if (!NT_STATUS_IS_OK(status
)) {
545 rep
= dcesrv_call
->replies
;
546 DLIST_REMOVE(dcesrv_call
->replies
, rep
);
548 state
->response
->extra_data
.data
= talloc_steal(state
->mem_ctx
,
550 state
->response
->length
+= rep
->blob
.length
;
555 talloc_free(mem_ctx
);
556 if (NT_STATUS_IS_OK(status
)) {
559 return WINBINDD_ERROR
;