1 /* $NetBSD: user.c,v 1.120 2008/07/21 13:37:00 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1999 Alistair G. Crooks. All rights reserved. 5 * Copyright (c) 2005 Liam J. Foy. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote 16 * products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #include <sys/cdefs.h> 32 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1999\ 35 The NetBSD Foundation, Inc. All rights reserved."); 36 __RCSID("$NetBSD: user.c,v 1.120 2008/07/21 13:37:00 lukem Exp $"); 37 #endif 38 39 #include <sys/types.h> 40 #include <sys/param.h> 41 #include <sys/stat.h> 42 43 #include <ctype.h> 44 #include <dirent.h> 45 #include <err.h> 46 #include <fcntl.h> 47 #include <grp.h> 48 #ifdef EXTENSIONS 49 #include <login_cap.h> 50 #endif 51 #include <paths.h> 52 #include <pwd.h> 53 #include <regex.h> 54 #include <stdarg.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <syslog.h> 59 #include <time.h> 60 #include <unistd.h> 61 #include <util.h> 62 #include <errno.h> 63 64 #include "defs.h" 65 #include "usermgmt.h" 66 67 68 /* this struct describes a uid range */ 69 typedef struct range_t { 70 int r_from; /* low uid */ 71 int r_to; /* high uid */ 72 } range_t; 73 74 /* this struct encapsulates the user information */ 75 typedef struct user_t { 76 int u_flags; /* see below */ 77 int u_uid; /* uid of user */ 78 char *u_password; /* encrypted password */ 79 char *u_comment; /* comment field */ 80 char *u_home; /* home directory */ 81 mode_t u_homeperm; /* permissions of home dir */ 82 char *u_primgrp; /* primary group */ 83 int u_groupc; /* # of secondary groups */ 84 const char *u_groupv[NGROUPS_MAX]; /* secondary groups */ 85 char *u_shell; /* user's shell */ 86 char *u_basedir; /* base directory for home */ 87 char *u_expire; /* when password will expire */ 88 char *u_inactive; /* when account will expire */ 89 char *u_skeldir; /* directory for startup files */ 90 char *u_class; /* login class */ 91 unsigned u_rsize; /* size of range array */ 92 unsigned u_rc; /* # of ranges */ 93 range_t *u_rv; /* the ranges */ 94 unsigned u_defrc; /* # of ranges in defaults */ 95 int u_preserve; /* preserve uids on deletion */ 96 int u_allow_samba; /* allow trailing '$' for samba login names */ 97 int u_locked; /* user account lock */ 98 } user_t; 99 100 /* flags for which fields of the user_t replace the passwd entry */ 101 enum { 102 F_COMMENT = 0x0001, 103 F_DUPUID = 0x0002, 104 F_EXPIRE = 0x0004, 105 F_GROUP = 0x0008, 106 F_HOMEDIR = 0x0010, 107 F_MKDIR = 0x0020, 108 F_INACTIVE = 0x0040, 109 F_PASSWORD = 0x0080, 110 F_SECGROUP = 0x0100, 111 F_SHELL = 0x0200, 112 F_UID = 0x0400, 113 F_USERNAME = 0x0800, 114 F_CLASS = 0x1000 115 }; 116 117 #define UNLOCK 0 118 #define LOCK 1 119 #define LOCKED "*LOCKED*" 120 121 #define PATH_LOGINCONF "/etc/login.conf" 122 123 #ifndef DEF_GROUP 124 #define DEF_GROUP "users" 125 #endif 126 127 #ifndef DEF_BASEDIR 128 #define DEF_BASEDIR "/home" 129 #endif 130 131 #ifndef DEF_SKELDIR 132 #define DEF_SKELDIR "/etc/skel" 133 #endif 134 135 #ifndef DEF_SHELL 136 #define DEF_SHELL _PATH_BSHELL 137 #endif 138 139 #ifndef DEF_COMMENT 140 #define DEF_COMMENT "" 141 #endif 142 143 #ifndef DEF_LOWUID 144 #define DEF_LOWUID 1000 145 #endif 146 147 #ifndef DEF_HIGHUID 148 #define DEF_HIGHUID 60000 149 #endif 150 151 #ifndef DEF_INACTIVE 152 #define DEF_INACTIVE 0 153 #endif 154 155 #ifndef DEF_EXPIRE 156 #define DEF_EXPIRE NULL 157 #endif 158 159 #ifndef DEF_CLASS 160 #define DEF_CLASS "" 161 #endif 162 163 #ifndef WAITSECS 164 #define WAITSECS 10 165 #endif 166 167 #ifndef NOBODY_UID 168 #define NOBODY_UID 32767 169 #endif 170 171 #ifndef DEF_HOMEPERM 172 #define DEF_HOMEPERM 0755 173 #endif 174 175 /* some useful constants */ 176 enum { 177 MaxShellNameLen = 256, 178 MaxFileNameLen = MAXPATHLEN, 179 MaxUserNameLen = LOGIN_NAME_MAX - 1, 180 MaxCommandLen = 2048, 181 MaxEntryLen = 2048, 182 PasswordLength = 2048, 183 184 DES_Len = 13, 185 186 LowGid = DEF_LOWUID, 187 HighGid = DEF_HIGHUID 188 }; 189 190 /* Full paths of programs used here */ 191 #define CHMOD "/bin/chmod" 192 #define CHOWN "/usr/sbin/chown" 193 #define MKDIR "/bin/mkdir" 194 #define MV "/bin/mv" 195 #define NOLOGIN "/sbin/nologin" 196 #define PAX "/bin/pax" 197 #define RM "/bin/rm" 198 199 #define UNSET_INACTIVE "Null (unset)" 200 #define UNSET_EXPIRY "Null (unset)" 201 202 static int asystem(const char *fmt, ...) 203 __attribute__((__format__(__printf__, 1, 2))); 204 static int is_number(const char *); 205 static struct group *find_group_info(const char *); 206 static int verbose; 207 208 static char * 209 skipspace(char *s) 210 { 211 for (; *s && isspace((unsigned char)*s) ; s++) { 212 } 213 return s; 214 } 215 216 static int 217 check_numeric(const char *val, const char *name) 218 { 219 if (!is_number(val)) { 220 errx(EXIT_FAILURE, "When using [-%c %s], " 221 "the %s must be numeric", *name, name, name); 222 } 223 return atoi(val); 224 } 225 226 /* resize *cpp appropriately then assign `n' chars of `s' to it */ 227 static void 228 memsave(char **cpp, const char *s, size_t n) 229 { 230 RENEW(char, *cpp, n + 1, exit(1)); 231 (void)memcpy(*cpp, s, n); 232 (*cpp)[n] = '\0'; 233 } 234 235 /* a replacement for system(3) */ 236 static int 237 asystem(const char *fmt, ...) 238 { 239 va_list vp; 240 char buf[MaxCommandLen]; 241 int ret; 242 243 va_start(vp, fmt); 244 (void)vsnprintf(buf, sizeof(buf), fmt, vp); 245 va_end(vp); 246 if (verbose) { 247 (void)printf("Command: %s\n", buf); 248 } 249 if ((ret = system(buf)) != 0) { 250 warn("Error running `%s'", buf); 251 } 252 return ret; 253 } 254 255 /* remove a users home directory, returning 1 for success (ie, no problems encountered) */ 256 static int 257 removehomedir(struct passwd *pwp) 258 { 259 struct stat st; 260 261 /* userid not root? */ 262 if (pwp->pw_uid == 0) { 263 warnx("Not deleting home directory `%s'; userid is 0", pwp->pw_dir); 264 return 0; 265 } 266 267 /* directory exists (and is a directory!) */ 268 if (stat(pwp->pw_dir, &st) < 0) { 269 warn("Cannot access home directory `%s'", pwp->pw_dir); 270 return 0; 271 } 272 if (!S_ISDIR(st.st_mode)) { 273 warnx("Home directory `%s' is not a directory", pwp->pw_dir); 274 return 0; 275 } 276 277 /* userid matches directory owner? */ 278 if (st.st_uid != pwp->pw_uid) { 279 warnx("User `%s' doesn't own directory `%s', not removed", 280 pwp->pw_name, pwp->pw_dir); 281 return 0; 282 } 283 284 (void)seteuid(pwp->pw_uid); 285 /* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */ 286 (void)asystem("%s -rf %s > /dev/null 2>&1 || true", RM, pwp->pw_dir); 287 (void)seteuid(0); 288 if (rmdir(pwp->pw_dir) < 0) { 289 warn("Unable to remove all files in `%s'", pwp->pw_dir); 290 return 0; 291 } 292 return 1; 293 } 294 295 /* return 1 if all of `s' is numeric */ 296 static int 297 is_number(const char *s) 298 { 299 for ( ; *s ; s++) { 300 if (!isdigit((unsigned char) *s)) { 301 return 0; 302 } 303 } 304 return 1; 305 } 306 307 /* 308 * check that the effective uid is 0 - called from funcs which will 309 * modify data and config files. 310 */ 311 static void 312 checkeuid(void) 313 { 314 if (geteuid() != 0) { 315 errx(EXIT_FAILURE, "Program must be run as root"); 316 } 317 } 318 319 /* copy any dot files into the user's home directory */ 320 static int 321 copydotfiles(char *skeldir, int uid, int gid, char *dir, mode_t homeperm) 322 { 323 struct dirent *dp; 324 DIR *dirp; 325 int n; 326 327 if ((dirp = opendir(skeldir)) == NULL) { 328 warn("Can't open source . files dir `%s'", skeldir); 329 return 0; 330 } 331 for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) { 332 if (strcmp(dp->d_name, ".") == 0 || 333 strcmp(dp->d_name, "..") == 0) { 334 continue; 335 } 336 n = 1; 337 } 338 (void)closedir(dirp); 339 if (n == 0) { 340 warnx("No \"dot\" initialisation files found"); 341 } else { 342 (void)asystem("cd %s && %s -rw -pe %s . %s", 343 skeldir, PAX, (verbose) ? "-v" : "", dir); 344 } 345 (void)asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir); 346 (void)asystem("%s -R u+w %s", CHMOD, dir); 347 #ifdef EXTENSIONS 348 (void)asystem("%s 0%o %s", CHMOD, homeperm, dir); 349 #endif 350 return n; 351 } 352 353 /* create a group entry with gid `gid' */ 354 static int 355 creategid(char *group, int gid, const char *name) 356 { 357 struct stat st; 358 FILE *from; 359 FILE *to; 360 char buf[MaxEntryLen]; 361 char f[MaxFileNameLen]; 362 int fd; 363 int cc; 364 365 if (getgrnam(group) != NULL) { 366 warnx("Can't create group `%s': already exists", group); 367 return 0; 368 } 369 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 370 warn("Can't create group `%s': can't open `%s'", name, 371 _PATH_GROUP); 372 return 0; 373 } 374 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 375 warn("Can't lock `%s'", _PATH_GROUP); 376 (void)fclose(from); 377 return 0; 378 } 379 (void)fstat(fileno(from), &st); 380 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 381 if ((fd = mkstemp(f)) < 0) { 382 warn("Can't create group `%s': mkstemp failed", group); 383 (void)fclose(from); 384 return 0; 385 } 386 if ((to = fdopen(fd, "w")) == NULL) { 387 warn("Can't create group `%s': fdopen `%s' failed", 388 group, f); 389 (void)fclose(from); 390 (void)close(fd); 391 (void)unlink(f); 392 return 0; 393 } 394 while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) { 395 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 396 warn("Can't create group `%s': short write to `%s'", 397 group, f); 398 (void)fclose(from); 399 (void)close(fd); 400 (void)unlink(f); 401 return 0; 402 } 403 } 404 (void)fprintf(to, "%s:*:%d:%s\n", group, gid, name); 405 (void)fclose(from); 406 (void)fclose(to); 407 if (rename(f, _PATH_GROUP) < 0) { 408 warn("Can't create group `%s': can't rename `%s' to `%s'", 409 group, f, _PATH_GROUP); 410 (void)unlink(f); 411 return 0; 412 } 413 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 414 syslog(LOG_INFO, "New group added: name=%s, gid=%d", group, gid); 415 return 1; 416 } 417 418 /* modify the group entry with name `group' to be newent */ 419 static int 420 modify_gid(char *group, char *newent) 421 { 422 struct stat st; 423 FILE *from; 424 FILE *to; 425 char buf[MaxEntryLen]; 426 char f[MaxFileNameLen]; 427 char *colon; 428 int groupc; 429 int entc; 430 int fd; 431 int cc; 432 433 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 434 warn("Can't modify group `%s': can't open `%s'", 435 group, _PATH_GROUP); 436 return 0; 437 } 438 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 439 warn("Can't modify group `%s': can't lock `%s'", 440 group, _PATH_GROUP); 441 (void)fclose(from); 442 return 0; 443 } 444 (void)fstat(fileno(from), &st); 445 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 446 if ((fd = mkstemp(f)) < 0) { 447 warn("Can't modify group `%s': mkstemp failed", group); 448 (void)fclose(from); 449 return 0; 450 } 451 if ((to = fdopen(fd, "w")) == NULL) { 452 warn("Can't modify group `%s': fdopen `%s' failed", group, f); 453 (void)fclose(from); 454 (void)close(fd); 455 (void)unlink(f); 456 return 0; 457 } 458 groupc = strlen(group); 459 while (fgets(buf, sizeof(buf), from) != NULL) { 460 cc = strlen(buf); 461 if ((colon = strchr(buf, ':')) == NULL) { 462 warnx("Badly formed entry `%s'", buf); 463 continue; 464 } 465 entc = (int)(colon - buf); 466 if (entc == groupc && 467 strncmp(group, buf, (unsigned) entc) == 0) { 468 if (newent == NULL) { 469 struct group *grp_rm; 470 struct passwd *user_pwd; 471 472 /* 473 * Check that the group being removed 474 * isn't any user's Primary group. Just 475 * warn if it is. This could cause problems 476 * if the group GID was reused for a 477 * different purpose. 478 */ 479 480 grp_rm = find_group_info(group); 481 while ((user_pwd = getpwent()) != NULL) { 482 if (user_pwd->pw_gid == grp_rm->gr_gid) { 483 warnx("Warning: group `%s'(%d)" 484 " is the primary group of" 485 " `%s'. Use caution if you" 486 " later add this GID.", 487 grp_rm->gr_name, 488 grp_rm->gr_gid, user_pwd->pw_name); 489 } 490 } 491 endpwent(); 492 continue; 493 } else { 494 cc = strlen(newent); 495 (void)strlcpy(buf, newent, sizeof(buf)); 496 } 497 } 498 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 499 warn("Can't modify group `%s': short write to `%s'", 500 group, f); 501 (void)fclose(from); 502 (void)close(fd); 503 (void)unlink(f); 504 return 0; 505 } 506 } 507 (void)fclose(from); 508 (void)fclose(to); 509 if (rename(f, _PATH_GROUP) < 0) { 510 warn("Can't modify group `%s': can't rename `%s' to `%s'", 511 group, f, _PATH_GROUP); 512 (void)unlink(f); 513 return 0; 514 } 515 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 516 if (newent == NULL) { 517 syslog(LOG_INFO, "group deleted: name=%s", group); 518 } else { 519 syslog(LOG_INFO, "group information modified: name=%s", group); 520 } 521 return 1; 522 } 523 524 /* modify the group entries for all `groups', by adding `user' */ 525 static int 526 append_group(char *user, int ngroups, const char **groups) 527 { 528 struct group *grp; 529 struct stat st; 530 FILE *from; 531 FILE *to; 532 char buf[MaxEntryLen]; 533 char f[MaxFileNameLen]; 534 char *colon; 535 int groupc; 536 int entc; 537 int fd; 538 int nc; 539 int cc; 540 int i; 541 int j; 542 543 for (i = 0 ; i < ngroups ; i++) { 544 if ((grp = getgrnam(groups[i])) == NULL) { 545 warnx("Can't append group `%s' for user `%s'", 546 groups[i], user); 547 } else { 548 for (j = 0 ; grp->gr_mem[j] ; j++) { 549 if (strcmp(user, grp->gr_mem[j]) == 0) { 550 /* already in it */ 551 groups[i] = ""; 552 } 553 } 554 } 555 } 556 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 557 warn("Can't append group(s) for `%s': can't open `%s'", 558 user, _PATH_GROUP); 559 return 0; 560 } 561 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 562 warn("Can't append group(s) for `%s': can't lock `%s'", 563 user, _PATH_GROUP); 564 (void)fclose(from); 565 return 0; 566 } 567 (void)fstat(fileno(from), &st); 568 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 569 if ((fd = mkstemp(f)) < 0) { 570 warn("Can't append group(s) for `%s': mkstemp failed", 571 user); 572 (void)fclose(from); 573 return 0; 574 } 575 if ((to = fdopen(fd, "w")) == NULL) { 576 warn("Can't append group(s) for `%s': fdopen `%s' failed", 577 user, f); 578 (void)fclose(from); 579 (void)close(fd); 580 (void)unlink(f); 581 return 0; 582 } 583 while (fgets(buf, sizeof(buf), from) != NULL) { 584 cc = strlen(buf); 585 if ((colon = strchr(buf, ':')) == NULL) { 586 warnx("Badly formed entry `%s'", buf); 587 continue; 588 } 589 entc = (int)(colon - buf); 590 for (i = 0 ; i < ngroups ; i++) { 591 if ((groupc = strlen(groups[i])) == 0) { 592 continue; 593 } 594 if (entc == groupc && 595 strncmp(groups[i], buf, (unsigned) entc) == 0) { 596 if ((nc = snprintf(&buf[cc - 1], 597 sizeof(buf) - cc + 1, "%s%s\n", 598 (buf[cc - 2] == ':') ? "" : ",", user)) < 0) { 599 warnx("Warning: group `%s' " 600 "entry too long", groups[i]); 601 } 602 cc += nc - 1; 603 } 604 } 605 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 606 warn("Can't append group(s) for `%s':" 607 " short write to `%s'", user, f); 608 (void)fclose(from); 609 (void)close(fd); 610 (void)unlink(f); 611 return 0; 612 } 613 } 614 (void)fclose(from); 615 (void)fclose(to); 616 if (rename(f, _PATH_GROUP) < 0) { 617 warn("Can't append group(s) for `%s': " 618 "can't rename `%s' to `%s'", user, f, _PATH_GROUP); 619 (void)unlink(f); 620 return 0; 621 } 622 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 623 return 1; 624 } 625 626 /* the valid characters for login and group names */ 627 #define VALID_CHAR(c) (isalnum(c) || (c) == '.' || (c) == '_' || (c) == '-') 628 629 /* return 1 if `login' is a valid login name */ 630 static int 631 valid_login(char *login_name, int allow_samba) 632 { 633 unsigned char *cp; 634 635 /* First character of a login name cannot be '-'. */ 636 if (*login_name == '-') { 637 return 0; 638 } 639 if (strlen(login_name) >= LOGIN_NAME_MAX) { 640 return 0; 641 } 642 for (cp = (unsigned char *)login_name ; *cp ; cp++) { 643 if (!VALID_CHAR(*cp)) { 644 #ifdef EXTENSIONS 645 /* check for a trailing '$' in a Samba user name */ 646 if (allow_samba && *cp == '$' && *(cp + 1) == 0x0) { 647 return 1; 648 } 649 #endif 650 return 0; 651 } 652 } 653 return 1; 654 } 655 656 /* return 1 if `group' is a valid group name */ 657 static int 658 valid_group(char *group) 659 { 660 unsigned char *cp; 661 662 for (cp = (unsigned char *)group; *cp; cp++) { 663 if (!VALID_CHAR(*cp)) { 664 return 0; 665 } 666 } 667 return 1; 668 } 669 670 /* find the next gid in the range lo .. hi */ 671 static int 672 getnextgid(int *gidp, int lo, int hi) 673 { 674 for (*gidp = lo ; *gidp < hi ; *gidp += 1) { 675 if (getgrgid((gid_t)*gidp) == NULL) { 676 return 1; 677 } 678 } 679 return 0; 680 } 681 682 #ifdef EXTENSIONS 683 /* save a range of uids */ 684 static int 685 save_range(user_t *up, char *cp) 686 { 687 int from; 688 int to; 689 int i; 690 691 if (up->u_rsize == 0) { 692 up->u_rsize = 32; 693 NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0)); 694 } else if (up->u_rc == up->u_rsize) { 695 up->u_rsize *= 2; 696 RENEW(range_t, up->u_rv, up->u_rsize, return(0)); 697 } 698 if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) { 699 for (i = up->u_defrc ; i < up->u_rc ; i++) { 700 if (up->u_rv[i].r_from == from && 701 up->u_rv[i].r_to == to) { 702 break; 703 } 704 } 705 if (i == up->u_rc) { 706 up->u_rv[up->u_rc].r_from = from; 707 up->u_rv[up->u_rc].r_to = to; 708 up->u_rc += 1; 709 } 710 } else { 711 warnx("Bad range `%s'", cp); 712 return 0; 713 } 714 return 1; 715 } 716 #endif 717 718 /* set the defaults in the defaults file */ 719 static int 720 setdefaults(user_t *up) 721 { 722 char template[MaxFileNameLen]; 723 FILE *fp; 724 int ret; 725 int fd; 726 #ifdef EXTENSIONS 727 int i; 728 #endif 729 730 (void)snprintf(template, sizeof(template), "%s.XXXXXX", 731 _PATH_USERMGMT_CONF); 732 if ((fd = mkstemp(template)) < 0) { 733 warn("Can't set defaults: can't mkstemp `%s' for writing", 734 _PATH_USERMGMT_CONF); 735 return 0; 736 } 737 if ((fp = fdopen(fd, "w")) == NULL) { 738 warn("Can't set defaults: can't fdopen `%s' for writing", 739 _PATH_USERMGMT_CONF); 740 return 0; 741 } 742 ret = 1; 743 if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 || 744 fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 || 745 fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 || 746 fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 || 747 #ifdef EXTENSIONS 748 fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 || 749 fprintf(fp, "homeperm\t0%o\n", up->u_homeperm) <= 0 || 750 #endif 751 fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ? 752 UNSET_INACTIVE : up->u_inactive) <= 0 || 753 fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ? 754 UNSET_EXPIRY : up->u_expire) <= 0 || 755 fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? 756 "false" : "true") <= 0) { 757 warn("Can't write to `%s'", _PATH_USERMGMT_CONF); 758 ret = 0; 759 } 760 #ifdef EXTENSIONS 761 for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0; 762 i < up->u_rc ; i++) { 763 if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, 764 up->u_rv[i].r_to) <= 0) { 765 warn("Can't set defaults: can't write to `%s'", 766 _PATH_USERMGMT_CONF); 767 ret = 0; 768 } 769 } 770 #endif 771 (void)fclose(fp); 772 if (ret) { 773 ret = ((rename(template, _PATH_USERMGMT_CONF) == 0) && 774 (chmod(_PATH_USERMGMT_CONF, 0644) == 0)); 775 } 776 return ret; 777 } 778 779 /* read the defaults file */ 780 static void 781 read_defaults(user_t *up) 782 { 783 struct stat st; 784 size_t lineno; 785 size_t len; 786 FILE *fp; 787 char *cp; 788 char *s; 789 790 memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP)); 791 memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR)); 792 memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR)); 793 memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL)); 794 memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT)); 795 #ifdef EXTENSIONS 796 memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS)); 797 #endif 798 up->u_rsize = 16; 799 up->u_defrc = 0; 800 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1)); 801 up->u_inactive = DEF_INACTIVE; 802 up->u_expire = DEF_EXPIRE; 803 if ((fp = fopen(_PATH_USERMGMT_CONF, "r")) == NULL) { 804 if (stat(_PATH_USERMGMT_CONF, &st) < 0 && !setdefaults(up)) { 805 warn("Can't create `%s' defaults file", 806 _PATH_USERMGMT_CONF); 807 } 808 fp = fopen(_PATH_USERMGMT_CONF, "r"); 809 } 810 if (fp != NULL) { 811 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) { 812 if (strncmp(s, "group", 5) == 0) { 813 cp = skipspace(s + 5); 814 memsave(&up->u_primgrp, (char *)cp, strlen(cp)); 815 } else if (strncmp(s, "base_dir", 8) == 0) { 816 cp = skipspace(s + 8); 817 memsave(&up->u_basedir, (char *)cp, strlen(cp)); 818 } else if (strncmp(s, "skel_dir", 8) == 0) { 819 cp = skipspace(s + 8); 820 memsave(&up->u_skeldir, (char *)cp, strlen(cp)); 821 } else if (strncmp(s, "shell", 5) == 0) { 822 cp = skipspace(s + 5); 823 memsave(&up->u_shell, cp, strlen(cp)); 824 #ifdef EXTENSIONS 825 } else if (strncmp((char *)s, "class", 5) == 0) { 826 cp = skipspace(s + 5); 827 memsave(&up->u_class, cp, strlen(cp)); 828 #endif 829 #ifdef EXTENSIONS 830 } else if (strncmp(s, "homeperm", 8) == 0) { 831 for (cp = s + 8; *cp && 832 isspace((unsigned char)*cp); cp++) 833 ; 834 up->u_homeperm = strtoul(cp, NULL, 8); 835 #endif 836 } else if (strncmp(s, "inactive", 8) == 0) { 837 cp = skipspace(s + 8); 838 if (strcmp(cp, UNSET_INACTIVE) == 0) { 839 if (up->u_inactive) { 840 FREE(up->u_inactive); 841 } 842 up->u_inactive = NULL; 843 } else { 844 memsave(&up->u_inactive, cp, strlen(cp)); 845 } 846 #ifdef EXTENSIONS 847 } else if (strncmp(s, "range", 5) == 0) { 848 cp = skipspace(s + 5); 849 (void)save_range(up, cp); 850 #endif 851 #ifdef EXTENSIONS 852 } else if (strncmp(s, "preserve", 8) == 0) { 853 cp = skipspace(s + 8); 854 up->u_preserve = 855 (strncmp(cp, "true", 4) == 0) ? 1 : 856 (strncmp(cp, "yes", 3) == 0) ? 1 : atoi(cp); 857 #endif 858 } else if (strncmp(s, "expire", 6) == 0) { 859 cp = skipspace(s + 6); 860 if (strcmp(cp, UNSET_EXPIRY) == 0) { 861 if (up->u_expire) { 862 FREE(up->u_expire); 863 } 864 up->u_expire = NULL; 865 } else { 866 memsave(&up->u_expire, cp, strlen(cp)); 867 } 868 } 869 (void)free(s); 870 } 871 (void)fclose(fp); 872 } 873 if (up->u_rc == 0) { 874 up->u_rv[up->u_rc].r_from = DEF_LOWUID; 875 up->u_rv[up->u_rc].r_to = DEF_HIGHUID; 876 up->u_rc += 1; 877 } 878 up->u_defrc = up->u_rc; 879 up->u_homeperm = DEF_HOMEPERM; 880 } 881 882 /* return the next valid unused uid */ 883 static int 884 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid) 885 { 886 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) { 887 if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) { 888 if (sync_uid_gid) { 889 if (getgrgid((gid_t)(*uid)) == NULL) { 890 return 1; 891 } 892 } else { 893 return 1; 894 } 895 } 896 } 897 return 0; 898 } 899 900 /* structure which defines a password type */ 901 typedef struct passwd_type_t { 902 const char *type; /* optional type descriptor */ 903 size_t desc_length; /* length of type descriptor */ 904 size_t length; /* length of password */ 905 const char *regex; /* regexp to output the password */ 906 size_t re_sub; /* subscript of regexp to use */ 907 } passwd_type_t; 908 909 static passwd_type_t passwd_types[] = { 910 { "$sha1", 5, 28, "\\$[^$]+\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* SHA1 */ 911 { "$2a", 3, 54, "\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* Blowfish */ 912 { "$1", 2, 34, NULL, 0 }, /* MD5 */ 913 { "", 0, DES_Len,NULL, 0 }, /* standard DES */ 914 { NULL, (size_t)~0, (size_t)~0, NULL, 0 } 915 /* none - terminate search */ 916 }; 917 918 /* return non-zero if it's a valid password - check length for cipher type */ 919 static int 920 valid_password_length(char *newpasswd) 921 { 922 passwd_type_t *pwtp; 923 regmatch_t matchv[10]; 924 regex_t r; 925 926 for (pwtp = passwd_types; pwtp->desc_length != (size_t)~0; pwtp++) { 927 if (strncmp(newpasswd, pwtp->type, pwtp->desc_length) == 0) { 928 if (pwtp->regex == NULL) { 929 return strlen(newpasswd) == pwtp->length; 930 } 931 (void)regcomp(&r, pwtp->regex, REG_EXTENDED); 932 if (regexec(&r, newpasswd, 10, matchv, 0) == 0) { 933 regfree(&r); 934 return (int)(matchv[pwtp->re_sub].rm_eo - 935 matchv[pwtp->re_sub].rm_so + 1) == 936 pwtp->length; 937 } 938 regfree(&r); 939 } 940 } 941 return 0; 942 } 943 944 #ifdef EXTENSIONS 945 /* return 1 if `class' is a valid login class */ 946 static int 947 valid_class(char *class) 948 { 949 login_cap_t *lc; 950 951 if (class == NULL || *class == '\0') { 952 return 1; 953 } 954 /* 955 * Check if /etc/login.conf exists. login_getclass() will 956 * return 1 due to it not existing, so not informing the 957 * user the actual login class does not exist. 958 */ 959 960 if (access(PATH_LOGINCONF, R_OK) == -1) { 961 warn("Access failed for `%s'; will not validate class `%s'", 962 PATH_LOGINCONF, class); 963 return 1; 964 } 965 966 if ((lc = login_getclass(class)) != NULL) { 967 login_close(lc); 968 return 1; 969 } 970 return 0; 971 } 972 973 /* return 1 if the `shellname' is a valid user shell */ 974 static int 975 valid_shell(const char *shellname) 976 { 977 char *shellp; 978 979 if (access(_PATH_SHELLS, R_OK) == -1) { 980 /* Don't exit */ 981 warn("Access failed for `%s'; will not validate shell `%s'", 982 _PATH_SHELLS, shellname); 983 return 1; 984 } 985 986 /* if nologin is used as a shell, consider it a valid shell */ 987 if (strcmp(shellname, NOLOGIN) == 0) 988 return 1; 989 990 while ((shellp = getusershell()) != NULL) 991 if (strcmp(shellp, shellname) == 0) 992 return 1; 993 994 warnx("Shell `%s' not found in `%s'", shellname, _PATH_SHELLS); 995 996 return access(shellname, X_OK) != -1; 997 } 998 #endif 999 1000 /* look for a valid time, return 0 if it was specified but bad */ 1001 static int 1002 scantime(time_t *tp, char *s) 1003 { 1004 struct tm tm; 1005 char *ep; 1006 long val; 1007 1008 *tp = 0; 1009 if (s != NULL) { 1010 (void)memset(&tm, 0, sizeof(tm)); 1011 if (strptime(s, "%c", &tm) != NULL) { 1012 *tp = mktime(&tm); 1013 return (*tp == -1) ? 0 : 1; 1014 } else if (strptime(s, "%B %d %Y", &tm) != NULL) { 1015 *tp = mktime(&tm); 1016 return (*tp == -1) ? 0 : 1; 1017 } else { 1018 errno = 0; 1019 *tp = val = strtol(s, &ep, 10); 1020 if (*ep != '\0' || *tp < -1 || errno == ERANGE) { 1021 *tp = 0; 1022 return 0; 1023 } 1024 if (*tp != val) { 1025 return 0; 1026 } 1027 } 1028 } 1029 return 1; 1030 } 1031 1032 /* add a user */ 1033 static int 1034 adduser(char *login_name, user_t *up) 1035 { 1036 struct group *grp; 1037 struct stat st; 1038 time_t expire; 1039 time_t inactive; 1040 char password[PasswordLength + 1]; 1041 char home[MaxFileNameLen]; 1042 char buf[MaxFileNameLen]; 1043 int sync_uid_gid; 1044 int masterfd; 1045 int ptmpfd; 1046 int gid; 1047 int cc; 1048 int i; 1049 1050 if (!valid_login(login_name, up->u_allow_samba)) { 1051 errx(EXIT_FAILURE, "Can't add user `%s': invalid login name", login_name); 1052 } 1053 #ifdef EXTENSIONS 1054 if (!valid_class(up->u_class)) { 1055 errx(EXIT_FAILURE, "Can't add user `%s': no such login class `%s'", 1056 login_name, up->u_class); 1057 } 1058 #endif 1059 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 1060 err(EXIT_FAILURE, "Can't add user `%s': can't open `%s'", 1061 login_name, _PATH_MASTERPASSWD); 1062 } 1063 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 1064 err(EXIT_FAILURE, "Can't add user `%s': can't lock `%s'", 1065 login_name, _PATH_MASTERPASSWD); 1066 } 1067 pw_init(); 1068 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 1069 int serrno = errno; 1070 (void)close(masterfd); 1071 errno = serrno; 1072 err(EXIT_FAILURE, "Can't add user `%s': can't obtain pw_lock", 1073 login_name); 1074 } 1075 while ((cc = read(masterfd, buf, sizeof(buf))) > 0) { 1076 if (write(ptmpfd, buf, (size_t)(cc)) != cc) { 1077 int serrno = errno; 1078 (void)close(masterfd); 1079 (void)close(ptmpfd); 1080 (void)pw_abort(); 1081 errno = serrno; 1082 err(EXIT_FAILURE, "Can't add user `%s': " 1083 "short write to /etc/ptmp", login_name); 1084 } 1085 } 1086 /* if no uid was specified, get next one in [low_uid..high_uid] range */ 1087 sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0); 1088 if (up->u_uid == -1) { 1089 int got_id = 0; 1090 1091 /* 1092 * Look for a free UID in the command line ranges (if any). 1093 * These start after the ranges specified in the config file. 1094 */ 1095 for (i = up->u_defrc; !got_id && i < up->u_rc ; i++) { 1096 got_id = getnextuid(sync_uid_gid, &up->u_uid, 1097 up->u_rv[i].r_from, up->u_rv[i].r_to); 1098 } 1099 /* 1100 * If there were no free UIDs in the command line ranges, 1101 * try the ranges from the config file (there will always 1102 * be at least one default). 1103 */ 1104 for (i = 0; !got_id && i < up->u_defrc; i++) { 1105 got_id = getnextuid(sync_uid_gid, &up->u_uid, 1106 up->u_rv[i].r_from, up->u_rv[i].r_to); 1107 } 1108 if (!got_id) { 1109 (void)close(ptmpfd); 1110 (void)pw_abort(); 1111 errx(EXIT_FAILURE, "Can't add user `%s': " 1112 "can't get next uid for %d", login_name, 1113 up->u_uid); 1114 } 1115 } 1116 /* check uid isn't already allocated */ 1117 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) { 1118 (void)close(ptmpfd); 1119 (void)pw_abort(); 1120 errx(EXIT_FAILURE, "Can't add user `%s': " 1121 "uid %d is already in use", login_name, up->u_uid); 1122 } 1123 /* if -g=uid was specified, check gid is unused */ 1124 if (sync_uid_gid) { 1125 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 1126 (void)close(ptmpfd); 1127 (void)pw_abort(); 1128 errx(EXIT_FAILURE, "Can't add user `%s': " 1129 "gid %d is already in use", login_name, 1130 up->u_uid); 1131 } 1132 gid = up->u_uid; 1133 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 1134 gid = grp->gr_gid; 1135 } else if (is_number(up->u_primgrp) && 1136 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) { 1137 gid = grp->gr_gid; 1138 } else { 1139 (void)close(ptmpfd); 1140 (void)pw_abort(); 1141 errx(EXIT_FAILURE, "Can't add user `%s': group %s not found", 1142 login_name, up->u_primgrp); 1143 } 1144 /* check name isn't already in use */ 1145 if (!(up->u_flags & F_DUPUID) && getpwnam(login_name) != NULL) { 1146 (void)close(ptmpfd); 1147 (void)pw_abort(); 1148 errx(EXIT_FAILURE, "Can't add user `%s': " 1149 "`%s' is already a user", login_name, login_name); 1150 } 1151 if (up->u_flags & F_HOMEDIR) { 1152 (void)strlcpy(home, up->u_home, sizeof(home)); 1153 } else { 1154 /* if home directory hasn't been given, make it up */ 1155 (void)snprintf(home, sizeof(home), "%s/%s", up->u_basedir, 1156 login_name); 1157 } 1158 if (up->u_flags & F_SHELL) { 1159 #ifdef EXTENSIONS 1160 if (!valid_shell(up->u_shell)) { 1161 int oerrno = errno; 1162 (void)close(ptmpfd); 1163 (void)pw_abort(); 1164 errno = oerrno; 1165 errx(EXIT_FAILURE, "Can't add user `%s': " 1166 "Cannot access shell `%s'", 1167 login_name, up->u_shell); 1168 } 1169 #endif 1170 } 1171 1172 if (!scantime(&inactive, up->u_inactive)) { 1173 warnx("Warning: inactive time `%s' invalid, password expiry off", 1174 up->u_inactive); 1175 } 1176 if (!scantime(&expire, up->u_expire) || expire == -1) { 1177 warnx("Warning: expire time `%s' invalid, account expiry off", 1178 up->u_expire); 1179 expire = 0; /* Just in case. */ 1180 } 1181 if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) { 1182 warnx("Warning: home directory `%s' doesn't exist, " 1183 "and -m was not specified", home); 1184 } 1185 password[sizeof(password) - 1] = '\0'; 1186 if (up->u_password != NULL && valid_password_length(up->u_password)) { 1187 (void)strlcpy(password, up->u_password, sizeof(password)); 1188 } else { 1189 (void)memset(password, '*', DES_Len); 1190 password[DES_Len] = 0; 1191 if (up->u_password != NULL) { 1192 warnx("Password `%s' is invalid: setting it to `%s'", 1193 up->u_password, password); 1194 } 1195 } 1196 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", 1197 login_name, 1198 password, 1199 up->u_uid, 1200 gid, 1201 #ifdef EXTENSIONS 1202 up->u_class, 1203 #else 1204 "", 1205 #endif 1206 (long) inactive, 1207 (long) expire, 1208 up->u_comment, 1209 home, 1210 up->u_shell); 1211 if (write(ptmpfd, buf, (size_t) cc) != cc) { 1212 int serrno = errno; 1213 (void)close(ptmpfd); 1214 (void)pw_abort(); 1215 errno = serrno; 1216 err(EXIT_FAILURE, "Can't add user `%s': write failed", 1217 login_name); 1218 } 1219 if (up->u_flags & F_MKDIR) { 1220 if (lstat(home, &st) == 0) { 1221 (void)close(ptmpfd); 1222 (void)pw_abort(); 1223 errx(EXIT_FAILURE, 1224 "Can't add user `%s': home directory `%s' " 1225 "already exists", login_name, home); 1226 } else { 1227 if (asystem("%s -p %s", MKDIR, home) != 0) { 1228 (void)close(ptmpfd); 1229 (void)pw_abort(); 1230 errx(EXIT_FAILURE, "Can't add user `%s': " 1231 "can't mkdir `%s'", login_name, home); 1232 } 1233 (void)copydotfiles(up->u_skeldir, up->u_uid, gid, home, 1234 up->u_homeperm); 1235 } 1236 } 1237 if (strcmp(up->u_primgrp, "=uid") == 0 && 1238 getgrnam(login_name) == NULL && 1239 !creategid(login_name, gid, login_name)) { 1240 (void)close(ptmpfd); 1241 (void)pw_abort(); 1242 errx(EXIT_FAILURE, "Can't add user `%s': can't create gid %d ", 1243 login_name, gid); 1244 } 1245 if (up->u_groupc > 0 && 1246 !append_group(login_name, up->u_groupc, up->u_groupv)) { 1247 (void)close(ptmpfd); 1248 (void)pw_abort(); 1249 errx(EXIT_FAILURE, "Can't add user `%s': can't append " 1250 "to new groups", login_name); 1251 } 1252 (void)close(ptmpfd); 1253 #if PW_MKDB_ARGC == 2 1254 if (pw_mkdb(login_name, 0) < 0) 1255 #else 1256 if (pw_mkdb() < 0) 1257 #endif 1258 { 1259 (void)pw_abort(); 1260 errx(EXIT_FAILURE, "Can't add user `%s': pw_mkdb failed", 1261 login_name); 1262 } 1263 syslog(LOG_INFO, "New user added: name=%s, uid=%d, gid=%d, home=%s, " 1264 "shell=%s", login_name, up->u_uid, gid, home, up->u_shell); 1265 return 1; 1266 } 1267 1268 /* remove a user from the groups file */ 1269 static int 1270 rm_user_from_groups(char *login_name) 1271 { 1272 struct stat st; 1273 regmatch_t matchv[10]; 1274 regex_t r; 1275 FILE *from; 1276 FILE *to; 1277 char line[MaxEntryLen]; 1278 char buf[MaxEntryLen]; 1279 char f[MaxFileNameLen]; 1280 int fd; 1281 int cc; 1282 int sc; 1283 1284 (void)snprintf(line, sizeof(line), "(:|,)(%s)(,|$)", login_name); 1285 if ((sc = regcomp(&r, line, REG_EXTENDED|REG_NEWLINE)) != 0) { 1286 (void)regerror(sc, &r, buf, sizeof(buf)); 1287 warnx("Can't compile regular expression `%s' (%s)", line, 1288 buf); 1289 return 0; 1290 } 1291 if ((from = fopen(_PATH_GROUP, "r")) == NULL) { 1292 warn("Can't remove user `%s' from `%s': can't open `%s'", 1293 login_name, _PATH_GROUP, _PATH_GROUP); 1294 return 0; 1295 } 1296 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) { 1297 warn("Can't remove user `%s' from `%s': can't lock `%s'", 1298 login_name, _PATH_GROUP, _PATH_GROUP); 1299 (void)fclose(from); 1300 return 0; 1301 } 1302 (void)fstat(fileno(from), &st); 1303 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP); 1304 if ((fd = mkstemp(f)) < 0) { 1305 warn("Can't remove user `%s' from `%s': mkstemp failed", 1306 login_name, _PATH_GROUP); 1307 (void)fclose(from); 1308 return 0; 1309 } 1310 if ((to = fdopen(fd, "w")) == NULL) { 1311 warn("Can't remove user `%s' from `%s': fdopen `%s' failed", 1312 login_name, _PATH_GROUP, f); 1313 (void)fclose(from); 1314 (void)close(fd); 1315 (void)unlink(f); 1316 return 0; 1317 } 1318 while (fgets(buf, sizeof(buf), from) != NULL) { 1319 cc = strlen(buf); 1320 if (regexec(&r, buf, 10, matchv, 0) == 0) { 1321 if (buf[(int)matchv[1].rm_so] == ',') { 1322 matchv[2].rm_so = matchv[1].rm_so; 1323 } else if (matchv[2].rm_eo != matchv[3].rm_eo) { 1324 matchv[2].rm_eo = matchv[3].rm_eo; 1325 } 1326 cc -= (int) matchv[2].rm_eo; 1327 sc = (int) matchv[2].rm_so; 1328 if (fwrite(buf, sizeof(char), (size_t)sc, to) != sc || 1329 fwrite(&buf[(int)matchv[2].rm_eo], sizeof(char), 1330 (size_t)cc, to) != cc) { 1331 warn("Can't remove user `%s' from `%s': " 1332 "short write to `%s'", login_name, 1333 _PATH_GROUP, f); 1334 (void)fclose(from); 1335 (void)close(fd); 1336 (void)unlink(f); 1337 return 0; 1338 } 1339 } else if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { 1340 warn("Can't remove user `%s' from `%s': " 1341 "short write to `%s'", login_name, _PATH_GROUP, f); 1342 (void)fclose(from); 1343 (void)close(fd); 1344 (void)unlink(f); 1345 return 0; 1346 } 1347 } 1348 (void)fclose(from); 1349 (void)fclose(to); 1350 if (rename(f, _PATH_GROUP) < 0) { 1351 warn("Can't remove user `%s' from `%s': " 1352 "can't rename `%s' to `%s'", 1353 login_name, _PATH_GROUP, f, _PATH_GROUP); 1354 (void)unlink(f); 1355 return 0; 1356 } 1357 (void)chmod(_PATH_GROUP, st.st_mode & 07777); 1358 return 1; 1359 } 1360 1361 /* check that the user or group is local, not from YP/NIS */ 1362 static int 1363 is_local(char *name, const char *file) 1364 { 1365 FILE *fp; 1366 char buf[MaxEntryLen]; 1367 size_t len; 1368 int ret; 1369 1370 if ((fp = fopen(file, "r")) == NULL) { 1371 err(EXIT_FAILURE, "Can't open `%s'", file); 1372 } 1373 len = strlen(name); 1374 for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) { 1375 if (strncmp(buf, name, len) == 0 && buf[len] == ':') { 1376 ret = 1; 1377 break; 1378 } 1379 } 1380 (void)fclose(fp); 1381 return ret; 1382 } 1383 1384 /* modify a user */ 1385 static int 1386 moduser(char *login_name, char *newlogin, user_t *up, int allow_samba) 1387 { 1388 struct passwd *pwp; 1389 struct group *grp; 1390 const char *homedir; 1391 char *locked_pwd; 1392 size_t colonc; 1393 size_t loginc; 1394 size_t len; 1395 FILE *master; 1396 char newdir[MaxFileNameLen]; 1397 char buf[MaxEntryLen]; 1398 char *colon; 1399 int masterfd; 1400 int ptmpfd; 1401 int error; 1402 1403 if (!valid_login(newlogin, allow_samba)) { 1404 errx(EXIT_FAILURE, "Can't modify user `%s': invalid login name", 1405 login_name); 1406 } 1407 if ((pwp = getpwnam(login_name)) == NULL) { 1408 errx(EXIT_FAILURE, "Can't modify user `%s': no such user", 1409 login_name); 1410 } 1411 if (!is_local(login_name, _PATH_MASTERPASSWD)) { 1412 errx(EXIT_FAILURE, "Can't modify user `%s': must be a local user", 1413 login_name); 1414 } 1415 /* keep dir name in case we need it for '-m' */ 1416 homedir = pwp->pw_dir; 1417 1418 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) { 1419 err(EXIT_FAILURE, "Can't modify user `%s': can't open `%s'", 1420 login_name, _PATH_MASTERPASSWD); 1421 } 1422 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) { 1423 err(EXIT_FAILURE, "Can't modify user `%s': can't lock `%s'", 1424 login_name, _PATH_MASTERPASSWD); 1425 } 1426 pw_init(); 1427 if ((ptmpfd = pw_lock(WAITSECS)) < 0) { 1428 int serrno = errno; 1429 (void)close(masterfd); 1430 errno = serrno; 1431 err(EXIT_FAILURE, "Can't modify user `%s': " 1432 "can't obtain pw_lock", login_name); 1433 } 1434 if ((master = fdopen(masterfd, "r")) == NULL) { 1435 int serrno = errno; 1436 (void)close(masterfd); 1437 (void)close(ptmpfd); 1438 (void)pw_abort(); 1439 errno = serrno; 1440 err(EXIT_FAILURE, "Can't modify user `%s': " 1441 "fdopen fd for %s", login_name, _PATH_MASTERPASSWD); 1442 } 1443 if (up != NULL) { 1444 if (up->u_flags & F_USERNAME) { 1445 /* 1446 * If changing name, 1447 * check new name isn't already in use 1448 */ 1449 if (strcmp(login_name, newlogin) != 0 && 1450 getpwnam(newlogin) != NULL) { 1451 (void)close(ptmpfd); 1452 (void)pw_abort(); 1453 errx(EXIT_FAILURE, "Can't modify user `%s': " 1454 "`%s' is already a user", login_name, 1455 newlogin); 1456 } 1457 pwp->pw_name = newlogin; 1458 1459 /* 1460 * Provide a new directory name in case the 1461 * home directory is to be moved. 1462 */ 1463 if (up->u_flags & F_MKDIR) { 1464 (void)snprintf(newdir, sizeof(newdir), "%s/%s", 1465 up->u_basedir, newlogin); 1466 pwp->pw_dir = newdir; 1467 } 1468 } 1469 if (up->u_flags & F_PASSWORD) { 1470 if (up->u_password != NULL) { 1471 if (!valid_password_length(up->u_password)) { 1472 (void)close(ptmpfd); 1473 (void)pw_abort(); 1474 errx(EXIT_FAILURE, 1475 "Can't modify user `%s': " 1476 "invalid password: `%s'", 1477 login_name, up->u_password); 1478 } 1479 if ((locked_pwd = 1480 strstr(pwp->pw_passwd, LOCKED)) != NULL) { 1481 /* 1482 * account is locked - keep it locked 1483 * and just change the password. 1484 */ 1485 if (asprintf(&locked_pwd, "%s%s", 1486 LOCKED, up->u_password) == -1) { 1487 (void)close(ptmpfd); 1488 (void)pw_abort(); 1489 err(EXIT_FAILURE, 1490 "Can't modify user `%s': " 1491 "asprintf failed", 1492 login_name); 1493 } 1494 pwp->pw_passwd = locked_pwd; 1495 } else { 1496 pwp->pw_passwd = up->u_password; 1497 } 1498 } 1499 } 1500 1501 /* check whether we should lock the account. */ 1502 if (up->u_locked == LOCK) { 1503 /* check to see account if already locked. */ 1504 if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED)) 1505 != NULL) { 1506 warnx("Account is already locked"); 1507 } else { 1508 if (asprintf(&locked_pwd, "%s%s", LOCKED, 1509 pwp->pw_passwd) == -1) { 1510 (void)close(ptmpfd); 1511 (void)pw_abort(); 1512 err(EXIT_FAILURE, 1513 "Can't modify user `%s': " 1514 "asprintf failed", login_name); 1515 } 1516 pwp->pw_passwd = locked_pwd; 1517 } 1518 } else if (up->u_locked == UNLOCK) { 1519 if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED)) 1520 == NULL) { 1521 warnx("Can't modify user `%s': " 1522 "account is not locked", login_name); 1523 } else { 1524 pwp->pw_passwd = locked_pwd + strlen(LOCKED); 1525 } 1526 } 1527 1528 if (up->u_flags & F_UID) { 1529 /* check uid isn't already allocated */ 1530 if (!(up->u_flags & F_DUPUID) && 1531 getpwuid((uid_t)(up->u_uid)) != NULL) { 1532 (void)close(ptmpfd); 1533 (void)pw_abort(); 1534 errx(EXIT_FAILURE, "Can't modify user `%s': " 1535 "uid `%d' is already in use", login_name, 1536 up->u_uid); 1537 } 1538 pwp->pw_uid = up->u_uid; 1539 } 1540 if (up->u_flags & F_GROUP) { 1541 /* if -g=uid was specified, check gid is unused */ 1542 if (strcmp(up->u_primgrp, "=uid") == 0) { 1543 if (getgrgid((gid_t)(up->u_uid)) != NULL) { 1544 (void)close(ptmpfd); 1545 (void)pw_abort(); 1546 errx(EXIT_FAILURE, 1547 "Can't modify user `%s': " 1548 "gid %d is already in use", 1549 login_name, up->u_uid); 1550 } 1551 pwp->pw_gid = up->u_uid; 1552 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) { 1553 pwp->pw_gid = grp->gr_gid; 1554 } else if (is_number(up->u_primgrp) && 1555 (grp = getgrgid( 1556 (gid_t)atoi(up->u_primgrp))) != NULL) { 1557 pwp->pw_gid = grp->gr_gid; 1558 } else { 1559 (void)close(ptmpfd); 1560 (void)pw_abort(); 1561 errx(EXIT_FAILURE, "Can't modify user `%s': " 1562 "group %s not found", login_name, 1563 up->u_primgrp); 1564 } 1565 } 1566 if (up->u_flags & F_INACTIVE) { 1567 if (!scantime(&pwp->pw_change, up->u_inactive)) { 1568 warnx("Warning: inactive time `%s' invalid, " 1569 "password expiry off", 1570 up->u_inactive); 1571 } 1572 } 1573 if (up->u_flags & F_EXPIRE) { 1574 if (!scantime(&pwp->pw_expire, up->u_expire) || 1575 pwp->pw_expire == -1) { 1576 warnx("Warning: expire time `%s' invalid, " 1577 "account expiry off", 1578 up->u_expire); 1579 pwp->pw_expire = 0; 1580 } 1581 } 1582 if (up->u_flags & F_COMMENT) { 1583 pwp->pw_gecos = up->u_comment; 1584 } 1585 if (up->u_flags & F_HOMEDIR) { 1586 pwp->pw_dir = up->u_home; 1587 } 1588 if (up->u_flags & F_SHELL) { 1589 #ifdef EXTENSIONS 1590 if (!valid_shell(up->u_shell)) { 1591 int oerrno = errno; 1592 (void)close(ptmpfd); 1593 (void)pw_abort(); 1594 errno = oerrno; 1595 errx(EXIT_FAILURE, "Can't modify user `%s': " 1596 "Cannot access shell `%s'", 1597 login_name, up->u_shell); 1598 } 1599 pwp->pw_shell = up->u_shell; 1600 #else 1601 pwp->pw_shell = up->u_shell; 1602 #endif 1603 } 1604 #ifdef EXTENSIONS 1605 if (up->u_flags & F_CLASS) { 1606 if (!valid_class(up->u_class)) { 1607 (void)close(ptmpfd); 1608 (void)pw_abort(); 1609 errx(EXIT_FAILURE, "Can't modify user `%s': " 1610 "no such login class `%s'", login_name, 1611 up->u_class); 1612 } 1613 pwp->pw_class = up->u_class; 1614 } 1615 #endif 1616 } 1617 loginc = strlen(login_name); 1618 while (fgets(buf, sizeof(buf), master) != NULL) { 1619 if ((colon = strchr(buf, ':')) == NULL) { 1620 warnx("Malformed entry `%s'. Skipping", buf); 1621 continue; 1622 } 1623 colonc = (size_t)(colon - buf); 1624 if (strncmp(login_name, buf, loginc) == 0 && loginc == colonc) { 1625 if (up != NULL) { 1626 len = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:" 1627 #ifdef EXTENSIONS 1628 "%s" 1629 #endif 1630 ":%ld:%ld:%s:%s:%s\n", 1631 newlogin, 1632 pwp->pw_passwd, 1633 pwp->pw_uid, 1634 pwp->pw_gid, 1635 #ifdef EXTENSIONS 1636 pwp->pw_class, 1637 #endif 1638 (long)pwp->pw_change, 1639 (long)pwp->pw_expire, 1640 pwp->pw_gecos, 1641 pwp->pw_dir, 1642 pwp->pw_shell); 1643 if (write(ptmpfd, buf, len) != len) { 1644 int serrno = errno; 1645 (void)close(ptmpfd); 1646 (void)pw_abort(); 1647 errno = serrno; 1648 err(EXIT_FAILURE, "Can't modify user " 1649 "`%s': write", login_name); 1650 } 1651 } 1652 } else { 1653 len = strlen(buf); 1654 if (write(ptmpfd, buf, len) != len) { 1655 int serrno = errno; 1656 (void)close(masterfd); 1657 (void)close(ptmpfd); 1658 (void)pw_abort(); 1659 errno = serrno; 1660 err(EXIT_FAILURE, "Can't modify `%s': " 1661 "write", login_name); 1662 } 1663 } 1664 } 1665 if (up != NULL) { 1666 if ((up->u_flags & F_MKDIR) && 1667 asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) { 1668 (void)close(ptmpfd); 1669 (void)pw_abort(); 1670 errx(EXIT_FAILURE, "Can't modify user `%s': " 1671 "can't move `%s' to `%s'", 1672 login_name, homedir, pwp->pw_dir); 1673 } 1674 if (up->u_groupc > 0 && 1675 !append_group(newlogin, up->u_groupc, up->u_groupv)) { 1676 (void)close(ptmpfd); 1677 (void)pw_abort(); 1678 errx(EXIT_FAILURE, "Can't modify user `%s': " 1679 "can't append `%s' to new groups", 1680 login_name, newlogin); 1681 } 1682 } 1683 (void)close(ptmpfd); 1684 (void)fclose(master); 1685 #if PW_MKDB_ARGC == 2 1686 if (up != NULL && strcmp(login_name, newlogin) == 0) { 1687 error = pw_mkdb(login_name, 0); 1688 } else { 1689 error = pw_mkdb(NULL, 0); 1690 } 1691 #else 1692 error = pw_mkdb(); 1693 #endif 1694 if (error < 0) { 1695 (void)pw_abort(); 1696 errx(EXIT_FAILURE, "Can't modify user `%s': pw_mkdb failed", 1697 login_name); 1698 } 1699 if (up == NULL) { 1700 syslog(LOG_INFO, "User removed: name=%s", login_name); 1701 } else if (strcmp(login_name, newlogin) == 0) { 1702 syslog(LOG_INFO, "User information modified: name=%s, uid=%d, " 1703 "gid=%d, home=%s, shell=%s", 1704 login_name, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, 1705 pwp->pw_shell); 1706 } else { 1707 syslog(LOG_INFO, "User information modified: name=%s, " 1708 "new name=%s, uid=%d, gid=%d, home=%s, shell=%s", 1709 login_name, newlogin, pwp->pw_uid, pwp->pw_gid, 1710 pwp->pw_dir, pwp->pw_shell); 1711 } 1712 return 1; 1713 } 1714 1715 #ifdef EXTENSIONS 1716 /* see if we can find out the user struct */ 1717 static struct passwd * 1718 find_user_info(const char *name) 1719 { 1720 struct passwd *pwp; 1721 1722 if ((pwp = getpwnam(name)) != NULL) { 1723 return pwp; 1724 } 1725 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) { 1726 return pwp; 1727 } 1728 return NULL; 1729 } 1730 #endif 1731 1732 /* see if we can find out the group struct */ 1733 static struct group * 1734 find_group_info(const char *name) 1735 { 1736 struct group *grp; 1737 1738 if ((grp = getgrnam(name)) != NULL) { 1739 return grp; 1740 } 1741 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) { 1742 return grp; 1743 } 1744 return NULL; 1745 } 1746 1747 /* print out usage message, and then exit */ 1748 void 1749 usermgmt_usage(const char *prog) 1750 { 1751 if (strcmp(prog, "useradd") == 0) { 1752 (void)fprintf(stderr, "usage: %s -D [-F] [-b base-dir] " 1753 "[-e expiry-time] [-f inactive-time]\n" 1754 "\t[-g gid | name | =uid] [-k skel-dir] [-L login-class]\n" 1755 "\t[-M homeperm] [-r lowuid..highuid] [-s shell]\n", prog); 1756 (void)fprintf(stderr, "usage: %s [-moSv] [-b base-dir] " 1757 "[-c comment] [-d home-dir] [-e expiry-time]\n" 1758 "\t[-f inactive-time] [-G secondary-group] " 1759 "[-g gid | name | =uid]\n" 1760 "\t[-k skeletondir] [-L login-class] [-M homeperm] " 1761 "[-p password]\n" 1762 "\t[-r lowuid..highuid] [-s shell] [-u uid] user\n", 1763 prog); 1764 } else if (strcmp(prog, "usermod") == 0) { 1765 (void)fprintf(stderr, "usage: %s [-FmoSv] [-C yes/no] " 1766 "[-c comment] [-d home-dir] [-e expiry-time]\n" 1767 "\t[-f inactive] [-G secondary-group] " 1768 "[-g gid | name | =uid]\n" 1769 "\t[-L login-class] [-l new-login] [-p password] " 1770 "[-s shell] [-u uid]\n" 1771 "\tuser\n", prog); 1772 } else if (strcmp(prog, "userdel") == 0) { 1773 (void)fprintf(stderr, "usage: %s -D [-p preserve-value]\n", prog); 1774 (void)fprintf(stderr, 1775 "usage: %s [-rSv] [-p preserve-value] user\n", prog); 1776 #ifdef EXTENSIONS 1777 } else if (strcmp(prog, "userinfo") == 0) { 1778 (void)fprintf(stderr, "usage: %s [-e] user\n", prog); 1779 #endif 1780 } else if (strcmp(prog, "groupadd") == 0) { 1781 (void)fprintf(stderr, "usage: %s [-ov] [-g gid]" 1782 " [-r lowgid..highgid] group\n", prog); 1783 } else if (strcmp(prog, "groupdel") == 0) { 1784 (void)fprintf(stderr, "usage: %s [-v] group\n", prog); 1785 } else if (strcmp(prog, "groupmod") == 0) { 1786 (void)fprintf(stderr, 1787 "usage: %s [-ov] [-g gid] [-n newname] group\n", prog); 1788 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) { 1789 (void)fprintf(stderr, 1790 "usage: %s ( add | del | mod | info ) ...\n", prog); 1791 #ifdef EXTENSIONS 1792 } else if (strcmp(prog, "groupinfo") == 0) { 1793 (void)fprintf(stderr, "usage: %s [-ev] group\n", prog); 1794 #endif 1795 } 1796 exit(EXIT_FAILURE); 1797 /* NOTREACHED */ 1798 } 1799 1800 #ifdef EXTENSIONS 1801 #define ADD_OPT_EXTENSIONS "M:p:r:vL:S" 1802 #else 1803 #define ADD_OPT_EXTENSIONS 1804 #endif 1805 1806 int 1807 useradd(int argc, char **argv) 1808 { 1809 user_t u; 1810 int defaultfield; 1811 int bigD; 1812 int c; 1813 #ifdef EXTENSIONS 1814 int i; 1815 #endif 1816 1817 (void)memset(&u, 0, sizeof(u)); 1818 read_defaults(&u); 1819 u.u_uid = -1; 1820 defaultfield = bigD = 0; 1821 while ((c = getopt(argc, argv, "DFG:b:c:d:e:f:g:k:mou:s:" 1822 ADD_OPT_EXTENSIONS)) != -1) { 1823 switch(c) { 1824 case 'D': 1825 bigD = 1; 1826 break; 1827 case 'F': 1828 /* 1829 * Setting -1 will force the new user to 1830 * change their password as soon as they 1831 * next log in - passwd(5). 1832 */ 1833 defaultfield = 1; 1834 memsave(&u.u_inactive, "-1", strlen("-1")); 1835 break; 1836 case 'G': 1837 while (u.u_groupc < NGROUPS_MAX && 1838 (u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL) { 1839 if (u.u_groupv[u.u_groupc][0] != 0) { 1840 u.u_groupc++; 1841 } 1842 } 1843 if (optarg != NULL) { 1844 warnx("Truncated list of secondary groups " 1845 "to %d entries", NGROUPS_MAX); 1846 } 1847 break; 1848 #ifdef EXTENSIONS 1849 case 'S': 1850 u.u_allow_samba = 1; 1851 break; 1852 #endif 1853 case 'b': 1854 defaultfield = 1; 1855 memsave(&u.u_basedir, optarg, strlen(optarg)); 1856 break; 1857 case 'c': 1858 memsave(&u.u_comment, optarg, strlen(optarg)); 1859 break; 1860 case 'd': 1861 memsave(&u.u_home, optarg, strlen(optarg)); 1862 u.u_flags |= F_HOMEDIR; 1863 break; 1864 case 'e': 1865 defaultfield = 1; 1866 memsave(&u.u_expire, optarg, strlen(optarg)); 1867 break; 1868 case 'f': 1869 defaultfield = 1; 1870 memsave(&u.u_inactive, optarg, strlen(optarg)); 1871 break; 1872 case 'g': 1873 defaultfield = 1; 1874 memsave(&u.u_primgrp, optarg, strlen(optarg)); 1875 break; 1876 case 'k': 1877 defaultfield = 1; 1878 memsave(&u.u_skeldir, optarg, strlen(optarg)); 1879 break; 1880 #ifdef EXTENSIONS 1881 case 'L': 1882 defaultfield = 1; 1883 memsave(&u.u_class, optarg, strlen(optarg)); 1884 break; 1885 #endif 1886 case 'm': 1887 u.u_flags |= F_MKDIR; 1888 break; 1889 #ifdef EXTENSIONS 1890 case 'M': 1891 defaultfield = 1; 1892 u.u_homeperm = strtoul(optarg, NULL, 8); 1893 break; 1894 #endif 1895 case 'o': 1896 u.u_flags |= F_DUPUID; 1897 break; 1898 #ifdef EXTENSIONS 1899 case 'p': 1900 memsave(&u.u_password, optarg, strlen(optarg)); 1901 break; 1902 #endif 1903 #ifdef EXTENSIONS 1904 case 'r': 1905 defaultfield = 1; 1906 (void)save_range(&u, optarg); 1907 break; 1908 #endif 1909 case 's': 1910 u.u_flags |= F_SHELL; 1911 defaultfield = 1; 1912 memsave(&u.u_shell, optarg, strlen(optarg)); 1913 break; 1914 case 'u': 1915 u.u_uid = check_numeric(optarg, "uid"); 1916 break; 1917 #ifdef EXTENSIONS 1918 case 'v': 1919 verbose = 1; 1920 break; 1921 #endif 1922 default: 1923 usermgmt_usage("useradd"); 1924 /* NOTREACHED */ 1925 } 1926 } 1927 if (bigD) { 1928 if (defaultfield) { 1929 checkeuid(); 1930 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 1931 } 1932 (void)printf("group\t\t%s\n", u.u_primgrp); 1933 (void)printf("base_dir\t%s\n", u.u_basedir); 1934 (void)printf("skel_dir\t%s\n", u.u_skeldir); 1935 (void)printf("shell\t\t%s\n", u.u_shell); 1936 #ifdef EXTENSIONS 1937 (void)printf("class\t\t%s\n", u.u_class); 1938 (void)printf("homeperm\t0%o\n", u.u_homeperm); 1939 #endif 1940 (void)printf("inactive\t%s\n", (u.u_inactive == NULL) ? 1941 UNSET_INACTIVE : u.u_inactive); 1942 (void)printf("expire\t\t%s\n", (u.u_expire == NULL) ? 1943 UNSET_EXPIRY : u.u_expire); 1944 #ifdef EXTENSIONS 1945 for (i = 0 ; i < u.u_rc ; i++) { 1946 (void)printf("range\t\t%d..%d\n", 1947 u.u_rv[i].r_from, u.u_rv[i].r_to); 1948 } 1949 #endif 1950 return EXIT_SUCCESS; 1951 } 1952 argc -= optind; 1953 argv += optind; 1954 if (argc != 1) { 1955 usermgmt_usage("useradd"); 1956 } 1957 checkeuid(); 1958 openlog("useradd", LOG_PID, LOG_USER); 1959 return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE; 1960 } 1961 1962 #ifdef EXTENSIONS 1963 #define MOD_OPT_EXTENSIONS "p:vL:S" 1964 #else 1965 #define MOD_OPT_EXTENSIONS 1966 #endif 1967 1968 int 1969 usermod(int argc, char **argv) 1970 { 1971 user_t u; 1972 char newuser[MaxUserNameLen + 1]; 1973 int c, have_new_user; 1974 1975 (void)memset(&u, 0, sizeof(u)); 1976 (void)memset(newuser, 0, sizeof(newuser)); 1977 read_defaults(&u); 1978 have_new_user = 0; 1979 u.u_locked = -1; 1980 while ((c = getopt(argc, argv, "C:FG:c:d:e:f:g:l:mos:u:" 1981 MOD_OPT_EXTENSIONS)) != -1) { 1982 switch(c) { 1983 case 'G': 1984 while (u.u_groupc < NGROUPS_MAX && 1985 (u.u_groupv[u.u_groupc] = 1986 strsep(&optarg, ",")) != NULL) { 1987 if (u.u_groupv[u.u_groupc][0] != 0) { 1988 u.u_groupc++; 1989 } 1990 } 1991 if (optarg != NULL) { 1992 warnx("Truncated list of secondary groups " 1993 "to %d entries", NGROUPS_MAX); 1994 } 1995 u.u_flags |= F_SECGROUP; 1996 break; 1997 #ifdef EXTENSIONS 1998 case 'S': 1999 u.u_allow_samba = 1; 2000 break; 2001 #endif 2002 case 'c': 2003 memsave(&u.u_comment, optarg, strlen(optarg)); 2004 u.u_flags |= F_COMMENT; 2005 break; 2006 case 'C': 2007 if (strcasecmp(optarg, "yes") == 0) { 2008 u.u_locked = LOCK; 2009 } else if (strcasecmp(optarg, "no") == 0) { 2010 u.u_locked = UNLOCK; 2011 } else { 2012 /* No idea. */ 2013 errx(EXIT_FAILURE, 2014 "Please type 'yes' or 'no'"); 2015 } 2016 break; 2017 case 'F': 2018 memsave(&u.u_inactive, "-1", strlen("-1")); 2019 u.u_flags |= F_INACTIVE; 2020 break; 2021 case 'd': 2022 memsave(&u.u_home, optarg, strlen(optarg)); 2023 u.u_flags |= F_HOMEDIR; 2024 break; 2025 case 'e': 2026 memsave(&u.u_expire, optarg, strlen(optarg)); 2027 u.u_flags |= F_EXPIRE; 2028 break; 2029 case 'f': 2030 memsave(&u.u_inactive, optarg, strlen(optarg)); 2031 u.u_flags |= F_INACTIVE; 2032 break; 2033 case 'g': 2034 memsave(&u.u_primgrp, optarg, strlen(optarg)); 2035 u.u_flags |= F_GROUP; 2036 break; 2037 case 'l': 2038 (void)strlcpy(newuser, optarg, sizeof(newuser)); 2039 have_new_user = 1; 2040 u.u_flags |= F_USERNAME; 2041 break; 2042 #ifdef EXTENSIONS 2043 case 'L': 2044 memsave(&u.u_class, optarg, strlen(optarg)); 2045 u.u_flags |= F_CLASS; 2046 break; 2047 #endif 2048 case 'm': 2049 u.u_flags |= F_MKDIR; 2050 break; 2051 case 'o': 2052 u.u_flags |= F_DUPUID; 2053 break; 2054 #ifdef EXTENSIONS 2055 case 'p': 2056 memsave(&u.u_password, optarg, strlen(optarg)); 2057 u.u_flags |= F_PASSWORD; 2058 break; 2059 #endif 2060 case 's': 2061 memsave(&u.u_shell, optarg, strlen(optarg)); 2062 u.u_flags |= F_SHELL; 2063 break; 2064 case 'u': 2065 u.u_uid = check_numeric(optarg, "uid"); 2066 u.u_flags |= F_UID; 2067 break; 2068 #ifdef EXTENSIONS 2069 case 'v': 2070 verbose = 1; 2071 break; 2072 #endif 2073 default: 2074 usermgmt_usage("usermod"); 2075 /* NOTREACHED */ 2076 } 2077 } 2078 if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) && 2079 !(u.u_flags & F_USERNAME)) { 2080 warnx("Option 'm' useless without 'd' or 'l' -- ignored"); 2081 u.u_flags &= ~F_MKDIR; 2082 } 2083 argc -= optind; 2084 argv += optind; 2085 if (argc != 1) { 2086 usermgmt_usage("usermod"); 2087 } 2088 checkeuid(); 2089 openlog("usermod", LOG_PID, LOG_USER); 2090 return moduser(*argv, (have_new_user) ? newuser : *argv, &u, 2091 u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE; 2092 } 2093 2094 #ifdef EXTENSIONS 2095 #define DEL_OPT_EXTENSIONS "Dp:vS" 2096 #else 2097 #define DEL_OPT_EXTENSIONS 2098 #endif 2099 2100 int 2101 userdel(int argc, char **argv) 2102 { 2103 struct passwd *pwp; 2104 user_t u; 2105 char password[PasswordLength + 1]; 2106 int defaultfield; 2107 int rmhome; 2108 int bigD; 2109 int c; 2110 2111 (void)memset(&u, 0, sizeof(u)); 2112 read_defaults(&u); 2113 defaultfield = bigD = rmhome = 0; 2114 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) { 2115 switch(c) { 2116 #ifdef EXTENSIONS 2117 case 'D': 2118 bigD = 1; 2119 break; 2120 #endif 2121 #ifdef EXTENSIONS 2122 case 'S': 2123 u.u_allow_samba = 1; 2124 break; 2125 #endif 2126 #ifdef EXTENSIONS 2127 case 'p': 2128 defaultfield = 1; 2129 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 : 2130 (strcmp(optarg, "yes") == 0) ? 1 : 2131 atoi(optarg); 2132 break; 2133 #endif 2134 case 'r': 2135 rmhome = 1; 2136 break; 2137 #ifdef EXTENSIONS 2138 case 'v': 2139 verbose = 1; 2140 break; 2141 #endif 2142 default: 2143 usermgmt_usage("userdel"); 2144 /* NOTREACHED */ 2145 } 2146 } 2147 #ifdef EXTENSIONS 2148 if (bigD) { 2149 if (defaultfield) { 2150 checkeuid(); 2151 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE; 2152 } 2153 (void)printf("preserve\t%s\n", (u.u_preserve) ? "true" : 2154 "false"); 2155 return EXIT_SUCCESS; 2156 } 2157 #endif 2158 argc -= optind; 2159 argv += optind; 2160 if (argc != 1) { 2161 usermgmt_usage("userdel"); 2162 } 2163 checkeuid(); 2164 if ((pwp = getpwnam(*argv)) == NULL) { 2165 warnx("No such user `%s'", *argv); 2166 return EXIT_FAILURE; 2167 } 2168 if (rmhome) { 2169 (void)removehomedir(pwp); 2170 } 2171 if (u.u_preserve) { 2172 u.u_flags |= F_SHELL; 2173 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN)); 2174 (void)memset(password, '*', DES_Len); 2175 password[DES_Len] = 0; 2176 memsave(&u.u_password, password, strlen(password)); 2177 u.u_flags |= F_PASSWORD; 2178 openlog("userdel", LOG_PID, LOG_USER); 2179 return moduser(*argv, *argv, &u, u.u_allow_samba) ? 2180 EXIT_SUCCESS : EXIT_FAILURE; 2181 } 2182 if (!rm_user_from_groups(*argv)) { 2183 return 0; 2184 } 2185 openlog("userdel", LOG_PID, LOG_USER); 2186 return moduser(*argv, *argv, NULL, u.u_allow_samba) ? 2187 EXIT_SUCCESS : EXIT_FAILURE; 2188 } 2189 2190 #ifdef EXTENSIONS 2191 #define GROUP_ADD_OPT_EXTENSIONS "r:v" 2192 #else 2193 #define GROUP_ADD_OPT_EXTENSIONS 2194 #endif 2195 2196 /* add a group */ 2197 int 2198 groupadd(int argc, char **argv) 2199 { 2200 int dupgid; 2201 int gid; 2202 int c; 2203 int lowgid; 2204 int highgid; 2205 2206 gid = -1; 2207 dupgid = 0; 2208 lowgid = LowGid; 2209 highgid = HighGid; 2210 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) { 2211 switch(c) { 2212 case 'g': 2213 gid = check_numeric(optarg, "gid"); 2214 break; 2215 case 'o': 2216 dupgid = 1; 2217 break; 2218 #ifdef EXTENSIONS 2219 case 'r': 2220 if (sscanf(optarg, "%d..%d", &lowgid, &highgid) != 2) { 2221 errx(EXIT_FAILURE, "Bad range `%s'", optarg); 2222 } 2223 break; 2224 case 'v': 2225 verbose = 1; 2226 break; 2227 #endif 2228 default: 2229 usermgmt_usage("groupadd"); 2230 /* NOTREACHED */ 2231 } 2232 } 2233 argc -= optind; 2234 argv += optind; 2235 if (argc != 1) { 2236 usermgmt_usage("groupadd"); 2237 } 2238 checkeuid(); 2239 if (gid < 0 && !getnextgid(&gid, lowgid, highgid)) { 2240 err(EXIT_FAILURE, "Can't add group: can't get next gid"); 2241 } 2242 if (!dupgid && getgrgid((gid_t) gid) != NULL) { 2243 errx(EXIT_FAILURE, "Can't add group: gid %d is a duplicate", 2244 gid); 2245 } 2246 if (!valid_group(*argv)) { 2247 warnx("Invalid group name `%s'", *argv); 2248 } 2249 openlog("groupadd", LOG_PID, LOG_USER); 2250 if (!creategid(*argv, gid, "")) 2251 exit(EXIT_FAILURE); 2252 2253 return EXIT_SUCCESS; 2254 } 2255 2256 #ifdef EXTENSIONS 2257 #define GROUP_DEL_OPT_EXTENSIONS "v" 2258 #else 2259 #define GROUP_DEL_OPT_EXTENSIONS 2260 #endif 2261 2262 /* remove a group */ 2263 int 2264 groupdel(int argc, char **argv) 2265 { 2266 int c; 2267 2268 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) { 2269 switch(c) { 2270 #ifdef EXTENSIONS 2271 case 'v': 2272 verbose = 1; 2273 break; 2274 #endif 2275 default: 2276 usermgmt_usage("groupdel"); 2277 /* NOTREACHED */ 2278 } 2279 } 2280 argc -= optind; 2281 argv += optind; 2282 if (argc != 1) { 2283 usermgmt_usage("groupdel"); 2284 } 2285 if (getgrnam(*argv) == NULL) { 2286 errx(EXIT_FAILURE, "No such group `%s'", *argv); 2287 } 2288 checkeuid(); 2289 openlog("groupdel", LOG_PID, LOG_USER); 2290 if (!modify_gid(*argv, NULL)) 2291 exit(EXIT_FAILURE); 2292 2293 return EXIT_SUCCESS; 2294 } 2295 2296 #ifdef EXTENSIONS 2297 #define GROUP_MOD_OPT_EXTENSIONS "v" 2298 #else 2299 #define GROUP_MOD_OPT_EXTENSIONS 2300 #endif 2301 2302 /* modify a group */ 2303 int 2304 groupmod(int argc, char **argv) 2305 { 2306 struct group *grp; 2307 char buf[MaxEntryLen]; 2308 char *newname; 2309 char **cpp; 2310 int dupgid; 2311 int gid; 2312 int cc; 2313 int c; 2314 2315 gid = -1; 2316 dupgid = 0; 2317 newname = NULL; 2318 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) { 2319 switch(c) { 2320 case 'g': 2321 gid = check_numeric(optarg, "gid"); 2322 break; 2323 case 'o': 2324 dupgid = 1; 2325 break; 2326 case 'n': 2327 memsave(&newname, optarg, strlen(optarg)); 2328 break; 2329 #ifdef EXTENSIONS 2330 case 'v': 2331 verbose = 1; 2332 break; 2333 #endif 2334 default: 2335 usermgmt_usage("groupmod"); 2336 /* NOTREACHED */ 2337 } 2338 } 2339 argc -= optind; 2340 argv += optind; 2341 if (argc != 1) { 2342 usermgmt_usage("groupmod"); 2343 } 2344 checkeuid(); 2345 if (gid < 0 && newname == NULL) { 2346 errx(EXIT_FAILURE, "Nothing to change"); 2347 } 2348 if (dupgid && gid < 0) { 2349 errx(EXIT_FAILURE, "Duplicate which gid?"); 2350 } 2351 if ((grp = find_group_info(*argv)) == NULL) { 2352 errx(EXIT_FAILURE, "Can't find group `%s' to modify", *argv); 2353 } 2354 if (!is_local(*argv, _PATH_GROUP)) { 2355 errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv); 2356 } 2357 if (newname != NULL && !valid_group(newname)) { 2358 warnx("Invalid group name `%s'", newname); 2359 } 2360 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:", 2361 (newname) ? newname : grp->gr_name, 2362 grp->gr_passwd, 2363 (gid < 0) ? grp->gr_gid : gid); 2364 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) { 2365 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp, 2366 (cpp[1] == NULL) ? "" : ","); 2367 } 2368 cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n"); 2369 if (newname != NULL) 2370 free(newname); 2371 openlog("groupmod", LOG_PID, LOG_USER); 2372 if (!modify_gid(*argv, buf)) 2373 exit(EXIT_FAILURE); 2374 2375 return EXIT_SUCCESS; 2376 } 2377 2378 #ifdef EXTENSIONS 2379 /* display user information */ 2380 int 2381 userinfo(int argc, char **argv) 2382 { 2383 struct passwd *pwp; 2384 struct group *grp; 2385 char buf[MaxEntryLen]; 2386 char **cpp; 2387 int exists; 2388 int cc; 2389 int i; 2390 2391 exists = 0; 2392 buf[0] = '\0'; 2393 while ((i = getopt(argc, argv, "e")) != -1) { 2394 switch(i) { 2395 case 'e': 2396 exists = 1; 2397 break; 2398 default: 2399 usermgmt_usage("userinfo"); 2400 /* NOTREACHED */ 2401 } 2402 } 2403 argc -= optind; 2404 argv += optind; 2405 if (argc != 1) { 2406 usermgmt_usage("userinfo"); 2407 } 2408 pwp = find_user_info(*argv); 2409 if (exists) { 2410 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE); 2411 } 2412 if (pwp == NULL) { 2413 errx(EXIT_FAILURE, "Can't find user `%s'", *argv); 2414 } 2415 (void)printf("login\t%s\n", pwp->pw_name); 2416 (void)printf("passwd\t%s\n", pwp->pw_passwd); 2417 (void)printf("uid\t%d\n", pwp->pw_uid); 2418 for (cc = 0 ; (grp = getgrent()) != NULL ; ) { 2419 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2420 if (strcmp(*cpp, *argv) == 0 && 2421 grp->gr_gid != pwp->pw_gid) { 2422 cc += snprintf(&buf[cc], sizeof(buf) - cc, 2423 "%s ", grp->gr_name); 2424 } 2425 } 2426 } 2427 if ((grp = getgrgid(pwp->pw_gid)) == NULL) { 2428 (void)printf("groups\t%d %s\n", pwp->pw_gid, buf); 2429 } else { 2430 (void)printf("groups\t%s %s\n", grp->gr_name, buf); 2431 } 2432 (void)printf("change\t%s", pwp->pw_change > 0 ? 2433 ctime(&pwp->pw_change) : pwp->pw_change == -1 ? 2434 "NEXT LOGIN\n" : "NEVER\n"); 2435 (void)printf("class\t%s\n", pwp->pw_class); 2436 (void)printf("gecos\t%s\n", pwp->pw_gecos); 2437 (void)printf("dir\t%s\n", pwp->pw_dir); 2438 (void)printf("shell\t%s\n", pwp->pw_shell); 2439 (void)printf("expire\t%s", pwp->pw_expire ? 2440 ctime(&pwp->pw_expire) : "NEVER\n"); 2441 return EXIT_SUCCESS; 2442 } 2443 #endif 2444 2445 #ifdef EXTENSIONS 2446 /* display user information */ 2447 int 2448 groupinfo(int argc, char **argv) 2449 { 2450 struct group *grp; 2451 char **cpp; 2452 int exists; 2453 int i; 2454 2455 exists = 0; 2456 while ((i = getopt(argc, argv, "ev")) != -1) { 2457 switch(i) { 2458 case 'e': 2459 exists = 1; 2460 break; 2461 case 'v': 2462 verbose = 1; 2463 break; 2464 default: 2465 usermgmt_usage("groupinfo"); 2466 /* NOTREACHED */ 2467 } 2468 } 2469 argc -= optind; 2470 argv += optind; 2471 if (argc != 1) { 2472 usermgmt_usage("groupinfo"); 2473 } 2474 grp = find_group_info(*argv); 2475 if (exists) { 2476 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE); 2477 } 2478 if (grp == NULL) { 2479 errx(EXIT_FAILURE, "Can't find group `%s'", *argv); 2480 } 2481 (void)printf("name\t%s\n", grp->gr_name); 2482 (void)printf("passwd\t%s\n", grp->gr_passwd); 2483 (void)printf("gid\t%d\n", grp->gr_gid); 2484 (void)printf("members\t"); 2485 for (cpp = grp->gr_mem ; *cpp ; cpp++) { 2486 (void)printf("%s", *cpp); 2487 if (*(cpp + 1)) { 2488 (void) printf(", "); 2489 } 2490 } 2491 (void)fputc('\n', stdout); 2492 return EXIT_SUCCESS; 2493 } 2494 #endif 2495