diff options
| author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2014-03-26 19:21:20 +0000 |
|---|---|---|
| committer | <> | 2014-05-08 15:03:54 +0000 |
| commit | fb123f93f9f5ce42c8e5785d2f8e0edaf951740e (patch) | |
| tree | c2103d76aec5f1f10892cd1d3a38e24f665ae5db /src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredentialProvider.cpp | |
| parent | 58ed4748338f9466599adfc8a9171280ed99e23f (diff) | |
| download | VirtualBox-master.tar.gz | |
Imported from /home/lorry/working-area/delta_VirtualBox/VirtualBox-4.3.10.tar.bz2.HEADVirtualBox-4.3.10master
Diffstat (limited to 'src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredentialProvider.cpp')
| -rw-r--r-- | src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredentialProvider.cpp | 216 |
1 files changed, 142 insertions, 74 deletions
diff --git a/src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredentialProvider.cpp b/src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredentialProvider.cpp index 97154d66..8d841301 100644 --- a/src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredentialProvider.cpp +++ b/src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredentialProvider.cpp @@ -4,7 +4,7 @@ */ /* - * Copyright (C) 2012 Oracle Corporation + * Copyright (C) 2012-2014 Oracle Corporation * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; @@ -20,6 +20,7 @@ *******************************************************************************/ #include <windows.h> #include <initguid.h> +#include <new> /* For bad_alloc. */ #ifdef VBOX_WITH_SENS # include <eventsys.h> @@ -40,11 +41,12 @@ /******************************************************************************* * Global Variables * *******************************************************************************/ -static LONG g_cDllRefs = 0; /**< Global DLL reference count. */ -static HINSTANCE g_hDllInst = NULL; /**< Global DLL hInstance. */ +static LONG g_cDllRefs = 0; /**< Global DLL reference count. */ +static HINSTANCE g_hDllInst = NULL; /**< Global DLL hInstance. */ #ifdef VBOX_WITH_SENS -static IEventSystem *g_pIEventSystem; /**< Pointer to IEventSystem interface. */ +static bool g_fSENSEnabled = false; +static IEventSystem *g_pIEventSystem = NULL; /**< Pointer to IEventSystem interface. */ /** * Subscribed SENS events. @@ -179,7 +181,7 @@ protected: LONG m_cRefs; }; -static VBoxCredProvSensLogon *g_pISensLogon; +static VBoxCredProvSensLogon *g_pISensLogon = NULL; /** @@ -199,85 +201,104 @@ static HRESULT VBoxCredentialProviderRegisterSENS(void) return hr; } - g_pISensLogon = new VBoxCredProvSensLogon(); - if (!g_pISensLogon) + try { - VBoxCredProvVerbose(0, "VBoxCredentialProviderRegisterSENS: Could not create interface instance; out of memory\n"); - return ERROR_OUTOFMEMORY; + g_pISensLogon = new VBoxCredProvSensLogon(); + AssertPtr(g_pISensLogon); + } + catch (std::bad_alloc &ex) + { + NOREF(ex); + hr = E_OUTOFMEMORY; } - IEventSubscription *pIEventSubscription; - int i; - for (i = 0; i < RT_ELEMENTS(g_aSENSEvents); i++) + if ( SUCCEEDED(hr) + && g_pIEventSystem) { - VBoxCredProvVerbose(0, "VBoxCredProv: Registering \"%s\" (%s) ...\n", - g_aSENSEvents[i].pszMethod, g_aSENSEvents[i].pszSubscriptionName); + IEventSubscription *pIEventSubscription; + int i; + for (i = 0; i < RT_ELEMENTS(g_aSENSEvents); i++) + { + VBoxCredProvVerbose(0, "VBoxCredProv: Registering \"%s\" (%s) ...\n", + g_aSENSEvents[i].pszMethod, g_aSENSEvents[i].pszSubscriptionName); - hr = CoCreateInstance(CLSID_CEventSubscription, 0, CLSCTX_SERVER, IID_IEventSubscription, (LPVOID*)&pIEventSubscription); - if (FAILED(hr)) - continue; + hr = CoCreateInstance(CLSID_CEventSubscription, 0, CLSCTX_SERVER, IID_IEventSubscription, (LPVOID*)&pIEventSubscription); + if (FAILED(hr)) + continue; - hr = pIEventSubscription->put_EventClassID(L"{d5978630-5b9f-11d1-8dd2-00aa004abd5e}" /* SENSGUID_EVENTCLASS_LOGON */); - if (FAILED(hr)) - break; + hr = pIEventSubscription->put_EventClassID(L"{d5978630-5b9f-11d1-8dd2-00aa004abd5e}" /* SENSGUID_EVENTCLASS_LOGON */); + if (FAILED(hr)) + break; - hr = pIEventSubscription->put_SubscriberInterface((IUnknown*)g_pISensLogon); - if (FAILED(hr)) - break; + hr = pIEventSubscription->put_SubscriberInterface((IUnknown*)g_pISensLogon); + if (FAILED(hr)) + break; - PRTUTF16 pwszTemp; - int rc = RTStrToUtf16(g_aSENSEvents[i].pszMethod, &pwszTemp); - if (RT_SUCCESS(rc)) - { - hr = pIEventSubscription->put_MethodName(pwszTemp); - RTUtf16Free(pwszTemp); - } - else - hr = ERROR_OUTOFMEMORY; - if (FAILED(hr)) - break; + PRTUTF16 pwszTemp; + int rc = RTStrToUtf16(g_aSENSEvents[i].pszMethod, &pwszTemp); + if (RT_SUCCESS(rc)) + { + hr = pIEventSubscription->put_MethodName(pwszTemp); + RTUtf16Free(pwszTemp); + } + else + hr = ERROR_OUTOFMEMORY; + if (FAILED(hr)) + break; - rc = RTStrToUtf16(g_aSENSEvents[i].pszSubscriptionName, &pwszTemp); - if (RT_SUCCESS(rc)) - { - hr = pIEventSubscription->put_SubscriptionName(pwszTemp); - RTUtf16Free(pwszTemp); - } - else - hr = ERROR_OUTOFMEMORY; - if (FAILED(hr)) - break; + rc = RTStrToUtf16(g_aSENSEvents[i].pszSubscriptionName, &pwszTemp); + if (RT_SUCCESS(rc)) + { + hr = pIEventSubscription->put_SubscriptionName(pwszTemp); + RTUtf16Free(pwszTemp); + } + else + hr = ERROR_OUTOFMEMORY; + if (FAILED(hr)) + break; - rc = RTStrToUtf16(g_aSENSEvents[i].pszSubscriptionUUID, &pwszTemp); - if (RT_SUCCESS(rc)) - { - hr = pIEventSubscription->put_SubscriptionID(pwszTemp); - RTUtf16Free(pwszTemp); - } - else - hr = ERROR_OUTOFMEMORY; - if (FAILED(hr)) - break; + rc = RTStrToUtf16(g_aSENSEvents[i].pszSubscriptionUUID, &pwszTemp); + if (RT_SUCCESS(rc)) + { + hr = pIEventSubscription->put_SubscriptionID(pwszTemp); + RTUtf16Free(pwszTemp); + } + else + hr = ERROR_OUTOFMEMORY; + if (FAILED(hr)) + break; - hr = pIEventSubscription->put_PerUser(TRUE); - if (FAILED(hr)) - break; + hr = pIEventSubscription->put_PerUser(TRUE); + if (FAILED(hr)) + break; + + hr = g_pIEventSystem->Store(PROGID_EventSubscription, (IUnknown*)pIEventSubscription); + if (FAILED(hr)) + break; + + pIEventSubscription->Release(); + pIEventSubscription = NULL; + } - hr = g_pIEventSystem->Store(PROGID_EventSubscription, (IUnknown*)pIEventSubscription); if (FAILED(hr)) - break; + VBoxCredProvVerbose(0, "VBoxCredentialProviderRegisterSENS: Could not register \"%s\" (%s), hr=%Rhrc\n", + g_aSENSEvents[i].pszMethod, g_aSENSEvents[i].pszSubscriptionName, hr); - pIEventSubscription->Release(); - pIEventSubscription = NULL; + if (pIEventSubscription != NULL) + pIEventSubscription->Release(); } if (FAILED(hr)) - VBoxCredProvVerbose(0, "VBoxCredentialProviderRegisterSENS: Could not register \"%s\" (%s), hr=%Rhrc\n", - g_aSENSEvents[i].pszMethod, g_aSENSEvents[i].pszSubscriptionName, hr); - - if (pIEventSubscription != NULL) - pIEventSubscription->Release(); + { + VBoxCredProvVerbose(0, "VBoxCredentialProviderRegisterSENS: Error registering SENS provider, hr=%Rhrc\n", hr); + if (g_pIEventSystem) + { + g_pIEventSystem->Release(); + g_pIEventSystem = NULL; + } + } + VBoxCredProvVerbose(0, "VBoxCredentialProviderRegisterSENS: Returning hr=%Rhrc\n", hr); return hr; } @@ -287,12 +308,17 @@ static HRESULT VBoxCredentialProviderRegisterSENS(void) static void VBoxCredentialProviderUnregisterSENS(void) { if (g_pIEventSystem) + { g_pIEventSystem->Release(); + g_pIEventSystem = NULL; + } /* We need to reconnecto to the event system because we can be called * in a different context COM can't handle. */ - HRESULT hr = CoCreateInstance(CLSID_CEventSystem, 0, CLSCTX_SERVER, IID_IEventSystem, (void**)&g_pIEventSystem); - if (SUCCEEDED(hr)) + HRESULT hr = CoCreateInstance(CLSID_CEventSystem, 0, + CLSCTX_SERVER, IID_IEventSystem, (void**)&g_pIEventSystem); + if ( SUCCEEDED(hr) + && g_pIEventSystem) { VBoxCredProvVerbose(0, "VBoxCredentialProviderUnregisterSENS\n"); @@ -329,10 +355,16 @@ static void VBoxCredentialProviderUnregisterSENS(void) } g_pIEventSystem->Release(); + g_pIEventSystem = NULL; } if (g_pISensLogon) + { delete g_pISensLogon; + g_pISensLogon = NULL; + } + + VBoxCredProvVerbose(0, "VBoxCredentialProviderUnregisterSENS: Returning hr=%Rhrc\n", hr); } #endif /* VBOX_WITH_SENS */ @@ -347,7 +379,7 @@ BOOL WINAPI DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID pReserved) { case DLL_PROCESS_ATTACH: { - int rc = RTR3InitDll(0 /* Flags */); + int rc = RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE); if (RT_SUCCESS(rc)) rc = VbglR3Init(); @@ -372,6 +404,9 @@ BOOL WINAPI DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID pReserved) case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: break; + + default: + break; } return TRUE; @@ -426,7 +461,8 @@ HRESULT __stdcall DllCanUnloadNow(void) #ifdef VBOX_WITH_SENS if (!g_cDllRefs) { - VBoxCredentialProviderUnregisterSENS(); + if (g_fSENSEnabled) + VBoxCredentialProviderUnregisterSENS(); CoUninitialize(); } @@ -452,23 +488,55 @@ HRESULT VBoxCredentialProviderCreate(REFCLSID classID, REFIID interfaceID, HRESULT hr; if (classID == CLSID_VBoxCredProvider) { - VBoxCredProvFactory* pFactory = new VBoxCredProvFactory(); - if (pFactory) + try { + VBoxCredProvFactory* pFactory = new VBoxCredProvFactory(); + AssertPtr(pFactory); hr = pFactory->QueryInterface(interfaceID, ppvInterface); pFactory->Release(); #ifdef VBOX_WITH_SENS - if (SUCCEEDED(hr)) + g_fSENSEnabled = true; /* By default SENS support is enabled. */ + + HKEY hKey; + /** @todo Add some registry wrapper function(s) as soon as we got more values to retrieve. */ + DWORD dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Oracle\\VirtualBox Guest Additions\\AutoLogon", + 0L, KEY_QUERY_VALUE, &hKey); + if (dwRet == ERROR_SUCCESS) + { + DWORD dwValue; + DWORD dwType = REG_DWORD; + DWORD dwSize = sizeof(DWORD); + + dwRet = RegQueryValueEx(hKey, L"HandleSENS", NULL, &dwType, (LPBYTE)&dwValue, &dwSize); + if ( dwRet == ERROR_SUCCESS + && dwType == REG_DWORD + && dwSize == sizeof(DWORD)) + { + g_fSENSEnabled = RT_BOOL(dwValue); + } + + RegCloseKey(hKey); + } + + VBoxCredProvVerbose(0, "VBoxCredentialProviderCreate: g_fSENSEnabled=%RTbool\n", + g_fSENSEnabled); + if ( SUCCEEDED(hr) + && g_fSENSEnabled) { HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED); VBoxCredentialProviderRegisterSENS(); } +#else + VBoxCredProvVerbose(0, "VBoxCredentialProviderCreate: SENS support is disabled\n"); #endif } - else + catch (std::bad_alloc &ex) + { + NOREF(ex); hr = E_OUTOFMEMORY; + } } else hr = CLASS_E_CLASSNOTAVAILABLE; |
