public bool RegisterComputer(Response response) { var domain = response.GetField("#ADDom"); var ou = response.GetField("#ADOU"); var adadmin = response.GetField("#ADUser"); var adpass = response.GetField("#ADPass"); var returnCode = ProcessHandler.Run("/bin/bash", $"{Path.Combine(Settings.Location, "/Scripts/Mac/osxADBind.sh")} {domain} {ou} {adadmin} {adpass}"); return returnCode == 0; }
public bool RegisterComputer(Response response) { // Check if the host is already part of the set domain by checking server IPs try { using (var domain = Domain.GetComputerDomain()) { var currentIP = Dns.GetHostAddresses(domain.Name); var targetIP = Dns.GetHostAddresses(response.GetField("#ADDom")); if (currentIP.Intersect(targetIP).Any()) { Log.Entry(Name, "Host is already joined to target domain"); return false; } } } catch (Exception) { // ignored } // Attempt to join the domain var returnCode = DomainWrapper(response, true, (JoinOptions.NetsetupJoinDomain | JoinOptions.NetsetupAcctCreate)); switch (returnCode) { case 2224: returnCode = DomainWrapper(response, true, JoinOptions.NetsetupJoinDomain); break; case 2: case 50: case 1355: returnCode = DomainWrapper(response, false, (JoinOptions.NetsetupJoinDomain | JoinOptions.NetsetupAcctCreate)); break; } // Entry the results Log.Entry(Name, $"{(_returnCodes.ContainsKey(returnCode) ? $"{_returnCodes[returnCode]}, code = " : "Unknown Return Code: ")} {returnCode}"); return returnCode == 0; }
//Get how long a user must be inactive before logging them out private int GetTimeOut(Response taskResponse) { try { var timeOut = int.Parse(taskResponse.GetField("#time")); if (timeOut >= _minimumTime) return timeOut; Log.Entry(Name, "Time set is less than 1 minute"); } catch (Exception ex) { Log.Error(Name, "Unable to parse time set"); Log.Error(Name, ex); } return 0; }
public static Printer PrinterFactory(Response printerData) { if (printerData.GetField("#type").Equals("iPrint")) return new iPrintPrinter(printerData.GetField("#name"), printerData.GetField("#ip"), printerData.GetField("#port"), printerData.GetField("#default").Equals("1")); if (printerData.GetField("#type").Equals("Network")) return new NetworkPrinter(printerData.GetField("#name"), printerData.GetField("#ip"), printerData.GetField("#port"), printerData.GetField("#default").Equals("1")); if (printerData.GetField("#type").Equals("Local")) return new LocalPrinter(printerData.GetField("#name"), printerData.GetField("#file"), printerData.GetField("#port"), printerData.GetField("#ip"), printerData.GetField("#model"), printerData.GetField("#default").Equals("1")); return null; }
public void ParseResponse() { /** * Ensure that responses can be parsed */ const string msg = "#!ok\n" + "#Foo=bar\n" + "#Empty=\n" + "#-X=Special"; var response = new Response(msg, false); Assert.IsFalse(response.Error); Assert.AreEqual("bar", response.GetField("#Foo")); Assert.IsEmpty(response.GetField("#Empty")); Assert.AreEqual("Special", response.GetField("#-X")); Assert.IsNullOrEmpty(response.GetField("#NON_EXISTENT")); }
//Create a proccess to run the snapin with private static Process GenerateProcess(Response taskResponse, string snapinPath) { var process = new Process { StartInfo = { CreateNoWindow = true, UseShellExecute = false, WindowStyle = ProcessWindowStyle.Hidden } }; //Check if the snapin run with field was specified if (!taskResponse.GetField("SNAPINRUNWITH").Equals("")) { process.StartInfo.FileName = Environment.ExpandEnvironmentVariables( taskResponse.GetField("SNAPINRUNWITH")); process.StartInfo.Arguments = Environment.ExpandEnvironmentVariables( $"{taskResponse.GetField("SNAPINRUNWITHARGS").Trim()} " + $"\"{snapinPath.Trim()}\" " + $"{Environment.ExpandEnvironmentVariables(taskResponse.GetField("SNAPINARGS"))}" .Trim()); } else { process.StartInfo.FileName = Environment.ExpandEnvironmentVariables(snapinPath); process.StartInfo.Arguments = Environment.ExpandEnvironmentVariables( taskResponse.GetField("SNAPINARGS")); } return process; }
//Execute the snapin once it has been downloaded private string StartSnapin(Response taskResponse, string snapinPath) { Notification.Emit( "Installing " + taskResponse.GetField("SNAPINNAME"), "Please do not shutdown until this is completed", $"snapin-{taskResponse.GetField("SNAPINNAME")}", true); var process = GenerateProcess(taskResponse, snapinPath); try { Log.Entry(Name, "Starting snapin..."); process.Start(); process.WaitForExit(); Log.Entry(Name, "Snapin finished"); Log.Entry(Name, "Return Code: " + process.ExitCode); Notification.Emit( taskResponse.GetField("SNAPINNAME") + " Installed", "Installation has finished and is now ready for use", $"snapin-{taskResponse.GetField("SNAPINNAME")}", true); return process.ExitCode.ToString(); } catch (Exception ex) { Log.Error(Name, "Could not start snapin"); Log.Error(Name, ex); } return "-1"; }
//Rename the computer and remove it from active directory private void RenameComputer(Response response) { Log.Entry(Name, "Checking Hostname"); if (!response.IsFieldValid("#hostname")) { Log.Error(Name, "Hostname is not specified"); return; } if (Environment.MachineName.ToLower().Equals(response.GetField("#hostname").ToLower())) { Log.Entry(Name, "Hostname is correct"); return; } //First unjoin it from active directory UnRegisterComputer(response); if (Power.ShuttingDown || Power.Requested) return; Log.Entry(Name, $"Renaming host to {response.GetField("#hostname")}"); try { _instance.RenameComputer(response.GetField("#hostname")); } catch (Exception ex) { Log.Error(Name, ex); } Power.Restart(Settings.Get("Company") + " needs to rename your computer", Power.ShutdownOptions.Delay); }
//Active a computer with a product key private void ActivateComputer(Response response) { if (!response.IsFieldValid("#Key")) return; try { _instance.ActivateComputer(response.GetField("#Key")); } catch (Exception ex) { Log.Error(Name, ex); } }
//Add a host to active directory private void RegisterComputer(Response response) { if (response.GetField("#AD") != "1") return; if (!response.IsFieldValid("#ADDom") || !response.IsFieldValid("#ADUser") || !response.IsFieldValid("#ADPass")) { Log.Error(Name, "Required Domain Joining information is missing"); return; } try { if (_instance.RegisterComputer(response)) Power.Restart("Host joined to Active Directory, restart required", Power.ShutdownOptions.Delay); } catch (Exception ex) { Log.Error(Name, ex); } }
protected override Response GetLoopData() { try { _config = Communication.GetResponse("/management/index.php?sub=requestClientInfo&configure"); var promptTime = SandBoxParse(_config, "promptTime", MIN_PROMPT, MAX_PROMPT, MIN_PROMPT); Settings.Set("PromptTime", promptTime.ToString()); Settings.Set("Company", _config.GetField("company")); Settings.Set("Color", _config.GetField("color")); Settings.Set("BannerHash", _config.GetField("bannerHash").ToUpper()); var bannerURL = _config.GetField("bannerURL"); if (!string.IsNullOrEmpty(bannerURL)) { Communication.DownloadFile(bannerURL, Path.Combine(Settings.Location, "banner.png")); } } catch (Exception ex) { Log.Error(Name, "Unable to get config data"); Log.Error(Name, ex); _config = null; } try { var response = Communication.GetResponse("/management/index.php?sub=requestClientInfo", true); // Construct the clientupdater data regardless of encryption var srvClientVersion = Communication.GetRawResponse("/service/getversion.php?clientver"); var srvVersion = Communication.GetRawResponse("/service/getversion.php"); var clientUpdaterData = new JObject { ["version"] = srvClientVersion }; response.Data["clientupdater"] = clientUpdaterData; Log.NewLine(); Log.Entry(Name, "Creating user agent cache"); try { Settings.Set("ServerVersion", srvVersion); var alo = response.GetSubResponse("autologout"); Settings.Set("ALOTime", (alo == null) ? "0" : alo.GetField("time")); var pDefault = response.GetSubResponse("printermanager"); Settings.Set("DefaultPrinter", (pDefault == null) ? "" : pDefault.GetField("default")); var display = response.GetSubResponse("displaymanager"); Settings.Set("DisplayX", (display == null || display.Error) ? "" : display.GetField("x")); Settings.Set("DisplayY", (display == null || display.Error) ? "" : display.GetField("y")); Settings.Set("DisplayR", (display == null || display.Error) ? "" : display.GetField("r")); } catch (Exception ex) { Log.Error(Name, "Unable to set user agent cache"); Log.Error(Name, ex); } return response; } catch (Exception ex) { Log.Error(Name, "Unable to get cycle data"); Log.Error(Name, ex); } return new Response(); }
private int SandBoxParse(Response response, string setting, int min, int max, int fallback) { if (response.IsFieldValid(setting)) { int value; var success = int.TryParse(response.GetField(setting), out value); if (success && value >= min && value <= max) { return value; } else { Log.Error(Name, $"Invalid {setting}, using default"); return fallback; } } else { Log.Error(Name, $"Invalid {setting}, using default"); return fallback; } }
private static int DomainWrapper(Response response, bool ou, JoinOptions options) { return NetJoinDomain(null, response.GetField("#ADDom"), ou ? response.GetField("#ADOU") : null, response.GetField("#ADUser"), response.GetField("#ADPass"), options); }
public void UnRegisterComputer(Response response) { try { var returnCode = NetUnjoinDomain(null, response.GetField("#ADUser"), response.GetField("#ADPass"), UnJoinOptions.NetsetupAccountDelete); Log.Entry(Name, $"{(_returnCodes.ContainsKey(returnCode) ? $"{_returnCodes[returnCode]}, code = " : "Unknown Return Code: ")} {returnCode}"); if (returnCode.Equals(0)) Power.Restart("Host left active directory, restart needed", Power.ShutdownOptions.Delay); } catch (Exception ex) { Log.Error(Name, ex); } }
//Active a computer with a product key private void ActivateComputer(Response response) { if (!response.IsFieldValid("#Key")) return; Log.Entry(Name, "Activing host with product key"); try { _instance.ActivateComputer(response.GetField("#Key")); } catch (Exception ex) { Log.Error(Name, ex); } }