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