Remove building with NOCRYPTO option
[minix.git] / usr.sbin / vipw / vipw.c
blob39bc0e2aa2fc61e1244e88d01aff25c7b8abe18e
1 /* $NetBSD: vipw.c,v 1.16 2011/08/31 16:25:00 plunky Exp $ */
3 /*
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)vipw.c 8.3 (Berkeley) 4/2/94";
41 #else
42 __RCSID("$NetBSD: vipw.c,v 1.16 2011/08/31 16:25:00 plunky Exp $");
43 #endif
44 #endif /* not lint */
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/stat.h>
50 #include <err.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <fcntl.h>
57 #include <errno.h>
58 #include <util.h>
60 static void copyfile(int, int);
61 __dead static void usage(void);
63 static char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN];
65 int
66 main(int argc, char *argv[])
68 const char *prefix;
69 int pfd, tfd;
70 struct stat begin, end;
71 int ch;
73 prefix = "";
74 while ((ch = getopt(argc, argv, "d:")) != -1) {
75 switch (ch) {
76 case 'd':
77 prefix = optarg;
78 if (pw_setprefix(prefix) < 0)
79 err(1, "%s", prefix);
80 break;
81 case '?':
82 default:
83 usage();
86 argc -= optind;
87 argv += optind;
89 if (argc != 0)
90 usage();
92 (void)snprintf(mpwd, sizeof(mpwd), "%s%s", prefix, _PATH_MASTERPASSWD);
93 (void)snprintf(mpwdl, sizeof(mpwdl), "%s%s", prefix,
94 _PATH_MASTERPASSWD_LOCK);
96 pw_init();
97 tfd = pw_lock(0);
98 if (tfd < 0) {
99 if (errno == EEXIST)
100 errx(1, "the passwd file is busy.");
101 else
102 err(1, "%s", mpwdl);
105 pfd = open(mpwd, O_RDONLY, 0);
106 if (pfd < 0)
107 pw_error(mpwd, 1, 1);
108 copyfile(pfd, tfd);
109 (void)close(tfd);
111 for (;;) {
112 if (stat(mpwdl, &begin))
113 pw_error(mpwdl, 1, 1);
114 pw_edit(0, NULL);
115 if (stat(mpwdl, &end))
116 pw_error(mpwdl, 1, 1);
117 if (begin.st_mtime == end.st_mtime &&
118 begin.st_mtimensec == end.st_mtimensec) {
119 warnx("no changes made");
120 pw_error(NULL, 0, 0);
122 if (pw_mkdb(NULL, 0) == 0)
123 break;
124 pw_prompt();
126 return 0;
129 static void
130 copyfile(int from, int to)
132 int nr, nw, off;
133 char buf[8*1024];
135 while ((nr = read(from, buf, sizeof(buf))) > 0)
136 for (off = 0; off < nr; nr -= nw, off += nw)
137 if ((nw = write(to, buf + off, nr)) < 0)
138 pw_error(mpwdl, 1, 1);
139 if (nr < 0)
140 pw_error(mpwd, 1, 1);
143 static void
144 usage(void)
147 (void)fprintf(stderr, "usage: %s [-d directory]\n", getprogname());
148 exit(1);