]> granicus.if.org Git - icinga2/blob - agent/windows-setup-agent/Program.cs
Merge pull request #5718 from Icinga/fix/verify-error-codes-and-returned-log-messages...
[icinga2] / agent / windows-setup-agent / Program.cs
1 using System;
2 using System.IO;
3 using System.Windows.Forms;
4 using Microsoft.Win32;
5 using System.Runtime.InteropServices;
6 using System.Text;
7
8 namespace Icinga
9 {
10         static class Program
11         {
12                 [DllImport("msi.dll", SetLastError = true)]
13                 static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf);
14
15                 [DllImport("msi.dll", CharSet = CharSet.Unicode)]
16                 static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len);
17
18                 public static string Icinga2InstallDir
19                 {
20                         get
21                         {
22                                 StringBuilder szProduct;
23
24                                 for (int index = 0; ; index++) {
25                                         szProduct = new StringBuilder(39);
26                                         if (MsiEnumProducts(index, szProduct) != 0)
27                                                 break;
28
29                                         int cbName = 128;
30                                         StringBuilder szName = new StringBuilder(cbName);
31
32                                         if (MsiGetProductInfo(szProduct.ToString(), "ProductName", szName, ref cbName) != 0)
33                                                 continue;
34
35                                         if (szName.ToString() != "Icinga 2")
36                                                 continue;
37
38                                         int cbLocation = 1024;
39                                         StringBuilder szLocation = new StringBuilder(cbLocation);
40                                         if (MsiGetProductInfo(szProduct.ToString(), "InstallLocation", szLocation, ref cbLocation) == 0)
41                                                 return szLocation.ToString();
42                                 }
43
44                                 return "";
45                         }
46                 }
47
48                 public static string Icinga2DataDir
49                 {
50                         get
51                         {
52                                 return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\icinga2";
53                         }
54                 }
55
56                 public static string Icinga2User
57                 {
58                         get
59                         {
60                                 if (!File.Exists(Icinga2DataDir + "\\etc\\icinga2\\user"))
61                                         return "NT AUTHORITY\\NetworkService";
62                                 System.IO.StreamReader file = new System.IO.StreamReader(Icinga2DataDir + "\\etc\\icinga2\\user");
63                                 string line = file.ReadLine();
64                                 file.Close();
65
66                                 if (line != null)
67                                         return line;
68                                 else
69                                         return "NT AUTHORITY\\NetworkService";
70                         }
71                 }
72
73
74                 public static void FatalError(Form owner, string message)
75                 {
76                         MessageBox.Show(owner, message, "Icinga 2 Setup Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
77                         Application.Exit();
78                 }
79
80                 /// <summary>
81                 /// The main entry point for the application.
82                 /// </summary>
83                 [STAThread]
84                 static void Main()
85                 {
86                         Application.EnableVisualStyles();
87                         Application.SetCompatibleTextRenderingDefault(false);
88
89                         string installDir = Program.Icinga2InstallDir;
90
91                         if (installDir == "") {
92                                 FatalError(null, "Icinga 2 does not seem to be installed properly.");
93                                 return;
94                         }
95
96                         Form form;
97
98                         if (File.Exists(Program.Icinga2DataDir + "\\etc\\icinga2\\features-enabled\\api.conf"))
99                                 form = new ServiceStatus();
100                         else
101                                 form = new SetupWizard();
102
103                         Application.Run(form);
104                 }
105         }
106 }