Contains the information that the FOG Server responds with
        //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);
        }
Beispiel #2
0
        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;
        }
Beispiel #3
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;
        }
Beispiel #4
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;
        }
Beispiel #5
0
 public void UnRegisterComputer(Response response)
 {
     throw new NotImplementedException();
 }
Beispiel #6
0
        public void ParseDataArray()
        {
            /**
             * Ensure that response arrays can be parsed
             */

            const string msg = "#!ok\n" +
                               "#obj0=foo\n" +
                               "#obj1=bar\n" +
                               "#obj2=22!";

            var response = new Response(msg, false);
            var objArray = response.GetList("#obj", false);

            Assert.AreEqual(3, objArray.Count);
            Assert.AreEqual("foo", objArray[0]);
            Assert.AreEqual("bar", objArray[1]);
            Assert.AreEqual("22!", objArray[2]);
        }
Beispiel #7
0
        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";
        }
        //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);
            }
        }
        //Remove the host from active directory
        private void UnRegisterComputer(Response response)
        {
            Log.Entry(Name, "Removing host from active directory");

            if (!response.IsFieldValid("#ADUser") || !response.IsFieldValid("#ADPass"))
            {
                Log.Error(Name, "Required Domain information is missing");
                return;
            }

            try
            {
                _instance.UnRegisterComputer(response);
            }
            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;
            }
        }
Beispiel #15
0
        /// <summary>
        ///     Loop through all the modules until an update or shutdown is pending
        /// </summary>
        protected virtual void ModuleLooper()
        {
            Log.NewLine();

            // Only run the service if there isn't a shutdown or update pending
            // However, keep looping if a power operation is only Requested as
            // the request may be aborted
            while (!Power.ShuttingDown && !Power.Updating)
            {
                LoopData = GetLoopData() ?? new Response();
                // Stop looping as soon as a shutdown or update pending
                foreach (var module in GetModules().TakeWhile(module => !Power.ShuttingDown && !Power.Updating))
                {
                    // Entry file formatting
                    Log.NewLine();
                    Log.PaddedHeader(module.GetName());
                    Log.Entry("Client-Info", $"Client Version: {Settings.Get("Version")}");
                    Log.Entry("Client-Info", $"Client OS:      {Settings.OS}");
                    Log.Entry("Client-Info", $"Server Version: {Settings.Get("ServerVersion")}");

                    try
                    {
                        var subResponse = LoopData.GetSubResponse(module.GetName().ToLower());
                        if (subResponse == null)
                            continue;

                        module.Start(subResponse);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(Name, "Unable to run module");
                        Log.Error(Name, ex);
                    }

                    // Entry file formatting
                    Log.Divider();
                    Log.NewLine();
                }

                // Skip checking for sleep time if there is a shutdown or update pending
                if (Power.ShuttingDown || Power.Updating) break;

                // Once all modules have been run, sleep for the set time
                var sleepTime = GetSleepTime() ?? DEFAULT_SLEEP_TIME;
                Log.Entry(Name, $"Sleeping for {sleepTime} seconds");
                Thread.Sleep(sleepTime * 1000);
            }
        }
Beispiel #16
0
        //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);
            }
        }