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, struct cli_credentials *credentials)
35 ctx->dcom = talloc(ctx, struct dcom_client_context);
36 ctx->dcom->credentials = credentials;
41 static NTSTATUS dcerpc_binding_from_STRINGBINDING(TALLOC_CTX *mem_ctx, struct dcerpc_binding **b_out, struct STRINGBINDING *bd)
43 char *host, *endpoint;
44 struct dcerpc_binding *b;
46 b = talloc_zero(mem_ctx, struct dcerpc_binding);
48 return NT_STATUS_NO_MEMORY;
51 b->transport = dcerpc_transport_by_endpoint_protocol(bd->wTowerId);
53 if (b->transport == -1) {
54 DEBUG(1, ("Can't find transport match endpoint protocol %d\n", bd->wTowerId));
55 return NT_STATUS_NOT_SUPPORTED;
58 host = talloc_strdup(b, bd->NetworkAddr);
59 endpoint = strchr(host, '[');
65 endpoint[strlen(endpoint)-1] = '\0';
69 b->endpoint = talloc_strdup(b, endpoint);
75 static NTSTATUS dcom_connect_host(struct com_context *ctx, struct dcerpc_pipe **p, const char *server)
77 struct dcerpc_binding *bd;
78 const char * available_transports[] = { "ncacn_ip_tcp", "ncacn_np" };
81 TALLOC_CTX *mem_ctx = talloc_init("dcom_connect");
84 return dcerpc_pipe_connect(ctx, p, "ncalrpc",
85 DCERPC_IREMOTEACTIVATION_UUID,
86 DCERPC_IREMOTEACTIVATION_VERSION,
87 ctx->dcom->credentials);
90 /* Allow server name to contain a binding string */
91 if (NT_STATUS_IS_OK(dcerpc_parse_binding(mem_ctx, server, &bd))) {
92 status = dcerpc_pipe_connect_b(ctx, p, bd,
93 DCERPC_IREMOTEACTIVATION_UUID,
94 DCERPC_IREMOTEACTIVATION_VERSION,
95 ctx->dcom->credentials);
101 for (i = 0; i < ARRAY_SIZE(available_transports); i++)
103 char *binding = talloc_asprintf(mem_ctx, "%s:%s", available_transports[i], server);
105 talloc_free(mem_ctx);
106 return NT_STATUS_NO_MEMORY;
109 status = dcerpc_pipe_connect(ctx, p, binding,
110 DCERPC_IREMOTEACTIVATION_UUID,
111 DCERPC_IREMOTEACTIVATION_VERSION,
112 ctx->dcom->credentials);
114 if (NT_STATUS_IS_OK(status)) {
115 talloc_free(mem_ctx);
120 talloc_free(mem_ctx);
124 struct dcom_object_exporter *object_exporter_by_oxid(struct com_context *ctx, uint64_t oxid)
126 struct dcom_object_exporter *ox;
127 for (ox = ctx->dcom->object_exporters; ox; ox = ox->next) {
128 if (ox->oxid == oxid) {
136 struct dcom_object_exporter *object_exporter_by_ip(struct com_context *ctx, struct IUnknown *ip)
138 return NULL; /* FIXME */
141 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)
143 uint16_t protseq[] = DCOM_NEGOTIATED_PROTOCOLS;
144 struct dcerpc_pipe *p;
145 struct dcom_object_exporter *m;
147 struct RemoteActivation r;
148 struct DUALSTRINGARRAY dualstring;
151 status = dcom_connect_host(ctx, &p, server);
152 if (NT_STATUS_IS_ERR(status)) {
153 DEBUG(1, ("Unable to connect to %s - %s\n", server, nt_errstr(status)));
154 return ntstatus_to_werror(status);
158 r.in.this.version.MajorVersion = COM_MAJOR_VERSION;
159 r.in.this.version.MinorVersion = COM_MINOR_VERSION;
160 r.in.this.cid = GUID_random();
162 r.in.ClientImpLevel = RPC_C_IMP_LEVEL_IDENTIFY;
163 r.in.num_protseqs = ARRAY_SIZE(protseq);
164 r.in.protseq = protseq;
165 r.in.Interfaces = num_ifaces;
167 r.out.ifaces = talloc_array(ctx, struct pMInterfacePointer, num_ifaces);
168 r.out.pdsaOxidBindings = &dualstring;
170 status = dcerpc_RemoteActivation(p, ctx, &r);
171 if(NT_STATUS_IS_ERR(status)) {
172 DEBUG(1, ("Error while running RemoteActivation %s\n", nt_errstr(status)));
173 return ntstatus_to_werror(status);
176 if(!W_ERROR_IS_OK(r.out.result)) {
180 if(!W_ERROR_IS_OK(r.out.hr)) {
184 *ip = talloc_array(ctx, struct IUnknown *, num_ifaces);
185 for (i = 0; i < num_ifaces; i++) {
186 results[i] = r.out.results[i];
188 if (W_ERROR_IS_OK(results[i])) {
189 status = dcom_IUnknown_from_OBJREF(ctx, &(*ip)[i], &r.out.ifaces[i].ip->obj);
190 if (!NT_STATUS_IS_OK(status)) {
191 results[i] = ntstatus_to_werror(status);
196 /* Add the OXID data for the returned oxid */
197 m = object_exporter_by_oxid(ctx, r.out.pOxid);
198 m->bindings = *r.out.pdsaOxidBindings;
203 WERROR dcom_get_class_object(struct com_context *ctx, struct GUID *clsid, const char *server, struct GUID *iid, struct IUnknown **ip)
205 struct dcom_object_exporter *m;
206 struct RemoteActivation r;
207 struct dcerpc_pipe *p;
208 struct DUALSTRINGARRAY dualstring;
210 struct MInterfacePointer pm;
211 struct pMInterfacePointer ifaces[1];
212 uint16_t protseq[] = DCOM_NEGOTIATED_PROTOCOLS;
215 return com_get_class_object(ctx, clsid, iid, ip);
218 status = dcom_connect_host(ctx, &p, server);
219 if (NT_STATUS_IS_ERR(status)) {
220 DEBUG(1, ("Unable to connect to %s - %s\n", server, nt_errstr(status)));
221 return ntstatus_to_werror(status);
225 r.in.this.version.MajorVersion = COM_MAJOR_VERSION;
226 r.in.this.version.MinorVersion = COM_MINOR_VERSION;
227 r.in.this.cid = GUID_random();
229 r.in.ClientImpLevel = RPC_C_IMP_LEVEL_IDENTIFY;
230 r.in.num_protseqs = ARRAY_SIZE(protseq);
231 r.in.protseq = protseq;
234 r.in.Mode = MODE_GET_CLASS_OBJECT;
235 r.out.ifaces = ifaces;
237 r.out.pdsaOxidBindings = &dualstring;
239 status = dcerpc_RemoteActivation(p, ctx, &r);
240 if(NT_STATUS_IS_ERR(status)) {
241 DEBUG(1, ("Error while running RemoteActivation - %s\n", nt_errstr(status)));
242 return ntstatus_to_werror(status);
245 if(!W_ERROR_IS_OK(r.out.result)) { return r.out.result; }
246 if(!W_ERROR_IS_OK(r.out.hr)) { return r.out.hr; }
247 if(!W_ERROR_IS_OK(r.out.results[0])) { return r.out.results[0]; }
249 /* Set up the interface data */
250 dcom_IUnknown_from_OBJREF(ctx, ip, &pm.obj);
252 /* Add the OXID data for the returned oxid */
253 m = object_exporter_by_oxid(ctx, r.out.pOxid);
254 m->bindings = *r.out.pdsaOxidBindings;
259 NTSTATUS dcom_get_pipe (struct IUnknown *iface, struct dcerpc_pipe **pp)
261 struct dcerpc_binding *binding;
266 struct dcerpc_pipe *p;
269 struct dcom_object_exporter *ox;
271 ox = object_exporter_by_ip(iface->ctx, iface);
273 tmp_ctx = talloc_new(NULL);
277 iid = iface->vtable->iid;
279 uuid = GUID_string(tmp_ctx, &iid);
282 if (!GUID_equal(&p->syntax.uuid, &iid)) {
283 struct dcerpc_pipe *p2;
284 ox->pipe->syntax.uuid = iid;
285 status = dcerpc_secondary_context(p, &p2, uuid, 0);
286 if (NT_STATUS_IS_OK(status)) {
290 p = talloc_reference(NULL, p);
293 talloc_free(tmp_ctx);
299 status = dcerpc_binding_from_STRINGBINDING(iface->ctx, &binding,
300 ox->bindings.stringbindings[i]);
301 if (!NT_STATUS_IS_OK(status)) {
302 DEBUG(1, ("Error parsing string binding"));
304 status = dcerpc_pipe_connect_b(NULL, &p, binding,
306 iface->ctx->dcom->credentials);
308 talloc_free(binding);
310 } while (!NT_STATUS_IS_OK(status) && ox->bindings.stringbindings[i]);
312 if (NT_STATUS_IS_ERR(status)) {
313 DEBUG(0, ("Unable to connect to remote host - %s\n", nt_errstr(status)));
314 talloc_free(tmp_ctx);
318 DEBUG(2, ("Successfully connected to OXID %llx\n", oxid));
321 talloc_free(tmp_ctx);
326 NTSTATUS dcom_OBJREF_from_IUnknown(struct OBJREF *o, struct IUnknown *p)
328 /* FIXME: Cache generated objref objects? */
331 o->signature = OBJREF_SIGNATURE;
334 o->flags = OBJREF_NULL;
336 o->iid = p->vtable->iid;
344 return NT_STATUS_NOT_IMPLEMENTED;
347 NTSTATUS dcom_IUnknown_from_OBJREF(struct com_context *ctx, struct IUnknown **_p, struct OBJREF *o)
350 struct dcom_object_exporter *ox;
357 case OBJREF_STANDARD:
358 p = talloc(ctx, struct IUnknown);
360 p->vtable = dcom_proxy_vtable_by_iid(&o->iid);
362 DEBUG(0, ("Unable to find proxy class for interface with IID %s\n", GUID_string(ctx, &o->iid)));
363 return NT_STATUS_NOT_SUPPORTED;
366 ox = object_exporter_by_oxid(ctx, o->u_objref.u_standard.std.oxid);
367 /* FIXME: Add object to list of objects to ping */
372 p = talloc(ctx, struct IUnknown);
374 ox = object_exporter_by_oxid(ctx, o->u_objref.u_handler.std.oxid );
375 /* FIXME: Add object to list of objects to ping */
376 /*FIXME p->vtable = dcom_vtable_by_clsid(&o->u_objref.u_handler.clsid);*/
377 /* FIXME: Do the custom unmarshaling call */
383 p = talloc(ctx, struct IUnknown);
386 /* FIXME: Do the actual custom unmarshaling call */
388 return NT_STATUS_NOT_SUPPORTED;
391 return NT_STATUS_NOT_SUPPORTED;
394 uint64_t dcom_get_current_oxid(void)