blob: f962b11e1ccee88c7386ca40cd4dda4aa9bcca18 [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 */
68 explicit
69 ManagementTool(const std::string& dbFile);
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
91 * @param ttl ttl for the created zone
92 * @param kskCertName if given, a zone will be created with this ksk certificate and its key
93 * @param kskCertName if given, a zone will be created with this dsk certificate and its key
94 */
95 void
96 createZone(const Name& zoneName,
97 const Name& parentZoneName,
98 const time::seconds& cacheTtl = DEFAULT_CACHE_TTL,
99 const time::seconds& certTtl = DEFAULT_CERT_TTL,
100 const Name& kskCertName = DEFAULT_CERT,
101 const Name& dskCertName = DEFAULT_CERT);
102
103 /** @brief Delete a Zone according to a given name.
104 *
105 * Specifically, It will do the following things:
106 * 1) KeyChain System: delete the Identity with zone name and all its keys/certificates
107 * 2) Local NDNS database: delete the zone record
108 * 3) Local NDNS database: delete the ID-CERT of the zone's DSK
109 */
110 void
111 deleteZone(const Name& zoneName);
112
113 /** @brief Export the certificate to file system
114 *
115 * @param certName the name of the certificate to be exported
116 * @param output the path to output to-be exported file, including the file name
117 */
118 void
119 exportCertificate(const Name& certName, const std::string& outFile = DEFAULT_IO);
120
121 /** @brief add rrset to the NDNS local database
122 *
123 * This one is only capable of adding NS type including NDNS_RESP and NDNS_AUTH and user defined
124 * type with string content.
125 * Other complicated situations can be handled by the other addRrSet() overload function.
126 *
127 * @param zoneName the name of the zone to hold the rrset
128 * @param label the rrset label
129 * @param type the rrset type
130 * @param ndnsType the ndnsType of the response, for user-defined type, just set it NDNS_RAW
131 * @param version the version of the response and rrset, default is Unix Timestamp
132 * @param content the content of the response
133 * @param dskName the DSK to signed the response, default is the zone's DSK
134 * @param ttl the ttl of the rrset
135 */
136 void
137 addRrSet(const Name& zoneName,
138 const Name& label,
139 const name::Component& type,
140 NdnsType ndnsType,
141 const uint64_t version = VERSION_USE_UNIX_TIMESTAMP,
142 const std::vector<std::string>& contents = DEFAULT_CONTENTS,
143 const Name& dskCertName = DEFAULT_CERT,
144 const time::seconds& ttl = DEFAULT_RR_TTL);
145
146 /** @brief add rrset to the NDNS local database
147 *
148 * This overload is capable of adding any data to the rrset as long as the supplied data is
149 * valid.
150 * A special case is to add the ID-CERT of KSK to the parent zone. At this case, the SS cert
151 * should be supplied, and therefore it will use the parent zone's DSK to resign the certificate.
152 * For other cases, the data will be added directly without any modification.
153 *
154 * @param zoneName the name of the zone to hold the rrset
155 * @param dataPath the path to the supplied data
156 * @param ttl the ttl of the rrset
157 * @param dskName the DSK to signed the special case, default is the zone's DSK
158 */
159 void
160 addRrSet(const Name& zoneName,
161 const std::string& inFile = DEFAULT_IO,
162 const time::seconds& ttl = DEFAULT_RR_TTL,
163 const Name& dskCertName = DEFAULT_CERT);
164
165 /** @brief remove rrset from the NDNS local database
166 *
167 * @param zonName the name of the zone holding the rrset
168 * @param label rrset's label
169 * @param type rrset's type
170 */
171 void
172 removeRrSet(const Name& zoneName, const Name& label, const name::Component& type);
173
174 /** @brief output the raw data of the selected rrset
175 *
176 * @param zonName the name of the zone holding the rrset
177 * @param label rrset's label
178 * @param type rrset's type
179 * @param os the ostream to print information to
180 * @param isPP indicate pretty print
181 */
182 void
183 getRrSet(const Name& zoneName,
184 const Name& label,
185 const name::Component& type,
186 std::ostream& os);
187
188 /** @brief generates an output like DNS zone file. Reference:
189 * http://en.wikipedia.org/wiki/Zone_file
190 *
191 * @param zoneName the name of the zone to investigate
192 * @param os the ostream to print information to
193 * @param printRaw set to print content of ndns-raw rrset
194 */
195 void
196 listZone(const Name& zoneName, std::ostream& os, const bool printRaw = false);
197
198 /** @brief lists all existing zones within this name server.
199 *
200 * @param os the ostream to print information to
201 */
202 void
203 listAllZones(std::ostream& os);
204
205private:
206 /** @brief add ID-CERT to the NDNS local database
207 */
208 void
209 addIdCert(Zone& zone, shared_ptr<IdentityCertificate> cert, const time::seconds& ttl);
210
211 /** @brief add zone to the NDNS local database
212 */
213 void
214 addZone(Zone& zone);
215
216 /** @brief remove zone from the NDNS local database
217 */
218 void
219 removeZone(Zone& zone);
220
221 /** @brief determine whether a certificate matches with both the identity and key type
222 */
223 bool
224 matchCertificate(const Name& certName, const Name& identity);
225
226private:
227 KeyChain m_keyChain;
228 DbMgr m_dbMgr;
229};
230
231} // namespace ndns
232} // namespace ndn
233#endif // NDNS_MGMT_MANAGEMENT_TOOL_HPP