xref: /openbsd/usr.bin/chpass/edit.c (revision 79b54589)
1*79b54589Sguenther /*	$OpenBSD: edit.c,v 1.36 2023/08/11 04:45:05 guenther Exp $	*/
24b826ba8Sderaadt /*	$NetBSD: edit.c,v 1.6 1996/05/15 21:50:45 jtc Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*-
5df930be7Sderaadt  * Copyright (c) 1990, 1993, 1994
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
16f75387cbSmillert  * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
18df930be7Sderaadt  *    without specific prior written permission.
19df930be7Sderaadt  *
20df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt  * SUCH DAMAGE.
31df930be7Sderaadt  */
32df930be7Sderaadt 
33df930be7Sderaadt #include <sys/stat.h>
34df930be7Sderaadt 
35df930be7Sderaadt #include <ctype.h>
36df930be7Sderaadt #include <err.h>
37df930be7Sderaadt #include <errno.h>
38a15d2b15Smillert #include <fcntl.h>
39df930be7Sderaadt #include <paths.h>
40df930be7Sderaadt #include <pwd.h>
41df930be7Sderaadt #include <stdio.h>
42df930be7Sderaadt #include <stdlib.h>
43df930be7Sderaadt #include <string.h>
44df930be7Sderaadt #include <unistd.h>
45b9fc9a72Sderaadt #include <limits.h>
464b826ba8Sderaadt #include <util.h>
47df930be7Sderaadt 
48df930be7Sderaadt #include "chpass.h"
49df930be7Sderaadt 
50120f1d91Smillert int
edit(char * tempname,struct passwd * pw)51e1f270f9Sderaadt edit(char *tempname, struct passwd *pw)
52df930be7Sderaadt {
53df930be7Sderaadt 	struct stat begin, end;
54df930be7Sderaadt 
55df930be7Sderaadt 	for (;;) {
56c18e99b7Sderaadt 		if (lstat(tempname, &begin) == -1 || S_ISLNK(begin.st_mode))
57120f1d91Smillert 			return (EDIT_ERROR);
584b826ba8Sderaadt 		pw_edit(1, tempname);
592d56537cSderaadt 		if (lstat(tempname, &end) == -1 || S_ISLNK(end.st_mode))
60120f1d91Smillert 			return (EDIT_ERROR);
61*79b54589Sguenther 		if (!timespeccmp(&begin.st_mtim, &end.st_mtim, -) &&
6271e3b011Sho 		    begin.st_size == end.st_size) {
63df930be7Sderaadt 			warnx("no changes made");
64120f1d91Smillert 			return (EDIT_NOCHANGE);
65df930be7Sderaadt 		}
66e3c6e362Smillert 		if (verify(tempname, pw))
67df930be7Sderaadt 			break;
68df930be7Sderaadt 		pw_prompt();
69df930be7Sderaadt 	}
70120f1d91Smillert 	return(EDIT_OK);
71df930be7Sderaadt }
72df930be7Sderaadt 
73df930be7Sderaadt /*
74df930be7Sderaadt  * display --
75df930be7Sderaadt  *	print out the file for the user to edit; strange side-effect:
76df930be7Sderaadt  *	set conditional flag if the user gets to edit the shell.
77df930be7Sderaadt  */
78df930be7Sderaadt void
display(char * tempname,int fd,struct passwd * pw)79e1f270f9Sderaadt display(char *tempname, int fd, struct passwd *pw)
80df930be7Sderaadt {
81df930be7Sderaadt 	FILE *fp;
829f52c269Sderaadt 	char *bp, *p;
839f52c269Sderaadt 	char chngstr[256];
84df930be7Sderaadt 
85df930be7Sderaadt 	if (!(fp = fdopen(fd, "w")))
86df930be7Sderaadt 		pw_error(tempname, 1, 1);
87df930be7Sderaadt 
88df930be7Sderaadt 	(void)fprintf(fp,
89df930be7Sderaadt 	    "# Changing user database information for %s.\n", pw->pw_name);
90df930be7Sderaadt 	if (!uid) {
91df930be7Sderaadt 		(void)fprintf(fp, "Login: %s\n", pw->pw_name);
9278386c57Sdownsj 		(void)fprintf(fp, "Encrypted password: %s\n", pw->pw_passwd);
937212eb4eSderaadt 		(void)fprintf(fp, "Uid [#]: %u\n", pw->pw_uid);
947212eb4eSderaadt 		(void)fprintf(fp, "Gid [# or name]: %u\n", pw->pw_gid);
95df930be7Sderaadt 		(void)fprintf(fp, "Change [month day year]: %s\n",
969f52c269Sderaadt 		    ttoa(chngstr, sizeof(chngstr), pw->pw_change));
97df930be7Sderaadt 		(void)fprintf(fp, "Expire [month day year]: %s\n",
989f52c269Sderaadt 		    ttoa(chngstr, sizeof(chngstr), pw->pw_expire));
99df930be7Sderaadt 		(void)fprintf(fp, "Class: %s\n", pw->pw_class);
100df930be7Sderaadt 		(void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
101df930be7Sderaadt 		(void)fprintf(fp, "Shell: %s\n",
102df930be7Sderaadt 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
103df930be7Sderaadt 	}
104df930be7Sderaadt 	/* Only admin can change "restricted" shells. */
105a0bffbc0Smillert 	else if (ok_shell(pw->pw_shell, NULL))
106df930be7Sderaadt 		/*
107df930be7Sderaadt 		 * Make shell a restricted field.  Ugly with a
108df930be7Sderaadt 		 * necklace, but there's not much else to do.
109df930be7Sderaadt 		 */
110df930be7Sderaadt 		(void)fprintf(fp, "Shell: %s\n",
111df930be7Sderaadt 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
112df930be7Sderaadt 	else
113df930be7Sderaadt 		list[E_SHELL].restricted = 1;
114df930be7Sderaadt 	bp = pw->pw_gecos;
115df930be7Sderaadt 	p = strsep(&bp, ",");
116df930be7Sderaadt 	(void)fprintf(fp, "Full Name: %s\n", p ? p : "");
117df930be7Sderaadt 	p = strsep(&bp, ",");
1184b78dd73Saaron 	(void)fprintf(fp, "Office Location: %s\n", p ? p : "");
119df930be7Sderaadt 	p = strsep(&bp, ",");
120df930be7Sderaadt 	(void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
121df930be7Sderaadt 	p = strsep(&bp, ",");
122df930be7Sderaadt 	(void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
123df930be7Sderaadt 
124df930be7Sderaadt 	(void)fchown(fd, getuid(), getgid());
125df930be7Sderaadt 	(void)fclose(fp);
126df930be7Sderaadt }
127df930be7Sderaadt 
128df930be7Sderaadt int
verify(char * tempname,struct passwd * pw)129e1f270f9Sderaadt verify(char *tempname, struct passwd *pw)
130df930be7Sderaadt {
131d2693af4Srobert 	unsigned int line;
132d2693af4Srobert 	size_t alen;
133df930be7Sderaadt 	static char buf[LINE_MAX];
134f57b352eSderaadt 	struct stat sb;
135f57b352eSderaadt 	char *p, *q;
136f57b352eSderaadt 	ENTRY *ep;
137f57b352eSderaadt 	FILE *fp;
138a15d2b15Smillert 	int fd;
139df930be7Sderaadt 
140a15d2b15Smillert 	if ((fd = open(tempname, O_RDONLY|O_NOFOLLOW)) == -1 ||
141a15d2b15Smillert 	    (fp = fdopen(fd, "r")) == NULL)
142df930be7Sderaadt 		pw_error(tempname, 1, 1);
143a15d2b15Smillert 	if (fstat(fd, &sb))
144df930be7Sderaadt 		pw_error(tempname, 1, 1);
145197dae64Smillert 	if (sb.st_size == 0 || sb.st_nlink != 1 || sb.st_uid != uid) {
146df930be7Sderaadt 		warnx("corrupted temporary file");
147df930be7Sderaadt 		goto bad;
148df930be7Sderaadt 	}
14911bcc710Sderaadt 	line = 0;
150df930be7Sderaadt 	while (fgets(buf, sizeof(buf), fp)) {
15111bcc710Sderaadt 		line++;
152df930be7Sderaadt 		if (!buf[0] || buf[0] == '#')
153df930be7Sderaadt 			continue;
15434152a3bSmillert 		if ((p = strchr(buf, '\n')) != NULL)
15534152a3bSmillert 			*p = '\0';
15634152a3bSmillert 		else if (!feof(fp)) {
157f57b352eSderaadt 			warnx("line %u too long", line);
158df930be7Sderaadt 			goto bad;
159df930be7Sderaadt 		}
160df930be7Sderaadt 		for (ep = list;; ++ep) {
161df930be7Sderaadt 			if (!ep->prompt) {
162f57b352eSderaadt 				warnx("unrecognized field on line %u", line);
163df930be7Sderaadt 				goto bad;
164df930be7Sderaadt 			}
165df930be7Sderaadt 			if (!strncasecmp(buf, ep->prompt, ep->len)) {
166df930be7Sderaadt 				if (ep->restricted && uid) {
167df930be7Sderaadt 					warnx(
168df930be7Sderaadt 					    "you may not change the %s field",
169df930be7Sderaadt 						ep->prompt);
170df930be7Sderaadt 					goto bad;
171df930be7Sderaadt 				}
172df930be7Sderaadt 				if (!(p = strchr(buf, ':'))) {
173f57b352eSderaadt 					warnx("line %u corrupted", line);
174df930be7Sderaadt 					goto bad;
175df930be7Sderaadt 				}
1766cd4fad2Sderaadt 				while (isspace((unsigned char)*++p))
1776cd4fad2Sderaadt 					;
1786cd4fad2Sderaadt 				for (q = p; isprint((unsigned char)*q); q++) {
179a7ba3c8dSaaron 					if (ep->except && strchr(ep->except,*q))
180a7ba3c8dSaaron 						break;
181a7ba3c8dSaaron 				}
182a7ba3c8dSaaron 				if (*q) {
183df930be7Sderaadt 					warnx(
184df930be7Sderaadt 				   "illegal character in the \"%s\" field",
185df930be7Sderaadt 					    ep->prompt);
186df930be7Sderaadt 					goto bad;
187df930be7Sderaadt 				}
188df930be7Sderaadt 				if ((ep->func)(p, pw, ep)) {
189df930be7Sderaadt bad:					(void)fclose(fp);
190df930be7Sderaadt 					return (0);
191df930be7Sderaadt 				}
192df930be7Sderaadt 				break;
193df930be7Sderaadt 			}
194df930be7Sderaadt 		}
195df930be7Sderaadt 	}
196df930be7Sderaadt 	(void)fclose(fp);
197df930be7Sderaadt 
198f1bacbb8Sderaadt 	if (list[E_NAME].save == NULL)
199f1bacbb8Sderaadt 		list[E_NAME].save = "";
200f1bacbb8Sderaadt 	if (list[E_BPHONE].save == NULL)
201f1bacbb8Sderaadt 		list[E_BPHONE].save = "";
202f1bacbb8Sderaadt 	if (list[E_HPHONE].save == NULL)
203f1bacbb8Sderaadt 		list[E_HPHONE].save = "";
204f1bacbb8Sderaadt 	if (list[E_LOCATE].save == NULL)
205f1bacbb8Sderaadt 		list[E_LOCATE].save = "";
206f1bacbb8Sderaadt 
207df930be7Sderaadt 	/* Build the gecos field. */
208d2d26747Sderaadt 	for (alen = 0, p = list[E_NAME].save; *p; p++)
209d2d26747Sderaadt 		if (*p == '&')
210d2d26747Sderaadt 			alen = alen + strlen(pw->pw_name) - 1;
211e1f270f9Sderaadt 	if (asprintf(&p, "%s,%s,%s,%s", list[E_NAME].save,
212e1f270f9Sderaadt 	    list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save) == -1)
213df930be7Sderaadt 		err(1, NULL);
214f57b352eSderaadt 	pw->pw_gecos = p;
215df930be7Sderaadt 
216df930be7Sderaadt 	if (snprintf(buf, sizeof(buf),
2177212eb4eSderaadt 	    "%s:%s:%u:%u:%s:%ld:%ld:%s:%s:%s",
218df930be7Sderaadt 	    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
219c3e1e82eSderaadt 	    (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
220cf9bf16bSderaadt 	    pw->pw_shell) >= 1023 ||
221cf9bf16bSderaadt 	    strlen(buf) + alen >= 1023) {
222df930be7Sderaadt 		warnx("entries too long");
223d2d26747Sderaadt 		free(p);
224df930be7Sderaadt 		return (0);
225df930be7Sderaadt 	}
226d2d26747Sderaadt 	free(p);
227d2d26747Sderaadt 
2284a436879Skstailey 	return (pw_scan(buf, pw, NULL));
229df930be7Sderaadt }
230