Skip to content

Commit 5b84678

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 64f4462 commit 5b84678

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

cmd/limactl/copy.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
5151
if err != nil {
5252
return err
5353
}
54+
instAddr := "127.0.0.1"
5455
instDirs := make(map[string]string)
5556
scpFlags := []string{}
5657
scpArgs := []string{}
@@ -68,6 +69,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
6869
if sshutil.DetectOpenSSHVersion().LessThan(*semver.New("8.0.0")) {
6970
legacySSH = true
7071
}
72+
localhostOnly := true
7173
for _, arg := range args {
7274
path := strings.Split(arg, ":")
7375
switch len(path) {
@@ -91,6 +93,10 @@ func copyAction(cmd *cobra.Command, args []string) error {
9193
} else {
9294
scpArgs = append(scpArgs, fmt.Sprintf("scp://%s@%s:%d/%s", u.Username, inst.SSHAddress, inst.SSHLocalPort, path[1]))
9395
}
96+
if inst.SSHAddress != "127.0.0.1" {
97+
instAddr = inst.SSHAddress
98+
localhostOnly = false
99+
}
94100
instDirs[instName] = inst.Dir
95101
default:
96102
return fmt.Errorf("path %q contains multiple colons", arg)
@@ -108,14 +114,14 @@ func copyAction(cmd *cobra.Command, args []string) error {
108114
// arguments such as ControlPath. This is preferred as we can multiplex
109115
// sessions without re-authenticating (MaxSessions permitting).
110116
for _, instDir := range instDirs {
111-
sshOpts, err = sshutil.SSHOpts(instDir, false, false, false, false)
117+
sshOpts, err = sshutil.SSHOpts(instDir, false, instAddr, false, false, false)
112118
if err != nil {
113119
return err
114120
}
115121
}
116122
} else {
117123
// Copying among multiple hosts; we can't pass in host-specific options.
118-
sshOpts, err = sshutil.CommonOpts(false)
124+
sshOpts, err = sshutil.CommonOpts(false, localhostOnly)
119125
if err != nil {
120126
return err
121127
}

cmd/limactl/shell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
168168
}
169169
}
170170

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

cmd/limactl/show_ssh.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
9191
if err != nil {
9292
return err
9393
}
94-
opts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
94+
opts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.Address, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
9595
if err != nil {
9696
return err
9797
}

pkg/hostagent/hostagent.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
131131
return nil, err
132132
}
133133

134-
sshOpts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
134+
sshOpts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.Address, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
135135
if err != nil {
136136
return nil, err
137137
}

pkg/sshutil/sshutil.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ var sshInfo struct {
125125
//
126126
// The result always contains the IdentityFile option.
127127
// The result never contains the Port option.
128-
func CommonOpts(useDotSSH bool) ([]string, error) {
128+
func CommonOpts(useDotSSH bool, localhost bool) ([]string, error) {
129129
configDir, err := dirnames.LimaConfigDir()
130130
if err != nil {
131131
return nil, err
@@ -180,14 +180,20 @@ func CommonOpts(useDotSSH bool) ([]string, error) {
180180
}
181181
}
182182

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

@@ -222,7 +228,7 @@ func CommonOpts(useDotSSH bool) ([]string, error) {
222228
}
223229

224230
// SSHOpts adds the following options to CommonOptions: User, ControlMaster, ControlPath, ControlPersist
225-
func SSHOpts(instDir string, useDotSSH, forwardAgent bool, forwardX11 bool, forwardX11Trusted bool) ([]string, error) {
231+
func SSHOpts(instDir string, useDotSSH bool, hostAddress string, forwardAgent bool, forwardX11 bool, forwardX11Trusted bool) ([]string, error) {
226232
controlSock := filepath.Join(instDir, filenames.SSHSock)
227233
if len(controlSock) >= osutil.UnixPathMax {
228234
return nil, fmt.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax)
@@ -231,7 +237,7 @@ func SSHOpts(instDir string, useDotSSH, forwardAgent bool, forwardX11 bool, forw
231237
if err != nil {
232238
return nil, err
233239
}
234-
opts, err := CommonOpts(useDotSSH)
240+
opts, err := CommonOpts(useDotSSH, hostAddress == "127.0.0.1")
235241
if err != nil {
236242
return nil, err
237243
}

0 commit comments

Comments
 (0)