xref: /freebsd/usr.bin/chpass/edit.c (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
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 #include <sys/param.h>
44 #include <sys/stat.h>
45 
46 #include <ctype.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <paths.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 #include <pw_scan.h>
57 #include <libutil.h>
58 
59 #include "chpass.h"
60 
61 static int display(const char *tfn, struct passwd *pw);
62 static struct passwd *verify(const char *tfn, struct passwd *pw);
63 
64 struct passwd *
65 edit(const char *tfn, struct passwd *pw)
66 {
67 	struct passwd *npw;
68 	char *line;
69 	size_t len;
70 
71 	if (display(tfn, pw) == -1)
72 		return (NULL);
73 	for (;;) {
74 		switch (pw_edit(1)) {
75 		case -1:
76 			return (NULL);
77 		case 0:
78 			return (pw_dup(pw));
79 		default:
80 			break;
81 		}
82 		if ((npw = verify(tfn, pw)) != NULL)
83 			return (npw);
84 		free(npw);
85 		printf("re-edit the password file? ");
86 		fflush(stdout);
87 		if ((line = fgetln(stdin, &len)) == NULL) {
88 			warn("fgetln()");
89 			return (NULL);
90 		}
91 		if (len > 0 && (*line == 'N' || *line == 'n'))
92 			return (NULL);
93 	}
94 }
95 
96 /*
97  * display --
98  *	print out the file for the user to edit; strange side-effect:
99  *	set conditional flag if the user gets to edit the shell.
100  */
101 static int
102 display(const char *tfn, struct passwd *pw)
103 {
104 	FILE *fp;
105 	char *bp, *gecos, *p;
106 
107 	if ((fp = fopen(tfn, "w")) == NULL) {
108 		warn("%s", tfn);
109 		return (-1);
110 	}
111 
112 	(void)fprintf(fp,
113 	    "#Changing user information for %s.\n", pw->pw_name);
114 	if (master_mode) {
115 		(void)fprintf(fp, "Login: %s\n", pw->pw_name);
116 		(void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
117 		(void)fprintf(fp, "Uid [#]: %lu\n", (unsigned long)pw->pw_uid);
118 		(void)fprintf(fp, "Gid [# or name]: %lu\n",
119 		    (unsigned long)pw->pw_gid);
120 		(void)fprintf(fp, "Change [month day year]: %s\n",
121 		    ttoa(pw->pw_change));
122 		(void)fprintf(fp, "Expire [month day year]: %s\n",
123 		    ttoa(pw->pw_expire));
124 		(void)fprintf(fp, "Class: %s\n", pw->pw_class);
125 		(void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
126 		(void)fprintf(fp, "Shell: %s\n",
127 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
128 	}
129 	/* Only admin can change "restricted" shells. */
130 #if 0
131 	else if (ok_shell(pw->pw_shell))
132 		/*
133 		 * Make shell a restricted field.  Ugly with a
134 		 * necklace, but there's not much else to do.
135 		 */
136 #else
137 	else if ((!list[E_SHELL].restricted && ok_shell(pw->pw_shell)) ||
138 	    master_mode)
139 		/*
140 		 * If change not restrict (table.c) and standard shell
141 		 *	OR if root, then allow editing of shell.
142 		 */
143 #endif
144 		(void)fprintf(fp, "Shell: %s\n",
145 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
146 	else
147 		list[E_SHELL].restricted = 1;
148 
149 	if ((bp = gecos = strdup(pw->pw_gecos)) == NULL) {
150 		warn(NULL);
151 		fclose(fp);
152 		return (-1);
153 	}
154 
155 	p = strsep(&bp, ",");
156 	p = strdup(p ? p : "");
157 	list[E_NAME].save = p;
158 	if (!list[E_NAME].restricted || master_mode)
159 	  (void)fprintf(fp, "Full Name: %s\n", p);
160 
161 	p = strsep(&bp, ",");
162 	p = strdup(p ? p : "");
163 	list[E_LOCATE].save = p;
164 	if (!list[E_LOCATE].restricted || master_mode)
165 	  (void)fprintf(fp, "Office Location: %s\n", p);
166 
167 	p = strsep(&bp, ",");
168 	p = strdup(p ? p : "");
169 	list[E_BPHONE].save = p;
170 	if (!list[E_BPHONE].restricted || master_mode)
171 	  (void)fprintf(fp, "Office Phone: %s\n", p);
172 
173 	p = strsep(&bp, ",");
174 	p = strdup(p ? p : "");
175 	list[E_HPHONE].save = p;
176 	if (!list[E_HPHONE].restricted || master_mode)
177 	  (void)fprintf(fp, "Home Phone: %s\n", p);
178 
179 	bp = strdup(bp ? bp : "");
180 	list[E_OTHER].save = bp;
181 	if (!list[E_OTHER].restricted || master_mode)
182 	  (void)fprintf(fp, "Other information: %s\n", bp);
183 
184 	free(gecos);
185 
186 	(void)fchown(fileno(fp), getuid(), getgid());
187 	(void)fclose(fp);
188 	return (0);
189 }
190 
191 static struct passwd *
192 verify(const char *tfn, struct passwd *pw)
193 {
194 	struct passwd *npw;
195 	ENTRY *ep;
196 	char *buf, *p, *val;
197 	struct stat sb;
198 	FILE *fp;
199 	int line;
200 	size_t len;
201 
202 	if ((pw = pw_dup(pw)) == NULL)
203 		return (NULL);
204 	if ((fp = fopen(tfn, "r")) == NULL ||
205 	    fstat(fileno(fp), &sb) == -1) {
206 		warn("%s", tfn);
207 		free(pw);
208 		return (NULL);
209 	}
210 	if (sb.st_size == 0) {
211 		warnx("corrupted temporary file");
212 		fclose(fp);
213 		free(pw);
214 		return (NULL);
215 	}
216 	val = NULL;
217 	for (line = 1; (buf = fgetln(fp, &len)) != NULL; ++line) {
218 		if (*buf == '\0' || *buf == '#')
219 			continue;
220 		while (len > 0 && isspace(buf[len - 1]))
221 			--len;
222 		for (ep = list;; ++ep) {
223 			if (!ep->prompt) {
224 				warnx("%s: unrecognized field on line %d",
225 				    tfn, line);
226 				goto bad;
227 			}
228 			if (ep->len > len)
229 				continue;
230 			if (strncasecmp(buf, ep->prompt, ep->len) != 0)
231 				continue;
232 			if (ep->restricted && !master_mode) {
233 				warnx("%s: you may not change the %s field",
234 				    tfn, ep->prompt);
235 				goto bad;
236 			}
237 			for (p = buf; p < buf + len && *p != ':'; ++p)
238 				/* nothing */ ;
239 			if (*p != ':') {
240 				warnx("%s: line %d corrupted", tfn, line);
241 				goto bad;
242 			}
243 			while (++p < buf + len && isspace(*p))
244 				/* nothing */ ;
245 			free(val);
246 			asprintf(&val, "%.*s", (int)(buf + len - p), p);
247 			if (val == NULL)
248 				goto bad;
249 			if (ep->except && strpbrk(val, ep->except)) {
250 				warnx("%s: invalid character in \"%s\" field '%s'",
251 				    tfn, ep->prompt, val);
252 				goto bad;
253 			}
254 			if ((ep->func)(val, pw, ep))
255 				goto bad;
256 			break;
257 		}
258 	}
259 	free(val);
260 	fclose(fp);
261 
262 	/* Build the gecos field. */
263 	len = asprintf(&p, "%s,%s,%s,%s,%s", list[E_NAME].save,
264 	    list[E_LOCATE].save, list[E_BPHONE].save,
265 	    list[E_HPHONE].save, list[E_OTHER].save);
266 	if (p == NULL) {
267 		warn("asprintf()");
268 		free(pw);
269 		return (NULL);
270 	}
271 	while (len > 0 && p[len - 1] == ',')
272 		p[--len] = '\0';
273 	pw->pw_gecos = p;
274 	buf = pw_make(pw);
275 	free(pw);
276 	free(p);
277 	if (buf == NULL) {
278 		warn("pw_make()");
279 		return (NULL);
280 	}
281 	npw = pw_scan(buf, PWSCAN_WARN|PWSCAN_MASTER);
282 	free(buf);
283 	return (npw);
284 bad:
285 	free(pw);
286 	free(val);
287 	fclose(fp);
288 	return (NULL);
289 }
290