xref: /netbsd-src/usr.bin/chpass/chpass.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: chpass.c,v 1.5 1995/03/26 04:55:25 glass Exp $	*/
2 
3 /*-
4  * Copyright (c) 1988, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1988, 1993, 1994\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)chpass.c	8.4 (Berkeley) 4/2/94";
45 #else
46 static char rcsid[] = "$NetBSD: chpass.c,v 1.5 1995/03/26 04:55:25 glass Exp $";
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/signal.h>
53 #include <sys/time.h>
54 #include <sys/resource.h>
55 
56 #include <ctype.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <pwd.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 
66 #include <pw_scan.h>
67 #include <pw_util.h>
68 #include "pw_copy.h"
69 
70 #include "chpass.h"
71 #include "pathnames.h"
72 
73 char *progname = "chpass";
74 char *tempname;
75 uid_t uid;
76 
77 #ifdef	YP
78 int use_yp;
79 int force_yp = 0;
80 extern struct passwd *ypgetpwnam(), *ypgetpwuid();
81 #endif
82 
83 void	baduser __P((void));
84 void	usage __P((void));
85 
86 int
87 main(argc, argv)
88 	int argc;
89 	char **argv;
90 {
91 	enum { NEWSH, LOADENTRY, EDITENTRY } op;
92 	struct passwd *pw, lpw;
93 	int ch, pfd, tfd;
94 	char *arg;
95 
96 #ifdef	YP
97 	use_yp = _yp_check(NULL);
98 #endif
99 
100 	op = EDITENTRY;
101 	while ((ch = getopt(argc, argv, "a:s:ly")) != EOF)
102 		switch(ch) {
103 		case 'a':
104 			op = LOADENTRY;
105 			arg = optarg;
106 			break;
107 		case 's':
108 			op = NEWSH;
109 			arg = optarg;
110 			break;
111 #ifdef	YP
112 		case 'l':
113 			use_yp = 0;
114 			break;
115 		case 'y':
116 			if (!use_yp) {
117 				warnx("YP not in use.");
118 				usage();
119 			}
120 			force_yp = 1;
121 			break;
122 #endif
123 		case '?':
124 		default:
125 			usage();
126 		}
127 	argc -= optind;
128 	argv += optind;
129 
130 #ifdef	YP
131 	if (op == LOADENTRY && use_yp)
132 		errx(1, "cannot load entry using NIS.\n\tUse the -l flag to load local.");
133 #endif
134 	uid = getuid();
135 
136 	if (op == EDITENTRY || op == NEWSH)
137 		switch(argc) {
138 		case 0:
139 			pw = getpwuid(uid);
140 #ifdef	YP
141 			if (pw && !force_yp)
142 				use_yp = 0;
143 			else if (use_yp)
144 				pw = ypgetpwuid(uid);
145 #endif	/* YP */
146 			if (!pw)
147 				errx(1, "unknown user: uid %u\n", uid);
148 			break;
149 		case 1:
150 			pw = getpwnam(*argv);
151 #ifdef	YP
152 			if (pw && !force_yp)
153 				use_yp = 0;
154 			else if (use_yp)
155 				pw = ypgetpwnam(*argv);
156 #endif	/* YP */
157 			if (!pw)
158 				errx(1, "unknown user: %s", *argv);
159 			if (uid && uid != pw->pw_uid)
160 				baduser();
161 			break;
162 		default:
163 			usage();
164 		}
165 
166 	if (op == NEWSH) {
167 		/* protect p_shell -- it thinks NULL is /bin/sh */
168 		if (!arg[0])
169 			usage();
170 		if (p_shell(arg, pw, (ENTRY *)NULL))
171 			pw_error((char *)NULL, 0, 1);
172 	}
173 
174 	if (op == LOADENTRY) {
175 		if (uid)
176 			baduser();
177 		pw = &lpw;
178 		if (!pw_scan(arg, pw))
179 			exit(1);
180 	}
181 
182 	/*
183 	 * The temporary file/file descriptor usage is a little tricky here.
184 	 * 1:	We start off with two fd's, one for the master password
185 	 *	file (used to lock everything), and one for a temporary file.
186 	 * 2:	Display() gets an fp for the temporary file, and copies the
187 	 *	user's information into it.  It then gives the temporary file
188 	 *	to the user and closes the fp, closing the underlying fd.
189 	 * 3:	The user edits the temporary file some number of times.
190 	 * 4:	Verify() gets an fp for the temporary file, and verifies the
191 	 *	contents.  It can't use an fp derived from the step #2 fd,
192 	 *	because the user's editor may have created a new instance of
193 	 *	the file.  Once the file is verified, its contents are stored
194 	 *	in a password structure.  The verify routine closes the fp,
195 	 *	closing the underlying fd.
196 	 * 5:	Delete the temporary file.
197 	 * 6:	Get a new temporary file/fd.  Pw_copy() gets an fp for it
198 	 *	file and copies the master password file into it, replacing
199 	 *	the user record with a new one.  We can't use the first
200 	 *	temporary file for this because it was owned by the user.
201 	 *	Pw_copy() closes its fp, flushing the data and closing the
202 	 *	underlying file descriptor.  We can't close the master
203 	 *	password fp, or we'd lose the lock.
204 	 * 7:	Call pw_mkdb() (which renames the temporary file) and exit.
205 	 *	The exit closes the master passwd fp/fd.
206 	 */
207 	pw_init();
208 	pfd = pw_lock();
209 	tfd = pw_tmp();
210 
211 	if (op == EDITENTRY) {
212 		display(tfd, pw);
213 		edit(pw);
214 		(void)unlink(tempname);
215 		tfd = pw_tmp();
216 	}
217 
218 #ifdef	YP
219 	if (use_yp) {
220 		(void)unlink(tempname);
221 		if (pw_yp(pw, uid))
222 			pw_error((char *)NULL, 0, 1);
223 		else
224 			exit(0);
225 	}
226 	else
227 #endif	/* YP */
228 	pw_copy(pfd, tfd, pw);
229 
230 	if (!pw_mkdb())
231 		pw_error((char *)NULL, 0, 1);
232 
233 	exit(0);
234 }
235 
236 void
237 baduser()
238 {
239 
240 	errx(1, "%s", strerror(EACCES));
241 }
242 
243 void
244 usage()
245 {
246 
247 #ifdef	YP
248 	(void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [-l]%s [user]\n", use_yp?" [-y]":"");
249 #else
250 	(void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n");
251 #endif
252 	exit(1);
253 }
254