Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

UtilityConfig.cpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. const string IdentityElementName = "identity";
  101. const string LicenseAttributeName = "licenseid";
  102. const string AuthenticationAttributeName = "authentication";
  103. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  105. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  106. UtilityConfig::UtilityConfig() :
  107. LicenseIdIsSpecified(false), AuthenticationIsSpecified(false)
  108. {
  109. SetExplain(false);
  110. SetVerbose(false);
  111. SetHelp(false);
  112. SetSetupRepair(false);
  113. SetStartSniffer(false);
  114. SetStopSniffer(false);
  115. }
  116. void
  117. UtilityConfig::CheckAndSetConfigFileName(const std::string DefaultFile[], int NumDefaultFiles) {
  118. string ProvisionalConfigFile = ConfigFileName;
  119. if (ProvisionalConfigFile.length() == 0) {
  120. int i;
  121. vector<string> FoundFile;
  122. for (i = 0; i < NumDefaultFiles; i++) {
  123. if (!FileExists(DefaultFile[i])) {
  124. continue; // File doesn't exist.
  125. }
  126. FoundFile.push_back(DefaultFile[i]); // Update list of found files.
  127. ProvisionalConfigFile = DefaultFile[i]; // Found configuration file.
  128. }
  129. if (0 == FoundFile.size()) { // No default file found.
  130. if (NumDefaultFiles > 0) {
  131. ProvisionalConfigFile = DefaultFile[0]; // Use the first default file.
  132. } else { // No default config file was specified.
  133. ostringstream Temp;
  134. Temp << "Internal error: NumDefaultFiles <= 0 at " << __FILE__ << ":" << __LINE__;
  135. throw runtime_error(Temp.str());
  136. }
  137. } else if (FoundFile.size() > 1) { // Multiple default files found.
  138. string Temp;
  139. Temp = "Configuration file was not specified, and more than one default configuration file was found::\n\n";
  140. for (i = 0; i < FoundFile.size(); i++) {
  141. Temp += " ";
  142. Temp += FoundFile[i] + "\n";
  143. }
  144. throw runtime_error(Temp);
  145. }
  146. }
  147. SetConfigFileName(ProvisionalConfigFile);
  148. }
  149. void
  150. UtilityConfig::CreateDefaultConfigFile(std::string SampleConfigFile) {
  151. std::string File = GetConfigFileName();
  152. if (!FileExists(File)) {
  153. if (!Explain()) {
  154. SaveFile.CreateBackupFile(File);
  155. }
  156. // Create the config file.
  157. Copy(SampleConfigFile, File);
  158. }
  159. SetMode(File, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // Set permissions.
  160. SetOwnerGroup(File); // Set to sniffer user.
  161. }
  162. void
  163. UtilityConfig::LoadConfig() {
  164. if (Verbose()) {
  165. cout << "Using configuration file " << GetConfigFileName() << ".\n";
  166. }
  167. // Load the data.
  168. try {
  169. CFGData.initializeFromFile(GetConfigFileName().c_str());
  170. } catch(...) {
  171. string Temp;
  172. Temp = "Error reading configuration file " + GetConfigFileName();
  173. Temp += ".";
  174. throw runtime_error(Temp);
  175. }
  176. if ( (CFGData.paths_workspace_path.length() == 0) ||
  177. (CFGData.paths_rulebase_path.length() == 0) ||
  178. (CFGData.paths_log_path.length() == 0) ||
  179. (CFGData.update_script_call.length() == 0) ||
  180. (CFGData.node_identity.length() == 0) ) {
  181. string Temp;
  182. Temp = "The configuration file " + GetConfigFileName();
  183. Temp += " did not have the necessary specification of one or more paths:\n";
  184. Temp += "\n Workspace path: " + CFGData.paths_workspace_path;
  185. Temp += "\n Rulebase path: " + CFGData.paths_rulebase_path;
  186. Temp += "\n Log path: " + CFGData.paths_log_path;
  187. Temp += "\n Update script: " + CFGData.update_script_call;
  188. Temp += "\n Identity file: " + CFGData.node_identity;
  189. throw runtime_error(Temp);
  190. }
  191. }
  192. string
  193. UtilityConfig::GetPlatformContents(void) {
  194. return CFGData.PlatformElementContents;
  195. }
  196. string
  197. UtilityConfig::GetConfigFileName(void) {
  198. return ConfigFileName;
  199. }
  200. void
  201. UtilityConfig::SetConfigFileName(string Name) {
  202. ConfigFileName = Name;
  203. }
  204. string
  205. UtilityConfig::GetWorkspacePath(void) {
  206. return CFGData.paths_workspace_path;
  207. }
  208. string
  209. UtilityConfig::GetRulebasePath(void) {
  210. return CFGData.paths_rulebase_path;
  211. }
  212. string
  213. UtilityConfig::GetLogPath(void) {
  214. return CFGData.paths_log_path;
  215. }
  216. string
  217. UtilityConfig::GetIdentityFileName(void) {
  218. return CFGData.node_identity;
  219. }
  220. string
  221. UtilityConfig::GetRulebaseScriptName(void) {
  222. return CFGData.update_script_call;
  223. }
  224. string
  225. UtilityConfig::GetIgnoreListFileName(void) {
  226. return GetWorkspacePath() + "GBUdbIgnoreList.txt";
  227. }
  228. string
  229. UtilityConfig::GetRulebaseFileName(void) {
  230. std::string Name;
  231. Name = GetRulebasePath();
  232. Name += LicenseId + ".snf";
  233. return Name;
  234. }
  235. string
  236. UtilityConfig::GetOperatingSystemType(void) {
  237. return OperatingSystemType;
  238. }
  239. void
  240. UtilityConfig::LoadInfo(){
  241. if ("OpenBSD" == OperatingSystemType) {
  242. PostfixMainCfPath = "/usr/local/etc/postfix/main.cf";
  243. PostfixMasterCfPath = "/usr/local/etc/postfix/master.cf";
  244. SnifferStartScriptDir = "/usr/local/sbin/";
  245. } else if ("FreeBSD" == OperatingSystemType) {
  246. PostfixMainCfPath = "/etc/postfix/main.cf";
  247. PostfixMasterCfPath = "/etc/postfix/master.cf";
  248. SnifferStartScriptDir = "/usr/local/etc/rc.d/";
  249. } else if ("Ubuntu" == OperatingSystemType) {
  250. PostfixMainCfPath = "/etc/postfix/main.cf";
  251. PostfixMasterCfPath = "/etc/postfix/master.cf";
  252. SnifferStartScriptDir = "/etc/init.d/";
  253. } else if ("RedHat" == OperatingSystemType) {
  254. PostfixMainCfPath = "/etc/postfix/main.cf";
  255. PostfixMasterCfPath = "/etc/postfix/master.cf";
  256. SnifferStartScriptDir = "/etc/init.d/";
  257. } else if ("Suse" == OperatingSystemType) {
  258. PostfixMainCfPath = "/etc/postfix/main.cf";
  259. PostfixMasterCfPath = "/etc/postfix/master.cf";
  260. SnifferStartScriptDir = "/etc/init.d/";
  261. } else {
  262. ostringstream Temp;
  263. Temp << "Internal error in UtilityConfig::LoadInfo: Invalid value of OperatingSystemType: "
  264. << OperatingSystemType;
  265. throw runtime_error(Temp.str());
  266. }
  267. }
  268. void
  269. UtilityConfig::SetupRepairIdentityFile(std::string SampleIdentityFile) {
  270. std::string File = GetIdentityFileName();
  271. if (!FileExists(File)) {
  272. if (!Explain()) {
  273. SaveFile.CreateBackupFile(File);
  274. }
  275. // Create the config file.
  276. Copy(SampleIdentityFile, File);
  277. }
  278. }
  279. void
  280. UtilityConfig::SetupRepairRulebaseScript() {
  281. std::string File = GetRulebaseScriptName();
  282. if (!FileExists(File)) {
  283. if (!Explain()) {
  284. SaveFile.CreateBackupFile(File);
  285. }
  286. Copy(SampleRulebaseScriptFile, File); // Copy if !Explain().
  287. }
  288. SetMode(File, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); // Set permissions.
  289. }
  290. void
  291. UtilityConfig::SetupRepairIgnoreListFile() {
  292. string IgnoreListFile = GetIgnoreListFileName();
  293. if (!FileExists(IgnoreListFile)) {
  294. if (!Explain()) {
  295. SaveFile.CreateBackupFile(File);
  296. }
  297. Copy(SampleIgnoreListFile, IgnoreListFile);
  298. }
  299. SetMode(IgnoreListFile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // Set permissions.
  300. SetOwnerGroup(IgnoreListFile); // Set to sniffer user.
  301. }
  302. void
  303. UtilityConfig::SetupRepairLogDir() {
  304. string LogDir = GetLogPath();
  305. if (!FileExists(LogDir)) {
  306. MkDir(LogDir);
  307. }
  308. SetMode(LogDir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  309. SetOwnerGroup(LogDir);
  310. }
  311. void
  312. SNFMilterConfig::SetupRepair(std::string SampleIdentityFile) {
  313. SetupRepairIdentityFile(SampleIdentityFile);
  314. SetupRepairRulebaseScript();
  315. SetupRepairIgnoreListFile();
  316. SetupRepairLogDir();
  317. }
  318. void
  319. UtilityConfig::UpdateRulebaseScriptCredentials() {
  320. std::string File = GetRulebaseScriptName();
  321. if (Verbose()) {
  322. cout << "Update authentication and license ID in the rulebase download script file " << File << "--\n";
  323. }
  324. ifstream Input;
  325. Input.open(File.c_str()); // Read the contents.
  326. if (!Input) {
  327. string Temp;
  328. Temp = "Error opening rulebase download script file " + File;
  329. Temp += " for reading: ";
  330. Temp += strerror(errno);
  331. throw runtime_error(Temp);
  332. }
  333. string Content;
  334. string Line;
  335. bool FoundLicense = false;
  336. bool FoundAuth = false;
  337. while (getline(Input, Line)) {
  338. if (CheckForString(Line, LicenseSearchString)) { // Check for license line.
  339. if (FoundLicense) { // Second license line found?
  340. string Temp;
  341. Temp = "Rulebase sownload script file " + File;
  342. Temp += " has the wrong format: Found two lines beginning with " + LicenseSearchString;
  343. throw runtime_error(Temp);
  344. }
  345. if (Verbose()) {
  346. cout << " Modify line: '" << Line << "'...\n";
  347. }
  348. FoundLicense = true;
  349. Line = LicenseSearchString + LicenseId; // Add license line.
  350. Line += " # Added by SNFSetup";
  351. }
  352. if (CheckForString(Line, AuthSearchString)) { // Check for authentication line.
  353. if (FoundAuth) { // Second authentication line found?
  354. string Temp;
  355. Temp = "Rulebase download script file " + File;
  356. Temp += " has the wrong format: Found two lines beginning with " + AuthSearchString;
  357. throw runtime_error(Temp);
  358. }
  359. if (Verbose()) {
  360. cout << " Modify line: '" << Line << "'...\n";
  361. }
  362. FoundAuth = true;
  363. Line = AuthSearchString + Authentication; // Add authentication line.
  364. Line += " # Added by SNFSetup";
  365. }
  366. Content += Line + "\n";
  367. }
  368. if (!FoundLicense || !FoundAuth) {
  369. string Temp;
  370. Temp = "Rulebase download script file " + File;
  371. Temp += " has the wrong format: Missing required line beginning with '" + LicenseSearchString;
  372. Temp += "' or '" + AuthSearchString;
  373. Temp += "'";
  374. throw runtime_error(Temp);
  375. }
  376. if (!Input.eof()) { // Should be at end-of-file.
  377. string Temp;
  378. Temp = "Error reading the rulebase download script file " + File;
  379. Temp += ": ";
  380. Temp += strerror(errno);
  381. throw runtime_error(Temp);
  382. }
  383. Input.close();
  384. if (Input.bad()) {
  385. string Temp;
  386. Temp = "Error closing the rulebase download script file " + File;
  387. Temp += " after reading: ";
  388. Temp += strerror(errno);
  389. throw runtime_error(Temp);
  390. }
  391. if (!Explain()) {
  392. SaveFile.CreateBackupFile(File);
  393. ofstream Output; // Write the updated contents.
  394. Output.open(File.c_str(), ios::trunc);
  395. if (!Output) {
  396. string Temp;
  397. Temp = "Error opening rulebase download script file " + File;
  398. Temp += " for writing: ";
  399. Temp += strerror(errno);
  400. throw runtime_error(Temp);
  401. }
  402. Output << Content;
  403. if (!Output) {
  404. string Temp;
  405. Temp = "Error writing the rulebase download script file " + File;
  406. Temp += ": ";
  407. Temp += strerror(errno);
  408. throw runtime_error(Temp);
  409. }
  410. Output.close();
  411. if (!Output) {
  412. string Temp;
  413. Temp = "Error closing the rulebase download script file " + File;
  414. Temp += " after writing: ";
  415. Temp += strerror(errno);
  416. throw runtime_error(Temp);
  417. }
  418. }
  419. OutputVerboseEnd();
  420. }
  421. void
  422. UtilityConfig::DownloadRulebase() {
  423. if (Verbose()) {
  424. std::cout << "Downloading the rulebase...";
  425. std::cout.flush();
  426. }
  427. std::string Command;
  428. Command = RulebaseDownloadCommand;
  429. std::string::size_type ScriptIndex = Command.find(ScriptNameKey);
  430. if (ScriptIndex != std::string::npos) { // Insert script full path?
  431. Command.replace(ScriptIndex, ScriptNameKey.length(), GetRulebaseScriptName());
  432. }
  433. std::string::size_type SnifferPathIndex = Command.find(SnifferPathKey);
  434. if (SnifferPathIndex != std::string::npos) { // Insert rulebase location?
  435. Command.replace(SnifferPathIndex, SnifferPathKey.length(), GetRulebasePath());
  436. }
  437. std::string StatusFile; // Construct download status file name.
  438. StatusFile = RulebaseDownloadStatusFile;
  439. SnifferPathIndex = StatusFile.find(SnifferPathKey);
  440. if (SnifferPathIndex != std::string::npos) { // Insert rulebase location?
  441. StatusFile.replace(SnifferPathIndex, SnifferPathKey.length(), GetRulebasePath());
  442. }
  443. if (!Explain()) {
  444. SaveFile.CreateBackupFile(GetRulebaseFileName()); // Save the current rulebase file.
  445. if (0 != remove(StatusFile.c_str())) { // Delete the status file.
  446. if (ENOENT != errno) { // No error if the file doesn't exist.
  447. std::ostringstream Temp;
  448. Temp << "Unable to remove rulebase download status file " << StatusFile
  449. << ": " << strerror(errno) << "\n";
  450. throw runtime_error(Temp.str());
  451. }
  452. }
  453. if (std::system(Command.c_str()) != 0) { // Download the rulebase.
  454. string Temp;
  455. Temp = "Error running the command '" + Command;
  456. Temp += "'.";
  457. throw runtime_error(Temp);
  458. }
  459. ifstream Input;
  460. Input.open(StatusFile.c_str()); // Check the status.
  461. if (!Input) {
  462. string Temp;
  463. Temp = "Error opening rulebase download scriptstatus file " + StatusFile;
  464. Temp += ": ";
  465. Temp += strerror(errno);
  466. throw runtime_error(Temp);
  467. }
  468. string Line;
  469. string PrevLine;
  470. string Content;
  471. while (getline(Input, Line)) { // Read the last line.
  472. PrevLine = Line;
  473. Content += Line + "\n";
  474. }
  475. if (PrevLine.find(SuccessKey) == std::string::npos) { // Check the status.
  476. string Temp;
  477. Temp = "Error downloading the rulebase. Rulebase download status:\n\n" + Content;
  478. throw runtime_error(Temp);
  479. }
  480. }
  481. OutputVerboseEnd();
  482. }
  483. void
  484. UtilityConfig::UpdateIdentityFile() {
  485. std::string File = GetIdentityFileName();
  486. if (Verbose()) {
  487. cout << "Update authentication and license ID in the identity file " << File << "--\n";
  488. }
  489. ifstream Input;
  490. Input.open(File.c_str()); // Read the contents.
  491. if (!Input) {
  492. string Temp;
  493. Temp = "Error opening identity file " + File;
  494. Temp += " for reading: ";
  495. Temp += strerror(errno);
  496. throw runtime_error(Temp);
  497. }
  498. ostringstream InputContents;
  499. if (!Input.eof()) { // Copy if there are characters.
  500. // Copying an empty file causes
  501. InputContents << Input.rdbuf(); // failbit to be set.
  502. }
  503. if (InputContents.bad() || InputContents.fail()) {
  504. std::string Temp;
  505. Temp = "Error reading identity file " + File;
  506. throw std::runtime_error(Temp);
  507. }
  508. Input.close();
  509. if (Input.bad()) {
  510. string Temp;
  511. Temp = "Error closing the identity file " + File;
  512. Temp += " after reading: ";
  513. Temp += strerror(errno);
  514. throw runtime_error(Temp);
  515. }
  516. string Content = InputContent.str();
  517. try {
  518. ReplaceXmlAttribute(&Content, IdentityElementName, LicenseAttributeName, LicenseId);
  519. ReplaceXmlAttribute(&Content, IdentityElementName, AuthenticationAtributeName, Authentication);
  520. } catch (std::exception &e) {
  521. std::string Temp;
  522. Temp = "Error updating credentials for identity file " + File;
  523. Temp += ": " + e.what();
  524. throw runtime_error(Temp);
  525. }
  526. if (!Explain()) {
  527. SaveFile.CreateBackupFile(File);
  528. ofstream Output;
  529. Output.open(File.c_str(), ios::trunc);
  530. if (!Output) {
  531. string Temp;
  532. Temp = "Error opening identity file " + File;
  533. Temp += " for writing: ";
  534. Temp += strerror(errno);
  535. throw runtime_error(Temp);
  536. }
  537. Output << Content;
  538. if (!Output) {
  539. string Temp;
  540. Temp = "Error writing the identity file " + File;
  541. Temp += ": ";
  542. Temp += strerror(errno);
  543. throw runtime_error(Temp);
  544. }
  545. Output.close();
  546. if (!Output) {
  547. string Temp;
  548. Temp = "Error closing the identity file " + File;
  549. Temp += " after writing: ";
  550. Temp += strerror(errno);
  551. throw runtime_error(Temp);
  552. }
  553. }
  554. OutputVerboseEnd();
  555. }
  556. #if 0
  557. void
  558. UtilityConfig::UpdateIdentityFileOld() {
  559. std::string File = GetIdentityFileName();
  560. ofstream Output;
  561. if (Verbose()) {
  562. cout << "Create identity file " << File << "...";
  563. }
  564. if (!Explain()) {
  565. SaveFile.CreateBackupFile(File);
  566. Output.open(File.c_str());
  567. if (!Output) {
  568. string Temp;
  569. Temp = "Error opening identity file " + File;
  570. Temp += ": ";
  571. Temp += strerror(errno);
  572. throw runtime_error(Temp);
  573. }
  574. Output << "<!-- License file created by SNFIdentity-->\n"
  575. << "<snf>\n"
  576. << " <identity licenseid='" << LicenseId << "' authentication='"
  577. << Authentication << "'/>\n"
  578. << "</snf>\n";
  579. if (!Output) {
  580. string Temp;
  581. Temp = "Error writing identity file " + File;
  582. Temp += ": ";
  583. Temp += strerror(errno);
  584. throw runtime_error(Temp);
  585. }
  586. Output.close();
  587. if (!Output) {
  588. string Temp;
  589. Temp = "Error closing identity file " + File;
  590. Temp += ": ";
  591. Temp += strerror(errno);
  592. throw runtime_error(Temp);
  593. }
  594. }
  595. OutputVerboseEnd();
  596. SetOwnerGroup(File); // Set the user and group.
  597. SetMode(File, S_IRUSR); // Set to readonly by owner.
  598. }
  599. #endif
  600. void
  601. UtilityConfig::StartSniffer(std::string Script) {
  602. std::string Command;
  603. Command = SnifferStartScriptDir + Script;
  604. Command += " start";
  605. if (Verbose()) {
  606. cout << "Starting Sniffer with the command '" << Command << "'...";
  607. }
  608. if (!Explain()) {
  609. if (std::system(Command.c_str()) == -1) { // Start the sniffer.
  610. string Temp;
  611. Temp = "Error running the command '" + Command;
  612. Temp += "' to start the Sniffer: ";
  613. Temp += strerror(errno);
  614. throw runtime_error(Temp);
  615. }
  616. }
  617. OutputVerboseEnd();
  618. }
  619. bool
  620. UtilityConfig::ProcessCommandLineItem(std::string OneInput) {
  621. bool ValidCommand = true;
  622. std::string TempString;
  623. if (OneInput == VerboseKey) {
  624. SetVerbose(true);
  625. } else if (OneInput == ExplainKey) {
  626. SetExplain(true);
  627. } else if (OneInput == HelpKey) {
  628. SetHelp(true);
  629. } else if (OneInput == SetupKey) {
  630. SetSetupRepair(true);
  631. } else if (OneInput == RepairKey) {
  632. SetSetupRepair(true);
  633. } else if (OneInput == StartSnifferKey) {
  634. SetStartSniffer(true);
  635. } else if (OneInput == StopSnifferKey) {
  636. SetStopSniffer(true);
  637. } else if (OneInput == ConfigFileKey) {
  638. SetConfigFileName(OneInput.substr(ConfigFileKey.length()));
  639. } else if (0 == OneInput.find(LicenseIdKey)) {
  640. TempString = Trim(OneInput.substr(LicenseIdKey.length())); // Copy only if not null after trimming.
  641. if (!TempString.empty()) {
  642. LicenseId = TempString;
  643. LicenseIdIsSpecified = true;
  644. } else {
  645. ValidCommand = false;
  646. }
  647. } else if (0 == OneInput.find(AuthenticationKey)) {
  648. Authentication = Trim(OneInput.substr(AuthenticationKey.length()));
  649. AuthenticationIsSpecified = true;
  650. } else {
  651. ValidCommand = false;
  652. }
  653. return ValidCommand;
  654. }
  655. bool
  656. UtilityConfig::CommandLineIsOkay() {
  657. return (AuthenticationIsSpecified == LicenseIdIsSpecified);
  658. }
  659. std::string
  660. UtilityConfig::HelpCommandLine(std::string ExclusiveCommandsHelp) {
  661. std::string Help;
  662. Help = SetupKey + " | ";
  663. Help += RepairKey + " | ";
  664. Help += ExclusiveCommandsHelp + " | ";
  665. Help += LicenseIdKey + "licenseid " + AuthenticationKey + "authentication | ";
  666. Help += StartSnifferKey + " | ";
  667. Help += StopSnifferKey + " ";
  668. Help += "[" + ConfigFileKey + "snf-config-file] ";
  669. Help += "[ " + VerboseKey + " " + ExplainKey + " ]";
  670. return Help;
  671. }
  672. std::string
  673. UtilityConfig::HelpDescription(std::string ExclusiveCommandsHelp) {
  674. std::string Desc;
  675. Desc = "creates the configuration files (snf-config-file and the\n";
  676. Desc += "ignore list file), the rulebase download script (default: getRulebase) if\n";
  677. Desc += "they don't exist, and also the identity file.\n\n";
  678. Desc += ExclusiveCommandsHelp;
  679. Desc += " -config=snf-config-file Specifies the configuration file\n";
  680. Desc += " -id=licenseid Specifies the license ID\n";
  681. Desc += " -auth=authentication Specifies the Authentication\n";
  682. Desc += " -v Provide verbose output\n";
  683. Desc += " -explain Provide an explaination of the actions\n";
  684. Desc += " without executing any commands\n";
  685. return Desc;
  686. }
  687. void
  688. UtilityConfig::SetSetupRepair(bool Specified) {
  689. SetupRepairRequested = Specified;
  690. }
  691. bool
  692. UtilityConfig::SetupRepairSpecified() {
  693. return SetupRepairRequested;
  694. }
  695. bool
  696. UtilityConfig::UpdateCredentialsSpecified() {
  697. return ( (LicenseId.length() > 0) && (Authentication.length() > 0) );
  698. }
  699. void
  700. UtilityConfig::SetStartSniffer(bool Specified) {
  701. StartSnifferRequested = Specified;
  702. }
  703. bool
  704. UtilityConfig::StartSnifferSpecified() {
  705. return StartSnifferRequested;
  706. }
  707. void
  708. UtilityConfig::SetStopSniffer(bool Specified) {
  709. StopSnifferRequested = Specified;
  710. }
  711. bool
  712. UtilityConfig::StopSnifferSpecified() {
  713. return StopSnifferRequested;
  714. }