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

Annotation of src/usr.bin/passwd/local_passwd.c, Revision 1.17

1.17    ! millert     1: /*     $OpenBSD: local_passwd.c,v 1.16 2001/07/07 00:10:49 millert Exp $       */
1.3       deraadt     2:
1.1       deraadt     3: /*-
                      4:  * Copyright (c) 1990 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
1.16      millert    37: /*static const char sccsid[] = "from: @(#)local_passwd.c       5.5 (Berkeley) 5/6/91";*/
1.17    ! millert    38: static const char rcsid[] = "$OpenBSD: local_passwd.c,v 1.16 2001/07/07 00:10:49 millert Exp $";
1.1       deraadt    39: #endif /* not lint */
                     40:
                     41: #include <sys/types.h>
1.2       deraadt    42: #include <sys/stat.h>
1.1       deraadt    43: #include <pwd.h>
1.7       weingart   44: #include <err.h>
1.1       deraadt    45: #include <errno.h>
                     46: #include <stdio.h>
                     47: #include <string.h>
1.2       deraadt    48: #include <unistd.h>
1.7       weingart   49: #include <ctype.h>
1.2       deraadt    50: #include <fcntl.h>
1.15      millert    51: #include <signal.h>
1.2       deraadt    52: #include <util.h>
1.14      millert    53: #include <login_cap.h>
1.1       deraadt    54:
1.7       weingart   55: static uid_t uid;
1.14      millert    56: extern int pwd_gensalt __P((char *, int, struct passwd *, login_cap_t *, char));
                     57: extern int pwd_check __P((struct passwd *, login_cap_t *, char *));
                     58: extern int pwd_gettries __P((struct passwd *, login_cap_t *));
                     59:
                     60: char *getnewpasswd __P((struct passwd *, login_cap_t *, int));
1.15      millert    61: void kbintr __P((int));
1.1       deraadt    62:
1.7       weingart   63: int
1.13      millert    64: local_passwd(uname, authenticated)
1.1       deraadt    65:        char *uname;
1.13      millert    66:        int authenticated;
1.1       deraadt    67: {
                     68:        struct passwd *pw;
1.14      millert    69:        login_cap_t *lc;
1.15      millert    70:        sigset_t fullset;
                     71:        time_t period;
1.16      millert    72:        int pfd, tfd = -1;
1.15      millert    73:        char *s = NULL;
1.1       deraadt    74:
                     75:        if (!(pw = getpwnam(uname))) {
                     76: #ifdef YP
                     77:                extern int use_yp;
                     78:                if (!use_yp)
                     79: #endif
1.7       weingart   80:                warnx("unknown user %s.", uname);
1.1       deraadt    81:                return(1);
                     82:        }
1.14      millert    83:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                     84:                warnx("unable to get login class for user %s.", uname);
                     85:                return(1);
                     86:        }
1.1       deraadt    87:
1.13      millert    88:        uid = authenticated ? pw->pw_uid : getuid();
1.1       deraadt    89:        if (uid && uid != pw->pw_uid) {
1.8       millert    90:                warnx("login/uid mismatch, username argument required.");
1.1       deraadt    91:                return(1);
                     92:        }
                     93:
1.14      millert    94:        /* Get the new password. */
                     95:        pw->pw_passwd = getnewpasswd(pw, lc, authenticated);
                     96:
                     97:        /* Reset password change time based on login.conf. */
                     98:        period = login_getcaptime(lc, "passwordtime", 0, 0);
                     99:        if (period > 0)
                    100:                pw->pw_change = time(NULL) + period;
                    101:        else
                    102:                pw->pw_change = 0;
                    103:
