]>
git.cameronkatri.com Git - pw-darwin.git/blob - chpass/edit.c
2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 2002 Networks Associates Technology, Inc.
9 * Portions of this software were developed for the FreeBSD Project by
10 * ThinkSec AS and NAI Labs, the Security Research Division of Network
11 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
12 * ("CBOSS"), as part of the DARPA CHATS research program.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 static char sccsid
[] = "@(#)edit.c 8.3 (Berkeley) 4/2/94";
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
52 #include <sys/param.h>
70 static int display(const char *tfn
, struct passwd
*pw
);
71 static struct passwd
*verify(const char *tfn
, struct passwd
*pw
);
74 edit(const char *tfn
, struct passwd
*pw
)
80 if (display(tfn
, pw
) == -1)
91 if ((npw
= verify(tfn
, pw
)) != NULL
)
94 printf("re-edit the password file? ");
96 if ((line
= fgetln(stdin
, &len
)) == NULL
) {
100 if (len
> 0 && (*line
== 'N' || *line
== 'n'))
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.
111 display(const char *tfn
, struct passwd
*pw
)
114 char *bp
, *gecos
, *p
;
116 if ((fp
= fopen(tfn
, "w")) == NULL
) {
122 "#Changing user information for %s.\n", pw
->pw_name
);
124 (void)fprintf(fp
, "Login: %s\n", pw
->pw_name
);
125 (void)fprintf(fp
, "Password: %s\n", pw
->pw_passwd
);
126 (void)fprintf(fp
, "Uid [#]: %lu\n", (unsigned long)pw
->pw_uid
);
127 (void)fprintf(fp
, "Gid [# or name]: %lu\n",
128 (unsigned long)pw
->pw_gid
);
129 (void)fprintf(fp
, "Change [month day year]: %s\n",
130 ttoa(pw
->pw_change
));
131 (void)fprintf(fp
, "Expire [month day year]: %s\n",
132 ttoa(pw
->pw_expire
));
133 (void)fprintf(fp
, "Class: %s\n", pw
->pw_class
);
134 (void)fprintf(fp
, "Home directory: %s\n", pw
->pw_dir
);
135 (void)fprintf(fp
, "Shell: %s\n",
136 *pw
->pw_shell
? pw
->pw_shell
: _PATH_BSHELL
);
138 /* Only admin can change "restricted" shells. */
140 else if (ok_shell(pw
->pw_shell
))
142 * Make shell a restricted field. Ugly with a
143 * necklace, but there's not much else to do.
146 else if ((!list
[E_SHELL
].restricted
&& ok_shell(pw
->pw_shell
)) ||
149 * If change not restrict (table.c) and standard shell
150 * OR if root, then allow editing of shell.
153 (void)fprintf(fp
, "Shell: %s\n",
154 *pw
->pw_shell
? pw
->pw_shell
: _PATH_BSHELL
);
156 list
[E_SHELL
].restricted
= 1;
158 if ((bp
= gecos
= strdup(pw
->pw_gecos
)) == NULL
) {
164 p
= strsep(&bp
, ",");
165 p
= strdup(p
? p
: "");
166 list
[E_NAME
].save
= p
;
167 if (!list
[E_NAME
].restricted
|| master_mode
)
168 (void)fprintf(fp
, "Full Name: %s\n", p
);
170 p
= strsep(&bp
, ",");
171 p
= strdup(p
? p
: "");
172 list
[E_LOCATE
].save
= p
;
173 if (!list
[E_LOCATE
].restricted
|| master_mode
)
174 (void)fprintf(fp
, "Office Location: %s\n", p
);
176 p
= strsep(&bp
, ",");
177 p
= strdup(p
? p
: "");
178 list
[E_BPHONE
].save
= p
;
179 if (!list
[E_BPHONE
].restricted
|| master_mode
)
180 (void)fprintf(fp
, "Office Phone: %s\n", p
);
182 p
= strsep(&bp
, ",");
183 p
= strdup(p
? p
: "");
184 list
[E_HPHONE
].save
= p
;
185 if (!list
[E_HPHONE
].restricted
|| master_mode
)
186 (void)fprintf(fp
, "Home Phone: %s\n", p
);
188 bp
= strdup(bp
? bp
: "");
189 list
[E_OTHER
].save
= bp
;
190 if (!list
[E_OTHER
].restricted
|| master_mode
)
191 (void)fprintf(fp
, "Other information: %s\n", bp
);
195 (void)fchown(fileno(fp
), getuid(), getgid());
200 static struct passwd
*
201 verify(const char *tfn
, struct passwd
*pw
)
211 if ((pw
= pw_dup(pw
)) == NULL
)
213 if ((fp
= fopen(tfn
, "r")) == NULL
||
214 fstat(fileno(fp
), &sb
) == -1) {
219 if (sb
.st_size
== 0) {
220 warnx("corrupted temporary file");
226 for (line
= 1; (buf
= fgetln(fp
, &len
)) != NULL
; ++line
) {
227 if (*buf
== '\0' || *buf
== '#')
229 while (len
> 0 && isspace(buf
[len
- 1]))
231 for (ep
= list
;; ++ep
) {
233 warnx("%s: unrecognized field on line %d",
239 if (strncasecmp(buf
, ep
->prompt
, ep
->len
) != 0)
241 if (ep
->restricted
&& !master_mode
) {
242 warnx("%s: you may not change the %s field",
246 for (p
= buf
; p
< buf
+ len
&& *p
!= ':'; ++p
)
249 warnx("%s: line %d corrupted", tfn
, line
);
252 while (++p
< buf
+ len
&& isspace(*p
))
255 asprintf(&val
, "%.*s", (int)(buf
+ len
- p
), p
);
258 if (ep
->except
&& strpbrk(val
, ep
->except
)) {
259 warnx("%s: invalid character in \"%s\" field '%s'",
260 tfn
, ep
->prompt
, val
);
263 if ((ep
->func
)(val
, pw
, ep
))
271 /* Build the gecos field. */
272 len
= asprintf(&p
, "%s,%s,%s,%s,%s", list
[E_NAME
].save
,
273 list
[E_LOCATE
].save
, list
[E_BPHONE
].save
,
274 list
[E_HPHONE
].save
, list
[E_OTHER
].save
);
280 while (len
> 0 && p
[len
- 1] == ',')
290 npw
= pw_scan(buf
, PWSCAN_WARN
|PWSCAN_MASTER
);