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 "dlinklist.h"
24 #include "librpc/gen_ndr/ndr_epmapper.h"
25 #include "librpc/gen_ndr/ndr_remact.h"
26 #include "librpc/gen_ndr/ndr_oxidresolver.h"
27 #include "librpc/gen_ndr/ndr_dcom.h"
28 #include "librpc/gen_ndr/com_dcom.h"
29 #include "lib/com/dcom/dcom.h"
31 #define DCOM_NEGOTIATED_PROTOCOLS { EPM_PROTOCOL_TCP, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NCALRPC }
33 struct dcom_client_context *dcom_client_init(struct com_context *ctx, const char *domain, const char *user, const char *password)
35 ctx->dcom = talloc(ctx, struct dcom_client_context);
36 ctx->dcom->domain = domain;
37 ctx->dcom->user = user;
38 ctx->dcom->password = password;
43 static NTSTATUS dcerpc_binding_from_STRINGBINDING(TALLOC_CTX *mem_ctx, struct dcerpc_binding **b_out, struct STRINGBINDING *bd)
45 char *host, *endpoint;
46 struct dcerpc_binding *b;
48 b = talloc_zero(mem_ctx, struct dcerpc_binding);
50 return NT_STATUS_NO_MEMORY;
53 b->transport = dcerpc_transport_by_endpoint_protocol(bd->wTowerId);
55 if (b->transport == -1) {
56 DEBUG(1, ("Can't find transport match endpoint protocol %d\n", bd->wTowerId));
57 return NT_STATUS_NOT_SUPPORTED;
60 host = talloc_strdup(b, bd->NetworkAddr);
61 endpoint = strchr(host, '[');
67 endpoint[strlen(endpoint)-1] = '\0';
71 b->endpoint = talloc_strdup(b, endpoint);
77 static NTSTATUS dcom_connect_host(struct com_context *ctx, struct dcerpc_pipe **p, const char *server)
79 struct dcerpc_binding *bd;
80 const char * available_transports[] = { "ncacn_ip_tcp", "ncacn_np" };
83 TALLOC_CTX *mem_ctx = talloc_init("dcom_connect");
86 return dcerpc_pipe_connect(p, "ncalrpc",
87 DCERPC_IREMOTEACTIVATION_UUID,
88 DCERPC_IREMOTEACTIVATION_VERSION,
90 ctx->dcom->domain, ctx->dcom->user, ctx->dcom->password);
93 /* Allow server name to contain a binding string */
94 if (NT_STATUS_IS_OK(dcerpc_parse_binding(mem_ctx, server, &bd))) {
95 status = dcerpc_pipe_connect_b(p, bd,
96 DCERPC_IREMOTEACTIVATION_UUID,
97 DCERPC_IREMOTEACTIVATION_VERSION,
99 ctx->dcom->domain, ctx->dcom->user, ctx->dcom->password);
101 talloc_free(mem_ctx);
105 for (i = 0; i < ARRAY_SIZE(available_transports); i++)
107 char *binding = talloc_asprintf(mem_ctx, "%s:%s", available_transports[i], server);
109 talloc_free(mem_ctx);
110 return NT_STATUS_NO_MEMORY;
113 status = dcerpc_pipe_connect(p, binding,
114 DCERPC_IREMOTEACTIVATION_UUID,
115 DCERPC_IREMOTEACTIVATION_VERSION,
117 ctx->dcom->domain, ctx->dcom->user, ctx->dcom->password);
119 if (NT_STATUS_IS_OK(status)) {
120 talloc_free(mem_ctx);
125 talloc_free(mem_ctx);
129 struct dcom_object_exporter *object_exporter_by_oxid(struct com_context *ctx, uint64_t oxid)
131 struct dcom_object_exporter *ox;
132 for (ox = ctx->dcom->object_exporters; ox; ox = ox->next) {
133 if (ox->oxid == oxid) {
141 struct dcom_object_exporter *object_exporter_by_ip(struct com_context *ctx, struct IUnknown *ip)
143 return NULL; /* FIXME */
146 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)
148 uint16_t protseq[] = DCOM_NEGOTIATED_PROTOCOLS;
149 struct dcerpc_pipe *p;
150 struct dcom_object_exporter *m;
152 struct RemoteActivation r;
153 struct DUALSTRINGARRAY dualstring;
156 status = dcom_connect_host(ctx, &p, server);
157 if (NT_STATUS_IS_ERR(status)) {
158 DEBUG(1, ("Unable to connect to %s - %s\n", server, nt_errstr(status)));
159 return ntstatus_to_werror(status);
163 r.in.this.version.MajorVersion = COM_MAJOR_VERSION;
164 r.in.this.version.MinorVersion = COM_MINOR_VERSION;
165 r.in.this.cid = GUID_random();
167 r.in.ClientImpLevel = RPC_C_IMP_LEVEL_IDENTIFY;
168 r.in.num_protseqs = ARRAY_SIZE(protseq);
169 r.in.protseq = protseq;
170 r.in.Interfaces = num_ifaces;
172 r.out.ifaces = talloc_array(ctx, struct pMInterfacePointer, num_ifaces);
173 r.out.pdsaOxidBindings = &dualstring;
175 status = dcerpc_RemoteActivation(p, ctx, &r);
176 if(NT_STATUS_IS_ERR(status)) {
177 DEBUG(1, ("Error while running RemoteActivation %s\n", nt_errstr(status)));
178 return ntstatus_to_werror(status);
181 if(!W_ERROR_IS_OK(r.out.result)) {
185 if(!W_ERROR_IS_OK(r.out.hr)) {
189 *ip = talloc_array(ctx, struct IUnknown *, num_ifaces);
190 for (i = 0; i < num_ifaces; i++) {
191 results[i] = r.out.results[i];
193 if (W_ERROR_IS_OK(results[i])) {
194 status = dcom_IUnknown_from_OBJREF(ctx, &(*ip)[i], &r.out.ifaces[i].ip->obj);
195 if (!NT_STATUS_IS_OK(status)) {
196 results[i] = ntstatus_to_werror(status);
201 /* Add the OXID data for the returned oxid */
202 m = object_exporter_by_oxid(ctx, r.out.pOxid);
203 m->bindings = *r.out.pdsaOxidBindings;
208 WERROR dcom_get_class_object(struct com_context *ctx, struct GUID *clsid, const char *server, struct GUID *iid, struct IUnknown **ip)
210 struct dcom_object_exporter *m;
211 struct RemoteActivation r;
212 struct dcerpc_pipe *p;
213 struct DUALSTRINGARRAY dualstring;
215 struct MInterfacePointer pm;
216 struct pMInterfacePointer ifaces[1];
217 uint16_t protseq[] = DCOM_NEGOTIATED_PROTOCOLS;
220 return com_get_class_object(ctx, clsid, iid, ip);
223 status = dcom_connect_host(ctx, &p, server);
224 if (NT_STATUS_IS_ERR(status)) {
225 DEBUG(1, ("Unable to connect to %s - %s\n", server, nt_errstr(status)));
226 return ntstatus_to_werror(status);
230 r.in.this.version.MajorVersion = COM_MAJOR_VERSION;
231 r.in.this.version.MinorVersion = COM_MINOR_VERSION;
232 r.in.this.cid = GUID_random();
234 r.in.ClientImpLevel = RPC_C_IMP_LEVEL_IDENTIFY;
235 r.in.num_protseqs = ARRAY_SIZE(protseq);
236 r.in.protseq = protseq;
239 r.in.Mode = MODE_GET_CLASS_OBJECT;
240 r.out.ifaces = ifaces;
242 r.out.pdsaOxidBindings = &dualstring;
244 status = dcerpc_RemoteActivation(p, ctx, &r);
245 if(NT_STATUS_IS_ERR(status)) {
246 DEBUG(1, ("Error while running RemoteActivation - %s\n", nt_errstr(status)));
247 return ntstatus_to_werror(status);
250 if(!W_ERROR_IS_OK(r.out.result)) { return r.out.result; }
251 if(!W_ERROR_IS_OK(r.out.hr)) { return r.out.hr; }
252 if(!W_ERROR_IS_OK(r.out.results[0])) { return r.out.results[0]; }
254 /* Set up the interface data */
255 dcom_IUnknown_from_OBJREF(ctx, ip, &pm.obj);
257 /* Add the OXID data for the returned oxid */
258 m = object_exporter_by_oxid(ctx, r.out.pOxid);
259 m->bindings = *r.out.pdsaOxidBindings;
264 NTSTATUS dcom_get_pipe (struct IUnknown *iface, struct dcerpc_pipe **pp)
266 struct dcerpc_binding *binding;
271 struct dcerpc_pipe *p;
274 struct dcom_object_exporter *ox;
276 ox = object_exporter_by_ip(iface->ctx, iface);
278 tmp_ctx = talloc_new(NULL);
282 iid = iface->vtable->iid;
284 uuid = GUID_string(tmp_ctx, &iid);
287 if (!GUID_equal(&p->syntax.uuid, &iid)) {
288 struct dcerpc_pipe *p2;
289 ox->pipe->syntax.uuid = iid;
290 status = dcerpc_secondary_context(p, &p2, uuid, 0);
291 if (NT_STATUS_IS_OK(status)) {
295 p = talloc_reference(NULL, p);
298 talloc_free(tmp_ctx);
304 status = dcerpc_binding_from_STRINGBINDING(iface->ctx, &binding,
305 ox->bindings.stringbindings[i]);
306 if (!NT_STATUS_IS_OK(status)) {
307 DEBUG(1, ("Error parsing string binding"));
309 status = dcerpc_pipe_connect_b(&p, binding,
312 iface->ctx->dcom->domain,
313 iface->ctx->dcom->user,
314 iface->ctx->dcom->password);
316 talloc_free(binding);
318 } while (!NT_STATUS_IS_OK(status) && ox->bindings.stringbindings[i]);
320 if (NT_STATUS_IS_ERR(status)) {
321 DEBUG(0, ("Unable to connect to remote host - %s\n", nt_errstr(status)));
322 talloc_free(tmp_ctx);
326 DEBUG(2, ("Successfully connected to OXID %llx\n", oxid));
329 talloc_free(tmp_ctx);
334 NTSTATUS dcom_OBJREF_from_IUnknown(struct OBJREF *o, struct IUnknown *p)
336 /* FIXME: Cache generated objref objects? */
339 o->signature = OBJREF_SIGNATURE;
342 o->flags = OBJREF_NULL;
344 o->iid = p->vtable->iid;
352 return NT_STATUS_NOT_IMPLEMENTED;
355 NTSTATUS dcom_IUnknown_from_OBJREF(struct com_context *ctx, struct IUnknown **_p, struct OBJREF *o)
358 struct dcom_object_exporter *ox;
365 case OBJREF_STANDARD:
366 p = talloc(ctx, struct IUnknown);
368 p->vtable = dcom_proxy_vtable_by_iid(&o->iid);
370 DEBUG(0, ("Unable to find proxy class for interface with IID %s\n", GUID_string(ctx, &o->iid)));
371 return NT_STATUS_NOT_SUPPORTED;
374 ox = object_exporter_by_oxid(ctx, o->u_objref.u_standard.std.oxid);
375 /* FIXME: Add object to list of objects to ping */
380 p = talloc(ctx, struct IUnknown);
382 ox = object_exporter_by_oxid(ctx, o->u_objref.u_handler.std.oxid );
383 /* FIXME: Add object to list of objects to ping */
384 /*FIXME p->vtable = dcom_vtable_by_clsid(&o->u_objref.u_handler.clsid);*/
385 /* FIXME: Do the custom unmarshaling call */
391 p = talloc(ctx, struct IUnknown);
394 /* FIXME: Do the actual custom unmarshaling call */
396 return NT_STATUS_NOT_SUPPORTED;
399 return NT_STATUS_NOT_SUPPORTED;
402 uint64_t dcom_get_current_oxid(void)