]> git.ipfire.org Git - thirdparty/pdns.git/blame - modules/luabackend/supermaster.cc
Include config.h only in .cc files
[thirdparty/pdns.git] / modules / luabackend / supermaster.cc
CommitLineData
e18dbde1
BH
1/*
2 Copyright (C) 2011 Fredrik Danerklint
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 as published
6 by the Free Software Foundation
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16*/
17
18
870a0fe4
AT
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
e18dbde1
BH
22#include "luabackend.hh"
23
24#include "pdns/logger.hh"
25#include "pdns/arguments.hh"
26
a5ffbceb
RK
27/*
28 //! determine if ip is a supermaster for a domain
719f9024 29 virtual bool superMasterBackend(const string &ip, const string &domain, const vector<DNSResourceRecord>&nsset, string *nameserver, string *account, DNSBackend **db)
e18dbde1
BH
30
31 //! called by PowerDNS to create a slave record for a superMaster
719f9024 32 virtual bool createSlaveDomain(const string &ip, const string &domain, const string &nameserver, const string &account)
e18dbde1
BH
33
34*/
35
719f9024 36bool LUABackend::superMasterBackend(const string &ip, const string &domain, const vector<DNSResourceRecord>&nsset, string *nameserver, string *account, DNSBackend **db) {
e18dbde1
BH
37
38 if (f_lua_supermasterbackend == 0)
39 return false;
40
41 if (logging)
42 L << Logger::Info << backend_name << "(superMasterBackend) BEGIN" << endl;
43
44 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_supermasterbackend);
45
46 lua_pushstring(lua, ip.c_str());
47 lua_pushstring(lua, domain.c_str());
48
49
50 lua_newtable(lua);
51 int c = 0;
52 for(vector<DNSResourceRecord>::const_iterator i=nsset.begin();i!=nsset.end();++i) {
53 c++;
54 lua_pushnumber(lua, c);
55
56 DNSResourceRecord rr;
57
58 rr.qtype = i->qtype;
59 rr.qclass = i->qclass;
e18dbde1
BH
60 rr.ttl = i->ttl;
61 rr.auth = i->auth;
62 rr.content = i->content;
63
64 dnsrr_to_table(lua, &rr);
65 lua_settable(lua, -3);
66 }
67
68 if(lua_pcall(lua, 3, 2, f_lua_exec_error) != 0) {
69 string e = backend_name + lua_tostring(lua, -1);
70 lua_pop(lua, 1);
71
72 throw runtime_error(e);
73 return false;
74 }
75
76 size_t returnedwhat = lua_type(lua, -1);
77 bool ok = false;
78
79 if (returnedwhat == LUA_TBOOLEAN)
80 ok = lua_toboolean(lua, -1);
81
82 lua_pop(lua, 1);
83
84 string a = "";
85 returnedwhat = lua_type(lua, -1);
86 if (returnedwhat == LUA_TSTRING)
87 a = lua_tostring(lua, -1);
88 lua_pop(lua, 1);
89
90 if (ok) {
91 *account = a;
92 *db = this;
93 }
94
95 if (logging)
96 L << Logger::Info << backend_name << "(superMasterBackend) END" << endl;
97
98 return ok;
99}
100
719f9024 101bool LUABackend::createSlaveDomain(const string &ip, const string &domain, const string &nameserver, const string &account) {
e18dbde1
BH
102
103 if (f_lua_createslavedomain == 0)
104 return false;
105
106 if (logging)
107 L << Logger::Info << backend_name << "(createSlaveDomain) BEGIN" << endl;
108
109 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_createslavedomain);
110
111 lua_pushstring(lua, ip.c_str());
112 lua_pushstring(lua, domain.c_str());
113 lua_pushstring(lua, account.c_str());
114
115 if(lua_pcall(lua, 3, 1, f_lua_exec_error) != 0) {
116 string e = backend_name + lua_tostring(lua, -1);
117 lua_pop(lua, 1);
118
119 throw runtime_error(e);
120 return false;
121 }
122
123 size_t returnedwhat = lua_type(lua, -1);
124 bool ok = false;
125
126 if (returnedwhat == LUA_TBOOLEAN)
127 ok = lua_toboolean(lua, -1);
128
129 lua_pop(lua, 1);
130
131 if (logging)
132 L << Logger::Info << backend_name << "(createSlaveDomain) END" << endl;
133
134 return ok;
135}