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.
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include "ueberbackend.hh"
27 #include "dnspacket.hh"
28 #include "packetcache.hh"
29 #include "dnsseckeeper.hh"
30 #include "lua-auth4.hh"
32 #include "namespaces.hh"
34 // silly Solaris people define PC
37 /** Central DNS logic according to RFC1034. Ask this class a question in the form of a DNSPacket
38 and it will return, synchronously, a DNSPacket answer, suitable for
39 sending out over the network.
41 The PacketHandler gives your question to the PacketCache for possible inclusion
44 In order to do so, the PacketHandler contains a reference to the global extern PacketCache PC
46 It also contains an UeberBackend instance for answering the subqueries needed to generate
50 class NSEC3PARAMRecordContent;
55 std::unique_ptr<DNSPacket> doQuestion(DNSPacket&); //!< hand us a DNS packet with a question, we give you an answer
56 std::unique_ptr<DNSPacket> question(DNSPacket&); //!< hand us a DNS packet with a question, we give you an answer
58 ~PacketHandler(); // defined in packethandler.cc, and does --count
59 static int numRunning(){return s_count;}; //!< Returns the number of running PacketHandlers. Called by Distributor
61 UeberBackend *getBackend();
63 int trySuperMasterSynchronous(const DNSPacket& p, const DNSName& tsigkeyname);
64 static NetmaskGroup s_allowNotifyFrom;
65 static set<string> s_forwardNotify;
66 static const std::shared_ptr<CDNSKEYRecordContent> s_deleteCDNSKEYContent;
67 static const std::shared_ptr<CDSRecordContent> s_deleteCDSContent;
70 int trySuperMaster(const DNSPacket& p, const DNSName& tsigkeyname);
71 int processNotify(const DNSPacket& );
72 void addRootReferral(DNSPacket& r);
73 int doChaosRequest(const DNSPacket& p, std::unique_ptr<DNSPacket>& r, DNSName &target) const;
74 bool addDNSKEY(DNSPacket& p, std::unique_ptr<DNSPacket>& r);
75 bool addCDNSKEY(DNSPacket& p, std::unique_ptr<DNSPacket>& r);
76 bool addCDS(DNSPacket& p, std::unique_ptr<DNSPacket>& r);
77 bool addNSEC3PARAM(const DNSPacket& p, std::unique_ptr<DNSPacket>& r);
78 void doAdditionalProcessing(DNSPacket& p, std::unique_ptr<DNSPacket>& r);
79 DNSName doAdditionalServiceProcessing(const DNSName &firstTarget, const uint16_t &qtype, std::unique_ptr<DNSPacket>& r);
80 void addNSECX(DNSPacket& p, std::unique_ptr<DNSPacket>& r, const DNSName &target, const DNSName &wildcard, int mode);
81 void addNSEC(DNSPacket& p, std::unique_ptr<DNSPacket>& r, const DNSName &target, const DNSName &wildcard, int mode);
82 bool getNSEC3Hashes(bool narrow, const std::string& hashed, bool decrement, DNSName& unhashed, std::string& before, std::string& after, int mode=0);
83 void addNSEC3(DNSPacket& p, std::unique_ptr<DNSPacket>& r, const DNSName &target, const DNSName &wildcard, const NSEC3PARAMRecordContent& nsec3param, bool narrow, int mode);
84 void emitNSEC(std::unique_ptr<DNSPacket>& r, const DNSName& name, const DNSName& next, int mode);
85 void emitNSEC3(std::unique_ptr<DNSPacket>& r, const NSEC3PARAMRecordContent &ns3rc, const DNSName& unhashed, const string& begin, const string& end, int mode);
86 int processUpdate(DNSPacket& p);
87 int forwardPacket(const string &msgPrefix, const DNSPacket& p, const DomainInfo& di);
88 uint performUpdate(const string &msgPrefix, const DNSRecord *rr, DomainInfo *di, bool isPresigned, bool* narrow, bool* haveNSEC3, NSEC3PARAMRecordContent *ns3pr, bool *updatedSerial);
89 int checkUpdatePrescan(const DNSRecord *rr);
90 int checkUpdatePrerequisites(const DNSRecord *rr, DomainInfo *di);
91 void increaseSerial(const string &msgPrefix, const DomainInfo *di, const string& soaEditSetting, bool haveNSEC3, bool narrow, const NSEC3PARAMRecordContent *ns3pr);
93 void makeNXDomain(DNSPacket& p, std::unique_ptr<DNSPacket>& r, const DNSName& target, const DNSName& wildcard);
94 void makeNOError(DNSPacket& p, std::unique_ptr<DNSPacket>& r, const DNSName& target, const DNSName& wildcard, int mode);
95 vector<DNSZoneRecord> getBestReferralNS(DNSPacket& p, const DNSName &target);
96 vector<DNSZoneRecord> getBestDNAMESynth(DNSPacket& p, DNSName &target);
97 bool tryDNAME(DNSPacket& p, std::unique_ptr<DNSPacket>& r, DNSName &target);
98 bool tryReferral(DNSPacket& p, std::unique_ptr<DNSPacket>& r, const DNSName &target, bool retargeted);
100 bool getBestWildcard(DNSPacket& p, const DNSName &target, DNSName &wildcard, vector<DNSZoneRecord>* ret);
101 bool tryWildcard(DNSPacket& p, std::unique_ptr<DNSPacket>& r, DNSName &target, DNSName &wildcard, bool& retargeted, bool& nodata);
102 bool addDSforNS(DNSPacket& p, std::unique_ptr<DNSPacket>& r, const DNSName& dsname);
103 void completeANYRecords(DNSPacket& p, std::unique_ptr<DNSPacket>& r, const DNSName &target);
105 void tkeyHandler(const DNSPacket& p, std::unique_ptr<DNSPacket>& r); //<! process TKEY record, and adds TKEY record to (r)eply, or error code.
107 static AtomicCounter s_count;
108 static std::mutex s_rfc2136lock;
109 bool d_logDNSDetails;
111 bool d_doExpandALIAS;
114 std::unique_ptr<AuthLua4> d_pdl;
115 std::unique_ptr<AuthLua4> d_update_policy_lua;
117 UeberBackend B; // every thread an own instance
118 DNSSECKeeper d_dk; // B is shared with DNSSECKeeper