]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/vipw/vipw.c
This commit was generated by cvs2svn to compensate for changes in r98937,
[FreeBSD/FreeBSD.git] / usr.sbin / vipw / vipw.c
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 #ifndef lint
42 static const char copyright[] =
43 "@(#) Copyright (c) 1987, 1993, 1994\n\
44         The Regents of the University of California.  All rights reserved.\n";
45 #endif /* not lint */
46
47 #ifndef lint
48 #if 0
49 static char sccsid[] = "@(#)vipw.c      8.3 (Berkeley) 4/2/94";
50 #endif
51 static const char rcsid[] =
52   "$FreeBSD$";
53 #endif /* not lint */
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(NULL)) {
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 }