]>
Commit | Line | Data |
---|---|---|
f814d7c8 | 1 | /* |
6edbf68a PL |
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 | */ | |
870a0fe4 AT |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
a416d398 | 25 | #include "ascii.hh" |
f814d7c8 BH |
26 | #include "dnsparser.hh" |
27 | #include "sstuff.hh" | |
28 | #include "misc.hh" | |
29 | #include "dnswriter.hh" | |
30 | #include "dnsrecords.hh" | |
f814d7c8 BH |
31 | #include "misc.hh" |
32 | #include <fstream> | |
33 | #include "dns.hh" | |
34 | #include "zoneparser-tng.hh" | |
125e4840 BH |
35 | #include <deque> |
36 | #include <boost/algorithm/string.hpp> | |
e0e48a7a | 37 | #include <system_error> |
1817fef6 | 38 | #include <cinttypes> |
f814d7c8 | 39 | |
3108d502 | 40 | static string g_INstr("IN"); |
41 | ||
1290c1d2 RP |
42 | ZoneParserTNG::ZoneParserTNG(const string& fname, DNSName zname, string reldir, bool upgradeContent): |
43 | d_reldir(std::move(reldir)), d_zonename(std::move(zname)), d_defaultttl(3600), | |
b4db4fe4 CH |
44 | d_templatecounter(0), d_templatestop(0), d_templatestep(0), |
45 | d_havedollarttl(false), d_fromfile(true), d_upgradeContent(upgradeContent) | |
46 | { | |
bf503cc0 BH |
47 | stackFile(fname); |
48 | } | |
49 | ||
1290c1d2 RP |
50 | ZoneParserTNG::ZoneParserTNG(const vector<string>& zonedata, DNSName zname, bool upgradeContent): |
51 | d_zonename(std::move(zname)), d_zonedata(zonedata), d_defaultttl(3600), | |
8a70e507 | 52 | d_templatecounter(0), d_templatestop(0), d_templatestep(0), |
b4db4fe4 | 53 | d_havedollarttl(false), d_fromfile(false), d_upgradeContent(upgradeContent) |
0f0e73fe | 54 | { |
0f0e73fe | 55 | d_zonedataline = d_zonedata.begin(); |
0f0e73fe MS |
56 | } |
57 | ||
bf503cc0 BH |
58 | void ZoneParserTNG::stackFile(const std::string& fname) |
59 | { | |
60 | FILE *fp=fopen(fname.c_str(), "r"); | |
e0e48a7a PL |
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 | } | |
cfe397d5 BH |
65 | |
66 | filestate fs(fp, fname); | |
67 | d_filestates.push(fs); | |
0f0e73fe | 68 | d_fromfile = true; |
f814d7c8 BH |
69 | } |
70 | ||
71 | ZoneParserTNG::~ZoneParserTNG() | |
72 | { | |
cfe397d5 BH |
73 | while(!d_filestates.empty()) { |
74 | fclose(d_filestates.top().d_fp); | |
75 | d_filestates.pop(); | |
bf503cc0 | 76 | } |
f814d7c8 BH |
77 | } |
78 | ||
125e4840 BH |
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 | ||
cfe397d5 BH |
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); | |
2326ec3f | 94 | return (c=='s' || c=='m' || c=='h' || c=='d' || c=='w' || c=='y'); |
cfe397d5 BH |
95 | } |
96 | return true; | |
97 | } | |
98 | ||
99 | ||
100 | unsigned int ZoneParserTNG::makeTTLFromZone(const string& str) | |
125e4840 BH |
101 | { |
102 | if(str.empty()) | |
103 | return 0; | |
104 | ||
97ce13be RG |
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 | ||
3108d502 | 113 | char lc=dns_tolower(str[str.length()-1]); |
125e4840 BH |
114 | if(!isdigit(lc)) |
115 | switch(lc) { | |
3108d502 | 116 | case 's': |
2326ec3f | 117 | break; |
3108d502 | 118 | case 'm': |
38e655b6 BH |
119 | val*=60; // minutes, not months! |
120 | break; | |
3108d502 | 121 | case 'h': |
125e4840 BH |
122 | val*=3600; |
123 | break; | |
3108d502 | 124 | case 'd': |
125e4840 BH |
125 | val*=3600*24; |
126 | break; | |
3108d502 | 127 | case 'w': |
125e4840 BH |
128 | val*=3600*24*7; |
129 | break; | |
3108d502 | 130 | case 'y': // ? :-) |
125e4840 BH |
131 | val*=3600*24*365; |
132 | break; | |
cfe397d5 | 133 | |
125e4840 | 134 | default: |
3fed7dbd | 135 | throw PDNSException("Unable to parse time specification '"+str+"' "+getLineOfFile()); |
125e4840 BH |
136 | } |
137 | return val; | |
138 | } | |
139 | ||
2e83ba09 BH |
140 | bool ZoneParserTNG::getTemplateLine() |
141 | { | |
b8c3ea84 | 142 | if(d_templateparts.empty() || d_templatecounter > d_templatestop) // no template, or done with |
2e83ba09 BH |
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, | |
1817fef6 | 153 | and radix being 'd', 'o', 'x' or 'X', defaulting to 'd' (so ${offset} is valid). |
2e83ba09 | 154 | |
1817fef6 | 155 | The width is zero-padded, so if the counter is at 1, the offset is 15, width is 3, and the radix is 'x', |
2e83ba09 BH |
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) { | |
4957a608 BH |
166 | outpart.append(1, c); |
167 | inescape=false; | |
168 | continue; | |
2e83ba09 | 169 | } |
4957a608 | 170 | |
2e83ba09 | 171 | if(part[pos]=='\\') { |
4957a608 BH |
172 | inescape=true; |
173 | continue; | |
2e83ba09 BH |
174 | } |
175 | if(c=='$') { | |
4957a608 | 176 | if(pos + 1 == part.size() || part[pos+1]!='{') { // a trailing $, or not followed by { |
335da0ba | 177 | outpart.append(std::to_string(d_templatecounter)); |
4957a608 BH |
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'; | |
1817fef6 RG |
196 | // parse format specifier |
197 | int extracted = sscanf(spec.c_str(), "%d,%d,%c", &offset, &width, &radix); | |
198 | if (extracted < 1) { | |
199 | throw PDNSException("Unable to parse offset, width and radix for $GENERATE's lhs from '"+spec+"' "+getLineOfFile()); | |
200 | } | |
4957a608 | 201 | |
4957a608 | 202 | char tmp[80]; |
4338e69f OM |
203 | switch (radix) { |
204 | case 'o': | |
205 | snprintf(tmp, sizeof(tmp), "%0*o", width, d_templatecounter + offset); | |
206 | break; | |
207 | case 'x': | |
208 | snprintf(tmp, sizeof(tmp), "%0*x", width, d_templatecounter + offset); | |
209 | break; | |
210 | case 'X': | |
211 | snprintf(tmp, sizeof(tmp), "%0*X", width, d_templatecounter + offset); | |
212 | break; | |
213 | case 'd': | |
214 | default: | |
215 | snprintf(tmp, sizeof(tmp), "%0*d", width, d_templatecounter + offset); | |
216 | break; | |
217 | } | |
4957a608 | 218 | outpart+=tmp; |
2e83ba09 BH |
219 | } |
220 | else | |
4957a608 | 221 | outpart.append(1, c); |
2e83ba09 BH |
222 | } |
223 | retline+=outpart; | |
224 | } | |
225 | d_templatecounter+=d_templatestep; | |
226 | ||
227 | d_line = retline; | |
228 | return true; | |
229 | } | |
230 | ||
050e6877 | 231 | static void chopComment(string& line) |
a09683af | 232 | { |
3108d502 | 233 | if(line.find(';')==string::npos) |
234 | return; | |
a09683af BH |
235 | string::size_type pos, len = line.length(); |
236 | bool inQuote=false; | |
237 | for(pos = 0 ; pos < len; ++pos) { | |
238 | if(line[pos]=='\\') | |
239 | pos++; | |
240 | else if(line[pos]=='"') | |
241 | inQuote=!inQuote; | |
242 | else if(line[pos]==';' && !inQuote) | |
243 | break; | |
244 | } | |
245 | if(pos != len) | |
246 | line.resize(pos); | |
247 | } | |
248 | ||
050e6877 | 249 | static bool findAndElide(string& line, char c) |
9f0076d7 BH |
250 | { |
251 | string::size_type pos, len = line.length(); | |
252 | bool inQuote=false; | |
253 | for(pos = 0 ; pos < len; ++pos) { | |
254 | if(line[pos]=='\\') | |
255 | pos++; | |
256 | else if(line[pos]=='"') | |
257 | inQuote=!inQuote; | |
258 | else if(line[pos]==c && !inQuote) | |
259 | break; | |
260 | } | |
261 | if(pos != len) { | |
262 | line.erase(pos, 1); | |
263 | return true; | |
264 | } | |
265 | return false; | |
266 | } | |
267 | ||
d16a2ccf PL |
268 | DNSName ZoneParserTNG::getZoneName() |
269 | { | |
270 | return d_zonename; | |
271 | } | |
272 | ||
d27ea394 BH |
273 | string ZoneParserTNG::getLineOfFile() |
274 | { | |
0f0e73fe | 275 | if (d_zonedata.size() > 0) |
335da0ba | 276 | return "on line "+std::to_string(std::distance(d_zonedata.begin(), d_zonedataline))+" of given string"; |
0f0e73fe | 277 | |
a1239f65 RG |
278 | if (d_filestates.empty()) |
279 | return ""; | |
280 | ||
335da0ba | 281 | return "on line "+std::to_string(d_filestates.top().d_lineno)+" of file '"+d_filestates.top().d_filename+"'"; |
d27ea394 | 282 | } |
cfe397d5 | 283 | |
c91effc8 | 284 | pair<string,int> ZoneParserTNG::getLineNumAndFile() |
285 | { | |
9923fc22 PD |
286 | if (d_filestates.empty()) |
287 | return {"", 0}; | |
288 | else | |
289 | return {d_filestates.top().d_filename, d_filestates.top().d_lineno}; | |
c91effc8 | 290 | } |
291 | ||
4950b140 | 292 | bool ZoneParserTNG::get(DNSResourceRecord& rr, std::string* comment) |
f814d7c8 BH |
293 | { |
294 | retry:; | |
2e83ba09 | 295 | if(!getTemplateLine() && !getLine()) |
f814d7c8 | 296 | return false; |
125e4840 | 297 | |
dc593046 | 298 | boost::trim_right_if(d_line, boost::is_any_of(" \t\r\n\x1a")); |
a5a1f447 | 299 | if(comment) |
300 | comment->clear(); | |
301 | if(comment && d_line.find(';') != string::npos) | |
302 | *comment = d_line.substr(d_line.find(';')); | |
125e4840 | 303 | |
9bbcf03a RG |
304 | d_parts.clear(); |
305 | vstringtok(d_parts, d_line); | |
306 | ||
307 | if(d_parts.empty()) | |
125e4840 BH |
308 | goto retry; |
309 | ||
9bbcf03a | 310 | if(d_parts[0].first != d_parts[0].second && d_line[d_parts[0].first]==';') // line consisting of nothing but comments |
4d2c97aa BH |
311 | goto retry; |
312 | ||
125e4840 | 313 | if(d_line[0]=='$') { |
9bbcf03a RG |
314 | string command=makeString(d_line, d_parts[0]); |
315 | if(pdns_iequals(command,"$TTL") && d_parts.size() > 1) { | |
dc593046 | 316 | d_defaultttl=makeTTLFromZone(trim_right_copy_if(makeString(d_line, d_parts[1]), boost::is_any_of(";"))); |
df1d406a BH |
317 | d_havedollarttl=true; |
318 | } | |
9bbcf03a RG |
319 | else if(pdns_iequals(command,"$INCLUDE") && d_parts.size() > 1 && d_fromfile) { |
320 | string fname=unquotify(makeString(d_line, d_parts[1])); | |
da042e6e | 321 | if(!fname.empty() && fname[0]!='/' && !d_reldir.empty()) |
4957a608 | 322 | fname=d_reldir+"/"+fname; |
da042e6e | 323 | stackFile(fname); |
bf503cc0 | 324 | } |
9bbcf03a RG |
325 | else if(pdns_iequals(command, "$ORIGIN") && d_parts.size() > 1) { |
326 | d_zonename = DNSName(makeString(d_line, d_parts[1])); | |
2e83ba09 | 327 | } |
9bbcf03a | 328 | else if(pdns_iequals(command, "$GENERATE") && d_parts.size() > 2) { |
91a862bf RG |
329 | if (!d_generateEnabled) { |
330 | throw exception("$GENERATE is not allowed in this zone"); | |
331 | } | |
bf503cc0 | 332 | // $GENERATE 1-127 $ CNAME $.0 |
1817fef6 RG |
333 | // The range part can be one of two forms: start-stop or start-stop/step. If the first |
334 | // form is used, then step is set to 1. start, stop and step must be positive | |
335 | // integers between 0 and (2^31)-1. start must not be larger than stop. | |
9bbcf03a | 336 | string range=makeString(d_line, d_parts[1]); |
2e83ba09 BH |
337 | d_templatestep=1; |
338 | d_templatestop=0; | |
1817fef6 RG |
339 | int extracted = sscanf(range.c_str(),"%" SCNu32 "-%" SCNu32 "/%" SCNu32, &d_templatecounter, &d_templatestop, &d_templatestep); |
340 | if (extracted == 2) { | |
341 | d_templatestep=1; | |
342 | } | |
343 | else if (extracted != 3) { | |
344 | throw exception("Invalid range from $GENERATE parameters '" + range + "'"); | |
345 | } | |
775a673a OM |
346 | if (d_templatestep < 1 || |
347 | d_templatestop < d_templatecounter) { | |
b4ab09cd | 348 | throw exception("Invalid $GENERATE parameters"); |
775a673a | 349 | } |
ba3d53d1 RG |
350 | if (d_maxGenerateSteps != 0) { |
351 | size_t numberOfSteps = (d_templatestop - d_templatecounter) / d_templatestep; | |
352 | if (numberOfSteps > d_maxGenerateSteps) { | |
353 | throw exception("The number of $GENERATE steps (" + std::to_string(numberOfSteps) + ") is too high, the maximum is set to " + std::to_string(d_maxGenerateSteps)); | |
354 | } | |
355 | } | |
2e83ba09 | 356 | d_templateline=d_line; |
9bbcf03a RG |
357 | d_parts.pop_front(); |
358 | d_parts.pop_front(); | |
2e83ba09 | 359 | |
9bbcf03a | 360 | d_templateparts=d_parts; |
2e83ba09 | 361 | goto retry; |
bf503cc0 | 362 | } |
125e4840 | 363 | else |
d27ea394 | 364 | throw exception("Can't parse zone line '"+d_line+"' "+getLineOfFile()); |
f814d7c8 | 365 | goto retry; |
f814d7c8 | 366 | } |
125e4840 | 367 | |
e720f311 | 368 | bool prevqname=false; |
9bbcf03a | 369 | string qname = makeString(d_line, d_parts[0]); // Don't use DNSName here! |
3108d502 | 370 | if(dns_isspace(d_line[0])) { |
125e4840 | 371 | rr.qname=d_prevqname; |
e720f311 KM |
372 | prevqname=true; |
373 | }else { | |
8171ab83 | 374 | rr.qname=DNSName(qname); |
9bbcf03a | 375 | d_parts.pop_front(); |
e720f311 | 376 | if(qname.empty() || qname[0]==';') |
125e4840 BH |
377 | goto retry; |
378 | } | |
d66269ef | 379 | if(qname=="@") |
125e4840 | 380 | rr.qname=d_zonename; |
e720f311 | 381 | else if(!prevqname && !isCanonical(qname)) |
675fa24c | 382 | rr.qname += d_zonename; |
125e4840 BH |
383 | d_prevqname=rr.qname; |
384 | ||
9bbcf03a | 385 | if(d_parts.empty()) |
d27ea394 | 386 | throw exception("Line with too little parts "+getLineOfFile()); |
125e4840 | 387 | |
125e4840 | 388 | string nextpart; |
f814d7c8 | 389 | |
125e4840 | 390 | rr.ttl=d_defaultttl; |
b4db4fe4 CH |
391 | bool haveTTL{false}, haveQTYPE{false}; |
392 | string qtypeString; | |
125e4840 BH |
393 | pair<string::size_type, string::size_type> range; |
394 | ||
9bbcf03a RG |
395 | while(!d_parts.empty()) { |
396 | range=d_parts.front(); | |
397 | d_parts.pop_front(); | |
125e4840 BH |
398 | nextpart=makeString(d_line, range); |
399 | if(nextpart.empty()) | |
400 | break; | |
401 | ||
a5a1f447 | 402 | if(nextpart.find(';')!=string::npos) { |
125e4840 | 403 | break; |
a5a1f447 | 404 | } |
125e4840 BH |
405 | |
406 | // cout<<"Next part: '"<<nextpart<<"'"<<endl; | |
3108d502 | 407 | |
408 | if(pdns_iequals(nextpart, g_INstr)) { | |
125e4840 BH |
409 | // cout<<"Ignoring 'IN'\n"; |
410 | continue; | |
411 | } | |
cfe397d5 | 412 | if(!haveTTL && !haveQTYPE && isTimeSpec(nextpart)) { |
125e4840 | 413 | rr.ttl=makeTTLFromZone(nextpart); |
7fd6c67e | 414 | if(!d_havedollarttl) |
415 | d_defaultttl = rr.ttl; | |
125e4840 BH |
416 | haveTTL=true; |
417 | // cout<<"ttl is probably: "<<rr.ttl<<endl; | |
418 | continue; | |
419 | } | |
420 | if(haveQTYPE) | |
421 | break; | |
422 | ||
423 | try { | |
b4db4fe4 | 424 | rr.qtype = DNSRecordContent::TypeToNumber(nextpart); |
125e4840 | 425 | // cout<<"Got qtype ("<<rr.qtype.getCode()<<")\n"; |
b4db4fe4 CH |
426 | qtypeString = nextpart; |
427 | haveQTYPE = true; | |
125e4840 BH |
428 | continue; |
429 | } | |
430 | catch(...) { | |
d27ea394 | 431 | throw runtime_error("Parsing zone content "+getLineOfFile()+ |
232f0877 CH |
432 | ": '"+nextpart+ |
433 | "' doesn't look like a qtype, stopping loop"); | |
125e4840 BH |
434 | } |
435 | } | |
436 | if(!haveQTYPE) | |
d27ea394 | 437 | throw exception("Malformed line "+getLineOfFile()+": '"+d_line+"'"); |
125e4840 | 438 | |
3108d502 | 439 | // rr.content=d_line.substr(range.first); |
440 | rr.content.assign(d_line, range.first, string::npos); | |
a09683af | 441 | chopComment(rr.content); |
dc593046 | 442 | trim_if(rr.content, boost::is_any_of(" \r\n\t\x1a")); |
849fde0b | 443 | |
3108d502 | 444 | if(rr.content.size()==1 && rr.content[0]=='@') |
90c3521b | 445 | rr.content=d_zonename.toString(); |
b33702d5 | 446 | |
9f0076d7 BH |
447 | if(findAndElide(rr.content, '(')) { // have found a ( and elided it |
448 | if(!findAndElide(rr.content, ')')) { | |
449 | while(getLine()) { | |
dc593046 | 450 | boost::trim_right(d_line); |
4957a608 | 451 | chopComment(d_line); |
dc593046 | 452 | boost::trim(d_line); |
4957a608 BH |
453 | |
454 | bool ended = findAndElide(d_line, ')'); | |
455 | rr.content+=" "+d_line; | |
456 | if(ended) | |
457 | break; | |
125e4840 | 458 | } |
125e4840 BH |
459 | } |
460 | } | |
dc593046 | 461 | boost::trim_if(rr.content, boost::is_any_of(" \r\n\t\x1a")); |
9f0076d7 | 462 | |
b4db4fe4 CH |
463 | if (d_upgradeContent && DNSRecordContent::isUnknownType(qtypeString)) { |
464 | rr.content = DNSRecordContent::upgradeContent(rr.qname, rr.qtype, rr.content); | |
465 | } | |
466 | ||
43f40013 | 467 | vector<string> recparts; |
125e4840 BH |
468 | switch(rr.qtype.getCode()) { |
469 | case QType::MX: | |
43f40013 BH |
470 | stringtok(recparts, rr.content); |
471 | if(recparts.size()==2) { | |
1293f91e PL |
472 | if (recparts[1]!=".") { |
473 | try { | |
474 | recparts[1] = toCanonic(d_zonename, recparts[1]).toStringRootDot(); | |
475 | } catch (std::exception &e) { | |
86f1af1c | 476 | throw PDNSException("Error in record '" + rr.qname.toLogString() + " " + rr.qtype.getName() + "': " + e.what()); |
1293f91e PL |
477 | } |
478 | } | |
43f40013 BH |
479 | rr.content=recparts[0]+" "+recparts[1]; |
480 | } | |
481 | break; | |
25e0cd7f | 482 | |
6348d406 PD |
483 | case QType::RP: |
484 | stringtok(recparts, rr.content); | |
485 | if(recparts.size()==2) { | |
0c0e717c PD |
486 | recparts[0] = toCanonic(d_zonename, recparts[0]).toStringRootDot(); |
487 | recparts[1] = toCanonic(d_zonename, recparts[1]).toStringRootDot(); | |
6348d406 PD |
488 | rr.content=recparts[0]+" "+recparts[1]; |
489 | } | |
490 | break; | |
491 | ||
25e0cd7f BH |
492 | case QType::SRV: |
493 | stringtok(recparts, rr.content); | |
494 | if(recparts.size()==4) { | |
1293f91e PL |
495 | if(recparts[3]!=".") { |
496 | try { | |
497 | recparts[3] = toCanonic(d_zonename, recparts[3]).toStringRootDot(); | |
498 | } catch (std::exception &e) { | |
86f1af1c | 499 | throw PDNSException("Error in record '" + rr.qname.toLogString() + " " + rr.qtype.getName() + "': " + e.what()); |
1293f91e PL |
500 | } |
501 | } | |
25e0cd7f BH |
502 | rr.content=recparts[0]+" "+recparts[1]+" "+recparts[2]+" "+recparts[3]; |
503 | } | |
504 | break; | |
505 | ||
43f40013 | 506 | |
125e4840 BH |
507 | case QType::NS: |
508 | case QType::CNAME: | |
8dee0750 | 509 | case QType::DNAME: |
125e4840 | 510 | case QType::PTR: |
1293f91e PL |
511 | try { |
512 | rr.content = toCanonic(d_zonename, rr.content).toStringRootDot(); | |
513 | } catch (std::exception &e) { | |
86f1af1c | 514 | throw PDNSException("Error in record '" + rr.qname.toLogString() + " " + rr.qtype.getName() + "': " + e.what()); |
1293f91e | 515 | } |
125e4840 | 516 | break; |
319d6e47 | 517 | case QType::AFSDB: |
9f067a15 JJ |
518 | stringtok(recparts, rr.content); |
519 | if(recparts.size() == 2) { | |
520 | try { | |
319d6e47 | 521 | recparts[1]=toCanonic(d_zonename, recparts[1]).toStringRootDot(); |
9f067a15 | 522 | } catch (std::exception &e) { |
86f1af1c | 523 | throw PDNSException("Error in record '" + rr.qname.toLogString() + " " + rr.qtype.getName() + "': " + e.what()); |
319d6e47 | 524 | } |
9f067a15 | 525 | } else { |
86f1af1c | 526 | throw PDNSException("AFSDB record for "+rr.qname.toLogString()+" invalid"); |
9f067a15 JJ |
527 | } |
528 | rr.content.clear(); | |
529 | for(string::size_type n = 0; n < recparts.size(); ++n) { | |
530 | if(n) | |
531 | rr.content.append(1,' '); | |
125e4840 | 532 | |
9f067a15 | 533 | rr.content+=recparts[n]; |
319d6e47 JJ |
534 | } |
535 | break; | |
125e4840 | 536 | case QType::SOA: |
43f40013 | 537 | stringtok(recparts, rr.content); |
0c306c7c | 538 | if(recparts.size() > 7) |
86f1af1c | 539 | throw PDNSException("SOA record contents for "+rr.qname.toLogString()+" contains too many parts"); |
43f40013 | 540 | if(recparts.size() > 1) { |
562c0b13 PL |
541 | try { |
542 | recparts[0]=toCanonic(d_zonename, recparts[0]).toStringRootDot(); | |
543 | recparts[1]=toCanonic(d_zonename, recparts[1]).toStringRootDot(); | |
1293f91e | 544 | } catch (std::exception &e) { |
86f1af1c | 545 | throw PDNSException("Error in record '" + rr.qname.toLogString() + " " + rr.qtype.getName() + "': " + e.what()); |
562c0b13 | 546 | } |
125e4840 BH |
547 | } |
548 | rr.content.clear(); | |
43f40013 | 549 | for(string::size_type n = 0; n < recparts.size(); ++n) { |
125e4840 | 550 | if(n) |
4957a608 | 551 | rr.content.append(1,' '); |
c9f935fa | 552 | |
82cc6877 | 553 | if(n > 1) |
335da0ba | 554 | rr.content+=std::to_string(makeTTLFromZone(recparts[n])); |
82cc6877 | 555 | else |
43f40013 | 556 | rr.content+=recparts[n]; |
125e4840 | 557 | } |
38e655b6 | 558 | break; |
125e4840 BH |
559 | default:; |
560 | } | |
f814d7c8 BH |
561 | return true; |
562 | } | |
563 | ||
834942f1 | 564 | |
f814d7c8 BH |
565 | bool ZoneParserTNG::getLine() |
566 | { | |
0f0e73fe MS |
567 | if (d_zonedata.size() > 0) { |
568 | if (d_zonedataline != d_zonedata.end()) { | |
569 | d_line = *d_zonedataline; | |
cb6655e7 | 570 | ++d_zonedataline; |
0f0e73fe MS |
571 | return true; |
572 | } | |
573 | return false; | |
574 | } | |
cfe397d5 | 575 | while(!d_filestates.empty()) { |
834942f1 | 576 | if(stringfgets(d_filestates.top().d_fp, d_line)) { |
cfe397d5 | 577 | d_filestates.top().d_lineno++; |
bf503cc0 BH |
578 | return true; |
579 | } | |
cfe397d5 BH |
580 | fclose(d_filestates.top().d_fp); |
581 | d_filestates.pop(); | |
f814d7c8 BH |
582 | } |
583 | return false; | |
584 | } |