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