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

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