NetBSD/sbin/rndctl/rndctl.c

357 lines
7.1 KiB
C
Raw Normal View History

2009-04-05 16:06:33 +04:00
/* $NetBSD: rndctl.c,v 1.19 2009/04/05 12:06:33 lukem Exp $ */
1998-01-09 11:03:16 +03:00
1997-10-13 07:58:05 +04:00
/*-
* Copyright (c) 1997 Michael Graff.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of other contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
2003-06-23 15:53:35 +04:00
#include <sys/cdefs.h>
#ifndef lint
2009-04-05 16:06:33 +04:00
__RCSID("$NetBSD: rndctl.c,v 1.19 2009/04/05 12:06:33 lukem Exp $");
2003-06-23 15:53:35 +04:00
#endif
1997-10-13 07:58:05 +04:00
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/rnd.h>
1997-10-13 07:58:05 +04:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <err.h>
1997-10-13 08:05:58 +04:00
#include <string.h>
1997-10-13 07:58:05 +04:00
typedef struct {
const char *a_name;
2001-09-09 03:20:37 +04:00
u_int32_t a_type;
1997-10-13 07:58:05 +04:00
} arg_t;
arg_t source_types[] = {
{ "???", RND_TYPE_UNKNOWN },
1997-10-13 07:58:05 +04:00
{ "disk", RND_TYPE_DISK },
{ "net", RND_TYPE_NET },
1997-10-13 07:58:05 +04:00
{ "tape", RND_TYPE_TAPE },
{ "tty", RND_TYPE_TTY },
{ "rng", RND_TYPE_RNG },
1997-10-13 07:58:05 +04:00
{ NULL, 0 }
};
1997-10-13 08:05:58 +04:00
static void usage(void);
u_int32_t find_type(char *name);
const char *find_name(u_int32_t);
1997-10-13 08:05:58 +04:00
void do_ioctl(rndctl_t *);
char * strflags(u_int32_t);
void do_list(int, u_int32_t, char *);
void do_stats(void);
1997-10-13 08:05:58 +04:00
static void
1997-10-13 07:58:05 +04:00
usage(void)
{
2001-09-09 03:20:37 +04:00
fprintf(stderr, "usage: %s -CEce [-d devname | -t devtype]\n",
getprogname());
fprintf(stderr, " %s -ls [-d devname | -t devtype]\n",
getprogname());
exit(1);
1997-10-13 07:58:05 +04:00
}
u_int32_t
find_type(char *name)
{
arg_t *a;
a = source_types;
2001-09-09 03:20:37 +04:00
while (a->a_name != NULL) {
if (strcmp(a->a_name, name) == 0)
return (a->a_type);
1997-10-13 07:58:05 +04:00
a++;
}
errx(1, "device name %s unknown", name);
2001-09-09 03:20:37 +04:00
return (0);
1997-10-13 07:58:05 +04:00
}
const char *
1997-10-13 07:58:05 +04:00
find_name(u_int32_t type)
{
arg_t *a;
a = source_types;
2001-09-09 03:20:37 +04:00
while (a->a_name != NULL) {
if (type == a->a_type)
return (a->a_name);
1997-10-13 07:58:05 +04:00
a++;
}
warnx("device type %u unknown", type);
return ("???");
1997-10-13 07:58:05 +04:00
}
void
do_ioctl(rndctl_t *rctl)
{
int fd;
int res;
fd = open("/dev/urandom", O_RDONLY, 0644);
if (fd < 0)
err(1, "open");
res = ioctl(fd, RNDCTL, rctl);
if (res < 0)
err(1, "ioctl(RNDCTL)");
close(fd);
}
char *
strflags(u_int32_t fl)
{
static char str[512];
str[0] = 0;
if (fl & RND_FLAG_NO_ESTIMATE)
;
2001-09-09 03:20:37 +04:00
else
2003-07-13 11:59:24 +04:00
strlcat(str, "estimate", sizeof(str));
2001-09-09 03:20:37 +04:00
1997-10-13 07:58:05 +04:00
if (fl & RND_FLAG_NO_COLLECT)
;
else {
if (str[0])
2003-07-13 11:59:24 +04:00
strlcat(str, ", ", sizeof(str));
strlcat(str, "collect", sizeof(str));
}
2001-09-09 03:20:37 +04:00
return (str);
1997-10-13 07:58:05 +04:00
}
#define HEADER "Source Bits Type Flags\n"
1997-10-13 07:58:05 +04:00
void
do_list(int all, u_int32_t type, char *name)
{
2001-09-09 03:20:37 +04:00
rndstat_t rstat;
rndstat_name_t rstat_name;
int fd;
int res;
2009-04-05 16:06:33 +04:00
uint32_t i;
2001-09-09 03:20:37 +04:00
u_int32_t start;
1997-10-13 07:58:05 +04:00
fd = open("/dev/urandom", O_RDONLY, 0644);
if (fd < 0)
err(1, "open");
if (all == 0 && type == 0xff) {
2003-05-18 03:16:47 +04:00
strncpy(rstat_name.name, name, sizeof(rstat_name.name));
1997-10-13 07:58:05 +04:00
res = ioctl(fd, RNDGETSRCNAME, &rstat_name);
if (res < 0)
err(1, "ioctl(RNDGETSRCNAME)");
printf(HEADER);
printf("%-16s %10u %-4s %s\n",
2001-09-09 03:20:37 +04:00
rstat_name.source.name,
rstat_name.source.total,
find_name(rstat_name.source.type),
strflags(rstat_name.source.flags));
1997-10-13 07:58:05 +04:00
close(fd);
return;
}
/*
2001-09-09 03:20:37 +04:00
* Run through all the devices present in the system, and either
1997-10-13 07:58:05 +04:00
* print out ones that match, or print out all of them.
*/
printf(HEADER);
start = 0;
for (;;) {
rstat.count = RND_MAXSTATCOUNT;
rstat.start = start;
res = ioctl(fd, RNDGETSRCNUM, &rstat);
if (res < 0)
err(1, "ioctl(RNDGETSRCNUM)");
2001-09-09 03:20:37 +04:00
1997-10-13 07:58:05 +04:00
if (rstat.count == 0)
break;
2001-09-09 03:20:37 +04:00
2009-04-05 16:06:33 +04:00
for (i = 0; i < rstat.count; i++) {
2001-09-09 03:20:37 +04:00
if (all != 0 ||
2009-04-05 16:06:33 +04:00
type == rstat.source[i].type)
printf("%-16s %10u %-4s %s\n",
2009-04-05 16:06:33 +04:00
rstat.source[i].name,
rstat.source[i].total,
find_name(rstat.source[i].type),
strflags(rstat.source[i].flags));
1997-10-13 07:58:05 +04:00
}
start += rstat.count;
}
close(fd);
}
void
do_stats()
{
rndpoolstat_t rs;
int fd;
2001-09-09 03:20:37 +04:00
fd = open("/dev/urandom", O_RDONLY, 0644);
if (fd < 0)
err(1, "open");
2001-09-09 03:20:37 +04:00
if (ioctl(fd, RNDGETPOOLSTAT, &rs) < 0)
err(1, "ioctl(RNDGETPOOLSTAT)");
printf("\t%9u bits mixed into pool\n", rs.added);
printf("\t%9u bits currently stored in pool (max %u)\n",
rs.curentropy, rs.maxentropy);
printf("\t%9u bits of entropy discarded due to full pool\n",
rs.discarded);
printf("\t%9u hard-random bits generated\n", rs.removed);
printf("\t%9u pseudo-random bits generated\n", rs.generated);
close(fd);
}
1997-10-13 07:58:05 +04:00
int
main(int argc, char **argv)
{
2001-09-09 03:20:37 +04:00
rndctl_t rctl;
int ch, cmd, lflag, mflag, sflag;
1997-10-13 07:58:05 +04:00
u_int32_t type;
2001-09-09 03:20:37 +04:00
char name[16];
1997-10-13 07:58:05 +04:00
rctl.mask = 0;
rctl.flags = 0;
cmd = 0;
lflag = 0;
mflag = 0;
sflag = 0;
1997-10-13 08:05:58 +04:00
type = 0xff;
1997-10-13 07:58:05 +04:00
while ((ch = getopt(argc, argv, "CEcelt:d:s")) != -1) {
2001-09-09 03:20:37 +04:00
switch (ch) {
1997-10-13 07:58:05 +04:00
case 'C':
rctl.flags |= RND_FLAG_NO_COLLECT;
rctl.mask |= RND_FLAG_NO_COLLECT;
mflag++;
break;
case 'E':
rctl.flags |= RND_FLAG_NO_ESTIMATE;
rctl.mask |= RND_FLAG_NO_ESTIMATE;
mflag++;
break;
case 'c':
rctl.flags &= ~RND_FLAG_NO_COLLECT;
rctl.mask |= RND_FLAG_NO_COLLECT;
mflag++;
break;
case 'e':
rctl.flags &= ~RND_FLAG_NO_ESTIMATE;
rctl.mask |= RND_FLAG_NO_ESTIMATE;
mflag++;
break;
case 'l':
lflag++;
break;
case 't':
if (cmd != 0)
usage();
cmd = 't';
type = find_type(optarg);
break;
case 'd':
if (cmd != 0)
usage();
cmd = 'd';
type = 0xff;
2003-05-18 03:16:47 +04:00
strlcpy(name, optarg, sizeof(name));
1997-10-13 07:58:05 +04:00
break;
case 's':
sflag++;
break;
1997-10-13 07:58:05 +04:00
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
/*
* No leftover non-option arguments.
*/
if (argc > 0)
usage();
1997-10-13 07:58:05 +04:00
/*
2001-09-09 03:20:37 +04:00
* Cannot list and modify at the same time.
1997-10-13 07:58:05 +04:00
*/
if ((lflag != 0 || sflag != 0) && mflag != 0)
1997-10-13 07:58:05 +04:00
usage();
/*
2001-09-09 03:20:37 +04:00
* Bomb out on no-ops.
1997-10-13 07:58:05 +04:00
*/
if (lflag == 0 && mflag == 0 && sflag == 0)
1997-10-13 07:58:05 +04:00
usage();
/*
2001-09-09 03:20:37 +04:00
* If not listing, we need a device name or a type.
1997-10-13 07:58:05 +04:00
*/
if (lflag == 0 && cmd == 0 && sflag == 0)
1997-10-13 07:58:05 +04:00
usage();
/*
2001-09-09 03:20:37 +04:00
* Modify request.
1997-10-13 07:58:05 +04:00
*/
if (mflag != 0) {
rctl.type = type;
2003-05-18 03:16:47 +04:00
strncpy(rctl.name, name, sizeof(rctl.name));
1997-10-13 07:58:05 +04:00
do_ioctl(&rctl);
exit(0);
}
/*
2001-09-09 03:20:37 +04:00
* List sources.
1997-10-13 07:58:05 +04:00
*/
if (lflag != 0)
do_list(cmd == 0, type, name);
if (sflag != 0)
do_stats();
2001-09-09 03:20:37 +04:00
exit(0);
1997-10-13 07:58:05 +04:00
}