pax_global_header00006660000000000000000000000064140267133540014517gustar00rootroot0000000000000052 comment=68c0c892a22a8bf79016d847904dd4f473033844 pinentry-0.0.2/000077500000000000000000000000001402671335400133665ustar00rootroot00000000000000pinentry-0.0.2/.gitignore000066400000000000000000000004151402671335400153560ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) # vendor/ pinentry-0.0.2/LICENSE000066400000000000000000000020571402671335400143770ustar00rootroot00000000000000MIT License Copyright (c) 2021 Gopass Authors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pinentry-0.0.2/README.md000066400000000000000000000000421402671335400146410ustar00rootroot00000000000000# pinentry Pinentry client in Go pinentry-0.0.2/go.mod000066400000000000000000000000551402671335400144740ustar00rootroot00000000000000module github.com/gopasspw/pinentry go 1.16 pinentry-0.0.2/go.sum000066400000000000000000000000001402671335400145070ustar00rootroot00000000000000pinentry-0.0.2/gpgconf/000077500000000000000000000000001402671335400150115ustar00rootroot00000000000000pinentry-0.0.2/gpgconf/gpgconf.go000066400000000000000000000010621402671335400167620ustar00rootroot00000000000000package gpgconf import ( "bufio" "bytes" "os" "os/exec" "strings" ) // Path returns the path to a GPG component func Path(key string) (string, error) { buf := &bytes.Buffer{} cmd := exec.Command("gpgconf") cmd.Stdout = buf cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { return "", err } key = strings.TrimSpace(strings.ToLower(key)) sc := bufio.NewScanner(buf) for sc.Scan() { p := strings.Split(strings.TrimSpace(sc.Text()), ":") if len(p) < 3 { continue } if key == p[0] { return p[2], nil } } return "", nil } pinentry-0.0.2/pinentry.go000066400000000000000000000064161402671335400155740ustar00rootroot00000000000000// Package pinentry implements a cross platform pinentry client. It can be used // to obtain credentials from the user through a simple UI application. package pinentry import ( "bufio" "bytes" "fmt" "io" "net/url" "os" "os/exec" "strings" ) var ( // Unescape enables unescaping of percent encoded values, // disabled by default for backward compatibility. See #1621 Unescape bool ) func init() { if sv := os.Getenv("GOPASS_PINENTRY_UNESCAPE"); sv == "on" || sv == "true" { Unescape = true } } // Client is a pinentry client type Client struct { cmd *exec.Cmd in io.WriteCloser out *bufio.Reader } // New creates a new pinentry client func New() (*Client, error) { cmd := exec.Command(GetBinary()) stdin, err := cmd.StdinPipe() if err != nil { return nil, err } stdout, err := cmd.StdoutPipe() if err != nil { return nil, err } br := bufio.NewReader(stdout) if err := cmd.Start(); err != nil { return nil, err } // check welcome message banner, _, err := br.ReadLine() if err != nil { return nil, err } if !bytes.HasPrefix(banner, []byte("OK")) { return nil, fmt.Errorf("wrong banner: %s", banner) } cl := &Client{ cmd: cmd, in: stdin, out: br, } return cl, nil } // Close closes the client func (c *Client) Close() { _ = c.in.Close() } // Confirm sends the confirm message func (c *Client) Confirm() bool { if err := c.Set("confirm", ""); err == nil { return true } return false } // Set sets a key func (c *Client) Set(key, value string) error { key = strings.ToUpper(key) if value != "" { value = " " + value } val := "SET" + key + value + "\n" if _, err := c.in.Write([]byte(val)); err != nil { return err } line, _, _ := c.out.ReadLine() if string(line) != "OK" { return fmt.Errorf("error: %s", line) } return nil } // Option sets an option, e.g. "default-cancel=Abbruch" or "allow-external-password-cache" func (c *Client) Option(value string) error { val := "OPTION " + value + "\n" if _, err := c.in.Write([]byte(val)); err != nil { return err } line, _, _ := c.out.ReadLine() if string(line) != "OK" { return fmt.Errorf("error: %s", line) } return nil } // GetPin asks for the pin func (c *Client) GetPin() ([]byte, error) { if _, err := c.in.Write([]byte("GETPIN\n")); err != nil { return nil, err } buf, _, err := c.out.ReadLine() if err != nil { return nil, err } if bytes.HasPrefix(buf, []byte("OK")) { return nil, nil } // handle status messages for bytes.HasPrefix(buf, []byte("S ")) { buf, _, err = c.out.ReadLine() if err != nil { return nil, err } } // now there should be some data if !bytes.HasPrefix(buf, []byte("D ")) { return nil, fmt.Errorf("unexpected response: %s", buf) } pin := make([]byte, len(buf)) if n := copy(pin, buf); n != len(buf) { return nil, fmt.Errorf("failed to copy pin: expected %d bytes only copied %d", len(buf), n) } ok, _, err := c.out.ReadLine() if err != nil { return nil, err } if !bytes.HasPrefix(ok, []byte("OK")) { return nil, fmt.Errorf("unexpected response: %s", ok) } pin = pin[2:] // Handle unescaping, if enabled if bytes.Contains(pin, []byte("%")) && Unescape { sv, err := url.QueryUnescape(string(pin)) if err != nil { return nil, fmt.Errorf("failed to unescape pin: %q", err) } pin = []byte(sv) } return pin, nil } pinentry-0.0.2/pinentry_darwin.go000066400000000000000000000003751402671335400171360ustar00rootroot00000000000000// +build darwin package pinentry import "github.com/gopasspw/pinentry/gpgconf" // GetBinary always returns pinentry-mac func GetBinary() string { if p, err := gpgconf.Path("pinentry"); err == nil && p != "" { return p } return "pinentry-mac" } pinentry-0.0.2/pinentry_others.go000066400000000000000000000003771402671335400171600ustar00rootroot00000000000000// +build !darwin,!windows package pinentry import "github.com/gopasspw/pinentry/gpgconf" // GetBinary returns the binary name func GetBinary() string { if p, err := gpgconf.Path("pinentry"); err == nil && p != "" { return p } return "pinentry" } pinentry-0.0.2/pinentry_test.go000066400000000000000000000005451402671335400166300ustar00rootroot00000000000000package pinentry import "fmt" func ExampleClient() { pi, err := New() if err != nil { panic(err) } _ = pi.Set("title", "Agent Pinentry") _ = pi.Set("desc", "Asking for a passphrase") _ = pi.Set("prompt", "Please enter your passphrase:") _ = pi.Set("ok", "OK") pin, err := pi.GetPin() if err != nil { panic(err) } fmt.Println(string(pin)) } pinentry-0.0.2/pinentry_windows.go000066400000000000000000000003761402671335400173450ustar00rootroot00000000000000// +build windows package pinentry import "github.com/gopasspw/pinentry/gpgconf" // GetBinary always returns pinentry.exe func GetBinary() string { if p, err := gpgconf.Path("pinentry"); err == nil && p != "" { return p } return "pinentry.exe" }