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