]> git.openstreetmap.org Git - dns.git/blobdiff - bin/mksshfp
Move FR to fastly
[dns.git] / bin / mksshfp
index e4bc8ed30c84f293c680e46daaba43bac18a3a8e..0e0027c3a20c388a05f2373c54a8c11a002a28c2 100755 (executable)
@@ -1,32 +1,80 @@
 #!/usr/bin/perl
 
+use strict;
+use warnings;
 
-open(SSHFP_JS, ">", "include/sshfp.js") || die $!;
+use Digest::SHA qw(sha256_hex);
+use MIME::Base64;
 
-print SSHFP_JS qq|var SSHFP_RECORDS = [\n|;
+my %algorithms = (
+    "ssh-rsa" => "1",
+    "ssh-dss" => "2",
+    "ecdsa-sha2-nistp256" => "3",
+    "ssh-ed25519" => "4"
+);
+
+my %hosts;
 
 if (-f "/etc/ssh/ssh_known_hosts")
 {
-    open(SSHFP, "-|","sshfp", "-k", "/etc/ssh/ssh_known_hosts") || die $!;
+    open(HOSTS, "<", "/etc/ssh/ssh_known_hosts") || die $!;
 
-    while (my $line = <SSHFP>)
+    while (my $line = <HOSTS>)
     {
-        if ($line =~ /^(\S+) IN SSHFP (\d+) (\d+) ([0-9A-F]+)$/)
+        if ($line =~ /^([^, ]+)\S* (\S+) (\S+)$/)
         {
             my $host = $1;
-            my $algorithm = $2;
-            my $type = $3;
-            my $value = $4;
+            my $algorithm = $algorithms{$2};
+            my $value = uc(sha256_hex(decode_base64($3)));
 
-            print SSHFP_JS qq|  SSHFP("${host}", ${algorithm}, ${type}, "${value}"),\n|;
-        }
-        else
-        {
-            warn $line;
+            $host =~ s/\.openstreetmap\.org$//;
+        
+            if ($algorithm ne "2")
+            {
+                my $wanted = 0;
+
+                if (exists($hosts{$host}))
+                {
+                    if ($algorithm eq "3")
+                    {
+                        $wanted = 1;
+                    }
+                    elsif ($algorithm eq "4" && $hosts{$host}->{algorithm} ne "3")
+                    {
+                        $wanted = 1;
+                    }
+                }
+                else
+                {
+                    $wanted = 1;
+                }
+
+                if ($wanted)
+                {
+                    $hosts{$host} = {
+                        algorithm => $algorithm,
+                        type => "2",
+                        value => $value
+                    };
+                }
+            }
         }
     }
 
-    close(SSHFP);
+    close(HOSTS);
+}
+
+open(SSHFP_JS, ">", "include/sshfp.js") || die $!;
+
+print SSHFP_JS qq|var SSHFP_RECORDS = [\n|;
+
+foreach my $host (sort keys %hosts)
+{
+    my $algorithm = $hosts{$host}->{algorithm};
+    my $type = $hosts{$host}->{type};
+    my $value = $hosts{$host}->{value};
+
+    print SSHFP_JS qq|  SSHFP("${host}", ${algorithm}, ${type}, "${value}"),\n|;
 }
 
 print SSHFP_JS qq|];\n|;