blob: b40f6f061069c5ceff9d515afbac362d4715eccf [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- 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#include "daemon/name-server.hpp"
21#include "logger.hpp"
22#include "config.hpp"
23#include "daemon/config-file.hpp"
24#include "ndn-cxx/security/key-chain.hpp"
25#include <boost/program_options.hpp>
26
27namespace ndn {
28namespace ndns {
29
30NDNS_LOG_INIT("NdnsDaemon");
31
32/**
33 * @brief Name Server Daemon
34 * @note NdnsDaemon allows multiple name servers hosted by the same daemon, and they
35 * share same KeyChain, DbMgr, Validator and Face
36 */
37class NdnsDaemon : noncopyable
38{
Shock Jiange1a81fd2014-11-20 20:25:49 -080039public:
Shock Jiangcde28712014-10-19 21:17:20 -070040 DEFINE_ERROR(Error, std::runtime_error);
41
Shock Jiangcde28712014-10-19 21:17:20 -070042 explicit
Shock Jiange1a81fd2014-11-20 20:25:49 -080043 NdnsDaemon(const std::string& configFile, Face& face, Face& validatorFace)
44 : m_face(face)
45 , m_validatorFace(validatorFace)
Shock Jiangcde28712014-10-19 21:17:20 -070046 {
47 try {
48 ConfigFile config;
Shock Jiange1a81fd2014-11-20 20:25:49 -080049 NDNS_LOG_INFO("NnsnDaemon ConfigFile = " << configFile);
Shock Jiangcde28712014-10-19 21:17:20 -070050
51 config.addSectionHandler("zones",
52 bind(&NdnsDaemon::processZonesSection, this, _1, _3));
53 config.addSectionHandler("hints",
54 bind(&NdnsDaemon::processHintsSection, this, _1, _3));
55
56 config.parse(configFile, false);
57
58 }
59 catch (boost::filesystem::filesystem_error& e) {
60 if (e.code() == boost::system::errc::permission_denied) {
61 NDNS_LOG_FATAL("Permissions denied for " << e.path1());
62 }
63 else {
64 NDNS_LOG_FATAL(e.what());
65 }
66 }
67 catch (const std::exception& e) {
68 NDNS_LOG_FATAL(e.what());
69 }
70 }
71
72 void
73 processHintsSection(const ndn::ndns::ConfigSection& section, const std::string& filename)
74 {
75 // hint is not supported yet
76 ;
77 }
78
79 void
80 processZonesSection(const ndn::ndns::ConfigSection& section, const std::string& filename)
81 {
82 using namespace boost::filesystem;
83 using namespace ndn::ndns;
84 using ndn::ndns::ConfigSection;
85
86 if (section.begin() == section.end()) {
87 throw Error("zones section is empty");
88 }
89
Shock Jiange1a81fd2014-11-20 20:25:49 -080090 std::string dbFile = DEFAULT_DATABASE_PATH "/" "ndns.db";
Shock Jiangcde28712014-10-19 21:17:20 -070091 ConfigSection::const_assoc_iterator item = section.find("dbFile");
92 if (item != section.not_found()) {
Shock Jiange1a81fd2014-11-20 20:25:49 -080093 dbFile = item->second.get_value<std::string>();
Shock Jiangcde28712014-10-19 21:17:20 -070094 }
Shock Jiange1a81fd2014-11-20 20:25:49 -080095 NDNS_LOG_INFO("DbFile = " << dbFile);
96 m_dbMgr = unique_ptr<DbMgr>(new DbMgr(dbFile));
Shock Jiangcde28712014-10-19 21:17:20 -070097
Shock Jiange1a81fd2014-11-20 20:25:49 -080098 std::string validatorConfigFile = DEFAULT_CONFIG_PATH "/" "validator.conf";
99 item = section.find("validatorConfigFile");
100 if (item != section.not_found()) {
101 validatorConfigFile = item->second.get_value<std::string>();
102 }
103 NDNS_LOG_INFO("ValidatorConfigFile = " << validatorConfigFile);
104 m_validator = unique_ptr<Validator>(new Validator(m_validatorFace, validatorConfigFile));
Shock Jiangcde28712014-10-19 21:17:20 -0700105
106 for (const auto& option : section) {
107 Name name;
108 Name cert;
109 if (option.first == "zone") {
110 try {
111 name = option.second.get<Name>("name"); // exception leads to exit
112 }
113 catch (const std::exception& e) {
114 NDNS_LOG_ERROR("Required `name' attribute missing in `zone' section");
115 throw Error("Required `name' attribute missing in `zone' section");
116 }
117 try {
118 cert = option.second.get<Name>("cert");
119 }
120 catch (std::exception&) {
121 ;
122 }
123
Shock Jiange1a81fd2014-11-20 20:25:49 -0800124
125 if (!m_keyChain.doesIdentityExist(name)) {
126 NDNS_LOG_FATAL("Identity: " << name << " does not exist in the KeyChain");
127 throw Error("Identity does not exist in the KeyChain");
128 }
129
Shock Jiangcde28712014-10-19 21:17:20 -0700130 if (cert.empty()) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800131 try {
132 cert = m_keyChain.getDefaultCertificateNameForIdentity(name);
133 }
134 catch (std::exception& e) {
135 NDNS_LOG_FATAL("Identity: " << name << " does not have default certificate. "
136 << e.what());
137 throw Error("identity does not have default certificate");
138 }
Shock Jiangcde28712014-10-19 21:17:20 -0700139 }
140 else {
141 if (!m_keyChain.doesCertificateExist(cert)) {
142 throw Error("Certificate `" + cert.toUri() + "` does not exist in the KeyChain");
143 }
144 }
145 NDNS_LOG_TRACE("name = " << name << " cert = " << cert);
146 m_servers.push_back(make_shared<NameServer>(name, cert, m_face, *m_dbMgr,
Shock Jiange1a81fd2014-11-20 20:25:49 -0800147 m_keyChain, *m_validator));
Shock Jiangcde28712014-10-19 21:17:20 -0700148 }
149 } // for
150 }
151
152private:
Shock Jiangcde28712014-10-19 21:17:20 -0700153 Face& m_face;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800154 Face& m_validatorFace;
155 unique_ptr<Validator> m_validator;
156 unique_ptr<DbMgr> m_dbMgr;
157 std::vector<shared_ptr<NameServer>> m_servers;
Shock Jiangcde28712014-10-19 21:17:20 -0700158 KeyChain m_keyChain;
159};
160
161} // namespace ndns
162} // namespace ndn
163
164int
165main(int argc, char* argv[])
166{
167 using std::string;
168 using ndn::ndns::ConfigFile;
169 using namespace ndn::ndns;
170
171 ndn::ndns::log::init();
172 string configFile = DEFAULT_CONFIG_PATH "/" "ndns.conf";
173
174 try {
175 namespace po = boost::program_options;
176 po::variables_map vm;
177
178 po::options_description generic("Generic Options");
179 generic.add_options()("help,h", "print help message");
180
181 po::options_description config("Configuration");
182 config.add_options()
183 ("config,c", po::value<string>(&configFile), "set the path of configuration file")
184 ;
185
186 po::options_description cmdline_options;
187 cmdline_options.add(generic).add(config);
188
189 po::parsed_options parsed =
190 po::command_line_parser(argc, argv).options(cmdline_options).run();
191
192 po::store(parsed, vm);
193 po::notify(vm);
194
195 if (vm.count("help")) {
196 std::cout << "Usage:\n"
197 << " ndns-daemon [-c configFile]\n"
198 << std::endl;
199 std::cout << generic << config << std::endl;
200 return 0;
201 }
202 }
203 catch (const std::exception& ex) {
204 std::cerr << "Parameter Error: " << ex.what() << std::endl;
205
206 return 1;
207 }
208 catch (...) {
209 std::cerr << "Parameter Unknown error" << std::endl;
210 return 1;
211 }
212
Shock Jiange1a81fd2014-11-20 20:25:49 -0800213 boost::asio::io_service io;
214 ndn::Face face(io);
215 ndn::Face validatorFace(io);
216
Shock Jiangcde28712014-10-19 21:17:20 -0700217 try {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800218 // NFD does not to forward Interests to the face it was received from.
219 // If the name server and its validator share same face,
220 // the validator cannot be forwarded to the name server itself
Shock Jiang06cd2142014-11-23 17:36:02 -0800221 // For current, two faces are used here.
222
Shock Jiange1a81fd2014-11-20 20:25:49 -0800223 // refs: http://redmine.named-data.net/issues/2206
224 // @TODO enhance validator to get the certificate from the local db if it has
Shock Jiangcde28712014-10-19 21:17:20 -0700225
Shock Jiange1a81fd2014-11-20 20:25:49 -0800226 NdnsDaemon daemon(configFile, face, validatorFace);
Shock Jiangcde28712014-10-19 21:17:20 -0700227 face.processEvents();
228 }
229 catch (std::exception& e) {
230 NDNS_LOG_FATAL("ERROR: " << e.what());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800231 return 1;
Shock Jiangcde28712014-10-19 21:17:20 -0700232 }
233
234 return 0;
235}