1 /* $OpenBSD: skeyinfo.c,v 1.14 2003/06/17 21:56:26 millert Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 2001, 2002 Todd C. Miller <Todd.Miller@courtesan.com> 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 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 while ((ch = getopt(argc, argv, "v")) != -1) 46 switch(ch) { 47 case 'v': 48 verbose = 1; 49 break; 50 default: 51 usage(); 52 } 53 argc -= optind; 54 argv += optind; 55 56 if (argc == 1) 57 name = argv[0]; 58 else if (argc > 1) 59 usage(); 60 61 if (name && getuid() != 0) 62 errx(1, "only root may specify an alternate user"); 63 64 if (name) { 65 if ((pw = getpwnam(name)) == NULL) 66 errx(1, "no passwd entry for %s", name); 67 } else { 68 if ((pw = getpwuid(getuid())) == NULL) 69 errx(1, "no passwd entry for uid %u", getuid()); 70 } 71 72 if ((name = strdup(pw->pw_name)) == NULL) 73 err(1, "cannot allocate memory"); 74 sevenbit(name); 75 76 error = skeylookup(&key, name); 77 switch (error) { 78 case 0: /* Success! */ 79 if (verbose) 80 (void)printf("otp-%s ", skey_get_algorithm()); 81 (void)printf("%d %s\n", key.n - 1, key.seed); 82 break; 83 case -1: /* File error */ 84 err(1, "cannot open %s/%s", _PATH_SKEYDIR, name); 85 break; 86 case 1: /* Unknown user */ 87 errx(1, "%s is not listed in %s", name, _PATH_SKEYDIR); 88 break; 89 } 90 (void)fclose(key.keyfile); 91 92 exit(error ? 1 : 0); 93 } 94 95 void 96 usage(void) 97 { 98 (void)fprintf(stderr, "usage: %s [-v] [user]\n", __progname); 99 exit(1); 100 } 101