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