LocalAuthority / scripts / host_manager.pl
Newer Older
1086 lines | 39.312kb
Xdev Host Manager authored 2 days ago
1
#!/usr/bin/env perl
2
#
3
# host_manager.pl - Minimal host registry web app with no CPAN dependencies.
4
#
5

            
6
use strict;
7
use warnings;
8

            
9
use Cwd qw(abs_path);
10
use Digest::SHA qw(hmac_sha1 hmac_sha256_hex sha256_hex);
11
use File::Basename qw(dirname);
12
use File::Path qw(make_path);
13
use IO::Socket::INET;
14
use POSIX qw(strftime);
15
use Time::HiRes qw(time);
16

            
17
my $script_dir = dirname(abs_path($0));
18
my $project_dir = dirname($script_dir);
19

            
20
my %opt = (
21
    bind => $ENV{HOST_MANAGER_BIND} || '127.0.0.1',
22
    port => $ENV{HOST_MANAGER_PORT} || 8088,
23
    data => $ENV{HOST_MANAGER_DATA} || "$project_dir/config/hosts.yaml",
24
    local_hosts_tsv => $ENV{HOST_MANAGER_LOCAL_HOSTS_TSV} || "$project_dir/config/local-hosts.tsv",
25
);
26

            
27
while (@ARGV) {
28
    my $arg = shift @ARGV;
29
    if ($arg eq '--bind') {
30
        $opt{bind} = shift @ARGV;
31
    } elsif ($arg eq '--port') {
32
        $opt{port} = shift @ARGV;
33
    } elsif ($arg eq '--data') {
34
        $opt{data} = shift @ARGV;
35
    } elsif ($arg eq '--local-hosts-tsv') {
36
        $opt{local_hosts_tsv} = shift @ARGV;
37
    } elsif ($arg eq '--help' || $arg eq '-h') {
38
        usage();
39
        exit 0;
40
    } else {
41
        die "Unknown option: $arg\n";
42
    }
43
}
44

            
45
my $session_secret = $ENV{HOST_MANAGER_SESSION_SECRET} || random_hex(32);
46
my %sessions;
47

            
48
my $server = IO::Socket::INET->new(
49
    LocalHost => $opt{bind},
50
    LocalPort => $opt{port},
51
    Proto => 'tcp',
52
    Listen => 10,
53
    ReuseAddr => 1,
54
) or die "Cannot listen on $opt{bind}:$opt{port}: $!\n";
55

            
56
print "host-manager listening on http://$opt{bind}:$opt{port}\n";
57
print "data file: $opt{data}\n";
58
print "OTP login: " . ($ENV{HOST_MANAGER_TOTP_SECRET} ? "enabled\n" : "disabled; set HOST_MANAGER_TOTP_SECRET\n");
59

            
60
while (my $client = $server->accept) {
61
    eval {
62
        $client->autoflush(1);
63
        handle_client($client);
64
    };
65
    if ($@) {
66
        eval { send_json($client, 500, { error => 'internal_error', detail => "$@" }); };
67
    }
68
    close $client;
69
}
70

            
71
sub usage {
72
    print <<"EOF";
73
Usage: perl scripts/host_manager.pl [--bind 127.0.0.1] [--port 8088]
74

            
75
Environment:
76
  HOST_MANAGER_TOTP_SECRET      Base32 TOTP secret required for write access.
77
  HOST_MANAGER_SESSION_SECRET   Optional session signing secret.
78
  HOST_MANAGER_DATA             Defaults to config/hosts.yaml.
79
  HOST_MANAGER_LOCAL_HOSTS_TSV  Defaults to config/local-hosts.tsv.
80

            
81
Read-only endpoints do not require authentication.
82
EOF
83
}
84

            
85
sub handle_client {
86
    my ($client) = @_;
87
    my $request_line = <$client>;
88
    return unless defined $request_line;
89
    $request_line =~ s/\r?\n$//;
90
    my ($method, $target) = $request_line =~ m{^([A-Z]+)\s+(\S+)\s+HTTP/};
91
    return send_text($client, 400, 'bad request') unless $method && $target;
92

            
93
    my %headers;
94
    while (my $line = <$client>) {
95
        $line =~ s/\r?\n$//;
96
        last if $line eq '';
97
        my ($k, $v) = split /:\s*/, $line, 2;
98
        $headers{lc $k} = $v if defined $k && defined $v;
99
    }
100

            
101
    my $body = '';
102
    if (($headers{'content-length'} || 0) > 0) {
103
        read($client, $body, int($headers{'content-length'}));
104
    }
105

            
106
    my ($path, $query) = split /\?/, $target, 2;
107
    my %query = parse_params($query || '');
108

            
109
    if ($method eq 'GET' && $path eq '/') {
110
        return send_html($client, 200, app_html());
111
    }
112
    if ($method eq 'GET' && $path eq '/healthz') {
Xdev Host Manager authored 2 days ago
113
        return send_json($client, 200, { ok => json_bool(1) });
Xdev Host Manager authored 2 days ago
114
    }
115
    if ($method eq 'GET' && $path eq '/api/session') {
116
        return send_json($client, 200, { authenticated => is_authenticated(\%headers) ? json_bool(1) : json_bool(0) });
117
    }
Xdev Host Manager authored 2 days ago
118
    if ($method eq 'POST' && $path eq '/api/login') {
119
        return send_json($client, 503, { error => 'otp_not_configured' }) unless $ENV{HOST_MANAGER_TOTP_SECRET};
120
        my $payload = request_payload(\%headers, $body);
121
        my $otp = $payload->{otp} || '';
122
        if (!verify_totp($ENV{HOST_MANAGER_TOTP_SECRET} || '', $otp)) {
123
            return send_json($client, 401, { error => 'invalid_otp' });
124
        }
125
        my $token = create_session();
126
        return send_json($client, 200, { ok => json_bool(1) }, [ "Set-Cookie: hm_session=$token; HttpOnly; SameSite=Strict; Path=/" ]);
127
    }
128
    if ($method eq 'POST' && $path eq '/api/logout') {
129
        expire_session(\%headers);
130
        return send_json($client, 200, { ok => json_bool(1) }, [ "Set-Cookie: hm_session=deleted; Max-Age=0; Path=/" ]);
131
    }
132

            
133
    return send_json($client, 401, { error => 'authentication_required' }) unless is_authenticated(\%headers);
134

            
Xdev Host Manager authored 2 days ago
135
    if ($method eq 'GET' && $path eq '/api/hosts') {
136
        my $registry = load_registry();
137
        return send_json($client, 200, registry_payload($registry));
138
    }
139
    if ($method eq 'GET' && $path eq '/download/hosts.yaml') {
140
        return send_file($client, $opt{data}, 'application/x-yaml; charset=utf-8', 'hosts.yaml');
141
    }
142
    if ($method eq 'GET' && $path eq '/download/local-hosts.tsv') {
143
        my $registry = load_registry();
144
        return send_download($client, 200, render_local_hosts_tsv($registry), 'text/tab-separated-values; charset=utf-8', 'local-hosts.tsv');
145
    }
146
    if ($method eq 'GET' && $path eq '/download/monitoring.json') {
147
        my $registry = load_registry();
148
        return send_download($client, 200, json_encode(render_monitoring($registry)), 'application/json; charset=utf-8', 'monitoring-hosts.json');
149
    }
150

            
151
    if ($method eq 'POST' && $path =~ m{^/api/}) {
152
        if ($path eq '/api/hosts/upsert') {
153
            my $payload = request_payload(\%headers, $body);
154
            return upsert_host($client, $payload);
155
        }
156
        if ($path eq '/api/hosts/delete') {
157
            my $payload = request_payload(\%headers, $body);
158
            return delete_host($client, $payload->{id} || '');
159
        }
160
        if ($path eq '/api/render/local-hosts-tsv') {
161
            my $registry = load_registry();
162
            my $content = render_local_hosts_tsv($registry);
163
            backup_file($opt{local_hosts_tsv});
164
            write_file($opt{local_hosts_tsv}, $content);
165
            return send_json($client, 200, { ok => json_bool(1), file => $opt{local_hosts_tsv} });
166
        }
167
    }
168

            
169
    return send_json($client, 404, { error => 'not_found' });
170
}
171

            
172
sub load_registry {
173
    return parse_hosts_yaml(read_file($opt{data}));
174
}
175

            
176
sub save_registry {
177
    my ($registry) = @_;
178
    $registry->{updated_at} = iso_now();
179
    backup_file($opt{data});
180
    write_file($opt{data}, render_hosts_yaml($registry));
181
}
182

            
183
sub registry_payload {
184
    my ($registry) = @_;
185
    my $problems = analyze_hosts($registry->{hosts});
186
    return {
187
        version => $registry->{version},
188
        updated_at => $registry->{updated_at},
189
        policy => $registry->{policy},
190
        hosts => $registry->{hosts},
191
        problems => $problems,
192
        counts => {
193
            hosts => scalar @{ $registry->{hosts} },
194
            problems => scalar @$problems,
195
        },
196
    };
197
}
198

            
199
sub upsert_host {
200
    my ($client, $payload) = @_;
201
    my $id = clean_id($payload->{id} || '');
202
    return send_json($client, 400, { error => 'invalid_id' }) unless $id;
203

            
204
    my $hosts_ip = clean_scalar($payload->{hosts_ip} || '');
205
    my $dns_ip = clean_scalar($payload->{dns_ip} || '');
206
    return send_json($client, 400, { error => 'missing_ip' }) unless $hosts_ip && $dns_ip;
207

            
208
    my @names = clean_list($payload->{names});
209
    return send_json($client, 400, { error => 'missing_names' }) unless @names;
210

            
211
    my $registry = load_registry();
212
    my %host = (
213
        id => $id,
214
        status => clean_scalar($payload->{status} || 'active'),
215
        hosts_ip => $hosts_ip,
216
        dns_ip => $dns_ip,
217
        names => \@names,
218
        roles => [ clean_list($payload->{roles}) ],
219
        sources => [ clean_list($payload->{sources}) ],
220
        monitoring => clean_scalar($payload->{monitoring} || 'pending'),
221
        notes => clean_scalar($payload->{notes} || ''),
222
    );
223

            
224
    my $replaced = 0;
225
    for my $i (0 .. $#{ $registry->{hosts} }) {
226
        if ($registry->{hosts}->[$i]{id} eq $id) {
227
            $registry->{hosts}->[$i] = \%host;
228
            $replaced = 1;
229
            last;
230
        }
231
    }
232
    push @{ $registry->{hosts} }, \%host unless $replaced;
233
    save_registry($registry);
234
    return send_json($client, 200, { ok => json_bool(1), host => \%host });
235
}
236

            
237
sub delete_host {
238
    my ($client, $id) = @_;
239
    $id = clean_id($id);
240
    return send_json($client, 400, { error => 'invalid_id' }) unless $id;
241

            
242
    my $registry = load_registry();
243
    my @kept = grep { $_->{id} ne $id } @{ $registry->{hosts} };
244
    return send_json($client, 404, { error => 'not_found' }) if @kept == @{ $registry->{hosts} };
245
    $registry->{hosts} = \@kept;
246
    save_registry($registry);
247
    return send_json($client, 200, { ok => json_bool(1) });
248
}
249

            
250
sub analyze_hosts {
251
    my ($hosts) = @_;
252
    my @problems;
253
    my (%names, %ids);
254
    for my $host (@$hosts) {
255
        push @problems, problem($host, 'duplicate-id', "Duplicate id $host->{id}") if $ids{ $host->{id} }++;
256
        my @fqdn = grep { /\.madagascar\.xdev\.ro$/ } @{ $host->{names} || [] };
257
        push @problems, problem($host, 'missing-fqdn', 'No madagascar.xdev.ro FQDN') unless @fqdn || ($host->{status} || '') ne 'active';
258
        push @problems, problem($host, 'deprecated-vad-is', 'Deprecated vad.is.xdev.ro name present')
259
            if grep { /\.vad\.is\.xdev\.ro$/ } @{ $host->{names} || [] };
260
        push @problems, problem($host, 'legacy-prefix', 'Legacy prefix should be normalized out')
261
            if grep { /^(is|vad|b)-/ } @{ $host->{names} || [] };
262
        for my $name (@{ $host->{names} || [] }) {
263
            push @problems, problem($host, 'duplicate-name', "Duplicate name $name") if $names{$name}++;
264
        }
265
        if (($host->{hosts_ip} || '') ne ($host->{dns_ip} || '') && ($host->{hosts_ip} || '') ne '127.0.0.1') {
266
            push @problems, problem($host, 'split-ip', 'hosts_ip differs from dns_ip; check that this is intentional');
267
        }
268
    }
269
    return \@problems;
270
}
271

            
272
sub problem {
273
    my ($host, $code, $message) = @_;
274
    return { host_id => $host->{id}, code => $code, message => $message };
275
}
276

            
277
sub render_local_hosts_tsv {
278
    my ($registry) = @_;
279
    my $out = "# Local DNS manifest for the madagascar network.\n";
280
    $out .= "# Generated by scripts/host_manager.pl from config/hosts.yaml.\n";
281
    $out .= "#\n";
282
    $out .= "# Format:\n";
283
    $out .= "# hosts_ip<TAB>dns_ip<TAB>name [aliases...]\n";
284
    $out .= "#\n";
285
    $out .= "# Priority rule:\n";
286
    $out .= "# - DHCP lease/reservation on 192.168.2.1 is canonical for LAN IP allocation.\n";
287
    $out .= "# - madagascar.json is canonical for cluster roles and service interfaces.\n";
288
    $out .= "# - This file publishes approved local DNS records derived from those sources.\n";
289
    for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} }) {
290
        next unless ($host->{status} || 'active') eq 'active';
291
        next unless @{ $host->{names} || [] };
292
        $out .= join("\t", $host->{hosts_ip}, $host->{dns_ip}, join(' ', @{ $host->{names} })) . "\n";
293
    }
