summaryrefslogtreecommitdiffstats
path: root/source3/winbindd/winbindd_dual_ndr.c
blob: 7835bd32123967d056eff5929d0bf042e5284be2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
   Unix SMB/CIFS implementation.

   Provide parent->child communication based on NDR marshalling

   Copyright (C) Volker Lendecke 2009

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * This file implements an RPC between winbind parent and child processes,
 * leveraging the autogenerated marshalling routines for MSRPC. This is not
 * MSRPC, as it does not go through the whole DCERPC fragmentation, we just
 * leverage much the same infrastructure we already have for it.
 */

#include "includes.h"
#include "winbindd/winbindd.h"
#include "winbindd/winbindd_proto.h"
#include "ntdomain.h"
#include "librpc/rpc/dcesrv_core.h"
#include "librpc/gen_ndr/ndr_winbind.h"
#include "rpc_server/rpc_config.h"
#include "rpc_server/rpc_server.h"
#include "rpc_dce.h"
#include "lib/tsocket/tsocket.h"

struct wbint_bh_state {
	struct winbindd_domain *domain;
	struct winbindd_child *child;
};

static bool wbint_bh_is_connected(struct dcerpc_binding_handle *h)
{
	struct wbint_bh_state *hs = dcerpc_binding_handle_data(h,
				     struct wbint_bh_state);

	if ((hs->domain == NULL) && (hs->child == NULL)) {
		return false;
	}

	return true;
}

static uint32_t wbint_bh_set_timeout(struct dcerpc_binding_handle *h,
				     uint32_t timeout)
{
	/* TODO: implement timeouts */
	return UINT32_MAX;
}

struct wbint_bh_raw_call_state {
	struct winbindd_domain *domain;
	uint32_t opnum;
	DATA_BLOB in_data;
	struct winbindd_request request;
	struct winbindd_response *response;
	DATA_BLOB out_data;
};

static void wbint_bh_raw_call_child_done(struct tevent_req *subreq);
static void wbint_bh_raw_call_domain_done(struct tevent_req *subreq);

static struct tevent_req *wbint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
						  struct tevent_context *ev,
						  struct dcerpc_binding_handle *h,
						  const struct GUID *object,
						  uint32_t opnum,
						  uint32_t in_flags,
						  const uint8_t *in_data,
						  size_t in_length)
{
	struct wbint_bh_state *hs =
		dcerpc_binding_handle_data(h,
		struct wbint_bh_state);
	struct tevent_req *req;
	struct wbint_bh_raw_call_state *state;
	bool ok;
	struct tevent_req *subreq;

	req = tevent_req_create(mem_ctx, &state,
				struct wbint_bh_raw_call_state);
	if (req == NULL) {
		return NULL;
	}
	state->domain = hs->domain;
	state->opnum = opnum;
	state->in_data.data = discard_const_p(uint8_t, in_data);
	state->in_data.length = in_length;

	ok = wbint_bh_is_connected(h);
	if (!ok) {
		tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
		return tevent_req_post(req, ev);
	}

	if ((state->domain != NULL)
	    && wcache_fetch_ndr(state, state->domain, state->opnum,
				&state->in_data, &state->out_data)) {
		DBG_DEBUG("Got opnum %"PRIu32" for domain %s from cache\n",
			  state->opnum, state->domain->name);
		tevent_req_done(req);
		return tevent_req_post(req, ev);
	}

	state->request.cmd = WINBINDD_DUAL_NDRCMD;
	state->request.data.ndrcmd = state->opnum;
	state->request.extra_data.data = (char *)state->in_data.data;
	state->request.extra_len = state->in_data.length;

	if (hs->child != NULL) {
		subreq = wb_child_request_send(state, ev, hs->child,
					       &state->request);
		if (tevent_req_nomem(subreq, req)) {
			return tevent_req_post(req, ev);
		}
		tevent_req_set_callback(
			subreq, wbint_bh_raw_call_child_done, req);
		return req;
	}

	subreq = wb_domain_request_send(state, ev, hs->domain,
					&state->request);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, wbint_bh_raw_call_domain_done, req);

	return req;
}

static void wbint_bh_raw_call_child_done(struct tevent_req *subreq)
{
	struct tevent_req *req =
		tevent_req_callback_data(subreq,
		struct tevent_req);
	struct wbint_bh_raw_call_state *state =
		tevent_req_data(req,
		struct wbint_bh_raw_call_state);
	int ret, err;

	ret = wb_child_request_recv(subreq, state, &state->response, &err);
	TALLOC_FREE(subreq);
	if (ret == -1) {
		NTSTATUS status = map_nt_error_from_unix(err);
		tevent_req_nterror(req, status);
		return;
	}

	state->out_data = data_blob_talloc(state,
		state->response->extra_data.data,
		state->response->length - sizeof(struct winbindd_response));
	if (state->response->extra_data.data && !state->out_data.data) {
		tevent_req_oom(req);
		return;
	}

	if (state->domain != NULL) {
		wcache_store_ndr(state->domain, state->opnum,
				 &state->in_data, &state->out_data);
	}

	tevent_req_done(req);
}

