blob: 93b8df4d5f16e5f63ac74db70e9576326d13b643 [file] [log] [blame]
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "rule.hpp"
23#include "util/logger.hpp"
24
25#include <boost/algorithm/string/predicate.hpp>
26
27NDN_LOG_INIT(ndn.security.validator_config.Rule);
28
29namespace ndn {
30namespace security {
31namespace v2 {
32namespace validator_config {
33
34Rule::Rule(const std::string& id, uint32_t pktType)
35 : m_id(id)
36 , m_pktType(pktType)
37{
38}
39
40void
41Rule::addFilter(unique_ptr<Filter> filter)
42{
43 m_filters.push_back(std::move(filter));
44}
45
46void
47Rule::addChecker(unique_ptr<Checker> checker)
48{
49 m_checkers.push_back(std::move(checker));
50}
51
52bool
53Rule::match(uint32_t pktType, const Name& pktName) const
54{
55 NDN_LOG_TRACE("Trying to match " << pktName);
56 if (pktType != m_pktType) {
57 BOOST_THROW_EXCEPTION(Error("Invalid packet type supplied (" +
58 to_string(pktType) + " != " + to_string(m_pktType) + ")"));
59 }
60
61 if (m_filters.empty()) {
62 return true;
63 }
64
65 bool retval = false;
66 for (const auto& filter : m_filters) {
67 retval |= filter->match(pktType, pktName);
68 if (retval) {
69 break;
70 }
71 }
72 return retval;
73}
74
75bool
76Rule::check(uint32_t pktType, const Name& pktName, const Name& klName, const shared_ptr<ValidationState>& state) const
77{
78 NDN_LOG_TRACE("Trying to check " << pktName << " with keyLocator " << klName);
79
80 if (pktType != m_pktType) {
81 BOOST_THROW_EXCEPTION(Error("Invalid packet type supplied (" +
82 to_string(pktType) + " != " + to_string(m_pktType) + ")"));
83 }
84
85 bool hasPendingResult = false;
86 for (const auto& checker : m_checkers) {
87 bool result = checker->check(pktType, pktName, klName, state);
88 if (!result) {
89 return result;
90 }
91 hasPendingResult = true;
92 }
93
94 return hasPendingResult;
95}
96
97unique_ptr<Rule>
98Rule::create(const ConfigSection& configSection, const std::string& configFilename)
99{
100 auto propertyIt = configSection.begin();
101
102 // Get rule.id
103 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "id")) {
104 BOOST_THROW_EXCEPTION(Error("Expecting <rule.id>"));
105 }
106
107 std::string ruleId = propertyIt->second.data();
108 propertyIt++;
109
110 // Get rule.for
111 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "for")) {
112 BOOST_THROW_EXCEPTION(Error("Expecting <rule.for> in rule: " + ruleId));
113 }
114
115 std::string usage = propertyIt->second.data();
116 propertyIt++;
117
118 bool isForData = false;
119 if (boost::iequals(usage, "data")) {
120 isForData = true;
121 }
122 else if (boost::iequals(usage, "interest")) {
123 isForData = false;
124 }
125 else {
126 BOOST_THROW_EXCEPTION(Error("Unrecognized <rule.for>: " + usage + " in rule: " + ruleId));
127 }
128
129 auto rule = make_unique<Rule>(ruleId, isForData ? tlv::Data : tlv::Interest);
130
131 // Get rule.filter(s)
132 for (; propertyIt != configSection.end(); propertyIt++) {
133 if (!boost::iequals(propertyIt->first, "filter")) {
134 if (boost::iequals(propertyIt->first, "checker")) {
135 break;
136 }
137 BOOST_THROW_EXCEPTION(Error("Expecting <rule.filter> in rule: " + ruleId));
138 }
139
140 rule->addFilter(Filter::create(propertyIt->second, configFilename));
141 }
142
143 // Get rule.checker(s)
144 bool hasCheckers = false;
145 for (; propertyIt != configSection.end(); propertyIt++) {
146 if (!boost::iequals(propertyIt->first, "checker")) {
147 BOOST_THROW_EXCEPTION(Error("Expecting <rule.checker> in rule: " + ruleId));
148 }
149
150 rule->addChecker(Checker::create(propertyIt->second, configFilename));
151 hasCheckers = true;
152 }
153
154 // Check other stuff
155 if (propertyIt != configSection.end()) {
156 BOOST_THROW_EXCEPTION(Error("Expecting the end of rule: " + ruleId));
157 }
158
159 if (!hasCheckers) {
160 BOOST_THROW_EXCEPTION(Error("No <rule.checker> is specified in rule: " + ruleId));
161 }
162
163 return rule;
164}
165
166} // namespace validator_config
167} // namespace v2
168} // namespace security
169} // namespace ndn