Changeset 1758

Show
Ignore:
Timestamp:
12/27/10 16:57:14 (2 years ago)
Author:
ahu
Message:

this implies that there will be more dnsseckeepers - database backed for example

Files:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/pdns/pdns/fsdnsseckeeper.cc

    r1755 r1758  
    6363} 
    6464 
    65 bool DNSSECKeeper::haveKSKFor(const std::string& zone, DNSSECPrivateKey* dpk) 
    66 { 
    67   fs::path full_path = fs::system_complete( fs::path(d_dirname + "/" + zone + "/ksks/" ) ); 
     65bool DNSSECKeeper::haveActiveKSKFor(const std::string& zone, DNSSECPrivateKey* dpk) 
     66{ 
     67  keyset_t keys = getKeys(zone, true); 
     68  // need to get an *active* one! 
     69  if(dpk && !keys.empty()) { 
     70    *dpk = keys.begin()->first; 
     71  } 
     72  return !keys.empty(); 
     73   
     74  #if 0 
     75  fs::path full_path = fs::system_complete( fs::path(d_dirname + "/" + zone + "/keys/" ) ); 
    6876 
    6977  if ( !fs::exists( full_path ) ) 
     
    95103 
    96104  return false; 
     105  #endif 
    97106} 
    98107 
     
    117126} 
    118127 
    119 void DNSSECKeeper::addZSKFor(const std::string& name, int algorithm, bool active) 
     128std::string DNSSECKeeper::getKeyFilenameById(const std::string& dirname, unsigned int id) 
     129{ 
     130  fs::path full_path = fs::system_complete( fs::path(dirname)); 
     131 
     132  if ( !fs::exists( full_path ) ) 
     133    unixDie("Unable to get free key id from '"+dirname+"'"); 
     134 
     135  fs::directory_iterator end_iter; 
     136  pair<string, string> parts; 
     137  for ( fs::directory_iterator dir_itr( full_path ); 
     138    dir_itr != end_iter; 
     139    ++dir_itr ) 
     140  { 
     141    parts = splitField(dir_itr->leaf(), '-'); 
     142          if(atoi(parts.first.c_str()) == (signed int)id)  
     143      return dirname+"/"+dir_itr->leaf(); 
     144  } 
     145  throw runtime_error("Could not get filename for key id '"+lexical_cast<string>(id)+"'"); 
     146} 
     147 
     148 
     149void DNSSECKeeper::addKey(const std::string& name, bool keyOrZone, int algorithm, bool active) 
    120150{ 
    121151  DNSSECPrivateKey dpk; 
     
    126156  drc.d_flags = 256; // KSK 
    127157  drc.d_algorithm = algorithm;  
    128   string iscName=d_dirname+"/"+name+"/zsks/"; 
     158  string iscName=d_dirname+"/"+name+"/keys/"; 
    129159  unsigned int id = getNextKeyIDFromDir(iscName); 
    130160  time_t inception=time(0); 
     
    133163  gmtime_r(&inception, &ts); 
    134164 
    135   iscName += (boost::format("%06d-%04d%02d%02d%02d%02d") % id 
     165  iscName += (boost::format("%06d-%04d%02d%02d%02d%02d.%u") % id 
    136166              % (1900+ts.tm_year) % (ts.tm_mon + 1) 
    137               % ts.tm_mday % ts.tm_hour % ts.tm_min).str(); 
    138  
     167              % ts.tm_mday % ts.tm_hour % ts.tm_min % drc.getTag()).str(); 
     168 
     169  iscName += keyOrZone ? ".ksk" : ".zsk"; 
    139170  iscName += active ? ".active" : ".passive"; 
    140171   
     
    152183 
    153184 
    154 static bool zskCompareByID(const DNSSECKeeper::zskset_t::value_type& a, const DNSSECKeeper::zskset_t::value_type& b) 
     185static bool zskCompareByID(const DNSSECKeeper::keyset_t::value_type& a, const DNSSECKeeper::keyset_t::value_type& b) 
    155186{ 
    156187  return a.second.id < b.second.id; 
    157188} 
    158189 
    159 void DNSSECKeeper::deleteZSKFor(const std::string& zname, const std::string& fname) 
    160 { 
    161   unlink((d_dirname +"/"+ zname +"/zsks/"+fname).c_str()); 
     190void DNSSECKeeper::removeKey(const std::string& zname, unsigned int id) 
     191{ 
     192  // unlink((d_dirname +"/"+ zname +"/zsks/"+fname).c_str()); 
     193  abort(); 
     194} 
     195 
     196void DNSSECKeeper::deactivateKey(const std::string& zname, unsigned int id) 
     197{ 
     198  // unlink((d_dirname +"/"+ zname +"/zsks/"+fname).c_str()); 
     199  string fname = getKeyFilenameById(d_dirname+"/keys/", id); 
     200  string newname = boost::replace_last_copy(fname, ".active", ".passive"); 
     201  if(rename(fname.c_str(), newname.c_str()) < 0) 
     202    unixDie("renaming file to deactivate key, from: '"+fname+"' to '"+newname+"'"); 
     203} 
     204 
     205void DNSSECKeeper::activateKey(const std::string& zname, unsigned int id) 
     206{ 
     207  // unlink((d_dirname +"/"+ zname +"/zsks/"+fname).c_str()); 
     208  abort(); 
    162209} 
    163210 
     
    202249 
    203250 
    204 DNSSECKeeper::zskset_t DNSSECKeeper::getZSKsFor(const std::string& zone, bool all) 
    205 { 
    206   zskset_t zskset; 
    207  
    208   fs::path full_path = fs::system_complete( fs::path(d_dirname + "/" + zone + "/zsks/" ) ); 
     251DNSSECKeeper::keyset_t DNSSECKeeper::getKeys(const std::string& zone, boost::tribool allOrKeyOrZone) 
     252{ 
     253  keyset_t keyset; 
     254 
     255  fs::path full_path = fs::system_complete( fs::path(d_dirname + "/" + zone + "/keys/" ) ); 
    209256 
    210257  if ( !fs::exists( full_path ) ) 
    211     return zskset; 
     258    return keyset; 
    212259 
    213260  fs::directory_iterator end_iter; 
     
    216263        ++dir_itr ) 
    217264  { 
    218     //    cerr<<"Entry: '"<< dir_itr->leaf() <<"'"<<endl; 
     265    //cerr<<"Entry: '"<< dir_itr->leaf() <<"'"<<endl; 
    219266    if(ends_with(dir_itr->leaf(),".isc")) { 
    220       //cerr<<"Hit!"<<endl; 
    221267      DNSSECPrivateKey dpk; 
    222268      getRSAKeyFromISC(&dpk.d_key.getContext(), dir_itr->path().file_string().c_str()); 
     
    236282      unsigned int id; 
    237283      sscanf(dir_itr->leaf().c_str(), "%06u-%04d%02d%02d%02d%02d", 
    238                 &id, 
    239              &ts1.tm_year,  
    240              &ts1.tm_mon, &ts1.tm_mday, &ts1.tm_hour, &ts1.tm_min); 
     284        &id, 
     285        &ts1.tm_year,  
     286        &ts1.tm_mon, &ts1.tm_mday, &ts1.tm_hour, &ts1.tm_min); 
    241287              
    242288 
     
    247293      KeyMetaData kmd; 
    248294       
    249           kmd.id = id; 
     295      kmd.id = id; 
    250296      kmd.fname = dir_itr->leaf(); 
    251297      kmd.active = kmd.fname.find(".active") != string::npos; 
    252       zskset.push_back(make_pair(dpk, kmd)); 
     298      kmd.keyOrZone = kmd.fname.find(".ksk") != string::npos; 
     299      if(boost::indeterminate(allOrKeyOrZone) || allOrKeyOrZone == kmd.keyOrZone) 
     300        keyset.push_back(make_pair(dpk, kmd)); 
    253301    } 
    254     sort(zskset.begin(), zskset.end(), zskCompareByID); 
    255   } 
    256  
    257   return zskset; 
     302    sort(keyset.begin(), keyset.end(), zskCompareByID); 
     303  } 
     304 
     305  return keyset; 
    258306} 
    259307 
     
    267315{ 
    268316  mkdir((d_dirname+"/"+name).c_str(), 0700); 
    269   mkdir((d_dirname+"/"+name+"/ksks").c_str(), 0700); 
    270   if(mkdir((d_dirname+"/"+name+"/zsks").c_str(), 0700) < 0) 
     317  if(mkdir((d_dirname+"/"+name+"/keys").c_str(), 0700) < 0) 
    271318    unixDie("Making directory for keys in '"+d_dirname+"'"); 
    272319 
     320 
    273321  // now add the KSK 
     322 
     323  addKey(name, true, algorithm); 
     324#if 0 
    274325 
    275326  DNSSECPrivateKey dpk; 
     
    280331  drc.d_flags = 257; // ZSK (?? for a KSK?) 
    281332  drc.d_algorithm = algorithm;   
    282   string iscName=d_dirname+"/"+name+"/ksks/"; 
     333  string iscName=d_dirname+"/"+name+"/keys/"; 
    283334 
    284335  time_t now=time(0); 
     
    286337  gmtime_r(&now, &ts); 
    287338  unsigned int id=1; 
    288   iscName += (boost::format("%06d-%04d%02d%02d%02d%02d.%u") % id 
     339  iscName += (boost::format("%06d-%04d%02d%02d%02d%02d.%u.%s.%s") % id 
    289340              % (1900+ts.tm_year) % (ts.tm_mon + 1) 
    290               % ts.tm_mday % ts.tm_hour % ts.tm_min % drc.getTag()).str(); 
     341              % ts.tm_mday % ts.tm_hour % ts.tm_min % drc.getTag() % "ksk" % "active").str(); 
    291342 
    292343 
     
    300351    dnskeyFile << toCanonic("", name) << " IN DNSKEY " << drc.getZoneRepresentation()<<endl; 
    301352  } 
    302  
     353#endif 
    303354} 
    304355