pw_yp.c revision 1.20 1 1.20 agc /* $NetBSD: pw_yp.c,v 1.20 2003/08/07 11:13:19 agc Exp $ */
2 1.5 glass
3 1.1 brezak /*
4 1.1 brezak * Copyright (c) 1988 The Regents of the University of California.
5 1.1 brezak * 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.20 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 brezak * may be used to endorse or promote products derived from this software
17 1.1 brezak * without specific prior written permission.
18 1.1 brezak *
19 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 brezak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 brezak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 brezak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 brezak * SUCH DAMAGE.
30 1.1 brezak */
31 1.12 lukem #include <sys/cdefs.h>
32 1.1 brezak #ifndef lint
33 1.5 glass #if 0
34 1.5 glass static char sccsid[] = "@(#)pw_yp.c 1.0 2/2/93";
35 1.5 glass #else
36 1.20 agc __RCSID("$NetBSD: pw_yp.c,v 1.20 2003/08/07 11:13:19 agc Exp $");
37 1.5 glass #endif
38 1.1 brezak #endif /* not lint */
39 1.1 brezak
40 1.1 brezak #ifdef YP
41 1.1 brezak
42 1.11 mikel #include <err.h>
43 1.12 lukem #include <errno.h>
44 1.12 lukem #include <netdb.h>
45 1.12 lukem #include <pwd.h>
46 1.1 brezak #include <stdio.h>
47 1.11 mikel #include <stdlib.h>
48 1.1 brezak #include <string.h>
49 1.1 brezak #include <time.h>
50 1.12 lukem #include <unistd.h>
51 1.12 lukem
52 1.1 brezak #include <rpc/rpc.h>
53 1.1 brezak #include <rpcsvc/yp_prot.h>
54 1.1 brezak #include <rpcsvc/ypclnt.h>
55 1.12 lukem
56 1.1 brezak #define passwd yp_passwd_rec
57 1.1 brezak #include <rpcsvc/yppasswd.h>
58 1.1 brezak #undef passwd
59 1.3 deraadt
60 1.12 lukem #include "chpass.h"
61 1.12 lukem
62 1.1 brezak static char *domain;
63 1.7 thorpej
64 1.7 thorpej /*
65 1.7 thorpej * Check if rpc.yppasswdd is running on the master YP server.
66 1.7 thorpej * XXX this duplicates some code, but is much less complex
67 1.7 thorpej * than the alternative.
68 1.7 thorpej */
69 1.7 thorpej int
70 1.7 thorpej check_yppasswdd()
71 1.7 thorpej {
72 1.7 thorpej char *master;
73 1.7 thorpej int rpcport;
74 1.7 thorpej
75 1.7 thorpej /*
76 1.7 thorpej * Get local domain
77 1.7 thorpej */
78 1.7 thorpej if (!domain && yp_get_default_domain(&domain) != 0)
79 1.7 thorpej return (1);
80 1.7 thorpej
81 1.7 thorpej /*
82 1.7 thorpej * Find the host for the passwd map; it should be running
83 1.7 thorpej * the daemon.
84 1.7 thorpej */
85 1.9 lukem master = NULL;
86 1.9 lukem if (yp_master(domain, "passwd.byname", &master) != 0) {
87 1.9 lukem if (master != NULL)
88 1.9 lukem free (master);
89 1.7 thorpej return (1);
90 1.9 lukem }
91 1.7 thorpej
92 1.7 thorpej /*
93 1.7 thorpej * Ask the portmapper for the port of the daemon.
94 1.7 thorpej */
95 1.7 thorpej if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
96 1.7 thorpej IPPROTO_UDP)) == 0)
97 1.7 thorpej return (1);
98 1.7 thorpej
99 1.7 thorpej /*
100 1.7 thorpej * Successful contact with rpc.yppasswdd.
101 1.7 thorpej */
102 1.7 thorpej return (0);
103 1.7 thorpej }
104 1.1 brezak
105 1.11 mikel int
106 1.1 brezak pw_yp(pw, uid)
107 1.3 deraadt struct passwd *pw;
108 1.3 deraadt uid_t uid;
109 1.1 brezak {
110 1.3 deraadt char *master;
111 1.3 deraadt int r, rpcport, status;
112 1.3 deraadt struct yppasswd yppasswd;
113 1.1 brezak struct timeval tv;
114 1.1 brezak CLIENT *client;
115 1.3 deraadt
116 1.3 deraadt /*
117 1.3 deraadt * Get local domain
118 1.3 deraadt */
119 1.6 thorpej if (!domain && (r = yp_get_default_domain(&domain)))
120 1.6 thorpej errx(1, "can't get local YP domain. Reason: %s",
121 1.6 thorpej yperr_string(r));
122 1.3 deraadt
123 1.3 deraadt /*
124 1.3 deraadt * Find the host for the passwd map; it should be running
125 1.3 deraadt * the daemon.
126 1.3 deraadt */
127 1.9 lukem master = NULL;
128 1.3 deraadt if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
129 1.9 lukem if (master)
130 1.9 lukem free (master);
131 1.6 thorpej warnx("can't find the master YP server. Reason: %s",
132 1.6 thorpej yperr_string(r));
133 1.6 thorpej return (1);
134 1.3 deraadt }
135 1.3 deraadt
136 1.3 deraadt /*
137 1.3 deraadt * Ask the portmapper for the port of the daemon.
138 1.3 deraadt */
139 1.3 deraadt if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
140 1.3 deraadt IPPROTO_UDP)) == 0) {
141 1.6 thorpej warnx("master YP server not running yppasswd daemon.\n\t%s\n",
142 1.6 thorpej "Can't change password.");
143 1.6 thorpej return (1);
144 1.3 deraadt }
145 1.3 deraadt
146 1.3 deraadt /*
147 1.15 simonb * Be sure the port is privileged
148 1.3 deraadt */
149 1.3 deraadt if (rpcport >= IPPORT_RESERVED) {
150 1.6 thorpej warnx("yppasswd daemon is on an invalid port.");
151 1.6 thorpej return (1);
152 1.3 deraadt }
153 1.3 deraadt
154 1.3 deraadt /* prompt for old password */
155 1.12 lukem memset(&yppasswd, 0, sizeof yppasswd);
156 1.3 deraadt yppasswd.oldpass = "none";
157 1.4 deraadt yppasswd.oldpass = getpass("Old password:");
158 1.3 deraadt if (!yppasswd.oldpass) {
159 1.6 thorpej warnx("Cancelled.");
160 1.6 thorpej return (1);
161 1.3 deraadt }
162 1.6 thorpej
163 1.3 deraadt /* tell rpc.yppasswdd */
164 1.14 mycroft yppasswd.newpw.pw_name = strdup(pw->pw_name);
165 1.19 itojun if (!yppasswd.newpw.pw_name) {
166 1.19 itojun err(1, "strdup");
167 1.19 itojun /*NOTREACHED*/
168 1.19 itojun }
169 1.14 mycroft yppasswd.newpw.pw_passwd = strdup(pw->pw_passwd);
170 1.19 itojun if (!yppasswd.newpw.pw_passwd) {
171 1.19 itojun err(1, "strdup");
172 1.19 itojun /*NOTREACHED*/
173 1.19 itojun }
174 1.14 mycroft yppasswd.newpw.pw_uid = pw->pw_uid;
175 1.14 mycroft yppasswd.newpw.pw_gid = pw->pw_gid;
176 1.14 mycroft yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
177 1.19 itojun if (!yppasswd.newpw.pw_gecos) {
178 1.19 itojun err(1, "strdup");
179 1.19 itojun /*NOTREACHED*/
180 1.19 itojun }
181 1.14 mycroft yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
182 1.19 itojun if (!yppasswd.newpw.pw_dir) {
183 1.19 itojun err(1, "strdup");
184 1.19 itojun /*NOTREACHED*/
185 1.19 itojun }
186 1.14 mycroft yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
187 1.19 itojun if (!yppasswd.newpw.pw_shell) {
188 1.19 itojun err(1, "strdup");
189 1.19 itojun /*NOTREACHED*/
190 1.19 itojun }
191 1.3 deraadt
192 1.3 deraadt client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
193 1.13 mrg if (client == NULL) {
194 1.6 thorpej warnx("cannot contact yppasswdd on %s: Reason: %s",
195 1.3 deraadt master, yperr_string(YPERR_YPBIND));
196 1.6 thorpej return (1);
197 1.3 deraadt }
198 1.3 deraadt client->cl_auth = authunix_create_default();
199 1.3 deraadt tv.tv_sec = 5;
200 1.3 deraadt tv.tv_usec = 0;
201 1.3 deraadt r = clnt_call(client, YPPASSWDPROC_UPDATE,
202 1.3 deraadt xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
203 1.3 deraadt if (r) {
204 1.6 thorpej warnx("rpc to yppasswdd failed.");
205 1.6 thorpej return (1);
206 1.6 thorpej } else if (status)
207 1.6 thorpej printf("Couldn't change YP password.\n");
208 1.6 thorpej else
209 1.6 thorpej printf("%s %s, %s\n",
210 1.6 thorpej "The YP password information has been changed on",
211 1.6 thorpej master, "the master YP passwd server.");
212 1.6 thorpej return (0);
213 1.1 brezak }
214 1.1 brezak
215 1.6 thorpej void
216 1.6 thorpej yppw_error(name, err, eval)
217 1.6 thorpej const char *name;
218 1.6 thorpej int err, eval;
219 1.6 thorpej {
220 1.6 thorpej
221 1.16 itojun if (err) {
222 1.16 itojun if (name)
223 1.16 itojun warn("%s", name);
224 1.16 itojun else
225 1.17 is warn(NULL);
226 1.16 itojun }
227 1.6 thorpej
228 1.6 thorpej errx(eval, "YP passwd information unchanged");
229 1.6 thorpej }
230 1.6 thorpej
231 1.6 thorpej void
232 1.6 thorpej yppw_prompt()
233 1.6 thorpej {
234 1.6 thorpej int c;
235 1.6 thorpej
236 1.6 thorpej (void)printf("re-edit the password file? [y]: ");
237 1.6 thorpej (void)fflush(stdout);
238 1.6 thorpej c = getchar();
239 1.6 thorpej if (c != EOF && c != '\n')
240 1.6 thorpej while (getchar() != '\n');
241 1.6 thorpej if (c == 'n')
242 1.6 thorpej yppw_error(NULL, 0, 0);
243 1.6 thorpej }
244 1.1 brezak #endif /* YP */
245