| 1 |
#!/usr/bin/perl |
| 2 |
# SPDX-License-Identifier: 0BSD OR MIT-0 |
| 3 |
|
| 4 |
use warnings; |
| 5 |
use strict; |
| 6 |
|
| 7 |
use File::Temp qw(tempfile); |
| 8 |
use Getopt::Std; |
| 9 |
use POSIX; |
| 10 |
|
| 11 |
sub wail { warn "nsvi: @_\n"; } |
| 12 |
sub fail { die "nsvi: @_\n"; } |
| 13 |
sub fale { die "nsvi: @_: $!\n"; } |
| 14 |
|
| 15 |
sub version { |
| 16 |
while (<DATA>) { |
| 17 |
print if m{^=head1 VERSION} ... m{^=head1 } |
| 18 |
and not m{^=head1 }; |
| 19 |
} |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
sub usage { |
| 24 |
print STDERR <<EOF; |
| 25 |
usage: nsvi [options] <zone> |
| 26 |
Transfer a zone from its server, edit it, then |
| 27 |
upload the edits using `nsdiff` and `nsupdate`. |
| 28 |
nsvi options: |
| 29 |
-h display full documentation |
| 30 |
-V display version information |
| 31 |
-n interactive confirmation |
| 32 |
-v turn on verbose output |
| 33 |
-01cCdD nsdiff options |
| 34 |
-S num|mode SOA serial number or mode |
| 35 |
-s server[#port] where to AXFR and UPDATE the zone |
| 36 |
-g use GSS-TSIG for UPDATE |
| 37 |
-k keyfile AXFR and UPDATE TSIG key |
| 38 |
-y [hmac:]name:key AXFR and UPDATE TSIG key |
| 39 |
EOF |
| 40 |
exit 1; |
| 41 |
} |
| 42 |
my %opt; |
| 43 |
usage unless getopts '-hV01cCdDgk:ns:S:vy:', \%opt; |
| 44 |
version if $opt{V}; |
| 45 |
exec "perldoc -oterm -F $0" if $opt{h}; |
| 46 |
usage if @ARGV != 1; |
| 47 |
my $zone = shift; |
| 48 |
|
| 49 |
$opt{v} = 'qr' if $opt{v}; |
| 50 |
|
| 51 |
my @dig = qw{dig +multiline +onesoa +nocmd +nostats +noadditional}; |
| 52 |
push @dig, map "-$_$opt{$_}", |
| 53 |
grep $opt{$_}, qw{k y}; |
| 54 |
if ($opt{s} and $opt{s} =~ m{^(.*)#(\d+)$}) { |
| 55 |
push @dig, "-p$2", "\@$1"; |
| 56 |
} elsif ($opt{s}) { |
| 57 |
push @dig, "\@$opt{s}"; |
| 58 |
} else { |
| 59 |
push @dig, "\@localhost"; |
| 60 |
} |
| 61 |
|
| 62 |
my @nsdiff = qw{nsdiff}; |
| 63 |
push @nsdiff, map "-$_", |
| 64 |
grep $opt{$_}, qw{0 1 c d D}; |
| 65 |
push @nsdiff, map "-$_$opt{$_}", |
| 66 |
grep $opt{$_}, qw{k s S v y}; |
| 67 |
push @nsdiff, "-slocalhost" unless $opt{s}; |
| 68 |
push @nsdiff, "-u" if $opt{s}; |
| 69 |
|
| 70 |
my @nsupdate = qw{nsupdate}; |
| 71 |
push @nsupdate, map "-$_", |
| 72 |
grep $opt{$_}, qw{g}; |
| 73 |
push @nsupdate, map "-$_$opt{$_}", |
| 74 |
grep $opt{$_}, qw{k y}; |
| 75 |
push @nsupdate, "-l" unless $opt{s}; |
| 76 |
|
| 77 |
my $secRRtypes = qr{NSEC|NSEC3|NSEC3PARAM|RRSIG}; |
| 78 |
$secRRtypes = qr{$secRRtypes|CDS|CDNSKEY} unless $opt{C}; |
| 79 |
$secRRtypes = qr{$secRRtypes|DNSKEY} unless $opt{D}; |
| 80 |
$secRRtypes = qr{$secRRtypes|DS} if $opt{d}; |
| 81 |
|
| 82 |
my $nl = qr{(?:;[^\n]*)?\n}; |
| 83 |
my $rdata = qr{(?:[^()\n]+ |
| 84 |
|(?:[(] |
| 85 |
(?:[^()\n]+|$nl)+ |
| 86 |
[)])+ |
| 87 |
)+$nl}x; |
| 88 |
my $dnssec = qr{(?m)^\S+\s+\d+\s+IN\s+($secRRtypes)\s+$rdata}; |
| 89 |
|
| 90 |
print "@dig axfr $zone" if $opt{v}; |
| 91 |
my $axfr = qx{@dig axfr $zone}; |
| 92 |
fail "failed to @dig axfr $zone" unless $axfr and $? == 0; |
| 93 |
|
| 94 |
$axfr =~ s{$dnssec}{}g; |
| 95 |
|
| 96 |
my ($fh,$fn) = tempfile("$zone.XXXXXXXXXX", |
| 97 |
TMPDIR => 1, UNLINK => 1); |
| 98 |
print $fh $axfr; |
| 99 |
close $fh; |
| 100 |
|
| 101 |
my $vi = $ENV{VISUAL} || $ENV{EDITOR} || "vi"; |
| 102 |
|
| 103 |
sub prompt { |
| 104 |
print shift; |
| 105 |
system "stty -icanon"; |
| 106 |
sysread STDIN, my $key, 1; |
| 107 |
system "stty icanon"; |
| 108 |
print "\n"; |
| 109 |
return $key; |
| 110 |
} |
| 111 |
|
| 112 |
sub retry { |
| 113 |
wail shift; |
| 114 |
my $key = prompt "re-edit and try again? (y/N) "; |
| 115 |
next RETRY if $key =~ m{[Yy]}; |
| 116 |
exit 1; |
| 117 |
} |
| 118 |
|
| 119 |
RETRY: for (;;) { |
| 120 |
system "$vi $fn"; |
| 121 |
fail "failed to $vi $fn" unless $? == 0; |
| 122 |
|
| 123 |
print "@nsdiff $zone $fn" if $opt{v}; |
| 124 |
my $diff = qx{@nsdiff $zone $fn}; |
| 125 |
if ($? == 0) { |
| 126 |
wail "no change"; |
| 127 |
exit 0; |
| 128 |
} |
| 129 |
retry "failed to @nsdiff $zone $fn" |
| 130 |
unless $diff and $? == 256; |
| 131 |
if ($opt{n}) { |
| 132 |
print "$diff\n"; |
| 133 |
my $key = prompt "make update, edit again, or quit? (u/e/Q) "; |
| 134 |
next RETRY if $key =~ m{[EeRr]}; |
| 135 |
exit 1 unless $key =~ m{[UuYy]}; |
| 136 |
} |
| 137 |
open my $ph, '|-', @nsupdate |
| 138 |
or retry "pipe to @nsupdate: $!"; |
| 139 |
print $ph $diff; |
| 140 |
last if close $ph; |
| 141 |
retry "pipe to @nsupdate: $!" if $!; |
| 142 |
retry "failed to @nsupdate"; |
| 143 |
} |
| 144 |
|
| 145 |
print "done\n" if $opt{v}; |
| 146 |
exit 0; |
| 147 |
|
| 148 |
__END__ |
| 149 |
|
| 150 |
=encoding utf8 |
| 151 |
|
| 152 |
=head1 NAME |
| 153 |
|
| 154 |
nsvi - transfer a zone, edit it, then upload the edits |
| 155 |
|
| 156 |
=head1 SYNOPSIS |
| 157 |
|
| 158 |
nsvi [B<-01cCdDghvV>] [B<-k> I<keyfile>] [B<-y> [I<hmac>:]I<name>:I<key>] |
| 159 |
[B<-S> I<mode>|I<num>] [B<-s> I<server>] <I<zone>> |
| 160 |
|
| 161 |
=head1 DESCRIPTION |
| 162 |
|
| 163 |
The B<nsvi> program makes an AXFR request for the zone, runs your |
| 164 |
editor so you can make whatever changes you require, then it runs |
| 165 |
B<nsdiff> | B<nsupdate> to push those changes to the server. |
| 166 |
|
| 167 |
Automatically-maintained DNSSEC records are stripped from the zone |
| 168 |
before it is passed to your editor, and you do not need to manually |
| 169 |
adjust the SOA serial number. |
| 170 |
|
| 171 |
=head1 OPTIONS |
| 172 |
|
| 173 |
Most B<nsvi> options are passed to B<nsdiff> and some to B<nsupdate>. |
| 174 |
|
| 175 |
=over |
| 176 |
|
| 177 |
=item B<-h> |
| 178 |
|
| 179 |
Display this documentation. |
| 180 |
|
| 181 |
=item B<-V> |
| 182 |
|
| 183 |
Display version information. |
| 184 |
|
| 185 |
=item B<-v> |
| 186 |
|
| 187 |
Verbose mode. |
| 188 |
|
| 189 |
=item B<-n> |
| 190 |
|
| 191 |
Interactive confirmation. |
| 192 |
|
| 193 |
When you quit the editor, you will be shown the changes, then asked |
| 194 |
whether to make the update (press B<U> or B<Y>), edit again (press |
| 195 |
B<E> or B<R>), or quit (press another key). |
| 196 |
|
| 197 |
=item B<-01cCdD> |
| 198 |
|
| 199 |
=item B<-S> B<mode>|I<num> |
| 200 |
|
| 201 |
These options are passed to B<nsdiff>. |
| 202 |
For details see the nsdiff manual. |
| 203 |
|
| 204 |
=item B<-s> I<server>[#I<port>] |
| 205 |
|
| 206 |
Transfer the zone from the server given in this option, and send the |
| 207 |
update request to the same place. You can specify the server host name |
| 208 |
or IP address, optionally followed by a "#" and the port number. |
| 209 |
|
| 210 |
If you do not use the B<-s> option, the zone will be transferred |
| 211 |
from I<localhost>, and B<nsvi> will use B<nsupdate> B<-l> to update |
| 212 |
the zone. |
| 213 |
|
| 214 |
=item B<-g> |
| 215 |
|
| 216 |
Passed to B<nsupdate> to use GSS-TSIG for UPDATE. |
| 217 |
|
| 218 |
=item B<-k> I<keyfile> |
| 219 |
|
| 220 |
TSIG key file, passed to B<dig>, B<nsdiff>, and B<nsupdate>. |
| 221 |
|
| 222 |
=item B<-y> [I<hmac>:]I<name>:I<key> |
| 223 |
|
| 224 |
Literal TSIG key, passed to B<dig>, B<nsdiff>, and B<nsupdate>. |
| 225 |
|
| 226 |
=back |
| 227 |
|
| 228 |
=head1 ENVIRONMENT |
| 229 |
|
| 230 |
=over |
| 231 |
|
| 232 |
=item B<TMPDIR> |
| 233 |
|
| 234 |
Location for temporary files. |
| 235 |
|
| 236 |
=item B<VISUAL> |
| 237 |
|
| 238 |
=item B<EDITOR> |
| 239 |
|
| 240 |
Which editor to use. C<$VISUAL> is used if it is set, |
| 241 |
otherwise C<$EDITOR>, otherwise B<vi>. |
| 242 |
|
| 243 |
=back |
| 244 |
|
| 245 |
=head1 VERSION |
| 246 |
|
| 247 |
This is nsvi-1.85 <https://dotat.at/prog/nsdiff/> |
| 248 |
|
| 249 |
Written by Tony Finch <[email protected]> <[email protected]> |
| 250 |
at Cambridge University Information Services. |
| 251 |
You may do anything with this. It has no warranty. |
| 252 |
|
| 253 |
=head1 ACKNOWLEDGMENTS |
| 254 |
|
| 255 |
Thanks to Tristan Le Guern for the B<-n> option and Mantas Mikulėnas |
| 256 |
for the B<-g> option. Thanks to David McBride and Petr Menšík for |
| 257 |
providing useful feedback. |
| 258 |
|
| 259 |
=head1 SEE ALSO |
| 260 |
|
| 261 |
nsdiff(1), nsupdate(1), dig(1). |
| 262 |
|
| 263 |
=cut |