xref: /freebsd/usr.sbin/vipw/vipw.c (revision 2357939bc239bd5334a169b62313806178dd8f30)
1 /*
2  * Copyright (c) 1987, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2002 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * Portions of this software were developed for the FreeBSD Project by
8  * ThinkSec AS and NAI Labs, the Security Research Division of Network
9  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
10  * ("CBOSS"), as part of the DARPA CHATS research program.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #if 0
42 #ifndef lint
43 static const char copyright[] =
44 "@(#) Copyright (c) 1987, 1993, 1994\n\
45 	The Regents of the University of California.  All rights reserved.\n";
46 #endif /* not lint */
47 
48 #ifndef lint
49 static char sccsid[] = "@(#)vipw.c	8.3 (Berkeley) 4/2/94";
50 #endif /* not lint */
51 #endif
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
54 
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 
58 #include <err.h>
59 #include <pwd.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 
65 #include <libutil.h>		/* must be after pwd.h */
66 
67 static void	usage(void);
68 
69 int
70 main(int argc, char *argv[])
71 {
72 	const char *passwd_dir = NULL;
73 	int ch, pfd, tfd;
74 	char *line;
75 	size_t len;
76 
77 	while ((ch = getopt(argc, argv, "d:")) != -1)
78 		switch (ch) {
79 		case 'd':
80 			passwd_dir = optarg;
81 			break;
82 		case '?':
83 		default:
84 			usage();
85 		}
86 
87 	argc -= optind;
88 	argv += optind;
89 
90 	if (argc != 0)
91 		usage();
92 
93 	if (pw_init(passwd_dir, NULL) == -1)
94 		err(1, "pw_init()");
95 	if ((pfd = pw_lock()) == -1) {
96 		pw_fini();
97 		err(1, "pw_lock()");
98 	}
99 	if ((tfd = pw_tmp(pfd)) == -1) {
100 		pw_fini();
101 		err(1, "pw_tmp()");
102 	}
103 	(void)close(tfd);
104 	/* Force umask for partial writes made in the edit phase */
105 	(void)umask(077);
106 
107 	for (;;) {
108 		switch (pw_edit(0)) {
109 		case -1:
110 			pw_fini();
111 			err(1, "pw_edit()");
112 		case 0:
113 			pw_fini();
114 			errx(0, "no changes made");
115 		default:
116 			break;
117 		}
118 		if (pw_mkdb(NULL) == 0) {
119 			pw_fini();
120 			errx(0, "password list updated");
121 		}
122 		printf("re-edit the password file? ");
123 		fflush(stdout);
124 		if ((line = fgetln(stdin, &len)) == NULL) {
125 			pw_fini();
126 			err(1, "fgetln()");
127 		}
128 		if (len > 0 && (*line == 'N' || *line == 'n'))
129 			break;
130 	}
131 	pw_fini();
132 	exit(0);
133 }
134 
135 static void
136 usage(void)
137 {
138 
139 	(void)fprintf(stderr, "usage: vipw [-d directory]\n");
140 	exit(1);
141 }
142