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