yp_passwd.c revision 1.9 1 /* $NetBSD: yp_passwd.c,v 1.9 1996/08/09 09:19:42 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1988 The Regents of the University of California.
5 * 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: @(#)yp_passwd.c 1.0 2/2/93";
39 #else
40 static char rcsid[] = "$NetBSD: yp_passwd.c,v 1.9 1996/08/09 09:19:42 thorpej Exp $";
41 #endif
42 #endif /* not lint */
43
44 #ifdef YP
45
46 #include <err.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <netdb.h>
50 #include <time.h>
51 #include <pwd.h>
52 #include <errno.h>
53 #include <rpc/rpc.h>
54 #include <rpcsvc/yp_prot.h>
55 #include <rpcsvc/ypclnt.h>
56 #define passwd yp_passwd_rec
57 #include <rpcsvc/yppasswd.h>
58 #undef passwd
59
60 #ifndef _PASSWORD_LEN
61 #define _PASSWORD_LEN PASS_MAX
62 #endif
63
64 extern char *__progname; /* from crt0.o */
65
66 static char *getnewpasswd();
67 static struct passwd *ypgetpwnam();
68
69 static uid_t uid;
70 char *domain;
71
72 static
73 pw_error(name, err, eval)
74 char *name;
75 int err, eval;
76 {
77 int sverrno;
78
79 if (err) {
80 sverrno = errno;
81 (void)fprintf(stderr, "%s: ", __progname);
82 if (name)
83 (void)fprintf(stderr, "%s: ", name);
84 (void)fprintf(stderr, "%s\n", strerror(sverrno));
85 }
86 errx(eval, "YP passwd database unchanged");
87 }
88
89 int
90 yp_passwd(username)
91 char *username;
92 {
93 char *master;
94 char *pp;
95 int r, rpcport, status;
96 struct yppasswd yppasswd;
97 struct passwd *pw;
98 struct timeval tv;
99 CLIENT *client;
100
101 uid = getuid();
102
103 /*
104 * Get local domain
105 */
106 if (r = yp_get_default_domain(&domain))
107 errx(1, "can't get local YP domain. Reason: %s",
108 yperr_string(r));
109
110 /*
111 * Find the host for the passwd map; it should be running
112 * the daemon.
113 */
114 if ((r = yp_master(domain, "passwd.byname", &master)) != 0)
115 errx(1, "can't find the master YP server. Reason: %s",
116 yperr_string(r));
117
118 /*
119 * Ask the portmapper for the port of the daemon.
120 */
121 if ((rpcport = getrpcport(master, YPPASSWDPROG,
122 YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0)
123 errx(1, "master YP server not running yppasswd daemon.\n\t%s\n",
124 "Can't change password.");
125
126 /*
127 * Be sure the port is priviledged
128 */
129 if (rpcport >= IPPORT_RESERVED)
130 errx(1, "yppasswd daemon is on an invalid port.");
131
132 /* Get user's login identity */
133 if (!(pw = ypgetpwnam(username)))
134 errx(1, "unknown user %s", username);
135
136 if (uid && uid != pw->pw_uid)
137 errx(1, "you may only change your own password: %s",
138 strerror(EACCES));
139
140 /* prompt for new password */
141 yppasswd.newpw.pw_passwd = getnewpasswd(pw, &yppasswd.oldpass);
142
143 /* tell rpc.yppasswdd */
144 yppasswd.newpw.pw_name = pw->pw_name;
145 yppasswd.newpw.pw_uid = pw->pw_uid;
146 yppasswd.newpw.pw_gid = pw->pw_gid;
147 yppasswd.newpw.pw_gecos = pw->pw_gecos;
148 yppasswd.newpw.pw_dir = pw->pw_dir;
149 yppasswd.newpw.pw_shell = pw->pw_shell;
150
151 client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
152 if (client==NULL) {
153 warnx("cannot contact yppasswdd on %s: Reason: %s",
154 master, yperr_string(YPERR_YPBIND));
155 return(YPERR_YPBIND);
156 }
157
158 client->cl_auth = authunix_create_default();
159 tv.tv_sec = 2;
160 tv.tv_usec = 0;
161 r = clnt_call(client, YPPASSWDPROC_UPDATE,
162 xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
163 if (r)
164 errx(1, "rpc to yppasswdd failed.");
165 else if (status)
166 printf("Couldn't change YP password.\n");
167 else
168 printf("The YP password has been changed on %s, %s\n",
169 master, "the master YP passwd server.");
170 exit(0);
171 }
172
173 static char *
174 getnewpasswd(pw, old_pass)
175 register struct passwd *pw;
176 char **old_pass;
177 {
178 static char buf[_PASSWORD_LEN+1];
179 register char *p, *t;
180 int tries;
181 char salt[9], *crypt(), *getpass();
182
183 (void)printf("Changing YP password for %s.\n", pw->pw_name);
184
185 if (old_pass) {
186 *old_pass = NULL;
187
188 if (pw->pw_passwd[0]) {
189 if (strcmp(crypt(p = getpass("Old password:"),
190 pw->pw_passwd), pw->pw_passwd)) {
191 errno = EACCES;
192 pw_error(NULL, 1, 1);
193 }
194 } else {
195 p = "";
196 }
197
198 *old_pass = strdup(p);
199 }
200 for (buf[0] = '\0', tries = 0;;) {
201 p = getpass("New password:");
202 if (!*p) {
203 (void)printf("Password unchanged.\n");
204 pw_error(NULL, 0, 0);
205 }
206 if (strlen(p) <= 5 && ++tries < 2) {
207 (void)printf("Please enter a longer password.\n");
208 continue;
209 }
210 for (t = p; *t && islower(*t); ++t);
211 if (!*t && ++tries < 2) {
212 (void)printf("Please don't use an all-lower case password.\nUnusual capitalization, control characters or digits are suggested.\n");
213 continue;
214 }
215 (void)strcpy(buf, p);
216 if (!strcmp(buf, getpass("Retype new password:")))
217 break;
218 (void)printf("Mismatch; try again, EOF to quit.\n");
219 }
220 /* grab a random printable character that isn't a colon */
221 (void)srandom((int)time((time_t *)NULL));
222 #ifdef NEWSALT
223 salt[0] = _PASSWORD_EFMT1;
224 to64(&salt[1], (long)(29 * 25), 4);
225 to64(&salt[5], random(), 4);
226 #else
227 to64(&salt[0], random(), 2);
228 #endif
229 return(strdup(crypt(buf, salt)));
230 }
231
232 static char *
233 pwskip(register char *p)
234 {
235 while (*p && *p != ':' && *p != '\n')
236 ++p;
237 if (*p)
238 *p++ = 0;
239 return (p);
240 }
241
242 struct passwd *
243 interpret(struct passwd *pwent, char *line)
244 {
245 register char *p = line;
246 register int c;
247
248 pwent->pw_passwd = "*";
249 pwent->pw_uid = 0;
250 pwent->pw_gid = 0;
251 pwent->pw_gecos = "";
252 pwent->pw_dir = "";
253 pwent->pw_shell = "";
254 pwent->pw_change = 0;
255 pwent->pw_expire = 0;
256 pwent->pw_class = "";
257
258 /* line without colon separators is no good, so ignore it */
259 if(!strchr(p,':'))
260 return(NULL);
261
262 pwent->pw_name = p;
263 p = pwskip(p);
264 pwent->pw_passwd = p;
265 p = pwskip(p);
266 pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
267 p = pwskip(p);
268 pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
269 p = pwskip(p);
270 pwent->pw_gecos = p;
271 p = pwskip(p);
272 pwent->pw_dir = p;
273 p = pwskip(p);
274 pwent->pw_shell = p;
275 while (*p && *p != '\n')
276 p++;
277 *p = '\0';
278 return (pwent);
279 }
280
281 static struct passwd *
282 ypgetpwnam(nam)
283 char *nam;
284 {
285 static struct passwd pwent;
286 static char line[1024];
287 char *val;
288 int reason, vallen;
289
290 reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
291 &val, &vallen);
292 switch(reason) {
293 case 0:
294 break;
295 default:
296 return (NULL);
297 break;
298 }
299 val[vallen] = '\0';
300 strcpy(line, val);
301 free(val);
302
303 return(interpret(&pwent, line));
304 }
305
306 #endif /* YP */
307