]>
git.cameronkatri.com Git - pw-darwin.git/blob - libutil/pw_util.c
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
39 static const char sccsid
[] = "@(#)pw_util.c 8.3 (Berkeley) 4/2/94";
41 static const char rcsid
[] =
46 * This file is used by all the "password" programs; vipw(8), chpass(1),
50 #include <sys/param.h>
51 #include <sys/errno.h>
53 #include <sys/resource.h>
72 static pid_t editpid
= -1;
73 static int lockfd
= -1;
74 static char masterpasswd
[PATH_MAX
];
75 static char passwd_dir
[PATH_MAX
];
76 static char tempname
[PATH_MAX
];
77 static int initialized
;
90 * Initialize statics and set limits, signals & umask to try to avoid
91 * interruptions, crashes etc. that might expose passord data.
94 pw_init(const char *dir
, const char *master
)
101 strcpy(passwd_dir
, _PATH_ETC
);
103 if (strlen(dir
) >= sizeof(passwd_dir
)) {
104 errno
= ENAMETOOLONG
;
107 strcpy(passwd_dir
, dir
);
110 if (master
== NULL
) {
112 strcpy(masterpasswd
, _PATH_MASTERPASSWD
);
113 } else if (snprintf(masterpasswd
, sizeof(masterpasswd
), "%s/%s",
114 passwd_dir
, _MASTERPASSWD
) > (int)sizeof(masterpasswd
)) {
115 errno
= ENAMETOOLONG
;
119 if (strlen(master
) >= sizeof(masterpasswd
)) {
120 errno
= ENAMETOOLONG
;
123 strcpy(masterpasswd
, master
);
127 * The code that follows is extremely disruptive to the calling
128 * process, and is therefore disabled until someone can conceive
129 * of a realistic scenario where it would fend off a compromise.
130 * Race conditions concerning the temporary files can be guarded
131 * against in other ways than masking signals (by checking stat(2)
132 * results after creation).
135 /* Unlimited resource limits. */
136 rlim
.rlim_cur
= rlim
.rlim_max
= RLIM_INFINITY
;
137 (void)setrlimit(RLIMIT_CPU
, &rlim
);
138 (void)setrlimit(RLIMIT_FSIZE
, &rlim
);
139 (void)setrlimit(RLIMIT_STACK
, &rlim
);
140 (void)setrlimit(RLIMIT_DATA
, &rlim
);
141 (void)setrlimit(RLIMIT_RSS
, &rlim
);
143 /* Don't drop core (not really necessary, but GP's). */
144 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
145 (void)setrlimit(RLIMIT_CORE
, &rlim
);
147 /* Turn off signals. */
148 (void)signal(SIGALRM
, SIG_IGN
);
149 (void)signal(SIGHUP
, SIG_IGN
);
150 (void)signal(SIGINT
, SIG_IGN
);
151 (void)signal(SIGPIPE
, SIG_IGN
);
152 (void)signal(SIGQUIT
, SIG_IGN
);
153 (void)signal(SIGTERM
, SIG_IGN
);
154 (void)signal(SIGCONT
, pw_cont
);
156 /* Create with exact permissions. */
164 * Lock the master password file.
170 if (*masterpasswd
== '\0')
174 * If the master password file doesn't exist, the system is hosed.
175 * Might as well try to build one. Set the close-on-exec bit so
176 * that users can't get at the encrypted passwords while editing.
177 * Open should allow flock'ing the file; see 4.4BSD. XXX
182 lockfd
= open(masterpasswd
, O_RDONLY
, 0);
183 if (lockfd
< 0 || fcntl(lockfd
, F_SETFD
, 1) == -1)
184 err(1, "%s", masterpasswd
);
185 /* XXX vulnerable to race conditions */
186 if (flock(lockfd
, LOCK_EX
|LOCK_NB
) == -1) {
187 if (errno
== EWOULDBLOCK
) {
188 errx(1, "the password db file is busy");
190 err(1, "could not lock the passwd file: ");
195 * If the password file was replaced while we were trying to
196 * get the lock, our hardlink count will be 0 and we have to
199 if (fstat(lockfd
, &st
) == -1)
200 err(1, "fstat() failed: ");
201 if (st
.st_nlink
!= 0)
210 * Create and open a presumably safe temp file for editing the password
211 * data, and copy the master password file into it.
221 if (*masterpasswd
== '\0')
223 if ((p
= strrchr(masterpasswd
, '/')))
227 if (snprintf(tempname
, sizeof(tempname
), "%.*spw.XXXXXX",
228 (int)(p
- masterpasswd
), masterpasswd
) >= (int)sizeof(tempname
)) {
229 errno
= ENAMETOOLONG
;
232 if ((tfd
= mkstemp(tempname
)) == -1)
235 while ((nr
= read(mfd
, buf
, sizeof(buf
))) > 0)
236 if (write(tfd
, buf
, (size_t)nr
) != nr
)
249 * Regenerate the password database.
252 pw_mkdb(const char *user
)
257 (void)fflush(stderr
);
258 switch ((pid
= fork())) {
264 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
265 "-d", passwd_dir
, tempname
, (char *)NULL
);
267 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
268 "-d", passwd_dir
, "-u", user
, tempname
,
276 if (waitpid(pid
, &pstat
, 0) == -1)
278 if (WIFEXITED(pstat
) && WEXITSTATUS(pstat
) == 0)
285 * Edit the temp file. Return -1 on error, >0 if the file was modified, 0
289 pw_edit(int notsetuid
)
291 struct sigaction sa
, sa_int
, sa_quit
;
292 sigset_t oldsigset
, nsigset
;
293 struct stat st1
, st2
;
297 if ((editor
= getenv("EDITOR")) == NULL
)
299 if (stat(tempname
, &st1
) == -1)
301 sa
.sa_handler
= SIG_IGN
;
302 sigemptyset(&sa
.sa_mask
);
304 sigaction(SIGINT
, &sa
, &sa_int
);
305 sigaction(SIGQUIT
, &sa
, &sa_quit
);
306 sigemptyset(&nsigset
);
307 sigaddset(&nsigset
, SIGCHLD
);
308 sigprocmask(SIG_BLOCK
, &nsigset
, &oldsigset
);
309 switch ((editpid
= fork())) {
313 sigaction(SIGINT
, &sa_int
, NULL
);
314 sigaction(SIGQUIT
, &sa_quit
, NULL
);
315 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
317 (void)setgid(getgid());
318 (void)setuid(getuid());
321 execlp(editor
, basename(editor
), tempname
, (char *)NULL
);
328 if (waitpid(editpid
, &pstat
, WUNTRACED
) == -1) {
334 } else if (WIFSTOPPED(pstat
)) {
335 raise(WSTOPSIG(pstat
));
336 } else if (WIFEXITED(pstat
) && WEXITSTATUS(pstat
) == 0) {
345 sigaction(SIGINT
, &sa_int
, NULL
);
346 sigaction(SIGQUIT
, &sa_quit
, NULL
);
347 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
348 if (stat(tempname
, &st2
) == -1)
350 return (st1
.st_mtim
.tv_sec
!= st2
.st_mtim
.tv_sec
||
351 st1
.st_mtim
.tv_nsec
!= st2
.st_mtim
.tv_nsec
);
355 * Clean up. Preserve errno for the caller's convenience.
367 kill(editpid
, SIGTERM
);
368 kill(editpid
, SIGCONT
);
369 waitpid(editpid
, &status
, 0);
372 if (*tempname
!= '\0') {
382 * Compares two struct pwds.
385 pw_equal(const struct passwd
*pw1
, const struct passwd
*pw2
)
387 return (strcmp(pw1
->pw_name
, pw2
->pw_name
) == 0 &&
388 pw1
->pw_uid
== pw2
->pw_uid
&&
389 pw1
->pw_gid
== pw2
->pw_gid
&&
390 strcmp(pw1
->pw_class
, pw2
->pw_class
) == 0 &&
391 pw1
->pw_change
== pw2
->pw_change
&&
392 pw1
->pw_expire
== pw2
->pw_expire
&&
393 strcmp(pw1
->pw_gecos
, pw2
->pw_gecos
) == 0 &&
394 strcmp(pw1
->pw_dir
, pw2
->pw_dir
) == 0 &&
395 strcmp(pw1
->pw_shell
, pw2
->pw_shell
) == 0);
399 * Make a passwd line out of a struct passwd.
402 pw_make(const struct passwd
*pw
)
406 asprintf(&line
, "%s:%s:%ju:%ju:%s:%ju:%ju:%s:%s:%s", pw
->pw_name
,
407 pw
->pw_passwd
, (uintmax_t)pw
->pw_uid
, (uintmax_t)pw
->pw_gid
,
408 pw
->pw_class
, (uintmax_t)pw
->pw_change
, (uintmax_t)pw
->pw_expire
,
409 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
414 * Make a passwd line (in v7 format) out of a struct passwd
417 pw_make_v7(const struct passwd
*pw
)
421 asprintf(&line
, "%s:*:%ju:%ju:%s:%s:%s", pw
->pw_name
,
422 (uintmax_t)pw
->pw_uid
, (uintmax_t)pw
->pw_gid
,
423 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
428 * Copy password file from one descriptor to another, replacing, deleting
429 * or adding a single record on the way.
432 pw_copy(int ffd
, int tfd
, const struct passwd
*pw
, struct passwd
*old_pw
)
434 char buf
[8192], *end
, *line
, *p
, *q
, *r
, t
;
436 const struct passwd
*spw
;
440 if (old_pw
== NULL
&& pw
== NULL
)
444 /* deleting a user */
448 if ((line
= pw_make(pw
)) == NULL
)
460 /* find the end of the current line */
461 for (p
= q
; q
< end
&& *q
!= '\0'; ++q
)
465 /* if we don't have a complete line, fill up the buffer */
469 if ((size_t)(q
- p
) >= sizeof(buf
)) {
470 warnx("passwd line too long");
471 errno
= EINVAL
; /* hack */
475 q
= memmove(buf
, p
, end
- p
);
480 readlen
= read(ffd
, end
, sizeof(buf
) - (end
- buf
));
484 len
= (size_t)readlen
;
485 if (len
== 0 && p
== buf
)
489 if (len
< (ssize_t
)sizeof(buf
)) {
491 if (len
> 0 && buf
[len
- 1] != '\n')
492 ++len
, *end
++ = '\n';
497 /* is it a blank line or a comment? */
498 for (r
= p
; r
< q
&& isspace(*r
); ++r
)
500 if (r
== q
|| *r
== '#') {
502 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
508 /* is it the one we're looking for? */
513 fpw
= pw_scan(r
, PWSCAN_MASTER
);
516 * fpw is either the struct passwd for the current line,
517 * or NULL if the line is malformed.
521 if (fpw
== NULL
|| strcmp(fpw
->pw_name
, spw
->pw_name
) != 0) {
525 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
530 if (old_pw
&& !pw_equal(fpw
, old_pw
)) {
531 warnx("entry inconsistent");
533 errno
= EINVAL
; /* hack */
538 /* it is, replace or remove it */
541 if (write(tfd
, line
, len
) != (int)len
)
544 /* when removed, avoid the \n */
547 /* we're done, just copy the rest over */
549 if (write(tfd
, q
, end
- q
) != end
- q
)
552 readlen
= read(ffd
, buf
, sizeof(buf
));
556 len
= (size_t)readlen
;
564 /* if we got here, we didn't find the old entry */
570 if ((size_t)write(tfd
, line
, len
) != len
||
571 write(tfd
, "\n", 1) != 1)
584 * Return the current value of tempname.
594 * Duplicate a struct passwd.
597 pw_dup(const struct passwd
*pw
)
604 if (pw
->pw_name
!= NULL
)
605 len
+= strlen(pw
->pw_name
) + 1;
606 if (pw
->pw_passwd
!= NULL
)
607 len
+= strlen(pw
->pw_passwd
) + 1;
608 if (pw
->pw_class
!= NULL
)
609 len
+= strlen(pw
->pw_class
) + 1;
610 if (pw
->pw_gecos
!= NULL
)
611 len
+= strlen(pw
->pw_gecos
) + 1;
612 if (pw
->pw_dir
!= NULL
)
613 len
+= strlen(pw
->pw_dir
) + 1;
614 if (pw
->pw_shell
!= NULL
)
615 len
+= strlen(pw
->pw_shell
) + 1;
616 if ((npw
= malloc((size_t)len
)) == NULL
)
618 memcpy(npw
, pw
, sizeof(*npw
));
619 dst
= (char *)npw
+ sizeof(*npw
);
620 if (pw
->pw_name
!= NULL
) {
622 dst
= stpcpy(npw
->pw_name
, pw
->pw_name
) + 1;
624 if (pw
->pw_passwd
!= NULL
) {
625 npw
->pw_passwd
= dst
;
626 dst
= stpcpy(npw
->pw_passwd
, pw
->pw_passwd
) + 1;
628 if (pw
->pw_class
!= NULL
) {
630 dst
= stpcpy(npw
->pw_class
, pw
->pw_class
) + 1;
632 if (pw
->pw_gecos
!= NULL
) {
634 dst
= stpcpy(npw
->pw_gecos
, pw
->pw_gecos
) + 1;
636 if (pw
->pw_dir
!= NULL
) {
638 dst
= stpcpy(npw
->pw_dir
, pw
->pw_dir
) + 1;
640 if (pw
->pw_shell
!= NULL
) {
642 dst
= stpcpy(npw
->pw_shell
, pw
->pw_shell
) + 1;
650 * Wrapper around an internal libc function
653 pw_scan(const char *line
, int flags
)
655 struct passwd pw
, *ret
;
658 if ((bp
= strdup(line
)) == NULL
)
660 if (!__pw_scan(bp
, &pw
, flags
)) {