1 /*
2  * Copyright 2009 Hans Leidekker for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #pragma once
20 
21 #include "wine/debug.h"
22 #include "wine/heap.h"
23 #include "wine/list.h"
24 #include "wine/unicode.h"
25 
26 IClientSecurity client_security DECLSPEC_HIDDEN;
27 struct list *table_list DECLSPEC_HIDDEN;
28 
29 #define SIZEOF(array) (sizeof(array)/sizeof((array)[0]))
30 
31 enum param_direction
32 {
33     PARAM_OUT   = -1,
34     PARAM_INOUT = 0,
35     PARAM_IN    = 1
36 };
37 
38 #define CIM_TYPE_MASK    0x00000fff
39 
40 #define COL_TYPE_MASK    0x0000ffff
41 #define COL_FLAG_DYNAMIC 0x00010000
42 #define COL_FLAG_KEY     0x00020000
43 #define COL_FLAG_METHOD  0x00040000
44 
45 typedef HRESULT (class_method)(IWbemClassObject *, IWbemClassObject *, IWbemClassObject **);
46 
47 enum operator
48 {
49     OP_EQ      = 1,
50     OP_AND     = 2,
51     OP_OR      = 3,
52     OP_GT      = 4,
53     OP_LT      = 5,
54     OP_LE      = 6,
55     OP_GE      = 7,
56     OP_NE      = 8,
57     OP_ISNULL  = 9,
58     OP_NOTNULL = 10,
59     OP_LIKE    = 11,
60     OP_NOT     = 12
61 };
62 
63 struct expr;
64 struct complex_expr
65 {
66     enum operator op;
67     struct expr *left;
68     struct expr *right;
69 };
70 
71 enum expr_type
72 {
73     EXPR_COMPLEX = 1,
74     EXPR_UNARY   = 2,
75     EXPR_PROPVAL = 3,
76     EXPR_SVAL    = 4,
77     EXPR_IVAL    = 5,
78     EXPR_BVAL    = 6
79 };
80 
81 struct expr
82 {
83     enum expr_type type;
84     union
85     {
86         struct complex_expr expr;
87         const struct property *propval;
88         const WCHAR *sval;
89         int ival;
90     } u;
91 };
92 
93 struct column
94 {
95     const WCHAR *name;
96     UINT type;
97     VARTYPE vartype; /* 0 for default mapping */
98 };
99 
100 enum fill_status
101 {
102     FILL_STATUS_FAILED = -1,
103     FILL_STATUS_UNFILTERED,
104     FILL_STATUS_FILTERED
105 };
106 
107 #define TABLE_FLAG_DYNAMIC 0x00000001
108 
109 struct table
110 {
111     const WCHAR *name;
112     UINT num_cols;
113     const struct column *columns;
114     UINT num_rows;
115     UINT num_rows_allocated;
116     BYTE *data;
117     enum fill_status (*fill)(struct table *, const struct expr *cond);
118     UINT flags;
119     struct list entry;
120     LONG refs;
121 };
122 
123 struct property
124 {
125     const WCHAR *name;
126     const WCHAR *class;
127     const struct property *next;
128 };
129 
130 struct array
131 {
132     UINT count;
133     void *ptr;
134 };
135 
136 struct field
137 {
138     UINT type;
139     VARTYPE vartype; /* 0 for default mapping */
140     union
141     {
142         LONGLONG ival;
143         WCHAR *sval;
144         struct array *aval;
145     } u;
146 };
147 
148 struct record
149 {
150     UINT count;
151     struct field *fields;
152     struct table *table;
153 };
154 
155 struct view
156 {
157     const struct property *proplist;
158     struct table *table;
159     const struct expr *cond;
160     UINT *result;
161     UINT  count;
162 };
163 
164 struct query
165 {
166     LONG refs;
167     struct view *view;
168     struct list mem;
169 };
170 
171 struct query *create_query(void) DECLSPEC_HIDDEN;
172 void free_query( struct query * ) DECLSPEC_HIDDEN;
173 struct query *addref_query( struct query * ) DECLSPEC_HIDDEN;
174 void release_query( struct query *query ) DECLSPEC_HIDDEN;
175 HRESULT exec_query( const WCHAR *, IEnumWbemClassObject ** ) DECLSPEC_HIDDEN;
176 HRESULT parse_query( const WCHAR *, struct view **, struct list * ) DECLSPEC_HIDDEN;
177 HRESULT create_view( const struct property *, const WCHAR *, const struct expr *,
178                      struct view ** ) DECLSPEC_HIDDEN;
179 void destroy_view( struct view * ) DECLSPEC_HIDDEN;
180 HRESULT execute_view( struct view * ) DECLSPEC_HIDDEN;
181 void init_table_list( void ) DECLSPEC_HIDDEN;
182 struct table *grab_table( const WCHAR * ) DECLSPEC_HIDDEN;
183 struct table *addref_table( struct table * ) DECLSPEC_HIDDEN;
184 void release_table( struct table * ) DECLSPEC_HIDDEN;
185 struct table *create_table( const WCHAR *, UINT, const struct column *, UINT, UINT, BYTE *,
186                             enum fill_status (*)(struct table *, const struct expr *) ) DECLSPEC_HIDDEN;
187 BOOL add_table( struct table * ) DECLSPEC_HIDDEN;
188 void free_columns( struct column *, UINT ) DECLSPEC_HIDDEN;
189 void free_row_values( const struct table *, UINT ) DECLSPEC_HIDDEN;
190 void clear_table( struct table * ) DECLSPEC_HIDDEN;
191 void free_table( struct table * ) DECLSPEC_HIDDEN;
192 UINT get_type_size( CIMTYPE ) DECLSPEC_HIDDEN;
193 HRESULT eval_cond( const struct table *, UINT, const struct expr *, LONGLONG *, UINT * ) DECLSPEC_HIDDEN;
194 HRESULT get_column_index( const struct table *, const WCHAR *, UINT * ) DECLSPEC_HIDDEN;
195 HRESULT get_value( const struct table *, UINT, UINT, LONGLONG * ) DECLSPEC_HIDDEN;
196 BSTR get_value_bstr( const struct table *, UINT, UINT ) DECLSPEC_HIDDEN;
197 HRESULT set_value( const struct table *, UINT, UINT, LONGLONG, CIMTYPE ) DECLSPEC_HIDDEN;
198 BOOL is_method( const struct table *, UINT ) DECLSPEC_HIDDEN;
199 HRESULT get_method( const struct table *, const WCHAR *, class_method ** ) DECLSPEC_HIDDEN;
200 HRESULT get_propval( const struct view *, UINT, const WCHAR *, VARIANT *,
201                      CIMTYPE *, LONG * ) DECLSPEC_HIDDEN;
202 HRESULT put_propval( const struct view *, UINT, const WCHAR *, VARIANT *, CIMTYPE ) DECLSPEC_HIDDEN;
203 HRESULT to_longlong( VARIANT *, LONGLONG *, CIMTYPE * ) DECLSPEC_HIDDEN;
204 SAFEARRAY *to_safearray( const struct array *, CIMTYPE ) DECLSPEC_HIDDEN;
205 VARTYPE to_vartype( CIMTYPE ) DECLSPEC_HIDDEN;
206 void destroy_array( struct array *, CIMTYPE ) DECLSPEC_HIDDEN;
207 BOOL is_selected_prop( const struct view *, const WCHAR * ) DECLSPEC_HIDDEN;
208 HRESULT get_properties( const struct view *, LONG, SAFEARRAY ** ) DECLSPEC_HIDDEN;
209 HRESULT get_object( const WCHAR *, IWbemClassObject ** ) DECLSPEC_HIDDEN;
210 BSTR get_method_name( const WCHAR *, UINT ) DECLSPEC_HIDDEN;
211 void set_variant( VARTYPE, LONGLONG, void *, VARIANT * ) DECLSPEC_HIDDEN;
212 HRESULT create_signature( const WCHAR *, const WCHAR *, enum param_direction,
213                           IWbemClassObject ** ) DECLSPEC_HIDDEN;
214 
215 HRESULT WbemLocator_create(LPVOID *) DECLSPEC_HIDDEN;
216 HRESULT WbemServices_create(const WCHAR *, LPVOID *) DECLSPEC_HIDDEN;
217 HRESULT create_class_object(const WCHAR *, IEnumWbemClassObject *, UINT,
218                             struct record *, IWbemClassObject **) DECLSPEC_HIDDEN;
219 HRESULT EnumWbemClassObject_create(struct query *, LPVOID *) DECLSPEC_HIDDEN;
220 HRESULT WbemQualifierSet_create(const WCHAR *, const WCHAR *, LPVOID *) DECLSPEC_HIDDEN;
221 
222 HRESULT process_get_owner(IWbemClassObject *, IWbemClassObject *, IWbemClassObject **) DECLSPEC_HIDDEN;
223 HRESULT reg_enum_key(IWbemClassObject *, IWbemClassObject *, IWbemClassObject **) DECLSPEC_HIDDEN;
224 HRESULT reg_enum_values(IWbemClassObject *, IWbemClassObject *, IWbemClassObject **) DECLSPEC_HIDDEN;
225 HRESULT reg_get_stringvalue(IWbemClassObject *, IWbemClassObject *, IWbemClassObject **) DECLSPEC_HIDDEN;
226 HRESULT service_pause_service(IWbemClassObject *, IWbemClassObject *, IWbemClassObject **) DECLSPEC_HIDDEN;
227 HRESULT service_resume_service(IWbemClassObject *, IWbemClassObject *, IWbemClassObject **) DECLSPEC_HIDDEN;
228 HRESULT service_start_service(IWbemClassObject *, IWbemClassObject *, IWbemClassObject **) DECLSPEC_HIDDEN;
229 HRESULT service_stop_service(IWbemClassObject *, IWbemClassObject *, IWbemClassObject **) DECLSPEC_HIDDEN;
230 HRESULT security_get_sd(IWbemClassObject *, IWbemClassObject *, IWbemClassObject **) DECLSPEC_HIDDEN;
231 HRESULT security_set_sd(IWbemClassObject *, IWbemClassObject *, IWbemClassObject **) DECLSPEC_HIDDEN;
232 
233 static inline WCHAR *heap_strdupW( const WCHAR *src )
234 {
235     WCHAR *dst;
236     if (!src) return NULL;
237     if ((dst = heap_alloc( (strlenW( src ) + 1) * sizeof(WCHAR) ))) strcpyW( dst, src );
238     return dst;
239 }
240 
241 static const WCHAR class_processW[] = {'W','i','n','3','2','_','P','r','o','c','e','s','s',0};
242 static const WCHAR class_serviceW[] = {'W','i','n','3','2','_','S','e','r','v','i','c','e',0};
243 static const WCHAR class_stdregprovW[] = {'S','t','d','R','e','g','P','r','o','v',0};
244 static const WCHAR class_systemsecurityW[] = {'_','_','S','y','s','t','e','m','S','e','c','u','r','i','t','y',0};
245 
246 static const WCHAR prop_nameW[] = {'N','a','m','e',0};
247 
248 static const WCHAR method_enumkeyW[] = {'E','n','u','m','K','e','y',0};
249 static const WCHAR method_enumvaluesW[] = {'E','n','u','m','V','a','l','u','e','s',0};
250 static const WCHAR method_getownerW[] = {'G','e','t','O','w','n','e','r',0};
251 static const WCHAR method_getsdW[] = {'G','e','t','S','D',0};
252 static const WCHAR method_getstringvalueW[] = {'G','e','t','S','t','r','i','n','g','V','a','l','u','e',0};
253 static const WCHAR method_pauseserviceW[] = {'P','a','u','s','e','S','e','r','v','i','c','e',0};
254 static const WCHAR method_resumeserviceW[] = {'R','e','s','u','m','e','S','e','r','v','i','c','e',0};
255 static const WCHAR method_setsdW[] = {'S','e','t','S','D',0};
256 static const WCHAR method_startserviceW[] = {'S','t','a','r','t','S','e','r','v','i','c','e',0};
257 static const WCHAR method_stopserviceW[] = {'S','t','o','p','S','e','r','v','i','c','e',0};
258 
259 static const WCHAR param_defkeyW[] = {'h','D','e','f','K','e','y',0};
260 static const WCHAR param_domainW[] = {'D','o','m','a','i','n',0};
261 static const WCHAR param_namesW[] = {'s','N','a','m','e','s',0};
262 static const WCHAR param_returnvalueW[] = {'R','e','t','u','r','n','V','a','l','u','e',0};
263 static const WCHAR param_sdW[] = {'S','D',0};
264 static const WCHAR param_subkeynameW[] = {'s','S','u','b','K','e','y','N','a','m','e',0};
265 static const WCHAR param_typesW[] = {'T','y','p','e','s',0};
266 static const WCHAR param_userW[] = {'U','s','e','r',0};
267 static const WCHAR param_valueW[] = {'s','V','a','l','u','e',0};
268 static const WCHAR param_valuenameW[] = {'s','V','a','l','u','e','N','a','m','e',0};
269