]>
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 char copyright
[] =
36 "@(#) Copyright (c) 1988, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid
[] = "From: @(#)chpass.c 8.4 (Berkeley) 4/2/94";
43 "$Id: chpass.c,v 1.9 1996/07/01 19:38:07 guido Exp $";
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"
77 void baduser
__P((void));
78 void usage
__P((void));
85 enum { NEWSH
, LOADENTRY
, EDITENTRY
, NEWPW
} op
;
86 struct passwd
*pw
, lpw
;
87 char *username
= NULL
;
97 while ((ch
= getopt(argc
, argv
, "a:p:s:d:h:oly")) != EOF
)
99 while ((ch
= getopt(argc
, argv
, "a:p:s:")) != EOF
)
118 warnx("Only the superuser can use the -h flag");
129 warnx("Only the superuser can use the -d flag");
133 if (yp_server
== NULL
)
134 yp_server
= "localhost";
144 _use_yp
= force_yp
= 1;
159 if (op
== EDITENTRY
|| op
== NEWSH
|| op
== NEWPW
)
164 get_yp_master(1); /* XXX just to set the suser flag */
168 get_yp_master(1); /* XXX just to set the suser flag */
171 if (!(pw
= getpwuid(uid
)))
172 errx(1, "unknown user: uid %u", uid
);
175 if (!(pw
= getpwnam(*argv
)))
176 errx(1, "unknown user: %s", *argv
);
178 if (uid
&& uid
!= pw
->pw_uid
)
185 /* protect p_shell -- it thinks NULL is /bin/sh */
188 if (p_shell(arg
, pw
, (ENTRY
*)NULL
))
189 pw_error((char *)NULL
, 0, 1);
192 if (op
== LOADENTRY
) {
196 if (!pw_scan(arg
, pw
))
199 username
= pw
->pw_name
;
205 if(strchr(arg
, ':')) {
206 errx(1, "invalid format for password");
212 * The temporary file/file descriptor usage is a little tricky here.
213 * 1: We start off with two fd's, one for the master password
214 * file (used to lock everything), and one for a temporary file.
215 * 2: Display() gets an fp for the temporary file, and copies the
216 * user's information into it. It then gives the temporary file
217 * to the user and closes the fp, closing the underlying fd.
218 * 3: The user edits the temporary file some number of times.
219 * 4: Verify() gets an fp for the temporary file, and verifies the
220 * contents. It can't use an fp derived from the step #2 fd,
221 * because the user's editor may have created a new instance of
222 * the file. Once the file is verified, its contents are stored
223 * in a password structure. The verify routine closes the fp,
224 * closing the underlying fd.
225 * 5: Delete the temporary file.
226 * 6: Get a new temporary file/fd. Pw_copy() gets an fp for it
227 * file and copies the master password file into it, replacing
228 * the user record with a new one. We can't use the first
229 * temporary file for this because it was owned by the user.
230 * Pw_copy() closes its fp, flushing the data and closing the
231 * underlying file descriptor. We can't close the master
232 * password fp, or we'd lose the lock.
233 * 7: Call pw_mkdb() (which renames the temporary file) and exit.
234 * The exit closes the master passwd fp/fd.
240 if (op
== EDITENTRY
) {
243 (void)unlink(tempname
);
250 (void)unlink(tempname
);
253 pw_copy(pfd
, tfd
, pw
);
255 if (!pw_mkdb(username
))
256 pw_error((char *)NULL
, 0, 1);
266 errx(1, "%s", strerror(EACCES
));
273 (void)fprintf(stderr
,
275 "usage: chpass [-l] [-y] [-d domain [-h host]] [-a list] [-p encpass] [-s shell] [user]\n");
277 "usage: chpass [-a list] [-p encpass] [-s shell] [user]\n");