summaryrefslogtreecommitdiff
path: root/librpc/rpc/binding_handle.c
blob: 3c74fdfdb17f02abc4e88ed70159dabe30f1e35f (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
/*
   Unix SMB/CIFS implementation.

   dcerpc binding handle functions

   Copyright (C) Stefan Metzmacher 2010

   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/>.
*/

#include "includes.h"
#include <tevent.h>
#include "../lib/util/tevent_ntstatus.h"
#include "librpc/rpc/dcerpc.h"
#include "rpc_common.h"

struct dcerpc_binding_handle {
	void *private_data;
	const struct dcerpc_binding_handle_ops *ops;
	const char *location;
	const struct GUID *object;
	const struct ndr_interface_table *table;
	struct tevent_context *sync_ev;
};

static int dcerpc_binding_handle_destructor(struct dcerpc_binding_handle *b)
{
	return 0;
}

struct dcerpc_binding_handle *_dcerpc_binding_handle_create(TALLOC_CTX *mem_ctx,
					const struct dcerpc_binding_handle_ops *ops,
					const struct GUID *object,
					const struct ndr_interface_table *table,
					void *pstate,
					size_t psize,
					const char *type,
					const char *location)
{
	struct dcerpc_binding_handle *h;
	void **ppstate = (void **)pstate;
	void *state;

	h = talloc_zero(mem_ctx, struct dcerpc_binding_handle);
	if (h == NULL) {
		return NULL;
	}
	h->ops		= ops;
	h->location	= location;
	h->object	= object;
	h->table	= table;

	state = talloc_zero_size(h, psize);
	if (state == NULL) {
		talloc_free(h);
		return NULL;
	}
	talloc_set_name_const(state, type);

	h->private_data = state;

	talloc_set_destructor(h, dcerpc_binding_handle_destructor);

	*ppstate = state;
	return h;
}

void *_dcerpc_binding_handle_data(struct dcerpc_binding_handle *h)
{
	return h->private_data;
}

void dcerpc_binding_handle_set_sync_ev(struct dcerpc_binding_handle *h,
				       struct tevent_context *ev)
{
	h->sync_ev = ev;
}

bool dcerpc_binding_handle_is_connected(struct dcerpc_binding_handle *h)
{
	return h->ops->is_connected(h);
}

uint32_t dcerpc_binding_handle_set_timeout(struct dcerpc_binding_handle *h,
					   uint32_t timeout)
{
	return h->ops->set_timeout(h, timeout);
}

struct dcerpc_binding_handle_raw_call_state {
	const struct dcerpc_binding_handle_ops *ops;
	uint8_t *out_data;
	size_t out_length;
	uint32_t out_flags;
};

static void dcerpc_binding_handle_raw_call_done(struct tevent_req *subreq);

struct tevent_req *dcerpc_binding_handle_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 tevent_req *req;
	struct dcerpc_binding_handle_raw_call_state *state;
	struct tevent_req *subreq;

	req = tevent_req_create(mem_ctx, &state,
				struct dcerpc_binding_handle_raw_call_state);
	if (req == NULL) {
		return NULL;
	}
	state->ops = h->ops;
	state->out_data = NULL;
	state->out_length = 0;
	state->out_flags = 0;

	subreq = state->ops->raw_call_send(state, ev, h,
					   object, opnum,
					   in_flags, in_data, in_length);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, dcerpc_binding_handle_raw_call_done, req);

	return req;
}

static void dcerpc_binding_handle_raw_call_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(subreq,
				 struct tevent_req);
	struct dcerpc_binding_handle_raw_call_state *state =
		tevent_req_data(req,
		struct dcerpc_binding_handle_raw_call_state);
	NTSTATUS error;

	error = state->ops->raw_call_recv(subreq, state,
					  &state->out_data,
					  &state->out_length,
					  &state->out_flags);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(error)) {
		tevent_req_nterror(req, error);
		return;
	}

	tevent_req_done(req);
}

