summaryrefslogtreecommitdiff
path: root/source4/lib/dcom/common/main.c
blob: 373013726860927aca08d9d8936834f5bb9669ef (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
/*
   Unix SMB/CIFS implementation.
   Main DCOM functionality
   Copyright (C) 2004 Jelmer Vernooij <jelmer@samba.org>

   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "dlinklist.h"
#include "librpc/gen_ndr/ndr_epmapper.h"
#include "librpc/gen_ndr/ndr_remact.h"
#include "librpc/gen_ndr/ndr_oxidresolver.h"
#include "librpc/gen_ndr/ndr_dcom.h"

#define DCOM_NEGOTIATED_PROTOCOLS { EPM_PROTOCOL_TCP, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NCALRPC }

static NTSTATUS dcerpc_binding_from_STRINGBINDING(TALLOC_CTX *mem_ctx, struct dcerpc_binding *b, struct STRINGBINDING *bd)
{
	char *host, *endpoint;

	ZERO_STRUCTP(b);
	
	b->transport = dcerpc_transport_by_endpoint_protocol(bd->wTowerId);

	if (b->transport == -1) {
		DEBUG(1, ("Can't find transport match endpoint protocol %d\n", bd->wTowerId));
		return NT_STATUS_NOT_SUPPORTED;
	}

	host = talloc_strdup(mem_ctx, bd->NetworkAddr);
	endpoint = strchr(host, '[');

	if (endpoint) {
		*endpoint = '\0';
		endpoint++;

		endpoint[strlen(endpoint)-1] = '\0';
	}

	b->host = host;
	b->endpoint = endpoint;

	return NT_STATUS_OK;
}

static NTSTATUS dcom_connect_host(struct dcom_context *ctx, struct dcerpc_pipe **p, const char *server)
{
	struct dcerpc_binding bd;
	enum dcerpc_transport_t available_transports[] = { NCACN_IP_TCP, NCACN_NP };
	int i;
	NTSTATUS status;
	TALLOC_CTX *mem_ctx = talloc_init("dcom_connect");

	/* Allow server name to contain a binding string */
	if (NT_STATUS_IS_OK(dcerpc_parse_binding(mem_ctx, server, &bd))) {
		status = dcerpc_pipe_connect_b(p, &bd, 
					DCERPC_IREMOTEACTIVATION_UUID, 
					DCERPC_IREMOTEACTIVATION_VERSION, 
					ctx->domain, ctx->user, ctx->password);

		talloc_destroy(mem_ctx);
		return status;
	}
	talloc_destroy(mem_ctx);

	ZERO_STRUCT(bd);
	bd.host = server;
	
	if (server == NULL) { 
		bd.transport = NCALRPC; 
		return dcerpc_pipe_connect_b(p, &bd, 
					DCERPC_IREMOTEACTIVATION_UUID, 
					DCERPC_IREMOTEACTIVATION_VERSION, 
					ctx->domain, ctx->user, ctx->password);
	}

	for (i = 0; i < ARRAY_SIZE(available_transports); i++)
	{
		bd.transport = available_transports[i];
		
		status = dcerpc_pipe_connect_b(p, &bd, 
						DCERPC_IREMOTEACTIVATION_UUID, 
						DCERPC_IREMOTEACTIVATION_VERSION, 
						ctx->domain, ctx->user, ctx->password);

		if (NT_STATUS_IS_OK(status)) {
			return status;
		}
	}
	
	return status;
}

WERROR dcom_init(struct dcom_context **ctx, const char *domain, const char *user, const char *pass)
{
	*ctx = talloc_p(NULL, struct dcom_context);
	(*ctx)->oxids = NULL;
	(*ctx)->domain = talloc_strdup(*ctx, domain);
	(*ctx)->user = talloc_strdup(*ctx, user);
	(*ctx)->password = talloc_strdup(*ctx, pass);
	(*ctx)->dcerpc_flags = 0;
	
	return WERR_OK;
}

static struct dcom_object_exporter *oxid_mapping_by_oxid (struct dcom_context *ctx, HYPER_T oxid)
{
	struct dcom_object_exporter *m;
	
	for (m = ctx->oxids;m;m = m->next) {
		if (m->oxid	== oxid) {
			break;
		}
	}

