xref: /freebsd/usr.sbin/vipw/vipw.c (revision 93deb2ae12638fa215ba60ded9c39d94e10b7d8b)
1dea673e9SRodney W. Grimes /*
2dea673e9SRodney W. Grimes  * Copyright (c) 1987, 1993, 1994
3dea673e9SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4dea673e9SRodney W. Grimes  *
5dea673e9SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6dea673e9SRodney W. Grimes  * modification, are permitted provided that the following conditions
7dea673e9SRodney W. Grimes  * are met:
8dea673e9SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10dea673e9SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12dea673e9SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13dea673e9SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14dea673e9SRodney W. Grimes  *    must display the following acknowledgement:
15dea673e9SRodney W. Grimes  *	This product includes software developed by the University of
16dea673e9SRodney W. Grimes  *	California, Berkeley and its contributors.
17dea673e9SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18dea673e9SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19dea673e9SRodney W. Grimes  *    without specific prior written permission.
20dea673e9SRodney W. Grimes  *
21dea673e9SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22dea673e9SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23dea673e9SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24dea673e9SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25dea673e9SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26dea673e9SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27dea673e9SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28dea673e9SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29dea673e9SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30dea673e9SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31dea673e9SRodney W. Grimes  * SUCH DAMAGE.
32dea673e9SRodney W. Grimes  */
33dea673e9SRodney W. Grimes 
34dea673e9SRodney W. Grimes #ifndef lint
35acb61b9eSPhilippe Charnier static const char copyright[] =
36dea673e9SRodney W. Grimes "@(#) Copyright (c) 1987, 1993, 1994\n\
37dea673e9SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
38dea673e9SRodney W. Grimes #endif /* not lint */
39dea673e9SRodney W. Grimes 
40dea673e9SRodney W. Grimes #ifndef lint
41acb61b9eSPhilippe Charnier #if 0
42dea673e9SRodney W. Grimes static char sccsid[] = "@(#)vipw.c	8.3 (Berkeley) 4/2/94";
43acb61b9eSPhilippe Charnier #endif
44acb61b9eSPhilippe Charnier static const char rcsid[] =
4597d92980SPeter Wemm   "$FreeBSD$";
46dea673e9SRodney W. Grimes #endif /* not lint */
47dea673e9SRodney W. Grimes 
48dea673e9SRodney W. Grimes #include <sys/types.h>
49dea673e9SRodney W. Grimes #include <sys/stat.h>
50dea673e9SRodney W. Grimes 
51dea673e9SRodney W. Grimes #include <err.h>
52dea673e9SRodney W. Grimes #include <pwd.h>
53dea673e9SRodney W. Grimes #include <stdio.h>
54dea673e9SRodney W. Grimes #include <stdlib.h>
55dea673e9SRodney W. Grimes #include <string.h>
56dea673e9SRodney W. Grimes #include <unistd.h>
57dea673e9SRodney W. Grimes 
58dea673e9SRodney W. Grimes #include "pw_util.h"
59dea673e9SRodney W. Grimes 
60af2d5f9bSSheldon Hearn extern char *mppath;
61af2d5f9bSSheldon Hearn extern char *masterpasswd;
62dea673e9SRodney W. Grimes char *tempname;
63dea673e9SRodney W. Grimes 
6493deb2aeSDag-Erling Smørgrav static void	copyfile(int, int);
6593deb2aeSDag-Erling Smørgrav static void	usage(void);
66dea673e9SRodney W. Grimes 
67dea673e9SRodney W. Grimes int
6893deb2aeSDag-Erling Smørgrav main(int argc, char *argv[])
69dea673e9SRodney W. Grimes {
70dea673e9SRodney W. Grimes 	int pfd, tfd;
71dea673e9SRodney W. Grimes 	struct stat begin, end;
72dea673e9SRodney W. Grimes 	int ch;
73dea673e9SRodney W. Grimes 
74af2d5f9bSSheldon Hearn 	while ((ch = getopt(argc, argv, "d:")) != -1)
75dea673e9SRodney W. Grimes 		switch (ch) {
76af2d5f9bSSheldon Hearn 		case 'd':
77af2d5f9bSSheldon Hearn 			if ((masterpasswd = malloc(strlen(optarg) +
78af2d5f9bSSheldon Hearn 			    strlen(_MASTERPASSWD) + 2)) == NULL)
79af2d5f9bSSheldon Hearn 				err(1, NULL);
80af2d5f9bSSheldon Hearn 			strcpy(masterpasswd, optarg);
81af2d5f9bSSheldon Hearn 			if (masterpasswd[strlen(masterpasswd) - 1] != '/')
8207ce2a90SSheldon Hearn 				strcat(masterpasswd, "/" _MASTERPASSWD);
8307ce2a90SSheldon Hearn 			else
84af2d5f9bSSheldon Hearn 				strcat(masterpasswd, _MASTERPASSWD);
85af2d5f9bSSheldon Hearn 			if ((mppath = strdup(optarg)) == NULL)
86af2d5f9bSSheldon Hearn 				err(1, NULL);
87af2d5f9bSSheldon Hearn 			if (mppath[strlen(mppath) - 1] == '/')
88af2d5f9bSSheldon Hearn 				mppath[strlen(mppath) - 1] = '\0';
89af2d5f9bSSheldon Hearn 			break;
90dea673e9SRodney W. Grimes 		case '?':
91dea673e9SRodney W. Grimes 		default:
92dea673e9SRodney W. Grimes 			usage();
93dea673e9SRodney W. Grimes 		}
94dea673e9SRodney W. Grimes 
95dea673e9SRodney W. Grimes 	argc -= optind;
96dea673e9SRodney W. Grimes 	argv += optind;
97dea673e9SRodney W. Grimes 
98dea673e9SRodney W. Grimes 	if (argc != 0)
99dea673e9SRodney W. Grimes 		usage();
100dea673e9SRodney W. Grimes 
101dea673e9SRodney W. Grimes 	pw_init();
102dea673e9SRodney W. Grimes 	pfd = pw_lock();
103dea673e9SRodney W. Grimes 	tfd = pw_tmp();
104dea673e9SRodney W. Grimes 	copyfile(pfd, tfd);
105dea673e9SRodney W. Grimes 	(void)close(tfd);
1062ece3ed4SPierre Beyssac 	/* Force umask for partial writes made in the edit phase */
1072ece3ed4SPierre Beyssac 	(void)umask(077);
108dea673e9SRodney W. Grimes 
109dea673e9SRodney W. Grimes 	for (;;) {
110dea673e9SRodney W. Grimes 		if (stat(tempname, &begin))
111dea673e9SRodney W. Grimes 			pw_error(tempname, 1, 1);
112dea673e9SRodney W. Grimes 		pw_edit(0);
113dea673e9SRodney W. Grimes 		if (stat(tempname, &end))
114dea673e9SRodney W. Grimes 			pw_error(tempname, 1, 1);
115dea673e9SRodney W. Grimes 		if (begin.st_mtime == end.st_mtime) {
116dea673e9SRodney W. Grimes 			warnx("no changes made");
117dea673e9SRodney W. Grimes 			pw_error((char *)NULL, 0, 0);
118dea673e9SRodney W. Grimes 		}
11979a1b8d9SGuido van Rooij 		if (pw_mkdb((char *)NULL))
120dea673e9SRodney W. Grimes 			break;
121dea673e9SRodney W. Grimes 		pw_prompt();
122dea673e9SRodney W. Grimes 	}
123dea673e9SRodney W. Grimes 	exit(0);
124dea673e9SRodney W. Grimes }
125dea673e9SRodney W. Grimes 
12693deb2aeSDag-Erling Smørgrav static void
12793deb2aeSDag-Erling Smørgrav copyfile(int from, int to)
128dea673e9SRodney W. Grimes {
129dea673e9SRodney W. Grimes 	int nr, nw, off;
130dea673e9SRodney W. Grimes 	char buf[8*1024];
131dea673e9SRodney W. Grimes 
132dea673e9SRodney W. Grimes 	while ((nr = read(from, buf, sizeof(buf))) > 0)
133dea673e9SRodney W. Grimes 		for (off = 0; off < nr; nr -= nw, off += nw)
134dea673e9SRodney W. Grimes 			if ((nw = write(to, buf + off, nr)) < 0)
135dea673e9SRodney W. Grimes 				pw_error(tempname, 1, 1);
136dea673e9SRodney W. Grimes 	if (nr < 0)
1377cf53d0aSSheldon Hearn 		pw_error(masterpasswd, 1, 1);
138dea673e9SRodney W. Grimes }
139dea673e9SRodney W. Grimes 
140acb61b9eSPhilippe Charnier static void
14193deb2aeSDag-Erling Smørgrav usage(void)
142dea673e9SRodney W. Grimes {
143dea673e9SRodney W. Grimes 
144af2d5f9bSSheldon Hearn 	(void)fprintf(stderr, "usage: vipw [ -d directory ]\n");
145dea673e9SRodney W. Grimes 	exit(1);
146dea673e9SRodney W. Grimes }
147