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