]>
git.saurik.com Git - apple/system_cmds.git/blob - chpass.tproj/edit.c
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>
76 extern char *tempname
;
82 struct stat begin
, end
;
85 if (stat(tempname
, &begin
))
86 pw_error(tempname
, 1, 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");
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.
111 char *bp
, *p
, *ttoa();
113 if (!(fp
= fdopen(fd
, "w")))
114 pw_error(tempname
, 1, 1);
117 "#Changing user database information for %s.\n", pw
->pw_name
);
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
);
132 /* Only admin can change "restricted" shells. */
133 else if (ok_shell(pw
->pw_shell
))
135 * Make shell a restricted field. Ugly with a
136 * necklace, but there's not much else to do.
138 (void)fprintf(fp
, "Shell: %s\n",
139 *pw
->pw_shell
? pw
->pw_shell
: _PATH_BSHELL
);
141 list
[E_SHELL
].restricted
= 1;
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
: "");
152 (void)fchown(fd
, getuid(), getgid());
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");
175 while (fgets(buf
, sizeof(buf
), fp
)) {
176 if (!buf
[0] || buf
[0] == '#')
178 if (!(p
= strchr(buf
, '\n'))) {
179 warnx("line too long");
183 for (ep
= list
;; ++ep
) {
185 warnx("unrecognized field");
188 if (!strncasecmp(buf
, ep
->prompt
, ep
->len
)) {
189 if (ep
->restricted
&& uid
) {
191 "you may not change the %s field",
195 if (!(p
= strchr(buf
, ':'))) {
196 warnx("line corrupted");
199 while (isspace(*++p
));
200 if (ep
->except
&& strpbrk(p
, ep
->except
)) {
202 "illegal character in the \"%s\" field",
206 if ((ep
->func
)(p
, pw
, ep
)) {
207 bad
: (void)fclose(fp
);
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
)))
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
);
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");
232 return (pw_scan(buf
, pw
));