yp_passwd.c revision 1.19.2.1 1 /* $NetBSD: yp_passwd.c,v 1.19.2.1 2000/10/08 18:45:38 he 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.19.2.1 2000/10/08 18:45:38 he 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 priviledged
153 */
154 if (rpcport >= IPPORT_RESERVED)
155 errx(1, "yppasswd daemon is on an invalid port.");
156
157 /* Get user's login identity */
158 if (!ypgetpwnam(username) ||
159 !(pw = getpwnam(username))) {
160 errx(1, "unknown user %s", username);
161 }
162
163 if (uid && uid != pw->pw_uid)
164 errx(1, "you may only change your own password: %s",
165 strerror(EACCES));
166
167 /* prompt for new password */
168 yppasswd.newpw.pw_passwd = getnewpasswd(pw, &yppasswd.oldpass);
169
170 /* tell rpc.yppasswdd */
171 yppasswd.newpw.pw_name = strdup(pw->pw_name);
172 yppasswd.newpw.pw_uid = pw->pw_uid;
173 yppasswd.newpw.pw_gid = pw->pw_gid;
174 yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
175 yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
176 yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
177
178 client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
179 if (client == NULL) {
180 warnx("cannot contact yppasswdd on %s: Reason: %s",
181 master, yperr_string(YPERR_YPBIND));
182 return (YPERR_YPBIND);
183 }
184
185 client->cl_auth = authunix_create_default();
186 tv.tv_sec = 2;
187 tv.tv_usec = 0;
188 r = clnt_call(client, YPPASSWDPROC_UPDATE,
189 xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
190 if (r)
191 errx(1, "rpc to yppasswdd failed.");
192 else if (status)
193 printf("Couldn't change YP password.\n");
194 else
195 printf("The YP password has been changed on %s, %s\n",
196 master, "the master YP passwd server.");
197 exit(0);
198 }
199
200 static char *
201 getnewpasswd(pw, old_pass)
202 struct passwd *pw;
203 char **old_pass;
204 {
205 int tries;
206 char *p, *t;
207 static char buf[_PASSWORD_LEN+1];
208 char salt[9];
209
210 (void)printf("Changing YP password for %s.\n", pw->pw_name);
211
212 if (old_pass) {
213 *old_pass = NULL;
214
215 if (pw->pw_passwd[0]) {
216 if (strcmp(crypt(p = getpass("Old password:"),
217 pw->pw_passwd), pw->pw_passwd)) {
218 (void)printf("Sorry.\n");
219 pw_error(NULL, 0, 1);
220 }
221 } else {
222 p = "";
223 }
224
225 *old_pass = strdup(p);
226 }
227 for (buf[0] = '\0', tries = 0;;) {
228 p = getpass("New password:");
229 if (!*p) {
230 (void)printf("Password unchanged.\n");
231 pw_error(NULL, 0, 0);
232 }
233 if (strlen(p) <= 5 && ++tries < 2) {
234 (void)printf("Please enter a longer password.\n");
235 continue;
236 }
237 for (t = p; *t && islower(*t); ++t);
238 if (!*t && ++tries < 2) {
239 (void)printf("Please don't use an all-lower case "
240 "password.\nUnusual capitalization, "
241 "control characters or digits are "
242 "suggested.\n");
243 continue;
244 }
245 (void)strncpy(buf, p, sizeof(buf) - 1);
246 if (!strcmp(buf, getpass("Retype new password:")))
247 break;
248 (void)printf("Mismatch; try again, EOF to quit.\n");
249 }
250 /* grab a random printable character that isn't a colon */
251 (void)srandom((int)time((time_t *)NULL));
252 #ifdef NEWSALT
253 salt[0] = _PASSWORD_EFMT1;
254 to64(&salt[1], (long)(29 * 25), 4);
255 to64(&salt[5], random(), 4);
256 #else
257 to64(&salt[0], random(), 2);
258 #endif
259 return(strdup(crypt(buf, salt)));
260 }
261
262 static int
263 ypgetpwnam(nam)
264 char *nam;
265 {
266 char *val;
267 int reason, vallen;
268
269 val = NULL;
270 reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
271 &val, &vallen);
272 if (reason != 0) {
273 if (val != NULL)
274 free(val);
275 return 0;
276 }
277 free(val);
278 return 1;
279 }
280
281 #endif /* YP */
282