Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
| 9 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 24 | |
| 25 | #include "command-validator.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 26 | #include "core/logger.hpp" |
| 27 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/util/io.hpp> |
| 29 | #include <ndn-cxx/security/identity-certificate.hpp> |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 30 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 31 | #include <boost/filesystem.hpp> |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 32 | #include <fstream> |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 33 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 34 | namespace nfd { |
| 35 | |
| 36 | NFD_LOG_INIT("CommandValidator"); |
| 37 | |
| 38 | CommandValidator::CommandValidator() |
| 39 | { |
| 40 | |
| 41 | } |
| 42 | |
| 43 | CommandValidator::~CommandValidator() |
| 44 | { |
| 45 | |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | CommandValidator::setConfigFile(ConfigFile& configFile) |
| 50 | { |
| 51 | configFile.addSectionHandler("authorizations", |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 52 | bind(&CommandValidator::onConfig, this, _1, _2, _3)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static inline void |
| 56 | aggregateErrors(std::stringstream& ss, const std::string& msg) |
| 57 | { |
| 58 | if (!ss.str().empty()) |
| 59 | { |
| 60 | ss << "\n"; |
| 61 | } |
| 62 | ss << msg; |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | CommandValidator::onConfig(const ConfigSection& section, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 67 | bool isDryRun, |
| 68 | const std::string& filename) |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 69 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 70 | using namespace boost::filesystem; |
| 71 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 72 | const ConfigSection EMPTY_SECTION; |
| 73 | |
| 74 | if (section.begin() == section.end()) |
| 75 | { |
| 76 | throw ConfigFile::Error("No authorize sections found"); |
| 77 | } |
| 78 | |
| 79 | std::stringstream dryRunErrors; |
| 80 | ConfigSection::const_iterator authIt; |
| 81 | for (authIt = section.begin(); authIt != section.end(); authIt++) |
| 82 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 83 | std::string certfile; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 84 | try |
| 85 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 86 | certfile = authIt->second.get<std::string>("certfile"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 87 | } |
| 88 | catch (const std::runtime_error& e) |
| 89 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 90 | std::string msg = "No certfile specified"; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 91 | if (!isDryRun) |
| 92 | { |
| 93 | throw ConfigFile::Error(msg); |
| 94 | } |
| 95 | aggregateErrors(dryRunErrors, msg); |
| 96 | continue; |
| 97 | } |
| 98 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 99 | shared_ptr<ndn::IdentityCertificate> id; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 100 | |
Yingdi Yu | c8f214c | 2014-04-29 20:39:37 -0700 | [diff] [blame^] | 101 | if (certfile != "any") |
| 102 | { |
| 103 | path certfilePath = absolute(certfile, path(filename).parent_path()); |
| 104 | NFD_LOG_DEBUG("generated certfile path: " << certfilePath.native()); |
| 105 | |
| 106 | std::ifstream in; |
| 107 | in.open(certfilePath.c_str()); |
| 108 | if (!in.is_open()) |
| 109 | { |
| 110 | std::string msg = "Unable to open certificate file " + certfilePath.native(); |
| 111 | if (!isDryRun) |
| 112 | { |
| 113 | throw ConfigFile::Error(msg); |
| 114 | } |
| 115 | aggregateErrors(dryRunErrors, msg); |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | try |
| 120 | { |
| 121 | id = ndn::io::load<ndn::IdentityCertificate>(in); |
| 122 | } |
| 123 | catch (const std::runtime_error& error) |
| 124 | { |
| 125 | // do nothing |
| 126 | } |
| 127 | |
| 128 | if (!static_cast<bool>(id)) { |
| 129 | std::string msg = "Malformed certificate file " + certfilePath.native(); |
| 130 | if (!isDryRun) |
| 131 | { |
| 132 | throw ConfigFile::Error(msg); |
| 133 | } |
| 134 | aggregateErrors(dryRunErrors, msg); |
| 135 | continue; |
Alexander Afanasyev | 5d2820d | 2014-04-24 00:10:31 -0700 | [diff] [blame] | 136 | } |
Alexander Afanasyev | 5d2820d | 2014-04-24 00:10:31 -0700 | [diff] [blame] | 137 | |
Yingdi Yu | c8f214c | 2014-04-29 20:39:37 -0700 | [diff] [blame^] | 138 | in.close(); |
| 139 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 140 | |
Yingdi Yu | c8f214c | 2014-04-29 20:39:37 -0700 | [diff] [blame^] | 141 | std::string keyNameForLogging; |
| 142 | if (static_cast<bool>(id)) |
| 143 | keyNameForLogging = id->getPublicKeyName().toUri(); |
| 144 | else |
| 145 | { |
| 146 | keyNameForLogging = "wildcard"; |
| 147 | NFD_LOG_WARN("Wildcard identity is intended for demo purpose only and " << |
| 148 | "SHOULD NOT be used in production environment"); |
| 149 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 150 | const ConfigSection* privileges = 0; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 151 | try |
| 152 | { |
| 153 | privileges = &authIt->second.get_child("privileges"); |
| 154 | } |
| 155 | catch (const std::runtime_error& error) |
| 156 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 157 | std::string msg = "No privileges section found for certificate file " + |
Yingdi Yu | c8f214c | 2014-04-29 20:39:37 -0700 | [diff] [blame^] | 158 | certfile + " (" + keyNameForLogging + ")"; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 159 | if (!isDryRun) |
| 160 | { |
| 161 | throw ConfigFile::Error(msg); |
| 162 | } |
| 163 | aggregateErrors(dryRunErrors, msg); |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | if (privileges->begin() == privileges->end()) |
| 168 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 169 | NFD_LOG_WARN("No privileges specified for certificate file " << certfile |
Yingdi Yu | c8f214c | 2014-04-29 20:39:37 -0700 | [diff] [blame^] | 170 | << " (" << keyNameForLogging << ")"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | ConfigSection::const_iterator privIt; |
| 174 | for (privIt = privileges->begin(); privIt != privileges->end(); privIt++) |
| 175 | { |
| 176 | const std::string& privilegeName = privIt->first; |
| 177 | if (m_supportedPrivileges.find(privilegeName) != m_supportedPrivileges.end()) |
| 178 | { |
| 179 | NFD_LOG_INFO("Giving privilege \"" << privilegeName |
Yingdi Yu | c8f214c | 2014-04-29 20:39:37 -0700 | [diff] [blame^] | 180 | << "\" to identity " << keyNameForLogging); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 181 | if (!isDryRun) |
| 182 | { |
| 183 | const std::string regex = "^<localhost><nfd><" + privilegeName + ">"; |
Yingdi Yu | c8f214c | 2014-04-29 20:39:37 -0700 | [diff] [blame^] | 184 | if (static_cast<bool>(id)) |
| 185 | m_validator.addInterestRule(regex, *id); |
| 186 | else |
| 187 | m_validator.addInterestBypassRule(regex); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | // Invalid configuration |
Yingdi Yu | c8f214c | 2014-04-29 20:39:37 -0700 | [diff] [blame^] | 193 | std::string msg = "Invalid privilege \"" + privilegeName + |
| 194 | "\" for certificate file " + certfile + " (" + keyNameForLogging + ")"; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 195 | if (!isDryRun) |
| 196 | { |
| 197 | throw ConfigFile::Error(msg); |
| 198 | } |
| 199 | aggregateErrors(dryRunErrors, msg); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if (!dryRunErrors.str().empty()) |
| 205 | { |
| 206 | throw ConfigFile::Error(dryRunErrors.str()); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | CommandValidator::addSupportedPrivilege(const std::string& privilege) |
| 212 | { |
| 213 | if (m_supportedPrivileges.find(privilege) != m_supportedPrivileges.end()) |
| 214 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 215 | throw CommandValidator::Error("Duplicated privilege: " + privilege); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 216 | } |
| 217 | m_supportedPrivileges.insert(privilege); |
| 218 | } |
| 219 | |
| 220 | } // namespace nfd |