]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dnsdistdist/dnsdist-kvs.hh
Merge pull request #9229 from rgacogne/dnsdist-webserver-allow-from
[thirdparty/pdns.git] / pdns / dnsdistdist / dnsdist-kvs.hh
CommitLineData
f441962a
RG
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
24#include "dnsdist.hh"
25
26class KeyValueLookupKey
27{
28public:
29 virtual ~KeyValueLookupKey()
30 {
31 }
77eebb37 32 virtual std::vector<std::string> getKeys(const DNSQuestion&) = 0;
f441962a
RG
33 virtual std::string toString() const = 0;
34};
35
36class KeyValueLookupKeySourceIP: public KeyValueLookupKey
37{
38public:
14de749b
RG
39 std::vector<std::string> getKeys(const ComboAddress& addr);
40
41 std::vector<std::string> getKeys(const DNSQuestion& dq) override
42 {
43 return getKeys(*dq.remote);
44 }
f441962a
RG
45
46 std::string toString() const override
47 {
48 return "source IP";
49 }
50};
51
52class KeyValueLookupKeyQName: public KeyValueLookupKey
53{
54public:
e56dbccd 55
b5b3fc9b
RG
56 KeyValueLookupKeyQName(bool wireFormat): d_wireFormat(wireFormat)
57 {
58 }
59
e56dbccd
RG
60 std::vector<std::string> getKeys(const DNSName& qname)
61 {
b5b3fc9b
RG
62 if (d_wireFormat) {
63 return {qname.toDNSStringLC()};
64 }
752db0de 65 return {qname.makeLowerCase().toStringRootDot()};
e56dbccd
RG
66 }
67
77eebb37
RG
68 std::vector<std::string> getKeys(const DNSQuestion& dq) override
69 {
e56dbccd 70 return getKeys(*dq.qname);
77eebb37
RG
71 }
72
73 std::string toString() const override
74 {
b5b3fc9b
RG
75 if (d_wireFormat) {
76 return "qname in wire format";
77 }
77eebb37
RG
78 return "qname";
79 }
b5b3fc9b
RG
80
81private:
82 bool d_wireFormat;
77eebb37
RG
83};
84
85class KeyValueLookupKeySuffix: public KeyValueLookupKey
86{
87public:
b5b3fc9b
RG
88 KeyValueLookupKeySuffix(size_t minLabels, bool wireFormat): d_minLabels(minLabels), d_wireFormat(wireFormat)
89 {
90 }
91
e56dbccd
RG
92 std::vector<std::string> getKeys(const DNSName& qname);
93
77eebb37 94 std::vector<std::string> getKeys(const DNSQuestion& dq) override
f441962a 95 {
e56dbccd 96 return getKeys(*dq.qname);
f441962a
RG
97 }
98
99 std::string toString() const override
100 {
b5b3fc9b 101 if (d_minLabels > 0) {
63dec90c 102 return "suffix " + std::string(d_wireFormat ? "in wire format " : "") + "with at least " + std::to_string(d_minLabels) + " label(s)";
b5b3fc9b
RG
103 }
104 return "suffix" + std::string(d_wireFormat ? " in wire format" : "");
f441962a 105 }
b5b3fc9b
RG
106
107private:
108 size_t d_minLabels;
109 bool d_wireFormat;
f441962a
RG
110};
111
112class KeyValueLookupKeyTag: public KeyValueLookupKey
113{
114public:
115 KeyValueLookupKeyTag(const std::string& tag): d_tag(tag)
116 {
117 }
118
77eebb37 119 std::vector<std::string> getKeys(const DNSQuestion& dq) override
f441962a 120 {
f441962a
RG
121 if (dq.qTag) {
122 const auto& it = dq.qTag->find(d_tag);
123 if (it != dq.qTag->end()) {
77eebb37 124 return { it->second };
f441962a
RG
125 }
126 }
77eebb37 127 return {};
f441962a
RG
128 }
129
130 std::string toString() const override
131 {
5abdcc39 132 return "value of the tag named '" + d_tag + "'";
f441962a
RG
133 }
134
135private:
136 std::string d_tag;
137};
138
139class KeyValueStore
140{
141public:
142 virtual ~KeyValueStore()
143 {
144 }
145
73e1f0c5 146 virtual bool keyExists(const std::string& key) = 0;
90fe8ae6 147 virtual bool getValue(const std::string& key, std::string& value) = 0;
14de749b
RG
148 virtual bool reload()
149 {
150 return false;
151 }
f441962a
RG
152};
153
154#ifdef HAVE_LMDB
155
ae27ae09 156#include "ext/lmdb-safe/lmdb-safe.hh"
f441962a
RG
157
158class LMDBKVStore: public KeyValueStore
159{
160public:
161 LMDBKVStore(const std::string& fname, const std::string& dbName): d_env(fname.c_str(), MDB_NOSUBDIR, 0600), d_fname(fname), d_dbName(dbName)
162 {
163 }
164
73e1f0c5 165 bool keyExists(const std::string& key) override;
90fe8ae6 166 bool getValue(const std::string& key, std::string& value) override;
f441962a
RG
167
168private:
169 MDBEnv d_env;
170 std::string d_fname;
171 std::string d_dbName;
172};
173
174#endif /* HAVE_LMDB */
90fe8ae6
RG
175
176#ifdef HAVE_CDB
177
178#include "cdb.hh"
179
180class CDBKVStore: public KeyValueStore
181{
182public:
e5407513 183 CDBKVStore(const std::string& fname, time_t refreshDelay);
040793d4 184 ~CDBKVStore();
90fe8ae6 185
73e1f0c5 186 bool keyExists(const std::string& key) override;
90fe8ae6 187 bool getValue(const std::string& key, std::string& value) override;
14de749b 188 bool reload() override;
90fe8ae6
RG
189
190private:
e5407513 191 void refreshDBIfNeeded(time_t now);
14de749b 192 bool reload(const struct stat& st);
e5407513
RG
193
194 std::unique_ptr<CDB> d_cdb{nullptr};
90fe8ae6 195 std::string d_fname;
f0941861 196 ReadWriteLock d_lock;
e5407513
RG
197 time_t d_mtime{0};
198 time_t d_nextCheck{0};
199 time_t d_refreshDelay{0};
200 std::atomic_flag d_refreshing;
90fe8ae6
RG
201};
202
203#endif /* HAVE_LMDB */