xref: /netbsd-src/usr.bin/chpass/edit.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: edit.c,v 1.7 1996/08/09 09:22:16 thorpej 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)edit.c	8.3 (Berkeley) 4/2/94";
39 #else
40 static char rcsid[] = "$NetBSD: edit.c,v 1.7 1996/08/09 09:22:16 thorpej Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <paths.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <util.h>
57 
58 #include "chpass.h"
59 
60 void
61 edit(tempname, pw)
62 	char *tempname;
63 	struct passwd *pw;
64 {
65 	struct stat begin, end;
66 
67 	for (;;) {
68 		if (stat(tempname, &begin))
69 			(*Pw_error)(tempname, 1, 1);
70 		pw_edit(1, tempname);
71 		if (stat(tempname, &end))
72 			(*Pw_error)(tempname, 1, 1);
73 		if (begin.st_mtime == end.st_mtime) {
74 			warnx("no changes made");
75 			unlink(tempname);
76 			(*Pw_error)(NULL, 0, 0);
77 		}
78 		if (verify(tempname, pw))
79 			break;
80 #ifdef YP
81 		if (use_yp)
82 			yppw_prompt();
83 		else
84 #endif
85 			pw_prompt();
86 	}
87 }
88 
89 /*
90  * display --
91  *	print out the file for the user to edit; strange side-effect:
92  *	set conditional flag if the user gets to edit the shell.
93  */
94 void
95 display(tempname, fd, pw)
96 	char *tempname;
97 	int fd;
98 	struct passwd *pw;
99 {
100 	FILE *fp;
101 	char *bp, *p, *ttoa();
102 
103 	if (!(fp = fdopen(fd, "w")))
104 		(*Pw_error)(tempname, 1, 1);
105 
106 	(void)fprintf(fp,
107 	    "#Changing user %sdatabase information for %s.\n",
108 	    use_yp ? "YP " : "", pw->pw_name);
109 	if (!uid) {
110 		(void)fprintf(fp, "Login: %s\n", pw->pw_name);
111 		(void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
112 		(void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
113 		(void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
114 		(void)fprintf(fp, "Change [month day year]: %s\n",
115 		    ttoa(pw->pw_change));
116 		(void)fprintf(fp, "Expire [month day year]: %s\n",
117 		    ttoa(pw->pw_expire));
118 		(void)fprintf(fp, "Class: %s\n", pw->pw_class);
119 		(void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
120 		(void)fprintf(fp, "Shell: %s\n",
121 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
122 	}
123 	/* Only admin can change "restricted" shells. */
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 		(void)fprintf(fp, "Shell: %s\n",
130 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
131 	else
132 		list[E_SHELL].restricted = 1;
133 	bp = pw->pw_gecos;
134 	p = strsep(&bp, ",");
135 	(void)fprintf(fp, "Full Name: %s\n", p ? p : "");
136 	p = strsep(&bp, ",");
137 	(void)fprintf(fp, "Location: %s\n", p ? p : "");
138 	p = strsep(&bp, ",");
139 	(void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
140 	p = strsep(&bp, ",");
141 	(void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
142 
143 	(void)fchown(fd, getuid(), getgid());
144 	(void)fclose(fp);
145 }
146 
147 int
148 verify(tempname, pw)
149 	char *tempname;
150 	struct passwd *pw;
151 {
152 	ENTRY *ep;
153 	char *p;
154 	struct stat sb;
155 	FILE *fp;
156 	int len;
157 	static char buf[LINE_MAX];
158 
159 	if (!(fp = fopen(tempname, "r")))
160 		(*Pw_error)(tempname, 1, 1);
161 	if (fstat(fileno(fp), &sb))
162 		(*Pw_error)(tempname, 1, 1);
163 	if (sb.st_size == 0) {
164 		warnx("corrupted temporary file");
165 		goto bad;
166 	}
167 	while (fgets(buf, sizeof(buf), fp)) {
168 		if (!buf[0] || buf[0] == '#')
169 			continue;
170 		if (!(p = strchr(buf, '\n'))) {
171 			warnx("line too long");
172 			goto bad;
173 		}
174 		*p = '\0';
175 		for (ep = list;; ++ep) {
176 			if (!ep->prompt) {
177 				warnx("unrecognized field");
178 				goto bad;
179 			}
180 			if (!strncasecmp(buf, ep->prompt, ep->len)) {
181 				if (ep->restricted && uid) {
182 					warnx(
183 					    "you may not change the %s field",
184 						ep->prompt);
185 					goto bad;
186 				}
187 				if (!(p = strchr(buf, ':'))) {
188 					warnx("line corrupted");
189 					goto bad;
190 				}
191 				while (isspace(*++p));
192 				if (ep->except && strpbrk(p, ep->except)) {
193 					warnx(
194 				   "illegal character in the \"%s\" field",
195 					    ep->prompt);
196 					goto bad;
197 				}
198 				if ((ep->func)(p, pw, ep)) {
199 bad:					(void)fclose(fp);
200 					return (0);
201 				}
202 				break;
203 			}
204 		}
205 	}
206 	(void)fclose(fp);
207 
208 	/* Build the gecos field. */
209 	len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
210 	    strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
211 	if (!(p = malloc(len)))
212 		err(1, NULL);
213 	(void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
214 	    list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
215 
216 	if (snprintf(buf, sizeof(buf),
217 	    "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
218 	    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
219 	    pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
220 	    pw->pw_shell) >= sizeof(buf)) {
221 		warnx("entries too long");
222 		return (0);
223 	}
224 	return (pw_scan(buf, pw, (int *)NULL));
225 }
226