294
    return $out;
295
}
296

            
297
sub render_monitoring {
298
    my ($registry) = @_;
299
    my @hosts;
300
    for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} }) {
301
        next unless ($host->{status} || 'active') eq 'active';
302
        next if ($host->{monitoring} || 'pending') eq 'disabled';
303
        push @hosts, {
304
            id => $host->{id},
305
            primary_name => $host->{names}[0],
306
            address => $host->{dns_ip},
307
            aliases => [ @{ $host->{names} || [] } ],
308
            roles => [ @{ $host->{roles} || [] } ],
309
            monitoring => $host->{monitoring} || 'pending',
310
            notes => $host->{notes} || '',
311
        };
312
    }
313
    return {
314
        version => $registry->{version},
315
        generated_at => iso_now(),
316
        source => 'config/hosts.yaml',
317
        hosts => \@hosts,
318
    };
319
}
320

            
321
sub parse_hosts_yaml {
322
    my ($text) = @_;
323
    my %registry = (
324
        version => 1,
325
        updated_at => '',
326
        policy => {},
327
        hosts => [],
328
    );
329
    my ($section, $current, $list_key);
330
    for my $line (split /\n/, $text) {
331
        next if $line =~ /^\s*$/ || $line =~ /^\s*#/;
332
        if ($line =~ /^version:\s*(\d+)/) {
333
            $registry{version} = int($1);
334
        } elsif ($line =~ /^updated_at:\s*(.+)$/) {
335
            $registry{updated_at} = yaml_unquote($1);
336
        } elsif ($line =~ /^policy:\s*$/) {
337
            $section = 'policy';
338
        } elsif ($line =~ /^hosts:\s*$/) {
339
            $section = 'hosts';
340
        } elsif (($section || '') eq 'policy' && $line =~ /^  ([A-Za-z0-9_]+):\s*(.+)$/) {
341
            $registry{policy}{$1} = yaml_unquote($2);
342
        } elsif (($section || '') eq 'hosts' && $line =~ /^  - id:\s*(.+)$/) {
343
            $current = {
344
                id => yaml_unquote($1),
345
                status => 'active',
346
                hosts_ip => '',
347
                dns_ip => '',
348
                names => [],
349
                roles => [],
350
                sources => [],
351
                monitoring => 'pending',
352
                notes => '',
353
            };
354
            push @{ $registry{hosts} }, $current;
355
            $list_key = undef;
356
        } elsif ($current && $line =~ /^    ([A-Za-z0-9_]+):\s*$/) {
357
            $list_key = $1;
358
            $current->{$list_key} ||= [];
359
        } elsif ($current && defined $list_key && $line =~ /^      -\s*(.+)$/) {
360
            push @{ $current->{$list_key} }, yaml_unquote($1);
361
        } elsif ($current && $line =~ /^    ([A-Za-z0-9_]+):\s*(.*)$/) {
362
            $current->{$1} = yaml_unquote($2);
363
            $list_key = undef;
364
        }
365
    }
366
    return \%registry;
367
}
368

            
369
sub render_hosts_yaml {
370
    my ($registry) = @_;
371
    my $out = "version: " . int($registry->{version} || 1) . "\n";
372
    $out .= "updated_at: " . yq($registry->{updated_at} || iso_now()) . "\n";
373
    $out .= "policy:\n";
374
    for my $key (sort keys %{ $registry->{policy} || {} }) {
375
        $out .= "  $key: " . yq($registry->{policy}{$key}) . "\n";
376
    }
377
    $out .= "hosts:\n";
378
    for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} || [] }) {
379
        $out .= "  - id: " . yq($host->{id}) . "\n";
380
        for my $key (qw(status hosts_ip dns_ip)) {
381
            $out .= "    $key: " . yq($host->{$key} || '') . "\n";
382
        }
383
        for my $key (qw(names roles sources)) {
384
            $out .= "    $key:\n";
385
            for my $value (@{ $host->{$key} || [] }) {
386
                $out .= "      - " . yq($value) . "\n";
387
            }
388
        }
389
        $out .= "    monitoring: " . yq($host->{monitoring} || 'pending') . "\n";
390
        $out .= "    notes: " . yq($host->{notes} || '') . "\n";
391
    }
