Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Junxiao Shi | 81e9876 | 2022-01-11 18:17:24 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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" |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 24 | #include "util/cert-helper.hpp" |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 25 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 26 | #include <iomanip> |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 27 | #include <iostream> |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 28 | |
| 29 | #include <boost/filesystem/operations.hpp> |
| 30 | #include <boost/filesystem/path.hpp> |
| 31 | #include <boost/algorithm/string/replace.hpp> |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 32 | #include <boost/lexical_cast.hpp> |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 33 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 34 | #include <ndn-cxx/util/regex.hpp> |
Jiewen Tan | d1dd86d | 2015-03-20 10:26:28 -0700 | [diff] [blame] | 35 | #include <ndn-cxx/util/indented-stream.hpp> |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 36 | #include <ndn-cxx/util/io.hpp> |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 37 | #include <ndn-cxx/link.hpp> |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 38 | #include <ndn-cxx/security/signing-helpers.hpp> |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 39 | #include <ndn-cxx/security/transform.hpp> |
| 40 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 41 | namespace ndn { |
| 42 | namespace ndns { |
| 43 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 44 | NDNS_LOG_INIT(ManagementTool); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 45 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 46 | using security::transform::base64Encode; |
| 47 | using security::transform::streamSink; |
| 48 | using security::transform::bufferSource; |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 49 | using security::Certificate; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 51 | ManagementTool::ManagementTool(const std::string& dbFile, KeyChain& keyChain) |
| 52 | : m_keyChain(keyChain) |
| 53 | , m_dbMgr(dbFile) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 57 | Zone |
| 58 | ManagementTool::createZone(const Name& zoneName, |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 59 | const Name& parentZoneName, |
| 60 | const time::seconds& cacheTtl, |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 61 | const time::seconds& certValidity, |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 62 | const Name& kskCertName, |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 63 | const Name& dskCertName, |
| 64 | const Name& dkeyCertName) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 65 | { |
| 66 | bool isRoot = zoneName == ROOT_ZONE; |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 67 | Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 68 | |
| 69 | //check preconditions |
| 70 | Zone zone(zoneName, cacheTtl); |
| 71 | if (m_dbMgr.find(zone)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 72 | NDN_THROW(Error(zoneName.toUri() + " is already present in the NDNS db")); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if (!isRoot && parentZoneName.equals(zoneName)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 76 | NDN_THROW(Error("Parent zone name can not be the zone itself")); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | if (!isRoot && !parentZoneName.isPrefixOf(zoneName)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 80 | NDN_THROW(Error(parentZoneName.toUri() + " is not a prefix of " + zoneName.toUri())); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Jiewen Tan | 01693fd | 2015-03-25 20:34:45 -0700 | [diff] [blame] | 83 | // if dsk is provided, there is no need to check ksk |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 84 | if (dskCertName != DEFAULT_CERT) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 85 | if (!matchCertificate(dskCertName, zoneIdentityName)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 86 | NDN_THROW(Error("Cannot verify DSK certificate")); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
Jiewen Tan | 01693fd | 2015-03-25 20:34:45 -0700 | [diff] [blame] | 89 | else if (kskCertName != DEFAULT_CERT) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 90 | if (!matchCertificate(kskCertName, zoneIdentityName)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 91 | NDN_THROW(Error("Cannot verify KSK certificate")); |
Jiewen Tan | 01693fd | 2015-03-25 20:34:45 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 94 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 95 | if (dkeyCertName == DEFAULT_CERT && isRoot) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 96 | NDN_THROW(Error("Cannot generate dkey for root zone")); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 99 | // Generate a parentZone's identity to generate a D-Key. |
| 100 | // This D-key will be passed to parent zone and resigned. |
| 101 | Name dkeyIdentityName; |
| 102 | if (dkeyCertName == DEFAULT_CERT) { |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 103 | dkeyIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 104 | .append(zoneName.getSubName(parentZoneName.size())); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 105 | } |
| 106 | else { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 107 | dkeyIdentityName = CertHelper::getIdentityNameFromCert(dkeyCertName); |
| 108 | } |
| 109 | NDNS_LOG_INFO("Generated D-Key's identityName: " + dkeyIdentityName.toUri()); |
| 110 | |
| 111 | Name dskName; |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame^] | 112 | security::Key ksk; |
| 113 | security::Key dsk; |
| 114 | security::Key dkey; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 115 | Certificate dskCert; |
| 116 | Certificate kskCert; |
| 117 | Certificate dkeyCert; |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame^] | 118 | auto zoneIdentity = m_keyChain.createIdentity(zoneIdentityName); |
| 119 | auto dkeyIdentity = m_keyChain.createIdentity(dkeyIdentityName); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 120 | |
| 121 | if (dkeyCertName == DEFAULT_CERT) { |
| 122 | dkey = m_keyChain.createKey(dkeyIdentity); |
| 123 | m_keyChain.deleteCertificate(dkey, dkey.getDefaultCertificate().getName()); |
| 124 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 125 | dkeyCert = CertHelper::createCertificate(m_keyChain, dkey, dkey, label::CERT_RR_TYPE.toUri(), certValidity); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 126 | dkeyCert.setFreshnessPeriod(cacheTtl); |
| 127 | m_keyChain.addCertificate(dkey, dkeyCert); |
| 128 | NDNS_LOG_INFO("Generated DKEY: " << dkeyCert.getName()); |
| 129 | |
| 130 | } |
| 131 | else { |
| 132 | dkeyCert = CertHelper::getCertificate(m_keyChain, dkeyIdentityName, dkeyCertName); |
| 133 | dkey = dkeyIdentity.getKey(dkeyCert.getKeyName()); |
| 134 | } |
| 135 | |
| 136 | if (kskCertName == DEFAULT_CERT) { |
| 137 | ksk = m_keyChain.createKey(zoneIdentity); |
| 138 | // delete automatically generated certificates, |
| 139 | // because its issue is 'self' instead of CERT_RR_TYPE |
| 140 | m_keyChain.deleteCertificate(ksk, ksk.getDefaultCertificate().getName()); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 141 | kskCert = CertHelper::createCertificate(m_keyChain, ksk, dkey, label::CERT_RR_TYPE.toUri(), certValidity); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 142 | kskCert.setFreshnessPeriod(cacheTtl); |
| 143 | m_keyChain.addCertificate(ksk, kskCert); |
| 144 | NDNS_LOG_INFO("Generated KSK: " << kskCert.getName()); |
| 145 | } |
| 146 | else { |
| 147 | // ksk usually might not be the default key of a zone |
| 148 | kskCert = CertHelper::getCertificate(m_keyChain, zoneIdentityName, kskCertName); |
| 149 | ksk = zoneIdentity.getKey(kskCert.getKeyName()); |
| 150 | } |
| 151 | |
| 152 | if (dskCertName == DEFAULT_CERT) { |
| 153 | // if no dsk provided, then generate a dsk either signed by ksk auto generated or user provided |
| 154 | dsk = m_keyChain.createKey(zoneIdentity); |
| 155 | m_keyChain.deleteCertificate(dsk, dsk.getDefaultCertificate().getName()); |
| 156 | dskCert = CertHelper::createCertificate(m_keyChain, dsk, ksk, label::CERT_RR_TYPE.toUri(), certValidity); |
| 157 | dskCert.setFreshnessPeriod(cacheTtl); |
| 158 | // dskCert will become the default certificate, since the default cert has been deleted. |
| 159 | m_keyChain.addCertificate(dsk, dskCert); |
| 160 | m_keyChain.setDefaultKey(zoneIdentity, dsk); |
| 161 | NDNS_LOG_INFO("Generated DSK: " << dskCert.getName()); |
| 162 | } |
| 163 | else { |
| 164 | dskCert = CertHelper::getCertificate(m_keyChain, zoneIdentityName, dskCertName); |
| 165 | dsk = zoneIdentity.getKey(dskCert.getKeyName()); |
| 166 | m_keyChain.setDefaultKey(zoneIdentity, dsk); |
| 167 | m_keyChain.setDefaultCertificate(dsk, dskCert); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | //second add zone to the database |
| 171 | NDNS_LOG_INFO("Start adding new zone to data base"); |
| 172 | addZone(zone); |
| 173 | |
| 174 | //third create ID-cert |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 175 | NDNS_LOG_INFO("Start adding Certificates to NDNS database"); |
| 176 | addIdCert(zone, kskCert, cacheTtl, dskCert); |
| 177 | addIdCert(zone, dskCert, cacheTtl, dskCert); |
| 178 | |
| 179 | NDNS_LOG_INFO("Start saving KSK and DSK's id to ZoneInfo"); |
| 180 | m_dbMgr.setZoneInfo(zone, "ksk", kskCert.wireEncode()); |
| 181 | m_dbMgr.setZoneInfo(zone, "dsk", dskCert.wireEncode()); |
| 182 | |
| 183 | NDNS_LOG_INFO("Start saving DKEY certificate id to ZoneInfo"); |
| 184 | m_dbMgr.setZoneInfo(zone, "dkey", dkeyCert.wireEncode()); |
| 185 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 186 | generateDoe(zone); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 187 | return zone; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void |
| 191 | ManagementTool::deleteZone(const Name& zoneName) |
| 192 | { |
| 193 | //check pre-conditions |
| 194 | Zone zone(zoneName); |
| 195 | if (!m_dbMgr.find(zone)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 196 | NDN_THROW(Error(zoneName.toUri() + " is not present in the NDNS db")); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | //first remove all rrsets of this zone from local ndns database |
| 200 | std::vector<Rrset> rrsets = m_dbMgr.findRrsets(zone); |
| 201 | for (Rrset& rrset : rrsets) { |
| 202 | m_dbMgr.remove(rrset); |
| 203 | } |
| 204 | |
| 205 | //second remove zone from local ndns database |
| 206 | removeZone(zone); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void |
| 210 | ManagementTool::exportCertificate(const Name& certName, const std::string& outFile) |
| 211 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 212 | // only search in local NDNS database |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 213 | security::Certificate cert; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 214 | shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<NDNS>(<>+)<CERT><>"); |
| 215 | if (!regex->match(certName)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 216 | NDN_THROW(Error("Certificate name is illegal")); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | Name zoneName = regex->expand("\\1"); |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 220 | Name identityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 221 | Name label = regex->expand("\\2"); |
| 222 | |
| 223 | Zone zone(zoneName); |
| 224 | Rrset rrset(&zone); |
| 225 | rrset.setLabel(label); |
| 226 | rrset.setType(label::CERT_RR_TYPE); |
| 227 | if (m_dbMgr.find(rrset)) { |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 228 | cert = security::Certificate(rrset.getData()); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 229 | } |
| 230 | else { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 231 | NDN_THROW(Error("Cannot find the cert: " + certName.toUri())); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | if (outFile == DEFAULT_IO) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 235 | ndn::io::save(cert, std::cout); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 236 | } |
| 237 | else { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 238 | ndn::io::save(cert, outFile); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 239 | NDNS_LOG_INFO("save cert to file: " << outFile); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 244 | ManagementTool::addMultiLevelLabelRrset(Rrset& rrset, |
| 245 | RrsetFactory& zoneRrFactory, |
| 246 | const time::seconds& authTtl) |
| 247 | { |
| 248 | const Name& label = rrset.getLabel(); |
| 249 | |
| 250 | // Check whether it is legal to insert the rrset |
| 251 | for (size_t i = 1; i <= label.size() - 1; i++) { |
| 252 | Name prefix = label.getPrefix(i); |
| 253 | Rrset prefixNsRr(rrset.getZone()); |
| 254 | prefixNsRr.setLabel(prefix); |
| 255 | prefixNsRr.setType(label::NS_RR_TYPE); |
| 256 | if (m_dbMgr.find(prefixNsRr)) { |
| 257 | Data data(prefixNsRr.getData()); |
| 258 | if (data.getContentType() == NDNS_LINK) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 259 | NDN_THROW(Error("Cannot override " + boost::lexical_cast<std::string>(prefixNsRr) + " (NDNS_LINK)")); |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // check that it does not override existing AUTH |
| 265 | if (rrset.getType() == label::NS_RR_TYPE) { |
| 266 | Rrset rrsetCopy = rrset; |
| 267 | if (m_dbMgr.find(rrsetCopy)) { |
| 268 | if (Data(rrsetCopy.getData()).getContentType() == NDNS_AUTH) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 269 | NDN_THROW(Error("Cannot override " + boost::lexical_cast<std::string>(rrsetCopy) + " (NDNS_AUTH)")); |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | for (size_t i = 1; i <= label.size() - 1; i++) { |
| 275 | Name prefix = label.getPrefix(i); |
| 276 | Rrset prefixNsRr(rrset.getZone()); |
| 277 | prefixNsRr.setLabel(prefix); |
| 278 | prefixNsRr.setType(label::NS_RR_TYPE); |
| 279 | if (m_dbMgr.find(prefixNsRr)) { |
| 280 | NDNS_LOG_INFO("NDNS_AUTH Rrset Label=" << prefix << " is already existed, insertion skipped"); |
| 281 | continue; |
| 282 | } |
| 283 | |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 284 | Rrset authRr = zoneRrFactory.generateAuthRrset(prefix, |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 285 | VERSION_USE_UNIX_TIMESTAMP, authTtl); |
| 286 | NDNS_LOG_INFO("Adding NDNS_AUTH " << authRr); |
| 287 | m_dbMgr.insert(authRr); |
| 288 | } |
| 289 | |
| 290 | checkRrsetVersion(rrset); |
| 291 | NDNS_LOG_INFO("Adding " << rrset); |
| 292 | m_dbMgr.insert(rrset); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 293 | generateDoe(*rrset.getZone()); |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 297 | ManagementTool::addRrset(Rrset& rrset) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 298 | { |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 299 | // check that it does not override existing AUTH |
| 300 | Rrset rrsetCopy = rrset; |
| 301 | rrsetCopy.setType(label::NS_RR_TYPE); |
| 302 | if (m_dbMgr.find(rrsetCopy)) { |
| 303 | if (Data(rrsetCopy.getData()).getContentType() == NDNS_AUTH) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 304 | NDN_THROW(Error("Can not add this Rrset: it overrides a NDNS_AUTH record")); |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 308 | checkRrsetVersion(rrset); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 309 | NDNS_LOG_INFO("Added " << rrset); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 310 | m_dbMgr.insert(rrset); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 311 | generateDoe(*rrset.getZone()); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 315 | ManagementTool::addRrsetFromFile(const Name& zoneName, |
| 316 | const std::string& inFile, |
| 317 | const time::seconds& ttl, |
| 318 | const Name& inputDskCertName, |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame^] | 319 | ndn::io::IoEncoding encoding, |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 320 | bool needResign) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 321 | { |
| 322 | //check precondition |
| 323 | Zone zone(zoneName); |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 324 | Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 325 | if (!m_dbMgr.find(zone)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 326 | NDN_THROW(Error(zoneName.toUri() + " is not present in the NDNS db")); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | Name dskName; |
| 330 | Name dskCertName = inputDskCertName; |
| 331 | if (dskCertName == DEFAULT_CERT) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 332 | dskName = CertHelper::getDefaultKeyNameOfIdentity(m_keyChain, zoneIdentityName); |
| 333 | dskCertName = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain, zoneIdentityName); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 334 | } |
| 335 | else { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 336 | if (!matchCertificate(dskCertName, zoneIdentityName)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 337 | NDN_THROW(Error("Cannot verify certificate")); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | if (inFile != DEFAULT_IO) { |
| 342 | boost::filesystem::path dir = boost::filesystem::path(inFile); |
| 343 | if (!boost::filesystem::exists(dir) || boost::filesystem::is_directory(dir)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 344 | NDN_THROW(Error("Data: " + inFile + " does not exist")); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 348 | // load data |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 349 | shared_ptr<Data> data; |
| 350 | if (inFile == DEFAULT_IO) |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 351 | data = ndn::io::load<ndn::Data>(std::cin, encoding); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 352 | else |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 353 | data = ndn::io::load<ndn::Data>(inFile, encoding); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 354 | |
Jiewen Tan | d2d2182 | 2015-03-19 15:37:03 -0700 | [diff] [blame] | 355 | if (data == nullptr) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 356 | NDN_THROW(Error("input does not contain a valid Data packet")); |
Jiewen Tan | d2d2182 | 2015-03-19 15:37:03 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 359 | if (needResign) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 360 | // TODO validityPeriod should be able to be configured |
| 361 | SignatureInfo info; |
| 362 | info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
| 363 | time::system_clock::now() + DEFAULT_CERT_TTL)); |
| 364 | m_keyChain.sign(*data, signingByCertificate(dskCertName).setSignatureInfo(info)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // create response for the input data |
| 368 | Response re; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 369 | re.fromData(zoneName, *data); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 370 | Name label = re.getRrLabel(); |
| 371 | name::Component type = re.getRrType(); |
| 372 | |
| 373 | Rrset rrset(&zone); |
| 374 | rrset.setLabel(label); |
| 375 | rrset.setType(type); |
| 376 | if (ttl == DEFAULT_RR_TTL) |
| 377 | rrset.setTtl(zone.getTtl()); |
| 378 | else |
| 379 | rrset.setTtl(ttl); |
| 380 | rrset.setVersion(re.getVersion()); |
| 381 | rrset.setData(data->wireEncode()); |
| 382 | |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 383 | checkRrsetVersion(rrset); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 384 | NDNS_LOG_INFO("Adding rrset from file " << rrset); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 385 | m_dbMgr.insert(rrset); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 386 | generateDoe(*rrset.getZone()); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 389 | security::Certificate |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 390 | ManagementTool::getZoneDkey(Zone& zone) |
| 391 | { |
| 392 | std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone); |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 393 | return security::Certificate(zoneInfo["dkey"]); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 396 | void |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame^] | 397 | ManagementTool::listZone(const Name& zoneName, std::ostream& os, bool printRaw) |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 398 | { |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 399 | Zone zone(zoneName); |
| 400 | if (!m_dbMgr.find(zone)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 401 | NDN_THROW(Error("Zone " + zoneName.toUri() + " is not found in the database")); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | //first output the zone name |
| 405 | os << "; Zone " << zoneName.toUri() << std::endl << std::endl; |
| 406 | |
| 407 | //second output all rrsets |
| 408 | std::vector<Rrset> rrsets = m_dbMgr.findRrsets(zone); |
| 409 | |
| 410 | //set width for different columns |
| 411 | size_t labelWidth = 0; |
| 412 | size_t ttlWidth = 0; |
| 413 | size_t typeWidth = 0; |
| 414 | for (Rrset& rrset : rrsets) { |
| 415 | Data data(rrset.getData()); |
| 416 | Response re; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 417 | re.fromData(zoneName, data); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 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; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 435 | re.fromData(zoneName, data); |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 436 | int iteration = re.getContentType() == NDNS_BLOB |
| 437 | || re.getContentType() == NDNS_KEY |
| 438 | || re.getContentType() == NDNS_AUTH ? 1 : re.getRrs().size(); |
| 439 | |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame^] | 440 | const auto& rrs = re.getRrs(); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 441 | |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 442 | if (re.getContentType() != NDNS_BLOB && re.getContentType() != NDNS_KEY) { |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 443 | os << "; rrset=" << rrset.getLabel().toUri() |
| 444 | << " type=" << rrset.getType().toUri() |
| 445 | << " version=" << rrset.getVersion().toUri() |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame] | 446 | << " signed-by=" << data.getKeyLocator()->getName().toUri() |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 447 | << std::endl; |
| 448 | } |
| 449 | |
| 450 | for (int i = 0; i < iteration; i++) { |
| 451 | os.setf(os.left); |
| 452 | os.width(labelWidth + 2); |
| 453 | os << rrset.getLabel().toUri(); |
| 454 | |
| 455 | os.width(ttlWidth + 2); |
| 456 | os << rrset.getTtl().count(); |
| 457 | |
| 458 | os.width(typeWidth + 2); |
| 459 | os << rrset.getType().toUri(); |
| 460 | |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 461 | if (re.getContentType() != NDNS_BLOB && re.getContentType() != NDNS_KEY) { |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 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) { |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 467 | BOOST_ASSERT(iteration == 1); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 468 | if (re.getContentType() == NDNS_AUTH) { |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 469 | const std::string authStr = "NDNS-Auth"; |
| 470 | os << authStr; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 471 | } |
| 472 | else { |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 473 | Link link(rrset.getData()); |
Junxiao Shi | 81e9876 | 2022-01-11 18:17:24 +0000 | [diff] [blame] | 474 | for (const auto& delegation : link.getDelegationList()) { |
| 475 | os << delegation << ";"; |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 478 | os << std::endl; |
| 479 | } |
| 480 | else { |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame] | 481 | bufferSource(rrs[i]) >> base64Encode() >> streamSink(os); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 486 | if (re.getContentType() == NDNS_BLOB || re.getContentType() == NDNS_KEY) { |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 487 | os.width(); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 488 | os << "; content-type=" << re.getContentType() |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 489 | << " version=" << rrset.getVersion().toUri() |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame] | 490 | << " signed-by=" << data.getKeyLocator()->getName().toUri(); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 491 | os << std::endl; |
| 492 | |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 493 | if (printRaw && (re.getContentType() == NDNS_BLOB |
| 494 | || re.getContentType() == NDNS_KEY)) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 495 | ndn::util::IndentedStream istream(os, "; "); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 496 | |
Jiewen Tan | d1dd86d | 2015-03-20 10:26:28 -0700 | [diff] [blame] | 497 | if (re.getRrType() == label::CERT_RR_TYPE) { |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 498 | security::Certificate cert(rrset.getData()); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 499 | os << cert; |
| 500 | // cert.printCertificate(istream); |
Jiewen Tan | d1dd86d | 2015-03-20 10:26:28 -0700 | [diff] [blame] | 501 | } |
| 502 | else { |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame] | 503 | bufferSource(re.getAppContent()) >> base64Encode() >> streamSink(os); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 504 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 505 | } |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 506 | os << std::endl; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 507 | } |
| 508 | else { |
| 509 | os << std::endl; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | void |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame^] | 515 | ManagementTool::listAllZones(std::ostream& os) |
| 516 | { |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 517 | std::vector<Zone> zones = m_dbMgr.listZones(); |
| 518 | |
| 519 | size_t nameWidth = 0; |
| 520 | for (const Zone& zone : zones) { |
| 521 | if (zone.getName().toUri().size() > nameWidth) |
| 522 | nameWidth = zone.getName().toUri().size(); |
| 523 | } |
| 524 | |
| 525 | for (const Zone& zone : zones) { |
| 526 | os.setf(os.left); |
| 527 | os.width(nameWidth + 2); |
| 528 | os << zone.getName().toUri(); |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 529 | Name zoneIdentity = Name(zone.getName()).append(label::NDNS_ITERATIVE_QUERY); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 530 | |
| 531 | os << "; default-ttl=" << zone.getTtl().count(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 532 | os << " default-key=" << CertHelper::getDefaultKeyNameOfIdentity(m_keyChain, zoneIdentity); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 533 | os << " default-certificate=" |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 534 | << CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain, zoneIdentity); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 535 | os << std::endl; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | void |
| 540 | ManagementTool::removeRrSet(const Name& zoneName, const Name& label, const name::Component& type) |
| 541 | { |
| 542 | Zone zone(zoneName); |
| 543 | Rrset rrset(&zone); |
| 544 | rrset.setLabel(label); |
| 545 | rrset.setType(type); |
| 546 | |
| 547 | if (!m_dbMgr.find(rrset)) { |
| 548 | return; |
| 549 | } |
| 550 | NDNS_LOG_INFO("Remove rrset with zone-id: " << zone.getId() << " label: " << label << " type: " |
| 551 | << type); |
| 552 | m_dbMgr.remove(rrset); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 553 | generateDoe(zone); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | void |
| 557 | ManagementTool::getRrSet(const Name& zoneName, |
| 558 | const Name& label, |
| 559 | const name::Component& type, |
| 560 | std::ostream& os) |
| 561 | { |
| 562 | Zone zone(zoneName); |
| 563 | Rrset rrset(&zone); |
| 564 | rrset.setLabel(label); |
| 565 | rrset.setType(type); |
| 566 | |
| 567 | if (!m_dbMgr.find(rrset)) { |
| 568 | os << "No record is found" << std::endl; |
| 569 | return; |
| 570 | } |
| 571 | |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame] | 572 | bufferSource(rrset.getData()) >> base64Encode() >> streamSink(os); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 576 | ManagementTool::addIdCert(Zone& zone, const Certificate& cert, |
| 577 | const time::seconds& ttl, |
| 578 | const Certificate& dskCert) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 579 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 580 | Rrset rrsetKey(&zone); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 581 | size_t size = zone.getName().size(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 582 | Name label = cert.getName().getSubName(size + 1, cert.getName().size() - size - 3); |
| 583 | rrsetKey.setLabel(label); |
| 584 | rrsetKey.setType(label::CERT_RR_TYPE); |
| 585 | rrsetKey.setTtl(ttl); |
| 586 | rrsetKey.setVersion(cert.getName().get(-1)); |
| 587 | rrsetKey.setData(cert.wireEncode()); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 588 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 589 | if (m_dbMgr.find(rrsetKey)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 590 | NDN_THROW(Error("CERT with label=" + label.toUri() + |
| 591 | " is already present in local NDNS database")); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 592 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 593 | |
| 594 | m_dbMgr.insert(rrsetKey); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 595 | NDNS_LOG_INFO("Add rrset with zone-id: " << zone.getId() << " label: " << label << " type: " |
| 596 | << label::CERT_RR_TYPE); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void |
| 600 | ManagementTool::addZone(Zone& zone) |
| 601 | { |
| 602 | if (m_dbMgr.find(zone)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 603 | NDN_THROW(Error("Zone with Name=" + zone.getName().toUri() + |
| 604 | " is already present in local NDNS database")); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 605 | } |
| 606 | NDNS_LOG_INFO("Add zone with Name: " << zone.getName().toUri()); |
| 607 | m_dbMgr.insert(zone); |
| 608 | } |
| 609 | |
| 610 | void |
| 611 | ManagementTool::removeZone(Zone& zone) |
| 612 | { |
| 613 | if (!m_dbMgr.find(zone)) { |
| 614 | return; |
| 615 | } |
| 616 | NDNS_LOG_INFO("Remove zone with Name: " << zone.getName().toUri()); |
| 617 | m_dbMgr.remove(zone); |
| 618 | } |
| 619 | |
| 620 | bool |
| 621 | ManagementTool::matchCertificate(const Name& certName, const Name& identity) |
| 622 | { |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame^] | 623 | auto id = m_keyChain.getPib().getIdentity(identity); |
| 624 | for (const auto& key : id.getKeys()) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 625 | try { |
| 626 | key.getCertificate(certName); |
| 627 | return true; |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame] | 628 | } |
| 629 | catch (const std::exception&) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 630 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 631 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 632 | return false; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 635 | void |
| 636 | ManagementTool::checkRrsetVersion(const Rrset& rrset) |
| 637 | { |
| 638 | Rrset originalRrset(rrset); |
| 639 | if (m_dbMgr.find(originalRrset)) { |
| 640 | // update only if rrset has a newer version |
| 641 | if (originalRrset.getVersion() == rrset.getVersion()) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 642 | NDN_THROW(Error("Duplicate: " + boost::lexical_cast<std::string>(originalRrset))); |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 643 | } |
| 644 | else if (originalRrset.getVersion() > rrset.getVersion()) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 645 | NDN_THROW(Error("Newer version exists: " + boost::lexical_cast<std::string>(originalRrset))); |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | m_dbMgr.remove(originalRrset); |
| 649 | } |
| 650 | } |
| 651 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 652 | void |
| 653 | ManagementTool::generateDoe(Zone& zone) |
| 654 | { |
| 655 | // check zone existence |
| 656 | if (!m_dbMgr.find(zone)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 657 | NDN_THROW(Error(zone.getName().toUri() + " is not present in the NDNS db")); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | // remove all the Doe records |
| 661 | m_dbMgr.removeRrsetsOfZoneByType(zone, label::DOE_RR_TYPE); |
| 662 | |
| 663 | // get the records out |
| 664 | std::vector<Rrset> allRecords = m_dbMgr.findRrsets(zone); |
| 665 | |
| 666 | // sort them by DoE label name (same as in the database) |
| 667 | std::sort(allRecords.begin(), allRecords.end()); |
| 668 | |
| 669 | RrsetFactory factory(m_dbMgr.getDbFile(), zone.getName(), m_keyChain, DEFAULT_CERT); |
| 670 | factory.checkZoneKey(); |
| 671 | |
| 672 | for (size_t i = 0; i < allRecords.size() - 1; i++) { |
| 673 | Name lowerLabel = Name(allRecords[i].getLabel()).append(allRecords[i].getType()); |
| 674 | Name upperLabel = Name(allRecords[i + 1].getLabel()).append(allRecords[i + 1].getType()); |
| 675 | Rrset doe = factory.generateDoeRrset(lowerLabel, |
| 676 | VERSION_USE_UNIX_TIMESTAMP, |
| 677 | DEFAULT_CACHE_TTL, lowerLabel, upperLabel); |
| 678 | m_dbMgr.insert(doe); |
| 679 | } |
| 680 | |
| 681 | Name lastLabel = Name(allRecords.back().getLabel()).append(allRecords.back().getType()); |
| 682 | Name firstLabel = Name(allRecords.front().getLabel()).append(allRecords.front().getType()); |
| 683 | Rrset lastRange = factory.generateDoeRrset(lastLabel, |
| 684 | VERSION_USE_UNIX_TIMESTAMP, |
| 685 | DEFAULT_CACHE_TTL, lastLabel, firstLabel); |
| 686 | m_dbMgr.insert(lastRange); |
| 687 | |
| 688 | // This guard will be the lowest label-ranked record |
| 689 | // so if requested label+type is less than the lowest label except for this one, it will be choosed |
| 690 | // by findLowerBound. This small trick avoids complicated SQL query in findLowerBound |
| 691 | Rrset guardRange = factory.generateDoeRrset(Name(""), |
| 692 | VERSION_USE_UNIX_TIMESTAMP, |
| 693 | DEFAULT_CACHE_TTL, lastLabel, firstLabel); |
| 694 | m_dbMgr.insert(guardRange); |
| 695 | NDNS_LOG_INFO("DoE record updated"); |
| 696 | } |
| 697 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 698 | } // namespace ndns |
| 699 | } // namespace ndn |