Home | History | Annotate | Line # | Download | only in passwd
local_passwd.c revision 1.1.1.2
      1 /*-
      2  * Copyright (c) 1990, 1993, 1994
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char sccsid[] = "@(#)local_passwd.c	8.3 (Berkeley) 4/2/94";
     36 #endif /* not lint */
     37 
     38 #include <sys/types.h>
     39 
     40 #include <ctype.h>
     41 #include <err.h>
     42 #include <errno.h>
     43 #include <pwd.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <time.h>
     48 #include <unistd.h>
     49 
     50 #include <pw_copy.h>
     51 #include <pw_util.h>
     52 
     53 #include "extern.h"
     54 
     55 static uid_t uid;
     56 
     57 char   *tempname;
     58 
     59 static unsigned char itoa64[] =		/* 0 ... 63 => ascii - 64 */
     60 	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     61 
     62 void
     63 to64(s, v, n)
     64 	char *s;
     65 	long v;
     66 	int n;
     67 {
     68 	while (--n >= 0) {
     69 		*s++ = itoa64[v&0x3f];
     70 		v >>= 6;
     71 	}
     72 }
     73 
     74 char *
     75 getnewpasswd(pw)
     76 	struct passwd *pw;
     77 {
     78 	int tries;
     79 	char *p, *t;
     80 	char buf[_PASSWORD_LEN+1], salt[9];
     81 
     82 	(void)printf("Changing local password for %s.\n", pw->pw_name);
     83 
     84 	if (uid && pw->pw_passwd[0] &&
     85 	    strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
     86 	    pw->pw_passwd)) {
     87 		errno = EACCES;
     88 		pw_error(NULL, 1, 1);
     89 	}
     90 
     91 	for (buf[0] = '\0', tries = 0;;) {
     92 		p = getpass("New password:");
     93 		if (!*p) {
     94 			(void)printf("Password unchanged.\n");
     95 			pw_error(NULL, 0, 0);
     96 		}
     97 		if (strlen(p) <= 5 && (uid != 0 || ++tries < 2)) {
     98 			(void)printf("Please enter a longer password.\n");
     99 			continue;
    100 		}
    101 		for (t = p; *t && islower(*t); ++t);
    102 		if (!*t && (uid != 0 || ++tries < 2)) {
    103 			(void)printf("Please don't use an all-lower case password.\nUnusual capitalization, control characters or digits are suggested.\n");
    104 			continue;
    105 		}
    106 		(void)strcpy(buf, p);
    107 		if (!strcmp(buf, getpass("Retype new password:")))
    108 			break;
    109 		(void)printf("Mismatch; try again, EOF to quit.\n");
    110 	}
    111 	/* grab a random printable character that isn't a colon */
    112 	(void)srandom((int)time((time_t *)NULL));
    113 #ifdef NEWSALT
    114 	salt[0] = _PASSWORD_EFMT1;
    115 	to64(&salt[1], (long)(29 * 25), 4);
    116 	to64(&salt[5], random(), 4);
    117 #else
    118 	to64(&salt[0], random(), 2);
    119 #endif
    120 	return (crypt(buf, salt));
    121 }
    122 
    123 int
    124 local_passwd(uname)
    125 	char *uname;
    126 {
    127 	struct passwd *pw;
    128 	int pfd, tfd;
    129 
    130 	if (!(pw = getpwnam(uname)))
    131 		errx(1, "unknown user %s", uname);
    132 
    133 	uid = getuid();
    134 	if (uid && uid != pw->pw_uid)
    135 		errx(1, "%s", strerror(EACCES));
    136 
    137 	pw_init();
    138 	pfd = pw_lock();
    139 	tfd = pw_tmp();
    140 
    141 	/*
    142 	 * Get the new password.  Reset passwd change time to zero; when
    143 	 * classes are implemented, go and get the "offset" value for this
    144 	 * class and reset the timer.
    145 	 */
    146 	pw->pw_passwd = getnewpasswd(pw);
    147 	pw->pw_change = 0;
    148 	pw_copy(pfd, tfd, pw);
    149 
    150 	if (!pw_mkdb())
    151 		pw_error((char *)NULL, 0, 1);
    152 	return (0);
    153 }
    154