392
    return $out;
393
}
394

            
395
sub request_payload {
396
    my ($headers, $body) = @_;
397
    my $type = $headers->{'content-type'} || '';
398
    if ($type =~ m{application/json}) {
399
        return json_decode($body || '{}');
400
    }
401
    return { parse_params($body || '') };
402
}
403

            
404
sub json_bool {
405
    my ($value) = @_;
406
    return bless \(my $bool = $value ? 1 : 0), 'HostManager::JSONBool';
407
}
408

            
409
sub json_encode {
410
    my ($value) = @_;
411
    if (!defined $value) {
412
        return 'null';
413
    }
414
    my $ref = ref($value);
415
    if (!$ref) {
416
        return $value if $value =~ /\A-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?\z/;
417
        return json_string($value);
418
    }
419
    if ($ref eq 'HostManager::JSONBool') {
420
        return $$value ? 'true' : 'false';
421
    }
422
    if ($ref eq 'ARRAY') {
423
        return '[' . join(',', map { json_encode($_) } @$value) . ']';
424
    }
425
    if ($ref eq 'HASH') {
426
        return '{' . join(',', map { json_string($_) . ':' . json_encode($value->{$_}) } sort keys %$value) . '}';
427
    }
428
    return json_string("$value");
429
}
430

            
431
sub json_string {
432
    my ($value) = @_;
433
    $value = '' unless defined $value;
434
    $value =~ s/\\/\\\\/g;
435
    $value =~ s/"/\\"/g;
436
    $value =~ s/\n/\\n/g;
437
    $value =~ s/\r/\\r/g;
438
    $value =~ s/\t/\\t/g;
439
    $value =~ s/([\x00-\x1f])/sprintf("\\u%04x", ord($1))/eg;
440
    return qq("$value");
441
}
442

            
443
sub json_decode {
444
    my ($text) = @_;
445
    my $i = 0;
446
    my $len = length($text);
447
    my ($parse_value, $parse_string, $parse_array, $parse_object, $parse_number, $skip_ws);
448

            
449
    $skip_ws = sub {
450
        $i++ while $i < $len && substr($text, $i, 1) =~ /\s/;
451
    };
452

            
453
    $parse_string = sub {
454
        die "Expected JSON string\n" unless substr($text, $i, 1) eq '"';
455
        $i++;
456
        my $out = '';
457
        while ($i < $len) {
458
            my $ch = substr($text, $i++, 1);
459
            return $out if $ch eq '"';
460
            if ($ch eq "\\") {
461
                die "Bad JSON escape\n" if $i >= $len;
462
                my $esc = substr($text, $i++, 1);
463
                if ($esc eq '"' || $esc eq "\\" || $esc eq '/') {
464
                    $out .= $esc;
465
                } elsif ($esc eq 'b') {
466
                    $out .= "\b";
467
                } elsif ($esc eq 'f') {
468
                    $out .= "\f";
469
                } elsif ($esc eq 'n') {
470
                    $out .= "\n";
471
                } elsif ($esc eq 'r') {
472
                    $out .= "\r";
473
                } elsif ($esc eq 't') {
474
                    $out .= "\t";
475
                } elsif ($esc eq 'u') {
476
                    my $hex = substr($text, $i, 4);
477
                    die "Bad JSON unicode escape\n" unless $hex =~ /\A[0-9A-Fa-f]{4}\z/;
478
                    $out .= chr(hex($hex));
479
                    $i += 4;
480
                } else {
481
                    die "Bad JSON escape\n";
482
                }
483
            } else {
484
                $out .= $ch;
485
            }
486
        }
487
        die "Unterminated JSON string\n";
488
    };
489

            
490
    $parse_number = sub {
491
        my $start = $i;
492
        $i++ if substr($text, $i, 1) eq '-';
493
        $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
494
        if ($i < $len && substr($text, $i, 1) eq '.') {
495
            $i++;
496
            $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
497
        }
498
        if ($i < $len && substr($text, $i, 1) =~ /[eE]/) {
499
            $i++;
500
            $i++ if $i < $len && substr($text, $i, 1) =~ /[+-]/;
501
            $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
502
        }
503
        return 0 + substr($text, $start, $i - $start);
504
    };
505

            
506
    $parse_array = sub {
507
        die "Expected JSON array\n" unless substr($text, $i, 1) eq '[';
508
        $i++;
509
        my @out;
510
        $skip_ws->();
511
        if ($i < $len && substr($text, $i, 1) eq ']') {
512
            $i++;
513
            return \@out;
514
        }
515
        while (1) {
516
            push @out, $parse_value->();
517
            $skip_ws->();
518
            my $ch = substr($text, $i++, 1);
519
            last if $ch eq ']';
520
            die "Expected JSON array comma\n" unless $ch eq ',';
521
        }
522
        return \@out;
523
    };
524

            
525
    $parse_object = sub {
526
        die "Expected JSON object\n" unless substr($text, $i, 1) eq '{';
527
        $i++;
528
        my %out;
529
        $skip_ws->();
530
        if ($i < $len && substr($text, $i, 1) eq '}') {
531
            $i++;
532
            return \%out;
533
        }
534
        while (1) {
535
            $skip_ws->();
536
            my $key = $parse_string->();
537
            $skip_ws->();
538
            die "Expected JSON object colon\n" unless substr($text, $i++, 1) eq ':';
539
            $out{$key} = $parse_value->();
540
            $skip_ws->();
541
            my $ch = substr($text, $i++, 1);
542
            last if $ch eq '}';
543
            die "Expected JSON object comma\n" unless $ch eq ',';
544
        }
545
        return \%out;
546
    };
547

            
548
    $parse_value = sub {
549
        $skip_ws->();
550
        die "Unexpected end of JSON\n" if $i >= $len;
551
        my $ch = substr($text, $i, 1);
552
        return $parse_string->() if $ch eq '"';
553
        return $parse_object->() if $ch eq '{';
554
        return $parse_array->() if $ch eq '[';
555
        if (substr($text, $i, 4) eq 'true') {
556
            $i += 4;
557
            return json_bool(1);
558
        }
559
        if (substr($text, $i, 5) eq 'false') {
560
            $i += 5;
561
            return json_bool(0);
562
        }
563
        if (substr($text, $i, 4) eq 'null') {
564
            $i += 4;
565
            return undef;
566
        }
567
        return $parse_number->() if $ch =~ /[-0-9]/;
568
        die "Unexpected JSON token\n";
569
    };
570

            
571
    my $value = $parse_value->();
572
    $skip_ws->();
573
    die "Trailing JSON content\n" if $i != $len;
574
    return $value;
575
}
576

            
577
sub parse_params {
578
    my ($text) = @_;
579
    my %out;
580
    for my $pair (split /&/, $text) {
581
        next unless length $pair;
582
        my ($k, $v) = split /=/, $pair, 2;
583
        $out{url_decode($k)} = url_decode($v || '');
584
    }
585
    return %out;
586
}
587

            
588
sub clean_id {
589
    my ($value) = @_;
590
    $value = lc clean_scalar($value);
591
    $value =~ s/[^a-z0-9_.-]+/-/g;
592
    $value =~ s/^-+|-+$//g;
593
    return $value;
594
}
595

            
596
sub clean_scalar {
597
    my ($value) = @_;
598
    $value = '' unless defined $value;
599
    $value =~ s/[\r\n\t]+/ /g;
600
    $value =~ s/^\s+|\s+$//g;
601
    return $value;
602
}
603

            
604
sub clean_list {
605
    my ($value) = @_;
606
    return () unless defined $value;
607
    my @items = ref($value) eq 'ARRAY' ? @$value : split /[\s,]+/, $value;
608
    my @clean;
609
    for my $item (@items) {
610
        $item = clean_scalar($item);
611
        push @clean, $item if length $item;
612
    }
613
    return @clean;
614
}
615

            
616
sub yq {
617
    my ($value) = @_;
618
    $value = '' unless defined $value;
619
    $value =~ s/\\/\\\\/g;
620
    $value =~ s/"/\\"/g;
621
    return qq("$value");
622
}
623

            
624
sub yaml_unquote {
625
    my ($value) = @_;
626
    $value = '' unless defined $value;
627
    $value =~ s/^\s+|\s+$//g;
628
    if ($value =~ /^"(.*)"$/) {
629
        $value = $1;
630
        $value =~ s/\\"/"/g;
631
        $value =~ s/\\\\/\\/g;
632
    }
633
    return $value;
634
}
635

            
636
sub verify_totp {
637
    my ($secret, $otp) = @_;
638
    return 0 unless $secret && $otp =~ /^\d{6}$/;
639
    my $key = eval { base32_decode($secret) };
640
    return 0 if $@ || !length $key;
641
    my $counter = int(time() / 30);
642
    for my $offset (-1, 0, 1) {
643
        return 1 if totp_code($key, $counter + $offset) eq $otp;
644
    }
645
    return 0;
646
}
647

            
648
sub totp_code {
649
    my ($key, $counter) = @_;
650
    my $msg = pack('NN', int($counter / 4294967296), $counter & 0xffffffff);
651
    my $hash = hmac_sha1($msg, $key);
652
    my $offset = ord(substr($hash, -1)) & 0x0f;
653
    my $bin = unpack('N', substr($hash, $offset, 4)) & 0x7fffffff;
654
    return sprintf('%06d', $bin % 1_000_000);
655
}
656

            
657
sub base32_decode {
658
    my ($text) = @_;
659
    $text = uc($text || '');
660
    $text =~ s/[^A-Z2-7]//g;
661
    my %map;
662
    my @chars = ('A'..'Z', '2'..'7');
663
    @map{@chars} = (0..31);
664
    my ($bits, $value, $out) = (0, 0, '');
665
    for my $char (split //, $text) {
666
        die "Invalid base32\n" unless exists $map{$char};
667
        $value = ($value << 5) | $map{$char};
668
        $bits += 5;
669
        while ($bits >= 8) {
670
            $bits -= 8;
671
            $out .= chr(($value >> $bits) & 0xff);
672
        }
673
    }
674
    return $out;
675
}
676

            
677
sub create_session {
678
    my $nonce = random_hex(24);
679
    my $expires = int(time() + 8 * 3600);
680
    my $sig = hmac_sha256_hex("$nonce:$expires", $session_secret);
681
    my $token = "$nonce:$expires:$sig";
682
    $sessions{$token} = $expires;
683
    return $token;
684
}
685

            
686
sub is_authenticated {
687
    my ($headers) = @_;
688
    my $token = cookie_value($headers->{'cookie'} || '', 'hm_session');
689
    return 0 unless $token;
690
    my ($nonce, $expires, $sig) = split /:/, $token;
691
    return 0 unless $nonce && $expires && $sig;
692
    return 0 if $expires < time();
693
    return 0 unless hmac_sha256_hex("$nonce:$expires", $session_secret) eq $sig;
694
    return exists $sessions{$token};
695
}
696

            
697
sub expire_session {
698
    my ($headers) = @_;
699
    my $token = cookie_value($headers->{'cookie'} || '', 'hm_session');
700
    delete $sessions{$token} if $token;
701
}
702

            
703
sub cookie_value {
704
    my ($cookie, $name) = @_;
705
    for my $part (split /;\s*/, $cookie) {
706
        my ($k, $v) = split /=/, $part, 2;
707
        return $v if defined $k && $k eq $name;
708
    }
709
    return '';
710
}
711

            
712
sub send_json {
713
    my ($client, $status, $payload, $extra_headers) = @_;
714
    return send_response($client, $status, json_encode($payload), 'application/json; charset=utf-8', $extra_headers);
715
}
716

            
717
sub send_html {
718
    my ($client, $status, $html) = @_;
719
    return send_response($client, $status, $html, 'text/html; charset=utf-8');
720
}
721

            
722
sub send_text {
723
    my ($client, $status, $text) = @_;
724
    return send_response($client, $status, $text, 'text/plain; charset=utf-8');
725
}
726

            
727
sub send_download {
728
    my ($client, $status, $content, $type, $filename) = @_;
729
    return send_response($client, $status, $content, $type, [ qq(Content-Disposition: attachment; filename="$filename") ]);
730
}
731

            
732
sub send_file {
733
    my ($client, $path, $type, $filename) = @_;
734
    return send_json($client, 404, { error => 'missing_file' }) unless -f $path;
735
    return send_download($client, 200, read_file($path), $type, $filename);
736
}
737

            
738
sub send_response {
739
    my ($client, $status, $body, $type, $extra_headers) = @_;
740
    my %reason = (200 => 'OK', 400 => 'Bad Request', 401 => 'Unauthorized', 404 => 'Not Found', 500 => 'Internal Server Error', 503 => 'Service Unavailable');
741
    $body = '' unless defined $body;
742
    print $client "HTTP/1.1 $status " . ($reason{$status} || 'OK') . "\r\n";
743
    print $client "Content-Type: $type\r\n";
744
    print $client "Content-Length: " . length($body) . "\r\n";
745
    print $client "Cache-Control: no-store\r\n";
746
    print $client "$_\r\n" for @{ $extra_headers || [] };
747
    print $client "Connection: close\r\n\r\n";
748
    print $client $body;
749
}
750

            
751
sub read_file {
752
    my ($path) = @_;
753
    open my $fh, '<', $path or die "Cannot read $path: $!";
754
    local $/;
755
    return <$fh>;
756
}
757

            
758
sub write_file {
759
    my ($path, $content) = @_;
760
    open my $fh, '>', $path or die "Cannot write $path: $!";
761
    print {$fh} $content;
762
    close $fh or die "Cannot close $path: $!";
763
}
764

            
765
sub backup_file {
766
    my ($path) = @_;
767
    return unless -f $path;
768
    my $backup_dir = "$project_dir/backups/host-manager";
769
    make_path($backup_dir) unless -d $backup_dir;
770
    my $name = $path;
771
    $name =~ s{.*/}{};
772
    my $stamp = strftime('%Y%m%d_%H%M%S', localtime);
773
    write_file("$backup_dir/$name.$stamp.bak", read_file($path));
774
}
775

            
776
sub url_decode {
777
    my ($value) = @_;
778
    $value = '' unless defined $value;
779
    $value =~ tr/+/ /;
780
    $value =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
781
    return $value;
782
}
783

            
784
sub random_hex {
785
    my ($bytes) = @_;
786
    if (open my $fh, '<:raw', '/dev/urandom') {
787
        read($fh, my $raw, $bytes);
788
        close $fh;
789
        return unpack('H*', $raw);
790
    }
791
    return sha256_hex(rand() . time() . $$);
792
}
793

            
794
sub iso_now {
795
    return strftime('%Y-%m-%dT%H:%M:%SZ', gmtime);
796
}
797

            
798
sub app_html {
799
    return <<'HTML';
800
<!doctype html>
801
<html lang="ro">
802
<head>
803
  <meta charset="utf-8">
804
  <meta name="viewport" content="width=device-width, initial-scale=1">
805
  <title>Host Manager</title>
806
  <style>
807
    :root {
808
      color-scheme: light;
809
      --ink: #152033;
810
      --muted: #647084;
811
      --line: #d8dee8;
812
      --soft: #f4f6f9;
813
      --panel: #ffffff;
814
      --accent: #1267d8;
815
      --bad: #b42318;
816
      --warn: #946200;
817
      --ok: #137333;
818
    }
819
    * { box-sizing: border-box; }
820
    body { margin: 0; font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color: var(--ink); background: #eef2f6; font-size: 14px; }
821
    header { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 18px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 2; }
822
    h1 { margin: 0; font-size: 18px; font-weight: 700; letter-spacing: 0; }
823
    main { padding: 16px; display: grid; gap: 16px; max-width: 1280px; margin: 0 auto; }
824
    .toolbar, .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
825
    .toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px; }
826
    .panel { overflow: hidden; }
827
    .panel-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); background: #fafbfc; }
