]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: add setACLFromFile()
authorMatti Hiljanen <matti@hiljanen.com>
Mon, 7 Dec 2020 11:37:25 +0000 (13:37 +0200)
committerMatti Hiljanen <matti@hiljanen.com>
Tue, 8 Dec 2020 08:52:27 +0000 (10:52 +0200)
pdns/dnsdist-console.cc
pdns/dnsdist-lua.cc

index 52328d9b6a02e81c1bb7ef84dd709f9ea283507f..ecf2d08d034fec5c092555f6e85393d681bb4a20 100644 (file)
@@ -511,6 +511,7 @@ const std::vector<ConsoleKeyword> g_consoleKeywords{
   { "roundrobin", false, "", "Simple round robin over available servers" },
   { "sendCustomTrap", true, "str", "send a custom `SNMP` trap from Lua, containing the `str` string"},
   { "setACL", true, "{netmask, netmask}", "replace the ACL set with these netmasks. Use `setACL({})` to reset the list, meaning no one can use us" },
+  { "setACLFromFile", true, "file", "replace the ACL set with netmasks in this file" },
   { "setAddEDNSToSelfGeneratedResponses", true, "add", "set whether to add EDNS to self-generated responses, provided that the initial query had EDNS" },
   { "setAllowEmptyResponse", true, "allow", "Set to true (defaults to false) to allow empty responses (qdcount=0) with a NoError or NXDomain rcode (default) from backends" },
   { "setAPIWritable", true, "bool, dir", "allow modifications via the API. if `dir` is set, it must be a valid directory where the configuration files will be written by the API" },
index 6de9aed7716fda1cb65f538565f999fbc7f1b179..e25b57dab8b2ffada1514eaac33bd5d4014c84e6 100644 (file)
@@ -675,6 +675,31 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
       g_ACL.setState(nmg);
   });
 
+  luaCtx.writeFunction("setACLFromFile", [](const std::string& file) {
+      setLuaSideEffect();
+      NetmaskGroup nmg;
+
+      ifstream ifs(file);
+      if(!ifs) {
+        throw std::runtime_error("Could not open '"+file+"': "+stringerror());
+      }
+
+      string::size_type pos;
+      string line;
+      while(getline(ifs,line)) {
+        pos=line.find('#');
+        if(pos!=string::npos)
+          line.resize(pos);
+        boost::trim(line);
+        if(line.empty())
+          continue;
+
+        nmg.addMask(line);
+      }
+
+      g_ACL.setState(nmg);
+  });
+
   luaCtx.writeFunction("showACL", []() {
       setLuaNoSideEffect();
       vector<string> vec;