1717package main
1818
1919import (
20+ "bufio"
21+ "bytes"
2022 "errors"
2123 "fmt"
2224 "io/ioutil"
@@ -37,25 +39,36 @@ import (
3739type sshClient struct {
3840 server string // Server name or IP without port number
3941 address string // IP address of the remote server
42+ pubkey []byte // RSA public key to authenticate the server
4043 client * ssh.Client
4144 logger log.Logger
4245}
4346
4447// dial establishes an SSH connection to a remote node using the current user and
45- // the user's configured private RSA key.
46- func dial (server string ) (* sshClient , error ) {
48+ // the user's configured private RSA key. If that fails, password authentication
49+ // is fallen back to. The caller may override the login user via user@server:port.
50+ func dial (server string , pubkey []byte ) (* sshClient , error ) {
4751 // Figure out a label for the server and a logger
4852 label := server
4953 if strings .Contains (label , ":" ) {
5054 label = label [:strings .Index (label , ":" )]
5155 }
56+ login := ""
57+ if strings .Contains (server , "@" ) {
58+ login = label [:strings .Index (label , "@" )]
59+ label = label [strings .Index (label , "@" )+ 1 :]
60+ server = server [strings .Index (server , "@" )+ 1 :]
61+ }
5262 logger := log .New ("server" , label )
5363 logger .Debug ("Attempting to establish SSH connection" )
5464
5565 user , err := user .Current ()
5666 if err != nil {
5767 return nil , err
5868 }
69+ if login == "" {
70+ login = user .Username
71+ }
5972 // Configure the supported authentication methods (private key and password)
6073 var auths []ssh.AuthMethod
6174
@@ -71,7 +84,7 @@ func dial(server string) (*sshClient, error) {
7184 }
7285 }
7386 auths = append (auths , ssh .PasswordCallback (func () (string , error ) {
74- fmt .Printf ("What's the login password for %s at %s? (won't be echoed)\n > " , user . Username , server )
87+ fmt .Printf ("What's the login password for %s at %s? (won't be echoed)\n > " , login , server )
7588 blob , err := terminal .ReadPassword (int (syscall .Stdin ))
7689
7790 fmt .Println ()
@@ -86,18 +99,44 @@ func dial(server string) (*sshClient, error) {
8699 return nil , errors .New ("no IPs associated with domain" )
87100 }
88101 // Try to dial in to the remote server
89- logger .Trace ("Dialing remote SSH server" , "user" , user . Username , "key" , path )
102+ logger .Trace ("Dialing remote SSH server" , "user" , login )
90103 if ! strings .Contains (server , ":" ) {
91104 server += ":22"
92105 }
93- client , err := ssh .Dial ("tcp" , server , & ssh.ClientConfig {User : user .Username , Auth : auths })
106+ keycheck := func (hostname string , remote net.Addr , key ssh.PublicKey ) error {
107+ // If no public key is known for SSH, ask the user to confirm
108+ if pubkey == nil {
109+ fmt .Printf ("The authenticity of host '%s (%s)' can't be established.\n " , hostname , remote )
110+ fmt .Printf ("SSH key fingerprint is %s [MD5]\n " , ssh .FingerprintLegacyMD5 (key ))
111+ fmt .Printf ("Are you sure you want to continue connecting (yes/no)? " )
112+
113+ text , err := bufio .NewReader (os .Stdin ).ReadString ('\n' )
114+ switch {
115+ case err != nil :
116+ return err
117+ case strings .TrimSpace (text ) == "yes" :
118+ pubkey = key .Marshal ()
119+ return nil
120+ default :
121+ return fmt .Errorf ("unknown auth choice: %v" , text )
122+ }
123+ }
124+ // If a public key exists for this SSH server, check that it matches
125+ if bytes .Compare (pubkey , key .Marshal ()) == 0 {
126+ return nil
127+ }
128+ // We have a mismatch, forbid connecting
129+ return errors .New ("ssh key mismatch, readd the machine to update" )
130+ }
131+ client , err := ssh .Dial ("tcp" , server , & ssh.ClientConfig {User : login , Auth : auths , HostKeyCallback : keycheck })
94132 if err != nil {
95133 return nil , err
96134 }
97135 // Connection established, return our utility wrapper
98136 c := & sshClient {
99137 server : label ,
100138 address : addr [0 ],
139+ pubkey : pubkey ,
101140 client : client ,
102141 logger : logger ,
103142 }
0 commit comments