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
|
/*
* 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.
*/
#include <cinttypes>
#include <iostream>
#include "filterpo.hh"
#include "namespaces.hh"
#include "dnsrecords.hh"
DNSFilterEngine::DNSFilterEngine()
{
}
bool DNSFilterEngine::Zone::findQNamePolicy(const DNSName& qname, DNSFilterEngine::Policy& pol) const
{
return findNamedPolicy(d_qpolName, qname, pol);
}
bool DNSFilterEngine::Zone::findNSPolicy(const DNSName& qname, DNSFilterEngine::Policy& pol) const
{
return findNamedPolicy(d_propolName, qname, pol);
}
bool DNSFilterEngine::Zone::findNSIPPolicy(const ComboAddress& addr, DNSFilterEngine::Policy& pol) const
{
if (const auto fnd = d_propolNSAddr.lookup(addr)) {
pol = fnd->second;
return true;
}
return false;
}
bool DNSFilterEngine::Zone::findResponsePolicy(const ComboAddress& addr, DNSFilterEngine::Policy& pol) const
{
if (const auto fnd = d_postpolAddr.lookup(addr)) {
pol = fnd->second;
return true;
}
return false;
}
bool DNSFilterEngine::Zone::findClientPolicy(const ComboAddress& addr, DNSFilterEngine::Policy& pol) const
{
if (const auto fnd = d_qpolAddr.lookup(addr)) {
pol = fnd->second;
return true;
}
return false;
}
bool DNSFilterEngine::Zone::findNamedPolicy(const std::unordered_map<DNSName, DNSFilterEngine::Policy>& polmap, const DNSName& qname, DNSFilterEngine::Policy& pol) const
{
/* for www.powerdns.com, we need to check:
www.powerdns.com.
*.powerdns.com.
*.com.
*.
*/
std::unordered_map<DNSName, DNSFilterEngine::Policy>::const_iterator iter;
iter = polmap.find(qname);
if(iter != polmap.end()) {
pol=iter->second;
return true;
}
DNSName s(qname);
while(s.chopOff()){
iter = polmap.find(g_wildcarddnsname+s);
if(iter != polmap.end()) {
pol=iter->second;
return true;
}
}
return false;
}
DNSFilterEngine::Policy DNSFilterEngine::getProcessingPolicy(const DNSName& qname, const std::unordered_map<std::string,bool>& discardedPolicies) const
{
// cout<<"Got question for nameserver name "<<qname<<endl;
Policy pol;
for(const auto& z : d_zones) {
const auto zoneName = z->getName();
if(zoneName && discardedPolicies.find(*zoneName) != discardedPolicies.end()) {
continue;
}
if(z->findNSPolicy(qname, pol)) {
// cerr<<"Had a hit on the nameserver ("<<qname<<") used to process the query"<<endl;
return pol;
}
}
return pol;
}
DNSFilterEngine::Policy DNSFilterEngine::getProcessingPolicy(const ComboAddress& address, const std::unordered_map<std::string,bool>& discardedPolicies) const
{
Policy pol;
// cout<<"Got question for nameserver IP "<<address.toString()<<endl;
for(const auto& z : d_zones) {
const auto zoneName = z->getName();
if(zoneName && discardedPolicies.find(*zoneName) != discardedPolicies.end()) {
continue;
}
if(z->findNSIPPolicy(address, pol)) {
// cerr<<"Had a hit on the nameserver ("<<address.toString()<<") used to process the query"<<endl;
return pol;
}
}
return pol;
}
DNSFilterEngine::Policy DNSFilterEngine::getQueryPolicy(const DNSName& qname, const ComboAddress& ca, const std::unordered_map<std::string,bool>& discardedPolicies) const
{
// cout<<"Got question for "<<qname<<" from "<<ca.toString()<<endl;
Policy pol;
for(const auto& z : d_zones) {
const auto zoneName = z->getName();
if(zoneName && discardedPolicies.find(*zoneName) != discardedPolicies.end()) {
continue;
}
if(z->findQNamePolicy(qname, pol)) {
// cerr<<"Had a hit on the name of the query"<<endl;
return pol;
}
if(z->findClientPolicy(ca, pol)) {
// cerr<<"Had a hit on the IP address ("<<ca.toString()<<") of the client"<<endl;
return pol;
}
}
return pol;
}
DNSFilterEngine::Policy DNSFilterEngine::getPostPolicy(const vector<DNSRecord>& records, const std::unordered_map<std::string,bool>& discardedPolicies) const
{
Policy pol;
ComboAddress ca;
for(const auto& r : records) {
if(r.d_place != DNSResourceRecord::ANSWER)
continue;
if(r.d_type == QType::A) {
if (auto rec = getRR<ARecordContent>(r)) {
ca = rec->getCA();
}
}
else if(r.d_type == QType::AAAA) {
if (auto rec = getRR<AAAARecordContent>(r)) {
ca = rec->getCA();
}
}
else
continue;
for(const auto& z : d_zones) {
const auto zoneName = z->getName();
if(zoneName && discardedPolicies.find(*zoneName) != discardedPolicies.end()) {
continue;
}
if(z->findResponsePolicy(ca, pol)) {
return pol;
}
}
}
return pol;
}
void DNSFilterEngine::assureZones(size_t zone)
{
if(d_zones.size() <= zone)
d_zones.resize(zone+1);
}
void DNSFilterEngine::Zone::addClientTrigger(const Netmask& nm, Policy pol)
{
pol.d_name = d_name;
pol.d_type = PolicyType::ClientIP;
d_qpolAddr.insert(nm).second=pol;
}
void DNSFilterEngine::Zone::addResponseTrigger(const Netmask& nm, Policy pol)
{
pol.d_name = d_name;
pol.d_type = PolicyType::ResponseIP;
d_postpolAddr.insert(nm).second=pol;
}
void DNSFilterEngine::Zone::addQNameTrigger(const DNSName& n, Policy pol)
{
pol.d_name = d_name;
pol.d_type = PolicyType::QName;
d_qpolName[n]=pol;
}
void DNSFilterEngine::Zone::addNSTrigger(const DNSName& n, Policy pol)
{
pol.d_name = d_name;
pol.d_type = PolicyType::NSDName;
d_propolName[n]=pol;
}
void DNSFilterEngine::Zone::addNSIPTrigger(const Netmask& nm, Policy pol)
{
pol.d_name = d_name;
pol.d_type = PolicyType::NSIP;
d_propolNSAddr.insert(nm).second = pol;
}
bool DNSFilterEngine::Zone::rmClientTrigger(const Netmask& nm, Policy pol)
{
d_qpolAddr.erase(nm);
return true;
}
bool DNSFilterEngine::Zone::rmResponseTrigger(const Netmask& nm, Policy pol)
{
d_postpolAddr.erase(nm);
return true;
}
bool DNSFilterEngine::Zone::rmQNameTrigger(const DNSName& n, Policy pol)
{
d_qpolName.erase(n); // XXX verify we had identical policy?
return true;
}
bool DNSFilterEngine::Zone::rmNSTrigger(const DNSName& n, Policy pol)
{
d_propolName.erase(n); // XXX verify policy matched? =pol;
return true;
}
bool DNSFilterEngine::Zone::rmNSIPTrigger(const Netmask& nm, Policy pol)
{
d_propolNSAddr.erase(nm);
return true;
}
DNSRecord DNSFilterEngine::Policy::getCustomRecord(const DNSName& qname) const
{
if (d_kind != PolicyKind::Custom) {
throw std::runtime_error("Asking for a custom record from a filtering policy of a non-custom type");
}
DNSRecord result;
result.d_name = qname;
result.d_type = d_custom->getType();
result.d_ttl = d_ttl;
result.d_class = QClass::IN;
result.d_place = DNSResourceRecord::ANSWER;
result.d_content = d_custom;
if (result.d_type == QType::CNAME) {
const auto content = std::dynamic_pointer_cast<CNAMERecordContent>(d_custom);
if (content) {
DNSName target = content->getTarget();
if (target.isWildcard()) {
target.chopOff();
result.d_content = std::make_shared<CNAMERecordContent>(qname + target);
}
}
}
return result;
}
std::string DNSFilterEngine::Policy::getKindToString() const
{
static const DNSName drop("rpz-drop."), truncate("rpz-tcp-only."), noaction("rpz-passthru.");
static const DNSName rpzClientIP("rpz-client-ip"), rpzIP("rpz-ip"),
rpzNSDname("rpz-nsdname"), rpzNSIP("rpz-nsip.");
static const std::string rpzPrefix("rpz-");
switch(d_kind) {
case DNSFilterEngine::PolicyKind::NoAction:
return noaction.toString();
case DNSFilterEngine::PolicyKind::Drop:
return drop.toString();
case DNSFilterEngine::PolicyKind::NXDOMAIN:
return g_rootdnsname.toString();
case PolicyKind::NODATA:
return g_wildcarddnsname.toString();
case DNSFilterEngine::PolicyKind::Truncate:
return truncate.toString();
default:
throw std::runtime_error("Unexpected DNSFilterEngine::Policy kind");
}
}
DNSRecord DNSFilterEngine::Policy::getRecord(const DNSName& qname) const
{
DNSRecord dr;
if (d_kind == PolicyKind::Custom) {
dr = getCustomRecord(qname);
}
else {
dr.d_name = qname;
dr.d_ttl = static_cast<uint32_t>(d_ttl);
dr.d_type = QType::CNAME;
dr.d_class = QClass::IN;
dr.d_content = DNSRecordContent::mastermake(QType::CNAME, QClass::IN, getKindToString());
}
return dr;
}
void DNSFilterEngine::Zone::dumpNamedPolicy(FILE* fp, const DNSName& name, const Policy& pol) const
{
DNSRecord dr = pol.getRecord(name);
fprintf(fp, "%s %" PRIu32 " IN %s %s\n", dr.d_name.toString().c_str(), dr.d_ttl, QType(dr.d_type).getName().c_str(), dr.d_content->getZoneRepresentation().c_str());
}
DNSName DNSFilterEngine::Zone::maskToRPZ(const Netmask& nm)
{
int bits = nm.getBits();
DNSName res(std::to_string(bits));
const auto addr = nm.getNetwork();
if (addr.isIPv4()) {
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&addr.sin4.sin_addr.s_addr);
res += DNSName(std::to_string(bytes[3]) + "." + std::to_string(bytes[2]) + "." + std::to_string(bytes[1]) + "." + std::to_string(bytes[0]));
}
else {
DNSName temp;
const auto str = addr.toString();
const auto len = str.size();
std::string::size_type begin = 0;
while (begin < len) {
std::string::size_type end = str.find(":", begin);
std::string sub;
if (end != string::npos) {
sub = str.substr(begin, end - begin);
}
else {
sub = str.substr(begin);
}
if (sub.empty()) {
temp = DNSName("zz") + temp;
}
else {
temp = DNSName(sub) + temp;
}
if (end == string::npos) {
break;
}
begin = end + 1;
}
res += temp;
}
return res;
}
void DNSFilterEngine::Zone::dumpAddrPolicy(FILE* fp, const Netmask& nm, const DNSName& name, const Policy& pol) const
{
DNSName full = maskToRPZ(nm);
full += name;
DNSRecord dr = pol.getRecord(full);
fprintf(fp, "%s %" PRIu32 " IN %s %s\n", dr.d_name.toString().c_str(), dr.d_ttl, QType(dr.d_type).getName().c_str(), dr.d_content->getZoneRepresentation().c_str());
}
void DNSFilterEngine::Zone::dump(FILE* fp) const
{
/* fake the SOA record */
auto soa = DNSRecordContent::mastermake(QType::SOA, QClass::IN, "fake.RPZ. hostmaster.fake.RPZ. " + std::to_string(d_serial) + " " + std::to_string(d_refresh) + " 600 3600000 604800");
fprintf(fp, "%s IN SOA %s\n", d_domain.toString().c_str(), soa->getZoneRepresentation().c_str());
for (const auto& pair : d_qpolName) {
dumpNamedPolicy(fp, pair.first + d_domain, pair.second);
}
for (const auto& pair : d_propolName) {
dumpNamedPolicy(fp, pair.first + DNSName("rpz-nsdname.") + d_domain, pair.second);
}
for (const auto pair : d_qpolAddr) {
dumpAddrPolicy(fp, pair->first, DNSName("rpz-client-ip.") + d_domain, pair->second);
}
for (const auto pair : d_propolNSAddr) {
dumpAddrPolicy(fp, pair->first, DNSName("rpz-nsip.") + d_domain, pair->second);
}
for (const auto pair : d_postpolAddr) {
dumpAddrPolicy(fp, pair->first, DNSName("rpz-ip.") + d_domain, pair->second);
}
}
|