ms-dnsp 0.1.0

Pure-Rust parser and builder for the MS-DNSP dnsRecord blob stored in Active-Directory-integrated DNS zones.
Documentation
//! Parse real `dnsRecord` blobs pulled off a domain controller and prove the round-trip.
//!
//! ```text
//! cargo run --example parse_dump -- dns_dump/
//! ```
//!
//! Every `*.bin` in the directory is one `dnsRecord` attribute value. A blob that parses but
//! does not re-encode byte-for-byte is a bug in this crate, not in the DC.

use ms_dnsp::{DnsRecord, RecordData};
use std::process::ExitCode;

fn main() -> ExitCode {
    let Some(dir) = std::env::args().nth(1) else {
        eprintln!("usage: parse_dump <directory of .bin dnsRecord values>");
        return ExitCode::FAILURE;
    };
    let entries = match std::fs::read_dir(&dir) {
        Ok(e) => e,
        Err(e) => {
            eprintln!("cannot read {dir}: {e}");
            return ExitCode::FAILURE;
        }
    };

    let (mut total, mut failed, mut lossy, mut unknown) = (0usize, 0usize, 0usize, 0usize);
    for entry in entries.flatten() {
        let path = entry.path();
        if path.extension().and_then(|s| s.to_str()) != Some("bin") {
            continue;
        }
        let raw = match std::fs::read(&path) {
            Ok(r) => r,
            Err(e) => {
                eprintln!("{}: {e}", path.display());
                failed += 1;
                continue;
            }
        };
        total += 1;
        let name = path.file_name().unwrap_or_default().to_string_lossy();

        match DnsRecord::parse(&raw) {
            Ok(rec) => {
                let again = rec.to_bytes();
                let mark = if again == raw {
                    ""
                } else {
                    lossy += 1;
                    "  ← RE-ENCODE DIFFERS"
                };
                if matches!(rec.data, RecordData::Unknown { .. }) {
                    unknown += 1;
                }
                println!(
                    "{name:<40} type {:<5} ttl {:<6} {}{}",
                    rec.record_type(),
                    rec.ttl,
                    summarize(&rec.data),
                    mark
                );
                if again != raw {
                    println!("    original: {}", hex(&raw));
                    println!("    re-coded: {}", hex(&again));
                }
            }
            Err(e) => {
                failed += 1;
                println!("{name:<40} PARSE FAILED: {e}");
                println!("    {}", hex(&raw));
            }
        }
    }

    println!("\n{total} record(s) · {failed} unparsed · {lossy} lossy · {unknown} unmodelled type(s)");
    if failed > 0 || lossy > 0 {
        return ExitCode::FAILURE;
    }
    ExitCode::SUCCESS
}

fn summarize(d: &RecordData) -> String {
    match d {
        RecordData::A(ip) => format!("A {ip}"),
        RecordData::Aaaa(ip) => format!("AAAA {ip}"),
        RecordData::Ns(n) => format!("NS {n}"),
        RecordData::Cname(n) => format!("CNAME {n}"),
        RecordData::Ptr(n) => format!("PTR {n}"),
        RecordData::Soa {
            primary_server,
            serial,
            ..
        } => format!("SOA {primary_server} serial={serial}"),
        RecordData::Mx {
            preference,
            exchange,
        } => format!("MX {preference} {exchange}"),
        RecordData::Srv {
            priority,
            weight,
            port,
            target,
        } => format!("SRV {priority} {weight} {port} {target}"),
        RecordData::Txt(v) => format!("TXT {v:?}"),
        RecordData::Tombstone(t) => format!("tombstone {t}"),
        RecordData::Unknown { ty, bytes } => format!("type {ty} ({} bytes, unmodelled)", bytes.len()),
    }
}

fn hex(b: &[u8]) -> String {
    b.iter().map(|x| format!("{x:02x}")).collect()
}