]>
Commit | Line | Data |
---|---|---|
3ea75603 MT |
1 | # SmoothWall CGIs |
2 | # | |
3 | # This code is distributed under the terms of the GPL | |
4 | # | |
5 | # (c) The SmoothWall Team | |
6 | # Copyright (C) 2002 Alex Hudson - getcgihash() rewrite | |
7 | # Copyright (C) 2002 Bob Grant <bob@cache.ucr.edu> - validmac() | |
8 | # Copyright (c) 2002/04/13 Steve Bootes - add alias section, helper functions | |
9 | # Copyright (c) 2002/08/23 Mark Wormgoor <mark@wormgoor.com> validfqdn() | |
10 | # Copyright (c) 2003/09/11 Darren Critchley <darrenc@telus.net> srtarray() | |
11 | # | |
3ea75603 MT |
12 | package Header; |
13 | ||
14 | use CGI(); | |
15 | use Socket; | |
16 | use Time::Local; | |
17 | ||
18 | $|=1; # line buffering | |
19 | ||
3ea75603 MT |
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'; | |
6d4147d2 | 35 | $Header::colourovpn = '#339999'; |
3ea75603 MT |
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 = (); | |
3ea75603 MT |
46 | |
47 | ### Make sure this is an SSL request | |
48 | if ($ENV{'SERVER_ADDR'} && $ENV{'HTTPS'} ne 'on') { | |
49 | print "Status: 302 Moved\r\n"; | |
f8e080ef | 50 | print "Location: https://$ENV{'SERVER_ADDR'}:444/$ENV{'PATH_INFO'}\r\n\r\n"; |
3ea75603 MT |
51 | exit 0; |
52 | } | |
53 | ||
54 | ### Initialize environment | |
55 | &readhash("${swroot}/main/settings", \%settings); | |
56 | &readhash("${swroot}/ethernet/settings", \%ethsettings); | |
57 | $language = $settings{'LANGUAGE'}; | |
58 | $hostname = $settings{'HOSTNAME'}; | |
59 | $hostnameintitle = 0; | |
60 | ||
61 | ### Initialize language | |
62 | if ($language =~ /^(\w+)$/) {$language = $1;} | |
63 | ||
64 | ### Read English Files | |
65 | if ( -d "/var/ipfire/langs/en/" ) { | |
66 | opendir(DIR, "/var/ipfire/langs/en/"); | |
67 | @names = readdir(DIR) or die "Cannot Read Directory: $!\n"; | |
68 | foreach $name(@names) { | |
69 | next if ($name eq "."); | |
70 | next if ($name eq ".."); | |
71 | next if (!($name =~ /\.pl$/)); | |
72 | require "${swroot}/langs/en/${name}"; | |
73 | }; | |
74 | }; | |
75 | ||
76 | ||
77 | ### Enable Language Files | |
78 | if ( -d "/var/ipfire/langs/${language}/" ) { | |
79 | opendir(DIR, "/var/ipfire/langs/${language}/"); | |
80 | @names = readdir(DIR) or die "Cannot Read Directory: $!\n"; | |
81 | foreach $name(@names) { | |
82 | next if ($name eq "."); | |
83 | next if ($name eq ".."); | |
84 | next if (!($name =~ /\.pl$/)); | |
85 | require "${swroot}/langs/${language}/${name}"; | |
86 | }; | |
87 | }; | |
88 | ||
7476f476 | 89 | ### Read IPFire Buildversion |
a7fb5630 | 90 | $FIREBUILD = "File not found: firebuild\n"; |
7476f476 HS |
91 | if (open(MYFile, "<${swroot}/firebuild")) { |
92 | $FIREBUILD = <MYFile>; | |
a7fb5630 | 93 | chomp($FIREBUILD); |
7476f476 | 94 | $FIREBUILD = "(Build: $FIREBUILD)"; |
a7fb5630 | 95 | close(MYFile); |
7476f476 | 96 | }; |
3ea75603 MT |
97 | |
98 | require "${swroot}/langs/en.pl"; | |
99 | require "${swroot}/langs/${language}.pl"; | |
f8e080ef | 100 | eval `/bin/cat /srv/web/ipfire/html/themes/$settings{'THEME'}/include/functions.pl`; |
3ea75603 MT |
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 { | |
24249567 | 125 | |
3ea75603 MT |
126 | my %subsystemhash = (); |
127 | my $subsystem = \%subsystemhash; | |
128 | ||
3ea75603 MT |
129 | my %substatushash = (); |
130 | my $substatus = \%substatushash; | |
3ea75603 MT |
131 | |
132 | my %subnetworkhash = (); | |
133 | my $subnetwork = \%subnetworkhash; | |
134 | ||
3ea75603 MT |
135 | my %subserviceshash = (); |
136 | my $subservices = \%subserviceshash; | |
137 | ||
3ea75603 MT |
138 | my %subfirewallhash = (); |
139 | my $subfirewall = \%subfirewallhash; | |
140 | ||
3ea75603 MT |
141 | my %subipfirehash = (); |
142 | my $subipfire = \%subipfirehash; | |
3ea75603 | 143 | |
24249567 MT |
144 | my %sublogshash = (); |
145 | my $sublogs = \%sublogshash; | |
3ea75603 | 146 | |
24249567 MT |
147 | eval `/bin/cat /var/ipfire/menu.d/*.menu`; |
148 | eval `/bin/cat /var/ipfire/menu.d/*.main`; | |
3ea75603 MT |
149 | |
150 | if (! blue_used() && ! orange_used()) { | |
a68fedca | 151 | $menu->{'05.firewall'}{'subMenu'}->{'40.dmz'}{'enabled'} = 0; |
c980f4d2 MT |
152 | } |
153 | if (! blue_used()) { | |
a68fedca | 154 | $menu->{'05.firewall'}{'subMenu'}->{'30.wireless'}{'enabled'} = 0; |
c980f4d2 | 155 | } |
4e565351 | 156 | if ( $ethsettings{'CONFIG_TYPE'} =~ /^(2|3|6|7)$/ && $ethsettings{'RED_TYPE'} eq 'STATIC' ) { |
a68fedca | 157 | $menu->{'03.network'}{'subMenu'}->{'70.aliases'}{'enabled'} = 1; |
3ea75603 | 158 | } |
3ea75603 MT |
159 | } |
160 | ||
161 | sub showhttpheaders | |
162 | { | |
163 | print "Pragma: no-cache\n"; | |
164 | print "Cache-control: no-cache\n"; | |
165 | print "Connection: close\n"; | |
166 | print "Content-type: text/html\n\n"; | |
167 | } | |
168 | ||
169 | sub is_menu_visible($) { | |
170 | my $link = shift; | |
171 | $link =~ s#\?.*$##; | |
172 | return (-e $ENV{'DOCUMENT_ROOT'}."/../$link"); | |
173 | } | |
174 | ||
175 | ||
176 | sub getlink($) { | |
177 | my $root = shift; | |
178 | if (! $root->{'enabled'}) { | |
179 | return ''; | |
180 | } | |
181 | if ($root->{'uri'} !~ /^$/) { | |
182 | my $vars = ''; | |
183 | if ($root->{'vars'} !~ /^$/) { | |
184 | $vars = '?'. $root->{'vars'}; | |
185 | } | |
186 | if (! is_menu_visible($root->{'uri'})) { | |
187 | return ''; | |
188 | } | |
189 | return $root->{'uri'}.$vars; | |
190 | } | |
191 | my $submenus = $root->{'subMenu'}; | |
192 | if (! $submenus) { | |
193 | return ''; | |
194 | } | |
195 | foreach my $item (sort keys %$submenus) { | |
196 | my $link = getlink($submenus->{$item}); | |
197 | if ($link ne '') { | |
198 | return $link; | |
199 | } | |
200 | } | |
201 | return ''; | |
202 | } | |
203 | ||
204 | ||
205 | sub compare_url($) { | |
206 | my $conf = shift; | |
207 | ||
208 | my $uri = $conf->{'uri'}; | |
209 | my $vars = $conf->{'vars'}; | |
210 | my $novars = $conf->{'novars'}; | |
211 | ||
212 | if ($uri eq '') { | |
213 | return 0; | |
214 | } | |
215 | if ($uri ne $URI[0]) { | |
216 | return 0; | |
217 | } | |
218 | if ($novars) { | |
219 | if ($URI[1] !~ /^$/) { | |
220 | return 0; | |
221 | } | |
222 | } | |
223 | if (! $vars) { | |
224 | return 1; | |
225 | } | |
226 | return ($URI[1] eq $vars); | |
227 | } | |
228 | ||
229 | ||
230 | sub gettitle($) { | |
231 | my $root = shift; | |
232 | ||
233 | if (! $root) { | |
234 | return ''; | |
235 | } | |
236 | foreach my $item (sort keys %$root) { | |
237 | my $val = $root->{$item}; | |
238 | if (compare_url($val)) { | |
239 | $val->{'selected'} = 1; | |
240 | if ($val->{'title'} !~ /^$/) { | |
241 | return $val->{'title'}; | |
242 | } | |
243 | return 'EMPTY TITLE'; | |
244 | } | |
245 | ||
246 | my $title = gettitle($val->{'subMenu'}); | |
247 | if ($title ne '') { | |
248 | $val->{'selected'} = 1; | |
249 | return $title; | |
250 | } | |
251 | } | |
252 | return ''; | |
253 | } | |
254 | ||
3ea75603 MT |
255 | sub writehash |
256 | { | |
257 | my $filename = $_[0]; | |
258 | my $hash = $_[1]; | |
259 | ||
260 | # write cgi vars to the file. | |
261 | open(FILE, ">${filename}") or die "Unable to write file $filename"; | |
262 | flock FILE, 2; | |
263 | foreach $var (keys %$hash) | |
264 | { | |
265 | $val = $hash->{$var}; | |
266 | # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y | |
267 | # location of the mouse are submitted as well, this was being written to the settings file causing | |
268 | # some serious grief! This skips the variable.x and variable.y | |
269 | if (!($var =~ /(.x|.y)$/)) { | |
270 | if ($val =~ / /) { | |
271 | $val = "\'$val\'"; } | |
272 | if (!($var =~ /^ACTION/)) { | |
273 | print FILE "${var}=${val}\n"; } | |
274 | } | |
275 | } | |
276 | close FILE; | |
277 | } | |
278 | ||
279 | sub readhash | |
280 | { | |
281 | my $filename = $_[0]; | |
282 | my $hash = $_[1]; | |
283 | my ($var, $val); | |
284 | ||
285 | open(FILE, $filename) or die "Unable to read file $filename"; | |
286 | ||
287 | while (<FILE>) | |
288 | { | |
289 | chop; | |
290 | ($var, $val) = split /=/, $_, 2; | |
291 | if ($var) | |
292 | { | |
293 | $val =~ s/^\'//g; | |
294 | $val =~ s/\'$//g; | |
295 | ||
296 | # Untaint variables read from hash | |
297 | $var =~ /([A-Za-z0-9_-]*)/; $var = $1; | |
298 | $val =~ /([\w\W]*)/; $val = $1; | |
299 | $hash->{$var} = $val; | |
300 | } | |
301 | } | |
302 | close FILE; | |
303 | } | |
304 | ||
305 | sub getcgihash { | |
306 | my ($hash, $params) = @_; | |
307 | my $cgi = CGI->new (); | |
308 | $hash->{'__CGI__'} = $cgi; | |
309 | return if ($ENV{'REQUEST_METHOD'} ne 'POST'); | |
310 | if (!$params->{'wantfile'}) { | |
311 | $CGI::DISABLE_UPLOADS = 1; | |
312 | $CGI::POST_MAX = 512 * 1024; | |
313 | } else { | |
314 | $CGI::POST_MAX = 10 * 1024 * 1024; | |
315 | } | |
316 | ||
317 | $cgi->referer() =~ m/^https?\:\/\/([^\/]+)/; | |
318 | my $referer = $1; | |
319 | $cgi->url() =~ m/^https?\:\/\/([^\/]+)/; | |
320 | my $servername = $1; | |
321 | return if ($referer ne $servername); | |
322 | ||
323 | ### Modified for getting multi-vars, split by | | |
324 | %temp = $cgi->Vars(); | |
325 | foreach my $key (keys %temp) { | |
326 | $hash->{$key} = $temp{$key}; | |
327 | $hash->{$key} =~ s/\0/|/g; | |
328 | $hash->{$key} =~ s/^\s*(.*?)\s*$/$1/; | |
329 | } | |
330 | ||
331 | if (($params->{'wantfile'})&&($params->{'filevar'})) { | |
332 | $hash->{$params->{'filevar'}} = $cgi->upload | |
333 | ($params->{'filevar'}); | |
334 | } | |
335 | return; | |
336 | } | |
337 | ||
338 | sub log | |
339 | { | |
340 | my $logmessage = $_[0]; | |
341 | $logmessage =~ /([\w\W]*)/; | |
342 | $logmessage = $1; | |
343 | system('/usr/bin/logger', '-t', 'ipfire', $logmessage); | |
344 | } | |
345 | ||
346 | sub age | |
347 | { | |
348 | my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, | |
349 | $atime, $mtime, $ctime, $blksize, $blocks) = stat $_[0]; | |
350 | my $now = time; | |
351 | ||
352 | my $totalsecs = $now - $mtime; | |
353 | my $days = int($totalsecs / 86400); | |
354 | my $totalhours = int($totalsecs / 3600); | |
355 | my $hours = $totalhours % 24; | |
356 | my $totalmins = int($totalsecs / 60); | |
357 | my $mins = $totalmins % 60; | |
358 | my $secs = $totalsecs % 60; | |
359 | ||
360 | return "${days}d ${hours}h ${mins}m ${secs}s"; | |
361 | } | |
362 | ||
363 | sub validip | |
364 | { | |
365 | my $ip = $_[0]; | |
366 | ||
367 | if (!($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)) { | |
368 | return 0; } | |
369 | else | |
370 | { | |
371 | @octets = ($1, $2, $3, $4); | |
372 | foreach $_ (@octets) | |
373 | { | |
374 | if (/^0./) { | |
375 | return 0; } | |
376 | if ($_ < 0 || $_ > 255) { | |
377 | return 0; } | |
378 | } | |
379 | return 1; | |
380 | } | |
381 | } | |
382 | ||
383 | sub validmask | |
384 | { | |
385 | my $mask = $_[0]; | |
386 | ||
387 | # secord part an ip? | |
388 | if (&validip($mask)) { | |
389 | return 1; } | |
390 | # second part a number? | |
391 | if (/^0/) { | |
392 | return 0; } | |
393 | if (!($mask =~ /^\d+$/)) { | |
394 | return 0; } | |
395 | if ($mask >= 0 && $mask <= 32) { | |
396 | return 1; } | |
397 | return 0; | |
398 | } | |
399 | ||
400 | sub validipormask | |
401 | { | |
402 | my $ipormask = $_[0]; | |
403 | ||
404 | # see if it is a IP only. | |
405 | if (&validip($ipormask)) { | |
406 | return 1; } | |
407 | # split it into number and mask. | |
408 | if (!($ipormask =~ /^(.*?)\/(.*?)$/)) { | |
409 | return 0; } | |
410 | $ip = $1; | |
411 | $mask = $2; | |
412 | # first part not a ip? | |
413 | if (!(&validip($ip))) { | |
414 | return 0; } | |
415 | return &validmask($mask); | |
416 | } | |
417 | ||
418 | sub validipandmask | |
419 | { | |
420 | my $ipandmask = $_[0]; | |
421 | ||
422 | # split it into number and mask. | |
423 | if (!($ipandmask =~ /^(.*?)\/(.*?)$/)) { | |
424 | return 0; } | |
425 | $ip = $1; | |
426 | $mask = $2; | |
427 | # first part not a ip? | |
428 | if (!(&validip($ip))) { | |
429 | return 0; } | |
430 | return &validmask($mask); | |
431 | } | |
432 | ||
433 | sub validport | |
434 | { | |
435 | $_ = $_[0]; | |
436 | ||
437 | if (!/^\d+$/) { | |
438 | return 0; } | |
439 | if (/^0./) { | |
440 | return 0; } | |
441 | if ($_ >= 1 && $_ <= 65535) { | |
442 | return 1; } | |
443 | return 0; | |
444 | } | |
445 | ||
446 | sub validmac | |
447 | { | |
448 | my $checkmac = $_[0]; | |
449 | my $ot = '[0-9a-f]{2}'; # 2 Hex digits (one octet) | |
450 | if ($checkmac !~ /^$ot:$ot:$ot:$ot:$ot:$ot$/i) | |
451 | { | |
452 | return 0; | |
453 | } | |
454 | return 1; | |
455 | } | |
456 | ||
457 | sub validhostname | |
458 | { | |
459 | # Checks a hostname against RFC1035 | |
460 | my $hostname = $_[0]; | |
461 | ||
462 | # Each part should be at least two characters in length | |
463 | # but no more than 63 characters | |
464 | if (length ($hostname) < 2 || length ($hostname) > 63) { | |
465 | return 0;} | |
466 | # Only valid characters are a-z, A-Z, 0-9 and - | |
467 | if ($hostname !~ /^[a-zA-Z0-9-]*$/) { | |
468 | return 0;} | |
469 | # First character can only be a letter or a digit | |
470 | if (substr ($hostname, 0, 1) !~ /^[a-zA-Z0-9]*$/) { | |
471 | return 0;} | |
472 | # Last character can only be a letter or a digit | |
473 | if (substr ($hostname, -1, 1) !~ /^[a-zA-Z0-9]*$/) { | |
474 | return 0;} | |
475 | return 1; | |
476 | } | |
477 | ||
478 | sub validdomainname | |
479 | { | |
480 | # Checks a domain name against RFC1035 | |
481 | my $domainname = $_[0]; | |
482 | my @parts = split (/\./, $domainname); # Split hostname at the '.' | |
483 | ||
484 | foreach $part (@parts) { | |
485 | # Each part should be at least two characters in length | |
486 | # but no more than 63 characters | |
487 | if (length ($part) < 2 || length ($part) > 63) { | |
488 | return 0;} | |
489 | # Only valid characters are a-z, A-Z, 0-9 and - | |
490 | if ($part !~ /^[a-zA-Z0-9-]*$/) { | |
491 | return 0;} | |
492 | # First character can only be a letter or a digit | |
493 | if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) { | |
494 | return 0;} | |
495 | # Last character can only be a letter or a digit | |
496 | if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) { | |
497 | return 0;} | |
498 | } | |
499 | return 1; | |
500 | } | |
501 | ||
502 | sub validfqdn | |
503 | { | |
504 | # Checks a fully qualified domain name against RFC1035 | |
505 | my $fqdn = $_[0]; | |
506 | my @parts = split (/\./, $fqdn); # Split hostname at the '.' | |
507 | if (scalar(@parts) < 2) { # At least two parts should | |
508 | return 0;} # exist in a FQDN | |
509 | # (i.e. hostname.domain) | |
510 | foreach $part (@parts) { | |
511 | # Each part should be at least two characters in length | |
512 | # but no more than 63 characters | |
513 | if (length ($part) < 2 || length ($part) > 63) { | |
514 | return 0;} | |
515 | # Only valid characters are a-z, A-Z, 0-9 and - | |
516 | if ($part !~ /^[a-zA-Z0-9-]*$/) { | |
517 | return 0;} | |
518 | # First character can only be a letter or a digit | |
519 | if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) { | |
520 | return 0;} | |
521 | # Last character can only be a letter or a digit | |
522 | if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) { | |
523 | return 0;} | |
524 | } | |
525 | return 1; | |
526 | } | |
527 | ||
528 | sub validportrange # used to check a port range | |
529 | { | |
530 | my $port = $_[0]; # port values | |
531 | $port =~ tr/-/:/; # replace all - with colons just in case someone used - | |
532 | my $srcdst = $_[1]; # is it a source or destination port | |
533 | ||
534 | if (!($port =~ /^(\d+)\:(\d+)$/)) { | |
535 | ||
536 | if (!(&validport($port))) { | |
537 | if ($srcdst eq 'src'){ | |
538 | return $tr{'source port numbers'}; | |
539 | } else { | |
540 | return $tr{'destination port numbers'}; | |
541 | } | |
542 | } | |
543 | } | |
544 | else | |
545 | { | |
546 | @ports = ($1, $2); | |
547 | if ($1 >= $2){ | |
548 | if ($srcdst eq 'src'){ | |
549 | return $tr{'bad source range'}; | |
550 | } else { | |
551 | return $tr{'bad destination range'}; | |
552 | } | |
553 | } | |
554 | foreach $_ (@ports) | |
555 | { | |
556 | if (!(&validport($_))) { | |
557 | if ($srcdst eq 'src'){ | |
558 | return $tr{'source port numbers'}; | |
559 | } else { | |
560 | return $tr{'destination port numbers'}; | |
561 | } | |
562 | } | |
563 | } | |
564 | return; | |
565 | } | |
566 | } | |
567 | ||
568 | # Test if IP is within a subnet | |
569 | # Call: IpInSubnet (Addr, Subnet, Subnet Mask) | |
570 | # Subnet can be an IP of the subnet: 10.0.0.0 or 10.0.0.1 | |
571 | # Everything in dottted notation | |
572 | # Return: TRUE/FALSE | |
573 | sub IpInSubnet | |
574 | { | |
575 | $ip = unpack('N', inet_aton(shift)); | |
576 | $start = unpack('N', inet_aton(shift)); | |
577 | $mask = unpack('N', inet_aton(shift)); | |
578 | $start &= $mask; # base of subnet... | |
579 | $end = $start + ~$mask; | |
580 | return (($ip >= $start) && ($ip <= $end)); | |
581 | } | |
582 | ||
583 | sub validemail { | |
584 | my $mail = shift; | |
585 | return 0 if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/ ); | |
586 | return 0 if ( $mail =~ /^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/); | |
587 | return 0 if ( $mail !~ /([0-9a-zA-Z]{1})\@./ ); | |
588 | return 0 if ( $mail !~ /.\@([0-9a-zA-Z]{1})/ ); | |
589 | return 0 if ( $mail =~ /.\.\-.|.\-\..|.\.\..|.\-\-./g ); | |
590 | return 0 if ( $mail =~ /.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./g ); | |
591 | return 0 if ( $mail !~ /\.([a-zA-Z]{2,3})$/ ); | |
592 | return 1; | |
593 | } | |
594 | ||
595 | sub readhasharray { | |
596 | my ($filename, $hash) = @_; | |
597 | ||
598 | open(FILE, $filename) or die "Unable to read file $filename"; | |
599 | ||
600 | while (<FILE>) { | |
601 | my ($key, $rest, @temp); | |
602 | chomp; | |
603 | ($key, $rest) = split (/,/, $_, 2); | |
604 | if ($key =~ /^[0-9]+$/ && $rest) { | |
605 | @temp = split (/,/, $rest); | |
606 | $hash->{$key} = \@temp; | |
607 | } | |
608 | } | |
609 | close FILE; | |
610 | return; | |
611 | } | |
612 | ||
613 | sub writehasharray { | |
614 | my ($filename, $hash) = @_; | |
615 | my ($key, @temp); | |
616 | ||
617 | open(FILE, ">$filename") or die "Unable to write to file $filename"; | |
618 | ||
619 | foreach $key (keys %$hash) { | |
620 | if ( $hash->{$key} ) { | |
621 | print FILE "$key"; | |
622 | foreach $i (0 .. $#{$hash->{$key}}) { | |
623 | print FILE ",$hash->{$key}[$i]"; | |
624 | } | |
625 | } | |
626 | print FILE "\n"; | |
627 | } | |
628 | close FILE; | |
629 | return; | |
630 | } | |
631 | ||
632 | sub findhasharraykey { | |
633 | foreach my $i (1 .. 1000000) { | |
634 | if ( ! exists $_[0]{$i}) { | |
635 | return $i; | |
636 | } | |
637 | } | |
638 | } | |
639 | ||
640 | sub cleanhtml | |
641 | { | |
642 | my $outstring =$_[0]; | |
643 | $outstring =~ tr/,/ / if not defined $_[1] or $_[1] ne 'y'; | |
644 | $outstring =~ s/&/&/g; | |
645 | $outstring =~ s/\'/'/g; | |
646 | $outstring =~ s/\"/"/g; | |
647 | $outstring =~ s/</</g; | |
648 | $outstring =~ s/>/>/g; | |
649 | return $outstring; | |
650 | } | |
986e08d9 | 651 | |
3ea75603 MT |
652 | sub connectionstatus |
653 | { | |
986e08d9 MT |
654 | my %pppsettings = (); |
655 | my %netsettings = (); | |
656 | my $iface=''; | |
657 | ||
658 | $pppsettings{'PROFILENAME'} = 'None'; | |
659 | &General::readhash("${General::swroot}/ppp/settings", \%pppsettings); | |
660 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
661 | ||
662 | my $profileused=''; | |
663 | if ( ! ( $netsettings{'CONFIG_TYPE'} =~ /^(2|3|6|7)$/ && $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) ) { | |
664 | $profileused="- $pppsettings{'PROFILENAME'}"; | |
665 | } | |
666 | ||
667 | if ( ( $pppsettings{'METHOD'} eq 'DHCP' && $netsettings{'RED_TYPE'} ne 'PPTP') | |
668 | || $netsettings{'RED_TYPE'} eq 'DHCP' ) { | |
669 | if (open(IFACE, "${General::swroot}/red/iface")) { | |
670 | $iface = <IFACE>; | |
671 | close IFACE; | |
672 | chomp ($iface); | |
673 | $iface =~ /([a-zA-Z0-9]*)/; $iface = $1; | |
674 | } | |
675 | } | |
676 | ||
677 | my ($timestr, $connstate); | |
678 | if ($netsettings{'CONFIG_TYPE'} =~ /^(0|1|4|5)$/ && $pppsettings{'TYPE'} =~ /^isdn/) { | |
679 | # Count ISDN channels | |
680 | my ($idmap, $chmap, $drmap, $usage, $flags, $phone); | |
681 | my @phonenumbers; | |
682 | my $count=0; | |
683 | ||
684 | open (FILE, "/dev/isdninfo"); | |
685 | ||
686 | $idmap = <FILE>; chop $idmap; | |
687 | $chmap = <FILE>; chop $chmap; | |
688 | $drmap = <FILE>; chop $drmap; | |
689 | $usage = <FILE>; chop $usage; | |
690 | $flags = <FILE>; chop $flags; | |
691 | $phone = <FILE>; chop $phone; | |
692 | ||
693 | $phone =~ s/^phone(\s*):(\s*)//; | |
694 | ||
695 | @phonenumbers = split / /, $phone; | |
696 | ||
697 | foreach (@phonenumbers) { | |
698 | if ($_ ne '???') { | |
699 | $count++; | |
700 | } | |
701 | } | |
702 | close (FILE); | |
703 | ||
704 | ## Connection status | |
705 | my $number; | |
706 | if ($count == 0) { | |
707 | $number = 'none!'; | |
708 | } elsif ($count == 1) { | |
709 | $number = 'single'; | |
710 | } else { | |
711 | $number = 'dual'; | |
712 | } | |
713 | ||
714 | if (-e "${General::swroot}/red/active") { | |
715 | $timestr = &General::age("${General::swroot}/red/active"); | |
716 | $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'connected'} - $number channel (<span class='ipcop_StatusBigRed'>$timestr</span>) $profileused</span>"; | |
717 | } else { | |
718 | if ($count == 0) { | |
719 | if (-e "${General::swroot}/red/dial-on-demand") { | |
720 | $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'dod waiting'} $profileused</span>"; | |
721 | } else { | |
722 | $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'idle'} $profileused</span>"; | |
723 | } | |
724 | } else { | |
725 | $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'connecting'} $profileused</span>"; | |
726 | } | |
727 | } | |
728 | } elsif ($netsettings{'RED_TYPE'} eq "STATIC" || $pppsettings {'METHOD'} eq 'STATIC') { | |
729 | if (-e "${General::swroot}/red/active") { | |
730 | $timestr = &General::age("${General::swroot}/red/active"); | |
731 | $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'connected'} (<span class='ipcop_StatusBigRed'>$timestr</span>) $profileused</span>"; | |
732 | } else { | |
733 | $connstate = "<span class='ipcop_StatusBig'>$Lang::tr{'idle'} $profileused</span>"; | |
734 | } | |
735 | } elsif ( ( (-e "${General::swroot}/dhcpc/dhcpcd-$iface.pid") && $netsettings{'RED_TYPE'} ne 'PPTP' ) || | |
736 | !system("/bin/ps -ef | /bin/grep -q '[p]ppd'") || !system("/bin/ps -ef | /bin/grep -q '[c]onnectioncheck'")) { | |
737 | if (-e "${General::swroot}/red/active") { | |
738 | $timestr = &General::age("${General::swroot}/red/active"); | |
739 | if ($pppsettings{'TYPE'} =~ /^(modem|bewanadsl|conexantpciadsl|eagleusbadsl)$/) { | |
740 | my $speed; | |
741 | if ($pppsettings{'TYPE'} eq 'modem') { | |
742 | open(CONNECTLOG, "/var/log/connect.log"); | |
743 | while (<CONNECTLOG>) { | |
744 | if (/CONNECT/) { | |
745 | $speed = (split / /)[6]; | |
746 | } | |
747 | } | |
748 | close (CONNECTLOG); | |
749 | } elsif ($pppsettings{'TYPE'} eq 'bewanadsl') { | |
750 | $speed = `/usr/bin/unicorn_status | /bin/grep Rate | /usr/bin/cut -f2 -d ':'`; | |
751 | } elsif ($pppsettings{'TYPE'} eq 'conexantpciadsl') { | |
752 | $speed = `/bin/cat /proc/net/atm/CnxAdsl:* | /bin/grep 'Line Rates' | /bin/sed -e 's+Line Rates: Receive+Rx+' -e 's+Transmit+Tx+'`; | |
753 | } elsif ($pppsettings{'TYPE'} eq 'eagleusbadsl') { | |
754 | $speed = `/usr/sbin/eaglestat | /bin/grep Rate`; | |
755 | } | |
f8e080ef | 756 | $connstate = "$Lang::tr{'connected'} ($timestr) $profileused (\@$speed)"; |
986e08d9 | 757 | } else { |
f8e080ef | 758 | $connstate = "$Lang::tr{'connected'} ($timestr) $profileused"; |
986e08d9 MT |
759 | } |
760 | } else { | |
761 | if (-e "${General::swroot}/red/dial-on-demand") { | |
f8e080ef | 762 | $connstate = "$Lang::tr{'dod waiting'} $profileused"; |
986e08d9 | 763 | } else { |
f8e080ef | 764 | $connstate = "$Lang::tr{'connecting'} $profileused"; |
986e08d9 MT |
765 | } |
766 | } | |
767 | } else { | |
f8e080ef | 768 | $connstate = "$Lang::tr{'idle'} $profileused"; |
986e08d9 | 769 | } |
3ea75603 MT |
770 | return $connstate; |
771 | } | |
772 | ||
773 | sub srtarray | |
774 | # Darren Critchley - darrenc@telus.net - (c) 2003 | |
775 | # &srtarray(SortOrder, AlphaNumeric, SortDirection, ArrayToBeSorted) | |
776 | # This subroutine will take the following parameters: | |
777 | # ColumnNumber = the column which you want to sort on, starts at 1 | |
778 | # AlphaNumberic = a or n (lowercase) defines whether the sort should be alpha or numberic | |
779 | # SortDirection = asc or dsc (lowercase) Ascending or Descending sort | |
780 | # ArrayToBeSorted = the array that wants sorting | |
781 | # | |
782 | # Returns an array that is sorted to your specs | |
783 | # | |
784 | # If SortOrder is greater than the elements in array, then it defaults to the first element | |
785 | # | |
786 | { | |
787 | my ($colno, $alpnum, $srtdir, @tobesorted) = @_; | |
788 | my @tmparray; | |
789 | my @srtedarray; | |
790 | my $line; | |
791 | my $newline; | |
792 | my $ttlitems = scalar @tobesorted; # want to know the number of rows in the passed array | |
793 | if ($ttlitems < 1){ # if no items, don't waste our time lets leave | |
794 | return (@tobesorted); | |
795 | } | |
796 | my @tmp = split(/\,/,$tobesorted[0]); | |
797 | $ttlitems = scalar @tmp; # this should be the number of elements in each row of the passed in array | |
798 | ||
799 | # Darren Critchley - validate parameters | |
800 | if ($colno > $ttlitems){$colno = '1';} | |
801 | $colno--; # remove one from colno to deal with arrays starting at 0 | |
802 | if($colno < 0){$colno = '0';} | |
803 | if ($alpnum ne '') { $alpnum = lc($alpnum); } else { $alpnum = 'a'; } | |
804 | if ($srtdir ne '') { $srtdir = lc($srtdir); } else { $srtdir = 'src'; } | |
805 | ||
806 | foreach $line (@tobesorted) | |
807 | { | |
808 | chomp($line); | |
809 | if ($line ne '') { | |
810 | my @temp = split(/\,/,$line); | |
811 | # Darren Critchley - juggle the fields so that the one we want to sort on is first | |
812 | my $tmpholder = $temp[0]; | |
813 | $temp[0] = $temp[$colno]; | |
814 | $temp[$colno] = $tmpholder; | |
815 | $newline = ""; | |
816 | for ($ctr=0; $ctr < $ttlitems ; $ctr++) { | |
817 | $newline=$newline . $temp[$ctr] . ","; | |
818 | } | |
819 | chop($newline); | |
820 | push(@tmparray,$newline); | |
821 | } | |
822 | } | |
823 | if ($alpnum eq 'n') { | |
824 | @tmparray = sort {$a <=> $b} @tmparray; | |
825 | } else { | |
826 | @tmparray = (sort @tmparray); | |
827 | } | |
828 | foreach $line (@tmparray) | |
829 | { | |
830 | chomp($line); | |
831 | if ($line ne '') { | |
832 | my @temp = split(/\,/,$line); | |
833 | my $tmpholder = $temp[0]; | |
834 | $temp[0] = $temp[$colno]; | |
835 | $temp[$colno] = $tmpholder; | |
836 | $newline = ""; | |
837 | for ($ctr=0; $ctr < $ttlitems ; $ctr++){ | |
838 | $newline=$newline . $temp[$ctr] . ","; | |
839 | } | |
840 | chop($newline); | |
841 | push(@srtedarray,$newline); | |
842 | } | |
843 | } | |
844 | ||
845 | if ($srtdir eq 'dsc') { | |
846 | @tmparray = reverse(@srtedarray); | |
847 | return (@tmparray); | |
848 | } else { | |
849 | return (@srtedarray); | |
850 | } | |
851 | } | |
852 | ||
853 | sub speedtouchversion | |
854 | { | |
855 | if (-f "/proc/bus/usb/devices") | |
856 | { | |
857 | $speedtouch=`/bin/cat /proc/bus/usb/devices | /bin/grep 'Vendor=06b9 ProdID=4061' | /usr/bin/cut -d ' ' -f6`; | |
858 | if ($speedtouch eq '') { | |
859 | $speedtouch= $tr{'connect the modem'}; | |
860 | } | |
861 | } else { | |
862 | $speedtouch='USB '.$tr{'not running'}; | |
863 | } | |
864 | return $speedtouch | |
865 | } | |
866 | ||
867 | sub CheckSortOrder { | |
868 | #Sorting of allocated leases | |
869 | if ($ENV{'QUERY_STRING'} =~ /^IPADDR|^ETHER|^HOSTNAME|^ENDTIME/ ) { | |
870 | my $newsort=$ENV{'QUERY_STRING'}; | |
871 | &readhash("${swroot}/dhcp/settings", \%dhcpsettings); | |
872 | $act=$dhcpsettings{'SORT_LEASELIST'}; | |
873 | #Reverse actual ? | |
874 | if ($act =~ $newsort) { | |
875 | if ($act !~ 'Rev') {$Rev='Rev'}; | |
876 | $newsort.=$Rev | |
877 | }; | |
878 | ||
879 | $dhcpsettings{'SORT_LEASELIST'}=$newsort; | |
880 | &writehash("${swroot}/dhcp/settings", \%dhcpsettings); | |
881 | $dhcpsettings{'ACTION'} = 'SORT'; # avoid the next test "First lauch" | |
882 | } | |
883 | ||
884 | } | |
885 | ||
886 | sub PrintActualLeases | |
887 | { | |
888 | &openbox('100%', 'left', $tr{'current dynamic leases'}); | |
889 | print <<END | |
890 | <table width='100%'> | |
891 | <tr> | |
892 | <td width='25%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IPADDR'><b>$tr{'ip address'}</b></a></td> | |
893 | <td width='25%' align='center'><a href='$ENV{'SCRIPT_NAME'}?ETHER'><b>$tr{'mac address'}</b></a></td> | |
894 | <td width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?HOSTNAME'><b>$tr{'hostname'}</b></a></td> | |
32fdd7d9 MT |
895 | <td width='25%' align='center'><a href='$ENV{'SCRIPT_NAME'}?ENDTIME'><b>$tr{'lease expires'} (local time d/m/y)</b></a></td> |
896 | <td width='5%' align='center'><b>Add to fix leases<b></td> | |
3ea75603 MT |
897 | </tr> |
898 | END | |
899 | ; | |
900 | ||
986e08d9 | 901 | open(LEASES,"/var/state/dhcp/dhcpd.leases") or die "Can't open dhcpd.leases"; |
3ea75603 MT |
902 | while ($line = <LEASES>) { |
903 | next if( $line =~ /^\s*#/ ); | |
904 | chomp($line); | |
905 | @temp = split (' ', $line); | |
906 | ||
907 | if ($line =~ /^\s*lease/) { | |
908 | $ip = $temp[1]; | |
909 | #All field are not necessarily read. Clear everything | |
910 | $endtime = 0; | |
911 | $ether = ""; | |
912 | $hostname = ""; | |
913 | } | |
914 | ||
915 | if ($line =~ /^\s*ends/) { | |
916 | $line =~ /(\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)/; | |
917 | $endtime = timegm($6, $5, $4, $3, $2 - 1, $1 - 1900); | |
918 | } | |
919 | ||
920 | if ($line =~ /^\s*hardware ethernet/) { | |
921 | $ether = $temp[2]; | |
922 | $ether =~ s/;//g; | |
923 | } | |
924 | ||
925 | if ($line =~ /^\s*client-hostname/) { | |
926 | $hostname = "$temp[1] $temp[2] $temp[3]"; | |
927 | $hostname =~ s/;//g; | |
928 | $hostname =~ s/\"//g; | |
929 | } | |
930 | ||
931 | if ($line eq "}") { | |
932 | @record = ('IPADDR',$ip,'ENDTIME',$endtime,'ETHER',$ether,'HOSTNAME',$hostname); | |
933 | $record = {}; # create a reference to empty hash | |
934 | %{$record} = @record; # populate that hash with @record | |
935 | $entries{$record->{'IPADDR'}} = $record; # add this to a hash of hashes | |
936 | } | |
937 | } | |
938 | close(LEASES); | |
939 | ||
940 | my $id = 0; | |
941 | foreach my $key (sort leasesort keys %entries) { | |
32fdd7d9 | 942 | print "<form method='post' action='/cgi-bin/dhcp.cgi'>\n"; |
3ea75603 MT |
943 | my $hostname = &cleanhtml($entries{$key}->{HOSTNAME},"y"); |
944 | ||
945 | if ($id % 2) { | |
946 | print "<tr bgcolor='$table1colour'>"; | |
947 | } | |
948 | else { | |
949 | print "<tr bgcolor='$table2colour'>"; | |
950 | } | |
951 | ||
952 | print <<END | |
32fdd7d9 MT |
953 | <td align='center'><input type='hidden' name='FIX_ADDR' value='$entries{$key}->{IPADDR}' />$entries{$key}->{IPADDR}</td> |
954 | <td align='center'><input type='hidden' name='FIX_MAC' value='$entries{$key}->{ETHER}' />$entries{$key}->{ETHER}</td> | |
955 | <td align='center'><input type='hidden' name='FIX_REMARK' value='$hostname' /> $hostname</td> | |
956 | <td align='center'><input type='hidden' name='FIX_ENABLED' value='on' /> | |
3ea75603 MT |
957 | END |
958 | ; | |
959 | ||
960 | ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $dst) = localtime ($entries{$key}->{ENDTIME}); | |
961 | $enddate = sprintf ("%02d/%02d/%d %02d:%02d:%02d",$mday,$mon+1,$year+1900,$hour,$min,$sec); | |
962 | ||
963 | if ($entries{$key}->{ENDTIME} < time() ){ | |
964 | print "<strike>$enddate</strike>"; | |
965 | } else { | |
966 | print "$enddate"; | |
967 | } | |
32fdd7d9 MT |
968 | print <<END |
969 | <td><input type='hidden' name='ACTION' value='$Lang::tr{'add'}2' /><input type='submit' name='SUBMIT' value='$Lang::tr{'add'}' /> | |
970 | </td></td></tr></form> | |
971 | END | |
972 | ; | |
3ea75603 MT |
973 | $id++; |
974 | } | |
975 | ||
976 | print "</table>"; | |
977 | &closebox(); | |
978 | } | |
979 | ||
980 | ||
981 | # This sub is used during display of actives leases | |
982 | sub leasesort { | |
983 | if (rindex ($dhcpsettings{'SORT_LEASELIST'},'Rev') != -1) | |
984 | { | |
985 | $qs=substr ($dhcpsettings{'SORT_LEASELIST'},0,length($dhcpsettings{'SORT_LEASELIST'})-3); | |
986 | if ($qs eq 'IPADDR') { | |
987 | @a = split(/\./,$entries{$a}->{$qs}); | |
988 | @b = split(/\./,$entries{$b}->{$qs}); | |
989 | ($b[0]<=>$a[0]) || | |
990 | ($b[1]<=>$a[1]) || | |
991 | ($b[2]<=>$a[2]) || | |
992 | ($b[3]<=>$a[3]); | |
993 | }else { | |
994 | $entries{$b}->{$qs} cmp $entries{$a}->{$qs}; | |
995 | } | |
996 | } | |
997 | else #not reverse | |
998 | { | |
999 | $qs=$dhcpsettings{'SORT_LEASELIST'}; | |
1000 | if ($qs eq 'IPADDR') { | |
1001 | @a = split(/\./,$entries{$a}->{$qs}); | |
1002 | @b = split(/\./,$entries{$b}->{$qs}); | |
1003 | ($a[0]<=>$b[0]) || | |
1004 | ($a[1]<=>$b[1]) || | |
1005 | ($a[2]<=>$b[2]) || | |
1006 | ($a[3]<=>$b[3]); | |
1007 | }else { | |
1008 | $entries{$a}->{$qs} cmp $entries{$b}->{$qs}; | |
1009 | } | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | sub get_uplinks() { | |
1014 | my @uplinks = (); | |
1015 | opendir(DIR, "${swroot}/uplinks/") || return \@uplinks; | |
1016 | foreach my $dir (readdir(DIR)) { | |
1017 | next if ($dir =~ /^\./); | |
1018 | next if (-f "${swroot}/uplinks/$dir"); | |
1019 | push(@uplinks, $dir); | |
1020 | } | |
1021 | closedir(DIR); | |
1022 | return \@uplinks; | |
1023 | } | |
1024 | ||
1025 | sub get_iface($) { | |
1026 | my $filename = shift; | |
1027 | chomp($filename); | |
1028 | open (F, $filename) || return ""; | |
1029 | my $iface = <F>; | |
1030 | close(F); | |
1031 | chomp($iface); | |
1032 | return $iface; | |
1033 | } | |
1034 | ||
1035 | sub get_red_ifaces_by_type($) { | |
1036 | my $type=shift; | |
1037 | my @gottypeiface = (); | |
1038 | my @gottypeuplink = (); | |
1039 | my @gottype = (); | |
1040 | ||
1041 | my $ref=get_uplinks(); | |
1042 | my @uplinks=@$ref; | |
1043 | my %set = (); | |
1044 | foreach my $link (@uplinks) { | |
1045 | eval { | |
1046 | &readhash("${swroot}/uplinks/$link/settings", \%set); | |
1047 | }; | |
1048 | push(@gottype, $link); | |
1049 | ||
1050 | my $iface = $set{'RED_DEV'}; | |
1051 | if (!$iface) { | |
1052 | $iface = get_iface("${swroot}/uplinks/$link/interface"); | |
1053 | } | |
1054 | next if (!$iface); | |
1055 | ||
1056 | if ($set{'RED_TYPE'} eq $type) { | |
1057 | push(@gottypeiface, $iface); | |
1058 | push(@gottypeuplink, $link); | |
1059 | } | |
1060 | } | |
1061 | return (\@gottypeiface, \@gottypeuplink, \@gottype); | |
1062 | } | |
1063 | ||
1064 | sub get_red_ifaces() { | |
1065 | return `cat ${swroot}/uplinks/*/interface 2>/dev/null`; | |
1066 | } | |
1067 | ||
1068 | sub get_zone_devices($) { | |
1069 | my $bridge = shift; | |
1070 | my @ifaces = (); | |
1071 | open (FILE, "${swroot}/ethernet/$bridge") || return ""; | |
1072 | foreach my $line (<FILE>) { | |
1073 | chomp($line); | |
1074 | next if (!$line); | |
1075 | push(@ifaces, $line); | |
1076 | } | |
1077 | close(FILE); | |
1078 | return \@ifaces; | |
1079 | } |