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