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