bindcar 0.6.0

HTTP REST API for managing BIND9 zones via rndc
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Copyright (c) 2025 Erick Bourgeois, firestoned
// SPDX-License-Identifier: MIT

//! RNDC output parser
//!
//! This module provides parsers for BIND9 RNDC command outputs using nom.
//!
//! # Examples
//!
//! ```rust
//! use bindcar::rndc_parser::parse_showzone;
//!
//! let output = r#"zone "example.com" { type primary; file "/var/cache/bind/example.com.zone"; };"#;
//! let config = parse_showzone(output).unwrap();
//! assert_eq!(config.zone_name, "example.com");
//! ```

use crate::rndc_types::{
    AutoDnssecMode, CheckNamesMode, DnsClass, ForwardMode, ForwarderSpec, MasterfileFormat,
    NotifyMode, PrimarySpec, ZoneConfig, ZoneType,
};
use nom::{
    branch::alt,
    bytes::complete::{tag, take_until, take_while1},
    character::complete::{char, multispace0},
    combinator::{map, opt, recognize},
    multi::many0,
    sequence::{delimited, preceded, terminated},
    IResult, Parser,
};
use std::net::IpAddr;
use thiserror::Error;

/// RNDC parse errors
#[derive(Debug, Error)]
pub enum RndcParseError {
    #[error("Parse error: {0}")]
    ParseError(String),

    #[error("Invalid zone type: {0}")]
    InvalidZoneType(String),

    #[error("Invalid DNS class: {0}")]
    InvalidDnsClass(String),

    #[error("Invalid IP address: {0}")]
    InvalidIpAddress(String),

    #[error("Missing required field: {0}")]
    MissingField(String),

    #[error("Incomplete input")]
    Incomplete,
}

pub type ParseResult<T> = Result<T, RndcParseError>;

// ========== Common Parser Primitives ==========

/// Skip whitespace around a parser
fn ws<'a, F, O>(inner: F) -> impl Parser<&'a str, Output = O, Error = nom::error::Error<&'a str>>
where
    F: Parser<&'a str, Output = O, Error = nom::error::Error<&'a str>>,
{
    delimited(multispace0, inner, multispace0)
}

/// Parse a semicolon
fn semicolon(input: &str) -> IResult<&str, char> {
    ws(char(';')).parse(input)
}

/// Parse a quoted string: "example"
pub(crate) fn quoted_string(input: &str) -> IResult<&str, String> {
    let (input, content) = delimited(char('"'), take_until("\""), char('"')).parse(input)?;
    Ok((input, content.to_string()))
}

/// Parse an identifier (alphanumeric with hyphens and underscores)
fn identifier(input: &str) -> IResult<&str, &str> {
    take_while1(|c: char| c.is_alphanumeric() || c == '_' || c == '-')(input)
}

/// Parse an IP address (IPv4 or IPv6), optionally with CIDR notation
/// Examples: 192.168.1.1, 192.168.1.1/32, 2001:db8::1, 2001:db8::1/128
pub(crate) fn ip_addr(input: &str) -> IResult<&str, IpAddr> {
    // Try to parse as much as possible that looks like an IP address
    let (input, addr_str) = recognize(take_while1(|c: char| {
        c.is_ascii_hexdigit() || c == '.' || c == ':'
    }))
    .parse(input)?;

    // Try to parse the string as an IP address
    let addr = match addr_str.parse::<IpAddr>() {
        Ok(addr) => addr,
        Err(_) => {
            return Err(nom::Err::Error(nom::error::Error::new(
                input,
                nom::error::ErrorKind::Verify,
            )))
        }
    };

    // Check for optional CIDR suffix (e.g., /32 or /128) and consume it
    let (input, _) =
        opt(preceded(char('/'), take_while1(|c: char| c.is_numeric()))).parse(input)?;

    Ok((input, addr))
}

/// Parse IP address with optional port
pub(crate) fn ip_with_port(input: &str) -> IResult<&str, PrimarySpec> {
    let (input, addr) = ws(ip_addr).parse(input)?;
    let (input, port) = opt(preceded(
        ws(tag("port")),
        map(take_while1(|c: char| c.is_numeric()), |s: &str| {
            s.parse::<u16>().ok()
        }),
    ))
    .parse(input)?;

    Ok((
        input,
        PrimarySpec {
            address: addr,
            port: port.flatten(),
        },
    ))
}

/// Parse a list of IP addresses: { addr; addr; }
fn ip_list(input: &str) -> IResult<&str, Vec<IpAddr>> {
    delimited(
        ws(char('{')),
        many0(terminated(ws(ip_addr), semicolon)),
        ws(char('}')),
    )
    .parse(input)
}