	/* Add oxid mapping if we couldn't find one */
	if (!m) {
		m = talloc_zero_p(ctx, struct dcom_object_exporter);
		m->oxid = oxid;
		DLIST_ADD(ctx->oxids, m);
	}

	return m;
}

WERROR dcom_ping(struct dcom_context *ctx)
{
	/* FIXME: If OID's waiting in queue, do a ComplexPing call */
	/* FIXME: otherwise, do a SimplePing call */
	return WERR_OK;
}

static WERROR dcom_create_object_remote(struct dcom_context *ctx, struct GUID *clsid, const char *server, int num_ifaces, struct GUID *iid, struct dcom_interface_p ***ip, WERROR *results)
{
	uint16 protseq[] = DCOM_NEGOTIATED_PROTOCOLS;
	struct dcerpc_pipe *p;
	struct dcom_object_exporter *m;
	NTSTATUS status;
	struct RemoteActivation r;
	struct DUALSTRINGARRAY dualstring;
	int i;

	status = dcom_connect_host(ctx, &p, server);
	if (NT_STATUS_IS_ERR(status)) {
		DEBUG(1, ("Unable to connect to %s - %s\n", server, nt_errstr(status)));
		return ntstatus_to_werror(status);
	}

	ZERO_STRUCT(r.in);
	r.in.this.version.MajorVersion = COM_MAJOR_VERSION;
	r.in.this.version.MinorVersion = COM_MINOR_VERSION;
	uuid_generate_random(&r.in.this.cid);
	r.in.Clsid = *clsid;
	r.in.ClientImpLevel = RPC_C_IMP_LEVEL_IDENTIFY;
	r.in.num_protseqs = ARRAY_SIZE(protseq);
	r.in.protseq = protseq;
	r.in.Interfaces = num_ifaces;
	r.in.pIIDs = iid;
	r.out.ifaces = talloc_array_p(ctx, struct pMInterfacePointer, num_ifaces);
	r.out.pdsaOxidBindings = &dualstring;
	
	status = dcerpc_RemoteActivation(p, ctx, &r);
	if(NT_STATUS_IS_ERR(status)) {
		DEBUG(1, ("Error while running RemoteActivation %s\n", nt_errstr(status)));
		return ntstatus_to_werror(status);
	}

	if(!W_ERROR_IS_OK(r.out.result)) {
		return r.out.result; 
	}
	
	if(!W_ERROR_IS_OK(r.out.hr)) { 
		return r.out.hr; 
	}

	*ip = talloc_array_p(ctx, struct dcom_interface_p *, num_ifaces);
	for (i = 0; i < num_ifaces; i++) {
		results[i] = r.out.results[i];
		(*ip)[i] = NULL;
		if (W_ERROR_IS_OK(results[i])) {
			status = dcom_ifacep_from_OBJREF(ctx, &(*ip)[i], &r.out.ifaces[i].p->obj);
			if (NT_STATUS_IS_OK(status)) {
				(*ip)[i]->private_references = 1;
			} else {
				results[i] = ntstatus_to_werror(status);
			}
		}
	}

	/* Add the OXID data for the returned oxid */
	m = oxid_mapping_by_oxid(ctx, r.out.pOxid);
	m->bindings = *r.out.pdsaOxidBindings;
	
	return WERR_OK;
}

