2011-05-15 01:43:07 +00:00
|
|
|
/*
|
2018-01-04 00:52:49 +00:00
|
|
|
Copyright 2011 The Perkeep Authors
|
2011-05-15 01:43:07 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-07-08 04:12:18 +00:00
|
|
|
// Package pinentry interfaces with the pinentry(1) command to securely
|
|
|
|
// prompt the user for a password using whichever user interface the
|
|
|
|
// user is currently using.
|
2018-01-02 04:48:34 +00:00
|
|
|
package pinentry // import "perkeep.org/internal/pinentry"
|
2011-05-15 01:43:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"errors"
|
2011-05-15 01:43:07 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"os/exec"
|
2011-05-15 01:43:07 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2013-07-08 04:12:18 +00:00
|
|
|
// ErrCancel is returned when the user explicitly aborts the password
|
|
|
|
// request.
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
var ErrCancel = errors.New("pinentry: Cancel")
|
2011-05-18 03:25:00 +00:00
|
|
|
|
2013-07-08 04:12:18 +00:00
|
|
|
// Request describes what the user should see during the request for
|
|
|
|
// their password.
|
2011-05-15 01:43:07 +00:00
|
|
|
type Request struct {
|
|
|
|
Desc, Prompt, OK, Cancel, Error string
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func catch(err *error) {
|
2011-06-02 23:47:40 +00:00
|
|
|
rerr := recover()
|
|
|
|
if rerr == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e, ok := rerr.(string); ok {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
*err = errors.New(e)
|
2011-06-02 23:47:40 +00:00
|
|
|
}
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
if e, ok := rerr.(error); ok {
|
2011-06-02 23:47:40 +00:00
|
|
|
*err = e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func check(err error) {
|
2011-06-02 23:47:40 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (r *Request) GetPIN() (pin string, outerr error) {
|
2011-06-02 23:47:40 +00:00
|
|
|
defer catch(&outerr)
|
2011-05-18 03:48:57 +00:00
|
|
|
bin, err := exec.LookPath("pinentry")
|
|
|
|
if err != nil {
|
|
|
|
return r.getPINNaïve()
|
|
|
|
}
|
2011-06-02 23:47:40 +00:00
|
|
|
cmd := exec.Command(bin)
|
|
|
|
stdin, _ := cmd.StdinPipe()
|
|
|
|
stdout, _ := cmd.StdoutPipe()
|
|
|
|
check(cmd.Start())
|
|
|
|
defer cmd.Wait()
|
|
|
|
defer stdin.Close()
|
|
|
|
br := bufio.NewReader(stdout)
|
2011-05-15 01:43:07 +00:00
|
|
|
lineb, _, err := br.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to get getpin greeting")
|
|
|
|
}
|
|
|
|
line := string(lineb)
|
|
|
|
if !strings.HasPrefix(line, "OK") {
|
|
|
|
return "", fmt.Errorf("getpin greeting said %q", line)
|
|
|
|
}
|
|
|
|
set := func(cmd string, val string) {
|
|
|
|
if val == "" {
|
|
|
|
return
|
|
|
|
}
|
2011-06-02 23:47:40 +00:00
|
|
|
fmt.Fprintf(stdin, "%s %s\n", cmd, val)
|
2011-05-15 01:43:07 +00:00
|
|
|
line, _, err := br.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
panic("Failed to " + cmd)
|
|
|
|
}
|
|
|
|
if string(line) != "OK" {
|
|
|
|
panic("Response to " + cmd + " was " + string(line))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set("SETPROMPT", r.Prompt)
|
|
|
|
set("SETDESC", r.Desc)
|
|
|
|
set("SETOK", r.OK)
|
|
|
|
set("SETCANCEL", r.Cancel)
|
|
|
|
set("SETERROR", r.Error)
|
2011-07-02 16:09:50 +00:00
|
|
|
set("OPTION", "ttytype="+os.Getenv("TERM"))
|
2011-05-17 04:52:01 +00:00
|
|
|
tty, err := os.Readlink("/proc/self/fd/0")
|
2011-05-15 01:43:07 +00:00
|
|
|
if err == nil {
|
2011-07-02 16:09:50 +00:00
|
|
|
set("OPTION", "ttyname="+tty)
|
2011-05-15 01:43:07 +00:00
|
|
|
}
|
2011-06-02 23:47:40 +00:00
|
|
|
fmt.Fprintf(stdin, "GETPIN\n")
|
2011-05-15 01:43:07 +00:00
|
|
|
lineb, _, err = br.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to read line after GETPIN: %v", err)
|
|
|
|
}
|
|
|
|
line = string(lineb)
|
2011-05-18 03:25:00 +00:00
|
|
|
if strings.HasPrefix(line, "D ") {
|
|
|
|
return line[2:], nil
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(line, "ERR 83886179 ") {
|
|
|
|
return "", ErrCancel
|
2011-05-15 01:43:07 +00:00
|
|
|
}
|
2011-05-18 03:25:00 +00:00
|
|
|
return "", fmt.Errorf("GETPIN response didn't start with D; got %q", line)
|
2011-05-15 01:43:07 +00:00
|
|
|
}
|
2011-05-18 03:48:57 +00:00
|
|
|
|
2017-02-06 18:00:04 +00:00
|
|
|
func runPass(bin string, args ...string) error {
|
2011-06-02 23:47:40 +00:00
|
|
|
cmd := exec.Command(bin, args...)
|
2017-02-06 18:00:04 +00:00
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
return fmt.Errorf("%v error: %v, %s", bin, err, out)
|
|
|
|
}
|
|
|
|
return nil
|
2011-05-18 03:48:57 +00:00
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (r *Request) getPINNaïve() (string, error) {
|
2011-05-18 03:48:57 +00:00
|
|
|
stty, err := exec.LookPath("stty")
|
|
|
|
if err != nil {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return "", errors.New("no pinentry or stty found")
|
2011-05-18 03:48:57 +00:00
|
|
|
}
|
2017-02-06 18:00:04 +00:00
|
|
|
if err := runPass(stty, "-echo"); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2011-05-18 03:48:57 +00:00
|
|
|
defer runPass(stty, "echo")
|
|
|
|
|
|
|
|
if r.Desc != "" {
|
|
|
|
fmt.Printf("%s\n\n", r.Desc)
|
|
|
|
}
|
|
|
|
prompt := r.Prompt
|
|
|
|
if prompt == "" {
|
|
|
|
prompt = "Password"
|
|
|
|
}
|
|
|
|
fmt.Printf("%s: ", prompt)
|
|
|
|
br := bufio.NewReader(os.Stdin)
|
|
|
|
line, _, err := br.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(line), nil
|
2011-07-02 16:09:50 +00:00
|
|
|
}
|