]>
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 $
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);
42 sub setup_default_networks
45 my $defaultNetworks = shift;
47 &readhash
("/var/ipfire/ethernet/settings", \
%netsettings);
49 # Get current defined networks (Red, Green, Blue, Orange)
50 $defaultNetworks->{$Lang::tr{'fwhost any'}}{'IPT'} = "0.0.0.0/0.0.0.0";
51 $defaultNetworks->{$Lang::tr{'fwhost any'}}{'NAME'} = "ALL";
53 $defaultNetworks->{$Lang::tr{'green'}}{'IPT'} = "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
54 $defaultNetworks->{$Lang::tr{'green'}}{'NET'} = "$netsettings{'GREEN_ADDRESS'}";
55 $defaultNetworks->{$Lang::tr{'green'}}{'NAME'} = "GREEN";
57 if ($netsettings{'RED_DEV'} ne ''){
58 $defaultNetworks->{$Lang::tr{'fwdfw red'}}{'IPT'} = "$netsettings{'RED_NETADDRESS'}/$netsettings{'RED_NETMASK'}";
59 $defaultNetworks->{$Lang::tr{'fwdfw red'}}{'NET'} = "$netsettings{'RED_ADDRESS'}";
60 $defaultNetworks->{$Lang::tr{'fwdfw red'}}{'NAME'} = "RED";
62 if ($netsettings{'ORANGE_DEV'} ne ''){
63 $defaultNetworks->{$Lang::tr{'orange'}}{'IPT'} = "$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}";
64 $defaultNetworks->{$Lang::tr{'orange'}}{'NET'} = "$netsettings{'ORANGE_ADDRESS'}";
65 $defaultNetworks->{$Lang::tr{'orange'}}{'NAME'} = "ORANGE";
68 if ($netsettings{'BLUE_DEV'} ne ''){
69 $defaultNetworks->{$Lang::tr{'blue'}}{'IPT'} = "$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}";
70 $defaultNetworks->{$Lang::tr{'blue'}}{'NET'} = "$netsettings{'BLUE_ADDRESS'}";
71 $defaultNetworks->{$Lang::tr{'blue'}}{'NAME'} = "BLUE";
75 $defaultNetworks->{'IPFire'}{'NAME'} = "IPFire";
78 if(-e
"${General::swroot}/ovpn/settings")
80 my %ovpnSettings = ();
81 &readhash
("${General::swroot}/ovpn/settings", \
%ovpnSettings);
84 if(defined($ovpnSettings{'DOVPN_SUBNET'}))
86 my ($ip,$sub) = split(/\//,$ovpnSettings{'DOVPN_SUBNET'});
87 $sub=&General
::iporsubtocidr
($sub);
88 my @tempovpnsubnet = split("\/", $ovpnSettings{'DOVPN_SUBNET'});
89 $defaultNetworks->{'OpenVPN ' ."($ip/$sub)"}{'ADR'} = $tempovpnsubnet[0];
90 $defaultNetworks->{'OpenVPN ' ."($ip/$sub)"}{'NAME'} = "OpenVPN-Dyn";
94 if(-e
"${General::swroot}/vpn/settings")
96 my %ipsecsettings = ();
97 &readhash
("${General::swroot}/vpn/settings", \
%ipsecsettings);
98 if($ipsecsettings{'RW_NET'} ne '')
100 my ($ip,$sub) = split(/\//,$ipsecsettings{'RW_NET'});
101 $sub=&General
::iporsubtocidr
($sub);
102 my @tempipsecsubnet = split("\/", $ipsecsettings{'RW_NET'});
103 $defaultNetworks->{'IPsec RW ' .$ip."/".$sub}{'ADR'} = $tempipsecsubnet[0];
104 $defaultNetworks->{'IPsec RW ' .$ip."/".$sub}{'NAME'} = "IPsec RW";
105 $defaultNetworks->{'IPsec RW ' .$ip."/".$sub}{'NET'} = &getnextip
($ip);
112 my $defaultNetworks = shift;
113 open(FILE
, "${General::swroot}/ethernet/aliases") or die 'Unable to open aliases file.';
114 my @current = <FILE
>;
117 foreach my $line (@current)
121 my @temp = split(/\,/,$line);
122 if ($temp[2] eq '') {
123 $temp[2] = "Alias $ctr : $temp[0]";
125 $defaultNetworks->{$temp[2]}{'IPT'} = "$temp[0]";
126 $defaultNetworks->{$temp[2]}{'NET'} = "$temp[0]";
135 my $filename = $_[0];
140 # Some ipcop code expects that readhash 'complete' the hash if new entries
141 # are presents. Not clear it !!!
144 open(FILE
, $filename) or die "Unable to read file $filename";
149 ($var, $val) = split /=/, $_, 2;
155 # Untaint variables read from hash
156 # trim space from begin and end
159 $var =~ /([A-Za-z0-9_-]*)/;
163 $hash->{$var} = $val;
172 my $filename = $_[0];
176 # write cgi vars to the file.
177 open(FILE
, ">${filename}") or die "Unable to write file $filename";
179 foreach $var (keys %$hash)
181 if ( $var eq "__CGI__"){next;}
182 $val = $hash->{$var};
183 # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
184 # location of the mouse are submitted as well, this was being written to the settings file causing
185 # some serious grief! This skips the variable.x and variable.y
186 if (!($var =~ /(.x|.y)$/)) {
189 if (!($var =~ /^ACTION/)) {
190 print FILE
"${var}=${val}\n"; }
198 # This function replaces the given hash in the original hash by keeping the old
199 # content and just replacing the new content
201 my $filename = $_[0];
206 readhash
("${filename}", \
%oldhash);
208 foreach $var (keys %$newhash){
209 $oldhash{$var}=$newhash->{$var};
212 # write cgi vars to the file.
213 open(FILE
, ">${filename}") or die "Unable to write file $filename";
215 foreach $var (keys %oldhash)
217 if ( $var eq "__CGI__"){next;}
218 $val = $oldhash{$var};
219 # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
220 # location of the mouse are submitted as well, this was being written to the settings file causing
221 # some serious grief! This skips the variable.x and variable.y
222 if (!($var =~ /(.x|.y)$/)) {
225 if (!($var =~ /^ACTION/)) {
226 print FILE
"${var}=${val}\n"; }
234 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
235 $atime, $mtime, $ctime, $blksize, $blocks) = stat $_[0];
238 my $dset = 0; # Day is set, when > 0
239 my $hset = 0; # Hour is set, when > 0
240 my $mset = 0; # Minute is set, when > 0
242 my $totalsecs = $now - $mtime;
243 my $days = int($totalsecs / 86400);
244 my $totalhours = int($totalsecs / 3600);
245 my $hours = $totalhours % 24;
246 my $totalmins = int($totalsecs / 60);
247 my $mins = $totalmins % 60;
248 my $secs = $totalsecs % 60;
251 ${timestring
} .= ${days
}.' '.$Lang::tr
{'days'}.', ';
255 ${timestring
} .= ${days
}.' '.$Lang::tr
{'day'}.', ';
259 if (($hours > 1) && !($dset)) {
260 ${timestring
} .= ${hours
}.' '.$Lang::tr
{'hours'}.', ';
263 elsif (($hours == 1) && !($dset)) {
264 ${timestring
} .= ${hours
}.' '.$Lang::tr
{'hour'}.', ';
268 ${timestring
} .= ${hours
}.' '.$Lang::tr
{'age shour'}.', ';
272 if ((($mins > 1) || ($mins == 0)) && !($dset || $hset)) {
273 ${timestring
} .= ${mins
}.' '.$Lang::tr
{'minutes'}.', ';
276 elsif (($mins == 1) && !($dset || $hset)) {
277 ${timestring
} .= ${mins
}.' '.$Lang::tr
{'minute'}.', ';
281 ${timestring
} .= ${mins
}.' '.$Lang::tr
{'age sminute'}.', ';
285 if ((($secs > 1) || ($secs == 0)) && !($dset || $hset || $mset)) {
286 ${timestring
} .= ${secs
}.' '.$Lang::tr
{'age seconds'};
288 elsif (($secs == 1) && !($dset || $hset || $mset)) {
289 ${timestring
} .= $secs.' '.$Lang::tr
{'age second'};
291 else { ${timestring
} .= $secs.' '.$Lang::tr
{'age ssecond'}; }
293 return ${timestring
};
300 if (!($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)) {
304 my @octets = ($1, $2, $3, $4);
309 if ($_ < 0 || $_ > 255) {
321 if (&validip
($mask)) {
323 # second part a number?
326 if (!($mask =~ /^\d+$/)) {
328 if ($mask >= 0 && $mask <= 32) {
335 my $ipormask = $_[0];
337 # see if it is a IP only.
338 if (&validip
($ipormask)) {
340 # split it into number and mask.
341 if (!($ipormask =~ /^(.*?)\/(.*?
)$/)) {
345 # first part not a ip?
346 if (!(&validip
($ip))) {
348 return &validmask
($mask);
353 #gets: Subnet in decimal (255.255.255.0)
354 #Gives: 24 (The cidr of network)
355 my ($byte1, $byte2, $byte3, $byte4) = split(/\./, $_[0].".0.0.0.0");
356 my $num = ($byte1 * 16777216) + ($byte2 * 65536) + ($byte3 * 256) + $byte4;
357 my $bin = unpack("B*", pack("N", $num));
358 my $count = ($bin =~ tr/1/1/);
364 #gets: Cidr of network (20-30 for ccd)
365 #Konverts 30 to 255.255.255.252 e.g
367 my $netmask = &Net
::IPv4Addr
::ipv4_cidr2msk
($cidr);
373 #Gets: Ip address or subnetmask in decimal oder CIDR
374 #Gives: What it gets only in CIDR format
379 if ($subnet =~ /^(.*?)\/(.*?
)$/) {
380 ($net,$mask) = split (/\//,$subnet);
386 #Subnet already in decimal and valid?
387 if ($mask=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
388 for (my $i=8;$i<=32;$i++){
389 if (&General
::cidrtosub
($i) eq $mask){
390 if ($full == 0){return $mask;}else{
391 return $net."/".$mask;
396 #Subnet in binary format?
397 if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=8))){
398 if($full == 0){ return &General
::cidrtosub
($mask);}else{
399 return $net."/".&General
::cidrtosub
($mask);
410 #gets: Ip Address or subnetmask in decimal oder CIDR
411 #Gives: What it gets only in CIDR format
416 if ($subnet =~ /^(.*?)\/(.*?
)$/) {
417 ($net,$mask) = split (/\//,$subnet);
422 #Subnet in decimal and valid?
423 if ($mask=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
424 for (my $i=8;$i<=32;$i++){
425 if (&General
::cidrtosub
($i) eq $mask){
426 if ($full == 0){return &General
::subtocidr
($mask);}else{
427 return $net."/".&General
::subtocidr
($mask);
432 #Subnet already in binary format?
433 if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=8))){
434 if($full == 0){ return $mask;}else{
435 return $net."/".$mask;
445 #Gets: IP, CIDR (10.10.10.0-255, 24)
447 my ($ccdip,$ccdsubnet) = @_;
448 my $ip_address_binary = inet_aton
( $ccdip );
449 my $netmask_binary = ~pack("N", (2**(32-$ccdsubnet))-1);
450 my $network_address = inet_ntoa
( $ip_address_binary & $netmask_binary );
451 return $network_address;
456 #Gets: IP in Form ("192.168.0.0/24")
457 #Gives: Broadcastaddress of network
459 my ($ccdip,$ccdsubnet) = split "/",$ccdnet;
460 my $ip_address_binary = inet_aton
( $ccdip );
461 my $netmask_binary = ~pack("N", (2**(32-$ccdsubnet))-1);
462 my $broadcast_address = inet_ntoa
( $ip_address_binary | ~$netmask_binary );
463 return $broadcast_address;
470 if ( $ip =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ ) {
471 $ip_num = (($1*256**3) + ($2*256**2) + ($3*256) + $4);
475 $ip_num = (($1*256**3) + ($2*256**2) + ($3*256) + $4);
484 $ip_num=int($ip_num/256);
486 $ip_num=int($ip_num/256);
488 $ip_num=int($ip_num/256);
490 $ip="$o4.$o3.$o2.$o1";
496 my $decip=&ip2dec
($_[0]);
498 return &dec2ip
($decip);
503 my $decip=&ip2dec
($_[0]);
505 return &dec2ip
($decip);
510 #Gets: Ip address in 192.168.0.0/24 or 192.168.0.0/255.255.255.0 and checks if subnet valid
511 #Gives: True bzw 0 if success or false
515 if (!($ccdnet =~ /^(.*?)\/(.*?
)$/)) {
518 my ($ccdip,$ccdsubnet)=split (/\//, $ccdnet);
520 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 ))) {
521 #Subnet in decimal and valid?
522 if ($ccdsubnet=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
523 for (my $i=8;$i<=32;$i++){
524 if (&General
::cidrtosub
($i) eq $ccdsubnet){
528 #Subnet already in binary format?
529 }elsif ($ccdsubnet=~/^(\d{1,2})$/ && (($1<=32 && $1>=8))){
546 my ($ip,$cidr)=split(/\//,$ccdnet);
547 $cidr=&iporsubtocidr
($cidr);
548 #get OVPN-Subnet (dynamic range)
550 &readhash
("${General::swroot}/ovpn/settings", \
%ovpnconf);
551 my ($ovpnip,$ovpncidr)= split (/\//,$ovpnconf{'DOVPN_SUBNET'});
552 $ovpncidr=&iporsubtocidr
($ovpncidr);
553 #check if we try to use same network as ovpn server
554 if ("$ip/$cidr" eq "$ovpnip/$ovpncidr") {
555 $errormessage=$errormessage.$Lang::tr
{'ccd err isovpnnet'}."<br>";
556 return $errormessage;
558 #check if we use a network-name/subnet that already exists
559 &readhasharray
("${General::swroot}/ovpn/ccd.conf", \
%ccdconfhash);
560 foreach my $key (keys %ccdconfhash) {
561 @ccdconf=split(/\//,$ccdconfhash{$key}[1]);
562 if ($ccdname eq $ccdconfhash{$key}[0])
564 $errormessage=$errormessage.$Lang::tr
{'ccd err nameexist'}."<br>";
565 return $errormessage;
567 my ($newip,$newsub) = split(/\//,$ccdnet);
568 if (&IpInSubnet
($newip,$ccdconf[0],&iporsubtodec
($ccdconf[1])))
570 $errormessage=$errormessage.$Lang::tr
{'ccd err issubnet'}."<br>";
571 return $errormessage;
574 #check if we use a ipsec right network which is already defined
576 &General
::readhasharray
("${General::swroot}/vpn/config", \
%ipsecconf);
577 foreach my $key (keys %ipsecconf){
578 if ($ipsecconf{$key}[11] ne ''){
579 my ($ipsecip,$ipsecsub) = split (/\//, $ipsecconf{$key}[11]);
580 $ipsecsub=&iporsubtodec
($ipsecsub);
581 if($ipsecconf{$key}[1] ne $ccdname){
582 if ( &IpInSubnet
($ip,$ipsecip,$ipsecsub) ){
583 $errormessage=$Lang::tr
{'ccd err isipsecnet'}." Name: $ipsecconf{$key}[1]";
584 return $errormessage;
589 #check if we use one of ipfire's networks (green,orange,blue)
591 &readhash
("${General::swroot}/ethernet/settings", \
%ownnet);
592 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;}
593 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;}
594 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;}
595 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;}
607 if ($_ >= 1 && $_ <= 65535) {
620 if ($_ == 53 || $_ == 222 || $_ == 444 || $_ == 81 ) {
622 elsif ($_ >= 1 && $_ <= 65535) {
629 my $checkmac = $_[0];
630 my $ot = '[0-9a-f]{2}'; # 2 Hex digits (one octet)
631 if ($checkmac !~ /^$ot:$ot:$ot:$ot:$ot:$ot$/i)
640 # Checks a hostname against RFC1035
641 my $hostname = $_[0];
643 # Each part should be at least two characters in length
644 # but no more than 63 characters
645 if (length ($hostname) < 1 || length ($hostname) > 63) {
647 # Only valid characters are a-z, A-Z, 0-9 and -
648 if ($hostname !~ /^[a-zA-Z0-9-\s]*$/) {
650 # First character can only be a letter or a digit
651 if (substr ($hostname, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
653 # Last character can only be a letter or a digit
654 if (substr ($hostname, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
663 # Checks a domain name against RFC1035
664 my $domainname = $_[0];
665 my @parts = split (/\./, $domainname); # Split hostname at the '.'
667 foreach $part (@parts) {
668 # Each part should be at least two characters in length
669 # but no more than 63 characters
670 if (length ($part) < 2 || length ($part) > 63) {
672 # Only valid characters are a-z, A-Z, 0-9 and -
673 if ($part !~ /^[a-zA-Z0-9-]*$/) {
675 # First character can only be a letter or a digit
676 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
678 # Last character can only be a letter or a digit
679 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
689 # Checks a fully qualified domain name against RFC1035
691 my @parts = split (/\./, $fqdn); # Split hostname at the '.'
692 if (scalar(@parts) < 2) { # At least two parts should
693 return 0;} # exist in a FQDN
694 # (i.e. hostname.domain)
695 foreach $part (@parts) {
696 # Each part should be at least one character in length
697 # but no more than 63 characters
698 if (length ($part) < 1 || length ($part) > 63) {
700 # Only valid characters are a-z, A-Z, 0-9 and -
701 if ($part !~ /^[a-zA-Z0-9-]*$/) {
703 # First character can only be a letter or a digit
704 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
706 # Last character can only be a letter or a digit
707 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
713 sub validportrange
# used to check a port range
715 my $port = $_[0]; # port values
716 $port =~ tr/-/:/; # replace all - with colons just in case someone used -
717 my $srcdst = $_[1]; # is it a source or destination port
719 if (!($port =~ /^(\d+)\:(\d+)$/)) {
721 if (!(&validport
($port))) {
722 if ($srcdst eq 'src'){
723 return $Lang::tr
{'source port numbers'};
725 return $Lang::tr
{'destination port numbers'};
731 my @ports = ($1, $2);
733 if ($srcdst eq 'src'){
734 return $Lang::tr
{'bad source range'};
736 return $Lang::tr
{'bad destination range'};
741 if (!(&validport
($_))) {
742 if ($srcdst eq 'src'){
743 return $Lang::tr
{'source port numbers'};
745 return $Lang::tr
{'destination port numbers'};
753 # Test if IP is within a subnet
754 # Call: IpInSubnet (Addr, Subnet, Subnet Mask)
755 # Subnet can be an IP of the subnet: 10.0.0.0 or 10.0.0.1
756 # Everything in dottted notation
760 my $ip = unpack('N', &Socket
::inet_aton
(shift));
761 my $start = unpack('N', &Socket
::inet_aton
(shift));
762 my $mask = unpack('N', &Socket
::inet_aton
(shift));
763 $start &= $mask; # base of subnet...
764 my $end = $start + ~$mask;
765 return (($ip >= $start) && ($ip <= $end));
769 # Return the following IP (IP+1) in dotted notation.
770 # Call: NextIP ('1.1.1.1');
775 return &Socket
::inet_ntoa
( pack("N", 1 + unpack('N', &Socket
::inet_aton
(shift))
781 return &Socket
::inet_ntoa
( pack("N", 4 + unpack('N', &Socket
::inet_aton
(shift))
787 my ($ip,$cidr) = &Net
::IPv4Addr
::ipv4_parse
(shift);
793 my ($ip,$cidr) = &Net
::IPv4Addr
::ipv4_parse
(shift);
794 my $netmask = &Net
::IPv4Addr
::ipv4_cidr2msk
($cidr);
795 return "$ip\/$netmask";
801 return 0 if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/ );
802 return 0 if ( $mail =~ /^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/);
803 return 0 if ( $mail !~ /([0-9a-zA-Z]{1})\@./ );
804 return 0 if ( $mail !~ /.\@([0-9a-zA-Z]{1})/ );
805 return 0 if ( $mail =~ /.\.\-.|.\-\..|.\.\..|.\-\-./g );
806 return 0 if ( $mail =~ /.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./g );
807 return 0 if ( $mail !~ /\.([a-zA-Z]{2,4})$/ );
812 # Currently only vpnmain use this three procs (readhasharray, writehasharray, findhasharray)
813 # The 'key' used is numeric but is perfectly unneeded! This will to be removed so don't use
814 # this code. Vpnmain will be splitted in parts: x509/pki, connection ipsec, connection other,... .
817 my ($filename, $hash) = @_;
820 open(FILE
, $filename) or die "Unable to read file $filename";
823 my ($key, $rest, @temp);
825 ($key, $rest) = split (/,/, $_, 2);
826 if ($key =~ /^[0-9]+$/) {
827 @temp = split (/,/, $rest);
828 $hash->{$key} = \
@temp;
836 my ($filename, $hash) = @_;
837 my ($key, @temp, $i);
839 open(FILE
, ">$filename") or die "Unable to write to file $filename";
841 foreach $key (keys %$hash) {
842 if ($key =~ /^[0-9]+$/) {
844 foreach $i (0 .. $#{$hash->{$key}}) {
845 print FILE
",$hash->{$key}[$i]";
854 sub findhasharraykey
{
855 foreach my $i (1 .. 1000000) {
856 if ( ! exists $_[0]{$i}) {
863 # Darren Critchley - darrenc@telus.net - (c) 2003
864 # &srtarray(SortOrder, AlphaNumeric, SortDirection, ArrayToBeSorted)
865 # This subroutine will take the following parameters:
866 # ColumnNumber = the column which you want to sort on, starts at 1
867 # AlphaNumberic = a or n (lowercase) defines whether the sort should be alpha or numberic
868 # SortDirection = asc or dsc (lowercase) Ascending or Descending sort
869 # ArrayToBeSorted = the array that wants sorting
871 # Returns an array that is sorted to your specs
873 # If SortOrder is greater than the elements in array, then it defaults to the first element
876 my ($colno, $alpnum, $srtdir, @tobesorted) = @_;
882 my $ttlitems = scalar @tobesorted; # want to know the number of rows in the passed array
883 if ($ttlitems < 1){ # if no items, don't waste our time lets leave
884 return (@tobesorted);
886 my @tmp = split(/\,/,$tobesorted[0]);
887 $ttlitems = scalar @tmp; # this should be the number of elements in each row of the passed in array
889 # Darren Critchley - validate parameters
890 if ($colno > $ttlitems){$colno = '1';}
891 $colno--; # remove one from colno to deal with arrays starting at 0
892 if($colno < 0){$colno = '0';}
893 if ($alpnum ne '') { $alpnum = lc($alpnum); } else { $alpnum = 'a'; }
894 if ($srtdir ne '') { $srtdir = lc($srtdir); } else { $srtdir = 'src'; }
896 foreach $line (@tobesorted)
900 my @temp = split(/\,/,$line);
901 # Darren Critchley - juggle the fields so that the one we want to sort on is first
902 my $tmpholder = $temp[0];
903 $temp[0] = $temp[$colno];
904 $temp[$colno] = $tmpholder;
906 for ($ctr=0; $ctr < $ttlitems ; $ctr++) {
907 $newline=$newline . $temp[$ctr] . ",";
910 push(@tmparray,$newline);
913 if ($alpnum eq 'n') {
914 @tmparray = sort {$a <=> $b} @tmparray;
916 @tmparray = (sort @tmparray);
918 foreach $line (@tmparray)
922 my @temp = split(/\,/,$line);
923 my $tmpholder = $temp[0];
924 $temp[0] = $temp[$colno];
925 $temp[$colno] = $tmpholder;
927 for ($ctr=0; $ctr < $ttlitems ; $ctr++){
928 $newline=$newline . $temp[$ctr] . ",";
931 push(@srtedarray,$newline);
935 if ($srtdir eq 'dsc') {
936 @tmparray = reverse(@srtedarray);
939 return (@srtedarray);
945 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
946 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
947 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
948 Net
::SSLeay
::set_proxy
($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
950 my $user_agent = &MakeUserAgent
();
951 my ($out, $response) = Net
::SSLeay
::get_http
( 'checkip4.dns.lightningwirelabs.com',
954 Net
::SSLeay
::make_headers
('User-Agent' => $user_agent )
956 if ($response =~ m
%HTTP/1\
.. 200 OK
%) {
957 $out =~ /Your IP address is: (\d+.\d+.\d+.\d+)/;
964 # Check if hostname.domain provided have IP provided
965 # use gethostbyname to verify that
971 # 1 IP matches host.domain
974 sub DyndnsServiceSync
($;$;$) {
976 my ($ip,$hostName,$domain) = @_;
979 #fix me no ip GROUP, what is the name ?
980 $hostName =~ s/$General::noipprefix//;
981 if ($hostName) { #may be empty
982 $hostName = "$hostName.$domain";
983 @addresses = gethostbyname($hostName);
986 if ($addresses[0] eq '') { # nothing returned ?
987 $hostName = $domain; # try resolving with domain only
988 @addresses = gethostbyname($hostName);
991 if ($addresses[0] ne '') { # got something ?
992 #&General::log("name:$addresses[0], alias:$addresses[1]");
993 # Build clear text list of IP
994 @addresses = map ( &Socket
::inet_ntoa
($_), @addresses[4..$#addresses]);
995 if (grep (/$ip/, @addresses)) {
1002 # This sub returns the red IP used to compare in DyndnsServiceSync
1004 sub GetDyndnsRedIP
{
1006 &General
::readhash
("${General::swroot}/ddns/settings", \
%settings);
1008 open(IP
, "${General::swroot}/red/local-ipaddress") or return 'unavailable';
1013 # 100.64.0.0/10 is reserved for dual-stack lite (http://tools.ietf.org/html/rfc6598).
1014 if (&General
::IpInSubnet
($ip,'10.0.0.0','255.0.0.0') ||
1015 &General
::IpInSubnet
($ip,'172.16.0.0.','255.240.0.0') ||
1016 &General
::IpInSubnet
($ip,'192.168.0.0','255.255.0.0') ||
1017 &General
::IpInSubnet
($ip,'100.64.0.0', '255.192.0.0'))
1019 if ($settings{'BEHINDROUTER'} eq 'FETCH_IP') {
1020 my $RealIP = &General
::FetchPublicIp
;
1021 $ip = (&General
::validip
($RealIP) ?
$RealIP : 'unavailable');
1027 # Translate ICMP code to text
1028 # ref: http://www.iana.org/assignments/icmp-parameters
1029 sub GetIcmpDescription
($) {
1031 my @icmp_description = (
1035 'Destination Unreachable',
1038 'Alternate Host Address',
1041 'Router Advertisement',
1042 'Router Solicitation', #10
1044 'Parameter Problem',
1047 'Information Request',
1048 'Information Reply',
1049 'Address Mask Request',
1050 'Address Mask Reply',
1051 'Reserved (for Security)',
1052 'Reserved (for Robustness Experiment)', #20
1063 'Datagram Conversion Error',
1064 'Mobile Host Redirect',
1065 'IPv6 Where-Are-You',
1067 'Mobile Registration Request',
1068 'Mobile Registration Reply',
1069 'Domain Name Request',
1070 'Domain Name Reply',
1074 if ($index>41) {return 'unknown'} else {return @icmp_description[$index]};
1077 sub GetCoreUpdateVersion
() {
1080 open(FILE
, "/opt/pakfire/db/core/mine");
1087 return $core_update;
1090 sub MakeUserAgent
() {
1091 my $user_agent = "IPFire/$General::version";
1093 my $core_update = &GetCoreUpdateVersion
();
1094 if ($core_update ne "") {
1095 $user_agent .= "/$core_update";
1101 sub RedIsWireless
() {
1102 # This function checks if a network device is a wireless device.
1105 &readhash
("${General::swroot}/ethernet/settings", \
%settings);
1107 # Find the name of the network device.
1108 my $device = $settings{'RED_DEV'};
1110 # Exit, if no device is configured.
1111 return 0 if ($device eq "");
1113 # Return 1 if the device is a wireless one.
1114 my $path = "/sys/class/net/$device/wireless";
1119 # Otherwise return zero.
1123 # Function to read a file with UTF-8 charset.
1124 sub read_file_utf8
($) {
1127 open my $in, '<:encoding(UTF-8)', $file or die "Could not open '$file' for reading $!";
1135 # Function to write a file with UTF-8 charset.
1136 sub write_file_utf8
($) {
1137 my ($file, $content) = @_;
1139 open my $out, '>:encoding(UTF-8)', $file or die "Could not open '$file' for writing $!";;
1140 print $out $content;
1146 my $FIREWALL_RELOAD_INDICATOR = "${General::swroot}/firewall/reread";
1148 sub firewall_config_changed
() {
1149 open FILE
, ">$FIREWALL_RELOAD_INDICATOR" or die "Could not open $FIREWALL_RELOAD_INDICATOR";
1153 sub firewall_needs_reload
() {
1154 if (-e
"$FIREWALL_RELOAD_INDICATOR") {
1161 sub firewall_reload
() {
1162 system("/usr/local/bin/firewallctrl");