]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/ueberbackend.hh
Merge pull request #9070 from rgacogne/boost-173
[thirdparty/pdns.git] / pdns / ueberbackend.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
12 *
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.
17 *
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.
21 */
22 #pragma once
23 #include <vector>
24 #include <map>
25 #include <string>
26 #include <algorithm>
27 #include <semaphore.h>
28 #include <mutex>
29 #include <condition_variable>
30
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <boost/utility.hpp>
36 #include "dnspacket.hh"
37 #include "dnsbackend.hh"
38
39 #include "namespaces.hh"
40
41 /** This is a very magic backend that allows us to load modules dynamically,
42 and query them in order. This is persistent over all UeberBackend instantiations
43 across multiple threads.
44
45 The UeberBackend is transparent for exceptions, which should fall straight through.
46 */
47
48 class UeberBackend : public boost::noncopyable
49 {
50 public:
51 UeberBackend(const string &pname="default");
52 ~UeberBackend();
53
54 bool superMasterBackend(const string &ip, const DNSName &domain, const vector<DNSResourceRecord>&nsset, string *nameserver, string *account, DNSBackend **db);
55
56 /** Tracks all created UeberBackend instances for us. We use this vector to notify
57 existing threads of new modules
58 */
59 static vector<UeberBackend *>instances;
60 static std::mutex instances_lock;
61
62 static bool loadmodule(const string &name);
63 static bool loadModules(const vector<string>& modules, const string& path);
64
65 static void go(void);
66
67 /** This contains all registered backends. The DynListener modifies this list for us when
68 new modules are loaded */
69 vector<DNSBackend*> backends;
70
71 void cleanup();
72
73 //! the very magic handle for UeberBackend questions
74 class handle
75 {
76 public:
77 bool get(DNSZoneRecord &dr);
78 handle();
79 ~handle();
80
81 //! The UeberBackend class where this handle belongs to
82 UeberBackend *parent;
83 //! The current real backend, which is answering questions
84 DNSBackend *d_hinterBackend;
85
86 //! DNSPacket who asked this question
87 DNSPacket* pkt_p;
88 DNSName qname;
89
90 //! Index of the current backend within the backends vector
91 unsigned int i;
92 QType qtype;
93
94 private:
95
96 static AtomicCounter instances;
97 };
98
99 void lookup(const QType &, const DNSName &qdomain, int zoneId, DNSPacket *pkt_p=nullptr);
100
101 /** Determines if we are authoritative for a zone, and at what level */
102 bool getAuth(const DNSName &target, const QType &qtype, SOAData* sd, bool cachedOk=true);
103 bool getSOA(const DNSName &domain, SOAData &sd);
104 /** Load SOA info from backends, ignoring the cache.*/
105 bool getSOAUncached(const DNSName &domain, SOAData &sd);
106 bool get(DNSZoneRecord &r);
107 void getAllDomains(vector<DomainInfo> *domains, bool include_disabled=false);
108
109 void getUnfreshSlaveInfos(vector<DomainInfo>* domains);
110 void getUpdatedMasters(vector<DomainInfo>* domains);
111 bool getDomainInfo(const DNSName &domain, DomainInfo &di, bool getSerial=true);
112 bool createDomain(const DNSName &domain);
113
114 bool doesDNSSEC();
115 bool addDomainKey(const DNSName& name, const DNSBackend::KeyData& key, int64_t& id);
116 bool getDomainKeys(const DNSName& name, std::vector<DNSBackend::KeyData>& keys);
117 bool getAllDomainMetadata(const DNSName& name, std::map<std::string, std::vector<std::string> >& meta);
118 bool getDomainMetadata(const DNSName& name, const std::string& kind, std::vector<std::string>& meta);
119 bool setDomainMetadata(const DNSName& name, const std::string& kind, const std::vector<std::string>& meta);
120
121 bool removeDomainKey(const DNSName& name, unsigned int id);
122 bool activateDomainKey(const DNSName& name, unsigned int id);
123 bool deactivateDomainKey(const DNSName& name, unsigned int id);
124 bool publishDomainKey(const DNSName& name, unsigned int id);
125 bool unpublishDomainKey(const DNSName& name, unsigned int id);
126
127 bool getTSIGKey(const DNSName& name, DNSName* algorithm, string* content);
128 bool setTSIGKey(const DNSName& name, const DNSName& algorithm, const string& content);
129 bool deleteTSIGKey(const DNSName& name);
130 bool getTSIGKeys(std::vector< struct TSIGKey > &keys);
131
132 void alsoNotifies(const DNSName &domain, set<string> *ips);
133 void rediscover(string* status=0);
134 void reload();
135 bool searchRecords(const string &pattern, int maxResults, vector<DNSResourceRecord>& result);
136 bool searchComments(const string &pattern, int maxResults, vector<Comment>& result);
137 private:
138 handle d_handle;
139 vector<DNSZoneRecord> d_answers;
140 vector<DNSZoneRecord>::const_iterator d_cachehandleiter;
141
142 static std::mutex d_mut;
143 static std::condition_variable d_cond;
144
145 struct Question
146 {
147 DNSName qname;
148 int zoneId;
149 QType qtype;
150 }d_question;
151
152 unsigned int d_cache_ttl, d_negcache_ttl;
153 int d_domain_id;
154 int d_ancount;
155
156 bool d_negcached;
157 bool d_cached;
158 static bool d_go;
159 bool d_stale;
160
161 int cacheHas(const Question &q, vector<DNSZoneRecord> &rrs);
162 void addNegCache(const Question &q);
163 void addCache(const Question &q, vector<DNSZoneRecord>&& rrs);
164
165 };