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

1.61    ! robert      1: /*     $OpenBSD: local_passwd.c,v 1.60 2021/08/28 06:46:49 robert 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: #include <sys/types.h>
1.2       deraadt    33: #include <sys/stat.h>
1.21      millert    34: #include <sys/uio.h>
                     35:
1.7       weingart   36: #include <err.h>
1.1       deraadt    37: #include <errno.h>
1.21      millert    38: #include <fcntl.h>
1.54      mestre     39: #include <paths.h>
1.21      millert    40: #include <pwd.h>
1.1       deraadt    41: #include <stdio.h>
1.28      deraadt    42: #include <stdlib.h>
1.21      millert    43: #include <signal.h>
1.1       deraadt    44: #include <string.h>
1.2       deraadt    45: #include <unistd.h>
                     46: #include <util.h>
1.14      millert    47: #include <login_cap.h>
1.50      tedu       48: #include <readpassphrase.h>
1.1       deraadt    49:
1.49      tedu       50: #define UNCHANGED_MSG  "Password unchanged."
1.27      millert    51:
1.7       weingart   52: static uid_t uid;
1.33      millert    53: extern int pwd_check(login_cap_t *, char *);
                     54: extern int pwd_gettries(login_cap_t *);
1.14      millert    55:
1.39      djm        56: int local_passwd(char *, int);
1.25      millert    57: char *getnewpasswd(struct passwd *, login_cap_t *, int);
                     58: void kbintr(int);
1.1       deraadt    59:
1.7       weingart   60: int
1.26      deraadt    61: local_passwd(char *uname, int authenticated)
1.1       deraadt    62: {
1.32      millert    63:        struct passwd *pw, *opw;
1.14      millert    64:        login_cap_t *lc;
1.15      millert    65:        sigset_t fullset;
                     66:        time_t period;
1.21      millert    67:        int i, pfd, tfd = -1;
1.20      millert    68:        int pwflags = _PASSWORD_OMITV7;
1.1       deraadt    69:
1.47      tim        70:        if (!(pw = getpwnam_shadow(uname))) {
1.7       weingart   71:                warnx("unknown user %s.", uname);
1.1       deraadt    72:                return(1);
                     73:        }
1.46      deraadt    74:
1.57      semarie    75:        if (unveil(_PATH_MASTERPASSWD_LOCK, "rwc") == -1)
1.59      beck       76:                err(1, "unveil %s", _PATH_MASTERPASSWD_LOCK);
1.54      mestre     77:        if (unveil(_PATH_MASTERPASSWD, "r") == -1)
1.59      beck       78:                err(1, "unveil %s", _PATH_MASTERPASSWD);
1.55      mestre     79:        if (unveil(_PATH_LOGIN_CONF, "r") == -1)
1.59      beck       80:                err(1, "unveil %s", _PATH_LOGIN_CONF);
1.57      semarie    81:        if (unveil(_PATH_LOGIN_CONF ".db", "r") == -1)
1.59      beck       82:                err(1, "unveil %s.db", _PATH_LOGIN_CONF);
1.54      mestre     83:        if (unveil(_PATH_BSHELL, "x") == -1)
1.59      beck       84:                err(1, "unveil %s", _PATH_BSHELL);
1.58      anton      85:        if (unveil(_PATH_SHELLS, "r") == -1)
1.59      beck       86:                err(1, "unveil %s", _PATH_SHELLS);
1.54      mestre     87:        if (unveil(_PATH_PWD_MKDB, "x") == -1)
1.59      beck       88:                err(1, "unveil %s", _PATH_PWD_MKDB);
1.46      deraadt    89:        if (pledge("stdio rpath wpath cpath getpw tty id proc exec", NULL) == -1)
                     90:                err(1, "pledge");
                     91:
1.32      millert    92:        if ((opw = pw_dup(pw)) == NULL) {
                     93:                warn(NULL);
                     94:                return(1);
                     95:        }
1.14      millert    96:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                     97:                warnx("unable to get login class for user %s.", uname);
1.36      moritz     98:                free(opw);
1.14      millert    99:                return(1);
                    100:        }
1.1       deraadt   101:
1.13      millert   102:        uid = authenticated ? pw->pw_uid : getuid();
1.1       deraadt   103:        if (uid && uid != pw->pw_uid) {
1.8       millert   104:                warnx("login/uid mismatch, username argument required.");
1.36      moritz    105:                free(opw);
1.1       deraadt   106:                return(1);
                    107:        }
                    108:
1.14      millert   109:        /* Get the new password. */
                    110:        pw->pw_passwd = getnewpasswd(pw, lc, authenticated);
                    111:
