vipw.c revision 1.7 1 /* $NetBSD: vipw.c,v 1.7 2000/12/06 14:04:25 tron 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.7 2000/12/06 14:04:25 tron Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <sys/stat.h>
53
54 #include <err.h>
55 #include <pwd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <fcntl.h>
61 #include <errno.h>
62 #include <util.h>
63
64 int main __P((int, char **));
65 static void copyfile __P((int, int));
66 static void usage __P((void));
67
68 char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN];
69
70 int
71 main(int argc, char *argv[])
72 {
73 char *prefix;
74 int pfd, tfd;
75 struct stat begin, end;
76 int ch;
77
78 prefix = "";
79 while ((ch = getopt(argc, argv, "d:")) != -1) {
80 switch (ch) {
81 case 'd':
82 prefix = optarg;
83 if (pw_setprefix(prefix) < 0)
84 err(1, "%s", prefix);
85 break;
86 case '?':
87 default:
88 usage();
89 }
90 }
91 argc -= optind;
92 argv += optind;
93
94 if (argc != 0)
95 usage();
96
97 (void)snprintf(mpwd, sizeof(mpwd), "%s%s", prefix, _PATH_MASTERPASSWD);
98 (void)snprintf(mpwdl, sizeof(mpwdl), "%s%s", prefix,
99 _PATH_MASTERPASSWD_LOCK);
100
101 pw_init();
102 tfd = pw_lock(0);
103 if (tfd < 0) {
104 if (errno == EEXIST)
105 errx(1, "the passwd file is busy.");
106 else
107 err(1, "%s", mpwdl);
108 }
109
110 pfd = open(mpwd, O_RDONLY, 0);
111 if (pfd < 0)
112 pw_error(mpwd, 1, 1);
113 copyfile(pfd, tfd);
114 (void)close(tfd);
115
116 for (;;) {
117 if (stat(mpwdl, &begin))
118 pw_error(mpwdl, 1, 1);
119 pw_edit(0, NULL);
120 if (stat(mpwdl, &end))
121 pw_error(mpwdl, 1, 1);
122 if (begin.st_mtime == end.st_mtime) {
123 warnx("no changes made");
124 pw_error((char *)NULL, 0, 0);
125 }
126 if (pw_mkdb() == 0)
127 break;
128 pw_prompt();
129 }
130 return 0;
131 }
132
133 static void
134 copyfile(int from, int to)
135 {
136 int nr, nw, off;
137 char buf[8*1024];
138
139 while ((nr = read(from, buf, sizeof(buf))) > 0)
140 for (off = 0; off < nr; nr -= nw, off += nw)
141 if ((nw = write(to, buf + off, nr)) < 0)
142 pw_error(mpwdl, 1, 1);
143 if (nr < 0)
144 pw_error(mpwd, 1, 1);
145 }
146
147 static void
148 usage(void)
149 {
150 extern char *__progname;
151
152 (void)fprintf(stderr, "Usage: %s [-d directory]\n", __progname);
153 exit(1);
154 }
155