/// Parse a list of primary specs: { addr; addr port 5353; }
fn primary_list(input: &str) -> IResult<&str, Vec<PrimarySpec>> {
    delimited(
        ws(char('{')),
        many0(terminated(ip_with_port, semicolon)),
        ws(char('}')),
    )
    .parse(input)
}

// ========== Zone Configuration Parser ==========

/// Statement types within a zone configuration
#[derive(Debug)]
#[allow(dead_code)]
enum ZoneStatement {
    // Core
    Type(ZoneType),
    File(String),

    // Primary/Secondary
    Primaries(Vec<PrimarySpec>),
    AlsoNotify(Vec<IpAddr>),
    Notify(NotifyMode),

    // Access Control
    AllowQuery(Vec<IpAddr>),
    AllowTransfer(Vec<IpAddr>),
    AllowUpdate(Vec<IpAddr>),
    AllowUpdateRaw(String),
    AllowUpdateForwarding(Vec<IpAddr>),
    AllowNotify(Vec<IpAddr>),

    // Transfer Control
    MaxTransferTimeIn(u32),
    MaxTransferTimeOut(u32),
    MaxTransferIdleIn(u32),
    MaxTransferIdleOut(u32),
    TransferSource(IpAddr),
    TransferSourceV6(IpAddr),
    NotifySource(IpAddr),
    NotifySourceV6(IpAddr),

    // Dynamic Updates
    UpdatePolicy(String),
    Journal(String),
    IxfrFromDifferences(bool),

    // DNSSEC
    InlineSigning(bool),
    AutoDnssec(AutoDnssecMode),
    KeyDirectory(String),
    SigValidityInterval(u32),
    DnskeySigValidity(u32),

    // Forwarding
    Forward(ForwardMode),
    Forwarders(Vec<ForwarderSpec>),

    // Zone Maintenance
    CheckNames(CheckNamesMode),
    CheckMx(CheckNamesMode),
    CheckIntegrity(bool),
    MasterfileFormat(MasterfileFormat),
    MaxZoneTtl(u32),

    // Refresh/Retry
    MaxRefreshTime(u32),
    MinRefreshTime(u32),
    MaxRetryTime(u32),
    MinRetryTime(u32),

    // Miscellaneous
    MultiMaster(bool),
    RequestIxfr(bool),
    RequestExpire(bool),

    // Catch-all for unknown options
    Unknown(String, String), // (option_name, raw_value)
}

/// Parse zone type statement: type primary;
fn parse_type_statement(input: &str) -> IResult<&str, ZoneStatement> {
    let (input, _) = ws(tag("type")).parse(input)?;
    let (input, type_str) = ws(identifier).parse(input)?;
    let (input, _) = semicolon(input)?;

    let zone_type = ZoneType::parse(type_str).ok_or_else(|| {
        nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Verify))
    })?;

    Ok((input, ZoneStatement::Type(zone_type)))
}

/// Parse file statement: file "/path/to/file";
fn parse_file_statement(input: &str) -> IResult<&str, ZoneStatement> {
    let (input, _) = ws(tag("file")).parse(input)?;
    let (input, file) = ws(quoted_string).parse(input)?;
    let (input, _) = semicolon(input)?;
    Ok((input, ZoneStatement::File(file)))
}

/// Parse primaries statement: primaries { addr; addr port 5353; };
/// Also handles legacy "masters" keyword
fn parse_primaries_statement(input: &str) -> IResult<&str, ZoneStatement> {
    let (input, _) = ws(alt((tag("primaries"), tag("masters")))).parse(input)?;
    let (input, primaries) = primary_list(input)?;
    let (input, _) = semicolon(input)?;
    Ok((input, ZoneStatement::Primaries(primaries)))
}

/// Parse also-notify statement: also-notify { addr; addr; };
fn parse_also_notify_statement(input: &str) -> IResult<&str, ZoneStatement> {
    let (input, _) = ws(tag("also-notify")).parse(input)?;
    let (input, addrs) = ip_list(input)?;
    let (input, _) = semicolon(input)?;
    Ok((input, ZoneStatement::AlsoNotify(addrs)))
}

/// Parse allow-transfer statement: allow-transfer { addr; addr; };
fn parse_allow_transfer_statement(input: &str) -> IResult<&str, ZoneStatement> {
    let (input, _) = ws(tag("allow-transfer")).parse(input)?;
    let (input, addrs) = ip_list(input)?;
    let (input, _) = semicolon(input)?;
    Ok((input, ZoneStatement::AllowTransfer(addrs)))
}

