Feature wish of a user
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / index.cgi
1 #!/usr/bin/perl
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 ###############################################################################
21
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31
32 my %cgiparams=();
33 my %pppsettings=();
34 my %modemsettings=();
35 my %netsettings=();
36 my %ddnssettings=();
37 my $warnmessage = '';
38 my $refresh = "";
39 my $ipaddr='';
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
52 my %color = ();
53 my %mainsettings = ();
54 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
55 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
56
57 my $connstate = &Header::connectionstatus();
58
59 if ($cgiparams{'ACTION'} eq $Lang::tr{'shutdown'} || $cgiparams{'ACTION'} eq $Lang::tr{'reboot'}) {
60 $refresh = "<meta http-equiv='refresh' content='300;'>";
61 } elsif ($connstate =~ /$Lang::tr{'connecting'}/ || /$Lang::tr{'connection closed'}/ ){
62 $refresh = "<meta http-equiv='refresh' content='5;'>";
63 } elsif ($connstate =~ /$Lang::tr{'dod waiting'}/ || -e "${General::swroot}/main/refreshindex") {
64 $refresh = "<meta http-equiv='refresh' content='30;'>";
65 }
66
67 if ($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
102 if ($cgiparams{'ACTION'} eq $Lang::tr{'dial'}) {
103 system('/usr/local/bin/redctrl start > /dev/null') == 0
104 or &General::log("Dial failed: $?"); sleep 1;}
105 elsif ($cgiparams{'ACTION'} eq $Lang::tr{'hangup'}) {
106 system('/usr/local/bin/redctrl stop > /dev/null') == 0
107 or &General::log("Hangup failed: $?"); sleep 1;}
108
109 my $c;
110 my $maxprofiles = 5;
111 my @profilenames = ();
112
113 for ($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 }
120 my %selected;
121 for ($c = 1; $c <= $maxprofiles; $c++) {
122 $selected{'PROFILE'}{$c} = '';
123 }
124 $selected{'PROFILE'}{$pppsettings{'PROFILE'}} = "selected='selected'";
125 my $dialButtonDisabled = "disabled='disabled'";
126
127
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
132
133
134 if ( ( $pppsettings{'VALID'} eq 'yes' && $modemsettings{'VALID'} eq 'yes' ) || ( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ && $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ )) {
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")) {
141 $ipaddr = <IPADDR>;
142 close IPADDR;
143 chomp ($ipaddr);
144 }
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
151 #if ( $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) {
152 # $ipaddr = $netsettings{'RED_ADDRESS'};
153 #}
154
155 my $death = 0;
156 my $rebirth = 0;
157
158 if ($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
168 if ($death == 0 && $rebirth == 0) {
169
170
171
172 if ($cgiparams{'ACTION'} eq $Lang::tr{'reboot ask'}) {
173 print <<END
174 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
175 <table width='100%'>
176 <tr>
177 <td colspan="3" align='left'><font color="red">$Lang::tr{'reboot sure'}</font></td>
178 </tr>
179 <tr>
180 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reboot'}' /></td>
181 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'refresh'}' /></td>
182 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'shutdown'}' /></td>
183 </tr>
184 </table>
185 </form>
186 END
187 ;
188 } else {
189 print <<END
190 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
191 <table width='100%'>
192 <tr>
193 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reboot ask'}' /></td>
194 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'refresh'}' /></td>
195 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'shutdown'}' /></td>
196 </tr>
197 </table>
198 </form>
199 END
200 ;
201 }
202 print <<END;
203
204 <!-- Table of networks -->
205 <table border='0' width=80%>
206 <tr> <th bgcolor='$color{'color20'}'>$Lang::tr{'network'}
207 <th bgcolor='$color{'color20'}'>IP
208 <th bgcolor='$color{'color20'}'>$Lang::tr{'status'}
209 <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>
210 <td width='30%' align='center'>$ipaddr
211 <td width='45%' align='center'>$connstate
212 END
213 if ( $netsettings{'RED_TYPE'} ne "STATIC" && $netsettings{'RED_TYPE'} ne "DHCP" ){
214 print `/usr/local/bin/dialctrl.pl show`;
215 print <<END;
216 <tr><td colspan='2'>
217 <form method='post' action='$ENV{'SCRIPT_NAME'}'>$Lang::tr{'profile'}:
218 <select name='PROFILE'>
219 END
220 for ($c = 1; $c <= $maxprofiles; $c++)
221 {
222 if ($profilenames[$c] ne '') {
223 $dialButtonDisabled = "";
224 print "\t<option value='$c' $selected{'PROFILE'}{$c}>$c. $profilenames[$c]</option>\n";
225 }
226 }
227 $dialButtonDisabled = "disabled='disabled'" if (-e '/var/run/ppp-ipfire.pid' || -e "${General::swroot}/red/active");
228 if ( ( $pppsettings{'VALID'} eq 'yes' ) || ( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ && $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) ) {
229 print <<END;
230 </select>
231 <input type='submit' name='ACTION' value='$Lang::tr{'dial profile'}' $dialButtonDisabled />
232 </form>
233 <td align='center'>
234 <table width='100%' border='0'>
235 <tr>
236 <td width='50%' align='right'> <form method='post' action='$ENV{'SCRIPT_NAME'}'>
237 <input type='submit' name='ACTION' value='$Lang::tr{'dial'}'>
238 </form>
239 <td width='50%' align='left'> <form method='post' action='$ENV{'SCRIPT_NAME'}'>
240 <input type='submit' name='ACTION' value='$Lang::tr{'hangup'}'>
241 </form>
242 </table>
243 END
244 } else {
245 print "$Lang::tr{'profile has errors'}\n </b></font>\n";
246 }
247 }
248 my $HOSTNAME = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
249 if ( "$HOSTNAME" ne "" ) {
250 print <<END;
251 <tr><td><b>Hostname:</b><td align='center'>$HOSTNAME<td>&nbsp;
252 END
253 }
254
255 if ( -e "/var/ipfire/red/remote-ipaddress" ) {
256 my $GATEWAY = `cat /var/ipfire/red/remote-ipaddress`;
257 chomp($GATEWAY);
258 print <<END;
259 <tr><td><b>Gateway:</b><td align='center'>$GATEWAY<td>&nbsp;
260 END
261 }
262
263 my $DNS1 = `cat /var/ipfire/red/dns1`;
264 my $DNS2 = `cat /var/ipfire/red/dns2`;
265 chomp($DNS1);
266 chomp($DNS1);
267
268 if ( $DNS1 ) { print <<END;
269 <tr><td><b>DNS-Server:</b><td align='center'>$DNS1
270 END
271 }
272 if ( $DNS2 ) { print <<END;
273 <td align='center'>$DNS2
274 END
275 } else { print <<END;
276 <td>&nbsp;
277 END
278 }
279
280 if ( $netsettings{'GREEN_DEV'} ) { print <<END;
281 <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>
282 <td width='30%' align='center'>$netsettings{'GREEN_ADDRESS'}
283 <td width='45%' align='center'>
284 END
285 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^ENABLE=on` ) {
286 print "Proxy an";
287 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^TRANSPARENT=on` ) { print " (transparent)"; }
288 } else { print "Proxy aus"; }
289 }
290 if ( $netsettings{'BLUE_DEV'} ) { print <<END;
291 <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>
292 <td width='30%' align='center'>$netsettings{'BLUE_ADDRESS'}
293 <td width='45%' align='center'>
294 END
295 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^ENABLE_BLUE=on` ) {
296 print "Proxy an";
297 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^TRANSPARENT_BLUE=on` ) { print " (transparent)"; }
298 } else { print "Proxy aus"; }
299 }
300 if ( $netsettings{'ORANGE_DEV'} ) { print <<END;
301 <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>
302 <td width='30%' align='center'>$netsettings{'ORANGE_ADDRESS'}
303 <td width='45%' align='center'><font color=$Header::colourgreen>Online</font>
304 END
305 }
306 if ( `cat /var/ipfire/vpn/settings | grep ^ENABLED=on` ||
307 `cat /var/ipfire/vpn/settings | grep ^ENABLED_BLUE=on` ) {
308 my $ipsecip = `cat /var/ipfire/vpn/settings | grep ^VPN_IP= | cut -c 8-`;
309 my @status = `/usr/sbin/ipsec auto --status`;
310 my %confighash = ();
311 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
312 print <<END;
313 <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>
314 <td width='30%' align='center'>$ipsecip
315 <td width='45%' align='center'><font color=$Header::colourgreen>Online</font>
316 END
317 my $id = 0;
318 my $gif;
319 foreach my $key (keys %confighash) {
320 if ($confighash{$key}[0] eq 'on') { $gif = 'on.gif'; } else { $gif = 'off.gif'; }
321
322 if ($id % 2) {
323 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>";
324 } else {
325 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>";
326 }
327
328 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>";
329 if ($confighash{$key}[0] eq 'off') {
330 $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>";
331 } else {
332 foreach my $line (@status) {
333 if ($line =~ /\"$confighash{$key}[1]\".*IPsec SA established/) {
334 $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>";
335 }
336 }
337 }
338 print "<td align='center'>$active</td>";
339 }
340 }
341 if ( `cat /var/ipfire/ovpn/settings | grep ^ENABLED=on` ||
342 `cat /var/ipfire/ovpn/settings | grep ^ENABLED_BLUE=on` ||
343 `cat /var/ipfire/ovpn/settings | grep ^ENABLED_ORANGE=on`) {
344 my $ovpnip = `cat /var/ipfire/ovpn/settings | grep ^DOVPN_SUBNET= | cut -c 14- | sed -e 's\/\\/255.255.255.0\/\/'`;
345 print <<END;
346 <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>
347 <td width='30%' align='center'>$ovpnip
348 <td width='45%' align='center'><font color=$Header::colourgreen>Online</font>
349 END
350 }
351
352 # Memory usage warning
353 my @free = `/usr/bin/free`;
354 $free[1] =~ m/(\d+)/;
355 my $mem = $1;
356 $free[2] =~ m/(\d+)/;
357 my $used = $1;
358 my $pct = int 100 * ($mem - $used) / $mem;
359 if ($used / $mem > 90) {
360 $warnmessage .= "<li> $Lang::tr{'high memory usage'}: $pct% !</li>\n";
361 }
362
363 # Diskspace usage warning
364 my @temp=();
365 my $temp2=();
366 my @df = `/bin/df -B M -x rootfs`;
367 foreach my $line (@df) {
368 next if $line =~ m/^Filesystem/;
369 if ($line =~ m/root/ ) {
370 $line =~ m/^.* (\d+)M.*$/;
371 @temp = split(/ +/,$line);
372 if ($1<5) {
373 # available:plain value in MB, and not %used as 10% is too much to waste on small disk
374 # and root size should not vary during time
375 $warnmessage .= "$Lang::tr{'filesystem full'}: $temp[0] <b>$Lang::tr{'free'}=$1M</b> !\n";
376 }
377
378 } else {
379 # $line =~ m/^.* (\d+)m.*$/;
380 $line =~ m/^.* (\d+)\%.*$/;
381 if ($1>90) {
382 @temp = split(/ /,$line);
383 $temp2=int(100-$1);
384 $warnmessage .= "$Lang::tr{'filesystem full'}: $temp[0] <b>$Lang::tr{'free'}=$temp2%</b> !\n";
385 }
386 }
387 }
388
389 if ($warnmessage) {
390 print "<tr><td align='center' bgcolor=$Header::colourred colspan='3'><font color='white'>$warnmessage</font></table>";
391 }
392 print <<END;
393 </table>
394
395 END
396 } else {
397 my $message='';
398 if ($death) {
399 $message = $Lang::tr{'ipfire has now shutdown'};
400 } else {
401 $message = $Lang::tr{'ipfire has now rebooted'};
402 }
403 print <<END
404 <div align='center'>
405 <table width='100%' bgcolor='#ffffff'>
406 <tr><td align='center'>
407 <br /><br /><img src='/images/IPFire.png' /><br /><br /><br />
408 </td></tr>
409 </table>
410 <br />
411 <font size='6'>$message</font>
412 </div>
413 END
414 ;
415
416 }
417
418 &Header::closebox();
419 &Header::closebigbox();
420 &Header::closepage();