1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
package request
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/browserpass/browserpass-native/errors"
"github.com/browserpass/browserpass-native/response"
log "github.com/sirupsen/logrus"
)
func fetchDecryptedContents(request *request) {
responseData := response.MakeFetchResponse()
if !strings.HasSuffix(request.File, ".gpg") {
log.Errorf("The requested password file '%v' does not have the expected '.gpg' extension", request.File)
response.SendErrorAndExit(
errors.CodeInvalidPasswordFileExtension,
&map[errors.Field]string{
errors.FieldMessage: "The requested password file does not have the expected '.gpg' extension",
errors.FieldAction: "fetch",
errors.FieldFile: request.File,
},
)
}
store, ok := request.Settings.Stores[request.StoreID]
if !ok {
log.Errorf(
"The password store with ID '%v' is not present in the list of stores '%+v'",
request.StoreID, request.Settings.Stores,
)
response.SendErrorAndExit(
errors.CodeInvalidPasswordStore,
&map[errors.Field]string{
errors.FieldMessage: "The password store is not present in the list of stores",
errors.FieldAction: "fetch",
errors.FieldStoreID: request.StoreID,
},
)
}
normalizedStorePath, err := normalizePasswordStorePath(store.Path)
if err != nil {
log.Errorf(
"The password store '%+v' is not accessible at its location: %+v",
store, err,
)
response.SendErrorAndExit(
errors.CodeInaccessiblePasswordStore,
&map[errors.Field]string{
errors.FieldMessage: "The password store is not accessible",
errors.FieldAction: "fetch",
errors.FieldError: err.Error(),
errors.FieldStoreID: store.ID,
errors.FieldStoreName: store.Name,
errors.FieldStorePath: store.Path,
},
)
}
store.Path = normalizedStorePath
var gpgPath string
if request.Settings.GpgPath != "" || store.Settings.GpgPath != "" {
if request.Settings.GpgPath != "" {
gpgPath = request.Settings.GpgPath
} else {
gpgPath = store.Settings.GpgPath
}
err = validateGpgBinary(gpgPath)
if err != nil {
log.Errorf(
"The provided gpg binary path '%v' is invalid: %+v",
gpgPath, err,
)
response.SendErrorAndExit(
errors.CodeInvalidGpgPath,
&map[errors.Field]string{
errors.FieldMessage: "The provided gpg binary path is invalid",
errors.FieldAction: "fetch",
errors.FieldError: err.Error(),
errors.FieldGpgPath: gpgPath,
},
)
}
} else {
gpgPath, err = detectGpgBinary()
if err != nil {
log.Error("Unable to detect the location of the gpg binary: ", err)
response.SendErrorAndExit(
errors.CodeUnableToDetectGpgPath,
&map[errors.Field]string{
errors.FieldMessage: "Unable to detect the location of the gpg binary",
errors.FieldAction: "fetch",
errors.FieldError: err.Error(),
},
)
}
}
responseData.Contents, err = decryptFile(&store, request.File, gpgPath)
if err != nil {
log.Errorf(
"Unable to decrypt the password file '%v' in the password store '%+v': %+v",
request.File, store, err,
)
response.SendErrorAndExit(
errors.CodeUnableToDecryptPasswordFile,
&map[errors.Field]string{
errors.FieldMessage: "Unable to decrypt the password file",
errors.FieldAction: "fetch",
errors.FieldError: err.Error(),
errors.FieldFile: request.File,
errors.FieldStoreID: store.ID,
errors.FieldStoreName: store.Name,
errors.FieldStorePath: store.Path,
},
)
}
response.SendOk(responseData)
}
func detectGpgBinary() (string, error) {
// Look in $PATH first, then check common locations - the first successful result wins
gpgBinaryPriorityList := []string{
"gpg2", "gpg",
"/bin/gpg2", "/usr/bin/gpg2", "/usr/local/bin/gpg2",
"/bin/gpg", "/usr/bin/gpg", "/usr/local/bin/gpg",
}
for _, binary := range gpgBinaryPriorityList {
err := validateGpgBinary(binary)
if err == nil {
return binary, nil
}
}
return "", fmt.Errorf("Unable to detect the location of the gpg binary to use")
}
func validateGpgBinary(gpgPath string) error {
return exec.Command(gpgPath, "--version").Run()
}
func decryptFile(store *store, file string, gpgPath string) (string, error) {
passwordFilePath := filepath.Join(store.Path, file)
passwordFile, err := os.Open(passwordFilePath)
if err != nil {
return "", err
}
var stdout, stderr bytes.Buffer
gpgOptions := []string{"--decrypt", "--yes", "--quiet", "--batch", "-"}
cmd := exec.Command(gpgPath, gpgOptions...)
cmd.Stdin = passwordFile
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("Error: %s, Stderr: %s", err.Error(), stderr.String())
}
return stdout.String(), nil
}
|