Home | History | Annotate | Line # | Download | only in chpass
chpass.c revision 1.28
      1 /*	$NetBSD: chpass.c,v 1.28 2003/08/07 11:13:18 agc 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)chpass.c	8.4 (Berkeley) 4/2/94";
     41 #else
     42 __RCSID("$NetBSD: chpass.c,v 1.28 2003/08/07 11:13:18 agc Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/stat.h>
     48 #include <sys/time.h>
     49 #include <sys/resource.h>
     50 
     51 #include <ctype.h>
     52 #include <err.h>
     53 #include <errno.h>
     54 #include <fcntl.h>
     55 #include <pwd.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 #include <util.h>
     61 
     62 #include "chpass.h"
     63 #include "pathnames.h"
     64 
     65 static char tempname[] = "/etc/pw.XXXXXX";
     66 uid_t uid;
     67 int use_yp;
     68 
     69 void	(*Pw_error) __P((const char *, int, int));
     70 
     71 #ifdef	YP
     72 extern	int _yp_check __P((char **));	/* buried deep inside libc */
     73 #endif
     74 
     75 void	baduser __P((void));
     76 void	cleanup __P((void));
     77 int	main __P((int, char **));
     78 void	usage __P((void));
     79 
     80 int
     81 main(argc, argv)
     82 	int argc;
     83 	char **argv;
     84 {
     85 	enum { NEWSH, LOADENTRY, EDITENTRY } op;
     86 	struct passwd *pw, lpw, old_pw;
     87 	int ch, dfd, pfd, tfd;
     88 #ifdef YP
     89 	int yflag;
     90 #endif
     91 	char *arg, *username = NULL;
     92 
     93 #ifdef __GNUC__
     94 	pw = NULL;		/* XXX gcc -Wuninitialized */
     95 	arg = NULL;
     96 #endif
     97 #ifdef	YP
     98 	use_yp = _yp_check(NULL);
     99 #endif
    100 
    101 	op = EDITENTRY;
    102 	while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
    103 		switch (ch) {
    104 		case 'a':
    105 			op = LOADENTRY;
    106 			arg = optarg;
    107 			break;
    108 		case 's':
    109 			op = NEWSH;
    110 			arg = optarg;
    111 			break;
    112 		case 'l':
    113 			use_yp = 0;
    114 			break;
    115 		case 'y':
    116 #ifdef	YP
    117 			if (!use_yp)
    118 				errx(1, "YP not in use.");
    119 			yflag = 1;
    120 #else
    121 			errx(1, "YP support not compiled in.");
    122 #endif
    123 			break;
    124 		default:
    125 			usage();
    126 		}
    127 	argc -= optind;
    128 	argv += optind;
    129 
    130 	uid = getuid();
    131 	switch (argc) {
    132 	case 0:
    133 		/* nothing */
    134 		break;
    135 
    136 	case 1:
    137 		username = argv[0];
    138 		break;
    139 
    140 	default:
    141 		usage();
    142 	}
    143 
    144 #ifdef YP
    145 	/*
    146 	 * We need to determine if we _really_ want to use YP.
    147 	 * If we defaulted to YP (i.e. were not given the -y flag),
    148 	 * and the master is not running rpc.yppasswdd, we check
    149 	 * to see if the user exists in the local passwd database.
    150 	 * If so, we use it, otherwise we error out.
    151 	 */
    152 	if (use_yp && yflag == 0) {
    153 		if (check_yppasswdd()) {
    154 			/*
    155 			 * We weren't able to contact rpc.yppasswdd.
    156 			 * Check to see if we're in the local
    157 			 * password database.  If we are, use it.
    158 			 */
    159 			if (username != NULL)
    160 				pw = getpwnam(username);
    161 			else
    162 				pw = getpwuid(uid);
    163 			if (pw != NULL)
    164 				use_yp = 0;
    165 			else {
    166 				warnx("master YP server not running yppasswd"
    167 				    " daemon.");
    168 				errx(1, "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"
    184 		    "\tUse the -l flag to load local.");
    185 #endif
    186 
    187 	if (op == EDITENTRY || op == NEWSH) {
    188 		if (username != NULL) {
    189 			pw = getpwnam(username);
    190 			if (pw == NULL)
    191 				errx(1, "unknown user: %s", username);
    192 			if (uid && uid != pw->pw_uid)
    193 				baduser();
    194 		} else {
    195 			pw = getpwuid(uid);
    196 			if (pw == NULL)
    197 				errx(1, "unknown user: uid %u", uid);
    198 		}
    199 
    200 		/* Make a copy for later verification */
    201 		old_pw = *pw;
    202 		old_pw.pw_gecos = strdup(old_pw.pw_gecos);
    203 		if (!old_pw.pw_gecos) {
    204 			err(1, "strdup");
    205 			/*NOTREACHED*/
    206 		}
    207 	}
    208 
    209 	if (op == NEWSH) {
    210 		/* protect p_shell -- it thinks NULL is /bin/sh */
    211 		if (!arg[0])
    212 			usage();
    213 		if (p_shell(arg, pw, NULL))
    214 			(*Pw_error)(NULL, 0, 1);
    215 	}
    216 
    217 	if (op == LOADENTRY) {
    218 		if (uid)
    219 			baduser();
    220 		pw = &lpw;
    221 		if (!pw_scan(arg, pw, NULL))
    222 			exit(1);
    223 	}
    224 
    225 	/* Edit the user passwd information if requested. */
    226 	if (op == EDITENTRY) {
    227 		dfd = mkstemp(tempname);
    228 		if (dfd < 0 || fcntl(dfd, F_SETFD, 1) < 0)
    229 			(*Pw_error)(tempname, 1, 1);
    230 		if (atexit(cleanup)) {
    231 			cleanup();
    232 			errx(1, "couldn't register cleanup");
    233 		}
    234 		display(tempname, dfd, pw);
    235 		edit(tempname, pw);
    236 	}
    237 
    238 #ifdef	YP
    239 	if (use_yp) {
    240 		if (pw_yp(pw, uid))
    241 			yppw_error((char *)NULL, 0, 1);
    242 		else
    243 			exit(0);
    244 		/* Will not exit from this if. */
    245 	}
    246 #endif	/* YP */
    247 
    248 
    249 	/*
    250 	 * Get the passwd lock file and open the passwd file for
    251 	 * reading.
    252 	 */
    253 	pw_init();
    254 	tfd = pw_lock(0);
    255 	if (tfd < 0) {
    256 		if (errno != EEXIST)
    257 			err(1, "%s", _PATH_MASTERPASSWD_LOCK);
    258 		warnx("The passwd file is busy, waiting...");
    259 		tfd = pw_lock(10);
    260 		if (tfd < 0) {
    261 			if (errno != EEXIST)
    262 				err(1, "%s", _PATH_MASTERPASSWD_LOCK);
    263 			errx(1, "The passwd file is still busy, "
    264 			     "try again later.");
    265 		}
    266 	}
    267 	if (fcntl(tfd, F_SETFD, 1) < 0)
    268 		pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
    269 
    270 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
    271 	if (pfd < 0 || fcntl(pfd, F_SETFD, 1) < 0)
    272 		pw_error(_PATH_MASTERPASSWD, 1, 1);
    273 
    274 	/* Copy the passwd file to the lock file, updating pw. */
    275 	pw_copy(pfd, tfd, pw, (op == LOADENTRY) ? NULL : &old_pw);
    276 
    277 	/* Now finish the passwd file update. */
    278 	if (pw_mkdb(username, 0) < 0)
    279 		pw_error(NULL, 0, 1);
    280 
    281 	exit(0);
    282 }
    283 
    284 void
    285 baduser()
    286 {
    287 
    288 	errx(1, "%s", strerror(EACCES));
    289 }
    290 
    291 void
    292 usage()
    293 {
    294 
    295 	(void)fprintf(stderr,
    296 	    "usage: %s [-a list] [-s shell] [-l] [user]\n"
    297 	    "       %s [-a list] [-s shell] [-y] [user]\n",
    298 	    getprogname(), getprogname());
    299 	exit(1);
    300 }
    301 
    302 void
    303 cleanup()
    304 {
    305 
    306 	(void)unlink(tempname);
    307 }
    308