2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 2002 Networks Associates Technology, Inc.
9 * Portions of this software were developed for the FreeBSD Project by
10 * ThinkSec AS and NAI Labs, the Security Research Division of Network
11 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
12 * ("CBOSS"), as part of the DARPA CHATS research program.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 #include <sys/param.h>
61 static int display(const char *tfn, struct passwd *pw);
62 static struct passwd *verify(const char *tfn, struct passwd *pw);
65 edit(const char *tfn, struct passwd *pw)
71 if (display(tfn, pw) == -1)
82 if ((npw = verify(tfn, pw)) != NULL)
85 printf("re-edit the password file? ");
87 if ((line = fgetln(stdin, &len)) == NULL) {
91 if (len > 0 && (*line == 'N' || *line == 'n'))
98 * print out the file for the user to edit; strange side-effect:
99 * set conditional flag if the user gets to edit the shell.
102 display(const char *tfn, struct passwd *pw)
105 char *bp, *gecos, *p;
107 if ((fp = fopen(tfn, "w")) == NULL) {
113 "#Changing user information for %s.\n", pw->pw_name);
115 (void)fprintf(fp, "Login: %s\n", pw->pw_name);
116 (void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
117 (void)fprintf(fp, "Uid [#]: %lu\n", (unsigned long)pw->pw_uid);
118 (void)fprintf(fp, "Gid [# or name]: %lu\n",
119 (unsigned long)pw->pw_gid);
120 (void)fprintf(fp, "Change [month day year]: %s\n",
121 ttoa(pw->pw_change));
122 (void)fprintf(fp, "Expire [month day year]: %s\n",
123 ttoa(pw->pw_expire));
124 (void)fprintf(fp, "Class: %s\n", pw->pw_class);
125 (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
126 (void)fprintf(fp, "Shell: %s\n",
127 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
129 /* Only admin can change "restricted" shells. */
131 else if (ok_shell(pw->pw_shell))
133 * Make shell a restricted field. Ugly with a
134 * necklace, but there's not much else to do.
137 else if ((!list[E_SHELL].restricted && ok_shell(pw->pw_shell)) ||
140 * If change not restrict (table.c) and standard shell
141 * OR if root, then allow editing of shell.
144 (void)fprintf(fp, "Shell: %s\n",
145 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
147 list[E_SHELL].restricted = 1;
149 if ((bp = gecos = strdup(pw->pw_gecos)) == NULL) {
155 p = strsep(&bp, ",");
156 p = strdup(p ? p : "");
157 list[E_NAME].save = p;
158 if (!list[E_NAME].restricted || master_mode)
159 (void)fprintf(fp, "Full Name: %s\n", p);
161 p = strsep(&bp, ",");
162 p = strdup(p ? p : "");
163 list[E_LOCATE].save = p;
164 if (!list[E_LOCATE].restricted || master_mode)
165 (void)fprintf(fp, "Office Location: %s\n", p);
167 p = strsep(&bp, ",");
168 p = strdup(p ? p : "");
169 list[E_BPHONE].save = p;
170 if (!list[E_BPHONE].restricted || master_mode)
171 (void)fprintf(fp, "Office Phone: %s\n", p);
173 p = strsep(&bp, ",");
174 p = strdup(p ? p : "");
175 list[E_HPHONE].save = p;
176 if (!list[E_HPHONE].restricted || master_mode)
177 (void)fprintf(fp, "Home Phone: %s\n", p);
179 bp = strdup(bp ? bp : "");
180 list[E_OTHER].save = bp;
181 if (!list[E_OTHER].restricted || master_mode)
182 (void)fprintf(fp, "Other information: %s\n", bp);
186 (void)fchown(fileno(fp), getuid(), getgid());
191 static struct passwd *
192 verify(const char *tfn, struct passwd *pw)
202 if ((pw = pw_dup(pw)) == NULL)
204 if ((fp = fopen(tfn, "r")) == NULL ||
205 fstat(fileno(fp), &sb) == -1) {
210 if (sb.st_size == 0) {
211 warnx("corrupted temporary file");
217 for (line = 1; (buf = fgetln(fp, &len)) != NULL; ++line) {
218 if (*buf == '\0' || *buf == '#')
220 while (len > 0 && isspace(buf[len - 1]))
222 for (ep = list;; ++ep) {
224 warnx("%s: unrecognized field on line %d",
230 if (strncasecmp(buf, ep->prompt, ep->len) != 0)
232 if (ep->restricted && !master_mode) {
233 warnx("%s: you may not change the %s field",
237 for (p = buf; p < buf + len && *p != ':'; ++p)
240 warnx("%s: line %d corrupted", tfn, line);
243 while (++p < buf + len && isspace(*p))
246 asprintf(&val, "%.*s", (int)(buf + len - p), p);
249 if (ep->except && strpbrk(val, ep->except)) {
250 warnx("%s: invalid character in \"%s\" field '%s'",
251 tfn, ep->prompt, val);
254 if ((ep->func)(val, pw, ep))
262 /* Build the gecos field. */
263 len = asprintf(&p, "%s,%s,%s,%s,%s", list[E_NAME].save,
264 list[E_LOCATE].save, list[E_BPHONE].save,
265 list[E_HPHONE].save, list[E_OTHER].save);
271 while (len > 0 && p[len - 1] == ',')
281 npw = pw_scan(buf, PWSCAN_WARN|PWSCAN_MASTER);