From 407cfc874aa7b8d1f40f59c47596189118aad621 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sat, 23 Mar 2024 12:22:33 +0100 Subject: [PATCH] Properly handle multiple lines targets (TXT, DKIM). --- src/storage/zone.cr | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/src/storage/zone.cr b/src/storage/zone.cr index e90a40b..6184ef8 100644 --- a/src/storage/zone.cr +++ b/src/storage/zone.cr @@ -212,6 +212,15 @@ class DNSManager::Storage::Zone errors end + + def to_s(io : IO) + io << "(#{ "%4d" % @rrid }) " + io << "#{ "%30s" % @name} #{ "%6d" % @ttl} #{ "%10s" % @rrtype } #{quoted_string @target}\n" + end + + def to_bind9(io : IO) + io << "#{@name} #{@ttl} IN #{@rrtype} #{split_line @target}\n" + end end class PTR < ResourceRecord @@ -523,17 +532,11 @@ class DNSManager::Storage::Zone def to_s(io : IO) io << "(#{ "%4d" % @rrid }) " - io << "#{ "%30s" % @name} #{ "%6d" % @ttl} DKIM " - io << "( " - io << split_line dkim.to_s - io << " )\n" + io << "#{ "%30s" % @name} #{ "%6d" % @ttl} DKIM #{split_line dkim.to_s}\n" end def to_bind9(io : IO) - io << "#{@name} #{@ttl} IN TXT " - io << "( " - io << split_line dkim.to_s - io << " )\n" + io << "#{@name} #{@ttl} IN TXT #{split_line dkim.to_s}\n" end end @@ -832,16 +835,30 @@ def qualifier_to_string(qualifier : DNSManager::Storage::Zone::SPF::Qualifier) : end def split_line(line : String) : String + max_char_per_line = 50 + iostr = IO::Memory.new line - slice = Bytes.new(50) + slice = Bytes.new(max_char_per_line) lines = "" + first_time = true while rbytes = iostr.read slice break if rbytes == 0 - lines += '"' - lines += String.new slice[0..rbytes -1] - lines += '"' - lines += "\n\t" + if first_time + first_time = false + else + lines += "\n\t" + end + lines += quoted_string (String.new slice[0..rbytes -1]) end + lines = "( #{lines} )" if lines.size > max_char_per_line lines end + +def quoted_string(value : String) : String + String.build do |io| + io << '"' + io << value + io << '"' + end +end