示例#1
0
        public void Start()
        {
            //Make sure WUA Is up to Date
            WUA.Run();
            WUA.DisableAutomaticWindowsUpdates();
            _updateChecker.Enable(SendNewUpdatesHandler);

            //Disable Java, Adobe, Acrobat Updaters
            DisableUpdaters.DisableAll();
            //Check WSUS Status
            Logger.Log("WSUS enabled? {0}", LogLevel.Info, WSUS.IsWSUSEnabled());
            Logger.Log("WSUS address? {0}", LogLevel.Info, WSUS.GetServerWSUS);
            Logger.Log("Automatic updates Enabled? {0}", LogLevel.Info, WSUS.IsAutomaticUpdatesEnabled());
            try
            {
                var sysTimeZone = GetTimeZone.GetMyTimeZone();
                Logger.Log("Current time zone {0}, utc off set {1}.", LogLevel.Info, sysTimeZone.time_zone,
                           sysTimeZone.utc_offset);
            }
            catch
            {
                Logger.Log("Unable to obtain timezone.", LogLevel.Error);
            }

            switch (WSUS.GetAutomaticUpdatesOptions())
            {
            case WSUS.AutomaticUpdateStatus.AutomaticDownloadAndNotifyOfInstall:
                Logger.Log("Automatic updates setting? {0}", LogLevel.Info, "Automatic download and notify of install.");
                break;

            case WSUS.AutomaticUpdateStatus.AutomaticDownloadAndScheduleInstall:
                Logger.Log("Automatic updates setting? {0}", LogLevel.Info, "Automatic download and schedule install.");
                break;

            case WSUS.AutomaticUpdateStatus.AutomaticUpdatesIsRequiredAndUsersCanConfigureIt:
                Logger.Log("Automatic updates setting? {0}", LogLevel.Info, "Automatic updates is required and users can configure it.");
                break;

            case WSUS.AutomaticUpdateStatus.NotifyBeforeDownload:
                Logger.Log("Automatic updates setting? {0}", LogLevel.Info, "Notify before download.");
                break;

            default:
                Logger.Log("Automatic updates setting? {0}", LogLevel.Info, "Not set.");
                break;
            }

            //Check AntiMalware and Firewall Settings
            Logger.Log("Anti Spyware protection? {0}", LogLevel.Info, WindowsAntiSpyware.IsProtectionEnabled());
            Logger.Log("Windows firewall protection? {0}", LogLevel.Info, WindowsFirewall.IsProtectionEnabled());

            //Check if system is Windows 8 and Disable automatic restarts
            //after critical system updates are installed.
            RvUtils.Windows8AutoRestart(false);
        }
        public static Operations.SavedOpData DownloadFile(Operations.SavedOpData update, UpdateDirectories updateTypePath)
        {
            var    uris = new List <DownloadUri>();
            string updateDirectory;

            switch (updateTypePath)
            {
            case UpdateDirectories.SupportedAppDir:
                updateDirectory = Settings.SupportedAppDirectory;
                break;

            case UpdateDirectories.CustomAppDir:
                updateDirectory = Settings.CustomAppDirectory;
                break;

            case UpdateDirectories.OSUpdateDir:
                updateDirectory = Settings.UpdateDirectory;
                break;

            default:
                updateDirectory = Settings.UpdateDirectory;
                break;
            }

            var savedir = Path.Combine(updateDirectory, update.filedata_app_id);

            foreach (var uriData in update.filedata_app_uris)
            {
                var tempDownloadUri = new DownloadUri();
                tempDownloadUri.FileName = uriData.file_name;
                tempDownloadUri.FileSize = uriData.file_size;
                tempDownloadUri.Hash     = uriData.file_hash;

                //add uri link from WSUS if its enabled
                if (WSUS.IsWSUSEnabled())
                {
                    var tempAvilApp = Agent.RV.WindowsApps.WindowsUpdates.GetAvailableUpdates();
                    foreach (var application in tempAvilApp)
                    {
                        if (application.Name == update.filedata_app_name)
                        {
                            foreach (var availUri in application.FileData)
                            {
                                tempDownloadUri.Uris.Add(availUri.Uri);
                            }
                        }
                    }
                }

                foreach (var uri in uriData.file_uris)
                {
                    tempDownloadUri.Uris.Add(uri);
                }

                uris.Add(tempDownloadUri);
            }

            if (Directory.Exists(savedir))
            {
                Directory.Delete(savedir, true);
            }
            Directory.CreateDirectory(savedir);

            foreach (var file in uris)
            {
                // Just in case the web server is using a self-signed cert.
                // Webclient won't validate the SSL/TLS cerficate if it's not trusted.
                var tempCallback = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); };

                var filepath = Path.Combine(savedir, file.FileName);

                try
                {
                    using (var client = new WebClient())
                    {
                        if (Settings.Proxy != null)
                        {
                            client.Proxy = Settings.Proxy;
                        }

                        var downloaded = false;
                        foreach (var uriSingle in file.Uris)
                        {
                            try
                            {
                                var splitted    = uriSingle.Split(new[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
                                var splitted2   = splitted[1].Split(new[] { '/' });
                                var relayserver = splitted2[0];

                                if (downloaded)
                                {
                                    break;
                                }

                                Logger.Log("Attempting to download {1} from {0} with file size of {2}.", LogLevel.Info, relayserver, file.FileName, file.FileSize);
                                client.DownloadFile(uriSingle, filepath);

                                if (File.Exists(filepath))
                                {
                                    var localFileHashMd5    = RvUtils.Md5HashFile(filepath).ToLower();
                                    var localFileHashSha1   = RvUtils.Sha1HashFile(filepath).ToLower();
                                    var localFileHashSha256 = RvUtils.Sha256HashFile(filepath).ToLower();

                                    Logger.Log("Download Complete,  {0}", LogLevel.Info, file.FileName);
                                    Logger.Log("Checking Hashes...");
                                    Logger.Log("Incoming Hash: {0}", LogLevel.Info, file.Hash);
                                    Logger.Log("Local MD5 Hash: {0}", LogLevel.Info, localFileHashMd5);
                                    Logger.Log("Local SHA1 Hash: {0}", LogLevel.Info, localFileHashSha1);
                                    Logger.Log("Local SHA256 Hash: {0}", LogLevel.Info, localFileHashSha256);
                                    downloaded = true;

                                    if (localFileHashMd5 != file.Hash.ToLower() && localFileHashSha1 != file.Hash.ToLower() && localFileHashSha256 != file.Hash.ToLower())
                                    {
                                        Logger.Log("Local file {0} Hash did not match remote's. Retrying with a different server.", LogLevel.Info, file.FileName);
                                        update.error   = "Local file Hash did not match remote. Bad file integrity. ";
                                        update.success = false.ToString().ToLower();
                                        downloaded     = false;
                                    }
                                }
                                else
                                {
                                    Logger.Log("File {0} did not download. Retrying with a different server.", LogLevel.Info, file.FileName);
                                    update.error   = "File did not download successfully, it was not found on disk. Please check download server.";
                                    update.success = false.ToString().ToLower();
                                    downloaded     = false;
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Log("File {0} failed to download correctly... Possible connection issue, Retrying with a different server.", LogLevel.Info, file.FileName);
                                update.error   = "File did not download correctly, Exception message: " + e.Message + ". Please check download server connectivity.";
                                update.success = false.ToString().ToLower();
                                downloaded     = false;
                            }
                        }

                        //Check if the file was successfully downloaded and return.
                        if (downloaded)
                        {
                            update.error   = String.Empty;
                            update.success = true.ToString().ToLower();
                        }
                    }
                }
                catch (Exception e)
                {
                    //Critical exception occurred.
                    update.error   = "Application did not download, Exception occured, refer to log for details.";
                    update.success = false.ToString().ToLower();
                    Logger.Log("One or more Application Files were not downloaded successfully; {0}.",
                               LogLevel.Error, file.FileName);
                    Logger.LogException(e);
                    return(update);
                }

                ServicePointManager.ServerCertificateValidationCallback = tempCallback;
            }

            return(update);
        }
示例#3
0
        public static Operations.SavedOpData DownloadUpdate(Operations.SavedOpData update)
        {
            var uris = new List <DownloadUri>();

            foreach (var uriData in update.filedata_app_uris)
            {
                var tempDownloadUri = new DownloadUri();
                tempDownloadUri.FileName = uriData.file_name;
                tempDownloadUri.FileSize = uriData.file_size;
                tempDownloadUri.Hash     = uriData.file_hash;

                foreach (var uri in uriData.file_uris)
                {
                    tempDownloadUri.Uris.Add(uri);
                }

                uris.Add(tempDownloadUri);
            }

            try
            {
                if (Directory.Exists(AgentUpdateDirectory))
                {
                    Directory.Delete(AgentUpdateDirectory, true);
                }
                Directory.CreateDirectory(AgentUpdateDirectory);
            }catch {}


            foreach (var file in uris)
            {
                // Just in case the web server is using a self-signed cert.
                // Webclient won't validate the SSL/TLS cerficate if it's not trusted.
                var tempCallback = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); };

                var filepath = Path.Combine(AgentUpdateDirectory, file.FileName);

                try
                {
                    using (var client = new WebClient())
                    {
                        if (Settings.Proxy != null)
                        {
                            client.Proxy = Settings.Proxy;
                        }

                        var downloaded = false;
                        foreach (var uriSingle in file.Uris)
                        {
                            try
                            {
                                var splitted    = uriSingle.Split(new[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
                                var splitted2   = splitted[1].Split(new[] { '/' });
                                var relayserver = splitted2[0];

                                if (downloaded)
                                {
                                    break;
                                }

                                Logger.Log("Attempting to download {1} from {0} with file size of {2}.", LogLevel.Info, relayserver, file.FileName, file.FileSize);

                                byte NetThrottle = byte.Parse(update.net_throttle);
                                if (NetThrottle > 0 || NetThrottle == null)
                                {
                                    Agent.RV.Utils.NetworkThrottle.DownloadThrottle(filepath, uriSingle, file.FileName, NetThrottle);
                                }
                                else
                                {
                                    client.DownloadFile(uriSingle, filepath);
                                }

                                if (File.Exists(filepath))
                                {
                                    var localFileHash = RvUtils.Md5HashFile(filepath).ToLower();
                                    Logger.Log("Download Complete,  {0}", LogLevel.Info, file.FileName);
                                    Logger.Log("Checking MD5 Hash...");
                                    Logger.Log("Incoming Hash: {0}", LogLevel.Info, file.Hash);
                                    Logger.Log("Local Hash: {0}", LogLevel.Info, localFileHash);
                                    downloaded = true;

                                    if (localFileHash != file.Hash.ToLower())
                                    {
                                        Logger.Log("Local file {0} Hash did not match remote's. Retrying with a different server.", LogLevel.Info, file.FileName);
                                        update.error   = "Local file Hash did not match remote. Bad file integrity. ";
                                        update.success = false.ToString().ToLower();
                                        downloaded     = false;
                                    }
                                }
                                else
                                {
                                    Logger.Log("File {0} did not download. Retrying with a different server.", LogLevel.Info, file.FileName);
                                    update.error   = "File did not download successfully, it was not found on disk. Please check download server.";
                                    update.success = false.ToString().ToLower();
                                    downloaded     = false;
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Log("File {0} failed to download correctly... Possible connection issue, Retrying with a different server.", LogLevel.Info, file.FileName);
                                update.error   = "File did not download correctly, Exception message: " + e.Message + ". Please check download server connectivity.";
                                update.success = false.ToString().ToLower();
                                downloaded     = false;
                            }
                        }

                        //Check if the file was successfully downloaded and return.
                        if (downloaded)
                        {
                            update.error   = String.Empty;
                            update.success = true.ToString().ToLower();
                        }
                    }
                }
                catch (Exception e)
                {
                    //Critical exception occurred.
                    update.error   = "Agent update did not download successfully, Exception occured, refer to log for details.";
                    update.success = false.ToString().ToLower();
                    Logger.Log("One or more Agent update Files were not downloaded successfully; {0}.",
                               LogLevel.Error, file.FileName);
                    Logger.LogException(e);
                    return(update);
                }

                ServicePointManager.ServerCertificateValidationCallback = tempCallback;
            }

            return(update);
        }
示例#4
0
        private void InstallWindowsUpdate(RvSofOperation operation)
        {
            var savedOperations = Operations.LoadOpDirectory().Where(p => p.operation == OperationValue.InstallWindowsUpdate).ToList();

            if (!savedOperations.Any())
            {
                Logger.Log("There are no operations remaining, Unable to install windows update: {0}", LogLevel.Warning, operation.Type);
                return;
            }

            WindowsUpdates.PopulateAvailableUpdatesList();
            WindowsUpdates.PopulateInstalledUpdatesList();

            foreach (var update in savedOperations)
            {
                //Check if update is already installed.
                ///////////////////////////////////////////////////////////////////////////////////////////
                if (WindowsUpdates.IsUpdateInstalled(update.filedata_app_name))
                {
                    Logger.Log("Update is already installed ({0}), sending back results.", LogLevel.Info, update.filedata_app_name);
                    Operations.UpdateStatus(update, Operations.OperationStatus.ResultsPending);
                    InstallSendResults(update, operation);
                    continue; //Move on to next update.
                }

                Logger.Log("Preparing to download");
                Operations.SavedOpData updateDownloadResults = Downloader.DownloadFile(update, Downloader.UpdateDirectories.OSUpdateDir);

                //If download fails, send back results to server and move to next package (if any).
                ////////////////////////////////////////////////////////////////////////////////////////////
                if (!String.IsNullOrEmpty(updateDownloadResults.error))
                {
                    Operations.UpdateStatus(updateDownloadResults, Operations.OperationStatus.ResultsPending);
                    InstallSendResults(updateDownloadResults, operation);
                    continue;
                }
                Logger.Log("Download completed for {0}", LogLevel.Info, update.filedata_app_name);

                Logger.Log("Installing {0} ", LogLevel.Info, update.filedata_app_name);
                Operations.SavedOpData updateInstallResults = WindowsUpdates.InstallWindowsUpdate(update);

                //If installation fails, send back results to server and move to next package (if any).
                /////////////////////////////////////////////////////////////////////////////////////////////
                if (!String.IsNullOrEmpty(updateInstallResults.error))
                {
                    Operations.UpdateStatus(updateDownloadResults, Operations.OperationStatus.ResultsPending);
                    InstallSendResults(updateInstallResults, operation);
                    continue;
                }
                Logger.Log("Installation of {0} was a success.", LogLevel.Info, update.filedata_app_name);
                Operations.UpdateStatus(updateDownloadResults, Operations.OperationStatus.ResultsPending);



                /////////////////////////////////////////////////////////////////////////////////////////////////////////
                //Check scenerio for this update, react accordingly.
                /////////////////////////////////////////////////////////////////////////////////////////////////////////
                if (Convert.ToBoolean(updateInstallResults.reboot_required) && Convert.ToBoolean(updateInstallResults.success) && (updateInstallResults.restart == "optional" || updateInstallResults.restart == "forced"))
                {
                    Operations.UpdateOperation(updateInstallResults, true, true, Operations.OperationStatus.Rebooting);
                    Operations.DeleteLocalUpdateBundleFolder(updateInstallResults);
                    Logger.Log("Rebooting system as per update requirement.");
                    RvUtils.RestartSystem();
                    Stop(); //System will restart to continue Windows update configuration, then ResumeOperations will start where we left off.
                }
                else if (Convert.ToBoolean(updateInstallResults.reboot_required) && !Convert.ToBoolean(updateInstallResults.success) && (updateInstallResults.restart == "optional" || updateInstallResults.restart == "forced"))
                {
                    Operations.UpdateOperation(updateInstallResults, false, true, Operations.OperationStatus.Rebooting);
                    Operations.DeleteLocalUpdateBundleFolder(updateInstallResults);
                    Logger.Log("Rebooting system as per update requirement.");
                    RvUtils.RestartSystem();
                    Stop(); //System will restart to continue Windows update configuration, then ResumeOperations will start where we left off.
                }
                else if (Convert.ToBoolean(updateInstallResults.reboot_required) && Convert.ToBoolean(updateInstallResults.success) && updateInstallResults.restart == "none")
                {
                    InstallSendResults(updateInstallResults, operation);
                }
                else if (Convert.ToBoolean(updateInstallResults.reboot_required) && !Convert.ToBoolean(updateInstallResults.success) && updateInstallResults.restart != "none")
                {
                    Operations.UpdateOperation(updateInstallResults, false, true, Operations.OperationStatus.Rebooting);
                    Operations.DeleteLocalUpdateBundleFolder(updateInstallResults);
                    RvUtils.RestartSystem();
                    Stop(); //System will restart to continue Windows update configuration, then ResumeOperations will start where we left off.
                }
                else if (Convert.ToBoolean(updateInstallResults.reboot_required) && updateInstallResults.restart != "none")
                {
                    var isInstalled = WindowsUpdates.IsUpdateInstalled(updateInstallResults.filedata_app_name);
                    Logger.Log("Rebooting system as per update requirement.");
                    Operations.UpdateOperation(updateInstallResults, isInstalled, true, Operations.OperationStatus.Rebooting);
                    RvUtils.RestartSystem();
                    Stop(); //System will restart to continue Windows update configuration, then ResumeOperations will start where we left off.
                }
                else
                {
                    InstallSendResults(updateInstallResults, operation);
                }
            }
        }