~ubuntu-branches/ubuntu/maverick/pdns/maverick-updates

« back to all changes in this revision

Viewing changes to pdns/zoneparser-tng.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matthijs Mohlmann, Matthijs Mohlmann, Christoph Haas
  • Date: 2007-04-15 23:23:39 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070415232339-5x3scc8gx04e50um
Tags: 2.9.21-1
[ Matthijs Mohlmann ]
* New upstream release. (Closes: #420294)
* Remove meta pdns package.
* Added new sqlite3 backend package.
* Months and minutes where mixed up. (Closes: #406462)
* Case sensitivity in bind backend caused PowerDNS to not serve a certain
  zone. (Closes: #406461)
* Bind backend forgot about zones on a notify. (Closes: #398213)

[ Christoph Haas ]
* Documented incorporated backend bind. (Closes: #415471)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
    PowerDNS Versatile Database Driven Nameserver
3
 
    Copyright (C) 2005  PowerDNS.COM BV
 
3
    Copyright (C) 2005 - 2007  PowerDNS.COM BV
4
4
 
5
5
    This program is free software; you can redistribute it and/or modify
6
6
    it under the terms of the GNU General Public License version 2 
13
13
 
14
14
    You should have received a copy of the GNU General Public License
15
15
    along with this program; if not, write to the Free Software
16
 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
17
*/
18
18
 
19
19
#include "dnsparser.hh"
21
21
#include "misc.hh"
22
22
#include "dnswriter.hh"
23
23
#include "dnsrecords.hh"
24
 
#include "statbag.hh"
25
24
#include "misc.hh"
26
25
#include <fstream>
27
26
#include "dns.hh"
28
27
#include "zoneparser-tng.hh"
29
 
 
30
 
ZoneParserTNG::ZoneParserTNG(const string& fname)
31
 
{
32
 
  d_fp=fopen(fname.c_str(), "r");
33
 
  if(!d_fp)
 
28
#include <deque>
 
29
#include <boost/algorithm/string.hpp>
 
30
#include <boost/lexical_cast.hpp>
 
31
 
 
32
ZoneParserTNG::ZoneParserTNG(const string& fname, const string& zname, const string& reldir) : d_reldir(reldir), d_zonename(zname), d_defaultttl(3600)
 
33
{
 
34
  d_zonename = toCanonic("", d_zonename);
 
35
  stackFile(fname);
 
36
}
 
37
 
 
38
void ZoneParserTNG::stackFile(const std::string& fname)
 
39
{
 
40
  FILE *fp=fopen(fname.c_str(), "r");
 
41
  if(!fp)
34
42
    throw runtime_error("Unable to open file '"+fname+"': "+stringerror());
 
43
  d_fps.push(fp);
35
44
}
36
45
 
37
46
ZoneParserTNG::~ZoneParserTNG()
38
47
{
39
 
  fclose(d_fp);
40
 
}
 
48
  while(!d_fps.empty()) {
 
49
    fclose(d_fps.top());
 
50
    d_fps.pop();
 
51
  }
 
52
}
 
53
 
 
54
static string makeString(const string& line, const pair<string::size_type, string::size_type>& range)
 
55
{
 
56
  return string(line.c_str() + range.first, range.second - range.first);
 
57
}
 
58
 
 
59
static unsigned int makeTTLFromZone(const string& str)
 
60
{
 
61
  if(str.empty())
 
62
    return 0;
 
63
 
 
64
  unsigned int val=atoi(str.c_str());
 
65
  char lc=toupper(str[str.length()-1]);
 
66
  if(!isdigit(lc))
 
67
    switch(lc) {
 
68
    case 'M':
 
69
      val*=60; // minutes, not months!
 
70
      break;
 
71
    case 'H':
 
72
      val*=3600;
 
73
      break;
 
74
    case 'D':
 
75
      val*=3600*24;
 
76
      break;
 
77
    case 'W':
 
78
      val*=3600*24*7;
 
79
      break;
 
80
    case 'Y': // ? :-)
 
81
      val*=3600*24*365;
 
82
      break;
 
83
    default:
 
84
      throw ZoneParserTNG::exception("Unable to parse time specification '"+str+"'");
 
85
    }
 
86
  return val;
 
87
}
 
88
 
 
89
bool ZoneParserTNG::getTemplateLine()
 
90
{
 
91
  if(d_templateparts.empty() || d_templatecounter > d_templatestop) // no template, or done with
 
92
    return false;
 
93
 
 
94
  string retline;
 
95
  for(parts_t::const_iterator iter = d_templateparts.begin() ; iter != d_templateparts.end(); ++iter) {
 
96
    if(iter != d_templateparts.begin())
 
97
      retline+=" ";
 
98
 
 
99
    string part=makeString(d_templateline, *iter);
 
100
    
 
101
    /* a part can contain a 'naked' $, an escaped $ (\$), or ${offset,width,radix}, with width defaulting to 0, 
 
102
       and radix beging 'd', 'o', 'x' or 'X', defaulting to 'd'. 
 
103
 
 
104
       The width is zero-padded, so if the counter is at 1, the offset is 15, with is 3, and the radix is 'x',
 
105
       output will be '010', from the input of ${15,3,x}
 
106
    */
 
107
 
 
108
    string outpart;
 
109
    outpart.reserve(part.size()+5);
 
110
    bool inescape=false;
 
111
 
 
112
    for(string::size_type pos = 0; pos < part.size() ; ++pos) {
 
113
      char c=part[pos];
 
114
      if(inescape) {
 
115
        outpart.append(1, c);
 
116
        inescape=false;
 
117
        continue;
 
118
      }
 
119
        
 
120
      if(part[pos]=='\\') {
 
121
        inescape=true;
 
122
        continue;
 
123
      }
 
124
      if(c=='$') {
 
125
        if(pos + 1 == part.size() || part[pos+1]!='{') {  // a trailing $, or not followed by {
 
126
          outpart.append(lexical_cast<string>(d_templatecounter));
 
127
          continue;
 
128
        }
 
129
        
 
130
        // need to deal with { case 
 
131
        
 
132
        pos+=2;
 
133
        string::size_type startPos=pos;
 
134
        for(; pos < part.size() && part[pos]!='}' ; ++pos)
 
135
          ;
 
136
        
 
137
        if(pos == part.size()) // partial spec
 
138
          break;
 
139
 
 
140
        // we are on the '}'
 
141
 
 
142
        string spec(part.c_str() + startPos, part.c_str() + pos);
 
143
        int offset=0, width=0;
 
144
        char radix='d';
 
145
        sscanf(spec.c_str(), "%d,%d,%c", &offset, &width, &radix);  // parse format specifier
 
146
 
 
147
        char format[12];
 
148
        snprintf(format, sizeof(format) - 1, "%%0%d%c", width, radix); // make into printf-style format
 
149
 
 
150
        char tmp[80];
 
151
        snprintf(tmp, sizeof(tmp)-1, format, d_templatecounter + offset); // and do the actual printing
 
152
        outpart+=tmp;
 
153
      }
 
154
      else
 
155
        outpart.append(1, c);
 
156
    }
 
157
    retline+=outpart;
 
158
  }
 
159
  d_templatecounter+=d_templatestep;
 
160
 
 
161
  d_line = retline;
 
162
  return true;
 
163
}
 
164
 
 
165
void chopComment(string& line)
 
166
{
 
167
  string::size_type pos, len = line.length();
 
168
  bool inQuote=false;
 
169
  for(pos = 0 ; pos < len; ++pos) {
 
170
    if(line[pos]=='\\') 
 
171
      pos++;
 
172
    else if(line[pos]=='"') 
 
173
      inQuote=!inQuote;
 
174
    else if(line[pos]==';' && !inQuote)
 
175
      break;
 
176
  }
 
177
  if(pos != len)
 
178
    line.resize(pos);
 
179
}
 
180
 
 
181
bool findAndElide(string& line, char c)
 
182
{
 
183
  string::size_type pos, len = line.length();
 
184
  bool inQuote=false;
 
185
  for(pos = 0 ; pos < len; ++pos) {
 
186
    if(line[pos]=='\\') 
 
187
      pos++;
 
188
    else if(line[pos]=='"') 
 
189
      inQuote=!inQuote;
 
190
    else if(line[pos]==c && !inQuote)
 
191
      break;
 
192
  }
 
193
  if(pos != len) {
 
194
    line.erase(pos, 1);
 
195
    return true;
 
196
  }
 
197
  return false;
 
198
}
 
199
 
41
200
 
42
201
bool ZoneParserTNG::get(DNSResourceRecord& rr) 
43
202
{
44
203
 retry:;
45
 
  if(!getLine())
46
 
    return false;
47
 
  string::size_type pos;
48
 
  vector<string> parts;
49
 
  
50
 
  if((pos=d_line.find(';'))!=string::npos)
51
 
    d_line.resize(pos);
52
 
  stripLine(d_line);
53
 
  if(d_line.empty())
54
 
    goto retry;
55
 
  
56
 
  parts.clear();
57
 
  stringtok(parts, d_line);
58
 
  if(parts.size()!=4 && parts.size()!=5) {
59
 
    cerr<<"Bad line: '"<<d_line<<"'\n";
60
 
    return false;
61
 
  }
62
 
  
63
 
  rr.qname=toLowerCanonic(parts[0]);
64
 
  rr.ttl=atoi(parts[1].c_str());
65
 
  string qclass("IN");
66
 
  
67
 
  rr.qtype=parts[2 + (parts.size()==5)];
68
 
  rr.content=parts[3 + (parts.size()==5)];
 
204
  if(!getTemplateLine() && !getLine())
 
205
    return false;
 
206
 
 
207
  chomp(d_line, " \r\n\x1a");
 
208
 
 
209
  parts_t parts;
 
210
  vstringtok(parts, d_line);
 
211
 
 
212
  if(parts.empty())
 
213
    goto retry;
 
214
 
 
215
  if(d_line[0]=='$') { 
 
216
    string command=makeString(d_line, parts[0]);
 
217
    if(command=="$TTL" && parts.size() > 1)
 
218
      d_defaultttl=makeTTLFromZone(makeString(d_line,parts[1]));
 
219
    else if(iequals(command,"$INCLUDE") && parts.size() > 1) {
 
220
      string fname=unquotify(makeString(d_line, parts[1]));
 
221
      if(!fname.empty() && fname[0]!='/' && !d_reldir.empty())
 
222
        fname=d_reldir+"/"+fname;
 
223
      stackFile(fname);
 
224
    }
 
225
    else if(iequals(command, "$ORIGIN") && parts.size() > 1) {
 
226
      d_zonename = toCanonic("", makeString(d_line, parts[1]));
 
227
    }
 
228
    else if(iequals(command, "$GENERATE") && parts.size() > 2) {
 
229
      // $GENERATE 1-127 $ CNAME $.0
 
230
      string range=makeString(d_line, parts[1]);
 
231
      d_templatestep=1;
 
232
      d_templatestop=0;
 
233
      sscanf(range.c_str(),"%d-%d/%d", &d_templatecounter, &d_templatestop, &d_templatestep);
 
234
      d_templateline=d_line;
 
235
      parts.pop_front();
 
236
      parts.pop_front();
 
237
 
 
238
      d_templateparts=parts;
 
239
      goto retry;
 
240
    }
 
241
    else
 
242
      throw exception("Can't parse zone line '"+d_line+"'");
 
243
    goto retry;
 
244
  }
 
245
 
 
246
  if(isspace(d_line[0])) 
 
247
    rr.qname=d_prevqname;
 
248
  else {
 
249
    rr.qname=makeString(d_line, parts[0]); 
 
250
    parts.pop_front();
 
251
    if(rr.qname.empty() || rr.qname[0]==';')
 
252
      goto retry;
 
253
  }
 
254
  if(rr.qname=="@")
 
255
    rr.qname=d_zonename;
 
256
  else if(!isCanonical(rr.qname)) {
 
257
    rr.qname.append(1,'.');
 
258
    rr.qname.append(d_zonename);
 
259
  }
 
260
  d_prevqname=rr.qname;
 
261
 
 
262
  if(parts.empty()) 
 
263
    throw exception("Line with too little parts");
 
264
 
 
265
  string nextpart;
 
266
  
 
267
  rr.ttl=d_defaultttl;
 
268
  bool haveTTL=0, haveQTYPE=0;
 
269
  pair<string::size_type, string::size_type> range;
 
270
 
 
271
  while(!parts.empty()) {
 
272
    range=parts.front();
 
273
    parts.pop_front();
 
274
    nextpart=makeString(d_line, range);
 
275
    if(nextpart.empty())
 
276
      break;
 
277
 
 
278
    if(nextpart.find(';')!=string::npos)
 
279
      break;
 
280
 
 
281
    // cout<<"Next part: '"<<nextpart<<"'"<<endl;
 
282
    
 
283
    if(!Utility::strcasecmp(nextpart.c_str(), "IN")) {
 
284
      // cout<<"Ignoring 'IN'\n";
 
285
      continue;
 
286
    }
 
287
    if(!haveTTL && !haveQTYPE && all(nextpart, is_digit())) {
 
288
      rr.ttl=makeTTLFromZone(nextpart);
 
289
      haveTTL=true;
 
290
      // cout<<"ttl is probably: "<<rr.ttl<<endl;
 
291
      continue;
 
292
    }
 
293
    if(haveQTYPE) 
 
294
      break;
 
295
 
 
296
    try {
 
297
      rr.qtype=DNSRecordContent::TypeToNumber(nextpart);
 
298
      // cout<<"Got qtype ("<<rr.qtype.getCode()<<")\n";
 
299
      haveQTYPE=1;
 
300
      continue;
 
301
    }
 
302
    catch(...) {
 
303
      throw runtime_error("Parsing zone content line: '"+nextpart+"' doesn't look like a qtype, stopping loop");
 
304
    }
 
305
  }
 
306
  if(!haveQTYPE) 
 
307
    throw exception("Malformed line '"+d_line+"'");
 
308
 
 
309
  rr.content=d_line.substr(range.first);
 
310
 
 
311
  chopComment(rr.content);
 
312
 
 
313
  if(equals(rr.content, "@"))
 
314
    rr.content=d_zonename;
 
315
 
 
316
  if(findAndElide(rr.content, '(')) {      // have found a ( and elided it
 
317
    if(!findAndElide(rr.content, ')')) {
 
318
      while(getLine()) {
 
319
        chomp(d_line,"\r\n ");
 
320
        chopComment(d_line);
 
321
        trim(d_line);
 
322
        
 
323
        bool ended = findAndElide(d_line, ')');
 
324
        rr.content+=" "+d_line;
 
325
        if(ended)
 
326
          break;
 
327
      }
 
328
    }
 
329
  }
 
330
 
 
331
  vector<string> soaparts;
 
332
  switch(rr.qtype.getCode()) {
 
333
  case QType::MX:
 
334
  case QType::NS:
 
335
  case QType::CNAME:
 
336
  case QType::PTR:
 
337
  case QType::SRV:
 
338
  case QType::AFSDB:
 
339
    rr.content=toCanonic(d_zonename, rr.content);
 
340
    break;
 
341
 
 
342
  case QType::SOA:
 
343
    stringtok(soaparts, rr.content);
 
344
    if(soaparts.size() > 1) {
 
345
      soaparts[0]=toCanonic(d_zonename, soaparts[0]);
 
346
      soaparts[1]=toCanonic(d_zonename, soaparts[1]);
 
347
    }
 
348
    rr.content.clear();
 
349
    for(string::size_type n = 0; n < soaparts.size(); ++n) {
 
350
      if(n)
 
351
        rr.content.append(1,' ');
 
352
      if(n > 1)
 
353
        rr.content+=lexical_cast<string>(makeTTLFromZone(soaparts[n]));
 
354
      else
 
355
        rr.content+=soaparts[n];
 
356
    }
 
357
    break;
 
358
  default:;
 
359
  }
 
360
 
 
361
  rr.d_place=DNSResourceRecord::ANSWER;
69
362
  return true;
70
363
}
71
364
 
72
365
bool ZoneParserTNG::getLine()
73
366
{
74
 
  char buffer[1024];
75
 
  if(fgets(buffer, 1024, d_fp)) {
76
 
    d_line=buffer;
77
 
    return true;
 
367
  while(!d_fps.empty()) {
 
368
    char buffer[1024];
 
369
    if(fgets(buffer, 1024, d_fps.top())) {
 
370
      d_line=buffer;
 
371
      return true;
 
372
    }
 
373
    fclose(d_fps.top());
 
374
    d_fps.pop();
78
375
  }
79
376
  return false;
80
377
}
81
 
 
82
 
 
83
 
#if 0
84
 
int main(int argc, char** argv)
85
 
try
86
 
{
87
 
  reportAllTypes();
88
 
  ZoneParserTNG zpt(argv[1]);
89
 
  DNSResourceRecord rr;
90
 
  while(zpt.get(rr)) {
91
 
  }
92
 
  
93
 
 
94
 
}
95
 
catch(...)
96
 
{}
97
 
#endif