xref: /netbsd/usr.bin/chpass/chpass.c (revision bf9ec67e)
1 /*	$NetBSD: chpass.c,v 1.22 2001/08/18 19:35:34 ad 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: chpass.c,v 1.22 2001/08/18 19:35:34 ad Exp $");
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53 #include <sys/resource.h>
54 
55 #include <ctype.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <pwd.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include <util.h>
65 
66 #include "chpass.h"
67 #include "pathnames.h"
68 
69 char *tempname;
70 uid_t uid;
71 int use_yp;
72 int yflag;
73 
74 void	(*Pw_error) __P((const char *, int, int));
75 
76 #ifdef	YP
77 extern	int _yp_check __P((char **));	/* buried deep inside libc */
78 #endif
79 
80 void	baduser __P((void));
81 int	main __P((int, char **));
82 void	usage __P((void));
83 
84 int
85 main(argc, argv)
86 	int argc;
87 	char **argv;
88 {
89 	enum { NEWSH, LOADENTRY, EDITENTRY } op;
90 	struct passwd *pw, lpw, old_pw;
91 	int ch, pfd, tfd, dfd;
92 	char *arg, *username = NULL, tempname[] = "/etc/pw.XXXXXX";
93 
94 #ifdef __GNUC__
95 	pw = NULL;		/* XXX gcc -Wuninitialized */
96 	arg = NULL;
97 #endif
98 #ifdef	YP
99 	use_yp = _yp_check(NULL);
100 #endif
101 
102 	op = EDITENTRY;
103 	while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
104 		switch(ch) {
105 		case 'a':
106 			op = LOADENTRY;
107 			arg = optarg;
108 			break;
109 		case 's':
110 			op = NEWSH;
111 			arg = optarg;
112 			break;
113 		case 'l':
114 			use_yp = 0;
115 			break;
116 		case 'y':
117 #ifdef	YP
118 			if (!use_yp)
119 				errx(1, "YP not in use.");
120 			yflag = 1;
121 #else
122 			errx(1, "YP support not compiled in.");
123 #endif
124 			break;
125 		default:
126 			usage();
127 		}
128 	argc -= optind;
129 	argv += optind;
130 
131 	uid = getuid();
132 	switch (argc) {
133 	case 0:
134 		/* nothing */
135 		break;
136 
137 	case 1:
138 		username = argv[0];
139 		break;
140 
141 	default:
142 		usage();
143 	}
144 
145 #ifdef YP
146 	/*
147 	 * We need to determine if we _really_ want to use YP.
148 	 * If we defaulted to YP (i.e. were not given the -y flag),
149 	 * and the master is not running rpc.yppasswdd, we check
150 	 * to see if the user exists in the local passwd database.
151 	 * If so, we use it, otherwise we error out.
152 	 */
153 	if (use_yp && yflag == 0) {
154 		if (check_yppasswdd()) {
155 			/*
156 			 * We weren't able to contact rpc.yppasswdd.
157 			 * Check to see if we're in the local
158 			 * password database.  If we are, use it.
159 			 */
160 			if (username != NULL)
161 				pw = getpwnam(username);
162 			else
163 				pw = getpwuid(uid);
164 			if (pw != NULL)
165 				use_yp = 0;
166 			else {
167 				errx(1, "master YP server not running yppasswd daemon.\n\t%s\n",
168 				    "Can't change password.");
169 			}
170 		}
171 	}
172 #endif
173 
174 #ifdef YP
175 	if (use_yp)
176 		Pw_error = yppw_error;
177 	else
178 #endif
179 		Pw_error = pw_error;
180 
181 #ifdef	YP
182 	if (op == LOADENTRY && use_yp)
183 		errx(1, "cannot load entry using YP.\n\tUse the -l flag to load local.");
184 #endif
185 
186 	if (op == EDITENTRY || op == NEWSH) {
187 		if (username != NULL) {
188 			pw = getpwnam(username);
189 			if (pw == NULL)
190 				errx(1, "unknown user: %s", username);
191 			if (uid && uid != pw->pw_uid)
192 				baduser();
193 		} else {
194 			pw = getpwuid(uid);
195 			if (pw == NULL)
196 				errx(1, "unknown user: uid %u\n", uid);
197 		}
198 
199 		/* Make a copy for later verification */
200 		old_pw = *pw;
201 		old_pw.pw_gecos = strdup(old_pw.pw_gecos);
202 	}
203 
204 	if (op == NEWSH) {
205 		/* protect p_shell -- it thinks NULL is /bin/sh */
206 		if (!arg[0])
207 			usage();
208 		if (p_shell(arg, pw, NULL))
209 			(*Pw_error)(NULL, 0, 1);
210 	}
211 
212 	if (op == LOADENTRY) {
213 		if (uid)
214 			baduser();
215 		pw = &lpw;
216 		if (!pw_scan(arg, pw, NULL))
217 			exit(1);
218 	}
219 
220 	/* Edit the user passwd information if requested. */
221 	if (op == EDITENTRY) {
222 		dfd = mkstemp(tempname);
223 		if (dfd < 0 || fcntl(dfd, F_SETFD, 1) < 0)
224 			(*Pw_error)(tempname, 1, 1);
225 		display(tempname, dfd, pw);
226 		edit(tempname, pw);
227 		(void)unlink(tempname);
228 	}
229 
230 #ifdef	YP
231 	if (use_yp) {
232 		if (pw_yp(pw, uid))
233 			yppw_error((char *)NULL, 0, 1);
234 		else
235 			exit(0);
236 		/* Will not exit from this if. */
237 	}
238 #endif	/* YP */
239 
240 
241 	/*
242 	 * Get the passwd lock file and open the passwd file for
243 	 * reading.
244 	 */
245 	pw_init();
246 	tfd = pw_lock(0);
247 	if (tfd < 0) {
248 		if (errno != EEXIST)
249 			err(1, "%s", _PATH_MASTERPASSWD_LOCK);
250 		warnx("The passwd file is busy, waiting...");
251 		tfd = pw_lock(10);
252 		if (tfd < 0) {
253 			if (errno != EEXIST)
254 				err(1, "%s", _PATH_MASTERPASSWD_LOCK);
255 			errx(1, "The passwd file is still busy, "
256 			     "try again later.");
257 		}
258 	}
259 	if (fcntl(tfd, F_SETFD, 1) < 0)
260 		pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
261 
262 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
263 	if (pfd < 0 || fcntl(pfd, F_SETFD, 1) < 0)
264 		pw_error(_PATH_MASTERPASSWD, 1, 1);
265 
266 	/* Copy the passwd file to the lock file, updating pw. */
267 	pw_copy(pfd, tfd, pw, (op == LOADENTRY) ? NULL : &old_pw);
268 
269 	/* Now finish the passwd file update. */
270 	if (pw_mkdb(username, 0) < 0)
271 		pw_error(NULL, 0, 1);
272 
273 	exit(0);
274 }
275 
276 void
277 baduser()
278 {
279 
280 	errx(1, "%s", strerror(EACCES));
281 }
282 
283 void
284 usage()
285 {
286 
287 #ifdef	YP
288 	(void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [-l]%s [user]\n", use_yp?" [-y]":"");
289 #else
290 	(void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n");
291 #endif
292 	exit(1);
293 }
294