]>
Commit | Line | Data |
---|---|---|
e18dbde1 | 1 | /* |
12471842 PL |
2 | * This file is part of PowerDNS or dnsdist. |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * originally authored by Fredrik Danerklint | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of version 2 of the GNU General Public License as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * In addition, for the avoidance of any doubt, permission is granted to | |
11 | * link this program with OpenSSL and to (re)distribute the binaries | |
12 | * produced as the result of such linking. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
22 | */ | |
870a0fe4 AT |
23 | #ifdef HAVE_CONFIG_H |
24 | #include "config.h" | |
25 | #endif | |
e18dbde1 BH |
26 | #include "luabackend.hh" |
27 | ||
28 | #include "pdns/logger.hh" | |
29 | #include "pdns/arguments.hh" | |
30 | ||
31 | string LUABackend::my_getArg(string a) { | |
32 | return getArg(a); | |
33 | } | |
34 | ||
35 | bool LUABackend::my_mustDo(string a) { | |
36 | return mustDo(a); | |
37 | } | |
38 | ||
96097f35 | 39 | bool LUABackend::my_isEmpty(string a) { |
63b0003d | 40 | return ::arg().isEmpty(string(LUABACKEND_PREFIX)+"-"+a); |
96097f35 | 41 | } |
42 | ||
e18dbde1 BH |
43 | bool LUABackend::domaininfo_from_table(DomainInfo *di) { |
44 | ||
45 | di->backend = NULL; | |
46 | ||
47 | if (!getValueFromTable(lua, "id", di->id)) | |
48 | return false; | |
49 | ||
50 | if (!getValueFromTable(lua, "zone", di->zone)) | |
51 | return false; | |
52 | ||
53 | if (!getValueFromTable(lua, "serial", di->serial)) | |
54 | return false; | |
55 | ||
56 | getValueFromTable(lua, "notified_serial", di->notified_serial); | |
57 | getValueFromTable(lua, "last_check", di->last_check); | |
58 | ||
59 | di->kind = DomainInfo::Native; | |
60 | ||
61 | string kind; | |
62 | if (getValueFromTable(lua, "kind", kind)) { | |
63 | ||
64 | if (kind == "MASTER") | |
65 | di->kind = DomainInfo::Master; | |
66 | else if (kind == "SLAVE") | |
67 | di->kind = DomainInfo::Slave; | |
68 | } | |
69 | ||
70 | lua_pushstring(lua, "masters"); | |
71 | lua_gettable(lua, -2); | |
72 | ||
73 | if(!lua_isnil(lua, -1)) { | |
74 | lua_pushnil(lua); | |
664716a3 | 75 | const char *value; |
e18dbde1 BH |
76 | while (lua_next(lua, -2)) { |
77 | value = lua_tostring(lua, -1); | |
78 | lua_pop(lua,1); | |
d622042f | 79 | di->masters.push_back(ComboAddress(value, 53)); |
e18dbde1 BH |
80 | } |
81 | } | |
82 | ||
83 | lua_pop(lua, 1); | |
84 | ||
85 | di->backend = this; | |
86 | ||
87 | return true; | |
88 | } | |
89 | ||
90 | void LUABackend::domains_from_table(vector<DomainInfo>* domains, const char *f_name) { | |
91 | lua_pushnil(lua); | |
92 | ||
e18dbde1 BH |
93 | size_t returnedwhat; |
94 | ||
95 | while (lua_next(lua, -2)) { | |
96 | returnedwhat = lua_type(lua, -1); | |
97 | if (returnedwhat == LUA_TTABLE) { | |
98 | DomainInfo di; | |
99 | ||
100 | if (domaininfo_from_table(&di)) | |
101 | domains->push_back(di); | |
102 | } | |
103 | ||
104 | lua_pop(lua,1); | |
e18dbde1 BH |
105 | } |
106 | } | |
107 | ||
108 | ||
690b86b7 | 109 | void LUABackend::dnsrr_to_table(lua_State *lua_state, const DNSResourceRecord *rr) { |
e18dbde1 | 110 | |
690b86b7 | 111 | lua_newtable(lua_state); |
e18dbde1 | 112 | |
690b86b7 OM |
113 | lua_pushliteral(lua_state, "qtype"); |
114 | lua_pushstring(lua_state, rr->qtype.getName().c_str()); | |
115 | lua_settable(lua_state, -3); | |
e18dbde1 | 116 | |
690b86b7 OM |
117 | lua_pushliteral(lua_state, "qclass"); |
118 | lua_pushinteger(lua_state, rr->qclass); | |
119 | lua_settable(lua_state, -3); | |
e18dbde1 | 120 | |
690b86b7 OM |
121 | lua_pushliteral(lua_state, "ttl"); |
122 | lua_pushinteger(lua_state, rr->ttl); | |
123 | lua_settable(lua_state, -3); | |
e18dbde1 | 124 | |
690b86b7 OM |
125 | lua_pushliteral(lua_state, "auth"); |
126 | lua_pushboolean(lua_state, rr->auth); | |
127 | lua_settable(lua_state, -3); | |
e18dbde1 | 128 | |
690b86b7 OM |
129 | lua_pushliteral(lua_state, "content"); |
130 | lua_pushstring(lua_state, rr->content.c_str()); | |
131 | lua_settable(lua_state, -3); | |
e18dbde1 | 132 | |
664716a3 | 133 | } |
68df5ed6 | 134 | |
690b86b7 | 135 | bool LUABackend::dnsrr_from_table(lua_State *lua_state, DNSResourceRecord &rr) { |
68df5ed6 | 136 | |
137 | bool got_content = false; | |
138 | string qt_name; | |
139 | uint16_t qt_code; | |
140 | ||
141 | // look for qname key first | |
142 | // try name key if qname wasn't set | |
690b86b7 OM |
143 | if (!getValueFromTable(lua_state, "qname", rr.qname)) |
144 | getValueFromTable(lua_state, "name", rr.qname); | |
68df5ed6 | 145 | |
146 | // qtype is either a table, string or number | |
147 | // when it's a table prefer the code key | |
690b86b7 OM |
148 | lua_pushliteral(lua_state, "qtype"); |
149 | lua_gettable(lua_state, -2); | |
150 | size_t returnedwhat = lua_type(lua_state, -1); | |
68df5ed6 | 151 | if (LUA_TTABLE == returnedwhat) { |
690b86b7 | 152 | if (getValueFromTable(lua_state, "code", qt_code)) |
68df5ed6 | 153 | rr.qtype = qt_code; |
154 | else | |
690b86b7 | 155 | if (getValueFromTable(lua_state, "name", qt_name)) |
68df5ed6 | 156 | rr.qtype = qt_name; |
690b86b7 | 157 | lua_pop(lua_state, 1); |
68df5ed6 | 158 | } else if (LUA_TNUMBER == returnedwhat) { |
690b86b7 OM |
159 | lua_pop(lua_state, 1); |
160 | if (getValueFromTable(lua_state, "qtype", qt_code)) | |
68df5ed6 | 161 | rr.qtype = qt_code; |
162 | } else { | |
690b86b7 OM |
163 | lua_pop(lua_state, 1); |
164 | if (getValueFromTable(lua_state, "qtype", qt_name)) | |
68df5ed6 | 165 | rr.qtype = qt_name; |
df1f9438 | 166 | else // fallback to old key for tests to pass |
690b86b7 | 167 | if (getValueFromTable(lua_state, "type", qt_name)) |
df1f9438 | 168 | rr.qtype = qt_name; |
68df5ed6 | 169 | } |
170 | ||
690b86b7 OM |
171 | getValueFromTable(lua_state, "qclass", rr.qclass); |
172 | getValueFromTable(lua_state, "domain_id", rr.domain_id); | |
173 | getValueFromTable(lua_state, "auth", rr.auth); | |
174 | getValueFromTable(lua_state, "last_modified", rr.last_modified); | |
68df5ed6 | 175 | |
690b86b7 OM |
176 | getValueFromTable(lua_state, "ttl", rr.ttl); |
177 | got_content = getValueFromTable(lua_state, "content", rr.content); | |
178 | getValueFromTable(lua_state, "scopeMask", rr.scopeMask); | |
68df5ed6 | 179 | |
180 | return got_content; | |
181 | ||
182 | } |