Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

UtilityConfig.cpp 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // UtilityConfig.cpp
  2. //
  3. // Copyright (C) 2011, ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // This file implements the common functionality for the configuration
  7. // utilities.
  8. #include <cerrno>
  9. #include <cstring>
  10. #include <unistd.h>
  11. #include <pwd.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <stdexcept>
  15. #include <sstream>
  16. #include <iostream>
  17. #include <fstream>
  18. #include <vector>
  19. #include "UtilityConfig.hpp"
  20. using namespace std;
  21. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
  23. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. // Initialize sample ignore list file path.
  25. #ifdef WIN
  26. // Windows OS.
  27. const std::string UtilityConfig::SampleIgnoreListFile("C:\\SNF\\GBUdbIgnoreList.txt.sample");
  28. #else
  29. #ifdef DEFAULT_DATA_DIR
  30. // *nix, DEFAULT_DATA_DIR is specified on the compile command line.
  31. const std::string UtilityConfig::SampleIgnoreListFile(DEFAULT_DATA_DIR "/GBUdbIgnoreList.txt.sample");
  32. #else
  33. // Not Windows, and DEFAULT_DATA_DIR is not specified on the compile
  34. // command line. In this case, we don't know the path for the sample
  35. // ignore list file.
  36. #error DEFAULT_DATA_DIR must be defined by -DDEFAULT_DATA_DIR="..." when compiling.
  37. #endif
  38. #endif
  39. // Initialize sample rulebase script file.
  40. #ifdef WIN
  41. // Windows OS.
  42. const std::string UtilityConfig::SampleRulebaseScriptFile("C:\\SNF\\getRulebase.sample");
  43. #else
  44. #ifdef SBIN_DIR
  45. // *nix, SBIN_DIR is specified on the compile command line.
  46. const std::string UtilityConfig::SampleRulebaseScriptFile(SBIN_DIR "/getRulebase.sample");
  47. #else
  48. // Not Windows, and SBIN_DIR is not specified on the compile
  49. // command line. In this case, we don't know the path for the sample
  50. // ignore list file.
  51. #error SBIN_DIR must be defined by -DSBIN_DIR="..." when compiling.
  52. #endif
  53. #endif
  54. // Initialize OS-specific values.
  55. #ifdef WIN
  56. // Windows OS.
  57. const std::string UtilityConfig::OperatingSystemType("Windows");
  58. #else
  59. #ifdef SNF_OSTYPE
  60. // *nix, SNF_OSTYPE is specified on the compile command line.
  61. const std::string UtilityConfig::OperatingSystemType(SNF_OSTYPE);
  62. #else
  63. // Not Windows, and SNF_OSTYPE is not specified on the compile command
  64. // line. In this case, we don't know the operating system.
  65. #error SNF_OSTYPE must be defined by -DSNF_OSTYPE="..." when compiling.
  66. #endif
  67. #endif
  68. /// Verbose command-line input.
  69. const string VerboseKey("-v");
  70. /// Explain command-line input.
  71. const string ExplainKey("-explain");
  72. /// Help command-line input.
  73. const string HelpKey("-h");
  74. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  76. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. UtilityConfig::UtilityConfig() {
  78. SetExplain(false);
  79. SetVerbose(false);
  80. SetHelp(false);
  81. }
  82. void
  83. UtilityConfig::CheckAndLoadConfigFile(const std::string DefaultFile[], int NumDefaultFiles) {
  84. string ProvisionalConfigFile = ConfigFile;
  85. if (ProvisionalConfigFile.length() == 0) {
  86. int i;
  87. vector<string> FoundFile;
  88. for (i = 0; i < NumDefaultFiles; i++) {
  89. if (!FileExists(DefaultFile[i])) {
  90. continue; // File doesn't exist.
  91. }
  92. FoundFile.push_back(DefaultFile[i]); // Update list of found files.
  93. ProvisionalConfigFile = DefaultFile[i]; // Found configuration file.
  94. }
  95. if (0 == FoundFile.size()) { // No default file found.
  96. string Temp;
  97. Temp = "Configuration file was not specified, and no default configuration file was found. ";
  98. Temp += "Checked:\n\n";
  99. for (i = 0; i < NumDefaultFiles; i++) {
  100. Temp += " ";
  101. Temp += DefaultFile[i] + "\n";
  102. }
  103. throw runtime_error(Temp);
  104. }
  105. if (FoundFile.size() > 1) { // No default file found.
  106. string Temp;
  107. Temp = "Configuration file was not specified, and more than one default configuration file was found::\n\n";
  108. for (i = 0; i < FoundFile.size(); i++) {
  109. Temp += " ";
  110. Temp += FoundFile[i] + "\n";
  111. }
  112. throw runtime_error(Temp);
  113. }
  114. }
  115. LoadConfigFile(ProvisionalConfigFile);
  116. }
  117. void
  118. UtilityConfig::LoadConfigFile(std::string Name) {
  119. SetConfigFileName(Name);
  120. if (Verbose()) {
  121. cout << "Using configuration file " << ConfigFile << ".\n";
  122. }
  123. // Load the data.
  124. try {
  125. CFGData.initializeFromFile(GetConfigFileName().c_str());
  126. } catch(...) {
  127. string Temp;
  128. Temp = "Error reading configuration file " + GetConfigFileName();
  129. Temp += ".";
  130. throw runtime_error(Temp);
  131. }
  132. if ( (CFGData.paths_workspace_path.length() == 0) ||
  133. (CFGData.paths_rulebase_path.length() == 0) ||
  134. (CFGData.paths_log_path.length() == 0) ||
  135. (CFGData.update_script_call.length() == 0) ||
  136. (CFGData.node_identity.length() == 0) ) {
  137. string Temp;
  138. Temp = "The configuration file " + GetConfigFileName();
  139. Temp += " did not have the necessary specification of one or more paths:\n";
  140. Temp += "\n Workspace path: " + CFGData.paths_workspace_path;
  141. Temp += "\n Rulebase path: " + CFGData.paths_rulebase_path;
  142. Temp += "\n Log path: " + CFGData.paths_log_path;
  143. Temp += "\n Update script: " + CFGData.update_script_call;
  144. Temp += "\n Identity file: " + CFGData.node_identity;
  145. throw runtime_error(Temp);
  146. }
  147. }
  148. string
  149. UtilityConfig::GetPlatformContents(void) {
  150. return CFGData.PlatformElementContents;
  151. }
  152. string
  153. UtilityConfig::GetConfigFileName(void) {
  154. return ConfigFile;
  155. }
  156. void
  157. UtilityConfig::SetConfigFileName(string Name) {
  158. ConfigFile = Name;
  159. }
  160. string
  161. UtilityConfig::GetWorkspacePath(void) {
  162. return CFGData.paths_workspace_path;
  163. }
  164. string
  165. UtilityConfig::GetRulebasePath(void) {
  166. return CFGData.paths_rulebase_path;
  167. }
  168. string
  169. UtilityConfig::GetLogPath(void) {
  170. return CFGData.paths_log_path;
  171. }
  172. string
  173. UtilityConfig::GetIdentityFileName(void) {
  174. return CFGData.node_identity;
  175. }
  176. string
  177. UtilityConfig::GetRulebaseScriptName(void) {
  178. return CFGData.update_script_call;
  179. }
  180. void
  181. UtilityConfig::StartOrRestartMta(std::string Mta) {
  182. if (Verbose()) {
  183. cout << "Restarting the " << Mta << " MTA...";
  184. }
  185. if (!Explain()) {
  186. cout << "restarting the MTA is not implemented...";
  187. }
  188. OutputVerboseEnd();
  189. }
  190. void
  191. UtilityConfig::LoadInfo(){
  192. if ("OpenBSD" == OperatingSystemType) {
  193. OsSpec = OpenBSD;
  194. PostfixMainCfPath = "/usr/local/etc/postfix/main.cf";
  195. PostfixMasterCfPath = "/usr/local/etc/postfix/master.cf";
  196. } else if ("FreeBSD" == OperatingSystemType) {
  197. OsSpec = FreeBSD;
  198. PostfixMainCfPath = "/etc/postfix/main.cf";
  199. PostfixMasterCfPath = "/etc/postfix/master.cf";
  200. } else if ("Ubuntu" == OperatingSystemType) {
  201. OsSpec = Ubuntu;
  202. PostfixMainCfPath = "/etc/postfix/main.cf";
  203. PostfixMasterCfPath = "/etc/postfix/master.cf";
  204. } else if ("RedHat" == OperatingSystemType) {
  205. OsSpec = RedHat;
  206. PostfixMainCfPath = "/etc/postfix/main.cf";
  207. PostfixMasterCfPath = "/etc/postfix/master.cf";
  208. } else if ("Suse" == OperatingSystemType) {
  209. OsSpec = Suse;
  210. PostfixMainCfPath = "/etc/postfix/main.cf";
  211. PostfixMasterCfPath = "/etc/postfix/master.cf";
  212. } else {
  213. ostringstream Temp;
  214. Temp << "Internal error in UtilityConfig::GetOsSpec: Invalid value of OperatingSystemType: "
  215. << OperatingSystemType;
  216. throw runtime_error(Temp.str());
  217. }
  218. }
  219. void
  220. UtilityConfig::UpdateIgnoreListFile() {
  221. string IgnoreListPath = GetWorkspacePath();
  222. string IgnoreListFile;
  223. IgnoreListFile = IgnoreListPath + DirectorySeparator;
  224. IgnoreListFile += "GBUdbIgnoreList.txt";
  225. SaveFile.CreateBackupFile(IgnoreListFile); // Save any existing file.
  226. if (!FileExists(IgnoreListFile)) {
  227. Copy(SampleIgnoreListFile, IgnoreListFile); // Use SNFMilter.xml.sample.
  228. }
  229. SetMode(IgnoreListFile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // Set permissions.
  230. SetOwnerGroup(IgnoreListFile); // Set to sniffer user.
  231. }
  232. void
  233. UtilityConfig::UpdateLogDir() {
  234. string LogDir = GetLogPath();
  235. if (!FileExists(LogDir)) {
  236. MkDir(LogDir);
  237. }
  238. SetMode(LogDir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  239. SetOwnerGroup(LogDir);
  240. }
  241. bool
  242. UtilityConfig::ProcessCommandLineItem(std::string OneInput) {
  243. bool ValidCommand = false;
  244. if (OneInput == VerboseKey) {
  245. SetVerbose(true);
  246. ValidCommand = true;
  247. } else if (OneInput == ExplainKey) {
  248. SetExplain(true);
  249. ValidCommand = true;
  250. } else if (OneInput == HelpKey) {
  251. SetHelp(true);
  252. ValidCommand = true;
  253. }
  254. return ValidCommand;
  255. }
  256. std::string
  257. UtilityConfig::HelpCommandLine() {
  258. std::string Help;
  259. Help = "[ " + VerboseKey + " | " + ExplainKey + " ]";
  260. return Help;
  261. }
  262. std::string
  263. UtilityConfig::HelpDescription() {
  264. std::string Desc;
  265. Desc = " -v Provide verbose output\n";
  266. Desc += " -explain Provide an explaination of the actions\n";
  267. Desc += " without executing any commands\n";
  268. return Desc;
  269. }