seedrng: more improvements
- drop initialization of some variables. - use eerrorx where possible - drop final cleanup instructions
This commit is contained in:
parent
9b16bfe0e7
commit
c0f5586743
@ -454,8 +454,8 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
static const char seedrng_prefix[] = "SeedRNG v1 Old+New Prefix";
|
static const char seedrng_prefix[] = "SeedRNG v1 Old+New Prefix";
|
||||||
static const char seedrng_failure[] = "SeedRNG v1 No New Seed Failure";
|
static const char seedrng_failure[] = "SeedRNG v1 No New Seed Failure";
|
||||||
int opt, fd = -1, dfd = -1, program_ret = 0;
|
int opt, fd, dfd, program_ret = 0;
|
||||||
char *seed_dir = NULL;
|
char *seed_dir;
|
||||||
uint8_t new_seed[MAX_SEED_LEN];
|
uint8_t new_seed[MAX_SEED_LEN];
|
||||||
size_t new_seed_len;
|
size_t new_seed_len;
|
||||||
bool new_seed_creditable;
|
bool new_seed_creditable;
|
||||||
@ -480,11 +480,8 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
if (!seed_dir)
|
if (!seed_dir)
|
||||||
seed_dir = xstrdup(DEFAULT_SEED_DIR);
|
seed_dir = xstrdup(DEFAULT_SEED_DIR);
|
||||||
if (getuid()) {
|
if (getuid())
|
||||||
eerror("%s: superuser access is required", applet);
|
eerrorx("%s: superuser access is required", applet);
|
||||||
program_ret = 1;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
umask(0077);
|
umask(0077);
|
||||||
|
|
||||||
blake2s_init(&hash, BLAKE2S_HASH_LEN);
|
blake2s_init(&hash, BLAKE2S_HASH_LEN);
|
||||||
@ -494,18 +491,12 @@ int main(int argc, char **argv)
|
|||||||
blake2s_update(&hash, &realtime, sizeof(realtime));
|
blake2s_update(&hash, &realtime, sizeof(realtime));
|
||||||
blake2s_update(&hash, &boottime, sizeof(boottime));
|
blake2s_update(&hash, &boottime, sizeof(boottime));
|
||||||
|
|
||||||
if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST) {
|
if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST)
|
||||||
eerror("%s: Unable to create seed directory: %s", applet, strerror(errno));
|
eerrorx("%s: Unable to create seed directory: %s", applet, strerror(errno));
|
||||||
program_ret = 1;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
dfd = open(seed_dir, O_DIRECTORY | O_RDONLY);
|
dfd = open(seed_dir, O_DIRECTORY | O_RDONLY);
|
||||||
if (dfd < 0 || flock(dfd, LOCK_EX) < 0) {
|
if (dfd < 0 || flock(dfd, LOCK_EX) < 0)
|
||||||
eerror("%s: Unable to lock seed directory: %s", applet, strerror(errno));
|
eerrorx("%s: Unable to lock seed directory: %s", applet, strerror(errno));
|
||||||
program_ret = 1;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (seed_from_file_if_exists(NON_CREDITABLE_SEED, dfd, false, &hash) < 0)
|
if (seed_from_file_if_exists(NON_CREDITABLE_SEED, dfd, false, &hash) < 0)
|
||||||
program_ret |= 1 << 1;
|
program_ret |= 1 << 1;
|
||||||
@ -527,23 +518,15 @@ int main(int argc, char **argv)
|
|||||||
fd = openat(dfd, NON_CREDITABLE_SEED, O_WRONLY | O_CREAT | O_TRUNC, 0400);
|
fd = openat(dfd, NON_CREDITABLE_SEED, O_WRONLY | O_CREAT | O_TRUNC, 0400);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
eerror("%s: Unable to open seed file for writing: %s", applet, strerror(errno));
|
eerror("%s: Unable to open seed file for writing: %s", applet, strerror(errno));
|
||||||
program_ret |= 1 << 4;
|
return program_ret | (1 << 4);
|
||||||
goto out;
|
|
||||||
}
|
}
|
||||||
if (write_full(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || fsync(fd) < 0) {
|
if (write_full(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || fsync(fd) < 0) {
|
||||||
eerror("%s: Unable to write seed file: %s", applet, strerror(errno));
|
eerror("%s: Unable to write seed file: %s", applet, strerror(errno));
|
||||||
program_ret |= 1 << 5;
|
return program_ret | (1 << 5);
|
||||||
goto out;
|
|
||||||
}
|
}
|
||||||
if (new_seed_creditable && renameat(dfd, NON_CREDITABLE_SEED, dfd, CREDITABLE_SEED) < 0) {
|
if (new_seed_creditable && renameat(dfd, NON_CREDITABLE_SEED, dfd, CREDITABLE_SEED) < 0) {
|
||||||
ewarn("%s: Unable to make new seed creditable: %s", applet, strerror(errno));
|
ewarn("%s: Unable to make new seed creditable: %s", applet, strerror(errno));
|
||||||
program_ret |= 1 << 6;
|
return program_ret | (1 << 6);
|
||||||
}
|
}
|
||||||
out:
|
|
||||||
if (fd >= 0)
|
|
||||||
close(fd);
|
|
||||||
if (dfd >= 0)
|
|
||||||
close(dfd);
|
|
||||||
free(seed_dir);
|
|
||||||
return program_ret;
|
return program_ret;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user