xref: /openbsd-src/usr.bin/chpass/chpass.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: chpass.c,v 1.36 2008/07/08 21:30:15 sobrado Exp $	*/
2 /*	$NetBSD: chpass.c,v 1.8 1996/05/15 21:50:43 jtc Exp $	*/
3 
4 /*-
5  * Copyright (c) 1988, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 static char copyright[] =
35 "@(#) Copyright (c) 1988, 1993, 1994\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)chpass.c	8.4 (Berkeley) 4/2/94";
42 #else
43 static char rcsid[] = "$OpenBSD: chpass.c,v 1.36 2008/07/08 21:30:15 sobrado Exp $";
44 #endif
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 #include <sys/resource.h>
49 #include <sys/stat.h>
50 #include <sys/time.h>
51 #include <sys/uio.h>
52 
53 #include <err.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <paths.h>
57 #include <pwd.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <util.h>
64 
65 #include "chpass.h"
66 
67 extern char *__progname;
68 
69 enum { NEWSH, LOADENTRY, EDITENTRY } op;
70 uid_t uid;
71 #ifdef	YP
72 int	use_yp;
73 int	force_yp = 0;
74 #endif
75 
76 void	baduser(void);
77 void	kbintr(int);
78 void	usage(void);
79 
80 int
81 main(int argc, char *argv[])
82 {
83 	struct passwd *pw = NULL, *opw = NULL, lpw;
84 	int i, ch, pfd, tfd, dfd;
85 	char *tz, *arg = NULL;
86 	sigset_t fullset;
87 
88 #ifdef	YP
89 	use_yp = _yp_check(NULL);
90 #endif
91 	/* We need to use the system timezone for date conversions. */
92 	if ((tz = getenv("TZ")) != NULL) {
93 	    unsetenv("TZ");
94 	    tzset();
95 	    setenv("TZ", tz, 1);
96 	}
97 
98 	op = EDITENTRY;
99 	while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
100 		switch(ch) {
101 		case 'a':
102 			op = LOADENTRY;
103 			arg = optarg;
104 			break;
105 		case 's':
106 			op = NEWSH;
107 			arg = optarg;
108 			break;
109 #ifdef	YP
110 		case 'l':
111 			use_yp = 0;
112 			break;
113 		case 'y':
114 			if (!use_yp) {
115 				warnx("YP not in use.");
116 				usage();
117 			}
118 			force_yp = 1;
119 			break;
120 #endif
121 		case '?':
122 		default:
123 			usage();
124 		}
125 	argc -= optind;
126 	argv += optind;
127 
128 #ifdef	YP
129 	if (op == LOADENTRY && use_yp)
130 		errx(1, "cannot load using YP, use -l to load local.");
131 #endif
132 	uid = getuid();
133 
134 	if (op == EDITENTRY || op == NEWSH)
135 		switch(argc) {
136 		case 0:
137 			pw = getpwuid(uid);
138 #ifdef	YP
139 			if (pw && !force_yp)
140 				use_yp = 0;
141 			else if (use_yp)
142 				pw = ypgetpwuid(uid);
143 #endif	/* YP */
144 			if (!pw)
145 				errx(1, "unknown user: uid %u", uid);
146 			break;
147 		case 1:
148 			pw = getpwnam(*argv);
149 #ifdef	YP
150 			if (pw && !force_yp)
151 				use_yp = 0;
152 			else if (use_yp)
153 				pw = ypgetpwnam(*argv);
154 #endif	/* YP */
155 			if (!pw)
156 				errx(1, "unknown user: %s", *argv);
157 			if (uid && uid != pw->pw_uid)
158 				baduser();
159 			break;
160 		default:
161 			usage();
162 		}
163 
164 	if (op == LOADENTRY) {
165 		if (argc != 0)
166 			errx(1, "option -a does not accept user argument");
167 		if (uid)
168 			baduser();
169 		pw = &lpw;
170 		if (!pw_scan(arg, pw, NULL))
171 			exit(1);
172 		opw = getpwnam(pw->pw_name);
173 	}
174 	if (opw == NULL && (opw = pw_dup(pw)) == NULL)
175 		err(1, NULL);
176 
177 	/* Edit the user passwd information if requested. */
178 	if (op == EDITENTRY) {
179 		char tempname[] = _PATH_VARTMP "pw.XXXXXXXXXX";
180 		int edit_status;
181 
182 		if ((pw = pw_dup(pw)) == NULL)
183 			pw_error(NULL, 1, 1);
184 		dfd = mkstemp(tempname);
185 		if (dfd == -1 || fcntl(dfd, F_SETFD, 1) == -1)
186 			pw_error(tempname, 1, 1);
187 		display(tempname, dfd, pw);
188 		edit_status = edit(tempname, pw);
189 		close(dfd);
190 		unlink(tempname);
191 
192 		switch (edit_status) {
193 		case EDIT_OK:
194 			break;
195 		case EDIT_NOCHANGE:
196 			pw_error(NULL, 0, 0);
197 			break;
198 		case EDIT_ERROR:
199 		default:
200 			pw_error(tempname, 1, 1);
201 			break;
202 		}
203 	}
204 
205 	if (op == NEWSH) {
206 		/* protect p_shell -- it thinks NULL is /bin/sh */
207 		if (!arg[0])
208 			usage();
209 		if (p_shell(arg, pw, NULL))
210 			pw_error(NULL, 0, 1);
211 	}
212 
213 	/* Drop user's real uid and block all signals to avoid a DoS. */
214 	setuid(0);
215 	sigfillset(&fullset);
216 	sigdelset(&fullset, SIGINT);
217 	sigprocmask(SIG_BLOCK, &fullset, NULL);
218 
219 	/* Get the passwd lock file and open the passwd file for reading. */
220 	pw_init();
221 	for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
222 		if (i == 4)
223 			(void)fputs("Attempting lock password file, "
224 			    "please wait or press ^C to abort", stderr);
225 		(void)signal(SIGINT, kbintr);
226 		if (i % 16 == 0)
227 			fputc('.', stderr);
228 		usleep(250000);
229 		(void)signal(SIGINT, SIG_IGN);
230 	}
231 	if (i >= 4)
232 		fputc('\n', stderr);
233 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
234 	if (pfd == -1 || fcntl(pfd, F_SETFD, 1) == -1)
235 		pw_error(_PATH_MASTERPASSWD, 1, 1);
236 
237 #ifdef	YP
238 	if (use_yp) {
239 		if (pw_yp(pw, uid))
240 			pw_error(NULL, 0, 1);
241 		else {
242 			pw_abort();
243 			exit(0);
244 		}
245 	} else
246 #endif	/* YP */
247 	{
248 		/* Copy the passwd file to the lock file, updating pw. */
249 		pw_copy(pfd, tfd, pw, opw);
250 
251 		/* If username changed we need to rebuild the entire db. */
252 		arg = !strcmp(opw->pw_name, pw->pw_name) ? pw->pw_name : NULL;
253 
254 		/* Now finish the passwd file update. */
255 		if (pw_mkdb(arg, 0) == -1)
256 			pw_error(NULL, 0, 1);
257 	}
258 
259 	exit(0);
260 }
261 
262 void
263 baduser(void)
264 {
265 
266 	errx(1, "%s", strerror(EACCES));
267 }
268 
269 /* ARGSUSED */
270 void
271 kbintr(int signo)
272 {
273 	struct iovec iv[5];
274 
275 	iv[0].iov_base = "\n";
276 	iv[0].iov_len = 1;
277 	iv[1].iov_base = __progname;
278 	iv[1].iov_len = strlen(__progname);
279 	iv[2].iov_base = ": ";
280 	iv[2].iov_len = 2;
281 	iv[3].iov_base = _PATH_MASTERPASSWD;
282 	iv[3].iov_len = sizeof(_PATH_MASTERPASSWD) - 1;
283 	iv[4].iov_base = " unchanged\n";
284 	iv[4].iov_len = 11;
285 	writev(STDERR_FILENO, iv, 5);
286 
287 	_exit(1);
288 }
289 
290 void
291 usage(void)
292 {
293 
294 #ifdef	YP
295 	(void)fprintf(stderr,
296 	    "usage: %s [-l%s] [-s newshell] [user]\n",
297 	    __progname, use_yp ? "y" : "");
298 	(void)fprintf(stderr,
299 	    "       %s [-l] -a list\n", __progname);
300 #else
301 	(void)fprintf(stderr, "usage: %s [-s newshell] [user]\n", __progname);
302 	(void)fprintf(stderr, "       %s -a list\n", __progname);
303 #endif
304 	exit(1);
305 }
306