3 Copyright
(C
) 2006 Andrzej Hajda
<andrzej.hajda@wp.pl
>
4 Copyright
(C
) 2008 Jelmer Vernooij
<jelmer@samba.org
>
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.
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.
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.
24 %include
"libcli/util/errors.i"
26 %import
"lib/talloc/talloc.i"
29 void push_object
(PyObject
**stack
, PyObject
*o
)
31 if
((!*stack
) ||
(*stack
== Py_None
)) {
35 if
(!PyTuple_Check
(*stack
)) {
37 *stack
= PyTuple_New
(1);
38 PyTuple_SetItem
(*stack
,0,o2
);
41 PyTuple_SetItem
(o3
,0,o
);
43 *stack
= PySequence_Concat
(o2
,o3
);
52 #include
"librpc/gen_ndr/misc.h"
53 #include
"librpc/rpc/dcerpc.h"
54 #include
"lib/com/dcom/dcom.h"
55 #include
"librpc/gen_ndr/com_dcom.h"
56 #include
"lib/wmi/wmi.h"
59 WERROR WBEM_ConnectServer
(struct com_context
*ctx
, const char
*server
, const char
*nspace
, const char
*user
, const char
*password
,
60 const char
*locale
, uint32_t flags
, const char
*authority
, struct IWbemContext
* wbem_ctx
, struct IWbemServices
** services
);
61 WERROR IEnumWbemClassObject_SmartNext
(struct IEnumWbemClassObject
*d
, TALLOC_CTX
*mem_ctx
, int32_t lTimeout
,uint32_t uCount
,
62 struct WbemClassObject
**apObjects
, uint32_t
*puReturned
);
64 static PyObject
*PyObject_FromCVAR
(uint32_t cimtype
, union CIMVAR
*cvar
);
65 static PyObject
*PySWbemObject_FromWbemClassObject
(struct WbemClassObject
*wco
);
67 static struct com_context
*com_ctx
;
68 static PyObject
*ComError
;
69 static PyObject
*mod_win32_client
;
70 static PyObject
*mod_pywintypes
;
72 typedef struct IUnknown IUnknown
;
73 typedef struct IWbemServices IWbemServices
;
74 typedef struct IWbemClassObject IWbemClassObject
;
75 typedef struct IEnumWbemClassObject IEnumWbemClassObject
;
80 #define RETURN_CVAR_ARRAY
(fmt
, arr
) {\
88 l
= PyList_New
(arr-
>count
);\
90 for
(i
= 0; i
< arr-
>count
; ++i
) {\
91 o
= _Py_BuildValue
(fmt
, arr-
>item
[i
]);\
96 PyList_SET_ITEM
(l
, i
, o
);\
101 static PyObject
*_Py_BuildValue
(char
*str
, ...
)
103 PyObject
* result
= NULL;
106 if
(str
&& *str == 'I') {
107 uint32_t value
= va_arg
(lst
, uint32_t
);
108 if
(value
& 0x80000000) {
109 result
= Py_BuildValue
("L", (long
)value
);
111 result
= Py_BuildValue
("i", value
);
114 result
= Py_VaBuildValue
(str
, lst
);
121 static PyObject
*PyObject_FromCVAR
(uint32_t cimtype
, union CIMVAR
*cvar
)
124 case CIM_SINT8
: return Py_BuildValue
("b", cvar-
>v_sint8
);
125 case CIM_UINT8
: return Py_BuildValue
("B", cvar-
>v_uint8
);
126 case CIM_SINT16
: return Py_BuildValue
("h", cvar-
>v_sint16
);
127 case CIM_UINT16
: return Py_BuildValue
("H", cvar-
>v_uint16
);
128 case CIM_SINT32
: return Py_BuildValue
("i", cvar-
>v_sint32
);
129 case CIM_UINT32
: return _Py_BuildValue
("I", cvar-
>v_uint32
);
130 case CIM_SINT64
: return Py_BuildValue
("L", cvar-
>v_sint64
);
131 case CIM_UINT64
: return Py_BuildValue
("K", cvar-
>v_uint64
);
132 case CIM_REAL32
: return Py_BuildValue
("f", cvar-
>v_real32
);
133 case CIM_REAL64
: return Py_BuildValue
("d", cvar-
>v_real64
);
134 case CIM_BOOLEAN
: return Py_BuildValue
("h", cvar-
>v_boolean
);
135 case CIM_STRING
: return Py_BuildValue
("s", cvar-
>v_string
);
136 case CIM_DATETIME
: return Py_BuildValue
("s", cvar-
>v_datetime
);
137 case CIM_REFERENCE
: return Py_BuildValue
("s", cvar-
>v_reference
);
138 case CIM_OBJECT
: return PySWbemObject_FromWbemClassObject
(cvar-
>v_object
);
139 case CIM_ARR_SINT8
: RETURN_CVAR_ARRAY
("b", cvar-
>a_sint8
);
140 case CIM_ARR_UINT8
: RETURN_CVAR_ARRAY
("B", cvar-
>a_uint8
);
141 case CIM_ARR_SINT16
: RETURN_CVAR_ARRAY
("h", cvar-
>a_sint16
);
142 case CIM_ARR_UINT16
: RETURN_CVAR_ARRAY
("H", cvar-
>a_uint16
);
143 case CIM_ARR_SINT32
: RETURN_CVAR_ARRAY
("i", cvar-
>a_sint32
);
144 case CIM_ARR_UINT32
: RETURN_CVAR_ARRAY
("I", cvar-
>a_uint32
);
145 case CIM_ARR_SINT64
: RETURN_CVAR_ARRAY
("L", cvar-
>a_sint64
);
146 case CIM_ARR_UINT64
: RETURN_CVAR_ARRAY
("K", cvar-
>a_uint64
);
147 case CIM_ARR_REAL32
: RETURN_CVAR_ARRAY
("f", cvar-
>a_real32
);
148 case CIM_ARR_REAL64
: RETURN_CVAR_ARRAY
("d", cvar-
>a_real64
);
149 case CIM_ARR_BOOLEAN
: RETURN_CVAR_ARRAY
("h", cvar-
>a_boolean
);
150 case CIM_ARR_STRING
: RETURN_CVAR_ARRAY
("s", cvar-
>a_string
);
151 case CIM_ARR_DATETIME
: RETURN_CVAR_ARRAY
("s", cvar-
>a_datetime
);
152 case CIM_ARR_REFERENCE
: RETURN_CVAR_ARRAY
("s", cvar-
>a_reference
);
156 str
= talloc_asprintf
(NULL, "Unsupported CIMTYPE(0x%04X)", cimtype
);
157 PyErr_SetString
(PyExc_RuntimeError
, str
);
164 #undef RETURN_CVAR_ARRAY
166 PyObject
*PySWbemObject_InitProperites
(PyObject
*o
, struct WbemClassObject
*wco
)
168 PyObject
*properties
;
175 properties
= PyObject_GetAttrString
(o
, "Properties_");
176 if
(!properties
) return
NULL;
177 addProp
= PyObject_GetAttrString
(properties
, "Add");
179 Py_DECREF
(properties
);
183 for
(i
= 0; i
< wco-
>obj_class-
>__PROPERTY_COUNT
; ++i
) {
184 PyObject
*args
, *property
;
186 args
= Py_BuildValue
("(si)", wco-
>obj_class-
>properties
[i
].property.name
, wco-
>obj_class-
>properties
[i
].property.desc-
>cimtype
& CIM_TYPEMASK);
187 if
(!args
) goto finish
;
188 property
= PyObject_CallObject
(addProp
, args
);
190 if
(!property
) goto finish
;
191 if
(wco-
>flags
& WCF_INSTANCE) {
194 if
(wco-
>instance-
>default_flags
[i
] & 1) {
198 value
= PyObject_FromCVAR
(wco-
>obj_class-
>properties
[i
].property.desc-
>cimtype
& CIM_TYPEMASK, &wco->instance->data[i]);
203 r
= PyObject_SetAttrString
(property
, "Value", value
);
206 PyErr_SetString
(PyExc_RuntimeError
, "Error setting value of property");
217 Py_DECREF
(properties
);
221 static PyObject
*PySWbemObject_FromWbemClassObject
(struct WbemClassObject
*wco
)
223 PyObject
*swo_class
, *swo
, *args
, *result
;
225 swo_class
= PyObject_GetAttrString
(mod_win32_client
, "SWbemObject");
226 if
(!swo_class
) return
NULL;
227 args
= PyTuple_New
(0);
229 Py_DECREF
(swo_class
);
232 swo
= PyObject_CallObject
(swo_class
, args
);
234 Py_DECREF
(swo_class
);
235 if
(!swo
) return
NULL;
237 result
= PySWbemObject_InitProperites
(swo
, wco
);
249 %typemap
(in
, numinputs
=0) struct com_context
*ctx
{
253 %typemap
(in
, numinputs
=0) struct IWbemServices
**services
(struct IWbemServices
*temp
) {
257 %typemap
(argout
) struct IWbemServices
**services
{
259 o
= SWIG_NewPointerObj
(*$
1, SWIGTYPE_p_IWbemServices
, 0);
260 push_object
(&$result, o);
263 WERROR WBEM_ConnectServer
(struct com_context
*ctx
, const char
*server
, const char
*nspace
, const char
*user
, const char
*password
,
264 const char
*locale
, uint32_t flags
, const char
*authority
, struct IWbemContext
* wbem_ctx
, struct IWbemServices
** services
);
266 %typemap
(in
, numinputs
=0) struct IEnumWbemClassObject
**ppEnum
(struct IEnumWbemClassObject
*temp
) {
270 %typemap
(argout
) struct IEnumWbemClassObject
**ppEnum
{
272 o
= SWIG_NewPointerObj
(*$
1, SWIGTYPE_p_IEnumWbemClassObject
, 0);
273 push_object
(&$result, o);
276 typedef struct IUnknown
{
278 uint32_t Release
(TALLOC_CTX
*mem_ctx
);
282 %typemap
(in
) struct BSTR
{
283 $
1.data
= PyString_AsString
($input
);
287 typedef struct IWbemServices
{
289 WERROR ExecQuery
(TALLOC_CTX
*mem_ctx
, struct BSTR strQueryLanguage
, struct BSTR strQuery
, int32_t lFlags
, struct IWbemContext
*pCtx
, struct IEnumWbemClassObject
**ppEnum
);
290 WERROR ExecNotificationQuery
(TALLOC_CTX
*mem_ctx
, struct BSTR strQueryLanguage
, struct BSTR strQuery
, int32_t lFlags
, struct IWbemContext
*pCtx
, struct IEnumWbemClassObject
**ppEnum
);
291 WERROR CreateInstanceEnum
(TALLOC_CTX
*mem_ctx
, struct BSTR strClass
,
292 int32_t lFlags
, struct IWbemContext
*pCtx
, struct IEnumWbemClassObject
**ppEnum
);
296 typedef struct IEnumWbemClassObject
{
298 WERROR Reset
(TALLOC_CTX
*mem_ctx
);
300 } IEnumWbemClassObject
;
302 %typemap
(in
, numinputs
=1) (uint32_t uCount
, struct WbemClassObject
**apObjects
, uint32_t
*puReturned
) (uint32_t uReturned
) {
303 if
(PyLong_Check
($input
))
304 $
1 = PyLong_AsUnsignedLong
($input
);
305 else if
(PyInt_Check
($input
))
306 $
1 = PyInt_AsLong
($input
);
308 PyErr_SetString
(PyExc_TypeError
,"Expected a long or an int");
311 $
2 = talloc_array
(NULL, struct WbemClassObject
*, $
1);
315 %typemap
(argout
) (struct WbemClassObject
**apObjects
, uint32_t
*puReturned
) {
322 $result
= PyTuple_New
(*$
2);
323 for
(i
= 0; i
< *$
2; ++i
) {
325 o
= PySWbemObject_FromWbemClassObject
($
1[i
]);
329 error
= PyTuple_SetItem
($result
, i
, o
);
334 if
(error
) return
NULL;
337 WERROR IEnumWbemClassObject_SmartNext
(struct IEnumWbemClassObject
*d
, TALLOC_CTX
*mem_ctx
, int32_t lTimeout
, uint32_t uCount
,
338 struct WbemClassObject
**apObjects
, uint32_t
*puReturned
);
342 mod_win32_client
= PyImport_ImportModule
("win32com.client");
343 mod_pywintypes
= PyImport_ImportModule
("pywintypes");
344 ComError
= PyObject_GetAttrString
(mod_pywintypes
, "com_error");
346 wmi_init
(&com_ctx, NULL);
350 pModule
= PyImport_ImportModule
( "win32com.client" );