summaryrefslogtreecommitdiff
path: root/source3/librpc/rpc/dcerpc_ep.c
blob: 520460a8539b2636a69daa3059b58e790932099d (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
/*
 *  Endpoint Mapper Functions
 *  DCERPC local endpoint mapper client routines
 *  Copyright (c) 2010-2011 Andreas Schneider.
 *
 *  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 "librpc/rpc/dcerpc.h"
#include "librpc/rpc/dcerpc_ep.h"
#include "../librpc/gen_ndr/ndr_epmapper_c.h"
#include "rpc_client/cli_pipe.h"
#include "auth.h"
#include "rpc_server/rpc_ncacn_np.h"
#include "../lib/tsocket/tsocket.h"

#define EPM_MAX_ANNOTATION_SIZE 64

static bool binding_vector_realloc(struct dcerpc_binding_vector *bvec)
{
	if (bvec->count >= bvec->allocated) {
		struct dcerpc_binding *tmp;

		tmp = talloc_realloc(bvec,
				     bvec->bindings,
				     struct dcerpc_binding,
				     bvec->allocated * 2);
		if (tmp == NULL) {
			return false;
		}
		bvec->bindings = tmp;
		bvec->allocated = bvec->allocated * 2;
	}

	return true;
}

NTSTATUS dcerpc_binding_vector_new(TALLOC_CTX *mem_ctx,
				   struct dcerpc_binding_vector **pbvec)
{
	struct dcerpc_binding_vector *bvec;
	NTSTATUS status;
	TALLOC_CTX *tmp_ctx;

	tmp_ctx = talloc_stackframe();
	if (tmp_ctx == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
	if (bvec == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto done;
	}

	bvec->bindings = talloc_zero_array(bvec,
					   struct dcerpc_binding,
					   4);
	if (bvec->bindings == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto done;
	}

	bvec->allocated = 4;
	bvec->count = 0;

	*pbvec = talloc_move(mem_ctx, &bvec);

	status = NT_STATUS_OK;
done:
	talloc_free(tmp_ctx);

	return status;
}

NTSTATUS dcerpc_binding_vector_add_np_default(const struct ndr_interface_table *iface,
					      struct dcerpc_binding_vector *bvec)
{
	uint32_t ep_count = iface->endpoints->count;
	uint32_t i;
	NTSTATUS status;
	bool ok;

	for (i = 0; i < ep_count; i++) {
		struct dcerpc_binding *b;

		b = talloc_zero(bvec->bindings, struct dcerpc_binding);
		if (b == NULL) {
			return NT_STATUS_NO_MEMORY;
		}

		status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
		if (!NT_STATUS_IS_OK(status)) {
			return NT_STATUS_UNSUCCESSFUL;
		}

		/* Only add the named pipes defined in the iface endpoints */
		if (b->transport != NCACN_NP) {
			talloc_free(b);
			continue;
		}

		b->object = iface->syntax_id;

		b->host = talloc_asprintf(b, "\\\\%s", lp_netbios_name());
		if (b->host == NULL) {
			talloc_free(b);
			return NT_STATUS_NO_MEMORY;
		}

		ok = binding_vector_realloc(bvec);
		if (!ok) {
			talloc_free(b);
			return NT_STATUS_NO_MEMORY;
		}

		bvec->bindings[bvec->count] = *b;
		bvec->count++;
	}

	return NT_STATUS_OK;
}

NTSTATUS dcerpc_binding_vector_add_port(const struct ndr_interface_table *iface,
					struct dcerpc_binding_vector *bvec,
					const char *host,
					uint16_t port)
{
	uint32_t ep_count = iface->endpoints->count;
	uint32_t i;
	NTSTATUS status;
	bool ok;

	for (i = 0; i < ep_count; i++) {
		struct dcerpc_binding *b;

		b = talloc_zero(bvec->bindings, struct dcerpc_binding);
		if (b == NULL) {
			return NT_STATUS_NO_MEMORY;
		}

		status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
		if (!NT_STATUS_IS_OK(status)) {
			return NT_STATUS_UNSUCCESSFUL;
		}

		if (b->transport != NCACN_IP_TCP) {
			talloc_free(b);
			continue;
		}

		b->object = iface->syntax_id;

		b->host = talloc_strdup(b, host);
		if (b->host == NULL) {
			talloc_free(b);
			return NT_STATUS_NO_MEMORY;
		}

		b->endpoint = talloc_asprintf(b, "%u", port);
		if (b->endpoint == NULL) {
			talloc_free(b);
			return NT_STATUS_NO_MEMORY;
		}

		ok = binding_vector_realloc(bvec);
		if (!ok) {
			talloc_free(b);
			return NT_STATUS_NO_MEMORY;
		}

		bvec->bindings[bvec->count] = *b;
		bvec->count++;

		break;
	}

	return NT_STATUS_OK;
}

