xref: /minix3/usr.bin/chpass/edit.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: edit.c,v 1.21 2011/08/31 16:24:57 plunky Exp $	*/
25c007436SBen Gras 
35c007436SBen Gras /*-
45c007436SBen Gras  * Copyright (c) 1990, 1993, 1994
55c007436SBen Gras  *	The Regents of the University of California.  All rights reserved.
65c007436SBen Gras  *
75c007436SBen Gras  * Redistribution and use in source and binary forms, with or without
85c007436SBen Gras  * modification, are permitted provided that the following conditions
95c007436SBen Gras  * are met:
105c007436SBen Gras  * 1. Redistributions of source code must retain the above copyright
115c007436SBen Gras  *    notice, this list of conditions and the following disclaimer.
125c007436SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
135c007436SBen Gras  *    notice, this list of conditions and the following disclaimer in the
145c007436SBen Gras  *    documentation and/or other materials provided with the distribution.
155c007436SBen Gras  * 3. Neither the name of the University nor the names of its contributors
165c007436SBen Gras  *    may be used to endorse or promote products derived from this software
175c007436SBen Gras  *    without specific prior written permission.
185c007436SBen Gras  *
195c007436SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205c007436SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215c007436SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225c007436SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235c007436SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245c007436SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255c007436SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265c007436SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275c007436SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285c007436SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295c007436SBen Gras  * SUCH DAMAGE.
305c007436SBen Gras  */
315c007436SBen Gras 
325c007436SBen Gras #include <sys/cdefs.h>
335c007436SBen Gras #ifndef lint
345c007436SBen Gras #if 0
355c007436SBen Gras static char sccsid[] = "@(#)edit.c	8.3 (Berkeley) 4/2/94";
365c007436SBen Gras #else
37*84d9c625SLionel Sambuc __RCSID("$NetBSD: edit.c,v 1.21 2011/08/31 16:24:57 plunky Exp $");
385c007436SBen Gras #endif
395c007436SBen Gras #endif /* not lint */
405c007436SBen Gras 
415c007436SBen Gras #include <sys/param.h>
425c007436SBen Gras #include <sys/stat.h>
435c007436SBen Gras 
445c007436SBen Gras #include <ctype.h>
455c007436SBen Gras #include <err.h>
465c007436SBen Gras #include <errno.h>
475c007436SBen Gras #include <paths.h>
485c007436SBen Gras #include <pwd.h>
495c007436SBen Gras #include <stdio.h>
505c007436SBen Gras #include <stdlib.h>
515c007436SBen Gras #include <string.h>
525c007436SBen Gras #include <unistd.h>
535c007436SBen Gras #include <fcntl.h>
545c007436SBen Gras #include <util.h>
555c007436SBen Gras 
565c007436SBen Gras #include "chpass.h"
575c007436SBen Gras 
585c007436SBen Gras void
edit(char * tempname,struct passwd * pw)595c007436SBen Gras edit(char *tempname, struct passwd *pw)
605c007436SBen Gras {
615c007436SBen Gras 	struct stat begin, end;
625c007436SBen Gras 
635c007436SBen Gras 	for (;;) {
645c007436SBen Gras 		if (stat(tempname, &begin))
655c007436SBen Gras 			(*Pw_error)(tempname, 1, 1);
665c007436SBen Gras 		pw_edit(1, tempname);
675c007436SBen Gras 		if (stat(tempname, &end))
685c007436SBen Gras 			(*Pw_error)(tempname, 1, 1);
695c007436SBen Gras 		if (begin.st_mtime == end.st_mtime) {
705c007436SBen Gras 			warnx("no changes made");
715c007436SBen Gras 			(*Pw_error)(NULL, 0, 0);
725c007436SBen Gras 		}
735c007436SBen Gras 		if (verify(tempname, pw))
745c007436SBen Gras 			break;
755c007436SBen Gras #ifdef YP
765c007436SBen Gras 		if (use_yp)
775c007436SBen Gras 			yppw_prompt();
785c007436SBen Gras 		else
795c007436SBen Gras #endif
805c007436SBen Gras 			pw_prompt();
815c007436SBen Gras 	}
825c007436SBen Gras }
835c007436SBen Gras 
845c007436SBen Gras /*
855c007436SBen Gras  * display --
865c007436SBen Gras  *	print out the file for the user to edit; strange side-effect:
875c007436SBen Gras  *	set conditional flag if the user gets to edit the shell.
885c007436SBen Gras  */
895c007436SBen Gras void
display(char * tempname,int fd,struct passwd * pw)905c007436SBen Gras display(char *tempname, int fd, struct passwd *pw)
915c007436SBen Gras {
925c007436SBen Gras 	FILE *fp;
935c007436SBen Gras 	char *bp, *p;
945c007436SBen Gras 	char chgstr[256], expstr[256];
955c007436SBen Gras 
965c007436SBen Gras 	if (!(fp = fdopen(fd, "w")))
975c007436SBen Gras 		(*Pw_error)(tempname, 1, 1);
985c007436SBen Gras 
995c007436SBen Gras 	(void)fprintf(fp,
1005c007436SBen Gras 	    "#Changing user %sdatabase information for %s.\n",
1015c007436SBen Gras 	    use_yp ? "YP " : "", pw->pw_name);
1025c007436SBen Gras 	if (!uid) {
1035c007436SBen Gras 		(void)fprintf(fp, "Login: %s\n", pw->pw_name);
1045c007436SBen Gras 		(void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
1055c007436SBen Gras 		(void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
1065c007436SBen Gras 		(void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
1075c007436SBen Gras 		(void)fprintf(fp, "Change [month day year]: %s\n",
1085c007436SBen Gras 		    ttoa(chgstr, sizeof chgstr, pw->pw_change));
1095c007436SBen Gras 		(void)fprintf(fp, "Expire [month day year]: %s\n",
1105c007436SBen Gras 		    ttoa(expstr, sizeof expstr, pw->pw_expire));
1115c007436SBen Gras 		(void)fprintf(fp, "Class: %s\n", pw->pw_class);
1125c007436SBen Gras 		(void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
1135c007436SBen Gras 		(void)fprintf(fp, "Shell: %s\n",
1145c007436SBen Gras 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
1155c007436SBen Gras 	}
1165c007436SBen Gras 	/* Only admin can change "restricted" shells. */
1175c007436SBen Gras 	else if (ok_shell(pw->pw_shell))
1185c007436SBen Gras 		/*
1195c007436SBen Gras 		 * Make shell a restricted field.  Ugly with a
1205c007436SBen Gras 		 * necklace, but there's not much else to do.
1215c007436SBen Gras 		 */
1225c007436SBen Gras 		(void)fprintf(fp, "Shell: %s\n",
1235c007436SBen Gras 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
1245c007436SBen Gras 	else
1255c007436SBen Gras 		list[E_SHELL].restricted = 1;
1265c007436SBen Gras 	bp = strdup(pw->pw_gecos);
1275c007436SBen Gras 	if (!bp) {
1285c007436SBen Gras 		err(1, "strdup");
1295c007436SBen Gras 		/*NOTREACHED*/
1305c007436SBen Gras 	}
1315c007436SBen Gras 	p = strsep(&bp, ",");
1325c007436SBen Gras 	(void)fprintf(fp, "Full Name: %s\n", p ? p : "");
1335c007436SBen Gras 	p = strsep(&bp, ",");
1345c007436SBen Gras 	(void)fprintf(fp, "Location: %s\n", p ? p : "");
1355c007436SBen Gras 	p = strsep(&bp, ",");
1365c007436SBen Gras 	(void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
1375c007436SBen Gras 	p = strsep(&bp, ",");
1385c007436SBen Gras 	(void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
1395c007436SBen Gras 
1405c007436SBen Gras 	(void)fchown(fd, getuid(), getgid());
1415c007436SBen Gras 	(void)fclose(fp);
1425c007436SBen Gras }
1435c007436SBen Gras 
1445c007436SBen Gras int
verify(char * tempname,struct passwd * pw)1455c007436SBen Gras verify(char *tempname, struct passwd *pw)
1465c007436SBen Gras {
1475c007436SBen Gras 	ENTRY *ep;
1485c007436SBen Gras 	char *p;
1495c007436SBen Gras 	struct stat sb;
1505c007436SBen Gras 	FILE *fp = NULL;
1515c007436SBen Gras 	int len, fd;
1525c007436SBen Gras 	static char buf[LINE_MAX];
1535c007436SBen Gras 
1545c007436SBen Gras 	if ((fd = open(tempname, O_RDONLY|O_NOFOLLOW)) == -1 ||
1555c007436SBen Gras 	    (fp = fdopen(fd, "r")) == NULL)
1565c007436SBen Gras 		(*Pw_error)(tempname, 1, 1);
1575c007436SBen Gras 	if (fstat(fd, &sb))
1585c007436SBen Gras 		(*Pw_error)(tempname, 1, 1);
1595c007436SBen Gras 	if (sb.st_size == 0 || sb.st_nlink != 1) {
1605c007436SBen Gras 		warnx("corrupted temporary file");
1615c007436SBen Gras 		goto bad;
1625c007436SBen Gras 	}
1635c007436SBen Gras 	while (fgets(buf, sizeof(buf), fp)) {
1645c007436SBen Gras 		if (!buf[0] || buf[0] == '#')
1655c007436SBen Gras 			continue;
1665c007436SBen Gras 		if (!(p = strchr(buf, '\n'))) {
1675c007436SBen Gras 			warnx("line too long");
1685c007436SBen Gras 			goto bad;
1695c007436SBen Gras 		}
1705c007436SBen Gras 		*p = '\0';
1715c007436SBen Gras 		for (ep = list;; ++ep) {
1725c007436SBen Gras 			if (!ep->prompt) {
1735c007436SBen Gras 				warnx("unrecognized field");
1745c007436SBen Gras 				goto bad;
1755c007436SBen Gras 			}
1765c007436SBen Gras 			if (!strncasecmp(buf, ep->prompt, ep->len)) {
1775c007436SBen Gras 				if (ep->restricted && uid) {
1785c007436SBen Gras 					warnx(
1795c007436SBen Gras 					    "you may not change the %s field",
1805c007436SBen Gras 						ep->prompt);
1815c007436SBen Gras 					goto bad;
1825c007436SBen Gras 				}
1835c007436SBen Gras 				if (!(p = strchr(buf, ':'))) {
1845c007436SBen Gras 					warnx("line corrupted");
1855c007436SBen Gras 					goto bad;
1865c007436SBen Gras 				}
1875c007436SBen Gras 				while (isspace((unsigned char)*++p));
1885c007436SBen Gras 				if (ep->except && strpbrk(p, ep->except)) {
1895c007436SBen Gras 					warnx(
1905c007436SBen Gras 				   "illegal character in the \"%s\" field",
1915c007436SBen Gras 					    ep->prompt);
1925c007436SBen Gras 					goto bad;
1935c007436SBen Gras 				}
1945c007436SBen Gras 				if ((ep->func)(p, pw, ep)) {
1955c007436SBen Gras bad:					(void)fclose(fp);
1965c007436SBen Gras 					return (0);
1975c007436SBen Gras 				}
1985c007436SBen Gras 				break;
1995c007436SBen Gras 			}
2005c007436SBen Gras 		}
2015c007436SBen Gras 	}
2025c007436SBen Gras 	(void)fclose(fp);
2035c007436SBen Gras 
2045c007436SBen Gras 	/* Build the gecos field. */
2055c007436SBen Gras 	len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
2065c007436SBen Gras 	    strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
2075c007436SBen Gras 	if (!(p = malloc(len)))
2085c007436SBen Gras 		err(1, "malloc");
2095c007436SBen Gras 	(void)snprintf(p, len, "%s,%s,%s,%s", list[E_NAME].save,
2105c007436SBen Gras 	    list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
2115c007436SBen Gras 	pw->pw_gecos = p;
2125c007436SBen Gras 
2135c007436SBen Gras 	if (snprintf(buf, sizeof(buf),
2145c007436SBen Gras 	    "%s:%s:%d:%d:%s:%lu:%lu:%s:%s:%s",
2155c007436SBen Gras 	    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
2165c007436SBen Gras 	    (u_long)pw->pw_change, (u_long)pw->pw_expire, pw->pw_gecos,
2175c007436SBen Gras 	    pw->pw_dir, pw->pw_shell) >= (int)sizeof(buf)) {
2185c007436SBen Gras 		warnx("entries too long");
2195c007436SBen Gras 		return (0);
2205c007436SBen Gras 	}
221*84d9c625SLionel Sambuc 	return (pw_scan(buf, pw, NULL));
2225c007436SBen Gras }
223