Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

UtilityConfig.hpp 17KB

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