xref: /freebsd/usr.bin/chpass/edit.c (revision f458f48b02dd15d64dc4f654c33030b5702940db)
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  *
33c3aac50fSPeter Wemm  * $FreeBSD$
349b50d902SRodney W. Grimes  */
359b50d902SRodney W. Grimes 
369b50d902SRodney W. Grimes #ifndef lint
37fa146c53SArchie Cobbs static const 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>
46b9efeafcSSheldon Hearn #include <md5.h>
479b50d902SRodney W. Grimes #include <paths.h>
489b50d902SRodney W. Grimes #include <pwd.h>
499b50d902SRodney W. Grimes #include <stdio.h>
509b50d902SRodney W. Grimes #include <stdlib.h>
519b50d902SRodney W. Grimes #include <string.h>
529b50d902SRodney W. Grimes #include <unistd.h>
539b50d902SRodney W. Grimes 
549b50d902SRodney W. Grimes #include <pw_scan.h>
559b50d902SRodney W. Grimes #include <pw_util.h>
569b50d902SRodney W. Grimes 
579b50d902SRodney W. Grimes #include "chpass.h"
5836715722SBill Paul #ifdef YP
5936715722SBill Paul #include "pw_yp.h"
6036715722SBill Paul #endif /* YP */
619b50d902SRodney W. Grimes 
629b50d902SRodney W. Grimes extern char *tempname;
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes void
659b50d902SRodney W. Grimes edit(pw)
669b50d902SRodney W. Grimes 	struct passwd *pw;
679b50d902SRodney W. Grimes {
689b50d902SRodney W. Grimes 	struct stat begin, end;
69b9efeafcSSheldon Hearn 	char *begin_sum, *end_sum;
709b50d902SRodney W. Grimes 
719b50d902SRodney W. Grimes 	for (;;) {
729b50d902SRodney W. Grimes 		if (stat(tempname, &begin))
739b50d902SRodney W. Grimes 			pw_error(tempname, 1, 1);
74b9efeafcSSheldon Hearn 		begin_sum = MD5File(tempname, (char *)NULL);
759b50d902SRodney W. Grimes 		pw_edit(1);
769b50d902SRodney W. Grimes 		if (stat(tempname, &end))
779b50d902SRodney W. Grimes 			pw_error(tempname, 1, 1);
78b9efeafcSSheldon Hearn 		end_sum = MD5File(tempname, (char *)NULL);
79b9efeafcSSheldon Hearn 		if ((begin.st_mtime == end.st_mtime) &&
80b9efeafcSSheldon Hearn 		    (strcmp(begin_sum, end_sum) == 0)) {
819b50d902SRodney W. Grimes 			warnx("no changes made");
829b50d902SRodney W. Grimes 			pw_error(NULL, 0, 0);
839b50d902SRodney W. Grimes 		}
84b9efeafcSSheldon Hearn 		free(begin_sum);
85b9efeafcSSheldon Hearn 		free(end_sum);
869b50d902SRodney W. Grimes 		if (verify(pw))
879b50d902SRodney W. Grimes 			break;
889b50d902SRodney W. Grimes 		pw_prompt();
899b50d902SRodney W. Grimes 	}
909b50d902SRodney W. Grimes }
919b50d902SRodney W. Grimes 
929b50d902SRodney W. Grimes /*
939b50d902SRodney W. Grimes  * display --
949b50d902SRodney W. Grimes  *	print out the file for the user to edit; strange side-effect:
959b50d902SRodney W. Grimes  *	set conditional flag if the user gets to edit the shell.
969b50d902SRodney W. Grimes  */
979b50d902SRodney W. Grimes void
989b50d902SRodney W. Grimes display(fd, pw)
999b50d902SRodney W. Grimes 	int fd;
1009b50d902SRodney W. Grimes 	struct passwd *pw;
1019b50d902SRodney W. Grimes {
1029b50d902SRodney W. Grimes 	FILE *fp;
1039b50d902SRodney W. Grimes 	char *bp, *p, *ttoa();
1049b50d902SRodney W. Grimes 
1059b50d902SRodney W. Grimes 	if (!(fp = fdopen(fd, "w")))
1069b50d902SRodney W. Grimes 		pw_error(tempname, 1, 1);
1079b50d902SRodney W. Grimes 
1089b50d902SRodney W. Grimes 	(void)fprintf(fp,
10936715722SBill Paul #ifdef YP
11036715722SBill Paul 	    "#Changing %s information for %s.\n", _use_yp ? "NIS" : "user database", pw->pw_name);
111c2dfe9feSBill Paul 	if (!uid && (!_use_yp || suser_override)) {
11236715722SBill Paul #else
1138e92f56aSBill Paul 	    "#Changing user database information for %s.\n", pw->pw_name);
1149b50d902SRodney W. Grimes 	if (!uid) {
11536715722SBill Paul #endif /* YP */
1169b50d902SRodney W. Grimes 		(void)fprintf(fp, "Login: %s\n", pw->pw_name);
1179b50d902SRodney W. Grimes 		(void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
118f458f48bSMike Barcroft 		(void)fprintf(fp, "Uid [#]: %lu\n", (unsigned long)pw->pw_uid);
119f458f48bSMike Barcroft 		(void)fprintf(fp, "Gid [# or name]: %lu\n",
120f458f48bSMike Barcroft 		    (unsigned long)pw->pw_gid);
1219b50d902SRodney W. Grimes 		(void)fprintf(fp, "Change [month day year]: %s\n",
1229b50d902SRodney W. Grimes 		    ttoa(pw->pw_change));
1239b50d902SRodney W. Grimes 		(void)fprintf(fp, "Expire [month day year]: %s\n",
1249b50d902SRodney W. Grimes 		    ttoa(pw->pw_expire));
1259b50d902SRodney W. Grimes 		(void)fprintf(fp, "Class: %s\n", pw->pw_class);
1269b50d902SRodney W. Grimes 		(void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
1279b50d902SRodney W. Grimes 		(void)fprintf(fp, "Shell: %s\n",
1289b50d902SRodney W. Grimes 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
1299b50d902SRodney W. Grimes 	}
1309b50d902SRodney W. Grimes 	/* Only admin can change "restricted" shells. */
13196846ff6SWarner Losh #if 0
1329b50d902SRodney W. Grimes 	else if (ok_shell(pw->pw_shell))
1339b50d902SRodney W. Grimes 		/*
1349b50d902SRodney W. Grimes 		 * Make shell a restricted field.  Ugly with a
1359b50d902SRodney W. Grimes 		 * necklace, but there's not much else to do.
1369b50d902SRodney W. Grimes 		 */
137c2dfe9feSBill Paul #else
138c2dfe9feSBill Paul 	else if ((!list[E_SHELL].restricted && ok_shell(pw->pw_shell)) || !uid)
139c2dfe9feSBill Paul 		/*
140c2dfe9feSBill Paul 		 * If change not restrict (table.c) and standard shell
141c2dfe9feSBill Paul 		 *	OR if root, then allow editing of shell.
142c2dfe9feSBill Paul 		 */
143c2dfe9feSBill Paul #endif
1449b50d902SRodney W. Grimes 		(void)fprintf(fp, "Shell: %s\n",
1459b50d902SRodney W. Grimes 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
1469b50d902SRodney W. Grimes 	else
1479b50d902SRodney W. Grimes 	  list[E_SHELL].restricted = 1;
1489b50d902SRodney W. Grimes 	bp = pw->pw_gecos;
149e1731211SJordan K. Hubbard 
1509b50d902SRodney W. Grimes 	p = strsep(&bp, ",");
151a83c5014SWarner Losh 	p = strdup(p ? p : "");
152a83c5014SWarner Losh 	list[E_NAME].save = p;
1539dc5391cSAndrey A. Chernov 	if (!list[E_NAME].restricted || !uid)
154a83c5014SWarner Losh 	  (void)fprintf(fp, "Full Name: %s\n", p);
155e1731211SJordan K. Hubbard 
1569b50d902SRodney W. Grimes         p = strsep(&bp, ",");
157a83c5014SWarner Losh 	p = strdup(p ? p : "");
158a83c5014SWarner Losh 	list[E_LOCATE].save = p;
1599dc5391cSAndrey A. Chernov 	if (!list[E_LOCATE].restricted || !uid)
160a83c5014SWarner Losh 	  (void)fprintf(fp, "Office Location: %s\n", p);
161e1731211SJordan K. Hubbard 
1629b50d902SRodney W. Grimes         p = strsep(&bp, ",");
163a83c5014SWarner Losh 	p = strdup(p ? p : "");
164a83c5014SWarner Losh 	list[E_BPHONE].save = p;
1659dc5391cSAndrey A. Chernov 	if (!list[E_BPHONE].restricted || !uid)
166a83c5014SWarner Losh 	  (void)fprintf(fp, "Office Phone: %s\n", p);
167e1731211SJordan K. Hubbard 
1689b50d902SRodney W. Grimes         p = strsep(&bp, ",");
169a83c5014SWarner Losh 	p = strdup(p ? p : "");
170a83c5014SWarner Losh 	list[E_HPHONE].save = p;
1719dc5391cSAndrey A. Chernov 	if (!list[E_HPHONE].restricted || !uid)
172a83c5014SWarner Losh 	  (void)fprintf(fp, "Home Phone: %s\n", p);
1739b50d902SRodney W. Grimes 
174a83c5014SWarner Losh 	bp = strdup(bp ? bp : "");
175a83c5014SWarner Losh 	list[E_OTHER].save = bp;
17619ae8d1eSPeter Hawkins 	if (!list[E_OTHER].restricted || !uid)
177a83c5014SWarner Losh 	  (void)fprintf(fp, "Other information: %s\n", bp);
17819ae8d1eSPeter Hawkins 
1799b50d902SRodney W. Grimes 	(void)fchown(fd, getuid(), getgid());
1809b50d902SRodney W. Grimes 	(void)fclose(fp);
1819b50d902SRodney W. Grimes }
1829b50d902SRodney W. Grimes 
1839b50d902SRodney W. Grimes int
1849b50d902SRodney W. Grimes verify(pw)
1859b50d902SRodney W. Grimes 	struct passwd *pw;
1869b50d902SRodney W. Grimes {
1879b50d902SRodney W. Grimes 	ENTRY *ep;
1889b50d902SRodney W. Grimes 	char *p;
1899b50d902SRodney W. Grimes 	struct stat sb;
1909b50d902SRodney W. Grimes 	FILE *fp;
1919fc081a4SPhilippe Charnier 	int len, line;
1920587e3a4SPeter Wemm 	static char buf[LINE_MAX];
1939b50d902SRodney W. Grimes 
1949b50d902SRodney W. Grimes 	if (!(fp = fopen(tempname, "r")))
1959b50d902SRodney W. Grimes 		pw_error(tempname, 1, 1);
1969b50d902SRodney W. Grimes 	if (fstat(fileno(fp), &sb))
1979b50d902SRodney W. Grimes 		pw_error(tempname, 1, 1);
1989b50d902SRodney W. Grimes 	if (sb.st_size == 0) {
1999b50d902SRodney W. Grimes 		warnx("corrupted temporary file");
2009b50d902SRodney W. Grimes 		goto bad;
2019b50d902SRodney W. Grimes 	}
2029fc081a4SPhilippe Charnier 	line = 0;
2039b50d902SRodney W. Grimes 	while (fgets(buf, sizeof(buf), fp)) {
2049fc081a4SPhilippe Charnier 		line++;
2059b50d902SRodney W. Grimes 		if (!buf[0] || buf[0] == '#')
2069b50d902SRodney W. Grimes 			continue;
2079b50d902SRodney W. Grimes 		if (!(p = strchr(buf, '\n'))) {
2089fc081a4SPhilippe Charnier 			warnx("line %d too long", line);
2099b50d902SRodney W. Grimes 			goto bad;
2109b50d902SRodney W. Grimes 		}
2119b50d902SRodney W. Grimes 		*p = '\0';
2129b50d902SRodney W. Grimes 		for (ep = list;; ++ep) {
2139b50d902SRodney W. Grimes 			if (!ep->prompt) {
2149fc081a4SPhilippe Charnier 				warnx("unrecognized field on line %d", line);
2159b50d902SRodney W. Grimes 				goto bad;
2169b50d902SRodney W. Grimes 			}
2179b50d902SRodney W. Grimes 			if (!strncasecmp(buf, ep->prompt, ep->len)) {
2189b50d902SRodney W. Grimes 				if (ep->restricted && uid) {
2199b50d902SRodney W. Grimes 					warnx(
2209b50d902SRodney W. Grimes 					    "you may not change the %s field",
2219b50d902SRodney W. Grimes 						ep->prompt);
2229b50d902SRodney W. Grimes 					goto bad;
2239b50d902SRodney W. Grimes 				}
2249b50d902SRodney W. Grimes 				if (!(p = strchr(buf, ':'))) {
2259fc081a4SPhilippe Charnier 					warnx("line %d corrupted", line);
2269b50d902SRodney W. Grimes 					goto bad;
2279b50d902SRodney W. Grimes 				}
2289b50d902SRodney W. Grimes 				while (isspace(*++p));
2299b50d902SRodney W. Grimes 				if (ep->except && strpbrk(p, ep->except)) {
2309b50d902SRodney W. Grimes 					warnx(
2319b50d902SRodney W. Grimes 				   "illegal character in the \"%s\" field",
2329b50d902SRodney W. Grimes 					    ep->prompt);
2339b50d902SRodney W. Grimes 					goto bad;
2349b50d902SRodney W. Grimes 				}
2359b50d902SRodney W. Grimes 				if ((ep->func)(p, pw, ep)) {
2369b50d902SRodney W. Grimes bad:					(void)fclose(fp);
2379b50d902SRodney W. Grimes 					return (0);
2389b50d902SRodney W. Grimes 				}
2399b50d902SRodney W. Grimes 				break;
2409b50d902SRodney W. Grimes 			}
2419b50d902SRodney W. Grimes 		}
2429b50d902SRodney W. Grimes 	}
2439b50d902SRodney W. Grimes 	(void)fclose(fp);
2449b50d902SRodney W. Grimes 
2459b50d902SRodney W. Grimes 	/* Build the gecos field. */
2469b50d902SRodney W. Grimes 	len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
24719ae8d1eSPeter Hawkins 	    strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) +
248a83c5014SWarner Losh 	    strlen(list[E_OTHER].save) + 5;
2499b50d902SRodney W. Grimes 	if (!(p = malloc(len)))
2509b50d902SRodney W. Grimes 		err(1, NULL);
25119ae8d1eSPeter Hawkins 	(void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s,%s", list[E_NAME].save,
25219ae8d1eSPeter Hawkins 	    list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save,
25319ae8d1eSPeter Hawkins 	    list[E_OTHER].save);
2549b50d902SRodney W. Grimes 
255366982a5SPeter Wemm 	while ((len = strlen(pw->pw_gecos)) && pw->pw_gecos[len - 1] == ',')
256366982a5SPeter Wemm 		pw->pw_gecos[len - 1] = '\0';
257366982a5SPeter Wemm 
2589b50d902SRodney W. Grimes 	if (snprintf(buf, sizeof(buf),
259f458f48bSMike Barcroft 	    "%s:%s:%lu:%lu:%s:%ld:%ld:%s:%s:%s",
260f458f48bSMike Barcroft 	    pw->pw_name, pw->pw_passwd, (unsigned long)pw->pw_uid,
261f458f48bSMike Barcroft 	    (unsigned long)pw->pw_gid, pw->pw_class, (long)pw->pw_change,
262f458f48bSMike Barcroft 	    (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
2639b50d902SRodney W. Grimes 	    pw->pw_shell) >= sizeof(buf)) {
2649b50d902SRodney W. Grimes 		warnx("entries too long");
2659fc081a4SPhilippe Charnier 		free(p);
2669b50d902SRodney W. Grimes 		return (0);
2679b50d902SRodney W. Grimes 	}
2689fc081a4SPhilippe Charnier 	free(p);
269248aee62SJacques Vidrine 	return (__pw_scan(buf, pw, _PWSCAN_WARN|_PWSCAN_MASTER));
2709b50d902SRodney W. Grimes }
271