static void wbint_bh_raw_call_domain_done(struct tevent_req *subreq)
{
	struct tevent_req *req =
		tevent_req_callback_data(subreq,
		struct tevent_req);
	struct wbint_bh_raw_call_state *state =
		tevent_req_data(req,
		struct wbint_bh_raw_call_state);
	int ret, err;

	ret = wb_domain_request_recv(subreq, state, &state->response, &err);
	TALLOC_FREE(subreq);
	if (ret == -1) {
		NTSTATUS status = map_nt_error_from_unix(err);
		tevent_req_nterror(req, status);
		return;
	}

	state->out_data = data_blob_talloc(state,
		state->response->extra_data.data,
		state->response->length - sizeof(struct winbindd_response));
	if (state->response->extra_data.data && !state->out_data.data) {
		tevent_req_oom(req);
		return;
	}

	if (state->domain != NULL) {
		wcache_store_ndr(state->domain, state->opnum,
				 &state->in_data, &state->out_data);
	}

	tevent_req_done(req);
}

static NTSTATUS wbint_bh_raw_call_recv(struct tevent_req *req,
					TALLOC_CTX *mem_ctx,
					uint8_t **out_data,
					size_t *out_length,
					uint32_t *out_flags)
{
	struct wbint_bh_raw_call_state *state =
		tevent_req_data(req,
		struct wbint_bh_raw_call_state);
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status)) {
		tevent_req_received(req);
		return status;
	}

	*out_data = talloc_move(mem_ctx, &state->out_data.data);
	*out_length = state->out_data.length;
	*out_flags = 0;
	tevent_req_received(req);
	return NT_STATUS_OK;
}

struct wbint_bh_disconnect_state {
	uint8_t _dummy;
};

static struct tevent_req *wbint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
						struct tevent_context *ev,
						struct dcerpc_binding_handle *h)
{
	struct wbint_bh_state *hs = dcerpc_binding_handle_data(h,
				     struct wbint_bh_state);
	struct tevent_req *req;
	struct wbint_bh_disconnect_state *state;
	bool ok;

	req = tevent_req_create(mem_ctx, &state,
				struct wbint_bh_disconnect_state);
	if (req == NULL) {
		return NULL;
	}

	ok = wbint_bh_is_connected(h);
	if (!ok) {
		tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
		return tevent_req_post(req, ev);
	}

	/*
	 * TODO: do a real async disconnect ...
	 */
	hs->domain = NULL;
	hs->child = NULL;

	tevent_req_done(req);
	return tevent_req_post(req, ev);
}

static NTSTATUS wbint_bh_disconnect_recv(struct tevent_req *req)
{
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status)) {
		tevent_req_received(req);
		return status;
	}

	tevent_req_received(req);
	return NT_STATUS_OK;
}

static bool wbint_bh_ref_alloc(struct dcerpc_binding_handle *h)
{
	return true;
}

static void wbint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
				  ndr_flags_type ndr_flags,
				  const void *_struct_ptr,
				  const struct ndr_interface_call *call)
{
	void *struct_ptr = discard_const(_struct_ptr);

	if (DEBUGLEVEL < 10) {
		return;
	}

	if (ndr_flags & NDR_IN) {
		ndr_print_function_debug(call->ndr_print,
					 call->name,
					 ndr_flags,
					 struct_ptr);
	}
	if (ndr_flags & NDR_OUT) {
		ndr_print_function_debug(call->ndr_print,
					 call->name,
					 ndr_flags,
					 struct_ptr);
	}
}

static const struct dcerpc_binding_handle_ops wbint_bh_ops = {
	.name			= "wbint",
	.is_connected		= wbint_bh_is_connected,
	.set_timeout		= wbint_bh_set_timeout,
	.raw_call_send		= wbint_bh_raw_call_send,
	.raw_call_recv		= wbint_bh_raw_call_recv,
	.disconnect_send	= wbint_bh_disconnect_send,
	.disconnect_recv	= wbint_bh_disconnect_recv,

	.ref_alloc		= wbint_bh_ref_alloc,
	.do_ndr_print		= wbint_bh_do_ndr_print,
};

