]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdist-lua.hh
Merge pull request #6063 from cyclops1982/3760
[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 class LuaAction : public DNSAction
25 {
26 public:
27 typedef std::function<std::tuple<int, string>(DNSQuestion* dq)> func_t;
28 LuaAction(LuaAction::func_t func) : d_func(func)
29 {}
30
31 Action operator()(DNSQuestion* dq, string* ruleresult) const override
32 {
33 std::lock_guard<std::mutex> lock(g_luamutex);
34 auto ret = d_func(dq);
35 if(ruleresult)
36 *ruleresult=std::get<1>(ret);
37 return (Action)std::get<0>(ret);
38 }
39
40 string toString() const override
41 {
42 return "Lua script";
43 }
44
45 private:
46 func_t d_func;
47 };
48
49 class LuaResponseAction : public DNSResponseAction
50 {
51 public:
52 typedef std::function<std::tuple<int, string>(DNSResponse* dr)> func_t;
53 LuaResponseAction(LuaResponseAction::func_t func) : d_func(func)
54 {}
55
56 Action operator()(DNSResponse* dr, string* ruleresult) const override
57 {
58 std::lock_guard<std::mutex> lock(g_luamutex);
59 auto ret = d_func(dr);
60 if(ruleresult)
61 *ruleresult=std::get<1>(ret);
62 return (Action)std::get<0>(ret);
63 }
64
65 string toString() const override
66 {
67 return "Lua response script";
68 }
69
70 private:
71 func_t d_func;
72 };
73
74 class SpoofAction : public DNSAction
75 {
76 public:
77 SpoofAction(const vector<ComboAddress>& addrs): d_addrs(addrs)
78 {
79 }
80 SpoofAction(const string& cname): d_cname(cname)
81 {
82 }
83 DNSAction::Action operator()(DNSQuestion* dq, string* ruleresult) const override;
84 string toString() const override
85 {
86 string ret = "spoof in ";
87 if(!d_cname.empty()) {
88 ret+=d_cname.toString()+ " ";
89 } else {
90 for(const auto& a : d_addrs)
91 ret += a.toString()+" ";
92 }
93 return ret;
94 }
95 private:
96 std::vector<ComboAddress> d_addrs;
97 DNSName d_cname;
98 };
99
100 typedef boost::variant<string, vector<pair<int, string>>, std::shared_ptr<DNSRule>, DNSName, vector<pair<int, DNSName> > > luadnsrule_t;
101 std::shared_ptr<DNSRule> makeRule(const luadnsrule_t& var);
102
103 typedef NetmaskTree<DynBlock> nmts_t;
104
105 void setupLuaActions();
106 void setupLuaBindings(bool client);
107 void setupLuaBindingsDNSQuestion();
108 void setupLuaRules();
109 void setupLuaInspection();
110 void setupLuaVars();