rc.firewall 6.16 KB
Newer Older
1
2
3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2004, 2005 University of Utah and the Flux Group.
5
6
7
8
9
# All rights reserved.
#
use English;
use Getopt::Std;

10
11
12
13
14
#
# For firewall rule logging: log accepted or rejected packets.
# XXX debugging
#
my $logaccept = 0;
Mike Hibler's avatar
Mike Hibler committed
15
my $logreject = 0;
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
#
# Hosts we need un-firewalled static routes for
#
my $elvinip = "224.4.0.1";
my $viproutes = "boss ops fs $elvinip";

my $action = "boot";
my $debug = 0;
my $fwinfo;
my @fwrules;

sub usage()
{
    print "Usage: " .
	scriptname() . "boot|shutdown|reconfig|reset\n";
    exit(1);
}

# Turn off line buffering on output
$| = 1;

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

#
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use libsetup;
use liblocsetup;
use libtmcc;
use librc;

#
# Must be root.
# 
if ($UID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!\n");
}

#
# Only local cluster nodes right now
#
exit(0)
    if (MFS() || REMOTE() || SIMHOST() || JAILHOST());

# Protos.
sub doboot();
sub doshutdown();
sub doreconfig();
sub docleanup();
sub firewaller();
sub firewallee();

# XXX for testing
#configtmcc("portnum", 7778);

#
# XXX for debugging default rules: make sure we always fetch
# from boss so we can pick up changes without rebooting using
# "/usr/local/etc/emulab/rc/rc.firewall reconfig"
#
configtmcc("nocache", 1);

# Allow default above.
if (@ARGV) {
    $action = $ARGV[0];
}

my $TMFWC = CONFDIR() . "/rc.fw";

# Execute the action.
SWITCH: for ($action) {
    /^boot$/i && do {
	doboot();
	last SWITCH;
    };
    /^shutdown$/i && do {
	doshutdown();
	last SWITCH;
    };
    /^reconfig$/i && do {
	doreconfig();
	last SWITCH;
    };
    /^reset$/i && do {
	docleanup();
	last SWITCH;
    };
    fatal("Invalid action: $action\n");
}
exit(0);

sub doboot()
{
    #
    # Get our firewall info
    #
    if (getfwconfig(\$fwinfo, \@fwrules) != 0) {
	exit(0);
    }

120
121
122
123
124
125
126
127
128
    #
    # Common case: no firewall, bug out
    #
    if ($fwinfo->{TYPE} eq "none") {
	print "No firewall\n"
	    if ($debug);
	exit(0);
    }

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    print "Setting up firewall\n";

    #
    # Create the rc.firewall script
    #
    open(FWC, ">$TMFWC") or
	fatal("Could not open $TMFWC: $!\n");
    print FWC "#!/bin/sh\n";
    print FWC "# auto-generated by $BINDIR/rc/rc.firewall, DO NOT EDIT\n";
    print FWC "if [ x\$1 = x ]; then action=enable; else action=\$1; fi\n";

    #
    # If TYPE is not "remote", we ourselves are a firewall
    #
    my $rc = 0;
    SWITCH: for ($fwinfo->{TYPE}) {
	/^ipfw$/i && do {
	    $rc = firewaller();
	    last SWITCH;
	};
149
150
151
152
	/^ipfw2-vlan$/i && do {
	    $rc = firewaller();
	    last SWITCH;
	};
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
	/^remote$/i && do {
	    last SWITCH;
	};
	fatal("Invalid firewall type: $fwinfo->{TYPE}\n");
    }

    #
    # If FWIP is set, we are the "client" of another firewall
    #
    if ($rc == 0 && defined($fwinfo->{FWIP})) {
	$rc = firewallee();
    }

    close(FWC);
    if ($rc) {
	unlink($TMFWC);
	exit($rc);
    }

    #
    # and execute it!
    #
    chmod(0755, $TMFWC);
    system("$TMFWC enable");
    if ($?) {
	fatal("Error running $TMFWC");
    }
}

