]>
git.ipfire.org Git - ipfire-2.x.git/blob - config/cfgroot/general-functions.pl
3 # This code is distributed under the terms of the GPL
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()
12 # $Id: general-functions.pl,v 1.1.2.26 2006/01/04 16:33:55 franck78 Exp $
22 $|=1; # line buffering
24 $General::version
= 'VERSION';
25 $General::swroot
= 'CONFIG_ROOT';
26 $General::noipprefix
= 'noipg-';
27 $General::adminmanualurl
= 'http://wiki.ipfire.org';
30 # log ("message") use default 'ipcop' tag
31 # log ("tag","message") use your tag
36 $tag = shift if (@_>1);
37 my $logmessage = $_[0];
38 $logmessage =~ /([\w\W]*)/;
40 system('/usr/bin/logger', '-t', $tag, $logmessage);
50 # Some ipcop code expects that readhash 'complete' the hash if new entries
51 # are presents. Not clear it !!!
54 open(FILE
, $filename) or die "Unable to read file $filename";
59 ($var, $val) = split /=/, $_, 2;
65 # Untaint variables read from hash
66 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
67 $val =~ /([\w\W]*)/; $val = $1;
81 # write cgi vars to the file.
82 open(FILE
, ">${filename}") or die "Unable to write file $filename";
84 foreach $var (keys %$hash)
87 # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
88 # location of the mouse are submitted as well, this was being written to the settings file causing
89 # some serious grief! This skips the variable.x and variable.y
90 if (!($var =~ /(.x|.y)$/)) {
93 if (!($var =~ /^ACTION/)) {
94 print FILE
"${var}=${val}\n"; }
103 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
104 $atime, $mtime, $ctime, $blksize, $blocks) = stat $_[0];
107 my $totalsecs = $now - $mtime;
108 my $days = int($totalsecs / 86400);
109 my $totalhours = int($totalsecs / 3600);
110 my $hours = $totalhours % 24;
111 my $totalmins = int($totalsecs / 60);
112 my $mins = $totalmins % 60;
113 my $secs = $totalsecs % 60;
115 return "${days}d ${hours}h ${mins}m ${secs}s";
122 if (!($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)) {
126 my @octets = ($1, $2, $3, $4);
131 if ($_ < 0 || $_ > 255) {
143 if (&validip
($mask)) {
145 # second part a number?
148 if (!($mask =~ /^\d+$/)) {
150 if ($mask >= 0 && $mask <= 32) {
157 my $ipormask = $_[0];
159 # see if it is a IP only.
160 if (&validip
($ipormask)) {
162 # split it into number and mask.
163 if (!($ipormask =~ /^(.*?)\/(.*?
)$/)) {
167 # first part not a ip?
168 if (!(&validip
($ip))) {
170 return &validmask
($mask);
175 my $ipandmask = $_[0];
177 # split it into number and mask.
178 if (!($ipandmask =~ /^(.*?)\/(.*?
)$/)) {
182 # first part not a ip?
183 if (!(&validip
($ip))) {
185 return &validmask
($mask);
196 if ($_ >= 1 && $_ <= 65535) {
203 my $checkmac = $_[0];
204 my $ot = '[0-9a-f]{2}'; # 2 Hex digits (one octet)
205 if ($checkmac !~ /^$ot:$ot:$ot:$ot:$ot:$ot$/i)
214 # Checks a hostname against RFC1035
215 my $hostname = $_[0];
217 # Each part should be at least two characters in length
218 # but no more than 63 characters
219 if (length ($hostname) < 1 || length ($hostname) > 63) {
221 # Only valid characters are a-z, A-Z, 0-9 and -
222 if ($hostname !~ /^[a-zA-Z0-9-]*$/) {
224 # First character can only be a letter or a digit
225 if (substr ($hostname, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
227 # Last character can only be a letter or a digit
228 if (substr ($hostname, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
237 # Checks a domain name against RFC1035
238 my $domainname = $_[0];
239 my @parts = split (/\./, $domainname); # Split hostname at the '.'
241 foreach $part (@parts) {
242 # Each part should be at least two characters in length
243 # but no more than 63 characters
244 if (length ($part) < 2 || length ($part) > 63) {
246 # Only valid characters are a-z, A-Z, 0-9 and -
247 if ($part !~ /^[a-zA-Z0-9-]*$/) {
249 # First character can only be a letter or a digit
250 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
252 # Last character can only be a letter or a digit
253 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
263 # Checks a fully qualified domain name against RFC1035
265 my @parts = split (/\./, $fqdn); # Split hostname at the '.'
266 if (scalar(@parts) < 2) { # At least two parts should
267 return 0;} # exist in a FQDN
268 # (i.e. hostname.domain)
269 foreach $part (@parts) {
270 # Each part should be at least one character in length
271 # but no more than 63 characters
272 if (length ($part) < 1 || length ($part) > 63) {
274 # Only valid characters are a-z, A-Z, 0-9 and -
275 if ($part !~ /^[a-zA-Z0-9-]*$/) {
277 # First character can only be a letter or a digit
278 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
280 # Last character can only be a letter or a digit
281 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
287 sub validportrange
# used to check a port range
289 my $port = $_[0]; # port values
290 $port =~ tr/-/:/; # replace all - with colons just in case someone used -
291 my $srcdst = $_[1]; # is it a source or destination port
293 if (!($port =~ /^(\d+)\:(\d+)$/)) {
295 if (!(&validport
($port))) {
296 if ($srcdst eq 'src'){
297 return $Lang::tr
{'source port numbers'};
299 return $Lang::tr
{'destination port numbers'};
305 my @ports = ($1, $2);
307 if ($srcdst eq 'src'){
308 return $Lang::tr
{'bad source range'};
310 return $Lang::tr
{'bad destination range'};
315 if (!(&validport
($_))) {
316 if ($srcdst eq 'src'){
317 return $Lang::tr
{'source port numbers'};
319 return $Lang::tr
{'destination port numbers'};
327 # Test if IP is within a subnet
328 # Call: IpInSubnet (Addr, Subnet, Subnet Mask)
329 # Subnet can be an IP of the subnet: 10.0.0.0 or 10.0.0.1
330 # Everything in dottted notation
334 my $ip = unpack('N', &Socket
::inet_aton
(shift));
335 my $start = unpack('N', &Socket
::inet_aton
(shift));
336 my $mask = unpack('N', &Socket
::inet_aton
(shift));
337 $start &= $mask; # base of subnet...
338 my $end = $start + ~$mask;
339 return (($ip >= $start) && ($ip <= $end));
343 # Return the following IP (IP+1) in dotted notation.
344 # Call: NextIP ('1.1.1.1');
349 return &Socket
::inet_ntoa
( pack("N", 1 + unpack('N', &Socket
::inet_aton
(shift))
356 return 0 if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/ );
357 return 0 if ( $mail =~ /^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/);
358 return 0 if ( $mail !~ /([0-9a-zA-Z]{1})\@./ );
359 return 0 if ( $mail !~ /.\@([0-9a-zA-Z]{1})/ );
360 return 0 if ( $mail =~ /.\.\-.|.\-\..|.\.\..|.\-\-./g );
361 return 0 if ( $mail =~ /.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./g );
362 return 0 if ( $mail !~ /\.([a-zA-Z]{2,4})$/ );
367 # Currently only vpnmain use this three procs (readhasharray, writehasharray, findhasharray)
368 # The 'key' used is numeric but is perfectly unneeded! This will to be removed so don't use
369 # this code. Vpnmain will be splitted in parts: x509/pki, connection ipsec, connection other,... .
372 my ($filename, $hash) = @_;
375 open(FILE
, $filename) or die "Unable to read file $filename";
378 my ($key, $rest, @temp);
380 ($key, $rest) = split (/,/, $_, 2);
381 if ($key =~ /^[0-9]+$/) {
382 @temp = split (/,/, $rest);
383 $hash->{$key} = \
@temp;
391 my ($filename, $hash) = @_;
392 my ($key, @temp, $i);
394 open(FILE
, ">$filename") or die "Unable to write to file $filename";
396 foreach $key (keys %$hash) {
397 if ($key =~ /^[0-9]+$/) {
399 foreach $i (0 .. $#{$hash->{$key}}) {
400 print FILE
",$hash->{$key}[$i]";
409 sub findhasharraykey
{
410 foreach my $i (1 .. 1000000) {
411 if ( ! exists $_[0]{$i}) {
418 # Darren Critchley - darrenc@telus.net - (c) 2003
419 # &srtarray(SortOrder, AlphaNumeric, SortDirection, ArrayToBeSorted)
420 # This subroutine will take the following parameters:
421 # ColumnNumber = the column which you want to sort on, starts at 1
422 # AlphaNumberic = a or n (lowercase) defines whether the sort should be alpha or numberic
423 # SortDirection = asc or dsc (lowercase) Ascending or Descending sort
424 # ArrayToBeSorted = the array that wants sorting
426 # Returns an array that is sorted to your specs
428 # If SortOrder is greater than the elements in array, then it defaults to the first element
431 my ($colno, $alpnum, $srtdir, @tobesorted) = @_;
437 my $ttlitems = scalar @tobesorted; # want to know the number of rows in the passed array
438 if ($ttlitems < 1){ # if no items, don't waste our time lets leave
439 return (@tobesorted);
441 my @tmp = split(/\,/,$tobesorted[0]);
442 $ttlitems = scalar @tmp; # this should be the number of elements in each row of the passed in array
444 # Darren Critchley - validate parameters
445 if ($colno > $ttlitems){$colno = '1';}
446 $colno--; # remove one from colno to deal with arrays starting at 0
447 if($colno < 0){$colno = '0';}
448 if ($alpnum ne '') { $alpnum = lc($alpnum); } else { $alpnum = 'a'; }
449 if ($srtdir ne '') { $srtdir = lc($srtdir); } else { $srtdir = 'src'; }
451 foreach $line (@tobesorted)
455 my @temp = split(/\,/,$line);
456 # Darren Critchley - juggle the fields so that the one we want to sort on is first
457 my $tmpholder = $temp[0];
458 $temp[0] = $temp[$colno];
459 $temp[$colno] = $tmpholder;
461 for ($ctr=0; $ctr < $ttlitems ; $ctr++) {
462 $newline=$newline . $temp[$ctr] . ",";
465 push(@tmparray,$newline);
468 if ($alpnum eq 'n') {
469 @tmparray = sort {$a <=> $b} @tmparray;
471 @tmparray = (sort @tmparray);
473 foreach $line (@tmparray)
477 my @temp = split(/\,/,$line);
478 my $tmpholder = $temp[0];
479 $temp[0] = $temp[$colno];
480 $temp[$colno] = $tmpholder;
482 for ($ctr=0; $ctr < $ttlitems ; $ctr++){
483 $newline=$newline . $temp[$ctr] . ",";
486 push(@srtedarray,$newline);
490 if ($srtdir eq 'dsc') {
491 @tmparray = reverse(@srtedarray);
494 return (@srtedarray);
500 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
501 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
502 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
503 Net
::SSLeay
::set_proxy
($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
505 my ($out, $response) = Net
::SSLeay
::get_http
( 'checkip.dyndns.org',
508 Net
::SSLeay
::make_headers
('User-Agent' => 'IPFire' )
510 if ($response =~ m
%HTTP/1\
.. 200 OK
%) {
511 $out =~ /Current IP Address: (\d+.\d+.\d+.\d+)/;
518 # Check if hostname.domain provided have IP provided
519 # use gethostbyname to verify that
525 # 1 IP matches host.domain
528 sub DyndnsServiceSync
($;$;$) {
530 my ($ip,$hostName,$domain) = @_;
533 #fix me no ip GROUP, what is the name ?
534 $hostName =~ s/$General::noipprefix//;
535 if ($hostName) { #may be empty
536 $hostName = "$hostName.$domain";
537 @addresses = gethostbyname($hostName);
540 if ($addresses[0] eq '') { # nothing returned ?
541 $hostName = $domain; # try resolving with domain only
542 @addresses = gethostbyname($hostName);
545 if ($addresses[0] ne '') { # got something ?
546 #&General::log("name:$addresses[0], alias:$addresses[1]");
547 # Build clear text list of IP
548 @addresses = map ( &Socket
::inet_ntoa
($_), @addresses[4..$#addresses]);
549 if (grep (/$ip/, @addresses)) {
556 # This sub returns the red IP used to compare in DyndnsServiceSync
560 &General
::readhash
("${General::swroot}/ddns/settings", \
%settings);
562 open(IP
, "${General::swroot}/red/local-ipaddress") or return 'unavailable';
567 if (&General
::IpInSubnet
($ip,'10.0.0.0','255.0.0.0') ||
568 &General
::IpInSubnet
($ip,'172.16.0.0.','255.240.0.0') ||
569 &General
::IpInSubnet
($ip,'192.168.0.0','255.255.0.0'))
571 if ($settings{'BEHINDROUTER'} eq 'FETCH_IP') {
572 my $RealIP = &General
::FetchPublicIp
;
573 $ip = (&General
::validip
($RealIP) ?
$RealIP : 'unavailable');
579 # Translate ICMP code to text
580 # ref: http://www.iana.org/assignments/icmp-parameters
581 sub GetIcmpDescription
($) {
583 my @icmp_description = (
587 'Destination Unreachable',
590 'Alternate Host Address',
593 'Router Advertisement',
594 'Router Solicitation', #10
599 'Information Request',
601 'Address Mask Request',
602 'Address Mask Reply',
603 'Reserved (for Security)',
604 'Reserved (for Robustness Experiment)', #20
615 'Datagram Conversion Error',
616 'Mobile Host Redirect',
617 'IPv6 Where-Are-You',
619 'Mobile Registration Request',
620 'Mobile Registration Reply',
621 'Domain Name Request',
626 if ($index>41) {return 'unknown'} else {return @icmp_description[$index]};