]>
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 $
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";
152 ($var, $val) = split /=/, $_, 2;
158 # Untaint variables read from hash
159 # trim space from begin and end
162 $var =~ /([A-Za-z0-9_-]*)/;
166 $hash->{$var} = $val;
175 my $filename = $_[0];
179 # write cgi vars to the file.
180 open(FILE
, ">${filename}") or die "Unable to write file $filename";
182 foreach $var (keys %$hash)
184 if ( $var eq "__CGI__"){next;}
185 $val = $hash->{$var};
186 # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
187 # location of the mouse are submitted as well, this was being written to the settings file causing
188 # some serious grief! This skips the variable.x and variable.y
189 if (!($var =~ /(.x|.y)$/)) {
192 if (!($var =~ /^ACTION/)) {
193 print FILE
"${var}=${val}\n"; }
201 # This function replaces the given hash in the original hash by keeping the old
202 # content and just replacing the new content
204 my $filename = $_[0];
209 readhash
("${filename}", \
%oldhash);
211 foreach $var (keys %$newhash){
212 $oldhash{$var}=$newhash->{$var};
215 # write cgi vars to the file.
216 open(FILE
, ">${filename}") or die "Unable to write file $filename";
218 foreach $var (keys %oldhash)
220 if ( $var eq "__CGI__"){next;}
221 $val = $oldhash{$var};
222 # Darren Critchley Jan 17, 2003 added the following because when submitting with a graphic, the x and y
223 # location of the mouse are submitted as well, this was being written to the settings file causing
224 # some serious grief! This skips the variable.x and variable.y
225 if (!($var =~ /(.x|.y)$/)) {
228 if (!($var =~ /^ACTION/)) {
229 print FILE
"${var}=${val}\n"; }
236 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
237 $atime, $mtime, $ctime, $blksize, $blocks) = stat $_[0];
238 my $totalsecs = time() - $mtime;
241 my $secs = $totalsecs % 60;
244 push(@s, "${secs}s");
247 my $min = $totalsecs % 60;
253 my $hrs = $totalsecs % 24;
259 my $days = int($totalsecs);
261 push(@s, "${days}d");
264 return join(" ", reverse(@s));
271 if (!($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)) {
275 my @octets = ($1, $2, $3, $4);
280 if ($_ < 0 || $_ > 255) {
290 return &Network
::check_netmask
($mask) || &Network
::check_prefix
($mask);
295 my $ipormask = $_[0];
297 # see if it is a IP only.
298 if (&validip
($ipormask)) {
300 # split it into number and mask.
301 if (!($ipormask =~ /^(.*?)\/(.*?
)$/)) {
305 # first part not a ip?
306 if (!(&validip
($ip))) {
308 return &validmask
($mask);
312 return &Network
::convert_netmask2prefix
(shift);
316 return &Network
::convert_prefix2netmask
(shift);
321 #Gets: Ip address or subnetmask in decimal oder CIDR
322 #Gives: What it gets only in CIDR format
327 if ($subnet =~ /^(.*?)\/(.*?
)$/) {
328 ($net,$mask) = split (/\//,$subnet);
334 #Subnet already in decimal and valid?
335 if ($mask=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
336 for (my $i=0;$i<=32;$i++){
337 if (&General
::cidrtosub
($i) eq $mask){
338 if ($full == 0){return $mask;}else{
339 return $net."/".$mask;
344 #Subnet in binary format?
345 if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=0))){
346 if($full == 0){ return &General
::cidrtosub
($mask);}else{
347 return $net."/".&General
::cidrtosub
($mask);
358 #gets: Ip Address or subnetmask in decimal oder CIDR
359 #Gives: What it gets only in CIDR format
364 if ($subnet =~ /^(.*?)\/(.*?
)$/) {
365 ($net,$mask) = split (/\//,$subnet);
370 #Subnet in decimal and valid?
371 if ($mask=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
372 for (my $i=0;$i<=32;$i++){
373 if (&General
::cidrtosub
($i) eq $mask){
374 if ($full == 0){return &General
::subtocidr
($mask);}else{
375 return $net."/".&General
::subtocidr
($mask);
380 #Subnet already in binary format?
381 if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=0))){
382 if($full == 0){ return $mask;}else{
383 return $net."/".$mask;
392 my $arg = join("/", @_);
394 return &Network
::get_netaddress
($arg);
399 #Gets: IP in Form ("192.168.0.0/24")
400 #Gives: Broadcastaddress of network
402 my ($ccdip,$ccdsubnet) = split "/",$ccdnet;
403 my $ip_address_binary = inet_aton
( $ccdip );
404 my $netmask_binary = ~pack("N", (2**(32-$ccdsubnet))-1);
405 my $broadcast_address = inet_ntoa
( $ip_address_binary | ~$netmask_binary );
406 return $broadcast_address;
410 return &Network
::ip2bin
(shift);
414 return &Network
::bin2ip
(shift);
418 return &Network
::find_next_ip_address
(shift, 4);
422 return &Network
::find_next_ip_address
(shift, -1);
427 #Gets: Ip address in 192.168.0.0/24 or 192.168.0.0/255.255.255.0 and checks if subnet valid
428 #Gives: True bzw 0 if success or false
432 if (!($ccdnet =~ /^(.*?)\/(.*?
)$/)) {
435 my ($ccdip,$ccdsubnet)=split (/\//, $ccdnet);
437 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 ))) {
438 #Subnet in decimal and valid?
439 if ($ccdsubnet=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
440 for (my $i=0;$i<=32;$i++){
441 if (&General
::cidrtosub
($i) eq $ccdsubnet){
445 #Subnet already in binary format?
446 }elsif ($ccdsubnet=~/^(\d{1,2})$/ && (($1<=32 && $1>=0))){
469 my ($ip,$cidr)=split(/\//,$ccdnet);
470 $cidr=&iporsubtocidr
($cidr);
472 #get OVPN-Subnet (dynamic range)
473 &readhash
("${General::swroot}/ovpn/settings", \
%ovpnconf);
474 my ($ovpnip,$ovpncidr)= split (/\//,$ovpnconf{'DOVPN_SUBNET'});
475 $ovpncidr=&iporsubtocidr
($ovpncidr);
477 #check if we try to use same network as ovpn server
478 if ("$ip/$cidr" eq "$ovpnip/$ovpncidr") {
479 $errormessage=$errormessage.$Lang::tr
{'ccd err isovpnnet'}."<br>";
480 return $errormessage;
483 #check if we try to use same network as another ovpn N2N
484 if($ownnet ne 'ovpn'){
485 &readhasharray
("${General::swroot}/ovpn/ovpnconfig", \
%ovpnconfhash);
486 foreach my $key (keys %ovpnconfhash) {
487 if ($ovpnconfhash{$key}[3] eq 'net'){
488 my @ovpnnet=split (/\//,$ovpnconfhash{$key}[11]);
489 if (&IpInSubnet
($ip,$ovpnnet[0],&iporsubtodec
($ovpnnet[1]))){
490 $errormessage=$errormessage.$Lang::tr
{'ccd err isovpnn2n'}." $ovpnconfhash{$key}[1] <br>";
491 return $errormessage;
497 #check if we use a network-name/subnet (static-ovpn) that already exists
498 &readhasharray
("${General::swroot}/ovpn/ccd.conf", \
%ccdconfhash);
499 foreach my $key (keys %ccdconfhash) {
500 @ccdconf=split(/\//,$ccdconfhash{$key}[1]);
501 if ($ccdname eq $ccdconfhash{$key}[0])
503 $errormessage=$errormessage.$Lang::tr
{'ccd err nameexist'}."<br>";
504 return $errormessage;
506 my ($newip,$newsub) = split(/\//,$ccdnet);
507 if (&IpInSubnet
($newip,$ccdconf[0],&iporsubtodec
($ccdconf[1])))
509 $errormessage=$errormessage.$Lang::tr
{'ccd err issubnet'}." $ccdconfhash{$key}[0]<br>";
510 return $errormessage;
514 #check if we use a ipsec right network which is already defined
515 if($ownnet ne 'ipsec'){
516 &General
::readhasharray
("${General::swroot}/vpn/config", \
%ipsecconf);
517 foreach my $key (keys %ipsecconf){
518 if ($ipsecconf{$key}[11] ne ''){
519 foreach my $ipsecsubitem (split(/\|/, $ipsecconf{$key}[11])) {
520 my ($ipsecip,$ipsecsub) = split (/\//, $ipsecconf{$key}[11]);
521 $ipsecsub=&iporsubtodec
($ipsecsub);
522 if($ipsecconf{$key}[1] ne $ccdname){
523 if ( &IpInSubnet
($ip,$ipsecip,$ipsecsub) ){
524 $errormessage=$Lang::tr
{'ccd err isipsecnet'}." Name: $ipsecconf{$key}[1]";
525 return $errormessage;
533 #check if we use the ipsec RW Network (if defined)
534 &readhash
("${General::swroot}/vpn/settings", \
%vpnconf);
535 if ($vpnconf{'RW_NET'} ne ''){
536 my ($ipsecrwnet,$ipsecrwsub)=split (/\//, $vpnconf{'RW_NET'});
537 if (&IpInSubnet
($ip,$ipsecrwnet,&iporsubtodec
($ipsecrwsub)))
539 $errormessage=$errormessage.$Lang::tr
{'ccd err isipsecrw'}."<br>";
540 return $errormessage;
544 #call check_net_internal
545 &General
::check_net_internal
($ccdnet);
548 sub check_net_internal
{
550 my ($ip,$cidr)=split(/\//,$network);
553 $cidr=&iporsubtocidr
($cidr);
554 #check if we use one of ipfire's networks (green,orange,blue)
555 &readhash
("${General::swroot}/ethernet/settings", \
%ownnet);
556 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;}
557 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;}
558 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;}
559 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;}
570 if ($_ >= 1 && $_ <= 65535) {
583 if ($_ == 53 || $_ == 222 || $_ == 444 || $_ == 81 ) {
585 elsif ($_ >= 1 && $_ <= 65535) {
592 my $checkmac = $_[0];
593 my $ot = '[0-9a-f]{2}'; # 2 Hex digits (one octet)
594 if ($checkmac !~ /^$ot:$ot:$ot:$ot:$ot:$ot$/i)
603 # Checks a hostname against RFC1035
604 my $hostname = $_[0];
606 # Each part should be at least two characters in length
607 # but no more than 63 characters
608 if (length ($hostname) < 1 || length ($hostname) > 63) {
610 # Only valid characters are a-z, A-Z, 0-9 and -
611 if ($hostname !~ /^[a-zA-Z0-9-\s]*$/) {
613 # First character can only be a letter or a digit
614 if (substr ($hostname, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
616 # Last character can only be a letter or a digit
617 if (substr ($hostname, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
626 # Checks a domain name against RFC1035
627 my $domainname = $_[0];
628 my @parts = split (/\./, $domainname); # Split hostname at the '.'
630 foreach $part (@parts) {
631 # Each part should be no more than 63 characters in length
632 if (length ($part) < 1 || length ($part) > 63) {
634 # Only valid characters are a-z, A-Z, 0-9, _ and -
635 if ($part !~ /^[a-zA-Z0-9_-]*$/) {
646 # Checks a fully qualified domain name against RFC1035
648 my @parts = split (/\./, $fqdn); # Split hostname at the '.'
649 if (scalar(@parts) < 2) { # At least two parts should
650 return 0;} # exist in a FQDN
651 # (i.e.hostname.domain)
652 foreach $part (@parts) {
653 # Each part should be at least one character in length
654 # but no more than 63 characters
655 if (length ($part) < 1 || length ($part) > 63) {
657 # Only valid characters are a-z, A-Z, 0-9 and -
658 if ($part !~ /^[a-zA-Z0-9-]*$/) {
660 # First character can only be a letter or a digit
661 if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
663 # Last character can only be a letter or a digit
664 if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
670 sub validportrange
# used to check a port range
672 my $port = $_[0]; # port values
673 $port =~ tr/-/:/; # replace all - with colons just in case someone used -
674 my $srcdst = $_[1]; # is it a source or destination port
676 if (!($port =~ /^(\d+)\:(\d+)$/)) {
678 if (!(&validport
($port))) {
679 if ($srcdst eq 'src'){
680 return $Lang::tr
{'source port numbers'};
682 return $Lang::tr
{'destination port numbers'};
688 my @ports = ($1, $2);
690 if ($srcdst eq 'src'){
691 return $Lang::tr
{'bad source range'};
693 return $Lang::tr
{'bad destination range'};
698 if (!(&validport
($_))) {
699 if ($srcdst eq 'src'){
700 return $Lang::tr
{'source port numbers'};
702 return $Lang::tr
{'destination port numbers'};
715 return &Network
::ip_address_in_network
($addr, "$network/$netmask");
719 # Return the following IP (IP+1) in dotted notation.
720 # Call: NextIP ('1.1.1.1');
724 return &Network
::find_next_ip_address
(shift, 1);
728 return &Network
::find_next_ip_address
(shift, 4);
732 my ($ip,$cidr) = &Net
::IPv4Addr
::ipv4_parse
(shift);
737 my ($ip,$cidr) = &Net
::IPv4Addr
::ipv4_parse
(shift);
738 my $netmask = &Net
::IPv4Addr
::ipv4_cidr2msk
($cidr);
739 return "$ip\/$netmask";
744 my @parts = split( /\@/, $address );
747 #check if we have one part before and after '@'
748 return 0 if ( $count != 2 );
750 #check if one of the parts starts or ends with a dot
751 return 0 if ( substr($parts[0],0,1) eq '.' );
752 return 0 if ( substr($parts[0],-1,1) eq '.' );
753 return 0 if ( substr($parts[1],0,1) eq '.' );
754 return 0 if ( substr($parts[1],-1,1) eq '.' );
756 #check first addresspart (before '@' sign)
757 return 0 if ( $parts[0] !~ m/^[a-zA-Z0-9\.!\-\+#]+$/ );
759 #check second addresspart (after '@' sign)
760 return 0 if ( $parts[1] !~ m/^[a-zA-Z0-9\.\-]+$/ );
766 # Currently only vpnmain use this three procs (readhasharray, writehasharray, findhasharray)
767 # The 'key' used is numeric but is perfectly unneeded! This will to be removed so don't use
768 # this code. Vpnmain will be splitted in parts: x509/pki, connection ipsec, connection other,... .
771 my ($filename, $hash) = @_;
774 open(FILE
, $filename) or die "Unable to read file $filename";
777 my ($key, $rest, @temp);
779 ($key, $rest) = split (/,/, $_, 2);
780 if ($key =~ /^[0-9]+$/) {
781 @temp = split (/,/, $rest);
782 $hash->{$key} = \
@temp;
790 my ($filename, $hash) = @_;
791 my ($key, @temp, $i);
793 open(FILE
, ">$filename") or die "Unable to write to file $filename";
795 foreach $key (keys %$hash) {
796 if ($key =~ /^[0-9]+$/) {
798 foreach $i (0 .. $#{$hash->{$key}}) {
799 print FILE
",$hash->{$key}[$i]";
808 sub findhasharraykey
{
809 foreach my $i (1 .. 1000000) {
810 if ( ! exists $_[0]{$i}) {
817 # Darren Critchley - darrenc@telus.net - (c) 2003
818 # &srtarray(SortOrder, AlphaNumeric, SortDirection, ArrayToBeSorted)
819 # This subroutine will take the following parameters:
820 # ColumnNumber = the column which you want to sort on, starts at 1
821 # AlphaNumberic = a or n (lowercase) defines whether the sort should be alpha or numberic
822 # SortDirection = asc or dsc (lowercase) Ascending or Descending sort
823 # ArrayToBeSorted = the array that wants sorting
825 # Returns an array that is sorted to your specs
827 # If SortOrder is greater than the elements in array, then it defaults to the first element
830 my ($colno, $alpnum, $srtdir, @tobesorted) = @_;
836 my $ttlitems = scalar @tobesorted; # want to know the number of rows in the passed array
837 if ($ttlitems < 1){ # if no items, don't waste our time lets leave
838 return (@tobesorted);
840 my @tmp = split(/\,/,$tobesorted[0]);
841 $ttlitems = scalar @tmp; # this should be the number of elements in each row of the passed in array
843 # Darren Critchley - validate parameters
844 if ($colno > $ttlitems){$colno = '1';}
845 $colno--; # remove one from colno to deal with arrays starting at 0
846 if($colno < 0){$colno = '0';}
847 if ($alpnum ne '') { $alpnum = lc($alpnum); } else { $alpnum = 'a'; }
848 if ($srtdir ne '') { $srtdir = lc($srtdir); } else { $srtdir = 'src'; }
850 foreach $line (@tobesorted)
854 my @temp = split(/\,/,$line);
855 # Darren Critchley - juggle the fields so that the one we want to sort on is first
856 my $tmpholder = $temp[0];
857 $temp[0] = $temp[$colno];
858 $temp[$colno] = $tmpholder;
860 for ($ctr=0; $ctr < $ttlitems ; $ctr++) {
861 $newline=$newline . $temp[$ctr] . ",";
864 push(@tmparray,$newline);
867 if ($alpnum eq 'n') {
868 @tmparray = sort {$a <=> $b} @tmparray;
870 @tmparray = (sort @tmparray);
872 foreach $line (@tmparray)
876 my @temp = split(/\,/,$line);
877 my $tmpholder = $temp[0];
878 $temp[0] = $temp[$colno];
879 $temp[$colno] = $tmpholder;
881 for ($ctr=0; $ctr < $ttlitems ; $ctr++){
882 $newline=$newline . $temp[$ctr] . ",";
885 push(@srtedarray,$newline);
889 if ($srtdir eq 'dsc') {
890 @tmparray = reverse(@srtedarray);
893 return (@srtedarray);
899 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
900 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
901 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
902 Net
::SSLeay
::set_proxy
($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
904 my $user_agent = &MakeUserAgent
();
905 my ($out, $response) = Net
::SSLeay
::get_http
( 'checkip4.dns.lightningwirelabs.com',
908 Net
::SSLeay
::make_headers
('User-Agent' => $user_agent )
910 if ($response =~ m
%HTTP/1\
.. 200 OK
%) {
911 $out =~ /Your IP address is: (\d+.\d+.\d+.\d+)/;
918 # Check if hostname.domain provided have IP provided
919 # use gethostbyname to verify that
925 # 1 IP matches host.domain
928 sub DyndnsServiceSync
($;$;$) {
930 my ($ip,$hostName,$domain) = @_;
933 #fix me no ip GROUP, what is the name ?
934 $hostName =~ s/$General::noipprefix//;
935 if ($hostName) { #may be empty
936 $hostName = "$hostName.$domain";
937 @addresses = gethostbyname($hostName);
940 if ($addresses[0] eq '') { # nothing returned ?
941 $hostName = $domain; # try resolving with domain only
942 @addresses = gethostbyname($hostName);
945 if ($addresses[0] ne '') { # got something ?
946 #&General::log("name:$addresses[0], alias:$addresses[1]");
947 # Build clear text list of IP
948 @addresses = map ( &Socket
::inet_ntoa
($_), @addresses[4..$#addresses]);
949 if (grep (/$ip/, @addresses)) {
956 # This sub returns the red IP used to compare in DyndnsServiceSync
960 &General
::readhash
("${General::swroot}/ddns/settings", \
%settings);
962 open(IP
, "${General::swroot}/red/local-ipaddress") or return 'unavailable';
967 # 100.64.0.0/10 is reserved for dual-stack lite (http://tools.ietf.org/html/rfc6598).
968 if (&General
::IpInSubnet
($ip,'10.0.0.0','255.0.0.0') ||
969 &General
::IpInSubnet
($ip,'172.16.0.0.','255.240.0.0') ||
970 &General
::IpInSubnet
($ip,'192.168.0.0','255.255.0.0') ||
971 &General
::IpInSubnet
($ip,'100.64.0.0', '255.192.0.0'))
973 if ($settings{'BEHINDROUTER'} eq 'FETCH_IP') {
974 my $RealIP = &General
::FetchPublicIp
;
975 $ip = (&General
::validip
($RealIP) ?
$RealIP : 'unavailable');
981 # Translate ICMP code to text
982 # ref: http://www.iana.org/assignments/icmp-parameters
983 sub GetIcmpDescription
($) {
985 my @icmp_description = (
989 'Destination Unreachable',
992 'Alternate Host Address',
995 'Router Advertisement',
996 'Router Solicitation', #10
1001 'Information Request',
1002 'Information Reply',
1003 'Address Mask Request',
1004 'Address Mask Reply',
1005 'Reserved (for Security)',
1006 'Reserved (for Robustness Experiment)', #20
1017 'Datagram Conversion Error',
1018 'Mobile Host Redirect',
1019 'IPv6 Where-Are-You',
1021 'Mobile Registration Request',
1022 'Mobile Registration Reply',
1023 'Domain Name Request',
1024 'Domain Name Reply',
1028 if ($index>41) {return 'unknown'} else {return $icmp_description[$index]};
1031 sub GetCoreUpdateVersion
() {
1034 open(FILE
, "/opt/pakfire/db/core/mine");
1041 return $core_update;
1044 sub MakeUserAgent
() {
1045 my $user_agent = "IPFire/$General::version";
1047 my $core_update = &GetCoreUpdateVersion
();
1048 if ($core_update ne "") {
1049 $user_agent .= "/$core_update";
1055 sub RedIsWireless
() {
1056 # This function checks if a network device is a wireless device.
1059 &readhash
("${General::swroot}/ethernet/settings", \
%settings);
1061 # Find the name of the network device.
1062 my $device = $settings{'RED_DEV'};
1064 # Exit, if no device is configured.
1065 return 0 if ($device eq "");
1067 # Return 1 if the device is a wireless one.
1068 my $path = "/sys/class/net/$device/wireless";
1073 # Otherwise return zero.
1077 # Function to read a file with UTF-8 charset.
1078 sub read_file_utf8
($) {
1081 open my $in, '<:encoding(UTF-8)', $file or die "Could not open '$file' for reading $!";
1089 # Function to write a file with UTF-8 charset.
1090 sub write_file_utf8
($) {
1091 my ($file, $content) = @_;
1093 open my $out, '>:encoding(UTF-8)', $file or die "Could not open '$file' for writing $!";;
1094 print $out $content;
1100 my $FIREWALL_RELOAD_INDICATOR = "${General::swroot}/firewall/reread";
1102 sub firewall_config_changed
() {
1103 open FILE
, ">$FIREWALL_RELOAD_INDICATOR" or die "Could not open $FIREWALL_RELOAD_INDICATOR";
1107 sub firewall_needs_reload
() {
1108 if (-e
"$FIREWALL_RELOAD_INDICATOR") {
1115 sub firewall_reload
() {
1116 system("/usr/local/bin/firewallctrl");
1119 # Function which will return the used interface for the red network zone (red0, ppp0, etc).
1120 sub get_red_interface
() {
1122 open(IFACE
, "${General::swroot}/red/iface") or die "Could not open /var/ipfire/red/iface";
1124 my $interface = <IFACE
>;