named_makeforw.in 6.6 KB
Newer Older
1 2 3 4
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2011 University of Utah and the Flux Group.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# All rights reserved.
#
use Getopt::Std;
use Socket;

#
# named_makeconf - helper script to create named.conf file. The template
# looks like standard file, only with %token% strings that need to be
# replaced. See below.
#
sub usage {
	print "Usage: $0 <templatefile>\n";
}

#
# Configure variables
#
my $OURDOMAIN                = "@OURDOMAIN@";
my ($BOSSTOKEN)              = ("@BOSSNODE@" =~ /^([-\w]+).*$/);
my ($USERTOKEN)              = ("@USERNODE@" =~ /^([-\w]+).*$/);
26
my ($FSTOKEN)                = ("@FSNODE@" =~ /^([-\w]+).*$/);
27 28 29 30 31 32
my $TESTBED_NETWORK          = "@TESTBED_NETWORK@";
my $TESTBED_NETMASK          = "@TESTBED_NETMASK@";
my $EXTERNAL_TESTBED_NETWORK = "@EXTERNAL_TESTBED_NETWORK@";
my $EXTERNAL_TESTBED_NETMASK = "@EXTERNAL_TESTBED_NETMASK@";
my $BOSSNODE_IP              = "@BOSSNODE_IP@";
my $USERNODE_IP              = "@USERNODE_IP@";
33
my $FSNODE_IP                = "@FSNODE_IP@";
34 35
my $EXTERNAL_BOSSNODE_IP     = "@EXTERNAL_BOSSNODE_IP@";
my $EXTERNAL_USERNODE_IP     = "@EXTERNAL_USERNODE_IP@";
36
my $EXTERNAL_FSNODE_IP       = "@EXTERNAL_FSNODE_IP@";
37 38 39 40 41 42 43 44 45 46 47 48

my $CONTROL_ROUTER_IP  = "@CONTROL_ROUTER_IP@";
my $CONTROL_NETWORK    = "@CONTROL_NETWORK@";
my $CONTROL_NETMASK    = "@CONTROL_NETMASK@";
my $PRIVATE_NETWORK    = "@PRIVATE_NETWORK@";
my $PRIVATE_ROUTER     = "@PRIVATE_ROUTER@";
my $PRIVATE_NETMASK    = "@PRIVATE_NETMASK@";
my $PUBLIC_NETWORK     = "@PUBLIC_NETWORK@";
my $PUBLIC_ROUTER      = "@PUBLIC_ROUTER@";
my $PUBLIC_NETMASK     = "@PUBLIC_NETMASK@";
my $NAMED_FORWARDERS   = "@NAMED_FORWARDERS@";

49 50 51
my $MXSTR = "\t\tIN\tMX 10\tops\n";


52 53
# Testbed libraries.
use lib "@prefix@/lib";
54
#use libdb;
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

sub isroutable($);

# This is for the dotted and reverse dotted notation needed for the
# reverse maps. This is probably all wrong ... 
my ($ia, $ib, $ic, $id) =
    ($TESTBED_NETWORK =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/);
my ($ea, $eb, $ec, $ed) =
    ($EXTERNAL_TESTBED_NETWORK =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/);

my $internal_forwfile = "${ia}.${ib}.${ic}.db.head";
my $external_forwfile = "${ea}.${eb}.${ec}.db.head";
my $internal_strings  = "";
my $external_strings  = "";

usage()
    if (@ARGV != 2);

my $which  = $ARGV[0];
my $infile = $ARGV[1];

open(IF,"<$infile") or
    die("Unable to open $infile for reading\n");

