]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdist-lua.hh
Merge pull request #8835 from omoerbeek/rec-prep-4.3.0-rc2
[thirdparty/pdns.git] / pdns / dnsdist-lua.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
24 struct ResponseConfig
25 {
26 boost::optional<bool> setAA{boost::none};
27 boost::optional<bool> setAD{boost::none};
28 boost::optional<bool> setRA{boost::none};
29 uint32_t ttl{60};
30 };
31 void setResponseHeadersFromConfig(dnsheader& dh, const ResponseConfig& config);
32
33 class LuaAction : public DNSAction
34 {
35 public:
36 typedef std::function<std::tuple<int, boost::optional<string> >(DNSQuestion* dq)> func_t;
37 LuaAction(const LuaAction::func_t& func) : d_func(func)
38 {}
39 Action operator()(DNSQuestion* dq, string* ruleresult) const override;
40 string toString() const override
41 {
42 return "Lua script";
43 }
44 private:
45 func_t d_func;
46 };
47
48 class LuaResponseAction : public DNSResponseAction
49 {
50 public:
51 typedef std::function<std::tuple<int, boost::optional<string> >(DNSResponse* dr)> func_t;
52 LuaResponseAction(const LuaResponseAction::func_t& func) : d_func(func)
53 {}
54 Action operator()(DNSResponse* dr, string* ruleresult) const override;
55 string toString() const override
56 {
57 return "Lua response script";
58 }
59 private:
60 func_t d_func;
61 };
62
63 class SpoofAction : public DNSAction
64 {
65 public:
66 SpoofAction(const vector<ComboAddress>& addrs): d_addrs(addrs)
67 {
68 for (const auto& addr : d_addrs) {
69 if (addr.isIPv4()) {
70 d_types.insert(QType::A);
71 }
72 else if (addr.isIPv6()) {
73 d_types.insert(QType::AAAA);
74 }
75 }
76
77 if (!d_addrs.empty()) {
78 d_types.insert(QType::ANY);
79 }
80 }
81
82 SpoofAction(const DNSName& cname): d_cname(cname)
83 {
84 }
85
86 SpoofAction(const std::string& raw): d_rawResponse(raw)
87 {
88 }
89
90 DNSAction::Action operator()(DNSQuestion* dq, string* ruleresult) const override;
91
92 string toString() const override
93 {
94 string ret = "spoof in ";
95 if (!d_cname.empty()) {
96 ret += d_cname.toString() + " ";
97 }
98 else if (!d_rawResponse.empty()) {
99 ret += "raw bytes ";
100 }
101 else {
102 for(const auto& a : d_addrs)
103 ret += a.toString()+" ";
104 }
105 return ret;
106 }
107
108
109 ResponseConfig d_responseConfig;
110 private:
111 std::vector<ComboAddress> d_addrs;
112 std::set<uint16_t> d_types;
113 std::string d_rawResponse;
114 DNSName d_cname;
115 };
116
117 typedef boost::variant<string, vector<pair<int, string>>, std::shared_ptr<DNSRule>, DNSName, vector<pair<int, DNSName> > > luadnsrule_t;
118 std::shared_ptr<DNSRule> makeRule(const luadnsrule_t& var);
119 typedef std::unordered_map<std::string, boost::variant<std::string> > luaruleparams_t;
120 void parseRuleParams(boost::optional<luaruleparams_t> params, boost::uuids::uuid& uuid, uint64_t& creationOrder);
121
122 typedef NetmaskTree<DynBlock> nmts_t;
123
124 vector<std::function<void(void)>> setupLua(bool client, bool configCheck, const std::string& config);
125 void setupLuaActions();
126 void setupLuaBindings(bool client);
127 void setupLuaBindingsDNSCrypt();
128 void setupLuaBindingsDNSQuestion();
129 void setupLuaBindingsKVS(bool client);
130 void setupLuaBindingsPacketCache();
131 void setupLuaBindingsProtoBuf(bool client, bool configCheck);
132 void setupLuaRules();
133 void setupLuaInspection();
134 void setupLuaVars();