38 #include <boost/filesystem.hpp>
54 static optional<std::string>
58 if (signerTag ==
nullptr) {
62 return signerTag->
get().toUri();
76 if (!state->getOutcome()) {
83 auto state1 = dynamic_pointer_cast<sec2::InterestValidationState>(state);
84 state1->getOriginalInterest().setTag(make_shared<SignerTag>(klName));
86 continueValidation(make_shared<sec2::CertificateRequest>(
Interest(klName)), state);
95 BOOST_ASSERT_MSG(
false,
"Data should not be passed to this policy");
99 shared_ptr<CommandAuthenticator>
105 CommandAuthenticator::CommandAuthenticator() =
default;
111 bind(&CommandAuthenticator::processConfig,
this, _1, _2, _3));
115 CommandAuthenticator::processConfig(
const ConfigSection& section,
bool isDryRun,
const std::string& filename)
119 for (
auto& kv : m_validators) {
120 kv.second = make_shared<sec2::Validator>(
121 make_unique<sec2::ValidationPolicyCommandInterest>(make_unique<CommandAuthenticatorValidationPolicy>()),
122 make_unique<sec2::CertificateFetcherOffline>());
126 if (section.empty()) {
127 NDN_THROW(ConfigFile::Error(
"'authorize' is missing under 'authorizations'"));
130 int authSectionIndex = 0;
131 for (
const auto& kv : section) {
132 if (kv.first !=
"authorize") {
133 NDN_THROW(ConfigFile::Error(
"'" + kv.first +
"' section is not permitted under 'authorizations'"));
137 std::string certfile;
139 certfile = authSection.get<std::string>(
"certfile");
141 catch (
const boost::property_tree::ptree_error&) {
142 NDN_THROW(ConfigFile::Error(
"'certfile' is missing under authorize[" +
147 shared_ptr<sec2::Certificate> cert;
148 if (certfile ==
"any") {
150 NFD_LOG_WARN(
"'certfile any' is intended for demo purposes only and "
151 "SHOULD NOT be used in production environments");
154 using namespace boost::filesystem;
155 path certfilePath = absolute(certfile, path(filename).parent_path());
156 cert = ndn::io::load<sec2::Certificate>(certfilePath.string());
157 if (cert ==
nullptr) {
158 NDN_THROW(ConfigFile::Error(
"cannot load certfile " + certfilePath.string() +
159 " for authorize[" +
to_string(authSectionIndex) +
"]"));
165 privSection = &authSection.get_child(
"privileges");
167 catch (
const boost::property_tree::ptree_error&) {
168 NDN_THROW(ConfigFile::Error(
"'privileges' is missing under authorize[" +
172 if (privSection->empty()) {
173 NFD_LOG_WARN(
"No privileges granted to certificate " << certfile);
175 for (
const auto& kv : *privSection) {
176 const std::string& module = kv.first;
177 auto found = m_validators.find(module);
178 if (found == m_validators.end()) {
179 NDN_THROW(ConfigFile::Error(
"unknown module '" + module +
180 "' under authorize[" +
to_string(authSectionIndex) +
"]"));
188 found->second = make_shared<sec2::Validator>(make_unique<sec2::ValidationPolicyAcceptAll>(),
189 make_unique<sec2::CertificateFetcherOffline>());
190 NFD_LOG_INFO(
"authorize module=" << module <<
" signer=any");
193 const Name& keyName = cert->getKeyName();
195 found->second->loadAnchor(certfile,
std::move(certCopy));
196 NFD_LOG_INFO(
"authorize module=" << module <<
" signer=" << keyName <<
" certfile=" << certfile);
207 m_validators[module];
209 auto self = this->shared_from_this();
214 auto validator =
self->m_validators.at(module);
215 auto successCb = [accept, validator] (
const Interest& interest1) {
217 BOOST_ASSERT(signer1 ||
219 std::string signer = signer1.value_or(
"*");
220 NFD_LOG_DEBUG(
"accept " << interest1.getName() <<
" signer=" << signer);
226 switch (err.getCode()) {
229 reply = RejectReply::SILENT;
233 reply = RejectReply::SILENT;
243 validator->validate(interest, successCb, failureCb);
246 NFD_LOG_DEBUG(
"reject " << interest.getName() <<
" signer=" <<