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 17// +build !windows 18 19package sshagent 20 21import ( 22 "errors" 23 "fmt" 24 "net" 25 "os" 26 27 "golang.org/x/crypto/ssh/agent" 28) 29 30// New returns a new agent.Agent that uses a unix socket 31func New() (agent.Agent, net.Conn, error) { 32 if !Available() { 33 return nil, nil, errors.New("SSH agent requested but SSH_AUTH_SOCK not-specified") 34 } 35 36 sshAuthSock := os.Getenv("SSH_AUTH_SOCK") 37 38 conn, err := net.Dial("unix", sshAuthSock) 39 if err != nil { 40 return nil, nil, fmt.Errorf("Error connecting to SSH_AUTH_SOCK: %v", err) 41 } 42 43 return agent.NewClient(conn), conn, nil 44} 45 46// Available returns true is a auth socket is defined 47func Available() bool { 48 return os.Getenv("SSH_AUTH_SOCK") != "" 49} 50