genconfig-iptables.pl 4.1 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2005-2014 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
23 24 25 26 27 28
#
use Getopt::Std;
use English;

my $datafile = "fw-rules";

29
my $optlist = "eMIf:t:";
30 31 32 33
my $domysql = 0;
my $doiptables = 1;
my $expand = 0;
my $qualifiers = 0;
34
my $type = "iptables-vlan";
35 36 37 38 39 40 41 42 43 44
my @lines;

sub usage()
{
    print "Usage: genconfig-iptables [-MI] config ...\n".
	"  -e      expand EMULAB_ variables\n".
	"  -f file specify the input rules file\n".
	"  -q      include qualifiers\n".
	"  -M      generate mysql commands\n".
	"  -I      generate iptables commands\n".
45
	"  -t type defaults to iptables-vlan\n".
46
	"\n".
47 48
	" Valid configs are: open, closed, basic, elabinelab\n".
	" Valid types are: iptables-vlan, iptables-dom0\n";
49 50 51 52 53 54 55
    exit(1);
}

my %fwvars;

sub getfwvars()
{
56
    # XXX for Utah Emulab as of 04/14
57
    $fwvars{EMULAB_GWIP} = "155.98.36.1";
58 59
    $fwvars{EMULAB_VGWIP} = "172.16.0.1";
    # XXX assume vnode GW MAC same as GW MAC
60 61 62
    $fwvars{EMULAB_GWMAC} = "00:d0:bc:f4:14:f8";
    $fwvars{EMULAB_NS} = "155.98.32.70";
    $fwvars{EMULAB_CNET} = "155.98.36.0/22";
63 64 65
    $fwvars{EMULAB_VNET} = "172.16.0.0/12";
    $fwvars{EMULAB_BOSSES} = "boss,subboss,subboss2";
    $fwvars{EMULAB_SERVERS} = "boss,subboss,subboss2,ops";
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    $fwvars{EMULAB_MCADDR} = "234.0.0.0/8";
    $fwvars{EMULAB_MCPORT} = "1025-65535";
}

sub expandfwvars($)
{
    my ($rule) = @_;

    getfwvars() if (!%fwvars);

    if ($rule =~ /EMULAB_\w+/) {
	foreach my $key (keys %fwvars) {
	    $rule =~ s/$key/$fwvars{$key}/g
		if (defined($fwvars{$key}));
	}
	if ($rule =~ /EMULAB_\w+/) {
	    warn("*** WARNING: Unexpanded firewall variable in: \n".
		 "    $rule\n");
	}
    }
    return $rule;
}

89
sub doconfig($$)
90
{
91
    my ($config,$type) = @_;
92
    my $ruleno = 1;
93
    my ($style, $enabled);
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

    if ($doiptables) {
	print "# $config\n";
	print "iptables -F\n";
	print "iptables -X\n";
    }
    if ($domysql) {
	$style = lc($config);
	# XXX
	$style = "emulab" if ($style eq "elabinelab");
	$enabled = 1;

	print "DELETE FROM `default_firewall_rules` WHERE ".
	    "type='$type' AND style='$style';\n";
    }

    foreach my $line (@lines) {
	next if ($line !~ /#.*$config/);
	next if ($line =~ /^#/);
	if ($line =~ /#\s*(\d+):.*/) {
	    $ruleno = $1;
	} else {
	    $ruleno++;
	}
	my $qual;
	if ($line =~ /#.*\+(\w+)/) {
	    $qual = $1;
	}

	($rule = $line) =~ s/\s*#.*//;
	chomp($rule);
	$rule = expandfwvars($rule) if ($expand);
	if ($doiptables) {
	    print "$rule # config=$config";
	    print ", $qual only)"
		if ($qualifiers && $qual);
	    print "\n";
	}
	if ($domysql) {
	    if ($qualifiers) {
		print "INSERT INTO `default_firewall_rules` VALUES (".
		    "'$type','$style',$enabled,$qual,$ruleno,'$rule');\n";
	    } else {
		print "INSERT INTO `default_firewall_rules` VALUES (".
		    "'$type','$style',$enabled,$ruleno,'$rule');\n";
	    }
	}
    }

    print "\n";
}

%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"M"})) {
    $domysql = 1;
    $doiptables = 0;
}
if (defined($options{"I"})) {
    $doiptables = 1;
    $domysql = 0;
}
if (defined($options{"e"})) {
    $expand = 1;
}
if (defined($options{"f"})) {
    $datafile = $options{"f"};
}
164 165 166
if (defined($options{"t"})) {
    $type = $options{"t"};
}
167 168 169 170 171 172 173 174 175 176
if (defined($options{"q"})) {
    $qualifiers = 1;
}

if (@ARGV == 0) {
    usage();
}
@lines = `cat $datafile`;
foreach my $config (@ARGV) {
    $config = uc($config);
177
    doconfig($config,$type);
178 179
}
exit(0);