xref: /freebsd/usr.bin/chpass/chpass.c (revision 2ad872c5794e4c26fdf6ed219ad3f09ca0d5304a)
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
35 static const 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
41 static const char sccsid[] = "From: @(#)chpass.c	8.4 (Berkeley) 4/2/94";
42 static const char rcsid[] =
43 	"$Id: chpass.c,v 1.14 1997/03/29 04:28:38 imp 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 <rpcsvc/yp.h>
67 int yp_errno = YP_TRUE;
68 #include "pw_yp.h"
69 #endif
70 
71 #include "chpass.h"
72 #include "pathnames.h"
73 
74 char *tempname;
75 uid_t uid;
76 
77 void	baduser __P((void));
78 void	usage __P((void));
79 
80 int
81 main(argc, argv)
82 	int argc;
83 	char **argv;
84 {
85 	enum { NEWSH, LOADENTRY, EDITENTRY, NEWPW, NEWEXP } op;
86 	struct passwd *pw = NULL, lpw;
87 	char *username = NULL;
88 	int ch, pfd, tfd;
89 	char *arg = NULL;
90 #ifdef YP
91 	int force_local = 0;
92 	int force_yp = 0;
93 #endif
94 
95 	op = EDITENTRY;
96 #ifdef YP
97 	while ((ch = getopt(argc, argv, "a:p:s:e:d:h:oly")) != -1)
98 #else
99 	while ((ch = getopt(argc, argv, "a:p:s:e:")) != -1)
100 #endif
101 		switch(ch) {
102 		case 'a':
103 			op = LOADENTRY;
104 			arg = optarg;
105 			break;
106 		case 's':
107 			op = NEWSH;
108 			arg = optarg;
109 			break;
110 		case 'p':
111 			op = NEWPW;
112 			arg = optarg;
113 			break;
114 		case 'e':
115 			op = NEWEXP;
116 			arg = optarg;
117 			break;
118 #ifdef YP
119 		case 'h':
120 #ifdef PARANOID
121 			if (getuid()) {
122 				warnx("Only the superuser can use the -h flag");
123 			} else {
124 #endif
125 				yp_server = optarg;
126 #ifdef PARANOID
127 			}
128 #endif
129 			break;
130 		case 'd':
131 #ifdef PARANOID
132 			if (getuid()) {
133 				warnx("Only the superuser can use the -d flag");
134 			} else {
135 #endif
136 				yp_domain = optarg;
137 				if (yp_server == NULL)
138 					yp_server = "localhost";
139 #ifdef PARANOID
140 			}
141 #endif
142 			break;
143 		case 'l':
144 			_use_yp = 0;
145 			force_local = 1;
146 			break;
147 		case 'y':
148 			_use_yp = force_yp = 1;
149 			break;
150 		case 'o':
151 			force_old++;
152 			break;
153 #endif
154 		case '?':
155 		default:
156 			usage();
157 		}
158 	argc -= optind;
159 	argv += optind;
160 
161 	uid = getuid();
162 
163 	if (op == EDITENTRY || op == NEWSH || op == NEWPW || op == NEWEXP)
164 		switch(argc) {
165 #ifdef YP
166 		case 0:
167 			GETPWUID(uid)
168 			get_yp_master(1); /* XXX just to set the suser flag */
169 			break;
170 		case 1:
171 			GETPWNAM(*argv)
172 			get_yp_master(1); /* XXX just to set the suser flag */
173 #else
174 		case 0:
175 			if (!(pw = getpwuid(uid)))
176 				errx(1, "unknown user: uid %u", uid);
177 			break;
178 		case 1:
179 			if (!(pw = getpwnam(*argv)))
180 				errx(1, "unknown user: %s", *argv);
181 #endif
182 			if (uid && uid != pw->pw_uid)
183 				baduser();
184 			break;
185 		default:
186 			usage();
187 		}
188 	if (op == NEWSH) {
189 		/* protect p_shell -- it thinks NULL is /bin/sh */
190 		if (!arg[0])
191 			usage();
192 		if (p_shell(arg, pw, (ENTRY *)NULL))
193 			pw_error((char *)NULL, 0, 1);
194 	}
195 
196 	if (op == NEWEXP) {
197 		if (uid)	/* only root can change expire */
198 			baduser();
199 		if (p_expire(arg, pw, (ENTRY *)NULL))
200 			pw_error((char *)NULL, 0, 1);
201 	}
202 
203 	if (op == LOADENTRY) {
204 		if (uid)
205 			baduser();
206 		pw = &lpw;
207 		if (!pw_scan(arg, pw))
208 			exit(1);
209 	}
210 	username = pw->pw_name;
211 
212 	if (op == NEWPW) {
213 		if (uid)
214 			baduser();
215 
216 		if(strchr(arg, ':')) {
217 			errx(1, "invalid format for password");
218 		}
219 		pw->pw_passwd = arg;
220 	}
221 
222 	/*
223 	 * The temporary file/file descriptor usage is a little tricky here.
224 	 * 1:	We start off with two fd's, one for the master password
225 	 *	file (used to lock everything), and one for a temporary file.
226 	 * 2:	Display() gets an fp for the temporary file, and copies the
227 	 *	user's information into it.  It then gives the temporary file
228 	 *	to the user and closes the fp, closing the underlying fd.
229 	 * 3:	The user edits the temporary file some number of times.
230 	 * 4:	Verify() gets an fp for the temporary file, and verifies the
231 	 *	contents.  It can't use an fp derived from the step #2 fd,
232 	 *	because the user's editor may have created a new instance of
233 	 *	the file.  Once the file is verified, its contents are stored
234 	 *	in a password structure.  The verify routine closes the fp,
235 	 *	closing the underlying fd.
236 	 * 5:	Delete the temporary file.
237 	 * 6:	Get a new temporary file/fd.  Pw_copy() gets an fp for it
238 	 *	file and copies the master password file into it, replacing
239 	 *	the user record with a new one.  We can't use the first
240 	 *	temporary file for this because it was owned by the user.
241 	 *	Pw_copy() closes its fp, flushing the data and closing the
242 	 *	underlying file descriptor.  We can't close the master
243 	 *	password fp, or we'd lose the lock.
244 	 * 7:	Call pw_mkdb() (which renames the temporary file) and exit.
245 	 *	The exit closes the master passwd fp/fd.
246 	 */
247 	pw_init();
248 	pfd = pw_lock();
249 	tfd = pw_tmp();
250 
251 	if (op == EDITENTRY) {
252 		display(tfd, pw);
253 		edit(pw);
254 		(void)unlink(tempname);
255 		tfd = pw_tmp();
256 	}
257 
258 #ifdef YP
259 	if (_use_yp) {
260 		yp_submit(pw);
261 		(void)unlink(tempname);
262 	} else {
263 #endif /* YP */
264 	pw_copy(pfd, tfd, pw);
265 
266 	if (!pw_mkdb(username))
267 		pw_error((char *)NULL, 0, 1);
268 #ifdef YP
269 	}
270 #endif /* YP */
271 	exit(0);
272 }
273 
274 void
275 baduser()
276 {
277 	errx(1, "%s", strerror(EACCES));
278 }
279 
280 void
281 usage()
282 {
283 
284 	(void)fprintf(stderr,
285 #ifdef YP
286 		"usage: chpass [-l] [-y] [-d domain [-h host]] [-a list] [-p encpass] [-s shell] [-e mmm dd yy] [user]\n");
287 #else
288 		"usage: chpass [-a list] [-p encpass] [-s shell] [-e mmm dd yy] [user]\n");
289 #endif
290 	exit(1);
291 }
292