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