Annotation of src/usr.bin/passwd/yp_passwd.c, Revision 1.2
1.2 ! deraadt 1: /* $OpenBSD$ */
! 2:
1.1 deraadt 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: #ifndef lint
36: /*static char sccsid[] = "from: @(#)yp_passwd.c 1.0 2/2/93";*/
1.2 ! deraadt 37: static char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.1.1.1 1995/10/18 08:45:54 deraadt Exp $";
1.1 deraadt 38: #endif /* not lint */
39:
40: #ifdef YP
41:
42: #include <stdio.h>
43: #include <string.h>
44: #include <netdb.h>
45: #include <time.h>
46: #include <pwd.h>
47: #include <errno.h>
48: #include <rpc/rpc.h>
49: #include <rpcsvc/yp_prot.h>
50: #include <rpcsvc/ypclnt.h>
51: #define passwd yp_passwd_rec
52: #include <rpcsvc/yppasswd.h>
53: #undef passwd
54:
55: #ifndef _PASSWORD_LEN
56: #define _PASSWORD_LEN PASS_MAX
57: #endif
58:
59: extern char *progname;
60: static char *getnewpasswd();
61: static struct passwd *ypgetpwnam();
62:
63: static uid_t uid;
64: char *domain;
65:
66: static
67: pw_error(name, err, eval)
68: char *name;
69: int err, eval;
70: {
71: int sverrno;
72:
73: if (err) {
74: sverrno = errno;
75: (void)fprintf(stderr, "%s: ", progname);
76: if (name)
77: (void)fprintf(stderr, "%s: ", name);
78: (void)fprintf(stderr, "%s\n", strerror(sverrno));
79: }
80: (void)fprintf(stderr,
81: "%s: YP passwd database unchanged\n", progname);
82: exit(eval);
83: }
84:
85: yp_passwd(username)
86: char *username;
87: {
88: char *master;
89: char *pp;
90: int r, rpcport, status;
91: struct yppasswd yppasswd;
92: struct passwd *pw;
93: struct timeval tv;
94: CLIENT *client;
95:
96: uid = getuid();
97:
98: /*
99: * Get local domain
100: */
101: if (r = yp_get_default_domain(&domain)) {
102: fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
103: progname, yperr_string(r));
104: exit(1);
105: }
106:
107: /*
108: * Find the host for the passwd map; it should be running
109: * the daemon.
110: */
111: if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
112: fprintf(stderr,
113: "%s: can't find the master YP server. Reason: %s\n",
114: progname, yperr_string(r));
115: exit(1);
116: }
117:
118: /*
119: * Ask the portmapper for the port of the daemon.
120: */
121: if ((rpcport = getrpcport(master, YPPASSWDPROG,
122: YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
123: fprintf(stderr,
124: "%s: master YP server not running yppasswd daemon.\n",
125: progname);
126: fprintf(stderr, "\tCan't change password.\n");
127: exit(1);
128: }
129:
130: /*
131: * Be sure the port is priviledged
132: */
133: if (rpcport >= IPPORT_RESERVED) {
134: fprintf(stderr, "%s: yppasswd daemon is on an invalid port.\n",
135: progname);
136: exit(1);
137: }
138:
139: /* Get user's login identity */
140: if (!(pw = ypgetpwnam(username))) {
141: (void)fprintf(stderr, "%s: unknown user %s.\n",
142: progname, username);
143: exit(1);
144: }
145:
146: if (uid && uid != pw->pw_uid) {
147: fprintf(stderr,
148: "%s: you may only change your own password: %s\n",
149: progname, strerror(EACCES));
150: exit(1);
151: }
152:
153: /* prompt for new password */
154: yppasswd.newpw.pw_passwd = getnewpasswd(pw, &yppasswd.oldpass);
155:
156: /* tell rpc.yppasswdd */
157: yppasswd.newpw.pw_name = pw->pw_name;
158: yppasswd.newpw.pw_uid = pw->pw_uid;
159: yppasswd.newpw.pw_gid = pw->pw_gid;
160: yppasswd.newpw.pw_gecos = pw->pw_gecos;
161: yppasswd.newpw.pw_dir = pw->pw_dir;
162: yppasswd.newpw.pw_shell = pw->pw_shell;
163:
164: client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
165: if (client==NULL) {
166: fprintf(stderr, "cannot contact yppasswdd on %s: Reason: %s\n",
167: master, yperr_string(YPERR_YPBIND));
168: return(YPERR_YPBIND);
169: }
170: client->cl_auth = authunix_create_default();
171: tv.tv_sec = 2;
172: tv.tv_usec = 0;
173: r = clnt_call(client, YPPASSWDPROC_UPDATE,
174: xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
175: if (r)
176: fprintf(stderr, "%s: rpc to yppasswdd failed.\n", progname);
177: else if (status)
178: printf("Couldn't change YP password.\n");
179: else
180: printf("The YP password has been changed on %s, the master YP passwd server.\n", master);
181: exit(0);
182: }
183:
184: static char *
185: getnewpasswd(pw, old_pass)
186: register struct passwd *pw;
187: char **old_pass;
188: {
189: static char buf[_PASSWORD_LEN+1];
190: register char *p, *t;
191: int tries;
192: char salt[9], *crypt(), *getpass();
193:
194: (void)printf("Changing YP password for %s.\n", pw->pw_name);
195:
196: if (old_pass) {
197: *old_pass = NULL;
198:
199: if (pw->pw_passwd[0]) {
200: if (strcmp(crypt(p = getpass("Old password:"),
201: pw->pw_passwd), pw->pw_passwd)) {
202: errno = EACCES;
203: pw_error(NULL, 1, 1);
204: }
205: } else {
206: p = "";
207: }
208:
209: *old_pass = strdup(p);
210: }
211: for (buf[0] = '\0', tries = 0;;) {
212: p = getpass("New password:");
213: if (!*p) {
214: (void)printf("Password unchanged.\n");
215: pw_error(NULL, 0, 0);
216: }
217: if (strlen(p) <= 5 && ++tries < 2) {
218: (void)printf("Please enter a longer password.\n");
219: continue;
220: }
221: for (t = p; *t && islower(*t); ++t);
222: if (!*t && ++tries < 2) {
223: (void)printf("Please don't use an all-lower case password.\nUnusual capitalization, control characters or digits are suggested.\n");
224: continue;
225: }
226: (void)strcpy(buf, p);
227: if (!strcmp(buf, getpass("Retype new password:")))
228: break;
229: (void)printf("Mismatch; try again, EOF to quit.\n");
230: }
231: /* grab a random printable character that isn't a colon */
232: (void)srandom((int)time((time_t *)NULL));
233: #ifdef NEWSALT
234: salt[0] = _PASSWORD_EFMT1;
235: to64(&salt[1], (long)(29 * 25), 4);
236: to64(&salt[5], random(), 4);
237: #else
238: to64(&salt[0], random(), 2);
239: #endif
240: return(strdup(crypt(buf, salt)));
241: }
242:
243: static char *
244: pwskip(register char *p)
245: {
246: while (*p && *p != ':' && *p != '\n')
247: ++p;
248: if (*p)
249: *p++ = 0;
250: return (p);
251: }
252:
253: struct passwd *
254: interpret(struct passwd *pwent, char *line)
255: {
256: register char *p = line;
257: register int c;
258:
259: pwent->pw_passwd = "*";
260: pwent->pw_uid = 0;
261: pwent->pw_gid = 0;
262: pwent->pw_gecos = "";
263: pwent->pw_dir = "";
264: pwent->pw_shell = "";
265: pwent->pw_change = 0;
266: pwent->pw_expire = 0;
267: pwent->pw_class = "";
268:
269: /* line without colon separators is no good, so ignore it */
270: if(!strchr(p,':'))
271: return(NULL);
272:
273: pwent->pw_name = p;
274: p = pwskip(p);
275: pwent->pw_passwd = p;
276: p = pwskip(p);
277: pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
278: p = pwskip(p);
279: pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
280: p = pwskip(p);
281: pwent->pw_gecos = p;
282: p = pwskip(p);
283: pwent->pw_dir = p;
284: p = pwskip(p);
285: pwent->pw_shell = p;
286: while (*p && *p != '\n')
287: p++;
288: *p = '\0';
289: return (pwent);
290: }
291:
292: static struct passwd *
293: ypgetpwnam(nam)
294: char *nam;
295: {
296: static struct passwd pwent;
297: static char line[1024];
298: char *val;
299: int reason, vallen;
300:
301: reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
302: &val, &vallen);
303: switch(reason) {
304: case 0:
305: break;
306: default:
307: return (NULL);
308: break;
309: }
310: val[vallen] = '\0';
311: strcpy(line, val);
312: free(val);
313:
314: return(interpret(&pwent, line));
315: }
316:
317: #endif /* YP */