Skip to content
This repository was archived by the owner on Nov 14, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions lib/sshcryptagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,48 @@ package sshcryptagent

import (
"fmt"
"io"
"net"
"os"
"strings"

"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)

type ReadWriter struct {
io.Reader
io.Writer
}

func NewReadWriter(r io.Reader, w io.Writer) io.ReadWriter {
return &ReadWriter{r, w}
}

func GetSigners() ([]ssh.Signer, error) {
conn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
return nil, fmt.Errorf("Could not connect to the SSH Agent socket. %s", err)
sockAddr := os.Getenv("SSH_AUTH_SOCK")

// ex: "socat.exe - UNIX-CONNECT:%a"
socatFormat := os.Getenv("SSH_AUTH_SOCAT")
var conn io.ReadWriter
var err error
if len(socatFormat) > 0 {
socatCommand := fmtCommand(socatFormat, sockAddr)
cmd := MkCommand(socatCommand)
cmd.Stderr = os.Stderr
pipeRd, err := cmd.StdoutPipe()
pipeWr, err := cmd.StdinPipe()

err = cmd.Start()
if err != nil {
return nil, fmt.Errorf("Could not start \"%s\": %s", socatCommand, err)
}
conn = NewReadWriter(pipeRd, pipeWr)
} else {
conn, err = net.Dial("unix", sockAddr)
if err != nil {
return nil, fmt.Errorf("Could not connect to the SSH Agent socket. %s", err)
}
}

sshAgent := agent.NewClient(conn)
Expand All @@ -27,3 +58,31 @@ func GetSigners() ([]ssh.Signer, error) {

return signers, nil
}

func fmtCommand(format string, address string) string {
var out strings.Builder
inPercent := false
for _, c := range format {
if inPercent {
inPercent = false
if (c == '%') {
out.WriteRune('%');
} else if (c == 'a') {
out.WriteString(address);
} else {
out.WriteRune('%')
out.WriteRune(c);
}
} else {
if c == '%' {
inPercent = true
} else {
out.WriteRune(c);
}
}
}
if inPercent {
out.WriteRune('%')
}
return out.String()
}
15 changes: 15 additions & 0 deletions lib/sshcryptagent/cmdline_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build !windows,!plan9

package sshcryptagent


import (
"os/exec"
)


//

func MkCommand(cmdline string) *exec.Cmd {
return exec.Command("sh", "-c", cmdline);
}
15 changes: 15 additions & 0 deletions lib/sshcryptagent/cmdline_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build windows

package sshcryptagent


import (
"os/exec"
)


//

func MkCommand(cmdline string) *exec.Cmd {
return exec.Command("cmd.exe", "/c", cmdline);
}