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