Copyright (C) 2011 Fredrik Danerklint
This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License version 2 as published
+ it under the terms of the GNU General Public License version 2 as published
by the Free Software Foundation
This program is distributed in the hope that it will be useful,
{LUA_MATHLIBNAME, luaopen_math},
{LUA_DBLIBNAME, luaopen_debug},
// {LUA_COLIBNAME, luaopen_coroutine},
-#ifdef USE_LUAJIT
+#ifdef USE_LUAJIT
{"bit", luaopen_bit},
{"jit", luaopen_jit},
#endif
};
int my_lua_panic (lua_State *lua) {
- lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND");
+ lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND");
LUABackend* lb = (LUABackend*)lua_touserdata(lua, -1);
-
+
assert(lua == lb->lua);
-
+
stringstream e;
-
+
e << lb->backend_name << "LUA PANIC! '" << lua_tostring(lua,-1) << "'" << endl;
-
+
throw LUAException (e.str());
-
+
return 0;
}
int i = lua_gettop(lua);
if (i < 1)
return 0;
-
- lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND");
+
+ lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND");
LUABackend* lb = (LUABackend*)lua_touserdata(lua, -1);
string a = lua_tostring(lua, 1);
if (::arg().isEmpty(a))
lua_pushnil(lua);
- else
+ else
lua_pushstring(lua, lb->my_getArg(a).c_str());
return 1;
int i = lua_gettop(lua);
if (i < 1)
return 0;
-
- lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND");
+
+ lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND");
LUABackend* lb = (LUABackend*)lua_touserdata(lua, -1);
-
+
string a = lua_tostring(lua, 1);
if (::arg().isEmpty(a))
lua_pushnil(lua);
- else
+ else
lua_pushboolean(lua, lb->my_mustDo(a));
return 1;
}
int l_dnspacket (lua_State *lua) {
- lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND");
+ lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND");
LUABackend* lb = (LUABackend*)lua_touserdata(lua, -1);
if (lb->dnspacket == NULL) {
lua_pushnil(lua);
-
+
return 1;
}
lua_pushstring(lua, lb->dnspacket->getRemote().c_str());
lua_pushnumber(lua, lb->dnspacket->getRemotePort());
lua_pushstring(lua, lb->dnspacket->getLocal().c_str());
-
+
return 3;
}
int l_logger (lua_State *lua) {
// assert(lua == lb->lua);
-
- lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND");
+
+ lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND");
LUABackend* lb = (LUABackend*)lua_touserdata(lua, -1);
-
+
int i = lua_gettop(lua);
if (i < 1)
return 0;
const char *ss;
log_level = lua_tointeger(lua, 1);
-
+
string space = "";
-
+
for(j=2; j<=i; j++) {
ss = lua_tostring(lua, j);
s << space << ss;
space = " ";
}
-
+
L.log(lb->backend_name + s.str(), (Logger::Urgency) log_level);
-
+
return 0;
}
void register_lua_functions(lua_State *lua) {
- lua_gc(lua, LUA_GCSTOP, 0); // stop collector during initialization
+ lua_gc(lua, LUA_GCSTOP, 0); // stop collector during initialization
const luaL_Reg *lib = lualibs;
for (; lib->func; lib++) {
lua_pushinteger(lua, Logger::Info);
lua_setglobal(lua, "log_info");
-
+
lua_pushinteger(lua, Logger::Debug);
lua_setglobal(lua, "log_debug");
lua_pushinteger(lua, Logger::None);
lua_setglobal(lua, "log_none");
-
+
lua_pushcfunction(lua, l_dnspacket);
lua_setglobal(lua, "dnspacket");
-
+
lua_pushcfunction(lua, l_logger);
lua_setglobal(lua, "logger");
lua_pushcfunction(lua, l_arg_mustdo);
lua_setglobal(lua, "mustdo");
-
+
lua_newtable(lua);
for(vector<QType::namenum>::const_iterator iter = QType::names.begin(); iter != QType::names.end(); ++iter) {
lua_pushnumber(lua, iter->second);
}
bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, string& value) {
- lua_pushstring(lua, key.c_str());
- lua_gettable(lua, -2);
+ lua_pushstring(lua, key.c_str());
+ lua_gettable(lua, -2);
bool ret = false;
-
+
if(!lua_isnil(lua, -1)) {
value = lua_tostring(lua, -1);
ret = true;
}
-
+
+ lua_pop(lua, 1);
+
+ return ret;
+}
+
+bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, DNSName& value) {
+ lua_pushstring(lua, key.c_str());
+ lua_gettable(lua, -2);
+
+ bool ret = false;
+
+ if(!lua_isnil(lua, -1)) {
+ value = DNSName(lua_tostring(lua, -1));
+ ret = true;
+ }
+
lua_pop(lua, 1);
-
+
return ret;
}
bool LUABackend::getValueFromTable(lua_State *lua, uint32_t key, string& value) {
- lua_pushnumber(lua, key);
- lua_gettable(lua, -2);
+ lua_pushnumber(lua, key);
+ lua_gettable(lua, -2);
bool ret = false;
-
+
if(!lua_isnil(lua, -1)) {
value = lua_tostring(lua, -1);
ret = true;
}
-
+
lua_pop(lua, 1);
-
+
return ret;
}
bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, time_t& value) {
- lua_pushstring(lua, key.c_str());
- lua_gettable(lua, -2);
+ lua_pushstring(lua, key.c_str());
+ lua_gettable(lua, -2);
bool ret = false;
-
+
if(!lua_isnil(lua, -1)) {
value = (time_t)lua_tonumber(lua, -1);
ret = true;
}
-
+
lua_pop(lua, 1);
-
+
return ret;
}
bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, uint32_t& value) {
- lua_pushstring(lua, key.c_str());
- lua_gettable(lua, -2);
+ lua_pushstring(lua, key.c_str());
+ lua_gettable(lua, -2);
bool ret = false;
-
+
if(!lua_isnil(lua, -1)) {
value = (uint32_t)lua_tonumber(lua, -1);
ret = true;
}
-
+
lua_pop(lua, 1);
-
+
return ret;
}
bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, uint16_t& value) {
- lua_pushstring(lua, key.c_str());
- lua_gettable(lua, -2);
+ lua_pushstring(lua, key.c_str());
+ lua_gettable(lua, -2);
bool ret = false;
-
+
if(!lua_isnil(lua, -1)) {
value = (uint16_t)lua_tonumber(lua, -1);
ret = true;
}
-
+
lua_pop(lua, 1);
-
+
return ret;
}
bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, int& value) {
- lua_pushstring(lua, key.c_str());
- lua_gettable(lua, -2);
+ lua_pushstring(lua, key.c_str());
+ lua_gettable(lua, -2);
bool ret = false;
-
+
if(!lua_isnil(lua, -1)) {
value = (int)lua_tonumber(lua, -1);
ret = true;
}
-
+
lua_pop(lua, 1);
-
+
return ret;
}
bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, bool& value) {
- lua_pushstring(lua, key.c_str());
- lua_gettable(lua, -2);
+ lua_pushstring(lua, key.c_str());
+ lua_gettable(lua, -2);
bool ret = false;
-
+
if(!lua_isnil(lua, -1)) {
value = lua_toboolean(lua, -1);
ret = true;
}
-
+
lua_pop(lua, 1);
-
+
return ret;
}
-
LUABackend(const string &suffix="");
~LUABackend();
- bool list(const string &target, int domain_id, bool include_disabled=false);
- void lookup(const QType &qtype, const string &qname, DNSPacket *p, int domain_id);
+ bool list(const DNSName &target, int domain_id, bool include_disabled=false);
+ void lookup(const QType &qtype, const DNSName &qname, DNSPacket *p, int domain_id);
bool get(DNSResourceRecord &rr);
//! fills the soadata struct with the SOA details. Returns false if there is no SOA.
bool getSOA(const string &name, SOAData &soadata, DNSPacket *p=0);
// SLAVE BACKEND
-
+
bool getDomainInfo(const string &domain, DomainInfo &di);
bool isMaster(const string &name, const string &ip);
void getUnfreshSlaveInfos(vector<DomainInfo>* domains);
bool getBeforeAndAfterNamesAbsolute(uint32_t id, const std::string& qname, std::string& unhashed, std::string& before, std::string& after);
bool updateDNSSECOrderAndAuthAbsolute(uint32_t domain_id, const std::string& qname, const std::string& ordername, bool auth);
bool updateDNSSECOrderAndAuth(uint32_t domain_id, const std::string& zonename, const std::string& qname, bool auth);
-
-
+
+
// OTHER
void reload();
void rediscover(string* status=0);
-
+
string backend_name;
lua_State *lua;
pthread_t backend_pid;
unsigned int backend_count;
-
+
int f_lua_exec_error;
-
+
//minimal functions....
int f_lua_list;
int f_lua_lookup;
int f_lua_get;
int f_lua_getsoa;
-
+
//master functions....
int f_lua_getupdatedmasters;
int f_lua_setnotifed;
// FUNCTIONS TO THIS BACKEND
bool getValueFromTable(lua_State *lua, const std::string& key, string& value);
+ bool getValueFromTable(lua_State *lua, const std::string& key, DNSName& value);
bool getValueFromTable(lua_State *lua, uint32_t key, string& value);
bool getValueFromTable(lua_State *lua, const std::string& key, time_t& value);
bool getValueFromTable(lua_State *lua, const std::string& key, uint32_t& value);
void dnsrr_to_table(lua_State *lua, const DNSResourceRecord *rr);
//reload.cc
- void get_lua_function(lua_State *lua, const char *name, int *function);
+ void get_lua_function(lua_State *lua, const char *name, int *function);
bool dnssec;
/*
//minimal.cc
bool content(DNSResourceRecord* rr);
-
+
void getTheFreshOnes(vector<DomainInfo>* domains, string *type, string *f_name);
bool checkDomainInfo(const string *domain, mongo::BSONObj *mongo_r, string *f_name, string *mongo_q, DomainInfo *di, SOAData *soadata = NULL);
-
-
+
+
//crc32.cc
int generateCRC32(const string& my_string);
-
+
string mongo_db;
string collection_domains;
string collection_records;
string collection_tsigkeys;
mongo::DBClientConnection m_db;
-
+
auto_ptr<mongo::DBClientCursor> cursor;
-
+
string q_name;
-
+
// long long unsigned int count;
mongo::Query mongo_query;
mongo::BSONObj mongo_record;
DNSResourceRecord rr_record;
string type;
mongo::BSONObjIterator* contents;
-
-
-
+
+
+
unsigned int default_ttl;
bool logging_cerr;
bool checkindex;
bool use_default_ttl;
-
+
bool axfr_soa;
SOAData last_soadata;
-*/
+*/
};
-#endif
+#endif
Copyright (C) 2011 Fredrik Danerklint
This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License version 2 as published
+ it under the terms of the GNU General Public License version 2 as published
by the Free Software Foundation
This program is distributed in the hope that it will be useful,
LUABackend::LUABackend(const string &suffix) {
setArgPrefix("lua"+suffix);
-
+
try {
if (pthread_equal(backend_pid, pthread_self())) {
backend_count = 1;
backend_pid = pthread_self();
}
-
+
// lb = NULL;
lua = NULL;
dnspacket = NULL;
}
}
-
+
LUABackend::~LUABackend() {
L<<Logger::Info<<backend_name<<"Closeing..." << endl;
-
+
lua_close(lua);
}
-bool LUABackend::list(const string &target, int domain_id, bool include_disabled) {
+bool LUABackend::list(const DNSName &target, int domain_id, bool include_disabled) {
if (logging)
L << Logger::Info << backend_name << "(list) BEGIN" << endl;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_list);
- lua_pushstring(lua, target.c_str());
+ lua_pushstring(lua, target.toString().c_str());
lua_pushnumber(lua, domain_id);
if(lua_pcall(lua, 2, 1, f_lua_exec_error) != 0) {
string e = backend_name + lua_tostring(lua, -1);
lua_pop(lua, 1);
-
+
throw runtime_error(e);
}
-
+
size_t returnedwhat = lua_type(lua, -1);
bool ok = false;
-
+
if (returnedwhat == LUA_TBOOLEAN)
ok = lua_toboolean(lua, -1);
-
+
lua_pop(lua, 1);
if (logging)
L << Logger::Info << backend_name << "(list) END" << endl;
-
+
return ok;
}
-
-void LUABackend::lookup(const QType &qtype, const string &qname, DNSPacket *p, int domain_id) {
+
+void LUABackend::lookup(const QType &qtype, const DNSName &qname, DNSPacket *p, int domain_id) {
if (logging)
L << Logger::Info << backend_name << "(lookup) BEGIN" << endl;
dnspacket = p;
-
+
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_lookup);
// lua_pushnumber(lua, qtype.getCode());
lua_pushstring(lua, qtype.getName().c_str());
- lua_pushstring(lua, qname.c_str());
+ lua_pushstring(lua, qname.toString(). c_str());
lua_pushnumber(lua, domain_id);
if(lua_pcall(lua, 3, 0, f_lua_exec_error) != 0) {
string e = backend_name + lua_tostring(lua, -1);
lua_pop(lua, 1);
-
+
dnspacket = NULL;
-
+
throw runtime_error(e);
return;
}
-
+
dnspacket = NULL;
-
+
if (logging)
L << Logger::Info << backend_name << "(lookup) END" << endl;
}
bool LUABackend::get(DNSResourceRecord &rr) {
if (logging)
L << Logger::Info << backend_name << "(get) BEGIN" << endl;
-
+
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_get);
-
+
if(lua_pcall(lua, 0, 1, f_lua_exec_error) != 0) {
string e = backend_name + lua_tostring(lua, -1);
lua_pop(lua, 1);
-
+
throw runtime_error(e);
return false;
}
-
+
size_t returnedwhat = lua_type(lua, -1);
if (returnedwhat != LUA_TTABLE) {
lua_pop(lua, 1 );
return false;
}
-
+
rr.content.clear();
-
+
// uint16_t qt;
string qt;
-
+
if (getValueFromTable(lua, "type", qt) )
rr.qtype = qt;
getValueFromTable(lua, "name", rr.qname);
getValueFromTable(lua, "last_modified", rr.last_modified);
getValueFromTable(lua, "ttl", rr.ttl);
- if (rr.ttl == 0)
+ if (rr.ttl == 0)
rr.ttl = ::arg().asNum( "default-ttl" );
-
+
getValueFromTable(lua, "content", rr.content);
lua_pop(lua, 1 );
-
+
if (logging)
L << Logger::Info << backend_name << "(get) END" << endl;
-
+
return !rr.content.empty();
}
bool LUABackend::getSOA(const string &name, SOAData &soadata, DNSPacket *p) {
if (logging)
L << Logger::Info << backend_name << "(getsoa) BEGIN" << endl;
-
+
dnspacket = p;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_getsoa);
-
+
lua_pushstring(lua, name.c_str());
-
+
if(lua_pcall(lua, 1, 1, f_lua_exec_error) != 0) {
string e = backend_name + lua_tostring(lua, -1);
lua_pop(lua, 1);
-
+
dnspacket = NULL;
-
+
throw runtime_error(e);
return false;
}
-
+
dnspacket = NULL;
-
+
size_t returnedwhat = lua_type(lua, -1);
if (returnedwhat != LUA_TTABLE) {
lua_pop(lua, 1 );
lua_pop(lua, 1 );
return false;
}
-
+
getValueFromTable(lua, "refresh", soadata.refresh);
getValueFromTable(lua, "retry", soadata.retry);
getValueFromTable(lua, "expire", soadata.expire);
getValueFromTable(lua, "default_ttl", soadata.default_ttl);
getValueFromTable(lua, "domain_id", soadata.domain_id);
-
+
getValueFromTable(lua, "ttl", soadata.ttl);
- if (soadata.ttl == 0 && soadata.default_ttl > 0)
+ if (soadata.ttl == 0 && soadata.default_ttl > 0)
soadata.ttl = soadata.default_ttl;
-
+
if (soadata.ttl == 0) {
lua_pop(lua, 1 );
return false;
}
-
+
if (!getValueFromTable(lua, "nameserver", soadata.nameserver)) {
soadata.nameserver = arg()["default-soa-name"];
if (soadata.nameserver.empty()) {
- L<<Logger::Error << backend_name << "(getSOA)" << " Error: SOA Record is missing nameserver for the domain '" << name << "'" << endl;
+ L<<Logger::Error << backend_name << "(getSOA)" << " Error: SOA Record is missing nameserver for the domain '" << name << "'" << endl;
lua_pop(lua, 1 );
return false;
}
}
-
+
if (!getValueFromTable(lua, "hostmaster", soadata.hostmaster))
soadata.hostmaster = "hostmaster." + name;
if (logging)
L << Logger::Info << backend_name << "(getsoa) END" << endl;
-
+
return true;
}