From bffcc2c2a801e145ce690a403b2838d4b6eca70d Mon Sep 17 00:00:00 2001 From: Jorge Luis Betancourt Gonzalez Date: Wed, 15 Sep 2021 00:05:04 +0200 Subject: [PATCH] Additional checks when running with `--check` Extend the checks that are run when the `--check` flag is provided. Previously we only checked that the binary existed in the current `$PATH`. If the binary is a symlink it will be resolved and the end file will be compared with `pinentry-mac`. This should prevent the issue reported in #3 where default gpg installation via homebrew will break because the output of `gpgconf` contains: ``` pinentry:Passphrase Entry:/usr/local/opt/pinentry/bin/pinentry ``` At the same time `/usr/local/opt/pinentry/bin/pinentry` by default points to `pinentry-curses` which means that pinentry-touchid is unable to call the fallback pinentry program entirely. --- main.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index f0eedeb..75f4153 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "errors" "flag" "fmt" + "io/fs" "log" "os" "os/exec" @@ -323,11 +324,36 @@ func main() { } if *check { - if _, err := exec.LookPath(pinentryBinary.GetBinary()); err != nil { - fmt.Fprintf(os.Stderr, "PIN entry program %q not found!\n", pinentryBinary.GetBinary()) + var err error + binaryPath := pinentryBinary.GetBinary() + if binaryPath, err = exec.LookPath(binaryPath); err != nil { + fmt.Fprintf(os.Stderr, "PIN entry program %q not found!\n", binaryPath) os.Exit(-1) } + // check if the binary is (or resolves to -- if it is a symlink) to pinentry-mac + info, err := os.Lstat(binaryPath) + if err != nil { + fmt.Fprintf(os.Stderr, "Couldn't lstat file in: %s\n", binaryPath) + os.Exit(-1) + } + + if info.Mode()&fs.ModeSymlink != 0 { + fmt.Printf("%v found symlink .... %s\n", emoji.MagnifyingGlassTiltedRight, binaryPath) + + path, err := filepath.EvalSymlinks(binaryPath) + if err != nil { + fmt.Fprintf(os.Stderr, "Couldn't resolve symlink in %s, error: %s", binaryPath, err) + os.Exit(-1) + } + + if !strings.Contains(path, "pinentry-mac") { + fmt.Fprintf(os.Stderr, "%v %s is a symlink that resolves to:\n %s -- not to pinentry-mac\n", + emoji.CrossMark, binaryPath, path) + os.Exit(-1) + } + } + fmt.Printf("%v %s fallback pinentry found\n", emoji.CheckMarkButton, pinentryBinary.GetBinary()) fmt.Printf("%v Looks good!\n", emoji.CheckMarkButton) os.Exit(0)