summaryrefslogtreecommitdiff
path: root/scripts/firewall/vyatta-show-firewall.pl
blob: 77a312b8b595328cea1591306c674da677e8c4a9 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/perl

use lib "/opt/vyatta/share/perl5/";
use Vyatta::Config;
use Vyatta::Interface;
use Vyatta::IpTables::Rule;
use Vyatta::IpTables::AddressFilter;
use Vyatta::Zone;

use warnings;
use strict;

exit 1 if ($#ARGV < 1);
my $tree_chain = $ARGV[0];
my $xsl_file = $ARGV[1];
my $rule_num = $ARGV[2];    # rule number to match (optional)

my $max_rule = 10000;

if (! -e $xsl_file) {
  print "Invalid XSL file \"$xsl_file\"\n";
  exit 1;
}

if (defined($rule_num) && (!($rule_num =~ /^\d+$/) || ($rule_num > $max_rule))) {
  print "Invalid rule number \"$rule_num\"\n";
  exit 1;
}

sub convert_to_easyunits {
  my $size = shift;
  my @args = qw/B K M G T P E Z Y/;

  while (@args && $size > 1000) {
    shift @args;
    $size /= 1000;
  }

  if ($args[0] eq 'B') {
    $size = sprintf("%d",$size);
    return "$size";
  } else {
    $size = sprintf("%.2f",$size);
    return "$size$args[0]";
  }
}

sub numerically { $a <=> $b; }
my $format1  = "%-5s %-8s %-9s %-8s %-40s";
my $format2  = "  %-78s";

sub show_interfaces_zones {
  my ($chain, $tree) = @_;

  my $cfg = new Vyatta::Config;
  my @int_strs = ();
  for (Vyatta::Interface::get_all_cfg_interfaces(1)) {
    my ($iname, $ipath) = ($_->{name}, $_->{path});
    for my $dir ($cfg->listOrigNodes("$ipath firewall")) {
      my $ichain = $cfg->returnOrigValue("$ipath firewall $dir $tree");
      if (defined($ichain) and $ichain eq $chain) {
        $dir =~ y/a-z/A-Z/;
        push @int_strs, "($iname,$dir)";
      }
    }
  }
  if (scalar(@int_strs) > 0) {
    print "\n\n Active on " . (join ' ', @int_strs);
  }
  print "\n";

  # check if chain used to filter traffic between zones
  my $used_in_zonefw=0;
  my @all_zones = Vyatta::Zone::get_all_zones("listOrigNodes");
  foreach my $zone (sort(@all_zones)) {
     my @from_zones = Vyatta::Zone::get_from_zones("listOrigNodes", $zone);
     my @from_zones_using_this_chain=();
     foreach my $from_zone (sort(@from_zones)) {
        my $fw_ruleset=Vyatta::Zone::get_firewall_ruleset("returnOrigValue",
                        $zone, $from_zone, $tree);
        if (defined $fw_ruleset && $fw_ruleset eq $chain) {
          push (@from_zones_using_this_chain, $from_zone);
          if ($used_in_zonefw == 0) {
            print "\n Active on traffic to -\n";
            $used_in_zonefw++;
          }
        }
     }
     if (scalar(@from_zones_using_this_chain) > 0) {
       my $single_or_multiple_zone = 'zone';
       if (scalar(@from_zones_using_this_chain) > 1) {
          $single_or_multiple_zone = 'zones';
       }
       my $string_fromzones=join(', ', sort(@from_zones_using_this_chain));
       print "  zone [$zone] from $single_or_multiple_zone [$string_fromzones]\n";
     }
  }

  # check if content-inspection is using this ruleset
  my $custom_filter = 0;
  my $config = new Vyatta::Config;
  $config->setLevel("content-inspection traffic-filter");
  my $custom_traffic_filter = $config->returnOrigValue('custom');
  if ((defined $custom_traffic_filter) && ($custom_traffic_filter eq $chain)) {
    $custom_filter = 1;
    print "\n Active on all incoming and forwarded traffic for content-inspection\n";
  }
  
  if ((scalar(@int_strs) == 0) && ($used_in_zonefw == 0)
        && ($custom_filter == 0)) {
    print "\n Inactive - Not applied to any interfaces, zones or for content-inspection.\n";
  } 
  print "\n";
}

# mapping from iptables/ip6tables target to config action
my %target_hash = ('RETURN'   => 'accept',
                   'DROP'     => 'drop',
                   'NFQUEUE'  => 'inspect',
                   'REJECT'   => 'reject',
                   'DSCP'     => 'modify',
                   'MARK'     => 'modify');

# mapping from config node to iptables/ip6tables table
my %table_hash = ( 'name'        => 'filter',
                   'ipv6-name'   => 'filter',
                   'modify'      => 'mangle',
                   'ipv6-modify' => 'mangle' );

# mapping from config node to iptables command. 
my %cmd_hash = ( 'name'        => 'iptables',
                 'ipv6-name'   => 'ip6tables',
                 'modify'      => 'iptables',
                 'ipv6-modify' => 'ip6tables');

# mapping from config node to printable string describing it.
my %description_hash = ( 'name'        => 'IPv4',
                         'ipv6-name'   => 'IPv6',
                         'modify'      => 'IPv4 Modify',
                         'ipv6-modify' => 'IPv6 Modify');


# mapping from config node to IP version string.
my %ip_version_hash = ( 'name'        => 'ipv4',
                        'ipv6-name'   => 'ipv6',
                        'modify'      => 'ipv4',
                        'ipv6-modify' => 'ipv6');

sub show_chain($$$) {
  my ($chain, $fh, $tree) = @_;

  my $table = $table_hash{$tree};
  my $iptables_cmd = $cmd_hash{$tree};

  open my $iptables, "-|"
      or exec "sudo", "/sbin/$iptables_cmd", "-t", $table, "-L", $chain, "-vnx"
      or exit 1;
  my @stats = ();
  while (<$iptables>) {
    if (!/^\s*(\d+[KMG]?)\s+(\d+[KMG]?)\s/) {
      next;
    }
    push @stats, ($1, $2);
  }
  close $iptables;

  print $fh "<opcommand name='firewallrules'><format type='row'>\n";
  my $config = new Vyatta::Config;
  $config->setLevel("firewall $tree $chain rule");
  my @rules = sort numerically $config->listOrigNodes();
  foreach (@rules) {
    my $rule = new Vyatta::IpTables::Rule;
    $rule->setupOrig("firewall $tree $chain rule $_");
    $rule->set_ip_version($ip_version_hash{$tree});

    if (defined($rule_num) && $rule_num != $_) {
      next;
    }
    next if $rule->is_disabled();

    # just take the stats from the 1st iptables rule and remove unneeded stats
    # (if this rule corresponds to multiple iptables rules). note that
    # depending on how our rule is translated into multiple iptables rules,
    # this may actually need to be the sum of all corresponding iptables stats
    # instead of just taking the first pair. for eg: when protocol is tcp_udp
    # we aggregate the counters for both tcp and udp rules below
    my $pkts = shift @stats;
    my $bytes = shift @stats;
    if (defined $rule->{_protocol} && $rule->{_protocol} eq 'tcp_udp') {
      my @tmp_stats = @stats;
      if (defined $rule->{_log} && $rule->{_log} eq 'enable') {      
        # shift stats for 2 iptables rule
        shift @tmp_stats; shift @tmp_stats; shift @tmp_stats; shift @tmp_stats;
      } elsif (defined $rule->{_recent_time} || defined $rule->{_recent_cnt}) {
        # shift stats for 1 rule
        shift @tmp_stats; shift @tmp_stats;
      } 
      $pkts += shift @tmp_stats;
      $bytes += shift @tmp_stats;
    }
    my $ipt_rules = $rule->get_num_ipt_rules();
    splice(@stats, 0, (($ipt_rules - 1) * 2));
    $pkts = convert_to_easyunits($pkts);
    $bytes = convert_to_easyunits($bytes);
    print $fh "  <row>\n";
    print $fh "    <rule_number>$_</rule_number>\n";
    print $fh "    <pkts>$pkts</pkts>\n";
    print $fh "    <bytes>$bytes</bytes>\n";
    $rule->outputXml($fh);
    print $fh "  </row>\n";
  }

  if (!defined($rule_num) || ($rule_num == $max_rule)) {
    # dummy rule
    print $fh "  <row>\n";
    print $fh "    <rule_number>$max_rule</rule_number>\n";
    my $pkts = convert_to_easyunits(shift @stats);
    my $bytes = convert_to_easyunits(shift @stats);
    print $fh "    <pkts>$pkts</pkts>\n";
    print $fh "    <bytes>$bytes</bytes>\n";
    my $rule = new Vyatta::IpTables::Rule;
    $rule->setupDummy("firewall $tree $chain");
    $rule->set_ip_version($ip_version_hash{$tree});
    $rule->outputXml($fh);
    print $fh "  </row>\n";
  }
  print $fh "</format></opcommand>\n";
}

sub show_chain_detail {

 my ($chain, $tree) = @_;
 my $table = $table_hash{$tree};
 my $iptables_cmd = $cmd_hash{$tree};

 my $config = new Vyatta::Config;
 $config->setLevel("firewall $tree $chain rule");
 my @rules = sort numerically $config->listOrigNodes();
 print "\n";
 printf($format1, 'rule', 'action', 'proto', 'packets', 'bytes');
 print "\n";
 printf($format1, '----', '------', '-----', '-------', '-----');
 foreach (@rules) {
  my $rule = new Vyatta::IpTables::Rule;
  $rule->setupOrig("firewall $tree $chain rule $_");
  if (defined($rule_num) && $rule_num != $_) {
      next;
  }
  next if $rule->is_disabled();
  print_detail_rule ($iptables_cmd, $table, $chain, $_, $tree);
 }
 if (!defined($rule_num) || ($rule_num == $max_rule)) {
  # dummy rule
  print_detail_rule ($iptables_cmd, $table, $chain, $max_rule, $tree);
 }
 print "\n";
}

sub print_detail_rule {
 my ($iptables_cmd, $table, $chain, $rule, $tree) = @_;
 my $string="";
 my $mul_lines="";
 my $udp_string = undef;
 
 # check from CLI if we have a condition set that creates more than 1 iptable rule
 # currenly LOG, RECENT, protocol tcp_udp in a CLI rule result in more than 1 iptable rule
 my $cli_rule = new Vyatta::IpTables::Rule;
 if ($rule eq $max_rule) {
   $cli_rule->setupDummy("firewall $tree $chain");
   $cli_rule->set_ip_version($ip_version_hash{$tree});
 } else {
   $cli_rule->setupOrig("firewall $tree $chain rule $rule");
 }
 if (defined $cli_rule->{_log} && "$cli_rule->{_log}" eq "enable") {
 
  # log enabled in rule so actual rule in iptables is second rule
  # now get line-num for 1st rule and use line-num+1 to list actual rule
   $mul_lines=`sudo /sbin/$iptables_cmd -t $table -L $chain -xv --line-num |
              awk '/$chain-$rule / {print \$0}'`;
   my @lines = split(/\s+/, $mul_lines, 2);
   my $line_num = $lines[0] + 1;
   $string=`sudo /sbin/$iptables_cmd -t $table -L $chain $line_num -xv |
              awk '/$chain-$rule / {print \$0}'`;

   if (defined $cli_rule->{_protocol} && $cli_rule->{_protocol} eq 'tcp_udp') {
     # we need the udp rule as well
     if (defined($cli_rule->{_recent_time}) || defined($cli_rule->{_recent_cnt})) { 
       $line_num = $line_num + 3;
     } else {
       $line_num = $line_num + 2;
     }
     $udp_string = `sudo /sbin/$iptables_cmd -t $table -L $chain $line_num -xv |
                    awk '/$chain-$rule / {print \$0}'`;
   }
 } elsif ( (defined($cli_rule->{_recent_time}) || defined($cli_rule->{_recent_cnt})) ||
           (defined $cli_rule->{_protocol} && $cli_rule->{_protocol} eq 'tcp_udp') ) {
 
  # recent enabled but not log so actual rule in iptables is first rule
  # now get line-num for 1st rule and use that to list actual rule
   $mul_lines=`sudo /sbin/$iptables_cmd -t $table -L $chain -xv --line-num |
              awk '/$chain-$rule / {print \$0}'`;
   my @lines = split(/\s+/, $mul_lines, 2);
   my $line_num = $lines[0];
   $string=`sudo /sbin/$iptables_cmd -t $table -L $chain $line_num -xv |
              awk '/$chain-$rule / {print \$0}'`;
     
   # we need the udp rule as well         
   if (defined($cli_rule->{_recent_time}) || defined($cli_rule->{_recent_cnt})) {
     $line_num = $line_num + 2;
     $udp_string=`sudo /sbin/$iptables_cmd -t $table -L $chain $line_num -xv |
              awk '/$chain-$rule / {print \$0}'`;
   } else {
     $line_num = $line_num + 1;
     $udp_string=`sudo /sbin/$iptables_cmd -t $table -L $chain $line_num -xv |
              awk '/$chain-$rule / {print \$0}'`;
   }
 } else {
 
   # there's a one-to-one relation between our CLI rule and iptable rule
   $string=`sudo /sbin/$iptables_cmd -t $table -L $chain -xv |
              awk '/$chain-$rule / {print \$0}'`;
 }
 
 my @string_words=();
 my @string_words_part1=();
 my @string_words_part2=();
 my @string_words_part3 = ();
 @string_words = split (/\s+/, $string, 14);
 @string_words=splice(@string_words, 1, 13);
 @string_words_part1=splice(@string_words, 0, 4); # packets, bytes, target, proto
 
 if (defined $cli_rule->{_protocol} && $cli_rule->{_protocol} eq 'tcp_udp') {
   $string_words_part1[3] = 'tcp_udp';
   
   # get udp rule packets, bytes
   my @udp_string_words=split(/\s+/, $udp_string, 14);
   @udp_string_words=splice(@udp_string_words, 1, 13);
   @udp_string_words=splice(@udp_string_words, 0, 4); # packets, bytes, target, proto
   $string_words_part1[0] += $udp_string_words[0];
   $string_words_part1[1] += $udp_string_words[1];
 }
 
 $string_words_part1[2]=$cli_rule->{_action} if defined $cli_rule->{_action};
 
 if ($iptables_cmd =~ /6/) {
  @string_words_part2=splice(@string_words, 2, 2);# source, destination
 } else {
  @string_words_part2=splice(@string_words, 3, 2);# source, destination
 }
 if ($rule != $max_rule) {
  if ($iptables_cmd =~ /6/) {
   @string_words_part3=splice(@string_words, 5);# all other matches after comment
  } else {
   @string_words_part3=splice(@string_words, 6);# all other matches after comment
  }
 }
 my $condition='condition - ';
 my $string_for_part3 = join (" ", @string_words_part3);
 chomp $string_for_part3;
 if (!($string_words_part2[1] eq "anywhere")) {
  $string_for_part3 = "daddr " . $string_words_part2[1] . " " .$string_for_part3;
 }
 if (!($string_words_part2[0] eq "anywhere")) {
  $string_for_part3 = "saddr " . $string_words_part2[0] . " " . $string_for_part3;
 }

 # make output pretty, replace iptables specific information with CLI related text
 $string_for_part3 =~ s/ipp2p\s\S+\s/P2P /g;
 $string_for_part3 =~ s/multiport//g;
 $string_for_part3 =~ s/recent: UPDATE\s(.+)\sname: DEFAULT side: source /RECENT $1 /g;
 $string_for_part3 =~ s/limit: /LIMIT /g;
 while ($string_for_part3 =~ m/set\s(\S+)\ssrc\s/) {
  my $group_type=get_group_type("$1");
  $string_for_part3 =~ s/set\s(\S+)\ssrc\s/SRC-$group_type-GROUP $1 /;
 }
 while ($string_for_part3 =~ m/set\s(\S+)\sdst\s/) {
  my $group_type=get_group_type("$1");
  $string_for_part3 =~ s/set\s(\S+)\sdst\s/DST-$group_type-GROUP $1 /;
 }
 $string_for_part3 =~ s/policy match dir in pol\s(\S+)\s/IPSEC-MATCH $1 /g;
 if (defined $cli_rule->{_tcp_flags}) {
  $string_for_part3 =~ s/flags:(\S+)\s/tcp-flags $cli_rule->{_tcp_flags} /g;
 }
 
 # add information not displayed when listing the underlying iptable rule
 if (defined($cli_rule->{_frag})) {
   $string_for_part3 .= "FRAGMENT match-frag ";
 } elsif (defined($cli_rule->{_non_frag})) {
   $string_for_part3 .= "FRAGMENT match-non-frag ";
 }
 if (defined $cli_rule->{_log} && "$cli_rule->{_log}" eq "enable") {
  $string_for_part3 .= "LOG enabled";
 }

 print "\n";
 printf($format1, "$rule", "$string_words_part1[2]", "$string_words_part1[3]",
        "$string_words_part1[0]", "$string_words_part1[1]");
 print "\n";
 # print condition
 if ($string_for_part3 =~ /\w/) {
    while (length($string_for_part3) > 66) {
     my $condition_str = substr $string_for_part3, 0 , 66;
     $condition .= $condition_str;
     printf($format2, $condition);
     $condition = '            ';
     $string_for_part3 = substr $string_for_part3, 66;
     print "\n";
    }
    # print last line which has less than 66 chars
    $condition .= $string_for_part3;
    printf($format2, $condition);
 }
 print "\n";
}

sub get_group_type {
  my $group=shift;
  my $config = new Vyatta::Config;
  $config->setLevel("firewall group");
  my @addr_groups = $config->listOrigNodes("address-group");
  my @ntwrk_groups = $config->listOrigNodes("network-group");
  my @port_groups = $config->listOrigNodes("port-group");
  if (scalar(grep(/^$group$/, @addr_groups)) > 0) {
   return ("ADDR");
  } elsif (scalar(grep(/^$group$/, @ntwrk_groups)) > 0) {
    return ("NTWRK");
  } elsif (scalar(grep(/^$group$/, @port_groups)) > 0) {
    return ("PORT");
  }
}

sub show_tree {
  my ($tree, $config, ) = @_;

  my $description = $description_hash{$tree};
  $config->setLevel("firewall $tree");
  my @chains = $config->listOrigNodes();
  my $chain_cnt=0;
  print "-" x 80 . "\n" if (scalar(@chains) > 0);
  foreach (sort @chains) {
    $chain_cnt++;
    print "$description Firewall \"$_\":";
    show_interfaces_zones($_, $tree);
    if (!($xsl_file =~ /detail/)) {
      open(RENDER, "| /opt/vyatta/sbin/render_xml $xsl_file") or exit 1;
      show_chain($_, *RENDER{IO}, $tree);
      close RENDER;
    } else {
      show_chain_detail($_, $tree);
    }
    print "-" x 80 . "\n" if ($chain_cnt < scalar(@chains));
  }
}


#
# main
#

my $tree;
my $config = new Vyatta::Config;
my @chains;
my @tree_chain_name = split('_', $tree_chain, 2);
my $tree_name = $tree_chain_name[0];
my $chain_name = $tree_chain_name[1];

# check if tree name is either 'all' or one of four keys in %table_hash
if (!($tree_name eq "all" || (scalar(grep(/^$tree_name$/, (keys %table_hash))) > 0))) {
 print "Invalid firewall type name [$tree_name]\n";
 exit 1;
}

if ($tree_name eq "all") {
  # Print all rule sets in all four trees
  foreach $tree (reverse(sort(keys %table_hash))) {
    show_tree($tree, $config);
  }
} elsif ($chain_name eq "all") {
    # Print all rule sets in specified tree
    $tree = $tree_name;
    show_tree($tree, $config);
} else {
  # Print given rule set in specified tree
    $tree = $tree_name;
    $config->setLevel("firewall $tree");
    @chains = $config->listOrigNodes();
    # validate chain-name
    if (!(scalar(grep(/^$chain_name$/, @chains)) > 0)) {
     print "Invalid firewall instance [$chain_name] \n";
     exit 1;
    }
    if (defined $rule_num) {
    #validate rule-num for given chain
     $config->setLevel("firewall $tree $chain_name rule");
     my @rules = $config->listOrigNodes();
     if (!((scalar(grep(/^$rule_num$/, @rules)) > 0) || 
           ($rule_num == $max_rule))) {
      print "Invalid rule $rule_num under firewall instance [$chain_name] \n";
      exit 1;
     }
    }
    my $description = $description_hash{$tree};
    print "\n$description Firewall \"$chain_name\":";
    show_interfaces_zones($chain_name, $tree);
    if (!($xsl_file =~ /detail/)) {
     open(RENDER, "| /opt/vyatta/sbin/render_xml $xsl_file") or exit 1;
     show_chain($chain_name, *RENDER{IO}, $tree);
     close RENDER;
    } else {
      show_chain_detail($chain_name, $tree);
    }
}

exit 0;

# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# perl-indent-level: 2
# End: