Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

UtilityConfig.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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 OS-dependent constants.
  25. #ifdef WIN
  26. // Windows OS.
  27. const std::string UtilityConfig::SampleIgnoreListFile("C:\\SNF\\GBUdbIgnoreList.txt.sample");
  28. const std::string UtilityConfig::RulebaseDownloadCommand("FIX THIS");
  29. const std::string UtilityConfig::RulebaseDownloadStatusFile("FIX THIS");
  30. const std::string ScriptNameKey("FIX THIS"); ///< Text to replace with script name.
  31. const std::string SnifferPathKey("FIX THIS"); ///< Text to replace with directory of the rulebase.
  32. const std::string UtilityConfig::SampleRulebaseScriptFile("C:\\SNF\\getRulebase.sample");
  33. const std::string UtilityConfig::OperatingSystemType("Windows");
  34. #else
  35. // *nix OS.
  36. // SCRIPT is replaced with the full path of the script run,
  37. // SNIFFER_PATH is replaced with the path of the rulebase.
  38. const std::string UtilityConfig::RulebaseDownloadCommand
  39. ("(cd SNIFFER_PATH; touch UpdateReady.txt; chown snfuser UpdateReady.txt; su -m snfuser -c SCRIPT)");
  40. const std::string ScriptNameKey("SCRIPT"); ///< Text to replace with script name.
  41. const std::string SnifferPathKey("SNIFFER_PATH"); ///< Text to replace with directory of the rulebase.
  42. // SNIFFER_PATH is replaced with the path of the rulebase.
  43. const std::string UtilityConfig::RulebaseDownloadStatusFile("SNIFFER_PATH/getRulebase.status");
  44. #ifdef DEFAULT_DATA_DIR
  45. // *nix, DEFAULT_DATA_DIR is specified on the compile command line.
  46. const std::string UtilityConfig::SampleIgnoreListFile(DEFAULT_DATA_DIR "/GBUdbIgnoreList.txt.sample");
  47. #else
  48. // Not Windows, and DEFAULT_DATA_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 DEFAULT_DATA_DIR must be defined by -DDEFAULT_DATA_DIR="..." when compiling.
  52. #endif
  53. #ifdef SBIN_DIR
  54. // *nix, SBIN_DIR is specified on the compile command line.
  55. const std::string UtilityConfig::SampleRulebaseScriptFile(SBIN_DIR "/getRulebase.sample");
  56. #else
  57. // Not Windows, and SBIN_DIR is not specified on the compile
  58. // command line. In this case, we don't know the path for the sample
  59. // ignore list file.
  60. #error SBIN_DIR must be defined by -DSBIN_DIR="..." when compiling.
  61. #endif
  62. #ifdef SNF_OSTYPE
  63. // *nix, SNF_OSTYPE is specified on the compile command line.
  64. const std::string UtilityConfig::OperatingSystemType(SNF_OSTYPE);
  65. #else
  66. // Not Windows, and SNF_OSTYPE is not specified on the compile command
  67. // line. In this case, we don't know the operating system.
  68. #error SNF_OSTYPE must be defined by -DSNF_OSTYPE="..." when compiling.
  69. #endif
  70. #endif
  71. /// Verbose command-line input.
  72. const string VerboseKey("-v");
  73. /// Explain command-line input.
  74. const string ExplainKey("-explain");
  75. /// Help command-line input.
  76. const string HelpKey("-h");
  77. /// Setup command-line input.
  78. const string SetupKey("-setup");
  79. /// Repair command-line input.
  80. const string RepairKey("-repair");
  81. /// Start sniffer command-line input.
  82. const string StartSnifferKey("-start");
  83. /// Stop sniffer command-line input.
  84. const string StopSnifferKey("-stop");
  85. /// Configuration file command-line input.
  86. const string ConfigFileKey("-config=");
  87. /// License ID command-line input.
  88. const string LicenseIdKey("-id=");
  89. /// Authentication command-line input.
  90. const string AuthenticationKey("-auth=");
  91. /// String that indicates a successful rulebase download.
  92. //
  93. // This string must be in the last line of the getRulebase status
  94. // file. Note: The getRulebase status file is created by the
  95. // getRulebase script, has the name getRulebase.status, and is in the
  96. // same directory as the rulebase files.
  97. const string SuccessKey("Success");
  98. const string LicenseSearchString = "LICENSE_ID=";
  99. const string AuthSearchString = "AUTHENTICATION=";
  100. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  101. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  102. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  103. UtilityConfig::UtilityConfig() :
  104. LicenseIdIsSpecified(false), AuthenticationIsSpecified(false), ConfigFileExists(true)
  105. {
  106. SetExplain(false);
  107. SetVerbose(false);
  108. SetHelp(false);
  109. SetSetupRepair(false);
  110. SetStartSniffer(false);
  111. SetStopSniffer(false);
  112. }
  113. void
  114. UtilityConfig::CheckAndSetConfigFileName(const std::string DefaultFile[], int NumDefaultFiles) {
  115. string ProvisionalConfigFile = ConfigFileName;
  116. if (ProvisionalConfigFile.length() == 0) {
  117. int i;
  118. vector<string> FoundFile;
  119. for (i = 0; i < NumDefaultFiles; i++) {
  120. if (!FileExists(DefaultFile[i])) {
  121. continue; // File doesn't exist.
  122. }
  123. FoundFile.push_back(DefaultFile[i]); // Update list of found files.
  124. ProvisionalConfigFile = DefaultFile[i]; // Found configuration file.
  125. }
  126. if (0 == FoundFile.size()) { // No default file found.
  127. if (NumDefaultFiles > 0) {
  128. ProvisionalConfigFile = DefaultFile[0]; // Use the first default file.
  129. ConfigFileExists = false;
  130. } else { // No default config file was specified.
  131. ostringstream Temp;
  132. Temp << "Internal error: NumDefaultFiles <= 0 at " << __FILE__ << ":" << __LINE__;
  133. throw runtime_error(Temp.str());
  134. }
  135. } else if (FoundFile.size() > 1) { // Multiple default files found.
  136. string Temp;
  137. Temp = "Configuration file was not specified, and more than one default configuration file was found::\n\n";
  138. for (i = 0; i < FoundFile.size(); i++) {
  139. Temp += " ";
  140. Temp += FoundFile[i] + "\n";
  141. }
  142. throw runtime_error(Temp);
  143. } else {
  144. ConfigFileExists = true; // Config file was found.
  145. }
  146. } else {
  147. ConfigFileExists = FileExists(ProvisionalConfigFile);
  148. }
  149. SetConfigFileName(ProvisionalConfigFile);
  150. }
  151. void
  152. UtilityConfig::CreateDefaultConfigFile(std::string SampleConfigFile) {
  153. std::string File = GetConfigFileName();
  154. if (!ConfigFileExists) {
  155. // Create the config file.
  156. Copy(SampleConfigFile, File);
  157. }
  158. }
  159. void
  160. UtilityConfig::CreateDefaultIdentityFile(std::string SampleIdentityFile) {
  161. std::string File = GetIdentityFileName();
  162. if (!FileExists(File)) {
  163. // Create the config file.
  164. Copy(SampleIdentityFile, File);
  165. }
  166. }
  167. void
  168. UtilityConfig::LoadConfig() {
  169. if (Verbose()) {
  170. cout << "Using configuration file " << GetConfigFileName() << ".\n";
  171. }
  172. // Load the data.
  173. try {
  174. CFGData.initializeFromFile(GetConfigFileName().c_str());
  175. } catch(...) {
  176. string Temp;
  177. Temp = "Error reading configuration file " + GetConfigFileName();
  178. Temp += ".";
  179. throw runtime_error(Temp);
  180. }
  181. if ( (CFGData.paths_workspace_path.length() == 0) ||
  182. (CFGData.paths_rulebase_path.length() == 0) ||
  183. (CFGData.paths_log_path.length() == 0) ||
  184. (CFGData.update_script_call.length() == 0) ||
  185. (CFGData.node_identity.length() == 0) ) {
  186. string Temp;
  187. Temp = "The configuration file " + GetConfigFileName();
  188. Temp += " did not have the necessary specification of one or more paths:\n";
  189. Temp += "\n Workspace path: " + CFGData.paths_workspace_path;
  190. Temp += "\n Rulebase path: " + CFGData.paths_rulebase_path;
  191. Temp += "\n Log path: " + CFGData.paths_log_path;
  192. Temp += "\n Update script: " + CFGData.update_script_call;
  193. Temp += "\n Identity file: " + CFGData.node_identity;
  194. throw runtime_error(Temp);
  195. }
  196. }
  197. string
  198. UtilityConfig::GetPlatformContents(void) {
  199. return CFGData.PlatformElementContents;
  200. }
  201. string
  202. UtilityConfig::GetConfigFileName(void) {
  203. return ConfigFileName;
  204. }
  205. void
  206. UtilityConfig::SetConfigFileName(string Name) {
  207. ConfigFileName = Name;
  208. }
  209. string
  210. UtilityConfig::GetWorkspacePath(void) {
  211. return CFGData.paths_workspace_path;
  212. }
  213. string
  214. UtilityConfig::GetRulebasePath(void) {
  215. return CFGData.paths_rulebase_path;
  216. }
  217. string
  218. UtilityConfig::GetLogPath(void) {
  219. return CFGData.paths_log_path;
  220. }
  221. string
  222. UtilityConfig::GetIdentityFileName(void) {
  223. return CFGData.node_identity;
  224. }
  225. string
  226. UtilityConfig::GetRulebaseScriptName(void) {
  227. return CFGData.update_script_call;
  228. }
  229. string
  230. UtilityConfig::GetIgnoreListFileName(void) {
  231. return GetWorkspacePath() + "GBUdbIgnoreList.txt";
  232. }
  233. string
  234. UtilityConfig::GetRulebaseFileName(void) {
  235. std::string Name;
  236. Name = GetRulebasePath();
  237. Name += LicenseId + ".snf";
  238. return Name;
  239. }
  240. string
  241. UtilityConfig::GetOperatingSystemType(void) {
  242. return OperatingSystemType;
  243. }
  244. void
  245. UtilityConfig::LoadInfo(){
  246. if ("OpenBSD" == OperatingSystemType) {
  247. PostfixMainCfPath = "/usr/local/etc/postfix/main.cf";
  248. PostfixMasterCfPath = "/usr/local/etc/postfix/master.cf";
  249. SnifferStartScriptDir = "/usr/local/sbin/";
  250. } else if ("FreeBSD" == OperatingSystemType) {
  251. PostfixMainCfPath = "/etc/postfix/main.cf";
  252. PostfixMasterCfPath = "/etc/postfix/master.cf";
  253. SnifferStartScriptDir = "/usr/local/etc/rc.d/";
  254. } else if ("Ubuntu" == OperatingSystemType) {
  255. PostfixMainCfPath = "/etc/postfix/main.cf";
  256. PostfixMasterCfPath = "/etc/postfix/master.cf";
  257. SnifferStartScriptDir = "/etc/init.d/";
  258. } else if ("RedHat" == OperatingSystemType) {
  259. PostfixMainCfPath = "/etc/postfix/main.cf";
  260. PostfixMasterCfPath = "/etc/postfix/master.cf";
  261. SnifferStartScriptDir = "/etc/init.d/";
  262. } else if ("Suse" == OperatingSystemType) {
  263. PostfixMainCfPath = "/etc/postfix/main.cf";
  264. PostfixMasterCfPath = "/etc/postfix/master.cf";
  265. SnifferStartScriptDir = "/etc/init.d/";
  266. } else {
  267. ostringstream Temp;
  268. Temp << "Internal error in UtilityConfig::LoadInfo: Invalid value of OperatingSystemType: "
  269. << OperatingSystemType;
  270. throw runtime_error(Temp.str());
  271. }
  272. }
  273. void
  274. UtilityConfig::UpdateIgnoreListFile() {
  275. string IgnoreListFile = GetIgnoreListFileName();
  276. if (!FileExists(IgnoreListFile)) {
  277. Copy(SampleIgnoreListFile, IgnoreListFile);
  278. }
  279. SetMode(IgnoreListFile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // Set permissions.
  280. SetOwnerGroup(IgnoreListFile); // Set to sniffer user.
  281. }
  282. void
  283. UtilityConfig::UpdateLogDir() {
  284. string LogDir = GetLogPath();
  285. if (!FileExists(LogDir)) {
  286. MkDir(LogDir);
  287. }
  288. SetMode(LogDir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  289. SetOwnerGroup(LogDir);
  290. }
  291. bool
  292. UtilityConfig::UpdateCredentialsSpecified() {
  293. return ( (LicenseId.length() > 0) && (Authentication.length() > 0) );
  294. }
  295. void
  296. UtilityConfig::CreateUpdateRulebaseScript() {
  297. std::string File = GetRulebaseScriptName();
  298. if (!FileExists(File)) {
  299. Copy(SampleRulebaseScriptFile, File); // Copy if !Explain().
  300. }
  301. if (UpdateCredentialsSpecified()) {
  302. UpdateRulebaseScriptCredentials();
  303. }
  304. SetMode(File, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); // Set permissions.
  305. }
  306. void
  307. UtilityConfig::UpdateRulebaseScriptCredentials() {
  308. std::string File = GetRulebaseScriptName();
  309. if (Verbose()) {
  310. cout << "Update authentication and license ID in the rulebase download script file " << File << "--\n";
  311. }
  312. ifstream Input;
  313. Input.open(File.c_str()); // Read the contents.
  314. if (!Input) {
  315. string Temp;
  316. Temp = "Error opening rulebase download script file " + File;
  317. Temp += " for reading: ";
  318. Temp += strerror(errno);
  319. throw runtime_error(Temp);
  320. }
  321. string Content;
  322. string Line;
  323. bool FoundLicense = false;
  324. bool FoundAuth = false;
  325. while (getline(Input, Line)) {
  326. if (CheckForString(Line, LicenseSearchString)) { // Check for license line.
  327. if (FoundLicense) { // Second license line found?
  328. string Temp;
  329. Temp = "Rulebase sownload script file " + File;
  330. Temp += " has the wrong format: Found two lines beginning with " + LicenseSearchString;
  331. throw runtime_error(Temp);
  332. }
  333. if (Verbose()) {
  334. cout << " Modify line: '" << Line << "'...\n";
  335. }
  336. FoundLicense = true;
  337. Line = LicenseSearchString + LicenseId; // Add license line.
  338. Line += " # Added by SNFSetup";
  339. }
  340. if (CheckForString(Line, AuthSearchString)) { // Check for authentication line.
  341. if (FoundAuth) { // Second authentication line found?
  342. string Temp;
  343. Temp = "Rulebase download script file " + File;
  344. Temp += " has the wrong format: Found two lines beginning with " + AuthSearchString;
  345. throw runtime_error(Temp);
  346. }
  347. if (Verbose()) {
  348. cout << " Modify line: '" << Line << "'...\n";
  349. }
  350. FoundAuth = true;
  351. Line = AuthSearchString + Authentication; // Add authentication line.
  352. Line += " # Added by SNFSetup";
  353. }
  354. Content += Line + "\n";
  355. }
  356. if (!FoundLicense || !FoundAuth) {
  357. string Temp;
  358. Temp = "Rulebase download script file " + File;
  359. Temp += " has the wrong format: Missing required line beginning with '" + LicenseSearchString;
  360. Temp += "' or '" + AuthSearchString;
  361. Temp += "'";
  362. throw runtime_error(Temp);
  363. }
  364. if (!Input.eof()) { // Should be at end-of-file.
  365. string Temp;
  366. Temp = "Error reading the rulebase download script file " + File;
  367. Temp += ": ";
  368. Temp += strerror(errno);
  369. throw runtime_error(Temp);
  370. }
  371. Input.close();
  372. if (Input.bad()) {
  373. string Temp;
  374. Temp = "Error closing the rulebase download script file " + File;
  375. Temp += " after reading: ";
  376. Temp += strerror(errno);
  377. throw runtime_error(Temp);
  378. }
  379. if (!Explain()) {
  380. ofstream Output; // Write the updated contents.
  381. Output.open(File.c_str(), ios::trunc);
  382. if (!Output) {
  383. string Temp;
  384. Temp = "Error opening rulebase download script file " + File;
  385. Temp += " for writing: ";
  386. Temp += strerror(errno);
  387. throw runtime_error(Temp);
  388. }
  389. Output << Content;
  390. if (!Output) {
  391. string Temp;
  392. Temp = "Error writing the rulebase download script file " + File;
  393. Temp += ": ";
  394. Temp += strerror(errno);
  395. throw runtime_error(Temp);
  396. }
  397. Output.close();
  398. if (!Output) {
  399. string Temp;
  400. Temp = "Error closing the rulebase download script file " + File;
  401. Temp += " after writing: ";
  402. Temp += strerror(errno);
  403. throw runtime_error(Temp);
  404. }
  405. }
  406. OutputVerboseEnd();
  407. }
  408. void
  409. UtilityConfig::DownloadRulebase() {
  410. if (!UpdateCredentialsSpecified()) {
  411. return;
  412. }
  413. if (Verbose()) {
  414. std::cout << "Downloading the rulebase...";
  415. std::cout.flush();
  416. }
  417. std::string Command;
  418. Command = RulebaseDownloadCommand;
  419. std::string::size_type ScriptIndex = Command.find(ScriptNameKey);
  420. if (ScriptIndex != std::string::npos) { // Insert script full path?
  421. Command.replace(ScriptIndex, ScriptNameKey.length(), GetRulebaseScriptName());
  422. }
  423. std::string::size_type SnifferPathIndex = Command.find(SnifferPathKey);
  424. if (SnifferPathIndex != std::string::npos) { // Insert rulebase location?
  425. Command.replace(SnifferPathIndex, SnifferPathKey.length(), GetRulebasePath());
  426. }
  427. std::string StatusFile; // Construct download status file name.
  428. StatusFile = RulebaseDownloadStatusFile;
  429. SnifferPathIndex = StatusFile.find(SnifferPathKey);
  430. if (SnifferPathIndex != std::string::npos) { // Insert rulebase location?
  431. StatusFile.replace(SnifferPathIndex, SnifferPathKey.length(), GetRulebasePath());
  432. }
  433. if (!Explain()) {
  434. if (0 != remove(StatusFile.c_str())) { // Delete the status file.
  435. if (ENOENT != errno) { // No error if the file doesn't exist.
  436. std::ostringstream Temp;
  437. Temp << "Unable to remove rulebase download status file " << StatusFile
  438. << ": " << strerror(errno) << "\n";
  439. throw runtime_error(Temp.str());
  440. }
  441. }
  442. if (std::system(Command.c_str()) != 0) { // Download the rulebase.
  443. string Temp;
  444. Temp = "Error running the command '" + Command;
  445. Temp += "'.";
  446. throw runtime_error(Temp);
  447. }
  448. ifstream Input;
  449. Input.open(StatusFile.c_str()); // Check the status.
  450. if (!Input) {
  451. string Temp;
  452. Temp = "Error opening rulebase download scriptstatus file " + StatusFile;
  453. Temp += ": ";
  454. Temp += strerror(errno);
  455. throw runtime_error(Temp);
  456. }
  457. string Line;
  458. string PrevLine;
  459. string Content;
  460. while (getline(Input, Line)) { // Read the last line.
  461. PrevLine = Line;
  462. Content += Line + "\n";
  463. }
  464. if (PrevLine.find(SuccessKey) == std::string::npos) { // Check the status.
  465. string Temp;
  466. Temp = "Error downloading the rulebase. Rulebase download status:\n\n" + Content;
  467. throw runtime_error(Temp);
  468. }
  469. }
  470. OutputVerboseEnd();
  471. }
  472. void
  473. UtilityConfig::UpdateIdentityFile() {
  474. std::string File = GetIdentityFileName();
  475. if (UpdateCredentialsSpecified()) {
  476. ofstream Output;
  477. if (Verbose()) {
  478. cout << "Create identity file " << File << "...";
  479. }
  480. if (!Explain()) {
  481. Output.open(File.c_str());
  482. if (!Output) {
  483. string Temp;
  484. Temp = "Error opening identity file " + File;
  485. Temp += ": ";
  486. Temp += strerror(errno);
  487. throw runtime_error(Temp);
  488. }
  489. Output << "<!-- License file created by SNFIdentity-->\n"
  490. << "<snf>\n"
  491. << " <identity licenseid='" << LicenseId << "' authentication='"
  492. << Authentication << "'/>\n"
  493. << "</snf>\n";
  494. if (!Output) {
  495. string Temp;
  496. Temp = "Error writing identity file " + File;
  497. Temp += ": ";
  498. Temp += strerror(errno);
  499. throw runtime_error(Temp);
  500. }
  501. Output.close();
  502. if (!Output) {
  503. string Temp;
  504. Temp = "Error closing identity file " + File;
  505. Temp += ": ";
  506. Temp += strerror(errno);
  507. throw runtime_error(Temp);
  508. }
  509. }
  510. OutputVerboseEnd();
  511. }
  512. SetOwnerGroup(File); // Set the user and group.
  513. SetMode(File, S_IRUSR); // Set to readonly by owner.
  514. }
  515. void
  516. UtilityConfig::StartSniffer(std::string Script) {
  517. std::string Command;
  518. Command = SnifferStartScriptDir + Script;
  519. Command += " start";
  520. if (Verbose()) {
  521. cout << "Starting Sniffer with the command '" << Command << "'...";
  522. }
  523. if (!Explain()) {
  524. if (std::system(Command.c_str()) == -1) { // Start the sniffer.
  525. string Temp;
  526. Temp = "Error running the command '" + Command;
  527. Temp += "' to start the Sniffer: ";
  528. Temp += strerror(errno);
  529. throw runtime_error(Temp);
  530. }
  531. }
  532. OutputVerboseEnd();
  533. }
  534. bool
  535. UtilityConfig::ProcessCommandLineItem(std::string OneInput) {
  536. bool ValidCommand = true;
  537. std::string TempString;
  538. if (OneInput == VerboseKey) {
  539. SetVerbose(true);
  540. } else if (OneInput == ExplainKey) {
  541. SetExplain(true);
  542. } else if (OneInput == HelpKey) {
  543. SetHelp(true);
  544. } else if (OneInput == SetupKey) {
  545. SetSetupRepair(true);
  546. } else if (OneInput == RepairKey) {
  547. SetSetupRepair(true);
  548. } else if (OneInput == StartSnifferKey) {
  549. SetStartSniffer(true);
  550. } else if (OneInput == StopSnifferKey) {
  551. SetStopSniffer(true);
  552. } else if (OneInput == ConfigFileKey) {
  553. SetConfigFileName(OneInput.substr(ConfigFileKey.length()));
  554. } else if (0 == OneInput.find(LicenseIdKey)) {
  555. TempString = Trim(OneInput.substr(LicenseIdKey.length())); // Copy only if not null after trimming.
  556. if (!TempString.empty()) {
  557. LicenseId = TempString;
  558. LicenseIdIsSpecified = true;
  559. } else {
  560. ValidCommand = false;
  561. }
  562. } else if (0 == OneInput.find(AuthenticationKey)) {
  563. Authentication = Trim(OneInput.substr(AuthenticationKey.length()));
  564. AuthenticationIsSpecified = true;
  565. } else {
  566. ValidCommand = false;
  567. }
  568. return ValidCommand;
  569. }
  570. bool
  571. UtilityConfig::CommandLineIsOkay() {
  572. return (AuthenticationIsSpecified == LicenseIdIsSpecified);
  573. }
  574. std::string
  575. UtilityConfig::HelpCommandLine() {
  576. std::string Help;
  577. Help = "[" + ConfigFileKey + "snf-config-file] ";
  578. Help += "[" + LicenseIdKey + "licenseid " + AuthenticationKey + "authentication] ";
  579. Help += "[ " + VerboseKey + " " + ExplainKey + " ]";
  580. return Help;
  581. }
  582. std::string
  583. UtilityConfig::HelpDescription() {
  584. std::string Desc;
  585. Desc = " -config=snf-config-file Specifies the configuration file\n";
  586. Desc += " -id=licenseid Specifies the license ID\n";
  587. Desc += " -auth=authentication Specifies the Authentication\n";
  588. Desc += " -v Provide verbose output\n";
  589. Desc += " -explain Provide an explaination of the actions\n";
  590. Desc += " without executing any commands\n";
  591. return Desc;
  592. }
  593. void
  594. UtilityConfig::SetSetupRepair(bool Specified) {
  595. SetupRepairRequested = Specified;
  596. }
  597. bool
  598. UtilityConfig::SetupRepairSpecified() {
  599. return SetupRepairRequested;
  600. }
  601. void
  602. UtilityConfig::SetStartSniffer(bool Specified) {
  603. StartSnifferRequested = Specified;
  604. }
  605. bool
  606. UtilityConfig::StartSnifferSpecified() {
  607. return StartSnifferRequested;
  608. }
  609. void
  610. UtilityConfig::SetStopSniffer(bool Specified) {
  611. StopSnifferRequested = Specified;
  612. }
  613. bool
  614. UtilityConfig::StopSnifferSpecified() {
  615. return StopSnifferRequested;
  616. }