Improve formatting, use printf rather than DEBUG.
[kai/samba-autobuild/.git] / source4 / lib / wmi / tools / wmis.c
1 /*
2    WMI Sample client
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 "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"
33
34 #include "lib/com/dcom/dcom.h"
35 #include "librpc/gen_ndr/com_wmi.h"
36 #include "librpc/ndr/ndr_table.h"
37
38 struct WBEMCLASS;
39 struct WBEMOBJECT;
40 enum CIMTYPE_ENUMERATION;
41
42 #include "wmi/proto.h"
43
44 struct program_args {
45     char *hostname;
46     char *query;
47 };
48
49 static void parse_args(int argc, char *argv[], struct program_args *pmyargs)
50 {
51     poptContext pc;
52     int opt, i;
53
54     int argc_new;
55     char **argv_new;
56
57     struct poptOption long_options[] = {
58         POPT_AUTOHELP
59         POPT_COMMON_SAMBA
60         POPT_COMMON_CONNECTION
61         POPT_COMMON_CREDENTIALS
62         POPT_COMMON_VERSION
63         POPT_TABLEEND
64     };
65
66     pc = poptGetContext("wmi", argc, (const char **) argv,
67                 long_options, POPT_CONTEXT_KEEP_FIRST);
68
69     poptSetOtherOptionHelp(pc, "//host\n\nExample: wmis -U [domain/]adminuser%password //host");
70
71     while ((opt = poptGetNextOpt(pc)) != -1) {
72         poptPrintUsage(pc, stdout, 0);
73         poptFreeContext(pc);
74         exit(1);
75     }
76
77     argv_new = discard_const_p(char *, poptGetArgs(pc));
78
79     argc_new = argc;
80     for (i = 0; i < argc; i++) {
81         if (argv_new[i] == NULL) {
82             argc_new = i;
83             break;
84         }
85     }
86
87     if (argc_new < 2 || argv_new[1][0] != '/'
88         || argv_new[1][1] != '/') {
89         poptPrintUsage(pc, stdout, 0);
90         poptFreeContext(pc);
91         exit(1);
92     }
93
94     pmyargs->hostname = argv_new[1] + 2;
95     poptFreeContext(pc);
96 }
97
98 #define WERR_CHECK(msg) if (!W_ERROR_IS_OK(result)) { \
99                             DEBUG(0, ("ERROR: %s\n", msg)); \
100                             goto error; \
101                         } else { \
102                             DEBUG(1, ("OK   : %s\n", msg)); \
103                         }
104 /*
105 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)
106 {
107         struct GUID clsid;
108         struct GUID iid;
109         WERROR result, coresult;
110         struct IUnknown **mqi;
111         struct IWbemLevel1Login *pL;
112
113         if (user) {
114                 char *cred;
115                 struct cli_credentials *cc;
116
117                 cred = talloc_asprintf(NULL, "%s%%%s", user, password);
118                 cc = cli_credentials_init(ctx);
119                 cli_credentials_set_conf(cc);
120                 cli_credentials_parse_string(cc, cred, CRED_SPECIFIED);
121                 dcom_set_server_credentials(ctx, server, cc);
122                 talloc_free(cred);
123         }
124
125         GUID_from_string(CLSID_WBEMLEVEL1LOGIN, &clsid);
126         GUID_from_string(COM_IWBEMLEVEL1LOGIN_UUID, &iid);
127         result = dcom_create_object(ctx, &clsid, server, 1, &iid, &mqi, &coresult);
128         WERR_CHECK("dcom_create_object.");
129         result = coresult;
130         WERR_CHECK("Create remote WMI object.");
131         pL = (struct IWbemLevel1Login *)mqi[0];
132         talloc_free(mqi);
133
134         result = IWbemLevel1Login_NTLMLogin(pL, ctx, nspace, locale, flags, wbem_ctx, services);
135         WERR_CHECK("Login to remote object.");
136 error:
137         return result;
138 }
139 */
140 WERROR WBEM_RemoteExecute(struct IWbemServices *pWS, const char *cmdline, uint32_t *ret_code)
141 {
142         struct IWbemClassObject *wco = NULL;
143         struct IWbemClassObject *inc, *outc, *in;
144         struct IWbemClassObject *out = NULL;
145         WERROR result;
146         union CIMVAR v;
147         TALLOC_CTX *ctx;
148
149         ctx = talloc_new(0);
150
151         result = IWbemServices_GetObject(pWS, ctx, "Win32_Process", 
152                                          WBEM_FLAG_RETURN_WBEM_COMPLETE, NULL, &wco, NULL);
153         WERR_CHECK("GetObject.");
154
155         result = IWbemClassObject_GetMethod(wco, ctx, "Create", 0, &inc, &outc);
156         WERR_CHECK("IWbemClassObject_GetMethod.");
157
158         result = IWbemClassObject_SpawnInstance(inc, ctx, 0, &in);
159         WERR_CHECK("IWbemClassObject_SpawnInstance.");
160
161         v.v_string = cmdline;
162         result = IWbemClassObject_Put(in, ctx, "CommandLine", 0, &v, 0);
163         WERR_CHECK("IWbemClassObject_Put(CommandLine).");
164
165         result = IWbemServices_ExecMethod(pWS, ctx, "Win32_Process", "Create", 0, NULL, in, &out, 
166                                           NULL);
167         WERR_CHECK("IWbemServices_ExecMethod.");
168
169         if (ret_code) {
170                 result = WbemClassObject_Get(out->object_data, ctx, "ReturnValue", 0, &v, 0, 0);
171                 WERR_CHECK("IWbemClassObject_Put(CommandLine).");
172                 *ret_code = v.v_uint32;
173         }
174 error:
175         talloc_free(ctx);
176         return result;
177 }
178
179 int main(int argc, char **argv)
180 {
181         struct program_args args = {};
182         struct com_context *ctx = NULL;
183         WERROR result;
184         NTSTATUS status;
185         struct IWbemServices *pWS = NULL;
186         struct IEnumWbemClassObject *pEnum = NULL;
187         uint32_t cnt;
188
189         parse_args(argc, argv, &args);
190
191         dcerpc_init();
192         ndr_table_init();
193
194         dcom_proxy_IUnknown_init();
195         dcom_proxy_IWbemLevel1Login_init();
196         dcom_proxy_IWbemServices_init();
197         dcom_proxy_IEnumWbemClassObject_init();
198         dcom_proxy_IRemUnknown_init();
199         dcom_proxy_IWbemFetchSmartEnum_init();
200         dcom_proxy_IWbemWCOSmartEnum_init();
201         dcom_proxy_IWbemClassObject_init();
202
203         com_init_ctx(&ctx, NULL);
204         dcom_client_init(ctx, cmdline_credentials);
205
206         result = WBEM_ConnectServer(ctx, args.hostname, "root\\cimv2", 0, 0, 0, 0, 0, 0, &pWS);
207         WERR_CHECK("WBEM_ConnectServer.");
208
209         printf("1: Creating directory C:\\wmi_test_dir_tmp using method Win32_Process.Create\n");
210         WBEM_RemoteExecute(pWS, "cmd.exe /C mkdir C:\\wmi_test_dir_tmp", &cnt);
211         WERR_CHECK("WBEM_RemoteExecute.");
212         printf("2: ReturnCode: %d\n", cnt);
213
214         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");
215         result = IWbemServices_ExecNotificationQuery(pWS, ctx, "WQL", "SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=\"C:\\\\\\\\wmi_test_dir_tmp\"'", WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum);
216         WERR_CHECK("WMI query execute.");
217         for (cnt = 0; cnt < 4; ++cnt) {
218                 struct WbemClassObject *co;
219                 uint32_t ret;
220                 result = IEnumWbemClassObject_SmartNext(pEnum, ctx, 0xFFFFFFFF, 1, &co, &ret);
221                 WERR_CHECK("IEnumWbemClassObject_Next.");
222                 printf("%s\n", co->obj_class->__CLASS);
223         }
224
225 error:
226         status = werror_to_ntstatus(result);
227         fprintf(stderr, "NTSTATUS: %s - %s\n", nt_errstr(status), get_friendly_nt_error_msg(status));
228         talloc_free(ctx);
229         return 1;
230 }