passwd.c revision 1.11 1 1.11 thorpej /* $NetBSD: passwd.c,v 1.11 1997/12/31 05:47:15 thorpej Exp $ */
2 1.8 christos
3 1.1 jtc /*
4 1.1 jtc * Copyright (c) 1987, 1993, 1994, 1995
5 1.1 jtc * The Regents of the University of California. All rights reserved.
6 1.1 jtc *
7 1.1 jtc * Redistribution and use in source and binary forms, with or without
8 1.1 jtc * modification, are permitted provided that the following conditions
9 1.1 jtc * are met:
10 1.1 jtc * 1. Redistributions of source code must retain the above copyright
11 1.1 jtc * notice, this list of conditions and the following disclaimer.
12 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer in the
14 1.1 jtc * documentation and/or other materials provided with the distribution.
15 1.1 jtc * 3. All advertising materials mentioning features or use of this software
16 1.1 jtc * must display the following acknowledgement:
17 1.1 jtc * This product includes software developed by the University of
18 1.1 jtc * California, Berkeley and its contributors.
19 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
20 1.1 jtc * may be used to endorse or promote products derived from this software
21 1.1 jtc * without specific prior written permission.
22 1.1 jtc *
23 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 jtc * SUCH DAMAGE.
34 1.1 jtc */
35 1.1 jtc
36 1.8 christos #include <sys/cdefs.h>
37 1.1 jtc #if defined(LIBC_SCCS) && !defined(lint)
38 1.11 thorpej __RCSID("$NetBSD: passwd.c,v 1.11 1997/12/31 05:47:15 thorpej Exp $");
39 1.1 jtc #endif /* LIBC_SCCS and not lint */
40 1.1 jtc
41 1.1 jtc #include <sys/types.h>
42 1.1 jtc #include <sys/stat.h>
43 1.1 jtc #include <sys/time.h>
44 1.1 jtc #include <sys/resource.h>
45 1.1 jtc #include <sys/wait.h>
46 1.1 jtc
47 1.3 thorpej #include <ctype.h>
48 1.5 mycroft #include <err.h>
49 1.1 jtc #include <fcntl.h>
50 1.1 jtc #include <unistd.h>
51 1.1 jtc #include <stdlib.h>
52 1.1 jtc #include <stdio.h>
53 1.1 jtc #include <string.h>
54 1.1 jtc #include <pwd.h>
55 1.1 jtc #include <errno.h>
56 1.1 jtc #include <paths.h>
57 1.1 jtc #include <signal.h>
58 1.1 jtc #include <limits.h>
59 1.1 jtc #include <util.h>
60 1.1 jtc
61 1.1 jtc static void pw_cont __P((int sig));
62 1.10 phil static int pw_equal __P((char *buf, struct passwd *old_pw));
63 1.1 jtc
64 1.1 jtc int
65 1.1 jtc pw_lock(retries)
66 1.1 jtc int retries;
67 1.1 jtc {
68 1.1 jtc int i, fd;
69 1.1 jtc mode_t old_mode;
70 1.1 jtc
71 1.1 jtc /* Acquire the lock file. */
72 1.1 jtc old_mode = umask(0);
73 1.1 jtc fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL, 0600);
74 1.1 jtc for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
75 1.1 jtc sleep(1);
76 1.1 jtc fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL,
77 1.1 jtc 0600);
78 1.1 jtc }
79 1.1 jtc umask(old_mode);
80 1.1 jtc return(fd);
81 1.1 jtc }
82 1.1 jtc
83 1.1 jtc int
84 1.1 jtc pw_mkdb()
85 1.1 jtc {
86 1.1 jtc int pstat;
87 1.1 jtc pid_t pid;
88 1.1 jtc
89 1.1 jtc pid = vfork();
90 1.1 jtc if (pid == 0) {
91 1.1 jtc execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p",
92 1.1 jtc _PATH_MASTERPASSWD_LOCK, NULL);
93 1.11 thorpej _exit(1);
94 1.1 jtc }
95 1.1 jtc pid = waitpid(pid, &pstat, 0);
96 1.2 ghudson if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
97 1.1 jtc return(-1);
98 1.1 jtc return(0);
99 1.1 jtc }
100 1.1 jtc
101 1.1 jtc int
102 1.1 jtc pw_abort()
103 1.1 jtc {
104 1.1 jtc return(unlink(_PATH_MASTERPASSWD_LOCK));
105 1.1 jtc }
106 1.1 jtc
107 1.1 jtc /* Everything below this point is intended for the convenience of programs
108 1.1 jtc * which allow a user to interactively edit the passwd file. Errors in the
109 1.1 jtc * routines below will cause the process to abort. */
110 1.1 jtc
111 1.1 jtc static pid_t editpid = -1;
112 1.1 jtc
113 1.1 jtc static void
114 1.1 jtc pw_cont(sig)
115 1.1 jtc int sig;
116 1.1 jtc {
117 1.1 jtc
118 1.1 jtc if (editpid != -1)
119 1.1 jtc kill(editpid, sig);
120 1.1 jtc }
121 1.1 jtc
122 1.1 jtc void
123 1.1 jtc pw_init()
124 1.1 jtc {
125 1.1 jtc struct rlimit rlim;
126 1.1 jtc
127 1.1 jtc /* Unlimited resource limits. */
128 1.1 jtc rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
129 1.1 jtc (void)setrlimit(RLIMIT_CPU, &rlim);
130 1.1 jtc (void)setrlimit(RLIMIT_FSIZE, &rlim);
131 1.1 jtc (void)setrlimit(RLIMIT_STACK, &rlim);
132 1.1 jtc (void)setrlimit(RLIMIT_DATA, &rlim);
133 1.1 jtc (void)setrlimit(RLIMIT_RSS, &rlim);
134 1.1 jtc
135 1.1 jtc /* Don't drop core (not really necessary, but GP's). */
136 1.1 jtc rlim.rlim_cur = rlim.rlim_max = 0;
137 1.1 jtc (void)setrlimit(RLIMIT_CORE, &rlim);
138 1.1 jtc
139 1.1 jtc /* Turn off signals. */
140 1.1 jtc (void)signal(SIGALRM, SIG_IGN);
141 1.1 jtc (void)signal(SIGHUP, SIG_IGN);
142 1.1 jtc (void)signal(SIGINT, SIG_IGN);
143 1.1 jtc (void)signal(SIGPIPE, SIG_IGN);
144 1.1 jtc (void)signal(SIGQUIT, SIG_IGN);
145 1.1 jtc (void)signal(SIGTERM, SIG_IGN);
146 1.1 jtc (void)signal(SIGCONT, pw_cont);
147 1.1 jtc }
148 1.1 jtc
149 1.1 jtc void
150 1.1 jtc pw_edit(notsetuid, filename)
151 1.1 jtc int notsetuid;
152 1.1 jtc const char *filename;
153 1.1 jtc {
154 1.8 christos int i, xargc, pstat;
155 1.1 jtc char *p, *editor;
156 1.3 thorpej char **xargv;
157 1.8 christos #ifdef __GNUC__
158 1.8 christos (void) &editor;
159 1.8 christos #endif
160 1.1 jtc
161 1.3 thorpej if (filename == NULL)
162 1.1 jtc filename = _PATH_MASTERPASSWD_LOCK;
163 1.3 thorpej if ((editor = getenv("EDITOR")) == NULL)
164 1.3 thorpej editor = strdup(_PATH_VI);
165 1.3 thorpej else
166 1.3 thorpej editor = strdup(editor);
167 1.7 mikel if ((p = strrchr(editor, '/')))
168 1.1 jtc ++p;
169 1.3 thorpej else
170 1.1 jtc p = editor;
171 1.1 jtc
172 1.3 thorpej /* Scan editor string, count spaces, allocate arg vector. */
173 1.3 thorpej for (i = 0, xargc = 0; p[i] != '\0'; i++) {
174 1.3 thorpej if (isspace(p[i])) {
175 1.3 thorpej while (isspace(p[i++]))
176 1.3 thorpej /* skip white space */ ;
177 1.3 thorpej if (p[i] == '\0')
178 1.3 thorpej break;
179 1.3 thorpej xargc++;
180 1.3 thorpej }
181 1.3 thorpej }
182 1.3 thorpej
183 1.3 thorpej /* argv[0] + <xargc args> + filename + NULL */
184 1.3 thorpej xargv = (char **)malloc(sizeof(char *) * (xargc + 3));
185 1.3 thorpej if (xargv == NULL)
186 1.3 thorpej pw_error("malloc failed", 1, 1);
187 1.3 thorpej
188 1.3 thorpej i = 0;
189 1.3 thorpej xargv[i++] = p;
190 1.3 thorpej for (; *p != '\0'; p++) {
191 1.3 thorpej if (isspace(*p)) {
192 1.3 thorpej while(isspace(*p))
193 1.3 thorpej *p++ = '\0'; /* blast whitespace */
194 1.3 thorpej if (*p == '\0')
195 1.3 thorpej break;
196 1.3 thorpej xargv[i++] = p;
197 1.3 thorpej }
198 1.3 thorpej }
199 1.3 thorpej
200 1.3 thorpej xargv[i++] = (char *)filename;
201 1.3 thorpej xargv[i] = NULL;
202 1.3 thorpej
203 1.1 jtc if (!(editpid = vfork())) {
204 1.1 jtc if (notsetuid) {
205 1.1 jtc setgid(getgid());
206 1.1 jtc setuid(getuid());
207 1.1 jtc }
208 1.3 thorpej execvp(editor, xargv);
209 1.1 jtc _exit(1);
210 1.1 jtc }
211 1.1 jtc for (;;) {
212 1.1 jtc editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
213 1.1 jtc if (editpid == -1)
214 1.1 jtc pw_error(editor, 1, 1);
215 1.1 jtc else if (WIFSTOPPED(pstat))
216 1.1 jtc raise(WSTOPSIG(pstat));
217 1.1 jtc else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
218 1.1 jtc break;
219 1.1 jtc else
220 1.1 jtc pw_error(editor, 1, 1);
221 1.1 jtc }
222 1.1 jtc editpid = -1;
223 1.3 thorpej free(editor);
224 1.3 thorpej free(xargv);
225 1.1 jtc }
226 1.1 jtc
227 1.1 jtc void
228 1.1 jtc pw_prompt()
229 1.1 jtc {
230 1.1 jtc int c;
231 1.1 jtc
232 1.1 jtc (void)printf("re-edit the password file? [y]: ");
233 1.1 jtc (void)fflush(stdout);
234 1.1 jtc c = getchar();
235 1.1 jtc if (c != EOF && c != '\n')
236 1.1 jtc while (getchar() != '\n');
237 1.1 jtc if (c == 'n')
238 1.1 jtc pw_error(NULL, 0, 0);
239 1.1 jtc }
240 1.1 jtc
241 1.10 phil /* for use in pw_copy(). Compare a pw entry to a pw struct. */
242 1.10 phil static int
243 1.10 phil pw_equal (buf, pw)
244 1.10 phil char *buf;
245 1.10 phil struct passwd *pw;
246 1.10 phil {
247 1.10 phil struct passwd buf_pw;
248 1.10 phil int len = strlen (buf);
249 1.10 phil if (buf[len-1] == '\n')
250 1.10 phil buf[len-1] = '\0';
251 1.10 phil if (!pw_scan(buf, &buf_pw, NULL))
252 1.10 phil return 0;
253 1.10 phil return !strcmp(pw->pw_name, buf_pw.pw_name)
254 1.10 phil && pw->pw_uid == buf_pw.pw_uid
255 1.10 phil && pw->pw_gid == buf_pw.pw_gid
256 1.10 phil && !strcmp(pw->pw_class, buf_pw.pw_class)
257 1.10 phil && (long)pw->pw_change == (long)buf_pw.pw_change
258 1.10 phil && (long)pw->pw_expire == (long)buf_pw.pw_expire
259 1.10 phil && !strcmp(pw->pw_gecos, buf_pw.pw_gecos)
260 1.10 phil && !strcmp(pw->pw_dir, buf_pw.pw_dir)
261 1.10 phil && !strcmp(pw->pw_shell, buf_pw.pw_shell);
262 1.10 phil }
263 1.10 phil
264 1.1 jtc void
265 1.10 phil pw_copy(ffd, tfd, pw, old_pw)
266 1.1 jtc int ffd, tfd;
267 1.10 phil struct passwd *pw, *old_pw;
268 1.1 jtc {
269 1.1 jtc FILE *from, *to;
270 1.1 jtc int done;
271 1.1 jtc char *p, buf[8192];
272 1.1 jtc
273 1.1 jtc if (!(from = fdopen(ffd, "r")))
274 1.1 jtc pw_error(_PATH_MASTERPASSWD, 1, 1);
275 1.1 jtc if (!(to = fdopen(tfd, "w")))
276 1.1 jtc pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
277 1.1 jtc
278 1.1 jtc for (done = 0; fgets(buf, sizeof(buf), from);) {
279 1.1 jtc if (!strchr(buf, '\n')) {
280 1.1 jtc warnx("%s: line too long", _PATH_MASTERPASSWD);
281 1.1 jtc pw_error(NULL, 0, 1);
282 1.1 jtc }
283 1.1 jtc if (done) {
284 1.1 jtc (void)fprintf(to, "%s", buf);
285 1.1 jtc if (ferror(to))
286 1.1 jtc goto err;
287 1.1 jtc continue;
288 1.1 jtc }
289 1.1 jtc if (!(p = strchr(buf, ':'))) {
290 1.1 jtc warnx("%s: corrupted entry", _PATH_MASTERPASSWD);
291 1.1 jtc pw_error(NULL, 0, 1);
292 1.1 jtc }
293 1.1 jtc *p = '\0';
294 1.1 jtc if (strcmp(buf, pw->pw_name)) {
295 1.1 jtc *p = ':';
296 1.1 jtc (void)fprintf(to, "%s", buf);
297 1.1 jtc if (ferror(to))
298 1.1 jtc goto err;
299 1.1 jtc continue;
300 1.1 jtc }
301 1.10 phil *p = ':';
302 1.10 phil if (old_pw && !pw_equal(buf, old_pw)) {
303 1.10 phil warnx("%s: entry inconsistent",
304 1.10 phil _PATH_MASTERPASSWD);
305 1.10 phil pw_error(NULL, 0, 1);
306 1.10 phil }
307 1.1 jtc (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
308 1.1 jtc pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
309 1.9 christos pw->pw_class, (long)pw->pw_change, (long)pw->pw_expire,
310 1.9 christos pw->pw_gecos, pw->pw_dir, pw->pw_shell);
311 1.1 jtc done = 1;
312 1.1 jtc if (ferror(to))
313 1.1 jtc goto err;
314 1.1 jtc }
315 1.10 phil /* Only append a new entry if real uid is root! */
316 1.10 phil if (!done)
317 1.10 phil if (getuid() == 0)
318 1.10 phil (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
319 1.10 phil pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
320 1.10 phil pw->pw_class, (long)pw->pw_change,
321 1.10 phil (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
322 1.10 phil pw->pw_shell);
323 1.10 phil else
324 1.10 phil warnx("%s: changes not made, no such entry",
325 1.10 phil _PATH_MASTERPASSWD);
326 1.1 jtc
327 1.1 jtc if (ferror(to))
328 1.1 jtc err: pw_error(NULL, 1, 1);
329 1.1 jtc (void)fclose(to);
330 1.1 jtc }
331 1.1 jtc
332 1.1 jtc int
333 1.1 jtc pw_scan(bp, pw, flags)
334 1.1 jtc char *bp;
335 1.1 jtc struct passwd *pw;
336 1.1 jtc int *flags;
337 1.1 jtc {
338 1.6 lukem unsigned long id;
339 1.1 jtc int root;
340 1.6 lukem char *p, *sh, *ep;
341 1.1 jtc
342 1.1 jtc if (flags != (int *)NULL)
343 1.1 jtc *flags = 0;
344 1.1 jtc
345 1.1 jtc if (!(pw->pw_name = strsep(&bp, ":"))) /* login */
346 1.1 jtc goto fmt;
347 1.1 jtc root = !strcmp(pw->pw_name, "root");
348 1.1 jtc
349 1.1 jtc if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
350 1.1 jtc goto fmt;
351 1.1 jtc
352 1.1 jtc if (!(p = strsep(&bp, ":"))) /* uid */
353 1.1 jtc goto fmt;
354 1.6 lukem id = strtoul(p, &ep, 10);
355 1.1 jtc if (root && id) {
356 1.1 jtc warnx("root uid should be 0");
357 1.1 jtc return (0);
358 1.1 jtc }
359 1.6 lukem if (id > UID_MAX || *ep != '\0') {
360 1.6 lukem warnx("invalid uid '%s'", p);
361 1.6 lukem return (0);
362 1.6 lukem }
363 1.6 lukem pw->pw_uid = (uid_t)id;
364 1.1 jtc if ((*p == '\0') && (flags != (int *)NULL))
365 1.1 jtc *flags |= _PASSWORD_NOUID;
366 1.1 jtc
367 1.1 jtc if (!(p = strsep(&bp, ":"))) /* gid */
368 1.1 jtc goto fmt;
369 1.6 lukem id = strtoul(p, &ep, 10);
370 1.6 lukem if (id > GID_MAX || *ep != '\0') {
371 1.6 lukem warnx("invalid gid '%s'", p);
372 1.6 lukem return (0);
373 1.6 lukem }
374 1.6 lukem pw->pw_gid = (gid_t)id;
375 1.1 jtc if ((*p == '\0') && (flags != (int *)NULL))
376 1.1 jtc *flags |= _PASSWORD_NOGID;
377 1.1 jtc
378 1.1 jtc pw->pw_class = strsep(&bp, ":"); /* class */
379 1.1 jtc if (!(p = strsep(&bp, ":"))) /* change */
380 1.1 jtc goto fmt;
381 1.1 jtc pw->pw_change = atol(p);
382 1.1 jtc if ((*p == '\0') && (flags != (int *)NULL))
383 1.1 jtc *flags |= _PASSWORD_NOCHG;
384 1.1 jtc if (!(p = strsep(&bp, ":"))) /* expire */
385 1.1 jtc goto fmt;
386 1.1 jtc pw->pw_expire = atol(p);
387 1.1 jtc if ((*p == '\0') && (flags != (int *)NULL))
388 1.1 jtc *flags |= _PASSWORD_NOEXP;
389 1.1 jtc pw->pw_gecos = strsep(&bp, ":"); /* gecos */
390 1.1 jtc pw->pw_dir = strsep(&bp, ":"); /* directory */
391 1.1 jtc if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
392 1.1 jtc goto fmt;
393 1.1 jtc
394 1.1 jtc p = pw->pw_shell;
395 1.1 jtc if (root && *p) /* empty == /bin/sh */
396 1.1 jtc for (setusershell();;) {
397 1.1 jtc if (!(sh = getusershell())) {
398 1.1 jtc warnx("warning, unknown root shell");
399 1.1 jtc break;
400 1.1 jtc }
401 1.1 jtc if (!strcmp(p, sh))
402 1.1 jtc break;
403 1.1 jtc }
404 1.1 jtc
405 1.7 mikel if ((p = strsep(&bp, ":"))) { /* too many */
406 1.1 jtc fmt: warnx("corrupted entry");
407 1.1 jtc return (0);
408 1.1 jtc }
409 1.1 jtc
410 1.1 jtc return (1);
411 1.1 jtc }
412 1.1 jtc
413 1.1 jtc void
414 1.1 jtc pw_error(name, err, eval)
415 1.1 jtc const char *name;
416 1.1 jtc int err, eval;
417 1.1 jtc {
418 1.1 jtc if (err)
419 1.1 jtc warn(name);
420 1.1 jtc
421 1.1 jtc warnx("%s: unchanged", _PATH_MASTERPASSWD);
422 1.1 jtc pw_abort();
423 1.1 jtc exit(eval);
424 1.1 jtc }
425 1.1 jtc
426