]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/filterpo.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "filterpo.hh"
24 #include "namespaces.hh"
25 #include "dnsrecords.hh"
27 DNSFilterEngine::DNSFilterEngine()
31 bool findNamedPolicy(const map
<DNSName
, DNSFilterEngine::Policy
>& polmap
, const DNSName
& qname
, DNSFilterEngine::Policy
& pol
)
35 /* for www.powerdns.com, we need to check:
46 auto iter
= polmap
.find(s
);
47 if(iter
!= polmap
.end()) {
52 iter
= polmap
.find(DNSName("*")+s
);
53 if(iter
!= polmap
.end()) {
63 DNSFilterEngine::Policy
DNSFilterEngine::getProcessingPolicy(const DNSName
& qname
) const
65 // cout<<"Got question for nameserver name "<<qname<<endl;
66 Policy pol
{PolicyKind::NoAction
, nullptr, "", 0};
67 for(const auto& z
: d_zones
) {
68 if(findNamedPolicy(z
.propolName
, qname
, pol
)) {
69 // cerr<<"Had a hit on the nameserver ("<<qname<<") used to process the query"<<endl;
77 DNSFilterEngine::Policy
DNSFilterEngine::getQueryPolicy(const DNSName
& qname
, const ComboAddress
& ca
) const
79 // cout<<"Got question for "<<qname<<" from "<<ca.toString()<<endl;
81 Policy pol
{PolicyKind::NoAction
, nullptr, "", 0};
82 for(const auto& z
: d_zones
) {
83 if(findNamedPolicy(z
.qpolName
, qname
, pol
)) {
84 // cerr<<"Had a hit on the name of the query"<<endl;
88 if(auto fnd
=z
.qpolAddr
.lookup(ca
)) {
89 // cerr<<"Had a hit on the IP address ("<<ca.toString()<<") of the client"<<endl;
97 DNSFilterEngine::Policy
DNSFilterEngine::getPostPolicy(const vector
<DNSRecord
>& records
) const
101 for(const auto& r
: records
) {
102 if(r
.d_place
!= DNSResourceRecord::ANSWER
)
104 if(r
.d_type
== QType::A
) {
105 if (auto rec
= getRR
<ARecordContent
>(r
)) {
109 else if(r
.d_type
== QType::AAAA
) {
110 if (auto rec
= getRR
<AAAARecordContent
>(r
)) {
117 for(const auto& z
: d_zones
) {
118 if(auto fnd
=z
.postpolAddr
.lookup(ca
))
122 return Policy
{PolicyKind::NoAction
, nullptr, "", 0};
125 void DNSFilterEngine::assureZones(int zone
)
127 if((int)d_zones
.size() <= zone
)
128 d_zones
.resize(zone
+1);
131 void DNSFilterEngine::clear(int zone
)
134 auto& z
= d_zones
[zone
];
136 z
.postpolAddr
.clear();
137 z
.propolName
.clear();
141 void DNSFilterEngine::addClientTrigger(const Netmask
& nm
, Policy pol
, int zone
)
144 d_zones
[zone
].qpolAddr
.insert(nm
).second
=pol
;
147 void DNSFilterEngine::addResponseTrigger(const Netmask
& nm
, Policy pol
, int zone
)
150 d_zones
[zone
].postpolAddr
.insert(nm
).second
=pol
;
153 void DNSFilterEngine::addQNameTrigger(const DNSName
& n
, Policy pol
, int zone
)
156 d_zones
[zone
].qpolName
[n
]=pol
;
159 void DNSFilterEngine::addNSTrigger(const DNSName
& n
, Policy pol
, int zone
)
162 d_zones
[zone
].propolName
[n
]=pol
;
165 bool DNSFilterEngine::rmClientTrigger(const Netmask
& nm
, Policy pol
, int zone
)
169 auto& qpols
= d_zones
[zone
].qpolAddr
;
174 bool DNSFilterEngine::rmResponseTrigger(const Netmask
& nm
, Policy pol
, int zone
)
177 auto& postpols
= d_zones
[zone
].postpolAddr
;
182 bool DNSFilterEngine::rmQNameTrigger(const DNSName
& n
, Policy pol
, int zone
)
185 d_zones
[zone
].qpolName
.erase(n
); // XXX verify we had identical policy?
189 bool DNSFilterEngine::rmNSTrigger(const DNSName
& n
, Policy pol
, int zone
)
192 d_zones
[zone
].propolName
.erase(n
); // XXX verify policy matched? =pol;