[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.16

1.16    ! millert     1: /*     $OpenBSD: skeyinfo.c,v 1.15 2015/11/01 14:02:37 tim Exp $       */
1.1       millert     2:
                      3: /*
1.16    ! millert     4:  * Copyright (c) 1997, 2001, 2002 Todd C. Miller <millert@openbsd.org>
1.1       millert     5:  *
1.12      millert     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.
1.1       millert     9:  *
1.14      millert    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.
1.1       millert    21:  */
                     22:
                     23: #include <err.h>
1.10      millert    24: #include <limits.h>
                     25: #include <paths.h>
1.1       millert    26: #include <pwd.h>
                     27: #include <stdio.h>
1.10      millert    28: #include <stdlib.h>
1.1       millert    29: #include <string.h>
                     30: #include <unistd.h>
                     31: #include <skey.h>
                     32:
                     33: extern char *__progname;
                     34:
1.9       millert    35: void usage(void);
1.1       millert    36:
                     37: int
1.9       millert    38: main(int argc, char **argv)
1.1       millert    39: {
                     40:        struct passwd *pw;
1.10      millert    41:        struct skey key;
                     42:        char *name = NULL;
                     43:        int error, ch, verbose = 0;
1.15      tim        44:
                     45:        if (pledge("stdio rpath wpath flock getpw", NULL) == -1)
                     46:                err(1, "pledge");
1.10      millert    47:
                     48:        while ((ch = getopt(argc, argv, "v")) != -1)
1.1       millert    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");
1.11      mpech      77:        sevenbit(name);
1.1       millert    78:
1.10      millert    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;
1.1       millert    92:        }
1.10      millert    93:        (void)fclose(key.keyfile);
1.1       millert    94:
1.10      millert    95:        exit(error ? 1 : 0);
1.1       millert    96: }
                     97:
                     98: void
1.13      deraadt    99: usage(void)
1.1       millert   100: {
1.10      millert   101:        (void)fprintf(stderr, "usage: %s [-v] [user]\n", __progname);
1.1       millert   102:        exit(1);
                    103: }