xref: /freebsd/usr.bin/chpass/edit.c (revision 9fc081a49e603c34891caeb53d77373458150ad9)
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.
329fc081a4SPhilippe Charnier  *
339fc081a4SPhilippe Charnier  *	$Id$
349b50d902SRodney W. Grimes  */
359b50d902SRodney W. Grimes 
369b50d902SRodney W. Grimes #ifndef lint
379b50d902SRodney W. Grimes static char sccsid[] = "@(#)edit.c	8.3 (Berkeley) 4/2/94";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #include <sys/param.h>
419b50d902SRodney W. Grimes #include <sys/stat.h>
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #include <ctype.h>
449b50d902SRodney W. Grimes #include <err.h>
459b50d902SRodney W. Grimes #include <errno.h>
469b50d902SRodney W. Grimes #include <paths.h>
479b50d902SRodney W. Grimes #include <pwd.h>
489b50d902SRodney W. Grimes #include <stdio.h>
499b50d902SRodney W. Grimes #include <stdlib.h>
509b50d902SRodney W. Grimes #include <string.h>
519b50d902SRodney W. Grimes #include <unistd.h>
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes #include <pw_scan.h>
549b50d902SRodney W. Grimes #include <pw_util.h>
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes #include "chpass.h"
5736715722SBill Paul #ifdef YP
5836715722SBill Paul #include "pw_yp.h"
5936715722SBill Paul #endif /* YP */
609b50d902SRodney W. Grimes 
619b50d902SRodney W. Grimes extern char *tempname;
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes void
649b50d902SRodney W. Grimes edit(pw)
659b50d902SRodney W. Grimes 	struct passwd *pw;
669b50d902SRodney W. Grimes {
679b50d902SRodney W. Grimes 	struct stat begin, end;
689b50d902SRodney W. Grimes 
699b50d902SRodney W. Grimes 	for (;;) {
709b50d902SRodney W. Grimes 		if (stat(tempname, &begin))
719b50d902SRodney W. Grimes 			pw_error(tempname, 1, 1);
729b50d902SRodney W. Grimes 		pw_edit(1);
739b50d902SRodney W. Grimes 		if (stat(tempname, &end))
749b50d902SRodney W. Grimes 			pw_error(tempname, 1, 1);
759b50d902SRodney W. Grimes 		if (begin.st_mtime == end.st_mtime) {
769b50d902SRodney W. Grimes 			warnx("no changes made");
779b50d902SRodney W. Grimes 			pw_error(NULL, 0, 0);
789b50d902SRodney W. Grimes 		}
799b50d902SRodney W. Grimes 		if (verify(pw))
809b50d902SRodney W. Grimes 			break;
819b50d902SRodney W. Grimes 		pw_prompt();
829b50d902SRodney W. Grimes 	}
839b50d902SRodney W. Grimes }
849b50d902SRodney W. Grimes 
859b50d902SRodney W. Grimes /*
869b50d902SRodney W. Grimes  * display --
879b50d902SRodney W. Grimes  *	print out the file for the user to edit; strange side-effect:
889b50d902SRodney W. Grimes  *	set conditional flag if the user gets to edit the shell.
899b50d902SRodney W. Grimes  */
909b50d902SRodney W. Grimes void
919b50d902SRodney W. Grimes display(fd, pw)
929b50d902SRodney W. Grimes 	int fd;
939b50d902SRodney W. Grimes 	struct passwd *pw;
949b50d902SRodney W. Grimes {
959b50d902SRodney W. Grimes 	FILE *fp;
969b50d902SRodney W. Grimes 	char *bp, *p, *ttoa();
979b50d902SRodney W. Grimes 
989b50d902SRodney W. Grimes 	if (!(fp = fdopen(fd, "w")))
999b50d902SRodney W. Grimes 		pw_error(tempname, 1, 1);
1009b50d902SRodney W. Grimes 
1019b50d902SRodney W. Grimes 	(void)fprintf(fp,
10236715722SBill Paul #ifdef YP
10336715722SBill Paul 	    "#Changing %s information for %s.\n", _use_yp ? "NIS" : "user database", pw->pw_name);
104c2dfe9feSBill Paul 	if (!uid && (!_use_yp || suser_override)) {
10536715722SBill Paul #else
1068e92f56aSBill Paul 	    "#Changing user database information for %s.\n", pw->pw_name);
1079b50d902SRodney W. Grimes 	if (!uid) {
10836715722SBill Paul #endif /* YP */
1099b50d902SRodney W. Grimes 		(void)fprintf(fp, "Login: %s\n", pw->pw_name);
1109b50d902SRodney W. Grimes 		(void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
1119b50d902SRodney W. Grimes 		(void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
1129b50d902SRodney W. Grimes 		(void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
1139b50d902SRodney W. Grimes 		(void)fprintf(fp, "Change [month day year]: %s\n",
1149b50d902SRodney W. Grimes 		    ttoa(pw->pw_change));
1159b50d902SRodney W. Grimes 		(void)fprintf(fp, "Expire [month day year]: %s\n",
1169b50d902SRodney W. Grimes 		    ttoa(pw->pw_expire));
1179b50d902SRodney W. Grimes 		(void)fprintf(fp, "Class: %s\n", pw->pw_class);
1189b50d902SRodney W. Grimes 		(void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
1199b50d902SRodney W. Grimes 		(void)fprintf(fp, "Shell: %s\n",
1209b50d902SRodney W. Grimes 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
1219b50d902SRodney W. Grimes 	}
1229b50d902SRodney W. Grimes 	/* Only admin can change "restricted" shells. */
123c2dfe9feSBill Paul #ifdef 0
1249b50d902SRodney W. Grimes 	else if (ok_shell(pw->pw_shell))
1259b50d902SRodney W. Grimes 		/*
1269b50d902SRodney W. Grimes 		 * Make shell a restricted field.  Ugly with a
1279b50d902SRodney W. Grimes 		 * necklace, but there's not much else to do.
1289b50d902SRodney W. Grimes 		 */
129c2dfe9feSBill Paul #else
130c2dfe9feSBill Paul 	else if ((!list[E_SHELL].restricted && ok_shell(pw->pw_shell)) || !uid)
131c2dfe9feSBill Paul 		/*
132c2dfe9feSBill Paul 		 * If change not restrict (table.c) and standard shell
133c2dfe9feSBill Paul 		 *	OR if root, then allow editing of shell.
134c2dfe9feSBill Paul 		 */
135c2dfe9feSBill Paul #endif
1369b50d902SRodney W. Grimes 		(void)fprintf(fp, "Shell: %s\n",
1379b50d902SRodney W. Grimes 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
1389b50d902SRodney W. Grimes 	else
1399b50d902SRodney W. Grimes 	  list[E_SHELL].restricted = 1;
1409b50d902SRodney W. Grimes 	bp = pw->pw_gecos;
141e1731211SJordan K. Hubbard 
1429b50d902SRodney W. Grimes 	p = strsep(&bp, ",");
143e1731211SJordan K. Hubbard 	if (p)
144e1731211SJordan K. Hubbard 	  list[E_NAME].save = strdup(p);
1459dc5391cSAndrey A. Chernov 	if (!list[E_NAME].restricted || !uid)
1469b50d902SRodney W. Grimes 	  (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
147e1731211SJordan K. Hubbard 
1489b50d902SRodney W. Grimes         p = strsep(&bp, ",");
149e1731211SJordan K. Hubbard 	if (p)
150e1731211SJordan K. Hubbard 	  list[E_LOCATE].save = strdup(p);
1519dc5391cSAndrey A. Chernov 	if (!list[E_LOCATE].restricted || !uid)
1529b50d902SRodney W. Grimes 	  (void)fprintf(fp, "Location: %s\n", p ? p : "");
153e1731211SJordan K. Hubbard 
1549b50d902SRodney W. Grimes         p = strsep(&bp, ",");
155e1731211SJordan K. Hubbard 	if (p)
156e1731211SJordan K. Hubbard 	  list[E_BPHONE].save = strdup(p);
1579dc5391cSAndrey A. Chernov 	if (!list[E_BPHONE].restricted || !uid)
1589b50d902SRodney W. Grimes 	  (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
159e1731211SJordan K. Hubbard 
1609b50d902SRodney W. Grimes         p = strsep(&bp, ",");
161e1731211SJordan K. Hubbard 	if (p)
162e1731211SJordan K. Hubbard 	  list[E_HPHONE].save = strdup(p);
1639dc5391cSAndrey A. Chernov 	if (!list[E_HPHONE].restricted || !uid)
1649b50d902SRodney W. Grimes 	  (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
1659b50d902SRodney W. Grimes 
1669b50d902SRodney W. Grimes 	(void)fchown(fd, getuid(), getgid());
1679b50d902SRodney W. Grimes 	(void)fclose(fp);
1689b50d902SRodney W. Grimes }
1699b50d902SRodney W. Grimes 
1709b50d902SRodney W. Grimes int
1719b50d902SRodney W. Grimes verify(pw)
1729b50d902SRodney W. Grimes 	struct passwd *pw;
1739b50d902SRodney W. Grimes {
1749b50d902SRodney W. Grimes 	ENTRY *ep;
1759b50d902SRodney W. Grimes 	char *p;
1769b50d902SRodney W. Grimes 	struct stat sb;
1779b50d902SRodney W. Grimes 	FILE *fp;
1789fc081a4SPhilippe Charnier 	int len, line;
1790587e3a4SPeter Wemm 	static char buf[LINE_MAX];
1809b50d902SRodney W. Grimes 
1819b50d902SRodney W. Grimes 	if (!(fp = fopen(tempname, "r")))
1829b50d902SRodney W. Grimes 		pw_error(tempname, 1, 1);
1839b50d902SRodney W. Grimes 	if (fstat(fileno(fp), &sb))
1849b50d902SRodney W. Grimes 		pw_error(tempname, 1, 1);
1859b50d902SRodney W. Grimes 	if (sb.st_size == 0) {
1869b50d902SRodney W. Grimes 		warnx("corrupted temporary file");
1879b50d902SRodney W. Grimes 		goto bad;
1889b50d902SRodney W. Grimes 	}
1899fc081a4SPhilippe Charnier 	line = 0;
1909b50d902SRodney W. Grimes 	while (fgets(buf, sizeof(buf), fp)) {
1919fc081a4SPhilippe Charnier 		line++;
1929b50d902SRodney W. Grimes 		if (!buf[0] || buf[0] == '#')
1939b50d902SRodney W. Grimes 			continue;
1949b50d902SRodney W. Grimes 		if (!(p = strchr(buf, '\n'))) {
1959fc081a4SPhilippe Charnier 			warnx("line %d too long", line);
1969b50d902SRodney W. Grimes 			goto bad;
1979b50d902SRodney W. Grimes 		}
1989b50d902SRodney W. Grimes 		*p = '\0';
1999b50d902SRodney W. Grimes 		for (ep = list;; ++ep) {
2009b50d902SRodney W. Grimes 			if (!ep->prompt) {
2019fc081a4SPhilippe Charnier 				warnx("unrecognized field on line %d", line);
2029b50d902SRodney W. Grimes 				goto bad;
2039b50d902SRodney W. Grimes 			}
2049b50d902SRodney W. Grimes 			if (!strncasecmp(buf, ep->prompt, ep->len)) {
2059b50d902SRodney W. Grimes 				if (ep->restricted && uid) {
2069b50d902SRodney W. Grimes 					warnx(
2079b50d902SRodney W. Grimes 					    "you may not change the %s field",
2089b50d902SRodney W. Grimes 						ep->prompt);
2099b50d902SRodney W. Grimes 					goto bad;
2109b50d902SRodney W. Grimes 				}
2119b50d902SRodney W. Grimes 				if (!(p = strchr(buf, ':'))) {
2129fc081a4SPhilippe Charnier 					warnx("line %d corrupted", line);
2139b50d902SRodney W. Grimes 					goto bad;
2149b50d902SRodney W. Grimes 				}
2159b50d902SRodney W. Grimes 				while (isspace(*++p));
2169b50d902SRodney W. Grimes 				if (ep->except && strpbrk(p, ep->except)) {
2179b50d902SRodney W. Grimes 					warnx(
2189b50d902SRodney W. Grimes 				   "illegal character in the \"%s\" field",
2199b50d902SRodney W. Grimes 					    ep->prompt);
2209b50d902SRodney W. Grimes 					goto bad;
2219b50d902SRodney W. Grimes 				}
2229b50d902SRodney W. Grimes 				if ((ep->func)(p, pw, ep)) {
2239b50d902SRodney W. Grimes bad:					(void)fclose(fp);
2249b50d902SRodney W. Grimes 					return (0);
2259b50d902SRodney W. Grimes 				}
2269b50d902SRodney W. Grimes 				break;
2279b50d902SRodney W. Grimes 			}
2289b50d902SRodney W. Grimes 		}
2299b50d902SRodney W. Grimes 	}
2309b50d902SRodney W. Grimes 	(void)fclose(fp);
2319b50d902SRodney W. Grimes 
2329b50d902SRodney W. Grimes 	/* Build the gecos field. */
2339b50d902SRodney W. Grimes 	len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
2349b50d902SRodney W. Grimes 	    strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
2359b50d902SRodney W. Grimes 	if (!(p = malloc(len)))
2369b50d902SRodney W. Grimes 		err(1, NULL);
2379b50d902SRodney W. Grimes 	(void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
2389b50d902SRodney W. Grimes 	    list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
2399b50d902SRodney W. Grimes 
240366982a5SPeter Wemm 	while ((len = strlen(pw->pw_gecos)) && pw->pw_gecos[len - 1] == ',')
241366982a5SPeter Wemm 		pw->pw_gecos[len - 1] = '\0';
242366982a5SPeter Wemm 
2439b50d902SRodney W. Grimes 	if (snprintf(buf, sizeof(buf),
2449b50d902SRodney W. Grimes 	    "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
2459b50d902SRodney W. Grimes 	    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
2469b50d902SRodney W. Grimes 	    pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
2479b50d902SRodney W. Grimes 	    pw->pw_shell) >= sizeof(buf)) {
2489b50d902SRodney W. Grimes 		warnx("entries too long");
2499fc081a4SPhilippe Charnier 		free(p);
2509b50d902SRodney W. Grimes 		return (0);
2519b50d902SRodney W. Grimes 	}
2529fc081a4SPhilippe Charnier 	free(p);
2539b50d902SRodney W. Grimes 	return (pw_scan(buf, pw));
2549b50d902SRodney W. Grimes }
255