Home | History | Annotate | Line # | Download | only in passwd
local_passwd.c revision 1.12
      1 /*	$NetBSD: local_passwd.c,v 1.12 1997/02/22 01:50:46 thorpej 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 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "from: @(#)local_passwd.c    8.3 (Berkeley) 4/2/94";
     39 #else
     40 static char rcsid[] = "$NetBSD: local_passwd.c,v 1.12 1997/02/22 01:50:46 thorpej Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 #include <sys/stat.h>
     46 #include <pwd.h>
     47 #include <errno.h>
     48 #include <stdio.h>
     49 #include <string.h>
     50 #include <unistd.h>
     51 #include <fcntl.h>
     52 #include <util.h>
     53 
     54 #include "extern.h"
     55 
     56 static uid_t uid;
     57 
     58 char *tempname;
     59 
     60 static unsigned char itoa64[] =		/* 0 ... 63 => ascii - 64 */
     61 	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     62 
     63 void
     64 to64(s, v, n)
     65 	char *s;
     66 	long v;
     67 	int n;
     68 {
     69 	while (--n >= 0) {
     70 		*s++ = itoa64[v&0x3f];
     71 		v >>= 6;
     72 	}
     73 }
     74 
     75 char *
     76 getnewpasswd(pw)
     77 	struct passwd *pw;
     78 {
     79 	int tries;
     80 	char *p, *t;
     81 	char buf[_PASSWORD_LEN+1], salt[9], *crypt(), *getpass();
     82 
     83 	(void)printf("Changing local password for %s.\n", pw->pw_name);
     84 
     85 	if (uid && pw->pw_passwd[0] &&
     86 	    strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
     87 	    pw->pw_passwd)) {
     88 		errno = EACCES;
     89 		pw_error(NULL, 1, 1);
     90 	}
     91 
     92 	for (buf[0] = '\0', tries = 0;;) {
     93 		p = getpass("New password:");
     94 		if (!*p) {
     95 			(void)printf("Password unchanged.\n");
     96 			pw_error(NULL, 0, 0);
     97 		}
     98 		if (strlen(p) <= 5 && ++tries < 2) {
     99 			(void)printf("Please enter a longer password.\n");
    100 			continue;
    101 		}
    102 		for (t = p; *t && islower(*t); ++t);
    103 		if (!*t && ++tries < 2) {
    104 			(void)printf("Please don't use an all-lower case "
    105 				     "password.\nUnusual capitalization, "
    106 				     "control characters or digits are "
    107 				     "suggested.\n");
    108 			continue;
    109 		}
    110 		(void)strncpy(buf, p, sizeof(buf) - 1);
    111 		if (!strcmp(buf, getpass("Retype new password:")))
    112 			break;
    113 		(void)printf("Mismatch; try again, EOF to quit.\n");
    114 	}
    115 	/* grab a random printable character that isn't a colon */
    116 	(void)srandom((int)time((time_t *)NULL));
    117 #ifdef NEWSALT
    118 	salt[0] = _PASSWORD_EFMT1;
    119 	to64(&salt[1], (long)(29 * 25), 4);
    120 	to64(&salt[5], random(), 4);
    121 #else
    122 	to64(&salt[0], random(), 2);
    123 #endif
    124 	return(crypt(buf, salt));
    125 }
    126 
    127 int
    128 local_passwd(uname)
    129 	char *uname;
    130 {
    131 	struct passwd *pw;
    132 	int pfd, tfd;
    133 	char *getnewpasswd();
    134 
    135 	if (!(pw = getpwnam(uname))) {
    136 		warnx("unknown user %s", uname);
    137 		return (1);
    138 	}
    139 
    140 	uid = getuid();
    141 	if (uid && uid != pw->pw_uid) {
    142 		warnx("%s", strerror(EACCES));
    143 		return (1);
    144 	}
    145 
    146 	pw_init();
    147 	tfd = pw_lock(0);
    148 	if (tfd < 0)
    149 		errx(1, "the passwd file is busy.");
    150 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
    151 	if (pfd < 0)
    152 		pw_error(_PATH_MASTERPASSWD, 1, 1);
    153 
    154 	/*
    155 	 * Get the new password.  Reset passwd change time to zero; when
    156 	 * classes are implemented, go and get the "offset" value for this
    157 	 * class and reset the timer.
    158 	 */
    159 	pw->pw_passwd = getnewpasswd(pw);
    160 	pw->pw_change = 0;
    161 	pw_copy(pfd, tfd, pw);
    162 
    163 	if (pw_mkdb() < 0)
    164 		pw_error((char *)NULL, 0, 1);
    165 	return (0);
    166 }
    167