target-arm: Breakout integratorcp and versatilepb cpu init
[qemu/ar7.git] / qga / vss-win32 / install.cpp
blobb0e4426c72dd22ed3b89b8d62f36b42caa9c8365
1 /*
2 * QEMU Guest Agent win32 VSS Provider installer
4 * Copyright Hitachi Data Systems Corp. 2013
6 * Authors:
7 * Tomoki Sekiyama <tomoki.sekiyama@hds.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include <stdio.h>
14 #include <string.h>
16 #include "vss-common.h"
17 #include "inc/win2003/vscoordint.h"
19 #include <comadmin.h>
20 #include <wbemidl.h>
21 #include <comdef.h>
22 #include <comutil.h>
24 extern HINSTANCE g_hinstDll;
26 const GUID CLSID_COMAdminCatalog = { 0xF618C514, 0xDFB8, 0x11d1,
27 {0xA2, 0xCF, 0x00, 0x80, 0x5F, 0xC7, 0x92, 0x35} };
28 const GUID IID_ICOMAdminCatalog2 = { 0x790C6E0B, 0x9194, 0x4cc9,
29 {0x94, 0x26, 0xA4, 0x8A, 0x63, 0x18, 0x56, 0x96} };
30 const GUID CLSID_WbemLocator = { 0x4590f811, 0x1d3a, 0x11d0,
31 {0x89, 0x1f, 0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24} };
32 const GUID IID_IWbemLocator = { 0xdc12a687, 0x737f, 0x11cf,
33 {0x88, 0x4d, 0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24} };
35 void errmsg(DWORD err, const char *text)
38 * `text' contains function call statement when errmsg is called via chk().
39 * To make error message more readable, we cut off the text after '('.
40 * If text doesn't contains '(', negative precision is given, which is
41 * treated as though it were missing.
43 char *msg = NULL, *nul = strchr(text, '(');
44 int len = nul ? nul - text : -1;
46 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
47 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
48 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
49 (char *)&msg, 0, NULL);
50 fprintf(stderr, "%.*s. (Error: %lx) %s\n", len, text, err, msg);
51 LocalFree(msg);
54 static void errmsg_dialog(DWORD err, const char *text, const char *opt = "")
56 char *msg, buf[512];
58 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
59 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
60 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
61 (char *)&msg, 0, NULL);
62 snprintf(buf, sizeof(buf), "%s%s. (Error: %lx) %s", text, opt, err, msg);
63 MessageBox(NULL, buf, "Error from " QGA_PROVIDER_NAME, MB_OK|MB_ICONERROR);
64 LocalFree(msg);
67 #define _chk(hr, status, msg, err_label) \
68 do { \
69 hr = (status); \
70 if (FAILED(hr)) { \
71 errmsg(hr, msg); \
72 goto err_label; \
73 } \
74 } while (0)
76 #define chk(status) _chk(hr, status, "Failed to " #status, out)
78 #if !defined(__MINGW64_VERSION_MAJOR) || !defined(__MINGW64_VERSION_MINOR) || \
79 __MINGW64_VERSION_MAJOR * 100 + __MINGW64_VERSION_MINOR < 301
80 void __stdcall _com_issue_error(HRESULT hr)
82 errmsg(hr, "Unexpected error in COM");
84 #endif
86 template<class T>
87 HRESULT put_Value(ICatalogObject *pObj, LPCWSTR name, T val)
89 return pObj->put_Value(_bstr_t(name), _variant_t(val));
92 /* Lookup Administrators group name from winmgmt */
93 static HRESULT GetAdminName(_bstr_t *name)
95 HRESULT hr;
96 COMPointer<IWbemLocator> pLoc;
97 COMPointer<IWbemServices> pSvc;
98 COMPointer<IEnumWbemClassObject> pEnum;
99 COMPointer<IWbemClassObject> pWobj;
100 ULONG returned;
101 _variant_t var;
103 chk(CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
104 IID_IWbemLocator, (LPVOID *)pLoc.replace()));
105 chk(pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, NULL,
106 0, 0, 0, pSvc.replace()));
107 chk(CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
108 NULL, RPC_C_AUTHN_LEVEL_CALL,
109 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE));
110 chk(pSvc->ExecQuery(_bstr_t(L"WQL"),
111 _bstr_t(L"select * from Win32_Account where "
112 "SID='S-1-5-32-544' and localAccount=TRUE"),
113 WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
114 NULL, pEnum.replace()));
115 if (!pEnum) {
116 hr = E_FAIL;
117 errmsg(hr, "Failed to query for Administrators");
118 goto out;
120 chk(pEnum->Next(WBEM_INFINITE, 1, pWobj.replace(), &returned));
121 if (returned == 0) {
122 hr = E_FAIL;
123 errmsg(hr, "No Administrators found");
124 goto out;
127 chk(pWobj->Get(_bstr_t(L"Name"), 0, &var, 0, 0));
128 try {
129 *name = var;
130 } catch(...) {
131 hr = E_FAIL;
132 errmsg(hr, "Failed to get name of Administrators");
133 goto out;
136 out:
137 return hr;
140 /* Find and iterate QGA VSS provider in COM+ Application Catalog */
141 static HRESULT QGAProviderFind(
142 HRESULT (*found)(ICatalogCollection *, int, void *), void *arg)
144 HRESULT hr;
145 COMInitializer initializer;
146 COMPointer<IUnknown> pUnknown;
147 COMPointer<ICOMAdminCatalog2> pCatalog;
148 COMPointer<ICatalogCollection> pColl;
149 COMPointer<ICatalogObject> pObj;
150 _variant_t var;
151 long i, n;
153 chk(CoCreateInstance(CLSID_COMAdminCatalog, NULL, CLSCTX_INPROC_SERVER,
154 IID_IUnknown, (void **)pUnknown.replace()));
155 chk(pUnknown->QueryInterface(IID_ICOMAdminCatalog2,
156 (void **)pCatalog.replace()));
157 chk(pCatalog->GetCollection(_bstr_t(L"Applications"),
158 (IDispatch **)pColl.replace()));
159 chk(pColl->Populate());
161 chk(pColl->get_Count(&n));
162 for (i = n - 1; i >= 0; i--) {
163 chk(pColl->get_Item(i, (IDispatch **)pObj.replace()));
164 chk(pObj->get_Value(_bstr_t(L"Name"), &var));
165 if (var == _variant_t(QGA_PROVIDER_LNAME)) {
166 if (FAILED(found(pColl, i, arg))) {
167 goto out;
171 chk(pColl->SaveChanges(&n));
173 out:
174 return hr;
177 /* Count QGA VSS provider in COM+ Application Catalog */
178 static HRESULT QGAProviderCount(ICatalogCollection *coll, int i, void *arg)
180 (*(int *)arg)++;
181 return S_OK;
184 /* Remove QGA VSS provider from COM+ Application Catalog Collection */
185 static HRESULT QGAProviderRemove(ICatalogCollection *coll, int i, void *arg)
187 HRESULT hr;
189 fprintf(stderr, "Removing COM+ Application: %s\n", QGA_PROVIDER_NAME);
190 chk(coll->Remove(i));
191 out:
192 return hr;
195 /* Unregister this module from COM+ Applications Catalog */
196 STDAPI COMUnregister(void)
198 HRESULT hr;
200 DllUnregisterServer();
201 chk(QGAProviderFind(QGAProviderRemove, NULL));
202 out:
203 return hr;
206 /* Register this module to COM+ Applications Catalog */
207 STDAPI COMRegister(void)
209 HRESULT hr;
210 COMInitializer initializer;
211 COMPointer<IUnknown> pUnknown;
212 COMPointer<ICOMAdminCatalog2> pCatalog;
213 COMPointer<ICatalogCollection> pApps, pRoles, pUsersInRole;
214 COMPointer<ICatalogObject> pObj;
215 long n;
216 _bstr_t name;
217 _variant_t key;
218 CHAR dllPath[MAX_PATH], tlbPath[MAX_PATH];
219 bool unregisterOnFailure = false;
220 int count = 0;
222 if (!g_hinstDll) {
223 errmsg(E_FAIL, "Failed to initialize DLL");
224 return E_FAIL;
227 chk(QGAProviderFind(QGAProviderCount, (void *)&count));
228 if (count) {
229 errmsg(E_ABORT, "QGA VSS Provider is already installed");
230 return E_ABORT;
233 chk(CoCreateInstance(CLSID_COMAdminCatalog, NULL, CLSCTX_INPROC_SERVER,
234 IID_IUnknown, (void **)pUnknown.replace()));
235 chk(pUnknown->QueryInterface(IID_ICOMAdminCatalog2,
236 (void **)pCatalog.replace()));
238 /* Install COM+ Component */
240 chk(pCatalog->GetCollection(_bstr_t(L"Applications"),
241 (IDispatch **)pApps.replace()));
242 chk(pApps->Populate());
243 chk(pApps->Add((IDispatch **)&pObj));
244 chk(put_Value(pObj, L"Name", QGA_PROVIDER_LNAME));
245 chk(put_Value(pObj, L"Description", QGA_PROVIDER_LNAME));
246 chk(put_Value(pObj, L"ApplicationAccessChecksEnabled", true));
247 chk(put_Value(pObj, L"Authentication", short(6)));
248 chk(put_Value(pObj, L"AuthenticationCapability", short(2)));
249 chk(put_Value(pObj, L"ImpersonationLevel", short(2)));
250 chk(pApps->SaveChanges(&n));
252 /* The app should be deleted if something fails after SaveChanges */
253 unregisterOnFailure = true;
255 chk(pObj->get_Key(&key));
257 if (!GetModuleFileName(g_hinstDll, dllPath, sizeof(dllPath))) {
258 hr = HRESULT_FROM_WIN32(GetLastError());
259 errmsg(hr, "GetModuleFileName failed");
260 goto out;
262 n = strlen(dllPath);
263 if (n < 3) {
264 hr = E_FAIL;
265 errmsg(hr, "Failed to lookup dll");
266 goto out;
268 strcpy(tlbPath, dllPath);
269 strcpy(tlbPath+n-3, "tlb");
270 fprintf(stderr, "Registering " QGA_PROVIDER_NAME ":\n");
271 fprintf(stderr, " %s\n", dllPath);
272 fprintf(stderr, " %s\n", tlbPath);
273 if (!PathFileExists(tlbPath)) {
274 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
275 errmsg(hr, "Failed to lookup tlb");
276 goto out;
279 chk(pCatalog->CreateServiceForApplication(
280 _bstr_t(QGA_PROVIDER_LNAME), _bstr_t(QGA_PROVIDER_LNAME),
281 _bstr_t(L"SERVICE_AUTO_START"), _bstr_t(L"SERVICE_ERROR_NORMAL"),
282 _bstr_t(L""), _bstr_t(L".\\localsystem"), _bstr_t(L""), FALSE));
283 chk(pCatalog->InstallComponent(_bstr_t(QGA_PROVIDER_LNAME),
284 _bstr_t(dllPath), _bstr_t(tlbPath),
285 _bstr_t("")));
287 /* Setup roles of the applicaion */
289 chk(pApps->GetCollection(_bstr_t(L"Roles"), key,
290 (IDispatch **)pRoles.replace()));
291 chk(pRoles->Populate());
292 chk(pRoles->Add((IDispatch **)pObj.replace()));
293 chk(put_Value(pObj, L"Name", L"Administrators"));
294 chk(put_Value(pObj, L"Description", L"Administrators group"));
295 chk(pRoles->SaveChanges(&n));
296 chk(pObj->get_Key(&key));
298 /* Setup users in the role */
300 chk(pRoles->GetCollection(_bstr_t(L"UsersInRole"), key,
301 (IDispatch **)pUsersInRole.replace()));
302 chk(pUsersInRole->Populate());
304 chk(pUsersInRole->Add((IDispatch **)pObj.replace()));
305 chk(GetAdminName(&name));
306 chk(put_Value(pObj, L"User", _bstr_t(".\\") + name));
308 chk(pUsersInRole->Add((IDispatch **)pObj.replace()));
309 chk(put_Value(pObj, L"User", L"SYSTEM"));
310 chk(pUsersInRole->SaveChanges(&n));
312 out:
313 if (unregisterOnFailure && FAILED(hr)) {
314 COMUnregister();
317 return hr;
321 static BOOL CreateRegistryKey(LPCTSTR key, LPCTSTR value, LPCTSTR data)
323 HKEY hKey;
324 LONG ret;
325 DWORD size;
327 ret = RegCreateKeyEx(HKEY_CLASSES_ROOT, key, 0, NULL,
328 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
329 if (ret != ERROR_SUCCESS) {
330 goto out;
333 if (data != NULL) {
334 size = strlen(data) + 1;
335 } else {
336 size = 0;
339 ret = RegSetValueEx(hKey, value, 0, REG_SZ, (LPBYTE)data, size);
340 RegCloseKey(hKey);
342 out:
343 if (ret != ERROR_SUCCESS) {
344 /* As we cannot printf within DllRegisterServer(), show a dialog. */
345 errmsg_dialog(ret, "Cannot add registry", key);
346 return FALSE;
348 return TRUE;
351 /* Register this dll as a VSS provider */
352 STDAPI DllRegisterServer(void)
354 COMInitializer initializer;
355 COMPointer<IVssAdmin> pVssAdmin;
356 HRESULT hr = E_FAIL;
357 char dllPath[MAX_PATH];
358 char key[256];
360 if (!g_hinstDll) {
361 errmsg_dialog(hr, "Module instance is not available");
362 goto out;
365 /* Add this module to registery */
367 sprintf(key, "CLSID\\%s", g_szClsid);
368 if (!CreateRegistryKey(key, NULL, g_szClsid)) {
369 goto out;
372 if (!GetModuleFileName(g_hinstDll, dllPath, sizeof(dllPath))) {
373 errmsg_dialog(GetLastError(), "GetModuleFileName failed");
374 goto out;
377 sprintf(key, "CLSID\\%s\\InprocServer32", g_szClsid);
378 if (!CreateRegistryKey(key, NULL, dllPath)) {
379 goto out;
382 if (!CreateRegistryKey(key, "ThreadingModel", "Apartment")) {
383 goto out;
386 sprintf(key, "CLSID\\%s\\ProgID", g_szClsid);
387 if (!CreateRegistryKey(key, NULL, g_szProgid)) {
388 goto out;
391 if (!CreateRegistryKey(g_szProgid, NULL, QGA_PROVIDER_NAME)) {
392 goto out;
395 sprintf(key, "%s\\CLSID", g_szProgid);
396 if (!CreateRegistryKey(key, NULL, g_szClsid)) {
397 goto out;
400 hr = CoCreateInstance(CLSID_VSSCoordinator, NULL, CLSCTX_ALL,
401 IID_IVssAdmin, (void **)pVssAdmin.replace());
402 if (FAILED(hr)) {
403 errmsg_dialog(hr, "CoCreateInstance(VSSCoordinator) failed");
404 goto out;
407 hr = pVssAdmin->RegisterProvider(g_gProviderId, CLSID_QGAVSSProvider,
408 const_cast<WCHAR*>(QGA_PROVIDER_LNAME),
409 VSS_PROV_SOFTWARE,
410 const_cast<WCHAR*>(QGA_PROVIDER_VERSION),
411 g_gProviderVersion);
412 if (FAILED(hr)) {
413 errmsg_dialog(hr, "RegisterProvider failed");
416 out:
417 if (FAILED(hr)) {
418 DllUnregisterServer();
421 return hr;
424 /* Unregister this VSS hardware provider from the system */
425 STDAPI DllUnregisterServer(void)
427 TCHAR key[256];
428 COMInitializer initializer;
429 COMPointer<IVssAdmin> pVssAdmin;
431 HRESULT hr = CoCreateInstance(CLSID_VSSCoordinator,
432 NULL, CLSCTX_ALL, IID_IVssAdmin,
433 (void **)pVssAdmin.replace());
434 if (SUCCEEDED(hr)) {
435 hr = pVssAdmin->UnregisterProvider(g_gProviderId);
436 } else {
437 errmsg(hr, "CoCreateInstance(VSSCoordinator) failed");
440 sprintf(key, "CLSID\\%s", g_szClsid);
441 SHDeleteKey(HKEY_CLASSES_ROOT, key);
442 SHDeleteKey(HKEY_CLASSES_ROOT, g_szProgid);
444 return S_OK; /* Uninstall should never fail */
448 /* Support function to convert ASCII string into BSTR (used in _bstr_t) */
449 namespace _com_util
451 BSTR WINAPI ConvertStringToBSTR(const char *ascii) {
452 int len = strlen(ascii);
453 BSTR bstr = SysAllocStringLen(NULL, len);
455 if (!bstr) {
456 return NULL;
459 if (mbstowcs(bstr, ascii, len) == (size_t)-1) {
460 fprintf(stderr, "Failed to convert string '%s' into BSTR", ascii);
461 bstr[0] = 0;
463 return bstr;