vipw.c revision 30765
11553Srgrimes/*
21553Srgrimes * Copyright (c) 1987, 1993, 1994
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
341553Srgrimes#ifndef lint
3530765Scharnierstatic const char copyright[] =
361553Srgrimes"@(#) Copyright (c) 1987, 1993, 1994\n\
371553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381553Srgrimes#endif /* not lint */
391553Srgrimes
401553Srgrimes#ifndef lint
4130765Scharnier#if 0
421553Srgrimesstatic char sccsid[] = "@(#)vipw.c	8.3 (Berkeley) 4/2/94";
4330765Scharnier#endif
4430765Scharnierstatic const char rcsid[] =
4530765Scharnier	"$Id$";
461553Srgrimes#endif /* not lint */
471553Srgrimes
481553Srgrimes#include <sys/types.h>
491553Srgrimes#include <sys/stat.h>
501553Srgrimes
511553Srgrimes#include <err.h>
521553Srgrimes#include <pwd.h>
531553Srgrimes#include <stdio.h>
541553Srgrimes#include <stdlib.h>
551553Srgrimes#include <string.h>
561553Srgrimes#include <unistd.h>
571553Srgrimes
581553Srgrimes#include "pw_util.h"
591553Srgrimes
601553Srgrimeschar *tempname;
611553Srgrimes
621553Srgrimesvoid	copyfile __P((int, int));
6330765Scharnierstatic void	usage __P((void));
641553Srgrimes
651553Srgrimesint
661553Srgrimesmain(argc, argv)
671553Srgrimes	int argc;
681553Srgrimes	char *argv[];
691553Srgrimes{
701553Srgrimes	int pfd, tfd;
711553Srgrimes	struct stat begin, end;
721553Srgrimes	int ch;
731553Srgrimes
7424428Simp	while ((ch = getopt(argc, argv, "")) != -1)
751553Srgrimes		switch (ch) {
761553Srgrimes		case '?':
771553Srgrimes		default:
781553Srgrimes			usage();
791553Srgrimes		}
808857Srgrimes
811553Srgrimes	argc -= optind;
821553Srgrimes	argv += optind;
831553Srgrimes
841553Srgrimes	if (argc != 0)
851553Srgrimes		usage();
861553Srgrimes
871553Srgrimes	pw_init();
881553Srgrimes	pfd = pw_lock();
891553Srgrimes	tfd = pw_tmp();
901553Srgrimes	copyfile(pfd, tfd);
911553Srgrimes	(void)close(tfd);
921553Srgrimes
931553Srgrimes	for (;;) {
941553Srgrimes		if (stat(tempname, &begin))
951553Srgrimes			pw_error(tempname, 1, 1);
961553Srgrimes		pw_edit(0);
971553Srgrimes		if (stat(tempname, &end))
981553Srgrimes			pw_error(tempname, 1, 1);
991553Srgrimes		if (begin.st_mtime == end.st_mtime) {
1001553Srgrimes			warnx("no changes made");
1011553Srgrimes			pw_error((char *)NULL, 0, 0);
1021553Srgrimes		}
10316876Sguido		if (pw_mkdb((char *)NULL))
1041553Srgrimes			break;
1051553Srgrimes		pw_prompt();
1061553Srgrimes	}
1071553Srgrimes	exit(0);
1081553Srgrimes}
1091553Srgrimes
1101553Srgrimesvoid
1111553Srgrimescopyfile(from, to)
1121553Srgrimes	int from, to;
1131553Srgrimes{
1141553Srgrimes	int nr, nw, off;
1151553Srgrimes	char buf[8*1024];
1168857Srgrimes
1171553Srgrimes	while ((nr = read(from, buf, sizeof(buf))) > 0)
1181553Srgrimes		for (off = 0; off < nr; nr -= nw, off += nw)
1191553Srgrimes			if ((nw = write(to, buf + off, nr)) < 0)
1201553Srgrimes				pw_error(tempname, 1, 1);
1211553Srgrimes	if (nr < 0)
1221553Srgrimes		pw_error(_PATH_MASTERPASSWD, 1, 1);
1231553Srgrimes}
1241553Srgrimes
12530765Scharnierstatic void
1261553Srgrimesusage()
1271553Srgrimes{
1281553Srgrimes
1291553Srgrimes	(void)fprintf(stderr, "usage: vipw\n");
1301553Srgrimes	exit(1);
1311553Srgrimes}
132