Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California. |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "management-tool.hpp" |
| 21 | #include "logger.hpp" |
| 22 | #include "ndns-label.hpp" |
| 23 | #include "ndns-tlv.hpp" |
| 24 | |
| 25 | #include <string> |
| 26 | #include <iomanip> |
| 27 | |
| 28 | #include <boost/filesystem/operations.hpp> |
| 29 | #include <boost/filesystem/path.hpp> |
| 30 | #include <boost/algorithm/string/replace.hpp> |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 31 | #include <boost/lexical_cast.hpp> |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 32 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 33 | #include <ndn-cxx/util/regex.hpp> |
Jiewen Tan | d1dd86d | 2015-03-20 10:26:28 -0700 | [diff] [blame] | 34 | #include <ndn-cxx/util/indented-stream.hpp> |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 35 | #include <ndn-cxx/encoding/oid.hpp> |
| 36 | #include <ndn-cxx/security/cryptopp.hpp> |
| 37 | |
| 38 | namespace ndn { |
| 39 | namespace ndns { |
| 40 | |
Alexander Afanasyev | c7c9900 | 2015-10-09 17:27:30 -0700 | [diff] [blame] | 41 | NDNS_LOG_INIT("ManagementTool") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 43 | ManagementTool::ManagementTool(const std::string& dbFile, KeyChain& keyChain) |
| 44 | : m_keyChain(keyChain) |
| 45 | , m_dbMgr(dbFile) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | ManagementTool::createZone(const Name &zoneName, |
| 51 | const Name& parentZoneName, |
| 52 | const time::seconds& cacheTtl, |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 53 | const time::seconds& certValidity, |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 54 | const Name& kskCertName, |
| 55 | const Name& dskCertName) |
| 56 | { |
| 57 | bool isRoot = zoneName == ROOT_ZONE; |
| 58 | |
| 59 | //check preconditions |
| 60 | Zone zone(zoneName, cacheTtl); |
| 61 | if (m_dbMgr.find(zone)) { |
| 62 | throw Error(zoneName.toUri() + " is already presented in the NDNS db"); |
| 63 | } |
| 64 | |
| 65 | if (!isRoot && parentZoneName.equals(zoneName)) { |
| 66 | throw Error("Parent zone name can not be the zone itself"); |
| 67 | } |
| 68 | |
| 69 | if (!isRoot && !parentZoneName.isPrefixOf(zoneName)) { |
| 70 | throw Error(parentZoneName.toUri() + " is not a prefix of " + zoneName.toUri()); |
| 71 | } |
| 72 | |
Jiewen Tan | 01693fd | 2015-03-25 20:34:45 -0700 | [diff] [blame] | 73 | // if dsk is provided, there is no need to check ksk |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 74 | if (dskCertName != DEFAULT_CERT) { |
| 75 | if (!matchCertificate(dskCertName, zoneName)) { |
| 76 | throw Error("Cannot verify DSK certificate"); |
| 77 | } |
| 78 | } |
Jiewen Tan | 01693fd | 2015-03-25 20:34:45 -0700 | [diff] [blame] | 79 | else if (kskCertName != DEFAULT_CERT) { |
| 80 | if (!matchCertificate(kskCertName, zoneName)) { |
| 81 | throw Error("Cannot verify KSK certificate"); |
| 82 | } |
| 83 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 84 | |
| 85 | if (kskCertName == DEFAULT_CERT && isRoot) { |
| 86 | throw Error("Cannot generate KSK for root zone"); |
| 87 | } |
| 88 | |
| 89 | //first generate KSK and DSK to the keyChain system, and add DSK as default |
| 90 | NDNS_LOG_INFO("Start generating KSK and DSK and their corresponding certificates"); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 91 | Name dskName; |
| 92 | shared_ptr<IdentityCertificate> dskCert; |
| 93 | if (dskCertName == DEFAULT_CERT) { |
Jiewen Tan | 01693fd | 2015-03-25 20:34:45 -0700 | [diff] [blame] | 94 | // if no dsk provided, then generate a dsk either signed by ksk auto generated or user provided |
| 95 | time::system_clock::TimePoint notBefore = time::system_clock::now(); |
| 96 | time::system_clock::TimePoint notAfter = notBefore + certValidity; |
| 97 | shared_ptr<IdentityCertificate> kskCert; |
| 98 | |
| 99 | if (kskCertName == DEFAULT_CERT) { |
| 100 | //create KSK's certificate |
| 101 | Name kskName = m_keyChain.generateRsaKeyPair(zoneName, true); |
| 102 | std::vector<CertificateSubjectDescription> kskDesc; |
| 103 | kskCert = m_keyChain.prepareUnsignedIdentityCertificate(kskName, zoneName, notBefore, |
| 104 | notAfter, kskDesc, parentZoneName); |
| 105 | kskCert->setFreshnessPeriod(cacheTtl); |
| 106 | |
| 107 | m_keyChain.selfSign(*kskCert); |
| 108 | m_keyChain.addCertificate(*kskCert); |
| 109 | NDNS_LOG_INFO("Generated KSK: " << kskCert->getName()); |
| 110 | } |
| 111 | else { |
| 112 | kskCert = m_keyChain.getCertificate(kskCertName); |
| 113 | } |
| 114 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 115 | dskName = m_keyChain.generateRsaKeyPairAsDefault(zoneName, false); |
| 116 | //create DSK's certificate |
| 117 | std::vector<CertificateSubjectDescription> dskDesc; |
| 118 | dskCert = m_keyChain.prepareUnsignedIdentityCertificate(dskName, zoneName, notBefore, notAfter, |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 119 | dskDesc, zoneName); |
| 120 | dskCert->setFreshnessPeriod(cacheTtl); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 121 | m_keyChain.sign(*dskCert, kskCert->getName()); |
| 122 | m_keyChain.addCertificateAsKeyDefault(*dskCert); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 123 | NDNS_LOG_INFO("Generated DSK: " << dskCert->getName()); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 124 | } |
| 125 | else { |
| 126 | dskCert = m_keyChain.getCertificate(dskCertName); |
| 127 | dskName = dskCert->getPublicKeyName(); |
| 128 | m_keyChain.setDefaultKeyNameForIdentity(dskName); |
| 129 | m_keyChain.setDefaultCertificateNameForKey(dskCert->getName()); |
| 130 | } |
| 131 | |
| 132 | //second add zone to the database |
| 133 | NDNS_LOG_INFO("Start adding new zone to data base"); |
| 134 | addZone(zone); |
| 135 | |
| 136 | //third create ID-cert |
| 137 | NDNS_LOG_INFO("Start creating DSK's ID-CERT"); |
| 138 | addIdCert(zone, dskCert, cacheTtl); |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | ManagementTool::deleteZone(const Name& zoneName) |
| 143 | { |
| 144 | //check pre-conditions |
| 145 | Zone zone(zoneName); |
| 146 | if (!m_dbMgr.find(zone)) { |
| 147 | throw Error(zoneName.toUri() + " is not presented in the NDNS db"); |
| 148 | } |
| 149 | |
| 150 | //first remove all rrsets of this zone from local ndns database |
| 151 | std::vector<Rrset> rrsets = m_dbMgr.findRrsets(zone); |
| 152 | for (Rrset& rrset : rrsets) { |
| 153 | m_dbMgr.remove(rrset); |
| 154 | } |
| 155 | |
| 156 | //second remove zone from local ndns database |
| 157 | removeZone(zone); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void |
| 161 | ManagementTool::exportCertificate(const Name& certName, const std::string& outFile) |
| 162 | { |
| 163 | //search for the certificate, start from KeyChain then local NDNS database |
| 164 | shared_ptr<IdentityCertificate> cert; |
| 165 | if (m_keyChain.doesCertificateExist(certName)) { |
| 166 | cert = m_keyChain.getCertificate(certName); |
| 167 | } |
| 168 | else { |
| 169 | shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>"); |
| 170 | if (regex->match(certName) != true) { |
| 171 | throw Error("Certificate name is illegal"); |
| 172 | } |
| 173 | Name zoneName = regex->expand("\\1"); |
| 174 | Name label = regex->expand("\\2"); |
| 175 | |
| 176 | Zone zone(zoneName); |
| 177 | Rrset rrset(&zone); |
| 178 | rrset.setLabel(label); |
| 179 | rrset.setType(label::CERT_RR_TYPE); |
| 180 | if (m_dbMgr.find(rrset)) { |
| 181 | Data data(rrset.getData()); |
| 182 | cert = make_shared<IdentityCertificate>(data); |
| 183 | } |
| 184 | else { |
| 185 | throw Error("Cannot find the cert: " + certName.toUri()); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (outFile == DEFAULT_IO) { |
| 190 | ndn::io::save(*cert, std::cout); |
| 191 | } |
| 192 | else { |
| 193 | ndn::io::save(*cert, outFile); |
| 194 | NDNS_LOG_INFO("save cert to file: " << outFile); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | ManagementTool::addRrSet(const Name& zoneName, |
| 200 | const Name& label, |
| 201 | const name::Component& type, |
| 202 | NdnsType ndnsType, |
| 203 | const uint64_t version, |
| 204 | const std::vector<std::string>& contents, |
| 205 | const Name& inputDskCertName, |
| 206 | const time::seconds& ttl) |
| 207 | { |
| 208 | // check pre-condition |
| 209 | Zone zone(zoneName); |
| 210 | if (!m_dbMgr.find(zone)) { |
| 211 | throw Error(zoneName.toUri() + " is not presented in the NDNS db"); |
| 212 | } |
| 213 | |
| 214 | if (ndnsType == NDNS_UNKNOWN) { |
| 215 | throw Error("The ndns type is unknown"); |
| 216 | } |
| 217 | |
| 218 | if (type == label::CERT_RR_TYPE) { |
| 219 | throw Error("It cannot handle ID-CERT rrset type"); |
| 220 | } |
| 221 | |
| 222 | // check strange rr type and ndns type combination |
| 223 | if (type == label::NS_RR_TYPE && ndnsType == NDNS_RAW) { |
| 224 | throw Error("NS cannot be of the type NDNS_RAW"); |
| 225 | } |
| 226 | |
| 227 | if (type == label::TXT_RR_TYPE && ndnsType != NDNS_RESP) { |
| 228 | throw Error("TXT cannot be of the type NDNS_RAW or NDNS_AUTH"); |
| 229 | } |
| 230 | |
| 231 | if (ndnsType == NDNS_RAW && contents.size() != 1) { |
| 232 | throw Error("NDNS_RAW must contain a single content element"); |
| 233 | } |
| 234 | |
| 235 | Name dskName; |
| 236 | Name dskCertName = inputDskCertName; |
| 237 | if (dskCertName == DEFAULT_CERT) { |
| 238 | dskName = m_keyChain.getDefaultKeyNameForIdentity(zoneName); |
| 239 | dskCertName = m_keyChain.getDefaultCertificateNameForKey(dskName); |
| 240 | } |
| 241 | else { |
| 242 | if (!matchCertificate(dskCertName, zoneName)) { |
| 243 | throw Error("Cannot verify certificate"); |
| 244 | } |
| 245 | } |
| 246 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 247 | time::seconds actualTtl = ttl; |
| 248 | if (ttl == DEFAULT_RR_TTL) |
| 249 | actualTtl = zone.getTtl(); |
| 250 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 251 | // set rrset |
| 252 | Rrset rrset(&zone); |
| 253 | rrset.setLabel(label); |
| 254 | rrset.setType(type); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 255 | rrset.setTtl(actualTtl); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 256 | |
| 257 | // set response |
| 258 | Response re; |
| 259 | re.setZone(zoneName); |
| 260 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 261 | re.setRrLabel(label); |
| 262 | re.setRrType(type); |
| 263 | re.setNdnsType(ndnsType); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 264 | re.setFreshnessPeriod(actualTtl); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 265 | |
| 266 | //set content according to ndns type |
| 267 | if (ndnsType == NDNS_RAW) { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 268 | Block tmp = makeBinaryBlock(ndn::tlv::Content, contents[0].c_str(), contents[0].length()); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 269 | re.setAppContent(tmp); |
| 270 | } |
| 271 | else if (ndnsType != NDNS_AUTH) { |
| 272 | if (contents.empty()) { |
| 273 | re.addRr(""); |
| 274 | } |
| 275 | else { |
| 276 | for (const auto& item : contents) { |
| 277 | re.addRr(item); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 282 | if (version != VERSION_USE_UNIX_TIMESTAMP) { |
| 283 | name::Component tmp = name::Component::fromVersion(version); |
| 284 | re.setVersion(tmp); |
| 285 | } |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 286 | shared_ptr<Data> data = re.toData(); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 287 | m_keyChain.sign(*data, dskCertName); |
| 288 | |
| 289 | rrset.setVersion(re.getVersion()); |
| 290 | rrset.setData(data->wireEncode()); |
| 291 | |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 292 | checkRrsetVersion(rrset); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 293 | NDNS_LOG_INFO("Added " << rrset); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 294 | m_dbMgr.insert(rrset); |
| 295 | } |
| 296 | |
| 297 | void |
| 298 | ManagementTool::addRrSet(const Name& zoneName, |
| 299 | const std::string& inFile, |
| 300 | const time::seconds& ttl, |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 301 | const Name& inputDskCertName, |
| 302 | const ndn::io::IoEncoding encoding) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 303 | { |
| 304 | //check precondition |
| 305 | Zone zone(zoneName); |
| 306 | if (!m_dbMgr.find(zone)) { |
| 307 | throw Error(zoneName.toUri() + " is not presented in the NDNS db"); |
| 308 | } |
| 309 | |
| 310 | Name dskName; |
| 311 | Name dskCertName = inputDskCertName; |
| 312 | if (dskCertName == DEFAULT_CERT) { |
| 313 | dskName = m_keyChain.getDefaultKeyNameForIdentity(zoneName); |
| 314 | dskCertName = m_keyChain.getDefaultCertificateNameForKey(dskName); |
| 315 | } |
| 316 | else { |
| 317 | if (!matchCertificate(dskCertName, zoneName)) { |
| 318 | throw Error("Cannot verify certificate"); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (inFile != DEFAULT_IO) { |
| 323 | boost::filesystem::path dir = boost::filesystem::path(inFile); |
| 324 | if (!boost::filesystem::exists(dir) || boost::filesystem::is_directory(dir)) { |
| 325 | throw Error("Data: " + inFile + " does not exist"); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | //first load the data |
| 330 | shared_ptr<Data> data; |
| 331 | if (inFile == DEFAULT_IO) |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 332 | data = ndn::io::load<ndn::Data>(std::cin, encoding); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 333 | else |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 334 | data = ndn::io::load<ndn::Data>(inFile, encoding); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 335 | |
Jiewen Tan | d2d2182 | 2015-03-19 15:37:03 -0700 | [diff] [blame] | 336 | if (data == nullptr) { |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 337 | throw Error("input does not contain a valid Data packet"); |
Jiewen Tan | d2d2182 | 2015-03-19 15:37:03 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame^] | 340 | // determine whether the data is a self-signed certificate |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 341 | shared_ptr<Regex> regex1 = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>"); |
| 342 | if (regex1->match(data->getName())) { |
| 343 | IdentityCertificate scert(*data); |
| 344 | Name keyName = scert.getPublicKeyName(); |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame^] | 345 | if (keyName.getPrefix(zoneName.size()) != zoneName) { |
| 346 | throw Error("the input key does not belong to the zone"); |
| 347 | } |
| 348 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 349 | Name keyLocator = scert.getSignature().getKeyLocator().getName(); |
| 350 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame^] | 351 | // if it is, extract the content and name from the data, and resign it using the dsk. |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 352 | shared_ptr<Regex> regex2 = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT>"); |
| 353 | BOOST_VERIFY(regex2->match(keyLocator) == true); |
| 354 | if (keyName == regex2->expand("\\1\\2")) { |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame^] | 355 | |
| 356 | Name canonicalName; |
| 357 | canonicalName |
| 358 | .append(zoneName) |
| 359 | .append("KEY") |
| 360 | .append(keyName.getSubName(zoneName.size(), keyName.size() - zoneName.size())) |
| 361 | .append("ID-CERT") |
| 362 | .append(data->getName().get(-1)); |
| 363 | |
| 364 | if (data->getName() != canonicalName) { |
| 365 | // name need to be adjusted |
| 366 | auto newData = make_shared<Data>(); |
| 367 | newData->setName(canonicalName); |
| 368 | newData->setMetaInfo(data->getMetaInfo()); |
| 369 | newData->setContent(data->getContent()); |
| 370 | m_keyChain.sign(*newData); |
| 371 | |
| 372 | data = newData; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 373 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | // create response for the input data |
| 378 | Response re; |
| 379 | Name hint; |
| 380 | re.fromData(hint, zoneName, *data); |
| 381 | Name label = re.getRrLabel(); |
| 382 | name::Component type = re.getRrType(); |
| 383 | |
| 384 | Rrset rrset(&zone); |
| 385 | rrset.setLabel(label); |
| 386 | rrset.setType(type); |
| 387 | if (ttl == DEFAULT_RR_TTL) |
| 388 | rrset.setTtl(zone.getTtl()); |
| 389 | else |
| 390 | rrset.setTtl(ttl); |
| 391 | rrset.setVersion(re.getVersion()); |
| 392 | rrset.setData(data->wireEncode()); |
| 393 | |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 394 | checkRrsetVersion(rrset); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 395 | NDNS_LOG_INFO("Added " << rrset); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 396 | m_dbMgr.insert(rrset); |
| 397 | } |
| 398 | |
| 399 | void |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 400 | ManagementTool::listZone(const Name& zoneName, std::ostream& os, const bool printRaw) |
| 401 | { |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 402 | Zone zone(zoneName); |
| 403 | if (!m_dbMgr.find(zone)) { |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 404 | throw Error("Zone " + zoneName.toUri() + " is not found in the database"); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | //first output the zone name |
| 408 | os << "; Zone " << zoneName.toUri() << std::endl << std::endl; |
| 409 | |
| 410 | //second output all rrsets |
| 411 | std::vector<Rrset> rrsets = m_dbMgr.findRrsets(zone); |
| 412 | |
| 413 | //set width for different columns |
| 414 | size_t labelWidth = 0; |
| 415 | size_t ttlWidth = 0; |
| 416 | size_t typeWidth = 0; |
| 417 | for (Rrset& rrset : rrsets) { |
| 418 | Data data(rrset.getData()); |
| 419 | Response re; |
| 420 | Name hint; |
| 421 | re.fromData(hint, zoneName, data); |
| 422 | |
| 423 | if (rrset.getLabel().toUri().size() > labelWidth) |
| 424 | labelWidth = rrset.getLabel().toUri().size(); |
| 425 | |
| 426 | std::stringstream seconds; |
| 427 | seconds << rrset.getTtl().count(); |
| 428 | if (seconds.str().size() > ttlWidth) |
| 429 | ttlWidth = seconds.str().size(); |
| 430 | |
| 431 | if (rrset.getType().toUri().size() > typeWidth) |
| 432 | typeWidth = rrset.getType().toUri().size(); |
| 433 | } |
| 434 | |
| 435 | //output |
| 436 | for (Rrset& rrset : rrsets) { |
| 437 | Data data(rrset.getData()); |
| 438 | Response re; |
| 439 | Name hint; |
| 440 | re.fromData(hint, zoneName, data); |
| 441 | int iteration = re.getNdnsType() == NDNS_RAW || re.getNdnsType() == NDNS_AUTH ? |
| 442 | 1 : re.getRrs().size(); |
| 443 | const std::vector<Block> &rrs = re.getRrs(); |
| 444 | |
| 445 | if (re.getNdnsType() != NDNS_RAW && re.getNdnsType() != NDNS_AUTH) { |
| 446 | os << "; rrset=" << rrset.getLabel().toUri() |
| 447 | << " type=" << rrset.getType().toUri() |
| 448 | << " version=" << rrset.getVersion().toUri() |
| 449 | << " signed-by=" << data.getSignature().getKeyLocator().getName().toUri() |
| 450 | << std::endl; |
| 451 | } |
| 452 | |
| 453 | for (int i = 0; i < iteration; i++) { |
| 454 | os.setf(os.left); |
| 455 | os.width(labelWidth + 2); |
| 456 | os << rrset.getLabel().toUri(); |
| 457 | |
| 458 | os.width(ttlWidth + 2); |
| 459 | os << rrset.getTtl().count(); |
| 460 | |
| 461 | os.width(typeWidth + 2); |
| 462 | os << rrset.getType().toUri(); |
| 463 | |
| 464 | if (re.getNdnsType() != NDNS_RAW && re.getNdnsType() != NDNS_AUTH) { |
| 465 | using namespace CryptoPP; |
| 466 | if (rrset.getType() == label::TXT_RR_TYPE) { |
| 467 | os.write(reinterpret_cast<const char*>(rrs[i].value()), rrs[i].value_size()); |
| 468 | os << std::endl; |
| 469 | } |
| 470 | else if (rrset.getType() == label::NS_RR_TYPE) { |
| 471 | //TODO output the NS data once we have it |
| 472 | os << std::endl; |
| 473 | } |
| 474 | else { |
| 475 | StringSource ss(rrs[i].wire(), rrs[i].size(), true, |
| 476 | new Base64Encoder(new FileSink(os), true, 64)); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if (re.getNdnsType() == NDNS_RAW || re.getNdnsType() == NDNS_AUTH) { |
| 482 | os.width(); |
| 483 | os << "; content-type=" << re.getNdnsType() |
| 484 | << " version=" << rrset.getVersion().toUri() |
| 485 | << " signed-by=" << data.getSignature().getKeyLocator().getName().toUri(); |
| 486 | os << std::endl; |
| 487 | |
| 488 | if (printRaw && re.getNdnsType() == NDNS_RAW) { |
Jiewen Tan | d1dd86d | 2015-03-20 10:26:28 -0700 | [diff] [blame] | 489 | util::IndentedStream istream(os, "; "); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 490 | |
Jiewen Tan | d1dd86d | 2015-03-20 10:26:28 -0700 | [diff] [blame] | 491 | if (re.getRrType() == label::CERT_RR_TYPE) { |
| 492 | shared_ptr<Data> data = re.toData(); |
| 493 | IdentityCertificate cert(*data); |
| 494 | cert.printCertificate(istream); |
| 495 | } |
| 496 | else { |
| 497 | using namespace CryptoPP; |
| 498 | StringSource ss(re.getAppContent().wire(), re.getAppContent().size(), true, |
| 499 | new Base64Encoder(new FileSink(istream), true, 64)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 500 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 501 | } |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 502 | os << std::endl; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 503 | } |
| 504 | else { |
| 505 | os << std::endl; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | void |
| 511 | ManagementTool::listAllZones(std::ostream& os) { |
| 512 | std::vector<Zone> zones = m_dbMgr.listZones(); |
| 513 | |
| 514 | size_t nameWidth = 0; |
| 515 | for (const Zone& zone : zones) { |
| 516 | if (zone.getName().toUri().size() > nameWidth) |
| 517 | nameWidth = zone.getName().toUri().size(); |
| 518 | } |
| 519 | |
| 520 | for (const Zone& zone : zones) { |
| 521 | os.setf(os.left); |
| 522 | os.width(nameWidth + 2); |
| 523 | os << zone.getName().toUri(); |
| 524 | |
| 525 | os << "; default-ttl=" << zone.getTtl().count(); |
| 526 | os << " default-key=" << m_keyChain.getDefaultKeyNameForIdentity(zone.getName()); |
| 527 | os << " default-certificate=" |
| 528 | << m_keyChain.getDefaultCertificateNameForIdentity(zone.getName()); |
| 529 | os << std::endl; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | void |
| 534 | ManagementTool::removeRrSet(const Name& zoneName, const Name& label, const name::Component& type) |
| 535 | { |
| 536 | Zone zone(zoneName); |
| 537 | Rrset rrset(&zone); |
| 538 | rrset.setLabel(label); |
| 539 | rrset.setType(type); |
| 540 | |
| 541 | if (!m_dbMgr.find(rrset)) { |
| 542 | return; |
| 543 | } |
| 544 | NDNS_LOG_INFO("Remove rrset with zone-id: " << zone.getId() << " label: " << label << " type: " |
| 545 | << type); |
| 546 | m_dbMgr.remove(rrset); |
| 547 | } |
| 548 | |
| 549 | void |
| 550 | ManagementTool::getRrSet(const Name& zoneName, |
| 551 | const Name& label, |
| 552 | const name::Component& type, |
| 553 | std::ostream& os) |
| 554 | { |
| 555 | Zone zone(zoneName); |
| 556 | Rrset rrset(&zone); |
| 557 | rrset.setLabel(label); |
| 558 | rrset.setType(type); |
| 559 | |
| 560 | if (!m_dbMgr.find(rrset)) { |
| 561 | os << "No record is found" << std::endl; |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | using namespace CryptoPP; |
| 566 | StringSource ss(rrset.getData().wire(), rrset.getData().size(), true, |
| 567 | new Base64Encoder(new FileSink(os), true, 64)); |
| 568 | } |
| 569 | |
| 570 | void |
| 571 | ManagementTool::addIdCert(Zone& zone, shared_ptr<IdentityCertificate> cert, |
| 572 | const time::seconds& ttl) |
| 573 | { |
| 574 | Rrset rrset(&zone); |
| 575 | size_t size = zone.getName().size(); |
| 576 | Name label = cert->getName().getSubName(size + 1, cert->getName().size() - size - 3); |
| 577 | rrset.setLabel(label); |
| 578 | rrset.setType(label::CERT_RR_TYPE); |
| 579 | rrset.setTtl(ttl); |
| 580 | rrset.setVersion(cert->getName().get(-1)); |
| 581 | rrset.setData(cert->wireEncode()); |
| 582 | |
| 583 | if (m_dbMgr.find(rrset)) { |
| 584 | throw Error("ID-CERT with label=" + label.toUri() + |
| 585 | " is already presented in local NDNS databse"); |
| 586 | } |
| 587 | NDNS_LOG_INFO("Add rrset with zone-id: " << zone.getId() << " label: " << label << " type: " |
| 588 | << label::CERT_RR_TYPE); |
| 589 | m_dbMgr.insert(rrset); |
| 590 | } |
| 591 | |
| 592 | void |
| 593 | ManagementTool::addZone(Zone& zone) |
| 594 | { |
| 595 | if (m_dbMgr.find(zone)) { |
| 596 | throw Error("Zone with Name=" + zone.getName().toUri() + |
| 597 | " is already presented in local NDNS databse"); |
| 598 | } |
| 599 | NDNS_LOG_INFO("Add zone with Name: " << zone.getName().toUri()); |
| 600 | m_dbMgr.insert(zone); |
| 601 | } |
| 602 | |
| 603 | void |
| 604 | ManagementTool::removeZone(Zone& zone) |
| 605 | { |
| 606 | if (!m_dbMgr.find(zone)) { |
| 607 | return; |
| 608 | } |
| 609 | NDNS_LOG_INFO("Remove zone with Name: " << zone.getName().toUri()); |
| 610 | m_dbMgr.remove(zone); |
| 611 | } |
| 612 | |
| 613 | bool |
| 614 | ManagementTool::matchCertificate(const Name& certName, const Name& identity) |
| 615 | { |
| 616 | if (!m_keyChain.doesCertificateExist(certName)) { |
| 617 | NDNS_LOG_WARN(certName.toUri() << " is not presented in KeyChain"); |
| 618 | return false; |
| 619 | } |
| 620 | |
| 621 | //check its public key information |
| 622 | shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(certName); |
| 623 | Name keyName = cert->getPublicKeyName(); |
| 624 | |
| 625 | if (!identity.isPrefixOf(keyName) || identity.size()!=keyName.size()-1) { |
| 626 | NDNS_LOG_WARN(keyName.toUri() << " is not a key of " << identity.toUri()); |
| 627 | return false; |
| 628 | } |
| 629 | |
Alexander Afanasyev | 74975e1 | 2016-08-01 14:24:03 -0700 | [diff] [blame] | 630 | if (!m_keyChain.doesKeyExistInTpm(keyName, KeyClass::PRIVATE)) { |
| 631 | NDNS_LOG_WARN("Private key: " << keyName.toUri() << " is not present in KeyChain"); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 632 | return false; |
| 633 | } |
| 634 | |
| 635 | return true; |
| 636 | } |
| 637 | |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 638 | void |
| 639 | ManagementTool::checkRrsetVersion(const Rrset& rrset) |
| 640 | { |
| 641 | Rrset originalRrset(rrset); |
| 642 | if (m_dbMgr.find(originalRrset)) { |
| 643 | // update only if rrset has a newer version |
| 644 | if (originalRrset.getVersion() == rrset.getVersion()) { |
| 645 | throw Error("Duplicate: " + boost::lexical_cast<std::string>(originalRrset)); |
| 646 | } |
| 647 | else if (originalRrset.getVersion() > rrset.getVersion()) { |
| 648 | throw Error("Newer version exists: " + boost::lexical_cast<std::string>(originalRrset)); |
| 649 | } |
| 650 | |
| 651 | m_dbMgr.remove(originalRrset); |
| 652 | } |
| 653 | } |
| 654 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 655 | } // namespace ndns |
| 656 | } // namespace ndn |