WERROR dcom_create_object(struct dcom_context *ctx, struct GUID *clsid, const char *server, int num_ifaces, struct GUID *iid, struct dcom_interface_p ***ip, WERROR *results)
{
	struct dcom_interface_p *factory, *iunk;
	struct QueryInterface qr;
	struct Release rr;
	struct CreateInstance cr;
	WERROR error;
	int i;
	NTSTATUS status;

	if (server != NULL) {
		return dcom_create_object_remote(ctx, clsid, server, num_ifaces, iid, ip, results);
	}
	
	/* Obtain class object */
	error = dcom_get_class_object(ctx, clsid, server, iid, &factory);
	if (!W_ERROR_IS_OK(error)) {
		DEBUG(3, ("Unable to obtain class object for %s\n", GUID_string(NULL, clsid)));
		return error;
	}

	dcom_OBJREF_from_ifacep(ctx, &cr.in.pUnknown->obj, factory);

	GUID_from_string(DCERPC_ICLASSFACTORY_UUID, cr.in.iid);
	
	/* Run IClassFactory::CreateInstance() */
	status = dcom_IClassFactory_CreateInstance(factory, ctx, &cr);
	if (NT_STATUS_IS_ERR(status)) {
		DEBUG(3, ("Error while calling IClassFactory::CreateInstance : %s\n", nt_errstr(status)));
		return ntstatus_to_werror(status);
	}
	
	/* Release class object */
	status = dcom_IUnknown_Release(factory, ctx, &rr);
	if (NT_STATUS_IS_ERR(status)) {
		DEBUG(3, ("Error freeing class factory: %s\n", nt_errstr(status)));
		return ntstatus_to_werror(status);
	}
	
	/* Do one or more QueryInterface calls */
	for (i = 0; i < num_ifaces; i++) {
		qr.in.iid = &iid[i];
		status = dcom_IUnknown_QueryInterface(iunk, ctx, &qr);
		if (NT_STATUS_IS_ERR(status)) {
			DEBUG(4, ("Error obtaining interface %s : %s\n", GUID_string(NULL, &iid[i]), nt_errstr(status)));
			return ntstatus_to_werror(status);
		}
		results[i] = qr.out.result;
	}

	return WERR_OK;
}

WERROR dcom_get_class_object_remote(struct dcom_context *ctx, struct GUID *clsid, const char *server, struct GUID *iid, struct dcom_interface_p **ip)
{
	struct dcom_object_exporter *m;
	struct RemoteActivation r;
	struct dcerpc_pipe *p;
	struct DUALSTRINGARRAY dualstring;
	NTSTATUS status;
	struct pMInterfacePointer pm;
	uint16 protseq[] = DCOM_NEGOTIATED_PROTOCOLS;

	status = dcom_connect_host(ctx, &p, server);
	if (NT_STATUS_IS_ERR(status)) {
		DEBUG(1, ("Unable to connect to %s - %s\n", server, nt_errstr(status)));
		return ntstatus_to_werror(status);
	}

	ZERO_STRUCT(r.in);
	r.in.this.version.MajorVersion = COM_MAJOR_VERSION;
	r.in.this.version.MinorVersion = COM_MINOR_VERSION;
	uuid_generate_random(&r.in.this.cid);
	r.in.Clsid = *clsid;
	r.in.ClientImpLevel = RPC_C_IMP_LEVEL_IDENTIFY;
	r.in.num_protseqs = ARRAY_SIZE(protseq);
	r.in.protseq = protseq;
	r.in.Interfaces = 1;
	r.in.pIIDs = iid;
	r.in.Mode = MODE_GET_CLASS_OBJECT;
	r.out.ifaces = &pm;
	r.out.pdsaOxidBindings = &dualstring;

	status = dcerpc_RemoteActivation(p, ctx, &r);
	if(NT_STATUS_IS_ERR(status)) {
		DEBUG(1, ("Error while running RemoteActivation - %s\n", nt_errstr(status)));
		return ntstatus_to_werror(status);
	}

	if(!W_ERROR_IS_OK(r.out.result)) { return r.out.result; }
	if(!W_ERROR_IS_OK(r.out.hr)) { return r.out.hr; }
	if(!W_ERROR_IS_OK(r.out.results[0])) { return r.out.results[0]; }
	
	/* Set up the interface data */
	dcom_ifacep_from_OBJREF(ctx, ip, &pm.p->obj);
	(*ip)->private_references = 1;
	
	/* Add the OXID data for the returned oxid */
	m = oxid_mapping_by_oxid(ctx, r.out.pOxid);
	m->bindings = *r.out.pdsaOxidBindings;

	return WERR_OK;
}

