File 3050-ssh-Redesigned-ssh_file-add_host_key.patch of Package erlang
From a5bc410a257bdad9a686100fae7146daad0efa30 Mon Sep 17 00:00:00 2001
From: Hans Nilsson <hans@erlang.org>
Date: Fri, 21 Feb 2020 11:45:01 +0100
Subject: [PATCH 10/10] ssh: Redesigned ssh_file:add_host_key
---
lib/ssh/src/ssh_file.erl | 52 ++++++++++++++++++++++++++----------------------
1 file changed, 28 insertions(+), 24 deletions(-)
diff --git a/lib/ssh/src/ssh_file.erl b/lib/ssh/src/ssh_file.erl
index 14933023a3..fb41aa9594 100644
--- a/lib/ssh/src/ssh_file.erl
+++ b/lib/ssh/src/ssh_file.erl
@@ -86,7 +86,12 @@ add_host_key(Host, Key, Opts) ->
case file:open(KnownHosts, [write,append]) of
{ok, Fd} ->
ok = file:change_mode(KnownHosts, 8#644),
- SshBin = public_key:ssh_encode([{Key, [{hostnames, [Host1]}]}], known_hosts),
+ KeyType = erlang:atom_to_binary(ssh_transport:public_algo(Key), latin1),
+ EncKey = ssh_message:ssh2_pubkey_encode(Key),
+ SshBin =
+ iolist_to_binary([Host1, " ",
+ KeyType," ",base64:encode(iolist_to_binary(EncKey)),
+ "\n"]),
Res = file:write(Fd, SshBin),
file:close(Fd),
Res;
@@ -94,7 +99,6 @@ add_host_key(Host, Key, Opts) ->
Error
end.
-
%%%================================================================
%%%
%%% Local functions
@@ -139,6 +143,28 @@ decode_key(Base64EncodedKey) ->
%%%---------------- CLIENT FUNCTIONS ------------------------------
+%%%--------------------------------
+%% in: "host" out: "host,1.2.3.4.
+add_ip(IP) when is_tuple(IP) ->
+ ssh_connection:encode_ip(IP);
+add_ip(Host) ->
+ case inet:getaddr(Host, inet) of
+ {ok, Addr} ->
+ case ssh_connection:encode_ip(Addr) of
+ false -> Host;
+ Host -> Host;
+ IPString -> Host ++ "," ++ IPString
+ end;
+ _ -> Host
+ end.
+
+replace_localhost("localhost") ->
+ {ok, Hostname} = inet:gethostname(),
+ Hostname;
+replace_localhost(Host) ->
+ Host.
+
+%%%--------------------------------
lookup_host_keys(Host, KeyType, Key, File) ->
case file:read_file(File) of
{ok,Bin} ->
@@ -193,28 +219,6 @@ host_match1(Host, Sz, [Pat|Pats]) ->
host_match1(_, _, []) ->
false.
-
-
-%%%--------------------------------
-%% in: "host" out: "host,1.2.3.4.
-add_ip(IP) when is_tuple(IP) ->
- ssh_connection:encode_ip(IP);
-add_ip(Host) ->
- case inet:getaddr(Host, inet) of
- {ok, Addr} ->
- case ssh_connection:encode_ip(Addr) of
- false -> Host;
- IPString -> Host ++ "," ++ IPString
- end;
- _ -> Host
- end.
-
-replace_localhost("localhost") ->
- {ok, Hostname} = inet:gethostname(),
- Hostname;
-replace_localhost(Host) ->
- Host.
-
%%%---------------- COMMON FUNCTIONS ------------------------------
read_ssh_key_file(Role, PrivPub, Algorithm, Opts) ->
--
2.16.4