while (<IF>) {
	if (/^(.*)\%([-\w]*)\%(.*)$/) {
	    if ($2 eq "bossnode_ip") {
		$internal_strings .= $1 . "$BOSSNODE_IP" . $3 . "\n";
		$external_strings .= $1 . "$EXTERNAL_BOSSNODE_IP" . $3 . "\n";
	    }
	    elsif ($2 eq "usernode_ip") {
		$internal_strings .= $1 . "$USERNODE_IP" . $3 . "\n";
		$external_strings .= $1 . "$EXTERNAL_USERNODE_IP" . $3 . "\n";
	    }
	    elsif ($2 eq "usernode_ina") {
                $internal_strings .=
91
                    "${USERTOKEN}\t\tIN\tA\t\t${USERNODE_IP}\n";
92
                $external_strings .=
93
                    "${USERTOKEN}\t\tIN\tA\t\t${EXTERNAL_USERNODE_IP}\n";
94 95 96 97 98
		#
		# If name is not "ops", we need an A record for that as well
		#
		if ($USERTOKEN ne "ops") {
		    $internal_strings .=
99
			"${MXSTR}ops\t\tIN\tA\t\t${USERNODE_IP}\n";
100
		    $external_strings .=
101
			"${MXSTR}ops\t\tIN\tA\t\t${EXTERNAL_USERNODE_IP}\n";
102 103 104 105
		}
		#
		# If name is not "users" we need a CNAME for that
		#
106
		if ($USERTOKEN ne "users") {
107
		    $internal_strings .=
108
			"users\t\tIN\tCNAME\tops\n";
109
		    $external_strings .=
110
			"users\t\tIN\tCNAME\tops\n";
111
		}
112 113 114
	    }
	    elsif ($2 eq "bossnode_ina") {
                $internal_strings .=
115
                    "${BOSSTOKEN}\t\tIN\tA\t\t${BOSSNODE_IP}\n${MXSTR}";
116
                $external_strings .=
117
                    "${BOSSTOKEN}\t\tIN\tA\t\t${EXTERNAL_BOSSNODE_IP}\n${MXSTR}";
118 119 120 121 122
		#
		# If name is not "boss", we need an A record for that as well
		#
		if ($BOSSTOKEN ne "boss") {
		    $internal_strings .=
123
			"boss\t\tIN\tA\t\t${BOSSNODE_IP}\n${MXSTR}";
124
		    $external_strings .=
125
			"boss\t\tIN\tA\t\t${EXTERNAL_BOSSNODE_IP}\n${MXSTR}";
126 127 128
		}
	    }
	    elsif ($2 eq "fsnode_ina") {
129 130 131 132 133 134 135 136 137 138 139 140 141
		if ($USERNODE_IP eq $FSNODE_IP) {
		    $internal_strings .=
                          "fs\t\tIN\tCNAME\tops\n";
		    $external_strings .=
                          "fs\t\tIN\tCNAME\tops\n";
		}
		elsif ($BOSSNODE_IP eq $FSNODE_IP) {
		    $internal_strings .=
                          "fs\t\tIN\tCNAME\tboss\n";
		    $external_strings .=
                          "fs\t\tIN\tCNAME\tboss\n";
		}
		else {
142 143 144 145 146
		    #
		    # If name is not "fs", we need an A record for that
		    #
		    if ($FSTOKEN ne "fs") {
			$internal_strings .=
147
                          "fs\t\tIN\tA\t\t${FSNODE_IP}\n${MXSTR}";
148
			$external_strings .=
149
                          "fs\t\tIN\tA\t\t${EXTERNAL_FSNODE_IP}\n${MXSTR}";
150 151 152 153 154
		    }
		    $internal_strings .=
                          "${FSTOKEN}\t\tIN\tA\t\t${FSNODE_IP}\n";
		    $external_strings .=
                          "${FSTOKEN}\t\tIN\tA\t\t${EXTERNAL_FSNODE_IP}\n";
155 156 157
		}
	    }
	    elsif ($2 eq "fsnode_mx") {
158 159
		if ("$USERNODE_IP" ne "$FSNODE_IP" &&
                    "$BOSSNODE_IP" ne "$FSNODE_IP") {
160 161
		    $internal_strings .= ${MXSTR};
		    $external_strings .= ${MXSTR};
162 163 164 165
		} else {
		    ;
		}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	    }
	    elsif ($2 eq "private-router") {
                my $foo = "private-router\tIN\tA\t${PRIVATE_ROUTER}\n";

		$internal_strings .= $foo;
		$external_strings .= $foo
                    if (isroutable($PRIVATE_ROUTER));
	    }
	    elsif ($2 eq "public-router") {
                my $foo = "public-router\tIN\tA\t${PUBLIC_ROUTER}\n";

		$internal_strings .= $foo;
		$external_strings .= $foo
                    if (isroutable($PUBLIC_ROUTER));
	    }
	    elsif ($2 eq "control-router") {
                my $foo = "control-router\tIN\tA\t${CONTROL_ROUTER_IP}\n";

		$internal_strings .= $foo;
		$external_strings .= $foo
                    if (isroutable($CONTROL_ROUTER_IP));
	    }
	    else {
		die("*** $0:\n".
		    "    Unknown special tag in named template file!\n");
	    }
	}
	else {
	    # A regular line
	    $internal_strings .= $_;
	    $external_strings .= $_;
	}
}
close(IF);

if ($which eq "-internal") {
    print $internal_strings;
}
else {
    print $external_strings;
}

exit(0);

#
# Is an IP routable?
#
sub isroutable($)
{
    my ($IP)  = @_;
    my ($a,$b,$c,$d) = ($IP =~ /^(\d*)\.(\d*)\.(\d*)\.(\d*)/);

    #
    # These are unroutable:
    # 10.0.0.0        -   10.255.255.255  (10/8 prefix)
    # 172.16.0.0      -   172.31.255.255  (172.16/12 prefix)
    # 192.168.0.0     -   192.168.255.255 (192.168/16 prefix)
    #

    # Easy tests.
    return 0
	if (($a eq "10") ||
	    ($a eq "192" && $b eq "168"));

    # Lastly
    return 0
	if (inet_ntoa((inet_aton($IP) & inet_aton("255.240.0.0"))) eq
	    "172.16.0.0");

    return 1;
}