1*e8235bc0SLionel Sambuc /* $NetBSD: local_passwd.c,v 1.36 2012/03/25 05:55:07 dholland Exp $ */
25c007436SBen Gras
35c007436SBen Gras /*-
45c007436SBen Gras * Copyright (c) 1990, 1993, 1994
55c007436SBen Gras * The Regents of the University of California. All rights reserved.
65c007436SBen Gras *
75c007436SBen Gras * Redistribution and use in source and binary forms, with or without
85c007436SBen Gras * modification, are permitted provided that the following conditions
95c007436SBen Gras * are met:
105c007436SBen Gras * 1. Redistributions of source code must retain the above copyright
115c007436SBen Gras * notice, this list of conditions and the following disclaimer.
125c007436SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
135c007436SBen Gras * notice, this list of conditions and the following disclaimer in the
145c007436SBen Gras * documentation and/or other materials provided with the distribution.
155c007436SBen Gras * 3. Neither the name of the University nor the names of its contributors
165c007436SBen Gras * may be used to endorse or promote products derived from this software
175c007436SBen Gras * without specific prior written permission.
185c007436SBen Gras *
195c007436SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205c007436SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215c007436SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225c007436SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235c007436SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245c007436SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255c007436SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265c007436SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275c007436SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285c007436SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295c007436SBen Gras * SUCH DAMAGE.
305c007436SBen Gras */
315c007436SBen Gras
325c007436SBen Gras #include <sys/cdefs.h>
335c007436SBen Gras #ifndef lint
345c007436SBen Gras #if 0
355c007436SBen Gras static char sccsid[] = "from: @(#)local_passwd.c 8.3 (Berkeley) 4/2/94";
365c007436SBen Gras #else
37*e8235bc0SLionel Sambuc __RCSID("$NetBSD: local_passwd.c,v 1.36 2012/03/25 05:55:07 dholland Exp $");
385c007436SBen Gras #endif
395c007436SBen Gras #endif /* not lint */
405c007436SBen Gras
415c007436SBen Gras #include <sys/types.h>
425c007436SBen Gras #include <sys/stat.h>
435c007436SBen Gras #include <ctype.h>
445c007436SBen Gras #include <err.h>
455c007436SBen Gras #include <errno.h>
465c007436SBen Gras #include <fcntl.h>
475c007436SBen Gras #include <pwd.h>
485c007436SBen Gras #include <stdio.h>
495c007436SBen Gras #include <stdlib.h>
505c007436SBen Gras #include <string.h>
515c007436SBen Gras #include <limits.h>
525c007436SBen Gras #include <time.h>
535c007436SBen Gras #include <unistd.h>
545c007436SBen Gras #include <util.h>
555c007436SBen Gras #include <login_cap.h>
565c007436SBen Gras #include <syslog.h>
575c007436SBen Gras
585c007436SBen Gras #include "extern.h"
595c007436SBen Gras
605c007436SBen Gras static uid_t uid;
615c007436SBen Gras
625c007436SBen Gras static char *
getnewpasswd(struct passwd * pw,int min_pw_len)635c007436SBen Gras getnewpasswd(struct passwd *pw, int min_pw_len)
645c007436SBen Gras {
655c007436SBen Gras int tries;
665c007436SBen Gras char *p, *t;
675c007436SBen Gras char buf[_PASSWORD_LEN+1], salt[_PASSWORD_LEN+1];
685c007436SBen Gras char option[LINE_MAX], *key, *opt;
695c007436SBen Gras
705c007436SBen Gras (void)printf("Changing local password for %s.\n", pw->pw_name);
715c007436SBen Gras
725c007436SBen Gras if (uid && pw->pw_passwd[0] &&
735c007436SBen Gras strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
745c007436SBen Gras pw->pw_passwd)) {
755c007436SBen Gras errno = EACCES;
765c007436SBen Gras syslog(LOG_AUTH | LOG_NOTICE,
775c007436SBen Gras "user %s (UID %lu) failed to change the "
785c007436SBen Gras "local password of user %s: %m",
795c007436SBen Gras pw->pw_name, (unsigned long)uid, pw->pw_name);
805c007436SBen Gras pw_error(NULL, 1, 1);
815c007436SBen Gras }
825c007436SBen Gras
835c007436SBen Gras for (buf[0] = '\0', tries = 0;;) {
845c007436SBen Gras p = getpass("New password:");
855c007436SBen Gras if (!*p) {
865c007436SBen Gras (void)printf("Password unchanged.\n");
875c007436SBen Gras pw_error(NULL, 0, 0);
885c007436SBen Gras }
895c007436SBen Gras if (min_pw_len > 0 && (int)strlen(p) < min_pw_len) {
905c007436SBen Gras (void) printf("Password is too short.\n");
915c007436SBen Gras continue;
925c007436SBen Gras }
935c007436SBen Gras if (strlen(p) <= 5 && ++tries < 2) {
945c007436SBen Gras (void)printf("Please enter a longer password.\n");
955c007436SBen Gras continue;
965c007436SBen Gras }
975c007436SBen Gras for (t = p; *t && islower((unsigned char)*t); ++t);
985c007436SBen Gras if (!*t && ++tries < 2) {
995c007436SBen Gras (void)printf("Please don't use an all-lower case "
1005c007436SBen Gras "password.\nUnusual capitalization, "
1015c007436SBen Gras "control characters or digits are "
1025c007436SBen Gras "suggested.\n");
1035c007436SBen Gras continue;
1045c007436SBen Gras }
1055c007436SBen Gras (void)strlcpy(buf, p, sizeof(buf));
1065c007436SBen Gras if (!strcmp(buf, getpass("Retype new password:")))
1075c007436SBen Gras break;
1085c007436SBen Gras (void)printf("Mismatch; try again, EOF to quit.\n");
1095c007436SBen Gras }
1105c007436SBen Gras
1115c007436SBen Gras pw_getpwconf(option, sizeof(option), pw, "localcipher");
1125c007436SBen Gras opt = option;
1135c007436SBen Gras key = strsep(&opt, ",");
1145c007436SBen Gras if(pw_gensalt(salt, _PASSWORD_LEN, key, opt) == -1) {
1155c007436SBen Gras warn("Couldn't generate salt");
1165c007436SBen Gras pw_error(NULL, 0, 0);
1175c007436SBen Gras }
1185c007436SBen Gras return(crypt(buf, salt));
1195c007436SBen Gras }
1205c007436SBen Gras
1215c007436SBen Gras #ifdef USE_PAM
1225c007436SBen Gras
1235c007436SBen Gras void
pwlocal_usage(const char * prefix)1245c007436SBen Gras pwlocal_usage(const char *prefix)
1255c007436SBen Gras {
1265c007436SBen Gras
1275c007436SBen Gras (void) fprintf(stderr, "%s %s [-d files | -l] [user]\n",
1285c007436SBen Gras prefix, getprogname());
1295c007436SBen Gras }
1305c007436SBen Gras
1315c007436SBen Gras void
pwlocal_process(const char * username,int argc,char ** argv)1325c007436SBen Gras pwlocal_process(const char *username, int argc, char **argv)
1335c007436SBen Gras {
1345c007436SBen Gras struct passwd *pw;
1355c007436SBen Gras struct passwd old_pw;
1365c007436SBen Gras time_t old_change;
1375c007436SBen Gras int pfd, tfd;
1385c007436SBen Gras int min_pw_len = 0;
1395c007436SBen Gras int pw_expiry = 0;
1405c007436SBen Gras int ch;
1415c007436SBen Gras #ifdef LOGIN_CAP
1425c007436SBen Gras login_cap_t *lc;
1435c007436SBen Gras #endif
1445c007436SBen Gras
1455c007436SBen Gras while ((ch = getopt(argc, argv, "l")) != -1) {
1465c007436SBen Gras switch (ch) {
1475c007436SBen Gras case 'l':
1485c007436SBen Gras /*
1495c007436SBen Gras * Aborb the -l that may have gotten us here.
1505c007436SBen Gras */
1515c007436SBen Gras break;
1525c007436SBen Gras
1535c007436SBen Gras default:
1545c007436SBen Gras usage();
1555c007436SBen Gras /* NOTREACHED */
1565c007436SBen Gras }
1575c007436SBen Gras }
1585c007436SBen Gras
1595c007436SBen Gras argc -= optind;
1605c007436SBen Gras argv += optind;
1615c007436SBen Gras
1625c007436SBen Gras switch (argc) {
1635c007436SBen Gras case 0:
1645c007436SBen Gras /* username already provided */
1655c007436SBen Gras break;
1665c007436SBen Gras case 1:
1675c007436SBen Gras username = argv[0];
1685c007436SBen Gras break;
1695c007436SBen Gras default:
1705c007436SBen Gras usage();
1715c007436SBen Gras /* NOTREACHED */
1725c007436SBen Gras }
1735c007436SBen Gras
1745c007436SBen Gras if (!(pw = getpwnam(username)))
1755c007436SBen Gras errx(1, "unknown user %s", username);
1765c007436SBen Gras
1775c007436SBen Gras uid = getuid();
1785c007436SBen Gras if (uid && uid != pw->pw_uid)
1795c007436SBen Gras errx(1, "%s", strerror(EACCES));
1805c007436SBen Gras
1815c007436SBen Gras /* Save the old pw information for comparing on pw_copy(). */
1825c007436SBen Gras old_pw = *pw;
1835c007436SBen Gras
1845c007436SBen Gras /*
1855c007436SBen Gras * Get class restrictions for this user, then get the new password.
1865c007436SBen Gras */
1875c007436SBen Gras #ifdef LOGIN_CAP
1885c007436SBen Gras if((lc = login_getclass(pw->pw_class)) != NULL) {
1895c007436SBen Gras min_pw_len = (int) login_getcapnum(lc, "minpasswordlen", 0, 0);
1905c007436SBen Gras pw_expiry = (int) login_getcaptime(lc, "passwordtime", 0, 0);
1915c007436SBen Gras login_close(lc);
1925c007436SBen Gras }
1935c007436SBen Gras #endif
194*e8235bc0SLionel Sambuc
1955c007436SBen Gras pw->pw_passwd = getnewpasswd(pw, min_pw_len);
1965c007436SBen Gras old_change = pw->pw_change;
1975c007436SBen Gras pw->pw_change = pw_expiry ? pw_expiry + time(NULL) : 0;
1985c007436SBen Gras
1995c007436SBen Gras /*
2005c007436SBen Gras * Now that the user has given us a new password, let us
2015c007436SBen Gras * change the database.
2025c007436SBen Gras */
2035c007436SBen Gras pw_init();
2045c007436SBen Gras tfd = pw_lock(0);
2055c007436SBen Gras if (tfd < 0) {
2065c007436SBen Gras warnx ("The passwd file is busy, waiting...");
2075c007436SBen Gras tfd = pw_lock(10);
2085c007436SBen Gras if (tfd < 0)
2095c007436SBen Gras errx(1, "The passwd file is still busy, "
2105c007436SBen Gras "try again later.");
2115c007436SBen Gras }
2125c007436SBen Gras
2135c007436SBen Gras pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
2145c007436SBen Gras if (pfd < 0)
2155c007436SBen Gras pw_error(_PATH_MASTERPASSWD, 1, 1);
2165c007436SBen Gras
2175c007436SBen Gras pw_copy(pfd, tfd, pw, &old_pw);
2185c007436SBen Gras
2195c007436SBen Gras if (pw_mkdb(username, old_change == pw->pw_change) < 0)
220*e8235bc0SLionel Sambuc pw_error(NULL, 0, 1);
2215c007436SBen Gras
2225c007436SBen Gras syslog(LOG_AUTH | LOG_INFO,
2235c007436SBen Gras "user %s (UID %lu) successfully changed "
2245c007436SBen Gras "the local password of user %s",
2255c007436SBen Gras uid ? username : "root", (unsigned long)uid, username);
2265c007436SBen Gras }
2275c007436SBen Gras
2285c007436SBen Gras #else /* ! USE_PAM */
2295c007436SBen Gras
2305c007436SBen Gras static int force_local;
2315c007436SBen Gras
2325c007436SBen Gras int
local_init(const char * progname)233*e8235bc0SLionel Sambuc local_init(const char *progname)
2345c007436SBen Gras {
2355c007436SBen Gras force_local = 0;
2365c007436SBen Gras return (0);
2375c007436SBen Gras }
2385c007436SBen Gras
2395c007436SBen Gras int
local_arg(char ch,const char * arg)2405c007436SBen Gras local_arg(char ch, const char *arg)
2415c007436SBen Gras {
2425c007436SBen Gras switch (ch) {
2435c007436SBen Gras case 'l':
2445c007436SBen Gras force_local = 1;
2455c007436SBen Gras break;
2465c007436SBen Gras default:
2475c007436SBen Gras return(0);
2485c007436SBen Gras }
2495c007436SBen Gras return(1);
2505c007436SBen Gras }
2515c007436SBen Gras
2525c007436SBen Gras int
local_arg_end(void)253*e8235bc0SLionel Sambuc local_arg_end(void)
2545c007436SBen Gras {
2555c007436SBen Gras if (force_local)
2565c007436SBen Gras return(PW_USE_FORCE);
2575c007436SBen Gras return(PW_USE);
2585c007436SBen Gras }
2595c007436SBen Gras
2605c007436SBen Gras void
local_end(void)261*e8235bc0SLionel Sambuc local_end(void)
2625c007436SBen Gras {
2635c007436SBen Gras /* NOOP */
2645c007436SBen Gras }
2655c007436SBen Gras
2665c007436SBen Gras int
local_chpw(const char * uname)267*e8235bc0SLionel Sambuc local_chpw(const char *uname)
2685c007436SBen Gras {
2695c007436SBen Gras struct passwd *pw;
2705c007436SBen Gras struct passwd old_pw;
2715c007436SBen Gras time_t old_change;
2725c007436SBen Gras int pfd, tfd;
2735c007436SBen Gras int min_pw_len = 0;
2745c007436SBen Gras int pw_expiry = 0;
2755c007436SBen Gras #ifdef LOGIN_CAP
2765c007436SBen Gras login_cap_t *lc;
2775c007436SBen Gras #endif
2785c007436SBen Gras
2795c007436SBen Gras if (!(pw = getpwnam(uname))) {
2805c007436SBen Gras warnx("unknown user %s", uname);
2815c007436SBen Gras return (1);
2825c007436SBen Gras }
2835c007436SBen Gras
2845c007436SBen Gras uid = getuid();
2855c007436SBen Gras if (uid && uid != pw->pw_uid) {
2865c007436SBen Gras warnx("%s", strerror(EACCES));
2875c007436SBen Gras return (1);
2885c007436SBen Gras }
2895c007436SBen Gras
2905c007436SBen Gras /* Save the old pw information for comparing on pw_copy(). */
2915c007436SBen Gras old_pw = *pw;
2925c007436SBen Gras
2935c007436SBen Gras /*
2945c007436SBen Gras * Get class restrictions for this user, then get the new password.
2955c007436SBen Gras */
2965c007436SBen Gras #ifdef LOGIN_CAP
2975c007436SBen Gras if((lc = login_getclass(pw->pw_class))) {
2985c007436SBen Gras min_pw_len = (int) login_getcapnum(lc, "minpasswordlen", 0, 0);
2995c007436SBen Gras pw_expiry = (int) login_getcaptime(lc, "passwordtime", 0, 0);
3005c007436SBen Gras login_close(lc);
3015c007436SBen Gras }
3025c007436SBen Gras #endif
303*e8235bc0SLionel Sambuc
3045c007436SBen Gras pw->pw_passwd = getnewpasswd(pw, min_pw_len);
3055c007436SBen Gras old_change = pw->pw_change;
3065c007436SBen Gras pw->pw_change = pw_expiry ? pw_expiry + time(NULL) : 0;
3075c007436SBen Gras
3085c007436SBen Gras /*
3095c007436SBen Gras * Now that the user has given us a new password, let us
3105c007436SBen Gras * change the database.
3115c007436SBen Gras */
3125c007436SBen Gras pw_init();
3135c007436SBen Gras tfd = pw_lock(0);
3145c007436SBen Gras if (tfd < 0) {
3155c007436SBen Gras warnx ("The passwd file is busy, waiting...");
3165c007436SBen Gras tfd = pw_lock(10);
3175c007436SBen Gras if (tfd < 0)
3185c007436SBen Gras errx(1, "The passwd file is still busy, "
3195c007436SBen Gras "try again later.");
3205c007436SBen Gras }
3215c007436SBen Gras
3225c007436SBen Gras pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
3235c007436SBen Gras if (pfd < 0)
3245c007436SBen Gras pw_error(_PATH_MASTERPASSWD, 1, 1);
3255c007436SBen Gras
3265c007436SBen Gras pw_copy(pfd, tfd, pw, &old_pw);
3275c007436SBen Gras
3285c007436SBen Gras if (pw_mkdb(uname, old_change == pw->pw_change) < 0)
329*e8235bc0SLionel Sambuc pw_error(NULL, 0, 1);
3305c007436SBen Gras
3315c007436SBen Gras syslog(LOG_AUTH | LOG_INFO,
3325c007436SBen Gras "user %s (UID %lu) successfully changed "
3335c007436SBen Gras "the local password of user %s",
3345c007436SBen Gras uid ? uname : "root", (unsigned long)uid, uname);
3355c007436SBen Gras
3365c007436SBen Gras return (0);
3375c007436SBen Gras }
3385c007436SBen Gras
3395c007436SBen Gras #endif /* USE_PAM */
340