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

1.8     ! millert     1: /*     $OpenBSD: skeyinfo.c,v 1.7 2001/06/17 22:44:51 millert Exp $    */
1.1       millert     2:
                      3: /*
1.7       millert     4:  * Copyright (c) 1997, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       millert     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 <pwd.h>
                     32: #include <stdio.h>
                     33: #include <string.h>
                     34: #include <unistd.h>
                     35: #include <skey.h>
1.7       millert    36: #include <login_cap.h>
                     37: #include <bsd_auth.h>
1.1       millert    38:
                     39: extern char *__progname;
                     40:
                     41: void usage __P((void));
                     42:
                     43: int
                     44: main(argc, argv)
                     45:        int argc;
                     46:        char **argv;
                     47: {
                     48:        struct passwd *pw;
1.7       millert    49:        char *challenge, *cp, *name = NULL;
                     50:        int ch, verbose = 0;
                     51:        auth_session_t *as;
1.1       millert    52:
                     53:        while ((ch = getopt(argc, argv, "v")) != -1)
                     54:                switch(ch) {
                     55:                case 'v':
                     56:                        verbose = 1;
                     57:                        break;
                     58:                default:
                     59:                        usage();
                     60:        }
                     61:        argc -= optind;
                     62:        argv += optind;
                     63:
                     64:        if (argc == 1)
                     65:                name = argv[0];
                     66:        else if (argc > 1)
                     67:                usage();
                     68:
                     69:        if (name && getuid() != 0)
                     70:                errx(1, "only root may specify an alternate user");
                     71:
                     72:        if (name) {
                     73:                if ((pw = getpwnam(name)) == NULL)
                     74:                        errx(1, "no passwd entry for %s", name);
                     75:        } else {
                     76:                if ((pw = getpwuid(getuid())) == NULL)
                     77:                        errx(1, "no passwd entry for uid %u", getuid());
                     78:        }
                     79:
                     80:        if ((name = strdup(pw->pw_name)) == NULL)
                     81:                err(1, "cannot allocate memory");
                     82:
1.7       millert    83:        as = auth_userchallenge(name, "skey", NULL, &challenge);
                     84:        if (as == NULL || challenge == NULL) {
1.8     ! millert    85:                if (as)
        !            86:                        auth_close(as);
1.7       millert    87:                errx(1, "unable to retrieve S/Key challenge for %s", name);
1.1       millert    88:        }
                     89:
1.7       millert    90:        /*
                     91:         * We only want the first line of the challenge so stop after a newline.
                     92:         * If the user wants the full challenge including the hash type
                     93:         * or if the challenge didn't start with 'otp-', print it verbatim.
                     94:         * Otherwise, strip off the first word.
                     95:         */
                     96:        if ((cp = strchr(challenge, '\n')))
                     97:                *cp = '\0';
                     98:        cp = strchr(challenge, ' ');
                     99:        if (verbose || *challenge != 'o' || !cp)
                    100:                cp = challenge;
                    101:        else
                    102:                cp++;
                    103:        puts(cp);
                    104:
                    105:        auth_close(as);
                    106:        exit(0);
1.1       millert   107: }
                    108:
                    109: void
                    110: usage()
                    111: {
                    112:        (void)fprintf(stderr, "Usage: %s [-v] [user]\n", __progname);
                    113:        exit(1);
                    114: }