More changes getting WMI code to compile, import manual marshalling.
[sfrench/samba-autobuild/.git] / source4 / lib / wmi / wbemdata.c
1 /*
2    WMI Implementation
3    Copyright (C) 2006 Andrzej Hajda <andrzej.hajda@wp.pl>
4    Copyright (C) 2008 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 "librpc/gen_ndr/dcom.h"
23 #include "librpc/gen_ndr/com_dcom.h"
24 #include "librpc/ndr/libndr.h"
25 #include "librpc/ndr/libndr_proto.h"
26 #include "lib/com/com.h"
27 #include "lib/com/dcom/dcom.h"
28 #include "lib/util/dlinklist.h"
29 #include "librpc/ndr/libndr.h"
30 #include "librpc/gen_ndr/ndr_dcom.h"
31 #include "librpc/rpc/dcerpc.h"
32 #include "librpc/gen_ndr/ndr_misc.h"
33 #include "lib/talloc/talloc.h"
34 #include "libcli/composite/composite.h"
35 #include "lib/wmi/wmi.h"
36 #include "librpc/ndr/ndr_wmi.h"
37
38 enum {
39         DATATYPE_CLASSOBJECT = 2,
40         DATATYPE_OBJECT = 3,
41         COFLAG_IS_CLASS = 4,
42 };
43
44 static enum ndr_err_code marshal(TALLOC_CTX *mem_ctx, struct IUnknown *pv, struct OBJREF *o)
45 {
46         struct ndr_push *ndr;
47         struct WbemClassObject *wco;
48         struct MInterfacePointer *mp;
49
50         mp = (struct MInterfacePointer *)((char *)o - offsetof(struct MInterfacePointer, obj)); /* FIXME:high remove this Mumbo Jumbo */
51         wco = pv->object_data;
52         ndr = talloc_zero(mem_ctx, struct ndr_push);
53         ndr->flags = 0;
54         ndr->alloc_size = 1024;
55         ndr->data = talloc_array(mp, uint8_t, ndr->alloc_size);
56
57         if (wco) {
58                 uint32_t ofs;
59                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0x12345678));
60                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
61                 NDR_CHECK(ndr_push_WbemClassObject(ndr, NDR_SCALARS | NDR_BUFFERS, wco));
62                 ofs = ndr->offset;
63                 ndr->offset = 4;
64                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ofs - 8));
65                 ndr->offset = ofs;
66         } else {
67                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
68         }
69         o->u_objref.u_custom.pData = talloc_realloc(mp, ndr->data, uint8_t, ndr->offset);
70         o->u_objref.u_custom.size = ndr->offset;
71         mp->size = sizeof(struct OBJREF) - sizeof(union OBJREF_Types) + sizeof(struct u_custom) + o->u_objref.u_custom.size - 4;
72         if (DEBUGLVL(9)) {
73                 NDR_PRINT_DEBUG(WbemClassObject, wco);
74         }
75         return NDR_ERR_SUCCESS;
76 }
77
78 static enum ndr_err_code unmarshal(TALLOC_CTX *mem_ctx, struct OBJREF *o, struct IUnknown **pv)
79 {
80         struct ndr_pull *ndr;
81         struct WbemClassObject *wco;
82         enum ndr_err_code ndr_err;
83         uint32_t u;
84
85         mem_ctx = talloc_new(0);
86         ndr = talloc_zero(mem_ctx, struct ndr_pull);
87         ndr->current_mem_ctx = mem_ctx;
88         ndr->data = o->u_objref.u_custom.pData;
89         ndr->data_size = o->u_objref.u_custom.size;
90
91         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
92         if (!u) {
93                 talloc_free(*pv);
94                 *pv = NULL;
95                 return NDR_ERR_SUCCESS;
96         }
97         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
98         if (u + 8 > ndr->data_size) {
99                 DEBUG(1, ("unmarshall_IWbemClassObject: Incorrect data_size"));
100                 return NDR_ERR_BUFSIZE;
101         }
102         wco = talloc_zero(*pv, struct WbemClassObject);
103         ndr->current_mem_ctx = wco;
104         ndr_err = ndr_pull_WbemClassObject(ndr, NDR_SCALARS | NDR_BUFFERS, wco);
105
106         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLVL(9))) {
107                 NDR_PRINT_DEBUG(WbemClassObject, wco);
108         }
109
110         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
111                 (*pv)->object_data = wco;
112         } else {
113                 talloc_free(wco);
114         }
115         return NDR_ERR_SUCCESS;
116 }
117
118 WERROR dcom_IWbemClassObject_from_WbemClassObject(struct com_context *ctx, struct IWbemClassObject **_p, struct WbemClassObject *wco)
119 {
120         struct IWbemClassObject *p;
121
122         p = talloc_zero(ctx, struct IWbemClassObject);
123         p->ctx = ctx;
124         p->obj.signature = 0x574f454d;
125         p->obj.flags = OBJREF_CUSTOM;
126         GUID_from_string("dc12a681-737f-11cf-884d-00aa004b2e24", &p->obj.iid);
127         GUID_from_string("4590f812-1d3a-11d0-891f-00aa004b2e24", &p->obj.u_objref.u_custom.clsid);
128         p->object_data = (void *)wco;
129         talloc_steal(p, p->object_data);
130         *_p = p;
131         return WERR_OK;
132 }
133
134 WERROR IWbemClassObject_GetMethod(struct IWbemClassObject *d, TALLOC_CTX *mem_ctx, const char *name, uint32_t flags, struct IWbemClassObject **in, struct IWbemClassObject **out)
135 {
136         uint32_t i;
137         struct WbemClassObject *wco;
138
139         wco = (struct WbemClassObject *)d->object_data;
140         for (i = 0; i < wco->obj_methods->count; ++i)
141                 if (!strcmp(wco->obj_methods->method[i].name, name)) {
142                         if (in) dcom_IWbemClassObject_from_WbemClassObject(d->ctx, in, wco->obj_methods->method[i].in);
143                         if (out) dcom_IWbemClassObject_from_WbemClassObject(d->ctx, out, wco->obj_methods->method[i].out);
144                         return WERR_OK;
145                 }
146         return WERR_NOT_FOUND;
147 }
148
149 void WbemClassObject_CreateInstance(struct WbemClassObject *wco)
150 {
151         uint32_t i;
152
153         wco->instance = talloc_zero(wco, struct WbemInstance);
154         wco->instance->default_flags = talloc_array(wco->instance, uint8_t, wco->obj_class->__PROPERTY_COUNT);
155         wco->instance->data = talloc_array(wco->instance, union CIMVAR, wco->obj_class->__PROPERTY_COUNT);
156         memset(wco->instance->data, 0, sizeof(union CIMVAR) * wco->obj_class->__PROPERTY_COUNT);
157         for (i = 0; i < wco->obj_class->__PROPERTY_COUNT; ++i) {
158                 wco->instance->default_flags[i] = 1; /* FIXME:high resolve this magic */
159         }
160         wco->instance->__CLASS = wco->obj_class->__CLASS;
161         wco->instance->u2_4 = 4;
162         wco->instance->u3_1 = 1;
163 }
164
165 WERROR IWbemClassObject_Clone(struct IWbemClassObject *d, TALLOC_CTX *mem_ctx, struct IWbemClassObject **copy)
166 {
167         return WERR_NOT_SUPPORTED;
168 }
169
170 WERROR IWbemClassObject_SpawnInstance(struct IWbemClassObject *d, TALLOC_CTX *mem_ctx, uint32_t flags, struct IWbemClassObject **instance)
171 {
172         struct WbemClassObject *wco, *nwco;
173
174         wco = (struct WbemClassObject *)d->object_data;
175         nwco = talloc_zero(mem_ctx, struct WbemClassObject);
176         nwco->flags = WCF_INSTANCE;
177         nwco->obj_class = wco->obj_class;
178         WbemClassObject_CreateInstance(nwco);
179         dcom_IWbemClassObject_from_WbemClassObject(d->ctx, instance, nwco);
180         return WERR_OK;
181 }
182
183 void duplicate_WbemQualifier(TALLOC_CTX *mem_ctx, const struct WbemQualifier *src, struct WbemQualifier *dst)
184 {
185         dst->name = src->name;
186         if (src->name) dst->name = talloc_strdup(mem_ctx, src->name);
187
188         dst->flavors = src->flavors;
189
190         dst->cimtype = src->cimtype;
191
192         duplicate_CIMVAR(mem_ctx, &src->value, &dst->value, src->cimtype);
193 }
194
195 void duplicate_CIMSTRINGS(TALLOC_CTX *mem_ctx, const struct CIMSTRINGS *src, struct CIMSTRINGS *dst)
196 {
197         uint32_t i;
198
199         dst->count = src->count;
200         for (i = 0; i < src->count; ++i)
201                 dst->item[i] = talloc_strdup(mem_ctx, src->item[i]);
202 }
203
204 void duplicate_WbemQualifiers(TALLOC_CTX *mem_ctx, const struct WbemQualifiers *src, struct WbemQualifiers *dst)
205 {
206         uint32_t i;
207
208         dst->count = src->count;
209         for (i = 0; i < src->count; ++i) {
210                 dst->item[i] = talloc_zero(mem_ctx, struct WbemQualifier);
211                 duplicate_WbemQualifier(dst->item[i], src->item[i], dst->item[i]);
212         }
213 }
214
215 void duplicate_WbemClass(TALLOC_CTX *mem_ctx, const struct WbemClass *src, struct WbemClass *dst)
216 {
217         uint32_t i;
218
219         dst->u_0 = src->u_0;
220
221         dst->__CLASS = src->__CLASS;
222         if (src->__CLASS) dst->__CLASS = talloc_strdup(mem_ctx, src->__CLASS);
223
224         duplicate_CIMSTRINGS(mem_ctx, &src->__DERIVATION, &dst->__DERIVATION);
225         duplicate_WbemQualifiers(mem_ctx, &src->qualifiers, &dst->qualifiers);
226
227         dst->__PROPERTY_COUNT = src->__PROPERTY_COUNT;
228
229         dst->properties = talloc_array(mem_ctx, struct WbemClassProperty, src->__PROPERTY_COUNT);
230         for (i = 0; i < src->__PROPERTY_COUNT; ++i) {
231                 dst->properties[i].property.name = talloc_strdup(dst->properties, src->properties[i].property.name);
232                 dst->properties[i].property.desc = talloc_memdup(dst->properties, src->properties[i].property.desc, sizeof(*src->properties[i].property.desc));
233                 duplicate_WbemQualifiers(dst->properties[i].property.desc, &src->properties[i].property.desc->qualifiers, &dst->properties[i].property.desc->qualifiers);
234         }
235
236         for (i = 0; i < src->__PROPERTY_COUNT; ++i) {
237                 dst->properties[i].default_flags = src->properties[i].default_flags;
238                 duplicate_CIMVAR(dst->properties, &src->properties[i].default_values, &dst->properties[i].default_values, src->properties[i].property.desc->cimtype);
239         }
240 }
241
242 void duplicate_WbemMethod(TALLOC_CTX *mem_ctx, const struct WbemMethod *src, struct WbemMethod *dst)
243 {
244         dst->name = src->name;
245         if (src->name) dst->name = talloc_strdup(mem_ctx, src->name);
246         
247         dst->u0 = src->u0;
248         dst->u1 = src->u1;
249         
250         dst->qualifiers = talloc_zero(mem_ctx, struct WbemQualifiers);
251         duplicate_WbemQualifiers(dst->qualifiers, src->qualifiers, dst->qualifiers);
252
253         dst->in = src->in;
254         if (src->in) {
255                 dst->in = talloc_zero(mem_ctx, struct WbemClassObject);
256                 duplicate_WbemClassObject(dst->in, src->in, dst->in);
257         }
258
259         dst->out = src->out;
260         if (src->out) {
261                 dst->out = talloc_zero(mem_ctx, struct WbemClassObject);
262                 duplicate_WbemClassObject(dst->out, src->out, dst->out);
263         }
264 }
265
266 void duplicate_WbemMethods(TALLOC_CTX *mem_ctx, const struct WbemMethods *src, struct WbemMethods *dst)
267 {
268         uint32_t i;
269
270         dst->count = src->count;
271         dst->u0 = src->u0;
272         for (i = 0; i < src->count; ++i)
273                 duplicate_WbemMethod(mem_ctx, &src->method[i], &dst->method[i]);
274 }
275
276 void duplicate_WbemInstance(TALLOC_CTX *mem_ctx, const struct WbemInstance *src, struct WbemInstance *dst, const struct WbemClass *cls)
277 {
278         uint32_t i;
279
280         dst->u1_0 = src->u1_0;
281         
282         dst->__CLASS = src->__CLASS;
283         if (src->__CLASS) dst->__CLASS = talloc_strdup(mem_ctx, src->__CLASS);
284
285         dst->default_flags = talloc_array(mem_ctx, uint8_t, cls->__PROPERTY_COUNT);
286         dst->data = talloc_array(mem_ctx, union CIMVAR, cls->__PROPERTY_COUNT);
287         for (i = 0; i < cls->__PROPERTY_COUNT; ++i) {
288                 dst->default_flags[i] = src->default_flags[i];
289                 duplicate_CIMVAR(dst->data, &src->data[i], &dst->data[i], cls->properties[i].property.desc->cimtype);
290         }
291
292         dst->u2_4 = src->u2_4;
293         dst->u3_1 = src->u3_1;
294 }
295
296 void duplicate_WbemClassObject(TALLOC_CTX *mem_ctx, const struct WbemClassObject *src, struct WbemClassObject *dst)
297 {
298         dst->flags = src->flags;
299         if (src->flags & WCF_CLASS) {
300                 dst->__SERVER = talloc_strdup(mem_ctx, src->__SERVER);
301                 dst->__NAMESPACE = talloc_strdup(mem_ctx, src->__NAMESPACE);
302         }
303         if (src->flags & WCF_DECORATIONS) {
304                 dst->sup_class = talloc_zero(mem_ctx, struct WbemClass);
305                 duplicate_WbemClass(dst->sup_class, src->sup_class, dst->sup_class);
306
307                 dst->sup_methods = talloc_zero(mem_ctx, struct WbemMethods);
308                 duplicate_WbemMethods(dst->sup_methods, src->sup_methods, dst->sup_methods);
309
310                 dst->obj_methods = talloc_zero(mem_ctx, struct WbemMethods);
311                 duplicate_WbemMethods(dst->obj_methods, src->obj_methods, dst->obj_methods);
312         }
313         if (src->flags & (WCF_CLASS | WCF_INSTANCE)) {
314                 dst->obj_class = talloc_zero(mem_ctx, struct WbemClass);
315                 duplicate_WbemClass(dst->obj_class, src->obj_class, dst->obj_class);
316         }
317         if (src->flags & WCF_INSTANCE) {
318                 dst->instance = talloc_zero(mem_ctx, struct WbemInstance);
319                 duplicate_WbemInstance(dst->instance, src->instance, dst->instance, src->obj_class);
320         }
321 }
322
323 void duplicate_CIMVAR(TALLOC_CTX *mem_ctx, const union CIMVAR *src, union CIMVAR *dst, enum CIMTYPE_ENUMERATION cimtype)
324 {
325         uint32_t i;
326
327         switch (cimtype & CIM_TYPEMASK) {
328         case CIM_SINT8:
329         case CIM_UINT8:
330         case CIM_SINT16:
331         case CIM_UINT16:
332         case CIM_SINT32:
333         case CIM_UINT32:
334         case CIM_SINT64:
335         case CIM_UINT64:
336         case CIM_REAL32:
337         case CIM_REAL64:
338         case CIM_BOOLEAN:
339                 *dst = *src;
340                 break;
341         case CIM_STRING:
342         case CIM_DATETIME:
343         case CIM_REFERENCE:
344                 dst->v_string = talloc_strdup(mem_ctx, src->v_string);
345                 break;
346         case CIM_OBJECT:
347                 dst->v_object = talloc_zero(mem_ctx, struct WbemClassObject);
348                 duplicate_WbemClassObject(dst->v_object, src->v_object, dst->v_object);
349                 break;
350         case CIM_ARR_SINT8:
351         case CIM_ARR_UINT8:
352                 dst->a_uint8 = talloc_memdup(mem_ctx, src->a_uint8, sizeof(struct arr_uint8));
353                 dst->a_uint8->item = talloc_memdup(dst->a_uint8, src->a_uint8->item, src->a_uint8->count);
354                 break;
355         case CIM_ARR_SINT16:
356         case CIM_ARR_UINT16:
357         case CIM_ARR_BOOLEAN:
358                 dst->a_uint8 = talloc_memdup(mem_ctx, src->a_uint8, sizeof(struct arr_uint8));
359                 dst->a_uint8->item = talloc_memdup(dst->a_uint8, src->a_uint8->item, 2*src->a_uint8->count);
360                 break;
361         case CIM_ARR_SINT32:
362         case CIM_ARR_UINT32:
363         case CIM_ARR_REAL32:
364                 dst->a_uint8 = talloc_memdup(mem_ctx, src->a_uint8, sizeof(struct arr_uint8));
365                 dst->a_uint8->item = talloc_memdup(dst->a_uint8, src->a_uint8->item, 4*src->a_uint8->count);
366                 break;
367         case CIM_ARR_SINT64:
368         case CIM_ARR_UINT64:
369         case CIM_ARR_REAL64:
370                 dst->a_uint8 = talloc_memdup(mem_ctx, src->a_uint8, sizeof(struct arr_uint8));
371                 dst->a_uint8->item = talloc_memdup(dst->a_uint8, src->a_uint8->item, 8*src->a_uint8->count);
372                 break;
373         case CIM_ARR_STRING:
374         case CIM_ARR_DATETIME:
375         case CIM_ARR_REFERENCE:
376                 dst->a_uint8 = talloc_memdup(mem_ctx, src->a_uint8, sizeof(struct arr_uint8));
377                 dst->a_uint8->item = talloc_memdup(dst->a_uint8, src->a_uint8->item, 4*src->a_uint8->count);
378                 for (i = 0; i < src->a_uint8->count; ++i)
379                         dst->a_string->item[i] = talloc_strdup(dst->a_uint8->item, src->a_string->item[i]);
380                 break;
381         default:
382                 DEBUG(0, ("duplicate_CIMVAR: cimtype 0x%04X not supported\n", cimtype & CIM_TYPEMASK));
383                 break;
384         }
385 }
386
387 WERROR WbemClassObject_Get(struct WbemClassObject *d, TALLOC_CTX *mem_ctx, const char *name, uint32_t flags, union CIMVAR *val, enum CIMTYPE_ENUMERATION *cimtype, uint32_t *flavor)
388 {
389         uint32_t i;
390         for (i = 0; i < d->obj_class->__PROPERTY_COUNT; ++i) {
391                 if (!strcmp(d->obj_class->properties[i].property.name, name)) {
392                         duplicate_CIMVAR(mem_ctx, &d->instance->data[i], val, d->obj_class->properties[i].property.desc->cimtype);
393                         if (cimtype != NULL) 
394                                 *cimtype = d->obj_class->properties[i].property.desc->cimtype;
395                         if (flavor != NULL) 
396                                 *flavor = 0; /* FIXME:avg implement flavor */
397                         return WERR_OK;
398                 }
399         }
400         return WERR_NOT_FOUND;
401 }
402
403 WERROR IWbemClassObject_Put(struct IWbemClassObject *d, TALLOC_CTX *mem_ctx, const char *name, uint32_t flags, union CIMVAR *val, enum CIMTYPE_ENUMERATION cimtype)
404 {
405         struct WbemClassObject *wco;
406         uint32_t i;
407
408         wco = (struct WbemClassObject *)d->object_data;
409         for (i = 0; i < wco->obj_class->__PROPERTY_COUNT; ++i) {
410                 if (!strcmp(wco->obj_class->properties[i].property.name, name)) {
411                         if (cimtype && cimtype != wco->obj_class->properties[i].property.desc->cimtype) return WERR_INVALID_PARAM;
412                         wco->instance->default_flags[i] = 0;
413                         duplicate_CIMVAR(wco->instance, val, &wco->instance->data[i], wco->obj_class->properties[i].property.desc->cimtype);
414                         return WERR_OK;
415                 }
416         }
417         return WERR_NOT_FOUND;
418 }
419
420 #define WERR_CHECK(msg) if (!W_ERROR_IS_OK(result)) { \
421                               DEBUG(1, ("ERROR: %s - %s\n", msg, wmi_errstr(result))); \
422         return result; \
423                           } else { \
424                               DEBUG(1, ("OK   : %s\n", msg)); \
425                           }
426
427 struct pair_guid_ptr {
428         struct GUID guid;
429         void *ptr;
430         struct pair_guid_ptr *next, *prev;
431 };
432
433 static void *get_ptr_by_guid(struct pair_guid_ptr *list, struct GUID *uuid)
434 {
435         for (; list; list = list->next) {
436                 if (GUID_equal(&list->guid, uuid))
437                                 return list->ptr;
438         }
439         return NULL;
440 }
441
442 static void add_pair_guid_ptr(TALLOC_CTX *mem_ctx, struct pair_guid_ptr **list, struct GUID *uuid, void *ptr)
443 {
444         struct pair_guid_ptr *e;
445
446         e = talloc(mem_ctx, struct pair_guid_ptr);
447         e->guid = *uuid;
448         e->ptr = ptr;
449         talloc_steal(e, ptr);
450         DLIST_ADD(*list, e);
451 }
452
453 struct IEnumWbemClassObject_data {
454         struct GUID guid;
455         struct IWbemFetchSmartEnum *pFSE;
456         struct IWbemWCOSmartEnum *pSE;
457         struct pair_guid_ptr *cache;
458 };
459 #define NDR_CHECK_EXPR(expr) do { if (!(expr)) {\
460                                         DEBUG(0, ("%s(%d): WBEMDATA_ERR(0x%08X): Error parsing(%s)\n", __FILE__, __LINE__, ndr->offset, #expr)); \
461                                         return NDR_ERR_VALIDATE; \
462                                         } \
463                                     } while(0)
464
465 #define NDR_CHECK_CONST(val, exp) NDR_CHECK_EXPR((val) == (exp))
466
467
468 static enum ndr_err_code WBEMDATA_Parse(TALLOC_CTX *mem_ctx, uint8_t *data, uint32_t size, struct IEnumWbemClassObject *d, uint32_t uCount, struct WbemClassObject **apObjects)
469 {
470         struct ndr_pull *ndr;
471         uint32_t u, i, ofs_next;
472         uint8_t u8, datatype;
473         struct GUID guid;
474         struct IEnumWbemClassObject_data *ecod;
475
476         if (!uCount) 
477                 return NDR_ERR_BAD_SWITCH;
478
479         ecod = d->object_data;
480
481         ndr = talloc_zero(mem_ctx, struct ndr_pull);
482         ndr->current_mem_ctx = d->ctx;
483         ndr->data = data;
484         ndr->data_size = size;
485         ndr_set_flags(&ndr->flags, LIBNDR_FLAG_NOALIGN);
486
487         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
488         NDR_CHECK_CONST(u, 0x0);
489         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
490         NDR_CHECK_CONST(u, *(const uint32_t *)"WBEM");
491         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
492         NDR_CHECK_CONST(u, *(const uint32_t *)"DATA");
493         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
494         NDR_CHECK_CONST(u, 0x1A); /* Length of header */
495         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
496         NDR_PULL_NEED_BYTES(ndr, u + 6);
497         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
498         NDR_CHECK_CONST(u, 0x0);
499         NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &u8));
500         NDR_CHECK_CONST(u8, 0x01); /* Major Version */
501         NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &u8));
502         NDR_CHECK_EXPR(u8 <= 1); /* Minor Version 0 - Win2000, 1 - XP/2003 */
503         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
504         NDR_CHECK_CONST(u, 0x8); /* Length of header */
505         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
506         NDR_PULL_NEED_BYTES(ndr, u);
507         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
508         NDR_CHECK_CONST(u, 0xC); /* Length of header */
509         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
510         NDR_PULL_NEED_BYTES(ndr, u + 4);
511         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
512         NDR_CHECK_CONST(u, uCount);
513         for (i = 0; i < uCount; ++i) {
514                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
515                 NDR_CHECK_CONST(u, 0x9); /* Length of header */
516                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
517                 NDR_PULL_NEED_BYTES(ndr, u + 1);
518                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &datatype));
519                 ofs_next = ndr->offset + u;
520                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
521                 NDR_CHECK_CONST(u, 0x18); /* Length of header */
522                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &u));
523                 NDR_PULL_NEED_BYTES(ndr, u + 16);
524                 NDR_CHECK(ndr_pull_GUID(ndr, NDR_SCALARS, &guid));
525                 switch (datatype) {
526                 case DATATYPE_CLASSOBJECT:
527                         apObjects[i] = talloc_zero(d->ctx, struct WbemClassObject);
528                         ndr->current_mem_ctx = apObjects[i];
529                         NDR_CHECK(ndr_pull_WbemClassObject(ndr, NDR_SCALARS|NDR_BUFFERS, apObjects[i]));
530                         ndr->current_mem_ctx = d->ctx;
531                         add_pair_guid_ptr(ecod, &ecod->cache, &guid, apObjects[i]->obj_class);
532                         break;
533                 case DATATYPE_OBJECT:
534                         apObjects[i] = talloc_zero(d->ctx, struct WbemClassObject);
535                         apObjects[i]->obj_class = get_ptr_by_guid(ecod->cache, &guid);
536                         (void)talloc_reference(apObjects[i], apObjects[i]->obj_class);
537                         ndr->current_mem_ctx = apObjects[i];
538                         NDR_CHECK(ndr_pull_WbemClassObject_Object(ndr, NDR_SCALARS|NDR_BUFFERS, apObjects[i]));
539                         ndr->current_mem_ctx = d->ctx;
540                         break;
541                 default:
542                         DEBUG(0, ("WBEMDATA_Parse: Data type %d not supported\n", datatype));
543                         return NDR_ERR_BAD_SWITCH;
544                 }
545                 ndr->offset = ofs_next;
546                 if (DEBUGLVL(9)) {
547                         NDR_PRINT_DEBUG(WbemClassObject, apObjects[i]);
548                 }
549         }
550         return NDR_ERR_SUCCESS;
551 }
552
553 WERROR IEnumWbemClassObject_SmartNext(struct IEnumWbemClassObject *d, TALLOC_CTX *mem_ctx, int32_t lTimeout, uint32_t uCount, struct WbemClassObject **apObjects, uint32_t *puReturned)
554 {
555         WERROR result;
556         NTSTATUS status;
557         struct IEnumWbemClassObject_data *ecod;
558         TALLOC_CTX *loc_ctx;
559         uint32_t size;
560         uint8_t *data;
561
562         loc_ctx = talloc_new(0);
563         ecod = d->object_data;
564         if (!ecod) {
565                 struct GUID iid;
566                 WERROR coresult;
567
568                 d->object_data = ecod = talloc_zero(d, struct IEnumWbemClassObject_data);
569                 GUID_from_string(COM_IWBEMFETCHSMARTENUM_UUID, &iid);
570                 result = dcom_query_interface((struct IUnknown *)d, 5, 1, &iid, (struct IUnknown **)&ecod->pFSE, &coresult);
571                 WERR_CHECK("dcom_query_interface.");
572                 result = coresult;
573                 WERR_CHECK("Retrieve enumerator of result(IWbemFetchSmartEnum).");
574
575                 result = IWbemFetchSmartEnum_Fetch(ecod->pFSE, mem_ctx, &ecod->pSE);
576                 WERR_CHECK("Retrieve enumerator of result(IWbemWCOSmartEnum).");
577
578                 ecod->guid = GUID_random();
579                 d->vtable->Release_send = dcom_proxy_IEnumWbemClassObject_Release_send;
580         }
581
582         result = IWbemWCOSmartEnum_Next(ecod->pSE, loc_ctx, &ecod->guid, lTimeout, uCount, puReturned, &size, &data);
583         if (!W_ERROR_EQUAL(result, WERR_BADFUNC)) {
584                 WERR_CHECK("IWbemWCOSmartEnum_Next.");
585         }
586
587         if (data) {
588                 NDR_CHECK(WBEMDATA_Parse(mem_ctx, data, size, d, *puReturned, apObjects));
589         }
590         if (!W_ERROR_IS_OK(result)) {
591                 status = werror_to_ntstatus(result);
592                 DEBUG(9, ("dcom_proxy_IEnumWbemClassObject_Next: %s - %s\n", nt_errstr(status), get_friendly_nt_error_msg(status)));
593         }
594         talloc_free(loc_ctx);
595         return result;
596 }
597
598 struct composite_context *dcom_proxy_IEnumWbemClassObject_Release_send(struct IUnknown *d, TALLOC_CTX *mem_ctx)
599 {
600         struct composite_context *c, *cr;
601         struct REMINTERFACEREF iref[3];
602         struct dcom_object_exporter *ox;
603         struct IEnumWbemClassObject_data *ecod;
604         int n;
605
606         c = composite_create(d->ctx, d->ctx->event_ctx);
607         if (c == NULL) return NULL;
608         c->private_data = d;
609
610         ox = object_exporter_by_ip(d->ctx, d);
611         iref[0].ipid = IUnknown_ipid(d);
612         iref[0].cPublicRefs = 5;
613         iref[0].cPrivateRefs = 0;
614         n = 1;
615
616         ecod = d->object_data;
617         if (ecod) {
618                 if (ecod->pFSE) {
619                         talloc_steal(d, ecod->pFSE);
620                         iref[n].ipid = IUnknown_ipid(ecod->pFSE);
621                         iref[n].cPublicRefs = 5;
622                         iref[n].cPrivateRefs = 0;
623                         ++n;
624                 }
625                 if (ecod->pSE) {
626                         talloc_steal(d, ecod->pSE);
627                         iref[n].ipid = IUnknown_ipid(ecod->pSE);
628                         iref[n].cPublicRefs = 5;
629                         iref[n].cPrivateRefs = 0;
630                         ++n;
631                 }
632         }
633         cr = IRemUnknown_RemRelease_send(ox->rem_unknown, mem_ctx, n, iref);
634
635         composite_continue(c, cr, dcom_release_continue, c);
636         return c;
637 }
638
639 NTSTATUS dcom_proxy_IWbemClassObject_init(void)
640 {
641         struct GUID clsid;
642         GUID_from_string("4590f812-1d3a-11d0-891f-00aa004b2e24", &clsid);
643         dcom_register_marshal(&clsid, marshal, unmarshal);
644
645 #if 0
646         struct IEnumWbemClassObject_vtable *proxy_vtable;
647         proxy_vtable = (struct IEnumWbemClassObject_vtable *)dcom_proxy_vtable_by_iid((struct GUID *)&dcerpc_table_IEnumWbemClassObject.syntax_id.uuid);
648         if (proxy_vtable)
649                 proxy_vtable->Release_send = dcom_proxy_IEnumWbemClassObject_Release_send;
650         else
651                 DEBUG(0, ("WARNING: IEnumWbemClassObject should be initialized before IWbemClassObject."));
652 #endif
653
654         return NT_STATUS_OK;
655 }