]>
Commit | Line | Data |
---|---|---|
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 | if ($connstate =~ /$Lang::tr{'dod waiting'}/ || -e "${General::swroot}/main/refreshindex") { | |
59 | $refresh = "<meta http-equiv='refresh' content='30;'>"; | |
60 | } elsif ($connstate =~ /$Lang::tr{'connecting'}/) { | |
61 | $refresh = "<meta http-equiv='refresh' content='5;'>"; | |
62 | } | |
63 | ||
64 | if ($cgiparams{'ACTION'} eq $Lang::tr{'dial profile'}) | |
65 | { | |
66 | my $profile = $cgiparams{'PROFILE'}; | |
67 | my %tempcgiparams = (); | |
68 | $tempcgiparams{'PROFILE'} = ''; | |
69 | &General::readhash("${General::swroot}/ppp/settings-$cgiparams{'PROFILE'}", | |
70 | \%tempcgiparams); | |
71 | ||
72 | # make a link from the selected profile to the "default" one. | |
73 | unlink("${General::swroot}/ppp/settings"); | |
74 | link("${General::swroot}/ppp/settings-$cgiparams{'PROFILE'}", | |
75 | "${General::swroot}/ppp/settings"); | |
76 | system ("/usr/bin/touch", "${General::swroot}/ppp/updatesettings"); | |
77 | ||
78 | # read in the new params "early" so we can write secrets. | |
79 | %cgiparams = (); | |
80 | &General::readhash("${General::swroot}/ppp/settings", \%cgiparams); | |
81 | $cgiparams{'PROFILE'} = $profile; | |
82 | $cgiparams{'BACKUPPROFILE'} = $profile; | |
83 | &General::writehash("${General::swroot}/ppp/settings-$cgiparams{'PROFILE'}", | |
84 | \%cgiparams); | |
85 | ||
86 | # write secrets file. | |
87 | open(FILE, ">/${General::swroot}/ppp/secrets") or die "Unable to write secrets file."; | |
88 | flock(FILE, 2); | |
89 | my $username = $cgiparams{'USERNAME'}; | |
90 | my $password = $cgiparams{'PASSWORD'}; | |
91 | print FILE "'$username' * '$password'\n"; | |
92 | chmod 0600, "${General::swroot}/ppp/secrets"; | |
93 | close FILE; | |
94 | ||
95 | &General::log("$Lang::tr{'profile made current'} $tempcgiparams{'PROFILENAME'}"); | |
96 | $cgiparams{'ACTION'} = "$Lang::tr{'dial'}"; | |
97 | } | |
98 | ||
99 | if ($cgiparams{'ACTION'} eq $Lang::tr{'dial'}) { | |
100 | system('/usr/local/bin/redctrl start > /dev/null') == 0 | |
101 | or &General::log("Dial failed: $?"); sleep 1;} | |
102 | elsif ($cgiparams{'ACTION'} eq $Lang::tr{'hangup'}) { | |
103 | system('/usr/local/bin/redctrl stop > /dev/null') == 0 | |
104 | or &General::log("Hangup failed: $?"); sleep 1;} | |
105 | ||
106 | my $c; | |
107 | my $maxprofiles = 5; | |
108 | my @profilenames = (); | |
109 | ||
110 | for ($c = 1; $c <= $maxprofiles; $c++) | |
111 | { | |
112 | my %temppppsettings = (); | |
113 | $temppppsettings{'PROFILENAME'} = ''; | |
114 | &General::readhash("${General::swroot}/ppp/settings-$c", \%temppppsettings); | |
115 | $profilenames[$c] = $temppppsettings{'PROFILENAME'}; | |
116 | } | |
117 | my %selected; | |
118 | for ($c = 1; $c <= $maxprofiles; $c++) { | |
119 | $selected{'PROFILE'}{$c} = ''; | |
120 | } | |
121 | $selected{'PROFILE'}{$pppsettings{'PROFILE'}} = "selected='selected'"; | |
122 | my $dialButtonDisabled = "disabled='disabled'"; | |
123 | ||
124 | ||
125 | &Header::openpage($Lang::tr{'main page'}, 1, $refresh); | |
126 | &Header::openbigbox('', 'center'); | |
127 | &Header::openbox('100%', 'center', &Header::cleanhtml(`/bin/uname -n`,"y")); | |
128 | ||
129 | ||
130 | ||
131 | if ( ( $pppsettings{'VALID'} eq 'yes' && $modemsettings{'VALID'} eq 'yes' ) || ( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ && $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ )) { | |
132 | if (open(IPADDR,"${General::swroot}/ddns/ipcache")) { | |
133 | $ipaddr = <IPADDR>; | |
134 | close IPADDR; | |
135 | chomp ($ipaddr); | |
136 | } | |
137 | if (open(IPADDR,"${General::swroot}/red/local-ipaddress")) { | |
138 | $ipaddr = <IPADDR>; | |
139 | close IPADDR; | |
140 | chomp ($ipaddr); | |
141 | } | |
142 | } elsif ($modemsettings{'VALID'} eq 'no') { | |
143 | print "$Lang::tr{'modem settings have errors'}\n </b></font>\n"; | |
144 | } else { | |
145 | print "$Lang::tr{'profile has errors'}\n </b></font>\n"; | |
146 | } | |
147 | ||
148 | #if ( $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) { | |
149 | # $ipaddr = $netsettings{'RED_ADDRESS'}; | |
150 | #} | |
151 | ||
152 | my $death = 0; | |
153 | my $rebirth = 0; | |
154 | ||
155 | if ($cgiparams{'ACTION'} eq $Lang::tr{'shutdown'}) { | |
156 | $death = 1; | |
157 | &General::log($Lang::tr{'shutting down ipfire'}); | |
158 | system '/usr/local/bin/ipfirereboot down'; | |
159 | } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'reboot'}) { | |
160 | $rebirth = 1; | |
161 | &General::log($Lang::tr{'rebooting ipfire'}); | |
162 | system '/usr/local/bin/ipfirereboot boot'; | |
163 | } | |
164 | ||
165 | if ($death == 0 && $rebirth == 0) { | |
166 | ||
167 | print <<END | |
168 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
169 | <table width='100%'> | |
170 | <tr> | |
171 | <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reboot'}' /></td> | |
172 | <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'refresh'}' /></td> | |
173 | <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'shutdown'}' /></td> | |
174 | </tr> | |
175 | </table> | |
176 | END | |
177 | ; | |
178 | print <<END; | |
179 | ||
180 | <!-- Table of networks --> | |
181 | <table border='0' width=80%> | |
182 | <tr> <th bgcolor='$color{'color20'}'>$Lang::tr{'network'} | |
183 | <th bgcolor='$color{'color20'}'>IP | |
184 | <th bgcolor='$color{'color20'}'>$Lang::tr{'status'} | |
185 | <tr> <td bgcolor='$Header::colourred' width='25%'><a href="/cgi-bin/pppsetup.cgi"><font size='2' color='white'><b>$Lang::tr{'internet'}:</b></font></a><br> | |
186 | <td width='30%' align='center'>$ipaddr | |
187 | <td width='45%' align='center'>$connstate | |
188 | END | |
189 | if ( $netsettings{'RED_TYPE'} ne "STATIC" && $netsettings{'RED_TYPE'} ne "DHCP" ){ | |
190 | print `/usr/local/bin/dialctrl.pl show`; | |
191 | print <<END; | |
192 | <tr><td colspan='2'> | |
193 | <form method='post' action='$ENV{'SCRIPT_NAME'}'>$Lang::tr{'profile'}: | |
194 | <select name='PROFILE'> | |
195 | END | |
196 | for ($c = 1; $c <= $maxprofiles; $c++) | |
197 | { | |
198 | if ($profilenames[$c] ne '') { | |
199 | $dialButtonDisabled = ""; | |
200 | print "\t<option value='$c' $selected{'PROFILE'}{$c}>$c. $profilenames[$c]</option>\n"; | |
201 | } | |
202 | } | |
203 | $dialButtonDisabled = "disabled='disabled'" if (-e '/var/run/ppp-ipfire.pid' || -e "${General::swroot}/red/active"); | |
204 | if ( ( $pppsettings{'VALID'} eq 'yes' ) || ( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ && $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) ) { | |
205 | print <<END; | |
206 | </select> | |
207 | <input type='submit' name='ACTION' value='$Lang::tr{'dial profile'}' $dialButtonDisabled /> | |
208 | </form> | |
209 | <td align='center'> | |
210 | <table width='100%' border='0'> | |
211 | <tr> | |
212 | <td width='50%' align='right'> <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
213 | <input type='submit' name='ACTION' value='$Lang::tr{'dial'}'> | |
214 | </form> | |
215 | <td width='50%' align='left'> <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
216 | <input type='submit' name='ACTION' value='$Lang::tr{'hangup'}'> | |
217 | </form> | |
218 | </table> | |
219 | END | |
220 | } else { | |
221 | print "$Lang::tr{'profile has errors'}\n </b></font>\n"; | |
222 | } | |
223 | } | |
224 | my $HOSTNAME = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0]; | |
225 | if ( "$HOSTNAME" ne "" ) { | |
226 | print <<END; | |
227 | <tr><td><b>Hostname:</b><td align='center'>$HOSTNAME<td> | |
228 | END | |
229 | } | |
230 | ||
231 | if ( -e "/var/ipfire/red/remote-ipaddress" ) { | |
232 | my $GATEWAY = `cat /var/ipfire/red/remote-ipaddress`; | |
233 | chomp($GATEWAY); | |
234 | print <<END; | |
235 | <tr><td><b>Gateway:</b><td align='center'>$GATEWAY<td> | |
236 | END | |
237 | } | |
238 | ||
239 | my $DNS1 = `cat /var/ipfire/red/dns1`; | |
240 | my $DNS2 = `cat /var/ipfire/red/dns2`; | |
241 | chomp($DNS1); | |
242 | chomp($DNS1); | |
243 | ||
244 | if ( $DNS1 ) { print <<END; | |
245 | <tr><td><b>DNS-Server:</b><td align='center'>$DNS1 | |
246 | END | |
247 | } | |
248 | if ( $DNS2 ) { print <<END; | |
249 | <td align='center'>$DNS2 | |
250 | END | |
251 | } else { print <<END; | |
252 | <td> | |
253 | END | |
254 | } | |
255 | ||
256 | if ( $netsettings{'GREEN_DEV'} ) { print <<END; | |
257 | <tr><td bgcolor='$Header::colourgreen' width='25%'><a href="/cgi-bin/dhcp.cgi"><font size='2' color='white'><b>$Lang::tr{'lan'}:</b></font></a> | |
258 | <td width='30%' align='center'>$netsettings{'GREEN_ADDRESS'} | |
259 | <td width='45%' align='center'> | |
260 | END | |
261 | if ( `cat /var/ipfire/proxy/advanced/settings | grep ^ENABLE=on` ) { | |
262 | print "Proxy an"; | |
263 | if ( `cat /var/ipfire/proxy/advanced/settings | grep ^TRANSPARENT=on` ) { print " (transparent)"; } | |
264 | } else { print "Proxy aus"; } | |
265 | } | |
266 | if ( $netsettings{'BLUE_DEV'} ) { print <<END; | |
267 | <tr><td bgcolor='$Header::colourblue' width='25%'><a href="/cgi-bin/wireless.cgi"><font size='2' color='white'><b>$Lang::tr{'wireless'}:</b></font></a><br> | |
268 | <td width='30%' align='center'>$netsettings{'BLUE_ADDRESS'} | |
269 | <td width='45%' align='center'> | |
270 | END | |
271 | if ( `cat /var/ipfire/proxy/advanced/settings | grep ^ENABLE_BLUE=on` ) { | |
272 | print "Proxy an"; | |
273 | if ( `cat /var/ipfire/proxy/advanced/settings | grep ^TRANSPARENT_BLUE=on` ) { print " (transparent)"; } | |
274 | } else { print "Proxy aus"; } | |
275 | } | |
276 | if ( $netsettings{'ORANGE_DEV'} ) { print <<END; | |
277 | <tr><td bgcolor='$Header::colourorange' width='25%'><a href="/cgi-bin/dmzholes.cgi"><font size='2' color='white'><b>$Lang::tr{'dmz'}:</b></font></a><br> | |
278 | <td width='30%' align='center'>$netsettings{'ORANGE_ADDRESS'} | |
279 | <td width='45%' align='center'><font color=$Header::colourgreen>Online</font> | |
280 | END | |
281 | } | |
282 | if ( `cat /var/ipfire/vpn/settings | grep ^ENABLED=on` || | |
283 | `cat /var/ipfire/vpn/settings | grep ^ENABLED_BLUE=on` ) { | |
284 | my $ipsecip = `cat /var/ipfire/vpn/settings | grep ^VPN_IP= | cut -c 8-`; | |
285 | my @status = `/usr/sbin/ipsec auto --status`; | |
286 | my %confighash = (); | |
287 | &General::readhasharray("${General::swroot}/vpn/config", \%confighash); | |
288 | print <<END; | |
289 | <tr><td bgcolor='$Header::colourvpn' width='25%'><a href="/cgi-bin/vpnmain.cgi"><font size='2' color='white'><b>$Lang::tr{'vpn'}:</b></font></a><br> | |
290 | <td width='30%' align='center'>$ipsecip | |
291 | <td width='45%' align='center'><font color=$Header::colourgreen>Online</font> | |
292 | END | |
293 | my $id = 0; | |
294 | my $gif; | |
295 | foreach my $key (keys %confighash) { | |
296 | if ($confighash{$key}[0] eq 'on') { $gif = 'on.gif'; } else { $gif = 'off.gif'; } | |
297 | ||
298 | if ($id % 2) { | |
299 | print "<tr><td align='center'> </td><td align='center' nowrap='nowrap' bgcolor='$color{'color20'}'>$confighash{$key}[1] / " . $Lang::tr{"$confighash{$key}[3]"} . " (" . $Lang::tr{"$confighash{$key}[4]"} . ")</td>"; | |
300 | } else { | |
301 | print "<tr><td align='center'> </td><td align='center' nowrap='nowrap' bgcolor='$color{'color22'}'>$confighash{$key}[1] / " . $Lang::tr{"$confighash{$key}[3]"} . " (" . $Lang::tr{"$confighash{$key}[4]"} . ")</td>"; | |
302 | } | |
303 | ||
304 | 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>"; | |
305 | if ($confighash{$key}[0] eq 'off') { | |
306 | $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>"; | |
307 | } else { | |
308 | foreach my $line (@status) { | |
309 | if ($line =~ /\"$confighash{$key}[1]\".*IPsec SA established/) { | |
310 | $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>"; | |
311 | } | |
312 | } | |
313 | } | |
314 | print "<td align='center'>$active</td>"; | |
315 | } | |
316 | } | |
317 | if ( `cat /var/ipfire/ovpn/settings | grep ^ENABLED=on` || | |
318 | `cat /var/ipfire/ovpn/settings | grep ^ENABLED_BLUE=on` || | |
319 | `cat /var/ipfire/ovpn/settings | grep ^ENABLED_ORANGE=on`) { | |
320 | my $ovpnip = `cat /var/ipfire/ovpn/settings | grep ^DOVPN_SUBNET= | cut -c 14- | sed -e 's\/\\/255.255.255.0\/\/'`; | |
321 | print <<END; | |
322 | <tr><td bgcolor='$Header::colourovpn' width='25%'><a href="/cgi-bin/ovpnmain.cgi"><font size='2' color='white'><b>OpenVPN:</b></font></a><br> | |
323 | <td width='30%' align='center'>$ovpnip | |
324 | <td width='45%' align='center'><font color=$Header::colourgreen>Online</font> | |
325 | END | |
326 | } | |
327 | ||
328 | # Memory usage warning | |
329 | my @free = `/usr/bin/free`; | |
330 | $free[1] =~ m/(\d+)/; | |
331 | my $mem = $1; | |
332 | $free[2] =~ m/(\d+)/; | |
333 | my $used = $1; | |
334 | my $pct = int 100 * ($mem - $used) / $mem; | |
335 | if ($used / $mem > 90) { | |
336 | $warnmessage .= "<li> $Lang::tr{'high memory usage'}: $pct% !</li>\n"; | |
337 | } | |
338 | ||
339 | # Diskspace usage warning | |
340 | my @temp=(); | |
341 | my $temp2=(); | |
342 | my @df = `/bin/df -B M -x rootfs`; | |
343 | foreach my $line (@df) { | |
344 | next if $line =~ m/^Filesystem/; | |
345 | if ($line =~ m/root/ ) { | |
346 | $line =~ m/^.* (\d+)M.*$/; | |
347 | @temp = split(/ +/,$line); | |
348 | if ($1<5) { | |
349 | # available:plain value in MB, and not %used as 10% is too much to waste on small disk | |
350 | # and root size should not vary during time | |
351 | $warnmessage .= "$Lang::tr{'filesystem full'}: $temp[0] <b>$Lang::tr{'free'}=$1M</b> !\n"; | |
352 | } | |
353 | ||
354 | } else { | |
355 | # $line =~ m/^.* (\d+)m.*$/; | |
356 | $line =~ m/^.* (\d+)\%.*$/; | |
357 | if ($1>90) { | |
358 | @temp = split(/ /,$line); | |
359 | $temp2=int(100-$1); | |
360 | $warnmessage .= "$Lang::tr{'filesystem full'}: $temp[0] <b>$Lang::tr{'free'}=$temp2%</b> !\n"; | |
361 | } | |
362 | } | |
363 | } | |
364 | ||
365 | if ($warnmessage) { | |
366 | print "<tr><td align='center' bgcolor=$Header::colourred colspan='3'><font color='white'>$warnmessage</font></table>"; | |
367 | } | |
368 | print <<END; | |
369 | </table> | |
370 | ||
371 | END | |
372 | } else { | |
373 | my $message=''; | |
374 | if ($death) { | |
375 | $message = $Lang::tr{'ipfire has now shutdown'}; | |
376 | } else { | |
377 | $message = $Lang::tr{'ipfire has now rebooted'}; | |
378 | } | |
379 | print <<END | |
380 | <div align='center'> | |
381 | <table width='100%' bgcolor='#ffffff'> | |
382 | <tr><td align='center'> | |
383 | <br /><br /><img src='/images/IPFire.png' /><br /><br /><br /> | |
384 | </td></tr> | |
385 | </table> | |
386 | <br /> | |
387 | <font size='6'>$message</font> | |
388 | </div> | |
389 | END | |
390 | ; | |
391 | } | |
392 | &Header::closebox(); | |
393 | &Header::closebigbox(); | |
394 | &Header::closepage(); |