From a9bf3959baf46a838d03594aefe0a402dfc759ae Mon Sep 17 00:00:00 2001 From: Kelven Yang Date: Tue, 1 Mar 2011 18:56:18 -0800 Subject: [PATCH] Add DHCP support to windows password manager --- pwdmgr-win/win/service/VMOpsServiceImpl.h | 7 +- .../win/service/VMOpsServiceProvider.cpp | 67 +++++++++++++++++++ .../win/service/VMOpsStartupWatcher.cpp | 9 +-- 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/pwdmgr-win/win/service/VMOpsServiceImpl.h b/pwdmgr-win/win/service/VMOpsServiceImpl.h index 7e3ab5e1540..81e4f7b7172 100644 --- a/pwdmgr-win/win/service/VMOpsServiceImpl.h +++ b/pwdmgr-win/win/service/VMOpsServiceImpl.h @@ -12,6 +12,8 @@ #include "ThreadUtil.h" #include +#include +#include namespace VMOps { @@ -35,6 +37,7 @@ public : public : HERROR SetPassword(LPCTSTR lpszUserName, LPCTSTR lpszPassword); + HERROR GetNextPasswordProvider(LPSTR lpszBuf, LPDWORD pdwLength); HERROR GetDefaultGateway(LPSTR lpszBuf, LPDWORD pdwLength); HERROR SimpleHttpGet(LPCTSTR lpszUrl, LPCTSTR lpszHeaders, LPVOID pOutputBuffer, DWORD dwBytesToRead, DWORD* pdwBytesRead); @@ -44,6 +47,8 @@ public : protected : CVMOpsStartupWatcher* m_pWatcher; + + std::list m_lstProviders; }; ///////////////////////////////////////////////////////////////////////////// @@ -62,7 +67,7 @@ protected : virtual DWORD ThreadRun(); BOOL DoStartupConfig(); - BOOL GetDomRUrl(LPTSTR lpszUrl); + BOOL GetPasswordProviderUrl(LPTSTR lpszUrl); protected : CVMOpsServiceProvider* m_pProvider; diff --git a/pwdmgr-win/win/service/VMOpsServiceProvider.cpp b/pwdmgr-win/win/service/VMOpsServiceProvider.cpp index 156be6e2d53..e722634bd0b 100644 --- a/pwdmgr-win/win/service/VMOpsServiceProvider.cpp +++ b/pwdmgr-win/win/service/VMOpsServiceProvider.cpp @@ -14,9 +14,47 @@ #include #include #include +#include using namespace VMOps; +///////////////////////////////////////////////////////////////////////////// +// Helpers +// +bool IsAddressAlreadyInList(const IP_ADDRESS_STRING& addr, std::list& listDhCPServers) +{ + for(std::list::iterator it = listDhCPServers.begin(); it != listDhCPServers.end(); it++) + { + if(strcmpi(addr.String, (*it).String) == 0) + return true; + } + + return false; +} + +void GetDHCPServers(std::list& listDhCPServers) +{ + ULONG ulSize = 0; + GetAdaptersInfo(NULL, &ulSize); + + PIP_ADAPTER_INFO pHead = (PIP_ADAPTER_INFO)new BYTE[ulSize]; + PIP_ADAPTER_INFO pAdapterInfo = pHead; + GetAdaptersInfo(pAdapterInfo, &ulSize); + + while(pAdapterInfo->Next != NULL) + { + if(pAdapterInfo->DhcpEnabled && !IsAddressAlreadyInList(pAdapterInfo->DhcpServer.IpAddress, listDhCPServers)) + listDhCPServers.push_back(pAdapterInfo->DhcpServer.IpAddress); + + pAdapterInfo = pAdapterInfo->Next; + } + + delete [](LPBYTE)pHead; +} + +///////////////////////////////////////////////////////////////////////////// +// CVMOpsServiceProvider +// CVMOpsServiceProvider::CVMOpsServiceProvider() { m_pWatcher = NULL; @@ -86,6 +124,35 @@ HERROR CVMOpsServiceProvider::SetPassword(LPCTSTR lpszUserName, LPCTSTR lpszPass return HERROR_SUCCESS; } +HERROR CVMOpsServiceProvider::GetNextPasswordProvider(LPSTR lpszBuf, LPDWORD pdwLength) +{ + if(m_lstProviders.size() == 0) + { + IP_ADDRESS_STRING addr; + memset(&addr, 0, sizeof(addr)); + DWORD dwLength = sizeof(addr.String); + GetDefaultGateway(addr.String, &dwLength); + + if(addr.String[0] != 0) + m_lstProviders.push_back(addr); + + GetDHCPServers(m_lstProviders); + } + + if(m_lstProviders.size() > 0) + { + strcpy(lpszBuf, (*(m_lstProviders.begin())).String); + m_lstProviders.pop_front(); + } + else + { + lpszBuf[0] = 0; + } + + return HERROR_SUCCESS; +} + + HERROR CVMOpsServiceProvider::GetDefaultGateway(LPSTR lpszBuf, LPDWORD pdwLength) { _ASSERTE(pdwLength); diff --git a/pwdmgr-win/win/service/VMOpsStartupWatcher.cpp b/pwdmgr-win/win/service/VMOpsStartupWatcher.cpp index 9794797c600..9a88fe0a45e 100644 --- a/pwdmgr-win/win/service/VMOpsStartupWatcher.cpp +++ b/pwdmgr-win/win/service/VMOpsStartupWatcher.cpp @@ -74,13 +74,13 @@ DWORD CVMOpsStartupWatcher::ThreadRun() return 0; } -BOOL CVMOpsStartupWatcher::GetDomRUrl(LPTSTR lpszUrl) +BOOL CVMOpsStartupWatcher::GetPasswordProviderUrl(LPTSTR lpszUrl) { // asumming we have enough space in lpszUrl char achBuf[256]; achBuf[0] = 0; DWORD dwLength = sizeof(achBuf); - if(m_pProvider->GetDefaultGateway(achBuf, &dwLength) == HERROR_SUCCESS) + if(m_pProvider->GetNextPasswordProvider(achBuf, &dwLength) == HERROR_SUCCESS) { USES_CONVERSION; @@ -93,6 +93,7 @@ BOOL CVMOpsStartupWatcher::GetDomRUrl(LPTSTR lpszUrl) return FALSE; } + BOOL CVMOpsStartupWatcher::DoStartupConfig() { USES_CONVERSION; @@ -107,7 +108,7 @@ BOOL CVMOpsStartupWatcher::DoStartupConfig() char achResult[256]; memset(achUrl, 0, sizeof(achUrl)); - GetDomRUrl(achUrl); + GetPasswordProviderUrl(achUrl); if(achUrl[0] != 0) { @@ -120,7 +121,7 @@ BOOL CVMOpsStartupWatcher::DoStartupConfig() achResult, dwBytesToRead, &dwBytesRead) == HERROR_SUCCESS) { achResult[dwBytesRead] = 0; - //Trim whitespace at tail + // Trim whitespace at tail int nPos = strlen(achResult) - 1; while(nPos > 0) {