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