Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // SNFIdentity.cpp
  2. //
  3. // Copyright (C) 2011, ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // This file contains the functions for SNFIdentityConfig.
  7. //
  8. // $Id$
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////////////////////////
  11. #include <errno.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/types.h>
  15. #include <pwd.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. #include <exception>
  20. #include <stdexcept>
  21. #include <sstream>
  22. #include <iostream>
  23. #include <fstream>
  24. #include <vector>
  25. #include "SNFIdentityConfig.hpp"
  26. using namespace std;
  27. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
  29. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  30. // Initialize command to download the rulebase.
  31. #ifdef WIN
  32. // Windows OS.
  33. const std::string SNFIdentityConfig::RulebaseDownloadCommand("FIX THIS");
  34. #else
  35. // *nix OS. SCRIPT is replaced with the full path of the script run,
  36. // SNIFFER_PATH is replaced with the path of the rulebase.
  37. const std::string SNFIdentityConfig::RulebaseDownloadCommand
  38. ("(cd SNIFFER_PATH; touch UpdateReady.txt; chown snfuser UpdateReady.txt; su -m snfuser -c SCRIPT)");
  39. #endif
  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. const string LicenseSearchString = "LICENSE_ID=";
  43. const string AuthSearchString = "AUTHENTICATION=";
  44. const string ConfigFileKey("-config=");
  45. const string LicenseIdKey("-id=");
  46. const string AuthenticationKey("-auth=");
  47. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  49. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. void
  51. SNFIdentityConfig::DisplayHelp(std::string Version, const std::string DefaultConfigFile[], int NumDefaultConfigFiles) {
  52. cout
  53. << Version << endl
  54. << "Copyright (C) 2011, ARM Research Labs, LLC (www.armresearch.com)\n\n"
  55. << "Usage:\n\n"
  56. << "SNFIdentity [" << ConfigFileKey << "snf-config-file] " << LicenseIdKey << "licenseid "
  57. << AuthenticationKey << "authentication "
  58. << UtilityConfig::HelpCommandLine() << "\n\n"
  59. << " -config=snf-config-file Specifies the configuration file\n"
  60. << " -id=licenseid Specifies the license ID\n"
  61. << " -auth=authentication Specifies the Authentication\n"
  62. << UtilityConfig::HelpDescription() << "\n"
  63. << "If snf-config-file is not specified, then the following files are tried:\n\n";
  64. for (int i = 0; i < NumDefaultConfigFiles; i++) {
  65. cout << " " << DefaultConfigFile[i] + "\n";
  66. }
  67. cout << "\nIf more than one default file is found, then SNFIdentity aborts.\n";
  68. };
  69. bool
  70. SNFIdentityConfig::GetCommandLineInput(int argc, char* argv[]) {
  71. int i;
  72. string OneInput;
  73. for (i = 1; i < argc; i++) { // Check each input.
  74. OneInput = argv[i];
  75. if (0 == OneInput.find(ConfigFileKey)) {
  76. SetConfigFileName(OneInput.substr(ConfigFileKey.length()));
  77. } else if (0 == OneInput.find(LicenseIdKey)) {
  78. LicenseID = OneInput.substr(LicenseIdKey.length());
  79. } else if (0 == OneInput.find(AuthenticationKey)) {
  80. Authentication = OneInput.substr(AuthenticationKey.length());
  81. } else {
  82. // Process command-line input by the base class.
  83. if (!ProcessCommandLineItem(OneInput)) {
  84. return false; // Illegal input.
  85. }
  86. }
  87. }
  88. return ( (LicenseID.length() > 0) &&
  89. (Authentication.length() > 0));
  90. }
  91. void
  92. SNFIdentityConfig::UpdateRulebaseScriptCredentials() {
  93. std::string File = GetRulebaseScriptName();
  94. if (Verbose()) {
  95. cout << "Update authentication and license ID in the rulebase download script file " << File << "--\n";
  96. }
  97. ifstream Input;
  98. Input.open(File.c_str()); // Read the contents.
  99. if (!Input) {
  100. string Temp;
  101. Temp = "Error opening rulebase download script file " + File;
  102. Temp += " for reading: ";
  103. Temp += strerror(errno);
  104. throw runtime_error(Temp);
  105. }
  106. string Content;
  107. string Line;
  108. bool FoundLicense = false;
  109. bool FoundAuth = false;
  110. while (getline(Input, Line)) {
  111. if (CheckForString(Line, LicenseSearchString)) { // Check for license line.
  112. if (FoundLicense) { // Second license line found?
  113. string Temp;
  114. Temp = "Rulebase sownload script file " + File;
  115. Temp += " has the wrong format: Found two lines beginning with " + LicenseSearchString;
  116. throw runtime_error(Temp);
  117. }
  118. if (Verbose()) {
  119. cout << " Modify line: '" << Line << "'...\n";
  120. }
  121. FoundLicense = true;
  122. Line = LicenseSearchString + LicenseID; // Add license line.
  123. Line += " # Added by SNFIdentity";
  124. }
  125. if (CheckForString(Line, AuthSearchString)) { // Check for authentication line.
  126. if (FoundAuth) { // Second authentication line found?
  127. string Temp;
  128. Temp = "Rulebase download script file " + File;
  129. Temp += " has the wrong format: Found two lines beginning with " + AuthSearchString;
  130. throw runtime_error(Temp);
  131. }
  132. if (Verbose()) {
  133. cout << " Modify line: '" << Line << "'...\n";
  134. }
  135. FoundAuth = true;
  136. Line = AuthSearchString + Authentication; // Add authentication line.
  137. Line += " # Added by SNFIdentity";
  138. }
  139. Content += Line + "\n";
  140. }
  141. if (!FoundLicense || !FoundAuth) {
  142. string Temp;
  143. Temp = "Rulebase download script file " + File;
  144. Temp += " has the wrong format: Missing required line beginning with '" + LicenseSearchString;
  145. Temp += "' or '" + AuthSearchString;
  146. Temp += "'";
  147. throw runtime_error(Temp);
  148. }
  149. if (!Input.eof()) { // Should be at end-of-file.
  150. string Temp;
  151. Temp = "Error reading the rulebase download script file " + File;
  152. Temp += ": ";
  153. Temp += strerror(errno);
  154. throw runtime_error(Temp);
  155. }
  156. Input.close();
  157. if (Input.bad()) {
  158. string Temp;
  159. Temp = "Error closing the rulebase download script file " + File;
  160. Temp += " after reading: ";
  161. Temp += strerror(errno);
  162. throw runtime_error(Temp);
  163. }
  164. if (!Explain()) {
  165. SaveFile.CreateBackupFile(File); // Save the existing file.
  166. ofstream Output; // Write the updated contents.
  167. Output.open(File.c_str(), ios::trunc);
  168. if (!Output) {
  169. string Temp;
  170. Temp = "Error opening rulebase download script file " + File;
  171. Temp += " for writing: ";
  172. Temp += strerror(errno);
  173. throw runtime_error(Temp);
  174. }
  175. Output << Content;
  176. if (!Output) {
  177. string Temp;
  178. Temp = "Error writing the rulebase download script file " + File;
  179. Temp += ": ";
  180. Temp += strerror(errno);
  181. throw runtime_error(Temp);
  182. }
  183. Output.close();
  184. if (!Output) {
  185. string Temp;
  186. Temp = "Error closing the rulebase download script file " + File;
  187. Temp += " after writing: ";
  188. Temp += strerror(errno);
  189. throw runtime_error(Temp);
  190. }
  191. }
  192. OutputVerboseEnd();
  193. SetMode(File, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); // Set permissions.
  194. }
  195. void
  196. SNFIdentityConfig::DownloadRulebase() {
  197. if (Verbose()) {
  198. std::cout << "Downloading the rulebase...";
  199. }
  200. std::string Command;
  201. Command = RulebaseDownloadCommand;
  202. std::string::size_type ScriptIndex = Command.find(ScriptNameKey);
  203. if (ScriptIndex != std::string::npos) { // Insert script full path?
  204. Command.replace(ScriptIndex, ScriptNameKey.length(), GetRulebaseScriptName());
  205. }
  206. std::string::size_type SnifferPathIndex = Command.find(SnifferPathKey);
  207. if (SnifferPathIndex != std::string::npos) { // Insert rulebase location?
  208. Command.replace(SnifferPathIndex, SnifferPathKey.length(), GetRulebasePath());
  209. }
  210. if (!Explain()) {
  211. SaveFile.CreateBackupFile(GetRulebaseFileName());
  212. if (std::system(Command.c_str()) != 0) {
  213. string Temp;
  214. Temp = "Error running the command '" + Command;
  215. Temp += "'.";
  216. throw runtime_error(Temp);
  217. }
  218. }
  219. OutputVerboseEnd();
  220. }
  221. void
  222. SNFIdentityConfig::CreateIdentityFile() {
  223. ofstream Output;
  224. std::string File = GetIdentityFileName();
  225. if (Verbose()) {
  226. cout << "Create identity file " << File << "...";
  227. }
  228. if (!Explain()) {
  229. SaveFile.CreateBackupFile(File);
  230. Output.open(File.c_str());
  231. if (!Output) {
  232. string Temp;
  233. Temp = "Error opening identity file " + File;
  234. Temp += ": ";
  235. Temp += strerror(errno);
  236. throw runtime_error(Temp);
  237. }
  238. Output << "<!-- License file created by SNFIdentity-->\n"
  239. << "<snf>\n"
  240. << " <identity licenseid='" << LicenseID << "' authentication='"
  241. << Authentication << "'/>\n"
  242. << "</snf>\n";
  243. if (!Output) {
  244. string Temp;
  245. Temp = "Error writing identity file " + File;
  246. Temp += ": ";
  247. Temp += strerror(errno);
  248. throw runtime_error(Temp);
  249. }
  250. Output.close();
  251. if (!Output) {
  252. string Temp;
  253. Temp = "Error closing identity file " + File;
  254. Temp += ": ";
  255. Temp += strerror(errno);
  256. throw runtime_error(Temp);
  257. }
  258. }
  259. OutputVerboseEnd();
  260. SetOwnerGroup(File); // Set the user and group.
  261. SetMode(File, S_IRUSR); // Set to readonly by owner.
  262. }
  263. string
  264. SNFIdentityConfig::GetRulebaseFileName(void) {
  265. std::string Name;
  266. Name = GetRulebasePath();
  267. Name += LicenseID + ".snf";
  268. return Name;
  269. }