1cb3d957be2d9120cc379daee3dcbb8980537717
[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::pagecolour = '#ffffff';
23 #$Header::tablecolour = '#a0a0a0';
24 $Header::tablecolour = '#FFFFFF';
25 $Header::bigboxcolour = '#F6F4F4';
26 $Header::boxcolour = '#EAE9EE';
27 $Header::bordercolour = '#000000';
28 $Header::table1colour = '#E0E0E0';
29 $Header::table2colour = '#F0F0F0';
30 $Header::colourred = '#993333';
31 $Header::colourorange = '#FF9933';
32 $Header::colouryellow = '#FFFF00';
33 $Header::colourgreen = '#339933';
34 $Header::colourblue = '#333399';
35 $Header::colourovpn = '#339999';
36 $Header::colourfw = '#000000';
37 $Header::colourvpn = '#990099';
38 $Header::colourerr = '#FF0000';
39 $Header::viewsize = 150;
40 $Header::errormessage = '';
41 my %menuhash = ();
42 my $menu = \%menuhash;
43 %settings = ();
44 %ethsettings = ();
45 @URI = ();
46 $Header::supported=0;
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'}:10443/$ENV{'PATH_INFO'}\r\n\r\n";
52 exit 0;
53 }
54
55 ### Initialize environment
56 &readhash("${swroot}/main/settings", \%settings);
57 &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 = "Datei firebuild nicht gefunden\n";
92 if (open(MYFile, "<${swroot}/firebuild")) {
93 $FIREBUILD = <MYFile>;
94 $FIREBUILD = "(Build: $FIREBUILD)";
95 close(_File);
96 };
97
98 require "${swroot}/langs/en.pl";
99 require "${swroot}/langs/${language}.pl";
100
101 sub orange_used () {
102 if ($ethsettings{'CONFIG_TYPE'} =~ /^[1357]$/) {
103 return 1;
104 }
105 return 0;
106 }
107
108 sub blue_used () {
109 if ($ethsettings{'CONFIG_TYPE'} =~ /^[4567]$/) {
110 return 1;
111 }
112 return 0;
113 }
114
115 sub is_modem {
116 if ($ethsettings{'CONFIG_TYPE'} =~ /^[0145]$/) {
117 return 1;
118 }
119 return 0;
120 }
121
122 ### Initialize menu
123 sub genmenu {
124 my %subsystemhash = ();
125 my $subsystem = \%subsystemhash;
126
127 $subsystem->{'10.home'} = {
128 'caption' => $tr{'alt home'},
129 'uri' => '/cgi-bin/index.cgi',
130 'title' => "$tr{'alt home'}",
131 'enabled' => 1,
132 };
133 $subsystem->{'20.passwords'} = {
134 'caption' => $tr{'sspasswords'},
135 'uri' => '/cgi-bin/changepw.cgi',
136 'title' => "$tr{'sspasswords'}",
137 'enabled' => 1,
138 };
139 $subsystem->{'30.ssh'} = {
140 'caption' => $tr{'ssh access'},
141 'uri' => '/cgi-bin/remote.cgi',
142 'title' => "$tr{'ssh access'}",
143 'enabled' => 1,
144 };
145 $subsystem->{'40.gui'} = {
146 'caption' => $tr{'gui settings'},
147 'uri' => '/cgi-bin/gui.cgi',
148 'title' => "$tr{'gui settings'}",
149 'enabled' => 1,
150 };
151 $subsystem->{'50.shutdown'} = {
152 'caption' => $tr{'shutdown'},
153 'uri' => '/cgi-bin/shutdown.cgi',
154 'title' => "$tr{'shutdown'} / $tr{'reboot'}",
155 'enabled' => 1,
156 };
157 $subsystem->{'99.credits'} = {
158 'caption' => $tr{'credits'},
159 'uri' => '/cgi-bin/credits.cgi',
160 'title' => "$tr{'credits'}",
161 'enabled' => 1,
162 };
163
164 my %substatushash = ();
165 my $substatus = \%substatushash;
166 $substatus->{'10.systemstatus'} = {
167 'caption' => $tr{'sssystem status'},
168 'uri' => '/cgi-bin/status.cgi',
169 'title' => "$tr{'sssystem status'}",
170 'enabled' => 1,
171 };
172 $substatus->{'20.networkstatus'} = {
173 'caption' => $tr{'ssnetwork status'},
174 'uri' => '/cgi-bin/netstatus.cgi',
175 'title' => "$tr{'ssnetwork status'}",
176 'enabled' => 1,
177 };
178 $substatus->{'30.systemgraphs'} = {
179 'caption' => $tr{'system graphs'},
180 'uri' => '/cgi-bin/graphs.cgi',
181 'novars' => 1,
182 'title' => "$tr{'system graphs'}",
183 'enabled' => 1,
184 };
185 $substatus->{'40.trafficgraphs'} = {
186 'caption' => $tr{'sstraffic graphs'},
187 'uri' => '/cgi-bin/graphs.cgi',
188 'vars' => 'graph=network',
189 'title' => "$tr{'sstraffic graphs'}",
190 'enabled' => 1,
191 };
192 $substatus->{'50.proxygraphs'} = {
193 'caption' => $tr{'ssproxy graphs'},
194 'uri' => '/cgi-bin/proxygraphs.cgi',
195 'title' => "$tr{'ssproxy graphs'}",
196 'enabled' => 1,
197 };
198 $substatus->{'60.fwhits'} = {
199 'caption' => 'Firewall-Diagramme',
200 'uri' => '/cgi-bin/fwhits.cgi',
201 'title' => "Firewall-Diagramme",
202 'enabled' => 1,
203 };
204 $substatus->{'70.hddtemp'} = {
205 'caption' => "$tr{'harddisk temperature graphs'}",
206 'uri' => '/cgi-bin/hddgraph.cgi',
207 'title' => "$tr{'harddisk temperature graphs'}",
208 'enabled' => 1,
209 };
210 $substatus->{'80.connections'} = {
211 'caption' => $tr{'connections'},
212 'uri' => '/cgi-bin/connections.cgi',
213 'title' => "$tr{'connections'}",
214 'enabled' => 1,
215 };
216 $substatus->{'90.nettraf'} = {
217 'caption' => $tr{'sstraffic'},
218 'uri' => '/cgi-bin/traffic.cgi',
219 'title' => "$tr{'sstraffic'}",
220 'enabled' => 1,
221 };
222 $substatus->{'99.iptable'} = {
223 'caption' => $tr{'iptable rules'},
224 'uri' => '/cgi-bin/iptables.cgi',
225 'title' => "$tr{'iptable rules'}",
226 'enabled' => 1,
227 };
228
229 my %subnetworkhash = ();
230 my $subnetwork = \%subnetworkhash;
231
232 $subnetwork->{'10.proxy'} = {'caption' => 'Webproxy',
233 'uri' => '/cgi-bin/proxy.cgi',
234 'title' => "Webproxy",
235 'enabled' => 1,
236 };
237 $subnetwork->{'20.urlfilter'} = {'caption' => 'URL-Filter',
238 'uri' => '/cgi-bin/urlfilter.cgi',
239 'title' => "URL-Filter",
240 'enabled' => 1,
241 };
242 $subnetwork->{'30.dhcp'} = {'caption' => $tr{'dhcp server'},
243 'uri' => '/cgi-bin/dhcp.cgi',
244 'title' => "$tr{'dhcp server'}",
245 'enabled' => 1,
246 };
247 $subnetwork->{'40.dialup'} = {
248 'caption' => $tr{'alt dialup'},
249 'uri' => '/cgi-bin/pppsetup.cgi',
250 'title' => "$tr{'alt dialup'}",
251 'enabled' => 1,
252 };
253 $subnetwork->{'50.hosts'} = {
254 'caption' => $tr{'edit hosts'},
255 'uri' => '/cgi-bin/hosts.cgi',
256 'title' => "$tr{'edit hosts'}",
257 'enabled' => 1,
258 };
259 $subnetwork->{'60.upload'} = {
260 'caption' => $tr{'upload'},
261 'uri' => '/cgi-bin/upload.cgi',
262 'title' => "$tr{'upload'}",
263 'enabled' => 0,
264 };
265 $subnetwork->{'70.aliases'} = {
266 'caption' => $tr{'aliases'},
267 'uri' => '/cgi-bin/aliases.cgi',
268 'title' => "$tr{'aliases'}",
269 'enabled' => 0,
270 };
271 $subnetwork->{'80.wakeonlan'} = {
272 'caption' => $tr{'WakeOnLan'},
273 'uri' => '/cgi-bin/wakeonlan.cgi',
274 'title' => "$tr{'WakeOnLan'}",
275 'enabled' => 1,
276 };
277
278 my %subserviceshash = ();
279 my $subservices = \%subserviceshash;
280
281
282 $subservices->{'10.openvpn'} = {
283 'caption' => 'OpenVPN',
284 'uri' => '/cgi-bin/ovpnmain.cgi',
285 'title' => "$tr{'virtual private networking'}",
286 'enabled' => 1,
287 };
288 $subservices->{'20.ipsec'} = {
289 'caption' => 'IPSec',
290 'uri' => '/cgi-bin/vpnmain.cgi',
291 'title' => "$tr{'virtual private networking'}",
292 'enabled' => 1,
293 };
294 $subservices->{'30.dyndns'} = {'caption' => $tr{'dynamic dns'},
295 'uri' => '/cgi-bin/ddns.cgi',
296 'title' => "$tr{'dynamic dns'}",
297 'enabled' => 1,
298 };
299 $subservices->{'40.time'} = {'caption' => $tr{'time server'},
300 'uri' => '/cgi-bin/time.cgi',
301 'title' => "$tr{'time server'}",
302 'enabled' => 1,
303 };
304 $subservices->{'50.qos'} = {'caption' => 'Quality of Service',
305 'uri' => '/cgi-bin/qos.cgi',
306 'title' => "Quality of Service",
307 'enabled' => 1,
308 };
309 $subservices->{'60.ids'} = {'caption' => $tr{'intrusion detection'},
310 'enabled' => 1,
311 'uri' => '/cgi-bin/ids.cgi',
312 'title' => "$tr{'intrusion detection system'}",
313 };
314
315
316
317 my %subfirewallhash = ();
318 my $subfirewall = \%subfirewallhash;
319
320
321 $subfirewall->{'10.dnat'} = {
322 'caption' => $tr{'ssport forwarding'},
323 'uri' => '/cgi-bin/portfw.cgi',
324 'title' => "$tr{'ssport forwarding'}",
325 'enabled' => 1,
326 };
327 $subfirewall->{'20.xtaccess'} = {
328 'caption' => $tr{'external access'},
329 'uri' => '/cgi-bin/xtaccess.cgi',
330 'title' => "$tr{'external access'}",
331 'enabled' => 1,
332 };
333 $subfirewall->{'30.wireless'} = {
334 'caption' => $tr{'blue access'},
335 'uri' => '/cgi-bin/wireless.cgi',
336 'title' => "$tr{'blue access'}",
337 'enabled' => 1,
338 };
339 $subfirewall->{'40.dmz'} = {
340 'caption' => $tr{'ssdmz pinholes'},
341 'uri' => '/cgi-bin/dmzholes.cgi',
342 'title' => "$tr{'dmz pinhole configuration'}",
343 'enabled' => 1,
344 };
345 $subfirewall->{'50.outgoing'} = {
346 'caption' => $tr{'outgoing firewall'},
347 'uri' => '/cgi-bin/outgoingfw.cgi',
348 'title' => "$tr{'outgoing firewall'}",
349 'enabled' => 1,
350 };
351 $subfirewall->{'60.fwopts'} = {
352 'caption' => $tr{'options fw'},
353 'uri' => '/cgi-bin/optionsfw.cgi',
354 'title' => "$tr{'options fw'}",
355 'enabled' => 1,
356 };
357
358 my %sublogshash = ();
359 my $sublogs = \%sublogshash;
360
361 $sublogs->{'10.summary'} = {'caption' => $tr{'log summary'},
362 'uri' => '/cgi-bin/logs.cgi/summary.dat',
363 'title' => "$tr{'log summary'}",
364 'enabled' => 1
365 };
366 $sublogs->{'20.settings'} = {'caption' => $tr{'log settings'},
367 'uri' => '/cgi-bin/logs.cgi/config.dat',
368 'title' => "$tr{'log settings'}",
369 'enabled' => 1
370 };
371 $sublogs->{'30.proxy'} = {'caption' => $tr{'proxy logs'},
372 'uri' => '/cgi-bin/logs.cgi/proxylog.dat',
373 'title' => "$tr{'proxy logs'}",
374 'enabled' => 1
375 };
376 $sublogs->{'40.firewall'} = {'caption' => $tr{'firewall logs'},
377 'uri' => '/cgi-bin/logs.cgi/firewalllog.dat',
378 'title' => "$tr{'firewall logs'}",
379 'enabled' => 1
380 };
381 $sublogs->{'50.ids'} = {'caption' => $tr{'ids logs'},
382 'uri' => '/cgi-bin/logs.cgi/ids.dat',
383 'title' => "$tr{'ids logs'}",
384 'enabled' => 1
385 };
386 $sublogs->{'60.urlfilter'} = {
387 'caption' => $tr{'urlfilter log'},
388 'uri' => '/cgi-bin/logs.cgi/urlfilter.dat',
389 'title' => "$tr{'urlfilter log'}",
390 'enabled' => 1,
391 };
392 $sublogs->{'70.openvpn'} = {'caption' => $tr{'openvpn log'},
393 'uri' => '/cgi-bin/logs.cgi/openvpn.dat',
394 'title' => "$tr{'openvpn log'}",
395 'enabled' => 1
396 };
397 $sublogs->{'80.system'} = {'caption' => $tr{'system logs'},
398 'uri' => '/cgi-bin/logs.cgi/log.dat',
399 'title' => "$tr{'system logs'}",
400 'enabled' => 1
401 };
402 $sublogs->{'90.userlog'} = {'caption' => $tr{'user proxy logs'},
403 'uri' => '/cgi-bin/logs.cgi/userlog.dat',
404 'title' => "$tr{'user log'}",
405 'enabled' => 1
406 };
407
408 my %subipfirehash = ();
409 my $subipfire = \%subipfirehash;
410 $subipfire->{'10.pakfire'} = {'caption' => 'Pakfire',
411 'uri' => '/cgi-bin/pakfire.cgi',
412 'title' => "Pakfire",
413 'enabled' => 1,
414 };
415 $subipfire->{'20.asterisk'} = {'caption' => 'Asterisk',
416 'uri' => '/cgi-bin/asterisk.cgi',
417 'title' => "Asterisk",
418 'enabled' => 1,
419 };
420 $subipfire->{'30.samba'} = {'caption' => 'Samba',
421 'uri' => '/cgi-bin/samba.cgi',
422 'title' => "Samba",
423 'enabled' => 1,
424 };
425 $subipfire->{'99.help'} = {'caption' => $tr{'help'},
426 'uri' => '/cgi-bin/help.cgi',
427 'title' => "$tr{'help'}",
428 'enabled' => 1,
429 };
430
431
432
433 $menu->{'01.system'} = {'caption' => $tr{'alt system'},
434 'enabled' => 1,
435 'subMenu' => $subsystem
436 };
437 $menu->{'02.status'} = {'caption' => $tr{'status'},
438 'enabled' => 1,
439 'subMenu' => $substatus
440 };
441 $menu->{'03.network'} = {'caption' => $tr{'network'},
442 'enabled' => 1,
443 'subMenu' => $subnetwork
444 };
445 $menu->{'04.services'} = {'caption' => $tr{'alt services'},
446 'enabled' => 1,
447 'subMenu' => $subservices
448 };
449 $menu->{'05.firewall'} = {'caption' => $tr{'firewall'},
450 'enabled' => 1,
451 'subMenu' => $subfirewall
452 };
453 $menu->{'06.proxy'} = {'caption' => $tr{'alt proxy'},
454 'enabled' => 1,
455 'subMenu' => $subproxy
456 };
457 $menu->{'07.ipfire'} = {'caption' => 'IPFire',
458 'enabled' => 1,
459 'subMenu' => $subipfire
460 };
461 $menu->{'08.logs'} = {'caption' => $tr{'alt logs'},
462 'enabled' => 1,
463 'subMenu' => $sublogs
464 };
465
466 if (! blue_used() && ! orange_used()) {
467 $menu->{'05.firewall'}{'subMenu'}->{'40.dmz'}{'enabled'} = 0;
468 }
469 if (! blue_used()) {
470 $menu->{'05.firewall'}{'subMenu'}->{'30.wireless'}{'enabled'} = 0;
471 }
472 if ( $ethsettings{'CONFIG_TYPE'} =~ /^(2|3|6|7)$/ && $ethsettings{'RED_TYPE'} eq 'STATIC' ) {
473 $menu->{'03.network'}{'subMenu'}->{'70.aliases'}{'enabled'} = 1;
474 }
475 }
476
477 sub showhttpheaders
478 {
479 print "Pragma: no-cache\n";
480 print "Cache-control: no-cache\n";
481 print "Connection: close\n";
482 print "Content-type: text/html\n\n";
483 }
484
485 sub is_menu_visible($) {
486 my $link = shift;
487 $link =~ s#\?.*$##;
488 return (-e $ENV{'DOCUMENT_ROOT'}."/../$link");
489 }
490
491
492 sub getlink($) {
493 my $root = shift;
494 if (! $root->{'enabled'}) {
495 return '';
496 }
497 if ($root->{'uri'} !~ /^$/) {
498 my $vars = '';
499 if ($root->{'vars'} !~ /^$/) {
500 $vars = '?'. $root->{'vars'};
501 }
502 if (! is_menu_visible($root->{'uri'})) {
503 return '';
504 }
505 return $root->{'uri'}.$vars;
506 }
507 my $submenus = $root->{'subMenu'};
508 if (! $submenus) {
509 return '';
510 }
511 foreach my $item (sort keys %$submenus) {
512 my $link = getlink($submenus->{$item});
513 if ($link ne '') {
514 return $link;
515 }
516 }
517 return '';
518 }
519
520
521 sub compare_url($) {
522 my $conf = shift;
523
524 my $uri = $conf->{'uri'};
525 my $vars = $conf->{'vars'};
526 my $novars = $conf->{'novars'};
527
528 if ($uri eq '') {
529 return 0;
530 }
531 if ($uri ne $URI[0]) {
532 return 0;
533 }
534 if ($novars) {
535 if ($URI[1] !~ /^$/) {
536 return 0;
537 }
538 }
539 if (! $vars) {
540 return 1;
541 }
542 return ($URI[1] eq $vars);
543 }
544
545
546 sub gettitle($) {
547 my $root = shift;
548
549 if (! $root) {
550 return '';
551 }
552 foreach my $item (sort keys %$root) {
553 my $val = $root->{$item};
554 if (compare_url($val)) {
555 $val->{'selected'} = 1;
556 if ($val->{'title'} !~ /^$/) {
557 return $val->{'title'};
558 }
559 return 'EMPTY TITLE';
560 }
561
562 my $title = gettitle($val->{'subMenu'});
563 if ($title ne '') {
564 $val->{'selected'} = 1;
565 return $title;
566 }
567 }
568 return '';
569 }
570
571
572 sub showmenu() {
573 print <<EOF
574 <div id="menu-top">
575 <ul>
576 EOF
577 ;
578 foreach my $k1 ( sort keys %$menu ) {
579 if (! $menu->{$k1}{'enabled'}) {
580 next;
581 }
582
583 my $link = getlink($menu->{$k1});
584 if ($link eq '') {
585 next;
586 }
587 if (! is_menu_visible($link)) {
588 next;
589 }
590 if ($menu->{$k1}->{'selected'}) {
591 print '<li class="selected">';
592 } else {
593 print '<li>';
594 }
595
596 print <<EOF
597 <div class="rcorner">
598 <a href="$link">$menu->{$k1}{'caption'}</a>
599 </div>
600 </li>
601 EOF
602 ;
603 }
604
605 print <<EOF
606 </ul>
607 </div>
608 EOF
609 ;
610 }
611
612 sub getselected($) {
613 my $root = shift;
614 if (!$root) {
615 return 0;
616 }
617
618 foreach my $item (%$root) {
619 if ($root->{$item}{'selected'}) {
620 return $root->{$item};
621 }
622 }
623 }
624
625 sub showsubsection($$) {
626 my $root = shift;
627 my $id = shift;
628 if ($id eq '') {
629 $id = 'menu-left';
630 }
631
632 if (! $root) {
633 return;
634 }
635 my $selected = getselected($root);
636 if (! $selected) {
637 return;
638 }
639 my $submenus = $selected->{'subMenu'};
640 if (! $submenus) {
641 return;
642 }
643
644 print <<EOF
645 <div id="$id">
646 <ul>
647 EOF
648 ;
649 foreach my $item (sort keys %$submenus) {
650 my $hash = $submenus->{$item};
651 if (! $hash->{'enabled'}) {
652 next;
653 }
654
655 my $link = getlink($hash);
656 if ($link eq '') {
657 next;
658 }
659 if (! is_menu_visible($link)) {
660 next;
661 }
662 if ($hash->{'selected'}) {
663 print '<li class="selected">';
664 } else {
665 print '<li>';
666 }
667
668 print <<EOF
669 <a href="$link">$hash->{'caption'}</a>
670 </li>
671 EOF
672 ;
673 }
674
675 print <<EOF
676 </ul>
677 </div>
678 EOF
679 ;
680
681 }
682
683
684 sub showsubsubsection($) {
685 my $root = shift;
686 if (!$root) {
687 return;
688 }
689 my $selected = getselected($root);
690 if (! $selected) {
691 return
692 }
693 if (! $selected->{'subMenu'}) {
694 return
695 }
696
697 showsubsection($selected->{'subMenu'}, 'menu-subtop');
698 }
699
700
701 sub get_helpuri() {
702 my $helpfile = '';
703 if ($URI[0] =~ /.*\/([^\/]+)\.cgi/) {
704 $helpfile = $1;
705 } else {
706 return '';
707 }
708 $helpfile .= '.help.html';
709
710 my $helpuri = '/doc/'.$language.'/'.$helpfile;
711 if (! -e $ENV{'DOCUMENT_ROOT'}.$helpuri) {
712 return '';
713 }
714 return $helpuri;
715 }
716
717
718 sub openpage {
719 my $title = shift;
720 my $boh = shift;
721 my $extrahead = shift;
722
723 @URI=split ('\?', $ENV{'REQUEST_URI'} );
724 &readhash("${swroot}/main/settings", \%settings);
725 &genmenu();
726
727 my $h2 = gettitle($menu);
728 my $helpuri = get_helpuri();
729
730 $title = "IPFire - $title";
731 if ($settings{'WINDOWWITHHOSTNAME'} eq 'on') {
732 $title = "$settings{'HOSTNAME'}.$settings{'DOMAINNAME'} - $title";
733 }
734
735 print <<END
736 <!DOCTYPE html
737 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
738 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
739
740 <html>
741 <head>
742 <title>$title</title>
743
744 $extrahead
745 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
746 END
747 ;
748 if ($settings{'FX'} eq 'on') {
749 print <<END
750 <meta http-equiv="Page-Enter" content="blendTrans(Duration=0.5,Transition=12)">
751 <meta http-equiv="Page-Exit" content="blendTrans(Duration=0.5,Transition=12)">
752 END
753 ;
754 }
755 print <<END
756 <link rel="shortcut icon" href="/favicon.ico" />
757 <style type="text/css">\@import url(/include/style.css);</style>
758 <style type="text/css">\@import url(/include/menu.css);</style>
759 <style type="text/css">\@import url(/include/content.css);</style>
760 <script language="javascript" type="text/javascript">
761
762 function swapVisibility(id) {
763 el = document.getElementById(id);
764 if(el.style.display != 'block') {
765 el.style.display = 'block'
766 }
767 else {
768 el.style.display = 'none'
769 }
770 }
771 </script>
772
773 </head>
774 <body>
775 <!-- IPFIRE HEADER -->
776
777 <div id="main">
778
779 <div id="header">
780 <img id="logo-product" src="/images/logo_ipfire.gif">
781 <div id="header-icons">
782 <a href="http://users.ipfire.eu/" target="_blank"><img border="0" src="/images/help.gif"></a>
783 </div>
784 </div>
785
786 END
787 ;
788
789 &showmenu();
790
791 print <<END
792 <div id="content">
793 <table width="90%">
794 <tr>
795 <td valign="top">
796 END
797 ;
798
799 &showsubsection($menu);
800
801 print <<END
802 <p><center><img src="/images/iptux.png" width='160px' height='160px'></center></p>
803 </td>
804 <td width="100%" valign="top">
805 <div id="page-content">
806 <h2>$h2</h2>
807 END
808 ;
809
810 &showsubsubsection($menu);
811
812 eval {
813 require 'ipfire-network.pl';
814 $supported = check_support();
815 warn_unsupported($supported);
816 };
817 }
818
819 sub closepage () {
820 my $status = &connectionstatus();
821 $uptime = `/usr/bin/uptime`;
822
823 print <<END
824 <div align="center">
825 <p>
826 <div style="font-size: 9px"><b>Status:</b> $status <b>Uptime:</b>$uptime <b>Version:</b> $FIREBUILD</div>
827 </p>
828 </div>
829 </body>
830 </html>
831 END
832 ;
833 }
834
835 sub openbigbox
836 {
837 my $width = $_[0];
838 my $align = $_[1];
839 my $sideimg = $_[2];
840
841 if ($errormessage) {
842 $bgcolor = "style='background-color: $colourerr;'";
843 } else {
844 $bgcolor = '';
845 }
846 }
847
848 sub closebigbox
849 {
850 # print "</td></tr></table></td></tr></table>\n"
851 }
852
853 sub openbox
854 {
855 $width = $_[0];
856 $align = $_[1];
857 $caption = $_[2];
858
859 if ($caption) { print "<h3>$caption</h3>\n"; } else { print "&nbsp;"; }
860
861 print "<table class=\"list\"><tr><td align=\"$align\">\n";
862 }
863
864 sub closebox
865 {
866 print "</td></tr></table><br><br>";
867 }
868
869 sub writehash
870 {
871 my $filename = $_[0];
872 my $hash = $_[1];
873
874 # write cgi vars to the file.
875 open(FILE, ">${filename}") or die "Unable to write file $filename";
876 flock FILE, 2;
877 foreach $var (keys %$hash)
878 {
879 $val = $hash->{$var};
880 # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
881 # location of the mouse are submitted as well, this was being written to the settings file causing
882 # some serious grief! This skips the variable.x and variable.y
883 if (!($var =~ /(.x|.y)$/)) {
884 if ($val =~ / /) {
885 $val = "\'$val\'"; }
886 if (!($var =~ /^ACTION/)) {
887 print FILE "${var}=${val}\n"; }
888 }
889 }
890 close FILE;
891 }
892
893 sub readhash
894 {
895 my $filename = $_[0];
896 my $hash = $_[1];
897 my ($var, $val);
898
899 open(FILE, $filename) or die "Unable to read file $filename";
900
901 while (<FILE>)
902 {
903 chop;
904 ($var, $val) = split /=/, $_, 2;
905 if ($var)
906 {
907 $val =~ s/^\'//g;
908 $val =~ s/\'$//g;
909
910 # Untaint variables read from hash
911 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
912 $val =~ /([\w\W]*)/; $val = $1;
913 $hash->{$var} = $val;
914 }
915 }
916 close FILE;
917 }
918
919 sub getcgihash {
920 my ($hash, $params) = @_;
921 my $cgi = CGI->new ();
922 $hash->{'__CGI__'} = $cgi;
923 return if ($ENV{'REQUEST_METHOD'} ne 'POST');
924 if (!$params->{'wantfile'}) {
925 $CGI::DISABLE_UPLOADS = 1;
926 $CGI::POST_MAX = 512 * 1024;
927 } else {
928 $CGI::POST_MAX = 10 * 1024 * 1024;
929 }
930
931 $cgi->referer() =~ m/^https?\:\/\/([^\/]+)/;
932 my $referer = $1;
933 $cgi->url() =~ m/^https?\:\/\/([^\/]+)/;
934 my $servername = $1;
935 return if ($referer ne $servername);
936
937 ### Modified for getting multi-vars, split by |
938 %temp = $cgi->Vars();
939 foreach my $key (keys %temp) {
940 $hash->{$key} = $temp{$key};
941 $hash->{$key} =~ s/\0/|/g;
942 $hash->{$key} =~ s/^\s*(.*?)\s*$/$1/;
943 }
944
945 if (($params->{'wantfile'})&&($params->{'filevar'})) {
946 $hash->{$params->{'filevar'}} = $cgi->upload
947 ($params->{'filevar'});
948 }
949 return;
950 }
951
952 sub log
953 {
954 my $logmessage = $_[0];
955 $logmessage =~ /([\w\W]*)/;
956 $logmessage = $1;
957 system('/usr/bin/logger', '-t', 'ipfire', $logmessage);
958 }
959
960 sub age
961 {
962 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
963 $atime, $mtime, $ctime, $blksize, $blocks) = stat $_[0];
964 my $now = time;
965
966 my $totalsecs = $now - $mtime;
967 my $days = int($totalsecs / 86400);
968 my $totalhours = int($totalsecs / 3600);
969 my $hours = $totalhours % 24;
970 my $totalmins = int($totalsecs / 60);
971 my $mins = $totalmins % 60;
972 my $secs = $totalsecs % 60;
973
974 return "${days}d ${hours}h ${mins}m ${secs}s";
975 }
976
977 sub validip
978 {
979 my $ip = $_[0];
980
981 if (!($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)) {
982 return 0; }
983 else
984 {
985 @octets = ($1, $2, $3, $4);
986 foreach $_ (@octets)
987 {
988 if (/^0./) {
989 return 0; }
990 if ($_ < 0 || $_ > 255) {
991 return 0; }
992 }
993 return 1;
994 }
995 }
996
997 sub validmask
998 {
999 my $mask = $_[0];
1000
1001 # secord part an ip?
1002 if (&validip($mask)) {
1003 return 1; }
1004 # second part a number?
1005 if (/^0/) {
1006 return 0; }
1007 if (!($mask =~ /^\d+$/)) {
1008 return 0; }
1009 if ($mask >= 0 && $mask <= 32) {
1010 return 1; }
1011 return 0;
1012 }
1013
1014 sub validipormask
1015 {
1016 my $ipormask = $_[0];
1017
1018 # see if it is a IP only.
1019 if (&validip($ipormask)) {
1020 return 1; }
1021 # split it into number and mask.
1022 if (!($ipormask =~ /^(.*?)\/(.*?)$/)) {
1023 return 0; }
1024 $ip = $1;
1025 $mask = $2;
1026 # first part not a ip?
1027 if (!(&validip($ip))) {
1028 return 0; }
1029 return &validmask($mask);
1030 }
1031
1032 sub validipandmask
1033 {
1034 my $ipandmask = $_[0];
1035
1036 # split it into number and mask.
1037 if (!($ipandmask =~ /^(.*?)\/(.*?)$/)) {
1038 return 0; }
1039 $ip = $1;
1040 $mask = $2;
1041 # first part not a ip?
1042 if (!(&validip($ip))) {
1043 return 0; }
1044 return &validmask($mask);
1045 }
1046
1047 sub validport
1048 {
1049 $_ = $_[0];
1050
1051 if (!/^\d+$/) {
1052 return 0; }
1053 if (/^0./) {
1054 return 0; }
1055 if ($_ >= 1 && $_ <= 65535) {
1056 return 1; }
1057 return 0;
1058 }
1059
1060 sub validmac
1061 {
1062 my $checkmac = $_[0];
1063 my $ot = '[0-9a-f]{2}'; # 2 Hex digits (one octet)
1064 if ($checkmac !~ /^$ot:$ot:$ot:$ot:$ot:$ot$/i)
1065 {
1066 return 0;
1067 }
1068 return 1;
1069 }
1070
1071 sub validhostname
1072 {
1073 # Checks a hostname against RFC1035
1074 my $hostname = $_[0];
1075
1076 # Each part should be at least two characters in length
1077 # but no more than 63 characters
1078 if (length ($hostname) < 2 || length ($hostname) > 63) {
1079 return 0;}
1080 # Only valid characters are a-z, A-Z, 0-9 and -
1081 if ($hostname !~ /^[a-zA-Z0-9-]*$/) {
1082 return 0;}
1083 # First character can only be a letter or a digit
1084 if (substr ($hostname, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
1085 return 0;}
1086 # Last character can only be a letter or a digit
1087 if (substr ($hostname, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
1088 return 0;}
1089 return 1;
1090 }
1091
1092 sub validdomainname
1093 {
1094 # Checks a domain name against RFC1035
1095 my $domainname = $_[0];
1096 my @parts = split (/\./, $domainname); # Split hostname at the '.'
1097
1098 foreach $part (@parts) {
1099 # Each part should be at least two characters in length
1100 # but no more than 63 characters
1101 if (length ($part) < 2 || length ($part) > 63) {
1102 return 0;}
1103 # Only valid characters are a-z, A-Z, 0-9 and -
1104 if ($part !~ /^[a-zA-Z0-9-]*$/) {
1105 return 0;}
1106 # First character can only be a letter or a digit
1107 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
1108 return 0;}
1109 # Last character can only be a letter or a digit
1110 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
1111 return 0;}
1112 }
1113 return 1;
1114 }
1115
1116 sub validfqdn
1117 {
1118 # Checks a fully qualified domain name against RFC1035
1119 my $fqdn = $_[0];
1120 my @parts = split (/\./, $fqdn); # Split hostname at the '.'
1121 if (scalar(@parts) < 2) { # At least two parts should
1122 return 0;} # exist in a FQDN
1123 # (i.e. hostname.domain)
1124 foreach $part (@parts) {
1125 # Each part should be at least two characters in length
1126 # but no more than 63 characters
1127 if (length ($part) < 2 || length ($part) > 63) {
1128 return 0;}
1129 # Only valid characters are a-z, A-Z, 0-9 and -
1130 if ($part !~ /^[a-zA-Z0-9-]*$/) {
1131 return 0;}
1132 # First character can only be a letter or a digit
1133 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
1134 return 0;}
1135 # Last character can only be a letter or a digit
1136 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
1137 return 0;}
1138 }
1139 return 1;
1140 }
1141
1142 sub validportrange # used to check a port range
1143 {
1144 my $port = $_[0]; # port values
1145 $port =~ tr/-/:/; # replace all - with colons just in case someone used -
1146 my $srcdst = $_[1]; # is it a source or destination port
1147
1148 if (!($port =~ /^(\d+)\:(\d+)$/)) {
1149
1150 if (!(&validport($port))) {
1151 if ($srcdst eq 'src'){
1152 return $tr{'source port numbers'};
1153 } else {
1154 return $tr{'destination port numbers'};
1155 }
1156 }
1157 }
1158 else
1159 {
1160 @ports = ($1, $2);
1161 if ($1 >= $2){
1162 if ($srcdst eq 'src'){
1163 return $tr{'bad source range'};
1164 } else {
1165 return $tr{'bad destination range'};
1166 }
1167 }
1168 foreach $_ (@ports)
1169 {
1170 if (!(&validport($_))) {
1171 if ($srcdst eq 'src'){
1172 return $tr{'source port numbers'};
1173 } else {
1174 return $tr{'destination port numbers'};
1175 }
1176 }
1177 }
1178 return;
1179 }
1180 }
1181
1182 # Test if IP is within a subnet
1183 # Call: IpInSubnet (Addr, Subnet, Subnet Mask)
1184 # Subnet can be an IP of the subnet: 10.0.0.0 or 10.0.0.1
1185 # Everything in dottted notation
1186 # Return: TRUE/FALSE
1187 sub IpInSubnet
1188 {
1189 $ip = unpack('N', inet_aton(shift));
1190 $start = unpack('N', inet_aton(shift));
1191 $mask = unpack('N', inet_aton(shift));
1192 $start &= $mask; # base of subnet...
1193 $end = $start + ~$mask;
1194 return (($ip >= $start) && ($ip <= $end));
1195 }
1196
1197 sub validemail {
1198 my $mail = shift;
1199 return 0 if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/ );
1200 return 0 if ( $mail =~ /^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/);
1201 return 0 if ( $mail !~ /([0-9a-zA-Z]{1})\@./ );
1202 return 0 if ( $mail !~ /.\@([0-9a-zA-Z]{1})/ );
1203 return 0 if ( $mail =~ /.\.\-.|.\-\..|.\.\..|.\-\-./g );
1204 return 0 if ( $mail =~ /.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./g );
1205 return 0 if ( $mail !~ /\.([a-zA-Z]{2,3})$/ );
1206 return 1;
1207 }
1208
1209 sub readhasharray {
1210 my ($filename, $hash) = @_;
1211
1212 open(FILE, $filename) or die "Unable to read file $filename";
1213
1214 while (<FILE>) {
1215 my ($key, $rest, @temp);
1216 chomp;
1217 ($key, $rest) = split (/,/, $_, 2);
1218 if ($key =~ /^[0-9]+$/ && $rest) {
1219 @temp = split (/,/, $rest);
1220 $hash->{$key} = \@temp;
1221 }
1222 }
1223 close FILE;
1224 return;
1225 }
1226
1227 sub writehasharray {
1228 my ($filename, $hash) = @_;
1229 my ($key, @temp);
1230
1231 open(FILE, ">$filename") or die "Unable to write to file $filename";
1232
1233 foreach $key (keys %$hash) {
1234 if ( $hash->{$key} ) {
1235 print FILE "$key";
1236 foreach $i (0 .. $#{$hash->{$key}}) {
1237 print FILE ",$hash->{$key}[$i]";
1238 }
1239 }
1240 print FILE "\n";
1241 }
1242 close FILE;
1243 return;
1244 }
1245
1246 sub findhasharraykey {
1247 foreach my $i (1 .. 1000000) {
1248 if ( ! exists $_[0]{$i}) {
1249 return $i;
1250 }
1251 }
1252 }
1253
1254 sub cleanhtml
1255 {
1256 my $outstring =$_[0];
1257 $outstring =~ tr/,/ / if not defined $_[1] or $_[1] ne 'y';
1258 $outstring =~ s/&/&amp;/g;
1259 $outstring =~ s/\'/&#039;/g;
1260 $outstring =~ s/\"/&quot;/g;
1261 $outstring =~ s/</&lt;/g;
1262 $outstring =~ s/>/&gt;/g;
1263 return $outstring;
1264 }
1265
1266 sub connectionstatus
1267 {
1268 my %pppsettings = ();
1269 my %netsettings = ();
1270 my $iface='';
1271
1272 $pppsettings{'PROFILENAME'} = 'None';
1273 &General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
1274 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
1275
1276 my $profileused='';
1277 if ( ! ( $netsettings{'CONFIG_TYPE'} =~ /^(2|3|6|7)$/ && $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) ) {
1278 $profileused="- $pppsettings{'PROFILENAME'}";
1279 }
1280
1281 if ( ( $pppsettings{'METHOD'} eq 'DHCP' && $netsettings{'RED_TYPE'} ne 'PPTP')
1282 || $netsettings{'RED_TYPE'} eq 'DHCP' ) {
1283 if (open(IFACE, "${General::swroot}/red/iface")) {
1284 $iface = <IFACE>;
1285 close IFACE;
1286 chomp ($iface);
1287 $iface =~ /([a-zA-Z0-9]*)/; $iface = $1;
1288 }
1289 }
1290
1291 my ($timestr, $connstate);
1292 if ($netsettings{'CONFIG_TYPE'} =~ /^(0|1|4|5)$/ && $pppsettings{'TYPE'} =~ /^isdn/) {
1293 # Count ISDN channels
1294 my ($idmap, $chmap, $drmap, $usage, $flags, $phone);
1295 my @phonenumbers;
1296 my $count=0;
1297
1298 open (FILE, "/dev/isdninfo");
1299
1300 $idmap = <FILE>; chop $idmap;
1301 $chmap = <FILE>; chop $chmap;
1302 $drmap = <FILE>; chop $drmap;
1303 $usage = <FILE>; chop $usage;
1304 $flags = <FILE>; chop $flags;
1305 $phone = <FILE>; chop $phone;
1306
1307 $phone =~ s/^phone(\s*):(\s*)//;
1308
1309 @phonenumbers = split / /, $phone;
1310
1311 foreach (@phonenumbers) {
1312 if ($_ ne '???') {
1313 $count++;
1314 }
1315 }
1316 close (FILE);
1317
1318 ## Connection status
1319 my $number;
1320 if ($count == 0) {
1321 $number = 'none!';
1322 } elsif ($count == 1) {
1323 $number = 'single';
1324 } else {
1325 $number = 'dual';
1326 }
1327
1328 if (-e "${General::swroot}/red/active") {
1329 $timestr = &General::age("${General::swroot}/red/active");
1330 $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'connected'} - $number channel (<span class='ipcop_StatusBigRed'>$timestr</span>) $profileused</span>";
1331 } else {
1332 if ($count == 0) {
1333 if (-e "${General::swroot}/red/dial-on-demand") {
1334 $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'dod waiting'} $profileused</span>";
1335 } else {
1336 $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'idle'} $profileused</span>";
1337 }
1338 } else {
1339 $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'connecting'} $profileused</span>";
1340 }
1341 }
1342 } elsif ($netsettings{'RED_TYPE'} eq "STATIC" || $pppsettings {'METHOD'} eq 'STATIC') {
1343 if (-e "${General::swroot}/red/active") {
1344 $timestr = &General::age("${General::swroot}/red/active");
1345 $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'connected'} (<span class='ipcop_StatusBigRed'>$timestr</span>) $profileused</span>";
1346 } else {
1347 $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'idle'} $profileused</span>";
1348 }
1349 } elsif ( ( (-e "${General::swroot}/dhcpc/dhcpcd-$iface.pid") && $netsettings{'RED_TYPE'} ne 'PPTP' ) ||
1350 !system("/bin/ps -ef | /bin/grep -q '[p]ppd'") || !system("/bin/ps -ef | /bin/grep -q '[c]onnectioncheck'")) {
1351 if (-e "${General::swroot}/red/active") {
1352 $timestr = &General::age("${General::swroot}/red/active");
1353 if ($pppsettings{'TYPE'} =~ /^(modem|bewanadsl|conexantpciadsl|eagleusbadsl)$/) {
1354 my $speed;
1355 if ($pppsettings{'TYPE'} eq 'modem') {
1356 open(CONNECTLOG, "/var/log/connect.log");
1357 while (<CONNECTLOG>) {
1358 if (/CONNECT/) {
1359 $speed = (split / /)[6];
1360 }
1361 }
1362 close (CONNECTLOG);
1363 } elsif ($pppsettings{'TYPE'} eq 'bewanadsl') {
1364 $speed = `/usr/bin/unicorn_status | /bin/grep Rate | /usr/bin/cut -f2 -d ':'`;
1365 } elsif ($pppsettings{'TYPE'} eq 'conexantpciadsl') {
1366 $speed = `/bin/cat /proc/net/atm/CnxAdsl:* | /bin/grep 'Line Rates' | /bin/sed -e 's+Line Rates: Receive+Rx+' -e 's+Transmit+Tx+'`;
1367 } elsif ($pppsettings{'TYPE'} eq 'eagleusbadsl') {
1368 $speed = `/usr/sbin/eaglestat | /bin/grep Rate`;
1369 }
1370 $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'connected'} (<span class='ipcop_StatusBigRed'>$timestr</span>) $profileused (\@$speed)</span>";
1371 } else {
1372 $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'connected'} (<span class='ipcop_StatusBigRed'>$timestr</span>) $profileused</span>";
1373 }
1374 } else {
1375 if (-e "${General::swroot}/red/dial-on-demand") {
1376 $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'dod waiting'} $profileused</span>";
1377 } else {
1378 $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'connecting'} $profileused</span>";
1379 }
1380 }
1381 } else {
1382 $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'idle'} $profileused</span>";
1383 }
1384 return $connstate;
1385 }
1386
1387 sub srtarray
1388 # Darren Critchley - darrenc@telus.net - (c) 2003
1389 # &srtarray(SortOrder, AlphaNumeric, SortDirection, ArrayToBeSorted)
1390 # This subroutine will take the following parameters:
1391 # ColumnNumber = the column which you want to sort on, starts at 1
1392 # AlphaNumberic = a or n (lowercase) defines whether the sort should be alpha or numberic
1393 # SortDirection = asc or dsc (lowercase) Ascending or Descending sort
1394 # ArrayToBeSorted = the array that wants sorting
1395 #
1396 # Returns an array that is sorted to your specs
1397 #
1398 # If SortOrder is greater than the elements in array, then it defaults to the first element
1399 #
1400 {
1401 my ($colno, $alpnum, $srtdir, @tobesorted) = @_;
1402 my @tmparray;
1403 my @srtedarray;
1404 my $line;
1405 my $newline;
1406 my $ttlitems = scalar @tobesorted; # want to know the number of rows in the passed array
1407 if ($ttlitems < 1){ # if no items, don't waste our time lets leave
1408 return (@tobesorted);
1409 }
1410 my @tmp = split(/\,/,$tobesorted[0]);
1411 $ttlitems = scalar @tmp; # this should be the number of elements in each row of the passed in array
1412
1413 # Darren Critchley - validate parameters
1414 if ($colno > $ttlitems){$colno = '1';}
1415 $colno--; # remove one from colno to deal with arrays starting at 0
1416 if($colno < 0){$colno = '0';}
1417 if ($alpnum ne '') { $alpnum = lc($alpnum); } else { $alpnum = 'a'; }
1418 if ($srtdir ne '') { $srtdir = lc($srtdir); } else { $srtdir = 'src'; }
1419
1420 foreach $line (@tobesorted)
1421 {
1422 chomp($line);
1423 if ($line ne '') {
1424 my @temp = split(/\,/,$line);
1425 # Darren Critchley - juggle the fields so that the one we want to sort on is first
1426 my $tmpholder = $temp[0];
1427 $temp[0] = $temp[$colno];
1428 $temp[$colno] = $tmpholder;
1429 $newline = "";
1430 for ($ctr=0; $ctr < $ttlitems ; $ctr++) {
1431 $newline=$newline . $temp[$ctr] . ",";
1432 }
1433 chop($newline);
1434 push(@tmparray,$newline);
1435 }
1436 }
1437 if ($alpnum eq 'n') {
1438 @tmparray = sort {$a <=> $b} @tmparray;
1439 } else {
1440 @tmparray = (sort @tmparray);
1441 }
1442 foreach $line (@tmparray)
1443 {
1444 chomp($line);
1445 if ($line ne '') {
1446 my @temp = split(/\,/,$line);
1447 my $tmpholder = $temp[0];
1448 $temp[0] = $temp[$colno];
1449 $temp[$colno] = $tmpholder;
1450 $newline = "";
1451 for ($ctr=0; $ctr < $ttlitems ; $ctr++){
1452 $newline=$newline . $temp[$ctr] . ",";
1453 }
1454 chop($newline);
1455 push(@srtedarray,$newline);
1456 }
1457 }
1458
1459 if ($srtdir eq 'dsc') {
1460 @tmparray = reverse(@srtedarray);
1461 return (@tmparray);
1462 } else {
1463 return (@srtedarray);
1464 }
1465 }
1466
1467 sub speedtouchversion
1468 {
1469 if (-f "/proc/bus/usb/devices")
1470 {
1471 $speedtouch=`/bin/cat /proc/bus/usb/devices | /bin/grep 'Vendor=06b9 ProdID=4061' | /usr/bin/cut -d ' ' -f6`;
1472 if ($speedtouch eq '') {
1473 $speedtouch= $tr{'connect the modem'};
1474 }
1475 } else {
1476 $speedtouch='USB '.$tr{'not running'};
1477 }
1478 return $speedtouch
1479 }
1480
1481 sub CheckSortOrder {
1482 #Sorting of allocated leases
1483 if ($ENV{'QUERY_STRING'} =~ /^IPADDR|^ETHER|^HOSTNAME|^ENDTIME/ ) {
1484 my $newsort=$ENV{'QUERY_STRING'};
1485 &readhash("${swroot}/dhcp/settings", \%dhcpsettings);
1486 $act=$dhcpsettings{'SORT_LEASELIST'};
1487 #Reverse actual ?
1488 if ($act =~ $newsort) {
1489 if ($act !~ 'Rev') {$Rev='Rev'};
1490 $newsort.=$Rev
1491 };
1492
1493 $dhcpsettings{'SORT_LEASELIST'}=$newsort;
1494 &writehash("${swroot}/dhcp/settings", \%dhcpsettings);
1495 $dhcpsettings{'ACTION'} = 'SORT'; # avoid the next test "First lauch"
1496 }
1497
1498 }
1499
1500 sub PrintActualLeases
1501 {
1502 &openbox('100%', 'left', $tr{'current dynamic leases'});
1503 print <<END
1504 <table width='100%'>
1505 <tr>
1506 <td width='25%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IPADDR'><b>$tr{'ip address'}</b></a></td>
1507 <td width='25%' align='center'><a href='$ENV{'SCRIPT_NAME'}?ETHER'><b>$tr{'mac address'}</b></a></td>
1508 <td width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?HOSTNAME'><b>$tr{'hostname'}</b></a></td>
1509 <td width='30%' align='center'><a href='$ENV{'SCRIPT_NAME'}?ENDTIME'><b>$tr{'lease expires'} (local time d/m/y)</b></a></td>
1510 </tr>
1511 END
1512 ;
1513
1514 open(LEASES,"/var/state/dhcp/dhcpd.leases") or die "Can't open dhcpd.leases";
1515 while ($line = <LEASES>) {
1516 next if( $line =~ /^\s*#/ );
1517 chomp($line);
1518 @temp = split (' ', $line);
1519
1520 if ($line =~ /^\s*lease/) {
1521 $ip = $temp[1];
1522 #All field are not necessarily read. Clear everything
1523 $endtime = 0;
1524 $ether = "";
1525 $hostname = "";
1526 }
1527
1528 if ($line =~ /^\s*ends/) {
1529 $line =~ /(\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)/;
1530 $endtime = timegm($6, $5, $4, $3, $2 - 1, $1 - 1900);
1531 }
1532
1533 if ($line =~ /^\s*hardware ethernet/) {
1534 $ether = $temp[2];
1535 $ether =~ s/;//g;
1536 }
1537
1538 if ($line =~ /^\s*client-hostname/) {
1539 $hostname = "$temp[1] $temp[2] $temp[3]";
1540 $hostname =~ s/;//g;
1541 $hostname =~ s/\"//g;
1542 }
1543
1544 if ($line eq "}") {
1545 @record = ('IPADDR',$ip,'ENDTIME',$endtime,'ETHER',$ether,'HOSTNAME',$hostname);
1546 $record = {}; # create a reference to empty hash
1547 %{$record} = @record; # populate that hash with @record
1548 $entries{$record->{'IPADDR'}} = $record; # add this to a hash of hashes
1549 }
1550 }
1551 close(LEASES);
1552
1553 my $id = 0;
1554 foreach my $key (sort leasesort keys %entries) {
1555
1556 my $hostname = &cleanhtml($entries{$key}->{HOSTNAME},"y");
1557
1558 if ($id % 2) {
1559 print "<tr bgcolor='$table1colour'>";
1560 }
1561 else {
1562 print "<tr bgcolor='$table2colour'>";
1563 }
1564
1565 print <<END
1566 <td align='center'>$entries{$key}->{IPADDR}</td>
1567 <td align='center'>$entries{$key}->{ETHER}</td>
1568 <td align='center'>&nbsp;$hostname </td>
1569 <td align='center'>
1570 END
1571 ;
1572
1573 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $dst) = localtime ($entries{$key}->{ENDTIME});
1574 $enddate = sprintf ("%02d/%02d/%d %02d:%02d:%02d",$mday,$mon+1,$year+1900,$hour,$min,$sec);
1575
1576 if ($entries{$key}->{ENDTIME} < time() ){
1577 print "<strike>$enddate</strike>";
1578 } else {
1579 print "$enddate";
1580 }
1581 print "</td></tr>";
1582 $id++;
1583 }
1584
1585 print "</table>";
1586 &closebox();
1587 }
1588
1589
1590 # This sub is used during display of actives leases
1591 sub leasesort {
1592 if (rindex ($dhcpsettings{'SORT_LEASELIST'},'Rev') != -1)
1593 {
1594 $qs=substr ($dhcpsettings{'SORT_LEASELIST'},0,length($dhcpsettings{'SORT_LEASELIST'})-3);
1595 if ($qs eq 'IPADDR') {
1596 @a = split(/\./,$entries{$a}->{$qs});
1597 @b = split(/\./,$entries{$b}->{$qs});
1598 ($b[0]<=>$a[0]) ||
1599 ($b[1]<=>$a[1]) ||
1600 ($b[2]<=>$a[2]) ||
1601 ($b[3]<=>$a[3]);
1602 }else {
1603 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
1604 }
1605 }
1606 else #not reverse
1607 {
1608 $qs=$dhcpsettings{'SORT_LEASELIST'};
1609 if ($qs eq 'IPADDR') {
1610 @a = split(/\./,$entries{$a}->{$qs});
1611 @b = split(/\./,$entries{$b}->{$qs});
1612 ($a[0]<=>$b[0]) ||
1613 ($a[1]<=>$b[1]) ||
1614 ($a[2]<=>$b[2]) ||
1615 ($a[3]<=>$b[3]);
1616 }else {
1617 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
1618 }
1619 }
1620 }
1621
1622 sub get_uplinks() {
1623 my @uplinks = ();
1624 opendir(DIR, "${swroot}/uplinks/") || return \@uplinks;
1625 foreach my $dir (readdir(DIR)) {
1626 next if ($dir =~ /^\./);
1627 next if (-f "${swroot}/uplinks/$dir");
1628 push(@uplinks, $dir);
1629 }
1630 closedir(DIR);
1631 return \@uplinks;
1632 }
1633
1634 sub get_iface($) {
1635 my $filename = shift;
1636 chomp($filename);
1637 open (F, $filename) || return "";
1638 my $iface = <F>;
1639 close(F);
1640 chomp($iface);
1641 return $iface;
1642 }
1643
1644 sub get_red_ifaces_by_type($) {
1645 my $type=shift;
1646 my @gottypeiface = ();
1647 my @gottypeuplink = ();
1648 my @gottype = ();
1649
1650 my $ref=get_uplinks();
1651 my @uplinks=@$ref;
1652 my %set = ();
1653 foreach my $link (@uplinks) {
1654 eval {
1655 &readhash("${swroot}/uplinks/$link/settings", \%set);
1656 };
1657 push(@gottype, $link);
1658
1659 my $iface = $set{'RED_DEV'};
1660 if (!$iface) {
1661 $iface = get_iface("${swroot}/uplinks/$link/interface");
1662 }
1663 next if (!$iface);
1664
1665 if ($set{'RED_TYPE'} eq $type) {
1666 push(@gottypeiface, $iface);
1667 push(@gottypeuplink, $link);
1668 }
1669 }
1670 return (\@gottypeiface, \@gottypeuplink, \@gottype);
1671 }
1672
1673 sub get_red_ifaces() {
1674 return `cat ${swroot}/uplinks/*/interface 2>/dev/null`;
1675 }
1676
1677 sub get_zone_devices($) {
1678 my $bridge = shift;
1679 my @ifaces = ();
1680 open (FILE, "${swroot}/ethernet/$bridge") || return "";
1681 foreach my $line (<FILE>) {
1682 chomp($line);
1683 next if (!$line);
1684 push(@ifaces, $line);
1685 }
1686 close(FILE);
1687 return \@ifaces;
1688 }