CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DecodingKey.cc
Go to the documentation of this file.
4 #include "CoralCommon/Cipher.h"
5 #include <sstream>
6 #include <fstream>
7 #include <unistd.h>
8 #include <pwd.h>
9 
10 static char DecodingKeySeparator(';');
11 
12 bool cond::DecodingKey::readUserKey(const std::string& keyFileName){
13  return readFromFile(getUserName(), keyFileName);
14 }
15 
16 bool cond::DecodingKey::readUserKeyString(const std::string& content){
17  return readFromString(getUserName(), content);
18 }
19 
20 bool cond::DecodingKey::readFromFile(const std::string& password, const std::string& keyFileName){
21  cond::FileReader reader;
22  reader.read(keyFileName);
23  return readFromString(password,reader.content());
24 }
25 
26 bool cond::DecodingKey::readFromString(const std::string& password, const std::string& content){
27  std::string decodedContent = coral::Cipher::decode(content,password);
28  size_t pos = decodedContent.find(DecodingKeySeparator);
29  if(pos==std::string::npos || pos==0){
30  std::stringstream msg;
31  msg << "Provided Key is invalid.";
32  throw cond::Exception(msg.str());
33  }
34  m_key = decodedContent.substr(0,pos);
35  m_dataSource = decodedContent.substr(pos+1);
36  return true;
37 }
38 
39 bool cond::DecodingKey::validateKey(const std::string& key){
40  if(key.find(DecodingKeySeparator)!=std::string::npos){
41  std::stringstream msg;
42  msg << "Invalid character ';' found in key string.";
43  throw cond::Exception(msg.str());
44  }
45  return true;
46 }
47 
48 
50  std::string userName("");
51  struct passwd* userp = ::getpwuid(::getuid());
52  if(userp) {
53  char* uName = userp->pw_name;
54  if(uName){
55  userName += uName;
56  }
57  }
58  if(userName.empty()){
59  std::stringstream msg;
60  msg << "Cannot determine login name.";
61  throw cond::Exception(msg.str());
62  }
63  return userName;
64 }
65 
66 bool cond::DecodingKey::createFile(const std::string& password, const std::string& key,
67  const std::string& dataSource, const std::string& keyFileName){
68  if(password.empty()){
69  std::stringstream msg;
70  msg << "Provided password is empty.";
71  throw cond::Exception(msg.str());
72  }
73  std::string content("");
74  validateKey(key);
75  if(dataSource.find(DecodingKeySeparator)!=std::string::npos){
76  std::stringstream msg;
77  msg << "Invalid character ';' found in data file name string.";
78  throw cond::Exception(msg.str());
79  }
80  content.append(key).append(1,DecodingKeySeparator).append(dataSource);
81  std::string encodedContent = coral::Cipher::encode(content,password);
82  std::ofstream keyFile;
83  keyFile.open(keyFileName.c_str());
84  if(!keyFile.good()){
85  keyFile.close();
86  std::stringstream msg;
87  msg << "Cannot open the key file \""<<keyFileName<<"\"";
88  throw cond::Exception(msg.str());
89  }
90  keyFile << encodedContent;
91  keyFile.flush();
92  keyFile.close();
93  return true;
94 }
95 
static bool validateKey(const std::string &key)
Definition: DecodingKey.cc:39
bool readUserKeyString(const std::string &content)
Definition: DecodingKey.cc:16
bool readFromString(const std::string &password, const std::string &content)
Definition: DecodingKey.cc:26
bool read(const std::string &fileName)
Definition: FileUtils.cc:6
bool decode(bool &, std::string const &)
Definition: types.cc:67
static char DecodingKeySeparator(';')
static std::string getUserName()
Definition: DecodingKey.cc:49
static bool createFile(const std::string &password, const std::string &key, const std::string &dataSource, const std::string &keyFileName)
Definition: DecodingKey.cc:66
const std::string & content() const
Definition: FileUtils.h:31
list key
Definition: combine.py:13
static const std::string keyFile("/nfshome0/hcalsw/.ReadOMDSKey")
bool readFromFile(const std::string &password, const std::string &keyFileName)
Definition: DecodingKey.cc:20
bool readUserKey(const std::string &keyFileName)
Definition: DecodingKey.cc:12