Skip to content

Commit

Permalink
Add option to choose icinga2s user
Browse files Browse the repository at this point in the history
Adds the --scm-user option and a check box and text field in the Agent

fixes #9119
  • Loading branch information
Crunsher committed Oct 25, 2016
1 parent c0bc156 commit 4b61aee
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 82 deletions.
85 changes: 51 additions & 34 deletions agent/windows-setup-agent/Program.cs
Expand Up @@ -9,52 +9,69 @@ namespace Icinga
{
static class Program
{
[DllImport("msi.dll", SetLastError = true)]
static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf);
[DllImport("msi.dll", SetLastError = true)]
static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf);

[DllImport("msi.dll", CharSet = CharSet.Unicode)]
static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len);
[DllImport("msi.dll", CharSet = CharSet.Unicode)]
static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len);

public static string Icinga2InstallDir
public static string Icinga2InstallDir
{
get
{
StringBuilder szProduct;
StringBuilder szProduct;

for (int index = 0; ; index++) {
szProduct = new StringBuilder(39);
if (MsiEnumProducts(index, szProduct) != 0)
break;
for (int index = 0; ; index++) {
szProduct = new StringBuilder(39);
if (MsiEnumProducts(index, szProduct) != 0)
break;

int cbName = 128;
StringBuilder szName = new StringBuilder(cbName);
int cbName = 128;
StringBuilder szName = new StringBuilder(cbName);

if (MsiGetProductInfo(szProduct.ToString(), "ProductName", szName, ref cbName) != 0)
continue;
if (MsiGetProductInfo(szProduct.ToString(), "ProductName", szName, ref cbName) != 0)
continue;

if (szName.ToString() != "Icinga 2")
continue;
if (szName.ToString() != "Icinga 2")
continue;

int cbLocation = 1024;
StringBuilder szLocation = new StringBuilder(cbLocation);
if (MsiGetProductInfo(szProduct.ToString(), "InstallLocation", szLocation, ref cbLocation) == 0)
return szLocation.ToString();
}
int cbLocation = 1024;
StringBuilder szLocation = new StringBuilder(cbLocation);
if (MsiGetProductInfo(szProduct.ToString(), "InstallLocation", szLocation, ref cbLocation) == 0)
return szLocation.ToString();
}

return "";
return "";
}
}

public static string Icinga2DataDir
{
get
{
return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\icinga2";
}
}
public static string Icinga2DataDir
{
get
{
return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\icinga2";
}
}

public static string Icinga2User
{
get
{
if (!File.Exists(Icinga2DataDir + "\\etc\\icinga2\\user"))
return "NT AUTHORITY\\NetworkService";
System.IO.StreamReader file = new System.IO.StreamReader(Icinga2DataDir + "\\etc\\icinga2\\user");
string line = file.ReadLine();
file.Close();

if (line != null)
return line;
else
return "NT AUTHORITY\\NetworkService";
}
}


public static void FatalError(Form owner, string message)
public static void FatalError(Form owner, string message)
{
MessageBox.Show(owner, message, "Icinga 2 Setup Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
Expand All @@ -71,10 +88,10 @@ static void Main()

string installDir = Program.Icinga2InstallDir;

if (installDir == "") {
FatalError(null, "Icinga 2 does not seem to be installed properly.");
return;
}
if (installDir == "") {
FatalError(null, "Icinga 2 does not seem to be installed properly.");
return;
}

Form form;

Expand Down
74 changes: 50 additions & 24 deletions agent/windows-setup-agent/SetupWizard.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 38 additions & 14 deletions agent/windows-setup-agent/SetupWizard.cs
Expand Up @@ -18,12 +18,16 @@ namespace Icinga
public partial class SetupWizard : Form
{
private string _TrustedFile;
private string Icinga2User;

public SetupWizard()
{
InitializeComponent();

txtInstanceName.Text = Icinga2InstanceName;

Icinga2User = Program.Icinga2User;
txtUser.Text = Icinga2User;
}

private void Warning(string message)
Expand Down Expand Up @@ -121,10 +125,12 @@ private bool RunProcess(string filename, string arguments, out string output)
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();
Expand Down Expand Up @@ -186,15 +192,16 @@ private void ConfigureService()
if (rdoNewMaster.Checked)
args += " --master";

Invoke((MethodInvoker)delegate {
Invoke((MethodInvoker)delegate
{
string master_host, master_port;
GetMasterHostPort(out master_host, out master_port);

args += " --master_host " + master_host + "," + master_port;

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;
}
Expand Down Expand Up @@ -224,11 +231,16 @@ private void ConfigureService()
SetConfigureStatus(50, "Setting ACLs for the Icinga 2 directory...");
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...");

Expand All @@ -244,14 +256,14 @@ private void ConfigureService()
}

if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
"--scm-install daemon",
"--scm-install --scm-user \"" + txtUser.Text + "\" daemon",
out output)) {
ShowErrorText("Running command 'icinga2.exe daemon --scm-install daemon' produced the following output:\n" + output);
ShowErrorText("\nRunning command 'icinga2.exe --scm-install --scm-user \"" +
txtUser.Text + "\" daemon' produced the following output:\n" + output);
return;
}

if (chkInstallNSCP.Checked)
{
if (chkInstallNSCP.Checked) {
SetConfigureStatus(85, "Waiting for NSClient++ installation to complete...");

Process proc = new Process();
Expand Down Expand Up @@ -321,6 +333,11 @@ private void btnNext_Click(object sender, EventArgs e)
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)
Expand Down Expand Up @@ -356,7 +373,7 @@ private void tbcPages_SelectedIndexChanged(object sender, EventArgs e)
thread.Start();
}

/*if (tbcPages.SelectedTab == tabParameters &&
/*if (tbcPages.SelectedTab == tabParameters &&
!File.Exists(Icinga2DataDir + "\\etc\\icinga2\\pki\\agent\\agent.crt")) {
byte[] bytes = Convert.FromBase64String(txtBundle.Text);
MemoryStream ms = new MemoryStream(bytes);
Expand All @@ -372,7 +389,7 @@ private void tbcPages_SelectedIndexChanged(object sender, EventArgs e)
tr.ReadToEnd(Icinga2DataDir + "\\etc\\icinga2\\pki\\agent");
}*/

if (tbcPages.SelectedTab == tabConfigure) {
if (tbcPages.SelectedTab == tabConfigure) {
Thread thread = new Thread(ConfigureService);
thread.Start();
}
Expand Down Expand Up @@ -478,6 +495,13 @@ private void btnRemoveEndpoint_Click(object sender, EventArgs e)
while (lvwEndpoints.SelectedItems.Count > 0) {
lvwEndpoints.Items.Remove(lvwEndpoints.SelectedItems[0]);
}
}
}

private void chkDifferentUser_CheckedChanged(object sender, EventArgs e)
{
txtUser.ReadOnly = !txtUser.ReadOnly;
if (txtUser.ReadOnly)
txtUser.Text = Icinga2User;
}
}
}

0 comments on commit 4b61aee

Please sign in to comment.