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