1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * Copyright (C) 2015 Benjamin Fry <benjaminfry@me.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
use std::collections::BTreeMap;
use std::io::Read;
use std::fs::File;

use ::error::*;
use ::rr::{ Name, RecordType, Record, DNSClass, RData};
use ::authority::{Authority, RrKey, ZoneType, RRSet};

use super::master_lex::{Lexer, Token};

/// ```text
/// 5. MASTER FILES
///
/// Master files are text files that contain RRs in text form.  Since the
/// contents of a zone can be expressed in the form of a list of RRs a
/// master file is most often used to define a zone, though it can be used
/// to list a cache's contents.  Hence, this section first discusses the
/// format of RRs in a master file, and then the special considerations when
/// a master file is used to create a zone in some name server.
///
/// 5.1. Format
///
/// The format of these files is a sequence of entries.  Entries are
/// predominantly line-oriented, though parentheses can be used to continue
/// a list of items across a line boundary, and text literals can contain
/// CRLF within the text.  Any combination of tabs and spaces act as a
/// delimiter between the separate items that make up an entry.  The end of
/// any line in the master file can end with a comment.  The comment starts
/// with a ";" (semicolon).
///
/// The following entries are defined:
///
///     <blank>[<comment>]
///
///     $ORIGIN <domain-name> [<comment>]
///
///     $INCLUDE <file-name> [<domain-name>] [<comment>]
///
///     <domain-name><rr> [<comment>]
///
///     <blank><rr> [<comment>]
///
/// Blank lines, with or without comments, are allowed anywhere in the file.
///
/// Two control entries are defined: $ORIGIN and $INCLUDE.  $ORIGIN is
/// followed by a domain name, and resets the current origin for relative
/// domain names to the stated name.  $INCLUDE inserts the named file into
/// the current file, and may optionally specify a domain name that sets the
/// relative domain name origin for the included file.  $INCLUDE may also
/// have a comment.  Note that a $INCLUDE entry never changes the relative
/// origin of the parent file, regardless of changes to the relative origin
/// made within the included file.
///
/// The last two forms represent RRs.  If an entry for an RR begins with a
/// blank, then the RR is assumed to be owned by the last stated owner.  If
/// an RR entry begins with a <domain-name>, then the owner name is reset.
///
/// <rr> contents take one of the following forms:
///
///     [<TTL>] [<class>] <type> <RDATA>
///
///     [<class>] [<TTL>] <type> <RDATA>
///
/// The RR begins with optional TTL and class fields, followed by a type and
/// RDATA field appropriate to the type and class.  Class and type use the
/// standard mnemonics, TTL is a decimal integer.  Omitted class and TTL
/// values are default to the last explicitly stated values.  Since type and
/// class mnemonics are disjoint, the parse is unique.  (Note that this
/// order is different from the order used in examples and the order used in
/// the actual RRs; the given order allows easier parsing and defaulting.)
///
/// <domain-name>s make up a large share of the data in the master file.
/// The labels in the domain name are expressed as character strings and
/// separated by dots.  Quoting conventions allow arbitrary characters to be
/// stored in domain names.  Domain names that end in a dot are called
/// absolute, and are taken as complete.  Domain names which do not end in a
/// dot are called relative; the actual domain name is the concatenation of
/// the relative part with an origin specified in a $ORIGIN, $INCLUDE, or as
/// an argument to the master file loading routine.  A relative name is an
/// error when no origin is available.
///
/// <character-string> is expressed in one or two ways: as a contiguous set
/// of characters without interior spaces, or as a string beginning with a "
/// and ending with a ".  Inside a " delimited string any character can
/// occur, except for a " itself, which must be quoted using \ (back slash).
///
/// Because these files are text files several special encodings are
/// necessary to allow arbitrary data to be loaded.  In particular:
///
///                 of the root.
///
/// @               A free standing @ is used to denote the current origin.
///
/// \X              where X is any character other than a digit (0-9), is
///                 used to quote that character so that its special meaning
///                 does not apply.  For example, "\." can be used to place
///                 a dot character in a label.
///
/// \DDD            where each D is a digit is the octet corresponding to
///                 the decimal number described by DDD.  The resulting
///                 octet is assumed to be text and is not checked for
///                 special meaning.
///
/// ( )             Parentheses are used to group data that crosses a line
///                 boundary.  In effect, line terminations are not
///                 recognized within parentheses.
///
/// ;               Semicolon is used to start a comment; the remainder of
///                 the line is ignored.
/// ```
pub struct Parser;

impl Parser {
  pub fn new() -> Self {
    Parser
  }

