pw_yp.c revision 1.12 1 /* $NetBSD: pw_yp.c,v 1.12 1997/10/18 12:49:05 lukem 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 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)pw_yp.c 1.0 2/2/93";
39 #else
40 __RCSID("$NetBSD: pw_yp.c,v 1.12 1997/10/18 12:49:05 lukem Exp $");
41 #endif
42 #endif /* not lint */
43
44 #ifdef YP
45
46 #include <err.h>
47 #include <errno.h>
48 #include <netdb.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54 #include <unistd.h>
55
56 #include <rpc/rpc.h>
57 #include <rpcsvc/yp_prot.h>
58 #include <rpcsvc/ypclnt.h>
59
60 #define passwd yp_passwd_rec
61 #include <rpcsvc/yppasswd.h>
62 #undef passwd
63
64 #include "chpass.h"
65
66 static char *domain;
67
68 static struct passwd *interpret __P((struct passwd *, char *));
69 static char *pwskip __P((char *));
70
71 /*
72 * Check if rpc.yppasswdd is running on the master YP server.
73 * XXX this duplicates some code, but is much less complex
74 * than the alternative.
75 */
76 int
77 check_yppasswdd()
78 {
79 char *master;
80 int rpcport;
81
82 /*
83 * Get local domain
84 */
85 if (!domain && yp_get_default_domain(&domain) != 0)
86 return (1);
87
88 /*
89 * Find the host for the passwd map; it should be running
90 * the daemon.
91 */
92 master = NULL;
93 if (yp_master(domain, "passwd.byname", &master) != 0) {
94 if (master != NULL)
95 free (master);
96 return (1);
97 }
98
99 /*
100 * Ask the portmapper for the port of the daemon.
101 */
102 if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
103 IPPROTO_UDP)) == 0)
104 return (1);
105
106 /*
107 * Successful contact with rpc.yppasswdd.
108 */
109 return (0);
110 }
111
112 int
113 pw_yp(pw, uid)
114 struct passwd *pw;
115 uid_t uid;
116 {
117 char *master;
118 int r, rpcport, status;
119 struct yppasswd yppasswd;
120 struct timeval tv;
121 CLIENT *client;
122
123 /*
124 * Get local domain
125 */
126 if (!domain && (r = yp_get_default_domain(&domain)))
127 errx(1, "can't get local YP domain. Reason: %s",
128 yperr_string(r));
129
130 /*
131 * Find the host for the passwd map; it should be running
132 * the daemon.
133 */
134 master = NULL;
135 if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
136 if (master)
137 free (master);
138 warnx("can't find the master YP server. Reason: %s",
139 yperr_string(r));
140 return (1);
141 }
142
143 /*
144 * Ask the portmapper for the port of the daemon.
145 */
146 if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
147 IPPROTO_UDP)) == 0) {
148 warnx("master YP server not running yppasswd daemon.\n\t%s\n",
149 "Can't change password.");
150 return (1);
151 }
152
153 /*
154 * Be sure the port is priviledged
155 */
156 if (rpcport >= IPPORT_RESERVED) {
157 warnx("yppasswd daemon is on an invalid port.");
158 return (1);
159 }
160
161 /* prompt for old password */
162 memset(&yppasswd, 0, sizeof yppasswd);
163 yppasswd.oldpass = "none";
164 yppasswd.oldpass = getpass("Old password:");
165 if (!yppasswd.oldpass) {
166 warnx("Cancelled.");
167 return (1);
168 }
169
170 /* tell rpc.yppasswdd */
171 yppasswd.newpw.pw_name = pw->pw_name;
172 yppasswd.newpw.pw_passwd= pw->pw_passwd;
173 yppasswd.newpw.pw_uid = pw->pw_uid;
174 yppasswd.newpw.pw_gid = pw->pw_gid;
175 yppasswd.newpw.pw_gecos = pw->pw_gecos;
176 yppasswd.newpw.pw_dir = pw->pw_dir;
177 yppasswd.newpw.pw_shell = pw->pw_shell;
178
179 client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
180 if (client==NULL) {
181 warnx("cannot contact yppasswdd on %s: Reason: %s",
182 master, yperr_string(YPERR_YPBIND));
183 return (1);
184 }
185 client->cl_auth = authunix_create_default();
186 tv.tv_sec = 5;
187 tv.tv_usec = 0;
188 r = clnt_call(client, YPPASSWDPROC_UPDATE,
189 xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
190 if (r) {
191 warnx("rpc to yppasswdd failed.");
192 return (1);
193 } else if (status)
194 printf("Couldn't change YP password.\n");
195 else
196 printf("%s %s, %s\n",
197 "The YP password information has been changed on",
198 master, "the master YP passwd server.");
199 return (0);
200 }
201
202 static char *
203 pwskip(p)
204 char *p;
205 {
206 while (*p && *p != ':' && *p != '\n')
207 ++p;
208 if (*p)
209 *p++ = 0;
210 return (p);
211 }
212
213 static struct passwd *
214 interpret(pwent, line)
215 struct passwd *pwent;
216 char *line;
217 {
218 char *p = line;
219
220 pwent->pw_passwd = "*";
221 pwent->pw_uid = 0;
222 pwent->pw_gid = 0;
223 pwent->pw_gecos = "";
224 pwent->pw_dir = "";
225 pwent->pw_shell = "";
226 pwent->pw_change = 0;
227 pwent->pw_expire = 0;
228 pwent->pw_class = "";
229
230 /* line without colon separators is no good, so ignore it */
231 if(!strchr(p,':'))
232 return(NULL);
233
234 pwent->pw_name = p;
235 p = pwskip(p);
236 pwent->pw_passwd = p;
237 p = pwskip(p);
238 pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
239 p = pwskip(p);
240 pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
241 p = pwskip(p);
242 pwent->pw_gecos = p;
243 p = pwskip(p);
244 pwent->pw_dir = p;
245 p = pwskip(p);
246 pwent->pw_shell = p;
247 while (*p && *p != '\n')
248 p++;
249 *p = '\0';
250 return (pwent);
251 }
252
253 struct passwd *
254 ypgetpwnam(nam)
255 const char *nam;
256 {
257 static struct passwd pwent;
258 static char line[1024];
259 char *val;
260 int reason, vallen;
261
262 /*
263 * Get local domain
264 */
265 if (!domain && (reason = yp_get_default_domain(&domain)))
266 errx(1, "can't get local YP domain. Reason: %s",
267 yperr_string(reason));
268
269 val = NULL;
270 reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
271 &val, &vallen);
272 if (reason != 0) {
273 if (val)
274 free (val);
275 return (NULL);
276 }
277 val[vallen] = '\0';
278 (void)strncpy(line, val, sizeof(line) - 1);
279 free(val);
280
281 return(interpret(&pwent, line));
282 }
283
284 struct passwd *
285 ypgetpwuid(uid)
286 uid_t uid;
287 {
288 static struct passwd pwent;
289 static char line[1024];
290 char *val;
291 int reason, vallen;
292 char namebuf[16];
293
294 if (!domain && (reason = yp_get_default_domain(&domain)))
295 errx(1, "can't get local YP domain. Reason: %s\n",
296 yperr_string(reason));
297
298 (void)snprintf(namebuf, sizeof namebuf, "%d", uid);
299 val = NULL;
300 reason = yp_match(domain, "passwd.byuid", namebuf, strlen(namebuf),
301 &val, &vallen);
302 if (reason != 0) {
303 if (val)
304 free (val);
305 return (NULL);
306 }
307 val[vallen] = '\0';
308 (void)strncpy(line, val, sizeof(line) - 1);
309 free(val);
310
311 return(interpret(&pwent, line));
312 }
313
314 void
315 yppw_error(name, err, eval)
316 const char *name;
317 int err, eval;
318 {
319
320 if (err)
321 warn(name);
322
323 errx(eval, "YP passwd information unchanged");
324 }
325
326 void
327 yppw_prompt()
328 {
329 int c;
330
331 (void)printf("re-edit the password file? [y]: ");
332 (void)fflush(stdout);
333 c = getchar();
334 if (c != EOF && c != '\n')
335 while (getchar() != '\n');
336 if (c == 'n')
337 yppw_error(NULL, 0, 0);
338 }
339 #endif /* YP */
340