2 * QEMU Guest Agent win32 VSS Provider installer
4 * Copyright Hitachi Data Systems Corp. 2013
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 #define BUFFER_SIZE 1024
25 extern HINSTANCE g_hinstDll;
27 const GUID CLSID_COMAdminCatalog = { 0xF618C514, 0xDFB8, 0x11d1,
28 {0xA2, 0xCF, 0x00, 0x80, 0x5F, 0xC7, 0x92, 0x35} };
29 const GUID IID_ICOMAdminCatalog2 = { 0x790C6E0B, 0x9194, 0x4cc9,
30 {0x94, 0x26, 0xA4, 0x8A, 0x63, 0x18, 0x56, 0x96} };
31 const GUID CLSID_WbemLocator = { 0x4590f811, 0x1d3a, 0x11d0,
32 {0x89, 0x1f, 0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24} };
33 const GUID IID_IWbemLocator = { 0xdc12a687, 0x737f, 0x11cf,
34 {0x88, 0x4d, 0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24} };
36 void errmsg(DWORD err, const char *text)
39 * `text' contains function call statement when errmsg is called via chk().
40 * To make error message more readable, we cut off the text after '('.
41 * If text doesn't contains '(', negative precision is given, which is
42 * treated as though it were missing.
44 char *msg = NULL, *nul = strchr(text, '(');
45 int len = nul ? nul - text : -1;
47 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
48 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
49 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
50 (char *)&msg, 0, NULL);
51 fprintf(stderr, "%.*s. (Error: %lx) %s\n", len, text, err, msg);
55 static void errmsg_dialog(DWORD err, const char *text, const char *opt = "")
59 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
60 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
61 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
62 (char *)&msg, 0, NULL);
63 snprintf(buf, sizeof(buf), "%s%s. (Error: %lx) %s", text, opt, err, msg);
64 MessageBox(NULL, buf, "Error from " QGA_PROVIDER_NAME, MB_OK|MB_ICONERROR);
68 #define _chk(hr, status, msg, err_label) \
77 #define chk(status) _chk(hr, status, "Failed to " #status, out)
79 #if !defined(__MINGW64_VERSION_MAJOR) || !defined(__MINGW64_VERSION_MINOR) || \
80 __MINGW64_VERSION_MAJOR * 100 + __MINGW64_VERSION_MINOR < 301
81 void __stdcall _com_issue_error(HRESULT hr)
83 errmsg(hr, "Unexpected error in COM");
88 HRESULT put_Value(ICatalogObject *pObj, LPCWSTR name, T val)
90 return pObj->put_Value(_bstr_t(name), _variant_t(val));
93 /* Lookup Administrators group name from winmgmt */
94 static HRESULT GetAdminName(_bstr_t *name)
97 COMPointer<IWbemLocator> pLoc;
98 COMPointer<IWbemServices> pSvc;
99 COMPointer<IEnumWbemClassObject> pEnum;
100 COMPointer<IWbemClassObject> pWobj;
104 chk(CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
105 IID_IWbemLocator, (LPVOID *)pLoc.replace()));
106 chk(pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, NULL,
107 0, 0, 0, pSvc.replace()));
108 chk(CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
109 NULL, RPC_C_AUTHN_LEVEL_CALL,
110 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE));
111 chk(pSvc->ExecQuery(_bstr_t(L"WQL"),
112 _bstr_t(L"select * from Win32_Account where "
113 "SID='S-1-5-32-544' and localAccount=TRUE"),
114 WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
115 NULL, pEnum.replace()));
118 errmsg(hr, "Failed to query for Administrators");
121 chk(pEnum->Next(WBEM_INFINITE, 1, pWobj.replace(), &returned));
124 errmsg(hr, "No Administrators found");
128 chk(pWobj->Get(_bstr_t(L"Name"), 0, &var, 0, 0));
133 errmsg(hr, "Failed to get name of Administrators");
141 /* Acquire group or user name by SID */
142 static HRESULT getNameByStringSID(
143 const wchar_t *sid, LPWSTR buffer, LPDWORD bufferLen)
147 SID_NAME_USE groupType;
148 DWORD domainNameLen = BUFFER_SIZE;
149 wchar_t domainName[BUFFER_SIZE];
151 chk(ConvertStringSidToSidW(sid, &psid));
152 LookupAccountSidW(NULL, psid, buffer, bufferLen,
153 domainName, &domainNameLen, &groupType);
154 hr = HRESULT_FROM_WIN32(GetLastError());
162 /* Find and iterate QGA VSS provider in COM+ Application Catalog */
163 static HRESULT QGAProviderFind(
164 HRESULT (*found)(ICatalogCollection *, int, void *), void *arg)
167 COMInitializer initializer;
168 COMPointer<IUnknown> pUnknown;
169 COMPointer<ICOMAdminCatalog2> pCatalog;
170 COMPointer<ICatalogCollection> pColl;
171 COMPointer<ICatalogObject> pObj;
175 chk(CoCreateInstance(CLSID_COMAdminCatalog, NULL, CLSCTX_INPROC_SERVER,
176 IID_IUnknown, (void **)pUnknown.replace()));
177 chk(pUnknown->QueryInterface(IID_ICOMAdminCatalog2,
178 (void **)pCatalog.replace()));
179 chk(pCatalog->GetCollection(_bstr_t(L"Applications"),
180 (IDispatch **)pColl.replace()));
181 chk(pColl->Populate());
183 chk(pColl->get_Count(&n));
184 for (i = n - 1; i >= 0; i--) {
185 chk(pColl->get_Item(i, (IDispatch **)pObj.replace()));
186 chk(pObj->get_Value(_bstr_t(L"Name"), &var));
187 if (var == _variant_t(QGA_PROVIDER_LNAME)) {
188 if (FAILED(found(pColl, i, arg))) {
193 chk(pColl->SaveChanges(&n));
199 /* Count QGA VSS provider in COM+ Application Catalog */
200 static HRESULT QGAProviderCount(ICatalogCollection *coll, int i, void *arg)
206 /* Remove QGA VSS provider from COM+ Application Catalog Collection */
207 static HRESULT QGAProviderRemove(ICatalogCollection *coll, int i, void *arg)
211 fprintf(stderr, "Removing COM+ Application: %s\n", QGA_PROVIDER_NAME);
212 chk(coll->Remove(i));
217 /* Unregister this module from COM+ Applications Catalog */
218 STDAPI COMUnregister(void)
222 DllUnregisterServer();
223 chk(QGAProviderFind(QGAProviderRemove, NULL));
228 /* Register this module to COM+ Applications Catalog */
229 STDAPI COMRegister(void)
232 COMInitializer initializer;
233 COMPointer<IUnknown> pUnknown;
234 COMPointer<ICOMAdminCatalog2> pCatalog;
235 COMPointer<ICatalogCollection> pApps, pRoles, pUsersInRole;
236 COMPointer<ICatalogObject> pObj;
240 CHAR dllPath[MAX_PATH], tlbPath[MAX_PATH];
241 bool unregisterOnFailure = false;
243 DWORD bufferLen = BUFFER_SIZE;
244 wchar_t buffer[BUFFER_SIZE];
245 const wchar_t *administratorsGroupSID = L"S-1-5-32-544";
246 const wchar_t *systemUserSID = L"S-1-5-18";
249 errmsg(E_FAIL, "Failed to initialize DLL");
253 chk(QGAProviderFind(QGAProviderCount, (void *)&count));
255 errmsg(E_ABORT, "QGA VSS Provider is already installed");
259 chk(CoCreateInstance(CLSID_COMAdminCatalog, NULL, CLSCTX_INPROC_SERVER,
260 IID_IUnknown, (void **)pUnknown.replace()));
261 chk(pUnknown->QueryInterface(IID_ICOMAdminCatalog2,
262 (void **)pCatalog.replace()));
264 /* Install COM+ Component */
266 chk(pCatalog->GetCollection(_bstr_t(L"Applications"),
267 (IDispatch **)pApps.replace()));
268 chk(pApps->Populate());
269 chk(pApps->Add((IDispatch **)&pObj));
270 chk(put_Value(pObj, L"Name", QGA_PROVIDER_LNAME));
271 chk(put_Value(pObj, L"Description", QGA_PROVIDER_LNAME));
272 chk(put_Value(pObj, L"ApplicationAccessChecksEnabled", true));
273 chk(put_Value(pObj, L"Authentication", short(6)));
274 chk(put_Value(pObj, L"AuthenticationCapability", short(2)));
275 chk(put_Value(pObj, L"ImpersonationLevel", short(2)));
276 chk(pApps->SaveChanges(&n));
278 /* The app should be deleted if something fails after SaveChanges */
279 unregisterOnFailure = true;
281 chk(pObj->get_Key(&key));
283 if (!GetModuleFileName(g_hinstDll, dllPath, sizeof(dllPath))) {
284 hr = HRESULT_FROM_WIN32(GetLastError());
285 errmsg(hr, "GetModuleFileName failed");
291 errmsg(hr, "Failed to lookup dll");
294 strcpy(tlbPath, dllPath);
295 strcpy(tlbPath+n-3, "tlb");
296 fprintf(stderr, "Registering " QGA_PROVIDER_NAME ":\n");
297 fprintf(stderr, " %s\n", dllPath);
298 fprintf(stderr, " %s\n", tlbPath);
299 if (!PathFileExists(tlbPath)) {
300 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
301 errmsg(hr, "Failed to lookup tlb");
305 chk(pCatalog->CreateServiceForApplication(
306 _bstr_t(QGA_PROVIDER_LNAME), _bstr_t(QGA_PROVIDER_LNAME),
307 _bstr_t(L"SERVICE_DEMAND_START"), _bstr_t(L"SERVICE_ERROR_NORMAL"),
308 _bstr_t(L""), _bstr_t(L".\\localsystem"), _bstr_t(L""), FALSE));
309 chk(pCatalog->InstallComponent(_bstr_t(QGA_PROVIDER_LNAME),
310 _bstr_t(dllPath), _bstr_t(tlbPath),
313 /* Setup roles of the applicaion */
315 chk(getNameByStringSID(administratorsGroupSID, buffer, &bufferLen));
316 chk(pApps->GetCollection(_bstr_t(L"Roles"), key,
317 (IDispatch **)pRoles.replace()));
318 chk(pRoles->Populate());
319 chk(pRoles->Add((IDispatch **)pObj.replace()));
320 chk(put_Value(pObj, L"Name", buffer));
321 chk(put_Value(pObj, L"Description", L"Administrators group"));
322 chk(pRoles->SaveChanges(&n));
323 chk(pObj->get_Key(&key));
325 /* Setup users in the role */
327 chk(pRoles->GetCollection(_bstr_t(L"UsersInRole"), key,
328 (IDispatch **)pUsersInRole.replace()));
329 chk(pUsersInRole->Populate());
331 chk(pUsersInRole->Add((IDispatch **)pObj.replace()));
332 chk(GetAdminName(&name));
333 chk(put_Value(pObj, L"User", _bstr_t(".\\") + name));
335 bufferLen = BUFFER_SIZE;
336 chk(getNameByStringSID(systemUserSID, buffer, &bufferLen));
337 chk(pUsersInRole->Add((IDispatch **)pObj.replace()));
338 chk(put_Value(pObj, L"User", buffer));
339 chk(pUsersInRole->SaveChanges(&n));
342 if (unregisterOnFailure && FAILED(hr)) {
350 static BOOL CreateRegistryKey(LPCTSTR key, LPCTSTR value, LPCTSTR data)
356 ret = RegCreateKeyEx(HKEY_CLASSES_ROOT, key, 0, NULL,
357 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
358 if (ret != ERROR_SUCCESS) {
363 size = strlen(data) + 1;
368 ret = RegSetValueEx(hKey, value, 0, REG_SZ, (LPBYTE)data, size);
372 if (ret != ERROR_SUCCESS) {
373 /* As we cannot printf within DllRegisterServer(), show a dialog. */
374 errmsg_dialog(ret, "Cannot add registry", key);
380 /* Register this dll as a VSS provider */
381 STDAPI DllRegisterServer(void)
383 COMInitializer initializer;
384 COMPointer<IVssAdmin> pVssAdmin;
386 char dllPath[MAX_PATH];
390 errmsg_dialog(hr, "Module instance is not available");
394 /* Add this module to registery */
396 sprintf(key, "CLSID\\%s", g_szClsid);
397 if (!CreateRegistryKey(key, NULL, g_szClsid)) {
401 if (!GetModuleFileName(g_hinstDll, dllPath, sizeof(dllPath))) {
402 errmsg_dialog(GetLastError(), "GetModuleFileName failed");
406 sprintf(key, "CLSID\\%s\\InprocServer32", g_szClsid);
407 if (!CreateRegistryKey(key, NULL, dllPath)) {
411 if (!CreateRegistryKey(key, "ThreadingModel", "Apartment")) {
415 sprintf(key, "CLSID\\%s\\ProgID", g_szClsid);
416 if (!CreateRegistryKey(key, NULL, g_szProgid)) {
420 if (!CreateRegistryKey(g_szProgid, NULL, QGA_PROVIDER_NAME)) {
424 sprintf(key, "%s\\CLSID", g_szProgid);
425 if (!CreateRegistryKey(key, NULL, g_szClsid)) {
429 hr = CoCreateInstance(CLSID_VSSCoordinator, NULL, CLSCTX_ALL,
430 IID_IVssAdmin, (void **)pVssAdmin.replace());
432 errmsg_dialog(hr, "CoCreateInstance(VSSCoordinator) failed");
436 hr = pVssAdmin->RegisterProvider(g_gProviderId, CLSID_QGAVSSProvider,
437 const_cast<WCHAR*>(QGA_PROVIDER_LNAME),
439 const_cast<WCHAR*>(QGA_PROVIDER_VERSION),
442 errmsg_dialog(hr, "RegisterProvider failed");
447 DllUnregisterServer();
453 /* Unregister this VSS hardware provider from the system */
454 STDAPI DllUnregisterServer(void)
457 COMInitializer initializer;
458 COMPointer<IVssAdmin> pVssAdmin;
460 HRESULT hr = CoCreateInstance(CLSID_VSSCoordinator,
461 NULL, CLSCTX_ALL, IID_IVssAdmin,
462 (void **)pVssAdmin.replace());
464 hr = pVssAdmin->UnregisterProvider(g_gProviderId);
466 errmsg(hr, "CoCreateInstance(VSSCoordinator) failed");
469 sprintf(key, "CLSID\\%s", g_szClsid);
470 SHDeleteKey(HKEY_CLASSES_ROOT, key);
471 SHDeleteKey(HKEY_CLASSES_ROOT, g_szProgid);
473 return S_OK; /* Uninstall should never fail */
477 /* Support function to convert ASCII string into BSTR (used in _bstr_t) */
480 BSTR WINAPI ConvertStringToBSTR(const char *ascii) {
481 int len = strlen(ascii);
482 BSTR bstr = SysAllocStringLen(NULL, len);
488 if (mbstowcs(bstr, ascii, len) == (size_t)-1) {
489 fprintf(stderr, "Failed to convert string '%s' into BSTR", ascii);
496 /* Stop QGA VSS provider service from COM+ Application Admin Catalog */
498 STDAPI StopService(void)
501 COMInitializer initializer;
502 COMPointer<IUnknown> pUnknown;
503 COMPointer<ICOMAdminCatalog2> pCatalog;
507 chk(QGAProviderFind(QGAProviderCount, (void *)&count));
509 chk(CoCreateInstance(CLSID_COMAdminCatalog, NULL, CLSCTX_INPROC_SERVER,
510 IID_IUnknown, (void **)pUnknown.replace()));
511 chk(pUnknown->QueryInterface(IID_ICOMAdminCatalog2,
512 (void **)pCatalog.replace()));
513 chk(pCatalog->ShutdownApplication(_bstr_t(QGA_PROVIDER_LNAME)));