4 using System.Collections.Generic;
5 using System.ComponentModel;
6 using System.Windows.Forms;
7 using System.Runtime.InteropServices;
8 using System.Security.Cryptography.X509Certificates;
9 using System.Threading;
10 using System.Net.NetworkInformation;
11 using System.IO.Compression;
12 using System.Diagnostics;
13 using System.ServiceProcess;
14 using System.Security.AccessControl;
18 public partial class SetupWizard : Form
20 private string _TrustedFile;
21 private string Icinga2User;
25 InitializeComponent();
27 txtInstanceName.Text = Icinga2InstanceName;
29 Icinga2User = Program.Icinga2User;
30 txtUser.Text = Icinga2User;
33 private void Warning(string message)
35 MessageBox.Show(this, message, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
38 private string Icinga2InstanceName
42 IPGlobalProperties props = IPGlobalProperties.GetIPGlobalProperties();
44 string fqdn = props.HostName;
46 if (props.DomainName != "")
47 fqdn += "." + props.DomainName;
53 private bool GetMasterHostPort(out string host, out string port)
55 foreach (ListViewItem lvi in lvwEndpoints.Items) {
56 if (lvi.SubItems.Count > 1) {
57 host = lvi.SubItems[1].Text;
58 port = lvi.SubItems[2].Text;
68 private void EnableFeature(string feature)
72 fp = File.Open(Program.Icinga2DataDir + String.Format("\\etc\\icinga2\\features-enabled\\{0}.conf", feature), FileMode.Create);
73 using (StreamWriter sw = new StreamWriter(fp, Encoding.ASCII)) {
75 sw.Write(String.Format("include \"../features-available/{0}.conf\"\n", feature));
83 private void SetRetrievalStatus(int pct)
86 Invoke((MethodInvoker)delegate { SetRetrievalStatus(pct); });
90 prgRetrieveCertificate.Value = pct;
93 private void SetConfigureStatus(int pct, string message)
96 Invoke((MethodInvoker)delegate { SetConfigureStatus(pct, message); });
100 prgConfig.Value = pct;
101 lblConfigStatus.Text = message;
104 private void ShowErrorText(string text)
106 if (InvokeRequired) {
107 Invoke((MethodInvoker)delegate { ShowErrorText(text); });
111 txtError.Text = text;
112 tbcPages.SelectedTab = tabError;
115 private bool RunProcess(string filename, string arguments, out string output)
117 ProcessStartInfo psi = new ProcessStartInfo();
118 psi.FileName = filename;
119 psi.Arguments = arguments;
120 psi.CreateNoWindow = true;
121 psi.UseShellExecute = false;
122 psi.RedirectStandardOutput = true;
123 psi.RedirectStandardError = true;
127 using (Process proc = Process.Start(psi)) {
128 proc.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs args)
130 result += args.Data + "\r\n";
132 proc.OutputDataReceived += delegate (object sender, DataReceivedEventArgs args)
134 result += args.Data + "\r\n";
136 proc.BeginOutputReadLine();
137 proc.BeginErrorReadLine();
142 if (proc.ExitCode != 0)
149 private void VerifyCertificate(string host, string port)
151 SetRetrievalStatus(25);
153 string pathPrefix = Program.Icinga2DataDir + "\\etc\\icinga2\\pki\\" + txtInstanceName.Text;
154 string processArguments = "pki new-cert --cn \"" + txtInstanceName.Text + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\"";
157 if (!File.Exists(pathPrefix + ".crt")) {
158 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
161 ShowErrorText("Running command 'icinga2.exe " + processArguments + "' produced the following output:\n" + output);
166 SetRetrievalStatus(50);
168 _TrustedFile = Path.GetTempFileName();
170 processArguments = "pki save-cert --host \"" + host + "\" --port \"" + port + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\" --trustedcert \"" + _TrustedFile + "\"";
171 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
174 ShowErrorText("Running command 'icinga2.exe " + processArguments + "' produced the following output:\n" + output);
178 SetRetrievalStatus(100);
180 X509Certificate2 cert = new X509Certificate2(_TrustedFile);
181 Invoke((MethodInvoker)delegate { ShowCertificatePrompt(cert); });
184 private void ConfigureService()
186 SetConfigureStatus(0, "Updating configuration files...");
192 Invoke((MethodInvoker)delegate
194 string master_host, master_port;
195 GetMasterHostPort(out master_host, out master_port);
197 args += " --master_host " + master_host + "," + master_port;
199 foreach (ListViewItem lvi in lvwEndpoints.Items) {
200 args += " --endpoint " + lvi.SubItems[0].Text;
202 if (lvi.SubItems.Count > 1)
203 args += "," + lvi.SubItems[1].Text + "," + lvi.SubItems[2].Text;
207 if (rdoListener.Checked)
208 args += " --listen ::," + txtListenerPort.Text;
210 if (chkAcceptConfig.Checked)
211 args += " --accept-config";
213 if (chkAcceptCommands.Checked)
214 args += " --accept-commands";
216 if (txtTicket.Text != "")
217 args += " --ticket \"" + txtTicket.Text + "\"";
219 args += " --trustedcert \"" + _TrustedFile + "\"";
220 args += " --cn \"" + txtInstanceName.Text + "\"";
221 args += " --zone \"" + txtInstanceName.Text + "\"";
223 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
226 ShowErrorText("Running command 'icinga2.exe " + "node setup" + args + "' produced the following output:\n" + output);
230 SetConfigureStatus(50, "Setting ACLs for the Icinga 2 directory...");
231 DirectoryInfo di = new DirectoryInfo(Program.Icinga2InstallDir);
232 DirectorySecurity ds = di.GetAccessControl();
233 FileSystemAccessRule rule = new FileSystemAccessRule(txtUser.Text,
234 FileSystemRights.Modify,
235 InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
237 ds.AddAccessRule(rule);
238 di.SetAccessControl(ds);
239 } catch (System.Security.Principal.IdentityNotMappedException) {
240 ShowErrorText("Could not set ACLs for \"" + txtUser.Text + "\". Identitiy is not mapped.\n");
244 SetConfigureStatus(75, "Installing the Icinga 2 service...");
246 RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
250 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
253 ShowErrorText("Running command 'icinga2.exe daemon --validate' produced the following output:\n" + output);
257 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
258 "--scm-install --scm-user \"" + txtUser.Text + "\" daemon",
260 ShowErrorText("\nRunning command 'icinga2.exe --scm-install --scm-user \"" +
261 txtUser.Text + "\" daemon' produced the following output:\n" + output);
265 if (chkInstallNSCP.Checked) {
266 SetConfigureStatus(85, "Waiting for NSClient++ installation to complete...");
268 Process proc = new Process();
269 proc.StartInfo.FileName = "msiexec.exe";
270 proc.StartInfo.Arguments = "/i \"" + Program.Icinga2InstallDir + "\\sbin\\NSCP.msi\"";
275 SetConfigureStatus(100, "Finished.");
277 // Override the completed text
278 lblSetupCompleted.Text = "The Icinga 2 Windows client was set up successfully.";
280 // Add a note for the user for ticket-less signing
281 if (txtTicket.Text == "") {
282 lblSetupCompleted.Text += "\n\nTicket was not specified. Please sign the certificate request on the Icinga 2 master node (requires v2.8+).";
288 private void FinishConfigure()
290 if (InvokeRequired) {
291 Invoke((MethodInvoker)FinishConfigure);
295 tbcPages.SelectedTab = tabFinish;
298 private void btnBack_Click(object sender, EventArgs e)
300 if (tbcPages.SelectedTab == tabError) {
301 tbcPages.SelectedIndex = 0;
307 if (tbcPages.SelectedTab == tabVerifyCertificate)
310 tbcPages.SelectedIndex -= offset;
313 private void btnNext_Click(object sender, EventArgs e)
315 if (tbcPages.SelectedTab == tabParameters) {
316 if (txtInstanceName.Text.Length == 0) {
317 Warning("Please enter an instance name.");
321 if (lvwEndpoints.Items.Count == 0) {
322 Warning("You need to add at least one master/satellite endpoint.");
327 if (!GetMasterHostPort(out host, out port)) {
328 Warning("Please enter a remote host and port for at least one of your endpoints.");
332 if (rdoListener.Checked && (txtListenerPort.Text == "")) {
333 Warning("You need to specify a listener port.");
337 if (txtUser.Text.Length == 0) {
338 Warning("Icinga 2 user may not be empty.");
343 if (tbcPages.SelectedTab == tabFinish || tbcPages.SelectedTab == tabError)
346 tbcPages.SelectedIndex++;
349 private void btnCancel_Click(object sender, EventArgs e)
354 private void tbcPages_SelectedIndexChanged(object sender, EventArgs e)
358 btnBack.Enabled = (tbcPages.SelectedTab == tabVerifyCertificate || tbcPages.SelectedTab == tabError);
359 btnNext.Enabled = (tbcPages.SelectedTab == tabParameters || tbcPages.SelectedTab == tabVerifyCertificate || tbcPages.SelectedTab == tabFinish);
361 if (tbcPages.SelectedTab == tabFinish) {
362 btnNext.Text = "&Finish >";
363 btnCancel.Enabled = false;
366 if (tbcPages.SelectedTab == tabRetrieveCertificate) {
367 ListViewItem lvi = lvwEndpoints.Items[0];
369 string master_host, master_port;
370 GetMasterHostPort(out master_host, out master_port);
372 Thread thread = new Thread((ThreadStart)delegate { VerifyCertificate(master_host, master_port); });
376 if (tbcPages.SelectedTab == tabConfigure) {
377 Thread thread = new Thread(ConfigureService);
382 private void RadioListener_CheckedChanged(object sender, EventArgs e)
384 txtListenerPort.Enabled = rdoListener.Checked;
387 private void AddCertificateField(string name, string shortValue, string longValue = null)
389 ListViewItem lvi = new ListViewItem();
391 lvi.SubItems.Add(shortValue);
392 if (longValue == null)
393 longValue = shortValue;
395 lvwX509Fields.Items.Add(lvi);
398 private string PadText(string input)
402 for (int i = 0; i < input.Length; i += 2) {
407 if (input.Length - i < 2)
408 len = input.Length - i;
409 output += input.Substring(i, len);
415 private void ShowCertificatePrompt(X509Certificate2 certificate)
417 txtX509Issuer.Text = certificate.Issuer;
418 txtX509Subject.Text = certificate.Subject;
420 lvwX509Fields.Items.Clear();
422 AddCertificateField("Version", "V" + certificate.Version.ToString());
423 AddCertificateField("Serial number", certificate.SerialNumber);
424 AddCertificateField("Signature algorithm", certificate.SignatureAlgorithm.FriendlyName);
425 AddCertificateField("Valid from", certificate.NotBefore.ToString());
426 AddCertificateField("Valid to", certificate.NotAfter.ToString());
428 string pkey = BitConverter.ToString(certificate.PublicKey.EncodedKeyValue.RawData).Replace("-", " ");
429 AddCertificateField("Public key", certificate.PublicKey.Oid.FriendlyName + " (" + certificate.PublicKey.Key.KeySize + " bits)", pkey);
431 string thumbprint = PadText(certificate.Thumbprint);
432 AddCertificateField("Thumbprint", thumbprint);
434 tbcPages.SelectedTab = tabVerifyCertificate;
437 private void btnAddEndpoint_Click(object sender, EventArgs e)
439 EndpointInputBox eib = new EndpointInputBox();
441 if (eib.ShowDialog(this) == DialogResult.Cancel)
444 ListViewItem lvi = new ListViewItem();
445 lvi.Text = eib.txtInstanceName.Text;
447 if (eib.chkConnect.Checked) {
448 lvi.SubItems.Add(eib.txtHost.Text);
449 lvi.SubItems.Add(eib.txtPort.Text);
452 lvwEndpoints.Items.Add(lvi);
455 private void lvwEndpoints_SelectedIndexChanged(object sender, EventArgs e)
457 btnRemoveEndpoint.Enabled = lvwEndpoints.SelectedItems.Count > 0;
458 btnEditEndpoint.Enabled = lvwEndpoints.SelectedItems.Count > 0;
461 private void lvwX509Fields_SelectedIndexChanged(object sender, EventArgs e)
463 if (lvwX509Fields.SelectedItems.Count == 0)
466 ListViewItem lvi = lvwX509Fields.SelectedItems[0];
468 txtX509Field.Text = (string)lvi.Tag;
471 private void btnRemoveEndpoint_Click(object sender, EventArgs e)
473 while (lvwEndpoints.SelectedItems.Count > 0) {
474 lvwEndpoints.Items.Remove(lvwEndpoints.SelectedItems[0]);
478 private void chkRunServiceAsThisUser_CheckedChanged(object sender, EventArgs e)
480 txtUser.Enabled = !txtUser.Enabled;
481 if (!txtUser.Enabled)
482 txtUser.Text = Icinga2User;
485 private void btnEditEndpoint_Click(object sender, EventArgs e)
487 ListViewItem lvi = lvwEndpoints.SelectedItems[0];
488 EndpointInputBox eib = new EndpointInputBox();
490 eib.Text = "Edit Endpoint";
491 eib.txtInstanceName.Text = lvi.SubItems[0].Text;
493 if (lvi.SubItems.Count >= 2) {
494 eib.txtHost.Text = lvi.SubItems[1].Text;
495 eib.txtPort.Text = lvi.SubItems[2].Text;
496 eib.chkConnect.Checked = true;
499 if (eib.ShowDialog(this) == DialogResult.Cancel)
502 lvwEndpoints.Items.Remove(lvi);
504 ListViewItem lvi2 = new ListViewItem();
505 lvi2.Text = eib.txtInstanceName.Text;
507 if (eib.chkConnect.Checked) {
508 lvi2.SubItems.Add(eib.txtHost.Text);
509 lvi2.SubItems.Add(eib.txtPort.Text);
512 lvwEndpoints.Items.Add(lvi2);