1 /* $OpenBSD: skeyinfo.c,v 1.4 1997/07/23 07:01:12 millert Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 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. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Todd C. Miller. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 23 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 24 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <err.h> 34 #include <limits.h> 35 #include <paths.h> 36 #include <pwd.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 #include <skey.h> 42 43 extern char *__progname; 44 45 void usage __P((void)); 46 47 int 48 main(argc, argv) 49 int argc; 50 char **argv; 51 { 52 struct passwd *pw; 53 struct skey key; 54 char *name = NULL; 55 int errs, ch, verbose = 0; 56 57 if (geteuid() != 0) 58 errx(1, "must be setuid root"); 59 60 while ((ch = getopt(argc, argv, "v")) != -1) 61 switch(ch) { 62 case 'v': 63 verbose = 1; 64 break; 65 default: 66 usage(); 67 } 68 argc -= optind; 69 argv += optind; 70 71 if (argc == 1) 72 name = argv[0]; 73 else if (argc > 1) 74 usage(); 75 76 if (name && getuid() != 0) 77 errx(1, "only root may specify an alternate user"); 78 79 if (name) { 80 if (strlen(name) > PASS_MAX) 81 errx(1, "username too long (%d chars max)", PASS_MAX); 82 if ((pw = getpwnam(name)) == NULL) 83 errx(1, "no passwd entry for %s", name); 84 } else { 85 if ((pw = getpwuid(getuid())) == NULL) 86 errx(1, "no passwd entry for uid %u", getuid()); 87 } 88 89 if ((name = strdup(pw->pw_name)) == NULL) 90 err(1, "cannot allocate memory"); 91 sevenbit(name); 92 93 errs = skeylookup(&key, name); 94 switch (errs) { 95 case 0: /* Success! */ 96 if (verbose) 97 (void)printf("otp-%s ", skey_get_algorithm()); 98 (void)printf("%d %s\n", key.n - 1, key.seed); 99 break; 100 case -1: /* File error */ 101 warnx("cannot open %s", _PATH_SKEYKEYS); 102 break; 103 case 1: /* Unknown user */ 104 warnx("%s is not listed in %s", name, _PATH_SKEYKEYS); 105 } 106 (void)fclose(key.keyfile); 107 108 exit(errs); 109 } 110 111 void 112 usage() 113 { 114 (void)fprintf(stderr, "Usage: %s [-v] [user]\n", __progname); 115 exit(1); 116 } 117