  pub fn parse_file(file: File, origin: Option<Name>, zone_type: ZoneType, allow_update: bool) -> ParseResult<Authority> {
    let mut file = file;
    let mut buf = String::new();

    // TODO, this should really use something to read line by line or some other method to
    //  keep the usage down. and be a custom lexer...
    try!(file.read_to_string(&mut buf));
    let lexer = Lexer::new(&buf);
    Self::new().parse(lexer, origin, zone_type, allow_update)
  }

  pub fn parse(&mut self, lexer: Lexer, origin: Option<Name>, zone_type: ZoneType, allow_update: bool) -> ParseResult<Authority> {
    let mut lexer = lexer;
    let mut records: BTreeMap<RrKey, RRSet> = BTreeMap::new();

    let mut origin: Option<Name> = origin;
    let mut current_name: Option<Name> = None;
    let mut rtype: Option<RecordType> = None;
    let mut ttl: Option<u32> = None;
    let mut class: Option<DNSClass> = None;
    let mut state = State::StartLine;
    let mut tokens: Vec<Token> = Vec::new();

    while let Some(t) = try!(lexer.next_token()) {
      state = match state {
        State::StartLine => {
          // current_name is not reset on the next line b/c it might be needed from the previous
          rtype = None;
          tokens.clear();

          match t {
            // if Dollar, then $INCLUDE or $ORIGIN
            Token::Include => unimplemented!(),
            Token::Origin => State::Origin,
            Token::Ttl => State::Ttl,

            // if CharData, then Name then ttl_class_type
            Token::CharData(ref data) => {
              current_name = Some(try!(Name::parse(data, origin.as_ref())));
              State::TtlClassType
            },

            // @ is a placeholder for specifying the current origin
            Token::At => {
              current_name = origin.clone(); // TODO a COW or RC would reduce copies...
              State::TtlClassType
            }

            // if blank, then nothing or ttl_class_type
            Token::Blank => {
              State::TtlClassType
            },
            Token::EOL => State::StartLine, // probably a comment
            _ => return Err(ParseError::UnexpectedToken(t)),
          }
        },
        State::Ttl => {
          match t {
            Token::CharData(ref data) => {
              ttl = Some(try!(Self::parse_time(data)));
              State::StartLine
            }
            _ => return Err(ParseError::UnexpectedToken(t)),
          }
        }
        State::Origin => {
          match t {
            Token::CharData(ref data) => {
              // TODO an origin was specified, should this be legal? definitely confusing...
              origin = Some(try!(Name::parse(data, None)));
              State::StartLine
            }
            _ => return Err(ParseError::UnexpectedToken(t)),
          }
        }
        State::Include => unimplemented!(),
        State::TtlClassType => {
          match t {
            // if number, TTL
            // Token::Number(ref num) => ttl = Some(*num),
            // One of Class or Type (these cannot be overlapping!)
            Token::CharData(ref data) => {
              // if it's a number it's a ttl
              let result: Result<u32, ParseError> = Self::parse_time(data);
              if result.is_ok() {
                ttl = result.ok();
                State::TtlClassType // hm, should this go to just ClassType?
              } else {
                // if can parse DNSClass, then class
                let result = DNSClass::from_str(data);
                if result.is_ok() {
                  class = result.ok();
                  State::TtlClassType
                } else {

                  // if can parse RecordType, then RecordType
                  rtype = Some(try!(RecordType::from_str(data)));
                  State::Record
                }
              }
            }
            // could be nothing if started with blank and is a comment, i.e. EOL
            Token::EOL => {
              State::StartLine // next line
            },
            _ => return Err(ParseError::UnexpectedToken(t)),
          }
        },
        State::Record => {
          // b/c of ownership rules, perhaps, just collect all the RData components as a list of
          //  tokens to pass into the processor
          match t {
            Token::EOL => {
              // call out to parsers for difference record types
              let rdata = try!(RData::parse(try!(rtype.ok_or(ParseError::RecordTypeNotSpecified)), &tokens, origin.as_ref()));

              // verify that we have everything we need for the record
              let mut record = Record::new();
              // TODO COW or RC would reduce mem usage, perhaps Name should have an intern()...
              //  might want to wait until RC.weak() stabilizes, as that would be needed for global
              //  memory where you want
              record.name(try!(current_name.clone().ok_or(ParseError::RecordNameNotSpecified)));
              record.rr_type(rtype.unwrap());
              record.dns_class(try!(class.ok_or(ParseError::RecordClassNotSpecified)));

              // slightly annoying, need to grab the TTL, then move rdata into the record,
              //  then check the Type again and have custom add logic.
              match rtype.unwrap() {
                RecordType::SOA => {
                  // TTL for the SOA is set internally...
                  // expire is for the SOA, minimum is default for records
                  if let RData::SOA(ref soa) = rdata {
                    // TODO, this looks wrong, get_expire() should be get_minimum(), right?
                    record.ttl(soa.get_expire() as u32); // the spec seems a little inaccurate with u32 and i32
                    if ttl.is_none() { ttl = Some(soa.get_minimum()); } // TODO: should this only set it if it's not set?
                  } else { assert!(false, "Invalid RData here, expected SOA: {:?}", rdata); }
                },
                _ => {
                  record.ttl(try!(ttl.ok_or(ParseError::RecordTTLNotSpecified)));
                },
              }

              // TODO validate record, e.g. the name of SRV record allows _ but others do not.

              // move the rdata into record...
              record.rdata(rdata);

              // add to the map
              let key = RrKey::new(record.get_name(), record.get_rr_type());

              match rtype.unwrap() {
                RecordType::SOA => {
                  let mut set = RRSet::new(record.get_name(), record.get_rr_type(), 0);
                  set.insert(record, 0);
                  if records.insert(key, set).is_some() {
                    return Err(ParseError::SoaAlreadySpecified);
                  }
                },
                _ => {
                  // add a Vec if it's not there, then add the record to the list
                  let mut set = records.entry(key).or_insert(RRSet::new(record.get_name(), record.get_rr_type(), 0));
                  set.insert(record, 0);
                },
              }

              State::StartLine
            },
            _ => { tokens.push(t); State::Record },
          }
        },
      }
    }

    //
    // build the Authority and return.
    Ok(Authority::new(try!(origin.ok_or(ParseError::OriginIsUndefined)), records, zone_type, allow_update))
  }

