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