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