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