1.46      deraadt   112:        if (pledge("stdio rpath wpath cpath getpw id proc exec", NULL) == -1)
                    113:                err(1, "pledge");
                    114:
1.14      millert   115:        /* Reset password change time based on login.conf. */
1.48      guenther  116:        period = (time_t)login_getcaptime(lc, "passwordtime", 0, 0);
1.19      millert   117:        if (period > 0) {
1.14      millert   118:                pw->pw_change = time(NULL) + period;
1.19      millert   119:        } else {
                    120:                /*
                    121:                 * If the pw change time is the same we only need
                    122:                 * to update the spwd.db file.
                    123:                 */
                    124:                if (pw->pw_change != 0)
                    125:                        pw->pw_change = 0;
                    126:                else
1.20      millert   127:                        pwflags = _PASSWORD_SECUREONLY;
1.19      millert   128:        }
1.14      millert   129:
1.15      millert   130:        /* Drop user's real uid and block all signals to avoid a DoS. */
                    131:        setuid(0);
                    132:        sigfillset(&fullset);
                    133:        sigdelset(&fullset, SIGINT);
                    134:        sigprocmask(SIG_BLOCK, &fullset, NULL);
                    135:
1.46      deraadt   136:        if (pledge("stdio rpath wpath cpath proc exec", NULL) == -1)
                    137:                err(1, "pledge");
                    138:
1.15      millert   139:        /* Get a lock on the passwd file and open it. */
                    140:        pw_init();
1.21      millert   141:        for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
                    142:                if (i == 4)
1.38      jsing     143:                        (void)fputs("Attempting to lock password file, "
1.21      millert   144:                            "please wait or press ^C to abort", stderr);
                    145:                (void)signal(SIGINT, kbintr);
                    146:                if (i % 16 == 0)
1.15      millert   147:                        fputc('.', stderr);
1.21      millert   148:                usleep(250000);
1.15      millert   149:                (void)signal(SIGINT, SIG_IGN);
                    150:        }
1.21      millert   151:        if (i >= 4)
                    152:                fputc('\n', stderr);
1.41      guenther  153:        pfd = open(_PATH_MASTERPASSWD, O_RDONLY | O_CLOEXEC, 0);
1.56      deraadt   154:        if (pfd == -1)
1.15      millert   155:                pw_error(_PATH_MASTERPASSWD, 1, 1);
                    156:
1.18      millert   157:        /* Update master.passwd file and rebuild spwd.db. */
1.32      millert   158:        pw_copy(pfd, tfd, pw, opw);
                    159:        free(opw);
1.56      deraadt   160:        if (pw_mkdb(uname, pwflags) == -1)
1.19      millert   161:                pw_error(NULL, 0, 1);
1.61    ! robert    162:
        !           163:        fprintf(stderr, "passwd: password updated successfully\n");
1.14      millert   164:
1.1       deraadt   165:        return(0);
                    166: }
                    167:
                    168: char *