WERROR dcom_get_class_object(struct dcom_context *ctx, struct GUID *clsid, const char *server, struct GUID *iid, struct dcom_interface_p **ip)
{
	const struct dcom_class *c;
	struct QueryInterface qi;
	NTSTATUS status;
	
	if (server != NULL) {
		return dcom_get_class_object_remote(ctx, clsid, server, iid, ip);
	}

	c = dcom_class_by_clsid(clsid);
	if (!c) {
		/* FIXME: Better error code.. */
		return WERR_DEST_NOT_FOUND;
	}
	
	qi.in.iid = iid;

	status = dcom_IUnknown_QueryInterface(c->class_object, ctx, &qi );
	if (NT_STATUS_IS_ERR(status)) {
		return ntstatus_to_werror(status);
	}

	if (!W_ERROR_IS_OK(qi.out.result)) { return qi.out.result; }

	dcom_ifacep_from_OBJREF(ctx, ip, &qi.out.data->obj);
	
	return WERR_OK;
}

NTSTATUS dcom_get_pipe (struct dcom_interface_p *iface, struct dcerpc_pipe **p)
{
	struct dcerpc_binding binding;
	struct GUID iid;
	HYPER_T oxid;
	NTSTATUS status;
	int i;

	*p = NULL;
	
	oxid = iface->ox->oxid;
	iid = iface->interface->iid;

	if (iface->ox->pipe) {
		if (!uuid_equal(&iface->ox->pipe->syntax.uuid, &iid)) {
			iface->ox->pipe->syntax.uuid = iid;
			status = dcerpc_alter(iface->ox->pipe, iface->ctx);
			if (NT_STATUS_IS_ERR(status)) {
				return status;
			}
		}
		*p = iface->ox->pipe;
		return NT_STATUS_OK;
	}

	i = 0;
	do {
		status = dcerpc_binding_from_STRINGBINDING(iface->ctx, &binding, iface->ox->bindings.stringbindings[i]);
		if (NT_STATUS_IS_ERR(status)) {
			DEBUG(1, ("Error parsing string binding"));
		} else {
			binding.flags = iface->ctx->dcerpc_flags;
			status = dcerpc_pipe_connect_b(&iface->ox->pipe, &binding, GUID_string(iface->ctx, &iid) , 0.0, iface->ctx->domain, iface->ctx->user, iface->ctx->password);
		}

		i++;
	} while (NT_STATUS_IS_ERR(status) && iface->ox->bindings.stringbindings[i]);

	if (NT_STATUS_IS_ERR(status)) {
		DEBUG(0, ("Unable to connect to remote host - %s\n", nt_errstr(status)));
		return status;
	}

	DEBUG(2, ("Successfully connected to OXID %llx\n", oxid));
	
	*p = iface->ox->pipe;
	return NT_STATUS_OK;
}

struct dcom_object *dcom_object_by_oid(struct dcom_object_exporter *ox, HYPER_T oid)
{
	struct dcom_object *o;

	for (o = ox->objects; o; o = o->next) {
		if (o->oid == oid) {
			break;
		}
	}

	if (o == NULL) {
		o = talloc_zero_p(ox, struct dcom_object);
		o->oid = oid;
		DLIST_ADD(ox->objects, o);
	}
	
	return o;
}


NTSTATUS dcom_OBJREF_from_ifacep(struct dcom_context *ctx, struct OBJREF *o, struct dcom_interface_p *_p)
{
	return NT_STATUS_NOT_IMPLEMENTED;	
}

