blob: 592205caa5b116bb9fd4a2ab7f590e408bd886a0 [file] [log] [blame]
Jiewen Tan870b29b2014-11-17 19:09:49 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Davide Pesavento38fd3982022-04-18 22:22:02 -04003 * Copyright (c) 2014-2022, Regents of the University of California.
Jiewen Tan870b29b2014-11-17 19:09:49 -08004 *
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
Jiewen Tan870b29b2014-11-17 19:09:49 -080023#include "ndns-enum.hpp"
Junxiao Shi15e51bc2018-12-12 13:48:56 -070024#include "clients/response.hpp"
25#include "daemon/db-mgr.hpp"
26#include "daemon/rrset.hpp"
27#include "daemon/rrset-factory.hpp"
28#include "daemon/zone.hpp"
Jiewen Tan870b29b2014-11-17 19:09:49 -080029
Yumin Xia2c509c22017-02-09 14:37:36 -080030#include <ndn-cxx/security/key-chain.hpp>
Jiewen Tan74d745c2015-03-20 01:40:41 -070031#include <ndn-cxx/util/io.hpp>
Jiewen Tan870b29b2014-11-17 19:09:49 -080032
Junxiao Shi15e51bc2018-12-12 13:48:56 -070033#include <stdexcept>
34
Jiewen Tan870b29b2014-11-17 19:09:49 -080035namespace ndn {
36namespace ndns {
37
Davide Pesavento38fd3982022-04-18 22:22:02 -040038inline const Name DEFAULT_CERT;
39inline const Name ROOT_ZONE;
40inline constexpr time::seconds DEFAULT_CACHE_TTL = time::seconds(3600);
41inline constexpr time::seconds DEFAULT_CERT_TTL = time::days(365);
42inline const std::vector<std::string> DEFAULT_CONTENTS;
43inline const std::string DEFAULT_IO = "-";
44inline constexpr time::seconds DEFAULT_RR_TTL = time::seconds(0);
45inline constexpr uint64_t VERSION_USE_UNIX_TIMESTAMP = std::numeric_limits<uint64_t>::max();
Jiewen Tan870b29b2014-11-17 19:09:49 -080046
47/**
48 * @brief provides management tools to the NDNS system, such as zone creation, zone delegation, DSK
49 * generation and root zone creation.
50 */
Davide Pesavento948c50c2020-12-26 21:30:45 -050051class ManagementTool : boost::noncopyable
Jiewen Tan870b29b2014-11-17 19:09:49 -080052{
53public:
54 /** @brief Represents an error might be thrown during runtime
55 */
56 class Error : public std::runtime_error
57 {
58 public:
Davide Pesavento948c50c2020-12-26 21:30:45 -050059 using std::runtime_error::runtime_error;
Jiewen Tan870b29b2014-11-17 19:09:49 -080060 };
61
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080062 /**
63 * @brief Create instance of the tool
64 *
65 * @param dbFile Path to the local database
66 * @param keyChain Keychain instance
Jiewen Tan870b29b2014-11-17 19:09:49 -080067 */
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080068 ManagementTool(const std::string& dbFile, KeyChain& keyChain);
Jiewen Tan870b29b2014-11-17 19:09:49 -080069
70 /** @brief Create a Zone according to a given name.
71 *
72 * Specifically, It will generate a KSK and a DSK (and their certificates) to the following
73 * places:
Yumin Xiaacd21332016-11-28 22:54:48 -080074 * 1. Local NDNS database: a new zone is added.
Yumin Xia2c509c22017-02-09 14:37:36 -080075 * 2. Local NDNS database: an CERT of the DSK is added.
Yumin Xiaacd21332016-11-28 22:54:48 -080076 * 3. KeyChain: an identity named with zone name is added.
77 * 4. KeyChain: a KSK and its self-signed certificate is added. The ownership of the KSK is the
Jiewen Tan870b29b2014-11-17 19:09:49 -080078 * parent zone.
Yumin Xiaacd21332016-11-28 22:54:48 -080079 * 5. KeyChain: a DSK and its KSK signed certificate is added.
Jiewen Tan870b29b2014-11-17 19:09:49 -080080 *
Yumin Xiaacd21332016-11-28 22:54:48 -080081 * - SS.cert (self-signed)
82 * - SKS.cert (self's Key signed)
83 * - PKS.cert (parent's Key Signed)
Jiewen Tan870b29b2014-11-17 19:09:49 -080084 *
Yumin Xiaacd21332016-11-28 22:54:48 -080085 * @note To create root zone, supply zoneName and parentZoneName both with ROOT_ZONE
Jiewen Tan870b29b2014-11-17 19:09:49 -080086 *
87 * @param zoneName zone's name
88 * @param parentZoneName parent zone's name
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080089 * @param cacheTtl default TTL for RR sets in the zone
90 * @param certValidity validity for automatically created DSK certificate (@p dskCertName
91 * should not be empty)
Jiewen Tan01693fd2015-03-25 20:34:45 -070092 * @param kskCertName if given, a zone will be created with this ksk certificate
93 * @param dskCertName if given, a zone will be created with this dsk certificate and provided
Yumin Xiaacd21332016-11-28 22:54:48 -080094 * ksk certificate will be ignored
Yumin Xia2c509c22017-02-09 14:37:36 -080095 * @param dkeyCertName if given, ksk will be signed by this d-key.
Jiewen Tan870b29b2014-11-17 19:09:49 -080096 */
Yumin Xia2c509c22017-02-09 14:37:36 -080097 Zone
Jiewen Tan870b29b2014-11-17 19:09:49 -080098 createZone(const Name& zoneName,
99 const Name& parentZoneName,
100 const time::seconds& cacheTtl = DEFAULT_CACHE_TTL,
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800101 const time::seconds& certValidity = DEFAULT_CERT_TTL,
Jiewen Tan870b29b2014-11-17 19:09:49 -0800102 const Name& kskCertName = DEFAULT_CERT,
Yumin Xia2c509c22017-02-09 14:37:36 -0800103 const Name& dskCertName = DEFAULT_CERT,
104 const Name& dkeyCertName = DEFAULT_CERT);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800105
106 /** @brief Delete a Zone according to a given name.
107 *
108 * Specifically, It will do the following things:
109 * 1) KeyChain System: delete the Identity with zone name and all its keys/certificates
110 * 2) Local NDNS database: delete the zone record
Yumin Xia2c509c22017-02-09 14:37:36 -0800111 * 3) Local NDNS database: delete the CERT of the zone's DSK
Jiewen Tan870b29b2014-11-17 19:09:49 -0800112 */
113 void
114 deleteZone(const Name& zoneName);
115
116 /** @brief Export the certificate to file system
117 *
118 * @param certName the name of the certificate to be exported
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800119 * @param outFile the path to output to-be exported file, including the file name
Jiewen Tan870b29b2014-11-17 19:09:49 -0800120 */
121 void
122 exportCertificate(const Name& certName, const std::string& outFile = DEFAULT_IO);
123
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800124 /** @brief Add rrset to the NDNS local database from a file
Jiewen Tan870b29b2014-11-17 19:09:49 -0800125 *
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800126 * The function Loads data from file and then adds it to the rrset without modification
127 * Loaded data is assummed to be valid
128 * Data will be resigned by zone's DSK, if needResign is true.
Jiewen Tan870b29b2014-11-17 19:09:49 -0800129 *
130 * @param zoneName the name of the zone to hold the rrset
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800131 * @param inFile the path to the supplied data
Jiewen Tan870b29b2014-11-17 19:09:49 -0800132 * @param ttl the ttl of the rrset
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800133 * @param dskCertName the DSK to signed the special case, default is the zone's DSK
Jiewen Tan74d745c2015-03-20 01:40:41 -0700134 * @param encoding the encoding of the input file
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800135 * @param needResign whether data should be resigned by DSK
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800136 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800137 void
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800138 addRrsetFromFile(const Name& zoneName,
139 const std::string& inFile = DEFAULT_IO,
140 const time::seconds& ttl = DEFAULT_RR_TTL,
141 const Name& dskCertName = DEFAULT_CERT,
Davide Pesavento38fd3982022-04-18 22:22:02 -0400142 ndn::io::IoEncoding encoding = ndn::io::BASE64,
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800143 bool needResign = false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800144
Yumin Xiaacd21332016-11-28 22:54:48 -0800145 /** @brief Add rrset to the NDNS local database
146 *
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700147 * @param rrset rrset
148 */
149 void
150 addRrset(Rrset& rrset);
151
Yumin Xiaacd21332016-11-28 22:54:48 -0800152 /** @brief Add rrset with multi-level label to the NDNS local database
153 *
154 * The appropriate AUTH records will be created automatically if they do not yet exist. The
155 * existing records are kept intact.
156 *
157 * @throw Error If one of the levels has been delegated to another zone. For example, if
158 * there is an NS record with label `/foo`, then inserting @p rrset having a
159 * multi-level label that use `/foo` as prefix will cause an error.
160 *
161 * @throw Error If @p rrset will override an AUTH record. For example, if there is already
162 * an AUTH record with label `/foo/bar`, then inserting NS-type @p rrset that
163 * has the the same label will cause an error.
164 *
165 * For example, inserting a rrset with `/foo/bar/test` label and TXT type into zone `/zone/NDNS`
166 * will create:
167 * - `/zone/NDNS/foo/NS` (.ContentType AUTH)
168 * - `/zone/NDNS/foo/bar/NS` (.ContentType AUTH)
169 * - `/zone/NDNS/foo/bar/test/TXT` (.ContentType NDNS-Resp)
170 *
171 * @param rrset rrset
172 * @param zoneRrFactory that is used for generate AUTH packet
173 * @param authTtl
174 */
175 void
176 addMultiLevelLabelRrset(Rrset& rrset,
177 RrsetFactory& zoneRrFactory,
178 const time::seconds& authTtl);
179
Jiewen Tan870b29b2014-11-17 19:09:49 -0800180 /** @brief remove rrset from the NDNS local database
181 *
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800182 * @param zoneName the name of the zone holding the rrset
Jiewen Tan870b29b2014-11-17 19:09:49 -0800183 * @param label rrset's label
184 * @param type rrset's type
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800185 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800186 void
187 removeRrSet(const Name& zoneName, const Name& label, const name::Component& type);
188
189 /** @brief output the raw data of the selected rrset
190 *
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800191 * @param zoneName the name of the zone holding the rrset
Jiewen Tan870b29b2014-11-17 19:09:49 -0800192 * @param label rrset's label
193 * @param type rrset's type
194 * @param os the ostream to print information to
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800195 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800196 void
197 getRrSet(const Name& zoneName,
198 const Name& label,
199 const name::Component& type,
200 std::ostream& os);
201
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400202 security::Certificate
Yumin Xia2c509c22017-02-09 14:37:36 -0800203 getZoneDkey(Zone& zone);
204
Jiewen Tan870b29b2014-11-17 19:09:49 -0800205 /** @brief generates an output like DNS zone file. Reference:
206 * http://en.wikipedia.org/wiki/Zone_file
207 *
208 * @param zoneName the name of the zone to investigate
209 * @param os the ostream to print information to
210 * @param printRaw set to print content of ndns-raw rrset
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800211 * @throw Error if zoneName does not exist in the database
212 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800213 void
Davide Pesavento38fd3982022-04-18 22:22:02 -0400214 listZone(const Name& zoneName, std::ostream& os, bool printRaw = false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800215
216 /** @brief lists all existing zones within this name server.
217 *
218 * @param os the ostream to print information to
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800219 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800220 void
221 listAllZones(std::ostream& os);
222
223private:
Yumin Xia2c509c22017-02-09 14:37:36 -0800224 /** @brief add CERT to the NDNS local database
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800225 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800226 void
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400227 addIdCert(Zone& zone, const ndn::security::Certificate& cert,
Yumin Xia2c509c22017-02-09 14:37:36 -0800228 const time::seconds& ttl,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400229 const ndn::security::Certificate& dskCertName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800230
231 /** @brief add zone to the NDNS local database
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800232 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800233 void
234 addZone(Zone& zone);
235
236 /** @brief remove zone from the NDNS local database
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800237 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800238 void
239 removeZone(Zone& zone);
240
241 /** @brief determine whether a certificate matches with both the identity and key type
242 */
243 bool
244 matchCertificate(const Name& certName, const Name& identity);
245
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700246 /** @brief determine whether an older version of the rrset exists
247 */
248 void
249 checkRrsetVersion(const Rrset& rrset);
250
Yumin Xia55a7cc42017-05-14 18:43:34 -0700251 /**
252 @brief generate all Doe records
253 */
254 void generateDoe(Zone& zone);
255
Jiewen Tan870b29b2014-11-17 19:09:49 -0800256private:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800257 KeyChain& m_keyChain;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800258 DbMgr m_dbMgr;
259};
260
261} // namespace ndns
262} // namespace ndn
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800263
Jiewen Tan870b29b2014-11-17 19:09:49 -0800264#endif // NDNS_MGMT_MANAGEMENT_TOOL_HPP