1a13c4773Sbostic /*-
2*cd1f3a57Spendry * Copyright (c) 1990, 1993, 1994
322b902e0Sbostic * The Regents of the University of California. All rights reserved.
4a13c4773Sbostic *
5a13c4773Sbostic * %sccs.include.redist.c%
6a13c4773Sbostic */
7a13c4773Sbostic
8a13c4773Sbostic #ifndef lint
9*cd1f3a57Spendry static char sccsid[] = "@(#)edit.c 8.3 (Berkeley) 04/02/94";
10a13c4773Sbostic #endif /* not lint */
11a13c4773Sbostic
12a13c4773Sbostic #include <sys/param.h>
13a13c4773Sbostic #include <sys/stat.h>
14577d5484Spendry
15577d5484Spendry #include <ctype.h>
16577d5484Spendry #include <err.h>
17a13c4773Sbostic #include <errno.h>
18a13c4773Sbostic #include <paths.h>
19577d5484Spendry #include <pwd.h>
20577d5484Spendry #include <stdio.h>
21a13c4773Sbostic #include <stdlib.h>
22a13c4773Sbostic #include <string.h>
23577d5484Spendry #include <unistd.h>
24577d5484Spendry
25577d5484Spendry #include <pw_scan.h>
26577d5484Spendry #include <pw_util.h>
27577d5484Spendry
28a13c4773Sbostic #include "chpass.h"
29a13c4773Sbostic
30a13c4773Sbostic extern char *tempname;
31a13c4773Sbostic
32f5344122Sbostic void
edit(pw)33f5344122Sbostic edit(pw)
34a13c4773Sbostic struct passwd *pw;
35a13c4773Sbostic {
36a13c4773Sbostic struct stat begin, end;
37a13c4773Sbostic
38a13c4773Sbostic for (;;) {
39a13c4773Sbostic if (stat(tempname, &begin))
40a13c4773Sbostic pw_error(tempname, 1, 1);
41f5344122Sbostic pw_edit(1);
42a13c4773Sbostic if (stat(tempname, &end))
43a13c4773Sbostic pw_error(tempname, 1, 1);
4498e9dd53Sbostic if (begin.st_mtime == end.st_mtime) {
45577d5484Spendry warnx("no changes made");
46f4941221Sbostic pw_error(NULL, 0, 0);
47a13c4773Sbostic }
48a13c4773Sbostic if (verify(pw))
49a13c4773Sbostic break;
50a13c4773Sbostic pw_prompt();
51a13c4773Sbostic }
52a13c4773Sbostic }
53a13c4773Sbostic
54a13c4773Sbostic /*
55a13c4773Sbostic * display --
56a13c4773Sbostic * print out the file for the user to edit; strange side-effect:
57a13c4773Sbostic * set conditional flag if the user gets to edit the shell.
58a13c4773Sbostic */
59577d5484Spendry void
display(fd,pw)60a13c4773Sbostic display(fd, pw)
61a13c4773Sbostic int fd;
62a13c4773Sbostic struct passwd *pw;
63a13c4773Sbostic {
64a13c4773Sbostic FILE *fp;
65577d5484Spendry char *bp, *p, *ttoa();
66a13c4773Sbostic
67a13c4773Sbostic if (!(fp = fdopen(fd, "w")))
68a13c4773Sbostic pw_error(tempname, 1, 1);
69a13c4773Sbostic
70a13c4773Sbostic (void)fprintf(fp,
71a13c4773Sbostic "#Changing user database information for %s.\n", pw->pw_name);
72a13c4773Sbostic if (!uid) {
73a13c4773Sbostic (void)fprintf(fp, "Login: %s\n", pw->pw_name);
74a13c4773Sbostic (void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
75a13c4773Sbostic (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
76a13c4773Sbostic (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
77a13c4773Sbostic (void)fprintf(fp, "Change [month day year]: %s\n",
78a13c4773Sbostic ttoa(pw->pw_change));
79a13c4773Sbostic (void)fprintf(fp, "Expire [month day year]: %s\n",
80a13c4773Sbostic ttoa(pw->pw_expire));
81a13c4773Sbostic (void)fprintf(fp, "Class: %s\n", pw->pw_class);
82a13c4773Sbostic (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
83a13c4773Sbostic (void)fprintf(fp, "Shell: %s\n",
84a13c4773Sbostic *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
85a13c4773Sbostic }
86a13c4773Sbostic /* Only admin can change "restricted" shells. */
87a13c4773Sbostic else if (ok_shell(pw->pw_shell))
88a13c4773Sbostic /*
89a13c4773Sbostic * Make shell a restricted field. Ugly with a
90a13c4773Sbostic * necklace, but there's not much else to do.
91a13c4773Sbostic */
92a13c4773Sbostic (void)fprintf(fp, "Shell: %s\n",
93a13c4773Sbostic *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
94a13c4773Sbostic else
95a13c4773Sbostic list[E_SHELL].restricted = 1;
96a13c4773Sbostic bp = pw->pw_gecos;
97a13c4773Sbostic p = strsep(&bp, ",");
98a13c4773Sbostic (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
99a13c4773Sbostic p = strsep(&bp, ",");
100a13c4773Sbostic (void)fprintf(fp, "Location: %s\n", p ? p : "");
101a13c4773Sbostic p = strsep(&bp, ",");
102a13c4773Sbostic (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
103a13c4773Sbostic p = strsep(&bp, ",");
104a13c4773Sbostic (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
105a13c4773Sbostic
106f5344122Sbostic (void)fchown(fd, getuid(), getgid());
107a13c4773Sbostic (void)fclose(fp);
108a13c4773Sbostic }
109a13c4773Sbostic
110577d5484Spendry int
verify(pw)111a13c4773Sbostic verify(pw)
112a13c4773Sbostic struct passwd *pw;
113a13c4773Sbostic {
114577d5484Spendry ENTRY *ep;
115577d5484Spendry char *p;
11698e9dd53Sbostic struct stat sb;
117a13c4773Sbostic FILE *fp;
118a13c4773Sbostic int len;
119a13c4773Sbostic char buf[LINE_MAX];
120a13c4773Sbostic
121a13c4773Sbostic if (!(fp = fopen(tempname, "r")))
122a13c4773Sbostic pw_error(tempname, 1, 1);
12398e9dd53Sbostic if (fstat(fileno(fp), &sb))
12498e9dd53Sbostic pw_error(tempname, 1, 1);
12598e9dd53Sbostic if (sb.st_size == 0) {
126577d5484Spendry warnx("corrupted temporary file");
12798e9dd53Sbostic goto bad;
12898e9dd53Sbostic }
129a13c4773Sbostic while (fgets(buf, sizeof(buf), fp)) {
130a13c4773Sbostic if (!buf[0] || buf[0] == '#')
131a13c4773Sbostic continue;
132577d5484Spendry if (!(p = strchr(buf, '\n'))) {
133577d5484Spendry warnx("line too long");
134a13c4773Sbostic goto bad;
135a13c4773Sbostic }
136a13c4773Sbostic *p = '\0';
137a13c4773Sbostic for (ep = list;; ++ep) {
138a13c4773Sbostic if (!ep->prompt) {
139577d5484Spendry warnx("unrecognized field");
140a13c4773Sbostic goto bad;
141a13c4773Sbostic }
142a13c4773Sbostic if (!strncasecmp(buf, ep->prompt, ep->len)) {
143a13c4773Sbostic if (ep->restricted && uid) {
144577d5484Spendry warnx(
145577d5484Spendry "you may not change the %s field",
146a13c4773Sbostic ep->prompt);
147a13c4773Sbostic goto bad;
148a13c4773Sbostic }
149577d5484Spendry if (!(p = strchr(buf, ':'))) {
150577d5484Spendry warnx("line corrupted");
151a13c4773Sbostic goto bad;
152a13c4773Sbostic }
153a13c4773Sbostic while (isspace(*++p));
154a13c4773Sbostic if (ep->except && strpbrk(p, ep->except)) {
155577d5484Spendry warnx(
156577d5484Spendry "illegal character in the \"%s\" field",
157a13c4773Sbostic ep->prompt);
158a13c4773Sbostic goto bad;
159a13c4773Sbostic }
160a13c4773Sbostic if ((ep->func)(p, pw, ep)) {
161a13c4773Sbostic bad: (void)fclose(fp);
162a13c4773Sbostic return (0);
163a13c4773Sbostic }
164a13c4773Sbostic break;
165a13c4773Sbostic }
166a13c4773Sbostic }
167a13c4773Sbostic }
168a13c4773Sbostic (void)fclose(fp);
169a13c4773Sbostic
170a13c4773Sbostic /* Build the gecos field. */
171a13c4773Sbostic len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
172a13c4773Sbostic strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
173577d5484Spendry if (!(p = malloc(len)))
174577d5484Spendry err(1, NULL);
175a13c4773Sbostic (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
176a13c4773Sbostic list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
177a13c4773Sbostic
178a13c4773Sbostic if (snprintf(buf, sizeof(buf),
179a13c4773Sbostic "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
180a13c4773Sbostic pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
181a13c4773Sbostic pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
182a13c4773Sbostic pw->pw_shell) >= sizeof(buf)) {
183577d5484Spendry warnx("entries too long");
184a13c4773Sbostic return (0);
185a13c4773Sbostic }
186a13c4773Sbostic return (pw_scan(buf, pw));
187a13c4773Sbostic }
188