rndctl.c revision 1.17 1 1.17 christos /* $NetBSD: rndctl.c,v 1.17 2005/06/27 01:00:06 christos Exp $ */
2 1.3 perry
3 1.1 explorer /*-
4 1.1 explorer * Copyright (c) 1997 Michael Graff.
5 1.1 explorer * All rights reserved.
6 1.1 explorer *
7 1.1 explorer * Redistribution and use in source and binary forms, with or without
8 1.1 explorer * modification, are permitted provided that the following conditions
9 1.1 explorer * are met:
10 1.1 explorer * 1. Redistributions of source code must retain the above copyright
11 1.1 explorer * notice, this list of conditions and the following disclaimer.
12 1.1 explorer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 explorer * notice, this list of conditions and the following disclaimer in the
14 1.1 explorer * documentation and/or other materials provided with the distribution.
15 1.1 explorer * 3. Neither the name of the author nor the names of other contributors
16 1.1 explorer * may be used to endorse or promote products derived from this software
17 1.1 explorer * without specific prior written permission.
18 1.1 explorer *
19 1.1 explorer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 explorer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 explorer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 explorer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 explorer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 1.1 explorer * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.1 explorer * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 1.1 explorer * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 1.1 explorer * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 explorer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 explorer * SUCH DAMAGE.
30 1.1 explorer */
31 1.15 agc #include <sys/cdefs.h>
32 1.15 agc
33 1.15 agc #ifndef lint
34 1.17 christos __RCSID("$NetBSD: rndctl.c,v 1.17 2005/06/27 01:00:06 christos Exp $");
35 1.15 agc #endif
36 1.15 agc
37 1.1 explorer
38 1.11 enami #include <sys/types.h>
39 1.11 enami #include <sys/ioctl.h>
40 1.11 enami #include <sys/rnd.h>
41 1.11 enami
42 1.1 explorer #include <stdio.h>
43 1.1 explorer #include <stdlib.h>
44 1.1 explorer #include <unistd.h>
45 1.1 explorer #include <fcntl.h>
46 1.1 explorer #include <errno.h>
47 1.1 explorer #include <err.h>
48 1.2 explorer #include <string.h>
49 1.1 explorer
50 1.1 explorer typedef struct {
51 1.17 christos const char *a_name;
52 1.9 enami u_int32_t a_type;
53 1.1 explorer } arg_t;
54 1.1 explorer
55 1.1 explorer arg_t source_types[] = {
56 1.6 sommerfe { "???", RND_TYPE_UNKNOWN },
57 1.1 explorer { "disk", RND_TYPE_DISK },
58 1.1 explorer { "net", RND_TYPE_NET },
59 1.1 explorer { "tape", RND_TYPE_TAPE },
60 1.1 explorer { "tty", RND_TYPE_TTY },
61 1.11 enami { "rng", RND_TYPE_RNG },
62 1.1 explorer { NULL, 0 }
63 1.1 explorer };
64 1.1 explorer
65 1.2 explorer static void usage(void);
66 1.2 explorer u_int32_t find_type(char *name);
67 1.17 christos const char *find_name(u_int32_t);
68 1.2 explorer void do_ioctl(rndctl_t *);
69 1.2 explorer char * strflags(u_int32_t);
70 1.2 explorer void do_list(int, u_int32_t, char *);
71 1.6 sommerfe void do_stats(void);
72 1.2 explorer
73 1.2 explorer static void
74 1.1 explorer usage(void)
75 1.1 explorer {
76 1.9 enami
77 1.11 enami fprintf(stderr, "usage: %s -CEce [-t devtype] [-d devname]\n",
78 1.11 enami getprogname());
79 1.11 enami fprintf(stderr, " %s -ls [-t devtype] [-d devname]\n",
80 1.11 enami getprogname());
81 1.5 mycroft exit(1);
82 1.1 explorer }
83 1.1 explorer
84 1.1 explorer u_int32_t
85 1.1 explorer find_type(char *name)
86 1.1 explorer {
87 1.1 explorer arg_t *a;
88 1.1 explorer
89 1.1 explorer a = source_types;
90 1.9 enami
91 1.9 enami while (a->a_name != NULL) {
92 1.9 enami if (strcmp(a->a_name, name) == 0)
93 1.9 enami return (a->a_type);
94 1.1 explorer a++;
95 1.1 explorer }
96 1.1 explorer
97 1.10 enami errx(1, "device name %s unknown", name);
98 1.9 enami return (0);
99 1.1 explorer }
100 1.1 explorer
101 1.17 christos const char *
102 1.1 explorer find_name(u_int32_t type)
103 1.1 explorer {
104 1.1 explorer arg_t *a;
105 1.1 explorer
106 1.1 explorer a = source_types;
107 1.9 enami
108 1.9 enami while (a->a_name != NULL) {
109 1.9 enami if (type == a->a_type)
110 1.9 enami return (a->a_name);
111 1.1 explorer a++;
112 1.1 explorer }
113 1.1 explorer
114 1.10 enami warnx("device type %u unknown", type);
115 1.10 enami return ("???");
116 1.1 explorer }
117 1.1 explorer
118 1.1 explorer void
119 1.1 explorer do_ioctl(rndctl_t *rctl)
120 1.1 explorer {
121 1.1 explorer int fd;
122 1.1 explorer int res;
123 1.1 explorer
124 1.1 explorer fd = open("/dev/urandom", O_RDONLY, 0644);
125 1.1 explorer if (fd < 0)
126 1.1 explorer err(1, "open");
127 1.1 explorer
128 1.1 explorer res = ioctl(fd, RNDCTL, rctl);
129 1.1 explorer if (res < 0)
130 1.1 explorer err(1, "ioctl(RNDCTL)");
131 1.1 explorer
132 1.1 explorer close(fd);
133 1.1 explorer }
134 1.1 explorer
135 1.1 explorer char *
136 1.1 explorer strflags(u_int32_t fl)
137 1.1 explorer {
138 1.1 explorer static char str[512];
139 1.1 explorer
140 1.1 explorer str[0] = 0;
141 1.1 explorer if (fl & RND_FLAG_NO_ESTIMATE)
142 1.6 sommerfe ;
143 1.9 enami else
144 1.16 itojun strlcat(str, "estimate", sizeof(str));
145 1.9 enami
146 1.1 explorer if (fl & RND_FLAG_NO_COLLECT)
147 1.6 sommerfe ;
148 1.6 sommerfe else {
149 1.6 sommerfe if (str[0])
150 1.16 itojun strlcat(str, ", ", sizeof(str));
151 1.16 itojun strlcat(str, "collect", sizeof(str));
152 1.6 sommerfe }
153 1.9 enami
154 1.9 enami return (str);
155 1.1 explorer }
156 1.1 explorer
157 1.6 sommerfe #define HEADER "Source Bits Type Flags\n"
158 1.1 explorer
159 1.1 explorer void
160 1.1 explorer do_list(int all, u_int32_t type, char *name)
161 1.1 explorer {
162 1.9 enami rndstat_t rstat;
163 1.9 enami rndstat_name_t rstat_name;
164 1.9 enami int fd;
165 1.9 enami int res;
166 1.9 enami u_int32_t start;
167 1.1 explorer
168 1.1 explorer fd = open("/dev/urandom", O_RDONLY, 0644);
169 1.1 explorer if (fd < 0)
170 1.1 explorer err(1, "open");
171 1.1 explorer
172 1.1 explorer if (all == 0 && type == 0xff) {
173 1.14 itojun strncpy(rstat_name.name, name, sizeof(rstat_name.name));
174 1.1 explorer res = ioctl(fd, RNDGETSRCNAME, &rstat_name);
175 1.1 explorer if (res < 0)
176 1.1 explorer err(1, "ioctl(RNDGETSRCNAME)");
177 1.1 explorer printf(HEADER);
178 1.6 sommerfe printf("%-16s %10u %-4s %s\n",
179 1.9 enami rstat_name.source.name,
180 1.9 enami rstat_name.source.total,
181 1.9 enami find_name(rstat_name.source.type),
182 1.9 enami strflags(rstat_name.source.flags));
183 1.1 explorer close(fd);
184 1.1 explorer return;
185 1.1 explorer }
186 1.1 explorer
187 1.1 explorer /*
188 1.9 enami * Run through all the devices present in the system, and either
189 1.1 explorer * print out ones that match, or print out all of them.
190 1.1 explorer */
191 1.1 explorer printf(HEADER);
192 1.1 explorer start = 0;
193 1.1 explorer for (;;) {
194 1.1 explorer rstat.count = RND_MAXSTATCOUNT;
195 1.1 explorer rstat.start = start;
196 1.1 explorer res = ioctl(fd, RNDGETSRCNUM, &rstat);
197 1.1 explorer if (res < 0)
198 1.1 explorer err(1, "ioctl(RNDGETSRCNUM)");
199 1.9 enami
200 1.1 explorer if (rstat.count == 0)
201 1.1 explorer break;
202 1.9 enami
203 1.9 enami for (res = 0; res < rstat.count; res++) {
204 1.9 enami if (all != 0 ||
205 1.9 enami type == rstat.source[res].type)
206 1.6 sommerfe printf("%-16s %10u %-4s %s\n",
207 1.9 enami rstat.source[res].name,
208 1.9 enami rstat.source[res].total,
209 1.9 enami find_name(rstat.source[res].type),
210 1.9 enami strflags(rstat.source[res].flags));
211 1.1 explorer }
212 1.1 explorer start += rstat.count;
213 1.1 explorer }
214 1.1 explorer
215 1.1 explorer close(fd);
216 1.1 explorer }
217 1.1 explorer
218 1.6 sommerfe void
219 1.6 sommerfe do_stats()
220 1.6 sommerfe {
221 1.6 sommerfe rndpoolstat_t rs;
222 1.6 sommerfe int fd;
223 1.9 enami
224 1.6 sommerfe fd = open("/dev/urandom", O_RDONLY, 0644);
225 1.6 sommerfe if (fd < 0)
226 1.6 sommerfe err(1, "open");
227 1.9 enami
228 1.6 sommerfe if (ioctl(fd, RNDGETPOOLSTAT, &rs) < 0)
229 1.6 sommerfe err(1, "ioctl(RNDGETPOOLSTAT)");
230 1.6 sommerfe
231 1.12 enami printf("\t%9u bits mixed into pool\n", rs.added);
232 1.12 enami printf("\t%9u bits currently stored in pool (max %u)\n",
233 1.6 sommerfe rs.curentropy, rs.maxentropy);
234 1.12 enami printf("\t%9u bits of entropy discarded due to full pool\n",
235 1.6 sommerfe rs.discarded);
236 1.12 enami printf("\t%9u hard-random bits generated\n", rs.removed);
237 1.12 enami printf("\t%9u pseudo-random bits generated\n", rs.generated);
238 1.6 sommerfe
239 1.6 sommerfe close(fd);
240 1.6 sommerfe }
241 1.6 sommerfe
242 1.1 explorer int
243 1.1 explorer main(int argc, char **argv)
244 1.1 explorer {
245 1.9 enami rndctl_t rctl;
246 1.9 enami int ch, cmd, lflag, mflag, sflag;
247 1.1 explorer u_int32_t type;
248 1.9 enami char name[16];
249 1.1 explorer
250 1.1 explorer rctl.mask = 0;
251 1.1 explorer rctl.flags = 0;
252 1.1 explorer
253 1.1 explorer cmd = 0;
254 1.1 explorer lflag = 0;
255 1.1 explorer mflag = 0;
256 1.7 joda sflag = 0;
257 1.2 explorer type = 0xff;
258 1.1 explorer
259 1.6 sommerfe while ((ch = getopt(argc, argv, "CEcelt:d:s")) != -1)
260 1.9 enami switch (ch) {
261 1.1 explorer case 'C':
262 1.1 explorer rctl.flags |= RND_FLAG_NO_COLLECT;
263 1.1 explorer rctl.mask |= RND_FLAG_NO_COLLECT;
264 1.1 explorer mflag++;
265 1.1 explorer break;
266 1.1 explorer case 'E':
267 1.1 explorer rctl.flags |= RND_FLAG_NO_ESTIMATE;
268 1.1 explorer rctl.mask |= RND_FLAG_NO_ESTIMATE;
269 1.1 explorer mflag++;
270 1.1 explorer break;
271 1.1 explorer case 'c':
272 1.1 explorer rctl.flags &= ~RND_FLAG_NO_COLLECT;
273 1.1 explorer rctl.mask |= RND_FLAG_NO_COLLECT;
274 1.1 explorer mflag++;
275 1.1 explorer break;
276 1.1 explorer case 'e':
277 1.1 explorer rctl.flags &= ~RND_FLAG_NO_ESTIMATE;
278 1.1 explorer rctl.mask |= RND_FLAG_NO_ESTIMATE;
279 1.1 explorer mflag++;
280 1.1 explorer break;
281 1.1 explorer case 'l':
282 1.1 explorer lflag++;
283 1.1 explorer break;
284 1.1 explorer case 't':
285 1.1 explorer if (cmd != 0)
286 1.1 explorer usage();
287 1.1 explorer cmd = 't';
288 1.1 explorer
289 1.1 explorer type = find_type(optarg);
290 1.1 explorer break;
291 1.1 explorer case 'd':
292 1.1 explorer if (cmd != 0)
293 1.1 explorer usage();
294 1.1 explorer cmd = 'd';
295 1.1 explorer
296 1.1 explorer type = 0xff;
297 1.14 itojun strlcpy(name, optarg, sizeof(name));
298 1.1 explorer break;
299 1.6 sommerfe case 's':
300 1.6 sommerfe sflag++;
301 1.6 sommerfe break;
302 1.1 explorer case '?':
303 1.1 explorer default:
304 1.1 explorer usage();
305 1.1 explorer }
306 1.1 explorer
307 1.1 explorer /*
308 1.9 enami * Cannot list and modify at the same time.
309 1.1 explorer */
310 1.6 sommerfe if ((lflag != 0 || sflag != 0) && mflag != 0)
311 1.1 explorer usage();
312 1.1 explorer
313 1.1 explorer /*
314 1.9 enami * Bomb out on no-ops.
315 1.1 explorer */
316 1.6 sommerfe if (lflag == 0 && mflag == 0 && sflag == 0)
317 1.1 explorer usage();
318 1.1 explorer
319 1.1 explorer /*
320 1.9 enami * If not listing, we need a device name or a type.
321 1.1 explorer */
322 1.6 sommerfe if (lflag == 0 && cmd == 0 && sflag == 0)
323 1.1 explorer usage();
324 1.1 explorer
325 1.1 explorer /*
326 1.9 enami * Modify request.
327 1.1 explorer */
328 1.1 explorer if (mflag != 0) {
329 1.1 explorer rctl.type = type;
330 1.14 itojun strncpy(rctl.name, name, sizeof(rctl.name));
331 1.1 explorer do_ioctl(&rctl);
332 1.1 explorer
333 1.1 explorer exit(0);
334 1.1 explorer }
335 1.1 explorer
336 1.1 explorer /*
337 1.9 enami * List sources.
338 1.1 explorer */
339 1.1 explorer if (lflag != 0)
340 1.1 explorer do_list(cmd == 0, type, name);
341 1.1 explorer
342 1.6 sommerfe if (sflag != 0)
343 1.6 sommerfe do_stats();
344 1.9 enami
345 1.9 enami exit(0);
346 1.1 explorer }
347