選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UtilityConfig.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. const std::string DirectorySeparator("\\");
  59. #else
  60. const std::string DirectorySeparator("/");
  61. #ifdef SNF_OSTYPE
  62. // *nix, SNF_OSTYPE is specified on the compile command line.
  63. const std::string UtilityConfig::OperatingSystemType(SNF_OSTYPE);
  64. #else
  65. // Not Windows, and SNF_OSTYPE is not specified on the compile command
  66. // line. In this case, we don't know the operating system.
  67. #error SNF_OSTYPE must be defined by -DSNF_OSTYPE="..." when compiling.
  68. #endif
  69. #endif
  70. /// SNF user name.
  71. const string SNFUserName = "snfuser";
  72. /// SNF group name.
  73. const string SNFGroupName = "snfuser";
  74. /// Verbose command-line input.
  75. const string VerboseKey("-v");
  76. /// Explain command-line input.
  77. const string ExplainKey("-explain");
  78. /// Help command-line input.
  79. const string HelpKey("-h");
  80. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  82. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  83. UtilityConfig::UtilityConfig() :
  84. ExplainRequested(false), VerboseRequested(false), HelpRequested(false) {
  85. }
  86. bool
  87. UtilityConfig::FileExists(const std::string File) {
  88. if (Verbose()) {
  89. cout << "Check whether " << File << " exists...";
  90. }
  91. struct stat StatBuf;
  92. if (0 != stat(File.c_str(), &StatBuf)) { // Error checking whether file exists.
  93. if (ENOENT == errno) { // Doesn't exist.
  94. OutputVerboseEnd();
  95. return false;
  96. }
  97. string Temp; // Other error from stat().
  98. Temp = "Error getting info for file " + File;
  99. Temp += ": ";
  100. Temp += strerror(errno);
  101. throw runtime_error(Temp);
  102. }
  103. OutputVerboseEnd();
  104. return true;
  105. }
  106. void
  107. UtilityConfig::CheckAndLoadConfigFile(const std::string DefaultFile[], int NumDefaultFiles) {
  108. string ProvisionalConfigFile = ConfigFile;
  109. if (ProvisionalConfigFile.length() == 0) {
  110. int i;
  111. vector<string> FoundFile;
  112. for (i = 0; i < NumDefaultFiles; i++) {
  113. if (!FileExists(DefaultFile[i])) {
  114. continue; // File doesn't exist.
  115. }
  116. FoundFile.push_back(DefaultFile[i]); // Update list of found files.
  117. ProvisionalConfigFile = DefaultFile[i]; // Found configuration file.
  118. }
  119. if (0 == FoundFile.size()) { // No default file found.
  120. string Temp;
  121. Temp = "Configuration file was not specified, and no default configuration file was found. ";
  122. Temp += "Checked:\n\n";
  123. for (i = 0; i < NumDefaultFiles; i++) {
  124. Temp += " ";
  125. Temp += DefaultFile[i] + "\n";
  126. }
  127. throw runtime_error(Temp);
  128. }
  129. if (FoundFile.size() > 1) { // No default file found.
  130. string Temp;
  131. Temp = "Configuration file was not specified, and more than one default configuration file was found::\n\n";
  132. for (i = 0; i < FoundFile.size(); i++) {
  133. Temp += " ";
  134. Temp += FoundFile[i] + "\n";
  135. }
  136. throw runtime_error(Temp);
  137. }
  138. }
  139. LoadConfigFile(ProvisionalConfigFile);
  140. }
  141. void
  142. UtilityConfig::LoadConfigFile(std::string Name) {
  143. SetConfigFileName(Name);
  144. if (Verbose()) {
  145. cout << "Using configuration file " << ConfigFile << ".\n";
  146. }
  147. // Load the data.
  148. try {
  149. CFGData.initializeFromFile(GetConfigFileName().c_str());
  150. } catch(...) {
  151. string Temp;
  152. Temp = "Error reading configuration file " + GetConfigFileName();
  153. Temp += ".";
  154. throw runtime_error(Temp);
  155. }
  156. if ( (CFGData.paths_workspace_path.length() == 0) ||
  157. (CFGData.paths_rulebase_path.length() == 0) ||
  158. (CFGData.paths_log_path.length() == 0) ||
  159. (CFGData.update_script_call.length() == 0) ||
  160. (CFGData.node_identity.length() == 0) ) {
  161. string Temp;
  162. Temp = "The configuration file " + GetConfigFileName();
  163. Temp += " did not have the necessary specification of one or more paths:\n";
  164. Temp += "\n Workspace path: " + CFGData.paths_workspace_path;
  165. Temp += "\n Rulebase path: " + CFGData.paths_rulebase_path;
  166. Temp += "\n Log path: " + CFGData.paths_log_path;
  167. Temp += "\n Update script: " + CFGData.update_script_call;
  168. Temp += "\n Identity file: " + CFGData.node_identity;
  169. throw runtime_error(Temp);
  170. }
  171. }
  172. string
  173. UtilityConfig::GetPlatformContents(void) {
  174. return CFGData.PlatformElementContents;
  175. }
  176. string
  177. UtilityConfig::GetConfigFileName(void) {
  178. return ConfigFile;
  179. }
  180. void
  181. UtilityConfig::SetConfigFileName(string Name) {
  182. ConfigFile = Name;
  183. }
  184. string
  185. UtilityConfig::GetWorkspacePath(void) {
  186. return CFGData.paths_workspace_path;
  187. }
  188. string
  189. UtilityConfig::GetRulebasePath(void) {
  190. return CFGData.paths_rulebase_path;
  191. }
  192. string
  193. UtilityConfig::GetLogPath(void) {
  194. return CFGData.paths_log_path;
  195. }
  196. string
  197. UtilityConfig::GetIdentityFileName(void) {
  198. return CFGData.node_identity;
  199. }
  200. string
  201. UtilityConfig::GetRulebaseScriptName(void) {
  202. return CFGData.update_script_call;
  203. }
  204. void
  205. UtilityConfig::Copy(std::string From, std::string To) {
  206. if (Verbose()) {
  207. cout << "Copy " << From << " to " << To << "...";
  208. }
  209. if (!Explain()) {
  210. ifstream Input;
  211. Input.open(From.c_str()); // Read the contents.
  212. if (!Input) {
  213. string Temp;
  214. Temp = "Error opening the file " + From;
  215. Temp += " to copy from: ";
  216. Temp += strerror(errno);
  217. throw runtime_error(Temp);
  218. }
  219. string Content;
  220. string Line;
  221. while (getline(Input, Line)) {
  222. Content += Line + "\n"; // Copy this line.
  223. }
  224. if (!Input.eof()) { // Should be at end-of-file.
  225. string Temp;
  226. Temp = "Error reading the " + From;
  227. Temp += " to copy from: ";
  228. Temp += strerror(errno);
  229. throw runtime_error(Temp);
  230. }
  231. Input.close();
  232. if (Input.bad()) {
  233. string Temp;
  234. Temp = "Error closing the file " + From;
  235. Temp += " to copy from: ";
  236. Temp += strerror(errno);
  237. throw runtime_error(Temp);
  238. }
  239. ofstream Output; // Write the contents.
  240. Output.open(To.c_str(), ios::trunc);
  241. if (!Output) {
  242. string Temp;
  243. Temp = "Error opening the file " + To;
  244. Temp += " to copy to: ";
  245. Temp += strerror(errno);
  246. throw runtime_error(Temp);
  247. }
  248. Output << Content;
  249. if (!Output) {
  250. string Temp;
  251. Temp = "Error writing the file " + To;
  252. Temp += " to copy to: ";
  253. Temp += strerror(errno);
  254. throw runtime_error(Temp);
  255. }
  256. Output.close();
  257. if (!Output) {
  258. string Temp;
  259. Temp = "Error closing the file " + To;
  260. Temp += " to copy to: ";
  261. Temp += strerror(errno);
  262. throw runtime_error(Temp);
  263. }
  264. }
  265. OutputVerboseEnd();
  266. }
  267. void
  268. UtilityConfig::SetOwnerGroup(std::string &File) {
  269. struct passwd *SNFPasswd;
  270. uid_t SNFUid;
  271. struct group *SNFGroup;
  272. gid_t SNFGid;
  273. if (Verbose()) {
  274. cout << "Set owner:group of " << File << " to "
  275. << SNFUserName << ":" << SNFGroupName << "...";
  276. }
  277. if (!Explain()) {
  278. SNFPasswd = getpwnam(SNFUserName.c_str());
  279. if (SNFPasswd == 0) {
  280. string Temp;
  281. Temp = "Error getting info for Sniffer user " + SNFUserName;
  282. Temp += ": ";
  283. Temp += strerror(errno);
  284. throw runtime_error(Temp);
  285. }
  286. SNFUid = SNFPasswd->pw_uid;
  287. SNFGid = SNFPasswd->pw_gid;
  288. if (chown(File.c_str(), SNFUid, SNFGid) != 0) {
  289. string Temp;
  290. Temp = "Error changing group and owner of file " + File;
  291. Temp += " to " + SNFUserName + ":" + SNFGroupName;
  292. Temp += ": ";
  293. Temp += strerror(errno);
  294. throw runtime_error(Temp);
  295. }
  296. }
  297. OutputVerboseEnd();
  298. }
  299. void
  300. UtilityConfig::SetMode(std::string &File, mode_t mode) {
  301. if (Verbose()) {
  302. cout << "Set mode of " << File << " to "
  303. << std::oct << mode << "...";
  304. }
  305. if (!Explain()) {
  306. if (chmod(File.c_str(), mode) != 0) {
  307. ostringstream Temp;
  308. Temp << "Error changing permissions of file " << File
  309. << " to " << mode << ": " << strerror(errno);
  310. throw runtime_error(Temp.str());
  311. }
  312. }
  313. OutputVerboseEnd();
  314. }
  315. void
  316. UtilityConfig::MkDir(std::string &Dir) {
  317. if (Verbose()) {
  318. cout << "Create directory " << Dir << "...";
  319. }
  320. if (!Explain()) {
  321. if (mkdir(Dir.c_str(), 0) != 0) {
  322. ostringstream Temp;
  323. Temp << "Error creating directory " << Dir << ": " << strerror(errno);
  324. throw runtime_error(Temp.str());
  325. }
  326. }
  327. OutputVerboseEnd();
  328. }
  329. void
  330. UtilityConfig::StartOrRestartMta(std::string Mta) {
  331. if (Verbose()) {
  332. cout << "Restarting the " << Mta << " MTA...";
  333. }
  334. if (!Explain()) {
  335. cout << "restarting the MTA is not implemented...";
  336. }
  337. OutputVerboseEnd();
  338. }
  339. bool
  340. UtilityConfig::CheckForString(std::string Line, std::string SearchString) {
  341. string::size_type Indx;
  342. Indx = Line.find_first_not_of(" \t"); // Trim leading whitespace.
  343. if (string::npos != Indx) {
  344. Line = Line.substr(Indx);
  345. }
  346. if (Line.substr(0, SearchString.length()) == SearchString) {
  347. return true;
  348. }
  349. return false;
  350. }
  351. void
  352. UtilityConfig::LoadInfo(){
  353. if ("OpenBSD" == OperatingSystemType) {
  354. OsSpec = OpenBSD;
  355. PostfixMainCfPath = "/usr/local/etc/postfix/main.cf";
  356. PostfixMasterCfPath = "/usr/local/etc/postfix/master.cf";
  357. } else if ("FreeBSD" == OperatingSystemType) {
  358. OsSpec = FreeBSD;
  359. PostfixMainCfPath = "/etc/postfix/main.cf";
  360. PostfixMasterCfPath = "/etc/postfix/master.cf";
  361. } else if ("Ubuntu" == OperatingSystemType) {
  362. OsSpec = Ubuntu;
  363. PostfixMainCfPath = "/etc/postfix/main.cf";
  364. PostfixMasterCfPath = "/etc/postfix/master.cf";
  365. } else if ("RedHat" == OperatingSystemType) {
  366. OsSpec = RedHat;
  367. PostfixMainCfPath = "/etc/postfix/main.cf";
  368. PostfixMasterCfPath = "/etc/postfix/master.cf";
  369. } else if ("Suse" == OperatingSystemType) {
  370. OsSpec = Suse;
  371. PostfixMainCfPath = "/etc/postfix/main.cf";
  372. PostfixMasterCfPath = "/etc/postfix/master.cf";
  373. } else {
  374. ostringstream Temp;
  375. Temp << "Internal error in UtilityConfig::GetOsSpec: Invalid value of OperatingSystemType: "
  376. << OperatingSystemType;
  377. throw runtime_error(Temp.str());
  378. }
  379. }
  380. void
  381. UtilityConfig::UpdateIgnoreListFile() {
  382. string IgnoreListPath = GetWorkspacePath();
  383. string IgnoreListFile;
  384. IgnoreListFile = IgnoreListPath + DirectorySeparator;
  385. IgnoreListFile += "GBUdbIgnoreList.txt";
  386. if (!FileExists(IgnoreListFile)) {
  387. Copy(SampleIgnoreListFile, IgnoreListFile); // Use SNFMilter.xml.sample.
  388. }
  389. SetMode(IgnoreListFile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // Set permissions.
  390. SetOwnerGroup(IgnoreListFile); // Set to sniffer user.
  391. }
  392. void
  393. UtilityConfig::UpdateLogDir() {
  394. string LogDir = GetLogPath();
  395. if (!FileExists(LogDir)) {
  396. MkDir(LogDir);
  397. }
  398. SetMode(LogDir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  399. SetOwnerGroup(LogDir);
  400. }
  401. bool
  402. UtilityConfig::ProcessCommandLineItem(std::string OneInput) {
  403. bool ValidCommand = false;
  404. if (OneInput == VerboseKey) {
  405. VerboseRequested = true;
  406. ValidCommand = true;
  407. } else if (OneInput == ExplainKey) {
  408. ExplainRequested = true;
  409. ValidCommand = true;
  410. } else if (OneInput == HelpKey) {
  411. HelpRequested = true;
  412. ValidCommand = true;
  413. }
  414. return ValidCommand;
  415. }
  416. bool
  417. UtilityConfig::Verbose() {
  418. return (VerboseRequested || ExplainRequested);
  419. }
  420. bool
  421. UtilityConfig::Explain() {
  422. return ExplainRequested;
  423. }
  424. bool
  425. UtilityConfig::Help() {
  426. return HelpRequested;
  427. }
  428. std::string
  429. UtilityConfig::HelpCommandLine() {
  430. std::string Help;
  431. Help = "[ " + VerboseKey + " | " + ExplainKey + " ]";
  432. return Help;
  433. }
  434. std::string
  435. UtilityConfig::HelpDescription() {
  436. std::string Desc;
  437. Desc = " -v Provide verbose output\n";
  438. Desc += " -explain Provide an explaination of the actions\n";
  439. Desc += " without executing any commands\n";
  440. return Desc;
  441. }
  442. void
  443. UtilityConfig::OutputVerboseEnd() {
  444. if (Verbose() && !Explain()) {
  445. cout << "done.\n";
  446. } else if (Explain()) {
  447. cout << "\n";
  448. }
  449. }