From 1e24bce546ae71fc64c2b0e6a3c06de9bb89f8de Mon Sep 17 00:00:00 2001 From: Bernd Schoolmann Date: Sun, 4 Feb 2024 00:08:25 +0100 Subject: [PATCH] Add windows ssh named pipe --- agent/ssh/ssh.go | 37 ---------------------------- agent/ssh/sshsocketunix.go | 46 +++++++++++++++++++++++++++++++++++ agent/ssh/sshsocketwindows.go | 38 +++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 37 deletions(-) create mode 100644 agent/ssh/sshsocketunix.go create mode 100644 agent/ssh/sshsocketwindows.go diff --git a/agent/ssh/ssh.go b/agent/ssh/ssh.go index e327f81..d195581 100644 --- a/agent/ssh/ssh.go +++ b/agent/ssh/ssh.go @@ -5,8 +5,6 @@ import ( "crypto/rand" "errors" "fmt" - "net" - "os" "time" "github.com/quexten/goldwarden/agent/config" @@ -184,38 +182,3 @@ func NewVaultAgent(vault *vault.Vault, config *config.Config, runtimeConfig *con }, } } - -func (v SSHAgentServer) Serve() { - path := v.runtimeConfig.SSHAgentSocketPath - if _, err := os.Stat(path); err == nil { - if err := os.Remove(path); err != nil { - log.Error("Could not remove old socket file: %s", err) - return - } - } - listener, err := net.Listen("unix", path) - if err != nil { - panic(err) - } - - log.Info("SSH Agent listening on %s", path) - - for { - var conn, err = listener.Accept() - if err != nil { - panic(err) - } - - callingContext := sockets.GetCallingContext(conn) - - log.Info("SSH Agent connection from %s>%s>%s \nby user %s", callingContext.GrandParentProcessName, callingContext.ParentProcessName, callingContext.ProcessName, callingContext.UserName) - log.Info("SSH Agent connection accepted") - - go agent.ServeAgent(vaultAgent{ - vault: v.vault, - config: v.config, - unlockRequestAction: v.unlockRequestAction, - context: callingContext, - }, conn) - } -} diff --git a/agent/ssh/sshsocketunix.go b/agent/ssh/sshsocketunix.go new file mode 100644 index 0000000..bce588d --- /dev/null +++ b/agent/ssh/sshsocketunix.go @@ -0,0 +1,46 @@ +//go:build !windows + +package ssh + +import ( + "net" + "os" + + "github.com/quexten/goldwarden/agent/sockets" + "golang.org/x/crypto/ssh/agent" +) + +func (v SSHAgentServer) Serve() { + path := v.runtimeConfig.SSHAgentSocketPath + if _, err := os.Stat(path); err == nil { + if err := os.Remove(path); err != nil { + log.Error("Could not remove old socket file: %s", err) + return + } + } + listener, err := net.Listen("unix", path) + if err != nil { + panic(err) + } + + log.Info("SSH Agent listening on %s", path) + + for { + var conn, err = listener.Accept() + if err != nil { + panic(err) + } + + callingContext := sockets.GetCallingContext(conn) + + log.Info("SSH Agent connection from %s>%s>%s \nby user %s", callingContext.GrandParentProcessName, callingContext.ParentProcessName, callingContext.ProcessName, callingContext.UserName) + log.Info("SSH Agent connection accepted") + + go agent.ServeAgent(vaultAgent{ + vault: v.vault, + config: v.config, + unlockRequestAction: v.unlockRequestAction, + context: callingContext, + }, conn) + } +} diff --git a/agent/ssh/sshsocketwindows.go b/agent/ssh/sshsocketwindows.go new file mode 100644 index 0000000..2905cdd --- /dev/null +++ b/agent/ssh/sshsocketwindows.go @@ -0,0 +1,38 @@ +//go:build windows + +package ssh + +import ( + "github.com/quexten/goldwarden/agent/sockets" + "golang.org/x/crypto/ssh/agent" +) + +func (v SSHAgentServer) Serve() { + pipePath := `\\.\pipe\openssh-ssh-agent` + + l, err := winio.ListenPipe(pipePath, nil) + if err != nil { + log.Fatal("listen error:", err) + } + defer l.Close() + log.Printf("Server listening on named pipe %v\n", pipePath) + + for { + conn, err := l.Accept() + if err != nil { + log.Fatal("accept error:", err) + } + + callingContext := sockets.GetCallingContext(conn) + + log.Info("SSH Agent connection from %s>%s>%s \nby user %s", callingContext.GrandParentProcessName, callingContext.ParentProcessName, callingContext.ProcessName, callingContext.UserName) + log.Info("SSH Agent connection accepted") + + go agent.ServeAgent(vaultAgent{ + vault: v.vault, + config: v.config, + unlockRequestAction: v.unlockRequestAction, + context: callingContext, + }, conn) + } +}