| 1 |
package request |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"io/ioutil" |
| 6 |
"os" |
| 7 |
"os/user" |
| 8 |
"path/filepath" |
| 9 |
|
| 10 |
"github.com/browserpass/browserpass-native/errors" |
| 11 |
"github.com/browserpass/browserpass-native/response" |
| 12 |
log "github.com/sirupsen/logrus" |
| 13 |
) |
| 14 |
|
| 15 |
func configure(request *request) { |
| 16 |
responseData := response.MakeConfigureResponse() |
| 17 |
|
| 18 |
// User configured gpgPath in the browser, check if it is a valid binary to use |
| 19 |
if request.Settings.GpgPath != "" { |
| 20 |
err := validateGpgBinary(request.Settings.GpgPath) |
| 21 |
if err != nil { |
| 22 |
log.Errorf( |
| 23 |
"The provided gpg binary path '%v' is invalid: %+v", |
| 24 |
request.Settings.GpgPath, err, |
| 25 |
) |
| 26 |
response.SendErrorAndExit( |
| 27 |
errors.CodeInvalidGpgPath, |
| 28 |
&map[errors.Field]string{ |
| 29 |
errors.FieldMessage: "The provided gpg binary path is invalid", |
| 30 |
errors.FieldAction: "configure", |
| 31 |
errors.FieldError: err.Error(), |
| 32 |
errors.FieldGpgPath: request.Settings.GpgPath, |
| 33 |
}, |
| 34 |
) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// Check that each and every store in the settings exists and is accessible. |
| 39 |
// Then read the default configuration for these stores (if available). |
| 40 |
for _, store := range request.Settings.Stores { |
| 41 |
normalizedStorePath, err := normalizePasswordStorePath(store.Path) |
| 42 |
if err != nil { |
| 43 |
log.Errorf( |
| 44 |
"The password store '%+v' is not accessible at its location: %+v", |
| 45 |
store, err, |
| 46 |
) |
| 47 |
response.SendErrorAndExit( |
| 48 |
errors.CodeInaccessiblePasswordStore, |
| 49 |
&map[errors.Field]string{ |
| 50 |
errors.FieldMessage: "The password store is not accessible", |
| 51 |
errors.FieldAction: "configure", |
| 52 |
errors.FieldError: err.Error(), |
| 53 |
errors.FieldStoreID: store.ID, |
| 54 |
errors.FieldStoreName: store.Name, |
| 55 |
errors.FieldStorePath: store.Path, |
| 56 |
}, |
| 57 |
) |
| 58 |
} |
| 59 |
|
| 60 |
store.Path = normalizedStorePath |
| 61 |
|
| 62 |
responseData.StoreSettings[store.ID], err = readDefaultSettings(store.Path) |
| 63 |
if err == nil { |
| 64 |
var storeSettings StoreSettings |
| 65 |
err = json.Unmarshal([]byte(responseData.StoreSettings[store.ID]), &storeSettings) |
| 66 |
} |
| 67 |
if err != nil { |
| 68 |
log.Errorf( |
| 69 |
"Unable to read .browserpass.json of the user-configured password store '%+v': %+v", |
| 70 |
store, err, |
| 71 |
) |
| 72 |
response.SendErrorAndExit( |
| 73 |
errors.CodeUnreadablePasswordStoreDefaultSettings, |
| 74 |
&map[errors.Field]string{ |
| 75 |
errors.FieldMessage: "Unable to read .browserpass.json of the password store", |
| 76 |
errors.FieldAction: "configure", |
| 77 |
errors.FieldError: err.Error(), |
| 78 |
errors.FieldStoreID: store.ID, |
| 79 |
errors.FieldStoreName: store.Name, |
| 80 |
errors.FieldStorePath: store.Path, |
| 81 |
}, |
| 82 |
) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Check whether a store in the default location exists and is accessible. |
| 87 |
// If there is at least one store in the settings, user will not use the default store => skip its validation. |
| 88 |
// However, if there are no stores in the settings, user expects to use the default password store => return an error if it is not accessible. |
| 89 |
if len(request.Settings.Stores) == 0 { |
| 90 |
possibleDefaultStorePath, err := getDefaultPasswordStorePath() |
| 91 |
if err != nil { |
| 92 |
log.Error("Unable to determine the location of the default password store: ", err) |
| 93 |
response.SendErrorAndExit( |
| 94 |
errors.CodeUnknownDefaultPasswordStoreLocation, |
| 95 |
&map[errors.Field]string{ |
| 96 |
errors.FieldMessage: "Unable to determine the location of the default password store", |
| 97 |
errors.FieldAction: "configure", |
| 98 |
errors.FieldError: err.Error(), |
| 99 |
}, |
| 100 |
) |
| 101 |
} else { |
| 102 |
responseData.DefaultStore.Path, err = normalizePasswordStorePath(possibleDefaultStorePath) |
| 103 |
if err != nil { |
| 104 |
log.Errorf( |
| 105 |
"The default password store is not accessible at the location '%v': %+v", |
| 106 |
possibleDefaultStorePath, err, |
| 107 |
) |
| 108 |
response.SendErrorAndExit( |
| 109 |
errors.CodeInaccessibleDefaultPasswordStore, |
| 110 |
&map[errors.Field]string{ |
| 111 |
errors.FieldMessage: "The default password store is not accessible", |
| 112 |
errors.FieldAction: "configure", |
| 113 |
errors.FieldError: err.Error(), |
| 114 |
errors.FieldStorePath: possibleDefaultStorePath, |
| 115 |
}, |
| 116 |
) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
responseData.DefaultStore.Settings, err = readDefaultSettings(responseData.DefaultStore.Path) |
| 121 |
if err == nil { |
| 122 |
var storeSettings StoreSettings |
| 123 |
err = json.Unmarshal([]byte(responseData.DefaultStore.Settings), &storeSettings) |
| 124 |
} |
| 125 |
if err != nil { |
| 126 |
log.Errorf( |
| 127 |
"Unable to read .browserpass.json of the default password store in '%v': %+v", |
| 128 |
responseData.DefaultStore.Path, err, |
| 129 |
) |
| 130 |
response.SendErrorAndExit( |
| 131 |
errors.CodeUnreadableDefaultPasswordStoreDefaultSettings, |
| 132 |
&map[errors.Field]string{ |
| 133 |
errors.FieldMessage: "Unable to read .browserpass.json of the default password store", |
| 134 |
errors.FieldAction: "configure", |
| 135 |
errors.FieldError: err.Error(), |
| 136 |
errors.FieldStorePath: responseData.DefaultStore.Path, |
| 137 |
}, |
| 138 |
) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
response.SendOk(responseData) |
| 143 |
} |
| 144 |
|
| 145 |
func getDefaultPasswordStorePath() (string, error) { |
| 146 |
path := os.Getenv("PASSWORD_STORE_DIR") |
| 147 |
if path != "" { |
| 148 |
return path, nil |
| 149 |
} |
| 150 |
|
| 151 |
usr, err := user.Current() |
| 152 |
if err != nil { |
| 153 |
return "", err |
| 154 |
} |
| 155 |
|
| 156 |
path = filepath.Join(usr.HomeDir, ".password-store") |
| 157 |
return path, nil |
| 158 |
} |
| 159 |
|
| 160 |
func readDefaultSettings(storePath string) (string, error) { |
| 161 |
content, err := ioutil.ReadFile(filepath.Join(storePath, ".browserpass.json")) |
| 162 |
if err == nil { |
| 163 |
return string(content), nil |
| 164 |
} |
| 165 |
if os.IsNotExist(err) { |
| 166 |
return "{}", nil |
| 167 |
} |
| 168 |
return "", err |
| 169 |
} |