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