]> git.openstreetmap.org Git - dns.git/blob - bin/mksshfp
Calculate SSHFP records directly instead of using sshfp
[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 %algorithms = (
10     "ssh-rsa" => "1",
11     "ssh-dss" => "2",
12     "ecdsa-sha2-nistp256" => "3",
13     "ssh-ed25519" => "4"
14 );
15
16 my %hosts;
17
18 if (-f "/etc/ssh/ssh_known_hosts")
19 {
20     open(HOSTS, "<", "/etc/ssh/ssh_known_hosts") || die $!;
21
22     while (my $line = <HOSTS>)
23     {
24         if ($line =~ /^([^, ]+)\S* (\S+) (\S+)$/)
25         {
26             my $host = $1;
27             my $algorithm = $algorithms{$2};
28             my $value = uc(sha256_hex(decode_base64($3)));
29
30             $host =~ s/\.openstreetmap\.org$//;
31         
32             if ($algorithm ne "2")
33             {
34                 my $wanted = 0;
35
36                 if (exists($hosts{$host}))
37                 {
38                     if ($algorithm eq "3")
39                     {
40                         $wanted = 1;
41                     }
42                     elsif ($algorithm eq "4" && $hosts{$host}->{algorithm} ne "3")
43                     {
44                         $wanted = 1;
45                     }
46                 }
47                 else
48                 {
49                     $wanted = 1;
50                 }
51
52                 if ($wanted)
53                 {
54                     $hosts{$host} = {
55                         algorithm => $algorithm,
56                         type => "2",
57                         value => $value
58                     };
59                 }
60             }
61         }
62     }
63
64     close(HOSTS);
65 }
66
67 open(SSHFP_JS, ">", "include/sshfp.js") || die $!;
68
69 print SSHFP_JS qq|var SSHFP_RECORDS = [\n|;
70
71 foreach my $host (sort keys %hosts)
72 {
73     my $algorithm = $hosts{$host}->{algorithm};
74     my $type = $hosts{$host}->{type};
75     my $value = $hosts{$host}->{value};
76
77     print SSHFP_JS qq|  SSHFP("${host}", ${algorithm}, ${type}, "${value}"),\n|;
78 }
79
80 print SSHFP_JS qq|];\n|;
81
82 close(SSHFP_JS);
83
84 exit 0;