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.
22 #include "lib/cmdline/popt_common.h"
23 #include "auth/credentials/credentials.h"
24 #include "librpc/rpc/dcerpc.h"
25 #include "librpc/gen_ndr/ndr_oxidresolver.h"
26 #include "librpc/gen_ndr/ndr_oxidresolver_c.h"
27 #include "librpc/gen_ndr/dcom.h"
28 #include "librpc/gen_ndr/ndr_dcom.h"
29 #include "librpc/gen_ndr/ndr_dcom_c.h"
30 #include "librpc/gen_ndr/ndr_remact_c.h"
31 #include "librpc/gen_ndr/ndr_epmapper_c.h"
32 #include "librpc/gen_ndr/com_dcom.h"
34 #include "lib/com/dcom/dcom.h"
35 #include "librpc/gen_ndr/com_wmi.h"
36 #include "librpc/ndr/ndr_table.h"
38 #include "lib/wmi/wmi.h"
45 static void parse_args(int argc
, char *argv
[], struct program_args
*pmyargs
)
53 struct poptOption long_options
[] = {
56 POPT_COMMON_CONNECTION
57 POPT_COMMON_CREDENTIALS
62 pc
= poptGetContext("wmi", argc
, (const char **) argv
,
63 long_options
, POPT_CONTEXT_KEEP_FIRST
);
65 poptSetOtherOptionHelp(pc
, "//host\n\nExample: wmis -U [domain/]adminuser%password //host");
67 while ((opt
= poptGetNextOpt(pc
)) != -1) {
68 poptPrintUsage(pc
, stdout
, 0);
73 argv_new
= discard_const_p(char *, poptGetArgs(pc
));
76 for (i
= 0; i
< argc
; i
++) {
77 if (argv_new
[i
] == NULL
) {
83 if (argc_new
< 2 || argv_new
[1][0] != '/'
84 || argv_new
[1][1] != '/') {
85 poptPrintUsage(pc
, stdout
, 0);
90 pmyargs
->hostname
= argv_new
[1] + 2;
94 #define WERR_CHECK(msg) if (!W_ERROR_IS_OK(result)) { \
95 DEBUG(0, ("ERROR: %s\n", msg)); \
98 DEBUG(1, ("OK : %s\n", msg)); \
101 WERROR WBEM_ConnectServer(struct com_context *ctx, const char *server, const char *nspace, const char *user, const char *password, const char *locale, uint32_t flags, const char *authority, struct IWbemContext* wbem_ctx, struct IWbemServices** services)
107 struct IUnknown **mqi;
108 struct IWbemLevel1Login *pL;
112 struct cli_credentials *cc;
114 cred = talloc_asprintf(NULL, "%s%%%s", user, password);
115 cc = cli_credentials_init(ctx);
116 cli_credentials_set_conf(cc);
117 cli_credentials_parse_string(cc, cred, CRED_SPECIFIED);
118 dcom_set_server_credentials(ctx, server, cc);
122 GUID_from_string(CLSID_WBEMLEVEL1LOGIN, &clsid);
123 GUID_from_string(COM_IWBEMLEVEL1LOGIN_UUID, &iid);
124 result = dcom_create_object(ctx, &clsid, server, 1, &iid, &mqi, &coresult);
125 WERR_CHECK("dcom_create_object.");
127 WERR_CHECK("Create remote WMI object.");
128 pL = (struct IWbemLevel1Login *)mqi[0];
131 result = IWbemLevel1Login_NTLMLogin(pL, ctx, nspace, locale, flags, wbem_ctx, services);
132 WERR_CHECK("Login to remote object.");
137 WERROR
WBEM_RemoteExecute(struct IWbemServices
*pWS
, const char *cmdline
, uint32_t *ret_code
)
139 struct IWbemClassObject
*wco
= NULL
;
140 struct IWbemClassObject
*inc
, *outc
, *in
;
141 struct IWbemClassObject
*out
= NULL
;
145 struct BSTR objectPath
, methodName
;
149 objectPath
.data
= "Win32_Process";
151 result
= IWbemServices_GetObject(pWS
, ctx
, objectPath
,
152 WBEM_FLAG_RETURN_WBEM_COMPLETE
, NULL
, &wco
, NULL
);
153 WERR_CHECK("GetObject.");
155 result
= IWbemClassObject_GetMethod(wco
, ctx
, "Create", 0, &inc
, &outc
);
156 WERR_CHECK("IWbemClassObject_GetMethod.");
158 result
= IWbemClassObject_SpawnInstance(inc
, ctx
, 0, &in
);
159 WERR_CHECK("IWbemClassObject_SpawnInstance.");
161 v
.v_string
= cmdline
;
162 result
= IWbemClassObject_Put(in
, ctx
, "CommandLine", 0, &v
, 0);
163 WERR_CHECK("IWbemClassObject_Put(CommandLine).");
165 methodName
.data
= "Create";
166 result
= IWbemServices_ExecMethod(pWS
, ctx
, objectPath
, methodName
, 0, NULL
, in
, &out
,
168 WERR_CHECK("IWbemServices_ExecMethod.");
171 result
= WbemClassObject_Get(out
->object_data
, ctx
, "ReturnValue", 0, &v
, 0, 0);
172 WERR_CHECK("IWbemClassObject_Put(CommandLine).");
173 *ret_code
= v
.v_uint32
;
180 int main(int argc
, char **argv
)
182 struct program_args args
= {};
183 struct com_context
*ctx
= NULL
;
186 struct IWbemServices
*pWS
= NULL
;
187 struct IEnumWbemClassObject
*pEnum
= NULL
;
189 struct BSTR queryLanguage
;
192 parse_args(argc
, argv
, &args
);
194 wmi_init(&ctx
, popt_get_cmdline_credentials());
195 result
= WBEM_ConnectServer(ctx
, args
.hostname
, "root\\cimv2", 0, 0, 0, 0, 0, 0, &pWS
);
196 WERR_CHECK("WBEM_ConnectServer.");
198 printf("1: Creating directory C:\\wmi_test_dir_tmp using method Win32_Process.Create\n");
199 WBEM_RemoteExecute(pWS
, "cmd.exe /C mkdir C:\\wmi_test_dir_tmp", &cnt
);
200 WERR_CHECK("WBEM_RemoteExecute.");
201 printf("2: ReturnCode: %d\n", cnt
);
203 printf("3: Monitoring directory C:\\wmi_test_dir_tmp. Please create/delete files in that directory to see notifications, after 4 events program quits.\n");
204 query
.data
= "SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=\"C:\\\\\\\\wmi_test_dir_tmp\"'";
205 queryLanguage
.data
= "WQL";
206 result
= IWbemServices_ExecNotificationQuery(pWS
, ctx
, queryLanguage
,
207 query
, WBEM_FLAG_RETURN_IMMEDIATELY
| WBEM_FLAG_FORWARD_ONLY
, NULL
, &pEnum
);
208 WERR_CHECK("WMI query execute.");
209 for (cnt
= 0; cnt
< 4; ++cnt
) {
210 struct WbemClassObject
*co
;
212 result
= IEnumWbemClassObject_SmartNext(pEnum
, ctx
, 0xFFFFFFFF, 1, &co
, &ret
);
213 WERR_CHECK("IEnumWbemClassObject_Next.");
214 printf("%s\n", co
->obj_class
->__CLASS
);
218 status
= werror_to_ntstatus(result
);
219 fprintf(stderr
, "NTSTATUS: %s - %s\n", nt_errstr(status
), get_friendly_nt_error_msg(status
));