]>
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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static const char sccsid
[] = "@(#)pw_util.c 8.3 (Berkeley) 4/2/94";
45 static const char rcsid
[] =
50 * This file is used by all the "password" programs; vipw(8), chpass(1),
54 #include <sys/param.h>
55 #include <sys/errno.h>
57 #include <sys/resource.h>
76 static pid_t editpid
= -1;
77 static int lockfd
= -1;
78 static char masterpasswd
[PATH_MAX
];
79 static char passwd_dir
[PATH_MAX
];
80 static char tempname
[PATH_MAX
];
81 static int initialized
;
94 * Initialize statics and set limits, signals & umask to try to avoid
95 * interruptions, crashes etc. that might expose passord data.
98 pw_init(const char *dir
, const char *master
)
105 strcpy(passwd_dir
, _PATH_ETC
);
107 if (strlen(dir
) >= sizeof(passwd_dir
)) {
108 errno
= ENAMETOOLONG
;
111 strcpy(passwd_dir
, dir
);
114 if (master
== NULL
) {
116 strcpy(masterpasswd
, _PATH_MASTERPASSWD
);
117 } else if (snprintf(masterpasswd
, sizeof(masterpasswd
), "%s/%s",
118 passwd_dir
, _MASTERPASSWD
) > (int)sizeof(masterpasswd
)) {
119 errno
= ENAMETOOLONG
;
123 if (strlen(master
) >= sizeof(masterpasswd
)) {
124 errno
= ENAMETOOLONG
;
127 strcpy(masterpasswd
, master
);
131 * The code that follows is extremely disruptive to the calling
132 * process, and is therefore disabled until someone can conceive
133 * of a realistic scenario where it would fend off a compromise.
134 * Race conditions concerning the temporary files can be guarded
135 * against in other ways than masking signals (by checking stat(2)
136 * results after creation).
139 /* Unlimited resource limits. */
140 rlim
.rlim_cur
= rlim
.rlim_max
= RLIM_INFINITY
;
141 (void)setrlimit(RLIMIT_CPU
, &rlim
);
142 (void)setrlimit(RLIMIT_FSIZE
, &rlim
);
143 (void)setrlimit(RLIMIT_STACK
, &rlim
);
144 (void)setrlimit(RLIMIT_DATA
, &rlim
);
145 (void)setrlimit(RLIMIT_RSS
, &rlim
);
147 /* Don't drop core (not really necessary, but GP's). */
148 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
149 (void)setrlimit(RLIMIT_CORE
, &rlim
);
151 /* Turn off signals. */
152 (void)signal(SIGALRM
, SIG_IGN
);
153 (void)signal(SIGHUP
, SIG_IGN
);
154 (void)signal(SIGINT
, SIG_IGN
);
155 (void)signal(SIGPIPE
, SIG_IGN
);
156 (void)signal(SIGQUIT
, SIG_IGN
);
157 (void)signal(SIGTERM
, SIG_IGN
);
158 (void)signal(SIGCONT
, pw_cont
);
160 /* Create with exact permissions. */
168 * Lock the master password file.
174 if (*masterpasswd
== '\0')
178 * If the master password file doesn't exist, the system is hosed.
179 * Might as well try to build one. Set the close-on-exec bit so
180 * that users can't get at the encrypted passwords while editing.
181 * Open should allow flock'ing the file; see 4.4BSD. XXX
186 lockfd
= open(masterpasswd
, O_RDONLY
, 0);
187 if (lockfd
< 0 || fcntl(lockfd
, F_SETFD
, 1) == -1)
188 err(1, "%s", masterpasswd
);
189 /* XXX vulnerable to race conditions */
190 if (flock(lockfd
, LOCK_EX
|LOCK_NB
) == -1) {
191 if (errno
== EWOULDBLOCK
) {
192 errx(1, "the password db file is busy");
194 err(1, "could not lock the passwd file: ");
199 * If the password file was replaced while we were trying to
200 * get the lock, our hardlink count will be 0 and we have to
203 if (fstat(lockfd
, &st
) == -1)
204 err(1, "fstat() failed: ");
205 if (st
.st_nlink
!= 0)
214 * Create and open a presumably safe temp file for editing the password
215 * data, and copy the master password file into it.
225 if (*masterpasswd
== '\0')
227 if ((p
= strrchr(masterpasswd
, '/')))
231 if (snprintf(tempname
, sizeof(tempname
), "%.*spw.XXXXXX",
232 (int)(p
- masterpasswd
), masterpasswd
) >= (int)sizeof(tempname
)) {
233 errno
= ENAMETOOLONG
;
236 if ((tfd
= mkstemp(tempname
)) == -1)
239 while ((nr
= read(mfd
, buf
, sizeof(buf
))) > 0)
240 if (write(tfd
, buf
, (size_t)nr
) != nr
)
253 * Regenerate the password database.
256 pw_mkdb(const char *user
)
261 (void)fflush(stderr
);
262 switch ((pid
= fork())) {
268 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
269 "-d", passwd_dir
, tempname
, NULL
);
271 execl(_PATH_PWD_MKDB
, "pwd_mkdb", "-p",
272 "-d", passwd_dir
, "-u", user
, tempname
, NULL
);
279 if (waitpid(pid
, &pstat
, 0) == -1)
281 if (WIFEXITED(pstat
) && WEXITSTATUS(pstat
) == 0)
288 * Edit the temp file. Return -1 on error, >0 if the file was modified, 0
292 pw_edit(int notsetuid
)
294 struct sigaction sa
, sa_int
, sa_quit
;
295 sigset_t oldsigset
, sigset
;
296 struct stat st1
, st2
;
300 if ((editor
= getenv("EDITOR")) == NULL
)
302 if (stat(tempname
, &st1
) == -1)
304 sa
.sa_handler
= SIG_IGN
;
305 sigemptyset(&sa
.sa_mask
);
307 sigaction(SIGINT
, &sa
, &sa_int
);
308 sigaction(SIGQUIT
, &sa
, &sa_quit
);
309 sigemptyset(&sigset
);
310 sigaddset(&sigset
, SIGCHLD
);
311 sigprocmask(SIG_BLOCK
, &sigset
, &oldsigset
);
312 switch ((editpid
= fork())) {
316 sigaction(SIGINT
, &sa_int
, NULL
);
317 sigaction(SIGQUIT
, &sa_quit
, NULL
);
318 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
320 (void)setgid(getgid());
321 (void)setuid(getuid());
324 execlp(editor
, basename(editor
), tempname
, NULL
);
331 if (waitpid(editpid
, &pstat
, WUNTRACED
) == -1) {
337 } else if (WIFSTOPPED(pstat
)) {
338 raise(WSTOPSIG(pstat
));
339 } else if (WIFEXITED(pstat
) && WEXITSTATUS(pstat
) == 0) {
348 sigaction(SIGINT
, &sa_int
, NULL
);
349 sigaction(SIGQUIT
, &sa_quit
, NULL
);
350 sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
351 if (stat(tempname
, &st2
) == -1)
353 return (st1
.st_mtime
!= st2
.st_mtime
);
357 * Clean up. Preserve errno for the caller's convenience.
369 kill(editpid
, SIGTERM
);
370 kill(editpid
, SIGCONT
);
371 waitpid(editpid
, &status
, 0);
374 if (*tempname
!= '\0') {
384 * Compares two struct pwds.
387 pw_equal(const struct passwd
*pw1
, const struct passwd
*pw2
)
389 return (strcmp(pw1
->pw_name
, pw2
->pw_name
) == 0 &&
390 pw1
->pw_uid
== pw2
->pw_uid
&&
391 pw1
->pw_gid
== pw2
->pw_gid
&&
392 strcmp(pw1
->pw_class
, pw2
->pw_class
) == 0 &&
393 pw1
->pw_change
== pw2
->pw_change
&&
394 pw1
->pw_expire
== pw2
->pw_expire
&&
395 strcmp(pw1
->pw_gecos
, pw2
->pw_gecos
) == 0 &&
396 strcmp(pw1
->pw_dir
, pw2
->pw_dir
) == 0 &&
397 strcmp(pw1
->pw_shell
, pw2
->pw_shell
) == 0);
401 * Make a passwd line out of a struct passwd.
404 pw_make(const struct passwd
*pw
)
408 asprintf(&line
, "%s:%s:%ju:%ju:%s:%ju:%ju:%s:%s:%s", pw
->pw_name
,
409 pw
->pw_passwd
, (uintmax_t)pw
->pw_uid
, (uintmax_t)pw
->pw_gid
,
410 pw
->pw_class
, (uintmax_t)pw
->pw_change
, (uintmax_t)pw
->pw_expire
,
411 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
416 * Copy password file from one descriptor to another, replacing or adding
417 * a single record on the way.
420 pw_copy(int ffd
, int tfd
, const struct passwd
*pw
, struct passwd
*old_pw
)
422 char buf
[8192], *end
, *line
, *p
, *q
, *r
, t
;
427 if ((line
= pw_make(pw
)) == NULL
)
434 /* find the end of the current line */
435 for (p
= q
; q
< end
&& *q
!= '\0'; ++q
)
439 /* if we don't have a complete line, fill up the buffer */
443 if ((size_t)(q
- p
) >= sizeof(buf
)) {
444 warnx("passwd line too long");
445 errno
= EINVAL
; /* hack */
449 q
= memmove(buf
, p
, end
- p
);
454 readlen
= read(ffd
, end
, sizeof(buf
) - (end
- buf
));
458 len
= (size_t)readlen
;
459 if (len
== 0 && p
== buf
)
463 if (len
< (ssize_t
)sizeof(buf
)) {
465 if (len
> 0 && buf
[len
- 1] != '\n')
466 ++len
, *end
++ = '\n';
471 /* is it a blank line or a comment? */
472 for (r
= p
; r
< q
&& isspace(*r
); ++r
)
474 if (r
== q
|| *r
== '#') {
476 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
482 /* is it the one we're looking for? */
485 fpw
= pw_scan(r
, PWSCAN_MASTER
);
487 if (strcmp(fpw
->pw_name
, pw
->pw_name
) != 0) {
490 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
495 if (old_pw
&& !pw_equal(fpw
, old_pw
)) {
496 warnx("entry inconsistent");
498 errno
= EINVAL
; /* hack */
503 /* it is, replace it */
505 if (write(tfd
, line
, len
) != (int)len
)
508 /* we're done, just copy the rest over */
510 if (write(tfd
, q
, end
- q
) != end
- q
)
513 readlen
= read(ffd
, buf
, sizeof(buf
));
517 len
= (size_t)readlen
;
525 /* if we got here, we have a new entry */
527 if ((size_t)write(tfd
, line
, len
) != len
||
528 write(tfd
, "\n", 1) != 1)
539 * Return the current value of tempname.
549 * Duplicate a struct passwd.
552 pw_dup(const struct passwd
*pw
)
558 (pw
->pw_name
? strlen(pw
->pw_name
) + 1 : 0) +
559 (pw
->pw_passwd
? strlen(pw
->pw_passwd
) + 1 : 0) +
560 (pw
->pw_class
? strlen(pw
->pw_class
) + 1 : 0) +
561 (pw
->pw_gecos
? strlen(pw
->pw_gecos
) + 1 : 0) +
562 (pw
->pw_dir
? strlen(pw
->pw_dir
) + 1 : 0) +
563 (pw
->pw_shell
? strlen(pw
->pw_shell
) + 1 : 0);
564 if ((npw
= malloc((size_t)len
)) == NULL
)
566 memcpy(npw
, pw
, sizeof(*npw
));
569 npw
->pw_name
= ((char *)npw
) + len
;
570 len
+= sprintf(npw
->pw_name
, "%s", pw
->pw_name
) + 1;
573 npw
->pw_passwd
= ((char *)npw
) + len
;
574 len
+= sprintf(npw
->pw_passwd
, "%s", pw
->pw_passwd
) + 1;
577 npw
->pw_class
= ((char *)npw
) + len
;
578 len
+= sprintf(npw
->pw_class
, "%s", pw
->pw_class
) + 1;
581 npw
->pw_gecos
= ((char *)npw
) + len
;
582 len
+= sprintf(npw
->pw_gecos
, "%s", pw
->pw_gecos
) + 1;
585 npw
->pw_dir
= ((char *)npw
) + len
;
586 len
+= sprintf(npw
->pw_dir
, "%s", pw
->pw_dir
) + 1;
589 npw
->pw_shell
= ((char *)npw
) + len
;
590 len
+= sprintf(npw
->pw_shell
, "%s", pw
->pw_shell
) + 1;
598 * Wrapper around an internal libc function
601 pw_scan(const char *line
, int flags
)
603 struct passwd pw
, *ret
;
606 if ((bp
= strdup(line
)) == NULL
)
608 if (!__pw_scan(bp
, &pw
, flags
)) {