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