2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/lib/libutil/pw_util.c,v 1.35 2004/05/18 15:53:58 stefanf Exp $
37 * $DragonFly: src/lib/libutil/pw_util.c,v 1.2 2007/12/30 13:44:33 matthias Exp $
41 * This file is used by all the "password" programs; vipw(8), chpass(1),
45 #include <sys/param.h>
46 #include <sys/errno.h>
48 #include <sys/resource.h>
67 static pid_t editpid
= -1;
68 static int lockfd
= -1;
69 static char masterpasswd
[PATH_MAX
];
70 static char passwd_dir
[PATH_MAX
];
71 static char tempname
[PATH_MAX
];
72 static int initialized
;
85 * Initialize statics and set limits, signals & umask to try to avoid
86 * interruptions, crashes etc. that might expose passord data.
89 pw_init(const char *dir
, const char *master
)
96 strcpy(passwd_dir
, _PATH_ETC
);
98 if (strlen(dir
) >= sizeof(passwd_dir
)) {
102 strcpy(passwd_dir
, dir
);
105 if (master
== NULL
) {
107 strcpy(masterpasswd
, _PATH_MASTERPASSWD
);
108 } else if (snprintf(masterpasswd
, sizeof(masterpasswd
), "%s/%s",
109 passwd_dir
, _MASTERPASSWD
) > (int)sizeof(masterpasswd
)) {
110 errno
= ENAMETOOLONG
;
114 if (strlen(master
) >= sizeof(masterpasswd
)) {
115 errno
= ENAMETOOLONG
;
118 strcpy(masterpasswd
, master
);
122 * The code that follows is extremely disruptive to the calling
123 * process, and is therefore disabled until someone can conceive
124 * of a realistic scenario where it would fend off a compromise.
125 * Race conditions concerning the temporary files can be guarded
126 * against in other ways than masking signals (by checking stat(2)
127 * results after creation).
130 /* Unlimited resource limits. */
131 rlim
.rlim_cur
= rlim
.rlim_max
= RLIM_INFINITY
;
132 (void)setrlimit(RLIMIT_CPU
, &rlim
);
133 (void)setrlimit(RLIMIT_FSIZE
, &rlim
);
134 (void)setrlimit(RLIMIT_STACK
, &rlim
);
135 (void)setrlimit(RLIMIT_DATA
, &rlim
);
136 (void)setrlimit(RLIMIT_RSS
, &rlim
);
138 /* Don't drop core (not really necessary, but GP's). */
139 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
140 (void)setrlimit(RLIMIT_CORE
, &rlim
);
142 /* Turn off signals. */
143 (void)signal(SIGALRM
, SIG_IGN
);
144 (void)signal(SIGHUP
, SIG_IGN
);
145 (void)signal(SIGINT
, SIG_IGN
);
146 (void)signal(SIGPIPE
, SIG_IGN
);
147 (void)signal(SIGQUIT
, SIG_IGN
);
148 (void)signal(SIGTERM
, SIG_IGN
);
149 (void)signal(SIGCONT
, pw_cont
);
151 /* Create with exact permissions. */
159 * Lock the master password file.
165 if (*masterpasswd
== '\0')
169 * If the master password file doesn't exist, the system is hosed.
170 * Might as well try to build one. Set the close-on-exec bit so
171 * that users can't get at the encrypted passwords while editing.
172 * Open should allow flock'ing the file; see 4.4BSD. XXX
177 lockfd
= open(masterpasswd
, O_RDONLY
, 0);
178 if (lockfd
< 0 || fcntl(lockfd
, F_SETFD
, 1) == -1)
179 err(1, "%s", masterpasswd
);
180 /* XXX vulnerable to race conditions */
181 if (flock(lockfd
, LOCK_EX
|LOCK_NB
) == -1) {
182 if (errno
== EWOULDBLOCK
) {
183 errx(1, "the password db file is busy");
185 err(1, "could not lock the passwd file: ");
190 * If the password file was replaced while we were trying to
191 * get the lock, our hardlink count will be 0 and we have to
194 if (fstat(lockfd
, &st
) == -1)
195 err(1, "fstat() failed: ");
196 if (st
.st_nlink
!= 0)
205 * Create and open a presumably safe temp file for editing the password
206 * data, and copy the master password file into it.
216 if (*masterpasswd
== '\0')
218 if ((p
= strrchr(masterpasswd
, '/')))
222 if (snprintf(tempname
, sizeof(tempname
), "%.*spw.XXXXXX",
223 (int)(p
- masterpasswd
), masterpasswd
) >= (int)sizeof(tempname
)) {
224 errno
= ENAMETOOLONG
;
227 if ((tfd
= mkstemp(tempname
)) == -1)
230 while ((nr
= read(mfd
, buf
, sizeof(buf
))) > 0)
231 if (write(tfd
, buf
, (size_t)nr
) != nr
)
244 * Regenerate the password database.
247 pw_mkdb(const char *user
)
252 (void)fflush(stderr
);
253 switch ((pid
= fork())) {
259 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
260 "-d", passwd_dir
, tempname
, (char *)NULL
);
262 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
263 "-d", passwd_dir
, "-u", user
, tempname
,
271 if (waitpid(pid
, &pstat
, 0) == -1)
273 if (WIFEXITED(pstat
) && WEXITSTATUS(pstat
) == 0)
280 * Edit the temp file. Return -1 on error, >0 if the file was modified, 0
284 pw_edit(int notsetuid
)
286 struct sigaction sa
, sa_int
, sa_quit
;
287 sigset_t oldsigset
, sigset
;
288 struct stat st1
, st2
;
292 if ((editor
= getenv("EDITOR")) == NULL
)
294 if (stat(tempname
, &st1
) == -1)
296 sa
.sa_handler
= SIG_IGN
;
297 sigemptyset(&sa
.sa_mask
);
299 sigaction(SIGINT
, &sa
, &sa_int
);
300 sigaction(SIGQUIT
, &sa
, &sa_quit
);
301 sigemptyset(&sigset
);
302 sigaddset(&sigset
, SIGCHLD
);
303 sigprocmask(SIG_BLOCK
, &sigset
, &oldsigset
);
304 switch ((editpid
= fork())) {
308 sigaction(SIGINT
, &sa_int
, NULL
);
309 sigaction(SIGQUIT
, &sa_quit
, NULL
);
310 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
312 (void)setgid(getgid());
313 (void)setuid(getuid());
316 execlp(editor
, basename(editor
), tempname
, (char *)NULL
);
323 if (waitpid(editpid
, &pstat
, WUNTRACED
) == -1) {
329 } else if (WIFSTOPPED(pstat
)) {
330 raise(WSTOPSIG(pstat
));
331 } else if (WIFEXITED(pstat
) && WEXITSTATUS(pstat
) == 0) {
340 sigaction(SIGINT
, &sa_int
, NULL
);
341 sigaction(SIGQUIT
, &sa_quit
, NULL
);
342 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
343 if (stat(tempname
, &st2
) == -1)
345 return (st1
.st_mtime
!= st2
.st_mtime
);
349 * Clean up. Preserve errno for the caller's convenience.
361 kill(editpid
, SIGTERM
);
362 kill(editpid
, SIGCONT
);
363 waitpid(editpid
, &status
, 0);
366 if (*tempname
!= '\0') {
376 * Compares two struct pwds.
379 pw_equal(const struct passwd
*pw1
, const struct passwd
*pw2
)
381 return (strcmp(pw1
->pw_name
, pw2
->pw_name
) == 0 &&
382 pw1
->pw_uid
== pw2
->pw_uid
&&
383 pw1
->pw_gid
== pw2
->pw_gid
&&
384 strcmp(pw1
->pw_class
, pw2
->pw_class
) == 0 &&
385 pw1
->pw_change
== pw2
->pw_change
&&
386 pw1
->pw_expire
== pw2
->pw_expire
&&
387 strcmp(pw1
->pw_gecos
, pw2
->pw_gecos
) == 0 &&
388 strcmp(pw1
->pw_dir
, pw2
->pw_dir
) == 0 &&
389 strcmp(pw1
->pw_shell
, pw2
->pw_shell
) == 0);
393 * Make a passwd line out of a struct passwd.
396 pw_make(const struct passwd
*pw
)
400 asprintf(&line
, "%s:%s:%ju:%ju:%s:%ju:%ju:%s:%s:%s", pw
->pw_name
,
401 pw
->pw_passwd
, (uintmax_t)pw
->pw_uid
, (uintmax_t)pw
->pw_gid
,
402 pw
->pw_class
, (uintmax_t)pw
->pw_change
, (uintmax_t)pw
->pw_expire
,
403 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
408 * Copy password file from one descriptor to another, replacing or adding
409 * a single record on the way.
412 pw_copy(int ffd
, int tfd
, const struct passwd
*pw
, struct passwd
*old_pw
)
414 char buf
[8192], *end
, *line
, *p
, *q
, *r
, t
;
419 if ((line
= pw_make(pw
)) == NULL
)
426 /* find the end of the current line */
427 for (p
= q
; q
< end
&& *q
!= '\0'; ++q
)
431 /* if we don't have a complete line, fill up the buffer */
435 if ((size_t)(q
- p
) >= sizeof(buf
)) {
436 warnx("passwd line too long");
437 errno
= EINVAL
; /* hack */
441 q
= memmove(buf
, p
, end
- p
);
446 readlen
= read(ffd
, end
, sizeof(buf
) - (end
- buf
));
450 len
= (size_t)readlen
;
451 if (len
== 0 && p
== buf
)
455 if (len
< (ssize_t
)sizeof(buf
)) {
457 if (len
> 0 && buf
[len
- 1] != '\n')
458 ++len
, *end
++ = '\n';
463 /* is it a blank line or a comment? */
464 for (r
= p
; r
< q
&& isspace(*r
); ++r
)
466 if (r
== q
|| *r
== '#') {
468 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
474 /* is it the one we're looking for? */
477 fpw
= pw_scan(r
, _PWSCAN_MASTER
);
479 if (strcmp(fpw
->pw_name
, pw
->pw_name
) != 0) {
482 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
487 if (old_pw
&& !pw_equal(fpw
, old_pw
)) {
488 warnx("entry inconsistent");
490 errno
= EINVAL
; /* hack */
495 /* it is, replace it */
497 if (write(tfd
, line
, len
) != (int)len
)
500 /* we're done, just copy the rest over */
502 if (write(tfd
, q
, end
- q
) != end
- q
)
505 readlen
= read(ffd
, buf
, sizeof(buf
));
509 len
= (size_t)readlen
;
517 /* if we got here, we have a new entry */
519 if ((size_t)write(tfd
, line
, len
) != len
||
520 write(tfd
, "\n", 1) != 1)
531 * Return the current value of tempname.
541 * Duplicate a struct passwd.
544 pw_dup(const struct passwd
*pw
)
550 (pw
->pw_name
? strlen(pw
->pw_name
) + 1 : 0) +
551 (pw
->pw_passwd
? strlen(pw
->pw_passwd
) + 1 : 0) +
552 (pw
->pw_class
? strlen(pw
->pw_class
) + 1 : 0) +
553 (pw
->pw_gecos
? strlen(pw
->pw_gecos
) + 1 : 0) +
554 (pw
->pw_dir
? strlen(pw
->pw_dir
) + 1 : 0) +
555 (pw
->pw_shell
? strlen(pw
->pw_shell
) + 1 : 0);
556 if ((npw
= malloc((size_t)len
)) == NULL
)
558 memcpy(npw
, pw
, sizeof(*npw
));
561 npw
->pw_name
= ((char *)npw
) + len
;
562 len
+= sprintf(npw
->pw_name
, "%s", pw
->pw_name
) + 1;
565 npw
->pw_passwd
= ((char *)npw
) + len
;
566 len
+= sprintf(npw
->pw_passwd
, "%s", pw
->pw_passwd
) + 1;
569 npw
->pw_class
= ((char *)npw
) + len
;
570 len
+= sprintf(npw
->pw_class
, "%s", pw
->pw_class
) + 1;
573 npw
->pw_gecos
= ((char *)npw
) + len
;
574 len
+= sprintf(npw
->pw_gecos
, "%s", pw
->pw_gecos
) + 1;
577 npw
->pw_dir
= ((char *)npw
) + len
;
578 len
+= sprintf(npw
->pw_dir
, "%s", pw
->pw_dir
) + 1;
581 npw
->pw_shell
= ((char *)npw
) + len
;
582 len
+= sprintf(npw
->pw_shell
, "%s", pw
->pw_shell
) + 1;
588 * Wrapper around an internal libc function
591 pw_scan(const char *line
, int flags
)
593 struct passwd pw
, *ret
;
596 if ((bp
= strdup(line
)) == NULL
)
598 if (!__pw_scan(bp
, &pw
, flags
)) {