00001 #include "CondCore/DBCommon/interface/DecodingKey.h" 00002 #include "CondCore/DBCommon/interface/FileUtils.h" 00003 #include "CondCore/DBCommon/interface/Exception.h" 00004 #include "CoralCommon/Cipher.h" 00005 #include <sstream> 00006 #include <fstream> 00007 #include <unistd.h> 00008 #include <pwd.h> 00009 00010 static char DecodingKeySeparator(';'); 00011 00012 bool cond::DecodingKey::readUserKey(const std::string& keyFileName){ 00013 return readFromFile(getUserName(), keyFileName); 00014 } 00015 00016 bool cond::DecodingKey::readUserKeyString(const std::string& content){ 00017 return readFromString(getUserName(), content); 00018 } 00019 00020 bool cond::DecodingKey::readFromFile(const std::string& password, const std::string& keyFileName){ 00021 cond::FileReader reader; 00022 reader.read(keyFileName); 00023 return readFromString(password,reader.content()); 00024 } 00025 00026 bool cond::DecodingKey::readFromString(const std::string& password, const std::string& content){ 00027 std::string decodedContent = coral::Cipher::decode(content,password); 00028 size_t pos = decodedContent.find(DecodingKeySeparator); 00029 if(pos==std::string::npos || pos==0){ 00030 std::stringstream msg; 00031 msg << "Provided Key is invalid."; 00032 throw cond::Exception(msg.str()); 00033 } 00034 m_key = decodedContent.substr(0,pos); 00035 m_dataSource = decodedContent.substr(pos+1); 00036 return true; 00037 } 00038 00039 bool cond::DecodingKey::validateKey(const std::string& key){ 00040 if(key.find(DecodingKeySeparator)!=std::string::npos){ 00041 std::stringstream msg; 00042 msg << "Invalid character ';' found in key string."; 00043 throw cond::Exception(msg.str()); 00044 } 00045 return true; 00046 } 00047 00048 00049 std::string cond::DecodingKey::getUserName(){ 00050 std::string userName(""); 00051 struct passwd* userp = ::getpwuid(::getuid()); 00052 if(userp) { 00053 char* uName = userp->pw_name; 00054 if(uName){ 00055 userName += uName; 00056 } 00057 } 00058 if(userName.empty()){ 00059 std::stringstream msg; 00060 msg << "Cannot determine login name."; 00061 throw cond::Exception(msg.str()); 00062 } 00063 return userName; 00064 } 00065 00066 bool cond::DecodingKey::createFile(const std::string& password, const std::string& key, 00067 const std::string& dataSource, const std::string& keyFileName){ 00068 if(password.empty()){ 00069 std::stringstream msg; 00070 msg << "Provided password is empty."; 00071 throw cond::Exception(msg.str()); 00072 } 00073 std::string content(""); 00074 validateKey(key); 00075 if(dataSource.find(DecodingKeySeparator)!=std::string::npos){ 00076 std::stringstream msg; 00077 msg << "Invalid character ';' found in data file name string."; 00078 throw cond::Exception(msg.str()); 00079 } 00080 content.append(key).append(1,DecodingKeySeparator).append(dataSource); 00081 std::string encodedContent = coral::Cipher::encode(content,password); 00082 std::ofstream keyFile; 00083 keyFile.open(keyFileName.c_str()); 00084 if(!keyFile.good()){ 00085 keyFile.close(); 00086 std::stringstream msg; 00087 msg << "Cannot open the key file \""<<keyFileName<<"\""; 00088 throw cond::Exception(msg.str()); 00089 } 00090 keyFile << encodedContent; 00091 keyFile.flush(); 00092 keyFile.close(); 00093 return true; 00094 } 00095