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