]>
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 * 3. 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>
71 static pid_t editpid
= -1;
72 static int lockfd
= -1;
73 static char masterpasswd
[PATH_MAX
];
74 static char passwd_dir
[PATH_MAX
];
75 static char tempname
[PATH_MAX
];
76 static int initialized
;
89 * Initialize statics and set limits, signals & umask to try to avoid
90 * interruptions, crashes etc. that might expose passord data.
93 pw_init(const char *dir
, const char *master
)
100 strcpy(passwd_dir
, _PATH_ETC
);
102 if (strlen(dir
) >= sizeof(passwd_dir
)) {
103 errno
= ENAMETOOLONG
;
106 strcpy(passwd_dir
, dir
);
109 if (master
== NULL
) {
111 strcpy(masterpasswd
, _PATH_MASTERPASSWD
);
112 } else if (snprintf(masterpasswd
, sizeof(masterpasswd
), "%s/%s",
113 passwd_dir
, _MASTERPASSWD
) > (int)sizeof(masterpasswd
)) {
114 errno
= ENAMETOOLONG
;
118 if (strlen(master
) >= sizeof(masterpasswd
)) {
119 errno
= ENAMETOOLONG
;
122 strcpy(masterpasswd
, master
);
126 * The code that follows is extremely disruptive to the calling
127 * process, and is therefore disabled until someone can conceive
128 * of a realistic scenario where it would fend off a compromise.
129 * Race conditions concerning the temporary files can be guarded
130 * against in other ways than masking signals (by checking stat(2)
131 * results after creation).
134 /* Unlimited resource limits. */
135 rlim
.rlim_cur
= rlim
.rlim_max
= RLIM_INFINITY
;
136 (void)setrlimit(RLIMIT_CPU
, &rlim
);
137 (void)setrlimit(RLIMIT_FSIZE
, &rlim
);
138 (void)setrlimit(RLIMIT_STACK
, &rlim
);
139 (void)setrlimit(RLIMIT_DATA
, &rlim
);
140 (void)setrlimit(RLIMIT_RSS
, &rlim
);
142 /* Don't drop core (not really necessary, but GP's). */
143 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
144 (void)setrlimit(RLIMIT_CORE
, &rlim
);
146 /* Turn off signals. */
147 (void)signal(SIGALRM
, SIG_IGN
);
148 (void)signal(SIGHUP
, SIG_IGN
);
149 (void)signal(SIGINT
, SIG_IGN
);
150 (void)signal(SIGPIPE
, SIG_IGN
);
151 (void)signal(SIGQUIT
, SIG_IGN
);
152 (void)signal(SIGTERM
, SIG_IGN
);
153 (void)signal(SIGCONT
, pw_cont
);
155 /* Create with exact permissions. */
163 * Lock the master password file.
169 if (*masterpasswd
== '\0')
173 * If the master password file doesn't exist, the system is hosed.
174 * Might as well try to build one. Set the close-on-exec bit so
175 * that users can't get at the encrypted passwords while editing.
176 * Open should allow flock'ing the file; see 4.4BSD. XXX
181 lockfd
= flopen(masterpasswd
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
, 0);
183 if (errno
== EWOULDBLOCK
) {
184 errx(1, "the password db file is busy");
186 err(1, "could not lock the passwd file: ");
191 * If the password file was replaced while we were trying to
192 * get the lock, our hardlink count will be 0 and we have to
195 if (fstat(lockfd
, &st
) == -1)
196 err(1, "fstat() failed: ");
197 if (st
.st_nlink
!= 0)
206 * Create and open a presumably safe temp file for editing the password
207 * data, and copy the master password file into it.
217 if (*masterpasswd
== '\0')
219 if ((p
= strrchr(masterpasswd
, '/')))
223 if (snprintf(tempname
, sizeof(tempname
), "%.*spw.XXXXXX",
224 (int)(p
- masterpasswd
), masterpasswd
) >= (int)sizeof(tempname
)) {
225 errno
= ENAMETOOLONG
;
228 if ((tfd
= mkostemp(tempname
, 0)) == -1)
231 while ((nr
= read(mfd
, buf
, sizeof(buf
))) > 0)
232 if (write(tfd
, buf
, (size_t)nr
) != nr
)
245 * Regenerate the password database.
248 pw_mkdb(const char *user
)
253 (void)fflush(stderr
);
254 switch ((pid
= fork())) {
260 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
261 "-d", passwd_dir
, tempname
, (char *)NULL
);
263 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
264 "-d", passwd_dir
, "-u", user
, tempname
,
272 if (waitpid(pid
, &pstat
, 0) == -1)
274 if (WIFEXITED(pstat
) && WEXITSTATUS(pstat
) == 0)
281 * Edit the temp file. Return -1 on error, >0 if the file was modified, 0
285 pw_edit(int notsetuid
)
287 struct sigaction sa
, sa_int
, sa_quit
;
288 sigset_t oldsigset
, nsigset
;
289 struct stat st1
, st2
;
293 if ((editor
= getenv("EDITOR")) == NULL
)
295 if (stat(tempname
, &st1
) == -1)
297 sa
.sa_handler
= SIG_IGN
;
298 sigemptyset(&sa
.sa_mask
);
300 sigaction(SIGINT
, &sa
, &sa_int
);
301 sigaction(SIGQUIT
, &sa
, &sa_quit
);
302 sigemptyset(&nsigset
);
303 sigaddset(&nsigset
, SIGCHLD
);
304 sigprocmask(SIG_BLOCK
, &nsigset
, &oldsigset
);
305 switch ((editpid
= fork())) {
309 sigaction(SIGINT
, &sa_int
, NULL
);
310 sigaction(SIGQUIT
, &sa_quit
, NULL
);
311 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
313 (void)setgid(getgid());
314 (void)setuid(getuid());
317 execlp(editor
, editor
, tempname
, (char *)NULL
);
324 if (waitpid(editpid
, &pstat
, WUNTRACED
) == -1) {
330 } else if (WIFSTOPPED(pstat
)) {
331 raise(WSTOPSIG(pstat
));
332 } else if (WIFEXITED(pstat
) && WEXITSTATUS(pstat
) == 0) {
341 sigaction(SIGINT
, &sa_int
, NULL
);
342 sigaction(SIGQUIT
, &sa_quit
, NULL
);
343 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
344 if (stat(tempname
, &st2
) == -1)
346 return (st1
.st_mtim
.tv_sec
!= st2
.st_mtim
.tv_sec
||
347 st1
.st_mtim
.tv_nsec
!= st2
.st_mtim
.tv_nsec
);
351 * Clean up. Preserve errno for the caller's convenience.
363 kill(editpid
, SIGTERM
);
364 kill(editpid
, SIGCONT
);
365 waitpid(editpid
, &status
, 0);
368 if (*tempname
!= '\0') {
378 * Compares two struct pwds.
381 pw_equal(const struct passwd
*pw1
, const struct passwd
*pw2
)
383 return (strcmp(pw1
->pw_name
, pw2
->pw_name
) == 0 &&
384 pw1
->pw_uid
== pw2
->pw_uid
&&
385 pw1
->pw_gid
== pw2
->pw_gid
&&
386 strcmp(pw1
->pw_class
, pw2
->pw_class
) == 0 &&
387 pw1
->pw_change
== pw2
->pw_change
&&
388 pw1
->pw_expire
== pw2
->pw_expire
&&
389 strcmp(pw1
->pw_gecos
, pw2
->pw_gecos
) == 0 &&
390 strcmp(pw1
->pw_dir
, pw2
->pw_dir
) == 0 &&
391 strcmp(pw1
->pw_shell
, pw2
->pw_shell
) == 0);
395 * Make a passwd line out of a struct passwd.
398 pw_make(const struct passwd
*pw
)
402 asprintf(&line
, "%s:%s:%ju:%ju:%s:%ju:%ju:%s:%s:%s", pw
->pw_name
,
403 pw
->pw_passwd
, (uintmax_t)pw
->pw_uid
, (uintmax_t)pw
->pw_gid
,
404 pw
->pw_class
, (uintmax_t)pw
->pw_change
, (uintmax_t)pw
->pw_expire
,
405 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
410 * Make a passwd line (in v7 format) out of a struct passwd
413 pw_make_v7(const struct passwd
*pw
)
417 asprintf(&line
, "%s:*:%ju:%ju:%s:%s:%s", pw
->pw_name
,
418 (uintmax_t)pw
->pw_uid
, (uintmax_t)pw
->pw_gid
,
419 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
424 * Copy password file from one descriptor to another, replacing, deleting
425 * or adding a single record on the way.
428 pw_copy(int ffd
, int tfd
, const struct passwd
*pw
, struct passwd
*old_pw
)
430 char *buf
, *end
, *line
, *p
, *q
, *r
, *tmp
;
432 const struct passwd
*spw
;
437 if (old_pw
== NULL
&& pw
== NULL
)
441 /* deleting a user */
445 if ((line
= pw_make(pw
)) == NULL
)
453 /* initialize the buffer */
454 if ((buf
= malloc(size
= 1024)) == NULL
)
461 /* find the end of the current line */
462 for (p
= q
; q
< end
&& *q
!= '\0'; ++q
)
466 /* if we don't have a complete line, fill up the buffer */
470 while ((size_t)(q
- p
) >= size
) {
471 if ((tmp
= realloc(buf
, size
* 2)) == NULL
) {
472 warnx("passwd line too long");
477 end
= tmp
+ (end
- buf
);
482 q
= memmove(buf
, p
, end
- p
);
487 readlen
= read(ffd
, end
, size
- (end
- buf
));
491 len
= (size_t)readlen
;
492 if (len
== 0 && p
== buf
)
498 if (len
> 0 && buf
[len
- 1] != '\n')
499 ++len
, *end
++ = '\n';
504 /* is it a blank line or a comment? */
505 for (r
= p
; r
< q
&& isspace(*r
); ++r
)
507 if (r
== q
|| *r
== '#') {
509 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
515 /* is it the one we're looking for? */
520 fpw
= pw_scan(r
, PWSCAN_MASTER
);
523 * fpw is either the struct passwd for the current line,
524 * or NULL if the line is malformed.
528 if (fpw
== NULL
|| strcmp(fpw
->pw_name
, spw
->pw_name
) != 0) {
532 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
537 if (old_pw
&& !pw_equal(fpw
, old_pw
)) {
538 warnx("entry inconsistent");
540 errno
= EINVAL
; /* hack */
545 /* it is, replace or remove it */
548 if (write(tfd
, line
, len
) != (int)len
)
551 /* when removed, avoid the \n */
554 /* we're done, just copy the rest over */
556 if (write(tfd
, q
, end
- q
) != end
- q
)
559 readlen
= read(ffd
, buf
, size
);
563 len
= (size_t)readlen
;
571 /* if we got here, we didn't find the old entry */
577 if ((size_t)write(tfd
, line
, len
) != len
||
578 write(tfd
, "\n", 1) != 1)
591 * Return the current value of tempname.
601 * Duplicate a struct passwd.
604 pw_dup(const struct passwd
*pw
)
611 if (pw
->pw_name
!= NULL
)
612 len
+= strlen(pw
->pw_name
) + 1;
613 if (pw
->pw_passwd
!= NULL
)
614 len
+= strlen(pw
->pw_passwd
) + 1;
615 if (pw
->pw_class
!= NULL
)
616 len
+= strlen(pw
->pw_class
) + 1;
617 if (pw
->pw_gecos
!= NULL
)
618 len
+= strlen(pw
->pw_gecos
) + 1;
619 if (pw
->pw_dir
!= NULL
)
620 len
+= strlen(pw
->pw_dir
) + 1;
621 if (pw
->pw_shell
!= NULL
)
622 len
+= strlen(pw
->pw_shell
) + 1;
623 if ((npw
= malloc((size_t)len
)) == NULL
)
625 memcpy(npw
, pw
, sizeof(*npw
));
626 dst
= (char *)npw
+ sizeof(*npw
);
627 if (pw
->pw_name
!= NULL
) {
629 dst
= stpcpy(npw
->pw_name
, pw
->pw_name
) + 1;
631 if (pw
->pw_passwd
!= NULL
) {
632 npw
->pw_passwd
= dst
;
633 dst
= stpcpy(npw
->pw_passwd
, pw
->pw_passwd
) + 1;
635 if (pw
->pw_class
!= NULL
) {
637 dst
= stpcpy(npw
->pw_class
, pw
->pw_class
) + 1;
639 if (pw
->pw_gecos
!= NULL
) {
641 dst
= stpcpy(npw
->pw_gecos
, pw
->pw_gecos
) + 1;
643 if (pw
->pw_dir
!= NULL
) {
645 dst
= stpcpy(npw
->pw_dir
, pw
->pw_dir
) + 1;
647 if (pw
->pw_shell
!= NULL
) {
649 dst
= stpcpy(npw
->pw_shell
, pw
->pw_shell
) + 1;
657 * Wrapper around an internal libc function
660 pw_scan(const char *line
, int flags
)
662 struct passwd pw
, *ret
;
665 if ((bp
= strdup(line
)) == NULL
)
667 if (!__pw_scan(bp
, &pw
, flags
)) {