chpass.c revision 10050
1/*-
2 * Copyright (c) 1988, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
36"@(#) Copyright (c) 1988, 1993, 1994\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "From: @(#)chpass.c	8.4 (Berkeley) 4/2/94";
42static char rcsid[] =
43	"$Id: chpass.c,v 1.3 1995/05/30 06:29:36 rgrimes Exp $";
44#endif /* not lint */
45
46#include <sys/param.h>
47#include <sys/stat.h>
48#include <sys/signal.h>
49#include <sys/time.h>
50#include <sys/resource.h>
51
52#include <ctype.h>
53#include <err.h>
54#include <errno.h>
55#include <fcntl.h>
56#include <pwd.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62#include <pw_scan.h>
63#include <pw_util.h>
64#include "pw_copy.h"
65#ifdef YP
66#include "pw_yp.h"
67#endif
68
69#include "chpass.h"
70#include "pathnames.h"
71
72char *progname = "chpass";
73char *tempname;
74uid_t uid;
75
76void	baduser __P((void));
77void	usage __P((void));
78
79int
80main(argc, argv)
81	int argc;
82	char **argv;
83{
84	enum { NEWSH, LOADENTRY, EDITENTRY, NEWPW } op;
85	struct passwd *pw, lpw;
86	int ch, pfd, tfd;
87	char *arg;
88
89	op = EDITENTRY;
90	while ((ch = getopt(argc, argv, "a:p:s:")) != EOF)
91		switch(ch) {
92		case 'a':
93			op = LOADENTRY;
94			arg = optarg;
95			break;
96		case 's':
97			op = NEWSH;
98			arg = optarg;
99			break;
100		case 'p':
101			op = NEWPW;
102			arg = optarg;
103			break;
104		case '?':
105		default:
106			usage();
107		}
108	argc -= optind;
109	argv += optind;
110
111	uid = getuid();
112
113	if (op == EDITENTRY || op == NEWSH || op == NEWPW)
114		switch(argc) {
115		case 0:
116			if (!(pw = getpwuid(uid)))
117				errx(1, "unknown user: uid %u", uid);
118			break;
119		case 1:
120			if (!(pw = getpwnam(*argv)))
121				errx(1, "unknown user: %s", *argv);
122			if (uid && uid != pw->pw_uid)
123				baduser();
124			break;
125		default:
126			usage();
127		}
128	if (op == NEWSH) {
129		/* protect p_shell -- it thinks NULL is /bin/sh */
130		if (!arg[0])
131			usage();
132		if (p_shell(arg, pw, (ENTRY *)NULL))
133			pw_error((char *)NULL, 0, 1);
134	}
135
136	if (op == LOADENTRY) {
137		if (uid)
138			baduser();
139		pw = &lpw;
140		if (!pw_scan(arg, pw))
141			exit(1);
142	}
143
144	if (op == NEWPW) {
145		if (uid)
146			baduser();
147
148		if(strchr(arg, ':')) {
149			errx(1, "invalid format for password");
150		}
151		pw->pw_passwd = arg;
152	}
153
154#ifdef YP
155	/*
156	 * XXX The man page says the data returned by getpwent()
157	 * and friends is stored in static buffers that may be
158	 * overwritten after successive invokations. Unfortunately,
159	 * we need to call getpwent() more than once with NIS
160	 * enabled.
161	 */
162	pw->pw_name = strdup(pw->pw_name);
163	pw->pw_passwd = strdup(pw->pw_passwd);
164	pw->pw_class = strdup(pw->pw_class);
165	pw->pw_gecos = strdup(pw->pw_gecos);
166	pw->pw_shell = strdup(pw->pw_shell);
167	pw->pw_dir = strdup(pw->pw_dir);
168	_use_yp = use_yp(pw->pw_name);
169#endif /* YP */
170
171	/*
172	 * The temporary file/file descriptor usage is a little tricky here.
173	 * 1:	We start off with two fd's, one for the master password
174	 *	file (used to lock everything), and one for a temporary file.
175	 * 2:	Display() gets an fp for the temporary file, and copies the
176	 *	user's information into it.  It then gives the temporary file
177	 *	to the user and closes the fp, closing the underlying fd.
178	 * 3:	The user edits the temporary file some number of times.
179	 * 4:	Verify() gets an fp for the temporary file, and verifies the
180	 *	contents.  It can't use an fp derived from the step #2 fd,
181	 *	because the user's editor may have created a new instance of
182	 *	the file.  Once the file is verified, its contents are stored
183	 *	in a password structure.  The verify routine closes the fp,
184	 *	closing the underlying fd.
185	 * 5:	Delete the temporary file.
186	 * 6:	Get a new temporary file/fd.  Pw_copy() gets an fp for it
187	 *	file and copies the master password file into it, replacing
188	 *	the user record with a new one.  We can't use the first
189	 *	temporary file for this because it was owned by the user.
190	 *	Pw_copy() closes its fp, flushing the data and closing the
191	 *	underlying file descriptor.  We can't close the master
192	 *	password fp, or we'd lose the lock.
193	 * 7:	Call pw_mkdb() (which renames the temporary file) and exit.
194	 *	The exit closes the master passwd fp/fd.
195	 */
196	pw_init();
197	pfd = pw_lock();
198	tfd = pw_tmp();
199
200	if (op == EDITENTRY) {
201		display(tfd, pw);
202		edit(pw);
203		(void)unlink(tempname);
204		tfd = pw_tmp();
205	}
206
207#ifdef YP
208	if (_use_yp) {
209		yp_submit(pw);
210		(void)unlink(tempname);
211	} else {
212#endif /* YP */
213	pw_copy(pfd, tfd, pw);
214
215	if (!pw_mkdb())
216		pw_error((char *)NULL, 0, 1);
217#ifdef YP
218	}
219#endif /* YP */
220	exit(0);
221}
222
223void
224baduser()
225{
226	errx(1, "%s", strerror(EACCES));
227}
228
229void
230usage()
231{
232
233	(void)fprintf(stderr,
234		"usage: chpass [-a list] [-p encpass] [-s shell] [user]\n");
235	exit(1);
236}
237