選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UtilityConfig.hpp 17KB

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