]>
git.ipfire.org Git - people/pmueller/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 $
20 use Locale
::Codes
::Country
;
22 use Net
::IPv4Addr
qw(:all);
23 $|=1; # line buffering
25 $General::version
= 'VERSION';
26 $General::swroot
= 'CONFIG_ROOT';
27 $General::noipprefix
= 'noipg-';
28 $General::adminmanualurl
= 'http://wiki.ipfire.org';
30 require "${General::swroot}/network-functions.pl";
33 # log ("message") use default 'ipcop' tag
34 # log ("tag","message") use your tag
39 $tag = shift if (@_>1);
40 my $logmessage = $_[0];
41 $logmessage =~ /([\w\W]*)/;
43 system('logger', '-t', $tag, $logmessage);
45 sub setup_default_networks
48 my $defaultNetworks = shift;
50 &readhash
("/var/ipfire/ethernet/settings", \
%netsettings);
52 # Get current defined networks (Red, Green, Blue, Orange)
53 $defaultNetworks->{$Lang::tr{'fwhost any'}}{'IPT'} = "0.0.0.0/0.0.0.0";
54 $defaultNetworks->{$Lang::tr{'fwhost any'}}{'NAME'} = "ALL";
56 $defaultNetworks->{$Lang::tr{'green'}}{'IPT'} = "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
57 $defaultNetworks->{$Lang::tr{'green'}}{'NET'} = "$netsettings{'GREEN_ADDRESS'}";
58 $defaultNetworks->{$Lang::tr{'green'}}{'NAME'} = "GREEN";
60 if ($netsettings{'RED_DEV'} ne ''){
61 $defaultNetworks->{$Lang::tr{'fwdfw red'}}{'IPT'} = "$netsettings{'RED_NETADDRESS'}/$netsettings{'RED_NETMASK'}";
62 $defaultNetworks->{$Lang::tr{'fwdfw red'}}{'NET'} = "$netsettings{'RED_ADDRESS'}";
63 $defaultNetworks->{$Lang::tr{'fwdfw red'}}{'NAME'} = "RED";
65 if ($netsettings{'ORANGE_DEV'} ne ''){
66 $defaultNetworks->{$Lang::tr{'orange'}}{'IPT'} = "$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}";
67 $defaultNetworks->{$Lang::tr{'orange'}}{'NET'} = "$netsettings{'ORANGE_ADDRESS'}";
68 $defaultNetworks->{$Lang::tr{'orange'}}{'NAME'} = "ORANGE";
71 if ($netsettings{'BLUE_DEV'} ne ''){
72 $defaultNetworks->{$Lang::tr{'blue'}}{'IPT'} = "$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}";
73 $defaultNetworks->{$Lang::tr{'blue'}}{'NET'} = "$netsettings{'BLUE_ADDRESS'}";
74 $defaultNetworks->{$Lang::tr{'blue'}}{'NAME'} = "BLUE";
78 $defaultNetworks->{'IPFire'}{'NAME'} = "IPFire";
81 if(-e
"${General::swroot}/ovpn/settings")
83 my %ovpnSettings = ();
84 &readhash
("${General::swroot}/ovpn/settings", \
%ovpnSettings);
87 if(defined($ovpnSettings{'DOVPN_SUBNET'}))
89 my ($ip,$sub) = split(/\//,$ovpnSettings{'DOVPN_SUBNET'});
90 $sub=&General
::iporsubtocidr
($sub);
91 my @tempovpnsubnet = split("\/", $ovpnSettings{'DOVPN_SUBNET'});
92 $defaultNetworks->{'OpenVPN ' ."($ip/$sub)"}{'ADR'} = $tempovpnsubnet[0];
93 $defaultNetworks->{'OpenVPN ' ."($ip/$sub)"}{'NAME'} = "OpenVPN-Dyn";
97 if(-e
"${General::swroot}/vpn/settings")
99 my %ipsecsettings = ();
100 &readhash
("${General::swroot}/vpn/settings", \
%ipsecsettings);
101 if($ipsecsettings{'RW_NET'} ne '')
103 my ($ip,$sub) = split(/\//,$ipsecsettings{'RW_NET'});
104 $sub=&General
::iporsubtocidr
($sub);
105 my @tempipsecsubnet = split("\/", $ipsecsettings{'RW_NET'});
106 $defaultNetworks->{'IPsec RW (' .$ip."/".$sub.")"}{'ADR'} = $tempipsecsubnet[0];
107 $defaultNetworks->{'IPsec RW (' .$ip."/".$sub.")"}{'NAME'} = "IPsec RW";
108 $defaultNetworks->{'IPsec RW (' .$ip."/".$sub.")"}{'NET'} = &getnextip
($ip);
115 my $defaultNetworks = shift;
116 open(FILE
, "${General::swroot}/ethernet/aliases") or die 'Unable to open aliases file.';
117 my @current = <FILE
>;
120 foreach my $line (@current)
124 my @temp = split(/\,/,$line);
125 if ($temp[2] eq '') {
126 $temp[2] = "Alias $ctr : $temp[0]";
128 $defaultNetworks->{$temp[2]}{'IPT'} = "$temp[0]";
129 $defaultNetworks->{$temp[2]}{'NET'} = "$temp[0]";
138 my $filename = $_[0];
143 # Some ipcop code expects that readhash 'complete' the hash if new entries
144 # are presents. Not clear it !!!
147 open(FILE
, $filename) or die "Unable to read file $filename";
154 next if ($_ =~ /\#/);
156 ($var, $val) = split /=/, $_, 2;
162 # Untaint variables read from hash
163 # trim space from begin and end
166 $var =~ /([A-Za-z0-9_-]*)/;
170 $hash->{$var} = $val;
179 my $filename = $_[0];
183 # write cgi vars to the file.
184 open(FILE
, ">${filename}") or die "Unable to write file $filename";
186 foreach $var (keys %$hash)
188 if ( $var eq "__CGI__"){next;}
189 $val = $hash->{$var};
190 # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
191 # location of the mouse are submitted as well, this was being written to the settings file causing
192 # some serious grief! This skips the variable.x and variable.y
193 if (!($var =~ /(.x|.y)$/)) {
196 if (!($var =~ /^ACTION/)) {
197 print FILE
"${var}=${val}\n"; }
205 # This function replaces the given hash in the original hash by keeping the old
206 # content and just replacing the new content
208 my $filename = $_[0];
213 readhash
("${filename}", \
%oldhash);
215 foreach $var (keys %$newhash){
216 $oldhash{$var}=$newhash->{$var};
219 # write cgi vars to the file.
220 open(FILE
, ">${filename}") or die "Unable to write file $filename";
222 foreach $var (keys %oldhash)
224 if ( $var eq "__CGI__"){next;}
225 $val = $oldhash{$var};
226 # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
227 # location of the mouse are submitted as well, this was being written to the settings file causing
228 # some serious grief! This skips the variable.x and variable.y
229 if (!($var =~ /(.x|.y)$/)) {
232 if (!($var =~ /^ACTION/)) {
233 print FILE
"${var}=${val}\n"; }
240 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
241 $atime, $mtime, $ctime, $blksize, $blocks) = stat $_[0];
242 my $t = time() - $mtime;
244 return &format_time
($t);
248 my $totalsecs = shift;
251 my $secs = $totalsecs % 60;
254 push(@s, "${secs}s");
257 my $min = $totalsecs % 60;
263 my $hrs = $totalsecs % 24;
269 my $days = int($totalsecs);
271 push(@s, "${days}d");
274 return join(" ", reverse(@s));
281 if (!($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)) {
285 my @octets = ($1, $2, $3, $4);
290 if ($_ < 0 || $_ > 255) {
300 return &Network
::check_netmask
($mask) || &Network
::check_prefix
($mask);
305 my $ipormask = $_[0];
307 # see if it is a IP only.
308 if (&validip
($ipormask)) {
310 # split it into number and mask.
311 if (!($ipormask =~ /^(.*?)\/(.*?
)$/)) {
315 # first part not a ip?
316 if (!(&validip
($ip))) {
318 return &validmask
($mask);
322 return &Network
::convert_netmask2prefix
(shift);
326 return &Network
::convert_prefix2netmask
(shift);
331 #Gets: Ip address or subnetmask in decimal oder CIDR
332 #Gives: What it gets only in CIDR format
337 if ($subnet =~ /^(.*?)\/(.*?
)$/) {
338 ($net,$mask) = split (/\//,$subnet);
344 #Subnet already in decimal and valid?
345 if ($mask=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
346 for (my $i=0;$i<=32;$i++){
347 if (&General
::cidrtosub
($i) eq $mask){
348 if ($full == 0){return $mask;}else{
349 return $net."/".$mask;
354 #Subnet in binary format?
355 if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=0))){
356 if($full == 0){ return &General
::cidrtosub
($mask);}else{
357 return $net."/".&General
::cidrtosub
($mask);
368 #gets: Ip Address or subnetmask in decimal oder CIDR
369 #Gives: What it gets only in CIDR format
374 if ($subnet =~ /^(.*?)\/(.*?
)$/) {
375 ($net,$mask) = split (/\//,$subnet);
380 #Subnet in decimal and valid?
381 if ($mask=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
382 for (my $i=0;$i<=32;$i++){
383 if (&General
::cidrtosub
($i) eq $mask){
384 if ($full == 0){return &General
::subtocidr
($mask);}else{
385 return $net."/".&General
::subtocidr
($mask);
390 #Subnet already in binary format?
391 if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=0))){
392 if($full == 0){ return $mask;}else{
393 return $net."/".$mask;
402 my $arg = join("/", @_);
404 return &Network
::get_netaddress
($arg);
409 #Gets: IP in Form ("192.168.0.0/24")
410 #Gives: Broadcastaddress of network
412 my ($ccdip,$ccdsubnet) = split "/",$ccdnet;
413 my $ip_address_binary = inet_aton
( $ccdip );
414 my $netmask_binary = ~pack("N", (2**(32-$ccdsubnet))-1);
415 my $broadcast_address = inet_ntoa
( $ip_address_binary | ~$netmask_binary );
416 return $broadcast_address;
420 return &Network
::ip2bin
(shift);
424 return &Network
::bin2ip
(shift);
428 return &Network
::find_next_ip_address
(shift, 4);
432 return &Network
::find_next_ip_address
(shift, -1);
437 #Gets: Ip address in 192.168.0.0/24 or 192.168.0.0/255.255.255.0 and checks if subnet valid
438 #Gives: True bzw 0 if success or false
442 if (!($ccdnet =~ /^(.*?)\/(.*?
)$/)) {
445 my ($ccdip,$ccdsubnet)=split (/\//, $ccdnet);
447 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 ))) {
448 #Subnet in decimal and valid?
449 if ($ccdsubnet=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
450 for (my $i=0;$i<=32;$i++){
451 if (&General
::cidrtosub
($i) eq $ccdsubnet){
455 #Subnet already in binary format?
456 }elsif ($ccdsubnet=~/^(\d{1,2})$/ && (($1<=32 && $1>=0))){
480 my ($ip,$cidr)=split(/\//,$ccdnet);
481 $cidr=&iporsubtocidr
($cidr);
483 #get OVPN-Subnet (dynamic range)
484 &readhash
("${General::swroot}/ovpn/settings", \
%ovpnconf);
485 my ($ovpnip,$ovpncidr)= split (/\//,$ovpnconf{'DOVPN_SUBNET'});
486 $ovpncidr=&iporsubtocidr
($ovpncidr);
488 #check if we try to use same network as ovpn server
489 if ("$ip/$cidr" eq "$ovpnip/$ovpncidr") {
490 $errormessage=$errormessage.$Lang::tr
{'ccd err isovpnnet'}."<br>";
491 return $errormessage;
494 #check if we try to use same network as another ovpn N2N
495 if($ownnet ne 'ovpn'){
496 &readhasharray
("${General::swroot}/ovpn/ovpnconfig", \
%ovpnconfhash);
497 foreach my $key (keys %ovpnconfhash) {
498 if ($ovpnconfhash{$key}[3] eq 'net'){
499 my @ovpnnet=split (/\//,$ovpnconfhash{$key}[11]);
500 if (&IpInSubnet
($ip,$ovpnnet[0],&iporsubtodec
($ovpnnet[1]))){
501 $errormessage=$errormessage.$Lang::tr
{'ccd err isovpnn2n'}." $ovpnconfhash{$key}[1] <br>";
502 return $errormessage;
508 #check if we use a network-name/subnet (static-ovpn) that already exists
509 &readhasharray
("${General::swroot}/ovpn/ccd.conf", \
%ccdconfhash);
510 foreach my $key (keys %ccdconfhash) {
511 @ccdconf=split(/\//,$ccdconfhash{$key}[1]);
512 if ($ccdname eq $ccdconfhash{$key}[0])
514 $errormessage=$errormessage.$Lang::tr
{'ccd err nameexist'}."<br>";
515 return $errormessage;
517 my ($newip,$newsub) = split(/\//,$ccdnet);
518 if (&IpInSubnet
($newip,$ccdconf[0],&iporsubtodec
($ccdconf[1])))
520 $errormessage=$errormessage.$Lang::tr
{'ccd err issubnet'}." $ccdconfhash{$key}[0]<br>";
521 return $errormessage;
525 #check if we use a ipsec right network which is already defined
526 if($ownnet ne 'ipsec'){
527 &General
::readhasharray
("${General::swroot}/vpn/config", \
%ipsecconf);
528 foreach my $key (keys %ipsecconf){
529 if ($ipsecconf{$key}[11] ne ''){
530 foreach my $ipsecsubitem (split(/\|/, $ipsecconf{$key}[11])) {
531 my ($ipsecip,$ipsecsub) = split (/\//, $ipsecconf{$key}[11]);
532 $ipsecsub=&iporsubtodec
($ipsecsub);
533 if($ipsecconf{$key}[1] ne $ccdname){
534 if ( &IpInSubnet
($ip,$ipsecip,$ipsecsub) ){
535 $errormessage=$Lang::tr
{'ccd err isipsecnet'}." Name: $ipsecconf{$key}[1]";
536 return $errormessage;
544 #check if we use the ipsec RW Network (if defined)
545 &readhash
("${General::swroot}/vpn/settings", \
%vpnconf);
546 if ($vpnconf{'RW_NET'} ne ''){
547 my ($ipsecrwnet,$ipsecrwsub)=split (/\//, $vpnconf{'RW_NET'});
548 if (&IpInSubnet
($ip,$ipsecrwnet,&iporsubtodec
($ipsecrwsub)))
550 $errormessage=$errormessage.$Lang::tr
{'ccd err isipsecrw'}."<br>";
551 return $errormessage;
555 #call check_net_internal
556 if ($checktype eq "exact")
558 &General
::check_net_internal_exact
($ccdnet);
560 &General
::check_net_internal_range
($ccdnet);
564 sub check_net_internal_range
{
566 my ($ip,$cidr)=split(/\//,$network);
569 $cidr=&iporsubtocidr
($cidr);
570 #check if we use one of ipfire's networks (green,orange,blue)
571 &readhash
("${General::swroot}/ethernet/settings", \
%ownnet);
572 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;}
573 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;}
574 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;}
575 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;}
578 sub check_net_internal_exact
{
580 my ($ip,$cidr)=split(/\//,$network);
583 $cidr=&iporsubtocidr
($cidr);
584 #check if we use one of ipfire's networks (green,orange,blue)
585 &readhash
("${General::swroot}/ethernet/settings", \
%ownnet);
586 if (($ownnet{'GREEN_NETADDRESS'} ne '' && $ownnet{'GREEN_NETADDRESS'} ne '0.0.0.0') && &Network
::network_equal
("$ownnet{'GREEN_NETADDRESS'}/$ownnet{'GREEN_NETMASK'}", $network)){ $errormessage=$Lang::tr
{'ccd err green'};return $errormessage;}
587 if (($ownnet{'ORANGE_NETADDRESS'} ne '' && $ownnet{'ORANGE_NETADDRESS'} ne '0.0.0.0') && &Network
::network_equal
("$ownnet{'ORANGE_NETADDRESS'}/$ownnet{'ORANGE_NETMASK'}", $network)){ $errormessage=$Lang::tr
{'ccd err orange'};return $errormessage;}
588 if (($ownnet{'BLUE_NETADDRESS'} ne '' && $ownnet{'BLUE_NETADDRESS'} ne '0.0.0.0') && &Network
::network_equal
("$ownnet{'BLUE_NETADDRESS'}/$ownnet{'BLUE_NETMASK'}", $network)){ $errormessage=$Lang::tr
{'ccd err blue'};return $errormessage;}
589 if (($ownnet{'RED_NETADDRESS'} ne '' && $ownnet{'RED_NETADDRESS'} ne '0.0.0.0') && &Network
::network_equal
("$ownnet{'RED_NETADDRESS'}/$ownnet{'RED_NETMASK'}", $network)){ $errormessage=$Lang::tr
{'ccd err red'};return $errormessage;}
600 if ($_ >= 1 && $_ <= 65535) {
613 if ($_ == 53 || $_ == 222 || $_ == 444 || $_ == 81 ) {
615 elsif ($_ >= 1 && $_ <= 65535) {
622 my $checkmac = $_[0];
623 my $ot = '[0-9a-f]{2}'; # 2 Hex digits (one octet)
624 if ($checkmac !~ /^$ot:$ot:$ot:$ot:$ot:$ot$/i)
633 # Checks a hostname against RFC1035
634 my $hostname = $_[0];
636 # Each part should be at least two characters in length
637 # but no more than 63 characters
638 if (length ($hostname) < 1 || length ($hostname) > 63) {
640 # Only valid characters are a-z, A-Z, 0-9 and -
641 if ($hostname !~ /^[a-zA-Z0-9-\s]*$/) {
643 # First character can only be a letter or a digit
644 if (substr ($hostname, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
646 # Last character can only be a letter or a digit
647 if (substr ($hostname, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
656 # Checks a domain name against RFC1035
657 my $domainname = $_[0];
658 my @parts = split (/\./, $domainname); # Split hostname at the '.'
660 foreach $part (@parts) {
661 # Each part should be no more than 63 characters in length
662 if (length ($part) < 1 || length ($part) > 63) {
664 # Only valid characters are a-z, A-Z, 0-9, _ and -
665 if ($part !~ /^[a-zA-Z0-9_-]*$/) {
676 # Checks a fully qualified domain name against RFC1035
678 my @parts = split (/\./, $fqdn); # Split hostname at the '.'
679 if (scalar(@parts) < 2) { # At least two parts should
680 return 0;} # exist in a FQDN
681 # (i.e.hostname.domain)
682 foreach $part (@parts) {
683 # Each part should be at least one character in length
684 # but no more than 63 characters
685 if (length ($part) < 1 || length ($part) > 63) {
687 # Only valid characters are a-z, A-Z, 0-9 and -
688 if ($part !~ /^[a-zA-Z0-9-]*$/) {
690 # First character can only be a letter or a digit
691 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
693 # Last character can only be a letter or a digit
694 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
700 sub validportrange
# used to check a port range
702 my $port = $_[0]; # port values
703 $port =~ tr/-/:/; # replace all - with colons just in case someone used -
704 my $srcdst = $_[1]; # is it a source or destination port
706 if (!($port =~ /^(\d+)\:(\d+)$/)) {
708 if (!(&validport
($port))) {
709 if ($srcdst eq 'src'){
710 return $Lang::tr
{'source port numbers'};
712 return $Lang::tr
{'destination port numbers'};
718 my @ports = ($1, $2);
720 if ($srcdst eq 'src'){
721 return $Lang::tr
{'bad source range'};
723 return $Lang::tr
{'bad destination range'};
728 if (!(&validport
($_))) {
729 if ($srcdst eq 'src'){
730 return $Lang::tr
{'source port numbers'};
732 return $Lang::tr
{'destination port numbers'};
745 return &Network
::ip_address_in_network
($addr, "$network/$netmask");
749 # Return the following IP (IP+1) in dotted notation.
750 # Call: NextIP ('1.1.1.1');
754 return &Network
::find_next_ip_address
(shift, 1);
758 return &Network
::find_next_ip_address
(shift, 4);
762 my ($ip,$cidr) = &Net
::IPv4Addr
::ipv4_parse
(shift);
767 my ($ip,$cidr) = &Net
::IPv4Addr
::ipv4_parse
(shift);
768 my $netmask = &Net
::IPv4Addr
::ipv4_cidr2msk
($cidr);
769 return "$ip\/$netmask";
774 my @parts = split( /\@/, $address );
777 #check if we have one part before and after '@'
778 return 0 if ( $count != 2 );
780 #check if one of the parts starts or ends with a dot
781 return 0 if ( substr($parts[0],0,1) eq '.' );
782 return 0 if ( substr($parts[0],-1,1) eq '.' );
783 return 0 if ( substr($parts[1],0,1) eq '.' );
784 return 0 if ( substr($parts[1],-1,1) eq '.' );
786 #check first addresspart (before '@' sign)
787 return 0 if ( $parts[0] !~ m/^[a-zA-Z0-9\.!\-\+#]+$/ );
789 #check second addresspart (after '@' sign)
790 return 0 if ( $parts[1] !~ m/^[a-zA-Z0-9\.\-]+$/ );
796 # Currently only vpnmain use this three procs (readhasharray, writehasharray, findhasharray)
797 # The 'key' used is numeric but is perfectly unneeded! This will to be removed so don't use
798 # this code. Vpnmain will be splitted in parts: x509/pki, connection ipsec, connection other,... .
801 my ($filename, $hash) = @_;
804 open(FILE
, $filename) or die "Unable to read file $filename";
807 my ($key, $rest, @temp);
809 ($key, $rest) = split (/,/, $_, 2);
810 if ($key =~ /^[0-9]+$/) {
811 @temp = split (/,/, $rest);
812 $hash->{$key} = \
@temp;
820 my ($filename, $hash) = @_;
821 my ($key, @temp, $i);
823 open(FILE
, ">$filename") or die "Unable to write to file $filename";
825 foreach $key (keys %$hash) {
826 if ($key =~ /^[0-9]+$/) {
828 foreach $i (0 .. $#{$hash->{$key}}) {
829 print FILE
",$hash->{$key}[$i]";
838 sub findhasharraykey
{
839 foreach my $i (1 .. 1000000) {
840 if ( ! exists $_[0]{$i}) {
847 # Darren Critchley - darrenc@telus.net - (c) 2003
848 # &srtarray(SortOrder, AlphaNumeric, SortDirection, ArrayToBeSorted)
849 # This subroutine will take the following parameters:
850 # ColumnNumber = the column which you want to sort on, starts at 1
851 # AlphaNumberic = a or n (lowercase) defines whether the sort should be alpha or numberic
852 # SortDirection = asc or dsc (lowercase) Ascending or Descending sort
853 # ArrayToBeSorted = the array that wants sorting
855 # Returns an array that is sorted to your specs
857 # If SortOrder is greater than the elements in array, then it defaults to the first element
860 my ($colno, $alpnum, $srtdir, @tobesorted) = @_;
866 my $ttlitems = scalar @tobesorted; # want to know the number of rows in the passed array
867 if ($ttlitems < 1){ # if no items, don't waste our time lets leave
868 return (@tobesorted);
870 my @tmp = split(/\,/,$tobesorted[0]);
871 $ttlitems = scalar @tmp; # this should be the number of elements in each row of the passed in array
873 # Darren Critchley - validate parameters
874 if ($colno > $ttlitems){$colno = '1';}
875 $colno--; # remove one from colno to deal with arrays starting at 0
876 if($colno < 0){$colno = '0';}
877 if ($alpnum ne '') { $alpnum = lc($alpnum); } else { $alpnum = 'a'; }
878 if ($srtdir ne '') { $srtdir = lc($srtdir); } else { $srtdir = 'src'; }
880 foreach $line (@tobesorted)
884 my @temp = split(/\,/,$line);
885 # Darren Critchley - juggle the fields so that the one we want to sort on is first
886 my $tmpholder = $temp[0];
887 $temp[0] = $temp[$colno];
888 $temp[$colno] = $tmpholder;
890 for ($ctr=0; $ctr < $ttlitems ; $ctr++) {
891 $newline=$newline . $temp[$ctr] . ",";
894 push(@tmparray,$newline);
897 if ($alpnum eq 'n') {
898 @tmparray = sort {$a <=> $b} @tmparray;
900 @tmparray = (sort @tmparray);
902 foreach $line (@tmparray)
906 my @temp = split(/\,/,$line);
907 my $tmpholder = $temp[0];
908 $temp[0] = $temp[$colno];
909 $temp[$colno] = $tmpholder;
911 for ($ctr=0; $ctr < $ttlitems ; $ctr++){
912 $newline=$newline . $temp[$ctr] . ",";
915 push(@srtedarray,$newline);
919 if ($srtdir eq 'dsc') {
920 @tmparray = reverse(@srtedarray);
923 return (@srtedarray);
929 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
930 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
931 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
932 Net
::SSLeay
::set_proxy
($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
934 my $user_agent = &MakeUserAgent
();
935 my ($out, $response) = Net
::SSLeay
::get_http
( 'checkip4.dns.lightningwirelabs.com',
938 Net
::SSLeay
::make_headers
('User-Agent' => $user_agent )
940 if ($response =~ m
%HTTP/1\
.. 200 OK
%) {
941 $out =~ /Your IP address is: (\d+.\d+.\d+.\d+)/;
948 # Check if hostname.domain provided have IP provided
949 # use gethostbyname to verify that
955 # 1 IP matches host.domain
958 sub DyndnsServiceSync
($;$;$) {
960 my ($ip,$hostName,$domain) = @_;
963 #fix me no ip GROUP, what is the name ?
964 $hostName =~ s/$General::noipprefix//;
965 if ($hostName) { #may be empty
966 $hostName = "$hostName.$domain";
967 @addresses = gethostbyname($hostName);
970 if ($addresses[0] eq '') { # nothing returned ?
971 $hostName = $domain; # try resolving with domain only
972 @addresses = gethostbyname($hostName);
975 if ($addresses[0] ne '') { # got something ?
976 #&General::log("name:$addresses[0], alias:$addresses[1]");
977 # Build clear text list of IP
978 @addresses = map ( &Socket
::inet_ntoa
($_), @addresses[4..$#addresses]);
979 if (grep (/$ip/, @addresses)) {
986 # This sub returns the red IP used to compare in DyndnsServiceSync
990 &General
::readhash
("${General::swroot}/ddns/settings", \
%settings);
992 open(IP
, "${General::swroot}/red/local-ipaddress") or return 'unavailable';
997 # 100.64.0.0/10 is reserved for dual-stack lite (http://tools.ietf.org/html/rfc6598).
998 if (&General
::IpInSubnet
($ip,'10.0.0.0','255.0.0.0') ||
999 &General
::IpInSubnet
($ip,'172.16.0.0.','255.240.0.0') ||
1000 &General
::IpInSubnet
($ip,'192.168.0.0','255.255.0.0') ||
1001 &General
::IpInSubnet
($ip,'100.64.0.0', '255.192.0.0'))
1003 if ($settings{'BEHINDROUTER'} eq 'FETCH_IP') {
1004 my $RealIP = &General
::FetchPublicIp
;
1005 $ip = (&General
::validip
($RealIP) ?
$RealIP : 'unavailable');
1011 # Translate ICMP code to text
1012 # ref: http://www.iana.org/assignments/icmp-parameters
1013 sub GetIcmpDescription
($) {
1015 my @icmp_description = (
1019 'Destination Unreachable',
1022 'Alternate Host Address',
1025 'Router Advertisement',
1026 'Router Solicitation', #10
1028 'Parameter Problem',
1031 'Information Request',
1032 'Information Reply',
1033 'Address Mask Request',
1034 'Address Mask Reply',
1035 'Reserved (for Security)',
1036 'Reserved (for Robustness Experiment)', #20
1047 'Datagram Conversion Error',
1048 'Mobile Host Redirect',
1049 'IPv6 Where-Are-You',
1051 'Mobile Registration Request',
1052 'Mobile Registration Reply',
1053 'Domain Name Request',
1054 'Domain Name Reply',
1058 if ($index>41) {return 'unknown'} else {return $icmp_description[$index]};
1061 sub GetCoreUpdateVersion
() {
1064 open(FILE
, "/opt/pakfire/db/core/mine");
1071 return $core_update;
1074 sub MakeUserAgent
() {
1075 my $user_agent = "IPFire/$General::version";
1077 my $core_update = &GetCoreUpdateVersion
();
1078 if ($core_update ne "") {
1079 $user_agent .= "/$core_update";
1085 sub RedIsWireless
() {
1086 # This function checks if a network device is a wireless device.
1089 &readhash
("${General::swroot}/ethernet/settings", \
%settings);
1091 # Find the name of the network device.
1092 my $device = $settings{'RED_DEV'};
1094 # Exit, if no device is configured.
1095 return 0 if ($device eq "");
1097 # Return 1 if the device is a wireless one.
1098 my $path = "/sys/class/net/$device/wireless";
1103 # Otherwise return zero.
1107 # Function to read a file with UTF-8 charset.
1108 sub read_file_utf8
($) {
1111 open my $in, '<:encoding(UTF-8)', $file or die "Could not open '$file' for reading $!";
1119 # Function to write a file with UTF-8 charset.
1120 sub write_file_utf8
($) {
1121 my ($file, $content) = @_;
1123 open my $out, '>:encoding(UTF-8)', $file or die "Could not open '$file' for writing $!";;
1124 print $out $content;
1130 my $FIREWALL_RELOAD_INDICATOR = "${General::swroot}/firewall/reread";
1132 sub firewall_config_changed
() {
1133 open FILE
, ">$FIREWALL_RELOAD_INDICATOR" or die "Could not open $FIREWALL_RELOAD_INDICATOR";
1137 sub firewall_needs_reload
() {
1138 if (-e
"$FIREWALL_RELOAD_INDICATOR") {
1145 sub firewall_reload
() {
1146 system("/usr/local/bin/firewallctrl");
1149 # Function which will return the used interface for the red network zone (red0, ppp0, etc).
1150 sub get_red_interface
() {
1152 open(IFACE
, "${General::swroot}/red/iface") or die "Could not open /var/ipfire/red/iface";
1154 my $interface = <IFACE
>;
1161 sub dnssec_status
() {
1162 my $path = "${General::swroot}/red/dnssec-status";
1164 open(STATUS
, $path) or return 0;
1165 my $status = <STATUS
>;
1172 sub number_cpu_cores
() {
1173 open my $cpuinfo, "/proc/cpuinfo" or die "Can't open cpuinfo: $!\n";
1174 my $cores = scalar (map /^processor/, <$cpuinfo>);