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

UtilityConfig.hpp 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "SNFMulti.hpp"
  11. #include <string>
  12. /// Base class for the Sniffer configuration.
  13. //
  14. // This class provides capability common to the configuration applications.
  15. //
  16. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. class UtilityConfig {
  18. public:
  19. /// Default constructor.
  20. UtilityConfig();
  21. /// Check whether a file exists.
  22. //
  23. // \returns true if the file exists, false otherwise.
  24. //
  25. bool FileExists(std::string File);
  26. /// Load the specified or default config file.
  27. //
  28. // This function loads (or reloads) the config file specified in
  29. // the most recent call to LoadConfigFile. If LoadConfigFile has
  30. // not been called, then this method searches for a unique default
  31. // config file in the specified list, and loads that config file.
  32. //
  33. // \param[in] DefaultFile is the list of default locations of the file.
  34. //
  35. // \param[in] NumDefaultFiles is the number of defaultlocations.
  36. //
  37. // If the local data member ConfigFile has a value of "" on input,
  38. // then this function checks for the existence (but not
  39. // readability) of a configuration file in several locations. If
  40. // exactly one file exists, then that file is loaded and
  41. // ConfigFile is set to that name. Otherwise, an exception is
  42. // thrown.
  43. //
  44. void CheckAndLoadConfigFile(const std::string DefaultFile[], int NumDefaultFiles);
  45. /// Load the specified configuration file.
  46. //
  47. // \param[in] Name is the name of the configuration file.
  48. //
  49. void LoadConfigFile(std::string Name);
  50. /// Set the configuration file name.
  51. //
  52. // \param[in] Name is the name of the configuration file.
  53. //
  54. void SetConfigFileName(std::string Name);
  55. /// Get the configuration file name.
  56. //
  57. // \returns the name of the configuration file.
  58. //
  59. std::string GetConfigFileName(void);
  60. /// Get the contents of the <platform> element of the loaded
  61. /// config file.
  62. //
  63. // \returns the contents of the <platform> element.
  64. //
  65. string GetPlatformContents(void);
  66. /// Get the workspace path.
  67. //
  68. // \returns the workspace path.
  69. std::string GetWorkspacePath(void);
  70. /// Get the rulebase path.
  71. //
  72. // \returns the rulebase path.
  73. std::string GetRulebasePath(void);
  74. /// Get the log path.
  75. //
  76. // \returns the log path.
  77. std::string GetLogPath(void);
  78. /// Get the identity file name.
  79. //
  80. // \returns the identity file name.
  81. std::string GetIdentityFileName(void);
  82. /// Get the rulebase script file name.
  83. //
  84. // \returns the rulebase script file name.
  85. std::string GetRulebaseScriptName(void);
  86. /// Copy a file.
  87. //
  88. // \param[in] From is the name of the source file.
  89. //
  90. // \param[in] To is the name of the destination file.
  91. //
  92. void Copy(std::string From, std::string To);
  93. /// Set the owner and group of the specified file.
  94. //
  95. // This function sets the owner and group of the specified file to the
  96. // value specified in UtilityConfig.cpp.
  97. //
  98. // \param[in] File is the specified file.
  99. //
  100. // \see SNFUserName.
  101. //
  102. // \see SNFGroupName.
  103. //
  104. void SetOwnerGroup(std::string &File);
  105. /// Set the mode of a file.
  106. //
  107. // This function sets the mode of the specified file. If an error
  108. // occurs, an exception is thrown.
  109. //
  110. // \param[in] File is the specified file.
  111. //
  112. // \param[in] is the mode.
  113. //
  114. void SetMode(std::string &File, mode_t mode);
  115. /// Create a directory.
  116. //
  117. // This function creates the specified directory. If an error
  118. // occurs, an exception is thrown.
  119. //
  120. // \param[in] Dir is the directory to create.
  121. //
  122. void MkDir(std::string &Dir);
  123. /// Create a backup link of the specified file.
  124. //
  125. // This method creates a backup of the specified file by creating
  126. // a link to the file. The name of the link is the name of the
  127. // file appended by the BackupSuffix data member.
  128. //
  129. // The file can be restored from the backup link by
  130. // RestoreFromBackupLink.
  131. //
  132. // \param[in] File is the file name to create a backup link for.
  133. //
  134. // \throws std::runtime_error in case of error.
  135. //
  136. // \see RestoreFromBackupLink.
  137. //
  138. void CreateBackupLink(std::string File);
  139. /// Remove the backup link of the specified file.
  140. //
  141. // This method removes the backup link of the specified file by
  142. // creating a link to the file. The name of the link is the name
  143. // of the file appended by the BackupSuffix data member.
  144. //
  145. // \param[in] File is the file name whose backup link is to be
  146. // removed.
  147. //
  148. void RemoveBackupLink(std::string File);
  149. /// Restore the specified file from the backup link.
  150. //
  151. // This method restores the specified from the backup link created
  152. // by CreateBackupLink().
  153. //
  154. // \param[in] File is the file name whose backup link is to be
  155. // removed.
  156. //
  157. // \see CreateBackupLink.
  158. //
  159. void RestoreFromBackupLink(std::string File);
  160. /// Restart the MTA.
  161. //
  162. // This function starts or restarts the MTA.
  163. //
  164. // \param[in] Mta specifies the MTA. The acceptable values are
  165. // "postfix" and "sendmail".
  166. //
  167. void StartOrRestartMta(std::string Mta);
  168. /// Check for a specified string at the beginning of a line.
  169. //
  170. // This function checks for the specified string at the beginning of a
  171. // line. Leading whitespace in the line is ignored.
  172. //
  173. // \param[in] Line is the line.
  174. //
  175. // \param[in] SearchString is the string to check for.
  176. //
  177. static bool CheckForString(std::string Line, std::string SearchString);
  178. /// Operating system specification.
  179. enum OperatingSystemSpecEnum {
  180. OpenBSD, ///< OpenBSD OS.
  181. FreeBSD, ///< FreeBSD OS.
  182. Ubuntu, ///< Ubuntu and variants.
  183. RedHat, ///< RedHat and variants.
  184. Suse ///< Suse and variants.
  185. };
  186. /// Load the operating-system-dependent info (file locations, etc).
  187. //
  188. // This method updates the public members that contain the OS
  189. // specification and file paths.
  190. //
  191. void LoadInfo();
  192. /// OS specification.
  193. OperatingSystemSpecEnum OsSpec;
  194. /// Postfix main.cf file path.
  195. std::string PostfixMainCfPath;
  196. /// Postfix main.cf file path.
  197. std::string PostfixMasterCfPath;
  198. /// Create or update the ignore list file.
  199. //
  200. // The ignore list file is created if it dosn't exist. In any
  201. // case, the owner/group is changed by SetOwnerGroup(), and the
  202. // permissions are changed to readonly for everyone, and
  203. // read/write for the owner.
  204. void UpdateIgnoreListFile();
  205. /// Create or update the log directory.
  206. //
  207. // The log directory is created if it dosn't exist. In any case,
  208. // the owner/group is changed by SetOwnerGroup(), and the
  209. // permissions are changed to r-x for everyone, and rwx for the
  210. // owner.
  211. void UpdateLogDir();
  212. /// Process one command-line item.
  213. //
  214. // \param[in] OneInput is the command-line item to process.
  215. //
  216. bool ProcessCommandLineItem(std::string OneInput);
  217. /// Provide verbose output?
  218. //
  219. // \returns true if the application is to provide verbose output.
  220. //
  221. bool Verbose();
  222. /// Provide an explanation of the actions only?
  223. //
  224. // \returns true if the application is to provide an explanation
  225. // of the actions without executing any commands.
  226. //
  227. bool Explain();
  228. /// Provide help?
  229. //
  230. // \returns true if the application is to output a help message.
  231. //
  232. bool Help();
  233. /// Output the legal command-line input.
  234. std::string HelpCommandLine();
  235. /// Output the description of the legal command-line input.
  236. std::string HelpDescription();
  237. /// Output the end of a verbose output line.
  238. void OutputVerboseEnd();
  239. private:
  240. bool VerboseRequested; ///< User requested verbose processing.
  241. bool ExplainRequested; ///< User requested verbose processing but without actually executing the commands.
  242. bool HelpRequested; ///< User requested help.
  243. std::string ConfigFile; ///< Configuration file name.
  244. static const std::string SampleIgnoreListFile; ///< Sample ignore list file.
  245. static const std::string SampleRulebaseScriptFile; ///< Sample rulebase script file.
  246. static const std::string BackupSuffix; ///< Suffix appended to create backup file name.
  247. snfCFGData CFGData; ///< Configuration data.
  248. /// Operating system type.
  249. //
  250. // This is either Windows or the value specified for
  251. // --enable-os-type when configuring for *nix.
  252. static const std::string OperatingSystemType;
  253. };
  254. #endif