29 #include <ndn-cxx/security/certificate-fetcher-offline.hpp>
30 #include <ndn-cxx/security/certificate-request.hpp>
31 #include <ndn-cxx/security/validation-policy.hpp>
32 #include <ndn-cxx/security/validation-policy-accept-all.hpp>
33 #include <ndn-cxx/security/validation-policy-command-interest.hpp>
34 #include <ndn-cxx/tag.hpp>
35 #include <ndn-cxx/util/io.hpp>
37 #include <boost/filesystem/operations.hpp>
38 #include <boost/filesystem/path.hpp>
40 namespace security = ndn::security;
56 static std::optional<std::string>
59 auto signerTag = interest.getTag<
SignerTag>();
60 if (signerTag ==
nullptr) {
64 return signerTag->get().toUri();
71 class CommandAuthenticatorValidationPolicy final :
public security::ValidationPolicy
75 checkPolicy(
const Interest& interest,
const shared_ptr<security::ValidationState>& state,
76 const ValidationContinuation& continueValidation)
final
78 auto sigInfo = getSignatureInfo(interest, *state);
79 if (!state->getOutcome()) {
82 Name klName = getKeyLocatorName(sigInfo, *state);
83 if (!state->getOutcome()) {
90 auto state1 = std::dynamic_pointer_cast<security::InterestValidationState>(state);
91 state1->getOriginalInterest().setTag(make_shared<SignerTag>(klName));
93 continueValidation(make_shared<security::CertificateRequest>(klName), state);
97 checkPolicy(
const Data&,
const shared_ptr<security::ValidationState>&,
98 const ValidationContinuation&)
final
102 BOOST_ASSERT_MSG(
false,
"Data should not be passed to this policy");
106 shared_ptr<CommandAuthenticator>
112 CommandAuthenticator::CommandAuthenticator() =
default;
118 processConfig(std::forward<decltype(args)>(args)...);
123 CommandAuthenticator::processConfig(
const ConfigSection& section,
bool isDryRun,
const std::string& filename)
127 for (
auto& kv : m_validators) {
128 kv.second = make_shared<security::Validator>(
129 make_unique<security::ValidationPolicyCommandInterest>(make_unique<CommandAuthenticatorValidationPolicy>()),
130 make_unique<security::CertificateFetcherOffline>());
134 if (section.empty()) {
135 NDN_THROW(ConfigFile::Error(
"'authorize' is missing under 'authorizations'"));
138 int authSectionIndex = 0;
139 for (
const auto& [sectionName, authSection] : section) {
140 if (sectionName !=
"authorize") {
141 NDN_THROW(ConfigFile::Error(
"'" + sectionName +
"' section is not permitted under 'authorizations'"));
144 std::string certfile;
146 certfile = authSection.get<std::string>(
"certfile");
148 catch (
const boost::property_tree::ptree_error&) {
149 NDN_THROW(ConfigFile::Error(
"'certfile' is missing under authorize[" +
150 std::to_string(authSectionIndex) +
"]"));
154 shared_ptr<security::Certificate> cert;
155 if (certfile ==
"any") {
157 NFD_LOG_WARN(
"'certfile any' is intended for demo purposes only and "
158 "SHOULD NOT be used in production environments");
161 using namespace boost::filesystem;
162 path certfilePath = absolute(certfile, path(filename).parent_path());
163 cert = ndn::io::load<security::Certificate>(certfilePath.string());
164 if (cert ==
nullptr) {
165 NDN_THROW(ConfigFile::Error(
"cannot load certfile " + certfilePath.string() +
166 " for authorize[" + std::to_string(authSectionIndex) +
"]"));
172 privSection = &authSection.get_child(
"privileges");
174 catch (
const boost::property_tree::ptree_error&) {
175 NDN_THROW(ConfigFile::Error(
"'privileges' is missing under authorize[" +
176 std::to_string(authSectionIndex) +
"]"));
179 if (privSection->empty()) {
180 NFD_LOG_WARN(
"No privileges granted to certificate " << certfile);
182 for (
const auto& kv : *privSection) {
183 const std::string& module = kv.first;
184 auto found = m_validators.find(module);
185 if (found == m_validators.end()) {
186 NDN_THROW(ConfigFile::Error(
"unknown module '" + module +
187 "' under authorize[" + std::to_string(authSectionIndex) +
"]"));
195 found->second = make_shared<security::Validator>(make_unique<security::ValidationPolicyAcceptAll>(),
196 make_unique<security::CertificateFetcherOffline>());
197 NFD_LOG_INFO(
"authorize module=" << module <<
" signer=any");
200 const Name& keyName = cert->getKeyName();
201 security::Certificate certCopy = *cert;
202 found->second->loadAnchor(certfile, std::move(certCopy));
203 NFD_LOG_INFO(
"authorize module=" << module <<
" signer=" << keyName <<
" certfile=" << certfile);
211 ndn::mgmt::Authorization
214 m_validators[module];
216 return [module,
self = shared_from_this()] (
const Name&,
const Interest& interest,
217 const ndn::mgmt::ControlParameters*,
218 const ndn::mgmt::AcceptContinuation& accept,
219 const ndn::mgmt::RejectContinuation& reject) {
220 auto validator =
self->m_validators.at(module);
222 auto successCb = [accept, validator] (
const Interest& interest1) {
224 BOOST_ASSERT(signer1 ||
225 dynamic_cast<security::ValidationPolicyAcceptAll*
>(&validator->getPolicy()) !=
nullptr);
226 std::string signer = signer1.value_or(
"*");
227 NFD_LOG_DEBUG(
"accept " << interest1.getName() <<
" signer=" << signer);
231 using ndn::security::ValidationError;
232 auto failureCb = [reject] (
const Interest& interest1,
const ValidationError& err) {
233 auto reply = ndn::mgmt::RejectReply::STATUS403;
234 if (err.getCode() == ValidationError::MALFORMED_SIGNATURE ||
235 err.getCode() == ValidationError::INVALID_KEY_LOCATOR) {
237 reply = ndn::mgmt::RejectReply::SILENT;
239 NFD_LOG_DEBUG(
"reject " << interest1.getName() <<
" signer=" <<
245 validator->validate(interest, successCb, failureCb);
248 NFD_LOG_DEBUG(
"reject " << interest.getName() <<
" signer=" <<
250 reject(ndn::mgmt::RejectReply::STATUS403);
Provides ControlCommand authorization according to NFD's configuration file.
ndn::mgmt::Authorization makeAuthorization(const std::string &module, const std::string &verb)
Returns an Authorization function for module/verb command.
void setConfigFile(ConfigFile &configFile)
static shared_ptr< CommandAuthenticator > create()
Configuration file parsing utility.
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
Setup notification of configuration file sections.
#define NFD_LOG_INIT(name)
ndn::SimpleTag< Name, 20 > SignerTag
An Interest tag to store the command signer.
boost::property_tree::ptree ConfigSection
A configuration file section.
static std::optional< std::string > getSignerFromTag(const Interest &interest)
Obtain signer from a SignerTag attached to interest, if available.