File: ueberbackend.hh

package info (click to toggle)
pdns-recursor 5.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,108 kB
  • sloc: cpp: 109,513; javascript: 20,651; python: 5,657; sh: 5,069; makefile: 780; ansic: 582; xml: 37
file content (189 lines) | stat: -rw-r--r-- 7,201 bytes parent folder | download | duplicates (2)
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
/*
 * 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 <vector>
#include <map>
#include <string>
#include <algorithm>
#include <mutex>
#include <condition_variable>

#include <boost/utility.hpp>

#include "dnspacket.hh"
#include "dnsbackend.hh"
#include "lock.hh"
#include "namespaces.hh"

/** This is a very magic backend that allows us to load modules dynamically,
    and query them in order. This is persistent over all UeberBackend instantiations
    across multiple threads.

    The UeberBackend is transparent for exceptions, which should fall straight through.
*/

class UeberBackend : public boost::noncopyable
{
public:
  UeberBackend(const string& pname = "default");
  ~UeberBackend();

  bool autoPrimaryBackend(const string& ip, const ZoneName& domain, const vector<DNSResourceRecord>& nsset, string* nameserver, string* account, DNSBackend** dnsBackend);

  bool autoPrimaryAdd(const AutoPrimary& primary);
  bool autoPrimaryRemove(const struct AutoPrimary& primary);
  bool autoPrimariesList(std::vector<AutoPrimary>& primaries);

  static bool loadmodule(const string& name);
  static bool loadModules(const vector<string>& modules, const string& path);

  static void go();

  /** This contains all registered backends. The DynListener modifies this list for us when
      new modules are loaded */
  vector<std::unique_ptr<DNSBackend>> backends;

  //! the very magic handle for UeberBackend questions
  class handle
  {
  public:
    bool get(DNSZoneRecord& record);
    handle();
    ~handle();

    //! The UeberBackend class where this handle belongs to
    UeberBackend* parent{nullptr};
    //! The current real backend, which is answering questions
    DNSBackend* d_hinterBackend{nullptr};

    //! DNSPacket who asked this question
    DNSPacket* pkt_p{nullptr};
    DNSName qname;

    //! Index of the current backend within the backends vector
    unsigned int i{0};
    QType qtype;
    domainid_t zoneId{UnknownDomainID};

  private:
    static AtomicCounter instances;
  };

  void lookup(const QType& qtype, const DNSName& qname, domainid_t zoneId, DNSPacket* pkt_p = nullptr);
  /** Read a single record from a lookup(...) result. */
  bool get(DNSZoneRecord& resourceRecord);
  /** Close state created by lookup(...). */
  void lookupEnd();

  /** Determines if we are authoritative for a zone, and at what level */
  bool getAuth(const ZoneName& target, const QType& qtype, SOAData* soaData, Netmask remote, bool cachedOk = true, DNSPacket* pkt_p = nullptr);
  /** Load SOA info from backends, ignoring the cache.*/
  bool getSOAUncached(const ZoneName& domain, SOAData& soaData);
  void getAllDomains(vector<DomainInfo>* domains, bool getSerial, bool include_disabled);

  void getUnfreshSecondaryInfos(vector<DomainInfo>* domains);
  void getUpdatedPrimaries(vector<DomainInfo>& domains, std::unordered_set<DNSName>& catalogs, CatalogHashMap& catalogHashes);
  bool getDomainInfo(const ZoneName& domain, DomainInfo& domainInfo, bool getSerial = true);
  bool createDomain(const ZoneName& domain, DomainInfo::DomainKind kind, const vector<ComboAddress>& primaries, const string& account);

  bool doesDNSSEC();
  bool addDomainKey(const ZoneName& name, const DNSBackend::KeyData& key, int64_t& keyID);
  bool getDomainKeys(const ZoneName& name, std::vector<DNSBackend::KeyData>& keys);
  bool getAllDomainMetadata(const ZoneName& name, std::map<std::string, std::vector<std::string>>& meta);
  bool getDomainMetadata(const ZoneName& name, const std::string& kind, std::vector<std::string>& meta);
  bool getDomainMetadata(const ZoneName& name, const std::string& kind, std::string& meta);
  bool setDomainMetadata(const ZoneName& name, const std::string& kind, const std::vector<std::string>& meta);
  bool setDomainMetadata(const ZoneName& name, const std::string& kind, const std::string& meta);

  bool removeDomainKey(const ZoneName& name, unsigned int keyID);
  bool activateDomainKey(const ZoneName& name, unsigned int keyID);
  bool deactivateDomainKey(const ZoneName& name, unsigned int keyID);
  bool publishDomainKey(const ZoneName& name, unsigned int keyID);
  bool unpublishDomainKey(const ZoneName& name, unsigned int keyID);

  void alsoNotifies(const ZoneName& domain, set<string>* ips);
  void rediscover(string* status = nullptr);
  void reload();

  bool setTSIGKey(const DNSName& name, const DNSName& algorithm, const string& content);
  bool getTSIGKey(const DNSName& name, DNSName& algorithm, string& content);
  bool getTSIGKeys(std::vector<struct TSIGKey>& keys);
  bool deleteTSIGKey(const DNSName& name);

  void viewList(vector<string>& result);
  void viewListZones(const string& view, vector<ZoneName>& result);
  bool viewAddZone(const string& /* view */, const ZoneName& /* zone */);
  bool viewDelZone(const string& /* view */, const ZoneName& /* zone */);

  bool networkSet(const Netmask& net, std::string& tag);
  void networkList(vector<pair<Netmask, string>>& networks);

  bool searchRecords(const string& pattern, vector<DNSResourceRecord>::size_type maxResults, vector<DNSResourceRecord>& result);
  bool searchComments(const string& pattern, size_t maxResults, vector<Comment>& result);

  void updateZoneCache();

  bool inTransaction();

  bool hasCreatedLocalFiles();

  unsigned int getCapabilities();

private:
  handle d_handle;
  vector<DNSZoneRecord> d_answers;
  vector<DNSZoneRecord>::const_iterator d_cachehandleiter;

  static std::mutex d_mut;
  static std::condition_variable d_cond;

  struct Question
  {
    DNSName qname;
    domainid_t zoneId;
    QType qtype;
  } d_question;

  unsigned int d_cache_ttl, d_negcache_ttl;
  uint16_t d_qtype{0};

  bool d_negcached{false};
  bool d_cached{false};
  static AtomicCounter* s_backendQueries;
  static bool d_go;
  bool d_stale{false};
  static bool s_doANYLookupsOnly;

  enum CacheResult
  {
    Miss = -1,
    NegativeMatch = 0,
    Hit = 1,
  };

  CacheResult cacheHas(const Question& question, vector<DNSZoneRecord>& resourceRecords) const;
  void addNegCache(const Question& question) const;
  void addCache(const Question& question, vector<DNSZoneRecord>&& rrs) const;

  bool fillSOAFromZoneRecord(ZoneName& shorter, domainid_t zoneId, SOAData* soaData);
  CacheResult fillSOAFromCache(SOAData* soaData, ZoneName& shorter);
};