summaryrefslogtreecommitdiff
path: root/source4/lib/dcom/common/main.c
blob: a2650e3446de1fd6d137b9fddce7948ea568f424 (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
/*
   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 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;
}

static NTSTATUS dcom_connect_STRINGBINDINGARRAY(struct dcom_context *ctx, struct dcerpc_pipe **p, DATA_BLOB *stringbindingarray)
{
	return NT_STATUS_NOT_SUPPORTED;
}

static NTSTATUS dcom_connect_DUALSTRINGBINDINGARRAY(struct dcom_context *ctx, struct dcerpc_pipe **p, struct DUALSTRINGARRAY *array)
{
	return NT_STATUS_NOT_SUPPORTED;
}

NTSTATUS dcerpc_IUnknown_AddRef(struct dcom_interface *p, TALLOC_CTX *mem_ctx, struct IUnknown_AddRef *rr) 
{
	struct RemAddRef r;
	struct REMINTERFACEREF ref;
	
	/* This is rather inefficient, but we'll patch it up later */
	r.in.cInterfaceRefs = 1;
	r.in.InterfaceRefs = &ref;

	return dcerpc_RemAddRef(p, mem_ctx, &r);
}

NTSTATUS dcerpc_IUnknown_Release(struct dcom_interface *p, TALLOC_CTX *mem_ctx, struct IUnknown_Release *rr)
{
	struct RemRelease r;
	struct REMINTERFACEREF ref;

	p->private_references--;

	/* Only do the remote version of this call when all local references have 
	 * been released */
	if (p->private_references == 0) {
		NTSTATUS status;
		r.in.cInterfaceRefs = 1;
		r.in.InterfaceRefs = &ref;

		status = dcerpc_RemRelease(p, mem_ctx, &r);
		
		if (NT_STATUS_IS_OK(status)) {
			talloc_destroy(p);	
		}

		return status;
	}

	return NT_STATUS_OK;
}

NTSTATUS dcerpc_IUnknown_QueryInterface(struct dcom_interface *o, TALLOC_CTX *mem_ctx, struct IUnknown_QueryInterface *rr)
{
	/* FIXME: Ask local server for interface pointer. Local server can then 
	 * call RemQueryInterface if necessary */
	return NT_STATUS_NOT_SUPPORTED;
}

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);
	
	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 **ip, WERROR *results)
{
	struct RemoteActivation r;
	int i;
	struct dcerpc_pipe *p;
	NTSTATUS status;
	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 = 5;
	r.in.this.version.MinorVersion = 1;
	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);
	
	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, num_ifaces);
	for (i = 0; i < num_ifaces; i++) {
		results[i] = r.out.results[i];
		(*ip)[i].private_references = 1;
		(*ip)[i].objref = &r.out.ifaces[i].p->obj;
		(*ip)[i].pipe = NULL;
		(*ip)[i].ctx = ctx;
	}

	return WERR_OK;
}

WERROR dcom_get_class_object(struct dcom_context *ctx, struct GUID *clsid, const char *server, struct GUID *iid, struct dcom_interface *ip)
{
	struct RemoteActivation r;
	struct dcerpc_pipe *p;
	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 = 5;
	r.in.this.version.MinorVersion = 1;
	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;

	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]; }
	
	ip->private_references = 1;
	ip->pipe = NULL;
	ip->objref = &pm.p->obj;
	ip->ctx = ctx;

	return WERR_OK;
}

struct dcom_oxid_mapping {
	struct dcom_oxid_mapping *prev, *next;	
	struct DUALSTRINGARRAY bindings;
	HYPER_T oxid;
	struct dcerpc_pipe *pipe;
};

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

	return NULL;
}

NTSTATUS dcom_get_pipe (struct dcom_interface *iface, struct dcerpc_pipe **p)
{
	struct dcom_oxid_mapping *m;
	HYPER_T oxid;
	NTSTATUS status;
	
	SMB_ASSERT(iface->objref->signature == OBJREF_SIGNATURE);

	if (iface->objref->flags & OBJREF_HANDLER) {
		DEBUG(0, ("dcom_get_pipe: OBJREF_HANDLER not supported!\n"));
		return NT_STATUS_NOT_SUPPORTED;
	}

	if (iface->objref->flags & OBJREF_CUSTOM) {
		DEBUG(0, ("dcom_get_pipe: OBJREF_CUSTOM not supported!\n"));
		return NT_STATUS_NOT_SUPPORTED;
	}

	DEBUG(1, ("DCOM: Connecting to %s\n", GUID_string(NULL, &iface->objref->iid)));

	oxid = iface->objref->u_objref.u_standard.std.oxid;

	m = oxid_mapping_by_oxid(iface->ctx, oxid);

	/* 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", oxid));

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

		status = dcom_connect_STRINGBINDINGARRAY(iface->ctx, &po, &iface->objref->u_objref.u_standard.saResAddr);

		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);
	}

	if (m->pipe) {
		*p = m->pipe;
		/* FIXME: Switch to correct IID using an alter context call */
		return NT_STATUS_OK;
	}
	
	status = dcom_connect_DUALSTRINGBINDINGARRAY(iface->ctx, &m->pipe, &m->bindings);

	if (NT_STATUS_IS_ERR(status)) {
		return status;
	}
	
	*p = m->pipe;
	return NT_STATUS_OK;
}