1
0
Fork 0
mirror of https://git.sr.ht/~rjarry/aerc synced 2025-07-10 17:00:22 +02:00
aerc/lib/pinentry/ttyname.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

45 lines
1.1 KiB
Go

package pinentry
import (
"fmt"
"os"
"strings"
"git.sr.ht/~rjarry/aerc/lib/log"
)
var missingGPGTTYmsg = `
You need to set GPG_TTY manually before starting aerc. Add the following to your
.bashrc or whatever initialization file is used for shell invocations:
GPG_TTY=$(tty)
export GPG_TTY
Further information can be found here:
https://www.gnupg.org/documentation/manuals/gnupg/Invoking-GPG_002dAGENT.html
`
// ttyname returns current name of the pty. This is necessary in order to tell
// pinentry where to ask for the passphrase.
//
// If there is a GPG_TTY environment variable set, use this one. Otherwise, try
// readline() on /proc/<pid>/fd/0.
//
// If both approaches fail, the user's only option is to set GPG_TTY manually.
//
// If tty name could not be determined, an empty string is returned.
func ttyname() string {
if s := os.Getenv("GPG_TTY"); s != "" {
return s
}
// try readlink or else show missing GPG_TTY warning msg
tty, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/0", os.Getpid()))
if err != nil {
log.Debugf("readlink: '%s' with err: %v", tty, err)
log.Warnf(missingGPGTTYmsg)
return ""
}
return strings.TrimSpace(tty)
}