forked from abh/ntppool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool
executable file
·499 lines (416 loc) · 14.8 KB
/
pool
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
# -*- cperl -*-
eval 'exec $CBROOT/bin/perl $0 ${1+"$@"}'
unless $running_under_some_shell;
use strict;
use Combust::DB;
use Combust::Config;
use List::Util qw(shuffle);
use Net::hostent;
use Net::NTP;
use Socket;
use Time::HiRes;
use Data::Dumper;
use Sys::Hostname qw(hostname);
use Geo::IP;
use Net::IP;
use LWP::Simple;
use Email::Simple;
use Email::Date;
use Email::Send 'SMTP';
use File::Path qw(mkpath);
use JSON::XS ();
use Math::Random::Secure qw(irand);
my $geo_ip = eval "Geo::IP->new(GEOIP_STANDARD)";
use NP::Model;
use constant LOGFILE => "$ENV{CBROOTLOCAL}/log";
my $pool_domain = Combust::Config->new->site->{ntppool}->{pool_domain}
or die "pool_domain configuration not setup";
$Net::NTP::TIMEOUT = 2;
my $db;
# exit handler
sub closedbconnection {
$db->rollback();
$db->disconnect();
}
# tools
sub ntpdate {
my ($host, $error) = @_;
my %ntp = eval { get_ntp_response($host); };
warn Data::Dumper->Dump([\%ntp]);
my @error;
$ntp{Stratum} ||= '';
push @error, "Bad stratum: $ntp{Stratum}"
unless $ntp{Stratum}
and $ntp{Stratum} < 6;
if (@error) {
print @error, "\n";
my @query = `ntpdate -q $host 2>&1`
or die "Error in command execution: ntpdate\n";
print @query;
return undef;
}
return 1;
}
sub logsay {
my $msg = join " ", @_;
open LOG, ">>" . LOGFILE
or die "Opening log (" . LOGFILE . "): $!\n";
my (undef, undef, undef, $d, $m, $y, undef) = localtime time;
printf LOG "%04d%02d%02d clockmaster %s\n", $y + 1900, $m + 1, $d, $msg;
close LOG;
}
sub server_information {
my $server = shift;
my @zones = $server->zones_display;
my @r;
push @r, sprintf "IP: %s, ID: %i, username: %s email: %s\n",
$server->ip,
$server->id,
$server->admin->username ? $server->admin->username : "-",
$server->admin->email;
push @r, sprintf "score: %.1f, in pool: %i, in server list: %i\n",
$server->score, $server->in_pool, $server->in_server_list;
push @r, "Hostname: " . $server->hostname . "\n";
my $country = $geo_ip->country_code_by_addr($server->ip);
$country = lc $country if $country;
push @r, "Country: $country - " if $country;
push @r, "Netspeed: " . $server->netspeed . "\n";
push @r, "Zones: ", join(", ", map { $_->name } @zones), "\n";
push @r, sprintf "in_pool: %i, in_server_list: %i\n", $server->in_pool, $server->in_server_list;
push @r, "Deletion on: ", $server->deletion_on->ymd, "\n"
if $server->deletion_on;
@r;
}
if ($#ARGV < 0) {
die "pool add|report|addmonitor|zone [<args>]\n";
}
# connect to the db
$db = Combust::DB::db_open('ntppool', {AutoCommit => 0})
or die "Couldn't connect to database\n";
#$SIG{__DIE__} = \&closedbconnection;
sub find_user_by_email {
my $email = shift;
my $user = NP::Model->user->fetch(email => $email);
$user = NP::Model->user->fetch(username => $email) unless $user;
$user = NP::Model->user->fetch(id => $email) unless $user;
$user = NP::Model->user->fetch_or_create(email => $email) unless $user;
$user->save;
return $user;
}
my $cmd = shift @ARGV;
if ($cmd eq "add") {
if ($#ARGV < 1) {
die "pool add <email> <host or IP> [<zone ...>]\n";
}
# look if the email address is already registered
my $email = shift @ARGV;
my $user = find_user_by_email($email);
# do we have a hostname or an IP?
my $hostname = shift @ARGV;
my $ip;
if (Net::IP::ip_is_ipv4($hostname)) {
$ip = $hostname;
$hostname = undef;
}
else {
my $addrs = gethostbyname($hostname)
or die "$hostname does not resolve.\n";
if ($#{$addrs->addr_list} > 0) {
print "warning: $hostname has multiple IP addresses.\n";
}
$ip = inet_ntoa(${$addrs->addr_list}[0]);
}
# test the timeserver
ntpdate $ip, "Bad server " . ($hostname ? $hostname : "") . "[$ip]:"
or exit;
my $server = NP::Model->server->fetch(ip => $ip);
if ($server) {
die "server $ip already in database for ", $server->user->email, "\n";
}
$server = NP::Model->server->create(
ip => $ip,
hostname => $hostname,
user_id => $user->id,
in_pool => 1,
);
for my $country (@ARGV) {
$server->join_zone($country);
}
$server->save;
unless (defined $hostname) { $hostname = $ip; }
my $z = join " ", @ARGV;
logsay($server->id . " add $email $hostname $z");
}
elsif ($cmd eq "addmonitor") {
if ($#ARGV < 2) {
die "pool addmonitor <email> <host or IP> <ip_version>\n";
}
my ($email, $hostname, $ip_version) = @ARGV;
die "ip_version must be v4 or v6" unless $ip_version =~ m/^v[46]$/;
my $db = NP::Model->db;
my $txn = $db->begin_scoped_work or die $db->error;
# look if the email address is already registered
my $user = find_user_by_email($email);
# do we have a hostname or an IP?
my $ip = Net::IP->new($hostname);
if ($ip) {
$ip = $ip->short;
$hostname = undef;
}
else {
my $addrs = gethostbyname($hostname)
or die "$hostname does not resolve.\n";
if ($#{$addrs->addr_list} > 0) {
print "warning: $hostname has multiple IP addresses.\n";
}
$ip = inet_ntoa(${$addrs->addr_list}[0]);
}
my $monitor = NP::Model->monitor->create(
ip => $ip,
user_id => $user->id,
ip_version => $ip_version,
);
require Math::BaseCalc;
my $base36 = Math::BaseCalc->new(digits => ['a'..'k', 'm'..'z', 2..9]);
my @numbers = map { $base36->to_base(irand) } (undef) x 3;
my $api_key = join "", @numbers;
$monitor->api_key( $api_key );
$monitor->save;
my $id = $monitor->id;
$db->dbh->do( q[ insert into server_scores (monitor_id, server_id, score_raw)
select ?, id, score_raw from servers
where ip_version = ? and deletion_on is null
], {}, $monitor->id, $monitor->ip_version );
for my $f (qw(ip ip_version api_key)) {
printf "%-10s: %s\n", $f, $monitor->$f;
}
$db->commit or die $db->error;
}
elsif ($cmd eq "edit") {
my $arg = shift @ARGV or die "pool zones <host or IP>\n";
my $server = NP::Model->server->find_server($arg);
die "Could not find server $arg\n" unless $server;
print server_information($server);
my $changes = 0;
while (my $k = shift @ARGV) {
$changes++;
print "\n";
my $v = shift @ARGV;
last unless defined $v;
print "Set $k -> $v";
if ($server->can($k)) {
$server->$k($v);
}
else {
die "Don\'t know how to set $k\n";
}
}
if ($changes and $server->save(changes_only => 1) > 0) {
print "\n\nUpdated =============\n";
print server_information($server);
}
}
elsif ($cmd eq "zones") {
my $arg = shift @ARGV or die "pool zones <host or IP>\n";
my $server = NP::Model->server->find_server($arg);
die "Could not find server $arg\n" unless $server;
print "\n";
print server_information($server);
my @zones = $server->zones;
for my $zone_name (@ARGV) {
my @zones = $server->zones;
if ($zone_name =~ s/^-//) {
my ($zone) = grep { $_->name eq $zone_name } @zones;
print "Could not find location '$zone_name' to delete\n" and next
unless $zone;
$server->leave_zone($zone_name);
}
else {
$zone_name =~ s/^\+//;
next if grep { $zone_name eq $_->name } @zones;
my $zone = NP::Model->zone->fetch(name => $zone_name);
print "Could not find '$zone_name'\n" and next unless $zone;
$server->join_zone($zone);
}
}
$server->save;
@zones = $server->zones_display;
print "Zones: ", join(", ", map { $_->name } @zones), "\n";
}
elsif ($cmd eq "rm") {
my $arg = shift @ARGV
or die "pool rm <host or IP>\n";
my $server = NP::Model->server->find_server($arg);
die "Could not find server $arg\n" unless $server;
my $server_desc = (join " / ", grep { defined $_ } $server->ip, $server->hostname);
logsay($server->id, "rm", $server_desc);
printf "%i, %s - %s\n", $server->id, $server_desc, $server->user->email;
my $date = DateTime->today;
$server->deletion_on($date);
$server->add_logs(
{ user_id => undef,
type => 'delete',
message => "Deletion scheduled for " . $date->ymd . " by pool admin"
}
);
$server->update;
}
elsif ($cmd eq "ip") {
my ($from, $to) = @ARGV;
$to or die "pool $cmd <host or IP> <IP>\n";
my $server = NP::Model->server->find_server($from);
die "Could not find server $from\n" unless $server;
print server_information($server);
my $server_desc = (join " / ", grep { defined $_ } $server->ip, $server->hostname);
logsay($server->id, "ip", "$server_desc => $to");
printf "%i, %s (%s) => %s\n", $server->id, $server_desc, $server->admin->email, $to;
$server->add_logs(
{ user_id => undef,
type => 'ip_change',
message => "IP change from " . $server->ip . " to $to",
}
);
$server->ip($to);
$server->score_raw(0);
$server->save;
}
elsif ($cmd eq "report") {
my $rcpt = shift;
my $config = Combust::Config->new;
my $url = $config->base_url("ntppool") . "/bulletin";
my $msg = get($url);
die "no msg?!" unless $msg;
my $head = "";
$rcpt and $head = <<EOT;
From: $pool_domain monitoring script <clockmaster\@fortytwo.ch>
To: <$rcpt>
EOT
if ($rcpt) {
my $email = Email::Simple->new($head . $msg);
$email->header_set('Message-ID' => join("-", int(rand(1000)), $$, time) . '@' . hostname);
$email->header_set('Date' => Email::Date::format_date);
my $return = send SMTP => $email, 'localhost';
}
else {
print $msg;
}
}
elsif ($cmd eq "zone") {
mkpath "$ENV{CBROOTLOCAL}/dns/", 0, 0711;
my $file = "$ENV{CBROOTLOCAL}/dns/$pool_domain";
open(my $fh, ">$file.tmp") or die "Could not open $file.tmp: $!";
open HEAD, "<header"
or die "Opening DNS zone file header (header): $!\n";
print $fh "# ATTENTION\n# This is a generated file\n#\n";
my $time = time;
while (<HEAD>) {
s/\@VERSION\@/$time/;
print $fh $_;
}
close HEAD;
my $ttl = 390;
my $pgeodns = {
serial => $time,
ttl => $ttl,
};
my $www_record = {
cname => 'ntppool-varnish.develooper.com',
ttl => 7200,
};
$pgeodns->{data}->{www} = $www_record;
$pgeodns->{data}->{web} = $www_record;
# todo: check logs that this isn't used...
for (0 .. 3) {
$pgeodns->{data}->{"$_.vendors"} = {
cname => "$_.$pool_domain",
ttl => 43200,
};
}
my $vendordir = "vendordns";
opendir my $dir, $vendordir or die "could not open '$vendordir' dir: $!";
my @vendor_files =
grep { $_ !~ /\~$/ and -f $_ } map {"$vendordir/$_"} readdir($dir);
closedir $dir;
# We used to read the data from the vendor files as tinydns data; now we
# just include them with the other vendor zone names...
my $vendor_zones = NP::Model->vendor_zone->get_vendor_zones(
query => [status => 'Approved'],
order => 'approved_on',
);
my @vendor_zone_names = map { $_->zone_name } @$vendor_zones;
push @vendor_zone_names, map { s!.*/!!; $_ } @vendor_files;
for my $name (@vendor_zone_names) {
for my $i (0 .. 3) {
$pgeodns->{data}->{"$i.$name"}->{alias} = "$i";
print $fh "C$i.$name.${pool_domain}:$i.${pool_domain}:$ttl\n";
}
}
my $zones = NP::Model->zone->get_zones_iterator(query => [dns => 1]);
while (my $zone = $zones->next) {
my $name = $zone->name;
$name = '' if $name eq '@';
$name = "$name." if $name;
if (my $entries = $zone->active_servers('v4')) {
my $min_non_duplicate_size = 2;
my $response_records = 3;
my @zones = ("", "0.", "1.", "2.", "3.");
my $zone_count = scalar @zones;
$min_non_duplicate_size = int(@$entries / $zone_count)
if (@$entries / $zone_count > $min_non_duplicate_size);
print $fh "# " . scalar @$entries . " active servers in ", $zone->name, "\n";
if ($#$entries < ($min_non_duplicate_size * $zone_count - 1)) {
print $fh "# possible duplicates in the zones below (", $zone->name, ")\n";
foreach my $z (@zones) {
(my $pgeodns_group = "$z${name}") =~ s/\.$//;
$pgeodns->{data}->{$pgeodns_group}->{a} = [];
@$entries = shuffle(@$entries);
my $i;
foreach my $e (@$entries) {
$i++;
print $fh "+$z${name}${pool_domain}:$$e[0]:$ttl\n"
if $i < 12;
push @{$pgeodns->{data}->{$pgeodns_group}->{a}}, $e;
}
}
}
else {
print $fh "# 'big' zone without duplicates below (", $zone->name, ")\n";
@$entries = shuffle(@$entries);
foreach my $z (@zones) {
(my $pgeodns_group = "$z${name}") =~ s/\.$//;
$pgeodns->{data}->{$pgeodns_group}->{a} = [];
for (my $i = 0; $i < $min_non_duplicate_size; $i++) {
my $e = shift @$entries;
print $fh "+$z${name}${pool_domain}:", $e->[0], ":$ttl\n"
if $i < 14;
push @{$pgeodns->{data}->{$pgeodns_group}->{a}}, $e;
}
}
}
}
if (my $entries = $zone->active_servers('v6')) {
@$entries = shuffle(@$entries);
# for now just put all IPv6 servers in the '2' zone
(my $pgeodns_group = "2.${name}") =~ s/\.$//;
push @{$pgeodns->{data}->{$pgeodns_group}->{aaaa}}, $_ for @$entries;
}
}
close $fh or die "could not close $file.tmp: $!";
rename "$file.tmp", "$file.tinydns"
or die "could not rename $file.tmp to $file.tinydns: $!";
my $json = JSON::XS->new->pretty->utf8;
my $js = $json->encode($pgeodns);
my $file = "$ENV{CBROOTLOCAL}/dns/$pool_domain.json";
open(my $js_fh, ">$file.tmp") or die "Could not open $file.tmp: $!";
print $js_fh $js;
close $js_fh or die "could not close $file.json.tmp: $!";
rename "$file.tmp", "$file"
or die "could not rename $file.tmp to $file: $!";
}
else {
die "Unknown command: $cmd\n";
}
#$db->commit() or die "Error on commit\n";
$SIG{__DIE__} = 'DEFAULT';
$db->disconnect();