Annotation of src/usr.bin/passwd/yp_passwd.c, Revision 1.7
1.7 ! provos 1: /* $OpenBSD: yp_passwd.c,v 1.6 1997/02/16 20:09:00 provos Exp $ */
1.2 deraadt 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.7 ! provos 37: static char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.6 1997/02/16 20:09:00 provos 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));
1.7 ! provos 168: free(yppasswd.newpw.pw_passwd);
1.1 deraadt 169: return(YPERR_YPBIND);
170: }
171: client->cl_auth = authunix_create_default();
172: tv.tv_sec = 2;
173: tv.tv_usec = 0;
174: r = clnt_call(client, YPPASSWDPROC_UPDATE,
1.3 deraadt 175: xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
1.1 deraadt 176: if (r)
177: fprintf(stderr, "%s: rpc to yppasswdd failed.\n", progname);
1.3 deraadt 178: else if (status) {
1.1 deraadt 179: printf("Couldn't change YP password.\n");
1.7 ! provos 180: free(yppasswd.newpw.pw_passwd);
1.3 deraadt 181: exit(1);
182: }
183: printf("The YP password has been changed on %s, the master YP passwd server.\n",
184: master);
1.7 ! provos 185: free(yppasswd.newpw.pw_passwd);
1.1 deraadt 186: exit(0);
187: }
188:
189: static char *
190: getnewpasswd(pw, old_pass)
191: register struct passwd *pw;
192: char **old_pass;
193: {
194: static char buf[_PASSWORD_LEN+1];
195: register char *p, *t;
196: int tries;
1.5 provos 197: char salt[_PASSWORD_LEN], *crypt(), *getpass();
1.6 provos 198: int pwd_gensalt __P(( char *, int, struct passwd *, char));
1.1 deraadt 199:
1.3 deraadt 200: printf("Changing YP password for %s.\n", pw->pw_name);
1.1 deraadt 201:
202: if (old_pass) {
203: *old_pass = NULL;
204:
205: if (pw->pw_passwd[0]) {
1.3 deraadt 206: p = getpass("Old password:");
207: if (strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
208: errno = EACCES;
209: pw_error(NULL, 1, 1);
1.1 deraadt 210: }
1.3 deraadt 211: } else
1.1 deraadt 212: p = "";
213: *old_pass = strdup(p);
214: }
1.3 deraadt 215:
1.1 deraadt 216: for (buf[0] = '\0', tries = 0;;) {
217: p = getpass("New password:");
218: if (!*p) {
1.3 deraadt 219: printf("Password unchanged.\n");
1.1 deraadt 220: pw_error(NULL, 0, 0);
221: }
222: if (strlen(p) <= 5 && ++tries < 2) {
1.3 deraadt 223: printf("Please enter a longer password.\n");
1.1 deraadt 224: continue;
225: }
1.3 deraadt 226: for (t = p; *t && islower(*t); ++t)
227: ;
1.1 deraadt 228: if (!*t && ++tries < 2) {
1.3 deraadt 229: printf("Please don't use an all-lower case password.\nUnusual capitalization, control characters or digits are suggested.\n");
1.1 deraadt 230: continue;
231: }
1.3 deraadt 232: strcpy(buf, p);
1.1 deraadt 233: if (!strcmp(buf, getpass("Retype new password:")))
234: break;
235: (void)printf("Mismatch; try again, EOF to quit.\n");
236: }
1.6 provos 237: if( !pwd_gensalt( salt, _PASSWORD_LEN, pw, 'y' )) {
238: (void)printf("Couldn't generate salt.\n");
239: pw_error(NULL, 0, 0);
240: }
1.1 deraadt 241: return(strdup(crypt(buf, salt)));
242: }
243:
244: static char *
245: pwskip(register char *p)
246: {
247: while (*p && *p != ':' && *p != '\n')
248: ++p;
249: if (*p)
250: *p++ = 0;
251: return (p);
252: }
253:
254: struct passwd *
255: interpret(struct passwd *pwent, char *line)
256: {
257: register char *p = line;
258: register int c;
259:
260: pwent->pw_passwd = "*";
261: pwent->pw_uid = 0;
262: pwent->pw_gid = 0;
263: pwent->pw_gecos = "";
264: pwent->pw_dir = "";
265: pwent->pw_shell = "";
266: pwent->pw_change = 0;
267: pwent->pw_expire = 0;
268: pwent->pw_class = "";
269:
270: /* line without colon separators is no good, so ignore it */
1.3 deraadt 271: if(!strchr(p, ':'))
1.1 deraadt 272: return(NULL);
273:
274: pwent->pw_name = p;
275: p = pwskip(p);
276: pwent->pw_passwd = p;
277: p = pwskip(p);
278: pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
279: p = pwskip(p);
280: pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
281: p = pwskip(p);
282: pwent->pw_gecos = p;
283: p = pwskip(p);
284: pwent->pw_dir = p;
285: p = pwskip(p);
286: pwent->pw_shell = p;
287: while (*p && *p != '\n')
288: p++;
289: *p = '\0';
290: return (pwent);
291: }
292:
1.4 deraadt 293: static char *__yplin;
294:
1.1 deraadt 295: static struct passwd *
296: ypgetpwnam(nam)
297: char *nam;
298: {
299: static struct passwd pwent;
300: char *val;
301: int reason, vallen;
302:
303: reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
1.3 deraadt 304: &val, &vallen);
1.1 deraadt 305: switch(reason) {
306: case 0:
307: break;
308: default:
309: return (NULL);
310: break;
311: }
312: val[vallen] = '\0';
1.4 deraadt 313: if (__yplin)
314: free(__yplin);
315: __yplin = (char *)malloc(vallen + 1);
316: strcpy(__yplin, val);
1.1 deraadt 317: free(val);
318:
1.4 deraadt 319: return(interpret(&pwent, __yplin));
1.1 deraadt 320: }
321:
322: #endif /* YP */