Annotation of src/usr.bin/passwd/yp_passwd.c, Revision 1.8
1.8 ! weingart 1: /* $OpenBSD: yp_passwd.c,v 1.7 1997/02/17 10:34:41 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.8 ! weingart 37: static char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.7 1997/02/17 10:34:41 provos 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)
! 75: warn(name != NULL ? 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.8 ! weingart 107: warnx("can't find the master YP server. Reason: %s\n", yperr_string(r));
1.1 deraadt 108: exit(1);
109: }
110:
111: /*
112: * Ask the portmapper for the port of the daemon.
113: */
114: if ((rpcport = getrpcport(master, YPPASSWDPROG,
115: YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
1.8 ! weingart 116: warnx("master YP server not running yppasswd daemon.");
! 117: warnx("Can't change password.");
1.1 deraadt 118: exit(1);
119: }
120:
121: /*
122: * Be sure the port is priviledged
123: */
124: if (rpcport >= IPPORT_RESERVED) {
1.8 ! weingart 125: warnx("yppasswd daemon is on an invalid port.");
1.1 deraadt 126: exit(1);
127: }
128:
129: /* Get user's login identity */
130: if (!(pw = ypgetpwnam(username))) {
1.8 ! weingart 131: warnx("unknown user %s.", username);
1.1 deraadt 132: exit(1);
133: }
134:
135: if (uid && uid != pw->pw_uid) {
1.8 ! weingart 136: warnx("you may only change your own password: %s", strerror(EACCES));
1.1 deraadt 137: exit(1);
138: }
139:
140: /* prompt for new password */
141: yppasswd.newpw.pw_passwd = getnewpasswd(pw, &yppasswd.oldpass);
142:
143: /* tell rpc.yppasswdd */
144: yppasswd.newpw.pw_name = pw->pw_name;
145: yppasswd.newpw.pw_uid = pw->pw_uid;
146: yppasswd.newpw.pw_gid = pw->pw_gid;
147: yppasswd.newpw.pw_gecos = pw->pw_gecos;
148: yppasswd.newpw.pw_dir = pw->pw_dir;
149: yppasswd.newpw.pw_shell = pw->pw_shell;
150:
151: client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
152: if (client==NULL) {
1.8 ! weingart 153: warnx("cannot contact yppasswdd on %s: Reason: %s\n",
1.1 deraadt 154: master, yperr_string(YPERR_YPBIND));
1.7 provos 155: free(yppasswd.newpw.pw_passwd);
1.1 deraadt 156: return(YPERR_YPBIND);
157: }
158: client->cl_auth = authunix_create_default();
159: tv.tv_sec = 2;
160: tv.tv_usec = 0;
161: r = clnt_call(client, YPPASSWDPROC_UPDATE,
1.3 deraadt 162: xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
1.1 deraadt 163: if (r)
1.8 ! weingart 164: warnx("rpc to yppasswdd failed.");
1.3 deraadt 165: else if (status) {
1.1 deraadt 166: printf("Couldn't change YP password.\n");
1.7 provos 167: free(yppasswd.newpw.pw_passwd);
1.3 deraadt 168: exit(1);
169: }
170: printf("The YP password has been changed on %s, the master YP passwd server.\n",
171: master);
1.7 provos 172: free(yppasswd.newpw.pw_passwd);
1.1 deraadt 173: exit(0);
174: }
175:
176: static char *
177: getnewpasswd(pw, old_pass)
178: register struct passwd *pw;
179: char **old_pass;
180: {
181: static char buf[_PASSWORD_LEN+1];
182: register char *p, *t;
183: int tries;
1.5 provos 184: char salt[_PASSWORD_LEN], *crypt(), *getpass();
1.6 provos 185: int pwd_gensalt __P(( char *, int, struct passwd *, char));
1.1 deraadt 186:
1.3 deraadt 187: printf("Changing YP password for %s.\n", pw->pw_name);
1.1 deraadt 188:
189: if (old_pass) {
190: *old_pass = NULL;
191:
192: if (pw->pw_passwd[0]) {
1.3 deraadt 193: p = getpass("Old password:");
194: if (strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
195: errno = EACCES;
196: pw_error(NULL, 1, 1);
1.1 deraadt 197: }
1.3 deraadt 198: } else
1.1 deraadt 199: p = "";
200: *old_pass = strdup(p);
201: }
1.3 deraadt 202:
1.1 deraadt 203: for (buf[0] = '\0', tries = 0;;) {
204: p = getpass("New password:");
205: if (!*p) {
1.3 deraadt 206: printf("Password unchanged.\n");
1.1 deraadt 207: pw_error(NULL, 0, 0);
208: }
209: if (strlen(p) <= 5 && ++tries < 2) {
1.3 deraadt 210: printf("Please enter a longer password.\n");
1.1 deraadt 211: continue;
212: }
1.3 deraadt 213: for (t = p; *t && islower(*t); ++t)
214: ;
1.1 deraadt 215: if (!*t && ++tries < 2) {
1.3 deraadt 216: printf("Please don't use an all-lower case password.\nUnusual capitalization, control characters or digits are suggested.\n");
1.1 deraadt 217: continue;
218: }
1.3 deraadt 219: strcpy(buf, p);
1.1 deraadt 220: if (!strcmp(buf, getpass("Retype new password:")))
221: break;
222: (void)printf("Mismatch; try again, EOF to quit.\n");
223: }
1.6 provos 224: if( !pwd_gensalt( salt, _PASSWORD_LEN, pw, 'y' )) {
225: (void)printf("Couldn't generate salt.\n");
226: pw_error(NULL, 0, 0);
227: }
1.1 deraadt 228: return(strdup(crypt(buf, salt)));
229: }
230:
231: static char *
232: pwskip(register char *p)
233: {
234: while (*p && *p != ':' && *p != '\n')
235: ++p;
236: if (*p)
237: *p++ = 0;
238: return (p);
239: }
240:
241: struct passwd *
242: interpret(struct passwd *pwent, char *line)
243: {
244: register char *p = line;
245:
246: pwent->pw_passwd = "*";
247: pwent->pw_uid = 0;
248: pwent->pw_gid = 0;
249: pwent->pw_gecos = "";
250: pwent->pw_dir = "";
251: pwent->pw_shell = "";
252: pwent->pw_change = 0;
253: pwent->pw_expire = 0;
254: pwent->pw_class = "";
255:
256: /* line without colon separators is no good, so ignore it */
1.3 deraadt 257: if(!strchr(p, ':'))
1.1 deraadt 258: return(NULL);
259:
260: pwent->pw_name = p;
261: p = pwskip(p);
262: pwent->pw_passwd = p;
263: p = pwskip(p);
264: pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
265: p = pwskip(p);
266: pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
267: p = pwskip(p);
268: pwent->pw_gecos = p;
269: p = pwskip(p);
270: pwent->pw_dir = p;
271: p = pwskip(p);
272: pwent->pw_shell = p;
273: while (*p && *p != '\n')
274: p++;
275: *p = '\0';
276: return (pwent);
277: }
278:
1.4 deraadt 279: static char *__yplin;
280:
1.1 deraadt 281: static struct passwd *
282: ypgetpwnam(nam)
283: char *nam;
284: {
285: static struct passwd pwent;
286: char *val;
287: int reason, vallen;
288:
289: reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
1.3 deraadt 290: &val, &vallen);
1.1 deraadt 291: switch(reason) {
292: case 0:
293: break;
294: default:
295: return (NULL);
296: break;
297: }
298: val[vallen] = '\0';
1.4 deraadt 299: if (__yplin)
300: free(__yplin);
301: __yplin = (char *)malloc(vallen + 1);
302: strcpy(__yplin, val);
1.1 deraadt 303: free(val);
304:
1.4 deraadt 305: return(interpret(&pwent, __yplin));
1.1 deraadt 306: }
307:
308: #endif /* YP */