]>
git.cameronkatri.com Git - pw-darwin.git/blob - chpass/chpass.c
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright
[] =
36 "@(#) Copyright (c) 1988, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid
[] = "From: @(#)chpass.c 8.4 (Berkeley) 4/2/94";
42 static const char rcsid
[] =
46 #include <sys/param.h>
48 #include <sys/signal.h>
50 #include <sys/resource.h>
66 #include <rpcsvc/yp.h>
67 int yp_errno
= YP_TRUE
;
72 #include "pathnames.h"
85 enum { NEWSH
, LOADENTRY
, EDITENTRY
, NEWPW
, NEWEXP
} op
;
86 struct passwd
*pw
= NULL
, lpw
, old_pw
;
87 char *username
= NULL
;
97 while ((ch
= getopt(argc
, argv
, "a:p:s:e:d:h:oly")) != -1)
99 while ((ch
= getopt(argc
, argv
, "a:p:s:e:")) != -1)
122 warnx("Only the superuser can use the -h flag");
133 warnx("Only the superuser can use the -d flag");
137 if (yp_server
== NULL
)
138 yp_server
= "localhost";
148 _use_yp
= force_yp
= 1;
163 if (op
== EDITENTRY
|| op
== NEWSH
|| op
== NEWPW
|| op
== NEWEXP
) {
168 get_yp_master(1); /* XXX just to set the suser flag */
172 get_yp_master(1); /* XXX just to set the suser flag */
175 if (!(pw
= getpwuid(uid
)))
176 errx(1, "unknown user: uid %lu",
180 if (!(pw
= getpwnam(*argv
)))
181 errx(1, "unknown user: %s", *argv
);
183 if (uid
&& uid
!= pw
->pw_uid
)
190 /* Make a copy for later verification */
192 old_pw
.pw_gecos
= strdup(old_pw
.pw_gecos
);
196 /* protect p_shell -- it thinks NULL is /bin/sh */
199 if (p_shell(arg
, pw
, (ENTRY
*)NULL
))
200 pw_error((char *)NULL
, 0, 1);
204 if (uid
) /* only root can change expire */
206 if (p_expire(arg
, pw
, (ENTRY
*)NULL
))
207 pw_error((char *)NULL
, 0, 1);
210 if (op
== LOADENTRY
) {
214 if (!__pw_scan(arg
, pw
, _PWSCAN_WARN
|_PWSCAN_MASTER
))
217 username
= pw
->pw_name
;
223 if(strchr(arg
, ':')) {
224 errx(1, "invalid format for password");
230 * The temporary file/file descriptor usage is a little tricky here.
231 * 1: Create a temporary file called tempname, get descriptor tfd.
232 * 2: Display() gets an fp for the temporary file, and copies the
233 * user's information into it. It then gives the temporary file
234 * to the user and closes the fp, closing the underlying fd.
235 * 3: The user edits the temporary file some number of times.
236 * The results are stored in pw by edit().
237 * 4: Delete the temporary file.
238 * 5: Make a new temporary file, descriptor tfd.
239 * 6: Get a descriptor for the master.passwd file, pfd, and
240 * lock master.passwd.
241 * 7: Pw_copy() gets descriptors for master.passwd and the
242 * temporary file and copies the master password file into it,
243 * replacing the modified user's record with a new one. We can't
244 * use the first temporary file for this because it was owned
245 * by the user. Pass the new and old user info. Check the
246 * entry for our user has not been changed by someone else by
247 * while the user was editing by comparing the old info to
248 * the entry freshly read from master.passwd. Pw_copy() closes
249 * its fp, flushing the data and closing the underlying file
250 * descriptor. We can't close the master password fp, or we'd
252 * 8: Call pw_mkdb() (which renames the temporary file) and exit.
253 * The exit closes the master passwd fp/fd.
258 if (op
== EDITENTRY
) {
261 (void)unlink(tempname
);
268 (void)unlink(tempname
);
272 pw_copy(pfd
, tfd
, pw
, (op
== LOADENTRY
) ? NULL
: &old_pw
);
274 if (!pw_mkdb(username
))
275 pw_error((char *)NULL
, 0, 1);
285 errx(1, "%s", strerror(EACCES
));
292 (void)fprintf(stderr
,
294 "usage: chpass [-l] [-y] [-d domain [-h host]] [-a list] [-p encpass] [-s shell] [-e mmm dd yy] [user]\n");
296 "usage: chpass [-a list] [-p encpass] [-s shell] [-e mmm dd yy] [user]\n");