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';
31 my %customservicegrp=();
40 require '/var/ipfire/general-functions.pl';
42 my $confignet = "${General::swroot}/fwhosts/customnetworks";
43 my $confighost = "${General::swroot}/fwhosts/customhosts";
44 my $configgrp = "${General::swroot}/fwhosts/customgroups";
45 my $configsrv = "${General::swroot}/fwhosts/customservices";
46 my $configsrvgrp = "${General::swroot}/fwhosts/customservicegrp";
47 my $configccdnet = "${General::swroot}/ovpn/ccd.conf";
48 my $configccdhost = "${General::swroot}/ovpn/ovpnconfig";
49 my $configipsec = "${General::swroot}/vpn/config";
50 my $configovpn = "${General::swroot}/ovpn/settings";
53 my $netsettings = "${General::swroot}/ethernet/settings";
55 &General
::readhash
("/var/ipfire/ethernet/settings", \
%netsettings);
56 &General
::readhash
("${General::swroot}/ovpn/settings", \
%ovpnsettings);
57 &General
::readhash
("${General::swroot}/vpn/settings", \
%ipsecsettings);
59 &General
::readhasharray
("$confignet", \
%customnetwork);
60 &General
::readhasharray
("$confighost", \
%customhost);
61 &General
::readhasharray
("$configgrp", \
%customgrp);
62 &General
::readhasharray
("$configccdnet", \
%ccdnet);
63 &General
::readhasharray
("$configccdhost", \
%ccdhost);
64 &General
::readhasharray
("$configipsec", \
%ipsecconf);
65 &General
::readhasharray
("$configsrv", \
%customservice);
66 &General
::readhasharray
("$configsrvgrp", \
%customservicegrp);
67 &General
::get_aliases
(\
%aliases);
72 foreach my $key (sort {$a <=> $b} keys %customservice){
73 if($customservice{$key}[0] eq $val){
74 if ($customservice{$key}[0] eq $val){
75 return $customservice{$key}[2];
87 foreach my $key (sort {$a <=> $b} keys %customservicegrp){
88 if($customservicegrp{$key}[0] eq $val){
89 if (&get_srv_prot
($customservicegrp{$key}[2]) eq 'TCP'){
91 }elsif(&get_srv_prot
($customservicegrp{$key}[2]) eq 'UDP'){
93 }elsif(&get_srv_prot
($customservicegrp{$key}[2]) eq 'ICMP'){
96 #Protocols used in servicegroups
97 push (@ips,$customservicegrp{$key}[2]);
101 if ($tcp eq '1'){push (@ips,'TCP');}
102 if ($udp eq '1'){push (@ips,'UDP');}
103 if ($icmp eq '1'){push (@ips,'ICMP');}
104 my $back=join(",",@ips);
113 foreach my $key (sort {$a <=> $b} keys %customservice){
114 if($customservice{$key}[0] eq $val && $customservice{$key}[2] eq $prot){
115 return $customservice{$key}[$field];
126 foreach my $key (sort {$a <=> $b} keys %customservicegrp){
127 if($customservicegrp{$key}[0] eq $val){
128 if ($prot ne 'ICMP'){
129 $value=&get_srv_port
($customservicegrp{$key}[2],1,$prot);
130 }elsif ($prot eq 'ICMP'){
131 $value=&get_srv_port
($customservicegrp{$key}[2],3,$prot);
133 push (@ips,$value) if ($value ne '') ;
137 if ($#ips gt 0){$back="-m multiport --dports ";}else{$back="--dport ";}
138 }elsif ($prot eq 'ICMP'){
139 $back="--icmp-type ";
142 $back.=join(",",@ips);
149 foreach my $key (sort {$a <=> $b} keys %ipsecconf){
150 if($ipsecconf{$key}[1] eq $val){
151 return $ipsecconf{$key}[$field];
155 sub get_ipsec_host_ip
159 foreach my $key (sort {$a <=> $b} keys %ipsecconf){
160 if($ipsecconf{$key}[1] eq $val){
161 return $ipsecconf{$key}[$field];
169 foreach my $key (sort {$a <=> $b} keys %ccdhost){
170 if($ccdhost{$key}[1] eq $val){
171 return $ccdhost{$key}[$field];
179 foreach my $key (sort {$a <=> $b} keys %ccdhost){
180 if($ccdhost{$key}[1] eq $val){
181 return $ccdhost{$key}[$field];
190 foreach my $key (sort {$a <=> $b} keys %ccdnet){
191 if($ccdnet{$key}[0] eq $val){
192 return $ccdnet{$key}[$field];
200 foreach my $key (sort {$a <=> $b} keys %customgrp){
201 if ($customgrp{$key}[0] eq $val){
202 &get_address
($customgrp{$key}[3],$src);
212 return "0.0.0.0/0.0.0.0";
213 }elsif($val eq 'GREEN'){
214 return "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
215 }elsif($val eq 'ORANGE'){
216 return "$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}";
217 }elsif($val eq 'BLUE'){
218 return "$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}";
219 }elsif($val eq 'RED'){
221 }elsif($val =~ /OpenVPN/i){
222 return "$ovpnsettings{'DOVPN_SUBNET'}";
223 }elsif($val =~ /IPsec/i){
224 return "$ipsecsettings{'RW_NET'}";
225 }elsif($val eq 'IPFire'){
232 if($net eq "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}"){
233 return "$netsettings{'GREEN_DEV'}";
235 if($net eq "$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}"){
236 return "$netsettings{'ORANGE_DEV'}";
238 if($net eq "$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}"){
239 return "$netsettings{'BLUE_DEV'}";
241 if($net eq "0.0.0.0/0") {
242 return &get_external_interface
();
249 foreach my $key (sort {$a <=> $b} keys %customnetwork){
250 if($customnetwork{$key}[0] eq $val){
251 return "$customnetwork{$key}[1]/$customnetwork{$key}[2]";
259 foreach my $key (sort {$a <=> $b} keys %customhost){
260 if($customhost{$key}[0] eq $val){
261 if ($customhost{$key}[1] eq 'mac' && $src eq 'src'){
262 return "-m mac --mac-source $customhost{$key}[2]";
263 }elsif($customhost{$key}[1] eq 'ip' && $src eq 'src'){
264 return "$customhost{$key}[2]";
265 }elsif($customhost{$key}[1] eq 'ip' && $src eq 'tgt'){
266 return "$customhost{$key}[2]";
267 }elsif($customhost{$key}[1] eq 'mac' && $src eq 'tgt'){
284 if ($type eq "src") {
285 $addr_type = $$hash{$key}[3];
286 $value = $$hash{$key}[4];
288 } elsif ($type eq "tgt") {
289 $addr_type = $$hash{$key}[5];
290 $value = $$hash{$key}[6];
293 if ($addr_type ~~ ["cust_grp_src", "cust_grp_tgt"]) {
294 foreach my $grp (sort {$a <=> $b} keys %customgrp) {
295 if ($customgrp{$grp}[0] eq $value) {
296 my @address = &get_address
($customgrp{$grp}[3], $customgrp{$grp}[2], $type);
299 push(@addresses, @address);
304 my @address = &get_address
($addr_type, $value, $type);
307 push(@addresses, @address);
321 # If the user manually typed an address, we just check if it is a MAC
322 # address. Otherwise, we assume that it is an IP address.
323 if ($key ~~ ["src_addr", "tgt_addr"]) {
324 if (&General
::validmac
($value)) {
325 push(@ret, ["-m mac --mac-source $value", ""]);
327 push(@ret, [$value, ""]);
330 # If a default network interface (GREEN, BLUE, etc.) is selected, we
331 # try to get the corresponding address of the network.
332 } elsif ($key ~~ ["std_net_src", "std_net_tgt", "Standard Network"]) {
333 my $external_interface = &get_external_interface
();
335 my $network_address = &get_std_net_ip
($value, $external_interface);
337 if ($network_address) {
338 my $interface = &get_interface
($network_address);
339 push(@ret, [$network_address, $interface]);
343 } elsif ($key ~~ ["cust_net_src", "cust_net_tgt", "Custom Network"]) {
344 my $network_address = &get_net_ip
($value);
345 if ($network_address) {
346 push(@ret, [$network_address, ""]);
350 } elsif ($key ~~ ["cust_host_src", "cust_host_tgt", "Custom Host"]) {
351 my $host_address = &get_host_ip
($value, $type);
353 push(@ret, [$host_address, ""]);
357 } elsif ($key ~~ ["ovpn_net_src", "ovpn_net_tgt", "OpenVPN static network"]) {
358 my $network_address = &get_ovpn_net_ip
($value, 1);
359 if ($network_address) {
360 push(@ret, [$network_address, ""]);
364 } elsif ($key ~~ ["ovpn_host_src", "ovpn_host_tgt", "OpenVPN static host"]) {
365 my $host_address = &get_ovpn_host_ip
($value, 33);
367 push(@ret, [$host_address, ""]);
371 } elsif ($key ~~ ["ovpn_n2n_src", "ovpn_n2n_tgt", "OpenVPN N-2-N"]) {
372 my $network_address = &get_ovpn_n2n_ip
($value, 11);
373 if ($network_address) {
374 push(@ret, [$network_address, ""]);
378 } elsif ($key ~~ ["ipsec_net_src", "ipsec_net_tgt", "IpSec Network"]) {
379 my $network_address = &get_ipsec_net_ip
($value, 11);
380 if ($network_address) {
381 push(@ret, [$network_address, ""]);
384 # The firewall's own IP addresses.
385 } elsif ($key ~~ ["ipfire", "ipfire_src"]) {
387 if ($value eq "ALL") {
388 push(@ret, ["0/0", ""]);
391 } elsif ($value eq "GREEN") {
392 push(@ret, [$netsettings{"GREEN_ADDRESS"}, ""]);
395 } elsif ($value eq "BLUE") {
396 push(@ret, [$netsettings{"BLUE_ADDRESS"}, ""]);
399 } elsif ($value eq "ORANGE") {
400 push(@ret, [$netsettings{"ORANGE_ADDRESS"}, ""]);
403 } elsif ($value ~~ ["RED", "RED1"]) {
404 my $address = &get_external_address
();
406 push(@ret, [$address, ""]);
411 my $alias = &get_alias
($value);
413 push(@ret, [$alias, ""]);
417 # If nothing was selected, we assume "any".
419 push(@ret, ["0/0", ""]);
424 sub get_external_interface
()
426 open(IFACE
, "/var/ipfire/red/iface") or return "";
432 sub get_external_address
()
434 open(ADDR
, "/var/ipfire/red/local-ipaddress") or return "";
435 my $address = <ADDR
>;
444 foreach my $alias (sort keys %aliases) {
446 return $aliases{$alias}{"IPT"};
451 sub get_nat_address
{
455 # Any static address of any zone.
456 if ($zone eq "AUTO") {
457 if ($source && ($source !~ m/mac/i )) {
458 my $firewall_ip = &get_internal_firewall_ip_address
($source, 1);
463 $firewall_ip = &get_matching_firewall_address
($source, 1);
469 return &get_external_address
();
471 } elsif ($zone eq "RED" || $zone eq "GREEN" || $zone eq "ORANGE" || $zone eq "BLUE") {
472 return $netsettings{$zone . "_ADDRESS"};
474 } elsif ($zone ~~ ["Default IP", "ALL"]) {
475 return &get_external_address
();
478 my $alias = &get_alias
($zone);
480 $alias = &get_external_address
();
485 print_error
("Could not find NAT address");
488 sub get_internal_firewall_ip_addresses
490 my $use_orange = shift;
492 my @zones = ("GREEN", "BLUE");
494 push(@zones, "ORANGE");
498 for my $zone (@zones) {
499 next unless (exists $netsettings{$zone . "_ADDRESS"});
501 my $zone_address = $netsettings{$zone . "_ADDRESS"};
502 push(@addresses, $zone_address);
507 sub get_matching_firewall_address
510 my $use_orange = shift;
512 my ($address, $netmask) = split("/", $addr);
514 my @zones = ("GREEN", "BLUE");
516 push(@zones, "ORANGE");
519 foreach my $zone (@zones) {
520 next unless (exists $netsettings{$zone . "_ADDRESS"});
522 my $zone_subnet = $netsettings{$zone . "_NETADDRESS"};
523 my $zone_mask = $netsettings{$zone . "_NETMASK"};
525 if (&General
::IpInSubnet
($address, $zone_subnet, $zone_mask)) {
526 return $netsettings{$zone . "_ADDRESS"};
532 sub get_internal_firewall_ip_address
535 my $use_orange = shift;
537 my ($net_address, $net_mask) = split("/", $subnet);
538 if ((!$net_mask) || ($net_mask ~~ ["32", "255.255.255.255"])) {
542 # Convert net mask into correct format for &General::IpInSubnet().
543 $net_mask = &General
::iporsubtodec
($net_mask);
545 my @addresses = &get_internal_firewall_ip_addresses
($use_orange);
546 foreach my $zone_address (@addresses) {
547 if (&General
::IpInSubnet
($zone_address, $net_address, $net_mask)) {
548 return $zone_address;