/// Parse allow-update statement: allow-update { addr; addr; }; or allow-update { key "name"; };
/// Captures both IP addresses and raw directive for key-based updates
fn parse_allow_update_statement(input: &str) -> IResult<&str, ZoneStatement> {
    let (input, _) = ws(tag("allow-update")).parse(input)?;

    // Capture the start position to extract raw content
    let start_input = input;

    // Parse the content between braces
    let (input, _) = ws(char('{')).parse(input)?;

    // Collect IP addresses and check for key references
    let mut addrs = Vec::new();
    let mut has_key_ref = false;
    let mut remaining = input;

    loop {
        // Skip whitespace
        let (input, _) = multispace0(remaining)?;

        // Check if we've reached the closing brace
        if let Ok((input, _)) = char::<_, nom::error::Error<&str>>('}')(input) {
            remaining = input;
            break;
        }

        // Check for "key" keyword
        if let Ok((input, _)) = ws(tag("key")).parse(input) {
            has_key_ref = true;
            // Skip to the next semicolon
            let (input, _) = take_until(";")(input)?;
            let (input, _) = char(';')(input)?;
            remaining = input;
        } else if let Ok((input, addr)) = ip_addr(input) {
            // Try to parse an IP address
            addrs.push(addr);
            // Consume the semicolon
            let (input, _) = semicolon(input)?;
            remaining = input;
        } else {
            // Skip to the next semicolon (handles other unknown statements)
            let (input, _) = take_until(";")(input)?;
            let (input, _) = char(';')(input)?;
            remaining = input;
        }
    }

    let (input, _) = semicolon(remaining)?;

    // If we found key references, capture the raw directive
    if has_key_ref {
        // Extract raw content from start to end
        let raw_len = start_input.len() - input.len();
        let raw_content = &start_input[..raw_len];
        Ok((
            input,
            ZoneStatement::AllowUpdateRaw(raw_content.to_string()),
        ))
    } else {
        Ok((input, ZoneStatement::AllowUpdate(addrs)))
    }
}

/// Parse an unknown/generic zone statement (catch-all)
/// Format: option-name value; or option-name { ... };
fn parse_unknown_statement(input: &str) -> IResult<&str, ZoneStatement> {
    // Parse the option name
    let (input, option_name) = ws(identifier).parse(input)?;

    // Capture starting position for value
    let start_input = input;

    // Try to parse value - could be a simple value or a block
    let (input, _value) = alt((
        // Block value: { ... };
        delimited(ws(char('{')), take_until("}"), ws(char('}'))),
        // Simple value (anything until semicolon)
        take_until(";"),
    ))
    .parse(input)?;

    // Calculate raw value
    let value_len = start_input.len() - input.len();
    let raw_value = start_input[..value_len].trim().to_string();

    let (input, _) = semicolon(input)?;

    Ok((
        input,
        ZoneStatement::Unknown(option_name.to_string(), raw_value),
    ))
}

/// Parse any zone statement
fn parse_zone_statement(input: &str) -> IResult<&str, ZoneStatement> {
    alt((
        parse_type_statement,
        parse_file_statement,
        parse_primaries_statement,
        parse_also_notify_statement,
        parse_allow_transfer_statement,
        parse_allow_update_statement,
        // Catch-all for unknown options (must be last)
        parse_unknown_statement,
    ))
    .parse(input)
}

