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