NTSTATUS dcerpc_binding_handle_raw_call_recv(struct tevent_req *req,
					     TALLOC_CTX *mem_ctx,
					     uint8_t **out_data,
					     size_t *out_length,
					     uint32_t *out_flags)
{
	struct dcerpc_binding_handle_raw_call_state *state =
		tevent_req_data(req,
		struct dcerpc_binding_handle_raw_call_state);
	NTSTATUS error;

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

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

NTSTATUS dcerpc_binding_handle_raw_call(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,
					TALLOC_CTX *mem_ctx,
					uint8_t **out_data,
					size_t *out_length,
					uint32_t *out_flags)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct tevent_context *ev;
	struct tevent_req *subreq;
	NTSTATUS status;

	/*
	 * TODO: allow only one sync call
	 */

	if (h->sync_ev) {
		ev = h->sync_ev;
	} else {
		ev = samba_tevent_context_init(frame);
	}
	if (ev == NULL) {
		talloc_free(frame);
		return NT_STATUS_NO_MEMORY;
	}

	subreq = dcerpc_binding_handle_raw_call_send(frame, ev,
						     h, object, opnum,
						     in_flags,
						     in_data,
						     in_length);
	if (subreq == NULL) {
		talloc_free(frame);
		return NT_STATUS_NO_MEMORY;
	}

	if (!tevent_req_poll(subreq, ev)) {
		status = map_nt_error_from_unix_common(errno);
		talloc_free(frame);
		return status;
	}

	status = dcerpc_binding_handle_raw_call_recv(subreq,
						     mem_ctx,
						     out_data,
						     out_length,
						     out_flags);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(frame);
		return status;
	}

	TALLOC_FREE(frame);
	return NT_STATUS_OK;
}

struct dcerpc_binding_handle_disconnect_state {
	const struct dcerpc_binding_handle_ops *ops;
};

static void dcerpc_binding_handle_disconnect_done(struct tevent_req *subreq);

struct tevent_req *dcerpc_binding_handle_disconnect_send(TALLOC_CTX *mem_ctx,
						struct tevent_context *ev,
						struct dcerpc_binding_handle *h)
{
	struct tevent_req *req;
	struct dcerpc_binding_handle_disconnect_state *state;
	struct tevent_req *subreq;

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

	state->ops = h->ops;

	subreq = state->ops->disconnect_send(state, ev, h);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, dcerpc_binding_handle_disconnect_done, req);

	return req;
}

static void dcerpc_binding_handle_disconnect_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(subreq,
				 struct tevent_req);
	struct dcerpc_binding_handle_disconnect_state *state =
		tevent_req_data(req,
		struct dcerpc_binding_handle_disconnect_state);
	NTSTATUS error;

	error = state->ops->disconnect_recv(subreq);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(error)) {
		tevent_req_nterror(req, error);
		return;
	}

	tevent_req_done(req);
}

NTSTATUS dcerpc_binding_handle_disconnect_recv(struct tevent_req *req)
{
	NTSTATUS error;

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

	tevent_req_received(req);
	return NT_STATUS_OK;
}

struct dcerpc_binding_handle_call_state {
	struct dcerpc_binding_handle *h;
	const struct ndr_interface_call *call;
	TALLOC_CTX *r_mem;
	void *r_ptr;
	struct ndr_push *push;
	DATA_BLOB request;
	DATA_BLOB response;
	struct ndr_pull *pull;
};

static void dcerpc_binding_handle_call_done(struct tevent_req *subreq);

struct tevent_req *dcerpc_binding_handle_call_send(TALLOC_CTX *mem_ctx,
					struct tevent_context *ev,
					struct dcerpc_binding_handle *h,
					const struct GUID *object,
					const struct ndr_interface_table *table,
					uint32_t opnum,
					TALLOC_CTX *r_mem,
					void *r_ptr)
{
	struct tevent_req *req;
	struct dcerpc_binding_handle_call_state *state;
	struct tevent_req *subreq;
	enum ndr_err_code ndr_err;

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

#if 0 /* TODO: activate this when the callers are fixed */
	if (table != h->table) {
		tevent_req_nterror(req, NT_STATUS_INVALID_HANDLE);
		return tevent_req_post(req, ev);
	}
#endif

	if (opnum >= table->num_calls) {
		tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
		return tevent_req_post(req, ev);
	}

	state->h = h;
	state->call = &table->calls[opnum];

	state->r_mem = r_mem;
	state->r_ptr = r_ptr;

	/* setup for a ndr_push_* call */
	state->push = ndr_push_init_ctx(state);
	if (tevent_req_nomem(state->push, req)) {
		return tevent_req_post(req, ev);
	}

	if (h->ops->ref_alloc && h->ops->ref_alloc(h)) {
		state->push->flags |= LIBNDR_FLAG_REF_ALLOC;
	}

	if (h->ops->push_bigendian && h->ops->push_bigendian(h)) {
		state->push->flags |= LIBNDR_FLAG_BIGENDIAN;
	}

	if (h->ops->use_ndr64 && h->ops->use_ndr64(h)) {
		state->push->flags |= LIBNDR_FLAG_NDR64;
	}

	if (h->ops->do_ndr_print) {
		h->ops->do_ndr_print(h, NDR_IN | NDR_SET_VALUES,
				     state->r_ptr, state->call);
	}

	/* push the structure into a blob */
	ndr_err = state->call->ndr_push(state->push, NDR_IN, state->r_ptr);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		NTSTATUS error;
		error = ndr_map_error2ntstatus(ndr_err);
		if (h->ops->ndr_push_failed) {
			h->ops->ndr_push_failed(h, error,
						state->r_ptr,
						state->call);
		}
		tevent_req_nterror(req, error);
		return tevent_req_post(req, ev);
	}

	/* retrieve the blob */
	state->request = ndr_push_blob(state->push);

	if (h->ops->ndr_validate_in) {
		NTSTATUS error;
		error = h->ops->ndr_validate_in(h, state,
						&state->request,
						state->call);
		if (!NT_STATUS_IS_OK(error)) {
			tevent_req_nterror(req, error);
			return tevent_req_post(req, ev);
		}
	}

	subreq = dcerpc_binding_handle_raw_call_send(state, ev,
						     h, object, opnum,
						     state->push->flags,
						     state->request.data,
						     state->request.length);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, dcerpc_binding_handle_call_done, req);

	return req;
}

