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"
32 #include "librpc/gen_ndr/srv_wbint.h"
34 struct wbint_bh_state
{
35 struct winbindd_domain
*domain
;
36 struct winbindd_child
*child
;
39 static bool wbint_bh_is_connected(struct dcerpc_binding_handle
*h
)
41 struct wbint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
42 struct wbint_bh_state
);
51 static uint32_t wbint_bh_set_timeout(struct dcerpc_binding_handle
*h
,
54 /* TODO: implement timeouts */
58 struct wbint_bh_raw_call_state
{
59 struct winbindd_domain
*domain
;
62 struct winbindd_request request
;
63 struct winbindd_response
*response
;
67 static void wbint_bh_raw_call_done(struct tevent_req
*subreq
);
69 static struct tevent_req
*wbint_bh_raw_call_send(TALLOC_CTX
*mem_ctx
,
70 struct tevent_context
*ev
,
71 struct dcerpc_binding_handle
*h
,
72 const struct GUID
*object
,
75 const uint8_t *in_data
,
78 struct wbint_bh_state
*hs
=
79 dcerpc_binding_handle_data(h
,
80 struct wbint_bh_state
);
81 struct tevent_req
*req
;
82 struct wbint_bh_raw_call_state
*state
;
84 struct tevent_req
*subreq
;
86 req
= tevent_req_create(mem_ctx
, &state
,
87 struct wbint_bh_raw_call_state
);
91 state
->domain
= hs
->domain
;
93 state
->in_data
.data
= discard_const_p(uint8_t, in_data
);
94 state
->in_data
.length
= in_length
;
96 ok
= wbint_bh_is_connected(h
);
98 tevent_req_nterror(req
, NT_STATUS_INVALID_CONNECTION
);
99 return tevent_req_post(req
, ev
);
102 if ((state
->domain
!= NULL
)
103 && wcache_fetch_ndr(state
, state
->domain
, state
->opnum
,
104 &state
->in_data
, &state
->out_data
)) {
105 tevent_req_done(req
);
106 return tevent_req_post(req
, ev
);
109 state
->request
.cmd
= WINBINDD_DUAL_NDRCMD
;
110 state
->request
.data
.ndrcmd
= state
->opnum
;
111 state
->request
.extra_data
.data
= (char *)state
->in_data
.data
;
112 state
->request
.extra_len
= state
->in_data
.length
;
114 subreq
= wb_child_request_send(state
, ev
, hs
->child
,
116 if (tevent_req_nomem(subreq
, req
)) {
117 return tevent_req_post(req
, ev
);
119 tevent_req_set_callback(subreq
, wbint_bh_raw_call_done
, req
);
124 static void wbint_bh_raw_call_done(struct tevent_req
*subreq
)
126 struct tevent_req
*req
=
127 tevent_req_callback_data(subreq
,
129 struct wbint_bh_raw_call_state
*state
=
131 struct wbint_bh_raw_call_state
);
134 ret
= wb_child_request_recv(subreq
, state
, &state
->response
, &err
);
137 NTSTATUS status
= map_nt_error_from_unix(err
);
138 tevent_req_nterror(req
, status
);
142 state
->out_data
= data_blob_talloc(state
,
143 state
->response
->extra_data
.data
,
144 state
->response
->length
- sizeof(struct winbindd_response
));
145 if (state
->response
->extra_data
.data
&& !state
->out_data
.data
) {
146 tevent_req_nomem(NULL
, req
);
150 if (state
->domain
!= NULL
) {
151 wcache_store_ndr(state
->domain
, state
->opnum
,
152 &state
->in_data
, &state
->out_data
);
155 tevent_req_done(req
);
158 static NTSTATUS
wbint_bh_raw_call_recv(struct tevent_req
*req
,
164 struct wbint_bh_raw_call_state
*state
=
166 struct wbint_bh_raw_call_state
);
169 if (tevent_req_is_nterror(req
, &status
)) {
170 tevent_req_received(req
);
174 *out_data
= talloc_move(mem_ctx
, &state
->out_data
.data
);
175 *out_length
= state
->out_data
.length
;
177 tevent_req_received(req
);
181 struct wbint_bh_disconnect_state
{
185 static struct tevent_req
*wbint_bh_disconnect_send(TALLOC_CTX
*mem_ctx
,
186 struct tevent_context
*ev
,
187 struct dcerpc_binding_handle
*h
)
189 struct wbint_bh_state
*hs
= dcerpc_binding_handle_data(h
,
190 struct wbint_bh_state
);
191 struct tevent_req
*req
;
192 struct wbint_bh_disconnect_state
*state
;
195 req
= tevent_req_create(mem_ctx
, &state
,
196 struct wbint_bh_disconnect_state
);
201 ok
= wbint_bh_is_connected(h
);
203 tevent_req_nterror(req
, NT_STATUS_INVALID_CONNECTION
);
204 return tevent_req_post(req
, ev
);
208 * TODO: do a real async disconnect ...
210 * For now the caller needs to free rpc_cli
214 tevent_req_done(req
);
215 return tevent_req_post(req
, ev
);
218 static NTSTATUS
wbint_bh_disconnect_recv(struct tevent_req
*req
)
222 if (tevent_req_is_nterror(req
, &status
)) {
223 tevent_req_received(req
);
227 tevent_req_received(req
);
231 static bool wbint_bh_ref_alloc(struct dcerpc_binding_handle
*h
)
236 static void wbint_bh_do_ndr_print(struct dcerpc_binding_handle
*h
,
238 const void *_struct_ptr
,
239 const struct ndr_interface_call
*call
)
241 void *struct_ptr
= discard_const(_struct_ptr
);
243 if (DEBUGLEVEL
< 10) {
247 if (ndr_flags
& NDR_IN
) {
248 ndr_print_function_debug(call
->ndr_print
,
253 if (ndr_flags
& NDR_OUT
) {
254 ndr_print_function_debug(call
->ndr_print
,
261 static const struct dcerpc_binding_handle_ops wbint_bh_ops
= {
263 .is_connected
= wbint_bh_is_connected
,
264 .set_timeout
= wbint_bh_set_timeout
,
265 .raw_call_send
= wbint_bh_raw_call_send
,
266 .raw_call_recv
= wbint_bh_raw_call_recv
,
267 .disconnect_send
= wbint_bh_disconnect_send
,
268 .disconnect_recv
= wbint_bh_disconnect_recv
,
270 .ref_alloc
= wbint_bh_ref_alloc
,
271 .do_ndr_print
= wbint_bh_do_ndr_print
,
274 /* initialise a wbint binding handle */
275 struct dcerpc_binding_handle
*wbint_binding_handle(TALLOC_CTX
*mem_ctx
,
276 struct winbindd_domain
*domain
,
277 struct winbindd_child
*child
)
279 struct dcerpc_binding_handle
*h
;
280 struct wbint_bh_state
*hs
;
282 h
= dcerpc_binding_handle_create(mem_ctx
,
287 struct wbint_bh_state
,
298 enum winbindd_result
winbindd_dual_ndrcmd(struct winbindd_domain
*domain
,
299 struct winbindd_cli_state
*state
)
301 struct pipes_struct p
;
302 struct api_struct
*fns
;
306 wbint_get_pipe_fns(&fns
, &num_fns
);
308 if (state
->request
->data
.ndrcmd
>= num_fns
) {
309 return WINBINDD_ERROR
;
312 DEBUG(10, ("winbindd_dual_ndrcmd: Running command %s (%s)\n",
313 fns
[state
->request
->data
.ndrcmd
].name
,
314 domain
? domain
->name
: "no domain"));
317 p
.mem_ctx
= talloc_stackframe();
318 p
.in_data
.data
= data_blob_const(state
->request
->extra_data
.data
,
319 state
->request
->extra_len
);
321 ret
= fns
[state
->request
->data
.ndrcmd
].fn(&p
);
323 TALLOC_FREE(p
.mem_ctx
);
324 return WINBINDD_ERROR
;
327 state
->response
->extra_data
.data
=
328 talloc_move(state
->mem_ctx
, &p
.out_data
.rdata
.data
);
329 state
->response
->length
+= p
.out_data
.rdata
.length
;
330 p
.out_data
.rdata
.length
= 0;
332 TALLOC_FREE(p
.mem_ctx
);
334 if (state
->response
->extra_data
.data
== NULL
) {
335 return WINBINDD_ERROR
;