Exemplo n.º 1
0
        private static Printer PrinterFactory(Response printerData)
        {
            if (PrinterExists(printerData)) return null;

            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;
        }
Exemplo n.º 2
0
 private static bool PrinterExists(Response printerData )
 {
     var printerQuery = new ManagementObjectSearcher("SELECT * from Win32_Printer");
     return (from ManagementBaseObject printer in printerQuery.Get() select printer.GetPropertyValue("Name"))
         .Contains(printerData.GetField("#name"));
 }
Exemplo n.º 3
0
        //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"));

                process.StartInfo.Arguments = Environment.ExpandEnvironmentVariables(
                    string.Format("{0} \"{1} \"{2}", taskResponse.GetField("SNAPINRUNWITHARGS"),
                        snapinPath, Environment.ExpandEnvironmentVariables(taskResponse.GetField("SNAPINARGS"))));
            }
            else
            {
                process.StartInfo.FileName = Environment.ExpandEnvironmentVariables(snapinPath);

                process.StartInfo.Arguments = Environment.ExpandEnvironmentVariables(
                    taskResponse.GetField("SNAPINARGS"));
            }

            return process;
        }
Exemplo n.º 4
0
        //Execute the snapin once it has been downloaded
        private string StartSnapin(Response taskResponse, string snapinPath)
        {
            var notification = new Notification(taskResponse.GetField("SNAPINNAME"),
                string.Format("FOG is installing {0}", taskResponse.GetField("SNAPINNAME")), 10);

            Bus.Emit(Bus.Channel.Notification, notification.GetJson(), 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 = new Notification(
                    string.Format("Finished {0}", taskResponse.GetField("SNAPINNAME")),
                    taskResponse.GetField("SNAPINNAME") + " finished installing", 10);

                Bus.Emit(Bus.Channel.Notification, notification.GetJson(), true);
                return process.ExitCode.ToString();
            }
            catch (Exception ex)
            {
                Log.Error(Name, "Could not start snapin");
                Log.Error(Name, ex);
            }

            return "-1";
        }
Exemplo n.º 5
0
        //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
            {
                var returnCode = NetUnjoinDomain(null, response.GetField("#ADUser"),
                    response.GetField("#ADPass"), UnJoinOptions.NetsetupAccountDelete);

                Log.Entry(Name, string.Format("{0} {1}", (_returnCodes.ContainsKey(returnCode)
                    ? string.Format("{0}, code = ", _returnCodes[returnCode])
                    : "Unknown Return Code: "), returnCode));

                if (returnCode.Equals(0))
                    Power.Restart("Host joined to active directory, restart needed", Power.FormOption.Delay);
            }
            catch (Exception ex)
            {
                Log.Error(Name, ex);
            }
        }
Exemplo n.º 6
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);

            Assert.IsFalse(response.Error);
            Assert.AreEqual("bar", response.GetField("#Foo"));
            Assert.IsEmpty(response.GetField("#Empty"));
            Assert.AreEqual("Special", response.GetField("#-X"));
            Assert.IsNull(response.GetField("#NON_EXISTENT"));
        }
Exemplo n.º 7
0
        //Add a host to active directory
        private void RegisterComputer(Response response)
        {
            Log.Entry(Name, "Registering host with active directory");

            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;
            }

            // Attempt to join the domain
            var returnCode = DomainWrapper(response, true, (JoinOptions.NetsetupJoinDomain | JoinOptions.NetsetupAcctCreate));

            if (returnCode == 2224)
                returnCode = DomainWrapper(response, true, JoinOptions.NetsetupJoinDomain);
            else if (returnCode == 2 || returnCode == 50)
                returnCode = DomainWrapper(response, false, (JoinOptions.NetsetupJoinDomain | JoinOptions.NetsetupAcctCreate));

            // Entry the results
            Log.Entry(Name, string.Format("{0} {1}", (_returnCodes.ContainsKey(returnCode)
                ? string.Format("{0}, code = ", _returnCodes[returnCode])
                : "Unknown Return Code: "), returnCode));

            if (returnCode.Equals(0))
                Power.Restart("Host joined to Active Directory, restart required", Power.FormOption.Delay);
        }