static void dcerpc_binding_handle_call_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(subreq,
				 struct tevent_req);
	struct dcerpc_binding_handle_call_state *state =
		tevent_req_data(req,
		struct dcerpc_binding_handle_call_state);
	struct dcerpc_binding_handle *h = state->h;
	NTSTATUS error;
	uint32_t out_flags = 0;
	enum ndr_err_code ndr_err;

	error = dcerpc_binding_handle_raw_call_recv(subreq, state,
						    &state->response.data,
						    &state->response.length,
						    &out_flags);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(error)) {
		tevent_req_nterror(req, error);
		return;
	}

	state->pull = ndr_pull_init_blob(&state->response, state);
	if (tevent_req_nomem(state->pull, req)) {
		return;
	}
	state->pull->flags = state->push->flags;

	if (out_flags & LIBNDR_FLAG_BIGENDIAN) {
		state->pull->flags |= LIBNDR_FLAG_BIGENDIAN;
	} else {
		state->pull->flags &= ~LIBNDR_FLAG_BIGENDIAN;
	}

	state->pull->current_mem_ctx = state->r_mem;

	/* pull the structure from the blob */
	ndr_err = state->call->ndr_pull(state->pull, NDR_OUT, state->r_ptr);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		error = ndr_map_error2ntstatus(ndr_err);
		if (h->ops->ndr_pull_failed) {
			h->ops->ndr_pull_failed(h, error,
						&state->response,
						state->call);
		}
		tevent_req_nterror(req, error);
		return;
	}

	if (h->ops->do_ndr_print) {
		h->ops->do_ndr_print(h, NDR_OUT,
				     state->r_ptr, state->call);
	}

	if (h->ops->ndr_validate_out) {
		error = h->ops->ndr_validate_out(h,
						 state->pull,
						 state->r_ptr,
						 state->call);
		if (!NT_STATUS_IS_OK(error)) {
			tevent_req_nterror(req, error);
			return;
		}
	}

	tevent_req_done(req);
}

NTSTATUS dcerpc_binding_handle_call_recv(struct tevent_req *req)
{
	return tevent_req_simple_recv_ntstatus(req);
}

NTSTATUS dcerpc_binding_handle_call(struct dcerpc_binding_handle *h,
				    const struct GUID *object,
				    const struct ndr_interface_table *table,
				    uint32_t opnum,
				    TALLOC_CTX *r_mem,
				    void *r_ptr)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct tevent_context *ev;
	struct tevent_req *subreq;
	NTSTATUS status = NT_STATUS_NO_MEMORY;

	/*
	 * TODO: allow only one sync call
	 */

	if (h->sync_ev) {
		ev = h->sync_ev;
	} else {
		ev = samba_tevent_context_init(frame);
	}
	if (ev == NULL) {
		goto fail;
	}

	subreq = dcerpc_binding_handle_call_send(frame, ev,
						 h, object, table,
						 opnum, r_mem, r_ptr);
	if (subreq == NULL) {
		goto fail;
	}

	if (!tevent_req_poll_ntstatus(subreq, ev, &status)) {
		goto fail;
	}

	status = dcerpc_binding_handle_call_recv(subreq);
fail:
	TALLOC_FREE(frame);
	return status;
}