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