forked from rfc1036/rpsltool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nagpeer
executable file
·282 lines (225 loc) · 7.43 KB
/
nagpeer
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
#!/usr/bin/perl
#
# Copyright 2006 by Marco d'Itri <[email protected]>
#
# 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.
use warnings;
use strict;
use YAML::XS qw(Load LoadFile);
use Template;
use File::Temp;
use FindBin;
use lib "$FindBin::RealBin/lib";
use RPSLToolUtils;
use RPSLToolNet;
use RPSLToolWhois;
use RPSLToolTemplate;
my $v6route = qr/[:0-9a-fA-F\/^\-\+]+/;
my $v4route = qr/[\.0-9\/^\-\+]+/;
my $route_set = qr/(?:[Aa][Ss][0-9]+:)?[Rr][Ss]-[:A-Za-z0-9_\-]+/;
my %default_parameters = (
whois_server => 'whois.ripe.net',
cache_root => '/tmp/rpsltool',
);
##############################################################################
my ($ip, $asn, $report_mode);
if (@ARGV != 2) {
usage(1);
} elsif ($ARGV[1] eq 'report') {
$report_mode = 1;
} elsif ($ARGV[1] =~ /^(?:as)?([0-9]{1,5})$/i) {
$asn = $1;
} elsif ($ARGV[1] =~ /^[a-f:0-9\.]+$/i) {
$ip = $ARGV[1]
} else {
usage(1);
}
##############################################################################
my ($param) = LoadFile($ARGV[0]);
die if not $param;
my ($param2, undef, $peers_config) = LoadFile($param->{peersfile});
die if not $param2;
%$param = (%default_parameters, %$param2, %$param);
make_boolean($param, qw(nomail ignore_custom_routes ignore_custom_asn
check_all_routes check_origin_only
whois_die_on_error whois_warn_on_error whois_warn_on_recursive_error));
my $peers = process_peers_config($peers_config, $param);
my $whois = whois_factory($param);
if ($report_mode) {
process_extra_config($peers, $param);
iterate_peers($peers, sub {
my ($peer, $afi) = @_;
return if not $peer->{$afi}->{import} or $peer->{is_backup}
or $peer->{$afi}->{skip};
compare_routes_received_irr($peer, $param, $whois, $afi);
});
create_web_page($peers, $param);
exit;
}
if ($asn) {
$ip = neigh_by_asn($peers, $asn);
die "No neighbor found for AS$asn\n" if not $ip;
}
my $peer = $peers->{$ip};
die "Neighbour $ip does not exist\n" if not $peer;
compare_routes_received_irr($peer, $param, $whois);
create_notice($peer, $param);
exit;
##############################################################################
sub compare_routes_received_irr {
my ($peer, $param, $whois, $afi) = @_;
$afi = ($peer->{ip} =~ /:/ ? 6 : 0) ? 'ipv6' : 'ipv4' if not $afi;
my $top_object = $peer->{$afi}->{import}->[0];
my $report_routes = 1;
$report_routes = 0
if not $param->{check_all_routes} and $top_object =~ /^</;
# If we want a full report for peers which are currently being filtered
# by as-path then we need to strip < > from the objects list.
my @import0;
if ($report_routes) {
@import0 = map { s/^<(.+)>$/$1/g; $_ } @{$peer->{$afi}->{import}};
} else {
@import0 = @{$peer->{$afi}->{import}};
}
# If requested, remove the routes and ASN which may have been added in
# the configuration file to support peers with incomplete objects.
# This is basically an hack needed because we do not know the "official"
# as-set or explicit list of ASN to import.
my @import;
foreach (@import0) {
next if $param->{ignore_custom_routes}
and /^(?:$v6route|$v4route)$/o;
next if $param->{ignore_custom_asn}
and /^<?AS[0-9]+>?$/i and $top_object !~ /^<?AS[0-9]/;
push(@import, $_);
}
$peer->{$afi}->{test_import} = \@import;
# RPSL-expand to routes and ASN the list of as-set and ASN from
# the configuration.
my $ipv6 = $afi =~ /^ipv6/ ? 1 : 0;
my ($routes_irr, $asn_irr) = $whois->import(\@import, $ipv6, 1);
# Read the list of routes actually received and their as-paths.
my $routes_received = [ ];
my $paths_received = { };
read_routes("$param->{routesdir}/$peer->{as}_$peer->{ip}_${afi}_rr",
$routes_received, $paths_received);
$peer->{$afi}->{paths} = $paths_received;
my $diff = filter_networks($routes_received, $routes_irr, 1);
my $asn_received;
if ($param->{check_origin_only}) {
$asn_received = origin2asn($paths_received);
} else {
$asn_received = paths2asn($paths_received);
}
my @asn;
if ($top_object !~ /$route_set/o) {
my %asn_irr = map { $_ => undef } @$asn_irr;
foreach my $as (map { "AS$_" } @$asn_received) {
push(@asn, $as) if not exists $asn_irr{$as};
}
$peer->{$afi}->{missing}->{asn} = \@asn;
}
if ($report_routes) {
my @routes;
my %asn_missing = map { $_ => undef } @asn;
foreach my $route (@$diff) {
my $origin = $paths_received->{$route}; $origin =~ s/^.*\s+/AS/;
push(@routes, $route) if not exists $asn_missing{$origin};
}
$peer->{$afi}->{missing}->{routes} = \@routes;
}
}
##############################################################################
sub create_web_page {
my ($peers, $param) = @_;
die "missing template" if not $param->{webtemplate};
my $vars = {
peers => $peers,
param => $param,
};
$Template::Config::STASH = 'Template::Stash::XS';
my $template = Template->new({
POST_CHOMP => 1,
DEBUG_UNDEF => 1,
ABSOLUTE => 1,
}) or early_error("Template->new: $Template::ERROR");
$template->create_net_vmethods;
die "missing template" if not $param->{webtemplate};
my $output;
$template->process($param->{webtemplate}, $vars, \$output)
or die $template->error;
print $output;
}
##############################################################################
sub process_extra_config {
my ($peers, $param) = @_;
iterate_peers($peers, sub {
my ($peer, $afi) = @_;
return unless $peer->{ip};
$peer->{$afi}->{leak} =
{ map { $_ => 1 } @{$param->{leaks}->{$peer->{ip}}} }
if $param->{leaks}->{$peer->{ip}};
$peer->{$afi}->{note} = $param->{notes}->{$peer->{ip}}
if $param->{notes}->{$peer->{ip}};
$peer->{$afi}->{skip} = 1
if $param->{skip} and grep(/^$peer->{ip}$/i, @{$param->{skip}});
});
}
##############################################################################
sub create_notice {
my ($peer, $param, $afi) = @_;
$afi = ($peer->{ip} =~ /:/ ? 6 : 0) ? 'ipv6' : 'ipv4' if not $afi;
my $vars = $peer->{$afi}->{missing};
return if not ($vars->{routes} and @{$vars->{routes}})
and not ($vars->{asn} and @{$vars->{asn}});
%$vars = (
%$vars,
as => $peer->{as},
ip => $peer->{ip},
paths => $peer->{$afi}->{paths},
param => $param,
);
$vars->{peeremail} = $param->{contacts}->{$peer->{as}}
if $param->{contacts}->{$peer->{as}};
$vars->{top_object} = $peer->{$afi}->{import}->[0];
$vars->{top_object} =~ s/^<(.+)>$/$1/g;
$Template::Config::STASH = 'Template::Stash::XS';
my $template = Template->new({
POST_CHOMP => 1,
DEBUG_UNDEF => 1,
ABSOLUTE => 1,
}) or early_error("Template->new: $Template::ERROR");
$template->create_net_vmethods;
my %intl = map { $_ => undef } @{$param->{international}};
my $input = exists $intl{$peer->{as}}
? $param->{itemplate} : $param->{template};
die "missing template" if not $input;
my $output;
$template->process($input, $vars, \$output) or die $template->error;
if ($param->{nomail}) {
print $output;
} else {
mail_text_interactive($param->{mailcmd}, $output);
}
}
sub mail_text_interactive {
my ($mailcmd, $text) = @_;
my $fh = new File::Temp;
my $filename = $fh->filename;
print $fh $text;
my $cmd = sprintf($mailcmd, $filename);
system $cmd and die "system: $?";
close $fh;
}
##############################################################################
sub usage {
print STDERR <<END;
Usage: nagpeer nagpeer.yml ASN
nagpeer nagpeer.yml IP
nagpeer nagpeer.yml report
END
exit(shift);
}