drsuapi.idl: fix source_dsa spelling
[samba4-gss.git] / source3 / winbindd / winbindd_dual_ndr.c
bloba1ce38c8bd1f3e25d29e789d5d81dc02fec484c8
1 /*
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.
29 #include "includes.h"
30 #include "winbindd/winbindd.h"
31 #include "winbindd/winbindd_proto.h"
32 #include "ntdomain.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"
37 #include "rpc_dce.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);
51 return hs->binding;
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)) {
60 return false;
63 return true;
66 static uint32_t wbint_bh_set_timeout(struct dcerpc_binding_handle *h,
67 uint32_t timeout)
69 /* TODO: implement timeouts */
70 return UINT32_MAX;
73 struct wbint_bh_raw_call_state {
74 struct winbindd_domain_ref domain;
75 uint32_t opnum;
76 DATA_BLOB in_data;
77 struct winbindd_request request;
78 struct winbindd_response *response;
79 DATA_BLOB out_data;
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,
89 uint32_t opnum,
90 uint32_t in_flags,
91 const uint8_t *in_data,
92 size_t in_length)
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;
99 bool ok;
100 struct tevent_req *subreq;
102 req = tevent_req_create(mem_ctx, &state,
103 struct wbint_bh_raw_call_state);
104 if (req == NULL) {
105 return NULL;
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);
113 if (!ok) {
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,
134 &state->request);
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);
140 return req;
143 subreq = wb_domain_request_send(state, ev, hs->domain,
144 &state->request);
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);
150 return 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,
157 struct tevent_req);
158 struct wbint_bh_raw_call_state *state =
159 tevent_req_data(req,
160 struct wbint_bh_raw_call_state);
161 int ret, err;
163 ret = wb_child_request_recv(subreq, state, &state->response, &err);
164 TALLOC_FREE(subreq);
165 if (ret == -1) {
166 NTSTATUS status = map_nt_error_from_unix(err);
167 tevent_req_nterror(req, status);
168 return;
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) {
175 tevent_req_oom(req);
176 return;
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,
186 struct tevent_req);
187 struct wbint_bh_raw_call_state *state =
188 tevent_req_data(req,
189 struct wbint_bh_raw_call_state);
190 int ret, err;
191 struct winbindd_domain *domain = NULL;
192 bool valid;
194 ret = wb_domain_request_recv(subreq, state, &state->response, &err);
195 TALLOC_FREE(subreq);
196 if (ret == -1) {
197 NTSTATUS status = map_nt_error_from_unix(err);
198 tevent_req_nterror(req, status);
199 return;
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) {
206 tevent_req_oom(req);
207 return;
210 valid = winbindd_domain_ref_get(&state->domain, &domain);
211 if (!valid) {
213 * winbindd_domain_ref_get() already generated
214 * a debug message for the stale domain!
216 * Just don't fill the cache
218 } else {
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,
227 TALLOC_CTX *mem_ctx,
228 uint8_t **out_data,
229 size_t *out_length,
230 uint32_t *out_flags)
232 struct wbint_bh_raw_call_state *state =
233 tevent_req_data(req,
234 struct wbint_bh_raw_call_state);
235 NTSTATUS status;
237 if (tevent_req_is_nterror(req, &status)) {
238 tevent_req_received(req);
239 return status;
242 *out_data = talloc_move(mem_ctx, &state->out_data.data);
243 *out_length = state->out_data.length;
244 *out_flags = 0;
245 tevent_req_received(req);
246 return NT_STATUS_OK;
249 struct wbint_bh_disconnect_state {
250 uint8_t _dummy;
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;
261 bool ok;
263 req = tevent_req_create(mem_ctx, &state,
264 struct wbint_bh_disconnect_state);
265 if (req == NULL) {
266 return NULL;
269 ok = wbint_bh_is_connected(h);
270 if (!ok) {
271 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
272 return tevent_req_post(req, ev);
276 * TODO: do a real async disconnect ...
278 hs->domain = NULL;
279 hs->child = NULL;
281 tevent_req_done(req);
282 return tevent_req_post(req, ev);
285 static NTSTATUS wbint_bh_disconnect_recv(struct tevent_req *req)
287 NTSTATUS status;
289 if (tevent_req_is_nterror(req, &status)) {
290 tevent_req_received(req);
291 return status;
294 tevent_req_received(req);
295 return NT_STATUS_OK;
298 static bool wbint_bh_ref_alloc(struct dcerpc_binding_handle *h)
300 return true;
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)) {
311 return;
314 if (ndr_flags & NDR_IN) {
315 ndr_print_function_debug(call->ndr_print,
316 call->name,
317 ndr_flags,
318 struct_ptr);
320 if (ndr_flags & NDR_OUT) {
321 ndr_print_function_debug(call->ndr_print,
322 call->name,
323 ndr_flags,
324 struct_ptr);
328 static const struct dcerpc_binding_handle_ops wbint_bh_ops = {
329 .name = "wbint",
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;
355 *_out = ncacn_conn;
357 return NT_STATUS_OK;
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;
365 NTSTATUS status;
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)) {
379 goto out;
382 status = dcesrv_find_endpoint(dce_ctx, binding, ep);
383 if (NT_STATUS_IS_OK(status)) {
384 goto out;
387 status = dcerpc_parse_binding(tmp_ctx, "ncalrpc:[WINBIND]", &binding);
388 if (!NT_STATUS_IS_OK(status)) {
389 goto out;
392 status = dcesrv_find_endpoint(dce_ctx, binding, ep);
393 if (NT_STATUS_IS_OK(status)) {
394 goto out;
397 status = dcerpc_parse_binding(tmp_ctx, "ncalrpc:[DEFAULT]", &binding);
398 if (!NT_STATUS_IS_OK(status)) {
399 goto out;
402 status = dcesrv_find_endpoint(dce_ctx, binding, ep);
403 if (!NT_STATUS_IS_OK(status)) {
404 goto out;
407 out:
408 talloc_free(tmp_ctx);
409 return status;
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;
420 NTSTATUS status;
422 conn = talloc_zero(mem_ctx, struct dcesrv_connection);
423 if (conn == NULL) {
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)) {
432 goto fail;
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;
439 goto fail;
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;
446 goto fail;
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;
455 goto fail;
458 DLIST_ADD(conn->contexts, context);
460 *_out = conn;
462 return NT_STATUS_OK;
463 fail:
464 talloc_free(conn);
465 return status;
468 static NTSTATUS set_remote_addresses(struct dcesrv_connection *conn,
469 int sock)
471 struct samba_sockaddr ssa;
472 struct tsocket_address *remote = NULL;
473 struct tsocket_address *local = NULL;
474 NTSTATUS status;
475 int ret;
477 ssa = (struct samba_sockaddr) { .sa_socklen = sizeof(ssa.u.ss), };
478 ret = getpeername(sock, &ssa.u.sa, &ssa.sa_socklen);
479 if (ret != 0) {
480 status = map_nt_error_from_unix(ret);
481 DBG_ERR("getpeername failed: %s\n", nt_errstr(status));
482 return status;
485 ret = tsocket_address_bsd_from_sockaddr(conn,
486 &ssa.u.sa,
487 ssa.sa_socklen,
488 &remote);
489 if (ret != 0) {
490 status = map_nt_error_from_unix(ret);
491 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
492 nt_errstr(status));
493 return status;
496 ssa = (struct samba_sockaddr) { .sa_socklen = sizeof(ssa.u.ss), };
497 ret = getsockname(sock, &ssa.u.sa, &ssa.sa_socklen);
498 if (ret != 0) {
499 status = map_nt_error_from_unix(ret);
500 DBG_ERR("getsockname failed: %s\n", nt_errstr(status));
501 return status;
504 ret = tsocket_address_bsd_from_sockaddr(conn,
505 &ssa.u.sa,
506 ssa.sa_socklen,
507 &local);
508 if (ret != 0) {
509 status = map_nt_error_from_unix(ret);
510 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
511 nt_errstr(status));
512 return status;
515 conn->local_address = talloc_move(conn, &local);
516 conn->remote_address = talloc_move(conn, &remote);
518 return NT_STATUS_OK;
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;
529 NTSTATUS status;
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,
537 &wbint_bh_ops,
538 NULL,
539 &ndr_table_winbind,
540 &hs,
541 struct wbint_bh_state,
542 __location__);
543 if (h == NULL) {
544 return NULL;
546 hs->domain = domain;
547 hs->child = child;
549 status = dcerpc_parse_binding(hs, "", &b);
550 if (!NT_STATUS_IS_OK(status)) {
551 TALLOC_FREE(h);
552 return NULL;
554 status = dcerpc_binding_set_transport(b, NCACN_INTERNAL);
555 if (!NT_STATUS_IS_OK(status)) {
556 TALLOC_FREE(h);
557 return NULL;
559 status = dcerpc_binding_set_string_option(b, "host", "localhost");
560 if (!NT_STATUS_IS_OK(status)) {
561 TALLOC_FREE(h);
562 return NULL;
564 status = dcerpc_binding_set_string_option(b,
565 "endpoint",
566 "winbindd_dual_ndrcmd");
567 if (!NT_STATUS_IS_OK(status)) {
568 TALLOC_FREE(h);
569 return NULL;
571 status = dcerpc_binding_set_abstract_syntax(b,
572 &ndr_table_winbind.syntax_id);
573 if (!NT_STATUS_IS_OK(status)) {
574 TALLOC_FREE(h);
575 return NULL;
578 hs->binding = b;
580 return h;
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;
592 TALLOC_CTX *mem_ctx;
593 NTSTATUS status;
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,
606 &ndr_table_winbind,
607 &ncacn_conn);
608 if (!NT_STATUS_IS_OK(status)) {
609 goto out;
612 status = make_internal_dcesrv_connection(ncacn_conn,
613 &ndr_table_winbind,
614 ncacn_conn,
615 &dcesrv_conn);
616 if (!NT_STATUS_IS_OK(status)) {
617 goto out;
620 status = set_remote_addresses(dcesrv_conn, state->sock);
621 if (!NT_STATUS_IS_OK(status)) {
622 goto out;
625 dcesrv_call = talloc_zero(dcesrv_conn, struct dcesrv_call_state);
626 if (dcesrv_call == NULL) {
627 status = NT_STATUS_NO_MEMORY;
628 goto out;
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)) {
642 goto out;
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)) {
654 goto out;
657 rep = dcesrv_call->replies;
658 DLIST_REMOVE(dcesrv_call->replies, rep);
660 state->response->extra_data.data = talloc_steal(state->mem_ctx,
661 rep->blob.data);
662 state->response->length += rep->blob.length;
664 talloc_free(rep);
666 out:
667 talloc_free(mem_ctx);
668 if (NT_STATUS_IS_OK(status)) {
669 return WINBINDD_OK;
671 return WINBINDD_ERROR;