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