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