xref: /netbsd-src/usr.bin/passwd/local_passwd.c (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * 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	5.5 (Berkeley) 5/6/91";
36 #endif /* not lint */
37 
38 #include <sys/types.h>
39 #include <pwd.h>
40 #include <errno.h>
41 #include <stdio.h>
42 
43 uid_t uid;
44 
45 char *progname = "passwd";
46 char *tempname;
47 
48 local_passwd(uname)
49 	char *uname;
50 {
51 	struct passwd *pw;
52 	int pfd, tfd;
53 	char *getnewpasswd();
54 
55 	if (!(pw = getpwnam(uname))) {
56 #ifdef YP
57 		extern int use_yp;
58 		if (!use_yp)
59 #endif
60 		(void)fprintf(stderr, "passwd: unknown user %s.\n", uname);
61 		return(1);
62 	}
63 
64 	uid = getuid();
65 	if (uid && uid != pw->pw_uid) {
66 		(void)fprintf(stderr, "passwd: %s\n", strerror(EACCES));
67 		return(1);
68 	}
69 
70 	pw_init();
71 	pfd = pw_lock();
72 	tfd = pw_tmp();
73 
74 	/*
75 	 * Get the new password.  Reset passwd change time to zero; when
76 	 * classes are implemented, go and get the "offset" value for this
77 	 * class and reset the timer.
78 	 */
79 	pw->pw_passwd = getnewpasswd(pw);
80 	pw->pw_change = 0;
81 	pw_copy(pfd, tfd, pw);
82 
83 	if (!pw_mkdb())
84 		pw_error((char *)NULL, 0, 1);
85 	return(0);
86 }
87 
88 char *
89 getnewpasswd(pw)
90 	register struct passwd *pw;
91 {
92 	register char *p, *t;
93 	int tries;
94 	char buf[_PASSWORD_LEN+1], salt[9], *crypt(), *getpass();
95 
96 	(void)printf("Changing local password for %s.\n", pw->pw_name);
97 
98 	if (uid && pw->pw_passwd[0] &&
99 	    strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
100 	    pw->pw_passwd)) {
101 		errno = EACCES;
102 		pw_error(NULL, 1, 1);
103 	}
104 
105 	for (buf[0] = '\0', tries = 0;;) {
106 		p = getpass("New password:");
107 		if (!*p) {
108 			(void)printf("Password unchanged.\n");
109 			pw_error(NULL, 0, 0);
110 		}
111 		if (strlen(p) <= 5 && (uid != 0 || ++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 && (uid != 0 || ++tries < 2)) {
117 			(void)printf("Please don't use an all-lower case password.\nUnusual capitalization, control characters or digits are suggested.\n");
118 			continue;
119 		}
120 		(void)strcpy(buf, p);
121 		if (!strcmp(buf, getpass("Retype new password:")))
122 			break;
123 		(void)printf("Mismatch; try again, EOF to quit.\n");
124 	}
125 	/* grab a random printable character that isn't a colon */
126 	(void)srandom((int)time((time_t *)NULL));
127 #ifdef NEWSALT
128 	salt[0] = _PASSWORD_EFMT1;
129 	to64(&salt[1], (long)(29 * 25), 4);
130 	to64(&salt[5], random(), 4);
131 #else
132 	to64(&salt[0], random(), 2);
133 #endif
134 	return(crypt(buf, salt));
135 }
136 
137 static unsigned char itoa64[] =		/* 0 ... 63 => ascii - 64 */
138 	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
139 
140 to64(s, v, n)
141 	register char *s;
142 	register long v;
143 	register int n;
144 {
145 	while (--n >= 0) {
146 		*s++ = itoa64[v&0x3f];
147 		v >>= 6;
148 	}
149 }
150