Home | History | Annotate | Line # | Download | only in passwd
local_passwd.c revision 1.19
      1 /*	$NetBSD: local_passwd.c,v 1.19 2000/02/14 04:36:21 aidan 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.19 2000/02/14 04:36:21 aidan 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 unsigned char itoa64[] =		/* 0 ... 63 => ascii - 64 */
     70 	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     71 
     72 void
     73 to64(s, v, n)
     74 	char *s;
     75 	long v;
     76 	int n;
     77 {
     78 	while (--n >= 0) {
     79 		*s++ = itoa64[v&0x3f];
     80 		v >>= 6;
     81 	}
     82 }
     83 
     84 static char *
     85 getnewpasswd(pw, min_pw_len)
     86 	struct passwd *pw;
     87 	int min_pw_len;
     88 {
     89 	int tries;
     90 	char *p, *t;
     91 	char buf[_PASSWORD_LEN+1], salt[9];
     92 
     93 	(void)printf("Changing local password for %s.\n", pw->pw_name);
     94 
     95 	if (uid && pw->pw_passwd[0] &&
     96 	    strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
     97 	    pw->pw_passwd)) {
     98 		errno = EACCES;
     99 		pw_error(NULL, 1, 1);
    100 	}
    101 
    102 	for (buf[0] = '\0', tries = 0;;) {
    103 		p = getpass("New password:");
    104 		if (!*p) {
    105 			(void)printf("Password unchanged.\n");
    106 			pw_error(NULL, 0, 0);
    107 		}
    108 		if (min_pw_len > 0 && strlen(p) < min_pw_len) {
    109 			(void) printf("Password is too short.\n");
    110 			continue;
    111 		}
    112 		if (strlen(p) <= 5 && ++tries < 2) {
    113 			(void)printf("Please enter a longer password.\n");
    114 			continue;
    115 		}
    116 		for (t = p; *t && islower(*t); ++t);
    117 		if (!*t && ++tries < 2) {
    118 			(void)printf("Please don't use an all-lower case "
    119 				     "password.\nUnusual capitalization, "
    120 				     "control characters or digits are "
    121 				     "suggested.\n");
    122 			continue;
    123 		}
    124 		(void)strncpy(buf, p, sizeof(buf) - 1);
    125 		buf[sizeof(buf) - 1] = '\0';
    126 		if (!strcmp(buf, getpass("Retype new password:")))
    127 			break;
    128 		(void)printf("Mismatch; try again, EOF to quit.\n");
    129 	}
    130 	/* grab a random printable character that isn't a colon */
    131 	(void)srandom((int)time((time_t *)NULL));
    132 #ifdef NEWSALT
    133 	salt[0] = _PASSWORD_EFMT1;
    134 	to64(&salt[1], (long)(29 * 25), 4);
    135 	to64(&salt[5], random(), 4);
    136 #else
    137 	to64(&salt[0], random(), 2);
    138 #endif
    139 	return(crypt(buf, salt));
    140 }
    141 
    142 int
    143 local_init(progname)
    144 	const char *progname;
    145 {
    146 	force_local = 0;
    147 	return (0);
    148 }
    149 
    150 int
    151 local_arg(char arg, const char *optarg)
    152 {
    153 	switch (arg) {
    154 	case 'l':
    155 		force_local = 1;
    156 		break;
    157 	default:
    158 		return(0);
    159 	}
    160 	return(1);
    161 }
    162 
    163 int
    164 local_arg_end()
    165 {
    166 	if (force_local)
    167 		return(PW_USE_FORCE);
    168 	return(PW_USE);
    169 }
    170 
    171 void
    172 local_end()
    173 {
    174 	/* NOOP */
    175 }
    176 
    177 int
    178 local_chpw(uname)
    179 	const char *uname;
    180 {
    181 	struct passwd *pw;
    182 	struct passwd old_pw;
    183 	int pfd, tfd;
    184 	int min_pw_len = 0;
    185 	int pw_expiry  = 0;
    186 #ifdef LOGIN_CAP
    187 	login_cap_t *lc;
    188 #endif
    189 
    190 	if (!(pw = getpwnam(uname))) {
    191 		warnx("unknown user %s", uname);
    192 		return (1);
    193 	}
    194 
    195 	uid = getuid();
    196 	if (uid && uid != pw->pw_uid) {
    197 		warnx("%s", strerror(EACCES));
    198 		return (1);
    199 	}
    200 
    201 	/* Save the old pw information for comparing on pw_copy(). */
    202 	old_pw = *pw;
    203 
    204 	/*
    205 	 * Get class restrictions for this user, then get the new password.
    206 	 */
    207 #ifdef LOGIN_CAP
    208 	if((lc = login_getclass(pw->pw_class))) {
    209 		min_pw_len = (int) login_getcapnum(lc, "minpasswordlen", 0, 0);
    210 		pw_expiry  = (int) login_getcaptime(lc, "passwordtime", 0, 0);
    211 		login_close(lc);
    212 	}
    213 #endif
    214 
    215 	pw->pw_passwd = getnewpasswd(pw, min_pw_len);
    216 	pw->pw_change = pw_expiry ? pw_expiry + time(NULL) : 0;
    217 
    218 	/*
    219 	 * Now that the user has given us a new password, let us
    220 	 * change the database.
    221 	 */
    222 	pw_init();
    223 	tfd = pw_lock(0);
    224 	if (tfd < 0) {
    225 		warnx ("The passwd file is busy, waiting...");
    226 		tfd = pw_lock(10);
    227 		if (tfd < 0)
    228 			errx(1, "The passwd file is still busy, "
    229 			     "try again later.");
    230 	}
    231 
    232 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
    233 	if (pfd < 0)
    234 		pw_error(_PATH_MASTERPASSWD, 1, 1);
    235 
    236 	pw_copy(pfd, tfd, pw, &old_pw);
    237 
    238 	if (pw_mkdb() < 0)
    239 		pw_error((char *)NULL, 0, 1);
    240 	return (0);
    241 }
    242