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

1.9     ! millert     1: /*     $OpenBSD: skeyinfo.c,v 1.8 2001/06/17 22:54:44 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:
1.9     ! millert    41: void usage(void);
1.1       millert    42:
                     43: int
1.9     ! millert    44: main(int argc, char **argv)
1.1       millert    45: {
                     46:        struct passwd *pw;
1.9     ! millert    47:        char *style, *challenge, *cp, *name;
1.7       millert    48:        int ch, verbose = 0;
1.9     ! millert    49:        login_cap_t *lc;
1.7       millert    50:        auth_session_t *as;
1.1       millert    51:
1.9     ! millert    52:        name = NULL;
        !            53:        style = "skey";
        !            54:        while ((ch = getopt(argc, argv, "a:v")) != -1)
1.1       millert    55:                switch(ch) {
1.9     ! millert    56:                case 'a':
        !            57:                        style = optarg;
        !            58:                        break;
1.1       millert    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.9     ! millert    87:        if ((lc = login_getclass(pw->pw_class)) == NULL)
        !            88:                errx(1, "unable to classify user %s", name);
        !            89:
        !            90:        if ((cp = login_getstyle(lc, style, NULL)) == NULL)
        !            91:                errx(1, "unknown authentication method %s", style);
        !            92:
        !            93:        as = auth_userchallenge(name, cp, NULL, &challenge);
1.7       millert    94:        if (as == NULL || challenge == NULL) {
1.8       millert    95:                if (as)
                     96:                        auth_close(as);
1.9     ! millert    97:                errx(1, "unable to retrieve challenge for %s", name);
1.1       millert    98:        }
                     99:
1.7       millert   100:        /*
                    101:         * We only want the first line of the challenge so stop after a newline.
                    102:         * If the user wants the full challenge including the hash type
                    103:         * or if the challenge didn't start with 'otp-', print it verbatim.
                    104:         * Otherwise, strip off the first word.
                    105:         */
                    106:        if ((cp = strchr(challenge, '\n')))
                    107:                *cp = '\0';
                    108:        cp = strchr(challenge, ' ');
                    109:        if (verbose || *challenge != 'o' || !cp)
                    110:                cp = challenge;
                    111:        else
                    112:                cp++;
                    113:        puts(cp);
                    114:
                    115:        auth_close(as);
                    116:        exit(0);
1.1       millert   117: }
                    118:
                    119: void
1.9     ! millert   120: usage(void)
1.1       millert   121: {
1.9     ! millert   122:
        !           123:        (void)fprintf(stderr, "Usage: %s [-a auth-type] [-v] [user]\n",
        !           124:            __progname);
1.1       millert   125:        exit(1);
                    126: }