Annotation of src/usr.bin/skeyinfo/skeyinfo.c, Revision 1.10
1.10 ! millert 1: /* $OpenBSD: skeyinfo.c,v 1.9 2001/06/19 01:49:45 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: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
1.5 millert 15: * 3. The name of the author may not be used to endorse or promote products
1.1 millert 16: * derived from this software without specific prior written permission.
17: *
18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: */
29:
30: #include <err.h>
1.10 ! millert 31: #include <limits.h>
! 32: #include <paths.h>
1.1 millert 33: #include <pwd.h>
34: #include <stdio.h>
1.10 ! millert 35: #include <stdlib.h>
1.1 millert 36: #include <string.h>
37: #include <unistd.h>
38: #include <skey.h>
39:
40: extern char *__progname;
41:
1.9 millert 42: void usage(void);
1.1 millert 43:
44: int
1.9 millert 45: main(int argc, char **argv)
1.1 millert 46: {
47: struct passwd *pw;
1.10 ! millert 48: struct skey key;
! 49: char *name = NULL;
! 50: int error, ch, verbose = 0;
! 51:
! 52: while ((ch = getopt(argc, argv, "v")) != -1)
1.1 millert 53: switch(ch) {
54: case 'v':
55: verbose = 1;
56: break;
57: default:
58: usage();
59: }
60: argc -= optind;
61: argv += optind;
62:
63: if (argc == 1)
64: name = argv[0];
65: else if (argc > 1)
66: usage();
67:
68: if (name && getuid() != 0)
69: errx(1, "only root may specify an alternate user");
70:
71: if (name) {
72: if ((pw = getpwnam(name)) == NULL)
73: errx(1, "no passwd entry for %s", name);
74: } else {
75: if ((pw = getpwuid(getuid())) == NULL)
76: errx(1, "no passwd entry for uid %u", getuid());
77: }
78:
79: if ((name = strdup(pw->pw_name)) == NULL)
80: err(1, "cannot allocate memory");
81:
1.10 ! millert 82: error = skeylookup(&key, name);
! 83: switch (error) {
! 84: case 0: /* Success! */
! 85: if (verbose)
! 86: (void)printf("otp-%s ", skey_get_algorithm());
! 87: (void)printf("%d %s\n", key.n - 1, key.seed);
! 88: break;
! 89: case -1: /* File error */
! 90: err(1, "cannot open %s/%s", _PATH_SKEYDIR, name);
! 91: break;
! 92: case 1: /* Unknown user */
! 93: errx(1, "%s is not listed in %s", name, _PATH_SKEYDIR);
! 94: break;
1.1 millert 95: }
1.10 ! millert 96: (void)fclose(key.keyfile);
1.1 millert 97:
1.10 ! millert 98: exit(error ? 1 : 0);
1.1 millert 99: }
100:
101: void
1.10 ! millert 102: usage()
1.1 millert 103: {
1.10 ! millert 104: (void)fprintf(stderr, "usage: %s [-v] [user]\n", __progname);
1.1 millert 105: exit(1);
106: }