NTSTATUS dcerpc_binding_vector_add_unix(const struct ndr_interface_table *iface,
					struct dcerpc_binding_vector *bvec,
					const char *name)
{
	uint32_t ep_count = iface->endpoints->count;
	uint32_t i;
	NTSTATUS status;
	bool ok;

	for (i = 0; i < ep_count; i++) {
		struct dcerpc_binding *b;

		b = talloc_zero(bvec->bindings, struct dcerpc_binding);
		if (b == NULL) {
			return NT_STATUS_NO_MEMORY;
		}

		status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
		if (!NT_STATUS_IS_OK(status)) {
			return NT_STATUS_UNSUCCESSFUL;
		}

		if (b->transport != NCALRPC) {
			talloc_free(b);
			continue;
		}

		b->object = iface->syntax_id;

		b->endpoint = talloc_asprintf(b,
					      "%s/%s",
					      lp_ncalrpc_dir(),
					      name);
		if (b->endpoint == NULL) {
			talloc_free(b);
			return NT_STATUS_NO_MEMORY;
		}

		ok = binding_vector_realloc(bvec);
		if (!ok) {
			talloc_free(b);
			return NT_STATUS_NO_MEMORY;
		}

		bvec->bindings[bvec->count] = *b;
		bvec->count++;

		break;
	}

	return NT_STATUS_OK;
}

NTSTATUS dcerpc_binding_vector_create(TALLOC_CTX *mem_ctx,
				      const struct ndr_interface_table *iface,
				      uint16_t port,
				      const char *ncalrpc,
				      struct dcerpc_binding_vector **pbvec)
{
	struct dcerpc_binding_vector *bvec;
	uint32_t ep_count;
	uint32_t count = 0;
	uint32_t i;
	NTSTATUS status;
	TALLOC_CTX *tmp_ctx;

	tmp_ctx = talloc_stackframe();
	if (tmp_ctx == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	ep_count = iface->endpoints->count;

	bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
	if (bvec == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto done;
	}

	bvec->bindings = talloc_zero_array(bvec, struct dcerpc_binding, ep_count);
	if (bvec->bindings == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto done;
	}

	for (i = 0; i < ep_count; i++) {
		struct dcerpc_binding *b;

		b = talloc_zero(bvec->bindings, struct dcerpc_binding);
		if (b == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto done;
		}

		status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
		if (!NT_STATUS_IS_OK(status)) {
			status = NT_STATUS_UNSUCCESSFUL;
			goto done;
		}

		b->object = iface->syntax_id;

		switch (b->transport) {
			case NCACN_NP:
				b->host = talloc_asprintf(b, "\\\\%s", lp_netbios_name());
				if (b->host == NULL) {
					status = NT_STATUS_NO_MEMORY;
					goto done;
				}
				break;
			case NCACN_IP_TCP:
				if (port == 0) {
					talloc_free(b);
					continue;
				}

				b->endpoint = talloc_asprintf(b, "%u", port);
				if (b->endpoint == NULL) {
					status = NT_STATUS_NO_MEMORY;
					goto done;
				}

				break;
			case NCALRPC:
				if (ncalrpc == NULL) {
					talloc_free(b);
					continue;
				}

				b->endpoint = talloc_asprintf(b,
							      "%s/%s",
							      lp_ncalrpc_dir(),
							      ncalrpc);
				if (b->endpoint == NULL) {
					status = NT_STATUS_NO_MEMORY;
					goto done;
				}
				break;
			default:
				talloc_free(b);
				continue;
		}

		bvec->bindings[count] = *b;
		count++;
	}

	bvec->count = count;

	*pbvec = talloc_move(mem_ctx, &bvec);

	status = NT_STATUS_OK;
done:
	talloc_free(tmp_ctx);

	return status;
}

static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
			    struct messaging_context *msg_ctx,
			    const struct ndr_interface_table *iface,
			    const struct dcerpc_binding_vector *bind_vec,
			    const struct GUID *object_guid,
			    const char *annotation,
			    uint32_t replace,
			    uint32_t unregister,
			    struct dcerpc_binding_handle **pbh)
{
	struct rpc_pipe_client *cli = NULL;
	struct dcerpc_binding_handle *h;
	struct pipe_auth_data *auth;
	const char *ncalrpc_sock;
	const char *rpcsrv_type;
	struct epm_entry_t *entries;
	uint32_t num_ents, i;
	TALLOC_CTX *tmp_ctx;
	uint32_t result = EPMAPPER_STATUS_OK;
	NTSTATUS status;

	if (iface == NULL) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (bind_vec == NULL || bind_vec->count == 0) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	tmp_ctx = talloc_stackframe();
	if (tmp_ctx == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
					   "rpc_server", "epmapper",
					   "none");

