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