random-seed.c 4.78 KB
Newer Older
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
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2010 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>

#include "log.h"
#include "util.h"

31 32
#define POOL_SIZE_MIN 512

33 34
int main(int argc, char *argv[]) {
        int seed_fd = -1, random_fd = -1;
35
        int ret = EXIT_FAILURE;
36 37
        void* buf;
        size_t buf_size = 0;
38
        ssize_t r;
39
        FILE *f;
40 41 42

        if (argc != 2) {
                log_error("This program requires one argument.");
43
                return EXIT_FAILURE;
44 45
        }

46
        log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
47
        log_parse_environment();
48
        log_open();
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63
        /* Read pool size, if possible */
        if ((f = fopen("/proc/sys/kernel/random/poolsize", "re"))) {
                fscanf(f, "%zu", &buf_size);
                fclose(f);
        }

        if (buf_size <= POOL_SIZE_MIN)
                buf_size = POOL_SIZE_MIN;

        if (!(buf = malloc(buf_size))) {
                log_error("Failed to allocate buffer.");
                goto finish;
        }

64 65 66 67 68
        if (mkdir_parents(RANDOM_SEED, 0755) < 0) {
                log_error("Failed to create directories parents of %s: %m", RANDOM_SEED);
                goto finish;
        }

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        /* When we load the seed we read it and write it to the device
         * and then immediately update the saved seed with new data,
         * to make sure the next boot gets seeded differently. */

        if (streq(argv[1], "load")) {

                if ((seed_fd = open(RANDOM_SEED, O_RDWR|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600)) < 0) {
                        if ((seed_fd = open(RANDOM_SEED, O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) {
                                log_error("Failed to open random seed: %m");
                                goto finish;
                        }
                }

                if ((random_fd = open("/dev/urandom", O_RDWR|O_CLOEXEC|O_NOCTTY, 0600)) < 0) {
                        if ((random_fd = open("/dev/urandom", O_WRONLY|O_CLOEXEC|O_NOCTTY, 0600)) < 0) {
                                log_error("Failed to open /dev/urandom: %m");
                                goto finish;
                        }
                }

89 90 91 92 93
                if ((r = loop_read(seed_fd, buf, buf_size, false)) <= 0) {

                        if (r != 0)
                                log_error("Failed to read seed file: %m");
                } else {
94 95
                        lseek(seed_fd, 0, SEEK_SET);

96
                        if ((r = loop_write(random_fd, buf, (size_t) r, false)) <= 0)
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
                                log_error("Failed to write seed to /dev/random: %s", r < 0 ? strerror(errno) : "short write");
                }

        } else if (streq(argv[1], "save")) {

                if ((seed_fd = open(RANDOM_SEED, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600)) < 0) {
                        log_error("Failed to open random seed: %m");
                        goto finish;
                }

                if ((random_fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) {
                        log_error("Failed to open /dev/urandom: %m");
                        goto finish;
                }
        } else {
                log_error("Unknown verb %s.", argv[1]);
                goto finish;
        }

        /* This is just a safety measure. Given that we are root and
         * most likely created the file ourselves the mode and owner
         * should be correct anyway. */
        fchmod(seed_fd, 0600);
        fchown(seed_fd, 0, 0);

122
        if ((r = loop_read(random_fd, buf, buf_size, false)) <= 0)
123 124
                log_error("Failed to read new seed from /dev/urandom: %s", r < 0 ? strerror(errno) : "EOF");
        else {
125
                if ((r = loop_write(seed_fd, buf, (size_t) r, false)) <= 0)
126 127 128
                        log_error("Failed to write new random seed file: %s", r < 0 ? strerror(errno) : "short write");
        }

129
        ret = EXIT_SUCCESS;
130 131 132 133 134 135 136 137

finish:
        if (random_fd >= 0)
                close_nointr_nofail(random_fd);

        if (seed_fd >= 0)
                close_nointr_nofail(seed_fd);

138 139
        free(buf);

140 141
        return ret;
}