Skip to content

Commit

Permalink
Adapt scp source and target arguments for legacy openssh
Browse files Browse the repository at this point in the history
This commit restricts use of URIs in scp command to openssh
clients starting from v8.0. On legacy clients the old syntax is
used.

For pre v8.0 openSSH clients only commands involving a single instance
are allowed.

Signed-off-by: David Cassany <dcassany@suse.com>
  • Loading branch information
davidcassany committed Nov 9, 2021
1 parent 9029b1c commit a3a7d44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
23 changes: 19 additions & 4 deletions cmd/limactl/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"strings"

"github.com/coreos/go-semver/semver"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store"
Expand Down Expand Up @@ -51,18 +52,22 @@ func copyAction(cmd *cobra.Command, args []string) error {
return err
}
instDirs := make(map[string]string)
scpFlags := []string{}
scpArgs := []string{}
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
if debug {
scpArgs = append(scpArgs, "-v")
scpFlags = append(scpFlags, "-v")
}
if recursive {
scpArgs = append(scpArgs, "-r")
scpFlags = append(scpFlags, "-r")
}
legacySSH := false
if sshutil.DetectOpenSSHVersion().LessThan(*semver.New("8.0.0")) {
legacySSH = true
}
scpArgs = append(scpArgs, "-3", "--")
for _, arg := range args {
path := strings.Split(arg, ":")
switch len(path) {
Expand All @@ -80,12 +85,22 @@ func copyAction(cmd *cobra.Command, args []string) error {
if inst.Status == store.StatusStopped {
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
}
scpArgs = append(scpArgs, fmt.Sprintf("scp://%s@127.0.0.1:%d/%s", u.Username, inst.SSHLocalPort, path[1]))
if legacySSH {
scpFlags = append(scpFlags, "-P", fmt.Sprintf("%d", inst.SSHLocalPort))
scpArgs = append(scpArgs, fmt.Sprintf("%s@127.0.0.1:%s", u.Username, path[1]))
} else {
scpArgs = append(scpArgs, fmt.Sprintf("scp://%s@127.0.0.1:%d/%s", u.Username, inst.SSHLocalPort, path[1]))
}
instDirs[instName] = inst.Dir
default:
return fmt.Errorf("path %q contains multiple colons", arg)
}
}
if legacySSH && len(instDirs) > 1 {
return fmt.Errorf("More than one (instance) host is involved in this command, this is only supported for openSSH v8.0 or higher")
}
scpFlags = append(scpFlags, "-3", "--")
scpArgs = append(scpFlags, scpArgs...)

var sshOpts []string
if len(instDirs) == 1 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sshutil/sshutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func CommonOpts(useDotSSH bool) ([]string, error) {

sshInfo.Do(func() {
sshInfo.aesAccelerated = detectAESAcceleration()
sshInfo.openSSHVersion = detectOpenSSHVersion()
sshInfo.openSSHVersion = DetectOpenSSHVersion()
})

// Only OpenSSH version 8.1 and later support adding ciphers to the front of the default set
Expand Down Expand Up @@ -246,7 +246,7 @@ func ParseOpenSSHVersion(version []byte) *semver.Version {
return &semver.Version{}
}

func detectOpenSSHVersion() semver.Version {
func DetectOpenSSHVersion() semver.Version {
var (
v semver.Version
stderr bytes.Buffer
Expand Down

0 comments on commit a3a7d44

Please sign in to comment.