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