Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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" |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 25 | #include "util/cert-helper.hpp" |
| 26 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 27 | #include <boost/program_options.hpp> |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 28 | #include <boost/filesystem.hpp> |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace ndns { |
| 32 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 33 | NDNS_LOG_INIT(NdnsDaemon); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 34 | |
| 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 | */ |
| 40 | class NdnsDaemon : noncopyable |
| 41 | { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 42 | public: |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 43 | DEFINE_ERROR(Error, std::runtime_error); |
| 44 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 45 | explicit |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 46 | NdnsDaemon(const std::string& configFile, Face& face, Face& validatorFace) |
| 47 | : m_face(face) |
| 48 | , m_validatorFace(validatorFace) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 49 | { |
| 50 | try { |
| 51 | ConfigFile config; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 52 | NDNS_LOG_INFO("NnsnDaemon ConfigFile = " << configFile); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 53 | |
| 54 | config.addSectionHandler("zones", |
| 55 | bind(&NdnsDaemon::processZonesSection, this, _1, _3)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 56 | |
| 57 | config.parse(configFile, false); |
| 58 | |
| 59 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 60 | catch (const boost::filesystem::filesystem_error& e) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 61 | 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 Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 74 | 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 81 | BOOST_THROW_EXCEPTION(Error("zones section is empty")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 84 | std::string dbFile = DEFAULT_DATABASE_PATH "/" "ndns.db"; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 85 | ConfigSection::const_assoc_iterator item = section.find("dbFile"); |
| 86 | if (item != section.not_found()) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 87 | dbFile = item->second.get_value<std::string>(); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 88 | } |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 89 | NDNS_LOG_INFO("DbFile = " << dbFile); |
| 90 | m_dbMgr = unique_ptr<DbMgr>(new DbMgr(dbFile)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 91 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 92 | 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 Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 98 | m_validator = NdnsValidatorBuilder::create(m_validatorFace, 500, 0, validatorConfigFile); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 99 | |
| 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 109 | BOOST_THROW_EXCEPTION(Error("Required `name' attribute missing in `zone' section")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 110 | } |
| 111 | try { |
| 112 | cert = option.second.get<Name>("cert"); |
| 113 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 114 | catch (const std::exception&) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 115 | ; |
| 116 | } |
| 117 | |
| 118 | if (cert.empty()) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 119 | try { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 120 | cert = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain, Name(name).append(label::NDNS_ITERATIVE_QUERY)); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 121 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 122 | catch (const std::exception& e) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 123 | NDNS_LOG_FATAL("Identity: " << name << " does not have default certificate. " |
| 124 | << e.what()); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 125 | BOOST_THROW_EXCEPTION(Error("identity does not have default certificate")); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 126 | } |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 127 | } |
| 128 | else { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 129 | 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 Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | NDNS_LOG_TRACE("name = " << name << " cert = " << cert); |
| 136 | m_servers.push_back(make_shared<NameServer>(name, cert, m_face, *m_dbMgr, |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 137 | m_keyChain, *m_validator)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 138 | } |
| 139 | } // for |
| 140 | } |
| 141 | |
| 142 | private: |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 143 | Face& m_face; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 144 | Face& m_validatorFace; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 145 | unique_ptr<security::v2::Validator> m_validator; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 146 | unique_ptr<DbMgr> m_dbMgr; |
| 147 | std::vector<shared_ptr<NameServer>> m_servers; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 148 | KeyChain m_keyChain; |
| 149 | }; |
| 150 | |
| 151 | } // namespace ndns |
| 152 | } // namespace ndn |
| 153 | |
| 154 | int |
| 155 | main(int argc, char* argv[]) |
| 156 | { |
| 157 | using std::string; |
| 158 | using ndn::ndns::ConfigFile; |
| 159 | using namespace ndn::ndns; |
| 160 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 161 | string configFile = DEFAULT_CONFIG_PATH "/" "ndns.conf"; |
| 162 | |
| 163 | try { |
| 164 | namespace po = boost::program_options; |
| 165 | po::variables_map vm; |
| 166 | |
| 167 | po::options_description generic("Generic Options"); |
| 168 | generic.add_options()("help,h", "print help message"); |
| 169 | |
| 170 | po::options_description config("Configuration"); |
| 171 | config.add_options() |
| 172 | ("config,c", po::value<string>(&configFile), "set the path of configuration file") |
| 173 | ; |
| 174 | |
| 175 | po::options_description cmdline_options; |
| 176 | cmdline_options.add(generic).add(config); |
| 177 | |
| 178 | po::parsed_options parsed = |
| 179 | po::command_line_parser(argc, argv).options(cmdline_options).run(); |
| 180 | |
| 181 | po::store(parsed, vm); |
| 182 | po::notify(vm); |
| 183 | |
| 184 | if (vm.count("help")) { |
| 185 | std::cout << "Usage:\n" |
| 186 | << " ndns-daemon [-c configFile]\n" |
| 187 | << std::endl; |
| 188 | std::cout << generic << config << std::endl; |
| 189 | return 0; |
| 190 | } |
| 191 | } |
| 192 | catch (const std::exception& ex) { |
| 193 | std::cerr << "Parameter Error: " << ex.what() << std::endl; |
| 194 | |
| 195 | return 1; |
| 196 | } |
| 197 | catch (...) { |
| 198 | std::cerr << "Parameter Unknown error" << std::endl; |
| 199 | return 1; |
| 200 | } |
| 201 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 202 | boost::asio::io_service io; |
| 203 | ndn::Face face(io); |
| 204 | ndn::Face validatorFace(io); |
| 205 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 206 | try { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 207 | // NFD does not to forward Interests to the face it was received from. |
| 208 | // If the name server and its validator share same face, |
| 209 | // the validator cannot be forwarded to the name server itself |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 210 | // For current, two faces are used here. |
| 211 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 212 | // refs: https://redmine.named-data.net/issues/2206 |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 213 | // @TODO enhance validator to get the certificate from the local db if it has |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 214 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 215 | NdnsDaemon daemon(configFile, face, validatorFace); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 216 | face.processEvents(); |
| 217 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 218 | catch (const std::exception& e) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 219 | NDNS_LOG_FATAL("ERROR: " << e.what()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 220 | return 1; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | } |