r12608: Remove some unused #include lines.
[abartlet/samba.git/.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 "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"
27
28 #define DCOM_NEGOTIATED_PROTOCOLS { EPM_PROTOCOL_TCP, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NCALRPC }
29
30 struct dcom_client_context *dcom_client_init(struct com_context *ctx, struct cli_credentials *credentials)
31 {
32         ctx->dcom = talloc(ctx, struct dcom_client_context);
33         ctx->dcom->credentials = credentials;
34
35         return ctx->dcom;
36 }
37
38 static NTSTATUS dcerpc_binding_from_STRINGBINDING(TALLOC_CTX *mem_ctx, struct dcerpc_binding **b_out, struct STRINGBINDING *bd)
39 {
40         char *host, *endpoint;
41         struct dcerpc_binding *b;
42
43         b = talloc_zero(mem_ctx, struct dcerpc_binding);
44         if (!b) {
45                 return NT_STATUS_NO_MEMORY;
46         }
47         
48         b->transport = dcerpc_transport_by_endpoint_protocol(bd->wTowerId);
49
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;
53         }
54
55         host = talloc_strdup(b, bd->NetworkAddr);
56         endpoint = strchr(host, '[');
57
58         if (endpoint) {
59                 *endpoint = '\0';
60                 endpoint++;
61
62                 endpoint[strlen(endpoint)-1] = '\0';
63         }
64
65         b->host = host;
66         b->endpoint = talloc_strdup(b, endpoint);
67
68         *b_out = b;
69         return NT_STATUS_OK;
70 }
71
72 static NTSTATUS dcom_connect_host(struct com_context *ctx, struct dcerpc_pipe **p, const char *server)
73 {
74         struct dcerpc_binding *bd;
75         const char * available_transports[] = { "ncacn_ip_tcp", "ncacn_np" };
76         int i;
77         NTSTATUS status;
78         TALLOC_CTX *mem_ctx = talloc_init("dcom_connect");
79
80         if (server == NULL) { 
81                 return dcerpc_pipe_connect(ctx, p, "ncalrpc", 
82                                                                    &dcerpc_table_IRemoteActivation,
83                                                                    ctx->dcom->credentials, ctx->event_ctx);
84         }
85
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);
91
92                 talloc_free(mem_ctx);
93                 return status;
94         }
95
96         for (i = 0; i < ARRAY_SIZE(available_transports); i++)
97         {
98                 char *binding = talloc_asprintf(mem_ctx, "%s:%s", available_transports[i], server);
99                 if (!binding) {
100                         talloc_free(mem_ctx);
101                         return NT_STATUS_NO_MEMORY;
102                 }
103                 
104                 status = dcerpc_pipe_connect(ctx, p, binding, 
105                                                                          &dcerpc_table_IRemoteActivation,
106                                              ctx->dcom->credentials, ctx->event_ctx);
107
108                 if (NT_STATUS_IS_OK(status)) {
109                         talloc_free(mem_ctx);
110                         return status;
111                 }
112         }
113         
114         talloc_free(mem_ctx);
115         return status;
116 }
117
118 struct dcom_object_exporter *object_exporter_by_oxid(struct com_context *ctx, uint64_t oxid)
119 {
120         struct dcom_object_exporter *ox;
121         for (ox = ctx->dcom->object_exporters; ox; ox = ox->next) {
122                 if (ox->oxid == oxid) {
123                         return ox;
124                 }
125         }
126
127         return NULL; 
128 }
129
130 struct dcom_object_exporter *object_exporter_by_ip(struct com_context *ctx, struct IUnknown *ip)
131 {
132         return NULL; /* FIXME */
133 }
134
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)
136 {
137         uint16_t protseq[] = DCOM_NEGOTIATED_PROTOCOLS;
138         struct dcerpc_pipe *p;
139         struct dcom_object_exporter *m;
140         NTSTATUS status;
141         struct RemoteActivation r;
142         struct DUALSTRINGARRAY dualstring;
143         int i;
144
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);
149         }
150
151         ZERO_STRUCT(r.in);
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();
155         r.in.Clsid = *clsid;
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;
160         r.in.pIIDs = iid;
161         r.out.ifaces = talloc_array(ctx, struct MInterfacePointer *, num_ifaces);
162         r.out.pdsaOxidBindings = &dualstring;
163         
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);
168         }
169
170         if(!W_ERROR_IS_OK(r.out.result)) {
171                 return r.out.result; 
172         }
173         
174         if(!W_ERROR_IS_OK(r.out.hr)) { 
175                 return r.out.hr; 
176         }
177
178         *ip = talloc_array(ctx, struct IUnknown *, num_ifaces);
179         for (i = 0; i < num_ifaces; i++) {
180                 results[i] = r.out.results[i];
181                 (*ip)[i] = NULL;
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);
186                         }
187                 }
188         }
189
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;
193         
194         return WERR_OK;
195 }
196
197 WERROR dcom_get_class_object(struct com_context *ctx, struct GUID *clsid, const char *server, struct GUID *iid, struct IUnknown **ip)
198 {
199         struct dcom_object_exporter *m;
200         struct RemoteActivation r;
201         struct dcerpc_pipe *p;
202         struct DUALSTRINGARRAY dualstring;
203         NTSTATUS status;
204         struct MInterfacePointer pm;
205         struct MInterfacePointer *ifaces[1];
206         uint16_t protseq[] = DCOM_NEGOTIATED_PROTOCOLS;
207
208         if (!server) {
209                 return com_get_class_object(ctx, clsid, iid, ip);
210         }
211
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);
216         }
217
218         ZERO_STRUCT(r.in);
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();
222         r.in.Clsid = *clsid;
223         r.in.ClientImpLevel = RPC_C_IMP_LEVEL_IDENTIFY;
224         r.in.num_protseqs = ARRAY_SIZE(protseq);
225         r.in.protseq = protseq;
226         r.in.Interfaces = 1;
227         r.in.pIIDs = iid;
228         r.in.Mode = MODE_GET_CLASS_OBJECT;
229         r.out.ifaces = ifaces;
230         ifaces[0] = &pm;
231         r.out.pdsaOxidBindings = &dualstring;
232
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);
237         }
238
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]; }
242         
243         /* Set up the interface data */
244         dcom_IUnknown_from_OBJREF(ctx, ip, &pm.obj);
245         
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;
249
250         return WERR_OK;
251 }
252
253 NTSTATUS dcom_get_pipe(struct IUnknown *iface, struct dcerpc_pipe **pp)
254 {
255         struct dcerpc_binding *binding;
256         struct GUID iid;
257         uint64_t oxid;
258         NTSTATUS status;
259         int i;
260         struct dcerpc_pipe *p;
261         TALLOC_CTX *tmp_ctx;
262         struct dcom_object_exporter *ox;
263
264         ox = object_exporter_by_ip(iface->ctx, iface);
265
266         tmp_ctx = talloc_new(NULL);
267
268         p = ox->pipe;
269         
270         iid = iface->vtable->iid;
271
272         if (p) {
273                 if (!GUID_equal(&p->syntax.uuid, &iid)) {
274                         struct dcerpc_pipe *p2;
275                         ox->pipe->syntax.uuid = iid;
276
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));
280
281                         if (NT_STATUS_IS_OK(status)) {
282                                 p = p2;
283                         }
284                 } else {
285                         p = talloc_reference(NULL, p);
286                 }
287                 *pp = p;
288                 talloc_free(tmp_ctx);
289                 return status;
290         }
291
292         i = 0;
293         do {
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"));
298                 } else {
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);
303                 }
304                 talloc_free(binding);
305                 i++;
306         } while (!NT_STATUS_IS_OK(status) && ox->bindings.stringbindings[i]);
307
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);
311                 return status;
312         }
313
314         DEBUG(2, ("Successfully connected to OXID %llx\n", (long long)oxid));
315         
316         *pp = p;
317         talloc_free(tmp_ctx);
318
319         return NT_STATUS_OK;
320 }
321
322 NTSTATUS dcom_OBJREF_from_IUnknown(struct OBJREF *o, struct IUnknown *p)
323 {
324         /* FIXME: Cache generated objref objects? */
325         ZERO_STRUCTP(o);
326         
327         o->signature = OBJREF_SIGNATURE;
328         
329         if (!p) {
330                 o->flags = OBJREF_NULL;
331         } else {
332                 o->iid = p->vtable->iid;
333                 /* 
334                 OBJREF_STANDARD
335                 OBJREF_CUSTOM
336                 OBJREF_HANDLER
337                 */
338         }
339
340         return NT_STATUS_NOT_IMPLEMENTED;       
341 }
342
343 NTSTATUS dcom_IUnknown_from_OBJREF(struct com_context *ctx, struct IUnknown **_p, struct OBJREF *o)
344 {
345         struct IUnknown *p;
346         struct dcom_object_exporter *ox;
347
348         switch(o->flags) {
349         case OBJREF_NULL: 
350                 *_p = NULL;
351                 return NT_STATUS_OK;
352                 
353         case OBJREF_STANDARD:
354                 p = talloc(ctx, struct IUnknown);
355                 p->ctx = ctx;   
356                 p->vtable = dcom_proxy_vtable_by_iid(&o->iid);
357                 if (!p->vtable) {
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;
360                 }
361
362                 ox = object_exporter_by_oxid(ctx, o->u_objref.u_standard.std.oxid);
363                 /* FIXME: Add object to list of objects to ping */
364                 *_p = p;
365                 return NT_STATUS_OK;
366                 
367         case OBJREF_HANDLER:
368                 p = talloc(ctx, struct IUnknown);
369                 p->ctx = ctx;   
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 */
374         
375                 *_p = p;
376                 return NT_STATUS_OK;
377                 
378         case OBJREF_CUSTOM:
379                 p = talloc(ctx, struct IUnknown);
380                 p->ctx = ctx;   
381                 p->vtable = NULL;
382                 /* FIXME: Do the actual custom unmarshaling call */
383                 *_p = p;
384                 return NT_STATUS_NOT_SUPPORTED;
385         }
386
387         return NT_STATUS_NOT_SUPPORTED;
388 }
389
390 uint64_t dcom_get_current_oxid(void)
391 {
392         return getpid();
393 }