/// Parse complete zone configuration from showzone output
///
/// Parses output from `rndc showzone <zonename>` which has the format:
/// ```text
/// zone "example.com" { type primary; file "/path"; };
/// ```
///
/// Or with optional class:
/// ```text
/// zone "example.com" IN { type primary; file "/path"; };
/// ```
fn parse_zone_config_internal(input: &str) -> IResult<&str, ZoneConfig> {
    // Parse: zone "name" [class] { statements };
    let (input, _) = ws(tag("zone")).parse(input)?;
    let (input, zone_name) = ws(quoted_string).parse(input)?;

    // Optional class (IN, CH, HS)
    let (input, class) = opt(ws(alt((tag("IN"), tag("CH"), tag("HS"))))).parse(input)?;
    let class = match class {
        Some("IN") => DnsClass::IN,
        Some("CH") => DnsClass::CH,
        Some("HS") => DnsClass::HS,
        _ => DnsClass::IN, // Default to IN
    };

    // Parse zone block
    let (input, statements) =
        delimited(ws(char('{')), many0(parse_zone_statement), ws(tag("};"))).parse(input)?;

    // Build ZoneConfig from statements
    let mut config = ZoneConfig::new(zone_name, ZoneType::Primary); // Default type
    config.class = class;

    for stmt in statements {
        match stmt {
            // Core
            ZoneStatement::Type(t) => config.zone_type = t,
            ZoneStatement::File(f) => config.file = Some(f),

            // Primary/Secondary
            ZoneStatement::Primaries(p) => config.primaries = Some(p),
            ZoneStatement::AlsoNotify(a) => config.also_notify = Some(a),
            ZoneStatement::Notify(n) => config.notify = Some(n),

            // Access Control
            ZoneStatement::AllowQuery(a) => config.allow_query = Some(a),
            ZoneStatement::AllowTransfer(a) => config.allow_transfer = Some(a),
            ZoneStatement::AllowUpdate(a) => config.allow_update = Some(a),
            ZoneStatement::AllowUpdateRaw(raw) => config.allow_update_raw = Some(raw),
            ZoneStatement::AllowUpdateForwarding(a) => config.allow_update_forwarding = Some(a),
            ZoneStatement::AllowNotify(a) => config.allow_notify = Some(a),

            // Transfer Control
            ZoneStatement::MaxTransferTimeIn(v) => config.max_transfer_time_in = Some(v),
            ZoneStatement::MaxTransferTimeOut(v) => config.max_transfer_time_out = Some(v),
            ZoneStatement::MaxTransferIdleIn(v) => config.max_transfer_idle_in = Some(v),
            ZoneStatement::MaxTransferIdleOut(v) => config.max_transfer_idle_out = Some(v),
            ZoneStatement::TransferSource(ip) => config.transfer_source = Some(ip),
            ZoneStatement::TransferSourceV6(ip) => config.transfer_source_v6 = Some(ip),
            ZoneStatement::NotifySource(ip) => config.notify_source = Some(ip),
            ZoneStatement::NotifySourceV6(ip) => config.notify_source_v6 = Some(ip),

            // Dynamic Updates
            ZoneStatement::UpdatePolicy(p) => config.update_policy = Some(p),
            ZoneStatement::Journal(j) => config.journal = Some(j),
            ZoneStatement::IxfrFromDifferences(v) => config.ixfr_from_differences = Some(v),

            // DNSSEC
            ZoneStatement::InlineSigning(v) => config.inline_signing = Some(v),
            ZoneStatement::AutoDnssec(m) => config.auto_dnssec = Some(m),
            ZoneStatement::KeyDirectory(d) => config.key_directory = Some(d),
            ZoneStatement::SigValidityInterval(v) => config.sig_validity_interval = Some(v),
            ZoneStatement::DnskeySigValidity(v) => config.dnskey_sig_validity = Some(v),

            // Forwarding
            ZoneStatement::Forward(m) => config.forward = Some(m),
            ZoneStatement::Forwarders(f) => config.forwarders = Some(f),

            // Zone Maintenance
            ZoneStatement::CheckNames(m) => config.check_names = Some(m),
            ZoneStatement::CheckMx(m) => config.check_mx = Some(m),
            ZoneStatement::CheckIntegrity(v) => config.check_integrity = Some(v),
            ZoneStatement::MasterfileFormat(f) => config.masterfile_format = Some(f),
            ZoneStatement::MaxZoneTtl(v) => config.max_zone_ttl = Some(v),

            // Refresh/Retry
            ZoneStatement::MaxRefreshTime(v) => config.max_refresh_time = Some(v),
            ZoneStatement::MinRefreshTime(v) => config.min_refresh_time = Some(v),
            ZoneStatement::MaxRetryTime(v) => config.max_retry_time = Some(v),
            ZoneStatement::MinRetryTime(v) => config.min_retry_time = Some(v),

            // Miscellaneous
            ZoneStatement::MultiMaster(v) => config.multi_master = Some(v),
            ZoneStatement::RequestIxfr(v) => config.request_ixfr = Some(v),
            ZoneStatement::RequestExpire(v) => config.request_expire = Some(v),

            // Catch-all
            ZoneStatement::Unknown(key, value) => {
                config.raw_options.insert(key, value);
            }
        }
    }

    Ok((input, config))
}

/// Parse `rndc showzone` output
///
/// # Examples
///
/// ```rust
/// use bindcar::rndc_parser::parse_showzone;
///
/// let output = r#"zone "example.com" { type primary; file "/var/cache/bind/example.com.zone"; };"#;
/// let config = parse_showzone(output).unwrap();
/// assert_eq!(config.zone_name, "example.com");
/// ```
pub fn parse_showzone(input: &str) -> ParseResult<ZoneConfig> {
    match parse_zone_config_internal(input.trim()) {
        Ok((_, config)) => Ok(config),
        Err(nom::Err::Error(e)) | Err(nom::Err::Failure(e)) => {
            Err(RndcParseError::ParseError(format!("Parse failed: {:?}", e)))
        }
        Err(nom::Err::Incomplete(_)) => Err(RndcParseError::Incomplete),
    }
}