08a928cda6b83da67b45194aee144c071a07db12
[kai/samba.git] / source4 / lib / com / dcom / main.c
1 /*
2    Unix SMB/CIFS implementation.
3    Main DCOM functionality
4    Copyright (C) 2004 Jelmer Vernooij <jelmer@samba.org>
5
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.
10
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.
15
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.
19 */
20
21 #include "includes.h"
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"
30
31 #define DCOM_NEGOTIATED_PROTOCOLS { EPM_PROTOCOL_TCP, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NCALRPC }
32
33 struct dcom_client_context *dcom_client_init(struct com_context *ctx, struct cli_credentials *credentials)
34 {
35         ctx->dcom = talloc(ctx, struct dcom_client_context);
36         ctx->dcom->credentials = credentials;
37
38         return ctx->dcom;
39 }
40
41 static NTSTATUS dcerpc_binding_from_STRINGBINDING(TALLOC_CTX *mem_ctx, struct dcerpc_binding **b_out, struct STRINGBINDING *bd)
42 {
43         char *host, *endpoint;
44         struct dcerpc_binding *b;
45
46         b = talloc_zero(mem_ctx, struct dcerpc_binding);
47         if (!b) {
48                 return NT_STATUS_NO_MEMORY;
49         }
50         
51         b->transport = dcerpc_transport_by_endpoint_protocol(bd->wTowerId);
52
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;
56         }
57
58         host = talloc_strdup(b, bd->NetworkAddr);
59         endpoint = strchr(host, '[');
60
61         if (endpoint) {
62                 *endpoint = '\0';
63                 endpoint++;
64
65                 endpoint[strlen(endpoint)-1] = '\0';
66         }
67
68         b->host = host;
69         b->endpoint = talloc_strdup(b, endpoint);
70
71         *b_out = b;
72         return NT_STATUS_OK;
73 }
74
75 static NTSTATUS dcom_connect_host(struct com_context *ctx, struct dcerpc_pipe **p, const char *server)
76 {
77         struct dcerpc_binding *bd;
78         const char * available_transports[] = { "ncacn_ip_tcp", "ncacn_np" };
79         int i;
80         NTSTATUS status;
81         TALLOC_CTX *mem_ctx = talloc_init("dcom_connect");
82
83         if (server == NULL) { 
84                 return dcerpc_pipe_connect(ctx, p, "ncalrpc", 
85                                            DCERPC_IREMOTEACTIVATION_UUID, 
86                                            DCERPC_IREMOTEACTIVATION_VERSION,
87                                            ctx->dcom->credentials);
88         }
89
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);
96
97                 talloc_free(mem_ctx);
98                 return status;
99         }
100
101         for (i = 0; i < ARRAY_SIZE(available_transports); i++)
102         {
103                 char *binding = talloc_asprintf(mem_ctx, "%s:%s", available_transports[i], server);
104                 if (!binding) {
105                         talloc_free(mem_ctx);
106                         return NT_STATUS_NO_MEMORY;
107                 }
108                 
109                 status = dcerpc_pipe_connect(ctx, p, binding, 
110                                              DCERPC_IREMOTEACTIVATION_UUID, 
111                                              DCERPC_IREMOTEACTIVATION_VERSION, 
112                                                  ctx->dcom->credentials);
113
114                 if (NT_STATUS_IS_OK(status)) {
115                         talloc_free(mem_ctx);
116                         return status;
117                 }
118         }
119         
120         talloc_free(mem_ctx);
121         return status;
122 }
123
124 struct dcom_object_exporter *object_exporter_by_oxid(struct com_context *ctx, uint64_t oxid)
125 {
126         struct dcom_object_exporter *ox;
127         for (ox = ctx->dcom->object_exporters; ox; ox = ox->next) {
128                 if (ox->oxid == oxid) {
129                         return ox;
130                 }
131         }
132
133         return NULL; 
134 }
135
136 struct dcom_object_exporter *object_exporter_by_ip(struct com_context *ctx, struct IUnknown *ip)
137 {
138         return NULL; /* FIXME */
139 }
140
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)
142 {
143         uint16_t protseq[] = DCOM_NEGOTIATED_PROTOCOLS;
144         struct dcerpc_pipe *p;
145         struct dcom_object_exporter *m;
146         NTSTATUS status;
147         struct RemoteActivation r;
148         struct DUALSTRINGARRAY dualstring;
149         int i;
150
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);
155         }
156
157         ZERO_STRUCT(r.in);
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();
161         r.in.Clsid = *clsid;
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;
166         r.in.pIIDs = iid;
167         r.out.ifaces = talloc_array(ctx, struct pMInterfacePointer, num_ifaces);
168         r.out.pdsaOxidBindings = &dualstring;
169         
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);
174         }
175
176         if(!W_ERROR_IS_OK(r.out.result)) {
177                 return r.out.result; 
178         }
179         
180         if(!W_ERROR_IS_OK(r.out.hr)) { 
181                 return r.out.hr; 
182         }
183
184         *ip = talloc_array(ctx, struct IUnknown *, num_ifaces);
185         for (i = 0; i < num_ifaces; i++) {
186                 results[i] = r.out.results[i];
187                 (*ip)[i] = NULL;
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);
192                         }
193                 }
194         }
195
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;
199         
200         return WERR_OK;
201 }
202
203 WERROR dcom_get_class_object(struct com_context *ctx, struct GUID *clsid, const char *server, struct GUID *iid, struct IUnknown **ip)
204 {
205         struct dcom_object_exporter *m;
206         struct RemoteActivation r;
207         struct dcerpc_pipe *p;
208         struct DUALSTRINGARRAY dualstring;
209         NTSTATUS status;
210         struct MInterfacePointer pm;
211         struct pMInterfacePointer ifaces[1];
212         uint16_t protseq[] = DCOM_NEGOTIATED_PROTOCOLS;
213
214         if (!server) {
215                 return com_get_class_object(ctx, clsid, iid, ip);
216         }
217
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);
222         }
223
224         ZERO_STRUCT(r.in);
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();
228         r.in.Clsid = *clsid;
229         r.in.ClientImpLevel = RPC_C_IMP_LEVEL_IDENTIFY;
230         r.in.num_protseqs = ARRAY_SIZE(protseq);
231         r.in.protseq = protseq;
232         r.in.Interfaces = 1;
233         r.in.pIIDs = iid;
234         r.in.Mode = MODE_GET_CLASS_OBJECT;
235         r.out.ifaces = ifaces;
236         ifaces[0].ip = &pm;
237         r.out.pdsaOxidBindings = &dualstring;
238
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);
243         }
244
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]; }
248         
249         /* Set up the interface data */
250         dcom_IUnknown_from_OBJREF(ctx, ip, &pm.obj);
251         
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;
255
256         return WERR_OK;
257 }
258
259 NTSTATUS dcom_get_pipe (struct IUnknown *iface, struct dcerpc_pipe **pp)
260 {
261         struct dcerpc_binding *binding;
262         struct GUID iid;
263         uint64_t oxid;
264         NTSTATUS status;
265         int i;
266         struct dcerpc_pipe *p;
267         TALLOC_CTX *tmp_ctx;
268         const char *uuid;
269         struct dcom_object_exporter *ox;
270
271         ox = object_exporter_by_ip(iface->ctx, iface);
272
273         tmp_ctx = talloc_new(NULL);
274
275         p = ox->pipe;
276         
277         iid = iface->vtable->iid;
278
279         uuid = GUID_string(tmp_ctx, &iid);
280         
281         if (p) {
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)) {
287                                 p = p2;
288                         }
289                 } else {
290                         p = talloc_reference(NULL, p);
291                 }
292                 *pp = p;
293                 talloc_free(tmp_ctx);
294                 return status;
295         }
296
297         i = 0;
298         do {
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"));
303                 } else {
304                         status = dcerpc_pipe_connect_b(NULL, &p, binding, 
305                                                        uuid, 0.0, 
306                                                        iface->ctx->dcom->credentials);
307                 }
308                 talloc_free(binding);
309                 i++;
310         } while (!NT_STATUS_IS_OK(status) && ox->bindings.stringbindings[i]);
311
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);
315                 return status;
316         }
317
318         DEBUG(2, ("Successfully connected to OXID %llx\n", oxid));
319         
320         *pp = p;
321         talloc_free(tmp_ctx);
322
323         return NT_STATUS_OK;
324 }
325
326 NTSTATUS dcom_OBJREF_from_IUnknown(struct OBJREF *o, struct IUnknown *p)
327 {
328         /* FIXME: Cache generated objref objects? */
329         ZERO_STRUCTP(o);
330         
331         o->signature = OBJREF_SIGNATURE;
332         
333         if (!p) {
334                 o->flags = OBJREF_NULL;
335         } else {
336                 o->iid = p->vtable->iid;
337                 /* 
338                 OBJREF_STANDARD
339                 OBJREF_CUSTOM
340                 OBJREF_HANDLER
341                 */
342         }
343
344         return NT_STATUS_NOT_IMPLEMENTED;       
345 }
346
347 NTSTATUS dcom_IUnknown_from_OBJREF(struct com_context *ctx, struct IUnknown **_p, struct OBJREF *o)
348 {
349         struct IUnknown *p;
350         struct dcom_object_exporter *ox;
351
352         switch(o->flags) {
353         case OBJREF_NULL: 
354                 *_p = NULL;
355                 return NT_STATUS_OK;
356                 
357         case OBJREF_STANDARD:
358                 p = talloc(ctx, struct IUnknown);
359                 p->ctx = ctx;   
360                 p->vtable = dcom_proxy_vtable_by_iid(&o->iid);
361                 if (!p->vtable) {
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;
364                 }
365
366                 ox = object_exporter_by_oxid(ctx, o->u_objref.u_standard.std.oxid);
367                 /* FIXME: Add object to list of objects to ping */
368                 *_p = p;
369                 return NT_STATUS_OK;
370                 
371         case OBJREF_HANDLER:
372                 p = talloc(ctx, struct IUnknown);
373                 p->ctx = ctx;   
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 */
378         
379                 *_p = p;
380                 return NT_STATUS_OK;
381                 
382         case OBJREF_CUSTOM:
383                 p = talloc(ctx, struct IUnknown);
384                 p->ctx = ctx;   
385                 p->vtable = NULL;
386                 /* FIXME: Do the actual custom unmarshaling call */
387                 *_p = p;
388                 return NT_STATUS_NOT_SUPPORTED;
389         }
390
391         return NT_STATUS_NOT_SUPPORTED;
392 }
393
394 uint64_t dcom_get_current_oxid(void)
395 {
396         return getpid();
397 }