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 | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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 | |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 20 | #include "logger.hpp" |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 21 | #include "daemon/config-file.hpp" |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 22 | #include "daemon/name-server.hpp" |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 23 | #include "util/cert-helper.hpp" |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 24 | #include "util/util.hpp" |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 25 | |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 26 | #include <ndn-cxx/face.hpp> |
| 27 | #include <ndn-cxx/security/key-chain.hpp> |
| 28 | |
| 29 | #include <boost/asio/io_service.hpp> |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 30 | #include <boost/program_options.hpp> |
| 31 | |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 32 | #include <iostream> |
| 33 | |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 34 | NDNS_LOG_INIT(NdnsDaemon); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 35 | |
| 36 | namespace ndn { |
| 37 | namespace ndns { |
| 38 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 39 | /** |
| 40 | * @brief Name Server Daemon |
| 41 | * @note NdnsDaemon allows multiple name servers hosted by the same daemon, and they |
| 42 | * share same KeyChain, DbMgr, Validator and Face |
| 43 | */ |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 44 | class NdnsDaemon : boost::noncopyable |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 45 | { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 46 | public: |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 47 | DEFINE_ERROR(Error, std::runtime_error); |
| 48 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 49 | NdnsDaemon(const std::string& configFile, Face& face, Face& validatorFace) |
| 50 | : m_face(face) |
| 51 | , m_validatorFace(validatorFace) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 52 | { |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 53 | NDNS_LOG_INFO("ConfigFile = " << configFile); |
| 54 | ConfigFile config; |
| 55 | config.addSectionHandler("zones", bind(&NdnsDaemon::processZonesSection, this, _1)); |
| 56 | config.parse(configFile, false); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 60 | processZonesSection(const ndn::ndns::ConfigSection& section) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 61 | { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 62 | if (section.begin() == section.end()) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 63 | NDN_THROW(Error("zones section is empty")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 66 | std::string dbFile = getDefaultDatabaseFile(); |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 67 | auto item = section.find("dbFile"); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 68 | if (item != section.not_found()) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 69 | dbFile = item->second.get_value<std::string>(); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 70 | } |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 71 | NDNS_LOG_INFO("DbFile = " << dbFile); |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 72 | m_dbMgr = make_unique<DbMgr>(dbFile); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 73 | |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 74 | std::string validatorConfigFile(NDNS_CONFDIR "/validator.conf"); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 75 | item = section.find("validatorConfigFile"); |
| 76 | if (item != section.not_found()) { |
| 77 | validatorConfigFile = item->second.get_value<std::string>(); |
| 78 | } |
| 79 | NDNS_LOG_INFO("ValidatorConfigFile = " << validatorConfigFile); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 80 | m_validator = NdnsValidatorBuilder::create(m_validatorFace, 500, 0, validatorConfigFile); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 81 | |
| 82 | for (const auto& option : section) { |
| 83 | Name name; |
| 84 | Name cert; |
| 85 | if (option.first == "zone") { |
| 86 | try { |
| 87 | name = option.second.get<Name>("name"); // exception leads to exit |
| 88 | } |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 89 | catch (const std::exception&) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 90 | NDNS_LOG_ERROR("Required `name' attribute missing in `zone' section"); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 91 | NDN_THROW(Error("Required `name' attribute missing in `zone' section")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 92 | } |
| 93 | try { |
| 94 | cert = option.second.get<Name>("cert"); |
| 95 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 96 | catch (const std::exception&) { |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 97 | // ignore |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | if (cert.empty()) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 101 | try { |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 102 | cert = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain, |
| 103 | Name(name) |
| 104 | .append(label::NDNS_ITERATIVE_QUERY)); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 105 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 106 | catch (const std::exception& e) { |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 107 | NDNS_LOG_ERROR("Identity " << name << " does not have a default certificate: " << e.what()); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 108 | NDN_THROW(Error("identity does not have default certificate")); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 109 | } |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 110 | } |
| 111 | else { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 112 | try { |
| 113 | CertHelper::getCertificate(m_keyChain, name, cert); |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 114 | } catch (const std::exception&) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 115 | NDN_THROW(Error("Certificate `" + cert.toUri() + "` does not exist in the KeyChain")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | NDNS_LOG_TRACE("name = " << name << " cert = " << cert); |
| 119 | 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] | 120 | m_keyChain, *m_validator)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 121 | } |
| 122 | } // for |
| 123 | } |
| 124 | |
| 125 | private: |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 126 | Face& m_face; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 127 | Face& m_validatorFace; |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 128 | unique_ptr<security::Validator> m_validator; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 129 | unique_ptr<DbMgr> m_dbMgr; |
| 130 | std::vector<shared_ptr<NameServer>> m_servers; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 131 | KeyChain m_keyChain; |
| 132 | }; |
| 133 | |
| 134 | } // namespace ndns |
| 135 | } // namespace ndn |
| 136 | |
| 137 | int |
| 138 | main(int argc, char* argv[]) |
| 139 | { |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 140 | std::string configFile(NDNS_CONFDIR "/ndns.conf"); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 141 | |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 142 | namespace po = boost::program_options; |
| 143 | po::options_description optsDesc("Options"); |
| 144 | optsDesc.add_options() |
| 145 | ("help,h", "print this help message and exit") |
| 146 | ("config-file,c", po::value<std::string>(&configFile)->default_value(configFile), |
| 147 | "path to configuration file") |
| 148 | ; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 149 | |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 150 | po::variables_map vm; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 151 | try { |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 152 | po::store(po::parse_command_line(argc, argv, optsDesc), vm); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 153 | po::notify(vm); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 154 | } |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 155 | catch (const po::error& e) { |
| 156 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 157 | return 2; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 158 | } |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 159 | catch (const boost::bad_any_cast& e) { |
| 160 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 161 | return 2; |
| 162 | } |
| 163 | |
| 164 | if (vm.count("help") != 0) { |
| 165 | std::cout << "Usage: " << argv[0] << " [options]\n" |
| 166 | << "\n" |
| 167 | << optsDesc; |
| 168 | return 0; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 171 | boost::asio::io_service io; |
| 172 | ndn::Face face(io); |
| 173 | ndn::Face validatorFace(io); |
| 174 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 175 | try { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 176 | // NFD does not to forward Interests to the face it was received from. |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 177 | // If the name server and its validator share the same face, |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 178 | // the validator cannot be forwarded to the name server itself |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 179 | // For now, two faces are used here. |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 180 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 181 | // refs: https://redmine.named-data.net/issues/2206 |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 182 | // @TODO enhance validator to get the certificate from the local db if present |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 183 | |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 184 | ndn::ndns::NdnsDaemon daemon(configFile, face, validatorFace); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 185 | face.processEvents(); |
| 186 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 187 | catch (const std::exception& e) { |
Davide Pesavento | 7debce9 | 2019-01-20 16:12:29 -0500 | [diff] [blame] | 188 | NDNS_LOG_FATAL(e.what()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 189 | return 1; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |