1 /*- 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. 5 * All rights reserved. 6 * 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. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 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. 23 * 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 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static const char sccsid[] = "@(#)pw_util.c 8.3 (Berkeley) 4/2/94"; 40 #endif 41 static const char rcsid[] = 42 "$FreeBSD$"; 43 #endif /* not lint */ 44 45 /* 46 * This file is used by all the "password" programs; vipw(8), chpass(1), 47 * and passwd(1). 48 */ 49 50 #include <sys/param.h> 51 #include <sys/errno.h> 52 #include <sys/time.h> 53 #include <sys/resource.h> 54 #include <sys/stat.h> 55 #include <sys/wait.h> 56 57 #include <ctype.h> 58 #include <err.h> 59 #include <fcntl.h> 60 #include <inttypes.h> 61 #include <libgen.h> 62 #include <paths.h> 63 #include <pwd.h> 64 #include <signal.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <unistd.h> 69 70 #include "libutil.h" 71 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; 78 79 #if 0 80 void 81 pw_cont(int sig) 82 { 83 84 if (editpid != -1) 85 kill(editpid, sig); 86 } 87 #endif 88 89 /* 90 * Initialize statics and set limits, signals & umask to try to avoid 91 * interruptions, crashes etc. that might expose passord data. 92 */ 93 int 94 pw_init(const char *dir, const char *master) 95 { 96 #if 0 97 struct rlimit rlim; 98 #endif 99 100 if (dir == NULL) { 101 strcpy(passwd_dir, _PATH_ETC); 102 } else { 103 if (strlen(dir) >= sizeof(passwd_dir)) { 104 errno = ENAMETOOLONG; 105 return (-1); 106 } 107 strcpy(passwd_dir, dir); 108 } 109 110 if (master == NULL) { 111 if (dir == 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; 116 return (-1); 117 } 118 } else { 119 if (strlen(master) >= sizeof(masterpasswd)) { 120 errno = ENAMETOOLONG; 121 return (-1); 122 } 123 strcpy(masterpasswd, master); 124 } 125 126 /* 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). 133 */ 134 #if 0 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); 142 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); 146 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); 155 156 /* Create with exact permissions. */ 157 (void)umask(0); 158 #endif 159 initialized = 1; 160 return (0); 161 } 162 163 /* 164 * Lock the master password file. 165 */ 166 int 167 pw_lock(void) 168 { 169 170 if (*masterpasswd == '\0') 171 return (-1); 172 173 /* 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 178 */ 179 for (;;) { 180 struct stat st; 181 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"); 189 } else { 190 err(1, "could not lock the passwd file: "); 191 } 192 } 193 194 /* 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 197 * close and retry. 198 */ 199 if (fstat(lockfd, &st) == -1) 200 err(1, "fstat() failed: "); 201 if (st.st_nlink != 0) 202 break; 203 close(lockfd); 204 lockfd = -1; 205 } 206 return (lockfd); 207 } 208 209 /* 210 * Create and open a presumably safe temp file for editing the password 211 * data, and copy the master password file into it. 212 */ 213 int 214 pw_tmp(int mfd) 215 { 216 char buf[8192]; 217 ssize_t nr; 218 const char *p; 219 int tfd; 220 221 if (*masterpasswd == '\0') 222 return (-1); 223 if ((p = strrchr(masterpasswd, '/'))) 224 ++p; 225 else 226 p = masterpasswd; 227 if (snprintf(tempname, sizeof(tempname), "%.*spw.XXXXXX", 228 (int)(p - masterpasswd), masterpasswd) >= (int)sizeof(tempname)) { 229 errno = ENAMETOOLONG; 230 return (-1); 231 } 232 if ((tfd = mkstemp(tempname)) == -1) 233 return (-1); 234 if (mfd != -1) { 235 while ((nr = read(mfd, buf, sizeof(buf))) > 0) 236 if (write(tfd, buf, (size_t)nr) != nr) 237 break; 238 if (nr != 0) { 239 unlink(tempname); 240 *tempname = '\0'; 241 close(tfd); 242 return (-1); 243 } 244 } 245 return (tfd); 246 } 247 248 /* 249 * Regenerate the password database. 250 */ 251 int 252 pw_mkdb(const char *user) 253 { 254 int pstat; 255 pid_t pid; 256 257 (void)fflush(stderr); 258 switch ((pid = fork())) { 259 case -1: 260 return (-1); 261 case 0: 262 /* child */ 263 if (user == NULL) 264 execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", 265 "-d", passwd_dir, tempname, (char *)NULL); 266 else 267 execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", 268 "-d", passwd_dir, "-u", user, tempname, 269 (char *)NULL); 270 _exit(1); 271 /* NOTREACHED */ 272 default: 273 /* parent */ 274 break; 275 } 276 if (waitpid(pid, &pstat, 0) == -1) 277 return (-1); 278 if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0) 279 return (0); 280 errno = 0; 281 return (-1); 282 } 283 284 /* 285 * Edit the temp file. Return -1 on error, >0 if the file was modified, 0 286 * if it was not. 287 */ 288 int 289 pw_edit(int notsetuid) 290 { 291 struct sigaction sa, sa_int, sa_quit; 292 sigset_t oldsigset, nsigset; 293 struct stat st1, st2; 294 const char *editor; 295 int pstat; 296 297 if ((editor = getenv("EDITOR")) == NULL) 298 editor = _PATH_VI; 299 if (stat(tempname, &st1) == -1) 300 return (-1); 301 sa.sa_handler = SIG_IGN; 302 sigemptyset(&sa.sa_mask); 303 sa.sa_flags = 0; 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())) { 310 case -1: 311 return (-1); 312 case 0: 313 sigaction(SIGINT, &sa_int, NULL); 314 sigaction(SIGQUIT, &sa_quit, NULL); 315 sigprocmask(SIG_SETMASK, &oldsigset, NULL); 316 if (notsetuid) { 317 (void)setgid(getgid()); 318 (void)setuid(getuid()); 319 } 320 errno = 0; 321 execlp(editor, basename(editor), tempname, (char *)NULL); 322 _exit(errno); 323 default: 324 /* parent */ 325 break; 326 } 327 for (;;) { 328 if (waitpid(editpid, &pstat, WUNTRACED) == -1) { 329 if (errno == EINTR) 330 continue; 331 unlink(tempname); 332 editpid = -1; 333 break; 334 } else if (WIFSTOPPED(pstat)) { 335 raise(WSTOPSIG(pstat)); 336 } else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0) { 337 editpid = -1; 338 break; 339 } else { 340 unlink(tempname); 341 editpid = -1; 342 break; 343 } 344 } 345 sigaction(SIGINT, &sa_int, NULL); 346 sigaction(SIGQUIT, &sa_quit, NULL); 347 sigprocmask(SIG_SETMASK, &oldsigset, NULL); 348 if (stat(tempname, &st2) == -1) 349 return (-1); 350 return (st1.st_mtime != st2.st_mtime); 351 } 352 353 /* 354 * Clean up. Preserve errno for the caller's convenience. 355 */ 356 void 357 pw_fini(void) 358 { 359 int serrno, status; 360 361 if (!initialized) 362 return; 363 initialized = 0; 364 serrno = errno; 365 if (editpid != -1) { 366 kill(editpid, SIGTERM); 367 kill(editpid, SIGCONT); 368 waitpid(editpid, &status, 0); 369 editpid = -1; 370 } 371 if (*tempname != '\0') { 372 unlink(tempname); 373 *tempname = '\0'; 374 } 375 if (lockfd != -1) 376 close(lockfd); 377 errno = serrno; 378 } 379 380 /* 381 * Compares two struct pwds. 382 */ 383 int 384 pw_equal(const struct passwd *pw1, const struct passwd *pw2) 385 { 386 return (strcmp(pw1->pw_name, pw2->pw_name) == 0 && 387 pw1->pw_uid == pw2->pw_uid && 388 pw1->pw_gid == pw2->pw_gid && 389 strcmp(pw1->pw_class, pw2->pw_class) == 0 && 390 pw1->pw_change == pw2->pw_change && 391 pw1->pw_expire == pw2->pw_expire && 392 strcmp(pw1->pw_gecos, pw2->pw_gecos) == 0 && 393 strcmp(pw1->pw_dir, pw2->pw_dir) == 0 && 394 strcmp(pw1->pw_shell, pw2->pw_shell) == 0); 395 } 396 397 /* 398 * Make a passwd line out of a struct passwd. 399 */ 400 char * 401 pw_make(const struct passwd *pw) 402 { 403 char *line; 404 405 asprintf(&line, "%s:%s:%ju:%ju:%s:%ju:%ju:%s:%s:%s", pw->pw_name, 406 pw->pw_passwd, (uintmax_t)pw->pw_uid, (uintmax_t)pw->pw_gid, 407 pw->pw_class, (uintmax_t)pw->pw_change, (uintmax_t)pw->pw_expire, 408 pw->pw_gecos, pw->pw_dir, pw->pw_shell); 409 return (line); 410 } 411 412 /* 413 * Make a passwd line (in v7 format) out of a struct passwd 414 */ 415 char * 416 pw_make_v7(const struct passwd *pw) 417 { 418 char *line; 419 420 asprintf(&line, "%s:*:%ju:%ju:%s:%s:%s", pw->pw_name, 421 (uintmax_t)pw->pw_uid, (uintmax_t)pw->pw_gid, 422 pw->pw_gecos, pw->pw_dir, pw->pw_shell); 423 return (line); 424 } 425 426 /* 427 * Copy password file from one descriptor to another, replacing, deleting 428 * or adding a single record on the way. 429 */ 430 int 431 pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw) 432 { 433 char buf[8192], *end, *line, *p, *q, *r, t; 434 struct passwd *fpw; 435 const struct passwd *spw; 436 size_t len; 437 int eof, readlen; 438 439 spw = pw; 440 if (pw == NULL) { 441 line = NULL; 442 if (old_pw == NULL) 443 return (-1); 444 spw = old_pw; 445 } else if ((line = pw_make(pw)) == NULL) 446 return (-1); 447 448 eof = 0; 449 len = 0; 450 p = q = end = buf; 451 for (;;) { 452 /* find the end of the current line */ 453 for (p = q; q < end && *q != '\0'; ++q) 454 if (*q == '\n') 455 break; 456 457 /* if we don't have a complete line, fill up the buffer */ 458 if (q >= end) { 459 if (eof) 460 break; 461 if ((size_t)(q - p) >= sizeof(buf)) { 462 warnx("passwd line too long"); 463 errno = EINVAL; /* hack */ 464 goto err; 465 } 466 if (p < end) { 467 q = memmove(buf, p, end - p); 468 end -= p - buf; 469 } else { 470 p = q = end = buf; 471 } 472 readlen = read(ffd, end, sizeof(buf) - (end - buf)); 473 if (readlen == -1) 474 goto err; 475 else 476 len = (size_t)readlen; 477 if (len == 0 && p == buf) 478 break; 479 end += len; 480 len = end - buf; 481 if (len < (ssize_t)sizeof(buf)) { 482 eof = 1; 483 if (len > 0 && buf[len - 1] != '\n') 484 ++len, *end++ = '\n'; 485 } 486 continue; 487 } 488 489 /* is it a blank line or a comment? */ 490 for (r = p; r < q && isspace(*r); ++r) 491 /* nothing */ ; 492 if (r == q || *r == '#') { 493 /* yep */ 494 if (write(tfd, p, q - p + 1) != q - p + 1) 495 goto err; 496 ++q; 497 continue; 498 } 499 500 /* is it the one we're looking for? */ 501 502 t = *q; 503 *q = '\0'; 504 505 fpw = pw_scan(r, PWSCAN_MASTER); 506 507 /* 508 * fpw is either the struct passwd for the current line, 509 * or NULL if the line is malformed. 510 */ 511 512 *q = t; 513 if (fpw == NULL || fpw->pw_uid != spw->pw_uid) { 514 /* nope */ 515 if (fpw != NULL) 516 free(fpw); 517 if (write(tfd, p, q - p + 1) != q - p + 1) 518 goto err; 519 ++q; 520 continue; 521 } 522 if (old_pw && !pw_equal(fpw, old_pw)) { 523 warnx("entry inconsistent"); 524 free(fpw); 525 errno = EINVAL; /* hack */ 526 goto err; 527 } 528 free(fpw); 529 530 /* it is, replace or remove it */ 531 if (line != NULL) { 532 len = strlen(line); 533 if (write(tfd, line, len) != (int)len) 534 goto err; 535 } else { 536 /* when removed, avoid the \n */ 537 q++; 538 } 539 /* we're done, just copy the rest over */ 540 for (;;) { 541 if (write(tfd, q, end - q) != end - q) 542 goto err; 543 q = buf; 544 readlen = read(ffd, buf, sizeof(buf)); 545 if (readlen == 0) 546 break; 547 else 548 len = (size_t)readlen; 549 if (readlen == -1) 550 goto err; 551 end = buf + len; 552 } 553 goto done; 554 } 555 556 /* if we got here, we didn't find the old entry */ 557 if (line == NULL) { 558 errno = ENOENT; 559 goto err; 560 } 561 len = strlen(line); 562 if ((size_t)write(tfd, line, len) != len || 563 write(tfd, "\n", 1) != 1) 564 goto err; 565 done: 566 if (line != NULL) 567 free(line); 568 return (0); 569 err: 570 if (line != NULL) 571 free(line); 572 return (-1); 573 } 574 575 /* 576 * Return the current value of tempname. 577 */ 578 const char * 579 pw_tempname(void) 580 { 581 582 return (tempname); 583 } 584 585 /* 586 * Duplicate a struct passwd. 587 */ 588 struct passwd * 589 pw_dup(const struct passwd *pw) 590 { 591 char *dst; 592 struct passwd *npw; 593 ssize_t len; 594 595 len = sizeof(*npw); 596 if (pw->pw_name != NULL) 597 len += strlen(pw->pw_name) + 1; 598 if (pw->pw_passwd != NULL) 599 len += strlen(pw->pw_passwd) + 1; 600 if (pw->pw_class != NULL) 601 len += strlen(pw->pw_class) + 1; 602 if (pw->pw_gecos != NULL) 603 len += strlen(pw->pw_gecos) + 1; 604 if (pw->pw_dir != NULL) 605 len += strlen(pw->pw_dir) + 1; 606 if (pw->pw_shell != NULL) 607 len += strlen(pw->pw_shell) + 1; 608 if ((npw = malloc((size_t)len)) == NULL) 609 return (NULL); 610 memcpy(npw, pw, sizeof(*npw)); 611 dst = (char *)npw + sizeof(*npw); 612 if (pw->pw_name != NULL) { 613 npw->pw_name = dst; 614 dst = stpcpy(npw->pw_name, pw->pw_name) + 1; 615 } 616 if (pw->pw_passwd != NULL) { 617 npw->pw_passwd = dst; 618 dst = stpcpy(npw->pw_passwd, pw->pw_passwd) + 1; 619 } 620 if (pw->pw_class != NULL) { 621 npw->pw_class = dst; 622 dst = stpcpy(npw->pw_class, pw->pw_class) + 1; 623 } 624 if (pw->pw_gecos != NULL) { 625 npw->pw_gecos = dst; 626 dst = stpcpy(npw->pw_gecos, pw->pw_gecos) + 1; 627 } 628 if (pw->pw_dir != NULL) { 629 npw->pw_dir = dst; 630 dst = stpcpy(npw->pw_dir, pw->pw_dir) + 1; 631 } 632 if (pw->pw_shell != NULL) { 633 npw->pw_shell = dst; 634 dst = stpcpy(npw->pw_shell, pw->pw_shell) + 1; 635 } 636 return (npw); 637 } 638 639 #include "pw_scan.h" 640 641 /* 642 * Wrapper around an internal libc function 643 */ 644 struct passwd * 645 pw_scan(const char *line, int flags) 646 { 647 struct passwd pw, *ret; 648 char *bp; 649 650 if ((bp = strdup(line)) == NULL) 651 return (NULL); 652 if (!__pw_scan(bp, &pw, flags)) { 653 free(bp); 654 return (NULL); 655 } 656 ret = pw_dup(&pw); 657 free(bp); 658 return (ret); 659 } 660