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;
24 InitializeComponent();
26 txtInstanceName.Text = Icinga2InstanceName;
29 private void Warning(string message)
31 MessageBox.Show(this, message, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
34 private string Icinga2InstanceName
38 IPGlobalProperties props = IPGlobalProperties.GetIPGlobalProperties();
40 string fqdn = props.HostName;
42 if (props.DomainName != "")
43 fqdn += "." + props.DomainName;
49 private bool GetMasterHostPort(out string host, out string port)
51 foreach (ListViewItem lvi in lvwEndpoints.Items) {
52 if (lvi.SubItems.Count > 1) {
53 host = lvi.SubItems[1].Text;
54 port = lvi.SubItems[2].Text;
64 private void EnableFeature(string feature)
68 fp = File.Open(Program.Icinga2InstallDir + String.Format("\\etc\\icinga2\\features-enabled\\{0}.conf", feature), FileMode.Create);
69 using (StreamWriter sw = new StreamWriter(fp, Encoding.ASCII)) {
71 sw.Write(String.Format("include \"../features-available/{0}.conf\"\n", feature));
79 private void SetRetrievalStatus(int pct)
82 Invoke((MethodInvoker)delegate { SetRetrievalStatus(pct); });
86 prgRetrieveCertificate.Value = pct;
89 private void SetConfigureStatus(int pct, string message)
92 Invoke((MethodInvoker)delegate { SetConfigureStatus(pct, message); });
96 prgConfig.Value = pct;
97 lblConfigStatus.Text = message;
100 private void ShowErrorText(string text)
102 if (InvokeRequired) {
103 Invoke((MethodInvoker)delegate { ShowErrorText(text); });
107 txtError.Text = text;
108 tbcPages.SelectedTab = tabError;
111 private bool RunProcess(string filename, string arguments, out string output)
113 ProcessStartInfo psi = new ProcessStartInfo();
114 psi.FileName = filename;
115 psi.Arguments = arguments;
116 psi.CreateNoWindow = true;
117 psi.UseShellExecute = false;
118 psi.RedirectStandardOutput = true;
119 psi.RedirectStandardError = true;
123 using (Process proc = Process.Start(psi)) {
124 proc.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs args) {
125 result += args.Data + "\r\n";
127 proc.OutputDataReceived += delegate(object sender, DataReceivedEventArgs args) {
128 result += args.Data + "\r\n";
130 proc.BeginOutputReadLine();
131 proc.BeginErrorReadLine();
136 if (proc.ExitCode != 0)
143 private void VerifyCertificate(string host, string port)
145 SetRetrievalStatus(25);
147 string pathPrefix = Program.Icinga2InstallDir + "\\etc\\icinga2\\pki\\" + txtInstanceName.Text;
151 if (!File.Exists(pathPrefix + ".crt")) {
152 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
153 "pki new-cert --cn \"" + txtInstanceName.Text + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\"",
155 ShowErrorText(output);
160 SetRetrievalStatus(50);
162 _TrustedFile = Path.GetTempFileName();
164 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
165 "pki save-cert --host \"" + host + "\" --port \"" + port + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\" --trustedcert \"" + _TrustedFile + "\"",
167 ShowErrorText(output);
171 SetRetrievalStatus(100);
173 X509Certificate2 cert = new X509Certificate2(_TrustedFile);
174 Invoke((MethodInvoker)delegate { ShowCertificatePrompt(cert); });
177 private void ConfigureService()
179 SetConfigureStatus(0, "Updating configuration files...");
185 if (rdoNewMaster.Checked)
188 Invoke((MethodInvoker)delegate {
189 string master_host, master_port;
190 GetMasterHostPort(out master_host, out master_port);
192 args += " --master_host " + master_host + "," + master_port;
194 foreach (ListViewItem lvi in lvwEndpoints.Items) {
195 args += " --endpoint " + lvi.SubItems[0].Text;
197 if (lvi.SubItems.Count > 1)
198 args += "," + lvi.SubItems[1].Text + "," + lvi.SubItems[2].Text;
202 if (rdoListener.Checked)
203 args += " --listen ::," + txtListenerPort.Text;
205 if (chkAcceptConfig.Checked)
206 args += " --accept-config";
208 if (chkAcceptCommands.Checked)
209 args += " --accept-commands";
211 args += " --ticket " + txtTicket.Text;
212 args += " --trustedcert " + _TrustedFile;
213 args += " --cn " + txtInstanceName.Text;
214 args += " --zone " + txtInstanceName.Text;
216 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
219 ShowErrorText(output);
223 SetConfigureStatus(50, "Setting ACLs for the Icinga 2 directory...");
224 DirectoryInfo di = new DirectoryInfo(Program.Icinga2InstallDir);
225 DirectorySecurity ds = di.GetAccessControl();
226 FileSystemAccessRule rule = new FileSystemAccessRule("NT AUTHORITY\\NetworkService",
227 FileSystemRights.Modify,
228 InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
229 ds.AddAccessRule(rule);
230 di.SetAccessControl(ds);
232 SetConfigureStatus(75, "Installing the Icinga 2 service...");
234 RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
238 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
241 ShowErrorText(output);
245 if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
246 "--scm-install daemon",
248 ShowErrorText(output);
252 if (chkInstallNSCP.Checked)
254 SetConfigureStatus(85, "Waiting for NSClient++ installation to complete...");
256 Process proc = new Process();
257 proc.StartInfo.FileName = "msiexec.exe";
258 proc.StartInfo.Arguments = "/i \"" + Program.Icinga2InstallDir + "\\sbin\\NSCP.msi\"";
263 SetConfigureStatus(100, "Finished.");
268 private void FinishConfigure()
270 if (InvokeRequired) {
271 Invoke((MethodInvoker)FinishConfigure);
275 tbcPages.SelectedTab = tabFinish;
278 private void btnBack_Click(object sender, EventArgs e)
280 if (tbcPages.SelectedTab == tabError) {
281 tbcPages.SelectedIndex = 0;
287 if (tbcPages.SelectedTab == tabVerifyCertificate)
290 tbcPages.SelectedIndex -= offset;
293 private void btnNext_Click(object sender, EventArgs e)
295 if (tbcPages.SelectedTab == tabParameters) {
296 if (txtInstanceName.Text.Length == 0) {
297 Warning("Please enter an instance name.");
301 if (txtTicket.Text.Length == 0) {
302 Warning("Please enter an agent ticket.");
306 if (rdoNoMaster.Checked) {
307 if (lvwEndpoints.Items.Count == 0) {
308 Warning("You need to add at least one master endpoint.");
313 if (!GetMasterHostPort(out host, out port)) {
314 Warning("Please enter a remote host and port for at least one of your endpoints.");
319 if (rdoListener.Checked && (txtListenerPort.Text == "")) {
320 Warning("You need to specify a listener port.");
325 if (tbcPages.SelectedTab == tabFinish || tbcPages.SelectedTab == tabError)
328 tbcPages.SelectedIndex++;
331 private void btnCancel_Click(object sender, EventArgs e)
336 private void tbcPages_SelectedIndexChanged(object sender, EventArgs e)
340 btnBack.Enabled = (tbcPages.SelectedTab == tabVerifyCertificate || tbcPages.SelectedTab == tabError);
341 btnNext.Enabled = (tbcPages.SelectedTab == tabParameters || tbcPages.SelectedTab == tabVerifyCertificate || tbcPages.SelectedTab == tabFinish);
343 if (tbcPages.SelectedTab == tabFinish) {
344 btnNext.Text = "&Finish >";
345 btnCancel.Enabled = false;
348 if (tbcPages.SelectedTab == tabRetrieveCertificate) {
349 ListViewItem lvi = lvwEndpoints.Items[0];
351 string master_host, master_port;
352 GetMasterHostPort(out master_host, out master_port);
354 Thread thread = new Thread((ThreadStart)delegate { VerifyCertificate(master_host, master_port); });
358 /*if (tbcPages.SelectedTab == tabParameters &&
359 !File.Exists(Icinga2InstallDir + "\\etc\\icinga2\\pki\\agent\\agent.crt")) {
360 byte[] bytes = Convert.FromBase64String(txtBundle.Text);
361 MemoryStream ms = new MemoryStream(bytes);
362 GZipStream gz = new GZipStream(ms, CompressionMode.Decompress);
363 MemoryStream ms2 = new MemoryStream();
365 byte[] buffer = new byte[512];
367 while ((rc = gz.Read(buffer, 0, buffer.Length)) > 0)
368 ms2.Write(buffer, 0, rc);
370 TarReader tr = new TarReader(ms2);
371 tr.ReadToEnd(Icinga2InstallDir + "\\etc\\icinga2\\pki\\agent");
374 if (tbcPages.SelectedTab == tabConfigure) {
375 Thread thread = new Thread(ConfigureService);
380 private void RadioMaster_CheckedChanged(object sender, EventArgs e)
382 lvwEndpoints.Enabled = !rdoNewMaster.Checked;
383 btnAddEndpoint.Enabled = !rdoNewMaster.Checked;
384 btnRemoveEndpoint.Enabled = !rdoNewMaster.Checked && lvwEndpoints.SelectedItems.Count > 0;
387 private void RadioListener_CheckedChanged(object sender, EventArgs e)
389 txtListenerPort.Enabled = rdoListener.Checked;
392 private void AddCertificateField(string name, string shortValue, string longValue = null)
394 ListViewItem lvi = new ListViewItem();
396 lvi.SubItems.Add(shortValue);
397 if (longValue == null)
398 longValue = shortValue;
400 lvwX509Fields.Items.Add(lvi);
403 private string PadText(string input)
407 for (int i = 0; i < input.Length; i += 2) {
412 if (input.Length - i < 2)
413 len = input.Length - i;
414 output += input.Substring(i, len);
420 private void ShowCertificatePrompt(X509Certificate2 certificate)
422 txtX509Issuer.Text = certificate.Issuer;
423 txtX509Subject.Text = certificate.Subject;
425 lvwX509Fields.Items.Clear();
427 AddCertificateField("Version", "V" + certificate.Version.ToString());
428 AddCertificateField("Serial number", certificate.SerialNumber);
429 AddCertificateField("Signature algorithm", certificate.SignatureAlgorithm.FriendlyName);
430 AddCertificateField("Valid from", certificate.NotBefore.ToString());
431 AddCertificateField("Valid to", certificate.NotAfter.ToString());
433 string pkey = BitConverter.ToString(certificate.PublicKey.EncodedKeyValue.RawData).Replace("-", " ");
434 AddCertificateField("Public key", certificate.PublicKey.Oid.FriendlyName + " (" + certificate.PublicKey.Key.KeySize + " bits)", pkey);
436 string thumbprint = PadText(certificate.Thumbprint);
437 AddCertificateField("Thumbprint", thumbprint);
439 tbcPages.SelectedTab = tabVerifyCertificate;
442 private void btnAddEndpoint_Click(object sender, EventArgs e)
444 EndpointInputBox eib = new EndpointInputBox();
446 if (eib.ShowDialog(this) == DialogResult.Cancel)
449 ListViewItem lvi = new ListViewItem();
450 lvi.Text = eib.txtInstanceName.Text;
452 if (eib.chkConnect.Checked) {
453 lvi.SubItems.Add(eib.txtHost.Text);
454 lvi.SubItems.Add(eib.txtPort.Text);
457 lvwEndpoints.Items.Add(lvi);
460 private void lvwEndpoints_SelectedIndexChanged(object sender, EventArgs e)
462 btnRemoveEndpoint.Enabled = lvwEndpoints.SelectedItems.Count > 0;
465 private void lvwX509Fields_SelectedIndexChanged(object sender, EventArgs e)
467 if (lvwX509Fields.SelectedItems.Count == 0)
470 ListViewItem lvi = lvwX509Fields.SelectedItems[0];
472 txtX509Field.Text = (string)lvi.Tag;
475 private void btnRemoveEndpoint_Click(object sender, EventArgs e)
477 while (lvwEndpoints.SelectedItems.Count > 0) {
478 lvwEndpoints.Items.Remove(lvwEndpoints.SelectedItems[0]);