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