edit.c revision 1.3 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1990 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.2 mycroft /*static char sccsid[] = "from: @(#)edit.c 5.2 (Berkeley) 3/3/91";*/
36 1.3 deraadt static char rcsid[] = "$Id: edit.c,v 1.3 1994/08/17 19:54:34 deraadt Exp $";
37 1.1 cgd #endif /* not lint */
38 1.1 cgd
39 1.1 cgd #include <sys/param.h>
40 1.1 cgd #include <sys/stat.h>
41 1.1 cgd #include <pwd.h>
42 1.1 cgd #include <errno.h>
43 1.1 cgd #include <stdio.h>
44 1.1 cgd #include <paths.h>
45 1.1 cgd #include <stdlib.h>
46 1.1 cgd #include <string.h>
47 1.1 cgd #include "chpass.h"
48 1.1 cgd
49 1.1 cgd extern char *tempname;
50 1.1 cgd
51 1.1 cgd void
52 1.1 cgd edit(pw)
53 1.1 cgd struct passwd *pw;
54 1.1 cgd {
55 1.1 cgd struct stat begin, end;
56 1.1 cgd
57 1.1 cgd for (;;) {
58 1.1 cgd if (stat(tempname, &begin))
59 1.1 cgd pw_error(tempname, 1, 1);
60 1.1 cgd pw_edit(1);
61 1.1 cgd if (stat(tempname, &end))
62 1.1 cgd pw_error(tempname, 1, 1);
63 1.1 cgd if (begin.st_mtime == end.st_mtime) {
64 1.1 cgd (void)fprintf(stderr, "chpass: no changes made\n");
65 1.1 cgd pw_error((char *)NULL, 0, 0);
66 1.1 cgd }
67 1.1 cgd if (verify(pw))
68 1.1 cgd break;
69 1.1 cgd pw_prompt();
70 1.1 cgd }
71 1.1 cgd }
72 1.1 cgd
73 1.1 cgd /*
74 1.1 cgd * display --
75 1.1 cgd * print out the file for the user to edit; strange side-effect:
76 1.1 cgd * set conditional flag if the user gets to edit the shell.
77 1.1 cgd */
78 1.1 cgd display(fd, pw)
79 1.1 cgd int fd;
80 1.1 cgd struct passwd *pw;
81 1.1 cgd {
82 1.1 cgd register char *p;
83 1.1 cgd FILE *fp;
84 1.1 cgd char *bp, *ok_shell(), *ttoa();
85 1.1 cgd
86 1.1 cgd if (!(fp = fdopen(fd, "w")))
87 1.1 cgd pw_error(tempname, 1, 1);
88 1.1 cgd
89 1.1 cgd (void)fprintf(fp,
90 1.1 cgd "#Changing user database information for %s.\n", pw->pw_name);
91 1.1 cgd if (!uid) {
92 1.1 cgd (void)fprintf(fp, "Login: %s\n", pw->pw_name);
93 1.1 cgd (void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
94 1.1 cgd (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
95 1.1 cgd (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
96 1.1 cgd (void)fprintf(fp, "Change [month day year]: %s\n",
97 1.1 cgd ttoa(pw->pw_change));
98 1.1 cgd (void)fprintf(fp, "Expire [month day year]: %s\n",
99 1.1 cgd ttoa(pw->pw_expire));
100 1.1 cgd (void)fprintf(fp, "Class: %s\n", pw->pw_class);
101 1.1 cgd (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
102 1.1 cgd (void)fprintf(fp, "Shell: %s\n",
103 1.1 cgd *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
104 1.1 cgd }
105 1.1 cgd /* Only admin can change "restricted" shells. */
106 1.1 cgd else if (ok_shell(pw->pw_shell))
107 1.1 cgd /*
108 1.1 cgd * Make shell a restricted field. Ugly with a
109 1.1 cgd * necklace, but there's not much else to do.
110 1.1 cgd */
111 1.1 cgd (void)fprintf(fp, "Shell: %s\n",
112 1.1 cgd *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
113 1.1 cgd else
114 1.1 cgd list[E_SHELL].restricted = 1;
115 1.1 cgd bp = pw->pw_gecos;
116 1.1 cgd p = strsep(&bp, ",");
117 1.1 cgd (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
118 1.1 cgd p = strsep(&bp, ",");
119 1.1 cgd (void)fprintf(fp, "Location: %s\n", p ? p : "");
120 1.1 cgd p = strsep(&bp, ",");
121 1.1 cgd (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
122 1.1 cgd p = strsep(&bp, ",");
123 1.1 cgd (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
124 1.1 cgd
125 1.1 cgd (void)fchown(fd, getuid(), getgid());
126 1.1 cgd (void)fclose(fp);
127 1.1 cgd }
128 1.1 cgd
129 1.1 cgd verify(pw)
130 1.1 cgd struct passwd *pw;
131 1.1 cgd {
132 1.1 cgd register ENTRY *ep;
133 1.1 cgd register char *p;
134 1.1 cgd FILE *fp;
135 1.1 cgd int len;
136 1.3 deraadt static char buf[LINE_MAX];
137 1.1 cgd
138 1.1 cgd if (!(fp = fopen(tempname, "r")))
139 1.1 cgd pw_error(tempname, 1, 1);
140 1.1 cgd while (fgets(buf, sizeof(buf), fp)) {
141 1.1 cgd if (!buf[0] || buf[0] == '#')
142 1.1 cgd continue;
143 1.1 cgd if (!(p = index(buf, '\n'))) {
144 1.1 cgd (void)fprintf(stderr, "chpass: line too long.\n");
145 1.1 cgd goto bad;
146 1.1 cgd }
147 1.1 cgd *p = '\0';
148 1.1 cgd for (ep = list;; ++ep) {
149 1.1 cgd if (!ep->prompt) {
150 1.1 cgd (void)fprintf(stderr,
151 1.1 cgd "chpass: unrecognized field.\n");
152 1.1 cgd goto bad;
153 1.1 cgd }
154 1.1 cgd if (!strncasecmp(buf, ep->prompt, ep->len)) {
155 1.1 cgd if (ep->restricted && uid) {
156 1.1 cgd (void)fprintf(stderr,
157 1.1 cgd "chpass: you may not change the %s field.\n",
158 1.1 cgd ep->prompt);
159 1.1 cgd goto bad;
160 1.1 cgd }
161 1.1 cgd if (!(p = index(buf, ':'))) {
162 1.1 cgd (void)fprintf(stderr,
163 1.1 cgd "chpass: line corrupted.\n");
164 1.1 cgd goto bad;
165 1.1 cgd }
166 1.1 cgd while (isspace(*++p));
167 1.1 cgd if (ep->except && strpbrk(p, ep->except)) {
168 1.1 cgd (void)fprintf(stderr,
169 1.1 cgd "chpass: illegal character in the \"%s\" field.\n",
170 1.1 cgd ep->prompt);
171 1.1 cgd goto bad;
172 1.1 cgd }
173 1.1 cgd if ((ep->func)(p, pw, ep)) {
174 1.1 cgd bad: (void)fclose(fp);
175 1.1 cgd return(0);
176 1.1 cgd }
177 1.1 cgd break;
178 1.1 cgd }
179 1.1 cgd }
180 1.1 cgd }
181 1.1 cgd (void)fclose(fp);
182 1.1 cgd
183 1.1 cgd /* Build the gecos field. */
184 1.1 cgd len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
185 1.1 cgd strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
186 1.1 cgd if (!(p = malloc(len))) {
187 1.1 cgd (void)fprintf(stderr, "chpass: %s\n", strerror(errno));
188 1.1 cgd exit(1);
189 1.1 cgd }
190 1.1 cgd (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
191 1.1 cgd list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
192 1.1 cgd
193 1.1 cgd if (snprintf(buf, sizeof(buf),
194 1.1 cgd "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
195 1.1 cgd pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
196 1.1 cgd pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
197 1.1 cgd pw->pw_shell) >= sizeof(buf)) {
198 1.1 cgd (void)fprintf(stderr, "chpass: entries too long\n");
199 1.1 cgd return(0);
200 1.1 cgd }
201 1.1 cgd return(pw_scan(buf, pw));
202 1.1 cgd }
203