1.15      millert   104:        /* Drop user's real uid and block all signals to avoid a DoS. */
                    105:        setuid(0);
                    106:        sigfillset(&fullset);
                    107:        sigdelset(&fullset, SIGINT);
                    108:        sigprocmask(SIG_BLOCK, &fullset, NULL);
                    109:
                    110:        /* Get a lock on the passwd file and open it. */
                    111:        pw_init();
                    112:        for (;;) {
                    113:                int i, c, d;
                    114:
                    115:                for (i = 0; i < (s ? 64 : 8) && (tfd = pw_lock(0)) == -1; i++) {
1.17    ! millert   116:                        if (i == 0)
        !           117:                                (void)fputs("Please wait", stderr);
1.15      millert   118:                        (void)signal(SIGINT, kbintr);
                    119:                        fputc('.', stderr);
                    120:                        usleep(250000);
                    121:                        (void)signal(SIGINT, SIG_IGN);
                    122:                }
1.17    ! millert   123:                if (i)
        !           124:                        fputc('\n', stderr);
1.15      millert   125:                if (tfd != -1)
                    126:                        break;
                    127:
                    128:                /* Unable to lock passwd file, let the user decide. */
                    129:                if (errno == EEXIST) {
                    130:                        if (s == NULL)
                    131:                                s = "The passwd file is busy,";
                    132:                        else
                    133:                                s = "The passwd file is still busy,";
                    134:                } else
                    135:                        s = "Unable to open passwd temp file,";
                    136:                (void)fprintf(stderr,
                    137:                    "%s do you want to wait until it is available? [y/n] ", s);
                    138:                (void)signal(SIGINT, kbintr);
                    139:                c = getchar();
                    140:                (void)signal(SIGINT, SIG_IGN);
                    141:                if (c != '\n')
                    142:                        while ((d = getchar()) != '\n' && d != EOF)
                    143:                                ;
                    144:                if (tolower(c) != 'y') {
                    145:                        printf("Password unchanged\n");
                    146:                        exit(1);
                    147:                }
                    148:        }
                    149:        pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
                    150:        if (pfd < 0 || fcntl(pfd, F_SETFD, 1) == -1)
                    151:                pw_error(_PATH_MASTERPASSWD, 1, 1);
                    152:
1.14      millert   153:        /* Update master.passwd file and build .db version. */
1.1       deraadt   154:        pw_copy(pfd, tfd, pw);
1.12      millert   155:        if (pw_mkdb(uname) < 0)
1.1       deraadt   156:                pw_error((char *)NULL, 0, 1);
1.14      millert   157:
1.1       deraadt   158:        return(0);
                    159: }
                    160:
                    161: char *
1.14      millert   162: getnewpasswd(pw, lc, authenticated)
                    163:        struct passwd *pw;
                    164:        login_cap_t *lc;
1.13      millert   165:        int authenticated;
1.1       deraadt   166: {
1.13      millert   167:        register char *p;
1.11      provos    168:        int tries, pwd_tries;
1.13      millert   169:        char buf[_PASSWORD_LEN+1], salt[_PASSWORD_LEN];
1.1       deraadt   170:
1.13      millert   171:        if (!authenticated) {
                    172:                (void)printf("Changing local password for %s.\n", pw->pw_name);
                    173:                if (uid && pw->pw_passwd[0] &&
                    174:                    strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
                    175:                    pw->pw_passwd)) {
                    176:                        errno = EACCES;
                    177:                        pw_error(NULL, 1, 1);
                    178:                }
1.1       deraadt   179:        }
1.11      provos    180:
1.14      millert   181:        pwd_tries = pwd_gettries(pw, lc);
1.1       deraadt   182:
                    183:        for (buf[0] = '\0', tries = 0;;) {
                    184:                p = getpass("New password:");
                    185:                if (!*p) {
                    186:                        (void)printf("Password unchanged.\n");
                    187:                        pw_error(NULL, 0, 0);
1.9       deraadt   188:                }
                    189:                if (strcmp(p, "s/key") == 0) {
                    190:                        printf("That password collides with a system feature. Choose another.\n");
                    191:                        continue;
1.1       deraadt   192:                }
1.11      provos    193:
                    194:                if ((tries++ < pwd_tries || pwd_tries == 0)
1.14      millert   195:                    && pwd_check(pw, lc, p) == 0)
1.1       deraadt   196:                        continue;
1.13      millert   197:                strlcpy(buf, p, sizeof(buf));
1.1       deraadt   198:                if (!strcmp(buf, getpass("Retype new password:")))
                    199:                        break;
                    200:                (void)printf("Mismatch; try again, EOF to quit.\n");
                    201:        }
1.14      millert   202:        if (!pwd_gensalt(salt, _PASSWORD_LEN, pw, lc, 'l')) {
1.6       provos    203:                (void)printf("Couldn't generate salt.\n");
                    204:                pw_error(NULL, 0, 0);
                    205:        }
1.1       deraadt   206:        return(crypt(buf, salt));
1.15      millert   207: }
                    208:
                    209: void
                    210: kbintr(signo)
                    211:        int signo;
                    212: {
                    213:        char msg[] = "\nPassword unchanged\n";
                    214:
                    215:        write(STDOUT_FILENO, msg, sizeof(msg) - 1);
                    216:        _exit(1);
1.1       deraadt   217: }