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 | /* |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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 | #ifndef NDNS_MGMT_MANAGEMENT_TOOL_HPP |
| 21 | #define NDNS_MGMT_MANAGEMENT_TOOL_HPP |
| 22 | |
| 23 | #include "config.hpp" |
| 24 | #include "ndns-enum.hpp" |
| 25 | #include "./daemon/zone.hpp" |
| 26 | #include "./daemon/db-mgr.hpp" |
| 27 | #include "./daemon/rrset.hpp" |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 28 | #include "./daemon/rrset-factory.hpp" |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 29 | #include "./clients/response.hpp" |
| 30 | |
| 31 | #include <stdexcept> |
| 32 | #include <ndn-cxx/common.hpp> |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 33 | #include <ndn-cxx/security/key-chain.hpp> |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 34 | #include <ndn-cxx/util/io.hpp> |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 35 | |
| 36 | namespace ndn { |
| 37 | namespace ndns { |
| 38 | |
| 39 | static const Name DEFAULT_CERT; |
| 40 | static const Name ROOT_ZONE; |
| 41 | static const time::seconds DEFAULT_CACHE_TTL = time::seconds(3600); |
| 42 | static const time::seconds DEFAULT_CERT_TTL = time::days(365); |
| 43 | static const std::vector<std::string> DEFAULT_CONTENTS; |
| 44 | static const std::string DEFAULT_IO = "-"; |
| 45 | static const time::seconds DEFAULT_RR_TTL = time::seconds(0); |
| 46 | static constexpr uint64_t VERSION_USE_UNIX_TIMESTAMP = std::numeric_limits<uint64_t>::max(); |
| 47 | |
| 48 | /** |
| 49 | * @brief provides management tools to the NDNS system, such as zone creation, zone delegation, DSK |
| 50 | * generation and root zone creation. |
| 51 | */ |
| 52 | class ManagementTool : noncopyable |
| 53 | { |
| 54 | public: |
| 55 | /** @brief Represents an error might be thrown during runtime |
| 56 | */ |
| 57 | class Error : public std::runtime_error |
| 58 | { |
| 59 | public: |
| 60 | explicit |
| 61 | Error(const std::string& what) : std::runtime_error(what) |
| 62 | { |
| 63 | } |
| 64 | }; |
| 65 | |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 66 | /** |
| 67 | * @brief Create instance of the tool |
| 68 | * |
| 69 | * @param dbFile Path to the local database |
| 70 | * @param keyChain Keychain instance |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 71 | */ |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 72 | ManagementTool(const std::string& dbFile, KeyChain& keyChain); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 73 | |
| 74 | /** @brief Create a Zone according to a given name. |
| 75 | * |
| 76 | * Specifically, It will generate a KSK and a DSK (and their certificates) to the following |
| 77 | * places: |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 78 | * 1. Local NDNS database: a new zone is added. |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 79 | * 2. Local NDNS database: an CERT of the DSK is added. |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 80 | * 3. KeyChain: an identity named with zone name is added. |
| 81 | * 4. KeyChain: a KSK and its self-signed certificate is added. The ownership of the KSK is the |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 82 | * parent zone. |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 83 | * 5. KeyChain: a DSK and its KSK signed certificate is added. |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 84 | * |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 85 | * - SS.cert (self-signed) |
| 86 | * - SKS.cert (self's Key signed) |
| 87 | * - PKS.cert (parent's Key Signed) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 88 | * |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 89 | * @note To create root zone, supply zoneName and parentZoneName both with ROOT_ZONE |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 90 | * |
| 91 | * @param zoneName zone's name |
| 92 | * @param parentZoneName parent zone's name |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 93 | * @param cacheTtl default TTL for RR sets in the zone |
| 94 | * @param certValidity validity for automatically created DSK certificate (@p dskCertName |
| 95 | * should not be empty) |
Jiewen Tan | 01693fd | 2015-03-25 20:34:45 -0700 | [diff] [blame] | 96 | * @param kskCertName if given, a zone will be created with this ksk certificate |
| 97 | * @param dskCertName if given, a zone will be created with this dsk certificate and provided |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 98 | * ksk certificate will be ignored |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 99 | * @param dkeyCertName if given, ksk will be signed by this d-key. |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 100 | */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 101 | Zone |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 102 | createZone(const Name& zoneName, |
| 103 | const Name& parentZoneName, |
| 104 | const time::seconds& cacheTtl = DEFAULT_CACHE_TTL, |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 105 | const time::seconds& certValidity = DEFAULT_CERT_TTL, |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 106 | const Name& kskCertName = DEFAULT_CERT, |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 107 | const Name& dskCertName = DEFAULT_CERT, |
| 108 | const Name& dkeyCertName = DEFAULT_CERT); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 109 | |
| 110 | /** @brief Delete a Zone according to a given name. |
| 111 | * |
| 112 | * Specifically, It will do the following things: |
| 113 | * 1) KeyChain System: delete the Identity with zone name and all its keys/certificates |
| 114 | * 2) Local NDNS database: delete the zone record |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 115 | * 3) Local NDNS database: delete the CERT of the zone's DSK |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 116 | */ |
| 117 | void |
| 118 | deleteZone(const Name& zoneName); |
| 119 | |
| 120 | /** @brief Export the certificate to file system |
| 121 | * |
| 122 | * @param certName the name of the certificate to be exported |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 123 | * @param outFile the path to output to-be exported file, including the file name |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 124 | */ |
| 125 | void |
| 126 | exportCertificate(const Name& certName, const std::string& outFile = DEFAULT_IO); |
| 127 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 128 | /** @brief Add rrset to the NDNS local database from a file |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 129 | * |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 130 | * The function Loads data from file and then adds it to the rrset without modification |
| 131 | * Loaded data is assummed to be valid |
| 132 | * Data will be resigned by zone's DSK, if needResign is true. |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 133 | * |
| 134 | * @param zoneName the name of the zone to hold the rrset |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 135 | * @param inFile the path to the supplied data |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 136 | * @param ttl the ttl of the rrset |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 137 | * @param dskCertName the DSK to signed the special case, default is the zone's DSK |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 138 | * @param encoding the encoding of the input file |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 139 | * @param needResign whether data should be resigned by DSK |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 140 | */ |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 141 | void |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 142 | addRrsetFromFile(const Name& zoneName, |
| 143 | const std::string& inFile = DEFAULT_IO, |
| 144 | const time::seconds& ttl = DEFAULT_RR_TTL, |
| 145 | const Name& dskCertName = DEFAULT_CERT, |
| 146 | const ndn::io::IoEncoding encoding = ndn::io::BASE64, |
| 147 | bool needResign = false); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 148 | |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 149 | /** @brief Add rrset to the NDNS local database |
| 150 | * |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 151 | * @param rrset rrset |
| 152 | */ |
| 153 | void |
| 154 | addRrset(Rrset& rrset); |
| 155 | |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 156 | /** @brief Add rrset with multi-level label to the NDNS local database |
| 157 | * |
| 158 | * The appropriate AUTH records will be created automatically if they do not yet exist. The |
| 159 | * existing records are kept intact. |
| 160 | * |
| 161 | * @throw Error If one of the levels has been delegated to another zone. For example, if |
| 162 | * there is an NS record with label `/foo`, then inserting @p rrset having a |
| 163 | * multi-level label that use `/foo` as prefix will cause an error. |
| 164 | * |
| 165 | * @throw Error If @p rrset will override an AUTH record. For example, if there is already |
| 166 | * an AUTH record with label `/foo/bar`, then inserting NS-type @p rrset that |
| 167 | * has the the same label will cause an error. |
| 168 | * |
| 169 | * For example, inserting a rrset with `/foo/bar/test` label and TXT type into zone `/zone/NDNS` |
| 170 | * will create: |
| 171 | * - `/zone/NDNS/foo/NS` (.ContentType AUTH) |
| 172 | * - `/zone/NDNS/foo/bar/NS` (.ContentType AUTH) |
| 173 | * - `/zone/NDNS/foo/bar/test/TXT` (.ContentType NDNS-Resp) |
| 174 | * |
| 175 | * @param rrset rrset |
| 176 | * @param zoneRrFactory that is used for generate AUTH packet |
| 177 | * @param authTtl |
| 178 | */ |
| 179 | void |
| 180 | addMultiLevelLabelRrset(Rrset& rrset, |
| 181 | RrsetFactory& zoneRrFactory, |
| 182 | const time::seconds& authTtl); |
| 183 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 184 | /** @brief remove rrset from the NDNS local database |
| 185 | * |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 186 | * @param zoneName the name of the zone holding the rrset |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 187 | * @param label rrset's label |
| 188 | * @param type rrset's type |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 189 | */ |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 190 | void |
| 191 | removeRrSet(const Name& zoneName, const Name& label, const name::Component& type); |
| 192 | |
| 193 | /** @brief output the raw data of the selected rrset |
| 194 | * |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 195 | * @param zoneName the name of the zone holding the rrset |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 196 | * @param label rrset's label |
| 197 | * @param type rrset's type |
| 198 | * @param os the ostream to print information to |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 199 | */ |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 200 | void |
| 201 | getRrSet(const Name& zoneName, |
| 202 | const Name& label, |
| 203 | const name::Component& type, |
| 204 | std::ostream& os); |
| 205 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 206 | security::v2::Certificate |
| 207 | getZoneDkey(Zone& zone); |
| 208 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 209 | /** @brief generates an output like DNS zone file. Reference: |
| 210 | * http://en.wikipedia.org/wiki/Zone_file |
| 211 | * |
| 212 | * @param zoneName the name of the zone to investigate |
| 213 | * @param os the ostream to print information to |
| 214 | * @param printRaw set to print content of ndns-raw rrset |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 215 | * @throw Error if zoneName does not exist in the database |
| 216 | */ |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 217 | void |
| 218 | listZone(const Name& zoneName, std::ostream& os, const bool printRaw = false); |
| 219 | |
| 220 | /** @brief lists all existing zones within this name server. |
| 221 | * |
| 222 | * @param os the ostream to print information to |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 223 | */ |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 224 | void |
| 225 | listAllZones(std::ostream& os); |
| 226 | |
| 227 | private: |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 228 | /** @brief add CERT to the NDNS local database |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 229 | */ |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 230 | void |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 231 | addIdCert(Zone& zone, const ndn::security::v2::Certificate& cert, |
| 232 | const time::seconds& ttl, |
| 233 | const ndn::security::v2::Certificate& dskCertName); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 234 | |
| 235 | /** @brief add zone to the NDNS local database |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 236 | */ |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 237 | void |
| 238 | addZone(Zone& zone); |
| 239 | |
| 240 | /** @brief remove zone from the NDNS local database |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 241 | */ |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 242 | void |
| 243 | removeZone(Zone& zone); |
| 244 | |
| 245 | /** @brief determine whether a certificate matches with both the identity and key type |
| 246 | */ |
| 247 | bool |
| 248 | matchCertificate(const Name& certName, const Name& identity); |
| 249 | |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 250 | /** @brief determine whether an older version of the rrset exists |
| 251 | */ |
| 252 | void |
| 253 | checkRrsetVersion(const Rrset& rrset); |
| 254 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 255 | /** |
| 256 | @brief generate all Doe records |
| 257 | */ |
| 258 | void generateDoe(Zone& zone); |
| 259 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 260 | private: |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 261 | KeyChain& m_keyChain; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 262 | DbMgr m_dbMgr; |
| 263 | }; |
| 264 | |
| 265 | } // namespace ndns |
| 266 | } // namespace ndn |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 267 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 268 | #endif // NDNS_MGMT_MANAGEMENT_TOOL_HPP |