	if (strcasecmp_m(rpcsrv_type, "embedded") == 0) {
		struct tsocket_address *local;
		int rc;

		rc = tsocket_address_inet_from_strings(tmp_ctx,
						       "ip",
						       "127.0.0.1",
						       0,
						       &local);
		if (rc < 0) {
			return NT_STATUS_NO_MEMORY;
		}

		status = rpcint_binding_handle(tmp_ctx,
					       &ndr_table_epmapper,
					       local,
					       get_session_info_system(),
					       msg_ctx,
					       &h);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(1, ("dcerpc_ep_register: Could not connect to "
				  "epmapper (%s)", nt_errstr(status)));
			goto done;
		}
	} else if (strcasecmp_m(rpcsrv_type, "daemon") == 0) {
		/* Connect to the endpoint mapper locally */
		ncalrpc_sock = talloc_asprintf(tmp_ctx,
					      "%s/%s",
					      lp_ncalrpc_dir(),
					      "EPMAPPER");
		if (ncalrpc_sock == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto done;
		}

		status = rpc_pipe_open_ncalrpc(tmp_ctx,
					       ncalrpc_sock,
					       &ndr_table_epmapper.syntax_id,
					       &cli);
		if (!NT_STATUS_IS_OK(status)) {
			goto done;
		}

		status = rpccli_ncalrpc_bind_data(cli, &auth);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(0, ("Failed to initialize anonymous bind.\n"));
			goto done;
		}

		status = rpc_pipe_bind(cli, auth);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
			goto done;
		}

		h = cli->binding_handle;
	} else {
		status = NT_STATUS_INVALID_PARAMETER;
		goto done;
	}

	num_ents = bind_vec->count;
	entries = talloc_array(tmp_ctx, struct epm_entry_t, num_ents);

	for (i = 0; i < num_ents; i++) {
		struct dcerpc_binding *map_binding = &bind_vec->bindings[i];
		struct epm_twr_t *map_tower;

		map_tower = talloc_zero(entries, struct epm_twr_t);
		if (map_tower == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto done;
		}

		status = dcerpc_binding_build_tower(entries,
						    map_binding,
						    &map_tower->tower);
		if (!NT_STATUS_IS_OK(status)) {
			goto done;
		}

		entries[i].tower = map_tower;
		if (annotation == NULL) {
			entries[i].annotation = talloc_strdup(entries, "");
		} else {
			entries[i].annotation = talloc_strndup(entries,
							       annotation,
							       EPM_MAX_ANNOTATION_SIZE);
		}
		if (entries[i].annotation == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto done;
		}

		if (object_guid != NULL) {
			entries[i].object = *object_guid;
		} else {
			entries[i].object = map_binding->object.uuid;
		}
	}

	if (unregister) {
		status = dcerpc_epm_Delete(h,
					   tmp_ctx,
					   num_ents,
					   entries,
					   &result);
	} else {
		status = dcerpc_epm_Insert(h,
					   tmp_ctx,
					   num_ents,
					   entries,
					   replace,
					   &result);
	}
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
			  nt_errstr(status)));
		goto done;
	}
	if (result != EPMAPPER_STATUS_OK) {
		DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
			  result));
		status = NT_STATUS_UNSUCCESSFUL;
		goto done;
	}

	if (pbh != NULL) {
		*pbh = talloc_move(mem_ctx, &h);
		talloc_steal(*pbh, cli);
	}

done:
	talloc_free(tmp_ctx);

	return status;
}

NTSTATUS dcerpc_ep_register(TALLOC_CTX *mem_ctx,
			    struct messaging_context *msg_ctx,
			    const struct ndr_interface_table *iface,
			    const struct dcerpc_binding_vector *bind_vec,
			    const struct GUID *object_guid,
			    const char *annotation,
			    struct dcerpc_binding_handle **ph)
{
	return ep_register(mem_ctx,
			   msg_ctx,
			   iface,
			   bind_vec,
			   object_guid,
			   annotation,
			   1,
			   0,
			   ph);
}

NTSTATUS dcerpc_ep_register_noreplace(TALLOC_CTX *mem_ctx,
				      struct messaging_context *msg_ctx,
				      const struct ndr_interface_table *iface,
				      const struct dcerpc_binding_vector *bind_vec,
				      const struct GUID *object_guid,
				      const char *annotation,
				      struct dcerpc_binding_handle **ph)
{
	return ep_register(mem_ctx,
			   msg_ctx,
			   iface,
			   bind_vec,
			   object_guid,
			   annotation,
			   0,
			   0,
			   ph);
}

NTSTATUS dcerpc_ep_unregister(struct messaging_context *msg_ctx,
			      const struct ndr_interface_table *iface,
			      const struct dcerpc_binding_vector *bind_vec,
			      const struct GUID *object_guid)
{
	return ep_register(NULL,
			   msg_ctx,
			   iface,
			   bind_vec,
			   object_guid,
			   NULL,
			   0,
			   1,
			   NULL);
}

/* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */