blob: 54a14071acb4ebbcd49af34ddd5be3c5bbb4bc80 [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>
34
35namespace ndn {
36namespace ndns {
37
38static const Name DEFAULT_CERT;
39static const Name ROOT_ZONE;
40static const time::seconds DEFAULT_CACHE_TTL = time::seconds(3600);
41static const time::seconds DEFAULT_CERT_TTL = time::days(365);
42static const std::vector<std::string> DEFAULT_CONTENTS;
43static const std::string DEFAULT_IO = "-";
44static const time::seconds DEFAULT_RR_TTL = time::seconds(0);
45static constexpr uint64_t VERSION_USE_UNIX_TIMESTAMP = std::numeric_limits<uint64_t>::max();
46
47/**
48 * @brief provides management tools to the NDNS system, such as zone creation, zone delegation, DSK
49 * generation and root zone creation.
50 */
51class ManagementTool : noncopyable
52{
53public:
54 /** @brief Represents an error might be thrown during runtime
55 */
56 class Error : public std::runtime_error
57 {
58 public:
59 explicit
60 Error(const std::string& what) : std::runtime_error(what)
61 {
62 }
63 };
64
65 /** @param certDir Path to the directory to store certificates
66 * @param dbFile Path to the local database
67 */
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:
74 * 1) Local NDNS database: a new zone is added.
75 * 2) Local NDNS database: an ID-CERT of the DSK is added.
76 * 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
78 * parent zone.
79 * 5) KeyChain: a DSK and its KSK signed certificate is added.
80 *
81 * -SS.cert (self-signed)
82 * -SKS.cert (self's Key signed)
83 * -PKS.cert (parent's Key Signed)
84 *
85 * @attention
86 * 1) to create root zone, supply zoneName and parentZoneName both with ROOT_ZONE
87 *
88 * @param zoneName zone's name
89 * @param parentZoneName parent zone's name
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080090 * @param cacheTtl default TTL for RR sets in the zone
91 * @param certValidity validity for automatically created DSK certificate (@p dskCertName
92 * should not be empty)
Jiewen Tan870b29b2014-11-17 19:09:49 -080093 * @param kskCertName if given, a zone will be created with this ksk certificate and its key
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080094 * @param dskCertName if given, a zone will be created with this dsk certificate and its key
Jiewen Tan870b29b2014-11-17 19:09:49 -080095 */
96 void
97 createZone(const Name& zoneName,
98 const Name& parentZoneName,
99 const time::seconds& cacheTtl = DEFAULT_CACHE_TTL,
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800100 const time::seconds& certValidity = DEFAULT_CERT_TTL,
Jiewen Tan870b29b2014-11-17 19:09:49 -0800101 const Name& kskCertName = DEFAULT_CERT,
102 const Name& dskCertName = DEFAULT_CERT);
103
104 /** @brief Delete a Zone according to a given name.
105 *
106 * Specifically, It will do the following things:
107 * 1) KeyChain System: delete the Identity with zone name and all its keys/certificates
108 * 2) Local NDNS database: delete the zone record
109 * 3) Local NDNS database: delete the ID-CERT of the zone's DSK
110 */
111 void
112 deleteZone(const Name& zoneName);
113
114 /** @brief Export the certificate to file system
115 *
116 * @param certName the name of the certificate to be exported
117 * @param output the path to output to-be exported file, including the file name
118 */
119 void
120 exportCertificate(const Name& certName, const std::string& outFile = DEFAULT_IO);
121
122 /** @brief add rrset to the NDNS local database
123 *
124 * This one is only capable of adding NS type including NDNS_RESP and NDNS_AUTH and user defined
125 * type with string content.
126 * Other complicated situations can be handled by the other addRrSet() overload function.
127 *
128 * @param zoneName the name of the zone to hold the rrset
129 * @param label the rrset label
130 * @param type the rrset type
131 * @param ndnsType the ndnsType of the response, for user-defined type, just set it NDNS_RAW
132 * @param version the version of the response and rrset, default is Unix Timestamp
133 * @param content the content of the response
134 * @param dskName the DSK to signed the response, default is the zone's DSK
135 * @param ttl the ttl of the rrset
136 */
137 void
138 addRrSet(const Name& zoneName,
139 const Name& label,
140 const name::Component& type,
141 NdnsType ndnsType,
142 const uint64_t version = VERSION_USE_UNIX_TIMESTAMP,
143 const std::vector<std::string>& contents = DEFAULT_CONTENTS,
144 const Name& dskCertName = DEFAULT_CERT,
145 const time::seconds& ttl = DEFAULT_RR_TTL);
146
147 /** @brief add rrset to the NDNS local database
148 *
149 * This overload is capable of adding any data to the rrset as long as the supplied data is
150 * valid.
151 * A special case is to add the ID-CERT of KSK to the parent zone. At this case, the SS cert
152 * should be supplied, and therefore it will use the parent zone's DSK to resign the certificate.
153 * For other cases, the data will be added directly without any modification.
154 *
155 * @param zoneName the name of the zone to hold the rrset
156 * @param dataPath the path to the supplied data
157 * @param ttl the ttl of the rrset
158 * @param dskName the DSK to signed the special case, default is the zone's DSK
159 */
160 void
161 addRrSet(const Name& zoneName,
162 const std::string& inFile = DEFAULT_IO,
163 const time::seconds& ttl = DEFAULT_RR_TTL,
164 const Name& dskCertName = DEFAULT_CERT);
165
166 /** @brief remove rrset from the NDNS local database
167 *
168 * @param zonName the name of the zone holding the rrset
169 * @param label rrset's label
170 * @param type rrset's type
171 */
172 void
173 removeRrSet(const Name& zoneName, const Name& label, const name::Component& type);
174
175 /** @brief output the raw data of the selected rrset
176 *
177 * @param zonName the name of the zone holding the rrset
178 * @param label rrset's label
179 * @param type rrset's type
180 * @param os the ostream to print information to
181 * @param isPP indicate pretty print
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800182 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800183 void
184 getRrSet(const Name& zoneName,
185 const Name& label,
186 const name::Component& type,
187 std::ostream& os);
188
189 /** @brief generates an output like DNS zone file. Reference:
190 * http://en.wikipedia.org/wiki/Zone_file
191 *
192 * @param zoneName the name of the zone to investigate
193 * @param os the ostream to print information to
194 * @param printRaw set to print content of ndns-raw rrset
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800195 * @throw Error if zoneName does not exist in the database
196 */
Jiewen Tan870b29b2014-11-17 19:09:49 -0800197 void
198 listZone(const Name& zoneName, std::ostream& os, const bool printRaw = false);
199
200 /** @brief lists all existing zones within this name server.
201 *
202 * @param os the ostream to print information to
203 */
204 void
205 listAllZones(std::ostream& os);
206
207private:
208 /** @brief add ID-CERT to the NDNS local database
209 */
210 void
211 addIdCert(Zone& zone, shared_ptr<IdentityCertificate> cert, const time::seconds& ttl);
212
213 /** @brief add zone to the NDNS local database
214 */
215 void
216 addZone(Zone& zone);
217
218 /** @brief remove zone from the NDNS local database
219 */
220 void
221 removeZone(Zone& zone);
222
223 /** @brief determine whether a certificate matches with both the identity and key type
224 */
225 bool
226 matchCertificate(const Name& certName, const Name& identity);
227
228private:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800229 KeyChain& m_keyChain;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800230 DbMgr m_dbMgr;
231};
232
233} // namespace ndns
234} // namespace ndn
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800235
Jiewen Tan870b29b2014-11-17 19:09:49 -0800236#endif // NDNS_MGMT_MANAGEMENT_TOOL_HPP