]> granicus.if.org Git - icinga2/blobdiff - agent/windows-setup-agent/SetupWizard.cs
Add option to choose icinga2s user
[icinga2] / agent / windows-setup-agent / SetupWizard.cs
index 8d300eadbb5f62558de226faf44d75cecf6eabc5..a1733e5a2b5d87a5fdb1a4c06b5644fbb5b0f478 100644 (file)
@@ -8,7 +8,6 @@ using System.Runtime.InteropServices;
 using System.Security.Cryptography.X509Certificates;
 using System.Threading;
 using System.Net.NetworkInformation;
-using Microsoft.Win32;
 using System.IO.Compression;
 using System.Diagnostics;
 using System.ServiceProcess;
@@ -19,18 +18,16 @@ namespace Icinga
        public partial class SetupWizard : Form
        {
                private string _TrustedFile;
+               private string Icinga2User;
 
                public SetupWizard()
                {
                        InitializeComponent();
 
                        txtInstanceName.Text = Icinga2InstanceName;
-               }
 
-               private void FatalError(string message)
-               {
-                       MessageBox.Show(this, message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
-                       Application.Exit();
+                       Icinga2User = Program.Icinga2User;
+                       txtUser.Text = Icinga2User;
                }
 
                private void Warning(string message)
@@ -38,19 +35,6 @@ namespace Icinga
                        MessageBox.Show(this, message, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
 
-               private string Icinga2InstallDir
-               {
-                       get
-                       {
-                               RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Icinga Development Team\\ICINGA2");
-
-                               if (rk == null)
-                                       return "";
-
-                               return (string)rk.GetValue("");
-                       }
-               }
-
                private string Icinga2InstanceName
                {
                        get
@@ -85,7 +69,7 @@ namespace Icinga
                {
                        FileStream fp = null;
                        try {
-                               fp = File.Open(Icinga2InstallDir + String.Format("\\etc\\icinga2\\features-enabled\\{0}.conf", feature), FileMode.Create);
+                               fp = File.Open(Program.Icinga2DataDir + String.Format("\\etc\\icinga2\\features-enabled\\{0}.conf", feature), FileMode.Create);
                                using (StreamWriter sw = new StreamWriter(fp, Encoding.ASCII)) {
                                        fp = null;
                                        sw.Write(String.Format("include \"../features-available/{0}.conf\"\n", feature));
@@ -141,10 +125,12 @@ namespace Icinga
                        String result = "";
 
                        using (Process proc = Process.Start(psi)) {
-                               proc.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs args) {
+                               proc.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs args)
+                               {
                                        result += args.Data + "\r\n";
                                };
-                               proc.OutputDataReceived += delegate(object sender, DataReceivedEventArgs args) {
+                               proc.OutputDataReceived += delegate (object sender, DataReceivedEventArgs args)
+                               {
                                        result += args.Data + "\r\n";
                                };
                                proc.BeginOutputReadLine();
@@ -164,15 +150,15 @@ namespace Icinga
                {
                        SetRetrievalStatus(25);
 
-                       string pathPrefix = Icinga2InstallDir + "\\etc\\icinga2\\pki\\" + txtInstanceName.Text;
-
+                       string pathPrefix = Program.Icinga2DataDir + "\\etc\\icinga2\\pki\\" + txtInstanceName.Text;
+                       string processArguments = "pki new-cert --cn \"" + txtInstanceName.Text + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\"";
                        string output;
 
                        if (!File.Exists(pathPrefix + ".crt")) {
-                               if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe",
-                                       "pki new-cert --cn \"" + txtInstanceName.Text + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\"",
+                               if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
+                                       processArguments,
                                        out output)) {
-                                       ShowErrorText(output);
+                                       ShowErrorText("Running command 'icinga2.exe " + processArguments + "' produced the following output:\n" + output);
                                        return;
                                }
                        }
@@ -181,10 +167,11 @@ namespace Icinga
 
                        _TrustedFile = Path.GetTempFileName();
 
-                       if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe",
-                               "pki save-cert --host \"" + host + "\" --port \"" + port + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\" --trustedcert \"" + _TrustedFile + "\"",
+                       processArguments = "pki save-cert --host \"" + host + "\" --port \"" + port + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\" --trustedcert \"" + _TrustedFile + "\"";
+                       if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
+                               processArguments,
                                out output)) {
-                               ShowErrorText(output);
+                               ShowErrorText("Running command 'icinga2.exe " + processArguments + "' produced the following output:\n" + output);
                                return;
                        }
 
@@ -205,7 +192,8 @@ namespace Icinga
                        if (rdoNewMaster.Checked)
                                args += " --master";
 
-                       Invoke((MethodInvoker)delegate {
+                       Invoke((MethodInvoker)delegate
+                       {
                                string master_host, master_port;
                                GetMasterHostPort(out master_host, out master_port);
 
@@ -213,7 +201,7 @@ namespace Icinga
 
                                foreach (ListViewItem lvi in lvwEndpoints.Items) {
                                        args += " --endpoint " + lvi.SubItems[0].Text;
-                                       
+
                                        if (lvi.SubItems.Count > 1)
                                                args += "," + lvi.SubItems[1].Text + "," + lvi.SubItems[2].Text;
                                }
@@ -222,47 +210,69 @@ namespace Icinga
                        if (rdoListener.Checked)
                                args += " --listen ::," + txtListenerPort.Text;
 
-                       args += " --ticket " + txtTicket.Text;
-                       args += " --trustedcert " + _TrustedFile;
-                       args += " --cn " + txtInstanceName.Text;
-                       args += " --zone " + txtInstanceName.Text;
+                       if (chkAcceptConfig.Checked)
+                               args += " --accept-config";
+
+                       if (chkAcceptCommands.Checked)
+                               args += " --accept-commands";
+
+                       args += " --ticket \"" + txtTicket.Text + "\"";
+                       args += " --trustedcert \"" + _TrustedFile + "\"";
+                       args += " --cn \"" + txtInstanceName.Text + "\"";
+                       args += " --zone \"" + txtInstanceName.Text + "\"";
 
-                       if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe",
+                       if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
                                "node setup" + args,
                                out output)) {
-                               ShowErrorText(output);
+                               ShowErrorText("Running command 'icinga2.exe " + "node setup" + args + "' produced the following output:\n" + output);
                                return;
                        }
 
                        SetConfigureStatus(50, "Setting ACLs for the Icinga 2 directory...");
-                       DirectoryInfo di = new DirectoryInfo(Icinga2InstallDir);
+                       DirectoryInfo di = new DirectoryInfo(Program.Icinga2InstallDir);
                        DirectorySecurity ds = di.GetAccessControl();
-                       FileSystemAccessRule rule = new FileSystemAccessRule("NT AUTHORITY\\NetworkService",
+                       FileSystemAccessRule rule = new FileSystemAccessRule(txtUser.Text,
                                FileSystemRights.Modify,
                                InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
-                       ds.AddAccessRule(rule);
-                       di.SetAccessControl(ds);
+                       try {
+                               ds.AddAccessRule(rule);
+                               di.SetAccessControl(ds);
+                       } catch (System.Security.Principal.IdentityNotMappedException) {
+                               ShowErrorText("Could not set ACLs for \"" + txtUser.Text + "\". Identitiy is not mapped.\n");
+                               return;
+                       }
 
                        SetConfigureStatus(75, "Installing the Icinga 2 service...");
 
-                       RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe",
+                       RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
                                "--scm-uninstall",
                                out output);
 
-                       if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe",
+                       if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
                                "daemon --validate",
                                out output)) {
-                               ShowErrorText(output);
+                               ShowErrorText("Running command 'icinga2.exe daemon --validate' produced the following output:\n" + output);
                                return;
                        }
 
-                       if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe",
-                               "--scm-install daemon",
+                       if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
+                               "--scm-install --scm-user \"" + txtUser.Text + "\" daemon",
                                out output)) {
-                               ShowErrorText(output);
+                               ShowErrorText("\nRunning command 'icinga2.exe --scm-install --scm-user \"" +
+                                   txtUser.Text + "\" daemon' produced the following output:\n" + output);
                                return;
                        }
 
+                       if (chkInstallNSCP.Checked) {
+                               SetConfigureStatus(85, "Waiting for NSClient++ installation to complete...");
+
+                               Process proc = new Process();
+                               proc.StartInfo.FileName = "msiexec.exe";
+                               proc.StartInfo.Arguments = "/i \"" + Program.Icinga2InstallDir + "\\sbin\\NSCP.msi\"";
+                               proc.Start();
+                               proc.WaitForExit();
+                       }
+
                        SetConfigureStatus(100, "Finished.");
 
                        FinishConfigure();
@@ -278,27 +288,6 @@ namespace Icinga
                        tbcPages.SelectedTab = tabFinish;
                }
 
-               private void AgentWizard_Shown(object sender, EventArgs e)
-               {
-                       string installDir = Icinga2InstallDir;
-
-                       if (installDir == "")
-                               FatalError("Icinga 2 does not seem to be installed properly.");
-
-                       /* TODO: This is something the NSIS installer should do */
-                       Directory.CreateDirectory(installDir + "\\etc\\icinga2\\pki");
-                       Directory.CreateDirectory(installDir + "\\var\\cache\\icinga2");
-                       Directory.CreateDirectory(installDir + "\\var\\lib\\icinga2\\pki");
-                       Directory.CreateDirectory(installDir + "\\var\\lib\\icinga2\\agent\\inventory");
-                       Directory.CreateDirectory(installDir + "\\var\\lib\\icinga2\\api\\config");
-                       Directory.CreateDirectory(installDir + "\\var\\lib\\icinga2\\api\\log");
-                       Directory.CreateDirectory(installDir + "\\var\\lib\\icinga2\\api\\zones");
-                       Directory.CreateDirectory(installDir + "\\var\\log\\icinga2\\compat\\archive");
-                       Directory.CreateDirectory(installDir + "\\var\\run\\icinga2\\cmd");
-                       Directory.CreateDirectory(installDir + "\\var\\spool\\icinga2\\perfdata");
-                       Directory.CreateDirectory(installDir + "\\var\\spool\\icinga2\\tmp");
-               }
-
                private void btnBack_Click(object sender, EventArgs e)
                {
                        if (tbcPages.SelectedTab == tabError) {
@@ -344,6 +333,11 @@ namespace Icinga
                                        Warning("You need to specify a listener port.");
                                        return;
                                }
+
+                               if (txtUser.Text.Length == 0) {
+                                       Warning("Icinga2 user may not be empty.");
+                                       return;
+                               }
                        }
 
                        if (tbcPages.SelectedTab == tabFinish || tbcPages.SelectedTab == tabError)
@@ -380,7 +374,7 @@ namespace Icinga
                        }
 
                        /*if (tbcPages.SelectedTab == tabParameters &&
-                               !File.Exists(Icinga2InstallDir + "\\etc\\icinga2\\pki\\agent\\agent.crt")) {
+                               !File.Exists(Icinga2DataDir + "\\etc\\icinga2\\pki\\agent\\agent.crt")) {
                                byte[] bytes = Convert.FromBase64String(txtBundle.Text);
                                MemoryStream ms = new MemoryStream(bytes);
                                GZipStream gz = new GZipStream(ms, CompressionMode.Decompress);
@@ -392,7 +386,7 @@ namespace Icinga
                                        ms2.Write(buffer, 0, rc);
                                ms2.Position = 0;
                                TarReader tr = new TarReader(ms2);
-                               tr.ReadToEnd(Icinga2InstallDir + "\\etc\\icinga2\\pki\\agent");
+                               tr.ReadToEnd(Icinga2DataDir + "\\etc\\icinga2\\pki\\agent");
                        }*/
 
                        if (tbcPages.SelectedTab == tabConfigure) {
@@ -502,5 +496,12 @@ namespace Icinga
                                lvwEndpoints.Items.Remove(lvwEndpoints.SelectedItems[0]);
                        }
                }
+
+               private void chkDifferentUser_CheckedChanged(object sender, EventArgs e)
+               {
+                       txtUser.ReadOnly = !txtUser.ReadOnly;
+                       if (txtUser.ReadOnly)
+                               txtUser.Text = Icinga2User;
+               }
        }
 }