]>
git.ipfire.org Git - people/teissler/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 $
21 use Net
::IPv4Addr
qw(:all);
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('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 # trim space from begin and end
69 $var =~ /([A-Za-z0-9_-]*)/;
86 # write cgi vars to the file.
87 open(FILE
, ">${filename}") or die "Unable to write file $filename";
89 foreach $var (keys %$hash)
91 if ( $var eq "__CGI__"){next;}
93 # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
94 # location of the mouse are submitted as well, this was being written to the settings file causing
95 # some serious grief! This skips the variable.x and variable.y
96 if (!($var =~ /(.x|.y)$/)) {
99 if (!($var =~ /^ACTION/)) {
100 print FILE
"${var}=${val}\n"; }
108 # This function replaces the given hash in the original hash by keeping the old
109 # content and just replacing the new content
111 my $filename = $_[0];
116 readhash
("${filename}", \
%oldhash);
118 foreach $var (keys %$newhash){
119 $oldhash{$var}=$newhash->{$var};
122 # write cgi vars to the file.
123 open(FILE
, ">${filename}") or die "Unable to write file $filename";
125 foreach $var (keys %oldhash)
127 if ( $var eq "__CGI__"){next;}
128 $val = $oldhash{$var};
129 # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
130 # location of the mouse are submitted as well, this was being written to the settings file causing
131 # some serious grief! This skips the variable.x and variable.y
132 if (!($var =~ /(.x|.y)$/)) {
135 if (!($var =~ /^ACTION/)) {
136 print FILE
"${var}=${val}\n"; }
144 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
145 $atime, $mtime, $ctime, $blksize, $blocks) = stat $_[0];
148 my $dset = 0; # Day is set, when > 0
149 my $hset = 0; # Hour is set, when > 0
150 my $mset = 0; # Minute is set, when > 0
152 my $totalsecs = $now - $mtime;
153 my $days = int($totalsecs / 86400);
154 my $totalhours = int($totalsecs / 3600);
155 my $hours = $totalhours % 24;
156 my $totalmins = int($totalsecs / 60);
157 my $mins = $totalmins % 60;
158 my $secs = $totalsecs % 60;
161 ${timestring
} .= ${days
}.' '.$Lang::tr
{'days'}.', ';
165 ${timestring
} .= ${days
}.' '.$Lang::tr
{'day'}.', ';
169 if (($hours > 1) && !($dset)) {
170 ${timestring
} .= ${hours
}.' '.$Lang::tr
{'hours'}.', ';
173 elsif (($hours == 1) && !($dset)) {
174 ${timestring
} .= ${hours
}.' '.$Lang::tr
{'hour'}.', ';
178 ${timestring
} .= ${hours
}.' '.$Lang::tr
{'age shour'}.', ';
182 if ((($mins > 1) || ($mins == 0)) && !($dset || $hset)) {
183 ${timestring
} .= ${mins
}.' '.$Lang::tr
{'minutes'}.', ';
186 elsif (($mins == 1) && !($dset || $hset)) {
187 ${timestring
} .= ${mins
}.' '.$Lang::tr
{'minute'}.', ';
191 ${timestring
} .= ${mins
}.' '.$Lang::tr
{'age sminute'}.', ';
195 if ((($secs > 1) || ($secs == 0)) && !($dset || $hset || $mset)) {
196 ${timestring
} .= ${secs
}.' '.$Lang::tr
{'age seconds'};
198 elsif (($secs == 1) && !($dset || $hset || $mset)) {
199 ${timestring
} .= $secs.' '.$Lang::tr
{'age second'};
201 else { ${timestring
} .= $secs.' '.$Lang::tr
{'age ssecond'}; }
203 return ${timestring
};
210 if (!($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)) {
214 my @octets = ($1, $2, $3, $4);
219 if ($_ < 0 || $_ > 255) {
231 if (&validip
($mask)) {
233 # second part a number?
236 if (!($mask =~ /^\d+$/)) {
238 if ($mask >= 0 && $mask <= 32) {
245 my $ipormask = $_[0];
247 # see if it is a IP only.
248 if (&validip
($ipormask)) {
250 # split it into number and mask.
251 if (!($ipormask =~ /^(.*?)\/(.*?
)$/)) {
255 # first part not a ip?
256 if (!(&validip
($ip))) {
258 return &validmask
($mask);
263 #gets: Subnet in decimal (255.255.255.0)
264 #Gives: 24 (The cidr of network)
265 my ($byte1, $byte2, $byte3, $byte4) = split(/\./, $_[0].".0.0.0.0");
266 my $num = ($byte1 * 16777216) + ($byte2 * 65536) + ($byte3 * 256) + $byte4;
267 my $bin = unpack("B*", pack("N", $num));
268 my $count = ($bin =~ tr/1/1/);
274 #gets: Cidr of network (20-30 for ccd)
275 #Konverts 30 to 255.255.255.252 e.g
277 my $netmask = &Net
::IPv4Addr
::ipv4_cidr2msk
($cidr);
283 #Gets: Ip address or subnetmask in decimal oder CIDR
284 #Gives: What it gets only in CIDR format
289 if ($subnet =~ /^(.*?)\/(.*?
)$/) {
290 ($net,$mask) = split (/\//,$subnet);
296 #Subnet already in decimal and valid?
297 if ($mask=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
298 for (my $i=8;$i<=32;$i++){
299 if (&General
::cidrtosub
($i) eq $mask){
300 if ($full == 0){return $mask;}else{
301 return $net."/".$mask;
306 #Subnet in binary format?
307 if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=8))){
308 if($full == 0){ return &General
::cidrtosub
($mask);}else{
309 return $net."/".&General
::cidrtosub
($mask);
320 #gets: Ip Address or subnetmask in decimal oder CIDR
321 #Gives: What it gets only in CIDR format
326 if ($subnet =~ /^(.*?)\/(.*?
)$/) {
327 ($net,$mask) = split (/\//,$subnet);
332 #Subnet in decimal and valid?
333 if ($mask=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
334 for (my $i=8;$i<=32;$i++){
335 if (&General
::cidrtosub
($i) eq $mask){
336 if ($full == 0){return &General
::subtocidr
($mask);}else{
337 return $net."/".&General
::subtocidr
($mask);
342 #Subnet already in binary format?
343 if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=8))){
344 if($full == 0){ return $mask;}else{
345 return $net."/".$mask;
355 #Gets: IP, CIDR (10.10.10.0-255, 24)
357 my ($ccdip,$ccdsubnet) = @_;
358 my $ip_address_binary = inet_aton
( $ccdip );
359 my $netmask_binary = ~pack("N", (2**(32-$ccdsubnet))-1);
360 my $network_address = inet_ntoa
( $ip_address_binary & $netmask_binary );
361 return $network_address;
366 #Gets: IP in Form ("192.168.0.0/24")
367 #Gives: Broadcastaddress of network
369 my ($ccdip,$ccdsubnet) = split "/",$ccdnet;
370 my $ip_address_binary = inet_aton
( $ccdip );
371 my $netmask_binary = ~pack("N", (2**(32-$ccdsubnet))-1);
372 my $broadcast_address = inet_ntoa
( $ip_address_binary | ~$netmask_binary );
373 return $broadcast_address;
380 if ( $ip =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ ) {
381 $ip_num = (($1*256**3) + ($2*256**2) + ($3*256) + $4);
385 $ip_num = (($1*256**3) + ($2*256**2) + ($3*256) + $4);
394 $ip_num=int($ip_num/256);
396 $ip_num=int($ip_num/256);
398 $ip_num=int($ip_num/256);
400 $ip="$o4.$o3.$o2.$o1";
406 my $decip=&ip2dec
($_[0]);
408 return &dec2ip
($decip);
413 my $decip=&ip2dec
($_[0]);
415 return &dec2ip
($decip);
420 #Gets: Ip address in 192.168.0.0/24 or 192.168.0.0/255.255.255.0 and checks if subnet valid
421 #Gives: True bzw 0 if success or false
425 if (!($ccdnet =~ /^(.*?)\/(.*?
)$/)) {
428 my ($ccdip,$ccdsubnet)=split (/\//, $ccdnet);
430 if ($ccdip=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1>0 && $1<=255 && $2>=0 && $2<=255 && $3>=0 && $3<=255 && $4<=255 ))) {
431 #Subnet in decimal and valid?
432 if ($ccdsubnet=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
433 for (my $i=8;$i<=32;$i++){
434 if (&General
::cidrtosub
($i) eq $ccdsubnet){
438 #Subnet already in binary format?
439 }elsif ($ccdsubnet=~/^(\d{1,2})$/ && (($1<=32 && $1>=8))){
456 my ($ip,$cidr)=split(/\//,$ccdnet);
457 $cidr=&iporsubtocidr
($cidr);
458 #get OVPN-Subnet (dynamic range)
460 &readhash
("${General::swroot}/ovpn/settings", \
%ovpnconf);
461 my ($ovpnip,$ovpncidr)= split (/\//,$ovpnconf{'DOVPN_SUBNET'});
462 $ovpncidr=&iporsubtocidr
($ovpncidr);
463 #check if we try to use same network as ovpn server
464 if ("$ip/$cidr" eq "$ovpnip/$ovpncidr") {
465 $errormessage=$errormessage.$Lang::tr
{'ccd err isovpnnet'}."<br>";
466 return $errormessage;
468 #check if we use a network-name/subnet that already exists
469 &readhasharray
("${General::swroot}/ovpn/ccd.conf", \
%ccdconfhash);
470 foreach my $key (keys %ccdconfhash) {
471 @ccdconf=split(/\//,$ccdconfhash{$key}[1]);
472 if ($ccdname eq $ccdconfhash{$key}[0])
474 $errormessage=$errormessage.$Lang::tr
{'ccd err nameexist'}."<br>";
475 return $errormessage;
477 my ($newip,$newsub) = split(/\//,$ccdnet);
478 if (&IpInSubnet
($newip,$ccdconf[0],&iporsubtodec
($ccdconf[1])))
480 $errormessage=$errormessage.$Lang::tr
{'ccd err issubnet'}."<br>";
481 return $errormessage;
484 #check if we use a ipsec right network which is already defined
486 &General
::readhasharray
("${General::swroot}/vpn/config", \
%ipsecconf);
487 foreach my $key (keys %ipsecconf){
488 if ($ipsecconf{$key}[11] ne ''){
489 my ($ipsecip,$ipsecsub) = split (/\//, $ipsecconf{$key}[11]);
490 $ipsecsub=&iporsubtodec
($ipsecsub);
491 if($ipsecconf{$key}[1] ne $ccdname){
492 if ( &IpInSubnet
($ip,$ipsecip,$ipsecsub) ){
493 $errormessage=$Lang::tr
{'ccd err isipsecnet'}." Name: $ipsecconf{$key}[1]";
494 return $errormessage;
499 #check if we use one of ipfire's networks (green,orange,blue)
501 &readhash
("${General::swroot}/ethernet/settings", \
%ownnet);
502 if (($ownnet{'GREEN_NETADDRESS'} ne '' && $ownnet{'GREEN_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet
($ownnet{'GREEN_NETADDRESS'},$ip,&iporsubtodec
($cidr))){ $errormessage=$Lang::tr
{'ccd err green'};return $errormessage;}
503 if (($ownnet{'ORANGE_NETADDRESS'} ne '' && $ownnet{'ORANGE_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet
($ownnet{'ORANGE_NETADDRESS'},$ip,&iporsubtodec
($cidr))){ $errormessage=$Lang::tr
{'ccd err orange'};return $errormessage;}
504 if (($ownnet{'BLUE_NETADDRESS'} ne '' && $ownnet{'BLUE_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet
($ownnet{'BLUE_NETADDRESS'},$ip,&iporsubtodec
($cidr))){ $errormessage=$Lang::tr
{'ccd err blue'};return $errormessage;}
505 if (($ownnet{'RED_NETADDRESS'} ne '' && $ownnet{'RED_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet
($ownnet{'RED_NETADDRESS'},$ip,&iporsubtodec
($cidr))){ $errormessage=$Lang::tr
{'ccd err red'};return $errormessage;}
517 if ($_ >= 1 && $_ <= 65535) {
530 if ($_ == 53 || $_ == 222 || $_ == 444 || $_ == 81 ) {
532 elsif ($_ >= 1 && $_ <= 65535) {
539 my $checkmac = $_[0];
540 my $ot = '[0-9a-f]{2}'; # 2 Hex digits (one octet)
541 if ($checkmac !~ /^$ot:$ot:$ot:$ot:$ot:$ot$/i)
550 # Checks a hostname against RFC1035
551 my $hostname = $_[0];
553 # Each part should be at least two characters in length
554 # but no more than 63 characters
555 if (length ($hostname) < 1 || length ($hostname) > 63) {
557 # Only valid characters are a-z, A-Z, 0-9 and -
558 if ($hostname !~ /^[a-zA-Z0-9-\s]*$/) {
560 # First character can only be a letter or a digit
561 if (substr ($hostname, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
563 # Last character can only be a letter or a digit
564 if (substr ($hostname, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
573 # Checks a domain name against RFC1035
574 my $domainname = $_[0];
575 my @parts = split (/\./, $domainname); # Split hostname at the '.'
577 foreach $part (@parts) {
578 # Each part should be at least two characters in length
579 # but no more than 63 characters
580 if (length ($part) < 2 || length ($part) > 63) {
582 # Only valid characters are a-z, A-Z, 0-9 and -
583 if ($part !~ /^[a-zA-Z0-9-]*$/) {
585 # First character can only be a letter or a digit
586 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
588 # Last character can only be a letter or a digit
589 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
599 # Checks a fully qualified domain name against RFC1035
601 my @parts = split (/\./, $fqdn); # Split hostname at the '.'
602 if (scalar(@parts) < 2) { # At least two parts should
603 return 0;} # exist in a FQDN
604 # (i.e. hostname.domain)
605 foreach $part (@parts) {
606 # Each part should be at least one character in length
607 # but no more than 63 characters
608 if (length ($part) < 1 || length ($part) > 63) {
610 # Only valid characters are a-z, A-Z, 0-9 and -
611 if ($part !~ /^[a-zA-Z0-9-]*$/) {
613 # First character can only be a letter or a digit
614 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
616 # Last character can only be a letter or a digit
617 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
623 sub validportrange
# used to check a port range
625 my $port = $_[0]; # port values
626 $port =~ tr/-/:/; # replace all - with colons just in case someone used -
627 my $srcdst = $_[1]; # is it a source or destination port
629 if (!($port =~ /^(\d+)\:(\d+)$/)) {
631 if (!(&validport
($port))) {
632 if ($srcdst eq 'src'){
633 return $Lang::tr
{'source port numbers'};
635 return $Lang::tr
{'destination port numbers'};
641 my @ports = ($1, $2);
643 if ($srcdst eq 'src'){
644 return $Lang::tr
{'bad source range'};
646 return $Lang::tr
{'bad destination range'};
651 if (!(&validport
($_))) {
652 if ($srcdst eq 'src'){
653 return $Lang::tr
{'source port numbers'};
655 return $Lang::tr
{'destination port numbers'};
663 # Test if IP is within a subnet
664 # Call: IpInSubnet (Addr, Subnet, Subnet Mask)
665 # Subnet can be an IP of the subnet: 10.0.0.0 or 10.0.0.1
666 # Everything in dottted notation
670 my $ip = unpack('N', &Socket
::inet_aton
(shift));
671 my $start = unpack('N', &Socket
::inet_aton
(shift));
672 my $mask = unpack('N', &Socket
::inet_aton
(shift));
673 $start &= $mask; # base of subnet...
674 my $end = $start + ~$mask;
675 return (($ip >= $start) && ($ip <= $end));
679 # Return the following IP (IP+1) in dotted notation.
680 # Call: NextIP ('1.1.1.1');
685 return &Socket
::inet_ntoa
( pack("N", 1 + unpack('N', &Socket
::inet_aton
(shift))
691 return &Socket
::inet_ntoa
( pack("N", 4 + unpack('N', &Socket
::inet_aton
(shift))
697 my ($ip,$cidr) = &Net
::IPv4Addr
::ipv4_parse
(shift);
703 my ($ip,$cidr) = &Net
::IPv4Addr
::ipv4_parse
(shift);
704 my $netmask = &Net
::IPv4Addr
::ipv4_cidr2msk
($cidr);
705 return "$ip\/$netmask";
711 return 0 if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/ );
712 return 0 if ( $mail =~ /^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/);
713 return 0 if ( $mail !~ /([0-9a-zA-Z]{1})\@./ );
714 return 0 if ( $mail !~ /.\@([0-9a-zA-Z]{1})/ );
715 return 0 if ( $mail =~ /.\.\-.|.\-\..|.\.\..|.\-\-./g );
716 return 0 if ( $mail =~ /.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./g );
717 return 0 if ( $mail !~ /\.([a-zA-Z]{2,4})$/ );
722 # Currently only vpnmain use this three procs (readhasharray, writehasharray, findhasharray)
723 # The 'key' used is numeric but is perfectly unneeded! This will to be removed so don't use
724 # this code. Vpnmain will be splitted in parts: x509/pki, connection ipsec, connection other,... .
727 my ($filename, $hash) = @_;
730 open(FILE
, $filename) or die "Unable to read file $filename";
733 my ($key, $rest, @temp);
735 ($key, $rest) = split (/,/, $_, 2);
736 if ($key =~ /^[0-9]+$/) {
737 @temp = split (/,/, $rest);
738 $hash->{$key} = \
@temp;
746 my ($filename, $hash) = @_;
747 my ($key, @temp, $i);
749 open(FILE
, ">$filename") or die "Unable to write to file $filename";
751 foreach $key (keys %$hash) {
752 if ($key =~ /^[0-9]+$/) {
754 foreach $i (0 .. $#{$hash->{$key}}) {
755 print FILE
",$hash->{$key}[$i]";
764 sub findhasharraykey
{
765 foreach my $i (1 .. 1000000) {
766 if ( ! exists $_[0]{$i}) {
773 # Darren Critchley - darrenc@telus.net - (c) 2003
774 # &srtarray(SortOrder, AlphaNumeric, SortDirection, ArrayToBeSorted)
775 # This subroutine will take the following parameters:
776 # ColumnNumber = the column which you want to sort on, starts at 1
777 # AlphaNumberic = a or n (lowercase) defines whether the sort should be alpha or numberic
778 # SortDirection = asc or dsc (lowercase) Ascending or Descending sort
779 # ArrayToBeSorted = the array that wants sorting
781 # Returns an array that is sorted to your specs
783 # If SortOrder is greater than the elements in array, then it defaults to the first element
786 my ($colno, $alpnum, $srtdir, @tobesorted) = @_;
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);
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
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'; }
806 foreach $line (@tobesorted)
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;
816 for ($ctr=0; $ctr < $ttlitems ; $ctr++) {
817 $newline=$newline . $temp[$ctr] . ",";
820 push(@tmparray,$newline);
823 if ($alpnum eq 'n') {
824 @tmparray = sort {$a <=> $b} @tmparray;
826 @tmparray = (sort @tmparray);
828 foreach $line (@tmparray)
832 my @temp = split(/\,/,$line);
833 my $tmpholder = $temp[0];
834 $temp[0] = $temp[$colno];
835 $temp[$colno] = $tmpholder;
837 for ($ctr=0; $ctr < $ttlitems ; $ctr++){
838 $newline=$newline . $temp[$ctr] . ",";
841 push(@srtedarray,$newline);
845 if ($srtdir eq 'dsc') {
846 @tmparray = reverse(@srtedarray);
849 return (@srtedarray);
855 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
856 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
857 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
858 Net
::SSLeay
::set_proxy
($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
860 my $user_agent = &MakeUserAgent
();
861 my ($out, $response) = Net
::SSLeay
::get_http
( 'checkip4.dns.lightningwirelabs.com',
864 Net
::SSLeay
::make_headers
('User-Agent' => $user_agent )
866 if ($response =~ m
%HTTP/1\
.. 200 OK
%) {
867 $out =~ /Your IP address is: (\d+.\d+.\d+.\d+)/;
874 # Check if hostname.domain provided have IP provided
875 # use gethostbyname to verify that
881 # 1 IP matches host.domain
884 sub DyndnsServiceSync
($;$;$) {
886 my ($ip,$hostName,$domain) = @_;
889 #fix me no ip GROUP, what is the name ?
890 $hostName =~ s/$General::noipprefix//;
891 if ($hostName) { #may be empty
892 $hostName = "$hostName.$domain";
893 @addresses = gethostbyname($hostName);
896 if ($addresses[0] eq '') { # nothing returned ?
897 $hostName = $domain; # try resolving with domain only
898 @addresses = gethostbyname($hostName);
901 if ($addresses[0] ne '') { # got something ?
902 #&General::log("name:$addresses[0], alias:$addresses[1]");
903 # Build clear text list of IP
904 @addresses = map ( &Socket
::inet_ntoa
($_), @addresses[4..$#addresses]);
905 if (grep (/$ip/, @addresses)) {
912 # This sub returns the red IP used to compare in DyndnsServiceSync
916 &General
::readhash
("${General::swroot}/ddns/settings", \
%settings);
918 open(IP
, "${General::swroot}/red/local-ipaddress") or return 'unavailable';
923 # 100.64.0.0/10 is reserved for dual-stack lite (http://tools.ietf.org/html/rfc6598).
924 if (&General
::IpInSubnet
($ip,'10.0.0.0','255.0.0.0') ||
925 &General
::IpInSubnet
($ip,'172.16.0.0.','255.240.0.0') ||
926 &General
::IpInSubnet
($ip,'192.168.0.0','255.255.0.0') ||
927 &General
::IpInSubnet
($ip,'100.64.0.0', '255.192.0.0'))
929 if ($settings{'BEHINDROUTER'} eq 'FETCH_IP') {
930 my $RealIP = &General
::FetchPublicIp
;
931 $ip = (&General
::validip
($RealIP) ?
$RealIP : 'unavailable');
937 # Translate ICMP code to text
938 # ref: http://www.iana.org/assignments/icmp-parameters
939 sub GetIcmpDescription
($) {
941 my @icmp_description = (
945 'Destination Unreachable',
948 'Alternate Host Address',
951 'Router Advertisement',
952 'Router Solicitation', #10
957 'Information Request',
959 'Address Mask Request',
960 'Address Mask Reply',
961 'Reserved (for Security)',
962 'Reserved (for Robustness Experiment)', #20
973 'Datagram Conversion Error',
974 'Mobile Host Redirect',
975 'IPv6 Where-Are-You',
977 'Mobile Registration Request',
978 'Mobile Registration Reply',
979 'Domain Name Request',
984 if ($index>41) {return 'unknown'} else {return @icmp_description[$index]};
987 sub GetCoreUpdateVersion
() {
990 open(FILE
, "/opt/pakfire/db/core/mine");
1000 sub MakeUserAgent
() {
1001 my $user_agent = "IPFire/$General::version";
1003 my $core_update = &GetCoreUpdateVersion
();
1004 if ($core_update ne "") {
1005 $user_agent .= "/$core_update";
1011 sub RedIsWireless
() {
1012 # This function checks if a network device is a wireless device.
1015 &readhash
("${General::swroot}/ethernet/settings", \
%settings);
1017 # Find the name of the network device.
1018 my $device = $settings{'RED_DEV'};
1020 # Exit, if no device is configured.
1021 return 0 if ($device eq "");
1023 # Return 1 if the device is a wireless one.
1024 my $path = "/sys/class/net/$device/wireless";
1029 # Otherwise return zero.