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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. const string LicenseSearchString = "LICENSE_ID=";
  31. const string AuthSearchString = "AUTHENTICATION=";
  32. const string ConfigFileKey("-config=");
  33. const string LicenseIdKey("-id=");
  34. const string AuthenticationKey("-auth=");
  35. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  37. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. void
  39. SNFIdentityConfig::DisplayHelp(std::string Version, const std::string DefaultConfigFile[], int NumDefaultConfigFiles) {
  40. cout
  41. << Version << endl
  42. << "Copyright (C) 2011, ARM Research Labs, LLC (www.armresearch.com)\n\n"
  43. << "Usage:\n\n"
  44. << "SNFIdentity [" << ConfigFileKey << "snf-config-file] " << LicenseIdKey << "licenseid "
  45. << AuthenticationKey << "authentication "
  46. << UtilityConfig::HelpCommandLine() << "\n\n"
  47. << " -config=snf-config-file Specifies the configuration file\n"
  48. << " -id=licenseid Specifies the license ID\n"
  49. << " -auth=authentication Specifies the Authentication\n"
  50. << UtilityConfig::HelpDescription() << "\n"
  51. << "If snf-config-file is not specified, then the following files are tried:\n\n";
  52. for (int i = 0; i < NumDefaultConfigFiles; i++) {
  53. cout << " " << DefaultConfigFile[i] + "\n";
  54. }
  55. cout << "\nIf more than one default file is found, then SNFIdentity aborts.\n";
  56. };
  57. bool
  58. SNFIdentityConfig::GetCommandLineInput(int argc, char* argv[]) {
  59. int i;
  60. string OneInput;
  61. for (i = 1; i < argc; i++) { // Check each input.
  62. OneInput = argv[i];
  63. if (0 == OneInput.find(ConfigFileKey)) {
  64. SetConfigFileName(OneInput.substr(ConfigFileKey.length()));
  65. } else if (0 == OneInput.find(LicenseIdKey)) {
  66. LicenseID = OneInput.substr(LicenseIdKey.length());
  67. } else if (0 == OneInput.find(AuthenticationKey)) {
  68. Authentication = OneInput.substr(AuthenticationKey.length());
  69. } else {
  70. // Process command-line input by the base class.
  71. if (!ProcessCommandLineItem(OneInput)) {
  72. return false; // Illegal input.
  73. }
  74. }
  75. }
  76. return ( (LicenseID.length() > 0) &&
  77. (Authentication.length() > 0));
  78. }
  79. void
  80. SNFIdentityConfig::CreateIdentityFile() {
  81. ofstream Output;
  82. std::string File = GetIdentityFileName();
  83. if (Verbose()) {
  84. cout << "Create identity file " << File << "...";
  85. }
  86. if (!Explain()) {
  87. Output.open(File.c_str());
  88. if (!Output) {
  89. string Temp;
  90. Temp = "Error opening identity file " + File;
  91. Temp += ": ";
  92. Temp += strerror(errno);
  93. throw runtime_error(Temp);
  94. }
  95. Output << "<!-- License file created by SNFIdentity-->\n"
  96. << "<snf>\n"
  97. << " <identity licenseid='" << LicenseID << "' authentication='"
  98. << Authentication << "'/>\n"
  99. << "</snf>\n";
  100. if (!Output) {
  101. string Temp;
  102. Temp = "Error writing identity file " + File;
  103. Temp += ": ";
  104. Temp += strerror(errno);
  105. throw runtime_error(Temp);
  106. }
  107. Output.close();
  108. if (!Output) {
  109. string Temp;
  110. Temp = "Error closing identity file " + File;
  111. Temp += ": ";
  112. Temp += strerror(errno);
  113. throw runtime_error(Temp);
  114. }
  115. }
  116. OutputVerboseEnd();
  117. SetOwnerGroup(File); // Set the user and group.
  118. SetMode(File, S_IRUSR); // Set to readonly by owner.
  119. }
  120. void
  121. SNFIdentityConfig::UpdateRulebaseScriptCredentials() {
  122. std::string File = GetRulebaseScriptName();
  123. if (Verbose()) {
  124. cout << "Update authentication and license ID in the rulebase file " << File << "--\n";
  125. }
  126. ifstream Input;
  127. Input.open(File.c_str()); // Read the contents.
  128. if (!Input) {
  129. string Temp;
  130. Temp = "Error opening rulebase file " + File;
  131. Temp += " for reading: ";
  132. Temp += strerror(errno);
  133. throw runtime_error(Temp);
  134. }
  135. string Content;
  136. string Line;
  137. bool FoundLicense = false;
  138. bool FoundAuth = false;
  139. while (getline(Input, Line)) {
  140. if (CheckForString(Line, LicenseSearchString)) { // Check for license line.
  141. if (FoundLicense) { // Second license line found?
  142. string Temp;
  143. Temp = "Rulebase file " + File;
  144. Temp += " has the wrong format: Found two lines beginning with " + LicenseSearchString;
  145. throw runtime_error(Temp);
  146. }
  147. if (Verbose()) {
  148. cout << " Modify line: '" << Line << "'...\n";
  149. }
  150. FoundLicense = true;
  151. Line = LicenseSearchString + LicenseID; // Add license line.
  152. Line += " # Added by SNFIdentity";
  153. }
  154. if (CheckForString(Line, AuthSearchString)) { // Check for authentication line.
  155. if (FoundAuth) { // Second authentication line found?
  156. string Temp;
  157. Temp = "Rulebase file " + File;
  158. Temp += " has the wrong format: Found two lines beginning with " + AuthSearchString;
  159. throw runtime_error(Temp);
  160. }
  161. if (Verbose()) {
  162. cout << " Modify line: '" << Line << "'...\n";
  163. }
  164. FoundAuth = true;
  165. Line = AuthSearchString + Authentication; // Add authentication line.
  166. Line += " # Added by SNFIdentity";
  167. }
  168. Content += Line + "\n";
  169. }
  170. if (!FoundLicense || !FoundAuth) {
  171. string Temp;
  172. Temp = "Rulebase file " + File;
  173. Temp += " has the wrong format: Missing required line beginning with '" + LicenseSearchString;
  174. Temp += "' or '" + AuthSearchString;
  175. Temp += "'";
  176. throw runtime_error(Temp);
  177. }
  178. if (!Input.eof()) { // Should be at end-of-file.
  179. string Temp;
  180. Temp = "Error reading the rulebase file " + File;
  181. Temp += ": ";
  182. Temp += strerror(errno);
  183. throw runtime_error(Temp);
  184. }
  185. Input.close();
  186. if (Input.bad()) {
  187. string Temp;
  188. Temp = "Error closing the rulebase file " + File;
  189. Temp += " after reading: ";
  190. Temp += strerror(errno);
  191. throw runtime_error(Temp);
  192. }
  193. if (!Explain()) {
  194. ofstream Output; // Write the updated contents.
  195. Output.open(File.c_str(), ios::trunc);
  196. if (!Output) {
  197. string Temp;
  198. Temp = "Error opening rulebase file " + File;
  199. Temp += " for writing: ";
  200. Temp += strerror(errno);
  201. throw runtime_error(Temp);
  202. }
  203. Output << Content;
  204. if (!Output) {
  205. string Temp;
  206. Temp = "Error writing the rulebase file " + File;
  207. Temp += ": ";
  208. Temp += strerror(errno);
  209. throw runtime_error(Temp);
  210. }
  211. Output.close();
  212. if (!Output) {
  213. string Temp;
  214. Temp = "Error closing the rulebase file " + File;
  215. Temp += " after writing: ";
  216. Temp += strerror(errno);
  217. throw runtime_error(Temp);
  218. }
  219. }
  220. OutputVerboseEnd();
  221. SetMode(File, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); // Set permissions.
  222. }