pw_yp.c revision 1.3 1 1.1 brezak /*
2 1.1 brezak * Copyright (c) 1988 The Regents of the University of California.
3 1.1 brezak * All rights reserved.
4 1.1 brezak *
5 1.1 brezak * Redistribution and use in source and binary forms, with or without
6 1.1 brezak * modification, are permitted provided that the following conditions
7 1.1 brezak * are met:
8 1.1 brezak * 1. Redistributions of source code must retain the above copyright
9 1.1 brezak * notice, this list of conditions and the following disclaimer.
10 1.1 brezak * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 brezak * notice, this list of conditions and the following disclaimer in the
12 1.1 brezak * documentation and/or other materials provided with the distribution.
13 1.1 brezak * 3. All advertising materials mentioning features or use of this software
14 1.1 brezak * must display the following acknowledgement:
15 1.1 brezak * This product includes software developed by the University of
16 1.1 brezak * California, Berkeley and its contributors.
17 1.1 brezak * 4. Neither the name of the University nor the names of its contributors
18 1.1 brezak * may be used to endorse or promote products derived from this software
19 1.1 brezak * without specific prior written permission.
20 1.1 brezak *
21 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 brezak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 brezak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 brezak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 brezak * SUCH DAMAGE.
32 1.1 brezak */
33 1.1 brezak #ifndef lint
34 1.2 mycroft /*static char sccsid[] = "from: @(#)pw_yp.c 1.0 2/2/93";*/
35 1.3 deraadt static char rcsid[] = "$Id: pw_yp.c,v 1.3 1994/06/29 21:40:21 deraadt Exp $";
36 1.1 brezak #endif /* not lint */
37 1.1 brezak
38 1.1 brezak #ifdef YP
39 1.1 brezak
40 1.1 brezak #include <stdio.h>
41 1.1 brezak #include <string.h>
42 1.1 brezak #include <netdb.h>
43 1.1 brezak #include <time.h>
44 1.1 brezak #include <pwd.h>
45 1.1 brezak #include <errno.h>
46 1.1 brezak #include <rpc/rpc.h>
47 1.1 brezak #include <rpcsvc/yp_prot.h>
48 1.1 brezak #include <rpcsvc/ypclnt.h>
49 1.1 brezak #define passwd yp_passwd_rec
50 1.1 brezak #include <rpcsvc/yppasswd.h>
51 1.1 brezak #undef passwd
52 1.3 deraadt
53 1.1 brezak extern char *progname;
54 1.1 brezak
55 1.1 brezak static char *domain;
56 1.1 brezak
57 1.1 brezak pw_yp(pw, uid)
58 1.3 deraadt struct passwd *pw;
59 1.3 deraadt uid_t uid;
60 1.1 brezak {
61 1.3 deraadt char *master;
62 1.3 deraadt char *pp;
63 1.3 deraadt int r, rpcport, status;
64 1.3 deraadt struct yppasswd yppasswd;
65 1.1 brezak struct timeval tv;
66 1.1 brezak CLIENT *client;
67 1.3 deraadt extern char *getpass();
68 1.3 deraadt
69 1.3 deraadt /*
70 1.3 deraadt * Get local domain
71 1.3 deraadt */
72 1.3 deraadt if (!domain && (r = yp_get_default_domain(&domain))) {
73 1.3 deraadt fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
74 1.3 deraadt progname, yperr_string(r));
75 1.3 deraadt return(0);
76 1.3 deraadt }
77 1.3 deraadt
78 1.3 deraadt /*
79 1.3 deraadt * Find the host for the passwd map; it should be running
80 1.3 deraadt * the daemon.
81 1.3 deraadt */
82 1.3 deraadt if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
83 1.3 deraadt fprintf(stderr,
84 1.3 deraadt "%s: can't find the master YP server. Reason: %s\n",
85 1.3 deraadt progname, yperr_string(r));
86 1.3 deraadt return(0);
87 1.3 deraadt }
88 1.3 deraadt
89 1.3 deraadt /*
90 1.3 deraadt * Ask the portmapper for the port of the daemon.
91 1.3 deraadt */
92 1.3 deraadt if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
93 1.3 deraadt IPPROTO_UDP)) == 0) {
94 1.3 deraadt fprintf(stderr,
95 1.3 deraadt "%s: master YP server not running yppasswd daemon.\n",
96 1.3 deraadt progname);
97 1.3 deraadt fprintf(stderr, "\tCan't change password.\n");
98 1.3 deraadt return(0);
99 1.3 deraadt }
100 1.3 deraadt
101 1.3 deraadt /*
102 1.3 deraadt * Be sure the port is priviledged
103 1.3 deraadt */
104 1.3 deraadt if (rpcport >= IPPORT_RESERVED) {
105 1.3 deraadt (void)fprintf(stderr,
106 1.3 deraadt "%s: yppasswd daemon running on an invalid port.\n",
107 1.3 deraadt progname);
108 1.3 deraadt return(0);
109 1.3 deraadt }
110 1.3 deraadt
111 1.3 deraadt /* prompt for old password */
112 1.3 deraadt bzero(&yppasswd, sizeof yppasswd);
113 1.3 deraadt yppasswd.oldpass = "none";
114 1.3 deraadt if (uid)
115 1.3 deraadt yppasswd.oldpass = getpass("Old password:");
116 1.3 deraadt if (!yppasswd.oldpass) {
117 1.3 deraadt (void)fprintf(stderr, "Cancelled.\n");
118 1.3 deraadt return(0);
119 1.3 deraadt }
120 1.3 deraadt
121 1.3 deraadt /* tell rpc.yppasswdd */
122 1.3 deraadt yppasswd.newpw.pw_name = pw->pw_name;
123 1.3 deraadt yppasswd.newpw.pw_passwd= pw->pw_passwd;
124 1.3 deraadt yppasswd.newpw.pw_uid = pw->pw_uid;
125 1.3 deraadt yppasswd.newpw.pw_gid = pw->pw_gid;
126 1.3 deraadt yppasswd.newpw.pw_gecos = pw->pw_gecos;
127 1.3 deraadt yppasswd.newpw.pw_dir = pw->pw_dir;
128 1.3 deraadt yppasswd.newpw.pw_shell = pw->pw_shell;
129 1.3 deraadt
130 1.3 deraadt client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
131 1.3 deraadt if (client==NULL) {
132 1.3 deraadt fprintf(stderr, "can't contact yppasswdd on %s: Reason: %s\n",
133 1.3 deraadt master, yperr_string(YPERR_YPBIND));
134 1.3 deraadt return(0);
135 1.3 deraadt }
136 1.3 deraadt client->cl_auth = authunix_create_default();
137 1.3 deraadt tv.tv_sec = 5;
138 1.3 deraadt tv.tv_usec = 0;
139 1.3 deraadt r = clnt_call(client, YPPASSWDPROC_UPDATE,
140 1.3 deraadt xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
141 1.3 deraadt if (r) {
142 1.3 deraadt fprintf(stderr, "%s: rpc to yppasswdd failed. %d\n", progname, r);
143 1.3 deraadt return(0);
144 1.3 deraadt } else if (status) {
145 1.3 deraadt printf("Couldn't change YP password information.\n");
146 1.3 deraadt return(0);
147 1.3 deraadt }
148 1.3 deraadt printf("The YP password information has been changed on %s, the master YP passwd server.\n", master);
149 1.1 brezak
150 1.3 deraadt return(1);
151 1.1 brezak }
152 1.1 brezak
153 1.1 brezak static char *
154 1.3 deraadt pwskip(p)
155 1.3 deraadt register char *p;
156 1.1 brezak {
157 1.1 brezak while (*p && *p != ':' && *p != '\n')
158 1.1 brezak ++p;
159 1.1 brezak if (*p)
160 1.1 brezak *p++ = 0;
161 1.1 brezak return (p);
162 1.1 brezak }
163 1.1 brezak
164 1.1 brezak static struct passwd *
165 1.3 deraadt interpret(pwent, line)
166 1.3 deraadt struct passwd *pwent;
167 1.3 deraadt char *line;
168 1.1 brezak {
169 1.1 brezak register char *p = line;
170 1.1 brezak register int c;
171 1.1 brezak
172 1.3 deraadt pwent->pw_passwd = "*";
173 1.3 deraadt pwent->pw_uid = 0;
174 1.3 deraadt pwent->pw_gid = 0;
175 1.3 deraadt pwent->pw_gecos = "";
176 1.3 deraadt pwent->pw_dir = "";
177 1.3 deraadt pwent->pw_shell = "";
178 1.1 brezak pwent->pw_change = 0;
179 1.1 brezak pwent->pw_expire = 0;
180 1.1 brezak pwent->pw_class = "";
181 1.3 deraadt
182 1.3 deraadt /* line without colon separators is no good, so ignore it */
183 1.3 deraadt if(!strchr(p,':'))
184 1.3 deraadt return(NULL);
185 1.1 brezak
186 1.1 brezak pwent->pw_name = p;
187 1.1 brezak p = pwskip(p);
188 1.1 brezak pwent->pw_passwd = p;
189 1.1 brezak p = pwskip(p);
190 1.1 brezak pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
191 1.1 brezak p = pwskip(p);
192 1.1 brezak pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
193 1.1 brezak p = pwskip(p);
194 1.1 brezak pwent->pw_gecos = p;
195 1.1 brezak p = pwskip(p);
196 1.1 brezak pwent->pw_dir = p;
197 1.1 brezak p = pwskip(p);
198 1.1 brezak pwent->pw_shell = p;
199 1.1 brezak while (*p && *p != '\n')
200 1.1 brezak p++;
201 1.1 brezak *p = '\0';
202 1.1 brezak return (pwent);
203 1.1 brezak }
204 1.1 brezak
205 1.1 brezak struct passwd *
206 1.1 brezak ypgetpwnam(nam)
207 1.3 deraadt char *nam;
208 1.1 brezak {
209 1.3 deraadt static struct passwd pwent;
210 1.3 deraadt static char line[1024];
211 1.3 deraadt char *val;
212 1.3 deraadt int reason, vallen;
213 1.3 deraadt
214 1.3 deraadt /*
215 1.3 deraadt * Get local domain
216 1.3 deraadt */
217 1.3 deraadt if (!domain && (reason = yp_get_default_domain(&domain))) {
218 1.3 deraadt fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
219 1.3 deraadt progname, yperr_string(reason));
220 1.3 deraadt exit(1);
221 1.3 deraadt }
222 1.3 deraadt
223 1.3 deraadt reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
224 1.3 deraadt &val, &vallen);
225 1.3 deraadt switch(reason) {
226 1.3 deraadt case 0:
227 1.3 deraadt break;
228 1.3 deraadt default:
229 1.3 deraadt return (NULL);
230 1.3 deraadt break;
231 1.3 deraadt }
232 1.3 deraadt val[vallen] = '\0';
233 1.3 deraadt strcpy(line, val);
234 1.3 deraadt free(val);
235 1.1 brezak
236 1.3 deraadt return(interpret(&pwent, line));
237 1.1 brezak }
238 1.1 brezak
239 1.1 brezak struct passwd *
240 1.1 brezak ypgetpwuid(uid)
241 1.3 deraadt uid_t uid;
242 1.1 brezak {
243 1.3 deraadt static struct passwd pwent;
244 1.3 deraadt static char line[1024];
245 1.3 deraadt char *val;
246 1.3 deraadt int reason, vallen;
247 1.3 deraadt char namebuf[16];
248 1.3 deraadt
249 1.3 deraadt if (!domain && (reason = yp_get_default_domain(&domain))) {
250 1.3 deraadt fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
251 1.3 deraadt progname, yperr_string(reason));
252 1.3 deraadt exit(1);
253 1.3 deraadt }
254 1.3 deraadt
255 1.3 deraadt sprintf(namebuf, "%d", uid);
256 1.3 deraadt reason = yp_match(domain, "passwd.byuid", namebuf, strlen(namebuf),
257 1.3 deraadt &val, &vallen);
258 1.3 deraadt switch(reason) {
259 1.3 deraadt case 0:
260 1.3 deraadt break;
261 1.3 deraadt default:
262 1.3 deraadt return (NULL);
263 1.3 deraadt break;
264 1.3 deraadt }
265 1.3 deraadt val[vallen] = '\0';
266 1.3 deraadt strcpy(line, val);
267 1.3 deraadt free(val);
268 1.1 brezak
269 1.3 deraadt return(interpret(&pwent, line));
270 1.1 brezak }
271 1.1 brezak
272 1.1 brezak #endif /* YP */
273