  /// parses the string following the rules from:
  ///  https://tools.ietf.org/html/rfc2308 (NXCaching RFC) and
  ///  http://www.zytrax.com/books/dns/apa/time.html
  ///
  /// default is seconds
  /// #s = seconds = # x 1 seconds (really!)
  /// #m = minutes = # x 60 seconds
  /// #h = hours   = # x 3600 seconds
  /// #d = day     = # x 86400 seconds
  /// #w = week    = # x 604800 seconds
  ///
  /// returns the result of the parsing or and error
  ///
  /// # Example
  /// ```
  /// use trust_dns::serialize::txt::Parser;
  ///
  /// assert_eq!(Parser::parse_time("0").unwrap(),  0);
  /// assert_eq!(Parser::parse_time("s").unwrap(),  0);
  /// assert_eq!(Parser::parse_time("0s").unwrap(), 0);
  /// assert_eq!(Parser::parse_time("1").unwrap(),  1);
  /// assert_eq!(Parser::parse_time("1S").unwrap(), 1);
  /// assert_eq!(Parser::parse_time("1s").unwrap(), 1);
  /// assert_eq!(Parser::parse_time("1M").unwrap(), 60);
  /// assert_eq!(Parser::parse_time("1m").unwrap(), 60);
  /// assert_eq!(Parser::parse_time("1H").unwrap(), 3600);
  /// assert_eq!(Parser::parse_time("1h").unwrap(), 3600);
  /// assert_eq!(Parser::parse_time("1D").unwrap(), 86400);
  /// assert_eq!(Parser::parse_time("1d").unwrap(), 86400);
  /// assert_eq!(Parser::parse_time("1W").unwrap(), 604800);
  /// assert_eq!(Parser::parse_time("1w").unwrap(), 604800);
  /// assert_eq!(Parser::parse_time("1s2d3w4h2m").unwrap(), 1+2*86400+3*604800+4*3600+2*60);
  /// assert_eq!(Parser::parse_time("3w3w").unwrap(), 3*604800+3*604800);
  /// ```
  pub fn parse_time(ttl_str: &str) -> ParseResult<u32> {
    let mut value: u32 = 0;
    let mut collect: u32 = 0;

    for c in ttl_str.chars() {
      match c {
        // TODO, should these all be checked operations?
        '0' ... '9' => { collect *= 10; collect += try!(c.to_digit(10).ok_or(ParseError::CharToIntError(c))); },
        'S' | 's'  => { value += collect; collect = 0; },
        'M' | 'm'  => { value += collect * 60; collect = 0; },
        'H' | 'h'  => { value += collect * 3600; collect = 0; },
        'D' | 'd'  => { value += collect * 86400; collect = 0; },
        'W' | 'w'  => { value += collect * 604800; collect = 0; },
        _ => return Err(ParseError::ParseTimeError(ttl_str.to_string())),
      }
    }

    return Ok(value + collect); // collects the initial num, or 0 if it was already collected
  }
}

#[allow(unused)]
enum State {
  StartLine,       // start of line, @, $<WORD>, Name, Blank
  TtlClassType,    // [<TTL>] [<class>] <type>,
  Ttl,             // $TTL <time>
  Record,
  Include,         // $INCLUDE <filename>
  Origin,
}