828
    .panel-head h2 { margin: 0; font-size: 14px; }
829
    .stats { display: flex; gap: 8px; flex-wrap: wrap; }
830
    .stat { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--soft); font-size: 12px; color: var(--muted); }
831
    button, input, select, textarea { font: inherit; }
832
    button, .linkbtn { border: 1px solid var(--line); background: #fff; color: var(--ink); border-radius: 6px; padding: 7px 10px; min-height: 34px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
833
    button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
834
    button.danger { color: var(--bad); }
Xdev Host Manager authored 2 days ago
835
    button:disabled, .linkbtn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; pointer-events: none; }
Xdev Host Manager authored 2 days ago
836
    input, select, textarea { width: 100%; border: 1px solid var(--line); border-radius: 6px; padding: 8px; background: #fff; color: var(--ink); }
837
    textarea { min-height: 74px; resize: vertical; }
838
    table { width: 100%; border-collapse: collapse; table-layout: fixed; }
839
    th, td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; overflow-wrap: anywhere; }
840
    th { color: var(--muted); font-size: 12px; font-weight: 700; background: #fafbfc; }
841
    tr:hover td { background: #f8fafc; }
842
    .pill { display: inline-block; padding: 2px 6px; border-radius: 999px; background: var(--soft); border: 1px solid var(--line); color: var(--muted); font-size: 12px; margin: 0 4px 4px 0; }
843
    .pill.ok { color: var(--ok); border-color: #b7dfc1; background: #edf8ef; }
844
    .pill.warn { color: var(--warn); border-color: #f1d184; background: #fff7df; }
845
    .pill.bad { color: var(--bad); border-color: #f0b8b3; background: #fff0ee; }
846
    .grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; padding: 14px; }
847
    .span2 { grid-column: 1 / -1; }
848
    label { display: grid; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 650; }
849
    .auth { display: flex; gap: 8px; align-items: center; }
850
    .auth input { width: 130px; }
851
    .muted { color: var(--muted); }
852
    .problems { padding: 10px 14px; display: grid; gap: 8px; }
853
    .problem { border-left: 3px solid var(--warn); padding: 7px 9px; background: #fffaf0; }
854
    @media (max-width: 760px) {
855
      header { align-items: stretch; flex-direction: column; }
856
      .grid { grid-template-columns: 1fr; }
857
      table { min-width: 760px; }
858
      .table-wrap { overflow-x: auto; }
859
    }
860
  </style>
861
</head>
862
<body>
863
  <header>
864
    <h1>Host Manager</h1>
865
    <form class="auth" id="login-form">
Xdev Host Manager authored 2 days ago
866
      <span id="auth-state" class="muted">locked</span>
Xdev Host Manager authored 2 days ago
867
      <input id="otp" name="otp" inputmode="numeric" pattern="[0-9]*" autocomplete="one-time-code" placeholder="OTP">
868
      <button class="primary" type="submit">Login</button>
869
      <button type="button" id="logout">Logout</button>
870
    </form>
871
  </header>
872
  <main>
873
    <section class="toolbar">
Xdev Host Manager authored 2 days ago
874
      <button id="refresh" data-auth-required>Refresh</button>
875
      <a class="linkbtn" data-auth-required href="/download/hosts.yaml">hosts.yaml</a>
876
      <a class="linkbtn" data-auth-required href="/download/local-hosts.tsv">local-hosts.tsv</a>
877
      <a class="linkbtn" data-auth-required href="/download/monitoring.json">monitoring.json</a>
878
      <button id="write-tsv" data-auth-required>Write local-hosts.tsv</button>
Xdev Host Manager authored 2 days ago
879
      <span id="message" class="muted"></span>
880
    </section>
881

            
882
    <section class="panel">
883
      <div class="panel-head">
884
        <h2>Overview</h2>
885
        <div class="stats" id="stats"></div>
886
      </div>
887
      <div class="problems" id="problems"></div>
888
    </section>
889

            
890
    <section class="panel">
891
      <div class="panel-head">
892
        <h2>Hosts</h2>
893
        <input id="filter" placeholder="filter" style="max-width: 240px">
894
      </div>
895
      <div class="table-wrap">
896
        <table>
897
          <thead>
898
            <tr>
899
              <th style="width: 120px">ID</th>
900
              <th style="width: 130px">hosts_ip</th>
901
              <th style="width: 130px">dns_ip</th>
902
              <th>Names</th>
903
              <th style="width: 150px">Roles</th>
904
              <th style="width: 110px">Monitoring</th>
905
              <th style="width: 90px">Status</th>
906
            </tr>
907
          </thead>
908
          <tbody id="hosts"></tbody>
909
        </table>
910
      </div>
911
    </section>
912

            
913
    <section class="panel">
914
      <div class="panel-head">
915
        <h2>Edit host</h2>
916
        <span class="muted">write access requires OTP</span>
917
      </div>
918
      <form id="host-form" class="grid">
919
        <label>ID<input name="id" required></label>
920
        <label>Status<select name="status"><option>active</option><option>planned</option><option>retired</option></select></label>
921
        <label>hosts_ip<input name="hosts_ip" required></label>
922
        <label>dns_ip<input name="dns_ip" required></label>
923
        <label class="span2">Names<textarea name="names" required></textarea></label>
924
        <label>Roles<input name="roles"></label>
925
        <label>Sources<input name="sources"></label>
926
        <label>Monitoring<select name="monitoring"><option>pending</option><option>enabled</option><option>disabled</option></select></label>
927
        <label>Notes<input name="notes"></label>
928
        <div class="span2">
929
          <button class="primary" type="submit">Save host</button>
930
          <button class="danger" type="button" id="delete-host">Delete host</button>
931
        </div>
932
      </form>
933
    </section>
934
  </main>
935
  <script>
936
    let state = { hosts: [], problems: [], authenticated: false };
937

            
938
    const $ = (id) => document.getElementById(id);
939
    const msg = (text) => { $('message').textContent = text || ''; };
940

            
941
    async function api(path, options = {}) {
942
      const res = await fetch(path, options);
943
      const body = await res.json();
944
      if (!res.ok) throw new Error(body.error || res.statusText);
945
      return body;
946
    }
947

            
948
    async function refresh() {
949
      const session = await api('/api/session');
950
      state.authenticated = session.authenticated;
Xdev Host Manager authored 2 days ago
951
      updateAuthUi();
952
      if (!state.authenticated) {
953
        state.hosts = [];
954
        state.problems = [];
955
        renderLocked();
956
        return;
957
      }
Xdev Host Manager authored 2 days ago
958
      const data = await api('/api/hosts');
959
      state.hosts = data.hosts || [];
960
      state.problems = data.problems || [];
961
      render(data);
962
    }
963

            
Xdev Host Manager authored 2 days ago
964
    function updateAuthUi() {
965
      $('auth-state').textContent = state.authenticated ? 'authenticated' : 'locked';
966
      document.querySelectorAll('[data-auth-required]').forEach(el => {
967
        if (el.tagName === 'A') {
968
          el.setAttribute('aria-disabled', state.authenticated ? 'false' : 'true');
969
        } else {
970
          el.disabled = !state.authenticated;
971
        }
972
      });
973
    }
974

            
975
    function renderLocked() {
976
      $('stats').innerHTML = '<span class="stat">authentication required</span>';
977
      $('problems').innerHTML = '<div class="muted" style="padding: 8px 0">Login with OTP to access host data, downloads, and configuration actions.</div>';
978
      $('hosts').innerHTML = '';
979
    }
980

            
Xdev Host Manager authored 2 days ago
981
    function render(data) {
982
      $('stats').innerHTML = [
983
        ['hosts', data.counts.hosts],
984
        ['problems', data.counts.problems],
985
        ['updated', data.updated_at || 'unknown']
986
      ].map(([k, v]) => `<span class="stat">${k}: ${escapeHtml(String(v))}</span>`).join('');
987

            
988
      $('problems').innerHTML = state.problems.length
989
        ? state.problems.map(p => `<div class="problem"><strong>${escapeHtml(p.host_id)}</strong> ${escapeHtml(p.code)}: ${escapeHtml(p.message)}</div>`).join('')
990
        : '<div class="muted" style="padding: 8px 0">No registry problems detected.</div>';
991

            
992
      renderHosts();
993
    }
994

            
995
    function renderHosts() {
996
      const filter = $('filter').value.toLowerCase();
997
      $('hosts').innerHTML = state.hosts
998
        .filter(h => JSON.stringify(h).toLowerCase().includes(filter))
999
        .map(h => {
1000
          const problems = state.problems.filter(p => p.host_id === h.id);
1001
          const cls = problems.length ? 'warn' : 'ok';
1002
          return `<tr data-id="${escapeHtml(h.id)}">
1003
            <td><button type="button" data-edit="${escapeHtml(h.id)}">${escapeHtml(h.id)}</button></td>
1004
            <td>${escapeHtml(h.hosts_ip || '')}</td>
1005
            <td>${escapeHtml(h.dns_ip || '')}</td>
1006
            <td>${(h.names || []).map(n => `<span class="pill">${escapeHtml(n)}</span>`).join('')}</td>
1007
            <td>${(h.roles || []).map(n => `<span class="pill">${escapeHtml(n)}</span>`).join('')}</td>
1008
            <td><span class="pill ${cls}">${escapeHtml(h.monitoring || '')}</span></td>
1009
            <td>${escapeHtml(h.status || '')}</td>
1010
          </tr>`;
1011
        }).join('');
1012
      document.querySelectorAll('[data-edit]').forEach(button => button.addEventListener('click', () => editHost(button.dataset.edit)));
1013
    }
1014

            
1015
    function editHost(id) {
1016
      const host = state.hosts.find(h => h.id === id);
1017
      if (!host) return;
1018
      const form = $('host-form');
1019
      for (const key of ['id', 'status', 'hosts_ip', 'dns_ip', 'monitoring', 'notes']) form.elements[key].value = host[key] || '';
1020
      form.elements.names.value = (host.names || []).join('\n');
1021
      form.elements.roles.value = (host.roles || []).join(' ');
1022
      form.elements.sources.value = (host.sources || []).join(' ');
1023
      window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
1024
    }
1025

            
1026
    function formObject(form) {
1027
      return Object.fromEntries(new FormData(form).entries());
1028
    }
1029

            
1030
    function escapeHtml(value) {
1031
      return value.replace(/[&<>"']/g, ch => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#039;'}[ch]));
1032
    }
1033

            
1034
    $('refresh').addEventListener('click', () => refresh().catch(e => msg(e.message)));
1035
    $('filter').addEventListener('input', renderHosts);
1036

            
1037
    $('login-form').addEventListener('submit', async (event) => {
1038
      event.preventDefault();
1039
      try {
1040
        await api('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ otp: $('otp').value }) });
1041
        $('otp').value = '';
1042
        msg('authenticated');
1043
        await refresh();
1044
      } catch (e) { msg(e.message); }
1045
    });
1046

            
1047
    $('logout').addEventListener('click', async () => {
1048
      await api('/api/logout', { method: 'POST' }).catch(() => {});
1049
      msg('logged out');
1050
      await refresh();
1051
    });
1052

            
1053
    $('host-form').addEventListener('submit', async (event) => {
1054
      event.preventDefault();
1055
      try {
1056
        await api('/api/hosts/upsert', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formObject(event.target)) });
1057
        msg('host saved');
1058
        await refresh();
1059
      } catch (e) { msg(e.message); }
1060
    });
1061

            
1062
    $('delete-host').addEventListener('click', async () => {
1063
      const id = $('host-form').elements.id.value;
1064
      if (!id || !confirm(`Delete ${id}?`)) return;
1065
      try {
1066
        await api('/api/hosts/delete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id }) });
1067
        $('host-form').reset();
1068
        msg('host deleted');
1069
        await refresh();
1070
      } catch (e) { msg(e.message); }
1071
    });
1072

            
1073
    $('write-tsv').addEventListener('click', async () => {
1074
      if (!confirm('Write config/local-hosts.tsv from hosts.yaml?')) return;
1075
      try {
1076
        await api('/api/render/local-hosts-tsv', { method: 'POST' });
1077
        msg('local-hosts.tsv written');
1078
      } catch (e) { msg(e.message); }
1079
    });
1080

            
1081
    refresh().catch(e => msg(e.message));
1082
  </script>
1083
</body>
1084
</html>
1085
HTML
1086
}