]>
git.cameronkatri.com Git - pw-darwin.git/blob - libutil/pw_util.c
2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 2002 Networks Associates Technology, Inc.
9 * Portions of this software were developed for the FreeBSD Project by
10 * ThinkSec AS and NAI Labs, the Security Research Division of Network
11 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
12 * ("CBOSS"), as part of the DARPA CHATS research program.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 __SCCSID("@(#)pw_util.c 8.3 (Berkeley) 4/2/94");
44 * This file is used by all the "password" programs; vipw(8), chpass(1),
48 #include <sys/param.h>
49 #include <sys/errno.h>
51 #include <sys/resource.h>
69 static pid_t editpid
= -1;
70 static int lockfd
= -1;
71 static char masterpasswd
[PATH_MAX
];
72 static char passwd_dir
[PATH_MAX
];
73 static char tempname
[PATH_MAX
];
74 static int initialized
;
87 * Initialize statics and set limits, signals & umask to try to avoid
88 * interruptions, crashes etc. that might expose passord data.
91 pw_init(const char *dir
, const char *master
)
98 strcpy(passwd_dir
, _PATH_ETC
);
100 if (strlen(dir
) >= sizeof(passwd_dir
)) {
101 errno
= ENAMETOOLONG
;
104 strcpy(passwd_dir
, dir
);
107 if (master
== NULL
) {
109 strcpy(masterpasswd
, _PATH_MASTERPASSWD
);
110 } else if (snprintf(masterpasswd
, sizeof(masterpasswd
), "%s/%s",
111 passwd_dir
, _MASTERPASSWD
) > (int)sizeof(masterpasswd
)) {
112 errno
= ENAMETOOLONG
;
116 if (strlen(master
) >= sizeof(masterpasswd
)) {
117 errno
= ENAMETOOLONG
;
120 strcpy(masterpasswd
, master
);
124 * The code that follows is extremely disruptive to the calling
125 * process, and is therefore disabled until someone can conceive
126 * of a realistic scenario where it would fend off a compromise.
127 * Race conditions concerning the temporary files can be guarded
128 * against in other ways than masking signals (by checking stat(2)
129 * results after creation).
132 /* Unlimited resource limits. */
133 rlim
.rlim_cur
= rlim
.rlim_max
= RLIM_INFINITY
;
134 (void)setrlimit(RLIMIT_CPU
, &rlim
);
135 (void)setrlimit(RLIMIT_FSIZE
, &rlim
);
136 (void)setrlimit(RLIMIT_STACK
, &rlim
);
137 (void)setrlimit(RLIMIT_DATA
, &rlim
);
138 (void)setrlimit(RLIMIT_RSS
, &rlim
);
140 /* Don't drop core (not really necessary, but GP's). */
141 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
142 (void)setrlimit(RLIMIT_CORE
, &rlim
);
144 /* Turn off signals. */
145 (void)signal(SIGALRM
, SIG_IGN
);
146 (void)signal(SIGHUP
, SIG_IGN
);
147 (void)signal(SIGINT
, SIG_IGN
);
148 (void)signal(SIGPIPE
, SIG_IGN
);
149 (void)signal(SIGQUIT
, SIG_IGN
);
150 (void)signal(SIGTERM
, SIG_IGN
);
151 (void)signal(SIGCONT
, pw_cont
);
153 /* Create with exact permissions. */
161 * Lock the master password file.
167 if (*masterpasswd
== '\0')
171 * If the master password file doesn't exist, the system is hosed.
172 * Might as well try to build one. Set the close-on-exec bit so
173 * that users can't get at the encrypted passwords while editing.
174 * Open should allow flock'ing the file; see 4.4BSD. XXX
179 lockfd
= flopen(masterpasswd
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
, 0);
181 if (errno
== EWOULDBLOCK
) {
182 errx(1, "the password db file is busy");
184 err(1, "could not lock the passwd file: ");
189 * If the password file was replaced while we were trying to
190 * get the lock, our hardlink count will be 0 and we have to
193 if (fstat(lockfd
, &st
) == -1)
194 err(1, "fstat() failed: ");
195 if (st
.st_nlink
!= 0)
204 * Create and open a presumably safe temp file for editing the password
205 * data, and copy the master password file into it.
215 if (*masterpasswd
== '\0')
217 if ((p
= strrchr(masterpasswd
, '/')))
221 if (snprintf(tempname
, sizeof(tempname
), "%.*spw.XXXXXX",
222 (int)(p
- masterpasswd
), masterpasswd
) >= (int)sizeof(tempname
)) {
223 errno
= ENAMETOOLONG
;
226 if ((tfd
= mkostemp(tempname
, 0)) == -1)
229 while ((nr
= read(mfd
, buf
, sizeof(buf
))) > 0)
230 if (write(tfd
, buf
, (size_t)nr
) != nr
)
243 * Regenerate the password database.
246 pw_mkdb(const char *user
)
251 (void)fflush(stderr
);
252 switch ((pid
= fork())) {
258 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
259 "-d", passwd_dir
, tempname
, (char *)NULL
);
261 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
262 "-d", passwd_dir
, "-u", user
, tempname
,
270 if (waitpid(pid
, &pstat
, 0) == -1)
272 if (WIFEXITED(pstat
) && WEXITSTATUS(pstat
) == 0)
279 * Edit the temp file. Return -1 on error, >0 if the file was modified, 0
283 pw_edit(int notsetuid
)
285 struct sigaction sa
, sa_int
, sa_quit
;
286 sigset_t oldsigset
, nsigset
;
287 struct stat st1
, st2
;
291 if ((editor
= getenv("EDITOR")) == NULL
)
293 if (stat(tempname
, &st1
) == -1)
295 sa
.sa_handler
= SIG_IGN
;
296 sigemptyset(&sa
.sa_mask
);
298 sigaction(SIGINT
, &sa
, &sa_int
);
299 sigaction(SIGQUIT
, &sa
, &sa_quit
);
300 sigemptyset(&nsigset
);
301 sigaddset(&nsigset
, SIGCHLD
);
302 sigprocmask(SIG_BLOCK
, &nsigset
, &oldsigset
);
303 switch ((editpid
= fork())) {
307 sigaction(SIGINT
, &sa_int
, NULL
);
308 sigaction(SIGQUIT
, &sa_quit
, NULL
);
309 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
311 (void)setgid(getgid());
312 (void)setuid(getuid());
315 execlp(editor
, editor
, tempname
, (char *)NULL
);
322 if (waitpid(editpid
, &pstat
, WUNTRACED
) == -1) {
328 } else if (WIFSTOPPED(pstat
)) {
329 raise(WSTOPSIG(pstat
));
330 } else if (WIFEXITED(pstat
) && WEXITSTATUS(pstat
) == 0) {
339 sigaction(SIGINT
, &sa_int
, NULL
);
340 sigaction(SIGQUIT
, &sa_quit
, NULL
);
341 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
342 if (stat(tempname
, &st2
) == -1)
344 return (st1
.st_mtim
.tv_sec
!= st2
.st_mtim
.tv_sec
||
345 st1
.st_mtim
.tv_nsec
!= st2
.st_mtim
.tv_nsec
);
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 * Make a passwd line (in v7 format) out of a struct passwd
411 pw_make_v7(const struct passwd
*pw
)
415 asprintf(&line
, "%s:*:%ju:%ju:%s:%s:%s", pw
->pw_name
,
416 (uintmax_t)pw
->pw_uid
, (uintmax_t)pw
->pw_gid
,
417 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
422 * Copy password file from one descriptor to another, replacing, deleting
423 * or adding a single record on the way.
426 pw_copy(int ffd
, int tfd
, const struct passwd
*pw
, struct passwd
*old_pw
)
428 char *buf
, *end
, *line
, *p
, *q
, *r
, *tmp
;
430 const struct passwd
*spw
;
435 if (old_pw
== NULL
&& pw
== NULL
)
439 /* deleting a user */
443 if ((line
= pw_make(pw
)) == NULL
)
451 /* initialize the buffer */
452 if ((buf
= malloc(size
= 1024)) == NULL
)
459 /* find the end of the current line */
460 for (p
= q
; q
< end
&& *q
!= '\0'; ++q
)
464 /* if we don't have a complete line, fill up the buffer */
468 while ((size_t)(q
- p
) >= size
) {
469 if ((tmp
= reallocarray(buf
, 2, size
)) == NULL
) {
470 warnx("passwd line too long");
475 end
= tmp
+ (end
- buf
);
480 q
= memmove(buf
, p
, end
- p
);
485 readlen
= read(ffd
, end
, size
- (end
- buf
));
489 len
= (size_t)readlen
;
490 if (len
== 0 && p
== buf
)
496 if (len
> 0 && buf
[len
- 1] != '\n')
497 ++len
, *end
++ = '\n';
502 /* is it a blank line or a comment? */
503 for (r
= p
; r
< q
&& isspace(*r
); ++r
)
505 if (r
== q
|| *r
== '#') {
507 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
513 /* is it the one we're looking for? */
518 fpw
= pw_scan(r
, PWSCAN_MASTER
);
521 * fpw is either the struct passwd for the current line,
522 * or NULL if the line is malformed.
526 if (fpw
== NULL
|| strcmp(fpw
->pw_name
, spw
->pw_name
) != 0) {
530 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
535 if (old_pw
&& !pw_equal(fpw
, old_pw
)) {
536 warnx("entry inconsistent");
538 errno
= EINVAL
; /* hack */
543 /* it is, replace or remove it */
546 if (write(tfd
, line
, len
) != (int)len
)
549 /* when removed, avoid the \n */
552 /* we're done, just copy the rest over */
554 if (write(tfd
, q
, end
- q
) != end
- q
)
557 readlen
= read(ffd
, buf
, size
);
561 len
= (size_t)readlen
;
569 /* if we got here, we didn't find the old entry */
575 if ((size_t)write(tfd
, line
, len
) != len
||
576 write(tfd
, "\n", 1) != 1)
589 * Return the current value of tempname.
599 * Duplicate a struct passwd.
602 pw_dup(const struct passwd
*pw
)
609 if (pw
->pw_name
!= NULL
)
610 len
+= strlen(pw
->pw_name
) + 1;
611 if (pw
->pw_passwd
!= NULL
)
612 len
+= strlen(pw
->pw_passwd
) + 1;
613 if (pw
->pw_class
!= NULL
)
614 len
+= strlen(pw
->pw_class
) + 1;
615 if (pw
->pw_gecos
!= NULL
)
616 len
+= strlen(pw
->pw_gecos
) + 1;
617 if (pw
->pw_dir
!= NULL
)
618 len
+= strlen(pw
->pw_dir
) + 1;
619 if (pw
->pw_shell
!= NULL
)
620 len
+= strlen(pw
->pw_shell
) + 1;
621 if ((npw
= malloc((size_t)len
)) == NULL
)
623 memcpy(npw
, pw
, sizeof(*npw
));
624 dst
= (char *)npw
+ sizeof(*npw
);
625 if (pw
->pw_name
!= NULL
) {
627 dst
= stpcpy(npw
->pw_name
, pw
->pw_name
) + 1;
629 if (pw
->pw_passwd
!= NULL
) {
630 npw
->pw_passwd
= dst
;
631 dst
= stpcpy(npw
->pw_passwd
, pw
->pw_passwd
) + 1;
633 if (pw
->pw_class
!= NULL
) {
635 dst
= stpcpy(npw
->pw_class
, pw
->pw_class
) + 1;
637 if (pw
->pw_gecos
!= NULL
) {
639 dst
= stpcpy(npw
->pw_gecos
, pw
->pw_gecos
) + 1;
641 if (pw
->pw_dir
!= NULL
) {
643 dst
= stpcpy(npw
->pw_dir
, pw
->pw_dir
) + 1;
645 if (pw
->pw_shell
!= NULL
) {
647 dst
= stpcpy(npw
->pw_shell
, pw
->pw_shell
) + 1;
655 * Wrapper around some internal libc functions.
659 pw_initpwd(struct passwd
*pw
)
666 pw_scan(const char *line
, int flags
)
668 struct passwd pw
, *ret
;
671 if ((bp
= strdup(line
)) == NULL
)
674 if (!__pw_scan(bp
, &pw
, flags
)) {