]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/zoneparser-tng.cc
Merge pull request #9134 from omoerbeek/secpoll-cleanup
[thirdparty/pdns.git] / pdns / zoneparser-tng.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "ascii.hh"
26 #include "dnsparser.hh"
27 #include "sstuff.hh"
28 #include "misc.hh"
29 #include "dnswriter.hh"
30 #include "dnsrecords.hh"
31 #include "misc.hh"
32 #include <fstream>
33 #include "dns.hh"
34 #include "zoneparser-tng.hh"
35 #include <deque>
36 #include <boost/algorithm/string.hpp>
37 #include <system_error>
38
39 static string g_INstr("IN");
40
41 ZoneParserTNG::ZoneParserTNG(const string& fname, const DNSName& zname, const string& reldir) : d_reldir(reldir),
42 d_zonename(zname), d_defaultttl(3600),
43 d_templatecounter(0), d_templatestop(0),
44 d_templatestep(0), d_havedollarttl(false){
45 stackFile(fname);
46 }
47
48 ZoneParserTNG::ZoneParserTNG(const vector<string> zonedata, const DNSName& zname):
49 d_zonename(zname), d_zonedata(zonedata), d_defaultttl(3600),
50 d_templatecounter(0), d_templatestop(0), d_templatestep(0),
51 d_havedollarttl(false), d_fromfile(false)
52 {
53 d_zonedataline = d_zonedata.begin();
54 }
55
56 void ZoneParserTNG::stackFile(const std::string& fname)
57 {
58 FILE *fp=fopen(fname.c_str(), "r");
59 if(!fp) {
60 std::error_code ec (errno,std::generic_category());
61 throw std::system_error(ec, "Unable to open file '"+fname+"': "+stringerror());
62 }
63
64 filestate fs(fp, fname);
65 d_filestates.push(fs);
66 d_fromfile = true;
67 }
68
69 ZoneParserTNG::~ZoneParserTNG()
70 {
71 while(!d_filestates.empty()) {
72 fclose(d_filestates.top().d_fp);
73 d_filestates.pop();
74 }
75 }
76
77 static string makeString(const string& line, const pair<string::size_type, string::size_type>& range)
78 {
79 return string(line.c_str() + range.first, range.second - range.first);
80 }
81
82 static bool isTimeSpec(const string& nextpart)
83 {
84 if(nextpart.empty())
85 return false;
86 for(string::const_iterator iter = nextpart.begin(); iter != nextpart.end(); ++iter) {
87 if(isdigit(*iter))
88 continue;
89 if(iter+1 != nextpart.end())
90 return false;
91 char c=tolower(*iter);
92 return (c=='s' || c=='m' || c=='h' || c=='d' || c=='w' || c=='y');
93 }
94 return true;
95 }
96
97
98 unsigned int ZoneParserTNG::makeTTLFromZone(const string& str)
99 {
100 if(str.empty())
101 return 0;
102
103 unsigned int val;
104 try {
105 val=pdns_stou(str);
106 }
107 catch (const std::out_of_range& oor) {
108 throw PDNSException("Unable to parse time specification '"+str+"' "+getLineOfFile());
109 }
110
111 char lc=dns_tolower(str[str.length()-1]);
112 if(!isdigit(lc))
113 switch(lc) {
114 case 's':
115 break;
116 case 'm':
117 val*=60; // minutes, not months!
118 break;
119 case 'h':
120 val*=3600;
121 break;
122 case 'd':
123 val*=3600*24;
124 break;
125 case 'w':
126 val*=3600*24*7;
127 break;
128 case 'y': // ? :-)
129 val*=3600*24*365;
130 break;
131
132 default:
133 throw PDNSException("Unable to parse time specification '"+str+"' "+getLineOfFile());
134 }
135 return val;
136 }
137
138 bool ZoneParserTNG::getTemplateLine()
139 {
140 if(d_templateparts.empty() || d_templatecounter > d_templatestop) // no template, or done with
141 return false;
142
143 string retline;
144 for(parts_t::const_iterator iter = d_templateparts.begin() ; iter != d_templateparts.end(); ++iter) {
145 if(iter != d_templateparts.begin())
146 retline+=" ";
147
148 string part=makeString(d_templateline, *iter);
149
150 /* a part can contain a 'naked' $, an escaped $ (\$), or ${offset,width,radix}, with width defaulting to 0,
151 and radix being 'd', 'o', 'x' or 'X', defaulting to 'd'.
152
153 The width is zero-padded, so if the counter is at 1, the offset is 15, with is 3, and the radix is 'x',
154 output will be '010', from the input of ${15,3,x}
155 */
156
157 string outpart;
158 outpart.reserve(part.size()+5);
159 bool inescape=false;
160
161 for(string::size_type pos = 0; pos < part.size() ; ++pos) {
162 char c=part[pos];
163 if(inescape) {
164 outpart.append(1, c);
165 inescape=false;
166 continue;
167 }
168
169 if(part[pos]=='\\') {
170 inescape=true;
171 continue;
172 }
173 if(c=='$') {
174 if(pos + 1 == part.size() || part[pos+1]!='{') { // a trailing $, or not followed by {
175 outpart.append(std::to_string(d_templatecounter));
176 continue;
177 }
178
179 // need to deal with { case
180
181 pos+=2;
182 string::size_type startPos=pos;
183 for(; pos < part.size() && part[pos]!='}' ; ++pos)
184 ;
185
186 if(pos == part.size()) // partial spec
187 break;
188
189 // we are on the '}'
190
191 string spec(part.c_str() + startPos, part.c_str() + pos);
192 int offset=0, width=0;
193 char radix='d';
194 sscanf(spec.c_str(), "%d,%d,%c", &offset, &width, &radix); // parse format specifier
195
196 char tmp[80];
197 switch (radix) {
198 case 'o':
199 snprintf(tmp, sizeof(tmp), "%0*o", width, d_templatecounter + offset);
200 break;
201 case 'x':
202 snprintf(tmp, sizeof(tmp), "%0*x", width, d_templatecounter + offset);
203 break;
204 case 'X':
205 snprintf(tmp, sizeof(tmp), "%0*X", width, d_templatecounter + offset);
206 break;
207 case 'd':
208 default:
209 snprintf(tmp, sizeof(tmp), "%0*d", width, d_templatecounter + offset);
210 break;
211 }
212 outpart+=tmp;
213 }
214 else
215 outpart.append(1, c);
216 }
217 retline+=outpart;
218 }
219 d_templatecounter+=d_templatestep;
220
221 d_line = retline;
222 return true;
223 }
224
225 static void chopComment(string& line)
226 {
227 if(line.find(';')==string::npos)
228 return;
229 string::size_type pos, len = line.length();
230 bool inQuote=false;
231 for(pos = 0 ; pos < len; ++pos) {
232 if(line[pos]=='\\')
233 pos++;
234 else if(line[pos]=='"')
235 inQuote=!inQuote;
236 else if(line[pos]==';' && !inQuote)
237 break;
238 }
239 if(pos != len)
240 line.resize(pos);
241 }
242
243 static bool findAndElide(string& line, char c)
244 {
245 string::size_type pos, len = line.length();
246 bool inQuote=false;
247 for(pos = 0 ; pos < len; ++pos) {
248 if(line[pos]=='\\')
249 pos++;
250 else if(line[pos]=='"')
251 inQuote=!inQuote;
252 else if(line[pos]==c && !inQuote)
253 break;
254 }
255 if(pos != len) {
256 line.erase(pos, 1);
257 return true;
258 }
259 return false;
260 }
261
262 DNSName ZoneParserTNG::getZoneName()
263 {
264 return d_zonename;
265 }
266
267 string ZoneParserTNG::getLineOfFile()
268 {
269 if (d_zonedata.size() > 0)
270 return "on line "+std::to_string(std::distance(d_zonedata.begin(), d_zonedataline))+" of given string";
271
272 if (d_filestates.empty())
273 return "";
274
275 return "on line "+std::to_string(d_filestates.top().d_lineno)+" of file '"+d_filestates.top().d_filename+"'";
276 }
277
278 pair<string,int> ZoneParserTNG::getLineNumAndFile()
279 {
280 if (d_filestates.empty())
281 return {"", 0};
282 else
283 return {d_filestates.top().d_filename, d_filestates.top().d_lineno};
284 }
285
286 bool ZoneParserTNG::get(DNSResourceRecord& rr, std::string* comment)
287 {
288 retry:;
289 if(!getTemplateLine() && !getLine())
290 return false;
291
292 boost::trim_right_if(d_line, is_any_of(" \t\r\n\x1a"));
293 if(comment)
294 comment->clear();
295 if(comment && d_line.find(';') != string::npos)
296 *comment = d_line.substr(d_line.find(';'));
297
298 d_parts.clear();
299 vstringtok(d_parts, d_line);
300
301 if(d_parts.empty())
302 goto retry;
303
304 if(d_parts[0].first != d_parts[0].second && d_line[d_parts[0].first]==';') // line consisting of nothing but comments
305 goto retry;
306
307 if(d_line[0]=='$') {
308 string command=makeString(d_line, d_parts[0]);
309 if(pdns_iequals(command,"$TTL") && d_parts.size() > 1) {
310 d_defaultttl=makeTTLFromZone(trim_right_copy_if(makeString(d_line, d_parts[1]), is_any_of(";")));
311 d_havedollarttl=true;
312 }
313 else if(pdns_iequals(command,"$INCLUDE") && d_parts.size() > 1 && d_fromfile) {
314 string fname=unquotify(makeString(d_line, d_parts[1]));
315 if(!fname.empty() && fname[0]!='/' && !d_reldir.empty())
316 fname=d_reldir+"/"+fname;
317 stackFile(fname);
318 }
319 else if(pdns_iequals(command, "$ORIGIN") && d_parts.size() > 1) {
320 d_zonename = DNSName(makeString(d_line, d_parts[1]));
321 }
322 else if(pdns_iequals(command, "$GENERATE") && d_parts.size() > 2) {
323 if (!d_generateEnabled) {
324 throw exception("$GENERATE is not allowed in this zone");
325 }
326 // $GENERATE 1-127 $ CNAME $.0
327 string range=makeString(d_line, d_parts[1]);
328 d_templatestep=1;
329 d_templatestop=0;
330 sscanf(range.c_str(),"%u-%u/%u", &d_templatecounter, &d_templatestop, &d_templatestep);
331 if (d_templatestep < 1 ||
332 d_templatestop < d_templatecounter) {
333 throw exception("Invalid $GENERATE parameters");
334 }
335 if (d_maxGenerateSteps != 0) {
336 size_t numberOfSteps = (d_templatestop - d_templatecounter) / d_templatestep;
337 if (numberOfSteps > d_maxGenerateSteps) {
338 throw exception("The number of $GENERATE steps (" + std::to_string(numberOfSteps) + ") is too high, the maximum is set to " + std::to_string(d_maxGenerateSteps));
339 }
340 }
341 d_templateline=d_line;
342 d_parts.pop_front();
343 d_parts.pop_front();
344
345 d_templateparts=d_parts;
346 goto retry;
347 }
348 else
349 throw exception("Can't parse zone line '"+d_line+"' "+getLineOfFile());
350 goto retry;
351 }
352
353 bool prevqname=false;
354 string qname = makeString(d_line, d_parts[0]); // Don't use DNSName here!
355 if(dns_isspace(d_line[0])) {
356 rr.qname=d_prevqname;
357 prevqname=true;
358 }else {
359 rr.qname=DNSName(qname);
360 d_parts.pop_front();
361 if(qname.empty() || qname[0]==';')
362 goto retry;
363 }
364 if(qname=="@")
365 rr.qname=d_zonename;
366 else if(!prevqname && !isCanonical(qname))
367 rr.qname += d_zonename;
368 d_prevqname=rr.qname;
369
370 if(d_parts.empty())
371 throw exception("Line with too little parts "+getLineOfFile());
372
373 string nextpart;
374
375 rr.ttl=d_defaultttl;
376 bool haveTTL=0, haveQTYPE=0;
377 pair<string::size_type, string::size_type> range;
378
379 while(!d_parts.empty()) {
380 range=d_parts.front();
381 d_parts.pop_front();
382 nextpart=makeString(d_line, range);
383 if(nextpart.empty())
384 break;
385
386 if(nextpart.find(';')!=string::npos) {
387 break;
388 }
389
390 // cout<<"Next part: '"<<nextpart<<"'"<<endl;
391
392 if(pdns_iequals(nextpart, g_INstr)) {
393 // cout<<"Ignoring 'IN'\n";
394 continue;
395 }
396 if(!haveTTL && !haveQTYPE && isTimeSpec(nextpart)) {
397 rr.ttl=makeTTLFromZone(nextpart);
398 if(!d_havedollarttl)
399 d_defaultttl = rr.ttl;
400 haveTTL=true;
401 // cout<<"ttl is probably: "<<rr.ttl<<endl;
402 continue;
403 }
404 if(haveQTYPE)
405 break;
406
407 try {
408 rr.qtype=DNSRecordContent::TypeToNumber(nextpart);
409 // cout<<"Got qtype ("<<rr.qtype.getCode()<<")\n";
410 haveQTYPE=1;
411 continue;
412 }
413 catch(...) {
414 throw runtime_error("Parsing zone content "+getLineOfFile()+
415 ": '"+nextpart+
416 "' doesn't look like a qtype, stopping loop");
417 }
418 }
419 if(!haveQTYPE)
420 throw exception("Malformed line "+getLineOfFile()+": '"+d_line+"'");
421
422 // rr.content=d_line.substr(range.first);
423 rr.content.assign(d_line, range.first, string::npos);
424 chopComment(rr.content);
425 trim_if(rr.content, is_any_of(" \r\n\t\x1a"));
426
427 if(rr.content.size()==1 && rr.content[0]=='@')
428 rr.content=d_zonename.toString();
429
430 if(findAndElide(rr.content, '(')) { // have found a ( and elided it
431 if(!findAndElide(rr.content, ')')) {
432 while(getLine()) {
433 trim_right(d_line);
434 chopComment(d_line);
435 trim(d_line);
436
437 bool ended = findAndElide(d_line, ')');
438 rr.content+=" "+d_line;
439 if(ended)
440 break;
441 }
442 }
443 }
444 trim_if(rr.content, is_any_of(" \r\n\t\x1a"));
445
446 vector<string> recparts;
447 switch(rr.qtype.getCode()) {
448 case QType::MX:
449 stringtok(recparts, rr.content);
450 if(recparts.size()==2) {
451 if (recparts[1]!=".") {
452 try {
453 recparts[1] = toCanonic(d_zonename, recparts[1]).toStringRootDot();
454 } catch (std::exception &e) {
455 throw PDNSException("Error in record '" + rr.qname.toLogString() + " " + rr.qtype.getName() + "': " + e.what());
456 }
457 }
458 rr.content=recparts[0]+" "+recparts[1];
459 }
460 break;
461
462 case QType::RP:
463 stringtok(recparts, rr.content);
464 if(recparts.size()==2) {
465 recparts[0] = toCanonic(d_zonename, recparts[0]).toStringRootDot();
466 recparts[1] = toCanonic(d_zonename, recparts[1]).toStringRootDot();
467 rr.content=recparts[0]+" "+recparts[1];
468 }
469 break;
470
471 case QType::SRV:
472 stringtok(recparts, rr.content);
473 if(recparts.size()==4) {
474 if(recparts[3]!=".") {
475 try {
476 recparts[3] = toCanonic(d_zonename, recparts[3]).toStringRootDot();
477 } catch (std::exception &e) {
478 throw PDNSException("Error in record '" + rr.qname.toLogString() + " " + rr.qtype.getName() + "': " + e.what());
479 }
480 }
481 rr.content=recparts[0]+" "+recparts[1]+" "+recparts[2]+" "+recparts[3];
482 }
483 break;
484
485
486 case QType::NS:
487 case QType::CNAME:
488 case QType::DNAME:
489 case QType::PTR:
490 try {
491 rr.content = toCanonic(d_zonename, rr.content).toStringRootDot();
492 } catch (std::exception &e) {
493 throw PDNSException("Error in record '" + rr.qname.toLogString() + " " + rr.qtype.getName() + "': " + e.what());
494 }
495 break;
496 case QType::AFSDB:
497 stringtok(recparts, rr.content);
498 if(recparts.size() == 2) {
499 try {
500 recparts[1]=toCanonic(d_zonename, recparts[1]).toStringRootDot();
501 } catch (std::exception &e) {
502 throw PDNSException("Error in record '" + rr.qname.toLogString() + " " + rr.qtype.getName() + "': " + e.what());
503 }
504 } else {
505 throw PDNSException("AFSDB record for "+rr.qname.toLogString()+" invalid");
506 }
507 rr.content.clear();
508 for(string::size_type n = 0; n < recparts.size(); ++n) {
509 if(n)
510 rr.content.append(1,' ');
511
512 rr.content+=recparts[n];
513 }
514 break;
515 case QType::SOA:
516 stringtok(recparts, rr.content);
517 if(recparts.size() > 7)
518 throw PDNSException("SOA record contents for "+rr.qname.toLogString()+" contains too many parts");
519 if(recparts.size() > 1) {
520 try {
521 recparts[0]=toCanonic(d_zonename, recparts[0]).toStringRootDot();
522 recparts[1]=toCanonic(d_zonename, recparts[1]).toStringRootDot();
523 } catch (std::exception &e) {
524 throw PDNSException("Error in record '" + rr.qname.toLogString() + " " + rr.qtype.getName() + "': " + e.what());
525 }
526 }
527 rr.content.clear();
528 for(string::size_type n = 0; n < recparts.size(); ++n) {
529 if(n)
530 rr.content.append(1,' ');
531
532 if(n > 1)
533 rr.content+=std::to_string(makeTTLFromZone(recparts[n]));
534 else
535 rr.content+=recparts[n];
536 }
537 break;
538 default:;
539 }
540 return true;
541 }
542
543
544 bool ZoneParserTNG::getLine()
545 {
546 if (d_zonedata.size() > 0) {
547 if (d_zonedataline != d_zonedata.end()) {
548 d_line = *d_zonedataline;
549 ++d_zonedataline;
550 return true;
551 }
552 return false;
553 }
554 while(!d_filestates.empty()) {
555 if(stringfgets(d_filestates.top().d_fp, d_line)) {
556 d_filestates.top().d_lineno++;
557 return true;
558 }
559 fclose(d_filestates.top().d_fp);
560 d_filestates.pop();
561 }
562 return false;
563 }