Home | History | Annotate | Line # | Download | only in passwd
local_passwd.c revision 1.26
      1 /*	$NetBSD: local_passwd.c,v 1.26 2003/08/07 11:15:26 agc Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993, 1994
      5  * 	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
     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/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "from: @(#)local_passwd.c    8.3 (Berkeley) 4/2/94";
     36 #else
     37 __RCSID("$NetBSD: local_passwd.c,v 1.26 2003/08/07 11:15:26 agc Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/types.h>
     42 #include <sys/stat.h>
     43 #include <ctype.h>
     44 #include <err.h>
     45 #include <errno.h>
     46 #include <fcntl.h>
     47 #include <pwd.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <time.h>
     52 #include <unistd.h>
     53 #include <util.h>
     54 #include <login_cap.h>
     55 
     56 #include "extern.h"
     57 
     58 static	char   *getnewpasswd __P((struct passwd *, int));
     59 
     60 static uid_t uid;
     61 static int force_local;
     62 
     63 char *tempname;
     64 
     65 static char *
     66 getnewpasswd(pw, min_pw_len)
     67 	struct passwd *pw;
     68 	int min_pw_len;
     69 {
     70 	int tries;
     71 	char *p, *t;
     72 	char buf[_PASSWORD_LEN+1], salt[_PASSWORD_LEN+1];
     73 
     74 	(void)printf("Changing local password for %s.\n", pw->pw_name);
     75 
     76 	if (uid && pw->pw_passwd[0] &&
     77 	    strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
     78 	    pw->pw_passwd)) {
     79 		errno = EACCES;
     80 		pw_error(NULL, 1, 1);
     81 	}
     82 
     83 	for (buf[0] = '\0', tries = 0;;) {
     84 		p = getpass("New password:");
     85 		if (!*p) {
     86 			(void)printf("Password unchanged.\n");
     87 			pw_error(NULL, 0, 0);
     88 		}
     89 		if (min_pw_len > 0 && strlen(p) < min_pw_len) {
     90 			(void) printf("Password is too short.\n");
     91 			continue;
     92 		}
     93 		if (strlen(p) <= 5 && ++tries < 2) {
     94 			(void)printf("Please enter a longer password.\n");
     95 			continue;
     96 		}
     97 		for (t = p; *t && islower(*t); ++t);
     98 		if (!*t && ++tries < 2) {
     99 			(void)printf("Please don't use an all-lower case "
    100 				     "password.\nUnusual capitalization, "
    101 				     "control characters or digits are "
    102 				     "suggested.\n");
    103 			continue;
    104 		}
    105 		(void)strlcpy(buf, p, sizeof(buf));
    106 		if (!strcmp(buf, getpass("Retype new password:")))
    107 			break;
    108 		(void)printf("Mismatch; try again, EOF to quit.\n");
    109 	}
    110 
    111 	if(!pwd_gensalt(salt, _PASSWORD_LEN, pw, 'l')) {
    112 		(void)printf("Couldn't generate salt.\n");
    113 		pw_error(NULL, 0, 0);
    114 	}
    115 	return(crypt(buf, salt));
    116 }
    117 
    118 int
    119 local_init(progname)
    120 	const char *progname;
    121 {
    122 	force_local = 0;
    123 	return (0);
    124 }
    125 
    126 int
    127 local_arg(char arg, const char *optarg)
    128 {
    129 	switch (arg) {
    130 	case 'l':
    131 		force_local = 1;
    132 		break;
    133 	default:
    134 		return(0);
    135 	}
    136 	return(1);
    137 }
    138 
    139 int
    140 local_arg_end()
    141 {
    142 	if (force_local)
    143 		return(PW_USE_FORCE);
    144 	return(PW_USE);
    145 }
    146 
    147 void
    148 local_end()
    149 {
    150 	/* NOOP */
    151 }
    152 
    153 int
    154 local_chpw(uname)
    155 	const char *uname;
    156 {
    157 	struct passwd *pw;
    158 	struct passwd old_pw;
    159 	time_t old_change;
    160 	int pfd, tfd;
    161 	int min_pw_len = 0;
    162 	int pw_expiry  = 0;
    163 #ifdef LOGIN_CAP
    164 	login_cap_t *lc;
    165 #endif
    166 
    167 	if (!(pw = getpwnam(uname))) {
    168 		warnx("unknown user %s", uname);
    169 		return (1);
    170 	}
    171 
    172 	uid = getuid();
    173 	if (uid && uid != pw->pw_uid) {
    174 		warnx("%s", strerror(EACCES));
    175 		return (1);
    176 	}
    177 
    178 	/* Save the old pw information for comparing on pw_copy(). */
    179 	old_pw = *pw;
    180 
    181 	/*
    182 	 * Get class restrictions for this user, then get the new password.
    183 	 */
    184 #ifdef LOGIN_CAP
    185 	if((lc = login_getclass(pw->pw_class))) {
    186 		min_pw_len = (int) login_getcapnum(lc, "minpasswordlen", 0, 0);
    187 		pw_expiry  = (int) login_getcaptime(lc, "passwordtime", 0, 0);
    188 		login_close(lc);
    189 	}
    190 #endif
    191 
    192 	pw->pw_passwd = getnewpasswd(pw, min_pw_len);
    193 	old_change = pw->pw_change;
    194 	pw->pw_change = pw_expiry ? pw_expiry + time(NULL) : 0;
    195 
    196 	/*
    197 	 * Now that the user has given us a new password, let us
    198 	 * change the database.
    199 	 */
    200 	pw_init();
    201 	tfd = pw_lock(0);
    202 	if (tfd < 0) {
    203 		warnx ("The passwd file is busy, waiting...");
    204 		tfd = pw_lock(10);
    205 		if (tfd < 0)
    206 			errx(1, "The passwd file is still busy, "
    207 			     "try again later.");
    208 	}
    209 
    210 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
    211 	if (pfd < 0)
    212 		pw_error(_PATH_MASTERPASSWD, 1, 1);
    213 
    214 	pw_copy(pfd, tfd, pw, &old_pw);
    215 
    216 	if (pw_mkdb(uname, old_change == pw->pw_change) < 0)
    217 		pw_error((char *)NULL, 0, 1);
    218 	return (0);
    219 }
    220