e3098177819ea88a41c8f29245f47b788576469b
[people/pmueller/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 Socket;
16 use Time::Local;
17
18 $|=1; # line buffering
19
20 $Header::revision = 'final';
21 $Header::swroot = '/var/ipfire';
22 $Header::graphdir='/srv/web/ipfire/html/graphs';
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';
36 $Header::colourovpn = '#339999';
37 $Header::colourfw = '#000000';
38 $Header::colourvpn = '#990099';
39 $Header::colourerr = '#FF0000';
40 $Header::viewsize = 150;
41 $Header::errormessage = '';
42 my %menuhash = ();
43 my $menu = \%menuhash;
44 %settings = ();
45 %ethsettings = ();
46 @URI = ();
47
48 ### Make sure this is an SSL request
49 if ($ENV{'SERVER_ADDR'} && $ENV{'HTTPS'} ne 'on') {
50 print "Status: 302 Moved\r\n";
51 print "Location: https://$ENV{'SERVER_ADDR'}:444/$ENV{'PATH_INFO'}\r\n\r\n";
52 exit 0;
53 }
54
55 ### Initialize environment
56 &General::readhash("${swroot}/main/settings", \%settings);
57 &General::readhash("${swroot}/ethernet/settings", \%ethsettings);
58 $language = $settings{'LANGUAGE'};
59 $hostname = $settings{'HOSTNAME'};
60 $hostnameintitle = 0;
61
62 ### Initialize language
63 if ($language =~ /^(\w+)$/) {$language = $1;}
64
65 ### Read English Files
66 if ( -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
79 if ( -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
90 ### Read IPFire Buildversion
91 $FIREBUILD = "File not found: firebuild\n";
92 if (open(MYFile, "<${swroot}/firebuild")) {
93 $FIREBUILD = <MYFile>;
94 chomp($FIREBUILD);
95 $FIREBUILD = "(Build: $FIREBUILD)";
96 close(MYFile);
97 };
98
99 require "${swroot}/langs/en.pl";
100 require "${swroot}/langs/${language}.pl";
101 eval `/bin/cat /srv/web/ipfire/html/themes/$settings{'THEME'}/include/functions.pl`;
102
103 sub orange_used () {
104 if ($ethsettings{'CONFIG_TYPE'} =~ /^[24]$/) {
105 return 1;
106 }
107 return 0;
108 }
109
110 sub blue_used () {
111 if ($ethsettings{'CONFIG_TYPE'} =~ /^[34]$/) {
112 return 1;
113 }
114 return 0;
115 }
116
117 sub is_modem {
118 if ($ethsettings{'CONFIG_TYPE'} =~ /^[0]$/) {
119 return 1;
120 }
121 return 0;
122 }
123
124 ### Initialize menu
125 sub genmenu {
126
127 my %subsystemhash = ();
128 my $subsystem = \%subsystemhash;
129
130 my %substatushash = ();
131 my $substatus = \%substatushash;
132
133 my %subnetworkhash = ();
134 my $subnetwork = \%subnetworkhash;
135
136 my %subserviceshash = ();
137 my $subservices = \%subserviceshash;
138
139 my %subfirewallhash = ();
140 my $subfirewall = \%subfirewallhash;
141
142 my %subipfirehash = ();
143 my $subipfire = \%subipfirehash;
144
145 my %sublogshash = ();
146 my $sublogs = \%sublogshash;
147
148 eval `/bin/cat /var/ipfire/menu.d/*.menu`;
149 eval `/bin/cat /var/ipfire/menu.d/*.main`;
150
151 if (! blue_used() && ! orange_used()) {
152 $menu->{'05.firewall'}{'subMenu'}->{'40.dmz'}{'enabled'} = 0;
153 }
154 if (! blue_used()) {
155 $menu->{'05.firewall'}{'subMenu'}->{'30.wireless'}{'enabled'} = 0;
156 }
157 if ( $ethsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ && $ethsettings{'RED_TYPE'} eq 'STATIC' ) {
158 $menu->{'03.network'}{'subMenu'}->{'70.aliases'}{'enabled'} = 1;
159 }
160 }
161
162 sub showhttpheaders
163 {
164 print "Pragma: no-cache\n";
165 print "Cache-control: no-cache\n";
166 print "Connection: close\n";
167 print "Content-type: text/html\n\n";
168 }
169
170 sub is_menu_visible($) {
171 my $link = shift;
172 $link =~ s#\?.*$##;
173 return (-e $ENV{'DOCUMENT_ROOT'}."/../$link");
174 }
175
176
177 sub 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
206 sub 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
231 sub 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
256 sub 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
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
295 sub 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
305 sub 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;
311 $outstring =~ s/\"/&quot;/g; #" This is just a workaround for the syntax highlighter
312 $outstring =~ s/</&lt;/g;
313 $outstring =~ s/>/&gt;/g;
314 return $outstring;
315 }
316
317 sub connectionstatus
318 {
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='';
328 unless ( $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) {
329 $profileused="- $pppsettings{'PROFILENAME'}";
330 }
331
332 my ($timestr, $connstate);
333
334 my $connstate = "<span>$Lang::tr{'idle'} $profileused</span>";
335
336 if (-e "${General::swroot}/red/active") {
337 $timestr = &General::age("${General::swroot}/red/active");
338 $connstate = "<span>$Lang::tr{'connected'} - (<span>$timestr</span>) $profileused</span>";
339 } else {
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>";
344 } else {
345 $connstate = "<span>$Lang::tr{'connecting'} $profileused</span>" if (system("ps -ef | grep -q '[p]ppd'"));
346 }
347 }
348
349 return $connstate;
350 }
351
352 sub CheckSortOrder {
353 #Sorting of allocated leases
354 if ($ENV{'QUERY_STRING'} =~ /^IPADDR|^ETHER|^HOSTNAME|^ENDTIME/ ) {
355 my $newsort=$ENV{'QUERY_STRING'};
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;
365 &writehash("${swroot}/dhcp/settings", \%dhcpsettings);
366 $dhcpsettings{'ACTION'} = 'SORT'; # avoid the next test "First lauch"
367 }
368
369 }
370
371 sub 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>
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>
382 </tr>
383 END
384 ;
385
386 open(LEASES,"/var/state/dhcp/dhcpd.leases") or die "Can't open dhcpd.leases";
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) {
427 print "<form method='post' action='/cgi-bin/dhcp.cgi'>\n";
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
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' />
442 END
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 }
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>
456 END
457 ;
458 $id++;
459 }
460
461 print "</table>";
462 &closebox();
463 }
464
465
466 # This sub is used during display of actives leases
467 sub 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 }