xref: /freebsd/usr.bin/chpass/edit.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
19b50d902SRodney W. Grimes /*-
2*df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6f1d05925SDag-Erling Smørgrav  * Copyright (c) 2002 Networks Associates Technology, Inc.
7f1d05925SDag-Erling Smørgrav  * All rights reserved.
8f1d05925SDag-Erling Smørgrav  *
9f1d05925SDag-Erling Smørgrav  * Portions of this software were developed for the FreeBSD Project by
10f1d05925SDag-Erling Smørgrav  * ThinkSec AS and NAI Labs, the Security Research Division of Network
11f1d05925SDag-Erling Smørgrav  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
12f1d05925SDag-Erling Smørgrav  * ("CBOSS"), as part of the DARPA CHATS research program.
139b50d902SRodney W. Grimes  *
149b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
159b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
169b50d902SRodney W. Grimes  * are met:
179b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
189b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
199b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
209b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
219b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
229b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
239b50d902SRodney W. Grimes  *    must display the following acknowledgement:
249b50d902SRodney W. Grimes  *	This product includes software developed by the University of
259b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
269b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
279b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
289b50d902SRodney W. Grimes  *    without specific prior written permission.
299b50d902SRodney W. Grimes  *
309b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
319b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
329b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
339b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
349b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
359b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
369b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
379b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
389b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
399b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
409b50d902SRodney W. Grimes  * SUCH DAMAGE.
419b50d902SRodney W. Grimes  */
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #include <sys/param.h>
449b50d902SRodney W. Grimes #include <sys/stat.h>
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #include <ctype.h>
479b50d902SRodney W. Grimes #include <err.h>
489b50d902SRodney W. Grimes #include <errno.h>
499b50d902SRodney W. Grimes #include <paths.h>
509b50d902SRodney W. Grimes #include <pwd.h>
519b50d902SRodney W. Grimes #include <stdio.h>
529b50d902SRodney W. Grimes #include <stdlib.h>
539b50d902SRodney W. Grimes #include <string.h>
549b50d902SRodney W. Grimes #include <unistd.h>
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes #include <pw_scan.h>
57f1d05925SDag-Erling Smørgrav #include <libutil.h>
589b50d902SRodney W. Grimes 
599b50d902SRodney W. Grimes #include "chpass.h"
609b50d902SRodney W. Grimes 
61f1d05925SDag-Erling Smørgrav static int display(const char *tfn, struct passwd *pw);
62f1d05925SDag-Erling Smørgrav static struct passwd *verify(const char *tfn, struct passwd *pw);
639b50d902SRodney W. Grimes 
64f1d05925SDag-Erling Smørgrav struct passwd *
edit(const char * tfn,struct passwd * pw)65f1d05925SDag-Erling Smørgrav edit(const char *tfn, struct passwd *pw)
669b50d902SRodney W. Grimes {
67f1d05925SDag-Erling Smørgrav 	struct passwd *npw;
68f1d05925SDag-Erling Smørgrav 	char *line;
69f1d05925SDag-Erling Smørgrav 	size_t len;
709b50d902SRodney W. Grimes 
71f1d05925SDag-Erling Smørgrav 	if (display(tfn, pw) == -1)
72f1d05925SDag-Erling Smørgrav 		return (NULL);
739b50d902SRodney W. Grimes 	for (;;) {
74f1d05925SDag-Erling Smørgrav 		switch (pw_edit(1)) {
75f1d05925SDag-Erling Smørgrav 		case -1:
76f1d05925SDag-Erling Smørgrav 			return (NULL);
77f1d05925SDag-Erling Smørgrav 		case 0:
78f1d05925SDag-Erling Smørgrav 			return (pw_dup(pw));
79f1d05925SDag-Erling Smørgrav 		default:
809b50d902SRodney W. Grimes 			break;
81f1d05925SDag-Erling Smørgrav 		}
82f1d05925SDag-Erling Smørgrav 		if ((npw = verify(tfn, pw)) != NULL)
83f1d05925SDag-Erling Smørgrav 			return (npw);
84f1d05925SDag-Erling Smørgrav 		free(npw);
85f1d05925SDag-Erling Smørgrav 		printf("re-edit the password file? ");
86f1d05925SDag-Erling Smørgrav 		fflush(stdout);
87f1d05925SDag-Erling Smørgrav 		if ((line = fgetln(stdin, &len)) == NULL) {
88f1d05925SDag-Erling Smørgrav 			warn("fgetln()");
89f1d05925SDag-Erling Smørgrav 			return (NULL);
90f1d05925SDag-Erling Smørgrav 		}
91f1d05925SDag-Erling Smørgrav 		if (len > 0 && (*line == 'N' || *line == 'n'))
92f1d05925SDag-Erling Smørgrav 			return (NULL);
939b50d902SRodney W. Grimes 	}
949b50d902SRodney W. Grimes }
959b50d902SRodney W. Grimes 
969b50d902SRodney W. Grimes /*
979b50d902SRodney W. Grimes  * display --
989b50d902SRodney W. Grimes  *	print out the file for the user to edit; strange side-effect:
999b50d902SRodney W. Grimes  *	set conditional flag if the user gets to edit the shell.
1009b50d902SRodney W. Grimes  */
101f1d05925SDag-Erling Smørgrav static int
display(const char * tfn,struct passwd * pw)102f1d05925SDag-Erling Smørgrav display(const char *tfn, struct passwd *pw)
1039b50d902SRodney W. Grimes {
1049b50d902SRodney W. Grimes 	FILE *fp;
105a4280091SDag-Erling Smørgrav 	char *bp, *gecos, *p;
1069b50d902SRodney W. Grimes 
107f1d05925SDag-Erling Smørgrav 	if ((fp = fopen(tfn, "w")) == NULL) {
108f1d05925SDag-Erling Smørgrav 		warn("%s", tfn);
109f1d05925SDag-Erling Smørgrav 		return (-1);
110f1d05925SDag-Erling Smørgrav 	}
1119b50d902SRodney W. Grimes 
1129b50d902SRodney W. Grimes 	(void)fprintf(fp,
113f1d05925SDag-Erling Smørgrav 	    "#Changing user information for %s.\n", pw->pw_name);
114f1d05925SDag-Erling Smørgrav 	if (master_mode) {
1159b50d902SRodney W. Grimes 		(void)fprintf(fp, "Login: %s\n", pw->pw_name);
1169b50d902SRodney W. Grimes 		(void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
117f458f48bSMike Barcroft 		(void)fprintf(fp, "Uid [#]: %lu\n", (unsigned long)pw->pw_uid);
118f458f48bSMike Barcroft 		(void)fprintf(fp, "Gid [# or name]: %lu\n",
119f458f48bSMike Barcroft 		    (unsigned long)pw->pw_gid);
1209b50d902SRodney W. Grimes 		(void)fprintf(fp, "Change [month day year]: %s\n",
1219b50d902SRodney W. Grimes 		    ttoa(pw->pw_change));
1229b50d902SRodney W. Grimes 		(void)fprintf(fp, "Expire [month day year]: %s\n",
1239b50d902SRodney W. Grimes 		    ttoa(pw->pw_expire));
1249b50d902SRodney W. Grimes 		(void)fprintf(fp, "Class: %s\n", pw->pw_class);
1259b50d902SRodney W. Grimes 		(void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
1269b50d902SRodney W. Grimes 		(void)fprintf(fp, "Shell: %s\n",
1279b50d902SRodney W. Grimes 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
1289b50d902SRodney W. Grimes 	}
1299b50d902SRodney W. Grimes 	/* Only admin can change "restricted" shells. */
13096846ff6SWarner Losh #if 0
1319b50d902SRodney W. Grimes 	else if (ok_shell(pw->pw_shell))
1329b50d902SRodney W. Grimes 		/*
1339b50d902SRodney W. Grimes 		 * Make shell a restricted field.  Ugly with a
1349b50d902SRodney W. Grimes 		 * necklace, but there's not much else to do.
1359b50d902SRodney W. Grimes 		 */
136c2dfe9feSBill Paul #else
137f1d05925SDag-Erling Smørgrav 	else if ((!list[E_SHELL].restricted && ok_shell(pw->pw_shell)) ||
138f1d05925SDag-Erling Smørgrav 	    master_mode)
139c2dfe9feSBill Paul 		/*
140c2dfe9feSBill Paul 		 * If change not restrict (table.c) and standard shell
141c2dfe9feSBill Paul 		 *	OR if root, then allow editing of shell.
142c2dfe9feSBill Paul 		 */
143c2dfe9feSBill Paul #endif
1449b50d902SRodney W. Grimes 		(void)fprintf(fp, "Shell: %s\n",
1459b50d902SRodney W. Grimes 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
1469b50d902SRodney W. Grimes 	else
1479b50d902SRodney W. Grimes 		list[E_SHELL].restricted = 1;
148a4280091SDag-Erling Smørgrav 
149a4280091SDag-Erling Smørgrav 	if ((bp = gecos = strdup(pw->pw_gecos)) == NULL) {
150a4280091SDag-Erling Smørgrav 		warn(NULL);
151a4280091SDag-Erling Smørgrav 		fclose(fp);
152a4280091SDag-Erling Smørgrav 		return (-1);
153a4280091SDag-Erling Smørgrav 	}
154e1731211SJordan K. Hubbard 
1559b50d902SRodney W. Grimes 	p = strsep(&bp, ",");
156a83c5014SWarner Losh 	p = strdup(p ? p : "");
157a83c5014SWarner Losh 	list[E_NAME].save = p;
158f1d05925SDag-Erling Smørgrav 	if (!list[E_NAME].restricted || master_mode)
159a83c5014SWarner Losh 	  (void)fprintf(fp, "Full Name: %s\n", p);
160e1731211SJordan K. Hubbard 
1619b50d902SRodney W. Grimes 	p = strsep(&bp, ",");
162a83c5014SWarner Losh 	p = strdup(p ? p : "");
163a83c5014SWarner Losh 	list[E_LOCATE].save = p;
164f1d05925SDag-Erling Smørgrav 	if (!list[E_LOCATE].restricted || master_mode)
165a83c5014SWarner Losh 	  (void)fprintf(fp, "Office Location: %s\n", p);
166e1731211SJordan K. Hubbard 
1679b50d902SRodney W. Grimes 	p = strsep(&bp, ",");
168a83c5014SWarner Losh 	p = strdup(p ? p : "");
169a83c5014SWarner Losh 	list[E_BPHONE].save = p;
170f1d05925SDag-Erling Smørgrav 	if (!list[E_BPHONE].restricted || master_mode)
171a83c5014SWarner Losh 	  (void)fprintf(fp, "Office Phone: %s\n", p);
172e1731211SJordan K. Hubbard 
1739b50d902SRodney W. Grimes 	p = strsep(&bp, ",");
174a83c5014SWarner Losh 	p = strdup(p ? p : "");
175a83c5014SWarner Losh 	list[E_HPHONE].save = p;
176f1d05925SDag-Erling Smørgrav 	if (!list[E_HPHONE].restricted || master_mode)
177a83c5014SWarner Losh 	  (void)fprintf(fp, "Home Phone: %s\n", p);
1789b50d902SRodney W. Grimes 
179a83c5014SWarner Losh 	bp = strdup(bp ? bp : "");
180a83c5014SWarner Losh 	list[E_OTHER].save = bp;
181f1d05925SDag-Erling Smørgrav 	if (!list[E_OTHER].restricted || master_mode)
182a83c5014SWarner Losh 	  (void)fprintf(fp, "Other information: %s\n", bp);
18319ae8d1eSPeter Hawkins 
184a4280091SDag-Erling Smørgrav 	free(gecos);
185a4280091SDag-Erling Smørgrav 
186f1d05925SDag-Erling Smørgrav 	(void)fchown(fileno(fp), getuid(), getgid());
1879b50d902SRodney W. Grimes 	(void)fclose(fp);
1889b50d902SRodney W. Grimes 	return (0);
1899b50d902SRodney W. Grimes }
190f1d05925SDag-Erling Smørgrav 
191f1d05925SDag-Erling Smørgrav static struct passwd *
verify(const char * tfn,struct passwd * pw)192f1d05925SDag-Erling Smørgrav verify(const char *tfn, struct passwd *pw)
193f1d05925SDag-Erling Smørgrav {
194f1d05925SDag-Erling Smørgrav 	struct passwd *npw;
195f1d05925SDag-Erling Smørgrav 	ENTRY *ep;
196f1d05925SDag-Erling Smørgrav 	char *buf, *p, *val;
197f1d05925SDag-Erling Smørgrav 	struct stat sb;
198f1d05925SDag-Erling Smørgrav 	FILE *fp;
199f1d05925SDag-Erling Smørgrav 	int line;
200f1d05925SDag-Erling Smørgrav 	size_t len;
201f1d05925SDag-Erling Smørgrav 
202f1d05925SDag-Erling Smørgrav 	if ((pw = pw_dup(pw)) == NULL)
203f1d05925SDag-Erling Smørgrav 		return (NULL);
204f1d05925SDag-Erling Smørgrav 	if ((fp = fopen(tfn, "r")) == NULL ||
205f1d05925SDag-Erling Smørgrav 	    fstat(fileno(fp), &sb) == -1) {
206f1d05925SDag-Erling Smørgrav 		warn("%s", tfn);
207f1d05925SDag-Erling Smørgrav 		free(pw);
208f1d05925SDag-Erling Smørgrav 		return (NULL);
209f1d05925SDag-Erling Smørgrav 	}
210f1d05925SDag-Erling Smørgrav 	if (sb.st_size == 0) {
211f1d05925SDag-Erling Smørgrav 		warnx("corrupted temporary file");
212f1d05925SDag-Erling Smørgrav 		fclose(fp);
213f1d05925SDag-Erling Smørgrav 		free(pw);
214f1d05925SDag-Erling Smørgrav 		return (NULL);
215f1d05925SDag-Erling Smørgrav 	}
216f1d05925SDag-Erling Smørgrav 	val = NULL;
217f1d05925SDag-Erling Smørgrav 	for (line = 1; (buf = fgetln(fp, &len)) != NULL; ++line) {
218f1d05925SDag-Erling Smørgrav 		if (*buf == '\0' || *buf == '#')
219f1d05925SDag-Erling Smørgrav 			continue;
220f1d05925SDag-Erling Smørgrav 		while (len > 0 && isspace(buf[len - 1]))
221f1d05925SDag-Erling Smørgrav 			--len;
222f1d05925SDag-Erling Smørgrav 		for (ep = list;; ++ep) {
223f1d05925SDag-Erling Smørgrav 			if (!ep->prompt) {
224f1d05925SDag-Erling Smørgrav 				warnx("%s: unrecognized field on line %d",
225f1d05925SDag-Erling Smørgrav 				    tfn, line);
226f1d05925SDag-Erling Smørgrav 				goto bad;
227f1d05925SDag-Erling Smørgrav 			}
228f1d05925SDag-Erling Smørgrav 			if (ep->len > len)
229f1d05925SDag-Erling Smørgrav 				continue;
230f1d05925SDag-Erling Smørgrav 			if (strncasecmp(buf, ep->prompt, ep->len) != 0)
231f1d05925SDag-Erling Smørgrav 				continue;
232f1d05925SDag-Erling Smørgrav 			if (ep->restricted && !master_mode) {
233f1d05925SDag-Erling Smørgrav 				warnx("%s: you may not change the %s field",
234f1d05925SDag-Erling Smørgrav 				    tfn, ep->prompt);
235f1d05925SDag-Erling Smørgrav 				goto bad;
236f1d05925SDag-Erling Smørgrav 			}
237f1d05925SDag-Erling Smørgrav 			for (p = buf; p < buf + len && *p != ':'; ++p)
238f1d05925SDag-Erling Smørgrav 				/* nothing */ ;
239f1d05925SDag-Erling Smørgrav 			if (*p != ':') {
240f1d05925SDag-Erling Smørgrav 				warnx("%s: line %d corrupted", tfn, line);
241f1d05925SDag-Erling Smørgrav 				goto bad;
242f1d05925SDag-Erling Smørgrav 			}
243f1d05925SDag-Erling Smørgrav 			while (++p < buf + len && isspace(*p))
244f1d05925SDag-Erling Smørgrav 				/* nothing */ ;
245f1d05925SDag-Erling Smørgrav 			free(val);
246f1d05925SDag-Erling Smørgrav 			asprintf(&val, "%.*s", (int)(buf + len - p), p);
247f1d05925SDag-Erling Smørgrav 			if (val == NULL)
248f1d05925SDag-Erling Smørgrav 				goto bad;
249f1d05925SDag-Erling Smørgrav 			if (ep->except && strpbrk(val, ep->except)) {
250f1d05925SDag-Erling Smørgrav 				warnx("%s: invalid character in \"%s\" field '%s'",
251f1d05925SDag-Erling Smørgrav 				    tfn, ep->prompt, val);
252f1d05925SDag-Erling Smørgrav 				goto bad;
253f1d05925SDag-Erling Smørgrav 			}
254f1d05925SDag-Erling Smørgrav 			if ((ep->func)(val, pw, ep))
255f1d05925SDag-Erling Smørgrav 				goto bad;
2569b50d902SRodney W. Grimes 			break;
2579b50d902SRodney W. Grimes 		}
2589b50d902SRodney W. Grimes 	}
259f1d05925SDag-Erling Smørgrav 	free(val);
260f1d05925SDag-Erling Smørgrav 	fclose(fp);
2619b50d902SRodney W. Grimes 
2629b50d902SRodney W. Grimes 	/* Build the gecos field. */
263f1d05925SDag-Erling Smørgrav 	len = asprintf(&p, "%s,%s,%s,%s,%s", list[E_NAME].save,
264f1d05925SDag-Erling Smørgrav 	    list[E_LOCATE].save, list[E_BPHONE].save,
265f1d05925SDag-Erling Smørgrav 	    list[E_HPHONE].save, list[E_OTHER].save);
266f1d05925SDag-Erling Smørgrav 	if (p == NULL) {
267f1d05925SDag-Erling Smørgrav 		warn("asprintf()");
268f1d05925SDag-Erling Smørgrav 		free(pw);
269f1d05925SDag-Erling Smørgrav 		return (NULL);
2709b50d902SRodney W. Grimes 	}
271f1d05925SDag-Erling Smørgrav 	while (len > 0 && p[len - 1] == ',')
272f1d05925SDag-Erling Smørgrav 		p[--len] = '\0';
273f1d05925SDag-Erling Smørgrav 	pw->pw_gecos = p;
274f1d05925SDag-Erling Smørgrav 	buf = pw_make(pw);
275f1d05925SDag-Erling Smørgrav 	free(pw);
2769fc081a4SPhilippe Charnier 	free(p);
277f1d05925SDag-Erling Smørgrav 	if (buf == NULL) {
278f1d05925SDag-Erling Smørgrav 		warn("pw_make()");
279f1d05925SDag-Erling Smørgrav 		return (NULL);
280f1d05925SDag-Erling Smørgrav 	}
281f1d05925SDag-Erling Smørgrav 	npw = pw_scan(buf, PWSCAN_WARN|PWSCAN_MASTER);
282f1d05925SDag-Erling Smørgrav 	free(buf);
283f1d05925SDag-Erling Smørgrav 	return (npw);
284f1d05925SDag-Erling Smørgrav bad:
285f1d05925SDag-Erling Smørgrav 	free(pw);
286f1d05925SDag-Erling Smørgrav 	free(val);
287f1d05925SDag-Erling Smørgrav 	fclose(fp);
288f1d05925SDag-Erling Smørgrav 	return (NULL);
2899b50d902SRodney W. Grimes }
290