From 835493b0bb86b66e8dd12f5bf2dd9d785e7f152f Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Tue, 6 Aug 2024 22:37:30 +0200 Subject: config: add pinentry support to cred commands Add pinentry support for the source-cred-cmd and outgoing-cred-cmd. Do a little dance around an unallowed import cycle. Signed-off-by: Koni Marti Acked-by: Robin Jarry --- config/accounts.go | 24 +++++++++++++++++++++++- main.go | 5 +++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/config/accounts.go b/config/accounts.go index 5bbae39f..35046f67 100644 --- a/config/accounts.go +++ b/config/accounts.go @@ -1,6 +1,7 @@ package config import ( + "bytes" "errors" "fmt" "net/url" @@ -18,6 +19,12 @@ import ( "github.com/go-ini/ini" ) +var ( + EnablePinentry func() + DisablePinentry func() + SetPinentryEnv func(*exec.Cmd) +) + type RemoteConfig struct { Value string PasswordCmd string @@ -52,11 +59,26 @@ func (c *RemoteConfig) ConnectionString() (string, error) { pw := c.cache if pw == "" { + usePinentry := EnablePinentry != nil && + DisablePinentry != nil && + SetPinentryEnv != nil + cmd := exec.Command("sh", "-c", c.PasswordCmd) cmd.Stdin = os.Stdin + + buf := new(bytes.Buffer) + cmd.Stderr = buf + + if usePinentry { + EnablePinentry() + defer DisablePinentry() + SetPinentryEnv(cmd) + } + output, err := cmd.Output() if err != nil { - return "", fmt.Errorf("failed to read password: %w", err) + return "", fmt.Errorf("failed to read password: %v: %w", + buf.String(), err) } pw = strings.TrimSpace(string(output)) } diff --git a/main.go b/main.go index efdbf653..66c2ed1a 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ import ( "git.sr.ht/~rjarry/aerc/lib/hooks" "git.sr.ht/~rjarry/aerc/lib/ipc" "git.sr.ht/~rjarry/aerc/lib/log" + "git.sr.ht/~rjarry/aerc/lib/pinentry" "git.sr.ht/~rjarry/aerc/lib/templates" "git.sr.ht/~rjarry/aerc/lib/ui" "git.sr.ht/~rjarry/aerc/models" @@ -221,6 +222,10 @@ func main() { } close(deferLoop) + config.EnablePinentry = pinentry.Enable + config.DisablePinentry = pinentry.Disable + config.SetPinentryEnv = pinentry.SetCmdEnv + startup, startupDone := context.WithCancel(context.Background()) if !noIPC { -- cgit