Skip to content

Commit

Permalink
lua-base4: Add include path feature
Browse files Browse the repository at this point in the history
Loads sorted list of files from given path mask.
  • Loading branch information
cmouse committed Jul 25, 2024
1 parent e2a1062 commit 11907b2
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 20 deletions.
5 changes: 3 additions & 2 deletions pdns/lua-auth4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

#include "ueberbackend.hh"

AuthLua4::AuthLua4() { prepareContext(); }

LuaContext* AuthLua4::getLua()
{
return d_lw.get();
Expand Down Expand Up @@ -84,6 +82,9 @@ void AuthLua4::postPrepareContext() {
d_lw->registerFunction<DNSName(UpdatePolicyQuery::*)()>("getTsigName", [](UpdatePolicyQuery& upq) { return upq.tsigName; });
d_lw->registerFunction<std::string(UpdatePolicyQuery::*)()>("getPeerPrincipal", [](UpdatePolicyQuery& upq) { return upq.peerPrincipal; });
/* end of update policy */
if (!d_include_path.empty()) {
includePath(d_include_path);
}
}

void AuthLua4::postLoad() {
Expand Down
4 changes: 3 additions & 1 deletion pdns/lua-auth4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
class AuthLua4 : public BaseLua4
{
public:
AuthLua4();
AuthLua4(const std::string& includePath="") : BaseLua4(includePath) {
prepareContext();
};
bool updatePolicy(const DNSName &qname, const QType& qtype, const DNSName &zonename, const DNSPacket& packet);
bool axfrfilter(const ComboAddress&, const DNSName&, const DNSResourceRecord&, std::vector<DNSResourceRecord>&);
LuaContext* getLua();
Expand Down
58 changes: 49 additions & 9 deletions pdns/lua-base4.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "config.h"
#include <cassert>
#include <fstream>
#include <unordered_set>
#include <unordered_map>
#include <typeinfo>
#include <sys/stat.h>
#include "logger.hh"
#include "logging.hh"
#include "iputils.hh"
Expand All @@ -15,24 +17,60 @@
#include "ext/luawrapper/include/LuaContext.hpp"
#include "dns_random.hh"

BaseLua4::BaseLua4() = default;

void BaseLua4::loadFile(const std::string& fname)
void BaseLua4::loadFile(const std::string& fname, bool doPostLoad)
{
std::ifstream ifs(fname);
if (!ifs) {
auto ret = errno;
auto msg = stringerror(ret);
SLOG(g_log << Logger::Error << "Unable to read configuration file from '" << fname << "': " << msg << endl,
g_slog->withName("lua")->error(Logr::Error, ret, "Unable to read configuration file", "file", Logging::Loggable(fname), "msg", Logging::Loggable(msg)));
g_log << Logger::Error << "Unable to read configuration file from '" << fname << "': " << msg << endl;
throw std::runtime_error(msg);
}
loadStream(ifs);
loadStream(ifs, doPostLoad);
};

void BaseLua4::loadString(const std::string &script) {
std::istringstream iss(script);
loadStream(iss);
loadStream(iss, true);
};

void BaseLua4::includePath(const std::string& directory) {
std::vector<std::string> vec;
const std::string& suffix = "lua";
auto directoryError = pdns::visit_directory(directory, [this, &directory, &suffix, &vec]([[maybe_unused]] ino_t inodeNumber, const std::string_view& name) {
(void)this;
if (boost::starts_with(name, ".")) {
return true; // skip any dots
}
if (boost::ends_with(name, suffix)) {
// build name
string fullName = directory + "/" + std::string(name);
// ensure it's readable file
struct stat statInfo
{
};
if (stat(fullName.c_str(), &statInfo) != 0 || !S_ISREG(statInfo.st_mode)) {
string msg = fullName + " is not a regular file";
g_log << Logger::Error << msg << std::endl;
throw PDNSException(msg);
}
vec.emplace_back(fullName);
}
return true;
});

if (directoryError) {
int err = errno;
string msg = directory + " is not accessible: " + stringerror(err);
g_log << Logger::Error << msg << std::endl;
throw PDNSException(msg);
}

std::sort(vec.begin(), vec.end(), CIStringComparePOSIX());

for(const auto& file: vec) {
loadFile(file, false);
}
};

// By default no features
Expand Down Expand Up @@ -289,10 +327,12 @@ void BaseLua4::prepareContext() {
d_lw->writeVariable("pdns", d_pd);
}

void BaseLua4::loadStream(std::istream &stream) {
void BaseLua4::loadStream(std::istream &stream, bool doPostLoad) {
d_lw->executeCode(stream);

postLoad();
if (doPostLoad) {
postLoad();
}
}

BaseLua4::~BaseLua4() = default;
10 changes: 6 additions & 4 deletions pdns/lua-base4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class BaseLua4 : public boost::noncopyable
{
protected:
std::unique_ptr<LuaContext> d_lw; // this is way on top because it must get destroyed _last_
std::string d_include_path; // path where scripts to include at postLoad are

public:
BaseLua4();
void loadFile(const std::string& fname);
void loadString(const std::string& script);
void loadStream(std::istream& stream);
BaseLua4(const std::string &includePath) : d_include_path(includePath) {};
void loadFile(const std::string &fname, bool doPostLoad=true);
void loadString(const std::string &script);
void loadStream(std::istream &stream, bool doPostLoad=true);
void includePath(const std::string &directory);
virtual ~BaseLua4(); // this is so unique_ptr works with an incomplete type
protected:
void prepareContext();
Expand Down
5 changes: 3 additions & 2 deletions pdns/recursordist/lua-recursor4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
#include <unordered_set>
#include "rec-main.hh"

RecursorLua4::RecursorLua4() { prepareContext(); }

boost::optional<dnsheader> RecursorLua4::DNSQuestion::getDH() const
{
if (dh != nullptr) {
Expand Down Expand Up @@ -495,6 +493,9 @@ void RecursorLua4::postPrepareContext()
(*event.discardedPolicies)[policy] = true;
}
});
if (!d_include_path.empty()) {
includePath(d_include_path);
}
}

// clang-format on
Expand Down
6 changes: 5 additions & 1 deletion pdns/recursordist/lua-recursor4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ struct LuaContext::Pusher<pdns_postresolve_ffi_handle*>
class RecursorLua4 : public BaseLua4
{
public:
RecursorLua4();
RecursorLua4(const std::string& includePath = "") :
BaseLua4(includePath)
{
prepareContext();
};
RecursorLua4(const RecursorLua4&) = delete;
RecursorLua4(RecursorLua4&&) = delete;
RecursorLua4& operator=(const RecursorLua4&) = delete;
Expand Down
3 changes: 2 additions & 1 deletion pdns/recursordist/rec-lua-conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ static void rpzPrimary(LuaConfigItems& lci, const boost::variant<string, std::ve
class RecLuaConfigContext : public BaseLua4
{
public:
RecLuaConfigContext()
RecLuaConfigContext() :
BaseLua4("")
{
prepareContext();
}
Expand Down

0 comments on commit 11907b2

Please sign in to comment.