passwd.c revision 1.15.2.2 1 /* $NetBSD: passwd.c,v 1.15.2.2 2000/10/04 14:11:02 he Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993, 1994, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 __RCSID("$NetBSD: passwd.c,v 1.15.2.2 2000/10/04 14:11:02 he Exp $");
39 #endif /* LIBC_SCCS and not lint */
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/resource.h>
45 #include <sys/wait.h>
46
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <limits.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 #include <util.h>
60
61 static void pw_cont __P((int sig));
62 static int pw_equal __P((char *buf, struct passwd *old_pw));
63
64 int
65 pw_lock(retries)
66 int retries;
67 {
68 int i, fd;
69 mode_t old_mode;
70 int oerrno;
71
72 /* Acquire the lock file. */
73 old_mode = umask(0);
74 fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL, 0600);
75 for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
76 sleep(1);
77 fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL,
78 0600);
79 }
80 oerrno = errno;
81 (void)umask(old_mode);
82 errno = oerrno;
83 return(fd);
84 }
85
86 int
87 pw_mkdb()
88 {
89 int pstat;
90 pid_t pid;
91
92 pid = vfork();
93 if (pid == -1)
94 return (-1);
95
96 if (pid == 0) {
97 execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p",
98 _PATH_MASTERPASSWD_LOCK, NULL);
99 _exit(1);
100 }
101 pid = waitpid(pid, &pstat, 0);
102 if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
103 return(-1);
104 return(0);
105 }
106
107 int
108 pw_abort()
109 {
110 return(unlink(_PATH_MASTERPASSWD_LOCK));
111 }
112
113 /* Everything below this point is intended for the convenience of programs
114 * which allow a user to interactively edit the passwd file. Errors in the
115 * routines below will cause the process to abort. */
116
117 static pid_t editpid = -1;
118
119 static void
120 pw_cont(sig)
121 int sig;
122 {
123
124 if (editpid != -1)
125 kill(editpid, sig);
126 }
127
128 void
129 pw_init()
130 {
131 struct rlimit rlim;
132
133 /* Unlimited resource limits. */
134 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
135 (void)setrlimit(RLIMIT_CPU, &rlim);
136 (void)setrlimit(RLIMIT_FSIZE, &rlim);
137 (void)setrlimit(RLIMIT_STACK, &rlim);
138 (void)setrlimit(RLIMIT_DATA, &rlim);
139 (void)setrlimit(RLIMIT_RSS, &rlim);
140
141 /* Don't drop core (not really necessary, but GP's). */
142 rlim.rlim_cur = rlim.rlim_max = 0;
143 (void)setrlimit(RLIMIT_CORE, &rlim);
144
145 /* Turn off signals. */
146 (void)signal(SIGALRM, SIG_IGN);
147 (void)signal(SIGHUP, SIG_IGN);
148 (void)signal(SIGINT, SIG_IGN);
149 (void)signal(SIGPIPE, SIG_IGN);
150 (void)signal(SIGQUIT, SIG_IGN);
151 (void)signal(SIGTERM, SIG_IGN);
152 (void)signal(SIGCONT, pw_cont);
153 }
154
155 void
156 pw_edit(notsetuid, filename)
157 int notsetuid;
158 const char *filename;
159 {
160 int pstat;
161 char *p, *editor;
162 char *argp[] = { "sh", "-c", NULL, NULL };
163
164 #ifdef __GNUC__
165 (void) &editor;
166 #endif
167
168 if (filename == NULL)
169 filename = _PATH_MASTERPASSWD_LOCK;
170
171 if ((editor = getenv("EDITOR")) == NULL)
172 editor = _PATH_VI;
173
174 p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
175 if (p == NULL)
176 return;
177
178 sprintf(p, "%s %s", editor, filename);
179 argp[2] = p;
180
181 switch(editpid = vfork()) {
182 case -1:
183 free(p);
184 return;
185 case 0:
186 if (notsetuid) {
187 setgid(getgid());
188 setuid(getuid());
189 }
190 execvp(_PATH_BSHELL, argp);
191 _exit(1);
192 }
193
194 free(p);
195
196 for (;;) {
197 editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
198 if (editpid == -1)
199 pw_error(editor, 1, 1);
200 else if (WIFSTOPPED(pstat))
201 raise(WSTOPSIG(pstat));
202 else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
203 break;
204 else
205 pw_error(editor, 1, 1);
206 }
207 editpid = -1;
208 }
209
210 void
211 pw_prompt()
212 {
213 int c;
214
215 (void)printf("re-edit the password file? [y]: ");
216 (void)fflush(stdout);
217 c = getchar();
218 if (c != EOF && c != '\n')
219 while (getchar() != '\n');
220 if (c == 'n')
221 pw_error(NULL, 0, 0);
222 }
223
224 /* for use in pw_copy(). Compare a pw entry to a pw struct. */
225 static int
226 pw_equal (buf, pw)
227 char *buf;
228 struct passwd *pw;
229 {
230 struct passwd buf_pw;
231 int len = strlen (buf);
232 if (buf[len-1] == '\n')
233 buf[len-1] = '\0';
234 if (!pw_scan(buf, &buf_pw, NULL))
235 return 0;
236 return !strcmp(pw->pw_name, buf_pw.pw_name)
237 && pw->pw_uid == buf_pw.pw_uid
238 && pw->pw_gid == buf_pw.pw_gid
239 && !strcmp(pw->pw_class, buf_pw.pw_class)
240 && (long)pw->pw_change == (long)buf_pw.pw_change
241 && (long)pw->pw_expire == (long)buf_pw.pw_expire
242 && !strcmp(pw->pw_gecos, buf_pw.pw_gecos)
243 && !strcmp(pw->pw_dir, buf_pw.pw_dir)
244 && !strcmp(pw->pw_shell, buf_pw.pw_shell);
245 }
246
247 void
248 pw_copy(ffd, tfd, pw, old_pw)
249 int ffd, tfd;
250 struct passwd *pw, *old_pw;
251 {
252 FILE *from, *to;
253 int done;
254 char *p, buf[8192];
255
256 if (!(from = fdopen(ffd, "r")))
257 pw_error(_PATH_MASTERPASSWD, 1, 1);
258 if (!(to = fdopen(tfd, "w")))
259 pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
260
261 for (done = 0; fgets(buf, sizeof(buf), from);) {
262 if (!strchr(buf, '\n')) {
263 warnx("%s: line too long", _PATH_MASTERPASSWD);
264 pw_error(NULL, 0, 1);
265 }
266 if (done) {
267 (void)fprintf(to, "%s", buf);
268 if (ferror(to))
269 goto err;
270 continue;
271 }
272 if (!(p = strchr(buf, ':'))) {
273 warnx("%s: corrupted entry", _PATH_MASTERPASSWD);
274 pw_error(NULL, 0, 1);
275 }
276 *p = '\0';
277 if (strcmp(buf, pw->pw_name)) {
278 *p = ':';
279 (void)fprintf(to, "%s", buf);
280 if (ferror(to))
281 goto err;
282 continue;
283 }
284 *p = ':';
285 if (old_pw && !pw_equal(buf, old_pw)) {
286 warnx("%s: entry inconsistent",
287 _PATH_MASTERPASSWD);
288 pw_error(NULL, 0, 1);
289 }
290 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
291 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
292 pw->pw_class, (long)pw->pw_change, (long)pw->pw_expire,
293 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
294 done = 1;
295 if (ferror(to))
296 goto err;
297 }
298 /* Only append a new entry if real uid is root! */
299 if (!done) {
300 if (getuid() == 0)
301 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
302 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
303 pw->pw_class, (long)pw->pw_change,
304 (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
305 pw->pw_shell);
306 else
307 warnx("%s: changes not made, no such entry",
308 _PATH_MASTERPASSWD);
309 }
310
311 if (ferror(to))
312 err: pw_error(NULL, 1, 1);
313 (void)fclose(to);
314 }
315
316 void
317 pw_error(name, err, eval)
318 const char *name;
319 int err, eval;
320 {
321 if (err)
322 warn("%s", name);
323
324 warnx("%s: unchanged", _PATH_MASTERPASSWD);
325 pw_abort();
326 exit(eval);
327 }
328