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