[BACK]Return to skeyinfo.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / skeyinfo

Annotation of src/usr.bin/skeyinfo/skeyinfo.c, Revision 1.6

1.6     ! millert     1: /*     $OpenBSD: skeyinfo.c,v 1.5 1998/06/21 22:14:03 millert Exp $    */
1.1       millert     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.
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>
                     31: #include <limits.h>
                     32: #include <paths.h>
                     33: #include <pwd.h>
                     34: #include <stdio.h>
                     35: #include <stdlib.h>
                     36: #include <string.h>
                     37: #include <unistd.h>
                     38: #include <skey.h>
                     39:
                     40: extern char *__progname;
                     41:
                     42: void usage __P((void));
                     43:
                     44: int
                     45: main(argc, argv)
                     46:        int argc;
                     47:        char **argv;
                     48: {
                     49:        struct passwd *pw;
                     50:        struct skey key;
                     51:        char *name = NULL;
1.6     ! millert    52:        int error, ch, verbose = 0;
1.1       millert    53:
                     54:        if (geteuid() != 0)
                     55:                errx(1, "must be setuid root");
                     56:
                     57:        while ((ch = getopt(argc, argv, "v")) != -1)
                     58:                switch(ch) {
                     59:                case 'v':
                     60:                        verbose = 1;
                     61:                        break;
                     62:                default:
                     63:                        usage();
                     64:        }
                     65:        argc -= optind;
                     66:        argv += optind;
                     67:
                     68:        if (argc == 1)
                     69:                name = argv[0];
                     70:        else if (argc > 1)
                     71:                usage();
                     72:
                     73:        if (name && getuid() != 0)
                     74:                errx(1, "only root may specify an alternate user");
                     75:
                     76:        if (name) {
                     77:                if ((pw = getpwnam(name)) == NULL)
                     78:                        errx(1, "no passwd entry for %s", name);
                     79:        } else {
                     80:                if ((pw = getpwuid(getuid())) == NULL)
                     81:                        errx(1, "no passwd entry for uid %u", getuid());
                     82:        }
                     83:
                     84:        if ((name = strdup(pw->pw_name)) == NULL)
                     85:                err(1, "cannot allocate memory");
                     86:
1.6     ! millert    87:        error = skeylookup(&key, name);
        !            88:        switch (error) {
1.1       millert    89:                case 0:         /* Success! */
                     90:                        if (verbose)
                     91:                                (void)printf("otp-%s ", skey_get_algorithm());
                     92:                        (void)printf("%d %s\n", key.n - 1, key.seed);
                     93:                        break;
                     94:                case -1:        /* File error */
1.6     ! millert    95:                        warn("cannot open %s", _PATH_SKEYKEYS);
1.1       millert    96:                        break;
                     97:                case 1:         /* Unknown user */
1.4       millert    98:                        warnx("%s is not listed in %s", name, _PATH_SKEYKEYS);
1.1       millert    99:        }
1.4       millert   100:        (void)fclose(key.keyfile);
1.1       millert   101:
1.6     ! millert   102:        exit(error ? 1 : 0);
1.1       millert   103: }
                    104:
                    105: void
                    106: usage()
                    107: {
                    108:        (void)fprintf(stderr, "Usage: %s [-v] [user]\n", __progname);
                    109:        exit(1);
                    110: }