blob: 65028ea5a0b08f661976af1110ca1fcf7617c436 [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yumin Xia6343c5b2016-10-20 15:45:50 -07003 * Copyright (c) 2014-2016, 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"
25#include <boost/program_options.hpp>
26
27namespace ndn {
28namespace ndns {
29
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070030NDNS_LOG_INIT("NdnsDaemon")
Shock Jiangcde28712014-10-19 21:17:20 -070031
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));
Shock Jiangcde28712014-10-19 21:17:20 -070053
54 config.parse(configFile, false);
55
56 }
57 catch (boost::filesystem::filesystem_error& e) {
58 if (e.code() == boost::system::errc::permission_denied) {
59 NDNS_LOG_FATAL("Permissions denied for " << e.path1());
60 }
61 else {
62 NDNS_LOG_FATAL(e.what());
63 }
64 }
65 catch (const std::exception& e) {
66 NDNS_LOG_FATAL(e.what());
67 }
68 }
69
70 void
Shock Jiangcde28712014-10-19 21:17:20 -070071 processZonesSection(const ndn::ndns::ConfigSection& section, const std::string& filename)
72 {
73 using namespace boost::filesystem;
74 using namespace ndn::ndns;
75 using ndn::ndns::ConfigSection;
76
77 if (section.begin() == section.end()) {
78 throw Error("zones section is empty");
79 }
80
Shock Jiange1a81fd2014-11-20 20:25:49 -080081 std::string dbFile = DEFAULT_DATABASE_PATH "/" "ndns.db";
Shock Jiangcde28712014-10-19 21:17:20 -070082 ConfigSection::const_assoc_iterator item = section.find("dbFile");
83 if (item != section.not_found()) {
Shock Jiange1a81fd2014-11-20 20:25:49 -080084 dbFile = item->second.get_value<std::string>();
Shock Jiangcde28712014-10-19 21:17:20 -070085 }
Shock Jiange1a81fd2014-11-20 20:25:49 -080086 NDNS_LOG_INFO("DbFile = " << dbFile);
87 m_dbMgr = unique_ptr<DbMgr>(new DbMgr(dbFile));
Shock Jiangcde28712014-10-19 21:17:20 -070088
Shock Jiange1a81fd2014-11-20 20:25:49 -080089 std::string validatorConfigFile = DEFAULT_CONFIG_PATH "/" "validator.conf";
90 item = section.find("validatorConfigFile");
91 if (item != section.not_found()) {
92 validatorConfigFile = item->second.get_value<std::string>();
93 }
94 NDNS_LOG_INFO("ValidatorConfigFile = " << validatorConfigFile);
95 m_validator = unique_ptr<Validator>(new Validator(m_validatorFace, validatorConfigFile));
Shock Jiangcde28712014-10-19 21:17:20 -070096
97 for (const auto& option : section) {
98 Name name;
99 Name cert;
100 if (option.first == "zone") {
101 try {
102 name = option.second.get<Name>("name"); // exception leads to exit
103 }
104 catch (const std::exception& e) {
105 NDNS_LOG_ERROR("Required `name' attribute missing in `zone' section");
106 throw Error("Required `name' attribute missing in `zone' section");
107 }
108 try {
109 cert = option.second.get<Name>("cert");
110 }
111 catch (std::exception&) {
112 ;
113 }
114
Shock Jiange1a81fd2014-11-20 20:25:49 -0800115
116 if (!m_keyChain.doesIdentityExist(name)) {
117 NDNS_LOG_FATAL("Identity: " << name << " does not exist in the KeyChain");
118 throw Error("Identity does not exist in the KeyChain");
119 }
120
Shock Jiangcde28712014-10-19 21:17:20 -0700121 if (cert.empty()) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800122 try {
123 cert = m_keyChain.getDefaultCertificateNameForIdentity(name);
124 }
125 catch (std::exception& e) {
126 NDNS_LOG_FATAL("Identity: " << name << " does not have default certificate. "
127 << e.what());
128 throw Error("identity does not have default certificate");
129 }
Shock Jiangcde28712014-10-19 21:17:20 -0700130 }
131 else {
132 if (!m_keyChain.doesCertificateExist(cert)) {
133 throw Error("Certificate `" + cert.toUri() + "` does not exist in the KeyChain");
134 }
135 }
136 NDNS_LOG_TRACE("name = " << name << " cert = " << cert);
137 m_servers.push_back(make_shared<NameServer>(name, cert, m_face, *m_dbMgr,
Shock Jiange1a81fd2014-11-20 20:25:49 -0800138 m_keyChain, *m_validator));
Shock Jiangcde28712014-10-19 21:17:20 -0700139 }
140 } // for
141 }
142
143private:
Shock Jiangcde28712014-10-19 21:17:20 -0700144 Face& m_face;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800145 Face& m_validatorFace;
146 unique_ptr<Validator> m_validator;
147 unique_ptr<DbMgr> m_dbMgr;
148 std::vector<shared_ptr<NameServer>> m_servers;
Shock Jiangcde28712014-10-19 21:17:20 -0700149 KeyChain m_keyChain;
150};
151
152} // namespace ndns
153} // namespace ndn
154
155int
156main(int argc, char* argv[])
157{
158 using std::string;
159 using ndn::ndns::ConfigFile;
160 using namespace ndn::ndns;
161
162 ndn::ndns::log::init();
163 string configFile = DEFAULT_CONFIG_PATH "/" "ndns.conf";
164
165 try {
166 namespace po = boost::program_options;
167 po::variables_map vm;
168
169 po::options_description generic("Generic Options");
170 generic.add_options()("help,h", "print help message");
171
172 po::options_description config("Configuration");
173 config.add_options()
174 ("config,c", po::value<string>(&configFile), "set the path of configuration file")
175 ;
176
177 po::options_description cmdline_options;
178 cmdline_options.add(generic).add(config);
179
180 po::parsed_options parsed =
181 po::command_line_parser(argc, argv).options(cmdline_options).run();
182
183 po::store(parsed, vm);
184 po::notify(vm);
185
186 if (vm.count("help")) {
187 std::cout << "Usage:\n"
188 << " ndns-daemon [-c configFile]\n"
189 << std::endl;
190 std::cout << generic << config << std::endl;
191 return 0;
192 }
193 }
194 catch (const std::exception& ex) {
195 std::cerr << "Parameter Error: " << ex.what() << std::endl;
196
197 return 1;
198 }
199 catch (...) {
200 std::cerr << "Parameter Unknown error" << std::endl;
201 return 1;
202 }
203
Shock Jiange1a81fd2014-11-20 20:25:49 -0800204 boost::asio::io_service io;
205 ndn::Face face(io);
206 ndn::Face validatorFace(io);
207
Shock Jiangcde28712014-10-19 21:17:20 -0700208 try {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800209 // NFD does not to forward Interests to the face it was received from.
210 // If the name server and its validator share same face,
211 // the validator cannot be forwarded to the name server itself
Shock Jiang06cd2142014-11-23 17:36:02 -0800212 // For current, two faces are used here.
213
Shock Jiange1a81fd2014-11-20 20:25:49 -0800214 // refs: http://redmine.named-data.net/issues/2206
215 // @TODO enhance validator to get the certificate from the local db if it has
Shock Jiangcde28712014-10-19 21:17:20 -0700216
Shock Jiange1a81fd2014-11-20 20:25:49 -0800217 NdnsDaemon daemon(configFile, face, validatorFace);
Shock Jiangcde28712014-10-19 21:17:20 -0700218 face.processEvents();
219 }
220 catch (std::exception& e) {
221 NDNS_LOG_FATAL("ERROR: " << e.what());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800222 return 1;
Shock Jiangcde28712014-10-19 21:17:20 -0700223 }
224
225 return 0;
226}