1 /* $OpenBSD: skeyinfo.c,v 1.16 2019/01/25 00:19:26 millert Exp $ */
2
3 /*
4 * Copyright (c) 1997, 2001, 2002 Todd C. Miller <millert@openbsd.org>
5 *
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.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * Sponsored in part by the Defense Advanced Research Projects
19 * Agency (DARPA) and Air Force Research Laboratory, Air Force
20 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21 */
22
23 #include <err.h>
24 #include <limits.h>
25 #include <paths.h>
26 #include <pwd.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <skey.h>
32
33 extern char *__progname;
34
35 void usage(void);
36
37 int
main(int argc,char ** argv)38 main(int argc, char **argv)
39 {
40 struct passwd *pw;
41 struct skey key;
42 char *name = NULL;
43 int error, ch, verbose = 0;
44
45 if (pledge("stdio rpath wpath flock getpw", NULL) == -1)
46 err(1, "pledge");
47
48 while ((ch = getopt(argc, argv, "v")) != -1)
49 switch(ch) {
50 case 'v':
51 verbose = 1;
52 break;
53 default:
54 usage();
55 }
56 argc -= optind;
57 argv += optind;
58
59 if (argc == 1)
60 name = argv[0];
61 else if (argc > 1)
62 usage();
63
64 if (name && getuid() != 0)
65 errx(1, "only root may specify an alternate user");
66
67 if (name) {
68 if ((pw = getpwnam(name)) == NULL)
69 errx(1, "no passwd entry for %s", name);
70 } else {
71 if ((pw = getpwuid(getuid())) == NULL)
72 errx(1, "no passwd entry for uid %u", getuid());
73 }
74
75 if ((name = strdup(pw->pw_name)) == NULL)
76 err(1, "cannot allocate memory");
77 sevenbit(name);
78
79 error = skeylookup(&key, name);
80 switch (error) {
81 case 0: /* Success! */
82 if (verbose)
83 (void)printf("otp-%s ", skey_get_algorithm());
84 (void)printf("%d %s\n", key.n - 1, key.seed);
85 break;
86 case -1: /* File error */
87 err(1, "cannot open %s/%s", _PATH_SKEYDIR, name);
88 break;
89 case 1: /* Unknown user */
90 errx(1, "%s is not listed in %s", name, _PATH_SKEYDIR);
91 break;
92 }
93 (void)fclose(key.keyfile);
94
95 exit(error ? 1 : 0);
96 }
97
98 void
usage(void)99 usage(void)
100 {
101 (void)fprintf(stderr, "usage: %s [-v] [user]\n", __progname);
102 exit(1);
103 }
104