2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2016-03-30 18:59:23 +02:00
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <fstream>
|
|
|
|
#include <direct.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shlwapi.h>
|
2016-04-01 07:37:03 +02:00
|
|
|
#include <shellapi.h>
|
2016-04-14 08:38:57 +02:00
|
|
|
#include <shlobj.h>
|
2016-07-05 13:49:49 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2016-03-30 18:59:23 +02:00
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
static std::string GetIcingaInstallPath(void)
|
2016-03-30 18:59:23 +02:00
|
|
|
{
|
2016-03-31 10:25:44 +02:00
|
|
|
char szFileName[MAX_PATH];
|
2017-12-14 15:37:20 +01:00
|
|
|
if (!GetModuleFileName(nullptr, szFileName, sizeof(szFileName)))
|
2016-03-31 10:25:44 +02:00
|
|
|
return "";
|
2016-04-14 08:38:57 +02:00
|
|
|
|
|
|
|
if (!PathRemoveFileSpec(szFileName))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
if (!PathRemoveFileSpec(szFileName))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
return szFileName;
|
2016-03-31 10:25:44 +02:00
|
|
|
}
|
|
|
|
|
2016-02-26 13:42:36 +01:00
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
static bool ExecuteCommand(const std::string& app, const std::string& arguments)
|
2016-03-31 10:25:44 +02:00
|
|
|
{
|
2016-04-01 07:37:03 +02:00
|
|
|
SHELLEXECUTEINFO sei = {};
|
|
|
|
sei.cbSize = sizeof(sei);
|
|
|
|
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
|
2016-04-14 08:38:57 +02:00
|
|
|
sei.lpFile = app.c_str();
|
|
|
|
sei.lpParameters = arguments.c_str();
|
2016-04-01 07:37:03 +02:00
|
|
|
sei.nShow = SW_HIDE;
|
|
|
|
if (!ShellExecuteEx(&sei))
|
|
|
|
return false;
|
2017-12-13 12:54:14 +01:00
|
|
|
|
2016-04-01 07:37:03 +02:00
|
|
|
if (!sei.hProcess)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
WaitForSingleObject(sei.hProcess, INFINITE);
|
|
|
|
|
|
|
|
DWORD exitCode;
|
2016-04-13 13:43:38 +02:00
|
|
|
BOOL res = GetExitCodeProcess(sei.hProcess, &exitCode);
|
2016-04-01 07:37:03 +02:00
|
|
|
CloseHandle(sei.hProcess);
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return exitCode == 0;
|
2016-03-31 10:25:44 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
static bool ExecuteIcingaCommand(const std::string& arguments)
|
2016-03-31 10:25:44 +02:00
|
|
|
{
|
2016-04-01 08:25:36 +02:00
|
|
|
return ExecuteCommand(GetIcingaInstallPath() + "\\sbin\\icinga2.exe", arguments);
|
2016-03-31 10:25:44 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
static std::string DirName(const std::string& path)
|
|
|
|
{
|
|
|
|
char *spath = strdup(path.c_str());
|
|
|
|
|
|
|
|
if (!PathRemoveFileSpec(spath)) {
|
|
|
|
free(spath);
|
|
|
|
throw std::runtime_error("PathRemoveFileSpec failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string result = spath;
|
|
|
|
|
|
|
|
free(spath);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PathExists(const std::string& path)
|
2016-03-31 13:54:10 +02:00
|
|
|
{
|
2016-04-14 08:38:57 +02:00
|
|
|
struct _stat statbuf;
|
|
|
|
return (_stat(path.c_str(), &statbuf) >= 0);
|
|
|
|
}
|
2016-03-31 13:54:10 +02:00
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
static std::string GetIcingaDataPath(void)
|
|
|
|
{
|
|
|
|
char path[MAX_PATH];
|
2017-12-14 15:37:20 +01:00
|
|
|
if (!SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_COMMON_APPDATA, nullptr, 0, path)))
|
2016-04-14 08:38:57 +02:00
|
|
|
throw std::runtime_error("SHGetFolderPath failed");
|
|
|
|
return std::string(path) + "\\icinga2";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MkDir(const std::string& path)
|
|
|
|
{
|
|
|
|
if (mkdir(path.c_str()) < 0 && errno != EEXIST)
|
|
|
|
throw std::runtime_error("mkdir failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MkDirP(const std::string& path)
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
while (pos != std::string::npos) {
|
|
|
|
pos = path.find_first_of("/\\", pos + 1);
|
|
|
|
|
|
|
|
std::string spath = path.substr(0, pos + 1);
|
2016-07-05 13:49:49 +02:00
|
|
|
struct _stat statbuf;
|
|
|
|
if (_stat(spath.c_str(), &statbuf) < 0 && errno == ENOENT)
|
2016-04-14 08:38:57 +02:00
|
|
|
MkDir(path.substr(0, pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string GetNSISInstallPath(void)
|
2016-04-01 08:25:36 +02:00
|
|
|
{
|
|
|
|
HKEY hKey;
|
2019-02-25 16:30:40 +01:00
|
|
|
//TODO: Change hardcoded key
|
2016-04-01 08:25:36 +02:00
|
|
|
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Icinga Development Team\\ICINGA2", 0,
|
|
|
|
KEY_QUERY_VALUE | KEY_WOW64_32KEY, &hKey) == ERROR_SUCCESS) {
|
|
|
|
BYTE pvData[MAX_PATH];
|
|
|
|
DWORD cbData = sizeof(pvData) - 1;
|
|
|
|
DWORD lType;
|
2017-12-14 15:37:20 +01:00
|
|
|
if (RegQueryValueEx(hKey, nullptr, nullptr, &lType, pvData, &cbData) == ERROR_SUCCESS && lType == REG_SZ) {
|
2016-04-01 08:25:36 +02:00
|
|
|
pvData[cbData] = '\0';
|
|
|
|
|
|
|
|
return (char *)pvData;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
2016-04-13 13:43:38 +02:00
|
|
|
|
2016-04-18 15:33:20 +02:00
|
|
|
static bool CopyDirectory(const std::string& source, const std::string& destination)
|
|
|
|
{
|
|
|
|
// SHFileOperation requires file names to be terminated with two \0s
|
|
|
|
std::string tmpSource = source + std::string(1, '\0');
|
|
|
|
std::string tmpDestination = destination + std::string(1, '\0');
|
|
|
|
|
|
|
|
SHFILEOPSTRUCT fop;
|
|
|
|
fop.wFunc = FO_COPY;
|
|
|
|
fop.pFrom = tmpSource.c_str();
|
|
|
|
fop.pTo = tmpDestination.c_str();
|
|
|
|
fop.fFlags = FOF_NO_UI;
|
|
|
|
|
|
|
|
return (SHFileOperation(&fop) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool DeleteDirectory(const std::string& dir)
|
|
|
|
{
|
|
|
|
// SHFileOperation requires file names to be terminated with two \0s
|
|
|
|
std::string tmpDir = dir + std::string(1, '\0');
|
|
|
|
|
|
|
|
SHFILEOPSTRUCT fop;
|
|
|
|
fop.wFunc = FO_DELETE;
|
|
|
|
fop.pFrom = tmpDir.c_str();
|
|
|
|
fop.fFlags = FOF_NO_UI;
|
2016-04-13 13:43:38 +02:00
|
|
|
|
2016-04-18 15:33:20 +02:00
|
|
|
return (SHFileOperation(&fop) == 0);
|
2016-04-13 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
2016-04-01 08:25:36 +02:00
|
|
|
static int UpgradeNSIS(void)
|
|
|
|
{
|
2016-04-14 08:38:57 +02:00
|
|
|
std::string installPath = GetNSISInstallPath();
|
2016-04-01 08:25:36 +02:00
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
if (installPath.empty())
|
2016-04-01 08:25:36 +02:00
|
|
|
return 0;
|
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
std::string uninstallerPath = installPath + "\\uninstall.exe";
|
2016-04-14 07:04:36 +02:00
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
if (!PathExists(uninstallerPath))
|
2016-04-01 08:25:36 +02:00
|
|
|
return 0;
|
|
|
|
|
2016-04-18 15:33:20 +02:00
|
|
|
std::string dataPath = GetIcingaDataPath();
|
2016-04-14 07:04:36 +02:00
|
|
|
|
2016-04-18 15:33:20 +02:00
|
|
|
if (dataPath.empty())
|
|
|
|
return 1;
|
2016-04-01 08:25:36 +02:00
|
|
|
|
2016-04-18 15:33:20 +02:00
|
|
|
bool moveUserData = !PathExists(dataPath);
|
2016-04-01 08:25:36 +02:00
|
|
|
|
2016-04-13 13:43:38 +02:00
|
|
|
/* perform open heart surgery on the user's data dirs - yay */
|
2016-04-18 15:33:20 +02:00
|
|
|
if (moveUserData) {
|
2016-04-14 08:38:57 +02:00
|
|
|
MkDir(dataPath.c_str());
|
2016-04-13 13:43:38 +02:00
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
std::string oldNameEtc = installPath + "\\etc";
|
|
|
|
std::string newNameEtc = dataPath + "\\etc";
|
2016-04-18 15:33:20 +02:00
|
|
|
if (!CopyDirectory(oldNameEtc, newNameEtc))
|
2016-04-13 13:43:38 +02:00
|
|
|
return 1;
|
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
std::string oldNameVar = installPath + "\\var";
|
|
|
|
std::string newNameVar = dataPath + "\\var";
|
2016-04-18 15:33:20 +02:00
|
|
|
if (!CopyDirectory(oldNameVar, newNameVar))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExecuteCommand(uninstallerPath, "/S _?=" + installPath);
|
|
|
|
|
|
|
|
_unlink(uninstallerPath.c_str());
|
|
|
|
|
|
|
|
if (moveUserData) {
|
|
|
|
std::string oldNameEtc = installPath + "\\etc";
|
|
|
|
if (!DeleteDirectory(oldNameEtc))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
std::string oldNameVar = installPath + "\\var";
|
|
|
|
if (!DeleteDirectory(oldNameVar))
|
2016-04-13 13:43:38 +02:00
|
|
|
return 1;
|
2016-04-14 14:24:41 +02:00
|
|
|
|
2016-04-14 14:39:48 +02:00
|
|
|
_rmdir(installPath.c_str());
|
2017-12-13 12:54:14 +01:00
|
|
|
}
|
2016-04-01 08:25:36 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-31 10:25:44 +02:00
|
|
|
static int InstallIcinga(void)
|
|
|
|
{
|
2016-04-14 08:38:57 +02:00
|
|
|
std::string installDir = GetIcingaInstallPath();
|
2021-06-04 11:17:48 +02:00
|
|
|
std::string skelDir = installDir + "\\share\\skel";
|
2016-04-14 08:38:57 +02:00
|
|
|
std::string dataDir = GetIcingaDataPath();
|
|
|
|
|
|
|
|
if (!PathExists(dataDir)) {
|
2021-06-04 11:17:48 +02:00
|
|
|
std::string sourceDir = skelDir + std::string(1, '\0');
|
2016-04-14 08:38:57 +02:00
|
|
|
std::string destinationDir = dataDir + std::string(1, '\0');
|
|
|
|
|
|
|
|
SHFILEOPSTRUCT fop;
|
|
|
|
fop.wFunc = FO_COPY;
|
|
|
|
fop.pFrom = sourceDir.c_str();
|
|
|
|
fop.pTo = destinationDir.c_str();
|
|
|
|
fop.fFlags = FOF_NO_UI | FOF_NOCOPYSECURITYATTRIBS;
|
2016-04-01 08:25:36 +02:00
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
if (SHFileOperation(&fop) != 0)
|
|
|
|
return 1;
|
2016-04-01 08:25:36 +02:00
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
MkDirP(dataDir + "/etc/icinga2/pki");
|
|
|
|
MkDirP(dataDir + "/var/cache/icinga2");
|
2017-09-22 15:00:45 +02:00
|
|
|
MkDirP(dataDir + "/var/lib/icinga2/certs");
|
|
|
|
MkDirP(dataDir + "/var/lib/icinga2/certificate-requests");
|
2016-04-14 08:38:57 +02:00
|
|
|
MkDirP(dataDir + "/var/lib/icinga2/agent/inventory");
|
|
|
|
MkDirP(dataDir + "/var/lib/icinga2/api/config");
|
|
|
|
MkDirP(dataDir + "/var/lib/icinga2/api/log");
|
|
|
|
MkDirP(dataDir + "/var/lib/icinga2/api/zones");
|
|
|
|
MkDirP(dataDir + "/var/log/icinga2/compat/archive");
|
|
|
|
MkDirP(dataDir + "/var/log/icinga2/crash");
|
|
|
|
MkDirP(dataDir + "/var/run/icinga2/cmd");
|
|
|
|
MkDirP(dataDir + "/var/spool/icinga2/perfdata");
|
|
|
|
MkDirP(dataDir + "/var/spool/icinga2/tmp");
|
|
|
|
}
|
2016-04-01 08:25:36 +02:00
|
|
|
|
2021-06-04 11:17:48 +02:00
|
|
|
// Upgrade from versions older than 2.13 by making the windowseventlog feature available,
|
|
|
|
// enable it by default and disable the old mainlog feature.
|
|
|
|
if (!PathExists(dataDir + "/etc/icinga2/features-available/windowseventlog.conf")) {
|
|
|
|
// Disable the old mainlog feature as it is replaced by windowseventlog by default.
|
|
|
|
std::string mainlogEnabledFile = dataDir + "/etc/icinga2/features-enabled/mainlog.conf";
|
|
|
|
if (PathExists(mainlogEnabledFile)) {
|
|
|
|
if (DeleteFileA(mainlogEnabledFile.c_str()) == 0) {
|
|
|
|
throw std::runtime_error("deleting '" + mainlogEnabledFile + "' failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install the new windowseventlog feature. As features-available/windowseventlog.conf is used as a marker file,
|
|
|
|
// copy it as the last step, so that this is run again should the upgrade be interrupted.
|
|
|
|
for (const std::string& d : {"features-enabled", "features-available"}) {
|
|
|
|
std::string sourceFile = skelDir + "/etc/icinga2/" + d + "/windowseventlog.conf";
|
|
|
|
std::string destinationFile = dataDir + "/etc/icinga2/" + d + "/windowseventlog.conf";
|
|
|
|
|
|
|
|
if (CopyFileA(sourceFile.c_str(), destinationFile.c_str(), false) == 0) {
|
|
|
|
throw std::runtime_error("copying '" + sourceFile + "' to '" + destinationFile + "' failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: In Icinga 2.14, rename features-available/mainlog.conf to mainlog.conf.deprecated
|
|
|
|
// so that it's no longer listed as an available feature.
|
|
|
|
|
2016-04-01 08:25:36 +02:00
|
|
|
ExecuteCommand("icacls", "\"" + dataDir + "\" /grant *S-1-5-20:(oi)(ci)m");
|
|
|
|
ExecuteCommand("icacls", "\"" + dataDir + "\\etc\" /inheritance:r /grant:r *S-1-5-20:(oi)(ci)m *S-1-5-32-544:(oi)(ci)f");
|
|
|
|
|
2016-03-31 10:25:44 +02:00
|
|
|
ExecuteIcingaCommand("--scm-install daemon");
|
|
|
|
|
2016-03-30 18:59:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-31 10:25:44 +02:00
|
|
|
static int UninstallIcinga(void)
|
2016-03-30 18:59:23 +02:00
|
|
|
{
|
2016-03-31 10:25:44 +02:00
|
|
|
ExecuteIcingaCommand("--scm-uninstall");
|
|
|
|
|
2016-03-30 18:59:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry point for the installer application.
|
|
|
|
*/
|
|
|
|
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
|
|
|
|
{
|
2017-12-14 15:37:20 +01:00
|
|
|
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
|
2016-04-01 07:37:03 +02:00
|
|
|
|
2016-03-31 10:25:44 +02:00
|
|
|
//AllocConsole();
|
2016-03-30 18:59:23 +02:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (strcmp(lpCmdLine, "install") == 0) {
|
|
|
|
rc = InstallIcinga();
|
|
|
|
} else if (strcmp(lpCmdLine, "uninstall") == 0) {
|
|
|
|
rc = UninstallIcinga();
|
2016-04-14 08:38:57 +02:00
|
|
|
} else if (strcmp(lpCmdLine, "upgrade-nsis") == 0) {
|
|
|
|
rc = UpgradeNSIS();
|
2016-03-30 18:59:23 +02:00
|
|
|
} else {
|
2017-12-14 15:37:20 +01:00
|
|
|
MessageBox(nullptr, "This application should only be run by the MSI installer package.", "Icinga 2 Installer", MB_ICONWARNING);
|
2016-03-30 18:59:23 +02:00
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
//::Sleep(3000s);
|
2016-03-31 10:25:44 +02:00
|
|
|
|
2016-04-14 08:38:57 +02:00
|
|
|
return rc;
|
2016-03-30 18:59:23 +02:00
|
|
|
}
|