sub firewaller()
{
184
185
186
187
    # XXX debugging
    $fwinfo->{LOGACCEPT} = $logaccept;
    $fwinfo->{LOGREJECT} = $logreject;

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    my ($upline, $downline) = os_fwconfig_line($fwinfo, @fwrules);

    print FWC "case \"\$action\" in\n";
    print FWC "  enable)\n";
    print FWC "    $upline\n";
    print FWC "    ;;\n";
    print FWC "  disable)\n";
    print FWC "    $downline\n";
    print FWC "    ;;\n";
    print FWC "  *)\n";
    print FWC "    echo \"Usage: rc.firewall {enable|disable}\"\n";
    print FWC "    exit 1\n";
    print FWC "    ;;\n";
    print FWC "esac\n";
    print FWC "exit 0\n";
    return 0;
}

#
# Configuration for a firewalled node.
#
209
# For a "transparent" firewall, there is nothing to do.
210
211
212
213
214
215
216
217
#
# However, in our "all volunteer" firewall model, we must replace our
# default route with one leading to the firewall node.  Before doing that,
# we ensure that we have static routes for boss, ops, fs, elvind, and the
# firewall node.
#
sub firewallee()
{
218
219
220
221
222
223
224
225
    #
    # XXX a firewall IP of 0 implies a transparent firewall
    #
    if ($fwinfo->{FWIP} eq "0.0.0.0") {
	print FWC "true\n";
	return 0;
    }

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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    my $orouter = `cat $BOOTDIR/routerip`;
    chomp($orouter);

    #
    # Assume the firewall itself is on a local interface with an already
    # existing interface route.  Thus we don't need to force a route.
    #
    #$viproutes .= " $fwinfo->{FWIP}";

    my ($upline, $downline) =
	os_fwrouteconfig_line($orouter, $fwinfo->{FWIP}, $viproutes);
    my $addroute =
	os_routing_add_manual("default", undef, undef, "\$nrouter", undef);
    my $delroute =
	os_routing_del_manual("default", undef, undef, undef, undef);

    print FWC "case \"\$action\" in\n";
    print FWC "  enable)\n";
    print FWC "    $upline\n";
    print FWC "    cp -p $BOOTDIR/routerip $BOOTDIR/orouterip\n";
    print FWC "    nrouter=$fwinfo->{FWIP}\n";
    print FWC "    ;;\n";
    print FWC "  disable)\n";
    print FWC "    nrouter=\`cat $BOOTDIR/orouterip\`\n";
    print FWC "    ;;\n";
    print FWC "  *)\n";
    print FWC "    echo \"Usage: rc.firewall {enable|disable}\"\n";
    print FWC "    exit 1\n";
    print FWC "    ;;\n";
    print FWC "esac\n";
    print FWC "$delroute >/dev/null 2>&1\n";
    print FWC "$addroute || {\n";
    print FWC "    echo \"Could not establish new default route\"\n";
    print FWC "    exit 1\n";
    print FWC "}\n";
    print FWC "echo \$nrouter >$BOOTDIR/routerip\n";
    print FWC "case \"\$action\" in\n";
    print FWC "  enable)\n";
    print FWC "    ;;\n";
    print FWC "  disable)\n";
    print FWC "    $downline\n";
    print FWC "    ;;\n";
    print FWC "esac\n";
    print FWC "exit 0\n";
    return 0;
}

#
# Shutdown Action.
#
sub doshutdown()
{
    # Bring all interfaces down.
    if (-e $TMFWC) {
	system("$TMFWC disable");
	if ($?) {
	    fatal("Error running $TMFWC");
	}
    }
}

#
# Node Reconfig Action (without rebooting).
#
sub doreconfig()
{
    doshutdown();
    return doboot();
}

#
# Node cleanup action (node is reset to completely clean state).
#
sub docleanup()
{
    unlink $TMFWC;
}