blob: bb13c1ed58c3985c4e360886518ce287270929a0 [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
66 /** @param certDir Path to the directory to store certificates
67 * @param dbFile Path to the local database
68 */
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080069 ManagementTool(const std::string& dbFile, KeyChain& keyChain);
Jiewen Tan870b29b2014-11-17 19:09:49 -080070
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 Afanasyevd6b3bda2014-11-25 17:33:58 -080091 * @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 Tan01693fd2015-03-25 20:34:45 -070094 * @param kskCertName if given, a zone will be created with this ksk certificate
95 * @param dskCertName if given, a zone will be created with this dsk certificate and provided
96 * ksk certificate will be ignored
Jiewen Tan870b29b2014-11-17 19:09:49 -080097 */
98 void
99 createZone(const Name& zoneName,
100 const Name& parentZoneName,
101 const time::seconds& cacheTtl = DEFAULT_CACHE_TTL,
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800102 const time::seconds& certValidity = DEFAULT_CERT_TTL,
Jiewen Tan870b29b2014-11-17 19:09:49 -0800103 const Name& kskCertName = DEFAULT_CERT,
104 const Name& dskCertName = DEFAULT_CERT);
105
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
111 * 3) Local NDNS database: delete the ID-CERT of the zone's DSK
112 */
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
119 * @param output the path to output to-be exported file, including the file name
120 */
121 void
122 exportCertificate(const Name& certName, const std::string& outFile = DEFAULT_IO);
123
124 /** @brief add rrset to the NDNS local database
125 *
126 * This one is only capable of adding NS type including NDNS_RESP and NDNS_AUTH and user defined
127 * type with string content.
128 * Other complicated situations can be handled by the other addRrSet() overload function.
129 *
130 * @param zoneName the name of the zone to hold the rrset
131 * @param label the rrset label
132 * @param type the rrset type
133 * @param ndnsType the ndnsType of the response, for user-defined type, just set it NDNS_RAW
134 * @param version the version of the response and rrset, default is Unix Timestamp
135 * @param content the content of the response
136 * @param dskName the DSK to signed the response, default is the zone's DSK
137 * @param ttl the ttl of the rrset
138 */
139 void
140 addRrSet(const Name& zoneName,
141 const Name& label,
142 const name::Component& type,
143 NdnsType ndnsType,
144 const uint64_t version = VERSION_USE_UNIX_TIMESTAMP,
145 const std::vector<std::string>& contents = DEFAULT_CONTENTS,
146 const Name& dskCertName = DEFAULT_CERT,
147 const time::seconds& ttl = DEFAULT_RR_TTL);
148
149 /** @brief add rrset to the NDNS local database
150 *
151 * This overload is capable of adding any data to the rrset as long as the supplied data is
152 * valid.
153 * A special case is to add the ID-CERT of KSK to the parent zone. At this case, the SS cert
154 * should be supplied, and therefore it will use the parent zone's DSK to resign the certificate.
155 * For other cases, the data will be added directly without any modification.
156 *
157 * @param zoneName the name of the zone to hold the rrset
158 * @param dataPath the path to the supplied data
159 * @param ttl the ttl of the rrset
160 * @param dskName the DSK to signed the special case, default is the zone's DSK
Jiewen Tan74d745c2015-03-20 01:40:41 -0700161 * @param encoding the encoding of the input file
Jiewen Tan870b29b2014-11-17 19:09:49 -0800162 */
163 void
164 addRrSet(const Name& zoneName,
165 const std::string& inFile = DEFAULT_IO,
166 const time::seconds& ttl = DEFAULT_RR_TTL,
Jiewen Tan74d745c2015-03-20 01:40:41 -0700167 const Name& dskCertName = DEFAULT_CERT,
168 const ndn::io::IoEncoding encoding = ndn::io::BASE_64);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800169
170 /** @brief remove rrset from the NDNS local database
171 *
172 * @param zonName the name of the zone holding the rrset
173 * @param label rrset's label
174 * @param type rrset's type
175 */
176 void
177 removeRrSet(const Name& zoneName, const Name& label, const name::Component& type);
178
179 /** @brief output the raw data of the selected rrset
180 *
181 * @param zonName the name of the zone holding the rrset
182 * @param label rrset's label
183 * @param type rrset's type
184 * @param os the ostream to print information to
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800185 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800186 void
187 getRrSet(const Name& zoneName,
188 const Name& label,
189 const name::Component& type,
190 std::ostream& os);
191
192 /** @brief generates an output like DNS zone file. Reference:
193 * http://en.wikipedia.org/wiki/Zone_file
194 *
195 * @param zoneName the name of the zone to investigate
196 * @param os the ostream to print information to
197 * @param printRaw set to print content of ndns-raw rrset
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800198 * @throw Error if zoneName does not exist in the database
199 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800200 void
201 listZone(const Name& zoneName, std::ostream& os, const bool printRaw = false);
202
203 /** @brief lists all existing zones within this name server.
204 *
205 * @param os the ostream to print information to
206 */
207 void
208 listAllZones(std::ostream& os);
209
210private:
211 /** @brief add ID-CERT to the NDNS local database
212 */
213 void
214 addIdCert(Zone& zone, shared_ptr<IdentityCertificate> cert, const time::seconds& ttl);
215
216 /** @brief add zone to the NDNS local database
217 */
218 void
219 addZone(Zone& zone);
220
221 /** @brief remove zone from the NDNS local database
222 */
223 void
224 removeZone(Zone& zone);
225
226 /** @brief determine whether a certificate matches with both the identity and key type
227 */
228 bool
229 matchCertificate(const Name& certName, const Name& identity);
230
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700231 /** @brief determine whether an older version of the rrset exists
232 */
233 void
234 checkRrsetVersion(const Rrset& rrset);
235
Jiewen Tan870b29b2014-11-17 19:09:49 -0800236private:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800237 KeyChain& m_keyChain;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800238 DbMgr m_dbMgr;
239};
240
241} // namespace ndns
242} // namespace ndn
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800243
Jiewen Tan870b29b2014-11-17 19:09:49 -0800244#endif // NDNS_MGMT_MANAGEMENT_TOOL_HPP