static NTSTATUS make_internal_ncacn_conn(TALLOC_CTX *mem_ctx,
				const struct ndr_interface_table *table,
				struct dcerpc_ncacn_conn **_out)
{
	struct dcerpc_ncacn_conn *ncacn_conn = NULL;

	ncacn_conn = talloc_zero(mem_ctx, struct dcerpc_ncacn_conn);
	if (ncacn_conn == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	ncacn_conn->p.mem_ctx = mem_ctx;

	*_out = ncacn_conn;

	return NT_STATUS_OK;
}

static NTSTATUS find_ncalrpc_default_endpoint(struct dcesrv_context *dce_ctx,
					      struct dcesrv_endpoint **ep)
{
	TALLOC_CTX *tmp_ctx = NULL;
	struct dcerpc_binding *binding = NULL;
	NTSTATUS status;

	tmp_ctx = talloc_new(dce_ctx);
	if (tmp_ctx == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	/*
	 * Some services use a rpcint binding handle in their initialization,
	 * before the server is fully initialized. Search the NCALRPC endpoint
	 * with and without endpoint
	 */
	status = dcerpc_parse_binding(tmp_ctx, "ncalrpc:", &binding);
	if (!NT_STATUS_IS_OK(status)) {
		goto out;
	}

	status = dcesrv_find_endpoint(dce_ctx, binding, ep);
	if (NT_STATUS_IS_OK(status)) {
		goto out;
	}

	status = dcerpc_parse_binding(tmp_ctx, "ncalrpc:[WINBIND]", &binding);
	if (!NT_STATUS_IS_OK(status)) {
		goto out;
	}

	status = dcesrv_find_endpoint(dce_ctx, binding, ep);
	if (NT_STATUS_IS_OK(status)) {
		goto out;
	}

	status = dcerpc_parse_binding(tmp_ctx, "ncalrpc:[DEFAULT]", &binding);
	if (!NT_STATUS_IS_OK(status)) {
		goto out;
	}

	status = dcesrv_find_endpoint(dce_ctx, binding, ep);
	if (!NT_STATUS_IS_OK(status)) {
		goto out;
	}

out:
	talloc_free(tmp_ctx);
	return status;
}

static NTSTATUS make_internal_dcesrv_connection(TALLOC_CTX *mem_ctx,
				const struct ndr_interface_table *ndr_table,
				struct dcerpc_ncacn_conn *ncacn_conn,
				struct dcesrv_connection **_out)
{
	struct dcesrv_connection *conn = NULL;
	struct dcesrv_connection_context *context = NULL;
	struct dcesrv_endpoint *endpoint = NULL;
	NTSTATUS status;

	conn = talloc_zero(mem_ctx, struct dcesrv_connection);
	if (conn == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	conn->dce_ctx = global_dcesrv_context();
	conn->preferred_transfer = &ndr_transfer_syntax_ndr;
	conn->transport.private_data = ncacn_conn;

	status = find_ncalrpc_default_endpoint(conn->dce_ctx, &endpoint);
	if (!NT_STATUS_IS_OK(status)) {
		goto fail;
	}
	conn->endpoint = endpoint;

	conn->default_auth_state = talloc_zero(conn, struct dcesrv_auth);
	if (conn->default_auth_state == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}
	conn->default_auth_state->auth_finished = true;

	context = talloc_zero(conn, struct dcesrv_connection_context);
	if (context == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}
	context->conn = conn;
	context->context_id = 0;
	context->transfer_syntax = *(conn->preferred_transfer);
	context->iface = find_interface_by_syntax_id(
		conn->endpoint, &ndr_table->syntax_id);
	if (context->iface == NULL) {
		status = NT_STATUS_RPC_INTERFACE_NOT_FOUND;
		goto fail;
	}

	DLIST_ADD(conn->contexts, context);

	*_out = conn;

	return NT_STATUS_OK;
fail:
	talloc_free(conn);
	return status;
}

static NTSTATUS set_remote_addresses(struct dcesrv_connection *conn,
				     int sock)
{
	struct sockaddr_storage st = { 0 };
	struct sockaddr *sar = (struct sockaddr *)&st;
	struct tsocket_address *remote = NULL;
	struct tsocket_address *local = NULL;
	socklen_t sa_len = sizeof(st);
	NTSTATUS status;
	int ret;

	ZERO_STRUCT(st);
	ret = getpeername(sock, sar, &sa_len);
	if (ret != 0) {
		status = map_nt_error_from_unix(ret);
		DBG_ERR("getpeername failed: %s\n", nt_errstr(status));
		return status;
	}

	ret = tsocket_address_bsd_from_sockaddr(conn, sar, sa_len, &remote);
	if (ret != 0) {
		status = map_nt_error_from_unix(ret);
		DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
			nt_errstr(status));
		return status;
	}

