Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. // snfCFGmgr.cpp
  2. // Copyright (C) 2006 - 2020 Arm Research Labs, LLC
  3. // See www.armresearch.com for the copyright terms.
  4. //
  5. // See snfCFGmgr.hpp for details.
  6. #include "snfCFGmgr.hpp"
  7. #include <iostream>
  8. namespace cd = codedweller;
  9. //// IntegerSetHandler /////////////////////////////////////////////////////////
  10. bool IntegerSetHandler::isListed(int x) { // How to check if an int is listed.
  11. return (IntegerSet.end() != IntegerSet.find(x));
  12. }
  13. //// snfCFGmgr /////////////////////////////////////////////////////////////////
  14. snfCFGmgr::snfCFGmgr() : // We construct a CFGmgr this way...
  15. AisActive(false), // So that A is active after 1st load()
  16. InitFileName(""), // and all of the Init strings are
  17. InitLicenseId(""), // empty.
  18. InitAuthentication(""),
  19. ConfigurationPath("") {
  20. }
  21. void snfCFGmgr::swapCFGData() { // This swaps the active dataset.
  22. AisActive = (AisActive)?false:true;
  23. }
  24. snfCFGData& snfCFGmgr::ActiveData() { // This returns the active dataset.
  25. return (AisActive) ? A : B;
  26. }
  27. snfCFGData& snfCFGmgr::InactiveData() { // This returns the inactive dataset.
  28. return (AisActive) ? B : A;
  29. }
  30. std::string snfCFGmgr::RuleFilePath() { // Rulebase file path
  31. return ActiveData().RuleFilePath;
  32. }
  33. std::string snfCFGmgr::SecurityKey() { // Security key for rulebase
  34. return ActiveData().SecurityKey;
  35. }
  36. snfCFGData* snfCFGmgr::ActiveConfiguration() { // Pointer to active configuration
  37. return &(ActiveData());
  38. }
  39. //// RangeHandler //////////////////////////////////////////////////////////////
  40. bool RangeHandler::isInBlack(RangePoint& x) { // Find if x is on the black side.
  41. if(EdgeMap.empty()) { // If there are no points then
  42. return false; // there is no map so there is
  43. } // no side to be on.
  44. // If there are points we will need
  45. std::set<RangePoint>::iterator iRangePoint; // to examine them.
  46. iRangePoint = EdgeMap.begin(); // What is the first point.
  47. if(x < (*iRangePoint)) { // If x is below that then
  48. return false; // x is out of range -- false.
  49. }
  50. iRangePoint = EdgeMap.end();--iRangePoint; // What is the last range point.
  51. if(x > (*iRangePoint)) { // If x is beyond that then
  52. return false; // x is out of range -- false.
  53. }
  54. // At this point we know our point is in the range of the edge map.
  55. // So our next task is to find the two points between which we will
  56. // interpolate our comparative result.
  57. iRangePoint = EdgeMap.lower_bound(x); // Find the lower point.
  58. if(x < (*iRangePoint)) --iRangePoint; // If we've overshot, then move back.
  59. RangePoint LowerBound = (*iRangePoint); // Grab the value at that point.
  60. iRangePoint = EdgeMap.upper_bound(x); // Find the upper point.
  61. if(iRangePoint == EdgeMap.end()) --iRangePoint; // If we've overshot, then move back.
  62. RangePoint UpperBound = (*iRangePoint); // Grab the value at that point.
  63. // So then, where is x in [Lower, Upper]
  64. // First we check the obvious matching values. Then if those fail we will
  65. // interpolate between the two points.
  66. double ComparativeProbability; // This value will map the edge.
  67. if(x == LowerBound) { // If we match the lower bound then
  68. ComparativeProbability = LowerBound.Probability; // that is the Probability we compare.
  69. } else
  70. if(x == UpperBound) { // If we match the upper bound then
  71. ComparativeProbability = UpperBound.Probability; // that is the Probability we compare.
  72. } else { // For in-between we interpolate.
  73. double ULDifference = UpperBound.Confidence - LowerBound.Confidence; // First, find the difference.
  74. double Incursion = x.Confidence - LowerBound.Confidence; // How far does x go past L to U?
  75. double Ratio = Incursion / ULDifference; // Express that as a ratio.
  76. ComparativeProbability = // Interpolate the Probability using
  77. (((1-Ratio) * LowerBound.Probability) + // a weighted average of the lower and
  78. (Ratio * UpperBound.Probability)); // upper bound values using the Ratio
  79. }
  80. // Now compare x to the interpolated edge.
  81. return (x.Probability >= ComparativeProbability); // True if on or right of the edge.
  82. }
  83. bool RangeHandler::isInWhite(RangePoint& x) {
  84. if(EdgeMap.empty()) { // If there are no points then
  85. return false; // there is no map so there is
  86. } // no side to be on.
  87. // If ther are points then we
  88. std::set<RangePoint>::iterator iRangePoint; // need to examine them.
  89. iRangePoint = EdgeMap.begin(); // What is the first point.
  90. if(x < (*iRangePoint)) { // If x is below that then
  91. return false; // x is out of range -- false.
  92. }
  93. iRangePoint = EdgeMap.end();--iRangePoint; // What is the last range point.
  94. if(x > (*iRangePoint)) { // If x is beyond that then
  95. return false; // x is out of range -- false.
  96. }
  97. // At this point we know our point is in the range of the edge map.
  98. // So our next task is to find the two points between which we will
  99. // interpolate our comparative result.
  100. iRangePoint = EdgeMap.lower_bound(x); // Find the lower point.
  101. if(x < (*iRangePoint)) --iRangePoint; // If we've overshot, then move back.
  102. RangePoint LowerBound = (*iRangePoint); // Grab the value at that point.
  103. iRangePoint = EdgeMap.upper_bound(x); // Find the upper point.
  104. if(iRangePoint == EdgeMap.end()) --iRangePoint; // If we've overshot, then move back.
  105. RangePoint UpperBound = (*iRangePoint); // Grab the value at that point.
  106. // So then, where is x in [Lower, Upper]
  107. // First we check the obvious matching values. Then if those fail we will
  108. // interpolate between the two points.
  109. double ComparativeProbability; // This value will map the edge.
  110. if(x == LowerBound) { // If we match the lower bound then
  111. ComparativeProbability = LowerBound.Probability; // that is the Probability we compare.
  112. } else
  113. if(x == UpperBound) { // If we match the upper bound then
  114. ComparativeProbability = UpperBound.Probability; // that is the Probability we compare.
  115. } else { // For in-between we interpolate.
  116. double ULDifference = UpperBound.Confidence - LowerBound.Confidence; // First, find the difference.
  117. double Incursion = x.Confidence - LowerBound.Confidence; // How far does x go past L to U?
  118. double Ratio = Incursion / ULDifference; // Express that as a ratio.
  119. ComparativeProbability = // Interpolate the Probability using
  120. (((1-Ratio) * LowerBound.Probability) + // a weighted average of the lower and
  121. (Ratio * UpperBound.Probability)); // upper bound values using the Ratio
  122. }
  123. // Now compare x to the interpolated edge.
  124. return (x.Probability <= ComparativeProbability); // True if on or left of the edge.
  125. }
  126. //// snfCFGData ////////////////////////////////////////////////////////////////
  127. snfCFGData::snfCFGData() : // Constructor. No init list because the
  128. MyCFGReader("snf") { // interpreter will set the defaults.
  129. WhiteRangeInitializer.setTarget(WhiteRangeHandler); // However, we do need to link up our
  130. BlackRangeInitializer.setTarget(BlackRangeHandler); // Initialization configurators with our
  131. CautionRangeInitializer.setTarget(CautionRangeHandler); // Handlers.
  132. RulePanicInitializer.setTarget(RulePanicHandler);
  133. XHDRSymbolHeadersInitializer.setTarget(XHDRSymbolHeaders);
  134. HeaderDirectivesInitializer.setTarget(HeaderDirectivesHandler);
  135. HDSourceHeaderInitializer.setTarget(HeaderDirectivesHandler);
  136. HDDrilldownInitializer.setTarget(HeaderDirectivesHandler);
  137. HDBypassHeaderInitializer.setTarget(HeaderDirectivesHandler);
  138. HDWhiteHeaderInitializer.setTarget(HeaderDirectivesHandler);
  139. TrainingBypassRuleInitializer.setTarget(TrainingBypassRuleHandler);
  140. TrainingWhiteRuleInitializer.setTarget(TrainingWhiteRuleHandler);
  141. MyCFGReader // Building our interpreter.
  142. .Element("node")
  143. .Attribute("identity", node_identity)
  144. .Attribute("licenseid", node_licenseid)
  145. .Attribute("authentication", node_authentication)
  146. .Element("paths")
  147. .Element("workspace")
  148. .Attribute("path", paths_workspace_path)
  149. .End("workspace")
  150. .Element("rulebase")
  151. .Attribute("path", paths_rulebase_path)
  152. .End("rulebase")
  153. .Element("log")
  154. .Attribute("path", paths_log_path)
  155. .End("log")
  156. .End("paths")
  157. .Element("logs")
  158. .Element("rotation")
  159. .Attribute("localtime", Logs_Rotation_LocalTime_OnOff, false)
  160. .Mnemonic("yes", "true")
  161. .Mnemonic("no", "false")
  162. .End("rotation")
  163. .Element("status")
  164. .Element("second")
  165. .Attribute("log", Status_SecondReport_Log_OnOff, false)
  166. .Mnemonic("yes", "true")
  167. .Mnemonic("no", "false")
  168. .Attribute("append", Status_SecondReport_Append_OnOff, false)
  169. .Mnemonic("yes", "true")
  170. .Mnemonic("no", "false")
  171. .End("second")
  172. .Element("minute")
  173. .Attribute("log", Status_MinuteReport_Log_OnOff, false)
  174. .Mnemonic("yes", "true")
  175. .Mnemonic("no", "false")
  176. .Attribute("append", Status_MinuteReport_Append_OnOff, false)
  177. .Mnemonic("yes", "true")
  178. .Mnemonic("no", "false")
  179. .End("minute")
  180. .Element("hour")
  181. .Attribute("log", Status_HourReport_Log_OnOff, false)
  182. .Mnemonic("yes", "true")
  183. .Mnemonic("no", "false")
  184. .Attribute("append", Status_HourReport_Append_OnOff, false)
  185. .Mnemonic("yes", "true")
  186. .Mnemonic("no", "false")
  187. .End("hour")
  188. .End("status")
  189. .Element("scan")
  190. .Element("identifier")
  191. .Attribute("force-message-id", Scan_Identifier_Force_Message_Id, false)
  192. .End("identifier")
  193. .Element("classic")
  194. .Attribute("mode", Scan_Classic_Mode, LogOutputMode_None)
  195. .Mnemonic("none", "0")
  196. .Mnemonic("api", "1")
  197. .Mnemonic("file", "2")
  198. .Attribute("rotate", Scan_Classic_Rotate, false)
  199. .Attribute("matches", Scan_Classic_Matches, ScanLogMatches_None)
  200. .Mnemonic("none", "0")
  201. .Mnemonic("unique", "1")
  202. .Mnemonic("all","2")
  203. .End("classic")
  204. .Element("xml")
  205. .Attribute("mode", Scan_XML_Mode, LogOutputMode_None)
  206. .Mnemonic("none", "0")
  207. .Mnemonic("api", "1")
  208. .Mnemonic("file", "2")
  209. .Attribute("rotate", Scan_XML_Rotate, false)
  210. .Attribute("matches", Scan_XML_Matches, ScanLogMatches_None)
  211. .Mnemonic("none", "0")
  212. .Mnemonic("unique", "1")
  213. .Mnemonic("all","2")
  214. .Attribute("performance", Scan_XML_Performance, false)
  215. .Attribute("gbudb", Scan_XML_GBUdb, false)
  216. .End("xml")
  217. .Element("xheaders")
  218. .atStartCall(XHDRSymbolHeadersInitializer)
  219. .Element("output")
  220. .Attribute("mode", XHDROutput_Mode, LogOutputMode_None)
  221. .Mnemonic("none", "0")
  222. .Mnemonic("api", "1")
  223. .Mnemonic("file", "2")
  224. .Mnemonic("inject", "3")
  225. .End("output")
  226. .Element("symbol", XHDRSymbolHeaders.Header, "")
  227. .atEndCall(XHDRSymbolHeaders)
  228. .Attribute("on-off", XHDRSymbolHeaders.OnOff, false)
  229. .Mnemonic("on", "true")
  230. .Mnemonic("off", "false")
  231. .Attribute("n", XHDRSymbolHeaders.Symbol, -1)
  232. .End("symbol")
  233. .Element("version", XHDRVersion_Header, "")
  234. .Attribute("on-off", XHDRVersion_OnOff, false)
  235. .Mnemonic("on", "true")
  236. .Mnemonic("off", "false")
  237. .End("version")
  238. .Element("license", XHDRLicense_Header, "")
  239. .Attribute("on-off", XHDRLicense_OnOff, false)
  240. .Mnemonic("on", "true")
  241. .Mnemonic("off", "false")
  242. .End("license")
  243. .Element("rulebase", XHDRRulebase_Header, "")
  244. .Attribute("on-off", XHDRRulebase_OnOff, false)
  245. .Mnemonic("on", "true")
  246. .Mnemonic("off", "false")
  247. .End("rulebase")
  248. .Element("identifier", XHDRIdentifier_Header, "")
  249. .Attribute("on-off", XHDRIdentifier_OnOff, false)
  250. .Mnemonic("on", "true")
  251. .Mnemonic("off", "false")
  252. .End("identifier")
  253. .Element("gbudb", XHDRGBUdb_Header, "")
  254. .Attribute("on-off", XHDRGBUdb_OnOff, false)
  255. .Mnemonic("on", "true")
  256. .Mnemonic("off", "false")
  257. .End("gbudb")
  258. .Element("result", XHDRResult_Header, "")
  259. .Attribute("on-off", XHDRResult_OnOff, false)
  260. .Mnemonic("on", "true")
  261. .Mnemonic("off", "false")
  262. .End("result")
  263. .Element("matches", XHDRMatches_Header, "")
  264. .Attribute("on-off", XHDRMatches_OnOff, false)
  265. .Mnemonic("on", "true")
  266. .Mnemonic("off", "false")
  267. .End("matches")
  268. .Element("black", XHDRBlack_Header, "")
  269. .Attribute("on-off", XHDRBlack_OnOff, false)
  270. .Mnemonic("on", "true")
  271. .Mnemonic("off", "false")
  272. .End("black")
  273. .Element("white", XHDRWhite_Header, "")
  274. .Attribute("on-off", XHDRWhite_OnOff, false)
  275. .Mnemonic("on", "true")
  276. .Mnemonic("off", "false")
  277. .End("white")
  278. .Element("clean", XHDRClean_Header, "")
  279. .Attribute("on-off", XHDRClean_OnOff, false)
  280. .Mnemonic("on", "true")
  281. .Mnemonic("off", "false")
  282. .End("clean")
  283. .End("xheaders")
  284. .End("scan")
  285. .End("logs")
  286. .Element("network")
  287. .Element("sync")
  288. .Attribute("secs", network_sync_secs, 30)
  289. .Attribute("host", network_sync_host, "sync.messagesniffer.net")
  290. .Attribute("port", network_sync_port, 25)
  291. .End("sync")
  292. .Element("update-script")
  293. .Attribute("on-off", update_script_on_off, false)
  294. .Mnemonic("on", "true")
  295. .Mnemonic("off", "false")
  296. .Attribute("call", update_script_call, "")
  297. .Attribute("guard-time", update_script_guard_time, 180)
  298. .End("update-script")
  299. .End("network")
  300. .Element("xci")
  301. .Attribute("on-off", XCI_OnOff, true)
  302. .Mnemonic("on", "true")
  303. .Mnemonic("off", "false")
  304. .Attribute("port", XCI_Port, 9001)
  305. .End("xci")
  306. .Element("gbudb")
  307. .Element("database")
  308. .Element("condense")
  309. .Attribute("minimum-seconds-between", gbudb_database_condense_minimum_seconds_between, 600)
  310. .Element("time-trigger")
  311. .Attribute("on-off", gbudb_database_condense_time_trigger_on_off, true)
  312. .Mnemonic("on", "true")
  313. .Mnemonic("off", "false")
  314. .Attribute("seconds", gbudb_database_condense_time_trigger_seconds, 84600)
  315. .End("time-trigger")
  316. .Element("posts-trigger")
  317. .Attribute("on-off", gbudb_database_condense_posts_trigger_on_off, false)
  318. .Mnemonic("on", "true")
  319. .Mnemonic("off", "false")
  320. .Attribute("posts", gbudb_database_condense_posts_trigger_posts, 32768)
  321. .End("posts-trigger")
  322. .Element("records-trigger")
  323. .Attribute("on-off", gbudb_database_condense_records_trigger_on_off, false)
  324. .Mnemonic("on", "true")
  325. .Mnemonic("off", "false")
  326. .Attribute("records", gbudb_database_condense_records_trigger_records, 150000)
  327. .End("records-trigger")
  328. .Element("size-trigger")
  329. .Attribute("on-off", gbudb_database_condense_size_trigger_on_off, false)
  330. .Mnemonic("on", "true")
  331. .Mnemonic("off", "false")
  332. .Attribute("megabytes", gbudb_database_condense_size_trigger_megabytes, 150)
  333. .End("size-trigger")
  334. .End("condense")
  335. .Element("checkpoint")
  336. .Attribute("on-off", gbudb_database_checkpoint_on_off, true)
  337. .Mnemonic("on", "true")
  338. .Mnemonic("off", "false")
  339. .Attribute("secs", gbudb_database_checkpoint_secs, 3600)
  340. .End("checkpoint")
  341. .End("database")
  342. .Element("regions")
  343. .Element("white")
  344. .atStartCall(WhiteRangeInitializer)
  345. .Attribute("on-off", WhiteRangeHandler.On_Off, true)
  346. .Mnemonic("on", "true")
  347. .Mnemonic("off", "false")
  348. .Attribute("symbol", WhiteRangeHandler.Symbol, 0)
  349. .Attribute("priority", WhiteRangeHandler.Priority, 1)
  350. .Element("edge")
  351. .atEndCall(WhiteRangeHandler)
  352. .Attribute("probability", WhiteRangeHandler.EdgeInput.Probability, 0.0)
  353. .Attribute("confidence", WhiteRangeHandler.EdgeInput.Confidence, 0.0)
  354. .End("edge")
  355. .Element("panic")
  356. .Attribute("on-off", gbudb_regions_white_panic_on_off, true)
  357. .Mnemonic("on", "true")
  358. .Mnemonic("off", "false")
  359. .Attribute("rule-range", gbudb_regions_white_panic_rule_range, 1000)
  360. .End("panic")
  361. .End("white")
  362. .Element("black")
  363. .atStartCall(BlackRangeInitializer)
  364. .Attribute("on-off", BlackRangeHandler.On_Off, true)
  365. .Mnemonic("on", "true")
  366. .Mnemonic("off", "false")
  367. .Attribute("symbol", BlackRangeHandler.Symbol, 63)
  368. .mapTo(gbudb_regions_black_truncate_symbol, 63)
  369. .Attribute("priority", BlackRangeHandler.Priority, 2)
  370. .Element("edge")
  371. .atEndCall(BlackRangeHandler)
  372. .Attribute("probability", BlackRangeHandler.EdgeInput.Probability, 0.0)
  373. .Attribute("confidence", BlackRangeHandler.EdgeInput.Confidence, 0.0)
  374. .End("edge")
  375. .Element("truncate")
  376. .Attribute("on-off", gbudb_regions_black_truncate_on_off, true)
  377. .Mnemonic("on", "true")
  378. .Mnemonic("off", "false")
  379. .Attribute("probability", gbudb_regions_black_truncate_probability, 0.5)
  380. .Attribute("peek-one-in", gbudb_regions_black_truncate_peek_one_in, 3)
  381. .Attribute("symbol", gbudb_regions_black_truncate_symbol, 63)
  382. .End("truncate")
  383. .Element("sample")
  384. .Attribute("on-off", gbudb_regions_black_sample_on_off, true)
  385. .Mnemonic("on", "true")
  386. .Mnemonic("off", "false")
  387. .Attribute("probability", gbudb_regions_black_sample_probability, 0.5)
  388. .Attribute("grab-one-in", gbudb_regions_black_sample_grab_one_in, 10)
  389. .Attribute("passthrough", gbudb_regions_black_sample_passthrough, false)
  390. .Attribute("passthrough-symbol", gbudb_regions_black_sample_passthrough_symbol, 0)
  391. .End("sample")
  392. .End("black")
  393. .Element("caution")
  394. .atStartCall(CautionRangeInitializer)
  395. .Attribute("on-off", CautionRangeHandler.On_Off, true)
  396. .Mnemonic("on", "true")
  397. .Mnemonic("off", "false")
  398. .Attribute("symbol", CautionRangeHandler.Symbol, 30)
  399. .Attribute("priority", CautionRangeHandler.Priority, 3)
  400. .Element("edge")
  401. .atEndCall(CautionRangeHandler)
  402. .Attribute("probability", CautionRangeHandler.EdgeInput.Probability, 0.0)
  403. .Attribute("confidence", CautionRangeHandler.EdgeInput.Confidence, 0.0)
  404. .End("edge")
  405. .End("caution")
  406. .End("regions")
  407. .Element("training")
  408. .atStartCall(HeaderDirectivesInitializer)
  409. .Attribute("on-off", GBUdbTrainingOn_Off, true)
  410. .Mnemonic("on", "true")
  411. .Mnemonic("off", "false")
  412. .Element("source")
  413. .Element("header")
  414. .atStartCall(HDSourceHeaderInitializer)
  415. .atEndCall(HeaderDirectivesHandler)
  416. .Attribute("name", HeaderDirectivesHandler.DirectiveInput.Header, "\n\n")
  417. .Attribute("received", HeaderDirectivesHandler.ContextInput.Contains, "\n\n")
  418. .Attribute("ordinal", HeaderDirectivesHandler.ContextInput.Ordinal, 0)
  419. .End("header")
  420. .End("source")
  421. .Element("drilldown")
  422. .Element("received")
  423. .atStartCall(HDDrilldownInitializer)
  424. .atEndCall(HeaderDirectivesHandler)
  425. .Attribute("ordinal", HeaderDirectivesHandler.DirectiveInput.Ordinal, 0)
  426. .Attribute("find", HeaderDirectivesHandler.DirectiveInput.Contains, "\n\n")
  427. .End("received")
  428. .End("drilldown")
  429. .Element("bypass")
  430. .atStartCall(TrainingBypassRuleInitializer)
  431. .Element("result")
  432. .atEndCall(TrainingBypassRuleHandler)
  433. .Attribute("code", TrainingBypassRuleHandler.IntegerInput,-1)
  434. .End("result")
  435. .Element("header")
  436. .atStartCall(HDBypassHeaderInitializer)
  437. .atEndCall(HeaderDirectivesHandler)
  438. .Attribute("name", HeaderDirectivesHandler.DirectiveInput.Header, "\n\n")
  439. .Attribute("ordinal", HeaderDirectivesHandler.DirectiveInput.Ordinal, 0)
  440. .Attribute("find", HeaderDirectivesHandler.DirectiveInput.Contains, "\n\n")
  441. .End("header")
  442. .End("bypass")
  443. .Element("white")
  444. .atStartCall(TrainingWhiteRuleInitializer)
  445. .Element("result")
  446. .atEndCall(TrainingWhiteRuleHandler)
  447. .Attribute("code", TrainingWhiteRuleHandler.IntegerInput,-1)
  448. .End("result")
  449. .Element("header")
  450. .atStartCall(HDWhiteHeaderInitializer)
  451. .atEndCall(HeaderDirectivesHandler)
  452. .Attribute("name", HeaderDirectivesHandler.DirectiveInput.Header, "\n\n")
  453. .Attribute("ordinal", HeaderDirectivesHandler.DirectiveInput.Ordinal, 0)
  454. .Attribute("find", HeaderDirectivesHandler.DirectiveInput.Contains, "\n\n")
  455. .End("header")
  456. .End("white")
  457. .End("training")
  458. .End("gbudb")
  459. .Element("rule-panics")
  460. .atStartCall(RulePanicInitializer)
  461. .Element("rule")
  462. .atEndCall(RulePanicHandler)
  463. .Attribute("id", RulePanicHandler.IntegerInput, -1)
  464. .End("rule")
  465. .End("rule-panics")
  466. .Element("platform", PlatformElementContents, "")
  467. .End("platform")
  468. .Element("msg-file")
  469. .Attribute("type", MessageFileTypeCGP_on_off, false)
  470. .Mnemonic("cgp", "true")
  471. .End("msg-file")
  472. .End("node")
  473. .End("snf");
  474. }
  475. void fixPathTermination(std::string& s) { // Ensure s ends in a / or a \ as needed.
  476. if(0 == s.length()) return; // If the string is empty we do nothing.
  477. // Determine what our path terminator should be by looking to
  478. // see what separator has already been used.
  479. char Terminator; // This will be our terminator.
  480. if(std::string::npos == s.find('\\')) { // If we're not using a backslash then
  481. Terminator = '/'; // we will use the forward slash.
  482. } else { // If we are using the backslash then
  483. Terminator = '\\'; // we will remain consistent and terminate
  484. } // with a backslash.
  485. // If the path that's given doesn't have a terminator then we will add
  486. // the appropriate separator to the end.
  487. if( // If the string is
  488. '\\' != s.at(s.length()-1) && // not terminated by a backslash nor
  489. '/' != s.at(s.length()-1) // by a forward slash then
  490. ) { // we will append an appropriate
  491. s.append(1,Terminator); // terminator. Otherwise we will
  492. } // leave it as it is.
  493. }
  494. void snfCFGData::initializeFromFile(const char* FileName) { // Initialize from the provided file.
  495. cd::ConfigurationData MyCFGData(FileName); // Create a cfg data object from the file.
  496. if(0 == MyCFGData.Data(0)) throw false; // If we didn't read a config file throw!
  497. MyCFGReader.initialize(); // Initialize to defaults.
  498. MyCFGReader.interpret(MyCFGData); // Interpret the data.
  499. fixPathTermination(paths_log_path); // Automagically fix / or \ termination
  500. fixPathTermination(paths_rulebase_path); // for the paths provided in the
  501. fixPathTermination(paths_workspace_path); // configuration <path/> section.
  502. ConfigFilePath = FileName; // Set the ConfigFilePath for what we read.
  503. }
  504. snfIPRange snfCFGData::RangeEvaluation(GBUdbRecord& R) { // Returns the range for a GBUdbRecord.
  505. if(Good == R.Flag()) { // If the flag on the IP is Good
  506. return snfIPRange::White; // then this IP is automatically white.
  507. } else
  508. if(Bad == R.Flag()) { // If the flag on this IP is Bad
  509. if(true == gbudb_regions_black_truncate_on_off) { // and truncate is turned on then
  510. return snfIPRange::Truncate; // the IP is automatically in the
  511. } else { // truncate range. If truncate is off
  512. return snfIPRange::Black; // then this IP is automatically black.
  513. }
  514. }
  515. // If it's not so simple then get a
  516. RangePoint P(R.Confidence(), R.Probability()); // range point and evaluate it that way.
  517. return RangeEvaluation(P);
  518. }
  519. snfIPRange snfCFGData::RangeEvaluation(RangePoint& p) { // Returns the range for a RangePoint.
  520. if( // If the IP is unknown, indicated
  521. 0.0 == p.Confidence && // by a zero confidence and
  522. 0.0 == p.Probability // a zero probability, then
  523. ) { // the range point cannot be "in"
  524. return snfIPRange::New; // any range.
  525. }
  526. if(WhiteRangeHandler.isInWhite(p)) { // If it's in the white range,
  527. return snfIPRange::White; // return White.
  528. } else // White has priority over all others.
  529. if(BlackRangeHandler.isInBlack(p)) { // If it's in the black range then
  530. if(p.Probability >= gbudb_regions_black_truncate_probability) { // determine if it's also in the truncate
  531. return snfIPRange::Truncate; // range, and if so - send back Truncate.
  532. } else { // If not then we can send back a
  533. return snfIPRange::Black; // normal black result.
  534. }
  535. } else // Black takes precedence over caution.
  536. if(CautionRangeHandler.isInBlack(p)) { // If we're in the caution range
  537. return snfIPRange::Caution; // then return caution.
  538. } // If none of those ranges matched then
  539. return snfIPRange::Normal; // the IP is in the normal range.
  540. }
  541. //// snfCFGmgr /////////////////////////////////////////////////////////////////
  542. void snfCFGmgr::initialize( // Initialize our configuration data.
  543. const char* FileName,
  544. const char* LicenseId,
  545. const char* Authentication) {
  546. // Check for NULLs and assign Init parameters
  547. InitFileName = (NULL==FileName)?"":FileName; // Initilization parameters are reused
  548. InitLicenseId = (NULL==LicenseId)?"":LicenseId; // any time load() is called.
  549. InitAuthentication = (NULL==Authentication)?"":Authentication;
  550. }
  551. //*****************************************************************************
  552. //// IMPORTANT: If the authentication string is provided in the initialize() it
  553. //// MUST NOT be put into D.node_authentication.
  554. //*****************************************************************************
  555. //// When the license ID and security string come from an OEM application they
  556. //// may not appear in the configuration files. If that is the case we will assume
  557. //// that they developer wants to keep the security string secret by encrypting it
  558. //// in their application and providing it to SNF at runtime. In that case we will
  559. //// not display the security key in the configuration log.
  560. ////
  561. //// To prevent hacking attempts, if the authentication information appears to be
  562. //// provided by configuration data then we will build the string from that data.
  563. //// that way an attacker can't trick the application into disclosing the true
  564. //// authentication string -- they will only get out what they put in.
  565. std::string SecurityKeyDisplayString(snfCFGData& D) { // Returns appropriate SecurityKey: data
  566. std::string ConfigLogSecurityKey = "************************"; // Start with a masked display.
  567. if(0 < D.node_authentication.length()) { // If auth info is in the config files then
  568. ConfigLogSecurityKey = D.node_licenseid + D.node_authentication; // build up the key from that data so it
  569. } // can be displayed in the config log.
  570. return ConfigLogSecurityKey;
  571. }
  572. void logCFGData(snfCFGData& D) { // Log interpreted cfg data (debug aid).
  573. try {
  574. std::string CFGLogPath; // Build the snf_cfg log path.
  575. CFGLogPath = D.paths_log_path +
  576. D.node_licenseid + "_snf_engine_cfg.log";
  577. std::ofstream cfgl(CFGLogPath.c_str(), std::ios::trunc); // Open and truncate the cfg log file.
  578. cfgl // Report important cfg information.
  579. << "SNF Engine Configuration" << std::endl
  580. << "____________" << std::endl
  581. << "Fundamentals" << std::endl
  582. << " License: " << D.node_licenseid << std::endl
  583. << " ConfigFilePath: " << D.ConfigFilePath << std::endl
  584. << " IdentityFilePath: " << D.node_identity << std::endl
  585. << " SecurityKey: " << SecurityKeyDisplayString(D) << std::endl
  586. << "_____" << std::endl
  587. << "Paths" << std::endl
  588. << " Log Path: " << D.paths_log_path << std::endl
  589. << " Rulebase Path: " << D.paths_rulebase_path << std::endl
  590. << " Workspace Path: " << D.paths_workspace_path << std::endl
  591. << " RuleFilePath: " << D.RuleFilePath << std::endl
  592. << "____" << std::endl
  593. << "Logs" << std::endl
  594. << std::endl
  595. << " Rotation-Midnight: " << ((D.Logs_Rotation_LocalTime_OnOff)? "Local" : "UTC") << std::endl
  596. << " ______" << std::endl
  597. << " Status" << std::endl
  598. << " PerSecond: "
  599. << ((D.Status_SecondReport_Log_OnOff)? "yes, " : "no, ")
  600. << "Append: "
  601. << ((D.Status_SecondReport_Append_OnOff)? "yes" : "no")
  602. << std::endl
  603. << " PerMinute: "
  604. << ((D.Status_MinuteReport_Log_OnOff)? "yes, " : "no, ")
  605. << "Append: "
  606. << ((D.Status_MinuteReport_Append_OnOff)? "yes" : "no")
  607. << std::endl
  608. << " PerHour: "
  609. << ((D.Status_HourReport_Log_OnOff)? "yes, " : "no, ")
  610. << "Append: "
  611. << ((D.Status_HourReport_Append_OnOff)? "yes" : "no")
  612. << std::endl
  613. << " ____" << std::endl
  614. << " Scan" << std::endl
  615. << " Identifier: "
  616. << ((D.Scan_Identifier_Force_Message_Id)? "Force RFC822 Message-ID" : "Use Provided Identifier")
  617. << std::endl
  618. << " Classic: Output-"
  619. << ((LogOutputMode_None == D.Scan_Classic_Mode)? "None, " :
  620. ((LogOutputMode_API == D.Scan_Classic_Mode)? "API, " :
  621. ((LogOutputMode_File == D.Scan_Classic_Mode)? "File, " : "Error!")))
  622. << ((D.Scan_Classic_Rotate)? "Rotating, ": "Non-Rotating, ")
  623. << ((D.Scan_Classic_Matches == ScanLogMatches_None) ? "No Mathes":
  624. ((D.Scan_Classic_Matches == ScanLogMatches_Unique) ? "Unique Matches":
  625. ((D.Scan_Classic_Matches == ScanLogMatches_All) ? "All Matches" : "Error!")))
  626. << std::endl
  627. << " XML: Output-"
  628. << ((LogOutputMode_None == D.Scan_XML_Mode)? "None, " :
  629. ((LogOutputMode_API == D.Scan_XML_Mode)? "API, " :
  630. ((LogOutputMode_File == D.Scan_XML_Mode)? "File, " : "Error!")))
  631. << ((D.Scan_XML_Rotate)? "Rotating, ": "Non-Rotating, ")
  632. << ((D.Scan_XML_Matches == ScanLogMatches_None) ? "No Mathes, ":
  633. ((D.Scan_XML_Matches == ScanLogMatches_Unique) ? "Unique Matches, ":
  634. ((D.Scan_XML_Matches == ScanLogMatches_All) ? "All Matches, " : "Match Error! ")))
  635. << ((D.Scan_XML_Performance)? "Performance Metrics, " : "No Performance Metrics, ")
  636. << ((D.Scan_XML_GBUdb)? "GBUdb Data" : "No GBUdb Data")
  637. << std::endl
  638. << " XHeaders:" << std::endl
  639. << " Output: "
  640. << ((LogOutputMode_None == D.XHDROutput_Mode) ? "None" :
  641. ((LogOutputMode_API == D.XHDROutput_Mode) ? "API" :
  642. ((LogOutputMode_File == D.XHDROutput_Mode) ? "File" :
  643. ((LogOutputMode_Inject == D.XHDROutput_Mode)? "Inject" : "Error!"))))
  644. << std::endl
  645. << " Version: "
  646. << ((D.XHDRVersion_OnOff)? "On, " : "Off, ")
  647. << D.XHDRVersion_Header
  648. << std::endl
  649. << " License: "
  650. << ((D.XHDRLicense_OnOff)? "On, " : "Off, ")
  651. << D.XHDRLicense_Header
  652. << std::endl
  653. << " Rulebase: "
  654. << ((D.XHDRRulebase_OnOff)? "On, " : "Off, ")
  655. << D.XHDRRulebase_Header
  656. << std::endl
  657. << " Identifier: "
  658. << ((D.XHDRIdentifier_OnOff)? "On, " : "Off, ")
  659. << D.XHDRIdentifier_Header
  660. << std::endl
  661. << " GBUdb: "
  662. << ((D.XHDRGBUdb_OnOff)? "On, " : "Off, ")
  663. << D.XHDRGBUdb_Header
  664. << std::endl
  665. << " Result: "
  666. << ((D.XHDRResult_OnOff)? "On, " : "Off, ")
  667. << D.XHDRResult_Header
  668. << std::endl
  669. << " Matches: "
  670. << ((D.XHDRMatches_OnOff)? "On, " : "Off, ")
  671. << D.XHDRMatches_Header
  672. << std::endl
  673. << " Black: "
  674. << ((D.XHDRBlack_OnOff)? "On, " : "Off, ")
  675. << D.XHDRBlack_Header
  676. << std::endl
  677. << " White: "
  678. << ((D.XHDRWhite_OnOff)? "On, " : "Off, ")
  679. << D.XHDRWhite_Header
  680. << std::endl
  681. << " Clean: "
  682. << ((D.XHDRClean_OnOff)? "On, " : "Off, ")
  683. << D.XHDRClean_Header
  684. << std::endl;
  685. for(
  686. std::set<XHDRSymbol>::iterator iH = D.XHDRSymbolHeaders.SymbolHeaders.begin();
  687. iH != D.XHDRSymbolHeaders.SymbolHeaders.end(); iH++
  688. ) {
  689. cfgl
  690. << " Symbol: "
  691. << (*iH).Symbol << ", "
  692. << (*iH).Header
  693. << std::endl;
  694. }
  695. cfgl
  696. << "_______" << std::endl
  697. << "Network" << std::endl
  698. << " Sync Host: " << D.network_sync_host << std::endl
  699. << " Sync Port: " << D.network_sync_port << std::endl
  700. << " Sync Secs: " << D.network_sync_secs << std::endl
  701. << " _____________" << std::endl
  702. << " Update-Script" << std::endl
  703. << " On-Off: " << ((D.update_script_on_off) ? "On" : "Off") << std::endl
  704. << " Script: " << D.update_script_call << std::endl
  705. << " Guard-Time: " << D.update_script_guard_time << " seconds" << std::endl
  706. << "___" << std::endl
  707. << "XCI" << std::endl
  708. << " " << ((D.XCI_OnOff)? "Enabled" : "Disabled") << std::endl
  709. << " Port: " << D.XCI_Port << std::endl
  710. << "_____" << std::endl
  711. << "GBUdb" << std::endl
  712. << " ____________" << std::endl
  713. << " Condensation" << std::endl
  714. << " Minimum-Seconds-Between = " << D.gbudb_database_condense_minimum_seconds_between << std::endl
  715. << " Time-Trigger: "
  716. << ((D.gbudb_database_condense_time_trigger_on_off)? "on, " : "off, ")
  717. << D.gbudb_database_condense_time_trigger_seconds << " seconds" << std::endl
  718. << " Posts-Trigger: "
  719. << ((D.gbudb_database_condense_posts_trigger_on_off)? "on, " : "off, ")
  720. << D.gbudb_database_condense_posts_trigger_posts << " posts" << std::endl
  721. << " Records-Trigger: "
  722. << ((D.gbudb_database_condense_records_trigger_on_off) ? "on, " : "off, ")
  723. << D.gbudb_database_condense_records_trigger_records << " records" << std::endl
  724. << " Size-Trigger: "
  725. << ((D.gbudb_database_condense_size_trigger_on_off) ? "on, " : "off, ")
  726. << D.gbudb_database_condense_size_trigger_megabytes << " megabytes" << std::endl
  727. << " __________" << std::endl
  728. << " Checkpoint" << std::endl
  729. << " Checkpoint: "
  730. << ((D.gbudb_database_checkpoint_on_off) ? "on, " : "off, ")
  731. << D.gbudb_database_checkpoint_secs << " seconds" << std::endl
  732. << " ______" << std::endl
  733. << " Ranges" << std::endl
  734. << " White: "
  735. << ((D.WhiteRangeHandler.On_Off) ? "on, " : "off, ")
  736. << "Symbol " << D.WhiteRangeHandler.Symbol << std::endl
  737. << " Auto-Panic: "
  738. << ((D.gbudb_regions_white_panic_on_off) ? "on, " : "off, ")
  739. << "Range " << D.gbudb_regions_white_panic_rule_range << std::endl
  740. << std::endl
  741. << " Caution: "
  742. << ((D.CautionRangeHandler.On_Off) ? "on, " : "off, ")
  743. << "Symbol " << D.CautionRangeHandler.Symbol << std::endl
  744. << std::endl
  745. << " Black: "
  746. << ((D.BlackRangeHandler.On_Off) ? "on, " : "off, ")
  747. << "Symbol " << D.BlackRangeHandler.Symbol << std::endl
  748. << " Truncate: "
  749. << ((D.gbudb_regions_black_truncate_on_off) ? "on, " : "off, ")
  750. << "Probability " << D.gbudb_regions_black_truncate_probability << ", "
  751. << "Peek-One-In " << D.gbudb_regions_black_truncate_peek_one_in << ", "
  752. << "Symbol " << D.gbudb_regions_black_truncate_symbol << std::endl
  753. << " Sample: "
  754. << ((D.gbudb_regions_black_sample_on_off) ? "on, " : "off, ")
  755. << "Probability: " << D.gbudb_regions_black_sample_probability << ", "
  756. << "Grab-One-In: " << D.gbudb_regions_black_sample_grab_one_in << ", " << std::endl
  757. << " Passthrough: "
  758. << ((D.gbudb_regions_black_sample_passthrough) ? "yes, " : "no, ")
  759. << "Passthrough Symbol " << D.gbudb_regions_black_sample_passthrough_symbol << std::endl
  760. << std::endl
  761. << " Range Map - [W]hite [B]lack [C]aution [ ]undefined" << std::endl << std::endl
  762. << " |-9876543210123456789+|" << std::endl;
  763. // Output GBUdb Range Map
  764. for(double c = 0; c < 1.01; c+=0.1) { // Run through the confidence
  765. cfgl << " |";
  766. for(double p = -1.0; p < 1.01; p+=0.1) { // and probability ranges.
  767. RangePoint t(c,p); // Test the range point w/ c & p
  768. if(D.WhiteRangeHandler.isInWhite(t)) { // If it's in the white range
  769. cfgl << "W"; // put in a W.
  770. } else
  771. if(D.BlackRangeHandler.isInBlack(t)) { // If it's in the black range
  772. cfgl << "B"; // put in a B.
  773. } else
  774. if(D.CautionRangeHandler.isInBlack(t)) { // If it's in the caution range
  775. cfgl << "C"; // put in a C.
  776. } else {
  777. cfgl << " "; // Otherwise put in a space.
  778. }
  779. }
  780. cfgl << "|" << c << std::endl;
  781. }
  782. cfgl << " |---------------------|" << std::endl;
  783. cfgl
  784. << std::endl
  785. << " ________" << std::endl
  786. << " Training" << std::endl
  787. << " GBUdb Updates: "
  788. << ((D.GBUdbTrainingOn_Off)? "Enabled" : "Disabled") << std::endl
  789. << std::endl;
  790. cfgl
  791. << " Source Header Directives: " << std::endl;
  792. for(
  793. HeaderDirectiveSet::iterator iD = D.HeaderDirectivesHandler.HeaderDirectives.begin();
  794. iD != D.HeaderDirectivesHandler.HeaderDirectives.end(); iD++
  795. ) {
  796. const HeaderFinderPattern& Dx = *iD;
  797. if(HeaderDirectiveContext == Dx.Directive) {
  798. cfgl
  799. << " "
  800. << "Context " << Dx.Context << " is a "
  801. << Dx.Header << " header at"
  802. << " Ordinal " << Dx.Ordinal
  803. << " that Contains " << Dx.Contains << std::endl;
  804. } else
  805. if(HeaderDirectiveSource == Dx.Directive) {
  806. cfgl
  807. << " "
  808. << "Context " << Dx.Context << " Source ip is in "
  809. << Dx.Header << " header at"
  810. << " Ordinal " << Dx.Ordinal << std::endl;
  811. }
  812. }
  813. cfgl << std::endl;
  814. cfgl
  815. << " Drilldown Header Directives: " << std::endl;
  816. for(
  817. HeaderDirectiveSet::iterator iD = D.HeaderDirectivesHandler.HeaderDirectives.begin();
  818. iD != D.HeaderDirectivesHandler.HeaderDirectives.end(); iD++
  819. ) {
  820. const HeaderFinderPattern& Dx = *iD;
  821. if(HeaderDirectiveDrillDown == Dx.Directive) {
  822. cfgl
  823. << " "
  824. << Dx.Header << " header at"
  825. << " Ordinal " << Dx.Ordinal
  826. << " Contains " << Dx.Contains << std::endl;
  827. }
  828. }
  829. cfgl << std::endl;
  830. cfgl
  831. << " Bypass Header Directives: " << std::endl;
  832. for(
  833. HeaderDirectiveSet::iterator iD = D.HeaderDirectivesHandler.HeaderDirectives.begin();
  834. iD != D.HeaderDirectivesHandler.HeaderDirectives.end(); iD++
  835. ) {
  836. const HeaderFinderPattern& Dx = *iD;
  837. if(HeaderDirectiveBypass == Dx.Directive) {
  838. cfgl
  839. << " "
  840. << Dx.Header << " header at"
  841. << " Ordinal " << Dx.Ordinal
  842. << " Contains " << Dx.Contains << std::endl;
  843. }
  844. }
  845. cfgl << std::endl;
  846. cfgl
  847. << " White Rule Header Directives: " << std::endl;
  848. for(
  849. HeaderDirectiveSet::iterator iD = D.HeaderDirectivesHandler.HeaderDirectives.begin();
  850. iD != D.HeaderDirectivesHandler.HeaderDirectives.end(); iD++
  851. ) {
  852. const HeaderFinderPattern& Dx = *iD;
  853. if(HeaderDirectiveWhite == Dx.Directive) {
  854. cfgl
  855. << " "
  856. << Dx.Header << " header at"
  857. << " Ordinal " << Dx.Ordinal
  858. << " Contains " << Dx.Contains << std::endl;
  859. }
  860. }
  861. cfgl << std::endl;
  862. cfgl
  863. << " White Rule Symbols: ";
  864. // Output white rule symbols
  865. for(
  866. std::set<int>::iterator ix = D.TrainingWhiteRuleHandler.IntegerSet.begin();
  867. ix != D.TrainingWhiteRuleHandler.IntegerSet.end();
  868. ix ++) {
  869. if(D.TrainingWhiteRuleHandler.IntegerSet.begin() != ix) {
  870. cfgl << ", ";
  871. }
  872. cfgl << (*ix);
  873. }
  874. cfgl << std::endl;
  875. // Rule Panics
  876. cfgl
  877. << "___________" << std::endl
  878. << "Rule-Panics" << std::endl;
  879. for(
  880. std::set<int>::iterator ix = D.RulePanicHandler.IntegerSet.begin();
  881. ix != D.RulePanicHandler.IntegerSet.end();
  882. ix ++) {
  883. cfgl << " Rule ID: " << (*ix) << std::endl;
  884. }
  885. cfgl << std::endl;
  886. cfgl
  887. << "___________" << std::endl
  888. << "Integration" << std::endl
  889. << std::endl
  890. << " Message Format: "
  891. << ((D.MessageFileTypeCGP_on_off)? "CGP" : "RFC822")
  892. << std::endl;
  893. #ifdef __BIG_ENDIAN__
  894. cfgl << " Rulebase Conversion: BIG ENDIAN" << std::endl;
  895. #else
  896. cfgl << " Rulebase Conversion: LITTLE ENDIAN" << std::endl;
  897. #endif
  898. cfgl
  899. << "________" << std::endl
  900. << "Platform" << std::endl
  901. << D.PlatformElementContents
  902. << std::endl;
  903. cfgl << std::endl; // End with a new line.
  904. cfgl.close(); // Close the cfg log file.
  905. } catch (...) {} // Ignore any errors.
  906. }
  907. void snfCFGmgr::load() {
  908. // What shall we configure -- the inactive snfCFGData.
  909. snfCFGData& CFGData = InactiveData();
  910. // How shall we configure?
  911. // If FileName ends in .snf then find the .cfg file for details.
  912. // If the FileName ends some other way it _should_ be our cfg file.
  913. int PathLength = InitFileName.length(); // How long is the path?
  914. const int MinimumPathLength = 12; // Must be at least licensid.snf long.
  915. if(MinimumPathLength > PathLength) throw LoadFailure(); // Path length is impossible? throw!
  916. const std::string SNFExt = ".snf"; // The extension we are looking for.
  917. const std::string CFGExt = ".xml"; // The default cfg extension.
  918. const int SNFExtLength = SNFExt.length(); // The length of the extension.
  919. int SNFExtPosition = InitFileName.rfind(SNFExt,PathLength); // Find the extension at the end.
  920. bool InitPathIsRulebase = false; // Was the init FileName the Rulebase?
  921. bool InitLicenseIdIsProvided = (0 < InitLicenseId.length()); // Was the init LicenseId provided?
  922. bool InitAuthenticationIsProvided = (0 < InitAuthentication.length()); // Was the authentication provided?
  923. if((PathLength - SNFExtLength) == SNFExtPosition) { // If path ends in .snf then
  924. InitPathIsRulebase = true; // set our flag to keep track then set
  925. ConfigurationPath = InitFileName.substr(0,SNFExtPosition); // our configuration path as the init
  926. ConfigurationPath.append(CFGExt); // file name with the config extension.
  927. } else { // If the init file is not a rulebase
  928. ConfigurationPath = InitFileName; // then it is the config file name.
  929. }
  930. // At this point we know where to read our configuration from.
  931. try { CFGData.initializeFromFile(ConfigurationPath.c_str()); } // Initialize the inactive config.
  932. catch(...) { // If that failed then throw.
  933. throw LoadFailure();
  934. }
  935. // Now that the main config has been read we create the derived cfg data.
  936. // Anything that was provided in Init takes precedence over the config.
  937. //// SecurityKey
  938. //// If an identity path has been provided we must load that data.
  939. if(0 < CFGData.node_identity.length()) { // If an identity path was provided
  940. cd::ConfigurationData Identity(CFGData.node_identity.c_str()); // then get the data from that file.
  941. cd::ConfigurationElement IdentityReader("snf"); // Create an Identity reader and
  942. IdentityReader // configure it.
  943. .Element("identity")
  944. .Attribute("licenseid", CFGData.node_licenseid)
  945. .Attribute("authentication", CFGData.node_authentication)
  946. .End("identity")
  947. .End("snf");
  948. IdentityReader.interpret(Identity); // Then read the data.
  949. }
  950. //// The SecurityKey is built from the licenseID and the Authentication
  951. if(InitLicenseIdIsProvided) { // If the LicenseID is OEM provided then
  952. CFGData.SecurityKey = InitLicenseId; // the first part of our security key is that.
  953. CFGData.node_licenseid = InitLicenseId; // Also override any file-loaded license ID.
  954. } else { // If it was not provided then we will get
  955. CFGData.SecurityKey = CFGData.node_licenseid; // the LicenseID from our config file.
  956. }
  957. std::string LicenseIDToUse = CFGData.SecurityKey; // Grab the License ID we want to use.
  958. if(InitAuthenticationIsProvided) { // If the Authentication has been provided then
  959. CFGData.SecurityKey += InitAuthentication; // we use it for the second part of our
  960. } else { // security key. Otherwise we will get the
  961. CFGData.SecurityKey += CFGData.node_authentication; // Authentication from the config file.
  962. }
  963. //// RuleFilePath
  964. if(InitPathIsRulebase) { // If the Rulebase path was provided
  965. CFGData.RuleFilePath = InitFileName; // then we have our rulebase path.
  966. } else { // If not then we must figure it out...
  967. CFGData.RuleFilePath = // We build the path from the base
  968. CFGData.paths_rulebase_path + // rulebase path concattonated with
  969. LicenseIDToUse + // the license id concattonated with
  970. SNFExt; // the rulebase extension.
  971. }
  972. // Once all of the configuration data is correct we make it active.
  973. swapCFGData(); // Then swap it into the active state.
  974. // Log the configuration data as it was interpreted.
  975. logCFGData(ActiveData());
  976. }