NTSTATUS dcom_ifacep_from_OBJREF(struct dcom_context *ctx, struct dcom_interface_p **_p, struct OBJREF *o)
{
	struct dcom_interface_p *p = talloc_p(ctx, struct dcom_interface_p);

	p->ctx = ctx;	
	p->interface = dcom_interface_by_iid(&o->iid);
	if (!p->interface) {
		DEBUG(0, ("Unable to find interface with IID %s\n", GUID_string(ctx, &o->iid)));
		return NT_STATUS_NOT_SUPPORTED;
	}

	p->private_references = 0;
	p->objref_flags = o->flags;
	
	switch(p->objref_flags) {
	case OBJREF_NULL: 
		p->object = NULL; 
		p->ox = NULL;
		p->vtable = dcom_proxy_vtable_by_iid(&p->interface->iid);
		ZERO_STRUCT(p->ipid);
		*_p = p;
		return NT_STATUS_OK;
		
	case OBJREF_STANDARD:
		p->ox = oxid_mapping_by_oxid(ctx, o->u_objref.u_standard.std.oxid);
		p->ipid = o->u_objref.u_standard.std.ipid;
		p->object = dcom_object_by_oid(p->ox, o->u_objref.u_standard.std.oid);
		p->ox->resolver_address = o->u_objref.u_standard.saResAddr;
		p->vtable = dcom_proxy_vtable_by_iid(&p->interface->iid);
		*_p = p;
		return NT_STATUS_OK;
		
	case OBJREF_HANDLER:
		p->ox = oxid_mapping_by_oxid(ctx, o->u_objref.u_handler.std.oxid );
		p->ipid = o->u_objref.u_handler.std.ipid;
		p->object = dcom_object_by_oid(p->ox, o->u_objref.u_standard.std.oid);
		p->ox->resolver_address = o->u_objref.u_handler.saResAddr;
/*FIXME		p->vtable = dcom_vtable_by_clsid(&o->u_objref.u_handler.clsid);*/
		/* FIXME: Do the custom unmarshaling call */
	
		*_p = p;
		return NT_STATUS_OK;
		
	case OBJREF_CUSTOM:
		{
			p->vtable = NULL;
		
		/* FIXME: Do the actual custom unmarshaling call */
		p->ox = NULL;
		p->object = NULL;
		ZERO_STRUCT(p->ipid);
		*_p = p;
		return NT_STATUS_NOT_SUPPORTED;
		}
	}

	return NT_STATUS_NOT_SUPPORTED;

	
#if 0
	struct dcom_oxid_mapping *m;
	/* Add OXID mapping if none present yet */
	if (!m) {
		struct dcerpc_pipe *po;
		struct ResolveOxid r;
		uint16 protseq[] = DCOM_NEGOTIATED_PROTOCOLS;

		DEBUG(3, ("No binding data present yet, resolving OXID %llu\n", p->ox->oxid));

		m = talloc_zero_p(p->ctx, struct dcom_oxid_mapping);
		m->oxid = oxid;	

		i = 0;
		do {
			status = dcerpc_binding_from_STRINGBINDING(p->ctx, &binding, p->client.objref->u_objref.u_standard.saResAddr.stringbindings[i]);

			if (NT_STATUS_IS_OK(status)) {
				binding.flags = iface->ctx->dcerpc_flags;
				status = dcerpc_pipe_connect_b(&po, &binding, DCERPC_IOXIDRESOLVER_UUID, DCERPC_IOXIDRESOLVER_VERSION, iface->ctx->domain, iface->ctx->user, iface->ctx->password);
			} else {
				DEBUG(1, ("Error parsing string binding - %s", nt_errstr(status)));
			}

			i++;
		} while (!NT_STATUS_IS_OK(status) && iface->client.objref->u_objref.u_standard.saResAddr.stringbindings[i]);

		if (NT_STATUS_IS_ERR(status)) {
			DEBUG(1, ("Error while connecting to OXID Resolver : %s\n", nt_errstr(status)));
			return status;
		}

		r.in.pOxid = oxid;
		r.in.cRequestedProtseqs = ARRAY_SIZE(protseq);
		r.in.arRequestedProtseqs = protseq;
		r.out.ppdsaOxidBindings = &m->bindings;

		status = dcerpc_ResolveOxid(po, iface->ctx, &r);
		if (NT_STATUS_IS_ERR(status)) {
			DEBUG(1, ("Error while resolving OXID: %s\n", nt_errstr(status)));
			return status;
		}

		dcerpc_pipe_close(po);

		DLIST_ADD(iface->ctx->oxids, m);
	}
#endif

	return NT_STATUS_NOT_SUPPORTED;	
}

HYPER_T dcom_get_current_oxid(void)
{
	return getpid();
}

struct dcom_interface_p *dcom_new_local_ifacep(struct dcom_context *ctx, const struct dcom_interface *iface, void *vtable, struct dcom_object *object)
{
	struct dcom_interface_p *ip = talloc_p(ctx, struct dcom_interface_p);

	ip->ctx = ctx;
	ip->interface = iface;
	ip->vtable = vtable;
	uuid_generate_random(&ip->ipid);
	ip->object = object;
	ip->objref_flags = 0;
	ip->orpc_flags = 0;
	ip->ox = NULL;
	ip->private_references = 1;
	
	return ip;
}