]>
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>
67 #include <os/availability.h>
68 API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
69 void * reallocarray(void * in_ptr
, size_t nmemb
, size_t size
) __DARWIN_EXTSN(reallocarray
) __result_use_check
;
73 static pid_t editpid
= -1;
74 static int lockfd
= -1;
75 static char masterpasswd
[PATH_MAX
];
76 static char passwd_dir
[PATH_MAX
];
77 static char tempname
[PATH_MAX
];
78 static int initialized
;
91 * Initialize statics and set limits, signals & umask to try to avoid
92 * interruptions, crashes etc. that might expose passord data.
95 pw_init(const char *dir
, const char *master
)
102 strcpy(passwd_dir
, _PATH_PWD
);
104 if (strlen(dir
) >= sizeof(passwd_dir
)) {
105 errno
= ENAMETOOLONG
;
108 strcpy(passwd_dir
, dir
);
111 if (master
== NULL
) {
113 strcpy(masterpasswd
, _PATH_MASTERPASSWD
);
114 } else if (snprintf(masterpasswd
, sizeof(masterpasswd
), "%s/%s",
115 passwd_dir
, _MASTERPASSWD
) > (int)sizeof(masterpasswd
)) {
116 errno
= ENAMETOOLONG
;
120 if (strlen(master
) >= sizeof(masterpasswd
)) {
121 errno
= ENAMETOOLONG
;
124 strcpy(masterpasswd
, master
);
128 * The code that follows is extremely disruptive to the calling
129 * process, and is therefore disabled until someone can conceive
130 * of a realistic scenario where it would fend off a compromise.
131 * Race conditions concerning the temporary files can be guarded
132 * against in other ways than masking signals (by checking stat(2)
133 * results after creation).
136 /* Unlimited resource limits. */
137 rlim
.rlim_cur
= rlim
.rlim_max
= RLIM_INFINITY
;
138 (void)setrlimit(RLIMIT_CPU
, &rlim
);
139 (void)setrlimit(RLIMIT_FSIZE
, &rlim
);
140 (void)setrlimit(RLIMIT_STACK
, &rlim
);
141 (void)setrlimit(RLIMIT_DATA
, &rlim
);
142 (void)setrlimit(RLIMIT_RSS
, &rlim
);
144 /* Don't drop core (not really necessary, but GP's). */
145 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
146 (void)setrlimit(RLIMIT_CORE
, &rlim
);
148 /* Turn off signals. */
149 (void)signal(SIGALRM
, SIG_IGN
);
150 (void)signal(SIGHUP
, SIG_IGN
);
151 (void)signal(SIGINT
, SIG_IGN
);
152 (void)signal(SIGPIPE
, SIG_IGN
);
153 (void)signal(SIGQUIT
, SIG_IGN
);
154 (void)signal(SIGTERM
, SIG_IGN
);
155 (void)signal(SIGCONT
, pw_cont
);
157 /* Create with exact permissions. */
165 * Lock the master password file.
171 if (*masterpasswd
== '\0')
175 * If the master password file doesn't exist, the system is hosed.
176 * Might as well try to build one. Set the close-on-exec bit so
177 * that users can't get at the encrypted passwords while editing.
178 * Open should allow flock'ing the file; see 4.4BSD. XXX
183 lockfd
= flopen(masterpasswd
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
, 0);
185 if (errno
== EWOULDBLOCK
) {
186 errx(1, "the password db file is busy");
188 err(1, "could not lock the passwd file");
193 * If the password file was replaced while we were trying to
194 * get the lock, our hardlink count will be 0 and we have to
197 if (fstat(lockfd
, &st
) == -1)
198 err(1, "fstat() failed");
199 if (st
.st_nlink
!= 0)
208 * Create and open a presumably safe temp file for editing the password
209 * data, and copy the master password file into it.
219 if (*masterpasswd
== '\0')
221 if ((p
= strrchr(masterpasswd
, '/')))
225 if (snprintf(tempname
, sizeof(tempname
), "%.*spw.XXXXXX",
226 (int)(p
- masterpasswd
), masterpasswd
) >= (int)sizeof(tempname
)) {
227 errno
= ENAMETOOLONG
;
230 if ((tfd
= mkostemp(tempname
, 0)) == -1)
233 while ((nr
= read(mfd
, buf
, sizeof(buf
))) > 0)
234 if (write(tfd
, buf
, (size_t)nr
) != nr
)
247 * Regenerate the password database.
250 pw_mkdb(const char *user
)
255 (void)fflush(stderr
);
256 switch ((pid
= fork())) {
262 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
263 "-d", passwd_dir
, tempname
, (char *)NULL
);
265 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
266 "-d", passwd_dir
, "-u", user
, tempname
,
274 if (waitpid(pid
, &pstat
, 0) == -1)
276 if (WIFEXITED(pstat
) && WEXITSTATUS(pstat
) == 0)
283 * Edit the temp file. Return -1 on error, >0 if the file was modified, 0
287 pw_edit(int notsetuid
)
289 struct sigaction sa
, sa_int
, sa_quit
;
290 sigset_t oldsigset
, nsigset
;
291 struct stat st1
, st2
;
295 if ((editor
= getenv("EDITOR")) == NULL
)
297 if (stat(tempname
, &st1
) == -1)
299 sa
.sa_handler
= SIG_IGN
;
300 sigemptyset(&sa
.sa_mask
);
302 sigaction(SIGINT
, &sa
, &sa_int
);
303 sigaction(SIGQUIT
, &sa
, &sa_quit
);
304 sigemptyset(&nsigset
);
305 sigaddset(&nsigset
, SIGCHLD
);
306 sigprocmask(SIG_BLOCK
, &nsigset
, &oldsigset
);
307 switch ((editpid
= fork())) {
311 sigaction(SIGINT
, &sa_int
, NULL
);
312 sigaction(SIGQUIT
, &sa_quit
, NULL
);
313 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
315 if (setgid(getgid()) == -1)
317 if (setuid(getuid()) == -1)
320 execlp(editor
, editor
, tempname
, (char *)NULL
);
321 err(1, "%s", editor
);
327 if (waitpid(editpid
, &pstat
, WUNTRACED
) == -1) {
333 } else if (WIFSTOPPED(pstat
)) {
334 raise(WSTOPSIG(pstat
));
335 } else if (WIFEXITED(pstat
)) {
336 if (WEXITSTATUS(pstat
) != 0)
337 errx(1, "\"%s\" exited with status %d", editor
, WEXITSTATUS(pstat
));
346 sigaction(SIGINT
, &sa_int
, NULL
);
347 sigaction(SIGQUIT
, &sa_quit
, NULL
);
348 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
349 if (stat(tempname
, &st2
) == -1)
351 return (st1
.st_mtimespec
.tv_sec
!= st2
.st_mtimespec
.tv_sec
||
352 st1
.st_mtimespec
.tv_nsec
!= st2
.st_mtimespec
.tv_nsec
);
356 * Clean up. Preserve errno for the caller's convenience.
368 kill(editpid
, SIGTERM
);
369 kill(editpid
, SIGCONT
);
370 waitpid(editpid
, &status
, 0);
373 if (*tempname
!= '\0') {
383 * Compares two struct pwds.
386 pw_equal(const struct passwd
*pw1
, const struct passwd
*pw2
)
388 return (strcmp(pw1
->pw_name
, pw2
->pw_name
) == 0 &&
389 pw1
->pw_uid
== pw2
->pw_uid
&&
390 pw1
->pw_gid
== pw2
->pw_gid
&&
391 strcmp(pw1
->pw_class
, pw2
->pw_class
) == 0 &&
392 pw1
->pw_change
== pw2
->pw_change
&&
393 pw1
->pw_expire
== pw2
->pw_expire
&&
394 strcmp(pw1
->pw_gecos
, pw2
->pw_gecos
) == 0 &&
395 strcmp(pw1
->pw_dir
, pw2
->pw_dir
) == 0 &&
396 strcmp(pw1
->pw_shell
, pw2
->pw_shell
) == 0);
400 * Make a passwd line out of a struct passwd.
403 pw_make(const struct passwd
*pw
)
407 asprintf(&line
, "%s:%s:%ju:%ju:%s:%ju:%ju:%s:%s:%s", pw
->pw_name
,
408 pw
->pw_passwd
, (uintmax_t)pw
->pw_uid
, (uintmax_t)pw
->pw_gid
,
409 pw
->pw_class
, (uintmax_t)pw
->pw_change
, (uintmax_t)pw
->pw_expire
,
410 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
415 * Make a passwd line (in v7 format) out of a struct passwd
418 pw_make_v7(const struct passwd
*pw
)
422 asprintf(&line
, "%s:*:%ju:%ju:%s:%s:%s", pw
->pw_name
,
423 (uintmax_t)pw
->pw_uid
, (uintmax_t)pw
->pw_gid
,
424 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
429 * Copy password file from one descriptor to another, replacing, deleting
430 * or adding a single record on the way.
433 pw_copy(int ffd
, int tfd
, const struct passwd
*pw
, struct passwd
*old_pw
)
435 char *buf
, *end
, *line
, *p
, *q
, *r
, *tmp
;
437 const struct passwd
*spw
;
442 if (old_pw
== NULL
&& pw
== NULL
)
446 /* deleting a user */
450 if ((line
= pw_make(pw
)) == NULL
)
458 /* initialize the buffer */
459 if ((buf
= malloc(size
= 1024)) == NULL
)
466 /* find the end of the current line */
467 for (p
= q
; q
< end
&& *q
!= '\0'; ++q
)
471 /* if we don't have a complete line, fill up the buffer */
475 while ((size_t)(q
- p
) >= size
) {
476 if ((tmp
= reallocarray(buf
, 2, size
)) == NULL
) {
477 warnx("passwd line too long");
482 end
= tmp
+ (end
- buf
);
487 q
= memmove(buf
, p
, end
- p
);
492 readlen
= read(ffd
, end
, size
- (end
- buf
));
496 len
= (size_t)readlen
;
497 if (len
== 0 && p
== buf
)
503 if (len
> 0 && buf
[len
- 1] != '\n')
504 ++len
, *end
++ = '\n';
509 /* is it a blank line or a comment? */
510 for (r
= p
; r
< q
&& isspace(*r
); ++r
)
512 if (r
== q
|| *r
== '#') {
514 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
520 /* is it the one we're looking for? */
525 fpw
= pw_scan(r
, PWSCAN_MASTER
);
528 * fpw is either the struct passwd for the current line,
529 * or NULL if the line is malformed.
533 if (fpw
== NULL
|| strcmp(fpw
->pw_name
, spw
->pw_name
) != 0) {
537 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
542 if (old_pw
&& !pw_equal(fpw
, old_pw
)) {
543 warnx("entry inconsistent");
545 errno
= EINVAL
; /* hack */
550 /* it is, replace or remove it */
553 if (write(tfd
, line
, len
) != (int)len
)
556 /* when removed, avoid the \n */
559 /* we're done, just copy the rest over */
561 if (write(tfd
, q
, end
- q
) != end
- q
)
564 readlen
= read(ffd
, buf
, size
);
568 len
= (size_t)readlen
;
576 /* if we got here, we didn't find the old entry */
582 if ((size_t)write(tfd
, line
, len
) != len
||
583 write(tfd
, "\n", 1) != 1)
596 * Return the current value of tempname.
606 * Duplicate a struct passwd.
609 pw_dup(const struct passwd
*pw
)
616 if (pw
->pw_name
!= NULL
)
617 len
+= strlen(pw
->pw_name
) + 1;
618 if (pw
->pw_passwd
!= NULL
)
619 len
+= strlen(pw
->pw_passwd
) + 1;
620 if (pw
->pw_class
!= NULL
)
621 len
+= strlen(pw
->pw_class
) + 1;
622 if (pw
->pw_gecos
!= NULL
)
623 len
+= strlen(pw
->pw_gecos
) + 1;
624 if (pw
->pw_dir
!= NULL
)
625 len
+= strlen(pw
->pw_dir
) + 1;
626 if (pw
->pw_shell
!= NULL
)
627 len
+= strlen(pw
->pw_shell
) + 1;
628 if ((npw
= malloc((size_t)len
)) == NULL
)
630 memcpy(npw
, pw
, sizeof(*npw
));
631 dst
= (char *)npw
+ sizeof(*npw
);
632 if (pw
->pw_name
!= NULL
) {
634 dst
= stpcpy(npw
->pw_name
, pw
->pw_name
) + 1;
636 if (pw
->pw_passwd
!= NULL
) {
637 npw
->pw_passwd
= dst
;
638 dst
= stpcpy(npw
->pw_passwd
, pw
->pw_passwd
) + 1;
640 if (pw
->pw_class
!= NULL
) {
642 dst
= stpcpy(npw
->pw_class
, pw
->pw_class
) + 1;
644 if (pw
->pw_gecos
!= NULL
) {
646 dst
= stpcpy(npw
->pw_gecos
, pw
->pw_gecos
) + 1;
648 if (pw
->pw_dir
!= NULL
) {
650 dst
= stpcpy(npw
->pw_dir
, pw
->pw_dir
) + 1;
652 if (pw
->pw_shell
!= NULL
) {
654 dst
= stpcpy(npw
->pw_shell
, pw
->pw_shell
) + 1;
662 * Wrapper around some internal libc functions.
666 pw_initpwd(struct passwd
*pw
)
673 pw_scan(const char *line
, int flags
)
675 struct passwd pw
, *ret
;
678 if ((bp
= strdup(line
)) == NULL
)
681 if (!__pw_scan(bp
, &pw
, flags
)) {