1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "ShInstUtil.h"
#pragma warning(push)
#pragma warning(disable: 4996) // SCL warning
#include <boost/smart_ptr.hpp>
#include <boost/algorithm/string/trim.hpp>
#pragma warning(pop)
static const std_string g_strNGenInstall = _T("ngen_install");
static const std_string g_strNGenUninstall = _T("ngen_uninstall");
static const std_string g_strNetCheck = _T("net_check");
static const std_string g_strPreLoadRegister = _T("preload_register");
static const std_string g_strPreLoadUnregister = _T("preload_unregister");
static LPCTSTR g_lpPathTrimChars = _T("\"' \t\r\n");
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(hInstance);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
INITCOMMONCONTROLSEX icc;
ZeroMemory(&icc, sizeof(INITCOMMONCONTROLSEX));
icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
icc.dwICC = ICC_STANDARD_CLASSES;
InitCommonControlsEx(&icc);
std_string strCmdLine = GetCommandLine();
boost::trim_if(strCmdLine, boost::is_any_of(g_lpPathTrimChars));
std::transform(strCmdLine.begin(), strCmdLine.end(), strCmdLine.begin(), tolower);
if((strCmdLine.size() >= g_strNGenInstall.size()) && (strCmdLine.substr(
strCmdLine.size() - g_strNGenInstall.size()) == g_strNGenInstall))
{
UpdateNativeImage(false);
Sleep(200);
UpdateNativeImage(true);
}
if((strCmdLine.size() >= g_strNGenUninstall.size()) && (strCmdLine.substr(
strCmdLine.size() - g_strNGenUninstall.size()) == g_strNGenUninstall))
{
UpdateNativeImage(false);
}
if((strCmdLine.size() >= g_strPreLoadRegister.size()) && (strCmdLine.substr(
strCmdLine.size() - g_strPreLoadRegister.size()) == g_strPreLoadRegister))
{
RegisterPreLoad(true);
}
if((strCmdLine.size() >= g_strPreLoadUnregister.size()) && (strCmdLine.substr(
strCmdLine.size() - g_strPreLoadUnregister.size()) == g_strPreLoadUnregister))
{
RegisterPreLoad(false);
}
if((strCmdLine.size() >= g_strNetCheck.size()) && (strCmdLine.substr(
strCmdLine.size() - g_strNetCheck.size()) == g_strNetCheck))
{
CheckDotNetInstalled();
}
return 0;
}
void UpdateNativeImage(bool bInstall)
{
const std_string strNGen = FindNGen();
if(strNGen.size() == 0) return;
const std_string strKeePassExe = GetKeePassExePath();
if(strKeePassExe.size() == 0) return;
std_string strParam = (bInstall ? _T("") : _T("un"));
strParam += _T("install \"");
strParam += strKeePassExe + _T("\"");
SHELLEXECUTEINFO sei;
ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = _T("open");
sei.lpFile = strNGen.c_str();
sei.lpParameters = strParam.c_str();
sei.nShow = SW_HIDE;
ShellExecuteEx(&sei);
if(sei.hProcess != NULL)
{
WaitForSingleObject(sei.hProcess, 16000);
CloseHandle(sei.hProcess);
}
}
void RegisterPreLoad(bool bRegister)
{
const std_string strPath = GetKeePassExePath();
if(strPath.size() == 0) return;
HKEY hKey = NULL;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
0, KEY_WRITE, &hKey) != ERROR_SUCCESS) return;
if(hKey == NULL) return;
const std_string strItemName = _T("KeePass 2 PreLoad");
std_string strItemValue = _T("\"");
strItemValue += strPath;
strItemValue += _T("\" --preload");
if(bRegister)
RegSetValueEx(hKey, strItemName.c_str(), 0, REG_SZ,
(const BYTE*)strItemValue.c_str(), static_cast<DWORD>(
(strItemValue.size() + 1) * sizeof(TCHAR)));
else
RegDeleteValue(hKey, strItemName.c_str());
RegCloseKey(hKey);
}
std_string GetNetInstallRoot()
{
std_string str;
HKEY hNet = NULL;
LONG lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\.NETFramework"),
0, KEY_READ | KEY_WOW64_64KEY, &hNet);
if((lRes != ERROR_SUCCESS) || (hNet == NULL)) return str;
const DWORD cbData = 2050;
BYTE pbData[cbData];
ZeroMemory(pbData, cbData * sizeof(BYTE));
DWORD dwData = cbData - 2;
lRes = RegQueryValueEx(hNet, _T("InstallRoot"), NULL, NULL, pbData, &dwData);
if(lRes == ERROR_SUCCESS) str = (LPCTSTR)(LPTSTR)pbData;
RegCloseKey(hNet);
return str;
}
std_string GetKeePassExePath()
{
const DWORD cbData = 2050;
TCHAR tszName[cbData];
ZeroMemory(tszName, cbData * sizeof(TCHAR));
GetModuleFileName(NULL, tszName, cbData - 2);
for(int i = static_cast<int>(_tcslen(tszName)) - 1; i >= 0; --i)
{
if(tszName[i] == _T('\\')) break;
else tszName[i] = 0;
}
std_string strPath = tszName;
boost::trim_if(strPath, boost::is_any_of(g_lpPathTrimChars));
if(strPath.size() == 0) return strPath;
return (strPath + _T("KeePass.exe"));
}
void EnsureTerminatingSeparator(std_string& strPath)
{
if(strPath.size() == 0) return;
if(strPath.c_str()[strPath.size() - 1] == _T('\\')) return;
strPath += _T("\\");
}
std_string FindNGen()
{
std_string strNGen;
std_string strRoot = GetNetInstallRoot();
if(strRoot.size() == 0) return strNGen;
EnsureTerminatingSeparator(strRoot);
ULONGLONG ullVersion = 0;
FindNGenRec(strRoot, strNGen, ullVersion);
return strNGen;
}
void FindNGenRec(const std_string& strPath, std_string& strNGenPath,
ULONGLONG& ullVersion)
{
const std_string strSearch = strPath + _T("*.*");
const std_string strNGen = _T("ngen.exe");
WIN32_FIND_DATA wfd;
ZeroMemory(&wfd, sizeof(WIN32_FIND_DATA));
HANDLE hFind = FindFirstFile(strSearch.c_str(), &wfd);
if(hFind == INVALID_HANDLE_VALUE) return;
do
{
if((wfd.cFileName[0] == 0) || (_tcsicmp(wfd.cFileName, _T(".")) == 0) ||
(_tcsicmp(wfd.cFileName, _T("..")) == 0)) { }
else if((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
FindNGenRec((strPath + wfd.cFileName) + _T("\\"), strNGenPath, ullVersion);
else if(_tcsicmp(wfd.cFileName, strNGen.c_str()) == 0)
{
const std_string strFullPath = strPath + strNGen;
const ULONGLONG ullThisVer = SiuGetFileVersion(strFullPath);
if(ullThisVer >= ullVersion)
{
strNGenPath = strFullPath;
ullVersion = ullThisVer;
}
}
}
while(FindNextFile(hFind, &wfd) != FALSE);
FindClose(hFind);
}
ULONGLONG SiuGetFileVersion(const std_string& strFilePath)
{
DWORD dwDummy = 0;
const DWORD dwVerSize = GetFileVersionInfoSize(
strFilePath.c_str(), &dwDummy);
if(dwVerSize == 0) return 0;
boost::scoped_array<BYTE> vVerInfo(new BYTE[dwVerSize]);
if(vVerInfo.get() == NULL) return 0; // Out of memory
if(GetFileVersionInfo(strFilePath.c_str(), 0, dwVerSize,
vVerInfo.get()) == FALSE) return 0;
VS_FIXEDFILEINFO* pFileInfo = NULL;
UINT uFixedInfoLen = 0;
if(VerQueryValue(vVerInfo.get(), _T("\\"), (LPVOID*)&pFileInfo,
&uFixedInfoLen) == FALSE) return 0;
if(pFileInfo == NULL) return 0;
return ((static_cast<ULONGLONG>(pFileInfo->dwFileVersionMS) <<
32) | static_cast<ULONGLONG>(pFileInfo->dwFileVersionLS));
}
void CheckDotNetInstalled()
{
OSVERSIONINFO osv;
ZeroMemory(&osv, sizeof(OSVERSIONINFO));
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osv);
if(osv.dwMajorVersion >= 6) return; // .NET ships with Vista and higher
const std_string strNGen = FindNGen();
if(strNGen.size() == 0)
{
std_string strMsg = _T("KeePass 2.x requires the Microsoft .NET Framework >= 2.0, ");
strMsg += _T("however this framework currently doesn't seem to be installed ");
strMsg += _T("on your computer. Without this framework, KeePass will not run.\r\n\r\n");
strMsg += _T("The Microsoft .NET Framework is available as free download from the ");
strMsg += _T("Microsoft website.\r\n\r\n");
strMsg += _T("Do you want to visit the Microsoft website now?");
const int nRes = MessageBox(NULL, strMsg.c_str(), _T("KeePass Setup"),
MB_ICONQUESTION | MB_YESNO);
if(nRes == IDYES)
{
SHELLEXECUTEINFO sei;
ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.lpVerb = _T("open");
sei.lpFile = _T("https://msdn.microsoft.com/en-us/netframework/aa569263.aspx");
sei.nShow = SW_SHOW;
ShellExecuteEx(&sei);
}
}
}
|