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

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