aboutsummaryrefslogtreecommitdiff
path: root/contrib/3.0b1-lease-convert
blob: 6e8157f0f396b06168230fdc95e28341e8e245fc (plain)
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
#!/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.1 2001-04-18 19:17:34 mellon 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.1 2001-04-18 19:17:34 mellon 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);
}