Annotation of src/usr.bin/passwd/yp_passwd.c, Revision 1.30
1.30 ! aanriot 1: /* $OpenBSD: yp_passwd.c,v 1.29 2006/03/30 21:06:29 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.
1.24 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
31: #ifndef lint
1.18 millert 32: /*static const char sccsid[] = "from: @(#)yp_passwd.c 1.0 2/2/93";*/
1.30 ! aanriot 33: static const char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.29 2006/03/30 21:06:29 deraadt Exp $";
1.1 deraadt 34: #endif /* not lint */
35:
36: #ifdef YP
37:
38: #include <stdio.h>
1.8 weingart 39: #include <stdlib.h>
1.1 deraadt 40: #include <string.h>
1.8 weingart 41: #include <unistd.h>
1.1 deraadt 42: #include <netdb.h>
43: #include <time.h>
44: #include <pwd.h>
1.8 weingart 45: #include <err.h>
1.1 deraadt 46: #include <errno.h>
1.8 weingart 47: #include <ctype.h>
1.17 millert 48: #include <login_cap.h>
1.1 deraadt 49: #include <rpc/rpc.h>
50: #include <rpcsvc/yp_prot.h>
51: #include <rpcsvc/ypclnt.h>
52: #define passwd yp_passwd_rec
53: #include <rpcsvc/yppasswd.h>
54: #undef passwd
55:
56: #ifndef _PASSWORD_LEN
57: #define _PASSWORD_LEN PASS_MAX
58: #endif
59:
1.28 millert 60: extern int pwd_gensalt(char *, int, login_cap_t *, char);
61: extern int pwd_check(login_cap_t *, char *);
62: extern int pwd_gettries(login_cap_t *);
1.21 fgsch 63: extern void kbintr(int);
1.14 provos 64:
1.23 deraadt 65: char *ypgetnewpasswd(struct passwd *, login_cap_t *, char **);
1.25 maja 66: struct passwd *ypgetpwnam(char *, int);
1.1 deraadt 67:
68: char *domain;
69:
1.8 weingart 70: static int
1.23 deraadt 71: pw_error(char *name, int err, int eval)
1.1 deraadt 72: {
1.21 fgsch 73: if (err) {
74: if (name)
75: warn("%s", name);
76: else
77: warn(NULL);
78: }
1.1 deraadt 79:
1.21 fgsch 80: warnx("YP passwd database: unchanged.");
1.1 deraadt 81: exit(eval);
82: }
83:
1.8 weingart 84: int
1.23 deraadt 85: yp_passwd(char *username)
1.1 deraadt 86: {
1.23 deraadt 87: struct yppasswd yppasswd;
1.25 maja 88: int r, rpcport, status, secure=0;
1.1 deraadt 89: struct passwd *pw;
90: struct timeval tv;
1.23 deraadt 91: login_cap_t *lc;
1.1 deraadt 92: CLIENT *client;
1.23 deraadt 93: char *master;
94: uid_t uid;
1.1 deraadt 95:
96: /*
97: * Get local domain
98: */
1.8 weingart 99: if ((r = yp_get_default_domain(&domain)) != 0) {
1.21 fgsch 100: warnx("can't get local YP domain. Reason: %s",
101: yperr_string(r));
102: return (1);
1.1 deraadt 103: }
104:
105: /*
106: * Find the host for the passwd map; it should be running
107: * the daemon.
108: */
1.25 maja 109: if ((r = yp_master(domain, "master.passwd.byname", &master)) == 0) {
110: secure=1;
111: } else if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
1.9 millert 112: warnx("can't find the master YP server. Reason: %s",
113: yperr_string(r));
1.21 fgsch 114: return (1);
1.1 deraadt 115: }
116:
117: /*
118: * Ask the portmapper for the port of the daemon.
119: */
120: if ((rpcport = getrpcport(master, YPPASSWDPROG,
121: YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
1.8 weingart 122: warnx("master YP server not running yppasswd daemon.");
123: warnx("Can't change password.");
1.21 fgsch 124: return (1);
1.1 deraadt 125: }
126:
127: /*
1.22 deraadt 128: * Be sure the port is privileged
1.1 deraadt 129: */
130: if (rpcport >= IPPORT_RESERVED) {
1.8 weingart 131: warnx("yppasswd daemon is on an invalid port.");
1.21 fgsch 132: return (1);
1.1 deraadt 133: }
134:
135: /* Get user's login identity */
1.25 maja 136: if (!(pw = ypgetpwnam(username, secure))) {
1.8 weingart 137: warnx("unknown user %s.", username);
1.21 fgsch 138: return (1);
1.1 deraadt 139: }
1.17 millert 140: if ((lc = login_getclass(pw->pw_class)) == NULL) {
141: warnx("unable to get login class for user %s.", username);
1.21 fgsch 142: return (1);
1.17 millert 143: }
1.21 fgsch 144:
1.15 millert 145: uid = getuid();
1.1 deraadt 146: if (uid && uid != pw->pw_uid) {
1.21 fgsch 147: warnx("you may only change your own password: %s",
148: strerror(EACCES));
149: return (1);
1.1 deraadt 150: }
151:
152: /* prompt for new password */
1.17 millert 153: yppasswd.newpw.pw_passwd = ypgetnewpasswd(pw, lc, &yppasswd.oldpass);
1.1 deraadt 154:
155: /* tell rpc.yppasswdd */
156: yppasswd.newpw.pw_name = pw->pw_name;
1.23 deraadt 157: yppasswd.newpw.pw_uid = pw->pw_uid;
1.1 deraadt 158: yppasswd.newpw.pw_gid = pw->pw_gid;
159: yppasswd.newpw.pw_gecos = pw->pw_gecos;
160: yppasswd.newpw.pw_dir = pw->pw_dir;
161: yppasswd.newpw.pw_shell = pw->pw_shell;
1.21 fgsch 162:
1.1 deraadt 163: client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
164: if (client==NULL) {
1.9 millert 165: warnx("cannot contact yppasswdd on %s: Reason: %s",
1.1 deraadt 166: master, yperr_string(YPERR_YPBIND));
1.7 provos 167: free(yppasswd.newpw.pw_passwd);
1.21 fgsch 168: return (YPERR_YPBIND);
1.1 deraadt 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,
1.3 deraadt 174: xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
1.30 ! aanriot 175: if (r) {
! 176: printf("rpc to yppasswdd failed.\n");
! 177: free(yppasswd.newpw.pw_passwd);
! 178: return (1);
! 179: } else if (status) {
1.1 deraadt 180: printf("Couldn't change YP password.\n");
1.7 provos 181: free(yppasswd.newpw.pw_passwd);
1.21 fgsch 182: return (1);
1.30 ! aanriot 183: } else {
! 184: printf("The YP password has been changed on %s, "
! 185: "the master YP passwd server.\n", master);
! 186: free(yppasswd.newpw.pw_passwd);
! 187: return (0);
1.3 deraadt 188: }
1.1 deraadt 189: }
190:
1.15 millert 191: char *
1.23 deraadt 192: ypgetnewpasswd(struct passwd *pw, login_cap_t *lc, char **old_pass)
1.1 deraadt 193: {
194: static char buf[_PASSWORD_LEN+1];
1.15 millert 195: char salt[_PASSWORD_LEN];
1.19 millert 196: sig_t saveint, savequit;
1.23 deraadt 197: int tries, pwd_tries;
198: char *p;
1.19 millert 199:
200: saveint = signal(SIGINT, kbintr);
201: savequit = signal(SIGQUIT, kbintr);
1.21 fgsch 202:
1.3 deraadt 203: printf("Changing YP password for %s.\n", pw->pw_name);
1.1 deraadt 204: if (old_pass) {
205: *old_pass = NULL;
1.21 fgsch 206:
1.1 deraadt 207: if (pw->pw_passwd[0]) {
1.3 deraadt 208: p = getpass("Old password:");
1.26 millert 209: if (p == NULL ||
210: strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
1.3 deraadt 211: errno = EACCES;
212: pw_error(NULL, 1, 1);
1.1 deraadt 213: }
1.3 deraadt 214: } else
1.1 deraadt 215: p = "";
216: *old_pass = strdup(p);
1.16 deraadt 217: if (*old_pass == NULL)
218: pw_error(NULL, 1, 1);
1.1 deraadt 219: }
1.3 deraadt 220:
1.28 millert 221: pwd_tries = pwd_gettries(lc);
1.14 provos 222:
1.1 deraadt 223: for (buf[0] = '\0', tries = 0;;) {
224: p = getpass("New password:");
1.27 millert 225: if (p == NULL || *p == '\0')
226: pw_error(NULL, 0, p == NULL ? 1 : 0);
1.10 deraadt 227: if (strcmp(p, "s/key") == 0) {
1.21 fgsch 228: printf("That password collides with a system feature. "
229: "Choose another.\n");
1.10 deraadt 230: continue;
1.1 deraadt 231: }
1.21 fgsch 232: if ((tries++ < pwd_tries || pwd_tries == 0)
1.28 millert 233: && pwd_check(lc, p) == 0)
1.1 deraadt 234: continue;
1.23 deraadt 235: strlcpy(buf, p, sizeof buf);
1.26 millert 236: p = getpass("Retype new password:");
237: if (p != NULL && strcmp(buf, p) == 0)
1.1 deraadt 238: break;
239: (void)printf("Mismatch; try again, EOF to quit.\n");
240: }
1.28 millert 241: if (!pwd_gensalt(salt, _PASSWORD_LEN, lc, 'y')) {
1.21 fgsch 242: (void)printf("Couldn't generate salt.\n");
243: pw_error(NULL, 0, 0);
244: }
1.16 deraadt 245: p = strdup(crypt(buf, salt));
246: if (p == NULL)
247: pw_error(NULL, 1, 1);
1.19 millert 248: (void)signal(SIGINT, saveint);
249: (void)signal(SIGQUIT, savequit);
250:
1.16 deraadt 251: return (p);
1.1 deraadt 252: }
253:
254: static char *
1.15 millert 255: pwskip(char *p)
1.1 deraadt 256: {
257: while (*p && *p != ':' && *p != '\n')
258: ++p;
259: if (*p)
260: *p++ = 0;
261: return (p);
262: }
263:
264: struct passwd *
1.25 maja 265: interpret(struct passwd *pwent, char *line, int secure)
1.1 deraadt 266: {
1.15 millert 267: char *p = line;
1.1 deraadt 268:
269: pwent->pw_passwd = "*";
270: pwent->pw_uid = 0;
271: pwent->pw_gid = 0;
272: pwent->pw_gecos = "";
273: pwent->pw_dir = "";
274: pwent->pw_shell = "";
275: pwent->pw_change = 0;
276: pwent->pw_expire = 0;
277: pwent->pw_class = "";
1.21 fgsch 278:
1.1 deraadt 279: /* line without colon separators is no good, so ignore it */
1.23 deraadt 280: if (!strchr(p, ':'))
1.21 fgsch 281: return (NULL);
1.1 deraadt 282:
283: pwent->pw_name = p;
284: p = pwskip(p);
285: pwent->pw_passwd = p;
286: p = pwskip(p);
287: pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
288: p = pwskip(p);
289: pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
290: p = pwskip(p);
1.25 maja 291: if ( secure == 1 ) {
292: pwent->pw_class = p;
293: p = pwskip(p);
294: pwent->pw_change = (time_t)strtoul(p, NULL, 10);
295: p = pwskip(p);
296: pwent->pw_expire = (time_t)strtoul(p, NULL, 10);
297: p = pwskip(p);
298: }
1.1 deraadt 299: pwent->pw_gecos = p;
300: p = pwskip(p);
301: pwent->pw_dir = p;
302: p = pwskip(p);
303: pwent->pw_shell = p;
304: while (*p && *p != '\n')
305: p++;
306: *p = '\0';
307: return (pwent);
308: }
309:
1.4 deraadt 310: static char *__yplin;
311:
1.15 millert 312: struct passwd *
1.25 maja 313: ypgetpwnam(char *nam, int secure)
1.1 deraadt 314: {
315: static struct passwd pwent;
1.23 deraadt 316: int reason, vallen;
1.1 deraadt 317: char *val;
1.21 fgsch 318:
1.25 maja 319: reason = yp_match(domain,
1.29 deraadt 320: secure ? "master.passwd.byname" : "passwd.byname",
321: nam, strlen(nam), &val, &vallen);
1.23 deraadt 322: switch (reason) {
1.1 deraadt 323: case 0:
324: break;
325: default:
326: return (NULL);
327: }
328: val[vallen] = '\0';
1.4 deraadt 329: if (__yplin)
330: free(__yplin);
331: __yplin = (char *)malloc(vallen + 1);
1.16 deraadt 332: if (__yplin == NULL)
333: pw_error(NULL, 1, 1);
1.23 deraadt 334: strlcpy(__yplin, val, vallen + 1);
1.1 deraadt 335: free(val);
336:
1.25 maja 337: return (interpret(&pwent, __yplin, secure));
1.1 deraadt 338: }
339:
340: #endif /* YP */