diff --git a/Gopkg.lock b/Gopkg.lock index a3e0571b..0a019e5c 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -264,6 +264,12 @@ packages = ["."] revision = "9520e82c474b0a04dd04f8a40959027271bab992" +[[projects]] + branch = "master" + name = "github.com/mitchellh/go-homedir" + packages = ["."] + revision = "b8bc1bf767474819792c23f32d8286a45736f1c6" + [[projects]] branch = "master" name = "github.com/moul/http2curl" @@ -377,6 +383,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "1a43293a9ffc91270316199bec5474167594f96a3612f899842532c0f224d694" + inputs-digest = "17ca5502a80ae70140ecf819a5b2757898b42deca821221afebce63488b691ea" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index afc118fc..1da6d27b 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -107,3 +107,7 @@ [[constraint]] branch = "master" name = "golang.org/x/crypto" + +[[constraint]] + branch = "master" + name = "github.com/mitchellh/go-homedir" diff --git a/commands/configtest.go b/commands/configtest.go index f8cb6d24..e3024b0f 100644 --- a/commands/configtest.go +++ b/commands/configtest.go @@ -45,6 +45,7 @@ type ConfigtestCmd struct { deep bool debug bool + vvv bool } // Name return subcommand name @@ -68,6 +69,7 @@ func (*ConfigtestCmd) Usage() string { [-containers-only] [-http-proxy=http://192.168.0.1:8080] [-debug] + [-vvv] [SERVER]... ` @@ -125,6 +127,8 @@ func (p *ConfigtestCmd) SetFlags(f *flag.FlagSet) { "containers-only", false, "Test containers only. Default: Test both of hosts and containers") + + f.BoolVar(&p.vvv, "vvv", false, "ssh -vvv") } // Execute execute @@ -134,6 +138,11 @@ func (p *ConfigtestCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interfa c.Conf.LogDir = p.logDir util.Log = util.NewCustomLogger(c.ServerInfo{}) + if err := mkdirDotVuls(); err != nil { + util.Log.Errorf("Failed to create .vuls: %s", err) + return subcommands.ExitUsageError + } + var keyPass string var err error if p.askKeyPassword { @@ -161,6 +170,7 @@ func (p *ConfigtestCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interfa if !(c.Conf.Fast || c.Conf.Offline || c.Conf.Deep) { c.Conf.Fast = true } + c.Conf.Vvv = p.vvv var servernames []string if 0 < len(f.Args()) { diff --git a/commands/scan.go b/commands/scan.go index 70e1357e..bcf890c1 100644 --- a/commands/scan.go +++ b/commands/scan.go @@ -188,6 +188,11 @@ func (p *ScanCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) c.Conf.LogDir = p.logDir util.Log = util.NewCustomLogger(c.ServerInfo{}) + if err := mkdirDotVuls(); err != nil { + util.Log.Errorf("Failed to create .vuls: %s", err) + return subcommands.ExitUsageError + } + var keyPass string var err error if p.askKeyPassword { diff --git a/commands/util.go b/commands/util.go index d0a08b5f..a97fd495 100644 --- a/commands/util.go +++ b/commands/util.go @@ -19,8 +19,11 @@ package commands import ( "fmt" + "os" + "path/filepath" "github.com/howeyc/gopass" + homedir "github.com/mitchellh/go-homedir" ) func getPasswd(prompt string) (string, error) { @@ -36,3 +39,17 @@ func getPasswd(prompt string) (string, error) { } } + +func mkdirDotVuls() error { + home, err := homedir.Dir() + if err != nil { + return err + } + dotVuls := filepath.Join(home, ".vuls") + if _, err := os.Stat(dotVuls); os.IsNotExist(err) { + if err := os.Mkdir(dotVuls, 0700); err != nil { + return err + } + } + return nil +} diff --git a/scan/executil.go b/scan/executil.go index 08ff93cb..f223fca4 100644 --- a/scan/executil.go +++ b/scan/executil.go @@ -26,6 +26,7 @@ import ( "net" "os" ex "os/exec" + "path/filepath" "strings" "syscall" "time" @@ -36,6 +37,7 @@ import ( "github.com/cenkalti/backoff" conf "github.com/future-architect/vuls/config" "github.com/future-architect/vuls/util" + homedir "github.com/mitchellh/go-homedir" "github.com/sirupsen/logrus" ) @@ -269,6 +271,15 @@ func sshExecExternal(c conf.ServerInfo, cmd string, sudo bool) (result execResul return sshExecNative(c, cmd, sudo) } + home, err := homedir.Dir() + if err != nil { + msg := fmt.Sprintf("Failed to get HOME directory: %s", err) + result.Stderr = msg + result.ExitStatus = 997 + return + } + controlPath := filepath.Join(home, ".vuls", `controlmaster-%r-%h.%p`) + defaultSSHArgs := []string{ "-tt", "-o", "StrictHostKeyChecking=yes", @@ -276,7 +287,7 @@ func sshExecExternal(c conf.ServerInfo, cmd string, sudo bool) (result execResul "-o", "ConnectionAttempts=3", "-o", "ConnectTimeout=10", "-o", "ControlMaster=auto", - "-o", `ControlPath=~/.ssh/controlmaster-%r-%h.%p`, + "-o", fmt.Sprintf("ControlPath=%s", controlPath), "-o", "Controlpersist=10m", } if conf.Conf.Vvv {