BSD 4_4 release
[unix-history] / usr / src / usr.bin / chpass / edit.c
CommitLineData
255347ad 1/*-
ad787160
C
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
255347ad 4 *
ad787160
C
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.
255347ad
KB
32 */
33
34#ifndef lint
ad787160 35static char sccsid[] = "@(#)edit.c 8.1 (Berkeley) 6/6/93";
255347ad
KB
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
456319a0
KB
50void
51edit(pw)
255347ad
KB
52 struct passwd *pw;
53{
54 struct stat begin, end;
55
255347ad
KB
56 for (;;) {
57 if (stat(tempname, &begin))
58 pw_error(tempname, 1, 1);
456319a0 59 pw_edit(1);
255347ad
KB
60 if (stat(tempname, &end))
61 pw_error(tempname, 1, 1);
3148146f 62 if (begin.st_mtime == end.st_mtime) {
255347ad 63 (void)fprintf(stderr, "chpass: no changes made\n");
488b1124 64 pw_error(NULL, 0, 0);
255347ad
KB
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
456319a0 124 (void)fchown(fd, getuid(), getgid());
255347ad
KB
125 (void)fclose(fp);
126}
127
128verify(pw)
129 struct passwd *pw;
130{
131 register ENTRY *ep;
132 register char *p;
3148146f 133 struct stat sb;
255347ad
KB
134 FILE *fp;
135 int len;
136 char buf[LINE_MAX];
137
138 if (!(fp = fopen(tempname, "r")))
139 pw_error(tempname, 1, 1);
3148146f
KB
140 if (fstat(fileno(fp), &sb))
141 pw_error(tempname, 1, 1);
142 if (sb.st_size == 0) {
143 (void)fprintf(stderr, "chpass: corrupted temporary file.\n");
144 goto bad;
145 }
255347ad
KB
146 while (fgets(buf, sizeof(buf), fp)) {
147 if (!buf[0] || buf[0] == '#')
148 continue;
149 if (!(p = index(buf, '\n'))) {
150 (void)fprintf(stderr, "chpass: line too long.\n");
151 goto bad;
152 }
153 *p = '\0';
154 for (ep = list;; ++ep) {
155 if (!ep->prompt) {
156 (void)fprintf(stderr,
157 "chpass: unrecognized field.\n");
158 goto bad;
159 }
160 if (!strncasecmp(buf, ep->prompt, ep->len)) {
161 if (ep->restricted && uid) {
162 (void)fprintf(stderr,
163 "chpass: you may not change the %s field.\n",
164 ep->prompt);
165 goto bad;
166 }
167 if (!(p = index(buf, ':'))) {
168 (void)fprintf(stderr,
169 "chpass: line corrupted.\n");
170 goto bad;
171 }
172 while (isspace(*++p));
173 if (ep->except && strpbrk(p, ep->except)) {
174 (void)fprintf(stderr,
175 "chpass: illegal character in the \"%s\" field.\n",
176 ep->prompt);
177 goto bad;
178 }
179 if ((ep->func)(p, pw, ep)) {
180bad: (void)fclose(fp);
181 return(0);
182 }
183 break;
184 }
185 }
186 }
187 (void)fclose(fp);
188
189 /* Build the gecos field. */
190 len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
191 strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
192 if (!(p = malloc(len))) {
193 (void)fprintf(stderr, "chpass: %s\n", strerror(errno));
194 exit(1);
195 }
196 (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
197 list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
198
199 if (snprintf(buf, sizeof(buf),
200 "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
201 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
202 pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
203 pw->pw_shell) >= sizeof(buf)) {
204 (void)fprintf(stderr, "chpass: entries too long\n");
205 return(0);
206 }
207 return(pw_scan(buf, pw));
208}