Fix a mbuf statistics bug.
[dragonfly.git] / usr.sbin / vipw / pw_util.c
blobe9dc139772b4f486d1498a036881d17bf1fd61f9
1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)pw_util.c 8.3 (Berkeley) 4/2/94
34 * $FreeBSD: src/usr.sbin/vipw/pw_util.c,v 1.17.2.4 2002/09/04 15:28:10 des Exp $
35 * $DragonFly: src/usr.sbin/vipw/pw_util.c,v 1.3 2004/12/18 22:48:14 swildner Exp $
39 * This file is used by all the "password" programs; vipw(8), chpass(1),
40 * and passwd(1).
43 #include <sys/param.h>
44 #include <sys/errno.h>
45 #include <sys/time.h>
46 #include <sys/resource.h>
47 #include <sys/stat.h>
48 #include <sys/wait.h>
50 #include <err.h>
51 #include <fcntl.h>
52 #include <paths.h>
53 #include <pwd.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
60 #include "pw_util.h"
62 extern char *tempname;
63 static pid_t editpid = -1;
64 static int lockfd;
65 char *mppath = _PATH_PWD;
66 char *masterpasswd = _PATH_MASTERPASSWD;
68 void
69 pw_cont(sig)
70 int sig;
73 if (editpid != -1)
74 kill(editpid, sig);
77 void
78 pw_init()
80 struct rlimit rlim;
82 /* Unlimited resource limits. */
83 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
84 setrlimit(RLIMIT_CPU, &rlim);
85 setrlimit(RLIMIT_FSIZE, &rlim);
86 setrlimit(RLIMIT_STACK, &rlim);
87 setrlimit(RLIMIT_DATA, &rlim);
88 setrlimit(RLIMIT_RSS, &rlim);
90 /* Don't drop core (not really necessary, but GP's). */
91 rlim.rlim_cur = rlim.rlim_max = 0;
92 setrlimit(RLIMIT_CORE, &rlim);
94 /* Turn off signals. */
95 signal(SIGALRM, SIG_IGN);
96 signal(SIGHUP, SIG_IGN);
97 signal(SIGINT, SIG_IGN);
98 signal(SIGPIPE, SIG_IGN);
99 signal(SIGQUIT, SIG_IGN);
100 signal(SIGTERM, SIG_IGN);
101 signal(SIGCONT, pw_cont);
103 /* Create with exact permissions. */
104 umask(0);
108 pw_lock()
111 * If the master password file doesn't exist, the system is hosed.
112 * Might as well try to build one. Set the close-on-exec bit so
113 * that users can't get at the encrypted passwords while editing.
114 * Open should allow flock'ing the file; see 4.4BSD. XXX
116 for (;;) {
117 struct stat st;
119 lockfd = open(masterpasswd, O_RDONLY, 0);
120 if (lockfd < 0 || fcntl(lockfd, F_SETFD, 1) == -1)
121 err(1, "%s", masterpasswd);
122 if (flock(lockfd, LOCK_EX|LOCK_NB))
123 errx(1, "the password db file is busy");
126 * If the password file was replaced while we were trying to
127 * get the lock, our hardlink count will be 0 and we have to
128 * close and retry.
130 if (fstat(lockfd, &st) < 0)
131 errx(1, "fstat() failed");
132 if (st.st_nlink != 0)
133 break;
134 close(lockfd);
135 lockfd = -1;
137 return (lockfd);
141 pw_tmp()
143 static char path[MAXPATHLEN];
144 int fd;
145 char *p;
147 if ((p = strrchr(masterpasswd, '/')) == NULL)
148 strcpy(path, "pw.XXXXXX");
149 else
150 if (snprintf(path, sizeof path, "%.*s/pw.XXXXXX",
151 (int)(p - masterpasswd), masterpasswd) >= sizeof path)
152 errx(1, "%s: path too long", masterpasswd);
153 if ((fd = mkstemp(path)) == -1)
154 err(1, "%s", path);
155 tempname = path;
156 return (fd);
160 pw_mkdb(username)
161 char *username;
163 int pstat;
164 pid_t pid;
166 fflush(stderr);
167 if (!(pid = fork())) {
168 if(!username) {
169 warnx("rebuilding the database...");
170 execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", "-d", mppath,
171 tempname, NULL);
172 } else {
173 warnx("updating the database...");
174 execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", "-d", mppath,
175 "-u", username, tempname, NULL);
177 pw_error(_PATH_PWD_MKDB, 1, 1);
179 pid = waitpid(pid, &pstat, 0);
180 if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
181 return (0);
182 warnx("done");
183 return (1);
186 void
187 pw_edit(notsetuid)
188 int notsetuid;
190 int pstat;
191 char *p, *editor;
193 if (!(editor = getenv("EDITOR")))
194 editor = _PATH_VI;
195 if ((p = strrchr(editor, '/')))
196 ++p;
197 else
198 p = editor;
200 if (!(editpid = fork())) {
201 if (notsetuid) {
202 setgid(getgid());
203 setuid(getuid());
205 errno = 0;
206 execlp(editor, p, tempname, NULL);
207 _exit(errno);
209 for (;;) {
210 editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
211 errno = WEXITSTATUS(pstat);
212 if (editpid == -1)
213 pw_error(editor, 1, 1);
214 else if (WIFSTOPPED(pstat))
215 raise(WSTOPSIG(pstat));
216 else if (WIFEXITED(pstat) && errno == 0)
217 break;
218 else
219 pw_error(editor, 1, 1);
221 editpid = -1;
224 void
225 pw_prompt()
227 int c, first;
229 printf("re-edit the password file? [y]: ");
230 fflush(stdout);
231 first = c = getchar();
232 while (c != '\n' && c != EOF)
233 c = getchar();
234 if (first == 'n')
235 pw_error(NULL, 0, 0);
238 void
239 pw_error(name, err, eval)
240 char *name;
241 int err, eval;
243 #ifdef YP
244 extern int _use_yp;
245 #endif /* YP */
246 if (err) {
247 if (name != NULL)
248 warn("%s", name);
249 else
250 warn(NULL);
252 #ifdef YP
253 if (_use_yp)
254 warnx("NIS information unchanged");
255 else
256 #endif /* YP */
257 warnx("%s: unchanged", masterpasswd);
258 unlink(tempname);
259 exit(eval);