You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UtilityConfig.cpp 22KB

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