NetBSD/dist/dhcp/contrib/3.0b1-lease-convert

127 lines
2.7 KiB
Perl
Executable File

#!/usr/bin/perl
#
# Start Date: Mon, 26 Mar 2001 14:24:09 +0200
# Time-stamp: <Monday, 26 March 2001 16:09:44 by brister>
# File: leaseconvertor.pl
# RCSId: $Id: 3.0b1-lease-convert,v 1.2 2005/08/11 17:13:21 drochner Exp $
#
# Description: Convert 3.0b1 to 3.0b2/final lease file format
#
require 5.004;
my $rcsID =<<'EOM';
$Id: 3.0b1-lease-convert,v 1.2 2005/08/11 17:13:21 drochner Exp $
EOM
use strict;
my $revstatement =<<'EOS';
switch (ns-update (delete (1, 12, ddns-rev-name, null))) {
case 0:
unset ddns-rev-name;
break;
}
EOS
my $fwdstatement =<<'EOS';
switch (ns-update (delete (1, 1, ddns-fwd-name, leased-address))) {
case 0:
unset ddns-fwd-name;
break;
}
EOS
if (@ARGV && $ARGV[0] =~ m!^-!) {
usage();
}
# read stdin and write stdout.
while (<>) {
if (! /^lease\s/) {
print;
} else {
my $lease = $_;
while (<>) {
$lease .= $_;
# in a b1 file we should only see a left curly brace on a lease
# lines. Seening it anywhere else means the user is probably
# running a b2 or later file through this.
# Ditto for a 'set' statement.
if (m!\{! || m!^\s*set\s!) {
warn "this doesn't look like a 3.0b1 file. Ignoring rest.\n";
print $lease;
dumpRestAndExit();
}
last if m!^\}\s*$!;
}
# $lease contains all the lines for the lease entry.
$lease = makeNewLease($lease);
print $lease;
}
}
sub usage {
my $prog = $0;
$prog =~ s!.*/!!;
print STDERR <<EOM;
usage: $prog [ file ]
Reads from the lease file listed on the command line (or stdin if not filename
given) and writes to stdout. Converts a 3.0b1-style leases file to a 3.0b2
style (for ad-hoc ddns updates).
EOM
exit (0);
}
# takes a string that's the lines of a lease entry and converts it, if
# necessary to a b2 style lease entry. Returns the new lease in printable form.
sub makeNewLease {
my ($lease) = @_;
my $convertedfwd;
my $convertedrev;
my $newlease = "";
foreach (split "\n", $lease) {
if (m!^(\s+)(ddns-fwd-name|ddns-rev-name)\s+(\"[^\"]+\"\s*;)!) {
$newlease .= $1 . "set " . $2 . " = " . $3 . "\n";
# If there's one of them, then it will always be the -fwd-. There
# may not always be a -rev-.
$convertedfwd++;
$convertedrev++ if ($2 eq "ddns-rev-name");
} elsif (m!^\s*\}!) {
if ($convertedfwd) {
$newlease .= "\ton expiry or release {\n";
$newlease .= $revstatement if $convertedrev;
$newlease .= $fwdstatement;
$newlease .= "\t on expiry or release;\n\t}\n";
}
$newlease .= "}\n";
} else {
$newlease .= $_ . "\n";
}
}
return $newlease;
}
sub dumpRestAndExit {
while (<>) {
print;
}
exit (0);
}