blob: 64968c74464be13ce0812f8f232b6d22b4100485 [file] [log] [blame]
Jiewen Tan870b29b2014-11-17 19:09:49 -08001/* -*- 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 Tan74d745c2015-03-20 01:40:41 -070034#include <ndn-cxx/util/io.hpp>
Jiewen Tan870b29b2014-11-17 19:09:49 -080035
36namespace ndn {
37namespace ndns {
38
39static const Name DEFAULT_CERT;
40static const Name ROOT_ZONE;
41static const time::seconds DEFAULT_CACHE_TTL = time::seconds(3600);
42static const time::seconds DEFAULT_CERT_TTL = time::days(365);
43static const std::vector<std::string> DEFAULT_CONTENTS;
44static const std::string DEFAULT_IO = "-";
45static const time::seconds DEFAULT_RR_TTL = time::seconds(0);
46static 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 */
52class ManagementTool : noncopyable
53{
54public:
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 Afanasyevdf2e9392016-03-10 11:50:53 -080066 /**
67 * @brief Create instance of the tool
68 *
69 * @param dbFile Path to the local database
70 * @param keyChain Keychain instance
Jiewen Tan870b29b2014-11-17 19:09:49 -080071 */
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080072 ManagementTool(const std::string& dbFile, KeyChain& keyChain);
Jiewen Tan870b29b2014-11-17 19:09:49 -080073
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:
78 * 1) Local NDNS database: a new zone is added.
79 * 2) Local NDNS database: an ID-CERT of the DSK is added.
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
82 * parent zone.
83 * 5) KeyChain: a DSK and its KSK signed certificate is added.
84 *
85 * -SS.cert (self-signed)
86 * -SKS.cert (self's Key signed)
87 * -PKS.cert (parent's Key Signed)
88 *
89 * @attention
90 * 1) to create root zone, supply zoneName and parentZoneName both with ROOT_ZONE
91 *
92 * @param zoneName zone's name
93 * @param parentZoneName parent zone's name
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080094 * @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 Tan01693fd2015-03-25 20:34:45 -070097 * @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
99 * ksk certificate will be ignored
Jiewen Tan870b29b2014-11-17 19:09:49 -0800100 */
101 void
102 createZone(const Name& zoneName,
103 const Name& parentZoneName,
104 const time::seconds& cacheTtl = DEFAULT_CACHE_TTL,
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800105 const time::seconds& certValidity = DEFAULT_CERT_TTL,
Jiewen Tan870b29b2014-11-17 19:09:49 -0800106 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 Afanasyevdf2e9392016-03-10 11:50:53 -0800122 * @param outFile the path to output to-be exported file, including the file name
Jiewen Tan870b29b2014-11-17 19:09:49 -0800123 */
124 void
125 exportCertificate(const Name& certName, const std::string& outFile = DEFAULT_IO);
126
127 /** @brief add rrset to the NDNS local database
128 *
129 * This one is only capable of adding NS type including NDNS_RESP and NDNS_AUTH and user defined
130 * type with string content.
131 * Other complicated situations can be handled by the other addRrSet() overload function.
132 *
133 * @param zoneName the name of the zone to hold the rrset
134 * @param label the rrset label
135 * @param type the rrset type
136 * @param ndnsType the ndnsType of the response, for user-defined type, just set it NDNS_RAW
137 * @param version the version of the response and rrset, default is Unix Timestamp
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800138 * @param contents the content of the response
139 * @param dskCertName the DSK to signed the response, default is the zone's DSK
Jiewen Tan870b29b2014-11-17 19:09:49 -0800140 * @param ttl the ttl of the rrset
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800141 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800142 void
143 addRrSet(const Name& zoneName,
144 const Name& label,
145 const name::Component& type,
146 NdnsType ndnsType,
147 const uint64_t version = VERSION_USE_UNIX_TIMESTAMP,
148 const std::vector<std::string>& contents = DEFAULT_CONTENTS,
149 const Name& dskCertName = DEFAULT_CERT,
150 const time::seconds& ttl = DEFAULT_RR_TTL);
151
152 /** @brief add rrset to the NDNS local database
153 *
154 * This overload is capable of adding any data to the rrset as long as the supplied data is
155 * valid.
156 * A special case is to add the ID-CERT of KSK to the parent zone. At this case, the SS cert
157 * should be supplied, and therefore it will use the parent zone's DSK to resign the certificate.
158 * For other cases, the data will be added directly without any modification.
159 *
160 * @param zoneName the name of the zone to hold the rrset
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800161 * @param inFile the path to the supplied data
Jiewen Tan870b29b2014-11-17 19:09:49 -0800162 * @param ttl the ttl of the rrset
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800163 * @param dskCertName the DSK to signed the special case, default is the zone's DSK
Jiewen Tan74d745c2015-03-20 01:40:41 -0700164 * @param encoding the encoding of the input file
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800165 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800166 void
167 addRrSet(const Name& zoneName,
168 const std::string& inFile = DEFAULT_IO,
169 const time::seconds& ttl = DEFAULT_RR_TTL,
Jiewen Tan74d745c2015-03-20 01:40:41 -0700170 const Name& dskCertName = DEFAULT_CERT,
171 const ndn::io::IoEncoding encoding = ndn::io::BASE_64);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800172
173 /** @brief remove rrset from the NDNS local database
174 *
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800175 * @param zoneName the name of the zone holding the rrset
Jiewen Tan870b29b2014-11-17 19:09:49 -0800176 * @param label rrset's label
177 * @param type rrset's type
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800178 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800179 void
180 removeRrSet(const Name& zoneName, const Name& label, const name::Component& type);
181
182 /** @brief output the raw data of the selected rrset
183 *
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800184 * @param zoneName the name of the zone holding the rrset
Jiewen Tan870b29b2014-11-17 19:09:49 -0800185 * @param label rrset's label
186 * @param type rrset's type
187 * @param os the ostream to print information to
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800188 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800189 void
190 getRrSet(const Name& zoneName,
191 const Name& label,
192 const name::Component& type,
193 std::ostream& os);
194
195 /** @brief generates an output like DNS zone file. Reference:
196 * http://en.wikipedia.org/wiki/Zone_file
197 *
198 * @param zoneName the name of the zone to investigate
199 * @param os the ostream to print information to
200 * @param printRaw set to print content of ndns-raw rrset
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800201 * @throw Error if zoneName does not exist in the database
202 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800203 void
204 listZone(const Name& zoneName, std::ostream& os, const bool printRaw = false);
205
206 /** @brief lists all existing zones within this name server.
207 *
208 * @param os the ostream to print information to
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800209 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800210 void
211 listAllZones(std::ostream& os);
212
213private:
214 /** @brief add ID-CERT to the NDNS local database
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800215 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800216 void
217 addIdCert(Zone& zone, shared_ptr<IdentityCertificate> cert, const time::seconds& ttl);
218
219 /** @brief add zone to the NDNS local database
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800220 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800221 void
222 addZone(Zone& zone);
223
224 /** @brief remove zone from the NDNS local database
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -0800225 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800226 void
227 removeZone(Zone& zone);
228
229 /** @brief determine whether a certificate matches with both the identity and key type
230 */
231 bool
232 matchCertificate(const Name& certName, const Name& identity);
233
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700234 /** @brief determine whether an older version of the rrset exists
235 */
236 void
237 checkRrsetVersion(const Rrset& rrset);
238
Jiewen Tan870b29b2014-11-17 19:09:49 -0800239private:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800240 KeyChain& m_keyChain;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800241 DbMgr m_dbMgr;
242};
243
244} // namespace ndns
245} // namespace ndn
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800246
Jiewen Tan870b29b2014-11-17 19:09:49 -0800247#endif // NDNS_MGMT_MANAGEMENT_TOOL_HPP