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