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