Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / vipw / pw_util.c
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
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.
20  *
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.
32  */
33
34 #ifndef lint
35 #if 0
36 static const char sccsid[] = "@(#)pw_util.c     8.3 (Berkeley) 4/2/94";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD: src/usr.sbin/vipw/pw_util.c,v 1.17.2.4 2002/09/04 15:28:10 des Exp $";
40 #endif /* not lint */
41
42 /*
43  * This file is used by all the "password" programs; vipw(8), chpass(1),
44  * and passwd(1).
45  */
46
47 #include <sys/param.h>
48 #include <sys/errno.h>
49 #include <sys/time.h>
50 #include <sys/resource.h>
51 #include <sys/stat.h>
52 #include <sys/wait.h>
53
54 #include <err.h>
55 #include <fcntl.h>
56 #include <paths.h>
57 #include <pwd.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #include "pw_util.h"
65
66 extern char *tempname;
67 static pid_t editpid = -1;
68 static int lockfd;
69 char *mppath = _PATH_PWD;
70 char *masterpasswd = _PATH_MASTERPASSWD;
71
72 void
73 pw_cont(sig)
74         int sig;
75 {
76
77         if (editpid != -1)
78                 kill(editpid, sig);
79 }
80
81 void
82 pw_init()
83 {
84         struct rlimit rlim;
85
86         /* Unlimited resource limits. */
87         rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
88         (void)setrlimit(RLIMIT_CPU, &rlim);
89         (void)setrlimit(RLIMIT_FSIZE, &rlim);
90         (void)setrlimit(RLIMIT_STACK, &rlim);
91         (void)setrlimit(RLIMIT_DATA, &rlim);
92         (void)setrlimit(RLIMIT_RSS, &rlim);
93
94         /* Don't drop core (not really necessary, but GP's). */
95         rlim.rlim_cur = rlim.rlim_max = 0;
96         (void)setrlimit(RLIMIT_CORE, &rlim);
97
98         /* Turn off signals. */
99         (void)signal(SIGALRM, SIG_IGN);
100         (void)signal(SIGHUP, SIG_IGN);
101         (void)signal(SIGINT, SIG_IGN);
102         (void)signal(SIGPIPE, SIG_IGN);
103         (void)signal(SIGQUIT, SIG_IGN);
104         (void)signal(SIGTERM, SIG_IGN);
105         (void)signal(SIGCONT, pw_cont);
106
107         /* Create with exact permissions. */
108         (void)umask(0);
109 }
110
111 int
112 pw_lock()
113 {
114         /*
115          * If the master password file doesn't exist, the system is hosed.
116          * Might as well try to build one.  Set the close-on-exec bit so
117          * that users can't get at the encrypted passwords while editing.
118          * Open should allow flock'ing the file; see 4.4BSD.    XXX
119          */
120         for (;;) {
121                 struct stat st;
122
123                 lockfd = open(masterpasswd, O_RDONLY, 0);
124                 if (lockfd < 0 || fcntl(lockfd, F_SETFD, 1) == -1)
125                         err(1, "%s", masterpasswd);
126                 if (flock(lockfd, LOCK_EX|LOCK_NB))
127                         errx(1, "the password db file is busy");
128
129                 /*
130                  * If the password file was replaced while we were trying to
131                  * get the lock, our hardlink count will be 0 and we have to
132                  * close and retry.
133                  */
134                 if (fstat(lockfd, &st) < 0)
135                         errx(1, "fstat() failed");
136                 if (st.st_nlink != 0)
137                         break;
138                 close(lockfd);
139                 lockfd = -1;
140         }
141         return (lockfd);
142 }
143
144 int
145 pw_tmp()
146 {
147         static char path[MAXPATHLEN];
148         int fd;
149         char *p;
150
151         if ((p = strrchr(masterpasswd, '/')) == NULL)
152                 strcpy(path, "pw.XXXXXX");
153         else
154                 if (snprintf(path, sizeof path, "%.*s/pw.XXXXXX",
155                     (int)(p - masterpasswd), masterpasswd) >= sizeof path)
156                         errx(1, "%s: path too long", masterpasswd);
157         if ((fd = mkstemp(path)) == -1)
158                 err(1, "%s", path);
159         tempname = path;
160         return (fd);
161 }
162
163 int
164 pw_mkdb(username)
165 char *username;
166 {
167         int pstat;
168         pid_t pid;
169
170         (void)fflush(stderr);
171         if (!(pid = fork())) {
172                 if(!username) {
173                         warnx("rebuilding the database...");
174                         execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", "-d", mppath,
175                             tempname, NULL);
176                 } else {
177                         warnx("updating the database...");
178                         execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", "-d", mppath,
179                             "-u", username, tempname, NULL);
180                 }
181                 pw_error(_PATH_PWD_MKDB, 1, 1);
182         }
183         pid = waitpid(pid, &pstat, 0);
184         if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
185                 return (0);
186         warnx("done");
187         return (1);
188 }
189
190 void
191 pw_edit(notsetuid)
192         int notsetuid;
193 {
194         int pstat;
195         char *p, *editor;
196
197         if (!(editor = getenv("EDITOR")))
198                 editor = _PATH_VI;
199         if ((p = strrchr(editor, '/')))
200                 ++p;
201         else
202                 p = editor;
203
204         if (!(editpid = fork())) {
205                 if (notsetuid) {
206                         (void)setgid(getgid());
207                         (void)setuid(getuid());
208                 }
209                 errno = 0;
210                 execlp(editor, p, tempname, NULL);
211                 _exit(errno);
212         }
213         for (;;) {
214                 editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
215                 errno = WEXITSTATUS(pstat);
216                 if (editpid == -1)
217                         pw_error(editor, 1, 1);
218                 else if (WIFSTOPPED(pstat))
219                         raise(WSTOPSIG(pstat));
220                 else if (WIFEXITED(pstat) && errno == 0)
221                         break;
222                 else
223                         pw_error(editor, 1, 1);
224         }
225         editpid = -1;
226 }
227
228 void
229 pw_prompt()
230 {
231         int c, first;
232
233         (void)printf("re-edit the password file? [y]: ");
234         (void)fflush(stdout);
235         first = c = getchar();
236         while (c != '\n' && c != EOF)
237                 c = getchar();
238         if (first == 'n')
239                 pw_error(NULL, 0, 0);
240 }
241
242 void
243 pw_error(name, err, eval)
244         char *name;
245         int err, eval;
246 {
247 #ifdef YP
248         extern int _use_yp;
249 #endif /* YP */
250         if (err) {
251                 if (name != NULL)
252                         warn("%s", name);
253                 else
254                         warn(NULL);
255         }
256 #ifdef YP
257         if (_use_yp)
258                 warnx("NIS information unchanged");
259         else
260 #endif /* YP */
261         warnx("%s: unchanged", masterpasswd);
262         (void)unlink(tempname);
263         exit(eval);
264 }