2 Unix SMB/CIFS implementation.
3 Main DCOM functionality
4 Copyright (C) 2004 Jelmer Vernooij <jelmer@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "system/filesys.h"
23 #include "librpc/gen_ndr/ndr_epmapper.h"
24 #include "librpc/gen_ndr/ndr_remact.h"
25 #include "librpc/gen_ndr/com_dcom.h"
26 #include "lib/com/dcom/dcom.h"
28 #define DCOM_NEGOTIATED_PROTOCOLS { EPM_PROTOCOL_TCP, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NCALRPC }
30 struct dcom_client_context *dcom_client_init(struct com_context *ctx, struct cli_credentials *credentials)
32 ctx->dcom = talloc(ctx, struct dcom_client_context);
33 ctx->dcom->credentials = credentials;
38 static NTSTATUS dcerpc_binding_from_STRINGBINDING(TALLOC_CTX *mem_ctx, struct dcerpc_binding **b_out, struct STRINGBINDING *bd)
40 char *host, *endpoint;
41 struct dcerpc_binding *b;
43 b = talloc_zero(mem_ctx, struct dcerpc_binding);
45 return NT_STATUS_NO_MEMORY;
48 b->transport = dcerpc_transport_by_endpoint_protocol(bd->wTowerId);
50 if (b->transport == -1) {
51 DEBUG(1, ("Can't find transport match endpoint protocol %d\n", bd->wTowerId));
52 return NT_STATUS_NOT_SUPPORTED;
55 host = talloc_strdup(b, bd->NetworkAddr);
56 endpoint = strchr(host, '[');
62 endpoint[strlen(endpoint)-1] = '\0';
66 b->endpoint = talloc_strdup(b, endpoint);
72 static NTSTATUS dcom_connect_host(struct com_context *ctx, struct dcerpc_pipe **p, const char *server)
74 struct dcerpc_binding *bd;
75 const char * available_transports[] = { "ncacn_ip_tcp", "ncacn_np" };
78 TALLOC_CTX *mem_ctx = talloc_init("dcom_connect");
81 return dcerpc_pipe_connect(ctx, p, "ncalrpc",
82 &dcerpc_table_IRemoteActivation,
83 ctx->dcom->credentials, ctx->event_ctx);
86 /* Allow server name to contain a binding string */
87 if (NT_STATUS_IS_OK(dcerpc_parse_binding(mem_ctx, server, &bd))) {
88 status = dcerpc_pipe_connect_b(ctx, p, bd,
89 &dcerpc_table_IRemoteActivation,
90 ctx->dcom->credentials, ctx->event_ctx);
96 for (i = 0; i < ARRAY_SIZE(available_transports); i++)
98 char *binding = talloc_asprintf(mem_ctx, "%s:%s", available_transports[i], server);
100 talloc_free(mem_ctx);
101 return NT_STATUS_NO_MEMORY;
104 status = dcerpc_pipe_connect(ctx, p, binding,
105 &dcerpc_table_IRemoteActivation,
106 ctx->dcom->credentials, ctx->event_ctx);
108 if (NT_STATUS_IS_OK(status)) {
109 talloc_free(mem_ctx);
114 talloc_free(mem_ctx);
118 struct dcom_object_exporter *object_exporter_by_oxid(struct com_context *ctx, uint64_t oxid)
120 struct dcom_object_exporter *ox;
121 for (ox = ctx->dcom->object_exporters; ox; ox = ox->next) {
122 if (ox->oxid == oxid) {
130 struct dcom_object_exporter *object_exporter_by_ip(struct com_context *ctx, struct IUnknown *ip)
132 return NULL; /* FIXME */
135 WERROR dcom_create_object(struct com_context *ctx, struct GUID *clsid, const char *server, int num_ifaces, struct GUID *iid, struct IUnknown ***ip, WERROR *results)
137 uint16_t protseq[] = DCOM_NEGOTIATED_PROTOCOLS;
138 struct dcerpc_pipe *p;
139 struct dcom_object_exporter *m;
141 struct RemoteActivation r;
142 struct DUALSTRINGARRAY dualstring;
145 status = dcom_connect_host(ctx, &p, server);
146 if (NT_STATUS_IS_ERR(status)) {
147 DEBUG(1, ("Unable to connect to %s - %s\n", server, nt_errstr(status)));
148 return ntstatus_to_werror(status);
152 r.in.this.version.MajorVersion = COM_MAJOR_VERSION;
153 r.in.this.version.MinorVersion = COM_MINOR_VERSION;
154 r.in.this.cid = GUID_random();
156 r.in.ClientImpLevel = RPC_C_IMP_LEVEL_IDENTIFY;
157 r.in.num_protseqs = ARRAY_SIZE(protseq);
158 r.in.protseq = protseq;
159 r.in.Interfaces = num_ifaces;
161 r.out.ifaces = talloc_array(ctx, struct MInterfacePointer *, num_ifaces);
162 r.out.pdsaOxidBindings = &dualstring;
164 status = dcerpc_RemoteActivation(p, ctx, &r);
165 if(NT_STATUS_IS_ERR(status)) {
166 DEBUG(1, ("Error while running RemoteActivation %s\n", nt_errstr(status)));
167 return ntstatus_to_werror(status);
170 if(!W_ERROR_IS_OK(r.out.result)) {
174 if(!W_ERROR_IS_OK(r.out.hr)) {
178 *ip = talloc_array(ctx, struct IUnknown *, num_ifaces);
179 for (i = 0; i < num_ifaces; i++) {
180 results[i] = r.out.results[i];
182 if (W_ERROR_IS_OK(results[i])) {
183 status = dcom_IUnknown_from_OBJREF(ctx, &(*ip)[i], &r.out.ifaces[i]->obj);
184 if (!NT_STATUS_IS_OK(status)) {
185 results[i] = ntstatus_to_werror(status);
190 /* Add the OXID data for the returned oxid */
191 m = object_exporter_by_oxid(ctx, r.out.pOxid);
192 m->bindings = *r.out.pdsaOxidBindings;
197 WERROR dcom_get_class_object(struct com_context *ctx, struct GUID *clsid, const char *server, struct GUID *iid, struct IUnknown **ip)
199 struct dcom_object_exporter *m;
200 struct RemoteActivation r;
201 struct dcerpc_pipe *p;
202 struct DUALSTRINGARRAY dualstring;
204 struct MInterfacePointer pm;
205 struct MInterfacePointer *ifaces[1];
206 uint16_t protseq[] = DCOM_NEGOTIATED_PROTOCOLS;
209 return com_get_class_object(ctx, clsid, iid, ip);
212 status = dcom_connect_host(ctx, &p, server);
213 if (NT_STATUS_IS_ERR(status)) {
214 DEBUG(1, ("Unable to connect to %s - %s\n", server, nt_errstr(status)));
215 return ntstatus_to_werror(status);
219 r.in.this.version.MajorVersion = COM_MAJOR_VERSION;
220 r.in.this.version.MinorVersion = COM_MINOR_VERSION;
221 r.in.this.cid = GUID_random();
223 r.in.ClientImpLevel = RPC_C_IMP_LEVEL_IDENTIFY;
224 r.in.num_protseqs = ARRAY_SIZE(protseq);
225 r.in.protseq = protseq;
228 r.in.Mode = MODE_GET_CLASS_OBJECT;
229 r.out.ifaces = ifaces;
231 r.out.pdsaOxidBindings = &dualstring;
233 status = dcerpc_RemoteActivation(p, ctx, &r);
234 if(NT_STATUS_IS_ERR(status)) {
235 DEBUG(1, ("Error while running RemoteActivation - %s\n", nt_errstr(status)));
236 return ntstatus_to_werror(status);
239 if(!W_ERROR_IS_OK(r.out.result)) { return r.out.result; }
240 if(!W_ERROR_IS_OK(r.out.hr)) { return r.out.hr; }
241 if(!W_ERROR_IS_OK(r.out.results[0])) { return r.out.results[0]; }
243 /* Set up the interface data */
244 dcom_IUnknown_from_OBJREF(ctx, ip, &pm.obj);
246 /* Add the OXID data for the returned oxid */
247 m = object_exporter_by_oxid(ctx, r.out.pOxid);
248 m->bindings = *r.out.pdsaOxidBindings;
253 NTSTATUS dcom_get_pipe(struct IUnknown *iface, struct dcerpc_pipe **pp)
255 struct dcerpc_binding *binding;
260 struct dcerpc_pipe *p;
262 struct dcom_object_exporter *ox;
264 ox = object_exporter_by_ip(iface->ctx, iface);
266 tmp_ctx = talloc_new(NULL);
270 iid = iface->vtable->iid;
273 if (!GUID_equal(&p->syntax.uuid, &iid)) {
274 struct dcerpc_pipe *p2;
275 ox->pipe->syntax.uuid = iid;
277 /* interface will always be present, so
278 * idl_iface_by_uuid can't return NULL */
279 status = dcerpc_secondary_context(p, &p2, idl_iface_by_uuid(&iid));
281 if (NT_STATUS_IS_OK(status)) {
285 p = talloc_reference(NULL, p);
288 talloc_free(tmp_ctx);
294 status = dcerpc_binding_from_STRINGBINDING(iface->ctx, &binding,
295 ox->bindings.stringbindings[i]);
296 if (!NT_STATUS_IS_OK(status)) {
297 DEBUG(1, ("Error parsing string binding"));
299 status = dcerpc_pipe_connect_b(NULL, &p, binding,
300 idl_iface_by_uuid(&iid),
301 iface->ctx->dcom->credentials,
302 iface->ctx->event_ctx);
304 talloc_free(binding);
306 } while (!NT_STATUS_IS_OK(status) && ox->bindings.stringbindings[i]);
308 if (NT_STATUS_IS_ERR(status)) {
309 DEBUG(0, ("Unable to connect to remote host - %s\n", nt_errstr(status)));
310 talloc_free(tmp_ctx);
314 DEBUG(2, ("Successfully connected to OXID %llx\n", (long long)oxid));
317 talloc_free(tmp_ctx);
322 NTSTATUS dcom_OBJREF_from_IUnknown(struct OBJREF *o, struct IUnknown *p)
324 /* FIXME: Cache generated objref objects? */
327 o->signature = OBJREF_SIGNATURE;
330 o->flags = OBJREF_NULL;
332 o->iid = p->vtable->iid;
340 return NT_STATUS_NOT_IMPLEMENTED;
343 NTSTATUS dcom_IUnknown_from_OBJREF(struct com_context *ctx, struct IUnknown **_p, struct OBJREF *o)
346 struct dcom_object_exporter *ox;
353 case OBJREF_STANDARD:
354 p = talloc(ctx, struct IUnknown);
356 p->vtable = dcom_proxy_vtable_by_iid(&o->iid);
358 DEBUG(0, ("Unable to find proxy class for interface with IID %s\n", GUID_string(ctx, &o->iid)));
359 return NT_STATUS_NOT_SUPPORTED;
362 ox = object_exporter_by_oxid(ctx, o->u_objref.u_standard.std.oxid);
363 /* FIXME: Add object to list of objects to ping */
368 p = talloc(ctx, struct IUnknown);
370 ox = object_exporter_by_oxid(ctx, o->u_objref.u_handler.std.oxid );
371 /* FIXME: Add object to list of objects to ping */
372 /*FIXME p->vtable = dcom_vtable_by_clsid(&o->u_objref.u_handler.clsid);*/
373 /* FIXME: Do the custom unmarshaling call */
379 p = talloc(ctx, struct IUnknown);
382 /* FIXME: Do the actual custom unmarshaling call */
384 return NT_STATUS_NOT_SUPPORTED;
387 return NT_STATUS_NOT_SUPPORTED;
390 uint64_t dcom_get_current_oxid(void)