]> git.saurik.com Git - apple/system_cmds.git/blob - chpass.tproj/edit.c
ecdbe7094e532b904fa22f004405343b78b8e888
[apple/system_cmds.git] / chpass.tproj / edit.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*-
26 * Copyright (c) 1990, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58 #include <sys/param.h>
59 #include <sys/stat.h>
60
61 #include <ctype.h>
62 #include <err.h>
63 #include <errno.h>
64 #include <paths.h>
65 #include <pwd.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70
71 #include <pw_scan.h>
72 #include <pw_util.h>
73
74 #include "chpass.h"
75 #ifdef DIRECTORY_SERVICE
76 #include "directory_service.h"
77
78 extern int dswhere;
79 #endif /* DIRECTORY_SERVICE */
80
81 extern char *tempname;
82
83 void
84 edit(pw)
85 struct passwd *pw;
86 {
87 struct stat begin, end;
88
89 for (;;) {
90 if (stat(tempname, &begin))
91 pw_error(tempname, 1, 1);
92 pw_edit(1);
93 if (stat(tempname, &end))
94 pw_error(tempname, 1, 1);
95 if (begin.st_mtime == end.st_mtime) {
96 warnx("no changes made");
97 pw_error(NULL, 0, 0);
98 }
99 if (verify(pw))
100 break;
101 pw_prompt();
102 }
103 }
104
105 /*
106 * display --
107 * print out the file for the user to edit; strange side-effect:
108 * set conditional flag if the user gets to edit the shell.
109 */
110 void
111 display(fd, pw)
112 int fd;
113 struct passwd *pw;
114 {
115 FILE *fp;
116 char *bp, *p, *ttoa();
117 #ifdef DIRECTORY_SERVICE
118 ENTRY *ep;
119 struct display d;
120 int ndisplayed = 0;
121 #endif /* DIRECTORY_SERVICE */
122
123 if (!(fp = fdopen(fd, "w")))
124 pw_error(tempname, 1, 1);
125
126 (void)fprintf(fp,
127 "# Changing user database information for %s.\n"
128 "#\n"
129 "# (use \"passwd\" to change the password)\n"
130 "##\n",
131 pw->pw_name);
132
133 #ifdef DIRECTORY_SERVICE
134 switch (dswhere) {
135 case WHERE_FILES:
136 (void)fprintf(fp,
137 "# Flat file: /etc/master.passwd\n"
138 "##\n");
139 break;
140 case WHERE_LOCALNI:
141 (void)fprintf(fp,
142 "# Local NetInfo Database\n"
143 "##\n");
144 break;
145 }
146 d.pw = pw;
147 bp = pw->pw_gecos;
148 p = strsep(&bp, ",");
149 d.fullname = (p ? p : "");
150 p = strsep(&bp, ",");
151 d.location = (p ? p : "");
152 p = strsep(&bp, ",");
153 d.officephone = (p ? p : "");
154 p = strsep(&bp, ",");
155 d.homephone = ( p ? p : "");
156
157 for (ep = list; ep->prompt; ep++)
158 if (!ep->restricted) {
159 ep->display(&d, fp);
160 ndisplayed++;
161 }
162 if(!ndisplayed) {
163 (void)fprintf(fp, "###################################\n");
164 (void)fprintf(fp, "# No fields are available to change\n");
165 (void)fprintf(fp, "###################################\n");
166 }
167 #else /* DIRECTORY_SERVICE */
168 (void)fprintf(fp,
169 "##\n"
170 "# User Database\n"
171 "# \n"
172 "# Note: This program edits the /etc/master.passwd file which is only \n"
173 "# consulted when the system is running in single-user mode. At other times \n"
174 "# this information is handled by lookupd. By default, lookupd gets \n"
175 "# information from NetInfo, so this file will not be consulted unless you \n"
176 "# have changed lookupd's configuration.\n"
177 "##\n");
178
179 if (!uid) {
180 (void)fprintf(fp, "Login: %s\n", pw->pw_name);
181 (void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
182 (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
183 (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
184 (void)fprintf(fp, "Change [month day year]: %s\n",
185 ttoa(pw->pw_change));
186 (void)fprintf(fp, "Expire [month day year]: %s\n",
187 ttoa(pw->pw_expire));
188 (void)fprintf(fp, "Class: %s\n", pw->pw_class);
189 (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
190 (void)fprintf(fp, "Shell: %s\n",
191 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
192 }
193 /* Only admin can change "restricted" shells. */
194 else if (ok_shell(pw->pw_shell))
195 /*
196 * Make shell a restricted field. Ugly with a
197 * necklace, but there's not much else to do.
198 */
199 (void)fprintf(fp, "Shell: %s\n",
200 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
201 else
202 list[E_SHELL].restricted = 1;
203 bp = pw->pw_gecos;
204 p = strsep(&bp, ",");
205 (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
206 p = strsep(&bp, ",");
207 (void)fprintf(fp, "Location: %s\n", p ? p : "");
208 p = strsep(&bp, ",");
209 (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
210 p = strsep(&bp, ",");
211 (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
212 #endif /* DIRECTORY_SERVICE */
213
214 (void)fchown(fd, getuid(), getgid());
215 (void)fclose(fp);
216 }
217
218 int
219 verify(pw)
220 struct passwd *pw;
221 {
222 ENTRY *ep;
223 char *p;
224 struct stat sb;
225 FILE *fp;
226 int len;
227 char buf[LINE_MAX];
228
229 if (!(fp = fopen(tempname, "r")))
230 pw_error(tempname, 1, 1);
231 if (fstat(fileno(fp), &sb))
232 pw_error(tempname, 1, 1);
233 if (sb.st_size == 0) {
234 warnx("corrupted temporary file");
235 goto bad;
236 }
237 while (fgets(buf, sizeof(buf), fp)) {
238 if (!buf[0] || buf[0] == '#')
239 continue;
240 if (!(p = strchr(buf, '\n'))) {
241 warnx("line too long");
242 goto bad;
243 }
244 *p = '\0';
245 for (ep = list;; ++ep) {
246 if (!ep->prompt) {
247 warnx("unrecognized field");
248 goto bad;
249 }
250 if (!strncasecmp(buf, ep->prompt, ep->len)) {
251 if (ep->restricted && uid) {
252 warnx(
253 "you may not change the %s field",
254 ep->prompt);
255 goto bad;
256 }
257 if (!(p = strchr(buf, ':'))) {
258 warnx("line corrupted");
259 goto bad;
260 }
261 while (isspace(*++p));
262 if (ep->except && strpbrk(p, ep->except)) {
263 warnx(
264 "illegal character in the \"%s\" field",
265 ep->prompt);
266 goto bad;
267 }
268 if ((ep->func)(p, pw, ep)) {
269 bad: (void)fclose(fp);
270 return (0);
271 }
272 break;
273 }
274 }
275 }
276 (void)fclose(fp);
277
278 /* Build the gecos field. */
279 #ifdef DIRECTORY_SERVICE
280 if (list[E_NAME].save) {
281 if (list[E_LOCATE].save) {
282 #endif /* DIRECTORY_SERVICE */
283 len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
284 strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
285 if (!(p = malloc(len)))
286 err(1, NULL);
287 (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
288 list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
289 #ifdef DIRECTORY_SERVICE
290 } else
291 pw->pw_gecos = list[E_NAME].save;
292 } else
293 pw->pw_gecos = "";
294 #endif /* DIRECTORY_SERVICE */
295
296 if (snprintf(buf, sizeof(buf),
297 "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
298 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
299 pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
300 pw->pw_shell) >= sizeof(buf)) {
301 warnx("entries too long");
302 return (0);
303 }
304 return (pw_scan(buf, pw, NULL));
305 }