2 * QEMU Guest Agent win32 VSS Provider installer
4 * Copyright Hitachi Data Systems Corp. 2013
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 "qemu/osdep.h"
15 #include "vss-common.h"
16 #include "inc/win2003/vscoordint.h"
23 extern HINSTANCE g_hinstDll;
25 const GUID CLSID_COMAdminCatalog = { 0xF618C514, 0xDFB8, 0x11d1,
26 {0xA2, 0xCF, 0x00, 0x80, 0x5F, 0xC7, 0x92, 0x35} };
27 const GUID IID_ICOMAdminCatalog2 = { 0x790C6E0B, 0x9194, 0x4cc9,
28 {0x94, 0x26, 0xA4, 0x8A, 0x63, 0x18, 0x56, 0x96} };
29 const GUID CLSID_WbemLocator = { 0x4590f811, 0x1d3a, 0x11d0,
30 {0x89, 0x1f, 0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24} };
31 const GUID IID_IWbemLocator = { 0xdc12a687, 0x737f, 0x11cf,
32 {0x88, 0x4d, 0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24} };
34 void errmsg(DWORD err, const char *text)
37 * `text' contains function call statement when errmsg is called via chk().
38 * To make error message more readable, we cut off the text after '('.
39 * If text doesn't contains '(', negative precision is given, which is
40 * treated as though it were missing.
42 char *msg = NULL, *nul = strchr(text, '(');
43 int len = nul ? nul - text : -1;
45 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
46 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
47 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
48 (char *)&msg, 0, NULL);
49 fprintf(stderr, "%.*s. (Error: %lx) %s\n", len, text, err, msg);
53 static void errmsg_dialog(DWORD err, const char *text, const char *opt = "")
57 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
58 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
59 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
60 (char *)&msg, 0, NULL);
61 snprintf(buf, sizeof(buf), "%s%s. (Error: %lx) %s", text, opt, err, msg);
62 MessageBox(NULL, buf, "Error from " QGA_PROVIDER_NAME, MB_OK|MB_ICONERROR);
66 #define _chk(hr, status, msg, err_label) \
75 #define chk(status) _chk(hr, status, "Failed to " #status, out)
77 #if !defined(__MINGW64_VERSION_MAJOR) || !defined(__MINGW64_VERSION_MINOR) || \
78 __MINGW64_VERSION_MAJOR * 100 + __MINGW64_VERSION_MINOR < 301
79 void __stdcall _com_issue_error(HRESULT hr)
81 errmsg(hr, "Unexpected error in COM");
86 HRESULT put_Value(ICatalogObject *pObj, LPCWSTR name, T val)
88 return pObj->put_Value(_bstr_t(name), _variant_t(val));
91 /* Lookup Administrators group name from winmgmt */
92 static HRESULT GetAdminName(_bstr_t *name)
95 COMPointer<IWbemLocator> pLoc;
96 COMPointer<IWbemServices> pSvc;
97 COMPointer<IEnumWbemClassObject> pEnum;
98 COMPointer<IWbemClassObject> pWobj;
102 chk(CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
103 IID_IWbemLocator, (LPVOID *)pLoc.replace()));
104 chk(pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, NULL,
105 0, 0, 0, pSvc.replace()));
106 chk(CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
107 NULL, RPC_C_AUTHN_LEVEL_CALL,
108 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE));
109 chk(pSvc->ExecQuery(_bstr_t(L"WQL"),
110 _bstr_t(L"select * from Win32_Account where "
111 "SID='S-1-5-32-544' and localAccount=TRUE"),
112 WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
113 NULL, pEnum.replace()));
116 errmsg(hr, "Failed to query for Administrators");
119 chk(pEnum->Next(WBEM_INFINITE, 1, pWobj.replace(), &returned));
122 errmsg(hr, "No Administrators found");
126 chk(pWobj->Get(_bstr_t(L"Name"), 0, &var, 0, 0));
131 errmsg(hr, "Failed to get name of Administrators");
139 /* Find and iterate QGA VSS provider in COM+ Application Catalog */
140 static HRESULT QGAProviderFind(
141 HRESULT (*found)(ICatalogCollection *, int, void *), void *arg)
144 COMInitializer initializer;
145 COMPointer<IUnknown> pUnknown;
146 COMPointer<ICOMAdminCatalog2> pCatalog;
147 COMPointer<ICatalogCollection> pColl;
148 COMPointer<ICatalogObject> pObj;
152 chk(CoCreateInstance(CLSID_COMAdminCatalog, NULL, CLSCTX_INPROC_SERVER,
153 IID_IUnknown, (void **)pUnknown.replace()));
154 chk(pUnknown->QueryInterface(IID_ICOMAdminCatalog2,
155 (void **)pCatalog.replace()));
156 chk(pCatalog->GetCollection(_bstr_t(L"Applications"),
157 (IDispatch **)pColl.replace()));
158 chk(pColl->Populate());
160 chk(pColl->get_Count(&n));
161 for (i = n - 1; i >= 0; i--) {
162 chk(pColl->get_Item(i, (IDispatch **)pObj.replace()));
163 chk(pObj->get_Value(_bstr_t(L"Name"), &var));
164 if (var == _variant_t(QGA_PROVIDER_LNAME)) {
165 if (FAILED(found(pColl, i, arg))) {
170 chk(pColl->SaveChanges(&n));
176 /* Count QGA VSS provider in COM+ Application Catalog */
177 static HRESULT QGAProviderCount(ICatalogCollection *coll, int i, void *arg)
183 /* Remove QGA VSS provider from COM+ Application Catalog Collection */
184 static HRESULT QGAProviderRemove(ICatalogCollection *coll, int i, void *arg)
188 fprintf(stderr, "Removing COM+ Application: %s\n", QGA_PROVIDER_NAME);
189 chk(coll->Remove(i));
194 /* Unregister this module from COM+ Applications Catalog */
195 STDAPI COMUnregister(void)
199 DllUnregisterServer();
200 chk(QGAProviderFind(QGAProviderRemove, NULL));
205 /* Register this module to COM+ Applications Catalog */
206 STDAPI COMRegister(void)
209 COMInitializer initializer;
210 COMPointer<IUnknown> pUnknown;
211 COMPointer<ICOMAdminCatalog2> pCatalog;
212 COMPointer<ICatalogCollection> pApps, pRoles, pUsersInRole;
213 COMPointer<ICatalogObject> pObj;
217 CHAR dllPath[MAX_PATH], tlbPath[MAX_PATH];
218 bool unregisterOnFailure = false;
222 errmsg(E_FAIL, "Failed to initialize DLL");
226 chk(QGAProviderFind(QGAProviderCount, (void *)&count));
228 errmsg(E_ABORT, "QGA VSS Provider is already installed");
232 chk(CoCreateInstance(CLSID_COMAdminCatalog, NULL, CLSCTX_INPROC_SERVER,
233 IID_IUnknown, (void **)pUnknown.replace()));
234 chk(pUnknown->QueryInterface(IID_ICOMAdminCatalog2,
235 (void **)pCatalog.replace()));
237 /* Install COM+ Component */
239 chk(pCatalog->GetCollection(_bstr_t(L"Applications"),
240 (IDispatch **)pApps.replace()));
241 chk(pApps->Populate());
242 chk(pApps->Add((IDispatch **)&pObj));
243 chk(put_Value(pObj, L"Name", QGA_PROVIDER_LNAME));
244 chk(put_Value(pObj, L"Description", QGA_PROVIDER_LNAME));
245 chk(put_Value(pObj, L"ApplicationAccessChecksEnabled", true));
246 chk(put_Value(pObj, L"Authentication", short(6)));
247 chk(put_Value(pObj, L"AuthenticationCapability", short(2)));
248 chk(put_Value(pObj, L"ImpersonationLevel", short(2)));
249 chk(pApps->SaveChanges(&n));
251 /* The app should be deleted if something fails after SaveChanges */
252 unregisterOnFailure = true;
254 chk(pObj->get_Key(&key));
256 if (!GetModuleFileName(g_hinstDll, dllPath, sizeof(dllPath))) {
257 hr = HRESULT_FROM_WIN32(GetLastError());
258 errmsg(hr, "GetModuleFileName failed");
264 errmsg(hr, "Failed to lookup dll");
267 strcpy(tlbPath, dllPath);
268 strcpy(tlbPath+n-3, "tlb");
269 fprintf(stderr, "Registering " QGA_PROVIDER_NAME ":\n");
270 fprintf(stderr, " %s\n", dllPath);
271 fprintf(stderr, " %s\n", tlbPath);
272 if (!PathFileExists(tlbPath)) {
273 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
274 errmsg(hr, "Failed to lookup tlb");
278 chk(pCatalog->CreateServiceForApplication(
279 _bstr_t(QGA_PROVIDER_LNAME), _bstr_t(QGA_PROVIDER_LNAME),
280 _bstr_t(L"SERVICE_AUTO_START"), _bstr_t(L"SERVICE_ERROR_NORMAL"),
281 _bstr_t(L""), _bstr_t(L".\\localsystem"), _bstr_t(L""), FALSE));
282 chk(pCatalog->InstallComponent(_bstr_t(QGA_PROVIDER_LNAME),
283 _bstr_t(dllPath), _bstr_t(tlbPath),
286 /* Setup roles of the applicaion */
288 chk(pApps->GetCollection(_bstr_t(L"Roles"), key,
289 (IDispatch **)pRoles.replace()));
290 chk(pRoles->Populate());
291 chk(pRoles->Add((IDispatch **)pObj.replace()));
292 chk(put_Value(pObj, L"Name", L"Administrators"));
293 chk(put_Value(pObj, L"Description", L"Administrators group"));
294 chk(pRoles->SaveChanges(&n));
295 chk(pObj->get_Key(&key));
297 /* Setup users in the role */
299 chk(pRoles->GetCollection(_bstr_t(L"UsersInRole"), key,
300 (IDispatch **)pUsersInRole.replace()));
301 chk(pUsersInRole->Populate());
303 chk(pUsersInRole->Add((IDispatch **)pObj.replace()));
304 chk(GetAdminName(&name));
305 chk(put_Value(pObj, L"User", _bstr_t(".\\") + name));
307 chk(pUsersInRole->Add((IDispatch **)pObj.replace()));
308 chk(put_Value(pObj, L"User", L"SYSTEM"));
309 chk(pUsersInRole->SaveChanges(&n));
312 if (unregisterOnFailure && FAILED(hr)) {
320 static BOOL CreateRegistryKey(LPCTSTR key, LPCTSTR value, LPCTSTR data)
326 ret = RegCreateKeyEx(HKEY_CLASSES_ROOT, key, 0, NULL,
327 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
328 if (ret != ERROR_SUCCESS) {
333 size = strlen(data) + 1;
338 ret = RegSetValueEx(hKey, value, 0, REG_SZ, (LPBYTE)data, size);
342 if (ret != ERROR_SUCCESS) {
343 /* As we cannot printf within DllRegisterServer(), show a dialog. */
344 errmsg_dialog(ret, "Cannot add registry", key);
350 /* Register this dll as a VSS provider */
351 STDAPI DllRegisterServer(void)
353 COMInitializer initializer;
354 COMPointer<IVssAdmin> pVssAdmin;
356 char dllPath[MAX_PATH];
360 errmsg_dialog(hr, "Module instance is not available");
364 /* Add this module to registery */
366 sprintf(key, "CLSID\\%s", g_szClsid);
367 if (!CreateRegistryKey(key, NULL, g_szClsid)) {
371 if (!GetModuleFileName(g_hinstDll, dllPath, sizeof(dllPath))) {
372 errmsg_dialog(GetLastError(), "GetModuleFileName failed");
376 sprintf(key, "CLSID\\%s\\InprocServer32", g_szClsid);
377 if (!CreateRegistryKey(key, NULL, dllPath)) {
381 if (!CreateRegistryKey(key, "ThreadingModel", "Apartment")) {
385 sprintf(key, "CLSID\\%s\\ProgID", g_szClsid);
386 if (!CreateRegistryKey(key, NULL, g_szProgid)) {
390 if (!CreateRegistryKey(g_szProgid, NULL, QGA_PROVIDER_NAME)) {
394 sprintf(key, "%s\\CLSID", g_szProgid);
395 if (!CreateRegistryKey(key, NULL, g_szClsid)) {
399 hr = CoCreateInstance(CLSID_VSSCoordinator, NULL, CLSCTX_ALL,
400 IID_IVssAdmin, (void **)pVssAdmin.replace());
402 errmsg_dialog(hr, "CoCreateInstance(VSSCoordinator) failed");
406 hr = pVssAdmin->RegisterProvider(g_gProviderId, CLSID_QGAVSSProvider,
407 const_cast<WCHAR*>(QGA_PROVIDER_LNAME),
409 const_cast<WCHAR*>(QGA_PROVIDER_VERSION),
412 errmsg_dialog(hr, "RegisterProvider failed");
417 DllUnregisterServer();
423 /* Unregister this VSS hardware provider from the system */
424 STDAPI DllUnregisterServer(void)
427 COMInitializer initializer;
428 COMPointer<IVssAdmin> pVssAdmin;
430 HRESULT hr = CoCreateInstance(CLSID_VSSCoordinator,
431 NULL, CLSCTX_ALL, IID_IVssAdmin,
432 (void **)pVssAdmin.replace());
434 hr = pVssAdmin->UnregisterProvider(g_gProviderId);
436 errmsg(hr, "CoCreateInstance(VSSCoordinator) failed");
439 sprintf(key, "CLSID\\%s", g_szClsid);
440 SHDeleteKey(HKEY_CLASSES_ROOT, key);
441 SHDeleteKey(HKEY_CLASSES_ROOT, g_szProgid);
443 return S_OK; /* Uninstall should never fail */
447 /* Support function to convert ASCII string into BSTR (used in _bstr_t) */
450 BSTR WINAPI ConvertStringToBSTR(const char *ascii) {
451 int len = strlen(ascii);
452 BSTR bstr = SysAllocStringLen(NULL, len);
458 if (mbstowcs(bstr, ascii, len) == (size_t)-1) {
459 fprintf(stderr, "Failed to convert string '%s' into BSTR", ascii);