You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

snfCFGmgr.hpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. // snfCFGmgr.hpp
  2. // Copyright (C) 2006 - 2009 Arm Research Labs, LLC
  3. // See www.armresearch.com for the copyright terms.
  4. //
  5. // SNF Configuration manager.
  6. //// Begin include only once
  7. #ifndef included_snfCFGmgr_hpp
  8. #define included_snfCFGmgr_hpp
  9. #include "SNFMulti/GBUdb.hpp"
  10. #include "SNFMulti/snf_HeaderFinder.hpp"
  11. #include "CodeDweller/XMLReader.hpp"
  12. #include "CodeDweller/threading.hpp"
  13. #include <string>
  14. #include <set>
  15. namespace SNFMulti {
  16. const unsigned long int HeaderDirectiveBypass = 0x00000001; // Bypass hd rule flag.
  17. const unsigned long int HeaderDirectiveWhite = 0x00000002; // White hd rule flag.
  18. const unsigned long int HeaderDirectiveDrillDown = 0x00000004; // DrillDown rule flag.
  19. const unsigned long int HeaderDirectiveSource = 0x00000008; // Source rule flag.
  20. const unsigned long int HeaderDirectiveContext = 0x80000000; // Context activation flag.
  21. class HeaderDirectiveHandler : public CodeDweller::Configurator { // Handle inputs to header directives.
  22. public:
  23. HeaderDirectiveSet HeaderDirectives; // Managed set of Header Directives.
  24. void operator()(CodeDweller::ConfigurationElement& E,
  25. CodeDweller::ConfigurationData& D) { // The configurator call adds the Input.
  26. if(HeaderDirectiveContext == ContextInput.Directive) { // If a context has been established
  27. ContextInput.Context = HeaderDirectives.size() + 1; // then setup the context ID and
  28. DirectiveInput.Context = ContextInput.Context; // share it with the input.
  29. HeaderDirectives.insert(ContextInput); // Insert the context tester and
  30. ContextInput.clear(); // then clear it for future use.
  31. }
  32. HeaderDirectives.insert(DirectiveInput); // Insert the directive and then
  33. DirectiveInput.clear(); // clear the input for future use.
  34. }
  35. HeaderFinderPattern ContextInput; // The context can be set externally.
  36. HeaderFinderPattern DirectiveInput; // The Input can be set externally.
  37. void reset() { // Reset the handler like this:
  38. HeaderDirectives.clear(); // Clear the header directives.
  39. ContextInput.clear(); // Clear the Context Input.
  40. DirectiveInput.clear(); // Clear the Directive Input.
  41. }
  42. };
  43. class HeaderDirectiveInitializer : public CodeDweller::Configurator { // Initializes Header Directives.
  44. private:
  45. HeaderDirectiveHandler* MyTarget; // Needs to know it's target.
  46. public:
  47. HeaderDirectiveInitializer() : MyTarget(NULL) {} // Constructor doesn't know it's target yet.
  48. void setTarget(HeaderDirectiveHandler& H) { MyTarget = &H; } // We have a way to set the target though ;-)
  49. void operator()(CodeDweller::ConfigurationElement& E,
  50. CodeDweller::ConfigurationData& D) { // The configurator() function goes to the
  51. if(NULL!=MyTarget) { // target (if it's set) and pushes the
  52. MyTarget->reset(); // reset button (empties the set).
  53. }
  54. }
  55. };
  56. class HeaderDirectiveWhiteHeaderInitializer : public CodeDweller::Configurator {// Initializes White Header Directives.
  57. private:
  58. HeaderDirectiveHandler* MyTarget; // Needs to know it's target.
  59. public:
  60. HeaderDirectiveWhiteHeaderInitializer() : MyTarget(NULL) {} // Constructor doesn't know it's target yet.
  61. void setTarget(HeaderDirectiveHandler& H) { MyTarget = &H; } // We have a way to set the target though ;-)
  62. void operator()(CodeDweller::ConfigurationElement& E,
  63. CodeDweller::ConfigurationData& D) { // The configurator() function goes to the
  64. if(NULL!=MyTarget) { // target (if it's set) and sets it up
  65. MyTarget->ContextInput.clear(); // for a white header directive.
  66. MyTarget->DirectiveInput.clear();
  67. MyTarget->DirectiveInput.Directive = HeaderDirectiveWhite;
  68. }
  69. }
  70. };
  71. class HeaderDirectiveBypassHeaderInitializer : public CodeDweller::Configurator {// Initializes Bypass Header Directives.
  72. private:
  73. HeaderDirectiveHandler* MyTarget; // Needs to know it's target.
  74. public:
  75. HeaderDirectiveBypassHeaderInitializer() : MyTarget(NULL) {} // Constructor doesn't know it's target yet.
  76. void setTarget(HeaderDirectiveHandler& H) { MyTarget = &H; } // We have a way to set the target though ;-)
  77. void operator()(CodeDweller::ConfigurationElement& E,
  78. CodeDweller::ConfigurationData& D) { // The configurator() function goes to the
  79. if(NULL!=MyTarget) { // target (if it's set) and sets it up
  80. MyTarget->ContextInput.clear(); // for a bypass header directive.
  81. MyTarget->DirectiveInput.clear();
  82. MyTarget->DirectiveInput.Directive = HeaderDirectiveBypass;
  83. }
  84. }
  85. };
  86. class HeaderDirectiveDrilldownInitializer : public CodeDweller::Configurator { // Initializes Drilldown Header Directives.
  87. private:
  88. HeaderDirectiveHandler* MyTarget; // Needs to know it's target.
  89. public:
  90. HeaderDirectiveDrilldownInitializer() : MyTarget(NULL) {} // Constructor doesn't know it's target yet.
  91. void setTarget(HeaderDirectiveHandler& H) { MyTarget = &H; } // We have a way to set the target though ;-)
  92. void operator()(CodeDweller::ConfigurationElement& E,
  93. CodeDweller::ConfigurationData& D) { // The configurator() function goes to the
  94. if(NULL!=MyTarget) { // target (if it's set) and sets it up for
  95. MyTarget->ContextInput.clear(); // a drilldown header directive.
  96. MyTarget->DirectiveInput.clear();
  97. MyTarget->DirectiveInput.Directive = HeaderDirectiveDrillDown;
  98. MyTarget->DirectiveInput.Header = "Received:";
  99. }
  100. }
  101. };
  102. class HeaderDirectiveSourceHeaderInitializer : public CodeDweller::Configurator {// Initializes Source Header Directives.
  103. private:
  104. HeaderDirectiveHandler* MyTarget; // Needs to know it's target.
  105. public:
  106. HeaderDirectiveSourceHeaderInitializer() : MyTarget(NULL) {} // Constructor doesn't know it's target yet.
  107. void setTarget(HeaderDirectiveHandler& H) { MyTarget = &H; } // We have a way to set the target though ;-)
  108. void operator()(CodeDweller::ConfigurationElement& E,
  109. CodeDweller::ConfigurationData& D) { // The configurator() function goes to the
  110. if(NULL!=MyTarget) { // target (if it's set) and sets it up
  111. MyTarget->ContextInput.clear(); // for a context sensitive source header
  112. MyTarget->DirectiveInput.clear(); // directive. Activation context as well
  113. MyTarget->ContextInput.Directive = HeaderDirectiveContext; // as source header data.
  114. MyTarget->ContextInput.Header = "Received:";
  115. MyTarget->DirectiveInput.Directive = HeaderDirectiveSource;
  116. }
  117. }
  118. };
  119. class RangePoint { // Range point x:Probability, y:Confidence
  120. public:
  121. RangePoint() : // The simple constructor sets all to zero.
  122. Probability(0.0),
  123. Confidence(0.0) {}
  124. RangePoint(double C, double P) : // This constructor sets the values.
  125. Probability(P),
  126. Confidence(C) {}
  127. double Probability; // Probability and Confidence are
  128. double Confidence; // freely accessible.
  129. bool operator<(const RangePoint& right) const { // Comparison of RangePoint objects depends
  130. return (Confidence < right.Confidence); // on the Confidence value. This is because
  131. } // Confidence is used as a "key" in the set.
  132. bool operator>(const RangePoint& right) const {
  133. return (Confidence > right.Confidence);
  134. }
  135. bool operator==(const RangePoint& right) const {
  136. return (Confidence == right.Confidence);
  137. }
  138. bool operator<=(const RangePoint& right) const {
  139. return (Confidence <= right.Confidence);
  140. }
  141. bool operator>=(const RangePoint& right) const {
  142. return (Confidence >= right.Confidence);
  143. }
  144. };
  145. class RangeHandler : public CodeDweller::Configurator { // The handler adds edgepoints and holds and
  146. public: // tests the set that defines the region.
  147. void operator()(CodeDweller::ConfigurationElement& E,
  148. CodeDweller::ConfigurationData& D) { // The () operator adds EdgeInput to the list.
  149. EdgeMap.insert(EdgeInput);
  150. }
  151. bool On_Off; // Ranges can be turned on and off.
  152. int Symbol; // They have a symbol assigned to them.
  153. int Priority; // They have an evaluation priority.
  154. RangePoint EdgeInput; // This EdgePoint is set, and added using ().
  155. std::set<RangePoint> EdgeMap; // This contains the set of EdgePoints.
  156. bool isInWhite(RangePoint& x); // True if x is inside the -P of the EdgeMap.
  157. bool isInBlack(RangePoint& x); // True if x is inside the +P of the EdgeMap.
  158. void reset() { EdgeMap.clear(); } // When we reset - we empty the EdgeMap.
  159. };
  160. class RangeInitializer : public CodeDweller::Configurator { // The RangeInitializer Configurator.
  161. private:
  162. RangeHandler* MyTarget; // Needs to know it's target.
  163. public:
  164. RangeInitializer() : MyTarget(NULL) {} // Constructor doesn't know it's target yet.
  165. void setTarget(RangeHandler& H) { MyTarget = &H; } // We have a way to set the target though ;-)
  166. void operator()(CodeDweller::ConfigurationElement& E,
  167. CodeDweller::ConfigurationData& D) { // The configurator() function goes to the
  168. if(NULL!=MyTarget) { // target (if it's set) and pushes the
  169. MyTarget->reset(); // reset button.
  170. }
  171. }
  172. };
  173. class IntegerSetHandler : public CodeDweller::Configurator { // Integer set handler for rule panics.
  174. public:
  175. void operator()(CodeDweller::ConfigurationElement& E,
  176. CodeDweller::ConfigurationData& D) { // The operator() inserts IntegerInput
  177. IntegerSet.insert(IntegerInput); // if it's not already a member.
  178. }
  179. int IntegerInput; // The input port.
  180. std::set<int> IntegerSet; // The set itself.
  181. bool isListed(int x); // How to check if an int is listed.
  182. void reset() { IntegerSet.clear(); } // How to reset (clear) the list.
  183. };
  184. class IntegerSetInitializer : public CodeDweller::Configurator { // The initializer resets the set.
  185. private:
  186. IntegerSetHandler* MyTarget; // It needs to know which set to init.
  187. public:
  188. IntegerSetInitializer() : MyTarget(NULL) {} // Start off not knowing where to go.
  189. void setTarget(IntegerSetHandler& H) { MyTarget = &H; } // Set a pointer to the handler.
  190. void operator()(CodeDweller::ConfigurationElement& E,
  191. CodeDweller::ConfigurationData& D) { // The operator() does the trick.
  192. if(NULL!=MyTarget) {
  193. MyTarget->reset();
  194. }
  195. }
  196. };
  197. class XHDRSymbol { // XHeader associated with a Symbol
  198. public:
  199. int Symbol; // The integer symbol.
  200. std::string Header; // The header to associate.
  201. XHDRSymbol(int FreshSymbol, std::string FreshHeader) : // Creating the object requires both.
  202. Symbol(FreshSymbol),
  203. Header(FreshHeader) {}
  204. bool operator<(const XHDRSymbol& right) const { // To live in a set we must have a <
  205. return (Symbol < right.Symbol); // operator. Only the symbol matters
  206. } // in this case.
  207. };
  208. class XHDRSymbolsHandler : public CodeDweller::Configurator { // XHDRSymbol hander.
  209. public:
  210. std::set<XHDRSymbol> SymbolHeaders; // Carries a set of Symbol Headers.
  211. void reset() { SymbolHeaders.clear(); } // Is reset by clearing the set.
  212. std::string HeaderForSymbol(int S) { // Can return a Header for symbol.
  213. std::string MatchingHeader = ""; // Starting with an empty string,
  214. std::set<XHDRSymbol>::iterator iS = SymbolHeaders.find(XHDRSymbol(S,""));// we look up the symbol and
  215. if(SymbolHeaders.end() != iS) { // if we find it then we will
  216. MatchingHeader = (*iS).Header; // return the matching header
  217. } // string. If not then we return
  218. return MatchingHeader; // the empty string.
  219. } // Coded in-line on purpose.
  220. bool OnOff; // Input OnOff value.
  221. int Symbol; // Input Symbol value.
  222. std::string Header; // Input Header value.
  223. void operator()(CodeDweller::ConfigurationElement& E,
  224. CodeDweller::ConfigurationData& D) { // The operator() inserts an XHDRSymbol
  225. if(OnOff) { // if the header entry is turned on and
  226. SymbolHeaders.insert(XHDRSymbol(Symbol, Header)); // if it's not already a member.
  227. }
  228. }
  229. };
  230. class XHDRSymbolsInitializer : public CodeDweller::Configurator { // The XHDRSymbols initializer.
  231. private:
  232. XHDRSymbolsHandler* MyTarget; // It needs to know which set to init.
  233. public:
  234. XHDRSymbolsInitializer() : MyTarget(NULL) {} // Start off not knowing where to go.
  235. void setTarget(XHDRSymbolsHandler& H) { MyTarget = &H; } // Set a pointer to the handler.
  236. void operator()(CodeDweller::ConfigurationElement& E,
  237. CodeDweller::ConfigurationData& D) { // The operator() does the trick.
  238. if(NULL!=MyTarget) {
  239. MyTarget->reset();
  240. }
  241. }
  242. };
  243. enum snfIPRange { // IP action ranges
  244. Unknown, // Unknown - not defined.
  245. White, // This is a good guy.
  246. Normal, // Benefit of the doubt.
  247. New, // It is new to us.
  248. Caution, // This is suspicious.
  249. Black, // This is bad.
  250. Truncate // Don't even bother looking.
  251. };
  252. const int ScanLogMatches_All = 2; // Include all matches.
  253. const int ScanLogMatches_Unique = 1; // Include 1 match of each rule.
  254. const int ScanLogMatches_None = 0; // Include only the final result.
  255. const int LogOutputMode_None = 0; // No output (don't process).
  256. const int LogOutputMode_API = 1; // Make available to API.
  257. const int LogOutputMode_File = 2; // Output to msgfile.xhdr.
  258. const int LogOutputMode_Inject = 3; // Inject into msgfile.
  259. class snfCFGData { // Object that stores our config data.
  260. private:
  261. CodeDweller::ConfigurationElement MyCFGReader; // This is how we read our cfg data.
  262. public:
  263. snfCFGData(); // Constructor handled in .cpp
  264. void initializeFromFile(const char* FileName); // Initialize from the provided file.
  265. int Generation; // Generation tag.
  266. // Here are the derived data elements...
  267. std::string ConfigFilePath; // Configuration file path
  268. std::string RuleFilePath; // Rulebase file path
  269. std::string SecurityKey; // Security key for rulebase
  270. // Here are the basic data elements...
  271. std::string node_identity;
  272. std::string node_licenseid;
  273. std::string node_authentication;
  274. //// paths
  275. std::string paths_workspace_path;
  276. std::string paths_rulebase_path;
  277. std::string paths_log_path;
  278. //// logging
  279. bool Logs_Rotation_LocalTime_OnOff;
  280. bool Status_SecondReport_Log_OnOff;
  281. bool Status_SecondReport_Append_OnOff;
  282. bool Status_MinuteReport_Log_OnOff;
  283. bool Status_MinuteReport_Append_OnOff;
  284. bool Status_HourReport_Log_OnOff;
  285. bool Status_HourReport_Append_OnOff;
  286. bool Scan_Identifier_Force_Message_Id;
  287. int Scan_Classic_Mode;
  288. bool Scan_Classic_Rotate;
  289. int Scan_Classic_Matches;
  290. int Scan_XML_Mode;
  291. bool Scan_XML_Rotate;
  292. int Scan_XML_Matches;
  293. bool Scan_XML_Performance;
  294. bool Scan_XML_GBUdb;
  295. //// xheaders
  296. int XHDROutput_Mode;
  297. bool XHDRVersion_OnOff;
  298. std::string XHDRVersion_Header;
  299. bool XHDRLicense_OnOff;
  300. std::string XHDRLicense_Header;
  301. bool XHDRRulebase_OnOff;
  302. std::string XHDRRulebase_Header;
  303. bool XHDRIdentifier_OnOff;
  304. std::string XHDRIdentifier_Header;
  305. bool XHDRGBUdb_OnOff;
  306. std::string XHDRGBUdb_Header;
  307. bool XHDRResult_OnOff;
  308. std::string XHDRResult_Header;
  309. bool XHDRMatches_OnOff;
  310. std::string XHDRMatches_Header;
  311. bool XHDRBlack_OnOff;
  312. std::string XHDRBlack_Header;
  313. bool XHDRWhite_OnOff;
  314. std::string XHDRWhite_Header;
  315. bool XHDRClean_OnOff;
  316. std::string XHDRClean_Header;
  317. XHDRSymbolsHandler XHDRSymbolHeaders;
  318. XHDRSymbolsInitializer XHDRSymbolHeadersInitializer;
  319. //// platform
  320. std::string PlatformElementContents;
  321. //// network
  322. int network_sync_secs;
  323. std::string network_sync_host;
  324. int network_sync_port;
  325. bool update_script_on_off;
  326. std::string update_script_call;
  327. int update_script_guard_time;
  328. //// gbudb
  329. int gbudb_database_condense_minimum_seconds_between;
  330. bool gbudb_database_condense_time_trigger_on_off;
  331. int gbudb_database_condense_time_trigger_seconds;
  332. bool gbudb_database_condense_posts_trigger_on_off;
  333. int gbudb_database_condense_posts_trigger_posts;
  334. bool gbudb_database_condense_records_trigger_on_off;
  335. int gbudb_database_condense_records_trigger_records;
  336. bool gbudb_database_condense_size_trigger_on_off;
  337. int gbudb_database_condense_size_trigger_megabytes;
  338. bool gbudb_database_checkpoint_on_off;
  339. int gbudb_database_checkpoint_secs;
  340. RangeHandler WhiteRangeHandler;
  341. RangeInitializer WhiteRangeInitializer;
  342. bool gbudb_regions_white_panic_on_off;
  343. int gbudb_regions_white_panic_rule_range;
  344. RangeHandler BlackRangeHandler;
  345. RangeInitializer BlackRangeInitializer;
  346. bool gbudb_regions_black_sample_on_off;
  347. double gbudb_regions_black_sample_probability;
  348. int gbudb_regions_black_sample_grab_one_in;
  349. bool gbudb_regions_black_sample_passthrough;
  350. int gbudb_regions_black_sample_passthrough_symbol;
  351. int gbudb_regions_black_truncate_symbol;
  352. bool gbudb_regions_black_truncate_on_off;
  353. double gbudb_regions_black_truncate_probability;
  354. int gbudb_regions_black_truncate_peek_one_in;
  355. RangeHandler CautionRangeHandler;
  356. RangeInitializer CautionRangeInitializer;
  357. snfIPRange RangeEvaluation(GBUdbRecord& R); // Returns the range for a GBUdbRecord.
  358. snfIPRange RangeEvaluation(RangePoint& p); // Returns the range for a RangePoint.
  359. HeaderDirectiveHandler HeaderDirectivesHandler; //** Handles header directives.
  360. HeaderDirectiveInitializer HeaderDirectivesInitializer; //** Initializes header directives set.
  361. HeaderDirectiveSourceHeaderInitializer HDSourceHeaderInitializer; //**** For source header directives.
  362. HeaderDirectiveDrilldownInitializer HDDrilldownInitializer; //**** For drilldown header directives.
  363. HeaderDirectiveBypassHeaderInitializer HDBypassHeaderInitializer; //**** For bypass header directives.
  364. HeaderDirectiveWhiteHeaderInitializer HDWhiteHeaderInitializer; //**** For white header directives.
  365. IntegerSetHandler TrainingBypassRuleHandler; // Rules to NOT train GBUdb with source.
  366. IntegerSetInitializer TrainingBypassRuleInitializer;
  367. IntegerSetHandler TrainingWhiteRuleHandler; // Rules to train GBUdb as white source.
  368. IntegerSetInitializer TrainingWhiteRuleInitializer;
  369. bool GBUdbTrainingOn_Off; // True when GBUdb training is allowed.
  370. IntegerSetHandler RulePanicHandler;
  371. IntegerSetInitializer RulePanicInitializer;
  372. bool XCI_OnOff; // XML Command Interface ON or OFF.
  373. int XCI_Port; // XML Command Interface Port number.
  374. bool MessageFileTypeCGP_on_off; // True for scanning communigate msgs.
  375. };
  376. class snfCFGmgr { // Object that manages our config data.
  377. private:
  378. CodeDweller::Mutex myMutex; // Serialize control during updates.
  379. snfCFGData A; // This is where we store one copy.
  380. snfCFGData B; // This is where we store the other.
  381. volatile bool AisActive; // This tells us which is active.
  382. void swapCFGData(); // This swaps the active dataset.
  383. snfCFGData& ActiveData(); // This returns the active dataset.
  384. snfCFGData& InactiveData(); // This returns the inactive dataset.
  385. std::string InitFileName; // Initilization parameters are reused
  386. std::string InitLicenseId; // any time load() is called.
  387. std::string InitAuthentication;
  388. std::string ConfigurationPath; // Path to active configuration file.
  389. public:
  390. snfCFGmgr(); // Constructor - to get things right
  391. void initialize( // In order to initialize we need to
  392. const char* FileName, // collect a path to our config or .snf
  393. const char* LicenseId, // our license id and our
  394. const char* Authentication // authentication.
  395. );
  396. class LoadFailure {}; // What we throw if load fails.
  397. void load(); // Load the configuration data.
  398. //// Access methods for config data...
  399. std::string RuleFilePath(); // Rulebase file path
  400. std::string SecurityKey(); // Security key for rulebase
  401. snfCFGData* ActiveConfiguration(); // Pointer to active configuration
  402. };
  403. #include "snfCFGmgr.inline.hpp"
  404. } // namespace SNFMulti
  405. #endif
  406. // End include only once