Skip to content

Commit 2427df5

Browse files
committed
Use host key checking outside localhost
Verify ssh host keys, when connecting to a remote server. The first connection will prompt, if not in known_hosts. Signed-off-by: Anders F Björklund <[email protected]>
1 parent 150332a commit 2427df5

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

cmd/limactl/copy.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
5252
if err != nil {
5353
return err
5454
}
55+
instAddr := "127.0.0.1"
5556
instDirs := make(map[string]string)
5657
scpFlags := []string{}
5758
scpArgs := []string{}
@@ -69,6 +70,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
6970
if sshutil.DetectOpenSSHVersion().LessThan(*semver.New("8.0.0")) {
7071
legacySSH = true
7172
}
73+
localhostOnly := true
7274
for _, arg := range args {
7375
path := strings.Split(arg, ":")
7476
switch len(path) {
@@ -92,6 +94,10 @@ func copyAction(cmd *cobra.Command, args []string) error {
9294
} else {
9395
scpArgs = append(scpArgs, fmt.Sprintf("scp://%s@%s:%d/%s", u.Username, inst.SSHAddress, inst.SSHLocalPort, path[1]))
9496
}
97+
if inst.SSHAddress != "127.0.0.1" {
98+
instAddr = inst.SSHAddress
99+
localhostOnly = false
100+
}
95101
instDirs[instName] = inst.Dir
96102
default:
97103
return fmt.Errorf("path %q contains multiple colons", arg)
@@ -109,14 +115,14 @@ func copyAction(cmd *cobra.Command, args []string) error {
109115
// arguments such as ControlPath. This is preferred as we can multiplex
110116
// sessions without re-authenticating (MaxSessions permitting).
111117
for _, instDir := range instDirs {
112-
sshOpts, err = sshutil.SSHOpts(instDir, false, false, false, false)
118+
sshOpts, err = sshutil.SSHOpts(instDir, false, instAddr, false, false, false)
113119
if err != nil {
114120
return err
115121
}
116122
}
117123
} else {
118124
// Copying among multiple hosts; we can't pass in host-specific options.
119-
sshOpts, err = sshutil.CommonOpts(false)
125+
sshOpts, err = sshutil.CommonOpts(false, localhostOnly)
120126
if err != nil {
121127
return err
122128
}

cmd/limactl/shell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
169169
}
170170
}
171171

172-
sshOpts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
172+
sshOpts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.Address, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
173173
if err != nil {
174174
return err
175175
}

cmd/limactl/show-ssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
9292
if err != nil {
9393
return err
9494
}
95-
opts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
95+
opts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.Address, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
9696
if err != nil {
9797
return err
9898
}

pkg/hostagent/hostagent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
140140
return nil, err
141141
}
142142

143-
sshOpts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
143+
sshOpts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.Address, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
144144
if err != nil {
145145
return nil, err
146146
}

pkg/sshutil/sshutil.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ var sshInfo struct {
126126
//
127127
// The result always contains the IdentityFile option.
128128
// The result never contains the Port option.
129-
func CommonOpts(useDotSSH bool) ([]string, error) {
129+
func CommonOpts(useDotSSH, localhost bool) ([]string, error) {
130130
configDir, err := dirnames.LimaConfigDir()
131131
if err != nil {
132132
return nil, err
@@ -181,14 +181,20 @@ func CommonOpts(useDotSSH bool) ([]string, error) {
181181
}
182182
}
183183

184+
if localhost {
185+
opts = append(opts,
186+
"StrictHostKeyChecking=no",
187+
"UserKnownHostsFile=/dev/null",
188+
"BatchMode=yes",
189+
)
190+
}
191+
184192
opts = append(opts,
185-
"StrictHostKeyChecking=no",
186-
"UserKnownHostsFile=/dev/null",
187193
"NoHostAuthenticationForLocalhost=yes",
188194
"GSSAPIAuthentication=no",
189195
"PreferredAuthentications=publickey",
190196
"Compression=no",
191-
"BatchMode=yes",
197+
"PasswordAuthentication=no",
192198
"IdentitiesOnly=yes",
193199
)
194200

@@ -223,7 +229,7 @@ func CommonOpts(useDotSSH bool) ([]string, error) {
223229
}
224230

225231
// SSHOpts adds the following options to CommonOptions: User, ControlMaster, ControlPath, ControlPersist.
226-
func SSHOpts(instDir string, useDotSSH, forwardAgent, forwardX11, forwardX11Trusted bool) ([]string, error) {
232+
func SSHOpts(instDir string, useDotSSH bool, hostAddress string, forwardAgent, forwardX11, forwardX11Trusted bool) ([]string, error) {
227233
controlSock := filepath.Join(instDir, filenames.SSHSock)
228234
if len(controlSock) >= osutil.UnixPathMax {
229235
return nil, fmt.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax)
@@ -232,7 +238,7 @@ func SSHOpts(instDir string, useDotSSH, forwardAgent, forwardX11, forwardX11Trus
232238
if err != nil {
233239
return nil, err
234240
}
235-
opts, err := CommonOpts(useDotSSH)
241+
opts, err := CommonOpts(useDotSSH, hostAddress == "127.0.0.1")
236242
if err != nil {
237243
return nil, err
238244
}

0 commit comments

Comments
 (0)