Annotation of src/usr.bin/skeyinfo/skeyinfo.c, Revision 1.13
1.13 ! deraadt 1: /* $OpenBSD: skeyinfo.c,v 1.12 2003/06/03 01:52:41 millert Exp $ */
1.1 millert 2:
3: /*
1.10 millert 4: * Copyright (c) 1997, 2001, 2002 Todd C. Miller <Todd.Miller@courtesan.com>
1.1 millert 5: *
1.12 millert 6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 millert 9: *
1.12 millert 10: * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12: * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
13: * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15: * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16: * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 millert 17: */
18:
19: #include <err.h>
1.10 millert 20: #include <limits.h>
21: #include <paths.h>
1.1 millert 22: #include <pwd.h>
23: #include <stdio.h>
1.10 millert 24: #include <stdlib.h>
1.1 millert 25: #include <string.h>
26: #include <unistd.h>
27: #include <skey.h>
28:
29: extern char *__progname;
30:
1.9 millert 31: void usage(void);
1.1 millert 32:
33: int
1.9 millert 34: main(int argc, char **argv)
1.1 millert 35: {
36: struct passwd *pw;
1.10 millert 37: struct skey key;
38: char *name = NULL;
39: int error, ch, verbose = 0;
40:
41: while ((ch = getopt(argc, argv, "v")) != -1)
1.1 millert 42: switch(ch) {
43: case 'v':
44: verbose = 1;
45: break;
46: default:
47: usage();
48: }
49: argc -= optind;
50: argv += optind;
51:
52: if (argc == 1)
53: name = argv[0];
54: else if (argc > 1)
55: usage();
56:
57: if (name && getuid() != 0)
58: errx(1, "only root may specify an alternate user");
59:
60: if (name) {
61: if ((pw = getpwnam(name)) == NULL)
62: errx(1, "no passwd entry for %s", name);
63: } else {
64: if ((pw = getpwuid(getuid())) == NULL)
65: errx(1, "no passwd entry for uid %u", getuid());
66: }
67:
68: if ((name = strdup(pw->pw_name)) == NULL)
69: err(1, "cannot allocate memory");
1.11 mpech 70: sevenbit(name);
1.1 millert 71:
1.10 millert 72: error = skeylookup(&key, name);
73: switch (error) {
74: case 0: /* Success! */
75: if (verbose)
76: (void)printf("otp-%s ", skey_get_algorithm());
77: (void)printf("%d %s\n", key.n - 1, key.seed);
78: break;
79: case -1: /* File error */
80: err(1, "cannot open %s/%s", _PATH_SKEYDIR, name);
81: break;
82: case 1: /* Unknown user */
83: errx(1, "%s is not listed in %s", name, _PATH_SKEYDIR);
84: break;
1.1 millert 85: }
1.10 millert 86: (void)fclose(key.keyfile);
1.1 millert 87:
1.10 millert 88: exit(error ? 1 : 0);
1.1 millert 89: }
90:
91: void
1.13 ! deraadt 92: usage(void)
1.1 millert 93: {
1.10 millert 94: (void)fprintf(stderr, "usage: %s [-v] [user]\n", __progname);
1.1 millert 95: exit(1);
96: }