header.pl: Remove "Connection: close".
[people/teissler/ipfire-2.x.git] / config / cfgroot / header.pl
CommitLineData
3ea75603
MT
1# SmoothWall CGIs
2#
3# This code is distributed under the terms of the GPL
4#
5# (c) The SmoothWall Team
6# Copyright (C) 2002 Alex Hudson - getcgihash() rewrite
7# Copyright (C) 2002 Bob Grant <bob@cache.ucr.edu> - validmac()
8# Copyright (c) 2002/04/13 Steve Bootes - add alias section, helper functions
9# Copyright (c) 2002/08/23 Mark Wormgoor <mark@wormgoor.com> validfqdn()
10# Copyright (c) 2003/09/11 Darren Critchley <darrenc@telus.net> srtarray()
11#
3ea75603
MT
12package Header;
13
14use CGI();
802d7954 15use File::Basename;
8b33e596 16use HTML::Entities();
3ea75603
MT
17use Socket;
18use Time::Local;
19
20$|=1; # line buffering
21
3ea75603
MT
22$Header::revision = 'final';
23$Header::swroot = '/var/ipfire';
0d909a4a 24$Header::graphdir='/srv/web/ipfire/html/graphs';
3ea75603
MT
25$Header::pagecolour = '#ffffff';
26#$Header::tablecolour = '#a0a0a0';
27$Header::tablecolour = '#FFFFFF';
28$Header::bigboxcolour = '#F6F4F4';
29$Header::boxcolour = '#EAE9EE';
30$Header::bordercolour = '#000000';
31$Header::table1colour = '#E0E0E0';
32$Header::table2colour = '#F0F0F0';
33$Header::colourred = '#993333';
34$Header::colourorange = '#FF9933';
35$Header::colouryellow = '#FFFF00';
36$Header::colourgreen = '#339933';
37$Header::colourblue = '#333399';
6d4147d2 38$Header::colourovpn = '#339999';
3ea75603
MT
39$Header::colourfw = '#000000';
40$Header::colourvpn = '#990099';
41$Header::colourerr = '#FF0000';
42$Header::viewsize = 150;
43$Header::errormessage = '';
44my %menuhash = ();
45my $menu = \%menuhash;
46%settings = ();
47%ethsettings = ();
48@URI = ();
3ea75603
MT
49
50### Make sure this is an SSL request
51if ($ENV{'SERVER_ADDR'} && $ENV{'HTTPS'} ne 'on') {
52 print "Status: 302 Moved\r\n";
f8e080ef 53 print "Location: https://$ENV{'SERVER_ADDR'}:444/$ENV{'PATH_INFO'}\r\n\r\n";
3ea75603
MT
54 exit 0;
55}
56
57### Initialize environment
6be4dd24
CS
58&General::readhash("${swroot}/main/settings", \%settings);
59&General::readhash("${swroot}/ethernet/settings", \%ethsettings);
3ea75603
MT
60$language = $settings{'LANGUAGE'};
61$hostname = $settings{'HOSTNAME'};
62$hostnameintitle = 0;
63
64### Initialize language
65if ($language =~ /^(\w+)$/) {$language = $1;}
66
67### Read English Files
68if ( -d "/var/ipfire/langs/en/" ) {
69 opendir(DIR, "/var/ipfire/langs/en/");
70 @names = readdir(DIR) or die "Cannot Read Directory: $!\n";
71 foreach $name(@names) {
72 next if ($name eq ".");
73 next if ($name eq "..");
74 next if (!($name =~ /\.pl$/));
75 require "${swroot}/langs/en/${name}";
76 };
77};
78
79
80### Enable Language Files
81if ( -d "/var/ipfire/langs/${language}/" ) {
82 opendir(DIR, "/var/ipfire/langs/${language}/");
83 @names = readdir(DIR) or die "Cannot Read Directory: $!\n";
84 foreach $name(@names) {
85 next if ($name eq ".");
86 next if ($name eq "..");
87 next if (!($name =~ /\.pl$/));
88 require "${swroot}/langs/${language}/${name}";
89 };
90};
91
935f311e
MT
92our $THEME_NAME = $settings{'THEME'};
93
3ea75603
MT
94require "${swroot}/langs/en.pl";
95require "${swroot}/langs/${language}.pl";
935f311e 96eval `/bin/cat /srv/web/ipfire/html/themes/$THEME_NAME/include/functions.pl`;
3ea75603
MT
97
98sub orange_used () {
6be4dd24 99 if ($ethsettings{'CONFIG_TYPE'} =~ /^[24]$/) {
3ea75603
MT
100 return 1;
101 }
102 return 0;
103}
104
105sub blue_used () {
6be4dd24 106 if ($ethsettings{'CONFIG_TYPE'} =~ /^[34]$/) {
3ea75603
MT
107 return 1;
108 }
109 return 0;
110}
111
112sub is_modem {
6be4dd24 113 if ($ethsettings{'CONFIG_TYPE'} =~ /^[0]$/) {
3ea75603
MT
114 return 1;
115 }
116 return 0;
117}
118
119### Initialize menu
120sub genmenu {
24249567 121
3ea75603
MT
122 my %subsystemhash = ();
123 my $subsystem = \%subsystemhash;
124
3ea75603
MT
125 my %substatushash = ();
126 my $substatus = \%substatushash;
3ea75603
MT
127
128 my %subnetworkhash = ();
129 my $subnetwork = \%subnetworkhash;
130
3ea75603
MT
131 my %subserviceshash = ();
132 my $subservices = \%subserviceshash;
133
3ea75603
MT
134 my %subfirewallhash = ();
135 my $subfirewall = \%subfirewallhash;
136
3ea75603
MT
137 my %subipfirehash = ();
138 my $subipfire = \%subipfirehash;
3ea75603 139
24249567
MT
140 my %sublogshash = ();
141 my $sublogs = \%sublogshash;
3ea75603 142
24249567
MT
143 eval `/bin/cat /var/ipfire/menu.d/*.menu`;
144 eval `/bin/cat /var/ipfire/menu.d/*.main`;
3ea75603 145
c980f4d2 146 if (! blue_used()) {
a615cab3 147 $menu->{'05.firewall'}{'subMenu'}->{'60.wireless'}{'enabled'} = 0;
c980f4d2 148 }
6be4dd24 149 if ( $ethsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ && $ethsettings{'RED_TYPE'} eq 'STATIC' ) {
a68fedca 150 $menu->{'03.network'}{'subMenu'}->{'70.aliases'}{'enabled'} = 1;
3ea75603 151 }
61027579
MT
152
153 if (&General::RedIsWireless()) {
154 $menu->{'01.system'}{'subMenu'}->{'21.wlan'}{'enabled'} = 1;
155 }
3ea75603
MT
156}
157
158sub showhttpheaders
159{
c43ae64f 160 print "Cache-control: private\n";
1d795ec5 161 print "Content-type: text/html; charset=UTF-8\n\n";
3ea75603
MT
162}
163
164sub is_menu_visible($) {
165 my $link = shift;
166 $link =~ s#\?.*$##;
167 return (-e $ENV{'DOCUMENT_ROOT'}."/../$link");
168}
169
170
171sub getlink($) {
172 my $root = shift;
173 if (! $root->{'enabled'}) {
174 return '';
175 }
176 if ($root->{'uri'} !~ /^$/) {
177 my $vars = '';
178 if ($root->{'vars'} !~ /^$/) {
179 $vars = '?'. $root->{'vars'};
180 }
181 if (! is_menu_visible($root->{'uri'})) {
182 return '';
183 }
184 return $root->{'uri'}.$vars;
185 }
186 my $submenus = $root->{'subMenu'};
187 if (! $submenus) {
188 return '';
189 }
190 foreach my $item (sort keys %$submenus) {
191 my $link = getlink($submenus->{$item});
192 if ($link ne '') {
193 return $link;
194 }
195 }
196 return '';
197}
198
199
200sub compare_url($) {
201 my $conf = shift;
202
203 my $uri = $conf->{'uri'};
204 my $vars = $conf->{'vars'};
205 my $novars = $conf->{'novars'};
206
207 if ($uri eq '') {
208 return 0;
209 }
210 if ($uri ne $URI[0]) {
211 return 0;
212 }
213 if ($novars) {
214 if ($URI[1] !~ /^$/) {
215 return 0;
216 }
217 }
218 if (! $vars) {
219 return 1;
220 }
221 return ($URI[1] eq $vars);
222}
223
224
225sub gettitle($) {
226 my $root = shift;
227
228 if (! $root) {
229 return '';
230 }
231 foreach my $item (sort keys %$root) {
232 my $val = $root->{$item};
233 if (compare_url($val)) {
234 $val->{'selected'} = 1;
235 if ($val->{'title'} !~ /^$/) {
236 return $val->{'title'};
237 }
238 return 'EMPTY TITLE';
239 }
240
241 my $title = gettitle($val->{'subMenu'});
242 if ($title ne '') {
243 $val->{'selected'} = 1;
244 return $title;
245 }
246 }
247 return '';
248}
249
3ea75603
MT
250sub getcgihash {
251 my ($hash, $params) = @_;
252 my $cgi = CGI->new ();
253 $hash->{'__CGI__'} = $cgi;
254 return if ($ENV{'REQUEST_METHOD'} ne 'POST');
255 if (!$params->{'wantfile'}) {
256 $CGI::DISABLE_UPLOADS = 1;
257 $CGI::POST_MAX = 512 * 1024;
258 } else {
259 $CGI::POST_MAX = 10 * 1024 * 1024;
260 }
261
262 $cgi->referer() =~ m/^https?\:\/\/([^\/]+)/;
263 my $referer = $1;
264 $cgi->url() =~ m/^https?\:\/\/([^\/]+)/;
265 my $servername = $1;
266 return if ($referer ne $servername);
267
268 ### Modified for getting multi-vars, split by |
269 %temp = $cgi->Vars();
270 foreach my $key (keys %temp) {
271 $hash->{$key} = $temp{$key};
272 $hash->{$key} =~ s/\0/|/g;
273 $hash->{$key} =~ s/^\s*(.*?)\s*$/$1/;
274 }
275
276 if (($params->{'wantfile'})&&($params->{'filevar'})) {
277 $hash->{$params->{'filevar'}} = $cgi->upload
278 ($params->{'filevar'});
279 }
280 return;
281}
282
3ea75603
MT
283
284# Test if IP is within a subnet
285# Call: IpInSubnet (Addr, Subnet, Subnet Mask)
286# Subnet can be an IP of the subnet: 10.0.0.0 or 10.0.0.1
287# Everything in dottted notation
288# Return: TRUE/FALSE
289sub IpInSubnet
290{
291 $ip = unpack('N', inet_aton(shift));
292 $start = unpack('N', inet_aton(shift));
293 $mask = unpack('N', inet_aton(shift));
294 $start &= $mask; # base of subnet...
295 $end = $start + ~$mask;
296 return (($ip >= $start) && ($ip <= $end));
297}
298
8b33e596
MT
299sub escape($) {
300 my $s = shift;
301 return HTML::Entities::encode_entities($s);
302}
303
304sub cleanhtml {
3ea75603
MT
305 my $outstring =$_[0];
306 $outstring =~ tr/,/ / if not defined $_[1] or $_[1] ne 'y';
8b33e596
MT
307
308 return escape($outstring);
3ea75603 309}
986e08d9 310
3ea75603
MT
311sub connectionstatus
312{
986e08d9
MT
313 my %pppsettings = ();
314 my %netsettings = ();
315 my $iface='';
316
317 $pppsettings{'PROFILENAME'} = 'None';
318 &General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
319 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
320
321 my $profileused='';
2fc53b15 322 unless ( $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) {
986e08d9
MT
323 $profileused="- $pppsettings{'PROFILENAME'}";
324 }
325
986e08d9 326 my ($timestr, $connstate);
986e08d9 327
7f9f8516 328 my $connstate = "<span>$Lang::tr{'idle'} $profileused</span>";
986e08d9 329
2fc53b15
MT
330 if (-e "${General::swroot}/red/active") {
331 $timestr = &General::age("${General::swroot}/red/active");
7f9f8516 332 $connstate = "<span>$Lang::tr{'connected'} - (<span>$timestr</span>) $profileused</span>";
2fc53b15 333 } else {
7f9f8516
CS
334 if ((open(KEEPCONNECTED, "</var/ipfire/red/keepconnected") == false) && ($pppsettings{'RECONNECTION'} eq "persistent")) {
335 $connstate = "<span>$Lang::tr{'connection closed'} $profileused</span>";
336 } elsif (($pppsettings{'RECONNECTION'} eq "dialondemand") && ( -e "${General::swroot}/red/dial-on-demand")) {
337 $connstate = "<span>$Lang::tr{'dod waiting'} $profileused</span>";
986e08d9 338 } else {
7f9f8516 339 $connstate = "<span>$Lang::tr{'connecting'} $profileused</span>" if (system("ps -ef | grep -q '[p]ppd'"));
986e08d9 340 }
986e08d9 341 }
2fc53b15 342
3ea75603
MT
343 return $connstate;
344}
345
3ea75603
MT
346sub CheckSortOrder {
347#Sorting of allocated leases
348 if ($ENV{'QUERY_STRING'} =~ /^IPADDR|^ETHER|^HOSTNAME|^ENDTIME/ ) {
6be4dd24 349 my $newsort=$ENV{'QUERY_STRING'};
cab84d3a 350 &General::readhash("${swroot}/dhcp/settings", \%dhcpsettings);
3ea75603
MT
351 $act=$dhcpsettings{'SORT_LEASELIST'};
352 #Reverse actual ?
353 if ($act =~ $newsort) {
354 if ($act !~ 'Rev') {$Rev='Rev'};
355 $newsort.=$Rev
356 };
357
358 $dhcpsettings{'SORT_LEASELIST'}=$newsort;
cab84d3a 359 &General::writehash("${swroot}/dhcp/settings", \%dhcpsettings);
3ea75603
MT
360 $dhcpsettings{'ACTION'} = 'SORT'; # avoid the next test "First lauch"
361 }
362
363}
364
365sub PrintActualLeases
366{
367 &openbox('100%', 'left', $tr{'current dynamic leases'});
368 print <<END
52ca3c80 369<table width='100%' class='tbl'>
3ea75603 370<tr>
52ca3c80
AM
371<th width='25%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IPADDR'><b>$tr{'ip address'}</b></a></th>
372<th width='25%' align='center'><a href='$ENV{'SCRIPT_NAME'}?ETHER'><b>$tr{'mac address'}</b></a></th>
373<th width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?HOSTNAME'><b>$tr{'hostname'}</b></a></th>
374<th width='25%' align='center'><a href='$ENV{'SCRIPT_NAME'}?ENDTIME'><b>$tr{'lease expires'} (local time d/m/y)</b></a></th>
375<th width='5%' align='center'><b>Add to fix leases<b></th>
3ea75603
MT
376</tr>
377END
52ca3c80 378;
3ea75603 379
986e08d9 380 open(LEASES,"/var/state/dhcp/dhcpd.leases") or die "Can't open dhcpd.leases";
3ea75603
MT
381 while ($line = <LEASES>) {
382 next if( $line =~ /^\s*#/ );
383 chomp($line);
384 @temp = split (' ', $line);
385
386 if ($line =~ /^\s*lease/) {
387 $ip = $temp[1];
388 #All field are not necessarily read. Clear everything
389 $endtime = 0;
390 $ether = "";
391 $hostname = "";
392 }
393
394 if ($line =~ /^\s*ends/) {
395 $line =~ /(\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)/;
396 $endtime = timegm($6, $5, $4, $3, $2 - 1, $1 - 1900);
397 }
398
399 if ($line =~ /^\s*hardware ethernet/) {
400 $ether = $temp[2];
401 $ether =~ s/;//g;
402 }
403
404 if ($line =~ /^\s*client-hostname/) {
405 $hostname = "$temp[1] $temp[2] $temp[3]";
406 $hostname =~ s/;//g;
407 $hostname =~ s/\"//g;
408 }
409
410 if ($line eq "}") {
411 @record = ('IPADDR',$ip,'ENDTIME',$endtime,'ETHER',$ether,'HOSTNAME',$hostname);
412 $record = {}; # create a reference to empty hash
413 %{$record} = @record; # populate that hash with @record
414 $entries{$record->{'IPADDR'}} = $record; # add this to a hash of hashes
415 }
416 }
417 close(LEASES);
418
419 my $id = 0;
52ca3c80 420 my $col="";
3ea75603 421 foreach my $key (sort leasesort keys %entries) {
32fdd7d9 422 print "<form method='post' action='/cgi-bin/dhcp.cgi'>\n";
3ea75603
MT
423 my $hostname = &cleanhtml($entries{$key}->{HOSTNAME},"y");
424
425 if ($id % 2) {
52ca3c80
AM
426 print "<tr>";
427 $col="bgcolor='$table1colour'";
3ea75603
MT
428 }
429 else {
52ca3c80
AM
430 print "<tr>";
431 $col="bgcolor='$table2colour'";
3ea75603
MT
432 }
433
434 print <<END
52ca3c80
AM
435<td align='center' $col><input type='hidden' name='FIX_ADDR' value='$entries{$key}->{IPADDR}' />$entries{$key}->{IPADDR}</td>
436<td align='center' $col><input type='hidden' name='FIX_MAC' value='$entries{$key}->{ETHER}' />$entries{$key}->{ETHER}</td>
437<td align='center' $col><input type='hidden' name='FIX_REMARK' value='$hostname' />&nbsp;$hostname</td>
438<td align='center' $col><input type='hidden' name='FIX_ENABLED' value='on' />
3ea75603 439END
52ca3c80 440;
3ea75603
MT
441
442 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $dst) = localtime ($entries{$key}->{ENDTIME});
443 $enddate = sprintf ("%02d/%02d/%d %02d:%02d:%02d",$mday,$mon+1,$year+1900,$hour,$min,$sec);
444
445 if ($entries{$key}->{ENDTIME} < time() ){
446 print "<strike>$enddate</strike>";
447 } else {
448 print "$enddate";
449 }
32fdd7d9 450 print <<END
52ca3c80
AM
451</td><td $col><input type='hidden' name='ACTION' value='$Lang::tr{'add'}2' /><input type='submit' name='SUBMIT' value='$Lang::tr{'add'}' />
452</td></tr></form>
32fdd7d9 453END
52ca3c80 454;
3ea75603
MT
455 $id++;
456 }
457
458 print "</table>";
459 &closebox();
460}
461
462
463# This sub is used during display of actives leases
464sub leasesort {
465 if (rindex ($dhcpsettings{'SORT_LEASELIST'},'Rev') != -1)
466 {
467 $qs=substr ($dhcpsettings{'SORT_LEASELIST'},0,length($dhcpsettings{'SORT_LEASELIST'})-3);
468 if ($qs eq 'IPADDR') {
469 @a = split(/\./,$entries{$a}->{$qs});
470 @b = split(/\./,$entries{$b}->{$qs});
471 ($b[0]<=>$a[0]) ||
472 ($b[1]<=>$a[1]) ||
473 ($b[2]<=>$a[2]) ||
474 ($b[3]<=>$a[3]);
475 }else {
476 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
477 }
478 }
479 else #not reverse
480 {
481 $qs=$dhcpsettings{'SORT_LEASELIST'};
482 if ($qs eq 'IPADDR') {
483 @a = split(/\./,$entries{$a}->{$qs});
484 @b = split(/\./,$entries{$b}->{$qs});
485 ($a[0]<=>$b[0]) ||
486 ($a[1]<=>$b[1]) ||
487 ($a[2]<=>$b[2]) ||
488 ($a[3]<=>$b[3]);
489 }else {
490 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
491 }
492 }
493}
1ddefbbd
CS
494
495sub colorize {
496 my $string = $_[0];
497 my @array = split(/\//,$string);
498 my $string2 = $array[0];
499
500 if ( $string eq "*" or $string eq "" ){
501 return $string;
502 } elsif ( $string =~ "ipsec" ){
503 return "<font color='".${Header::colourvpn}."'>".$string."</font>";
504 } elsif ( $string =~ "tun" ){
505 return "<font color='".${Header::colourovpn}."'>".$string."</font>";
506 } elsif ( $string =~ "lo" or $string =~ "127.0.0.0" ){
507 return "<font color='".${Header::colourfw}."'>".$string."</font>";
508 } elsif ( $string =~ $ethsettings{'GREEN_DEV'} or &IpInSubnet($string2,$ethsettings{'GREEN_NETADDRESS'},$ethsettings{'GREEN_NETMASK'}) ){
509 return "<font color='".${Header::colourgreen}."'>".$string."</font>";
510 } elsif ( $string =~ "ppp0" or $string =~ $ethsettings{'RED_DEV'} or $string =~ "0.0.0.0" or $string =~ $ethsettings{'RED_ADDRESS'} ){
511 return "<font color='".${Header::colourred}."'>".$string."</font>";
512 } elsif ( $ethsettings{'CONFIG_TYPE'}>1 and ( $string =~ $ethsettings{'BLUE_DEV'} or &IpInSubnet($string2,$ethsettings{'BLUE_NETADDRESS'},$ethsettings{'BLUE_NETMASK'}) )){
513 return "<font color='".${Header::colourblue}."'>".$string."</font>";
514 } elsif ( $ethsettings{'CONFIG_TYPE'}>2 and ( $string =~ $ethsettings{'ORANGE_DEV'} or &IpInSubnet($string2,$ethsettings{'ORANGE_NETADDRESS'},$ethsettings{'ORANGE_NETMASK'}) )){
515 return "<font color='".${Header::colourorange}."'>".$string."</font>";
516 } else {
517 return $string;
518 }
519}