2021-05-20 19:50:35 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package sshprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Microsoft/go-winio"
|
|
|
|
"github.com/pkg/errors"
|
2021-05-27 18:06:32 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2021-05-20 19:50:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Returns the Windows OpenSSH agent named pipe path, but
|
|
|
|
// only if the agent is running. Returns an error otherwise.
|
|
|
|
func getFallbackAgentPath() (string, error) {
|
|
|
|
// Windows OpenSSH agent uses a named pipe rather
|
|
|
|
// than a UNIX socket. These pipes do not play nice
|
|
|
|
// with os.Stat (which tries to open its target), so
|
|
|
|
// use a FindFirstFile syscall to check for existence.
|
2021-05-27 18:06:32 +00:00
|
|
|
var fd windows.Win32finddata
|
2021-05-20 19:50:35 +00:00
|
|
|
|
|
|
|
path := `\\.\pipe\openssh-ssh-agent`
|
2021-05-27 18:06:32 +00:00
|
|
|
pathPtr, _ := windows.UTF16PtrFromString(path)
|
|
|
|
handle, err := windows.FindFirstFile(pathPtr, &fd)
|
2021-05-20 19:50:35 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
msg := "Windows OpenSSH agent not available at %s." +
|
|
|
|
" Enable the SSH agent service or set SSH_AUTH_SOCK."
|
|
|
|
return "", errors.Errorf(msg, path)
|
|
|
|
}
|
|
|
|
|
2021-05-27 18:06:32 +00:00
|
|
|
_ = windows.CloseHandle(handle)
|
2021-05-20 19:50:35 +00:00
|
|
|
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the path references a named pipe.
|
|
|
|
func isWindowsPipePath(path string) bool {
|
|
|
|
// If path matches \\*\pipe\* then it references a named pipe
|
|
|
|
// and requires winio.DialPipe() rather than DialTimeout("unix").
|
|
|
|
// Slashes and backslashes may be used interchangeably in the path.
|
|
|
|
// Path separators may consist of multiple consecutive (back)slashes.
|
2021-05-27 18:20:08 +00:00
|
|
|
pipePattern := strings.ReplaceAll("^[/]{2}[^/]+[/]+pipe[/]+", "/", `\\/`)
|
2021-05-20 19:50:35 +00:00
|
|
|
ok, _ := regexp.MatchString(pipePattern, path)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2021-05-27 18:08:31 +00:00
|
|
|
func getWindowsPipeDialer(path string) *socketDialer {
|
2021-05-20 19:50:35 +00:00
|
|
|
if isWindowsPipePath(path) {
|
|
|
|
return &socketDialer{path: path, dialer: windowsPipeDialer}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func windowsPipeDialer(path string) (net.Conn, error) {
|
|
|
|
return winio.DialPipe(path, nil)
|
|
|
|
}
|