Home | History | Annotate | Line # | Download | only in chpass
chpass.c revision 1.5
      1  1.5    glass /*	$NetBSD: chpass.c,v 1.5 1995/03/26 04:55:25 glass 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.1      cgd #ifndef lint
     37  1.5    glass static char copyright[] =
     38  1.5    glass "@(#) Copyright (c) 1988, 1993, 1994\n\
     39  1.5    glass 	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.5    glass static char rcsid[] = "$NetBSD: chpass.c,v 1.5 1995/03/26 04:55:25 glass 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/signal.h>
     53  1.1      cgd #include <sys/time.h>
     54  1.1      cgd #include <sys/resource.h>
     55  1.5    glass 
     56  1.5    glass #include <ctype.h>
     57  1.5    glass #include <err.h>
     58  1.5    glass #include <errno.h>
     59  1.1      cgd #include <fcntl.h>
     60  1.1      cgd #include <pwd.h>
     61  1.1      cgd #include <stdio.h>
     62  1.5    glass #include <stdlib.h>
     63  1.1      cgd #include <string.h>
     64  1.5    glass #include <unistd.h>
     65  1.5    glass 
     66  1.5    glass #include <pw_scan.h>
     67  1.5    glass #include <pw_util.h>
     68  1.5    glass #include "pw_copy.h"
     69  1.5    glass 
     70  1.1      cgd #include "chpass.h"
     71  1.1      cgd #include "pathnames.h"
     72  1.1      cgd 
     73  1.1      cgd char *progname = "chpass";
     74  1.1      cgd char *tempname;
     75  1.1      cgd uid_t uid;
     76  1.1      cgd 
     77  1.2   brezak #ifdef	YP
     78  1.2   brezak int use_yp;
     79  1.2   brezak int force_yp = 0;
     80  1.2   brezak extern struct passwd *ypgetpwnam(), *ypgetpwuid();
     81  1.2   brezak #endif
     82  1.2   brezak 
     83  1.5    glass void	baduser __P((void));
     84  1.5    glass void	usage __P((void));
     85  1.5    glass 
     86  1.5    glass int
     87  1.1      cgd main(argc, argv)
     88  1.1      cgd 	int argc;
     89  1.1      cgd 	char **argv;
     90  1.1      cgd {
     91  1.5    glass 	enum { NEWSH, LOADENTRY, EDITENTRY } op;
     92  1.5    glass 	struct passwd *pw, lpw;
     93  1.1      cgd 	int ch, pfd, tfd;
     94  1.1      cgd 	char *arg;
     95  1.1      cgd 
     96  1.2   brezak #ifdef	YP
     97  1.4  deraadt 	use_yp = _yp_check(NULL);
     98  1.2   brezak #endif
     99  1.2   brezak 
    100  1.1      cgd 	op = EDITENTRY;
    101  1.2   brezak 	while ((ch = getopt(argc, argv, "a:s:ly")) != EOF)
    102  1.1      cgd 		switch(ch) {
    103  1.1      cgd 		case 'a':
    104  1.1      cgd 			op = LOADENTRY;
    105  1.1      cgd 			arg = optarg;
    106  1.1      cgd 			break;
    107  1.1      cgd 		case 's':
    108  1.1      cgd 			op = NEWSH;
    109  1.1      cgd 			arg = optarg;
    110  1.1      cgd 			break;
    111  1.2   brezak #ifdef	YP
    112  1.4  deraadt 		case 'l':
    113  1.4  deraadt 			use_yp = 0;
    114  1.4  deraadt 			break;
    115  1.4  deraadt 		case 'y':
    116  1.4  deraadt 			if (!use_yp) {
    117  1.5    glass 				warnx("YP not in use.");
    118  1.4  deraadt 				usage();
    119  1.4  deraadt 			}
    120  1.4  deraadt 			force_yp = 1;
    121  1.4  deraadt 			break;
    122  1.2   brezak #endif
    123  1.1      cgd 		case '?':
    124  1.1      cgd 		default:
    125  1.1      cgd 			usage();
    126  1.1      cgd 		}
    127  1.1      cgd 	argc -= optind;
    128  1.1      cgd 	argv += optind;
    129  1.1      cgd 
    130  1.2   brezak #ifdef	YP
    131  1.5    glass 	if (op == LOADENTRY && use_yp)
    132  1.5    glass 		errx(1, "cannot load entry using NIS.\n\tUse the -l flag to load local.");
    133  1.2   brezak #endif
    134  1.1      cgd 	uid = getuid();
    135  1.1      cgd 
    136  1.1      cgd 	if (op == EDITENTRY || op == NEWSH)
    137  1.1      cgd 		switch(argc) {
    138  1.1      cgd 		case 0:
    139  1.4  deraadt 			pw = getpwuid(uid);
    140  1.2   brezak #ifdef	YP
    141  1.4  deraadt 			if (pw && !force_yp)
    142  1.4  deraadt 				use_yp = 0;
    143  1.4  deraadt 			else if (use_yp)
    144  1.4  deraadt 				pw = ypgetpwuid(uid);
    145  1.2   brezak #endif	/* YP */
    146  1.5    glass 			if (!pw)
    147  1.5    glass 				errx(1, "unknown user: uid %u\n", uid);
    148  1.1      cgd 			break;
    149  1.1      cgd 		case 1:
    150  1.2   brezak 			pw = getpwnam(*argv);
    151  1.2   brezak #ifdef	YP
    152  1.4  deraadt 			if (pw && !force_yp)
    153  1.4  deraadt 				use_yp = 0;
    154  1.4  deraadt 			else if (use_yp)
    155  1.4  deraadt 				pw = ypgetpwnam(*argv);
    156  1.2   brezak #endif	/* YP */
    157  1.5    glass 			if (!pw)
    158  1.5    glass 				errx(1, "unknown user: %s", *argv);
    159  1.1      cgd 			if (uid && uid != pw->pw_uid)
    160  1.1      cgd 				baduser();
    161  1.1      cgd 			break;
    162  1.1      cgd 		default:
    163  1.1      cgd 			usage();
    164  1.1      cgd 		}
    165  1.1      cgd 
    166  1.1      cgd 	if (op == NEWSH) {
    167  1.1      cgd 		/* protect p_shell -- it thinks NULL is /bin/sh */
    168  1.1      cgd 		if (!arg[0])
    169  1.1      cgd 			usage();
    170  1.1      cgd 		if (p_shell(arg, pw, (ENTRY *)NULL))
    171  1.1      cgd 			pw_error((char *)NULL, 0, 1);
    172  1.1      cgd 	}
    173  1.1      cgd 
    174  1.1      cgd 	if (op == LOADENTRY) {
    175  1.1      cgd 		if (uid)
    176  1.1      cgd 			baduser();
    177  1.1      cgd 		pw = &lpw;
    178  1.1      cgd 		if (!pw_scan(arg, pw))
    179  1.1      cgd 			exit(1);
    180  1.1      cgd 	}
    181  1.1      cgd 
    182  1.1      cgd 	/*
    183  1.1      cgd 	 * The temporary file/file descriptor usage is a little tricky here.
    184  1.1      cgd 	 * 1:	We start off with two fd's, one for the master password
    185  1.1      cgd 	 *	file (used to lock everything), and one for a temporary file.
    186  1.1      cgd 	 * 2:	Display() gets an fp for the temporary file, and copies the
    187  1.1      cgd 	 *	user's information into it.  It then gives the temporary file
    188  1.1      cgd 	 *	to the user and closes the fp, closing the underlying fd.
    189  1.1      cgd 	 * 3:	The user edits the temporary file some number of times.
    190  1.1      cgd 	 * 4:	Verify() gets an fp for the temporary file, and verifies the
    191  1.1      cgd 	 *	contents.  It can't use an fp derived from the step #2 fd,
    192  1.1      cgd 	 *	because the user's editor may have created a new instance of
    193  1.1      cgd 	 *	the file.  Once the file is verified, its contents are stored
    194  1.1      cgd 	 *	in a password structure.  The verify routine closes the fp,
    195  1.1      cgd 	 *	closing the underlying fd.
    196  1.1      cgd 	 * 5:	Delete the temporary file.
    197  1.1      cgd 	 * 6:	Get a new temporary file/fd.  Pw_copy() gets an fp for it
    198  1.1      cgd 	 *	file and copies the master password file into it, replacing
    199  1.1      cgd 	 *	the user record with a new one.  We can't use the first
    200  1.1      cgd 	 *	temporary file for this because it was owned by the user.
    201  1.1      cgd 	 *	Pw_copy() closes its fp, flushing the data and closing the
    202  1.1      cgd 	 *	underlying file descriptor.  We can't close the master
    203  1.1      cgd 	 *	password fp, or we'd lose the lock.
    204  1.1      cgd 	 * 7:	Call pw_mkdb() (which renames the temporary file) and exit.
    205  1.1      cgd 	 *	The exit closes the master passwd fp/fd.
    206  1.1      cgd 	 */
    207  1.1      cgd 	pw_init();
    208  1.1      cgd 	pfd = pw_lock();
    209  1.1      cgd 	tfd = pw_tmp();
    210  1.1      cgd 
    211  1.1      cgd 	if (op == EDITENTRY) {
    212  1.1      cgd 		display(tfd, pw);
    213  1.1      cgd 		edit(pw);
    214  1.1      cgd 		(void)unlink(tempname);
    215  1.1      cgd 		tfd = pw_tmp();
    216  1.1      cgd 	}
    217  1.1      cgd 
    218  1.2   brezak #ifdef	YP
    219  1.4  deraadt 	if (use_yp) {
    220  1.2   brezak 		(void)unlink(tempname);
    221  1.4  deraadt 		if (pw_yp(pw, uid))
    222  1.4  deraadt 			pw_error((char *)NULL, 0, 1);
    223  1.4  deraadt 		else
    224  1.4  deraadt 			exit(0);
    225  1.4  deraadt 	}
    226  1.4  deraadt 	else
    227  1.2   brezak #endif	/* YP */
    228  1.1      cgd 	pw_copy(pfd, tfd, pw);
    229  1.1      cgd 
    230  1.1      cgd 	if (!pw_mkdb())
    231  1.1      cgd 		pw_error((char *)NULL, 0, 1);
    232  1.2   brezak 
    233  1.1      cgd 	exit(0);
    234  1.1      cgd }
    235  1.1      cgd 
    236  1.5    glass void
    237  1.1      cgd baduser()
    238  1.1      cgd {
    239  1.5    glass 
    240  1.5    glass 	errx(1, "%s", strerror(EACCES));
    241  1.1      cgd }
    242  1.1      cgd 
    243  1.5    glass void
    244  1.1      cgd usage()
    245  1.1      cgd {
    246  1.5    glass 
    247  1.2   brezak #ifdef	YP
    248  1.2   brezak 	(void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [-l]%s [user]\n", use_yp?" [-y]":"");
    249  1.2   brezak #else
    250  1.1      cgd 	(void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n");
    251  1.2   brezak #endif
    252  1.1      cgd 	exit(1);
    253  1.1      cgd }
    254