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

#
4
# Copyright (c) 2000-2018 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#
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@";
41
my $CONFIG_TARGETSYS         = @CONFIG_TARGETSYS@;
42 43
my ($BOSSTOKEN)              = ("@BOSSNODE@" =~ /^([-\w]+).*$/);
my ($USERTOKEN)              = ("@USERNODE@" =~ /^([-\w]+).*$/);
44
my ($FSTOKEN)                = ("@FSNODE@" =~ /^([-\w]+).*$/);
45 46 47 48 49 50
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@";
51
my $FSNODE_IP                = "@FSNODE_IP@";
52
my $NTPSERVER                = "@NTPSERVER@";
53 54
my $EXTERNAL_BOSSNODE_IP     = "@EXTERNAL_BOSSNODE_IP@";
my $EXTERNAL_USERNODE_IP     = "@EXTERNAL_USERNODE_IP@";
55
my $EXTERNAL_FSNODE_IP       = "@EXTERNAL_FSNODE_IP@";
56 57 58 59 60 61 62 63 64 65 66 67

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@";

68 69
my $MXSTR = "\t\tIN\tMX 10\tops\n";

70 71
# Testbed libraries.
use lib "@prefix@/lib";
72
#use libdb;
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

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 .=
109
                    "${USERTOKEN}\t\tIN\tA\t\t${USERNODE_IP}\n";
110
                $external_strings .=
111
                    "${USERTOKEN}\t\tIN\tA\t\t${EXTERNAL_USERNODE_IP}\n";
112 113 114 115 116
		#
		# If name is not "ops", we need an A record for that as well
		#
		if ($USERTOKEN ne "ops") {
		    $internal_strings .=
117
			"${MXSTR}ops\t\tIN\tA\t\t${USERNODE_IP}\n";
118
		    $external_strings .=
119
			"${MXSTR}ops\t\tIN\tA\t\t${EXTERNAL_USERNODE_IP}\n";
120 121 122 123
		}
		#
		# If name is not "users" we need a CNAME for that
		#
124
		if ($USERTOKEN ne "users") {
125
		    $internal_strings .=
126
			"users\t\tIN\tCNAME\tops\n";
127
		    $external_strings .=
128
			"users\t\tIN\tCNAME\tops\n";
129
		}
130 131 132
	    }
	    elsif ($2 eq "bossnode_ina") {
                $internal_strings .=
133
                    "${BOSSTOKEN}\t\tIN\tA\t\t${BOSSNODE_IP}\n${MXSTR}";
134
                $external_strings .=
135
                    "${BOSSTOKEN}\t\tIN\tA\t\t${EXTERNAL_BOSSNODE_IP}\n${MXSTR}";
136 137 138 139 140
		#
		# If name is not "boss", we need an A record for that as well
		#
		if ($BOSSTOKEN ne "boss") {
		    $internal_strings .=
141
			"boss\t\tIN\tA\t\t${BOSSNODE_IP}\n${MXSTR}";
142
		    $external_strings .=
143
			"boss\t\tIN\tA\t\t${EXTERNAL_BOSSNODE_IP}\n${MXSTR}";
144 145 146
		}
	    }
	    elsif ($2 eq "fsnode_ina") {
147 148 149 150 151 152 153 154 155 156 157 158 159
		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 {
160 161 162 163 164
		    #
		    # If name is not "fs", we need an A record for that
		    #
		    if ($FSTOKEN ne "fs") {
			$internal_strings .=
165
                          "fs\t\tIN\tA\t\t${FSNODE_IP}\n${MXSTR}";
166
			$external_strings .=
167
                          "fs\t\tIN\tA\t\t${EXTERNAL_FSNODE_IP}\n${MXSTR}";
168 169 170 171 172
		    }
		    $internal_strings .=
                          "${FSTOKEN}\t\tIN\tA\t\t${FSNODE_IP}\n";
		    $external_strings .=
                          "${FSTOKEN}\t\tIN\tA\t\t${EXTERNAL_FSNODE_IP}\n";
173 174 175
		}
	    }
	    elsif ($2 eq "fsnode_mx") {
176 177
		if ("$USERNODE_IP" ne "$FSNODE_IP" &&
                    "$BOSSNODE_IP" ne "$FSNODE_IP") {
178 179
		    $internal_strings .= ${MXSTR};
		    $external_strings .= ${MXSTR};
180 181 182
		} else {
		    ;
		}
183 184 185 186 187 188 189 190
	    }
	    elsif ($2 eq "ntp_cname") {
		$internal_strings .=
			"ntp1\t\tIN\tCNAME\t${NTPSERVER}\n".
			"ntp2\t\tIN\tCNAME\t${NTPSERVER}\n";
		$external_strings .=
			"ntp1\t\tIN\tCNAME\t${NTPSERVER}\n".
			"ntp2\t\tIN\tCNAME\t${NTPSERVER}\n";
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	    }
	    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));
	    }
213
	    elsif ($2 eq "secondaries") {
214
		if ($CONFIG_TARGETSYS) {
215 216 217 218 219 220
		    my $foo = "	IN	NS	ns.emulab.net.\n";

		    $internal_strings .= $foo;
		    $external_strings .= $foo;
		}
	    }
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	    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;
}