Annotation of src/usr.bin/passwd/yp_passwd.c, Revision 1.28
1.28 ! millert 1: /* $OpenBSD: yp_passwd.c,v 1.27 2004/03/12 22:59:28 millert 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.28 ! millert 33: static const char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.27 2004/03/12 22:59:28 millert 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.1 deraadt 175: if (r)
1.8 weingart 176: warnx("rpc to yppasswdd failed.");
1.3 deraadt 177: else if (status) {
1.1 deraadt 178: printf("Couldn't change YP password.\n");
1.7 provos 179: free(yppasswd.newpw.pw_passwd);
1.21 fgsch 180: return (1);
1.3 deraadt 181: }
1.21 fgsch 182: printf("The YP password has been changed on %s, "
183: "the master YP passwd server.\n", master);
1.7 provos 184: free(yppasswd.newpw.pw_passwd);
1.21 fgsch 185: return (0);
1.1 deraadt 186: }
187:
1.15 millert 188: char *
1.23 deraadt 189: ypgetnewpasswd(struct passwd *pw, login_cap_t *lc, char **old_pass)
1.1 deraadt 190: {
191: static char buf[_PASSWORD_LEN+1];
1.15 millert 192: char salt[_PASSWORD_LEN];
1.19 millert 193: sig_t saveint, savequit;
1.23 deraadt 194: int tries, pwd_tries;
195: char *p;
1.19 millert 196:
197: saveint = signal(SIGINT, kbintr);
198: savequit = signal(SIGQUIT, kbintr);
1.21 fgsch 199:
1.3 deraadt 200: printf("Changing YP password for %s.\n", pw->pw_name);
1.1 deraadt 201: if (old_pass) {
202: *old_pass = NULL;
1.21 fgsch 203:
1.1 deraadt 204: if (pw->pw_passwd[0]) {
1.3 deraadt 205: p = getpass("Old password:");
1.26 millert 206: if (p == NULL ||
207: strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
1.3 deraadt 208: errno = EACCES;
209: pw_error(NULL, 1, 1);
1.1 deraadt 210: }
1.3 deraadt 211: } else
1.1 deraadt 212: p = "";
213: *old_pass = strdup(p);
1.16 deraadt 214: if (*old_pass == NULL)
215: pw_error(NULL, 1, 1);
1.1 deraadt 216: }
1.3 deraadt 217:
1.28 ! millert 218: pwd_tries = pwd_gettries(lc);
1.14 provos 219:
1.1 deraadt 220: for (buf[0] = '\0', tries = 0;;) {
221: p = getpass("New password:");
1.27 millert 222: if (p == NULL || *p == '\0')
223: pw_error(NULL, 0, p == NULL ? 1 : 0);
1.10 deraadt 224: if (strcmp(p, "s/key") == 0) {
1.21 fgsch 225: printf("That password collides with a system feature. "
226: "Choose another.\n");
1.10 deraadt 227: continue;
1.1 deraadt 228: }
1.21 fgsch 229: if ((tries++ < pwd_tries || pwd_tries == 0)
1.28 ! millert 230: && pwd_check(lc, p) == 0)
1.1 deraadt 231: continue;
1.23 deraadt 232: strlcpy(buf, p, sizeof buf);
1.26 millert 233: p = getpass("Retype new password:");
234: if (p != NULL && strcmp(buf, p) == 0)
1.1 deraadt 235: break;
236: (void)printf("Mismatch; try again, EOF to quit.\n");
237: }
1.28 ! millert 238: if (!pwd_gensalt(salt, _PASSWORD_LEN, lc, 'y')) {
1.21 fgsch 239: (void)printf("Couldn't generate salt.\n");
240: pw_error(NULL, 0, 0);
241: }
1.16 deraadt 242: p = strdup(crypt(buf, salt));
243: if (p == NULL)
244: pw_error(NULL, 1, 1);
1.19 millert 245: (void)signal(SIGINT, saveint);
246: (void)signal(SIGQUIT, savequit);
247:
1.16 deraadt 248: return (p);
1.1 deraadt 249: }
250:
251: static char *
1.15 millert 252: pwskip(char *p)
1.1 deraadt 253: {
254: while (*p && *p != ':' && *p != '\n')
255: ++p;
256: if (*p)
257: *p++ = 0;
258: return (p);
259: }
260:
261: struct passwd *
1.25 maja 262: interpret(struct passwd *pwent, char *line, int secure)
1.1 deraadt 263: {
1.15 millert 264: char *p = line;
1.1 deraadt 265:
266: pwent->pw_passwd = "*";
267: pwent->pw_uid = 0;
268: pwent->pw_gid = 0;
269: pwent->pw_gecos = "";
270: pwent->pw_dir = "";
271: pwent->pw_shell = "";
272: pwent->pw_change = 0;
273: pwent->pw_expire = 0;
274: pwent->pw_class = "";
1.21 fgsch 275:
1.1 deraadt 276: /* line without colon separators is no good, so ignore it */
1.23 deraadt 277: if (!strchr(p, ':'))
1.21 fgsch 278: return (NULL);
1.1 deraadt 279:
280: pwent->pw_name = p;
281: p = pwskip(p);
282: pwent->pw_passwd = p;
283: p = pwskip(p);
284: pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
285: p = pwskip(p);
286: pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
287: p = pwskip(p);
1.25 maja 288: if ( secure == 1 ) {
289: pwent->pw_class = p;
290: p = pwskip(p);
291: pwent->pw_change = (time_t)strtoul(p, NULL, 10);
292: p = pwskip(p);
293: pwent->pw_expire = (time_t)strtoul(p, NULL, 10);
294: p = pwskip(p);
295: }
1.1 deraadt 296: pwent->pw_gecos = p;
297: p = pwskip(p);
298: pwent->pw_dir = p;
299: p = pwskip(p);
300: pwent->pw_shell = p;
301: while (*p && *p != '\n')
302: p++;
303: *p = '\0';
304: return (pwent);
305: }
306:
1.4 deraadt 307: static char *__yplin;
308:
1.15 millert 309: struct passwd *
1.25 maja 310: ypgetpwnam(char *nam, int secure)
1.1 deraadt 311: {
312: static struct passwd pwent;
1.23 deraadt 313: int reason, vallen;
1.1 deraadt 314: char *val;
1.21 fgsch 315:
1.25 maja 316: reason = yp_match(domain,
317: secure ? "master.passwd.byname" : "passwd.byname",
318: nam, strlen(nam), &val, &vallen);
1.23 deraadt 319: switch (reason) {
1.1 deraadt 320: case 0:
321: break;
322: default:
323: return (NULL);
324: break;
325: }
326: val[vallen] = '\0';
1.4 deraadt 327: if (__yplin)
328: free(__yplin);
329: __yplin = (char *)malloc(vallen + 1);
1.16 deraadt 330: if (__yplin == NULL)
331: pw_error(NULL, 1, 1);
1.23 deraadt 332: strlcpy(__yplin, val, vallen + 1);
1.1 deraadt 333: free(val);
334:
1.25 maja 335: return (interpret(&pwent, __yplin, secure));
1.1 deraadt 336: }
337:
338: #endif /* YP */