ctdb-server: Remove duplicate logic
[samba4-gss.git] / source3 / winbindd / winbindd_dual_ndr.c
blob718405cc1370d36bbc18d7a5f62cf95042e25f8b
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;
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 *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 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 ((state->domain != NULL)
119 && wcache_fetch_ndr(state, state->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, state->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 if (state->domain != NULL) {
180 wcache_store_ndr(state->domain, state->opnum,
181 &state->in_data, &state->out_data);
184 tevent_req_done(req);
187 static void wbint_bh_raw_call_domain_done(struct tevent_req *subreq)
189 struct tevent_req *req =
190 tevent_req_callback_data(subreq,
191 struct tevent_req);
192 struct wbint_bh_raw_call_state *state =
193 tevent_req_data(req,
194 struct wbint_bh_raw_call_state);
195 int ret, err;
197 ret = wb_domain_request_recv(subreq, state, &state->response, &err);
198 TALLOC_FREE(subreq);
199 if (ret == -1) {
200 NTSTATUS status = map_nt_error_from_unix(err);
201 tevent_req_nterror(req, status);
202 return;
205 state->out_data = data_blob_talloc(state,
206 state->response->extra_data.data,
207 state->response->length - sizeof(struct winbindd_response));
208 if (state->response->extra_data.data && !state->out_data.data) {
209 tevent_req_oom(req);
210 return;
213 if (state->domain != NULL) {
214 wcache_store_ndr(state->domain, state->opnum,
215 &state->in_data, &state->out_data);
218 tevent_req_done(req);
221 static NTSTATUS wbint_bh_raw_call_recv(struct tevent_req *req,
222 TALLOC_CTX *mem_ctx,
223 uint8_t **out_data,
224 size_t *out_length,
225 uint32_t *out_flags)
227 struct wbint_bh_raw_call_state *state =
228 tevent_req_data(req,
229 struct wbint_bh_raw_call_state);
230 NTSTATUS status;
232 if (tevent_req_is_nterror(req, &status)) {
233 tevent_req_received(req);
234 return status;
237 *out_data = talloc_move(mem_ctx, &state->out_data.data);
238 *out_length = state->out_data.length;
239 *out_flags = 0;
240 tevent_req_received(req);
241 return NT_STATUS_OK;
244 struct wbint_bh_disconnect_state {
245 uint8_t _dummy;
248 static struct tevent_req *wbint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
249 struct tevent_context *ev,
250 struct dcerpc_binding_handle *h)
252 struct wbint_bh_state *hs = dcerpc_binding_handle_data(h,
253 struct wbint_bh_state);
254 struct tevent_req *req;
255 struct wbint_bh_disconnect_state *state;
256 bool ok;
258 req = tevent_req_create(mem_ctx, &state,
259 struct wbint_bh_disconnect_state);
260 if (req == NULL) {
261 return NULL;
264 ok = wbint_bh_is_connected(h);
265 if (!ok) {
266 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
267 return tevent_req_post(req, ev);
271 * TODO: do a real async disconnect ...
273 hs->domain = NULL;
274 hs->child = NULL;
276 tevent_req_done(req);
277 return tevent_req_post(req, ev);
280 static NTSTATUS wbint_bh_disconnect_recv(struct tevent_req *req)
282 NTSTATUS status;
284 if (tevent_req_is_nterror(req, &status)) {
285 tevent_req_received(req);
286 return status;
289 tevent_req_received(req);
290 return NT_STATUS_OK;
293 static bool wbint_bh_ref_alloc(struct dcerpc_binding_handle *h)
295 return true;
298 static void wbint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
299 ndr_flags_type ndr_flags,
300 const void *_struct_ptr,
301 const struct ndr_interface_call *call)
303 void *struct_ptr = discard_const(_struct_ptr);
305 if (DEBUGLEVEL < 10) {
306 return;
309 if (ndr_flags & NDR_IN) {
310 ndr_print_function_debug(call->ndr_print,
311 call->name,
312 ndr_flags,
313 struct_ptr);
315 if (ndr_flags & NDR_OUT) {
316 ndr_print_function_debug(call->ndr_print,
317 call->name,
318 ndr_flags,
319 struct_ptr);
323 static const struct dcerpc_binding_handle_ops wbint_bh_ops = {
324 .name = "wbint",
325 .get_binding = wbint_bh_get_binding,
326 .is_connected = wbint_bh_is_connected,
327 .set_timeout = wbint_bh_set_timeout,
328 .raw_call_send = wbint_bh_raw_call_send,
329 .raw_call_recv = wbint_bh_raw_call_recv,
330 .disconnect_send = wbint_bh_disconnect_send,
331 .disconnect_recv = wbint_bh_disconnect_recv,
333 .ref_alloc = wbint_bh_ref_alloc,
334 .do_ndr_print = wbint_bh_do_ndr_print,
337 static NTSTATUS make_internal_ncacn_conn(TALLOC_CTX *mem_ctx,
338 const struct ndr_interface_table *table,
339 struct dcerpc_ncacn_conn **_out)
341 struct dcerpc_ncacn_conn *ncacn_conn = NULL;
343 ncacn_conn = talloc_zero(mem_ctx, struct dcerpc_ncacn_conn);
344 if (ncacn_conn == NULL) {
345 return NT_STATUS_NO_MEMORY;
348 ncacn_conn->p.mem_ctx = mem_ctx;
350 *_out = ncacn_conn;
352 return NT_STATUS_OK;
355 static NTSTATUS find_ncalrpc_default_endpoint(struct dcesrv_context *dce_ctx,
356 struct dcesrv_endpoint **ep)
358 TALLOC_CTX *tmp_ctx = NULL;
359 struct dcerpc_binding *binding = NULL;
360 NTSTATUS status;
362 tmp_ctx = talloc_new(dce_ctx);
363 if (tmp_ctx == NULL) {
364 return NT_STATUS_NO_MEMORY;
368 * Some services use a rpcint binding handle in their initialization,
369 * before the server is fully initialized. Search the NCALRPC endpoint
370 * with and without endpoint
372 status = dcerpc_parse_binding(tmp_ctx, "ncalrpc:", &binding);
373 if (!NT_STATUS_IS_OK(status)) {
374 goto out;
377 status = dcesrv_find_endpoint(dce_ctx, binding, ep);
378 if (NT_STATUS_IS_OK(status)) {
379 goto out;
382 status = dcerpc_parse_binding(tmp_ctx, "ncalrpc:[WINBIND]", &binding);
383 if (!NT_STATUS_IS_OK(status)) {
384 goto out;
387 status = dcesrv_find_endpoint(dce_ctx, binding, ep);
388 if (NT_STATUS_IS_OK(status)) {
389 goto out;
392 status = dcerpc_parse_binding(tmp_ctx, "ncalrpc:[DEFAULT]", &binding);
393 if (!NT_STATUS_IS_OK(status)) {
394 goto out;
397 status = dcesrv_find_endpoint(dce_ctx, binding, ep);
398 if (!NT_STATUS_IS_OK(status)) {
399 goto out;
402 out:
403 talloc_free(tmp_ctx);
404 return status;
407 static NTSTATUS make_internal_dcesrv_connection(TALLOC_CTX *mem_ctx,
408 const struct ndr_interface_table *ndr_table,
409 struct dcerpc_ncacn_conn *ncacn_conn,
410 struct dcesrv_connection **_out)
412 struct dcesrv_connection *conn = NULL;
413 struct dcesrv_connection_context *context = NULL;
414 struct dcesrv_endpoint *endpoint = NULL;
415 NTSTATUS status;
417 conn = talloc_zero(mem_ctx, struct dcesrv_connection);
418 if (conn == NULL) {
419 return NT_STATUS_NO_MEMORY;
421 conn->dce_ctx = global_dcesrv_context();
422 conn->preferred_transfer = &ndr_transfer_syntax_ndr;
423 conn->transport.private_data = ncacn_conn;
425 status = find_ncalrpc_default_endpoint(conn->dce_ctx, &endpoint);
426 if (!NT_STATUS_IS_OK(status)) {
427 goto fail;
429 conn->endpoint = endpoint;
431 conn->default_auth_state = talloc_zero(conn, struct dcesrv_auth);
432 if (conn->default_auth_state == NULL) {
433 status = NT_STATUS_NO_MEMORY;
434 goto fail;
436 conn->default_auth_state->auth_finished = true;
438 context = talloc_zero(conn, struct dcesrv_connection_context);
439 if (context == NULL) {
440 status = NT_STATUS_NO_MEMORY;
441 goto fail;
443 context->conn = conn;
444 context->context_id = 0;
445 context->transfer_syntax = *(conn->preferred_transfer);
446 context->iface = find_interface_by_syntax_id(
447 conn->endpoint, &ndr_table->syntax_id);
448 if (context->iface == NULL) {
449 status = NT_STATUS_RPC_INTERFACE_NOT_FOUND;
450 goto fail;
453 DLIST_ADD(conn->contexts, context);
455 *_out = conn;
457 return NT_STATUS_OK;
458 fail:
459 talloc_free(conn);
460 return status;
463 static NTSTATUS set_remote_addresses(struct dcesrv_connection *conn,
464 int sock)
466 struct sockaddr_storage st = { 0 };
467 struct sockaddr *sar = (struct sockaddr *)&st;
468 struct tsocket_address *remote = NULL;
469 struct tsocket_address *local = NULL;
470 socklen_t sa_len = sizeof(st);
471 NTSTATUS status;
472 int ret;
474 ZERO_STRUCT(st);
475 ret = getpeername(sock, sar, &sa_len);
476 if (ret != 0) {
477 status = map_nt_error_from_unix(ret);
478 DBG_ERR("getpeername failed: %s\n", nt_errstr(status));
479 return status;
482 ret = tsocket_address_bsd_from_sockaddr(conn, sar, sa_len, &remote);
483 if (ret != 0) {
484 status = map_nt_error_from_unix(ret);
485 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
486 nt_errstr(status));
487 return status;
490 ZERO_STRUCT(st);
491 ret = getsockname(sock, sar, &sa_len);
492 if (ret != 0) {
493 status = map_nt_error_from_unix(ret);
494 DBG_ERR("getsockname failed: %s\n", nt_errstr(status));
495 return status;
498 ret = tsocket_address_bsd_from_sockaddr(conn, sar, sa_len, &local);
499 if (ret != 0) {
500 status = map_nt_error_from_unix(ret);
501 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
502 nt_errstr(status));
503 return status;
506 conn->local_address = talloc_move(conn, &local);
507 conn->remote_address = talloc_move(conn, &remote);
509 return NT_STATUS_OK;
512 /* initialise a wbint binding handle */
513 struct dcerpc_binding_handle *wbint_binding_handle(TALLOC_CTX *mem_ctx,
514 struct winbindd_domain *domain,
515 struct winbindd_child *child)
517 struct dcerpc_binding_handle *h = NULL;
518 struct wbint_bh_state *hs = NULL;
519 struct dcerpc_binding *b = NULL;
520 NTSTATUS status;
522 h = dcerpc_binding_handle_create(mem_ctx,
523 &wbint_bh_ops,
524 NULL,
525 &ndr_table_winbind,
526 &hs,
527 struct wbint_bh_state,
528 __location__);
529 if (h == NULL) {
530 return NULL;
532 hs->domain = domain;
533 hs->child = child;
535 status = dcerpc_parse_binding(hs, "", &b);
536 if (!NT_STATUS_IS_OK(status)) {
537 TALLOC_FREE(h);
538 return NULL;
540 status = dcerpc_binding_set_transport(b, NCACN_INTERNAL);
541 if (!NT_STATUS_IS_OK(status)) {
542 TALLOC_FREE(h);
543 return NULL;
545 status = dcerpc_binding_set_string_option(b, "host", "localhost");
546 if (!NT_STATUS_IS_OK(status)) {
547 TALLOC_FREE(h);
548 return NULL;
550 status = dcerpc_binding_set_string_option(b,
551 "endpoint",
552 "winbindd_dual_ndrcmd");
553 if (!NT_STATUS_IS_OK(status)) {
554 TALLOC_FREE(h);
555 return NULL;
557 status = dcerpc_binding_set_abstract_syntax(b,
558 &ndr_table_winbind.syntax_id);
559 if (!NT_STATUS_IS_OK(status)) {
560 TALLOC_FREE(h);
561 return NULL;
564 hs->binding = b;
566 return h;
569 enum winbindd_result winbindd_dual_ndrcmd(struct winbindd_domain *domain,
570 struct winbindd_cli_state *state)
572 struct dcerpc_ncacn_conn *ncacn_conn = NULL;
573 struct dcesrv_connection *dcesrv_conn = NULL;
574 struct dcesrv_call_state *dcesrv_call = NULL;
575 struct data_blob_list_item *rep = NULL;
576 struct dcesrv_context_callbacks *cb = NULL;
577 uint32_t opnum = state->request->data.ndrcmd;
578 TALLOC_CTX *mem_ctx;
579 NTSTATUS status;
581 DBG_DEBUG("Running command %s (domain '%s')\n",
582 ndr_table_winbind.calls[opnum].name,
583 domain ? domain->name : "(null)");
585 mem_ctx = talloc_stackframe();
586 if (mem_ctx == NULL) {
587 DBG_ERR("No memory\n");
588 return WINBINDD_ERROR;
591 status = make_internal_ncacn_conn(mem_ctx,
592 &ndr_table_winbind,
593 &ncacn_conn);
594 if (!NT_STATUS_IS_OK(status)) {
595 goto out;
598 status = make_internal_dcesrv_connection(ncacn_conn,
599 &ndr_table_winbind,
600 ncacn_conn,
601 &dcesrv_conn);
602 if (!NT_STATUS_IS_OK(status)) {
603 goto out;
606 status = set_remote_addresses(dcesrv_conn, state->sock);
607 if (!NT_STATUS_IS_OK(status)) {
608 goto out;
611 dcesrv_call = talloc_zero(dcesrv_conn, struct dcesrv_call_state);
612 if (dcesrv_call == NULL) {
613 status = NT_STATUS_NO_MEMORY;
614 goto out;
617 dcesrv_call->conn = dcesrv_conn;
618 dcesrv_call->context = dcesrv_conn->contexts;
619 dcesrv_call->auth_state = dcesrv_conn->default_auth_state;
621 ZERO_STRUCT(dcesrv_call->pkt);
622 dcesrv_call->pkt.u.bind.assoc_group_id = 0;
624 cb = dcesrv_call->conn->dce_ctx->callbacks;
625 status = cb->assoc_group.find(
626 dcesrv_call, cb->assoc_group.private_data);
627 if (!NT_STATUS_IS_OK(status)) {
628 goto out;
631 ZERO_STRUCT(dcesrv_call->pkt);
632 dcesrv_call->pkt.u.request.opnum = opnum;
633 dcesrv_call->pkt.u.request.context_id = 0;
634 dcesrv_call->pkt.u.request.stub_and_verifier =
635 data_blob_const(state->request->extra_data.data,
636 state->request->extra_len);
638 status = dcesrv_call_dispatch_local(dcesrv_call);
639 if (!NT_STATUS_IS_OK(status)) {
640 goto out;
643 rep = dcesrv_call->replies;
644 DLIST_REMOVE(dcesrv_call->replies, rep);
646 state->response->extra_data.data = talloc_steal(state->mem_ctx,
647 rep->blob.data);
648 state->response->length += rep->blob.length;
650 talloc_free(rep);
652 out:
653 talloc_free(mem_ctx);
654 if (NT_STATUS_IS_OK(status)) {
655 return WINBINDD_OK;
657 return WINBINDD_ERROR;