1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (C) 1996 5 * David L. Nugent. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 */ 29 30 #ifndef lint 31 static const char rcsid[] = 32 "$FreeBSD$"; 33 #endif /* not lint */ 34 35 #include <sys/param.h> 36 #include <sys/types.h> 37 38 #include <assert.h> 39 #include <ctype.h> 40 #include <dirent.h> 41 #include <err.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 #include <grp.h> 45 #include <pwd.h> 46 #include <libutil.h> 47 #include <login_cap.h> 48 #include <paths.h> 49 #include <string.h> 50 #include <sysexits.h> 51 #include <termios.h> 52 #include <unistd.h> 53 #include <spawn.h> 54 55 #include "pw.h" 56 #include "bitmap.h" 57 #include "psdate.h" 58 59 #define LOGNAMESIZE (MAXLOGNAME-1) 60 61 extern char **environ; 62 static char locked_str[] = "*LOCKED*"; 63 64 static struct passwd fakeuser = { 65 "nouser", 66 "*", 67 -1, 68 -1, 69 0, 70 "", 71 "User &", 72 "/nonexistent", 73 "/bin/sh", 74 0, 75 0 76 }; 77 78 static int print_user(struct passwd *pwd, bool pretty, bool v7); 79 static uid_t pw_uidpolicy(struct userconf *cnf, intmax_t id); 80 static uid_t pw_gidpolicy(struct userconf *cnf, char *grname, char *nam, 81 gid_t prefer, bool dryrun); 82 static char *pw_homepolicy(struct userconf * cnf, char *homedir, 83 const char *user); 84 static char *pw_shellpolicy(struct userconf * cnf); 85 static char *pw_password(struct userconf * cnf, char const * user); 86 static char *shell_path(char const * path, char *shells[], char *sh); 87 static void rmat(uid_t uid); 88 89 static void 90 mkdir_home_parents(int dfd, const char *dir) 91 { 92 struct stat st; 93 char *dirs, *tmp; 94 95 if (*dir != '/') 96 errx(EX_DATAERR, "invalid base directory for home '%s'", dir); 97 98 dir++; 99 100 if (fstatat(dfd, dir, &st, 0) != -1) { 101 if (S_ISDIR(st.st_mode)) 102 return; 103 errx(EX_OSFILE, "root home `/%s' is not a directory", dir); 104 } 105 106 dirs = strdup(dir); 107 if (dirs == NULL) 108 errx(EX_UNAVAILABLE, "out of memory"); 109 110 tmp = strrchr(dirs, '/'); 111 if (tmp == NULL) { 112 free(dirs); 113 return; 114 } 115 tmp[0] = '\0'; 116 117 /* 118 * This is a kludge especially for Joerg :) 119 * If the home directory would be created in the root partition, then 120 * we really create it under /usr which is likely to have more space. 121 * But we create a symlink from cnf->home -> "/usr" -> cnf->home 122 */ 123 if (strchr(dirs, '/') == NULL) { 124 asprintf(&tmp, "usr/%s", dirs); 125 if (tmp == NULL) 126 errx(EX_UNAVAILABLE, "out of memory"); 127 if (mkdirat(dfd, tmp, _DEF_DIRMODE) != -1 || errno == EEXIST) { 128 fchownat(dfd, tmp, 0, 0, 0); 129 symlinkat(tmp, dfd, dirs); 130 } 131 free(tmp); 132 } 133 tmp = dirs; 134 if (fstatat(dfd, dirs, &st, 0) == -1) { 135 while ((tmp = strchr(tmp + 1, '/')) != NULL) { 136 *tmp = '\0'; 137 if (fstatat(dfd, dirs, &st, 0) == -1) { 138 if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1) 139 err(EX_OSFILE, "'%s' (root home parent) is not a directory", dirs); 140 } 141 *tmp = '/'; 142 } 143 } 144 if (fstatat(dfd, dirs, &st, 0) == -1) { 145 if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1) 146 err(EX_OSFILE, "'%s' (root home parent) is not a directory", dirs); 147 fchownat(dfd, dirs, 0, 0, 0); 148 } 149 150 free(dirs); 151 } 152 153 static void 154 create_and_populate_homedir(struct userconf *cnf, struct passwd *pwd, 155 const char *skeldir, mode_t homemode, bool update) 156 { 157 int skelfd = -1; 158 159 /* Create home parents directories */ 160 mkdir_home_parents(conf.rootfd, pwd->pw_dir); 161 162 if (skeldir != NULL && *skeldir != '\0') { 163 if (*skeldir == '/') 164 skeldir++; 165 skelfd = openat(conf.rootfd, skeldir, O_DIRECTORY|O_CLOEXEC); 166 } 167 168 copymkdir(conf.rootfd, pwd->pw_dir, skelfd, homemode, pwd->pw_uid, 169 pwd->pw_gid, 0); 170 pw_log(cnf, update ? M_UPDATE : M_ADD, W_USER, "%s(%ju) home %s made", 171 pwd->pw_name, (uintmax_t)pwd->pw_uid, pwd->pw_dir); 172 } 173 174 static int 175 pw_set_passwd(struct passwd *pwd, int fd, bool precrypted, bool update) 176 { 177 int b, istty; 178 struct termios t, n; 179 login_cap_t *lc; 180 char line[_PASSWORD_LEN+1]; 181 char *p; 182 183 if (fd == '-') { 184 if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 185 pwd->pw_passwd = "*"; /* No access */ 186 return (1); 187 } 188 return (0); 189 } 190 191 if ((istty = isatty(fd))) { 192 if (tcgetattr(fd, &t) == -1) 193 istty = 0; 194 else { 195 n = t; 196 n.c_lflag &= ~(ECHO); 197 tcsetattr(fd, TCSANOW, &n); 198 printf("%s%spassword for user %s:", 199 update ? "new " : "", 200 precrypted ? "encrypted " : "", 201 pwd->pw_name); 202 fflush(stdout); 203 } 204 } 205 b = read(fd, line, sizeof(line) - 1); 206 if (istty) { /* Restore state */ 207 tcsetattr(fd, TCSANOW, &t); 208 fputc('\n', stdout); 209 fflush(stdout); 210 } 211 212 if (b < 0) 213 err(EX_IOERR, "-%c file descriptor", 214 precrypted ? 'H' : 'h'); 215 line[b] = '\0'; 216 if ((p = strpbrk(line, "\r\n")) != NULL) 217 *p = '\0'; 218 if (!*line) 219 errx(EX_DATAERR, "empty password read on file descriptor %d", 220 fd); 221 if (precrypted) { 222 if (strchr(line, ':') != NULL) 223 errx(EX_DATAERR, "bad encrypted password"); 224 pwd->pw_passwd = strdup(line); 225 } else { 226 lc = login_getpwclass(pwd); 227 if (lc == NULL || 228 login_setcryptfmt(lc, "sha512", NULL) == NULL) 229 warn("setting crypt(3) format"); 230 login_close(lc); 231 pwd->pw_passwd = pw_pwcrypt(line); 232 } 233 return (1); 234 } 235 236 static void 237 perform_chgpwent(const char *name, struct passwd *pwd, char *nispasswd) 238 { 239 int rc; 240 struct passwd *nispwd; 241 242 /* duplicate for nis so that chgpwent is not modifying before NIS */ 243 if (nispasswd && *nispasswd == '/') 244 nispwd = pw_dup(pwd); 245 246 rc = chgpwent(name, pwd); 247 if (rc == -1) 248 errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name); 249 else if (rc != 0) 250 err(EX_IOERR, "passwd file update"); 251 252 if (nispasswd && *nispasswd == '/') { 253 rc = chgnispwent(nispasswd, name, nispwd); 254 if (rc == -1) 255 warn("User '%s' not found in NIS passwd", pwd->pw_name); 256 else if (rc != 0) 257 warn("NIS passwd update"); 258 /* NOTE: NIS-only update errors are not fatal */ 259 } 260 } 261 262 /* 263 * The M_LOCK and M_UNLOCK functions simply add or remove 264 * a "*LOCKED*" prefix from in front of the password to 265 * prevent it decoding correctly, and therefore prevents 266 * access. Of course, this only prevents access via 267 * password authentication (not ssh, kerberos or any 268 * other method that does not use the UNIX password) but 269 * that is a known limitation. 270 */ 271 static int 272 pw_userlock(char *arg1, int mode) 273 { 274 struct passwd *pwd = NULL; 275 char *passtmp = NULL; 276 char *name; 277 bool locked = false; 278 uid_t id = (uid_t)-1; 279 280 if (geteuid() != 0) 281 errx(EX_NOPERM, "you must be root"); 282 283 if (arg1 == NULL) 284 errx(EX_DATAERR, "username or id required"); 285 286 name = arg1; 287 if (arg1[strspn(name, "0123456789")] == '\0') 288 id = pw_checkid(name, UID_MAX); 289 290 pwd = GETPWNAM(pw_checkname(name, 0)); 291 if (pwd == NULL && id != (uid_t)-1) { 292 pwd = GETPWUID(id); 293 if (pwd != NULL) 294 name = pwd->pw_name; 295 } 296 if (pwd == NULL) { 297 if (id == (uid_t)-1) 298 errx(EX_NOUSER, "no such name or uid `%ju'", (uintmax_t) id); 299 errx(EX_NOUSER, "no such user `%s'", name); 300 } 301 302 if (name == NULL) 303 name = pwd->pw_name; 304 305 if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str) -1) == 0) 306 locked = true; 307 if (mode == M_LOCK && locked) 308 errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 309 if (mode == M_UNLOCK && !locked) 310 errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 311 312 if (mode == M_LOCK) { 313 asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 314 if (passtmp == NULL) /* disaster */ 315 errx(EX_UNAVAILABLE, "out of memory"); 316 pwd->pw_passwd = passtmp; 317 } else { 318 pwd->pw_passwd += sizeof(locked_str)-1; 319 } 320 321 perform_chgpwent(name, pwd, NULL); 322 free(passtmp); 323 324 return (EXIT_SUCCESS); 325 } 326 327 static uid_t 328 pw_uidpolicy(struct userconf * cnf, intmax_t id) 329 { 330 struct passwd *pwd; 331 struct bitmap bm; 332 uid_t uid = (uid_t) - 1; 333 334 /* 335 * Check the given uid, if any 336 */ 337 if (id >= 0) { 338 uid = (uid_t) id; 339 340 if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate) 341 errx(EX_DATAERR, "uid `%ju' has already been allocated", 342 (uintmax_t)pwd->pw_uid); 343 return (uid); 344 } 345 /* 346 * We need to allocate the next available uid under one of 347 * two policies a) Grab the first unused uid b) Grab the 348 * highest possible unused uid 349 */ 350 if (cnf->min_uid >= cnf->max_uid) { /* Sanity 351 * claus^H^H^H^Hheck */ 352 cnf->min_uid = 1000; 353 cnf->max_uid = 32000; 354 } 355 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 356 357 /* 358 * Now, let's fill the bitmap from the password file 359 */ 360 SETPWENT(); 361 while ((pwd = GETPWENT()) != NULL) 362 if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 363 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 364 ENDPWENT(); 365 366 /* 367 * Then apply the policy, with fallback to reuse if necessary 368 */ 369 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 370 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 371 372 /* 373 * Another sanity check 374 */ 375 if (uid < cnf->min_uid || uid > cnf->max_uid) 376 errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 377 bm_dealloc(&bm); 378 return (uid); 379 } 380 381 static uid_t 382 pw_gidpolicy(struct userconf *cnf, char *grname, char *nam, gid_t prefer, bool dryrun) 383 { 384 struct group *grp; 385 gid_t gid = (uid_t) - 1; 386 387 /* 388 * Check the given gid, if any 389 */ 390 SETGRENT(); 391 if (grname) { 392 if ((grp = GETGRNAM(grname)) == NULL) { 393 gid = pw_checkid(grname, GID_MAX); 394 grp = GETGRGID(gid); 395 } 396 gid = grp->gr_gid; 397 } else if ((grp = GETGRNAM(nam)) != NULL && 398 (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 399 gid = grp->gr_gid; /* Already created? Use it anyway... */ 400 } else { 401 intmax_t grid = -1; 402 403 /* 404 * We need to auto-create a group with the user's name. We 405 * can send all the appropriate output to our sister routine 406 * bit first see if we can create a group with gid==uid so we 407 * can keep the user and group ids in sync. We purposely do 408 * NOT check the gid range if we can force the sync. If the 409 * user's name dups an existing group, then the group add 410 * function will happily handle that case for us and exit. 411 */ 412 if (GETGRGID(prefer) == NULL) 413 grid = prefer; 414 if (dryrun) { 415 gid = pw_groupnext(cnf, true); 416 } else { 417 if (grid == -1) 418 grid = pw_groupnext(cnf, true); 419 groupadd(cnf, nam, grid, NULL, -1, false, false, false); 420 if ((grp = GETGRNAM(nam)) != NULL) 421 gid = grp->gr_gid; 422 } 423 } 424 ENDGRENT(); 425 return (gid); 426 } 427 428 static char * 429 pw_homepolicy(struct userconf * cnf, char *homedir, const char *user) 430 { 431 static char home[128]; 432 433 if (homedir) 434 return (homedir); 435 436 if (cnf->home == NULL || *cnf->home == '\0') 437 errx(EX_CONFIG, "no base home directory set"); 438 snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 439 440 return (home); 441 } 442 443 static char * 444 shell_path(char const * path, char *shells[], char *sh) 445 { 446 if (sh != NULL && (*sh == '/' || *sh == '\0')) 447 return sh; /* specified full path or forced none */ 448 else { 449 char *p; 450 char paths[_UC_MAXLINE]; 451 452 /* 453 * We need to search paths 454 */ 455 strlcpy(paths, path, sizeof(paths)); 456 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 457 int i; 458 static char shellpath[256]; 459 460 if (sh != NULL) { 461 snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); 462 if (access(shellpath, X_OK) == 0) 463 return shellpath; 464 } else 465 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 466 snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); 467 if (access(shellpath, X_OK) == 0) 468 return shellpath; 469 } 470 } 471 if (sh == NULL) 472 errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 473 errx(EX_CONFIG, "no default shell available or defined"); 474 return NULL; 475 } 476 } 477 478 static char * 479 pw_shellpolicy(struct userconf * cnf) 480 { 481 482 return shell_path(cnf->shelldir, cnf->shells, cnf->shell_default); 483 } 484 485 #define SALTSIZE 32 486 487 static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 488 489 char * 490 pw_pwcrypt(char *password) 491 { 492 int i; 493 char salt[SALTSIZE + 1]; 494 char *cryptpw; 495 static char buf[256]; 496 size_t pwlen; 497 498 /* 499 * Calculate a salt value 500 */ 501 for (i = 0; i < SALTSIZE; i++) 502 salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 503 salt[SALTSIZE] = '\0'; 504 505 cryptpw = crypt(password, salt); 506 if (cryptpw == NULL) 507 errx(EX_CONFIG, "crypt(3) failure"); 508 pwlen = strlcpy(buf, cryptpw, sizeof(buf)); 509 assert(pwlen < sizeof(buf)); 510 return (buf); 511 } 512 513 static char * 514 pw_password(struct userconf * cnf, char const * user) 515 { 516 int i, l; 517 char pwbuf[32]; 518 519 switch (cnf->default_password) { 520 case P_NONE: /* No password at all! */ 521 return ""; 522 case P_RANDOM: /* Random password */ 523 l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 524 for (i = 0; i < l; i++) 525 pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 526 pwbuf[i] = '\0'; 527 528 /* 529 * We give this information back to the user 530 */ 531 if (conf.fd == -1) { 532 if (isatty(STDOUT_FILENO)) 533 printf("Password for '%s' is: ", user); 534 printf("%s\n", pwbuf); 535 fflush(stdout); 536 } 537 break; 538 case P_YES: /* user's name */ 539 strlcpy(pwbuf, user, sizeof(pwbuf)); 540 break; 541 case P_NO: /* No login - default */ 542 /* FALLTHROUGH */ 543 default: 544 return "*"; 545 } 546 return pw_pwcrypt(pwbuf); 547 } 548 549 static int 550 print_user(struct passwd * pwd, bool pretty, bool v7) 551 { 552 int j; 553 char *p; 554 struct group *grp = GETGRGID(pwd->pw_gid); 555 char uname[60] = "User &", office[60] = "[None]", 556 wphone[60] = "[None]", hphone[60] = "[None]"; 557 char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 558 struct tm * tptr; 559 560 if (!pretty) { 561 p = v7 ? pw_make_v7(pwd) : pw_make(pwd); 562 printf("%s\n", p); 563 free(p); 564 return (EXIT_SUCCESS); 565 } 566 567 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 568 strlcpy(uname, p, sizeof(uname)); 569 if ((p = strtok(NULL, ",")) != NULL) { 570 strlcpy(office, p, sizeof(office)); 571 if ((p = strtok(NULL, ",")) != NULL) { 572 strlcpy(wphone, p, sizeof(wphone)); 573 if ((p = strtok(NULL, "")) != NULL) { 574 strlcpy(hphone, p, sizeof(hphone)); 575 } 576 } 577 } 578 } 579 /* 580 * Handle '&' in gecos field 581 */ 582 if ((p = strchr(uname, '&')) != NULL) { 583 int l = strlen(pwd->pw_name); 584 int m = strlen(p); 585 586 memmove(p + l, p + 1, m); 587 memmove(p, pwd->pw_name, l); 588 *p = (char) toupper((unsigned char)*p); 589 } 590 if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 591 strftime(acexpire, sizeof acexpire, "%c", tptr); 592 if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 593 strftime(pwexpire, sizeof pwexpire, "%c", tptr); 594 printf("Login Name: %-15s #%-12ju Group: %-15s #%ju\n" 595 " Full Name: %s\n" 596 " Home: %-26.26s Class: %s\n" 597 " Shell: %-26.26s Office: %s\n" 598 "Work Phone: %-26.26s Home Phone: %s\n" 599 "Acc Expire: %-26.26s Pwd Expire: %s\n", 600 pwd->pw_name, (uintmax_t)pwd->pw_uid, 601 grp ? grp->gr_name : "(invalid)", (uintmax_t)pwd->pw_gid, 602 uname, pwd->pw_dir, pwd->pw_class, 603 pwd->pw_shell, office, wphone, hphone, 604 acexpire, pwexpire); 605 SETGRENT(); 606 j = 0; 607 while ((grp=GETGRENT()) != NULL) { 608 int i = 0; 609 if (grp->gr_mem != NULL) { 610 while (grp->gr_mem[i] != NULL) { 611 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) { 612 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 613 break; 614 } 615 ++i; 616 } 617 } 618 } 619 ENDGRENT(); 620 printf("%s", j ? "\n" : ""); 621 return (EXIT_SUCCESS); 622 } 623 624 char * 625 pw_checkname(char *name, int gecos) 626 { 627 char showch[8]; 628 const char *badchars, *ch, *showtype; 629 int reject; 630 631 ch = name; 632 reject = 0; 633 if (gecos) { 634 /* See if the name is valid as a gecos (comment) field. */ 635 badchars = ":"; 636 showtype = "gecos field"; 637 } else { 638 /* See if the name is valid as a userid or group. */ 639 badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\";"; 640 showtype = "userid/group name"; 641 /* Userids and groups can not have a leading '-'. */ 642 if (*ch == '-') 643 reject = 1; 644 } 645 if (!reject) { 646 while (*ch) { 647 if (strchr(badchars, *ch) != NULL || 648 (!gecos && *ch < ' ') || 649 *ch == 127) { 650 reject = 1; 651 break; 652 } 653 /* 8-bit characters are only allowed in GECOS fields */ 654 if (!gecos && (*ch & 0x80)) { 655 reject = 1; 656 break; 657 } 658 ch++; 659 } 660 } 661 /* 662 * A `$' is allowed as the final character for userids and groups, 663 * mainly for the benefit of samba. 664 */ 665 if (reject && !gecos) { 666 if (*ch == '$' && *(ch + 1) == '\0') { 667 reject = 0; 668 ch++; 669 } 670 } 671 if (reject) { 672 snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 673 ? "`%c'" : "0x%02x", *ch); 674 errx(EX_DATAERR, "invalid character %s at position %td in %s", 675 showch, (ch - name), showtype); 676 } 677 if (!gecos && (ch - name) > LOGNAMESIZE) 678 errx(EX_USAGE, "name too long `%s' (max is %d)", name, 679 LOGNAMESIZE); 680 681 return (name); 682 } 683 684 static void 685 rmat(uid_t uid) 686 { 687 DIR *d = opendir("/var/at/jobs"); 688 689 if (d != NULL) { 690 struct dirent *e; 691 692 while ((e = readdir(d)) != NULL) { 693 struct stat st; 694 695 if (strncmp(e->d_name, ".lock", 5) != 0 && 696 stat(e->d_name, &st) == 0 && 697 !S_ISDIR(st.st_mode) && 698 st.st_uid == uid) { 699 const char *argv[] = { 700 "/usr/sbin/atrm", 701 e->d_name, 702 NULL 703 }; 704 if (posix_spawn(NULL, argv[0], NULL, NULL, 705 (char *const *) argv, environ)) { 706 warn("Failed to execute '%s %s'", 707 argv[0], argv[1]); 708 } 709 } 710 } 711 closedir(d); 712 } 713 } 714 715 int 716 pw_user_next(int argc, char **argv, char *name __unused) 717 { 718 struct userconf *cnf = NULL; 719 const char *cfg = NULL; 720 int ch; 721 bool quiet = false; 722 uid_t next; 723 724 while ((ch = getopt(argc, argv, "C:q")) != -1) { 725 switch (ch) { 726 case 'C': 727 cfg = optarg; 728 break; 729 case 'q': 730 quiet = true; 731 break; 732 default: 733 exit(EX_USAGE); 734 } 735 } 736 737 if (quiet) 738 freopen(_PATH_DEVNULL, "w", stderr); 739 740 cnf = get_userconfig(cfg); 741 742 next = pw_uidpolicy(cnf, -1); 743 744 printf("%ju:", (uintmax_t)next); 745 pw_groupnext(cnf, quiet); 746 747 return (EXIT_SUCCESS); 748 } 749 750 int 751 pw_user_show(int argc, char **argv, char *arg1) 752 { 753 struct passwd *pwd = NULL; 754 char *name = NULL; 755 intmax_t id = -1; 756 int ch; 757 bool all = false; 758 bool pretty = false; 759 bool force = false; 760 bool v7 = false; 761 bool quiet = false; 762 763 if (arg1 != NULL) { 764 if (arg1[strspn(arg1, "0123456789")] == '\0') 765 id = pw_checkid(arg1, UID_MAX); 766 else 767 name = arg1; 768 } 769 770 while ((ch = getopt(argc, argv, "C:qn:u:FPa7")) != -1) { 771 switch (ch) { 772 case 'C': 773 /* ignore compatibility */ 774 break; 775 case 'q': 776 quiet = true; 777 break; 778 case 'n': 779 name = optarg; 780 break; 781 case 'u': 782 id = pw_checkid(optarg, UID_MAX); 783 break; 784 case 'F': 785 force = true; 786 break; 787 case 'P': 788 pretty = true; 789 break; 790 case 'a': 791 all = true; 792 break; 793 case '7': 794 v7 = true; 795 break; 796 default: 797 exit(EX_USAGE); 798 } 799 } 800 801 if (quiet) 802 freopen(_PATH_DEVNULL, "w", stderr); 803 804 if (all) { 805 SETPWENT(); 806 while ((pwd = GETPWENT()) != NULL) 807 print_user(pwd, pretty, v7); 808 ENDPWENT(); 809 return (EXIT_SUCCESS); 810 } 811 812 if (id < 0 && name == NULL) 813 errx(EX_DATAERR, "username or id required"); 814 815 pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 816 if (pwd == NULL) { 817 if (force) { 818 pwd = &fakeuser; 819 } else { 820 if (name == NULL) 821 errx(EX_NOUSER, "no such uid `%ju'", 822 (uintmax_t) id); 823 errx(EX_NOUSER, "no such user `%s'", name); 824 } 825 } 826 827 return (print_user(pwd, pretty, v7)); 828 } 829 830 int 831 pw_user_del(int argc, char **argv, char *arg1) 832 { 833 struct userconf *cnf = NULL; 834 struct passwd *pwd = NULL; 835 struct group *gr, *grp; 836 char *name = NULL; 837 char grname[MAXLOGNAME]; 838 char *nispasswd = NULL; 839 char file[MAXPATHLEN]; 840 char home[MAXPATHLEN]; 841 const char *cfg = NULL; 842 struct stat st; 843 intmax_t id = -1; 844 int ch, rc; 845 bool nis = false; 846 bool deletehome = false; 847 bool quiet = false; 848 849 if (arg1 != NULL) { 850 if (arg1[strspn(arg1, "0123456789")] == '\0') 851 id = pw_checkid(arg1, UID_MAX); 852 else 853 name = arg1; 854 } 855 856 while ((ch = getopt(argc, argv, "C:qn:u:rYy:")) != -1) { 857 switch (ch) { 858 case 'C': 859 cfg = optarg; 860 break; 861 case 'q': 862 quiet = true; 863 break; 864 case 'n': 865 name = optarg; 866 break; 867 case 'u': 868 id = pw_checkid(optarg, UID_MAX); 869 break; 870 case 'r': 871 deletehome = true; 872 break; 873 case 'y': 874 nispasswd = optarg; 875 break; 876 case 'Y': 877 nis = true; 878 break; 879 default: 880 exit(EX_USAGE); 881 } 882 } 883 884 if (quiet) 885 freopen(_PATH_DEVNULL, "w", stderr); 886 887 if (id < 0 && name == NULL) 888 errx(EX_DATAERR, "username or id required"); 889 890 cnf = get_userconfig(cfg); 891 892 if (nispasswd == NULL) 893 nispasswd = cnf->nispasswd; 894 895 pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 896 if (pwd == NULL) { 897 if (name == NULL) 898 errx(EX_NOUSER, "no such uid `%ju'", (uintmax_t) id); 899 errx(EX_NOUSER, "no such user `%s'", name); 900 } 901 902 if (PWF._altdir == PWF_REGULAR && 903 ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) { 904 if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 905 if (!nis && nispasswd && *nispasswd != '/') 906 errx(EX_NOUSER, "Cannot remove NIS user `%s'", 907 name); 908 } else { 909 errx(EX_NOUSER, "Cannot remove non local user `%s'", 910 name); 911 } 912 } 913 914 id = pwd->pw_uid; 915 if (name == NULL) 916 name = pwd->pw_name; 917 918 if (strcmp(pwd->pw_name, "root") == 0) 919 errx(EX_DATAERR, "cannot remove user 'root'"); 920 921 if (!PWALTDIR()) { 922 /* Remove crontabs */ 923 snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 924 if (access(file, F_OK) == 0) { 925 const char *argv[] = { 926 "crontab", 927 "-u", 928 pwd->pw_name, 929 "-r", 930 NULL 931 }; 932 if (posix_spawnp(NULL, argv[0], NULL, NULL, 933 (char *const *) argv, environ)) { 934 warn("Failed to execute '%s %s'", 935 argv[0], argv[1]); 936 } 937 } 938 } 939 940 /* 941 * Save these for later, since contents of pwd may be 942 * invalidated by deletion 943 */ 944 snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 945 strlcpy(home, pwd->pw_dir, sizeof(home)); 946 gr = GETGRGID(pwd->pw_gid); 947 if (gr != NULL) 948 strlcpy(grname, gr->gr_name, LOGNAMESIZE); 949 else 950 grname[0] = '\0'; 951 952 rc = delpwent(pwd); 953 if (rc == -1) 954 err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 955 else if (rc != 0) 956 err(EX_IOERR, "passwd update"); 957 958 if (nis && nispasswd && *nispasswd=='/') { 959 rc = delnispwent(nispasswd, name); 960 if (rc == -1) 961 warnx("WARNING: user '%s' does not exist in NIS passwd", 962 pwd->pw_name); 963 else if (rc != 0) 964 warn("WARNING: NIS passwd update"); 965 } 966 967 grp = GETGRNAM(name); 968 if (grp != NULL && 969 (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 970 strcmp(name, grname) == 0) 971 delgrent(GETGRNAM(name)); 972 SETGRENT(); 973 while ((grp = GETGRENT()) != NULL) { 974 int i, j; 975 char group[MAXLOGNAME]; 976 if (grp->gr_mem == NULL) 977 continue; 978 979 for (i = 0; grp->gr_mem[i] != NULL; i++) { 980 if (strcmp(grp->gr_mem[i], name) != 0) 981 continue; 982 983 for (j = i; grp->gr_mem[j] != NULL; j++) 984 grp->gr_mem[j] = grp->gr_mem[j+1]; 985 strlcpy(group, grp->gr_name, MAXLOGNAME); 986 chggrent(group, grp); 987 } 988 } 989 ENDGRENT(); 990 991 pw_log(cnf, M_DELETE, W_USER, "%s(%ju) account removed", name, 992 (uintmax_t)id); 993 994 /* Remove mail file */ 995 if (PWALTDIR() != PWF_ALT) 996 unlinkat(conf.rootfd, file + 1, 0); 997 998 /* Remove at jobs */ 999 if (!PWALTDIR() && getpwuid(id) == NULL) 1000 rmat(id); 1001 1002 /* Remove home directory and contents */ 1003 if (PWALTDIR() != PWF_ALT && deletehome && *home == '/' && 1004 GETPWUID(id) == NULL && 1005 fstatat(conf.rootfd, home + 1, &st, 0) != -1) { 1006 rm_r(conf.rootfd, home, id); 1007 pw_log(cnf, M_DELETE, W_USER, "%s(%ju) home '%s' %s" 1008 "removed", name, (uintmax_t)id, home, 1009 fstatat(conf.rootfd, home + 1, &st, 0) == -1 ? "" : "not " 1010 "completely "); 1011 } 1012 1013 return (EXIT_SUCCESS); 1014 } 1015 1016 int 1017 pw_user_lock(int argc, char **argv, char *arg1) 1018 { 1019 int ch; 1020 1021 while ((ch = getopt(argc, argv, "Cq")) != -1) { 1022 switch (ch) { 1023 case 'C': 1024 case 'q': 1025 /* compatibility */ 1026 break; 1027 default: 1028 exit(EX_USAGE); 1029 } 1030 } 1031 1032 return (pw_userlock(arg1, M_LOCK)); 1033 } 1034 1035 int 1036 pw_user_unlock(int argc, char **argv, char *arg1) 1037 { 1038 int ch; 1039 1040 while ((ch = getopt(argc, argv, "Cq")) != -1) { 1041 switch (ch) { 1042 case 'C': 1043 case 'q': 1044 /* compatibility */ 1045 break; 1046 default: 1047 exit(EX_USAGE); 1048 } 1049 } 1050 1051 return (pw_userlock(arg1, M_UNLOCK)); 1052 } 1053 1054 static struct group * 1055 group_from_name_or_id(char *name) 1056 { 1057 const char *errstr = NULL; 1058 struct group *grp; 1059 uintmax_t id; 1060 1061 if ((grp = GETGRNAM(name)) == NULL) { 1062 id = strtounum(name, 0, GID_MAX, &errstr); 1063 if (errstr) 1064 errx(EX_NOUSER, "group `%s' does not exist", name); 1065 grp = GETGRGID(id); 1066 if (grp == NULL) 1067 errx(EX_NOUSER, "group `%s' does not exist", name); 1068 } 1069 1070 return (grp); 1071 } 1072 1073 static void 1074 split_groups(StringList **groups, char *groupsstr) 1075 { 1076 struct group *grp; 1077 char *p; 1078 char tok[] = ", \t"; 1079 1080 if (*groups == NULL) 1081 *groups = sl_init(); 1082 for (p = strtok(groupsstr, tok); p != NULL; p = strtok(NULL, tok)) { 1083 grp = group_from_name_or_id(p); 1084 sl_add(*groups, newstr(grp->gr_name)); 1085 } 1086 } 1087 1088 static void 1089 validate_grname(struct userconf *cnf, char *group) 1090 { 1091 struct group *grp; 1092 1093 if (group == NULL || *group == '\0') { 1094 cnf->default_group = ""; 1095 return; 1096 } 1097 grp = group_from_name_or_id(group); 1098 cnf->default_group = newstr(grp->gr_name); 1099 } 1100 1101 static mode_t 1102 validate_mode(char *mode) 1103 { 1104 mode_t m; 1105 void *set; 1106 1107 if ((set = setmode(mode)) == NULL) 1108 errx(EX_DATAERR, "invalid directory creation mode '%s'", mode); 1109 1110 m = getmode(set, _DEF_DIRMODE); 1111 free(set); 1112 return (m); 1113 } 1114 1115 static long 1116 validate_expire(char *str, int opt) 1117 { 1118 if (!numerics(str)) 1119 errx(EX_DATAERR, "-%c argument must be numeric " 1120 "when setting defaults: %s", (char)opt, str); 1121 return strtol(str, NULL, 0); 1122 } 1123 1124 static void 1125 mix_config(struct userconf *cmdcnf, struct userconf *cfg) 1126 { 1127 1128 if (cmdcnf->default_password < 0) 1129 cmdcnf->default_password = cfg->default_password; 1130 if (cmdcnf->reuse_uids == 0) 1131 cmdcnf->reuse_uids = cfg->reuse_uids; 1132 if (cmdcnf->reuse_gids == 0) 1133 cmdcnf->reuse_gids = cfg->reuse_gids; 1134 if (cmdcnf->nispasswd == NULL) 1135 cmdcnf->nispasswd = cfg->nispasswd; 1136 if (cmdcnf->dotdir == NULL) 1137 cmdcnf->dotdir = cfg->dotdir; 1138 if (cmdcnf->newmail == NULL) 1139 cmdcnf->newmail = cfg->newmail; 1140 if (cmdcnf->logfile == NULL) 1141 cmdcnf->logfile = cfg->logfile; 1142 if (cmdcnf->home == NULL) 1143 cmdcnf->home = cfg->home; 1144 if (cmdcnf->homemode == 0) 1145 cmdcnf->homemode = cfg->homemode; 1146 if (cmdcnf->shelldir == NULL) 1147 cmdcnf->shelldir = cfg->shelldir; 1148 if (cmdcnf->shells == NULL) 1149 cmdcnf->shells = cfg->shells; 1150 if (cmdcnf->shell_default == NULL) 1151 cmdcnf->shell_default = cfg->shell_default; 1152 if (cmdcnf->default_group == NULL) 1153 cmdcnf->default_group = cfg->default_group; 1154 if (cmdcnf->groups == NULL) 1155 cmdcnf->groups = cfg->groups; 1156 if (cmdcnf->default_class == NULL) 1157 cmdcnf->default_class = cfg->default_class; 1158 if (cmdcnf->min_uid == 0) 1159 cmdcnf->min_uid = cfg->min_uid; 1160 if (cmdcnf->max_uid == 0) 1161 cmdcnf->max_uid = cfg->max_uid; 1162 if (cmdcnf->min_gid == 0) 1163 cmdcnf->min_gid = cfg->min_gid; 1164 if (cmdcnf->max_gid == 0) 1165 cmdcnf->max_gid = cfg->max_gid; 1166 if (cmdcnf->expire_days < 0) 1167 cmdcnf->expire_days = cfg->expire_days; 1168 if (cmdcnf->password_days < 0) 1169 cmdcnf->password_days = cfg->password_days; 1170 } 1171 1172 int 1173 pw_user_add(int argc, char **argv, char *arg1) 1174 { 1175 struct userconf *cnf, *cmdcnf; 1176 struct passwd *pwd; 1177 struct group *grp; 1178 struct stat st; 1179 char args[] = "C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y"; 1180 char line[_PASSWORD_LEN+1], path[MAXPATHLEN]; 1181 char *gecos, *homedir, *skel, *walk, *userid, *groupid, *grname; 1182 char *default_passwd, *name, *p; 1183 const char *cfg = NULL; 1184 login_cap_t *lc; 1185 FILE *pfp, *fp; 1186 intmax_t id = -1; 1187 time_t now; 1188 int rc, ch, fd = -1; 1189 size_t i; 1190 bool dryrun, nis, pretty, quiet, createhome, precrypted, genconf; 1191 1192 dryrun = nis = pretty = quiet = createhome = precrypted = false; 1193 genconf = false; 1194 gecos = homedir = skel = userid = groupid = default_passwd = NULL; 1195 grname = name = NULL; 1196 1197 if ((cmdcnf = calloc(1, sizeof(struct userconf))) == NULL) 1198 err(EXIT_FAILURE, "calloc()"); 1199 1200 cmdcnf->default_password = cmdcnf->expire_days = cmdcnf->password_days = -1; 1201 now = time(NULL); 1202 1203 if (arg1 != NULL) { 1204 if (arg1[strspn(arg1, "0123456789")] == '\0') 1205 id = pw_checkid(arg1, UID_MAX); 1206 else 1207 name = pw_checkname(arg1, 0); 1208 } 1209 1210 while ((ch = getopt(argc, argv, args)) != -1) { 1211 switch (ch) { 1212 case 'C': 1213 cfg = optarg; 1214 break; 1215 case 'q': 1216 quiet = true; 1217 break; 1218 case 'n': 1219 name = pw_checkname(optarg, 0); 1220 break; 1221 case 'u': 1222 userid = optarg; 1223 break; 1224 case 'c': 1225 gecos = pw_checkname(optarg, 1); 1226 break; 1227 case 'd': 1228 homedir = optarg; 1229 break; 1230 case 'e': 1231 if (genconf) 1232 cmdcnf->expire_days = validate_expire(optarg, ch); 1233 else 1234 cmdcnf->expire_days = parse_date(now, optarg); 1235 break; 1236 case 'p': 1237 if (genconf) 1238 cmdcnf->password_days = validate_expire(optarg, ch); 1239 else 1240 cmdcnf->password_days = parse_date(now, optarg); 1241 break; 1242 case 'g': 1243 validate_grname(cmdcnf, optarg); 1244 grname = optarg; 1245 break; 1246 case 'G': 1247 split_groups(&cmdcnf->groups, optarg); 1248 break; 1249 case 'm': 1250 createhome = true; 1251 break; 1252 case 'M': 1253 cmdcnf->homemode = validate_mode(optarg); 1254 break; 1255 case 'k': 1256 walk = skel = optarg; 1257 if (*walk == '/') 1258 walk++; 1259 if (fstatat(conf.rootfd, walk, &st, 0) == -1) 1260 errx(EX_OSFILE, "skeleton `%s' does not " 1261 "exists", skel); 1262 if (!S_ISDIR(st.st_mode)) 1263 errx(EX_OSFILE, "skeleton `%s' is not a " 1264 "directory", skel); 1265 cmdcnf->dotdir = skel; 1266 break; 1267 case 's': 1268 cmdcnf->shell_default = optarg; 1269 break; 1270 case 'o': 1271 conf.checkduplicate = false; 1272 break; 1273 case 'L': 1274 cmdcnf->default_class = pw_checkname(optarg, 0); 1275 break; 1276 case 'i': 1277 groupid = optarg; 1278 break; 1279 case 'w': 1280 default_passwd = optarg; 1281 break; 1282 case 'H': 1283 if (fd != -1) 1284 errx(EX_USAGE, "'-h' and '-H' are mutually " 1285 "exclusive options"); 1286 fd = pw_checkfd(optarg); 1287 precrypted = true; 1288 if (fd == '-') 1289 errx(EX_USAGE, "-H expects a file descriptor"); 1290 break; 1291 case 'h': 1292 if (fd != -1) 1293 errx(EX_USAGE, "'-h' and '-H' are mutually " 1294 "exclusive options"); 1295 fd = pw_checkfd(optarg); 1296 break; 1297 case 'D': 1298 genconf = true; 1299 break; 1300 case 'b': 1301 cmdcnf->home = optarg; 1302 break; 1303 case 'N': 1304 dryrun = true; 1305 break; 1306 case 'P': 1307 pretty = true; 1308 break; 1309 case 'y': 1310 cmdcnf->nispasswd = optarg; 1311 break; 1312 case 'Y': 1313 nis = true; 1314 break; 1315 default: 1316 exit(EX_USAGE); 1317 } 1318 } 1319 1320 if (geteuid() != 0 && ! dryrun) 1321 errx(EX_NOPERM, "you must be root"); 1322 1323 if (quiet) 1324 freopen(_PATH_DEVNULL, "w", stderr); 1325 1326 cnf = get_userconfig(cfg); 1327 1328 mix_config(cmdcnf, cnf); 1329 if (default_passwd) 1330 cmdcnf->default_password = passwd_val(default_passwd, 1331 cnf->default_password); 1332 if (genconf) { 1333 if (name != NULL) 1334 errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 1335 if (userid != NULL) { 1336 if ((p = strtok(userid, ", \t")) != NULL) 1337 cmdcnf->min_uid = pw_checkid(p, UID_MAX); 1338 if (cmdcnf->min_uid == 0) 1339 cmdcnf->min_uid = 1000; 1340 if ((p = strtok(NULL, " ,\t")) != NULL) 1341 cmdcnf->max_uid = pw_checkid(p, UID_MAX); 1342 if (cmdcnf->max_uid == 0) 1343 cmdcnf->max_uid = 32000; 1344 } 1345 if (groupid != NULL) { 1346 if ((p = strtok(groupid, ", \t")) != NULL) 1347 cmdcnf->min_gid = pw_checkid(p, GID_MAX); 1348 if (cmdcnf->min_gid == 0) 1349 cmdcnf->min_gid = 1000; 1350 if ((p = strtok(NULL, " ,\t")) != NULL) 1351 cmdcnf->max_gid = pw_checkid(p, GID_MAX); 1352 if (cmdcnf->max_gid == 0) 1353 cmdcnf->max_gid = 32000; 1354 } 1355 if (write_userconfig(cmdcnf, cfg)) 1356 return (EXIT_SUCCESS); 1357 err(EX_IOERR, "config update"); 1358 } 1359 1360 if (userid) 1361 id = pw_checkid(userid, UID_MAX); 1362 if (id < 0 && name == NULL) 1363 errx(EX_DATAERR, "user name or id required"); 1364 1365 if (name == NULL) 1366 errx(EX_DATAERR, "login name required"); 1367 1368 if (GETPWNAM(name) != NULL) 1369 errx(EX_DATAERR, "login name `%s' already exists", name); 1370 1371 if (!grname) 1372 grname = cmdcnf->default_group; 1373 1374 pwd = &fakeuser; 1375 pwd->pw_name = name; 1376 pwd->pw_class = cmdcnf->default_class ? cmdcnf->default_class : ""; 1377 pwd->pw_uid = pw_uidpolicy(cmdcnf, id); 1378 pwd->pw_gid = pw_gidpolicy(cnf, grname, pwd->pw_name, 1379 (gid_t) pwd->pw_uid, dryrun); 1380 1381 /* cmdcnf->password_days and cmdcnf->expire_days hold unixtime here */ 1382 if (cmdcnf->password_days > 0) 1383 pwd->pw_change = cmdcnf->password_days; 1384 if (cmdcnf->expire_days > 0) 1385 pwd->pw_expire = cmdcnf->expire_days; 1386 1387 pwd->pw_dir = pw_homepolicy(cmdcnf, homedir, pwd->pw_name); 1388 pwd->pw_shell = pw_shellpolicy(cmdcnf); 1389 lc = login_getpwclass(pwd); 1390 if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 1391 warn("setting crypt(3) format"); 1392 login_close(lc); 1393 pwd->pw_passwd = pw_password(cmdcnf, pwd->pw_name); 1394 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 1395 warnx("WARNING: new account `%s' has a uid of 0 " 1396 "(superuser access!)", pwd->pw_name); 1397 if (gecos) 1398 pwd->pw_gecos = gecos; 1399 1400 if (fd != -1) 1401 pw_set_passwd(pwd, fd, precrypted, false); 1402 1403 if (dryrun) 1404 return (print_user(pwd, pretty, false)); 1405 1406 if ((rc = addpwent(pwd)) != 0) { 1407 if (rc == -1) 1408 errx(EX_IOERR, "user '%s' already exists", 1409 pwd->pw_name); 1410 else if (rc != 0) 1411 err(EX_IOERR, "passwd file update"); 1412 } 1413 if (nis && cmdcnf->nispasswd && *cmdcnf->nispasswd == '/') { 1414 printf("%s\n", cmdcnf->nispasswd); 1415 rc = addnispwent(cmdcnf->nispasswd, pwd); 1416 if (rc == -1) 1417 warnx("User '%s' already exists in NIS passwd", 1418 pwd->pw_name); 1419 else if (rc != 0) 1420 warn("NIS passwd update"); 1421 /* NOTE: we treat NIS-only update errors as non-fatal */ 1422 } 1423 1424 if (cmdcnf->groups != NULL) { 1425 for (i = 0; i < cmdcnf->groups->sl_cur; i++) { 1426 grp = GETGRNAM(cmdcnf->groups->sl_str[i]); 1427 grp = gr_add(grp, pwd->pw_name); 1428 /* 1429 * grp can only be NULL in 2 cases: 1430 * - the new member is already a member 1431 * - a problem with memory occurs 1432 * in both cases we want to skip now. 1433 */ 1434 if (grp == NULL) 1435 continue; 1436 chggrent(grp->gr_name, grp); 1437 free(grp); 1438 } 1439 } 1440 1441 pwd = GETPWNAM(name); 1442 if (pwd == NULL) 1443 errx(EX_NOUSER, "user '%s' disappeared during update", name); 1444 1445 grp = GETGRGID(pwd->pw_gid); 1446 pw_log(cnf, M_ADD, W_USER, "%s(%ju):%s(%ju):%s:%s:%s", 1447 pwd->pw_name, (uintmax_t)pwd->pw_uid, 1448 grp ? grp->gr_name : "unknown", 1449 (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1), 1450 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 1451 1452 /* 1453 * let's touch and chown the user's mail file. This is not 1454 * strictly necessary under BSD with a 0755 maildir but it also 1455 * doesn't hurt anything to create the empty mailfile 1456 */ 1457 if (PWALTDIR() != PWF_ALT) { 1458 snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, 1459 pwd->pw_name); 1460 /* Preserve contents & mtime */ 1461 close(openat(conf.rootfd, path +1, O_RDWR | O_CREAT, 0600)); 1462 fchownat(conf.rootfd, path + 1, pwd->pw_uid, pwd->pw_gid, 1463 AT_SYMLINK_NOFOLLOW); 1464 } 1465 1466 /* 1467 * Let's create and populate the user's home directory. Note 1468 * that this also `works' for editing users if -m is used, but 1469 * existing files will *not* be overwritten. 1470 */ 1471 if (PWALTDIR() != PWF_ALT && createhome && pwd->pw_dir && 1472 *pwd->pw_dir == '/' && pwd->pw_dir[1]) 1473 create_and_populate_homedir(cmdcnf, pwd, cmdcnf->dotdir, 1474 cmdcnf->homemode, false); 1475 1476 if (!PWALTDIR() && cmdcnf->newmail && *cmdcnf->newmail && 1477 (fp = fopen(cnf->newmail, "r")) != NULL) { 1478 if ((pfp = popen(_PATH_SENDMAIL " -t", "w")) == NULL) 1479 warn("sendmail"); 1480 else { 1481 fprintf(pfp, "From: root\n" "To: %s\n" 1482 "Subject: Welcome!\n\n", pwd->pw_name); 1483 while (fgets(line, sizeof(line), fp) != NULL) { 1484 /* Do substitutions? */ 1485 fputs(line, pfp); 1486 } 1487 pclose(pfp); 1488 pw_log(cnf, M_ADD, W_USER, "%s(%ju) new user mail sent", 1489 pwd->pw_name, (uintmax_t)pwd->pw_uid); 1490 } 1491 fclose(fp); 1492 } 1493 1494 if (nis && nis_update() == 0) 1495 pw_log(cnf, M_ADD, W_USER, "NIS maps updated"); 1496 1497 return (EXIT_SUCCESS); 1498 } 1499 1500 int 1501 pw_user_mod(int argc, char **argv, char *arg1) 1502 { 1503 struct userconf *cnf; 1504 struct passwd *pwd; 1505 struct group *grp; 1506 StringList *groups = NULL; 1507 char args[] = "C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:NPYy:"; 1508 const char *cfg = NULL; 1509 char *gecos, *homedir, *grname, *name, *newname, *walk, *skel, *shell; 1510 char *passwd, *class, *nispasswd; 1511 login_cap_t *lc; 1512 struct stat st; 1513 intmax_t id = -1; 1514 int ch, fd = -1; 1515 size_t i, j; 1516 bool quiet, createhome, pretty, dryrun, nis, edited; 1517 bool precrypted; 1518 mode_t homemode = 0; 1519 time_t expire_time, password_time, now; 1520 1521 expire_time = password_time = -1; 1522 gecos = homedir = grname = name = newname = skel = shell =NULL; 1523 passwd = NULL; 1524 class = nispasswd = NULL; 1525 quiet = createhome = pretty = dryrun = nis = precrypted = false; 1526 edited = false; 1527 now = time(NULL); 1528 1529 if (arg1 != NULL) { 1530 if (arg1[strspn(arg1, "0123456789")] == '\0') 1531 id = pw_checkid(arg1, UID_MAX); 1532 else 1533 name = arg1; 1534 } 1535 1536 while ((ch = getopt(argc, argv, args)) != -1) { 1537 switch (ch) { 1538 case 'C': 1539 cfg = optarg; 1540 break; 1541 case 'q': 1542 quiet = true; 1543 break; 1544 case 'n': 1545 name = optarg; 1546 break; 1547 case 'u': 1548 id = pw_checkid(optarg, UID_MAX); 1549 break; 1550 case 'c': 1551 gecos = pw_checkname(optarg, 1); 1552 break; 1553 case 'd': 1554 homedir = optarg; 1555 break; 1556 case 'e': 1557 expire_time = parse_date(now, optarg); 1558 break; 1559 case 'p': 1560 password_time = parse_date(now, optarg); 1561 break; 1562 case 'g': 1563 group_from_name_or_id(optarg); 1564 grname = optarg; 1565 break; 1566 case 'G': 1567 split_groups(&groups, optarg); 1568 break; 1569 case 'm': 1570 createhome = true; 1571 break; 1572 case 'M': 1573 homemode = validate_mode(optarg); 1574 break; 1575 case 'l': 1576 newname = optarg; 1577 break; 1578 case 'k': 1579 walk = skel = optarg; 1580 if (*walk == '/') 1581 walk++; 1582 if (fstatat(conf.rootfd, walk, &st, 0) == -1) 1583 errx(EX_OSFILE, "skeleton `%s' does not " 1584 "exists", skel); 1585 if (!S_ISDIR(st.st_mode)) 1586 errx(EX_OSFILE, "skeleton `%s' is not a " 1587 "directory", skel); 1588 break; 1589 case 's': 1590 shell = optarg; 1591 break; 1592 case 'w': 1593 passwd = optarg; 1594 break; 1595 case 'L': 1596 class = pw_checkname(optarg, 0); 1597 break; 1598 case 'H': 1599 if (fd != -1) 1600 errx(EX_USAGE, "'-h' and '-H' are mutually " 1601 "exclusive options"); 1602 fd = pw_checkfd(optarg); 1603 precrypted = true; 1604 if (fd == '-') 1605 errx(EX_USAGE, "-H expects a file descriptor"); 1606 break; 1607 case 'h': 1608 if (fd != -1) 1609 errx(EX_USAGE, "'-h' and '-H' are mutually " 1610 "exclusive options"); 1611 fd = pw_checkfd(optarg); 1612 break; 1613 case 'N': 1614 dryrun = true; 1615 break; 1616 case 'P': 1617 pretty = true; 1618 break; 1619 case 'y': 1620 nispasswd = optarg; 1621 break; 1622 case 'Y': 1623 nis = true; 1624 break; 1625 default: 1626 exit(EX_USAGE); 1627 } 1628 } 1629 1630 if (geteuid() != 0 && ! dryrun) 1631 errx(EX_NOPERM, "you must be root"); 1632 1633 if (quiet) 1634 freopen(_PATH_DEVNULL, "w", stderr); 1635 1636 cnf = get_userconfig(cfg); 1637 1638 if (id < 0 && name == NULL) 1639 errx(EX_DATAERR, "username or id required"); 1640 1641 pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 1642 if (pwd == NULL) { 1643 if (name == NULL) 1644 errx(EX_NOUSER, "no such uid `%ju'", 1645 (uintmax_t) id); 1646 errx(EX_NOUSER, "no such user `%s'", name); 1647 } 1648 1649 if (name == NULL) 1650 name = pwd->pw_name; 1651 1652 if (nis && nispasswd == NULL) 1653 nispasswd = cnf->nispasswd; 1654 1655 if (PWF._altdir == PWF_REGULAR && 1656 ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) { 1657 if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 1658 if (!nis && nispasswd && *nispasswd != '/') 1659 errx(EX_NOUSER, "Cannot modify NIS user `%s'", 1660 name); 1661 } else { 1662 errx(EX_NOUSER, "Cannot modify non local user `%s'", 1663 name); 1664 } 1665 } 1666 1667 if (newname) { 1668 if (strcmp(pwd->pw_name, "root") == 0) 1669 errx(EX_DATAERR, "can't rename `root' account"); 1670 if (strcmp(pwd->pw_name, newname) != 0) { 1671 pwd->pw_name = pw_checkname(newname, 0); 1672 edited = true; 1673 } 1674 } 1675 1676 if (id >= 0 && pwd->pw_uid != id) { 1677 pwd->pw_uid = id; 1678 edited = true; 1679 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 1680 errx(EX_DATAERR, "can't change uid of `root' account"); 1681 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 1682 warnx("WARNING: account `%s' will have a uid of 0 " 1683 "(superuser access!)", pwd->pw_name); 1684 } 1685 1686 if (grname && pwd->pw_uid != 0) { 1687 grp = GETGRNAM(grname); 1688 if (grp == NULL) 1689 grp = GETGRGID(pw_checkid(grname, GID_MAX)); 1690 if (grp->gr_gid != pwd->pw_gid) { 1691 pwd->pw_gid = grp->gr_gid; 1692 edited = true; 1693 } 1694 } 1695 1696 1697 if (password_time >= 0 && pwd->pw_change != password_time) { 1698 pwd->pw_change = password_time; 1699 edited = true; 1700 } 1701 1702 if (expire_time >= 0 && pwd->pw_expire != expire_time) { 1703 pwd->pw_expire = expire_time; 1704 edited = true; 1705 } 1706 1707 if (shell) { 1708 shell = shell_path(cnf->shelldir, cnf->shells, shell); 1709 if (shell == NULL) 1710 shell = ""; 1711 if (strcmp(shell, pwd->pw_shell) != 0) { 1712 pwd->pw_shell = shell; 1713 edited = true; 1714 } 1715 } 1716 1717 if (class && strcmp(pwd->pw_class, class) != 0) { 1718 pwd->pw_class = class; 1719 edited = true; 1720 } 1721 1722 if (homedir && strcmp(pwd->pw_dir, homedir) != 0) { 1723 pwd->pw_dir = homedir; 1724 edited = true; 1725 if (fstatat(conf.rootfd, pwd->pw_dir, &st, 0) == -1) { 1726 if (!createhome) 1727 warnx("WARNING: home `%s' does not exist", 1728 pwd->pw_dir); 1729 } else if (!S_ISDIR(st.st_mode)) { 1730 warnx("WARNING: home `%s' is not a directory", 1731 pwd->pw_dir); 1732 } 1733 } 1734 1735 if (passwd && conf.fd == -1) { 1736 lc = login_getpwclass(pwd); 1737 if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 1738 warn("setting crypt(3) format"); 1739 login_close(lc); 1740 cnf->default_password = passwd_val(passwd, 1741 cnf->default_password); 1742 pwd->pw_passwd = pw_password(cnf, pwd->pw_name); 1743 edited = true; 1744 } 1745 1746 if (gecos && strcmp(pwd->pw_gecos, gecos) != 0) { 1747 pwd->pw_gecos = gecos; 1748 edited = true; 1749 } 1750 1751 if (fd != -1) 1752 edited = pw_set_passwd(pwd, fd, precrypted, true); 1753 1754 if (dryrun) 1755 return (print_user(pwd, pretty, false)); 1756 1757 if (edited) /* Only updated this if required */ 1758 perform_chgpwent(name, pwd, nis ? nispasswd : NULL); 1759 /* Now perform the needed changes concern groups */ 1760 if (groups != NULL) { 1761 /* Delete User from groups using old name */ 1762 SETGRENT(); 1763 while ((grp = GETGRENT()) != NULL) { 1764 if (grp->gr_mem == NULL) 1765 continue; 1766 for (i = 0; grp->gr_mem[i] != NULL; i++) { 1767 if (strcmp(grp->gr_mem[i] , name) != 0) 1768 continue; 1769 for (j = i; grp->gr_mem[j] != NULL ; j++) 1770 grp->gr_mem[j] = grp->gr_mem[j+1]; 1771 chggrent(grp->gr_name, grp); 1772 break; 1773 } 1774 } 1775 ENDGRENT(); 1776 /* Add the user to the needed groups */ 1777 for (i = 0; i < groups->sl_cur; i++) { 1778 grp = GETGRNAM(groups->sl_str[i]); 1779 grp = gr_add(grp, pwd->pw_name); 1780 if (grp == NULL) 1781 continue; 1782 chggrent(grp->gr_name, grp); 1783 free(grp); 1784 } 1785 } 1786 /* In case of rename we need to walk over the different groups */ 1787 if (newname) { 1788 SETGRENT(); 1789 while ((grp = GETGRENT()) != NULL) { 1790 if (grp->gr_mem == NULL) 1791 continue; 1792 for (i = 0; grp->gr_mem[i] != NULL; i++) { 1793 if (strcmp(grp->gr_mem[i], name) != 0) 1794 continue; 1795 grp->gr_mem[i] = newname; 1796 chggrent(grp->gr_name, grp); 1797 break; 1798 } 1799 } 1800 } 1801 1802 /* go get a current version of pwd */ 1803 if (newname) 1804 name = newname; 1805 pwd = GETPWNAM(name); 1806 if (pwd == NULL) 1807 errx(EX_NOUSER, "user '%s' disappeared during update", name); 1808 grp = GETGRGID(pwd->pw_gid); 1809 pw_log(cnf, M_UPDATE, W_USER, "%s(%ju):%s(%ju):%s:%s:%s", 1810 pwd->pw_name, (uintmax_t)pwd->pw_uid, 1811 grp ? grp->gr_name : "unknown", 1812 (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1), 1813 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 1814 1815 /* 1816 * Let's create and populate the user's home directory. Note 1817 * that this also `works' for editing users if -m is used, but 1818 * existing files will *not* be overwritten. 1819 */ 1820 if (PWALTDIR() != PWF_ALT && createhome && pwd->pw_dir && 1821 *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 1822 if (!skel) 1823 skel = cnf->dotdir; 1824 if (homemode == 0) 1825 homemode = cnf->homemode; 1826 create_and_populate_homedir(cnf, pwd, skel, homemode, true); 1827 } 1828 1829 if (nis && nis_update() == 0) 1830 pw_log(cnf, M_UPDATE, W_USER, "NIS maps updated"); 1831 1832 return (EXIT_SUCCESS); 1833 } 1834