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 | #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" |
| 28 | #include "./clients/response.hpp" |
| 29 | |
| 30 | #include <stdexcept> |
| 31 | #include <ndn-cxx/common.hpp> |
| 32 | #include <ndn-cxx/security/identity-certificate.hpp> |
| 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 | |
| 66 | /** @param certDir Path to the directory to store certificates |
| 67 | * @param dbFile Path to the local database |
| 68 | */ |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 69 | ManagementTool(const std::string& dbFile, KeyChain& keyChain); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 70 | |
| 71 | /** @brief Create a Zone according to a given name. |
| 72 | * |
| 73 | * Specifically, It will generate a KSK and a DSK (and their certificates) to the following |
| 74 | * places: |
| 75 | * 1) Local NDNS database: a new zone is added. |
| 76 | * 2) Local NDNS database: an ID-CERT of the DSK is added. |
| 77 | * 3) KeyChain: an identity named with zone name is added. |
| 78 | * 4) KeyChain: a KSK and its self-signed certificate is added. The ownership of the KSK is the |
| 79 | * parent zone. |
| 80 | * 5) KeyChain: a DSK and its KSK signed certificate is added. |
| 81 | * |
| 82 | * -SS.cert (self-signed) |
| 83 | * -SKS.cert (self's Key signed) |
| 84 | * -PKS.cert (parent's Key Signed) |
| 85 | * |
| 86 | * @attention |
| 87 | * 1) to create root zone, supply zoneName and parentZoneName both with ROOT_ZONE |
| 88 | * |
| 89 | * @param zoneName zone's name |
| 90 | * @param parentZoneName parent zone's name |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 91 | * @param cacheTtl default TTL for RR sets in the zone |
| 92 | * @param certValidity validity for automatically created DSK certificate (@p dskCertName |
| 93 | * should not be empty) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 94 | * @param kskCertName if given, a zone will be created with this ksk certificate and its key |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 95 | * @param dskCertName if given, a zone will be created with this dsk certificate and its key |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 96 | */ |
| 97 | void |
| 98 | createZone(const Name& zoneName, |
| 99 | const Name& parentZoneName, |
| 100 | const time::seconds& cacheTtl = DEFAULT_CACHE_TTL, |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 101 | const time::seconds& certValidity = DEFAULT_CERT_TTL, |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 102 | const Name& kskCertName = DEFAULT_CERT, |
| 103 | const Name& dskCertName = DEFAULT_CERT); |
| 104 | |
| 105 | /** @brief Delete a Zone according to a given name. |
| 106 | * |
| 107 | * Specifically, It will do the following things: |
| 108 | * 1) KeyChain System: delete the Identity with zone name and all its keys/certificates |
| 109 | * 2) Local NDNS database: delete the zone record |
| 110 | * 3) Local NDNS database: delete the ID-CERT of the zone's DSK |
| 111 | */ |
| 112 | void |
| 113 | deleteZone(const Name& zoneName); |
| 114 | |
| 115 | /** @brief Export the certificate to file system |
| 116 | * |
| 117 | * @param certName the name of the certificate to be exported |
| 118 | * @param output the path to output to-be exported file, including the file name |
| 119 | */ |
| 120 | void |
| 121 | exportCertificate(const Name& certName, const std::string& outFile = DEFAULT_IO); |
| 122 | |
| 123 | /** @brief add rrset to the NDNS local database |
| 124 | * |
| 125 | * This one is only capable of adding NS type including NDNS_RESP and NDNS_AUTH and user defined |
| 126 | * type with string content. |
| 127 | * Other complicated situations can be handled by the other addRrSet() overload function. |
| 128 | * |
| 129 | * @param zoneName the name of the zone to hold the rrset |
| 130 | * @param label the rrset label |
| 131 | * @param type the rrset type |
| 132 | * @param ndnsType the ndnsType of the response, for user-defined type, just set it NDNS_RAW |
| 133 | * @param version the version of the response and rrset, default is Unix Timestamp |
| 134 | * @param content the content of the response |
| 135 | * @param dskName the DSK to signed the response, default is the zone's DSK |
| 136 | * @param ttl the ttl of the rrset |
| 137 | */ |
| 138 | void |
| 139 | addRrSet(const Name& zoneName, |
| 140 | const Name& label, |
| 141 | const name::Component& type, |
| 142 | NdnsType ndnsType, |
| 143 | const uint64_t version = VERSION_USE_UNIX_TIMESTAMP, |
| 144 | const std::vector<std::string>& contents = DEFAULT_CONTENTS, |
| 145 | const Name& dskCertName = DEFAULT_CERT, |
| 146 | const time::seconds& ttl = DEFAULT_RR_TTL); |
| 147 | |
| 148 | /** @brief add rrset to the NDNS local database |
| 149 | * |
| 150 | * This overload is capable of adding any data to the rrset as long as the supplied data is |
| 151 | * valid. |
| 152 | * A special case is to add the ID-CERT of KSK to the parent zone. At this case, the SS cert |
| 153 | * should be supplied, and therefore it will use the parent zone's DSK to resign the certificate. |
| 154 | * For other cases, the data will be added directly without any modification. |
| 155 | * |
| 156 | * @param zoneName the name of the zone to hold the rrset |
| 157 | * @param dataPath the path to the supplied data |
| 158 | * @param ttl the ttl of the rrset |
| 159 | * @param dskName 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] | 160 | * @param encoding the encoding of the input file |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 161 | */ |
| 162 | void |
| 163 | addRrSet(const Name& zoneName, |
| 164 | const std::string& inFile = DEFAULT_IO, |
| 165 | const time::seconds& ttl = DEFAULT_RR_TTL, |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 166 | const Name& dskCertName = DEFAULT_CERT, |
| 167 | const ndn::io::IoEncoding encoding = ndn::io::BASE_64); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 168 | |
| 169 | /** @brief remove rrset from the NDNS local database |
| 170 | * |
| 171 | * @param zonName the name of the zone holding the rrset |
| 172 | * @param label rrset's label |
| 173 | * @param type rrset's type |
| 174 | */ |
| 175 | void |
| 176 | removeRrSet(const Name& zoneName, const Name& label, const name::Component& type); |
| 177 | |
| 178 | /** @brief output the raw data of the selected rrset |
| 179 | * |
| 180 | * @param zonName the name of the zone holding the rrset |
| 181 | * @param label rrset's label |
| 182 | * @param type rrset's type |
| 183 | * @param os the ostream to print information to |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 184 | */ |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 185 | void |
| 186 | getRrSet(const Name& zoneName, |
| 187 | const Name& label, |
| 188 | const name::Component& type, |
| 189 | std::ostream& os); |
| 190 | |
| 191 | /** @brief generates an output like DNS zone file. Reference: |
| 192 | * http://en.wikipedia.org/wiki/Zone_file |
| 193 | * |
| 194 | * @param zoneName the name of the zone to investigate |
| 195 | * @param os the ostream to print information to |
| 196 | * @param printRaw set to print content of ndns-raw rrset |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 197 | * @throw Error if zoneName does not exist in the database |
| 198 | */ |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 199 | void |
| 200 | listZone(const Name& zoneName, std::ostream& os, const bool printRaw = false); |
| 201 | |
| 202 | /** @brief lists all existing zones within this name server. |
| 203 | * |
| 204 | * @param os the ostream to print information to |
| 205 | */ |
| 206 | void |
| 207 | listAllZones(std::ostream& os); |
| 208 | |
| 209 | private: |
| 210 | /** @brief add ID-CERT to the NDNS local database |
| 211 | */ |
| 212 | void |
| 213 | addIdCert(Zone& zone, shared_ptr<IdentityCertificate> cert, const time::seconds& ttl); |
| 214 | |
| 215 | /** @brief add zone to the NDNS local database |
| 216 | */ |
| 217 | void |
| 218 | addZone(Zone& zone); |
| 219 | |
| 220 | /** @brief remove zone from the NDNS local database |
| 221 | */ |
| 222 | void |
| 223 | removeZone(Zone& zone); |
| 224 | |
| 225 | /** @brief determine whether a certificate matches with both the identity and key type |
| 226 | */ |
| 227 | bool |
| 228 | matchCertificate(const Name& certName, const Name& identity); |
| 229 | |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 230 | /** @brief determine whether an older version of the rrset exists |
| 231 | */ |
| 232 | void |
| 233 | checkRrsetVersion(const Rrset& rrset); |
| 234 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 235 | private: |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 236 | KeyChain& m_keyChain; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 237 | DbMgr m_dbMgr; |
| 238 | }; |
| 239 | |
| 240 | } // namespace ndns |
| 241 | } // namespace ndn |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 242 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 243 | #endif // NDNS_MGMT_MANAGEMENT_TOOL_HPP |