1
0
Fork 0
mirror of https://git.sr.ht/~rjarry/aerc synced 2025-07-13 04:30:22 +02:00
aerc/lib/pinentry/pinentry.go
Koni Marti 163ea3ec7d aerc: support terminal-based pinentry programs
Support terminal-based pinentry programs. Suspend vaxis before running
the command that can trigger a pinentry call. Provide the proper tty in
the GPG_TTY environment variable (and set a TERM variable if not
provided; this is necessary for pinentry-curses). Finally, resume vaxis.

To enable terminal-based pinentry support, you have to set

	[general]
	use-terminal-pinentry = true

in your aerc.conf. Any GUI-based pinentry programs will work the same as
before if this option is not set to true.

To test pinentry-tty, add the following to your ~/.gnupg/gpg-agent.conf:

	pinentry-program /usr/bin/pinentry-tty

and kill all running gpg-agents:

	$ killall gpg-agent

Fixes: https://todo.sr.ht/~rjarry/aerc/202
Changelog-fixed: Terminal-based pinentry programs
 (e.g. `pinentry-curses`) now work properly.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
2024-10-12 00:12:25 +02:00

71 lines
1.2 KiB
Go

package pinentry
import (
"fmt"
"os"
"os/exec"
"strings"
"sync/atomic"
"git.sr.ht/~rjarry/aerc/config"
"git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/ui"
)
var pinentryMode int32 = 0
func Enable() {
if !config.General.UsePinentry {
return
}
if atomic.SwapInt32(&pinentryMode, 1) == 1 {
// cannot enter pinentry mode twice
return
}
ui.SuspendScreen()
}
func Disable() {
if atomic.SwapInt32(&pinentryMode, 0) == 0 {
// not in pinentry mode
return
}
ui.ResumeScreen()
}
func SetCmdEnv(cmd *exec.Cmd) {
if cmd == nil || atomic.LoadInt32(&pinentryMode) == 0 {
return
}
env := cmd.Env
if env == nil {
env = os.Environ()
}
hasTerm := false
hasGPGTTY := false
for _, e := range env {
switch {
case strings.HasPrefix(strings.ToUpper(e), "TERM="):
log.Debugf("pinentry: use %v", e)
hasTerm = true
case strings.HasPrefix(strings.ToUpper(e), "GPG_TTY="):
log.Debugf("pinentry: use %v", e)
hasGPGTTY = true
}
}
if !hasTerm {
env = append(env, "TERM=xterm-256color")
log.Debugf("pinentry: set TERM=xterm-256color")
}
if !hasGPGTTY {
tty := ttyname()
env = append(env, fmt.Sprintf("GPG_TTY=%s", tty))
log.Debugf("pinentry: set GPG_TTY=%s", tty)
}
cmd.Env = env
}