Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 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 | |
| 41 | NDNS_LOG_INIT("ManagementTool"); |
| 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) { |
| 268 | Block tmp = ndn::dataBlock(ndn::tlv::Content, contents[0].c_str(), contents[0].length()); |
| 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 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 340 | //determine whether the data is a self-signed certificate |
| 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(); |
| 345 | Name keyLocator = scert.getSignature().getKeyLocator().getName(); |
| 346 | |
| 347 | //if it is, extract the content and name from the data, and resign it using the dsk. |
| 348 | shared_ptr<Regex> regex2 = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT>"); |
| 349 | BOOST_VERIFY(regex2->match(keyLocator) == true); |
| 350 | if (keyName == regex2->expand("\\1\\2")) { |
| 351 | shared_ptr<Data> pre = data; |
| 352 | Name name = pre->getName(); |
| 353 | //check whether the name is legal or not. if not converting it to a legal name |
| 354 | if (zoneName != regex1->expand("\\1")) { |
| 355 | Name comp1 = regex1->expand("\\1").getSubName(zoneName.size()); |
| 356 | Name comp2 = regex1->expand("\\2"); |
| 357 | name = zoneName; |
| 358 | name.append("KEY"); |
| 359 | name.append(comp1); |
| 360 | name.append(comp2); |
| 361 | name.append("ID-CERT"); |
| 362 | name.append(pre->getName().get(-1)); |
| 363 | } |
| 364 | |
| 365 | data = make_shared<Data>(); |
| 366 | data->setName(name); |
| 367 | data->setContent(pre->getContent()); |
| 368 | |
| 369 | m_keyChain.sign(*data, dskCertName); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | // create response for the input data |
| 374 | Response re; |
| 375 | Name hint; |
| 376 | re.fromData(hint, zoneName, *data); |
| 377 | Name label = re.getRrLabel(); |
| 378 | name::Component type = re.getRrType(); |
| 379 | |
| 380 | Rrset rrset(&zone); |
| 381 | rrset.setLabel(label); |
| 382 | rrset.setType(type); |
| 383 | if (ttl == DEFAULT_RR_TTL) |
| 384 | rrset.setTtl(zone.getTtl()); |
| 385 | else |
| 386 | rrset.setTtl(ttl); |
| 387 | rrset.setVersion(re.getVersion()); |
| 388 | rrset.setData(data->wireEncode()); |
| 389 | |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 390 | checkRrsetVersion(rrset); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 391 | NDNS_LOG_INFO("Added " << rrset); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 392 | m_dbMgr.insert(rrset); |
| 393 | } |
| 394 | |
| 395 | void |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 396 | ManagementTool::listZone(const Name& zoneName, std::ostream& os, const bool printRaw) |
| 397 | { |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 398 | Zone zone(zoneName); |
| 399 | if (!m_dbMgr.find(zone)) { |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 400 | throw Error("Zone " + zoneName.toUri() + " is not found in the database"); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | //first output the zone name |
| 404 | os << "; Zone " << zoneName.toUri() << std::endl << std::endl; |
| 405 | |
| 406 | //second output all rrsets |
| 407 | std::vector<Rrset> rrsets = m_dbMgr.findRrsets(zone); |
| 408 | |
| 409 | //set width for different columns |
| 410 | size_t labelWidth = 0; |
| 411 | size_t ttlWidth = 0; |
| 412 | size_t typeWidth = 0; |
| 413 | for (Rrset& rrset : rrsets) { |
| 414 | Data data(rrset.getData()); |
| 415 | Response re; |
| 416 | Name hint; |
| 417 | re.fromData(hint, zoneName, data); |
| 418 | |
| 419 | if (rrset.getLabel().toUri().size() > labelWidth) |
| 420 | labelWidth = rrset.getLabel().toUri().size(); |
| 421 | |
| 422 | std::stringstream seconds; |
| 423 | seconds << rrset.getTtl().count(); |
| 424 | if (seconds.str().size() > ttlWidth) |
| 425 | ttlWidth = seconds.str().size(); |
| 426 | |
| 427 | if (rrset.getType().toUri().size() > typeWidth) |
| 428 | typeWidth = rrset.getType().toUri().size(); |
| 429 | } |
| 430 | |
| 431 | //output |
| 432 | for (Rrset& rrset : rrsets) { |
| 433 | Data data(rrset.getData()); |
| 434 | Response re; |
| 435 | Name hint; |
| 436 | re.fromData(hint, zoneName, data); |
| 437 | int iteration = re.getNdnsType() == NDNS_RAW || re.getNdnsType() == NDNS_AUTH ? |
| 438 | 1 : re.getRrs().size(); |
| 439 | const std::vector<Block> &rrs = re.getRrs(); |
| 440 | |
| 441 | if (re.getNdnsType() != NDNS_RAW && re.getNdnsType() != NDNS_AUTH) { |
| 442 | os << "; rrset=" << rrset.getLabel().toUri() |
| 443 | << " type=" << rrset.getType().toUri() |
| 444 | << " version=" << rrset.getVersion().toUri() |
| 445 | << " signed-by=" << data.getSignature().getKeyLocator().getName().toUri() |
| 446 | << std::endl; |
| 447 | } |
| 448 | |
| 449 | for (int i = 0; i < iteration; i++) { |
| 450 | os.setf(os.left); |
| 451 | os.width(labelWidth + 2); |
| 452 | os << rrset.getLabel().toUri(); |
| 453 | |
| 454 | os.width(ttlWidth + 2); |
| 455 | os << rrset.getTtl().count(); |
| 456 | |
| 457 | os.width(typeWidth + 2); |
| 458 | os << rrset.getType().toUri(); |
| 459 | |
| 460 | if (re.getNdnsType() != NDNS_RAW && re.getNdnsType() != NDNS_AUTH) { |
| 461 | using namespace CryptoPP; |
| 462 | if (rrset.getType() == label::TXT_RR_TYPE) { |
| 463 | os.write(reinterpret_cast<const char*>(rrs[i].value()), rrs[i].value_size()); |
| 464 | os << std::endl; |
| 465 | } |
| 466 | else if (rrset.getType() == label::NS_RR_TYPE) { |
| 467 | //TODO output the NS data once we have it |
| 468 | os << std::endl; |
| 469 | } |
| 470 | else { |
| 471 | StringSource ss(rrs[i].wire(), rrs[i].size(), true, |
| 472 | new Base64Encoder(new FileSink(os), true, 64)); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (re.getNdnsType() == NDNS_RAW || re.getNdnsType() == NDNS_AUTH) { |
| 478 | os.width(); |
| 479 | os << "; content-type=" << re.getNdnsType() |
| 480 | << " version=" << rrset.getVersion().toUri() |
| 481 | << " signed-by=" << data.getSignature().getKeyLocator().getName().toUri(); |
| 482 | os << std::endl; |
| 483 | |
| 484 | if (printRaw && re.getNdnsType() == NDNS_RAW) { |
Jiewen Tan | d1dd86d | 2015-03-20 10:26:28 -0700 | [diff] [blame] | 485 | util::IndentedStream istream(os, "; "); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 486 | |
Jiewen Tan | d1dd86d | 2015-03-20 10:26:28 -0700 | [diff] [blame] | 487 | if (re.getRrType() == label::CERT_RR_TYPE) { |
| 488 | shared_ptr<Data> data = re.toData(); |
| 489 | IdentityCertificate cert(*data); |
| 490 | cert.printCertificate(istream); |
| 491 | } |
| 492 | else { |
| 493 | using namespace CryptoPP; |
| 494 | StringSource ss(re.getAppContent().wire(), re.getAppContent().size(), true, |
| 495 | new Base64Encoder(new FileSink(istream), true, 64)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 496 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 497 | } |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 498 | os << std::endl; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 499 | } |
| 500 | else { |
| 501 | os << std::endl; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void |
| 507 | ManagementTool::listAllZones(std::ostream& os) { |
| 508 | std::vector<Zone> zones = m_dbMgr.listZones(); |
| 509 | |
| 510 | size_t nameWidth = 0; |
| 511 | for (const Zone& zone : zones) { |
| 512 | if (zone.getName().toUri().size() > nameWidth) |
| 513 | nameWidth = zone.getName().toUri().size(); |
| 514 | } |
| 515 | |
| 516 | for (const Zone& zone : zones) { |
| 517 | os.setf(os.left); |
| 518 | os.width(nameWidth + 2); |
| 519 | os << zone.getName().toUri(); |
| 520 | |
| 521 | os << "; default-ttl=" << zone.getTtl().count(); |
| 522 | os << " default-key=" << m_keyChain.getDefaultKeyNameForIdentity(zone.getName()); |
| 523 | os << " default-certificate=" |
| 524 | << m_keyChain.getDefaultCertificateNameForIdentity(zone.getName()); |
| 525 | os << std::endl; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | void |
| 530 | ManagementTool::removeRrSet(const Name& zoneName, const Name& label, const name::Component& type) |
| 531 | { |
| 532 | Zone zone(zoneName); |
| 533 | Rrset rrset(&zone); |
| 534 | rrset.setLabel(label); |
| 535 | rrset.setType(type); |
| 536 | |
| 537 | if (!m_dbMgr.find(rrset)) { |
| 538 | return; |
| 539 | } |
| 540 | NDNS_LOG_INFO("Remove rrset with zone-id: " << zone.getId() << " label: " << label << " type: " |
| 541 | << type); |
| 542 | m_dbMgr.remove(rrset); |
| 543 | } |
| 544 | |
| 545 | void |
| 546 | ManagementTool::getRrSet(const Name& zoneName, |
| 547 | const Name& label, |
| 548 | const name::Component& type, |
| 549 | std::ostream& os) |
| 550 | { |
| 551 | Zone zone(zoneName); |
| 552 | Rrset rrset(&zone); |
| 553 | rrset.setLabel(label); |
| 554 | rrset.setType(type); |
| 555 | |
| 556 | if (!m_dbMgr.find(rrset)) { |
| 557 | os << "No record is found" << std::endl; |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | using namespace CryptoPP; |
| 562 | StringSource ss(rrset.getData().wire(), rrset.getData().size(), true, |
| 563 | new Base64Encoder(new FileSink(os), true, 64)); |
| 564 | } |
| 565 | |
| 566 | void |
| 567 | ManagementTool::addIdCert(Zone& zone, shared_ptr<IdentityCertificate> cert, |
| 568 | const time::seconds& ttl) |
| 569 | { |
| 570 | Rrset rrset(&zone); |
| 571 | size_t size = zone.getName().size(); |
| 572 | Name label = cert->getName().getSubName(size + 1, cert->getName().size() - size - 3); |
| 573 | rrset.setLabel(label); |
| 574 | rrset.setType(label::CERT_RR_TYPE); |
| 575 | rrset.setTtl(ttl); |
| 576 | rrset.setVersion(cert->getName().get(-1)); |
| 577 | rrset.setData(cert->wireEncode()); |
| 578 | |
| 579 | if (m_dbMgr.find(rrset)) { |
| 580 | throw Error("ID-CERT with label=" + label.toUri() + |
| 581 | " is already presented in local NDNS databse"); |
| 582 | } |
| 583 | NDNS_LOG_INFO("Add rrset with zone-id: " << zone.getId() << " label: " << label << " type: " |
| 584 | << label::CERT_RR_TYPE); |
| 585 | m_dbMgr.insert(rrset); |
| 586 | } |
| 587 | |
| 588 | void |
| 589 | ManagementTool::addZone(Zone& zone) |
| 590 | { |
| 591 | if (m_dbMgr.find(zone)) { |
| 592 | throw Error("Zone with Name=" + zone.getName().toUri() + |
| 593 | " is already presented in local NDNS databse"); |
| 594 | } |
| 595 | NDNS_LOG_INFO("Add zone with Name: " << zone.getName().toUri()); |
| 596 | m_dbMgr.insert(zone); |
| 597 | } |
| 598 | |
| 599 | void |
| 600 | ManagementTool::removeZone(Zone& zone) |
| 601 | { |
| 602 | if (!m_dbMgr.find(zone)) { |
| 603 | return; |
| 604 | } |
| 605 | NDNS_LOG_INFO("Remove zone with Name: " << zone.getName().toUri()); |
| 606 | m_dbMgr.remove(zone); |
| 607 | } |
| 608 | |
| 609 | bool |
| 610 | ManagementTool::matchCertificate(const Name& certName, const Name& identity) |
| 611 | { |
| 612 | if (!m_keyChain.doesCertificateExist(certName)) { |
| 613 | NDNS_LOG_WARN(certName.toUri() << " is not presented in KeyChain"); |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | //check its public key information |
| 618 | shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(certName); |
| 619 | Name keyName = cert->getPublicKeyName(); |
| 620 | |
| 621 | if (!identity.isPrefixOf(keyName) || identity.size()!=keyName.size()-1) { |
| 622 | NDNS_LOG_WARN(keyName.toUri() << " is not a key of " << identity.toUri()); |
| 623 | return false; |
| 624 | } |
| 625 | |
| 626 | if (!m_keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)) { |
| 627 | NDNS_LOG_WARN("Private key: " << keyName.toUri() << " is not presented in KeyChain"); |
| 628 | return false; |
| 629 | } |
| 630 | |
| 631 | return true; |
| 632 | } |
| 633 | |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 634 | void |
| 635 | ManagementTool::checkRrsetVersion(const Rrset& rrset) |
| 636 | { |
| 637 | Rrset originalRrset(rrset); |
| 638 | if (m_dbMgr.find(originalRrset)) { |
| 639 | // update only if rrset has a newer version |
| 640 | if (originalRrset.getVersion() == rrset.getVersion()) { |
| 641 | throw Error("Duplicate: " + boost::lexical_cast<std::string>(originalRrset)); |
| 642 | } |
| 643 | else if (originalRrset.getVersion() > rrset.getVersion()) { |
| 644 | throw Error("Newer version exists: " + boost::lexical_cast<std::string>(originalRrset)); |
| 645 | } |
| 646 | |
| 647 | m_dbMgr.remove(originalRrset); |
| 648 | } |
| 649 | } |
| 650 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 651 | } // namespace ndns |
| 652 | } // namespace ndn |