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.
5 //
6 #include <sstream>
7 #include <string.h>
8 #include <fstream>
9 #include <vector>
10 #include <pwd.h>
11 #include <ctime>
12 
15 
16 // character set same as base64, except for last two (missing are + and / )
17 static const char* b64str =
18  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
19 
20 static const std::string KEY_HEADER("Cond_Authentication_Key");
21 
22 static const std::string NAMEPREFIX("N=");
23 static const std::string KEYPREFIX("K=");
24 static const std::string OWNERPREFIX("O=");
25 
26 static const std::string DATEPREFIX("D=");
27 
28 static const std::string SERVICEPREFIX("S=");
29 static const std::string CONNECTIONPREFIX("C=");
30 static const std::string USERPREFIX("U=");
31 static const std::string PASSWORDPREFIX("P=");
32 
33 static const std::string DEFAULT_SERVICE("Cond_Default_Service");
34 
35 namespace cond {
36  char randomChar(){
37  int irand = ::rand()%(::strlen(b64str));
38  return b64str[irand];
39  }
40 
42  std::string loginName("");
43  struct passwd* userp = ::getpwuid(::getuid());
44  if(userp) {
45  char* uName = userp->pw_name;
46  if(uName){
47  loginName += uName;
48  }
49  }
50  if(loginName.empty()){
51  std::string msg("Cannot determine login name.");
52  throwException(msg,"DecodingKey::getLoginName");
53  }
54  return loginName;
55  }
56 
57  void parseLineForNamedParams( const std::string& line, std::map<std::string,std::string>& params ){
58  std::stringstream str( line );
59  std::string paramName("");
60  std::string paramValue("");
61  while( str.good() ){
62  std::string item("");
63  getline( str, item, ItemSeparator);
64  if( item.size()>3 ){
65  paramName = item.substr(0,2);
66  paramValue = item.substr(2);
67  params.insert( std::make_pair( paramName, paramValue ) );
68  }
69  }
70  }
71 
72 }
73 
75  ::srand( m_iteration+2 );
76  int rseed = ::rand();
77  int seed = ::time( NULL)%10 + rseed;
78  ::srand( seed );
79  std::string ret("");
80  for( size_t i=0;i<keySize; i++ ){
81  ret += randomChar();
82  }
83  m_iteration++;
84  return ret;
85 }
86 
88  ::srand( m_iteration+2 );
89  int rseed = ::rand();
90  int seed = ::time( NULL)%10 + rseed;
91  ::srand( seed );
92  size_t sz = rand()%maxSize;
93  return make( sz );
94 }
95 
97  std::stringstream s;
98  s<<NAMEPREFIX<<"<principal_name>"<<std::endl;
99  s<<OWNERPREFIX<<"<owner_name, optional>"<<std::endl;
100  s<<KEYPREFIX<<"<key, leave empty if generated>"<<std::endl;
101  //s<<DATEPREFIX<<"<expiring date, optional>"<<std::endl;
102  s<<SERVICEPREFIX<<"<service_name0>;"<<CONNECTIONPREFIX<<"<service0_connection_string>;"<<USERPREFIX<<"<user0_name>;"<<PASSWORDPREFIX<<"<password0>;"<<std::endl;
103  s<<SERVICEPREFIX<<"<service_name1>;"<<CONNECTIONPREFIX<<"<service1_connection_string>;"<<USERPREFIX<<"<user1_name>;"<<PASSWORDPREFIX<<"<password1>;"<<std::endl;
104  s<<SERVICEPREFIX<<"<service_name2>;"<<CONNECTIONPREFIX<<"<service2_connection_string>;"<<USERPREFIX<<"<user2_name>;"<<PASSWORDPREFIX<<"<password2>;"<<std::endl;
105  return s.str();
106 }
107 
108 size_t cond::auth::DecodingKey::init( const std::string& keyFileName, const std::string& password, bool readMode ){
109  if(keyFileName.empty()){
110  std::string msg("Provided key file name is empty.");
111  throwException(msg,"DecodingKey::init");
112  }
113  m_fileName = keyFileName;
114  m_pwd = password;
115  m_mode = readMode;
116  m_principalName.clear();
117  m_principalKey.clear();
118  m_owner.clear();
119  m_services.clear();
120  size_t nelem = 0;
121  if( m_mode ){
122  std::ifstream keyFile (m_fileName.c_str(),std::ios::in|std::ios::binary|std::ios::ate);
123  if (keyFile.is_open()){
124  size_t fsize = keyFile.tellg();
125  unsigned char* buff = (unsigned char*)malloc( fsize );
126  keyFile.seekg (0, std::ios::beg);
127  keyFile.read (reinterpret_cast<char*>(buff), fsize);
128  Cipher cipher( m_pwd );
129  std::string content = cipher.decrypt( buff, fsize );
130  free ( buff );
131  // skip the header + line separator
132  if( content.substr( 0, KEY_HEADER.size() )!=KEY_HEADER ){
133  std::string msg("Provided key content is invalid.");
134  throwException(msg,"DecodingKey::init");
135  }
136  std::stringstream str( content.substr( KEY_HEADER.size()+1) );
137  while( str.good() ){
139  getline ( str, line,LineSeparator );
140  if(line.size()>3 ){
141  if( line.substr(0,2)==NAMEPREFIX ){
142  m_principalName = line.substr(2);
143  } else if ( line.substr(0,2)== KEYPREFIX ){
144  m_principalKey = line.substr(2);
145  } else if ( line.substr(0,2)== OWNERPREFIX ){
146  m_owner = line.substr(2);
147  } else if ( line.substr(0,2)== SERVICEPREFIX ){
148  std::stringstream serviceStr( line.substr(2) );
149  std::vector<std::string> sdata;
150  while( serviceStr.good() ){
151  sdata.push_back( std::string("") );
152  getline( serviceStr, sdata.back(), ItemSeparator);
153  }
154  std::map< std::string, ServiceCredentials >::iterator iS = m_services.insert( std::make_pair( sdata[0], ServiceCredentials() ) ).first;
155  iS->second.connectionString = sdata[1];
156  iS->second.userName = sdata[2];
157  iS->second.password = sdata[3];
158  nelem++;
159  }
160  }
161  }
162  keyFile.close();
163  if( m_principalName.empty() || m_principalKey.empty() ){
164  std::string msg = "Provided key is invalid.";
165  throwException(msg,"DecodingKey::init");
166  }
167  if( !m_owner.empty() ){
168  std::string currentUser = getLoginName();
169  if(m_owner != currentUser ){
170  m_principalName.clear();
171  m_principalKey.clear();
172  m_owner.clear();
173  m_services.clear();
174  std::string msg = "Provided key is invalid for user=" + currentUser;
175  throwException(msg,"DecodingKey::init");
176  }
177  }
178  } else {
179  std::string msg = "Required Key File \""+m_fileName+"\" is missing or unreadable.";
180  throwException(msg,"DecodingKey::init");
181  }
182  }
183  return nelem;
184 }
185 
187  size_t nelem = 0;
188  if(inputFileName.empty()){
189  std::string msg("Provided input file name is empty.");
190  throwException(msg,"DecodingKey::readFromInputFile");
191  }
192  m_principalName.clear();
193  m_principalKey.clear();
194  m_owner.clear();
195  m_services.clear();
196  std::ifstream inputFile (inputFileName.c_str());
197  if (inputFile.is_open()){
198  std::map<std::string,std::string> params;
199  while ( inputFile.good() ){
201  getline (inputFile, line);
202  params.clear();
203  if(line.size()>3 ){
204  if( line.substr(0,2)==NAMEPREFIX ){
205  m_principalName = line.substr(2);
206  } else if ( line.substr(0,2)== KEYPREFIX ){
207  m_principalKey = line.substr(2);
208  } else if ( line.substr(0,2)== OWNERPREFIX ){
209  m_owner = line.substr(2);
210  } else if ( line.substr(0,2)== SERVICEPREFIX ){
211  parseLineForNamedParams( line, params );
213  ServiceCredentials creds;
214  creds.connectionString = params[ CONNECTIONPREFIX ];
215  creds.userName = params[ USERPREFIX ];
216  creds.password = params[ PASSWORDPREFIX ];
217  m_services.insert( std::make_pair( serviceName, creds ) );
218  nelem++;
219  }
220  }
221  }
222  inputFile.close();
223  if( m_principalKey.empty() && generatedKeySize){
225  m_principalKey = gen.make( generatedKeySize );
226  }
227 
228  } else {
229  std::string msg = "Provided Input File \""+inputFileName+"\n is invalid.";
230  throwException(msg,"DecodingKey::readFromInputFile");
231  }
232  return nelem;
233 }
234 
235 void cond::auth::DecodingKey::list( std::ostream& out ){
236  out <<NAMEPREFIX<<m_principalName<<std::endl;
237  out <<KEYPREFIX<<m_principalKey<<std::endl;
238  out <<OWNERPREFIX<<m_owner<<std::endl;
239  for( std::map< std::string, ServiceCredentials >::const_iterator iS = m_services.begin();
240  iS != m_services.end(); iS++ ){
241  out <<SERVICEPREFIX<<iS->first<<";";
242  out <<CONNECTIONPREFIX<<iS->second.connectionString<<";";
243  out <<USERPREFIX<<iS->second.userName<<";";
244  out <<PASSWORDPREFIX<<iS->second.password<<";"<<std::endl;
245  }
246 }
247 
249  std::ofstream outFile ( m_fileName.c_str(),std::ios::binary);
250  if (outFile.is_open()){
251  std::stringstream content;
252  content << KEY_HEADER << LineSeparator;
253  if( !m_principalName.empty() ){
254  content << NAMEPREFIX << m_principalName << LineSeparator;
255  }
256  if( !m_principalKey.empty() ){
257  content << KEYPREFIX << m_principalKey << LineSeparator;
258  }
259  if( !m_owner.empty() ){
260  content << OWNERPREFIX << m_owner << LineSeparator;
261  }
262  for( std::map< std::string, ServiceCredentials >::const_iterator iD = m_services.begin();
263  iD != m_services.end(); ++iD ){
264  content << SERVICEPREFIX << iD->first << ItemSeparator;
265  content << iD->second.connectionString << ItemSeparator;
266  content << iD->second.userName << ItemSeparator;
267  content << iD->second.password << ItemSeparator;
268  content << LineSeparator;
269  }
270  Cipher cipher( m_pwd );
271  unsigned char* out;
272  size_t outSize = cipher.encrypt( content.str(), out );
273  outFile.write( reinterpret_cast<char*>(out),outSize);
274  free (out );
275  } else {
276  std::string msg("");
277  msg += "Provided Key File \""+m_fileName+"\n is invalid.";
278  throwException(msg,"DecodingKey::flush");
279  }
280  outFile.close();
281 }
282 
284  addService( DEFAULT_SERVICE, connectionString, "", "" );
285 }
286 
289  const std::string& userName,
290  const std::string& password ){
291  std::map< std::string, ServiceCredentials >::iterator iK = m_services.find( serviceName );
292  if( iK == m_services.end() ){
293  iK = m_services.insert( std::make_pair( serviceName, ServiceCredentials() ) ).first;
294  }
295  iK->second.connectionString = connectionString;
296  iK->second.userName = userName;
297  iK->second.password = password;
298 }
299 
int i
Definition: DBlmapReader.cc:9
tuple ret
prodAgent to be discontinued
static const std::string KEYPREFIX("K=")
size_t init(const std::string &keyFileName, const std::string &password, bool readMode=true)
Definition: DecodingKey.cc:108
void addService(const std::string &serviceName, const std::string &connectionString, const std::string &userName, const std::string &password)
Definition: DecodingKey.cc:287
void parseLineForNamedParams(const std::string &line, std::map< std::string, std::string > &params)
Definition: DecodingKey.cc:57
S make(const edm::ParameterSet &cfg)
std::string getLoginName()
Definition: DecodingKey.cc:41
#define NULL
Definition: scimark2.h:8
static const std::string USERPREFIX("U=")
static const std::string serviceName
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:21
static const std::string SERVICEPREFIX("S=")
static const std::string DATEPREFIX("D=")
size_t createFromInputFile(const std::string &inputFileName, size_t generatedKeySize=0)
Definition: DecodingKey.cc:186
#define constexpr
static const char * b64str
Definition: DecodingKey.cc:17
static const std::string KEY_HEADER("Cond_Authentication_Key")
char LineSeparator
Definition: DecodingKey.cc:14
def gen
run2 Cosmic #### Run 256259 @ 0T 2015C### Run 272133 @ 3.8T 2016B###
static const std::string DEFAULT_SERVICE("Cond_Default_Service")
static std::string templateFile()
Definition: DecodingKey.cc:96
tuple maxSize
&#39;/store/data/Commissioning08/BeamHalo/RECO/StuffAlmostToP5_v1/000/061/642/10A0FE34-A67D-DD11-AD05-000...
size_t encrypt(const std::string &input, unsigned char *&output)
Definition: Cipher.cc:71
void addDefaultService(const std::string &connectionString)
Definition: DecodingKey.cc:283
static const std::string PASSWORDPREFIX("P=")
string connectionString
Definition: autoCondHLT.py:4
char ItemSeparator
Definition: DecodingKey.cc:13
static const std::string OWNERPREFIX("O=")
std::string makeWithRandomSize(size_t maxSize)
Definition: DecodingKey.cc:87
std::string make(size_t keySize)
Definition: DecodingKey.cc:74
static const std::string CONNECTIONPREFIX("C=")
std::string decrypt(const unsigned char *input, size_t inputSize)
Definition: Cipher.cc:79
static const std::string NAMEPREFIX("N=")
static const std::string keyFile("/nfshome0/hcalsw/.ReadOMDSKey")
Signal rand(Signal arg)
Definition: vlib.cc:442
char randomChar()
Definition: DecodingKey.cc:36
void list(std::ostream &out)
Definition: DecodingKey.cc:235