Esempio n. 1
0
        public Dictionary <string, string> GetCustomConfigurationObjects(PluginConfiguration config)
        {
            Dictionary <string, string> result = new Dictionary <string, string>();

            foreach (PropertyInfo pi in config.GetType().GetProperties())
            {
                result.Add(pi.Name, pi.GetValue(config, null).ToString());
            }
            return(result);
        }
Esempio n. 2
0
 public void LoadAgents()
 {
     if (LoadedAgents == null)
     {
         LoadedAgents = new Dictionary <SecurityAgent, AgentProxy>();
     }
     if (LoadedAgents.Count > 0)
     {
         UnloadAgents();
     }
     foreach (SecurityAgent agent in this)
     {
         if (agent.Enabled)
         {
             try {
                 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
                 System.Security.Policy.Evidence adevidence = AppDomain.CurrentDomain.Evidence;
                 AppDomain  domain = AppDomain.CreateDomain("Cyberarms.Agents." + agent.Id, adevidence, setup);
                 AgentProxy proxy  = new AgentProxy(agent.AssemblyFilename, agent.Name);
                 proxy.Configuration.AgentName              = agent.Name;
                 proxy.Configuration.AssemblyName           = agent.AssemblyName;
                 proxy.Configuration.Enabled                = agent.Enabled;
                 proxy.Configuration.HardLockAttempts       = agent.HardLockAttempts;
                 proxy.Configuration.HardLockDurationHrs    = agent.HardLockTimeHours;
                 proxy.Configuration.NeverUnlock            = agent.LockForever;
                 proxy.Configuration.OverwriteConfiguration = agent.OverrideConfig;
                 proxy.Configuration.SoftLockAttempts       = agent.SoftLockAttempts;
                 proxy.Configuration.SoftLockDurationMins   = agent.SoftLockTimeMinutes;
                 PluginConfiguration pc = proxy.Configuration.AgentSettings;
                 if (pc != null)
                 {
                     foreach (PropertyInfo pi in pc.GetType().GetProperties())
                     {
                         if (agent.CustomConfiguration.ContainsKey(pi.Name))
                         {
                             if (pi.PropertyType == typeof(int))
                             {
                                 int result;
                                 int.TryParse(agent.CustomConfiguration[pi.Name], out result);
                                 pi.SetValue(pc, result, null);
                             }
                         }
                     }
                 }
                 agent.AppDomain = domain;
                 agent.Reload();
                 LoadedAgents.Add(agent, proxy);
             } catch (Exception ex) {
                 throw ex;
             }
         }
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Reconstructs a new Terminals configuration file from a broken one.
        /// </summary>
        /// <remarks>Please read all the comments in this methods before chaning something.</remarks>
        /// <returns>Returns a complete new Terminals configuration in the form of a <see cref="System.Configuration.Configuration">.NET configuration file</see>.</returns>
        private static System.Configuration.Configuration ImportConfiguration()
        {
            // If we are already in the remapping process/progress
            // i.e. if we are in this method -> we don't want to go
            // through it a second or third time for just calling
            // a property in the Settings class
            // like
            //        public static bool ShowFullInformationToolTips
            //        {
            //            get { return GetSection().ShowFullInformationToolTips; }
            //
            //            set
            //            {
            //                GetSection().ShowFullInformationToolTips = value;
            //                SaveImmediatelyIfRequested();
            //            }
            //        }
            // Which internally calls GetSection() -> and that forces to call
            // this method if the mapping of the file fails.
            // -> So quickly jump out of this procedure
            //
            // BTW: Every Save() method call will be ignored as long as the
            // remappingInProgress := true
            // That's why we save the file that has been constructed in memory
            // to the disk until we are finished with the whole parsing process.
            // (-> see 'Save()' at the end of this method)
            if (remappingInProgress)
            {
                return(OpenConfiguration());
            }

            // Stop the file watcher while we create a plain new configuration file
            if (fileWatcher != null)
            {
                fileWatcher.StopObservation();
            }

            remappingInProgress = true;

            // Create a temporary copy of this file and overwrite the original
            // one with a plain new configuration file (as can be seen in the next
            // few lines (i.e. SaveDefaultConfigFile())
            // We'll parse the temporary copy of the file that has been created and
            // update the plain file
            if (string.IsNullOrEmpty(tempFile))
            {
                tempFile = Path.GetTempFileName();
                CopyFile(ConfigurationFileLocation, tempFile);
            }

            // Restart the file watcher again
            SaveDefaultConfigFile();

            if (fileWatcher != null)
            {
                fileWatcher.StartObservation();
            }

            // get a list of the properties on the Settings object (static props)
            PropertyInfo[] propList = typeof(Settings).GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.SetProperty);

            // Load the whole xml file
            // No problem if this line fails -> the plain new copy is
            // totally enough
            XmlDocument doc = LoadDocument(tempFile);

            // get the settings (options)
            XmlNode root = doc.SelectSingleNode("/configuration/settings");

            bool terminalsPasswordDetected = false;

            try
            {
                // for each setting's attribute
                foreach (XmlAttribute att in root.Attributes)
                {
                    // Set the Terminals password used to encrypt and decrypt the passwords if enabled
                    try
                    {
                        if (att.Name.Equals("TerminalsPassword", StringComparison.CurrentCultureIgnoreCase))
                        {
                            terminalsPasswordDetected = true;
                            if (!string.IsNullOrWhiteSpace(att.Value))
                            {
                                remappingSection.TerminalsPassword = att.Value;
                                Log.Debug("The Terminals password has been set in the new configuration file.");
                                continue;
                            }

                            Log.Debug("A Terminals password attribute exists in the broken original configuration file but it hasn't been set.");
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        terminalsPasswordDetected = true;
                        Log.Error("Error occured while trying to set the Terminals password in the new configuration file. This error will prevent terminals from decrypting your passwords.", ex);
                        continue;
                    }

                    // scan for the related property if any
                    foreach (PropertyInfo info in propList)
                    {
                        try
                        {
                            if (info.Name.ToLower() == att.Name.ToLower())
                            {
                                // found a matching property, try to set it
                                string val = att.Value;

                                if (info.PropertyType.Name == "Version")
                                {
                                    info.SetValue(null, new Version(val), null);
                                }
                                else
                                {
                                    info.SetValue(null, Convert.ChangeType(val, info.PropertyType), null);
                                }

                                break;
                            }
                        }
                        catch (Exception exc)
                        {
                            Log.Error("Error occured while trying parse the configuration file and map " + att.Name + ": " + exc.Message);
                            break;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                Log.Error("Error occured while trying parse the configuration file and remapping the root settings.", exc);
            }

            if (!terminalsPasswordDetected)
            {
                Log.Debug("No Terminals password has been found in the broken configuration file.");
            }

            // pluginOptions
            // favoritesButtonsList
            // toolStripSettings
            // savedConnectionsList

            // -> WAS ist mit Groups und anderen Objekten in den Favorites im Code? -> Code-Tree überprüfen

            // usersMRUList
            // serversMRUList
            // domainsMRUList


            // tags -> werden implizit durch die Favorites gesetzt - Überprüfen ob korrekt

            // Nicht alle Felder bei den Favorites werden gesetzt

            // Work through every favorite configuration element
            XmlNodeList favs = doc.SelectNodes("/configuration/settings/favorites/add");

            try
            {
                foreach (XmlNode fav in favs)
                {
                    FavoriteConfigurationElement newFav = new FavoriteConfigurationElement();

                    // Add the plugin configuration for each favorite element
                    if (fav.ChildNodes != null && fav.ChildNodes.Count == 1)
                    {
                        XmlNode pluginRootElement = fav.ChildNodes[0];

                        if (pluginRootElement.Name.Equals("PLUGINS", StringComparison.InvariantCultureIgnoreCase))
                        {
                            foreach (XmlNode plugin in pluginRootElement.ChildNodes)
                            {
                                if (plugin.Name.Equals("PLUGIN", StringComparison.InvariantCultureIgnoreCase) && plugin.Attributes != null && plugin.Attributes.Count > 0)
                                {
                                    PluginConfiguration pluginConfig = new PluginConfiguration();

                                    if (plugin.Attributes["name"] != null && !string.IsNullOrEmpty(plugin.Attributes["name"].Value))
                                    {
                                        pluginConfig.Name = plugin.Attributes["name"].Value;
                                    }

                                    if (plugin.Attributes["value"] != null && !string.IsNullOrEmpty(plugin.Attributes["value"].Value))
                                    {
                                        pluginConfig.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty).First(property => property.Name.Equals("value", StringComparison.InvariantCultureIgnoreCase)).SetValue(pluginConfig, plugin.Attributes["value"].Value, null);
                                    }

                                    if (plugin.Attributes["defaultValue"] != null && !string.IsNullOrEmpty(plugin.Attributes["defaultValue"].Value))
                                    {
                                        pluginConfig.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty).First(property => property.Name.Equals("defaultValue", StringComparison.InvariantCultureIgnoreCase)).SetValue(pluginConfig, plugin.Attributes["defaultValue"].Value, null);
                                    }

                                    newFav.PluginConfigurations.Add(pluginConfig);
                                }
                            }
                        }
                    }

                    // Add the attributes of each favorite
                    foreach (XmlAttribute att in fav.Attributes)
                    {
                        foreach (PropertyInfo info in newFav.GetType().GetProperties())
                        {
                            try
                            {
                                if (info.Name.ToLower() == att.Name.ToLower())
                                {
                                    // found a matching property, try to set it
                                    string val = att.Value;
                                    if (info.PropertyType.IsEnum)
                                    {
                                        info.SetValue(newFav, Enum.Parse(info.PropertyType, val), null);
                                    }
                                    else
                                    {
                                        info.SetValue(newFav, Convert.ChangeType(val, info.PropertyType), null);
                                    }

                                    break;
                                }
                            }
                            catch (Exception exc)
                            {
                                string favoriteName = "favorite";

                                if (fav == null || fav.Attributes.Count < 1 || fav.Attributes["name"] == null || string.IsNullOrEmpty(fav.Attributes["name"].Value))
                                {
                                    favoriteName += "s";
                                }
                                else
                                {
                                    favoriteName += " " + fav.Attributes["name"].Value;
                                }

                                favoriteName += ": ";

                                Log.Error("Error occured while trying parse the configuration file and remapping the " + favoriteName + exc.Message);
                                break;
                            }
                        }
                    }

                    AddFavorite(newFav);
                }
            }
            catch (Exception exc)
            {
                Log.Error("Error remapping favorites: " + exc.Message);
                remappingInProgress = false;
                return(Config);
                //return _config;
                //return OpenConfiguration();
            }

            File.Delete(tempFile);
            remappingInProgress = false;
            Save();
            return(_config);
            //return OpenConfiguration();
        }
        /// <summary>
        /// Reconstructs a new Terminals configuration file from a broken one.
        /// </summary>
        /// <remarks>Please read all the comments in this methods before chaning something.</remarks>
        /// <returns>Returns a complete new Terminals configuration in the form of a <see cref="System.Configuration.Configuration">.NET configuration file</see>.</returns>
        private static System.Configuration.Configuration ImportConfiguration()
        {
        	// If we are already in the remapping process/progress
        	// i.e. if we are in this method -> we don't want to go
        	// through it a second or third time for just calling
			// a property in the Settings class
			// like			
        	//        public static bool ShowFullInformationToolTips
			//        {
			//            get { return GetSection().ShowFullInformationToolTips; }
			//
			//            set
			//            {
			//                GetSection().ShowFullInformationToolTips = value;
			//                SaveImmediatelyIfRequested();
			//            }
			//        }
			// Which internally calls GetSection() -> and that forces to call
			// this method if the mapping of the file fails.
			// -> So quickly jump out of this procedure
			//
			// BTW: Every Save() method call will be ignored as long as the
			// remappingInProgress := true
			// That's why we save the file that has been constructed in memory
			// to the disk until we are finished with the whole parsing process.
			// (-> see 'Save()' at the end of this method)
        	if (remappingInProgress)
        		return OpenConfiguration();
        	
        	// Stop the file watcher while we create a plain new configuration file
            if (fileWatcher != null)
                fileWatcher.StopObservation();
        	
       		remappingInProgress = true;
       		
       		// Create a temporary copy of this file and overwrite the original
       		// one with a plain new configuration file (as can be seen in the next
       		// few lines (i.e. SaveDefaultConfigFile())
       		// We'll parse the temporary copy of the file that has been created and
			// update the plain file       		
       		if (string.IsNullOrEmpty(tempFile))
       		{
       			tempFile = Path.GetTempFileName();
       			CopyFile(ConfigurationFileLocation, tempFile);
       		}

       		// Restart the file watcher again
       		SaveDefaultConfigFile(); 
       		
            if (fileWatcher != null)
                fileWatcher.StartObservation();

            // get a list of the properties on the Settings object (static props)
            PropertyInfo[] propList = typeof (Settings).GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.SetProperty);

            // Load the whole xml file
            // No problem if this line fails -> the plain new copy is
            // totally enough
            XmlDocument doc = LoadDocument(tempFile);

            // get the settings (options)
            XmlNode root = doc.SelectSingleNode("/configuration/settings");
            
            bool terminalsPasswordDetected = false;
            
            try
            {
                // for each setting's attribute
                foreach (XmlAttribute att in root.Attributes)
                {
                	// Set the Terminals password used to encrypt and decrypt the passwords if enabled
                	try
                	{
	                   	if (att.Name.Equals("TerminalsPassword", StringComparison.CurrentCultureIgnoreCase))
	                    {
	                   		terminalsPasswordDetected = true;
	                   		if (!string.IsNullOrWhiteSpace(att.Value))
	                   		{
		                   		remappingSection.TerminalsPassword = att.Value;
		                    	Log.Debug("The Terminals password has been set in the new configuration file.");
		                    	continue;
	                   		}
	                   		
	                   		Log.Debug("A Terminals password attribute exists in the broken original configuration file but it hasn't been set.");
		                    continue;
	                    }
                	}
                	catch (Exception ex)
                	{
                		terminalsPasswordDetected = true;
                		Log.Error("Error occured while trying to set the Terminals password in the new configuration file. This error will prevent terminals from decrypting your passwords.", ex);
                		continue;
                	}
                	
                    // scan for the related property if any
                    foreach (PropertyInfo info in propList)
                    {
                        try
                        {                        	
                            if (info.Name.ToLower() == att.Name.ToLower())
                            {
                                // found a matching property, try to set it
                                string val = att.Value;
                                
                                if (info.PropertyType.Name == "Version")
                                	info.SetValue(null, new Version(val), null);
                                else
                                	info.SetValue(null, Convert.ChangeType(val, info.PropertyType), null);
                                
                                break;
                            }
                        }
                        catch (Exception exc)
                        {
                            Log.Error("Error occured while trying parse the configuration file and map " + att.Name + ": " + exc.Message);
                            break;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                Log.Error("Error occured while trying parse the configuration file and remapping the root settings.", exc);
            }

            if (!terminalsPasswordDetected)
            {
            	Log.Debug("No Terminals password has been found in the broken configuration file.");
            }
            
            // pluginOptions
            // favoritesButtonsList
            // toolStripSettings
            // savedConnectionsList
            
            // -> WAS ist mit Groups und anderen Objekten in den Favorites im Code? -> Code-Tree überprüfen
            
            // usersMRUList
            // serversMRUList
            // domainsMRUList
            
            
            // tags -> werden implizit durch die Favorites gesetzt - Überprüfen ob korrekt
            
            // Nicht alle Felder bei den Favorites werden gesetzt
            
            // Work through every favorite configuration element
            XmlNodeList favs = doc.SelectNodes("/configuration/settings/favorites/add");
            try
            {
                foreach (XmlNode fav in favs)
            	{
                    FavoriteConfigurationElement newFav = new FavoriteConfigurationElement();
                    
                    // Add the plugin configuration for each favorite element
                    if (fav.ChildNodes != null && fav.ChildNodes.Count == 1)
                    {
                    	XmlNode pluginRootElement = fav.ChildNodes[0];
                    	
                    	if (pluginRootElement.Name.Equals("PLUGINS", StringComparison.InvariantCultureIgnoreCase))
                    	{
                    		foreach (XmlNode plugin in pluginRootElement.ChildNodes)
                    		{
                    			if (plugin.Name.Equals("PLUGIN", StringComparison.InvariantCultureIgnoreCase) && plugin.Attributes != null && plugin.Attributes.Count > 0)
                    			{
                    				PluginConfiguration pluginConfig = new PluginConfiguration();
                    				
                    				if (plugin.Attributes["name"] != null && !string.IsNullOrEmpty(plugin.Attributes["name"].Value))
                    					pluginConfig.Name = plugin.Attributes["name"].Value;
                    				
                    				if (plugin.Attributes["value"] != null && !string.IsNullOrEmpty(plugin.Attributes["value"].Value))
                    				{
                    					pluginConfig.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty).First(property => property.Name.Equals("value", StringComparison.InvariantCultureIgnoreCase)).SetValue(pluginConfig, plugin.Attributes["value"].Value, null);
                    				}
                    				
                    				if (plugin.Attributes["defaultValue"] != null && !string.IsNullOrEmpty(plugin.Attributes["defaultValue"].Value))
                    				{
                    					pluginConfig.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty).First(property => property.Name.Equals("defaultValue", StringComparison.InvariantCultureIgnoreCase)).SetValue(pluginConfig, plugin.Attributes["defaultValue"].Value, null);
                    				}
                    				
                    				newFav.PluginConfigurations.Add(pluginConfig);
                    			}
                    		}
                    	}
                    }
                    
                    // Add the attributes of each favorite
                    foreach (XmlAttribute att in fav.Attributes)
                    {                    	
                        foreach (PropertyInfo info in newFav.GetType().GetProperties())
                        {
                            try
                            {
                                if (info.Name.ToLower() == att.Name.ToLower())
                                {
                                    // found a matching property, try to set it
                                    string val = att.Value;
                                    if (info.PropertyType.IsEnum)
                                    {
                                        info.SetValue(newFav, Enum.Parse(info.PropertyType, val), null);
                                    }
                                    else
                                    {
                                        info.SetValue(newFav, Convert.ChangeType(val, info.PropertyType), null);
                                    }

                                    break;
                                }
                            }
                            catch (Exception exc)
                            {
                            	string favoriteName = "favorite";
                            	
                            	if (fav == null || fav.Attributes.Count < 1 || fav.Attributes["name"] == null || string.IsNullOrEmpty(fav.Attributes["name"].Value))
                            		favoriteName += "s";
                            	else
                            		favoriteName += " " + fav.Attributes["name"].Value;
                            	
                            	favoriteName += ": ";
                            	
                                Log.Error("Error occured while trying parse the configuration file and remapping the " + favoriteName + exc.Message);
                                break;
                            }
                        }
                    }

                    AddFavorite(newFav);
                }
            }
            catch (Exception exc)
            {
                Log.Error("Error remapping favorites: " + exc.Message);
                remappingInProgress = false;
	            return Config;
	            //return _config;
	            //return OpenConfiguration();
            }
            
            File.Delete(tempFile);
			remappingInProgress = false;
            Save();
            return _config;
            //return OpenConfiguration();
        }