]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/index.cgi
core update: add last webif changes.
[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 require "/opt/pakfire/lib/functions.pl";
32
33 my %cgiparams=();
34 my %pppsettings=();
35 my %modemsettings=();
36 my %netsettings=();
37 my %ddnssettings=();
38 my $warnmessage = '';
39 my $refresh = "";
40 my $ipaddr='';
41
42
43 &Header::showhttpheaders();
44
45 $cgiparams{'ACTION'} = '';
46 &Header::getcgihash(\%cgiparams);
47 $pppsettings{'VALID'} = '';
48 $pppsettings{'PROFILENAME'} = 'None';
49 &General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
50 &General::readhash("${General::swroot}/modem/settings", \%modemsettings);
51 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
52 &General::readhash("${General::swroot}/ddns/settings", \%ddnssettings);
53
54 my %color = ();
55 my %mainsettings = ();
56 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
57 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
58
59 my $connstate = &Header::connectionstatus();
60
61 # check if reboot is necessary
62 my $reboot = 0;
63 if (`find /var/run/need_reboot 2>/dev/null`) {
64 $reboot = 1;
65 }
66
67 if ($cgiparams{'ACTION'} eq $Lang::tr{'shutdown'} || $cgiparams{'ACTION'} eq $Lang::tr{'reboot'}) {
68 $refresh = "<meta http-equiv='refresh' content='300;'>";
69 } elsif ($connstate =~ /$Lang::tr{'connecting'}/ || /$Lang::tr{'connection closed'}/ ){
70 $refresh = "<meta http-equiv='refresh' content='5;'>";
71 } elsif ($connstate =~ /$Lang::tr{'dod waiting'}/ || -e "${General::swroot}/main/refreshindex") {
72 $refresh = "<meta http-equiv='refresh' content='30;'>";
73 }
74
75 if ($cgiparams{'ACTION'} eq $Lang::tr{'dial profile'})
76 {
77 my $profile = $cgiparams{'PROFILE'};
78 my %tempcgiparams = ();
79 $tempcgiparams{'PROFILE'} = '';
80 &General::readhash("${General::swroot}/ppp/settings-$cgiparams{'PROFILE'}",
81 \%tempcgiparams);
82
83 # make a link from the selected profile to the "default" one.
84 unlink("${General::swroot}/ppp/settings");
85 link("${General::swroot}/ppp/settings-$cgiparams{'PROFILE'}",
86 "${General::swroot}/ppp/settings");
87 system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings");
88
89 # read in the new params "early" so we can write secrets.
90 %cgiparams = ();
91 &General::readhash("${General::swroot}/ppp/settings", \%cgiparams);
92 $cgiparams{'PROFILE'} = $profile;
93 $cgiparams{'BACKUPPROFILE'} = $profile;
94 &General::writehash("${General::swroot}/ppp/settings-$cgiparams{'PROFILE'}",
95 \%cgiparams);
96
97 # write secrets file.
98 open(FILE, ">/${General::swroot}/ppp/secrets") or die "Unable to write secrets file.";
99 flock(FILE, 2);
100 my $username = $cgiparams{'USERNAME'};
101 my $password = $cgiparams{'PASSWORD'};
102 print FILE "'$username' * '$password'\n";
103 chmod 0600, "${General::swroot}/ppp/secrets";
104 close FILE;
105
106 &General::log("$Lang::tr{'profile made current'} $tempcgiparams{'PROFILENAME'}");
107 $cgiparams{'ACTION'} = "$Lang::tr{'dial'}";
108 }
109
110 if ($cgiparams{'ACTION'} eq $Lang::tr{'dial'}) {
111 system('/usr/local/bin/redctrl start > /dev/null') == 0
112 or &General::log("Dial failed: $?"); sleep 1;}
113 elsif ($cgiparams{'ACTION'} eq $Lang::tr{'hangup'}) {
114 system('/usr/local/bin/redctrl stop > /dev/null') == 0
115 or &General::log("Hangup failed: $?"); sleep 1;}
116
117 my $c;
118 my $maxprofiles = 5;
119 my @profilenames = ();
120
121 for ($c = 1; $c <= $maxprofiles; $c++)
122 {
123 my %temppppsettings = ();
124 $temppppsettings{'PROFILENAME'} = '';
125 &General::readhash("${General::swroot}/ppp/settings-$c", \%temppppsettings);
126 $profilenames[$c] = $temppppsettings{'PROFILENAME'};
127 }
128 my %selected;
129 for ($c = 1; $c <= $maxprofiles; $c++) {
130 $selected{'PROFILE'}{$c} = '';
131 }
132 $selected{'PROFILE'}{$pppsettings{'PROFILE'}} = "selected='selected'";
133 my $dialButtonDisabled = "disabled='disabled'";
134
135
136 &Header::openpage($Lang::tr{'main page'}, 1, $refresh);
137 &Header::openbigbox('', 'center');
138 &Header::openbox('100%', 'center', &Header::cleanhtml(`/bin/uname -n`,"y"));
139
140
141
142 if ( ( $pppsettings{'VALID'} eq 'yes' && $modemsettings{'VALID'} eq 'yes' ) || ( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ && $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ )) {
143 if (open(IPADDR,"${General::swroot}/ddns/ipcache")) {
144 $ipaddr = <IPADDR>;
145 close IPADDR;
146 chomp ($ipaddr);
147 }
148 if (open(IPADDR,"${General::swroot}/red/local-ipaddress")) {
149 $ipaddr = <IPADDR>;
150 close IPADDR;
151 chomp ($ipaddr);
152 }
153 } elsif ($modemsettings{'VALID'} eq 'no') {
154 print "$Lang::tr{'modem settings have errors'}\n </b></font>\n";
155 } else {
156 print "$Lang::tr{'profile has errors'}\n </b></font>\n";
157 }
158
159 #if ( $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) {
160 # $ipaddr = $netsettings{'RED_ADDRESS'};
161 #}
162
163 my $death = 0;
164 my $rebirth = 0;
165
166 if ($cgiparams{'ACTION'} eq $Lang::tr{'shutdown'}) {
167 $death = 1;
168 &General::log($Lang::tr{'shutting down ipfire'});
169 system '/usr/local/bin/ipfirereboot down';
170 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'reboot'}) {
171 $rebirth = 1;
172 &General::log($Lang::tr{'rebooting ipfire'});
173 system '/usr/local/bin/ipfirereboot boot';
174 }
175
176 if ($death == 0 && $rebirth == 0) {
177
178 if ($mainsettings{'REBOOTQUESTION'} eq "off") {
179 print <<END
180 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
181 <table width='100%'>
182 <tr>
183 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reboot'}' /></td>
184 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'refresh'}' /></td>
185 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'shutdown'}' /></td>
186 </tr>
187 </table>
188 </form>
189 END
190 ;
191 } else {
192 if ($cgiparams{'ACTION'} eq $Lang::tr{'reboot ask'}) {
193 print <<END
194 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
195 <table width='100%'>
196 <tr>
197 <td colspan="3" align='left'><font color="red">$Lang::tr{'reboot sure'}</font></td>
198 </tr>
199 <tr>
200 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reboot'}' /></td>
201 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'refresh'}' /></td>
202 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'shutdown ask'}' /></td>
203 </tr>
204 </table>
205 </form>
206 END
207 ;
208 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'shutdown ask'}) {
209 print <<END
210 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
211 <table width='100%'>
212 <tr>
213 <td colspan="3" align='right'><font color="red">$Lang::tr{'shutdown sure'}</font></td>
214 </tr>
215 <tr>
216 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reboot ask'}' /></td>
217 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'refresh'}' /></td>
218 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'shutdown'}' /></td>
219 </tr>
220 </table>
221 </form>
222 END
223 ;
224 } else {
225 print <<END
226 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
227 <table width='100%'>
228 <tr>
229 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reboot ask'}' /></td>
230 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'refresh'}' /></td>
231 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'shutdown ask'}' /></td>
232 </tr>
233 </table>
234 </form>
235 END
236 ;
237 }
238 }
239 print <<END;
240
241
242 <!-- Table of networks -->
243 <table border='0' width=80%>
244 <tr> <th bgcolor='$color{'color20'}'>$Lang::tr{'network'}
245 <th bgcolor='$color{'color20'}'>IP
246 <th bgcolor='$color{'color20'}'>$Lang::tr{'status'}
247 <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>
248 <td width='30%' align='center'>$ipaddr
249 <td width='45%' align='center'>$connstate
250 END
251 if ( $netsettings{'RED_TYPE'} ne "STATIC" && $netsettings{'RED_TYPE'} ne "DHCP" ){
252 print `/usr/local/bin/dialctrl.pl show`;
253 print <<END;
254 <tr><td colspan='2'>
255 <form method='post' action='$ENV{'SCRIPT_NAME'}'>$Lang::tr{'profile'}:
256 <select name='PROFILE'>
257 END
258 for ($c = 1; $c <= $maxprofiles; $c++)
259 {
260 if ($profilenames[$c] ne '') {
261 $dialButtonDisabled = "";
262 print "\t<option value='$c' $selected{'PROFILE'}{$c}>$c. $profilenames[$c]</option>\n";
263 }
264 }
265 $dialButtonDisabled = "disabled='disabled'" if (-e '/var/run/ppp-ipfire.pid' || -e "${General::swroot}/red/active");
266 if ( ( $pppsettings{'VALID'} eq 'yes' ) || ( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ && $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) ) {
267 print <<END;
268 </select>
269 <input type='submit' name='ACTION' value='$Lang::tr{'dial profile'}' $dialButtonDisabled />
270 </form>
271 <td align='center'>
272 <table width='100%' border='0'>
273 <tr>
274 <td width='50%' align='right'> <form method='post' action='$ENV{'SCRIPT_NAME'}'>
275 <input type='submit' name='ACTION' value='$Lang::tr{'dial'}'>
276 </form>
277 <td width='50%' align='left'> <form method='post' action='$ENV{'SCRIPT_NAME'}'>
278 <input type='submit' name='ACTION' value='$Lang::tr{'hangup'}'>
279 </form>
280 </table>
281 END
282 } else {
283 print "$Lang::tr{'profile has errors'}\n </b></font>\n";
284 }
285 }
286 my $HOSTNAME = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
287 if ( "$HOSTNAME" ne "" ) {
288 print <<END;
289 <tr><td><b>Hostname:</b><td align='center'>$HOSTNAME<td>&nbsp;
290 END
291 }
292
293 if ( -e "/var/ipfire/red/remote-ipaddress" ) {
294 my $GATEWAY = `cat /var/ipfire/red/remote-ipaddress`;
295 chomp($GATEWAY);
296 print <<END;
297 <tr><td><b>Gateway:</b><td align='center'>$GATEWAY<td>&nbsp;
298 END
299 }
300
301 my $DNS1 = `cat /var/ipfire/red/dns1`;
302 my $DNS2 = `cat /var/ipfire/red/dns2`;
303 chomp($DNS1);
304 chomp($DNS1);
305
306 if ( $DNS1 ) { print <<END;
307 <tr><td><b>DNS-Server:</b><td align='center'>$DNS1
308 END
309 }
310 if ( $DNS2 ) { print <<END;
311 <td align='center'>$DNS2
312 END
313 } else { print <<END;
314 <td>&nbsp;
315 END
316 }
317
318 if ( $netsettings{'GREEN_DEV'} ) { print <<END;
319 <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>
320 <td width='30%' align='center'>$netsettings{'GREEN_ADDRESS'}
321 <td width='45%' align='center'>
322 END
323 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^ENABLE=on` ) {
324 print $Lang::tr{'advproxy on'};
325 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^TRANSPARENT=on` ) { print " (transparent)"; }
326 } else { print $Lang::tr{'advproxy off'}; }
327 }
328 if ( $netsettings{'BLUE_DEV'} ) { print <<END;
329 <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>
330 <td width='30%' align='center'>$netsettings{'BLUE_ADDRESS'}
331 <td width='45%' align='center'>
332 END
333 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^ENABLE_BLUE=on` ) {
334 print $Lang::tr{'advproxy on'};
335 if ( `cat /var/ipfire/proxy/advanced/settings | grep ^TRANSPARENT_BLUE=on` ) { print " (transparent)"; }
336 } else { print $Lang::tr{'advproxy off'}; }
337 }
338 if ( $netsettings{'ORANGE_DEV'} ) { print <<END;
339 <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>
340 <td width='30%' align='center'>$netsettings{'ORANGE_ADDRESS'}
341 <td width='45%' align='center'><font color=$Header::colourgreen>Online</font>
342 END
343 }
344 if ( `cat /var/ipfire/vpn/settings | grep ^ENABLED=on` ||
345 `cat /var/ipfire/vpn/settings | grep ^ENABLED_BLUE=on` ) {
346 my $ipsecip = `cat /var/ipfire/vpn/settings | grep ^VPN_IP= | cut -c 8-`;
347 my @status = `/usr/local/bin/ipsecctrl I`;
348 my %confighash = ();
349 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
350 print <<END;
351 <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>
352 <td width='30%' align='center'>$ipsecip
353 <td width='45%' align='center'><font color=$Header::colourgreen>Online</font>
354 END
355 my $id = 0;
356 my $gif;
357 foreach my $key (keys %confighash) {
358 if ($confighash{$key}[0] eq 'on') { $gif = 'on.gif'; } else { $gif = 'off.gif'; }
359
360 if ($id % 2) {
361 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>";
362 } else {
363 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>";
364 }
365
366 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>";
367 if ($confighash{$key}[0] eq 'off') {
368 $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>";
369 } else {
370 foreach my $line (@status) {
371 if ($line =~ /\"$confighash{$key}[1]\".*IPsec SA established/) {
372 $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>";
373 }
374 }
375 }
376 print "<td align='center'>$active</td>";
377 }
378 }
379 if ( `cat /var/ipfire/ovpn/settings | grep ^ENABLED=on` ||
380 `cat /var/ipfire/ovpn/settings | grep ^ENABLED_BLUE=on` ||
381 `cat /var/ipfire/ovpn/settings | grep ^ENABLED_ORANGE=on`) {
382 my $ovpnip = `cat /var/ipfire/ovpn/settings | grep ^DOVPN_SUBNET= | cut -c 14- | sed -e 's\/\\/255.255.255.0\/\/'`;
383 print <<END;
384 <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>
385 <td width='30%' align='center'>$ovpnip
386 <td width='45%' align='center'><font color=$Header::colourgreen>Online</font>
387 END
388 }
389
390 # Memory usage warning
391 my @free = `/usr/bin/free`;
392 $free[1] =~ m/(\d+)/;
393 my $mem = $1;
394 $free[2] =~ m/(\d+)/;
395 my $used = $1;
396 my $pct = int 100 * ($mem - $used) / $mem;
397 if ($used / $mem > 90) {
398 $warnmessage .= "<li> $Lang::tr{'high memory usage'}: $pct% !</li>\n";
399 }
400
401 # Diskspace usage warning
402 my @temp=();
403 my $temp2=();
404 my @df = `/bin/df -B M -x rootfs`;
405 foreach my $line (@df) {
406 next if $line =~ m/^Filesystem/;
407 if ($line =~ m/root/ ) {
408 $line =~ m/^.* (\d+)M.*$/;
409 @temp = split(/ +/,$line);
410 if ($1<5) {
411 # available:plain value in MB, and not %used as 10% is too much to waste on small disk
412 # and root size should not vary during time
413 $warnmessage .= "<li> $Lang::tr{'filesystem full'}: $temp[0] <b>$Lang::tr{'free'}=$1M</b> !</li>\n";
414 }
415
416 } else {
417 # $line =~ m/^.* (\d+)m.*$/;
418 $line =~ m/^.* (\d+)\%.*$/;
419 if ($1>90) {
420 @temp = split(/ /,$line);
421 $temp2=int(100-$1);
422 $warnmessage .= "<li> $Lang::tr{'filesystem full'}: $temp[0] <b>$Lang::tr{'free'}=$temp2%</b> !</li>\n";
423 }
424 }
425 }
426
427 # S.M.A.R.T. health warning
428 my @files = `/bin/ls /var/run/smartctl_out_hddtemp-*`;
429 foreach my $file (@files) {
430 chomp ($file);
431 my $disk=`echo $file | cut -d"-" -f2`;
432 chomp ($disk);
433 if (`/bin/grep "SAVE ALL DATA" $file`) {
434 $warnmessage .= "<li> $Lang::tr{'smartwarn1'} /dev/$disk $Lang::tr{'smartwarn2'} !</li>\n\n";
435 }
436 }
437
438
439 if ($warnmessage) {
440 print "<tr><td align='center' bgcolor=$Header::colourred colspan='3'><font color='white'>$warnmessage</font></table>";
441 }
442 print <<END;
443 </table>
444 END
445 ;
446 &Pakfire::dblist("upgrade", "notice");
447 print <<END;
448 END
449 if ($reboot == 1) {
450 print "<br /><font color='red'>$Lang::tr{'needreboot'}!</font>";
451 }
452 } else {
453 my $message='';
454 if ($death) {
455 $message = $Lang::tr{'ipfire has now shutdown'};
456 } else {
457 $message = $Lang::tr{'ipfire has now rebooted'};
458 }
459 print <<END
460 <div align='center'>
461 <table width='100%' bgcolor='#ffffff'>
462 <tr><td align='center'>
463 <br /><br /><img src='/images/IPFire.png' /><br /><br /><br />
464 </td></tr>
465 </table>
466 <br />
467 <font size='6'>$message</font>
468 </div>
469 END
470 ;
471
472 }
473
474 &Header::closebox();
475 &Header::closebigbox();
476 &Header::closepage();