19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1990, 1993, 1994 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 359b50d902SRodney W. Grimes static char sccsid[] = "@(#)edit.c 8.3 (Berkeley) 4/2/94"; 369b50d902SRodney W. Grimes #endif /* not lint */ 379b50d902SRodney W. Grimes 389b50d902SRodney W. Grimes #include <sys/param.h> 399b50d902SRodney W. Grimes #include <sys/stat.h> 409b50d902SRodney W. Grimes 419b50d902SRodney W. Grimes #include <ctype.h> 429b50d902SRodney W. Grimes #include <err.h> 439b50d902SRodney W. Grimes #include <errno.h> 449b50d902SRodney W. Grimes #include <paths.h> 459b50d902SRodney W. Grimes #include <pwd.h> 469b50d902SRodney W. Grimes #include <stdio.h> 479b50d902SRodney W. Grimes #include <stdlib.h> 489b50d902SRodney W. Grimes #include <string.h> 499b50d902SRodney W. Grimes #include <unistd.h> 509b50d902SRodney W. Grimes 519b50d902SRodney W. Grimes #include <pw_scan.h> 529b50d902SRodney W. Grimes #include <pw_util.h> 539b50d902SRodney W. Grimes 549b50d902SRodney W. Grimes #include "chpass.h" 559b50d902SRodney W. Grimes 569b50d902SRodney W. Grimes extern char *tempname; 579b50d902SRodney W. Grimes 589b50d902SRodney W. Grimes void 599b50d902SRodney W. Grimes edit(pw) 609b50d902SRodney W. Grimes struct passwd *pw; 619b50d902SRodney W. Grimes { 629b50d902SRodney W. Grimes struct stat begin, end; 639b50d902SRodney W. Grimes 649b50d902SRodney W. Grimes for (;;) { 659b50d902SRodney W. Grimes if (stat(tempname, &begin)) 669b50d902SRodney W. Grimes pw_error(tempname, 1, 1); 679b50d902SRodney W. Grimes pw_edit(1); 689b50d902SRodney W. Grimes if (stat(tempname, &end)) 699b50d902SRodney W. Grimes pw_error(tempname, 1, 1); 709b50d902SRodney W. Grimes if (begin.st_mtime == end.st_mtime) { 719b50d902SRodney W. Grimes warnx("no changes made"); 729b50d902SRodney W. Grimes pw_error(NULL, 0, 0); 739b50d902SRodney W. Grimes } 749b50d902SRodney W. Grimes if (verify(pw)) 759b50d902SRodney W. Grimes break; 769b50d902SRodney W. Grimes pw_prompt(); 779b50d902SRodney W. Grimes } 789b50d902SRodney W. Grimes } 799b50d902SRodney W. Grimes 809b50d902SRodney W. Grimes /* 819b50d902SRodney W. Grimes * display -- 829b50d902SRodney W. Grimes * print out the file for the user to edit; strange side-effect: 839b50d902SRodney W. Grimes * set conditional flag if the user gets to edit the shell. 849b50d902SRodney W. Grimes */ 859b50d902SRodney W. Grimes void 869b50d902SRodney W. Grimes display(fd, pw) 879b50d902SRodney W. Grimes int fd; 889b50d902SRodney W. Grimes struct passwd *pw; 899b50d902SRodney W. Grimes { 909b50d902SRodney W. Grimes FILE *fp; 919b50d902SRodney W. Grimes char *bp, *p, *ttoa(); 929b50d902SRodney W. Grimes 939b50d902SRodney W. Grimes if (!(fp = fdopen(fd, "w"))) 949b50d902SRodney W. Grimes pw_error(tempname, 1, 1); 959b50d902SRodney W. Grimes 969b50d902SRodney W. Grimes (void)fprintf(fp, 979b50d902SRodney W. Grimes "#Changing user database information for %s.\n", pw->pw_name); 989b50d902SRodney W. Grimes if (!uid) { 999b50d902SRodney W. Grimes (void)fprintf(fp, "Login: %s\n", pw->pw_name); 1009b50d902SRodney W. Grimes (void)fprintf(fp, "Password: %s\n", pw->pw_passwd); 1019b50d902SRodney W. Grimes (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid); 1029b50d902SRodney W. Grimes (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid); 1039b50d902SRodney W. Grimes (void)fprintf(fp, "Change [month day year]: %s\n", 1049b50d902SRodney W. Grimes ttoa(pw->pw_change)); 1059b50d902SRodney W. Grimes (void)fprintf(fp, "Expire [month day year]: %s\n", 1069b50d902SRodney W. Grimes ttoa(pw->pw_expire)); 1079b50d902SRodney W. Grimes (void)fprintf(fp, "Class: %s\n", pw->pw_class); 1089b50d902SRodney W. Grimes (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir); 1099b50d902SRodney W. Grimes (void)fprintf(fp, "Shell: %s\n", 1109b50d902SRodney W. Grimes *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 1119b50d902SRodney W. Grimes } 1129b50d902SRodney W. Grimes /* Only admin can change "restricted" shells. */ 1139b50d902SRodney W. Grimes else if (ok_shell(pw->pw_shell)) 1149b50d902SRodney W. Grimes /* 1159b50d902SRodney W. Grimes * Make shell a restricted field. Ugly with a 1169b50d902SRodney W. Grimes * necklace, but there's not much else to do. 1179b50d902SRodney W. Grimes */ 1189b50d902SRodney W. Grimes (void)fprintf(fp, "Shell: %s\n", 1199b50d902SRodney W. Grimes *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 1209b50d902SRodney W. Grimes else 1219b50d902SRodney W. Grimes list[E_SHELL].restricted = 1; 1229b50d902SRodney W. Grimes bp = pw->pw_gecos; 1239b50d902SRodney W. Grimes p = strsep(&bp, ","); 1249b50d902SRodney W. Grimes (void)fprintf(fp, "Full Name: %s\n", p ? p : ""); 1259b50d902SRodney W. Grimes p = strsep(&bp, ","); 1269b50d902SRodney W. Grimes (void)fprintf(fp, "Location: %s\n", p ? p : ""); 1279b50d902SRodney W. Grimes p = strsep(&bp, ","); 1289b50d902SRodney W. Grimes (void)fprintf(fp, "Office Phone: %s\n", p ? p : ""); 1299b50d902SRodney W. Grimes p = strsep(&bp, ","); 1309b50d902SRodney W. Grimes (void)fprintf(fp, "Home Phone: %s\n", p ? p : ""); 1319b50d902SRodney W. Grimes 1329b50d902SRodney W. Grimes (void)fchown(fd, getuid(), getgid()); 1339b50d902SRodney W. Grimes (void)fclose(fp); 1349b50d902SRodney W. Grimes } 1359b50d902SRodney W. Grimes 1369b50d902SRodney W. Grimes int 1379b50d902SRodney W. Grimes verify(pw) 1389b50d902SRodney W. Grimes struct passwd *pw; 1399b50d902SRodney W. Grimes { 1409b50d902SRodney W. Grimes ENTRY *ep; 1419b50d902SRodney W. Grimes char *p; 1429b50d902SRodney W. Grimes struct stat sb; 1439b50d902SRodney W. Grimes FILE *fp; 1449b50d902SRodney W. Grimes int len; 1459b50d902SRodney W. Grimes char buf[LINE_MAX]; 1469b50d902SRodney W. Grimes 1479b50d902SRodney W. Grimes if (!(fp = fopen(tempname, "r"))) 1489b50d902SRodney W. Grimes pw_error(tempname, 1, 1); 1499b50d902SRodney W. Grimes if (fstat(fileno(fp), &sb)) 1509b50d902SRodney W. Grimes pw_error(tempname, 1, 1); 1519b50d902SRodney W. Grimes if (sb.st_size == 0) { 1529b50d902SRodney W. Grimes warnx("corrupted temporary file"); 1539b50d902SRodney W. Grimes goto bad; 1549b50d902SRodney W. Grimes } 1559b50d902SRodney W. Grimes while (fgets(buf, sizeof(buf), fp)) { 1569b50d902SRodney W. Grimes if (!buf[0] || buf[0] == '#') 1579b50d902SRodney W. Grimes continue; 1589b50d902SRodney W. Grimes if (!(p = strchr(buf, '\n'))) { 1599b50d902SRodney W. Grimes warnx("line too long"); 1609b50d902SRodney W. Grimes goto bad; 1619b50d902SRodney W. Grimes } 1629b50d902SRodney W. Grimes *p = '\0'; 1639b50d902SRodney W. Grimes for (ep = list;; ++ep) { 1649b50d902SRodney W. Grimes if (!ep->prompt) { 1659b50d902SRodney W. Grimes warnx("unrecognized field"); 1669b50d902SRodney W. Grimes goto bad; 1679b50d902SRodney W. Grimes } 1689b50d902SRodney W. Grimes if (!strncasecmp(buf, ep->prompt, ep->len)) { 1699b50d902SRodney W. Grimes if (ep->restricted && uid) { 1709b50d902SRodney W. Grimes warnx( 1719b50d902SRodney W. Grimes "you may not change the %s field", 1729b50d902SRodney W. Grimes ep->prompt); 1739b50d902SRodney W. Grimes goto bad; 1749b50d902SRodney W. Grimes } 1759b50d902SRodney W. Grimes if (!(p = strchr(buf, ':'))) { 1769b50d902SRodney W. Grimes warnx("line corrupted"); 1779b50d902SRodney W. Grimes goto bad; 1789b50d902SRodney W. Grimes } 1799b50d902SRodney W. Grimes while (isspace(*++p)); 1809b50d902SRodney W. Grimes if (ep->except && strpbrk(p, ep->except)) { 1819b50d902SRodney W. Grimes warnx( 1829b50d902SRodney W. Grimes "illegal character in the \"%s\" field", 1839b50d902SRodney W. Grimes ep->prompt); 1849b50d902SRodney W. Grimes goto bad; 1859b50d902SRodney W. Grimes } 1869b50d902SRodney W. Grimes if ((ep->func)(p, pw, ep)) { 1879b50d902SRodney W. Grimes bad: (void)fclose(fp); 1889b50d902SRodney W. Grimes return (0); 1899b50d902SRodney W. Grimes } 1909b50d902SRodney W. Grimes break; 1919b50d902SRodney W. Grimes } 1929b50d902SRodney W. Grimes } 1939b50d902SRodney W. Grimes } 1949b50d902SRodney W. Grimes (void)fclose(fp); 1959b50d902SRodney W. Grimes 1969b50d902SRodney W. Grimes /* Build the gecos field. */ 1979b50d902SRodney W. Grimes len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) + 1989b50d902SRodney W. Grimes strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4; 1999b50d902SRodney W. Grimes if (!(p = malloc(len))) 2009b50d902SRodney W. Grimes err(1, NULL); 2019b50d902SRodney W. Grimes (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save, 2029b50d902SRodney W. Grimes list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save); 2039b50d902SRodney W. Grimes 2049b50d902SRodney W. Grimes if (snprintf(buf, sizeof(buf), 2059b50d902SRodney W. Grimes "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", 2069b50d902SRodney W. Grimes pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, 2079b50d902SRodney W. Grimes pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir, 2089b50d902SRodney W. Grimes pw->pw_shell) >= sizeof(buf)) { 2099b50d902SRodney W. Grimes warnx("entries too long"); 2109b50d902SRodney W. Grimes return (0); 2119b50d902SRodney W. Grimes } 2129b50d902SRodney W. Grimes return (pw_scan(buf, pw)); 2139b50d902SRodney W. Grimes } 214