]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - html/cgi-bin/connections.cgi
mympd: remove create config start
[ipfire-2.x.git] / html / cgi-bin / connections.cgi
index f1ed2125a2cb9e253ca332f7de01c1b09985e601..24702980c8f4eebe04a5921bd5f3677299cd69b9 100644 (file)
@@ -2,7 +2,7 @@
 ###############################################################################
 #                                                                             #
 # IPFire.org - A linux based firewall                                         #
-# Copyright (C) 2007-2012  IPFire Team  <info@ipfire.org>                     #
+# Copyright (C) 2007-2023  IPFire Team  <info@ipfire.org>                     #
 #                                                                             #
 # This program is free software: you can redistribute it and/or modify        #
 # it under the terms of the GNU General Public License as published by        #
@@ -20,6 +20,7 @@
 ###############################################################################
 
 use strict;
+use experimental 'smartmatch';
 
 use Net::IPv4Addr qw( :all );
 use Switch;
@@ -31,6 +32,7 @@ use Switch;
 require '/var/ipfire/general-functions.pl';
 require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
+require "${General::swroot}/location-functions.pl";
 
 my $colour_multicast = "#A0A0A0";
 
@@ -55,7 +57,7 @@ if (length ($ENV{'QUERY_STRING'}) > 0){
                ($name, $value) = split(/=/, $pair);
                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # e.g. "%20" => " "
                $value =~ s/[^a-zA-Z0-9]*//g; # a-Z 0-9 will pass
-               $cgiin{$name} = $value; 
+               $cgiin{$name} = $value;
        }
 }
 
@@ -94,8 +96,8 @@ if ( $cgiin{'sort_field'} ~~ [ '1','2','3','4','5','6','7','8','9' ] ) {
 }
 
 # Read and sort the connection tracking table
