]>
git.saurik.com Git - apple/system_cmds.git/blob - chpass.tproj/edit.c
ecdbe7094e532b904fa22f004405343b78b8e888
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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.
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
58 #include <sys/param.h>
75 #ifdef DIRECTORY_SERVICE
76 #include "directory_service.h"
79 #endif /* DIRECTORY_SERVICE */
81 extern char *tempname
;
87 struct stat begin
, end
;
90 if (stat(tempname
, &begin
))
91 pw_error(tempname
, 1, 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");
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.
116 char *bp
, *p
, *ttoa();
117 #ifdef DIRECTORY_SERVICE
121 #endif /* DIRECTORY_SERVICE */
123 if (!(fp
= fdopen(fd
, "w")))
124 pw_error(tempname
, 1, 1);
127 "# Changing user database information for %s.\n"
129 "# (use \"passwd\" to change the password)\n"
133 #ifdef DIRECTORY_SERVICE
137 "# Flat file: /etc/master.passwd\n"
142 "# Local NetInfo Database\n"
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
: "");
157 for (ep
= list
; ep
->prompt
; ep
++)
158 if (!ep
->restricted
) {
163 (void)fprintf(fp
, "###################################\n");
164 (void)fprintf(fp
, "# No fields are available to change\n");
165 (void)fprintf(fp
, "###################################\n");
167 #else /* DIRECTORY_SERVICE */
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"
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
);
193 /* Only admin can change "restricted" shells. */
194 else if (ok_shell(pw
->pw_shell
))
196 * Make shell a restricted field. Ugly with a
197 * necklace, but there's not much else to do.
199 (void)fprintf(fp
, "Shell: %s\n",
200 *pw
->pw_shell
? pw
->pw_shell
: _PATH_BSHELL
);
202 list
[E_SHELL
].restricted
= 1;
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 */
214 (void)fchown(fd
, getuid(), getgid());
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");
237 while (fgets(buf
, sizeof(buf
), fp
)) {
238 if (!buf
[0] || buf
[0] == '#')
240 if (!(p
= strchr(buf
, '\n'))) {
241 warnx("line too long");
245 for (ep
= list
;; ++ep
) {
247 warnx("unrecognized field");
250 if (!strncasecmp(buf
, ep
->prompt
, ep
->len
)) {
251 if (ep
->restricted
&& uid
) {
253 "you may not change the %s field",
257 if (!(p
= strchr(buf
, ':'))) {
258 warnx("line corrupted");
261 while (isspace(*++p
));
262 if (ep
->except
&& strpbrk(p
, ep
->except
)) {
264 "illegal character in the \"%s\" field",
268 if ((ep
->func
)(p
, pw
, ep
)) {
269 bad
: (void)fclose(fp
);
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
)))
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
291 pw
->pw_gecos
= list
[E_NAME
].save
;
294 #endif /* DIRECTORY_SERVICE */
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");
304 return (pw_scan(buf
, pw
, NULL
));