2019-09-04 22:19:39 -04:00
|
|
|
//+build !android !linux
|
|
|
|
|
2019-09-04 22:21:01 -04:00
|
|
|
package gpass
|
2019-09-04 22:19:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setAgentInfo() {
|
2019-09-04 22:43:29 -04:00
|
|
|
// get UID of current user
|
2019-09-04 22:19:39 -04:00
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: cannot get user ID: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
uid := usr.Uid
|
|
|
|
|
|
|
|
// get GPG Agent PID
|
2019-09-04 22:43:29 -04:00
|
|
|
// look for gpg-agent running as the current user:
|
2019-09-04 22:19:39 -04:00
|
|
|
output, err := exec.Command("pgrep", "-U", uid, "gpg-agent").Output()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: %s\n", err)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
fmt.Printf("gpg-agent process number is %s\n", output)
|
|
|
|
}
|
2019-09-04 22:43:29 -04:00
|
|
|
// trim trailing /n and convert to int
|
2019-09-04 22:19:39 -04:00
|
|
|
pid, err := strconv.Atoi(string(output[:len(output)-1]))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Integer conversion failed: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// find agent socket file
|
2019-09-04 22:43:29 -04:00
|
|
|
// find unix domain sockets opened by the current user's gpg-agent
|
2019-09-04 22:19:39 -04:00
|
|
|
cmd := exec.Command("lsof", "-w", "-Fn", "-u", uid, "-baUcgpg-agent")
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: connect stdout to lsof: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: cannot run lsof: %s\n", err)
|
|
|
|
}
|
|
|
|
|
2019-09-04 22:43:29 -04:00
|
|
|
// look for a socket named "S.gpg-agent"
|
2019-09-04 22:19:39 -04:00
|
|
|
filename := ""
|
|
|
|
for scanner.Scan() {
|
|
|
|
x := scanner.Text()
|
|
|
|
if x[0] != 'n' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
x = x[1:]
|
|
|
|
if path.Base(x) == "S.gpg-agent" {
|
|
|
|
fmt.Println(x)
|
|
|
|
filename = x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
|
|
|
|
if filename == "" {
|
|
|
|
fmt.Printf("Error: gpg-agent socket file not found\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s := fmt.Sprintf("%s:%d:1", filename, pid)
|
|
|
|
fmt.Printf("GPG_AGENT_INFO = %s\n", s)
|
|
|
|
os.Setenv("GPG_AGENT_INFO",s)
|
2019-09-04 22:43:29 -04:00
|
|
|
|
|
|
|
// gpg-agent is running, so use GPGPrompt as password prompt
|
2019-09-04 22:21:01 -04:00
|
|
|
ask = GPGPrompt
|
2019-09-04 22:19:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
setAgentInfo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Clip(x string) {
|
|
|
|
b := bytes.NewBuffer([]byte(x))
|
|
|
|
cmd := exec.Command("pbcopy")
|
|
|
|
cmd.Stdin = b
|
|
|
|
cmd.Run()
|
|
|
|
}
|
|
|
|
|