Annotation of src/usr.bin/passwd/yp_passwd.c, Revision 1.24
1.24 ! millert 1: /* $OpenBSD: yp_passwd.c,v 1.23 2002/06/28 22:28:17 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.24 ! millert 33: static const char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.23 2002/06/28 22:28:17 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.21 fgsch 60: extern int pwd_gensalt(char *, int, struct passwd *, login_cap_t *, char);
61: extern int pwd_check(struct passwd *, login_cap_t *, char *);
62: extern int pwd_gettries(struct passwd *, login_cap_t *);
63: extern void kbintr(int);
1.14 provos 64:
1.23 deraadt 65: char *ypgetnewpasswd(struct passwd *, login_cap_t *, char **);
66: struct passwd *ypgetpwnam(char *);
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.1 deraadt 88: int r, rpcport, status;
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: */
109: if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
1.9 millert 110: warnx("can't find the master YP server. Reason: %s",
111: yperr_string(r));
1.21 fgsch 112: return (1);
1.1 deraadt 113: }
114:
115: /*
116: * Ask the portmapper for the port of the daemon.
117: */
118: if ((rpcport = getrpcport(master, YPPASSWDPROG,
119: YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
1.8 weingart 120: warnx("master YP server not running yppasswd daemon.");
121: warnx("Can't change password.");
1.21 fgsch 122: return (1);
1.1 deraadt 123: }
124:
125: /*
1.22 deraadt 126: * Be sure the port is privileged
1.1 deraadt 127: */
128: if (rpcport >= IPPORT_RESERVED) {
1.8 weingart 129: warnx("yppasswd daemon is on an invalid port.");
1.21 fgsch 130: return (1);
1.1 deraadt 131: }
132:
133: /* Get user's login identity */
134: if (!(pw = ypgetpwnam(username))) {
1.8 weingart 135: warnx("unknown user %s.", username);
1.21 fgsch 136: return (1);
1.1 deraadt 137: }
1.17 millert 138: if ((lc = login_getclass(pw->pw_class)) == NULL) {
139: warnx("unable to get login class for user %s.", username);
1.21 fgsch 140: return (1);
1.17 millert 141: }
1.21 fgsch 142:
1.15 millert 143: uid = getuid();
1.1 deraadt 144: if (uid && uid != pw->pw_uid) {
1.21 fgsch 145: warnx("you may only change your own password: %s",
146: strerror(EACCES));
147: return (1);
1.1 deraadt 148: }
149:
150: /* prompt for new password */
1.17 millert 151: yppasswd.newpw.pw_passwd = ypgetnewpasswd(pw, lc, &yppasswd.oldpass);
1.1 deraadt 152:
153: /* tell rpc.yppasswdd */
154: yppasswd.newpw.pw_name = pw->pw_name;
1.23 deraadt 155: yppasswd.newpw.pw_uid = pw->pw_uid;
1.1 deraadt 156: yppasswd.newpw.pw_gid = pw->pw_gid;
157: yppasswd.newpw.pw_gecos = pw->pw_gecos;
158: yppasswd.newpw.pw_dir = pw->pw_dir;
159: yppasswd.newpw.pw_shell = pw->pw_shell;
1.21 fgsch 160:
1.1 deraadt 161: client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
162: if (client==NULL) {
1.9 millert 163: warnx("cannot contact yppasswdd on %s: Reason: %s",
1.1 deraadt 164: master, yperr_string(YPERR_YPBIND));
1.7 provos 165: free(yppasswd.newpw.pw_passwd);
1.21 fgsch 166: return (YPERR_YPBIND);
1.1 deraadt 167: }
168: client->cl_auth = authunix_create_default();
169: tv.tv_sec = 2;
170: tv.tv_usec = 0;
171: r = clnt_call(client, YPPASSWDPROC_UPDATE,
1.3 deraadt 172: xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
1.1 deraadt 173: if (r)
1.8 weingart 174: warnx("rpc to yppasswdd failed.");
1.3 deraadt 175: else if (status) {
1.1 deraadt 176: printf("Couldn't change YP password.\n");
1.7 provos 177: free(yppasswd.newpw.pw_passwd);
1.21 fgsch 178: return (1);
1.3 deraadt 179: }
1.21 fgsch 180: printf("The YP password has been changed on %s, "
181: "the master YP passwd server.\n", master);
1.7 provos 182: free(yppasswd.newpw.pw_passwd);
1.21 fgsch 183: return (0);
1.1 deraadt 184: }
185:
1.15 millert 186: char *
1.23 deraadt 187: ypgetnewpasswd(struct passwd *pw, login_cap_t *lc, char **old_pass)
1.1 deraadt 188: {
189: static char buf[_PASSWORD_LEN+1];
1.15 millert 190: char salt[_PASSWORD_LEN];
1.19 millert 191: sig_t saveint, savequit;
1.23 deraadt 192: int tries, pwd_tries;
193: char *p;
1.19 millert 194:
195: saveint = signal(SIGINT, kbintr);
196: savequit = signal(SIGQUIT, kbintr);
1.21 fgsch 197:
1.3 deraadt 198: printf("Changing YP password for %s.\n", pw->pw_name);
1.1 deraadt 199: if (old_pass) {
200: *old_pass = NULL;
1.21 fgsch 201:
1.1 deraadt 202: if (pw->pw_passwd[0]) {
1.3 deraadt 203: p = getpass("Old password:");
204: if (strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
205: errno = EACCES;
206: pw_error(NULL, 1, 1);
1.1 deraadt 207: }
1.3 deraadt 208: } else
1.1 deraadt 209: p = "";
210: *old_pass = strdup(p);
1.16 deraadt 211: if (*old_pass == NULL)
212: pw_error(NULL, 1, 1);
1.1 deraadt 213: }
1.3 deraadt 214:
1.17 millert 215: pwd_tries = pwd_gettries(pw, lc);
1.14 provos 216:
1.1 deraadt 217: for (buf[0] = '\0', tries = 0;;) {
218: p = getpass("New password:");
219: if (!*p) {
1.3 deraadt 220: printf("Password unchanged.\n");
1.1 deraadt 221: pw_error(NULL, 0, 0);
1.10 deraadt 222: }
223: if (strcmp(p, "s/key") == 0) {
1.21 fgsch 224: printf("That password collides with a system feature. "
225: "Choose another.\n");
1.10 deraadt 226: continue;
1.1 deraadt 227: }
1.21 fgsch 228: if ((tries++ < pwd_tries || pwd_tries == 0)
1.17 millert 229: && pwd_check(pw, lc, p) == 0)
1.1 deraadt 230: continue;
1.23 deraadt 231: strlcpy(buf, p, sizeof buf);
1.1 deraadt 232: if (!strcmp(buf, getpass("Retype new password:")))
233: break;
234: (void)printf("Mismatch; try again, EOF to quit.\n");
235: }
1.23 deraadt 236: if (!pwd_gensalt(salt, _PASSWORD_LEN, pw, lc, 'y')) {
1.21 fgsch 237: (void)printf("Couldn't generate salt.\n");
238: pw_error(NULL, 0, 0);
239: }
1.16 deraadt 240: p = strdup(crypt(buf, salt));
241: if (p == NULL)
242: pw_error(NULL, 1, 1);
1.19 millert 243: (void)signal(SIGINT, saveint);
244: (void)signal(SIGQUIT, savequit);
245:
1.16 deraadt 246: return (p);
1.1 deraadt 247: }
248:
249: static char *
1.15 millert 250: pwskip(char *p)
1.1 deraadt 251: {
252: while (*p && *p != ':' && *p != '\n')
253: ++p;
254: if (*p)
255: *p++ = 0;
256: return (p);
257: }
258:
259: struct passwd *
260: interpret(struct passwd *pwent, char *line)
261: {
1.15 millert 262: char *p = line;
1.1 deraadt 263:
264: pwent->pw_passwd = "*";
265: pwent->pw_uid = 0;
266: pwent->pw_gid = 0;
267: pwent->pw_gecos = "";
268: pwent->pw_dir = "";
269: pwent->pw_shell = "";
270: pwent->pw_change = 0;
271: pwent->pw_expire = 0;
272: pwent->pw_class = "";
1.21 fgsch 273:
1.1 deraadt 274: /* line without colon separators is no good, so ignore it */
1.23 deraadt 275: if (!strchr(p, ':'))
1.21 fgsch 276: return (NULL);
1.1 deraadt 277:
278: pwent->pw_name = p;
279: p = pwskip(p);
280: pwent->pw_passwd = p;
281: p = pwskip(p);
282: pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
283: p = pwskip(p);
284: pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
285: p = pwskip(p);
286: pwent->pw_gecos = p;
287: p = pwskip(p);
288: pwent->pw_dir = p;
289: p = pwskip(p);
290: pwent->pw_shell = p;
291: while (*p && *p != '\n')
292: p++;
293: *p = '\0';
294: return (pwent);
295: }
296:
1.4 deraadt 297: static char *__yplin;
298:
1.15 millert 299: struct passwd *
1.23 deraadt 300: ypgetpwnam(char *nam)
1.1 deraadt 301: {
302: static struct passwd pwent;
1.23 deraadt 303: int reason, vallen;
1.1 deraadt 304: char *val;
1.21 fgsch 305:
1.1 deraadt 306: reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
1.3 deraadt 307: &val, &vallen);
1.23 deraadt 308: switch (reason) {
1.1 deraadt 309: case 0:
310: break;
311: default:
312: return (NULL);
313: break;
314: }
315: val[vallen] = '\0';
1.4 deraadt 316: if (__yplin)
317: free(__yplin);
318: __yplin = (char *)malloc(vallen + 1);
1.16 deraadt 319: if (__yplin == NULL)
320: pw_error(NULL, 1, 1);
1.23 deraadt 321: strlcpy(__yplin, val, vallen + 1);
1.1 deraadt 322: free(val);
323:
1.21 fgsch 324: return (interpret(&pwent, __yplin));
1.1 deraadt 325: }
326:
327: #endif /* YP */