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 my @nets = split(/\|/, $network_address);
395 foreach my $net (@nets) {
396 push(@ret, [$net, ""]);
399 # The firewall's own IP addresses.
400 } elsif ($key ~~ ["ipfire", "ipfire_src"]) {
402 if ($value eq "ALL") {
403 push(@ret, ["0/0", ""]);
406 } elsif ($value eq "GREEN") {
407 push(@ret, [$netsettings{"GREEN_ADDRESS"}, ""]);
410 } elsif ($value eq "BLUE") {
411 push(@ret, [$netsettings{"BLUE_ADDRESS"}, ""]);
414 } elsif ($value eq "ORANGE") {
415 push(@ret, [$netsettings{"ORANGE_ADDRESS"}, ""]);
418 } elsif ($value ~~ ["RED", "RED1"]) {
419 my $address = &get_external_address
();
421 push(@ret, [$address, ""]);
426 my $alias = &get_alias
($value);
428 push(@ret, [$alias, ""]);
432 # Handle rule options with GeoIP as source.
433 } elsif ($key eq "cust_geoip_src") {
434 # Get external interface.
435 my $external_interface = &get_external_interface
();
437 push(@ret, ["-m geoip --src-cc $value", "$external_interface"]);
439 # Handle rule options with GeoIP as target.
440 } elsif ($key eq "cust_geoip_tgt") {
441 # Get external interface.
442 my $external_interface = &get_external_interface
();
444 push(@ret, ["-m geoip --dst-cc $value", "$external_interface"]);
446 # If nothing was selected, we assume "any".
448 push(@ret, ["0/0", ""]);
453 sub get_external_interface
()
455 open(IFACE
, "/var/ipfire/red/iface") or return "";
461 sub get_external_address
()
463 open(ADDR
, "/var/ipfire/red/local-ipaddress") or return "";
464 my $address = <ADDR
>;
473 foreach my $alias (sort keys %aliases) {
475 return $aliases{$alias}{"IPT"};
480 sub get_nat_address
{
484 # Any static address of any zone.
485 if ($zone eq "AUTO") {
486 if ($source && ($source !~ m/mac/i )) {
487 my $firewall_ip = &get_internal_firewall_ip_address
($source, 1);
492 $firewall_ip = &get_matching_firewall_address
($source, 1);
498 return &get_external_address
();
500 } elsif ($zone eq "RED" || $zone eq "GREEN" || $zone eq "ORANGE" || $zone eq "BLUE") {
501 return $netsettings{$zone . "_ADDRESS"};
503 } elsif ($zone ~~ ["Default IP", "ALL"]) {
504 return &get_external_address
();
507 my $alias = &get_alias
($zone);
509 $alias = &get_external_address
();
514 print_error
("Could not find NAT address");
517 sub get_internal_firewall_ip_addresses
519 my $use_orange = shift;
521 my @zones = ("GREEN", "BLUE");
523 push(@zones, "ORANGE");
527 for my $zone (@zones) {
528 next unless (exists $netsettings{$zone . "_ADDRESS"});
530 my $zone_address = $netsettings{$zone . "_ADDRESS"};
531 push(@addresses, $zone_address);
536 sub get_matching_firewall_address
539 my $use_orange = shift;
541 my ($address, $netmask) = split("/", $addr);
543 my @zones = ("GREEN", "BLUE");
545 push(@zones, "ORANGE");
548 foreach my $zone (@zones) {
549 next unless (exists $netsettings{$zone . "_ADDRESS"});
551 my $zone_subnet = $netsettings{$zone . "_NETADDRESS"};
552 my $zone_mask = $netsettings{$zone . "_NETMASK"};
554 if (&General
::IpInSubnet
($address, $zone_subnet, $zone_mask)) {
555 return $netsettings{$zone . "_ADDRESS"};
561 sub get_internal_firewall_ip_address
564 my $use_orange = shift;
566 my ($net_address, $net_mask) = split("/", $subnet);
567 if ((!$net_mask) || ($net_mask ~~ ["32", "255.255.255.255"])) {
571 # Convert net mask into correct format for &General::IpInSubnet().
572 $net_mask = &General
::iporsubtodec
($net_mask);
574 my @addresses = &get_internal_firewall_ip_addresses
($use_orange);
575 foreach my $zone_address (@addresses) {
576 if (&General
::IpInSubnet
($zone_address, $net_address, $net_mask)) {
577 return $zone_address;
584 sub get_geoip_locations
() {
585 # Path to the directory which contains the binary geoip
587 my $directory="/usr/share/xt_geoip/LE";
589 # Array to store the final country list.
590 my @country_codes = ();
592 # Open location and do a directory listing.
593 opendir(DIR
, "$directory");
594 my @locations = readdir(DIR
);
597 # Loop through the directory listing, and cut of the file extensions.
598 foreach my $location (sort @locations) {
600 next if($location =~ /^\.$/);
601 next if($location =~ /^\.\.$/);
603 # Remove whitespaces.
606 # Cut-off file extension.
607 my ($country_code, $extension) = split(/\./, $location);
609 # Add country code to array.
610 push(@country_codes, $country_code);
613 # Return final array.
614 return @country_codes;