-
Notifications
You must be signed in to change notification settings - Fork 10
/
cut-netspoc
executable file
·677 lines (583 loc) · 19.7 KB
/
cut-netspoc
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#!/usr/bin/perl
=head1 NAME
cut-netspoc - Print parts of a netspoc configuration to stdout
=head1 SYNOPSIS
cut-netspoc [options] FILE|DIR [service:name] ...
=head1 DESCRIPTION
Reads a Netspoc configuration and prints parts of this configuration
to STDOUT. If one or more services are given as argument, only those
parts are printed, that are referenced by given serices. If no service
is given, it acts as if all services are specified. This is useful to
eliminate all disabled parts of the topology.
=head1 OPTIONS
=item B<-q>
Quiet, don't print status 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>
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 Getopt::Long;
use Pod::Usage;
use Netspoc::Compiler::GetArgs qw(read_config combine_config);
use Netspoc::Compiler::File qw(process_file_or_dir *input);
use Netspoc::Compiler::Common;
use Netspoc::Compiler::Pass1;
use Encode qw(decode_utf8);
# VERSION: inserted by DZP::OurPkgVersion
####################################################################
# Argument processing
####################################################################
my ($quiet, $help, $man, $ipv6);
GetOptions ( 'quiet!' => \$quiet,
'ipv6' => \$ipv6,
'help|?' => \$help,
man => \$man,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
my $path = shift @ARGV;
# Give each source part an index to preserve original order.
my $src_index = 1;
# Enhanced version of parse_input, which stores source code of each
# global definition.
sub parse_input_with_src {
my $length = length $input;
while (skip_space_and_comment, pos $input != $length) {
my $pre = pos $input;
my $object = parse_toplevel();
my $post = pos $input;
$object->{src_code} = substr $input, $pre, $post - $pre;
$object->{src_index} = $src_index++;
}
}
sub change_attribute_src_code {
my ($attr, $object, $replace) = @_;
my $code = $object->{src_code};
# Find attribute outside of comment.
$code =~ m/ ^[^#]* \Q$attr\E (?=[\s;=#]) /gmx;
my $start = pos($code) - length($attr);
# Helper functions to parse attribute value.
my $skip_space_and_comment = sub {
$code =~ /\G (?:(?: [#] .* (?: \n | $ )) | \s*)*/gcx;
};
my $check = sub {
my ($string) = @_;
$skip_space_and_comment->();
return $code =~ /\G\Q$string\E/gc;
};
my $read_token = sub {
$skip_space_and_comment->();
$code =~ /\G [^=,;\s#]* /gcx;
};
my $read_attr_body;
$read_attr_body = sub {
if ($check->(';')) {
# Attribute has no value; finished.
}
elsif ($check->('=')) {
# Read complex value.
if ($check->('{')) {
if ($check->('description')) {
$check->('=');
$code =~ /\G .* \n/gcx;
}
while (1) {
last if $check->('}');
$read_token->();
$read_attr_body->();
}
}
# Read comma separated list of values.
else {
while (1) {
last if $check->(';');
$read_token->();
$check->(',');
}
}
}
};
$read_attr_body->();
my $end = pos($code);
# Remove leading white space and trailing line break.
if (not $replace) {
# Find trailing line break.
$code =~ /\G [ \t]* (?: [#] .* )? (?: \n | $ )/gcx;
$end = pos($code);
# Find leading white space.
pos($code) = $start;
if ($code =~ /([ \t]*)\G/g) {
$start -= length($1);
}
}
my $len = $end - $start;
substr($object->{src_code}, $start, $len) = $replace;
}
sub remove_attribute_src_code {
my ($attr, $object) = @_;
change_attribute_src_code($attr, $object, '');
}
# This function is called by path_walk to mark all managed routers
# on path from src to dst of $rule with attribute {is_used}.
sub mark_topology {
my ($rule, $in_intf, $out_intf) = @_;
my $router = ($in_intf || $out_intf)->{router};
$router->{is_used} = 1;
# debug "Used $router->{name}";
if ($in_intf) {
$in_intf->{is_used} = 1;
$in_intf->{network}->{is_used} = 1;
}
if ($out_intf) {
$out_intf->{is_used} = 1;
$out_intf->{network}->{is_used} = 1;
}
return;
}
# Mark unmanaged routers and networks
# between marked and managed parts of topology.
sub path_unmanaged {
my ($obj, $in_interface) = @_;
return 1 if $obj->{is_used};
return if $obj->{managed} || $obj->{semi_managed};
return if $obj->{active_path};
local $obj->{active_path} = 1;
my $result = 0;
for my $interface (@{ $obj->{interfaces} }) {
next if $interface eq $in_interface;
next if $interface->{main_interface};
my $next = $interface->{is_network($obj) ? 'router' : 'network'};
if (path_unmanaged($next, $interface)) {
$obj->{is_used} = 1;
$interface->{is_used} = 1;
# debug "Marked $interface->{name}";
$result = 1;
}
}
return $result;
}
sub mark_unmanaged {
progress('Marking unmanged routers');
for my $network (values %networks) {
next if not $network->{is_used};
# debug "Path unmanaged $network->{name}";
for my $interface (@{ $network->{interfaces} }) {
next if $interface->{main_interface};
my $router = $interface->{router};
# debug "Try $router->{name} $interface->{name}";
if (path_unmanaged($router, $interface)) {
$interface->{is_used} = 1;
# debug "Marked $interface->{name}";
}
}
}
return;
}
sub mark_path {
my ($src, $dst) = @_;
my $rule = {src => $src, dst => $dst, prt => {}};
# debug "Path $src->{name} $dst->{name}";
&single_path_walk($rule, \&mark_topology);
return;
}
# We can't use %aggregates from Netspoc.pm because it only holds named
# aggregates. But we need unnamed aggregates like any:[network:XX]
# as well.
my @all_zones;
sub get_zones {
my %all_zones;
for my $network (values %networks) {
next if $network->{disabled};
my $zone = $network->{zone};
$all_zones{$zone} = $zone;
}
@all_zones = values %all_zones;
return;
}
####################################################################
# Main program
####################################################################
my $file_config = read_config($path);
$config = combine_config($file_config,
{time_stamps => 1, max_errors => 9999, verbose => !$quiet,
ipv6 => $ipv6? 1 : 0 });
init_global_vars();
show_version();
# Read and process netspoc configuration data.
# Remember source code of each global definition.
process_file_or_dir($path, \&parse_input_with_src);
my %name2var = ( service => \%services);
if (@ARGV) {
# Read names of services from command line.
for my $arg (map { decode_utf8($_, 1) } @ARGV) {
my ($type, $name) = split_typed_name $arg;
my $ref = $name2var{$type} or pod2usage(2);
my $thing = $ref->{$name} or fatal_err "Unknown $arg";
$thing->{is_used} = 1;
}
# Delete unwanted global definitions.
for my $ref (values %name2var) {
for my $name (keys %$ref) {
unless ($ref->{$name}->{is_used}) {
delete $ref->{$name};
}
}
}
}
# Retain all services, if no argument is given.
# Only remove disabled parts.
&order_protocols();
&link_topology();
&mark_disabled();
&set_zone();
&setpath();
&distribute_nat_info();
find_subnets_in_zone();
link_reroute_permit();
normalize_services();
group_path_rules();
# Process rules of services.
sub process_rules {
my ($path_rules) = @_;
for my $rule (@{ $path_rules->{deny} }, @{ $path_rules->{permit} }) {
my ($src_list, $dst_list, $prt_list) = @{$rule}{qw(src dst prt)};
for my $obj (@$src_list, @$dst_list) {
# path_walk only handles managed routers and interfaces.
# Mark all objects additionally here.
my $type = ref $obj;
if ($type eq 'Network') {
# debug "Used $obj->{name}";
$obj->{is_used} = 1;
}
elsif ($type eq 'Host') {
$obj->{is_used} = 1;
# debug "Used $obj->{name}";
$obj->{network}->{is_used} = 1;
}
elsif ($type eq 'Interface') {
$obj->{is_used} = 1;
# debug "Used $obj->{name}";
$obj->{router}->{is_used} = 1;
$obj->{network}->{is_used} = 1;
}
}
&path_walk($rule, \&mark_topology);
for my $prt (@$prt_list) {
my $orig_prt = get_orig_prt(+{ %$rule, prt => $prt });
$prt->{is_used} = 1;
}
}
return;
}
# Mark parts of topology which are referenced by rules of services.
process_rules(\%path_rules);
# Mark networks inside marked aggregates.
get_zones();
for my $zone (@all_zones) {
for my $aggregate (values %{ $zone->{ipmask2aggregate} }) {
$aggregate->{is_used} or next;
# debug "Marking networks of $aggregate->{name} in $zone->{name}";
for my $network (@{ $aggregate->{networks} }) {
$network->{is_used} = 1;
}
}
}
# Mark zones having attributes that influence their networks.
for my $network (values %networks) {
$network->{is_used} or next;
my $zone = $network->{zone};
my $aggregate0 = $zone->{ipmask2aggregate}->{"$zero_ip$zero_ip"};
# Simplifies test below.
$aggregate0->{owner} = $zone->{owner};
grep { $zone->{$_} } qw(has_unenforceable nat no_in_acl) or next;
$aggregate0->{is_used} = 1;
$zone->{is_used} = 1;
}
for my $zone (@all_zones) {
$zone->{is_used} or next;
if (my $link = $zone->{link}) {
$link->{is_used} = 1;
}
}
# Mark areas having attributes that influence their networks.
for my $zone (@all_zones) {
$zone->{is_used} or next;
for my $area (values %{ $zone->{areas} }) {
$area->{nat} or next;
$area->{is_used} = 1;
}
}
# Mark interfaces which are referenced by used areas.
for my $area (values %areas) {
$area->{is_used} or next;
if (my $anchor = $area->{anchor}) {
$anchor->{is_used} = 1;
}
else {
for my $interface (@{ $area->{border} }, @{ $area->{inclusive_border} })
{
$interface->{is_used} = 1;
$interface->{router}->{is_used} = 1;
$interface->{network}->{is_used} = 1;
}
}
}
# Mark networks having NAT attributes that influence their subnets.
for my $network (values %networks) {
$network->{is_used} or next;
my $up = $network;
my @up_chain;
# Walk chain of inheritance.
# Mark supernet with NAT attribute and also all supernets in between.
# We need to mark supernets in between, because they might have
# identity NAT attributes, which have been deleted already.
while ($up = $up->{up}) {
if ($up->{is_aggregate}) {
$up->{mask} eq $zero_ip or next;
my $zone = $up->{zone};
# Check if NAT attribute was inherited from zone or areas.
$zone->{is_used} or
grep { $_->{is_used} } values %{ $zone->{areas} } or
next;
}
else {
push @up_chain, $up;
$up->{nat} or next;
}
for my $supernet (@up_chain) {
$supernet->{is_used} = 1;
# debug "marked: $supernet->{name}";
}
@up_chain = ();
}
}
# Call this after topology has been marked.
&expand_crypto();
# 1. call to mark unmanaged parts of topology.
# Needed to mark unmanaged crypto routers.
mark_unmanaged();
# Mark bridge and bridged networks.
for my $network (values %networks) {
$network->{is_used} or next;
$network->{bridged} or next;
for my $bridge_in (@{ $network->{interfaces} }) {
$bridge_in->{ip} eq 'bridged' or next;
$bridge_in->{is_used} = 1;
my $bridge = $bridge_in->{router};
$bridge->{is_used} = 1;
for my $bridge_out (@{ $bridge->{interfaces} }) {
if ($bridge_out->{hardware}->{name} eq 'device' &&
$bridge->{model}->{class} eq 'ASA') {
$bridge_out->{is_used} = 1;
}
elsif ($bridge_out->{ip} eq 'bridged') {
$bridge_out->{is_used} = 1;
$bridge_out->{network}->{is_used} = 1;
}
}
}
}
for my $router (values %routers) {
$router->{is_used} or next;
# Mark fragments of marked crypto routers.
for my $interface (get_intf($router)) {
my $fragment = $interface->{router};
next if $fragment eq $router;
# debug "Fragment: $fragment->{name}";
$fragment->{is_used} = 1;
}
# Mark path of crypto tunnels.
# Mark main interface of secondary or virtual interface.
for my $interface (get_intf($router)) {
$interface->{is_used} or next;
if (my $main = $interface->{main_interface} || $interface->{orig_main})
{
$main->{is_used} = 1;
}
if ($interface->{ip} eq 'tunnel') {
my $peer = $interface->{peer};
my $real_intf = $interface->{real_interface};
mark_path($real_intf, $peer->{real_interface});
}
}
}
# 2. call to mark unmanaged parts of topology.
# Needed to mark crypto path of unmanaged crypto routers.
mark_unmanaged();
for my $router (values %routers) {
$router->{is_used} or next;
for my $interface (get_intf($router)) {
$interface->{is_used} or next;
# Remove unused networks referenced in attribute reroute_permit.
if (my $networks = $interface->{reroute_permit}) {
my @used_networks = grep { $_->{is_used} } @$networks;
if (not @used_networks) {
remove_attribute_src_code('reroute_permit', $router);
}
elsif(@$networks != @used_networks) {
my $used_net_names =
join ', ', map { $_->{name} } @used_networks;
my $new_code = "reroute_permit = $used_net_names;";
change_attribute_src_code('reroute_permit', $router, $new_code);
}
}
# Mark crypto definitions which are referenced by
# already marked interfaces.
if (my $crypto_list = $interface->{hub}) {
for my $crypto (@$crypto_list) {
$crypto->{is_used} = 1;
my $type = $crypto->{type};
$type->{is_used} = 1;
$type->{key_exchange}->{is_used} = 1;
}
}
# Mark networks referenced by interfaces
# implictly marked by expand_group.
if ($interface->{is_used}) {
$interface->{network}->{is_used} = 1;
}
}
}
# Remove definitions of unused hosts from networks.
progress('Removing unused hosts');
for my $network (values %networks) {
$network->{is_used} or next;
my $hosts = $network->{hosts};
# Retain at least one host of network with ID hosts.
if ($network->{has_id_hosts}) {
if (not grep { $_->{is_used} } @$hosts) {
$hosts->[0]->{is_used} = 1;
}
}
for my $host (@$hosts) {
next if $host->{is_used};
my $name = $host->{name};
# Remove trailing network name of ID-host.
if ($name =~ /^host:id:/) {
$name =~ s/ [.] [^.]+ $ //x;
}
# progress("- $name");
remove_attribute_src_code($name, $network);
}
}
# Remove definitions of unused interfaces from routers
progress('Removing unused interfaces');
for my $router (values %routers) {
$router->{is_used} or next;
for my $interface (get_intf($router)) {
next if $interface->{is_used};
next if $interface->{ip} eq 'tunnel';
# Rename "interface:router.network" to "interface:network".
my $name = $interface->{name};
$name =~ s/ : [^.]+ [.] /:/x;
# Ignore secondary and virtual interfaces.
next if $name =~ s/[.]\w+$//;
# progress("- $interface->{name}");
remove_attribute_src_code($name, $router);
}
}
# Remove one or multiple occurences of attribute 'owner'.
# Multiple from embedded host or interface definiton.
progress('Removing referenced owners');
for my $object (values %networks, values %routers,
values %areas, values %aggregates)
{
$object->{is_used} or next;
$object->{src_code} or next;
while ($object->{src_code} =~ /^[^#]*owner *=/m) {
# progress("- $object->{name}");
remove_attribute_src_code('owner', $object);
}
}
# Remove attribute 'sub_owner'.
progress('Removing referenced sub_owners');
for my $service (values %services) {
$service->{sub_owner} or next;
remove_attribute_src_code('sub_owner', $service);
}
# Remove attribute 'router_attributes'
# with 'owner', 'policy_distribution_point' and 'general_permit'.
for my $area (values %areas) {
$area->{is_used} or next;
$area->{router_attributes} or next;
remove_attribute_src_code('router_attributes', $area);
}
# Remove attribute 'policy_distribution_point'
progress('Removing referenced policy_distribution_point');
for my $router (values %routers) {
$router->{is_used} or next;
$router->{policy_distribution_point} or next;
$router->{src_code} =~ /^[^#]policy_distribution_point/m or next;
remove_attribute_src_code('policy_distribution_point', $router);
}
sub by_index {
return $a->{src_index} <=> $b->{src_index}
}
# Print marked parts of netspoc configuration.
# Routers and networks have been marked by mark_topology.
# Protocols have been marked while @path_rules have been processed above.
# Groups and protocolroups objects have been marked during expand_services.
for my $object ( sort by_index map { $_->{src_index} ? ($_) : () }
values %routers,
values %networks,
values %aggregates,
values %areas,
values %groups,
values %protocols,
values %protocolgroups,
values %isakmp,
values %ipsec,
values %crypto,
)
{
if ($object->{is_used}) {
# There are some internal objects without src_code.
if (my $src_code = $object->{src_code}) {
print $src_code, "\n";
}
}
}
# Source of pathrestrictions can't be used literally,
# but must be reconstructed from internal data structure.
for my $restrict (sort by_index values %pathrestrictions) {
my $used = 0;
for my $interface (@{ $restrict->{elements} }) {
if ($interface->{is_used}) {
$used++;
}
else {
$interface = undef;
}
}
next if $used < 2;
print "$restrict->{name} =\n";
for my $interface (@{ $restrict->{elements} }) {
next unless $interface;
next if $interface->{ip} eq 'tunnel';
print " $interface->{name},\n";
}
print ";\n";
}
# All unwanted services have already been deleted above.
for my $service (sort by_index values %services) {
next if $service->{disabled};
print $service->{src_code}, "\n";
}