You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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, ///< OpenBSD OS.
  24. SnifferIsStopped, ///< FreeBSD OS.
  25. SnifferRunningStatusIsUknown ///< Ubuntu and variants.
  26. };
  27. /// Default constructor.
  28. UtilityConfig(void);
  29. /// Object to back up and restore files.
  30. FileBackup SaveFile;
  31. /// Set the config file name to the default if it wasn't specified.
  32. //
  33. // If the configuration file wasn't specified by
  34. // SetConfigFileName() on the command line, then this method sets
  35. // the config file to the default. The default is the unique file
  36. // that exists in the specified list. If more than one file in
  37. // the specified list exists, an exception is thrown.
  38. //
  39. // If the configuration file was specified by SetConfigFileName()
  40. // or on the command line, then this method does nothing.
  41. //
  42. // \param[in] DefaultFile is the list of default locations of the file.
  43. //
  44. // \param[in] NumDefaultFiles is the number of defaultlocations.
  45. //
  46. void CheckAndSetConfigFileName(const std::string DefaultFile[], int NumDefaultFiles);
  47. /// If the configuration file doesn't exist, create it from the
  48. /// sample file. In any case, set the owner and mode.
  49. //
  50. // This method creates the default configuration file if the
  51. // specified configuration file doesn't exist.
  52. //
  53. // The method CheckAndSetConfigFileName must be called before this
  54. // method.
  55. //
  56. // \param[in] SampleConfigFile is the name of the sample
  57. // configuration file.
  58. //
  59. void CreateDefaultConfigFile(std::string SampleConfigFile);
  60. /// Setup/repair the configuration.
  61. //
  62. // This method creates any configuration files that don't exist
  63. // from the sample files.
  64. void SetupCreate(void);
  65. /// Load the configuration from the file specified by SetConfigFileName.
  66. //
  67. void LoadConfig(void);
  68. /// Set the configuration file name.
  69. //
  70. // \param[in] Name is the name of the configuration file.
  71. //
  72. void SetConfigFileName(std::string Name);
  73. /// Get the configuration file name.
  74. //
  75. // \returns the name of the configuration file.
  76. //
  77. std::string GetConfigFileName(void);
  78. /// Get the contents of the <platform> element of the loaded
  79. /// config file.
  80. //
  81. // \returns the contents of the <platform> element.
  82. //
  83. string GetPlatformContents(void);
  84. /// Get the workspace path.
  85. //
  86. // \returns the workspace path.
  87. std::string GetWorkspacePath(void);
  88. /// Get the rulebase path.
  89. //
  90. // \returns the rulebase path.
  91. std::string GetRulebasePath(void);
  92. /// Get the log path.
  93. //
  94. // \returns the log path.
  95. std::string GetLogPath(void);
  96. /// Get the identity file name.
  97. //
  98. // \returns the identity file name.
  99. std::string GetIdentityFileName(void);
  100. /// Get the rulebase script file name.
  101. //
  102. // \returns the rulebase script file name.
  103. std::string GetRulebaseScriptName(void);
  104. /// Get the ignore list file name.
  105. //
  106. // \returns the ignore list file name.
  107. //
  108. std::string GetIgnoreListFileName(void);
  109. /// Return the rulebase file name.
  110. //
  111. // \returns the name of the rulebase file, including the path.
  112. //
  113. std::string GetRulebaseFileName(void);
  114. /// Get the operating system type.
  115. //
  116. // \returns the operating system type. This is the value of
  117. // SNF_OSTYPE specified on the compile commandline. For *nix, it
  118. // is identical to the value of the --enable-os-type command-line
  119. // input to ./configure:
  120. //
  121. // <ol>
  122. // <li>OpenBSD</li>
  123. // <li>FreeBSD</li>
  124. // <li>Suse</li>
  125. // <li>RedHat</li>
  126. // <li>Ubuntu</li>
  127. // </ol>
  128. //
  129. std::string GetOperatingSystemType(void);
  130. /// Load the operating-system-dependent info (file locations, etc).
  131. //
  132. // This method updates the public members that contain the OS
  133. // specification and file paths.
  134. //
  135. void LoadInfo(void);
  136. /// Postfix main.cf file path.
  137. std::string PostfixMainCfPath;
  138. /// Postfix master.cf file path.
  139. std::string PostfixMasterCfPath;
  140. /// Directory containing the Sniffer start script.
  141. std::string SnifferStartScriptDir;
  142. /// Setup/repair the configuration.
  143. //
  144. // Copy the following files from the sample files if they don't
  145. // exist:
  146. //
  147. // <ol>
  148. // <li> Identity file. </li>
  149. // <li> Ignore list file. </li>
  150. // <li> Rulebase script. </li>
  151. // </ol>
  152. //
  153. // Set the owner/group of each of the above files.
  154. //
  155. // Make sure that the log directory exists and has the correct
  156. // owner and permissions.
  157. //
  158. void SetupRepair(const std::string SampleIdentityFile);
  159. /// Update the rulebase script credentials.
  160. //
  161. // This method updates the rulebase with the credentials specified
  162. // on the command line.
  163. //
  164. void UpdateRulebaseScriptCredentials(void);
  165. /// Download the rulebase.
  166. //
  167. void DownloadRulebase(void);
  168. /// Update the identity file.
  169. //
  170. // If the credentials were supplied, this method updates the
  171. // identity file with the supplied credentials.
  172. //
  173. // In any case, the owner/group is changed by SetOwnerGroup(), and
  174. // the permissions are changed to readonly for the owner.
  175. //
  176. // \pre Either the identity file must exist, or the credentials
  177. // must be supplied so that the identity file is created.
  178. //
  179. // \see SetOwnerGroup().
  180. //
  181. void UpdateIdentityFile(void);
  182. /// Start the sniffer.
  183. //
  184. // This method runs the specified sniffer start script in the
  185. // appropriate (i.e. OS-dependent) directory. The script is
  186. // prepended with the directory, and run with an argument of
  187. // "start".
  188. //
  189. // \param[in] Script is the name of the start script.
  190. //
  191. // \pre LoadInfo() must have been called. That method initializes
  192. // the directory the script resides in.
  193. //
  194. void StartSniffer(std::string Script);
  195. /// Process one command-line item.
  196. //
  197. // \param[in] OneInput is the command-line item to process.
  198. //
  199. bool ProcessCommandLineItem(std::string OneInput);
  200. /// Check whether the command-line parameters were specified
  201. /// correctly.
  202. //
  203. // This function check that either both the LicenseID and
  204. // Authentication were specified, or neither were.
  205. //
  206. // \returns if the command-line parameters were specified
  207. // correctly, false otherwise.
  208. bool CommandLineIsOkay(void);
  209. /// Output the legal command-line input.
  210. //
  211. // \param[in] ExclusiveCommands contains the command-line help of
  212. // the additional commands implemented by the descendent classes.
  213. // Only one command may be specified when invoking the
  214. // configuration utility.
  215. //
  216. std::string HelpCommandLine(std::string ExclusiveCommands);
  217. /// Output the description of the legal command-line input.
  218. //
  219. // \param[in] ExclusiveCommandsHelp contains the description of
  220. // the additional commands implemented by the descendent classes.
  221. //
  222. std::string HelpDescription(std::string ExclusiveCommandsHelp);
  223. /// Store whether the setup/help command was specified.
  224. //
  225. // \param[in] Specified specifies whether the command was specified.
  226. //
  227. void SetSetupRepair(bool Specified);
  228. /// Setup/repair specified?
  229. //
  230. // \returns true if the setup/help command was specified on the command line.
  231. //
  232. bool SetupRepairSpecified(void);
  233. /// Determine whether the credentials should be updated.
  234. //
  235. // This method determines whether the credentials should be
  236. // updated. If the user specified both the License ID and
  237. // Authentication, then the credentials should be updated.
  238. //
  239. // \returns true if the credentials should be updated.
  240. //
  241. bool UpdateCredentialsSpecified(void);
  242. /// Store whether the start sniffer command was specified.
  243. //
  244. // \param[in] Specified specifies whether the command was specified.
  245. //
  246. void SetStartSniffer(bool Specified);
  247. /// Start sniffer specified?
  248. //
  249. // \returns true if the start sniffer command was specified on the command line.
  250. //
  251. bool StartSnifferSpecified(void);
  252. /// Store whether the stop sniffer command was specified.
  253. //
  254. // \param[in] Specified specifies whether the command was specified.
  255. //
  256. void SetStopSniffer(bool Specified);
  257. /// Stop sniffer specified?
  258. //
  259. // \returns true if the stop sniffer command was specified on the command line.
  260. //
  261. bool StopSnifferSpecified(void);
  262. private:
  263. /// Setup/repair the identity file.
  264. //
  265. // If the identity file doesn't exist, create it from the sample
  266. // file.
  267. //
  268. // In any case, set the owner and permissions of the identity
  269. // file.
  270. //
  271. // \param[in] SampleIdentityFile is the name of the sample
  272. // identity file.
  273. //
  274. // \note The configuration information must be loaded before calling this method.
  275. //
  276. // \see CheckAndSetConfigFileName.
  277. //
  278. // \see CreateDefaultConfigFile.
  279. //
  280. // \see LoadConfig.
  281. //
  282. // \see LoadInfo.
  283. //
  284. void SetupRepairIdentityFile(std::string SampleIdentityFile);
  285. /// Setup/repair the rulebase script.
  286. //
  287. // If the rulebase script doesn't exist, this method creates the
  288. // rulebase script from the sample rulebase script.
  289. //
  290. // In any case, set the owner and permissions of the rulebase
  291. // script.
  292. //
  293. void SetupRepairRulebaseScript(void);
  294. /// Setup/repair the ignore list file.
  295. //
  296. // The ignore list file is created if it dosn't exist. In any
  297. // case, the owner/group is changed by SetOwnerGroup(), and the
  298. // permissions are changed to readonly for everyone, and
  299. // read/write for the owner.
  300. void SetupRepairIgnoreListFile(void);
  301. /// Setup/repair the log directory.
  302. //
  303. // The log directory is created if it dosn't exist. In any case,
  304. // the owner/group is changed by SetOwnerGroup(), and the
  305. // permissions are changed to r-x for everyone, and rwx for the
  306. // owner.
  307. void SetupRepairLogDir(void);
  308. std::string ConfigFileName; ///< Configuration file name.
  309. std::string LicenseId; ///< License ID string.
  310. bool LicenseIdIsSpecified; ///< true if the License ID was specified on the command line.
  311. std::string Authentication; ///< Authentication string.
  312. bool AuthenticationIsSpecified; ///< true if the Authentication was specified on the command line.
  313. static const std::string RulebaseDownloadCommand; ///< Command to download the rulebase.
  314. static const std::string RulebaseDownloadStatusFile; ///< Status file for rulebase download status.
  315. static const std::string SampleIgnoreListFile; ///< Sample ignore list file.
  316. static const std::string SampleRulebaseScriptFile; ///< Sample rulebase script file.
  317. snfCFGData CFGData; ///< Configuration data.
  318. /// Operating system type.
  319. //
  320. // This is either Windows or the value specified for
  321. // --enable-os-type when configuring for *nix.
  322. static const std::string OperatingSystemType;
  323. bool SetupRepairRequested; ///< User requested setup/repair.
  324. bool StartSnifferRequested; ///< User requested that Sniffer be started.
  325. bool StopSnifferRequested; ///< User requested that Sniffer be stopped.
  326. };
  327. #endif