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.Trim();
58 port = lvi.SubItems[2].Text.Trim();
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); });
182 } catch (Exception e) {
183 ShowErrorText("Failed to receive certificate: " + e.Message);
187 private void ConfigureService()
189 SetConfigureStatus(0, "Updating configuration files...");
195 Invoke((MethodInvoker)delegate
197 string master_host, master_port;
198 GetMasterHostPort(out master_host, out master_port);
200 args += " --master_host " + master_host + "," + master_port;
202 foreach (ListViewItem lvi in lvwEndpoints.Items) {
203 args += " --endpoint " + lvi.SubItems[0].Text.Trim();
205 if (lvi.SubItems.Count > 1)
206 args += "," + lvi.SubItems[1].Text.Trim() + "," + lvi.SubItems[2].Text.Trim();
210 if (rdoListener.Checked)
211 args += " --listen ::," + txtListenerPort.Text.Trim();
213 if (chkAcceptConfig.Checked)
214 args += " --accept-config";
216 if (chkAcceptCommands.Checked)
217 args += " --accept-commands";
219 string ticket = txtTicket.Text.Trim();
221 if (ticket.Length > 0)
222 args += " --ticket \"" + ticket + "\"";
224 args += " --trustedcert \"" + _TrustedFile + "\"";
225 args += " --cn \"" + txtInstanceName.Text.Trim() + "\"";
226 args += " --zone \"" + txtInstanceName.Text.Trim() + "\"";
228 foreach (ListViewItem lvi in lvwGlobalZones.Items) {
229 args += " --global_zones " + lvi.SubItems[0].Text.Trim();
232 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
235 ShowErrorText("Running command 'icinga2.exe " + "node setup" + args + "' produced the following output:\n" + output);
239 SetConfigureStatus(50, "Setting ACLs for the Icinga 2 directory...");
241 string serviceUser = txtUser.Text.Trim();
243 DirectoryInfo di = new DirectoryInfo(Program.Icinga2InstallDir);
244 DirectorySecurity ds = di.GetAccessControl();
245 FileSystemAccessRule rule = new FileSystemAccessRule(serviceUser,
246 FileSystemRights.Modify,
247 InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
249 ds.AddAccessRule(rule);
250 di.SetAccessControl(ds);
251 } catch (System.Security.Principal.IdentityNotMappedException) {
252 ShowErrorText("Could not set ACLs for user \"" + serviceUser + "\". Identitiy is not mapped.\n");
256 SetConfigureStatus(75, "Installing the Icinga 2 service...");
258 RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
262 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
265 ShowErrorText("Running command 'icinga2.exe daemon --validate' produced the following output:\n" + output);
269 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
270 "--scm-install --scm-user \"" + serviceUser + "\" daemon",
272 ShowErrorText("\nRunning command 'icinga2.exe --scm-install --scm-user \"" +
273 serviceUser + "\" daemon' produced the following output:\n" + output);
277 if (chkInstallNSCP.Checked) {
278 SetConfigureStatus(85, "Waiting for NSClient++ installation to complete...");
280 Process proc = new Process();
281 proc.StartInfo.FileName = "msiexec.exe";
282 proc.StartInfo.Arguments = "/i \"" + Program.Icinga2InstallDir + "\\sbin\\NSCP.msi\"";
287 SetConfigureStatus(100, "Finished.");
289 // Override the completed text
290 lblSetupCompleted.Text = "The Icinga 2 Windows client was set up successfully.";
292 // Add a note for the user for ticket-less signing
293 if (ticket.Length == 0) {
294 lblSetupCompleted.Text += "\n\nTicket was not specified. Please sign the certificate request on the Icinga 2 master node (requires v2.8+).";
300 private void FinishConfigure()
302 if (InvokeRequired) {
303 Invoke((MethodInvoker)FinishConfigure);
307 tbcPages.SelectedTab = tabFinish;
310 private void btnBack_Click(object sender, EventArgs e)
312 if (tbcPages.SelectedTab == tabError) {
313 tbcPages.SelectedIndex = 0;
319 if (tbcPages.SelectedTab == tabVerifyCertificate)
322 tbcPages.SelectedIndex -= offset;
325 private void btnNext_Click(object sender, EventArgs e)
327 if (tbcPages.SelectedTab == tabParameters) {
328 if (txtInstanceName.Text.Length == 0) {
329 Warning("Please enter an instance name.");
333 if (lvwEndpoints.Items.Count == 0) {
334 Warning("You need to add at least one master/satellite endpoint.");
339 if (!GetMasterHostPort(out host, out port)) {
340 Warning("Please enter a remote host and port for at least one of your endpoints.");
344 if (rdoListener.Checked && (txtListenerPort.Text == "")) {
345 Warning("You need to specify a listener port.");
349 if (txtUser.Text.Length == 0) {
350 Warning("Icinga 2 service user may not be empty.");
355 if (tbcPages.SelectedTab == tabFinish || tbcPages.SelectedTab == tabError)
358 tbcPages.SelectedIndex++;
361 private void btnCancel_Click(object sender, EventArgs e)
366 private void tbcPages_SelectedIndexChanged(object sender, EventArgs e)
370 btnBack.Enabled = (tbcPages.SelectedTab == tabVerifyCertificate || tbcPages.SelectedTab == tabError);
371 btnNext.Enabled = (tbcPages.SelectedTab == tabParameters || tbcPages.SelectedTab == tabVerifyCertificate || tbcPages.SelectedTab == tabFinish);
373 if (tbcPages.SelectedTab == tabFinish) {
374 btnNext.Text = "&Finish >";
375 btnCancel.Enabled = false;
378 if (tbcPages.SelectedTab == tabRetrieveCertificate) {
379 ListViewItem lvi = lvwEndpoints.Items[0];
381 string master_host, master_port;
382 GetMasterHostPort(out master_host, out master_port);
384 Thread thread = new Thread((ThreadStart)delegate { VerifyCertificate(master_host, master_port); });
388 if (tbcPages.SelectedTab == tabConfigure) {
389 Thread thread = new Thread(ConfigureService);
394 private void RadioListener_CheckedChanged(object sender, EventArgs e)
396 txtListenerPort.Enabled = rdoListener.Checked;
399 private void AddCertificateField(string name, string shortValue, string longValue = null)
401 ListViewItem lvi = new ListViewItem();
403 lvi.SubItems.Add(shortValue);
404 if (longValue == null)
405 longValue = shortValue;
407 lvwX509Fields.Items.Add(lvi);
410 private string PadText(string input)
414 for (int i = 0; i < input.Length; i += 2) {
419 if (input.Length - i < 2)
420 len = input.Length - i;
421 output += input.Substring(i, len);
427 private void ShowCertificatePrompt(X509Certificate2 certificate)
429 txtX509Issuer.Text = certificate.Issuer;
430 txtX509Subject.Text = certificate.Subject;
432 lvwX509Fields.Items.Clear();
434 AddCertificateField("Version", "V" + certificate.Version.ToString());
435 AddCertificateField("Serial number", certificate.SerialNumber);
436 AddCertificateField("Signature algorithm", certificate.SignatureAlgorithm.FriendlyName);
437 AddCertificateField("Valid from", certificate.NotBefore.ToString());
438 AddCertificateField("Valid to", certificate.NotAfter.ToString());
440 string pkey = BitConverter.ToString(certificate.PublicKey.EncodedKeyValue.RawData).Replace("-", " ");
441 AddCertificateField("Public key", certificate.PublicKey.Oid.FriendlyName + " (" + certificate.PublicKey.Key.KeySize + " bits)", pkey);
443 string thumbprint = PadText(certificate.Thumbprint);
444 AddCertificateField("Thumbprint", thumbprint);
446 tbcPages.SelectedTab = tabVerifyCertificate;
449 private void btnAddEndpoint_Click(object sender, EventArgs e)
451 EndpointInputBox eib = new EndpointInputBox();
453 if (eib.ShowDialog(this) == DialogResult.Cancel)
456 ListViewItem lvi = new ListViewItem();
457 lvi.Text = eib.txtInstanceName.Text;
459 if (eib.chkConnect.Checked) {
460 lvi.SubItems.Add(eib.txtHost.Text);
461 lvi.SubItems.Add(eib.txtPort.Text);
464 lvwEndpoints.Items.Add(lvi);
467 private void lvwEndpoints_SelectedIndexChanged(object sender, EventArgs e)
469 btnRemoveEndpoint.Enabled = lvwEndpoints.SelectedItems.Count > 0;
470 btnEditEndpoint.Enabled = lvwEndpoints.SelectedItems.Count > 0;
473 private void lvwX509Fields_SelectedIndexChanged(object sender, EventArgs e)
475 if (lvwX509Fields.SelectedItems.Count == 0)
478 ListViewItem lvi = lvwX509Fields.SelectedItems[0];
480 txtX509Field.Text = Convert.ToString(lvi.Tag);
483 private void btnRemoveEndpoint_Click(object sender, EventArgs e)
485 while (lvwEndpoints.SelectedItems.Count > 0) {
486 lvwEndpoints.Items.Remove(lvwEndpoints.SelectedItems[0]);
490 private void chkRunServiceAsThisUser_CheckedChanged(object sender, EventArgs e)
492 txtUser.Enabled = !txtUser.Enabled;
493 if (!txtUser.Enabled)
494 txtUser.Text = Icinga2User;
497 private void btnEditEndpoint_Click(object sender, EventArgs e)
499 ListViewItem lvi = lvwEndpoints.SelectedItems[0];
500 EndpointInputBox eib = new EndpointInputBox();
502 eib.Text = "Edit Endpoint";
503 eib.txtInstanceName.Text = lvi.SubItems[0].Text;
505 if (lvi.SubItems.Count >= 2) {
506 eib.txtHost.Text = lvi.SubItems[1].Text;
507 eib.txtPort.Text = lvi.SubItems[2].Text;
508 eib.chkConnect.Checked = true;
511 if (eib.ShowDialog(this) == DialogResult.Cancel)
514 lvwEndpoints.Items.Remove(lvi);
516 ListViewItem lvi2 = new ListViewItem();
517 lvi2.Text = eib.txtInstanceName.Text;
519 if (eib.chkConnect.Checked) {
520 lvi2.SubItems.Add(eib.txtHost.Text);
521 lvi2.SubItems.Add(eib.txtPort.Text);
524 lvwEndpoints.Items.Add(lvi2);
527 private void btnAddGlobalZone_Click(object sender, EventArgs e)
529 GlobalZonesInputBox gzib = new GlobalZonesInputBox(lvwGlobalZones.Items);
531 if (gzib.ShowDialog(this) == DialogResult.Cancel)
534 ListViewItem lvi = new ListViewItem();
535 lvi.Text = gzib.txtGlobalZoneName.Text;
537 lvwGlobalZones.Items.Add(lvi);
540 private void btnRemoveGlobalZone_Click(object sender, EventArgs e)
542 while (lvwGlobalZones.SelectedItems.Count > 0) {
543 lvwGlobalZones.Items.Remove(lvwGlobalZones.SelectedItems[0]);
547 private void lvwGlobalZones_SelectedIndexChanged(object sender, EventArgs e)
549 btnEditGlobalZone.Enabled = lvwGlobalZones.SelectedItems.Count > 0;
550 btnRemoveGlobalZone.Enabled = lvwGlobalZones.SelectedItems.Count > 0;
553 private void btnEditGlobalZone_Click(object sender, EventArgs e)
555 ListViewItem lvi = lvwGlobalZones.SelectedItems[0];
556 GlobalZonesInputBox gzib = new GlobalZonesInputBox(lvwGlobalZones.Items);
558 gzib.Text = "Edit Global Zone";
559 gzib.txtGlobalZoneName.Text = lvi.SubItems[0].Text;
561 if (gzib.ShowDialog(this) == DialogResult.Cancel)
564 lvwGlobalZones.Items.Remove(lvi);
566 ListViewItem lvi2 = new ListViewItem();
567 lvi2.Text = gzib.txtGlobalZoneName.Text;
569 lvwGlobalZones.Items.Add(lvi2);