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