summaryrefslogtreecommitdiff
path: root/scripts/firewall/vyatta-show-firewall-summary.pl
blob: dfcf7769c8f760df8b0ec5581f5f0f655444ee41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env perl
use lib '/opt/vyatta/share/perl5/';
use Vyatta::FirewallOpMode::Summary;

my %description_hash = ( 'name'        => 'IPv4',
                         'ipv6-name'   => 'IPv6',
                         'modify'      => 'IPv4 Modify',
                         'ipv6-modify' => 'IPv6 Modify');

my %gr_desc_hash = ( 'network' => 'Network',
                     'address' => 'Address',
                     'port' => 'Port');

sub print_global_fw_header {
  print "\n" . "-" x 24 . "\n";
  print "Firewall Global Settings\n";
  print "-" x 24 . "\n";
}

sub print_fw_ruleset_header {
  print "\n" . "-" x 24 . "\n";
  print "Firewall Rulesets\n";
  print "-" x 24 . "\n";
}

sub print_fw_group_header {
  print "\n" . "-" x 24 . "\n";
  print "Firewall Groups\n";
  print "-" x 24 . "\n";
}

my $hash = Vyatta::FirewallOpMode::Summary::get_firewall_summary();
print_global_fw_header;
if (scalar(keys(%{$hash->{'global'}})) > 0){
  my $state_format = "  %-15s %-8s %-8s\n";
  print "\nFirewall state-policy for all IPv4 and Ipv6 traffic\n\n";
  printf($state_format, 'state', 'action', 'log');
  printf($state_format, '-----', '------', '---');
  foreach my $state (keys(%{$hash->{'global'}})){
    printf $state_format, $state, 
           $hash->{'global'}->{$state}->{action}, 
           $hash->{'global'}->{$state}->{log};
  }
}
print_fw_ruleset_header;
my $format = "  %-26s%-15s%-s\n";
for my $tree (keys(%{$hash})){
  next if ($tree eq 'global');
  next if (scalar(keys(%{$hash->{$tree}})) == 0);
  print "\n$description_hash{$tree} name:\n\n";
  printf $format, 'Rule-set name', 'Description', 'References';
  printf $format, '-------------', '-----------', '----------';
  for my $chain (keys(%{$hash->{$tree}})){
    my $description = $hash->{$tree}->{$chain}->{description};
    my @intfs = @{$hash->{$tree}->{$chain}->{references}->{interfaces}};
    my @zones = @{$hash->{$tree}->{$chain}->{references}->{zones}};
    my $ci = $hash->{$tree}->{$chain}->{references}->{'content-inspection'};
    if (length($description) > 15){
       printf $format, $chain, $description, '';
       $description = '';
       $chain = ''; 
    }
    if (scalar(@intfs) > 0){
      my $intf_str = '';
      my $numintfs = 0;
      foreach my $intf (@intfs){
        $numintfs++;
        if ((length($intf_str) + length("$intf, ")) > 38) {
          printf $format, $chain, $description, $intf_str;
          ($chain, $description, $intf_str) = ('', '', '');
        }
        if ($numintfs < scalar(@intfs)){
          $intf_str .= "$intf, ";
        } else {
          if (scalar(@zones) > 0){
            $intf_str .= "$intf,";
          } else {
            $intf_str .= "$intf";
          } 
        }
      }
      printf $format, $chain, $description, $intf_str ;
      if (scalar(@zones) > 0){
        my $zone_str = '';
        my $numzones = 0;
        foreach my $zone (@zones){
          $numzones++;
          if ($numzones < scalar(@zones)){
            $zone_str .= "$zone, ";
          } else {
            $zone_str .= "$zone";
          }
        }
        if (length($zone_str) > 38){
          foreach my $zone (@zones){
            printf $format, '', '', $zone;
          }
        } else { 
          printf $format, '', '', $zone_str;
        }
      }
    } elsif (scalar(@zones) > 0){
      my $zone_str = '';
      my $numzones = 0;
      foreach my $zone (@zones){
        $numzones++;
        if ($numzones < scalar(@zones)){
          $zone_str .= "$zone, ";
        } else {
          $zone_str .= "$zone";
        }
      }
      if (length($zone_str) > 38){
        my $fzone = pop @zones;
        printf $format, $chain, $description, "$fzone,";
        my $numzones = 0;
        foreach my $zone (@zones){
          $numzones++;
          if ($numzones < scalar(@zones)){
            printf $format, '', '', "$zone,";
          } else { 
            printf $format, '', '', "$zone";
          }
        }
      } else {
        printf $format, $chain, $description, "$zone_str";
      } 
    }
  }
  print "\n";
}
my $gr_hash = Vyatta::FirewallOpMode::Summary::get_group_summary();
print_fw_group_header;
foreach my $type (keys(%{$gr_hash})){
  print "\n$gr_desc_hash{$type} Groups:\n\n";
  printf $format, 'Group name', 'Description', 'References';
  printf $format, '----------', '-----------', '----------';
  foreach my $group (keys(%{$gr_hash->{$type}})){
    my $description = $gr_hash->{$type}->{$group}->{'description'};
    my @refs = @{$gr_hash->{$type}->{$group}->{'references'}};
    my $numrefs = 0;
    if (scalar(@refs) > 0) {
      my $fref = pop @refs;
      $fref = "$fref," if (scalar(@refs) > 0);
      if (length($description) > 15) {
        printf $format, $group, $description, '';
        printf $format, '', '', $fref;
      } else {
        printf $format, $group, $description, $fref;
      }
      foreach my $ref (@refs){
        $numrefs++;
        if ($numrefs < scalar(@refs)) {
          printf $format, '', '', "$ref, ";
        } else {
          printf $format, '', '', $ref;
        } 
      }
    }
  }
  print "\n";
}