2013-08-11 14:34:47 +00:00
|
|
|
// +build !appengine
|
|
|
|
|
2011-05-17 04:52:01 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
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 gpgagent interacts with the local GPG Agent.
|
2011-05-17 04:52:01 +00:00
|
|
|
package gpgagent
|
|
|
|
|
|
|
|
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
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2011-05-17 04:52:01 +00:00
|
|
|
"fmt"
|
2011-08-25 15:14:47 +00:00
|
|
|
|
2011-05-17 04:52:01 +00:00
|
|
|
"io"
|
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
|
|
|
"net"
|
|
|
|
"net/url"
|
2011-05-17 04:52:01 +00:00
|
|
|
"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
|
|
|
"strings"
|
2011-05-17 04:52:01 +00:00
|
|
|
)
|
|
|
|
|
2013-07-08 04:12:18 +00:00
|
|
|
// Conn is a connection to the GPG agent.
|
2011-05-18 02:32:01 +00:00
|
|
|
type Conn struct {
|
|
|
|
c io.ReadWriteCloser
|
|
|
|
br *bufio.Reader
|
|
|
|
}
|
|
|
|
|
2013-07-08 04:12:18 +00:00
|
|
|
var (
|
|
|
|
ErrNoAgent = errors.New("GPG_AGENT_INFO not set in environment")
|
|
|
|
ErrNoData = errors.New("GPG_ERR_NO_DATA cache miss")
|
|
|
|
ErrCancel = errors.New("gpgagent: Cancel")
|
|
|
|
)
|
2011-05-18 02:32:01 +00:00
|
|
|
|
2013-07-08 04:12:18 +00:00
|
|
|
// NewConn connects to the GPG Agent as described in the
|
|
|
|
// GPG_AGENT_INFO environment variable.
|
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 NewConn() (*Conn, error) {
|
2011-06-30 04:13:03 +00:00
|
|
|
sp := strings.SplitN(os.Getenv("GPG_AGENT_INFO"), ":", 3)
|
2011-05-18 02:32:01 +00:00
|
|
|
if len(sp) == 0 || len(sp[0]) == 0 {
|
|
|
|
return nil, ErrNoAgent
|
|
|
|
}
|
|
|
|
addr := &net.UnixAddr{Net: "unix", Name: sp[0]}
|
|
|
|
uc, err := net.DialUnix("unix", nil, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
br := bufio.NewReader(uc)
|
|
|
|
lineb, err := br.ReadSlice('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
line := string(lineb)
|
|
|
|
if !strings.HasPrefix(line, "OK") {
|
|
|
|
return nil, fmt.Errorf("gpgagent: didn't get OK; got %q", line)
|
|
|
|
}
|
|
|
|
return &Conn{uc, br}, 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
|
|
|
func (c *Conn) Close() error {
|
2011-05-18 02:32:01 +00:00
|
|
|
c.br = nil
|
|
|
|
return c.c.Close()
|
|
|
|
}
|
|
|
|
|
2013-07-08 04:12:18 +00:00
|
|
|
// PassphraseRequest is a request to get a passphrase from the GPG
|
|
|
|
// Agent.
|
2011-05-17 04:52:01 +00:00
|
|
|
type PassphraseRequest struct {
|
2011-05-18 02:32:01 +00:00
|
|
|
CacheKey, Error, Prompt, Desc string
|
2011-05-17 04:52:01 +00:00
|
|
|
|
2011-05-18 02:32:01 +00:00
|
|
|
// If the option --no-ask is used and the passphrase is not in
|
|
|
|
// the cache the user will not be asked to enter a passphrase
|
|
|
|
// but the error code GPG_ERR_NO_DATA is returned. (ErrNoData)
|
|
|
|
NoAsk bool
|
2011-05-17 04:52:01 +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 (c *Conn) RemoveFromCache(cacheKey string) error {
|
2011-08-25 15:14:47 +00:00
|
|
|
_, err := fmt.Fprintf(c.c, "CLEAR_PASSPHRASE %s\n", url.QueryEscape(cacheKey))
|
2011-05-18 02:32:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lineb, err := c.br.ReadSlice('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
line := string(lineb)
|
|
|
|
if !strings.HasPrefix(line, "OK") {
|
|
|
|
return fmt.Errorf("gpgagent: CLEAR_PASSPHRASE returned %q", line)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2011-05-17 04:52:01 +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 (c *Conn) GetPassphrase(pr *PassphraseRequest) (passphrase string, outerr error) {
|
2011-05-17 04:52:01 +00:00
|
|
|
defer func() {
|
|
|
|
if e, ok := recover().(string); ok {
|
|
|
|
passphrase = ""
|
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
|
|
|
outerr = errors.New(e)
|
2011-05-17 04:52:01 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
set := func(cmd string, val string) {
|
|
|
|
if val == "" {
|
|
|
|
return
|
|
|
|
}
|
2011-05-18 02:32:01 +00:00
|
|
|
_, err := fmt.Fprintf(c.c, "%s %s\n", cmd, val)
|
|
|
|
if err != nil {
|
|
|
|
panic("gpgagent: failed to send " + cmd)
|
|
|
|
}
|
|
|
|
line, _, err := c.br.ReadLine()
|
2011-05-17 04:52:01 +00:00
|
|
|
if err != nil {
|
2011-05-18 02:32:01 +00:00
|
|
|
panic("gpgagent: failed to read " + cmd)
|
2011-05-17 04:52:01 +00:00
|
|
|
}
|
|
|
|
if !strings.HasPrefix(string(line), "OK") {
|
2011-05-18 02:32:01 +00:00
|
|
|
panic("gpgagent: response to " + cmd + " was " + string(line))
|
2011-05-17 04:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if d := os.Getenv("DISPLAY"); d != "" {
|
|
|
|
set("OPTION", "display="+d)
|
|
|
|
}
|
|
|
|
tty, err := os.Readlink("/proc/self/fd/0")
|
|
|
|
if err == nil {
|
|
|
|
set("OPTION", "ttyname="+tty)
|
|
|
|
}
|
|
|
|
set("OPTION", "ttytype="+os.Getenv("TERM"))
|
2011-05-18 02:32:01 +00:00
|
|
|
opts := ""
|
|
|
|
if pr.NoAsk {
|
|
|
|
opts += "--no-ask "
|
|
|
|
}
|
2011-05-18 03:25:00 +00:00
|
|
|
|
|
|
|
encOrX := func(s string) string {
|
|
|
|
if s == "" {
|
|
|
|
return "X"
|
|
|
|
}
|
2011-08-25 15:14:47 +00:00
|
|
|
return url.QueryEscape(s)
|
2011-05-18 03:25:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-18 02:32:01 +00:00
|
|
|
_, err = fmt.Fprintf(c.c, "GET_PASSPHRASE %s%s %s %s %s\n",
|
|
|
|
opts,
|
2011-08-25 15:14:47 +00:00
|
|
|
url.QueryEscape(pr.CacheKey),
|
2011-05-18 03:25:00 +00:00
|
|
|
encOrX(pr.Error),
|
|
|
|
encOrX(pr.Prompt),
|
|
|
|
encOrX(pr.Desc))
|
2011-05-18 02:32:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
lineb, err := c.br.ReadSlice('\n')
|
2011-05-17 04:52:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
line := string(lineb)
|
2011-05-18 02:32:01 +00:00
|
|
|
if strings.HasPrefix(line, "OK ") {
|
|
|
|
decb, err := hex.DecodeString(line[3 : len(line)-1])
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(decb), nil
|
|
|
|
}
|
2011-06-30 04:13:03 +00:00
|
|
|
fields := strings.Split(line, " ")
|
2011-05-18 02:32:01 +00:00
|
|
|
if len(fields) >= 2 && fields[0] == "ERR" {
|
|
|
|
switch fields[1] {
|
|
|
|
case "67108922":
|
|
|
|
return "", ErrNoData
|
|
|
|
case "83886179":
|
|
|
|
return "", ErrCancel
|
|
|
|
}
|
|
|
|
}
|
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(line)
|
2011-05-17 04:52:01 +00:00
|
|
|
}
|