Merge branch 'master' of ssh://arne_f@ipfire.org/pub/git/ipfire-2.x
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / index.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007 Michael Tremer & Christian Schmidt #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
ac1cfefa
MT
21
22use strict;
23
24# enable only the following on debugging purpose
cb5e9c6c
CS
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
ac1cfefa 27
e383179b 28require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31
32my %cgiparams=();
33my %pppsettings=();
34my %modemsettings=();
35my %netsettings=();
36my %ddnssettings=();
37my $warnmessage = '';
c85ec3b4 38my $refresh = "";
e383179b 39my $ipaddr='';
ac1cfefa
MT
40
41&Header::showhttpheaders();
42
43$cgiparams{'ACTION'} = '';
44&Header::getcgihash(\%cgiparams);
45$pppsettings{'VALID'} = '';
46$pppsettings{'PROFILENAME'} = 'None';
47&General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
48&General::readhash("${General::swroot}/modem/settings", \%modemsettings);
49&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
50&General::readhash("${General::swroot}/ddns/settings", \%ddnssettings);
51
f2fdd0c1
CS
52my %color = ();
53my %mainsettings = ();
54&General::readhash("${General::swroot}/main/settings", \%mainsettings);
55&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
56
ac1cfefa 57my $connstate = &Header::connectionstatus();
56b7a3e2
CS
58
59if ($cgiparams{'ACTION'} eq $Lang::tr{'shutdown'} || $cgiparams{'ACTION'} eq $Lang::tr{'reboot'}) {
60 $refresh = "<meta http-equiv='refresh' content='300;'>";
8a5898e7 61} elsif ($connstate =~ /$Lang::tr{'connecting'}/ || /$Lang::tr{'connection closed'}/ ){
ac1cfefa 62 $refresh = "<meta http-equiv='refresh' content='5;'>";
56b7a3e2
CS
63} elsif ($connstate =~ /$Lang::tr{'dod waiting'}/ || -e "${General::swroot}/main/refreshindex") {
64 $refresh = "<meta http-equiv='refresh' content='30;'>";
ac1cfefa
MT
65}
66
957363eb
CS
67if ($cgiparams{'ACTION'} eq $Lang::tr{'dial profile'})
68{
69 my $profile = $cgiparams{'PROFILE'};
70 my %tempcgiparams = ();
71 $tempcgiparams{'PROFILE'} = '';
72 &General::readhash("${General::swroot}/ppp/settings-$cgiparams{'PROFILE'}",
73 \%tempcgiparams);
74
75 # make a link from the selected profile to the "default" one.
76 unlink("${General::swroot}/ppp/settings");
77 link("${General::swroot}/ppp/settings-$cgiparams{'PROFILE'}",
78 "${General::swroot}/ppp/settings");
79 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
80
81 # read in the new params "early" so we can write secrets.
82 %cgiparams = ();
83 &General::readhash("${General::swroot}/ppp/settings", \%cgiparams);
84 $cgiparams{'PROFILE'} = $profile;
85 $cgiparams{'BACKUPPROFILE'} = $profile;
86 &General::writehash("${General::swroot}/ppp/settings-$cgiparams{'PROFILE'}",
87 \%cgiparams);
88
89 # write secrets file.
90 open(FILE, ">/${General::swroot}/ppp/secrets") or die "Unable to write secrets file.";
91 flock(FILE, 2);
92 my $username = $cgiparams{'USERNAME'};
93 my $password = $cgiparams{'PASSWORD'};
94 print FILE "'$username' * '$password'\n";
95 chmod 0600, "${General::swroot}/ppp/secrets";
96 close FILE;
97
98 &General::log("$Lang::tr{'profile made current'} $tempcgiparams{'PROFILENAME'}");
99 $cgiparams{'ACTION'} = "$Lang::tr{'dial'}";
100}
101
102if ($cgiparams{'ACTION'} eq $Lang::tr{'dial'}) {
a56b5be4 103 system('/usr/local/bin/redctrl start > /dev/null') == 0
957363eb
CS
104 or &General::log("Dial failed: $?"); sleep 1;}
105elsif ($cgiparams{'ACTION'} eq $Lang::tr{'hangup'}) {
a56b5be4 106 system('/usr/local/bin/redctrl stop > /dev/null') == 0
957363eb
CS
107 or &General::log("Hangup failed: $?"); sleep 1;}
108
e383179b
MT
109my $c;
110my $maxprofiles = 5;
111my @profilenames = ();
112
113for ($c = 1; $c <= $maxprofiles; $c++)
114{
115 my %temppppsettings = ();
116 $temppppsettings{'PROFILENAME'} = '';
117 &General::readhash("${General::swroot}/ppp/settings-$c", \%temppppsettings);
118 $profilenames[$c] = $temppppsettings{'PROFILENAME'};
119}
120my %selected;
121for ($c = 1; $c <= $maxprofiles; $c++) {
122 $selected{'PROFILE'}{$c} = '';
123}
124$selected{'PROFILE'}{$pppsettings{'PROFILE'}} = "selected='selected'";
125my $dialButtonDisabled = "disabled='disabled'";
126
127
ac1cfefa
MT
128&Header::openpage($Lang::tr{'main page'}, 1, $refresh);
129&Header::openbigbox('', 'center');
130&Header::openbox('100%', 'center', &Header::cleanhtml(`/bin/uname -n`,"y"));
131
3489ebac
MT
132
133
a08a5f33 134if ( ( $pppsettings{'VALID'} eq 'yes' && $modemsettings{'VALID'} eq 'yes' ) || ( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ && $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ )) {
986e08d9
MT
135 if (open(IPADDR,"${General::swroot}/ddns/ipcache")) {
136 $ipaddr = <IPADDR>;
137 close IPADDR;
138 chomp ($ipaddr);
139 }
140 if (open(IPADDR,"${General::swroot}/red/local-ipaddress")) {
786f2c8a 141 $ipaddr = <IPADDR>;
986e08d9
MT
142 close IPADDR;
143 chomp ($ipaddr);
144 }
ac1cfefa
MT
145} elsif ($modemsettings{'VALID'} eq 'no') {
146 print "$Lang::tr{'modem settings have errors'}\n </b></font>\n";
147} else {
148 print "$Lang::tr{'profile has errors'}\n </b></font>\n";
149}
150
3489ebac
MT
151#if ( $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) {
152# $ipaddr = $netsettings{'RED_ADDRESS'};
153#}
e383179b 154
a56b5be4
CS
155my $death = 0;
156my $rebirth = 0;
157
158if ($cgiparams{'ACTION'} eq $Lang::tr{'shutdown'}) {
159 $death = 1;
160 &General::log($Lang::tr{'shutting down ipfire'});
161 system '/usr/local/bin/ipfirereboot down';
162} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'reboot'}) {
163 $rebirth = 1;
164 &General::log($Lang::tr{'rebooting ipfire'});
165 system '/usr/local/bin/ipfirereboot boot';
166}
167
168if ($death == 0 && $rebirth == 0) {
169
b4ad0027
JPT
170if ($cgiparams{'ACTION'} eq $Lang::tr{'reboot ask'}) {
171 print <<END
172<form method='post' action='$ENV{'SCRIPT_NAME'}'>
173<table width='100%'>
174 <tr>
175 <td colspan="3" align='left'><font color="red">$Lang::tr{'reboot sure'}</font></td>
176 </tr>
177 <tr>
178 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reboot'}' /></td>
179 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'refresh'}' /></td>
c853dcce
JPT
180 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'shutdown ask'}' /></td>
181</tr>
182</table>
183</form>
184END
185;
186} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'shutdown ask'}) {
187 print <<END
188<form method='post' action='$ENV{'SCRIPT_NAME'}'>
189<table width='100%'>
190 <tr>
191 <td colspan="3" align='right'><font color="red">$Lang::tr{'shutdown sure'}</font></td>
192 </tr>
193 <tr>
194 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reboot ask'}' /></td>
195 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'refresh'}' /></td>
b4ad0027
JPT
196 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'shutdown'}' /></td>
197</tr>
198</table>
199</form>
200END
201;
202} else {
a56b5be4
CS
203print <<END
204<form method='post' action='$ENV{'SCRIPT_NAME'}'>
205<table width='100%'>
e383179b 206<tr>
b4ad0027 207 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reboot ask'}' /></td>
a56b5be4 208 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'refresh'}' /></td>
c853dcce 209 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'shutdown ask'}' /></td>
a56b5be4
CS
210</tr>
211</table>
d2414ee7 212</form>
a56b5be4
CS
213END
214;
b4ad0027 215}
a56b5be4 216print <<END;
e383179b
MT
217
218<!-- Table of networks -->
219<table border='0' width=80%>
f2fdd0c1
CS
220 <tr> <th bgcolor='$color{'color20'}'>$Lang::tr{'network'}
221 <th bgcolor='$color{'color20'}'>IP
222 <th bgcolor='$color{'color20'}'>$Lang::tr{'status'}
33e1f48c 223 <tr> <td align='center' bgcolor='$Header::colourred' width='25%'><a href="/cgi-bin/pppsetup.cgi"><font size='2' color='white'><b>$Lang::tr{'internet'}</b></font></a><br>
c85ec3b4
CS
224 <td width='30%' align='center'>$ipaddr
225 <td width='45%' align='center'>$connstate
f3bbb213 226END
c85ec3b4 227if ( $netsettings{'RED_TYPE'} ne "STATIC" && $netsettings{'RED_TYPE'} ne "DHCP" ){
f3bbb213
MT
228print `/usr/local/bin/dialctrl.pl show`;
229print <<END;
e383179b 230 <tr><td colspan='2'>
957363eb 231 <form method='post' action='$ENV{'SCRIPT_NAME'}'>$Lang::tr{'profile'}:
e383179b
MT
232 <select name='PROFILE'>
233END
234 for ($c = 1; $c <= $maxprofiles; $c++)
235 {
236 if ($profilenames[$c] ne '') {
237 $dialButtonDisabled = "";
238 print "\t<option value='$c' $selected{'PROFILE'}{$c}>$c. $profilenames[$c]</option>\n";
239 }
240 }
c6aa4ac1 241 $dialButtonDisabled = "disabled='disabled'" if (-e '/var/run/ppp-ipfire.pid' || -e "${General::swroot}/red/active");
a08a5f33 242 if ( ( $pppsettings{'VALID'} eq 'yes' ) || ( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ && $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) ) {
e383179b
MT
243 print <<END;
244 </select>
245 <input type='submit' name='ACTION' value='$Lang::tr{'dial profile'}' $dialButtonDisabled />
246 </form>
247 <td align='center'>
248 <table width='100%' border='0'>
249 <tr>
957363eb 250 <td width='50%' align='right'> <form method='post' action='$ENV{'SCRIPT_NAME'}'>
e383179b
MT
251 <input type='submit' name='ACTION' value='$Lang::tr{'dial'}'>
252 </form>
957363eb 253 <td width='50%' align='left'> <form method='post' action='$ENV{'SCRIPT_NAME'}'>
e383179b
MT
254 <input type='submit' name='ACTION' value='$Lang::tr{'hangup'}'>
255 </form>
256 </table>
257END
258 } else {
259 print "$Lang::tr{'profile has errors'}\n </b></font>\n";
260 }
c85ec3b4 261}
786f2c8a
MT
262 my $HOSTNAME = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
263 if ( "$HOSTNAME" ne "" ) {
264 print <<END;
c85ec3b4 265 <tr><td><b>Hostname:</b><td align='center'>$HOSTNAME<td>&nbsp;
786f2c8a
MT
266END
267 }
268
269 if ( -e "/var/ipfire/red/remote-ipaddress" ) {
270 my $GATEWAY = `cat /var/ipfire/red/remote-ipaddress`;
271 chomp($GATEWAY);
272 print <<END;
c85ec3b4 273 <tr><td><b>Gateway:</b><td align='center'>$GATEWAY<td>&nbsp;
786f2c8a
MT
274END
275 }
276
c6aa4ac1
MT
277 my $DNS1 = `cat /var/ipfire/red/dns1`;
278 my $DNS2 = `cat /var/ipfire/red/dns2`;
279 chomp($DNS1);
280 chomp($DNS1);
281
282 if ( $DNS1 ) { print <<END;
c85ec3b4 283 <tr><td><b>DNS-Server:</b><td align='center'>$DNS1
c6aa4ac1
MT
284END
285 }
286 if ( $DNS2 ) { print <<END;
c85ec3b4 287 <td align='center'>$DNS2
c6aa4ac1
MT
288END
289 } else { print <<END;
290 <td>&nbsp;
291END
292 }
293
072cd997 294 if ( $netsettings{'GREEN_DEV'} ) { print <<END;
33e1f48c 295 <tr><td align='center' bgcolor='$Header::colourgreen' width='25%'><a href="/cgi-bin/dhcp.cgi"><font size='2' color='white'><b>$Lang::tr{'lan'}</b></font></a>
c85ec3b4
CS
296 <td width='30%' align='center'>$netsettings{'GREEN_ADDRESS'}
297 <td width='45%' align='center'>
e383179b 298END
072cd997
MT
299 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^ENABLE=on` ) {
300 print "Proxy an";
301 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^TRANSPARENT=on` ) { print " (transparent)"; }
302 } else { print "Proxy aus"; }
303 }
304 if ( $netsettings{'BLUE_DEV'} ) { print <<END;
33e1f48c 305 <tr><td align='center' bgcolor='$Header::colourblue' width='25%'><a href="/cgi-bin/wireless.cgi"><font size='2' color='white'><b>$Lang::tr{'wireless'}</b></font></a><br>
c85ec3b4
CS
306 <td width='30%' align='center'>$netsettings{'BLUE_ADDRESS'}
307 <td width='45%' align='center'>
e383179b 308END
072cd997
MT
309 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^ENABLE_BLUE=on` ) {
310 print "Proxy an";
311 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^TRANSPARENT_BLUE=on` ) { print " (transparent)"; }
312 } else { print "Proxy aus"; }
313 }
39a7cc11 314 if ( $netsettings{'ORANGE_DEV'} ) { print <<END;
33e1f48c 315 <tr><td align='center' bgcolor='$Header::colourorange' width='25%'><a href="/cgi-bin/dmzholes.cgi"><font size='2' color='white'><b>$Lang::tr{'dmz'}</b></font></a><br>
c85ec3b4
CS
316 <td width='30%' align='center'>$netsettings{'ORANGE_ADDRESS'}
317 <td width='45%' align='center'><font color=$Header::colourgreen>Online</font>
e383179b 318END
072cd997 319 }
39a7cc11
MT
320 if ( `cat /var/ipfire/vpn/settings | grep ^ENABLED=on` ||
321 `cat /var/ipfire/vpn/settings | grep ^ENABLED_BLUE=on` ) {
072cd997 322 my $ipsecip = `cat /var/ipfire/vpn/settings | grep ^VPN_IP= | cut -c 8-`;
e455cafe
MT
323 my @status = `/usr/sbin/ipsec auto --status`;
324 my %confighash = ();
325 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
072cd997 326 print <<END;
33e1f48c 327 <tr><td align='center' bgcolor='$Header::colourvpn' width='25%'><a href="/cgi-bin/vpnmain.cgi"><font size='2' color='white'><b>$Lang::tr{'vpn'}</b></font></a><br>
c85ec3b4
CS
328 <td width='30%' align='center'>$ipsecip
329 <td width='45%' align='center'><font color=$Header::colourgreen>Online</font>
e383179b 330END
e455cafe
MT
331 my $id = 0;
332 my $gif;
333 foreach my $key (keys %confighash) {
334 if ($confighash{$key}[0] eq 'on') { $gif = 'on.gif'; } else { $gif = 'off.gif'; }
335
336 if ($id % 2) {
9a2a723e 337 print "<tr><td align='left' nowrap='nowrap' bgcolor='$color{'color20'}'>$confighash{$key}[1] / " . $Lang::tr{"$confighash{$key}[3]"} . " (" . $Lang::tr{"$confighash{$key}[4]"} . ")</td><td align='center'>$confighash{$key}[11]</td>";
e455cafe 338 } else {
9a2a723e 339 print "<tr></td><td align='left' nowrap='nowrap' bgcolor='$color{'color22'}'>$confighash{$key}[1] / " . $Lang::tr{"$confighash{$key}[3]"} . " (" . $Lang::tr{"$confighash{$key}[4]"} . ")</td><td align='center'>$confighash{$key}[11]</td>";
e455cafe 340 }
fe6cda92 341
e455cafe
MT
342 my $active = "<table cellpadding='2' cellspacing='0' bgcolor='${Header::colourred}' width='100%'><tr><td align='center'><b><font color='#FFFFFF'>$Lang::tr{'capsclosed'}</font></b></td></tr></table>";
343 if ($confighash{$key}[0] eq 'off') {
344 $active = "<table cellpadding='2' cellspacing='0' bgcolor='${Header::colourblue}' width='100%'><tr><td align='center'><b><font color='#FFFFFF'>$Lang::tr{'capsclosed'}</font></b></td></tr></table>";
345 } else {
346 foreach my $line (@status) {
347 if ($line =~ /\"$confighash{$key}[1]\".*IPsec SA established/) {
f2fdd0c1 348 $active = "<table cellpadding='2' cellspacing='0' bgcolor='${Header::colourgreen}' width='100%'><tr><td align='center'><b><font color='#FFFFFF'>$Lang::tr{'capsopen'}</font></b></td></tr></table>";
e455cafe
MT
349 }
350 }
351 }
352 print "<td align='center'>$active</td>";
353 }
072cd997
MT
354 }
355 if ( `cat /var/ipfire/ovpn/settings | grep ^ENABLED=on` ||
356 `cat /var/ipfire/ovpn/settings | grep ^ENABLED_BLUE=on` ||
357 `cat /var/ipfire/ovpn/settings | grep ^ENABLED_ORANGE=on`) {
358 my $ovpnip = `cat /var/ipfire/ovpn/settings | grep ^DOVPN_SUBNET= | cut -c 14- | sed -e 's\/\\/255.255.255.0\/\/'`;
359 print <<END;
33e1f48c 360 <tr><td align='center' bgcolor='$Header::colourovpn' width='25%'><a href="/cgi-bin/ovpnmain.cgi"><font size='2' color='white'><b>OpenVPN</b></font></a><br>
c85ec3b4
CS
361 <td width='30%' align='center'>$ovpnip
362 <td width='45%' align='center'><font color=$Header::colourgreen>Online</font>
072cd997
MT
363END
364 }
ac1cfefa
MT
365
366# Memory usage warning
367my @free = `/usr/bin/free`;
368$free[1] =~ m/(\d+)/;
369my $mem = $1;
370$free[2] =~ m/(\d+)/;
371my $used = $1;
372my $pct = int 100 * ($mem - $used) / $mem;
373if ($used / $mem > 90) {
374 $warnmessage .= "<li> $Lang::tr{'high memory usage'}: $pct% !</li>\n";
375}
376
377# Diskspace usage warning
378my @temp=();
379my $temp2=();
380my @df = `/bin/df -B M -x rootfs`;
381foreach my $line (@df) {
382 next if $line =~ m/^Filesystem/;
383 if ($line =~ m/root/ ) {
384 $line =~ m/^.* (\d+)M.*$/;
385 @temp = split(/ +/,$line);
386 if ($1<5) {
387 # available:plain value in MB, and not %used as 10% is too much to waste on small disk
388 # and root size should not vary during time
02dc0a76 389 $warnmessage .= "$Lang::tr{'filesystem full'}: $temp[0] <b>$Lang::tr{'free'}=$1M</b> !\n";
ac1cfefa
MT
390 }
391
392 } else {
393 # $line =~ m/^.* (\d+)m.*$/;
394 $line =~ m/^.* (\d+)\%.*$/;
395 if ($1>90) {
396 @temp = split(/ /,$line);
397 $temp2=int(100-$1);
02dc0a76 398 $warnmessage .= "$Lang::tr{'filesystem full'}: $temp[0] <b>$Lang::tr{'free'}=$temp2%</b> !\n";
ac1cfefa
MT
399 }
400 }
401}
402
ac1cfefa 403if ($warnmessage) {
02dc0a76 404 print "<tr><td align='center' bgcolor=$Header::colourred colspan='3'><font color='white'>$warnmessage</font></table>";
ac1cfefa 405}
02dc0a76
MT
406print <<END;
407</table>
f3bbb213 408
02dc0a76 409END
8de160ff
MT
410} else {
411 my $message='';
412 if ($death) {
413 $message = $Lang::tr{'ipfire has now shutdown'};
414 } else {
415 $message = $Lang::tr{'ipfire has now rebooted'};
416 }
417 print <<END
418<div align='center'>
419<table width='100%' bgcolor='#ffffff'>
420<tr><td align='center'>
94ec137d 421<br /><br /><img src='/images/IPFire.png' /><br /><br /><br />
8de160ff
MT
422</td></tr>
423</table>
424<br />
425<font size='6'>$message</font>
426</div>
427END
428;
b4ad0027
JPT
429
430}
431
ac1cfefa 432&Header::closebox();
ac1cfefa 433&Header::closebigbox();
ac1cfefa 434&Header::closepage();