]> git.saurik.com Git - apple/system_cmds.git/blob - chpass.tproj/edit.c
system_cmds-230.7.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 * 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
76 extern char *tempname;
77
78 void
79 edit(pw)
80 struct passwd *pw;
81 {
82 struct stat begin, end;
83
84 for (;;) {
85 if (stat(tempname, &begin))
86 pw_error(tempname, 1, 1);
87 pw_edit(1);
88 if (stat(tempname, &end))
89 pw_error(tempname, 1, 1);
90 if (begin.st_mtime == end.st_mtime) {
91 warnx("no changes made");
92 pw_error(NULL, 0, 0);
93 }
94 if (verify(pw))
95 break;
96 pw_prompt();
97 }
98 }
99
100 /*
101 * display --
102 * print out the file for the user to edit; strange side-effect:
103 * set conditional flag if the user gets to edit the shell.
104 */
105 void
106 display(fd, pw)
107 int fd;
108 struct passwd *pw;
109 {
110 FILE *fp;
111 char *bp, *p, *ttoa();
112
113 if (!(fp = fdopen(fd, "w")))
114 pw_error(tempname, 1, 1);
115
116 (void)fprintf(fp,
117 "#Changing user database information for %s.\n", pw->pw_name);
118 if (!uid) {
119 (void)fprintf(fp, "Login: %s\n", pw->pw_name);
120 (void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
121 (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
122 (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
123 (void)fprintf(fp, "Change [month day year]: %s\n",
124 ttoa(pw->pw_change));
125 (void)fprintf(fp, "Expire [month day year]: %s\n",
126 ttoa(pw->pw_expire));
127 (void)fprintf(fp, "Class: %s\n", pw->pw_class);
128 (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
129 (void)fprintf(fp, "Shell: %s\n",
130 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
131 }
132 /* Only admin can change "restricted" shells. */
133 else if (ok_shell(pw->pw_shell))
134 /*
135 * Make shell a restricted field. Ugly with a
136 * necklace, but there's not much else to do.
137 */
138 (void)fprintf(fp, "Shell: %s\n",
139 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
140 else
141 list[E_SHELL].restricted = 1;
142 bp = pw->pw_gecos;
143 p = strsep(&bp, ",");
144 (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
145 p = strsep(&bp, ",");
146 (void)fprintf(fp, "Location: %s\n", p ? p : "");
147 p = strsep(&bp, ",");
148 (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
149 p = strsep(&bp, ",");
150 (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
151
152 (void)fchown(fd, getuid(), getgid());
153 (void)fclose(fp);
154 }
155
156 int
157 verify(pw)
158 struct passwd *pw;
159 {
160 ENTRY *ep;
161 char *p;
162 struct stat sb;
163 FILE *fp;
164 int len;
165 char buf[LINE_MAX];
166
167 if (!(fp = fopen(tempname, "r")))
168 pw_error(tempname, 1, 1);
169 if (fstat(fileno(fp), &sb))
170 pw_error(tempname, 1, 1);
171 if (sb.st_size == 0) {
172 warnx("corrupted temporary file");
173 goto bad;
174 }
175 while (fgets(buf, sizeof(buf), fp)) {
176 if (!buf[0] || buf[0] == '#')
177 continue;
178 if (!(p = strchr(buf, '\n'))) {
179 warnx("line too long");
180 goto bad;
181 }
182 *p = '\0';
183 for (ep = list;; ++ep) {
184 if (!ep->prompt) {
185 warnx("unrecognized field");
186 goto bad;
187 }
188 if (!strncasecmp(buf, ep->prompt, ep->len)) {
189 if (ep->restricted && uid) {
190 warnx(
191 "you may not change the %s field",
192 ep->prompt);
193 goto bad;
194 }
195 if (!(p = strchr(buf, ':'))) {
196 warnx("line corrupted");
197 goto bad;
198 }
199 while (isspace(*++p));
200 if (ep->except && strpbrk(p, ep->except)) {
201 warnx(
202 "illegal character in the \"%s\" field",
203 ep->prompt);
204 goto bad;
205 }
206 if ((ep->func)(p, pw, ep)) {
207 bad: (void)fclose(fp);
208 return (0);
209 }
210 break;
211 }
212 }
213 }
214 (void)fclose(fp);
215
216 /* Build the gecos field. */
217 len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
218 strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
219 if (!(p = malloc(len)))
220 err(1, NULL);
221 (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
222 list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
223
224 if (snprintf(buf, sizeof(buf),
225 "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
226 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
227 pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
228 pw->pw_shell) >= sizeof(buf)) {
229 warnx("entries too long");
230 return (0);
231 }
232 return (pw_scan(buf, pw));
233 }