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" 5536715722SBill Paul #ifdef YP 5636715722SBill Paul #include "pw_yp.h" 5736715722SBill Paul #endif /* YP */ 589b50d902SRodney W. Grimes 599b50d902SRodney W. Grimes extern char *tempname; 609b50d902SRodney W. Grimes 619b50d902SRodney W. Grimes void 629b50d902SRodney W. Grimes edit(pw) 639b50d902SRodney W. Grimes struct passwd *pw; 649b50d902SRodney W. Grimes { 659b50d902SRodney W. Grimes struct stat begin, end; 669b50d902SRodney W. Grimes 679b50d902SRodney W. Grimes for (;;) { 689b50d902SRodney W. Grimes if (stat(tempname, &begin)) 699b50d902SRodney W. Grimes pw_error(tempname, 1, 1); 709b50d902SRodney W. Grimes pw_edit(1); 719b50d902SRodney W. Grimes if (stat(tempname, &end)) 729b50d902SRodney W. Grimes pw_error(tempname, 1, 1); 739b50d902SRodney W. Grimes if (begin.st_mtime == end.st_mtime) { 749b50d902SRodney W. Grimes warnx("no changes made"); 759b50d902SRodney W. Grimes pw_error(NULL, 0, 0); 769b50d902SRodney W. Grimes } 779b50d902SRodney W. Grimes if (verify(pw)) 789b50d902SRodney W. Grimes break; 799b50d902SRodney W. Grimes pw_prompt(); 809b50d902SRodney W. Grimes } 819b50d902SRodney W. Grimes } 829b50d902SRodney W. Grimes 839b50d902SRodney W. Grimes /* 849b50d902SRodney W. Grimes * display -- 859b50d902SRodney W. Grimes * print out the file for the user to edit; strange side-effect: 869b50d902SRodney W. Grimes * set conditional flag if the user gets to edit the shell. 879b50d902SRodney W. Grimes */ 889b50d902SRodney W. Grimes void 899b50d902SRodney W. Grimes display(fd, pw) 909b50d902SRodney W. Grimes int fd; 919b50d902SRodney W. Grimes struct passwd *pw; 929b50d902SRodney W. Grimes { 939b50d902SRodney W. Grimes FILE *fp; 949b50d902SRodney W. Grimes char *bp, *p, *ttoa(); 959b50d902SRodney W. Grimes 969b50d902SRodney W. Grimes if (!(fp = fdopen(fd, "w"))) 979b50d902SRodney W. Grimes pw_error(tempname, 1, 1); 989b50d902SRodney W. Grimes 999b50d902SRodney W. Grimes (void)fprintf(fp, 10036715722SBill Paul #ifdef YP 10136715722SBill Paul "#Changing %s information for %s.\n", _use_yp ? "NIS" : "user database", pw->pw_name); 102c2dfe9feSBill Paul if (!uid && (!_use_yp || suser_override)) { 10336715722SBill Paul #else 10436715722SBill Paul "#Changing user database information for %s.\n", pw->pw_name) 1059b50d902SRodney W. Grimes if (!uid) { 10636715722SBill Paul #endif /* YP */ 1079b50d902SRodney W. Grimes (void)fprintf(fp, "Login: %s\n", pw->pw_name); 1089b50d902SRodney W. Grimes (void)fprintf(fp, "Password: %s\n", pw->pw_passwd); 1099b50d902SRodney W. Grimes (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid); 1109b50d902SRodney W. Grimes (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid); 1119b50d902SRodney W. Grimes (void)fprintf(fp, "Change [month day year]: %s\n", 1129b50d902SRodney W. Grimes ttoa(pw->pw_change)); 1139b50d902SRodney W. Grimes (void)fprintf(fp, "Expire [month day year]: %s\n", 1149b50d902SRodney W. Grimes ttoa(pw->pw_expire)); 1159b50d902SRodney W. Grimes (void)fprintf(fp, "Class: %s\n", pw->pw_class); 1169b50d902SRodney W. Grimes (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir); 1179b50d902SRodney W. Grimes (void)fprintf(fp, "Shell: %s\n", 1189b50d902SRodney W. Grimes *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 1199b50d902SRodney W. Grimes } 1209b50d902SRodney W. Grimes /* Only admin can change "restricted" shells. */ 121c2dfe9feSBill Paul #ifdef 0 1229b50d902SRodney W. Grimes else if (ok_shell(pw->pw_shell)) 1239b50d902SRodney W. Grimes /* 1249b50d902SRodney W. Grimes * Make shell a restricted field. Ugly with a 1259b50d902SRodney W. Grimes * necklace, but there's not much else to do. 1269b50d902SRodney W. Grimes */ 127c2dfe9feSBill Paul #else 128c2dfe9feSBill Paul else if ((!list[E_SHELL].restricted && ok_shell(pw->pw_shell)) || !uid) 129c2dfe9feSBill Paul /* 130c2dfe9feSBill Paul * If change not restrict (table.c) and standard shell 131c2dfe9feSBill Paul * OR if root, then allow editing of shell. 132c2dfe9feSBill Paul */ 133c2dfe9feSBill Paul #endif 1349b50d902SRodney W. Grimes (void)fprintf(fp, "Shell: %s\n", 1359b50d902SRodney W. Grimes *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 1369b50d902SRodney W. Grimes else 1379b50d902SRodney W. Grimes list[E_SHELL].restricted = 1; 1389b50d902SRodney W. Grimes bp = pw->pw_gecos; 139e1731211SJordan K. Hubbard 1409b50d902SRodney W. Grimes p = strsep(&bp, ","); 141e1731211SJordan K. Hubbard if (p) 142e1731211SJordan K. Hubbard list[E_NAME].save = strdup(p); 1439dc5391cSAndrey A. Chernov if (!list[E_NAME].restricted || !uid) 1449b50d902SRodney W. Grimes (void)fprintf(fp, "Full Name: %s\n", p ? p : ""); 145e1731211SJordan K. Hubbard 1469b50d902SRodney W. Grimes p = strsep(&bp, ","); 147e1731211SJordan K. Hubbard if (p) 148e1731211SJordan K. Hubbard list[E_LOCATE].save = strdup(p); 1499dc5391cSAndrey A. Chernov if (!list[E_LOCATE].restricted || !uid) 1509b50d902SRodney W. Grimes (void)fprintf(fp, "Location: %s\n", p ? p : ""); 151e1731211SJordan K. Hubbard 1529b50d902SRodney W. Grimes p = strsep(&bp, ","); 153e1731211SJordan K. Hubbard if (p) 154e1731211SJordan K. Hubbard list[E_BPHONE].save = strdup(p); 1559dc5391cSAndrey A. Chernov if (!list[E_BPHONE].restricted || !uid) 1569b50d902SRodney W. Grimes (void)fprintf(fp, "Office Phone: %s\n", p ? p : ""); 157e1731211SJordan K. Hubbard 1589b50d902SRodney W. Grimes p = strsep(&bp, ","); 159e1731211SJordan K. Hubbard if (p) 160e1731211SJordan K. Hubbard list[E_HPHONE].save = strdup(p); 1619dc5391cSAndrey A. Chernov if (!list[E_HPHONE].restricted || !uid) 1629b50d902SRodney W. Grimes (void)fprintf(fp, "Home Phone: %s\n", p ? p : ""); 1639b50d902SRodney W. Grimes 1649b50d902SRodney W. Grimes (void)fchown(fd, getuid(), getgid()); 1659b50d902SRodney W. Grimes (void)fclose(fp); 1669b50d902SRodney W. Grimes } 1679b50d902SRodney W. Grimes 1689b50d902SRodney W. Grimes int 1699b50d902SRodney W. Grimes verify(pw) 1709b50d902SRodney W. Grimes struct passwd *pw; 1719b50d902SRodney W. Grimes { 1729b50d902SRodney W. Grimes ENTRY *ep; 1739b50d902SRodney W. Grimes char *p; 1749b50d902SRodney W. Grimes struct stat sb; 1759b50d902SRodney W. Grimes FILE *fp; 1769b50d902SRodney W. Grimes int len; 1779b50d902SRodney W. Grimes char buf[LINE_MAX]; 1789b50d902SRodney W. Grimes 1799b50d902SRodney W. Grimes if (!(fp = fopen(tempname, "r"))) 1809b50d902SRodney W. Grimes pw_error(tempname, 1, 1); 1819b50d902SRodney W. Grimes if (fstat(fileno(fp), &sb)) 1829b50d902SRodney W. Grimes pw_error(tempname, 1, 1); 1839b50d902SRodney W. Grimes if (sb.st_size == 0) { 1849b50d902SRodney W. Grimes warnx("corrupted temporary file"); 1859b50d902SRodney W. Grimes goto bad; 1869b50d902SRodney W. Grimes } 1879b50d902SRodney W. Grimes while (fgets(buf, sizeof(buf), fp)) { 1889b50d902SRodney W. Grimes if (!buf[0] || buf[0] == '#') 1899b50d902SRodney W. Grimes continue; 1909b50d902SRodney W. Grimes if (!(p = strchr(buf, '\n'))) { 1919b50d902SRodney W. Grimes warnx("line too long"); 1929b50d902SRodney W. Grimes goto bad; 1939b50d902SRodney W. Grimes } 1949b50d902SRodney W. Grimes *p = '\0'; 1959b50d902SRodney W. Grimes for (ep = list;; ++ep) { 1969b50d902SRodney W. Grimes if (!ep->prompt) { 1979b50d902SRodney W. Grimes warnx("unrecognized field"); 1989b50d902SRodney W. Grimes goto bad; 1999b50d902SRodney W. Grimes } 2009b50d902SRodney W. Grimes if (!strncasecmp(buf, ep->prompt, ep->len)) { 2019b50d902SRodney W. Grimes if (ep->restricted && uid) { 2029b50d902SRodney W. Grimes warnx( 2039b50d902SRodney W. Grimes "you may not change the %s field", 2049b50d902SRodney W. Grimes ep->prompt); 2059b50d902SRodney W. Grimes goto bad; 2069b50d902SRodney W. Grimes } 2079b50d902SRodney W. Grimes if (!(p = strchr(buf, ':'))) { 2089b50d902SRodney W. Grimes warnx("line corrupted"); 2099b50d902SRodney W. Grimes goto bad; 2109b50d902SRodney W. Grimes } 2119b50d902SRodney W. Grimes while (isspace(*++p)); 2129b50d902SRodney W. Grimes if (ep->except && strpbrk(p, ep->except)) { 2139b50d902SRodney W. Grimes warnx( 2149b50d902SRodney W. Grimes "illegal character in the \"%s\" field", 2159b50d902SRodney W. Grimes ep->prompt); 2169b50d902SRodney W. Grimes goto bad; 2179b50d902SRodney W. Grimes } 2189b50d902SRodney W. Grimes if ((ep->func)(p, pw, ep)) { 2199b50d902SRodney W. Grimes bad: (void)fclose(fp); 2209b50d902SRodney W. Grimes return (0); 2219b50d902SRodney W. Grimes } 2229b50d902SRodney W. Grimes break; 2239b50d902SRodney W. Grimes } 2249b50d902SRodney W. Grimes } 2259b50d902SRodney W. Grimes } 2269b50d902SRodney W. Grimes (void)fclose(fp); 2279b50d902SRodney W. Grimes 2289b50d902SRodney W. Grimes /* Build the gecos field. */ 2299b50d902SRodney W. Grimes len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) + 2309b50d902SRodney W. Grimes strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4; 2319b50d902SRodney W. Grimes if (!(p = malloc(len))) 2329b50d902SRodney W. Grimes err(1, NULL); 2339b50d902SRodney W. Grimes (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save, 2349b50d902SRodney W. Grimes list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save); 2359b50d902SRodney W. Grimes 2369b50d902SRodney W. Grimes if (snprintf(buf, sizeof(buf), 2379b50d902SRodney W. Grimes "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", 2389b50d902SRodney W. Grimes pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, 2399b50d902SRodney W. Grimes pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir, 2409b50d902SRodney W. Grimes pw->pw_shell) >= sizeof(buf)) { 2419b50d902SRodney W. Grimes warnx("entries too long"); 2429b50d902SRodney W. Grimes return (0); 2439b50d902SRodney W. Grimes } 2449b50d902SRodney W. Grimes return (pw_scan(buf, pw)); 2459b50d902SRodney W. Grimes } 246