Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 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" |
| 25 | #include <boost/program_options.hpp> |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndns { |
| 29 | |
| 30 | NDNS_LOG_INIT("NdnsDaemon"); |
| 31 | |
| 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 | */ |
| 37 | class NdnsDaemon : noncopyable |
| 38 | { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 39 | public: |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 40 | DEFINE_ERROR(Error, std::runtime_error); |
| 41 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 42 | explicit |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 43 | NdnsDaemon(const std::string& configFile, Face& face, Face& validatorFace) |
| 44 | : m_face(face) |
| 45 | , m_validatorFace(validatorFace) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 46 | { |
| 47 | try { |
| 48 | ConfigFile config; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 49 | NDNS_LOG_INFO("NnsnDaemon ConfigFile = " << configFile); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 50 | |
| 51 | config.addSectionHandler("zones", |
| 52 | bind(&NdnsDaemon::processZonesSection, this, _1, _3)); |
| 53 | config.addSectionHandler("hints", |
| 54 | bind(&NdnsDaemon::processHintsSection, this, _1, _3)); |
| 55 | |
| 56 | config.parse(configFile, false); |
| 57 | |
| 58 | } |
| 59 | catch (boost::filesystem::filesystem_error& e) { |
| 60 | if (e.code() == boost::system::errc::permission_denied) { |
| 61 | NDNS_LOG_FATAL("Permissions denied for " << e.path1()); |
| 62 | } |
| 63 | else { |
| 64 | NDNS_LOG_FATAL(e.what()); |
| 65 | } |
| 66 | } |
| 67 | catch (const std::exception& e) { |
| 68 | NDNS_LOG_FATAL(e.what()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | processHintsSection(const ndn::ndns::ConfigSection& section, const std::string& filename) |
| 74 | { |
| 75 | // hint is not supported yet |
| 76 | ; |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | processZonesSection(const ndn::ndns::ConfigSection& section, const std::string& filename) |
| 81 | { |
| 82 | using namespace boost::filesystem; |
| 83 | using namespace ndn::ndns; |
| 84 | using ndn::ndns::ConfigSection; |
| 85 | |
| 86 | if (section.begin() == section.end()) { |
| 87 | throw Error("zones section is empty"); |
| 88 | } |
| 89 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 90 | std::string dbFile = DEFAULT_DATABASE_PATH "/" "ndns.db"; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 91 | ConfigSection::const_assoc_iterator item = section.find("dbFile"); |
| 92 | if (item != section.not_found()) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 93 | dbFile = item->second.get_value<std::string>(); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 94 | } |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 95 | NDNS_LOG_INFO("DbFile = " << dbFile); |
| 96 | m_dbMgr = unique_ptr<DbMgr>(new DbMgr(dbFile)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 97 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 98 | std::string validatorConfigFile = DEFAULT_CONFIG_PATH "/" "validator.conf"; |
| 99 | item = section.find("validatorConfigFile"); |
| 100 | if (item != section.not_found()) { |
| 101 | validatorConfigFile = item->second.get_value<std::string>(); |
| 102 | } |
| 103 | NDNS_LOG_INFO("ValidatorConfigFile = " << validatorConfigFile); |
| 104 | m_validator = unique_ptr<Validator>(new Validator(m_validatorFace, validatorConfigFile)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 105 | |
| 106 | for (const auto& option : section) { |
| 107 | Name name; |
| 108 | Name cert; |
| 109 | if (option.first == "zone") { |
| 110 | try { |
| 111 | name = option.second.get<Name>("name"); // exception leads to exit |
| 112 | } |
| 113 | catch (const std::exception& e) { |
| 114 | NDNS_LOG_ERROR("Required `name' attribute missing in `zone' section"); |
| 115 | throw Error("Required `name' attribute missing in `zone' section"); |
| 116 | } |
| 117 | try { |
| 118 | cert = option.second.get<Name>("cert"); |
| 119 | } |
| 120 | catch (std::exception&) { |
| 121 | ; |
| 122 | } |
| 123 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 124 | |
| 125 | if (!m_keyChain.doesIdentityExist(name)) { |
| 126 | NDNS_LOG_FATAL("Identity: " << name << " does not exist in the KeyChain"); |
| 127 | throw Error("Identity does not exist in the KeyChain"); |
| 128 | } |
| 129 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 130 | if (cert.empty()) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 131 | try { |
| 132 | cert = m_keyChain.getDefaultCertificateNameForIdentity(name); |
| 133 | } |
| 134 | catch (std::exception& e) { |
| 135 | NDNS_LOG_FATAL("Identity: " << name << " does not have default certificate. " |
| 136 | << e.what()); |
| 137 | throw Error("identity does not have default certificate"); |
| 138 | } |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 139 | } |
| 140 | else { |
| 141 | if (!m_keyChain.doesCertificateExist(cert)) { |
| 142 | throw Error("Certificate `" + cert.toUri() + "` does not exist in the KeyChain"); |
| 143 | } |
| 144 | } |
| 145 | NDNS_LOG_TRACE("name = " << name << " cert = " << cert); |
| 146 | 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] | 147 | m_keyChain, *m_validator)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 148 | } |
| 149 | } // for |
| 150 | } |
| 151 | |
| 152 | private: |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 153 | Face& m_face; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 154 | Face& m_validatorFace; |
| 155 | unique_ptr<Validator> m_validator; |
| 156 | unique_ptr<DbMgr> m_dbMgr; |
| 157 | std::vector<shared_ptr<NameServer>> m_servers; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 158 | KeyChain m_keyChain; |
| 159 | }; |
| 160 | |
| 161 | } // namespace ndns |
| 162 | } // namespace ndn |
| 163 | |
| 164 | int |
| 165 | main(int argc, char* argv[]) |
| 166 | { |
| 167 | using std::string; |
| 168 | using ndn::ndns::ConfigFile; |
| 169 | using namespace ndn::ndns; |
| 170 | |
| 171 | ndn::ndns::log::init(); |
| 172 | string configFile = DEFAULT_CONFIG_PATH "/" "ndns.conf"; |
| 173 | |
| 174 | try { |
| 175 | namespace po = boost::program_options; |
| 176 | po::variables_map vm; |
| 177 | |
| 178 | po::options_description generic("Generic Options"); |
| 179 | generic.add_options()("help,h", "print help message"); |
| 180 | |
| 181 | po::options_description config("Configuration"); |
| 182 | config.add_options() |
| 183 | ("config,c", po::value<string>(&configFile), "set the path of configuration file") |
| 184 | ; |
| 185 | |
| 186 | po::options_description cmdline_options; |
| 187 | cmdline_options.add(generic).add(config); |
| 188 | |
| 189 | po::parsed_options parsed = |
| 190 | po::command_line_parser(argc, argv).options(cmdline_options).run(); |
| 191 | |
| 192 | po::store(parsed, vm); |
| 193 | po::notify(vm); |
| 194 | |
| 195 | if (vm.count("help")) { |
| 196 | std::cout << "Usage:\n" |
| 197 | << " ndns-daemon [-c configFile]\n" |
| 198 | << std::endl; |
| 199 | std::cout << generic << config << std::endl; |
| 200 | return 0; |
| 201 | } |
| 202 | } |
| 203 | catch (const std::exception& ex) { |
| 204 | std::cerr << "Parameter Error: " << ex.what() << std::endl; |
| 205 | |
| 206 | return 1; |
| 207 | } |
| 208 | catch (...) { |
| 209 | std::cerr << "Parameter Unknown error" << std::endl; |
| 210 | return 1; |
| 211 | } |
| 212 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 213 | boost::asio::io_service io; |
| 214 | ndn::Face face(io); |
| 215 | ndn::Face validatorFace(io); |
| 216 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 217 | try { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 218 | // NFD does not to forward Interests to the face it was received from. |
| 219 | // If the name server and its validator share same face, |
| 220 | // the validator cannot be forwarded to the name server itself |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 221 | // For current, two faces are used here. |
| 222 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 223 | // refs: http://redmine.named-data.net/issues/2206 |
| 224 | // @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] | 225 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 226 | NdnsDaemon daemon(configFile, face, validatorFace); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 227 | face.processEvents(); |
| 228 | } |
| 229 | catch (std::exception& e) { |
| 230 | NDNS_LOG_FATAL("ERROR: " << e.what()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 231 | return 1; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |