blob: 549cd343323e14c48c6fa19be08f3feed60d2024 [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
3 * Copyright (c) 2014-2017, Regents of the University of California.
Shock Jiangcde28712014-10-19 21:17:20 -07004 *
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"
Yumin Xia2c509c22017-02-09 14:37:36 -080025#include "util/cert-helper.hpp"
26
Shock Jiangcde28712014-10-19 21:17:20 -070027#include <boost/program_options.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080028#include <boost/filesystem.hpp>
Shock Jiangcde28712014-10-19 21:17:20 -070029
30namespace ndn {
31namespace ndns {
32
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070033NDNS_LOG_INIT("NdnsDaemon")
Shock Jiangcde28712014-10-19 21:17:20 -070034
35/**
36 * @brief Name Server Daemon
37 * @note NdnsDaemon allows multiple name servers hosted by the same daemon, and they
38 * share same KeyChain, DbMgr, Validator and Face
39 */
40class NdnsDaemon : noncopyable
41{
Shock Jiange1a81fd2014-11-20 20:25:49 -080042public:
Shock Jiangcde28712014-10-19 21:17:20 -070043 DEFINE_ERROR(Error, std::runtime_error);
44
Shock Jiangcde28712014-10-19 21:17:20 -070045 explicit
Shock Jiange1a81fd2014-11-20 20:25:49 -080046 NdnsDaemon(const std::string& configFile, Face& face, Face& validatorFace)
47 : m_face(face)
48 , m_validatorFace(validatorFace)
Shock Jiangcde28712014-10-19 21:17:20 -070049 {
50 try {
51 ConfigFile config;
Shock Jiange1a81fd2014-11-20 20:25:49 -080052 NDNS_LOG_INFO("NnsnDaemon ConfigFile = " << configFile);
Shock Jiangcde28712014-10-19 21:17:20 -070053
54 config.addSectionHandler("zones",
55 bind(&NdnsDaemon::processZonesSection, this, _1, _3));
Shock Jiangcde28712014-10-19 21:17:20 -070056
57 config.parse(configFile, false);
58
59 }
Yumin Xia2c509c22017-02-09 14:37:36 -080060 catch (const boost::filesystem::filesystem_error& e) {
Shock Jiangcde28712014-10-19 21:17:20 -070061 if (e.code() == boost::system::errc::permission_denied) {
62 NDNS_LOG_FATAL("Permissions denied for " << e.path1());
63 }
64 else {
65 NDNS_LOG_FATAL(e.what());
66 }
67 }
68 catch (const std::exception& e) {
69 NDNS_LOG_FATAL(e.what());
70 }
71 }
72
73 void
Shock Jiangcde28712014-10-19 21:17:20 -070074 processZonesSection(const ndn::ndns::ConfigSection& section, const std::string& filename)
75 {
76 using namespace boost::filesystem;
77 using namespace ndn::ndns;
78 using ndn::ndns::ConfigSection;
79
80 if (section.begin() == section.end()) {
Yumin Xia2c509c22017-02-09 14:37:36 -080081 BOOST_THROW_EXCEPTION(Error("zones section is empty"));
Shock Jiangcde28712014-10-19 21:17:20 -070082 }
83
Shock Jiange1a81fd2014-11-20 20:25:49 -080084 std::string dbFile = DEFAULT_DATABASE_PATH "/" "ndns.db";
Shock Jiangcde28712014-10-19 21:17:20 -070085 ConfigSection::const_assoc_iterator item = section.find("dbFile");
86 if (item != section.not_found()) {
Shock Jiange1a81fd2014-11-20 20:25:49 -080087 dbFile = item->second.get_value<std::string>();
Shock Jiangcde28712014-10-19 21:17:20 -070088 }
Shock Jiange1a81fd2014-11-20 20:25:49 -080089 NDNS_LOG_INFO("DbFile = " << dbFile);
90 m_dbMgr = unique_ptr<DbMgr>(new DbMgr(dbFile));
Shock Jiangcde28712014-10-19 21:17:20 -070091
Shock Jiange1a81fd2014-11-20 20:25:49 -080092 std::string validatorConfigFile = DEFAULT_CONFIG_PATH "/" "validator.conf";
93 item = section.find("validatorConfigFile");
94 if (item != section.not_found()) {
95 validatorConfigFile = item->second.get_value<std::string>();
96 }
97 NDNS_LOG_INFO("ValidatorConfigFile = " << validatorConfigFile);
Yumin Xia2c509c22017-02-09 14:37:36 -080098 m_validator = NdnsValidatorBuilder::create(m_validatorFace, validatorConfigFile);
Shock Jiangcde28712014-10-19 21:17:20 -070099
100 for (const auto& option : section) {
101 Name name;
102 Name cert;
103 if (option.first == "zone") {
104 try {
105 name = option.second.get<Name>("name"); // exception leads to exit
106 }
107 catch (const std::exception& e) {
108 NDNS_LOG_ERROR("Required `name' attribute missing in `zone' section");
Yumin Xia2c509c22017-02-09 14:37:36 -0800109 BOOST_THROW_EXCEPTION(Error("Required `name' attribute missing in `zone' section"));
Shock Jiangcde28712014-10-19 21:17:20 -0700110 }
111 try {
112 cert = option.second.get<Name>("cert");
113 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800114 catch (const std::exception&) {
Shock Jiangcde28712014-10-19 21:17:20 -0700115 ;
116 }
117
118 if (cert.empty()) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800119 try {
Yumin Xia2c509c22017-02-09 14:37:36 -0800120 cert = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain, Name(name).append(label::NDNS_ITERATIVE_QUERY));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800121 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800122 catch (const std::exception& e) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800123 NDNS_LOG_FATAL("Identity: " << name << " does not have default certificate. "
124 << e.what());
Yumin Xia2c509c22017-02-09 14:37:36 -0800125 BOOST_THROW_EXCEPTION(Error("identity does not have default certificate"));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800126 }
Shock Jiangcde28712014-10-19 21:17:20 -0700127 }
128 else {
Yumin Xia2c509c22017-02-09 14:37:36 -0800129 try {
130 CertHelper::getCertificate(m_keyChain, name, cert);
131 } catch (const std::exception& e) {
132 BOOST_THROW_EXCEPTION(Error("Certificate `" + cert.toUri() + "` does not exist in the KeyChain"));
Shock Jiangcde28712014-10-19 21:17:20 -0700133 }
134 }
135 NDNS_LOG_TRACE("name = " << name << " cert = " << cert);
136 m_servers.push_back(make_shared<NameServer>(name, cert, m_face, *m_dbMgr,
Shock Jiange1a81fd2014-11-20 20:25:49 -0800137 m_keyChain, *m_validator));
Shock Jiangcde28712014-10-19 21:17:20 -0700138 }
139 } // for
140 }
141
142private:
Shock Jiangcde28712014-10-19 21:17:20 -0700143 Face& m_face;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800144 Face& m_validatorFace;
Yumin Xia2c509c22017-02-09 14:37:36 -0800145 unique_ptr<security::v2::Validator> m_validator;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800146 unique_ptr<DbMgr> m_dbMgr;
147 std::vector<shared_ptr<NameServer>> m_servers;
Shock Jiangcde28712014-10-19 21:17:20 -0700148 KeyChain m_keyChain;
149};
150
151} // namespace ndns
152} // namespace ndn
153
154int
155main(int argc, char* argv[])
156{
157 using std::string;
158 using ndn::ndns::ConfigFile;
159 using namespace ndn::ndns;
160
161 ndn::ndns::log::init();
162 string configFile = DEFAULT_CONFIG_PATH "/" "ndns.conf";
163
164 try {
165 namespace po = boost::program_options;
166 po::variables_map vm;
167
168 po::options_description generic("Generic Options");
169 generic.add_options()("help,h", "print help message");
170
171 po::options_description config("Configuration");
172 config.add_options()
173 ("config,c", po::value<string>(&configFile), "set the path of configuration file")
174 ;
175
176 po::options_description cmdline_options;
177 cmdline_options.add(generic).add(config);
178
179 po::parsed_options parsed =
180 po::command_line_parser(argc, argv).options(cmdline_options).run();
181
182 po::store(parsed, vm);
183 po::notify(vm);
184
185 if (vm.count("help")) {
186 std::cout << "Usage:\n"
187 << " ndns-daemon [-c configFile]\n"
188 << std::endl;
189 std::cout << generic << config << std::endl;
190 return 0;
191 }
192 }
193 catch (const std::exception& ex) {
194 std::cerr << "Parameter Error: " << ex.what() << std::endl;
195
196 return 1;
197 }
198 catch (...) {
199 std::cerr << "Parameter Unknown error" << std::endl;
200 return 1;
201 }
202
Shock Jiange1a81fd2014-11-20 20:25:49 -0800203 boost::asio::io_service io;
204 ndn::Face face(io);
205 ndn::Face validatorFace(io);
206
Shock Jiangcde28712014-10-19 21:17:20 -0700207 try {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800208 // NFD does not to forward Interests to the face it was received from.
209 // If the name server and its validator share same face,
210 // the validator cannot be forwarded to the name server itself
Shock Jiang06cd2142014-11-23 17:36:02 -0800211 // For current, two faces are used here.
212
Yumin Xia2c509c22017-02-09 14:37:36 -0800213 // refs: https://redmine.named-data.net/issues/2206
Shock Jiange1a81fd2014-11-20 20:25:49 -0800214 // @TODO enhance validator to get the certificate from the local db if it has
Shock Jiangcde28712014-10-19 21:17:20 -0700215
Shock Jiange1a81fd2014-11-20 20:25:49 -0800216 NdnsDaemon daemon(configFile, face, validatorFace);
Shock Jiangcde28712014-10-19 21:17:20 -0700217 face.processEvents();
218 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800219 catch (const std::exception& e) {
Shock Jiangcde28712014-10-19 21:17:20 -0700220 NDNS_LOG_FATAL("ERROR: " << e.what());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800221 return 1;
Shock Jiangcde28712014-10-19 21:17:20 -0700222 }
223
224 return 0;
225}