	ZERO_STRUCT(st);
	ret = getsockname(sock, sar, &sa_len);
	if (ret != 0) {
		status = map_nt_error_from_unix(ret);
		DBG_ERR("getsockname failed: %s\n", nt_errstr(status));
		return status;
	}

	ret = tsocket_address_bsd_from_sockaddr(conn, sar, sa_len, &local);
	if (ret != 0) {
		status = map_nt_error_from_unix(ret);
		DBG_ERR("tsocket_address_bsd_from_sockaddr failed: %s\n",
			nt_errstr(status));
		return status;
	}

	conn->local_address = talloc_move(conn, &local);
	conn->remote_address = talloc_move(conn, &remote);

	return NT_STATUS_OK;
}

/* initialise a wbint binding handle */
struct dcerpc_binding_handle *wbint_binding_handle(TALLOC_CTX *mem_ctx,
						struct winbindd_domain *domain,
						struct winbindd_child *child)
{
	struct dcerpc_binding_handle *h;
	struct wbint_bh_state *hs;

	h = dcerpc_binding_handle_create(mem_ctx,
					 &wbint_bh_ops,
					 NULL,
					 &ndr_table_winbind,
					 &hs,
					 struct wbint_bh_state,
					 __location__);
	if (h == NULL) {
		return NULL;
	}
	hs->domain = domain;
	hs->child = child;

	return h;
}

enum winbindd_result winbindd_dual_ndrcmd(struct winbindd_domain *domain,
					  struct winbindd_cli_state *state)
{
	struct dcerpc_ncacn_conn *ncacn_conn = NULL;
	struct dcesrv_connection *dcesrv_conn = NULL;
	struct dcesrv_call_state *dcesrv_call = NULL;
	struct data_blob_list_item *rep = NULL;
	struct dcesrv_context_callbacks *cb = NULL;
	uint32_t opnum = state->request->data.ndrcmd;
	TALLOC_CTX *mem_ctx;
	NTSTATUS status;

	DBG_DEBUG("Running command %s (domain '%s')\n",
		  ndr_table_winbind.calls[opnum].name,
		  domain ? domain->name : "(null)");

	mem_ctx = talloc_stackframe();
	if (mem_ctx == NULL) {
		DBG_ERR("No memory\n");
		return WINBINDD_ERROR;
	}

	status = make_internal_ncacn_conn(mem_ctx,
					  &ndr_table_winbind,
					  &ncacn_conn);
	if (!NT_STATUS_IS_OK(status)) {
		goto out;
	}

	status = make_internal_dcesrv_connection(ncacn_conn,
						 &ndr_table_winbind,
						 ncacn_conn,
						 &dcesrv_conn);
	if (!NT_STATUS_IS_OK(status)) {
		goto out;
	}

	status = set_remote_addresses(dcesrv_conn, state->sock);
	if (!NT_STATUS_IS_OK(status)) {
		goto out;
	}

	dcesrv_call = talloc_zero(dcesrv_conn, struct dcesrv_call_state);
	if (dcesrv_call == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto out;
	}

	dcesrv_call->conn = dcesrv_conn;
	dcesrv_call->context = dcesrv_conn->contexts;
	dcesrv_call->auth_state = dcesrv_conn->default_auth_state;

	ZERO_STRUCT(dcesrv_call->pkt);
	dcesrv_call->pkt.u.bind.assoc_group_id = 0;

	cb = dcesrv_call->conn->dce_ctx->callbacks;
	status = cb->assoc_group.find(
		dcesrv_call, cb->assoc_group.private_data);
	if (!NT_STATUS_IS_OK(status)) {
		goto out;
	}

	ZERO_STRUCT(dcesrv_call->pkt);
	dcesrv_call->pkt.u.request.opnum = opnum;
	dcesrv_call->pkt.u.request.context_id = 0;
	dcesrv_call->pkt.u.request.stub_and_verifier =
		data_blob_const(state->request->extra_data.data,
				state->request->extra_len);

	status = dcesrv_call_dispatch_local(dcesrv_call);
	if (!NT_STATUS_IS_OK(status)) {
		goto out;
	}

	rep = dcesrv_call->replies;
	DLIST_REMOVE(dcesrv_call->replies, rep);

	state->response->extra_data.data = talloc_steal(state->mem_ctx,
							rep->blob.data);
	state->response->length += rep->blob.length;

	talloc_free(rep);

out:
	talloc_free(mem_ctx);
	if (NT_STATUS_IS_OK(status)) {
		return WINBINDD_OK;
	}
	return WINBINDD_ERROR;
}