xref: /freebsd/usr.bin/chpass/edit.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1990, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 2002 Networks Associates Technology, Inc.
7  * All rights reserved.
8  *
9  * Portions of this software were developed for the FreeBSD Project by
10  * ThinkSec AS and NAI Labs, the Security Research Division of Network
11  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
12  * ("CBOSS"), as part of the DARPA CHATS research program.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *	This product includes software developed by the University of
25  *	California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  */
42 
43 #if 0
44 #endif
45 
46 #include <sys/cdefs.h>
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 
50 #include <ctype.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <paths.h>
54 #include <pwd.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include <pw_scan.h>
61 #include <libutil.h>
62 
63 #include "chpass.h"
64 
65 static int display(const char *tfn, struct passwd *pw);
66 static struct passwd *verify(const char *tfn, struct passwd *pw);
67 
68 struct passwd *
69 edit(const char *tfn, struct passwd *pw)
70 {
71 	struct passwd *npw;
72 	char *line;
73 	size_t len;
74 
75 	if (display(tfn, pw) == -1)
76 		return (NULL);
77 	for (;;) {
78 		switch (pw_edit(1)) {
79 		case -1:
80 			return (NULL);
81 		case 0:
82 			return (pw_dup(pw));
83 		default:
84 			break;
85 		}
86 		if ((npw = verify(tfn, pw)) != NULL)
87 			return (npw);
88 		free(npw);
89 		printf("re-edit the password file? ");
90 		fflush(stdout);
91 		if ((line = fgetln(stdin, &len)) == NULL) {
92 			warn("fgetln()");
93 			return (NULL);
94 		}
95 		if (len > 0 && (*line == 'N' || *line == 'n'))
96 			return (NULL);
97 	}
98 }
99 
100 /*
101  * display --
102  *	print out the file for the user to edit; strange side-effect:
103  *	set conditional flag if the user gets to edit the shell.
104  */
105 static int
106 display(const char *tfn, struct passwd *pw)
107 {
108 	FILE *fp;
109 	char *bp, *gecos, *p;
110 
111 	if ((fp = fopen(tfn, "w")) == NULL) {
112 		warn("%s", tfn);
113 		return (-1);
114 	}
115 
116 	(void)fprintf(fp,
117 	    "#Changing user information for %s.\n", pw->pw_name);
118 	if (master_mode) {
119 		(void)fprintf(fp, "Login: %s\n", pw->pw_name);
120 		(void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
121 		(void)fprintf(fp, "Uid [#]: %lu\n", (unsigned long)pw->pw_uid);
122 		(void)fprintf(fp, "Gid [# or name]: %lu\n",
123 		    (unsigned long)pw->pw_gid);
124 		(void)fprintf(fp, "Change [month day year]: %s\n",
125 		    ttoa(pw->pw_change));
126 		(void)fprintf(fp, "Expire [month day year]: %s\n",
127 		    ttoa(pw->pw_expire));
128 		(void)fprintf(fp, "Class: %s\n", pw->pw_class);
129 		(void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
130 		(void)fprintf(fp, "Shell: %s\n",
131 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
132 	}
133 	/* Only admin can change "restricted" shells. */
134 #if 0
135 	else if (ok_shell(pw->pw_shell))
136 		/*
137 		 * Make shell a restricted field.  Ugly with a
138 		 * necklace, but there's not much else to do.
139 		 */
140 #else
141 	else if ((!list[E_SHELL].restricted && ok_shell(pw->pw_shell)) ||
142 	    master_mode)
143 		/*
144 		 * If change not restrict (table.c) and standard shell
145 		 *	OR if root, then allow editing of shell.
146 		 */
147 #endif
148 		(void)fprintf(fp, "Shell: %s\n",
149 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
150 	else
151 		list[E_SHELL].restricted = 1;
152 
153 	if ((bp = gecos = strdup(pw->pw_gecos)) == NULL) {
154 		warn(NULL);
155 		fclose(fp);
156 		return (-1);
157 	}
158 
159 	p = strsep(&bp, ",");
160 	p = strdup(p ? p : "");
161 	list[E_NAME].save = p;
162 	if (!list[E_NAME].restricted || master_mode)
163 	  (void)fprintf(fp, "Full Name: %s\n", p);
164 
165 	p = strsep(&bp, ",");
166 	p = strdup(p ? p : "");
167 	list[E_LOCATE].save = p;
168 	if (!list[E_LOCATE].restricted || master_mode)
169 	  (void)fprintf(fp, "Office Location: %s\n", p);
170 
171 	p = strsep(&bp, ",");
172 	p = strdup(p ? p : "");
173 	list[E_BPHONE].save = p;
174 	if (!list[E_BPHONE].restricted || master_mode)
175 	  (void)fprintf(fp, "Office Phone: %s\n", p);
176 
177 	p = strsep(&bp, ",");
178 	p = strdup(p ? p : "");
179 	list[E_HPHONE].save = p;
180 	if (!list[E_HPHONE].restricted || master_mode)
181 	  (void)fprintf(fp, "Home Phone: %s\n", p);
182 
183 	bp = strdup(bp ? bp : "");
184 	list[E_OTHER].save = bp;
185 	if (!list[E_OTHER].restricted || master_mode)
186 	  (void)fprintf(fp, "Other information: %s\n", bp);
187 
188 	free(gecos);
189 
190 	(void)fchown(fileno(fp), getuid(), getgid());
191 	(void)fclose(fp);
192 	return (0);
193 }
194 
195 static struct passwd *
196 verify(const char *tfn, struct passwd *pw)
197 {
198 	struct passwd *npw;
199 	ENTRY *ep;
200 	char *buf, *p, *val;
201 	struct stat sb;
202 	FILE *fp;
203 	int line;
204 	size_t len;
205 
206 	if ((pw = pw_dup(pw)) == NULL)
207 		return (NULL);
208 	if ((fp = fopen(tfn, "r")) == NULL ||
209 	    fstat(fileno(fp), &sb) == -1) {
210 		warn("%s", tfn);
211 		free(pw);
212 		return (NULL);
213 	}
214 	if (sb.st_size == 0) {
215 		warnx("corrupted temporary file");
216 		fclose(fp);
217 		free(pw);
218 		return (NULL);
219 	}
220 	val = NULL;
221 	for (line = 1; (buf = fgetln(fp, &len)) != NULL; ++line) {
222 		if (*buf == '\0' || *buf == '#')
223 			continue;
224 		while (len > 0 && isspace(buf[len - 1]))
225 			--len;
226 		for (ep = list;; ++ep) {
227 			if (!ep->prompt) {
228 				warnx("%s: unrecognized field on line %d",
229 				    tfn, line);
230 				goto bad;
231 			}
232 			if (ep->len > len)
233 				continue;
234 			if (strncasecmp(buf, ep->prompt, ep->len) != 0)
235 				continue;
236 			if (ep->restricted && !master_mode) {
237 				warnx("%s: you may not change the %s field",
238 				    tfn, ep->prompt);
239 				goto bad;
240 			}
241 			for (p = buf; p < buf + len && *p != ':'; ++p)
242 				/* nothing */ ;
243 			if (*p != ':') {
244 				warnx("%s: line %d corrupted", tfn, line);
245 				goto bad;
246 			}
247 			while (++p < buf + len && isspace(*p))
248 				/* nothing */ ;
249 			free(val);
250 			asprintf(&val, "%.*s", (int)(buf + len - p), p);
251 			if (val == NULL)
252 				goto bad;
253 			if (ep->except && strpbrk(val, ep->except)) {
254 				warnx("%s: invalid character in \"%s\" field '%s'",
255 				    tfn, ep->prompt, val);
256 				goto bad;
257 			}
258 			if ((ep->func)(val, pw, ep))
259 				goto bad;
260 			break;
261 		}
262 	}
263 	free(val);
264 	fclose(fp);
265 
266 	/* Build the gecos field. */
267 	len = asprintf(&p, "%s,%s,%s,%s,%s", list[E_NAME].save,
268 	    list[E_LOCATE].save, list[E_BPHONE].save,
269 	    list[E_HPHONE].save, list[E_OTHER].save);
270 	if (p == NULL) {
271 		warn("asprintf()");
272 		free(pw);
273 		return (NULL);
274 	}
275 	while (len > 0 && p[len - 1] == ',')
276 		p[--len] = '\0';
277 	pw->pw_gecos = p;
278 	buf = pw_make(pw);
279 	free(pw);
280 	free(p);
281 	if (buf == NULL) {
282 		warn("pw_make()");
283 		return (NULL);
284 	}
285 	npw = pw_scan(buf, PWSCAN_WARN|PWSCAN_MASTER);
286 	free(buf);
287 	return (npw);
288 bad:
289 	free(pw);
290 	free(val);
291 	fclose(fp);
292 	return (NULL);
293 }
294