-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimal.cc
323 lines (231 loc) · 9.23 KB
/
minimal.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Copyright (C) 2011-2013 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
by the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "luabackend.hh"
#include "pdns/logger.hh"
#include "pdns/arguments.hh"
/* FIRST PART */
bool LUABackend::first = true;
unsigned int LUABackend::backend_count = 0;
unsigned int LUABackend::backend_numbers = 0;
pthread_mutex_t LUABackend::lock_the_lock;
lua_State *LUABackend::lua;
DNSPacket *LUABackend::dnspacket;
int LUABackend::f_lua_exec_error;
//minimal functions....
int LUABackend::f_lua_list;
int LUABackend::f_lua_lookup;
int LUABackend::f_lua_get;
int LUABackend::f_lua_getsoa;
//master functions....
int LUABackend::f_lua_getupdatedmasters;
int LUABackend::f_lua_setnotifed;
//slave functions....
int LUABackend::f_lua_getdomaininfo;
int LUABackend::f_lua_ismaster;
int LUABackend::f_lua_getunfreshslaveinfos;
int LUABackend::f_lua_setfresh;
int LUABackend::f_lua_starttransaction;
int LUABackend::f_lua_committransaction;
int LUABackend::f_lua_aborttransaction;
int LUABackend::f_lua_feedrecord;
//supermaster functions....
int LUABackend::f_lua_supermasterbackend;
int LUABackend::f_lua_createslavedomain;
//reload
int LUABackend::f_lua_reload;
//rediscover
int LUABackend::f_lua_rediscover;
//dnssec
int LUABackend::f_lua_alsonotifies;
int LUABackend::f_lua_getdomainmetadata;
int LUABackend::f_lua_setdomainmetadata;
int LUABackend::f_lua_getdomainkeys;
int LUABackend::f_lua_removedomainkey;
int LUABackend::f_lua_activatedomainkey;
int LUABackend::f_lua_deactivatedomainkey;
int LUABackend::f_lua_updatedomainkey;
int LUABackend::f_lua_gettsigkey;
int LUABackend::f_lua_adddomainkey;
int LUABackend::f_lua_getbeforeandafternamesabsolute;
int LUABackend::f_lua_updatednssecorderandauthabsolute;
int LUABackend::f_lua_updatednssecorderandauth;
bool LUABackend::logging = false;
LUABackend::LUABackend(const string &suffix) {
setArgPrefix("lua" + suffix);
// Make sure only one (the first) backend instance is initializing static things
Lock l(&lock_the_lock);
backend_count++;
backend_numbers++;
backend_mynumber = backend_numbers;
backend_name.clear();
backend_pid = pthread_self();
backend_name = "[LUABackend " + uitoa(backend_numbers) +"] ";
if (!first)
return;
first = false;
//see lua_functions.cc
init(&lua);
}
LUABackend::~LUABackend() {
// Make sure only one (the first) backend instance is initializing static things
Lock l(&lock_the_lock);
backend_count--;
if (logging)
L << Logger::Info << backend_name << "backend number: '" << backend_mynumber << "' - ending!" << endl;
if (backend_count == 0) {
lua_close(lua);
}
}
bool LUABackend::list(const string &target, int domain_id) {
if ((errno = pthread_mutex_lock(&lock_the_lock)))
throw runtime_error("(getsoa) Unable to acquiring lock: " + stringerror());
if (logging)
L << Logger::Info << backend_name << "(list) BEGIN target: '" << target << "' domain_id: '" << domain_id << "'" << endl;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_list);
lua_pushlstring(lua, target.c_str(), target.size());
lua_pushnumber(lua, domain_id);
if(lua_pcall(lua, 2, 1, f_lua_exec_error) != 0) {
pthread_mutex_unlock(&lock_the_lock);
string e = backend_name + "(list) " + lua_tostring(lua, -1);
lua_pop(lua, 1);
throw runtime_error(e);
return false;
}
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 - " << (ok ? "true" : "false") << endl;
if (!ok)
pthread_mutex_unlock(&lock_the_lock);
return ok;
}
void LUABackend::lookup(const QType &qtype, const string &qname, DNSPacket *p, int domain_id) {
if ((errno = pthread_mutex_lock(&lock_the_lock)))
throw runtime_error("(getsoa) Unable to acquiring lock: " + stringerror());
if (logging)
L << Logger::Info << backend_name << "(lookup) BEGIN qtype: '" << qtype.getName().c_str() << "' qname: '" << qname << "' domain_id: '" << domain_id << "'" << endl;
dnspacket = p;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_lookup);
lua_pushlstring(lua, qtype.getName().c_str(), qtype.getName().size());
lua_pushlstring(lua, qname.c_str(), qname.size());
lua_pushnumber(lua, domain_id);
if(lua_pcall(lua, 3, 0, f_lua_exec_error) != 0) {
pthread_mutex_unlock(&lock_the_lock);
string e = backend_name + "(lookup) " + 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) {
pthread_mutex_unlock(&lock_the_lock);
string e = backend_name + "(get) " + lua_tostring(lua, -1);
lua_pop(lua, 1);
throw runtime_error(e);
return false;
}
rr.content.clear();
size_t returnedwhat = lua_type(lua, -1);
if (returnedwhat == LUA_TTABLE) {
string qt;
if (gVFT::getValueFromTable(lua, "type", qt))
rr.qtype = qt;
gVFT::getValueFromTable(lua, "name", rr.qname);
gVFT::getValueFromTable(lua, "domain_id", rr.domain_id);
gVFT::getValueFromTable(lua, "last_modified", rr.last_modified);
gVFT::getValueFromTable(lua, "priority", rr.priority);
if (!gVFT::getValueFromTable(lua, "auth", rr.auth))
rr.auth = 1;
gVFT::getValueFromTable(lua, "ttl", rr.ttl);
gVFT::getValueFromTable(lua, "content", rr.content);
}
lua_pop(lua, 1);
bool more = !rr.content.empty();
if (logging)
L << Logger::Info << backend_name << "(get) END - " << (more ? "more" : "no more") << " record(s) to follow" << endl;
if (!more)
pthread_mutex_unlock(&lock_the_lock);
return more;
}
bool LUABackend::getSOA(const string &name, SOAData &soadata, DNSPacket *p) {
Lock l(&lock_the_lock);
if (logging)
L << Logger::Info << backend_name << "(getsoa) BEGIN name: '" << name << "'" << endl;
dnspacket = p;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_getsoa);
lua_pushlstring(lua, name.c_str(), name.size());
if (lua_pcall(lua, 1, 1, f_lua_exec_error) != 0) {
string e = backend_name + "(getsoa) " + 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 );
if (logging)
L << Logger::Info << backend_name << "(getsoa) END with NO soa" << endl;
return false;
}
soadata.db = this;
soadata.serial = 0;
gVFT::getValueFromTable(lua, "serial", soadata.serial);
if (soadata.serial == 0) {
lua_pop(lua, 1 );
L << Logger::Error << backend_name << "(getsoa) ERROR - No serialnumber for the domain: '" << name << "'" << endl;
return false;
}
gVFT::getValueFromTable(lua, "refresh", soadata.refresh);
gVFT::getValueFromTable(lua, "retry", soadata.retry);
gVFT::getValueFromTable(lua, "expire", soadata.expire);
gVFT::getValueFromTable(lua, "default_ttl", soadata.default_ttl);
gVFT::getValueFromTable(lua, "domain_id", soadata.domain_id);
gVFT::getValueFromTable(lua, "ttl", soadata.ttl);
if (soadata.ttl == 0 && soadata.default_ttl > 0)
soadata.ttl = soadata.default_ttl;
if (soadata.ttl == 0) {
lua_pop(lua, 1);
L << Logger::Error << backend_name << "(getsoa) ERROR - No ttl for the domain: '" << name << "'" << endl;
return false;
}
if (!gVFT::getValueFromTable(lua, "nameserver", soadata.nameserver)) {
soadata.nameserver = arg()["default-soa-name"];
if (soadata.nameserver.empty()) {
lua_pop(lua, 1);
L << Logger::Error << backend_name << "(getsoa) ERROR - SOA Record is missing nameserver for the domain: '" << name << "'" << endl;
return false;
}
}
if (!gVFT::getValueFromTable(lua, "hostmaster", soadata.hostmaster))
soadata.hostmaster = "hostmaster." + name;
if (!gVFT::getValueFromTable(lua, "qname", soadata.qname))
soadata.qname = name;
lua_pop(lua, 1);
if (logging)
L << Logger::Info << backend_name << "(getsoa) END with soa" << endl;
return true;
}