passwd.c revision 1.1 1 /*
2 * Copyright (c) 1987, 1993, 1994, 1995
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 #if defined(LIBC_SCCS) && !defined(lint)
35 static char rcsid[] = "$NetBSD: passwd.c,v 1.1 1996/05/15 21:42:31 jtc Exp $";
36 #endif /* LIBC_SCCS and not lint */
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #include <sys/wait.h>
43
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <pwd.h>
50 #include <errno.h>
51 #include <paths.h>
52 #include <signal.h>
53 #include <limits.h>
54 #include <util.h>
55
56 static void pw_cont __P((int sig));
57
58 int
59 pw_lock(retries)
60 int retries;
61 {
62 int i, fd;
63 mode_t old_mode;
64
65 /* Acquire the lock file. */
66 old_mode = umask(0);
67 fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL, 0600);
68 for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
69 sleep(1);
70 fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL,
71 0600);
72 }
73 umask(old_mode);
74 return(fd);
75 }
76
77 int
78 pw_mkdb()
79 {
80 int pstat;
81 pid_t pid;
82
83 pid = vfork();
84 if (pid == 0) {
85 execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p",
86 _PATH_MASTERPASSWD_LOCK, NULL);
87 exit(1);
88 }
89 pid = waitpid(pid, &pstat, 0);
90 if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0) {
91 unlink(_PATH_MASTERPASSWD_LOCK);
92 return(-1);
93 }
94 return(0);
95 }
96
97 int
98 pw_abort()
99 {
100 return(unlink(_PATH_MASTERPASSWD_LOCK));
101 }
102
103 /* Everything below this point is intended for the convenience of programs
104 * which allow a user to interactively edit the passwd file. Errors in the
105 * routines below will cause the process to abort. */
106
107 static pid_t editpid = -1;
108
109 static void
110 pw_cont(sig)
111 int sig;
112 {
113
114 if (editpid != -1)
115 kill(editpid, sig);
116 }
117
118 void
119 pw_init()
120 {
121 struct rlimit rlim;
122
123 /* Unlimited resource limits. */
124 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
125 (void)setrlimit(RLIMIT_CPU, &rlim);
126 (void)setrlimit(RLIMIT_FSIZE, &rlim);
127 (void)setrlimit(RLIMIT_STACK, &rlim);
128 (void)setrlimit(RLIMIT_DATA, &rlim);
129 (void)setrlimit(RLIMIT_RSS, &rlim);
130
131 /* Don't drop core (not really necessary, but GP's). */
132 rlim.rlim_cur = rlim.rlim_max = 0;
133 (void)setrlimit(RLIMIT_CORE, &rlim);
134
135 /* Turn off signals. */
136 (void)signal(SIGALRM, SIG_IGN);
137 (void)signal(SIGHUP, SIG_IGN);
138 (void)signal(SIGINT, SIG_IGN);
139 (void)signal(SIGPIPE, SIG_IGN);
140 (void)signal(SIGQUIT, SIG_IGN);
141 (void)signal(SIGTERM, SIG_IGN);
142 (void)signal(SIGCONT, pw_cont);
143 }
144
145 void
146 pw_edit(notsetuid, filename)
147 int notsetuid;
148 const char *filename;
149 {
150 int pstat;
151 char *p, *editor;
152
153 if (!filename)
154 filename = _PATH_MASTERPASSWD_LOCK;
155 if (!(editor = getenv("EDITOR")))
156 editor = _PATH_VI;
157 if (p = strrchr(editor, '/'))
158 ++p;
159 else
160 p = editor;
161
162 if (!(editpid = vfork())) {
163 if (notsetuid) {
164 setgid(getgid());
165 setuid(getuid());
166 }
167 execlp(editor, p, filename, NULL);
168 _exit(1);
169 }
170 for (;;) {
171 editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
172 if (editpid == -1)
173 pw_error(editor, 1, 1);
174 else if (WIFSTOPPED(pstat))
175 raise(WSTOPSIG(pstat));
176 else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
177 break;
178 else
179 pw_error(editor, 1, 1);
180 }
181 editpid = -1;
182 }
183
184 void
185 pw_prompt()
186 {
187 int c;
188
189 (void)printf("re-edit the password file? [y]: ");
190 (void)fflush(stdout);
191 c = getchar();
192 if (c != EOF && c != '\n')
193 while (getchar() != '\n');
194 if (c == 'n')
195 pw_error(NULL, 0, 0);
196 }
197
198 void
199 pw_copy(ffd, tfd, pw)
200 int ffd, tfd;
201 struct passwd *pw;
202 {
203 FILE *from, *to;
204 int done;
205 char *p, buf[8192];
206
207 if (!(from = fdopen(ffd, "r")))
208 pw_error(_PATH_MASTERPASSWD, 1, 1);
209 if (!(to = fdopen(tfd, "w")))
210 pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
211
212 for (done = 0; fgets(buf, sizeof(buf), from);) {
213 if (!strchr(buf, '\n')) {
214 warnx("%s: line too long", _PATH_MASTERPASSWD);
215 pw_error(NULL, 0, 1);
216 }
217 if (done) {
218 (void)fprintf(to, "%s", buf);
219 if (ferror(to))
220 goto err;
221 continue;
222 }
223 if (!(p = strchr(buf, ':'))) {
224 warnx("%s: corrupted entry", _PATH_MASTERPASSWD);
225 pw_error(NULL, 0, 1);
226 }
227 *p = '\0';
228 if (strcmp(buf, pw->pw_name)) {
229 *p = ':';
230 (void)fprintf(to, "%s", buf);
231 if (ferror(to))
232 goto err;
233 continue;
234 }
235 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
236 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
237 pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
238 pw->pw_dir, pw->pw_shell);
239 done = 1;
240 if (ferror(to))
241 goto err;
242 }
243 if (!done)
244 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
245 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
246 pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
247 pw->pw_dir, pw->pw_shell);
248
249 if (ferror(to))
250 err: pw_error(NULL, 1, 1);
251 (void)fclose(to);
252 }
253
254 int
255 pw_scan(bp, pw, flags)
256 char *bp;
257 struct passwd *pw;
258 int *flags;
259 {
260 long id;
261 int root;
262 char *p, *sh;
263
264 if (flags != (int *)NULL)
265 *flags = 0;
266
267 if (!(pw->pw_name = strsep(&bp, ":"))) /* login */
268 goto fmt;
269 root = !strcmp(pw->pw_name, "root");
270
271 if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
272 goto fmt;
273
274 if (!(p = strsep(&bp, ":"))) /* uid */
275 goto fmt;
276 id = atol(p);
277 if (root && id) {
278 warnx("root uid should be 0");
279 return (0);
280 }
281 if (id > USHRT_MAX) {
282 warnx("%s > max uid value (%d)", p, USHRT_MAX);
283 return (0);
284 }
285 pw->pw_uid = id;
286 if ((*p == '\0') && (flags != (int *)NULL))
287 *flags |= _PASSWORD_NOUID;
288
289 if (!(p = strsep(&bp, ":"))) /* gid */
290 goto fmt;
291 id = atol(p);
292 if (id > USHRT_MAX) {
293 warnx("%s > max gid value (%d)", p, USHRT_MAX);
294 return (0);
295 }
296 pw->pw_gid = id;
297 if ((*p == '\0') && (flags != (int *)NULL))
298 *flags |= _PASSWORD_NOGID;
299
300 pw->pw_class = strsep(&bp, ":"); /* class */
301 if (!(p = strsep(&bp, ":"))) /* change */
302 goto fmt;
303 pw->pw_change = atol(p);
304 if ((*p == '\0') && (flags != (int *)NULL))
305 *flags |= _PASSWORD_NOCHG;
306 if (!(p = strsep(&bp, ":"))) /* expire */
307 goto fmt;
308 pw->pw_expire = atol(p);
309 if ((*p == '\0') && (flags != (int *)NULL))
310 *flags |= _PASSWORD_NOEXP;
311 pw->pw_gecos = strsep(&bp, ":"); /* gecos */
312 pw->pw_dir = strsep(&bp, ":"); /* directory */
313 if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
314 goto fmt;
315
316 p = pw->pw_shell;
317 if (root && *p) /* empty == /bin/sh */
318 for (setusershell();;) {
319 if (!(sh = getusershell())) {
320 warnx("warning, unknown root shell");
321 break;
322 }
323 if (!strcmp(p, sh))
324 break;
325 }
326
327 if (p = strsep(&bp, ":")) { /* too many */
328 fmt: warnx("corrupted entry");
329 return (0);
330 }
331
332 return (1);
333 }
334
335 void
336 pw_error(name, err, eval)
337 const char *name;
338 int err, eval;
339 {
340 if (err)
341 warn(name);
342
343 warnx("%s: unchanged", _PATH_MASTERPASSWD);
344 pw_abort();
345 exit(eval);
346 }
347
348