Home | History | Annotate | Line # | Download | only in vipw
vipw.c revision 1.5
      1 /*	$NetBSD: vipw.c,v 1.5 1997/10/17 14:31:13 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 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) 1987, 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[] = "@(#)vipw.c	8.3 (Berkeley) 4/2/94";
     45 #else
     46 __RCSID("$NetBSD: vipw.c,v 1.5 1997/10/17 14:31:13 lukem Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/types.h>
     51 #include <sys/stat.h>
     52 
     53 #include <err.h>
     54 #include <pwd.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 #include <fcntl.h>
     60 #include <util.h>
     61 
     62 void	copyfile __P((int, int));
     63 int	main __P((int, char **));
     64 void	usage __P((void));
     65 
     66 int
     67 main(argc, argv)
     68 	int argc;
     69 	char *argv[];
     70 {
     71 	int pfd, tfd;
     72 	struct stat begin, end;
     73 	int ch;
     74 
     75 	while ((ch = getopt(argc, argv, "")) != -1) {
     76 		switch (ch) {
     77 		case '?':
     78 		default:
     79 			usage();
     80 		}
     81 	}
     82 	argc -= optind;
     83 	argv += optind;
     84 
     85 	if (argc != 0)
     86 		usage();
     87 
     88 	pw_init();
     89 	tfd = pw_lock(0);
     90 	if (tfd < 0)
     91 		errx(1, "the passwd file is busy.");
     92 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
     93 	if (pfd < 0)
     94 		pw_error(_PATH_MASTERPASSWD, 1, 1);
     95 	copyfile(pfd, tfd);
     96 	(void)close(tfd);
     97 
     98 	for (;;) {
     99 		if (stat(_PATH_MASTERPASSWD_LOCK, &begin))
    100 			pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
    101 		pw_edit(0, NULL);
    102 		if (stat(_PATH_MASTERPASSWD_LOCK, &end))
    103 			pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
    104 		if (begin.st_mtime == end.st_mtime) {
    105 			warnx("no changes made");
    106 			pw_error((char *)NULL, 0, 0);
    107 		}
    108 		if (pw_mkdb() == 0)
    109 			break;
    110 		pw_prompt();
    111 	}
    112 	exit(0);
    113 }
    114 
    115 void
    116 copyfile(from, to)
    117 	int from, to;
    118 {
    119 	int nr, nw, off;
    120 	char buf[8*1024];
    121 
    122 	while ((nr = read(from, buf, sizeof(buf))) > 0)
    123 		for (off = 0; off < nr; nr -= nw, off += nw)
    124 			if ((nw = write(to, buf + off, nr)) < 0)
    125 				pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
    126 	if (nr < 0)
    127 		pw_error(_PATH_MASTERPASSWD, 1, 1);
    128 }
    129 
    130 void
    131 usage()
    132 {
    133 
    134 	(void)fprintf(stderr, "usage: vipw\n");
    135 	exit(1);
    136 }
    137