vipw.c revision 1.6 1 /* $NetBSD: vipw.c,v 1.6 1998/12/09 12:40:15 christos 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.6 1998/12/09 12:40:15 christos 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 <errno.h>
61 #include <util.h>
62
63 int main __P((int, char **));
64 static void copyfile __P((int, int));
65 static void usage __P((void));
66
67 int
68 main(argc, argv)
69 int argc;
70 char *argv[];
71 {
72 int pfd, tfd;
73 struct stat begin, end;
74 int ch;
75
76 while ((ch = getopt(argc, argv, "")) != -1) {
77 switch (ch) {
78 case '?':
79 default:
80 usage();
81 }
82 }
83 argc -= optind;
84 argv += optind;
85
86 if (argc != 0)
87 usage();
88
89 pw_init();
90 tfd = pw_lock(0);
91 if (tfd < 0) {
92 if (errno == EEXIST)
93 errx(1, "the passwd file is busy.");
94 else
95 err(1, "%s", _PATH_MASTERPASSWD_LOCK);
96 }
97
98 pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
99 if (pfd < 0)
100 pw_error(_PATH_MASTERPASSWD, 1, 1);
101 copyfile(pfd, tfd);
102 (void)close(tfd);
103
104 for (;;) {
105 if (stat(_PATH_MASTERPASSWD_LOCK, &begin))
106 pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
107 pw_edit(0, NULL);
108 if (stat(_PATH_MASTERPASSWD_LOCK, &end))
109 pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
110 if (begin.st_mtime == end.st_mtime) {
111 warnx("no changes made");
112 pw_error((char *)NULL, 0, 0);
113 }
114 if (pw_mkdb() == 0)
115 break;
116 pw_prompt();
117 }
118 return 0;
119 }
120
121 static void
122 copyfile(from, to)
123 int from, to;
124 {
125 int nr, nw, off;
126 char buf[8*1024];
127
128 while ((nr = read(from, buf, sizeof(buf))) > 0)
129 for (off = 0; off < nr; nr -= nw, off += nw)
130 if ((nw = write(to, buf + off, nr)) < 0)
131 pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
132 if (nr < 0)
133 pw_error(_PATH_MASTERPASSWD, 1, 1);
134 }
135
136 static void
137 usage()
138 {
139 extern char *__progname;
140
141 (void)fprintf(stderr, "Usage: %s\n", __progname);
142 exit(1);
143 }
144