1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include "iputils.hh"
#include "dns.hh"
#include "dnsname.hh"
#include "dnsparser.hh"
#include "logging.hh"
#include <map>
#include <unordered_map>
#include <limits>
/* This class implements a filtering policy that is able to fully implement RPZ, but is not bound to it.
In other words, it is generic enough to support RPZ, but could get its data from other places.
We know the following actions:
No action - just pass it on
Drop - drop a query, no response
NXDOMAIN - fake up an NXDOMAIN for the query
NODATA - just return no data for this qtype
Truncate - set TC bit
Modified - "we fake an answer for you"
These actions can be caused by the following triggers:
qname - the query name
client-ip - the IP address of the requestor
response-ip - an IP address in the response
ns-name - the name of a server used in the delegation
ns-ip - the IP address of a server used in the delegation
This means we get several hook points:
1) when the query comes in: qname & client-ip
2) during processing: ns-name & ns-ip
3) after processing: response-ip
Triggers meanwhile can apply to:
Verbatim domain names
Wildcard versions (*.domain.com does NOT match domain.com)
Netmasks (IPv4 and IPv6)
Finally, triggers are grouped in different zones. The "first" zone that has a match
is consulted. Then within that zone, rules again have precedences.
*/
class DNSFilterEngine
{
public:
enum class PolicyKind : uint8_t { NoAction, Drop, NXDOMAIN, NODATA, Truncate, Custom};
enum class PolicyType : uint8_t { None, QName, ClientIP, ResponseIP, NSDName, NSIP };
typedef uint16_t Priority;
static const Priority maximumPriority = std::numeric_limits<Priority>::max();
static std::string getKindToString(PolicyKind kind);
static std::string getTypeToString(PolicyType type);
struct PolicyZoneData
{
/* shared by all the policies from a single zone */
std::unordered_set<std::string> d_tags;
std::string d_name;
std::string d_extendedErrorExtra;
boost::optional<uint16_t> d_extendedErrorCode{boost::none};
Priority d_priority{maximumPriority};
bool d_policyOverridesGettag{true};
};
struct Policy
{
Policy(): d_ttl(0), d_kind(PolicyKind::NoAction), d_type(PolicyType::None)
{
}
Policy(PolicyKind kind, PolicyType type, int32_t ttl=0, std::shared_ptr<PolicyZoneData> data=nullptr, const std::vector<std::shared_ptr<DNSRecordContent>>& custom={}): d_custom(custom), d_zoneData(data), d_ttl(ttl), d_kind(kind), d_type(type)
{
}
bool operator==(const Policy& rhs) const
{
return d_kind == rhs.d_kind && d_type == rhs.d_type && d_ttl == rhs.d_ttl && d_custom == rhs.d_custom;
}
const std::string& getName() const
{
static const std::string notSet;
if (d_zoneData) {
return d_zoneData->d_name;
}
return notSet;
}
void setName(const std::string& name)
{
/* until now the PolicyZoneData was shared,
we now need to copy it, then write to it */
std::shared_ptr<PolicyZoneData> newZoneData;
if (d_zoneData) {
newZoneData = std::make_shared<PolicyZoneData>(*d_zoneData);
}
else {
newZoneData = std::make_shared<PolicyZoneData>();
}
newZoneData->d_name = name;
d_zoneData = newZoneData;
}
const std::unordered_set<std::string>& getTags() const
{
static const std::unordered_set<std::string> notSet;
if (d_zoneData) {
return d_zoneData->d_tags;
}
return notSet;
}
Priority getPriority() const
{
static Priority notSet = maximumPriority;
if (d_zoneData) {
return d_zoneData->d_priority;
}
return notSet;
}
bool policyOverridesGettag() const {
if (d_zoneData) {
return d_zoneData->d_policyOverridesGettag;
}
return true;
}
bool wasHit() const
{
return (d_type != DNSFilterEngine::PolicyType::None && d_kind != DNSFilterEngine::PolicyKind::NoAction);
}
std::string getLogString() const;
void info(Logr::Priority prio, const std::shared_ptr<Logr::Logger>& log) const;
std::vector<DNSRecord> getCustomRecords(const DNSName& qname, uint16_t qtype) const;
std::vector<DNSRecord> getRecords(const DNSName& qname) const;
std::vector<std::shared_ptr<DNSRecordContent>> d_custom;
std::shared_ptr<PolicyZoneData> d_zoneData{nullptr};
DNSName d_trigger;
string d_hit;
/* Yup, we are currently using the same TTL for every record for a given name */
int32_t d_ttl;
PolicyKind d_kind;
PolicyType d_type;
private:
DNSRecord getRecordFromCustom(const DNSName& qname, const std::shared_ptr<DNSRecordContent>& custom) const;
};
class Zone {
public:
Zone(): d_zoneData(std::make_shared<PolicyZoneData>())
{
}
void clear()
{
d_qpolAddr.clear();
d_postpolAddr.clear();
d_propolName.clear();
d_propolNSAddr.clear();
d_qpolName.clear();
}
void reserve(size_t entriesCount)
{
d_qpolName.reserve(entriesCount);
}
void setName(const std::string& name)
{
d_zoneData->d_name = name;
}
void setDomain(const DNSName& domain)
{
d_domain = domain;
}
void setSerial(uint32_t serial)
{
d_serial = serial;
}
void setRefresh(uint32_t refresh)
{
d_refresh = refresh;
}
void setTags(std::unordered_set<std::string>&& tags)
{
d_zoneData->d_tags = std::move(tags);
}
void setPolicyOverridesGettag(bool flag)
{
d_zoneData->d_policyOverridesGettag = flag;
}
void setExtendedErrorCode(uint16_t code)
{
d_zoneData->d_extendedErrorCode = code;
}
void setExtendedErrorExtra(const std::string& extra)
{
d_zoneData->d_extendedErrorExtra = extra;
}
const std::string& getName() const
{
return d_zoneData->d_name;
}
DNSName getDomain() const
{
return d_domain;
}
uint32_t getRefresh() const
{
return d_refresh;
}
uint32_t getSerial() const
{
return d_serial;
}
size_t size() const
{
return d_qpolAddr.size() + d_postpolAddr.size() + d_propolName.size() + d_propolNSAddr.size() + d_qpolName.size();
}
void dump(FILE * fp) const;
void addClientTrigger(const Netmask& nm, Policy&& pol, bool ignoreDuplicate = false);
void addQNameTrigger(const DNSName& nm, Policy&& pol, bool ignoreDuplicate = false);
void addNSTrigger(const DNSName& dn, Policy&& pol, bool ignoreDuplicate = false);
void addNSIPTrigger(const Netmask& nm, Policy&& pol, bool ignoreDuplicate = false);
void addResponseTrigger(const Netmask& nm, Policy&& pol, bool ignoreDuplicate = false);
bool rmClientTrigger(const Netmask& nm, const Policy& pol);
bool rmQNameTrigger(const DNSName& nm, const Policy& pol);
bool rmNSTrigger(const DNSName& dn, const Policy& pol);
bool rmNSIPTrigger(const Netmask& nm, const Policy& pol);
bool rmResponseTrigger(const Netmask& nm, const Policy& pol);
bool findExactQNamePolicy(const DNSName& qname, DNSFilterEngine::Policy& pol) const;
bool findExactNSPolicy(const DNSName& qname, DNSFilterEngine::Policy& pol) const;
bool findNSIPPolicy(const ComboAddress& addr, DNSFilterEngine::Policy& pol) const;
bool findResponsePolicy(const ComboAddress& addr, DNSFilterEngine::Policy& pol) const;
bool findClientPolicy(const ComboAddress& addr, DNSFilterEngine::Policy& pol) const;
bool hasClientPolicies() const
{
return !d_qpolAddr.empty();
}
bool hasQNamePolicies() const
{
return !d_qpolName.empty();
}
bool hasNSPolicies() const
{
return !d_propolName.empty();
}
bool hasNSIPPolicies() const
{
return !d_propolNSAddr.empty();
}
bool hasResponsePolicies() const
{
return !d_postpolAddr.empty();
}
Priority getPriority() const {
return d_zoneData->d_priority;
}
void setPriority(Priority p) {
d_zoneData->d_priority = p;
}
static DNSName maskToRPZ(const Netmask& nm);
private:
void addNameTrigger(std::unordered_map<DNSName,Policy>& map, const DNSName& n, Policy&& pol, bool ignoreDuplicate, PolicyType ptype);
void addNetmaskTrigger(NetmaskTree<Policy>& nmt, const Netmask& nm, Policy&& pol, bool ignoreDuplicate, PolicyType ptype);
bool rmNameTrigger(std::unordered_map<DNSName,Policy>& map, const DNSName& n, const Policy& pol);
bool rmNetmaskTrigger(NetmaskTree<Policy>& nmt, const Netmask& nm, const Policy& pol);
private:
static bool findExactNamedPolicy(const std::unordered_map<DNSName, DNSFilterEngine::Policy>& polmap, const DNSName& qname, DNSFilterEngine::Policy& pol);
static bool findNamedPolicy(const std::unordered_map<DNSName, DNSFilterEngine::Policy>& polmap, const DNSName& qname, DNSFilterEngine::Policy& pol);
static void dumpNamedPolicy(FILE* fp, const DNSName& name, const Policy& pol);
static void dumpAddrPolicy(FILE* fp, const Netmask& nm, const DNSName& name, const Policy& pol);
std::unordered_map<DNSName, Policy> d_qpolName; // QNAME trigger (RPZ)
NetmaskTree<Policy> d_qpolAddr; // Source address
std::unordered_map<DNSName, Policy> d_propolName; // NSDNAME (RPZ)
NetmaskTree<Policy> d_propolNSAddr; // NSIP (RPZ)
NetmaskTree<Policy> d_postpolAddr; // IP trigger (RPZ)
DNSName d_domain;
std::shared_ptr<PolicyZoneData> d_zoneData{nullptr};
uint32_t d_serial{0};
uint32_t d_refresh{0};
};
DNSFilterEngine();
void clear()
{
for(auto& z : d_zones) {
z->clear();
}
}
void clearZones()
{
d_zones.clear();
}
const std::shared_ptr<Zone> getZone(size_t zoneIdx) const
{
std::shared_ptr<Zone> result{nullptr};
if (zoneIdx < d_zones.size()) {
result = d_zones[zoneIdx];
}
return result;
}
const std::shared_ptr<Zone> getZone(const std::string& name) const
{
for (const auto& zone : d_zones) {
const auto& zName = zone->getName();
if (zName == name) {
return zone;
}
}
return nullptr;
}
size_t addZone(std::shared_ptr<Zone> newZone)
{
newZone->setPriority(d_zones.size());
d_zones.push_back(newZone);
return (d_zones.size() - 1);
}
void setZone(size_t zoneIdx, std::shared_ptr<Zone> newZone)
{
if (newZone) {
assureZones(zoneIdx);
newZone->setPriority(zoneIdx);
d_zones[zoneIdx] = newZone;
}
}
bool getQueryPolicy(const DNSName& qname, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& policy) const;
bool getClientPolicy(const ComboAddress& ca, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& policy) const;
bool getProcessingPolicy(const DNSName& qname, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& policy) const;
bool getProcessingPolicy(const ComboAddress& address, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& policy) const;
bool getPostPolicy(const vector<DNSRecord>& records, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& policy) const;
bool getPostPolicy(const DNSRecord& record, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& policy) const;
// A few convenience methods for the unit test code
Policy getQueryPolicy(const DNSName& qname, const std::unordered_map<std::string,bool>& discardedPolicies, Priority p) const {
Policy policy;
policy.d_zoneData = std::make_shared<PolicyZoneData>();
policy.d_zoneData->d_priority = p;
getQueryPolicy(qname, discardedPolicies, policy);
return policy;
}
Policy getClientPolicy(const ComboAddress& ca, const std::unordered_map<std::string,bool>& discardedPolicies, Priority p) const {
Policy policy;
policy.d_zoneData = std::make_shared<PolicyZoneData>();
policy.d_zoneData->d_priority = p;
getClientPolicy(ca, discardedPolicies, policy);
return policy;
}
Policy getProcessingPolicy(const DNSName& qname, const std::unordered_map<std::string,bool>& discardedPolicies, Priority p) const {
Policy policy;
policy.d_zoneData = std::make_shared<PolicyZoneData>();
policy.d_zoneData->d_priority = p;
getProcessingPolicy(qname, discardedPolicies, policy);
return policy;
}
Policy getProcessingPolicy(const ComboAddress& address, const std::unordered_map<std::string,bool>& discardedPolicies, Priority p) const {
Policy policy;
policy.d_zoneData = std::make_shared<PolicyZoneData>();
policy.d_zoneData->d_priority = p;
getProcessingPolicy(address, discardedPolicies, policy);
return policy;
}
Policy getPostPolicy(const vector<DNSRecord>& records, const std::unordered_map<std::string,bool>& discardedPolicies, Priority p) const {
Policy policy;
policy.d_zoneData = std::make_shared<PolicyZoneData>();
policy.d_zoneData->d_priority = p;
getPostPolicy(records, discardedPolicies, policy);
return policy;
}
size_t size() const {
return d_zones.size();
}
private:
void assureZones(size_t zone);
vector<std::shared_ptr<Zone>> d_zones;
};
void mergePolicyTags(std::unordered_set<std::string>& tags, const std::unordered_set<std::string>& newTags);
|