]>
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 * "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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1990, 1993, 1994
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
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.
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
57 #include <sys/param.h>
74 #ifdef DIRECTORY_SERVICE
75 #include "directory_service.h"
78 #endif /* DIRECTORY_SERVICE */
80 extern char *tempname
;
86 struct stat begin
, end
;
89 if (stat(tempname
, &begin
))
90 pw_error(tempname
, 1, 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");
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.
115 char *bp
, *p
, *ttoa();
116 #ifdef DIRECTORY_SERVICE
120 #endif /* DIRECTORY_SERVICE */
122 if (!(fp
= fdopen(fd
, "w")))
123 pw_error(tempname
, 1, 1);
126 "# Changing user database information for %s.\n"
128 "# (use \"passwd\" to change the password)\n"
132 #ifdef DIRECTORY_SERVICE
136 "# Flat file: /etc/master.passwd\n"
141 "# Local NetInfo Database\n"
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
: "");
156 for (ep
= list
; ep
->prompt
; ep
++)
157 if (!ep
->restricted
) {
162 (void)fprintf(fp
, "###################################\n");
163 (void)fprintf(fp
, "# No fields are available to change\n");
164 (void)fprintf(fp
, "###################################\n");
166 #else /* DIRECTORY_SERVICE */
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"
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
);
192 /* Only admin can change "restricted" shells. */
193 else if (ok_shell(pw
->pw_shell
))
195 * Make shell a restricted field. Ugly with a
196 * necklace, but there's not much else to do.
198 (void)fprintf(fp
, "Shell: %s\n",
199 *pw
->pw_shell
? pw
->pw_shell
: _PATH_BSHELL
);
201 list
[E_SHELL
].restricted
= 1;
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 */
213 (void)fchown(fd
, getuid(), getgid());
226 static char buf
[LINE_MAX
];
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");
236 while (fgets(buf
, sizeof(buf
), fp
)) {
237 if (!buf
[0] || buf
[0] == '#')
239 if (!(p
= strchr(buf
, '\n'))) {
240 warnx("line too long");
244 for (ep
= list
;; ++ep
) {
246 warnx("unrecognized field");
249 if (!strncasecmp(buf
, ep
->prompt
, ep
->len
)) {
250 if (ep
->restricted
&& uid
) {
252 "you may not change the %s field",
256 if (!(p
= strchr(buf
, ':'))) {
257 warnx("line corrupted");
260 while (isspace(*++p
));
261 if (ep
->except
&& strpbrk(p
, ep
->except
)) {
263 "illegal character in the \"%s\" field",
267 if ((ep
->func
)(p
, pw
, ep
)) {
268 bad
: (void)fclose(fp
);
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
)))
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
290 pw
->pw_gecos
= list
[E_NAME
].save
;
293 #endif /* DIRECTORY_SERVICE */
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");
303 return (pw_scan(buf
, pw
, NULL
));