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