Annotation of src/usr.bin/passwd/yp_passwd.c, Revision 1.32
1.32 ! deraadt 1: /* $OpenBSD: yp_passwd.c,v 1.31 2008/11/06 05:35:56 djm 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:
32: #ifdef YP
33:
34: #include <stdio.h>
1.8 weingart 35: #include <stdlib.h>
1.1 deraadt 36: #include <string.h>
1.8 weingart 37: #include <unistd.h>
1.1 deraadt 38: #include <netdb.h>
39: #include <time.h>
40: #include <pwd.h>
1.8 weingart 41: #include <err.h>
1.1 deraadt 42: #include <errno.h>
1.8 weingart 43: #include <ctype.h>
1.17 millert 44: #include <login_cap.h>
1.1 deraadt 45: #include <rpc/rpc.h>
46: #include <rpcsvc/yp_prot.h>
47: #include <rpcsvc/ypclnt.h>
48: #define passwd yp_passwd_rec
49: #include <rpcsvc/yppasswd.h>
50: #undef passwd
51:
52: #ifndef _PASSWORD_LEN
53: #define _PASSWORD_LEN PASS_MAX
54: #endif
55:
1.28 millert 56: extern int pwd_gensalt(char *, int, login_cap_t *, char);
57: extern int pwd_check(login_cap_t *, char *);
58: extern int pwd_gettries(login_cap_t *);
1.21 fgsch 59: extern void kbintr(int);
1.31 djm 60: int yp_passwd(char *);
1.14 provos 61:
1.23 deraadt 62: char *ypgetnewpasswd(struct passwd *, login_cap_t *, char **);
1.25 maja 63: struct passwd *ypgetpwnam(char *, int);
1.31 djm 64: struct passwd *interpret(struct passwd *, char *, int);
1.1 deraadt 65:
66: char *domain;
67:
1.8 weingart 68: static int
1.31 djm 69: pw_error(char *name, int error, int eval)
1.1 deraadt 70: {
1.31 djm 71: if (error) {
1.21 fgsch 72: if (name)
73: warn("%s", name);
74: else
75: warn(NULL);
76: }
1.1 deraadt 77:
1.21 fgsch 78: warnx("YP passwd database: unchanged.");
1.1 deraadt 79: exit(eval);
80: }
81:
1.8 weingart 82: int
1.23 deraadt 83: yp_passwd(char *username)
1.1 deraadt 84: {
1.31 djm 85: struct yppasswd yppwd;
1.25 maja 86: int r, rpcport, status, secure=0;
1.1 deraadt 87: struct passwd *pw;
88: struct timeval tv;
1.23 deraadt 89: login_cap_t *lc;
1.1 deraadt 90: CLIENT *client;
1.23 deraadt 91: char *master;
92: uid_t uid;
1.1 deraadt 93:
94: /*
95: * Get local domain
96: */
1.8 weingart 97: if ((r = yp_get_default_domain(&domain)) != 0) {
1.21 fgsch 98: warnx("can't get local YP domain. Reason: %s",
99: yperr_string(r));
100: return (1);
1.1 deraadt 101: }
102:
103: /*
104: * Find the host for the passwd map; it should be running
105: * the daemon.
106: */
1.25 maja 107: if ((r = yp_master(domain, "master.passwd.byname", &master)) == 0) {
108: secure=1;
109: } else 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 */
1.25 maja 134: if (!(pw = ypgetpwnam(username, secure))) {
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.31 djm 151: yppwd.newpw.pw_passwd = ypgetnewpasswd(pw, lc, &yppwd.oldpass);
1.1 deraadt 152:
153: /* tell rpc.yppasswdd */
1.31 djm 154: yppwd.newpw.pw_name = pw->pw_name;
155: yppwd.newpw.pw_uid = pw->pw_uid;
156: yppwd.newpw.pw_gid = pw->pw_gid;
157: yppwd.newpw.pw_gecos = pw->pw_gecos;
158: yppwd.newpw.pw_dir = pw->pw_dir;
159: yppwd.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.31 djm 165: free(yppwd.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.31 djm 172: xdr_yppasswd, &yppwd, xdr_int, &status, tv);
1.30 aanriot 173: if (r) {
174: printf("rpc to yppasswdd failed.\n");
1.31 djm 175: free(yppwd.newpw.pw_passwd);
1.30 aanriot 176: return (1);
177: } else if (status) {
1.1 deraadt 178: printf("Couldn't change YP password.\n");
1.31 djm 179: free(yppwd.newpw.pw_passwd);
1.21 fgsch 180: return (1);
1.30 aanriot 181: } else {
182: printf("The YP password has been changed on %s, "
183: "the master YP passwd server.\n", master);
1.31 djm 184: free(yppwd.newpw.pw_passwd);
1.30 aanriot 185: return (0);
1.3 deraadt 186: }
1.1 deraadt 187: }
188:
1.15 millert 189: char *
1.23 deraadt 190: ypgetnewpasswd(struct passwd *pw, login_cap_t *lc, char **old_pass)
1.1 deraadt 191: {
192: static char buf[_PASSWORD_LEN+1];
1.15 millert 193: char salt[_PASSWORD_LEN];
1.19 millert 194: sig_t saveint, savequit;
1.23 deraadt 195: int tries, pwd_tries;
196: char *p;
1.19 millert 197:
198: saveint = signal(SIGINT, kbintr);
199: savequit = signal(SIGQUIT, kbintr);
1.21 fgsch 200:
1.3 deraadt 201: printf("Changing YP password for %s.\n", pw->pw_name);
1.1 deraadt 202: if (old_pass) {
203: *old_pass = NULL;
1.21 fgsch 204:
1.1 deraadt 205: if (pw->pw_passwd[0]) {
1.3 deraadt 206: p = getpass("Old password:");
1.26 millert 207: if (p == NULL ||
208: strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
1.3 deraadt 209: errno = EACCES;
210: pw_error(NULL, 1, 1);
1.1 deraadt 211: }
1.3 deraadt 212: } else
1.1 deraadt 213: p = "";
214: *old_pass = strdup(p);
1.16 deraadt 215: if (*old_pass == NULL)
216: pw_error(NULL, 1, 1);
1.1 deraadt 217: }
1.3 deraadt 218:
1.28 millert 219: pwd_tries = pwd_gettries(lc);
1.14 provos 220:
1.1 deraadt 221: for (buf[0] = '\0', tries = 0;;) {
222: p = getpass("New password:");
1.27 millert 223: if (p == NULL || *p == '\0')
224: pw_error(NULL, 0, p == NULL ? 1 : 0);
1.10 deraadt 225: if (strcmp(p, "s/key") == 0) {
1.21 fgsch 226: printf("That password collides with a system feature. "
227: "Choose another.\n");
1.10 deraadt 228: continue;
1.1 deraadt 229: }
1.21 fgsch 230: if ((tries++ < pwd_tries || pwd_tries == 0)
1.28 millert 231: && pwd_check(lc, p) == 0)
1.1 deraadt 232: continue;
1.23 deraadt 233: strlcpy(buf, p, sizeof buf);
1.26 millert 234: p = getpass("Retype new password:");
235: if (p != NULL && strcmp(buf, p) == 0)
1.1 deraadt 236: break;
237: (void)printf("Mismatch; try again, EOF to quit.\n");
238: }
1.28 millert 239: if (!pwd_gensalt(salt, _PASSWORD_LEN, lc, 'y')) {
1.21 fgsch 240: (void)printf("Couldn't generate salt.\n");
241: pw_error(NULL, 0, 0);
242: }
1.16 deraadt 243: p = strdup(crypt(buf, salt));
244: if (p == NULL)
245: pw_error(NULL, 1, 1);
1.19 millert 246: (void)signal(SIGINT, saveint);
247: (void)signal(SIGQUIT, savequit);
248:
1.16 deraadt 249: return (p);
1.1 deraadt 250: }
251:
252: static char *
1.15 millert 253: pwskip(char *p)
1.1 deraadt 254: {
255: while (*p && *p != ':' && *p != '\n')
256: ++p;
257: if (*p)
258: *p++ = 0;
259: return (p);
260: }
261:
262: struct passwd *
1.25 maja 263: interpret(struct passwd *pwent, char *line, int secure)
1.1 deraadt 264: {
1.15 millert 265: char *p = line;
1.1 deraadt 266:
267: pwent->pw_passwd = "*";
268: pwent->pw_uid = 0;
269: pwent->pw_gid = 0;
270: pwent->pw_gecos = "";
271: pwent->pw_dir = "";
272: pwent->pw_shell = "";
273: pwent->pw_change = 0;
274: pwent->pw_expire = 0;
275: pwent->pw_class = "";
1.21 fgsch 276:
1.1 deraadt 277: /* line without colon separators is no good, so ignore it */
1.23 deraadt 278: if (!strchr(p, ':'))
1.21 fgsch 279: return (NULL);
1.1 deraadt 280:
281: pwent->pw_name = p;
282: p = pwskip(p);
283: pwent->pw_passwd = p;
284: p = pwskip(p);
285: pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
286: p = pwskip(p);
287: pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
288: p = pwskip(p);
1.25 maja 289: if ( secure == 1 ) {
290: pwent->pw_class = p;
291: p = pwskip(p);
292: pwent->pw_change = (time_t)strtoul(p, NULL, 10);
293: p = pwskip(p);
294: pwent->pw_expire = (time_t)strtoul(p, NULL, 10);
295: p = pwskip(p);
296: }
1.1 deraadt 297: pwent->pw_gecos = p;
298: p = pwskip(p);
299: pwent->pw_dir = p;
300: p = pwskip(p);
301: pwent->pw_shell = p;
302: while (*p && *p != '\n')
303: p++;
304: *p = '\0';
305: return (pwent);
306: }
307:
1.4 deraadt 308: static char *__yplin;
309:
1.15 millert 310: struct passwd *
1.25 maja 311: ypgetpwnam(char *nam, int secure)
1.1 deraadt 312: {
313: static struct passwd pwent;
1.23 deraadt 314: int reason, vallen;
1.1 deraadt 315: char *val;
1.21 fgsch 316:
1.25 maja 317: reason = yp_match(domain,
1.29 deraadt 318: secure ? "master.passwd.byname" : "passwd.byname",
319: nam, strlen(nam), &val, &vallen);
1.23 deraadt 320: switch (reason) {
1.1 deraadt 321: case 0:
322: break;
323: default:
324: return (NULL);
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 */