util:datablob: data_blob_pad checks its alignment assumption
[samba.git] / source3 / winbindd / winbindd_dual_ndr.c
blob4fd7fb7c8c3971399e5fe35b34ed12a48cf3f627
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 samba_sockaddr ssa;
467 struct tsocket_address *remote = NULL;
468 struct tsocket_address *local = NULL;
469 NTSTATUS status;
470 int ret;
472 ssa = (struct samba_sockaddr) { .sa_socklen = sizeof(ssa.u.ss), };
473 ret = getpeername(sock, &ssa.u.sa, &ssa.sa_socklen);
474 if (ret != 0) {
475 status = map_nt_error_from_unix(ret);
476 DBG_ERR("getpeername failed: %s\n", nt_errstr(status));
477 return status;
480 ret = tsocket_address_bsd_from_sockaddr(conn,
481 &ssa.u.sa,
482 ssa.sa_socklen,
483 &remote);
484 if (ret != 0) {
485 status = map_nt_error_from_unix(ret);
486 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
487 nt_errstr(status));
488 return status;
491 ssa = (struct samba_sockaddr) { .sa_socklen = sizeof(ssa.u.ss), };
492 ret = getsockname(sock, &ssa.u.sa, &ssa.sa_socklen);
493 if (ret != 0) {
494 status = map_nt_error_from_unix(ret);
495 DBG_ERR("getsockname failed: %s\n", nt_errstr(status));
496 return status;
499 ret = tsocket_address_bsd_from_sockaddr(conn,
500 &ssa.u.sa,
501 ssa.sa_socklen,
502 &local);
503 if (ret != 0) {
504 status = map_nt_error_from_unix(ret);
505 DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
506 nt_errstr(status));
507 return status;
510 conn->local_address = talloc_move(conn, &local);
511 conn->remote_address = talloc_move(conn, &remote);
513 return NT_STATUS_OK;
516 /* initialise a wbint binding handle */
517 struct dcerpc_binding_handle *wbint_binding_handle(TALLOC_CTX *mem_ctx,
518 struct winbindd_domain *domain,
519 struct winbindd_child *child)
521 struct dcerpc_binding_handle *h = NULL;
522 struct wbint_bh_state *hs = NULL;
523 struct dcerpc_binding *b = NULL;
524 NTSTATUS status;
526 h = dcerpc_binding_handle_create(mem_ctx,
527 &wbint_bh_ops,
528 NULL,
529 &ndr_table_winbind,
530 &hs,
531 struct wbint_bh_state,
532 __location__);
533 if (h == NULL) {
534 return NULL;
536 hs->domain = domain;
537 hs->child = child;
539 status = dcerpc_parse_binding(hs, "", &b);
540 if (!NT_STATUS_IS_OK(status)) {
541 TALLOC_FREE(h);
542 return NULL;
544 status = dcerpc_binding_set_transport(b, NCACN_INTERNAL);
545 if (!NT_STATUS_IS_OK(status)) {
546 TALLOC_FREE(h);
547 return NULL;
549 status = dcerpc_binding_set_string_option(b, "host", "localhost");
550 if (!NT_STATUS_IS_OK(status)) {
551 TALLOC_FREE(h);
552 return NULL;
554 status = dcerpc_binding_set_string_option(b,
555 "endpoint",
556 "winbindd_dual_ndrcmd");
557 if (!NT_STATUS_IS_OK(status)) {
558 TALLOC_FREE(h);
559 return NULL;
561 status = dcerpc_binding_set_abstract_syntax(b,
562 &ndr_table_winbind.syntax_id);
563 if (!NT_STATUS_IS_OK(status)) {
564 TALLOC_FREE(h);
565 return NULL;
568 hs->binding = b;
570 return h;
573 enum winbindd_result winbindd_dual_ndrcmd(struct winbindd_domain *domain,
574 struct winbindd_cli_state *state)
576 struct dcerpc_ncacn_conn *ncacn_conn = NULL;
577 struct dcesrv_connection *dcesrv_conn = NULL;
578 struct dcesrv_call_state *dcesrv_call = NULL;
579 struct data_blob_list_item *rep = NULL;
580 struct dcesrv_context_callbacks *cb = NULL;
581 uint32_t opnum = state->request->data.ndrcmd;
582 TALLOC_CTX *mem_ctx;
583 NTSTATUS status;
585 DBG_DEBUG("Running command %s (domain '%s')\n",
586 ndr_table_winbind.calls[opnum].name,
587 domain ? domain->name : "(null)");
589 mem_ctx = talloc_stackframe();
590 if (mem_ctx == NULL) {
591 DBG_ERR("No memory\n");
592 return WINBINDD_ERROR;
595 status = make_internal_ncacn_conn(mem_ctx,
596 &ndr_table_winbind,
597 &ncacn_conn);
598 if (!NT_STATUS_IS_OK(status)) {
599 goto out;
602 status = make_internal_dcesrv_connection(ncacn_conn,
603 &ndr_table_winbind,
604 ncacn_conn,
605 &dcesrv_conn);
606 if (!NT_STATUS_IS_OK(status)) {
607 goto out;
610 status = set_remote_addresses(dcesrv_conn, state->sock);
611 if (!NT_STATUS_IS_OK(status)) {
612 goto out;
615 dcesrv_call = talloc_zero(dcesrv_conn, struct dcesrv_call_state);
616 if (dcesrv_call == NULL) {
617 status = NT_STATUS_NO_MEMORY;
618 goto out;
621 dcesrv_call->conn = dcesrv_conn;
622 dcesrv_call->context = dcesrv_conn->contexts;
623 dcesrv_call->auth_state = dcesrv_conn->default_auth_state;
625 ZERO_STRUCT(dcesrv_call->pkt);
626 dcesrv_call->pkt.u.bind.assoc_group_id = 0;
628 cb = dcesrv_call->conn->dce_ctx->callbacks;
629 status = cb->assoc_group.find(
630 dcesrv_call, cb->assoc_group.private_data);
631 if (!NT_STATUS_IS_OK(status)) {
632 goto out;
635 ZERO_STRUCT(dcesrv_call->pkt);
636 dcesrv_call->pkt.u.request.opnum = opnum;
637 dcesrv_call->pkt.u.request.context_id = 0;
638 dcesrv_call->pkt.u.request.stub_and_verifier =
639 data_blob_const(state->request->extra_data.data,
640 state->request->extra_len);
642 status = dcesrv_call_dispatch_local(dcesrv_call);
643 if (!NT_STATUS_IS_OK(status)) {
644 goto out;
647 rep = dcesrv_call->replies;
648 DLIST_REMOVE(dcesrv_call->replies, rep);
650 state->response->extra_data.data = talloc_steal(state->mem_ctx,
651 rep->blob.data);
652 state->response->length += rep->blob.length;
654 talloc_free(rep);
656 out:
657 talloc_free(mem_ctx);
658 if (NT_STATUS_IS_OK(status)) {
659 return WINBINDD_OK;
661 return WINBINDD_ERROR;