chiark / gitweb /
91aa6c6b4e6917578664cf9fe0c2f7347db5e51c
[elogind.git] / src / random-seed.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/stat.h>
27
28 #include "log.h"
29 #include "util.h"
30
31 int main(int argc, char *argv[]) {
32         int seed_fd = -1, random_fd = -1;
33         int ret = 1;
34         uint8_t buf[512];
35         ssize_t r;
36
37         if (argc != 2) {
38                 log_error("This program requires one argument.");
39                 return 1;
40         }
41
42         log_parse_environment();
43
44         /* When we load the seed we read it and write it to the device
45          * and then immediately update the saved seed with new data,
46          * to make sure the next boot gets seeded differently. */
47
48         if (streq(argv[1], "load")) {
49
50                 if ((seed_fd = open(RANDOM_SEED, O_RDWR|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600)) < 0) {
51                         if ((seed_fd = open(RANDOM_SEED, O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) {
52                                 log_error("Failed to open random seed: %m");
53                                 goto finish;
54                         }
55                 }
56
57                 if ((random_fd = open("/dev/urandom", O_RDWR|O_CLOEXEC|O_NOCTTY, 0600)) < 0) {
58                         if ((random_fd = open("/dev/urandom", O_WRONLY|O_CLOEXEC|O_NOCTTY, 0600)) < 0) {
59                                 log_error("Failed to open /dev/urandom: %m");
60                                 goto finish;
61                         }
62                 }
63
64                 if ((r = loop_read(seed_fd, buf, sizeof(buf), false)) != sizeof(buf))
65                         log_error("Failed to read seed file: %s", r < 0 ? strerror(errno) : "EOF");
66                 else {
67                         lseek(seed_fd, 0, SEEK_SET);
68
69                         if ((r = loop_write(random_fd, buf, sizeof(buf), false)) != sizeof(buf))
70                                 log_error("Failed to write seed to /dev/random: %s", r < 0 ? strerror(errno) : "short write");
71                 }
72
73         } else if (streq(argv[1], "save")) {
74
75                 if ((seed_fd = open(RANDOM_SEED, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600)) < 0) {
76                         log_error("Failed to open random seed: %m");
77                         goto finish;
78                 }
79
80                 if ((random_fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) {
81                         log_error("Failed to open /dev/urandom: %m");
82                         goto finish;
83                 }
84         } else {
85                 log_error("Unknown verb %s.", argv[1]);
86                 goto finish;
87         }
88
89         /* This is just a safety measure. Given that we are root and
90          * most likely created the file ourselves the mode and owner
91          * should be correct anyway. */
92         fchmod(seed_fd, 0600);
93         fchown(seed_fd, 0, 0);
94
95         if ((r = loop_read(random_fd, buf, sizeof(buf), false)) != sizeof(buf))
96                 log_error("Failed to read new seed from /dev/urandom: %s", r < 0 ? strerror(errno) : "EOF");
97         else {
98                 if ((r = loop_write(seed_fd, buf, sizeof(buf), false)) != sizeof(buf))
99                         log_error("Failed to write new random seed file: %s", r < 0 ? strerror(errno) : "short write");
100         }
101
102         ret = 0;
103
104 finish:
105         if (random_fd >= 0)
106                 close_nointr_nofail(random_fd);
107
108         if (seed_fd >= 0)
109                 close_nointr_nofail(seed_fd);
110
111         return ret;
112 }