X-Git-Url: https://granicus.if.org/sourcecode?a=blobdiff_plain;f=agent%2Fwindows-setup-agent%2FProgram.cs;h=b22b042eb70e077df81744489982638ec4a3e44b;hb=6d81c5c34ff1a5e605997bf9133976d4900665cb;hp=a36af0ef928a723a40e82c34c1542bf1711fe252;hpb=6995b0e5ef6a921d33e37b03790b13a85786b8fe;p=icinga2 diff --git a/agent/windows-setup-agent/Program.cs b/agent/windows-setup-agent/Program.cs index a36af0ef9..b22b042eb 100644 --- a/agent/windows-setup-agent/Program.cs +++ b/agent/windows-setup-agent/Program.cs @@ -2,25 +2,78 @@ using System.IO; using System.Windows.Forms; using Microsoft.Win32; +using System.Runtime.InteropServices; +using System.Text; namespace Icinga { - static class Program - { + internal static class NativeMethods + { + [DllImport("msi.dll", CharSet = CharSet.Unicode)] + internal static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf); + + [DllImport("msi.dll", CharSet = CharSet.Unicode)] + internal static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len); + } + static class Program + { public static string Icinga2InstallDir { get { - RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Icinga Development Team\\ICINGA2"); + StringBuilder szProduct; + + for (int index = 0; ; index++) { + szProduct = new StringBuilder(39); + if (NativeMethods.MsiEnumProducts(index, szProduct) != 0) + break; + + int cbName = 128; + StringBuilder szName = new StringBuilder(cbName); + + if (NativeMethods.MsiGetProductInfo(szProduct.ToString(), "ProductName", szName, ref cbName) != 0) + continue; + + if (szName.ToString() != "Icinga 2") + continue; + + int cbLocation = 1024; + StringBuilder szLocation = new StringBuilder(cbLocation); + if (NativeMethods.MsiGetProductInfo(szProduct.ToString(), "InstallLocation", szLocation, ref cbLocation) == 0) + return szLocation.ToString(); + } + + return ""; + } + } + + public static string Icinga2DataDir + { + get + { + return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\icinga2"; + } + } - if (rk == null) - return ""; + public static string Icinga2User + { + get + { + if (!File.Exists(Icinga2DataDir + "\\etc\\icinga2\\user")) + return "NT AUTHORITY\\NetworkService"; + System.IO.StreamReader file = new System.IO.StreamReader(Icinga2DataDir + "\\etc\\icinga2\\user"); + string line = file.ReadLine(); + file.Close(); - return (string)rk.GetValue(""); + if (line != null) + return line; + else + return "NT AUTHORITY\\NetworkService"; } } + public static void FatalError(Form owner, string message) { MessageBox.Show(owner, message, "Icinga 2 Setup Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -38,12 +91,14 @@ namespace Icinga string installDir = Program.Icinga2InstallDir; - if (installDir == "") + if (installDir == "") { FatalError(null, "Icinga 2 does not seem to be installed properly."); + return; + } Form form; - if (File.Exists(installDir + "\\etc\\icinga2\\features-enabled\\api.conf")) + if (File.Exists(Program.Icinga2DataDir + "\\etc\\icinga2\\features-enabled\\api.conf")) form = new ServiceStatus(); else form = new SetupWizard();