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