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

1.39    ! djm         1: /*     $OpenBSD: local_passwd.c,v 1.38 2008/04/27 10:01:53 jsing 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.
1.29      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
1.16      millert    33: /*static const char sccsid[] = "from: @(#)local_passwd.c       5.5 (Berkeley) 5/6/91";*/
1.39    ! djm        34: static const char rcsid[] = "$OpenBSD: local_passwd.c,v 1.38 2008/04/27 10:01:53 jsing Exp $";
1.1       deraadt    35: #endif /* not lint */
                     36:
                     37: #include <sys/types.h>
1.2       deraadt    38: #include <sys/stat.h>
1.21      millert    39: #include <sys/uio.h>
                     40:
1.7       weingart   41: #include <err.h>
1.1       deraadt    42: #include <errno.h>
1.21      millert    43: #include <fcntl.h>
                     44: #include <pwd.h>
1.1       deraadt    45: #include <stdio.h>
1.28      deraadt    46: #include <stdlib.h>
1.21      millert    47: #include <signal.h>
1.1       deraadt    48: #include <string.h>
1.2       deraadt    49: #include <unistd.h>
                     50: #include <util.h>
1.14      millert    51: #include <login_cap.h>
1.1       deraadt    52:
1.27      millert    53: #define UNCHANGED_MSG  "Password unchanged.\n"
                     54:
1.7       weingart   55: static uid_t uid;
1.33      millert    56: extern int pwd_gensalt(char *, int, login_cap_t *, char);
                     57: extern int pwd_check(login_cap_t *, char *);
                     58: extern int pwd_gettries(login_cap_t *);
1.14      millert    59:
1.39    ! djm        60: int local_passwd(char *, int);
1.25      millert    61: char *getnewpasswd(struct passwd *, login_cap_t *, int);
                     62: void kbintr(int);
1.1       deraadt    63:
1.7       weingart   64: int
1.26      deraadt    65: local_passwd(char *uname, int authenticated)
1.1       deraadt    66: {
1.32      millert    67:        struct passwd *pw, *opw;
1.14      millert    68:        login_cap_t *lc;
1.15      millert    69:        sigset_t fullset;
                     70:        time_t period;
1.21      millert    71:        int i, pfd, tfd = -1;
1.20      millert    72:        int pwflags = _PASSWORD_OMITV7;
1.1       deraadt    73:
                     74:        if (!(pw = getpwnam(uname))) {
                     75: #ifdef YP
                     76:                extern int use_yp;
                     77:                if (!use_yp)
                     78: #endif
1.7       weingart   79:                warnx("unknown user %s.", uname);
1.1       deraadt    80:                return(1);
                     81:        }
1.32      millert    82:        if ((opw = pw_dup(pw)) == NULL) {
                     83:                warn(NULL);
                     84:                return(1);
                     85:        }
1.14      millert    86:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                     87:                warnx("unable to get login class for user %s.", uname);
1.36      moritz     88:                free(opw);
1.14      millert    89:                return(1);
                     90:        }
1.1       deraadt    91:
1.13      millert    92:        uid = authenticated ? pw->pw_uid : getuid();
1.1       deraadt    93:        if (uid && uid != pw->pw_uid) {
1.8       millert    94:                warnx("login/uid mismatch, username argument required.");
1.36      moritz     95:                free(opw);
1.1       deraadt    96:                return(1);
                     97:        }
                     98:
1.14      millert    99:        /* Get the new password. */
                    100:        pw->pw_passwd = getnewpasswd(pw, lc, authenticated);
                    101:
                    102:        /* Reset password change time based on login.conf. */
1.30      deraadt   103:        period = (time_t)login_getcaptime(lc, "passwordtime",
                    104:            (quad_t)0, (quad_t)0);
1.19      millert   105:        if (period > 0) {
1.14      millert   106:                pw->pw_change = time(NULL) + period;
1.19      millert   107:        } else {
                    108:                /*
                    109:                 * If the pw change time is the same we only need
                    110:                 * to update the spwd.db file.
                    111:                 */
                    112:                if (pw->pw_change != 0)
                    113:                        pw->pw_change = 0;
                    114:                else
1.20      millert   115:                        pwflags = _PASSWORD_SECUREONLY;
1.19      millert   116:        }
1.14      millert   117:
1.15      millert   118:        /* Drop user's real uid and block all signals to avoid a DoS. */
                    119:        setuid(0);
                    120:        sigfillset(&fullset);
                    121:        sigdelset(&fullset, SIGINT);
                    122:        sigprocmask(SIG_BLOCK, &fullset, NULL);
                    123:
                    124:        /* Get a lock on the passwd file and open it. */
                    125:        pw_init();
1.21      millert   126:        for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
                    127:                if (i == 4)
