-
Notifications
You must be signed in to change notification settings - Fork 5
/
danectl-zonefile
executable file
·178 lines (146 loc) · 4.42 KB
/
danectl-zonefile
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
#!/usr/bin/env perl
BEGIN { pop @INC if $INC[-1] eq '.' }
use 5.006;
use warnings;
use strict;
# danectl - DNSSEC DANE implementation manager
# https://raf.org/danectl
# https://github.com/raforg/danectl
# https://codeberg.org/raforg/danectl
#
# Copyright (C) 2021-2023 raf <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <https://www.gnu.org/licenses/>.
#
# 20230718 raf <[email protected]>
# danectl-zonefile - Adapt danectl DNS RR output to modify a BIND9 zonefile
# usage: danectl rollover <cert-name> | danectl-zonectl <zonefile>
my $zonefile = shift or die "usage: $0 zonefile\n";
die "usage: $0 zonefile\n" unless $zonefile;
die "usage: $0 is not a readable/writable file\n" unless -f $zonefile && -r _ && -w _;
my $rec = qr/(?:TLSA|SSHFP|SMIMEA|OPENPGPKEY)/i;
# Read the zonefile
my $zone = do
{
local $/;
open my $fh, '<', $zonefile or die "$0: failed to open $zonefile for reading: $!\n";
my $data = <$fh>;
close $fh;
$data;
};
# Create a backup of the zonefile
open my $bakfh, '>', "$zonefile.bak" or die "$0: failed to open $zonefile.bak for writing: $!\n";
print $bakfh $zone;
close $bakfh;
# Read (from stdin) the modifications to apply to the zonefile
my %deletions;
my %additions;
my %comments;
my $comments = '';
while (<>)
{
# Skip real comments and blank lines
$comments .= $_ if /^; /; # Record comments for additions
next if /^; /;
next if /^$/;
# Deletions (single line)
if (/^;(\S+\.\tIN\t$rec)\t([^(].*)$/smx)
{
my ($name, $data) = ($1, $2);
$deletions{$name} = [] unless exists $deletions{$name};
push @{$deletions{$name}}, $data;
$comments = '';
next;
}
# Deletions (multiple lines)
if (/^;(\S+\.\tIN\t$rec)\t([(])$/smx)
{
my ($name, $data) = ($1, $2);
$data .= "\n";
while (<>)
{
die "$0: unexpected line in multi-line deletion: $_" unless /^;/;
$data .= substr($_, 1); # Exclude the leading ;
last if /^;\t[)]$/;
}
$deletions{$name} = [] unless exists $deletions{$name};
push @{$deletions{$name}}, $data;
$comments = '';
next;
}
# Additions (single line)
if (/^(\S+\.\tIN\t$rec)\t([^(].*)$/smx)
{
my ($name, $data) = ($1, $2);
$additions{$name} = [] unless exists $additions{$name};
push @{$additions{$name}}, $data;
$comments{$name} = $comments unless exists $comments{$name};
$comments = '';
next;
}
# Additions (multiple lines)
if (/^(\S+\.\tIN\t$rec)\t([(])$/smx)
{
my ($name, $data) = ($1, $2);
$data .= "\n";
while (<>)
{
$data .= $_;
last if /^\t[)]$/;
}
$additions{$name} = [] unless exists $additions{$name};
push @{$additions{$name}}, $data;
$comments{$name} = $comments unless exists $comments{$name};
$comments = '';
next;
}
}
# Replace deletions with additions if possible
for my $name (sort keys %deletions)
{
while (scalar @{$deletions{$name}})
{
my $del_data = shift @{$deletions{$name}};
my $add_data = shift @{$additions{$name}};
my $old = "$name\t$del_data";
my $new = (defined $add_data) ? "$name\t$add_data" : '';
my $offset = index($zone, $old);
my $zone_before = $zone;
substr($zone, $offset, length($old), $new) unless $offset == -1;
my $changed = ($zone ne $zone_before);
unshift @{$additions{$name}}, $add_data unless $changed; # Handle this below
}
}
# Otherwise, append any remaining additions
for my $name (sort keys %additions)
{
my $comment = "\n" . $comments{$name};
my $first = 1;
while (scalar @{$additions{$name}})
{
my $add_data = shift @{$additions{$name}};
next unless defined $add_data;
# Insert a blank line before each addition unless there's one already
$zone = substr($zone, 0, -1) if $first && substr($zone, -2) eq "\n\n";
$zone .= "$comment$name\t$add_data";
# Only include the comment once per $name
$comment = '';
$first = 0;
}
}
# Save the modified zone to the zonefile
open my $zonefh, '>', $zonefile or die "$0: failed to open $zonefile for writing: $!\n";
print $zonefh $zone;
close $zonefh;
# vi:set ts=4 sw=4: