]>
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"; }
233 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
234 $atime, $mtime, $ctime, $blksize, $blocks) = stat $_[0];
235 my $totalsecs = time() - $mtime;
238 my $secs = $totalsecs % 60;
241 push(@s, "${secs}s");
244 my $min = $totalsecs % 60;
250 my $hrs = $totalsecs % 24;
256 my $days = int($totalsecs);
258 push(@s, "${days}d");
261 return join(" ", reverse(@s));
268 if (!($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)) {
272 my @octets = ($1, $2, $3, $4);
277 if ($_ < 0 || $_ > 255) {
289 if (&validip
($mask)) {
291 # second part a number?
294 if (!($mask =~ /^\d+$/)) {
296 if ($mask >= 0 && $mask <= 32) {
303 my $ipormask = $_[0];
305 # see if it is a IP only.
306 if (&validip
($ipormask)) {
308 # split it into number and mask.
309 if (!($ipormask =~ /^(.*?)\/(.*?
)$/)) {
313 # first part not a ip?
314 if (!(&validip
($ip))) {
316 return &validmask
($mask);
321 #gets: Subnet in decimal (255.255.255.0)
322 #Gives: 24 (The cidr of network)
323 my ($byte1, $byte2, $byte3, $byte4) = split(/\./, $_[0].".0.0.0.0");
324 my $num = ($byte1 * 16777216) + ($byte2 * 65536) + ($byte3 * 256) + $byte4;
325 my $bin = unpack("B*", pack("N", $num));
326 my $count = ($bin =~ tr/1/1/);
332 #gets: Cidr of network (20-30 for ccd)
333 #Konverts 30 to 255.255.255.252 e.g
335 my $netmask = &Net
::IPv4Addr
::ipv4_cidr2msk
($cidr);
341 #Gets: Ip address or subnetmask in decimal oder CIDR
342 #Gives: What it gets only in CIDR format
347 if ($subnet =~ /^(.*?)\/(.*?
)$/) {
348 ($net,$mask) = split (/\//,$subnet);
354 #Subnet already in decimal and valid?
355 if ($mask=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
356 for (my $i=0;$i<=32;$i++){
357 if (&General
::cidrtosub
($i) eq $mask){
358 if ($full == 0){return $mask;}else{
359 return $net."/".$mask;
364 #Subnet in binary format?
365 if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=0))){
366 if($full == 0){ return &General
::cidrtosub
($mask);}else{
367 return $net."/".&General
::cidrtosub
($mask);
378 #gets: Ip Address or subnetmask in decimal oder CIDR
379 #Gives: What it gets only in CIDR format
384 if ($subnet =~ /^(.*?)\/(.*?
)$/) {
385 ($net,$mask) = split (/\//,$subnet);
390 #Subnet in decimal and valid?
391 if ($mask=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
392 for (my $i=0;$i<=32;$i++){
393 if (&General
::cidrtosub
($i) eq $mask){
394 if ($full == 0){return &General
::subtocidr
($mask);}else{
395 return $net."/".&General
::subtocidr
($mask);
400 #Subnet already in binary format?
401 if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=0))){
402 if($full == 0){ return $mask;}else{
403 return $net."/".$mask;
413 #Gets: IP, CIDR (10.10.10.0-255, 24)
415 my ($ccdip,$ccdsubnet) = @_;
416 my $ip_address_binary = inet_aton
( $ccdip );
417 my $netmask_binary = ~pack("N", (2**(32-$ccdsubnet))-1);
418 my $network_address = inet_ntoa
( $ip_address_binary & $netmask_binary );
419 return $network_address;
424 #Gets: IP in Form ("192.168.0.0/24")
425 #Gives: Broadcastaddress of network
427 my ($ccdip,$ccdsubnet) = split "/",$ccdnet;
428 my $ip_address_binary = inet_aton
( $ccdip );
429 my $netmask_binary = ~pack("N", (2**(32-$ccdsubnet))-1);
430 my $broadcast_address = inet_ntoa
( $ip_address_binary | ~$netmask_binary );
431 return $broadcast_address;
438 if ( $ip =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ ) {
439 $ip_num = (($1*256**3) + ($2*256**2) + ($3*256) + $4);
443 $ip_num = (($1*256**3) + ($2*256**2) + ($3*256) + $4);
452 $ip_num=int($ip_num/256);
454 $ip_num=int($ip_num/256);
456 $ip_num=int($ip_num/256);
458 $ip="$o4.$o3.$o2.$o1";
464 my $decip=&ip2dec
($_[0]);
466 return &dec2ip
($decip);
471 my $decip=&ip2dec
($_[0]);
473 return &dec2ip
($decip);
478 #Gets: Ip address in 192.168.0.0/24 or 192.168.0.0/255.255.255.0 and checks if subnet valid
479 #Gives: True bzw 0 if success or false
483 if (!($ccdnet =~ /^(.*?)\/(.*?
)$/)) {
486 my ($ccdip,$ccdsubnet)=split (/\//, $ccdnet);
488 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 ))) {
489 #Subnet in decimal and valid?
490 if ($ccdsubnet=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
491 for (my $i=0;$i<=32;$i++){
492 if (&General
::cidrtosub
($i) eq $ccdsubnet){
496 #Subnet already in binary format?
497 }elsif ($ccdsubnet=~/^(\d{1,2})$/ && (($1<=32 && $1>=0))){
520 my ($ip,$cidr)=split(/\//,$ccdnet);
521 $cidr=&iporsubtocidr
($cidr);
523 #get OVPN-Subnet (dynamic range)
524 &readhash
("${General::swroot}/ovpn/settings", \
%ovpnconf);
525 my ($ovpnip,$ovpncidr)= split (/\//,$ovpnconf{'DOVPN_SUBNET'});
526 $ovpncidr=&iporsubtocidr
($ovpncidr);
528 #check if we try to use same network as ovpn server
529 if ("$ip/$cidr" eq "$ovpnip/$ovpncidr") {
530 $errormessage=$errormessage.$Lang::tr
{'ccd err isovpnnet'}."<br>";
531 return $errormessage;
534 #check if we try to use same network as another ovpn N2N
535 if($ownnet ne 'ovpn'){
536 &readhasharray
("${General::swroot}/ovpn/ovpnconfig", \
%ovpnconfhash);
537 foreach my $key (keys %ovpnconfhash) {
538 if ($ovpnconfhash{$key}[3] eq 'net'){
539 my @ovpnnet=split (/\//,$ovpnconfhash{$key}[11]);
540 if (&IpInSubnet
($ip,$ovpnnet[0],&iporsubtodec
($ovpnnet[1]))){
541 $errormessage=$errormessage.$Lang::tr
{'ccd err isovpnn2n'}." $ovpnconfhash{$key}[1] <br>";
542 return $errormessage;
548 #check if we use a network-name/subnet (static-ovpn) that already exists
549 &readhasharray
("${General::swroot}/ovpn/ccd.conf", \
%ccdconfhash);
550 foreach my $key (keys %ccdconfhash) {
551 @ccdconf=split(/\//,$ccdconfhash{$key}[1]);
552 if ($ccdname eq $ccdconfhash{$key}[0])
554 $errormessage=$errormessage.$Lang::tr
{'ccd err nameexist'}."<br>";
555 return $errormessage;
557 my ($newip,$newsub) = split(/\//,$ccdnet);
558 if (&IpInSubnet
($newip,$ccdconf[0],&iporsubtodec
($ccdconf[1])))
560 $errormessage=$errormessage.$Lang::tr
{'ccd err issubnet'}." $ccdconfhash{$key}[0]<br>";
561 return $errormessage;
565 #check if we use a ipsec right network which is already defined
566 if($ownnet ne 'ipsec'){
567 &General
::readhasharray
("${General::swroot}/vpn/config", \
%ipsecconf);
568 foreach my $key (keys %ipsecconf){
569 if ($ipsecconf{$key}[11] ne ''){
570 my ($ipsecip,$ipsecsub) = split (/\//, $ipsecconf{$key}[11]);
571 $ipsecsub=&iporsubtodec
($ipsecsub);
572 if($ipsecconf{$key}[1] ne $ccdname){
573 if ( &IpInSubnet
($ip,$ipsecip,$ipsecsub) ){
574 $errormessage=$Lang::tr
{'ccd err isipsecnet'}." Name: $ipsecconf{$key}[1]";
575 return $errormessage;
582 #check if we use the ipsec RW Network (if defined)
583 &readhash
("${General::swroot}/vpn/settings", \
%vpnconf);
584 if ($vpnconf{'RW_NET'} ne ''){
585 my ($ipsecrwnet,$ipsecrwsub)=split (/\//, $vpnconf{'RW_NET'});
586 if (&IpInSubnet
($ip,$ipsecrwnet,&iporsubtodec
($ipsecrwsub)))
588 $errormessage=$errormessage.$Lang::tr
{'ccd err isipsecrw'}."<br>";
589 return $errormessage;
593 #check if we use one of ipfire's networks (green,orange,blue)
594 &readhash
("${General::swroot}/ethernet/settings", \
%ownnet);
595 if (($ownnet{'GREEN_NETADDRESS'} ne '' && $ownnet{'GREEN_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet
($ip,$ownnet{'GREEN_NETADDRESS'},&iporsubtodec
($ownnet{'GREEN_NETMASK'}))){ $errormessage=$Lang::tr
{'ccd err green'};return $errormessage;}
596 if (($ownnet{'ORANGE_NETADDRESS'} ne '' && $ownnet{'ORANGE_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet
($ip,$ownnet{'ORANGE_NETADDRESS'},&iporsubtodec
($ownnet{'ORANGE_NETMASK'}))){ $errormessage=$Lang::tr
{'ccd err orange'};return $errormessage;}
597 if (($ownnet{'BLUE_NETADDRESS'} ne '' && $ownnet{'BLUE_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet
($ip,$ownnet{'BLUE_NETADDRESS'},&iporsubtodec
($ownnet{'BLUE_NETMASK'}))){ $errormessage=$Lang::tr
{'ccd err blue'};return $errormessage;}
598 if (($ownnet{'RED_NETADDRESS'} ne '' && $ownnet{'RED_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet
($ip,$ownnet{'RED_NETADDRESS'},&iporsubtodec
($ownnet{'RED_NETMASK'}))){ $errormessage=$Lang::tr
{'ccd err red'};return $errormessage;}
610 if ($_ >= 1 && $_ <= 65535) {
623 if ($_ == 53 || $_ == 222 || $_ == 444 || $_ == 81 ) {
625 elsif ($_ >= 1 && $_ <= 65535) {
632 my $checkmac = $_[0];
633 my $ot = '[0-9a-f]{2}'; # 2 Hex digits (one octet)
634 if ($checkmac !~ /^$ot:$ot:$ot:$ot:$ot:$ot$/i)
643 # Checks a hostname against RFC1035
644 my $hostname = $_[0];
646 # Each part should be at least two characters in length
647 # but no more than 63 characters
648 if (length ($hostname) < 1 || length ($hostname) > 63) {
650 # Only valid characters are a-z, A-Z, 0-9 and -
651 if ($hostname !~ /^[a-zA-Z0-9-\s]*$/) {
653 # First character can only be a letter or a digit
654 if (substr ($hostname, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
656 # Last character can only be a letter or a digit
657 if (substr ($hostname, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
666 # Checks a domain name against RFC1035
667 my $domainname = $_[0];
668 my @parts = split (/\./, $domainname); # Split hostname at the '.'
670 foreach $part (@parts) {
671 # Each part should be at least two characters in length
672 # but no more than 63 characters
673 if (length ($part) < 2 || length ($part) > 63) {
675 # Only valid characters are a-z, A-Z, 0-9 and -
676 if ($part !~ /^[a-zA-Z0-9-]*$/) {
678 # First character can only be a letter or a digit
679 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
681 # Last character can only be a letter or a digit
682 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
692 # Checks a fully qualified domain name against RFC1035
694 my @parts = split (/\./, $fqdn); # Split hostname at the '.'
695 if (scalar(@parts) < 2) { # At least two parts should
696 return 0;} # exist in a FQDN
697 # (i.e. hostname.domain)
698 foreach $part (@parts) {
699 # Each part should be at least one character in length
700 # but no more than 63 characters
701 if (length ($part) < 1 || length ($part) > 63) {
703 # Only valid characters are a-z, A-Z, 0-9 and -
704 if ($part !~ /^[a-zA-Z0-9-]*$/) {
706 # First character can only be a letter or a digit
707 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
709 # Last character can only be a letter or a digit
710 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
716 sub validportrange
# used to check a port range
718 my $port = $_[0]; # port values
719 $port =~ tr/-/:/; # replace all - with colons just in case someone used -
720 my $srcdst = $_[1]; # is it a source or destination port
722 if (!($port =~ /^(\d+)\:(\d+)$/)) {
724 if (!(&validport
($port))) {
725 if ($srcdst eq 'src'){
726 return $Lang::tr
{'source port numbers'};
728 return $Lang::tr
{'destination port numbers'};
734 my @ports = ($1, $2);
736 if ($srcdst eq 'src'){
737 return $Lang::tr
{'bad source range'};
739 return $Lang::tr
{'bad destination range'};
744 if (!(&validport
($_))) {
745 if ($srcdst eq 'src'){
746 return $Lang::tr
{'source port numbers'};
748 return $Lang::tr
{'destination port numbers'};
756 # Test if IP is within a subnet
757 # Call: IpInSubnet (Addr, Subnet, Subnet Mask)
758 # Subnet can be an IP of the subnet: 10.0.0.0 or 10.0.0.1
759 # Everything in dottted notation
763 my $ip = unpack('N', &Socket
::inet_aton
(shift));
764 my $start = unpack('N', &Socket
::inet_aton
(shift));
765 my $mask = unpack('N', &Socket
::inet_aton
(shift));
766 $start &= $mask; # base of subnet...
767 my $end = $start + ~$mask;
768 return (($ip >= $start) && ($ip <= $end));
772 # Return the following IP (IP+1) in dotted notation.
773 # Call: NextIP ('1.1.1.1');
778 return &Socket
::inet_ntoa
( pack("N", 1 + unpack('N', &Socket
::inet_aton
(shift))
784 return &Socket
::inet_ntoa
( pack("N", 4 + unpack('N', &Socket
::inet_aton
(shift))
790 my ($ip,$cidr) = &Net
::IPv4Addr
::ipv4_parse
(shift);
796 my ($ip,$cidr) = &Net
::IPv4Addr
::ipv4_parse
(shift);
797 my $netmask = &Net
::IPv4Addr
::ipv4_cidr2msk
($cidr);
798 return "$ip\/$netmask";
804 return 0 if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/ );
805 return 0 if ( $mail =~ /^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/);
806 return 0 if ( $mail !~ /([0-9a-zA-Z]{1})\@./ );
807 return 0 if ( $mail !~ /.\@([0-9a-zA-Z]{1})/ );
808 return 0 if ( $mail =~ /.\.\-.|.\-\..|.\.\..|.\-\-./g );
809 return 0 if ( $mail =~ /.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./g );
810 return 0 if ( $mail !~ /\.([a-zA-Z]{2,4})$/ );
815 # Currently only vpnmain use this three procs (readhasharray, writehasharray, findhasharray)
816 # The 'key' used is numeric but is perfectly unneeded! This will to be removed so don't use
817 # this code. Vpnmain will be splitted in parts: x509/pki, connection ipsec, connection other,... .
820 my ($filename, $hash) = @_;
823 open(FILE
, $filename) or die "Unable to read file $filename";
826 my ($key, $rest, @temp);
828 ($key, $rest) = split (/,/, $_, 2);
829 if ($key =~ /^[0-9]+$/) {
830 @temp = split (/,/, $rest);
831 $hash->{$key} = \
@temp;
839 my ($filename, $hash) = @_;
840 my ($key, @temp, $i);
842 open(FILE
, ">$filename") or die "Unable to write to file $filename";
844 foreach $key (keys %$hash) {
845 if ($key =~ /^[0-9]+$/) {
847 foreach $i (0 .. $#{$hash->{$key}}) {
848 print FILE
",$hash->{$key}[$i]";
857 sub findhasharraykey
{
858 foreach my $i (1 .. 1000000) {
859 if ( ! exists $_[0]{$i}) {
866 # Darren Critchley - darrenc@telus.net - (c) 2003
867 # &srtarray(SortOrder, AlphaNumeric, SortDirection, ArrayToBeSorted)
868 # This subroutine will take the following parameters:
869 # ColumnNumber = the column which you want to sort on, starts at 1
870 # AlphaNumberic = a or n (lowercase) defines whether the sort should be alpha or numberic
871 # SortDirection = asc or dsc (lowercase) Ascending or Descending sort
872 # ArrayToBeSorted = the array that wants sorting
874 # Returns an array that is sorted to your specs
876 # If SortOrder is greater than the elements in array, then it defaults to the first element
879 my ($colno, $alpnum, $srtdir, @tobesorted) = @_;
885 my $ttlitems = scalar @tobesorted; # want to know the number of rows in the passed array
886 if ($ttlitems < 1){ # if no items, don't waste our time lets leave
887 return (@tobesorted);
889 my @tmp = split(/\,/,$tobesorted[0]);
890 $ttlitems = scalar @tmp; # this should be the number of elements in each row of the passed in array
892 # Darren Critchley - validate parameters
893 if ($colno > $ttlitems){$colno = '1';}
894 $colno--; # remove one from colno to deal with arrays starting at 0
895 if($colno < 0){$colno = '0';}
896 if ($alpnum ne '') { $alpnum = lc($alpnum); } else { $alpnum = 'a'; }
897 if ($srtdir ne '') { $srtdir = lc($srtdir); } else { $srtdir = 'src'; }
899 foreach $line (@tobesorted)
903 my @temp = split(/\,/,$line);
904 # Darren Critchley - juggle the fields so that the one we want to sort on is first
905 my $tmpholder = $temp[0];
906 $temp[0] = $temp[$colno];
907 $temp[$colno] = $tmpholder;
909 for ($ctr=0; $ctr < $ttlitems ; $ctr++) {
910 $newline=$newline . $temp[$ctr] . ",";
913 push(@tmparray,$newline);
916 if ($alpnum eq 'n') {
917 @tmparray = sort {$a <=> $b} @tmparray;
919 @tmparray = (sort @tmparray);
921 foreach $line (@tmparray)
925 my @temp = split(/\,/,$line);
926 my $tmpholder = $temp[0];
927 $temp[0] = $temp[$colno];
928 $temp[$colno] = $tmpholder;
930 for ($ctr=0; $ctr < $ttlitems ; $ctr++){
931 $newline=$newline . $temp[$ctr] . ",";
934 push(@srtedarray,$newline);
938 if ($srtdir eq 'dsc') {
939 @tmparray = reverse(@srtedarray);
942 return (@srtedarray);
948 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
949 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
950 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
951 Net
::SSLeay
::set_proxy
($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
953 my $user_agent = &MakeUserAgent
();
954 my ($out, $response) = Net
::SSLeay
::get_http
( 'checkip4.dns.lightningwirelabs.com',
957 Net
::SSLeay
::make_headers
('User-Agent' => $user_agent )
959 if ($response =~ m
%HTTP/1\
.. 200 OK
%) {
960 $out =~ /Your IP address is: (\d+.\d+.\d+.\d+)/;
967 # Check if hostname.domain provided have IP provided
968 # use gethostbyname to verify that
974 # 1 IP matches host.domain
977 sub DyndnsServiceSync
($;$;$) {
979 my ($ip,$hostName,$domain) = @_;
982 #fix me no ip GROUP, what is the name ?
983 $hostName =~ s/$General::noipprefix//;
984 if ($hostName) { #may be empty
985 $hostName = "$hostName.$domain";
986 @addresses = gethostbyname($hostName);
989 if ($addresses[0] eq '') { # nothing returned ?
990 $hostName = $domain; # try resolving with domain only
991 @addresses = gethostbyname($hostName);
994 if ($addresses[0] ne '') { # got something ?
995 #&General::log("name:$addresses[0], alias:$addresses[1]");
996 # Build clear text list of IP
997 @addresses = map ( &Socket
::inet_ntoa
($_), @addresses[4..$#addresses]);
998 if (grep (/$ip/, @addresses)) {
1005 # This sub returns the red IP used to compare in DyndnsServiceSync
1007 sub GetDyndnsRedIP
{
1009 &General
::readhash
("${General::swroot}/ddns/settings", \
%settings);
1011 open(IP
, "${General::swroot}/red/local-ipaddress") or return 'unavailable';
1016 # 100.64.0.0/10 is reserved for dual-stack lite (http://tools.ietf.org/html/rfc6598).
1017 if (&General
::IpInSubnet
($ip,'10.0.0.0','255.0.0.0') ||
1018 &General
::IpInSubnet
($ip,'172.16.0.0.','255.240.0.0') ||
1019 &General
::IpInSubnet
($ip,'192.168.0.0','255.255.0.0') ||
1020 &General
::IpInSubnet
($ip,'100.64.0.0', '255.192.0.0'))
1022 if ($settings{'BEHINDROUTER'} eq 'FETCH_IP') {
1023 my $RealIP = &General
::FetchPublicIp
;
1024 $ip = (&General
::validip
($RealIP) ?
$RealIP : 'unavailable');
1030 # Translate ICMP code to text
1031 # ref: http://www.iana.org/assignments/icmp-parameters
1032 sub GetIcmpDescription
($) {
1034 my @icmp_description = (
1038 'Destination Unreachable',
1041 'Alternate Host Address',
1044 'Router Advertisement',
1045 'Router Solicitation', #10
1047 'Parameter Problem',
1050 'Information Request',
1051 'Information Reply',
1052 'Address Mask Request',
1053 'Address Mask Reply',
1054 'Reserved (for Security)',
1055 'Reserved (for Robustness Experiment)', #20
1066 'Datagram Conversion Error',
1067 'Mobile Host Redirect',
1068 'IPv6 Where-Are-You',
1070 'Mobile Registration Request',
1071 'Mobile Registration Reply',
1072 'Domain Name Request',
1073 'Domain Name Reply',
1077 if ($index>41) {return 'unknown'} else {return $icmp_description[$index]};
1080 sub GetCoreUpdateVersion
() {
1083 open(FILE
, "/opt/pakfire/db/core/mine");
1090 return $core_update;
1093 sub MakeUserAgent
() {
1094 my $user_agent = "IPFire/$General::version";
1096 my $core_update = &GetCoreUpdateVersion
();
1097 if ($core_update ne "") {
1098 $user_agent .= "/$core_update";
1104 sub RedIsWireless
() {
1105 # This function checks if a network device is a wireless device.
1108 &readhash
("${General::swroot}/ethernet/settings", \
%settings);
1110 # Find the name of the network device.
1111 my $device = $settings{'RED_DEV'};
1113 # Exit, if no device is configured.
1114 return 0 if ($device eq "");
1116 # Return 1 if the device is a wireless one.
1117 my $path = "/sys/class/net/$device/wireless";
1122 # Otherwise return zero.
1126 # Function to read a file with UTF-8 charset.
1127 sub read_file_utf8
($) {
1130 open my $in, '<:encoding(UTF-8)', $file or die "Could not open '$file' for reading $!";
1138 # Function to write a file with UTF-8 charset.
1139 sub write_file_utf8
($) {
1140 my ($file, $content) = @_;
1142 open my $out, '>:encoding(UTF-8)', $file or die "Could not open '$file' for writing $!";;
1143 print $out $content;
1149 my $FIREWALL_RELOAD_INDICATOR = "${General::swroot}/firewall/reread";
1151 sub firewall_config_changed
() {
1152 open FILE
, ">$FIREWALL_RELOAD_INDICATOR" or die "Could not open $FIREWALL_RELOAD_INDICATOR";
1156 sub firewall_needs_reload
() {
1157 if (-e
"$FIREWALL_RELOAD_INDICATOR") {
1164 sub firewall_reload
() {
1165 system("/usr/local/bin/firewallctrl");