env-util: replace unsetenv_erase() by new getenv_steal_erase() helper
The new helper combines a bunch of steps every invocation of unsetenv_erase() did so far: getenv() + strdup() + unsetenv_erase(). Let's unify this into one helper that is harder to use incorrectly. It's in inspired by TAKE_PTR() in a way: get the env var out and invalidate where it was before.
This commit is contained in:
committed by
Yu Watanabe
parent
5cf84d2545
commit
e99ca14741
@@ -30,12 +30,12 @@ int acquire_fido2_key(
|
||||
size_t *ret_decrypted_key_size,
|
||||
AskPasswordFlags ask_password_flags) {
|
||||
|
||||
_cleanup_(erase_and_freep) char *envpw = NULL;
|
||||
_cleanup_strv_free_erase_ char **pins = NULL;
|
||||
_cleanup_free_ void *loaded_salt = NULL;
|
||||
bool device_exists = false;
|
||||
const char *salt;
|
||||
size_t salt_size;
|
||||
char *e;
|
||||
int r;
|
||||
|
||||
ask_password_flags |= ASK_PASSWORD_PUSH_CACHE | ASK_PASSWORD_ACCEPT_CACHED;
|
||||
@@ -66,13 +66,13 @@ int acquire_fido2_key(
|
||||
salt = loaded_salt;
|
||||
}
|
||||
|
||||
e = getenv("PIN");
|
||||
if (e) {
|
||||
pins = strv_new(e);
|
||||
r = getenv_steal_erase("PIN", &envpw);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to acquire password from environment: %m");
|
||||
if (r > 0) {
|
||||
pins = strv_new(envpw);
|
||||
if (!pins)
|
||||
return log_oom();
|
||||
|
||||
assert_se(unsetenv_erase("PIN") >= 0);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
|
||||
@@ -818,20 +818,19 @@ static bool libcryptsetup_plugins_support(void) {
|
||||
|
||||
#if HAVE_LIBCRYPTSETUP_PLUGINS
|
||||
static int acquire_pins_from_env_variable(char ***ret_pins) {
|
||||
char *e;
|
||||
_cleanup_(erase_and_freep) char *envpin = NULL;
|
||||
_cleanup_strv_free_erase_ char **pins = NULL;
|
||||
int r;
|
||||
|
||||
assert(ret_pins);
|
||||
|
||||
e = getenv("PIN");
|
||||
if (e) {
|
||||
pins = strv_new(e);
|
||||
r = getenv_steal_erase("PIN", &envpin);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to acquire PIN from environment: %m");
|
||||
if (r > 0) {
|
||||
pins = strv_new(envpin);
|
||||
if (!pins)
|
||||
return log_oom();
|
||||
|
||||
string_erase(e);
|
||||
if (unsetenv("PIN") < 0)
|
||||
return log_error_errno(errno, "Failed to unset $PIN: %m");
|
||||
}
|
||||
|
||||
*ret_pins = TAKE_PTR(pins);
|
||||
|
||||
Reference in New Issue
Block a user