From fe48c1412d9d988831cb1580535aaac702c0b3f6 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Mon, 8 May 2023 04:01:33 +0200 Subject: [PATCH] Print a human-readable summary of a zone. --- src/client/main.cr | 10 +++++++++- src/storage/zone.cr | 31 +++++++++++++++++++++++++++---- tools/write-zone-file.cr | 13 +++++++++---- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/client/main.cr b/src/client/main.cr index 2314cee..00180d8 100644 --- a/src/client/main.cr +++ b/src/client/main.cr @@ -100,7 +100,15 @@ class Actions domains.each do |domain| begin pp! domain - pp! @dnsmanagerd.user_zone_get domain + response = @dnsmanagerd.user_zone_get domain + case response + when DNSManager::Response::Zone + pp! response.zone + puts response.zone + else + puts "Didn't get the zone" + pp! response + end rescue e puts "error for user_zone_get: #{e.message}" end diff --git a/src/storage/zone.cr b/src/storage/zone.cr index 2059e2f..c7fdef3 100644 --- a/src/storage/zone.cr +++ b/src/storage/zone.cr @@ -46,6 +46,10 @@ class DNSManager::Storage::Zone def get_errors : Array(Error) [] of Error end + + def to_s(io : IO) + io << "#{@name} #{@ttl} IN #{@rrtype.upcase} #{@target}\n" + end end class SOA < ResourceRecord @@ -63,6 +67,15 @@ class DNSManager::Storage::Zone @rrtype = "soa" end + def to_s(io : IO) + io << "#{name} #{ttl} #{target} #{@rrtype.upcase} (#{mname} #{rname}\n" + io << "\t\t#{serial}; serial\n" + io << "\t\t#{refresh}; refresh\n" + io << "\t\t#{retry}; retry\n" + io << "\t\t#{expire}; expire\n" + io << "\t)\n" + end + def get_errors : Array(Error) errors = [] of Error @@ -209,6 +222,10 @@ class DNSManager::Storage::Zone @rrtype = "mx" end + def to_s(io : IO) + io << "#{@name} #{@ttl} IN #{@rrtype.upcase} #{@priority} #{@target}\n" + end + def get_errors : Array(Error) errors = [] of Error @@ -220,8 +237,11 @@ class DNSManager::Storage::Zone errors << "MX invalid ttl: #{@ttl}, shouldn't be less than #{Zone.ttl_limit_min}" end - unless Zone.is_domain_valid? @target - errors << "MX invalid target (domain): #{@target}" + # MX target can either be a subdomain or a FQDN. + unless Zone.is_subdomain_valid? @target + unless Zone.is_domain_valid? @target + errors << "MX invalid target (domain): #{@target}" + end end errors @@ -256,7 +276,10 @@ class DNSManager::Storage::Zone end def to_s(io : IO) - io << "TEST" + io << "domain: #{@domain}\n" + @resources.each do |rr| + io << rr + end end def get_errors? : Array(Error)? @@ -350,7 +373,7 @@ class DNSManager::Storage::Zone false end rescue e - Baguette::Log.warning "wrong IPv4 address: #{address}" + Baguette::Log.warning "wrong IPv6 address: #{address}" false end diff --git a/tools/write-zone-file.cr b/tools/write-zone-file.cr index 853ead5..bdb7611 100644 --- a/tools/write-zone-file.cr +++ b/tools/write-zone-file.cr @@ -19,17 +19,22 @@ zone = DSZ.new domain # Add some values. # -zone.resources << DSZ::A.new "www", 600.to_u32, "10.0.0.1" -zone.resources << DSZ::A.new "www2", 600.to_u32, "10.0.0.2" - zone.resources << DSZ::SOA.new "#{domain}.", # name 60.to_u32, # TTL "IN", # target (??) "ns0.some-example.com.", # Master Name Server for the zone - "john\.doe.#{domain}" # admin email address + "john\\.doe.#{domain}" # admin email address + +zone.resources << DSZ::A.new "www", 600.to_u32, "10.0.0.1" +zone.resources << DSZ::A.new "www2", 600.to_u32, "10.0.0.2" zone.resources << DSZ::NS.new "#{domain}.", 3600.to_u32, "ns1.some-example.com." zone.resources << DSZ::NS.new "#{domain}.", 3600.to_u32, "ns0.some-example.com." +zone.resources << DSZ::A.new "mail", 300.to_u32, "10.0.0.10" +zone.resources << DSZ::CNAME.new "mail2", 600.to_u32, "www" +zone.resources << DSZ::MX.new "mail", 300.to_u32, "mail", 10 +zone.resources << DSZ::MX.new "mail", 300.to_u32, "mail2", 5 + File.write("#{domain}.json", zone.to_json)