Exemplo n.º 8
0
        //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;
            }

            Log.Entry(Name, string.Format("Renaming host to {0}", response.GetField("#hostname")));

            if (!UserHandler.IsUserLoggedIn() || response.GetField("#force").Equals("1"))
            {
                Log.Entry(Name, "Unregistering computer");
                //First unjoin it from active directory
                UnRegisterComputer(response);

                Log.Entry(Name, "Updating registry");

                RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters","NV Hostname",
                    response.GetField("#hostname"));
                RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName", "ComputerName",
                    response.GetField("#hostname"));
                RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName",
                    response.GetField("#hostname"));

                Power.Restart(RegistryHandler.GetSystemSetting("Company") + " needs to rename your computer", Power.FormOption.Delay);
            }
            else if(!_notifiedUser)
            {
                Log.Entry(Name, "User is currently logged in, will try again later");

                var notification = new Notification("Please log off",
                    string.Format(
                        "{0} is attemping to service your computer, please log off at the soonest available time",
                        RegistryHandler.GetSystemSetting("Company")), 120);

                Bus.Emit(Bus.Channel.Notification, notification.GetJson(), true);

                _notifiedUser = true;
            }
        }
Exemplo n.º 9
0
        //Active a computer with a product key
        private void ActivateComputer(Response response)
        {
            Log.Entry(Name, "Activing host with product key");

            if (!response.IsFieldValid("#Key"))
                return;
            if (response.GetField("#Key").Length != 29)
            {
                Log.Error(Name, "Invalid product key");
                return;
            }

            try
            {
                using(var process = new Process { StartInfo = {
                            FileName = @"cscript",
                            Arguments = string.Format("//B //Nologo {0}\\slmgr.vbs /ipk {1}",
                                Environment.SystemDirectory, response.GetField("#Key")),
                            WindowStyle = ProcessWindowStyle.Hidden
                        }
                    })
                {
                    //Give windows the new key
                    process.Start();
                    process.WaitForExit();
                    process.Close();

                    //Try and activate the new key
                    process.StartInfo.Arguments = string.Format("//B //Nologo {0}\\slmgr.vbs /ato", Environment.SystemDirectory);
                    process.Start();
                    process.WaitForExit();
                    process.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Error(Name, ex);
            }
        }
Exemplo n.º 10
0
 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);
 }
Exemplo n.º 11
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;
        }
Exemplo n.º 12
0
        //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;
            }

            Log.Entry(Name, string.Format("Renaming host to {0}", response.GetField("#hostname")));

            Log.Entry(Name, "Unregistering computer");
            //First unjoin it from active directory
            UnRegisterComputer(response);
            if (Power.ShuttingDown || Power.Requested) return;

               Log.Entry(Name, "Updating registry");

            RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters","NV Hostname",
                response.GetField("#hostname"));
            RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName", "ComputerName",
                response.GetField("#hostname"));
            RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName",
                response.GetField("#hostname"));

            Power.Restart(RegistryHandler.GetSystemSetting("Company") + " needs to rename your computer", Power.FormOption.Delay);
        }
Exemplo n.º 13
0
        //Add a host to active directory
        private void RegisterComputer(Response response)
        {
            if (response.GetField("#AD") != "1")
                return;

            Log.Entry(Name, "Registering host with active directory");

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

            // 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;
                    }

                }
            }
            catch (Exception)
            {
                // ignored
            }

            // Attempt to join the domain
            var returnCode = DomainWrapper(response, true, (JoinOptions.NetsetupJoinDomain | JoinOptions.NetsetupAcctCreate));

            if (returnCode == 2224)
                returnCode = DomainWrapper(response, true, JoinOptions.NetsetupJoinDomain);
            else if (returnCode == 2 || returnCode == 50)
                returnCode = DomainWrapper(response, false, (JoinOptions.NetsetupJoinDomain | JoinOptions.NetsetupAcctCreate));

            // Entry the results
            Log.Entry(Name, string.Format("{0} {1}", (_returnCodes.ContainsKey(returnCode)
                ? string.Format("{0}, code = ", _returnCodes[returnCode])
                : "Unknown Return Code: "), returnCode));

            if (returnCode.Equals(0))
                Power.Restart("Host joined to Active Directory, restart required", Power.FormOption.Delay);
        }