1.38      jsing     128:                        (void)fputs("Attempting to lock password file, "
1.21      millert   129:                            "please wait or press ^C to abort", stderr);
                    130:                (void)signal(SIGINT, kbintr);
                    131:                if (i % 16 == 0)
1.15      millert   132:                        fputc('.', stderr);
1.21      millert   133:                usleep(250000);
1.15      millert   134:                (void)signal(SIGINT, SIG_IGN);
                    135:        }
1.21      millert   136:        if (i >= 4)
                    137:                fputc('\n', stderr);
1.15      millert   138:        pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
1.36      moritz    139:        if (pfd < 0 || fcntl(pfd, F_SETFD, FD_CLOEXEC) == -1)
1.15      millert   140:                pw_error(_PATH_MASTERPASSWD, 1, 1);
                    141:
1.18      millert   142:        /* Update master.passwd file and rebuild spwd.db. */
1.32      millert   143:        pw_copy(pfd, tfd, pw, opw);
                    144:        free(opw);
1.20      millert   145:        if (pw_mkdb(uname, pwflags) < 0)
1.19      millert   146:                pw_error(NULL, 0, 1);
1.14      millert   147:
1.1       deraadt   148:        return(0);
                    149: }
                    150:
                    151: char *
1.26      deraadt   152: getnewpasswd(struct passwd *pw, login_cap_t *lc, int authenticated)
1.1       deraadt   153: {
1.22      mpech     154:        char *p;
1.11      provos    155:        int tries, pwd_tries;
1.13      millert   156:        char buf[_PASSWORD_LEN+1], salt[_PASSWORD_LEN];
1.24      millert   157:        sig_t saveint, savequit;
1.1       deraadt   158:
1.24      millert   159:        saveint = signal(SIGINT, kbintr);
                    160:        savequit = signal(SIGQUIT, kbintr);
1.23      millert   161:
1.13      millert   162:        if (!authenticated) {
                    163:                (void)printf("Changing local password for %s.\n", pw->pw_name);
1.27      millert   164:                if (uid != 0 && pw->pw_passwd[0] != '\0') {
                    165:                        p = getpass("Old password:");
1.31      millert   166:                        if (p == NULL || *p == '\0') {
1.27      millert   167:                                (void)printf(UNCHANGED_MSG);
                    168:                                pw_abort();
1.31      millert   169:                                exit(p == NULL ? 1 : 0);
1.27      millert   170:                        }
                    171:                        if (strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
                    172:                                errno = EACCES;
                    173:                                pw_error(NULL, 1, 1);
                    174:                        }
1.13      millert   175:                }
1.1       deraadt   176:        }
1.26      deraadt   177:
1.33      millert   178:        pwd_tries = pwd_gettries(lc);
1.1       deraadt   179:
                    180:        for (buf[0] = '\0', tries = 0;;) {
                    181:                p = getpass("New password:");
1.31      millert   182:                if (p == NULL || *p == '\0') {
1.27      millert   183:                        (void)printf(UNCHANGED_MSG);
                    184:                        pw_abort();
1.31      millert   185:                        exit(p == NULL ? 1 : 0);
1.9       deraadt   186:                }
                    187:                if (strcmp(p, "s/key") == 0) {
                    188:                        printf("That password collides with a system feature. Choose another.\n");
                    189:                        continue;
1.1       deraadt   190:                }
1.26      deraadt   191:
1.37      deraadt   192:                if ((tries++ < pwd_tries || pwd_tries == 0) &&
                    193:                    pwd_check(lc, p) == 0)
1.1       deraadt   194:                        continue;
1.13      millert   195:                strlcpy(buf, p, sizeof(buf));
1.31      millert   196:                p = getpass("Retype new password:");
                    197:                if (p != NULL && strcmp(buf, p) == 0)
1.1       deraadt   198:                        break;
                    199:                (void)printf("Mismatch; try again, EOF to quit.\n");
                    200:        }
1.33      millert   201:        if (!pwd_gensalt(salt, _PASSWORD_LEN, lc, 'l')) {
1.6       provos    202:                (void)printf("Couldn't generate salt.\n");
                    203:                pw_error(NULL, 0, 0);
                    204:        }
1.24      millert   205:        (void)signal(SIGINT, saveint);
                    206:        (void)signal(SIGQUIT, savequit);
1.23      millert   207:
1.1       deraadt   208:        return(crypt(buf, salt));
1.15      millert   209: }
                    210:
1.35      deraadt   211: /* ARGSUSED */
1.15      millert   212: void
1.26      deraadt   213: kbintr(int signo)
1.15      millert   214: {
1.34      millert   215:        write(STDOUT_FILENO, "\n", 1);
1.27      millert   216:        write(STDOUT_FILENO, UNCHANGED_MSG, sizeof(UNCHANGED_MSG) - 1);
                    217:        _exit(0);
1.1       deraadt   218: }