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