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