Skip to content

Commit

Permalink
Additional checks when running with --check
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jorgelbg committed Sep 14, 2021
1 parent ce41bda commit bffcc2c
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"flag"
"fmt"
"io/fs"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bffcc2c

Please sign in to comment.