]> git.openstreetmap.org Git - dns.git/blob - bin/mksshfp
remove draco
[dns.git] / bin / mksshfp
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Digest::SHA qw(sha256_hex);
7 use MIME::Base64;
8
9 my %hosts;
10
11 if (-f "/etc/ssh/ssh_known_hosts")
12 {
13     open(HOSTS, "<", "/etc/ssh/ssh_known_hosts") || die $!;
14
15     while (my $line = <HOSTS>)
16     {
17         last if $line =~ /^# Manually maintained records$/;
18
19         if ($line =~ /^([^, ]+)\S* (\S+) (\S+)$/)
20         {
21             my $host = $1;
22             my $algorithm = $2;
23             my $value = uc(sha256_hex(decode_base64($3)));
24
25             $host =~ s/\.openstreetmap\.org$//;
26
27             if ($algorithm ne "2")
28             {
29                 $hosts{$host} ||= {};
30
31                 $hosts{$host}->{$algorithm} = $value;
32             }
33         }
34     }
35
36     close(HOSTS);
37 }
38
39 open(SSHFP_JS, ">", "include/sshfp.js") || die $!;
40
41 print SSHFP_JS qq|var SSHFP_RECORDS = [\n|;
42
43 foreach my $host (sort keys %hosts)
44 {
45     if ($hosts{$host}->{"ecdsa-sha2-nistp256"} || $hosts{$host}->{"ssh-ed25519"})
46     {
47         if ($hosts{$host}->{"ecdsa-sha2-nistp256"})
48         {
49             print SSHFP_JS sshfp_record($host, "3", $hosts{$host}->{"ecdsa-sha2-nistp256"});
50         }
51
52         if ($hosts{$host}->{"ssh-ed25519"})
53         {
54             print SSHFP_JS sshfp_record($host, "4", $hosts{$host}->{"ssh-ed25519"});
55         }
56     }
57     elsif ($hosts{$host}->{"ssh-rsa"})
58     {
59         print SSHFP_JS sshfp_record($host, "1", $hosts{$host}->{"ssh-rsa"});
60     }
61 }
62
63 print SSHFP_JS qq|];\n|;
64
65 close(SSHFP_JS);
66
67 exit 0;
68
69 sub sshfp_record
70 {
71     my $host = shift;
72     my $algorithm = shift;
73     my $value = shift;
74
75     return qq|  SSHFP("${host}", ${algorithm}, 2, "${value}"),\n|;
76 }