2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2013 Alexander Marx <amarx@ipfire.org> #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
23 no warnings
'uninitialized';
30 my %customgeoipgrp=();
32 my %customservicegrp=();
41 require '/var/ipfire/general-functions.pl';
43 my $confignet = "${General::swroot}/fwhosts/customnetworks";
44 my $confighost = "${General::swroot}/fwhosts/customhosts";
45 my $configgrp = "${General::swroot}/fwhosts/customgroups";
46 my $configgeoipgrp = "${General::swroot}/fwhosts/customgeoipgrp";
47 my $configsrv = "${General::swroot}/fwhosts/customservices";
48 my $configsrvgrp = "${General::swroot}/fwhosts/customservicegrp";
49 my $configccdnet = "${General::swroot}/ovpn/ccd.conf";
50 my $configccdhost = "${General::swroot}/ovpn/ovpnconfig";
51 my $configipsec = "${General::swroot}/vpn/config";
52 my $configovpn = "${General::swroot}/ovpn/settings";
55 my $netsettings = "${General::swroot}/ethernet/settings";
57 &General
::readhash
("/var/ipfire/ethernet/settings", \
%netsettings);
58 &General
::readhash
("${General::swroot}/ovpn/settings", \
%ovpnsettings);
59 &General
::readhash
("${General::swroot}/vpn/settings", \
%ipsecsettings);
61 &General
::readhasharray
("$confignet", \
%customnetwork);
62 &General
::readhasharray
("$confighost", \
%customhost);
63 &General
::readhasharray
("$configgrp", \
%customgrp);
64 &General
::readhasharray
("$configgeoipgrp", \
%customgeoipgrp);
65 &General
::readhasharray
("$configccdnet", \
%ccdnet);
66 &General
::readhasharray
("$configccdhost", \
%ccdhost);
67 &General
::readhasharray
("$configipsec", \
%ipsecconf);
68 &General
::readhasharray
("$configsrv", \
%customservice);
69 &General
::readhasharray
("$configsrvgrp", \
%customservicegrp);
70 &General
::get_aliases
(\
%aliases);
75 foreach my $key (sort {$a <=> $b} keys %customservice){
76 if($customservice{$key}[0] eq $val){
77 if ($customservice{$key}[0] eq $val){
78 return $customservice{$key}[2];
90 foreach my $key (sort {$a <=> $b} keys %customservicegrp){
91 if($customservicegrp{$key}[0] eq $val){
92 if (&get_srv_prot
($customservicegrp{$key}[2]) eq 'TCP'){
94 }elsif(&get_srv_prot
($customservicegrp{$key}[2]) eq 'UDP'){
96 }elsif(&get_srv_prot
($customservicegrp{$key}[2]) eq 'ICMP'){
99 #Protocols used in servicegroups
100 push (@ips,$customservicegrp{$key}[2]);
104 if ($tcp eq '1'){push (@ips,'TCP');}
105 if ($udp eq '1'){push (@ips,'UDP');}
106 if ($icmp eq '1'){push (@ips,'ICMP');}
107 my $back=join(",",@ips);
116 foreach my $key (sort {$a <=> $b} keys %customservice){
117 if($customservice{$key}[0] eq $val && $customservice{$key}[2] eq $prot){
118 return $customservice{$key}[$field];
129 foreach my $key (sort {$a <=> $b} keys %customservicegrp){
130 if($customservicegrp{$key}[0] eq $val){
131 if ($prot ne 'ICMP'){
132 $value=&get_srv_port
($customservicegrp{$key}[2],1,$prot);
133 }elsif ($prot eq 'ICMP'){
134 $value=&get_srv_port
($customservicegrp{$key}[2],3,$prot);
136 push (@ips,$value) if ($value ne '') ;
140 if ($#ips gt 0){$back="-m multiport --dports ";}else{$back="--dport ";}
141 }elsif ($prot eq 'ICMP'){
142 $back="--icmp-type ";
145 $back.=join(",",@ips);
152 foreach my $key (sort {$a <=> $b} keys %ipsecconf){
153 if($ipsecconf{$key}[1] eq $val){
154 return $ipsecconf{$key}[$field];
158 sub get_ipsec_host_ip
162 foreach my $key (sort {$a <=> $b} keys %ipsecconf){
163 if($ipsecconf{$key}[1] eq $val){
164 return $ipsecconf{$key}[$field];
172 foreach my $key (sort {$a <=> $b} keys %ccdhost){
173 if($ccdhost{$key}[1] eq $val){
174 return $ccdhost{$key}[$field];
182 foreach my $key (sort {$a <=> $b} keys %ccdhost){
183 if($ccdhost{$key}[1] eq $val){
184 return $ccdhost{$key}[$field];
193 foreach my $key (sort {$a <=> $b} keys %ccdnet){
194 if($ccdnet{$key}[0] eq $val){
195 return $ccdnet{$key}[$field];
203 foreach my $key (sort {$a <=> $b} keys %customgrp){
204 if ($customgrp{$key}[0] eq $val){
205 &get_address
($customgrp{$key}[3],$src);
215 return "0.0.0.0/0.0.0.0";
216 }elsif($val eq 'GREEN'){
217 return "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
218 }elsif($val eq 'ORANGE'){
219 return "$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}";
220 }elsif($val eq 'BLUE'){
221 return "$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}";
222 }elsif($val eq 'RED'){
224 }elsif($val =~ /OpenVPN/i){
225 return "$ovpnsettings{'DOVPN_SUBNET'}";
226 }elsif($val =~ /IPsec/i){
227 return "$ipsecsettings{'RW_NET'}";
228 }elsif($val eq 'IPFire'){
235 if($net eq "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}"){
236 return "$netsettings{'GREEN_DEV'}";
238 if($net eq "$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}"){
239 return "$netsettings{'ORANGE_DEV'}";
241 if($net eq "$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}"){
242 return "$netsettings{'BLUE_DEV'}";
244 if($net eq "0.0.0.0/0") {
245 return &get_external_interface
();
252 foreach my $key (sort {$a <=> $b} keys %customnetwork){
253 if($customnetwork{$key}[0] eq $val){
254 return "$customnetwork{$key}[1]/$customnetwork{$key}[2]";
262 foreach my $key (sort {$a <=> $b} keys %customhost){
263 if($customhost{$key}[0] eq $val){
264 if ($customhost{$key}[1] eq 'mac' && $src eq 'src'){
265 return "-m mac --mac-source $customhost{$key}[2]";
266 }elsif($customhost{$key}[1] eq 'ip' && $src eq 'src'){
267 return "$customhost{$key}[2]";
268 }elsif($customhost{$key}[1] eq 'ip' && $src eq 'tgt'){
269 return "$customhost{$key}[2]";
270 }elsif($customhost{$key}[1] eq 'mac' && $src eq 'tgt'){
287 if ($type eq "src") {
288 $addr_type = $$hash{$key}[3];
289 $value = $$hash{$key}[4];
291 } elsif ($type eq "tgt") {
292 $addr_type = $$hash{$key}[5];
293 $value = $$hash{$key}[6];
296 if ($addr_type ~~ ["cust_grp_src", "cust_grp_tgt"]) {
297 foreach my $grp (sort {$a <=> $b} keys %customgrp) {
298 if ($customgrp{$grp}[0] eq $value) {
299 my @address = &get_address
($customgrp{$grp}[3], $customgrp{$grp}[2], $type);
302 push(@addresses, @address);
306 }elsif ($addr_type ~~ ["cust_geoip_src", "cust_geoip_tgt"] && $value =~ "group:") {
307 $value=substr($value,6);
308 foreach my $grp (sort {$a <=> $b} keys %customgeoipgrp) {
309 if ($customgeoipgrp{$grp}[0] eq $value) {
310 my @address = &get_address
($addr_type, $customgeoipgrp{$grp}[2], $type);
313 push(@addresses, @address);
318 my @address = &get_address
($addr_type, $value, $type);
321 push(@addresses, @address);
335 # If the user manually typed an address, we just check if it is a MAC
336 # address. Otherwise, we assume that it is an IP address.
337 if ($key ~~ ["src_addr", "tgt_addr"]) {
338 if (&General
::validmac
($value)) {
339 push(@ret, ["-m mac --mac-source $value", ""]);
341 push(@ret, [$value, ""]);
344 # If a default network interface (GREEN, BLUE, etc.) is selected, we
345 # try to get the corresponding address of the network.
346 } elsif ($key ~~ ["std_net_src", "std_net_tgt", "Standard Network"]) {
347 my $external_interface = &get_external_interface
();
349 my $network_address = &get_std_net_ip
($value, $external_interface);
351 if ($network_address) {
352 my $interface = &get_interface
($network_address);
353 push(@ret, [$network_address, $interface]);
357 } elsif ($key ~~ ["cust_net_src", "cust_net_tgt", "Custom Network"]) {
358 my $network_address = &get_net_ip
($value);
359 if ($network_address) {
360 push(@ret, [$network_address, ""]);
364 } elsif ($key ~~ ["cust_host_src", "cust_host_tgt", "Custom Host"]) {
365 my $host_address = &get_host_ip
($value, $type);
367 push(@ret, [$host_address, ""]);
371 } elsif ($key ~~ ["ovpn_net_src", "ovpn_net_tgt", "OpenVPN static network"]) {
372 my $network_address = &get_ovpn_net_ip
($value, 1);
373 if ($network_address) {
374 push(@ret, [$network_address, ""]);
378 } elsif ($key ~~ ["ovpn_host_src", "ovpn_host_tgt", "OpenVPN static host"]) {
379 my $host_address = &get_ovpn_host_ip
($value, 33);
381 push(@ret, [$host_address, ""]);
385 } elsif ($key ~~ ["ovpn_n2n_src", "ovpn_n2n_tgt", "OpenVPN N-2-N"]) {
386 my $network_address = &get_ovpn_n2n_ip
($value, 11);
387 if ($network_address) {
388 push(@ret, [$network_address, ""]);
392 } elsif ($key ~~ ["ipsec_net_src", "ipsec_net_tgt", "IpSec Network"]) {
393 my $network_address = &get_ipsec_net_ip
($value, 11);
394 if ($network_address) {
395 push(@ret, [$network_address, ""]);
398 # The firewall's own IP addresses.
399 } elsif ($key ~~ ["ipfire", "ipfire_src"]) {
401 if ($value eq "ALL") {
402 push(@ret, ["0/0", ""]);
405 } elsif ($value eq "GREEN") {
406 push(@ret, [$netsettings{"GREEN_ADDRESS"}, ""]);
409 } elsif ($value eq "BLUE") {
410 push(@ret, [$netsettings{"BLUE_ADDRESS"}, ""]);
413 } elsif ($value eq "ORANGE") {
414 push(@ret, [$netsettings{"ORANGE_ADDRESS"}, ""]);
417 } elsif ($value ~~ ["RED", "RED1"]) {
418 my $address = &get_external_address
();
420 push(@ret, [$address, ""]);
425 my $alias = &get_alias
($value);
427 push(@ret, [$alias, ""]);
431 # Handle rule options with GeoIP as source.
432 } elsif ($key eq "cust_geoip_src") {
433 # Get external interface.
434 my $external_interface = &get_external_interface
();
436 push(@ret, ["-m geoip --src-cc $value", "$external_interface"]);
438 # Handle rule options with GeoIP as target.
439 } elsif ($key eq "cust_geoip_tgt") {
440 # Get external interface.
441 my $external_interface = &get_external_interface
();
443 push(@ret, ["-m geoip --dst-cc $value", "$external_interface"]);
445 # If nothing was selected, we assume "any".
447 push(@ret, ["0/0", ""]);
452 sub get_external_interface
()
454 open(IFACE
, "/var/ipfire/red/iface") or return "";
460 sub get_external_address
()
462 open(ADDR
, "/var/ipfire/red/local-ipaddress") or return "";
463 my $address = <ADDR
>;
472 foreach my $alias (sort keys %aliases) {
474 return $aliases{$alias}{"IPT"};
479 sub get_nat_address
{
483 # Any static address of any zone.
484 if ($zone eq "AUTO") {
485 if ($source && ($source !~ m/mac/i )) {
486 my $firewall_ip = &get_internal_firewall_ip_address
($source, 1);
491 $firewall_ip = &get_matching_firewall_address
($source, 1);
497 return &get_external_address
();
499 } elsif ($zone eq "RED" || $zone eq "GREEN" || $zone eq "ORANGE" || $zone eq "BLUE") {
500 return $netsettings{$zone . "_ADDRESS"};
502 } elsif ($zone ~~ ["Default IP", "ALL"]) {
503 return &get_external_address
();
506 my $alias = &get_alias
($zone);
508 $alias = &get_external_address
();
513 print_error
("Could not find NAT address");
516 sub get_internal_firewall_ip_addresses
518 my $use_orange = shift;
520 my @zones = ("GREEN", "BLUE");
522 push(@zones, "ORANGE");
526 for my $zone (@zones) {
527 next unless (exists $netsettings{$zone . "_ADDRESS"});
529 my $zone_address = $netsettings{$zone . "_ADDRESS"};
530 push(@addresses, $zone_address);
535 sub get_matching_firewall_address
538 my $use_orange = shift;
540 my ($address, $netmask) = split("/", $addr);
542 my @zones = ("GREEN", "BLUE");
544 push(@zones, "ORANGE");
547 foreach my $zone (@zones) {
548 next unless (exists $netsettings{$zone . "_ADDRESS"});
550 my $zone_subnet = $netsettings{$zone . "_NETADDRESS"};
551 my $zone_mask = $netsettings{$zone . "_NETMASK"};
553 if (&General
::IpInSubnet
($address, $zone_subnet, $zone_mask)) {
554 return $netsettings{$zone . "_ADDRESS"};
560 sub get_internal_firewall_ip_address
563 my $use_orange = shift;
565 my ($net_address, $net_mask) = split("/", $subnet);
566 if ((!$net_mask) || ($net_mask ~~ ["32", "255.255.255.255"])) {
570 # Convert net mask into correct format for &General::IpInSubnet().
571 $net_mask = &General
::iporsubtodec
($net_mask);
573 my @addresses = &get_internal_firewall_ip_addresses
($use_orange);
574 foreach my $zone_address (@addresses) {
575 if (&General
::IpInSubnet
($zone_address, $net_address, $net_mask)) {
576 return $zone_address;
583 sub get_geoip_locations
() {
584 # Path to the directory which contains the binary geoip
586 my $directory="/usr/share/xt_geoip/LE";
588 # Array to store the final country list.
589 my @country_codes = ();
591 # Open location and do a directory listing.
592 opendir(DIR
, "$directory");
593 my @locations = readdir(DIR
);
596 # Loop through the directory listing, and cut of the file extensions.
597 foreach my $location (sort @locations) {
599 next if($location =~ /^\.$/);
600 next if($location =~ /^\.\.$/);
602 # Remove whitespaces.
605 # Cut-off file extension.
606 my ($country_code, $extension) = split(/\./, $location);
608 # Add country code to array.
609 push(@country_codes, $country_code);
612 # Return final array.
613 return @country_codes;