xref: /openbsd-src/usr.bin/chpass/chpass.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: chpass.c,v 1.37 2009/10/27 23:59:36 deraadt 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 #include <sys/param.h>
34 #include <sys/resource.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/uio.h>
38 
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <paths.h>
43 #include <pwd.h>
44 #include <signal.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <util.h>
50 
51 #include "chpass.h"
52 
53 extern char *__progname;
54 
55 enum { NEWSH, LOADENTRY, EDITENTRY } op;
56 uid_t uid;
57 #ifdef	YP
58 int	use_yp;
59 int	force_yp = 0;
60 #endif
61 
62 void	baduser(void);
63 void	kbintr(int);
64 void	usage(void);
65 
66 int
67 main(int argc, char *argv[])
68 {
69 	struct passwd *pw = NULL, *opw = NULL, lpw;
70 	int i, ch, pfd, tfd, dfd;
71 	char *tz, *arg = NULL;
72 	sigset_t fullset;
73 
74 #ifdef	YP
75 	use_yp = _yp_check(NULL);
76 #endif
77 	/* We need to use the system timezone for date conversions. */
78 	if ((tz = getenv("TZ")) != NULL) {
79 	    unsetenv("TZ");
80 	    tzset();
81 	    setenv("TZ", tz, 1);
82 	}
83 
84 	op = EDITENTRY;
85 	while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
86 		switch(ch) {
87 		case 'a':
88 			op = LOADENTRY;
89 			arg = optarg;
90 			break;
91 		case 's':
92 			op = NEWSH;
93 			arg = optarg;
94 			break;
95 #ifdef	YP
96 		case 'l':
97 			use_yp = 0;
98 			break;
99 		case 'y':
100 			if (!use_yp) {
101 				warnx("YP not in use.");
102 				usage();
103 			}
104 			force_yp = 1;
105 			break;
106 #endif
107 		case '?':
108 		default:
109 			usage();
110 		}
111 	argc -= optind;
112 	argv += optind;
113 
114 #ifdef	YP
115 	if (op == LOADENTRY && use_yp)
116 		errx(1, "cannot load using YP, use -l to load local.");
117 #endif
118 	uid = getuid();
119 
120 	if (op == EDITENTRY || op == NEWSH)
121 		switch(argc) {
122 		case 0:
123 			pw = getpwuid(uid);
124 #ifdef	YP
125 			if (pw && !force_yp)
126 				use_yp = 0;
127 			else if (use_yp)
128 				pw = ypgetpwuid(uid);
129 #endif	/* YP */
130 			if (!pw)
131 				errx(1, "unknown user: uid %u", uid);
132 			break;
133 		case 1:
134 			pw = getpwnam(*argv);
135 #ifdef	YP
136 			if (pw && !force_yp)
137 				use_yp = 0;
138 			else if (use_yp)
139 				pw = ypgetpwnam(*argv);
140 #endif	/* YP */
141 			if (!pw)
142 				errx(1, "unknown user: %s", *argv);
143 			if (uid && uid != pw->pw_uid)
144 				baduser();
145 			break;
146 		default:
147 			usage();
148 		}
149 
150 	if (op == LOADENTRY) {
151 		if (argc != 0)
152 			errx(1, "option -a does not accept user argument");
153 		if (uid)
154 			baduser();
155 		pw = &lpw;
156 		if (!pw_scan(arg, pw, NULL))
157 			exit(1);
158 		opw = getpwnam(pw->pw_name);
159 	}
160 	if (opw == NULL && (opw = pw_dup(pw)) == NULL)
161 		err(1, NULL);
162 
163 	/* Edit the user passwd information if requested. */
164 	if (op == EDITENTRY) {
165 		char tempname[] = _PATH_VARTMP "pw.XXXXXXXXXX";
166 		int edit_status;
167 
168 		if ((pw = pw_dup(pw)) == NULL)
169 			pw_error(NULL, 1, 1);
170 		dfd = mkstemp(tempname);
171 		if (dfd == -1 || fcntl(dfd, F_SETFD, 1) == -1)
172 			pw_error(tempname, 1, 1);
173 		display(tempname, dfd, pw);
174 		edit_status = edit(tempname, pw);
175 		close(dfd);
176 		unlink(tempname);
177 
178 		switch (edit_status) {
179 		case EDIT_OK:
180 			break;
181 		case EDIT_NOCHANGE:
182 			pw_error(NULL, 0, 0);
183 			break;
184 		case EDIT_ERROR:
185 		default:
186 			pw_error(tempname, 1, 1);
187 			break;
188 		}
189 	}
190 
191 	if (op == NEWSH) {
192 		/* protect p_shell -- it thinks NULL is /bin/sh */
193 		if (!arg[0])
194 			usage();
195 		if (p_shell(arg, pw, NULL))
196 			pw_error(NULL, 0, 1);
197 	}
198 
199 	/* Drop user's real uid and block all signals to avoid a DoS. */
200 	setuid(0);
201 	sigfillset(&fullset);
202 	sigdelset(&fullset, SIGINT);
203 	sigprocmask(SIG_BLOCK, &fullset, NULL);
204 
205 	/* Get the passwd lock file and open the passwd file for reading. */
206 	pw_init();
207 	for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
208 		if (i == 4)
209 			(void)fputs("Attempting lock password file, "
210 			    "please wait or press ^C to abort", stderr);
211 		(void)signal(SIGINT, kbintr);
212 		if (i % 16 == 0)
213 			fputc('.', stderr);
214 		usleep(250000);
215 		(void)signal(SIGINT, SIG_IGN);
216 	}
217 	if (i >= 4)
218 		fputc('\n', stderr);
219 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
220 	if (pfd == -1 || fcntl(pfd, F_SETFD, 1) == -1)
221 		pw_error(_PATH_MASTERPASSWD, 1, 1);
222 
223 #ifdef	YP
224 	if (use_yp) {
225 		if (pw_yp(pw, uid))
226 			pw_error(NULL, 0, 1);
227 		else {
228 			pw_abort();
229 			exit(0);
230 		}
231 	} else
232 #endif	/* YP */
233 	{
234 		/* Copy the passwd file to the lock file, updating pw. */
235 		pw_copy(pfd, tfd, pw, opw);
236 
237 		/* If username changed we need to rebuild the entire db. */
238 		arg = !strcmp(opw->pw_name, pw->pw_name) ? pw->pw_name : NULL;
239 
240 		/* Now finish the passwd file update. */
241 		if (pw_mkdb(arg, 0) == -1)
242 			pw_error(NULL, 0, 1);
243 	}
244 
245 	exit(0);
246 }
247 
248 void
249 baduser(void)
250 {
251 
252 	errx(1, "%s", strerror(EACCES));
253 }
254 
255 /* ARGSUSED */
256 void
257 kbintr(int signo)
258 {
259 	struct iovec iv[5];
260 
261 	iv[0].iov_base = "\n";
262 	iv[0].iov_len = 1;
263 	iv[1].iov_base = __progname;
264 	iv[1].iov_len = strlen(__progname);
265 	iv[2].iov_base = ": ";
266 	iv[2].iov_len = 2;
267 	iv[3].iov_base = _PATH_MASTERPASSWD;
268 	iv[3].iov_len = sizeof(_PATH_MASTERPASSWD) - 1;
269 	iv[4].iov_base = " unchanged\n";
270 	iv[4].iov_len = 11;
271 	writev(STDERR_FILENO, iv, 5);
272 
273 	_exit(1);
274 }
275 
276 void
277 usage(void)
278 {
279 
280 #ifdef	YP
281 	(void)fprintf(stderr,
282 	    "usage: %s [-l%s] [-s newshell] [user]\n",
283 	    __progname, use_yp ? "y" : "");
284 	(void)fprintf(stderr,
285 	    "       %s [-l] -a list\n", __progname);
286 #else
287 	(void)fprintf(stderr, "usage: %s [-s newshell] [user]\n", __progname);
288 	(void)fprintf(stderr, "       %s -a list\n", __progname);
289 #endif
290 	exit(1);
291 }
292