You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.2 KiB

  1. //
  2. // Copyright 2015, Sander van Harmelen
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // +build !windows
  17. package sshagent
  18. import (
  19. "errors"
  20. "fmt"
  21. "net"
  22. "os"
  23. "golang.org/x/crypto/ssh/agent"
  24. )
  25. // New returns a new agent.Agent that uses a unix socket
  26. func New() (agent.Agent, net.Conn, error) {
  27. if !Available() {
  28. return nil, nil, errors.New("SSH agent requested but SSH_AUTH_SOCK not-specified")
  29. }
  30. sshAuthSock := os.Getenv("SSH_AUTH_SOCK")
  31. conn, err := net.Dial("unix", sshAuthSock)
  32. if err != nil {
  33. return nil, nil, fmt.Errorf("Error connecting to SSH_AUTH_SOCK: %v", err)
  34. }
  35. return agent.NewClient(conn), conn, nil
  36. }
  37. // Available returns true is a auth socket is defined
  38. func Available() bool {
  39. return os.Getenv("SSH_AUTH_SOCK") != ""
  40. }