-# do sorting 
-if ($SORT_FIELD and $SORT_ORDER) { 
+# do sorting
+if ($SORT_FIELD and $SORT_ORDER) {
        # field sorting when sorting arguments are sane
        open(CONNTRACK, "/usr/local/bin/getconntracktable | /usr/local/bin/consort.sh $SORT_FIELD $SORT_ORDER |") or die "Unable to read conntrack table";
 } else {
@@ -144,24 +146,29 @@ if ($netsettings{'RED_DEV'}) {
        }
 }
 
+# Call safe system_output function to get all available routes.
+my @all_routes = &General::system_output("/sbin/route", "-n");
+
 # Add Green Firewall Interface
 push(@network, $netsettings{'GREEN_ADDRESS'});
 push(@masklen, "255.255.255.255" );
 push(@colour, ${Header::colourfw} );
 
-# Add Green Network to Array
-push(@network, $netsettings{'GREEN_NETADDRESS'});
-push(@masklen, $netsettings{'GREEN_NETMASK'} );
-push(@colour, ${Header::colourgreen} );
-
-# Add Green Routes to Array
-my @routes = `/sbin/route -n | /bin/grep $netsettings{'GREEN_DEV'}`;
-foreach my $route (@routes) {
-       chomp($route);
-       my @temp = split(/[\t ]+/, $route);
-       push(@network, $temp[0]);
-       push(@masklen, $temp[2]);
+if ($netsettings{'GREEN_DEV'}) {
+       # Add Green Network to Array
+       push(@network, $netsettings{'GREEN_NETADDRESS'});
+       push(@masklen, $netsettings{'GREEN_NETMASK'} );
        push(@colour, ${Header::colourgreen} );
+
+       # Add Green Routes to Array
+       my @routes = grep (/$netsettings{'GREEN_DEV'}/, @all_routes);
+       foreach my $route (@routes) {
+               chomp($route);
+               my @temp = split(/[\t ]+/, $route);
+               push(@network, $temp[0]);
+               push(@masklen, $temp[2]);
+               push(@colour, ${Header::colourgreen} );
+       }
 }
 
 # Add Blue Firewall Interface
@@ -176,7 +183,7 @@ if ($netsettings{'BLUE_DEV'}) {
        push(@colour, ${Header::colourblue} );
 
        # Add Blue Routes to Array
-       @routes = `/sbin/route -n | /bin/grep $netsettings{'BLUE_DEV'}`;
+       my @routes = grep(/$netsettings{'BLUE_DEV'}/, @all_routes);
        foreach my $route (@routes) {
                chomp($route);
                my @temp = split(/[\t ]+/, $route);
@@ -197,7 +204,7 @@ if ($netsettings{'ORANGE_DEV'}) {
        push(@masklen, $netsettings{'ORANGE_NETMASK'} );
        push(@colour, ${Header::colourorange} );
        # Add Orange Routes to Array
-       @routes = `/sbin/route -n | /bin/grep $netsettings{'ORANGE_DEV'}`;
+       my @routes = grep(/$netsettings{'ORANGE_DEV'}/, @all_routes);
        foreach my $route (@routes) {
                chomp($route);
                my @temp = split(/[\t ]+/, $route);
@@ -240,13 +247,13 @@ if (-e "${General::swroot}/ovpn/settings") {
 
 # Add OpenVPN net for custom OVPNs
 if (-e "${General::swroot}/ovpn/ccd.conf") {
-       open(OVPNSUB, "${General::swroot}/ovpn/ccd.conf");      
+       open(OVPNSUB, "${General::swroot}/ovpn/ccd.conf");
        my @ovpnsub = <OVPNSUB>;
        close(OVPNSUB);
 
        foreach (@ovpnsub) {
                my ($network, $mask) = split '/', (split ',', $_)[2];
-               
+
                $mask = ipv4_cidr2msk($mask) unless &General::validip($mask);
 
                push(@network, $network);
@@ -261,15 +268,19 @@ close(IPSEC);
 
 foreach my $line (@ipsec) {
        my @vpn = split(',', $line);
-       my ($network, $mask) = split("/", $vpn[12]);
 
-       if (!&General::validip($mask)) {
-               $mask = ipv4_cidr2msk($mask);
-       }
+       my @subnets = split(/\|/, $vpn[12]);
+       for my $subnet (@subnets) {
+               my ($network, $mask) = split("/", $subnet);
+
+               if (!&General::validip($mask)) {
+                       $mask = ipv4_cidr2msk($mask);
+               }
 
-       push(@network, $network);
-       push(@masklen, $mask);
-       push(@colour, ${Header::colourvpn});
+               push(@network, $network);
+               push(@masklen, $mask);
+               push(@colour, ${Header::colourvpn});
+       }
 }
 
 if (-e "${General::swroot}/ovpn/n2nconf") {
@@ -341,7 +352,7 @@ if ($SORT_FIELD and $SORT_ORDER) {
                $Lang::tr{'destination port'},
                $Lang::tr{'protocol'},
                $Lang::tr{'connection'}.' '.$Lang::tr{'status'},
-               $Lang::tr{'expires'}.' ('.$Lang::tr{'seconds'}.')',
+               $Lang::tr{'expires'}.' ('.$Lang::tr{'hours:minutes:seconds'}.')',
                $Lang::tr{'download'},
                $Lang::tr{'upload'}
        );
@@ -375,6 +386,7 @@ print <<END;
                                <a href="?sort_field=3&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
                                <a href="?sort_field=3&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
                        </th>
+                       <th>&nbsp;</th>
                        <th style='text-align:center' colspan='2'>
                                <a href="?sort_field=2&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
                                <a href="?sort_field=2&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
@@ -382,6 +394,7 @@ print <<END;
                                <a href="?sort_field=4&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
                                <a href="?sort_field=4&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
                        </th>
+                       <th>&nbsp;</th>
                        <th style='text-align:center'>
                                <a href="?sort_field=8&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
                                <a href="?sort_field=8&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
@@ -405,9 +418,15 @@ print <<END;
                        <th style='text-align:center' colspan='2'>
                                $Lang::tr{'source ip and port'}
                        </th>
+                       <th style='text-align:center'>
+                               $Lang::tr{'country'}
+                       </th>
                        <th style='text-align:center' colspan='2'>
                                $Lang::tr{'dest ip and port'}
                        </th>
+                       <th style='text-align:center'>
+                               $Lang::tr{'country'}
+                       </th>
                        <th style='text-align:center'>
                                $Lang::tr{'download'} /
                                <br>$Lang::tr{'upload'}
@@ -416,7 +435,7 @@ print <<END;
                                $Lang::tr{'connection'}<br>$Lang::tr{'status'}
                        </th>
                        <th style='text-align:center'>
-                               $Lang::tr{'expires'}<br>($Lang::tr{'seconds'})
+                               $Lang::tr{'expires'}<br>($Lang::tr{'hours:minutes:seconds'})
                        </th>
                </tr>
 END
@@ -520,7 +539,8 @@ foreach my $line (@conntrack) {
        }
 
        my $sip_colour = ipcolour($sip);
-       my $dip_colour = ipcolour($dip);
+       # use colour of destination network for DNAT
+       my $dip_colour = $dip ne $dip_ret ? ipcolour($dip_ret) : ipcolour($dip);
 
        my $sserv = '';
        if ($sport < 1024) {
@@ -535,11 +555,17 @@ foreach my $line (@conntrack) {
        my $bytes_in = format_bytes($bytes[0]);
        my $bytes_out = format_bytes($bytes[1]);
 
+       # enumerate location information
+       my $srcccode = &Location::Functions::lookup_country_code($sip_ret);
+       my $src_flag_icon = &Location::Functions::get_flag_icon($srcccode);
+       my $dstccode = &Location::Functions::lookup_country_code($dip_ret);
+       my $dst_flag_icon = &Location::Functions::get_flag_icon($dstccode);
+
        # Format TTL
        $ttl = format_time($ttl);
 
        my $sip_extra;
-       if ($sip ne $sip_ret) {
+       if ($sip_ret && $sip ne $sip_ret) {
                $sip_extra = "<span style='color:#FFFFFF;'>&gt;</span> ";
                $sip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$sip_ret'>";
                $sip_extra .= " <span style='color:#FFFFFF;'>$sip_ret</span>";
@@ -547,7 +573,7 @@ foreach my $line (@conntrack) {
        }
 
        my $dip_extra;
-       if ($dip ne $dip_ret) {
+       if ($dip_ret && $dip ne $dip_ret) {
                $dip_extra = "<span style='color:#FFFFFF;'>&gt;</span> ";
                $dip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$dip_ret'>";
                $dip_extra .= " <span style='color:#FFFFFF;'>$dip_ret</span>";
@@ -563,7 +589,7 @@ foreach my $line (@conntrack) {
                }
 
                $sport_extra = "<span style='color:#FFFFFF;'>&gt;</span> ";
-               $sport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$sport_ret' target='top' title='$sserv_ret'>";
+               $sport_extra .= "<a href='https://isc.sans.edu/port.html?port=$sport_ret' target='top' title='$sserv_ret'>";
                $sport_extra .= " <span style='color:#FFFFFF;'>$sport_ret</span>";
                $sport_extra .= "</a>";
        }
@@ -576,7 +602,7 @@ foreach my $line (@conntrack) {
                }
 
                $dport_extra = "<span style='color:#FFFFFF;'>&gt;</span> ";
-               $dport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$dport_ret' target='top' title='$dserv_ret'>";
+               $dport_extra .= "<a href='https://isc.sans.edu/port.html?port=$dport_ret' target='top' title='$dserv_ret'>";
                $dport_extra .= " <span style='color:#FFFFFF;'>$dport_ret</span>";
                $dport_extra .= "</a>";
        }
@@ -591,11 +617,14 @@ foreach my $line (@conntrack) {
                        $sip_extra
                </td>
                <td style='text-align:center; background-color:$sip_colour;'>
-                       <a href='http://isc.sans.org/port_details.php?port=$sport' target='top' title='$sserv'>
+                       <a href='https://isc.sans.edu/port.html?port=$sport' target='top' title='$sserv'>
                                <span style='color:#FFFFFF;'>$sport</span>
                        </a>
                        $sport_extra
                </td>
+               <td style='text-align:center; background-color:$sip_colour;'>
+                       <a href='country.cgi#$srcccode'><img src='$src_flag_icon' border='0' align='absmiddle' alt='$srcccode' title='$srcccode' /></a>
+               </td>
                <td style='text-align:center; background-color:$dip_colour;'>
                        <a href='/cgi-bin/ipinfo.cgi?ip=$dip'>
                                <span style='color:#FFFFFF;'>$dip</span>
@@ -603,11 +632,14 @@ foreach my $line (@conntrack) {
                        $dip_extra
                </td>
                <td style='text-align:center; background-color:$dip_colour;'>
-                       <a href='http://isc.sans.org/port_details.php?port=$dport' target='top' title='$dserv'>
+                       <a href='https://isc.sans.edu/port.html?port=$dport' target='top' title='$dserv'>
                                <span style='color:#FFFFFF;'>$dport</span>
                        </a>
                        $dport_extra
                </td>
+               <td style='text-align:center; background-color:$sip_colour;'>
+                       <a href='country.cgi#$dstccode'><img src='$dst_flag_icon' border='0' align='absmiddle' alt='$dstccode' title='$dstccode' /></a>
+               </td>
                <td style='text-align:center'>
                        $bytes_in / $bytes_out
                </td>
@@ -660,15 +692,17 @@ sub ipcolour($) {
        my ($ip) = $_[0];
        my $found = 0;
 
-       foreach my $line (@network) {
-               if ($network[$id] eq '') {
-                       $id++;
-               } else {
-                       if (!$found && ipv4_in_network($network[$id], $masklen[$id], $ip) ) {
-                               $found = 1;
-                               $colour = $colour[$id];
+       if ($ip) {
+               foreach my $line (@network) {
+                       if ($network[$id] eq '') {
+                               $id++;
+                       } else {
+                               if (!$found && ipv4_in_network($network[$id], $masklen[$id], $ip) ) {
+                                       $found = 1;
+                                       $colour = $colour[$id];
+                               }
+                               $id++;
                        }
-                       $id++;
                }
        }