yp_passwd.c revision 1.25 1 1.25 itojun /* $NetBSD: yp_passwd.c,v 1.25 2002/11/16 04:41:50 itojun Exp $ */
2 1.9 thorpej
3 1.1 brezak /*
4 1.11 tls * Copyright (c) 1988, 1990, 1993, 1994
5 1.11 tls * The Regents of the University of California. All rights reserved.
6 1.1 brezak *
7 1.1 brezak * Redistribution and use in source and binary forms, with or without
8 1.1 brezak * modification, are permitted provided that the following conditions
9 1.1 brezak * are met:
10 1.1 brezak * 1. Redistributions of source code must retain the above copyright
11 1.1 brezak * notice, this list of conditions and the following disclaimer.
12 1.1 brezak * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 brezak * notice, this list of conditions and the following disclaimer in the
14 1.1 brezak * documentation and/or other materials provided with the distribution.
15 1.1 brezak * 3. All advertising materials mentioning features or use of this software
16 1.1 brezak * must display the following acknowledgement:
17 1.1 brezak * This product includes software developed by the University of
18 1.1 brezak * California, Berkeley and its contributors.
19 1.1 brezak * 4. Neither the name of the University nor the names of its contributors
20 1.1 brezak * may be used to endorse or promote products derived from this software
21 1.1 brezak * without specific prior written permission.
22 1.1 brezak *
23 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 brezak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 brezak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 brezak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 brezak * SUCH DAMAGE.
34 1.1 brezak */
35 1.9 thorpej
36 1.15 lukem #include <sys/cdefs.h>
37 1.1 brezak #ifndef lint
38 1.9 thorpej #if 0
39 1.11 tls static char sccsid[] = "from: @(#)local_passwd.c 8.3 (Berkeley) 4/2/94";
40 1.9 thorpej #else
41 1.25 itojun __RCSID("$NetBSD: yp_passwd.c,v 1.25 2002/11/16 04:41:50 itojun Exp $");
42 1.9 thorpej #endif
43 1.1 brezak #endif /* not lint */
44 1.1 brezak
45 1.1 brezak #ifdef YP
46 1.1 brezak
47 1.15 lukem #include <ctype.h>
48 1.9 thorpej #include <err.h>
49 1.15 lukem #include <errno.h>
50 1.15 lukem #include <netdb.h>
51 1.15 lukem #include <pwd.h>
52 1.1 brezak #include <stdio.h>
53 1.15 lukem #include <stdlib.h>
54 1.1 brezak #include <string.h>
55 1.1 brezak #include <time.h>
56 1.15 lukem #include <unistd.h>
57 1.15 lukem
58 1.1 brezak #include <rpc/rpc.h>
59 1.1 brezak #include <rpcsvc/yp_prot.h>
60 1.1 brezak #include <rpcsvc/ypclnt.h>
61 1.15 lukem
62 1.15 lukem #include "extern.h"
63 1.15 lukem
64 1.1 brezak #define passwd yp_passwd_rec
65 1.1 brezak #include <rpcsvc/yppasswd.h>
66 1.1 brezak #undef passwd
67 1.4 deraadt
68 1.1 brezak #ifndef _PASSWORD_LEN
69 1.1 brezak #define _PASSWORD_LEN PASS_MAX
70 1.1 brezak #endif
71 1.9 thorpej
72 1.22 aidan static int yflag;
73 1.10 thorpej
74 1.15 lukem static char *getnewpasswd __P((struct passwd *, char **));
75 1.22 aidan static int ypgetpwnam __P((const char *));
76 1.15 lukem static void pw_error __P((char *, int, int));
77 1.1 brezak
78 1.1 brezak static uid_t uid;
79 1.1 brezak char *domain;
80 1.1 brezak
81 1.15 lukem static void
82 1.4 deraadt pw_error(name, err, eval)
83 1.4 deraadt char *name;
84 1.4 deraadt int err, eval;
85 1.4 deraadt {
86 1.17 mrg
87 1.15 lukem if (err)
88 1.18 mrg warn("%s", name);
89 1.9 thorpej errx(eval, "YP passwd database unchanged");
90 1.4 deraadt }
91 1.4 deraadt
92 1.22 aidan int
93 1.22 aidan yp_init(progname)
94 1.22 aidan const char *progname;
95 1.22 aidan {
96 1.22 aidan int yppwd;
97 1.22 aidan
98 1.22 aidan if (strcmp(progname, "yppasswd") == 0) {
99 1.22 aidan yppwd = 1;
100 1.22 aidan } else
101 1.22 aidan yppwd = 0;
102 1.22 aidan yflag = 0;
103 1.22 aidan if (_yp_check(NULL) == 0) {
104 1.22 aidan /* can't use YP. */
105 1.22 aidan if (yppwd)
106 1.22 aidan errx(1, "YP not in use.");
107 1.22 aidan return(-1);
108 1.22 aidan }
109 1.22 aidan return (0);
110 1.22 aidan }
111 1.22 aidan
112 1.22 aidan int
113 1.22 aidan yp_arg(ch, arg)
114 1.22 aidan char ch;
115 1.22 aidan const char *arg;
116 1.22 aidan {
117 1.22 aidan switch (ch) {
118 1.22 aidan case 'y':
119 1.22 aidan yflag = 1;
120 1.22 aidan break;
121 1.22 aidan default:
122 1.22 aidan return(0);
123 1.22 aidan }
124 1.22 aidan return(1);
125 1.22 aidan }
126 1.22 aidan
127 1.22 aidan int
128 1.22 aidan yp_arg_end()
129 1.16 tv {
130 1.22 aidan if (yflag)
131 1.22 aidan return (PW_USE_FORCE);
132 1.22 aidan return (PW_USE);
133 1.22 aidan }
134 1.17 mrg
135 1.22 aidan void
136 1.22 aidan yp_end()
137 1.22 aidan {
138 1.22 aidan /* NOOP */
139 1.16 tv }
140 1.16 tv
141 1.9 thorpej int
142 1.22 aidan yp_chpw(username)
143 1.22 aidan const char *username;
144 1.1 brezak {
145 1.4 deraadt char *master;
146 1.4 deraadt int r, rpcport, status;
147 1.4 deraadt struct yppasswd yppasswd;
148 1.4 deraadt struct passwd *pw;
149 1.1 brezak struct timeval tv;
150 1.1 brezak CLIENT *client;
151 1.21 mjl
152 1.4 deraadt uid = getuid();
153 1.4 deraadt
154 1.4 deraadt /*
155 1.4 deraadt * Get local domain
156 1.4 deraadt */
157 1.22 aidan if ((r = yp_get_default_domain(&domain)) != 0)
158 1.9 thorpej errx(1, "can't get local YP domain. Reason: %s",
159 1.9 thorpej yperr_string(r));
160 1.4 deraadt
161 1.4 deraadt /*
162 1.4 deraadt * Find the host for the passwd map; it should be running
163 1.4 deraadt * the daemon.
164 1.4 deraadt */
165 1.16 tv if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
166 1.22 aidan warnx("can't find the master YP server. Reason: %s",
167 1.9 thorpej yperr_string(r));
168 1.22 aidan /* continuation */
169 1.22 aidan return(-1);
170 1.16 tv }
171 1.1 brezak
172 1.4 deraadt /*
173 1.4 deraadt * Ask the portmapper for the port of the daemon.
174 1.4 deraadt */
175 1.4 deraadt if ((rpcport = getrpcport(master, YPPASSWDPROG,
176 1.10 thorpej YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
177 1.22 aidan warnx("master YP server not running yppasswd daemon.\n\t%s\n",
178 1.22 aidan "Can't change YP password.");
179 1.22 aidan /* continuation */
180 1.22 aidan return(-1);
181 1.10 thorpej }
182 1.1 brezak
183 1.4 deraadt /*
184 1.20 simonb * Be sure the port is privileged
185 1.4 deraadt */
186 1.9 thorpej if (rpcport >= IPPORT_RESERVED)
187 1.9 thorpej errx(1, "yppasswd daemon is on an invalid port.");
188 1.4 deraadt
189 1.21 mjl /* Bail out if this is a local (non-yp) user, */
190 1.21 mjl /* then get user's login identity */
191 1.21 mjl if (!ypgetpwnam(username) ||
192 1.21 mjl !(pw = getpwnam(username))) {
193 1.22 aidan warnx("YP unknown user %s", username);
194 1.22 aidan /* continuation */
195 1.22 aidan return(-1);
196 1.16 tv }
197 1.9 thorpej
198 1.9 thorpej if (uid && uid != pw->pw_uid)
199 1.9 thorpej errx(1, "you may only change your own password: %s",
200 1.9 thorpej strerror(EACCES));
201 1.1 brezak
202 1.4 deraadt /* prompt for new password */
203 1.4 deraadt yppasswd.newpw.pw_passwd = getnewpasswd(pw, &yppasswd.oldpass);
204 1.1 brezak
205 1.4 deraadt /* tell rpc.yppasswdd */
206 1.19 mycroft yppasswd.newpw.pw_name = strdup(pw->pw_name);
207 1.4 deraadt yppasswd.newpw.pw_uid = pw->pw_uid;
208 1.4 deraadt yppasswd.newpw.pw_gid = pw->pw_gid;
209 1.19 mycroft yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
210 1.19 mycroft yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
211 1.19 mycroft yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
212 1.16 tv
213 1.4 deraadt client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
214 1.17 mrg if (client == NULL) {
215 1.9 thorpej warnx("cannot contact yppasswdd on %s: Reason: %s",
216 1.5 deraadt master, yperr_string(YPERR_YPBIND));
217 1.17 mrg return (YPERR_YPBIND);
218 1.4 deraadt }
219 1.9 thorpej
220 1.4 deraadt client->cl_auth = authunix_create_default();
221 1.4 deraadt tv.tv_sec = 2;
222 1.4 deraadt tv.tv_usec = 0;
223 1.4 deraadt r = clnt_call(client, YPPASSWDPROC_UPDATE,
224 1.9 thorpej xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
225 1.4 deraadt if (r)
226 1.9 thorpej errx(1, "rpc to yppasswdd failed.");
227 1.4 deraadt else if (status)
228 1.4 deraadt printf("Couldn't change YP password.\n");
229 1.4 deraadt else
230 1.9 thorpej printf("The YP password has been changed on %s, %s\n",
231 1.9 thorpej master, "the master YP passwd server.");
232 1.22 aidan return(0);
233 1.1 brezak }
234 1.1 brezak
235 1.1 brezak static char *
236 1.1 brezak getnewpasswd(pw, old_pass)
237 1.11 tls struct passwd *pw;
238 1.4 deraadt char **old_pass;
239 1.1 brezak {
240 1.11 tls int tries;
241 1.11 tls char *p, *t;
242 1.4 deraadt static char buf[_PASSWORD_LEN+1];
243 1.23 ad char salt[_PASSWORD_LEN+1];
244 1.23 ad
245 1.2 brezak (void)printf("Changing YP password for %s.\n", pw->pw_name);
246 1.1 brezak
247 1.6 deraadt if (old_pass) {
248 1.4 deraadt *old_pass = NULL;
249 1.4 deraadt
250 1.8 phil if (pw->pw_passwd[0]) {
251 1.8 phil if (strcmp(crypt(p = getpass("Old password:"),
252 1.8 phil pw->pw_passwd), pw->pw_passwd)) {
253 1.18 mrg (void)printf("Sorry.\n");
254 1.18 mrg pw_error(NULL, 0, 1);
255 1.8 phil }
256 1.7 phil } else {
257 1.7 phil p = "";
258 1.4 deraadt }
259 1.7 phil
260 1.4 deraadt *old_pass = strdup(p);
261 1.4 deraadt }
262 1.1 brezak for (buf[0] = '\0', tries = 0;;) {
263 1.1 brezak p = getpass("New password:");
264 1.1 brezak if (!*p) {
265 1.1 brezak (void)printf("Password unchanged.\n");
266 1.1 brezak pw_error(NULL, 0, 0);
267 1.1 brezak }
268 1.4 deraadt if (strlen(p) <= 5 && ++tries < 2) {
269 1.1 brezak (void)printf("Please enter a longer password.\n");
270 1.1 brezak continue;
271 1.1 brezak }
272 1.1 brezak for (t = p; *t && islower(*t); ++t);
273 1.4 deraadt if (!*t && ++tries < 2) {
274 1.13 thorpej (void)printf("Please don't use an all-lower case "
275 1.13 thorpej "password.\nUnusual capitalization, "
276 1.13 thorpej "control characters or digits are "
277 1.13 thorpej "suggested.\n");
278 1.1 brezak continue;
279 1.1 brezak }
280 1.25 itojun (void)strlcpy(buf, p, sizeof(buf));
281 1.1 brezak if (!strcmp(buf, getpass("Retype new password:")))
282 1.1 brezak break;
283 1.1 brezak (void)printf("Mismatch; try again, EOF to quit.\n");
284 1.1 brezak }
285 1.23 ad
286 1.23 ad if(!pwd_gensalt(salt, _PASSWORD_LEN, pw, 'y' )) {
287 1.23 ad (void)printf("Couldn't generate salt.\n");
288 1.23 ad pw_error(NULL, 0, 0);
289 1.23 ad }
290 1.1 brezak return(strdup(crypt(buf, salt)));
291 1.1 brezak }
292 1.1 brezak
293 1.21 mjl static int
294 1.1 brezak ypgetpwnam(nam)
295 1.22 aidan const char *nam;
296 1.1 brezak {
297 1.4 deraadt char *val;
298 1.4 deraadt int reason, vallen;
299 1.4 deraadt
300 1.14 lukem val = NULL;
301 1.4 deraadt reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
302 1.4 deraadt &val, &vallen);
303 1.14 lukem if (reason != 0) {
304 1.14 lukem if (val != NULL)
305 1.14 lukem free(val);
306 1.21 mjl return 0;
307 1.4 deraadt }
308 1.4 deraadt free(val);
309 1.21 mjl return 1;
310 1.1 brezak }
311 1.1 brezak
312 1.1 brezak #endif /* YP */
313