Improve comments, tweak default password prompt function.

This commit is contained in:
Greg 2019-09-04 22:43:29 -04:00
parent 9c711e9022
commit 915f93083d
2 changed files with 8 additions and 2 deletions

View File

@ -14,7 +14,7 @@ import (
) )
func setAgentInfo() { func setAgentInfo() {
// get UID // get UID of current user
usr, err := user.Current() usr, err := user.Current()
if err != nil { if err != nil {
fmt.Printf("Error: cannot get user ID: %s\n", err) fmt.Printf("Error: cannot get user ID: %s\n", err)
@ -23,6 +23,7 @@ func setAgentInfo() {
uid := usr.Uid uid := usr.Uid
// get GPG Agent PID // get GPG Agent PID
// look for gpg-agent running as the current user:
output, err := exec.Command("pgrep", "-U", uid, "gpg-agent").Output() output, err := exec.Command("pgrep", "-U", uid, "gpg-agent").Output()
if err != nil { if err != nil {
fmt.Printf("Error: %s\n", err) fmt.Printf("Error: %s\n", err)
@ -30,6 +31,7 @@ func setAgentInfo() {
} else { } else {
fmt.Printf("gpg-agent process number is %s\n", output) fmt.Printf("gpg-agent process number is %s\n", output)
} }
// trim trailing /n and convert to int
pid, err := strconv.Atoi(string(output[:len(output)-1])) pid, err := strconv.Atoi(string(output[:len(output)-1]))
if err != nil { if err != nil {
fmt.Printf("Integer conversion failed: %s\n", err) fmt.Printf("Integer conversion failed: %s\n", err)
@ -37,6 +39,7 @@ func setAgentInfo() {
} }
// find agent socket file // find agent socket file
// find unix domain sockets opened by the current user's gpg-agent
cmd := exec.Command("lsof", "-w", "-Fn", "-u", uid, "-baUcgpg-agent") cmd := exec.Command("lsof", "-w", "-Fn", "-u", uid, "-baUcgpg-agent")
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
@ -49,6 +52,7 @@ func setAgentInfo() {
fmt.Printf("Error: cannot run lsof: %s\n", err) fmt.Printf("Error: cannot run lsof: %s\n", err)
} }
// look for a socket named "S.gpg-agent"
filename := "" filename := ""
for scanner.Scan() { for scanner.Scan() {
x := scanner.Text() x := scanner.Text()
@ -70,6 +74,8 @@ func setAgentInfo() {
s := fmt.Sprintf("%s:%d:1", filename, pid) s := fmt.Sprintf("%s:%d:1", filename, pid)
fmt.Printf("GPG_AGENT_INFO = %s\n", s) fmt.Printf("GPG_AGENT_INFO = %s\n", s)
os.Setenv("GPG_AGENT_INFO",s) os.Setenv("GPG_AGENT_INFO",s)
// gpg-agent is running, so use GPGPrompt as password prompt
ask = GPGPrompt ask = GPGPrompt
} }

View File

@ -176,7 +176,7 @@ func GPGPrompt(keys []openpgp.Key, symmetric bool) ([]byte, error) {
} }
func (s *Store) Decrypt(name string, prompts ...func() []byte) (string,error) { func (s *Store) Decrypt(name string, prompts ...func() []byte) (string,error) {
if len(prompts) > 0 { if ask == nil {
ask = AskPass(prompts...) ask = AskPass(prompts...)
} }
file := path.Join(s.Dir, strings.Join([]string{name,".gpg"},"")) file := path.Join(s.Dir, strings.Join([]string{name,".gpg"},""))