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

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