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