xref: /netbsd-src/usr.bin/chpass/chpass.c (revision 9f61b80465425a3cc94c6438ccec8497c7340a45)
1*9f61b804Splunky /*	$NetBSD: chpass.c,v 1.35 2011/08/31 16:24:57 plunky Exp $	*/
27659edadSglass 
361f28255Scgd /*-
47659edadSglass  * Copyright (c) 1988, 1993, 1994
57659edadSglass  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
325645d77bSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3498e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
3598e5374cSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
397659edadSglass #if 0
407659edadSglass static char sccsid[] = "@(#)chpass.c	8.4 (Berkeley) 4/2/94";
417659edadSglass #else
42*9f61b804Splunky __RCSID("$NetBSD: chpass.c,v 1.35 2011/08/31 16:24:57 plunky Exp $");
437659edadSglass #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
4661f28255Scgd #include <sys/param.h>
4761f28255Scgd #include <sys/stat.h>
4861f28255Scgd #include <sys/time.h>
4961f28255Scgd #include <sys/resource.h>
507659edadSglass 
517659edadSglass #include <ctype.h>
527659edadSglass #include <err.h>
537659edadSglass #include <errno.h>
5461f28255Scgd #include <fcntl.h>
5561f28255Scgd #include <pwd.h>
5661f28255Scgd #include <stdio.h>
577659edadSglass #include <stdlib.h>
5861f28255Scgd #include <string.h>
597659edadSglass #include <unistd.h>
60370990b2Sjtc #include <util.h>
61925c6f17Selad #include <libgen.h>
627659edadSglass 
6361f28255Scgd #include "chpass.h"
6461f28255Scgd #include "pathnames.h"
6561f28255Scgd 
66925c6f17Selad static char tempname[] = "/tmp/pw.XXXXXX";
6761f28255Scgd uid_t uid;
6851956e93Sthorpej int use_yp;
6951956e93Sthorpej 
70971b39dfSxtraeme void	(*Pw_error)(const char *, int, int);
7161f28255Scgd 
7296267d0eSbrezak #ifdef	YP
73971b39dfSxtraeme extern	int _yp_check(char **);	/* buried deep inside libc */
7496267d0eSbrezak #endif
7596267d0eSbrezak 
766e33bec8Sjoerg __dead static void	baduser(void);
776e33bec8Sjoerg static void	cleanup(void);
786e33bec8Sjoerg __dead static void	usage(void);
797659edadSglass 
807659edadSglass int
main(int argc,char ** argv)81971b39dfSxtraeme main(int argc, char **argv)
8261f28255Scgd {
837659edadSglass 	enum { NEWSH, LOADENTRY, EDITENTRY } op;
846907a00eSphil 	struct passwd *pw, lpw, old_pw;
85ec0349cbSenami 	int ch, dfd, pfd, tfd;
86ec0349cbSenami #ifdef YP
87903f12a5Slukem 	int yflag = 0;
88ec0349cbSenami #endif
89809178f7Senami 	char *arg, *username = NULL;
9061f28255Scgd 
910a3628b2Smikel #ifdef __GNUC__
920a3628b2Smikel 	pw = NULL;		/* XXX gcc -Wuninitialized */
930a3628b2Smikel 	arg = NULL;
940a3628b2Smikel #endif
9596267d0eSbrezak #ifdef	YP
9696267d0eSbrezak 	use_yp = _yp_check(NULL);
9796267d0eSbrezak #endif
9896267d0eSbrezak 
9961f28255Scgd 	op = EDITENTRY;
1000a3628b2Smikel 	while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
10161f28255Scgd 		switch (ch) {
10261f28255Scgd 		case 'a':
10361f28255Scgd 			op = LOADENTRY;
10461f28255Scgd 			arg = optarg;
10561f28255Scgd 			break;
10661f28255Scgd 		case 's':
10761f28255Scgd 			op = NEWSH;
10861f28255Scgd 			arg = optarg;
10961f28255Scgd 			break;
11096267d0eSbrezak 		case 'l':
11196267d0eSbrezak 			use_yp = 0;
11296267d0eSbrezak 			break;
11396267d0eSbrezak 		case 'y':
11451956e93Sthorpej #ifdef	YP
11551956e93Sthorpej 			if (!use_yp)
11651956e93Sthorpej 				errx(1, "YP not in use.");
11797fdc79aSthorpej 			yflag = 1;
11851956e93Sthorpej #else
11951956e93Sthorpej 			errx(1, "YP support not compiled in.");
12096267d0eSbrezak #endif
12151956e93Sthorpej 			break;
12261f28255Scgd 		default:
12361f28255Scgd 			usage();
12461f28255Scgd 		}
12561f28255Scgd 	argc -= optind;
12661f28255Scgd 	argv += optind;
12761f28255Scgd 
12897fdc79aSthorpej 	uid = getuid();
12997fdc79aSthorpej 	switch (argc) {
13097fdc79aSthorpej 	case 0:
13197fdc79aSthorpej 		/* nothing */
13297fdc79aSthorpej 		break;
13397fdc79aSthorpej 
13497fdc79aSthorpej 	case 1:
13597fdc79aSthorpej 		username = argv[0];
13697fdc79aSthorpej 		break;
13797fdc79aSthorpej 
13897fdc79aSthorpej 	default:
13997fdc79aSthorpej 		usage();
14097fdc79aSthorpej 	}
14197fdc79aSthorpej 
14297fdc79aSthorpej #ifdef YP
14397fdc79aSthorpej 	/*
14497fdc79aSthorpej 	 * We need to determine if we _really_ want to use YP.
14597fdc79aSthorpej 	 * If we defaulted to YP (i.e. were not given the -y flag),
14697fdc79aSthorpej 	 * and the master is not running rpc.yppasswdd, we check
14797fdc79aSthorpej 	 * to see if the user exists in the local passwd database.
14897fdc79aSthorpej 	 * If so, we use it, otherwise we error out.
14997fdc79aSthorpej 	 */
15097fdc79aSthorpej 	if (use_yp && yflag == 0) {
15197fdc79aSthorpej 		if (check_yppasswdd()) {
15297fdc79aSthorpej 			/*
15397fdc79aSthorpej 			 * We weren't able to contact rpc.yppasswdd.
15497fdc79aSthorpej 			 * Check to see if we're in the local
15597fdc79aSthorpej 			 * password database.  If we are, use it.
15697fdc79aSthorpej 			 */
15797fdc79aSthorpej 			if (username != NULL)
15897fdc79aSthorpej 				pw = getpwnam(username);
15997fdc79aSthorpej 			else
16097fdc79aSthorpej 				pw = getpwuid(uid);
16197fdc79aSthorpej 			if (pw != NULL)
16297fdc79aSthorpej 				use_yp = 0;
16397fdc79aSthorpej 			else {
164809178f7Senami 				warnx("master YP server not running yppasswd"
165809178f7Senami 				    " daemon.");
166809178f7Senami 				errx(1, "Can't change password.");
16797fdc79aSthorpej 			}
16897fdc79aSthorpej 		}
16997fdc79aSthorpej 	}
17097fdc79aSthorpej #endif
17197fdc79aSthorpej 
17296267d0eSbrezak #ifdef YP
17351956e93Sthorpej 	if (use_yp)
17451956e93Sthorpej 		Pw_error = yppw_error;
17551956e93Sthorpej 	else
17651956e93Sthorpej #endif
17751956e93Sthorpej 		Pw_error = pw_error;
17851956e93Sthorpej 
17951956e93Sthorpej #ifdef	YP
1807659edadSglass 	if (op == LOADENTRY && use_yp)
181809178f7Senami 		errx(1, "cannot load entry using YP.\n"
182809178f7Senami 		    "\tUse the -l flag to load local.");
18396267d0eSbrezak #endif
18461f28255Scgd 
18597fdc79aSthorpej 	if (op == EDITENTRY || op == NEWSH) {
186938125b3Scjs 		if (username != NULL) {
18797fdc79aSthorpej 			pw = getpwnam(username);
18897fdc79aSthorpej 			if (pw == NULL)
18997fdc79aSthorpej 				errx(1, "unknown user: %s", username);
19097fdc79aSthorpej 			if (uid && uid != pw->pw_uid)
19197fdc79aSthorpej 				baduser();
19297fdc79aSthorpej 		} else {
19351956e93Sthorpej 			pw = getpwuid(uid);
19497fdc79aSthorpej 			if (pw == NULL)
195f51456c2Sitojun 				errx(1, "unknown user: uid %u", uid);
19697fdc79aSthorpej 		}
197eb4489ceSmjl 
198eb4489ceSmjl 		/* Make a copy for later verification */
199eb4489ceSmjl 		old_pw = *pw;
200eb4489ceSmjl 		old_pw.pw_gecos = strdup(old_pw.pw_gecos);
2015f2d0b66Sitojun 		if (!old_pw.pw_gecos) {
2025f2d0b66Sitojun 			err(1, "strdup");
2035f2d0b66Sitojun 			/*NOTREACHED*/
2045f2d0b66Sitojun 		}
20561f28255Scgd 	}
20661f28255Scgd 
20761f28255Scgd 	if (op == NEWSH) {
20861f28255Scgd 		/* protect p_shell -- it thinks NULL is /bin/sh */
20961f28255Scgd 		if (!arg[0])
21061f28255Scgd 			usage();
211af7b5d4dSthorpej 		if (p_shell(arg, pw, NULL))
212af7b5d4dSthorpej 			(*Pw_error)(NULL, 0, 1);
21361f28255Scgd 	}
21461f28255Scgd 
21561f28255Scgd 	if (op == LOADENTRY) {
21661f28255Scgd 		if (uid)
21761f28255Scgd 			baduser();
21861f28255Scgd 		pw = &lpw;
219af7b5d4dSthorpej 		if (!pw_scan(arg, pw, NULL))
22061f28255Scgd 			exit(1);
22161f28255Scgd 	}
22261f28255Scgd 
223370990b2Sjtc 	/* Edit the user passwd information if requested. */
22461f28255Scgd 	if (op == EDITENTRY) {
225925c6f17Selad 		struct stat sb;
226925c6f17Selad 
227370990b2Sjtc 		dfd = mkstemp(tempname);
228af7b5d4dSthorpej 		if (dfd < 0 || fcntl(dfd, F_SETFD, 1) < 0)
22951956e93Sthorpej 			(*Pw_error)(tempname, 1, 1);
230809178f7Senami 		if (atexit(cleanup)) {
231809178f7Senami 			cleanup();
232809178f7Senami 			errx(1, "couldn't register cleanup");
233809178f7Senami 		}
234925c6f17Selad 		if (stat(dirname(tempname), &sb) == -1)
235925c6f17Selad 			err(1, "couldn't stat `%s'", dirname(tempname));
236925c6f17Selad 		if (!(sb.st_mode & S_ISTXT))
237925c6f17Selad 			errx(1, "temporary directory `%s' is not sticky",
238925c6f17Selad 			    dirname(tempname));
239925c6f17Selad 
240370990b2Sjtc 		display(tempname, dfd, pw);
241370990b2Sjtc 		edit(tempname, pw);
24261f28255Scgd 	}
24361f28255Scgd 
24496267d0eSbrezak #ifdef	YP
24596267d0eSbrezak 	if (use_yp) {
24696267d0eSbrezak 		if (pw_yp(pw, uid))
247*9f61b804Splunky 			yppw_error(NULL, 0, 1);
24896267d0eSbrezak 		else
24996267d0eSbrezak 			exit(0);
2506907a00eSphil 		/* Will not exit from this if. */
25196267d0eSbrezak 	}
25296267d0eSbrezak #endif	/* YP */
253370990b2Sjtc 
2546907a00eSphil 
2556907a00eSphil 	/*
2566907a00eSphil 	 * Get the passwd lock file and open the passwd file for
2576907a00eSphil 	 * reading.
2586907a00eSphil 	 */
2596907a00eSphil 	pw_init();
2606907a00eSphil 	tfd = pw_lock(0);
2616907a00eSphil 	if (tfd < 0) {
262fef2d92eSthorpej 		if (errno != EEXIST)
263bbef2fbaSitojun 			err(1, "%s", _PATH_MASTERPASSWD_LOCK);
2646907a00eSphil 		warnx("The passwd file is busy, waiting...");
2656907a00eSphil 		tfd = pw_lock(10);
266fef2d92eSthorpej 		if (tfd < 0) {
267fef2d92eSthorpej 			if (errno != EEXIST)
268bbef2fbaSitojun 				err(1, "%s", _PATH_MASTERPASSWD_LOCK);
2696907a00eSphil 			errx(1, "The passwd file is still busy, "
2706907a00eSphil 			     "try again later.");
2716907a00eSphil 		}
272fef2d92eSthorpej 	}
273af7b5d4dSthorpej 	if (fcntl(tfd, F_SETFD, 1) < 0)
274af7b5d4dSthorpej 		pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
2756907a00eSphil 
2766907a00eSphil 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
277af7b5d4dSthorpej 	if (pfd < 0 || fcntl(pfd, F_SETFD, 1) < 0)
2786907a00eSphil 		pw_error(_PATH_MASTERPASSWD, 1, 1);
2796907a00eSphil 
280370990b2Sjtc 	/* Copy the passwd file to the lock file, updating pw. */
281eb4489ceSmjl 	pw_copy(pfd, tfd, pw, (op == LOADENTRY) ? NULL : &old_pw);
28261f28255Scgd 
2834c60e998Shubertf 	close(pfd);
2844c60e998Shubertf 	close(tfd);
2854c60e998Shubertf 
286370990b2Sjtc 	/* Now finish the passwd file update. */
2871e8e78edSad 	if (pw_mkdb(username, 0) < 0)
288af7b5d4dSthorpej 		pw_error(NULL, 0, 1);
28996267d0eSbrezak 
29061f28255Scgd 	exit(0);
29161f28255Scgd }
29261f28255Scgd 
2936e33bec8Sjoerg static void
baduser(void)294971b39dfSxtraeme baduser(void)
29561f28255Scgd {
2967659edadSglass 
2977659edadSglass 	errx(1, "%s", strerror(EACCES));
29861f28255Scgd }
29961f28255Scgd 
3006e33bec8Sjoerg static void
usage(void)301971b39dfSxtraeme usage(void)
30261f28255Scgd {
3037659edadSglass 
304809178f7Senami 	(void)fprintf(stderr,
305809178f7Senami 	    "usage: %s [-a list] [-s shell] [-l] [user]\n"
306f2ad80d3Ssoren 	    "       %s [-a list] [-s shell] [-y] [user]\n",
307f2ad80d3Ssoren 	    getprogname(), getprogname());
30861f28255Scgd 	exit(1);
30961f28255Scgd }
310809178f7Senami 
3116e33bec8Sjoerg static void
cleanup(void)312971b39dfSxtraeme cleanup(void)
313809178f7Senami {
314809178f7Senami 
315809178f7Senami 	(void)unlink(tempname);
316809178f7Senami }
317