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

Annotation of src/usr.bin/passwd/passwd.c, Revision 1.15

1.15    ! deraadt     1: /*     $OpenBSD: passwd.c,v 1.14 2002/05/10 06:52:03 hugh Exp $        */
1.4       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1988 The Regents of the University of California.
                      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 the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: char copyright[] =
                     38: "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
                     39:  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
1.11      millert    43: /*static const char sccsid[] = "from: @(#)passwd.c     5.5 (Berkeley) 7/6/91";*/
1.15    ! deraadt    44: static const char rcsid[] = "$OpenBSD: passwd.c,v 1.14 2002/05/10 06:52:03 hugh Exp $";
1.1       deraadt    45: #endif /* not lint */
                     46:
                     47: #include <stdio.h>
                     48: #include <string.h>
                     49: #include <unistd.h>
1.12      millert    50: #include <err.h>
1.3       tholo      51: #ifdef KERBEROS
                     52: #include <kerberosIV/krb.h>
                     53: #endif
1.1       deraadt    54:
                     55: /*
                     56:  * Note on configuration:
                     57:  *      Generally one would not use both Kerberos and YP
                     58:  *      to maintain passwords.
                     59:  *
                     60:  */
                     61:
                     62: int use_kerberos;
                     63: int use_yp;
                     64:
                     65: #ifdef YP
                     66: int force_yp;
                     67: #endif
                     68:
1.9       millert    69: extern int local_passwd(char *, int);
1.6       weingart   70: extern int yp_passwd(char *);
1.7       art        71: extern int krb_passwd(int, char **);
1.10      hin        72: extern int krb5_passwd(int, char **);
1.14      hugh       73: extern int _yp_check(char **);
1.15    ! deraadt    74: void usage(int retval);
1.6       weingart   75:
                     76: int
1.15    ! deraadt    77: main(int argc, char **argv)
1.1       deraadt    78: {
                     79:        extern int optind;
1.15    ! deraadt    80:        char *username;
1.13      mpech      81:        int ch;
1.14      hugh       82: #ifdef YP
1.1       deraadt    83:        int status = 0;
1.14      hugh       84: #endif
1.15    ! deraadt    85:
1.3       tholo      86: #if defined(KERBEROS) || defined(KERBEROS5)
                     87:        extern char realm[];
1.1       deraadt    88:
1.3       tholo      89:        if (krb_get_lrealm(realm,1) == KSUCCESS)
                     90:                use_kerberos = 1;
1.1       deraadt    91: #endif
                     92: #ifdef YP
                     93:        use_yp = _yp_check(NULL);
                     94: #endif
                     95:
1.6       weingart   96:        /* Process args and options */
1.10      hin        97:        while ((ch = getopt(argc, argv, "lykK")) != -1)
1.1       deraadt    98:                switch (ch) {
                     99:                case 'l':               /* change local password file */
                    100:                        use_kerberos = 0;
                    101:                        use_yp = 0;
                    102:                        break;
                    103:                case 'k':               /* change Kerberos password */
1.10      hin       104: #if defined(KERBEROS)
1.1       deraadt   105:                        use_kerberos = 1;
                    106:                        use_yp = 0;
1.7       art       107:                        exit(krb_passwd(argc, argv));
1.1       deraadt   108:                        break;
                    109: #else
                    110:                        fprintf(stderr, "passwd: Kerberos not compiled in\n");
                    111:                        exit(1);
1.10      hin       112: #endif
                    113:                case 'K':
                    114: #ifdef KRB5
                    115:                        /* Skip programname and '-K' option */
1.15    ! deraadt   116:                        argc -= 2;
        !           117:                        argv += 2;
1.10      hin       118:                        exit(krb5_passwd(argc, argv));
1.15    ! deraadt   119: #else
1.10      hin       120:                        errx(1, "KerberosV support not enabled");
                    121:                        break;
1.1       deraadt   122: #endif
                    123:                case 'y':               /* change YP password */
                    124: #ifdef YP
                    125:                        if (!use_yp) {
                    126:                                fprintf(stderr, "passwd: YP not in use.\n");
                    127:                                exit(1);
                    128:                        }
                    129:                        use_kerberos = 0;
                    130:                        use_yp = 1;
                    131:                        force_yp = 1;
                    132:                        break;
                    133: #else
                    134:                        fprintf(stderr, "passwd: YP not compiled in\n");
                    135:                        exit(1);
                    136: #endif
                    137:                default:
1.8       ericj     138:                        usage(1);
1.1       deraadt   139:                }
                    140:
                    141:        argc -= optind;
                    142:        argv += optind;
                    143:
                    144:        username = getlogin();
                    145:        if (username == NULL) {
                    146:                fprintf(stderr, "passwd: who are you ??\n");
                    147:                exit(1);
                    148:        }
1.15    ! deraadt   149:
        !           150:        switch (argc) {
1.1       deraadt   151:        case 0:
                    152:                break;
                    153:        case 1:
                    154: #if defined(KERBEROS) || defined(KERBEROS5)
1.15    ! deraadt   155:                if (use_kerberos && strcmp(argv[0], username)) {
        !           156:                        (void)fprintf(stderr, "passwd: %s\n\t%s\n%s\n",
        !           157:                            "to change another user's Kerberos password, do",
        !           158:                            "\"passwd -k -u <user>\";",
        !           159:                            "to change a user's local passwd, use \"passwd -l <user>\"");
        !           160:                        exit(1);
        !           161:                }
1.1       deraadt   162: #endif
                    163:                username = argv[0];
                    164:                break;
                    165:        default:
1.8       ericj     166:                usage(1);
1.1       deraadt   167:        }
                    168:
                    169: #if defined(KERBEROS) || defined(KERBEROS5)
1.15    ! deraadt   170:        if (use_kerberos)
        !           171:                exit(krb_passwd(argc, argv));
1.1       deraadt   172: #endif
1.7       art       173:
1.1       deraadt   174: #ifdef YP
1.9       millert   175:        if (force_yp || ((status = local_passwd(username, 0)) && use_yp))
1.1       deraadt   176:                exit(yp_passwd(username));
                    177:        exit(status);
                    178: #endif
1.9       millert   179:        exit(local_passwd(username, 0));
1.1       deraadt   180: }
                    181:
1.6       weingart  182: void
1.15    ! deraadt   183: usage(int retval)
1.1       deraadt   184: {
1.15    ! deraadt   185:        fprintf(stderr, "usage: passwd [-l] [-y] [-k [-n name] [-i instance] "
        !           186:            "[-r realm] [-u username[.instance][@realm]] [user]\n");
1.8       ericj     187:        exit(retval);
1.1       deraadt   188: }