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