123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- // SNFIdentity.cpp
- //
- // Copyright (C) 2011, ARM Research Labs, LLC.
- // See www.armresearch.com for the copyright terms.
- //
- // This file contains the functions for SNFIdentityConfig.
- //
- // $Id$
- //
- ///////////////////////////////////////////////////////////////////////////////////////////////////
-
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <pwd.h>
-
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
-
- #include <exception>
- #include <stdexcept>
- #include <sstream>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include "SNFIdentityConfig.hpp"
-
- using namespace std;
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- // Initialize command to download the rulebase.
- #ifdef WIN
-
- // Windows OS.
- const std::string SNFIdentityConfig::RulebaseDownloadCommand("FIX THIS");
-
- #else
-
- // *nix OS. SCRIPT is replaced with the full path of the script run,
- // SNIFFER_PATH is replaced with the path of the rulebase.
- const std::string SNFIdentityConfig::RulebaseDownloadCommand
- ("(cd SNIFFER_PATH; touch UpdateReady.txt; chown snfuser UpdateReady.txt; su -m snfuser -c SCRIPT)");
-
- #endif
-
- const std::string ScriptNameKey("SCRIPT"); ///< Text to replace with script name.
- const std::string SnifferPathKey("SNIFFER_PATH"); ///< Text to replace with directory of the rulebase.
-
- const string LicenseSearchString = "LICENSE_ID=";
- const string AuthSearchString = "AUTHENTICATION=";
-
- const string ConfigFileKey("-config=");
- const string LicenseIdKey("-id=");
- const string AuthenticationKey("-auth=");
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // End of configuration. /////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- void
- SNFIdentityConfig::DisplayHelp(std::string Version, const std::string DefaultConfigFile[], int NumDefaultConfigFiles) {
-
- cout
- << Version << endl
- << "Copyright (C) 2011, ARM Research Labs, LLC (www.armresearch.com)\n\n"
- << "Usage:\n\n"
- << "SNFIdentity [" << ConfigFileKey << "snf-config-file] " << LicenseIdKey << "licenseid "
- << AuthenticationKey << "authentication "
- << UtilityConfig::HelpCommandLine() << "\n\n"
- << " -config=snf-config-file Specifies the configuration file\n"
- << " -id=licenseid Specifies the license ID\n"
- << " -auth=authentication Specifies the Authentication\n"
- << UtilityConfig::HelpDescription() << "\n"
- << "If snf-config-file is not specified, then the following files are tried:\n\n";
-
- for (int i = 0; i < NumDefaultConfigFiles; i++) {
-
- cout << " " << DefaultConfigFile[i] + "\n";
-
- }
-
- cout << "\nIf more than one default file is found, then SNFIdentity aborts.\n";
-
- };
-
- bool
- SNFIdentityConfig::GetCommandLineInput(int argc, char* argv[]) {
-
- int i;
- string OneInput;
-
- for (i = 1; i < argc; i++) { // Check each input.
-
- OneInput = argv[i];
-
- if (0 == OneInput.find(ConfigFileKey)) {
-
- SetConfigFileName(OneInput.substr(ConfigFileKey.length()));
-
- } else if (0 == OneInput.find(LicenseIdKey)) {
-
- LicenseID = OneInput.substr(LicenseIdKey.length());
-
- } else if (0 == OneInput.find(AuthenticationKey)) {
-
- Authentication = OneInput.substr(AuthenticationKey.length());
-
- } else {
-
- // Process command-line input by the base class.
- if (!ProcessCommandLineItem(OneInput)) {
-
- return false; // Illegal input.
-
- }
-
- }
-
- }
-
- return ( (LicenseID.length() > 0) &&
- (Authentication.length() > 0));
-
- }
-
- void
- SNFIdentityConfig::UpdateRulebaseScriptCredentials() {
-
- std::string File = GetRulebaseScriptName();
-
- if (Verbose()) {
-
- cout << "Update authentication and license ID in the rulebase download script file " << File << "--\n";
-
- }
-
- ifstream Input;
-
- Input.open(File.c_str()); // Read the contents.
- if (!Input) {
- string Temp;
-
- Temp = "Error opening rulebase download script file " + File;
- Temp += " for reading: ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
-
- string Content;
- string Line;
- bool FoundLicense = false;
- bool FoundAuth = false;
-
- while (getline(Input, Line)) {
-
- if (CheckForString(Line, LicenseSearchString)) { // Check for license line.
-
- if (FoundLicense) { // Second license line found?
- string Temp;
-
- Temp = "Rulebase sownload script file " + File;
- Temp += " has the wrong format: Found two lines beginning with " + LicenseSearchString;
- throw runtime_error(Temp);
-
- }
-
- if (Verbose()) {
-
- cout << " Modify line: '" << Line << "'...\n";
-
- }
- FoundLicense = true;
- Line = LicenseSearchString + LicenseID; // Add license line.
- Line += " # Added by SNFIdentity";
-
- }
-
- if (CheckForString(Line, AuthSearchString)) { // Check for authentication line.
-
- if (FoundAuth) { // Second authentication line found?
- string Temp;
-
- Temp = "Rulebase download script file " + File;
- Temp += " has the wrong format: Found two lines beginning with " + AuthSearchString;
- throw runtime_error(Temp);
- }
-
- if (Verbose()) {
-
- cout << " Modify line: '" << Line << "'...\n";
-
- }
- FoundAuth = true;
- Line = AuthSearchString + Authentication; // Add authentication line.
- Line += " # Added by SNFIdentity";
-
- }
-
- Content += Line + "\n";
-
- }
-
- if (!FoundLicense || !FoundAuth) {
- string Temp;
-
- Temp = "Rulebase download script file " + File;
- Temp += " has the wrong format: Missing required line beginning with '" + LicenseSearchString;
- Temp += "' or '" + AuthSearchString;
- Temp += "'";
- throw runtime_error(Temp);
- }
-
- if (!Input.eof()) { // Should be at end-of-file.
- string Temp;
-
- Temp = "Error reading the rulebase download script file " + File;
- Temp += ": ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
-
- Input.close();
- if (Input.bad()) {
- string Temp;
-
- Temp = "Error closing the rulebase download script file " + File;
- Temp += " after reading: ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
-
-
- if (!Explain()) {
-
- SaveFile.CreateBackupFile(File); // Save the existing file.
-
- ofstream Output; // Write the updated contents.
-
- Output.open(File.c_str(), ios::trunc);
- if (!Output) {
- string Temp;
-
- Temp = "Error opening rulebase download script file " + File;
- Temp += " for writing: ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
-
- Output << Content;
- if (!Output) {
- string Temp;
-
- Temp = "Error writing the rulebase download script file " + File;
- Temp += ": ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
-
- Output.close();
- if (!Output) {
- string Temp;
-
- Temp = "Error closing the rulebase download script file " + File;
- Temp += " after writing: ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
-
- }
-
- OutputVerboseEnd();
-
- SetMode(File, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); // Set permissions.
-
- }
-
- void
- SNFIdentityConfig::DownloadRulebase() {
-
- if (Verbose()) {
-
- std::cout << "Downloading the rulebase...";
-
- }
-
- std::string Command;
-
- Command = RulebaseDownloadCommand;
-
- std::string::size_type ScriptIndex = Command.find(ScriptNameKey);
-
- if (ScriptIndex != std::string::npos) { // Insert script full path?
-
- Command.replace(ScriptIndex, ScriptNameKey.length(), GetRulebaseScriptName());
-
- }
-
- std::string::size_type SnifferPathIndex = Command.find(SnifferPathKey);
-
- if (SnifferPathIndex != std::string::npos) { // Insert rulebase location?
-
- Command.replace(SnifferPathIndex, SnifferPathKey.length(), GetRulebasePath());
-
- }
-
- if (!Explain()) {
-
- SaveFile.CreateBackupFile(GetRulebaseFileName());
-
- if (std::system(Command.c_str()) != 0) {
-
- string Temp;
-
- Temp = "Error running the command '" + Command;
- Temp += "'.";
- throw runtime_error(Temp);
-
- }
-
- }
-
- OutputVerboseEnd();
-
- }
-
- void
- SNFIdentityConfig::CreateIdentityFile() {
-
- ofstream Output;
- std::string File = GetIdentityFileName();
-
- if (Verbose()) {
-
- cout << "Create identity file " << File << "...";
-
- }
-
- if (!Explain()) {
-
- SaveFile.CreateBackupFile(File);
-
- Output.open(File.c_str());
- if (!Output) {
- string Temp;
-
- Temp = "Error opening identity file " + File;
- Temp += ": ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
-
- Output << "<!-- License file created by SNFIdentity-->\n"
- << "<snf>\n"
- << " <identity licenseid='" << LicenseID << "' authentication='"
- << Authentication << "'/>\n"
- << "</snf>\n";
- if (!Output) {
- string Temp;
-
- Temp = "Error writing identity file " + File;
- Temp += ": ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
-
- Output.close();
- if (!Output) {
- string Temp;
-
- Temp = "Error closing identity file " + File;
- Temp += ": ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
-
- }
-
- OutputVerboseEnd();
-
- SetOwnerGroup(File); // Set the user and group.
-
- SetMode(File, S_IRUSR); // Set to readonly by owner.
- }
-
- string
- SNFIdentityConfig::GetRulebaseFileName(void) {
-
- std::string Name;
-
- Name = GetRulebasePath();
- Name += LicenseID + ".snf";
-
- return Name;
-
- }
|