1.26      deraadt   169: getnewpasswd(struct passwd *pw, login_cap_t *lc, int authenticated)
1.1       deraadt   170: {
1.43      tedu      171:        static char hash[_PASSWORD_LEN];
1.52      tedu      172:        char newpass[1024];
1.44      tedu      173:        char *p, *pref;
1.11      provos    174:        int tries, pwd_tries;
1.24      millert   175:        sig_t saveint, savequit;
1.1       deraadt   176:
1.24      millert   177:        saveint = signal(SIGINT, kbintr);
                    178:        savequit = signal(SIGQUIT, kbintr);
1.23      millert   179:
1.13      millert   180:        if (!authenticated) {
1.60      robert    181:                fprintf(stderr, "Changing password for %s.\n", pw->pw_name);
1.27      millert   182:                if (uid != 0 && pw->pw_passwd[0] != '\0') {
1.52      tedu      183:                        char oldpass[1024];
1.50      tedu      184:
                    185:                        p = readpassphrase("Old password:", oldpass,
                    186:                            sizeof(oldpass), RPP_ECHO_OFF);
1.31      millert   187:                        if (p == NULL || *p == '\0') {
1.60      robert    188:                                fprintf(stderr, "%s\n", UNCHANGED_MSG);
1.27      millert   189:                                pw_abort();
1.31      millert   190:                                exit(p == NULL ? 1 : 0);
1.27      millert   191:                        }
1.42      tedu      192:                        if (crypt_checkpass(p, pw->pw_passwd) != 0) {
1.27      millert   193:                                errno = EACCES;
1.51      gsoares   194:                                explicit_bzero(oldpass, sizeof(oldpass));
1.27      millert   195:                                pw_error(NULL, 1, 1);
                    196:                        }
1.51      gsoares   197:                        explicit_bzero(oldpass, sizeof(oldpass));
1.13      millert   198:                }
1.1       deraadt   199:        }
1.26      deraadt   200:
1.33      millert   201:        pwd_tries = pwd_gettries(lc);
1.1       deraadt   202:
1.50      tedu      203:        for (newpass[0] = '\0', tries = 0;;) {
1.52      tedu      204:                char repeat[1024];
1.50      tedu      205:
                    206:                p = readpassphrase("New password:", newpass, sizeof(newpass),
                    207:                    RPP_ECHO_OFF);
1.31      millert   208:                if (p == NULL || *p == '\0') {
1.60      robert    209:                        fprintf(stderr, "%s\n", UNCHANGED_MSG);
1.27      millert   210:                        pw_abort();
1.31      millert   211:                        exit(p == NULL ? 1 : 0);
1.9       deraadt   212:                }
                    213:                if (strcmp(p, "s/key") == 0) {
1.60      robert    214:                        fprintf(stderr, "That password collides with a system feature. Choose another.\n");
1.9       deraadt   215:                        continue;
1.1       deraadt   216:                }
1.26      deraadt   217:
1.37      deraadt   218:                if ((tries++ < pwd_tries || pwd_tries == 0) &&
                    219:                    pwd_check(lc, p) == 0)
1.1       deraadt   220:                        continue;
1.50      tedu      221:                p = readpassphrase("Retype new password:", repeat, sizeof(repeat),
                    222:                    RPP_ECHO_OFF);
1.53      millert   223:                if (p != NULL && strcmp(newpass, p) == 0) {
                    224:                        explicit_bzero(repeat, sizeof(repeat));
1.1       deraadt   225:                        break;
1.53      millert   226:                }
1.60      robert    227:                fprintf(stderr, "Mismatch; try again, EOF to quit.\n");
1.53      millert   228:                explicit_bzero(repeat, sizeof(repeat));
1.51      gsoares   229:                explicit_bzero(newpass, sizeof(newpass));
1.1       deraadt   230:        }
1.43      tedu      231:
1.24      millert   232:        (void)signal(SIGINT, saveint);
                    233:        (void)signal(SIGQUIT, savequit);
1.23      millert   234:
1.44      tedu      235:        pref = login_getcapstr(lc, "localcipher", NULL, NULL);
1.50      tedu      236:        if (crypt_newhash(newpass, pref, hash, sizeof(hash)) != 0) {
1.60      robert    237:                fprintf(stderr, "Couldn't generate hash.\n");
1.51      gsoares   238:                explicit_bzero(newpass, sizeof(newpass));
1.43      tedu      239:                pw_error(NULL, 0, 0);
                    240:        }
1.51      gsoares   241:        explicit_bzero(newpass, sizeof(newpass));
1.44      tedu      242:        free(pref);
1.43      tedu      243:        return hash;
1.15      millert   244: }
                    245:
                    246: void
1.26      deraadt   247: kbintr(int signo)
1.15      millert   248: {
1.49      tedu      249:        dprintf(STDOUT_FILENO, "\n%s\n", UNCHANGED_MSG);
1.27      millert   250:        _exit(0);
1.1       deraadt   251: }