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