]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/random-seed/random-seed.c
Merge pull request #1359 from jengelh/ue
[thirdparty/systemd.git] / src / random-seed / 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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28 #include "alloc-util.h"
29 #include "fd-util.h"
30 #include "io-util.h"
31 #include "log.h"
32 #include "mkdir.h"
33 #include "string-util.h"
34 #include "util.h"
35
36 #define POOL_SIZE_MIN 512
37
38 int main(int argc, char *argv[]) {
39 _cleanup_close_ int seed_fd = -1, random_fd = -1;
40 _cleanup_free_ void* buf = NULL;
41 size_t buf_size = 0;
42 ssize_t k;
43 int r;
44 FILE *f;
45 bool refresh_seed_file = true;
46
47 if (argc != 2) {
48 log_error("This program requires one argument.");
49 return EXIT_FAILURE;
50 }
51
52 log_set_target(LOG_TARGET_AUTO);
53 log_parse_environment();
54 log_open();
55
56 umask(0022);
57
58 /* Read pool size, if possible */
59 f = fopen("/proc/sys/kernel/random/poolsize", "re");
60 if (f) {
61 if (fscanf(f, "%zu", &buf_size) > 0)
62 /* poolsize is in bits on 2.6, but we want bytes */
63 buf_size /= 8;
64
65 fclose(f);
66 }
67
68 if (buf_size <= POOL_SIZE_MIN)
69 buf_size = POOL_SIZE_MIN;
70
71 buf = malloc(buf_size);
72 if (!buf) {
73 r = log_oom();
74 goto finish;
75 }
76
77 r = mkdir_parents_label(RANDOM_SEED, 0755);
78 if (r < 0) {
79 log_error_errno(r, "Failed to create directory " RANDOM_SEED_DIR ": %m");
80 goto finish;
81 }
82
83 /* When we load the seed we read it and write it to the device
84 * and then immediately update the saved seed with new data,
85 * to make sure the next boot gets seeded differently. */
86
87 if (streq(argv[1], "load")) {
88
89 seed_fd = open(RANDOM_SEED, O_RDWR|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600);
90 if (seed_fd < 0) {
91 seed_fd = open(RANDOM_SEED, O_RDONLY|O_CLOEXEC|O_NOCTTY);
92 if (seed_fd < 0) {
93 r = log_error_errno(errno, "Failed to open " RANDOM_SEED ": %m");
94 goto finish;
95 }
96
97 refresh_seed_file = false;
98 }
99
100 random_fd = open("/dev/urandom", O_RDWR|O_CLOEXEC|O_NOCTTY, 0600);
101 if (random_fd < 0) {
102 random_fd = open("/dev/urandom", O_WRONLY|O_CLOEXEC|O_NOCTTY, 0600);
103 if (random_fd < 0) {
104 r = log_error_errno(errno, "Failed to open /dev/urandom: %m");
105 goto finish;
106 }
107 }
108
109 k = loop_read(seed_fd, buf, buf_size, false);
110 if (k < 0)
111 r = log_error_errno(k, "Failed to read seed from " RANDOM_SEED ": %m");
112 else if (k == 0)
113 log_debug("Seed file " RANDOM_SEED " not yet initialized, proceeding.");
114 else {
115 (void) lseek(seed_fd, 0, SEEK_SET);
116
117 r = loop_write(random_fd, buf, (size_t) k, false);
118 if (r < 0)
119 log_error_errno(r, "Failed to write seed to /dev/urandom: %m");
120 }
121
122 } else if (streq(argv[1], "save")) {
123
124 seed_fd = open(RANDOM_SEED, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600);
125 if (seed_fd < 0) {
126 r = log_error_errno(errno, "Failed to open " RANDOM_SEED ": %m");
127 goto finish;
128 }
129
130 random_fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
131 if (random_fd < 0) {
132 r = log_error_errno(errno, "Failed to open /dev/urandom: %m");
133 goto finish;
134 }
135
136 } else {
137 log_error("Unknown verb '%s'.", argv[1]);
138 r = -EINVAL;
139 goto finish;
140 }
141
142 if (refresh_seed_file) {
143
144 /* This is just a safety measure. Given that we are root and
145 * most likely created the file ourselves the mode and owner
146 * should be correct anyway. */
147 (void) fchmod(seed_fd, 0600);
148 (void) fchown(seed_fd, 0, 0);
149
150 k = loop_read(random_fd, buf, buf_size, false);
151 if (k < 0) {
152 r = log_error_errno(k, "Failed to read new seed from /dev/urandom: %m");
153 goto finish;
154 }
155 if (k == 0) {
156 log_error("Got EOF while reading from /dev/urandom.");
157 r = -EIO;
158 goto finish;
159 }
160
161 r = loop_write(seed_fd, buf, (size_t) k, false);
162 if (r < 0)
163 log_error_errno(r, "Failed to write new random seed file: %m");
164 }
165
166 finish:
167 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
168 }