Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

UtilityConfig.hpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // UtilityConfig.hpp
  2. //
  3. // Copyright (C) 2011 ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // This file defines the interface used by the configuration utilities.
  7. //
  8. #ifndef UtilityConfighpp_included
  9. #define UtilityConfighpp_included
  10. #include <string>
  11. #include "SNFMulti.hpp"
  12. #include "Utility.hpp"
  13. #include "FileBackup.hpp"
  14. /// Base class for the Sniffer configuration.
  15. //
  16. // This class provides capability common to the configuration applications.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. class UtilityConfig : public Utility {
  20. public:
  21. /// Running status of the Sniffer application.
  22. enum SnifferRunningStateEnum {
  23. SnifferIsRunning, ///< Sniffer is running.
  24. SnifferIsStopped ///< Sniffer is not running.
  25. };
  26. /// Default constructor.
  27. UtilityConfig(void);
  28. /// Object to back up and restore files.
  29. FileBackup SaveFile;
  30. /// Set the config file name to the default if it wasn't specified.
  31. //
  32. // If the configuration file wasn't specified by
  33. // SetConfigFileName() on the command line, then this method sets
  34. // the config file to the specified default.
  35. //
  36. // If the configuration file was specified by SetConfigFileName()
  37. // or on the command line, then this method does nothing.
  38. //
  39. // \param[in] DefaultFile is the default file name.
  40. //
  41. void CheckAndSetConfigFileName(const std::string DefaultFile);
  42. /// If the configuration file doesn't exist, create it from the
  43. /// sample file. In any case, set the owner and mode.
  44. //
  45. // This method creates the default configuration file if the
  46. // specified configuration file doesn't exist.
  47. //
  48. // The method CheckAndSetConfigFileName must be called before this
  49. // method.
  50. //
  51. // \param[in] SampleConfigFile is the name of the sample
  52. // configuration file.
  53. //
  54. void CreateDefaultConfigFile(std::string SampleConfigFile);
  55. /// Setup/repair the configuration.
  56. //
  57. // This method creates any configuration files that don't exist
  58. // from the sample files.
  59. void SetupCreate(void);
  60. /// Load the configuration from the file specified by SetConfigFileName.
  61. //
  62. void LoadConfig(void);
  63. /// Set the configuration file name.
  64. //
  65. // \param[in] Name is the name of the configuration file.
  66. //
  67. void SetConfigFileName(std::string Name);
  68. /// Get the configuration file name.
  69. //
  70. // \returns the name of the configuration file.
  71. //
  72. std::string GetConfigFileName(void);
  73. /// Get the contents of the <platform> element of the loaded
  74. /// config file.
  75. //
  76. // \returns the contents of the <platform> element.
  77. //
  78. string GetPlatformContents(void);
  79. /// Get the workspace path.
  80. //
  81. // \returns the workspace path.
  82. std::string GetWorkspacePath(void);
  83. /// Get the rulebase path.
  84. //
  85. // \returns the rulebase path.
  86. std::string GetRulebasePath(void);
  87. /// Get the log path.
  88. //
  89. // \returns the log path.
  90. std::string GetLogPath(void);
  91. /// Get the status.second log file name.
  92. //
  93. // \returns the status.second log file name.
  94. std::string GetStatusSecondLogFileName(void);
  95. /// Get the status.minute log file name.
  96. //
  97. // \returns the status.minute log file name.
  98. std::string GetStatusMinuteLogFileName(void);
  99. /// Get the identity file name.
  100. //
  101. // \returns the identity file name.
  102. std::string GetIdentityFileName(void);
  103. /// Get the rulebase script file name.
  104. //
  105. // \returns the rulebase script file name.
  106. std::string GetRulebaseScriptName(void);
  107. /// Get the ignore list file name.
  108. //
  109. // \returns the ignore list file name.
  110. //
  111. std::string GetIgnoreListFileName(void);
  112. /// Return the rulebase file name.
  113. //
  114. // \returns the name of the rulebase file, including the path.
  115. //
  116. std::string GetRulebaseFileName(void);
  117. /// Get the operating system type.
  118. //
  119. // \returns the operating system type. This is the value of
  120. // SNF_OSTYPE specified on the compile commandline. For *nix, it
  121. // is identical to the value of the --enable-os-type command-line
  122. // input to ./configure:
  123. //
  124. // <ol>
  125. // <li>OpenBSD</li>
  126. // <li>FreeBSD</li>
  127. // <li>Suse</li>
  128. // <li>RedHat</li>
  129. // <li>Ubuntu</li>
  130. // </ol>
  131. //
  132. std::string GetOperatingSystemType(void);
  133. /// Load the operating-system-dependent info (file locations, etc).
  134. //
  135. // This method updates the public members that contain the OS
  136. // specification and file paths.
  137. //
  138. void LoadInfo(void);
  139. /// Postfix main.cf file path.
  140. std::string PostfixMainCfPath;
  141. /// Postfix master.cf file path.
  142. std::string PostfixMasterCfPath;
  143. /// Directory containing the Sniffer start script.
  144. std::string SnifferStartScriptDir;
  145. /// Setup/repair the configuration.
  146. //
  147. // Copy the following files from the sample files if they don't
  148. // exist:
  149. //
  150. // <ol>
  151. // <li> Identity file. </li>
  152. // <li> Ignore list file. </li>
  153. // <li> Rulebase script. </li>
  154. // </ol>
  155. //
  156. // Set the owner/group of each of the above files.
  157. //
  158. // Make sure that the log directory exists and has the correct
  159. // owner and permissions.
  160. //
  161. // \param[in] SampleIdentityFile is the name of the sample identity file.
  162. //
  163. // \note The configuration information must be loaded before calling this method.
  164. //
  165. // \see CheckAndSetConfigFileName.
  166. //
  167. // \see CreateDefaultConfigFile.
  168. //
  169. // \see LoadConfig.
  170. //
  171. // \see LoadInfo.
  172. //
  173. void SetupRepair(const std::string SampleIdentityFile);
  174. /// Restore any missing configuration files.
  175. //
  176. // Restore missing configuration files from the sample files. The
  177. // files restored are the ones restored by SetupRepair.
  178. //
  179. // \param[in] SampleIdentityFile is the name of the sample identity file.
  180. //
  181. void RestoreMissingConfigFiles(const std::string SampleIdentityFile);
  182. /// Set the owner, group, and permissions of the configuration
  183. /// files and directories.
  184. //
  185. // This method sets the ownership, group, and permissions of all
  186. // the configuration files.
  187. //
  188. void SetOwnerPermissionsOfConfigFiles(void);
  189. /// Update the rulebase script credentials.
  190. //
  191. // This method updates the rulebase with the credentials specified
  192. // on the command line.
  193. //
  194. void UpdateRulebaseScriptCredentials(void);
  195. /// Download the rulebase.
  196. //
  197. void DownloadRulebase(void);
  198. /// Update the identity file.
  199. //
  200. // If the credentials were supplied, this method updates the
  201. // identity file with the supplied credentials.
  202. //
  203. // In any case, the owner/group is changed by SetOwnerGroup(), and
  204. // the permissions are changed to readonly for the owner.
  205. //
  206. // \pre Either the identity file must exist, or the credentials
  207. // must be supplied so that the identity file is created.
  208. //
  209. // \see SetOwnerGroup().
  210. //
  211. void UpdateIdentityFile(void);
  212. /// Get the Sniffer running status.
  213. //
  214. // This method determines whether or not the specified application
  215. // is running.
  216. //
  217. // \param[in] ApplicationName is the specified application name.
  218. //
  219. // \returns enumeration specifying the running state.
  220. //
  221. SnifferRunningStateEnum GetRunningState(std::string ApplicationName);
  222. /// Start the Sniffer application if it isn't running.
  223. //
  224. // This method runs the specified sniffer start script and
  225. // arguments in the appropriate (i.e. OS-dependent) directory.
  226. // The script is prepended with the directory.
  227. //
  228. // \param[in] ScriptAndArgs contains the name of the start script
  229. // and any arguments.
  230. //
  231. // \param[in] ApplicationName is the name of the application to
  232. // run. This is passed to GetRunningState().
  233. //
  234. // \pre LoadInfo() must have been called. That method initializes
  235. // the directory the script resides in.
  236. //
  237. void StartSniffer(std::string ScriptAndArgs, std::string ApplicationName);
  238. /// Stop the Sniffer application if it's running.
  239. //
  240. // This method runs the specified sniffer stop script and
  241. // arguments in the appropriate (i.e. OS-dependent) directory.
  242. // The script is prepended with the directory.
  243. //
  244. // \param[in] ScriptAndArgs contains the name of the stop script
  245. // and any arguments.
  246. //
  247. // \param[in] ApplicationName is the name of the application to
  248. // top. This is passed to GetRunningState().
  249. //
  250. // \pre LoadInfo() must have been called. That method initializes
  251. // the directory the script resides in.
  252. //
  253. void StopSniffer(std::string ScriptAndArgs, std::string ApplicationName);
  254. /// Process one command-line item.
  255. //
  256. // \param[in] OneInput is the command-line item to process.
  257. //
  258. bool ProcessCommandLineItem(std::string OneInput);
  259. /// Check whether the command-line parameters were specified
  260. /// correctly.
  261. //
  262. // This function check that either both the LicenseID and
  263. // Authentication were specified, or neither were.
  264. //
  265. // \returns if the command-line parameters were specified
  266. // correctly, false otherwise.
  267. bool CommandLineIsOkay(void);
  268. /// Output the legal command-line input.
  269. //
  270. // \param[in] ExclusiveCommands contains the command-line help of
  271. // the additional commands implemented by the descendent classes.
  272. // Only one command may be specified when invoking the
  273. // configuration utility.
  274. //
  275. std::string HelpCommandLine(std::string ExclusiveCommands);
  276. /// Output the description of the legal command-line input.
  277. //
  278. // \param[in] ExclusiveCommandsHelp contains the description of
  279. // the additional commands implemented by the descendent classes.
  280. //
  281. std::string HelpDescription(std::string ExclusiveCommandsHelp);
  282. /// Store whether the setup/help command was specified.
  283. //
  284. // \param[in] Specified specifies whether the command was specified.
  285. //
  286. void SetSetupRepair(bool Specified);
  287. /// Setup/repair specified?
  288. //
  289. // \returns true if the setup/help command was specified on the command line.
  290. //
  291. bool SetupRepairSpecified(void);
  292. /// Determine whether the credentials should be updated.
  293. //
  294. // This method determines whether the credentials should be
  295. // updated. If the user specified both the License ID and
  296. // Authentication, then the credentials should be updated.
  297. //
  298. // \returns true if the credentials should be updated.
  299. //
  300. bool UpdateCredentialsSpecified(void);
  301. /// Store whether the start sniffer command was specified.
  302. //
  303. // \param[in] Specified specifies whether the command was specified.
  304. //
  305. void SetStartSniffer(bool Specified);
  306. /// Start sniffer specified?
  307. //
  308. // \returns true if the start sniffer command was specified on the command line.
  309. //
  310. bool StartSnifferSpecified(void);
  311. /// Store whether the stop sniffer command was specified.
  312. //
  313. // \param[in] Specified specifies whether the command was specified.
  314. //
  315. void SetStopSniffer(bool Specified);
  316. /// Stop sniffer specified?
  317. //
  318. // \returns true if the stop sniffer command was specified on the command line.
  319. //
  320. bool StopSnifferSpecified(void);
  321. private:
  322. /// Method for checking the status of Sniffer.
  323. enum StatusCheckMethod {
  324. StatusCheckXci, ///< Check using XCI.
  325. StatusCheckSecond, ///< Check using status.second log file.
  326. StatusCheckMinute, ///< Check using status.minute log file.
  327. StatusCheckNotAvailable ///< No method for checking is available.
  328. };
  329. /// Determine the mode for checking the status of Sniffer.
  330. //
  331. // This method determines how the status of the sniffer should be
  332. // checked. The configuration loaded from the configuration file
  333. // is used to determine the method.
  334. //
  335. // If XCI is enabled, then the preferred method is XCI.
  336. // Otherwise, if status.second logging is enabled, the preferred
  337. // method is to check the status.second file. Otherwise, if
  338. // status.minute logging is enabled, the preferred method is to
  339. // check the status.minute file.
  340. //
  341. // \returns Enumeration value indicating how to check the sniffer
  342. // status.
  343. //
  344. StatusCheckMethod GetPreferredStatusCheckMethod(void);
  345. /// Get the Sniffer status report using the preferred method.
  346. //
  347. // \returns Status report obtained from Sniffer using the method
  348. // specified by GetPrefferedStatusCheckMethod() if the Sniffer is
  349. // running. If the Sniffer is not running, "" is returned.
  350. //
  351. // \see GetPreferredStatusCheckMethod().
  352. //
  353. std::string GetSnifferStatusReport();
  354. /// Check the Sniffer status report.
  355. //
  356. // This method checks that the status report is well-formed by
  357. // extracting the <platform> element contents, and also checks
  358. // that the <platform> element contents contains the specified
  359. // application name.
  360. //
  361. // If the status report is not well-formed, or the expected
  362. // application name isn't in the <platform> element content, an
  363. // exception is thrown.
  364. //
  365. // \param[in] StatusReport is the status report obtained by
  366. // GetSnifferStatusReport().
  367. //
  368. // \param[in] ApplicationName is the specified application name.
  369. //
  370. void CheckSnifferStatusReport(std::string StatusReport, std::string ApplicationName);
  371. /// Get the Sniffer status report using XCI.
  372. //
  373. // \returns Status report obtained from Sniffer using XCI if the
  374. // Sniffer is running. Otherwise, "" is returned.
  375. //
  376. std::string GetReportViaXci();
  377. /// Get the Sniffer status report from the specified log file.
  378. //
  379. // This method gets the status report of a running Sniffer.
  380. // Whether or not the Sniffer is running is determined by reading
  381. // the specified log file twice, separated by the specified time
  382. // interval. If the log file contents are different, then the
  383. // Sniffer is running. Otherwise, the Sniffer is not running.
  384. //
  385. // \param[in] LogFileName is the name of the log file.
  386. //
  387. // \param[in] SleepTime_msec is the length of time to wait between
  388. // log file reads.
  389. //
  390. // \returns Status report obtained from Sniffer using the
  391. // specified log file if the Sniffer is running. Otherwise, "" is
  392. // returned.
  393. //
  394. std::string GetReportViaLogFile(std::string LogFileName, int SleepTime_msec);
  395. std::string ConfigFileName; ///< Configuration file name.
  396. std::string LicenseId; ///< License ID string.
  397. bool LicenseIdIsSpecified; ///< true if the License ID was specified on the command line.
  398. std::string Authentication; ///< Authentication string.
  399. bool AuthenticationIsSpecified; ///< true if the Authentication was specified on the command line.
  400. static const std::string RulebaseDownloadCommand; ///< Command to download the rulebase.
  401. static const std::string RulebaseDownloadStatusFile; ///< Status file for rulebase download status.
  402. static const std::string SampleIgnoreListFile; ///< Sample ignore list file.
  403. static const std::string SampleRulebaseScriptFile; ///< Sample rulebase script file.
  404. static const long LogFileReportSize = 4096; ///< Size of log file report.
  405. snfCFGData CFGData; ///< Configuration data.
  406. /// Operating system type.
  407. //
  408. // This is either Windows or the value specified for
  409. // --enable-os-type when configuring for *nix.
  410. static const std::string OperatingSystemType;
  411. bool SetupRepairRequested; ///< User requested setup/repair.
  412. bool StartSnifferRequested; ///< User requested that Sniffer be started.
  413. bool StopSnifferRequested; ///< User requested that Sniffer be stopped.
  414. };
  415. #endif