-
Notifications
You must be signed in to change notification settings - Fork 10
/
print-group
executable file
·320 lines (260 loc) · 8.94 KB
/
print-group
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
#!/usr/bin/perl
=head1 NAME
print-group - Show elements of a netspoc group definition
=head1 SYNOPSIS
print-group [options] FILE|DIR ['group:name,...']
=head1 DESCRIPTION
This program prints the elements of one or more netspoc group
definitions. By default it reads a group definition from command line
and shows a line with IP/prefixlen and name for each element separated
by tab character. Group is a simple group, some automatic group, or a
union or intersection or complement of simpler groups.
With option B<-f>, group definitions are read from a file with a
single group definition per line. In the output, a comment line is
prepended to elements of each group. The format of this comment line
is a B<#> character followed by the corresponding input line.
=head1 OPTIONS
=over 4
=item B<-nat> name
Uses network:name as reference when resolving IP address in a NAT environment.
=item B<-unused>
Show only elements not used in any rules.
=item B<-name>
Show only name of elements.
=item B<-ip>
Show only IP address of elements.
=item B<-owner>
Show owner of elements.
=item B<-f> FILE
Read group definitions from FILE, one definition per line.
If B<-> is specified, read from standard input.
=item B<-quiet>
Don't print progress messages.
=item B<-help>
Prints a brief help message and exits.
=item B<-man>
Prints the manual page and exits.
=back
=head1 COPYRIGHT AND DISCLAIMER
(c) 2017 by Heinz Knutzen <heinz.knutzen@googlemail.com>
This program uses module Netspoc.pm,
a Network Security Policy Compiler,
http://hknutzen.github.com/Netspoc
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
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
=cut
use strict;
use warnings;
use open qw(:std :utf8);
use Encode qw(decode_utf8);
use Netspoc::Compiler::GetArgs qw(read_config combine_config);
use Netspoc::Compiler::File qw(*current_file *input);
use Netspoc::Compiler::Common;
use Netspoc::Compiler::Pass1;
use Getopt::Long;
use Pod::Usage;
# VERSION: inserted by DZP::OurPkgVersion
# Take higher bits from network NAT, lower bits from original IP.
# This works with and without NAT.
sub nat {
my ($ip, $network) = @_;
return($network->{ip} | $ip & ~ $network->{mask});
}
# Print IP address of $obj in context of $no_nat_set.
sub print_address {
my ($obj, $no_nat_set) = @_;
my $type = ref $obj;
if ($type eq 'Network') {
$obj = get_nat_network($obj, $no_nat_set);
my $ip = $obj->{ip};
if ($ip eq 'unnumbered') {
return $ip;
}
elsif ($obj->{hidden}) {
return 'hidden';
}
else {
my $prefix = mask2prefix($obj->{mask});
return print_ip($ip) . ($prefix == ($config->{ipv6} == 1? 128 : 32)
? '' : "/$prefix");
}
}
elsif ($type eq 'Host' or $type eq 'Interface') {
my $network = get_nat_network($obj->{network}, $no_nat_set);
if ($network->{dynamic}) {
my $nat_tag = $network->{nat_tag};
if ($obj->{nat} and (my $ip = $obj->{nat}->{$nat_tag})) {
# Single static NAT IP for this object.
return print_ip($ip);
}
elsif ($network->{hidden}) {
return 'hidden';
}
else {
# Dynamic NAT, take whole network.
my $ip = $network->{ip};
my $prefix = mask2prefix($network->{mask});
return print_ip($ip) . ($prefix ==
($config->{ipv6} == 1? 128 : 32)
? '' : "/$prefix");
}
}
elsif (my $range = $obj->{range}) {
return
join('-', map { print_ip(nat($_, $network)) } @$range);
}
elsif ($obj->{ip} =~ /unnumbered|short|bridged/) {
return $obj->{ip};
}
elsif ($obj->{ip} eq 'negotiated') {
# Take whole network.
my $ip = $network->{ip};
my $prefix = mask2prefix($network->{mask});
return print_ip($ip) . ($prefix == ($config->{ipv6} == 1? 128 : 32)
? '' : "/$prefix");
}
else {
return print_ip(nat($obj->{ip}, $network));
}
}
elsif ($type eq 'Autointerface') {
return 'unknown';
}
}
####################################################################
# Argument processing
####################################################################
my ($nat_net, $show_unused, $show_ip, $show_name, $show_owner, $groups_file,
$quiet, $help, $ipv6, $man);
GetOptions ( 'nat=s' => \$nat_net,
'unused' => \$show_unused,
'ip!' => \$show_ip,
'name!' => \$show_name,
'owner!' => \$show_owner,
'f=s' => \$groups_file,
'quiet!' => \$quiet,
'help|?' => \$help,
'ipv6' => \$ipv6,
man => \$man,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
$show_ip or $show_name or $show_ip = $show_name = 1;
my $in_path = shift @ARGV or pod2usage(2);
my @groups;
if ($groups_file) {
## no critic ProhibitTwoArgOpen
open(my $fh, $groups_file) or die "Can't open $groups_file: $!\n";
@groups = <$fh>;
close $fh;
}
else {
my $definition = shift @ARGV or pod2usage(2);
$definition = decode_utf8($definition, 1);
push @groups, $definition;
}
@ARGV and pod2usage(2);
####################################################################
# Main program
####################################################################
my $file_config = &read_config($in_path);
$config = combine_config($file_config, { verbose => !$quiet,
ipv6 => $ipv6? 1 : 0 });
init_global_vars();
# Parse group definition(s).
for (my $i = 0; $i < @groups; $i++) {
my $definition = $groups[$i];
chomp $definition;
my $line = $i + 1;
$input = "$definition;";
$current_file =
$groups_file ? "line $line of file $groups_file" : 'group definition';
my $parsed = read_union(';');
skip_space_and_comment();
pos $input == length $input or die "Invalid group specification\n";
$groups[$i] = [$definition, $parsed];
}
# Read and process Netspoc configuration file or directory.
&read_file_or_dir($in_path);
&link_topology();
&mark_disabled();
&set_zone();
&setpath();
&distribute_nat_info();
find_subnets_in_zone();
&abort_on_error();
# Find network for resolving NAT addresses.
my $no_nat_set;
if($nat_net) {
$nat_net =~ s/^network://;
my $net = $networks{$nat_net} or
die "Unknown network:$nat_net of option '-nat'\n";
$no_nat_set = $net->{nat_domain}->{no_nat_set};
}
# Prepare finding unused objects by marking used objects.
my %used;
if ($show_unused) {
order_protocols();
normalize_services();
abort_on_error();
for my $action (qw(permit deny)) {
my $rules = $service_rules{$action} or next;
for my $rule (@$rules) {
for my $what (qw(src dst)) {
my $group = $rule->{$what};
for my $obj (@$group) {
$used{$obj} = 1;
if (is_host($obj) or is_interface($obj)) {
$used{$obj->{network}} = 1;
}
}
}
}
}
}
if ($show_owner) {
propagate_owners();
}
# Expand group definition(s).
for my $aref (@groups) {
my ($definition, $parsed) = @$aref;
my $elements = expand_group($parsed, 'print-group');
if ($show_unused) {
$elements = [ grep { not $used{$_} } @$elements ];
}
# Collect IP address, name, owner.
my @info = map { my $v = [ print_address($_, $no_nat_set),
$_->{name} ];
push @$v, $_->{owner}->{name} if $show_owner && $_->{owner};
$v } @$elements;
abort_on_error();
# Prepend group definition as comment.
if ($groups_file) {
print "# $definition\n";
}
# Sort by IP address and print results.
for my $aref (sort { $a->[0] cmp $b->[0] } @info) {
my ($address, $name, $owner) = @$aref;
my @result;
if ($show_ip) {
push @result, $address;
}
if ($show_name) {
push @result, $name;
}
if ($show_owner) {
$owner ||= 'none';
push @result, $owner;
}
print join("\t", @result), "\n";
}
}