示例#1
0
 public static void SaveRulesConfigurationFile(ref RulesFile F, string Filename)
 {
     Tracer.TraceInformation("enter-saverulesconfigurationfile");
     try
     {
         XmlSerializer serializer = new XmlSerializer(typeof(RulesFile));
         StreamWriter  writer     = new StreamWriter(Filename, false);
         serializer.Serialize((TextWriter)writer, F);
         writer.Close();
     }
     catch (Exception ex)
     {
         Tracer.TraceError("error {0}", ex.GetBaseException());
         throw;
     }
     finally
     {
         Tracer.TraceInformation("exit-saverulesconfigurationfile");
     }
 }
示例#2
0
 public static void LoadSettingsFromFile(string Filename, ref RulesFile Rules)
 {
     Tracer.TraceInformation("enter-loadsettingsfromfile");
     try
     {
         XmlSerializer serializer = new XmlSerializer(typeof(RulesFile));
         StreamReader  textReader = new StreamReader(Filename);
         Rules = (RulesFile)serializer.Deserialize(textReader);
         textReader.Close();
     }
     catch (Exception ex)
     {
         Tracer.TraceError("error {0}", ex.GetBaseException());
         throw;
     }
     finally
     {
         Tracer.TraceInformation("exit-loadsettingsfromfile");
     }
 }
示例#3
0
        public void Initialize()
        {
            Tracer.TraceInformation("enter-initialize");
            try
            {
                System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
                FileVersionInfo            fvi      = FileVersionInfo.GetVersionInfo(assembly.Location);
                Tracer.TraceInformation("fim-mre-version {0}", fvi.FileVersion);

                RegistryKey machineRegistry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                RegistryKey mreRootKey      = machineRegistry.OpenSubKey(@"SOFTWARE\Granfeldt\FIM\MRE", false);

                if (mreRootKey != null)
                {
                    string logFileValue = mreRootKey.GetValue("DebugLogFileName", null) as string;
                    if (logFileValue != null)
                    {
                        Tracer.TraceWarning("DebugLogFileName registry key is deprecated. Use trace logging instead.");
                    }

                    string ruleFilesAlternativePath = mreRootKey.GetValue("RuleFilesAlternativePath", null) as string;
                    if (!string.IsNullOrEmpty(ruleFilesAlternativePath))
                    {
                        ruleFilesPath = ruleFilesAlternativePath;
                        Tracer.TraceInformation("registry-alternative-rulefiles-path '{0}'", ruleFilesPath);
                    }
                }

                if (!EventLog.SourceExists(EventLogSource))
                {
                    Tracer.TraceInformation("creating-eventlog-source source: {0}, logname: ", EventLogSource, EventLogName);
                    EventLog.CreateEventSource(EventLogSource, EventLogName);
                }
                EventLog evl = new EventLog(EventLogName);
                evl.Log    = EventLogName;
                evl.Source = EventLogSource;

                EventLogTraceListener eventLog = new EventLogTraceListener(EventLogSource);
                eventLog.EventLog = evl;
                EventTypeFilter filter = new EventTypeFilter(SourceLevels.Warning | SourceLevels.Error | SourceLevels.Critical);
                eventLog.TraceOutputOptions = TraceOptions.Callstack;
                eventLog.Filter             = filter;
                Tracer.Trace.Listeners.Add(eventLog);

#if DEBUG
                // for debugging, we use current path
                if (ruleFilesPath == Utils.ExtensionsDirectory)
                {
                    ruleFilesPath = Directory.GetCurrentDirectory();
                }
#endif
                Tracer.TraceInformation("loading-rule-files-from '{0}'", ruleFilesPath);
                this.EngineRules = new Dictionary <string, List <Rule> >();
                this.Externals   = new Dictionary <string, External>();

                string[] ruleFiles = Directory.GetFiles(ruleFilesPath, "*.mre.xml", SearchOption.AllDirectories);
                foreach (string ruleFile in ruleFiles)
                {
                    RulesFile rules = new RulesFile();
                    Tracer.TraceInformation("loading-rule-file '{0}'", ruleFile);
                    MVRules.LoadSettingsFromFile(ruleFile, ref rules);

                    foreach (Rule rule in rules.Rules)
                    {
                        if (rule.Enabled)
                        {
                            Tracer.TraceInformation("found-active-rule {0}", rule.Name);
                        }
                        else
                        {
                            Tracer.TraceInformation("found-inactive-rule {0}", rule.Name);
                            continue;
                        }

                        rule.EnsureBackwardsCompatibility();

                        if (rule.Enabled && rule.RenameDnFlow != null)
                        {
                            Tracer.TraceWarning("RenameDnFlow XML element is obsolete and will not be supported in coming versions. Please see documentation for more information.");
                        }

                        if (!this.EngineRules.ContainsKey(rule.SourceObject))
                        {
                            this.EngineRules.Add(rule.SourceObject, new List <Rule>());
                        }

                        this.EngineRules[rule.SourceObject].Add(rule);
                    }

                    // handle loading externals
                    foreach (External e in rules.Externals)
                    {
                        if (string.IsNullOrEmpty(e.ReferenceId))
                        {
                            Tracer.TraceWarning("skipping-external-without-referenceid-value");
                            continue;
                        }
                        if (!this.Externals.ContainsKey(e.ReferenceId))
                        {
                            Tracer.TraceInformation("external referenceid: {0}, type: {1}, filename: {2}", e.ReferenceId, e.Type, e.Filename);
                            e.EnsureFullFilename(ruleFilesPath);
                            e.LoadExternalAssembly();
                            this.Externals.Add(e.ReferenceId, e);
                            foreach (External i in Externals?.Values)
                            {
                                i.Initialize();
                            }
                        }
                        else
                        {
                            Tracer.TraceError("cannot-add-duplicate-external-referenceid-value {0}", e.ReferenceId);
                        }
                    }
                }

                if (this.EngineRules.Count == 0)
                {
                    Tracer.TraceWarning("no-rules-loaded");
                }
            }
            catch (Exception ex)
            {
                Tracer.TraceError("error {0}", ex.GetBaseException());
                throw;
            }
            finally
            {
                Tracer.TraceInformation("exit-initialize");
            }
        }