]> git.ipfire.org Git - thirdparty/pdns.git/blob - modules/luabackend/dnssec.cc
use DNSName for before and afternames in the other backends
[thirdparty/pdns.git] / modules / luabackend / dnssec.cc
1 /*
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 */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "luabackend.hh"
27
28 #include "pdns/logger.hh"
29 #include "pdns/arguments.hh"
30
31
32 bool LUABackend::updateDNSSECOrderAndAuth(uint32_t domain_id, const DNSName& zonename, const DNSName& qname, bool auth) {
33
34 if(f_lua_updatednssecorderandauth == 0) {
35
36 if(logging)
37 L << Logger::Info << backend_name << "(updateDNSSECOrderAndAuth) domain_id: '" << domain_id << "' zonename: '" << zonename << "' qname: '" << qname << "' auth: '" << auth << "'" << endl;
38
39 string ins=qname.makeRelative(zonename).makeLowerCase().labelReverse().toString(" ", false);
40 return this->updateDNSSECOrderAndAuthAbsolute(domain_id, qname, ins, auth);
41 }
42
43 if(logging)
44 L << Logger::Info << backend_name << "(updateDNSSECOrderAndAuth) BEGIN domain_id: '" << domain_id << "' zonename: '" << zonename << "' qname: '" << qname << "' auth: '" << auth << "'" << endl;
45
46 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_updatednssecorderandauth);
47
48 lua_pushinteger(lua, domain_id);
49 lua_pushstring(lua, zonename.toString().c_str());
50 lua_pushstring(lua, qname.toString().c_str());
51 lua_pushboolean(lua, auth);
52
53 if(lua_pcall(lua, 4, 1, f_lua_exec_error) != 0) {
54 string e = backend_name + lua_tostring(lua, -1);
55 lua_pop(lua, 1);
56
57 throw runtime_error(e);
58 return false;
59 }
60
61 size_t returnedwhat = lua_type(lua, -1);
62 bool ok = false;
63
64 if (returnedwhat == LUA_TBOOLEAN)
65 ok = lua_toboolean(lua, -1);
66
67 lua_pop(lua, 1);
68
69 if(logging)
70 L << Logger::Info << backend_name << "(updateDNSSECOrderAndAuth) END" << endl;
71
72 return ok;
73 }
74
75 bool LUABackend::updateDNSSECOrderNameAndAuth(unsigned int, DNSName const&, DNSName const&, DNSName const&, bool, unsigned short)
76 {
77 return false;
78 }
79
80 bool LUABackend::updateDNSSECOrderAndAuthAbsolute(uint32_t domain_id, const DNSName& qname, const std::string& ordername, bool auth) {
81
82 if(f_lua_updatednssecorderandauthabsolute == 0)
83 return false;
84
85 if(logging)
86 L << Logger::Info << backend_name << "(updateDNSSECOrderAndAuthAbsolute) BEGIN domain_id: '" << domain_id << "' qname: '" << qname << "' ordername: '" << ordername << "' auth: '" << auth << "'" << endl;
87
88 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_updatednssecorderandauthabsolute);
89
90 lua_pushinteger(lua, domain_id);
91 lua_pushstring(lua, qname.toString().c_str());
92 lua_pushstring(lua, ordername.c_str());
93 lua_pushboolean(lua, auth);
94
95 if(lua_pcall(lua, 4, 1, f_lua_exec_error) != 0) {
96 string e = backend_name + lua_tostring(lua, -1);
97 lua_pop(lua, 1);
98
99 throw runtime_error(e);
100 return false;
101 }
102
103 size_t returnedwhat = lua_type(lua, -1);
104 bool ok = false;
105
106 if (returnedwhat == LUA_TBOOLEAN)
107 ok = lua_toboolean(lua, -1);
108
109 lua_pop(lua, 1);
110
111 if(logging)
112 L << Logger::Info << backend_name << "(updateDNSSECOrderAndAuthAbsolute) END" << endl;
113
114 return ok;
115 }
116
117 bool LUABackend::getBeforeAndAfterNamesAbsolute(uint32_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after) {
118
119 if(f_lua_getbeforeandafternamesabsolute == 0)
120 return false;
121
122 unhashed.clear();
123 before.clear();
124 after.clear();
125
126 if(logging)
127 L << Logger::Info << backend_name << "(getBeforeAndAfterNamesAbsolute) BEGIN id: '" << id << "' qname: '" << qname << "'" << endl;
128
129 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_updatednssecorderandauthabsolute);
130
131 lua_pushinteger(lua, id);
132 lua_pushstring(lua, qname.toString().c_str());
133
134 if(lua_pcall(lua, 2, 3, f_lua_exec_error) != 0) {
135 string e = backend_name + lua_tostring(lua, -1);
136 lua_pop(lua, 1);
137
138 throw runtime_error(e);
139 return false;
140 }
141
142 size_t returnedwhat = lua_type(lua, -1);
143 bool ok = returnedwhat == LUA_TSTRING;
144
145 if (!ok) {
146 if(logging)
147 L << Logger::Info << backend_name << "(getBeforeAndAfterNamesAbsolute) ERROR!" << endl;
148
149 return false;
150 }
151
152 //will this be correct since we are poping one at the time?
153 unhashed = DNSName(lua_tostring(lua, -1));
154 lua_pop(lua, 1);
155
156 returnedwhat = lua_type(lua, -1);
157 ok = (returnedwhat == LUA_TSTRING) && ok;
158
159 before = DNSName(lua_tostring(lua, -1));
160 lua_pop(lua, 1);
161
162 returnedwhat = lua_type(lua, -1);
163 ok = (returnedwhat == LUA_TSTRING) && ok;
164
165 after = DNSName(lua_tostring(lua, -1));
166 lua_pop(lua, 1);
167
168 if(logging)
169 L << Logger::Info << backend_name << "(getBeforeAndAfterNamesAbsolute) END unhashed: '" << unhashed << "' before: '" << before << "' after: '" << after << "' " << endl;
170
171 return ok;
172 }
173
174 bool LUABackend::updateDomainKey(const DNSName& name, unsigned int &id, bool toowhat ) {
175
176 if(f_lua_updatedomainkey == 0)
177 return false;
178
179 if(logging)
180 L << Logger::Info << backend_name << "(updateDomainKey) BEGIN name: '" << name << "' id: '" << id << "' toowhat: '" << toowhat << "'" << endl;
181
182 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_updatedomainkey);
183
184 lua_pushstring(lua, name.toString().c_str());
185 lua_pushinteger(lua, id);
186 lua_pushboolean(lua, toowhat);
187
188 if(lua_pcall(lua, 3, 1, f_lua_exec_error) != 0) {
189 string e = backend_name + lua_tostring(lua, -1);
190 lua_pop(lua, 1);
191
192 throw runtime_error(e);
193 return false;
194 }
195
196 size_t returnedwhat = lua_type(lua, -1);
197 bool ok = false;
198
199 if (returnedwhat == LUA_TBOOLEAN)
200 ok = lua_toboolean(lua, -1);
201
202 lua_pop(lua, 1);
203
204 if(logging)
205 L << Logger::Info << backend_name << "(updateDomainKey) END" << endl;
206
207 return ok;
208 }
209
210 bool LUABackend::activateDomainKey(const DNSName& name, unsigned int id) {
211
212 if(f_lua_activatedomainkey == 0)
213 return updateDomainKey(name, id, true);
214
215 if(logging)
216 L << Logger::Info << backend_name << "(activateDomainKey) BEGIN name: '" << name << "' id: '" << id << endl;
217
218 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_activatedomainkey);
219
220 lua_pushstring(lua, name.toString().c_str());
221 lua_pushinteger(lua, id);
222
223 if(lua_pcall(lua, 2, 1, f_lua_exec_error) != 0) {
224 string e = backend_name + lua_tostring(lua, -1);
225 lua_pop(lua, 1);
226
227 throw runtime_error(e);
228 return false;
229 }
230
231 size_t returnedwhat = lua_type(lua, -1);
232 bool ok = false;
233
234 if (returnedwhat == LUA_TBOOLEAN)
235 ok = lua_toboolean(lua, -1);
236
237 lua_pop(lua, 1);
238
239 if(logging)
240 L << Logger::Info << backend_name << "(activateDomainKey) END" << endl;
241
242 return ok;
243 }
244
245 bool LUABackend::deactivateDomainKey(const DNSName& name, unsigned int id) {
246
247 if(f_lua_deactivatedomainkey == 0)
248 return updateDomainKey(name, id, false);
249
250 if(logging)
251 L << Logger::Info << backend_name << "(deactivateDomainKey) BEGIN name: '" << name << "' id: '" << id << endl;
252
253 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_deactivatedomainkey);
254
255 lua_pushstring(lua, name.toString().c_str());
256 lua_pushinteger(lua, id);
257
258 if(lua_pcall(lua, 2, 1, f_lua_exec_error) != 0) {
259 string e = backend_name + lua_tostring(lua, -1);
260 lua_pop(lua, 1);
261
262 throw runtime_error(e);
263 return false;
264 }
265
266 size_t returnedwhat = lua_type(lua, -1);
267 bool ok = false;
268
269 if (returnedwhat == LUA_TBOOLEAN)
270 ok = lua_toboolean(lua, -1);
271
272 lua_pop(lua, 1);
273
274 if(logging)
275 L << Logger::Info << backend_name << "(deactivateDomainKey) END" << endl;
276
277 return ok;
278 }
279
280 bool LUABackend::removeDomainKey(const DNSName& name, unsigned int id) {
281
282 if(f_lua_removedomainkey == 0)
283 return false;
284
285 if(logging)
286 L << Logger::Info << backend_name << "(removeDomainKey) BEGIN name: '" << name << "' id: '" << id << endl;
287
288 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_removedomainkey);
289
290 lua_pushstring(lua, name.toString().c_str());
291 lua_pushinteger(lua, id);
292
293 if(lua_pcall(lua, 2, 1, f_lua_exec_error) != 0) {
294 string e = backend_name + lua_tostring(lua, -1);
295 lua_pop(lua, 1);
296
297 throw runtime_error(e);
298 return false;
299 }
300
301 size_t returnedwhat = lua_type(lua, -1);
302 bool ok = false;
303
304 if (returnedwhat == LUA_TBOOLEAN)
305 ok = lua_toboolean(lua, -1);
306
307 lua_pop(lua, 1);
308
309 if(logging)
310 L << Logger::Info << backend_name << "(removeDomainKey) END" << endl;
311
312 return ok;
313 }
314
315 bool LUABackend::addDomainKey(const DNSName& name, const KeyData& key, int64_t& id) {
316 // there is no logging function in pdnsutil when running this routine?
317
318 //key = id, flags, active, content
319
320 if(f_lua_adddomainkey == 0)
321 return false;
322
323 if(logging)
324 //L << Logger::Info << backend_name << "(addDomainKey) BEGIN name: '" << name << "' id: '" << id << endl;
325 cerr << backend_name << "(addDomainKey) BEGIN name: '" << name << endl;
326
327 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_adddomainkey);
328
329 lua_pushstring(lua, name.toString().c_str());
330
331 lua_newtable(lua);
332
333 lua_pushliteral(lua, "flags");
334 lua_pushinteger(lua, key.flags);
335 lua_settable(lua, -3);
336
337 lua_pushliteral(lua, "active");
338 lua_pushboolean(lua, key.active);
339 lua_settable(lua, -3);
340
341 lua_pushliteral(lua, "content");
342 lua_pushstring(lua, key.content.c_str());
343 lua_settable(lua, -3);
344
345 if(lua_pcall(lua, 2, 1, f_lua_exec_error) != 0) {
346 string e = backend_name + lua_tostring(lua, -1);
347 lua_pop(lua, 1);
348
349 throw runtime_error(e);
350 }
351
352 size_t returnedwhat = lua_type(lua, -1);
353 int ok = -1;
354
355 if (returnedwhat == LUA_TNUMBER)
356 ok = lua_tonumber(lua, -1);
357
358 lua_pop(lua, 1);
359
360 if(logging)
361 cerr << backend_name << "(addDomainKey) END" << endl;
362
363 return ok >= 0;
364 }
365
366 bool LUABackend::getDomainKeys(const DNSName& name, unsigned int kind, std::vector<KeyData>& keys) {
367 //what is kind used for?
368
369 if(f_lua_getdomainkeys == 0)
370 return false;
371
372 if(logging)
373 L << Logger::Info << backend_name << "(getDomainKeys) BEGIN name: '" << name << "' kind: '" << kind << endl;
374
375 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_getdomainkeys);
376
377 lua_pushstring(lua, name.toString().c_str());
378 lua_pushinteger(lua, kind);
379
380 if(lua_pcall(lua, 2, 1, f_lua_exec_error) != 0) {
381 string e = backend_name + lua_tostring(lua, -1);
382 lua_pop(lua, 1);
383
384 throw runtime_error(e);
385 return false;
386 }
387
388 size_t returnedwhat = lua_type(lua, -1);
389
390 if (returnedwhat != LUA_TTABLE) {
391 lua_pop(lua, 1);
392 if(logging)
393 L << Logger::Info << backend_name << "(getDomainKeys) ERROR!" << endl;
394
395 return false;
396 }
397
398 lua_pushnil(lua);
399
400 int j = 0;
401
402 while (lua_next(lua, -2)) {
403 returnedwhat = lua_type(lua, -1);
404 if (returnedwhat == LUA_TTABLE) {
405 KeyData kd;
406 bool i,f,a,c = false;
407
408 i = getValueFromTable(lua, "id", kd.id);
409 f = getValueFromTable(lua, "flags", kd.flags);
410 a = getValueFromTable(lua, "active", kd.active);
411 c = getValueFromTable(lua, "content", kd.content);
412
413 if (i && f && a && c) {
414 j++;
415 keys.push_back(kd);
416 }
417 }
418
419 lua_pop(lua,1);
420 }
421
422 if(logging)
423 L << Logger::Info << backend_name << "(getDomainKeys) END" << endl;
424
425 return j > 0;
426 }
427
428 bool LUABackend::getTSIGKey(const DNSName& name, DNSName* algorithm, string* content) {
429
430 if(f_lua_gettsigkey == 0)
431 return false;
432
433 if(logging)
434 L << Logger::Info << backend_name << "(getTSIGKey) BEGIN name: '" << name << "'" << endl;
435
436 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_gettsigkey);
437
438 lua_pushstring(lua, name.toString().c_str());
439
440 if(lua_pcall(lua, 1, 2, f_lua_exec_error) != 0) {
441 string e = backend_name + lua_tostring(lua, -1);
442 lua_pop(lua, 1);
443
444 throw runtime_error(e);
445 return false;
446 }
447
448 if ( (lua_type(lua, -1) != LUA_TSTRING) && (lua_type(lua, -2) != LUA_TSTRING) ) {
449 lua_pop(lua, 2);
450 if(logging)
451 L << Logger::Info << backend_name << "(getTSIGKey) ERROR" << endl;
452 return false;
453 }
454
455 string a,c = "";
456
457 a = lua_tostring(lua, -1);
458 lua_pop(lua, 1);
459
460 c = lua_tostring(lua, -1);
461 lua_pop(lua, 1);
462
463 *algorithm = DNSName(a);
464 *content = c;
465
466 if(logging)
467 L << Logger::Info << backend_name << "(getTSIGKey) END" << endl;
468
469 return true;
470 }
471
472 bool LUABackend::setDomainMetadata(const DNSName& name, const std::string& kind, const std::vector<std::string>& meta) {
473
474 if(f_lua_setdomainmetadata == 0)
475 return false;
476
477 if(logging)
478 L << Logger::Info << backend_name << "(setDomainMetadata) BEGIN name: '" << name << "' kind: '" << kind << "'" << endl;
479
480 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_setdomainmetadata);
481
482 lua_pushstring(lua, name.toString().c_str());
483 lua_pushstring(lua, kind.c_str());
484
485 lua_newtable(lua);
486
487 std::vector<std::string>::const_iterator i;
488
489 int c = 0;
490
491 for(i = meta.begin(); i<meta.end(); i++ ) {
492 c++;
493 lua_pushinteger(lua, c);
494 lua_pushstring(lua, i->c_str());
495 lua_settable(lua, -3);
496 }
497
498 if(lua_pcall(lua, 3, 1, f_lua_exec_error) != 0) {
499 string e = backend_name + lua_tostring(lua, -1);
500 lua_pop(lua, 1);
501
502 throw runtime_error(e);
503 return false;
504 }
505
506 size_t returnedwhat = lua_type(lua, -1);
507 bool ok = false;
508
509 if (returnedwhat == LUA_TBOOLEAN)
510 ok = lua_toboolean(lua, -1);
511
512 lua_pop(lua, 1);
513
514 if(logging)
515 L << Logger::Info << backend_name << "(setDomainMetadata) END" << endl;
516
517 return ok;
518
519 }
520
521 bool LUABackend::getDomainMetadata(const DNSName& name, const std::string& kind, std::vector<std::string>& meta) {
522 if(f_lua_getdomainmetadata == 0)
523 return false;
524
525 if(logging)
526 L << Logger::Info << backend_name << "(getDomainMetadata) BEGIN name: '" << name << "' kind: '" << kind << "'" << endl;
527
528 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_getdomainmetadata);
529
530 lua_pushstring(lua, name.toString().c_str());
531 lua_pushstring(lua, kind.c_str());
532
533 if(lua_pcall(lua, 2, 1, f_lua_exec_error) != 0) {
534 string e = backend_name + lua_tostring(lua, -1);
535 lua_pop(lua, 1);
536
537 throw runtime_error(e);
538 return false;
539 }
540
541 if (lua_type(lua, -1) != LUA_TTABLE)
542 return false;
543
544 lua_pushnil(lua);
545
546 int j = 0;
547 size_t returnedwhat;
548
549 while (lua_next(lua, -2)) {
550 returnedwhat = lua_type(lua, -1);
551 if (returnedwhat == LUA_TSTRING) {
552 j++;
553 meta.push_back(lua_tostring(lua, -1));
554 }
555
556 lua_pop(lua,1);
557 }
558
559 if(logging)
560 L << Logger::Info << backend_name << "(getDomainMetadata) END" << endl;
561
562 return j > 0;
563
564 }
565
566 void LUABackend::alsoNotifies(const DNSName& domain, set<string> *ips) {
567
568 if(f_lua_alsonotifies == 0)
569 return;
570
571 if(logging)
572 L << Logger::Info << backend_name << "(alsonotifies) BEGIN domain: '" << domain << "'" << endl;
573
574 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_alsonotifies);
575
576 lua_pushstring(lua, domain.toString().c_str());
577
578 if(lua_pcall(lua, 1, 1, f_lua_exec_error) != 0) {
579 string e = backend_name + lua_tostring(lua, -1);
580 lua_pop(lua, 1);
581
582 throw runtime_error(e);
583 return;
584 }
585
586 if (lua_type(lua, -1) != LUA_TTABLE)
587 return;
588
589 lua_pushnil(lua);
590
591 size_t returnedwhat;
592
593 while (lua_next(lua, -2)) {
594 returnedwhat = lua_type(lua, -1);
595 if (returnedwhat == LUA_TSTRING) {
596 ips->insert(lua_tostring(lua, -1));
597 }
598
599 lua_pop(lua,1);
600 }
601
602 if(logging)
603 L << Logger::Info << backend_name << "(alsoNotifies) END" << endl;
604
605 return;
606
607 }