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