Home | History | Annotate | Line # | Download | only in chpass
chpass.c revision 1.25
      1 /*	$NetBSD: chpass.c,v 1.25 2002/08/08 04:49:26 enami 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.25 2002/08/08 04:49:26 enami 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 static char tempname[] = "/etc/pw.XXXXXX";
     70 uid_t uid;
     71 int use_yp;
     72 
     73 void	(*Pw_error) __P((const char *, int, int));
     74 
     75 #ifdef	YP
     76 extern	int _yp_check __P((char **));	/* buried deep inside libc */
     77 #endif
     78 
     79 void	baduser __P((void));
     80 void	cleanup __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, dfd, pfd, tfd, yflag;
     92 	char *arg, *username = NULL;
     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 				warnx("master YP server not running yppasswd"
    168 				    " daemon.");
    169 				errx(1, "Can't change password.");
    170 			}
    171 		}
    172 	}
    173 #endif
    174 
    175 #ifdef YP
    176 	if (use_yp)
    177 		Pw_error = yppw_error;
    178 	else
    179 #endif
    180 		Pw_error = pw_error;
    181 
    182 #ifdef	YP
    183 	if (op == LOADENTRY && use_yp)
    184 		errx(1, "cannot load entry using YP.\n"
    185 		    "\tUse the -l flag to load local.");
    186 #endif
    187 
    188 	if (op == EDITENTRY || op == NEWSH) {
    189 		if (username != NULL) {
    190 			pw = getpwnam(username);
    191 			if (pw == NULL)
    192 				errx(1, "unknown user: %s", username);
    193 			if (uid && uid != pw->pw_uid)
    194 				baduser();
    195 		} else {
    196 			pw = getpwuid(uid);
    197 			if (pw == NULL)
    198 				errx(1, "unknown user: uid %u", uid);
    199 		}
    200 
    201 		/* Make a copy for later verification */
    202 		old_pw = *pw;
    203 		old_pw.pw_gecos = strdup(old_pw.pw_gecos);
    204 	}
    205 
    206 	if (op == NEWSH) {
    207 		/* protect p_shell -- it thinks NULL is /bin/sh */
    208 		if (!arg[0])
    209 			usage();
    210 		if (p_shell(arg, pw, NULL))
    211 			(*Pw_error)(NULL, 0, 1);
    212 	}
    213 
    214 	if (op == LOADENTRY) {
    215 		if (uid)
    216 			baduser();
    217 		pw = &lpw;
    218 		if (!pw_scan(arg, pw, NULL))
    219 			exit(1);
    220 	}
    221 
    222 	/* Edit the user passwd information if requested. */
    223 	if (op == EDITENTRY) {
    224 		dfd = mkstemp(tempname);
    225 		if (dfd < 0 || fcntl(dfd, F_SETFD, 1) < 0)
    226 			(*Pw_error)(tempname, 1, 1);
    227 		if (atexit(cleanup)) {
    228 			cleanup();
    229 			errx(1, "couldn't register cleanup");
    230 		}
    231 		display(tempname, dfd, pw);
    232 		edit(tempname, pw);
    233 	}
    234 
    235 #ifdef	YP
    236 	if (use_yp) {
    237 		if (pw_yp(pw, uid))
    238 			yppw_error((char *)NULL, 0, 1);
    239 		else
    240 			exit(0);
    241 		/* Will not exit from this if. */
    242 	}
    243 #endif	/* YP */
    244 
    245 
    246 	/*
    247 	 * Get the passwd lock file and open the passwd file for
    248 	 * reading.
    249 	 */
    250 	pw_init();
    251 	tfd = pw_lock(0);
    252 	if (tfd < 0) {
    253 		if (errno != EEXIST)
    254 			err(1, "%s", _PATH_MASTERPASSWD_LOCK);
    255 		warnx("The passwd file is busy, waiting...");
    256 		tfd = pw_lock(10);
    257 		if (tfd < 0) {
    258 			if (errno != EEXIST)
    259 				err(1, "%s", _PATH_MASTERPASSWD_LOCK);
    260 			errx(1, "The passwd file is still busy, "
    261 			     "try again later.");
    262 		}
    263 	}
    264 	if (fcntl(tfd, F_SETFD, 1) < 0)
    265 		pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
    266 
    267 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
    268 	if (pfd < 0 || fcntl(pfd, F_SETFD, 1) < 0)
    269 		pw_error(_PATH_MASTERPASSWD, 1, 1);
    270 
    271 	/* Copy the passwd file to the lock file, updating pw. */
    272 	pw_copy(pfd, tfd, pw, (op == LOADENTRY) ? NULL : &old_pw);
    273 
    274 	/* Now finish the passwd file update. */
    275 	if (pw_mkdb(username, 0) < 0)
    276 		pw_error(NULL, 0, 1);
    277 
    278 	exit(0);
    279 }
    280 
    281 void
    282 baduser()
    283 {
    284 
    285 	errx(1, "%s", strerror(EACCES));
    286 }
    287 
    288 void
    289 usage()
    290 {
    291 
    292 	(void)fprintf(stderr,
    293 	    "usage: %s [-a list] [-s shell] [-l] [user]\n"
    294 	    "       %s [-a list] [-s shell] [-y] [user]\n",
    295 	    getprogname(), getprogname());
    296 	exit(1);
    297 }
    298 
    299 void
    300 cleanup()
    301 {
    302 
    303 	(void)unlink(tempname);
    304 }
    305